2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_shared.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_extent_busy.h"
36 #include "xfs_error.h"
37 #include "xfs_cksum.h"
38 #include "xfs_trace.h"
39 #include "xfs_buf_item.h"
41 struct workqueue_struct *xfs_alloc_wq;
43 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
45 #define XFSA_FIXUP_BNO_OK 1
46 #define XFSA_FIXUP_CNT_OK 2
48 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
49 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
50 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
51 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
52 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
55 * Lookup the record equal to [bno, len] in the btree given by cur.
57 STATIC int /* error */
59 struct xfs_btree_cur *cur, /* btree cursor */
60 xfs_agblock_t bno, /* starting block of extent */
61 xfs_extlen_t len, /* length of extent */
62 int *stat) /* success/failure */
64 cur->bc_rec.a.ar_startblock = bno;
65 cur->bc_rec.a.ar_blockcount = len;
66 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
70 * Lookup the first record greater than or equal to [bno, len]
71 * in the btree given by cur.
75 struct xfs_btree_cur *cur, /* btree cursor */
76 xfs_agblock_t bno, /* starting block of extent */
77 xfs_extlen_t len, /* length of extent */
78 int *stat) /* success/failure */
80 cur->bc_rec.a.ar_startblock = bno;
81 cur->bc_rec.a.ar_blockcount = len;
82 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
86 * Lookup the first record less than or equal to [bno, len]
87 * in the btree given by cur.
91 struct xfs_btree_cur *cur, /* btree cursor */
92 xfs_agblock_t bno, /* starting block of extent */
93 xfs_extlen_t len, /* length of extent */
94 int *stat) /* success/failure */
96 cur->bc_rec.a.ar_startblock = bno;
97 cur->bc_rec.a.ar_blockcount = len;
98 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
102 * Update the record referred to by cur to the value given
104 * This either works (return 0) or gets an EFSCORRUPTED error.
106 STATIC int /* error */
108 struct xfs_btree_cur *cur, /* btree cursor */
109 xfs_agblock_t bno, /* starting block of extent */
110 xfs_extlen_t len) /* length of extent */
112 union xfs_btree_rec rec;
114 rec.alloc.ar_startblock = cpu_to_be32(bno);
115 rec.alloc.ar_blockcount = cpu_to_be32(len);
116 return xfs_btree_update(cur, &rec);
120 * Get the data from the pointed-to record.
124 struct xfs_btree_cur *cur, /* btree cursor */
125 xfs_agblock_t *bno, /* output: starting block of extent */
126 xfs_extlen_t *len, /* output: length of extent */
127 int *stat) /* output: success/failure */
129 union xfs_btree_rec *rec;
132 error = xfs_btree_get_rec(cur, &rec, stat);
133 if (!error && *stat == 1) {
134 *bno = be32_to_cpu(rec->alloc.ar_startblock);
135 *len = be32_to_cpu(rec->alloc.ar_blockcount);
141 * Compute aligned version of the found extent.
142 * Takes alignment and min length into account.
145 xfs_alloc_compute_aligned(
146 xfs_alloc_arg_t *args, /* allocation argument structure */
147 xfs_agblock_t foundbno, /* starting block in found extent */
148 xfs_extlen_t foundlen, /* length in found extent */
149 xfs_agblock_t *resbno, /* result block number */
150 xfs_extlen_t *reslen) /* result length */
155 /* Trim busy sections out of found extent */
156 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
158 if (args->alignment > 1 && len >= args->minlen) {
159 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
160 xfs_extlen_t diff = aligned_bno - bno;
162 *resbno = aligned_bno;
163 *reslen = diff >= len ? 0 : len - diff;
171 * Compute best start block and diff for "near" allocations.
172 * freelen >= wantlen already checked by caller.
174 STATIC xfs_extlen_t /* difference value (absolute) */
175 xfs_alloc_compute_diff(
176 xfs_agblock_t wantbno, /* target starting block */
177 xfs_extlen_t wantlen, /* target length */
178 xfs_extlen_t alignment, /* target alignment */
179 char userdata, /* are we allocating data? */
180 xfs_agblock_t freebno, /* freespace's starting block */
181 xfs_extlen_t freelen, /* freespace's length */
182 xfs_agblock_t *newbnop) /* result: best start block from free */
184 xfs_agblock_t freeend; /* end of freespace extent */
185 xfs_agblock_t newbno1; /* return block number */
186 xfs_agblock_t newbno2; /* other new block number */
187 xfs_extlen_t newlen1=0; /* length with newbno1 */
188 xfs_extlen_t newlen2=0; /* length with newbno2 */
189 xfs_agblock_t wantend; /* end of target extent */
191 ASSERT(freelen >= wantlen);
192 freeend = freebno + freelen;
193 wantend = wantbno + wantlen;
195 * We want to allocate from the start of a free extent if it is past
196 * the desired block or if we are allocating user data and the free
197 * extent is before desired block. The second case is there to allow
198 * for contiguous allocation from the remaining free space if the file
199 * grows in the short term.
201 if (freebno >= wantbno || (userdata && freeend < wantend)) {
202 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
203 newbno1 = NULLAGBLOCK;
204 } else if (freeend >= wantend && alignment > 1) {
205 newbno1 = roundup(wantbno, alignment);
206 newbno2 = newbno1 - alignment;
207 if (newbno1 >= freeend)
208 newbno1 = NULLAGBLOCK;
210 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
211 if (newbno2 < freebno)
212 newbno2 = NULLAGBLOCK;
214 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
215 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
216 if (newlen1 < newlen2 ||
217 (newlen1 == newlen2 &&
218 XFS_ABSDIFF(newbno1, wantbno) >
219 XFS_ABSDIFF(newbno2, wantbno)))
221 } else if (newbno2 != NULLAGBLOCK)
223 } else if (freeend >= wantend) {
225 } else if (alignment > 1) {
226 newbno1 = roundup(freeend - wantlen, alignment);
227 if (newbno1 > freeend - wantlen &&
228 newbno1 - alignment >= freebno)
229 newbno1 -= alignment;
230 else if (newbno1 >= freeend)
231 newbno1 = NULLAGBLOCK;
233 newbno1 = freeend - wantlen;
235 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
239 * Fix up the length, based on mod and prod.
240 * len should be k * prod + mod for some k.
241 * If len is too small it is returned unchanged.
242 * If len hits maxlen it is left alone.
246 xfs_alloc_arg_t *args) /* allocation argument structure */
251 ASSERT(args->mod < args->prod);
253 ASSERT(rlen >= args->minlen);
254 ASSERT(rlen <= args->maxlen);
255 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
256 (args->mod == 0 && rlen < args->prod))
258 k = rlen % args->prod;
262 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
265 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
269 ASSERT(rlen >= args->minlen);
270 ASSERT(rlen <= args->maxlen);
275 * Fix up length if there is too little space left in the a.g.
276 * Return 1 if ok, 0 if too little, should give up.
279 xfs_alloc_fix_minleft(
280 xfs_alloc_arg_t *args) /* allocation argument structure */
282 xfs_agf_t *agf; /* a.g. freelist header */
283 int diff; /* free space difference */
285 if (args->minleft == 0)
287 agf = XFS_BUF_TO_AGF(args->agbp);
288 diff = be32_to_cpu(agf->agf_freeblks)
289 - args->len - args->minleft;
292 args->len += diff; /* shrink the allocated space */
293 if (args->len >= args->minlen)
295 args->agbno = NULLAGBLOCK;
300 * Update the two btrees, logically removing from freespace the extent
301 * starting at rbno, rlen blocks. The extent is contained within the
302 * actual (current) free extent fbno for flen blocks.
303 * Flags are passed in indicating whether the cursors are set to the
306 STATIC int /* error code */
307 xfs_alloc_fixup_trees(
308 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
309 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
310 xfs_agblock_t fbno, /* starting block of free extent */
311 xfs_extlen_t flen, /* length of free extent */
312 xfs_agblock_t rbno, /* starting block of returned extent */
313 xfs_extlen_t rlen, /* length of returned extent */
314 int flags) /* flags, XFSA_FIXUP_... */
316 int error; /* error code */
317 int i; /* operation results */
318 xfs_agblock_t nfbno1; /* first new free startblock */
319 xfs_agblock_t nfbno2; /* second new free startblock */
320 xfs_extlen_t nflen1=0; /* first new free length */
321 xfs_extlen_t nflen2=0; /* second new free length */
324 * Look up the record in the by-size tree if necessary.
326 if (flags & XFSA_FIXUP_CNT_OK) {
328 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
330 XFS_WANT_CORRUPTED_RETURN(
331 i == 1 && nfbno1 == fbno && nflen1 == flen);
334 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
336 XFS_WANT_CORRUPTED_RETURN(i == 1);
339 * Look up the record in the by-block tree if necessary.
341 if (flags & XFSA_FIXUP_BNO_OK) {
343 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
345 XFS_WANT_CORRUPTED_RETURN(
346 i == 1 && nfbno1 == fbno && nflen1 == flen);
349 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
351 XFS_WANT_CORRUPTED_RETURN(i == 1);
355 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
356 struct xfs_btree_block *bnoblock;
357 struct xfs_btree_block *cntblock;
359 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
360 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
362 XFS_WANT_CORRUPTED_RETURN(
363 bnoblock->bb_numrecs == cntblock->bb_numrecs);
368 * Deal with all four cases: the allocated record is contained
369 * within the freespace record, so we can have new freespace
370 * at either (or both) end, or no freespace remaining.
372 if (rbno == fbno && rlen == flen)
373 nfbno1 = nfbno2 = NULLAGBLOCK;
374 else if (rbno == fbno) {
375 nfbno1 = rbno + rlen;
376 nflen1 = flen - rlen;
377 nfbno2 = NULLAGBLOCK;
378 } else if (rbno + rlen == fbno + flen) {
380 nflen1 = flen - rlen;
381 nfbno2 = NULLAGBLOCK;
384 nflen1 = rbno - fbno;
385 nfbno2 = rbno + rlen;
386 nflen2 = (fbno + flen) - nfbno2;
389 * Delete the entry from the by-size btree.
391 if ((error = xfs_btree_delete(cnt_cur, &i)))
393 XFS_WANT_CORRUPTED_RETURN(i == 1);
395 * Add new by-size btree entry(s).
397 if (nfbno1 != NULLAGBLOCK) {
398 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
400 XFS_WANT_CORRUPTED_RETURN(i == 0);
401 if ((error = xfs_btree_insert(cnt_cur, &i)))
403 XFS_WANT_CORRUPTED_RETURN(i == 1);
405 if (nfbno2 != NULLAGBLOCK) {
406 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
408 XFS_WANT_CORRUPTED_RETURN(i == 0);
409 if ((error = xfs_btree_insert(cnt_cur, &i)))
411 XFS_WANT_CORRUPTED_RETURN(i == 1);
414 * Fix up the by-block btree entry(s).
416 if (nfbno1 == NULLAGBLOCK) {
418 * No remaining freespace, just delete the by-block tree entry.
420 if ((error = xfs_btree_delete(bno_cur, &i)))
422 XFS_WANT_CORRUPTED_RETURN(i == 1);
425 * Update the by-block entry to start later|be shorter.
427 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
430 if (nfbno2 != NULLAGBLOCK) {
432 * 2 resulting free entries, need to add one.
434 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
436 XFS_WANT_CORRUPTED_RETURN(i == 0);
437 if ((error = xfs_btree_insert(bno_cur, &i)))
439 XFS_WANT_CORRUPTED_RETURN(i == 1);
448 struct xfs_mount *mp = bp->b_target->bt_mount;
449 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
452 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_uuid))
454 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
457 * during growfs operations, the perag is not fully initialised,
458 * so we can't use it for any useful checking. growfs ensures we can't
459 * use it by using uncached buffers that don't have the perag attached
460 * so we can detect and avoid this problem.
462 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
465 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
466 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
467 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
474 xfs_agfl_read_verify(
477 struct xfs_mount *mp = bp->b_target->bt_mount;
481 * There is no verification of non-crc AGFLs because mkfs does not
482 * initialise the AGFL to zero or NULL. Hence the only valid part of the
483 * AGFL is what the AGF says is active. We can't get to the AGF, so we
484 * can't verify just those entries are valid.
486 if (!xfs_sb_version_hascrc(&mp->m_sb))
489 agfl_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
490 offsetof(struct xfs_agfl, agfl_crc));
492 agfl_ok = agfl_ok && xfs_agfl_verify(bp);
495 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
496 xfs_buf_ioerror(bp, EFSCORRUPTED);
501 xfs_agfl_write_verify(
504 struct xfs_mount *mp = bp->b_target->bt_mount;
505 struct xfs_buf_log_item *bip = bp->b_fspriv;
507 /* no verification of non-crc AGFLs */
508 if (!xfs_sb_version_hascrc(&mp->m_sb))
511 if (!xfs_agfl_verify(bp)) {
512 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
513 xfs_buf_ioerror(bp, EFSCORRUPTED);
518 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
520 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
521 offsetof(struct xfs_agfl, agfl_crc));
524 const struct xfs_buf_ops xfs_agfl_buf_ops = {
525 .verify_read = xfs_agfl_read_verify,
526 .verify_write = xfs_agfl_write_verify,
530 * Read in the allocation group free block array.
532 STATIC int /* error */
534 xfs_mount_t *mp, /* mount point structure */
535 xfs_trans_t *tp, /* transaction pointer */
536 xfs_agnumber_t agno, /* allocation group number */
537 xfs_buf_t **bpp) /* buffer for the ag free block array */
539 xfs_buf_t *bp; /* return value */
542 ASSERT(agno != NULLAGNUMBER);
543 error = xfs_trans_read_buf(
544 mp, tp, mp->m_ddev_targp,
545 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
546 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
549 ASSERT(!xfs_buf_geterror(bp));
550 xfs_buf_set_ref(bp, XFS_AGFL_REF);
556 xfs_alloc_update_counters(
557 struct xfs_trans *tp,
558 struct xfs_perag *pag,
559 struct xfs_buf *agbp,
562 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
564 pag->pagf_freeblks += len;
565 be32_add_cpu(&agf->agf_freeblks, len);
567 xfs_trans_agblocks_delta(tp, len);
568 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
569 be32_to_cpu(agf->agf_length)))
572 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
577 * Allocation group level functions.
581 * Allocate a variable extent in the allocation group agno.
582 * Type and bno are used to determine where in the allocation group the
584 * Extent's length (returned in *len) will be between minlen and maxlen,
585 * and of the form k * prod + mod unless there's nothing that large.
586 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
588 STATIC int /* error */
589 xfs_alloc_ag_vextent(
590 xfs_alloc_arg_t *args) /* argument structure for allocation */
594 ASSERT(args->minlen > 0);
595 ASSERT(args->maxlen > 0);
596 ASSERT(args->minlen <= args->maxlen);
597 ASSERT(args->mod < args->prod);
598 ASSERT(args->alignment > 0);
600 * Branch to correct routine based on the type.
603 switch (args->type) {
604 case XFS_ALLOCTYPE_THIS_AG:
605 error = xfs_alloc_ag_vextent_size(args);
607 case XFS_ALLOCTYPE_NEAR_BNO:
608 error = xfs_alloc_ag_vextent_near(args);
610 case XFS_ALLOCTYPE_THIS_BNO:
611 error = xfs_alloc_ag_vextent_exact(args);
618 if (error || args->agbno == NULLAGBLOCK)
621 ASSERT(args->len >= args->minlen);
622 ASSERT(args->len <= args->maxlen);
623 ASSERT(!args->wasfromfl || !args->isfl);
624 ASSERT(args->agbno % args->alignment == 0);
626 if (!args->wasfromfl) {
627 error = xfs_alloc_update_counters(args->tp, args->pag,
629 -((long)(args->len)));
633 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
634 args->agbno, args->len));
638 xfs_trans_mod_sb(args->tp, args->wasdel ?
639 XFS_TRANS_SB_RES_FDBLOCKS :
640 XFS_TRANS_SB_FDBLOCKS,
641 -((long)(args->len)));
644 XFS_STATS_INC(xs_allocx);
645 XFS_STATS_ADD(xs_allocb, args->len);
650 * Allocate a variable extent at exactly agno/bno.
651 * Extent's length (returned in *len) will be between minlen and maxlen,
652 * and of the form k * prod + mod unless there's nothing that large.
653 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
655 STATIC int /* error */
656 xfs_alloc_ag_vextent_exact(
657 xfs_alloc_arg_t *args) /* allocation argument structure */
659 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
660 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
662 xfs_agblock_t fbno; /* start block of found extent */
663 xfs_extlen_t flen; /* length of found extent */
664 xfs_agblock_t tbno; /* start block of trimmed extent */
665 xfs_extlen_t tlen; /* length of trimmed extent */
666 xfs_agblock_t tend; /* end block of trimmed extent */
667 int i; /* success/failure of operation */
669 ASSERT(args->alignment == 1);
672 * Allocate/initialize a cursor for the by-number freespace btree.
674 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
675 args->agno, XFS_BTNUM_BNO);
678 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
679 * Look for the closest free block <= bno, it must contain bno
680 * if any free block does.
682 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
689 * Grab the freespace record.
691 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
694 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
695 ASSERT(fbno <= args->agbno);
698 * Check for overlapping busy extents.
700 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
703 * Give up if the start of the extent is busy, or the freespace isn't
704 * long enough for the minimum request.
706 if (tbno > args->agbno)
708 if (tlen < args->minlen)
711 if (tend < args->agbno + args->minlen)
715 * End of extent will be smaller of the freespace end and the
716 * maximal requested end.
718 * Fix the length according to mod and prod if given.
720 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
722 xfs_alloc_fix_len(args);
723 if (!xfs_alloc_fix_minleft(args))
726 ASSERT(args->agbno + args->len <= tend);
729 * We are allocating agbno for args->len
730 * Allocate/initialize a cursor for the by-size btree.
732 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
733 args->agno, XFS_BTNUM_CNT);
734 ASSERT(args->agbno + args->len <=
735 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
736 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
737 args->len, XFSA_FIXUP_BNO_OK);
739 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
743 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
744 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
747 trace_xfs_alloc_exact_done(args);
751 /* Didn't find it, return null. */
752 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
753 args->agbno = NULLAGBLOCK;
754 trace_xfs_alloc_exact_notfound(args);
758 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
759 trace_xfs_alloc_exact_error(args);
764 * Search the btree in a given direction via the search cursor and compare
765 * the records found against the good extent we've already found.
768 xfs_alloc_find_best_extent(
769 struct xfs_alloc_arg *args, /* allocation argument structure */
770 struct xfs_btree_cur **gcur, /* good cursor */
771 struct xfs_btree_cur **scur, /* searching cursor */
772 xfs_agblock_t gdiff, /* difference for search comparison */
773 xfs_agblock_t *sbno, /* extent found by search */
774 xfs_extlen_t *slen, /* extent length */
775 xfs_agblock_t *sbnoa, /* aligned extent found by search */
776 xfs_extlen_t *slena, /* aligned extent length */
777 int dir) /* 0 = search right, 1 = search left */
784 /* The good extent is perfect, no need to search. */
789 * Look until we find a better one, run out of space or run off the end.
792 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
795 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
796 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
799 * The good extent is closer than this one.
802 if (*sbnoa >= args->agbno + gdiff)
805 if (*sbnoa <= args->agbno - gdiff)
810 * Same distance, compare length and pick the best.
812 if (*slena >= args->minlen) {
813 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
814 xfs_alloc_fix_len(args);
816 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
818 args->userdata, *sbnoa,
822 * Choose closer size and invalidate other cursor.
830 error = xfs_btree_increment(*scur, 0, &i);
832 error = xfs_btree_decrement(*scur, 0, &i);
838 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
843 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
848 /* caller invalidates cursors */
853 * Allocate a variable extent near bno in the allocation group agno.
854 * Extent's length (returned in len) will be between minlen and maxlen,
855 * and of the form k * prod + mod unless there's nothing that large.
856 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
858 STATIC int /* error */
859 xfs_alloc_ag_vextent_near(
860 xfs_alloc_arg_t *args) /* allocation argument structure */
862 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
863 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
864 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
865 xfs_agblock_t gtbno; /* start bno of right side entry */
866 xfs_agblock_t gtbnoa; /* aligned ... */
867 xfs_extlen_t gtdiff; /* difference to right side entry */
868 xfs_extlen_t gtlen; /* length of right side entry */
869 xfs_extlen_t gtlena; /* aligned ... */
870 xfs_agblock_t gtnew; /* useful start bno of right side */
871 int error; /* error code */
872 int i; /* result code, temporary */
873 int j; /* result code, temporary */
874 xfs_agblock_t ltbno; /* start bno of left side entry */
875 xfs_agblock_t ltbnoa; /* aligned ... */
876 xfs_extlen_t ltdiff; /* difference to left side entry */
877 xfs_extlen_t ltlen; /* length of left side entry */
878 xfs_extlen_t ltlena; /* aligned ... */
879 xfs_agblock_t ltnew; /* useful start bno of left side */
880 xfs_extlen_t rlen; /* length of returned extent */
884 * Randomly don't execute the first algorithm.
886 int dofirst; /* set to do first algorithm */
888 dofirst = prandom_u32() & 1;
899 * Get a cursor for the by-size btree.
901 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
902 args->agno, XFS_BTNUM_CNT);
905 * See if there are any free extents as big as maxlen.
907 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
910 * If none, then pick up the last entry in the tree unless the
914 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
917 if (i == 0 || ltlen == 0) {
918 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
919 trace_xfs_alloc_near_noentry(args);
928 * If the requested extent is large wrt the freespaces available
929 * in this a.g., then the cursor will be pointing to a btree entry
930 * near the right edge of the tree. If it's in the last btree leaf
931 * block, then we just examine all the entries in that block
932 * that are big enough, and pick the best one.
933 * This is written as a while loop so we can break out of it,
934 * but we never loop back to the top.
936 while (xfs_btree_islastblock(cnt_cur, 0)) {
940 xfs_agblock_t bnew=0;
947 * Start from the entry that lookup found, sequence through
948 * all larger free blocks. If we're actually pointing at a
949 * record smaller than maxlen, go to the start of this block,
950 * and skip all those smaller than minlen.
952 if (ltlen || args->alignment > 1) {
953 cnt_cur->bc_ptrs[0] = 1;
955 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
958 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
959 if (ltlen >= args->minlen)
961 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
964 ASSERT(ltlen >= args->minlen);
968 i = cnt_cur->bc_ptrs[0];
969 for (j = 1, blen = 0, bdiff = 0;
970 !error && j && (blen < args->maxlen || bdiff > 0);
971 error = xfs_btree_increment(cnt_cur, 0, &j)) {
973 * For each entry, decide if it's better than
974 * the previous best entry.
976 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
978 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
979 xfs_alloc_compute_aligned(args, ltbno, ltlen,
981 if (ltlena < args->minlen)
983 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
984 xfs_alloc_fix_len(args);
985 ASSERT(args->len >= args->minlen);
986 if (args->len < blen)
988 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
989 args->alignment, args->userdata, ltbnoa,
991 if (ltnew != NULLAGBLOCK &&
992 (args->len > blen || ltdiff < bdiff)) {
996 besti = cnt_cur->bc_ptrs[0];
1000 * It didn't work. We COULD be in a case where
1001 * there's a good record somewhere, so try again.
1006 * Point at the best entry, and retrieve it again.
1008 cnt_cur->bc_ptrs[0] = besti;
1009 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
1011 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1012 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1014 if (!xfs_alloc_fix_minleft(args)) {
1015 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1016 trace_xfs_alloc_near_nominleft(args);
1021 * We are allocating starting at bnew for blen blocks.
1024 ASSERT(bnew >= ltbno);
1025 ASSERT(bnew + blen <= ltbno + ltlen);
1027 * Set up a cursor for the by-bno tree.
1029 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1030 args->agbp, args->agno, XFS_BTNUM_BNO);
1032 * Fix up the btree entries.
1034 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1035 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1037 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1038 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1040 trace_xfs_alloc_near_first(args);
1045 * Search in the by-bno tree to the left and to the right
1046 * simultaneously, until in each case we find a space big enough,
1047 * or run into the edge of the tree. When we run into the edge,
1048 * we deallocate that cursor.
1049 * If both searches succeed, we compare the two spaces and pick
1051 * With alignment, it's possible for both to fail; the upper
1052 * level algorithm that picks allocation groups for allocations
1053 * is not supposed to do this.
1056 * Allocate and initialize the cursor for the leftward search.
1058 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1059 args->agno, XFS_BTNUM_BNO);
1061 * Lookup <= bno to find the leftward search's starting point.
1063 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1067 * Didn't find anything; use this cursor for the rightward
1070 bno_cur_gt = bno_cur_lt;
1074 * Found something. Duplicate the cursor for the rightward search.
1076 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1079 * Increment the cursor, so we will point at the entry just right
1080 * of the leftward entry if any, or to the leftmost entry.
1082 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1086 * It failed, there are no rightward entries.
1088 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1092 * Loop going left with the leftward cursor, right with the
1093 * rightward cursor, until either both directions give up or
1094 * we find an entry at least as big as minlen.
1098 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1100 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1101 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1103 if (ltlena >= args->minlen)
1105 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1108 xfs_btree_del_cursor(bno_cur_lt,
1114 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1116 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1117 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1119 if (gtlena >= args->minlen)
1121 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1124 xfs_btree_del_cursor(bno_cur_gt,
1129 } while (bno_cur_lt || bno_cur_gt);
1132 * Got both cursors still active, need to find better entry.
1134 if (bno_cur_lt && bno_cur_gt) {
1135 if (ltlena >= args->minlen) {
1137 * Left side is good, look for a right side entry.
1139 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1140 xfs_alloc_fix_len(args);
1141 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1142 args->alignment, args->userdata, ltbnoa,
1145 error = xfs_alloc_find_best_extent(args,
1146 &bno_cur_lt, &bno_cur_gt,
1147 ltdiff, >bno, >len,
1149 0 /* search right */);
1151 ASSERT(gtlena >= args->minlen);
1154 * Right side is good, look for a left side entry.
1156 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1157 xfs_alloc_fix_len(args);
1158 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1159 args->alignment, args->userdata, gtbnoa,
1162 error = xfs_alloc_find_best_extent(args,
1163 &bno_cur_gt, &bno_cur_lt,
1164 gtdiff, <bno, <len,
1166 1 /* search left */);
1174 * If we couldn't get anything, give up.
1176 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1177 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1180 trace_xfs_alloc_near_busy(args);
1181 xfs_log_force(args->mp, XFS_LOG_SYNC);
1184 trace_xfs_alloc_size_neither(args);
1185 args->agbno = NULLAGBLOCK;
1190 * At this point we have selected a freespace entry, either to the
1191 * left or to the right. If it's on the right, copy all the
1192 * useful variables to the "left" set so we only have one
1193 * copy of this code.
1196 bno_cur_lt = bno_cur_gt;
1207 * Fix up the length and compute the useful address.
1209 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1210 xfs_alloc_fix_len(args);
1211 if (!xfs_alloc_fix_minleft(args)) {
1212 trace_xfs_alloc_near_nominleft(args);
1213 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1214 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1218 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1219 args->userdata, ltbnoa, ltlena, <new);
1220 ASSERT(ltnew >= ltbno);
1221 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1222 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1223 args->agbno = ltnew;
1225 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1226 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1230 trace_xfs_alloc_near_greater(args);
1232 trace_xfs_alloc_near_lesser(args);
1234 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1235 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1239 trace_xfs_alloc_near_error(args);
1240 if (cnt_cur != NULL)
1241 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1242 if (bno_cur_lt != NULL)
1243 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1244 if (bno_cur_gt != NULL)
1245 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1250 * Allocate a variable extent anywhere in the allocation group agno.
1251 * Extent's length (returned in len) will be between minlen and maxlen,
1252 * and of the form k * prod + mod unless there's nothing that large.
1253 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1255 STATIC int /* error */
1256 xfs_alloc_ag_vextent_size(
1257 xfs_alloc_arg_t *args) /* allocation argument structure */
1259 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1260 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1261 int error; /* error result */
1262 xfs_agblock_t fbno; /* start of found freespace */
1263 xfs_extlen_t flen; /* length of found freespace */
1264 int i; /* temp status variable */
1265 xfs_agblock_t rbno; /* returned block number */
1266 xfs_extlen_t rlen; /* length of returned extent */
1271 * Allocate and initialize a cursor for the by-size btree.
1273 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1274 args->agno, XFS_BTNUM_CNT);
1278 * Look for an entry >= maxlen+alignment-1 blocks.
1280 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1281 args->maxlen + args->alignment - 1, &i)))
1285 * If none or we have busy extents that we cannot allocate from, then
1286 * we have to settle for a smaller extent. In the case that there are
1287 * no large extents, this will return the last entry in the tree unless
1288 * the tree is empty. In the case that there are only busy large
1289 * extents, this will return the largest small extent unless there
1290 * are no smaller extents available.
1292 if (!i || forced > 1) {
1293 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1297 if (i == 0 || flen == 0) {
1298 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1299 trace_xfs_alloc_size_noentry(args);
1303 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1306 * Search for a non-busy extent that is large enough.
1307 * If we are at low space, don't check, or if we fall of
1308 * the end of the btree, turn off the busy check and
1312 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1315 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1317 xfs_alloc_compute_aligned(args, fbno, flen,
1320 if (rlen >= args->maxlen)
1323 error = xfs_btree_increment(cnt_cur, 0, &i);
1328 * Our only valid extents must have been busy.
1329 * Make it unbusy by forcing the log out and
1330 * retrying. If we've been here before, forcing
1331 * the log isn't making the extents available,
1332 * which means they have probably been freed in
1333 * this transaction. In that case, we have to
1334 * give up on them and we'll attempt a minlen
1335 * allocation the next time around.
1337 xfs_btree_del_cursor(cnt_cur,
1339 trace_xfs_alloc_size_busy(args);
1341 xfs_log_force(args->mp, XFS_LOG_SYNC);
1348 * In the first case above, we got the last entry in the
1349 * by-size btree. Now we check to see if the space hits maxlen
1350 * once aligned; if not, we search left for something better.
1351 * This can't happen in the second case above.
1353 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1354 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1355 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1356 if (rlen < args->maxlen) {
1357 xfs_agblock_t bestfbno;
1358 xfs_extlen_t bestflen;
1359 xfs_agblock_t bestrbno;
1360 xfs_extlen_t bestrlen;
1367 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1371 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1374 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1375 if (flen < bestrlen)
1377 xfs_alloc_compute_aligned(args, fbno, flen,
1379 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1380 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1381 (rlen <= flen && rbno + rlen <= fbno + flen),
1383 if (rlen > bestrlen) {
1388 if (rlen == args->maxlen)
1392 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1395 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1401 args->wasfromfl = 0;
1403 * Fix up the length.
1406 if (rlen < args->minlen) {
1408 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1409 trace_xfs_alloc_size_busy(args);
1410 xfs_log_force(args->mp, XFS_LOG_SYNC);
1415 xfs_alloc_fix_len(args);
1417 if (!xfs_alloc_fix_minleft(args))
1420 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1422 * Allocate and initialize a cursor for the by-block tree.
1424 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1425 args->agno, XFS_BTNUM_BNO);
1426 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1427 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1429 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1430 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1431 cnt_cur = bno_cur = NULL;
1434 XFS_WANT_CORRUPTED_GOTO(
1435 args->agbno + args->len <=
1436 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1438 trace_xfs_alloc_size_done(args);
1442 trace_xfs_alloc_size_error(args);
1444 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1446 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1450 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1451 trace_xfs_alloc_size_nominleft(args);
1452 args->agbno = NULLAGBLOCK;
1457 * Deal with the case where only small freespaces remain.
1458 * Either return the contents of the last freespace record,
1459 * or allocate space from the freelist if there is nothing in the tree.
1461 STATIC int /* error */
1462 xfs_alloc_ag_vextent_small(
1463 xfs_alloc_arg_t *args, /* allocation argument structure */
1464 xfs_btree_cur_t *ccur, /* by-size cursor */
1465 xfs_agblock_t *fbnop, /* result block number */
1466 xfs_extlen_t *flenp, /* result length */
1467 int *stat) /* status: 0-freelist, 1-normal/none */
1474 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1477 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1479 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1482 * Nothing in the btree, try the freelist. Make sure
1483 * to respect minleft even when pulling from the
1486 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1487 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1489 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1492 if (fbno != NULLAGBLOCK) {
1493 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1496 if (args->userdata) {
1499 bp = xfs_btree_get_bufs(args->mp, args->tp,
1500 args->agno, fbno, 0);
1501 xfs_trans_binval(args->tp, bp);
1505 XFS_WANT_CORRUPTED_GOTO(
1506 args->agbno + args->len <=
1507 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1509 args->wasfromfl = 1;
1510 trace_xfs_alloc_small_freelist(args);
1515 * Nothing in the freelist.
1521 * Can't allocate from the freelist for some reason.
1528 * Can't do the allocation, give up.
1530 if (flen < args->minlen) {
1531 args->agbno = NULLAGBLOCK;
1532 trace_xfs_alloc_small_notenough(args);
1538 trace_xfs_alloc_small_done(args);
1542 trace_xfs_alloc_small_error(args);
1547 * Free the extent starting at agno/bno for length.
1549 STATIC int /* error */
1551 xfs_trans_t *tp, /* transaction pointer */
1552 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1553 xfs_agnumber_t agno, /* allocation group number */
1554 xfs_agblock_t bno, /* starting block number */
1555 xfs_extlen_t len, /* length of extent */
1556 int isfl) /* set if is freelist blocks - no sb acctg */
1558 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1559 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1560 int error; /* error return value */
1561 xfs_agblock_t gtbno; /* start of right neighbor block */
1562 xfs_extlen_t gtlen; /* length of right neighbor block */
1563 int haveleft; /* have a left neighbor block */
1564 int haveright; /* have a right neighbor block */
1565 int i; /* temp, result code */
1566 xfs_agblock_t ltbno; /* start of left neighbor block */
1567 xfs_extlen_t ltlen; /* length of left neighbor block */
1568 xfs_mount_t *mp; /* mount point struct for filesystem */
1569 xfs_agblock_t nbno; /* new starting block of freespace */
1570 xfs_extlen_t nlen; /* new length of freespace */
1571 xfs_perag_t *pag; /* per allocation group data */
1575 * Allocate and initialize a cursor for the by-block btree.
1577 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1580 * Look for a neighboring block on the left (lower block numbers)
1581 * that is contiguous with this space.
1583 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1587 * There is a block to our left.
1589 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1591 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1593 * It's not contiguous, though.
1595 if (ltbno + ltlen < bno)
1599 * If this failure happens the request to free this
1600 * space was invalid, it's (partly) already free.
1603 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1607 * Look for a neighboring block on the right (higher block numbers)
1608 * that is contiguous with this space.
1610 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1614 * There is a block to our right.
1616 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1618 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1620 * It's not contiguous, though.
1622 if (bno + len < gtbno)
1626 * If this failure happens the request to free this
1627 * space was invalid, it's (partly) already free.
1630 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1634 * Now allocate and initialize a cursor for the by-size tree.
1636 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1638 * Have both left and right contiguous neighbors.
1639 * Merge all three into a single free block.
1641 if (haveleft && haveright) {
1643 * Delete the old by-size entry on the left.
1645 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1647 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1648 if ((error = xfs_btree_delete(cnt_cur, &i)))
1650 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1652 * Delete the old by-size entry on the right.
1654 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1656 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1657 if ((error = xfs_btree_delete(cnt_cur, &i)))
1659 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1661 * Delete the old by-block entry for the right block.
1663 if ((error = xfs_btree_delete(bno_cur, &i)))
1665 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1667 * Move the by-block cursor back to the left neighbor.
1669 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1671 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1674 * Check that this is the right record: delete didn't
1675 * mangle the cursor.
1678 xfs_agblock_t xxbno;
1681 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1684 XFS_WANT_CORRUPTED_GOTO(
1685 i == 1 && xxbno == ltbno && xxlen == ltlen,
1690 * Update remaining by-block entry to the new, joined block.
1693 nlen = len + ltlen + gtlen;
1694 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1698 * Have only a left contiguous neighbor.
1699 * Merge it together with the new freespace.
1701 else if (haveleft) {
1703 * Delete the old by-size entry on the left.
1705 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1707 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1708 if ((error = xfs_btree_delete(cnt_cur, &i)))
1710 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1712 * Back up the by-block cursor to the left neighbor, and
1713 * update its length.
1715 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1717 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1720 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1724 * Have only a right contiguous neighbor.
1725 * Merge it together with the new freespace.
1727 else if (haveright) {
1729 * Delete the old by-size entry on the right.
1731 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1733 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1734 if ((error = xfs_btree_delete(cnt_cur, &i)))
1736 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1738 * Update the starting block and length of the right
1739 * neighbor in the by-block tree.
1743 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1747 * No contiguous neighbors.
1748 * Insert the new freespace into the by-block tree.
1753 if ((error = xfs_btree_insert(bno_cur, &i)))
1755 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1757 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1760 * In all cases we need to insert the new freespace in the by-size tree.
1762 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1764 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1765 if ((error = xfs_btree_insert(cnt_cur, &i)))
1767 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1768 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1772 * Update the freespace totals in the ag and superblock.
1774 pag = xfs_perag_get(mp, agno);
1775 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1781 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1782 XFS_STATS_INC(xs_freex);
1783 XFS_STATS_ADD(xs_freeb, len);
1785 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
1790 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
1792 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1799 * Visible (exported) allocation/free functions.
1800 * Some of these are used just by xfs_alloc_btree.c and this file.
1804 * Compute and fill in value of m_ag_maxlevels.
1807 xfs_alloc_compute_maxlevels(
1808 xfs_mount_t *mp) /* file system mount structure */
1816 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1817 minleafrecs = mp->m_alloc_mnr[0];
1818 minnoderecs = mp->m_alloc_mnr[1];
1819 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1820 for (level = 1; maxblocks > 1; level++)
1821 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1822 mp->m_ag_maxlevels = level;
1826 * Find the length of the longest extent in an AG.
1829 xfs_alloc_longest_free_extent(
1830 struct xfs_mount *mp,
1831 struct xfs_perag *pag)
1833 xfs_extlen_t need, delta = 0;
1835 need = XFS_MIN_FREELIST_PAG(pag, mp);
1836 if (need > pag->pagf_flcount)
1837 delta = need - pag->pagf_flcount;
1839 if (pag->pagf_longest > delta)
1840 return pag->pagf_longest - delta;
1841 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1845 * Decide whether to use this allocation group for this allocation.
1846 * If so, fix up the btree freelist's size.
1848 STATIC int /* error */
1849 xfs_alloc_fix_freelist(
1850 xfs_alloc_arg_t *args, /* allocation argument structure */
1851 int flags) /* XFS_ALLOC_FLAG_... */
1853 xfs_buf_t *agbp; /* agf buffer pointer */
1854 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1855 xfs_buf_t *agflbp;/* agfl buffer pointer */
1856 xfs_agblock_t bno; /* freelist block */
1857 xfs_extlen_t delta; /* new blocks needed in freelist */
1858 int error; /* error result code */
1859 xfs_extlen_t longest;/* longest extent in allocation group */
1860 xfs_mount_t *mp; /* file system mount point structure */
1861 xfs_extlen_t need; /* total blocks needed in freelist */
1862 xfs_perag_t *pag; /* per-ag information structure */
1863 xfs_alloc_arg_t targs; /* local allocation arguments */
1864 xfs_trans_t *tp; /* transaction pointer */
1870 if (!pag->pagf_init) {
1871 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1874 if (!pag->pagf_init) {
1875 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1876 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1884 * If this is a metadata preferred pag and we are user data
1885 * then try somewhere else if we are not being asked to
1886 * try harder at this point
1888 if (pag->pagf_metadata && args->userdata &&
1889 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1890 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1895 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1897 * If it looks like there isn't a long enough extent, or enough
1898 * total blocks, reject it.
1900 need = XFS_MIN_FREELIST_PAG(pag, mp);
1901 longest = xfs_alloc_longest_free_extent(mp, pag);
1902 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1904 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1905 need - args->total) < (int)args->minleft)) {
1907 xfs_trans_brelse(tp, agbp);
1914 * Get the a.g. freespace buffer.
1915 * Can fail if we're not blocking on locks, and it's held.
1918 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1922 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1923 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1929 * Figure out how many blocks we should have in the freelist.
1931 agf = XFS_BUF_TO_AGF(agbp);
1932 need = XFS_MIN_FREELIST(agf, mp);
1934 * If there isn't enough total or single-extent, reject it.
1936 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1937 delta = need > be32_to_cpu(agf->agf_flcount) ?
1938 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1939 longest = be32_to_cpu(agf->agf_longest);
1940 longest = (longest > delta) ? (longest - delta) :
1941 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1942 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1944 ((int)(be32_to_cpu(agf->agf_freeblks) +
1945 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1946 (int)args->minleft)) {
1947 xfs_trans_brelse(tp, agbp);
1953 * Make the freelist shorter if it's too long.
1955 while (be32_to_cpu(agf->agf_flcount) > need) {
1958 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1961 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1963 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1964 xfs_trans_binval(tp, bp);
1967 * Initialize the args structure.
1969 memset(&targs, 0, sizeof(targs));
1973 targs.agno = args->agno;
1974 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1975 targs.type = XFS_ALLOCTYPE_THIS_AG;
1977 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1980 * Make the freelist longer if it's too short.
1982 while (be32_to_cpu(agf->agf_flcount) < need) {
1984 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1986 * Allocate as many blocks as possible at once.
1988 if ((error = xfs_alloc_ag_vextent(&targs))) {
1989 xfs_trans_brelse(tp, agflbp);
1993 * Stop if we run out. Won't happen if callers are obeying
1994 * the restrictions correctly. Can happen for free calls
1995 * on a completely full ag.
1997 if (targs.agbno == NULLAGBLOCK) {
1998 if (flags & XFS_ALLOC_FLAG_FREEING)
2000 xfs_trans_brelse(tp, agflbp);
2005 * Put each allocated block on the list.
2007 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2008 error = xfs_alloc_put_freelist(tp, agbp,
2014 xfs_trans_brelse(tp, agflbp);
2020 * Get a block from the freelist.
2021 * Returns with the buffer for the block gotten.
2024 xfs_alloc_get_freelist(
2025 xfs_trans_t *tp, /* transaction pointer */
2026 xfs_buf_t *agbp, /* buffer containing the agf structure */
2027 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2028 int btreeblk) /* destination is a AGF btree */
2030 xfs_agf_t *agf; /* a.g. freespace structure */
2031 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2032 xfs_agblock_t bno; /* block number returned */
2036 xfs_mount_t *mp = tp->t_mountp;
2037 xfs_perag_t *pag; /* per allocation group data */
2040 * Freelist is empty, give up.
2042 agf = XFS_BUF_TO_AGF(agbp);
2043 if (!agf->agf_flcount) {
2044 *bnop = NULLAGBLOCK;
2048 * Read the array of free blocks.
2050 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2057 * Get the block number and update the data structures.
2059 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2060 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2061 be32_add_cpu(&agf->agf_flfirst, 1);
2062 xfs_trans_brelse(tp, agflbp);
2063 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2064 agf->agf_flfirst = 0;
2066 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2067 be32_add_cpu(&agf->agf_flcount, -1);
2068 xfs_trans_agflist_delta(tp, -1);
2069 pag->pagf_flcount--;
2072 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2074 be32_add_cpu(&agf->agf_btreeblks, 1);
2075 pag->pagf_btreeblks++;
2076 logflags |= XFS_AGF_BTREEBLKS;
2079 xfs_alloc_log_agf(tp, agbp, logflags);
2086 * Log the given fields from the agf structure.
2090 xfs_trans_t *tp, /* transaction pointer */
2091 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2092 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2094 int first; /* first byte offset */
2095 int last; /* last byte offset */
2096 static const short offsets[] = {
2097 offsetof(xfs_agf_t, agf_magicnum),
2098 offsetof(xfs_agf_t, agf_versionnum),
2099 offsetof(xfs_agf_t, agf_seqno),
2100 offsetof(xfs_agf_t, agf_length),
2101 offsetof(xfs_agf_t, agf_roots[0]),
2102 offsetof(xfs_agf_t, agf_levels[0]),
2103 offsetof(xfs_agf_t, agf_flfirst),
2104 offsetof(xfs_agf_t, agf_fllast),
2105 offsetof(xfs_agf_t, agf_flcount),
2106 offsetof(xfs_agf_t, agf_freeblks),
2107 offsetof(xfs_agf_t, agf_longest),
2108 offsetof(xfs_agf_t, agf_btreeblks),
2109 offsetof(xfs_agf_t, agf_uuid),
2113 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2115 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2117 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2118 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2122 * Interface for inode allocation to force the pag data to be initialized.
2125 xfs_alloc_pagf_init(
2126 xfs_mount_t *mp, /* file system mount structure */
2127 xfs_trans_t *tp, /* transaction pointer */
2128 xfs_agnumber_t agno, /* allocation group number */
2129 int flags) /* XFS_ALLOC_FLAGS_... */
2134 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2137 xfs_trans_brelse(tp, bp);
2142 * Put the block on the freelist for the allocation group.
2145 xfs_alloc_put_freelist(
2146 xfs_trans_t *tp, /* transaction pointer */
2147 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2148 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2149 xfs_agblock_t bno, /* block being freed */
2150 int btreeblk) /* block came from a AGF btree */
2152 xfs_agf_t *agf; /* a.g. freespace structure */
2153 __be32 *blockp;/* pointer to array entry */
2156 xfs_mount_t *mp; /* mount structure */
2157 xfs_perag_t *pag; /* per allocation group data */
2161 agf = XFS_BUF_TO_AGF(agbp);
2164 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2165 be32_to_cpu(agf->agf_seqno), &agflbp)))
2167 be32_add_cpu(&agf->agf_fllast, 1);
2168 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2169 agf->agf_fllast = 0;
2171 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2172 be32_add_cpu(&agf->agf_flcount, 1);
2173 xfs_trans_agflist_delta(tp, 1);
2174 pag->pagf_flcount++;
2176 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2178 be32_add_cpu(&agf->agf_btreeblks, -1);
2179 pag->pagf_btreeblks--;
2180 logflags |= XFS_AGF_BTREEBLKS;
2184 xfs_alloc_log_agf(tp, agbp, logflags);
2186 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2188 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2189 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2190 *blockp = cpu_to_be32(bno);
2191 startoff = (char *)blockp - (char *)agflbp->b_addr;
2193 xfs_alloc_log_agf(tp, agbp, logflags);
2195 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2196 xfs_trans_log_buf(tp, agflbp, startoff,
2197 startoff + sizeof(xfs_agblock_t) - 1);
2203 struct xfs_mount *mp,
2206 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
2208 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2209 !uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_uuid))
2212 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2213 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2214 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2215 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2216 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2217 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2221 * during growfs operations, the perag is not fully initialised,
2222 * so we can't use it for any useful checking. growfs ensures we can't
2223 * use it by using uncached buffers that don't have the perag attached
2224 * so we can detect and avoid this problem.
2226 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2229 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2230 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2238 xfs_agf_read_verify(
2241 struct xfs_mount *mp = bp->b_target->bt_mount;
2244 if (xfs_sb_version_hascrc(&mp->m_sb))
2245 agf_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
2246 offsetof(struct xfs_agf, agf_crc));
2248 agf_ok = agf_ok && xfs_agf_verify(mp, bp);
2250 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2251 XFS_RANDOM_ALLOC_READ_AGF))) {
2252 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2253 xfs_buf_ioerror(bp, EFSCORRUPTED);
2258 xfs_agf_write_verify(
2261 struct xfs_mount *mp = bp->b_target->bt_mount;
2262 struct xfs_buf_log_item *bip = bp->b_fspriv;
2264 if (!xfs_agf_verify(mp, bp)) {
2265 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2266 xfs_buf_ioerror(bp, EFSCORRUPTED);
2270 if (!xfs_sb_version_hascrc(&mp->m_sb))
2274 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2276 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
2277 offsetof(struct xfs_agf, agf_crc));
2280 const struct xfs_buf_ops xfs_agf_buf_ops = {
2281 .verify_read = xfs_agf_read_verify,
2282 .verify_write = xfs_agf_write_verify,
2286 * Read in the allocation group header (free/alloc section).
2290 struct xfs_mount *mp, /* mount point structure */
2291 struct xfs_trans *tp, /* transaction pointer */
2292 xfs_agnumber_t agno, /* allocation group number */
2293 int flags, /* XFS_BUF_ */
2294 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2298 ASSERT(agno != NULLAGNUMBER);
2299 error = xfs_trans_read_buf(
2300 mp, tp, mp->m_ddev_targp,
2301 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2302 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2308 ASSERT(!(*bpp)->b_error);
2309 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2314 * Read in the allocation group header (free/alloc section).
2318 struct xfs_mount *mp, /* mount point structure */
2319 struct xfs_trans *tp, /* transaction pointer */
2320 xfs_agnumber_t agno, /* allocation group number */
2321 int flags, /* XFS_ALLOC_FLAG_... */
2322 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2324 struct xfs_agf *agf; /* ag freelist header */
2325 struct xfs_perag *pag; /* per allocation group data */
2328 ASSERT(agno != NULLAGNUMBER);
2330 error = xfs_read_agf(mp, tp, agno,
2331 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2337 ASSERT(!(*bpp)->b_error);
2339 agf = XFS_BUF_TO_AGF(*bpp);
2340 pag = xfs_perag_get(mp, agno);
2341 if (!pag->pagf_init) {
2342 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2343 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2344 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2345 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2346 pag->pagf_levels[XFS_BTNUM_BNOi] =
2347 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2348 pag->pagf_levels[XFS_BTNUM_CNTi] =
2349 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2350 spin_lock_init(&pag->pagb_lock);
2351 pag->pagb_count = 0;
2352 pag->pagb_tree = RB_ROOT;
2356 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2357 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2358 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2359 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2360 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2361 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2362 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2363 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2364 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2372 * Allocate an extent (variable-size).
2373 * Depending on the allocation type, we either look in a single allocation
2374 * group or loop over the allocation groups to find the result.
2378 xfs_alloc_arg_t *args) /* allocation argument structure */
2380 xfs_agblock_t agsize; /* allocation group size */
2382 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2383 xfs_extlen_t minleft;/* minimum left value, temp copy */
2384 xfs_mount_t *mp; /* mount structure pointer */
2385 xfs_agnumber_t sagno; /* starting allocation group number */
2386 xfs_alloctype_t type; /* input allocation type */
2389 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2392 type = args->otype = args->type;
2393 args->agbno = NULLAGBLOCK;
2395 * Just fix this up, for the case where the last a.g. is shorter
2396 * (or there's only one a.g.) and the caller couldn't easily figure
2397 * that out (xfs_bmap_alloc).
2399 agsize = mp->m_sb.sb_agblocks;
2400 if (args->maxlen > agsize)
2401 args->maxlen = agsize;
2402 if (args->alignment == 0)
2403 args->alignment = 1;
2404 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2405 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2406 ASSERT(args->minlen <= args->maxlen);
2407 ASSERT(args->minlen <= agsize);
2408 ASSERT(args->mod < args->prod);
2409 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2410 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2411 args->minlen > args->maxlen || args->minlen > agsize ||
2412 args->mod >= args->prod) {
2413 args->fsbno = NULLFSBLOCK;
2414 trace_xfs_alloc_vextent_badargs(args);
2417 minleft = args->minleft;
2420 case XFS_ALLOCTYPE_THIS_AG:
2421 case XFS_ALLOCTYPE_NEAR_BNO:
2422 case XFS_ALLOCTYPE_THIS_BNO:
2424 * These three force us into a single a.g.
2426 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2427 args->pag = xfs_perag_get(mp, args->agno);
2429 error = xfs_alloc_fix_freelist(args, 0);
2430 args->minleft = minleft;
2432 trace_xfs_alloc_vextent_nofix(args);
2436 trace_xfs_alloc_vextent_noagbp(args);
2439 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2440 if ((error = xfs_alloc_ag_vextent(args)))
2443 case XFS_ALLOCTYPE_START_BNO:
2445 * Try near allocation first, then anywhere-in-ag after
2446 * the first a.g. fails.
2448 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2449 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2450 args->fsbno = XFS_AGB_TO_FSB(mp,
2451 ((mp->m_agfrotor / rotorstep) %
2452 mp->m_sb.sb_agcount), 0);
2455 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2456 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2458 case XFS_ALLOCTYPE_ANY_AG:
2459 case XFS_ALLOCTYPE_START_AG:
2460 case XFS_ALLOCTYPE_FIRST_AG:
2462 * Rotate through the allocation groups looking for a winner.
2464 if (type == XFS_ALLOCTYPE_ANY_AG) {
2466 * Start with the last place we left off.
2468 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2469 mp->m_sb.sb_agcount;
2470 args->type = XFS_ALLOCTYPE_THIS_AG;
2471 flags = XFS_ALLOC_FLAG_TRYLOCK;
2472 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2474 * Start with allocation group given by bno.
2476 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2477 args->type = XFS_ALLOCTYPE_THIS_AG;
2481 if (type == XFS_ALLOCTYPE_START_AG)
2482 args->type = XFS_ALLOCTYPE_THIS_AG;
2484 * Start with the given allocation group.
2486 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2487 flags = XFS_ALLOC_FLAG_TRYLOCK;
2490 * Loop over allocation groups twice; first time with
2491 * trylock set, second time without.
2494 args->pag = xfs_perag_get(mp, args->agno);
2495 if (no_min) args->minleft = 0;
2496 error = xfs_alloc_fix_freelist(args, flags);
2497 args->minleft = minleft;
2499 trace_xfs_alloc_vextent_nofix(args);
2503 * If we get a buffer back then the allocation will fly.
2506 if ((error = xfs_alloc_ag_vextent(args)))
2511 trace_xfs_alloc_vextent_loopfailed(args);
2514 * Didn't work, figure out the next iteration.
2516 if (args->agno == sagno &&
2517 type == XFS_ALLOCTYPE_START_BNO)
2518 args->type = XFS_ALLOCTYPE_THIS_AG;
2520 * For the first allocation, we can try any AG to get
2521 * space. However, if we already have allocated a
2522 * block, we don't want to try AGs whose number is below
2523 * sagno. Otherwise, we may end up with out-of-order
2524 * locking of AGF, which might cause deadlock.
2526 if (++(args->agno) == mp->m_sb.sb_agcount) {
2527 if (args->firstblock != NULLFSBLOCK)
2533 * Reached the starting a.g., must either be done
2534 * or switch to non-trylock mode.
2536 if (args->agno == sagno) {
2538 args->agbno = NULLAGBLOCK;
2539 trace_xfs_alloc_vextent_allfailed(args);
2546 if (type == XFS_ALLOCTYPE_START_BNO) {
2547 args->agbno = XFS_FSB_TO_AGBNO(mp,
2549 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2553 xfs_perag_put(args->pag);
2555 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2556 if (args->agno == sagno)
2557 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2558 (mp->m_sb.sb_agcount * rotorstep);
2560 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2561 (mp->m_sb.sb_agcount * rotorstep);
2568 if (args->agbno == NULLAGBLOCK)
2569 args->fsbno = NULLFSBLOCK;
2571 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2573 ASSERT(args->len >= args->minlen);
2574 ASSERT(args->len <= args->maxlen);
2575 ASSERT(args->agbno % args->alignment == 0);
2576 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2580 xfs_perag_put(args->pag);
2583 xfs_perag_put(args->pag);
2589 * Just break up the extent address and hand off to xfs_free_ag_extent
2590 * after fixing up the freelist.
2594 xfs_trans_t *tp, /* transaction pointer */
2595 xfs_fsblock_t bno, /* starting block number of extent */
2596 xfs_extlen_t len) /* length of extent */
2598 xfs_alloc_arg_t args;
2602 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2604 args.mp = tp->t_mountp;
2607 * validate that the block number is legal - the enables us to detect
2608 * and handle a silent filesystem corruption rather than crashing.
2610 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2611 if (args.agno >= args.mp->m_sb.sb_agcount)
2612 return EFSCORRUPTED;
2614 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2615 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2616 return EFSCORRUPTED;
2618 args.pag = xfs_perag_get(args.mp, args.agno);
2621 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2625 /* validate the extent size is legal now we have the agf locked */
2626 if (args.agbno + len >
2627 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2628 error = EFSCORRUPTED;
2632 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2634 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
2636 xfs_perag_put(args.pag);