2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_extent_busy.h"
34 #include "xfs_error.h"
35 #include "xfs_cksum.h"
36 #include "xfs_trace.h"
37 #include "xfs_trans.h"
38 #include "xfs_buf_item.h"
40 #include "xfs_ag_resv.h"
42 struct workqueue_struct
*xfs_alloc_wq
;
44 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
46 #define XFSA_FIXUP_BNO_OK 1
47 #define XFSA_FIXUP_CNT_OK 2
49 STATIC
int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t
*);
50 STATIC
int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t
*);
51 STATIC
int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t
*);
52 STATIC
int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t
*,
53 xfs_btree_cur_t
*, xfs_agblock_t
*, xfs_extlen_t
*, int *);
56 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
57 * the beginning of the block for a proper header with the location information
64 unsigned int size
= mp
->m_sb
.sb_sectsize
;
66 if (xfs_sb_version_hascrc(&mp
->m_sb
))
67 size
-= sizeof(struct xfs_agfl
);
69 return size
/ sizeof(xfs_agblock_t
);
76 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
77 return XFS_RMAP_BLOCK(mp
) + 1;
78 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
79 return XFS_FIBT_BLOCK(mp
) + 1;
80 return XFS_IBT_BLOCK(mp
) + 1;
87 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
88 return xfs_refc_block(mp
) + 1;
89 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
90 return XFS_RMAP_BLOCK(mp
) + 1;
91 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
92 return XFS_FIBT_BLOCK(mp
) + 1;
93 return XFS_IBT_BLOCK(mp
) + 1;
97 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
98 * AGF buffer (PV 947395), we place constraints on the relationship among
99 * actual allocations for data blocks, freelist blocks, and potential file data
100 * bmap btree blocks. However, these restrictions may result in no actual space
101 * allocated for a delayed extent, for example, a data block in a certain AG is
102 * allocated but there is no additional block for the additional bmap btree
103 * block due to a split of the bmap btree of the file. The result of this may
104 * lead to an infinite loop when the file gets flushed to disk and all delayed
105 * extents need to be actually allocated. To get around this, we explicitly set
106 * aside a few blocks which will not be reserved in delayed allocation.
108 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
109 * potential split of the file's bmap btree.
113 struct xfs_mount
*mp
)
115 return mp
->m_sb
.sb_agcount
* (XFS_ALLOC_AGFL_RESERVE
+ 4);
119 * When deciding how much space to allocate out of an AG, we limit the
120 * allocation maximum size to the size the AG. However, we cannot use all the
121 * blocks in the AG - some are permanently used by metadata. These
122 * blocks are generally:
123 * - the AG superblock, AGF, AGI and AGFL
124 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
125 * the AGI free inode and rmap btree root blocks.
126 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
127 * - the rmapbt root block
129 * The AG headers are sector sized, so the amount of space they take up is
130 * dependent on filesystem geometry. The others are all single blocks.
133 xfs_alloc_ag_max_usable(
134 struct xfs_mount
*mp
)
138 blocks
= XFS_BB_TO_FSB(mp
, XFS_FSS_TO_BB(mp
, 4)); /* ag headers */
139 blocks
+= XFS_ALLOC_AGFL_RESERVE
;
140 blocks
+= 3; /* AGF, AGI btree root blocks */
141 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
142 blocks
++; /* finobt root block */
143 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
144 blocks
++; /* rmap root block */
145 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
146 blocks
++; /* refcount root block */
148 return mp
->m_sb
.sb_agblocks
- blocks
;
152 * Lookup the record equal to [bno, len] in the btree given by cur.
154 STATIC
int /* error */
156 struct xfs_btree_cur
*cur
, /* btree cursor */
157 xfs_agblock_t bno
, /* starting block of extent */
158 xfs_extlen_t len
, /* length of extent */
159 int *stat
) /* success/failure */
161 cur
->bc_rec
.a
.ar_startblock
= bno
;
162 cur
->bc_rec
.a
.ar_blockcount
= len
;
163 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
167 * Lookup the first record greater than or equal to [bno, len]
168 * in the btree given by cur.
172 struct xfs_btree_cur
*cur
, /* btree cursor */
173 xfs_agblock_t bno
, /* starting block of extent */
174 xfs_extlen_t len
, /* length of extent */
175 int *stat
) /* success/failure */
177 cur
->bc_rec
.a
.ar_startblock
= bno
;
178 cur
->bc_rec
.a
.ar_blockcount
= len
;
179 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
183 * Lookup the first record less than or equal to [bno, len]
184 * in the btree given by cur.
186 static int /* error */
188 struct xfs_btree_cur
*cur
, /* btree cursor */
189 xfs_agblock_t bno
, /* starting block of extent */
190 xfs_extlen_t len
, /* length of extent */
191 int *stat
) /* success/failure */
193 cur
->bc_rec
.a
.ar_startblock
= bno
;
194 cur
->bc_rec
.a
.ar_blockcount
= len
;
195 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
199 * Update the record referred to by cur to the value given
201 * This either works (return 0) or gets an EFSCORRUPTED error.
203 STATIC
int /* error */
205 struct xfs_btree_cur
*cur
, /* btree cursor */
206 xfs_agblock_t bno
, /* starting block of extent */
207 xfs_extlen_t len
) /* length of extent */
209 union xfs_btree_rec rec
;
211 rec
.alloc
.ar_startblock
= cpu_to_be32(bno
);
212 rec
.alloc
.ar_blockcount
= cpu_to_be32(len
);
213 return xfs_btree_update(cur
, &rec
);
217 * Get the data from the pointed-to record.
221 struct xfs_btree_cur
*cur
, /* btree cursor */
222 xfs_agblock_t
*bno
, /* output: starting block of extent */
223 xfs_extlen_t
*len
, /* output: length of extent */
224 int *stat
) /* output: success/failure */
226 union xfs_btree_rec
*rec
;
229 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
230 if (!error
&& *stat
== 1) {
231 *bno
= be32_to_cpu(rec
->alloc
.ar_startblock
);
232 *len
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
238 * Compute aligned version of the found extent.
239 * Takes alignment and min length into account.
242 xfs_alloc_compute_aligned(
243 xfs_alloc_arg_t
*args
, /* allocation argument structure */
244 xfs_agblock_t foundbno
, /* starting block in found extent */
245 xfs_extlen_t foundlen
, /* length in found extent */
246 xfs_agblock_t
*resbno
, /* result block number */
247 xfs_extlen_t
*reslen
, /* result length */
250 xfs_agblock_t bno
= foundbno
;
251 xfs_extlen_t len
= foundlen
;
255 /* Trim busy sections out of found extent */
256 busy
= xfs_extent_busy_trim(args
, &bno
, &len
, busy_gen
);
259 * If we have a largish extent that happens to start before min_agbno,
260 * see if we can shift it into range...
262 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
263 diff
= args
->min_agbno
- bno
;
270 if (args
->alignment
> 1 && len
>= args
->minlen
) {
271 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
273 diff
= aligned_bno
- bno
;
275 *resbno
= aligned_bno
;
276 *reslen
= diff
>= len
? 0 : len
- diff
;
286 * Compute best start block and diff for "near" allocations.
287 * freelen >= wantlen already checked by caller.
289 STATIC xfs_extlen_t
/* difference value (absolute) */
290 xfs_alloc_compute_diff(
291 xfs_agblock_t wantbno
, /* target starting block */
292 xfs_extlen_t wantlen
, /* target length */
293 xfs_extlen_t alignment
, /* target alignment */
294 int datatype
, /* are we allocating data? */
295 xfs_agblock_t freebno
, /* freespace's starting block */
296 xfs_extlen_t freelen
, /* freespace's length */
297 xfs_agblock_t
*newbnop
) /* result: best start block from free */
299 xfs_agblock_t freeend
; /* end of freespace extent */
300 xfs_agblock_t newbno1
; /* return block number */
301 xfs_agblock_t newbno2
; /* other new block number */
302 xfs_extlen_t newlen1
=0; /* length with newbno1 */
303 xfs_extlen_t newlen2
=0; /* length with newbno2 */
304 xfs_agblock_t wantend
; /* end of target extent */
305 bool userdata
= xfs_alloc_is_userdata(datatype
);
307 ASSERT(freelen
>= wantlen
);
308 freeend
= freebno
+ freelen
;
309 wantend
= wantbno
+ wantlen
;
311 * We want to allocate from the start of a free extent if it is past
312 * the desired block or if we are allocating user data and the free
313 * extent is before desired block. The second case is there to allow
314 * for contiguous allocation from the remaining free space if the file
315 * grows in the short term.
317 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
318 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
319 newbno1
= NULLAGBLOCK
;
320 } else if (freeend
>= wantend
&& alignment
> 1) {
321 newbno1
= roundup(wantbno
, alignment
);
322 newbno2
= newbno1
- alignment
;
323 if (newbno1
>= freeend
)
324 newbno1
= NULLAGBLOCK
;
326 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
327 if (newbno2
< freebno
)
328 newbno2
= NULLAGBLOCK
;
330 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
331 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
332 if (newlen1
< newlen2
||
333 (newlen1
== newlen2
&&
334 XFS_ABSDIFF(newbno1
, wantbno
) >
335 XFS_ABSDIFF(newbno2
, wantbno
)))
337 } else if (newbno2
!= NULLAGBLOCK
)
339 } else if (freeend
>= wantend
) {
341 } else if (alignment
> 1) {
342 newbno1
= roundup(freeend
- wantlen
, alignment
);
343 if (newbno1
> freeend
- wantlen
&&
344 newbno1
- alignment
>= freebno
)
345 newbno1
-= alignment
;
346 else if (newbno1
>= freeend
)
347 newbno1
= NULLAGBLOCK
;
349 newbno1
= freeend
- wantlen
;
351 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
355 * Fix up the length, based on mod and prod.
356 * len should be k * prod + mod for some k.
357 * If len is too small it is returned unchanged.
358 * If len hits maxlen it is left alone.
362 xfs_alloc_arg_t
*args
) /* allocation argument structure */
367 ASSERT(args
->mod
< args
->prod
);
369 ASSERT(rlen
>= args
->minlen
);
370 ASSERT(rlen
<= args
->maxlen
);
371 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
372 (args
->mod
== 0 && rlen
< args
->prod
))
374 k
= rlen
% args
->prod
;
378 rlen
= rlen
- (k
- args
->mod
);
380 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
381 /* casts to (int) catch length underflows */
382 if ((int)rlen
< (int)args
->minlen
)
384 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
385 ASSERT(rlen
% args
->prod
== args
->mod
);
386 ASSERT(args
->pag
->pagf_freeblks
+ args
->pag
->pagf_flcount
>=
387 rlen
+ args
->minleft
);
392 * Update the two btrees, logically removing from freespace the extent
393 * starting at rbno, rlen blocks. The extent is contained within the
394 * actual (current) free extent fbno for flen blocks.
395 * Flags are passed in indicating whether the cursors are set to the
398 STATIC
int /* error code */
399 xfs_alloc_fixup_trees(
400 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
401 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
402 xfs_agblock_t fbno
, /* starting block of free extent */
403 xfs_extlen_t flen
, /* length of free extent */
404 xfs_agblock_t rbno
, /* starting block of returned extent */
405 xfs_extlen_t rlen
, /* length of returned extent */
406 int flags
) /* flags, XFSA_FIXUP_... */
408 int error
; /* error code */
409 int i
; /* operation results */
410 xfs_agblock_t nfbno1
; /* first new free startblock */
411 xfs_agblock_t nfbno2
; /* second new free startblock */
412 xfs_extlen_t nflen1
=0; /* first new free length */
413 xfs_extlen_t nflen2
=0; /* second new free length */
414 struct xfs_mount
*mp
;
419 * Look up the record in the by-size tree if necessary.
421 if (flags
& XFSA_FIXUP_CNT_OK
) {
423 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
425 XFS_WANT_CORRUPTED_RETURN(mp
,
426 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
429 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
431 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
434 * Look up the record in the by-block tree if necessary.
436 if (flags
& XFSA_FIXUP_BNO_OK
) {
438 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
440 XFS_WANT_CORRUPTED_RETURN(mp
,
441 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
444 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
446 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
450 if (bno_cur
->bc_nlevels
== 1 && cnt_cur
->bc_nlevels
== 1) {
451 struct xfs_btree_block
*bnoblock
;
452 struct xfs_btree_block
*cntblock
;
454 bnoblock
= XFS_BUF_TO_BLOCK(bno_cur
->bc_bufs
[0]);
455 cntblock
= XFS_BUF_TO_BLOCK(cnt_cur
->bc_bufs
[0]);
457 XFS_WANT_CORRUPTED_RETURN(mp
,
458 bnoblock
->bb_numrecs
== cntblock
->bb_numrecs
);
463 * Deal with all four cases: the allocated record is contained
464 * within the freespace record, so we can have new freespace
465 * at either (or both) end, or no freespace remaining.
467 if (rbno
== fbno
&& rlen
== flen
)
468 nfbno1
= nfbno2
= NULLAGBLOCK
;
469 else if (rbno
== fbno
) {
470 nfbno1
= rbno
+ rlen
;
471 nflen1
= flen
- rlen
;
472 nfbno2
= NULLAGBLOCK
;
473 } else if (rbno
+ rlen
== fbno
+ flen
) {
475 nflen1
= flen
- rlen
;
476 nfbno2
= NULLAGBLOCK
;
479 nflen1
= rbno
- fbno
;
480 nfbno2
= rbno
+ rlen
;
481 nflen2
= (fbno
+ flen
) - nfbno2
;
484 * Delete the entry from the by-size btree.
486 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
488 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
490 * Add new by-size btree entry(s).
492 if (nfbno1
!= NULLAGBLOCK
) {
493 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
495 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
496 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
498 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
500 if (nfbno2
!= NULLAGBLOCK
) {
501 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
503 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
504 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
506 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
509 * Fix up the by-block btree entry(s).
511 if (nfbno1
== NULLAGBLOCK
) {
513 * No remaining freespace, just delete the by-block tree entry.
515 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
517 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
520 * Update the by-block entry to start later|be shorter.
522 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
525 if (nfbno2
!= NULLAGBLOCK
) {
527 * 2 resulting free entries, need to add one.
529 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
531 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
532 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
534 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
543 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
544 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
547 if (!uuid_equal(&agfl
->agfl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
549 if (be32_to_cpu(agfl
->agfl_magicnum
) != XFS_AGFL_MAGIC
)
552 * during growfs operations, the perag is not fully initialised,
553 * so we can't use it for any useful checking. growfs ensures we can't
554 * use it by using uncached buffers that don't have the perag attached
555 * so we can detect and avoid this problem.
557 if (bp
->b_pag
&& be32_to_cpu(agfl
->agfl_seqno
) != bp
->b_pag
->pag_agno
)
560 for (i
= 0; i
< xfs_agfl_size(mp
); i
++) {
561 if (be32_to_cpu(agfl
->agfl_bno
[i
]) != NULLAGBLOCK
&&
562 be32_to_cpu(agfl
->agfl_bno
[i
]) >= mp
->m_sb
.sb_agblocks
)
566 return xfs_log_check_lsn(mp
,
567 be64_to_cpu(XFS_BUF_TO_AGFL(bp
)->agfl_lsn
));
571 xfs_agfl_read_verify(
574 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
577 * There is no verification of non-crc AGFLs because mkfs does not
578 * initialise the AGFL to zero or NULL. Hence the only valid part of the
579 * AGFL is what the AGF says is active. We can't get to the AGF, so we
580 * can't verify just those entries are valid.
582 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
585 if (!xfs_buf_verify_cksum(bp
, XFS_AGFL_CRC_OFF
))
586 xfs_buf_ioerror(bp
, -EFSBADCRC
);
587 else if (!xfs_agfl_verify(bp
))
588 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
591 xfs_verifier_error(bp
);
595 xfs_agfl_write_verify(
598 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
599 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
601 /* no verification of non-crc AGFLs */
602 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
605 if (!xfs_agfl_verify(bp
)) {
606 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
607 xfs_verifier_error(bp
);
612 XFS_BUF_TO_AGFL(bp
)->agfl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
614 xfs_buf_update_cksum(bp
, XFS_AGFL_CRC_OFF
);
617 const struct xfs_buf_ops xfs_agfl_buf_ops
= {
619 .verify_read
= xfs_agfl_read_verify
,
620 .verify_write
= xfs_agfl_write_verify
,
624 * Read in the allocation group free block array.
628 xfs_mount_t
*mp
, /* mount point structure */
629 xfs_trans_t
*tp
, /* transaction pointer */
630 xfs_agnumber_t agno
, /* allocation group number */
631 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
633 xfs_buf_t
*bp
; /* return value */
636 ASSERT(agno
!= NULLAGNUMBER
);
637 error
= xfs_trans_read_buf(
638 mp
, tp
, mp
->m_ddev_targp
,
639 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
640 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_agfl_buf_ops
);
643 xfs_buf_set_ref(bp
, XFS_AGFL_REF
);
649 xfs_alloc_update_counters(
650 struct xfs_trans
*tp
,
651 struct xfs_perag
*pag
,
652 struct xfs_buf
*agbp
,
655 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
657 pag
->pagf_freeblks
+= len
;
658 be32_add_cpu(&agf
->agf_freeblks
, len
);
660 xfs_trans_agblocks_delta(tp
, len
);
661 if (unlikely(be32_to_cpu(agf
->agf_freeblks
) >
662 be32_to_cpu(agf
->agf_length
)))
663 return -EFSCORRUPTED
;
665 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
670 * Allocation group level functions.
674 * Allocate a variable extent in the allocation group agno.
675 * Type and bno are used to determine where in the allocation group the
677 * Extent's length (returned in *len) will be between minlen and maxlen,
678 * and of the form k * prod + mod unless there's nothing that large.
679 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
681 STATIC
int /* error */
682 xfs_alloc_ag_vextent(
683 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
687 ASSERT(args
->minlen
> 0);
688 ASSERT(args
->maxlen
> 0);
689 ASSERT(args
->minlen
<= args
->maxlen
);
690 ASSERT(args
->mod
< args
->prod
);
691 ASSERT(args
->alignment
> 0);
694 * Branch to correct routine based on the type.
697 switch (args
->type
) {
698 case XFS_ALLOCTYPE_THIS_AG
:
699 error
= xfs_alloc_ag_vextent_size(args
);
701 case XFS_ALLOCTYPE_NEAR_BNO
:
702 error
= xfs_alloc_ag_vextent_near(args
);
704 case XFS_ALLOCTYPE_THIS_BNO
:
705 error
= xfs_alloc_ag_vextent_exact(args
);
712 if (error
|| args
->agbno
== NULLAGBLOCK
)
715 ASSERT(args
->len
>= args
->minlen
);
716 ASSERT(args
->len
<= args
->maxlen
);
717 ASSERT(!args
->wasfromfl
|| args
->resv
!= XFS_AG_RESV_AGFL
);
718 ASSERT(args
->agbno
% args
->alignment
== 0);
720 /* if not file data, insert new block into the reverse map btree */
721 if (args
->oinfo
.oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
722 error
= xfs_rmap_alloc(args
->tp
, args
->agbp
, args
->agno
,
723 args
->agbno
, args
->len
, &args
->oinfo
);
728 if (!args
->wasfromfl
) {
729 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
731 -((long)(args
->len
)));
735 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
736 args
->agbno
, args
->len
));
739 xfs_ag_resv_alloc_extent(args
->pag
, args
->resv
, args
);
741 XFS_STATS_INC(args
->mp
, xs_allocx
);
742 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
747 * Allocate a variable extent at exactly agno/bno.
748 * Extent's length (returned in *len) will be between minlen and maxlen,
749 * and of the form k * prod + mod unless there's nothing that large.
750 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
752 STATIC
int /* error */
753 xfs_alloc_ag_vextent_exact(
754 xfs_alloc_arg_t
*args
) /* allocation argument structure */
756 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
757 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
759 xfs_agblock_t fbno
; /* start block of found extent */
760 xfs_extlen_t flen
; /* length of found extent */
761 xfs_agblock_t tbno
; /* start block of busy extent */
762 xfs_extlen_t tlen
; /* length of busy extent */
763 xfs_agblock_t tend
; /* end block of busy extent */
764 int i
; /* success/failure of operation */
767 ASSERT(args
->alignment
== 1);
770 * Allocate/initialize a cursor for the by-number freespace btree.
772 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
773 args
->agno
, XFS_BTNUM_BNO
);
776 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
777 * Look for the closest free block <= bno, it must contain bno
778 * if any free block does.
780 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
787 * Grab the freespace record.
789 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
792 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
793 ASSERT(fbno
<= args
->agbno
);
796 * Check for overlapping busy extents.
800 xfs_extent_busy_trim(args
, &tbno
, &tlen
, &busy_gen
);
803 * Give up if the start of the extent is busy, or the freespace isn't
804 * long enough for the minimum request.
806 if (tbno
> args
->agbno
)
808 if (tlen
< args
->minlen
)
811 if (tend
< args
->agbno
+ args
->minlen
)
815 * End of extent will be smaller of the freespace end and the
816 * maximal requested end.
818 * Fix the length according to mod and prod if given.
820 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
822 xfs_alloc_fix_len(args
);
823 ASSERT(args
->agbno
+ args
->len
<= tend
);
826 * We are allocating agbno for args->len
827 * Allocate/initialize a cursor for the by-size btree.
829 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
830 args
->agno
, XFS_BTNUM_CNT
);
831 ASSERT(args
->agbno
+ args
->len
<=
832 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
833 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
834 args
->len
, XFSA_FIXUP_BNO_OK
);
836 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
840 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
841 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
844 trace_xfs_alloc_exact_done(args
);
848 /* Didn't find it, return null. */
849 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
850 args
->agbno
= NULLAGBLOCK
;
851 trace_xfs_alloc_exact_notfound(args
);
855 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
856 trace_xfs_alloc_exact_error(args
);
861 * Search the btree in a given direction via the search cursor and compare
862 * the records found against the good extent we've already found.
865 xfs_alloc_find_best_extent(
866 struct xfs_alloc_arg
*args
, /* allocation argument structure */
867 struct xfs_btree_cur
**gcur
, /* good cursor */
868 struct xfs_btree_cur
**scur
, /* searching cursor */
869 xfs_agblock_t gdiff
, /* difference for search comparison */
870 xfs_agblock_t
*sbno
, /* extent found by search */
871 xfs_extlen_t
*slen
, /* extent length */
872 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
873 xfs_extlen_t
*slena
, /* aligned extent length */
874 int dir
) /* 0 = search right, 1 = search left */
882 /* The good extent is perfect, no need to search. */
887 * Look until we find a better one, run out of space or run off the end.
890 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
893 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
894 xfs_alloc_compute_aligned(args
, *sbno
, *slen
,
895 sbnoa
, slena
, &busy_gen
);
898 * The good extent is closer than this one.
901 if (*sbnoa
> args
->max_agbno
)
903 if (*sbnoa
>= args
->agbno
+ gdiff
)
906 if (*sbnoa
< args
->min_agbno
)
908 if (*sbnoa
<= args
->agbno
- gdiff
)
913 * Same distance, compare length and pick the best.
915 if (*slena
>= args
->minlen
) {
916 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
917 xfs_alloc_fix_len(args
);
919 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
921 args
->datatype
, *sbnoa
,
925 * Choose closer size and invalidate other cursor.
933 error
= xfs_btree_increment(*scur
, 0, &i
);
935 error
= xfs_btree_decrement(*scur
, 0, &i
);
941 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
946 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
951 /* caller invalidates cursors */
956 * Allocate a variable extent near bno in the allocation group agno.
957 * Extent's length (returned in len) will be between minlen and maxlen,
958 * and of the form k * prod + mod unless there's nothing that large.
959 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
961 STATIC
int /* error */
962 xfs_alloc_ag_vextent_near(
963 xfs_alloc_arg_t
*args
) /* allocation argument structure */
965 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
966 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
967 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
968 xfs_agblock_t gtbno
; /* start bno of right side entry */
969 xfs_agblock_t gtbnoa
; /* aligned ... */
970 xfs_extlen_t gtdiff
; /* difference to right side entry */
971 xfs_extlen_t gtlen
; /* length of right side entry */
972 xfs_extlen_t gtlena
; /* aligned ... */
973 xfs_agblock_t gtnew
; /* useful start bno of right side */
974 int error
; /* error code */
975 int i
; /* result code, temporary */
976 int j
; /* result code, temporary */
977 xfs_agblock_t ltbno
; /* start bno of left side entry */
978 xfs_agblock_t ltbnoa
; /* aligned ... */
979 xfs_extlen_t ltdiff
; /* difference to left side entry */
980 xfs_extlen_t ltlen
; /* length of left side entry */
981 xfs_extlen_t ltlena
; /* aligned ... */
982 xfs_agblock_t ltnew
; /* useful start bno of left side */
983 xfs_extlen_t rlen
; /* length of returned extent */
988 * Randomly don't execute the first algorithm.
990 int dofirst
; /* set to do first algorithm */
992 dofirst
= prandom_u32() & 1;
995 /* handle unitialized agbno range so caller doesn't have to */
996 if (!args
->min_agbno
&& !args
->max_agbno
)
997 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
998 ASSERT(args
->min_agbno
<= args
->max_agbno
);
1000 /* clamp agbno to the range if it's outside */
1001 if (args
->agbno
< args
->min_agbno
)
1002 args
->agbno
= args
->min_agbno
;
1003 if (args
->agbno
> args
->max_agbno
)
1004 args
->agbno
= args
->max_agbno
;
1015 * Get a cursor for the by-size btree.
1017 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1018 args
->agno
, XFS_BTNUM_CNT
);
1021 * See if there are any free extents as big as maxlen.
1023 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
1026 * If none, then pick up the last entry in the tree unless the
1030 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
1033 if (i
== 0 || ltlen
== 0) {
1034 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1035 trace_xfs_alloc_near_noentry(args
);
1040 args
->wasfromfl
= 0;
1044 * If the requested extent is large wrt the freespaces available
1045 * in this a.g., then the cursor will be pointing to a btree entry
1046 * near the right edge of the tree. If it's in the last btree leaf
1047 * block, then we just examine all the entries in that block
1048 * that are big enough, and pick the best one.
1049 * This is written as a while loop so we can break out of it,
1050 * but we never loop back to the top.
1052 while (xfs_btree_islastblock(cnt_cur
, 0)) {
1055 xfs_extlen_t blen
=0;
1056 xfs_agblock_t bnew
=0;
1063 * Start from the entry that lookup found, sequence through
1064 * all larger free blocks. If we're actually pointing at a
1065 * record smaller than maxlen, go to the start of this block,
1066 * and skip all those smaller than minlen.
1068 if (ltlen
|| args
->alignment
> 1) {
1069 cnt_cur
->bc_ptrs
[0] = 1;
1071 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
1074 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1075 if (ltlen
>= args
->minlen
)
1077 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
1080 ASSERT(ltlen
>= args
->minlen
);
1084 i
= cnt_cur
->bc_ptrs
[0];
1085 for (j
= 1, blen
= 0, bdiff
= 0;
1086 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
1087 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
1089 * For each entry, decide if it's better than
1090 * the previous best entry.
1092 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1094 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1095 busy
= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1096 <bnoa
, <lena
, &busy_gen
);
1097 if (ltlena
< args
->minlen
)
1099 if (ltbnoa
< args
->min_agbno
|| ltbnoa
> args
->max_agbno
)
1101 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1102 xfs_alloc_fix_len(args
);
1103 ASSERT(args
->len
>= args
->minlen
);
1104 if (args
->len
< blen
)
1106 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1107 args
->alignment
, args
->datatype
, ltbnoa
,
1109 if (ltnew
!= NULLAGBLOCK
&&
1110 (args
->len
> blen
|| ltdiff
< bdiff
)) {
1114 besti
= cnt_cur
->bc_ptrs
[0];
1118 * It didn't work. We COULD be in a case where
1119 * there's a good record somewhere, so try again.
1124 * Point at the best entry, and retrieve it again.
1126 cnt_cur
->bc_ptrs
[0] = besti
;
1127 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1129 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1130 ASSERT(ltbno
+ ltlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1134 * We are allocating starting at bnew for blen blocks.
1137 ASSERT(bnew
>= ltbno
);
1138 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1140 * Set up a cursor for the by-bno tree.
1142 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1143 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1145 * Fix up the btree entries.
1147 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1148 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1150 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1151 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1153 trace_xfs_alloc_near_first(args
);
1158 * Search in the by-bno tree to the left and to the right
1159 * simultaneously, until in each case we find a space big enough,
1160 * or run into the edge of the tree. When we run into the edge,
1161 * we deallocate that cursor.
1162 * If both searches succeed, we compare the two spaces and pick
1164 * With alignment, it's possible for both to fail; the upper
1165 * level algorithm that picks allocation groups for allocations
1166 * is not supposed to do this.
1169 * Allocate and initialize the cursor for the leftward search.
1171 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1172 args
->agno
, XFS_BTNUM_BNO
);
1174 * Lookup <= bno to find the leftward search's starting point.
1176 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1180 * Didn't find anything; use this cursor for the rightward
1183 bno_cur_gt
= bno_cur_lt
;
1187 * Found something. Duplicate the cursor for the rightward search.
1189 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1192 * Increment the cursor, so we will point at the entry just right
1193 * of the leftward entry if any, or to the leftmost entry.
1195 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1199 * It failed, there are no rightward entries.
1201 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1205 * Loop going left with the leftward cursor, right with the
1206 * rightward cursor, until either both directions give up or
1207 * we find an entry at least as big as minlen.
1211 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1213 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1214 busy
|= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1215 <bnoa
, <lena
, &busy_gen
);
1216 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1218 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1220 if (!i
|| ltbnoa
< args
->min_agbno
) {
1221 xfs_btree_del_cursor(bno_cur_lt
,
1227 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1229 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1230 busy
|= xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1231 >bnoa
, >lena
, &busy_gen
);
1232 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1234 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1236 if (!i
|| gtbnoa
> args
->max_agbno
) {
1237 xfs_btree_del_cursor(bno_cur_gt
,
1242 } while (bno_cur_lt
|| bno_cur_gt
);
1245 * Got both cursors still active, need to find better entry.
1247 if (bno_cur_lt
&& bno_cur_gt
) {
1248 if (ltlena
>= args
->minlen
) {
1250 * Left side is good, look for a right side entry.
1252 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1253 xfs_alloc_fix_len(args
);
1254 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1255 args
->alignment
, args
->datatype
, ltbnoa
,
1258 error
= xfs_alloc_find_best_extent(args
,
1259 &bno_cur_lt
, &bno_cur_gt
,
1260 ltdiff
, >bno
, >len
,
1262 0 /* search right */);
1264 ASSERT(gtlena
>= args
->minlen
);
1267 * Right side is good, look for a left side entry.
1269 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1270 xfs_alloc_fix_len(args
);
1271 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1272 args
->alignment
, args
->datatype
, gtbnoa
,
1275 error
= xfs_alloc_find_best_extent(args
,
1276 &bno_cur_gt
, &bno_cur_lt
,
1277 gtdiff
, <bno
, <len
,
1279 1 /* search left */);
1287 * If we couldn't get anything, give up.
1289 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1290 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1293 trace_xfs_alloc_near_busy(args
);
1294 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1297 trace_xfs_alloc_size_neither(args
);
1298 args
->agbno
= NULLAGBLOCK
;
1303 * At this point we have selected a freespace entry, either to the
1304 * left or to the right. If it's on the right, copy all the
1305 * useful variables to the "left" set so we only have one
1306 * copy of this code.
1309 bno_cur_lt
= bno_cur_gt
;
1320 * Fix up the length and compute the useful address.
1322 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1323 xfs_alloc_fix_len(args
);
1325 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1326 args
->datatype
, ltbnoa
, ltlena
, <new
);
1327 ASSERT(ltnew
>= ltbno
);
1328 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1329 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1330 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1331 args
->agbno
= ltnew
;
1333 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1334 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1338 trace_xfs_alloc_near_greater(args
);
1340 trace_xfs_alloc_near_lesser(args
);
1342 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1343 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1347 trace_xfs_alloc_near_error(args
);
1348 if (cnt_cur
!= NULL
)
1349 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1350 if (bno_cur_lt
!= NULL
)
1351 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1352 if (bno_cur_gt
!= NULL
)
1353 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1358 * Allocate a variable extent anywhere in the allocation group agno.
1359 * Extent's length (returned in len) will be between minlen and maxlen,
1360 * and of the form k * prod + mod unless there's nothing that large.
1361 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1363 STATIC
int /* error */
1364 xfs_alloc_ag_vextent_size(
1365 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1367 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1368 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1369 int error
; /* error result */
1370 xfs_agblock_t fbno
; /* start of found freespace */
1371 xfs_extlen_t flen
; /* length of found freespace */
1372 int i
; /* temp status variable */
1373 xfs_agblock_t rbno
; /* returned block number */
1374 xfs_extlen_t rlen
; /* length of returned extent */
1380 * Allocate and initialize a cursor for the by-size btree.
1382 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1383 args
->agno
, XFS_BTNUM_CNT
);
1388 * Look for an entry >= maxlen+alignment-1 blocks.
1390 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1391 args
->maxlen
+ args
->alignment
- 1, &i
)))
1395 * If none then we have to settle for a smaller extent. In the case that
1396 * there are no large extents, this will return the last entry in the
1397 * tree unless the tree is empty. In the case that there are only busy
1398 * large extents, this will return the largest small extent unless there
1399 * are no smaller extents available.
1402 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1406 if (i
== 0 || flen
== 0) {
1407 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1408 trace_xfs_alloc_size_noentry(args
);
1412 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
,
1416 * Search for a non-busy extent that is large enough.
1419 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1422 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1424 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1425 &rbno
, &rlen
, &busy_gen
);
1427 if (rlen
>= args
->maxlen
)
1430 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1435 * Our only valid extents must have been busy.
1436 * Make it unbusy by forcing the log out and
1439 xfs_btree_del_cursor(cnt_cur
,
1441 trace_xfs_alloc_size_busy(args
);
1442 xfs_extent_busy_flush(args
->mp
,
1443 args
->pag
, busy_gen
);
1450 * In the first case above, we got the last entry in the
1451 * by-size btree. Now we check to see if the space hits maxlen
1452 * once aligned; if not, we search left for something better.
1453 * This can't happen in the second case above.
1455 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1456 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1457 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1458 if (rlen
< args
->maxlen
) {
1459 xfs_agblock_t bestfbno
;
1460 xfs_extlen_t bestflen
;
1461 xfs_agblock_t bestrbno
;
1462 xfs_extlen_t bestrlen
;
1469 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1473 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1476 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1477 if (flen
< bestrlen
)
1479 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1480 &rbno
, &rlen
, &busy_gen
);
1481 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1482 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1483 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1485 if (rlen
> bestrlen
) {
1490 if (rlen
== args
->maxlen
)
1494 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1497 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1503 args
->wasfromfl
= 0;
1505 * Fix up the length.
1508 if (rlen
< args
->minlen
) {
1510 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1511 trace_xfs_alloc_size_busy(args
);
1512 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1517 xfs_alloc_fix_len(args
);
1520 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1522 * Allocate and initialize a cursor for the by-block tree.
1524 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1525 args
->agno
, XFS_BTNUM_BNO
);
1526 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1527 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1529 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1530 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1531 cnt_cur
= bno_cur
= NULL
;
1534 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1535 args
->agbno
+ args
->len
<=
1536 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1538 trace_xfs_alloc_size_done(args
);
1542 trace_xfs_alloc_size_error(args
);
1544 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1546 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1550 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1551 trace_xfs_alloc_size_nominleft(args
);
1552 args
->agbno
= NULLAGBLOCK
;
1557 * Deal with the case where only small freespaces remain.
1558 * Either return the contents of the last freespace record,
1559 * or allocate space from the freelist if there is nothing in the tree.
1561 STATIC
int /* error */
1562 xfs_alloc_ag_vextent_small(
1563 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1564 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1565 xfs_agblock_t
*fbnop
, /* result block number */
1566 xfs_extlen_t
*flenp
, /* result length */
1567 int *stat
) /* status: 0-freelist, 1-normal/none */
1569 struct xfs_owner_info oinfo
;
1570 struct xfs_perag
*pag
;
1576 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1579 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1581 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1584 * Nothing in the btree, try the freelist. Make sure
1585 * to respect minleft even when pulling from the
1588 else if (args
->minlen
== 1 && args
->alignment
== 1 &&
1589 args
->resv
!= XFS_AG_RESV_AGFL
&&
1590 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1592 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1595 if (fbno
!= NULLAGBLOCK
) {
1596 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1597 xfs_alloc_allow_busy_reuse(args
->datatype
));
1599 if (xfs_alloc_is_userdata(args
->datatype
)) {
1602 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1603 args
->agno
, fbno
, 0);
1605 error
= -EFSCORRUPTED
;
1608 xfs_trans_binval(args
->tp
, bp
);
1612 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1613 args
->agbno
+ args
->len
<=
1614 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1616 args
->wasfromfl
= 1;
1617 trace_xfs_alloc_small_freelist(args
);
1620 * If we're feeding an AGFL block to something that
1621 * doesn't live in the free space, we need to clear
1622 * out the OWN_AG rmap and add the block back to
1623 * the AGFL per-AG reservation.
1625 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_AG
);
1626 error
= xfs_rmap_free(args
->tp
, args
->agbp
, args
->agno
,
1630 pag
= xfs_perag_get(args
->mp
, args
->agno
);
1631 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_AGFL
,
1639 * Nothing in the freelist.
1645 * Can't allocate from the freelist for some reason.
1652 * Can't do the allocation, give up.
1654 if (flen
< args
->minlen
) {
1655 args
->agbno
= NULLAGBLOCK
;
1656 trace_xfs_alloc_small_notenough(args
);
1662 trace_xfs_alloc_small_done(args
);
1666 trace_xfs_alloc_small_error(args
);
1671 * Free the extent starting at agno/bno for length.
1677 xfs_agnumber_t agno
,
1680 struct xfs_owner_info
*oinfo
,
1681 enum xfs_ag_resv_type type
)
1683 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1684 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1685 int error
; /* error return value */
1686 xfs_agblock_t gtbno
; /* start of right neighbor block */
1687 xfs_extlen_t gtlen
; /* length of right neighbor block */
1688 int haveleft
; /* have a left neighbor block */
1689 int haveright
; /* have a right neighbor block */
1690 int i
; /* temp, result code */
1691 xfs_agblock_t ltbno
; /* start of left neighbor block */
1692 xfs_extlen_t ltlen
; /* length of left neighbor block */
1693 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1694 xfs_agblock_t nbno
; /* new starting block of freespace */
1695 xfs_extlen_t nlen
; /* new length of freespace */
1696 xfs_perag_t
*pag
; /* per allocation group data */
1698 bno_cur
= cnt_cur
= NULL
;
1701 if (oinfo
->oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
1702 error
= xfs_rmap_free(tp
, agbp
, agno
, bno
, len
, oinfo
);
1708 * Allocate and initialize a cursor for the by-block btree.
1710 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1712 * Look for a neighboring block on the left (lower block numbers)
1713 * that is contiguous with this space.
1715 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1719 * There is a block to our left.
1721 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1723 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1725 * It's not contiguous, though.
1727 if (ltbno
+ ltlen
< bno
)
1731 * If this failure happens the request to free this
1732 * space was invalid, it's (partly) already free.
1735 XFS_WANT_CORRUPTED_GOTO(mp
,
1736 ltbno
+ ltlen
<= bno
, error0
);
1740 * Look for a neighboring block on the right (higher block numbers)
1741 * that is contiguous with this space.
1743 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1747 * There is a block to our right.
1749 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1751 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1753 * It's not contiguous, though.
1755 if (bno
+ len
< gtbno
)
1759 * If this failure happens the request to free this
1760 * space was invalid, it's (partly) already free.
1763 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1767 * Now allocate and initialize a cursor for the by-size tree.
1769 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1771 * Have both left and right contiguous neighbors.
1772 * Merge all three into a single free block.
1774 if (haveleft
&& haveright
) {
1776 * Delete the old by-size entry on the left.
1778 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1780 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1781 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1783 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1785 * Delete the old by-size entry on the right.
1787 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1789 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1790 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1792 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1794 * Delete the old by-block entry for the right block.
1796 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1800 * Move the by-block cursor back to the left neighbor.
1802 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1804 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1807 * Check that this is the right record: delete didn't
1808 * mangle the cursor.
1811 xfs_agblock_t xxbno
;
1814 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1817 XFS_WANT_CORRUPTED_GOTO(mp
,
1818 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1823 * Update remaining by-block entry to the new, joined block.
1826 nlen
= len
+ ltlen
+ gtlen
;
1827 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1831 * Have only a left contiguous neighbor.
1832 * Merge it together with the new freespace.
1834 else if (haveleft
) {
1836 * Delete the old by-size entry on the left.
1838 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1840 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1841 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1843 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1845 * Back up the by-block cursor to the left neighbor, and
1846 * update its length.
1848 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1850 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1853 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1857 * Have only a right contiguous neighbor.
1858 * Merge it together with the new freespace.
1860 else if (haveright
) {
1862 * Delete the old by-size entry on the right.
1864 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1866 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1867 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1869 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1871 * Update the starting block and length of the right
1872 * neighbor in the by-block tree.
1876 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1880 * No contiguous neighbors.
1881 * Insert the new freespace into the by-block tree.
1886 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1888 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1890 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1893 * In all cases we need to insert the new freespace in the by-size tree.
1895 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1897 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1898 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1900 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1901 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1905 * Update the freespace totals in the ag and superblock.
1907 pag
= xfs_perag_get(mp
, agno
);
1908 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1909 xfs_ag_resv_free_extent(pag
, type
, tp
, len
);
1914 XFS_STATS_INC(mp
, xs_freex
);
1915 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1917 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1918 haveleft
, haveright
);
1923 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1926 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1928 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1933 * Visible (exported) allocation/free functions.
1934 * Some of these are used just by xfs_alloc_btree.c and this file.
1938 * Compute and fill in value of m_ag_maxlevels.
1941 xfs_alloc_compute_maxlevels(
1942 xfs_mount_t
*mp
) /* file system mount structure */
1944 mp
->m_ag_maxlevels
= xfs_btree_compute_maxlevels(mp
, mp
->m_alloc_mnr
,
1945 (mp
->m_sb
.sb_agblocks
+ 1) / 2);
1949 * Find the length of the longest extent in an AG. The 'need' parameter
1950 * specifies how much space we're going to need for the AGFL and the
1951 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1955 xfs_alloc_longest_free_extent(
1956 struct xfs_mount
*mp
,
1957 struct xfs_perag
*pag
,
1959 xfs_extlen_t reserved
)
1961 xfs_extlen_t delta
= 0;
1964 * If the AGFL needs a recharge, we'll have to subtract that from the
1967 if (need
> pag
->pagf_flcount
)
1968 delta
= need
- pag
->pagf_flcount
;
1971 * If we cannot maintain others' reservations with space from the
1972 * not-longest freesp extents, we'll have to subtract /that/ from
1973 * the longest extent too.
1975 if (pag
->pagf_freeblks
- pag
->pagf_longest
< reserved
)
1976 delta
+= reserved
- (pag
->pagf_freeblks
- pag
->pagf_longest
);
1979 * If the longest extent is long enough to satisfy all the
1980 * reservations and AGFL rules in place, we can return this extent.
1982 if (pag
->pagf_longest
> delta
)
1983 return pag
->pagf_longest
- delta
;
1985 /* Otherwise, let the caller try for 1 block if there's space. */
1986 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
1990 xfs_alloc_min_freelist(
1991 struct xfs_mount
*mp
,
1992 struct xfs_perag
*pag
)
1994 unsigned int min_free
;
1996 /* space needed by-bno freespace btree */
1997 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
1998 mp
->m_ag_maxlevels
);
1999 /* space needed by-size freespace btree */
2000 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
2001 mp
->m_ag_maxlevels
);
2002 /* space needed reverse mapping used space btree */
2003 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
2004 min_free
+= min_t(unsigned int,
2005 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] + 1,
2006 mp
->m_rmap_maxlevels
);
2012 * Check if the operation we are fixing up the freelist for should go ahead or
2013 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2014 * is dependent on whether the size and shape of free space available will
2015 * permit the requested allocation to take place.
2018 xfs_alloc_space_available(
2019 struct xfs_alloc_arg
*args
,
2020 xfs_extlen_t min_free
,
2023 struct xfs_perag
*pag
= args
->pag
;
2024 xfs_extlen_t alloc_len
, longest
;
2025 xfs_extlen_t reservation
; /* blocks that are still reserved */
2028 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2031 reservation
= xfs_ag_resv_needed(pag
, args
->resv
);
2033 /* do we have enough contiguous free space for the allocation? */
2034 alloc_len
= args
->minlen
+ (args
->alignment
- 1) + args
->minalignslop
;
2035 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
,
2037 if (longest
< alloc_len
)
2040 /* do we have enough free space remaining for the allocation? */
2041 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
2042 reservation
- min_free
- args
->minleft
);
2043 if (available
< (int)max(args
->total
, alloc_len
))
2047 * Clamp maxlen to the amount of free space available for the actual
2048 * extent allocation.
2050 if (available
< (int)args
->maxlen
&& !(flags
& XFS_ALLOC_FLAG_CHECK
)) {
2051 args
->maxlen
= available
;
2052 ASSERT(args
->maxlen
> 0);
2053 ASSERT(args
->maxlen
>= args
->minlen
);
2060 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2061 * is to detect an agfl header padding mismatch between current and early v5
2062 * kernels. This problem manifests as a 1-slot size difference between the
2063 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2064 * may also catch variants of agfl count corruption unrelated to padding. Either
2065 * way, we'll reset the agfl and warn the user.
2067 * Return true if a reset is required before the agfl can be used, false
2071 xfs_agfl_needs_reset(
2072 struct xfs_mount
*mp
,
2073 struct xfs_agf
*agf
)
2075 uint32_t f
= be32_to_cpu(agf
->agf_flfirst
);
2076 uint32_t l
= be32_to_cpu(agf
->agf_fllast
);
2077 uint32_t c
= be32_to_cpu(agf
->agf_flcount
);
2078 int agfl_size
= xfs_agfl_size(mp
);
2081 /* no agfl header on v4 supers */
2082 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2086 * The agf read verifier catches severe corruption of these fields.
2087 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2088 * the verifier allows it.
2090 if (f
>= agfl_size
|| l
>= agfl_size
)
2096 * Check consistency between the on-disk count and the active range. An
2097 * agfl padding mismatch manifests as an inconsistent flcount.
2102 active
= agfl_size
- f
+ l
+ 1;
2110 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2111 * agfl content cannot be trusted. Warn the user that a repair is required to
2112 * recover leaked blocks.
2114 * The purpose of this mechanism is to handle filesystems affected by the agfl
2115 * header padding mismatch problem. A reset keeps the filesystem online with a
2116 * relatively minor free space accounting inconsistency rather than suffer the
2117 * inevitable crash from use of an invalid agfl block.
2121 struct xfs_trans
*tp
,
2122 struct xfs_buf
*agbp
,
2123 struct xfs_perag
*pag
)
2125 struct xfs_mount
*mp
= tp
->t_mountp
;
2126 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
2128 ASSERT(pag
->pagf_agflreset
);
2129 trace_xfs_agfl_reset(mp
, agf
, 0, _RET_IP_
);
2132 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2133 "Please unmount and run xfs_repair.",
2134 pag
->pag_agno
, pag
->pagf_flcount
);
2136 agf
->agf_flfirst
= 0;
2137 agf
->agf_fllast
= cpu_to_be32(xfs_agfl_size(mp
) - 1);
2138 agf
->agf_flcount
= 0;
2139 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLFIRST
| XFS_AGF_FLLAST
|
2142 pag
->pagf_flcount
= 0;
2143 pag
->pagf_agflreset
= false;
2147 * Decide whether to use this allocation group for this allocation.
2148 * If so, fix up the btree freelist's size.
2151 xfs_alloc_fix_freelist(
2152 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2153 int flags
) /* XFS_ALLOC_FLAG_... */
2155 struct xfs_mount
*mp
= args
->mp
;
2156 struct xfs_perag
*pag
= args
->pag
;
2157 struct xfs_trans
*tp
= args
->tp
;
2158 struct xfs_buf
*agbp
= NULL
;
2159 struct xfs_buf
*agflbp
= NULL
;
2160 struct xfs_alloc_arg targs
; /* local allocation arguments */
2161 xfs_agblock_t bno
; /* freelist block */
2162 xfs_extlen_t need
; /* total blocks needed in freelist */
2165 if (!pag
->pagf_init
) {
2166 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2169 if (!pag
->pagf_init
) {
2170 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2171 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2172 goto out_agbp_relse
;
2177 * If this is a metadata preferred pag and we are user data then try
2178 * somewhere else if we are not being asked to try harder at this
2181 if (pag
->pagf_metadata
&& xfs_alloc_is_userdata(args
->datatype
) &&
2182 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2183 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2184 goto out_agbp_relse
;
2187 need
= xfs_alloc_min_freelist(mp
, pag
);
2188 if (!xfs_alloc_space_available(args
, need
, flags
|
2189 XFS_ALLOC_FLAG_CHECK
))
2190 goto out_agbp_relse
;
2193 * Get the a.g. freespace buffer.
2194 * Can fail if we're not blocking on locks, and it's held.
2197 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2201 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2202 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2207 /* reset a padding mismatched agfl before final free space check */
2208 if (pag
->pagf_agflreset
)
2209 xfs_agfl_reset(tp
, agbp
, pag
);
2211 /* If there isn't enough total space or single-extent, reject it. */
2212 need
= xfs_alloc_min_freelist(mp
, pag
);
2213 if (!xfs_alloc_space_available(args
, need
, flags
))
2214 goto out_agbp_relse
;
2217 * Make the freelist shorter if it's too long.
2219 * Note that from this point onwards, we will always release the agf and
2220 * agfl buffers on error. This handles the case where we error out and
2221 * the buffers are clean or may not have been joined to the transaction
2222 * and hence need to be released manually. If they have been joined to
2223 * the transaction, then xfs_trans_brelse() will handle them
2224 * appropriately based on the recursion count and dirty state of the
2227 * XXX (dgc): When we have lots of free space, does this buy us
2228 * anything other than extra overhead when we need to put more blocks
2229 * back on the free list? Maybe we should only do this when space is
2230 * getting low or the AGFL is more than half full?
2232 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2233 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2234 * updating the rmapbt. Both flags are used in xfs_repair while we're
2235 * rebuilding the rmapbt, and neither are used by the kernel. They're
2236 * both required to ensure that rmaps are correctly recorded for the
2237 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2238 * repair/rmap.c in xfsprogs for details.
2240 memset(&targs
, 0, sizeof(targs
));
2241 if (flags
& XFS_ALLOC_FLAG_NORMAP
)
2242 xfs_rmap_skip_owner_update(&targs
.oinfo
);
2244 xfs_rmap_ag_owner(&targs
.oinfo
, XFS_RMAP_OWN_AG
);
2245 while (!(flags
& XFS_ALLOC_FLAG_NOSHRINK
) && pag
->pagf_flcount
> need
) {
2248 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2250 goto out_agbp_relse
;
2251 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1,
2252 &targs
.oinfo
, XFS_AG_RESV_AGFL
);
2254 goto out_agbp_relse
;
2255 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2257 error
= -EFSCORRUPTED
;
2258 goto out_agbp_relse
;
2260 xfs_trans_binval(tp
, bp
);
2266 targs
.agno
= args
->agno
;
2267 targs
.alignment
= targs
.minlen
= targs
.prod
= 1;
2268 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2270 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2272 goto out_agbp_relse
;
2274 /* Make the freelist longer if it's too short. */
2275 while (pag
->pagf_flcount
< need
) {
2277 targs
.maxlen
= need
- pag
->pagf_flcount
;
2278 targs
.resv
= XFS_AG_RESV_AGFL
;
2280 /* Allocate as many blocks as possible at once. */
2281 error
= xfs_alloc_ag_vextent(&targs
);
2283 goto out_agflbp_relse
;
2286 * Stop if we run out. Won't happen if callers are obeying
2287 * the restrictions correctly. Can happen for free calls
2288 * on a completely full ag.
2290 if (targs
.agbno
== NULLAGBLOCK
) {
2291 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2293 goto out_agflbp_relse
;
2296 * Put each allocated block on the list.
2298 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2299 error
= xfs_alloc_put_freelist(tp
, agbp
,
2302 goto out_agflbp_relse
;
2305 xfs_trans_brelse(tp
, agflbp
);
2310 xfs_trans_brelse(tp
, agflbp
);
2313 xfs_trans_brelse(tp
, agbp
);
2320 * Get a block from the freelist.
2321 * Returns with the buffer for the block gotten.
2324 xfs_alloc_get_freelist(
2325 xfs_trans_t
*tp
, /* transaction pointer */
2326 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2327 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2328 int btreeblk
) /* destination is a AGF btree */
2330 xfs_agf_t
*agf
; /* a.g. freespace structure */
2331 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2332 xfs_agblock_t bno
; /* block number returned */
2336 xfs_mount_t
*mp
= tp
->t_mountp
;
2337 xfs_perag_t
*pag
; /* per allocation group data */
2340 * Freelist is empty, give up.
2342 agf
= XFS_BUF_TO_AGF(agbp
);
2343 if (!agf
->agf_flcount
) {
2344 *bnop
= NULLAGBLOCK
;
2348 * Read the array of free blocks.
2350 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2357 * Get the block number and update the data structures.
2359 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2360 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2361 be32_add_cpu(&agf
->agf_flfirst
, 1);
2362 xfs_trans_brelse(tp
, agflbp
);
2363 if (be32_to_cpu(agf
->agf_flfirst
) == xfs_agfl_size(mp
))
2364 agf
->agf_flfirst
= 0;
2366 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2367 ASSERT(!pag
->pagf_agflreset
);
2368 be32_add_cpu(&agf
->agf_flcount
, -1);
2369 xfs_trans_agflist_delta(tp
, -1);
2370 pag
->pagf_flcount
--;
2373 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2375 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2376 pag
->pagf_btreeblks
++;
2377 logflags
|= XFS_AGF_BTREEBLKS
;
2380 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2387 * Log the given fields from the agf structure.
2391 xfs_trans_t
*tp
, /* transaction pointer */
2392 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2393 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2395 int first
; /* first byte offset */
2396 int last
; /* last byte offset */
2397 static const short offsets
[] = {
2398 offsetof(xfs_agf_t
, agf_magicnum
),
2399 offsetof(xfs_agf_t
, agf_versionnum
),
2400 offsetof(xfs_agf_t
, agf_seqno
),
2401 offsetof(xfs_agf_t
, agf_length
),
2402 offsetof(xfs_agf_t
, agf_roots
[0]),
2403 offsetof(xfs_agf_t
, agf_levels
[0]),
2404 offsetof(xfs_agf_t
, agf_flfirst
),
2405 offsetof(xfs_agf_t
, agf_fllast
),
2406 offsetof(xfs_agf_t
, agf_flcount
),
2407 offsetof(xfs_agf_t
, agf_freeblks
),
2408 offsetof(xfs_agf_t
, agf_longest
),
2409 offsetof(xfs_agf_t
, agf_btreeblks
),
2410 offsetof(xfs_agf_t
, agf_uuid
),
2411 offsetof(xfs_agf_t
, agf_rmap_blocks
),
2412 offsetof(xfs_agf_t
, agf_refcount_blocks
),
2413 offsetof(xfs_agf_t
, agf_refcount_root
),
2414 offsetof(xfs_agf_t
, agf_refcount_level
),
2415 /* needed so that we don't log the whole rest of the structure: */
2416 offsetof(xfs_agf_t
, agf_spare64
),
2420 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2422 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2424 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2425 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2429 * Interface for inode allocation to force the pag data to be initialized.
2432 xfs_alloc_pagf_init(
2433 xfs_mount_t
*mp
, /* file system mount structure */
2434 xfs_trans_t
*tp
, /* transaction pointer */
2435 xfs_agnumber_t agno
, /* allocation group number */
2436 int flags
) /* XFS_ALLOC_FLAGS_... */
2441 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2444 xfs_trans_brelse(tp
, bp
);
2449 * Put the block on the freelist for the allocation group.
2452 xfs_alloc_put_freelist(
2453 xfs_trans_t
*tp
, /* transaction pointer */
2454 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2455 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2456 xfs_agblock_t bno
, /* block being freed */
2457 int btreeblk
) /* block came from a AGF btree */
2459 xfs_agf_t
*agf
; /* a.g. freespace structure */
2460 __be32
*blockp
;/* pointer to array entry */
2463 xfs_mount_t
*mp
; /* mount structure */
2464 xfs_perag_t
*pag
; /* per allocation group data */
2468 agf
= XFS_BUF_TO_AGF(agbp
);
2471 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2472 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2474 be32_add_cpu(&agf
->agf_fllast
, 1);
2475 if (be32_to_cpu(agf
->agf_fllast
) == xfs_agfl_size(mp
))
2476 agf
->agf_fllast
= 0;
2478 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2479 ASSERT(!pag
->pagf_agflreset
);
2480 be32_add_cpu(&agf
->agf_flcount
, 1);
2481 xfs_trans_agflist_delta(tp
, 1);
2482 pag
->pagf_flcount
++;
2484 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2486 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2487 pag
->pagf_btreeblks
--;
2488 logflags
|= XFS_AGF_BTREEBLKS
;
2492 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2494 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= xfs_agfl_size(mp
));
2496 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2497 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2498 *blockp
= cpu_to_be32(bno
);
2499 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2501 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2503 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2504 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2505 startoff
+ sizeof(xfs_agblock_t
) - 1);
2511 struct xfs_mount
*mp
,
2514 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2516 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2517 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2519 if (!xfs_log_check_lsn(mp
,
2520 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2524 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2525 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2526 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2527 be32_to_cpu(agf
->agf_flfirst
) < xfs_agfl_size(mp
) &&
2528 be32_to_cpu(agf
->agf_fllast
) < xfs_agfl_size(mp
) &&
2529 be32_to_cpu(agf
->agf_flcount
) <= xfs_agfl_size(mp
)))
2532 if (be32_to_cpu(agf
->agf_length
) > mp
->m_sb
.sb_dblocks
)
2535 if (be32_to_cpu(agf
->agf_freeblks
) < be32_to_cpu(agf
->agf_longest
) ||
2536 be32_to_cpu(agf
->agf_freeblks
) > be32_to_cpu(agf
->agf_length
))
2539 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) < 1 ||
2540 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) < 1 ||
2541 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2542 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2545 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2546 (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) < 1 ||
2547 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) > XFS_BTREE_MAXLEVELS
))
2550 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2551 be32_to_cpu(agf
->agf_rmap_blocks
) > be32_to_cpu(agf
->agf_length
))
2555 * during growfs operations, the perag is not fully initialised,
2556 * so we can't use it for any useful checking. growfs ensures we can't
2557 * use it by using uncached buffers that don't have the perag attached
2558 * so we can detect and avoid this problem.
2560 if (bp
->b_pag
&& be32_to_cpu(agf
->agf_seqno
) != bp
->b_pag
->pag_agno
)
2563 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2564 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2567 if (xfs_sb_version_hasreflink(&mp
->m_sb
) &&
2568 be32_to_cpu(agf
->agf_refcount_blocks
) >
2569 be32_to_cpu(agf
->agf_length
))
2572 if (xfs_sb_version_hasreflink(&mp
->m_sb
) &&
2573 (be32_to_cpu(agf
->agf_refcount_level
) < 1 ||
2574 be32_to_cpu(agf
->agf_refcount_level
) > XFS_BTREE_MAXLEVELS
))
2582 xfs_agf_read_verify(
2585 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2587 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2588 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2589 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2590 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp
, bp
), mp
,
2591 XFS_ERRTAG_ALLOC_READ_AGF
))
2592 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2595 xfs_verifier_error(bp
);
2599 xfs_agf_write_verify(
2602 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2603 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2605 if (!xfs_agf_verify(mp
, bp
)) {
2606 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2607 xfs_verifier_error(bp
);
2611 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2615 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2617 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2620 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2622 .verify_read
= xfs_agf_read_verify
,
2623 .verify_write
= xfs_agf_write_verify
,
2627 * Read in the allocation group header (free/alloc section).
2631 struct xfs_mount
*mp
, /* mount point structure */
2632 struct xfs_trans
*tp
, /* transaction pointer */
2633 xfs_agnumber_t agno
, /* allocation group number */
2634 int flags
, /* XFS_BUF_ */
2635 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2639 trace_xfs_read_agf(mp
, agno
);
2641 ASSERT(agno
!= NULLAGNUMBER
);
2642 error
= xfs_trans_read_buf(
2643 mp
, tp
, mp
->m_ddev_targp
,
2644 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2645 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2651 ASSERT(!(*bpp
)->b_error
);
2652 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2657 * Read in the allocation group header (free/alloc section).
2661 struct xfs_mount
*mp
, /* mount point structure */
2662 struct xfs_trans
*tp
, /* transaction pointer */
2663 xfs_agnumber_t agno
, /* allocation group number */
2664 int flags
, /* XFS_ALLOC_FLAG_... */
2665 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2667 struct xfs_agf
*agf
; /* ag freelist header */
2668 struct xfs_perag
*pag
; /* per allocation group data */
2671 trace_xfs_alloc_read_agf(mp
, agno
);
2673 ASSERT(agno
!= NULLAGNUMBER
);
2674 error
= xfs_read_agf(mp
, tp
, agno
,
2675 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2681 ASSERT(!(*bpp
)->b_error
);
2683 agf
= XFS_BUF_TO_AGF(*bpp
);
2684 pag
= xfs_perag_get(mp
, agno
);
2685 if (!pag
->pagf_init
) {
2686 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2687 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2688 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2689 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2690 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2691 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2692 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2693 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2694 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] =
2695 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAPi
]);
2696 pag
->pagf_refcount_level
= be32_to_cpu(agf
->agf_refcount_level
);
2697 spin_lock_init(&pag
->pagb_lock
);
2698 pag
->pagb_count
= 0;
2699 pag
->pagb_tree
= RB_ROOT
;
2701 pag
->pagf_agflreset
= xfs_agfl_needs_reset(mp
, agf
);
2704 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2705 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2706 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2707 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2708 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2709 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2710 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2711 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2712 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2720 * Allocate an extent (variable-size).
2721 * Depending on the allocation type, we either look in a single allocation
2722 * group or loop over the allocation groups to find the result.
2726 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2728 xfs_agblock_t agsize
; /* allocation group size */
2730 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2731 xfs_mount_t
*mp
; /* mount structure pointer */
2732 xfs_agnumber_t sagno
; /* starting allocation group number */
2733 xfs_alloctype_t type
; /* input allocation type */
2735 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2738 type
= args
->otype
= args
->type
;
2739 args
->agbno
= NULLAGBLOCK
;
2741 * Just fix this up, for the case where the last a.g. is shorter
2742 * (or there's only one a.g.) and the caller couldn't easily figure
2743 * that out (xfs_bmap_alloc).
2745 agsize
= mp
->m_sb
.sb_agblocks
;
2746 if (args
->maxlen
> agsize
)
2747 args
->maxlen
= agsize
;
2748 if (args
->alignment
== 0)
2749 args
->alignment
= 1;
2750 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2751 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2752 ASSERT(args
->minlen
<= args
->maxlen
);
2753 ASSERT(args
->minlen
<= agsize
);
2754 ASSERT(args
->mod
< args
->prod
);
2755 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2756 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2757 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2758 args
->mod
>= args
->prod
) {
2759 args
->fsbno
= NULLFSBLOCK
;
2760 trace_xfs_alloc_vextent_badargs(args
);
2765 case XFS_ALLOCTYPE_THIS_AG
:
2766 case XFS_ALLOCTYPE_NEAR_BNO
:
2767 case XFS_ALLOCTYPE_THIS_BNO
:
2769 * These three force us into a single a.g.
2771 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2772 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2773 error
= xfs_alloc_fix_freelist(args
, 0);
2775 trace_xfs_alloc_vextent_nofix(args
);
2779 trace_xfs_alloc_vextent_noagbp(args
);
2782 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2783 if ((error
= xfs_alloc_ag_vextent(args
)))
2786 case XFS_ALLOCTYPE_START_BNO
:
2788 * Try near allocation first, then anywhere-in-ag after
2789 * the first a.g. fails.
2791 if ((args
->datatype
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2792 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2793 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2794 ((mp
->m_agfrotor
/ rotorstep
) %
2795 mp
->m_sb
.sb_agcount
), 0);
2798 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2799 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2801 case XFS_ALLOCTYPE_FIRST_AG
:
2803 * Rotate through the allocation groups looking for a winner.
2805 if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2807 * Start with allocation group given by bno.
2809 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2810 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2815 * Start with the given allocation group.
2817 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2818 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2821 * Loop over allocation groups twice; first time with
2822 * trylock set, second time without.
2825 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2826 error
= xfs_alloc_fix_freelist(args
, flags
);
2828 trace_xfs_alloc_vextent_nofix(args
);
2832 * If we get a buffer back then the allocation will fly.
2835 if ((error
= xfs_alloc_ag_vextent(args
)))
2840 trace_xfs_alloc_vextent_loopfailed(args
);
2843 * Didn't work, figure out the next iteration.
2845 if (args
->agno
== sagno
&&
2846 type
== XFS_ALLOCTYPE_START_BNO
)
2847 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2849 * For the first allocation, we can try any AG to get
2850 * space. However, if we already have allocated a
2851 * block, we don't want to try AGs whose number is below
2852 * sagno. Otherwise, we may end up with out-of-order
2853 * locking of AGF, which might cause deadlock.
2855 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2856 if (args
->firstblock
!= NULLFSBLOCK
)
2862 * Reached the starting a.g., must either be done
2863 * or switch to non-trylock mode.
2865 if (args
->agno
== sagno
) {
2867 args
->agbno
= NULLAGBLOCK
;
2868 trace_xfs_alloc_vextent_allfailed(args
);
2873 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2874 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2876 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2879 xfs_perag_put(args
->pag
);
2882 if (args
->agno
== sagno
)
2883 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2884 (mp
->m_sb
.sb_agcount
* rotorstep
);
2886 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2887 (mp
->m_sb
.sb_agcount
* rotorstep
);
2894 if (args
->agbno
== NULLAGBLOCK
)
2895 args
->fsbno
= NULLFSBLOCK
;
2897 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2899 ASSERT(args
->len
>= args
->minlen
);
2900 ASSERT(args
->len
<= args
->maxlen
);
2901 ASSERT(args
->agbno
% args
->alignment
== 0);
2902 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2906 /* Zero the extent if we were asked to do so */
2907 if (args
->datatype
& XFS_ALLOC_USERDATA_ZERO
) {
2908 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2914 xfs_perag_put(args
->pag
);
2917 xfs_perag_put(args
->pag
);
2921 /* Ensure that the freelist is at full capacity. */
2923 xfs_free_extent_fix_freelist(
2924 struct xfs_trans
*tp
,
2925 xfs_agnumber_t agno
,
2926 struct xfs_buf
**agbp
)
2928 struct xfs_alloc_arg args
;
2931 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
2933 args
.mp
= tp
->t_mountp
;
2937 * validate that the block number is legal - the enables us to detect
2938 * and handle a silent filesystem corruption rather than crashing.
2940 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2941 return -EFSCORRUPTED
;
2943 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2946 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2952 xfs_perag_put(args
.pag
);
2958 * Just break up the extent address and hand off to xfs_free_ag_extent
2959 * after fixing up the freelist.
2963 struct xfs_trans
*tp
, /* transaction pointer */
2964 xfs_fsblock_t bno
, /* starting block number of extent */
2965 xfs_extlen_t len
, /* length of extent */
2966 struct xfs_owner_info
*oinfo
, /* extent owner */
2967 enum xfs_ag_resv_type type
) /* block reservation type */
2969 struct xfs_mount
*mp
= tp
->t_mountp
;
2970 struct xfs_buf
*agbp
;
2971 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, bno
);
2972 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
2976 ASSERT(type
!= XFS_AG_RESV_AGFL
);
2978 if (XFS_TEST_ERROR(false, mp
,
2979 XFS_ERRTAG_FREE_EXTENT
))
2982 error
= xfs_free_extent_fix_freelist(tp
, agno
, &agbp
);
2986 XFS_WANT_CORRUPTED_GOTO(mp
, agbno
< mp
->m_sb
.sb_agblocks
, err
);
2988 /* validate the extent size is legal now we have the agf locked */
2989 XFS_WANT_CORRUPTED_GOTO(mp
,
2990 agbno
+ len
<= be32_to_cpu(XFS_BUF_TO_AGF(agbp
)->agf_length
),
2993 error
= xfs_free_ag_extent(tp
, agbp
, agno
, agbno
, len
, oinfo
, type
);
2997 xfs_extent_busy_insert(tp
, agno
, agbno
, len
, 0);
3001 xfs_trans_brelse(tp
, agbp
);
3005 struct xfs_alloc_query_range_info
{
3006 xfs_alloc_query_range_fn fn
;
3010 /* Format btree record and pass to our callback. */
3012 xfs_alloc_query_range_helper(
3013 struct xfs_btree_cur
*cur
,
3014 union xfs_btree_rec
*rec
,
3017 struct xfs_alloc_query_range_info
*query
= priv
;
3018 struct xfs_alloc_rec_incore irec
;
3020 irec
.ar_startblock
= be32_to_cpu(rec
->alloc
.ar_startblock
);
3021 irec
.ar_blockcount
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
3022 return query
->fn(cur
, &irec
, query
->priv
);
3025 /* Find all free space within a given range of blocks. */
3027 xfs_alloc_query_range(
3028 struct xfs_btree_cur
*cur
,
3029 struct xfs_alloc_rec_incore
*low_rec
,
3030 struct xfs_alloc_rec_incore
*high_rec
,
3031 xfs_alloc_query_range_fn fn
,
3034 union xfs_btree_irec low_brec
;
3035 union xfs_btree_irec high_brec
;
3036 struct xfs_alloc_query_range_info query
;
3038 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
3039 low_brec
.a
= *low_rec
;
3040 high_brec
.a
= *high_rec
;
3043 return xfs_btree_query_range(cur
, &low_brec
, &high_brec
,
3044 xfs_alloc_query_range_helper
, &query
);
3047 /* Find all free space records. */
3049 xfs_alloc_query_all(
3050 struct xfs_btree_cur
*cur
,
3051 xfs_alloc_query_range_fn fn
,
3054 struct xfs_alloc_query_range_info query
;
3056 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
3059 return xfs_btree_query_all(cur
, xfs_alloc_query_range_helper
, &query
);