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_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h"
39 #include "xfs_error.h"
40 #include "xfs_trace.h"
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
49 xfs_alloc_busy_search(struct xfs_mount *mp, xfs_agnumber_t agno,
50 xfs_agblock_t bno, xfs_extlen_t len);
53 * Prototypes for per-ag allocation routines
56 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
57 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
58 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
59 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
60 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
67 * Lookup the record equal to [bno, len] in the btree given by cur.
69 STATIC int /* error */
71 struct xfs_btree_cur *cur, /* btree cursor */
72 xfs_agblock_t bno, /* starting block of extent */
73 xfs_extlen_t len, /* length of extent */
74 int *stat) /* success/failure */
76 cur->bc_rec.a.ar_startblock = bno;
77 cur->bc_rec.a.ar_blockcount = len;
78 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
82 * Lookup the first record greater than or equal to [bno, len]
83 * in the btree given by cur.
85 STATIC int /* error */
87 struct xfs_btree_cur *cur, /* btree cursor */
88 xfs_agblock_t bno, /* starting block of extent */
89 xfs_extlen_t len, /* length of extent */
90 int *stat) /* success/failure */
92 cur->bc_rec.a.ar_startblock = bno;
93 cur->bc_rec.a.ar_blockcount = len;
94 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
98 * Lookup the first record less than or equal to [bno, len]
99 * in the btree given by cur.
101 STATIC int /* error */
103 struct xfs_btree_cur *cur, /* btree cursor */
104 xfs_agblock_t bno, /* starting block of extent */
105 xfs_extlen_t len, /* length of extent */
106 int *stat) /* success/failure */
108 cur->bc_rec.a.ar_startblock = bno;
109 cur->bc_rec.a.ar_blockcount = len;
110 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
114 * Update the record referred to by cur to the value given
116 * This either works (return 0) or gets an EFSCORRUPTED error.
118 STATIC int /* error */
120 struct xfs_btree_cur *cur, /* btree cursor */
121 xfs_agblock_t bno, /* starting block of extent */
122 xfs_extlen_t len) /* length of extent */
124 union xfs_btree_rec rec;
126 rec.alloc.ar_startblock = cpu_to_be32(bno);
127 rec.alloc.ar_blockcount = cpu_to_be32(len);
128 return xfs_btree_update(cur, &rec);
132 * Get the data from the pointed-to record.
134 STATIC int /* error */
136 struct xfs_btree_cur *cur, /* btree cursor */
137 xfs_agblock_t *bno, /* output: starting block of extent */
138 xfs_extlen_t *len, /* output: length of extent */
139 int *stat) /* output: success/failure */
141 union xfs_btree_rec *rec;
144 error = xfs_btree_get_rec(cur, &rec, stat);
145 if (!error && *stat == 1) {
146 *bno = be32_to_cpu(rec->alloc.ar_startblock);
147 *len = be32_to_cpu(rec->alloc.ar_blockcount);
153 * Compute aligned version of the found extent.
154 * Takes alignment and min length into account.
157 xfs_alloc_compute_aligned(
158 xfs_agblock_t foundbno, /* starting block in found extent */
159 xfs_extlen_t foundlen, /* length in found extent */
160 xfs_extlen_t alignment, /* alignment for allocation */
161 xfs_extlen_t minlen, /* minimum length for allocation */
162 xfs_agblock_t *resbno, /* result block number */
163 xfs_extlen_t *reslen) /* result length */
169 if (alignment > 1 && foundlen >= minlen) {
170 bno = roundup(foundbno, alignment);
171 diff = bno - foundbno;
172 len = diff >= foundlen ? 0 : foundlen - diff;
182 * Compute best start block and diff for "near" allocations.
183 * freelen >= wantlen already checked by caller.
185 STATIC xfs_extlen_t /* difference value (absolute) */
186 xfs_alloc_compute_diff(
187 xfs_agblock_t wantbno, /* target starting block */
188 xfs_extlen_t wantlen, /* target length */
189 xfs_extlen_t alignment, /* target alignment */
190 xfs_agblock_t freebno, /* freespace's starting block */
191 xfs_extlen_t freelen, /* freespace's length */
192 xfs_agblock_t *newbnop) /* result: best start block from free */
194 xfs_agblock_t freeend; /* end of freespace extent */
195 xfs_agblock_t newbno1; /* return block number */
196 xfs_agblock_t newbno2; /* other new block number */
197 xfs_extlen_t newlen1=0; /* length with newbno1 */
198 xfs_extlen_t newlen2=0; /* length with newbno2 */
199 xfs_agblock_t wantend; /* end of target extent */
201 ASSERT(freelen >= wantlen);
202 freeend = freebno + freelen;
203 wantend = wantbno + wantlen;
204 if (freebno >= wantbno) {
205 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
206 newbno1 = NULLAGBLOCK;
207 } else if (freeend >= wantend && alignment > 1) {
208 newbno1 = roundup(wantbno, alignment);
209 newbno2 = newbno1 - alignment;
210 if (newbno1 >= freeend)
211 newbno1 = NULLAGBLOCK;
213 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
214 if (newbno2 < freebno)
215 newbno2 = NULLAGBLOCK;
217 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
218 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
219 if (newlen1 < newlen2 ||
220 (newlen1 == newlen2 &&
221 XFS_ABSDIFF(newbno1, wantbno) >
222 XFS_ABSDIFF(newbno2, wantbno)))
224 } else if (newbno2 != NULLAGBLOCK)
226 } else if (freeend >= wantend) {
228 } else if (alignment > 1) {
229 newbno1 = roundup(freeend - wantlen, alignment);
230 if (newbno1 > freeend - wantlen &&
231 newbno1 - alignment >= freebno)
232 newbno1 -= alignment;
233 else if (newbno1 >= freeend)
234 newbno1 = NULLAGBLOCK;
236 newbno1 = freeend - wantlen;
238 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
242 * Fix up the length, based on mod and prod.
243 * len should be k * prod + mod for some k.
244 * If len is too small it is returned unchanged.
245 * If len hits maxlen it is left alone.
249 xfs_alloc_arg_t *args) /* allocation argument structure */
254 ASSERT(args->mod < args->prod);
256 ASSERT(rlen >= args->minlen);
257 ASSERT(rlen <= args->maxlen);
258 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
259 (args->mod == 0 && rlen < args->prod))
261 k = rlen % args->prod;
265 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
268 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
272 ASSERT(rlen >= args->minlen);
273 ASSERT(rlen <= args->maxlen);
278 * Fix up length if there is too little space left in the a.g.
279 * Return 1 if ok, 0 if too little, should give up.
282 xfs_alloc_fix_minleft(
283 xfs_alloc_arg_t *args) /* allocation argument structure */
285 xfs_agf_t *agf; /* a.g. freelist header */
286 int diff; /* free space difference */
288 if (args->minleft == 0)
290 agf = XFS_BUF_TO_AGF(args->agbp);
291 diff = be32_to_cpu(agf->agf_freeblks)
292 + be32_to_cpu(agf->agf_flcount)
293 - args->len - args->minleft;
296 args->len += diff; /* shrink the allocated space */
297 if (args->len >= args->minlen)
299 args->agbno = NULLAGBLOCK;
304 * Update the two btrees, logically removing from freespace the extent
305 * starting at rbno, rlen blocks. The extent is contained within the
306 * actual (current) free extent fbno for flen blocks.
307 * Flags are passed in indicating whether the cursors are set to the
310 STATIC int /* error code */
311 xfs_alloc_fixup_trees(
312 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
313 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
314 xfs_agblock_t fbno, /* starting block of free extent */
315 xfs_extlen_t flen, /* length of free extent */
316 xfs_agblock_t rbno, /* starting block of returned extent */
317 xfs_extlen_t rlen, /* length of returned extent */
318 int flags) /* flags, XFSA_FIXUP_... */
320 int error; /* error code */
321 int i; /* operation results */
322 xfs_agblock_t nfbno1; /* first new free startblock */
323 xfs_agblock_t nfbno2; /* second new free startblock */
324 xfs_extlen_t nflen1=0; /* first new free length */
325 xfs_extlen_t nflen2=0; /* second new free length */
328 * Look up the record in the by-size tree if necessary.
330 if (flags & XFSA_FIXUP_CNT_OK) {
332 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
334 XFS_WANT_CORRUPTED_RETURN(
335 i == 1 && nfbno1 == fbno && nflen1 == flen);
338 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
340 XFS_WANT_CORRUPTED_RETURN(i == 1);
343 * Look up the record in the by-block tree if necessary.
345 if (flags & XFSA_FIXUP_BNO_OK) {
347 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
349 XFS_WANT_CORRUPTED_RETURN(
350 i == 1 && nfbno1 == fbno && nflen1 == flen);
353 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
355 XFS_WANT_CORRUPTED_RETURN(i == 1);
359 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
360 struct xfs_btree_block *bnoblock;
361 struct xfs_btree_block *cntblock;
363 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
364 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
366 XFS_WANT_CORRUPTED_RETURN(
367 bnoblock->bb_numrecs == cntblock->bb_numrecs);
372 * Deal with all four cases: the allocated record is contained
373 * within the freespace record, so we can have new freespace
374 * at either (or both) end, or no freespace remaining.
376 if (rbno == fbno && rlen == flen)
377 nfbno1 = nfbno2 = NULLAGBLOCK;
378 else if (rbno == fbno) {
379 nfbno1 = rbno + rlen;
380 nflen1 = flen - rlen;
381 nfbno2 = NULLAGBLOCK;
382 } else if (rbno + rlen == fbno + flen) {
384 nflen1 = flen - rlen;
385 nfbno2 = NULLAGBLOCK;
388 nflen1 = rbno - fbno;
389 nfbno2 = rbno + rlen;
390 nflen2 = (fbno + flen) - nfbno2;
393 * Delete the entry from the by-size btree.
395 if ((error = xfs_btree_delete(cnt_cur, &i)))
397 XFS_WANT_CORRUPTED_RETURN(i == 1);
399 * Add new by-size btree entry(s).
401 if (nfbno1 != NULLAGBLOCK) {
402 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
404 XFS_WANT_CORRUPTED_RETURN(i == 0);
405 if ((error = xfs_btree_insert(cnt_cur, &i)))
407 XFS_WANT_CORRUPTED_RETURN(i == 1);
409 if (nfbno2 != NULLAGBLOCK) {
410 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
412 XFS_WANT_CORRUPTED_RETURN(i == 0);
413 if ((error = xfs_btree_insert(cnt_cur, &i)))
415 XFS_WANT_CORRUPTED_RETURN(i == 1);
418 * Fix up the by-block btree entry(s).
420 if (nfbno1 == NULLAGBLOCK) {
422 * No remaining freespace, just delete the by-block tree entry.
424 if ((error = xfs_btree_delete(bno_cur, &i)))
426 XFS_WANT_CORRUPTED_RETURN(i == 1);
429 * Update the by-block entry to start later|be shorter.
431 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
434 if (nfbno2 != NULLAGBLOCK) {
436 * 2 resulting free entries, need to add one.
438 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
440 XFS_WANT_CORRUPTED_RETURN(i == 0);
441 if ((error = xfs_btree_insert(bno_cur, &i)))
443 XFS_WANT_CORRUPTED_RETURN(i == 1);
449 * Read in the allocation group free block array.
451 STATIC int /* error */
453 xfs_mount_t *mp, /* mount point structure */
454 xfs_trans_t *tp, /* transaction pointer */
455 xfs_agnumber_t agno, /* allocation group number */
456 xfs_buf_t **bpp) /* buffer for the ag free block array */
458 xfs_buf_t *bp; /* return value */
461 ASSERT(agno != NULLAGNUMBER);
462 error = xfs_trans_read_buf(
463 mp, tp, mp->m_ddev_targp,
464 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
465 XFS_FSS_TO_BB(mp, 1), 0, &bp);
469 ASSERT(!XFS_BUF_GETERROR(bp));
470 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
476 * Allocation group level functions.
480 * Allocate a variable extent in the allocation group agno.
481 * Type and bno are used to determine where in the allocation group the
483 * Extent's length (returned in *len) will be between minlen and maxlen,
484 * and of the form k * prod + mod unless there's nothing that large.
485 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
487 STATIC int /* error */
488 xfs_alloc_ag_vextent(
489 xfs_alloc_arg_t *args) /* argument structure for allocation */
493 ASSERT(args->minlen > 0);
494 ASSERT(args->maxlen > 0);
495 ASSERT(args->minlen <= args->maxlen);
496 ASSERT(args->mod < args->prod);
497 ASSERT(args->alignment > 0);
499 * Branch to correct routine based on the type.
502 switch (args->type) {
503 case XFS_ALLOCTYPE_THIS_AG:
504 error = xfs_alloc_ag_vextent_size(args);
506 case XFS_ALLOCTYPE_NEAR_BNO:
507 error = xfs_alloc_ag_vextent_near(args);
509 case XFS_ALLOCTYPE_THIS_BNO:
510 error = xfs_alloc_ag_vextent_exact(args);
519 * If the allocation worked, need to change the agf structure
520 * (and log it), and the superblock.
522 if (args->agbno != NULLAGBLOCK) {
523 xfs_agf_t *agf; /* allocation group freelist header */
524 long slen = (long)args->len;
526 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
527 ASSERT(!(args->wasfromfl) || !args->isfl);
528 ASSERT(args->agbno % args->alignment == 0);
529 if (!(args->wasfromfl)) {
531 agf = XFS_BUF_TO_AGF(args->agbp);
532 be32_add_cpu(&agf->agf_freeblks, -(args->len));
533 xfs_trans_agblocks_delta(args->tp,
534 -((long)(args->len)));
535 args->pag->pagf_freeblks -= args->len;
536 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
537 be32_to_cpu(agf->agf_length));
538 xfs_alloc_log_agf(args->tp, args->agbp,
541 * Search the busylist for these blocks and mark the
542 * transaction as synchronous if blocks are found. This
543 * avoids the need to block due to a synchronous log
544 * force to ensure correct ordering as the synchronous
545 * transaction will guarantee that for us.
547 if (xfs_alloc_busy_search(args->mp, args->agno,
548 args->agbno, args->len))
549 xfs_trans_set_sync(args->tp);
552 xfs_trans_mod_sb(args->tp,
553 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
554 XFS_TRANS_SB_FDBLOCKS, -slen);
555 XFS_STATS_INC(xs_allocx);
556 XFS_STATS_ADD(xs_allocb, args->len);
562 * Allocate a variable extent at exactly agno/bno.
563 * Extent's length (returned in *len) will be between minlen and maxlen,
564 * and of the form k * prod + mod unless there's nothing that large.
565 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
567 STATIC int /* error */
568 xfs_alloc_ag_vextent_exact(
569 xfs_alloc_arg_t *args) /* allocation argument structure */
571 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
572 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
573 xfs_agblock_t end; /* end of allocated extent */
575 xfs_agblock_t fbno; /* start block of found extent */
576 xfs_agblock_t fend; /* end block of found extent */
577 xfs_extlen_t flen; /* length of found extent */
578 int i; /* success/failure of operation */
579 xfs_agblock_t maxend; /* end of maximal extent */
580 xfs_agblock_t minend; /* end of minimal extent */
581 xfs_extlen_t rlen; /* length of returned extent */
583 ASSERT(args->alignment == 1);
585 * Allocate/initialize a cursor for the by-number freespace btree.
587 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
588 args->agno, XFS_BTNUM_BNO);
590 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
591 * Look for the closest free block <= bno, it must contain bno
592 * if any free block does.
594 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
598 * Didn't find it, return null.
600 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
601 args->agbno = NULLAGBLOCK;
605 * Grab the freespace record.
607 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
609 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
610 ASSERT(fbno <= args->agbno);
611 minend = args->agbno + args->minlen;
612 maxend = args->agbno + args->maxlen;
615 * Give up if the freespace isn't long enough for the minimum request.
618 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
619 args->agbno = NULLAGBLOCK;
623 * End of extent will be smaller of the freespace end and the
624 * maximal requested end.
626 end = XFS_AGBLOCK_MIN(fend, maxend);
628 * Fix the length according to mod and prod if given.
630 args->len = end - args->agbno;
631 xfs_alloc_fix_len(args);
632 if (!xfs_alloc_fix_minleft(args)) {
633 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
637 ASSERT(args->agbno + rlen <= fend);
638 end = args->agbno + rlen;
640 * We are allocating agbno for rlen [agbno .. end]
641 * Allocate/initialize a cursor for the by-size btree.
643 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
644 args->agno, XFS_BTNUM_CNT);
645 ASSERT(args->agbno + args->len <=
646 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
647 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
648 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
649 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
652 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
653 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
655 trace_xfs_alloc_exact_done(args);
660 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
661 trace_xfs_alloc_exact_error(args);
666 * Allocate a variable extent near bno in the allocation group agno.
667 * Extent's length (returned in len) will be between minlen and maxlen,
668 * and of the form k * prod + mod unless there's nothing that large.
669 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
671 STATIC int /* error */
672 xfs_alloc_ag_vextent_near(
673 xfs_alloc_arg_t *args) /* allocation argument structure */
675 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
676 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
677 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
678 xfs_agblock_t gtbno; /* start bno of right side entry */
679 xfs_agblock_t gtbnoa; /* aligned ... */
680 xfs_extlen_t gtdiff; /* difference to right side entry */
681 xfs_extlen_t gtlen; /* length of right side entry */
682 xfs_extlen_t gtlena; /* aligned ... */
683 xfs_agblock_t gtnew; /* useful start bno of right side */
684 int error; /* error code */
685 int i; /* result code, temporary */
686 int j; /* result code, temporary */
687 xfs_agblock_t ltbno; /* start bno of left side entry */
688 xfs_agblock_t ltbnoa; /* aligned ... */
689 xfs_extlen_t ltdiff; /* difference to left side entry */
691 xfs_agblock_t ltend; /* end bno of left side entry */
692 xfs_extlen_t ltlen; /* length of left side entry */
693 xfs_extlen_t ltlena; /* aligned ... */
694 xfs_agblock_t ltnew; /* useful start bno of left side */
695 xfs_extlen_t rlen; /* length of returned extent */
696 #if defined(DEBUG) && defined(__KERNEL__)
698 * Randomly don't execute the first algorithm.
700 int dofirst; /* set to do first algorithm */
702 dofirst = random32() & 1;
705 * Get a cursor for the by-size btree.
707 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
708 args->agno, XFS_BTNUM_CNT);
710 bno_cur_lt = bno_cur_gt = NULL;
712 * See if there are any free extents as big as maxlen.
714 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
717 * If none, then pick up the last entry in the tree unless the
721 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
724 if (i == 0 || ltlen == 0) {
725 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
733 * If the requested extent is large wrt the freespaces available
734 * in this a.g., then the cursor will be pointing to a btree entry
735 * near the right edge of the tree. If it's in the last btree leaf
736 * block, then we just examine all the entries in that block
737 * that are big enough, and pick the best one.
738 * This is written as a while loop so we can break out of it,
739 * but we never loop back to the top.
741 while (xfs_btree_islastblock(cnt_cur, 0)) {
745 xfs_agblock_t bnew=0;
747 #if defined(DEBUG) && defined(__KERNEL__)
752 * Start from the entry that lookup found, sequence through
753 * all larger free blocks. If we're actually pointing at a
754 * record smaller than maxlen, go to the start of this block,
755 * and skip all those smaller than minlen.
757 if (ltlen || args->alignment > 1) {
758 cnt_cur->bc_ptrs[0] = 1;
760 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
763 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
764 if (ltlen >= args->minlen)
766 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
769 ASSERT(ltlen >= args->minlen);
773 i = cnt_cur->bc_ptrs[0];
774 for (j = 1, blen = 0, bdiff = 0;
775 !error && j && (blen < args->maxlen || bdiff > 0);
776 error = xfs_btree_increment(cnt_cur, 0, &j)) {
778 * For each entry, decide if it's better than
779 * the previous best entry.
781 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
783 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
784 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
785 args->minlen, <bnoa, <lena);
786 if (ltlena < args->minlen)
788 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
789 xfs_alloc_fix_len(args);
790 ASSERT(args->len >= args->minlen);
791 if (args->len < blen)
793 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
794 args->alignment, ltbno, ltlen, <new);
795 if (ltnew != NULLAGBLOCK &&
796 (args->len > blen || ltdiff < bdiff)) {
800 besti = cnt_cur->bc_ptrs[0];
804 * It didn't work. We COULD be in a case where
805 * there's a good record somewhere, so try again.
810 * Point at the best entry, and retrieve it again.
812 cnt_cur->bc_ptrs[0] = besti;
813 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
815 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
816 ltend = ltbno + ltlen;
817 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
819 if (!xfs_alloc_fix_minleft(args)) {
820 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
821 trace_xfs_alloc_near_nominleft(args);
826 * We are allocating starting at bnew for blen blocks.
829 ASSERT(bnew >= ltbno);
830 ASSERT(bnew + blen <= ltend);
832 * Set up a cursor for the by-bno tree.
834 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
835 args->agbp, args->agno, XFS_BTNUM_BNO);
837 * Fix up the btree entries.
839 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
840 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
842 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
843 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
845 trace_xfs_alloc_near_first(args);
850 * Search in the by-bno tree to the left and to the right
851 * simultaneously, until in each case we find a space big enough,
852 * or run into the edge of the tree. When we run into the edge,
853 * we deallocate that cursor.
854 * If both searches succeed, we compare the two spaces and pick
856 * With alignment, it's possible for both to fail; the upper
857 * level algorithm that picks allocation groups for allocations
858 * is not supposed to do this.
861 * Allocate and initialize the cursor for the leftward search.
863 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
864 args->agno, XFS_BTNUM_BNO);
866 * Lookup <= bno to find the leftward search's starting point.
868 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
872 * Didn't find anything; use this cursor for the rightward
875 bno_cur_gt = bno_cur_lt;
879 * Found something. Duplicate the cursor for the rightward search.
881 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
884 * Increment the cursor, so we will point at the entry just right
885 * of the leftward entry if any, or to the leftmost entry.
887 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
891 * It failed, there are no rightward entries.
893 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
897 * Loop going left with the leftward cursor, right with the
898 * rightward cursor, until either both directions give up or
899 * we find an entry at least as big as minlen.
903 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
905 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
906 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
907 args->minlen, <bnoa, <lena);
908 if (ltlena >= args->minlen)
910 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
913 xfs_btree_del_cursor(bno_cur_lt,
919 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
921 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
922 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
923 args->minlen, >bnoa, >lena);
924 if (gtlena >= args->minlen)
926 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
929 xfs_btree_del_cursor(bno_cur_gt,
934 } while (bno_cur_lt || bno_cur_gt);
936 * Got both cursors still active, need to find better entry.
938 if (bno_cur_lt && bno_cur_gt) {
940 * Left side is long enough, look for a right side entry.
942 if (ltlena >= args->minlen) {
946 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
947 xfs_alloc_fix_len(args);
949 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
950 args->alignment, ltbno, ltlen, <new);
956 * Look until we find a better one, run out of
957 * space, or run off the end.
959 while (bno_cur_lt && bno_cur_gt) {
960 if ((error = xfs_alloc_get_rec(
964 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
965 xfs_alloc_compute_aligned(gtbno, gtlen,
966 args->alignment, args->minlen,
969 * The left one is clearly better.
971 if (gtbnoa >= args->agbno + ltdiff) {
972 xfs_btree_del_cursor(
979 * If we reach a big enough entry,
980 * compare the two and pick the best.
982 if (gtlena >= args->minlen) {
984 XFS_EXTLEN_MIN(gtlena,
986 xfs_alloc_fix_len(args);
988 gtdiff = xfs_alloc_compute_diff(
991 gtbno, gtlen, >new);
993 * Right side is better.
995 if (gtdiff < ltdiff) {
996 xfs_btree_del_cursor(
1002 * Left side is better.
1005 xfs_btree_del_cursor(
1013 * Fell off the right end.
1015 if ((error = xfs_btree_increment(
1016 bno_cur_gt, 0, &i)))
1019 xfs_btree_del_cursor(
1028 * The left side is perfect, trash the right side.
1031 xfs_btree_del_cursor(bno_cur_gt,
1037 * It's the right side that was found first, look left.
1041 * Fix up the length.
1043 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1044 xfs_alloc_fix_len(args);
1046 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1047 args->alignment, gtbno, gtlen, >new);
1049 * Right side entry isn't perfect.
1053 * Look until we find a better one, run out of
1054 * space, or run off the end.
1056 while (bno_cur_lt && bno_cur_gt) {
1057 if ((error = xfs_alloc_get_rec(
1061 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1062 xfs_alloc_compute_aligned(ltbno, ltlen,
1063 args->alignment, args->minlen,
1066 * The right one is clearly better.
1068 if (ltbnoa <= args->agbno - gtdiff) {
1069 xfs_btree_del_cursor(
1076 * If we reach a big enough entry,
1077 * compare the two and pick the best.
1079 if (ltlena >= args->minlen) {
1080 args->len = XFS_EXTLEN_MIN(
1081 ltlena, args->maxlen);
1082 xfs_alloc_fix_len(args);
1084 ltdiff = xfs_alloc_compute_diff(
1087 ltbno, ltlen, <new);
1089 * Left side is better.
1091 if (ltdiff < gtdiff) {
1092 xfs_btree_del_cursor(
1098 * Right side is better.
1101 xfs_btree_del_cursor(
1109 * Fell off the left end.
1111 if ((error = xfs_btree_decrement(
1112 bno_cur_lt, 0, &i)))
1115 xfs_btree_del_cursor(bno_cur_lt,
1123 * The right side is perfect, trash the left side.
1126 xfs_btree_del_cursor(bno_cur_lt,
1133 * If we couldn't get anything, give up.
1135 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1136 trace_xfs_alloc_size_neither(args);
1137 args->agbno = NULLAGBLOCK;
1141 * At this point we have selected a freespace entry, either to the
1142 * left or to the right. If it's on the right, copy all the
1143 * useful variables to the "left" set so we only have one
1144 * copy of this code.
1147 bno_cur_lt = bno_cur_gt;
1157 * Fix up the length and compute the useful address.
1159 ltend = ltbno + ltlen;
1160 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1161 xfs_alloc_fix_len(args);
1162 if (!xfs_alloc_fix_minleft(args)) {
1163 trace_xfs_alloc_near_nominleft(args);
1164 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1165 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1169 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1171 ASSERT(ltnew >= ltbno);
1172 ASSERT(ltnew + rlen <= ltend);
1173 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1174 args->agbno = ltnew;
1175 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1176 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1180 trace_xfs_alloc_near_greater(args);
1182 trace_xfs_alloc_near_lesser(args);
1184 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1185 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1189 trace_xfs_alloc_near_error(args);
1190 if (cnt_cur != NULL)
1191 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1192 if (bno_cur_lt != NULL)
1193 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1194 if (bno_cur_gt != NULL)
1195 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1200 * Allocate a variable extent anywhere in the allocation group agno.
1201 * Extent's length (returned in len) will be between minlen and maxlen,
1202 * and of the form k * prod + mod unless there's nothing that large.
1203 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1205 STATIC int /* error */
1206 xfs_alloc_ag_vextent_size(
1207 xfs_alloc_arg_t *args) /* allocation argument structure */
1209 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1210 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1211 int error; /* error result */
1212 xfs_agblock_t fbno; /* start of found freespace */
1213 xfs_extlen_t flen; /* length of found freespace */
1214 int i; /* temp status variable */
1215 xfs_agblock_t rbno; /* returned block number */
1216 xfs_extlen_t rlen; /* length of returned extent */
1219 * Allocate and initialize a cursor for the by-size btree.
1221 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1222 args->agno, XFS_BTNUM_CNT);
1225 * Look for an entry >= maxlen+alignment-1 blocks.
1227 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1228 args->maxlen + args->alignment - 1, &i)))
1231 * If none, then pick up the last entry in the tree unless the
1235 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1238 if (i == 0 || flen == 0) {
1239 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1240 trace_xfs_alloc_size_noentry(args);
1246 * There's a freespace as big as maxlen+alignment-1, get it.
1249 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1251 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1254 * In the first case above, we got the last entry in the
1255 * by-size btree. Now we check to see if the space hits maxlen
1256 * once aligned; if not, we search left for something better.
1257 * This can't happen in the second case above.
1259 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1261 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1262 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1263 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1264 if (rlen < args->maxlen) {
1265 xfs_agblock_t bestfbno;
1266 xfs_extlen_t bestflen;
1267 xfs_agblock_t bestrbno;
1268 xfs_extlen_t bestrlen;
1275 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1279 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1282 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1283 if (flen < bestrlen)
1285 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1286 args->minlen, &rbno, &rlen);
1287 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1288 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1289 (rlen <= flen && rbno + rlen <= fbno + flen),
1291 if (rlen > bestrlen) {
1296 if (rlen == args->maxlen)
1300 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1303 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1309 args->wasfromfl = 0;
1311 * Fix up the length.
1314 xfs_alloc_fix_len(args);
1315 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1316 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1317 trace_xfs_alloc_size_nominleft(args);
1318 args->agbno = NULLAGBLOCK;
1322 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1324 * Allocate and initialize a cursor for the by-block tree.
1326 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1327 args->agno, XFS_BTNUM_BNO);
1328 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1329 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1331 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1332 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1333 cnt_cur = bno_cur = NULL;
1336 XFS_WANT_CORRUPTED_GOTO(
1337 args->agbno + args->len <=
1338 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1340 trace_xfs_alloc_size_done(args);
1344 trace_xfs_alloc_size_error(args);
1346 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1348 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1353 * Deal with the case where only small freespaces remain.
1354 * Either return the contents of the last freespace record,
1355 * or allocate space from the freelist if there is nothing in the tree.
1357 STATIC int /* error */
1358 xfs_alloc_ag_vextent_small(
1359 xfs_alloc_arg_t *args, /* allocation argument structure */
1360 xfs_btree_cur_t *ccur, /* by-size cursor */
1361 xfs_agblock_t *fbnop, /* result block number */
1362 xfs_extlen_t *flenp, /* result length */
1363 int *stat) /* status: 0-freelist, 1-normal/none */
1370 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1373 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1375 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1378 * Nothing in the btree, try the freelist. Make sure
1379 * to respect minleft even when pulling from the
1382 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1383 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1385 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1388 if (fbno != NULLAGBLOCK) {
1389 if (args->userdata) {
1392 bp = xfs_btree_get_bufs(args->mp, args->tp,
1393 args->agno, fbno, 0);
1394 xfs_trans_binval(args->tp, bp);
1398 XFS_WANT_CORRUPTED_GOTO(
1399 args->agbno + args->len <=
1400 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1402 args->wasfromfl = 1;
1403 trace_xfs_alloc_small_freelist(args);
1408 * Nothing in the freelist.
1414 * Can't allocate from the freelist for some reason.
1421 * Can't do the allocation, give up.
1423 if (flen < args->minlen) {
1424 args->agbno = NULLAGBLOCK;
1425 trace_xfs_alloc_small_notenough(args);
1431 trace_xfs_alloc_small_done(args);
1435 trace_xfs_alloc_small_error(args);
1440 * Free the extent starting at agno/bno for length.
1442 STATIC int /* error */
1444 xfs_trans_t *tp, /* transaction pointer */
1445 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1446 xfs_agnumber_t agno, /* allocation group number */
1447 xfs_agblock_t bno, /* starting block number */
1448 xfs_extlen_t len, /* length of extent */
1449 int isfl) /* set if is freelist blocks - no sb acctg */
1451 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1452 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1453 int error; /* error return value */
1454 xfs_agblock_t gtbno; /* start of right neighbor block */
1455 xfs_extlen_t gtlen; /* length of right neighbor block */
1456 int haveleft; /* have a left neighbor block */
1457 int haveright; /* have a right neighbor block */
1458 int i; /* temp, result code */
1459 xfs_agblock_t ltbno; /* start of left neighbor block */
1460 xfs_extlen_t ltlen; /* length of left neighbor block */
1461 xfs_mount_t *mp; /* mount point struct for filesystem */
1462 xfs_agblock_t nbno; /* new starting block of freespace */
1463 xfs_extlen_t nlen; /* new length of freespace */
1467 * Allocate and initialize a cursor for the by-block btree.
1469 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1472 * Look for a neighboring block on the left (lower block numbers)
1473 * that is contiguous with this space.
1475 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1479 * There is a block to our left.
1481 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1483 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1485 * It's not contiguous, though.
1487 if (ltbno + ltlen < bno)
1491 * If this failure happens the request to free this
1492 * space was invalid, it's (partly) already free.
1495 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1499 * Look for a neighboring block on the right (higher block numbers)
1500 * that is contiguous with this space.
1502 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1506 * There is a block to our right.
1508 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1510 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1512 * It's not contiguous, though.
1514 if (bno + len < gtbno)
1518 * If this failure happens the request to free this
1519 * space was invalid, it's (partly) already free.
1522 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1526 * Now allocate and initialize a cursor for the by-size tree.
1528 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1530 * Have both left and right contiguous neighbors.
1531 * Merge all three into a single free block.
1533 if (haveleft && haveright) {
1535 * Delete the old by-size entry on the left.
1537 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1539 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1540 if ((error = xfs_btree_delete(cnt_cur, &i)))
1542 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1544 * Delete the old by-size entry on the right.
1546 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1548 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1549 if ((error = xfs_btree_delete(cnt_cur, &i)))
1551 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1553 * Delete the old by-block entry for the right block.
1555 if ((error = xfs_btree_delete(bno_cur, &i)))
1557 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1559 * Move the by-block cursor back to the left neighbor.
1561 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1563 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1566 * Check that this is the right record: delete didn't
1567 * mangle the cursor.
1570 xfs_agblock_t xxbno;
1573 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1576 XFS_WANT_CORRUPTED_GOTO(
1577 i == 1 && xxbno == ltbno && xxlen == ltlen,
1582 * Update remaining by-block entry to the new, joined block.
1585 nlen = len + ltlen + gtlen;
1586 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1590 * Have only a left contiguous neighbor.
1591 * Merge it together with the new freespace.
1593 else if (haveleft) {
1595 * Delete the old by-size entry on the left.
1597 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1599 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1600 if ((error = xfs_btree_delete(cnt_cur, &i)))
1602 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1604 * Back up the by-block cursor to the left neighbor, and
1605 * update its length.
1607 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1609 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1612 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1616 * Have only a right contiguous neighbor.
1617 * Merge it together with the new freespace.
1619 else if (haveright) {
1621 * Delete the old by-size entry on the right.
1623 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1625 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1626 if ((error = xfs_btree_delete(cnt_cur, &i)))
1628 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1630 * Update the starting block and length of the right
1631 * neighbor in the by-block tree.
1635 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1639 * No contiguous neighbors.
1640 * Insert the new freespace into the by-block tree.
1645 if ((error = xfs_btree_insert(bno_cur, &i)))
1647 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1649 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1652 * In all cases we need to insert the new freespace in the by-size tree.
1654 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1656 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1657 if ((error = xfs_btree_insert(cnt_cur, &i)))
1659 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1660 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1663 * Update the freespace totals in the ag and superblock.
1667 xfs_perag_t *pag; /* per allocation group data */
1669 pag = xfs_perag_get(mp, agno);
1670 pag->pagf_freeblks += len;
1673 agf = XFS_BUF_TO_AGF(agbp);
1674 be32_add_cpu(&agf->agf_freeblks, len);
1675 xfs_trans_agblocks_delta(tp, len);
1676 XFS_WANT_CORRUPTED_GOTO(
1677 be32_to_cpu(agf->agf_freeblks) <=
1678 be32_to_cpu(agf->agf_length),
1680 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1682 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1683 XFS_STATS_INC(xs_freex);
1684 XFS_STATS_ADD(xs_freeb, len);
1687 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
1690 * Since blocks move to the free list without the coordination
1691 * used in xfs_bmap_finish, we can't allow block to be available
1692 * for reallocation and non-transaction writing (user data)
1693 * until we know that the transaction that moved it to the free
1694 * list is permanently on disk. We track the blocks by declaring
1695 * these blocks as "busy"; the busy list is maintained on a per-ag
1696 * basis and each transaction records which entries should be removed
1697 * when the iclog commits to disk. If a busy block is allocated,
1698 * the iclog is pushed up to the LSN that freed the block.
1700 xfs_alloc_busy_insert(tp, agno, bno, len);
1704 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
1706 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1708 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1713 * Visible (exported) allocation/free functions.
1714 * Some of these are used just by xfs_alloc_btree.c and this file.
1718 * Compute and fill in value of m_ag_maxlevels.
1721 xfs_alloc_compute_maxlevels(
1722 xfs_mount_t *mp) /* file system mount structure */
1730 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1731 minleafrecs = mp->m_alloc_mnr[0];
1732 minnoderecs = mp->m_alloc_mnr[1];
1733 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1734 for (level = 1; maxblocks > 1; level++)
1735 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1736 mp->m_ag_maxlevels = level;
1740 * Find the length of the longest extent in an AG.
1743 xfs_alloc_longest_free_extent(
1744 struct xfs_mount *mp,
1745 struct xfs_perag *pag)
1747 xfs_extlen_t need, delta = 0;
1749 need = XFS_MIN_FREELIST_PAG(pag, mp);
1750 if (need > pag->pagf_flcount)
1751 delta = need - pag->pagf_flcount;
1753 if (pag->pagf_longest > delta)
1754 return pag->pagf_longest - delta;
1755 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1759 * Decide whether to use this allocation group for this allocation.
1760 * If so, fix up the btree freelist's size.
1762 STATIC int /* error */
1763 xfs_alloc_fix_freelist(
1764 xfs_alloc_arg_t *args, /* allocation argument structure */
1765 int flags) /* XFS_ALLOC_FLAG_... */
1767 xfs_buf_t *agbp; /* agf buffer pointer */
1768 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1769 xfs_buf_t *agflbp;/* agfl buffer pointer */
1770 xfs_agblock_t bno; /* freelist block */
1771 xfs_extlen_t delta; /* new blocks needed in freelist */
1772 int error; /* error result code */
1773 xfs_extlen_t longest;/* longest extent in allocation group */
1774 xfs_mount_t *mp; /* file system mount point structure */
1775 xfs_extlen_t need; /* total blocks needed in freelist */
1776 xfs_perag_t *pag; /* per-ag information structure */
1777 xfs_alloc_arg_t targs; /* local allocation arguments */
1778 xfs_trans_t *tp; /* transaction pointer */
1784 if (!pag->pagf_init) {
1785 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1788 if (!pag->pagf_init) {
1789 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1790 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1798 * If this is a metadata preferred pag and we are user data
1799 * then try somewhere else if we are not being asked to
1800 * try harder at this point
1802 if (pag->pagf_metadata && args->userdata &&
1803 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1804 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1809 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1811 * If it looks like there isn't a long enough extent, or enough
1812 * total blocks, reject it.
1814 need = XFS_MIN_FREELIST_PAG(pag, mp);
1815 longest = xfs_alloc_longest_free_extent(mp, pag);
1816 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1818 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1819 need - args->total) < (int)args->minleft)) {
1821 xfs_trans_brelse(tp, agbp);
1828 * Get the a.g. freespace buffer.
1829 * Can fail if we're not blocking on locks, and it's held.
1832 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1836 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1837 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1843 * Figure out how many blocks we should have in the freelist.
1845 agf = XFS_BUF_TO_AGF(agbp);
1846 need = XFS_MIN_FREELIST(agf, mp);
1848 * If there isn't enough total or single-extent, reject it.
1850 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1851 delta = need > be32_to_cpu(agf->agf_flcount) ?
1852 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1853 longest = be32_to_cpu(agf->agf_longest);
1854 longest = (longest > delta) ? (longest - delta) :
1855 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1856 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1858 ((int)(be32_to_cpu(agf->agf_freeblks) +
1859 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1860 (int)args->minleft)) {
1861 xfs_trans_brelse(tp, agbp);
1867 * Make the freelist shorter if it's too long.
1869 while (be32_to_cpu(agf->agf_flcount) > need) {
1872 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1875 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1877 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1878 xfs_trans_binval(tp, bp);
1881 * Initialize the args structure.
1886 targs.agno = args->agno;
1887 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1888 targs.minalignslop = 0;
1889 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1890 targs.type = XFS_ALLOCTYPE_THIS_AG;
1892 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1895 * Make the freelist longer if it's too short.
1897 while (be32_to_cpu(agf->agf_flcount) < need) {
1899 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1901 * Allocate as many blocks as possible at once.
1903 if ((error = xfs_alloc_ag_vextent(&targs))) {
1904 xfs_trans_brelse(tp, agflbp);
1908 * Stop if we run out. Won't happen if callers are obeying
1909 * the restrictions correctly. Can happen for free calls
1910 * on a completely full ag.
1912 if (targs.agbno == NULLAGBLOCK) {
1913 if (flags & XFS_ALLOC_FLAG_FREEING)
1915 xfs_trans_brelse(tp, agflbp);
1920 * Put each allocated block on the list.
1922 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
1923 error = xfs_alloc_put_freelist(tp, agbp,
1929 xfs_trans_brelse(tp, agflbp);
1935 * Get a block from the freelist.
1936 * Returns with the buffer for the block gotten.
1939 xfs_alloc_get_freelist(
1940 xfs_trans_t *tp, /* transaction pointer */
1941 xfs_buf_t *agbp, /* buffer containing the agf structure */
1942 xfs_agblock_t *bnop, /* block address retrieved from freelist */
1943 int btreeblk) /* destination is a AGF btree */
1945 xfs_agf_t *agf; /* a.g. freespace structure */
1946 xfs_agfl_t *agfl; /* a.g. freelist structure */
1947 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
1948 xfs_agblock_t bno; /* block number returned */
1951 xfs_mount_t *mp; /* mount structure */
1952 xfs_perag_t *pag; /* per allocation group data */
1954 agf = XFS_BUF_TO_AGF(agbp);
1956 * Freelist is empty, give up.
1958 if (!agf->agf_flcount) {
1959 *bnop = NULLAGBLOCK;
1963 * Read the array of free blocks.
1966 if ((error = xfs_alloc_read_agfl(mp, tp,
1967 be32_to_cpu(agf->agf_seqno), &agflbp)))
1969 agfl = XFS_BUF_TO_AGFL(agflbp);
1971 * Get the block number and update the data structures.
1973 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
1974 be32_add_cpu(&agf->agf_flfirst, 1);
1975 xfs_trans_brelse(tp, agflbp);
1976 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
1977 agf->agf_flfirst = 0;
1979 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
1980 be32_add_cpu(&agf->agf_flcount, -1);
1981 xfs_trans_agflist_delta(tp, -1);
1982 pag->pagf_flcount--;
1985 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
1987 be32_add_cpu(&agf->agf_btreeblks, 1);
1988 pag->pagf_btreeblks++;
1989 logflags |= XFS_AGF_BTREEBLKS;
1992 xfs_alloc_log_agf(tp, agbp, logflags);
1996 * As blocks are freed, they are added to the per-ag busy list and
1997 * remain there until the freeing transaction is committed to disk.
1998 * Now that we have allocated blocks, this list must be searched to see
1999 * if a block is being reused. If one is, then the freeing transaction
2000 * must be pushed to disk before this transaction.
2002 * We do this by setting the current transaction to a sync transaction
2003 * which guarantees that the freeing transaction is on disk before this
2004 * transaction. This is done instead of a synchronous log force here so
2005 * that we don't sit and wait with the AGF locked in the transaction
2006 * during the log force.
2008 if (xfs_alloc_busy_search(mp, be32_to_cpu(agf->agf_seqno), bno, 1))
2009 xfs_trans_set_sync(tp);
2014 * Log the given fields from the agf structure.
2018 xfs_trans_t *tp, /* transaction pointer */
2019 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2020 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2022 int first; /* first byte offset */
2023 int last; /* last byte offset */
2024 static const short offsets[] = {
2025 offsetof(xfs_agf_t, agf_magicnum),
2026 offsetof(xfs_agf_t, agf_versionnum),
2027 offsetof(xfs_agf_t, agf_seqno),
2028 offsetof(xfs_agf_t, agf_length),
2029 offsetof(xfs_agf_t, agf_roots[0]),
2030 offsetof(xfs_agf_t, agf_levels[0]),
2031 offsetof(xfs_agf_t, agf_flfirst),
2032 offsetof(xfs_agf_t, agf_fllast),
2033 offsetof(xfs_agf_t, agf_flcount),
2034 offsetof(xfs_agf_t, agf_freeblks),
2035 offsetof(xfs_agf_t, agf_longest),
2036 offsetof(xfs_agf_t, agf_btreeblks),
2040 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2042 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2043 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2047 * Interface for inode allocation to force the pag data to be initialized.
2050 xfs_alloc_pagf_init(
2051 xfs_mount_t *mp, /* file system mount structure */
2052 xfs_trans_t *tp, /* transaction pointer */
2053 xfs_agnumber_t agno, /* allocation group number */
2054 int flags) /* XFS_ALLOC_FLAGS_... */
2059 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2062 xfs_trans_brelse(tp, bp);
2067 * Put the block on the freelist for the allocation group.
2070 xfs_alloc_put_freelist(
2071 xfs_trans_t *tp, /* transaction pointer */
2072 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2073 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2074 xfs_agblock_t bno, /* block being freed */
2075 int btreeblk) /* block came from a AGF btree */
2077 xfs_agf_t *agf; /* a.g. freespace structure */
2078 xfs_agfl_t *agfl; /* a.g. free block array */
2079 __be32 *blockp;/* pointer to array entry */
2082 xfs_mount_t *mp; /* mount structure */
2083 xfs_perag_t *pag; /* per allocation group data */
2085 agf = XFS_BUF_TO_AGF(agbp);
2088 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2089 be32_to_cpu(agf->agf_seqno), &agflbp)))
2091 agfl = XFS_BUF_TO_AGFL(agflbp);
2092 be32_add_cpu(&agf->agf_fllast, 1);
2093 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2094 agf->agf_fllast = 0;
2096 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2097 be32_add_cpu(&agf->agf_flcount, 1);
2098 xfs_trans_agflist_delta(tp, 1);
2099 pag->pagf_flcount++;
2101 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2103 be32_add_cpu(&agf->agf_btreeblks, -1);
2104 pag->pagf_btreeblks--;
2105 logflags |= XFS_AGF_BTREEBLKS;
2109 xfs_alloc_log_agf(tp, agbp, logflags);
2111 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2112 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2113 *blockp = cpu_to_be32(bno);
2114 xfs_alloc_log_agf(tp, agbp, logflags);
2115 xfs_trans_log_buf(tp, agflbp,
2116 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2117 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2118 sizeof(xfs_agblock_t) - 1));
2123 * Read in the allocation group header (free/alloc section).
2127 struct xfs_mount *mp, /* mount point structure */
2128 struct xfs_trans *tp, /* transaction pointer */
2129 xfs_agnumber_t agno, /* allocation group number */
2130 int flags, /* XFS_BUF_ */
2131 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2133 struct xfs_agf *agf; /* ag freelist header */
2134 int agf_ok; /* set if agf is consistent */
2137 ASSERT(agno != NULLAGNUMBER);
2138 error = xfs_trans_read_buf(
2139 mp, tp, mp->m_ddev_targp,
2140 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2141 XFS_FSS_TO_BB(mp, 1), flags, bpp);
2147 ASSERT(!XFS_BUF_GETERROR(*bpp));
2148 agf = XFS_BUF_TO_AGF(*bpp);
2151 * Validate the magic number of the agf block.
2154 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2155 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2156 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2157 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2158 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2159 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
2160 be32_to_cpu(agf->agf_seqno) == agno;
2161 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2162 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2163 be32_to_cpu(agf->agf_length);
2164 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2165 XFS_RANDOM_ALLOC_READ_AGF))) {
2166 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2167 XFS_ERRLEVEL_LOW, mp, agf);
2168 xfs_trans_brelse(tp, *bpp);
2169 return XFS_ERROR(EFSCORRUPTED);
2171 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
2176 * Read in the allocation group header (free/alloc section).
2180 struct xfs_mount *mp, /* mount point structure */
2181 struct xfs_trans *tp, /* transaction pointer */
2182 xfs_agnumber_t agno, /* allocation group number */
2183 int flags, /* XFS_ALLOC_FLAG_... */
2184 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2186 struct xfs_agf *agf; /* ag freelist header */
2187 struct xfs_perag *pag; /* per allocation group data */
2190 ASSERT(agno != NULLAGNUMBER);
2192 error = xfs_read_agf(mp, tp, agno,
2193 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2199 ASSERT(!XFS_BUF_GETERROR(*bpp));
2201 agf = XFS_BUF_TO_AGF(*bpp);
2202 pag = xfs_perag_get(mp, agno);
2203 if (!pag->pagf_init) {
2204 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2205 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2206 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2207 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2208 pag->pagf_levels[XFS_BTNUM_BNOi] =
2209 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2210 pag->pagf_levels[XFS_BTNUM_CNTi] =
2211 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2212 spin_lock_init(&pag->pagb_lock);
2213 pag->pagb_count = 0;
2214 pag->pagb_tree = RB_ROOT;
2218 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2219 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2220 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2221 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2222 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2223 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2224 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2225 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2226 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2234 * Allocate an extent (variable-size).
2235 * Depending on the allocation type, we either look in a single allocation
2236 * group or loop over the allocation groups to find the result.
2240 xfs_alloc_arg_t *args) /* allocation argument structure */
2242 xfs_agblock_t agsize; /* allocation group size */
2244 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2245 xfs_extlen_t minleft;/* minimum left value, temp copy */
2246 xfs_mount_t *mp; /* mount structure pointer */
2247 xfs_agnumber_t sagno; /* starting allocation group number */
2248 xfs_alloctype_t type; /* input allocation type */
2251 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2254 type = args->otype = args->type;
2255 args->agbno = NULLAGBLOCK;
2257 * Just fix this up, for the case where the last a.g. is shorter
2258 * (or there's only one a.g.) and the caller couldn't easily figure
2259 * that out (xfs_bmap_alloc).
2261 agsize = mp->m_sb.sb_agblocks;
2262 if (args->maxlen > agsize)
2263 args->maxlen = agsize;
2264 if (args->alignment == 0)
2265 args->alignment = 1;
2266 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2267 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2268 ASSERT(args->minlen <= args->maxlen);
2269 ASSERT(args->minlen <= agsize);
2270 ASSERT(args->mod < args->prod);
2271 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2272 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2273 args->minlen > args->maxlen || args->minlen > agsize ||
2274 args->mod >= args->prod) {
2275 args->fsbno = NULLFSBLOCK;
2276 trace_xfs_alloc_vextent_badargs(args);
2279 minleft = args->minleft;
2282 case XFS_ALLOCTYPE_THIS_AG:
2283 case XFS_ALLOCTYPE_NEAR_BNO:
2284 case XFS_ALLOCTYPE_THIS_BNO:
2286 * These three force us into a single a.g.
2288 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2289 args->pag = xfs_perag_get(mp, args->agno);
2291 error = xfs_alloc_fix_freelist(args, 0);
2292 args->minleft = minleft;
2294 trace_xfs_alloc_vextent_nofix(args);
2298 trace_xfs_alloc_vextent_noagbp(args);
2301 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2302 if ((error = xfs_alloc_ag_vextent(args)))
2305 case XFS_ALLOCTYPE_START_BNO:
2307 * Try near allocation first, then anywhere-in-ag after
2308 * the first a.g. fails.
2310 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2311 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2312 args->fsbno = XFS_AGB_TO_FSB(mp,
2313 ((mp->m_agfrotor / rotorstep) %
2314 mp->m_sb.sb_agcount), 0);
2317 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2318 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2320 case XFS_ALLOCTYPE_ANY_AG:
2321 case XFS_ALLOCTYPE_START_AG:
2322 case XFS_ALLOCTYPE_FIRST_AG:
2324 * Rotate through the allocation groups looking for a winner.
2326 if (type == XFS_ALLOCTYPE_ANY_AG) {
2328 * Start with the last place we left off.
2330 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2331 mp->m_sb.sb_agcount;
2332 args->type = XFS_ALLOCTYPE_THIS_AG;
2333 flags = XFS_ALLOC_FLAG_TRYLOCK;
2334 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2336 * Start with allocation group given by bno.
2338 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2339 args->type = XFS_ALLOCTYPE_THIS_AG;
2343 if (type == XFS_ALLOCTYPE_START_AG)
2344 args->type = XFS_ALLOCTYPE_THIS_AG;
2346 * Start with the given allocation group.
2348 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2349 flags = XFS_ALLOC_FLAG_TRYLOCK;
2352 * Loop over allocation groups twice; first time with
2353 * trylock set, second time without.
2356 args->pag = xfs_perag_get(mp, args->agno);
2357 if (no_min) args->minleft = 0;
2358 error = xfs_alloc_fix_freelist(args, flags);
2359 args->minleft = minleft;
2361 trace_xfs_alloc_vextent_nofix(args);
2365 * If we get a buffer back then the allocation will fly.
2368 if ((error = xfs_alloc_ag_vextent(args)))
2373 trace_xfs_alloc_vextent_loopfailed(args);
2376 * Didn't work, figure out the next iteration.
2378 if (args->agno == sagno &&
2379 type == XFS_ALLOCTYPE_START_BNO)
2380 args->type = XFS_ALLOCTYPE_THIS_AG;
2382 * For the first allocation, we can try any AG to get
2383 * space. However, if we already have allocated a
2384 * block, we don't want to try AGs whose number is below
2385 * sagno. Otherwise, we may end up with out-of-order
2386 * locking of AGF, which might cause deadlock.
2388 if (++(args->agno) == mp->m_sb.sb_agcount) {
2389 if (args->firstblock != NULLFSBLOCK)
2395 * Reached the starting a.g., must either be done
2396 * or switch to non-trylock mode.
2398 if (args->agno == sagno) {
2400 args->agbno = NULLAGBLOCK;
2401 trace_xfs_alloc_vextent_allfailed(args);
2408 if (type == XFS_ALLOCTYPE_START_BNO) {
2409 args->agbno = XFS_FSB_TO_AGBNO(mp,
2411 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2415 xfs_perag_put(args->pag);
2417 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2418 if (args->agno == sagno)
2419 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2420 (mp->m_sb.sb_agcount * rotorstep);
2422 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2423 (mp->m_sb.sb_agcount * rotorstep);
2430 if (args->agbno == NULLAGBLOCK)
2431 args->fsbno = NULLFSBLOCK;
2433 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2435 ASSERT(args->len >= args->minlen);
2436 ASSERT(args->len <= args->maxlen);
2437 ASSERT(args->agbno % args->alignment == 0);
2438 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2442 xfs_perag_put(args->pag);
2445 xfs_perag_put(args->pag);
2451 * Just break up the extent address and hand off to xfs_free_ag_extent
2452 * after fixing up the freelist.
2456 xfs_trans_t *tp, /* transaction pointer */
2457 xfs_fsblock_t bno, /* starting block number of extent */
2458 xfs_extlen_t len) /* length of extent */
2460 xfs_alloc_arg_t args;
2464 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2466 args.mp = tp->t_mountp;
2467 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2468 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2469 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2470 args.pag = xfs_perag_get(args.mp, args.agno);
2471 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2474 ASSERT(args.agbp != NULL);
2475 ASSERT((args.agbno + len) <=
2476 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2478 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2480 xfs_perag_put(args.pag);
2486 * AG Busy list management
2487 * The busy list contains block ranges that have been freed but whose
2488 * transactions have not yet hit disk. If any block listed in a busy
2489 * list is reused, the transaction that freed it must be forced to disk
2490 * before continuing to use the block.
2492 * xfs_alloc_busy_insert - add to the per-ag busy list
2493 * xfs_alloc_busy_clear - remove an item from the per-ag busy list
2494 * xfs_alloc_busy_search - search for a busy extent
2498 * Insert a new extent into the busy tree.
2500 * The busy extent tree is indexed by the start block of the busy extent.
2501 * there can be multiple overlapping ranges in the busy extent tree but only
2502 * ever one entry at a given start block. The reason for this is that
2503 * multi-block extents can be freed, then smaller chunks of that extent
2504 * allocated and freed again before the first transaction commit is on disk.
2505 * If the exact same start block is freed a second time, we have to wait for
2506 * that busy extent to pass out of the tree before the new extent is inserted.
2507 * There are two main cases we have to handle here.
2509 * The first case is a transaction that triggers a "free - allocate - free"
2510 * cycle. This can occur during btree manipulations as a btree block is freed
2511 * to the freelist, then allocated from the free list, then freed again. In
2512 * this case, the second extxpnet free is what triggers the duplicate and as
2513 * such the transaction IDs should match. Because the extent was allocated in
2514 * this transaction, the transaction must be marked as synchronous. This is
2515 * true for all cases where the free/alloc/free occurs in the one transaction,
2516 * hence the addition of the ASSERT(tp->t_flags & XFS_TRANS_SYNC) to this case.
2517 * This serves to catch violations of the second case quite effectively.
2519 * The second case is where the free/alloc/free occur in different
2520 * transactions. In this case, the thread freeing the extent the second time
2521 * can't mark the extent busy immediately because it is already tracked in a
2522 * transaction that may be committing. When the log commit for the existing
2523 * busy extent completes, the busy extent will be removed from the tree. If we
2524 * allow the second busy insert to continue using that busy extent structure,
2525 * it can be freed before this transaction is safely in the log. Hence our
2526 * only option in this case is to force the log to remove the existing busy
2527 * extent from the list before we insert the new one with the current
2530 * The problem we are trying to avoid in the free-alloc-free in separate
2531 * transactions is most easily described with a timeline:
2533 * Thread 1 Thread 2 Thread 3 xfslogd
2556 * checkpoint completes
2558 * By issuing a log force in thread 3 @ "KABOOM", the thread will block until
2559 * the checkpoint completes, and the busy extent it matched will have been
2560 * removed from the tree when it is woken. Hence it can then continue safely.
2562 * However, to ensure this matching process is robust, we need to use the
2563 * transaction ID for identifying transaction, as delayed logging results in
2564 * the busy extent and transaction lifecycles being different. i.e. the busy
2565 * extent is active for a lot longer than the transaction. Hence the
2566 * transaction structure can be freed and reallocated, then mark the same
2567 * extent busy again in the new transaction. In this case the new transaction
2568 * will have a different tid but can have the same address, and hence we need
2569 * to check against the tid.
2571 * Future: for delayed logging, we could avoid the log force if the extent was
2572 * first freed in the current checkpoint sequence. This, however, requires the
2573 * ability to pin the current checkpoint in memory until this transaction
2574 * commits to ensure that both the original free and the current one combine
2575 * logically into the one checkpoint. If the checkpoint sequences are
2576 * different, however, we still need to wait on a log force.
2579 xfs_alloc_busy_insert(
2580 struct xfs_trans *tp,
2581 xfs_agnumber_t agno,
2585 struct xfs_busy_extent *new;
2586 struct xfs_busy_extent *busyp;
2587 struct xfs_perag *pag;
2588 struct rb_node **rbp;
2589 struct rb_node *parent;
2593 new = kmem_zalloc(sizeof(struct xfs_busy_extent), KM_MAYFAIL);
2596 * No Memory! Since it is now not possible to track the free
2597 * block, make this a synchronous transaction to insure that
2598 * the block is not reused before this transaction commits.
2600 trace_xfs_alloc_busy(tp, agno, bno, len, 1);
2601 xfs_trans_set_sync(tp);
2608 new->tid = xfs_log_get_trans_ident(tp);
2610 INIT_LIST_HEAD(&new->list);
2612 /* trace before insert to be able to see failed inserts */
2613 trace_xfs_alloc_busy(tp, agno, bno, len, 0);
2615 pag = xfs_perag_get(tp->t_mountp, new->agno);
2617 spin_lock(&pag->pagb_lock);
2618 rbp = &pag->pagb_tree.rb_node;
2622 while (*rbp && match >= 0) {
2624 busyp = rb_entry(parent, struct xfs_busy_extent, rb_node);
2626 if (new->bno < busyp->bno) {
2627 /* may overlap, but exact start block is lower */
2628 rbp = &(*rbp)->rb_left;
2629 if (new->bno + new->length > busyp->bno)
2630 match = busyp->tid == new->tid ? 1 : -1;
2631 } else if (new->bno > busyp->bno) {
2632 /* may overlap, but exact start block is higher */
2633 rbp = &(*rbp)->rb_right;
2634 if (bno < busyp->bno + busyp->length)
2635 match = busyp->tid == new->tid ? 1 : -1;
2637 match = busyp->tid == new->tid ? 1 : -1;
2642 /* overlap marked busy in different transaction */
2643 spin_unlock(&pag->pagb_lock);
2644 xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
2649 * overlap marked busy in same transaction. Update if exact
2650 * start block match, otherwise combine the busy extents into
2653 if (busyp->bno == new->bno) {
2654 busyp->length = max(busyp->length, new->length);
2655 spin_unlock(&pag->pagb_lock);
2656 ASSERT(tp->t_flags & XFS_TRANS_SYNC);
2661 rb_erase(&busyp->rb_node, &pag->pagb_tree);
2662 new->length = max(busyp->bno + busyp->length,
2663 new->bno + new->length) -
2664 min(busyp->bno, new->bno);
2665 new->bno = min(busyp->bno, new->bno);
2669 rb_link_node(&new->rb_node, parent, rbp);
2670 rb_insert_color(&new->rb_node, &pag->pagb_tree);
2672 list_add(&new->list, &tp->t_busy);
2673 spin_unlock(&pag->pagb_lock);
2679 * Search for a busy extent within the range of the extent we are about to
2680 * allocate. You need to be holding the busy extent tree lock when calling
2681 * xfs_alloc_busy_search(). This function returns 0 for no overlapping busy
2682 * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
2683 * match. This is done so that a non-zero return indicates an overlap that
2684 * will require a synchronous transaction, but it can still be
2685 * used to distinguish between a partial or exact match.
2688 xfs_alloc_busy_search(
2689 struct xfs_mount *mp,
2690 xfs_agnumber_t agno,
2694 struct xfs_perag *pag;
2695 struct rb_node *rbp;
2696 struct xfs_busy_extent *busyp;
2699 pag = xfs_perag_get(mp, agno);
2700 spin_lock(&pag->pagb_lock);
2702 rbp = pag->pagb_tree.rb_node;
2704 /* find closest start bno overlap */
2706 busyp = rb_entry(rbp, struct xfs_busy_extent, rb_node);
2707 if (bno < busyp->bno) {
2708 /* may overlap, but exact start block is lower */
2709 if (bno + len > busyp->bno)
2712 } else if (bno > busyp->bno) {
2713 /* may overlap, but exact start block is higher */
2714 if (bno < busyp->bno + busyp->length)
2716 rbp = rbp->rb_right;
2718 /* bno matches busyp, length determines exact match */
2719 match = (busyp->length == len) ? 1 : -1;
2723 spin_unlock(&pag->pagb_lock);
2724 trace_xfs_alloc_busysearch(mp, agno, bno, len, !!match);
2730 xfs_alloc_busy_clear(
2731 struct xfs_mount *mp,
2732 struct xfs_busy_extent *busyp)
2734 struct xfs_perag *pag;
2736 trace_xfs_alloc_unbusy(mp, busyp->agno, busyp->bno,
2739 ASSERT(xfs_alloc_busy_search(mp, busyp->agno, busyp->bno,
2740 busyp->length) == 1);
2742 list_del_init(&busyp->list);
2744 pag = xfs_perag_get(mp, busyp->agno);
2745 spin_lock(&pag->pagb_lock);
2746 rb_erase(&busyp->rb_node, &pag->pagb_tree);
2747 spin_unlock(&pag->pagb_lock);