1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_btree.h"
17 #include "xfs_alloc_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_extent_busy.h"
20 #include "xfs_errortag.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_trans.h"
24 #include "xfs_buf_item.h"
27 #include "xfs_ag_resv.h"
30 struct kmem_cache *xfs_extfree_item_cache;
32 struct workqueue_struct *xfs_alloc_wq;
34 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
36 #define XFSA_FIXUP_BNO_OK 1
37 #define XFSA_FIXUP_CNT_OK 2
39 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
40 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
41 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
44 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
45 * the beginning of the block for a proper header with the location information
52 unsigned int size = mp->m_sb.sb_sectsize;
55 size -= sizeof(struct xfs_agfl);
57 return size / sizeof(xfs_agblock_t);
64 if (xfs_has_rmapbt(mp))
65 return XFS_RMAP_BLOCK(mp) + 1;
66 if (xfs_has_finobt(mp))
67 return XFS_FIBT_BLOCK(mp) + 1;
68 return XFS_IBT_BLOCK(mp) + 1;
75 if (xfs_has_reflink(mp))
76 return xfs_refc_block(mp) + 1;
77 if (xfs_has_rmapbt(mp))
78 return XFS_RMAP_BLOCK(mp) + 1;
79 if (xfs_has_finobt(mp))
80 return XFS_FIBT_BLOCK(mp) + 1;
81 return XFS_IBT_BLOCK(mp) + 1;
85 * The number of blocks per AG that we withhold from xfs_mod_fdblocks to
86 * guarantee that we can refill the AGFL prior to allocating space in a nearly
87 * full AG. Although the space described by the free space btrees, the
88 * blocks used by the freesp btrees themselves, and the blocks owned by the
89 * AGFL are counted in the ondisk fdblocks, it's a mistake to let the ondisk
90 * free space in the AG drop so low that the free space btrees cannot refill an
91 * empty AGFL up to the minimum level. Rather than grind through empty AGs
92 * until the fs goes down, we subtract this many AG blocks from the incore
93 * fdblocks to ensure user allocation does not overcommit the space the
94 * filesystem needs for the AGFLs. The rmap btree uses a per-AG reservation to
95 * withhold space from xfs_mod_fdblocks, so we do not account for that here.
97 #define XFS_ALLOCBT_AGFL_RESERVE 4
100 * Compute the number of blocks that we set aside to guarantee the ability to
101 * refill the AGFL and handle a full bmap btree split.
103 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
104 * AGF buffer (PV 947395), we place constraints on the relationship among
105 * actual allocations for data blocks, freelist blocks, and potential file data
106 * bmap btree blocks. However, these restrictions may result in no actual space
107 * allocated for a delayed extent, for example, a data block in a certain AG is
108 * allocated but there is no additional block for the additional bmap btree
109 * block due to a split of the bmap btree of the file. The result of this may
110 * lead to an infinite loop when the file gets flushed to disk and all delayed
111 * extents need to be actually allocated. To get around this, we explicitly set
112 * aside a few blocks which will not be reserved in delayed allocation.
114 * For each AG, we need to reserve enough blocks to replenish a totally empty
115 * AGFL and 4 more to handle a potential split of the file's bmap btree.
119 struct xfs_mount *mp)
121 return mp->m_sb.sb_agcount * (XFS_ALLOCBT_AGFL_RESERVE + 4);
125 * When deciding how much space to allocate out of an AG, we limit the
126 * allocation maximum size to the size the AG. However, we cannot use all the
127 * blocks in the AG - some are permanently used by metadata. These
128 * blocks are generally:
129 * - the AG superblock, AGF, AGI and AGFL
130 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
131 * the AGI free inode and rmap btree root blocks.
132 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
133 * - the rmapbt root block
135 * The AG headers are sector sized, so the amount of space they take up is
136 * dependent on filesystem geometry. The others are all single blocks.
139 xfs_alloc_ag_max_usable(
140 struct xfs_mount *mp)
144 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
145 blocks += XFS_ALLOCBT_AGFL_RESERVE;
146 blocks += 3; /* AGF, AGI btree root blocks */
147 if (xfs_has_finobt(mp))
148 blocks++; /* finobt root block */
149 if (xfs_has_rmapbt(mp))
150 blocks++; /* rmap root block */
151 if (xfs_has_reflink(mp))
152 blocks++; /* refcount root block */
154 return mp->m_sb.sb_agblocks - blocks;
158 * Lookup the record equal to [bno, len] in the btree given by cur.
160 STATIC int /* error */
162 struct xfs_btree_cur *cur, /* btree cursor */
163 xfs_agblock_t bno, /* starting block of extent */
164 xfs_extlen_t len, /* length of extent */
165 int *stat) /* success/failure */
169 cur->bc_rec.a.ar_startblock = bno;
170 cur->bc_rec.a.ar_blockcount = len;
171 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
172 cur->bc_ag.abt.active = (*stat == 1);
177 * Lookup the first record greater than or equal to [bno, len]
178 * in the btree given by cur.
182 struct xfs_btree_cur *cur, /* btree cursor */
183 xfs_agblock_t bno, /* starting block of extent */
184 xfs_extlen_t len, /* length of extent */
185 int *stat) /* success/failure */
189 cur->bc_rec.a.ar_startblock = bno;
190 cur->bc_rec.a.ar_blockcount = len;
191 error = xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
192 cur->bc_ag.abt.active = (*stat == 1);
197 * Lookup the first record less than or equal to [bno, len]
198 * in the btree given by cur.
202 struct xfs_btree_cur *cur, /* btree cursor */
203 xfs_agblock_t bno, /* starting block of extent */
204 xfs_extlen_t len, /* length of extent */
205 int *stat) /* success/failure */
208 cur->bc_rec.a.ar_startblock = bno;
209 cur->bc_rec.a.ar_blockcount = len;
210 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
211 cur->bc_ag.abt.active = (*stat == 1);
216 xfs_alloc_cur_active(
217 struct xfs_btree_cur *cur)
219 return cur && cur->bc_ag.abt.active;
223 * Update the record referred to by cur to the value given
225 * This either works (return 0) or gets an EFSCORRUPTED error.
227 STATIC int /* error */
229 struct xfs_btree_cur *cur, /* btree cursor */
230 xfs_agblock_t bno, /* starting block of extent */
231 xfs_extlen_t len) /* length of extent */
233 union xfs_btree_rec rec;
235 rec.alloc.ar_startblock = cpu_to_be32(bno);
236 rec.alloc.ar_blockcount = cpu_to_be32(len);
237 return xfs_btree_update(cur, &rec);
241 * Get the data from the pointed-to record.
245 struct xfs_btree_cur *cur, /* btree cursor */
246 xfs_agblock_t *bno, /* output: starting block of extent */
247 xfs_extlen_t *len, /* output: length of extent */
248 int *stat) /* output: success/failure */
250 struct xfs_mount *mp = cur->bc_mp;
251 struct xfs_perag *pag = cur->bc_ag.pag;
252 union xfs_btree_rec *rec;
255 error = xfs_btree_get_rec(cur, &rec, stat);
256 if (error || !(*stat))
259 *bno = be32_to_cpu(rec->alloc.ar_startblock);
260 *len = be32_to_cpu(rec->alloc.ar_blockcount);
265 /* check for valid extent range, including overflow */
266 if (!xfs_verify_agbext(pag, *bno, *len))
273 "%s Freespace BTree record corruption in AG %d detected!",
274 cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size",
277 "start block 0x%x block count 0x%x", *bno, *len);
278 return -EFSCORRUPTED;
282 * Compute aligned version of the found extent.
283 * Takes alignment and min length into account.
286 xfs_alloc_compute_aligned(
287 xfs_alloc_arg_t *args, /* allocation argument structure */
288 xfs_agblock_t foundbno, /* starting block in found extent */
289 xfs_extlen_t foundlen, /* length in found extent */
290 xfs_agblock_t *resbno, /* result block number */
291 xfs_extlen_t *reslen, /* result length */
294 xfs_agblock_t bno = foundbno;
295 xfs_extlen_t len = foundlen;
299 /* Trim busy sections out of found extent */
300 busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
303 * If we have a largish extent that happens to start before min_agbno,
304 * see if we can shift it into range...
306 if (bno < args->min_agbno && bno + len > args->min_agbno) {
307 diff = args->min_agbno - bno;
314 if (args->alignment > 1 && len >= args->minlen) {
315 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
317 diff = aligned_bno - bno;
319 *resbno = aligned_bno;
320 *reslen = diff >= len ? 0 : len - diff;
330 * Compute best start block and diff for "near" allocations.
331 * freelen >= wantlen already checked by caller.
333 STATIC xfs_extlen_t /* difference value (absolute) */
334 xfs_alloc_compute_diff(
335 xfs_agblock_t wantbno, /* target starting block */
336 xfs_extlen_t wantlen, /* target length */
337 xfs_extlen_t alignment, /* target alignment */
338 int datatype, /* are we allocating data? */
339 xfs_agblock_t freebno, /* freespace's starting block */
340 xfs_extlen_t freelen, /* freespace's length */
341 xfs_agblock_t *newbnop) /* result: best start block from free */
343 xfs_agblock_t freeend; /* end of freespace extent */
344 xfs_agblock_t newbno1; /* return block number */
345 xfs_agblock_t newbno2; /* other new block number */
346 xfs_extlen_t newlen1=0; /* length with newbno1 */
347 xfs_extlen_t newlen2=0; /* length with newbno2 */
348 xfs_agblock_t wantend; /* end of target extent */
349 bool userdata = datatype & XFS_ALLOC_USERDATA;
351 ASSERT(freelen >= wantlen);
352 freeend = freebno + freelen;
353 wantend = wantbno + wantlen;
355 * We want to allocate from the start of a free extent if it is past
356 * the desired block or if we are allocating user data and the free
357 * extent is before desired block. The second case is there to allow
358 * for contiguous allocation from the remaining free space if the file
359 * grows in the short term.
361 if (freebno >= wantbno || (userdata && freeend < wantend)) {
362 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
363 newbno1 = NULLAGBLOCK;
364 } else if (freeend >= wantend && alignment > 1) {
365 newbno1 = roundup(wantbno, alignment);
366 newbno2 = newbno1 - alignment;
367 if (newbno1 >= freeend)
368 newbno1 = NULLAGBLOCK;
370 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
371 if (newbno2 < freebno)
372 newbno2 = NULLAGBLOCK;
374 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
375 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
376 if (newlen1 < newlen2 ||
377 (newlen1 == newlen2 &&
378 XFS_ABSDIFF(newbno1, wantbno) >
379 XFS_ABSDIFF(newbno2, wantbno)))
381 } else if (newbno2 != NULLAGBLOCK)
383 } else if (freeend >= wantend) {
385 } else if (alignment > 1) {
386 newbno1 = roundup(freeend - wantlen, alignment);
387 if (newbno1 > freeend - wantlen &&
388 newbno1 - alignment >= freebno)
389 newbno1 -= alignment;
390 else if (newbno1 >= freeend)
391 newbno1 = NULLAGBLOCK;
393 newbno1 = freeend - wantlen;
395 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
399 * Fix up the length, based on mod and prod.
400 * len should be k * prod + mod for some k.
401 * If len is too small it is returned unchanged.
402 * If len hits maxlen it is left alone.
406 xfs_alloc_arg_t *args) /* allocation argument structure */
411 ASSERT(args->mod < args->prod);
413 ASSERT(rlen >= args->minlen);
414 ASSERT(rlen <= args->maxlen);
415 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
416 (args->mod == 0 && rlen < args->prod))
418 k = rlen % args->prod;
422 rlen = rlen - (k - args->mod);
424 rlen = rlen - args->prod + (args->mod - k);
425 /* casts to (int) catch length underflows */
426 if ((int)rlen < (int)args->minlen)
428 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
429 ASSERT(rlen % args->prod == args->mod);
430 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
431 rlen + args->minleft);
436 * Update the two btrees, logically removing from freespace the extent
437 * starting at rbno, rlen blocks. The extent is contained within the
438 * actual (current) free extent fbno for flen blocks.
439 * Flags are passed in indicating whether the cursors are set to the
442 STATIC int /* error code */
443 xfs_alloc_fixup_trees(
444 struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */
445 struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */
446 xfs_agblock_t fbno, /* starting block of free extent */
447 xfs_extlen_t flen, /* length of free extent */
448 xfs_agblock_t rbno, /* starting block of returned extent */
449 xfs_extlen_t rlen, /* length of returned extent */
450 int flags) /* flags, XFSA_FIXUP_... */
452 int error; /* error code */
453 int i; /* operation results */
454 xfs_agblock_t nfbno1; /* first new free startblock */
455 xfs_agblock_t nfbno2; /* second new free startblock */
456 xfs_extlen_t nflen1=0; /* first new free length */
457 xfs_extlen_t nflen2=0; /* second new free length */
458 struct xfs_mount *mp;
463 * Look up the record in the by-size tree if necessary.
465 if (flags & XFSA_FIXUP_CNT_OK) {
467 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
469 if (XFS_IS_CORRUPT(mp,
473 return -EFSCORRUPTED;
476 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
478 if (XFS_IS_CORRUPT(mp, i != 1))
479 return -EFSCORRUPTED;
482 * Look up the record in the by-block tree if necessary.
484 if (flags & XFSA_FIXUP_BNO_OK) {
486 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
488 if (XFS_IS_CORRUPT(mp,
492 return -EFSCORRUPTED;
495 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
497 if (XFS_IS_CORRUPT(mp, i != 1))
498 return -EFSCORRUPTED;
502 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
503 struct xfs_btree_block *bnoblock;
504 struct xfs_btree_block *cntblock;
506 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp);
507 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp);
509 if (XFS_IS_CORRUPT(mp,
510 bnoblock->bb_numrecs !=
511 cntblock->bb_numrecs))
512 return -EFSCORRUPTED;
517 * Deal with all four cases: the allocated record is contained
518 * within the freespace record, so we can have new freespace
519 * at either (or both) end, or no freespace remaining.
521 if (rbno == fbno && rlen == flen)
522 nfbno1 = nfbno2 = NULLAGBLOCK;
523 else if (rbno == fbno) {
524 nfbno1 = rbno + rlen;
525 nflen1 = flen - rlen;
526 nfbno2 = NULLAGBLOCK;
527 } else if (rbno + rlen == fbno + flen) {
529 nflen1 = flen - rlen;
530 nfbno2 = NULLAGBLOCK;
533 nflen1 = rbno - fbno;
534 nfbno2 = rbno + rlen;
535 nflen2 = (fbno + flen) - nfbno2;
538 * Delete the entry from the by-size btree.
540 if ((error = xfs_btree_delete(cnt_cur, &i)))
542 if (XFS_IS_CORRUPT(mp, i != 1))
543 return -EFSCORRUPTED;
545 * Add new by-size btree entry(s).
547 if (nfbno1 != NULLAGBLOCK) {
548 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
550 if (XFS_IS_CORRUPT(mp, i != 0))
551 return -EFSCORRUPTED;
552 if ((error = xfs_btree_insert(cnt_cur, &i)))
554 if (XFS_IS_CORRUPT(mp, i != 1))
555 return -EFSCORRUPTED;
557 if (nfbno2 != NULLAGBLOCK) {
558 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
560 if (XFS_IS_CORRUPT(mp, i != 0))
561 return -EFSCORRUPTED;
562 if ((error = xfs_btree_insert(cnt_cur, &i)))
564 if (XFS_IS_CORRUPT(mp, i != 1))
565 return -EFSCORRUPTED;
568 * Fix up the by-block btree entry(s).
570 if (nfbno1 == NULLAGBLOCK) {
572 * No remaining freespace, just delete the by-block tree entry.
574 if ((error = xfs_btree_delete(bno_cur, &i)))
576 if (XFS_IS_CORRUPT(mp, i != 1))
577 return -EFSCORRUPTED;
580 * Update the by-block entry to start later|be shorter.
582 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
585 if (nfbno2 != NULLAGBLOCK) {
587 * 2 resulting free entries, need to add one.
589 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
591 if (XFS_IS_CORRUPT(mp, i != 0))
592 return -EFSCORRUPTED;
593 if ((error = xfs_btree_insert(bno_cur, &i)))
595 if (XFS_IS_CORRUPT(mp, i != 1))
596 return -EFSCORRUPTED;
601 static xfs_failaddr_t
605 struct xfs_mount *mp = bp->b_mount;
606 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
607 __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp);
611 * There is no verification of non-crc AGFLs because mkfs does not
612 * initialise the AGFL to zero or NULL. Hence the only valid part of the
613 * AGFL is what the AGF says is active. We can't get to the AGF, so we
614 * can't verify just those entries are valid.
616 if (!xfs_has_crc(mp))
619 if (!xfs_verify_magic(bp, agfl->agfl_magicnum))
620 return __this_address;
621 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
622 return __this_address;
624 * during growfs operations, the perag is not fully initialised,
625 * so we can't use it for any useful checking. growfs ensures we can't
626 * use it by using uncached buffers that don't have the perag attached
627 * so we can detect and avoid this problem.
629 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
630 return __this_address;
632 for (i = 0; i < xfs_agfl_size(mp); i++) {
633 if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK &&
634 be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks)
635 return __this_address;
638 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
639 return __this_address;
644 xfs_agfl_read_verify(
647 struct xfs_mount *mp = bp->b_mount;
651 * There is no verification of non-crc AGFLs because mkfs does not
652 * initialise the AGFL to zero or NULL. Hence the only valid part of the
653 * AGFL is what the AGF says is active. We can't get to the AGF, so we
654 * can't verify just those entries are valid.
656 if (!xfs_has_crc(mp))
659 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
660 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
662 fa = xfs_agfl_verify(bp);
664 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
669 xfs_agfl_write_verify(
672 struct xfs_mount *mp = bp->b_mount;
673 struct xfs_buf_log_item *bip = bp->b_log_item;
676 /* no verification of non-crc AGFLs */
677 if (!xfs_has_crc(mp))
680 fa = xfs_agfl_verify(bp);
682 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
687 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
689 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
692 const struct xfs_buf_ops xfs_agfl_buf_ops = {
694 .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) },
695 .verify_read = xfs_agfl_read_verify,
696 .verify_write = xfs_agfl_write_verify,
697 .verify_struct = xfs_agfl_verify,
701 * Read in the allocation group free block array.
705 struct xfs_perag *pag,
706 struct xfs_trans *tp,
707 struct xfs_buf **bpp)
709 struct xfs_mount *mp = pag->pag_mount;
713 error = xfs_trans_read_buf(
714 mp, tp, mp->m_ddev_targp,
715 XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGFL_DADDR(mp)),
716 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
719 xfs_buf_set_ref(bp, XFS_AGFL_REF);
725 xfs_alloc_update_counters(
726 struct xfs_trans *tp,
727 struct xfs_buf *agbp,
730 struct xfs_agf *agf = agbp->b_addr;
732 agbp->b_pag->pagf_freeblks += len;
733 be32_add_cpu(&agf->agf_freeblks, len);
735 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
736 be32_to_cpu(agf->agf_length))) {
737 xfs_buf_mark_corrupt(agbp);
738 return -EFSCORRUPTED;
741 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
746 * Block allocation algorithm and data structures.
748 struct xfs_alloc_cur {
749 struct xfs_btree_cur *cnt; /* btree cursors */
750 struct xfs_btree_cur *bnolt;
751 struct xfs_btree_cur *bnogt;
752 xfs_extlen_t cur_len;/* current search length */
753 xfs_agblock_t rec_bno;/* extent startblock */
754 xfs_extlen_t rec_len;/* extent length */
755 xfs_agblock_t bno; /* alloc bno */
756 xfs_extlen_t len; /* alloc len */
757 xfs_extlen_t diff; /* diff from search bno */
758 unsigned int busy_gen;/* busy state */
763 * Set up cursors, etc. in the extent allocation cursor. This function can be
764 * called multiple times to reset an initialized structure without having to
765 * reallocate cursors.
769 struct xfs_alloc_arg *args,
770 struct xfs_alloc_cur *acur)
775 ASSERT(args->alignment == 1 || args->type != XFS_ALLOCTYPE_THIS_BNO);
777 acur->cur_len = args->maxlen;
787 * Perform an initial cntbt lookup to check for availability of maxlen
788 * extents. If this fails, we'll return -ENOSPC to signal the caller to
789 * attempt a small allocation.
792 acur->cnt = xfs_allocbt_init_cursor(args->mp, args->tp,
793 args->agbp, args->pag, XFS_BTNUM_CNT);
794 error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i);
799 * Allocate the bnobt left and right search cursors.
802 acur->bnolt = xfs_allocbt_init_cursor(args->mp, args->tp,
803 args->agbp, args->pag, XFS_BTNUM_BNO);
805 acur->bnogt = xfs_allocbt_init_cursor(args->mp, args->tp,
806 args->agbp, args->pag, XFS_BTNUM_BNO);
807 return i == 1 ? 0 : -ENOSPC;
812 struct xfs_alloc_cur *acur,
815 int cur_error = XFS_BTREE_NOERROR;
818 cur_error = XFS_BTREE_ERROR;
821 xfs_btree_del_cursor(acur->cnt, cur_error);
823 xfs_btree_del_cursor(acur->bnolt, cur_error);
825 xfs_btree_del_cursor(acur->bnogt, cur_error);
826 acur->cnt = acur->bnolt = acur->bnogt = NULL;
830 * Check an extent for allocation and track the best available candidate in the
831 * allocation structure. The cursor is deactivated if it has entered an out of
832 * range state based on allocation arguments. Optionally return the extent
833 * extent geometry and allocation status if requested by the caller.
837 struct xfs_alloc_arg *args,
838 struct xfs_alloc_cur *acur,
839 struct xfs_btree_cur *cur,
843 xfs_agblock_t bno, bnoa, bnew;
844 xfs_extlen_t len, lena, diff = -1;
846 unsigned busy_gen = 0;
847 bool deactivate = false;
848 bool isbnobt = cur->bc_btnum == XFS_BTNUM_BNO;
852 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
855 if (XFS_IS_CORRUPT(args->mp, i != 1))
856 return -EFSCORRUPTED;
859 * Check minlen and deactivate a cntbt cursor if out of acceptable size
860 * range (i.e., walking backwards looking for a minlen extent).
862 if (len < args->minlen) {
863 deactivate = !isbnobt;
867 busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena,
871 acur->busy_gen = busy_gen;
872 /* deactivate a bnobt cursor outside of locality range */
873 if (bnoa < args->min_agbno || bnoa > args->max_agbno) {
874 deactivate = isbnobt;
877 if (lena < args->minlen)
880 args->len = XFS_EXTLEN_MIN(lena, args->maxlen);
881 xfs_alloc_fix_len(args);
882 ASSERT(args->len >= args->minlen);
883 if (args->len < acur->len)
887 * We have an aligned record that satisfies minlen and beats or matches
888 * the candidate extent size. Compare locality for near allocation mode.
890 ASSERT(args->type == XFS_ALLOCTYPE_NEAR_BNO);
891 diff = xfs_alloc_compute_diff(args->agbno, args->len,
892 args->alignment, args->datatype,
894 if (bnew == NULLAGBLOCK)
898 * Deactivate a bnobt cursor with worse locality than the current best.
900 if (diff > acur->diff) {
901 deactivate = isbnobt;
905 ASSERT(args->len > acur->len ||
906 (args->len == acur->len && diff <= acur->diff));
910 acur->len = args->len;
915 * We're done if we found a perfect allocation. This only deactivates
916 * the current cursor, but this is just an optimization to terminate a
917 * cntbt search that otherwise runs to the edge of the tree.
919 if (acur->diff == 0 && acur->len == args->maxlen)
923 cur->bc_ag.abt.active = false;
924 trace_xfs_alloc_cur_check(args->mp, cur->bc_btnum, bno, len, diff,
930 * Complete an allocation of a candidate extent. Remove the extent from both
931 * trees and update the args structure.
934 xfs_alloc_cur_finish(
935 struct xfs_alloc_arg *args,
936 struct xfs_alloc_cur *acur)
938 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
941 ASSERT(acur->cnt && acur->bnolt);
942 ASSERT(acur->bno >= acur->rec_bno);
943 ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len);
944 ASSERT(acur->rec_bno + acur->rec_len <= be32_to_cpu(agf->agf_length));
946 error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno,
947 acur->rec_len, acur->bno, acur->len, 0);
951 args->agbno = acur->bno;
952 args->len = acur->len;
955 trace_xfs_alloc_cur(args);
960 * Locality allocation lookup algorithm. This expects a cntbt cursor and uses
961 * bno optimized lookup to search for extents with ideal size and locality.
964 xfs_alloc_cntbt_iter(
965 struct xfs_alloc_arg *args,
966 struct xfs_alloc_cur *acur)
968 struct xfs_btree_cur *cur = acur->cnt;
970 xfs_extlen_t len, cur_len;
974 if (!xfs_alloc_cur_active(cur))
977 /* locality optimized lookup */
978 cur_len = acur->cur_len;
979 error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i);
984 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
988 /* check the current record and update search length from it */
989 error = xfs_alloc_cur_check(args, acur, cur, &i);
992 ASSERT(len >= acur->cur_len);
996 * We looked up the first record >= [agbno, len] above. The agbno is a
997 * secondary key and so the current record may lie just before or after
998 * agbno. If it is past agbno, check the previous record too so long as
999 * the length matches as it may be closer. Don't check a smaller record
1000 * because that could deactivate our cursor.
1002 if (bno > args->agbno) {
1003 error = xfs_btree_decrement(cur, 0, &i);
1005 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
1006 if (!error && i && len == acur->cur_len)
1007 error = xfs_alloc_cur_check(args, acur, cur,
1015 * Increment the search key until we find at least one allocation
1016 * candidate or if the extent we found was larger. Otherwise, double the
1017 * search key to optimize the search. Efficiency is more important here
1018 * than absolute best locality.
1021 if (!acur->len || acur->cur_len >= cur_len)
1024 acur->cur_len = cur_len;
1030 * Deal with the case where only small freespaces remain. Either return the
1031 * contents of the last freespace record, or allocate space from the freelist if
1032 * there is nothing in the tree.
1034 STATIC int /* error */
1035 xfs_alloc_ag_vextent_small(
1036 struct xfs_alloc_arg *args, /* allocation argument structure */
1037 struct xfs_btree_cur *ccur, /* optional by-size cursor */
1038 xfs_agblock_t *fbnop, /* result block number */
1039 xfs_extlen_t *flenp, /* result length */
1040 int *stat) /* status: 0-freelist, 1-normal/none */
1042 struct xfs_agf *agf = args->agbp->b_addr;
1044 xfs_agblock_t fbno = NULLAGBLOCK;
1045 xfs_extlen_t flen = 0;
1049 * If a cntbt cursor is provided, try to allocate the largest record in
1050 * the tree. Try the AGFL if the cntbt is empty, otherwise fail the
1051 * allocation. Make sure to respect minleft even when pulling from the
1055 error = xfs_btree_decrement(ccur, 0, &i);
1059 error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i);
1062 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1063 error = -EFSCORRUPTED;
1069 if (args->minlen != 1 || args->alignment != 1 ||
1070 args->resv == XFS_AG_RESV_AGFL ||
1071 be32_to_cpu(agf->agf_flcount) <= args->minleft)
1074 error = xfs_alloc_get_freelist(args->pag, args->tp, args->agbp,
1078 if (fbno == NULLAGBLOCK)
1081 xfs_extent_busy_reuse(args->mp, args->pag, fbno, 1,
1082 (args->datatype & XFS_ALLOC_NOBUSY));
1084 if (args->datatype & XFS_ALLOC_USERDATA) {
1087 error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp,
1088 XFS_AGB_TO_DADDR(args->mp, args->agno, fbno),
1089 args->mp->m_bsize, 0, &bp);
1092 xfs_trans_binval(args->tp, bp);
1094 *fbnop = args->agbno = fbno;
1095 *flenp = args->len = 1;
1096 if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) {
1097 error = -EFSCORRUPTED;
1100 args->wasfromfl = 1;
1101 trace_xfs_alloc_small_freelist(args);
1104 * If we're feeding an AGFL block to something that doesn't live in the
1105 * free space, we need to clear out the OWN_AG rmap.
1107 error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1,
1108 &XFS_RMAP_OINFO_AG);
1117 * Can't do the allocation, give up.
1119 if (flen < args->minlen) {
1120 args->agbno = NULLAGBLOCK;
1121 trace_xfs_alloc_small_notenough(args);
1127 trace_xfs_alloc_small_done(args);
1131 trace_xfs_alloc_small_error(args);
1136 * Allocate a variable extent in the allocation group agno.
1137 * Type and bno are used to determine where in the allocation group the
1138 * extent will start.
1139 * Extent's length (returned in *len) will be between minlen and maxlen,
1140 * and of the form k * prod + mod unless there's nothing that large.
1141 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1143 STATIC int /* error */
1144 xfs_alloc_ag_vextent(
1145 xfs_alloc_arg_t *args) /* argument structure for allocation */
1149 ASSERT(args->minlen > 0);
1150 ASSERT(args->maxlen > 0);
1151 ASSERT(args->minlen <= args->maxlen);
1152 ASSERT(args->mod < args->prod);
1153 ASSERT(args->alignment > 0);
1156 * Branch to correct routine based on the type.
1158 args->wasfromfl = 0;
1159 switch (args->type) {
1160 case XFS_ALLOCTYPE_THIS_AG:
1161 error = xfs_alloc_ag_vextent_size(args);
1163 case XFS_ALLOCTYPE_NEAR_BNO:
1164 error = xfs_alloc_ag_vextent_near(args);
1166 case XFS_ALLOCTYPE_THIS_BNO:
1167 error = xfs_alloc_ag_vextent_exact(args);
1174 if (error || args->agbno == NULLAGBLOCK)
1177 ASSERT(args->len >= args->minlen);
1178 ASSERT(args->len <= args->maxlen);
1179 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
1180 ASSERT(args->agbno % args->alignment == 0);
1182 /* if not file data, insert new block into the reverse map btree */
1183 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
1184 error = xfs_rmap_alloc(args->tp, args->agbp, args->pag,
1185 args->agbno, args->len, &args->oinfo);
1190 if (!args->wasfromfl) {
1191 error = xfs_alloc_update_counters(args->tp, args->agbp,
1192 -((long)(args->len)));
1196 ASSERT(!xfs_extent_busy_search(args->mp, args->pag,
1197 args->agbno, args->len));
1200 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
1202 XFS_STATS_INC(args->mp, xs_allocx);
1203 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
1208 * Allocate a variable extent at exactly agno/bno.
1209 * Extent's length (returned in *len) will be between minlen and maxlen,
1210 * and of the form k * prod + mod unless there's nothing that large.
1211 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
1213 STATIC int /* error */
1214 xfs_alloc_ag_vextent_exact(
1215 xfs_alloc_arg_t *args) /* allocation argument structure */
1217 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
1218 struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */
1219 struct xfs_btree_cur *cnt_cur;/* by count btree cursor */
1221 xfs_agblock_t fbno; /* start block of found extent */
1222 xfs_extlen_t flen; /* length of found extent */
1223 xfs_agblock_t tbno; /* start block of busy extent */
1224 xfs_extlen_t tlen; /* length of busy extent */
1225 xfs_agblock_t tend; /* end block of busy extent */
1226 int i; /* success/failure of operation */
1229 ASSERT(args->alignment == 1);
1232 * Allocate/initialize a cursor for the by-number freespace btree.
1234 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1235 args->pag, XFS_BTNUM_BNO);
1238 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
1239 * Look for the closest free block <= bno, it must contain bno
1240 * if any free block does.
1242 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
1249 * Grab the freespace record.
1251 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
1254 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1255 error = -EFSCORRUPTED;
1258 ASSERT(fbno <= args->agbno);
1261 * Check for overlapping busy extents.
1265 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
1268 * Give up if the start of the extent is busy, or the freespace isn't
1269 * long enough for the minimum request.
1271 if (tbno > args->agbno)
1273 if (tlen < args->minlen)
1276 if (tend < args->agbno + args->minlen)
1280 * End of extent will be smaller of the freespace end and the
1281 * maximal requested end.
1283 * Fix the length according to mod and prod if given.
1285 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
1287 xfs_alloc_fix_len(args);
1288 ASSERT(args->agbno + args->len <= tend);
1291 * We are allocating agbno for args->len
1292 * Allocate/initialize a cursor for the by-size btree.
1294 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1295 args->pag, XFS_BTNUM_CNT);
1296 ASSERT(args->agbno + args->len <= be32_to_cpu(agf->agf_length));
1297 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
1298 args->len, XFSA_FIXUP_BNO_OK);
1300 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1304 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1305 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1307 args->wasfromfl = 0;
1308 trace_xfs_alloc_exact_done(args);
1312 /* Didn't find it, return null. */
1313 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1314 args->agbno = NULLAGBLOCK;
1315 trace_xfs_alloc_exact_notfound(args);
1319 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1320 trace_xfs_alloc_exact_error(args);
1325 * Search a given number of btree records in a given direction. Check each
1326 * record against the good extent we've already found.
1329 xfs_alloc_walk_iter(
1330 struct xfs_alloc_arg *args,
1331 struct xfs_alloc_cur *acur,
1332 struct xfs_btree_cur *cur,
1334 bool find_one, /* quit on first candidate */
1335 int count, /* rec count (-1 for infinite) */
1344 * Search so long as the cursor is active or we find a better extent.
1345 * The cursor is deactivated if it extends beyond the range of the
1346 * current allocation candidate.
1348 while (xfs_alloc_cur_active(cur) && count) {
1349 error = xfs_alloc_cur_check(args, acur, cur, &i);
1357 if (!xfs_alloc_cur_active(cur))
1361 error = xfs_btree_increment(cur, 0, &i);
1363 error = xfs_btree_decrement(cur, 0, &i);
1367 cur->bc_ag.abt.active = false;
1377 * Search the by-bno and by-size btrees in parallel in search of an extent with
1378 * ideal locality based on the NEAR mode ->agbno locality hint.
1381 xfs_alloc_ag_vextent_locality(
1382 struct xfs_alloc_arg *args,
1383 struct xfs_alloc_cur *acur,
1386 struct xfs_btree_cur *fbcur = NULL;
1391 ASSERT(acur->len == 0);
1392 ASSERT(args->type == XFS_ALLOCTYPE_NEAR_BNO);
1396 error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i);
1399 error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i);
1402 error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i);
1407 * Search the bnobt and cntbt in parallel. Search the bnobt left and
1408 * right and lookup the closest extent to the locality hint for each
1409 * extent size key in the cntbt. The entire search terminates
1410 * immediately on a bnobt hit because that means we've found best case
1411 * locality. Otherwise the search continues until the cntbt cursor runs
1412 * off the end of the tree. If no allocation candidate is found at this
1413 * point, give up on locality, walk backwards from the end of the cntbt
1414 * and take the first available extent.
1416 * The parallel tree searches balance each other out to provide fairly
1417 * consistent performance for various situations. The bnobt search can
1418 * have pathological behavior in the worst case scenario of larger
1419 * allocation requests and fragmented free space. On the other hand, the
1420 * bnobt is able to satisfy most smaller allocation requests much more
1421 * quickly than the cntbt. The cntbt search can sift through fragmented
1422 * free space and sets of free extents for larger allocation requests
1423 * more quickly than the bnobt. Since the locality hint is just a hint
1424 * and we don't want to scan the entire bnobt for perfect locality, the
1425 * cntbt search essentially bounds the bnobt search such that we can
1426 * find good enough locality at reasonable performance in most cases.
1428 while (xfs_alloc_cur_active(acur->bnolt) ||
1429 xfs_alloc_cur_active(acur->bnogt) ||
1430 xfs_alloc_cur_active(acur->cnt)) {
1432 trace_xfs_alloc_cur_lookup(args);
1435 * Search the bnobt left and right. In the case of a hit, finish
1436 * the search in the opposite direction and we're done.
1438 error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false,
1443 trace_xfs_alloc_cur_left(args);
1444 fbcur = acur->bnogt;
1448 error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true,
1453 trace_xfs_alloc_cur_right(args);
1454 fbcur = acur->bnolt;
1460 * Check the extent with best locality based on the current
1461 * extent size search key and keep track of the best candidate.
1463 error = xfs_alloc_cntbt_iter(args, acur);
1466 if (!xfs_alloc_cur_active(acur->cnt)) {
1467 trace_xfs_alloc_cur_lookup_done(args);
1473 * If we failed to find anything due to busy extents, return empty
1474 * handed so the caller can flush and retry. If no busy extents were
1475 * found, walk backwards from the end of the cntbt as a last resort.
1477 if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) {
1478 error = xfs_btree_decrement(acur->cnt, 0, &i);
1482 acur->cnt->bc_ag.abt.active = true;
1489 * Search in the opposite direction for a better entry in the case of
1490 * a bnobt hit or walk backwards from the end of the cntbt.
1493 error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1,
1505 /* Check the last block of the cnt btree for allocations. */
1507 xfs_alloc_ag_vextent_lastblock(
1508 struct xfs_alloc_arg *args,
1509 struct xfs_alloc_cur *acur,
1518 /* Randomly don't execute the first algorithm. */
1519 if (get_random_u32_below(2))
1524 * Start from the entry that lookup found, sequence through all larger
1525 * free blocks. If we're actually pointing at a record smaller than
1526 * maxlen, go to the start of this block, and skip all those smaller
1529 if (*len || args->alignment > 1) {
1530 acur->cnt->bc_levels[0].ptr = 1;
1532 error = xfs_alloc_get_rec(acur->cnt, bno, len, &i);
1535 if (XFS_IS_CORRUPT(args->mp, i != 1))
1536 return -EFSCORRUPTED;
1537 if (*len >= args->minlen)
1539 error = xfs_btree_increment(acur->cnt, 0, &i);
1543 ASSERT(*len >= args->minlen);
1548 error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i);
1553 * It didn't work. We COULD be in a case where there's a good record
1554 * somewhere, so try again.
1559 trace_xfs_alloc_near_first(args);
1565 * Allocate a variable extent near bno in the allocation group agno.
1566 * Extent's length (returned in len) will be between minlen and maxlen,
1567 * and of the form k * prod + mod unless there's nothing that large.
1568 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1571 xfs_alloc_ag_vextent_near(
1572 struct xfs_alloc_arg *args)
1574 struct xfs_alloc_cur acur = {};
1575 int error; /* error code */
1576 int i; /* result code, temporary */
1580 /* handle uninitialized agbno range so caller doesn't have to */
1581 if (!args->min_agbno && !args->max_agbno)
1582 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1583 ASSERT(args->min_agbno <= args->max_agbno);
1585 /* clamp agbno to the range if it's outside */
1586 if (args->agbno < args->min_agbno)
1587 args->agbno = args->min_agbno;
1588 if (args->agbno > args->max_agbno)
1589 args->agbno = args->max_agbno;
1595 * Set up cursors and see if there are any free extents as big as
1596 * maxlen. If not, pick the last entry in the tree unless the tree is
1599 error = xfs_alloc_cur_setup(args, &acur);
1600 if (error == -ENOSPC) {
1601 error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno,
1605 if (i == 0 || len == 0) {
1606 trace_xfs_alloc_near_noentry(args);
1616 * If the requested extent is large wrt the freespaces available
1617 * in this a.g., then the cursor will be pointing to a btree entry
1618 * near the right edge of the tree. If it's in the last btree leaf
1619 * block, then we just examine all the entries in that block
1620 * that are big enough, and pick the best one.
1622 if (xfs_btree_islastblock(acur.cnt, 0)) {
1623 bool allocated = false;
1625 error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len,
1634 * Second algorithm. Combined cntbt and bnobt search to find ideal
1637 error = xfs_alloc_ag_vextent_locality(args, &acur, &i);
1642 * If we couldn't get anything, give up.
1646 trace_xfs_alloc_near_busy(args);
1647 xfs_extent_busy_flush(args->mp, args->pag,
1651 trace_xfs_alloc_size_neither(args);
1652 args->agbno = NULLAGBLOCK;
1657 /* fix up btrees on a successful allocation */
1658 error = xfs_alloc_cur_finish(args, &acur);
1661 xfs_alloc_cur_close(&acur, error);
1666 * Allocate a variable extent anywhere in the allocation group agno.
1667 * Extent's length (returned in len) will be between minlen and maxlen,
1668 * and of the form k * prod + mod unless there's nothing that large.
1669 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1671 STATIC int /* error */
1672 xfs_alloc_ag_vextent_size(
1673 xfs_alloc_arg_t *args) /* allocation argument structure */
1675 struct xfs_agf *agf = args->agbp->b_addr;
1676 struct xfs_btree_cur *bno_cur; /* cursor for bno btree */
1677 struct xfs_btree_cur *cnt_cur; /* cursor for cnt btree */
1678 int error; /* error result */
1679 xfs_agblock_t fbno; /* start of found freespace */
1680 xfs_extlen_t flen; /* length of found freespace */
1681 int i; /* temp status variable */
1682 xfs_agblock_t rbno; /* returned block number */
1683 xfs_extlen_t rlen; /* length of returned extent */
1689 * Allocate and initialize a cursor for the by-size btree.
1691 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1692 args->pag, XFS_BTNUM_CNT);
1696 * Look for an entry >= maxlen+alignment-1 blocks.
1698 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1699 args->maxlen + args->alignment - 1, &i)))
1703 * If none then we have to settle for a smaller extent. In the case that
1704 * there are no large extents, this will return the last entry in the
1705 * tree unless the tree is empty. In the case that there are only busy
1706 * large extents, this will return the largest small extent unless there
1707 * are no smaller extents available.
1710 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1714 if (i == 0 || flen == 0) {
1715 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1716 trace_xfs_alloc_size_noentry(args);
1720 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1724 * Search for a non-busy extent that is large enough.
1727 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1730 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1731 error = -EFSCORRUPTED;
1735 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1736 &rbno, &rlen, &busy_gen);
1738 if (rlen >= args->maxlen)
1741 error = xfs_btree_increment(cnt_cur, 0, &i);
1746 * Our only valid extents must have been busy.
1747 * Make it unbusy by forcing the log out and
1750 xfs_btree_del_cursor(cnt_cur,
1752 trace_xfs_alloc_size_busy(args);
1753 xfs_extent_busy_flush(args->mp,
1754 args->pag, busy_gen);
1761 * In the first case above, we got the last entry in the
1762 * by-size btree. Now we check to see if the space hits maxlen
1763 * once aligned; if not, we search left for something better.
1764 * This can't happen in the second case above.
1766 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1767 if (XFS_IS_CORRUPT(args->mp,
1770 rbno + rlen > fbno + flen))) {
1771 error = -EFSCORRUPTED;
1774 if (rlen < args->maxlen) {
1775 xfs_agblock_t bestfbno;
1776 xfs_extlen_t bestflen;
1777 xfs_agblock_t bestrbno;
1778 xfs_extlen_t bestrlen;
1785 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1789 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1792 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1793 error = -EFSCORRUPTED;
1796 if (flen < bestrlen)
1798 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1799 &rbno, &rlen, &busy_gen);
1800 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1801 if (XFS_IS_CORRUPT(args->mp,
1804 rbno + rlen > fbno + flen))) {
1805 error = -EFSCORRUPTED;
1808 if (rlen > bestrlen) {
1813 if (rlen == args->maxlen)
1817 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1820 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1821 error = -EFSCORRUPTED;
1829 args->wasfromfl = 0;
1831 * Fix up the length.
1834 if (rlen < args->minlen) {
1836 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1837 trace_xfs_alloc_size_busy(args);
1838 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
1843 xfs_alloc_fix_len(args);
1846 if (XFS_IS_CORRUPT(args->mp, rlen > flen)) {
1847 error = -EFSCORRUPTED;
1851 * Allocate and initialize a cursor for the by-block tree.
1853 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1854 args->pag, XFS_BTNUM_BNO);
1855 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1856 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1858 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1859 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1860 cnt_cur = bno_cur = NULL;
1863 if (XFS_IS_CORRUPT(args->mp,
1864 args->agbno + args->len >
1865 be32_to_cpu(agf->agf_length))) {
1866 error = -EFSCORRUPTED;
1869 trace_xfs_alloc_size_done(args);
1873 trace_xfs_alloc_size_error(args);
1875 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1877 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1881 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1882 trace_xfs_alloc_size_nominleft(args);
1883 args->agbno = NULLAGBLOCK;
1888 * Free the extent starting at agno/bno for length.
1892 struct xfs_trans *tp,
1893 struct xfs_buf *agbp,
1894 xfs_agnumber_t agno,
1897 const struct xfs_owner_info *oinfo,
1898 enum xfs_ag_resv_type type)
1900 struct xfs_mount *mp;
1901 struct xfs_btree_cur *bno_cur;
1902 struct xfs_btree_cur *cnt_cur;
1903 xfs_agblock_t gtbno; /* start of right neighbor */
1904 xfs_extlen_t gtlen; /* length of right neighbor */
1905 xfs_agblock_t ltbno; /* start of left neighbor */
1906 xfs_extlen_t ltlen; /* length of left neighbor */
1907 xfs_agblock_t nbno; /* new starting block of freesp */
1908 xfs_extlen_t nlen; /* new length of freespace */
1909 int haveleft; /* have a left neighbor */
1910 int haveright; /* have a right neighbor */
1913 struct xfs_perag *pag = agbp->b_pag;
1915 bno_cur = cnt_cur = NULL;
1918 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
1919 error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo);
1925 * Allocate and initialize a cursor for the by-block btree.
1927 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_BNO);
1929 * Look for a neighboring block on the left (lower block numbers)
1930 * that is contiguous with this space.
1932 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1936 * There is a block to our left.
1938 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1940 if (XFS_IS_CORRUPT(mp, i != 1)) {
1941 error = -EFSCORRUPTED;
1945 * It's not contiguous, though.
1947 if (ltbno + ltlen < bno)
1951 * If this failure happens the request to free this
1952 * space was invalid, it's (partly) already free.
1955 if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) {
1956 error = -EFSCORRUPTED;
1962 * Look for a neighboring block on the right (higher block numbers)
1963 * that is contiguous with this space.
1965 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1969 * There is a block to our right.
1971 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1973 if (XFS_IS_CORRUPT(mp, i != 1)) {
1974 error = -EFSCORRUPTED;
1978 * It's not contiguous, though.
1980 if (bno + len < gtbno)
1984 * If this failure happens the request to free this
1985 * space was invalid, it's (partly) already free.
1988 if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) {
1989 error = -EFSCORRUPTED;
1995 * Now allocate and initialize a cursor for the by-size tree.
1997 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_CNT);
1999 * Have both left and right contiguous neighbors.
2000 * Merge all three into a single free block.
2002 if (haveleft && haveright) {
2004 * Delete the old by-size entry on the left.
2006 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2008 if (XFS_IS_CORRUPT(mp, i != 1)) {
2009 error = -EFSCORRUPTED;
2012 if ((error = xfs_btree_delete(cnt_cur, &i)))
2014 if (XFS_IS_CORRUPT(mp, i != 1)) {
2015 error = -EFSCORRUPTED;
2019 * Delete the old by-size entry on the right.
2021 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2023 if (XFS_IS_CORRUPT(mp, i != 1)) {
2024 error = -EFSCORRUPTED;
2027 if ((error = xfs_btree_delete(cnt_cur, &i)))
2029 if (XFS_IS_CORRUPT(mp, i != 1)) {
2030 error = -EFSCORRUPTED;
2034 * Delete the old by-block entry for the right block.
2036 if ((error = xfs_btree_delete(bno_cur, &i)))
2038 if (XFS_IS_CORRUPT(mp, i != 1)) {
2039 error = -EFSCORRUPTED;
2043 * Move the by-block cursor back to the left neighbor.
2045 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
2047 if (XFS_IS_CORRUPT(mp, i != 1)) {
2048 error = -EFSCORRUPTED;
2053 * Check that this is the right record: delete didn't
2054 * mangle the cursor.
2057 xfs_agblock_t xxbno;
2060 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
2063 if (XFS_IS_CORRUPT(mp,
2067 error = -EFSCORRUPTED;
2073 * Update remaining by-block entry to the new, joined block.
2076 nlen = len + ltlen + gtlen;
2077 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2081 * Have only a left contiguous neighbor.
2082 * Merge it together with the new freespace.
2084 else if (haveleft) {
2086 * Delete the old by-size entry on the left.
2088 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2090 if (XFS_IS_CORRUPT(mp, i != 1)) {
2091 error = -EFSCORRUPTED;
2094 if ((error = xfs_btree_delete(cnt_cur, &i)))
2096 if (XFS_IS_CORRUPT(mp, i != 1)) {
2097 error = -EFSCORRUPTED;
2101 * Back up the by-block cursor to the left neighbor, and
2102 * update its length.
2104 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
2106 if (XFS_IS_CORRUPT(mp, i != 1)) {
2107 error = -EFSCORRUPTED;
2112 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2116 * Have only a right contiguous neighbor.
2117 * Merge it together with the new freespace.
2119 else if (haveright) {
2121 * Delete the old by-size entry on the right.
2123 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2125 if (XFS_IS_CORRUPT(mp, i != 1)) {
2126 error = -EFSCORRUPTED;
2129 if ((error = xfs_btree_delete(cnt_cur, &i)))
2131 if (XFS_IS_CORRUPT(mp, i != 1)) {
2132 error = -EFSCORRUPTED;
2136 * Update the starting block and length of the right
2137 * neighbor in the by-block tree.
2141 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2145 * No contiguous neighbors.
2146 * Insert the new freespace into the by-block tree.
2151 if ((error = xfs_btree_insert(bno_cur, &i)))
2153 if (XFS_IS_CORRUPT(mp, i != 1)) {
2154 error = -EFSCORRUPTED;
2158 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
2161 * In all cases we need to insert the new freespace in the by-size tree.
2163 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
2165 if (XFS_IS_CORRUPT(mp, i != 0)) {
2166 error = -EFSCORRUPTED;
2169 if ((error = xfs_btree_insert(cnt_cur, &i)))
2171 if (XFS_IS_CORRUPT(mp, i != 1)) {
2172 error = -EFSCORRUPTED;
2175 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2179 * Update the freespace totals in the ag and superblock.
2181 error = xfs_alloc_update_counters(tp, agbp, len);
2182 xfs_ag_resv_free_extent(agbp->b_pag, type, tp, len);
2186 XFS_STATS_INC(mp, xs_freex);
2187 XFS_STATS_ADD(mp, xs_freeb, len);
2189 trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
2194 trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
2196 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
2198 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
2203 * Visible (exported) allocation/free functions.
2204 * Some of these are used just by xfs_alloc_btree.c and this file.
2208 * Compute and fill in value of m_alloc_maxlevels.
2211 xfs_alloc_compute_maxlevels(
2212 xfs_mount_t *mp) /* file system mount structure */
2214 mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
2215 (mp->m_sb.sb_agblocks + 1) / 2);
2216 ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
2220 * Find the length of the longest extent in an AG. The 'need' parameter
2221 * specifies how much space we're going to need for the AGFL and the
2222 * 'reserved' parameter tells us how many blocks in this AG are reserved for
2226 xfs_alloc_longest_free_extent(
2227 struct xfs_perag *pag,
2229 xfs_extlen_t reserved)
2231 xfs_extlen_t delta = 0;
2234 * If the AGFL needs a recharge, we'll have to subtract that from the
2237 if (need > pag->pagf_flcount)
2238 delta = need - pag->pagf_flcount;
2241 * If we cannot maintain others' reservations with space from the
2242 * not-longest freesp extents, we'll have to subtract /that/ from
2243 * the longest extent too.
2245 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2246 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2249 * If the longest extent is long enough to satisfy all the
2250 * reservations and AGFL rules in place, we can return this extent.
2252 if (pag->pagf_longest > delta)
2253 return min_t(xfs_extlen_t, pag->pag_mount->m_ag_max_usable,
2254 pag->pagf_longest - delta);
2256 /* Otherwise, let the caller try for 1 block if there's space. */
2257 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2261 * Compute the minimum length of the AGFL in the given AG. If @pag is NULL,
2262 * return the largest possible minimum length.
2265 xfs_alloc_min_freelist(
2266 struct xfs_mount *mp,
2267 struct xfs_perag *pag)
2269 /* AG btrees have at least 1 level. */
2270 static const uint8_t fake_levels[XFS_BTNUM_AGF] = {1, 1, 1};
2271 const uint8_t *levels = pag ? pag->pagf_levels : fake_levels;
2272 unsigned int min_free;
2274 ASSERT(mp->m_alloc_maxlevels > 0);
2276 /* space needed by-bno freespace btree */
2277 min_free = min_t(unsigned int, levels[XFS_BTNUM_BNOi] + 1,
2278 mp->m_alloc_maxlevels);
2279 /* space needed by-size freespace btree */
2280 min_free += min_t(unsigned int, levels[XFS_BTNUM_CNTi] + 1,
2281 mp->m_alloc_maxlevels);
2282 /* space needed reverse mapping used space btree */
2283 if (xfs_has_rmapbt(mp))
2284 min_free += min_t(unsigned int, levels[XFS_BTNUM_RMAPi] + 1,
2285 mp->m_rmap_maxlevels);
2291 * Check if the operation we are fixing up the freelist for should go ahead or
2292 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2293 * is dependent on whether the size and shape of free space available will
2294 * permit the requested allocation to take place.
2297 xfs_alloc_space_available(
2298 struct xfs_alloc_arg *args,
2299 xfs_extlen_t min_free,
2302 struct xfs_perag *pag = args->pag;
2303 xfs_extlen_t alloc_len, longest;
2304 xfs_extlen_t reservation; /* blocks that are still reserved */
2306 xfs_extlen_t agflcount;
2308 if (flags & XFS_ALLOC_FLAG_FREEING)
2311 reservation = xfs_ag_resv_needed(pag, args->resv);
2313 /* do we have enough contiguous free space for the allocation? */
2314 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
2315 longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
2316 if (longest < alloc_len)
2320 * Do we have enough free space remaining for the allocation? Don't
2321 * account extra agfl blocks because we are about to defer free them,
2322 * making them unavailable until the current transaction commits.
2324 agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free);
2325 available = (int)(pag->pagf_freeblks + agflcount -
2326 reservation - min_free - args->minleft);
2327 if (available < (int)max(args->total, alloc_len))
2331 * Clamp maxlen to the amount of free space available for the actual
2332 * extent allocation.
2334 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2335 args->maxlen = available;
2336 ASSERT(args->maxlen > 0);
2337 ASSERT(args->maxlen >= args->minlen);
2344 xfs_free_agfl_block(
2345 struct xfs_trans *tp,
2346 xfs_agnumber_t agno,
2347 xfs_agblock_t agbno,
2348 struct xfs_buf *agbp,
2349 struct xfs_owner_info *oinfo)
2354 error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
2359 error = xfs_trans_get_buf(tp, tp->t_mountp->m_ddev_targp,
2360 XFS_AGB_TO_DADDR(tp->t_mountp, agno, agbno),
2361 tp->t_mountp->m_bsize, 0, &bp);
2364 xfs_trans_binval(tp, bp);
2370 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2371 * is to detect an agfl header padding mismatch between current and early v5
2372 * kernels. This problem manifests as a 1-slot size difference between the
2373 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2374 * may also catch variants of agfl count corruption unrelated to padding. Either
2375 * way, we'll reset the agfl and warn the user.
2377 * Return true if a reset is required before the agfl can be used, false
2381 xfs_agfl_needs_reset(
2382 struct xfs_mount *mp,
2383 struct xfs_agf *agf)
2385 uint32_t f = be32_to_cpu(agf->agf_flfirst);
2386 uint32_t l = be32_to_cpu(agf->agf_fllast);
2387 uint32_t c = be32_to_cpu(agf->agf_flcount);
2388 int agfl_size = xfs_agfl_size(mp);
2391 /* no agfl header on v4 supers */
2392 if (!xfs_has_crc(mp))
2396 * The agf read verifier catches severe corruption of these fields.
2397 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2398 * the verifier allows it.
2400 if (f >= agfl_size || l >= agfl_size)
2406 * Check consistency between the on-disk count and the active range. An
2407 * agfl padding mismatch manifests as an inconsistent flcount.
2412 active = agfl_size - f + l + 1;
2420 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2421 * agfl content cannot be trusted. Warn the user that a repair is required to
2422 * recover leaked blocks.
2424 * The purpose of this mechanism is to handle filesystems affected by the agfl
2425 * header padding mismatch problem. A reset keeps the filesystem online with a
2426 * relatively minor free space accounting inconsistency rather than suffer the
2427 * inevitable crash from use of an invalid agfl block.
2431 struct xfs_trans *tp,
2432 struct xfs_buf *agbp,
2433 struct xfs_perag *pag)
2435 struct xfs_mount *mp = tp->t_mountp;
2436 struct xfs_agf *agf = agbp->b_addr;
2438 ASSERT(xfs_perag_agfl_needs_reset(pag));
2439 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
2442 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2443 "Please unmount and run xfs_repair.",
2444 pag->pag_agno, pag->pagf_flcount);
2446 agf->agf_flfirst = 0;
2447 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
2448 agf->agf_flcount = 0;
2449 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
2452 pag->pagf_flcount = 0;
2453 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
2457 * Defer an AGFL block free. This is effectively equivalent to
2458 * xfs_free_extent_later() with some special handling particular to AGFL blocks.
2460 * Deferring AGFL frees helps prevent log reservation overruns due to too many
2461 * allocation operations in a transaction. AGFL frees are prone to this problem
2462 * because for one they are always freed one at a time. Further, an immediate
2463 * AGFL block free can cause a btree join and require another block free before
2464 * the real allocation can proceed. Deferring the free disconnects freeing up
2465 * the AGFL slot from freeing the block.
2468 xfs_defer_agfl_block(
2469 struct xfs_trans *tp,
2470 xfs_agnumber_t agno,
2471 xfs_fsblock_t agbno,
2472 struct xfs_owner_info *oinfo)
2474 struct xfs_mount *mp = tp->t_mountp;
2475 struct xfs_extent_free_item *xefi;
2477 ASSERT(xfs_extfree_item_cache != NULL);
2478 ASSERT(oinfo != NULL);
2480 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
2481 GFP_KERNEL | __GFP_NOFAIL);
2482 xefi->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
2483 xefi->xefi_blockcount = 1;
2484 xefi->xefi_owner = oinfo->oi_owner;
2486 trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
2488 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &xefi->xefi_list);
2492 * Add the extent to the list of extents to be free at transaction end.
2493 * The list is maintained sorted (by block number).
2496 __xfs_free_extent_later(
2497 struct xfs_trans *tp,
2500 const struct xfs_owner_info *oinfo,
2503 struct xfs_extent_free_item *xefi;
2505 struct xfs_mount *mp = tp->t_mountp;
2506 xfs_agnumber_t agno;
2507 xfs_agblock_t agbno;
2509 ASSERT(bno != NULLFSBLOCK);
2511 ASSERT(len <= XFS_MAX_BMBT_EXTLEN);
2512 ASSERT(!isnullstartblock(bno));
2513 agno = XFS_FSB_TO_AGNO(mp, bno);
2514 agbno = XFS_FSB_TO_AGBNO(mp, bno);
2515 ASSERT(agno < mp->m_sb.sb_agcount);
2516 ASSERT(agbno < mp->m_sb.sb_agblocks);
2517 ASSERT(len < mp->m_sb.sb_agblocks);
2518 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
2520 ASSERT(xfs_extfree_item_cache != NULL);
2522 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
2523 GFP_KERNEL | __GFP_NOFAIL);
2524 xefi->xefi_startblock = bno;
2525 xefi->xefi_blockcount = (xfs_extlen_t)len;
2527 xefi->xefi_flags |= XFS_EFI_SKIP_DISCARD;
2529 ASSERT(oinfo->oi_offset == 0);
2531 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
2532 xefi->xefi_flags |= XFS_EFI_ATTR_FORK;
2533 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
2534 xefi->xefi_flags |= XFS_EFI_BMBT_BLOCK;
2535 xefi->xefi_owner = oinfo->oi_owner;
2537 xefi->xefi_owner = XFS_RMAP_OWN_NULL;
2539 trace_xfs_bmap_free_defer(tp->t_mountp,
2540 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
2541 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
2542 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &xefi->xefi_list);
2547 * Check if an AGF has a free extent record whose length is equal to
2551 xfs_exact_minlen_extent_available(
2552 struct xfs_alloc_arg *args,
2553 struct xfs_buf *agbp,
2556 struct xfs_btree_cur *cnt_cur;
2561 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, agbp,
2562 args->pag, XFS_BTNUM_CNT);
2563 error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat);
2568 error = -EFSCORRUPTED;
2572 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat);
2576 if (*stat == 1 && flen != args->minlen)
2580 xfs_btree_del_cursor(cnt_cur, error);
2587 * Decide whether to use this allocation group for this allocation.
2588 * If so, fix up the btree freelist's size.
2591 xfs_alloc_fix_freelist(
2592 struct xfs_alloc_arg *args, /* allocation argument structure */
2593 int flags) /* XFS_ALLOC_FLAG_... */
2595 struct xfs_mount *mp = args->mp;
2596 struct xfs_perag *pag = args->pag;
2597 struct xfs_trans *tp = args->tp;
2598 struct xfs_buf *agbp = NULL;
2599 struct xfs_buf *agflbp = NULL;
2600 struct xfs_alloc_arg targs; /* local allocation arguments */
2601 xfs_agblock_t bno; /* freelist block */
2602 xfs_extlen_t need; /* total blocks needed in freelist */
2605 /* deferred ops (AGFL block frees) require permanent transactions */
2606 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
2608 if (!xfs_perag_initialised_agf(pag)) {
2609 error = xfs_alloc_read_agf(pag, tp, flags, &agbp);
2611 /* Couldn't lock the AGF so skip this AG. */
2612 if (error == -EAGAIN)
2619 * If this is a metadata preferred pag and we are user data then try
2620 * somewhere else if we are not being asked to try harder at this
2623 if (xfs_perag_prefers_metadata(pag) &&
2624 (args->datatype & XFS_ALLOC_USERDATA) &&
2625 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2626 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2627 goto out_agbp_relse;
2630 need = xfs_alloc_min_freelist(mp, pag);
2631 if (!xfs_alloc_space_available(args, need, flags |
2632 XFS_ALLOC_FLAG_CHECK))
2633 goto out_agbp_relse;
2636 * Get the a.g. freespace buffer.
2637 * Can fail if we're not blocking on locks, and it's held.
2640 error = xfs_alloc_read_agf(pag, tp, flags, &agbp);
2642 /* Couldn't lock the AGF so skip this AG. */
2643 if (error == -EAGAIN)
2649 /* reset a padding mismatched agfl before final free space check */
2650 if (xfs_perag_agfl_needs_reset(pag))
2651 xfs_agfl_reset(tp, agbp, pag);
2653 /* If there isn't enough total space or single-extent, reject it. */
2654 need = xfs_alloc_min_freelist(mp, pag);
2655 if (!xfs_alloc_space_available(args, need, flags))
2656 goto out_agbp_relse;
2659 if (args->alloc_minlen_only) {
2662 error = xfs_exact_minlen_extent_available(args, agbp, &stat);
2664 goto out_agbp_relse;
2668 * Make the freelist shorter if it's too long.
2670 * Note that from this point onwards, we will always release the agf and
2671 * agfl buffers on error. This handles the case where we error out and
2672 * the buffers are clean or may not have been joined to the transaction
2673 * and hence need to be released manually. If they have been joined to
2674 * the transaction, then xfs_trans_brelse() will handle them
2675 * appropriately based on the recursion count and dirty state of the
2678 * XXX (dgc): When we have lots of free space, does this buy us
2679 * anything other than extra overhead when we need to put more blocks
2680 * back on the free list? Maybe we should only do this when space is
2681 * getting low or the AGFL is more than half full?
2683 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2684 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2685 * updating the rmapbt. Both flags are used in xfs_repair while we're
2686 * rebuilding the rmapbt, and neither are used by the kernel. They're
2687 * both required to ensure that rmaps are correctly recorded for the
2688 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2689 * repair/rmap.c in xfsprogs for details.
2691 memset(&targs, 0, sizeof(targs));
2692 /* struct copy below */
2693 if (flags & XFS_ALLOC_FLAG_NORMAP)
2694 targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
2696 targs.oinfo = XFS_RMAP_OINFO_AG;
2697 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
2698 error = xfs_alloc_get_freelist(pag, tp, agbp, &bno, 0);
2700 goto out_agbp_relse;
2702 /* defer agfl frees */
2703 xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
2709 targs.agno = args->agno;
2710 targs.alignment = targs.minlen = targs.prod = 1;
2711 targs.type = XFS_ALLOCTYPE_THIS_AG;
2713 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2715 goto out_agbp_relse;
2717 /* Make the freelist longer if it's too short. */
2718 while (pag->pagf_flcount < need) {
2720 targs.maxlen = need - pag->pagf_flcount;
2721 targs.resv = XFS_AG_RESV_AGFL;
2723 /* Allocate as many blocks as possible at once. */
2724 error = xfs_alloc_ag_vextent(&targs);
2726 goto out_agflbp_relse;
2729 * Stop if we run out. Won't happen if callers are obeying
2730 * the restrictions correctly. Can happen for free calls
2731 * on a completely full ag.
2733 if (targs.agbno == NULLAGBLOCK) {
2734 if (flags & XFS_ALLOC_FLAG_FREEING)
2736 goto out_agflbp_relse;
2739 * Put each allocated block on the list.
2741 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2742 error = xfs_alloc_put_freelist(pag, tp, agbp,
2745 goto out_agflbp_relse;
2748 xfs_trans_brelse(tp, agflbp);
2753 xfs_trans_brelse(tp, agflbp);
2756 xfs_trans_brelse(tp, agbp);
2763 * Get a block from the freelist.
2764 * Returns with the buffer for the block gotten.
2767 xfs_alloc_get_freelist(
2768 struct xfs_perag *pag,
2769 struct xfs_trans *tp,
2770 struct xfs_buf *agbp,
2771 xfs_agblock_t *bnop,
2774 struct xfs_agf *agf = agbp->b_addr;
2775 struct xfs_buf *agflbp;
2780 struct xfs_mount *mp = tp->t_mountp;
2783 * Freelist is empty, give up.
2785 if (!agf->agf_flcount) {
2786 *bnop = NULLAGBLOCK;
2790 * Read the array of free blocks.
2792 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2798 * Get the block number and update the data structures.
2800 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
2801 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2802 be32_add_cpu(&agf->agf_flfirst, 1);
2803 xfs_trans_brelse(tp, agflbp);
2804 if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
2805 agf->agf_flfirst = 0;
2807 ASSERT(!xfs_perag_agfl_needs_reset(pag));
2808 be32_add_cpu(&agf->agf_flcount, -1);
2809 pag->pagf_flcount--;
2811 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2813 be32_add_cpu(&agf->agf_btreeblks, 1);
2814 pag->pagf_btreeblks++;
2815 logflags |= XFS_AGF_BTREEBLKS;
2818 xfs_alloc_log_agf(tp, agbp, logflags);
2825 * Log the given fields from the agf structure.
2829 struct xfs_trans *tp,
2833 int first; /* first byte offset */
2834 int last; /* last byte offset */
2835 static const short offsets[] = {
2836 offsetof(xfs_agf_t, agf_magicnum),
2837 offsetof(xfs_agf_t, agf_versionnum),
2838 offsetof(xfs_agf_t, agf_seqno),
2839 offsetof(xfs_agf_t, agf_length),
2840 offsetof(xfs_agf_t, agf_roots[0]),
2841 offsetof(xfs_agf_t, agf_levels[0]),
2842 offsetof(xfs_agf_t, agf_flfirst),
2843 offsetof(xfs_agf_t, agf_fllast),
2844 offsetof(xfs_agf_t, agf_flcount),
2845 offsetof(xfs_agf_t, agf_freeblks),
2846 offsetof(xfs_agf_t, agf_longest),
2847 offsetof(xfs_agf_t, agf_btreeblks),
2848 offsetof(xfs_agf_t, agf_uuid),
2849 offsetof(xfs_agf_t, agf_rmap_blocks),
2850 offsetof(xfs_agf_t, agf_refcount_blocks),
2851 offsetof(xfs_agf_t, agf_refcount_root),
2852 offsetof(xfs_agf_t, agf_refcount_level),
2853 /* needed so that we don't log the whole rest of the structure: */
2854 offsetof(xfs_agf_t, agf_spare64),
2858 trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_);
2860 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2862 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2863 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2867 * Put the block on the freelist for the allocation group.
2870 xfs_alloc_put_freelist(
2871 struct xfs_perag *pag,
2872 struct xfs_trans *tp,
2873 struct xfs_buf *agbp,
2874 struct xfs_buf *agflbp,
2878 struct xfs_mount *mp = tp->t_mountp;
2879 struct xfs_agf *agf = agbp->b_addr;
2887 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2892 be32_add_cpu(&agf->agf_fllast, 1);
2893 if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
2894 agf->agf_fllast = 0;
2896 ASSERT(!xfs_perag_agfl_needs_reset(pag));
2897 be32_add_cpu(&agf->agf_flcount, 1);
2898 pag->pagf_flcount++;
2900 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2902 be32_add_cpu(&agf->agf_btreeblks, -1);
2903 pag->pagf_btreeblks--;
2904 logflags |= XFS_AGF_BTREEBLKS;
2907 xfs_alloc_log_agf(tp, agbp, logflags);
2909 ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
2911 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
2912 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2913 *blockp = cpu_to_be32(bno);
2914 startoff = (char *)blockp - (char *)agflbp->b_addr;
2916 xfs_alloc_log_agf(tp, agbp, logflags);
2918 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2919 xfs_trans_log_buf(tp, agflbp, startoff,
2920 startoff + sizeof(xfs_agblock_t) - 1);
2924 static xfs_failaddr_t
2928 struct xfs_mount *mp = bp->b_mount;
2929 struct xfs_agf *agf = bp->b_addr;
2931 if (xfs_has_crc(mp)) {
2932 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
2933 return __this_address;
2934 if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn)))
2935 return __this_address;
2938 if (!xfs_verify_magic(bp, agf->agf_magicnum))
2939 return __this_address;
2941 if (!(XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2942 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2943 be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
2944 be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
2945 be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
2946 return __this_address;
2948 if (be32_to_cpu(agf->agf_length) > mp->m_sb.sb_dblocks)
2949 return __this_address;
2951 if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
2952 be32_to_cpu(agf->agf_freeblks) > be32_to_cpu(agf->agf_length))
2953 return __this_address;
2955 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2956 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2957 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) >
2958 mp->m_alloc_maxlevels ||
2959 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) >
2960 mp->m_alloc_maxlevels)
2961 return __this_address;
2963 if (xfs_has_rmapbt(mp) &&
2964 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2965 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) >
2966 mp->m_rmap_maxlevels))
2967 return __this_address;
2969 if (xfs_has_rmapbt(mp) &&
2970 be32_to_cpu(agf->agf_rmap_blocks) > be32_to_cpu(agf->agf_length))
2971 return __this_address;
2974 * during growfs operations, the perag is not fully initialised,
2975 * so we can't use it for any useful checking. growfs ensures we can't
2976 * use it by using uncached buffers that don't have the perag attached
2977 * so we can detect and avoid this problem.
2979 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2980 return __this_address;
2982 if (xfs_has_lazysbcount(mp) &&
2983 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2984 return __this_address;
2986 if (xfs_has_reflink(mp) &&
2987 be32_to_cpu(agf->agf_refcount_blocks) >
2988 be32_to_cpu(agf->agf_length))
2989 return __this_address;
2991 if (xfs_has_reflink(mp) &&
2992 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2993 be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels))
2994 return __this_address;
3001 xfs_agf_read_verify(
3004 struct xfs_mount *mp = bp->b_mount;
3007 if (xfs_has_crc(mp) &&
3008 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
3009 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
3011 fa = xfs_agf_verify(bp);
3012 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
3013 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
3018 xfs_agf_write_verify(
3021 struct xfs_mount *mp = bp->b_mount;
3022 struct xfs_buf_log_item *bip = bp->b_log_item;
3023 struct xfs_agf *agf = bp->b_addr;
3026 fa = xfs_agf_verify(bp);
3028 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
3032 if (!xfs_has_crc(mp))
3036 agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
3038 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
3041 const struct xfs_buf_ops xfs_agf_buf_ops = {
3043 .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) },
3044 .verify_read = xfs_agf_read_verify,
3045 .verify_write = xfs_agf_write_verify,
3046 .verify_struct = xfs_agf_verify,
3050 * Read in the allocation group header (free/alloc section).
3054 struct xfs_perag *pag,
3055 struct xfs_trans *tp,
3057 struct xfs_buf **agfbpp)
3059 struct xfs_mount *mp = pag->pag_mount;
3062 trace_xfs_read_agf(pag->pag_mount, pag->pag_agno);
3064 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
3065 XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGF_DADDR(mp)),
3066 XFS_FSS_TO_BB(mp, 1), flags, agfbpp, &xfs_agf_buf_ops);
3070 xfs_buf_set_ref(*agfbpp, XFS_AGF_REF);
3075 * Read in the allocation group header (free/alloc section) and initialise the
3076 * perag structure if necessary. If the caller provides @agfbpp, then return the
3077 * locked buffer to the caller, otherwise free it.
3081 struct xfs_perag *pag,
3082 struct xfs_trans *tp,
3084 struct xfs_buf **agfbpp)
3086 struct xfs_buf *agfbp;
3087 struct xfs_agf *agf;
3091 trace_xfs_alloc_read_agf(pag->pag_mount, pag->pag_agno);
3093 /* We don't support trylock when freeing. */
3094 ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) !=
3095 (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK));
3096 error = xfs_read_agf(pag, tp,
3097 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
3102 agf = agfbp->b_addr;
3103 if (!xfs_perag_initialised_agf(pag)) {
3104 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
3105 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
3106 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
3107 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
3108 pag->pagf_levels[XFS_BTNUM_BNOi] =
3109 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
3110 pag->pagf_levels[XFS_BTNUM_CNTi] =
3111 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
3112 pag->pagf_levels[XFS_BTNUM_RMAPi] =
3113 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
3114 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
3115 if (xfs_agfl_needs_reset(pag->pag_mount, agf))
3116 set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
3119 * Update the in-core allocbt counter. Filter out the rmapbt
3120 * subset of the btreeblks counter because the rmapbt is managed
3121 * by perag reservation. Subtract one for the rmapbt root block
3122 * because the rmap counter includes it while the btreeblks
3123 * counter only tracks non-root blocks.
3125 allocbt_blks = pag->pagf_btreeblks;
3126 if (xfs_has_rmapbt(pag->pag_mount))
3127 allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1;
3128 if (allocbt_blks > 0)
3129 atomic64_add(allocbt_blks,
3130 &pag->pag_mount->m_allocbt_blks);
3132 set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
3135 else if (!xfs_is_shutdown(pag->pag_mount)) {
3136 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
3137 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
3138 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
3139 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
3140 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
3141 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
3142 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
3143 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
3149 xfs_trans_brelse(tp, agfbp);
3154 * Allocate an extent (variable-size).
3155 * Depending on the allocation type, we either look in a single allocation
3156 * group or loop over the allocation groups to find the result.
3160 struct xfs_alloc_arg *args) /* allocation argument structure */
3162 xfs_agblock_t agsize; /* allocation group size */
3164 int flags; /* XFS_ALLOC_FLAG_... locking flags */
3165 struct xfs_mount *mp; /* mount structure pointer */
3166 xfs_agnumber_t sagno; /* starting allocation group number */
3167 xfs_alloctype_t type; /* input allocation type */
3169 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
3170 xfs_agnumber_t minimum_agno = 0;
3173 type = args->otype = args->type;
3174 args->agbno = NULLAGBLOCK;
3175 if (args->tp->t_highest_agno != NULLAGNUMBER)
3176 minimum_agno = args->tp->t_highest_agno;
3178 * Just fix this up, for the case where the last a.g. is shorter
3179 * (or there's only one a.g.) and the caller couldn't easily figure
3180 * that out (xfs_bmap_alloc).
3182 agsize = mp->m_sb.sb_agblocks;
3183 if (args->maxlen > agsize)
3184 args->maxlen = agsize;
3185 if (args->alignment == 0)
3186 args->alignment = 1;
3187 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
3188 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
3189 ASSERT(args->minlen <= args->maxlen);
3190 ASSERT(args->minlen <= agsize);
3191 ASSERT(args->mod < args->prod);
3192 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
3193 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
3194 args->minlen > args->maxlen || args->minlen > agsize ||
3195 args->mod >= args->prod) {
3196 args->fsbno = NULLFSBLOCK;
3197 trace_xfs_alloc_vextent_badargs(args);
3202 case XFS_ALLOCTYPE_THIS_AG:
3203 case XFS_ALLOCTYPE_NEAR_BNO:
3204 case XFS_ALLOCTYPE_THIS_BNO:
3206 * These three force us into a single a.g.
3208 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
3209 args->pag = xfs_perag_get(mp, args->agno);
3211 if (minimum_agno > args->agno) {
3212 trace_xfs_alloc_vextent_skip_deadlock(args);
3217 error = xfs_alloc_fix_freelist(args, 0);
3219 trace_xfs_alloc_vextent_nofix(args);
3223 trace_xfs_alloc_vextent_noagbp(args);
3226 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
3227 if ((error = xfs_alloc_ag_vextent(args)))
3230 case XFS_ALLOCTYPE_START_BNO:
3232 * Try near allocation first, then anywhere-in-ag after
3233 * the first a.g. fails.
3235 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
3236 xfs_is_inode32(mp)) {
3237 args->fsbno = XFS_AGB_TO_FSB(mp,
3238 ((mp->m_agfrotor / rotorstep) %
3239 mp->m_sb.sb_agcount), 0);
3242 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
3243 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3245 case XFS_ALLOCTYPE_FIRST_AG:
3247 * Rotate through the allocation groups looking for a winner.
3248 * If we are blocking, we must obey minimum_agno contraints for
3249 * avoiding ABBA deadlocks on AGF locking.
3251 if (type == XFS_ALLOCTYPE_FIRST_AG) {
3253 * Start with allocation group given by bno.
3255 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
3256 args->type = XFS_ALLOCTYPE_THIS_AG;
3257 sagno = minimum_agno;
3261 * Start with the given allocation group.
3263 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
3264 flags = XFS_ALLOC_FLAG_TRYLOCK;
3268 * Loop over allocation groups twice; first time with
3269 * trylock set, second time without.
3272 args->pag = xfs_perag_get(mp, args->agno);
3273 error = xfs_alloc_fix_freelist(args, flags);
3275 trace_xfs_alloc_vextent_nofix(args);
3279 * If we get a buffer back then the allocation will fly.
3282 if ((error = xfs_alloc_ag_vextent(args)))
3287 trace_xfs_alloc_vextent_loopfailed(args);
3290 * Didn't work, figure out the next iteration.
3292 if (args->agno == sagno &&
3293 type == XFS_ALLOCTYPE_START_BNO)
3294 args->type = XFS_ALLOCTYPE_THIS_AG;
3297 * If we are try-locking, we can't deadlock on AGF
3298 * locks, so we can wrap all the way back to the first
3299 * AG. Otherwise, wrap back to the start AG so we can't
3300 * deadlock, and let the end of scan handler decide what
3303 if (++(args->agno) == mp->m_sb.sb_agcount) {
3304 if (flags & XFS_ALLOC_FLAG_TRYLOCK)
3311 * Reached the starting a.g., must either be done
3312 * or switch to non-trylock mode.
3314 if (args->agno == sagno) {
3316 args->agbno = NULLAGBLOCK;
3317 trace_xfs_alloc_vextent_allfailed(args);
3322 * Blocking pass next, so we must obey minimum
3323 * agno constraints to avoid ABBA AGF deadlocks.
3326 if (minimum_agno > sagno)
3327 sagno = minimum_agno;
3329 if (type == XFS_ALLOCTYPE_START_BNO) {
3330 args->agbno = XFS_FSB_TO_AGBNO(mp,
3332 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3335 xfs_perag_put(args->pag);
3338 if (args->agno == sagno)
3339 mp->m_agfrotor = (mp->m_agfrotor + 1) %
3340 (mp->m_sb.sb_agcount * rotorstep);
3342 mp->m_agfrotor = (args->agno * rotorstep + 1) %
3343 (mp->m_sb.sb_agcount * rotorstep);
3350 if (args->agbno == NULLAGBLOCK) {
3351 args->fsbno = NULLFSBLOCK;
3353 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
3355 ASSERT(args->len >= args->minlen);
3356 ASSERT(args->len <= args->maxlen);
3357 ASSERT(args->agbno % args->alignment == 0);
3358 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
3365 * We end up here with a locked AGF. If we failed, the caller is likely
3366 * going to try to allocate again with different parameters, and that
3367 * can widen the AGs that are searched for free space. If we have to do
3368 * BMBT block allocation, we have to do a new allocation.
3370 * Hence leaving this function with the AGF locked opens up potential
3371 * ABBA AGF deadlocks because a future allocation attempt in this
3372 * transaction may attempt to lock a lower number AGF.
3374 * We can't release the AGF until the transaction is commited, so at
3375 * this point we must update the "firstblock" tracker to point at this
3376 * AG if the tracker is empty or points to a lower AG. This allows the
3377 * next allocation attempt to be modified appropriately to avoid
3381 (args->tp->t_highest_agno == NULLAGNUMBER ||
3382 args->pag->pag_agno > minimum_agno))
3383 args->tp->t_highest_agno = args->pag->pag_agno;
3384 xfs_perag_put(args->pag);
3387 xfs_perag_put(args->pag);
3391 /* Ensure that the freelist is at full capacity. */
3393 xfs_free_extent_fix_freelist(
3394 struct xfs_trans *tp,
3395 struct xfs_perag *pag,
3396 struct xfs_buf **agbp)
3398 struct xfs_alloc_arg args;
3401 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3403 args.mp = tp->t_mountp;
3404 args.agno = pag->pag_agno;
3408 * validate that the block number is legal - the enables us to detect
3409 * and handle a silent filesystem corruption rather than crashing.
3411 if (args.agno >= args.mp->m_sb.sb_agcount)
3412 return -EFSCORRUPTED;
3414 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3424 * Just break up the extent address and hand off to xfs_free_ag_extent
3425 * after fixing up the freelist.
3429 struct xfs_trans *tp,
3432 const struct xfs_owner_info *oinfo,
3433 enum xfs_ag_resv_type type,
3436 struct xfs_mount *mp = tp->t_mountp;
3437 struct xfs_buf *agbp;
3438 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
3439 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
3440 struct xfs_agf *agf;
3442 unsigned int busy_flags = 0;
3443 struct xfs_perag *pag;
3446 ASSERT(type != XFS_AG_RESV_AGFL);
3448 if (XFS_TEST_ERROR(false, mp,
3449 XFS_ERRTAG_FREE_EXTENT))
3452 pag = xfs_perag_get(mp, agno);
3453 error = xfs_free_extent_fix_freelist(tp, pag, &agbp);
3458 if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) {
3459 error = -EFSCORRUPTED;
3463 /* validate the extent size is legal now we have the agf locked */
3464 if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) {
3465 error = -EFSCORRUPTED;
3469 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
3474 busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
3475 xfs_extent_busy_insert(tp, pag, agbno, len, busy_flags);
3480 xfs_trans_brelse(tp, agbp);
3486 struct xfs_alloc_query_range_info {
3487 xfs_alloc_query_range_fn fn;
3491 /* Format btree record and pass to our callback. */
3493 xfs_alloc_query_range_helper(
3494 struct xfs_btree_cur *cur,
3495 const union xfs_btree_rec *rec,
3498 struct xfs_alloc_query_range_info *query = priv;
3499 struct xfs_alloc_rec_incore irec;
3501 irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
3502 irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
3503 return query->fn(cur, &irec, query->priv);
3506 /* Find all free space within a given range of blocks. */
3508 xfs_alloc_query_range(
3509 struct xfs_btree_cur *cur,
3510 const struct xfs_alloc_rec_incore *low_rec,
3511 const struct xfs_alloc_rec_incore *high_rec,
3512 xfs_alloc_query_range_fn fn,
3515 union xfs_btree_irec low_brec;
3516 union xfs_btree_irec high_brec;
3517 struct xfs_alloc_query_range_info query;
3519 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3520 low_brec.a = *low_rec;
3521 high_brec.a = *high_rec;
3524 return xfs_btree_query_range(cur, &low_brec, &high_brec,
3525 xfs_alloc_query_range_helper, &query);
3528 /* Find all free space records. */
3530 xfs_alloc_query_all(
3531 struct xfs_btree_cur *cur,
3532 xfs_alloc_query_range_fn fn,
3535 struct xfs_alloc_query_range_info query;
3537 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3540 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
3543 /* Is there a record covering a given extent? */
3545 xfs_alloc_has_record(
3546 struct xfs_btree_cur *cur,
3551 union xfs_btree_irec low;
3552 union xfs_btree_irec high;
3554 memset(&low, 0, sizeof(low));
3555 low.a.ar_startblock = bno;
3556 memset(&high, 0xFF, sizeof(high));
3557 high.a.ar_startblock = bno + len - 1;
3559 return xfs_btree_has_record(cur, &low, &high, exists);
3563 * Walk all the blocks in the AGFL. The @walk_fn can return any negative
3564 * error code or XFS_ITER_*.
3568 struct xfs_mount *mp,
3569 struct xfs_agf *agf,
3570 struct xfs_buf *agflbp,
3571 xfs_agfl_walk_fn walk_fn,
3578 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
3579 i = be32_to_cpu(agf->agf_flfirst);
3581 /* Nothing to walk in an empty AGFL. */
3582 if (agf->agf_flcount == cpu_to_be32(0))
3585 /* Otherwise, walk from first to last, wrapping as needed. */
3587 error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
3590 if (i == be32_to_cpu(agf->agf_fllast))
3592 if (++i == xfs_agfl_size(mp))
3600 xfs_extfree_intent_init_cache(void)
3602 xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent",
3603 sizeof(struct xfs_extent_free_item),
3606 return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM;
3610 xfs_extfree_intent_destroy_cache(void)
3612 kmem_cache_destroy(xfs_extfree_item_cache);
3613 xfs_extfree_item_cache = NULL;