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
18 #include "libxfs_priv.h"
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_cksum.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_ag_resv.h"
38 struct workqueue_struct
*xfs_alloc_wq
;
40 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
42 #define XFSA_FIXUP_BNO_OK 1
43 #define XFSA_FIXUP_CNT_OK 2
45 STATIC
int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t
*);
46 STATIC
int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t
*);
47 STATIC
int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t
*);
48 STATIC
int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t
*,
49 xfs_btree_cur_t
*, xfs_agblock_t
*, xfs_extlen_t
*, int *);
55 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
56 return XFS_RMAP_BLOCK(mp
) + 1;
57 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
58 return XFS_FIBT_BLOCK(mp
) + 1;
59 return XFS_IBT_BLOCK(mp
) + 1;
66 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
67 return xfs_refc_block(mp
) + 1;
68 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
69 return XFS_RMAP_BLOCK(mp
) + 1;
70 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
71 return XFS_FIBT_BLOCK(mp
) + 1;
72 return XFS_IBT_BLOCK(mp
) + 1;
76 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
77 * AGF buffer (PV 947395), we place constraints on the relationship among
78 * actual allocations for data blocks, freelist blocks, and potential file data
79 * bmap btree blocks. However, these restrictions may result in no actual space
80 * allocated for a delayed extent, for example, a data block in a certain AG is
81 * allocated but there is no additional block for the additional bmap btree
82 * block due to a split of the bmap btree of the file. The result of this may
83 * lead to an infinite loop when the file gets flushed to disk and all delayed
84 * extents need to be actually allocated. To get around this, we explicitly set
85 * aside a few blocks which will not be reserved in delayed allocation.
87 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
88 * potential split of the file's bmap btree.
96 blocks
= 4 + (mp
->m_sb
.sb_agcount
* XFS_ALLOC_AGFL_RESERVE
);
101 * When deciding how much space to allocate out of an AG, we limit the
102 * allocation maximum size to the size the AG. However, we cannot use all the
103 * blocks in the AG - some are permanently used by metadata. These
104 * blocks are generally:
105 * - the AG superblock, AGF, AGI and AGFL
106 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
107 * the AGI free inode and rmap btree root blocks.
108 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
109 * - the rmapbt root block
111 * The AG headers are sector sized, so the amount of space they take up is
112 * dependent on filesystem geometry. The others are all single blocks.
115 xfs_alloc_ag_max_usable(
116 struct xfs_mount
*mp
)
120 blocks
= XFS_BB_TO_FSB(mp
, XFS_FSS_TO_BB(mp
, 4)); /* ag headers */
121 blocks
+= XFS_ALLOC_AGFL_RESERVE
;
122 blocks
+= 3; /* AGF, AGI btree root blocks */
123 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
124 blocks
++; /* finobt root block */
125 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
126 blocks
++; /* rmap root block */
128 return mp
->m_sb
.sb_agblocks
- blocks
;
132 * Lookup the record equal to [bno, len] in the btree given by cur.
134 STATIC
int /* error */
136 struct xfs_btree_cur
*cur
, /* btree cursor */
137 xfs_agblock_t bno
, /* starting block of extent */
138 xfs_extlen_t len
, /* length of extent */
139 int *stat
) /* success/failure */
141 cur
->bc_rec
.a
.ar_startblock
= bno
;
142 cur
->bc_rec
.a
.ar_blockcount
= len
;
143 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
147 * Lookup the first record greater than or equal to [bno, len]
148 * in the btree given by cur.
152 struct xfs_btree_cur
*cur
, /* btree cursor */
153 xfs_agblock_t bno
, /* starting block of extent */
154 xfs_extlen_t len
, /* length of extent */
155 int *stat
) /* success/failure */
157 cur
->bc_rec
.a
.ar_startblock
= bno
;
158 cur
->bc_rec
.a
.ar_blockcount
= len
;
159 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
163 * Lookup the first record less than or equal to [bno, len]
164 * in the btree given by cur.
166 static int /* error */
168 struct xfs_btree_cur
*cur
, /* btree cursor */
169 xfs_agblock_t bno
, /* starting block of extent */
170 xfs_extlen_t len
, /* length of extent */
171 int *stat
) /* success/failure */
173 cur
->bc_rec
.a
.ar_startblock
= bno
;
174 cur
->bc_rec
.a
.ar_blockcount
= len
;
175 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
179 * Update the record referred to by cur to the value given
181 * This either works (return 0) or gets an EFSCORRUPTED error.
183 STATIC
int /* error */
185 struct xfs_btree_cur
*cur
, /* btree cursor */
186 xfs_agblock_t bno
, /* starting block of extent */
187 xfs_extlen_t len
) /* length of extent */
189 union xfs_btree_rec rec
;
191 rec
.alloc
.ar_startblock
= cpu_to_be32(bno
);
192 rec
.alloc
.ar_blockcount
= cpu_to_be32(len
);
193 return xfs_btree_update(cur
, &rec
);
197 * Get the data from the pointed-to record.
201 struct xfs_btree_cur
*cur
, /* btree cursor */
202 xfs_agblock_t
*bno
, /* output: starting block of extent */
203 xfs_extlen_t
*len
, /* output: length of extent */
204 int *stat
) /* output: success/failure */
206 union xfs_btree_rec
*rec
;
209 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
210 if (!error
&& *stat
== 1) {
211 *bno
= be32_to_cpu(rec
->alloc
.ar_startblock
);
212 *len
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
218 * Compute aligned version of the found extent.
219 * Takes alignment and min length into account.
222 xfs_alloc_compute_aligned(
223 xfs_alloc_arg_t
*args
, /* allocation argument structure */
224 xfs_agblock_t foundbno
, /* starting block in found extent */
225 xfs_extlen_t foundlen
, /* length in found extent */
226 xfs_agblock_t
*resbno
, /* result block number */
227 xfs_extlen_t
*reslen
) /* result length */
233 /* Trim busy sections out of found extent */
234 xfs_extent_busy_trim(args
, foundbno
, foundlen
, &bno
, &len
);
237 * If we have a largish extent that happens to start before min_agbno,
238 * see if we can shift it into range...
240 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
241 diff
= args
->min_agbno
- bno
;
248 if (args
->alignment
> 1 && len
>= args
->minlen
) {
249 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
251 diff
= aligned_bno
- bno
;
253 *resbno
= aligned_bno
;
254 *reslen
= diff
>= len
? 0 : len
- diff
;
262 * Compute best start block and diff for "near" allocations.
263 * freelen >= wantlen already checked by caller.
265 STATIC xfs_extlen_t
/* difference value (absolute) */
266 xfs_alloc_compute_diff(
267 xfs_agblock_t wantbno
, /* target starting block */
268 xfs_extlen_t wantlen
, /* target length */
269 xfs_extlen_t alignment
, /* target alignment */
270 int datatype
, /* are we allocating data? */
271 xfs_agblock_t freebno
, /* freespace's starting block */
272 xfs_extlen_t freelen
, /* freespace's length */
273 xfs_agblock_t
*newbnop
) /* result: best start block from free */
275 xfs_agblock_t freeend
; /* end of freespace extent */
276 xfs_agblock_t newbno1
; /* return block number */
277 xfs_agblock_t newbno2
; /* other new block number */
278 xfs_extlen_t newlen1
=0; /* length with newbno1 */
279 xfs_extlen_t newlen2
=0; /* length with newbno2 */
280 xfs_agblock_t wantend
; /* end of target extent */
281 bool userdata
= xfs_alloc_is_userdata(datatype
);
283 ASSERT(freelen
>= wantlen
);
284 freeend
= freebno
+ freelen
;
285 wantend
= wantbno
+ wantlen
;
287 * We want to allocate from the start of a free extent if it is past
288 * the desired block or if we are allocating user data and the free
289 * extent is before desired block. The second case is there to allow
290 * for contiguous allocation from the remaining free space if the file
291 * grows in the short term.
293 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
294 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
295 newbno1
= NULLAGBLOCK
;
296 } else if (freeend
>= wantend
&& alignment
> 1) {
297 newbno1
= roundup(wantbno
, alignment
);
298 newbno2
= newbno1
- alignment
;
299 if (newbno1
>= freeend
)
300 newbno1
= NULLAGBLOCK
;
302 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
303 if (newbno2
< freebno
)
304 newbno2
= NULLAGBLOCK
;
306 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
307 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
308 if (newlen1
< newlen2
||
309 (newlen1
== newlen2
&&
310 XFS_ABSDIFF(newbno1
, wantbno
) >
311 XFS_ABSDIFF(newbno2
, wantbno
)))
313 } else if (newbno2
!= NULLAGBLOCK
)
315 } else if (freeend
>= wantend
) {
317 } else if (alignment
> 1) {
318 newbno1
= roundup(freeend
- wantlen
, alignment
);
319 if (newbno1
> freeend
- wantlen
&&
320 newbno1
- alignment
>= freebno
)
321 newbno1
-= alignment
;
322 else if (newbno1
>= freeend
)
323 newbno1
= NULLAGBLOCK
;
325 newbno1
= freeend
- wantlen
;
327 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
331 * Fix up the length, based on mod and prod.
332 * len should be k * prod + mod for some k.
333 * If len is too small it is returned unchanged.
334 * If len hits maxlen it is left alone.
338 xfs_alloc_arg_t
*args
) /* allocation argument structure */
343 ASSERT(args
->mod
< args
->prod
);
345 ASSERT(rlen
>= args
->minlen
);
346 ASSERT(rlen
<= args
->maxlen
);
347 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
348 (args
->mod
== 0 && rlen
< args
->prod
))
350 k
= rlen
% args
->prod
;
354 rlen
= rlen
- (k
- args
->mod
);
356 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
357 /* casts to (int) catch length underflows */
358 if ((int)rlen
< (int)args
->minlen
)
360 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
361 ASSERT(rlen
% args
->prod
== args
->mod
);
366 * Fix up length if there is too little space left in the a.g.
367 * Return 1 if ok, 0 if too little, should give up.
370 xfs_alloc_fix_minleft(
371 xfs_alloc_arg_t
*args
) /* allocation argument structure */
373 xfs_agf_t
*agf
; /* a.g. freelist header */
374 int diff
; /* free space difference */
376 if (args
->minleft
== 0)
378 agf
= XFS_BUF_TO_AGF(args
->agbp
);
379 diff
= be32_to_cpu(agf
->agf_freeblks
)
380 - args
->len
- args
->minleft
;
383 args
->len
+= diff
; /* shrink the allocated space */
384 /* casts to (int) catch length underflows */
385 if ((int)args
->len
>= (int)args
->minlen
)
387 args
->agbno
= NULLAGBLOCK
;
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.
626 STATIC
int /* error */
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 */
686 xfs_extlen_t reservation
;
689 ASSERT(args
->minlen
> 0);
690 ASSERT(args
->maxlen
> 0);
691 ASSERT(args
->minlen
<= args
->maxlen
);
692 ASSERT(args
->mod
< args
->prod
);
693 ASSERT(args
->alignment
> 0);
696 * Clamp maxlen to the amount of free space minus any reservations
697 * that have been made.
699 oldmax
= args
->maxlen
;
700 reservation
= xfs_ag_resv_needed(args
->pag
, args
->resv
);
701 if (args
->maxlen
> args
->pag
->pagf_freeblks
- reservation
)
702 args
->maxlen
= args
->pag
->pagf_freeblks
- reservation
;
703 if (args
->maxlen
== 0) {
704 args
->agbno
= NULLAGBLOCK
;
705 args
->maxlen
= oldmax
;
710 * Branch to correct routine based on the type.
713 switch (args
->type
) {
714 case XFS_ALLOCTYPE_THIS_AG
:
715 error
= xfs_alloc_ag_vextent_size(args
);
717 case XFS_ALLOCTYPE_NEAR_BNO
:
718 error
= xfs_alloc_ag_vextent_near(args
);
720 case XFS_ALLOCTYPE_THIS_BNO
:
721 error
= xfs_alloc_ag_vextent_exact(args
);
728 args
->maxlen
= oldmax
;
730 if (error
|| args
->agbno
== NULLAGBLOCK
)
733 ASSERT(args
->len
>= args
->minlen
);
734 ASSERT(args
->len
<= args
->maxlen
);
735 ASSERT(!args
->wasfromfl
|| args
->resv
!= XFS_AG_RESV_AGFL
);
736 ASSERT(args
->agbno
% args
->alignment
== 0);
738 /* if not file data, insert new block into the reverse map btree */
739 if (args
->oinfo
.oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
740 error
= xfs_rmap_alloc(args
->tp
, args
->agbp
, args
->agno
,
741 args
->agbno
, args
->len
, &args
->oinfo
);
746 if (!args
->wasfromfl
) {
747 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
749 -((long)(args
->len
)));
753 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
754 args
->agbno
, args
->len
));
757 xfs_ag_resv_alloc_extent(args
->pag
, args
->resv
, args
);
759 XFS_STATS_INC(args
->mp
, xs_allocx
);
760 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
765 * Allocate a variable extent at exactly agno/bno.
766 * Extent's length (returned in *len) will be between minlen and maxlen,
767 * and of the form k * prod + mod unless there's nothing that large.
768 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
770 STATIC
int /* error */
771 xfs_alloc_ag_vextent_exact(
772 xfs_alloc_arg_t
*args
) /* allocation argument structure */
774 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
775 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
777 xfs_agblock_t fbno
; /* start block of found extent */
778 xfs_extlen_t flen
; /* length of found extent */
779 xfs_agblock_t tbno
; /* start block of trimmed extent */
780 xfs_extlen_t tlen
; /* length of trimmed extent */
781 xfs_agblock_t tend
; /* end block of trimmed extent */
782 int i
; /* success/failure of operation */
784 ASSERT(args
->alignment
== 1);
787 * Allocate/initialize a cursor for the by-number freespace btree.
789 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
790 args
->agno
, XFS_BTNUM_BNO
);
793 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
794 * Look for the closest free block <= bno, it must contain bno
795 * if any free block does.
797 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
804 * Grab the freespace record.
806 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
809 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
810 ASSERT(fbno
<= args
->agbno
);
813 * Check for overlapping busy extents.
815 xfs_extent_busy_trim(args
, fbno
, flen
, &tbno
, &tlen
);
818 * Give up if the start of the extent is busy, or the freespace isn't
819 * long enough for the minimum request.
821 if (tbno
> args
->agbno
)
823 if (tlen
< args
->minlen
)
826 if (tend
< args
->agbno
+ args
->minlen
)
830 * End of extent will be smaller of the freespace end and the
831 * maximal requested end.
833 * Fix the length according to mod and prod if given.
835 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
837 xfs_alloc_fix_len(args
);
838 if (!xfs_alloc_fix_minleft(args
))
841 ASSERT(args
->agbno
+ args
->len
<= tend
);
844 * We are allocating agbno for args->len
845 * Allocate/initialize a cursor for the by-size btree.
847 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
848 args
->agno
, XFS_BTNUM_CNT
);
849 ASSERT(args
->agbno
+ args
->len
<=
850 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
851 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
852 args
->len
, XFSA_FIXUP_BNO_OK
);
854 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
858 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
859 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
862 trace_xfs_alloc_exact_done(args
);
866 /* Didn't find it, return null. */
867 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
868 args
->agbno
= NULLAGBLOCK
;
869 trace_xfs_alloc_exact_notfound(args
);
873 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
874 trace_xfs_alloc_exact_error(args
);
879 * Search the btree in a given direction via the search cursor and compare
880 * the records found against the good extent we've already found.
883 xfs_alloc_find_best_extent(
884 struct xfs_alloc_arg
*args
, /* allocation argument structure */
885 struct xfs_btree_cur
**gcur
, /* good cursor */
886 struct xfs_btree_cur
**scur
, /* searching cursor */
887 xfs_agblock_t gdiff
, /* difference for search comparison */
888 xfs_agblock_t
*sbno
, /* extent found by search */
889 xfs_extlen_t
*slen
, /* extent length */
890 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
891 xfs_extlen_t
*slena
, /* aligned extent length */
892 int dir
) /* 0 = search right, 1 = search left */
899 /* The good extent is perfect, no need to search. */
904 * Look until we find a better one, run out of space or run off the end.
907 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
910 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
911 xfs_alloc_compute_aligned(args
, *sbno
, *slen
, sbnoa
, slena
);
914 * The good extent is closer than this one.
917 if (*sbnoa
> args
->max_agbno
)
919 if (*sbnoa
>= args
->agbno
+ gdiff
)
922 if (*sbnoa
< args
->min_agbno
)
924 if (*sbnoa
<= args
->agbno
- gdiff
)
929 * Same distance, compare length and pick the best.
931 if (*slena
>= args
->minlen
) {
932 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
933 xfs_alloc_fix_len(args
);
935 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
937 args
->datatype
, *sbnoa
,
941 * Choose closer size and invalidate other cursor.
949 error
= xfs_btree_increment(*scur
, 0, &i
);
951 error
= xfs_btree_decrement(*scur
, 0, &i
);
957 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
962 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
967 /* caller invalidates cursors */
972 * Allocate a variable extent near bno in the allocation group agno.
973 * Extent's length (returned in len) will be between minlen and maxlen,
974 * and of the form k * prod + mod unless there's nothing that large.
975 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
977 STATIC
int /* error */
978 xfs_alloc_ag_vextent_near(
979 xfs_alloc_arg_t
*args
) /* allocation argument structure */
981 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
982 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
983 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
984 xfs_agblock_t gtbno
; /* start bno of right side entry */
985 xfs_agblock_t gtbnoa
; /* aligned ... */
986 xfs_extlen_t gtdiff
; /* difference to right side entry */
987 xfs_extlen_t gtlen
; /* length of right side entry */
988 xfs_extlen_t gtlena
; /* aligned ... */
989 xfs_agblock_t gtnew
; /* useful start bno of right side */
990 int error
; /* error code */
991 int i
; /* result code, temporary */
992 int j
; /* result code, temporary */
993 xfs_agblock_t ltbno
; /* start bno of left side entry */
994 xfs_agblock_t ltbnoa
; /* aligned ... */
995 xfs_extlen_t ltdiff
; /* difference to left side entry */
996 xfs_extlen_t ltlen
; /* length of left side entry */
997 xfs_extlen_t ltlena
; /* aligned ... */
998 xfs_agblock_t ltnew
; /* useful start bno of left side */
999 xfs_extlen_t rlen
; /* length of returned extent */
1003 * Randomly don't execute the first algorithm.
1005 int dofirst
; /* set to do first algorithm */
1007 dofirst
= prandom_u32() & 1;
1010 /* handle unitialized agbno range so caller doesn't have to */
1011 if (!args
->min_agbno
&& !args
->max_agbno
)
1012 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
1013 ASSERT(args
->min_agbno
<= args
->max_agbno
);
1015 /* clamp agbno to the range if it's outside */
1016 if (args
->agbno
< args
->min_agbno
)
1017 args
->agbno
= args
->min_agbno
;
1018 if (args
->agbno
> args
->max_agbno
)
1019 args
->agbno
= args
->max_agbno
;
1029 * Get a cursor for the by-size btree.
1031 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1032 args
->agno
, XFS_BTNUM_CNT
);
1035 * See if there are any free extents as big as maxlen.
1037 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
1040 * If none, then pick up the last entry in the tree unless the
1044 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
1047 if (i
== 0 || ltlen
== 0) {
1048 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1049 trace_xfs_alloc_near_noentry(args
);
1054 args
->wasfromfl
= 0;
1058 * If the requested extent is large wrt the freespaces available
1059 * in this a.g., then the cursor will be pointing to a btree entry
1060 * near the right edge of the tree. If it's in the last btree leaf
1061 * block, then we just examine all the entries in that block
1062 * that are big enough, and pick the best one.
1063 * This is written as a while loop so we can break out of it,
1064 * but we never loop back to the top.
1066 while (xfs_btree_islastblock(cnt_cur
, 0)) {
1069 xfs_extlen_t blen
=0;
1070 xfs_agblock_t bnew
=0;
1077 * Start from the entry that lookup found, sequence through
1078 * all larger free blocks. If we're actually pointing at a
1079 * record smaller than maxlen, go to the start of this block,
1080 * and skip all those smaller than minlen.
1082 if (ltlen
|| args
->alignment
> 1) {
1083 cnt_cur
->bc_ptrs
[0] = 1;
1085 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
1088 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1089 if (ltlen
>= args
->minlen
)
1091 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
1094 ASSERT(ltlen
>= args
->minlen
);
1098 i
= cnt_cur
->bc_ptrs
[0];
1099 for (j
= 1, blen
= 0, bdiff
= 0;
1100 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
1101 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
1103 * For each entry, decide if it's better than
1104 * the previous best entry.
1106 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1108 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1109 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1111 if (ltlena
< args
->minlen
)
1113 if (ltbnoa
< args
->min_agbno
|| ltbnoa
> args
->max_agbno
)
1115 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1116 xfs_alloc_fix_len(args
);
1117 ASSERT(args
->len
>= args
->minlen
);
1118 if (args
->len
< blen
)
1120 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1121 args
->alignment
, args
->datatype
, ltbnoa
,
1123 if (ltnew
!= NULLAGBLOCK
&&
1124 (args
->len
> blen
|| ltdiff
< bdiff
)) {
1128 besti
= cnt_cur
->bc_ptrs
[0];
1132 * It didn't work. We COULD be in a case where
1133 * there's a good record somewhere, so try again.
1138 * Point at the best entry, and retrieve it again.
1140 cnt_cur
->bc_ptrs
[0] = besti
;
1141 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1143 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1144 ASSERT(ltbno
+ ltlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1146 if (!xfs_alloc_fix_minleft(args
)) {
1147 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1148 trace_xfs_alloc_near_nominleft(args
);
1153 * We are allocating starting at bnew for blen blocks.
1156 ASSERT(bnew
>= ltbno
);
1157 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1159 * Set up a cursor for the by-bno tree.
1161 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1162 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1164 * Fix up the btree entries.
1166 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1167 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1169 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1170 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1172 trace_xfs_alloc_near_first(args
);
1177 * Search in the by-bno tree to the left and to the right
1178 * simultaneously, until in each case we find a space big enough,
1179 * or run into the edge of the tree. When we run into the edge,
1180 * we deallocate that cursor.
1181 * If both searches succeed, we compare the two spaces and pick
1183 * With alignment, it's possible for both to fail; the upper
1184 * level algorithm that picks allocation groups for allocations
1185 * is not supposed to do this.
1188 * Allocate and initialize the cursor for the leftward search.
1190 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1191 args
->agno
, XFS_BTNUM_BNO
);
1193 * Lookup <= bno to find the leftward search's starting point.
1195 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1199 * Didn't find anything; use this cursor for the rightward
1202 bno_cur_gt
= bno_cur_lt
;
1206 * Found something. Duplicate the cursor for the rightward search.
1208 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1211 * Increment the cursor, so we will point at the entry just right
1212 * of the leftward entry if any, or to the leftmost entry.
1214 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1218 * It failed, there are no rightward entries.
1220 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1224 * Loop going left with the leftward cursor, right with the
1225 * rightward cursor, until either both directions give up or
1226 * we find an entry at least as big as minlen.
1230 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1232 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1233 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1235 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1237 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1239 if (!i
|| ltbnoa
< args
->min_agbno
) {
1240 xfs_btree_del_cursor(bno_cur_lt
,
1246 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1248 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1249 xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1251 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1253 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1255 if (!i
|| gtbnoa
> args
->max_agbno
) {
1256 xfs_btree_del_cursor(bno_cur_gt
,
1261 } while (bno_cur_lt
|| bno_cur_gt
);
1264 * Got both cursors still active, need to find better entry.
1266 if (bno_cur_lt
&& bno_cur_gt
) {
1267 if (ltlena
>= args
->minlen
) {
1269 * Left side is good, look for a right side entry.
1271 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1272 xfs_alloc_fix_len(args
);
1273 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1274 args
->alignment
, args
->datatype
, ltbnoa
,
1277 error
= xfs_alloc_find_best_extent(args
,
1278 &bno_cur_lt
, &bno_cur_gt
,
1279 ltdiff
, >bno
, >len
,
1281 0 /* search right */);
1283 ASSERT(gtlena
>= args
->minlen
);
1286 * Right side is good, look for a left side entry.
1288 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1289 xfs_alloc_fix_len(args
);
1290 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1291 args
->alignment
, args
->datatype
, gtbnoa
,
1294 error
= xfs_alloc_find_best_extent(args
,
1295 &bno_cur_gt
, &bno_cur_lt
,
1296 gtdiff
, <bno
, <len
,
1298 1 /* search left */);
1306 * If we couldn't get anything, give up.
1308 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1309 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1312 trace_xfs_alloc_near_busy(args
);
1313 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1316 trace_xfs_alloc_size_neither(args
);
1317 args
->agbno
= NULLAGBLOCK
;
1322 * At this point we have selected a freespace entry, either to the
1323 * left or to the right. If it's on the right, copy all the
1324 * useful variables to the "left" set so we only have one
1325 * copy of this code.
1328 bno_cur_lt
= bno_cur_gt
;
1339 * Fix up the length and compute the useful address.
1341 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1342 xfs_alloc_fix_len(args
);
1343 if (!xfs_alloc_fix_minleft(args
)) {
1344 trace_xfs_alloc_near_nominleft(args
);
1345 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1346 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1350 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1351 args
->datatype
, ltbnoa
, ltlena
, <new
);
1352 ASSERT(ltnew
>= ltbno
);
1353 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1354 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1355 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1356 args
->agbno
= ltnew
;
1358 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1359 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1363 trace_xfs_alloc_near_greater(args
);
1365 trace_xfs_alloc_near_lesser(args
);
1367 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1368 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1372 trace_xfs_alloc_near_error(args
);
1373 if (cnt_cur
!= NULL
)
1374 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1375 if (bno_cur_lt
!= NULL
)
1376 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1377 if (bno_cur_gt
!= NULL
)
1378 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1383 * Allocate a variable extent anywhere in the allocation group agno.
1384 * Extent's length (returned in len) will be between minlen and maxlen,
1385 * and of the form k * prod + mod unless there's nothing that large.
1386 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1388 STATIC
int /* error */
1389 xfs_alloc_ag_vextent_size(
1390 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1392 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1393 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1394 int error
; /* error result */
1395 xfs_agblock_t fbno
; /* start of found freespace */
1396 xfs_extlen_t flen
; /* length of found freespace */
1397 int i
; /* temp status variable */
1398 xfs_agblock_t rbno
; /* returned block number */
1399 xfs_extlen_t rlen
; /* length of returned extent */
1404 * Allocate and initialize a cursor for the by-size btree.
1406 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1407 args
->agno
, XFS_BTNUM_CNT
);
1411 * Look for an entry >= maxlen+alignment-1 blocks.
1413 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1414 args
->maxlen
+ args
->alignment
- 1, &i
)))
1418 * If none or we have busy extents that we cannot allocate from, then
1419 * we have to settle for a smaller extent. In the case that there are
1420 * no large extents, this will return the last entry in the tree unless
1421 * the tree is empty. In the case that there are only busy large
1422 * extents, this will return the largest small extent unless there
1423 * are no smaller extents available.
1425 if (!i
|| forced
> 1) {
1426 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1430 if (i
== 0 || flen
== 0) {
1431 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1432 trace_xfs_alloc_size_noentry(args
);
1436 xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
, &rlen
);
1439 * Search for a non-busy extent that is large enough.
1440 * If we are at low space, don't check, or if we fall of
1441 * the end of the btree, turn off the busy check and
1445 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1448 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1450 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1453 if (rlen
>= args
->maxlen
)
1456 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1461 * Our only valid extents must have been busy.
1462 * Make it unbusy by forcing the log out and
1463 * retrying. If we've been here before, forcing
1464 * the log isn't making the extents available,
1465 * which means they have probably been freed in
1466 * this transaction. In that case, we have to
1467 * give up on them and we'll attempt a minlen
1468 * allocation the next time around.
1470 xfs_btree_del_cursor(cnt_cur
,
1472 trace_xfs_alloc_size_busy(args
);
1474 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1481 * In the first case above, we got the last entry in the
1482 * by-size btree. Now we check to see if the space hits maxlen
1483 * once aligned; if not, we search left for something better.
1484 * This can't happen in the second case above.
1486 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1487 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1488 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1489 if (rlen
< args
->maxlen
) {
1490 xfs_agblock_t bestfbno
;
1491 xfs_extlen_t bestflen
;
1492 xfs_agblock_t bestrbno
;
1493 xfs_extlen_t bestrlen
;
1500 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1504 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1507 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1508 if (flen
< bestrlen
)
1510 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1512 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1513 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1514 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1516 if (rlen
> bestrlen
) {
1521 if (rlen
== args
->maxlen
)
1525 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1528 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1534 args
->wasfromfl
= 0;
1536 * Fix up the length.
1539 if (rlen
< args
->minlen
) {
1541 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1542 trace_xfs_alloc_size_busy(args
);
1543 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1548 xfs_alloc_fix_len(args
);
1550 if (!xfs_alloc_fix_minleft(args
))
1553 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1555 * Allocate and initialize a cursor for the by-block tree.
1557 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1558 args
->agno
, XFS_BTNUM_BNO
);
1559 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1560 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1562 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1563 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1564 cnt_cur
= bno_cur
= NULL
;
1567 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1568 args
->agbno
+ args
->len
<=
1569 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1571 trace_xfs_alloc_size_done(args
);
1575 trace_xfs_alloc_size_error(args
);
1577 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1579 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1583 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1584 trace_xfs_alloc_size_nominleft(args
);
1585 args
->agbno
= NULLAGBLOCK
;
1590 * Deal with the case where only small freespaces remain.
1591 * Either return the contents of the last freespace record,
1592 * or allocate space from the freelist if there is nothing in the tree.
1594 STATIC
int /* error */
1595 xfs_alloc_ag_vextent_small(
1596 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1597 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1598 xfs_agblock_t
*fbnop
, /* result block number */
1599 xfs_extlen_t
*flenp
, /* result length */
1600 int *stat
) /* status: 0-freelist, 1-normal/none */
1602 struct xfs_owner_info oinfo
;
1603 struct xfs_perag
*pag
;
1609 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1612 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1614 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1617 * Nothing in the btree, try the freelist. Make sure
1618 * to respect minleft even when pulling from the
1621 else if (args
->minlen
== 1 && args
->alignment
== 1 &&
1622 args
->resv
!= XFS_AG_RESV_AGFL
&&
1623 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1625 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1628 if (fbno
!= NULLAGBLOCK
) {
1629 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1630 xfs_alloc_allow_busy_reuse(args
->datatype
));
1632 if (xfs_alloc_is_userdata(args
->datatype
)) {
1635 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1636 args
->agno
, fbno
, 0);
1637 xfs_trans_binval(args
->tp
, bp
);
1641 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1642 args
->agbno
+ args
->len
<=
1643 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1645 args
->wasfromfl
= 1;
1646 trace_xfs_alloc_small_freelist(args
);
1649 * If we're feeding an AGFL block to something that
1650 * doesn't live in the free space, we need to clear
1651 * out the OWN_AG rmap and add the block back to
1652 * the AGFL per-AG reservation.
1654 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_AG
);
1655 error
= xfs_rmap_free(args
->tp
, args
->agbp
, args
->agno
,
1659 pag
= xfs_perag_get(args
->mp
, args
->agno
);
1660 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_AGFL
,
1668 * Nothing in the freelist.
1674 * Can't allocate from the freelist for some reason.
1681 * Can't do the allocation, give up.
1683 if (flen
< args
->minlen
) {
1684 args
->agbno
= NULLAGBLOCK
;
1685 trace_xfs_alloc_small_notenough(args
);
1691 trace_xfs_alloc_small_done(args
);
1695 trace_xfs_alloc_small_error(args
);
1700 * Free the extent starting at agno/bno for length.
1706 xfs_agnumber_t agno
,
1709 struct xfs_owner_info
*oinfo
,
1710 enum xfs_ag_resv_type type
)
1712 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1713 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1714 int error
; /* error return value */
1715 xfs_agblock_t gtbno
; /* start of right neighbor block */
1716 xfs_extlen_t gtlen
; /* length of right neighbor block */
1717 int haveleft
; /* have a left neighbor block */
1718 int haveright
; /* have a right neighbor block */
1719 int i
; /* temp, result code */
1720 xfs_agblock_t ltbno
; /* start of left neighbor block */
1721 xfs_extlen_t ltlen
; /* length of left neighbor block */
1722 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1723 xfs_agblock_t nbno
; /* new starting block of freespace */
1724 xfs_extlen_t nlen
; /* new length of freespace */
1725 xfs_perag_t
*pag
; /* per allocation group data */
1727 bno_cur
= cnt_cur
= NULL
;
1730 if (oinfo
->oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
1731 error
= xfs_rmap_free(tp
, agbp
, agno
, bno
, len
, oinfo
);
1737 * Allocate and initialize a cursor for the by-block btree.
1739 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1741 * Look for a neighboring block on the left (lower block numbers)
1742 * that is contiguous with this space.
1744 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1748 * There is a block to our left.
1750 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1752 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1754 * It's not contiguous, though.
1756 if (ltbno
+ ltlen
< bno
)
1760 * If this failure happens the request to free this
1761 * space was invalid, it's (partly) already free.
1764 XFS_WANT_CORRUPTED_GOTO(mp
,
1765 ltbno
+ ltlen
<= bno
, error0
);
1769 * Look for a neighboring block on the right (higher block numbers)
1770 * that is contiguous with this space.
1772 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1776 * There is a block to our right.
1778 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1780 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1782 * It's not contiguous, though.
1784 if (bno
+ len
< gtbno
)
1788 * If this failure happens the request to free this
1789 * space was invalid, it's (partly) already free.
1792 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1796 * Now allocate and initialize a cursor for the by-size tree.
1798 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1800 * Have both left and right contiguous neighbors.
1801 * Merge all three into a single free block.
1803 if (haveleft
&& haveright
) {
1805 * Delete the old by-size entry on the left.
1807 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1809 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1810 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1812 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1814 * Delete the old by-size entry on the right.
1816 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1818 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1819 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1821 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1823 * Delete the old by-block entry for the right block.
1825 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1827 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1829 * Move the by-block cursor back to the left neighbor.
1831 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1833 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1836 * Check that this is the right record: delete didn't
1837 * mangle the cursor.
1840 xfs_agblock_t xxbno
;
1843 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1846 XFS_WANT_CORRUPTED_GOTO(mp
,
1847 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1852 * Update remaining by-block entry to the new, joined block.
1855 nlen
= len
+ ltlen
+ gtlen
;
1856 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1860 * Have only a left contiguous neighbor.
1861 * Merge it together with the new freespace.
1863 else if (haveleft
) {
1865 * Delete the old by-size entry on the left.
1867 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1869 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1870 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1872 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1874 * Back up the by-block cursor to the left neighbor, and
1875 * update its length.
1877 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1879 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1882 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1886 * Have only a right contiguous neighbor.
1887 * Merge it together with the new freespace.
1889 else if (haveright
) {
1891 * Delete the old by-size entry on the right.
1893 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1895 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1896 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1898 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1900 * Update the starting block and length of the right
1901 * neighbor in the by-block tree.
1905 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1909 * No contiguous neighbors.
1910 * Insert the new freespace into the by-block tree.
1915 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1917 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1919 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1922 * In all cases we need to insert the new freespace in the by-size tree.
1924 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1926 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1927 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1929 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1930 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1934 * Update the freespace totals in the ag and superblock.
1936 pag
= xfs_perag_get(mp
, agno
);
1937 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1938 xfs_ag_resv_free_extent(pag
, type
, tp
, len
);
1943 XFS_STATS_INC(mp
, xs_freex
);
1944 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1946 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1947 haveleft
, haveright
);
1952 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1955 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1957 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1962 * Visible (exported) allocation/free functions.
1963 * Some of these are used just by xfs_alloc_btree.c and this file.
1967 * Compute and fill in value of m_ag_maxlevels.
1970 xfs_alloc_compute_maxlevels(
1971 xfs_mount_t
*mp
) /* file system mount structure */
1973 mp
->m_ag_maxlevels
= xfs_btree_compute_maxlevels(mp
, mp
->m_alloc_mnr
,
1974 (mp
->m_sb
.sb_agblocks
+ 1) / 2);
1978 * Find the length of the longest extent in an AG. The 'need' parameter
1979 * specifies how much space we're going to need for the AGFL and the
1980 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1984 xfs_alloc_longest_free_extent(
1985 struct xfs_mount
*mp
,
1986 struct xfs_perag
*pag
,
1988 xfs_extlen_t reserved
)
1990 xfs_extlen_t delta
= 0;
1993 * If the AGFL needs a recharge, we'll have to subtract that from the
1996 if (need
> pag
->pagf_flcount
)
1997 delta
= need
- pag
->pagf_flcount
;
2000 * If we cannot maintain others' reservations with space from the
2001 * not-longest freesp extents, we'll have to subtract /that/ from
2002 * the longest extent too.
2004 if (pag
->pagf_freeblks
- pag
->pagf_longest
< reserved
)
2005 delta
+= reserved
- (pag
->pagf_freeblks
- pag
->pagf_longest
);
2008 * If the longest extent is long enough to satisfy all the
2009 * reservations and AGFL rules in place, we can return this extent.
2011 if (pag
->pagf_longest
> delta
)
2012 return pag
->pagf_longest
- delta
;
2014 /* Otherwise, let the caller try for 1 block if there's space. */
2015 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
2019 xfs_alloc_min_freelist(
2020 struct xfs_mount
*mp
,
2021 struct xfs_perag
*pag
)
2023 unsigned int min_free
;
2025 /* space needed by-bno freespace btree */
2026 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
2027 mp
->m_ag_maxlevels
);
2028 /* space needed by-size freespace btree */
2029 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
2030 mp
->m_ag_maxlevels
);
2031 /* space needed reverse mapping used space btree */
2032 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
2033 min_free
+= min_t(unsigned int,
2034 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] + 1,
2035 mp
->m_rmap_maxlevels
);
2041 * Check if the operation we are fixing up the freelist for should go ahead or
2042 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2043 * is dependent on whether the size and shape of free space available will
2044 * permit the requested allocation to take place.
2047 xfs_alloc_space_available(
2048 struct xfs_alloc_arg
*args
,
2049 xfs_extlen_t min_free
,
2052 struct xfs_perag
*pag
= args
->pag
;
2053 xfs_extlen_t longest
;
2054 xfs_extlen_t reservation
; /* blocks that are still reserved */
2057 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2060 reservation
= xfs_ag_resv_needed(pag
, args
->resv
);
2062 /* do we have enough contiguous free space for the allocation? */
2063 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
,
2065 if ((args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1) > longest
)
2068 /* do we have enough free space remaining for the allocation? */
2069 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
2070 reservation
- min_free
- args
->total
);
2071 if (available
< (int)args
->minleft
|| available
<= 0)
2078 * Decide whether to use this allocation group for this allocation.
2079 * If so, fix up the btree freelist's size.
2082 xfs_alloc_fix_freelist(
2083 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2084 int flags
) /* XFS_ALLOC_FLAG_... */
2086 struct xfs_mount
*mp
= args
->mp
;
2087 struct xfs_perag
*pag
= args
->pag
;
2088 struct xfs_trans
*tp
= args
->tp
;
2089 struct xfs_buf
*agbp
= NULL
;
2090 struct xfs_buf
*agflbp
= NULL
;
2091 struct xfs_alloc_arg targs
; /* local allocation arguments */
2092 xfs_agblock_t bno
; /* freelist block */
2093 xfs_extlen_t need
; /* total blocks needed in freelist */
2096 if (!pag
->pagf_init
) {
2097 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2100 if (!pag
->pagf_init
) {
2101 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2102 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2103 goto out_agbp_relse
;
2108 * If this is a metadata preferred pag and we are user data then try
2109 * somewhere else if we are not being asked to try harder at this
2112 if (pag
->pagf_metadata
&& xfs_alloc_is_userdata(args
->datatype
) &&
2113 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2114 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2115 goto out_agbp_relse
;
2118 need
= xfs_alloc_min_freelist(mp
, pag
);
2119 if (!xfs_alloc_space_available(args
, need
, flags
))
2120 goto out_agbp_relse
;
2123 * Get the a.g. freespace buffer.
2124 * Can fail if we're not blocking on locks, and it's held.
2127 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2131 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2132 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2137 /* If there isn't enough total space or single-extent, reject it. */
2138 need
= xfs_alloc_min_freelist(mp
, pag
);
2139 if (!xfs_alloc_space_available(args
, need
, flags
))
2140 goto out_agbp_relse
;
2143 * Make the freelist shorter if it's too long.
2145 * Note that from this point onwards, we will always release the agf and
2146 * agfl buffers on error. This handles the case where we error out and
2147 * the buffers are clean or may not have been joined to the transaction
2148 * and hence need to be released manually. If they have been joined to
2149 * the transaction, then xfs_trans_brelse() will handle them
2150 * appropriately based on the recursion count and dirty state of the
2153 * XXX (dgc): When we have lots of free space, does this buy us
2154 * anything other than extra overhead when we need to put more blocks
2155 * back on the free list? Maybe we should only do this when space is
2156 * getting low or the AGFL is more than half full?
2158 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2159 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2160 * updating the rmapbt. Both flags are used in xfs_repair while we're
2161 * rebuilding the rmapbt, and neither are used by the kernel. They're
2162 * both required to ensure that rmaps are correctly recorded for the
2163 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2164 * repair/rmap.c in xfsprogs for details.
2166 memset(&targs
, 0, sizeof(targs
));
2167 if (flags
& XFS_ALLOC_FLAG_NORMAP
)
2168 xfs_rmap_skip_owner_update(&targs
.oinfo
);
2170 xfs_rmap_ag_owner(&targs
.oinfo
, XFS_RMAP_OWN_AG
);
2171 while (!(flags
& XFS_ALLOC_FLAG_NOSHRINK
) && pag
->pagf_flcount
> need
) {
2174 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2176 goto out_agbp_relse
;
2177 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1,
2178 &targs
.oinfo
, XFS_AG_RESV_AGFL
);
2180 goto out_agbp_relse
;
2181 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2182 xfs_trans_binval(tp
, bp
);
2188 targs
.agno
= args
->agno
;
2189 targs
.alignment
= targs
.minlen
= targs
.prod
= 1;
2190 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2192 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2194 goto out_agbp_relse
;
2196 /* Make the freelist longer if it's too short. */
2197 while (pag
->pagf_flcount
< need
) {
2199 targs
.maxlen
= need
- pag
->pagf_flcount
;
2200 targs
.resv
= XFS_AG_RESV_AGFL
;
2202 /* Allocate as many blocks as possible at once. */
2203 error
= xfs_alloc_ag_vextent(&targs
);
2205 goto out_agflbp_relse
;
2208 * Stop if we run out. Won't happen if callers are obeying
2209 * the restrictions correctly. Can happen for free calls
2210 * on a completely full ag.
2212 if (targs
.agbno
== NULLAGBLOCK
) {
2213 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2215 goto out_agflbp_relse
;
2218 * Put each allocated block on the list.
2220 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2221 error
= xfs_alloc_put_freelist(tp
, agbp
,
2224 goto out_agflbp_relse
;
2227 xfs_trans_brelse(tp
, agflbp
);
2232 xfs_trans_brelse(tp
, agflbp
);
2235 xfs_trans_brelse(tp
, agbp
);
2242 * Get a block from the freelist.
2243 * Returns with the buffer for the block gotten.
2246 xfs_alloc_get_freelist(
2247 xfs_trans_t
*tp
, /* transaction pointer */
2248 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2249 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2250 int btreeblk
) /* destination is a AGF btree */
2252 xfs_agf_t
*agf
; /* a.g. freespace structure */
2253 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2254 xfs_agblock_t bno
; /* block number returned */
2258 xfs_mount_t
*mp
= tp
->t_mountp
;
2259 xfs_perag_t
*pag
; /* per allocation group data */
2262 * Freelist is empty, give up.
2264 agf
= XFS_BUF_TO_AGF(agbp
);
2265 if (!agf
->agf_flcount
) {
2266 *bnop
= NULLAGBLOCK
;
2270 * Read the array of free blocks.
2272 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2279 * Get the block number and update the data structures.
2281 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2282 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2283 be32_add_cpu(&agf
->agf_flfirst
, 1);
2284 xfs_trans_brelse(tp
, agflbp
);
2285 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2286 agf
->agf_flfirst
= 0;
2288 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2289 be32_add_cpu(&agf
->agf_flcount
, -1);
2290 xfs_trans_agflist_delta(tp
, -1);
2291 pag
->pagf_flcount
--;
2294 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2296 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2297 pag
->pagf_btreeblks
++;
2298 logflags
|= XFS_AGF_BTREEBLKS
;
2301 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2308 * Log the given fields from the agf structure.
2312 xfs_trans_t
*tp
, /* transaction pointer */
2313 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2314 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2316 int first
; /* first byte offset */
2317 int last
; /* last byte offset */
2318 static const short offsets
[] = {
2319 offsetof(xfs_agf_t
, agf_magicnum
),
2320 offsetof(xfs_agf_t
, agf_versionnum
),
2321 offsetof(xfs_agf_t
, agf_seqno
),
2322 offsetof(xfs_agf_t
, agf_length
),
2323 offsetof(xfs_agf_t
, agf_roots
[0]),
2324 offsetof(xfs_agf_t
, agf_levels
[0]),
2325 offsetof(xfs_agf_t
, agf_flfirst
),
2326 offsetof(xfs_agf_t
, agf_fllast
),
2327 offsetof(xfs_agf_t
, agf_flcount
),
2328 offsetof(xfs_agf_t
, agf_freeblks
),
2329 offsetof(xfs_agf_t
, agf_longest
),
2330 offsetof(xfs_agf_t
, agf_btreeblks
),
2331 offsetof(xfs_agf_t
, agf_uuid
),
2332 offsetof(xfs_agf_t
, agf_rmap_blocks
),
2333 /* needed so that we don't log the whole rest of the structure: */
2334 offsetof(xfs_agf_t
, agf_spare64
),
2338 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2340 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2342 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2343 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2347 * Interface for inode allocation to force the pag data to be initialized.
2350 xfs_alloc_pagf_init(
2351 xfs_mount_t
*mp
, /* file system mount structure */
2352 xfs_trans_t
*tp
, /* transaction pointer */
2353 xfs_agnumber_t agno
, /* allocation group number */
2354 int flags
) /* XFS_ALLOC_FLAGS_... */
2359 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2362 xfs_trans_brelse(tp
, bp
);
2367 * Put the block on the freelist for the allocation group.
2370 xfs_alloc_put_freelist(
2371 xfs_trans_t
*tp
, /* transaction pointer */
2372 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2373 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2374 xfs_agblock_t bno
, /* block being freed */
2375 int btreeblk
) /* block came from a AGF btree */
2377 xfs_agf_t
*agf
; /* a.g. freespace structure */
2378 __be32
*blockp
;/* pointer to array entry */
2381 xfs_mount_t
*mp
; /* mount structure */
2382 xfs_perag_t
*pag
; /* per allocation group data */
2386 agf
= XFS_BUF_TO_AGF(agbp
);
2389 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2390 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2392 be32_add_cpu(&agf
->agf_fllast
, 1);
2393 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2394 agf
->agf_fllast
= 0;
2396 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2397 be32_add_cpu(&agf
->agf_flcount
, 1);
2398 xfs_trans_agflist_delta(tp
, 1);
2399 pag
->pagf_flcount
++;
2401 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2403 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2404 pag
->pagf_btreeblks
--;
2405 logflags
|= XFS_AGF_BTREEBLKS
;
2409 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2411 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2413 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2414 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2415 *blockp
= cpu_to_be32(bno
);
2416 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2418 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2420 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2421 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2422 startoff
+ sizeof(xfs_agblock_t
) - 1);
2428 struct xfs_mount
*mp
,
2431 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2433 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2434 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2436 if (!xfs_log_check_lsn(mp
,
2437 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2441 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2442 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2443 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2444 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2445 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2446 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
)))
2449 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2450 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2453 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2454 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) > XFS_BTREE_MAXLEVELS
)
2458 * during growfs operations, the perag is not fully initialised,
2459 * so we can't use it for any useful checking. growfs ensures we can't
2460 * use it by using uncached buffers that don't have the perag attached
2461 * so we can detect and avoid this problem.
2463 if (bp
->b_pag
&& be32_to_cpu(agf
->agf_seqno
) != bp
->b_pag
->pag_agno
)
2466 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2467 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2470 if (xfs_sb_version_hasreflink(&mp
->m_sb
) &&
2471 be32_to_cpu(agf
->agf_refcount_level
) > XFS_BTREE_MAXLEVELS
)
2479 xfs_agf_read_verify(
2482 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2484 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2485 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2486 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2487 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp
, bp
), mp
,
2488 XFS_ERRTAG_ALLOC_READ_AGF
,
2489 XFS_RANDOM_ALLOC_READ_AGF
))
2490 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2493 xfs_verifier_error(bp
);
2497 xfs_agf_write_verify(
2500 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2501 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2503 if (!xfs_agf_verify(mp
, bp
)) {
2504 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2505 xfs_verifier_error(bp
);
2509 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2513 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2515 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2518 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2520 .verify_read
= xfs_agf_read_verify
,
2521 .verify_write
= xfs_agf_write_verify
,
2525 * Read in the allocation group header (free/alloc section).
2529 struct xfs_mount
*mp
, /* mount point structure */
2530 struct xfs_trans
*tp
, /* transaction pointer */
2531 xfs_agnumber_t agno
, /* allocation group number */
2532 int flags
, /* XFS_BUF_ */
2533 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2537 trace_xfs_read_agf(mp
, agno
);
2539 ASSERT(agno
!= NULLAGNUMBER
);
2540 error
= xfs_trans_read_buf(
2541 mp
, tp
, mp
->m_ddev_targp
,
2542 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2543 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2549 ASSERT(!(*bpp
)->b_error
);
2550 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2555 * Read in the allocation group header (free/alloc section).
2559 struct xfs_mount
*mp
, /* mount point structure */
2560 struct xfs_trans
*tp
, /* transaction pointer */
2561 xfs_agnumber_t agno
, /* allocation group number */
2562 int flags
, /* XFS_ALLOC_FLAG_... */
2563 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2565 struct xfs_agf
*agf
; /* ag freelist header */
2566 struct xfs_perag
*pag
; /* per allocation group data */
2569 trace_xfs_alloc_read_agf(mp
, agno
);
2571 ASSERT(agno
!= NULLAGNUMBER
);
2572 error
= xfs_read_agf(mp
, tp
, agno
,
2573 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2579 ASSERT(!(*bpp
)->b_error
);
2581 agf
= XFS_BUF_TO_AGF(*bpp
);
2582 pag
= xfs_perag_get(mp
, agno
);
2583 if (!pag
->pagf_init
) {
2584 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2585 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2586 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2587 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2588 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2589 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2590 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2591 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2592 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] =
2593 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAPi
]);
2594 pag
->pagf_refcount_level
= be32_to_cpu(agf
->agf_refcount_level
);
2595 spin_lock_init(&pag
->pagb_lock
);
2596 pag
->pagb_count
= 0;
2597 /* XXX: pagb_tree doesn't exist in userspace */
2598 //pag->pagb_tree = RB_ROOT;
2602 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2603 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2604 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2605 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2606 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2607 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2608 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2609 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2610 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2618 * Allocate an extent (variable-size).
2619 * Depending on the allocation type, we either look in a single allocation
2620 * group or loop over the allocation groups to find the result.
2624 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2626 xfs_agblock_t agsize
; /* allocation group size */
2628 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2629 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2630 xfs_mount_t
*mp
; /* mount structure pointer */
2631 xfs_agnumber_t sagno
; /* starting allocation group number */
2632 xfs_alloctype_t type
; /* input allocation type */
2635 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2638 type
= args
->otype
= args
->type
;
2639 args
->agbno
= NULLAGBLOCK
;
2641 * Just fix this up, for the case where the last a.g. is shorter
2642 * (or there's only one a.g.) and the caller couldn't easily figure
2643 * that out (xfs_bmap_alloc).
2645 agsize
= mp
->m_sb
.sb_agblocks
;
2646 if (args
->maxlen
> agsize
)
2647 args
->maxlen
= agsize
;
2648 if (args
->alignment
== 0)
2649 args
->alignment
= 1;
2650 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2651 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2652 ASSERT(args
->minlen
<= args
->maxlen
);
2653 ASSERT(args
->minlen
<= agsize
);
2654 ASSERT(args
->mod
< args
->prod
);
2655 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2656 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2657 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2658 args
->mod
>= args
->prod
) {
2659 args
->fsbno
= NULLFSBLOCK
;
2660 trace_xfs_alloc_vextent_badargs(args
);
2663 minleft
= args
->minleft
;
2666 case XFS_ALLOCTYPE_THIS_AG
:
2667 case XFS_ALLOCTYPE_NEAR_BNO
:
2668 case XFS_ALLOCTYPE_THIS_BNO
:
2670 * These three force us into a single a.g.
2672 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2673 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2675 error
= xfs_alloc_fix_freelist(args
, 0);
2676 args
->minleft
= minleft
;
2678 trace_xfs_alloc_vextent_nofix(args
);
2682 trace_xfs_alloc_vextent_noagbp(args
);
2685 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2686 if ((error
= xfs_alloc_ag_vextent(args
)))
2689 case XFS_ALLOCTYPE_START_BNO
:
2691 * Try near allocation first, then anywhere-in-ag after
2692 * the first a.g. fails.
2694 if ((args
->datatype
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2695 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2696 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2697 ((mp
->m_agfrotor
/ rotorstep
) %
2698 mp
->m_sb
.sb_agcount
), 0);
2701 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2702 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2704 case XFS_ALLOCTYPE_ANY_AG
:
2705 case XFS_ALLOCTYPE_START_AG
:
2706 case XFS_ALLOCTYPE_FIRST_AG
:
2708 * Rotate through the allocation groups looking for a winner.
2710 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2712 * Start with the last place we left off.
2714 args
->agno
= sagno
= (mp
->m_agfrotor
/ rotorstep
) %
2715 mp
->m_sb
.sb_agcount
;
2716 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2717 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2718 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2720 * Start with allocation group given by bno.
2722 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2723 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2727 if (type
== XFS_ALLOCTYPE_START_AG
)
2728 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2730 * Start with the given allocation group.
2732 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2733 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2736 * Loop over allocation groups twice; first time with
2737 * trylock set, second time without.
2740 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2741 if (no_min
) args
->minleft
= 0;
2742 error
= xfs_alloc_fix_freelist(args
, flags
);
2743 args
->minleft
= minleft
;
2745 trace_xfs_alloc_vextent_nofix(args
);
2749 * If we get a buffer back then the allocation will fly.
2752 if ((error
= xfs_alloc_ag_vextent(args
)))
2757 trace_xfs_alloc_vextent_loopfailed(args
);
2760 * Didn't work, figure out the next iteration.
2762 if (args
->agno
== sagno
&&
2763 type
== XFS_ALLOCTYPE_START_BNO
)
2764 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2766 * For the first allocation, we can try any AG to get
2767 * space. However, if we already have allocated a
2768 * block, we don't want to try AGs whose number is below
2769 * sagno. Otherwise, we may end up with out-of-order
2770 * locking of AGF, which might cause deadlock.
2772 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2773 if (args
->firstblock
!= NULLFSBLOCK
)
2779 * Reached the starting a.g., must either be done
2780 * or switch to non-trylock mode.
2782 if (args
->agno
== sagno
) {
2784 args
->agbno
= NULLAGBLOCK
;
2785 trace_xfs_alloc_vextent_allfailed(args
);
2792 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2793 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2795 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2799 xfs_perag_put(args
->pag
);
2801 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
)) {
2802 if (args
->agno
== sagno
)
2803 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2804 (mp
->m_sb
.sb_agcount
* rotorstep
);
2806 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2807 (mp
->m_sb
.sb_agcount
* rotorstep
);
2814 if (args
->agbno
== NULLAGBLOCK
)
2815 args
->fsbno
= NULLFSBLOCK
;
2817 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2819 ASSERT(args
->len
>= args
->minlen
);
2820 ASSERT(args
->len
<= args
->maxlen
);
2821 ASSERT(args
->agbno
% args
->alignment
== 0);
2822 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2826 /* Zero the extent if we were asked to do so */
2827 if (args
->datatype
& XFS_ALLOC_USERDATA_ZERO
) {
2828 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2834 xfs_perag_put(args
->pag
);
2837 xfs_perag_put(args
->pag
);
2841 /* Ensure that the freelist is at full capacity. */
2843 xfs_free_extent_fix_freelist(
2844 struct xfs_trans
*tp
,
2845 xfs_agnumber_t agno
,
2846 struct xfs_buf
**agbp
)
2848 struct xfs_alloc_arg args
;
2851 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
2853 args
.mp
= tp
->t_mountp
;
2857 * validate that the block number is legal - the enables us to detect
2858 * and handle a silent filesystem corruption rather than crashing.
2860 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2861 return -EFSCORRUPTED
;
2863 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2866 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2872 xfs_perag_put(args
.pag
);
2878 * Just break up the extent address and hand off to xfs_free_ag_extent
2879 * after fixing up the freelist.
2883 struct xfs_trans
*tp
, /* transaction pointer */
2884 xfs_fsblock_t bno
, /* starting block number of extent */
2885 xfs_extlen_t len
, /* length of extent */
2886 struct xfs_owner_info
*oinfo
, /* extent owner */
2887 enum xfs_ag_resv_type type
) /* block reservation type */
2889 struct xfs_mount
*mp
= tp
->t_mountp
;
2890 struct xfs_buf
*agbp
;
2891 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, bno
);
2892 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
2896 ASSERT(type
!= XFS_AG_RESV_AGFL
);
2898 if (XFS_TEST_ERROR(false, mp
,
2899 XFS_ERRTAG_FREE_EXTENT
,
2900 XFS_RANDOM_FREE_EXTENT
))
2903 error
= xfs_free_extent_fix_freelist(tp
, agno
, &agbp
);
2907 XFS_WANT_CORRUPTED_GOTO(mp
, agbno
< mp
->m_sb
.sb_agblocks
, err
);
2909 /* validate the extent size is legal now we have the agf locked */
2910 XFS_WANT_CORRUPTED_GOTO(mp
,
2911 agbno
+ len
<= be32_to_cpu(XFS_BUF_TO_AGF(agbp
)->agf_length
),
2914 error
= xfs_free_ag_extent(tp
, agbp
, agno
, agbno
, len
, oinfo
, type
);
2918 xfs_extent_busy_insert(tp
, agno
, agbno
, len
, 0);
2922 xfs_trans_brelse(tp
, agbp
);