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;
63 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
64 * AGF buffer (PV 947395), we place constraints on the relationship among
65 * actual allocations for data blocks, freelist blocks, and potential file data
66 * bmap btree blocks. However, these restrictions may result in no actual space
67 * allocated for a delayed extent, for example, a data block in a certain AG is
68 * allocated but there is no additional block for the additional bmap btree
69 * block due to a split of the bmap btree of the file. The result of this may
70 * lead to an infinite loop when the file gets flushed to disk and all delayed
71 * extents need to be actually allocated. To get around this, we explicitly set
72 * aside a few blocks which will not be reserved in delayed allocation.
74 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
75 * potential split of the file's bmap btree.
83 blocks
= 4 + (mp
->m_sb
.sb_agcount
* XFS_ALLOC_AGFL_RESERVE
);
88 * When deciding how much space to allocate out of an AG, we limit the
89 * allocation maximum size to the size the AG. However, we cannot use all the
90 * blocks in the AG - some are permanently used by metadata. These
91 * blocks are generally:
92 * - the AG superblock, AGF, AGI and AGFL
93 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
94 * the AGI free inode and rmap btree root blocks.
95 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
96 * - the rmapbt root block
98 * The AG headers are sector sized, so the amount of space they take up is
99 * dependent on filesystem geometry. The others are all single blocks.
102 xfs_alloc_ag_max_usable(
103 struct xfs_mount
*mp
)
107 blocks
= XFS_BB_TO_FSB(mp
, XFS_FSS_TO_BB(mp
, 4)); /* ag headers */
108 blocks
+= XFS_ALLOC_AGFL_RESERVE
;
109 blocks
+= 3; /* AGF, AGI btree root blocks */
110 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
111 blocks
++; /* finobt root block */
112 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
113 blocks
++; /* rmap root block */
115 return mp
->m_sb
.sb_agblocks
- blocks
;
119 * Lookup the record equal to [bno, len] in the btree given by cur.
121 STATIC
int /* error */
123 struct xfs_btree_cur
*cur
, /* btree cursor */
124 xfs_agblock_t bno
, /* starting block of extent */
125 xfs_extlen_t len
, /* length of extent */
126 int *stat
) /* success/failure */
128 cur
->bc_rec
.a
.ar_startblock
= bno
;
129 cur
->bc_rec
.a
.ar_blockcount
= len
;
130 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
134 * Lookup the first record greater than or equal to [bno, len]
135 * in the btree given by cur.
139 struct xfs_btree_cur
*cur
, /* btree cursor */
140 xfs_agblock_t bno
, /* starting block of extent */
141 xfs_extlen_t len
, /* length of extent */
142 int *stat
) /* success/failure */
144 cur
->bc_rec
.a
.ar_startblock
= bno
;
145 cur
->bc_rec
.a
.ar_blockcount
= len
;
146 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
150 * Lookup the first record less than or equal to [bno, len]
151 * in the btree given by cur.
153 static int /* error */
155 struct xfs_btree_cur
*cur
, /* btree cursor */
156 xfs_agblock_t bno
, /* starting block of extent */
157 xfs_extlen_t len
, /* length of extent */
158 int *stat
) /* success/failure */
160 cur
->bc_rec
.a
.ar_startblock
= bno
;
161 cur
->bc_rec
.a
.ar_blockcount
= len
;
162 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
166 * Update the record referred to by cur to the value given
168 * This either works (return 0) or gets an EFSCORRUPTED error.
170 STATIC
int /* error */
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 */
176 union xfs_btree_rec rec
;
178 rec
.alloc
.ar_startblock
= cpu_to_be32(bno
);
179 rec
.alloc
.ar_blockcount
= cpu_to_be32(len
);
180 return xfs_btree_update(cur
, &rec
);
184 * Get the data from the pointed-to record.
188 struct xfs_btree_cur
*cur
, /* btree cursor */
189 xfs_agblock_t
*bno
, /* output: starting block of extent */
190 xfs_extlen_t
*len
, /* output: length of extent */
191 int *stat
) /* output: success/failure */
193 union xfs_btree_rec
*rec
;
196 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
197 if (!error
&& *stat
== 1) {
198 *bno
= be32_to_cpu(rec
->alloc
.ar_startblock
);
199 *len
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
205 * Compute aligned version of the found extent.
206 * Takes alignment and min length into account.
209 xfs_alloc_compute_aligned(
210 xfs_alloc_arg_t
*args
, /* allocation argument structure */
211 xfs_agblock_t foundbno
, /* starting block in found extent */
212 xfs_extlen_t foundlen
, /* length in found extent */
213 xfs_agblock_t
*resbno
, /* result block number */
214 xfs_extlen_t
*reslen
) /* result length */
220 /* Trim busy sections out of found extent */
221 xfs_extent_busy_trim(args
, foundbno
, foundlen
, &bno
, &len
);
224 * If we have a largish extent that happens to start before min_agbno,
225 * see if we can shift it into range...
227 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
228 diff
= args
->min_agbno
- bno
;
235 if (args
->alignment
> 1 && len
>= args
->minlen
) {
236 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
238 diff
= aligned_bno
- bno
;
240 *resbno
= aligned_bno
;
241 *reslen
= diff
>= len
? 0 : len
- diff
;
249 * Compute best start block and diff for "near" allocations.
250 * freelen >= wantlen already checked by caller.
252 STATIC xfs_extlen_t
/* difference value (absolute) */
253 xfs_alloc_compute_diff(
254 xfs_agblock_t wantbno
, /* target starting block */
255 xfs_extlen_t wantlen
, /* target length */
256 xfs_extlen_t alignment
, /* target alignment */
257 char userdata
, /* are we allocating data? */
258 xfs_agblock_t freebno
, /* freespace's starting block */
259 xfs_extlen_t freelen
, /* freespace's length */
260 xfs_agblock_t
*newbnop
) /* result: best start block from free */
262 xfs_agblock_t freeend
; /* end of freespace extent */
263 xfs_agblock_t newbno1
; /* return block number */
264 xfs_agblock_t newbno2
; /* other new block number */
265 xfs_extlen_t newlen1
=0; /* length with newbno1 */
266 xfs_extlen_t newlen2
=0; /* length with newbno2 */
267 xfs_agblock_t wantend
; /* end of target extent */
269 ASSERT(freelen
>= wantlen
);
270 freeend
= freebno
+ freelen
;
271 wantend
= wantbno
+ wantlen
;
273 * We want to allocate from the start of a free extent if it is past
274 * the desired block or if we are allocating user data and the free
275 * extent is before desired block. The second case is there to allow
276 * for contiguous allocation from the remaining free space if the file
277 * grows in the short term.
279 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
280 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
281 newbno1
= NULLAGBLOCK
;
282 } else if (freeend
>= wantend
&& alignment
> 1) {
283 newbno1
= roundup(wantbno
, alignment
);
284 newbno2
= newbno1
- alignment
;
285 if (newbno1
>= freeend
)
286 newbno1
= NULLAGBLOCK
;
288 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
289 if (newbno2
< freebno
)
290 newbno2
= NULLAGBLOCK
;
292 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
293 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
294 if (newlen1
< newlen2
||
295 (newlen1
== newlen2
&&
296 XFS_ABSDIFF(newbno1
, wantbno
) >
297 XFS_ABSDIFF(newbno2
, wantbno
)))
299 } else if (newbno2
!= NULLAGBLOCK
)
301 } else if (freeend
>= wantend
) {
303 } else if (alignment
> 1) {
304 newbno1
= roundup(freeend
- wantlen
, alignment
);
305 if (newbno1
> freeend
- wantlen
&&
306 newbno1
- alignment
>= freebno
)
307 newbno1
-= alignment
;
308 else if (newbno1
>= freeend
)
309 newbno1
= NULLAGBLOCK
;
311 newbno1
= freeend
- wantlen
;
313 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
317 * Fix up the length, based on mod and prod.
318 * len should be k * prod + mod for some k.
319 * If len is too small it is returned unchanged.
320 * If len hits maxlen it is left alone.
324 xfs_alloc_arg_t
*args
) /* allocation argument structure */
329 ASSERT(args
->mod
< args
->prod
);
331 ASSERT(rlen
>= args
->minlen
);
332 ASSERT(rlen
<= args
->maxlen
);
333 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
334 (args
->mod
== 0 && rlen
< args
->prod
))
336 k
= rlen
% args
->prod
;
340 rlen
= rlen
- (k
- args
->mod
);
342 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
343 /* casts to (int) catch length underflows */
344 if ((int)rlen
< (int)args
->minlen
)
346 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
347 ASSERT(rlen
% args
->prod
== args
->mod
);
352 * Fix up length if there is too little space left in the a.g.
353 * Return 1 if ok, 0 if too little, should give up.
356 xfs_alloc_fix_minleft(
357 xfs_alloc_arg_t
*args
) /* allocation argument structure */
359 xfs_agf_t
*agf
; /* a.g. freelist header */
360 int diff
; /* free space difference */
362 if (args
->minleft
== 0)
364 agf
= XFS_BUF_TO_AGF(args
->agbp
);
365 diff
= be32_to_cpu(agf
->agf_freeblks
)
366 - args
->len
- args
->minleft
;
369 args
->len
+= diff
; /* shrink the allocated space */
370 /* casts to (int) catch length underflows */
371 if ((int)args
->len
>= (int)args
->minlen
)
373 args
->agbno
= NULLAGBLOCK
;
378 * Update the two btrees, logically removing from freespace the extent
379 * starting at rbno, rlen blocks. The extent is contained within the
380 * actual (current) free extent fbno for flen blocks.
381 * Flags are passed in indicating whether the cursors are set to the
384 STATIC
int /* error code */
385 xfs_alloc_fixup_trees(
386 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
387 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
388 xfs_agblock_t fbno
, /* starting block of free extent */
389 xfs_extlen_t flen
, /* length of free extent */
390 xfs_agblock_t rbno
, /* starting block of returned extent */
391 xfs_extlen_t rlen
, /* length of returned extent */
392 int flags
) /* flags, XFSA_FIXUP_... */
394 int error
; /* error code */
395 int i
; /* operation results */
396 xfs_agblock_t nfbno1
; /* first new free startblock */
397 xfs_agblock_t nfbno2
; /* second new free startblock */
398 xfs_extlen_t nflen1
=0; /* first new free length */
399 xfs_extlen_t nflen2
=0; /* second new free length */
400 struct xfs_mount
*mp
;
405 * Look up the record in the by-size tree if necessary.
407 if (flags
& XFSA_FIXUP_CNT_OK
) {
409 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
411 XFS_WANT_CORRUPTED_RETURN(mp
,
412 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
415 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
417 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
420 * Look up the record in the by-block tree if necessary.
422 if (flags
& XFSA_FIXUP_BNO_OK
) {
424 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
426 XFS_WANT_CORRUPTED_RETURN(mp
,
427 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
430 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
432 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
436 if (bno_cur
->bc_nlevels
== 1 && cnt_cur
->bc_nlevels
== 1) {
437 struct xfs_btree_block
*bnoblock
;
438 struct xfs_btree_block
*cntblock
;
440 bnoblock
= XFS_BUF_TO_BLOCK(bno_cur
->bc_bufs
[0]);
441 cntblock
= XFS_BUF_TO_BLOCK(cnt_cur
->bc_bufs
[0]);
443 XFS_WANT_CORRUPTED_RETURN(mp
,
444 bnoblock
->bb_numrecs
== cntblock
->bb_numrecs
);
449 * Deal with all four cases: the allocated record is contained
450 * within the freespace record, so we can have new freespace
451 * at either (or both) end, or no freespace remaining.
453 if (rbno
== fbno
&& rlen
== flen
)
454 nfbno1
= nfbno2
= NULLAGBLOCK
;
455 else if (rbno
== fbno
) {
456 nfbno1
= rbno
+ rlen
;
457 nflen1
= flen
- rlen
;
458 nfbno2
= NULLAGBLOCK
;
459 } else if (rbno
+ rlen
== fbno
+ flen
) {
461 nflen1
= flen
- rlen
;
462 nfbno2
= NULLAGBLOCK
;
465 nflen1
= rbno
- fbno
;
466 nfbno2
= rbno
+ rlen
;
467 nflen2
= (fbno
+ flen
) - nfbno2
;
470 * Delete the entry from the by-size btree.
472 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
474 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
476 * Add new by-size btree entry(s).
478 if (nfbno1
!= NULLAGBLOCK
) {
479 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
481 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
482 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
484 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
486 if (nfbno2
!= NULLAGBLOCK
) {
487 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
489 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
490 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
492 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
495 * Fix up the by-block btree entry(s).
497 if (nfbno1
== NULLAGBLOCK
) {
499 * No remaining freespace, just delete the by-block tree entry.
501 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
503 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
506 * Update the by-block entry to start later|be shorter.
508 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
511 if (nfbno2
!= NULLAGBLOCK
) {
513 * 2 resulting free entries, need to add one.
515 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
517 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
518 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
520 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
529 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
530 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
533 if (!uuid_equal(&agfl
->agfl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
535 if (be32_to_cpu(agfl
->agfl_magicnum
) != XFS_AGFL_MAGIC
)
538 * during growfs operations, the perag is not fully initialised,
539 * so we can't use it for any useful checking. growfs ensures we can't
540 * use it by using uncached buffers that don't have the perag attached
541 * so we can detect and avoid this problem.
543 if (bp
->b_pag
&& be32_to_cpu(agfl
->agfl_seqno
) != bp
->b_pag
->pag_agno
)
546 for (i
= 0; i
< XFS_AGFL_SIZE(mp
); i
++) {
547 if (be32_to_cpu(agfl
->agfl_bno
[i
]) != NULLAGBLOCK
&&
548 be32_to_cpu(agfl
->agfl_bno
[i
]) >= mp
->m_sb
.sb_agblocks
)
552 return xfs_log_check_lsn(mp
,
553 be64_to_cpu(XFS_BUF_TO_AGFL(bp
)->agfl_lsn
));
557 xfs_agfl_read_verify(
560 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
563 * There is no verification of non-crc AGFLs because mkfs does not
564 * initialise the AGFL to zero or NULL. Hence the only valid part of the
565 * AGFL is what the AGF says is active. We can't get to the AGF, so we
566 * can't verify just those entries are valid.
568 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
571 if (!xfs_buf_verify_cksum(bp
, XFS_AGFL_CRC_OFF
))
572 xfs_buf_ioerror(bp
, -EFSBADCRC
);
573 else if (!xfs_agfl_verify(bp
))
574 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
577 xfs_verifier_error(bp
);
581 xfs_agfl_write_verify(
584 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
585 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
587 /* no verification of non-crc AGFLs */
588 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
591 if (!xfs_agfl_verify(bp
)) {
592 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
593 xfs_verifier_error(bp
);
598 XFS_BUF_TO_AGFL(bp
)->agfl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
600 xfs_buf_update_cksum(bp
, XFS_AGFL_CRC_OFF
);
603 const struct xfs_buf_ops xfs_agfl_buf_ops
= {
605 .verify_read
= xfs_agfl_read_verify
,
606 .verify_write
= xfs_agfl_write_verify
,
610 * Read in the allocation group free block array.
612 STATIC
int /* error */
614 xfs_mount_t
*mp
, /* mount point structure */
615 xfs_trans_t
*tp
, /* transaction pointer */
616 xfs_agnumber_t agno
, /* allocation group number */
617 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
619 xfs_buf_t
*bp
; /* return value */
622 ASSERT(agno
!= NULLAGNUMBER
);
623 error
= xfs_trans_read_buf(
624 mp
, tp
, mp
->m_ddev_targp
,
625 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
626 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_agfl_buf_ops
);
629 xfs_buf_set_ref(bp
, XFS_AGFL_REF
);
635 xfs_alloc_update_counters(
636 struct xfs_trans
*tp
,
637 struct xfs_perag
*pag
,
638 struct xfs_buf
*agbp
,
641 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
643 pag
->pagf_freeblks
+= len
;
644 be32_add_cpu(&agf
->agf_freeblks
, len
);
646 xfs_trans_agblocks_delta(tp
, len
);
647 if (unlikely(be32_to_cpu(agf
->agf_freeblks
) >
648 be32_to_cpu(agf
->agf_length
)))
649 return -EFSCORRUPTED
;
651 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
656 * Allocation group level functions.
660 * Allocate a variable extent in the allocation group agno.
661 * Type and bno are used to determine where in the allocation group the
663 * Extent's length (returned in *len) will be between minlen and maxlen,
664 * and of the form k * prod + mod unless there's nothing that large.
665 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
667 STATIC
int /* error */
668 xfs_alloc_ag_vextent(
669 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
672 xfs_extlen_t reservation
;
675 ASSERT(args
->minlen
> 0);
676 ASSERT(args
->maxlen
> 0);
677 ASSERT(args
->minlen
<= args
->maxlen
);
678 ASSERT(args
->mod
< args
->prod
);
679 ASSERT(args
->alignment
> 0);
682 * Clamp maxlen to the amount of free space minus any reservations
683 * that have been made.
685 oldmax
= args
->maxlen
;
686 reservation
= xfs_ag_resv_needed(args
->pag
, args
->resv
);
687 if (args
->maxlen
> args
->pag
->pagf_freeblks
- reservation
)
688 args
->maxlen
= args
->pag
->pagf_freeblks
- reservation
;
689 if (args
->maxlen
== 0) {
690 args
->agbno
= NULLAGBLOCK
;
691 args
->maxlen
= oldmax
;
696 * Branch to correct routine based on the type.
699 switch (args
->type
) {
700 case XFS_ALLOCTYPE_THIS_AG
:
701 error
= xfs_alloc_ag_vextent_size(args
);
703 case XFS_ALLOCTYPE_NEAR_BNO
:
704 error
= xfs_alloc_ag_vextent_near(args
);
706 case XFS_ALLOCTYPE_THIS_BNO
:
707 error
= xfs_alloc_ag_vextent_exact(args
);
714 args
->maxlen
= oldmax
;
716 if (error
|| args
->agbno
== NULLAGBLOCK
)
719 ASSERT(args
->len
>= args
->minlen
);
720 ASSERT(args
->len
<= args
->maxlen
);
721 ASSERT(!args
->wasfromfl
|| args
->resv
!= XFS_AG_RESV_AGFL
);
722 ASSERT(args
->agbno
% args
->alignment
== 0);
724 /* if not file data, insert new block into the reverse map btree */
725 if (args
->oinfo
.oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
726 error
= xfs_rmap_alloc(args
->tp
, args
->agbp
, args
->agno
,
727 args
->agbno
, args
->len
, &args
->oinfo
);
732 if (!args
->wasfromfl
) {
733 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
735 -((long)(args
->len
)));
739 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
740 args
->agbno
, args
->len
));
743 xfs_ag_resv_alloc_extent(args
->pag
, args
->resv
, args
);
745 XFS_STATS_INC(args
->mp
, xs_allocx
);
746 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
751 * Allocate a variable extent at exactly agno/bno.
752 * Extent's length (returned in *len) will be between minlen and maxlen,
753 * and of the form k * prod + mod unless there's nothing that large.
754 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
756 STATIC
int /* error */
757 xfs_alloc_ag_vextent_exact(
758 xfs_alloc_arg_t
*args
) /* allocation argument structure */
760 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
761 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
763 xfs_agblock_t fbno
; /* start block of found extent */
764 xfs_extlen_t flen
; /* length of found extent */
765 xfs_agblock_t tbno
; /* start block of trimmed extent */
766 xfs_extlen_t tlen
; /* length of trimmed extent */
767 xfs_agblock_t tend
; /* end block of trimmed extent */
768 int i
; /* success/failure of operation */
770 ASSERT(args
->alignment
== 1);
773 * Allocate/initialize a cursor for the by-number freespace btree.
775 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
776 args
->agno
, XFS_BTNUM_BNO
);
779 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
780 * Look for the closest free block <= bno, it must contain bno
781 * if any free block does.
783 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
790 * Grab the freespace record.
792 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
795 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
796 ASSERT(fbno
<= args
->agbno
);
799 * Check for overlapping busy extents.
801 xfs_extent_busy_trim(args
, fbno
, flen
, &tbno
, &tlen
);
804 * Give up if the start of the extent is busy, or the freespace isn't
805 * long enough for the minimum request.
807 if (tbno
> args
->agbno
)
809 if (tlen
< args
->minlen
)
812 if (tend
< args
->agbno
+ args
->minlen
)
816 * End of extent will be smaller of the freespace end and the
817 * maximal requested end.
819 * Fix the length according to mod and prod if given.
821 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
823 xfs_alloc_fix_len(args
);
824 if (!xfs_alloc_fix_minleft(args
))
827 ASSERT(args
->agbno
+ args
->len
<= tend
);
830 * We are allocating agbno for args->len
831 * Allocate/initialize a cursor for the by-size btree.
833 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
834 args
->agno
, XFS_BTNUM_CNT
);
835 ASSERT(args
->agbno
+ args
->len
<=
836 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
837 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
838 args
->len
, XFSA_FIXUP_BNO_OK
);
840 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
844 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
845 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
848 trace_xfs_alloc_exact_done(args
);
852 /* Didn't find it, return null. */
853 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
854 args
->agbno
= NULLAGBLOCK
;
855 trace_xfs_alloc_exact_notfound(args
);
859 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
860 trace_xfs_alloc_exact_error(args
);
865 * Search the btree in a given direction via the search cursor and compare
866 * the records found against the good extent we've already found.
869 xfs_alloc_find_best_extent(
870 struct xfs_alloc_arg
*args
, /* allocation argument structure */
871 struct xfs_btree_cur
**gcur
, /* good cursor */
872 struct xfs_btree_cur
**scur
, /* searching cursor */
873 xfs_agblock_t gdiff
, /* difference for search comparison */
874 xfs_agblock_t
*sbno
, /* extent found by search */
875 xfs_extlen_t
*slen
, /* extent length */
876 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
877 xfs_extlen_t
*slena
, /* aligned extent length */
878 int dir
) /* 0 = search right, 1 = search left */
885 /* The good extent is perfect, no need to search. */
890 * Look until we find a better one, run out of space or run off the end.
893 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
896 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
897 xfs_alloc_compute_aligned(args
, *sbno
, *slen
, sbnoa
, slena
);
900 * The good extent is closer than this one.
903 if (*sbnoa
> args
->max_agbno
)
905 if (*sbnoa
>= args
->agbno
+ gdiff
)
908 if (*sbnoa
< args
->min_agbno
)
910 if (*sbnoa
<= args
->agbno
- gdiff
)
915 * Same distance, compare length and pick the best.
917 if (*slena
>= args
->minlen
) {
918 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
919 xfs_alloc_fix_len(args
);
921 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
923 args
->userdata
, *sbnoa
,
927 * Choose closer size and invalidate other cursor.
935 error
= xfs_btree_increment(*scur
, 0, &i
);
937 error
= xfs_btree_decrement(*scur
, 0, &i
);
943 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
948 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
953 /* caller invalidates cursors */
958 * Allocate a variable extent near bno in the allocation group agno.
959 * Extent's length (returned in len) will be between minlen and maxlen,
960 * and of the form k * prod + mod unless there's nothing that large.
961 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
963 STATIC
int /* error */
964 xfs_alloc_ag_vextent_near(
965 xfs_alloc_arg_t
*args
) /* allocation argument structure */
967 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
968 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
969 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
970 xfs_agblock_t gtbno
; /* start bno of right side entry */
971 xfs_agblock_t gtbnoa
; /* aligned ... */
972 xfs_extlen_t gtdiff
; /* difference to right side entry */
973 xfs_extlen_t gtlen
; /* length of right side entry */
974 xfs_extlen_t gtlena
; /* aligned ... */
975 xfs_agblock_t gtnew
; /* useful start bno of right side */
976 int error
; /* error code */
977 int i
; /* result code, temporary */
978 int j
; /* result code, temporary */
979 xfs_agblock_t ltbno
; /* start bno of left side entry */
980 xfs_agblock_t ltbnoa
; /* aligned ... */
981 xfs_extlen_t ltdiff
; /* difference to left side entry */
982 xfs_extlen_t ltlen
; /* length of left side entry */
983 xfs_extlen_t ltlena
; /* aligned ... */
984 xfs_agblock_t ltnew
; /* useful start bno of left side */
985 xfs_extlen_t rlen
; /* length of returned extent */
989 * Randomly don't execute the first algorithm.
991 int dofirst
; /* set to do first algorithm */
993 dofirst
= prandom_u32() & 1;
996 /* handle unitialized agbno range so caller doesn't have to */
997 if (!args
->min_agbno
&& !args
->max_agbno
)
998 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
999 ASSERT(args
->min_agbno
<= args
->max_agbno
);
1001 /* clamp agbno to the range if it's outside */
1002 if (args
->agbno
< args
->min_agbno
)
1003 args
->agbno
= args
->min_agbno
;
1004 if (args
->agbno
> args
->max_agbno
)
1005 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 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
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
->userdata
, 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
));
1132 if (!xfs_alloc_fix_minleft(args
)) {
1133 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1134 trace_xfs_alloc_near_nominleft(args
);
1139 * We are allocating starting at bnew for blen blocks.
1142 ASSERT(bnew
>= ltbno
);
1143 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1145 * Set up a cursor for the by-bno tree.
1147 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1148 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1150 * Fix up the btree entries.
1152 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1153 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1155 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1156 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1158 trace_xfs_alloc_near_first(args
);
1163 * Search in the by-bno tree to the left and to the right
1164 * simultaneously, until in each case we find a space big enough,
1165 * or run into the edge of the tree. When we run into the edge,
1166 * we deallocate that cursor.
1167 * If both searches succeed, we compare the two spaces and pick
1169 * With alignment, it's possible for both to fail; the upper
1170 * level algorithm that picks allocation groups for allocations
1171 * is not supposed to do this.
1174 * Allocate and initialize the cursor for the leftward search.
1176 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1177 args
->agno
, XFS_BTNUM_BNO
);
1179 * Lookup <= bno to find the leftward search's starting point.
1181 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1185 * Didn't find anything; use this cursor for the rightward
1188 bno_cur_gt
= bno_cur_lt
;
1192 * Found something. Duplicate the cursor for the rightward search.
1194 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1197 * Increment the cursor, so we will point at the entry just right
1198 * of the leftward entry if any, or to the leftmost entry.
1200 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1204 * It failed, there are no rightward entries.
1206 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1210 * Loop going left with the leftward cursor, right with the
1211 * rightward cursor, until either both directions give up or
1212 * we find an entry at least as big as minlen.
1216 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1218 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1219 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1221 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1223 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1225 if (!i
|| ltbnoa
< args
->min_agbno
) {
1226 xfs_btree_del_cursor(bno_cur_lt
,
1232 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1234 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1235 xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1237 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1239 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1241 if (!i
|| gtbnoa
> args
->max_agbno
) {
1242 xfs_btree_del_cursor(bno_cur_gt
,
1247 } while (bno_cur_lt
|| bno_cur_gt
);
1250 * Got both cursors still active, need to find better entry.
1252 if (bno_cur_lt
&& bno_cur_gt
) {
1253 if (ltlena
>= args
->minlen
) {
1255 * Left side is good, look for a right side entry.
1257 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1258 xfs_alloc_fix_len(args
);
1259 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1260 args
->alignment
, args
->userdata
, ltbnoa
,
1263 error
= xfs_alloc_find_best_extent(args
,
1264 &bno_cur_lt
, &bno_cur_gt
,
1265 ltdiff
, >bno
, >len
,
1267 0 /* search right */);
1269 ASSERT(gtlena
>= args
->minlen
);
1272 * Right side is good, look for a left side entry.
1274 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1275 xfs_alloc_fix_len(args
);
1276 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1277 args
->alignment
, args
->userdata
, gtbnoa
,
1280 error
= xfs_alloc_find_best_extent(args
,
1281 &bno_cur_gt
, &bno_cur_lt
,
1282 gtdiff
, <bno
, <len
,
1284 1 /* search left */);
1292 * If we couldn't get anything, give up.
1294 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1295 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1298 trace_xfs_alloc_near_busy(args
);
1299 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1302 trace_xfs_alloc_size_neither(args
);
1303 args
->agbno
= NULLAGBLOCK
;
1308 * At this point we have selected a freespace entry, either to the
1309 * left or to the right. If it's on the right, copy all the
1310 * useful variables to the "left" set so we only have one
1311 * copy of this code.
1314 bno_cur_lt
= bno_cur_gt
;
1325 * Fix up the length and compute the useful address.
1327 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1328 xfs_alloc_fix_len(args
);
1329 if (!xfs_alloc_fix_minleft(args
)) {
1330 trace_xfs_alloc_near_nominleft(args
);
1331 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1332 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1336 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1337 args
->userdata
, ltbnoa
, ltlena
, <new
);
1338 ASSERT(ltnew
>= ltbno
);
1339 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1340 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1341 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1342 args
->agbno
= ltnew
;
1344 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1345 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1349 trace_xfs_alloc_near_greater(args
);
1351 trace_xfs_alloc_near_lesser(args
);
1353 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1354 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1358 trace_xfs_alloc_near_error(args
);
1359 if (cnt_cur
!= NULL
)
1360 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1361 if (bno_cur_lt
!= NULL
)
1362 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1363 if (bno_cur_gt
!= NULL
)
1364 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1369 * Allocate a variable extent anywhere in the allocation group agno.
1370 * Extent's length (returned in len) will be between minlen and maxlen,
1371 * and of the form k * prod + mod unless there's nothing that large.
1372 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1374 STATIC
int /* error */
1375 xfs_alloc_ag_vextent_size(
1376 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1378 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1379 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1380 int error
; /* error result */
1381 xfs_agblock_t fbno
; /* start of found freespace */
1382 xfs_extlen_t flen
; /* length of found freespace */
1383 int i
; /* temp status variable */
1384 xfs_agblock_t rbno
; /* returned block number */
1385 xfs_extlen_t rlen
; /* length of returned extent */
1390 * Allocate and initialize a cursor for the by-size btree.
1392 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1393 args
->agno
, XFS_BTNUM_CNT
);
1397 * Look for an entry >= maxlen+alignment-1 blocks.
1399 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1400 args
->maxlen
+ args
->alignment
- 1, &i
)))
1404 * If none or we have busy extents that we cannot allocate from, then
1405 * we have to settle for a smaller extent. In the case that there are
1406 * no large extents, this will return the last entry in the tree unless
1407 * the tree is empty. In the case that there are only busy large
1408 * extents, this will return the largest small extent unless there
1409 * are no smaller extents available.
1411 if (!i
|| forced
> 1) {
1412 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1416 if (i
== 0 || flen
== 0) {
1417 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1418 trace_xfs_alloc_size_noentry(args
);
1422 xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
, &rlen
);
1425 * Search for a non-busy extent that is large enough.
1426 * If we are at low space, don't check, or if we fall of
1427 * the end of the btree, turn off the busy check and
1431 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1434 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1436 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1439 if (rlen
>= args
->maxlen
)
1442 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1447 * Our only valid extents must have been busy.
1448 * Make it unbusy by forcing the log out and
1449 * retrying. If we've been here before, forcing
1450 * the log isn't making the extents available,
1451 * which means they have probably been freed in
1452 * this transaction. In that case, we have to
1453 * give up on them and we'll attempt a minlen
1454 * allocation the next time around.
1456 xfs_btree_del_cursor(cnt_cur
,
1458 trace_xfs_alloc_size_busy(args
);
1460 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1467 * In the first case above, we got the last entry in the
1468 * by-size btree. Now we check to see if the space hits maxlen
1469 * once aligned; if not, we search left for something better.
1470 * This can't happen in the second case above.
1472 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1473 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1474 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1475 if (rlen
< args
->maxlen
) {
1476 xfs_agblock_t bestfbno
;
1477 xfs_extlen_t bestflen
;
1478 xfs_agblock_t bestrbno
;
1479 xfs_extlen_t bestrlen
;
1486 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1490 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1493 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1494 if (flen
< bestrlen
)
1496 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1498 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1499 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1500 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1502 if (rlen
> bestrlen
) {
1507 if (rlen
== args
->maxlen
)
1511 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1514 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1520 args
->wasfromfl
= 0;
1522 * Fix up the length.
1525 if (rlen
< args
->minlen
) {
1527 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1528 trace_xfs_alloc_size_busy(args
);
1529 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1534 xfs_alloc_fix_len(args
);
1536 if (!xfs_alloc_fix_minleft(args
))
1539 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1541 * Allocate and initialize a cursor for the by-block tree.
1543 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1544 args
->agno
, XFS_BTNUM_BNO
);
1545 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1546 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1548 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1549 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1550 cnt_cur
= bno_cur
= NULL
;
1553 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1554 args
->agbno
+ args
->len
<=
1555 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1557 trace_xfs_alloc_size_done(args
);
1561 trace_xfs_alloc_size_error(args
);
1563 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1565 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1569 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1570 trace_xfs_alloc_size_nominleft(args
);
1571 args
->agbno
= NULLAGBLOCK
;
1576 * Deal with the case where only small freespaces remain.
1577 * Either return the contents of the last freespace record,
1578 * or allocate space from the freelist if there is nothing in the tree.
1580 STATIC
int /* error */
1581 xfs_alloc_ag_vextent_small(
1582 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1583 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1584 xfs_agblock_t
*fbnop
, /* result block number */
1585 xfs_extlen_t
*flenp
, /* result length */
1586 int *stat
) /* status: 0-freelist, 1-normal/none */
1588 struct xfs_owner_info oinfo
;
1589 struct xfs_perag
*pag
;
1595 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1598 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1600 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1603 * Nothing in the btree, try the freelist. Make sure
1604 * to respect minleft even when pulling from the
1607 else if (args
->minlen
== 1 && args
->alignment
== 1 &&
1608 args
->resv
!= XFS_AG_RESV_AGFL
&&
1609 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1611 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1614 if (fbno
!= NULLAGBLOCK
) {
1615 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1618 if (args
->userdata
) {
1621 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1622 args
->agno
, fbno
, 0);
1623 xfs_trans_binval(args
->tp
, bp
);
1627 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1628 args
->agbno
+ args
->len
<=
1629 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1631 args
->wasfromfl
= 1;
1632 trace_xfs_alloc_small_freelist(args
);
1635 * If we're feeding an AGFL block to something that
1636 * doesn't live in the free space, we need to clear
1637 * out the OWN_AG rmap and add the block back to
1638 * the AGFL per-AG reservation.
1640 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_AG
);
1641 error
= xfs_rmap_free(args
->tp
, args
->agbp
, args
->agno
,
1645 pag
= xfs_perag_get(args
->mp
, args
->agno
);
1646 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_AGFL
,
1654 * Nothing in the freelist.
1660 * Can't allocate from the freelist for some reason.
1667 * Can't do the allocation, give up.
1669 if (flen
< args
->minlen
) {
1670 args
->agbno
= NULLAGBLOCK
;
1671 trace_xfs_alloc_small_notenough(args
);
1677 trace_xfs_alloc_small_done(args
);
1681 trace_xfs_alloc_small_error(args
);
1686 * Free the extent starting at agno/bno for length.
1692 xfs_agnumber_t agno
,
1695 struct xfs_owner_info
*oinfo
,
1696 enum xfs_ag_resv_type type
)
1698 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1699 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1700 int error
; /* error return value */
1701 xfs_agblock_t gtbno
; /* start of right neighbor block */
1702 xfs_extlen_t gtlen
; /* length of right neighbor block */
1703 int haveleft
; /* have a left neighbor block */
1704 int haveright
; /* have a right neighbor block */
1705 int i
; /* temp, result code */
1706 xfs_agblock_t ltbno
; /* start of left neighbor block */
1707 xfs_extlen_t ltlen
; /* length of left neighbor block */
1708 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1709 xfs_agblock_t nbno
; /* new starting block of freespace */
1710 xfs_extlen_t nlen
; /* new length of freespace */
1711 xfs_perag_t
*pag
; /* per allocation group data */
1713 bno_cur
= cnt_cur
= NULL
;
1716 if (oinfo
->oi_owner
!= XFS_RMAP_OWN_UNKNOWN
) {
1717 error
= xfs_rmap_free(tp
, agbp
, agno
, bno
, len
, oinfo
);
1723 * Allocate and initialize a cursor for the by-block btree.
1725 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1727 * Look for a neighboring block on the left (lower block numbers)
1728 * that is contiguous with this space.
1730 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1734 * There is a block to our left.
1736 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1738 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1740 * It's not contiguous, though.
1742 if (ltbno
+ ltlen
< bno
)
1746 * If this failure happens the request to free this
1747 * space was invalid, it's (partly) already free.
1750 XFS_WANT_CORRUPTED_GOTO(mp
,
1751 ltbno
+ ltlen
<= bno
, error0
);
1755 * Look for a neighboring block on the right (higher block numbers)
1756 * that is contiguous with this space.
1758 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1762 * There is a block to our right.
1764 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1766 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1768 * It's not contiguous, though.
1770 if (bno
+ len
< gtbno
)
1774 * If this failure happens the request to free this
1775 * space was invalid, it's (partly) already free.
1778 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1782 * Now allocate and initialize a cursor for the by-size tree.
1784 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1786 * Have both left and right contiguous neighbors.
1787 * Merge all three into a single free block.
1789 if (haveleft
&& haveright
) {
1791 * Delete the old by-size entry on the left.
1793 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1795 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1796 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1798 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1800 * Delete the old by-size entry on the right.
1802 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1804 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1805 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1807 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1809 * Delete the old by-block entry for the right block.
1811 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1813 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1815 * Move the by-block cursor back to the left neighbor.
1817 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1819 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1822 * Check that this is the right record: delete didn't
1823 * mangle the cursor.
1826 xfs_agblock_t xxbno
;
1829 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1832 XFS_WANT_CORRUPTED_GOTO(mp
,
1833 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1838 * Update remaining by-block entry to the new, joined block.
1841 nlen
= len
+ ltlen
+ gtlen
;
1842 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1846 * Have only a left contiguous neighbor.
1847 * Merge it together with the new freespace.
1849 else if (haveleft
) {
1851 * Delete the old by-size entry on the left.
1853 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1855 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1856 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1858 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1860 * Back up the by-block cursor to the left neighbor, and
1861 * update its length.
1863 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1865 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1868 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1872 * Have only a right contiguous neighbor.
1873 * Merge it together with the new freespace.
1875 else if (haveright
) {
1877 * Delete the old by-size entry on the right.
1879 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1882 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1884 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1886 * Update the starting block and length of the right
1887 * neighbor in the by-block tree.
1891 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1895 * No contiguous neighbors.
1896 * Insert the new freespace into the by-block tree.
1901 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1903 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1905 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1908 * In all cases we need to insert the new freespace in the by-size tree.
1910 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1912 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1913 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1915 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1916 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1920 * Update the freespace totals in the ag and superblock.
1922 pag
= xfs_perag_get(mp
, agno
);
1923 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1924 xfs_ag_resv_free_extent(pag
, type
, tp
, len
);
1929 XFS_STATS_INC(mp
, xs_freex
);
1930 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1932 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1933 haveleft
, haveright
);
1938 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1941 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1943 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1948 * Visible (exported) allocation/free functions.
1949 * Some of these are used just by xfs_alloc_btree.c and this file.
1953 * Compute and fill in value of m_ag_maxlevels.
1956 xfs_alloc_compute_maxlevels(
1957 xfs_mount_t
*mp
) /* file system mount structure */
1959 mp
->m_ag_maxlevels
= xfs_btree_compute_maxlevels(mp
, mp
->m_alloc_mnr
,
1960 (mp
->m_sb
.sb_agblocks
+ 1) / 2);
1964 * Find the length of the longest extent in an AG. The 'need' parameter
1965 * specifies how much space we're going to need for the AGFL and the
1966 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1970 xfs_alloc_longest_free_extent(
1971 struct xfs_mount
*mp
,
1972 struct xfs_perag
*pag
,
1974 xfs_extlen_t reserved
)
1976 xfs_extlen_t delta
= 0;
1979 * If the AGFL needs a recharge, we'll have to subtract that from the
1982 if (need
> pag
->pagf_flcount
)
1983 delta
= need
- pag
->pagf_flcount
;
1986 * If we cannot maintain others' reservations with space from the
1987 * not-longest freesp extents, we'll have to subtract /that/ from
1988 * the longest extent too.
1990 if (pag
->pagf_freeblks
- pag
->pagf_longest
< reserved
)
1991 delta
+= reserved
- (pag
->pagf_freeblks
- pag
->pagf_longest
);
1994 * If the longest extent is long enough to satisfy all the
1995 * reservations and AGFL rules in place, we can return this extent.
1997 if (pag
->pagf_longest
> delta
)
1998 return pag
->pagf_longest
- delta
;
2000 /* Otherwise, let the caller try for 1 block if there's space. */
2001 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
2005 xfs_alloc_min_freelist(
2006 struct xfs_mount
*mp
,
2007 struct xfs_perag
*pag
)
2009 unsigned int min_free
;
2011 /* space needed by-bno freespace btree */
2012 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
2013 mp
->m_ag_maxlevels
);
2014 /* space needed by-size freespace btree */
2015 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
2016 mp
->m_ag_maxlevels
);
2017 /* space needed reverse mapping used space btree */
2018 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
2019 min_free
+= min_t(unsigned int,
2020 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] + 1,
2021 mp
->m_rmap_maxlevels
);
2027 * Check if the operation we are fixing up the freelist for should go ahead or
2028 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2029 * is dependent on whether the size and shape of free space available will
2030 * permit the requested allocation to take place.
2033 xfs_alloc_space_available(
2034 struct xfs_alloc_arg
*args
,
2035 xfs_extlen_t min_free
,
2038 struct xfs_perag
*pag
= args
->pag
;
2039 xfs_extlen_t longest
;
2040 xfs_extlen_t reservation
; /* blocks that are still reserved */
2043 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2046 reservation
= xfs_ag_resv_needed(pag
, args
->resv
);
2048 /* do we have enough contiguous free space for the allocation? */
2049 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
,
2051 if ((args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1) > longest
)
2054 /* do we have enough free space remaining for the allocation? */
2055 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
2056 reservation
- min_free
- args
->total
);
2057 if (available
< (int)args
->minleft
|| available
<= 0)
2064 * Decide whether to use this allocation group for this allocation.
2065 * If so, fix up the btree freelist's size.
2068 xfs_alloc_fix_freelist(
2069 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2070 int flags
) /* XFS_ALLOC_FLAG_... */
2072 struct xfs_mount
*mp
= args
->mp
;
2073 struct xfs_perag
*pag
= args
->pag
;
2074 struct xfs_trans
*tp
= args
->tp
;
2075 struct xfs_buf
*agbp
= NULL
;
2076 struct xfs_buf
*agflbp
= NULL
;
2077 struct xfs_alloc_arg targs
; /* local allocation arguments */
2078 xfs_agblock_t bno
; /* freelist block */
2079 xfs_extlen_t need
; /* total blocks needed in freelist */
2082 if (!pag
->pagf_init
) {
2083 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2086 if (!pag
->pagf_init
) {
2087 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2088 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2089 goto out_agbp_relse
;
2094 * If this is a metadata preferred pag and we are user data then try
2095 * somewhere else if we are not being asked to try harder at this
2098 if (pag
->pagf_metadata
&& args
->userdata
&&
2099 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2100 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2101 goto out_agbp_relse
;
2104 need
= xfs_alloc_min_freelist(mp
, pag
);
2105 if (!xfs_alloc_space_available(args
, need
, flags
))
2106 goto out_agbp_relse
;
2109 * Get the a.g. freespace buffer.
2110 * Can fail if we're not blocking on locks, and it's held.
2113 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2117 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2118 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2123 /* If there isn't enough total space or single-extent, reject it. */
2124 need
= xfs_alloc_min_freelist(mp
, pag
);
2125 if (!xfs_alloc_space_available(args
, need
, flags
))
2126 goto out_agbp_relse
;
2129 * Make the freelist shorter if it's too long.
2131 * Note that from this point onwards, we will always release the agf and
2132 * agfl buffers on error. This handles the case where we error out and
2133 * the buffers are clean or may not have been joined to the transaction
2134 * and hence need to be released manually. If they have been joined to
2135 * the transaction, then xfs_trans_brelse() will handle them
2136 * appropriately based on the recursion count and dirty state of the
2139 * XXX (dgc): When we have lots of free space, does this buy us
2140 * anything other than extra overhead when we need to put more blocks
2141 * back on the free list? Maybe we should only do this when space is
2142 * getting low or the AGFL is more than half full?
2144 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2145 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2146 * updating the rmapbt. Both flags are used in xfs_repair while we're
2147 * rebuilding the rmapbt, and neither are used by the kernel. They're
2148 * both required to ensure that rmaps are correctly recorded for the
2149 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2150 * repair/rmap.c in xfsprogs for details.
2152 memset(&targs
, 0, sizeof(targs
));
2153 if (flags
& XFS_ALLOC_FLAG_NORMAP
)
2154 xfs_rmap_skip_owner_update(&targs
.oinfo
);
2156 xfs_rmap_ag_owner(&targs
.oinfo
, XFS_RMAP_OWN_AG
);
2157 while (!(flags
& XFS_ALLOC_FLAG_NOSHRINK
) && pag
->pagf_flcount
> need
) {
2160 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2162 goto out_agbp_relse
;
2163 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1,
2164 &targs
.oinfo
, XFS_AG_RESV_AGFL
);
2166 goto out_agbp_relse
;
2167 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2168 xfs_trans_binval(tp
, bp
);
2174 targs
.agno
= args
->agno
;
2175 targs
.alignment
= targs
.minlen
= targs
.prod
= 1;
2176 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2178 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2180 goto out_agbp_relse
;
2182 /* Make the freelist longer if it's too short. */
2183 while (pag
->pagf_flcount
< need
) {
2185 targs
.maxlen
= need
- pag
->pagf_flcount
;
2186 targs
.resv
= XFS_AG_RESV_AGFL
;
2188 /* Allocate as many blocks as possible at once. */
2189 error
= xfs_alloc_ag_vextent(&targs
);
2191 goto out_agflbp_relse
;
2194 * Stop if we run out. Won't happen if callers are obeying
2195 * the restrictions correctly. Can happen for free calls
2196 * on a completely full ag.
2198 if (targs
.agbno
== NULLAGBLOCK
) {
2199 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2201 goto out_agflbp_relse
;
2204 * Put each allocated block on the list.
2206 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2207 error
= xfs_alloc_put_freelist(tp
, agbp
,
2210 goto out_agflbp_relse
;
2213 xfs_trans_brelse(tp
, agflbp
);
2218 xfs_trans_brelse(tp
, agflbp
);
2221 xfs_trans_brelse(tp
, agbp
);
2228 * Get a block from the freelist.
2229 * Returns with the buffer for the block gotten.
2232 xfs_alloc_get_freelist(
2233 xfs_trans_t
*tp
, /* transaction pointer */
2234 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2235 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2236 int btreeblk
) /* destination is a AGF btree */
2238 xfs_agf_t
*agf
; /* a.g. freespace structure */
2239 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2240 xfs_agblock_t bno
; /* block number returned */
2244 xfs_mount_t
*mp
= tp
->t_mountp
;
2245 xfs_perag_t
*pag
; /* per allocation group data */
2248 * Freelist is empty, give up.
2250 agf
= XFS_BUF_TO_AGF(agbp
);
2251 if (!agf
->agf_flcount
) {
2252 *bnop
= NULLAGBLOCK
;
2256 * Read the array of free blocks.
2258 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2265 * Get the block number and update the data structures.
2267 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2268 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2269 be32_add_cpu(&agf
->agf_flfirst
, 1);
2270 xfs_trans_brelse(tp
, agflbp
);
2271 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2272 agf
->agf_flfirst
= 0;
2274 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2275 be32_add_cpu(&agf
->agf_flcount
, -1);
2276 xfs_trans_agflist_delta(tp
, -1);
2277 pag
->pagf_flcount
--;
2280 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2282 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2283 pag
->pagf_btreeblks
++;
2284 logflags
|= XFS_AGF_BTREEBLKS
;
2287 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2294 * Log the given fields from the agf structure.
2298 xfs_trans_t
*tp
, /* transaction pointer */
2299 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2300 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2302 int first
; /* first byte offset */
2303 int last
; /* last byte offset */
2304 static const short offsets
[] = {
2305 offsetof(xfs_agf_t
, agf_magicnum
),
2306 offsetof(xfs_agf_t
, agf_versionnum
),
2307 offsetof(xfs_agf_t
, agf_seqno
),
2308 offsetof(xfs_agf_t
, agf_length
),
2309 offsetof(xfs_agf_t
, agf_roots
[0]),
2310 offsetof(xfs_agf_t
, agf_levels
[0]),
2311 offsetof(xfs_agf_t
, agf_flfirst
),
2312 offsetof(xfs_agf_t
, agf_fllast
),
2313 offsetof(xfs_agf_t
, agf_flcount
),
2314 offsetof(xfs_agf_t
, agf_freeblks
),
2315 offsetof(xfs_agf_t
, agf_longest
),
2316 offsetof(xfs_agf_t
, agf_btreeblks
),
2317 offsetof(xfs_agf_t
, agf_uuid
),
2318 offsetof(xfs_agf_t
, agf_rmap_blocks
),
2319 /* needed so that we don't log the whole rest of the structure: */
2320 offsetof(xfs_agf_t
, agf_spare64
),
2324 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2326 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2328 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2329 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2333 * Interface for inode allocation to force the pag data to be initialized.
2336 xfs_alloc_pagf_init(
2337 xfs_mount_t
*mp
, /* file system mount structure */
2338 xfs_trans_t
*tp
, /* transaction pointer */
2339 xfs_agnumber_t agno
, /* allocation group number */
2340 int flags
) /* XFS_ALLOC_FLAGS_... */
2345 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2348 xfs_trans_brelse(tp
, bp
);
2353 * Put the block on the freelist for the allocation group.
2356 xfs_alloc_put_freelist(
2357 xfs_trans_t
*tp
, /* transaction pointer */
2358 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2359 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2360 xfs_agblock_t bno
, /* block being freed */
2361 int btreeblk
) /* block came from a AGF btree */
2363 xfs_agf_t
*agf
; /* a.g. freespace structure */
2364 __be32
*blockp
;/* pointer to array entry */
2367 xfs_mount_t
*mp
; /* mount structure */
2368 xfs_perag_t
*pag
; /* per allocation group data */
2372 agf
= XFS_BUF_TO_AGF(agbp
);
2375 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2376 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2378 be32_add_cpu(&agf
->agf_fllast
, 1);
2379 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2380 agf
->agf_fllast
= 0;
2382 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2383 be32_add_cpu(&agf
->agf_flcount
, 1);
2384 xfs_trans_agflist_delta(tp
, 1);
2385 pag
->pagf_flcount
++;
2387 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2389 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2390 pag
->pagf_btreeblks
--;
2391 logflags
|= XFS_AGF_BTREEBLKS
;
2395 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2397 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2399 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2400 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2401 *blockp
= cpu_to_be32(bno
);
2402 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2404 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2406 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2407 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2408 startoff
+ sizeof(xfs_agblock_t
) - 1);
2414 struct xfs_mount
*mp
,
2417 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2419 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2420 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2422 if (!xfs_log_check_lsn(mp
,
2423 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2427 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2428 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2429 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2430 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2431 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2432 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
)))
2435 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2436 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2439 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2440 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) > XFS_BTREE_MAXLEVELS
)
2444 * during growfs operations, the perag is not fully initialised,
2445 * so we can't use it for any useful checking. growfs ensures we can't
2446 * use it by using uncached buffers that don't have the perag attached
2447 * so we can detect and avoid this problem.
2449 if (bp
->b_pag
&& be32_to_cpu(agf
->agf_seqno
) != bp
->b_pag
->pag_agno
)
2452 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2453 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2461 xfs_agf_read_verify(
2464 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2466 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2467 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2468 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2469 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp
, bp
), mp
,
2470 XFS_ERRTAG_ALLOC_READ_AGF
,
2471 XFS_RANDOM_ALLOC_READ_AGF
))
2472 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2475 xfs_verifier_error(bp
);
2479 xfs_agf_write_verify(
2482 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2483 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2485 if (!xfs_agf_verify(mp
, bp
)) {
2486 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2487 xfs_verifier_error(bp
);
2491 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2495 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2497 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2500 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2502 .verify_read
= xfs_agf_read_verify
,
2503 .verify_write
= xfs_agf_write_verify
,
2507 * Read in the allocation group header (free/alloc section).
2511 struct xfs_mount
*mp
, /* mount point structure */
2512 struct xfs_trans
*tp
, /* transaction pointer */
2513 xfs_agnumber_t agno
, /* allocation group number */
2514 int flags
, /* XFS_BUF_ */
2515 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2519 trace_xfs_read_agf(mp
, agno
);
2521 ASSERT(agno
!= NULLAGNUMBER
);
2522 error
= xfs_trans_read_buf(
2523 mp
, tp
, mp
->m_ddev_targp
,
2524 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2525 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2531 ASSERT(!(*bpp
)->b_error
);
2532 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2537 * Read in the allocation group header (free/alloc section).
2541 struct xfs_mount
*mp
, /* mount point structure */
2542 struct xfs_trans
*tp
, /* transaction pointer */
2543 xfs_agnumber_t agno
, /* allocation group number */
2544 int flags
, /* XFS_ALLOC_FLAG_... */
2545 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2547 struct xfs_agf
*agf
; /* ag freelist header */
2548 struct xfs_perag
*pag
; /* per allocation group data */
2551 trace_xfs_alloc_read_agf(mp
, agno
);
2553 ASSERT(agno
!= NULLAGNUMBER
);
2554 error
= xfs_read_agf(mp
, tp
, agno
,
2555 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2561 ASSERT(!(*bpp
)->b_error
);
2563 agf
= XFS_BUF_TO_AGF(*bpp
);
2564 pag
= xfs_perag_get(mp
, agno
);
2565 if (!pag
->pagf_init
) {
2566 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2567 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2568 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2569 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2570 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2571 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2572 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2573 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2574 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] =
2575 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAPi
]);
2576 spin_lock_init(&pag
->pagb_lock
);
2577 pag
->pagb_count
= 0;
2578 /* XXX: pagb_tree doesn't exist in userspace */
2579 //pag->pagb_tree = RB_ROOT;
2583 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2584 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2585 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2586 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2587 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2588 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2589 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2590 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2591 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2599 * Allocate an extent (variable-size).
2600 * Depending on the allocation type, we either look in a single allocation
2601 * group or loop over the allocation groups to find the result.
2605 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2607 xfs_agblock_t agsize
; /* allocation group size */
2609 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2610 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2611 xfs_mount_t
*mp
; /* mount structure pointer */
2612 xfs_agnumber_t sagno
; /* starting allocation group number */
2613 xfs_alloctype_t type
; /* input allocation type */
2616 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2619 type
= args
->otype
= args
->type
;
2620 args
->agbno
= NULLAGBLOCK
;
2622 * Just fix this up, for the case where the last a.g. is shorter
2623 * (or there's only one a.g.) and the caller couldn't easily figure
2624 * that out (xfs_bmap_alloc).
2626 agsize
= mp
->m_sb
.sb_agblocks
;
2627 if (args
->maxlen
> agsize
)
2628 args
->maxlen
= agsize
;
2629 if (args
->alignment
== 0)
2630 args
->alignment
= 1;
2631 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2632 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2633 ASSERT(args
->minlen
<= args
->maxlen
);
2634 ASSERT(args
->minlen
<= agsize
);
2635 ASSERT(args
->mod
< args
->prod
);
2636 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2637 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2638 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2639 args
->mod
>= args
->prod
) {
2640 args
->fsbno
= NULLFSBLOCK
;
2641 trace_xfs_alloc_vextent_badargs(args
);
2644 minleft
= args
->minleft
;
2647 case XFS_ALLOCTYPE_THIS_AG
:
2648 case XFS_ALLOCTYPE_NEAR_BNO
:
2649 case XFS_ALLOCTYPE_THIS_BNO
:
2651 * These three force us into a single a.g.
2653 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2654 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2656 error
= xfs_alloc_fix_freelist(args
, 0);
2657 args
->minleft
= minleft
;
2659 trace_xfs_alloc_vextent_nofix(args
);
2663 trace_xfs_alloc_vextent_noagbp(args
);
2666 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2667 if ((error
= xfs_alloc_ag_vextent(args
)))
2670 case XFS_ALLOCTYPE_START_BNO
:
2672 * Try near allocation first, then anywhere-in-ag after
2673 * the first a.g. fails.
2675 if ((args
->userdata
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2676 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2677 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2678 ((mp
->m_agfrotor
/ rotorstep
) %
2679 mp
->m_sb
.sb_agcount
), 0);
2682 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2683 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2685 case XFS_ALLOCTYPE_ANY_AG
:
2686 case XFS_ALLOCTYPE_START_AG
:
2687 case XFS_ALLOCTYPE_FIRST_AG
:
2689 * Rotate through the allocation groups looking for a winner.
2691 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2693 * Start with the last place we left off.
2695 args
->agno
= sagno
= (mp
->m_agfrotor
/ rotorstep
) %
2696 mp
->m_sb
.sb_agcount
;
2697 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2698 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2699 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2701 * Start with allocation group given by bno.
2703 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2704 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2708 if (type
== XFS_ALLOCTYPE_START_AG
)
2709 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2711 * Start with the given allocation group.
2713 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2714 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2717 * Loop over allocation groups twice; first time with
2718 * trylock set, second time without.
2721 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2722 if (no_min
) args
->minleft
= 0;
2723 error
= xfs_alloc_fix_freelist(args
, flags
);
2724 args
->minleft
= minleft
;
2726 trace_xfs_alloc_vextent_nofix(args
);
2730 * If we get a buffer back then the allocation will fly.
2733 if ((error
= xfs_alloc_ag_vextent(args
)))
2738 trace_xfs_alloc_vextent_loopfailed(args
);
2741 * Didn't work, figure out the next iteration.
2743 if (args
->agno
== sagno
&&
2744 type
== XFS_ALLOCTYPE_START_BNO
)
2745 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2747 * For the first allocation, we can try any AG to get
2748 * space. However, if we already have allocated a
2749 * block, we don't want to try AGs whose number is below
2750 * sagno. Otherwise, we may end up with out-of-order
2751 * locking of AGF, which might cause deadlock.
2753 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2754 if (args
->firstblock
!= NULLFSBLOCK
)
2760 * Reached the starting a.g., must either be done
2761 * or switch to non-trylock mode.
2763 if (args
->agno
== sagno
) {
2765 args
->agbno
= NULLAGBLOCK
;
2766 trace_xfs_alloc_vextent_allfailed(args
);
2773 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2774 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2776 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2780 xfs_perag_put(args
->pag
);
2782 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
)) {
2783 if (args
->agno
== sagno
)
2784 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2785 (mp
->m_sb
.sb_agcount
* rotorstep
);
2787 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2788 (mp
->m_sb
.sb_agcount
* rotorstep
);
2795 if (args
->agbno
== NULLAGBLOCK
)
2796 args
->fsbno
= NULLFSBLOCK
;
2798 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2800 ASSERT(args
->len
>= args
->minlen
);
2801 ASSERT(args
->len
<= args
->maxlen
);
2802 ASSERT(args
->agbno
% args
->alignment
== 0);
2803 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2807 /* Zero the extent if we were asked to do so */
2808 if (args
->userdata
& XFS_ALLOC_USERDATA_ZERO
) {
2809 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2815 xfs_perag_put(args
->pag
);
2818 xfs_perag_put(args
->pag
);
2822 /* Ensure that the freelist is at full capacity. */
2824 xfs_free_extent_fix_freelist(
2825 struct xfs_trans
*tp
,
2826 xfs_agnumber_t agno
,
2827 struct xfs_buf
**agbp
)
2829 struct xfs_alloc_arg args
;
2832 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
2834 args
.mp
= tp
->t_mountp
;
2838 * validate that the block number is legal - the enables us to detect
2839 * and handle a silent filesystem corruption rather than crashing.
2841 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2842 return -EFSCORRUPTED
;
2844 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2847 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2853 xfs_perag_put(args
.pag
);
2859 * Just break up the extent address and hand off to xfs_free_ag_extent
2860 * after fixing up the freelist.
2864 struct xfs_trans
*tp
, /* transaction pointer */
2865 xfs_fsblock_t bno
, /* starting block number of extent */
2866 xfs_extlen_t len
, /* length of extent */
2867 struct xfs_owner_info
*oinfo
, /* extent owner */
2868 enum xfs_ag_resv_type type
) /* block reservation type */
2870 struct xfs_mount
*mp
= tp
->t_mountp
;
2871 struct xfs_buf
*agbp
;
2872 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, bno
);
2873 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
2877 ASSERT(type
!= XFS_AG_RESV_AGFL
);
2879 if (XFS_TEST_ERROR(false, mp
,
2880 XFS_ERRTAG_FREE_EXTENT
,
2881 XFS_RANDOM_FREE_EXTENT
))
2884 error
= xfs_free_extent_fix_freelist(tp
, agno
, &agbp
);
2888 XFS_WANT_CORRUPTED_GOTO(mp
, agbno
< mp
->m_sb
.sb_agblocks
, err
);
2890 /* validate the extent size is legal now we have the agf locked */
2891 XFS_WANT_CORRUPTED_GOTO(mp
,
2892 agbno
+ len
<= be32_to_cpu(XFS_BUF_TO_AGF(agbp
)->agf_length
),
2895 error
= xfs_free_ag_extent(tp
, agbp
, agno
, agbno
, len
, oinfo
, type
);
2899 xfs_extent_busy_insert(tp
, agno
, agbno
, len
, 0);
2903 xfs_trans_brelse(tp
, agbp
);