2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_extent_busy.h"
36 #include "xfs_error.h"
37 #include "xfs_cksum.h"
38 #include "xfs_trace.h"
39 #include "xfs_trans.h"
40 #include "xfs_buf_item.h"
43 struct workqueue_struct *xfs_alloc_wq;
45 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
47 #define XFSA_FIXUP_BNO_OK 1
48 #define XFSA_FIXUP_CNT_OK 2
50 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
51 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
52 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
53 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
54 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
57 * Lookup the record equal to [bno, len] in the btree given by cur.
59 STATIC int /* error */
61 struct xfs_btree_cur *cur, /* btree cursor */
62 xfs_agblock_t bno, /* starting block of extent */
63 xfs_extlen_t len, /* length of extent */
64 int *stat) /* success/failure */
66 cur->bc_rec.a.ar_startblock = bno;
67 cur->bc_rec.a.ar_blockcount = len;
68 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
72 * Lookup the first record greater than or equal to [bno, len]
73 * in the btree given by cur.
77 struct xfs_btree_cur *cur, /* btree cursor */
78 xfs_agblock_t bno, /* starting block of extent */
79 xfs_extlen_t len, /* length of extent */
80 int *stat) /* success/failure */
82 cur->bc_rec.a.ar_startblock = bno;
83 cur->bc_rec.a.ar_blockcount = len;
84 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
88 * Lookup the first record less than or equal to [bno, len]
89 * in the btree given by cur.
93 struct xfs_btree_cur *cur, /* btree cursor */
94 xfs_agblock_t bno, /* starting block of extent */
95 xfs_extlen_t len, /* length of extent */
96 int *stat) /* success/failure */
98 cur->bc_rec.a.ar_startblock = bno;
99 cur->bc_rec.a.ar_blockcount = len;
100 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
104 * Update the record referred to by cur to the value given
106 * This either works (return 0) or gets an EFSCORRUPTED error.
108 STATIC int /* error */
110 struct xfs_btree_cur *cur, /* btree cursor */
111 xfs_agblock_t bno, /* starting block of extent */
112 xfs_extlen_t len) /* length of extent */
114 union xfs_btree_rec rec;
116 rec.alloc.ar_startblock = cpu_to_be32(bno);
117 rec.alloc.ar_blockcount = cpu_to_be32(len);
118 return xfs_btree_update(cur, &rec);
122 * Get the data from the pointed-to record.
126 struct xfs_btree_cur *cur, /* btree cursor */
127 xfs_agblock_t *bno, /* output: starting block of extent */
128 xfs_extlen_t *len, /* output: length of extent */
129 int *stat) /* output: success/failure */
131 union xfs_btree_rec *rec;
134 error = xfs_btree_get_rec(cur, &rec, stat);
135 if (!error && *stat == 1) {
136 *bno = be32_to_cpu(rec->alloc.ar_startblock);
137 *len = be32_to_cpu(rec->alloc.ar_blockcount);
143 * Compute aligned version of the found extent.
144 * Takes alignment and min length into account.
147 xfs_alloc_compute_aligned(
148 xfs_alloc_arg_t *args, /* allocation argument structure */
149 xfs_agblock_t foundbno, /* starting block in found extent */
150 xfs_extlen_t foundlen, /* length in found extent */
151 xfs_agblock_t *resbno, /* result block number */
152 xfs_extlen_t *reslen) /* result length */
157 /* Trim busy sections out of found extent */
158 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
160 if (args->alignment > 1 && len >= args->minlen) {
161 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
162 xfs_extlen_t diff = aligned_bno - bno;
164 *resbno = aligned_bno;
165 *reslen = diff >= len ? 0 : len - diff;
173 * Compute best start block and diff for "near" allocations.
174 * freelen >= wantlen already checked by caller.
176 STATIC xfs_extlen_t /* difference value (absolute) */
177 xfs_alloc_compute_diff(
178 xfs_agblock_t wantbno, /* target starting block */
179 xfs_extlen_t wantlen, /* target length */
180 xfs_extlen_t alignment, /* target alignment */
181 char userdata, /* are we allocating data? */
182 xfs_agblock_t freebno, /* freespace's starting block */
183 xfs_extlen_t freelen, /* freespace's length */
184 xfs_agblock_t *newbnop) /* result: best start block from free */
186 xfs_agblock_t freeend; /* end of freespace extent */
187 xfs_agblock_t newbno1; /* return block number */
188 xfs_agblock_t newbno2; /* other new block number */
189 xfs_extlen_t newlen1=0; /* length with newbno1 */
190 xfs_extlen_t newlen2=0; /* length with newbno2 */
191 xfs_agblock_t wantend; /* end of target extent */
193 ASSERT(freelen >= wantlen);
194 freeend = freebno + freelen;
195 wantend = wantbno + wantlen;
197 * We want to allocate from the start of a free extent if it is past
198 * the desired block or if we are allocating user data and the free
199 * extent is before desired block. The second case is there to allow
200 * for contiguous allocation from the remaining free space if the file
201 * grows in the short term.
203 if (freebno >= wantbno || (userdata && freeend < wantend)) {
204 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
205 newbno1 = NULLAGBLOCK;
206 } else if (freeend >= wantend && alignment > 1) {
207 newbno1 = roundup(wantbno, alignment);
208 newbno2 = newbno1 - alignment;
209 if (newbno1 >= freeend)
210 newbno1 = NULLAGBLOCK;
212 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
213 if (newbno2 < freebno)
214 newbno2 = NULLAGBLOCK;
216 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
217 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
218 if (newlen1 < newlen2 ||
219 (newlen1 == newlen2 &&
220 XFS_ABSDIFF(newbno1, wantbno) >
221 XFS_ABSDIFF(newbno2, wantbno)))
223 } else if (newbno2 != NULLAGBLOCK)
225 } else if (freeend >= wantend) {
227 } else if (alignment > 1) {
228 newbno1 = roundup(freeend - wantlen, alignment);
229 if (newbno1 > freeend - wantlen &&
230 newbno1 - alignment >= freebno)
231 newbno1 -= alignment;
232 else if (newbno1 >= freeend)
233 newbno1 = NULLAGBLOCK;
235 newbno1 = freeend - wantlen;
237 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
241 * Fix up the length, based on mod and prod.
242 * len should be k * prod + mod for some k.
243 * If len is too small it is returned unchanged.
244 * If len hits maxlen it is left alone.
248 xfs_alloc_arg_t *args) /* allocation argument structure */
253 ASSERT(args->mod < args->prod);
255 ASSERT(rlen >= args->minlen);
256 ASSERT(rlen <= args->maxlen);
257 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
258 (args->mod == 0 && rlen < args->prod))
260 k = rlen % args->prod;
264 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
267 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
271 ASSERT(rlen >= args->minlen);
272 ASSERT(rlen <= args->maxlen);
277 * Fix up length if there is too little space left in the a.g.
278 * Return 1 if ok, 0 if too little, should give up.
281 xfs_alloc_fix_minleft(
282 xfs_alloc_arg_t *args) /* allocation argument structure */
284 xfs_agf_t *agf; /* a.g. freelist header */
285 int diff; /* free space difference */
287 if (args->minleft == 0)
289 agf = XFS_BUF_TO_AGF(args->agbp);
290 diff = be32_to_cpu(agf->agf_freeblks)
291 - args->len - args->minleft;
294 args->len += diff; /* shrink the allocated space */
295 if (args->len >= args->minlen)
297 args->agbno = NULLAGBLOCK;
302 * Update the two btrees, logically removing from freespace the extent
303 * starting at rbno, rlen blocks. The extent is contained within the
304 * actual (current) free extent fbno for flen blocks.
305 * Flags are passed in indicating whether the cursors are set to the
308 STATIC int /* error code */
309 xfs_alloc_fixup_trees(
310 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
311 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
312 xfs_agblock_t fbno, /* starting block of free extent */
313 xfs_extlen_t flen, /* length of free extent */
314 xfs_agblock_t rbno, /* starting block of returned extent */
315 xfs_extlen_t rlen, /* length of returned extent */
316 int flags) /* flags, XFSA_FIXUP_... */
318 int error; /* error code */
319 int i; /* operation results */
320 xfs_agblock_t nfbno1; /* first new free startblock */
321 xfs_agblock_t nfbno2; /* second new free startblock */
322 xfs_extlen_t nflen1=0; /* first new free length */
323 xfs_extlen_t nflen2=0; /* second new free length */
326 * Look up the record in the by-size tree if necessary.
328 if (flags & XFSA_FIXUP_CNT_OK) {
330 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
332 XFS_WANT_CORRUPTED_RETURN(
333 i == 1 && nfbno1 == fbno && nflen1 == flen);
336 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
338 XFS_WANT_CORRUPTED_RETURN(i == 1);
341 * Look up the record in the by-block tree if necessary.
343 if (flags & XFSA_FIXUP_BNO_OK) {
345 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
347 XFS_WANT_CORRUPTED_RETURN(
348 i == 1 && nfbno1 == fbno && nflen1 == flen);
351 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
353 XFS_WANT_CORRUPTED_RETURN(i == 1);
357 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
358 struct xfs_btree_block *bnoblock;
359 struct xfs_btree_block *cntblock;
361 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
362 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
364 XFS_WANT_CORRUPTED_RETURN(
365 bnoblock->bb_numrecs == cntblock->bb_numrecs);
370 * Deal with all four cases: the allocated record is contained
371 * within the freespace record, so we can have new freespace
372 * at either (or both) end, or no freespace remaining.
374 if (rbno == fbno && rlen == flen)
375 nfbno1 = nfbno2 = NULLAGBLOCK;
376 else if (rbno == fbno) {
377 nfbno1 = rbno + rlen;
378 nflen1 = flen - rlen;
379 nfbno2 = NULLAGBLOCK;
380 } else if (rbno + rlen == fbno + flen) {
382 nflen1 = flen - rlen;
383 nfbno2 = NULLAGBLOCK;
386 nflen1 = rbno - fbno;
387 nfbno2 = rbno + rlen;
388 nflen2 = (fbno + flen) - nfbno2;
391 * Delete the entry from the by-size btree.
393 if ((error = xfs_btree_delete(cnt_cur, &i)))
395 XFS_WANT_CORRUPTED_RETURN(i == 1);
397 * Add new by-size btree entry(s).
399 if (nfbno1 != NULLAGBLOCK) {
400 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
402 XFS_WANT_CORRUPTED_RETURN(i == 0);
403 if ((error = xfs_btree_insert(cnt_cur, &i)))
405 XFS_WANT_CORRUPTED_RETURN(i == 1);
407 if (nfbno2 != NULLAGBLOCK) {
408 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
410 XFS_WANT_CORRUPTED_RETURN(i == 0);
411 if ((error = xfs_btree_insert(cnt_cur, &i)))
413 XFS_WANT_CORRUPTED_RETURN(i == 1);
416 * Fix up the by-block btree entry(s).
418 if (nfbno1 == NULLAGBLOCK) {
420 * No remaining freespace, just delete the by-block tree entry.
422 if ((error = xfs_btree_delete(bno_cur, &i)))
424 XFS_WANT_CORRUPTED_RETURN(i == 1);
427 * Update the by-block entry to start later|be shorter.
429 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
432 if (nfbno2 != NULLAGBLOCK) {
434 * 2 resulting free entries, need to add one.
436 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
438 XFS_WANT_CORRUPTED_RETURN(i == 0);
439 if ((error = xfs_btree_insert(bno_cur, &i)))
441 XFS_WANT_CORRUPTED_RETURN(i == 1);
450 struct xfs_mount *mp = bp->b_target->bt_mount;
451 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
454 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_uuid))
456 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
459 * during growfs operations, the perag is not fully initialised,
460 * so we can't use it for any useful checking. growfs ensures we can't
461 * use it by using uncached buffers that don't have the perag attached
462 * so we can detect and avoid this problem.
464 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
467 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
468 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
469 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
476 xfs_agfl_read_verify(
479 struct xfs_mount *mp = bp->b_target->bt_mount;
483 * There is no verification of non-crc AGFLs because mkfs does not
484 * initialise the AGFL to zero or NULL. Hence the only valid part of the
485 * AGFL is what the AGF says is active. We can't get to the AGF, so we
486 * can't verify just those entries are valid.
488 if (!xfs_sb_version_hascrc(&mp->m_sb))
491 agfl_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
492 offsetof(struct xfs_agfl, agfl_crc));
494 agfl_ok = agfl_ok && xfs_agfl_verify(bp);
497 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
498 xfs_buf_ioerror(bp, EFSCORRUPTED);
503 xfs_agfl_write_verify(
506 struct xfs_mount *mp = bp->b_target->bt_mount;
507 struct xfs_buf_log_item *bip = bp->b_fspriv;
509 /* no verification of non-crc AGFLs */
510 if (!xfs_sb_version_hascrc(&mp->m_sb))
513 if (!xfs_agfl_verify(bp)) {
514 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
515 xfs_buf_ioerror(bp, EFSCORRUPTED);
520 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
522 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
523 offsetof(struct xfs_agfl, agfl_crc));
526 const struct xfs_buf_ops xfs_agfl_buf_ops = {
527 .verify_read = xfs_agfl_read_verify,
528 .verify_write = xfs_agfl_write_verify,
532 * Read in the allocation group free block array.
534 STATIC int /* error */
536 xfs_mount_t *mp, /* mount point structure */
537 xfs_trans_t *tp, /* transaction pointer */
538 xfs_agnumber_t agno, /* allocation group number */
539 xfs_buf_t **bpp) /* buffer for the ag free block array */
541 xfs_buf_t *bp; /* return value */
544 ASSERT(agno != NULLAGNUMBER);
545 error = xfs_trans_read_buf(
546 mp, tp, mp->m_ddev_targp,
547 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
548 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
551 ASSERT(!xfs_buf_geterror(bp));
552 xfs_buf_set_ref(bp, XFS_AGFL_REF);
558 xfs_alloc_update_counters(
559 struct xfs_trans *tp,
560 struct xfs_perag *pag,
561 struct xfs_buf *agbp,
564 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
566 pag->pagf_freeblks += len;
567 be32_add_cpu(&agf->agf_freeblks, len);
569 xfs_trans_agblocks_delta(tp, len);
570 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
571 be32_to_cpu(agf->agf_length)))
574 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
579 * Allocation group level functions.
583 * Allocate a variable extent in the allocation group agno.
584 * Type and bno are used to determine where in the allocation group the
586 * Extent's length (returned in *len) will be between minlen and maxlen,
587 * and of the form k * prod + mod unless there's nothing that large.
588 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
590 STATIC int /* error */
591 xfs_alloc_ag_vextent(
592 xfs_alloc_arg_t *args) /* argument structure for allocation */
596 ASSERT(args->minlen > 0);
597 ASSERT(args->maxlen > 0);
598 ASSERT(args->minlen <= args->maxlen);
599 ASSERT(args->mod < args->prod);
600 ASSERT(args->alignment > 0);
602 * Branch to correct routine based on the type.
605 switch (args->type) {
606 case XFS_ALLOCTYPE_THIS_AG:
607 error = xfs_alloc_ag_vextent_size(args);
609 case XFS_ALLOCTYPE_NEAR_BNO:
610 error = xfs_alloc_ag_vextent_near(args);
612 case XFS_ALLOCTYPE_THIS_BNO:
613 error = xfs_alloc_ag_vextent_exact(args);
620 if (error || args->agbno == NULLAGBLOCK)
623 ASSERT(args->len >= args->minlen);
624 ASSERT(args->len <= args->maxlen);
625 ASSERT(!args->wasfromfl || !args->isfl);
626 ASSERT(args->agbno % args->alignment == 0);
628 if (!args->wasfromfl) {
629 error = xfs_alloc_update_counters(args->tp, args->pag,
631 -((long)(args->len)));
635 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
636 args->agbno, args->len));
640 xfs_trans_mod_sb(args->tp, args->wasdel ?
641 XFS_TRANS_SB_RES_FDBLOCKS :
642 XFS_TRANS_SB_FDBLOCKS,
643 -((long)(args->len)));
646 XFS_STATS_INC(xs_allocx);
647 XFS_STATS_ADD(xs_allocb, args->len);
652 * Allocate a variable extent at exactly agno/bno.
653 * Extent's length (returned in *len) will be between minlen and maxlen,
654 * and of the form k * prod + mod unless there's nothing that large.
655 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
657 STATIC int /* error */
658 xfs_alloc_ag_vextent_exact(
659 xfs_alloc_arg_t *args) /* allocation argument structure */
661 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
662 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
664 xfs_agblock_t fbno; /* start block of found extent */
665 xfs_extlen_t flen; /* length of found extent */
666 xfs_agblock_t tbno; /* start block of trimmed extent */
667 xfs_extlen_t tlen; /* length of trimmed extent */
668 xfs_agblock_t tend; /* end block of trimmed extent */
669 int i; /* success/failure of operation */
671 ASSERT(args->alignment == 1);
674 * Allocate/initialize a cursor for the by-number freespace btree.
676 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
677 args->agno, XFS_BTNUM_BNO);
680 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
681 * Look for the closest free block <= bno, it must contain bno
682 * if any free block does.
684 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
691 * Grab the freespace record.
693 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
696 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
697 ASSERT(fbno <= args->agbno);
700 * Check for overlapping busy extents.
702 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
705 * Give up if the start of the extent is busy, or the freespace isn't
706 * long enough for the minimum request.
708 if (tbno > args->agbno)
710 if (tlen < args->minlen)
713 if (tend < args->agbno + args->minlen)
717 * End of extent will be smaller of the freespace end and the
718 * maximal requested end.
720 * Fix the length according to mod and prod if given.
722 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
724 xfs_alloc_fix_len(args);
725 if (!xfs_alloc_fix_minleft(args))
728 ASSERT(args->agbno + args->len <= tend);
731 * We are allocating agbno for args->len
732 * Allocate/initialize a cursor for the by-size btree.
734 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
735 args->agno, XFS_BTNUM_CNT);
736 ASSERT(args->agbno + args->len <=
737 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
738 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
739 args->len, XFSA_FIXUP_BNO_OK);
741 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
745 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
746 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
749 trace_xfs_alloc_exact_done(args);
753 /* Didn't find it, return null. */
754 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
755 args->agbno = NULLAGBLOCK;
756 trace_xfs_alloc_exact_notfound(args);
760 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
761 trace_xfs_alloc_exact_error(args);
766 * Search the btree in a given direction via the search cursor and compare
767 * the records found against the good extent we've already found.
770 xfs_alloc_find_best_extent(
771 struct xfs_alloc_arg *args, /* allocation argument structure */
772 struct xfs_btree_cur **gcur, /* good cursor */
773 struct xfs_btree_cur **scur, /* searching cursor */
774 xfs_agblock_t gdiff, /* difference for search comparison */
775 xfs_agblock_t *sbno, /* extent found by search */
776 xfs_extlen_t *slen, /* extent length */
777 xfs_agblock_t *sbnoa, /* aligned extent found by search */
778 xfs_extlen_t *slena, /* aligned extent length */
779 int dir) /* 0 = search right, 1 = search left */
786 /* The good extent is perfect, no need to search. */
791 * Look until we find a better one, run out of space or run off the end.
794 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
797 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
798 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
801 * The good extent is closer than this one.
804 if (*sbnoa >= args->agbno + gdiff)
807 if (*sbnoa <= args->agbno - gdiff)
812 * Same distance, compare length and pick the best.
814 if (*slena >= args->minlen) {
815 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
816 xfs_alloc_fix_len(args);
818 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
820 args->userdata, *sbnoa,
824 * Choose closer size and invalidate other cursor.
832 error = xfs_btree_increment(*scur, 0, &i);
834 error = xfs_btree_decrement(*scur, 0, &i);
840 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
845 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
850 /* caller invalidates cursors */
855 * Allocate a variable extent near bno in the allocation group agno.
856 * Extent's length (returned in len) will be between minlen and maxlen,
857 * and of the form k * prod + mod unless there's nothing that large.
858 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
860 STATIC int /* error */
861 xfs_alloc_ag_vextent_near(
862 xfs_alloc_arg_t *args) /* allocation argument structure */
864 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
865 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
866 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
867 xfs_agblock_t gtbno; /* start bno of right side entry */
868 xfs_agblock_t gtbnoa; /* aligned ... */
869 xfs_extlen_t gtdiff; /* difference to right side entry */
870 xfs_extlen_t gtlen; /* length of right side entry */
871 xfs_extlen_t gtlena; /* aligned ... */
872 xfs_agblock_t gtnew; /* useful start bno of right side */
873 int error; /* error code */
874 int i; /* result code, temporary */
875 int j; /* result code, temporary */
876 xfs_agblock_t ltbno; /* start bno of left side entry */
877 xfs_agblock_t ltbnoa; /* aligned ... */
878 xfs_extlen_t ltdiff; /* difference to left side entry */
879 xfs_extlen_t ltlen; /* length of left side entry */
880 xfs_extlen_t ltlena; /* aligned ... */
881 xfs_agblock_t ltnew; /* useful start bno of left side */
882 xfs_extlen_t rlen; /* length of returned extent */
886 * Randomly don't execute the first algorithm.
888 int dofirst; /* set to do first algorithm */
890 dofirst = prandom_u32() & 1;
901 * Get a cursor for the by-size btree.
903 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
904 args->agno, XFS_BTNUM_CNT);
907 * See if there are any free extents as big as maxlen.
909 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
912 * If none, then pick up the last entry in the tree unless the
916 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
919 if (i == 0 || ltlen == 0) {
920 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
921 trace_xfs_alloc_near_noentry(args);
930 * If the requested extent is large wrt the freespaces available
931 * in this a.g., then the cursor will be pointing to a btree entry
932 * near the right edge of the tree. If it's in the last btree leaf
933 * block, then we just examine all the entries in that block
934 * that are big enough, and pick the best one.
935 * This is written as a while loop so we can break out of it,
936 * but we never loop back to the top.
938 while (xfs_btree_islastblock(cnt_cur, 0)) {
942 xfs_agblock_t bnew=0;
949 * Start from the entry that lookup found, sequence through
950 * all larger free blocks. If we're actually pointing at a
951 * record smaller than maxlen, go to the start of this block,
952 * and skip all those smaller than minlen.
954 if (ltlen || args->alignment > 1) {
955 cnt_cur->bc_ptrs[0] = 1;
957 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
960 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
961 if (ltlen >= args->minlen)
963 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
966 ASSERT(ltlen >= args->minlen);
970 i = cnt_cur->bc_ptrs[0];
971 for (j = 1, blen = 0, bdiff = 0;
972 !error && j && (blen < args->maxlen || bdiff > 0);
973 error = xfs_btree_increment(cnt_cur, 0, &j)) {
975 * For each entry, decide if it's better than
976 * the previous best entry.
978 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
980 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
981 xfs_alloc_compute_aligned(args, ltbno, ltlen,
983 if (ltlena < args->minlen)
985 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
986 xfs_alloc_fix_len(args);
987 ASSERT(args->len >= args->minlen);
988 if (args->len < blen)
990 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
991 args->alignment, args->userdata, ltbnoa,
993 if (ltnew != NULLAGBLOCK &&
994 (args->len > blen || ltdiff < bdiff)) {
998 besti = cnt_cur->bc_ptrs[0];
1002 * It didn't work. We COULD be in a case where
1003 * there's a good record somewhere, so try again.
1008 * Point at the best entry, and retrieve it again.
1010 cnt_cur->bc_ptrs[0] = besti;
1011 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
1013 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1014 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1016 if (!xfs_alloc_fix_minleft(args)) {
1017 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1018 trace_xfs_alloc_near_nominleft(args);
1023 * We are allocating starting at bnew for blen blocks.
1026 ASSERT(bnew >= ltbno);
1027 ASSERT(bnew + blen <= ltbno + ltlen);
1029 * Set up a cursor for the by-bno tree.
1031 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1032 args->agbp, args->agno, XFS_BTNUM_BNO);
1034 * Fix up the btree entries.
1036 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1037 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1039 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1040 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1042 trace_xfs_alloc_near_first(args);
1047 * Search in the by-bno tree to the left and to the right
1048 * simultaneously, until in each case we find a space big enough,
1049 * or run into the edge of the tree. When we run into the edge,
1050 * we deallocate that cursor.
1051 * If both searches succeed, we compare the two spaces and pick
1053 * With alignment, it's possible for both to fail; the upper
1054 * level algorithm that picks allocation groups for allocations
1055 * is not supposed to do this.
1058 * Allocate and initialize the cursor for the leftward search.
1060 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1061 args->agno, XFS_BTNUM_BNO);
1063 * Lookup <= bno to find the leftward search's starting point.
1065 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1069 * Didn't find anything; use this cursor for the rightward
1072 bno_cur_gt = bno_cur_lt;
1076 * Found something. Duplicate the cursor for the rightward search.
1078 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1081 * Increment the cursor, so we will point at the entry just right
1082 * of the leftward entry if any, or to the leftmost entry.
1084 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1088 * It failed, there are no rightward entries.
1090 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1094 * Loop going left with the leftward cursor, right with the
1095 * rightward cursor, until either both directions give up or
1096 * we find an entry at least as big as minlen.
1100 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1102 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1103 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1105 if (ltlena >= args->minlen)
1107 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1110 xfs_btree_del_cursor(bno_cur_lt,
1116 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1118 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1119 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1121 if (gtlena >= args->minlen)
1123 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1126 xfs_btree_del_cursor(bno_cur_gt,
1131 } while (bno_cur_lt || bno_cur_gt);
1134 * Got both cursors still active, need to find better entry.
1136 if (bno_cur_lt && bno_cur_gt) {
1137 if (ltlena >= args->minlen) {
1139 * Left side is good, look for a right side entry.
1141 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1142 xfs_alloc_fix_len(args);
1143 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1144 args->alignment, args->userdata, ltbnoa,
1147 error = xfs_alloc_find_best_extent(args,
1148 &bno_cur_lt, &bno_cur_gt,
1149 ltdiff, >bno, >len,
1151 0 /* search right */);
1153 ASSERT(gtlena >= args->minlen);
1156 * Right side is good, look for a left side entry.
1158 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1159 xfs_alloc_fix_len(args);
1160 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1161 args->alignment, args->userdata, gtbnoa,
1164 error = xfs_alloc_find_best_extent(args,
1165 &bno_cur_gt, &bno_cur_lt,
1166 gtdiff, <bno, <len,
1168 1 /* search left */);
1176 * If we couldn't get anything, give up.
1178 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1179 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1182 trace_xfs_alloc_near_busy(args);
1183 xfs_log_force(args->mp, XFS_LOG_SYNC);
1186 trace_xfs_alloc_size_neither(args);
1187 args->agbno = NULLAGBLOCK;
1192 * At this point we have selected a freespace entry, either to the
1193 * left or to the right. If it's on the right, copy all the
1194 * useful variables to the "left" set so we only have one
1195 * copy of this code.
1198 bno_cur_lt = bno_cur_gt;
1209 * Fix up the length and compute the useful address.
1211 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1212 xfs_alloc_fix_len(args);
1213 if (!xfs_alloc_fix_minleft(args)) {
1214 trace_xfs_alloc_near_nominleft(args);
1215 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1216 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1220 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1221 args->userdata, ltbnoa, ltlena, <new);
1222 ASSERT(ltnew >= ltbno);
1223 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1224 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1225 args->agbno = ltnew;
1227 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1228 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1232 trace_xfs_alloc_near_greater(args);
1234 trace_xfs_alloc_near_lesser(args);
1236 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1237 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1241 trace_xfs_alloc_near_error(args);
1242 if (cnt_cur != NULL)
1243 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1244 if (bno_cur_lt != NULL)
1245 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1246 if (bno_cur_gt != NULL)
1247 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1252 * Allocate a variable extent anywhere in the allocation group agno.
1253 * Extent's length (returned in len) will be between minlen and maxlen,
1254 * and of the form k * prod + mod unless there's nothing that large.
1255 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1257 STATIC int /* error */
1258 xfs_alloc_ag_vextent_size(
1259 xfs_alloc_arg_t *args) /* allocation argument structure */
1261 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1262 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1263 int error; /* error result */
1264 xfs_agblock_t fbno; /* start of found freespace */
1265 xfs_extlen_t flen; /* length of found freespace */
1266 int i; /* temp status variable */
1267 xfs_agblock_t rbno; /* returned block number */
1268 xfs_extlen_t rlen; /* length of returned extent */
1273 * Allocate and initialize a cursor for the by-size btree.
1275 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1276 args->agno, XFS_BTNUM_CNT);
1280 * Look for an entry >= maxlen+alignment-1 blocks.
1282 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1283 args->maxlen + args->alignment - 1, &i)))
1287 * If none or we have busy extents that we cannot allocate from, then
1288 * we have to settle for a smaller extent. In the case that there are
1289 * no large extents, this will return the last entry in the tree unless
1290 * the tree is empty. In the case that there are only busy large
1291 * extents, this will return the largest small extent unless there
1292 * are no smaller extents available.
1294 if (!i || forced > 1) {
1295 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1299 if (i == 0 || flen == 0) {
1300 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1301 trace_xfs_alloc_size_noentry(args);
1305 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1308 * Search for a non-busy extent that is large enough.
1309 * If we are at low space, don't check, or if we fall of
1310 * the end of the btree, turn off the busy check and
1314 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1317 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1319 xfs_alloc_compute_aligned(args, fbno, flen,
1322 if (rlen >= args->maxlen)
1325 error = xfs_btree_increment(cnt_cur, 0, &i);
1330 * Our only valid extents must have been busy.
1331 * Make it unbusy by forcing the log out and
1332 * retrying. If we've been here before, forcing
1333 * the log isn't making the extents available,
1334 * which means they have probably been freed in
1335 * this transaction. In that case, we have to
1336 * give up on them and we'll attempt a minlen
1337 * allocation the next time around.
1339 xfs_btree_del_cursor(cnt_cur,
1341 trace_xfs_alloc_size_busy(args);
1343 xfs_log_force(args->mp, XFS_LOG_SYNC);
1350 * In the first case above, we got the last entry in the
1351 * by-size btree. Now we check to see if the space hits maxlen
1352 * once aligned; if not, we search left for something better.
1353 * This can't happen in the second case above.
1355 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1356 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1357 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1358 if (rlen < args->maxlen) {
1359 xfs_agblock_t bestfbno;
1360 xfs_extlen_t bestflen;
1361 xfs_agblock_t bestrbno;
1362 xfs_extlen_t bestrlen;
1369 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1373 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1376 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1377 if (flen < bestrlen)
1379 xfs_alloc_compute_aligned(args, fbno, flen,
1381 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1382 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1383 (rlen <= flen && rbno + rlen <= fbno + flen),
1385 if (rlen > bestrlen) {
1390 if (rlen == args->maxlen)
1394 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1397 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1403 args->wasfromfl = 0;
1405 * Fix up the length.
1408 if (rlen < args->minlen) {
1410 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1411 trace_xfs_alloc_size_busy(args);
1412 xfs_log_force(args->mp, XFS_LOG_SYNC);
1417 xfs_alloc_fix_len(args);
1419 if (!xfs_alloc_fix_minleft(args))
1422 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1424 * Allocate and initialize a cursor for the by-block tree.
1426 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1427 args->agno, XFS_BTNUM_BNO);
1428 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1429 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1431 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1432 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1433 cnt_cur = bno_cur = NULL;
1436 XFS_WANT_CORRUPTED_GOTO(
1437 args->agbno + args->len <=
1438 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1440 trace_xfs_alloc_size_done(args);
1444 trace_xfs_alloc_size_error(args);
1446 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1448 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1452 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1453 trace_xfs_alloc_size_nominleft(args);
1454 args->agbno = NULLAGBLOCK;
1459 * Deal with the case where only small freespaces remain.
1460 * Either return the contents of the last freespace record,
1461 * or allocate space from the freelist if there is nothing in the tree.
1463 STATIC int /* error */
1464 xfs_alloc_ag_vextent_small(
1465 xfs_alloc_arg_t *args, /* allocation argument structure */
1466 xfs_btree_cur_t *ccur, /* by-size cursor */
1467 xfs_agblock_t *fbnop, /* result block number */
1468 xfs_extlen_t *flenp, /* result length */
1469 int *stat) /* status: 0-freelist, 1-normal/none */
1476 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1479 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1481 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1484 * Nothing in the btree, try the freelist. Make sure
1485 * to respect minleft even when pulling from the
1488 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1489 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1491 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1494 if (fbno != NULLAGBLOCK) {
1495 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1498 if (args->userdata) {
1501 bp = xfs_btree_get_bufs(args->mp, args->tp,
1502 args->agno, fbno, 0);
1503 xfs_trans_binval(args->tp, bp);
1507 XFS_WANT_CORRUPTED_GOTO(
1508 args->agbno + args->len <=
1509 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1511 args->wasfromfl = 1;
1512 trace_xfs_alloc_small_freelist(args);
1517 * Nothing in the freelist.
1523 * Can't allocate from the freelist for some reason.
1530 * Can't do the allocation, give up.
1532 if (flen < args->minlen) {
1533 args->agbno = NULLAGBLOCK;
1534 trace_xfs_alloc_small_notenough(args);
1540 trace_xfs_alloc_small_done(args);
1544 trace_xfs_alloc_small_error(args);
1549 * Free the extent starting at agno/bno for length.
1551 STATIC int /* error */
1553 xfs_trans_t *tp, /* transaction pointer */
1554 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1555 xfs_agnumber_t agno, /* allocation group number */
1556 xfs_agblock_t bno, /* starting block number */
1557 xfs_extlen_t len, /* length of extent */
1558 int isfl) /* set if is freelist blocks - no sb acctg */
1560 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1561 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1562 int error; /* error return value */
1563 xfs_agblock_t gtbno; /* start of right neighbor block */
1564 xfs_extlen_t gtlen; /* length of right neighbor block */
1565 int haveleft; /* have a left neighbor block */
1566 int haveright; /* have a right neighbor block */
1567 int i; /* temp, result code */
1568 xfs_agblock_t ltbno; /* start of left neighbor block */
1569 xfs_extlen_t ltlen; /* length of left neighbor block */
1570 xfs_mount_t *mp; /* mount point struct for filesystem */
1571 xfs_agblock_t nbno; /* new starting block of freespace */
1572 xfs_extlen_t nlen; /* new length of freespace */
1573 xfs_perag_t *pag; /* per allocation group data */
1577 * Allocate and initialize a cursor for the by-block btree.
1579 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1582 * Look for a neighboring block on the left (lower block numbers)
1583 * that is contiguous with this space.
1585 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1589 * There is a block to our left.
1591 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1593 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1595 * It's not contiguous, though.
1597 if (ltbno + ltlen < bno)
1601 * If this failure happens the request to free this
1602 * space was invalid, it's (partly) already free.
1605 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1609 * Look for a neighboring block on the right (higher block numbers)
1610 * that is contiguous with this space.
1612 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1616 * There is a block to our right.
1618 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1620 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1622 * It's not contiguous, though.
1624 if (bno + len < gtbno)
1628 * If this failure happens the request to free this
1629 * space was invalid, it's (partly) already free.
1632 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1636 * Now allocate and initialize a cursor for the by-size tree.
1638 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1640 * Have both left and right contiguous neighbors.
1641 * Merge all three into a single free block.
1643 if (haveleft && haveright) {
1645 * Delete the old by-size entry on the left.
1647 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1649 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1650 if ((error = xfs_btree_delete(cnt_cur, &i)))
1652 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1654 * Delete the old by-size entry on the right.
1656 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1658 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1659 if ((error = xfs_btree_delete(cnt_cur, &i)))
1661 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1663 * Delete the old by-block entry for the right block.
1665 if ((error = xfs_btree_delete(bno_cur, &i)))
1667 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1669 * Move the by-block cursor back to the left neighbor.
1671 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1673 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1676 * Check that this is the right record: delete didn't
1677 * mangle the cursor.
1680 xfs_agblock_t xxbno;
1683 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1686 XFS_WANT_CORRUPTED_GOTO(
1687 i == 1 && xxbno == ltbno && xxlen == ltlen,
1692 * Update remaining by-block entry to the new, joined block.
1695 nlen = len + ltlen + gtlen;
1696 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1700 * Have only a left contiguous neighbor.
1701 * Merge it together with the new freespace.
1703 else if (haveleft) {
1705 * Delete the old by-size entry on the left.
1707 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1709 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1710 if ((error = xfs_btree_delete(cnt_cur, &i)))
1712 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1714 * Back up the by-block cursor to the left neighbor, and
1715 * update its length.
1717 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1719 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1722 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1726 * Have only a right contiguous neighbor.
1727 * Merge it together with the new freespace.
1729 else if (haveright) {
1731 * Delete the old by-size entry on the right.
1733 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1735 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1736 if ((error = xfs_btree_delete(cnt_cur, &i)))
1738 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1740 * Update the starting block and length of the right
1741 * neighbor in the by-block tree.
1745 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1749 * No contiguous neighbors.
1750 * Insert the new freespace into the by-block tree.
1755 if ((error = xfs_btree_insert(bno_cur, &i)))
1757 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1759 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1762 * In all cases we need to insert the new freespace in the by-size tree.
1764 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1766 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1767 if ((error = xfs_btree_insert(cnt_cur, &i)))
1769 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1770 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1774 * Update the freespace totals in the ag and superblock.
1776 pag = xfs_perag_get(mp, agno);
1777 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1783 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1784 XFS_STATS_INC(xs_freex);
1785 XFS_STATS_ADD(xs_freeb, len);
1787 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
1792 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
1794 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1796 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1801 * Visible (exported) allocation/free functions.
1802 * Some of these are used just by xfs_alloc_btree.c and this file.
1806 * Compute and fill in value of m_ag_maxlevels.
1809 xfs_alloc_compute_maxlevels(
1810 xfs_mount_t *mp) /* file system mount structure */
1818 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1819 minleafrecs = mp->m_alloc_mnr[0];
1820 minnoderecs = mp->m_alloc_mnr[1];
1821 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1822 for (level = 1; maxblocks > 1; level++)
1823 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1824 mp->m_ag_maxlevels = level;
1828 * Find the length of the longest extent in an AG.
1831 xfs_alloc_longest_free_extent(
1832 struct xfs_mount *mp,
1833 struct xfs_perag *pag)
1835 xfs_extlen_t need, delta = 0;
1837 need = XFS_MIN_FREELIST_PAG(pag, mp);
1838 if (need > pag->pagf_flcount)
1839 delta = need - pag->pagf_flcount;
1841 if (pag->pagf_longest > delta)
1842 return pag->pagf_longest - delta;
1843 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1847 * Decide whether to use this allocation group for this allocation.
1848 * If so, fix up the btree freelist's size.
1850 STATIC int /* error */
1851 xfs_alloc_fix_freelist(
1852 xfs_alloc_arg_t *args, /* allocation argument structure */
1853 int flags) /* XFS_ALLOC_FLAG_... */
1855 xfs_buf_t *agbp; /* agf buffer pointer */
1856 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1857 xfs_buf_t *agflbp;/* agfl buffer pointer */
1858 xfs_agblock_t bno; /* freelist block */
1859 xfs_extlen_t delta; /* new blocks needed in freelist */
1860 int error; /* error result code */
1861 xfs_extlen_t longest;/* longest extent in allocation group */
1862 xfs_mount_t *mp; /* file system mount point structure */
1863 xfs_extlen_t need; /* total blocks needed in freelist */
1864 xfs_perag_t *pag; /* per-ag information structure */
1865 xfs_alloc_arg_t targs; /* local allocation arguments */
1866 xfs_trans_t *tp; /* transaction pointer */
1872 if (!pag->pagf_init) {
1873 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1876 if (!pag->pagf_init) {
1877 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1878 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1886 * If this is a metadata preferred pag and we are user data
1887 * then try somewhere else if we are not being asked to
1888 * try harder at this point
1890 if (pag->pagf_metadata && args->userdata &&
1891 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1892 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1897 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1899 * If it looks like there isn't a long enough extent, or enough
1900 * total blocks, reject it.
1902 need = XFS_MIN_FREELIST_PAG(pag, mp);
1903 longest = xfs_alloc_longest_free_extent(mp, pag);
1904 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1906 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1907 need - args->total) < (int)args->minleft)) {
1909 xfs_trans_brelse(tp, agbp);
1916 * Get the a.g. freespace buffer.
1917 * Can fail if we're not blocking on locks, and it's held.
1920 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1924 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1925 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1931 * Figure out how many blocks we should have in the freelist.
1933 agf = XFS_BUF_TO_AGF(agbp);
1934 need = XFS_MIN_FREELIST(agf, mp);
1936 * If there isn't enough total or single-extent, reject it.
1938 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1939 delta = need > be32_to_cpu(agf->agf_flcount) ?
1940 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1941 longest = be32_to_cpu(agf->agf_longest);
1942 longest = (longest > delta) ? (longest - delta) :
1943 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1944 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1946 ((int)(be32_to_cpu(agf->agf_freeblks) +
1947 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1948 (int)args->minleft)) {
1949 xfs_trans_brelse(tp, agbp);
1955 * Make the freelist shorter if it's too long.
1957 while (be32_to_cpu(agf->agf_flcount) > need) {
1960 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1963 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1965 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1966 xfs_trans_binval(tp, bp);
1969 * Initialize the args structure.
1971 memset(&targs, 0, sizeof(targs));
1975 targs.agno = args->agno;
1976 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1977 targs.type = XFS_ALLOCTYPE_THIS_AG;
1979 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1982 * Make the freelist longer if it's too short.
1984 while (be32_to_cpu(agf->agf_flcount) < need) {
1986 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1988 * Allocate as many blocks as possible at once.
1990 if ((error = xfs_alloc_ag_vextent(&targs))) {
1991 xfs_trans_brelse(tp, agflbp);
1995 * Stop if we run out. Won't happen if callers are obeying
1996 * the restrictions correctly. Can happen for free calls
1997 * on a completely full ag.
1999 if (targs.agbno == NULLAGBLOCK) {
2000 if (flags & XFS_ALLOC_FLAG_FREEING)
2002 xfs_trans_brelse(tp, agflbp);
2007 * Put each allocated block on the list.
2009 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2010 error = xfs_alloc_put_freelist(tp, agbp,
2016 xfs_trans_brelse(tp, agflbp);
2022 * Get a block from the freelist.
2023 * Returns with the buffer for the block gotten.
2026 xfs_alloc_get_freelist(
2027 xfs_trans_t *tp, /* transaction pointer */
2028 xfs_buf_t *agbp, /* buffer containing the agf structure */
2029 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2030 int btreeblk) /* destination is a AGF btree */
2032 xfs_agf_t *agf; /* a.g. freespace structure */
2033 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2034 xfs_agblock_t bno; /* block number returned */
2038 xfs_mount_t *mp = tp->t_mountp;
2039 xfs_perag_t *pag; /* per allocation group data */
2042 * Freelist is empty, give up.
2044 agf = XFS_BUF_TO_AGF(agbp);
2045 if (!agf->agf_flcount) {
2046 *bnop = NULLAGBLOCK;
2050 * Read the array of free blocks.
2052 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2059 * Get the block number and update the data structures.
2061 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2062 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2063 be32_add_cpu(&agf->agf_flfirst, 1);
2064 xfs_trans_brelse(tp, agflbp);
2065 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2066 agf->agf_flfirst = 0;
2068 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2069 be32_add_cpu(&agf->agf_flcount, -1);
2070 xfs_trans_agflist_delta(tp, -1);
2071 pag->pagf_flcount--;
2074 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2076 be32_add_cpu(&agf->agf_btreeblks, 1);
2077 pag->pagf_btreeblks++;
2078 logflags |= XFS_AGF_BTREEBLKS;
2081 xfs_alloc_log_agf(tp, agbp, logflags);
2088 * Log the given fields from the agf structure.
2092 xfs_trans_t *tp, /* transaction pointer */
2093 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2094 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2096 int first; /* first byte offset */
2097 int last; /* last byte offset */
2098 static const short offsets[] = {
2099 offsetof(xfs_agf_t, agf_magicnum),
2100 offsetof(xfs_agf_t, agf_versionnum),
2101 offsetof(xfs_agf_t, agf_seqno),
2102 offsetof(xfs_agf_t, agf_length),
2103 offsetof(xfs_agf_t, agf_roots[0]),
2104 offsetof(xfs_agf_t, agf_levels[0]),
2105 offsetof(xfs_agf_t, agf_flfirst),
2106 offsetof(xfs_agf_t, agf_fllast),
2107 offsetof(xfs_agf_t, agf_flcount),
2108 offsetof(xfs_agf_t, agf_freeblks),
2109 offsetof(xfs_agf_t, agf_longest),
2110 offsetof(xfs_agf_t, agf_btreeblks),
2111 offsetof(xfs_agf_t, agf_uuid),
2115 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2117 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2119 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2120 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2124 * Interface for inode allocation to force the pag data to be initialized.
2127 xfs_alloc_pagf_init(
2128 xfs_mount_t *mp, /* file system mount structure */
2129 xfs_trans_t *tp, /* transaction pointer */
2130 xfs_agnumber_t agno, /* allocation group number */
2131 int flags) /* XFS_ALLOC_FLAGS_... */
2136 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2139 xfs_trans_brelse(tp, bp);
2144 * Put the block on the freelist for the allocation group.
2147 xfs_alloc_put_freelist(
2148 xfs_trans_t *tp, /* transaction pointer */
2149 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2150 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2151 xfs_agblock_t bno, /* block being freed */
2152 int btreeblk) /* block came from a AGF btree */
2154 xfs_agf_t *agf; /* a.g. freespace structure */
2155 __be32 *blockp;/* pointer to array entry */
2158 xfs_mount_t *mp; /* mount structure */
2159 xfs_perag_t *pag; /* per allocation group data */
2163 agf = XFS_BUF_TO_AGF(agbp);
2166 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2167 be32_to_cpu(agf->agf_seqno), &agflbp)))
2169 be32_add_cpu(&agf->agf_fllast, 1);
2170 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2171 agf->agf_fllast = 0;
2173 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2174 be32_add_cpu(&agf->agf_flcount, 1);
2175 xfs_trans_agflist_delta(tp, 1);
2176 pag->pagf_flcount++;
2178 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2180 be32_add_cpu(&agf->agf_btreeblks, -1);
2181 pag->pagf_btreeblks--;
2182 logflags |= XFS_AGF_BTREEBLKS;
2186 xfs_alloc_log_agf(tp, agbp, logflags);
2188 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2190 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2191 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2192 *blockp = cpu_to_be32(bno);
2193 startoff = (char *)blockp - (char *)agflbp->b_addr;
2195 xfs_alloc_log_agf(tp, agbp, logflags);
2197 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2198 xfs_trans_log_buf(tp, agflbp, startoff,
2199 startoff + sizeof(xfs_agblock_t) - 1);
2205 struct xfs_mount *mp,
2208 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
2210 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2211 !uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_uuid))
2214 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2215 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2216 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2217 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2218 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2219 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2223 * during growfs operations, the perag is not fully initialised,
2224 * so we can't use it for any useful checking. growfs ensures we can't
2225 * use it by using uncached buffers that don't have the perag attached
2226 * so we can detect and avoid this problem.
2228 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2231 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2232 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2240 xfs_agf_read_verify(
2243 struct xfs_mount *mp = bp->b_target->bt_mount;
2246 if (xfs_sb_version_hascrc(&mp->m_sb))
2247 agf_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
2248 offsetof(struct xfs_agf, agf_crc));
2250 agf_ok = agf_ok && xfs_agf_verify(mp, bp);
2252 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2253 XFS_RANDOM_ALLOC_READ_AGF))) {
2254 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2255 xfs_buf_ioerror(bp, EFSCORRUPTED);
2260 xfs_agf_write_verify(
2263 struct xfs_mount *mp = bp->b_target->bt_mount;
2264 struct xfs_buf_log_item *bip = bp->b_fspriv;
2266 if (!xfs_agf_verify(mp, bp)) {
2267 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2268 xfs_buf_ioerror(bp, EFSCORRUPTED);
2272 if (!xfs_sb_version_hascrc(&mp->m_sb))
2276 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2278 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
2279 offsetof(struct xfs_agf, agf_crc));
2282 const struct xfs_buf_ops xfs_agf_buf_ops = {
2283 .verify_read = xfs_agf_read_verify,
2284 .verify_write = xfs_agf_write_verify,
2288 * Read in the allocation group header (free/alloc section).
2292 struct xfs_mount *mp, /* mount point structure */
2293 struct xfs_trans *tp, /* transaction pointer */
2294 xfs_agnumber_t agno, /* allocation group number */
2295 int flags, /* XFS_BUF_ */
2296 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2300 ASSERT(agno != NULLAGNUMBER);
2301 error = xfs_trans_read_buf(
2302 mp, tp, mp->m_ddev_targp,
2303 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2304 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2310 ASSERT(!(*bpp)->b_error);
2311 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2316 * Read in the allocation group header (free/alloc section).
2320 struct xfs_mount *mp, /* mount point structure */
2321 struct xfs_trans *tp, /* transaction pointer */
2322 xfs_agnumber_t agno, /* allocation group number */
2323 int flags, /* XFS_ALLOC_FLAG_... */
2324 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2326 struct xfs_agf *agf; /* ag freelist header */
2327 struct xfs_perag *pag; /* per allocation group data */
2330 ASSERT(agno != NULLAGNUMBER);
2332 error = xfs_read_agf(mp, tp, agno,
2333 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2339 ASSERT(!(*bpp)->b_error);
2341 agf = XFS_BUF_TO_AGF(*bpp);
2342 pag = xfs_perag_get(mp, agno);
2343 if (!pag->pagf_init) {
2344 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2345 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2346 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2347 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2348 pag->pagf_levels[XFS_BTNUM_BNOi] =
2349 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2350 pag->pagf_levels[XFS_BTNUM_CNTi] =
2351 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2352 spin_lock_init(&pag->pagb_lock);
2353 pag->pagb_count = 0;
2354 pag->pagb_tree = RB_ROOT;
2358 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2359 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2360 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2361 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2362 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2363 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2364 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2365 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2366 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2374 * Allocate an extent (variable-size).
2375 * Depending on the allocation type, we either look in a single allocation
2376 * group or loop over the allocation groups to find the result.
2380 xfs_alloc_arg_t *args) /* allocation argument structure */
2382 xfs_agblock_t agsize; /* allocation group size */
2384 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2385 xfs_extlen_t minleft;/* minimum left value, temp copy */
2386 xfs_mount_t *mp; /* mount structure pointer */
2387 xfs_agnumber_t sagno; /* starting allocation group number */
2388 xfs_alloctype_t type; /* input allocation type */
2391 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2394 type = args->otype = args->type;
2395 args->agbno = NULLAGBLOCK;
2397 * Just fix this up, for the case where the last a.g. is shorter
2398 * (or there's only one a.g.) and the caller couldn't easily figure
2399 * that out (xfs_bmap_alloc).
2401 agsize = mp->m_sb.sb_agblocks;
2402 if (args->maxlen > agsize)
2403 args->maxlen = agsize;
2404 if (args->alignment == 0)
2405 args->alignment = 1;
2406 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2407 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2408 ASSERT(args->minlen <= args->maxlen);
2409 ASSERT(args->minlen <= agsize);
2410 ASSERT(args->mod < args->prod);
2411 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2412 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2413 args->minlen > args->maxlen || args->minlen > agsize ||
2414 args->mod >= args->prod) {
2415 args->fsbno = NULLFSBLOCK;
2416 trace_xfs_alloc_vextent_badargs(args);
2419 minleft = args->minleft;
2422 case XFS_ALLOCTYPE_THIS_AG:
2423 case XFS_ALLOCTYPE_NEAR_BNO:
2424 case XFS_ALLOCTYPE_THIS_BNO:
2426 * These three force us into a single a.g.
2428 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2429 args->pag = xfs_perag_get(mp, args->agno);
2431 error = xfs_alloc_fix_freelist(args, 0);
2432 args->minleft = minleft;
2434 trace_xfs_alloc_vextent_nofix(args);
2438 trace_xfs_alloc_vextent_noagbp(args);
2441 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2442 if ((error = xfs_alloc_ag_vextent(args)))
2445 case XFS_ALLOCTYPE_START_BNO:
2447 * Try near allocation first, then anywhere-in-ag after
2448 * the first a.g. fails.
2450 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2451 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2452 args->fsbno = XFS_AGB_TO_FSB(mp,
2453 ((mp->m_agfrotor / rotorstep) %
2454 mp->m_sb.sb_agcount), 0);
2457 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2458 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2460 case XFS_ALLOCTYPE_ANY_AG:
2461 case XFS_ALLOCTYPE_START_AG:
2462 case XFS_ALLOCTYPE_FIRST_AG:
2464 * Rotate through the allocation groups looking for a winner.
2466 if (type == XFS_ALLOCTYPE_ANY_AG) {
2468 * Start with the last place we left off.
2470 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2471 mp->m_sb.sb_agcount;
2472 args->type = XFS_ALLOCTYPE_THIS_AG;
2473 flags = XFS_ALLOC_FLAG_TRYLOCK;
2474 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2476 * Start with allocation group given by bno.
2478 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2479 args->type = XFS_ALLOCTYPE_THIS_AG;
2483 if (type == XFS_ALLOCTYPE_START_AG)
2484 args->type = XFS_ALLOCTYPE_THIS_AG;
2486 * Start with the given allocation group.
2488 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2489 flags = XFS_ALLOC_FLAG_TRYLOCK;
2492 * Loop over allocation groups twice; first time with
2493 * trylock set, second time without.
2496 args->pag = xfs_perag_get(mp, args->agno);
2497 if (no_min) args->minleft = 0;
2498 error = xfs_alloc_fix_freelist(args, flags);
2499 args->minleft = minleft;
2501 trace_xfs_alloc_vextent_nofix(args);
2505 * If we get a buffer back then the allocation will fly.
2508 if ((error = xfs_alloc_ag_vextent(args)))
2513 trace_xfs_alloc_vextent_loopfailed(args);
2516 * Didn't work, figure out the next iteration.
2518 if (args->agno == sagno &&
2519 type == XFS_ALLOCTYPE_START_BNO)
2520 args->type = XFS_ALLOCTYPE_THIS_AG;
2522 * For the first allocation, we can try any AG to get
2523 * space. However, if we already have allocated a
2524 * block, we don't want to try AGs whose number is below
2525 * sagno. Otherwise, we may end up with out-of-order
2526 * locking of AGF, which might cause deadlock.
2528 if (++(args->agno) == mp->m_sb.sb_agcount) {
2529 if (args->firstblock != NULLFSBLOCK)
2535 * Reached the starting a.g., must either be done
2536 * or switch to non-trylock mode.
2538 if (args->agno == sagno) {
2540 args->agbno = NULLAGBLOCK;
2541 trace_xfs_alloc_vextent_allfailed(args);
2548 if (type == XFS_ALLOCTYPE_START_BNO) {
2549 args->agbno = XFS_FSB_TO_AGBNO(mp,
2551 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2555 xfs_perag_put(args->pag);
2557 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2558 if (args->agno == sagno)
2559 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2560 (mp->m_sb.sb_agcount * rotorstep);
2562 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2563 (mp->m_sb.sb_agcount * rotorstep);
2570 if (args->agbno == NULLAGBLOCK)
2571 args->fsbno = NULLFSBLOCK;
2573 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2575 ASSERT(args->len >= args->minlen);
2576 ASSERT(args->len <= args->maxlen);
2577 ASSERT(args->agbno % args->alignment == 0);
2578 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2582 xfs_perag_put(args->pag);
2585 xfs_perag_put(args->pag);
2591 * Just break up the extent address and hand off to xfs_free_ag_extent
2592 * after fixing up the freelist.
2596 xfs_trans_t *tp, /* transaction pointer */
2597 xfs_fsblock_t bno, /* starting block number of extent */
2598 xfs_extlen_t len) /* length of extent */
2600 xfs_alloc_arg_t args;
2604 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2606 args.mp = tp->t_mountp;
2609 * validate that the block number is legal - the enables us to detect
2610 * and handle a silent filesystem corruption rather than crashing.
2612 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2613 if (args.agno >= args.mp->m_sb.sb_agcount)
2614 return EFSCORRUPTED;
2616 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2617 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2618 return EFSCORRUPTED;
2620 args.pag = xfs_perag_get(args.mp, args.agno);
2623 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2627 /* validate the extent size is legal now we have the agf locked */
2628 if (args.agbno + len >
2629 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2630 error = EFSCORRUPTED;
2634 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2636 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
2638 xfs_perag_put(args.pag);