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_inode.h"
28 #include "xfs_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_alloc.h"
31 #include "xfs_extent_busy.h"
32 #include "xfs_error.h"
33 #include "xfs_cksum.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_buf_item.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 *);
53 * Lookup the record equal to [bno, len] in the btree given by cur.
55 STATIC
int /* error */
57 struct xfs_btree_cur
*cur
, /* btree cursor */
58 xfs_agblock_t bno
, /* starting block of extent */
59 xfs_extlen_t len
, /* length of extent */
60 int *stat
) /* success/failure */
62 cur
->bc_rec
.a
.ar_startblock
= bno
;
63 cur
->bc_rec
.a
.ar_blockcount
= len
;
64 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
68 * Lookup the first record greater than or equal to [bno, len]
69 * in the btree given by cur.
73 struct xfs_btree_cur
*cur
, /* btree cursor */
74 xfs_agblock_t bno
, /* starting block of extent */
75 xfs_extlen_t len
, /* length of extent */
76 int *stat
) /* success/failure */
78 cur
->bc_rec
.a
.ar_startblock
= bno
;
79 cur
->bc_rec
.a
.ar_blockcount
= len
;
80 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
84 * Lookup the first record less than or equal to [bno, len]
85 * in the btree given by cur.
89 struct xfs_btree_cur
*cur
, /* btree cursor */
90 xfs_agblock_t bno
, /* starting block of extent */
91 xfs_extlen_t len
, /* length of extent */
92 int *stat
) /* success/failure */
94 cur
->bc_rec
.a
.ar_startblock
= bno
;
95 cur
->bc_rec
.a
.ar_blockcount
= len
;
96 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
100 * Update the record referred to by cur to the value given
102 * This either works (return 0) or gets an EFSCORRUPTED error.
104 STATIC
int /* error */
106 struct xfs_btree_cur
*cur
, /* btree cursor */
107 xfs_agblock_t bno
, /* starting block of extent */
108 xfs_extlen_t len
) /* length of extent */
110 union xfs_btree_rec rec
;
112 rec
.alloc
.ar_startblock
= cpu_to_be32(bno
);
113 rec
.alloc
.ar_blockcount
= cpu_to_be32(len
);
114 return xfs_btree_update(cur
, &rec
);
118 * Get the data from the pointed-to record.
122 struct xfs_btree_cur
*cur
, /* btree cursor */
123 xfs_agblock_t
*bno
, /* output: starting block of extent */
124 xfs_extlen_t
*len
, /* output: length of extent */
125 int *stat
) /* output: success/failure */
127 union xfs_btree_rec
*rec
;
130 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
131 if (!error
&& *stat
== 1) {
132 *bno
= be32_to_cpu(rec
->alloc
.ar_startblock
);
133 *len
= be32_to_cpu(rec
->alloc
.ar_blockcount
);
139 * Compute aligned version of the found extent.
140 * Takes alignment and min length into account.
143 xfs_alloc_compute_aligned(
144 xfs_alloc_arg_t
*args
, /* allocation argument structure */
145 xfs_agblock_t foundbno
, /* starting block in found extent */
146 xfs_extlen_t foundlen
, /* length in found extent */
147 xfs_agblock_t
*resbno
, /* result block number */
148 xfs_extlen_t
*reslen
) /* result length */
154 /* Trim busy sections out of found extent */
155 xfs_extent_busy_trim(args
, foundbno
, foundlen
, &bno
, &len
);
158 * If we have a largish extent that happens to start before min_agbno,
159 * see if we can shift it into range...
161 if (bno
< args
->min_agbno
&& bno
+ len
> args
->min_agbno
) {
162 diff
= args
->min_agbno
- bno
;
169 if (args
->alignment
> 1 && len
>= args
->minlen
) {
170 xfs_agblock_t aligned_bno
= roundup(bno
, args
->alignment
);
172 diff
= aligned_bno
- bno
;
174 *resbno
= aligned_bno
;
175 *reslen
= diff
>= len
? 0 : len
- diff
;
183 * Compute best start block and diff for "near" allocations.
184 * freelen >= wantlen already checked by caller.
186 STATIC xfs_extlen_t
/* difference value (absolute) */
187 xfs_alloc_compute_diff(
188 xfs_agblock_t wantbno
, /* target starting block */
189 xfs_extlen_t wantlen
, /* target length */
190 xfs_extlen_t alignment
, /* target alignment */
191 char userdata
, /* are we allocating data? */
192 xfs_agblock_t freebno
, /* freespace's starting block */
193 xfs_extlen_t freelen
, /* freespace's length */
194 xfs_agblock_t
*newbnop
) /* result: best start block from free */
196 xfs_agblock_t freeend
; /* end of freespace extent */
197 xfs_agblock_t newbno1
; /* return block number */
198 xfs_agblock_t newbno2
; /* other new block number */
199 xfs_extlen_t newlen1
=0; /* length with newbno1 */
200 xfs_extlen_t newlen2
=0; /* length with newbno2 */
201 xfs_agblock_t wantend
; /* end of target extent */
203 ASSERT(freelen
>= wantlen
);
204 freeend
= freebno
+ freelen
;
205 wantend
= wantbno
+ wantlen
;
207 * We want to allocate from the start of a free extent if it is past
208 * the desired block or if we are allocating user data and the free
209 * extent is before desired block. The second case is there to allow
210 * for contiguous allocation from the remaining free space if the file
211 * grows in the short term.
213 if (freebno
>= wantbno
|| (userdata
&& freeend
< wantend
)) {
214 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
215 newbno1
= NULLAGBLOCK
;
216 } else if (freeend
>= wantend
&& alignment
> 1) {
217 newbno1
= roundup(wantbno
, alignment
);
218 newbno2
= newbno1
- alignment
;
219 if (newbno1
>= freeend
)
220 newbno1
= NULLAGBLOCK
;
222 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
223 if (newbno2
< freebno
)
224 newbno2
= NULLAGBLOCK
;
226 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
227 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
228 if (newlen1
< newlen2
||
229 (newlen1
== newlen2
&&
230 XFS_ABSDIFF(newbno1
, wantbno
) >
231 XFS_ABSDIFF(newbno2
, wantbno
)))
233 } else if (newbno2
!= NULLAGBLOCK
)
235 } else if (freeend
>= wantend
) {
237 } else if (alignment
> 1) {
238 newbno1
= roundup(freeend
- wantlen
, alignment
);
239 if (newbno1
> freeend
- wantlen
&&
240 newbno1
- alignment
>= freebno
)
241 newbno1
-= alignment
;
242 else if (newbno1
>= freeend
)
243 newbno1
= NULLAGBLOCK
;
245 newbno1
= freeend
- wantlen
;
247 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
251 * Fix up the length, based on mod and prod.
252 * len should be k * prod + mod for some k.
253 * If len is too small it is returned unchanged.
254 * If len hits maxlen it is left alone.
258 xfs_alloc_arg_t
*args
) /* allocation argument structure */
263 ASSERT(args
->mod
< args
->prod
);
265 ASSERT(rlen
>= args
->minlen
);
266 ASSERT(rlen
<= args
->maxlen
);
267 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
268 (args
->mod
== 0 && rlen
< args
->prod
))
270 k
= rlen
% args
->prod
;
274 rlen
= rlen
- (k
- args
->mod
);
276 rlen
= rlen
- args
->prod
+ (args
->mod
- k
);
277 /* casts to (int) catch length underflows */
278 if ((int)rlen
< (int)args
->minlen
)
280 ASSERT(rlen
>= args
->minlen
&& rlen
<= args
->maxlen
);
281 ASSERT(rlen
% args
->prod
== args
->mod
);
286 * Fix up length if there is too little space left in the a.g.
287 * Return 1 if ok, 0 if too little, should give up.
290 xfs_alloc_fix_minleft(
291 xfs_alloc_arg_t
*args
) /* allocation argument structure */
293 xfs_agf_t
*agf
; /* a.g. freelist header */
294 int diff
; /* free space difference */
296 if (args
->minleft
== 0)
298 agf
= XFS_BUF_TO_AGF(args
->agbp
);
299 diff
= be32_to_cpu(agf
->agf_freeblks
)
300 - args
->len
- args
->minleft
;
303 args
->len
+= diff
; /* shrink the allocated space */
304 /* casts to (int) catch length underflows */
305 if ((int)args
->len
>= (int)args
->minlen
)
307 args
->agbno
= NULLAGBLOCK
;
312 * Update the two btrees, logically removing from freespace the extent
313 * starting at rbno, rlen blocks. The extent is contained within the
314 * actual (current) free extent fbno for flen blocks.
315 * Flags are passed in indicating whether the cursors are set to the
318 STATIC
int /* error code */
319 xfs_alloc_fixup_trees(
320 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
321 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
322 xfs_agblock_t fbno
, /* starting block of free extent */
323 xfs_extlen_t flen
, /* length of free extent */
324 xfs_agblock_t rbno
, /* starting block of returned extent */
325 xfs_extlen_t rlen
, /* length of returned extent */
326 int flags
) /* flags, XFSA_FIXUP_... */
328 int error
; /* error code */
329 int i
; /* operation results */
330 xfs_agblock_t nfbno1
; /* first new free startblock */
331 xfs_agblock_t nfbno2
; /* second new free startblock */
332 xfs_extlen_t nflen1
=0; /* first new free length */
333 xfs_extlen_t nflen2
=0; /* second new free length */
334 struct xfs_mount
*mp
;
339 * Look up the record in the by-size tree if necessary.
341 if (flags
& XFSA_FIXUP_CNT_OK
) {
343 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
345 XFS_WANT_CORRUPTED_RETURN(mp
,
346 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
349 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
351 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
354 * Look up the record in the by-block tree if necessary.
356 if (flags
& XFSA_FIXUP_BNO_OK
) {
358 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
360 XFS_WANT_CORRUPTED_RETURN(mp
,
361 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
364 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
366 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
370 if (bno_cur
->bc_nlevels
== 1 && cnt_cur
->bc_nlevels
== 1) {
371 struct xfs_btree_block
*bnoblock
;
372 struct xfs_btree_block
*cntblock
;
374 bnoblock
= XFS_BUF_TO_BLOCK(bno_cur
->bc_bufs
[0]);
375 cntblock
= XFS_BUF_TO_BLOCK(cnt_cur
->bc_bufs
[0]);
377 XFS_WANT_CORRUPTED_RETURN(mp
,
378 bnoblock
->bb_numrecs
== cntblock
->bb_numrecs
);
383 * Deal with all four cases: the allocated record is contained
384 * within the freespace record, so we can have new freespace
385 * at either (or both) end, or no freespace remaining.
387 if (rbno
== fbno
&& rlen
== flen
)
388 nfbno1
= nfbno2
= NULLAGBLOCK
;
389 else if (rbno
== fbno
) {
390 nfbno1
= rbno
+ rlen
;
391 nflen1
= flen
- rlen
;
392 nfbno2
= NULLAGBLOCK
;
393 } else if (rbno
+ rlen
== fbno
+ flen
) {
395 nflen1
= flen
- rlen
;
396 nfbno2
= NULLAGBLOCK
;
399 nflen1
= rbno
- fbno
;
400 nfbno2
= rbno
+ rlen
;
401 nflen2
= (fbno
+ flen
) - nfbno2
;
404 * Delete the entry from the by-size btree.
406 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
408 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
410 * Add new by-size btree entry(s).
412 if (nfbno1
!= NULLAGBLOCK
) {
413 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
415 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
416 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
418 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
420 if (nfbno2
!= NULLAGBLOCK
) {
421 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
423 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
424 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
426 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
429 * Fix up the by-block btree entry(s).
431 if (nfbno1
== NULLAGBLOCK
) {
433 * No remaining freespace, just delete the by-block tree entry.
435 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
437 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
440 * Update the by-block entry to start later|be shorter.
442 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
445 if (nfbno2
!= NULLAGBLOCK
) {
447 * 2 resulting free entries, need to add one.
449 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
451 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 0);
452 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
454 XFS_WANT_CORRUPTED_RETURN(mp
, i
== 1);
463 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
464 struct xfs_agfl
*agfl
= XFS_BUF_TO_AGFL(bp
);
467 if (!uuid_equal(&agfl
->agfl_uuid
, &mp
->m_sb
.sb_meta_uuid
))
469 if (be32_to_cpu(agfl
->agfl_magicnum
) != XFS_AGFL_MAGIC
)
472 * during growfs operations, the perag is not fully initialised,
473 * so we can't use it for any useful checking. growfs ensures we can't
474 * use it by using uncached buffers that don't have the perag attached
475 * so we can detect and avoid this problem.
477 if (bp
->b_pag
&& be32_to_cpu(agfl
->agfl_seqno
) != bp
->b_pag
->pag_agno
)
480 for (i
= 0; i
< XFS_AGFL_SIZE(mp
); i
++) {
481 if (be32_to_cpu(agfl
->agfl_bno
[i
]) != NULLAGBLOCK
&&
482 be32_to_cpu(agfl
->agfl_bno
[i
]) >= mp
->m_sb
.sb_agblocks
)
486 return xfs_log_check_lsn(mp
,
487 be64_to_cpu(XFS_BUF_TO_AGFL(bp
)->agfl_lsn
));
491 xfs_agfl_read_verify(
494 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
497 * There is no verification of non-crc AGFLs because mkfs does not
498 * initialise the AGFL to zero or NULL. Hence the only valid part of the
499 * AGFL is what the AGF says is active. We can't get to the AGF, so we
500 * can't verify just those entries are valid.
502 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
505 if (!xfs_buf_verify_cksum(bp
, XFS_AGFL_CRC_OFF
))
506 xfs_buf_ioerror(bp
, -EFSBADCRC
);
507 else if (!xfs_agfl_verify(bp
))
508 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
511 xfs_verifier_error(bp
);
515 xfs_agfl_write_verify(
518 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
519 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
521 /* no verification of non-crc AGFLs */
522 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
525 if (!xfs_agfl_verify(bp
)) {
526 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
527 xfs_verifier_error(bp
);
532 XFS_BUF_TO_AGFL(bp
)->agfl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
534 xfs_buf_update_cksum(bp
, XFS_AGFL_CRC_OFF
);
537 const struct xfs_buf_ops xfs_agfl_buf_ops
= {
539 .verify_read
= xfs_agfl_read_verify
,
540 .verify_write
= xfs_agfl_write_verify
,
544 * Read in the allocation group free block array.
546 STATIC
int /* error */
548 xfs_mount_t
*mp
, /* mount point structure */
549 xfs_trans_t
*tp
, /* transaction pointer */
550 xfs_agnumber_t agno
, /* allocation group number */
551 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
553 xfs_buf_t
*bp
; /* return value */
556 ASSERT(agno
!= NULLAGNUMBER
);
557 error
= xfs_trans_read_buf(
558 mp
, tp
, mp
->m_ddev_targp
,
559 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
560 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_agfl_buf_ops
);
563 xfs_buf_set_ref(bp
, XFS_AGFL_REF
);
569 xfs_alloc_update_counters(
570 struct xfs_trans
*tp
,
571 struct xfs_perag
*pag
,
572 struct xfs_buf
*agbp
,
575 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
577 pag
->pagf_freeblks
+= len
;
578 be32_add_cpu(&agf
->agf_freeblks
, len
);
580 xfs_trans_agblocks_delta(tp
, len
);
581 if (unlikely(be32_to_cpu(agf
->agf_freeblks
) >
582 be32_to_cpu(agf
->agf_length
)))
583 return -EFSCORRUPTED
;
585 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
590 * Allocation group level functions.
594 * Allocate a variable extent in the allocation group agno.
595 * Type and bno are used to determine where in the allocation group the
597 * Extent's length (returned in *len) will be between minlen and maxlen,
598 * and of the form k * prod + mod unless there's nothing that large.
599 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
601 STATIC
int /* error */
602 xfs_alloc_ag_vextent(
603 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
607 ASSERT(args
->minlen
> 0);
608 ASSERT(args
->maxlen
> 0);
609 ASSERT(args
->minlen
<= args
->maxlen
);
610 ASSERT(args
->mod
< args
->prod
);
611 ASSERT(args
->alignment
> 0);
613 * Branch to correct routine based on the type.
616 switch (args
->type
) {
617 case XFS_ALLOCTYPE_THIS_AG
:
618 error
= xfs_alloc_ag_vextent_size(args
);
620 case XFS_ALLOCTYPE_NEAR_BNO
:
621 error
= xfs_alloc_ag_vextent_near(args
);
623 case XFS_ALLOCTYPE_THIS_BNO
:
624 error
= xfs_alloc_ag_vextent_exact(args
);
631 if (error
|| args
->agbno
== NULLAGBLOCK
)
634 ASSERT(args
->len
>= args
->minlen
);
635 ASSERT(args
->len
<= args
->maxlen
);
636 ASSERT(!args
->wasfromfl
|| !args
->isfl
);
637 ASSERT(args
->agbno
% args
->alignment
== 0);
639 if (!args
->wasfromfl
) {
640 error
= xfs_alloc_update_counters(args
->tp
, args
->pag
,
642 -((long)(args
->len
)));
646 ASSERT(!xfs_extent_busy_search(args
->mp
, args
->agno
,
647 args
->agbno
, args
->len
));
651 xfs_trans_mod_sb(args
->tp
, args
->wasdel
?
652 XFS_TRANS_SB_RES_FDBLOCKS
:
653 XFS_TRANS_SB_FDBLOCKS
,
654 -((long)(args
->len
)));
657 XFS_STATS_INC(args
->mp
, xs_allocx
);
658 XFS_STATS_ADD(args
->mp
, xs_allocb
, args
->len
);
663 * Allocate a variable extent at exactly agno/bno.
664 * Extent's length (returned in *len) will be between minlen and maxlen,
665 * and of the form k * prod + mod unless there's nothing that large.
666 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
668 STATIC
int /* error */
669 xfs_alloc_ag_vextent_exact(
670 xfs_alloc_arg_t
*args
) /* allocation argument structure */
672 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
673 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
675 xfs_agblock_t fbno
; /* start block of found extent */
676 xfs_extlen_t flen
; /* length of found extent */
677 xfs_agblock_t tbno
; /* start block of trimmed extent */
678 xfs_extlen_t tlen
; /* length of trimmed extent */
679 xfs_agblock_t tend
; /* end block of trimmed extent */
680 int i
; /* success/failure of operation */
682 ASSERT(args
->alignment
== 1);
685 * Allocate/initialize a cursor for the by-number freespace btree.
687 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
688 args
->agno
, XFS_BTNUM_BNO
);
691 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
692 * Look for the closest free block <= bno, it must contain bno
693 * if any free block does.
695 error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
);
702 * Grab the freespace record.
704 error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
);
707 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
708 ASSERT(fbno
<= args
->agbno
);
711 * Check for overlapping busy extents.
713 xfs_extent_busy_trim(args
, fbno
, flen
, &tbno
, &tlen
);
716 * Give up if the start of the extent is busy, or the freespace isn't
717 * long enough for the minimum request.
719 if (tbno
> args
->agbno
)
721 if (tlen
< args
->minlen
)
724 if (tend
< args
->agbno
+ args
->minlen
)
728 * End of extent will be smaller of the freespace end and the
729 * maximal requested end.
731 * Fix the length according to mod and prod if given.
733 args
->len
= XFS_AGBLOCK_MIN(tend
, args
->agbno
+ args
->maxlen
)
735 xfs_alloc_fix_len(args
);
736 if (!xfs_alloc_fix_minleft(args
))
739 ASSERT(args
->agbno
+ args
->len
<= tend
);
742 * We are allocating agbno for args->len
743 * Allocate/initialize a cursor for the by-size btree.
745 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
746 args
->agno
, XFS_BTNUM_CNT
);
747 ASSERT(args
->agbno
+ args
->len
<=
748 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
749 error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
, args
->agbno
,
750 args
->len
, XFSA_FIXUP_BNO_OK
);
752 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
756 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
757 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
760 trace_xfs_alloc_exact_done(args
);
764 /* Didn't find it, return null. */
765 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
766 args
->agbno
= NULLAGBLOCK
;
767 trace_xfs_alloc_exact_notfound(args
);
771 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
772 trace_xfs_alloc_exact_error(args
);
777 * Search the btree in a given direction via the search cursor and compare
778 * the records found against the good extent we've already found.
781 xfs_alloc_find_best_extent(
782 struct xfs_alloc_arg
*args
, /* allocation argument structure */
783 struct xfs_btree_cur
**gcur
, /* good cursor */
784 struct xfs_btree_cur
**scur
, /* searching cursor */
785 xfs_agblock_t gdiff
, /* difference for search comparison */
786 xfs_agblock_t
*sbno
, /* extent found by search */
787 xfs_extlen_t
*slen
, /* extent length */
788 xfs_agblock_t
*sbnoa
, /* aligned extent found by search */
789 xfs_extlen_t
*slena
, /* aligned extent length */
790 int dir
) /* 0 = search right, 1 = search left */
797 /* The good extent is perfect, no need to search. */
802 * Look until we find a better one, run out of space or run off the end.
805 error
= xfs_alloc_get_rec(*scur
, sbno
, slen
, &i
);
808 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
809 xfs_alloc_compute_aligned(args
, *sbno
, *slen
, sbnoa
, slena
);
812 * The good extent is closer than this one.
815 if (*sbnoa
> args
->max_agbno
)
817 if (*sbnoa
>= args
->agbno
+ gdiff
)
820 if (*sbnoa
< args
->min_agbno
)
822 if (*sbnoa
<= args
->agbno
- gdiff
)
827 * Same distance, compare length and pick the best.
829 if (*slena
>= args
->minlen
) {
830 args
->len
= XFS_EXTLEN_MIN(*slena
, args
->maxlen
);
831 xfs_alloc_fix_len(args
);
833 sdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
835 args
->userdata
, *sbnoa
,
839 * Choose closer size and invalidate other cursor.
847 error
= xfs_btree_increment(*scur
, 0, &i
);
849 error
= xfs_btree_decrement(*scur
, 0, &i
);
855 xfs_btree_del_cursor(*scur
, XFS_BTREE_NOERROR
);
860 xfs_btree_del_cursor(*gcur
, XFS_BTREE_NOERROR
);
865 /* caller invalidates cursors */
870 * Allocate a variable extent near bno in the allocation group agno.
871 * Extent's length (returned in len) will be between minlen and maxlen,
872 * and of the form k * prod + mod unless there's nothing that large.
873 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
875 STATIC
int /* error */
876 xfs_alloc_ag_vextent_near(
877 xfs_alloc_arg_t
*args
) /* allocation argument structure */
879 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
880 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
881 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
882 xfs_agblock_t gtbno
; /* start bno of right side entry */
883 xfs_agblock_t gtbnoa
; /* aligned ... */
884 xfs_extlen_t gtdiff
; /* difference to right side entry */
885 xfs_extlen_t gtlen
; /* length of right side entry */
886 xfs_extlen_t gtlena
; /* aligned ... */
887 xfs_agblock_t gtnew
; /* useful start bno of right side */
888 int error
; /* error code */
889 int i
; /* result code, temporary */
890 int j
; /* result code, temporary */
891 xfs_agblock_t ltbno
; /* start bno of left side entry */
892 xfs_agblock_t ltbnoa
; /* aligned ... */
893 xfs_extlen_t ltdiff
; /* difference to left side entry */
894 xfs_extlen_t ltlen
; /* length of left side entry */
895 xfs_extlen_t ltlena
; /* aligned ... */
896 xfs_agblock_t ltnew
; /* useful start bno of left side */
897 xfs_extlen_t rlen
; /* length of returned extent */
901 * Randomly don't execute the first algorithm.
903 int dofirst
; /* set to do first algorithm */
905 dofirst
= prandom_u32() & 1;
908 /* handle unitialized agbno range so caller doesn't have to */
909 if (!args
->min_agbno
&& !args
->max_agbno
)
910 args
->max_agbno
= args
->mp
->m_sb
.sb_agblocks
- 1;
911 ASSERT(args
->min_agbno
<= args
->max_agbno
);
913 /* clamp agbno to the range if it's outside */
914 if (args
->agbno
< args
->min_agbno
)
915 args
->agbno
= args
->min_agbno
;
916 if (args
->agbno
> args
->max_agbno
)
917 args
->agbno
= args
->max_agbno
;
927 * Get a cursor for the by-size btree.
929 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
930 args
->agno
, XFS_BTNUM_CNT
);
933 * See if there are any free extents as big as maxlen.
935 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
938 * If none, then pick up the last entry in the tree unless the
942 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
945 if (i
== 0 || ltlen
== 0) {
946 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
947 trace_xfs_alloc_near_noentry(args
);
956 * If the requested extent is large wrt the freespaces available
957 * in this a.g., then the cursor will be pointing to a btree entry
958 * near the right edge of the tree. If it's in the last btree leaf
959 * block, then we just examine all the entries in that block
960 * that are big enough, and pick the best one.
961 * This is written as a while loop so we can break out of it,
962 * but we never loop back to the top.
964 while (xfs_btree_islastblock(cnt_cur
, 0)) {
968 xfs_agblock_t bnew
=0;
975 * Start from the entry that lookup found, sequence through
976 * all larger free blocks. If we're actually pointing at a
977 * record smaller than maxlen, go to the start of this block,
978 * and skip all those smaller than minlen.
980 if (ltlen
|| args
->alignment
> 1) {
981 cnt_cur
->bc_ptrs
[0] = 1;
983 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
986 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
987 if (ltlen
>= args
->minlen
)
989 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
992 ASSERT(ltlen
>= args
->minlen
);
996 i
= cnt_cur
->bc_ptrs
[0];
997 for (j
= 1, blen
= 0, bdiff
= 0;
998 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
999 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
1001 * For each entry, decide if it's better than
1002 * the previous best entry.
1004 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1006 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1007 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1009 if (ltlena
< args
->minlen
)
1011 if (ltbnoa
< args
->min_agbno
|| ltbnoa
> args
->max_agbno
)
1013 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1014 xfs_alloc_fix_len(args
);
1015 ASSERT(args
->len
>= args
->minlen
);
1016 if (args
->len
< blen
)
1018 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1019 args
->alignment
, args
->userdata
, ltbnoa
,
1021 if (ltnew
!= NULLAGBLOCK
&&
1022 (args
->len
> blen
|| ltdiff
< bdiff
)) {
1026 besti
= cnt_cur
->bc_ptrs
[0];
1030 * It didn't work. We COULD be in a case where
1031 * there's a good record somewhere, so try again.
1036 * Point at the best entry, and retrieve it again.
1038 cnt_cur
->bc_ptrs
[0] = besti
;
1039 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
1041 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1042 ASSERT(ltbno
+ ltlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1044 if (!xfs_alloc_fix_minleft(args
)) {
1045 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1046 trace_xfs_alloc_near_nominleft(args
);
1051 * We are allocating starting at bnew for blen blocks.
1054 ASSERT(bnew
>= ltbno
);
1055 ASSERT(bnew
+ blen
<= ltbno
+ ltlen
);
1057 * Set up a cursor for the by-bno tree.
1059 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
1060 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
1062 * Fix up the btree entries.
1064 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
1065 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
1067 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1068 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1070 trace_xfs_alloc_near_first(args
);
1075 * Search in the by-bno tree to the left and to the right
1076 * simultaneously, until in each case we find a space big enough,
1077 * or run into the edge of the tree. When we run into the edge,
1078 * we deallocate that cursor.
1079 * If both searches succeed, we compare the two spaces and pick
1081 * With alignment, it's possible for both to fail; the upper
1082 * level algorithm that picks allocation groups for allocations
1083 * is not supposed to do this.
1086 * Allocate and initialize the cursor for the leftward search.
1088 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1089 args
->agno
, XFS_BTNUM_BNO
);
1091 * Lookup <= bno to find the leftward search's starting point.
1093 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
1097 * Didn't find anything; use this cursor for the rightward
1100 bno_cur_gt
= bno_cur_lt
;
1104 * Found something. Duplicate the cursor for the rightward search.
1106 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
1109 * Increment the cursor, so we will point at the entry just right
1110 * of the leftward entry if any, or to the leftmost entry.
1112 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1116 * It failed, there are no rightward entries.
1118 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
1122 * Loop going left with the leftward cursor, right with the
1123 * rightward cursor, until either both directions give up or
1124 * we find an entry at least as big as minlen.
1128 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1130 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1131 xfs_alloc_compute_aligned(args
, ltbno
, ltlen
,
1133 if (ltlena
>= args
->minlen
&& ltbnoa
>= args
->min_agbno
)
1135 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1137 if (!i
|| ltbnoa
< args
->min_agbno
) {
1138 xfs_btree_del_cursor(bno_cur_lt
,
1144 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1146 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1147 xfs_alloc_compute_aligned(args
, gtbno
, gtlen
,
1149 if (gtlena
>= args
->minlen
&& gtbnoa
<= args
->max_agbno
)
1151 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1153 if (!i
|| gtbnoa
> args
->max_agbno
) {
1154 xfs_btree_del_cursor(bno_cur_gt
,
1159 } while (bno_cur_lt
|| bno_cur_gt
);
1162 * Got both cursors still active, need to find better entry.
1164 if (bno_cur_lt
&& bno_cur_gt
) {
1165 if (ltlena
>= args
->minlen
) {
1167 * Left side is good, look for a right side entry.
1169 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1170 xfs_alloc_fix_len(args
);
1171 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1172 args
->alignment
, args
->userdata
, ltbnoa
,
1175 error
= xfs_alloc_find_best_extent(args
,
1176 &bno_cur_lt
, &bno_cur_gt
,
1177 ltdiff
, >bno
, >len
,
1179 0 /* search right */);
1181 ASSERT(gtlena
>= args
->minlen
);
1184 * Right side is good, look for a left side entry.
1186 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1187 xfs_alloc_fix_len(args
);
1188 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
1189 args
->alignment
, args
->userdata
, gtbnoa
,
1192 error
= xfs_alloc_find_best_extent(args
,
1193 &bno_cur_gt
, &bno_cur_lt
,
1194 gtdiff
, <bno
, <len
,
1196 1 /* search left */);
1204 * If we couldn't get anything, give up.
1206 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1207 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1210 trace_xfs_alloc_near_busy(args
);
1211 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1214 trace_xfs_alloc_size_neither(args
);
1215 args
->agbno
= NULLAGBLOCK
;
1220 * At this point we have selected a freespace entry, either to the
1221 * left or to the right. If it's on the right, copy all the
1222 * useful variables to the "left" set so we only have one
1223 * copy of this code.
1226 bno_cur_lt
= bno_cur_gt
;
1237 * Fix up the length and compute the useful address.
1239 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1240 xfs_alloc_fix_len(args
);
1241 if (!xfs_alloc_fix_minleft(args
)) {
1242 trace_xfs_alloc_near_nominleft(args
);
1243 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1244 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1248 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
,
1249 args
->userdata
, ltbnoa
, ltlena
, <new
);
1250 ASSERT(ltnew
>= ltbno
);
1251 ASSERT(ltnew
+ rlen
<= ltbnoa
+ ltlena
);
1252 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1253 ASSERT(ltnew
>= args
->min_agbno
&& ltnew
<= args
->max_agbno
);
1254 args
->agbno
= ltnew
;
1256 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1257 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1261 trace_xfs_alloc_near_greater(args
);
1263 trace_xfs_alloc_near_lesser(args
);
1265 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1266 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1270 trace_xfs_alloc_near_error(args
);
1271 if (cnt_cur
!= NULL
)
1272 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1273 if (bno_cur_lt
!= NULL
)
1274 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1275 if (bno_cur_gt
!= NULL
)
1276 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1281 * Allocate a variable extent anywhere in the allocation group agno.
1282 * Extent's length (returned in len) will be between minlen and maxlen,
1283 * and of the form k * prod + mod unless there's nothing that large.
1284 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1286 STATIC
int /* error */
1287 xfs_alloc_ag_vextent_size(
1288 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1290 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1291 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1292 int error
; /* error result */
1293 xfs_agblock_t fbno
; /* start of found freespace */
1294 xfs_extlen_t flen
; /* length of found freespace */
1295 int i
; /* temp status variable */
1296 xfs_agblock_t rbno
; /* returned block number */
1297 xfs_extlen_t rlen
; /* length of returned extent */
1302 * Allocate and initialize a cursor for the by-size btree.
1304 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1305 args
->agno
, XFS_BTNUM_CNT
);
1309 * Look for an entry >= maxlen+alignment-1 blocks.
1311 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1312 args
->maxlen
+ args
->alignment
- 1, &i
)))
1316 * If none or we have busy extents that we cannot allocate from, then
1317 * we have to settle for a smaller extent. In the case that there are
1318 * no large extents, this will return the last entry in the tree unless
1319 * the tree is empty. In the case that there are only busy large
1320 * extents, this will return the largest small extent unless there
1321 * are no smaller extents available.
1323 if (!i
|| forced
> 1) {
1324 error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
,
1328 if (i
== 0 || flen
== 0) {
1329 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1330 trace_xfs_alloc_size_noentry(args
);
1334 xfs_alloc_compute_aligned(args
, fbno
, flen
, &rbno
, &rlen
);
1337 * Search for a non-busy extent that is large enough.
1338 * If we are at low space, don't check, or if we fall of
1339 * the end of the btree, turn off the busy check and
1343 error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
);
1346 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1348 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1351 if (rlen
>= args
->maxlen
)
1354 error
= xfs_btree_increment(cnt_cur
, 0, &i
);
1359 * Our only valid extents must have been busy.
1360 * Make it unbusy by forcing the log out and
1361 * retrying. If we've been here before, forcing
1362 * the log isn't making the extents available,
1363 * which means they have probably been freed in
1364 * this transaction. In that case, we have to
1365 * give up on them and we'll attempt a minlen
1366 * allocation the next time around.
1368 xfs_btree_del_cursor(cnt_cur
,
1370 trace_xfs_alloc_size_busy(args
);
1372 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1379 * In the first case above, we got the last entry in the
1380 * by-size btree. Now we check to see if the space hits maxlen
1381 * once aligned; if not, we search left for something better.
1382 * This can't happen in the second case above.
1384 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1385 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1386 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1387 if (rlen
< args
->maxlen
) {
1388 xfs_agblock_t bestfbno
;
1389 xfs_extlen_t bestflen
;
1390 xfs_agblock_t bestrbno
;
1391 xfs_extlen_t bestrlen
;
1398 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1402 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1405 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1406 if (flen
< bestrlen
)
1408 xfs_alloc_compute_aligned(args
, fbno
, flen
,
1410 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1411 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
== 0 ||
1412 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1414 if (rlen
> bestrlen
) {
1419 if (rlen
== args
->maxlen
)
1423 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1426 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1432 args
->wasfromfl
= 0;
1434 * Fix up the length.
1437 if (rlen
< args
->minlen
) {
1439 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1440 trace_xfs_alloc_size_busy(args
);
1441 xfs_log_force(args
->mp
, XFS_LOG_SYNC
);
1446 xfs_alloc_fix_len(args
);
1448 if (!xfs_alloc_fix_minleft(args
))
1451 XFS_WANT_CORRUPTED_GOTO(args
->mp
, rlen
<= flen
, error0
);
1453 * Allocate and initialize a cursor for the by-block tree.
1455 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1456 args
->agno
, XFS_BTNUM_BNO
);
1457 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1458 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1460 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1461 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1462 cnt_cur
= bno_cur
= NULL
;
1465 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1466 args
->agbno
+ args
->len
<=
1467 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1469 trace_xfs_alloc_size_done(args
);
1473 trace_xfs_alloc_size_error(args
);
1475 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1477 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1481 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1482 trace_xfs_alloc_size_nominleft(args
);
1483 args
->agbno
= NULLAGBLOCK
;
1488 * Deal with the case where only small freespaces remain.
1489 * Either return the contents of the last freespace record,
1490 * or allocate space from the freelist if there is nothing in the tree.
1492 STATIC
int /* error */
1493 xfs_alloc_ag_vextent_small(
1494 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1495 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1496 xfs_agblock_t
*fbnop
, /* result block number */
1497 xfs_extlen_t
*flenp
, /* result length */
1498 int *stat
) /* status: 0-freelist, 1-normal/none */
1505 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1508 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1510 XFS_WANT_CORRUPTED_GOTO(args
->mp
, i
== 1, error0
);
1513 * Nothing in the btree, try the freelist. Make sure
1514 * to respect minleft even when pulling from the
1517 else if (args
->minlen
== 1 && args
->alignment
== 1 && !args
->isfl
&&
1518 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1520 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1523 if (fbno
!= NULLAGBLOCK
) {
1524 xfs_extent_busy_reuse(args
->mp
, args
->agno
, fbno
, 1,
1527 if (args
->userdata
) {
1530 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1531 args
->agno
, fbno
, 0);
1532 xfs_trans_binval(args
->tp
, bp
);
1536 XFS_WANT_CORRUPTED_GOTO(args
->mp
,
1537 args
->agbno
+ args
->len
<=
1538 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1540 args
->wasfromfl
= 1;
1541 trace_xfs_alloc_small_freelist(args
);
1546 * Nothing in the freelist.
1552 * Can't allocate from the freelist for some reason.
1559 * Can't do the allocation, give up.
1561 if (flen
< args
->minlen
) {
1562 args
->agbno
= NULLAGBLOCK
;
1563 trace_xfs_alloc_small_notenough(args
);
1569 trace_xfs_alloc_small_done(args
);
1573 trace_xfs_alloc_small_error(args
);
1578 * Free the extent starting at agno/bno for length.
1580 STATIC
int /* error */
1582 xfs_trans_t
*tp
, /* transaction pointer */
1583 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
1584 xfs_agnumber_t agno
, /* allocation group number */
1585 xfs_agblock_t bno
, /* starting block number */
1586 xfs_extlen_t len
, /* length of extent */
1587 int isfl
) /* set if is freelist blocks - no sb acctg */
1589 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1590 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1591 int error
; /* error return value */
1592 xfs_agblock_t gtbno
; /* start of right neighbor block */
1593 xfs_extlen_t gtlen
; /* length of right neighbor block */
1594 int haveleft
; /* have a left neighbor block */
1595 int haveright
; /* have a right neighbor block */
1596 int i
; /* temp, result code */
1597 xfs_agblock_t ltbno
; /* start of left neighbor block */
1598 xfs_extlen_t ltlen
; /* length of left neighbor block */
1599 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1600 xfs_agblock_t nbno
; /* new starting block of freespace */
1601 xfs_extlen_t nlen
; /* new length of freespace */
1602 xfs_perag_t
*pag
; /* per allocation group data */
1606 * Allocate and initialize a cursor for the by-block btree.
1608 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1611 * Look for a neighboring block on the left (lower block numbers)
1612 * that is contiguous with this space.
1614 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1618 * There is a block to our left.
1620 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1622 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1624 * It's not contiguous, though.
1626 if (ltbno
+ ltlen
< bno
)
1630 * If this failure happens the request to free this
1631 * space was invalid, it's (partly) already free.
1634 XFS_WANT_CORRUPTED_GOTO(mp
,
1635 ltbno
+ ltlen
<= bno
, error0
);
1639 * Look for a neighboring block on the right (higher block numbers)
1640 * that is contiguous with this space.
1642 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1646 * There is a block to our right.
1648 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1650 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1652 * It's not contiguous, though.
1654 if (bno
+ len
< gtbno
)
1658 * If this failure happens the request to free this
1659 * space was invalid, it's (partly) already free.
1662 XFS_WANT_CORRUPTED_GOTO(mp
, gtbno
>= bno
+ len
, error0
);
1666 * Now allocate and initialize a cursor for the by-size tree.
1668 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1670 * Have both left and right contiguous neighbors.
1671 * Merge all three into a single free block.
1673 if (haveleft
&& haveright
) {
1675 * Delete the old by-size entry on the left.
1677 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1679 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1680 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1682 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1684 * Delete the old by-size entry on the right.
1686 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1688 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1689 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1691 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1693 * Delete the old by-block entry for the right block.
1695 if ((error
= xfs_btree_delete(bno_cur
, &i
)))
1697 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1699 * Move the by-block cursor back to the left neighbor.
1701 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1703 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1706 * Check that this is the right record: delete didn't
1707 * mangle the cursor.
1710 xfs_agblock_t xxbno
;
1713 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1716 XFS_WANT_CORRUPTED_GOTO(mp
,
1717 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1722 * Update remaining by-block entry to the new, joined block.
1725 nlen
= len
+ ltlen
+ gtlen
;
1726 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1730 * Have only a left contiguous neighbor.
1731 * Merge it together with the new freespace.
1733 else if (haveleft
) {
1735 * Delete the old by-size entry on the left.
1737 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1739 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1740 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1742 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1744 * Back up the by-block cursor to the left neighbor, and
1745 * update its length.
1747 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1749 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1752 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1756 * Have only a right contiguous neighbor.
1757 * Merge it together with the new freespace.
1759 else if (haveright
) {
1761 * Delete the old by-size entry on the right.
1763 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1765 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1766 if ((error
= xfs_btree_delete(cnt_cur
, &i
)))
1768 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1770 * Update the starting block and length of the right
1771 * neighbor in the by-block tree.
1775 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1779 * No contiguous neighbors.
1780 * Insert the new freespace into the by-block tree.
1785 if ((error
= xfs_btree_insert(bno_cur
, &i
)))
1787 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1789 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1792 * In all cases we need to insert the new freespace in the by-size tree.
1794 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1796 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 0, error0
);
1797 if ((error
= xfs_btree_insert(cnt_cur
, &i
)))
1799 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1800 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1804 * Update the freespace totals in the ag and superblock.
1806 pag
= xfs_perag_get(mp
, agno
);
1807 error
= xfs_alloc_update_counters(tp
, pag
, agbp
, len
);
1813 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, (long)len
);
1814 XFS_STATS_INC(mp
, xs_freex
);
1815 XFS_STATS_ADD(mp
, xs_freeb
, len
);
1817 trace_xfs_free_extent(mp
, agno
, bno
, len
, isfl
, haveleft
, haveright
);
1822 trace_xfs_free_extent(mp
, agno
, bno
, len
, isfl
, -1, -1);
1824 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1826 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1831 * Visible (exported) allocation/free functions.
1832 * Some of these are used just by xfs_alloc_btree.c and this file.
1836 * Compute and fill in value of m_ag_maxlevels.
1839 xfs_alloc_compute_maxlevels(
1840 xfs_mount_t
*mp
) /* file system mount structure */
1848 maxleafents
= (mp
->m_sb
.sb_agblocks
+ 1) / 2;
1849 minleafrecs
= mp
->m_alloc_mnr
[0];
1850 minnoderecs
= mp
->m_alloc_mnr
[1];
1851 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1852 for (level
= 1; maxblocks
> 1; level
++)
1853 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1854 mp
->m_ag_maxlevels
= level
;
1858 * Find the length of the longest extent in an AG.
1861 xfs_alloc_longest_free_extent(
1862 struct xfs_mount
*mp
,
1863 struct xfs_perag
*pag
,
1866 xfs_extlen_t delta
= 0;
1868 if (need
> pag
->pagf_flcount
)
1869 delta
= need
- pag
->pagf_flcount
;
1871 if (pag
->pagf_longest
> delta
)
1872 return pag
->pagf_longest
- delta
;
1873 return pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0;
1877 xfs_alloc_min_freelist(
1878 struct xfs_mount
*mp
,
1879 struct xfs_perag
*pag
)
1881 unsigned int min_free
;
1883 /* space needed by-bno freespace btree */
1884 min_free
= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_BNOi
] + 1,
1885 mp
->m_ag_maxlevels
);
1886 /* space needed by-size freespace btree */
1887 min_free
+= min_t(unsigned int, pag
->pagf_levels
[XFS_BTNUM_CNTi
] + 1,
1888 mp
->m_ag_maxlevels
);
1894 * Check if the operation we are fixing up the freelist for should go ahead or
1895 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1896 * is dependent on whether the size and shape of free space available will
1897 * permit the requested allocation to take place.
1900 xfs_alloc_space_available(
1901 struct xfs_alloc_arg
*args
,
1902 xfs_extlen_t min_free
,
1905 struct xfs_perag
*pag
= args
->pag
;
1906 xfs_extlen_t longest
;
1909 if (flags
& XFS_ALLOC_FLAG_FREEING
)
1912 /* do we have enough contiguous free space for the allocation? */
1913 longest
= xfs_alloc_longest_free_extent(args
->mp
, pag
, min_free
);
1914 if ((args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1) > longest
)
1917 /* do have enough free space remaining for the allocation? */
1918 available
= (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
1919 min_free
- args
->total
);
1920 if (available
< (int)args
->minleft
)
1927 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
1928 * is to detect an agfl header padding mismatch between current and early v5
1929 * kernels. This problem manifests as a 1-slot size difference between the
1930 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
1931 * may also catch variants of agfl count corruption unrelated to padding. Either
1932 * way, we'll reset the agfl and warn the user.
1934 * Return true if a reset is required before the agfl can be used, false
1938 xfs_agfl_needs_reset(
1939 struct xfs_mount
*mp
,
1940 struct xfs_agf
*agf
)
1942 uint32_t f
= be32_to_cpu(agf
->agf_flfirst
);
1943 uint32_t l
= be32_to_cpu(agf
->agf_fllast
);
1944 uint32_t c
= be32_to_cpu(agf
->agf_flcount
);
1945 int agfl_size
= XFS_AGFL_SIZE(mp
);
1948 /* no agfl header on v4 supers */
1949 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
1953 * The agf read verifier catches severe corruption of these fields.
1954 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
1955 * the verifier allows it.
1957 if (f
>= agfl_size
|| l
>= agfl_size
)
1963 * Check consistency between the on-disk count and the active range. An
1964 * agfl padding mismatch manifests as an inconsistent flcount.
1969 active
= agfl_size
- f
+ l
+ 1;
1977 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
1978 * agfl content cannot be trusted. Warn the user that a repair is required to
1979 * recover leaked blocks.
1981 * The purpose of this mechanism is to handle filesystems affected by the agfl
1982 * header padding mismatch problem. A reset keeps the filesystem online with a
1983 * relatively minor free space accounting inconsistency rather than suffer the
1984 * inevitable crash from use of an invalid agfl block.
1988 struct xfs_trans
*tp
,
1989 struct xfs_buf
*agbp
,
1990 struct xfs_perag
*pag
)
1992 struct xfs_mount
*mp
= tp
->t_mountp
;
1993 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(agbp
);
1995 ASSERT(pag
->pagf_agflreset
);
1996 trace_xfs_agfl_reset(mp
, agf
, 0, _RET_IP_
);
1999 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2000 "Please unmount and run xfs_repair.",
2001 pag
->pag_agno
, pag
->pagf_flcount
);
2003 agf
->agf_flfirst
= 0;
2004 agf
->agf_fllast
= cpu_to_be32(XFS_AGFL_SIZE(mp
) - 1);
2005 agf
->agf_flcount
= 0;
2006 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLFIRST
| XFS_AGF_FLLAST
|
2009 pag
->pagf_flcount
= 0;
2010 pag
->pagf_agflreset
= false;
2014 * Decide whether to use this allocation group for this allocation.
2015 * If so, fix up the btree freelist's size.
2017 STATIC
int /* error */
2018 xfs_alloc_fix_freelist(
2019 struct xfs_alloc_arg
*args
, /* allocation argument structure */
2020 int flags
) /* XFS_ALLOC_FLAG_... */
2022 struct xfs_mount
*mp
= args
->mp
;
2023 struct xfs_perag
*pag
= args
->pag
;
2024 struct xfs_trans
*tp
= args
->tp
;
2025 struct xfs_buf
*agbp
= NULL
;
2026 struct xfs_buf
*agflbp
= NULL
;
2027 struct xfs_alloc_arg targs
; /* local allocation arguments */
2028 xfs_agblock_t bno
; /* freelist block */
2029 xfs_extlen_t need
; /* total blocks needed in freelist */
2032 if (!pag
->pagf_init
) {
2033 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2036 if (!pag
->pagf_init
) {
2037 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2038 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2039 goto out_agbp_relse
;
2044 * If this is a metadata preferred pag and we are user data then try
2045 * somewhere else if we are not being asked to try harder at this
2048 if (pag
->pagf_metadata
&& args
->userdata
&&
2049 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
2050 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2051 goto out_agbp_relse
;
2054 need
= xfs_alloc_min_freelist(mp
, pag
);
2055 if (!xfs_alloc_space_available(args
, need
, flags
))
2056 goto out_agbp_relse
;
2059 * Get the a.g. freespace buffer.
2060 * Can fail if we're not blocking on locks, and it's held.
2063 error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
, &agbp
);
2067 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
2068 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
2073 /* reset a padding mismatched agfl before final free space check */
2074 if (pag
->pagf_agflreset
)
2075 xfs_agfl_reset(tp
, agbp
, pag
);
2077 /* If there isn't enough total space or single-extent, reject it. */
2078 need
= xfs_alloc_min_freelist(mp
, pag
);
2079 if (!xfs_alloc_space_available(args
, need
, flags
))
2080 goto out_agbp_relse
;
2083 * Make the freelist shorter if it's too long.
2085 * Note that from this point onwards, we will always release the agf and
2086 * agfl buffers on error. This handles the case where we error out and
2087 * the buffers are clean or may not have been joined to the transaction
2088 * and hence need to be released manually. If they have been joined to
2089 * the transaction, then xfs_trans_brelse() will handle them
2090 * appropriately based on the recursion count and dirty state of the
2093 * XXX (dgc): When we have lots of free space, does this buy us
2094 * anything other than extra overhead when we need to put more blocks
2095 * back on the free list? Maybe we should only do this when space is
2096 * getting low or the AGFL is more than half full?
2098 while (pag
->pagf_flcount
> need
) {
2101 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
2103 goto out_agbp_relse
;
2104 error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1, 1);
2106 goto out_agbp_relse
;
2107 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
2108 xfs_trans_binval(tp
, bp
);
2111 memset(&targs
, 0, sizeof(targs
));
2115 targs
.agno
= args
->agno
;
2116 targs
.alignment
= targs
.minlen
= targs
.prod
= targs
.isfl
= 1;
2117 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
2119 error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
);
2121 goto out_agbp_relse
;
2123 /* Make the freelist longer if it's too short. */
2124 while (pag
->pagf_flcount
< need
) {
2126 targs
.maxlen
= need
- pag
->pagf_flcount
;
2128 /* Allocate as many blocks as possible at once. */
2129 error
= xfs_alloc_ag_vextent(&targs
);
2131 goto out_agflbp_relse
;
2134 * Stop if we run out. Won't happen if callers are obeying
2135 * the restrictions correctly. Can happen for free calls
2136 * on a completely full ag.
2138 if (targs
.agbno
== NULLAGBLOCK
) {
2139 if (flags
& XFS_ALLOC_FLAG_FREEING
)
2141 goto out_agflbp_relse
;
2144 * Put each allocated block on the list.
2146 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2147 error
= xfs_alloc_put_freelist(tp
, agbp
,
2150 goto out_agflbp_relse
;
2153 xfs_trans_brelse(tp
, agflbp
);
2158 xfs_trans_brelse(tp
, agflbp
);
2161 xfs_trans_brelse(tp
, agbp
);
2168 * Get a block from the freelist.
2169 * Returns with the buffer for the block gotten.
2172 xfs_alloc_get_freelist(
2173 xfs_trans_t
*tp
, /* transaction pointer */
2174 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2175 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2176 int btreeblk
) /* destination is a AGF btree */
2178 xfs_agf_t
*agf
; /* a.g. freespace structure */
2179 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2180 xfs_agblock_t bno
; /* block number returned */
2184 xfs_mount_t
*mp
= tp
->t_mountp
;
2185 xfs_perag_t
*pag
; /* per allocation group data */
2188 * Freelist is empty, give up.
2190 agf
= XFS_BUF_TO_AGF(agbp
);
2191 if (!agf
->agf_flcount
) {
2192 *bnop
= NULLAGBLOCK
;
2196 * Read the array of free blocks.
2198 error
= xfs_alloc_read_agfl(mp
, tp
, be32_to_cpu(agf
->agf_seqno
),
2205 * Get the block number and update the data structures.
2207 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2208 bno
= be32_to_cpu(agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2209 be32_add_cpu(&agf
->agf_flfirst
, 1);
2210 xfs_trans_brelse(tp
, agflbp
);
2211 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2212 agf
->agf_flfirst
= 0;
2214 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2215 ASSERT(!pag
->pagf_agflreset
);
2216 be32_add_cpu(&agf
->agf_flcount
, -1);
2217 xfs_trans_agflist_delta(tp
, -1);
2218 pag
->pagf_flcount
--;
2221 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2223 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2224 pag
->pagf_btreeblks
++;
2225 logflags
|= XFS_AGF_BTREEBLKS
;
2228 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2235 * Log the given fields from the agf structure.
2239 xfs_trans_t
*tp
, /* transaction pointer */
2240 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2241 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2243 int first
; /* first byte offset */
2244 int last
; /* last byte offset */
2245 static const short offsets
[] = {
2246 offsetof(xfs_agf_t
, agf_magicnum
),
2247 offsetof(xfs_agf_t
, agf_versionnum
),
2248 offsetof(xfs_agf_t
, agf_seqno
),
2249 offsetof(xfs_agf_t
, agf_length
),
2250 offsetof(xfs_agf_t
, agf_roots
[0]),
2251 offsetof(xfs_agf_t
, agf_levels
[0]),
2252 offsetof(xfs_agf_t
, agf_flfirst
),
2253 offsetof(xfs_agf_t
, agf_fllast
),
2254 offsetof(xfs_agf_t
, agf_flcount
),
2255 offsetof(xfs_agf_t
, agf_freeblks
),
2256 offsetof(xfs_agf_t
, agf_longest
),
2257 offsetof(xfs_agf_t
, agf_btreeblks
),
2258 offsetof(xfs_agf_t
, agf_uuid
),
2262 trace_xfs_agf(tp
->t_mountp
, XFS_BUF_TO_AGF(bp
), fields
, _RET_IP_
);
2264 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGF_BUF
);
2266 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2267 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2271 * Interface for inode allocation to force the pag data to be initialized.
2274 xfs_alloc_pagf_init(
2275 xfs_mount_t
*mp
, /* file system mount structure */
2276 xfs_trans_t
*tp
, /* transaction pointer */
2277 xfs_agnumber_t agno
, /* allocation group number */
2278 int flags
) /* XFS_ALLOC_FLAGS_... */
2283 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2286 xfs_trans_brelse(tp
, bp
);
2291 * Put the block on the freelist for the allocation group.
2294 xfs_alloc_put_freelist(
2295 xfs_trans_t
*tp
, /* transaction pointer */
2296 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2297 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2298 xfs_agblock_t bno
, /* block being freed */
2299 int btreeblk
) /* block came from a AGF btree */
2301 xfs_agf_t
*agf
; /* a.g. freespace structure */
2302 __be32
*blockp
;/* pointer to array entry */
2305 xfs_mount_t
*mp
; /* mount structure */
2306 xfs_perag_t
*pag
; /* per allocation group data */
2310 agf
= XFS_BUF_TO_AGF(agbp
);
2313 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2314 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2316 be32_add_cpu(&agf
->agf_fllast
, 1);
2317 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2318 agf
->agf_fllast
= 0;
2320 pag
= xfs_perag_get(mp
, be32_to_cpu(agf
->agf_seqno
));
2321 ASSERT(!pag
->pagf_agflreset
);
2322 be32_add_cpu(&agf
->agf_flcount
, 1);
2323 xfs_trans_agflist_delta(tp
, 1);
2324 pag
->pagf_flcount
++;
2326 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2328 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2329 pag
->pagf_btreeblks
--;
2330 logflags
|= XFS_AGF_BTREEBLKS
;
2334 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2336 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2338 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, agflbp
);
2339 blockp
= &agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2340 *blockp
= cpu_to_be32(bno
);
2341 startoff
= (char *)blockp
- (char *)agflbp
->b_addr
;
2343 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2345 xfs_trans_buf_set_type(tp
, agflbp
, XFS_BLFT_AGFL_BUF
);
2346 xfs_trans_log_buf(tp
, agflbp
, startoff
,
2347 startoff
+ sizeof(xfs_agblock_t
) - 1);
2353 struct xfs_mount
*mp
,
2356 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(bp
);
2358 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2359 if (!uuid_equal(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2361 if (!xfs_log_check_lsn(mp
,
2362 be64_to_cpu(XFS_BUF_TO_AGF(bp
)->agf_lsn
)))
2366 if (!(agf
->agf_magicnum
== cpu_to_be32(XFS_AGF_MAGIC
) &&
2367 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2368 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2369 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2370 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2371 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
)))
2374 if (be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]) > XFS_BTREE_MAXLEVELS
||
2375 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]) > XFS_BTREE_MAXLEVELS
)
2379 * during growfs operations, the perag is not fully initialised,
2380 * so we can't use it for any useful checking. growfs ensures we can't
2381 * use it by using uncached buffers that don't have the perag attached
2382 * so we can detect and avoid this problem.
2384 if (bp
->b_pag
&& be32_to_cpu(agf
->agf_seqno
) != bp
->b_pag
->pag_agno
)
2387 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
2388 be32_to_cpu(agf
->agf_btreeblks
) > be32_to_cpu(agf
->agf_length
))
2396 xfs_agf_read_verify(
2399 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2401 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2402 !xfs_buf_verify_cksum(bp
, XFS_AGF_CRC_OFF
))
2403 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2404 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp
, bp
), mp
,
2405 XFS_ERRTAG_ALLOC_READ_AGF
,
2406 XFS_RANDOM_ALLOC_READ_AGF
))
2407 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2410 xfs_verifier_error(bp
);
2414 xfs_agf_write_verify(
2417 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2418 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2420 if (!xfs_agf_verify(mp
, bp
)) {
2421 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2422 xfs_verifier_error(bp
);
2426 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2430 XFS_BUF_TO_AGF(bp
)->agf_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2432 xfs_buf_update_cksum(bp
, XFS_AGF_CRC_OFF
);
2435 const struct xfs_buf_ops xfs_agf_buf_ops
= {
2437 .verify_read
= xfs_agf_read_verify
,
2438 .verify_write
= xfs_agf_write_verify
,
2442 * Read in the allocation group header (free/alloc section).
2446 struct xfs_mount
*mp
, /* mount point structure */
2447 struct xfs_trans
*tp
, /* transaction pointer */
2448 xfs_agnumber_t agno
, /* allocation group number */
2449 int flags
, /* XFS_BUF_ */
2450 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2454 trace_xfs_read_agf(mp
, agno
);
2456 ASSERT(agno
!= NULLAGNUMBER
);
2457 error
= xfs_trans_read_buf(
2458 mp
, tp
, mp
->m_ddev_targp
,
2459 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2460 XFS_FSS_TO_BB(mp
, 1), flags
, bpp
, &xfs_agf_buf_ops
);
2466 ASSERT(!(*bpp
)->b_error
);
2467 xfs_buf_set_ref(*bpp
, XFS_AGF_REF
);
2472 * Read in the allocation group header (free/alloc section).
2476 struct xfs_mount
*mp
, /* mount point structure */
2477 struct xfs_trans
*tp
, /* transaction pointer */
2478 xfs_agnumber_t agno
, /* allocation group number */
2479 int flags
, /* XFS_ALLOC_FLAG_... */
2480 struct xfs_buf
**bpp
) /* buffer for the ag freelist header */
2482 struct xfs_agf
*agf
; /* ag freelist header */
2483 struct xfs_perag
*pag
; /* per allocation group data */
2486 trace_xfs_alloc_read_agf(mp
, agno
);
2488 ASSERT(agno
!= NULLAGNUMBER
);
2489 error
= xfs_read_agf(mp
, tp
, agno
,
2490 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XBF_TRYLOCK
: 0,
2496 ASSERT(!(*bpp
)->b_error
);
2498 agf
= XFS_BUF_TO_AGF(*bpp
);
2499 pag
= xfs_perag_get(mp
, agno
);
2500 if (!pag
->pagf_init
) {
2501 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2502 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2503 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2504 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2505 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2506 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2507 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2508 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2509 spin_lock_init(&pag
->pagb_lock
);
2510 pag
->pagb_count
= 0;
2511 pag
->pagb_tree
= RB_ROOT
;
2513 pag
->pagf_agflreset
= xfs_agfl_needs_reset(mp
, agf
);
2516 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2517 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2518 ASSERT(pag
->pagf_btreeblks
== be32_to_cpu(agf
->agf_btreeblks
));
2519 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2520 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2521 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2522 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2523 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2524 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2532 * Allocate an extent (variable-size).
2533 * Depending on the allocation type, we either look in a single allocation
2534 * group or loop over the allocation groups to find the result.
2538 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2540 xfs_agblock_t agsize
; /* allocation group size */
2542 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2543 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2544 xfs_mount_t
*mp
; /* mount structure pointer */
2545 xfs_agnumber_t sagno
; /* starting allocation group number */
2546 xfs_alloctype_t type
; /* input allocation type */
2549 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2552 type
= args
->otype
= args
->type
;
2553 args
->agbno
= NULLAGBLOCK
;
2555 * Just fix this up, for the case where the last a.g. is shorter
2556 * (or there's only one a.g.) and the caller couldn't easily figure
2557 * that out (xfs_bmap_alloc).
2559 agsize
= mp
->m_sb
.sb_agblocks
;
2560 if (args
->maxlen
> agsize
)
2561 args
->maxlen
= agsize
;
2562 if (args
->alignment
== 0)
2563 args
->alignment
= 1;
2564 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2565 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2566 ASSERT(args
->minlen
<= args
->maxlen
);
2567 ASSERT(args
->minlen
<= agsize
);
2568 ASSERT(args
->mod
< args
->prod
);
2569 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2570 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2571 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2572 args
->mod
>= args
->prod
) {
2573 args
->fsbno
= NULLFSBLOCK
;
2574 trace_xfs_alloc_vextent_badargs(args
);
2577 minleft
= args
->minleft
;
2580 case XFS_ALLOCTYPE_THIS_AG
:
2581 case XFS_ALLOCTYPE_NEAR_BNO
:
2582 case XFS_ALLOCTYPE_THIS_BNO
:
2584 * These three force us into a single a.g.
2586 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2587 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2589 error
= xfs_alloc_fix_freelist(args
, 0);
2590 args
->minleft
= minleft
;
2592 trace_xfs_alloc_vextent_nofix(args
);
2596 trace_xfs_alloc_vextent_noagbp(args
);
2599 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2600 if ((error
= xfs_alloc_ag_vextent(args
)))
2603 case XFS_ALLOCTYPE_START_BNO
:
2605 * Try near allocation first, then anywhere-in-ag after
2606 * the first a.g. fails.
2608 if ((args
->userdata
& XFS_ALLOC_INITIAL_USER_DATA
) &&
2609 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2610 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2611 ((mp
->m_agfrotor
/ rotorstep
) %
2612 mp
->m_sb
.sb_agcount
), 0);
2615 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2616 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2618 case XFS_ALLOCTYPE_ANY_AG
:
2619 case XFS_ALLOCTYPE_START_AG
:
2620 case XFS_ALLOCTYPE_FIRST_AG
:
2622 * Rotate through the allocation groups looking for a winner.
2624 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2626 * Start with the last place we left off.
2628 args
->agno
= sagno
= (mp
->m_agfrotor
/ rotorstep
) %
2629 mp
->m_sb
.sb_agcount
;
2630 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2631 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2632 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2634 * Start with allocation group given by bno.
2636 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2637 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2641 if (type
== XFS_ALLOCTYPE_START_AG
)
2642 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2644 * Start with the given allocation group.
2646 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2647 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2650 * Loop over allocation groups twice; first time with
2651 * trylock set, second time without.
2654 args
->pag
= xfs_perag_get(mp
, args
->agno
);
2655 if (no_min
) args
->minleft
= 0;
2656 error
= xfs_alloc_fix_freelist(args
, flags
);
2657 args
->minleft
= minleft
;
2659 trace_xfs_alloc_vextent_nofix(args
);
2663 * If we get a buffer back then the allocation will fly.
2666 if ((error
= xfs_alloc_ag_vextent(args
)))
2671 trace_xfs_alloc_vextent_loopfailed(args
);
2674 * Didn't work, figure out the next iteration.
2676 if (args
->agno
== sagno
&&
2677 type
== XFS_ALLOCTYPE_START_BNO
)
2678 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2680 * For the first allocation, we can try any AG to get
2681 * space. However, if we already have allocated a
2682 * block, we don't want to try AGs whose number is below
2683 * sagno. Otherwise, we may end up with out-of-order
2684 * locking of AGF, which might cause deadlock.
2686 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2687 if (args
->firstblock
!= NULLFSBLOCK
)
2693 * Reached the starting a.g., must either be done
2694 * or switch to non-trylock mode.
2696 if (args
->agno
== sagno
) {
2698 args
->agbno
= NULLAGBLOCK
;
2699 trace_xfs_alloc_vextent_allfailed(args
);
2706 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2707 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2709 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2713 xfs_perag_put(args
->pag
);
2715 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
)) {
2716 if (args
->agno
== sagno
)
2717 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2718 (mp
->m_sb
.sb_agcount
* rotorstep
);
2720 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2721 (mp
->m_sb
.sb_agcount
* rotorstep
);
2728 if (args
->agbno
== NULLAGBLOCK
)
2729 args
->fsbno
= NULLFSBLOCK
;
2731 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2733 ASSERT(args
->len
>= args
->minlen
);
2734 ASSERT(args
->len
<= args
->maxlen
);
2735 ASSERT(args
->agbno
% args
->alignment
== 0);
2736 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2740 /* Zero the extent if we were asked to do so */
2741 if (args
->userdata
& XFS_ALLOC_USERDATA_ZERO
) {
2742 error
= xfs_zero_extent(args
->ip
, args
->fsbno
, args
->len
);
2748 xfs_perag_put(args
->pag
);
2751 xfs_perag_put(args
->pag
);
2757 * Just break up the extent address and hand off to xfs_free_ag_extent
2758 * after fixing up the freelist.
2762 xfs_trans_t
*tp
, /* transaction pointer */
2763 xfs_fsblock_t bno
, /* starting block number of extent */
2764 xfs_extlen_t len
) /* length of extent */
2766 xfs_alloc_arg_t args
;
2770 memset(&args
, 0, sizeof(xfs_alloc_arg_t
));
2772 args
.mp
= tp
->t_mountp
;
2775 * validate that the block number is legal - the enables us to detect
2776 * and handle a silent filesystem corruption rather than crashing.
2778 args
.agno
= XFS_FSB_TO_AGNO(args
.mp
, bno
);
2779 if (args
.agno
>= args
.mp
->m_sb
.sb_agcount
)
2780 return -EFSCORRUPTED
;
2782 args
.agbno
= XFS_FSB_TO_AGBNO(args
.mp
, bno
);
2783 if (args
.agbno
>= args
.mp
->m_sb
.sb_agblocks
)
2784 return -EFSCORRUPTED
;
2786 args
.pag
= xfs_perag_get(args
.mp
, args
.agno
);
2789 error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
);
2793 /* validate the extent size is legal now we have the agf locked */
2794 if (args
.agbno
+ len
>
2795 be32_to_cpu(XFS_BUF_TO_AGF(args
.agbp
)->agf_length
)) {
2796 error
= -EFSCORRUPTED
;
2800 error
= xfs_free_ag_extent(tp
, args
.agbp
, args
.agno
, args
.agbno
, len
, 0);
2802 xfs_extent_busy_insert(tp
, args
.agno
, args
.agbno
, len
, 0);
2804 xfs_perag_put(args
.pag
);