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
40 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
41 * the beginning of the block for a proper header with the location information
48 unsigned int size = mp->m_sb.sb_sectsize;
51 size -= sizeof(struct xfs_agfl);
53 return size / sizeof(xfs_agblock_t);
60 if (xfs_has_rmapbt(mp))
61 return XFS_RMAP_BLOCK(mp) + 1;
62 if (xfs_has_finobt(mp))
63 return XFS_FIBT_BLOCK(mp) + 1;
64 return XFS_IBT_BLOCK(mp) + 1;
71 if (xfs_has_reflink(mp))
72 return xfs_refc_block(mp) + 1;
73 if (xfs_has_rmapbt(mp))
74 return XFS_RMAP_BLOCK(mp) + 1;
75 if (xfs_has_finobt(mp))
76 return XFS_FIBT_BLOCK(mp) + 1;
77 return XFS_IBT_BLOCK(mp) + 1;
81 * The number of blocks per AG that we withhold from xfs_mod_fdblocks to
82 * guarantee that we can refill the AGFL prior to allocating space in a nearly
83 * full AG. Although the space described by the free space btrees, the
84 * blocks used by the freesp btrees themselves, and the blocks owned by the
85 * AGFL are counted in the ondisk fdblocks, it's a mistake to let the ondisk
86 * free space in the AG drop so low that the free space btrees cannot refill an
87 * empty AGFL up to the minimum level. Rather than grind through empty AGs
88 * until the fs goes down, we subtract this many AG blocks from the incore
89 * fdblocks to ensure user allocation does not overcommit the space the
90 * filesystem needs for the AGFLs. The rmap btree uses a per-AG reservation to
91 * withhold space from xfs_mod_fdblocks, so we do not account for that here.
93 #define XFS_ALLOCBT_AGFL_RESERVE 4
96 * Compute the number of blocks that we set aside to guarantee the ability to
97 * refill the AGFL and handle a full bmap btree split.
99 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
100 * AGF buffer (PV 947395), we place constraints on the relationship among
101 * actual allocations for data blocks, freelist blocks, and potential file data
102 * bmap btree blocks. However, these restrictions may result in no actual space
103 * allocated for a delayed extent, for example, a data block in a certain AG is
104 * allocated but there is no additional block for the additional bmap btree
105 * block due to a split of the bmap btree of the file. The result of this may
106 * lead to an infinite loop when the file gets flushed to disk and all delayed
107 * extents need to be actually allocated. To get around this, we explicitly set
108 * aside a few blocks which will not be reserved in delayed allocation.
110 * For each AG, we need to reserve enough blocks to replenish a totally empty
111 * AGFL and 4 more to handle a potential split of the file's bmap btree.
115 struct xfs_mount *mp)
117 return mp->m_sb.sb_agcount * (XFS_ALLOCBT_AGFL_RESERVE + 4);
121 * When deciding how much space to allocate out of an AG, we limit the
122 * allocation maximum size to the size the AG. However, we cannot use all the
123 * blocks in the AG - some are permanently used by metadata. These
124 * blocks are generally:
125 * - the AG superblock, AGF, AGI and AGFL
126 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
127 * the AGI free inode and rmap btree root blocks.
128 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
129 * - the rmapbt root block
131 * The AG headers are sector sized, so the amount of space they take up is
132 * dependent on filesystem geometry. The others are all single blocks.
135 xfs_alloc_ag_max_usable(
136 struct xfs_mount *mp)
140 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
141 blocks += XFS_ALLOCBT_AGFL_RESERVE;
142 blocks += 3; /* AGF, AGI btree root blocks */
143 if (xfs_has_finobt(mp))
144 blocks++; /* finobt root block */
145 if (xfs_has_rmapbt(mp))
146 blocks++; /* rmap root block */
147 if (xfs_has_reflink(mp))
148 blocks++; /* refcount root block */
150 return mp->m_sb.sb_agblocks - blocks;
154 * Lookup the record equal to [bno, len] in the btree given by cur.
156 STATIC int /* error */
158 struct xfs_btree_cur *cur, /* btree cursor */
159 xfs_agblock_t bno, /* starting block of extent */
160 xfs_extlen_t len, /* length of extent */
161 int *stat) /* success/failure */
165 cur->bc_rec.a.ar_startblock = bno;
166 cur->bc_rec.a.ar_blockcount = len;
167 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
168 cur->bc_ag.abt.active = (*stat == 1);
173 * Lookup the first record greater than or equal to [bno, len]
174 * in the btree given by cur.
178 struct xfs_btree_cur *cur, /* btree cursor */
179 xfs_agblock_t bno, /* starting block of extent */
180 xfs_extlen_t len, /* length of extent */
181 int *stat) /* success/failure */
185 cur->bc_rec.a.ar_startblock = bno;
186 cur->bc_rec.a.ar_blockcount = len;
187 error = xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
188 cur->bc_ag.abt.active = (*stat == 1);
193 * Lookup the first record less than or equal to [bno, len]
194 * in the btree given by cur.
198 struct xfs_btree_cur *cur, /* btree cursor */
199 xfs_agblock_t bno, /* starting block of extent */
200 xfs_extlen_t len, /* length of extent */
201 int *stat) /* success/failure */
204 cur->bc_rec.a.ar_startblock = bno;
205 cur->bc_rec.a.ar_blockcount = len;
206 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
207 cur->bc_ag.abt.active = (*stat == 1);
212 xfs_alloc_cur_active(
213 struct xfs_btree_cur *cur)
215 return cur && cur->bc_ag.abt.active;
219 * Update the record referred to by cur to the value given
221 * This either works (return 0) or gets an EFSCORRUPTED error.
223 STATIC int /* error */
225 struct xfs_btree_cur *cur, /* btree cursor */
226 xfs_agblock_t bno, /* starting block of extent */
227 xfs_extlen_t len) /* length of extent */
229 union xfs_btree_rec rec;
231 rec.alloc.ar_startblock = cpu_to_be32(bno);
232 rec.alloc.ar_blockcount = cpu_to_be32(len);
233 return xfs_btree_update(cur, &rec);
237 * Get the data from the pointed-to record.
241 struct xfs_btree_cur *cur, /* btree cursor */
242 xfs_agblock_t *bno, /* output: starting block of extent */
243 xfs_extlen_t *len, /* output: length of extent */
244 int *stat) /* output: success/failure */
246 struct xfs_mount *mp = cur->bc_mp;
247 struct xfs_perag *pag = cur->bc_ag.pag;
248 union xfs_btree_rec *rec;
251 error = xfs_btree_get_rec(cur, &rec, stat);
252 if (error || !(*stat))
255 *bno = be32_to_cpu(rec->alloc.ar_startblock);
256 *len = be32_to_cpu(rec->alloc.ar_blockcount);
261 /* check for valid extent range, including overflow */
262 if (!xfs_verify_agbext(pag, *bno, *len))
269 "%s Freespace BTree record corruption in AG %d detected!",
270 cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size",
273 "start block 0x%x block count 0x%x", *bno, *len);
274 return -EFSCORRUPTED;
278 * Compute aligned version of the found extent.
279 * Takes alignment and min length into account.
282 xfs_alloc_compute_aligned(
283 xfs_alloc_arg_t *args, /* allocation argument structure */
284 xfs_agblock_t foundbno, /* starting block in found extent */
285 xfs_extlen_t foundlen, /* length in found extent */
286 xfs_agblock_t *resbno, /* result block number */
287 xfs_extlen_t *reslen, /* result length */
290 xfs_agblock_t bno = foundbno;
291 xfs_extlen_t len = foundlen;
295 /* Trim busy sections out of found extent */
296 busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
299 * If we have a largish extent that happens to start before min_agbno,
300 * see if we can shift it into range...
302 if (bno < args->min_agbno && bno + len > args->min_agbno) {
303 diff = args->min_agbno - bno;
310 if (args->alignment > 1 && len >= args->minlen) {
311 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
313 diff = aligned_bno - bno;
315 *resbno = aligned_bno;
316 *reslen = diff >= len ? 0 : len - diff;
326 * Compute best start block and diff for "near" allocations.
327 * freelen >= wantlen already checked by caller.
329 STATIC xfs_extlen_t /* difference value (absolute) */
330 xfs_alloc_compute_diff(
331 xfs_agblock_t wantbno, /* target starting block */
332 xfs_extlen_t wantlen, /* target length */
333 xfs_extlen_t alignment, /* target alignment */
334 int datatype, /* are we allocating data? */
335 xfs_agblock_t freebno, /* freespace's starting block */
336 xfs_extlen_t freelen, /* freespace's length */
337 xfs_agblock_t *newbnop) /* result: best start block from free */
339 xfs_agblock_t freeend; /* end of freespace extent */
340 xfs_agblock_t newbno1; /* return block number */
341 xfs_agblock_t newbno2; /* other new block number */
342 xfs_extlen_t newlen1=0; /* length with newbno1 */
343 xfs_extlen_t newlen2=0; /* length with newbno2 */
344 xfs_agblock_t wantend; /* end of target extent */
345 bool userdata = datatype & XFS_ALLOC_USERDATA;
347 ASSERT(freelen >= wantlen);
348 freeend = freebno + freelen;
349 wantend = wantbno + wantlen;
351 * We want to allocate from the start of a free extent if it is past
352 * the desired block or if we are allocating user data and the free
353 * extent is before desired block. The second case is there to allow
354 * for contiguous allocation from the remaining free space if the file
355 * grows in the short term.
357 if (freebno >= wantbno || (userdata && freeend < wantend)) {
358 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
359 newbno1 = NULLAGBLOCK;
360 } else if (freeend >= wantend && alignment > 1) {
361 newbno1 = roundup(wantbno, alignment);
362 newbno2 = newbno1 - alignment;
363 if (newbno1 >= freeend)
364 newbno1 = NULLAGBLOCK;
366 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
367 if (newbno2 < freebno)
368 newbno2 = NULLAGBLOCK;
370 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
371 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
372 if (newlen1 < newlen2 ||
373 (newlen1 == newlen2 &&
374 XFS_ABSDIFF(newbno1, wantbno) >
375 XFS_ABSDIFF(newbno2, wantbno)))
377 } else if (newbno2 != NULLAGBLOCK)
379 } else if (freeend >= wantend) {
381 } else if (alignment > 1) {
382 newbno1 = roundup(freeend - wantlen, alignment);
383 if (newbno1 > freeend - wantlen &&
384 newbno1 - alignment >= freebno)
385 newbno1 -= alignment;
386 else if (newbno1 >= freeend)
387 newbno1 = NULLAGBLOCK;
389 newbno1 = freeend - wantlen;
391 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
395 * Fix up the length, based on mod and prod.
396 * len should be k * prod + mod for some k.
397 * If len is too small it is returned unchanged.
398 * If len hits maxlen it is left alone.
402 xfs_alloc_arg_t *args) /* allocation argument structure */
407 ASSERT(args->mod < args->prod);
409 ASSERT(rlen >= args->minlen);
410 ASSERT(rlen <= args->maxlen);
411 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
412 (args->mod == 0 && rlen < args->prod))
414 k = rlen % args->prod;
418 rlen = rlen - (k - args->mod);
420 rlen = rlen - args->prod + (args->mod - k);
421 /* casts to (int) catch length underflows */
422 if ((int)rlen < (int)args->minlen)
424 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
425 ASSERT(rlen % args->prod == args->mod);
426 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
427 rlen + args->minleft);
432 * Update the two btrees, logically removing from freespace the extent
433 * starting at rbno, rlen blocks. The extent is contained within the
434 * actual (current) free extent fbno for flen blocks.
435 * Flags are passed in indicating whether the cursors are set to the
438 STATIC int /* error code */
439 xfs_alloc_fixup_trees(
440 struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */
441 struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */
442 xfs_agblock_t fbno, /* starting block of free extent */
443 xfs_extlen_t flen, /* length of free extent */
444 xfs_agblock_t rbno, /* starting block of returned extent */
445 xfs_extlen_t rlen, /* length of returned extent */
446 int flags) /* flags, XFSA_FIXUP_... */
448 int error; /* error code */
449 int i; /* operation results */
450 xfs_agblock_t nfbno1; /* first new free startblock */
451 xfs_agblock_t nfbno2; /* second new free startblock */
452 xfs_extlen_t nflen1=0; /* first new free length */
453 xfs_extlen_t nflen2=0; /* second new free length */
454 struct xfs_mount *mp;
459 * Look up the record in the by-size tree if necessary.
461 if (flags & XFSA_FIXUP_CNT_OK) {
463 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
465 if (XFS_IS_CORRUPT(mp,
469 return -EFSCORRUPTED;
472 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
474 if (XFS_IS_CORRUPT(mp, i != 1))
475 return -EFSCORRUPTED;
478 * Look up the record in the by-block tree if necessary.
480 if (flags & XFSA_FIXUP_BNO_OK) {
482 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
484 if (XFS_IS_CORRUPT(mp,
488 return -EFSCORRUPTED;
491 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
493 if (XFS_IS_CORRUPT(mp, i != 1))
494 return -EFSCORRUPTED;
498 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
499 struct xfs_btree_block *bnoblock;
500 struct xfs_btree_block *cntblock;
502 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp);
503 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp);
505 if (XFS_IS_CORRUPT(mp,
506 bnoblock->bb_numrecs !=
507 cntblock->bb_numrecs))
508 return -EFSCORRUPTED;
513 * Deal with all four cases: the allocated record is contained
514 * within the freespace record, so we can have new freespace
515 * at either (or both) end, or no freespace remaining.
517 if (rbno == fbno && rlen == flen)
518 nfbno1 = nfbno2 = NULLAGBLOCK;
519 else if (rbno == fbno) {
520 nfbno1 = rbno + rlen;
521 nflen1 = flen - rlen;
522 nfbno2 = NULLAGBLOCK;
523 } else if (rbno + rlen == fbno + flen) {
525 nflen1 = flen - rlen;
526 nfbno2 = NULLAGBLOCK;
529 nflen1 = rbno - fbno;
530 nfbno2 = rbno + rlen;
531 nflen2 = (fbno + flen) - nfbno2;
534 * Delete the entry from the by-size btree.
536 if ((error = xfs_btree_delete(cnt_cur, &i)))
538 if (XFS_IS_CORRUPT(mp, i != 1))
539 return -EFSCORRUPTED;
541 * Add new by-size btree entry(s).
543 if (nfbno1 != NULLAGBLOCK) {
544 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
546 if (XFS_IS_CORRUPT(mp, i != 0))
547 return -EFSCORRUPTED;
548 if ((error = xfs_btree_insert(cnt_cur, &i)))
550 if (XFS_IS_CORRUPT(mp, i != 1))
551 return -EFSCORRUPTED;
553 if (nfbno2 != NULLAGBLOCK) {
554 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
556 if (XFS_IS_CORRUPT(mp, i != 0))
557 return -EFSCORRUPTED;
558 if ((error = xfs_btree_insert(cnt_cur, &i)))
560 if (XFS_IS_CORRUPT(mp, i != 1))
561 return -EFSCORRUPTED;
564 * Fix up the by-block btree entry(s).
566 if (nfbno1 == NULLAGBLOCK) {
568 * No remaining freespace, just delete the by-block tree entry.
570 if ((error = xfs_btree_delete(bno_cur, &i)))
572 if (XFS_IS_CORRUPT(mp, i != 1))
573 return -EFSCORRUPTED;
576 * Update the by-block entry to start later|be shorter.
578 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
581 if (nfbno2 != NULLAGBLOCK) {
583 * 2 resulting free entries, need to add one.
585 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
587 if (XFS_IS_CORRUPT(mp, i != 0))
588 return -EFSCORRUPTED;
589 if ((error = xfs_btree_insert(bno_cur, &i)))
591 if (XFS_IS_CORRUPT(mp, i != 1))
592 return -EFSCORRUPTED;
597 static xfs_failaddr_t
601 struct xfs_mount *mp = bp->b_mount;
602 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
603 __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp);
607 * There is no verification of non-crc AGFLs because mkfs does not
608 * initialise the AGFL to zero or NULL. Hence the only valid part of the
609 * AGFL is what the AGF says is active. We can't get to the AGF, so we
610 * can't verify just those entries are valid.
612 if (!xfs_has_crc(mp))
615 if (!xfs_verify_magic(bp, agfl->agfl_magicnum))
616 return __this_address;
617 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
618 return __this_address;
620 * during growfs operations, the perag is not fully initialised,
621 * so we can't use it for any useful checking. growfs ensures we can't
622 * use it by using uncached buffers that don't have the perag attached
623 * so we can detect and avoid this problem.
625 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
626 return __this_address;
628 for (i = 0; i < xfs_agfl_size(mp); i++) {
629 if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK &&
630 be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks)
631 return __this_address;
634 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
635 return __this_address;
640 xfs_agfl_read_verify(
643 struct xfs_mount *mp = bp->b_mount;
647 * There is no verification of non-crc AGFLs because mkfs does not
648 * initialise the AGFL to zero or NULL. Hence the only valid part of the
649 * AGFL is what the AGF says is active. We can't get to the AGF, so we
650 * can't verify just those entries are valid.
652 if (!xfs_has_crc(mp))
655 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
656 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
658 fa = xfs_agfl_verify(bp);
660 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
665 xfs_agfl_write_verify(
668 struct xfs_mount *mp = bp->b_mount;
669 struct xfs_buf_log_item *bip = bp->b_log_item;
672 /* no verification of non-crc AGFLs */
673 if (!xfs_has_crc(mp))
676 fa = xfs_agfl_verify(bp);
678 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
683 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
685 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
688 const struct xfs_buf_ops xfs_agfl_buf_ops = {
690 .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) },
691 .verify_read = xfs_agfl_read_verify,
692 .verify_write = xfs_agfl_write_verify,
693 .verify_struct = xfs_agfl_verify,
697 * Read in the allocation group free block array.
701 struct xfs_perag *pag,
702 struct xfs_trans *tp,
703 struct xfs_buf **bpp)
705 struct xfs_mount *mp = pag->pag_mount;
709 error = xfs_trans_read_buf(
710 mp, tp, mp->m_ddev_targp,
711 XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGFL_DADDR(mp)),
712 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
715 xfs_buf_set_ref(bp, XFS_AGFL_REF);
721 xfs_alloc_update_counters(
722 struct xfs_trans *tp,
723 struct xfs_buf *agbp,
726 struct xfs_agf *agf = agbp->b_addr;
728 agbp->b_pag->pagf_freeblks += len;
729 be32_add_cpu(&agf->agf_freeblks, len);
731 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
732 be32_to_cpu(agf->agf_length))) {
733 xfs_buf_mark_corrupt(agbp);
734 return -EFSCORRUPTED;
737 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
742 * Block allocation algorithm and data structures.
744 struct xfs_alloc_cur {
745 struct xfs_btree_cur *cnt; /* btree cursors */
746 struct xfs_btree_cur *bnolt;
747 struct xfs_btree_cur *bnogt;
748 xfs_extlen_t cur_len;/* current search length */
749 xfs_agblock_t rec_bno;/* extent startblock */
750 xfs_extlen_t rec_len;/* extent length */
751 xfs_agblock_t bno; /* alloc bno */
752 xfs_extlen_t len; /* alloc len */
753 xfs_extlen_t diff; /* diff from search bno */
754 unsigned int busy_gen;/* busy state */
759 * Set up cursors, etc. in the extent allocation cursor. This function can be
760 * called multiple times to reset an initialized structure without having to
761 * reallocate cursors.
765 struct xfs_alloc_arg *args,
766 struct xfs_alloc_cur *acur)
771 acur->cur_len = args->maxlen;
781 * Perform an initial cntbt lookup to check for availability of maxlen
782 * extents. If this fails, we'll return -ENOSPC to signal the caller to
783 * attempt a small allocation.
786 acur->cnt = xfs_allocbt_init_cursor(args->mp, args->tp,
787 args->agbp, args->pag, XFS_BTNUM_CNT);
788 error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i);
793 * Allocate the bnobt left and right search cursors.
796 acur->bnolt = xfs_allocbt_init_cursor(args->mp, args->tp,
797 args->agbp, args->pag, XFS_BTNUM_BNO);
799 acur->bnogt = xfs_allocbt_init_cursor(args->mp, args->tp,
800 args->agbp, args->pag, XFS_BTNUM_BNO);
801 return i == 1 ? 0 : -ENOSPC;
806 struct xfs_alloc_cur *acur,
809 int cur_error = XFS_BTREE_NOERROR;
812 cur_error = XFS_BTREE_ERROR;
815 xfs_btree_del_cursor(acur->cnt, cur_error);
817 xfs_btree_del_cursor(acur->bnolt, cur_error);
819 xfs_btree_del_cursor(acur->bnogt, cur_error);
820 acur->cnt = acur->bnolt = acur->bnogt = NULL;
824 * Check an extent for allocation and track the best available candidate in the
825 * allocation structure. The cursor is deactivated if it has entered an out of
826 * range state based on allocation arguments. Optionally return the extent
827 * extent geometry and allocation status if requested by the caller.
831 struct xfs_alloc_arg *args,
832 struct xfs_alloc_cur *acur,
833 struct xfs_btree_cur *cur,
837 xfs_agblock_t bno, bnoa, bnew;
838 xfs_extlen_t len, lena, diff = -1;
840 unsigned busy_gen = 0;
841 bool deactivate = false;
842 bool isbnobt = cur->bc_btnum == XFS_BTNUM_BNO;
846 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
849 if (XFS_IS_CORRUPT(args->mp, i != 1))
850 return -EFSCORRUPTED;
853 * Check minlen and deactivate a cntbt cursor if out of acceptable size
854 * range (i.e., walking backwards looking for a minlen extent).
856 if (len < args->minlen) {
857 deactivate = !isbnobt;
861 busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena,
865 acur->busy_gen = busy_gen;
866 /* deactivate a bnobt cursor outside of locality range */
867 if (bnoa < args->min_agbno || bnoa > args->max_agbno) {
868 deactivate = isbnobt;
871 if (lena < args->minlen)
874 args->len = XFS_EXTLEN_MIN(lena, args->maxlen);
875 xfs_alloc_fix_len(args);
876 ASSERT(args->len >= args->minlen);
877 if (args->len < acur->len)
881 * We have an aligned record that satisfies minlen and beats or matches
882 * the candidate extent size. Compare locality for near allocation mode.
884 diff = xfs_alloc_compute_diff(args->agbno, args->len,
885 args->alignment, args->datatype,
887 if (bnew == NULLAGBLOCK)
891 * Deactivate a bnobt cursor with worse locality than the current best.
893 if (diff > acur->diff) {
894 deactivate = isbnobt;
898 ASSERT(args->len > acur->len ||
899 (args->len == acur->len && diff <= acur->diff));
903 acur->len = args->len;
908 * We're done if we found a perfect allocation. This only deactivates
909 * the current cursor, but this is just an optimization to terminate a
910 * cntbt search that otherwise runs to the edge of the tree.
912 if (acur->diff == 0 && acur->len == args->maxlen)
916 cur->bc_ag.abt.active = false;
917 trace_xfs_alloc_cur_check(args->mp, cur->bc_btnum, bno, len, diff,
923 * Complete an allocation of a candidate extent. Remove the extent from both
924 * trees and update the args structure.
927 xfs_alloc_cur_finish(
928 struct xfs_alloc_arg *args,
929 struct xfs_alloc_cur *acur)
931 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
934 ASSERT(acur->cnt && acur->bnolt);
935 ASSERT(acur->bno >= acur->rec_bno);
936 ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len);
937 ASSERT(acur->rec_bno + acur->rec_len <= be32_to_cpu(agf->agf_length));
939 error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno,
940 acur->rec_len, acur->bno, acur->len, 0);
944 args->agbno = acur->bno;
945 args->len = acur->len;
948 trace_xfs_alloc_cur(args);
953 * Locality allocation lookup algorithm. This expects a cntbt cursor and uses
954 * bno optimized lookup to search for extents with ideal size and locality.
957 xfs_alloc_cntbt_iter(
958 struct xfs_alloc_arg *args,
959 struct xfs_alloc_cur *acur)
961 struct xfs_btree_cur *cur = acur->cnt;
963 xfs_extlen_t len, cur_len;
967 if (!xfs_alloc_cur_active(cur))
970 /* locality optimized lookup */
971 cur_len = acur->cur_len;
972 error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i);
977 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
981 /* check the current record and update search length from it */
982 error = xfs_alloc_cur_check(args, acur, cur, &i);
985 ASSERT(len >= acur->cur_len);
989 * We looked up the first record >= [agbno, len] above. The agbno is a
990 * secondary key and so the current record may lie just before or after
991 * agbno. If it is past agbno, check the previous record too so long as
992 * the length matches as it may be closer. Don't check a smaller record
993 * because that could deactivate our cursor.
995 if (bno > args->agbno) {
996 error = xfs_btree_decrement(cur, 0, &i);
998 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
999 if (!error && i && len == acur->cur_len)
1000 error = xfs_alloc_cur_check(args, acur, cur,
1008 * Increment the search key until we find at least one allocation
1009 * candidate or if the extent we found was larger. Otherwise, double the
1010 * search key to optimize the search. Efficiency is more important here
1011 * than absolute best locality.
1014 if (!acur->len || acur->cur_len >= cur_len)
1017 acur->cur_len = cur_len;
1023 * Deal with the case where only small freespaces remain. Either return the
1024 * contents of the last freespace record, or allocate space from the freelist if
1025 * there is nothing in the tree.
1027 STATIC int /* error */
1028 xfs_alloc_ag_vextent_small(
1029 struct xfs_alloc_arg *args, /* allocation argument structure */
1030 struct xfs_btree_cur *ccur, /* optional by-size cursor */
1031 xfs_agblock_t *fbnop, /* result block number */
1032 xfs_extlen_t *flenp, /* result length */
1033 int *stat) /* status: 0-freelist, 1-normal/none */
1035 struct xfs_agf *agf = args->agbp->b_addr;
1037 xfs_agblock_t fbno = NULLAGBLOCK;
1038 xfs_extlen_t flen = 0;
1042 * If a cntbt cursor is provided, try to allocate the largest record in
1043 * the tree. Try the AGFL if the cntbt is empty, otherwise fail the
1044 * allocation. Make sure to respect minleft even when pulling from the
1048 error = xfs_btree_decrement(ccur, 0, &i);
1052 error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i);
1055 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1056 error = -EFSCORRUPTED;
1062 if (args->minlen != 1 || args->alignment != 1 ||
1063 args->resv == XFS_AG_RESV_AGFL ||
1064 be32_to_cpu(agf->agf_flcount) <= args->minleft)
1067 error = xfs_alloc_get_freelist(args->pag, args->tp, args->agbp,
1071 if (fbno == NULLAGBLOCK)
1074 xfs_extent_busy_reuse(args->mp, args->pag, fbno, 1,
1075 (args->datatype & XFS_ALLOC_NOBUSY));
1077 if (args->datatype & XFS_ALLOC_USERDATA) {
1080 error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp,
1081 XFS_AGB_TO_DADDR(args->mp, args->agno, fbno),
1082 args->mp->m_bsize, 0, &bp);
1085 xfs_trans_binval(args->tp, bp);
1087 *fbnop = args->agbno = fbno;
1088 *flenp = args->len = 1;
1089 if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) {
1090 error = -EFSCORRUPTED;
1093 args->wasfromfl = 1;
1094 trace_xfs_alloc_small_freelist(args);
1097 * If we're feeding an AGFL block to something that doesn't live in the
1098 * free space, we need to clear out the OWN_AG rmap.
1100 error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1,
1101 &XFS_RMAP_OINFO_AG);
1110 * Can't do the allocation, give up.
1112 if (flen < args->minlen) {
1113 args->agbno = NULLAGBLOCK;
1114 trace_xfs_alloc_small_notenough(args);
1120 trace_xfs_alloc_small_done(args);
1124 trace_xfs_alloc_small_error(args);
1129 * Allocate a variable extent at exactly agno/bno.
1130 * Extent's length (returned in *len) will be between minlen and maxlen,
1131 * and of the form k * prod + mod unless there's nothing that large.
1132 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
1134 STATIC int /* error */
1135 xfs_alloc_ag_vextent_exact(
1136 xfs_alloc_arg_t *args) /* allocation argument structure */
1138 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
1139 struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */
1140 struct xfs_btree_cur *cnt_cur;/* by count btree cursor */
1142 xfs_agblock_t fbno; /* start block of found extent */
1143 xfs_extlen_t flen; /* length of found extent */
1144 xfs_agblock_t tbno; /* start block of busy extent */
1145 xfs_extlen_t tlen; /* length of busy extent */
1146 xfs_agblock_t tend; /* end block of busy extent */
1147 int i; /* success/failure of operation */
1150 ASSERT(args->alignment == 1);
1153 * Allocate/initialize a cursor for the by-number freespace btree.
1155 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1156 args->pag, XFS_BTNUM_BNO);
1159 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
1160 * Look for the closest free block <= bno, it must contain bno
1161 * if any free block does.
1163 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
1170 * Grab the freespace record.
1172 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
1175 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1176 error = -EFSCORRUPTED;
1179 ASSERT(fbno <= args->agbno);
1182 * Check for overlapping busy extents.
1186 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
1189 * Give up if the start of the extent is busy, or the freespace isn't
1190 * long enough for the minimum request.
1192 if (tbno > args->agbno)
1194 if (tlen < args->minlen)
1197 if (tend < args->agbno + args->minlen)
1201 * End of extent will be smaller of the freespace end and the
1202 * maximal requested end.
1204 * Fix the length according to mod and prod if given.
1206 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
1208 xfs_alloc_fix_len(args);
1209 ASSERT(args->agbno + args->len <= tend);
1212 * We are allocating agbno for args->len
1213 * Allocate/initialize a cursor for the by-size btree.
1215 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1216 args->pag, XFS_BTNUM_CNT);
1217 ASSERT(args->agbno + args->len <= be32_to_cpu(agf->agf_length));
1218 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
1219 args->len, XFSA_FIXUP_BNO_OK);
1221 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1225 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1226 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1228 args->wasfromfl = 0;
1229 trace_xfs_alloc_exact_done(args);
1233 /* Didn't find it, return null. */
1234 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1235 args->agbno = NULLAGBLOCK;
1236 trace_xfs_alloc_exact_notfound(args);
1240 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1241 trace_xfs_alloc_exact_error(args);
1246 * Search a given number of btree records in a given direction. Check each
1247 * record against the good extent we've already found.
1250 xfs_alloc_walk_iter(
1251 struct xfs_alloc_arg *args,
1252 struct xfs_alloc_cur *acur,
1253 struct xfs_btree_cur *cur,
1255 bool find_one, /* quit on first candidate */
1256 int count, /* rec count (-1 for infinite) */
1265 * Search so long as the cursor is active or we find a better extent.
1266 * The cursor is deactivated if it extends beyond the range of the
1267 * current allocation candidate.
1269 while (xfs_alloc_cur_active(cur) && count) {
1270 error = xfs_alloc_cur_check(args, acur, cur, &i);
1278 if (!xfs_alloc_cur_active(cur))
1282 error = xfs_btree_increment(cur, 0, &i);
1284 error = xfs_btree_decrement(cur, 0, &i);
1288 cur->bc_ag.abt.active = false;
1298 * Search the by-bno and by-size btrees in parallel in search of an extent with
1299 * ideal locality based on the NEAR mode ->agbno locality hint.
1302 xfs_alloc_ag_vextent_locality(
1303 struct xfs_alloc_arg *args,
1304 struct xfs_alloc_cur *acur,
1307 struct xfs_btree_cur *fbcur = NULL;
1312 ASSERT(acur->len == 0);
1316 error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i);
1319 error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i);
1322 error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i);
1327 * Search the bnobt and cntbt in parallel. Search the bnobt left and
1328 * right and lookup the closest extent to the locality hint for each
1329 * extent size key in the cntbt. The entire search terminates
1330 * immediately on a bnobt hit because that means we've found best case
1331 * locality. Otherwise the search continues until the cntbt cursor runs
1332 * off the end of the tree. If no allocation candidate is found at this
1333 * point, give up on locality, walk backwards from the end of the cntbt
1334 * and take the first available extent.
1336 * The parallel tree searches balance each other out to provide fairly
1337 * consistent performance for various situations. The bnobt search can
1338 * have pathological behavior in the worst case scenario of larger
1339 * allocation requests and fragmented free space. On the other hand, the
1340 * bnobt is able to satisfy most smaller allocation requests much more
1341 * quickly than the cntbt. The cntbt search can sift through fragmented
1342 * free space and sets of free extents for larger allocation requests
1343 * more quickly than the bnobt. Since the locality hint is just a hint
1344 * and we don't want to scan the entire bnobt for perfect locality, the
1345 * cntbt search essentially bounds the bnobt search such that we can
1346 * find good enough locality at reasonable performance in most cases.
1348 while (xfs_alloc_cur_active(acur->bnolt) ||
1349 xfs_alloc_cur_active(acur->bnogt) ||
1350 xfs_alloc_cur_active(acur->cnt)) {
1352 trace_xfs_alloc_cur_lookup(args);
1355 * Search the bnobt left and right. In the case of a hit, finish
1356 * the search in the opposite direction and we're done.
1358 error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false,
1363 trace_xfs_alloc_cur_left(args);
1364 fbcur = acur->bnogt;
1368 error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true,
1373 trace_xfs_alloc_cur_right(args);
1374 fbcur = acur->bnolt;
1380 * Check the extent with best locality based on the current
1381 * extent size search key and keep track of the best candidate.
1383 error = xfs_alloc_cntbt_iter(args, acur);
1386 if (!xfs_alloc_cur_active(acur->cnt)) {
1387 trace_xfs_alloc_cur_lookup_done(args);
1393 * If we failed to find anything due to busy extents, return empty
1394 * handed so the caller can flush and retry. If no busy extents were
1395 * found, walk backwards from the end of the cntbt as a last resort.
1397 if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) {
1398 error = xfs_btree_decrement(acur->cnt, 0, &i);
1402 acur->cnt->bc_ag.abt.active = true;
1409 * Search in the opposite direction for a better entry in the case of
1410 * a bnobt hit or walk backwards from the end of the cntbt.
1413 error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1,
1425 /* Check the last block of the cnt btree for allocations. */
1427 xfs_alloc_ag_vextent_lastblock(
1428 struct xfs_alloc_arg *args,
1429 struct xfs_alloc_cur *acur,
1438 /* Randomly don't execute the first algorithm. */
1439 if (get_random_u32_below(2))
1444 * Start from the entry that lookup found, sequence through all larger
1445 * free blocks. If we're actually pointing at a record smaller than
1446 * maxlen, go to the start of this block, and skip all those smaller
1449 if (*len || args->alignment > 1) {
1450 acur->cnt->bc_levels[0].ptr = 1;
1452 error = xfs_alloc_get_rec(acur->cnt, bno, len, &i);
1455 if (XFS_IS_CORRUPT(args->mp, i != 1))
1456 return -EFSCORRUPTED;
1457 if (*len >= args->minlen)
1459 error = xfs_btree_increment(acur->cnt, 0, &i);
1463 ASSERT(*len >= args->minlen);
1468 error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i);
1473 * It didn't work. We COULD be in a case where there's a good record
1474 * somewhere, so try again.
1479 trace_xfs_alloc_near_first(args);
1485 * Allocate a variable extent near bno in the allocation group agno.
1486 * Extent's length (returned in len) will be between minlen and maxlen,
1487 * and of the form k * prod + mod unless there's nothing that large.
1488 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1491 xfs_alloc_ag_vextent_near(
1492 struct xfs_alloc_arg *args)
1494 struct xfs_alloc_cur acur = {};
1495 int error; /* error code */
1496 int i; /* result code, temporary */
1500 /* handle uninitialized agbno range so caller doesn't have to */
1501 if (!args->min_agbno && !args->max_agbno)
1502 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1503 ASSERT(args->min_agbno <= args->max_agbno);
1505 /* clamp agbno to the range if it's outside */
1506 if (args->agbno < args->min_agbno)
1507 args->agbno = args->min_agbno;
1508 if (args->agbno > args->max_agbno)
1509 args->agbno = args->max_agbno;
1515 * Set up cursors and see if there are any free extents as big as
1516 * maxlen. If not, pick the last entry in the tree unless the tree is
1519 error = xfs_alloc_cur_setup(args, &acur);
1520 if (error == -ENOSPC) {
1521 error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno,
1525 if (i == 0 || len == 0) {
1526 trace_xfs_alloc_near_noentry(args);
1536 * If the requested extent is large wrt the freespaces available
1537 * in this a.g., then the cursor will be pointing to a btree entry
1538 * near the right edge of the tree. If it's in the last btree leaf
1539 * block, then we just examine all the entries in that block
1540 * that are big enough, and pick the best one.
1542 if (xfs_btree_islastblock(acur.cnt, 0)) {
1543 bool allocated = false;
1545 error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len,
1554 * Second algorithm. Combined cntbt and bnobt search to find ideal
1557 error = xfs_alloc_ag_vextent_locality(args, &acur, &i);
1562 * If we couldn't get anything, give up.
1566 trace_xfs_alloc_near_busy(args);
1567 xfs_extent_busy_flush(args->mp, args->pag,
1571 trace_xfs_alloc_size_neither(args);
1572 args->agbno = NULLAGBLOCK;
1577 /* fix up btrees on a successful allocation */
1578 error = xfs_alloc_cur_finish(args, &acur);
1581 xfs_alloc_cur_close(&acur, error);
1586 * Allocate a variable extent anywhere in the allocation group agno.
1587 * Extent's length (returned in len) will be between minlen and maxlen,
1588 * and of the form k * prod + mod unless there's nothing that large.
1589 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1591 STATIC int /* error */
1592 xfs_alloc_ag_vextent_size(
1593 xfs_alloc_arg_t *args) /* allocation argument structure */
1595 struct xfs_agf *agf = args->agbp->b_addr;
1596 struct xfs_btree_cur *bno_cur; /* cursor for bno btree */
1597 struct xfs_btree_cur *cnt_cur; /* cursor for cnt btree */
1598 int error; /* error result */
1599 xfs_agblock_t fbno; /* start of found freespace */
1600 xfs_extlen_t flen; /* length of found freespace */
1601 int i; /* temp status variable */
1602 xfs_agblock_t rbno; /* returned block number */
1603 xfs_extlen_t rlen; /* length of returned extent */
1609 * Allocate and initialize a cursor for the by-size btree.
1611 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1612 args->pag, XFS_BTNUM_CNT);
1616 * Look for an entry >= maxlen+alignment-1 blocks.
1618 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1619 args->maxlen + args->alignment - 1, &i)))
1623 * If none then we have to settle for a smaller extent. In the case that
1624 * there are no large extents, this will return the last entry in the
1625 * tree unless the tree is empty. In the case that there are only busy
1626 * large extents, this will return the largest small extent unless there
1627 * are no smaller extents available.
1630 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1634 if (i == 0 || flen == 0) {
1635 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1636 trace_xfs_alloc_size_noentry(args);
1640 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1644 * Search for a non-busy extent that is large enough.
1647 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1650 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1651 error = -EFSCORRUPTED;
1655 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1656 &rbno, &rlen, &busy_gen);
1658 if (rlen >= args->maxlen)
1661 error = xfs_btree_increment(cnt_cur, 0, &i);
1666 * Our only valid extents must have been busy.
1667 * Make it unbusy by forcing the log out and
1670 xfs_btree_del_cursor(cnt_cur,
1672 trace_xfs_alloc_size_busy(args);
1673 xfs_extent_busy_flush(args->mp,
1674 args->pag, busy_gen);
1681 * In the first case above, we got the last entry in the
1682 * by-size btree. Now we check to see if the space hits maxlen
1683 * once aligned; if not, we search left for something better.
1684 * This can't happen in the second case above.
1686 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1687 if (XFS_IS_CORRUPT(args->mp,
1690 rbno + rlen > fbno + flen))) {
1691 error = -EFSCORRUPTED;
1694 if (rlen < args->maxlen) {
1695 xfs_agblock_t bestfbno;
1696 xfs_extlen_t bestflen;
1697 xfs_agblock_t bestrbno;
1698 xfs_extlen_t bestrlen;
1705 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1709 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1712 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1713 error = -EFSCORRUPTED;
1716 if (flen < bestrlen)
1718 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1719 &rbno, &rlen, &busy_gen);
1720 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1721 if (XFS_IS_CORRUPT(args->mp,
1724 rbno + rlen > fbno + flen))) {
1725 error = -EFSCORRUPTED;
1728 if (rlen > bestrlen) {
1733 if (rlen == args->maxlen)
1737 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1740 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1741 error = -EFSCORRUPTED;
1749 args->wasfromfl = 0;
1751 * Fix up the length.
1754 if (rlen < args->minlen) {
1756 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1757 trace_xfs_alloc_size_busy(args);
1758 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
1763 xfs_alloc_fix_len(args);
1766 if (XFS_IS_CORRUPT(args->mp, rlen > flen)) {
1767 error = -EFSCORRUPTED;
1771 * Allocate and initialize a cursor for the by-block tree.
1773 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1774 args->pag, XFS_BTNUM_BNO);
1775 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1776 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1778 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1779 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1780 cnt_cur = bno_cur = NULL;
1783 if (XFS_IS_CORRUPT(args->mp,
1784 args->agbno + args->len >
1785 be32_to_cpu(agf->agf_length))) {
1786 error = -EFSCORRUPTED;
1789 trace_xfs_alloc_size_done(args);
1793 trace_xfs_alloc_size_error(args);
1795 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1797 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1801 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1802 trace_xfs_alloc_size_nominleft(args);
1803 args->agbno = NULLAGBLOCK;
1808 * Free the extent starting at agno/bno for length.
1812 struct xfs_trans *tp,
1813 struct xfs_buf *agbp,
1814 xfs_agnumber_t agno,
1817 const struct xfs_owner_info *oinfo,
1818 enum xfs_ag_resv_type type)
1820 struct xfs_mount *mp;
1821 struct xfs_btree_cur *bno_cur;
1822 struct xfs_btree_cur *cnt_cur;
1823 xfs_agblock_t gtbno; /* start of right neighbor */
1824 xfs_extlen_t gtlen; /* length of right neighbor */
1825 xfs_agblock_t ltbno; /* start of left neighbor */
1826 xfs_extlen_t ltlen; /* length of left neighbor */
1827 xfs_agblock_t nbno; /* new starting block of freesp */
1828 xfs_extlen_t nlen; /* new length of freespace */
1829 int haveleft; /* have a left neighbor */
1830 int haveright; /* have a right neighbor */
1833 struct xfs_perag *pag = agbp->b_pag;
1835 bno_cur = cnt_cur = NULL;
1838 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
1839 error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo);
1845 * Allocate and initialize a cursor for the by-block btree.
1847 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_BNO);
1849 * Look for a neighboring block on the left (lower block numbers)
1850 * that is contiguous with this space.
1852 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1856 * There is a block to our left.
1858 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1860 if (XFS_IS_CORRUPT(mp, i != 1)) {
1861 error = -EFSCORRUPTED;
1865 * It's not contiguous, though.
1867 if (ltbno + ltlen < bno)
1871 * If this failure happens the request to free this
1872 * space was invalid, it's (partly) already free.
1875 if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) {
1876 error = -EFSCORRUPTED;
1882 * Look for a neighboring block on the right (higher block numbers)
1883 * that is contiguous with this space.
1885 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1889 * There is a block to our right.
1891 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1893 if (XFS_IS_CORRUPT(mp, i != 1)) {
1894 error = -EFSCORRUPTED;
1898 * It's not contiguous, though.
1900 if (bno + len < gtbno)
1904 * If this failure happens the request to free this
1905 * space was invalid, it's (partly) already free.
1908 if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) {
1909 error = -EFSCORRUPTED;
1915 * Now allocate and initialize a cursor for the by-size tree.
1917 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_CNT);
1919 * Have both left and right contiguous neighbors.
1920 * Merge all three into a single free block.
1922 if (haveleft && haveright) {
1924 * Delete the old by-size entry on the left.
1926 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1928 if (XFS_IS_CORRUPT(mp, i != 1)) {
1929 error = -EFSCORRUPTED;
1932 if ((error = xfs_btree_delete(cnt_cur, &i)))
1934 if (XFS_IS_CORRUPT(mp, i != 1)) {
1935 error = -EFSCORRUPTED;
1939 * Delete the old by-size entry on the right.
1941 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1943 if (XFS_IS_CORRUPT(mp, i != 1)) {
1944 error = -EFSCORRUPTED;
1947 if ((error = xfs_btree_delete(cnt_cur, &i)))
1949 if (XFS_IS_CORRUPT(mp, i != 1)) {
1950 error = -EFSCORRUPTED;
1954 * Delete the old by-block entry for the right block.
1956 if ((error = xfs_btree_delete(bno_cur, &i)))
1958 if (XFS_IS_CORRUPT(mp, i != 1)) {
1959 error = -EFSCORRUPTED;
1963 * Move the by-block cursor back to the left neighbor.
1965 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1967 if (XFS_IS_CORRUPT(mp, i != 1)) {
1968 error = -EFSCORRUPTED;
1973 * Check that this is the right record: delete didn't
1974 * mangle the cursor.
1977 xfs_agblock_t xxbno;
1980 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1983 if (XFS_IS_CORRUPT(mp,
1987 error = -EFSCORRUPTED;
1993 * Update remaining by-block entry to the new, joined block.
1996 nlen = len + ltlen + gtlen;
1997 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2001 * Have only a left contiguous neighbor.
2002 * Merge it together with the new freespace.
2004 else if (haveleft) {
2006 * Delete the old by-size entry on the left.
2008 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2010 if (XFS_IS_CORRUPT(mp, i != 1)) {
2011 error = -EFSCORRUPTED;
2014 if ((error = xfs_btree_delete(cnt_cur, &i)))
2016 if (XFS_IS_CORRUPT(mp, i != 1)) {
2017 error = -EFSCORRUPTED;
2021 * Back up the by-block cursor to the left neighbor, and
2022 * update its length.
2024 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
2026 if (XFS_IS_CORRUPT(mp, i != 1)) {
2027 error = -EFSCORRUPTED;
2032 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2036 * Have only a right contiguous neighbor.
2037 * Merge it together with the new freespace.
2039 else if (haveright) {
2041 * Delete the old by-size entry on the right.
2043 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2045 if (XFS_IS_CORRUPT(mp, i != 1)) {
2046 error = -EFSCORRUPTED;
2049 if ((error = xfs_btree_delete(cnt_cur, &i)))
2051 if (XFS_IS_CORRUPT(mp, i != 1)) {
2052 error = -EFSCORRUPTED;
2056 * Update the starting block and length of the right
2057 * neighbor in the by-block tree.
2061 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2065 * No contiguous neighbors.
2066 * Insert the new freespace into the by-block tree.
2071 if ((error = xfs_btree_insert(bno_cur, &i)))
2073 if (XFS_IS_CORRUPT(mp, i != 1)) {
2074 error = -EFSCORRUPTED;
2078 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
2081 * In all cases we need to insert the new freespace in the by-size tree.
2083 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
2085 if (XFS_IS_CORRUPT(mp, i != 0)) {
2086 error = -EFSCORRUPTED;
2089 if ((error = xfs_btree_insert(cnt_cur, &i)))
2091 if (XFS_IS_CORRUPT(mp, i != 1)) {
2092 error = -EFSCORRUPTED;
2095 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2099 * Update the freespace totals in the ag and superblock.
2101 error = xfs_alloc_update_counters(tp, agbp, len);
2102 xfs_ag_resv_free_extent(agbp->b_pag, type, tp, len);
2106 XFS_STATS_INC(mp, xs_freex);
2107 XFS_STATS_ADD(mp, xs_freeb, len);
2109 trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
2114 trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
2116 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
2118 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
2123 * Visible (exported) allocation/free functions.
2124 * Some of these are used just by xfs_alloc_btree.c and this file.
2128 * Compute and fill in value of m_alloc_maxlevels.
2131 xfs_alloc_compute_maxlevels(
2132 xfs_mount_t *mp) /* file system mount structure */
2134 mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
2135 (mp->m_sb.sb_agblocks + 1) / 2);
2136 ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
2140 * Find the length of the longest extent in an AG. The 'need' parameter
2141 * specifies how much space we're going to need for the AGFL and the
2142 * 'reserved' parameter tells us how many blocks in this AG are reserved for
2146 xfs_alloc_longest_free_extent(
2147 struct xfs_perag *pag,
2149 xfs_extlen_t reserved)
2151 xfs_extlen_t delta = 0;
2154 * If the AGFL needs a recharge, we'll have to subtract that from the
2157 if (need > pag->pagf_flcount)
2158 delta = need - pag->pagf_flcount;
2161 * If we cannot maintain others' reservations with space from the
2162 * not-longest freesp extents, we'll have to subtract /that/ from
2163 * the longest extent too.
2165 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2166 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2169 * If the longest extent is long enough to satisfy all the
2170 * reservations and AGFL rules in place, we can return this extent.
2172 if (pag->pagf_longest > delta)
2173 return min_t(xfs_extlen_t, pag->pag_mount->m_ag_max_usable,
2174 pag->pagf_longest - delta);
2176 /* Otherwise, let the caller try for 1 block if there's space. */
2177 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2181 * Compute the minimum length of the AGFL in the given AG. If @pag is NULL,
2182 * return the largest possible minimum length.
2185 xfs_alloc_min_freelist(
2186 struct xfs_mount *mp,
2187 struct xfs_perag *pag)
2189 /* AG btrees have at least 1 level. */
2190 static const uint8_t fake_levels[XFS_BTNUM_AGF] = {1, 1, 1};
2191 const uint8_t *levels = pag ? pag->pagf_levels : fake_levels;
2192 unsigned int min_free;
2194 ASSERT(mp->m_alloc_maxlevels > 0);
2196 /* space needed by-bno freespace btree */
2197 min_free = min_t(unsigned int, levels[XFS_BTNUM_BNOi] + 1,
2198 mp->m_alloc_maxlevels);
2199 /* space needed by-size freespace btree */
2200 min_free += min_t(unsigned int, levels[XFS_BTNUM_CNTi] + 1,
2201 mp->m_alloc_maxlevels);
2202 /* space needed reverse mapping used space btree */
2203 if (xfs_has_rmapbt(mp))
2204 min_free += min_t(unsigned int, levels[XFS_BTNUM_RMAPi] + 1,
2205 mp->m_rmap_maxlevels);
2211 * Check if the operation we are fixing up the freelist for should go ahead or
2212 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2213 * is dependent on whether the size and shape of free space available will
2214 * permit the requested allocation to take place.
2217 xfs_alloc_space_available(
2218 struct xfs_alloc_arg *args,
2219 xfs_extlen_t min_free,
2222 struct xfs_perag *pag = args->pag;
2223 xfs_extlen_t alloc_len, longest;
2224 xfs_extlen_t reservation; /* blocks that are still reserved */
2226 xfs_extlen_t agflcount;
2228 if (flags & XFS_ALLOC_FLAG_FREEING)
2231 reservation = xfs_ag_resv_needed(pag, args->resv);
2233 /* do we have enough contiguous free space for the allocation? */
2234 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
2235 longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
2236 if (longest < alloc_len)
2240 * Do we have enough free space remaining for the allocation? Don't
2241 * account extra agfl blocks because we are about to defer free them,
2242 * making them unavailable until the current transaction commits.
2244 agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free);
2245 available = (int)(pag->pagf_freeblks + agflcount -
2246 reservation - min_free - args->minleft);
2247 if (available < (int)max(args->total, alloc_len))
2251 * Clamp maxlen to the amount of free space available for the actual
2252 * extent allocation.
2254 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2255 args->maxlen = available;
2256 ASSERT(args->maxlen > 0);
2257 ASSERT(args->maxlen >= args->minlen);
2264 xfs_free_agfl_block(
2265 struct xfs_trans *tp,
2266 xfs_agnumber_t agno,
2267 xfs_agblock_t agbno,
2268 struct xfs_buf *agbp,
2269 struct xfs_owner_info *oinfo)
2274 error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
2279 error = xfs_trans_get_buf(tp, tp->t_mountp->m_ddev_targp,
2280 XFS_AGB_TO_DADDR(tp->t_mountp, agno, agbno),
2281 tp->t_mountp->m_bsize, 0, &bp);
2284 xfs_trans_binval(tp, bp);
2290 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2291 * is to detect an agfl header padding mismatch between current and early v5
2292 * kernels. This problem manifests as a 1-slot size difference between the
2293 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2294 * may also catch variants of agfl count corruption unrelated to padding. Either
2295 * way, we'll reset the agfl and warn the user.
2297 * Return true if a reset is required before the agfl can be used, false
2301 xfs_agfl_needs_reset(
2302 struct xfs_mount *mp,
2303 struct xfs_agf *agf)
2305 uint32_t f = be32_to_cpu(agf->agf_flfirst);
2306 uint32_t l = be32_to_cpu(agf->agf_fllast);
2307 uint32_t c = be32_to_cpu(agf->agf_flcount);
2308 int agfl_size = xfs_agfl_size(mp);
2311 /* no agfl header on v4 supers */
2312 if (!xfs_has_crc(mp))
2316 * The agf read verifier catches severe corruption of these fields.
2317 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2318 * the verifier allows it.
2320 if (f >= agfl_size || l >= agfl_size)
2326 * Check consistency between the on-disk count and the active range. An
2327 * agfl padding mismatch manifests as an inconsistent flcount.
2332 active = agfl_size - f + l + 1;
2340 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2341 * agfl content cannot be trusted. Warn the user that a repair is required to
2342 * recover leaked blocks.
2344 * The purpose of this mechanism is to handle filesystems affected by the agfl
2345 * header padding mismatch problem. A reset keeps the filesystem online with a
2346 * relatively minor free space accounting inconsistency rather than suffer the
2347 * inevitable crash from use of an invalid agfl block.
2351 struct xfs_trans *tp,
2352 struct xfs_buf *agbp,
2353 struct xfs_perag *pag)
2355 struct xfs_mount *mp = tp->t_mountp;
2356 struct xfs_agf *agf = agbp->b_addr;
2358 ASSERT(xfs_perag_agfl_needs_reset(pag));
2359 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
2362 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2363 "Please unmount and run xfs_repair.",
2364 pag->pag_agno, pag->pagf_flcount);
2366 agf->agf_flfirst = 0;
2367 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
2368 agf->agf_flcount = 0;
2369 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
2372 pag->pagf_flcount = 0;
2373 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
2377 * Defer an AGFL block free. This is effectively equivalent to
2378 * xfs_free_extent_later() with some special handling particular to AGFL blocks.
2380 * Deferring AGFL frees helps prevent log reservation overruns due to too many
2381 * allocation operations in a transaction. AGFL frees are prone to this problem
2382 * because for one they are always freed one at a time. Further, an immediate
2383 * AGFL block free can cause a btree join and require another block free before
2384 * the real allocation can proceed. Deferring the free disconnects freeing up
2385 * the AGFL slot from freeing the block.
2388 xfs_defer_agfl_block(
2389 struct xfs_trans *tp,
2390 xfs_agnumber_t agno,
2391 xfs_fsblock_t agbno,
2392 struct xfs_owner_info *oinfo)
2394 struct xfs_mount *mp = tp->t_mountp;
2395 struct xfs_extent_free_item *xefi;
2397 ASSERT(xfs_extfree_item_cache != NULL);
2398 ASSERT(oinfo != NULL);
2400 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
2401 GFP_KERNEL | __GFP_NOFAIL);
2402 xefi->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
2403 xefi->xefi_blockcount = 1;
2404 xefi->xefi_owner = oinfo->oi_owner;
2406 trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
2408 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &xefi->xefi_list);
2412 * Add the extent to the list of extents to be free at transaction end.
2413 * The list is maintained sorted (by block number).
2416 __xfs_free_extent_later(
2417 struct xfs_trans *tp,
2420 const struct xfs_owner_info *oinfo,
2423 struct xfs_extent_free_item *xefi;
2425 struct xfs_mount *mp = tp->t_mountp;
2426 xfs_agnumber_t agno;
2427 xfs_agblock_t agbno;
2429 ASSERT(bno != NULLFSBLOCK);
2431 ASSERT(len <= XFS_MAX_BMBT_EXTLEN);
2432 ASSERT(!isnullstartblock(bno));
2433 agno = XFS_FSB_TO_AGNO(mp, bno);
2434 agbno = XFS_FSB_TO_AGBNO(mp, bno);
2435 ASSERT(agno < mp->m_sb.sb_agcount);
2436 ASSERT(agbno < mp->m_sb.sb_agblocks);
2437 ASSERT(len < mp->m_sb.sb_agblocks);
2438 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
2440 ASSERT(xfs_extfree_item_cache != NULL);
2442 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
2443 GFP_KERNEL | __GFP_NOFAIL);
2444 xefi->xefi_startblock = bno;
2445 xefi->xefi_blockcount = (xfs_extlen_t)len;
2447 xefi->xefi_flags |= XFS_EFI_SKIP_DISCARD;
2449 ASSERT(oinfo->oi_offset == 0);
2451 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
2452 xefi->xefi_flags |= XFS_EFI_ATTR_FORK;
2453 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
2454 xefi->xefi_flags |= XFS_EFI_BMBT_BLOCK;
2455 xefi->xefi_owner = oinfo->oi_owner;
2457 xefi->xefi_owner = XFS_RMAP_OWN_NULL;
2459 trace_xfs_bmap_free_defer(tp->t_mountp,
2460 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
2461 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
2462 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &xefi->xefi_list);
2467 * Check if an AGF has a free extent record whose length is equal to
2471 xfs_exact_minlen_extent_available(
2472 struct xfs_alloc_arg *args,
2473 struct xfs_buf *agbp,
2476 struct xfs_btree_cur *cnt_cur;
2481 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, agbp,
2482 args->pag, XFS_BTNUM_CNT);
2483 error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat);
2488 error = -EFSCORRUPTED;
2492 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat);
2496 if (*stat == 1 && flen != args->minlen)
2500 xfs_btree_del_cursor(cnt_cur, error);
2507 * Decide whether to use this allocation group for this allocation.
2508 * If so, fix up the btree freelist's size.
2511 xfs_alloc_fix_freelist(
2512 struct xfs_alloc_arg *args, /* allocation argument structure */
2513 int flags) /* XFS_ALLOC_FLAG_... */
2515 struct xfs_mount *mp = args->mp;
2516 struct xfs_perag *pag = args->pag;
2517 struct xfs_trans *tp = args->tp;
2518 struct xfs_buf *agbp = NULL;
2519 struct xfs_buf *agflbp = NULL;
2520 struct xfs_alloc_arg targs; /* local allocation arguments */
2521 xfs_agblock_t bno; /* freelist block */
2522 xfs_extlen_t need; /* total blocks needed in freelist */
2525 /* deferred ops (AGFL block frees) require permanent transactions */
2526 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
2528 if (!xfs_perag_initialised_agf(pag)) {
2529 error = xfs_alloc_read_agf(pag, tp, flags, &agbp);
2531 /* Couldn't lock the AGF so skip this AG. */
2532 if (error == -EAGAIN)
2539 * If this is a metadata preferred pag and we are user data then try
2540 * somewhere else if we are not being asked to try harder at this
2543 if (xfs_perag_prefers_metadata(pag) &&
2544 (args->datatype & XFS_ALLOC_USERDATA) &&
2545 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2546 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2547 goto out_agbp_relse;
2550 need = xfs_alloc_min_freelist(mp, pag);
2551 if (!xfs_alloc_space_available(args, need, flags |
2552 XFS_ALLOC_FLAG_CHECK))
2553 goto out_agbp_relse;
2556 * Get the a.g. freespace buffer.
2557 * Can fail if we're not blocking on locks, and it's held.
2560 error = xfs_alloc_read_agf(pag, tp, flags, &agbp);
2562 /* Couldn't lock the AGF so skip this AG. */
2563 if (error == -EAGAIN)
2569 /* reset a padding mismatched agfl before final free space check */
2570 if (xfs_perag_agfl_needs_reset(pag))
2571 xfs_agfl_reset(tp, agbp, pag);
2573 /* If there isn't enough total space or single-extent, reject it. */
2574 need = xfs_alloc_min_freelist(mp, pag);
2575 if (!xfs_alloc_space_available(args, need, flags))
2576 goto out_agbp_relse;
2579 if (args->alloc_minlen_only) {
2582 error = xfs_exact_minlen_extent_available(args, agbp, &stat);
2584 goto out_agbp_relse;
2588 * Make the freelist shorter if it's too long.
2590 * Note that from this point onwards, we will always release the agf and
2591 * agfl buffers on error. This handles the case where we error out and
2592 * the buffers are clean or may not have been joined to the transaction
2593 * and hence need to be released manually. If they have been joined to
2594 * the transaction, then xfs_trans_brelse() will handle them
2595 * appropriately based on the recursion count and dirty state of the
2598 * XXX (dgc): When we have lots of free space, does this buy us
2599 * anything other than extra overhead when we need to put more blocks
2600 * back on the free list? Maybe we should only do this when space is
2601 * getting low or the AGFL is more than half full?
2603 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2604 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2605 * updating the rmapbt. Both flags are used in xfs_repair while we're
2606 * rebuilding the rmapbt, and neither are used by the kernel. They're
2607 * both required to ensure that rmaps are correctly recorded for the
2608 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2609 * repair/rmap.c in xfsprogs for details.
2611 memset(&targs, 0, sizeof(targs));
2612 /* struct copy below */
2613 if (flags & XFS_ALLOC_FLAG_NORMAP)
2614 targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
2616 targs.oinfo = XFS_RMAP_OINFO_AG;
2617 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
2618 error = xfs_alloc_get_freelist(pag, tp, agbp, &bno, 0);
2620 goto out_agbp_relse;
2622 /* defer agfl frees */
2623 xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
2629 targs.agno = args->agno;
2630 targs.alignment = targs.minlen = targs.prod = 1;
2632 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2634 goto out_agbp_relse;
2636 /* Make the freelist longer if it's too short. */
2637 while (pag->pagf_flcount < need) {
2639 targs.maxlen = need - pag->pagf_flcount;
2640 targs.resv = XFS_AG_RESV_AGFL;
2642 /* Allocate as many blocks as possible at once. */
2643 error = xfs_alloc_ag_vextent_size(&targs);
2645 goto out_agflbp_relse;
2648 * Stop if we run out. Won't happen if callers are obeying
2649 * the restrictions correctly. Can happen for free calls
2650 * on a completely full ag.
2652 if (targs.agbno == NULLAGBLOCK) {
2653 if (flags & XFS_ALLOC_FLAG_FREEING)
2655 goto out_agflbp_relse;
2658 if (!xfs_rmap_should_skip_owner_update(&targs.oinfo)) {
2659 error = xfs_rmap_alloc(tp, agbp, pag,
2660 targs.agbno, targs.len, &targs.oinfo);
2662 goto out_agflbp_relse;
2664 error = xfs_alloc_update_counters(tp, agbp,
2665 -((long)(targs.len)));
2667 goto out_agflbp_relse;
2670 * Put each allocated block on the list.
2672 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2673 error = xfs_alloc_put_freelist(pag, tp, agbp,
2676 goto out_agflbp_relse;
2679 xfs_trans_brelse(tp, agflbp);
2684 xfs_trans_brelse(tp, agflbp);
2687 xfs_trans_brelse(tp, agbp);
2694 * Get a block from the freelist.
2695 * Returns with the buffer for the block gotten.
2698 xfs_alloc_get_freelist(
2699 struct xfs_perag *pag,
2700 struct xfs_trans *tp,
2701 struct xfs_buf *agbp,
2702 xfs_agblock_t *bnop,
2705 struct xfs_agf *agf = agbp->b_addr;
2706 struct xfs_buf *agflbp;
2711 struct xfs_mount *mp = tp->t_mountp;
2714 * Freelist is empty, give up.
2716 if (!agf->agf_flcount) {
2717 *bnop = NULLAGBLOCK;
2721 * Read the array of free blocks.
2723 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2729 * Get the block number and update the data structures.
2731 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
2732 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2733 be32_add_cpu(&agf->agf_flfirst, 1);
2734 xfs_trans_brelse(tp, agflbp);
2735 if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
2736 agf->agf_flfirst = 0;
2738 ASSERT(!xfs_perag_agfl_needs_reset(pag));
2739 be32_add_cpu(&agf->agf_flcount, -1);
2740 pag->pagf_flcount--;
2742 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2744 be32_add_cpu(&agf->agf_btreeblks, 1);
2745 pag->pagf_btreeblks++;
2746 logflags |= XFS_AGF_BTREEBLKS;
2749 xfs_alloc_log_agf(tp, agbp, logflags);
2756 * Log the given fields from the agf structure.
2760 struct xfs_trans *tp,
2764 int first; /* first byte offset */
2765 int last; /* last byte offset */
2766 static const short offsets[] = {
2767 offsetof(xfs_agf_t, agf_magicnum),
2768 offsetof(xfs_agf_t, agf_versionnum),
2769 offsetof(xfs_agf_t, agf_seqno),
2770 offsetof(xfs_agf_t, agf_length),
2771 offsetof(xfs_agf_t, agf_roots[0]),
2772 offsetof(xfs_agf_t, agf_levels[0]),
2773 offsetof(xfs_agf_t, agf_flfirst),
2774 offsetof(xfs_agf_t, agf_fllast),
2775 offsetof(xfs_agf_t, agf_flcount),
2776 offsetof(xfs_agf_t, agf_freeblks),
2777 offsetof(xfs_agf_t, agf_longest),
2778 offsetof(xfs_agf_t, agf_btreeblks),
2779 offsetof(xfs_agf_t, agf_uuid),
2780 offsetof(xfs_agf_t, agf_rmap_blocks),
2781 offsetof(xfs_agf_t, agf_refcount_blocks),
2782 offsetof(xfs_agf_t, agf_refcount_root),
2783 offsetof(xfs_agf_t, agf_refcount_level),
2784 /* needed so that we don't log the whole rest of the structure: */
2785 offsetof(xfs_agf_t, agf_spare64),
2789 trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_);
2791 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2793 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2794 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2798 * Put the block on the freelist for the allocation group.
2801 xfs_alloc_put_freelist(
2802 struct xfs_perag *pag,
2803 struct xfs_trans *tp,
2804 struct xfs_buf *agbp,
2805 struct xfs_buf *agflbp,
2809 struct xfs_mount *mp = tp->t_mountp;
2810 struct xfs_agf *agf = agbp->b_addr;
2818 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2823 be32_add_cpu(&agf->agf_fllast, 1);
2824 if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
2825 agf->agf_fllast = 0;
2827 ASSERT(!xfs_perag_agfl_needs_reset(pag));
2828 be32_add_cpu(&agf->agf_flcount, 1);
2829 pag->pagf_flcount++;
2831 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2833 be32_add_cpu(&agf->agf_btreeblks, -1);
2834 pag->pagf_btreeblks--;
2835 logflags |= XFS_AGF_BTREEBLKS;
2838 xfs_alloc_log_agf(tp, agbp, logflags);
2840 ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
2842 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
2843 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2844 *blockp = cpu_to_be32(bno);
2845 startoff = (char *)blockp - (char *)agflbp->b_addr;
2847 xfs_alloc_log_agf(tp, agbp, logflags);
2849 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2850 xfs_trans_log_buf(tp, agflbp, startoff,
2851 startoff + sizeof(xfs_agblock_t) - 1);
2855 static xfs_failaddr_t
2859 struct xfs_mount *mp = bp->b_mount;
2860 struct xfs_agf *agf = bp->b_addr;
2862 if (xfs_has_crc(mp)) {
2863 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
2864 return __this_address;
2865 if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn)))
2866 return __this_address;
2869 if (!xfs_verify_magic(bp, agf->agf_magicnum))
2870 return __this_address;
2872 if (!(XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2873 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2874 be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
2875 be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
2876 be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
2877 return __this_address;
2879 if (be32_to_cpu(agf->agf_length) > mp->m_sb.sb_dblocks)
2880 return __this_address;
2882 if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
2883 be32_to_cpu(agf->agf_freeblks) > be32_to_cpu(agf->agf_length))
2884 return __this_address;
2886 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2887 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2888 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) >
2889 mp->m_alloc_maxlevels ||
2890 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) >
2891 mp->m_alloc_maxlevels)
2892 return __this_address;
2894 if (xfs_has_rmapbt(mp) &&
2895 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2896 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) >
2897 mp->m_rmap_maxlevels))
2898 return __this_address;
2900 if (xfs_has_rmapbt(mp) &&
2901 be32_to_cpu(agf->agf_rmap_blocks) > be32_to_cpu(agf->agf_length))
2902 return __this_address;
2905 * during growfs operations, the perag is not fully initialised,
2906 * so we can't use it for any useful checking. growfs ensures we can't
2907 * use it by using uncached buffers that don't have the perag attached
2908 * so we can detect and avoid this problem.
2910 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2911 return __this_address;
2913 if (xfs_has_lazysbcount(mp) &&
2914 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2915 return __this_address;
2917 if (xfs_has_reflink(mp) &&
2918 be32_to_cpu(agf->agf_refcount_blocks) >
2919 be32_to_cpu(agf->agf_length))
2920 return __this_address;
2922 if (xfs_has_reflink(mp) &&
2923 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2924 be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels))
2925 return __this_address;
2932 xfs_agf_read_verify(
2935 struct xfs_mount *mp = bp->b_mount;
2938 if (xfs_has_crc(mp) &&
2939 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
2940 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2942 fa = xfs_agf_verify(bp);
2943 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
2944 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2949 xfs_agf_write_verify(
2952 struct xfs_mount *mp = bp->b_mount;
2953 struct xfs_buf_log_item *bip = bp->b_log_item;
2954 struct xfs_agf *agf = bp->b_addr;
2957 fa = xfs_agf_verify(bp);
2959 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2963 if (!xfs_has_crc(mp))
2967 agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2969 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
2972 const struct xfs_buf_ops xfs_agf_buf_ops = {
2974 .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) },
2975 .verify_read = xfs_agf_read_verify,
2976 .verify_write = xfs_agf_write_verify,
2977 .verify_struct = xfs_agf_verify,
2981 * Read in the allocation group header (free/alloc section).
2985 struct xfs_perag *pag,
2986 struct xfs_trans *tp,
2988 struct xfs_buf **agfbpp)
2990 struct xfs_mount *mp = pag->pag_mount;
2993 trace_xfs_read_agf(pag->pag_mount, pag->pag_agno);
2995 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
2996 XFS_AG_DADDR(mp, pag->pag_agno, XFS_AGF_DADDR(mp)),
2997 XFS_FSS_TO_BB(mp, 1), flags, agfbpp, &xfs_agf_buf_ops);
3001 xfs_buf_set_ref(*agfbpp, XFS_AGF_REF);
3006 * Read in the allocation group header (free/alloc section) and initialise the
3007 * perag structure if necessary. If the caller provides @agfbpp, then return the
3008 * locked buffer to the caller, otherwise free it.
3012 struct xfs_perag *pag,
3013 struct xfs_trans *tp,
3015 struct xfs_buf **agfbpp)
3017 struct xfs_buf *agfbp;
3018 struct xfs_agf *agf;
3022 trace_xfs_alloc_read_agf(pag->pag_mount, pag->pag_agno);
3024 /* We don't support trylock when freeing. */
3025 ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) !=
3026 (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK));
3027 error = xfs_read_agf(pag, tp,
3028 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
3033 agf = agfbp->b_addr;
3034 if (!xfs_perag_initialised_agf(pag)) {
3035 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
3036 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
3037 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
3038 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
3039 pag->pagf_levels[XFS_BTNUM_BNOi] =
3040 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
3041 pag->pagf_levels[XFS_BTNUM_CNTi] =
3042 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
3043 pag->pagf_levels[XFS_BTNUM_RMAPi] =
3044 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
3045 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
3046 if (xfs_agfl_needs_reset(pag->pag_mount, agf))
3047 set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
3050 * Update the in-core allocbt counter. Filter out the rmapbt
3051 * subset of the btreeblks counter because the rmapbt is managed
3052 * by perag reservation. Subtract one for the rmapbt root block
3053 * because the rmap counter includes it while the btreeblks
3054 * counter only tracks non-root blocks.
3056 allocbt_blks = pag->pagf_btreeblks;
3057 if (xfs_has_rmapbt(pag->pag_mount))
3058 allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1;
3059 if (allocbt_blks > 0)
3060 atomic64_add(allocbt_blks,
3061 &pag->pag_mount->m_allocbt_blks);
3063 set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
3066 else if (!xfs_is_shutdown(pag->pag_mount)) {
3067 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
3068 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
3069 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
3070 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
3071 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
3072 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
3073 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
3074 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
3080 xfs_trans_brelse(tp, agfbp);
3085 * Pre-proces allocation arguments to set initial state that we don't require
3086 * callers to set up correctly, as well as bounds check the allocation args
3090 xfs_alloc_vextent_check_args(
3091 struct xfs_alloc_arg *args,
3092 xfs_fsblock_t target,
3093 xfs_agnumber_t *minimum_agno)
3095 struct xfs_mount *mp = args->mp;
3096 xfs_agblock_t agsize;
3098 args->fsbno = NULLFSBLOCK;
3101 if (args->tp->t_highest_agno != NULLAGNUMBER)
3102 *minimum_agno = args->tp->t_highest_agno;
3105 * Just fix this up, for the case where the last a.g. is shorter
3106 * (or there's only one a.g.) and the caller couldn't easily figure
3107 * that out (xfs_bmap_alloc).
3109 agsize = mp->m_sb.sb_agblocks;
3110 if (args->maxlen > agsize)
3111 args->maxlen = agsize;
3112 if (args->alignment == 0)
3113 args->alignment = 1;
3115 ASSERT(args->minlen > 0);
3116 ASSERT(args->maxlen > 0);
3117 ASSERT(args->alignment > 0);
3118 ASSERT(args->resv != XFS_AG_RESV_AGFL);
3120 ASSERT(XFS_FSB_TO_AGNO(mp, target) < mp->m_sb.sb_agcount);
3121 ASSERT(XFS_FSB_TO_AGBNO(mp, target) < agsize);
3122 ASSERT(args->minlen <= args->maxlen);
3123 ASSERT(args->minlen <= agsize);
3124 ASSERT(args->mod < args->prod);
3126 if (XFS_FSB_TO_AGNO(mp, target) >= mp->m_sb.sb_agcount ||
3127 XFS_FSB_TO_AGBNO(mp, target) >= agsize ||
3128 args->minlen > args->maxlen || args->minlen > agsize ||
3129 args->mod >= args->prod) {
3130 trace_xfs_alloc_vextent_badargs(args);
3134 if (args->agno != NULLAGNUMBER && *minimum_agno > args->agno) {
3135 trace_xfs_alloc_vextent_skip_deadlock(args);
3143 * Prepare an AG for allocation. If the AG is not prepared to accept the
3144 * allocation, return failure.
3146 * XXX(dgc): The complexity of "need_pag" will go away as all caller paths are
3147 * modified to hold their own perag references.
3150 xfs_alloc_vextent_prepare_ag(
3151 struct xfs_alloc_arg *args)
3153 bool need_pag = !args->pag;
3157 args->pag = xfs_perag_get(args->mp, args->agno);
3159 error = xfs_alloc_fix_freelist(args, 0);
3161 trace_xfs_alloc_vextent_nofix(args);
3163 xfs_perag_put(args->pag);
3164 args->agbno = NULLAGBLOCK;
3168 /* cannot allocate in this AG at all */
3169 trace_xfs_alloc_vextent_noagbp(args);
3170 args->agbno = NULLAGBLOCK;
3173 args->wasfromfl = 0;
3178 * Post-process allocation results to account for the allocation if it succeed
3179 * and set the allocated block number correctly for the caller.
3181 * XXX: we should really be returning ENOSPC for ENOSPC, not
3182 * hiding it behind a "successful" NULLFSBLOCK allocation.
3185 xfs_alloc_vextent_finish(
3186 struct xfs_alloc_arg *args,
3187 xfs_agnumber_t minimum_agno,
3191 struct xfs_mount *mp = args->mp;
3195 * We can end up here with a locked AGF. If we failed, the caller is
3196 * likely going to try to allocate again with different parameters, and
3197 * that can widen the AGs that are searched for free space. If we have
3198 * to do BMBT block allocation, we have to do a new allocation.
3200 * Hence leaving this function with the AGF locked opens up potential
3201 * ABBA AGF deadlocks because a future allocation attempt in this
3202 * transaction may attempt to lock a lower number AGF.
3204 * We can't release the AGF until the transaction is commited, so at
3205 * this point we must update the "first allocation" tracker to point at
3206 * this AG if the tracker is empty or points to a lower AG. This allows
3207 * the next allocation attempt to be modified appropriately to avoid
3211 (args->tp->t_highest_agno == NULLAGNUMBER ||
3212 args->agno > minimum_agno))
3213 args->tp->t_highest_agno = args->agno;
3216 * If the allocation failed with an error or we had an ENOSPC result,
3217 * preserve the returned error whilst also marking the allocation result
3218 * as "no extent allocated". This ensures that callers that fail to
3219 * capture the error will still treat it as a failed allocation.
3221 if (alloc_error || args->agbno == NULLAGBLOCK) {
3222 args->fsbno = NULLFSBLOCK;
3223 error = alloc_error;
3224 goto out_drop_perag;
3227 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
3229 ASSERT(args->len >= args->minlen);
3230 ASSERT(args->len <= args->maxlen);
3231 ASSERT(args->agbno % args->alignment == 0);
3232 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno), args->len);
3234 /* if not file data, insert new block into the reverse map btree */
3235 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
3236 error = xfs_rmap_alloc(args->tp, args->agbp, args->pag,
3237 args->agbno, args->len, &args->oinfo);
3239 goto out_drop_perag;
3242 if (!args->wasfromfl) {
3243 error = xfs_alloc_update_counters(args->tp, args->agbp,
3244 -((long)(args->len)));
3246 goto out_drop_perag;
3248 ASSERT(!xfs_extent_busy_search(mp, args->pag, args->agbno,
3252 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
3254 XFS_STATS_INC(mp, xs_allocx);
3255 XFS_STATS_ADD(mp, xs_allocb, args->len);
3259 xfs_perag_put(args->pag);
3266 * Allocate within a single AG only. This uses a best-fit length algorithm so if
3267 * you need an exact sized allocation without locality constraints, this is the
3268 * fastest way to do it.
3270 * Caller is expected to hold a perag reference in args->pag.
3273 xfs_alloc_vextent_this_ag(
3274 struct xfs_alloc_arg *args,
3275 xfs_agnumber_t agno)
3277 struct xfs_mount *mp = args->mp;
3278 xfs_agnumber_t minimum_agno;
3283 error = xfs_alloc_vextent_check_args(args, XFS_AGB_TO_FSB(mp, agno, 0),
3286 if (error == -ENOSPC)
3291 error = xfs_alloc_vextent_prepare_ag(args);
3292 if (!error && args->agbp)
3293 error = xfs_alloc_ag_vextent_size(args);
3295 return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
3299 * Iterate all AGs trying to allocate an extent starting from @start_ag.
3301 * If the incoming allocation type is XFS_ALLOCTYPE_NEAR_BNO, it means the
3302 * allocation attempts in @start_agno have locality information. If we fail to
3303 * allocate in that AG, then we revert to anywhere-in-AG for all the other AGs
3304 * we attempt to allocation in as there is no locality optimisation possible for
3305 * those allocations.
3307 * When we wrap the AG iteration at the end of the filesystem, we have to be
3308 * careful not to wrap into AGs below ones we already have locked in the
3309 * transaction if we are doing a blocking iteration. This will result in an
3310 * out-of-order locking of AGFs and hence can cause deadlocks.
3313 xfs_alloc_vextent_iterate_ags(
3314 struct xfs_alloc_arg *args,
3315 xfs_agnumber_t minimum_agno,
3316 xfs_agnumber_t start_agno,
3317 xfs_agblock_t target_agbno,
3320 struct xfs_mount *mp = args->mp;
3323 ASSERT(start_agno >= minimum_agno);
3326 * Loop over allocation groups twice; first time with
3327 * trylock set, second time without.
3329 args->agno = start_agno;
3331 args->pag = xfs_perag_get(mp, args->agno);
3332 error = xfs_alloc_vextent_prepare_ag(args);
3338 * Allocation is supposed to succeed now, so break out
3339 * of the loop regardless of whether we succeed or not.
3341 if (args->agno == start_agno && target_agbno) {
3342 args->agbno = target_agbno;
3343 error = xfs_alloc_ag_vextent_near(args);
3346 error = xfs_alloc_ag_vextent_size(args);
3351 trace_xfs_alloc_vextent_loopfailed(args);
3354 * If we are try-locking, we can't deadlock on AGF locks so we
3355 * can wrap all the way back to the first AG. Otherwise, wrap
3356 * back to the start AG so we can't deadlock and let the end of
3357 * scan handler decide what to do next.
3359 if (++(args->agno) == mp->m_sb.sb_agcount) {
3360 if (flags & XFS_ALLOC_FLAG_TRYLOCK)
3363 args->agno = minimum_agno;
3367 * Reached the starting a.g., must either be done
3368 * or switch to non-trylock mode.
3370 if (args->agno == start_agno) {
3372 args->agbno = NULLAGBLOCK;
3373 trace_xfs_alloc_vextent_allfailed(args);
3376 args->agbno = target_agbno;
3379 xfs_perag_put(args->pag);
3383 * The perag is left referenced in args for the caller to clean
3384 * up after they've finished the allocation.
3390 * Iterate from the AGs from the start AG to the end of the filesystem, trying
3391 * to allocate blocks. It starts with a near allocation attempt in the initial
3392 * AG, then falls back to anywhere-in-ag after the first AG fails. It will wrap
3393 * back to zero if allowed by previous allocations in this transaction,
3394 * otherwise will wrap back to the start AG and run a second blocking pass to
3395 * the end of the filesystem.
3398 xfs_alloc_vextent_start_ag(
3399 struct xfs_alloc_arg *args,
3400 xfs_fsblock_t target)
3402 struct xfs_mount *mp = args->mp;
3403 xfs_agnumber_t minimum_agno;
3404 xfs_agnumber_t start_agno;
3405 xfs_agnumber_t rotorstep = xfs_rotorstep;
3406 bool bump_rotor = false;
3409 args->agno = NULLAGNUMBER;
3410 args->agbno = NULLAGBLOCK;
3411 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3413 if (error == -ENOSPC)
3418 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
3419 xfs_is_inode32(mp)) {
3420 target = XFS_AGB_TO_FSB(mp,
3421 ((mp->m_agfrotor / rotorstep) %
3422 mp->m_sb.sb_agcount), 0);
3426 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
3427 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
3428 XFS_FSB_TO_AGBNO(mp, target), XFS_ALLOC_FLAG_TRYLOCK);
3431 if (args->agno == start_agno)
3432 mp->m_agfrotor = (mp->m_agfrotor + 1) %
3433 (mp->m_sb.sb_agcount * rotorstep);
3435 mp->m_agfrotor = (args->agno * rotorstep + 1) %
3436 (mp->m_sb.sb_agcount * rotorstep);
3439 return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
3443 * Iterate from the agno indicated via @target through to the end of the
3444 * filesystem attempting blocking allocation. This does not wrap or try a second
3445 * pass, so will not recurse into AGs lower than indicated by the target.
3448 xfs_alloc_vextent_first_ag(
3449 struct xfs_alloc_arg *args,
3450 xfs_fsblock_t target)
3452 struct xfs_mount *mp = args->mp;
3453 xfs_agnumber_t minimum_agno;
3454 xfs_agnumber_t start_agno;
3457 args->agno = NULLAGNUMBER;
3458 args->agbno = NULLAGBLOCK;
3459 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3461 if (error == -ENOSPC)
3466 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
3467 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
3468 XFS_FSB_TO_AGBNO(mp, target), 0);
3469 return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
3473 * Allocate at the exact block target or fail. Caller is expected to hold a
3474 * perag reference in args->pag.
3477 xfs_alloc_vextent_exact_bno(
3478 struct xfs_alloc_arg *args,
3479 xfs_fsblock_t target)
3481 struct xfs_mount *mp = args->mp;
3482 xfs_agnumber_t minimum_agno;
3485 args->agno = XFS_FSB_TO_AGNO(mp, target);
3486 args->agbno = XFS_FSB_TO_AGBNO(mp, target);
3487 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3489 if (error == -ENOSPC)
3494 error = xfs_alloc_vextent_prepare_ag(args);
3495 if (!error && args->agbp)
3496 error = xfs_alloc_ag_vextent_exact(args);
3498 return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
3502 * Allocate an extent as close to the target as possible. If there are not
3503 * viable candidates in the AG, then fail the allocation.
3505 * Caller may or may not have a per-ag reference in args->pag.
3508 xfs_alloc_vextent_near_bno(
3509 struct xfs_alloc_arg *args,
3510 xfs_fsblock_t target)
3512 struct xfs_mount *mp = args->mp;
3513 xfs_agnumber_t minimum_agno;
3514 bool needs_perag = args->pag == NULL;
3517 args->agno = XFS_FSB_TO_AGNO(mp, target);
3518 args->agbno = XFS_FSB_TO_AGBNO(mp, target);
3519 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3521 if (error == -ENOSPC)
3527 args->pag = xfs_perag_get(mp, args->agno);
3529 error = xfs_alloc_vextent_prepare_ag(args);
3530 if (!error && args->agbp)
3531 error = xfs_alloc_ag_vextent_near(args);
3533 return xfs_alloc_vextent_finish(args, minimum_agno, error, needs_perag);
3536 /* Ensure that the freelist is at full capacity. */
3538 xfs_free_extent_fix_freelist(
3539 struct xfs_trans *tp,
3540 struct xfs_perag *pag,
3541 struct xfs_buf **agbp)
3543 struct xfs_alloc_arg args;
3546 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3548 args.mp = tp->t_mountp;
3549 args.agno = pag->pag_agno;
3553 * validate that the block number is legal - the enables us to detect
3554 * and handle a silent filesystem corruption rather than crashing.
3556 if (args.agno >= args.mp->m_sb.sb_agcount)
3557 return -EFSCORRUPTED;
3559 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3569 * Just break up the extent address and hand off to xfs_free_ag_extent
3570 * after fixing up the freelist.
3574 struct xfs_trans *tp,
3577 const struct xfs_owner_info *oinfo,
3578 enum xfs_ag_resv_type type,
3581 struct xfs_mount *mp = tp->t_mountp;
3582 struct xfs_buf *agbp;
3583 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
3584 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
3585 struct xfs_agf *agf;
3587 unsigned int busy_flags = 0;
3588 struct xfs_perag *pag;
3591 ASSERT(type != XFS_AG_RESV_AGFL);
3593 if (XFS_TEST_ERROR(false, mp,
3594 XFS_ERRTAG_FREE_EXTENT))
3597 pag = xfs_perag_get(mp, agno);
3598 error = xfs_free_extent_fix_freelist(tp, pag, &agbp);
3603 if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) {
3604 error = -EFSCORRUPTED;
3608 /* validate the extent size is legal now we have the agf locked */
3609 if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) {
3610 error = -EFSCORRUPTED;
3614 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
3619 busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
3620 xfs_extent_busy_insert(tp, pag, agbno, len, busy_flags);
3625 xfs_trans_brelse(tp, agbp);
3631 struct xfs_alloc_query_range_info {
3632 xfs_alloc_query_range_fn fn;
3636 /* Format btree record and pass to our callback. */
3638 xfs_alloc_query_range_helper(
3639 struct xfs_btree_cur *cur,
3640 const union xfs_btree_rec *rec,
3643 struct xfs_alloc_query_range_info *query = priv;
3644 struct xfs_alloc_rec_incore irec;
3646 irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
3647 irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
3648 return query->fn(cur, &irec, query->priv);
3651 /* Find all free space within a given range of blocks. */
3653 xfs_alloc_query_range(
3654 struct xfs_btree_cur *cur,
3655 const struct xfs_alloc_rec_incore *low_rec,
3656 const struct xfs_alloc_rec_incore *high_rec,
3657 xfs_alloc_query_range_fn fn,
3660 union xfs_btree_irec low_brec;
3661 union xfs_btree_irec high_brec;
3662 struct xfs_alloc_query_range_info query;
3664 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3665 low_brec.a = *low_rec;
3666 high_brec.a = *high_rec;
3669 return xfs_btree_query_range(cur, &low_brec, &high_brec,
3670 xfs_alloc_query_range_helper, &query);
3673 /* Find all free space records. */
3675 xfs_alloc_query_all(
3676 struct xfs_btree_cur *cur,
3677 xfs_alloc_query_range_fn fn,
3680 struct xfs_alloc_query_range_info query;
3682 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3685 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
3688 /* Is there a record covering a given extent? */
3690 xfs_alloc_has_record(
3691 struct xfs_btree_cur *cur,
3696 union xfs_btree_irec low;
3697 union xfs_btree_irec high;
3699 memset(&low, 0, sizeof(low));
3700 low.a.ar_startblock = bno;
3701 memset(&high, 0xFF, sizeof(high));
3702 high.a.ar_startblock = bno + len - 1;
3704 return xfs_btree_has_record(cur, &low, &high, exists);
3708 * Walk all the blocks in the AGFL. The @walk_fn can return any negative
3709 * error code or XFS_ITER_*.
3713 struct xfs_mount *mp,
3714 struct xfs_agf *agf,
3715 struct xfs_buf *agflbp,
3716 xfs_agfl_walk_fn walk_fn,
3723 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
3724 i = be32_to_cpu(agf->agf_flfirst);
3726 /* Nothing to walk in an empty AGFL. */
3727 if (agf->agf_flcount == cpu_to_be32(0))
3730 /* Otherwise, walk from first to last, wrapping as needed. */
3732 error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
3735 if (i == be32_to_cpu(agf->agf_fllast))
3737 if (++i == xfs_agfl_size(mp))
3745 xfs_extfree_intent_init_cache(void)
3747 xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent",
3748 sizeof(struct xfs_extent_free_item),
3751 return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM;
3755 xfs_extfree_intent_destroy_cache(void)
3757 kmem_cache_destroy(xfs_extfree_item_cache);
3758 xfs_extfree_item_cache = NULL;