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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc_btree.h"
29 #include "xfs_ialloc_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_alloc.h"
34 #include "xfs_extent_busy.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
38 struct workqueue_struct *xfs_alloc_wq;
40 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
42 #define XFSA_FIXUP_BNO_OK 1
43 #define XFSA_FIXUP_CNT_OK 2
45 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
46 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
47 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
48 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
49 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
52 * Lookup the record equal to [bno, len] in the btree given by cur.
54 STATIC int /* error */
56 struct xfs_btree_cur *cur, /* btree cursor */
57 xfs_agblock_t bno, /* starting block of extent */
58 xfs_extlen_t len, /* length of extent */
59 int *stat) /* success/failure */
61 cur->bc_rec.a.ar_startblock = bno;
62 cur->bc_rec.a.ar_blockcount = len;
63 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
67 * Lookup the first record greater than or equal to [bno, len]
68 * in the btree given by cur.
72 struct xfs_btree_cur *cur, /* btree cursor */
73 xfs_agblock_t bno, /* starting block of extent */
74 xfs_extlen_t len, /* length of extent */
75 int *stat) /* success/failure */
77 cur->bc_rec.a.ar_startblock = bno;
78 cur->bc_rec.a.ar_blockcount = len;
79 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
83 * Lookup the first record less than or equal to [bno, len]
84 * in the btree given by cur.
88 struct xfs_btree_cur *cur, /* btree cursor */
89 xfs_agblock_t bno, /* starting block of extent */
90 xfs_extlen_t len, /* length of extent */
91 int *stat) /* success/failure */
93 cur->bc_rec.a.ar_startblock = bno;
94 cur->bc_rec.a.ar_blockcount = len;
95 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
99 * Update the record referred to by cur to the value given
101 * This either works (return 0) or gets an EFSCORRUPTED error.
103 STATIC int /* error */
105 struct xfs_btree_cur *cur, /* btree cursor */
106 xfs_agblock_t bno, /* starting block of extent */
107 xfs_extlen_t len) /* length of extent */
109 union xfs_btree_rec rec;
111 rec.alloc.ar_startblock = cpu_to_be32(bno);
112 rec.alloc.ar_blockcount = cpu_to_be32(len);
113 return xfs_btree_update(cur, &rec);
117 * Get the data from the pointed-to record.
121 struct xfs_btree_cur *cur, /* btree cursor */
122 xfs_agblock_t *bno, /* output: starting block of extent */
123 xfs_extlen_t *len, /* output: length of extent */
124 int *stat) /* output: success/failure */
126 union xfs_btree_rec *rec;
129 error = xfs_btree_get_rec(cur, &rec, stat);
130 if (!error && *stat == 1) {
131 *bno = be32_to_cpu(rec->alloc.ar_startblock);
132 *len = be32_to_cpu(rec->alloc.ar_blockcount);
138 * Compute aligned version of the found extent.
139 * Takes alignment and min length into account.
142 xfs_alloc_compute_aligned(
143 xfs_alloc_arg_t *args, /* allocation argument structure */
144 xfs_agblock_t foundbno, /* starting block in found extent */
145 xfs_extlen_t foundlen, /* length in found extent */
146 xfs_agblock_t *resbno, /* result block number */
147 xfs_extlen_t *reslen) /* result length */
152 /* Trim busy sections out of found extent */
153 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
155 if (args->alignment > 1 && len >= args->minlen) {
156 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
157 xfs_extlen_t diff = aligned_bno - bno;
159 *resbno = aligned_bno;
160 *reslen = diff >= len ? 0 : len - diff;
168 * Compute best start block and diff for "near" allocations.
169 * freelen >= wantlen already checked by caller.
171 STATIC xfs_extlen_t /* difference value (absolute) */
172 xfs_alloc_compute_diff(
173 xfs_agblock_t wantbno, /* target starting block */
174 xfs_extlen_t wantlen, /* target length */
175 xfs_extlen_t alignment, /* target alignment */
176 xfs_agblock_t freebno, /* freespace's starting block */
177 xfs_extlen_t freelen, /* freespace's length */
178 xfs_agblock_t *newbnop) /* result: best start block from free */
180 xfs_agblock_t freeend; /* end of freespace extent */
181 xfs_agblock_t newbno1; /* return block number */
182 xfs_agblock_t newbno2; /* other new block number */
183 xfs_extlen_t newlen1=0; /* length with newbno1 */
184 xfs_extlen_t newlen2=0; /* length with newbno2 */
185 xfs_agblock_t wantend; /* end of target extent */
187 ASSERT(freelen >= wantlen);
188 freeend = freebno + freelen;
189 wantend = wantbno + wantlen;
190 if (freebno >= wantbno) {
191 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
192 newbno1 = NULLAGBLOCK;
193 } else if (freeend >= wantend && alignment > 1) {
194 newbno1 = roundup(wantbno, alignment);
195 newbno2 = newbno1 - alignment;
196 if (newbno1 >= freeend)
197 newbno1 = NULLAGBLOCK;
199 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
200 if (newbno2 < freebno)
201 newbno2 = NULLAGBLOCK;
203 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
204 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
205 if (newlen1 < newlen2 ||
206 (newlen1 == newlen2 &&
207 XFS_ABSDIFF(newbno1, wantbno) >
208 XFS_ABSDIFF(newbno2, wantbno)))
210 } else if (newbno2 != NULLAGBLOCK)
212 } else if (freeend >= wantend) {
214 } else if (alignment > 1) {
215 newbno1 = roundup(freeend - wantlen, alignment);
216 if (newbno1 > freeend - wantlen &&
217 newbno1 - alignment >= freebno)
218 newbno1 -= alignment;
219 else if (newbno1 >= freeend)
220 newbno1 = NULLAGBLOCK;
222 newbno1 = freeend - wantlen;
224 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
228 * Fix up the length, based on mod and prod.
229 * len should be k * prod + mod for some k.
230 * If len is too small it is returned unchanged.
231 * If len hits maxlen it is left alone.
235 xfs_alloc_arg_t *args) /* allocation argument structure */
240 ASSERT(args->mod < args->prod);
242 ASSERT(rlen >= args->minlen);
243 ASSERT(rlen <= args->maxlen);
244 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
245 (args->mod == 0 && rlen < args->prod))
247 k = rlen % args->prod;
251 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
254 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
258 ASSERT(rlen >= args->minlen);
259 ASSERT(rlen <= args->maxlen);
264 * Fix up length if there is too little space left in the a.g.
265 * Return 1 if ok, 0 if too little, should give up.
268 xfs_alloc_fix_minleft(
269 xfs_alloc_arg_t *args) /* allocation argument structure */
271 xfs_agf_t *agf; /* a.g. freelist header */
272 int diff; /* free space difference */
274 if (args->minleft == 0)
276 agf = XFS_BUF_TO_AGF(args->agbp);
277 diff = be32_to_cpu(agf->agf_freeblks)
278 - args->len - args->minleft;
281 args->len += diff; /* shrink the allocated space */
282 if (args->len >= args->minlen)
284 args->agbno = NULLAGBLOCK;
289 * Update the two btrees, logically removing from freespace the extent
290 * starting at rbno, rlen blocks. The extent is contained within the
291 * actual (current) free extent fbno for flen blocks.
292 * Flags are passed in indicating whether the cursors are set to the
295 STATIC int /* error code */
296 xfs_alloc_fixup_trees(
297 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
298 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
299 xfs_agblock_t fbno, /* starting block of free extent */
300 xfs_extlen_t flen, /* length of free extent */
301 xfs_agblock_t rbno, /* starting block of returned extent */
302 xfs_extlen_t rlen, /* length of returned extent */
303 int flags) /* flags, XFSA_FIXUP_... */
305 int error; /* error code */
306 int i; /* operation results */
307 xfs_agblock_t nfbno1; /* first new free startblock */
308 xfs_agblock_t nfbno2; /* second new free startblock */
309 xfs_extlen_t nflen1=0; /* first new free length */
310 xfs_extlen_t nflen2=0; /* second new free length */
313 * Look up the record in the by-size tree if necessary.
315 if (flags & XFSA_FIXUP_CNT_OK) {
317 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
319 XFS_WANT_CORRUPTED_RETURN(
320 i == 1 && nfbno1 == fbno && nflen1 == flen);
323 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
325 XFS_WANT_CORRUPTED_RETURN(i == 1);
328 * Look up the record in the by-block tree if necessary.
330 if (flags & XFSA_FIXUP_BNO_OK) {
332 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
334 XFS_WANT_CORRUPTED_RETURN(
335 i == 1 && nfbno1 == fbno && nflen1 == flen);
338 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
340 XFS_WANT_CORRUPTED_RETURN(i == 1);
344 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
345 struct xfs_btree_block *bnoblock;
346 struct xfs_btree_block *cntblock;
348 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
349 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
351 XFS_WANT_CORRUPTED_RETURN(
352 bnoblock->bb_numrecs == cntblock->bb_numrecs);
357 * Deal with all four cases: the allocated record is contained
358 * within the freespace record, so we can have new freespace
359 * at either (or both) end, or no freespace remaining.
361 if (rbno == fbno && rlen == flen)
362 nfbno1 = nfbno2 = NULLAGBLOCK;
363 else if (rbno == fbno) {
364 nfbno1 = rbno + rlen;
365 nflen1 = flen - rlen;
366 nfbno2 = NULLAGBLOCK;
367 } else if (rbno + rlen == fbno + flen) {
369 nflen1 = flen - rlen;
370 nfbno2 = NULLAGBLOCK;
373 nflen1 = rbno - fbno;
374 nfbno2 = rbno + rlen;
375 nflen2 = (fbno + flen) - nfbno2;
378 * Delete the entry from the by-size btree.
380 if ((error = xfs_btree_delete(cnt_cur, &i)))
382 XFS_WANT_CORRUPTED_RETURN(i == 1);
384 * Add new by-size btree entry(s).
386 if (nfbno1 != NULLAGBLOCK) {
387 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
389 XFS_WANT_CORRUPTED_RETURN(i == 0);
390 if ((error = xfs_btree_insert(cnt_cur, &i)))
392 XFS_WANT_CORRUPTED_RETURN(i == 1);
394 if (nfbno2 != NULLAGBLOCK) {
395 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
397 XFS_WANT_CORRUPTED_RETURN(i == 0);
398 if ((error = xfs_btree_insert(cnt_cur, &i)))
400 XFS_WANT_CORRUPTED_RETURN(i == 1);
403 * Fix up the by-block btree entry(s).
405 if (nfbno1 == NULLAGBLOCK) {
407 * No remaining freespace, just delete the by-block tree entry.
409 if ((error = xfs_btree_delete(bno_cur, &i)))
411 XFS_WANT_CORRUPTED_RETURN(i == 1);
414 * Update the by-block entry to start later|be shorter.
416 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
419 if (nfbno2 != NULLAGBLOCK) {
421 * 2 resulting free entries, need to add one.
423 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
425 XFS_WANT_CORRUPTED_RETURN(i == 0);
426 if ((error = xfs_btree_insert(bno_cur, &i)))
428 XFS_WANT_CORRUPTED_RETURN(i == 1);
437 #ifdef WHEN_CRCS_COME_ALONG
439 * we cannot actually do any verification of the AGFL because mkfs does
440 * not initialise the AGFL to zero or NULL. Hence the only valid part of
441 * the AGFL is what the AGF says is active. We can't get to the AGF, so
442 * we can't verify just those entries are valid.
444 * This problem goes away when the CRC format change comes along as that
445 * requires the AGFL to be initialised by mkfs. At that point, we can
446 * verify the blocks in the agfl -active or not- lie within the bounds
447 * of the AG. Until then, just leave this check ifdef'd out.
449 struct xfs_mount *mp = bp->b_target->bt_mount;
450 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
455 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
456 if (be32_to_cpu(agfl->agfl_bno[i]) == NULLAGBLOCK ||
457 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
462 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, agfl);
463 xfs_buf_ioerror(bp, EFSCORRUPTED);
469 xfs_agfl_write_verify(
476 xfs_agfl_read_verify(
482 const struct xfs_buf_ops xfs_agfl_buf_ops = {
483 .verify_read = xfs_agfl_read_verify,
484 .verify_write = xfs_agfl_write_verify,
488 * Read in the allocation group free block array.
490 STATIC int /* error */
492 xfs_mount_t *mp, /* mount point structure */
493 xfs_trans_t *tp, /* transaction pointer */
494 xfs_agnumber_t agno, /* allocation group number */
495 xfs_buf_t **bpp) /* buffer for the ag free block array */
497 xfs_buf_t *bp; /* return value */
500 ASSERT(agno != NULLAGNUMBER);
501 error = xfs_trans_read_buf(
502 mp, tp, mp->m_ddev_targp,
503 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
504 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
507 ASSERT(!xfs_buf_geterror(bp));
508 xfs_buf_set_ref(bp, XFS_AGFL_REF);
514 xfs_alloc_update_counters(
515 struct xfs_trans *tp,
516 struct xfs_perag *pag,
517 struct xfs_buf *agbp,
520 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
522 pag->pagf_freeblks += len;
523 be32_add_cpu(&agf->agf_freeblks, len);
525 xfs_trans_agblocks_delta(tp, len);
526 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
527 be32_to_cpu(agf->agf_length)))
530 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
535 * Allocation group level functions.
539 * Allocate a variable extent in the allocation group agno.
540 * Type and bno are used to determine where in the allocation group the
542 * Extent's length (returned in *len) will be between minlen and maxlen,
543 * and of the form k * prod + mod unless there's nothing that large.
544 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
546 STATIC int /* error */
547 xfs_alloc_ag_vextent(
548 xfs_alloc_arg_t *args) /* argument structure for allocation */
552 ASSERT(args->minlen > 0);
553 ASSERT(args->maxlen > 0);
554 ASSERT(args->minlen <= args->maxlen);
555 ASSERT(args->mod < args->prod);
556 ASSERT(args->alignment > 0);
558 * Branch to correct routine based on the type.
561 switch (args->type) {
562 case XFS_ALLOCTYPE_THIS_AG:
563 error = xfs_alloc_ag_vextent_size(args);
565 case XFS_ALLOCTYPE_NEAR_BNO:
566 error = xfs_alloc_ag_vextent_near(args);
568 case XFS_ALLOCTYPE_THIS_BNO:
569 error = xfs_alloc_ag_vextent_exact(args);
576 if (error || args->agbno == NULLAGBLOCK)
579 ASSERT(args->len >= args->minlen);
580 ASSERT(args->len <= args->maxlen);
581 ASSERT(!args->wasfromfl || !args->isfl);
582 ASSERT(args->agbno % args->alignment == 0);
584 if (!args->wasfromfl) {
585 error = xfs_alloc_update_counters(args->tp, args->pag,
587 -((long)(args->len)));
591 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
592 args->agbno, args->len));
596 xfs_trans_mod_sb(args->tp, args->wasdel ?
597 XFS_TRANS_SB_RES_FDBLOCKS :
598 XFS_TRANS_SB_FDBLOCKS,
599 -((long)(args->len)));
602 XFS_STATS_INC(xs_allocx);
603 XFS_STATS_ADD(xs_allocb, args->len);
608 * Allocate a variable extent at exactly agno/bno.
609 * Extent's length (returned in *len) will be between minlen and maxlen,
610 * and of the form k * prod + mod unless there's nothing that large.
611 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
613 STATIC int /* error */
614 xfs_alloc_ag_vextent_exact(
615 xfs_alloc_arg_t *args) /* allocation argument structure */
617 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
618 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
620 xfs_agblock_t fbno; /* start block of found extent */
621 xfs_extlen_t flen; /* length of found extent */
622 xfs_agblock_t tbno; /* start block of trimmed extent */
623 xfs_extlen_t tlen; /* length of trimmed extent */
624 xfs_agblock_t tend; /* end block of trimmed extent */
625 int i; /* success/failure of operation */
627 ASSERT(args->alignment == 1);
630 * Allocate/initialize a cursor for the by-number freespace btree.
632 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
633 args->agno, XFS_BTNUM_BNO);
636 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
637 * Look for the closest free block <= bno, it must contain bno
638 * if any free block does.
640 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
647 * Grab the freespace record.
649 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
652 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
653 ASSERT(fbno <= args->agbno);
656 * Check for overlapping busy extents.
658 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
661 * Give up if the start of the extent is busy, or the freespace isn't
662 * long enough for the minimum request.
664 if (tbno > args->agbno)
666 if (tlen < args->minlen)
669 if (tend < args->agbno + args->minlen)
673 * End of extent will be smaller of the freespace end and the
674 * maximal requested end.
676 * Fix the length according to mod and prod if given.
678 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
680 xfs_alloc_fix_len(args);
681 if (!xfs_alloc_fix_minleft(args))
684 ASSERT(args->agbno + args->len <= tend);
687 * We are allocating agbno for args->len
688 * Allocate/initialize a cursor for the by-size btree.
690 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
691 args->agno, XFS_BTNUM_CNT);
692 ASSERT(args->agbno + args->len <=
693 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
694 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
695 args->len, XFSA_FIXUP_BNO_OK);
697 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
701 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
702 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
705 trace_xfs_alloc_exact_done(args);
709 /* Didn't find it, return null. */
710 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
711 args->agbno = NULLAGBLOCK;
712 trace_xfs_alloc_exact_notfound(args);
716 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
717 trace_xfs_alloc_exact_error(args);
722 * Search the btree in a given direction via the search cursor and compare
723 * the records found against the good extent we've already found.
726 xfs_alloc_find_best_extent(
727 struct xfs_alloc_arg *args, /* allocation argument structure */
728 struct xfs_btree_cur **gcur, /* good cursor */
729 struct xfs_btree_cur **scur, /* searching cursor */
730 xfs_agblock_t gdiff, /* difference for search comparison */
731 xfs_agblock_t *sbno, /* extent found by search */
732 xfs_extlen_t *slen, /* extent length */
733 xfs_agblock_t *sbnoa, /* aligned extent found by search */
734 xfs_extlen_t *slena, /* aligned extent length */
735 int dir) /* 0 = search right, 1 = search left */
742 /* The good extent is perfect, no need to search. */
747 * Look until we find a better one, run out of space or run off the end.
750 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
753 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
754 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
757 * The good extent is closer than this one.
760 if (*sbnoa >= args->agbno + gdiff)
763 if (*sbnoa <= args->agbno - gdiff)
768 * Same distance, compare length and pick the best.
770 if (*slena >= args->minlen) {
771 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
772 xfs_alloc_fix_len(args);
774 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
775 args->alignment, *sbnoa,
779 * Choose closer size and invalidate other cursor.
787 error = xfs_btree_increment(*scur, 0, &i);
789 error = xfs_btree_decrement(*scur, 0, &i);
795 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
800 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
805 /* caller invalidates cursors */
810 * Allocate a variable extent near bno in the allocation group agno.
811 * Extent's length (returned in len) will be between minlen and maxlen,
812 * and of the form k * prod + mod unless there's nothing that large.
813 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
815 STATIC int /* error */
816 xfs_alloc_ag_vextent_near(
817 xfs_alloc_arg_t *args) /* allocation argument structure */
819 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
820 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
821 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
822 xfs_agblock_t gtbno; /* start bno of right side entry */
823 xfs_agblock_t gtbnoa; /* aligned ... */
824 xfs_extlen_t gtdiff; /* difference to right side entry */
825 xfs_extlen_t gtlen; /* length of right side entry */
826 xfs_extlen_t gtlena; /* aligned ... */
827 xfs_agblock_t gtnew; /* useful start bno of right side */
828 int error; /* error code */
829 int i; /* result code, temporary */
830 int j; /* result code, temporary */
831 xfs_agblock_t ltbno; /* start bno of left side entry */
832 xfs_agblock_t ltbnoa; /* aligned ... */
833 xfs_extlen_t ltdiff; /* difference to left side entry */
834 xfs_extlen_t ltlen; /* length of left side entry */
835 xfs_extlen_t ltlena; /* aligned ... */
836 xfs_agblock_t ltnew; /* useful start bno of left side */
837 xfs_extlen_t rlen; /* length of returned extent */
839 #if defined(DEBUG) && defined(__KERNEL__)
841 * Randomly don't execute the first algorithm.
843 int dofirst; /* set to do first algorithm */
845 dofirst = random32() & 1;
856 * Get a cursor for the by-size btree.
858 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
859 args->agno, XFS_BTNUM_CNT);
862 * See if there are any free extents as big as maxlen.
864 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
867 * If none, then pick up the last entry in the tree unless the
871 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
874 if (i == 0 || ltlen == 0) {
875 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
876 trace_xfs_alloc_near_noentry(args);
885 * If the requested extent is large wrt the freespaces available
886 * in this a.g., then the cursor will be pointing to a btree entry
887 * near the right edge of the tree. If it's in the last btree leaf
888 * block, then we just examine all the entries in that block
889 * that are big enough, and pick the best one.
890 * This is written as a while loop so we can break out of it,
891 * but we never loop back to the top.
893 while (xfs_btree_islastblock(cnt_cur, 0)) {
897 xfs_agblock_t bnew=0;
899 #if defined(DEBUG) && defined(__KERNEL__)
904 * Start from the entry that lookup found, sequence through
905 * all larger free blocks. If we're actually pointing at a
906 * record smaller than maxlen, go to the start of this block,
907 * and skip all those smaller than minlen.
909 if (ltlen || args->alignment > 1) {
910 cnt_cur->bc_ptrs[0] = 1;
912 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
915 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
916 if (ltlen >= args->minlen)
918 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
921 ASSERT(ltlen >= args->minlen);
925 i = cnt_cur->bc_ptrs[0];
926 for (j = 1, blen = 0, bdiff = 0;
927 !error && j && (blen < args->maxlen || bdiff > 0);
928 error = xfs_btree_increment(cnt_cur, 0, &j)) {
930 * For each entry, decide if it's better than
931 * the previous best entry.
933 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
935 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
936 xfs_alloc_compute_aligned(args, ltbno, ltlen,
938 if (ltlena < args->minlen)
940 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
941 xfs_alloc_fix_len(args);
942 ASSERT(args->len >= args->minlen);
943 if (args->len < blen)
945 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
946 args->alignment, ltbnoa, ltlena, <new);
947 if (ltnew != NULLAGBLOCK &&
948 (args->len > blen || ltdiff < bdiff)) {
952 besti = cnt_cur->bc_ptrs[0];
956 * It didn't work. We COULD be in a case where
957 * there's a good record somewhere, so try again.
962 * Point at the best entry, and retrieve it again.
964 cnt_cur->bc_ptrs[0] = besti;
965 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
967 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
968 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
970 if (!xfs_alloc_fix_minleft(args)) {
971 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
972 trace_xfs_alloc_near_nominleft(args);
977 * We are allocating starting at bnew for blen blocks.
980 ASSERT(bnew >= ltbno);
981 ASSERT(bnew + blen <= ltbno + ltlen);
983 * Set up a cursor for the by-bno tree.
985 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
986 args->agbp, args->agno, XFS_BTNUM_BNO);
988 * Fix up the btree entries.
990 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
991 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
993 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
994 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
996 trace_xfs_alloc_near_first(args);
1001 * Search in the by-bno tree to the left and to the right
1002 * simultaneously, until in each case we find a space big enough,
1003 * or run into the edge of the tree. When we run into the edge,
1004 * we deallocate that cursor.
1005 * If both searches succeed, we compare the two spaces and pick
1007 * With alignment, it's possible for both to fail; the upper
1008 * level algorithm that picks allocation groups for allocations
1009 * is not supposed to do this.
1012 * Allocate and initialize the cursor for the leftward search.
1014 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1015 args->agno, XFS_BTNUM_BNO);
1017 * Lookup <= bno to find the leftward search's starting point.
1019 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1023 * Didn't find anything; use this cursor for the rightward
1026 bno_cur_gt = bno_cur_lt;
1030 * Found something. Duplicate the cursor for the rightward search.
1032 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1035 * Increment the cursor, so we will point at the entry just right
1036 * of the leftward entry if any, or to the leftmost entry.
1038 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1042 * It failed, there are no rightward entries.
1044 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1048 * Loop going left with the leftward cursor, right with the
1049 * rightward cursor, until either both directions give up or
1050 * we find an entry at least as big as minlen.
1054 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1056 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1057 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1059 if (ltlena >= args->minlen)
1061 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1064 xfs_btree_del_cursor(bno_cur_lt,
1070 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1072 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1073 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1075 if (gtlena >= args->minlen)
1077 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1080 xfs_btree_del_cursor(bno_cur_gt,
1085 } while (bno_cur_lt || bno_cur_gt);
1088 * Got both cursors still active, need to find better entry.
1090 if (bno_cur_lt && bno_cur_gt) {
1091 if (ltlena >= args->minlen) {
1093 * Left side is good, look for a right side entry.
1095 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1096 xfs_alloc_fix_len(args);
1097 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1098 args->alignment, ltbnoa, ltlena, <new);
1100 error = xfs_alloc_find_best_extent(args,
1101 &bno_cur_lt, &bno_cur_gt,
1102 ltdiff, >bno, >len,
1104 0 /* search right */);
1106 ASSERT(gtlena >= args->minlen);
1109 * Right side is good, look for a left side entry.
1111 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1112 xfs_alloc_fix_len(args);
1113 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1114 args->alignment, gtbnoa, gtlena, >new);
1116 error = xfs_alloc_find_best_extent(args,
1117 &bno_cur_gt, &bno_cur_lt,
1118 gtdiff, <bno, <len,
1120 1 /* search left */);
1128 * If we couldn't get anything, give up.
1130 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1131 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1134 trace_xfs_alloc_near_busy(args);
1135 xfs_log_force(args->mp, XFS_LOG_SYNC);
1138 trace_xfs_alloc_size_neither(args);
1139 args->agbno = NULLAGBLOCK;
1144 * At this point we have selected a freespace entry, either to the
1145 * left or to the right. If it's on the right, copy all the
1146 * useful variables to the "left" set so we only have one
1147 * copy of this code.
1150 bno_cur_lt = bno_cur_gt;
1161 * Fix up the length and compute the useful address.
1163 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1164 xfs_alloc_fix_len(args);
1165 if (!xfs_alloc_fix_minleft(args)) {
1166 trace_xfs_alloc_near_nominleft(args);
1167 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1168 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1172 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1173 ltbnoa, ltlena, <new);
1174 ASSERT(ltnew >= ltbno);
1175 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1176 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1177 args->agbno = ltnew;
1179 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1180 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1184 trace_xfs_alloc_near_greater(args);
1186 trace_xfs_alloc_near_lesser(args);
1188 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1189 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1193 trace_xfs_alloc_near_error(args);
1194 if (cnt_cur != NULL)
1195 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1196 if (bno_cur_lt != NULL)
1197 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1198 if (bno_cur_gt != NULL)
1199 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1204 * Allocate a variable extent anywhere in the allocation group agno.
1205 * Extent's length (returned in len) will be between minlen and maxlen,
1206 * and of the form k * prod + mod unless there's nothing that large.
1207 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1209 STATIC int /* error */
1210 xfs_alloc_ag_vextent_size(
1211 xfs_alloc_arg_t *args) /* allocation argument structure */
1213 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1214 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1215 int error; /* error result */
1216 xfs_agblock_t fbno; /* start of found freespace */
1217 xfs_extlen_t flen; /* length of found freespace */
1218 int i; /* temp status variable */
1219 xfs_agblock_t rbno; /* returned block number */
1220 xfs_extlen_t rlen; /* length of returned extent */
1225 * Allocate and initialize a cursor for the by-size btree.
1227 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1228 args->agno, XFS_BTNUM_CNT);
1232 * Look for an entry >= maxlen+alignment-1 blocks.
1234 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1235 args->maxlen + args->alignment - 1, &i)))
1239 * If none or we have busy extents that we cannot allocate from, then
1240 * we have to settle for a smaller extent. In the case that there are
1241 * no large extents, this will return the last entry in the tree unless
1242 * the tree is empty. In the case that there are only busy large
1243 * extents, this will return the largest small extent unless there
1244 * are no smaller extents available.
1246 if (!i || forced > 1) {
1247 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1251 if (i == 0 || flen == 0) {
1252 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1253 trace_xfs_alloc_size_noentry(args);
1257 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1260 * Search for a non-busy extent that is large enough.
1261 * If we are at low space, don't check, or if we fall of
1262 * the end of the btree, turn off the busy check and
1266 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1269 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1271 xfs_alloc_compute_aligned(args, fbno, flen,
1274 if (rlen >= args->maxlen)
1277 error = xfs_btree_increment(cnt_cur, 0, &i);
1282 * Our only valid extents must have been busy.
1283 * Make it unbusy by forcing the log out and
1284 * retrying. If we've been here before, forcing
1285 * the log isn't making the extents available,
1286 * which means they have probably been freed in
1287 * this transaction. In that case, we have to
1288 * give up on them and we'll attempt a minlen
1289 * allocation the next time around.
1291 xfs_btree_del_cursor(cnt_cur,
1293 trace_xfs_alloc_size_busy(args);
1295 xfs_log_force(args->mp, XFS_LOG_SYNC);
1302 * In the first case above, we got the last entry in the
1303 * by-size btree. Now we check to see if the space hits maxlen
1304 * once aligned; if not, we search left for something better.
1305 * This can't happen in the second case above.
1307 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1308 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1309 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1310 if (rlen < args->maxlen) {
1311 xfs_agblock_t bestfbno;
1312 xfs_extlen_t bestflen;
1313 xfs_agblock_t bestrbno;
1314 xfs_extlen_t bestrlen;
1321 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1325 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1328 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1329 if (flen < bestrlen)
1331 xfs_alloc_compute_aligned(args, fbno, flen,
1333 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1334 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1335 (rlen <= flen && rbno + rlen <= fbno + flen),
1337 if (rlen > bestrlen) {
1342 if (rlen == args->maxlen)
1346 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1349 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1355 args->wasfromfl = 0;
1357 * Fix up the length.
1360 if (rlen < args->minlen) {
1362 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1363 trace_xfs_alloc_size_busy(args);
1364 xfs_log_force(args->mp, XFS_LOG_SYNC);
1369 xfs_alloc_fix_len(args);
1371 if (!xfs_alloc_fix_minleft(args))
1374 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1376 * Allocate and initialize a cursor for the by-block tree.
1378 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1379 args->agno, XFS_BTNUM_BNO);
1380 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1381 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1383 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1384 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1385 cnt_cur = bno_cur = NULL;
1388 XFS_WANT_CORRUPTED_GOTO(
1389 args->agbno + args->len <=
1390 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1392 trace_xfs_alloc_size_done(args);
1396 trace_xfs_alloc_size_error(args);
1398 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1400 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1404 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1405 trace_xfs_alloc_size_nominleft(args);
1406 args->agbno = NULLAGBLOCK;
1411 * Deal with the case where only small freespaces remain.
1412 * Either return the contents of the last freespace record,
1413 * or allocate space from the freelist if there is nothing in the tree.
1415 STATIC int /* error */
1416 xfs_alloc_ag_vextent_small(
1417 xfs_alloc_arg_t *args, /* allocation argument structure */
1418 xfs_btree_cur_t *ccur, /* by-size cursor */
1419 xfs_agblock_t *fbnop, /* result block number */
1420 xfs_extlen_t *flenp, /* result length */
1421 int *stat) /* status: 0-freelist, 1-normal/none */
1428 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1431 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1433 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1436 * Nothing in the btree, try the freelist. Make sure
1437 * to respect minleft even when pulling from the
1440 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1441 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1443 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1446 if (fbno != NULLAGBLOCK) {
1447 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1450 if (args->userdata) {
1453 bp = xfs_btree_get_bufs(args->mp, args->tp,
1454 args->agno, fbno, 0);
1455 xfs_trans_binval(args->tp, bp);
1459 XFS_WANT_CORRUPTED_GOTO(
1460 args->agbno + args->len <=
1461 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1463 args->wasfromfl = 1;
1464 trace_xfs_alloc_small_freelist(args);
1469 * Nothing in the freelist.
1475 * Can't allocate from the freelist for some reason.
1482 * Can't do the allocation, give up.
1484 if (flen < args->minlen) {
1485 args->agbno = NULLAGBLOCK;
1486 trace_xfs_alloc_small_notenough(args);
1492 trace_xfs_alloc_small_done(args);
1496 trace_xfs_alloc_small_error(args);
1501 * Free the extent starting at agno/bno for length.
1503 STATIC int /* error */
1505 xfs_trans_t *tp, /* transaction pointer */
1506 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1507 xfs_agnumber_t agno, /* allocation group number */
1508 xfs_agblock_t bno, /* starting block number */
1509 xfs_extlen_t len, /* length of extent */
1510 int isfl) /* set if is freelist blocks - no sb acctg */
1512 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1513 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1514 int error; /* error return value */
1515 xfs_agblock_t gtbno; /* start of right neighbor block */
1516 xfs_extlen_t gtlen; /* length of right neighbor block */
1517 int haveleft; /* have a left neighbor block */
1518 int haveright; /* have a right neighbor block */
1519 int i; /* temp, result code */
1520 xfs_agblock_t ltbno; /* start of left neighbor block */
1521 xfs_extlen_t ltlen; /* length of left neighbor block */
1522 xfs_mount_t *mp; /* mount point struct for filesystem */
1523 xfs_agblock_t nbno; /* new starting block of freespace */
1524 xfs_extlen_t nlen; /* new length of freespace */
1525 xfs_perag_t *pag; /* per allocation group data */
1529 * Allocate and initialize a cursor for the by-block btree.
1531 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1534 * Look for a neighboring block on the left (lower block numbers)
1535 * that is contiguous with this space.
1537 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1541 * There is a block to our left.
1543 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1545 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1547 * It's not contiguous, though.
1549 if (ltbno + ltlen < bno)
1553 * If this failure happens the request to free this
1554 * space was invalid, it's (partly) already free.
1557 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1561 * Look for a neighboring block on the right (higher block numbers)
1562 * that is contiguous with this space.
1564 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1568 * There is a block to our right.
1570 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1572 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1574 * It's not contiguous, though.
1576 if (bno + len < gtbno)
1580 * If this failure happens the request to free this
1581 * space was invalid, it's (partly) already free.
1584 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1588 * Now allocate and initialize a cursor for the by-size tree.
1590 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1592 * Have both left and right contiguous neighbors.
1593 * Merge all three into a single free block.
1595 if (haveleft && haveright) {
1597 * Delete the old by-size entry on the left.
1599 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1601 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1602 if ((error = xfs_btree_delete(cnt_cur, &i)))
1604 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1606 * Delete the old by-size entry on the right.
1608 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1610 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1611 if ((error = xfs_btree_delete(cnt_cur, &i)))
1613 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1615 * Delete the old by-block entry for the right block.
1617 if ((error = xfs_btree_delete(bno_cur, &i)))
1619 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1621 * Move the by-block cursor back to the left neighbor.
1623 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1625 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1628 * Check that this is the right record: delete didn't
1629 * mangle the cursor.
1632 xfs_agblock_t xxbno;
1635 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1638 XFS_WANT_CORRUPTED_GOTO(
1639 i == 1 && xxbno == ltbno && xxlen == ltlen,
1644 * Update remaining by-block entry to the new, joined block.
1647 nlen = len + ltlen + gtlen;
1648 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1652 * Have only a left contiguous neighbor.
1653 * Merge it together with the new freespace.
1655 else if (haveleft) {
1657 * Delete the old by-size entry on the left.
1659 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1661 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1662 if ((error = xfs_btree_delete(cnt_cur, &i)))
1664 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1666 * Back up the by-block cursor to the left neighbor, and
1667 * update its length.
1669 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1671 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1674 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1678 * Have only a right contiguous neighbor.
1679 * Merge it together with the new freespace.
1681 else if (haveright) {
1683 * Delete the old by-size entry on the right.
1685 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1687 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1688 if ((error = xfs_btree_delete(cnt_cur, &i)))
1690 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1692 * Update the starting block and length of the right
1693 * neighbor in the by-block tree.
1697 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1701 * No contiguous neighbors.
1702 * Insert the new freespace into the by-block tree.
1707 if ((error = xfs_btree_insert(bno_cur, &i)))
1709 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1711 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1714 * In all cases we need to insert the new freespace in the by-size tree.
1716 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1718 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1719 if ((error = xfs_btree_insert(cnt_cur, &i)))
1721 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1722 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1726 * Update the freespace totals in the ag and superblock.
1728 pag = xfs_perag_get(mp, agno);
1729 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1735 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1736 XFS_STATS_INC(xs_freex);
1737 XFS_STATS_ADD(xs_freeb, len);
1739 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
1744 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
1746 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1748 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1753 * Visible (exported) allocation/free functions.
1754 * Some of these are used just by xfs_alloc_btree.c and this file.
1758 * Compute and fill in value of m_ag_maxlevels.
1761 xfs_alloc_compute_maxlevels(
1762 xfs_mount_t *mp) /* file system mount structure */
1770 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1771 minleafrecs = mp->m_alloc_mnr[0];
1772 minnoderecs = mp->m_alloc_mnr[1];
1773 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1774 for (level = 1; maxblocks > 1; level++)
1775 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1776 mp->m_ag_maxlevels = level;
1780 * Find the length of the longest extent in an AG.
1783 xfs_alloc_longest_free_extent(
1784 struct xfs_mount *mp,
1785 struct xfs_perag *pag)
1787 xfs_extlen_t need, delta = 0;
1789 need = XFS_MIN_FREELIST_PAG(pag, mp);
1790 if (need > pag->pagf_flcount)
1791 delta = need - pag->pagf_flcount;
1793 if (pag->pagf_longest > delta)
1794 return pag->pagf_longest - delta;
1795 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1799 * Decide whether to use this allocation group for this allocation.
1800 * If so, fix up the btree freelist's size.
1802 STATIC int /* error */
1803 xfs_alloc_fix_freelist(
1804 xfs_alloc_arg_t *args, /* allocation argument structure */
1805 int flags) /* XFS_ALLOC_FLAG_... */
1807 xfs_buf_t *agbp; /* agf buffer pointer */
1808 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1809 xfs_buf_t *agflbp;/* agfl buffer pointer */
1810 xfs_agblock_t bno; /* freelist block */
1811 xfs_extlen_t delta; /* new blocks needed in freelist */
1812 int error; /* error result code */
1813 xfs_extlen_t longest;/* longest extent in allocation group */
1814 xfs_mount_t *mp; /* file system mount point structure */
1815 xfs_extlen_t need; /* total blocks needed in freelist */
1816 xfs_perag_t *pag; /* per-ag information structure */
1817 xfs_alloc_arg_t targs; /* local allocation arguments */
1818 xfs_trans_t *tp; /* transaction pointer */
1824 if (!pag->pagf_init) {
1825 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1828 if (!pag->pagf_init) {
1829 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1830 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1838 * If this is a metadata preferred pag and we are user data
1839 * then try somewhere else if we are not being asked to
1840 * try harder at this point
1842 if (pag->pagf_metadata && args->userdata &&
1843 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1844 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1849 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1851 * If it looks like there isn't a long enough extent, or enough
1852 * total blocks, reject it.
1854 need = XFS_MIN_FREELIST_PAG(pag, mp);
1855 longest = xfs_alloc_longest_free_extent(mp, pag);
1856 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1858 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1859 need - args->total) < (int)args->minleft)) {
1861 xfs_trans_brelse(tp, agbp);
1868 * Get the a.g. freespace buffer.
1869 * Can fail if we're not blocking on locks, and it's held.
1872 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1876 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1877 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1883 * Figure out how many blocks we should have in the freelist.
1885 agf = XFS_BUF_TO_AGF(agbp);
1886 need = XFS_MIN_FREELIST(agf, mp);
1888 * If there isn't enough total or single-extent, reject it.
1890 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1891 delta = need > be32_to_cpu(agf->agf_flcount) ?
1892 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1893 longest = be32_to_cpu(agf->agf_longest);
1894 longest = (longest > delta) ? (longest - delta) :
1895 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1896 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1898 ((int)(be32_to_cpu(agf->agf_freeblks) +
1899 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1900 (int)args->minleft)) {
1901 xfs_trans_brelse(tp, agbp);
1907 * Make the freelist shorter if it's too long.
1909 while (be32_to_cpu(agf->agf_flcount) > need) {
1912 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1915 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1917 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1918 xfs_trans_binval(tp, bp);
1921 * Initialize the args structure.
1923 memset(&targs, 0, sizeof(targs));
1927 targs.agno = args->agno;
1928 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1929 targs.minalignslop = 0;
1930 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1931 targs.type = XFS_ALLOCTYPE_THIS_AG;
1933 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1936 * Make the freelist longer if it's too short.
1938 while (be32_to_cpu(agf->agf_flcount) < need) {
1940 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1942 * Allocate as many blocks as possible at once.
1944 if ((error = xfs_alloc_ag_vextent(&targs))) {
1945 xfs_trans_brelse(tp, agflbp);
1949 * Stop if we run out. Won't happen if callers are obeying
1950 * the restrictions correctly. Can happen for free calls
1951 * on a completely full ag.
1953 if (targs.agbno == NULLAGBLOCK) {
1954 if (flags & XFS_ALLOC_FLAG_FREEING)
1956 xfs_trans_brelse(tp, agflbp);
1961 * Put each allocated block on the list.
1963 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
1964 error = xfs_alloc_put_freelist(tp, agbp,
1970 xfs_trans_brelse(tp, agflbp);
1976 * Get a block from the freelist.
1977 * Returns with the buffer for the block gotten.
1980 xfs_alloc_get_freelist(
1981 xfs_trans_t *tp, /* transaction pointer */
1982 xfs_buf_t *agbp, /* buffer containing the agf structure */
1983 xfs_agblock_t *bnop, /* block address retrieved from freelist */
1984 int btreeblk) /* destination is a AGF btree */
1986 xfs_agf_t *agf; /* a.g. freespace structure */
1987 xfs_agfl_t *agfl; /* a.g. freelist structure */
1988 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
1989 xfs_agblock_t bno; /* block number returned */
1992 xfs_mount_t *mp; /* mount structure */
1993 xfs_perag_t *pag; /* per allocation group data */
1995 agf = XFS_BUF_TO_AGF(agbp);
1997 * Freelist is empty, give up.
1999 if (!agf->agf_flcount) {
2000 *bnop = NULLAGBLOCK;
2004 * Read the array of free blocks.
2007 if ((error = xfs_alloc_read_agfl(mp, tp,
2008 be32_to_cpu(agf->agf_seqno), &agflbp)))
2010 agfl = XFS_BUF_TO_AGFL(agflbp);
2012 * Get the block number and update the data structures.
2014 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2015 be32_add_cpu(&agf->agf_flfirst, 1);
2016 xfs_trans_brelse(tp, agflbp);
2017 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2018 agf->agf_flfirst = 0;
2020 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2021 be32_add_cpu(&agf->agf_flcount, -1);
2022 xfs_trans_agflist_delta(tp, -1);
2023 pag->pagf_flcount--;
2026 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2028 be32_add_cpu(&agf->agf_btreeblks, 1);
2029 pag->pagf_btreeblks++;
2030 logflags |= XFS_AGF_BTREEBLKS;
2033 xfs_alloc_log_agf(tp, agbp, logflags);
2040 * Log the given fields from the agf structure.
2044 xfs_trans_t *tp, /* transaction pointer */
2045 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2046 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2048 int first; /* first byte offset */
2049 int last; /* last byte offset */
2050 static const short offsets[] = {
2051 offsetof(xfs_agf_t, agf_magicnum),
2052 offsetof(xfs_agf_t, agf_versionnum),
2053 offsetof(xfs_agf_t, agf_seqno),
2054 offsetof(xfs_agf_t, agf_length),
2055 offsetof(xfs_agf_t, agf_roots[0]),
2056 offsetof(xfs_agf_t, agf_levels[0]),
2057 offsetof(xfs_agf_t, agf_flfirst),
2058 offsetof(xfs_agf_t, agf_fllast),
2059 offsetof(xfs_agf_t, agf_flcount),
2060 offsetof(xfs_agf_t, agf_freeblks),
2061 offsetof(xfs_agf_t, agf_longest),
2062 offsetof(xfs_agf_t, agf_btreeblks),
2066 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2068 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2069 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2073 * Interface for inode allocation to force the pag data to be initialized.
2076 xfs_alloc_pagf_init(
2077 xfs_mount_t *mp, /* file system mount structure */
2078 xfs_trans_t *tp, /* transaction pointer */
2079 xfs_agnumber_t agno, /* allocation group number */
2080 int flags) /* XFS_ALLOC_FLAGS_... */
2085 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2088 xfs_trans_brelse(tp, bp);
2093 * Put the block on the freelist for the allocation group.
2096 xfs_alloc_put_freelist(
2097 xfs_trans_t *tp, /* transaction pointer */
2098 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2099 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2100 xfs_agblock_t bno, /* block being freed */
2101 int btreeblk) /* block came from a AGF btree */
2103 xfs_agf_t *agf; /* a.g. freespace structure */
2104 xfs_agfl_t *agfl; /* a.g. free block array */
2105 __be32 *blockp;/* pointer to array entry */
2108 xfs_mount_t *mp; /* mount structure */
2109 xfs_perag_t *pag; /* per allocation group data */
2111 agf = XFS_BUF_TO_AGF(agbp);
2114 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2115 be32_to_cpu(agf->agf_seqno), &agflbp)))
2117 agfl = XFS_BUF_TO_AGFL(agflbp);
2118 be32_add_cpu(&agf->agf_fllast, 1);
2119 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2120 agf->agf_fllast = 0;
2122 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2123 be32_add_cpu(&agf->agf_flcount, 1);
2124 xfs_trans_agflist_delta(tp, 1);
2125 pag->pagf_flcount++;
2127 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2129 be32_add_cpu(&agf->agf_btreeblks, -1);
2130 pag->pagf_btreeblks--;
2131 logflags |= XFS_AGF_BTREEBLKS;
2135 xfs_alloc_log_agf(tp, agbp, logflags);
2137 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2138 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2139 *blockp = cpu_to_be32(bno);
2140 xfs_alloc_log_agf(tp, agbp, logflags);
2141 xfs_trans_log_buf(tp, agflbp,
2142 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2143 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2144 sizeof(xfs_agblock_t) - 1));
2152 struct xfs_mount *mp = bp->b_target->bt_mount;
2153 struct xfs_agf *agf;
2156 agf = XFS_BUF_TO_AGF(bp);
2158 agf_ok = agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2159 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2160 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2161 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2162 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2163 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
2166 * during growfs operations, the perag is not fully initialised,
2167 * so we can't use it for any useful checking. growfs ensures we can't
2168 * use it by using uncached buffers that don't have the perag attached
2169 * so we can detect and avoid this problem.
2172 agf_ok = agf_ok && be32_to_cpu(agf->agf_seqno) ==
2173 bp->b_pag->pag_agno;
2175 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2176 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2177 be32_to_cpu(agf->agf_length);
2179 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2180 XFS_RANDOM_ALLOC_READ_AGF))) {
2181 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, agf);
2182 xfs_buf_ioerror(bp, EFSCORRUPTED);
2187 xfs_agf_read_verify(
2194 xfs_agf_write_verify(
2200 const struct xfs_buf_ops xfs_agf_buf_ops = {
2201 .verify_read = xfs_agf_read_verify,
2202 .verify_write = xfs_agf_write_verify,
2206 * Read in the allocation group header (free/alloc section).
2210 struct xfs_mount *mp, /* mount point structure */
2211 struct xfs_trans *tp, /* transaction pointer */
2212 xfs_agnumber_t agno, /* allocation group number */
2213 int flags, /* XFS_BUF_ */
2214 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2218 ASSERT(agno != NULLAGNUMBER);
2219 error = xfs_trans_read_buf(
2220 mp, tp, mp->m_ddev_targp,
2221 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2222 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2228 ASSERT(!(*bpp)->b_error);
2229 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2234 * Read in the allocation group header (free/alloc section).
2238 struct xfs_mount *mp, /* mount point structure */
2239 struct xfs_trans *tp, /* transaction pointer */
2240 xfs_agnumber_t agno, /* allocation group number */
2241 int flags, /* XFS_ALLOC_FLAG_... */
2242 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2244 struct xfs_agf *agf; /* ag freelist header */
2245 struct xfs_perag *pag; /* per allocation group data */
2248 ASSERT(agno != NULLAGNUMBER);
2250 error = xfs_read_agf(mp, tp, agno,
2251 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2257 ASSERT(!(*bpp)->b_error);
2259 agf = XFS_BUF_TO_AGF(*bpp);
2260 pag = xfs_perag_get(mp, agno);
2261 if (!pag->pagf_init) {
2262 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2263 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2264 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2265 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2266 pag->pagf_levels[XFS_BTNUM_BNOi] =
2267 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2268 pag->pagf_levels[XFS_BTNUM_CNTi] =
2269 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2270 spin_lock_init(&pag->pagb_lock);
2271 pag->pagb_count = 0;
2272 pag->pagb_tree = RB_ROOT;
2276 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2277 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2278 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2279 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2280 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2281 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2282 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2283 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2284 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2292 * Allocate an extent (variable-size).
2293 * Depending on the allocation type, we either look in a single allocation
2294 * group or loop over the allocation groups to find the result.
2298 xfs_alloc_arg_t *args) /* allocation argument structure */
2300 xfs_agblock_t agsize; /* allocation group size */
2302 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2303 xfs_extlen_t minleft;/* minimum left value, temp copy */
2304 xfs_mount_t *mp; /* mount structure pointer */
2305 xfs_agnumber_t sagno; /* starting allocation group number */
2306 xfs_alloctype_t type; /* input allocation type */
2309 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2312 type = args->otype = args->type;
2313 args->agbno = NULLAGBLOCK;
2315 * Just fix this up, for the case where the last a.g. is shorter
2316 * (or there's only one a.g.) and the caller couldn't easily figure
2317 * that out (xfs_bmap_alloc).
2319 agsize = mp->m_sb.sb_agblocks;
2320 if (args->maxlen > agsize)
2321 args->maxlen = agsize;
2322 if (args->alignment == 0)
2323 args->alignment = 1;
2324 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2325 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2326 ASSERT(args->minlen <= args->maxlen);
2327 ASSERT(args->minlen <= agsize);
2328 ASSERT(args->mod < args->prod);
2329 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2330 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2331 args->minlen > args->maxlen || args->minlen > agsize ||
2332 args->mod >= args->prod) {
2333 args->fsbno = NULLFSBLOCK;
2334 trace_xfs_alloc_vextent_badargs(args);
2337 minleft = args->minleft;
2340 case XFS_ALLOCTYPE_THIS_AG:
2341 case XFS_ALLOCTYPE_NEAR_BNO:
2342 case XFS_ALLOCTYPE_THIS_BNO:
2344 * These three force us into a single a.g.
2346 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2347 args->pag = xfs_perag_get(mp, args->agno);
2349 error = xfs_alloc_fix_freelist(args, 0);
2350 args->minleft = minleft;
2352 trace_xfs_alloc_vextent_nofix(args);
2356 trace_xfs_alloc_vextent_noagbp(args);
2359 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2360 if ((error = xfs_alloc_ag_vextent(args)))
2363 case XFS_ALLOCTYPE_START_BNO:
2365 * Try near allocation first, then anywhere-in-ag after
2366 * the first a.g. fails.
2368 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2369 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2370 args->fsbno = XFS_AGB_TO_FSB(mp,
2371 ((mp->m_agfrotor / rotorstep) %
2372 mp->m_sb.sb_agcount), 0);
2375 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2376 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2378 case XFS_ALLOCTYPE_ANY_AG:
2379 case XFS_ALLOCTYPE_START_AG:
2380 case XFS_ALLOCTYPE_FIRST_AG:
2382 * Rotate through the allocation groups looking for a winner.
2384 if (type == XFS_ALLOCTYPE_ANY_AG) {
2386 * Start with the last place we left off.
2388 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2389 mp->m_sb.sb_agcount;
2390 args->type = XFS_ALLOCTYPE_THIS_AG;
2391 flags = XFS_ALLOC_FLAG_TRYLOCK;
2392 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2394 * Start with allocation group given by bno.
2396 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2397 args->type = XFS_ALLOCTYPE_THIS_AG;
2401 if (type == XFS_ALLOCTYPE_START_AG)
2402 args->type = XFS_ALLOCTYPE_THIS_AG;
2404 * Start with the given allocation group.
2406 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2407 flags = XFS_ALLOC_FLAG_TRYLOCK;
2410 * Loop over allocation groups twice; first time with
2411 * trylock set, second time without.
2414 args->pag = xfs_perag_get(mp, args->agno);
2415 if (no_min) args->minleft = 0;
2416 error = xfs_alloc_fix_freelist(args, flags);
2417 args->minleft = minleft;
2419 trace_xfs_alloc_vextent_nofix(args);
2423 * If we get a buffer back then the allocation will fly.
2426 if ((error = xfs_alloc_ag_vextent(args)))
2431 trace_xfs_alloc_vextent_loopfailed(args);
2434 * Didn't work, figure out the next iteration.
2436 if (args->agno == sagno &&
2437 type == XFS_ALLOCTYPE_START_BNO)
2438 args->type = XFS_ALLOCTYPE_THIS_AG;
2440 * For the first allocation, we can try any AG to get
2441 * space. However, if we already have allocated a
2442 * block, we don't want to try AGs whose number is below
2443 * sagno. Otherwise, we may end up with out-of-order
2444 * locking of AGF, which might cause deadlock.
2446 if (++(args->agno) == mp->m_sb.sb_agcount) {
2447 if (args->firstblock != NULLFSBLOCK)
2453 * Reached the starting a.g., must either be done
2454 * or switch to non-trylock mode.
2456 if (args->agno == sagno) {
2458 args->agbno = NULLAGBLOCK;
2459 trace_xfs_alloc_vextent_allfailed(args);
2466 if (type == XFS_ALLOCTYPE_START_BNO) {
2467 args->agbno = XFS_FSB_TO_AGBNO(mp,
2469 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2473 xfs_perag_put(args->pag);
2475 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2476 if (args->agno == sagno)
2477 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2478 (mp->m_sb.sb_agcount * rotorstep);
2480 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2481 (mp->m_sb.sb_agcount * rotorstep);
2488 if (args->agbno == NULLAGBLOCK)
2489 args->fsbno = NULLFSBLOCK;
2491 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2493 ASSERT(args->len >= args->minlen);
2494 ASSERT(args->len <= args->maxlen);
2495 ASSERT(args->agbno % args->alignment == 0);
2496 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2500 xfs_perag_put(args->pag);
2503 xfs_perag_put(args->pag);
2509 * Just break up the extent address and hand off to xfs_free_ag_extent
2510 * after fixing up the freelist.
2514 xfs_trans_t *tp, /* transaction pointer */
2515 xfs_fsblock_t bno, /* starting block number of extent */
2516 xfs_extlen_t len) /* length of extent */
2518 xfs_alloc_arg_t args;
2522 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2524 args.mp = tp->t_mountp;
2527 * validate that the block number is legal - the enables us to detect
2528 * and handle a silent filesystem corruption rather than crashing.
2530 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2531 if (args.agno >= args.mp->m_sb.sb_agcount)
2532 return EFSCORRUPTED;
2534 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2535 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2536 return EFSCORRUPTED;
2538 args.pag = xfs_perag_get(args.mp, args.agno);
2541 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2545 /* validate the extent size is legal now we have the agf locked */
2546 if (args.agbno + len >
2547 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2548 error = EFSCORRUPTED;
2552 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2554 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
2556 xfs_perag_put(args.pag);