2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
36 #define XFSA_FIXUP_BNO_OK 1
37 #define XFSA_FIXUP_CNT_OK 2
40 * Compute aligned version of the found extent.
41 * Takes alignment and min length into account.
43 STATIC
int /* success (>= minlen) */
44 xfs_alloc_compute_aligned(
45 xfs_agblock_t foundbno
, /* starting block in found extent */
46 xfs_extlen_t foundlen
, /* length in found extent */
47 xfs_extlen_t alignment
, /* alignment for allocation */
48 xfs_extlen_t minlen
, /* minimum length for allocation */
49 xfs_agblock_t
*resbno
, /* result block number */
50 xfs_extlen_t
*reslen
) /* result length */
56 if (alignment
> 1 && foundlen
>= minlen
) {
57 bno
= roundup(foundbno
, alignment
);
58 diff
= bno
- foundbno
;
59 len
= diff
>= foundlen
? 0 : foundlen
- diff
;
70 * Compute best start block and diff for "near" allocations.
71 * freelen >= wantlen already checked by caller.
73 STATIC xfs_extlen_t
/* difference value (absolute) */
74 xfs_alloc_compute_diff(
75 xfs_agblock_t wantbno
, /* target starting block */
76 xfs_extlen_t wantlen
, /* target length */
77 xfs_extlen_t alignment
, /* target alignment */
78 xfs_agblock_t freebno
, /* freespace's starting block */
79 xfs_extlen_t freelen
, /* freespace's length */
80 xfs_agblock_t
*newbnop
) /* result: best start block from free */
82 xfs_agblock_t freeend
; /* end of freespace extent */
83 xfs_agblock_t newbno1
; /* return block number */
84 xfs_agblock_t newbno2
; /* other new block number */
85 xfs_extlen_t newlen1
=0; /* length with newbno1 */
86 xfs_extlen_t newlen2
=0; /* length with newbno2 */
87 xfs_agblock_t wantend
; /* end of target extent */
89 ASSERT(freelen
>= wantlen
);
90 freeend
= freebno
+ freelen
;
91 wantend
= wantbno
+ wantlen
;
92 if (freebno
>= wantbno
) {
93 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
94 newbno1
= NULLAGBLOCK
;
95 } else if (freeend
>= wantend
&& alignment
> 1) {
96 newbno1
= roundup(wantbno
, alignment
);
97 newbno2
= newbno1
- alignment
;
98 if (newbno1
>= freeend
)
99 newbno1
= NULLAGBLOCK
;
101 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
102 if (newbno2
< freebno
)
103 newbno2
= NULLAGBLOCK
;
105 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
106 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
107 if (newlen1
< newlen2
||
108 (newlen1
== newlen2
&&
109 XFS_ABSDIFF(newbno1
, wantbno
) >
110 XFS_ABSDIFF(newbno2
, wantbno
)))
112 } else if (newbno2
!= NULLAGBLOCK
)
114 } else if (freeend
>= wantend
) {
116 } else if (alignment
> 1) {
117 newbno1
= roundup(freeend
- wantlen
, alignment
);
118 if (newbno1
> freeend
- wantlen
&&
119 newbno1
- alignment
>= freebno
)
120 newbno1
-= alignment
;
121 else if (newbno1
>= freeend
)
122 newbno1
= NULLAGBLOCK
;
124 newbno1
= freeend
- wantlen
;
126 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
130 * Fix up the length, based on mod and prod.
131 * len should be k * prod + mod for some k.
132 * If len is too small it is returned unchanged.
133 * If len hits maxlen it is left alone.
137 xfs_alloc_arg_t
*args
) /* allocation argument structure */
142 ASSERT(args
->mod
< args
->prod
);
144 ASSERT(rlen
>= args
->minlen
);
145 ASSERT(rlen
<= args
->maxlen
);
146 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
147 (args
->mod
== 0 && rlen
< args
->prod
))
149 k
= rlen
% args
->prod
;
153 if ((int)(rlen
= rlen
- k
- args
->mod
) < (int)args
->minlen
)
156 if ((int)(rlen
= rlen
- args
->prod
- (args
->mod
- k
)) <
160 ASSERT(rlen
>= args
->minlen
);
161 ASSERT(rlen
<= args
->maxlen
);
166 * Fix up length if there is too little space left in the a.g.
167 * Return 1 if ok, 0 if too little, should give up.
170 xfs_alloc_fix_minleft(
171 xfs_alloc_arg_t
*args
) /* allocation argument structure */
173 xfs_agf_t
*agf
; /* a.g. freelist header */
174 int diff
; /* free space difference */
176 if (args
->minleft
== 0)
178 agf
= XFS_BUF_TO_AGF(args
->agbp
);
179 diff
= INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
)
180 + INT_GET(agf
->agf_flcount
, ARCH_CONVERT
)
181 - args
->len
- args
->minleft
;
184 args
->len
+= diff
; /* shrink the allocated space */
185 if (args
->len
>= args
->minlen
)
187 args
->agbno
= NULLAGBLOCK
;
192 * Update the two btrees, logically removing from freespace the extent
193 * starting at rbno, rlen blocks. The extent is contained within the
194 * actual (current) free extent fbno for flen blocks.
195 * Flags are passed in indicating whether the cursors are set to the
198 STATIC
int /* error code */
199 xfs_alloc_fixup_trees(
200 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
201 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
202 xfs_agblock_t fbno
, /* starting block of free extent */
203 xfs_extlen_t flen
, /* length of free extent */
204 xfs_agblock_t rbno
, /* starting block of returned extent */
205 xfs_extlen_t rlen
, /* length of returned extent */
206 int flags
) /* flags, XFSA_FIXUP_... */
208 int error
; /* error code */
209 int i
; /* operation results */
210 xfs_agblock_t nfbno1
; /* first new free startblock */
211 xfs_agblock_t nfbno2
; /* second new free startblock */
212 xfs_extlen_t nflen1
=0; /* first new free length */
213 xfs_extlen_t nflen2
=0; /* second new free length */
216 * Look up the record in the by-size tree if necessary.
218 if (flags
& XFSA_FIXUP_CNT_OK
) {
220 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
222 XFS_WANT_CORRUPTED_RETURN(
223 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
226 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
228 XFS_WANT_CORRUPTED_RETURN(i
== 1);
231 * Look up the record in the by-block tree if necessary.
233 if (flags
& XFSA_FIXUP_BNO_OK
) {
235 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
237 XFS_WANT_CORRUPTED_RETURN(
238 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
241 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
243 XFS_WANT_CORRUPTED_RETURN(i
== 1);
247 xfs_alloc_block_t
*bnoblock
;
248 xfs_alloc_block_t
*cntblock
;
250 if (bno_cur
->bc_nlevels
== 1 &&
251 cnt_cur
->bc_nlevels
== 1) {
252 bnoblock
= XFS_BUF_TO_ALLOC_BLOCK(bno_cur
->bc_bufs
[0]);
253 cntblock
= XFS_BUF_TO_ALLOC_BLOCK(cnt_cur
->bc_bufs
[0]);
254 XFS_WANT_CORRUPTED_RETURN(
255 INT_GET(bnoblock
->bb_numrecs
, ARCH_CONVERT
) == INT_GET(cntblock
->bb_numrecs
, ARCH_CONVERT
));
260 * Deal with all four cases: the allocated record is contained
261 * within the freespace record, so we can have new freespace
262 * at either (or both) end, or no freespace remaining.
264 if (rbno
== fbno
&& rlen
== flen
)
265 nfbno1
= nfbno2
= NULLAGBLOCK
;
266 else if (rbno
== fbno
) {
267 nfbno1
= rbno
+ rlen
;
268 nflen1
= flen
- rlen
;
269 nfbno2
= NULLAGBLOCK
;
270 } else if (rbno
+ rlen
== fbno
+ flen
) {
272 nflen1
= flen
- rlen
;
273 nfbno2
= NULLAGBLOCK
;
276 nflen1
= rbno
- fbno
;
277 nfbno2
= rbno
+ rlen
;
278 nflen2
= (fbno
+ flen
) - nfbno2
;
281 * Delete the entry from the by-size btree.
283 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
285 XFS_WANT_CORRUPTED_RETURN(i
== 1);
287 * Add new by-size btree entry(s).
289 if (nfbno1
!= NULLAGBLOCK
) {
290 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
292 XFS_WANT_CORRUPTED_RETURN(i
== 0);
293 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
295 XFS_WANT_CORRUPTED_RETURN(i
== 1);
297 if (nfbno2
!= NULLAGBLOCK
) {
298 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
300 XFS_WANT_CORRUPTED_RETURN(i
== 0);
301 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
303 XFS_WANT_CORRUPTED_RETURN(i
== 1);
306 * Fix up the by-block btree entry(s).
308 if (nfbno1
== NULLAGBLOCK
) {
310 * No remaining freespace, just delete the by-block tree entry.
312 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
314 XFS_WANT_CORRUPTED_RETURN(i
== 1);
317 * Update the by-block entry to start later|be shorter.
319 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
322 if (nfbno2
!= NULLAGBLOCK
) {
324 * 2 resulting free entries, need to add one.
326 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
328 XFS_WANT_CORRUPTED_RETURN(i
== 0);
329 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
331 XFS_WANT_CORRUPTED_RETURN(i
== 1);
337 * Read in the allocation group free block array.
339 STATIC
int /* error */
341 xfs_mount_t
*mp
, /* mount point structure */
342 xfs_trans_t
*tp
, /* transaction pointer */
343 xfs_agnumber_t agno
, /* allocation group number */
344 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
346 xfs_buf_t
*bp
; /* return value */
349 ASSERT(agno
!= NULLAGNUMBER
);
350 error
= xfs_trans_read_buf(
351 mp
, tp
, mp
->m_ddev_targp
,
352 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
353 XFS_FSS_TO_BB(mp
, 1), 0, &bp
);
357 ASSERT(!XFS_BUF_GETERROR(bp
));
358 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGFL
, XFS_AGFL_REF
);
363 #if defined(XFS_ALLOC_TRACE)
365 * Add an allocation trace entry for an alloc call.
368 xfs_alloc_trace_alloc(
369 char *name
, /* function tag string */
370 char *str
, /* additional string */
371 xfs_alloc_arg_t
*args
, /* allocation argument structure */
372 int line
) /* source line number */
374 ktrace_enter(xfs_alloc_trace_buf
,
375 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_ALLOC
| (line
<< 16)),
379 (void *)(__psunsigned_t
)args
->agno
,
380 (void *)(__psunsigned_t
)args
->agbno
,
381 (void *)(__psunsigned_t
)args
->minlen
,
382 (void *)(__psunsigned_t
)args
->maxlen
,
383 (void *)(__psunsigned_t
)args
->mod
,
384 (void *)(__psunsigned_t
)args
->prod
,
385 (void *)(__psunsigned_t
)args
->minleft
,
386 (void *)(__psunsigned_t
)args
->total
,
387 (void *)(__psunsigned_t
)args
->alignment
,
388 (void *)(__psunsigned_t
)args
->len
,
389 (void *)((((__psint_t
)args
->type
) << 16) |
390 (__psint_t
)args
->otype
),
391 (void *)(__psint_t
)((args
->wasdel
<< 3) |
392 (args
->wasfromfl
<< 2) |
394 (args
->userdata
<< 0)));
398 * Add an allocation trace entry for a free call.
401 xfs_alloc_trace_free(
402 char *name
, /* function tag string */
403 char *str
, /* additional string */
404 xfs_mount_t
*mp
, /* file system mount point */
405 xfs_agnumber_t agno
, /* allocation group number */
406 xfs_agblock_t agbno
, /* a.g. relative block number */
407 xfs_extlen_t len
, /* length of extent */
408 int isfl
, /* set if is freelist allocation/free */
409 int line
) /* source line number */
411 ktrace_enter(xfs_alloc_trace_buf
,
412 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_FREE
| (line
<< 16)),
416 (void *)(__psunsigned_t
)agno
,
417 (void *)(__psunsigned_t
)agbno
,
418 (void *)(__psunsigned_t
)len
,
419 (void *)(__psint_t
)isfl
,
420 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
424 * Add an allocation trace entry for modifying an agf.
427 xfs_alloc_trace_modagf(
428 char *name
, /* function tag string */
429 char *str
, /* additional string */
430 xfs_mount_t
*mp
, /* file system mount point */
431 xfs_agf_t
*agf
, /* new agf value */
432 int flags
, /* logging flags for agf */
433 int line
) /* source line number */
435 ktrace_enter(xfs_alloc_trace_buf
,
436 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_MODAGF
| (line
<< 16)),
440 (void *)(__psint_t
)flags
,
441 (void *)(__psunsigned_t
)INT_GET(agf
->agf_seqno
, ARCH_CONVERT
),
442 (void *)(__psunsigned_t
)INT_GET(agf
->agf_length
, ARCH_CONVERT
),
443 (void *)(__psunsigned_t
)INT_GET(agf
->agf_roots
[XFS_BTNUM_BNO
],
445 (void *)(__psunsigned_t
)INT_GET(agf
->agf_roots
[XFS_BTNUM_CNT
],
447 (void *)(__psunsigned_t
)INT_GET(agf
->agf_levels
[XFS_BTNUM_BNO
],
449 (void *)(__psunsigned_t
)INT_GET(agf
->agf_levels
[XFS_BTNUM_CNT
],
451 (void *)(__psunsigned_t
)INT_GET(agf
->agf_flfirst
, ARCH_CONVERT
),
452 (void *)(__psunsigned_t
)INT_GET(agf
->agf_fllast
, ARCH_CONVERT
),
453 (void *)(__psunsigned_t
)INT_GET(agf
->agf_flcount
, ARCH_CONVERT
),
454 (void *)(__psunsigned_t
)INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
),
455 (void *)(__psunsigned_t
)INT_GET(agf
->agf_longest
, ARCH_CONVERT
));
457 #endif /* XFS_ALLOC_TRACE */
460 * Allocation group level functions.
464 * Allocate a variable extent in the allocation group agno.
465 * Type and bno are used to determine where in the allocation group the
467 * Extent's length (returned in *len) will be between minlen and maxlen,
468 * and of the form k * prod + mod unless there's nothing that large.
469 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
471 STATIC
int /* error */
472 xfs_alloc_ag_vextent(
473 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
476 #ifdef XFS_ALLOC_TRACE
477 static char fname
[] = "xfs_alloc_ag_vextent";
480 ASSERT(args
->minlen
> 0);
481 ASSERT(args
->maxlen
> 0);
482 ASSERT(args
->minlen
<= args
->maxlen
);
483 ASSERT(args
->mod
< args
->prod
);
484 ASSERT(args
->alignment
> 0);
486 * Branch to correct routine based on the type.
489 switch (args
->type
) {
490 case XFS_ALLOCTYPE_THIS_AG
:
491 error
= xfs_alloc_ag_vextent_size(args
);
493 case XFS_ALLOCTYPE_NEAR_BNO
:
494 error
= xfs_alloc_ag_vextent_near(args
);
496 case XFS_ALLOCTYPE_THIS_BNO
:
497 error
= xfs_alloc_ag_vextent_exact(args
);
506 * If the allocation worked, need to change the agf structure
507 * (and log it), and the superblock.
509 if (args
->agbno
!= NULLAGBLOCK
) {
510 xfs_agf_t
*agf
; /* allocation group freelist header */
511 #ifdef XFS_ALLOC_TRACE
512 xfs_mount_t
*mp
= args
->mp
;
514 long slen
= (long)args
->len
;
516 ASSERT(args
->len
>= args
->minlen
&& args
->len
<= args
->maxlen
);
517 ASSERT(!(args
->wasfromfl
) || !args
->isfl
);
518 ASSERT(args
->agbno
% args
->alignment
== 0);
519 if (!(args
->wasfromfl
)) {
521 agf
= XFS_BUF_TO_AGF(args
->agbp
);
522 INT_MOD(agf
->agf_freeblks
, ARCH_CONVERT
, -(args
->len
));
523 xfs_trans_agblocks_delta(args
->tp
,
524 -((long)(args
->len
)));
525 args
->pag
->pagf_freeblks
-= args
->len
;
526 ASSERT(INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
)
527 <= INT_GET(agf
->agf_length
, ARCH_CONVERT
));
528 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
529 xfs_alloc_log_agf(args
->tp
, args
->agbp
,
531 /* search the busylist for these blocks */
532 xfs_alloc_search_busy(args
->tp
, args
->agno
,
533 args
->agbno
, args
->len
);
536 xfs_trans_mod_sb(args
->tp
,
537 args
->wasdel
? XFS_TRANS_SB_RES_FDBLOCKS
:
538 XFS_TRANS_SB_FDBLOCKS
, -slen
);
539 XFS_STATS_INC(xfsstats
.xs_allocx
);
540 XFS_STATS_ADD(xfsstats
.xs_allocb
, args
->len
);
546 * Allocate a variable extent at exactly agno/bno.
547 * Extent's length (returned in *len) will be between minlen and maxlen,
548 * and of the form k * prod + mod unless there's nothing that large.
549 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
551 STATIC
int /* error */
552 xfs_alloc_ag_vextent_exact(
553 xfs_alloc_arg_t
*args
) /* allocation argument structure */
555 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
556 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
557 xfs_agblock_t end
; /* end of allocated extent */
559 xfs_agblock_t fbno
; /* start block of found extent */
560 xfs_agblock_t fend
; /* end block of found extent */
561 xfs_extlen_t flen
; /* length of found extent */
562 #ifdef XFS_ALLOC_TRACE
563 static char fname
[] = "xfs_alloc_ag_vextent_exact";
565 int i
; /* success/failure of operation */
566 xfs_agblock_t maxend
; /* end of maximal extent */
567 xfs_agblock_t minend
; /* end of minimal extent */
568 xfs_extlen_t rlen
; /* length of returned extent */
570 ASSERT(args
->alignment
== 1);
572 * Allocate/initialize a cursor for the by-number freespace btree.
574 bno_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
575 args
->agno
, XFS_BTNUM_BNO
, 0, 0);
577 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
578 * Look for the closest free block <= bno, it must contain bno
579 * if any free block does.
581 if ((error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
)))
585 * Didn't find it, return null.
587 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
588 args
->agbno
= NULLAGBLOCK
;
592 * Grab the freespace record.
594 if ((error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
)))
596 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
597 ASSERT(fbno
<= args
->agbno
);
598 minend
= args
->agbno
+ args
->minlen
;
599 maxend
= args
->agbno
+ args
->maxlen
;
602 * Give up if the freespace isn't long enough for the minimum request.
605 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
606 args
->agbno
= NULLAGBLOCK
;
610 * End of extent will be smaller of the freespace end and the
611 * maximal requested end.
613 end
= XFS_AGBLOCK_MIN(fend
, maxend
);
615 * Fix the length according to mod and prod if given.
617 args
->len
= end
- args
->agbno
;
618 xfs_alloc_fix_len(args
);
619 if (!xfs_alloc_fix_minleft(args
)) {
620 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
624 ASSERT(args
->agbno
+ rlen
<= fend
);
625 end
= args
->agbno
+ rlen
;
627 * We are allocating agbno for rlen [agbno .. end]
628 * Allocate/initialize a cursor for the by-size btree.
630 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
631 args
->agno
, XFS_BTNUM_CNT
, 0, 0);
632 ASSERT(args
->agbno
+ args
->len
<=
633 INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
,
635 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
636 args
->agbno
, args
->len
, XFSA_FIXUP_BNO_OK
))) {
637 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
640 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
641 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
642 TRACE_ALLOC("normal", args
);
647 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
648 TRACE_ALLOC("error", args
);
653 * Allocate a variable extent near bno in the allocation group agno.
654 * Extent's length (returned in len) will be between minlen and maxlen,
655 * and of the form k * prod + mod unless there's nothing that large.
656 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
658 STATIC
int /* error */
659 xfs_alloc_ag_vextent_near(
660 xfs_alloc_arg_t
*args
) /* allocation argument structure */
662 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
663 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
664 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
665 #ifdef XFS_ALLOC_TRACE
666 static char fname
[] = "xfs_alloc_ag_vextent_near";
668 xfs_agblock_t gtbno
; /* start bno of right side entry */
669 xfs_agblock_t gtbnoa
; /* aligned ... */
670 xfs_extlen_t gtdiff
; /* difference to right side entry */
671 xfs_extlen_t gtlen
; /* length of right side entry */
672 xfs_extlen_t gtlena
; /* aligned ... */
673 xfs_agblock_t gtnew
; /* useful start bno of right side */
674 int error
; /* error code */
675 int i
; /* result code, temporary */
676 int j
; /* result code, temporary */
677 xfs_agblock_t ltbno
; /* start bno of left side entry */
678 xfs_agblock_t ltbnoa
; /* aligned ... */
679 xfs_extlen_t ltdiff
; /* difference to left side entry */
681 xfs_agblock_t ltend
; /* end bno of left side entry */
682 xfs_extlen_t ltlen
; /* length of left side entry */
683 xfs_extlen_t ltlena
; /* aligned ... */
684 xfs_agblock_t ltnew
; /* useful start bno of left side */
685 xfs_extlen_t rlen
; /* length of returned extent */
686 #if defined(DEBUG) && defined(__KERNEL__)
688 * Randomly don't execute the first algorithm.
690 static int seed
; /* randomizing seed value */
691 int dofirst
; /* set to do first algorithm */
692 timespec_t now
; /* current time */
696 seed
= (int)now
.tv_sec
^ (int)now
.tv_nsec
;
698 dofirst
= random() & 1;
701 * Get a cursor for the by-size btree.
703 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
704 args
->agno
, XFS_BTNUM_CNT
, 0, 0);
706 bno_cur_lt
= bno_cur_gt
= NULL
;
708 * See if there are any free extents as big as maxlen.
710 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
713 * If none, then pick up the last entry in the tree unless the
717 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
720 if (i
== 0 || ltlen
== 0) {
721 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
729 * If the requested extent is large wrt the freespaces available
730 * in this a.g., then the cursor will be pointing to a btree entry
731 * near the right edge of the tree. If it's in the last btree leaf
732 * block, then we just examine all the entries in that block
733 * that are big enough, and pick the best one.
734 * This is written as a while loop so we can break out of it,
735 * but we never loop back to the top.
737 while (xfs_btree_islastblock(cnt_cur
, 0)) {
741 xfs_agblock_t bnew
=0;
743 #if defined(DEBUG) && defined(__KERNEL__)
748 * Start from the entry that lookup found, sequence through
749 * all larger free blocks. If we're actually pointing at a
750 * record smaller than maxlen, go to the start of this block,
751 * and skip all those smaller than minlen.
753 if (ltlen
|| args
->alignment
> 1) {
754 cnt_cur
->bc_ptrs
[0] = 1;
756 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
759 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
760 if (ltlen
>= args
->minlen
)
762 if ((error
= xfs_alloc_increment(cnt_cur
, 0, &i
)))
765 ASSERT(ltlen
>= args
->minlen
);
769 i
= cnt_cur
->bc_ptrs
[0];
770 for (j
= 1, blen
= 0, bdiff
= 0;
771 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
772 error
= xfs_alloc_increment(cnt_cur
, 0, &j
)) {
774 * For each entry, decide if it's better than
775 * the previous best entry.
777 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
779 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
780 if (!xfs_alloc_compute_aligned(ltbno
, ltlen
,
781 args
->alignment
, args
->minlen
,
784 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
785 xfs_alloc_fix_len(args
);
786 ASSERT(args
->len
>= args
->minlen
);
787 if (args
->len
< blen
)
789 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
790 args
->alignment
, ltbno
, ltlen
, <new
);
791 if (ltnew
!= NULLAGBLOCK
&&
792 (args
->len
> blen
|| ltdiff
< bdiff
)) {
796 besti
= cnt_cur
->bc_ptrs
[0];
800 * It didn't work. We COULD be in a case where
801 * there's a good record somewhere, so try again.
806 * Point at the best entry, and retrieve it again.
808 cnt_cur
->bc_ptrs
[0] = besti
;
809 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
811 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
812 ltend
= ltbno
+ ltlen
;
813 ASSERT(ltend
<= INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
,
816 if (!xfs_alloc_fix_minleft(args
)) {
817 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
818 TRACE_ALLOC("nominleft", args
);
823 * We are allocating starting at bnew for blen blocks.
826 ASSERT(bnew
>= ltbno
);
827 ASSERT(bnew
+ blen
<= ltend
);
829 * Set up a cursor for the by-bno tree.
831 bno_cur_lt
= xfs_btree_init_cursor(args
->mp
, args
->tp
,
832 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
, 0, 0);
834 * Fix up the btree entries.
836 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
837 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
839 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
840 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
841 TRACE_ALLOC("first", args
);
846 * Search in the by-bno tree to the left and to the right
847 * simultaneously, until in each case we find a space big enough,
848 * or run into the edge of the tree. When we run into the edge,
849 * we deallocate that cursor.
850 * If both searches succeed, we compare the two spaces and pick
852 * With alignment, it's possible for both to fail; the upper
853 * level algorithm that picks allocation groups for allocations
854 * is not supposed to do this.
857 * Allocate and initialize the cursor for the leftward search.
859 bno_cur_lt
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
860 args
->agno
, XFS_BTNUM_BNO
, 0, 0);
862 * Lookup <= bno to find the leftward search's starting point.
864 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
868 * Didn't find anything; use this cursor for the rightward
871 bno_cur_gt
= bno_cur_lt
;
875 * Found something. Duplicate the cursor for the rightward search.
877 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
880 * Increment the cursor, so we will point at the entry just right
881 * of the leftward entry if any, or to the leftmost entry.
883 if ((error
= xfs_alloc_increment(bno_cur_gt
, 0, &i
)))
887 * It failed, there are no rightward entries.
889 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
893 * Loop going left with the leftward cursor, right with the
894 * rightward cursor, until either both directions give up or
895 * we find an entry at least as big as minlen.
899 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
901 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
902 if (xfs_alloc_compute_aligned(ltbno
, ltlen
,
903 args
->alignment
, args
->minlen
,
906 if ((error
= xfs_alloc_decrement(bno_cur_lt
, 0, &i
)))
909 xfs_btree_del_cursor(bno_cur_lt
,
915 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
917 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
918 if (xfs_alloc_compute_aligned(gtbno
, gtlen
,
919 args
->alignment
, args
->minlen
,
922 if ((error
= xfs_alloc_increment(bno_cur_gt
, 0, &i
)))
925 xfs_btree_del_cursor(bno_cur_gt
,
930 } while (bno_cur_lt
|| bno_cur_gt
);
932 * Got both cursors still active, need to find better entry.
934 if (bno_cur_lt
&& bno_cur_gt
) {
936 * Left side is long enough, look for a right side entry.
938 if (ltlena
>= args
->minlen
) {
942 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
943 xfs_alloc_fix_len(args
);
945 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
946 args
->alignment
, ltbno
, ltlen
, <new
);
952 * Look until we find a better one, run out of
953 * space, or run off the end.
955 while (bno_cur_lt
&& bno_cur_gt
) {
956 if ((error
= xfs_alloc_get_rec(
960 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
961 xfs_alloc_compute_aligned(gtbno
, gtlen
,
962 args
->alignment
, args
->minlen
,
965 * The left one is clearly better.
967 if (gtbnoa
>= args
->agbno
+ ltdiff
) {
968 xfs_btree_del_cursor(
975 * If we reach a big enough entry,
976 * compare the two and pick the best.
978 if (gtlena
>= args
->minlen
) {
980 XFS_EXTLEN_MIN(gtlena
,
982 xfs_alloc_fix_len(args
);
984 gtdiff
= xfs_alloc_compute_diff(
987 gtbno
, gtlen
, >new
);
989 * Right side is better.
991 if (gtdiff
< ltdiff
) {
992 xfs_btree_del_cursor(
998 * Left side is better.
1001 xfs_btree_del_cursor(
1009 * Fell off the right end.
1011 if ((error
= xfs_alloc_increment(
1012 bno_cur_gt
, 0, &i
)))
1015 xfs_btree_del_cursor(
1024 * The left side is perfect, trash the right side.
1027 xfs_btree_del_cursor(bno_cur_gt
,
1033 * It's the right side that was found first, look left.
1037 * Fix up the length.
1039 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1040 xfs_alloc_fix_len(args
);
1042 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
1043 args
->alignment
, gtbno
, gtlen
, >new
);
1045 * Right side entry isn't perfect.
1049 * Look until we find a better one, run out of
1050 * space, or run off the end.
1052 while (bno_cur_lt
&& bno_cur_gt
) {
1053 if ((error
= xfs_alloc_get_rec(
1057 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1058 xfs_alloc_compute_aligned(ltbno
, ltlen
,
1059 args
->alignment
, args
->minlen
,
1062 * The right one is clearly better.
1064 if (ltbnoa
<= args
->agbno
- gtdiff
) {
1065 xfs_btree_del_cursor(
1072 * If we reach a big enough entry,
1073 * compare the two and pick the best.
1075 if (ltlena
>= args
->minlen
) {
1076 args
->len
= XFS_EXTLEN_MIN(
1077 ltlena
, args
->maxlen
);
1078 xfs_alloc_fix_len(args
);
1080 ltdiff
= xfs_alloc_compute_diff(
1083 ltbno
, ltlen
, <new
);
1085 * Left side is better.
1087 if (ltdiff
< gtdiff
) {
1088 xfs_btree_del_cursor(
1094 * Right side is better.
1097 xfs_btree_del_cursor(
1105 * Fell off the left end.
1107 if ((error
= xfs_alloc_decrement(
1108 bno_cur_lt
, 0, &i
)))
1111 xfs_btree_del_cursor(bno_cur_lt
,
1119 * The right side is perfect, trash the left side.
1122 xfs_btree_del_cursor(bno_cur_lt
,
1129 * If we couldn't get anything, give up.
1131 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1132 TRACE_ALLOC("neither", args
);
1133 args
->agbno
= NULLAGBLOCK
;
1137 * At this point we have selected a freespace entry, either to the
1138 * left or to the right. If it's on the right, copy all the
1139 * useful variables to the "left" set so we only have one
1140 * copy of this code.
1143 bno_cur_lt
= bno_cur_gt
;
1153 * Fix up the length and compute the useful address.
1155 ltend
= ltbno
+ ltlen
;
1156 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1157 xfs_alloc_fix_len(args
);
1158 if (!xfs_alloc_fix_minleft(args
)) {
1159 TRACE_ALLOC("nominleft", args
);
1160 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1161 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1165 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
, ltbno
,
1167 ASSERT(ltnew
>= ltbno
);
1168 ASSERT(ltnew
+ rlen
<= ltend
);
1169 ASSERT(ltnew
+ rlen
<= INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
,
1171 args
->agbno
= ltnew
;
1172 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1173 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1175 TRACE_ALLOC(j
? "gt" : "lt", args
);
1176 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1177 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1181 TRACE_ALLOC("error", args
);
1182 if (cnt_cur
!= NULL
)
1183 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1184 if (bno_cur_lt
!= NULL
)
1185 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1186 if (bno_cur_gt
!= NULL
)
1187 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1192 * Allocate a variable extent anywhere in the allocation group agno.
1193 * Extent's length (returned in len) will be between minlen and maxlen,
1194 * and of the form k * prod + mod unless there's nothing that large.
1195 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1197 STATIC
int /* error */
1198 xfs_alloc_ag_vextent_size(
1199 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1201 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1202 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1203 int error
; /* error result */
1204 xfs_agblock_t fbno
; /* start of found freespace */
1205 xfs_extlen_t flen
; /* length of found freespace */
1206 #ifdef XFS_ALLOC_TRACE
1207 static char fname
[] = "xfs_alloc_ag_vextent_size";
1209 int i
; /* temp status variable */
1210 xfs_agblock_t rbno
; /* returned block number */
1211 xfs_extlen_t rlen
; /* length of returned extent */
1214 * Allocate and initialize a cursor for the by-size btree.
1216 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1217 args
->agno
, XFS_BTNUM_CNT
, 0, 0);
1220 * Look for an entry >= maxlen+alignment-1 blocks.
1222 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1223 args
->maxlen
+ args
->alignment
- 1, &i
)))
1226 * If none, then pick up the last entry in the tree unless the
1230 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, &fbno
,
1233 if (i
== 0 || flen
== 0) {
1234 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1235 TRACE_ALLOC("noentry", args
);
1241 * There's a freespace as big as maxlen+alignment-1, get it.
1244 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
)))
1246 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1249 * In the first case above, we got the last entry in the
1250 * by-size btree. Now we check to see if the space hits maxlen
1251 * once aligned; if not, we search left for something better.
1252 * This can't happen in the second case above.
1254 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
, args
->minlen
,
1256 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1257 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1258 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1259 if (rlen
< args
->maxlen
) {
1260 xfs_agblock_t bestfbno
;
1261 xfs_extlen_t bestflen
;
1262 xfs_agblock_t bestrbno
;
1263 xfs_extlen_t bestrlen
;
1270 if ((error
= xfs_alloc_decrement(cnt_cur
, 0, &i
)))
1274 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1277 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1278 if (flen
< bestrlen
)
1280 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
,
1281 args
->minlen
, &rbno
, &rlen
);
1282 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1283 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1284 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1286 if (rlen
> bestrlen
) {
1291 if (rlen
== args
->maxlen
)
1295 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1298 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1304 args
->wasfromfl
= 0;
1306 * Fix up the length.
1309 xfs_alloc_fix_len(args
);
1310 if (rlen
< args
->minlen
|| !xfs_alloc_fix_minleft(args
)) {
1311 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1312 TRACE_ALLOC("nominleft", args
);
1313 args
->agbno
= NULLAGBLOCK
;
1317 XFS_WANT_CORRUPTED_GOTO(rlen
<= flen
, error0
);
1319 * Allocate and initialize a cursor for the by-block tree.
1321 bno_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1322 args
->agno
, XFS_BTNUM_BNO
, 0, 0);
1323 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1324 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1326 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1327 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1328 cnt_cur
= bno_cur
= NULL
;
1331 XFS_WANT_CORRUPTED_GOTO(
1332 args
->agbno
+ args
->len
<=
1333 INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
,
1336 TRACE_ALLOC("normal", args
);
1340 TRACE_ALLOC("error", args
);
1342 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1344 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1349 * Deal with the case where only small freespaces remain.
1350 * Either return the contents of the last freespace record,
1351 * or allocate space from the freelist if there is nothing in the tree.
1353 STATIC
int /* error */
1354 xfs_alloc_ag_vextent_small(
1355 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1356 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1357 xfs_agblock_t
*fbnop
, /* result block number */
1358 xfs_extlen_t
*flenp
, /* result length */
1359 int *stat
) /* status: 0-freelist, 1-normal/none */
1364 #ifdef XFS_ALLOC_TRACE
1365 static char fname
[] = "xfs_alloc_ag_vextent_small";
1369 if ((error
= xfs_alloc_decrement(ccur
, 0, &i
)))
1372 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1374 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1377 * Nothing in the btree, try the freelist. Make sure
1378 * to respect minleft even when pulling from the
1381 else if (args
->minlen
== 1 && args
->alignment
== 1 && !args
->isfl
&&
1382 (INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
,
1383 ARCH_CONVERT
) > args
->minleft
)) {
1384 if ((error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
)))
1386 if (fbno
!= NULLAGBLOCK
) {
1387 if (args
->userdata
) {
1390 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1391 args
->agno
, fbno
, 0);
1392 xfs_trans_binval(args
->tp
, bp
);
1396 XFS_WANT_CORRUPTED_GOTO(
1397 args
->agbno
+ args
->len
<=
1398 INT_GET(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
,
1401 args
->wasfromfl
= 1;
1402 TRACE_ALLOC("freelist", args
);
1407 * Nothing in the freelist.
1413 * Can't allocate from the freelist for some reason.
1418 * Can't do the allocation, give up.
1420 if (flen
< args
->minlen
) {
1421 args
->agbno
= NULLAGBLOCK
;
1422 TRACE_ALLOC("notenough", args
);
1428 TRACE_ALLOC("normal", args
);
1432 TRACE_ALLOC("error", args
);
1437 * Free the extent starting at agno/bno for length.
1439 STATIC
int /* error */
1441 xfs_trans_t
*tp
, /* transaction pointer */
1442 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
1443 xfs_agnumber_t agno
, /* allocation group number */
1444 xfs_agblock_t bno
, /* starting block number */
1445 xfs_extlen_t len
, /* length of extent */
1446 int isfl
) /* set if is freelist blocks - no sb acctg */
1448 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1449 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1450 int error
; /* error return value */
1451 #ifdef XFS_ALLOC_TRACE
1452 static char fname
[] = "xfs_free_ag_extent";
1454 xfs_agblock_t gtbno
; /* start of right neighbor block */
1455 xfs_extlen_t gtlen
; /* length of right neighbor block */
1456 int haveleft
; /* have a left neighbor block */
1457 int haveright
; /* have a right neighbor block */
1458 int i
; /* temp, result code */
1459 xfs_agblock_t ltbno
; /* start of left neighbor block */
1460 xfs_extlen_t ltlen
; /* length of left neighbor block */
1461 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1462 xfs_agblock_t nbno
; /* new starting block of freespace */
1463 xfs_extlen_t nlen
; /* new length of freespace */
1467 * Allocate and initialize a cursor for the by-block btree.
1469 bno_cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
, 0,
1473 * Look for a neighboring block on the left (lower block numbers)
1474 * that is contiguous with this space.
1476 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1480 * There is a block to our left.
1482 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1484 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1486 * It's not contiguous, though.
1488 if (ltbno
+ ltlen
< bno
)
1492 * If this failure happens the request to free this
1493 * space was invalid, it's (partly) already free.
1496 XFS_WANT_CORRUPTED_GOTO(ltbno
+ ltlen
<= bno
, error0
);
1500 * Look for a neighboring block on the right (higher block numbers)
1501 * that is contiguous with this space.
1503 if ((error
= xfs_alloc_increment(bno_cur
, 0, &haveright
)))
1507 * There is a block to our right.
1509 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1511 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1513 * It's not contiguous, though.
1515 if (bno
+ len
< gtbno
)
1519 * If this failure happens the request to free this
1520 * space was invalid, it's (partly) already free.
1523 XFS_WANT_CORRUPTED_GOTO(gtbno
>= bno
+ len
, error0
);
1527 * Now allocate and initialize a cursor for the by-size tree.
1529 cnt_cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
, 0,
1532 * Have both left and right contiguous neighbors.
1533 * Merge all three into a single free block.
1535 if (haveleft
&& haveright
) {
1537 * Delete the old by-size entry on the left.
1539 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1541 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1542 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1544 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1546 * Delete the old by-size entry on the right.
1548 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1550 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1551 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1553 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1555 * Delete the old by-block entry for the right block.
1557 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
1559 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1561 * Move the by-block cursor back to the left neighbor.
1563 if ((error
= xfs_alloc_decrement(bno_cur
, 0, &i
)))
1565 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1568 * Check that this is the right record: delete didn't
1569 * mangle the cursor.
1572 xfs_agblock_t xxbno
;
1575 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1578 XFS_WANT_CORRUPTED_GOTO(
1579 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1584 * Update remaining by-block entry to the new, joined block.
1587 nlen
= len
+ ltlen
+ gtlen
;
1588 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1592 * Have only a left contiguous neighbor.
1593 * Merge it together with the new freespace.
1595 else if (haveleft
) {
1597 * Delete the old by-size entry on the left.
1599 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1601 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1602 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1604 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1606 * Back up the by-block cursor to the left neighbor, and
1607 * update its length.
1609 if ((error
= xfs_alloc_decrement(bno_cur
, 0, &i
)))
1611 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1614 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1618 * Have only a right contiguous neighbor.
1619 * Merge it together with the new freespace.
1621 else if (haveright
) {
1623 * Delete the old by-size entry on the right.
1625 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1627 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1628 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1630 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1632 * Update the starting block and length of the right
1633 * neighbor in the by-block tree.
1637 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1641 * No contiguous neighbors.
1642 * Insert the new freespace into the by-block tree.
1647 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
1649 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1651 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1654 * In all cases we need to insert the new freespace in the by-size tree.
1656 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1658 XFS_WANT_CORRUPTED_GOTO(i
== 0, error0
);
1659 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
1661 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1662 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1665 * Update the freespace totals in the ag and superblock.
1669 xfs_perag_t
*pag
; /* per allocation group data */
1671 agf
= XFS_BUF_TO_AGF(agbp
);
1672 pag
= &mp
->m_perag
[agno
];
1673 INT_MOD(agf
->agf_freeblks
, ARCH_CONVERT
, len
);
1674 xfs_trans_agblocks_delta(tp
, len
);
1675 pag
->pagf_freeblks
+= len
;
1676 XFS_WANT_CORRUPTED_GOTO(
1677 INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
)
1678 <= INT_GET(agf
->agf_length
, ARCH_CONVERT
),
1680 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
1681 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
1683 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, (long)len
);
1684 XFS_STATS_INC(xfsstats
.xs_freex
);
1685 XFS_STATS_ADD(xfsstats
.xs_freeb
, len
);
1687 TRACE_FREE(haveleft
?
1688 (haveright
? "both" : "left") :
1689 (haveright
? "right" : "none"),
1690 agno
, bno
, len
, isfl
);
1693 * Since blocks move to the free list without the coordination
1694 * used in xfs_bmap_finish, we can't allow block to be available
1695 * for reallocation and non-transaction writing (user data)
1696 * until we know that the transaction that moved it to the free
1697 * list is permanently on disk. We track the blocks by declaring
1698 * these blocks as "busy"; the busy list is maintained on a per-ag
1699 * basis and each transaction records which entries should be removed
1700 * when the iclog commits to disk. If a busy block is allocated,
1701 * the iclog is pushed up to the LSN that freed the block.
1703 xfs_alloc_mark_busy(tp
, agno
, bno
, len
);
1707 TRACE_FREE("error", agno
, bno
, len
, isfl
);
1709 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1711 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1716 * Visible (exported) allocation/free functions.
1717 * Some of these are used just by xfs_alloc_btree.c and this file.
1721 * Compute and fill in value of m_ag_maxlevels.
1724 xfs_alloc_compute_maxlevels(
1725 xfs_mount_t
*mp
) /* file system mount structure */
1733 maxleafents
= (mp
->m_sb
.sb_agblocks
+ 1) / 2;
1734 minleafrecs
= mp
->m_alloc_mnr
[0];
1735 minnoderecs
= mp
->m_alloc_mnr
[1];
1736 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1737 for (level
= 1; maxblocks
> 1; level
++)
1738 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1739 mp
->m_ag_maxlevels
= level
;
1743 * Decide whether to use this allocation group for this allocation.
1744 * If so, fix up the btree freelist's size.
1745 * This is external so mkfs can call it, too.
1748 xfs_alloc_fix_freelist(
1749 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1750 int flags
) /* XFS_ALLOC_FLAG_... */
1752 xfs_buf_t
*agbp
; /* agf buffer pointer */
1753 xfs_agf_t
*agf
; /* a.g. freespace structure pointer */
1754 xfs_buf_t
*agflbp
;/* agfl buffer pointer */
1755 xfs_agblock_t bno
; /* freelist block */
1756 xfs_extlen_t delta
; /* new blocks needed in freelist */
1757 int error
; /* error result code */
1758 xfs_extlen_t longest
;/* longest extent in allocation group */
1759 xfs_mount_t
*mp
; /* file system mount point structure */
1760 xfs_extlen_t need
; /* total blocks needed in freelist */
1761 xfs_perag_t
*pag
; /* per-ag information structure */
1762 xfs_alloc_arg_t targs
; /* local allocation arguments */
1763 xfs_trans_t
*tp
; /* transaction pointer */
1769 if (!pag
->pagf_init
) {
1770 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1773 if (!pag
->pagf_init
) {
1780 /* If this is a metadata prefered pag and we are user data
1781 * then try somewhere else if we are not being asked to
1782 * try harder at this point
1784 if (pag
->pagf_metadata
&& args
->userdata
&& flags
) {
1789 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
1790 delta
= need
> pag
->pagf_flcount
? need
- pag
->pagf_flcount
: 0;
1792 * If it looks like there isn't a long enough extent, or enough
1793 * total blocks, reject it.
1795 longest
= (pag
->pagf_longest
> delta
) ?
1796 (pag
->pagf_longest
- delta
) :
1797 (pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0);
1798 if (args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1 > longest
||
1800 (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
1801 need
- args
->total
) <
1802 (int)args
->minleft
)) {
1804 xfs_trans_brelse(tp
, agbp
);
1809 * Get the a.g. freespace buffer.
1810 * Can fail if we're not blocking on locks, and it's held.
1813 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1822 * Figure out how many blocks we should have in the freelist.
1824 agf
= XFS_BUF_TO_AGF(agbp
);
1825 need
= XFS_MIN_FREELIST(agf
, mp
);
1826 delta
= need
> INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) ?
1827 (need
- INT_GET(agf
->agf_flcount
, ARCH_CONVERT
)) : 0;
1829 * If there isn't enough total or single-extent, reject it.
1831 longest
= INT_GET(agf
->agf_longest
, ARCH_CONVERT
);
1832 longest
= (longest
> delta
) ? (longest
- delta
) :
1833 (INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) > 0 || longest
> 0);
1834 if (args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1 > longest
||
1836 (int)(INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
) +
1837 INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) - need
- args
->total
) <
1838 (int)args
->minleft
)) {
1839 xfs_trans_brelse(tp
, agbp
);
1844 * Make the freelist shorter if it's too long.
1846 while (INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) > need
) {
1849 if ((error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
)))
1851 if ((error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1, 1)))
1853 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
1854 xfs_trans_binval(tp
, bp
);
1857 * Initialize the args structure.
1862 targs
.agno
= args
->agno
;
1863 targs
.mod
= targs
.minleft
= targs
.wasdel
= targs
.userdata
=
1864 targs
.minalignslop
= 0;
1865 targs
.alignment
= targs
.minlen
= targs
.prod
= targs
.isfl
= 1;
1866 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
1868 if ((error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
)))
1871 * Make the freelist longer if it's too short.
1873 while (INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) < need
) {
1875 targs
.maxlen
= need
- INT_GET(agf
->agf_flcount
, ARCH_CONVERT
);
1877 * Allocate as many blocks as possible at once.
1879 if ((error
= xfs_alloc_ag_vextent(&targs
)))
1882 * Stop if we run out. Won't happen if callers are obeying
1883 * the restrictions correctly. Can happen for free calls
1884 * on a completely full ag.
1886 if (targs
.agbno
== NULLAGBLOCK
)
1889 * Put each allocated block on the list.
1891 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
1892 if ((error
= xfs_alloc_put_freelist(tp
, agbp
, agflbp
,
1902 * Get a block from the freelist.
1903 * Returns with the buffer for the block gotten.
1906 xfs_alloc_get_freelist(
1907 xfs_trans_t
*tp
, /* transaction pointer */
1908 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
1909 xfs_agblock_t
*bnop
) /* block address retrieved from freelist */
1911 xfs_agf_t
*agf
; /* a.g. freespace structure */
1912 xfs_agfl_t
*agfl
; /* a.g. freelist structure */
1913 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
1914 xfs_agblock_t bno
; /* block number returned */
1916 #ifdef XFS_ALLOC_TRACE
1917 static char fname
[] = "xfs_alloc_get_freelist";
1919 xfs_mount_t
*mp
; /* mount structure */
1920 xfs_perag_t
*pag
; /* per allocation group data */
1922 agf
= XFS_BUF_TO_AGF(agbp
);
1924 * Freelist is empty, give up.
1926 if (INT_ISZERO(agf
->agf_flcount
, ARCH_CONVERT
)) {
1927 *bnop
= NULLAGBLOCK
;
1931 * Read the array of free blocks.
1934 if ((error
= xfs_alloc_read_agfl(mp
, tp
,
1935 INT_GET(agf
->agf_seqno
, ARCH_CONVERT
), &agflbp
)))
1937 agfl
= XFS_BUF_TO_AGFL(agflbp
);
1939 * Get the block number and update the data structures.
1941 bno
= INT_GET(agfl
->agfl_bno
[INT_GET(agf
->agf_flfirst
, ARCH_CONVERT
)], ARCH_CONVERT
);
1942 INT_MOD(agf
->agf_flfirst
, ARCH_CONVERT
, 1);
1943 xfs_trans_brelse(tp
, agflbp
);
1944 if (INT_GET(agf
->agf_flfirst
, ARCH_CONVERT
) == XFS_AGFL_SIZE(mp
))
1945 INT_ZERO(agf
->agf_flfirst
, ARCH_CONVERT
);
1946 pag
= &mp
->m_perag
[INT_GET(agf
->agf_seqno
, ARCH_CONVERT
)];
1947 INT_MOD(agf
->agf_flcount
, ARCH_CONVERT
, -1);
1948 xfs_trans_agflist_delta(tp
, -1);
1949 pag
->pagf_flcount
--;
1950 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
);
1951 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
);
1955 * As blocks are freed, they are added to the per-ag busy list
1956 * and remain there until the freeing transaction is committed to
1957 * disk. Now that we have allocated blocks, this list must be
1958 * searched to see if a block is being reused. If one is, then
1959 * the freeing transaction must be pushed to disk NOW by forcing
1960 * to disk all iclogs up that transaction's LSN.
1962 xfs_alloc_search_busy(tp
, INT_GET(agf
->agf_seqno
, ARCH_CONVERT
), bno
, 1);
1967 * Log the given fields from the agf structure.
1971 xfs_trans_t
*tp
, /* transaction pointer */
1972 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
1973 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
1975 int first
; /* first byte offset */
1976 int last
; /* last byte offset */
1977 static const short offsets
[] = {
1978 offsetof(xfs_agf_t
, agf_magicnum
),
1979 offsetof(xfs_agf_t
, agf_versionnum
),
1980 offsetof(xfs_agf_t
, agf_seqno
),
1981 offsetof(xfs_agf_t
, agf_length
),
1982 offsetof(xfs_agf_t
, agf_roots
[0]),
1983 offsetof(xfs_agf_t
, agf_levels
[0]),
1984 offsetof(xfs_agf_t
, agf_flfirst
),
1985 offsetof(xfs_agf_t
, agf_fllast
),
1986 offsetof(xfs_agf_t
, agf_flcount
),
1987 offsetof(xfs_agf_t
, agf_freeblks
),
1988 offsetof(xfs_agf_t
, agf_longest
),
1992 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
1993 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
1997 * Interface for inode allocation to force the pag data to be initialized.
2000 xfs_alloc_pagf_init(
2001 xfs_mount_t
*mp
, /* file system mount structure */
2002 xfs_trans_t
*tp
, /* transaction pointer */
2003 xfs_agnumber_t agno
, /* allocation group number */
2004 int flags
) /* XFS_ALLOC_FLAGS_... */
2009 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2012 xfs_trans_brelse(tp
, bp
);
2017 * Put the block on the freelist for the allocation group.
2020 xfs_alloc_put_freelist(
2021 xfs_trans_t
*tp
, /* transaction pointer */
2022 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2023 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2024 xfs_agblock_t bno
) /* block being freed */
2026 xfs_agf_t
*agf
; /* a.g. freespace structure */
2027 xfs_agfl_t
*agfl
; /* a.g. free block array */
2028 xfs_agblock_t
*blockp
;/* pointer to array entry */
2030 #ifdef XFS_ALLOC_TRACE
2031 static char fname
[] = "xfs_alloc_put_freelist";
2033 xfs_mount_t
*mp
; /* mount structure */
2034 xfs_perag_t
*pag
; /* per allocation group data */
2036 agf
= XFS_BUF_TO_AGF(agbp
);
2039 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2040 INT_GET(agf
->agf_seqno
, ARCH_CONVERT
), &agflbp
)))
2042 agfl
= XFS_BUF_TO_AGFL(agflbp
);
2043 INT_MOD(agf
->agf_fllast
, ARCH_CONVERT
, 1);
2044 if (INT_GET(agf
->agf_fllast
, ARCH_CONVERT
) == XFS_AGFL_SIZE(mp
))
2045 INT_ZERO(agf
->agf_fllast
, ARCH_CONVERT
);
2046 pag
= &mp
->m_perag
[INT_GET(agf
->agf_seqno
, ARCH_CONVERT
)];
2047 INT_MOD(agf
->agf_flcount
, ARCH_CONVERT
, 1);
2048 xfs_trans_agflist_delta(tp
, 1);
2049 pag
->pagf_flcount
++;
2050 ASSERT(INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) <= XFS_AGFL_SIZE(mp
));
2051 blockp
= &agfl
->agfl_bno
[INT_GET(agf
->agf_fllast
, ARCH_CONVERT
)];
2052 INT_SET(*blockp
, ARCH_CONVERT
, bno
);
2053 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
);
2054 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
);
2055 xfs_trans_log_buf(tp
, agflbp
,
2056 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
),
2057 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
+
2058 sizeof(xfs_agblock_t
) - 1));
2063 * Read in the allocation group header (free/alloc section).
2067 xfs_mount_t
*mp
, /* mount point structure */
2068 xfs_trans_t
*tp
, /* transaction pointer */
2069 xfs_agnumber_t agno
, /* allocation group number */
2070 int flags
, /* XFS_ALLOC_FLAG_... */
2071 xfs_buf_t
**bpp
) /* buffer for the ag freelist header */
2073 xfs_agf_t
*agf
; /* ag freelist header */
2074 int agf_ok
; /* set if agf is consistent */
2075 xfs_buf_t
*bp
; /* return value */
2076 xfs_perag_t
*pag
; /* per allocation group data */
2079 ASSERT(agno
!= NULLAGNUMBER
);
2080 error
= xfs_trans_read_buf(
2081 mp
, tp
, mp
->m_ddev_targp
,
2082 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2083 XFS_FSS_TO_BB(mp
, 1),
2084 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XFS_BUF_TRYLOCK
: 0U,
2088 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
2094 * Validate the magic number of the agf block.
2096 agf
= XFS_BUF_TO_AGF(bp
);
2098 INT_GET(agf
->agf_magicnum
, ARCH_CONVERT
) == XFS_AGF_MAGIC
&&
2099 XFS_AGF_GOOD_VERSION(
2100 INT_GET(agf
->agf_versionnum
, ARCH_CONVERT
)) &&
2101 INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
) <=
2102 INT_GET(agf
->agf_length
, ARCH_CONVERT
) &&
2103 INT_GET(agf
->agf_flfirst
, ARCH_CONVERT
) < XFS_AGFL_SIZE(mp
) &&
2104 INT_GET(agf
->agf_fllast
, ARCH_CONVERT
) < XFS_AGFL_SIZE(mp
) &&
2105 INT_GET(agf
->agf_flcount
, ARCH_CONVERT
) <= XFS_AGFL_SIZE(mp
);
2106 if (unlikely(XFS_TEST_ERROR(!agf_ok
, mp
, XFS_ERRTAG_ALLOC_READ_AGF
,
2107 XFS_RANDOM_ALLOC_READ_AGF
))) {
2108 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2109 XFS_ERRLEVEL_LOW
, mp
, agf
);
2110 xfs_trans_brelse(tp
, bp
);
2111 return XFS_ERROR(EFSCORRUPTED
);
2113 pag
= &mp
->m_perag
[agno
];
2114 if (!pag
->pagf_init
) {
2115 pag
->pagf_freeblks
= INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
);
2116 pag
->pagf_flcount
= INT_GET(agf
->agf_flcount
, ARCH_CONVERT
);
2117 pag
->pagf_longest
= INT_GET(agf
->agf_longest
, ARCH_CONVERT
);
2118 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2119 INT_GET(agf
->agf_levels
[XFS_BTNUM_BNOi
], ARCH_CONVERT
);
2120 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2121 INT_GET(agf
->agf_levels
[XFS_BTNUM_CNTi
], ARCH_CONVERT
);
2122 spinlock_init(&pag
->pagb_lock
, "xfspagb");
2123 pag
->pagb_list
= kmem_zalloc(XFS_PAGB_NUM_SLOTS
*
2124 sizeof(xfs_perag_busy_t
), KM_SLEEP
);
2128 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2129 ASSERT(pag
->pagf_freeblks
== INT_GET(agf
->agf_freeblks
, ARCH_CONVERT
));
2130 ASSERT(pag
->pagf_flcount
== INT_GET(agf
->agf_flcount
, ARCH_CONVERT
));
2131 ASSERT(pag
->pagf_longest
== INT_GET(agf
->agf_longest
, ARCH_CONVERT
));
2132 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2133 INT_GET(agf
->agf_levels
[XFS_BTNUM_BNOi
], ARCH_CONVERT
));
2134 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2135 INT_GET(agf
->agf_levels
[XFS_BTNUM_CNTi
], ARCH_CONVERT
));
2138 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGF
, XFS_AGF_REF
);
2144 * Allocate an extent (variable-size).
2145 * Depending on the allocation type, we either look in a single allocation
2146 * group or loop over the allocation groups to find the result.
2150 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2152 xfs_agblock_t agsize
; /* allocation group size */
2154 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2155 #ifdef XFS_ALLOC_TRACE
2156 static char fname
[] = "xfs_alloc_vextent";
2158 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2159 xfs_mount_t
*mp
; /* mount structure pointer */
2160 xfs_agnumber_t sagno
; /* starting allocation group number */
2161 xfs_alloctype_t type
; /* input allocation type */
2166 type
= args
->otype
= args
->type
;
2167 args
->agbno
= NULLAGBLOCK
;
2169 * Just fix this up, for the case where the last a.g. is shorter
2170 * (or there's only one a.g.) and the caller couldn't easily figure
2171 * that out (xfs_bmap_alloc).
2173 agsize
= mp
->m_sb
.sb_agblocks
;
2174 if (args
->maxlen
> agsize
)
2175 args
->maxlen
= agsize
;
2176 if (args
->alignment
== 0)
2177 args
->alignment
= 1;
2178 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2179 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2180 ASSERT(args
->minlen
<= args
->maxlen
);
2181 ASSERT(args
->minlen
<= agsize
);
2182 ASSERT(args
->mod
< args
->prod
);
2183 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2184 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2185 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2186 args
->mod
>= args
->prod
) {
2187 args
->fsbno
= NULLFSBLOCK
;
2188 TRACE_ALLOC("badargs", args
);
2191 minleft
= args
->minleft
;
2194 case XFS_ALLOCTYPE_THIS_AG
:
2195 case XFS_ALLOCTYPE_NEAR_BNO
:
2196 case XFS_ALLOCTYPE_THIS_BNO
:
2198 * These three force us into a single a.g.
2200 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2201 down_read(&mp
->m_peraglock
);
2202 args
->pag
= &mp
->m_perag
[args
->agno
];
2204 error
= xfs_alloc_fix_freelist(args
, 0);
2205 args
->minleft
= minleft
;
2207 TRACE_ALLOC("nofix", args
);
2211 up_read(&mp
->m_peraglock
);
2212 TRACE_ALLOC("noagbp", args
);
2215 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2216 if ((error
= xfs_alloc_ag_vextent(args
)))
2218 up_read(&mp
->m_peraglock
);
2220 case XFS_ALLOCTYPE_START_BNO
:
2222 * Try near allocation first, then anywhere-in-ag after
2223 * the first a.g. fails.
2225 if ((args
->userdata
== XFS_ALLOC_INITIAL_USER_DATA
) &&
2226 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2227 args
->fsbno
= XFS_AGB_TO_FSB(mp
, mp
->m_agfrotor
, 0);
2230 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2231 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2233 case XFS_ALLOCTYPE_ANY_AG
:
2234 case XFS_ALLOCTYPE_START_AG
:
2235 case XFS_ALLOCTYPE_FIRST_AG
:
2237 * Rotate through the allocation groups looking for a winner.
2239 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2241 * Start with the last place we left off.
2243 args
->agno
= sagno
= mp
->m_agfrotor
;
2244 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2245 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2246 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2248 * Start with allocation group given by bno.
2250 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2251 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2255 if (type
== XFS_ALLOCTYPE_START_AG
)
2256 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2258 * Start with the given allocation group.
2260 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2261 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2264 * Loop over allocation groups twice; first time with
2265 * trylock set, second time without.
2267 down_read(&mp
->m_peraglock
);
2269 args
->pag
= &mp
->m_perag
[args
->agno
];
2270 if (no_min
) args
->minleft
= 0;
2271 error
= xfs_alloc_fix_freelist(args
, flags
);
2272 args
->minleft
= minleft
;
2274 TRACE_ALLOC("nofix", args
);
2278 * If we get a buffer back then the allocation will fly.
2281 if ((error
= xfs_alloc_ag_vextent(args
)))
2285 TRACE_ALLOC("loopfailed", args
);
2287 * Didn't work, figure out the next iteration.
2289 if (args
->agno
== sagno
&&
2290 type
== XFS_ALLOCTYPE_START_BNO
)
2291 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2292 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
)
2295 * Reached the starting a.g., must either be done
2296 * or switch to non-trylock mode.
2298 if (args
->agno
== sagno
) {
2300 args
->agbno
= NULLAGBLOCK
;
2301 TRACE_ALLOC("allfailed", args
);
2308 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2309 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2311 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2316 up_read(&mp
->m_peraglock
);
2317 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
))
2318 mp
->m_agfrotor
= (args
->agno
+ 1) % mp
->m_sb
.sb_agcount
;
2324 if (args
->agbno
== NULLAGBLOCK
)
2325 args
->fsbno
= NULLFSBLOCK
;
2327 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2329 ASSERT(args
->len
>= args
->minlen
);
2330 ASSERT(args
->len
<= args
->maxlen
);
2331 ASSERT(args
->agbno
% args
->alignment
== 0);
2332 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2338 up_read(&mp
->m_peraglock
);
2344 * Just break up the extent address and hand off to xfs_free_ag_extent
2345 * after fixing up the freelist.
2349 xfs_trans_t
*tp
, /* transaction pointer */
2350 xfs_fsblock_t bno
, /* starting block number of extent */
2351 xfs_extlen_t len
) /* length of extent */
2354 xfs_agf_t
*agf
; /* a.g. freespace header */
2356 xfs_alloc_arg_t args
; /* allocation argument structure */
2361 args
.mp
= tp
->t_mountp
;
2362 args
.agno
= XFS_FSB_TO_AGNO(args
.mp
, bno
);
2363 ASSERT(args
.agno
< args
.mp
->m_sb
.sb_agcount
);
2364 args
.agbno
= XFS_FSB_TO_AGBNO(args
.mp
, bno
);
2366 args
.minlen
= args
.minleft
= args
.minalignslop
= 0;
2367 down_read(&args
.mp
->m_peraglock
);
2368 args
.pag
= &args
.mp
->m_perag
[args
.agno
];
2369 if ((error
= xfs_alloc_fix_freelist(&args
, 0)))
2372 ASSERT(args
.agbp
!= NULL
);
2373 agf
= XFS_BUF_TO_AGF(args
.agbp
);
2374 ASSERT(args
.agbno
+ len
<= INT_GET(agf
->agf_length
, ARCH_CONVERT
));
2376 error
= xfs_free_ag_extent(tp
, args
.agbp
, args
.agno
, args
.agbno
,
2379 up_read(&args
.mp
->m_peraglock
);