2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_extent_busy.h"
34 #include "xfs_errortag.h"
35 #include "xfs_error.h"
36 #include "xfs_cksum.h"
37 #include "xfs_trace.h"
38 #include "xfs_trans.h"
39 #include "xfs_buf_item.h"
41 #include "xfs_ag_resv.h"
43 struct workqueue_struct
*xfs_alloc_wq
;
45 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
47 #define XFSA_FIXUP_BNO_OK 1
48 #define XFSA_FIXUP_CNT_OK 2
50 STATIC
int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t
*);
51 STATIC
int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t
*);
52 STATIC
int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t
*);
53 STATIC
int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t
*,
54 xfs_btree_cur_t
*, xfs_agblock_t
*, xfs_extlen_t
*, int *);
60 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
61 return XFS_RMAP_BLOCK(mp
) + 1;
62 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
63 return XFS_FIBT_BLOCK(mp
) + 1;
64 return XFS_IBT_BLOCK(mp
) + 1;
71 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
72 return xfs_refc_block(mp
) + 1;
73 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
74 return XFS_RMAP_BLOCK(mp
) + 1;
75 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
76 return XFS_FIBT_BLOCK(mp
) + 1;
77 return XFS_IBT_BLOCK(mp
) + 1;
81 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
82 * AGF buffer (PV 947395), we place constraints on the relationship among
83 * actual allocations for data blocks, freelist blocks, and potential file data
84 * bmap btree blocks. However, these restrictions may result in no actual space
85 * allocated for a delayed extent, for example, a data block in a certain AG is
86 * allocated but there is no additional block for the additional bmap btree
87 * block due to a split of the bmap btree of the file. The result of this may
88 * lead to an infinite loop when the file gets flushed to disk and all delayed
89 * extents need to be actually allocated. To get around this, we explicitly set
90 * aside a few blocks which will not be reserved in delayed allocation.
92 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
93 * potential split of the file's bmap btree.
99 return mp
->m_sb
.sb_agcount
* (XFS_ALLOC_AGFL_RESERVE
+ 4);
103 * When deciding how much space to allocate out of an AG, we limit the
104 * allocation maximum size to the size the AG. However, we cannot use all the
105 * blocks in the AG - some are permanently used by metadata. These
106 * blocks are generally:
107 * - the AG superblock, AGF, AGI and AGFL
108 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
109 * the AGI free inode and rmap btree root blocks.
110 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
111 * - the rmapbt root block
113 * The AG headers are sector sized, so the amount of space they take up is
114 * dependent on filesystem geometry. The others are all single blocks.
117 xfs_alloc_ag_max_usable(
118 struct xfs_mount
*mp
)
122 blocks
= XFS_BB_TO_FSB(mp
, XFS_FSS_TO_BB(mp
, 4)); /* ag headers */
123 blocks
+= XFS_ALLOC_AGFL_RESERVE
;
124 blocks
+= 3; /* AGF, AGI btree root blocks */
125 if (xfs_sb_version_hasfinobt(&mp
->m_sb
))
126 blocks
++; /* finobt root block */
127 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
128 blocks
++; /* rmap root block */
129 if (xfs_sb_version_hasreflink(&mp
->m_sb
))
130 blocks
++; /* refcount root block */
132 return mp
->m_sb
.sb_agblocks
- blocks
;
136 * Lookup the record equal to [bno, len] in the btree given by cur.
138 STATIC
int /* error */
140 struct xfs_btree_cur
*cur
, /* btree cursor */
141 xfs_agblock_t bno
, /* starting block of extent */
142 xfs_extlen_t len
, /* length of extent */
143 int *stat
) /* success/failure */
145 cur
->bc_rec
.a
.ar_startblock
= bno
;
146 cur
->bc_rec
.a
.ar_blockcount
= len
;
147 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
151 * Lookup the first record greater than or equal to [bno, len]
152 * in the btree given by cur.
156 struct xfs_btree_cur
*cur
, /* btree cursor */
157 xfs_agblock_t bno
, /* starting block of extent */
158 xfs_extlen_t len
, /* length of extent */
159 int *stat
) /* success/failure */
161 cur
->bc_rec
.a
.ar_startblock
= bno
;
162 cur
->bc_rec
.a
.ar_blockcount
= len
;
163 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
167 * Lookup the first record less than or equal to [bno, len]
168 * in the btree given by cur.
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 */
175 int *stat
) /* success/failure */
177 cur
->bc_rec
.a
.ar_startblock
= bno
;
178 cur
->bc_rec
.a
.ar_blockcount
= len
;
179 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
183 * Update the record referred to by cur to the value given
185 * This either works (return 0) or gets an EFSCORRUPTED error.
187 STATIC
int /* error */
189 struct xfs_btree_cur
*cur
, /* btree cursor */
190 xfs_agblock_t bno
, /* starting block of extent */
191 xfs_extlen_t len
) /* length of extent */
193 union xfs_btree_rec rec
;
195 rec
.alloc
.ar_startblock
= cpu_to_be32(bno
);
196 rec
.alloc
.ar_blockcount
= cpu_to_be32(len
);
197 return xfs_btree_update(cur
, &rec
);
201 * Get the data from the pointed-to record.
205 struct xfs_btree_cur
*cur
, /* btree cursor */
206 xfs_agblock_t
*bno
, /* output: starting block of extent */
207 xfs_extlen_t
*len
, /* output: length of extent */
208 int *stat
) /* output: success/failure */
210 union xfs_btree_rec
*rec
;
213 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
214 if (!error
&& *stat
== 1) {
215 *bno
= be32_to_cpu(rec
->alloc
.ar_startblock
);
216 *len
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
222 * Compute aligned version of the found extent.
223 * Takes alignment and min length into account.
226 xfs_alloc_compute_aligned(
227 xfs_alloc_arg_t
*args
, /* allocation argument structure */
228 xfs_agblock_t foundbno
, /* starting block in found extent */
229 xfs_extlen_t foundlen
, /* length in found extent */
230 xfs_agblock_t
*resbno
, /* result block number */
231 xfs_extlen_t
*reslen
, /* result length */
234 xfs_agblock_t bno
= foundbno
;
235 xfs_extlen_t len
= foundlen
;
239 /* Trim busy sections out of found extent */
240 busy
= xfs_extent_busy_trim(args
, &bno
, &len
, busy_gen
);
243 * If we have a largish extent that happens to start before min_agbno,
244 * see if we can shift it into range...
246 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
247 diff
= args
->min_agbno
- bno
;
254 if (args
->alignment
> 1 && len
>= args
->minlen
) {
255 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
257 diff
= aligned_bno
- bno
;
259 *resbno
= aligned_bno
;
260 *reslen
= diff
>= len
? 0 : len
- diff
;
270 * Compute best start block and diff for "near" allocations.
271 * freelen >= wantlen already checked by caller.
273 STATIC xfs_extlen_t
/* difference value (absolute) */
274 xfs_alloc_compute_diff(
275 xfs_agblock_t wantbno
, /* target starting block */
276 xfs_extlen_t wantlen
, /* target length */
277 xfs_extlen_t alignment
, /* target alignment */
278 int datatype
, /* are we allocating data? */
279 xfs_agblock_t freebno
, /* freespace's starting block */
280 xfs_extlen_t freelen
, /* freespace's length */
281 xfs_agblock_t
*newbnop
) /* result: best start block from free */
283 xfs_agblock_t freeend
; /* end of freespace extent */
284 xfs_agblock_t newbno1
; /* return block number */
285 xfs_agblock_t newbno2
; /* other new block number */
286 xfs_extlen_t newlen1
=0; /* length with newbno1 */
287 xfs_extlen_t newlen2
=0; /* length with newbno2 */
288 xfs_agblock_t wantend
; /* end of target extent */
289 bool userdata
= xfs_alloc_is_userdata(datatype
);
291 ASSERT(freelen
>= wantlen
);
292 freeend
= freebno
+ freelen
;
293 wantend
= wantbno
+ wantlen
;
295 * We want to allocate from the start of a free extent if it is past
296 * the desired block or if we are allocating user data and the free
297 * extent is before desired block. The second case is there to allow
298 * for contiguous allocation from the remaining free space if the file
299 * grows in the short term.
301 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
302 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
303 newbno1
= NULLAGBLOCK
;
304 } else if (freeend
>= wantend
&& alignment
> 1) {
305 newbno1
= roundup(wantbno
, alignment
);
306 newbno2
= newbno1
- alignment
;
307 if (newbno1
>= freeend
)
308 newbno1
= NULLAGBLOCK
;
310 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
311 if (newbno2
< freebno
)
312 newbno2
= NULLAGBLOCK
;
314 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
315 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
316 if (newlen1
< newlen2
||
317 (newlen1
== newlen2
&&
318 XFS_ABSDIFF(newbno1
, wantbno
) >
319 XFS_ABSDIFF(newbno2
, wantbno
)))
321 } else if (newbno2
!= NULLAGBLOCK
)
323 } else if (freeend
>= wantend
) {
325 } else if (alignment
> 1) {
326 newbno1
= roundup(freeend
- wantlen
, alignment
);
327 if (newbno1
> freeend
- wantlen
&&
328 newbno1
- alignment
>= freebno
)
329 newbno1
-= alignment
;
330 else if (newbno1
>= freeend
)
331 newbno1
= NULLAGBLOCK
;
333 newbno1
= freeend
- wantlen
;
335 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
339 * Fix up the length, based on mod and prod.
340 * len should be k * prod + mod for some k.
341 * If len is too small it is returned unchanged.
342 * If len hits maxlen it is left alone.
346 xfs_alloc_arg_t
*args
) /* allocation argument structure */
351 ASSERT(args
->mod
< args
->prod
);
353 ASSERT(rlen
>= args
->minlen
);
354 ASSERT(rlen
<= args
->maxlen
);
355 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
356 (args
->mod
== 0 && rlen
< args
->prod
))
358 k
= rlen
% args
->prod
;
362 rlen
= rlen
- (k
- args
->mod
);
364 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
365 /* casts to (int) catch length underflows */
366 if ((int)rlen
< (int)args
->minlen
)
368 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
369 ASSERT(rlen
% args
->prod
== args
->mod
);
370 ASSERT(args
->pag
->pagf_freeblks
+ args
->pag
->pagf_flcount
>=
371 rlen
+ args
->minleft
);
376 * Update the two btrees, logically removing from freespace the extent
377 * starting at rbno, rlen blocks. The extent is contained within the
378 * actual (current) free extent fbno for flen blocks.
379 * Flags are passed in indicating whether the cursors are set to the
382 STATIC
int /* error code */
383 xfs_alloc_fixup_trees(
384 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
385 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
386 xfs_agblock_t fbno
, /* starting block of free extent */
387 xfs_extlen_t flen
, /* length of free extent */
388 xfs_agblock_t rbno
, /* starting block of returned extent */
389 xfs_extlen_t rlen
, /* length of returned extent */
390 int flags
) /* flags, XFSA_FIXUP_... */
392 int error
; /* error code */
393 int i
; /* operation results */
394 xfs_agblock_t nfbno1
; /* first new free startblock */
395 xfs_agblock_t nfbno2
; /* second new free startblock */
396 xfs_extlen_t nflen1
=0; /* first new free length */
397 xfs_extlen_t nflen2
=0; /* second new free length */
398 struct xfs_mount
*mp
;
403 * Look up the record in the by-size tree if necessary.
405 if (flags
& XFSA_FIXUP_CNT_OK
) {
407 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
409 XFS_WANT_CORRUPTED_RETURN(mp
,
410 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
413 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
415 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
418 * Look up the record in the by-block tree if necessary.
420 if (flags
& XFSA_FIXUP_BNO_OK
) {
422 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
424 XFS_WANT_CORRUPTED_RETURN(mp
,
425 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
428 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
430 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
434 if (bno_cur
->bc_nlevels
== 1 && cnt_cur
->bc_nlevels
== 1) {
435 struct xfs_btree_block
*bnoblock
;
436 struct xfs_btree_block
*cntblock
;
438 bnoblock
= XFS_BUF_TO_BLOCK(bno_cur
->bc_bufs
[0]);
439 cntblock
= XFS_BUF_TO_BLOCK(cnt_cur
->bc_bufs
[0]);
441 XFS_WANT_CORRUPTED_RETURN(mp
,
442 bnoblock
->bb_numrecs
== cntblock
->bb_numrecs
);
447 * Deal with all four cases: the allocated record is contained
448 * within the freespace record, so we can have new freespace
449 * at either (or both) end, or no freespace remaining.
451 if (rbno
== fbno
&& rlen
== flen
)
452 nfbno1
= nfbno2
= NULLAGBLOCK
;
453 else if (rbno
== fbno
) {
454 nfbno1
= rbno
+ rlen
;
455 nflen1
= flen
- rlen
;
456 nfbno2
= NULLAGBLOCK
;
457 } else if (rbno
+ rlen
== fbno
+ flen
) {
459 nflen1
= flen
- rlen
;
460 nfbno2
= NULLAGBLOCK
;
463 nflen1
= rbno
- fbno
;
464 nfbno2
= rbno
+ rlen
;
465 nflen2
= (fbno
+ flen
) - nfbno2
;
468 * Delete the entry from the by-size btree.
470 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
472 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
474 * Add new by-size btree entry(s).
476 if (nfbno1
!= NULLAGBLOCK
) {
477 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
479 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
480 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
482 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
484 if (nfbno2
!= NULLAGBLOCK
) {
485 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
487 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
488 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
490 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
493 * Fix up the by-block btree entry(s).
495 if (nfbno1
== NULLAGBLOCK
) {
497 * No remaining freespace, just delete the by-block tree entry.
499 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
501 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
504 * Update the by-block entry to start later|be shorter.
506 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
509 if (nfbno2
!= NULLAGBLOCK
) {
511 * 2 resulting free entries, need to add one.
513 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
515 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
516 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
518 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
527 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
528 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
531 if (!uuid_equal(&agfl
->agfl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
533 if (be32_to_cpu(agfl
->agfl_magicnum
) != XFS_AGFL_MAGIC
)
536 * during growfs operations, the perag is not fully initialised,
537 * so we can't use it for any useful checking. growfs ensures we can't
538 * use it by using uncached buffers that don't have the perag attached
539 * so we can detect and avoid this problem.
541 if (bp
->b_pag
&& be32_to_cpu(agfl
->agfl_seqno
) != bp
->b_pag
->pag_agno
)
544 for (i
= 0; i
< XFS_AGFL_SIZE(mp
); i
++) {
545 if (be32_to_cpu(agfl
->agfl_bno
[i
]) != NULLAGBLOCK
&&
546 be32_to_cpu(agfl
->agfl_bno
[i
]) >= mp
->m_sb
.sb_agblocks
)
550 return xfs_log_check_lsn(mp
,
551 be64_to_cpu(XFS_BUF_TO_AGFL(bp
)->agfl_lsn
));
555 xfs_agfl_read_verify(
558 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
561 * There is no verification of non-crc AGFLs because mkfs does not
562 * initialise the AGFL to zero or NULL. Hence the only valid part of the
563 * AGFL is what the AGF says is active. We can't get to the AGF, so we
564 * can't verify just those entries are valid.
566 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
569 if (!xfs_buf_verify_cksum(bp
, XFS_AGFL_CRC_OFF
))
570 xfs_buf_ioerror(bp
, -EFSBADCRC
);
571 else if (!xfs_agfl_verify(bp
))
572 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
575 xfs_verifier_error(bp
);
579 xfs_agfl_write_verify(
582 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
583 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
585 /* no verification of non-crc AGFLs */
586 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
589 if (!xfs_agfl_verify(bp
)) {
590 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
591 xfs_verifier_error(bp
);
596 XFS_BUF_TO_AGFL(bp
)->agfl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
598 xfs_buf_update_cksum(bp
, XFS_AGFL_CRC_OFF
);
601 const struct xfs_buf_ops xfs_agfl_buf_ops
= {
603 .verify_read
= xfs_agfl_read_verify
,
604 .verify_write
= xfs_agfl_write_verify
,
608 * Read in the allocation group free block array.
612 xfs_mount_t
*mp
, /* mount point structure */
613 xfs_trans_t
*tp
, /* transaction pointer */
614 xfs_agnumber_t agno
, /* allocation group number */
615 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
617 xfs_buf_t
*bp
; /* return value */
620 ASSERT(agno
!= NULLAGNUMBER
);
621 error
= xfs_trans_read_buf(
622 mp
, tp
, mp
->m_ddev_targp
,
623 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
624 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_agfl_buf_ops
);
627 xfs_buf_set_ref(bp
, XFS_AGFL_REF
);
633 xfs_alloc_update_counters(
634 struct xfs_trans
*tp
,
635 struct xfs_perag
*pag
,
636 struct xfs_buf
*agbp
,
639 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
641 pag
->pagf_freeblks
+= len
;
642 be32_add_cpu(&agf
->agf_freeblks
, len
);
644 xfs_trans_agblocks_delta(tp
, len
);
645 if (unlikely(be32_to_cpu(agf
->agf_freeblks
) >
646 be32_to_cpu(agf
->agf_length
)))
647 return -EFSCORRUPTED
;
649 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
654 * Allocation group level functions.
658 * Allocate a variable extent in the allocation group agno.
659 * Type and bno are used to determine where in the allocation group the
661 * Extent's length (returned in *len) will be between minlen and maxlen,
662 * and of the form k * prod + mod unless there's nothing that large.
663 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
665 STATIC
int /* error */
666 xfs_alloc_ag_vextent(
667 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
671 ASSERT(args
->minlen
> 0);
672 ASSERT(args
->maxlen
> 0);
673 ASSERT(args
->minlen
<= args
->maxlen
);
674 ASSERT(args
->mod
< args
->prod
);
675 ASSERT(args
->alignment
> 0);
678 * Branch to correct routine based on the type.
681 switch (args
->type
) {
682 case XFS_ALLOCTYPE_THIS_AG
:
683 error
= xfs_alloc_ag_vextent_size(args
);
685 case XFS_ALLOCTYPE_NEAR_BNO
:
686 error
= xfs_alloc_ag_vextent_near(args
);
688 case XFS_ALLOCTYPE_THIS_BNO
:
689 error
= xfs_alloc_ag_vextent_exact(args
);
696 if (error
|| args
->agbno
== NULLAGBLOCK
)
699 ASSERT(args
->len
>= args
->minlen
);
700 ASSERT(args
->len
<= args
->maxlen
);
701 ASSERT(!args
->wasfromfl
|| args
->resv
!= XFS_AG_RESV_AGFL
);
702 ASSERT(args
->agbno
% args
->alignment
== 0);
704 /* if not file data, insert new block into the reverse map btree */
705 if (!xfs_rmap_should_skip_owner_update(&args
->oinfo
)) {
706 error
= xfs_rmap_alloc(args
->tp
, args
->agbp
, args
->agno
,
707 args
->agbno
, args
->len
, &args
->oinfo
);
712 if (!args
->wasfromfl
) {
713 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
715 -((long)(args
->len
)));
719 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
720 args
->agbno
, args
->len
));
723 xfs_ag_resv_alloc_extent(args
->pag
, args
->resv
, args
);
725 XFS_STATS_INC(args
->mp
, xs_allocx
);
726 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
731 * Allocate a variable extent at exactly agno/bno.
732 * Extent's length (returned in *len) will be between minlen and maxlen,
733 * and of the form k * prod + mod unless there's nothing that large.
734 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
736 STATIC
int /* error */
737 xfs_alloc_ag_vextent_exact(
738 xfs_alloc_arg_t
*args
) /* allocation argument structure */
740 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
741 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
743 xfs_agblock_t fbno
; /* start block of found extent */
744 xfs_extlen_t flen
; /* length of found extent */
745 xfs_agblock_t tbno
; /* start block of busy extent */
746 xfs_extlen_t tlen
; /* length of busy extent */
747 xfs_agblock_t tend
; /* end block of busy extent */
748 int i
; /* success/failure of operation */
751 ASSERT(args
->alignment
== 1);
754 * Allocate/initialize a cursor for the by-number freespace btree.
756 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
757 args
->agno
, XFS_BTNUM_BNO
);
760 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
761 * Look for the closest free block <= bno, it must contain bno
762 * if any free block does.
764 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
771 * Grab the freespace record.
773 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
776 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
777 ASSERT(fbno
<= args
->agbno
);
780 * Check for overlapping busy extents.
784 xfs_extent_busy_trim(args
, &tbno
, &tlen
, &busy_gen
);
787 * Give up if the start of the extent is busy, or the freespace isn't
788 * long enough for the minimum request.
790 if (tbno
> args
->agbno
)
792 if (tlen
< args
->minlen
)
795 if (tend
< args
->agbno
+ args
->minlen
)
799 * End of extent will be smaller of the freespace end and the
800 * maximal requested end.
802 * Fix the length according to mod and prod if given.
804 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
806 xfs_alloc_fix_len(args
);
807 ASSERT(args
->agbno
+ args
->len
<= tend
);
810 * We are allocating agbno for args->len
811 * Allocate/initialize a cursor for the by-size btree.
813 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
814 args
->agno
, XFS_BTNUM_CNT
);
815 ASSERT(args
->agbno
+ args
->len
<=
816 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
817 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
818 args
->len
, XFSA_FIXUP_BNO_OK
);
820 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
824 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
825 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
828 trace_xfs_alloc_exact_done(args
);
832 /* Didn't find it, return null. */
833 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
834 args
->agbno
= NULLAGBLOCK
;
835 trace_xfs_alloc_exact_notfound(args
);
839 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
840 trace_xfs_alloc_exact_error(args
);
845 * Search the btree in a given direction via the search cursor and compare
846 * the records found against the good extent we've already found.
849 xfs_alloc_find_best_extent(
850 struct xfs_alloc_arg
*args
, /* allocation argument structure */
851 struct xfs_btree_cur
**gcur
, /* good cursor */
852 struct xfs_btree_cur
**scur
, /* searching cursor */
853 xfs_agblock_t gdiff
, /* difference for search comparison */
854 xfs_agblock_t
*sbno
, /* extent found by search */
855 xfs_extlen_t
*slen
, /* extent length */
856 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
857 xfs_extlen_t
*slena
, /* aligned extent length */
858 int dir
) /* 0 = search right, 1 = search left */
866 /* The good extent is perfect, no need to search. */
871 * Look until we find a better one, run out of space or run off the end.
874 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
877 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
878 xfs_alloc_compute_aligned(args
, *sbno
, *slen
,
879 sbnoa
, slena
, &busy_gen
);
882 * The good extent is closer than this one.
885 if (*sbnoa
> args
->max_agbno
)
887 if (*sbnoa
>= args
->agbno
+ gdiff
)
890 if (*sbnoa
< args
->min_agbno
)
892 if (*sbnoa
<= args
->agbno
- gdiff
)
897 * Same distance, compare length and pick the best.
899 if (*slena
>= args
->minlen
) {
900 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
901 xfs_alloc_fix_len(args
);
903 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
905 args
->datatype
, *sbnoa
,
909 * Choose closer size and invalidate other cursor.
917 error
= xfs_btree_increment(*scur
, 0, &i
);
919 error
= xfs_btree_decrement(*scur
, 0, &i
);
925 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
930 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
935 /* caller invalidates cursors */
940 * Allocate a variable extent near bno in the allocation group agno.
941 * Extent's length (returned in len) will be between minlen and maxlen,
942 * and of the form k * prod + mod unless there's nothing that large.
943 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
945 STATIC
int /* error */
946 xfs_alloc_ag_vextent_near(
947 xfs_alloc_arg_t
*args
) /* allocation argument structure */
949 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
950 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
951 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
952 xfs_agblock_t gtbno
; /* start bno of right side entry */
953 xfs_agblock_t gtbnoa
; /* aligned ... */
954 xfs_extlen_t gtdiff
; /* difference to right side entry */
955 xfs_extlen_t gtlen
; /* length of right side entry */
956 xfs_extlen_t gtlena
; /* aligned ... */
957 xfs_agblock_t gtnew
; /* useful start bno of right side */
958 int error
; /* error code */
959 int i
; /* result code, temporary */
960 int j
; /* result code, temporary */
961 xfs_agblock_t ltbno
; /* start bno of left side entry */
962 xfs_agblock_t ltbnoa
; /* aligned ... */
963 xfs_extlen_t ltdiff
; /* difference to left side entry */
964 xfs_extlen_t ltlen
; /* length of left side entry */
965 xfs_extlen_t ltlena
; /* aligned ... */
966 xfs_agblock_t ltnew
; /* useful start bno of left side */
967 xfs_extlen_t rlen
; /* length of returned extent */
972 * Randomly don't execute the first algorithm.
974 int dofirst
; /* set to do first algorithm */
976 dofirst
= prandom_u32() & 1;
979 /* handle unitialized agbno range so caller doesn't have to */
980 if (!args
->min_agbno
&& !args
->max_agbno
)
981 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
982 ASSERT(args
->min_agbno
<= args
->max_agbno
);
984 /* clamp agbno to the range if it's outside */
985 if (args
->agbno
< args
->min_agbno
)
986 args
->agbno
= args
->min_agbno
;
987 if (args
->agbno
> args
->max_agbno
)
988 args
->agbno
= args
->max_agbno
;
999 * Get a cursor for the by-size btree.
1001 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1002 args
->agno
, XFS_BTNUM_CNT
);
1005 * See if there are any free extents as big as maxlen.
1007 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
1010 * If none, then pick up the last entry in the tree unless the
1014 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
1017 if (i
== 0 || ltlen
== 0) {
1018 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1019 trace_xfs_alloc_near_noentry(args
);
1024 args
->wasfromfl
= 0;
1028 * If the requested extent is large wrt the freespaces available
1029 * in this a.g., then the cursor will be pointing to a btree entry
1030 * near the right edge of the tree. If it's in the last btree leaf
1031 * block, then we just examine all the entries in that block
1032 * that are big enough, and pick the best one.
1033 * This is written as a while loop so we can break out of it,
1034 * but we never loop back to the top.
1036 while (xfs_btree_islastblock(cnt_cur
, 0)) {
1039 xfs_extlen_t blen
=0;
1040 xfs_agblock_t bnew
=0;
1047 * Start from the entry that lookup found, sequence through
1048 * all larger free blocks. If we're actually pointing at a
1049 * record smaller than maxlen, go to the start of this block,
1050 * and skip all those smaller than minlen.
1052 if (ltlen
|| args
->alignment
> 1) {
1053 cnt_cur
->bc_ptrs
[0] = 1;
1055 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
1058 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1059 if (ltlen
>= args
->minlen
)
1061 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
1064 ASSERT(ltlen
>= args
->minlen
);
1068 i
= cnt_cur
->bc_ptrs
[0];
1069 for (j
= 1, blen
= 0, bdiff
= 0;
1070 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
1071 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
1073 * For each entry, decide if it's better than
1074 * the previous best entry.
1076 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1078 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1079 busy
= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1080 <bnoa
, <lena
, &busy_gen
);
1081 if (ltlena
< args
->minlen
)
1083 if (ltbnoa
< args
->min_agbno
|| ltbnoa
> args
->max_agbno
)
1085 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1086 xfs_alloc_fix_len(args
);
1087 ASSERT(args
->len
>= args
->minlen
);
1088 if (args
->len
< blen
)
1090 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1091 args
->alignment
, args
->datatype
, ltbnoa
,
1093 if (ltnew
!= NULLAGBLOCK
&&
1094 (args
->len
> blen
|| ltdiff
< bdiff
)) {
1098 besti
= cnt_cur
->bc_ptrs
[0];
1102 * It didn't work. We COULD be in a case where
1103 * there's a good record somewhere, so try again.
1108 * Point at the best entry, and retrieve it again.
1110 cnt_cur
->bc_ptrs
[0] = besti
;
1111 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1113 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1114 ASSERT(ltbno
+ ltlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1118 * We are allocating starting at bnew for blen blocks.
1121 ASSERT(bnew
>= ltbno
);
1122 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1124 * Set up a cursor for the by-bno tree.
1126 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1127 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1129 * Fix up the btree entries.
1131 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1132 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1134 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1135 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1137 trace_xfs_alloc_near_first(args
);
1142 * Search in the by-bno tree to the left and to the right
1143 * simultaneously, until in each case we find a space big enough,
1144 * or run into the edge of the tree. When we run into the edge,
1145 * we deallocate that cursor.
1146 * If both searches succeed, we compare the two spaces and pick
1148 * With alignment, it's possible for both to fail; the upper
1149 * level algorithm that picks allocation groups for allocations
1150 * is not supposed to do this.
1153 * Allocate and initialize the cursor for the leftward search.
1155 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1156 args
->agno
, XFS_BTNUM_BNO
);
1158 * Lookup <= bno to find the leftward search's starting point.
1160 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1164 * Didn't find anything; use this cursor for the rightward
1167 bno_cur_gt
= bno_cur_lt
;
1171 * Found something. Duplicate the cursor for the rightward search.
1173 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1176 * Increment the cursor, so we will point at the entry just right
1177 * of the leftward entry if any, or to the leftmost entry.
1179 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1183 * It failed, there are no rightward entries.
1185 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1189 * Loop going left with the leftward cursor, right with the
1190 * rightward cursor, until either both directions give up or
1191 * we find an entry at least as big as minlen.
1195 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1197 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1198 busy
|= xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1199 <bnoa
, <lena
, &busy_gen
);
1200 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1202 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1204 if (!i
|| ltbnoa
< args
->min_agbno
) {
1205 xfs_btree_del_cursor(bno_cur_lt
,
1211 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1213 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1214 busy
|= xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1215 >bnoa
, >lena
, &busy_gen
);
1216 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1218 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1220 if (!i
|| gtbnoa
> args
->max_agbno
) {
1221 xfs_btree_del_cursor(bno_cur_gt
,
1226 } while (bno_cur_lt
|| bno_cur_gt
);
1229 * Got both cursors still active, need to find better entry.
1231 if (bno_cur_lt
&& bno_cur_gt
) {
1232 if (ltlena
>= args
->minlen
) {
1234 * Left side is good, look for a right side entry.
1236 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1237 xfs_alloc_fix_len(args
);
1238 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1239 args
->alignment
, args
->datatype
, ltbnoa
,
1242 error
= xfs_alloc_find_best_extent(args
,
1243 &bno_cur_lt
, &bno_cur_gt
,
1244 ltdiff
, >bno
, >len
,
1246 0 /* search right */);
1248 ASSERT(gtlena
>= args
->minlen
);
1251 * Right side is good, look for a left side entry.
1253 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1254 xfs_alloc_fix_len(args
);
1255 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1256 args
->alignment
, args
->datatype
, gtbnoa
,
1259 error
= xfs_alloc_find_best_extent(args
,
1260 &bno_cur_gt
, &bno_cur_lt
,
1261 gtdiff
, <bno
, <len
,
1263 1 /* search left */);
1271 * If we couldn't get anything, give up.
1273 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1274 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1277 trace_xfs_alloc_near_busy(args
);
1278 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1281 trace_xfs_alloc_size_neither(args
);
1282 args
->agbno
= NULLAGBLOCK
;
1287 * At this point we have selected a freespace entry, either to the
1288 * left or to the right. If it's on the right, copy all the
1289 * useful variables to the "left" set so we only have one
1290 * copy of this code.
1293 bno_cur_lt
= bno_cur_gt
;
1304 * Fix up the length and compute the useful address.
1306 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1307 xfs_alloc_fix_len(args
);
1309 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1310 args
->datatype
, ltbnoa
, ltlena
, <new
);
1311 ASSERT(ltnew
>= ltbno
);
1312 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1313 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1314 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1315 args
->agbno
= ltnew
;
1317 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1318 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1322 trace_xfs_alloc_near_greater(args
);
1324 trace_xfs_alloc_near_lesser(args
);
1326 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1327 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1331 trace_xfs_alloc_near_error(args
);
1332 if (cnt_cur
!= NULL
)
1333 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1334 if (bno_cur_lt
!= NULL
)
1335 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1336 if (bno_cur_gt
!= NULL
)
1337 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1342 * Allocate a variable extent anywhere in the allocation group agno.
1343 * Extent's length (returned in len) will be between minlen and maxlen,
1344 * and of the form k * prod + mod unless there's nothing that large.
1345 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1347 STATIC
int /* error */
1348 xfs_alloc_ag_vextent_size(
1349 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1351 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1352 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1353 int error
; /* error result */
1354 xfs_agblock_t fbno
; /* start of found freespace */
1355 xfs_extlen_t flen
; /* length of found freespace */
1356 int i
; /* temp status variable */
1357 xfs_agblock_t rbno
; /* returned block number */
1358 xfs_extlen_t rlen
; /* length of returned extent */
1364 * Allocate and initialize a cursor for the by-size btree.
1366 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1367 args
->agno
, XFS_BTNUM_CNT
);
1372 * Look for an entry >= maxlen+alignment-1 blocks.
1374 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1375 args
->maxlen
+ args
->alignment
- 1, &i
)))
1379 * If none then we have to settle for a smaller extent. In the case that
1380 * there are no large extents, this will return the last entry in the
1381 * tree unless the tree is empty. In the case that there are only busy
1382 * large extents, this will return the largest small extent unless there
1383 * are no smaller extents available.
1386 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1390 if (i
== 0 || flen
== 0) {
1391 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1392 trace_xfs_alloc_size_noentry(args
);
1396 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
,
1400 * Search for a non-busy extent that is large enough.
1403 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1406 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1408 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1409 &rbno
, &rlen
, &busy_gen
);
1411 if (rlen
>= args
->maxlen
)
1414 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1419 * Our only valid extents must have been busy.
1420 * Make it unbusy by forcing the log out and
1423 xfs_btree_del_cursor(cnt_cur
,
1425 trace_xfs_alloc_size_busy(args
);
1426 xfs_extent_busy_flush(args
->mp
,
1427 args
->pag
, busy_gen
);
1434 * In the first case above, we got the last entry in the
1435 * by-size btree. Now we check to see if the space hits maxlen
1436 * once aligned; if not, we search left for something better.
1437 * This can't happen in the second case above.
1439 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1440 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1441 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1442 if (rlen
< args
->maxlen
) {
1443 xfs_agblock_t bestfbno
;
1444 xfs_extlen_t bestflen
;
1445 xfs_agblock_t bestrbno
;
1446 xfs_extlen_t bestrlen
;
1453 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1457 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1460 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1461 if (flen
< bestrlen
)
1463 busy
= xfs_alloc_compute_aligned(args
, fbno
, flen
,
1464 &rbno
, &rlen
, &busy_gen
);
1465 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1466 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1467 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1469 if (rlen
> bestrlen
) {
1474 if (rlen
== args
->maxlen
)
1478 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1481 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1487 args
->wasfromfl
= 0;
1489 * Fix up the length.
1492 if (rlen
< args
->minlen
) {
1494 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1495 trace_xfs_alloc_size_busy(args
);
1496 xfs_extent_busy_flush(args
->mp
, args
->pag
, busy_gen
);
1501 xfs_alloc_fix_len(args
);
1504 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1506 * Allocate and initialize a cursor for the by-block tree.
1508 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1509 args
->agno
, XFS_BTNUM_BNO
);
1510 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1511 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1513 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1514 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1515 cnt_cur
= bno_cur
= NULL
;
1518 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1519 args
->agbno
+ args
->len
<=
1520 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1522 trace_xfs_alloc_size_done(args
);
1526 trace_xfs_alloc_size_error(args
);
1528 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1530 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1534 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1535 trace_xfs_alloc_size_nominleft(args
);
1536 args
->agbno
= NULLAGBLOCK
;
1541 * Deal with the case where only small freespaces remain.
1542 * Either return the contents of the last freespace record,
1543 * or allocate space from the freelist if there is nothing in the tree.
1545 STATIC
int /* error */
1546 xfs_alloc_ag_vextent_small(
1547 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1548 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1549 xfs_agblock_t
*fbnop
, /* result block number */
1550 xfs_extlen_t
*flenp
, /* result length */
1551 int *stat
) /* status: 0-freelist, 1-normal/none */
1553 struct xfs_owner_info oinfo
;
1554 struct xfs_perag
*pag
;
1560 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1563 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1565 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1568 * Nothing in the btree, try the freelist. Make sure
1569 * to respect minleft even when pulling from the
1572 else if (args
->minlen
== 1 && args
->alignment
== 1 &&
1573 args
->resv
!= XFS_AG_RESV_AGFL
&&
1574 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1576 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1579 if (fbno
!= NULLAGBLOCK
) {
1580 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1581 xfs_alloc_allow_busy_reuse(args
->datatype
));
1583 if (xfs_alloc_is_userdata(args
->datatype
)) {
1586 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1587 args
->agno
, fbno
, 0);
1589 error
= -EFSCORRUPTED
;
1592 xfs_trans_binval(args
->tp
, bp
);
1596 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1597 args
->agbno
+ args
->len
<=
1598 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1600 args
->wasfromfl
= 1;
1601 trace_xfs_alloc_small_freelist(args
);
1604 * If we're feeding an AGFL block to something that
1605 * doesn't live in the free space, we need to clear
1606 * out the OWN_AG rmap and add the block back to
1607 * the AGFL per-AG reservation.
1609 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_AG
);
1610 error
= xfs_rmap_free(args
->tp
, args
->agbp
, args
->agno
,
1614 pag
= xfs_perag_get(args
->mp
, args
->agno
);
1615 xfs_ag_resv_free_extent(pag
, XFS_AG_RESV_AGFL
,
1623 * Nothing in the freelist.
1629 * Can't allocate from the freelist for some reason.
1636 * Can't do the allocation, give up.
1638 if (flen
< args
->minlen
) {
1639 args
->agbno
= NULLAGBLOCK
;
1640 trace_xfs_alloc_small_notenough(args
);
1646 trace_xfs_alloc_small_done(args
);
1650 trace_xfs_alloc_small_error(args
);
1655 * Free the extent starting at agno/bno for length.
1661 xfs_agnumber_t agno
,
1664 struct xfs_owner_info
*oinfo
,
1665 enum xfs_ag_resv_type type
)
1667 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1668 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1669 int error
; /* error return value */
1670 xfs_agblock_t gtbno
; /* start of right neighbor block */
1671 xfs_extlen_t gtlen
; /* length of right neighbor block */
1672 int haveleft
; /* have a left neighbor block */
1673 int haveright
; /* have a right neighbor block */
1674 int i
; /* temp, result code */
1675 xfs_agblock_t ltbno
; /* start of left neighbor block */
1676 xfs_extlen_t ltlen
; /* length of left neighbor block */
1677 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1678 xfs_agblock_t nbno
; /* new starting block of freespace */
1679 xfs_extlen_t nlen
; /* new length of freespace */
1680 xfs_perag_t
*pag
; /* per allocation group data */
1682 bno_cur
= cnt_cur
= NULL
;
1685 if (!xfs_rmap_should_skip_owner_update(oinfo
)) {
1686 error
= xfs_rmap_free(tp
, agbp
, agno
, bno
, len
, oinfo
);
1692 * Allocate and initialize a cursor for the by-block btree.
1694 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1696 * Look for a neighboring block on the left (lower block numbers)
1697 * that is contiguous with this space.
1699 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1703 * There is a block to our left.
1705 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1707 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1709 * It's not contiguous, though.
1711 if (ltbno
+ ltlen
< bno
)
1715 * If this failure happens the request to free this
1716 * space was invalid, it's (partly) already free.
1719 XFS_WANT_CORRUPTED_GOTO(mp
,
1720 ltbno
+ ltlen
<= bno
, error0
);
1724 * Look for a neighboring block on the right (higher block numbers)
1725 * that is contiguous with this space.
1727 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1731 * There is a block to our right.
1733 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1735 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1737 * It's not contiguous, though.
1739 if (bno
+ len
< gtbno
)
1743 * If this failure happens the request to free this
1744 * space was invalid, it's (partly) already free.
1747 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1751 * Now allocate and initialize a cursor for the by-size tree.
1753 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1755 * Have both left and right contiguous neighbors.
1756 * Merge all three into a single free block.
1758 if (haveleft
&& haveright
) {
1760 * Delete the old by-size entry on the left.
1762 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1764 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1765 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1767 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1769 * Delete the old by-size entry on the right.
1771 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &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-block entry for the right block.
1780 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1782 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1784 * Move the by-block cursor back to the left neighbor.
1786 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1788 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1791 * Check that this is the right record: delete didn't
1792 * mangle the cursor.
1795 xfs_agblock_t xxbno
;
1798 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1801 XFS_WANT_CORRUPTED_GOTO(mp
,
1802 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1807 * Update remaining by-block entry to the new, joined block.
1810 nlen
= len
+ ltlen
+ gtlen
;
1811 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1815 * Have only a left contiguous neighbor.
1816 * Merge it together with the new freespace.
1818 else if (haveleft
) {
1820 * Delete the old by-size entry on the left.
1822 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1824 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1825 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1827 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1829 * Back up the by-block cursor to the left neighbor, and
1830 * update its length.
1832 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1834 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1837 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1841 * Have only a right contiguous neighbor.
1842 * Merge it together with the new freespace.
1844 else if (haveright
) {
1846 * Delete the old by-size entry on the right.
1848 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1850 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1851 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1853 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1855 * Update the starting block and length of the right
1856 * neighbor in the by-block tree.
1860 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1864 * No contiguous neighbors.
1865 * Insert the new freespace into the by-block tree.
1870 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1872 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1874 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1877 * In all cases we need to insert the new freespace in the by-size tree.
1879 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1881 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1882 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1884 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1885 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1889 * Update the freespace totals in the ag and superblock.
1891 pag
= xfs_perag_get(mp
, agno
);
1892 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1893 xfs_ag_resv_free_extent(pag
, type
, tp
, len
);
1898 XFS_STATS_INC(mp
, xs_freex
);
1899 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1901 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1902 haveleft
, haveright
);
1907 trace_xfs_free_extent(mp
, agno
, bno
, len
, type
== XFS_AG_RESV_AGFL
,
1910 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1912 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1917 * Visible (exported) allocation/free functions.
1918 * Some of these are used just by xfs_alloc_btree.c and this file.
1922 * Compute and fill in value of m_ag_maxlevels.
1925 xfs_alloc_compute_maxlevels(
1926 xfs_mount_t
*mp
) /* file system mount structure */
1928 mp
->m_ag_maxlevels
= xfs_btree_compute_maxlevels(mp
, mp
->m_alloc_mnr
,
1929 (mp
->m_sb
.sb_agblocks
+ 1) / 2);
1933 * Find the length of the longest extent in an AG. The 'need' parameter
1934 * specifies how much space we're going to need for the AGFL and the
1935 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1939 xfs_alloc_longest_free_extent(
1940 struct xfs_mount
*mp
,
1941 struct xfs_perag
*pag
,
1943 xfs_extlen_t reserved
)
1945 xfs_extlen_t delta
= 0;
1948 * If the AGFL needs a recharge, we'll have to subtract that from the
1951 if (need
> pag
->pagf_flcount
)
1952 delta
= need
- pag
->pagf_flcount
;
1955 * If we cannot maintain others' reservations with space from the
1956 * not-longest freesp extents, we'll have to subtract /that/ from
1957 * the longest extent too.
1959 if (pag
->pagf_freeblks
- pag
->pagf_longest
< reserved
)
1960 delta
+= reserved
- (pag
->pagf_freeblks
- pag
->pagf_longest
);
1963 * If the longest extent is long enough to satisfy all the
1964 * reservations and AGFL rules in place, we can return this extent.
1966 if (pag
->pagf_longest
> delta
)
1967 return pag
->pagf_longest
- delta
;
1969 /* Otherwise, let the caller try for 1 block if there's space. */
1970 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
1974 xfs_alloc_min_freelist(
1975 struct xfs_mount
*mp
,
1976 struct xfs_perag
*pag
)
1978 unsigned int min_free
;
1980 /* space needed by-bno freespace btree */
1981 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
1982 mp
->m_ag_maxlevels
);
1983 /* space needed by-size freespace btree */
1984 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
1985 mp
->m_ag_maxlevels
);
1986 /* space needed reverse mapping used space btree */
1987 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
))
1988 min_free
+= min_t(unsigned int,
1989 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] + 1,
1990 mp
->m_rmap_maxlevels
);
1996 * Check if the operation we are fixing up the freelist for should go ahead or
1997 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1998 * is dependent on whether the size and shape of free space available will
1999 * permit the requested allocation to take place.
2002 xfs_alloc_space_available(
2003 struct xfs_alloc_arg
*args
,
2004 xfs_extlen_t min_free
,
2007 struct xfs_perag
*pag
= args
->pag
;
2008 xfs_extlen_t alloc_len
, longest
;
2009 xfs_extlen_t reservation
; /* blocks that are still reserved */
2012 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2015 reservation
= xfs_ag_resv_needed(pag
, args
->resv
);
2017 /* do we have enough contiguous free space for the allocation? */
2018 alloc_len
= args
->minlen
+ (args
->alignment
- 1) + args
->minalignslop
;
2019 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
,
2021 if (longest
< alloc_len
)
2024 /* do we have enough free space remaining for the allocation? */
2025 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
2026 reservation
- min_free
- args
->minleft
);
2027 if (available
< (int)max(args
->total
, alloc_len
))
2031 * Clamp maxlen to the amount of free space available for the actual
2032 * extent allocation.
2034 if (available
< (int)args
->maxlen
&& !(flags
& XFS_ALLOC_FLAG_CHECK
)) {
2035 args
->maxlen
= available
;
2036 ASSERT(args
->maxlen
> 0);
2037 ASSERT(args
->maxlen
>= args
->minlen
);
2044 * Decide whether to use this allocation group for this allocation.
2045 * If so, fix up the btree freelist's size.
2048 xfs_alloc_fix_freelist(
2049 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2050 int flags
) /* XFS_ALLOC_FLAG_... */
2052 struct xfs_mount
*mp
= args
->mp
;
2053 struct xfs_perag
*pag
= args
->pag
;
2054 struct xfs_trans
*tp
= args
->tp
;
2055 struct xfs_buf
*agbp
= NULL
;
2056 struct xfs_buf
*agflbp
= NULL
;
2057 struct xfs_alloc_arg targs
; /* local allocation arguments */
2058 xfs_agblock_t bno
; /* freelist block */
2059 xfs_extlen_t need
; /* total blocks needed in freelist */
2062 if (!pag
->pagf_init
) {
2063 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2066 if (!pag
->pagf_init
) {
2067 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2068 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2069 goto out_agbp_relse
;
2074 * If this is a metadata preferred pag and we are user data then try
2075 * somewhere else if we are not being asked to try harder at this
2078 if (pag
->pagf_metadata
&& xfs_alloc_is_userdata(args
->datatype
) &&
2079 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2080 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2081 goto out_agbp_relse
;
2084 need
= xfs_alloc_min_freelist(mp
, pag
);
2085 if (!xfs_alloc_space_available(args
, need
, flags
|
2086 XFS_ALLOC_FLAG_CHECK
))
2087 goto out_agbp_relse
;
2090 * Get the a.g. freespace buffer.
2091 * Can fail if we're not blocking on locks, and it's held.
2094 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2098 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2099 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2104 /* If there isn't enough total space or single-extent, reject it. */
2105 need
= xfs_alloc_min_freelist(mp
, pag
);
2106 if (!xfs_alloc_space_available(args
, need
, flags
))
2107 goto out_agbp_relse
;
2110 * Make the freelist shorter if it's too long.
2112 * Note that from this point onwards, we will always release the agf and
2113 * agfl buffers on error. This handles the case where we error out and
2114 * the buffers are clean or may not have been joined to the transaction
2115 * and hence need to be released manually. If they have been joined to
2116 * the transaction, then xfs_trans_brelse() will handle them
2117 * appropriately based on the recursion count and dirty state of the
2120 * XXX (dgc): When we have lots of free space, does this buy us
2121 * anything other than extra overhead when we need to put more blocks
2122 * back on the free list? Maybe we should only do this when space is
2123 * getting low or the AGFL is more than half full?
2125 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2126 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2127 * updating the rmapbt. Both flags are used in xfs_repair while we're
2128 * rebuilding the rmapbt, and neither are used by the kernel. They're
2129 * both required to ensure that rmaps are correctly recorded for the
2130 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2131 * repair/rmap.c in xfsprogs for details.
2133 memset(&targs
, 0, sizeof(targs
));
2134 if (flags
& XFS_ALLOC_FLAG_NORMAP
)
2135 xfs_rmap_skip_owner_update(&targs
.oinfo
);
2137 xfs_rmap_ag_owner(&targs
.oinfo
, XFS_RMAP_OWN_AG
);
2138 while (!(flags
& XFS_ALLOC_FLAG_NOSHRINK
) && pag
->pagf_flcount
> need
) {
2141 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2143 goto out_agbp_relse
;
2144 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1,
2145 &targs
.oinfo
, XFS_AG_RESV_AGFL
);
2147 goto out_agbp_relse
;
2148 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2150 error
= -EFSCORRUPTED
;
2151 goto out_agbp_relse
;
2153 xfs_trans_binval(tp
, bp
);
2159 targs
.agno
= args
->agno
;
2160 targs
.alignment
= targs
.minlen
= targs
.prod
= 1;
2161 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2163 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2165 goto out_agbp_relse
;
2167 /* Make the freelist longer if it's too short. */
2168 while (pag
->pagf_flcount
< need
) {
2170 targs
.maxlen
= need
- pag
->pagf_flcount
;
2171 targs
.resv
= XFS_AG_RESV_AGFL
;
2173 /* Allocate as many blocks as possible at once. */
2174 error
= xfs_alloc_ag_vextent(&targs
);
2176 goto out_agflbp_relse
;
2179 * Stop if we run out. Won't happen if callers are obeying
2180 * the restrictions correctly. Can happen for free calls
2181 * on a completely full ag.
2183 if (targs
.agbno
== NULLAGBLOCK
) {
2184 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2186 goto out_agflbp_relse
;
2189 * Put each allocated block on the list.
2191 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2192 error
= xfs_alloc_put_freelist(tp
, agbp
,
2195 goto out_agflbp_relse
;
2198 xfs_trans_brelse(tp
, agflbp
);
2203 xfs_trans_brelse(tp
, agflbp
);
2206 xfs_trans_brelse(tp
, agbp
);
2213 * Get a block from the freelist.
2214 * Returns with the buffer for the block gotten.
2217 xfs_alloc_get_freelist(
2218 xfs_trans_t
*tp
, /* transaction pointer */
2219 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2220 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2221 int btreeblk
) /* destination is a AGF btree */
2223 xfs_agf_t
*agf
; /* a.g. freespace structure */
2224 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2225 xfs_agblock_t bno
; /* block number returned */
2229 xfs_mount_t
*mp
= tp
->t_mountp
;
2230 xfs_perag_t
*pag
; /* per allocation group data */
2233 * Freelist is empty, give up.
2235 agf
= XFS_BUF_TO_AGF(agbp
);
2236 if (!agf
->agf_flcount
) {
2237 *bnop
= NULLAGBLOCK
;
2241 * Read the array of free blocks.
2243 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2250 * Get the block number and update the data structures.
2252 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2253 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2254 be32_add_cpu(&agf
->agf_flfirst
, 1);
2255 xfs_trans_brelse(tp
, agflbp
);
2256 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2257 agf
->agf_flfirst
= 0;
2259 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2260 be32_add_cpu(&agf
->agf_flcount
, -1);
2261 xfs_trans_agflist_delta(tp
, -1);
2262 pag
->pagf_flcount
--;
2265 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2267 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2268 pag
->pagf_btreeblks
++;
2269 logflags
|= XFS_AGF_BTREEBLKS
;
2272 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2279 * Log the given fields from the agf structure.
2283 xfs_trans_t
*tp
, /* transaction pointer */
2284 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2285 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2287 int first
; /* first byte offset */
2288 int last
; /* last byte offset */
2289 static const short offsets
[] = {
2290 offsetof(xfs_agf_t
, agf_magicnum
),
2291 offsetof(xfs_agf_t
, agf_versionnum
),
2292 offsetof(xfs_agf_t
, agf_seqno
),
2293 offsetof(xfs_agf_t
, agf_length
),
2294 offsetof(xfs_agf_t
, agf_roots
[0]),
2295 offsetof(xfs_agf_t
, agf_levels
[0]),
2296 offsetof(xfs_agf_t
, agf_flfirst
),
2297 offsetof(xfs_agf_t
, agf_fllast
),
2298 offsetof(xfs_agf_t
, agf_flcount
),
2299 offsetof(xfs_agf_t
, agf_freeblks
),
2300 offsetof(xfs_agf_t
, agf_longest
),
2301 offsetof(xfs_agf_t
, agf_btreeblks
),
2302 offsetof(xfs_agf_t
, agf_uuid
),
2303 offsetof(xfs_agf_t
, agf_rmap_blocks
),
2304 offsetof(xfs_agf_t
, agf_refcount_blocks
),
2305 offsetof(xfs_agf_t
, agf_refcount_root
),
2306 offsetof(xfs_agf_t
, agf_refcount_level
),
2307 /* needed so that we don't log the whole rest of the structure: */
2308 offsetof(xfs_agf_t
, agf_spare64
),
2312 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2314 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2316 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2317 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2321 * Interface for inode allocation to force the pag data to be initialized.
2324 xfs_alloc_pagf_init(
2325 xfs_mount_t
*mp
, /* file system mount structure */
2326 xfs_trans_t
*tp
, /* transaction pointer */
2327 xfs_agnumber_t agno
, /* allocation group number */
2328 int flags
) /* XFS_ALLOC_FLAGS_... */
2333 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2336 xfs_trans_brelse(tp
, bp
);
2341 * Put the block on the freelist for the allocation group.
2344 xfs_alloc_put_freelist(
2345 xfs_trans_t
*tp
, /* transaction pointer */
2346 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2347 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2348 xfs_agblock_t bno
, /* block being freed */
2349 int btreeblk
) /* block came from a AGF btree */
2351 xfs_agf_t
*agf
; /* a.g. freespace structure */
2352 __be32
*blockp
;/* pointer to array entry */
2355 xfs_mount_t
*mp
; /* mount structure */
2356 xfs_perag_t
*pag
; /* per allocation group data */
2360 agf
= XFS_BUF_TO_AGF(agbp
);
2363 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2364 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2366 be32_add_cpu(&agf
->agf_fllast
, 1);
2367 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2368 agf
->agf_fllast
= 0;
2370 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2371 be32_add_cpu(&agf
->agf_flcount
, 1);
2372 xfs_trans_agflist_delta(tp
, 1);
2373 pag
->pagf_flcount
++;
2375 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2377 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2378 pag
->pagf_btreeblks
--;
2379 logflags
|= XFS_AGF_BTREEBLKS
;
2383 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2385 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2387 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2388 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2389 *blockp
= cpu_to_be32(bno
);
2390 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2392 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2394 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2395 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2396 startoff
+ sizeof(xfs_agblock_t
) - 1);
2402 struct xfs_mount
*mp
,
2405 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2407 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2408 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2410 if (!xfs_log_check_lsn(mp
,
2411 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2415 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2416 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2417 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2418 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2419 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2420 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
)))
2423 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) < 1 ||
2424 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) < 1 ||
2425 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2426 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2429 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
) &&
2430 (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) < 1 ||
2431 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]) > XFS_BTREE_MAXLEVELS
))
2435 * during growfs operations, the perag is not fully initialised,
2436 * so we can't use it for any useful checking. growfs ensures we can't
2437 * use it by using uncached buffers that don't have the perag attached
2438 * so we can detect and avoid this problem.
2440 if (bp
->b_pag
&& be32_to_cpu(agf
->agf_seqno
) != bp
->b_pag
->pag_agno
)
2443 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2444 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2447 if (xfs_sb_version_hasreflink(&mp
->m_sb
) &&
2448 (be32_to_cpu(agf
->agf_refcount_level
) < 1 ||
2449 be32_to_cpu(agf
->agf_refcount_level
) > XFS_BTREE_MAXLEVELS
))
2457 xfs_agf_read_verify(
2460 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2462 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2463 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2464 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2465 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp
, bp
), mp
,
2466 XFS_ERRTAG_ALLOC_READ_AGF
))
2467 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2470 xfs_verifier_error(bp
);
2474 xfs_agf_write_verify(
2477 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2478 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2480 if (!xfs_agf_verify(mp
, bp
)) {
2481 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2482 xfs_verifier_error(bp
);
2486 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2490 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2492 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2495 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2497 .verify_read
= xfs_agf_read_verify
,
2498 .verify_write
= xfs_agf_write_verify
,
2502 * Read in the allocation group header (free/alloc section).
2506 struct xfs_mount
*mp
, /* mount point structure */
2507 struct xfs_trans
*tp
, /* transaction pointer */
2508 xfs_agnumber_t agno
, /* allocation group number */
2509 int flags
, /* XFS_BUF_ */
2510 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2514 trace_xfs_read_agf(mp
, agno
);
2516 ASSERT(agno
!= NULLAGNUMBER
);
2517 error
= xfs_trans_read_buf(
2518 mp
, tp
, mp
->m_ddev_targp
,
2519 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2520 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2526 ASSERT(!(*bpp
)->b_error
);
2527 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2532 * Read in the allocation group header (free/alloc section).
2536 struct xfs_mount
*mp
, /* mount point structure */
2537 struct xfs_trans
*tp
, /* transaction pointer */
2538 xfs_agnumber_t agno
, /* allocation group number */
2539 int flags
, /* XFS_ALLOC_FLAG_... */
2540 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2542 struct xfs_agf
*agf
; /* ag freelist header */
2543 struct xfs_perag
*pag
; /* per allocation group data */
2546 trace_xfs_alloc_read_agf(mp
, agno
);
2548 ASSERT(agno
!= NULLAGNUMBER
);
2549 error
= xfs_read_agf(mp
, tp
, agno
,
2550 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2556 ASSERT(!(*bpp
)->b_error
);
2558 agf
= XFS_BUF_TO_AGF(*bpp
);
2559 pag
= xfs_perag_get(mp
, agno
);
2560 if (!pag
->pagf_init
) {
2561 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2562 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2563 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2564 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2565 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2566 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2567 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2568 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2569 pag
->pagf_levels
[XFS_BTNUM_RMAPi
] =
2570 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAPi
]);
2571 pag
->pagf_refcount_level
= be32_to_cpu(agf
->agf_refcount_level
);
2572 spin_lock_init(&pag
->pagb_lock
);
2573 pag
->pagb_count
= 0;
2574 pag
->pagb_tree
= RB_ROOT
;
2578 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2579 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2580 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2581 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2582 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2583 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2584 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2585 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2586 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2594 * Allocate an extent (variable-size).
2595 * Depending on the allocation type, we either look in a single allocation
2596 * group or loop over the allocation groups to find the result.
2600 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2602 xfs_agblock_t agsize
; /* allocation group size */
2604 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2605 xfs_mount_t
*mp
; /* mount structure pointer */
2606 xfs_agnumber_t sagno
; /* starting allocation group number */
2607 xfs_alloctype_t type
; /* input allocation type */
2609 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2612 type
= args
->otype
= args
->type
;
2613 args
->agbno
= NULLAGBLOCK
;
2615 * Just fix this up, for the case where the last a.g. is shorter
2616 * (or there's only one a.g.) and the caller couldn't easily figure
2617 * that out (xfs_bmap_alloc).
2619 agsize
= mp
->m_sb
.sb_agblocks
;
2620 if (args
->maxlen
> agsize
)
2621 args
->maxlen
= agsize
;
2622 if (args
->alignment
== 0)
2623 args
->alignment
= 1;
2624 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2625 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2626 ASSERT(args
->minlen
<= args
->maxlen
);
2627 ASSERT(args
->minlen
<= agsize
);
2628 ASSERT(args
->mod
< args
->prod
);
2629 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2630 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2631 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2632 args
->mod
>= args
->prod
) {
2633 args
->fsbno
= NULLFSBLOCK
;
2634 trace_xfs_alloc_vextent_badargs(args
);
2639 case XFS_ALLOCTYPE_THIS_AG
:
2640 case XFS_ALLOCTYPE_NEAR_BNO
:
2641 case XFS_ALLOCTYPE_THIS_BNO
:
2643 * These three force us into a single a.g.
2645 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2646 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2647 error
= xfs_alloc_fix_freelist(args
, 0);
2649 trace_xfs_alloc_vextent_nofix(args
);
2653 trace_xfs_alloc_vextent_noagbp(args
);
2656 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2657 if ((error
= xfs_alloc_ag_vextent(args
)))
2660 case XFS_ALLOCTYPE_START_BNO
:
2662 * Try near allocation first, then anywhere-in-ag after
2663 * the first a.g. fails.
2665 if ((args
->datatype
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2666 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2667 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2668 ((mp
->m_agfrotor
/ rotorstep
) %
2669 mp
->m_sb
.sb_agcount
), 0);
2672 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2673 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2675 case XFS_ALLOCTYPE_FIRST_AG
:
2677 * Rotate through the allocation groups looking for a winner.
2679 if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2681 * Start with allocation group given by bno.
2683 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2684 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2689 * Start with the given allocation group.
2691 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2692 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2695 * Loop over allocation groups twice; first time with
2696 * trylock set, second time without.
2699 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2700 error
= xfs_alloc_fix_freelist(args
, flags
);
2702 trace_xfs_alloc_vextent_nofix(args
);
2706 * If we get a buffer back then the allocation will fly.
2709 if ((error
= xfs_alloc_ag_vextent(args
)))
2714 trace_xfs_alloc_vextent_loopfailed(args
);
2717 * Didn't work, figure out the next iteration.
2719 if (args
->agno
== sagno
&&
2720 type
== XFS_ALLOCTYPE_START_BNO
)
2721 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2723 * For the first allocation, we can try any AG to get
2724 * space. However, if we already have allocated a
2725 * block, we don't want to try AGs whose number is below
2726 * sagno. Otherwise, we may end up with out-of-order
2727 * locking of AGF, which might cause deadlock.
2729 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2730 if (args
->firstblock
!= NULLFSBLOCK
)
2736 * Reached the starting a.g., must either be done
2737 * or switch to non-trylock mode.
2739 if (args
->agno
== sagno
) {
2741 args
->agbno
= NULLAGBLOCK
;
2742 trace_xfs_alloc_vextent_allfailed(args
);
2747 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2748 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2750 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2753 xfs_perag_put(args
->pag
);
2756 if (args
->agno
== sagno
)
2757 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2758 (mp
->m_sb
.sb_agcount
* rotorstep
);
2760 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2761 (mp
->m_sb
.sb_agcount
* rotorstep
);
2768 if (args
->agbno
== NULLAGBLOCK
)
2769 args
->fsbno
= NULLFSBLOCK
;
2771 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2773 ASSERT(args
->len
>= args
->minlen
);
2774 ASSERT(args
->len
<= args
->maxlen
);
2775 ASSERT(args
->agbno
% args
->alignment
== 0);
2776 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2780 /* Zero the extent if we were asked to do so */
2781 if (args
->datatype
& XFS_ALLOC_USERDATA_ZERO
) {
2782 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2788 xfs_perag_put(args
->pag
);
2791 xfs_perag_put(args
->pag
);
2795 /* Ensure that the freelist is at full capacity. */
2797 xfs_free_extent_fix_freelist(
2798 struct xfs_trans
*tp
,
2799 xfs_agnumber_t agno
,
2800 struct xfs_buf
**agbp
)
2802 struct xfs_alloc_arg args
;
2805 memset(&args
, 0, sizeof(struct xfs_alloc_arg
));
2807 args
.mp
= tp
->t_mountp
;
2811 * validate that the block number is legal - the enables us to detect
2812 * and handle a silent filesystem corruption rather than crashing.
2814 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2815 return -EFSCORRUPTED
;
2817 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2820 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2826 xfs_perag_put(args
.pag
);
2832 * Just break up the extent address and hand off to xfs_free_ag_extent
2833 * after fixing up the freelist.
2837 struct xfs_trans
*tp
, /* transaction pointer */
2838 xfs_fsblock_t bno
, /* starting block number of extent */
2839 xfs_extlen_t len
, /* length of extent */
2840 struct xfs_owner_info
*oinfo
, /* extent owner */
2841 enum xfs_ag_resv_type type
) /* block reservation type */
2843 struct xfs_mount
*mp
= tp
->t_mountp
;
2844 struct xfs_buf
*agbp
;
2845 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, bno
);
2846 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
2850 ASSERT(type
!= XFS_AG_RESV_AGFL
);
2852 if (XFS_TEST_ERROR(false, mp
,
2853 XFS_ERRTAG_FREE_EXTENT
))
2856 error
= xfs_free_extent_fix_freelist(tp
, agno
, &agbp
);
2860 XFS_WANT_CORRUPTED_GOTO(mp
, agbno
< mp
->m_sb
.sb_agblocks
, err
);
2862 /* validate the extent size is legal now we have the agf locked */
2863 XFS_WANT_CORRUPTED_GOTO(mp
,
2864 agbno
+ len
<= be32_to_cpu(XFS_BUF_TO_AGF(agbp
)->agf_length
),
2867 error
= xfs_free_ag_extent(tp
, agbp
, agno
, agbno
, len
, oinfo
, type
);
2871 xfs_extent_busy_insert(tp
, agno
, agbno
, len
, 0);
2875 xfs_trans_brelse(tp
, agbp
);
2879 struct xfs_alloc_query_range_info
{
2880 xfs_alloc_query_range_fn fn
;
2884 /* Format btree record and pass to our callback. */
2886 xfs_alloc_query_range_helper(
2887 struct xfs_btree_cur
*cur
,
2888 union xfs_btree_rec
*rec
,
2891 struct xfs_alloc_query_range_info
*query
= priv
;
2892 struct xfs_alloc_rec_incore irec
;
2894 irec
.ar_startblock
= be32_to_cpu(rec
->alloc
.ar_startblock
);
2895 irec
.ar_blockcount
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
2896 return query
->fn(cur
, &irec
, query
->priv
);
2899 /* Find all free space within a given range of blocks. */
2901 xfs_alloc_query_range(
2902 struct xfs_btree_cur
*cur
,
2903 struct xfs_alloc_rec_incore
*low_rec
,
2904 struct xfs_alloc_rec_incore
*high_rec
,
2905 xfs_alloc_query_range_fn fn
,
2908 union xfs_btree_irec low_brec
;
2909 union xfs_btree_irec high_brec
;
2910 struct xfs_alloc_query_range_info query
;
2912 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
2913 low_brec
.a
= *low_rec
;
2914 high_brec
.a
= *high_rec
;
2917 return xfs_btree_query_range(cur
, &low_brec
, &high_brec
,
2918 xfs_alloc_query_range_helper
, &query
);
2921 /* Find all free space records. */
2923 xfs_alloc_query_all(
2924 struct xfs_btree_cur
*cur
,
2925 xfs_alloc_query_range_fn fn
,
2928 struct xfs_alloc_query_range_info query
;
2930 ASSERT(cur
->bc_btnum
== XFS_BTNUM_BNO
);
2933 return xfs_btree_query_all(cur
, xfs_alloc_query_range_helper
, &query
);
2936 /* Find the size of the AG, in blocks. */
2939 struct xfs_mount
*mp
,
2940 xfs_agnumber_t agno
)
2942 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
2944 if (agno
< mp
->m_sb
.sb_agcount
- 1)
2945 return mp
->m_sb
.sb_agblocks
;
2946 return mp
->m_sb
.sb_dblocks
- (agno
* mp
->m_sb
.sb_agblocks
);
2950 * Verify that an AG block number pointer neither points outside the AG
2951 * nor points at static metadata.
2955 struct xfs_mount
*mp
,
2956 xfs_agnumber_t agno
,
2957 xfs_agblock_t agbno
)
2961 eoag
= xfs_ag_block_count(mp
, agno
);
2964 if (agbno
<= XFS_AGFL_BLOCK(mp
))
2970 * Verify that an FS block number pointer neither points outside the
2971 * filesystem nor points at static AG metadata.
2975 struct xfs_mount
*mp
,
2976 xfs_fsblock_t fsbno
)
2978 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, fsbno
);
2980 if (agno
>= mp
->m_sb
.sb_agcount
)
2982 return xfs_verify_agbno(mp
, agno
, XFS_FSB_TO_AGBNO(mp
, fsbno
));