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_errortag.h"
34 #include "xfs_cksum.h"
35 #include "xfs_trace.h"
36 #include "xfs_trans.h"
37 #include "xfs_ag_resv.h"
39 struct workqueue_struct
*xfs_alloc_wq
;
41 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
43 #define XFSA_FIXUP_BNO_OK 1
44 #define XFSA_FIXUP_CNT_OK 2
46 STATIC
int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t
*);
47 STATIC
int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t
*);
48 STATIC
int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t
*);
49 STATIC
int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t
*,
50 xfs_btree_cur_t
*, xfs_agblock_t
*, xfs_extlen_t
*, int *);
56 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
57 return XFS_RMAP_BLOCK(mp
) + 1;
58 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
59 return XFS_FIBT_BLOCK(mp
) + 1;
60 return XFS_IBT_BLOCK(mp
) + 1;
67 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
68 return xfs_refc_block(mp
) + 1;
69 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
70 return XFS_RMAP_BLOCK(mp
) + 1;
71 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
72 return XFS_FIBT_BLOCK(mp
) + 1;
73 return XFS_IBT_BLOCK(mp
) + 1;
77 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
78 * AGF buffer (PV 947395), we place constraints on the relationship among
79 * actual allocations for data blocks, freelist blocks, and potential file data
80 * bmap btree blocks. However, these restrictions may result in no actual space
81 * allocated for a delayed extent, for example, a data block in a certain AG is
82 * allocated but there is no additional block for the additional bmap btree
83 * block due to a split of the bmap btree of the file. The result of this may
84 * lead to an infinite loop when the file gets flushed to disk and all delayed
85 * extents need to be actually allocated. To get around this, we explicitly set
86 * aside a few blocks which will not be reserved in delayed allocation.
88 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
89 * potential split of the file's bmap btree.
95 return mp
->m_sb
.sb_agcount
* (XFS_ALLOC_AGFL_RESERVE
+ 4);
99 * When deciding how much space to allocate out of an AG, we limit the
100 * allocation maximum size to the size the AG. However, we cannot use all the
101 * blocks in the AG - some are permanently used by metadata. These
102 * blocks are generally:
103 * - the AG superblock, AGF, AGI and AGFL
104 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
105 * the AGI free inode and rmap btree root blocks.
106 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
107 * - the rmapbt root block
109 * The AG headers are sector sized, so the amount of space they take up is
110 * dependent on filesystem geometry. The others are all single blocks.
113 xfs_alloc_ag_max_usable(
114 struct xfs_mount
*mp
)
118 blocks
= XFS_BB_TO_FSB(mp
, XFS_FSS_TO_BB(mp
, 4)); /* ag headers */
119 blocks
+= XFS_ALLOC_AGFL_RESERVE
;
120 blocks
+= 3; /* AGF, AGI btree root blocks */
121 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
122 blocks
++; /* finobt root block */
123 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
124 blocks
++; /* rmap root block */
125 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
126 blocks
++; /* refcount 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.
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 */
230 xfs_agblock_t bno
= foundbno
;
231 xfs_extlen_t len
= foundlen
;
235 /* Trim busy sections out of found extent */
236 busy
= xfs_extent_busy_trim(args
, &bno
, &len
, busy_gen
);
239 * If we have a largish extent that happens to start before min_agbno,
240 * see if we can shift it into range...
242 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
243 diff
= args
->min_agbno
- bno
;
250 if (args
->alignment
> 1 && len
>= args
->minlen
) {
251 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
253 diff
= aligned_bno
- bno
;
255 *resbno
= aligned_bno
;
256 *reslen
= diff
>= len
? 0 : len
- diff
;
266 * Compute best start block and diff for "near" allocations.
267 * freelen >= wantlen already checked by caller.
269 STATIC xfs_extlen_t
/* difference value (absolute) */
270 xfs_alloc_compute_diff(
271 xfs_agblock_t wantbno
, /* target starting block */
272 xfs_extlen_t wantlen
, /* target length */
273 xfs_extlen_t alignment
, /* target alignment */
274 int datatype
, /* are we allocating data? */
275 xfs_agblock_t freebno
, /* freespace's starting block */
276 xfs_extlen_t freelen
, /* freespace's length */
277 xfs_agblock_t
*newbnop
) /* result: best start block from free */
279 xfs_agblock_t freeend
; /* end of freespace extent */
280 xfs_agblock_t newbno1
; /* return block number */
281 xfs_agblock_t newbno2
; /* other new block number */
282 xfs_extlen_t newlen1
=0; /* length with newbno1 */
283 xfs_extlen_t newlen2
=0; /* length with newbno2 */
284 xfs_agblock_t wantend
; /* end of target extent */
285 bool userdata
= xfs_alloc_is_userdata(datatype
);
287 ASSERT(freelen
>= wantlen
);
288 freeend
= freebno
+ freelen
;
289 wantend
= wantbno
+ wantlen
;
291 * We want to allocate from the start of a free extent if it is past
292 * the desired block or if we are allocating user data and the free
293 * extent is before desired block. The second case is there to allow
294 * for contiguous allocation from the remaining free space if the file
295 * grows in the short term.
297 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
298 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
299 newbno1
= NULLAGBLOCK
;
300 } else if (freeend
>= wantend
&& alignment
> 1) {
301 newbno1
= roundup(wantbno
, alignment
);
302 newbno2
= newbno1
- alignment
;
303 if (newbno1
>= freeend
)
304 newbno1
= NULLAGBLOCK
;
306 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
307 if (newbno2
< freebno
)
308 newbno2
= NULLAGBLOCK
;
310 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
311 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
312 if (newlen1
< newlen2
||
313 (newlen1
== newlen2
&&
314 XFS_ABSDIFF(newbno1
, wantbno
) >
315 XFS_ABSDIFF(newbno2
, wantbno
)))
317 } else if (newbno2
!= NULLAGBLOCK
)
319 } else if (freeend
>= wantend
) {
321 } else if (alignment
> 1) {
322 newbno1
= roundup(freeend
- wantlen
, alignment
);
323 if (newbno1
> freeend
- wantlen
&&
324 newbno1
- alignment
>= freebno
)
325 newbno1
-= alignment
;
326 else if (newbno1
>= freeend
)
327 newbno1
= NULLAGBLOCK
;
329 newbno1
= freeend
- wantlen
;
331 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
335 * Fix up the length, based on mod and prod.
336 * len should be k * prod + mod for some k.
337 * If len is too small it is returned unchanged.
338 * If len hits maxlen it is left alone.
342 xfs_alloc_arg_t
*args
) /* allocation argument structure */
347 ASSERT(args
->mod
< args
->prod
);
349 ASSERT(rlen
>= args
->minlen
);
350 ASSERT(rlen
<= args
->maxlen
);
351 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
352 (args
->mod
== 0 && rlen
< args
->prod
))
354 k
= rlen
% args
->prod
;
358 rlen
= rlen
- (k
- args
->mod
);
360 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
361 /* casts to (int) catch length underflows */
362 if ((int)rlen
< (int)args
->minlen
)
364 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
365 ASSERT(rlen
% args
->prod
== args
->mod
);
366 ASSERT(args
->pag
->pagf_freeblks
+ args
->pag
->pagf_flcount
>=
367 rlen
+ args
->minleft
);
372 * Update the two btrees, logically removing from freespace the extent
373 * starting at rbno, rlen blocks. The extent is contained within the
374 * actual (current) free extent fbno for flen blocks.
375 * Flags are passed in indicating whether the cursors are set to the
378 STATIC
int /* error code */
379 xfs_alloc_fixup_trees(
380 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
381 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
382 xfs_agblock_t fbno
, /* starting block of free extent */
383 xfs_extlen_t flen
, /* length of free extent */
384 xfs_agblock_t rbno
, /* starting block of returned extent */
385 xfs_extlen_t rlen
, /* length of returned extent */
386 int flags
) /* flags, XFSA_FIXUP_... */
388 int error
; /* error code */
389 int i
; /* operation results */
390 xfs_agblock_t nfbno1
; /* first new free startblock */
391 xfs_agblock_t nfbno2
; /* second new free startblock */
392 xfs_extlen_t nflen1
=0; /* first new free length */
393 xfs_extlen_t nflen2
=0; /* second new free length */
394 struct xfs_mount
*mp
;
399 * Look up the record in the by-size tree if necessary.
401 if (flags
& XFSA_FIXUP_CNT_OK
) {
403 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
405 XFS_WANT_CORRUPTED_RETURN(mp
,
406 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
409 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
411 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
414 * Look up the record in the by-block tree if necessary.
416 if (flags
& XFSA_FIXUP_BNO_OK
) {
418 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
420 XFS_WANT_CORRUPTED_RETURN(mp
,
421 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
424 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
426 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
430 if (bno_cur
->bc_nlevels
== 1 && cnt_cur
->bc_nlevels
== 1) {
431 struct xfs_btree_block
*bnoblock
;
432 struct xfs_btree_block
*cntblock
;
434 bnoblock
= XFS_BUF_TO_BLOCK(bno_cur
->bc_bufs
[0]);
435 cntblock
= XFS_BUF_TO_BLOCK(cnt_cur
->bc_bufs
[0]);
437 XFS_WANT_CORRUPTED_RETURN(mp
,
438 bnoblock
->bb_numrecs
== cntblock
->bb_numrecs
);
443 * Deal with all four cases: the allocated record is contained
444 * within the freespace record, so we can have new freespace
445 * at either (or both) end, or no freespace remaining.
447 if (rbno
== fbno
&& rlen
== flen
)
448 nfbno1
= nfbno2
= NULLAGBLOCK
;
449 else if (rbno
== fbno
) {
450 nfbno1
= rbno
+ rlen
;
451 nflen1
= flen
- rlen
;
452 nfbno2
= NULLAGBLOCK
;
453 } else if (rbno
+ rlen
== fbno
+ flen
) {
455 nflen1
= flen
- rlen
;
456 nfbno2
= NULLAGBLOCK
;
459 nflen1
= rbno
- fbno
;
460 nfbno2
= rbno
+ rlen
;
461 nflen2
= (fbno
+ flen
) - nfbno2
;
464 * Delete the entry from the by-size btree.
466 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
468 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
470 * Add new by-size btree entry(s).
472 if (nfbno1
!= NULLAGBLOCK
) {
473 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
475 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
476 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
478 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
480 if (nfbno2
!= NULLAGBLOCK
) {
481 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
483 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
484 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
486 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
489 * Fix up the by-block btree entry(s).
491 if (nfbno1
== NULLAGBLOCK
) {
493 * No remaining freespace, just delete the by-block tree entry.
495 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
497 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
500 * Update the by-block entry to start later|be shorter.
502 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
505 if (nfbno2
!= NULLAGBLOCK
) {
507 * 2 resulting free entries, need to add one.
509 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
511 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
512 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
514 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
519 static xfs_failaddr_t
523 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
524 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
528 * There is no verification of non-crc AGFLs because mkfs does not
529 * initialise the AGFL to zero or NULL. Hence the only valid part of the
530 * AGFL is what the AGF says is active. We can't get to the AGF, so we
531 * can't verify just those entries are valid.
533 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
536 if (!uuid_equal(&agfl
->agfl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
537 return __this_address
;
538 if (be32_to_cpu(agfl
->agfl_magicnum
) != XFS_AGFL_MAGIC
)
539 return __this_address
;
541 * during growfs operations, the perag is not fully initialised,
542 * so we can't use it for any useful checking. growfs ensures we can't
543 * use it by using uncached buffers that don't have the perag attached
544 * so we can detect and avoid this problem.
546 if (bp
->b_pag
&& be32_to_cpu(agfl
->agfl_seqno
) != bp
->b_pag
->pag_agno
)
547 return __this_address
;
549 for (i
= 0; i
< XFS_AGFL_SIZE(mp
); i
++) {
550 if (be32_to_cpu(agfl
->agfl_bno
[i
]) != NULLAGBLOCK
&&
551 be32_to_cpu(agfl
->agfl_bno
[i
]) >= mp
->m_sb
.sb_agblocks
)
552 return __this_address
;
555 if (!xfs_log_check_lsn(mp
, be64_to_cpu(XFS_BUF_TO_AGFL(bp
)->agfl_lsn
)))
556 return __this_address
;
561 xfs_agfl_read_verify(
564 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
568 * There is no verification of non-crc AGFLs because mkfs does not
569 * initialise the AGFL to zero or NULL. Hence the only valid part of the
570 * AGFL is what the AGF says is active. We can't get to the AGF, so we
571 * can't verify just those entries are valid.
573 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
576 if (!xfs_buf_verify_cksum(bp
, XFS_AGFL_CRC_OFF
))
577 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
579 fa
= xfs_agfl_verify(bp
);
581 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
586 xfs_agfl_write_verify(
589 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
590 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
593 /* no verification of non-crc AGFLs */
594 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
597 fa
= xfs_agfl_verify(bp
);
599 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
604 XFS_BUF_TO_AGFL(bp
)->agfl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
606 xfs_buf_update_cksum(bp
, XFS_AGFL_CRC_OFF
);
609 const struct xfs_buf_ops xfs_agfl_buf_ops
= {
611 .verify_read
= xfs_agfl_read_verify
,
612 .verify_write
= xfs_agfl_write_verify
,
613 .verify_struct
= xfs_agfl_verify
,
617 * Read in the allocation group free block array.
621 xfs_mount_t
*mp
, /* mount point structure */
622 xfs_trans_t
*tp
, /* transaction pointer */
623 xfs_agnumber_t agno
, /* allocation group number */
624 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
626 xfs_buf_t
*bp
; /* return value */
629 ASSERT(agno
!= NULLAGNUMBER
);
630 error
= xfs_trans_read_buf(
631 mp
, tp
, mp
->m_ddev_targp
,
632 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
633 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_agfl_buf_ops
);
636 xfs_buf_set_ref(bp
, XFS_AGFL_REF
);
642 xfs_alloc_update_counters(
643 struct xfs_trans
*tp
,
644 struct xfs_perag
*pag
,
645 struct xfs_buf
*agbp
,
648 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
650 pag
->pagf_freeblks
+= len
;
651 be32_add_cpu(&agf
->agf_freeblks
, len
);
653 xfs_trans_agblocks_delta(tp
, len
);
654 if (unlikely(be32_to_cpu(agf
->agf_freeblks
) >
655 be32_to_cpu(agf
->agf_length
)))
656 return -EFSCORRUPTED
;
658 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
663 * Allocation group level functions.
667 * Allocate a variable extent in the allocation group agno.
668 * Type and bno are used to determine where in the allocation group the
670 * Extent's length (returned in *len) will be between minlen and maxlen,
671 * and of the form k * prod + mod unless there's nothing that large.
672 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
674 STATIC
int /* error */
675 xfs_alloc_ag_vextent(
676 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
680 ASSERT(args
->minlen
> 0);
681 ASSERT(args
->maxlen
> 0);
682 ASSERT(args
->minlen
<= args
->maxlen
);
683 ASSERT(args
->mod
< args
->prod
);
684 ASSERT(args
->alignment
> 0);
687 * Branch to correct routine based on the type.
690 switch (args
->type
) {
691 case XFS_ALLOCTYPE_THIS_AG
:
692 error
= xfs_alloc_ag_vextent_size(args
);
694 case XFS_ALLOCTYPE_NEAR_BNO
:
695 error
= xfs_alloc_ag_vextent_near(args
);
697 case XFS_ALLOCTYPE_THIS_BNO
:
698 error
= xfs_alloc_ag_vextent_exact(args
);
705 if (error
|| args
->agbno
== NULLAGBLOCK
)
708 ASSERT(args
->len
>= args
->minlen
);
709 ASSERT(args
->len
<= args
->maxlen
);
710 ASSERT(!args
->wasfromfl
|| args
->resv
!= XFS_AG_RESV_AGFL
);
711 ASSERT(args
->agbno
% args
->alignment
== 0);
713 /* if not file data, insert new block into the reverse map btree */
714 if (!xfs_rmap_should_skip_owner_update(&args
->oinfo
)) {
715 error
= xfs_rmap_alloc(args
->tp
, args
->agbp
, args
->agno
,
716 args
->agbno
, args
->len
, &args
->oinfo
);
721 if (!args
->wasfromfl
) {
722 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
724 -((long)(args
->len
)));
728 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
729 args
->agbno
, args
->len
));
732 xfs_ag_resv_alloc_extent(args
->pag
, args
->resv
, args
);
734 XFS_STATS_INC(args
->mp
, xs_allocx
);
735 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
740 * Allocate a variable extent at exactly agno/bno.
741 * Extent's length (returned in *len) will be between minlen and maxlen,
742 * and of the form k * prod + mod unless there's nothing that large.
743 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
745 STATIC
int /* error */
746 xfs_alloc_ag_vextent_exact(
747 xfs_alloc_arg_t
*args
) /* allocation argument structure */
749 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
750 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
752 xfs_agblock_t fbno
; /* start block of found extent */
753 xfs_extlen_t flen
; /* length of found extent */
754 xfs_agblock_t tbno
; /* start block of busy extent */
755 xfs_extlen_t tlen
; /* length of busy extent */
756 xfs_agblock_t tend
; /* end block of busy extent */
757 int i
; /* success/failure of operation */
760 ASSERT(args
->alignment
== 1);
763 * Allocate/initialize a cursor for the by-number freespace btree.
765 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
766 args
->agno
, XFS_BTNUM_BNO
);
769 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
770 * Look for the closest free block <= bno, it must contain bno
771 * if any free block does.
773 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
780 * Grab the freespace record.
782 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
785 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
786 ASSERT(fbno
<= args
->agbno
);
789 * Check for overlapping busy extents.
793 xfs_extent_busy_trim(args
, &tbno
, &tlen
, &busy_gen
);
796 * Give up if the start of the extent is busy, or the freespace isn't
797 * long enough for the minimum request.
799 if (tbno
> args
->agbno
)
801 if (tlen
< args
->minlen
)
804 if (tend
< args
->agbno
+ args
->minlen
)
808 * End of extent will be smaller of the freespace end and the
809 * maximal requested end.
811 * Fix the length according to mod and prod if given.
813 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
815 xfs_alloc_fix_len(args
);
816 ASSERT(args
->agbno
+ args
->len
<= tend
);
819 * We are allocating agbno for args->len
820 * Allocate/initialize a cursor for the by-size btree.
822 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
823 args
->agno
, XFS_BTNUM_CNT
);
824 ASSERT(args
->agbno
+ args
->len
<=
825 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
826 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
827 args
->len
, XFSA_FIXUP_BNO_OK
);
829 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
833 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
834 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
837 trace_xfs_alloc_exact_done(args
);
841 /* Didn't find it, return null. */
842 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
843 args
->agbno
= NULLAGBLOCK
;
844 trace_xfs_alloc_exact_notfound(args
);
848 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
849 trace_xfs_alloc_exact_error(args
);
854 * Search the btree in a given direction via the search cursor and compare
855 * the records found against the good extent we've already found.
858 xfs_alloc_find_best_extent(
859 struct xfs_alloc_arg
*args
, /* allocation argument structure */
860 struct xfs_btree_cur
**gcur
, /* good cursor */
861 struct xfs_btree_cur
**scur
, /* searching cursor */
862 xfs_agblock_t gdiff
, /* difference for search comparison */
863 xfs_agblock_t
*sbno
, /* extent found by search */
864 xfs_extlen_t
*slen
, /* extent length */
865 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
866 xfs_extlen_t
*slena
, /* aligned extent length */
867 int dir
) /* 0 = search right, 1 = search left */
875 /* The good extent is perfect, no need to search. */
880 * Look until we find a better one, run out of space or run off the end.
883 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
886 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
887 xfs_alloc_compute_aligned(args
, *sbno
, *slen
,
888 sbnoa
, slena
, &busy_gen
);
891 * The good extent is closer than this one.
894 if (*sbnoa
> args
->max_agbno
)
896 if (*sbnoa
>= args
->agbno
+ gdiff
)
899 if (*sbnoa
< args
->min_agbno
)
901 if (*sbnoa
<= args
->agbno
- gdiff
)
906 * Same distance, compare length and pick the best.
908 if (*slena
>= args
->minlen
) {
909 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
910 xfs_alloc_fix_len(args
);
912 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
914 args
->datatype
, *sbnoa
,
918 * Choose closer size and invalidate other cursor.
926 error
= xfs_btree_increment(*scur
, 0, &i
);
928 error
= xfs_btree_decrement(*scur
, 0, &i
);
934 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
939 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
944 /* caller invalidates cursors */
949 * Allocate a variable extent near bno in the allocation group agno.
950 * Extent's length (returned in len) will be between minlen and maxlen,
951 * and of the form k * prod + mod unless there's nothing that large.
952 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
954 STATIC
int /* error */
955 xfs_alloc_ag_vextent_near(
956 xfs_alloc_arg_t
*args
) /* allocation argument structure */
958 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
959 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
960 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
961 xfs_agblock_t gtbno
; /* start bno of right side entry */
962 xfs_agblock_t gtbnoa
; /* aligned ... */
963 xfs_extlen_t gtdiff
; /* difference to right side entry */
964 xfs_extlen_t gtlen
; /* length of right side entry */
965 xfs_extlen_t gtlena
; /* aligned ... */
966 xfs_agblock_t gtnew
; /* useful start bno of right side */
967 int error
; /* error code */
968 int i
; /* result code, temporary */
969 int j
; /* result code, temporary */
970 xfs_agblock_t ltbno
; /* start bno of left side entry */
971 xfs_agblock_t ltbnoa
; /* aligned ... */
972 xfs_extlen_t ltdiff
; /* difference to left side entry */
973 xfs_extlen_t ltlen
; /* length of left side entry */
974 xfs_extlen_t ltlena
; /* aligned ... */
975 xfs_agblock_t ltnew
; /* useful start bno of left side */
976 xfs_extlen_t rlen
; /* length of returned extent */
981 * Randomly don't execute the first algorithm.
983 int dofirst
; /* set to do first algorithm */
985 dofirst
= prandom_u32() & 1;
988 /* handle unitialized agbno range so caller doesn't have to */
989 if (!args
->min_agbno
&& !args
->max_agbno
)
990 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
991 ASSERT(args
->min_agbno
<= args
->max_agbno
);
993 /* clamp agbno to the range if it's outside */
994 if (args
->agbno
< args
->min_agbno
)
995 args
->agbno
= args
->min_agbno
;
996 if (args
->agbno
> args
->max_agbno
)
997 args
->agbno
= args
->max_agbno
;
1008 * Get a cursor for the by-size btree.
1010 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1011 args
->agno
, XFS_BTNUM_CNT
);
1014 * See if there are any free extents as big as maxlen.
1016 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
1019 * If none, then pick up the last entry in the tree unless the
1023 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
1026 if (i
== 0 || ltlen
== 0) {
1027 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1028 trace_xfs_alloc_near_noentry(args
);
1033 args
->wasfromfl
= 0;
1037 * If the requested extent is large wrt the freespaces available
1038 * in this a.g., then the cursor will be pointing to a btree entry
1039 * near the right edge of the tree. If it's in the last btree leaf
1040 * block, then we just examine all the entries in that block
1041 * that are big enough, and pick the best one.
1042 * This is written as a while loop so we can break out of it,
1043 * but we never loop back to the top.
1045 while (xfs_btree_islastblock(cnt_cur
, 0)) {
1048 xfs_extlen_t blen
=0;
1049 xfs_agblock_t bnew
=0;
1056 * Start from the entry that lookup found, sequence through
1057 * all larger free blocks. If we're actually pointing at a
1058 * record smaller than maxlen, go to the start of this block,
1059 * and skip all those smaller than minlen.
1061 if (ltlen
|| args
->alignment
> 1) {
1062 cnt_cur
->bc_ptrs
[0] = 1;
1064 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
1067 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1068 if (ltlen
>= args
->minlen
)
1070 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
1073 ASSERT(ltlen
>= args
->minlen
);
1077 i
= cnt_cur
->bc_ptrs
[0];
1078 for (j
= 1, blen
= 0, bdiff
= 0;
1079 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
1080 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
1082 * For each entry, decide if it's better than
1083 * the previous best entry.
1085 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1087 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1088 busy
= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1089 <bnoa
, <lena
, &busy_gen
);
1090 if (ltlena
< args
->minlen
)
1092 if (ltbnoa
< args
->min_agbno
|| ltbnoa
> args
->max_agbno
)
1094 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1095 xfs_alloc_fix_len(args
);
1096 ASSERT(args
->len
>= args
->minlen
);
1097 if (args
->len
< blen
)
1099 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1100 args
->alignment
, args
->datatype
, ltbnoa
,
1102 if (ltnew
!= NULLAGBLOCK
&&
1103 (args
->len
> blen
|| ltdiff
< bdiff
)) {
1107 besti
= cnt_cur
->bc_ptrs
[0];
1111 * It didn't work. We COULD be in a case where
1112 * there's a good record somewhere, so try again.
1117 * Point at the best entry, and retrieve it again.
1119 cnt_cur
->bc_ptrs
[0] = besti
;
1120 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1122 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1123 ASSERT(ltbno
+ ltlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1127 * We are allocating starting at bnew for blen blocks.
1130 ASSERT(bnew
>= ltbno
);
1131 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1133 * Set up a cursor for the by-bno tree.
1135 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1136 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1138 * Fix up the btree entries.
1140 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1141 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1143 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1144 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1146 trace_xfs_alloc_near_first(args
);
1151 * Search in the by-bno tree to the left and to the right
1152 * simultaneously, until in each case we find a space big enough,
1153 * or run into the edge of the tree. When we run into the edge,
1154 * we deallocate that cursor.
1155 * If both searches succeed, we compare the two spaces and pick
1157 * With alignment, it's possible for both to fail; the upper
1158 * level algorithm that picks allocation groups for allocations
1159 * is not supposed to do this.
1162 * Allocate and initialize the cursor for the leftward search.
1164 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1165 args
->agno
, XFS_BTNUM_BNO
);
1167 * Lookup <= bno to find the leftward search's starting point.
1169 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1173 * Didn't find anything; use this cursor for the rightward
1176 bno_cur_gt
= bno_cur_lt
;
1180 * Found something. Duplicate the cursor for the rightward search.
1182 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1185 * Increment the cursor, so we will point at the entry just right
1186 * of the leftward entry if any, or to the leftmost entry.
1188 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1192 * It failed, there are no rightward entries.
1194 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1198 * Loop going left with the leftward cursor, right with the
1199 * rightward cursor, until either both directions give up or
1200 * we find an entry at least as big as minlen.
1204 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1206 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1207 busy
|= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1208 <bnoa
, <lena
, &busy_gen
);
1209 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1211 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1213 if (!i
|| ltbnoa
< args
->min_agbno
) {
1214 xfs_btree_del_cursor(bno_cur_lt
,
1220 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1222 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1223 busy
|= xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1224 >bnoa
, >lena
, &busy_gen
);
1225 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1227 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1229 if (!i
|| gtbnoa
> args
->max_agbno
) {
1230 xfs_btree_del_cursor(bno_cur_gt
,
1235 } while (bno_cur_lt
|| bno_cur_gt
);
1238 * Got both cursors still active, need to find better entry.
1240 if (bno_cur_lt
&& bno_cur_gt
) {
1241 if (ltlena
>= args
->minlen
) {
1243 * Left side is good, look for a right side entry.
1245 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1246 xfs_alloc_fix_len(args
);
1247 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1248 args
->alignment
, args
->datatype
, ltbnoa
,
1251 error
= xfs_alloc_find_best_extent(args
,
1252 &bno_cur_lt
, &bno_cur_gt
,
1253 ltdiff
, >bno
, >len
,
1255 0 /* search right */);
1257 ASSERT(gtlena
>= args
->minlen
);
1260 * Right side is good, look for a left side entry.
1262 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1263 xfs_alloc_fix_len(args
);
1264 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1265 args
->alignment
, args
->datatype
, gtbnoa
,
1268 error
= xfs_alloc_find_best_extent(args
,
1269 &bno_cur_gt
, &bno_cur_lt
,
1270 gtdiff
, <bno
, <len
,
1272 1 /* search left */);
1280 * If we couldn't get anything, give up.
1282 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1283 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1286 trace_xfs_alloc_near_busy(args
);
1287 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1290 trace_xfs_alloc_size_neither(args
);
1291 args
->agbno
= NULLAGBLOCK
;
1296 * At this point we have selected a freespace entry, either to the
1297 * left or to the right. If it's on the right, copy all the
1298 * useful variables to the "left" set so we only have one
1299 * copy of this code.
1302 bno_cur_lt
= bno_cur_gt
;
1313 * Fix up the length and compute the useful address.
1315 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1316 xfs_alloc_fix_len(args
);
1318 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1319 args
->datatype
, ltbnoa
, ltlena
, <new
);
1320 ASSERT(ltnew
>= ltbno
);
1321 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1322 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1323 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1324 args
->agbno
= ltnew
;
1326 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1327 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1331 trace_xfs_alloc_near_greater(args
);
1333 trace_xfs_alloc_near_lesser(args
);
1335 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1336 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1340 trace_xfs_alloc_near_error(args
);
1341 if (cnt_cur
!= NULL
)
1342 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1343 if (bno_cur_lt
!= NULL
)
1344 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1345 if (bno_cur_gt
!= NULL
)
1346 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1351 * Allocate a variable extent anywhere in the allocation group agno.
1352 * Extent's length (returned in len) will be between minlen and maxlen,
1353 * and of the form k * prod + mod unless there's nothing that large.
1354 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1356 STATIC
int /* error */
1357 xfs_alloc_ag_vextent_size(
1358 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1360 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1361 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1362 int error
; /* error result */
1363 xfs_agblock_t fbno
; /* start of found freespace */
1364 xfs_extlen_t flen
; /* length of found freespace */
1365 int i
; /* temp status variable */
1366 xfs_agblock_t rbno
; /* returned block number */
1367 xfs_extlen_t rlen
; /* length of returned extent */
1373 * Allocate and initialize a cursor for the by-size btree.
1375 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1376 args
->agno
, XFS_BTNUM_CNT
);
1381 * Look for an entry >= maxlen+alignment-1 blocks.
1383 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1384 args
->maxlen
+ args
->alignment
- 1, &i
)))
1388 * If none then we have to settle for a smaller extent. In the case that
1389 * there are no large extents, this will return the last entry in the
1390 * tree unless the tree is empty. In the case that there are only busy
1391 * large extents, this will return the largest small extent unless there
1392 * are no smaller extents available.
1395 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1399 if (i
== 0 || flen
== 0) {
1400 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1401 trace_xfs_alloc_size_noentry(args
);
1405 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
,
1409 * Search for a non-busy extent that is large enough.
1412 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1415 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1417 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1418 &rbno
, &rlen
, &busy_gen
);
1420 if (rlen
>= args
->maxlen
)
1423 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1428 * Our only valid extents must have been busy.
1429 * Make it unbusy by forcing the log out and
1432 xfs_btree_del_cursor(cnt_cur
,
1434 trace_xfs_alloc_size_busy(args
);
1435 xfs_extent_busy_flush(args
->mp
,
1436 args
->pag
, busy_gen
);
1443 * In the first case above, we got the last entry in the
1444 * by-size btree. Now we check to see if the space hits maxlen
1445 * once aligned; if not, we search left for something better.
1446 * This can't happen in the second case above.
1448 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1449 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1450 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1451 if (rlen
< args
->maxlen
) {
1452 xfs_agblock_t bestfbno
;
1453 xfs_extlen_t bestflen
;
1454 xfs_agblock_t bestrbno
;
1455 xfs_extlen_t bestrlen
;
1462 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1466 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1469 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1470 if (flen
< bestrlen
)
1472 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1473 &rbno
, &rlen
, &busy_gen
);
1474 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1475 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1476 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1478 if (rlen
> bestrlen
) {
1483 if (rlen
== args
->maxlen
)
1487 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1490 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1496 args
->wasfromfl
= 0;
1498 * Fix up the length.
1501 if (rlen
< args
->minlen
) {
1503 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1504 trace_xfs_alloc_size_busy(args
);
1505 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1510 xfs_alloc_fix_len(args
);
1513 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1515 * Allocate and initialize a cursor for the by-block tree.
1517 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1518 args
->agno
, XFS_BTNUM_BNO
);
1519 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1520 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1522 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1523 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1524 cnt_cur
= bno_cur
= NULL
;
1527 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1528 args
->agbno
+ args
->len
<=
1529 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1531 trace_xfs_alloc_size_done(args
);
1535 trace_xfs_alloc_size_error(args
);
1537 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1539 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1543 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1544 trace_xfs_alloc_size_nominleft(args
);
1545 args
->agbno
= NULLAGBLOCK
;
1550 * Deal with the case where only small freespaces remain.
1551 * Either return the contents of the last freespace record,
1552 * or allocate space from the freelist if there is nothing in the tree.
1554 STATIC
int /* error */
1555 xfs_alloc_ag_vextent_small(
1556 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1557 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1558 xfs_agblock_t
*fbnop
, /* result block number */
1559 xfs_extlen_t
*flenp
, /* result length */
1560 int *stat
) /* status: 0-freelist, 1-normal/none */
1562 struct xfs_owner_info oinfo
;
1563 struct xfs_perag
*pag
;
1569 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1572 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1574 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1577 * Nothing in the btree, try the freelist. Make sure
1578 * to respect minleft even when pulling from the
1581 else if (args
->minlen
== 1 && args
->alignment
== 1 &&
1582 args
->resv
!= XFS_AG_RESV_AGFL
&&
1583 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1585 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1588 if (fbno
!= NULLAGBLOCK
) {
1589 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1590 xfs_alloc_allow_busy_reuse(args
->datatype
));
1592 if (xfs_alloc_is_userdata(args
->datatype
)) {
1595 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1596 args
->agno
, fbno
, 0);
1598 error
= -EFSCORRUPTED
;
1601 xfs_trans_binval(args
->tp
, bp
);
1605 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1606 args
->agbno
+ args
->len
<=
1607 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1609 args
->wasfromfl
= 1;
1610 trace_xfs_alloc_small_freelist(args
);
1613 * If we're feeding an AGFL block to something that
1614 * doesn't live in the free space, we need to clear
1615 * out the OWN_AG rmap and add the block back to
1616 * the AGFL per-AG reservation.
1618 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_AG
);
1619 error
= xfs_rmap_free(args
->tp
, args
->agbp
, args
->agno
,
1623 pag
= xfs_perag_get(args
->mp
, args
->agno
);
1624 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_AGFL
,
1632 * Nothing in the freelist.
1638 * Can't allocate from the freelist for some reason.
1645 * Can't do the allocation, give up.
1647 if (flen
< args
->minlen
) {
1648 args
->agbno
= NULLAGBLOCK
;
1649 trace_xfs_alloc_small_notenough(args
);
1655 trace_xfs_alloc_small_done(args
);
1659 trace_xfs_alloc_small_error(args
);
1664 * Free the extent starting at agno/bno for length.
1670 xfs_agnumber_t agno
,
1673 struct xfs_owner_info
*oinfo
,
1674 enum xfs_ag_resv_type type
)
1676 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1677 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1678 int error
; /* error return value */
1679 xfs_agblock_t gtbno
; /* start of right neighbor block */
1680 xfs_extlen_t gtlen
; /* length of right neighbor block */
1681 int haveleft
; /* have a left neighbor block */
1682 int haveright
; /* have a right neighbor block */
1683 int i
; /* temp, result code */
1684 xfs_agblock_t ltbno
; /* start of left neighbor block */
1685 xfs_extlen_t ltlen
; /* length of left neighbor block */
1686 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1687 xfs_agblock_t nbno
; /* new starting block of freespace */
1688 xfs_extlen_t nlen
; /* new length of freespace */
1689 xfs_perag_t
*pag
; /* per allocation group data */
1691 bno_cur
= cnt_cur
= NULL
;
1694 if (!xfs_rmap_should_skip_owner_update(oinfo
)) {
1695 error
= xfs_rmap_free(tp
, agbp
, agno
, bno
, len
, oinfo
);
1701 * Allocate and initialize a cursor for the by-block btree.
1703 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1705 * Look for a neighboring block on the left (lower block numbers)
1706 * that is contiguous with this space.
1708 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1712 * There is a block to our left.
1714 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1716 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1718 * It's not contiguous, though.
1720 if (ltbno
+ ltlen
< bno
)
1724 * If this failure happens the request to free this
1725 * space was invalid, it's (partly) already free.
1728 XFS_WANT_CORRUPTED_GOTO(mp
,
1729 ltbno
+ ltlen
<= bno
, error0
);
1733 * Look for a neighboring block on the right (higher block numbers)
1734 * that is contiguous with this space.
1736 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1740 * There is a block to our right.
1742 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1744 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1746 * It's not contiguous, though.
1748 if (bno
+ len
< gtbno
)
1752 * If this failure happens the request to free this
1753 * space was invalid, it's (partly) already free.
1756 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1760 * Now allocate and initialize a cursor for the by-size tree.
1762 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1764 * Have both left and right contiguous neighbors.
1765 * Merge all three into a single free block.
1767 if (haveleft
&& haveright
) {
1769 * Delete the old by-size entry on the left.
1771 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1773 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1774 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1776 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1778 * Delete the old by-size entry on the right.
1780 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1782 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1783 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1785 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1787 * Delete the old by-block entry for the right block.
1789 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1791 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1793 * Move the by-block cursor back to the left neighbor.
1795 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1797 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1800 * Check that this is the right record: delete didn't
1801 * mangle the cursor.
1804 xfs_agblock_t xxbno
;
1807 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1810 XFS_WANT_CORRUPTED_GOTO(mp
,
1811 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1816 * Update remaining by-block entry to the new, joined block.
1819 nlen
= len
+ ltlen
+ gtlen
;
1820 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1824 * Have only a left contiguous neighbor.
1825 * Merge it together with the new freespace.
1827 else if (haveleft
) {
1829 * Delete the old by-size entry on the left.
1831 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1833 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1834 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1836 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1838 * Back up the by-block cursor to the left neighbor, and
1839 * update its length.
1841 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1843 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1846 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1850 * Have only a right contiguous neighbor.
1851 * Merge it together with the new freespace.
1853 else if (haveright
) {
1855 * Delete the old by-size entry on the right.
1857 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1859 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1860 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1862 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1864 * Update the starting block and length of the right
1865 * neighbor in the by-block tree.
1869 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1873 * No contiguous neighbors.
1874 * Insert the new freespace into the by-block tree.
1879 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1883 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1886 * In all cases we need to insert the new freespace in the by-size tree.
1888 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1890 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1891 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1893 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1894 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1898 * Update the freespace totals in the ag and superblock.
1900 pag
= xfs_perag_get(mp
, agno
);
1901 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1902 xfs_ag_resv_free_extent(pag
, type
, tp
, len
);
1907 XFS_STATS_INC(mp
, xs_freex
);
1908 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1910 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1911 haveleft
, haveright
);
1916 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1919 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1921 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1926 * Visible (exported) allocation/free functions.
1927 * Some of these are used just by xfs_alloc_btree.c and this file.
1931 * Compute and fill in value of m_ag_maxlevels.
1934 xfs_alloc_compute_maxlevels(
1935 xfs_mount_t
*mp
) /* file system mount structure */
1937 mp
->m_ag_maxlevels
= xfs_btree_compute_maxlevels(mp
, mp
->m_alloc_mnr
,
1938 (mp
->m_sb
.sb_agblocks
+ 1) / 2);
1942 * Find the length of the longest extent in an AG. The 'need' parameter
1943 * specifies how much space we're going to need for the AGFL and the
1944 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1948 xfs_alloc_longest_free_extent(
1949 struct xfs_mount
*mp
,
1950 struct xfs_perag
*pag
,
1952 xfs_extlen_t reserved
)
1954 xfs_extlen_t delta
= 0;
1957 * If the AGFL needs a recharge, we'll have to subtract that from the
1960 if (need
> pag
->pagf_flcount
)
1961 delta
= need
- pag
->pagf_flcount
;
1964 * If we cannot maintain others' reservations with space from the
1965 * not-longest freesp extents, we'll have to subtract /that/ from
1966 * the longest extent too.
1968 if (pag
->pagf_freeblks
- pag
->pagf_longest
< reserved
)
1969 delta
+= reserved
- (pag
->pagf_freeblks
- pag
->pagf_longest
);
1972 * If the longest extent is long enough to satisfy all the
1973 * reservations and AGFL rules in place, we can return this extent.
1975 if (pag
->pagf_longest
> delta
)
1976 return pag
->pagf_longest
- delta
;
1978 /* Otherwise, let the caller try for 1 block if there's space. */
1979 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
1983 xfs_alloc_min_freelist(
1984 struct xfs_mount
*mp
,
1985 struct xfs_perag
*pag
)
1987 unsigned int min_free
;
1989 /* space needed by-bno freespace btree */
1990 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
1991 mp
->m_ag_maxlevels
);
1992 /* space needed by-size freespace btree */
1993 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
1994 mp
->m_ag_maxlevels
);
1995 /* space needed reverse mapping used space btree */
1996 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
1997 min_free
+= min_t(unsigned int,
1998 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] + 1,
1999 mp
->m_rmap_maxlevels
);
2005 * Check if the operation we are fixing up the freelist for should go ahead or
2006 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2007 * is dependent on whether the size and shape of free space available will
2008 * permit the requested allocation to take place.
2011 xfs_alloc_space_available(
2012 struct xfs_alloc_arg
*args
,
2013 xfs_extlen_t min_free
,
2016 struct xfs_perag
*pag
= args
->pag
;
2017 xfs_extlen_t alloc_len
, longest
;
2018 xfs_extlen_t reservation
; /* blocks that are still reserved */
2021 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2024 reservation
= xfs_ag_resv_needed(pag
, args
->resv
);
2026 /* do we have enough contiguous free space for the allocation? */
2027 alloc_len
= args
->minlen
+ (args
->alignment
- 1) + args
->minalignslop
;
2028 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
,
2030 if (longest
< alloc_len
)
2033 /* do we have enough free space remaining for the allocation? */
2034 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
2035 reservation
- min_free
- args
->minleft
);
2036 if (available
< (int)max(args
->total
, alloc_len
))
2040 * Clamp maxlen to the amount of free space available for the actual
2041 * extent allocation.
2043 if (available
< (int)args
->maxlen
&& !(flags
& XFS_ALLOC_FLAG_CHECK
)) {
2044 args
->maxlen
= available
;
2045 ASSERT(args
->maxlen
> 0);
2046 ASSERT(args
->maxlen
>= args
->minlen
);
2053 * Decide whether to use this allocation group for this allocation.
2054 * If so, fix up the btree freelist's size.
2057 xfs_alloc_fix_freelist(
2058 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2059 int flags
) /* XFS_ALLOC_FLAG_... */
2061 struct xfs_mount
*mp
= args
->mp
;
2062 struct xfs_perag
*pag
= args
->pag
;
2063 struct xfs_trans
*tp
= args
->tp
;
2064 struct xfs_buf
*agbp
= NULL
;
2065 struct xfs_buf
*agflbp
= NULL
;
2066 struct xfs_alloc_arg targs
; /* local allocation arguments */
2067 xfs_agblock_t bno
; /* freelist block */
2068 xfs_extlen_t need
; /* total blocks needed in freelist */
2071 if (!pag
->pagf_init
) {
2072 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2075 if (!pag
->pagf_init
) {
2076 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2077 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2078 goto out_agbp_relse
;
2083 * If this is a metadata preferred pag and we are user data then try
2084 * somewhere else if we are not being asked to try harder at this
2087 if (pag
->pagf_metadata
&& xfs_alloc_is_userdata(args
->datatype
) &&
2088 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2089 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2090 goto out_agbp_relse
;
2093 need
= xfs_alloc_min_freelist(mp
, pag
);
2094 if (!xfs_alloc_space_available(args
, need
, flags
|
2095 XFS_ALLOC_FLAG_CHECK
))
2096 goto out_agbp_relse
;
2099 * Get the a.g. freespace buffer.
2100 * Can fail if we're not blocking on locks, and it's held.
2103 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2107 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2108 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2113 /* If there isn't enough total space or single-extent, reject it. */
2114 need
= xfs_alloc_min_freelist(mp
, pag
);
2115 if (!xfs_alloc_space_available(args
, need
, flags
))
2116 goto out_agbp_relse
;
2119 * Make the freelist shorter if it's too long.
2121 * Note that from this point onwards, we will always release the agf and
2122 * agfl buffers on error. This handles the case where we error out and
2123 * the buffers are clean or may not have been joined to the transaction
2124 * and hence need to be released manually. If they have been joined to
2125 * the transaction, then xfs_trans_brelse() will handle them
2126 * appropriately based on the recursion count and dirty state of the
2129 * XXX (dgc): When we have lots of free space, does this buy us
2130 * anything other than extra overhead when we need to put more blocks
2131 * back on the free list? Maybe we should only do this when space is
2132 * getting low or the AGFL is more than half full?
2134 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2135 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2136 * updating the rmapbt. Both flags are used in xfs_repair while we're
2137 * rebuilding the rmapbt, and neither are used by the kernel. They're
2138 * both required to ensure that rmaps are correctly recorded for the
2139 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2140 * repair/rmap.c in xfsprogs for details.
2142 memset(&targs
, 0, sizeof(targs
));
2143 if (flags
& XFS_ALLOC_FLAG_NORMAP
)
2144 xfs_rmap_skip_owner_update(&targs
.oinfo
);
2146 xfs_rmap_ag_owner(&targs
.oinfo
, XFS_RMAP_OWN_AG
);
2147 while (!(flags
& XFS_ALLOC_FLAG_NOSHRINK
) && pag
->pagf_flcount
> need
) {
2150 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2152 goto out_agbp_relse
;
2153 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1,
2154 &targs
.oinfo
, XFS_AG_RESV_AGFL
);
2156 goto out_agbp_relse
;
2157 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2159 error
= -EFSCORRUPTED
;
2160 goto out_agbp_relse
;
2162 xfs_trans_binval(tp
, bp
);
2168 targs
.agno
= args
->agno
;
2169 targs
.alignment
= targs
.minlen
= targs
.prod
= 1;
2170 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2172 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2174 goto out_agbp_relse
;
2176 /* Make the freelist longer if it's too short. */
2177 while (pag
->pagf_flcount
< need
) {
2179 targs
.maxlen
= need
- pag
->pagf_flcount
;
2180 targs
.resv
= XFS_AG_RESV_AGFL
;
2182 /* Allocate as many blocks as possible at once. */
2183 error
= xfs_alloc_ag_vextent(&targs
);
2185 goto out_agflbp_relse
;
2188 * Stop if we run out. Won't happen if callers are obeying
2189 * the restrictions correctly. Can happen for free calls
2190 * on a completely full ag.
2192 if (targs
.agbno
== NULLAGBLOCK
) {
2193 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2195 goto out_agflbp_relse
;
2198 * Put each allocated block on the list.
2200 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2201 error
= xfs_alloc_put_freelist(tp
, agbp
,
2204 goto out_agflbp_relse
;
2207 xfs_trans_brelse(tp
, agflbp
);
2212 xfs_trans_brelse(tp
, agflbp
);
2215 xfs_trans_brelse(tp
, agbp
);
2222 * Get a block from the freelist.
2223 * Returns with the buffer for the block gotten.
2226 xfs_alloc_get_freelist(
2227 xfs_trans_t
*tp
, /* transaction pointer */
2228 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2229 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2230 int btreeblk
) /* destination is a AGF btree */
2232 xfs_agf_t
*agf
; /* a.g. freespace structure */
2233 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2234 xfs_agblock_t bno
; /* block number returned */
2238 xfs_mount_t
*mp
= tp
->t_mountp
;
2239 xfs_perag_t
*pag
; /* per allocation group data */
2242 * Freelist is empty, give up.
2244 agf
= XFS_BUF_TO_AGF(agbp
);
2245 if (!agf
->agf_flcount
) {
2246 *bnop
= NULLAGBLOCK
;
2250 * Read the array of free blocks.
2252 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2259 * Get the block number and update the data structures.
2261 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2262 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2263 be32_add_cpu(&agf
->agf_flfirst
, 1);
2264 xfs_trans_brelse(tp
, agflbp
);
2265 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2266 agf
->agf_flfirst
= 0;
2268 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2269 be32_add_cpu(&agf
->agf_flcount
, -1);
2270 xfs_trans_agflist_delta(tp
, -1);
2271 pag
->pagf_flcount
--;
2274 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2276 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2277 pag
->pagf_btreeblks
++;
2278 logflags
|= XFS_AGF_BTREEBLKS
;
2281 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2288 * Log the given fields from the agf structure.
2292 xfs_trans_t
*tp
, /* transaction pointer */
2293 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2294 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2296 int first
; /* first byte offset */
2297 int last
; /* last byte offset */
2298 static const short offsets
[] = {
2299 offsetof(xfs_agf_t
, agf_magicnum
),
2300 offsetof(xfs_agf_t
, agf_versionnum
),
2301 offsetof(xfs_agf_t
, agf_seqno
),
2302 offsetof(xfs_agf_t
, agf_length
),
2303 offsetof(xfs_agf_t
, agf_roots
[0]),
2304 offsetof(xfs_agf_t
, agf_levels
[0]),
2305 offsetof(xfs_agf_t
, agf_flfirst
),
2306 offsetof(xfs_agf_t
, agf_fllast
),
2307 offsetof(xfs_agf_t
, agf_flcount
),
2308 offsetof(xfs_agf_t
, agf_freeblks
),
2309 offsetof(xfs_agf_t
, agf_longest
),
2310 offsetof(xfs_agf_t
, agf_btreeblks
),
2311 offsetof(xfs_agf_t
, agf_uuid
),
2312 offsetof(xfs_agf_t
, agf_rmap_blocks
),
2313 offsetof(xfs_agf_t
, agf_refcount_blocks
),
2314 offsetof(xfs_agf_t
, agf_refcount_root
),
2315 offsetof(xfs_agf_t
, agf_refcount_level
),
2316 /* needed so that we don't log the whole rest of the structure: */
2317 offsetof(xfs_agf_t
, agf_spare64
),
2321 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2323 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2325 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2326 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2330 * Interface for inode allocation to force the pag data to be initialized.
2333 xfs_alloc_pagf_init(
2334 xfs_mount_t
*mp
, /* file system mount structure */
2335 xfs_trans_t
*tp
, /* transaction pointer */
2336 xfs_agnumber_t agno
, /* allocation group number */
2337 int flags
) /* XFS_ALLOC_FLAGS_... */
2342 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2345 xfs_trans_brelse(tp
, bp
);
2350 * Put the block on the freelist for the allocation group.
2353 xfs_alloc_put_freelist(
2354 xfs_trans_t
*tp
, /* transaction pointer */
2355 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2356 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2357 xfs_agblock_t bno
, /* block being freed */
2358 int btreeblk
) /* block came from a AGF btree */
2360 xfs_agf_t
*agf
; /* a.g. freespace structure */
2361 __be32
*blockp
;/* pointer to array entry */
2364 xfs_mount_t
*mp
; /* mount structure */
2365 xfs_perag_t
*pag
; /* per allocation group data */
2369 agf
= XFS_BUF_TO_AGF(agbp
);
2372 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2373 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2375 be32_add_cpu(&agf
->agf_fllast
, 1);
2376 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2377 agf
->agf_fllast
= 0;
2379 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2380 be32_add_cpu(&agf
->agf_flcount
, 1);
2381 xfs_trans_agflist_delta(tp
, 1);
2382 pag
->pagf_flcount
++;
2384 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2386 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2387 pag
->pagf_btreeblks
--;
2388 logflags
|= XFS_AGF_BTREEBLKS
;
2392 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2394 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2396 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2397 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2398 *blockp
= cpu_to_be32(bno
);
2399 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2401 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2403 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2404 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2405 startoff
+ sizeof(xfs_agblock_t
) - 1);
2409 static xfs_failaddr_t
2413 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2414 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2416 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2417 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2418 return __this_address
;
2419 if (!xfs_log_check_lsn(mp
,
2420 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2421 return __this_address
;
2424 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2425 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2426 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2427 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2428 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2429 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
)))
2430 return __this_address
;
2432 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) < 1 ||
2433 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) < 1 ||
2434 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2435 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2436 return __this_address
;
2438 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2439 (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) < 1 ||
2440 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) > XFS_BTREE_MAXLEVELS
))
2441 return __this_address
;
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
)
2450 return __this_address
;
2452 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2453 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2454 return __this_address
;
2456 if (xfs_sb_version_hasreflink(&mp
->m_sb
) &&
2457 (be32_to_cpu(agf
->agf_refcount_level
) < 1 ||
2458 be32_to_cpu(agf
->agf_refcount_level
) > XFS_BTREE_MAXLEVELS
))
2459 return __this_address
;
2466 xfs_agf_read_verify(
2469 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2472 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2473 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2474 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
2476 fa
= xfs_agf_verify(bp
);
2477 if (XFS_TEST_ERROR(fa
, mp
, XFS_ERRTAG_ALLOC_READ_AGF
))
2478 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
2483 xfs_agf_write_verify(
2486 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2487 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2490 fa
= xfs_agf_verify(bp
);
2492 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
2496 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2500 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2502 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2505 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2507 .verify_read
= xfs_agf_read_verify
,
2508 .verify_write
= xfs_agf_write_verify
,
2509 .verify_struct
= xfs_agf_verify
,
2513 * Read in the allocation group header (free/alloc section).
2517 struct xfs_mount
*mp
, /* mount point structure */
2518 struct xfs_trans
*tp
, /* transaction pointer */
2519 xfs_agnumber_t agno
, /* allocation group number */
2520 int flags
, /* XFS_BUF_ */
2521 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2525 trace_xfs_read_agf(mp
, agno
);
2527 ASSERT(agno
!= NULLAGNUMBER
);
2528 error
= xfs_trans_read_buf(
2529 mp
, tp
, mp
->m_ddev_targp
,
2530 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2531 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2537 ASSERT(!(*bpp
)->b_error
);
2538 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2543 * Read in the allocation group header (free/alloc section).
2547 struct xfs_mount
*mp
, /* mount point structure */
2548 struct xfs_trans
*tp
, /* transaction pointer */
2549 xfs_agnumber_t agno
, /* allocation group number */
2550 int flags
, /* XFS_ALLOC_FLAG_... */
2551 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2553 struct xfs_agf
*agf
; /* ag freelist header */
2554 struct xfs_perag
*pag
; /* per allocation group data */
2557 trace_xfs_alloc_read_agf(mp
, agno
);
2559 ASSERT(agno
!= NULLAGNUMBER
);
2560 error
= xfs_read_agf(mp
, tp
, agno
,
2561 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2567 ASSERT(!(*bpp
)->b_error
);
2569 agf
= XFS_BUF_TO_AGF(*bpp
);
2570 pag
= xfs_perag_get(mp
, agno
);
2571 if (!pag
->pagf_init
) {
2572 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2573 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2574 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2575 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2576 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2577 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2578 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2579 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2580 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] =
2581 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAPi
]);
2582 pag
->pagf_refcount_level
= be32_to_cpu(agf
->agf_refcount_level
);
2583 spin_lock_init(&pag
->pagb_lock
);
2584 pag
->pagb_count
= 0;
2585 /* XXX: pagb_tree doesn't exist in userspace */
2586 //pag->pagb_tree = RB_ROOT;
2590 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2591 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2592 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2593 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2594 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2595 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2596 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2597 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2598 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2606 * Allocate an extent (variable-size).
2607 * Depending on the allocation type, we either look in a single allocation
2608 * group or loop over the allocation groups to find the result.
2612 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2614 xfs_agblock_t agsize
; /* allocation group size */
2616 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2617 xfs_mount_t
*mp
; /* mount structure pointer */
2618 xfs_agnumber_t sagno
; /* starting allocation group number */
2619 xfs_alloctype_t type
; /* input allocation type */
2621 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2624 type
= args
->otype
= args
->type
;
2625 args
->agbno
= NULLAGBLOCK
;
2627 * Just fix this up, for the case where the last a.g. is shorter
2628 * (or there's only one a.g.) and the caller couldn't easily figure
2629 * that out (xfs_bmap_alloc).
2631 agsize
= mp
->m_sb
.sb_agblocks
;
2632 if (args
->maxlen
> agsize
)
2633 args
->maxlen
= agsize
;
2634 if (args
->alignment
== 0)
2635 args
->alignment
= 1;
2636 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2637 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2638 ASSERT(args
->minlen
<= args
->maxlen
);
2639 ASSERT(args
->minlen
<= agsize
);
2640 ASSERT(args
->mod
< args
->prod
);
2641 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2642 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2643 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2644 args
->mod
>= args
->prod
) {
2645 args
->fsbno
= NULLFSBLOCK
;
2646 trace_xfs_alloc_vextent_badargs(args
);
2651 case XFS_ALLOCTYPE_THIS_AG
:
2652 case XFS_ALLOCTYPE_NEAR_BNO
:
2653 case XFS_ALLOCTYPE_THIS_BNO
:
2655 * These three force us into a single a.g.
2657 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2658 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2659 error
= xfs_alloc_fix_freelist(args
, 0);
2661 trace_xfs_alloc_vextent_nofix(args
);
2665 trace_xfs_alloc_vextent_noagbp(args
);
2668 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2669 if ((error
= xfs_alloc_ag_vextent(args
)))
2672 case XFS_ALLOCTYPE_START_BNO
:
2674 * Try near allocation first, then anywhere-in-ag after
2675 * the first a.g. fails.
2677 if ((args
->datatype
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2678 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2679 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2680 ((mp
->m_agfrotor
/ rotorstep
) %
2681 mp
->m_sb
.sb_agcount
), 0);
2684 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2685 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2687 case XFS_ALLOCTYPE_FIRST_AG
:
2689 * Rotate through the allocation groups looking for a winner.
2691 if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2693 * Start with allocation group given by bno.
2695 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2696 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2701 * Start with the given allocation group.
2703 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2704 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2707 * Loop over allocation groups twice; first time with
2708 * trylock set, second time without.
2711 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2712 error
= xfs_alloc_fix_freelist(args
, flags
);
2714 trace_xfs_alloc_vextent_nofix(args
);
2718 * If we get a buffer back then the allocation will fly.
2721 if ((error
= xfs_alloc_ag_vextent(args
)))
2726 trace_xfs_alloc_vextent_loopfailed(args
);
2729 * Didn't work, figure out the next iteration.
2731 if (args
->agno
== sagno
&&
2732 type
== XFS_ALLOCTYPE_START_BNO
)
2733 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2735 * For the first allocation, we can try any AG to get
2736 * space. However, if we already have allocated a
2737 * block, we don't want to try AGs whose number is below
2738 * sagno. Otherwise, we may end up with out-of-order
2739 * locking of AGF, which might cause deadlock.
2741 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2742 if (args
->firstblock
!= NULLFSBLOCK
)
2748 * Reached the starting a.g., must either be done
2749 * or switch to non-trylock mode.
2751 if (args
->agno
== sagno
) {
2753 args
->agbno
= NULLAGBLOCK
;
2754 trace_xfs_alloc_vextent_allfailed(args
);
2759 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2760 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2762 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2765 xfs_perag_put(args
->pag
);
2768 if (args
->agno
== sagno
)
2769 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2770 (mp
->m_sb
.sb_agcount
* rotorstep
);
2772 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2773 (mp
->m_sb
.sb_agcount
* rotorstep
);
2780 if (args
->agbno
== NULLAGBLOCK
)
2781 args
->fsbno
= NULLFSBLOCK
;
2783 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2785 ASSERT(args
->len
>= args
->minlen
);
2786 ASSERT(args
->len
<= args
->maxlen
);
2787 ASSERT(args
->agbno
% args
->alignment
== 0);
2788 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2792 /* Zero the extent if we were asked to do so */
2793 if (args
->datatype
& XFS_ALLOC_USERDATA_ZERO
) {
2794 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2800 xfs_perag_put(args
->pag
);
2803 xfs_perag_put(args
->pag
);
2807 /* Ensure that the freelist is at full capacity. */
2809 xfs_free_extent_fix_freelist(
2810 struct xfs_trans
*tp
,
2811 xfs_agnumber_t agno
,
2812 struct xfs_buf
**agbp
)
2814 struct xfs_alloc_arg args
;
2817 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
2819 args
.mp
= tp
->t_mountp
;
2823 * validate that the block number is legal - the enables us to detect
2824 * and handle a silent filesystem corruption rather than crashing.
2826 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2827 return -EFSCORRUPTED
;
2829 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2832 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2838 xfs_perag_put(args
.pag
);
2844 * Just break up the extent address and hand off to xfs_free_ag_extent
2845 * after fixing up the freelist.
2849 struct xfs_trans
*tp
, /* transaction pointer */
2850 xfs_fsblock_t bno
, /* starting block number of extent */
2851 xfs_extlen_t len
, /* length of extent */
2852 struct xfs_owner_info
*oinfo
, /* extent owner */
2853 enum xfs_ag_resv_type type
) /* block reservation type */
2855 struct xfs_mount
*mp
= tp
->t_mountp
;
2856 struct xfs_buf
*agbp
;
2857 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, bno
);
2858 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
2862 ASSERT(type
!= XFS_AG_RESV_AGFL
);
2864 if (XFS_TEST_ERROR(false, mp
,
2865 XFS_ERRTAG_FREE_EXTENT
))
2868 error
= xfs_free_extent_fix_freelist(tp
, agno
, &agbp
);
2872 XFS_WANT_CORRUPTED_GOTO(mp
, agbno
< mp
->m_sb
.sb_agblocks
, err
);
2874 /* validate the extent size is legal now we have the agf locked */
2875 XFS_WANT_CORRUPTED_GOTO(mp
,
2876 agbno
+ len
<= be32_to_cpu(XFS_BUF_TO_AGF(agbp
)->agf_length
),
2879 error
= xfs_free_ag_extent(tp
, agbp
, agno
, agbno
, len
, oinfo
, type
);
2883 xfs_extent_busy_insert(tp
, agno
, agbno
, len
, 0);
2887 xfs_trans_brelse(tp
, agbp
);
2891 struct xfs_alloc_query_range_info
{
2892 xfs_alloc_query_range_fn fn
;
2896 /* Format btree record and pass to our callback. */
2898 xfs_alloc_query_range_helper(
2899 struct xfs_btree_cur
*cur
,
2900 union xfs_btree_rec
*rec
,
2903 struct xfs_alloc_query_range_info
*query
= priv
;
2904 struct xfs_alloc_rec_incore irec
;
2906 irec
.ar_startblock
= be32_to_cpu(rec
->alloc
.ar_startblock
);
2907 irec
.ar_blockcount
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
2908 return query
->fn(cur
, &irec
, query
->priv
);
2911 /* Find all free space within a given range of blocks. */
2913 xfs_alloc_query_range(
2914 struct xfs_btree_cur
*cur
,
2915 struct xfs_alloc_rec_incore
*low_rec
,
2916 struct xfs_alloc_rec_incore
*high_rec
,
2917 xfs_alloc_query_range_fn fn
,
2920 union xfs_btree_irec low_brec
;
2921 union xfs_btree_irec high_brec
;
2922 struct xfs_alloc_query_range_info query
;
2924 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
2925 low_brec
.a
= *low_rec
;
2926 high_brec
.a
= *high_rec
;
2929 return xfs_btree_query_range(cur
, &low_brec
, &high_brec
,
2930 xfs_alloc_query_range_helper
, &query
);
2933 /* Find all free space records. */
2935 xfs_alloc_query_all(
2936 struct xfs_btree_cur
*cur
,
2937 xfs_alloc_query_range_fn fn
,
2940 struct xfs_alloc_query_range_info query
;
2942 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
2945 return xfs_btree_query_all(cur
, xfs_alloc_query_range_helper
, &query
);
2948 /* Find the size of the AG, in blocks. */
2951 struct xfs_mount
*mp
,
2952 xfs_agnumber_t agno
)
2954 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
2956 if (agno
< mp
->m_sb
.sb_agcount
- 1)
2957 return mp
->m_sb
.sb_agblocks
;
2958 return mp
->m_sb
.sb_dblocks
- (agno
* mp
->m_sb
.sb_agblocks
);
2962 * Verify that an AG block number pointer neither points outside the AG
2963 * nor points at static metadata.
2967 struct xfs_mount
*mp
,
2968 xfs_agnumber_t agno
,
2969 xfs_agblock_t agbno
)
2973 eoag
= xfs_ag_block_count(mp
, agno
);
2976 if (agbno
<= XFS_AGFL_BLOCK(mp
))
2982 * Verify that an FS block number pointer neither points outside the
2983 * filesystem nor points at static AG metadata.
2987 struct xfs_mount
*mp
,
2988 xfs_fsblock_t fsbno
)
2990 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, fsbno
);
2992 if (agno
>= mp
->m_sb
.sb_agcount
)
2994 return xfs_verify_agbno(mp
, agno
, XFS_FSB_TO_AGBNO(mp
, fsbno
));
2997 /* Is there a record covering a given extent? */
2999 xfs_alloc_has_record(
3000 struct xfs_btree_cur
*cur
,
3005 union xfs_btree_irec low
;
3006 union xfs_btree_irec high
;
3008 memset(&low
, 0, sizeof(low
));
3009 low
.a
.ar_startblock
= bno
;
3010 memset(&high
, 0xFF, sizeof(high
));
3011 high
.a
.ar_startblock
= bno
+ len
- 1;
3013 return xfs_btree_has_record(cur
, &low
, &high
, exists
);