2 * Copyright (c) 2000-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
21 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
22 #define XFSA_FIXUP_BNO_OK 1
23 #define XFSA_FIXUP_CNT_OK 2
26 * Compute aligned version of the found extent.
27 * Takes alignment and min length into account.
29 STATIC
int /* success (>= minlen) */
30 xfs_alloc_compute_aligned(
31 xfs_agblock_t foundbno
, /* starting block in found extent */
32 xfs_extlen_t foundlen
, /* length in found extent */
33 xfs_extlen_t alignment
, /* alignment for allocation */
34 xfs_extlen_t minlen
, /* minimum length for allocation */
35 xfs_agblock_t
*resbno
, /* result block number */
36 xfs_extlen_t
*reslen
) /* result length */
42 if (alignment
> 1 && foundlen
>= minlen
) {
43 bno
= roundup(foundbno
, alignment
);
44 diff
= bno
- foundbno
;
45 len
= diff
>= foundlen
? 0 : foundlen
- diff
;
56 * Compute best start block and diff for "near" allocations.
57 * freelen >= wantlen already checked by caller.
59 STATIC xfs_extlen_t
/* difference value (absolute) */
60 xfs_alloc_compute_diff(
61 xfs_agblock_t wantbno
, /* target starting block */
62 xfs_extlen_t wantlen
, /* target length */
63 xfs_extlen_t alignment
, /* target alignment */
64 xfs_agblock_t freebno
, /* freespace's starting block */
65 xfs_extlen_t freelen
, /* freespace's length */
66 xfs_agblock_t
*newbnop
) /* result: best start block from free */
68 xfs_agblock_t freeend
; /* end of freespace extent */
69 xfs_agblock_t newbno1
; /* return block number */
70 xfs_agblock_t newbno2
; /* other new block number */
71 xfs_extlen_t newlen1
=0; /* length with newbno1 */
72 xfs_extlen_t newlen2
=0; /* length with newbno2 */
73 xfs_agblock_t wantend
; /* end of target extent */
75 ASSERT(freelen
>= wantlen
);
76 freeend
= freebno
+ freelen
;
77 wantend
= wantbno
+ wantlen
;
78 if (freebno
>= wantbno
) {
79 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
80 newbno1
= NULLAGBLOCK
;
81 } else if (freeend
>= wantend
&& alignment
> 1) {
82 newbno1
= roundup(wantbno
, alignment
);
83 newbno2
= newbno1
- alignment
;
84 if (newbno1
>= freeend
)
85 newbno1
= NULLAGBLOCK
;
87 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
88 if (newbno2
< freebno
)
89 newbno2
= NULLAGBLOCK
;
91 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
92 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
93 if (newlen1
< newlen2
||
94 (newlen1
== newlen2
&&
95 XFS_ABSDIFF(newbno1
, wantbno
) >
96 XFS_ABSDIFF(newbno2
, wantbno
)))
98 } else if (newbno2
!= NULLAGBLOCK
)
100 } else if (freeend
>= wantend
) {
102 } else if (alignment
> 1) {
103 newbno1
= roundup(freeend
- wantlen
, alignment
);
104 if (newbno1
> freeend
- wantlen
&&
105 newbno1
- alignment
>= freebno
)
106 newbno1
-= alignment
;
107 else if (newbno1
>= freeend
)
108 newbno1
= NULLAGBLOCK
;
110 newbno1
= freeend
- wantlen
;
112 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
116 * Fix up the length, based on mod and prod.
117 * len should be k * prod + mod for some k.
118 * If len is too small it is returned unchanged.
119 * If len hits maxlen it is left alone.
123 xfs_alloc_arg_t
*args
) /* allocation argument structure */
128 ASSERT(args
->mod
< args
->prod
);
130 ASSERT(rlen
>= args
->minlen
);
131 ASSERT(rlen
<= args
->maxlen
);
132 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
133 (args
->mod
== 0 && rlen
< args
->prod
))
135 k
= rlen
% args
->prod
;
139 if ((int)(rlen
= rlen
- k
- args
->mod
) < (int)args
->minlen
)
142 if ((int)(rlen
= rlen
- args
->prod
- (args
->mod
- k
)) <
146 ASSERT(rlen
>= args
->minlen
);
147 ASSERT(rlen
<= args
->maxlen
);
152 * Fix up length if there is too little space left in the a.g.
153 * Return 1 if ok, 0 if too little, should give up.
156 xfs_alloc_fix_minleft(
157 xfs_alloc_arg_t
*args
) /* allocation argument structure */
159 xfs_agf_t
*agf
; /* a.g. freelist header */
160 int diff
; /* free space difference */
162 if (args
->minleft
== 0)
164 agf
= XFS_BUF_TO_AGF(args
->agbp
);
165 diff
= be32_to_cpu(agf
->agf_freeblks
)
166 + be32_to_cpu(agf
->agf_flcount
)
167 - args
->len
- args
->minleft
;
170 args
->len
+= diff
; /* shrink the allocated space */
171 if (args
->len
>= args
->minlen
)
173 args
->agbno
= NULLAGBLOCK
;
178 * Update the two btrees, logically removing from freespace the extent
179 * starting at rbno, rlen blocks. The extent is contained within the
180 * actual (current) free extent fbno for flen blocks.
181 * Flags are passed in indicating whether the cursors are set to the
184 STATIC
int /* error code */
185 xfs_alloc_fixup_trees(
186 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
187 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
188 xfs_agblock_t fbno
, /* starting block of free extent */
189 xfs_extlen_t flen
, /* length of free extent */
190 xfs_agblock_t rbno
, /* starting block of returned extent */
191 xfs_extlen_t rlen
, /* length of returned extent */
192 int flags
) /* flags, XFSA_FIXUP_... */
194 int error
; /* error code */
195 int i
; /* operation results */
196 xfs_agblock_t nfbno1
; /* first new free startblock */
197 xfs_agblock_t nfbno2
; /* second new free startblock */
198 xfs_extlen_t nflen1
=0; /* first new free length */
199 xfs_extlen_t nflen2
=0; /* second new free length */
202 * Look up the record in the by-size tree if necessary.
204 if (flags
& XFSA_FIXUP_CNT_OK
) {
206 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
208 XFS_WANT_CORRUPTED_RETURN(
209 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
212 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
214 XFS_WANT_CORRUPTED_RETURN(i
== 1);
217 * Look up the record in the by-block tree if necessary.
219 if (flags
& XFSA_FIXUP_BNO_OK
) {
221 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
223 XFS_WANT_CORRUPTED_RETURN(
224 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
227 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
229 XFS_WANT_CORRUPTED_RETURN(i
== 1);
233 xfs_alloc_block_t
*bnoblock
;
234 xfs_alloc_block_t
*cntblock
;
236 if (bno_cur
->bc_nlevels
== 1 &&
237 cnt_cur
->bc_nlevels
== 1) {
238 bnoblock
= XFS_BUF_TO_ALLOC_BLOCK(bno_cur
->bc_bufs
[0]);
239 cntblock
= XFS_BUF_TO_ALLOC_BLOCK(cnt_cur
->bc_bufs
[0]);
240 XFS_WANT_CORRUPTED_RETURN(
241 be16_to_cpu(bnoblock
->bb_numrecs
) ==
242 be16_to_cpu(cntblock
->bb_numrecs
));
247 * Deal with all four cases: the allocated record is contained
248 * within the freespace record, so we can have new freespace
249 * at either (or both) end, or no freespace remaining.
251 if (rbno
== fbno
&& rlen
== flen
)
252 nfbno1
= nfbno2
= NULLAGBLOCK
;
253 else if (rbno
== fbno
) {
254 nfbno1
= rbno
+ rlen
;
255 nflen1
= flen
- rlen
;
256 nfbno2
= NULLAGBLOCK
;
257 } else if (rbno
+ rlen
== fbno
+ flen
) {
259 nflen1
= flen
- rlen
;
260 nfbno2
= NULLAGBLOCK
;
263 nflen1
= rbno
- fbno
;
264 nfbno2
= rbno
+ rlen
;
265 nflen2
= (fbno
+ flen
) - nfbno2
;
268 * Delete the entry from the by-size btree.
270 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
272 XFS_WANT_CORRUPTED_RETURN(i
== 1);
274 * Add new by-size btree entry(s).
276 if (nfbno1
!= NULLAGBLOCK
) {
277 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
279 XFS_WANT_CORRUPTED_RETURN(i
== 0);
280 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
282 XFS_WANT_CORRUPTED_RETURN(i
== 1);
284 if (nfbno2
!= NULLAGBLOCK
) {
285 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
287 XFS_WANT_CORRUPTED_RETURN(i
== 0);
288 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
290 XFS_WANT_CORRUPTED_RETURN(i
== 1);
293 * Fix up the by-block btree entry(s).
295 if (nfbno1
== NULLAGBLOCK
) {
297 * No remaining freespace, just delete the by-block tree entry.
299 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
301 XFS_WANT_CORRUPTED_RETURN(i
== 1);
304 * Update the by-block entry to start later|be shorter.
306 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
309 if (nfbno2
!= NULLAGBLOCK
) {
311 * 2 resulting free entries, need to add one.
313 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
315 XFS_WANT_CORRUPTED_RETURN(i
== 0);
316 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
318 XFS_WANT_CORRUPTED_RETURN(i
== 1);
324 * Read in the allocation group free block array.
326 STATIC
int /* error */
328 xfs_mount_t
*mp
, /* mount point structure */
329 xfs_trans_t
*tp
, /* transaction pointer */
330 xfs_agnumber_t agno
, /* allocation group number */
331 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
333 xfs_buf_t
*bp
; /* return value */
336 ASSERT(agno
!= NULLAGNUMBER
);
337 error
= xfs_trans_read_buf(
338 mp
, tp
, mp
->m_ddev_targp
,
339 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
340 XFS_FSS_TO_BB(mp
, 1), 0, &bp
);
344 ASSERT(!XFS_BUF_GETERROR(bp
));
345 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGFL
, XFS_AGFL_REF
);
351 //#if defined(XFS_ALLOC_TRACE)
353 * Add an allocation trace entry for an alloc call.
356 xfs_alloc_trace_alloc(
357 char *name
, /* function tag string */
358 char *str
, /* additional string */
359 xfs_alloc_arg_t
*args
, /* allocation argument structure */
360 int line
) /* source line number */
362 ktrace_enter(xfs_alloc_trace_buf
,
363 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_ALLOC
| (line
<< 16)),
367 (void *)(__psunsigned_t
)args
->agno
,
368 (void *)(__psunsigned_t
)args
->agbno
,
369 (void *)(__psunsigned_t
)args
->minlen
,
370 (void *)(__psunsigned_t
)args
->maxlen
,
371 (void *)(__psunsigned_t
)args
->mod
,
372 (void *)(__psunsigned_t
)args
->prod
,
373 (void *)(__psunsigned_t
)args
->minleft
,
374 (void *)(__psunsigned_t
)args
->total
,
375 (void *)(__psunsigned_t
)args
->alignment
,
376 (void *)(__psunsigned_t
)args
->len
,
377 (void *)((((__psint_t
)args
->type
) << 16) |
378 (__psint_t
)args
->otype
),
379 (void *)(__psint_t
)((args
->wasdel
<< 3) |
380 (args
->wasfromfl
<< 2) |
382 (args
->userdata
<< 0)));
386 * Add an allocation trace entry for a free call.
389 xfs_alloc_trace_free(
390 char *name
, /* function tag string */
391 char *str
, /* additional string */
392 xfs_mount_t
*mp
, /* file system mount point */
393 xfs_agnumber_t agno
, /* allocation group number */
394 xfs_agblock_t agbno
, /* a.g. relative block number */
395 xfs_extlen_t len
, /* length of extent */
396 int isfl
, /* set if is freelist allocation/free */
397 int line
) /* source line number */
399 ktrace_enter(xfs_alloc_trace_buf
,
400 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_FREE
| (line
<< 16)),
404 (void *)(__psunsigned_t
)agno
,
405 (void *)(__psunsigned_t
)agbno
,
406 (void *)(__psunsigned_t
)len
,
407 (void *)(__psint_t
)isfl
,
408 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
412 * Add an allocation trace entry for modifying an agf.
415 xfs_alloc_trace_modagf(
416 char *name
, /* function tag string */
417 char *str
, /* additional string */
418 xfs_mount_t
*mp
, /* file system mount point */
419 xfs_agf_t
*agf
, /* new agf value */
420 int flags
, /* logging flags for agf */
421 int line
) /* source line number */
423 ktrace_enter(xfs_alloc_trace_buf
,
424 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_MODAGF
| (line
<< 16)),
428 (void *)(__psint_t
)flags
,
429 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_seqno
).
430 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_length
),
431 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]),
432 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]).
433 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]).
434 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]).
435 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_flfirst
),
436 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_fllast
),
437 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_flcount
),
438 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_freeblks
),
439 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_longest
));
441 #endif /* XFS_ALLOC_TRACE */
444 * Allocation group level functions.
448 * Allocate a variable extent in the allocation group agno.
449 * Type and bno are used to determine where in the allocation group the
451 * Extent's length (returned in *len) will be between minlen and maxlen,
452 * and of the form k * prod + mod unless there's nothing that large.
453 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
455 STATIC
int /* error */
456 xfs_alloc_ag_vextent(
457 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
460 #ifdef XFS_ALLOC_TRACE
461 static char fname
[] = "xfs_alloc_ag_vextent";
464 ASSERT(args
->minlen
> 0);
465 ASSERT(args
->maxlen
> 0);
466 ASSERT(args
->minlen
<= args
->maxlen
);
467 ASSERT(args
->mod
< args
->prod
);
468 ASSERT(args
->alignment
> 0);
470 * Branch to correct routine based on the type.
473 switch (args
->type
) {
474 case XFS_ALLOCTYPE_THIS_AG
:
475 error
= xfs_alloc_ag_vextent_size(args
);
477 case XFS_ALLOCTYPE_NEAR_BNO
:
478 error
= xfs_alloc_ag_vextent_near(args
);
480 case XFS_ALLOCTYPE_THIS_BNO
:
481 error
= xfs_alloc_ag_vextent_exact(args
);
490 * If the allocation worked, need to change the agf structure
491 * (and log it), and the superblock.
493 if (args
->agbno
!= NULLAGBLOCK
) {
494 xfs_agf_t
*agf
; /* allocation group freelist header */
495 #ifdef XFS_ALLOC_TRACE
496 xfs_mount_t
*mp
= args
->mp
;
498 long slen
= (long)args
->len
;
500 ASSERT(args
->len
>= args
->minlen
&& args
->len
<= args
->maxlen
);
501 ASSERT(!(args
->wasfromfl
) || !args
->isfl
);
502 ASSERT(args
->agbno
% args
->alignment
== 0);
503 if (!(args
->wasfromfl
)) {
505 agf
= XFS_BUF_TO_AGF(args
->agbp
);
506 be32_add(&agf
->agf_freeblks
, -(args
->len
));
507 xfs_trans_agblocks_delta(args
->tp
,
508 -((long)(args
->len
)));
509 args
->pag
->pagf_freeblks
-= args
->len
;
510 ASSERT(be32_to_cpu(agf
->agf_freeblks
) <=
511 be32_to_cpu(agf
->agf_length
));
512 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
513 xfs_alloc_log_agf(args
->tp
, args
->agbp
,
515 /* search the busylist for these blocks */
516 xfs_alloc_search_busy(args
->tp
, args
->agno
,
517 args
->agbno
, args
->len
);
520 xfs_trans_mod_sb(args
->tp
,
521 args
->wasdel
? XFS_TRANS_SB_RES_FDBLOCKS
:
522 XFS_TRANS_SB_FDBLOCKS
, -slen
);
523 XFS_STATS_INC(xs_allocx
);
524 XFS_STATS_ADD(xs_allocb
, args
->len
);
530 * Allocate a variable extent at exactly agno/bno.
531 * Extent's length (returned in *len) will be between minlen and maxlen,
532 * and of the form k * prod + mod unless there's nothing that large.
533 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
535 STATIC
int /* error */
536 xfs_alloc_ag_vextent_exact(
537 xfs_alloc_arg_t
*args
) /* allocation argument structure */
539 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
540 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
541 xfs_agblock_t end
; /* end of allocated extent */
543 xfs_agblock_t fbno
; /* start block of found extent */
544 xfs_agblock_t fend
; /* end block of found extent */
545 xfs_extlen_t flen
; /* length of found extent */
546 #ifdef XFS_ALLOC_TRACE
547 static char fname
[] = "xfs_alloc_ag_vextent_exact";
549 int i
; /* success/failure of operation */
550 xfs_agblock_t maxend
; /* end of maximal extent */
551 xfs_agblock_t minend
; /* end of minimal extent */
552 xfs_extlen_t rlen
; /* length of returned extent */
554 ASSERT(args
->alignment
== 1);
556 * Allocate/initialize a cursor for the by-number freespace btree.
558 bno_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
559 args
->agno
, XFS_BTNUM_BNO
, NULL
, 0);
561 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
562 * Look for the closest free block <= bno, it must contain bno
563 * if any free block does.
565 if ((error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
)))
569 * Didn't find it, return null.
571 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
572 args
->agbno
= NULLAGBLOCK
;
576 * Grab the freespace record.
578 if ((error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
)))
580 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
581 ASSERT(fbno
<= args
->agbno
);
582 minend
= args
->agbno
+ args
->minlen
;
583 maxend
= args
->agbno
+ args
->maxlen
;
586 * Give up if the freespace isn't long enough for the minimum request.
589 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
590 args
->agbno
= NULLAGBLOCK
;
594 * End of extent will be smaller of the freespace end and the
595 * maximal requested end.
597 end
= XFS_AGBLOCK_MIN(fend
, maxend
);
599 * Fix the length according to mod and prod if given.
601 args
->len
= end
- args
->agbno
;
602 xfs_alloc_fix_len(args
);
603 if (!xfs_alloc_fix_minleft(args
)) {
604 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
608 ASSERT(args
->agbno
+ rlen
<= fend
);
609 end
= args
->agbno
+ rlen
;
611 * We are allocating agbno for rlen [agbno .. end]
612 * Allocate/initialize a cursor for the by-size btree.
614 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
615 args
->agno
, XFS_BTNUM_CNT
, NULL
, 0);
616 ASSERT(args
->agbno
+ args
->len
<=
617 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
618 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
619 args
->agbno
, args
->len
, XFSA_FIXUP_BNO_OK
))) {
620 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
623 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
624 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
625 TRACE_ALLOC("normal", args
);
630 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
631 TRACE_ALLOC("error", args
);
636 * Allocate a variable extent near bno in the allocation group agno.
637 * Extent's length (returned in len) will be between minlen and maxlen,
638 * and of the form k * prod + mod unless there's nothing that large.
639 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
641 STATIC
int /* error */
642 xfs_alloc_ag_vextent_near(
643 xfs_alloc_arg_t
*args
) /* allocation argument structure */
645 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
646 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
647 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
648 #ifdef XFS_ALLOC_TRACE
649 static char fname
[] = "xfs_alloc_ag_vextent_near";
651 xfs_agblock_t gtbno
; /* start bno of right side entry */
652 xfs_agblock_t gtbnoa
; /* aligned ... */
653 xfs_extlen_t gtdiff
; /* difference to right side entry */
654 xfs_extlen_t gtlen
; /* length of right side entry */
655 xfs_extlen_t gtlena
; /* aligned ... */
656 xfs_agblock_t gtnew
; /* useful start bno of right side */
657 int error
; /* error code */
658 int i
; /* result code, temporary */
659 int j
; /* result code, temporary */
660 xfs_agblock_t ltbno
; /* start bno of left side entry */
661 xfs_agblock_t ltbnoa
; /* aligned ... */
662 xfs_extlen_t ltdiff
; /* difference to left side entry */
664 xfs_agblock_t ltend
; /* end bno of left side entry */
665 xfs_extlen_t ltlen
; /* length of left side entry */
666 xfs_extlen_t ltlena
; /* aligned ... */
667 xfs_agblock_t ltnew
; /* useful start bno of left side */
668 xfs_extlen_t rlen
; /* length of returned extent */
669 #if defined(DEBUG) && defined(__KERNEL__)
671 * Randomly don't execute the first algorithm.
673 int dofirst
; /* set to do first algorithm */
675 dofirst
= random() & 1;
678 * Get a cursor for the by-size btree.
680 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
681 args
->agno
, XFS_BTNUM_CNT
, NULL
, 0);
683 bno_cur_lt
= bno_cur_gt
= NULL
;
685 * See if there are any free extents as big as maxlen.
687 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
690 * If none, then pick up the last entry in the tree unless the
694 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
697 if (i
== 0 || ltlen
== 0) {
698 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
706 * If the requested extent is large wrt the freespaces available
707 * in this a.g., then the cursor will be pointing to a btree entry
708 * near the right edge of the tree. If it's in the last btree leaf
709 * block, then we just examine all the entries in that block
710 * that are big enough, and pick the best one.
711 * This is written as a while loop so we can break out of it,
712 * but we never loop back to the top.
714 while (xfs_btree_islastblock(cnt_cur
, 0)) {
718 xfs_agblock_t bnew
=0;
720 #if defined(DEBUG) && defined(__KERNEL__)
725 * Start from the entry that lookup found, sequence through
726 * all larger free blocks. If we're actually pointing at a
727 * record smaller than maxlen, go to the start of this block,
728 * and skip all those smaller than minlen.
730 if (ltlen
|| args
->alignment
> 1) {
731 cnt_cur
->bc_ptrs
[0] = 1;
733 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
736 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
737 if (ltlen
>= args
->minlen
)
739 if ((error
= xfs_alloc_increment(cnt_cur
, 0, &i
)))
742 ASSERT(ltlen
>= args
->minlen
);
746 i
= cnt_cur
->bc_ptrs
[0];
747 for (j
= 1, blen
= 0, bdiff
= 0;
748 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
749 error
= xfs_alloc_increment(cnt_cur
, 0, &j
)) {
751 * For each entry, decide if it's better than
752 * the previous best entry.
754 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
756 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
757 if (!xfs_alloc_compute_aligned(ltbno
, ltlen
,
758 args
->alignment
, args
->minlen
,
761 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
762 xfs_alloc_fix_len(args
);
763 ASSERT(args
->len
>= args
->minlen
);
764 if (args
->len
< blen
)
766 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
767 args
->alignment
, ltbno
, ltlen
, <new
);
768 if (ltnew
!= NULLAGBLOCK
&&
769 (args
->len
> blen
|| ltdiff
< bdiff
)) {
773 besti
= cnt_cur
->bc_ptrs
[0];
777 * It didn't work. We COULD be in a case where
778 * there's a good record somewhere, so try again.
783 * Point at the best entry, and retrieve it again.
785 cnt_cur
->bc_ptrs
[0] = besti
;
786 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
788 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
789 ltend
= ltbno
+ ltlen
;
790 ASSERT(ltend
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
792 if (!xfs_alloc_fix_minleft(args
)) {
793 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
794 TRACE_ALLOC("nominleft", args
);
799 * We are allocating starting at bnew for blen blocks.
802 ASSERT(bnew
>= ltbno
);
803 ASSERT(bnew
+ blen
<= ltend
);
805 * Set up a cursor for the by-bno tree.
807 bno_cur_lt
= xfs_btree_init_cursor(args
->mp
, args
->tp
,
808 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
, NULL
, 0);
810 * Fix up the btree entries.
812 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
813 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
815 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
816 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
817 TRACE_ALLOC("first", args
);
822 * Search in the by-bno tree to the left and to the right
823 * simultaneously, until in each case we find a space big enough,
824 * or run into the edge of the tree. When we run into the edge,
825 * we deallocate that cursor.
826 * If both searches succeed, we compare the two spaces and pick
828 * With alignment, it's possible for both to fail; the upper
829 * level algorithm that picks allocation groups for allocations
830 * is not supposed to do this.
833 * Allocate and initialize the cursor for the leftward search.
835 bno_cur_lt
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
836 args
->agno
, XFS_BTNUM_BNO
, NULL
, 0);
838 * Lookup <= bno to find the leftward search's starting point.
840 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
844 * Didn't find anything; use this cursor for the rightward
847 bno_cur_gt
= bno_cur_lt
;
851 * Found something. Duplicate the cursor for the rightward search.
853 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
856 * Increment the cursor, so we will point at the entry just right
857 * of the leftward entry if any, or to the leftmost entry.
859 if ((error
= xfs_alloc_increment(bno_cur_gt
, 0, &i
)))
863 * It failed, there are no rightward entries.
865 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
869 * Loop going left with the leftward cursor, right with the
870 * rightward cursor, until either both directions give up or
871 * we find an entry at least as big as minlen.
875 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
877 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
878 if (xfs_alloc_compute_aligned(ltbno
, ltlen
,
879 args
->alignment
, args
->minlen
,
882 if ((error
= xfs_alloc_decrement(bno_cur_lt
, 0, &i
)))
885 xfs_btree_del_cursor(bno_cur_lt
,
891 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
893 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
894 if (xfs_alloc_compute_aligned(gtbno
, gtlen
,
895 args
->alignment
, args
->minlen
,
898 if ((error
= xfs_alloc_increment(bno_cur_gt
, 0, &i
)))
901 xfs_btree_del_cursor(bno_cur_gt
,
906 } while (bno_cur_lt
|| bno_cur_gt
);
908 * Got both cursors still active, need to find better entry.
910 if (bno_cur_lt
&& bno_cur_gt
) {
912 * Left side is long enough, look for a right side entry.
914 if (ltlena
>= args
->minlen
) {
918 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
919 xfs_alloc_fix_len(args
);
921 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
922 args
->alignment
, ltbno
, ltlen
, <new
);
928 * Look until we find a better one, run out of
929 * space, or run off the end.
931 while (bno_cur_lt
&& bno_cur_gt
) {
932 if ((error
= xfs_alloc_get_rec(
936 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
937 xfs_alloc_compute_aligned(gtbno
, gtlen
,
938 args
->alignment
, args
->minlen
,
941 * The left one is clearly better.
943 if (gtbnoa
>= args
->agbno
+ ltdiff
) {
944 xfs_btree_del_cursor(
951 * If we reach a big enough entry,
952 * compare the two and pick the best.
954 if (gtlena
>= args
->minlen
) {
956 XFS_EXTLEN_MIN(gtlena
,
958 xfs_alloc_fix_len(args
);
960 gtdiff
= xfs_alloc_compute_diff(
963 gtbno
, gtlen
, >new
);
965 * Right side is better.
967 if (gtdiff
< ltdiff
) {
968 xfs_btree_del_cursor(
974 * Left side is better.
977 xfs_btree_del_cursor(
985 * Fell off the right end.
987 if ((error
= xfs_alloc_increment(
991 xfs_btree_del_cursor(
1000 * The left side is perfect, trash the right side.
1003 xfs_btree_del_cursor(bno_cur_gt
,
1009 * It's the right side that was found first, look left.
1013 * Fix up the length.
1015 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1016 xfs_alloc_fix_len(args
);
1018 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
1019 args
->alignment
, gtbno
, gtlen
, >new
);
1021 * Right side entry isn't perfect.
1025 * Look until we find a better one, run out of
1026 * space, or run off the end.
1028 while (bno_cur_lt
&& bno_cur_gt
) {
1029 if ((error
= xfs_alloc_get_rec(
1033 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1034 xfs_alloc_compute_aligned(ltbno
, ltlen
,
1035 args
->alignment
, args
->minlen
,
1038 * The right one is clearly better.
1040 if (ltbnoa
<= args
->agbno
- gtdiff
) {
1041 xfs_btree_del_cursor(
1048 * If we reach a big enough entry,
1049 * compare the two and pick the best.
1051 if (ltlena
>= args
->minlen
) {
1052 args
->len
= XFS_EXTLEN_MIN(
1053 ltlena
, args
->maxlen
);
1054 xfs_alloc_fix_len(args
);
1056 ltdiff
= xfs_alloc_compute_diff(
1059 ltbno
, ltlen
, <new
);
1061 * Left side is better.
1063 if (ltdiff
< gtdiff
) {
1064 xfs_btree_del_cursor(
1070 * Right side is better.
1073 xfs_btree_del_cursor(
1081 * Fell off the left end.
1083 if ((error
= xfs_alloc_decrement(
1084 bno_cur_lt
, 0, &i
)))
1087 xfs_btree_del_cursor(bno_cur_lt
,
1095 * The right side is perfect, trash the left side.
1098 xfs_btree_del_cursor(bno_cur_lt
,
1105 * If we couldn't get anything, give up.
1107 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1108 TRACE_ALLOC("neither", args
);
1109 args
->agbno
= NULLAGBLOCK
;
1113 * At this point we have selected a freespace entry, either to the
1114 * left or to the right. If it's on the right, copy all the
1115 * useful variables to the "left" set so we only have one
1116 * copy of this code.
1119 bno_cur_lt
= bno_cur_gt
;
1129 * Fix up the length and compute the useful address.
1131 ltend
= ltbno
+ ltlen
;
1132 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1133 xfs_alloc_fix_len(args
);
1134 if (!xfs_alloc_fix_minleft(args
)) {
1135 TRACE_ALLOC("nominleft", args
);
1136 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1137 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1141 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
, ltbno
,
1143 ASSERT(ltnew
>= ltbno
);
1144 ASSERT(ltnew
+ rlen
<= ltend
);
1145 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1146 args
->agbno
= ltnew
;
1147 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1148 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1150 TRACE_ALLOC(j
? "gt" : "lt", args
);
1151 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1152 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1156 TRACE_ALLOC("error", args
);
1157 if (cnt_cur
!= NULL
)
1158 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1159 if (bno_cur_lt
!= NULL
)
1160 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1161 if (bno_cur_gt
!= NULL
)
1162 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1167 * Allocate a variable extent anywhere in the allocation group agno.
1168 * Extent's length (returned in len) will be between minlen and maxlen,
1169 * and of the form k * prod + mod unless there's nothing that large.
1170 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1172 STATIC
int /* error */
1173 xfs_alloc_ag_vextent_size(
1174 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1176 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1177 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1178 int error
; /* error result */
1179 xfs_agblock_t fbno
; /* start of found freespace */
1180 xfs_extlen_t flen
; /* length of found freespace */
1181 #ifdef XFS_ALLOC_TRACE
1182 static char fname
[] = "xfs_alloc_ag_vextent_size";
1184 int i
; /* temp status variable */
1185 xfs_agblock_t rbno
; /* returned block number */
1186 xfs_extlen_t rlen
; /* length of returned extent */
1189 * Allocate and initialize a cursor for the by-size btree.
1191 cnt_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1192 args
->agno
, XFS_BTNUM_CNT
, NULL
, 0);
1195 * Look for an entry >= maxlen+alignment-1 blocks.
1197 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1198 args
->maxlen
+ args
->alignment
- 1, &i
)))
1201 * If none, then pick up the last entry in the tree unless the
1205 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, &fbno
,
1208 if (i
== 0 || flen
== 0) {
1209 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1210 TRACE_ALLOC("noentry", args
);
1216 * There's a freespace as big as maxlen+alignment-1, get it.
1219 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
)))
1221 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1224 * In the first case above, we got the last entry in the
1225 * by-size btree. Now we check to see if the space hits maxlen
1226 * once aligned; if not, we search left for something better.
1227 * This can't happen in the second case above.
1229 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
, args
->minlen
,
1231 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1232 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1233 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1234 if (rlen
< args
->maxlen
) {
1235 xfs_agblock_t bestfbno
;
1236 xfs_extlen_t bestflen
;
1237 xfs_agblock_t bestrbno
;
1238 xfs_extlen_t bestrlen
;
1245 if ((error
= xfs_alloc_decrement(cnt_cur
, 0, &i
)))
1249 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1252 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1253 if (flen
< bestrlen
)
1255 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
,
1256 args
->minlen
, &rbno
, &rlen
);
1257 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1258 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1259 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1261 if (rlen
> bestrlen
) {
1266 if (rlen
== args
->maxlen
)
1270 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1273 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1279 args
->wasfromfl
= 0;
1281 * Fix up the length.
1284 xfs_alloc_fix_len(args
);
1285 if (rlen
< args
->minlen
|| !xfs_alloc_fix_minleft(args
)) {
1286 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1287 TRACE_ALLOC("nominleft", args
);
1288 args
->agbno
= NULLAGBLOCK
;
1292 XFS_WANT_CORRUPTED_GOTO(rlen
<= flen
, error0
);
1294 * Allocate and initialize a cursor for the by-block tree.
1296 bno_cur
= xfs_btree_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1297 args
->agno
, XFS_BTNUM_BNO
, NULL
, 0);
1298 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1299 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1301 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1302 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1303 cnt_cur
= bno_cur
= NULL
;
1306 XFS_WANT_CORRUPTED_GOTO(
1307 args
->agbno
+ args
->len
<=
1308 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1310 TRACE_ALLOC("normal", args
);
1314 TRACE_ALLOC("error", args
);
1316 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1318 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1323 * Deal with the case where only small freespaces remain.
1324 * Either return the contents of the last freespace record,
1325 * or allocate space from the freelist if there is nothing in the tree.
1327 STATIC
int /* error */
1328 xfs_alloc_ag_vextent_small(
1329 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1330 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1331 xfs_agblock_t
*fbnop
, /* result block number */
1332 xfs_extlen_t
*flenp
, /* result length */
1333 int *stat
) /* status: 0-freelist, 1-normal/none */
1338 #ifdef XFS_ALLOC_TRACE
1339 static char fname
[] = "xfs_alloc_ag_vextent_small";
1343 if ((error
= xfs_alloc_decrement(ccur
, 0, &i
)))
1346 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1348 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1351 * Nothing in the btree, try the freelist. Make sure
1352 * to respect minleft even when pulling from the
1355 else if (args
->minlen
== 1 && args
->alignment
== 1 && !args
->isfl
&&
1356 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1358 if ((error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
)))
1360 if (fbno
!= NULLAGBLOCK
) {
1361 if (args
->userdata
) {
1364 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1365 args
->agno
, fbno
, 0);
1366 xfs_trans_binval(args
->tp
, bp
);
1370 XFS_WANT_CORRUPTED_GOTO(
1371 args
->agbno
+ args
->len
<=
1372 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1374 args
->wasfromfl
= 1;
1375 TRACE_ALLOC("freelist", args
);
1380 * Nothing in the freelist.
1386 * Can't allocate from the freelist for some reason.
1391 * Can't do the allocation, give up.
1393 if (flen
< args
->minlen
) {
1394 args
->agbno
= NULLAGBLOCK
;
1395 TRACE_ALLOC("notenough", args
);
1401 TRACE_ALLOC("normal", args
);
1405 TRACE_ALLOC("error", args
);
1410 * Free the extent starting at agno/bno for length.
1412 STATIC
int /* error */
1414 xfs_trans_t
*tp
, /* transaction pointer */
1415 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
1416 xfs_agnumber_t agno
, /* allocation group number */
1417 xfs_agblock_t bno
, /* starting block number */
1418 xfs_extlen_t len
, /* length of extent */
1419 int isfl
) /* set if is freelist blocks - no sb acctg */
1421 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1422 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1423 int error
; /* error return value */
1424 #ifdef XFS_ALLOC_TRACE
1425 static char fname
[] = "xfs_free_ag_extent";
1427 xfs_agblock_t gtbno
; /* start of right neighbor block */
1428 xfs_extlen_t gtlen
; /* length of right neighbor block */
1429 int haveleft
; /* have a left neighbor block */
1430 int haveright
; /* have a right neighbor block */
1431 int i
; /* temp, result code */
1432 xfs_agblock_t ltbno
; /* start of left neighbor block */
1433 xfs_extlen_t ltlen
; /* length of left neighbor block */
1434 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1435 xfs_agblock_t nbno
; /* new starting block of freespace */
1436 xfs_extlen_t nlen
; /* new length of freespace */
1440 * Allocate and initialize a cursor for the by-block btree.
1442 bno_cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
, NULL
,
1446 * Look for a neighboring block on the left (lower block numbers)
1447 * that is contiguous with this space.
1449 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1453 * There is a block to our left.
1455 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1457 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1459 * It's not contiguous, though.
1461 if (ltbno
+ ltlen
< bno
)
1465 * If this failure happens the request to free this
1466 * space was invalid, it's (partly) already free.
1469 XFS_WANT_CORRUPTED_GOTO(ltbno
+ ltlen
<= bno
, error0
);
1473 * Look for a neighboring block on the right (higher block numbers)
1474 * that is contiguous with this space.
1476 if ((error
= xfs_alloc_increment(bno_cur
, 0, &haveright
)))
1480 * There is a block to our right.
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 (bno
+ len
< gtbno
)
1492 * If this failure happens the request to free this
1493 * space was invalid, it's (partly) already free.
1496 XFS_WANT_CORRUPTED_GOTO(gtbno
>= bno
+ len
, error0
);
1500 * Now allocate and initialize a cursor for the by-size tree.
1502 cnt_cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
, NULL
,
1505 * Have both left and right contiguous neighbors.
1506 * Merge all three into a single free block.
1508 if (haveleft
&& haveright
) {
1510 * Delete the old by-size entry on the left.
1512 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1514 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1515 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1517 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1519 * Delete the old by-size entry on the right.
1521 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1523 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1524 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1526 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1528 * Delete the old by-block entry for the right block.
1530 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
1532 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1534 * Move the by-block cursor back to the left neighbor.
1536 if ((error
= xfs_alloc_decrement(bno_cur
, 0, &i
)))
1538 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1541 * Check that this is the right record: delete didn't
1542 * mangle the cursor.
1545 xfs_agblock_t xxbno
;
1548 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1551 XFS_WANT_CORRUPTED_GOTO(
1552 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1557 * Update remaining by-block entry to the new, joined block.
1560 nlen
= len
+ ltlen
+ gtlen
;
1561 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1565 * Have only a left contiguous neighbor.
1566 * Merge it together with the new freespace.
1568 else if (haveleft
) {
1570 * Delete the old by-size entry on the left.
1572 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1574 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1575 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1577 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1579 * Back up the by-block cursor to the left neighbor, and
1580 * update its length.
1582 if ((error
= xfs_alloc_decrement(bno_cur
, 0, &i
)))
1584 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1587 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1591 * Have only a right contiguous neighbor.
1592 * Merge it together with the new freespace.
1594 else if (haveright
) {
1596 * Delete the old by-size entry on the right.
1598 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1600 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1601 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1603 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1605 * Update the starting block and length of the right
1606 * neighbor in the by-block tree.
1610 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1614 * No contiguous neighbors.
1615 * Insert the new freespace into the by-block tree.
1620 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
1622 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1624 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1627 * In all cases we need to insert the new freespace in the by-size tree.
1629 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1631 XFS_WANT_CORRUPTED_GOTO(i
== 0, error0
);
1632 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
1634 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1635 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1638 * Update the freespace totals in the ag and superblock.
1642 xfs_perag_t
*pag
; /* per allocation group data */
1644 agf
= XFS_BUF_TO_AGF(agbp
);
1645 pag
= &mp
->m_perag
[agno
];
1646 be32_add(&agf
->agf_freeblks
, len
);
1647 xfs_trans_agblocks_delta(tp
, len
);
1648 pag
->pagf_freeblks
+= len
;
1649 XFS_WANT_CORRUPTED_GOTO(
1650 be32_to_cpu(agf
->agf_freeblks
) <=
1651 be32_to_cpu(agf
->agf_length
),
1653 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
1654 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
1656 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, (long)len
);
1657 XFS_STATS_INC(xs_freex
);
1658 XFS_STATS_ADD(xs_freeb
, len
);
1660 TRACE_FREE(haveleft
?
1661 (haveright
? "both" : "left") :
1662 (haveright
? "right" : "none"),
1663 agno
, bno
, len
, isfl
);
1666 * Since blocks move to the free list without the coordination
1667 * used in xfs_bmap_finish, we can't allow block to be available
1668 * for reallocation and non-transaction writing (user data)
1669 * until we know that the transaction that moved it to the free
1670 * list is permanently on disk. We track the blocks by declaring
1671 * these blocks as "busy"; the busy list is maintained on a per-ag
1672 * basis and each transaction records which entries should be removed
1673 * when the iclog commits to disk. If a busy block is allocated,
1674 * the iclog is pushed up to the LSN that freed the block.
1676 xfs_alloc_mark_busy(tp
, agno
, bno
, len
);
1680 TRACE_FREE("error", agno
, bno
, len
, isfl
);
1682 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1684 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1689 * Visible (exported) allocation/free functions.
1690 * Some of these are used just by xfs_alloc_btree.c and this file.
1694 * Compute and fill in value of m_ag_maxlevels.
1697 xfs_alloc_compute_maxlevels(
1698 xfs_mount_t
*mp
) /* file system mount structure */
1706 maxleafents
= (mp
->m_sb
.sb_agblocks
+ 1) / 2;
1707 minleafrecs
= mp
->m_alloc_mnr
[0];
1708 minnoderecs
= mp
->m_alloc_mnr
[1];
1709 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1710 for (level
= 1; maxblocks
> 1; level
++)
1711 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1712 mp
->m_ag_maxlevels
= level
;
1716 * Decide whether to use this allocation group for this allocation.
1717 * If so, fix up the btree freelist's size.
1718 * This is external so mkfs can call it, too.
1721 xfs_alloc_fix_freelist(
1722 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1723 int flags
) /* XFS_ALLOC_FLAG_... */
1725 xfs_buf_t
*agbp
; /* agf buffer pointer */
1726 xfs_agf_t
*agf
; /* a.g. freespace structure pointer */
1727 xfs_buf_t
*agflbp
;/* agfl buffer pointer */
1728 xfs_agblock_t bno
; /* freelist block */
1729 xfs_extlen_t delta
; /* new blocks needed in freelist */
1730 int error
; /* error result code */
1731 xfs_extlen_t longest
;/* longest extent in allocation group */
1732 xfs_mount_t
*mp
; /* file system mount point structure */
1733 xfs_extlen_t need
; /* total blocks needed in freelist */
1734 xfs_perag_t
*pag
; /* per-ag information structure */
1735 xfs_alloc_arg_t targs
; /* local allocation arguments */
1736 xfs_trans_t
*tp
; /* transaction pointer */
1742 if (!pag
->pagf_init
) {
1743 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1746 if (!pag
->pagf_init
) {
1753 /* If this is a metadata prefered pag and we are user data
1754 * then try somewhere else if we are not being asked to
1755 * try harder at this point
1757 if (pag
->pagf_metadata
&& args
->userdata
&& flags
) {
1762 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
1763 delta
= need
> pag
->pagf_flcount
? need
- pag
->pagf_flcount
: 0;
1765 * If it looks like there isn't a long enough extent, or enough
1766 * total blocks, reject it.
1768 longest
= (pag
->pagf_longest
> delta
) ?
1769 (pag
->pagf_longest
- delta
) :
1770 (pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0);
1771 if (args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1 > longest
||
1773 (int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
1774 need
- args
->total
) <
1775 (int)args
->minleft
)) {
1777 xfs_trans_brelse(tp
, agbp
);
1782 * Get the a.g. freespace buffer.
1783 * Can fail if we're not blocking on locks, and it's held.
1786 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1795 * Figure out how many blocks we should have in the freelist.
1797 agf
= XFS_BUF_TO_AGF(agbp
);
1798 need
= XFS_MIN_FREELIST(agf
, mp
);
1799 delta
= need
> be32_to_cpu(agf
->agf_flcount
) ?
1800 (need
- be32_to_cpu(agf
->agf_flcount
)) : 0;
1802 * If there isn't enough total or single-extent, reject it.
1804 longest
= be32_to_cpu(agf
->agf_longest
);
1805 longest
= (longest
> delta
) ? (longest
- delta
) :
1806 (be32_to_cpu(agf
->agf_flcount
) > 0 || longest
> 0);
1807 if (args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1 > longest
||
1809 (int)(be32_to_cpu(agf
->agf_freeblks
) +
1810 be32_to_cpu(agf
->agf_flcount
) - need
- args
->total
) <
1811 (int)args
->minleft
)) {
1812 xfs_trans_brelse(tp
, agbp
);
1817 * Make the freelist shorter if it's too long.
1819 while (be32_to_cpu(agf
->agf_flcount
) > need
) {
1822 if ((error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
)))
1824 if ((error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1, 1)))
1826 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
1827 xfs_trans_binval(tp
, bp
);
1830 * Initialize the args structure.
1835 targs
.agno
= args
->agno
;
1836 targs
.mod
= targs
.minleft
= targs
.wasdel
= targs
.userdata
=
1837 targs
.minalignslop
= 0;
1838 targs
.alignment
= targs
.minlen
= targs
.prod
= targs
.isfl
= 1;
1839 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
1841 if ((error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
)))
1844 * Make the freelist longer if it's too short.
1846 while (be32_to_cpu(agf
->agf_flcount
) < need
) {
1848 targs
.maxlen
= need
- be32_to_cpu(agf
->agf_flcount
);
1850 * Allocate as many blocks as possible at once.
1852 if ((error
= xfs_alloc_ag_vextent(&targs
))) {
1853 xfs_trans_brelse(tp
, agflbp
);
1857 * Stop if we run out. Won't happen if callers are obeying
1858 * the restrictions correctly. Can happen for free calls
1859 * on a completely full ag.
1861 if (targs
.agbno
== NULLAGBLOCK
)
1864 * Put each allocated block on the list.
1866 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
1867 if ((error
= xfs_alloc_put_freelist(tp
, agbp
, agflbp
,
1872 xfs_trans_brelse(tp
, agflbp
);
1878 * Get a block from the freelist.
1879 * Returns with the buffer for the block gotten.
1882 xfs_alloc_get_freelist(
1883 xfs_trans_t
*tp
, /* transaction pointer */
1884 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
1885 xfs_agblock_t
*bnop
) /* block address retrieved from freelist */
1887 xfs_agf_t
*agf
; /* a.g. freespace structure */
1888 xfs_agfl_t
*agfl
; /* a.g. freelist structure */
1889 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
1890 xfs_agblock_t bno
; /* block number returned */
1892 #ifdef XFS_ALLOC_TRACE
1893 static char fname
[] = "xfs_alloc_get_freelist";
1895 xfs_mount_t
*mp
; /* mount structure */
1896 xfs_perag_t
*pag
; /* per allocation group data */
1898 agf
= XFS_BUF_TO_AGF(agbp
);
1900 * Freelist is empty, give up.
1902 if (!agf
->agf_flcount
) {
1903 *bnop
= NULLAGBLOCK
;
1907 * Read the array of free blocks.
1910 if ((error
= xfs_alloc_read_agfl(mp
, tp
,
1911 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
1913 agfl
= XFS_BUF_TO_AGFL(agflbp
);
1915 * Get the block number and update the data structures.
1917 bno
= INT_GET(agfl
->agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)], ARCH_CONVERT
);
1918 be32_add(&agf
->agf_flfirst
, 1);
1919 xfs_trans_brelse(tp
, agflbp
);
1920 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
1921 agf
->agf_flfirst
= 0;
1922 pag
= &mp
->m_perag
[be32_to_cpu(agf
->agf_seqno
)];
1923 be32_add(&agf
->agf_flcount
, -1);
1924 xfs_trans_agflist_delta(tp
, -1);
1925 pag
->pagf_flcount
--;
1926 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
);
1927 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
);
1931 * As blocks are freed, they are added to the per-ag busy list
1932 * and remain there until the freeing transaction is committed to
1933 * disk. Now that we have allocated blocks, this list must be
1934 * searched to see if a block is being reused. If one is, then
1935 * the freeing transaction must be pushed to disk NOW by forcing
1936 * to disk all iclogs up that transaction's LSN.
1938 xfs_alloc_search_busy(tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1);
1943 * Log the given fields from the agf structure.
1947 xfs_trans_t
*tp
, /* transaction pointer */
1948 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
1949 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
1951 int first
; /* first byte offset */
1952 int last
; /* last byte offset */
1953 static const short offsets
[] = {
1954 offsetof(xfs_agf_t
, agf_magicnum
),
1955 offsetof(xfs_agf_t
, agf_versionnum
),
1956 offsetof(xfs_agf_t
, agf_seqno
),
1957 offsetof(xfs_agf_t
, agf_length
),
1958 offsetof(xfs_agf_t
, agf_roots
[0]),
1959 offsetof(xfs_agf_t
, agf_levels
[0]),
1960 offsetof(xfs_agf_t
, agf_flfirst
),
1961 offsetof(xfs_agf_t
, agf_fllast
),
1962 offsetof(xfs_agf_t
, agf_flcount
),
1963 offsetof(xfs_agf_t
, agf_freeblks
),
1964 offsetof(xfs_agf_t
, agf_longest
),
1968 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
1969 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
1973 * Interface for inode allocation to force the pag data to be initialized.
1976 xfs_alloc_pagf_init(
1977 xfs_mount_t
*mp
, /* file system mount structure */
1978 xfs_trans_t
*tp
, /* transaction pointer */
1979 xfs_agnumber_t agno
, /* allocation group number */
1980 int flags
) /* XFS_ALLOC_FLAGS_... */
1985 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
1988 xfs_trans_brelse(tp
, bp
);
1993 * Put the block on the freelist for the allocation group.
1996 xfs_alloc_put_freelist(
1997 xfs_trans_t
*tp
, /* transaction pointer */
1998 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
1999 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2000 xfs_agblock_t bno
) /* block being freed */
2002 xfs_agf_t
*agf
; /* a.g. freespace structure */
2003 xfs_agfl_t
*agfl
; /* a.g. free block array */
2004 xfs_agblock_t
*blockp
;/* pointer to array entry */
2006 #ifdef XFS_ALLOC_TRACE
2007 static char fname
[] = "xfs_alloc_put_freelist";
2009 xfs_mount_t
*mp
; /* mount structure */
2010 xfs_perag_t
*pag
; /* per allocation group data */
2012 agf
= XFS_BUF_TO_AGF(agbp
);
2015 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2016 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2018 agfl
= XFS_BUF_TO_AGFL(agflbp
);
2019 be32_add(&agf
->agf_fllast
, 1);
2020 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2021 agf
->agf_fllast
= 0;
2022 pag
= &mp
->m_perag
[be32_to_cpu(agf
->agf_seqno
)];
2023 be32_add(&agf
->agf_flcount
, 1);
2024 xfs_trans_agflist_delta(tp
, 1);
2025 pag
->pagf_flcount
++;
2026 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2027 blockp
= &agfl
->agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2028 INT_SET(*blockp
, ARCH_CONVERT
, bno
);
2029 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
);
2030 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
);
2031 xfs_trans_log_buf(tp
, agflbp
,
2032 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
),
2033 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
+
2034 sizeof(xfs_agblock_t
) - 1));
2039 * Read in the allocation group header (free/alloc section).
2043 xfs_mount_t
*mp
, /* mount point structure */
2044 xfs_trans_t
*tp
, /* transaction pointer */
2045 xfs_agnumber_t agno
, /* allocation group number */
2046 int flags
, /* XFS_ALLOC_FLAG_... */
2047 xfs_buf_t
**bpp
) /* buffer for the ag freelist header */
2049 xfs_agf_t
*agf
; /* ag freelist header */
2050 int agf_ok
; /* set if agf is consistent */
2051 xfs_buf_t
*bp
; /* return value */
2052 xfs_perag_t
*pag
; /* per allocation group data */
2055 ASSERT(agno
!= NULLAGNUMBER
);
2056 error
= xfs_trans_read_buf(
2057 mp
, tp
, mp
->m_ddev_targp
,
2058 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2059 XFS_FSS_TO_BB(mp
, 1),
2060 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XFS_BUF_TRYLOCK
: 0U,
2064 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
2070 * Validate the magic number of the agf block.
2072 agf
= XFS_BUF_TO_AGF(bp
);
2074 be32_to_cpu(agf
->agf_magicnum
) == XFS_AGF_MAGIC
&&
2075 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2076 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2077 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2078 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2079 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
);
2080 if (unlikely(XFS_TEST_ERROR(!agf_ok
, mp
, XFS_ERRTAG_ALLOC_READ_AGF
,
2081 XFS_RANDOM_ALLOC_READ_AGF
))) {
2082 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2083 XFS_ERRLEVEL_LOW
, mp
, agf
);
2084 xfs_trans_brelse(tp
, bp
);
2085 return XFS_ERROR(EFSCORRUPTED
);
2087 pag
= &mp
->m_perag
[agno
];
2088 if (!pag
->pagf_init
) {
2089 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2090 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2091 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2092 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2093 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2094 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2095 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2096 spinlock_init(&pag
->pagb_lock
, "xfspagb");
2097 pag
->pagb_list
= kmem_zalloc(XFS_PAGB_NUM_SLOTS
*
2098 sizeof(xfs_perag_busy_t
), KM_SLEEP
);
2102 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2103 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2104 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2105 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2106 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2107 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2108 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2109 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2112 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGF
, XFS_AGF_REF
);
2118 * Allocate an extent (variable-size).
2119 * Depending on the allocation type, we either look in a single allocation
2120 * group or loop over the allocation groups to find the result.
2124 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2126 xfs_agblock_t agsize
; /* allocation group size */
2128 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2129 #ifdef XFS_ALLOC_TRACE
2130 static char fname
[] = "xfs_alloc_vextent";
2132 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2133 xfs_mount_t
*mp
; /* mount structure pointer */
2134 xfs_agnumber_t sagno
; /* starting allocation group number */
2135 xfs_alloctype_t type
; /* input allocation type */
2138 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2141 type
= args
->otype
= args
->type
;
2142 args
->agbno
= NULLAGBLOCK
;
2144 * Just fix this up, for the case where the last a.g. is shorter
2145 * (or there's only one a.g.) and the caller couldn't easily figure
2146 * that out (xfs_bmap_alloc).
2148 agsize
= mp
->m_sb
.sb_agblocks
;
2149 if (args
->maxlen
> agsize
)
2150 args
->maxlen
= agsize
;
2151 if (args
->alignment
== 0)
2152 args
->alignment
= 1;
2153 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2154 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2155 ASSERT(args
->minlen
<= args
->maxlen
);
2156 ASSERT(args
->minlen
<= agsize
);
2157 ASSERT(args
->mod
< args
->prod
);
2158 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2159 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2160 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2161 args
->mod
>= args
->prod
) {
2162 args
->fsbno
= NULLFSBLOCK
;
2163 TRACE_ALLOC("badargs", args
);
2166 minleft
= args
->minleft
;
2169 case XFS_ALLOCTYPE_THIS_AG
:
2170 case XFS_ALLOCTYPE_NEAR_BNO
:
2171 case XFS_ALLOCTYPE_THIS_BNO
:
2173 * These three force us into a single a.g.
2175 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2176 down_read(&mp
->m_peraglock
);
2177 args
->pag
= &mp
->m_perag
[args
->agno
];
2179 error
= xfs_alloc_fix_freelist(args
, 0);
2180 args
->minleft
= minleft
;
2182 TRACE_ALLOC("nofix", args
);
2186 up_read(&mp
->m_peraglock
);
2187 TRACE_ALLOC("noagbp", args
);
2190 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2191 if ((error
= xfs_alloc_ag_vextent(args
)))
2193 up_read(&mp
->m_peraglock
);
2195 case XFS_ALLOCTYPE_START_BNO
:
2197 * Try near allocation first, then anywhere-in-ag after
2198 * the first a.g. fails.
2200 if ((args
->userdata
== XFS_ALLOC_INITIAL_USER_DATA
) &&
2201 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2202 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2203 ((mp
->m_agfrotor
/ rotorstep
) %
2204 mp
->m_sb
.sb_agcount
), 0);
2207 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2208 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2210 case XFS_ALLOCTYPE_ANY_AG
:
2211 case XFS_ALLOCTYPE_START_AG
:
2212 case XFS_ALLOCTYPE_FIRST_AG
:
2214 * Rotate through the allocation groups looking for a winner.
2216 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2218 * Start with the last place we left off.
2220 args
->agno
= sagno
= (mp
->m_agfrotor
/ rotorstep
) %
2221 mp
->m_sb
.sb_agcount
;
2222 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2223 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2224 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2226 * Start with allocation group given by bno.
2228 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2229 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2233 if (type
== XFS_ALLOCTYPE_START_AG
)
2234 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2236 * Start with the given allocation group.
2238 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2239 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2242 * Loop over allocation groups twice; first time with
2243 * trylock set, second time without.
2245 down_read(&mp
->m_peraglock
);
2247 args
->pag
= &mp
->m_perag
[args
->agno
];
2248 if (no_min
) args
->minleft
= 0;
2249 error
= xfs_alloc_fix_freelist(args
, flags
);
2250 args
->minleft
= minleft
;
2252 TRACE_ALLOC("nofix", args
);
2256 * If we get a buffer back then the allocation will fly.
2259 if ((error
= xfs_alloc_ag_vextent(args
)))
2263 TRACE_ALLOC("loopfailed", args
);
2265 * Didn't work, figure out the next iteration.
2267 if (args
->agno
== sagno
&&
2268 type
== XFS_ALLOCTYPE_START_BNO
)
2269 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2270 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
)
2273 * Reached the starting a.g., must either be done
2274 * or switch to non-trylock mode.
2276 if (args
->agno
== sagno
) {
2278 args
->agbno
= NULLAGBLOCK
;
2279 TRACE_ALLOC("allfailed", args
);
2286 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2287 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2289 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2294 up_read(&mp
->m_peraglock
);
2295 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
)) {
2296 if (args
->agno
== sagno
)
2297 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2298 (mp
->m_sb
.sb_agcount
* rotorstep
);
2300 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2301 (mp
->m_sb
.sb_agcount
* rotorstep
);
2308 if (args
->agbno
== NULLAGBLOCK
)
2309 args
->fsbno
= NULLFSBLOCK
;
2311 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2313 ASSERT(args
->len
>= args
->minlen
);
2314 ASSERT(args
->len
<= args
->maxlen
);
2315 ASSERT(args
->agbno
% args
->alignment
== 0);
2316 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2322 up_read(&mp
->m_peraglock
);
2328 * Just break up the extent address and hand off to xfs_free_ag_extent
2329 * after fixing up the freelist.
2333 xfs_trans_t
*tp
, /* transaction pointer */
2334 xfs_fsblock_t bno
, /* starting block number of extent */
2335 xfs_extlen_t len
) /* length of extent */
2338 xfs_agf_t
*agf
; /* a.g. freespace header */
2340 xfs_alloc_arg_t args
; /* allocation argument structure */
2345 args
.mp
= tp
->t_mountp
;
2346 args
.agno
= XFS_FSB_TO_AGNO(args
.mp
, bno
);
2347 ASSERT(args
.agno
< args
.mp
->m_sb
.sb_agcount
);
2348 args
.agbno
= XFS_FSB_TO_AGBNO(args
.mp
, bno
);
2350 args
.minlen
= args
.minleft
= args
.minalignslop
= 0;
2351 down_read(&args
.mp
->m_peraglock
);
2352 args
.pag
= &args
.mp
->m_perag
[args
.agno
];
2353 if ((error
= xfs_alloc_fix_freelist(&args
, 0)))
2356 ASSERT(args
.agbp
!= NULL
);
2357 agf
= XFS_BUF_TO_AGF(args
.agbp
);
2358 ASSERT(args
.agbno
+ len
<= be32_to_cpu(agf
->agf_length
));
2360 error
= xfs_free_ag_extent(tp
, args
.agbp
, args
.agno
, args
.agbno
,
2363 up_read(&args
.mp
->m_peraglock
);