2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_error.h"
43 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
45 #define XFSA_FIXUP_BNO_OK 1
46 #define XFSA_FIXUP_CNT_OK 2
49 xfs_alloc_search_busy(xfs_trans_t
*tp
,
54 #if defined(XFS_ALLOC_TRACE)
55 ktrace_t
*xfs_alloc_trace_buf
;
57 #define TRACE_ALLOC(s,a) \
58 xfs_alloc_trace_alloc(__func__, s, a, __LINE__)
59 #define TRACE_FREE(s,a,b,x,f) \
60 xfs_alloc_trace_free(__func__, s, mp, a, b, x, f, __LINE__)
61 #define TRACE_MODAGF(s,a,f) \
62 xfs_alloc_trace_modagf(__func__, s, mp, a, f, __LINE__)
63 #define TRACE_BUSY(__func__,s,ag,agb,l,sl,tp) \
64 xfs_alloc_trace_busy(__func__, s, mp, ag, agb, l, sl, tp, XFS_ALLOC_KTRACE_BUSY, __LINE__)
65 #define TRACE_UNBUSY(__func__,s,ag,sl,tp) \
66 xfs_alloc_trace_busy(__func__, s, mp, ag, -1, -1, sl, tp, XFS_ALLOC_KTRACE_UNBUSY, __LINE__)
67 #define TRACE_BUSYSEARCH(__func__,s,ag,agb,l,tp) \
68 xfs_alloc_trace_busy(__func__, s, mp, ag, agb, l, 0, tp, XFS_ALLOC_KTRACE_BUSYSEARCH, __LINE__)
70 #define TRACE_ALLOC(s,a)
71 #define TRACE_FREE(s,a,b,x,f)
72 #define TRACE_MODAGF(s,a,f)
73 #define TRACE_BUSY(s,a,ag,agb,l,sl,tp)
74 #define TRACE_UNBUSY(fname,s,ag,sl,tp)
75 #define TRACE_BUSYSEARCH(fname,s,ag,agb,l,tp)
76 #endif /* XFS_ALLOC_TRACE */
79 * Prototypes for per-ag allocation routines
82 STATIC
int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t
*);
83 STATIC
int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t
*);
84 STATIC
int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t
*);
85 STATIC
int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t
*,
86 xfs_btree_cur_t
*, xfs_agblock_t
*, xfs_extlen_t
*, int *);
93 * Lookup the record equal to [bno, len] in the btree given by cur.
95 STATIC
int /* error */
97 struct xfs_btree_cur
*cur
, /* btree cursor */
98 xfs_agblock_t bno
, /* starting block of extent */
99 xfs_extlen_t len
, /* length of extent */
100 int *stat
) /* success/failure */
102 cur
->bc_rec
.a
.ar_startblock
= bno
;
103 cur
->bc_rec
.a
.ar_blockcount
= len
;
104 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
108 * Lookup the first record greater than or equal to [bno, len]
109 * in the btree given by cur.
111 STATIC
int /* error */
113 struct xfs_btree_cur
*cur
, /* btree cursor */
114 xfs_agblock_t bno
, /* starting block of extent */
115 xfs_extlen_t len
, /* length of extent */
116 int *stat
) /* success/failure */
118 cur
->bc_rec
.a
.ar_startblock
= bno
;
119 cur
->bc_rec
.a
.ar_blockcount
= len
;
120 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
124 * Lookup the first record less than or equal to [bno, len]
125 * in the btree given by cur.
127 STATIC
int /* error */
129 struct xfs_btree_cur
*cur
, /* btree cursor */
130 xfs_agblock_t bno
, /* starting block of extent */
131 xfs_extlen_t len
, /* length of extent */
132 int *stat
) /* success/failure */
134 cur
->bc_rec
.a
.ar_startblock
= bno
;
135 cur
->bc_rec
.a
.ar_blockcount
= len
;
136 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
141 * Compute aligned version of the found extent.
142 * Takes alignment and min length into account.
145 xfs_alloc_compute_aligned(
146 xfs_agblock_t foundbno
, /* starting block in found extent */
147 xfs_extlen_t foundlen
, /* length in found extent */
148 xfs_extlen_t alignment
, /* alignment for allocation */
149 xfs_extlen_t minlen
, /* minimum length for allocation */
150 xfs_agblock_t
*resbno
, /* result block number */
151 xfs_extlen_t
*reslen
) /* result length */
157 if (alignment
> 1 && foundlen
>= minlen
) {
158 bno
= roundup(foundbno
, alignment
);
159 diff
= bno
- foundbno
;
160 len
= diff
>= foundlen
? 0 : foundlen
- diff
;
170 * Compute best start block and diff for "near" allocations.
171 * freelen >= wantlen already checked by caller.
173 STATIC xfs_extlen_t
/* difference value (absolute) */
174 xfs_alloc_compute_diff(
175 xfs_agblock_t wantbno
, /* target starting block */
176 xfs_extlen_t wantlen
, /* target length */
177 xfs_extlen_t alignment
, /* target alignment */
178 xfs_agblock_t freebno
, /* freespace's starting block */
179 xfs_extlen_t freelen
, /* freespace's length */
180 xfs_agblock_t
*newbnop
) /* result: best start block from free */
182 xfs_agblock_t freeend
; /* end of freespace extent */
183 xfs_agblock_t newbno1
; /* return block number */
184 xfs_agblock_t newbno2
; /* other new block number */
185 xfs_extlen_t newlen1
=0; /* length with newbno1 */
186 xfs_extlen_t newlen2
=0; /* length with newbno2 */
187 xfs_agblock_t wantend
; /* end of target extent */
189 ASSERT(freelen
>= wantlen
);
190 freeend
= freebno
+ freelen
;
191 wantend
= wantbno
+ wantlen
;
192 if (freebno
>= wantbno
) {
193 if ((newbno1
= roundup(freebno
, alignment
)) >= freeend
)
194 newbno1
= NULLAGBLOCK
;
195 } else if (freeend
>= wantend
&& alignment
> 1) {
196 newbno1
= roundup(wantbno
, alignment
);
197 newbno2
= newbno1
- alignment
;
198 if (newbno1
>= freeend
)
199 newbno1
= NULLAGBLOCK
;
201 newlen1
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno1
);
202 if (newbno2
< freebno
)
203 newbno2
= NULLAGBLOCK
;
205 newlen2
= XFS_EXTLEN_MIN(wantlen
, freeend
- newbno2
);
206 if (newbno1
!= NULLAGBLOCK
&& newbno2
!= NULLAGBLOCK
) {
207 if (newlen1
< newlen2
||
208 (newlen1
== newlen2
&&
209 XFS_ABSDIFF(newbno1
, wantbno
) >
210 XFS_ABSDIFF(newbno2
, wantbno
)))
212 } else if (newbno2
!= NULLAGBLOCK
)
214 } else if (freeend
>= wantend
) {
216 } else if (alignment
> 1) {
217 newbno1
= roundup(freeend
- wantlen
, alignment
);
218 if (newbno1
> freeend
- wantlen
&&
219 newbno1
- alignment
>= freebno
)
220 newbno1
-= alignment
;
221 else if (newbno1
>= freeend
)
222 newbno1
= NULLAGBLOCK
;
224 newbno1
= freeend
- wantlen
;
226 return newbno1
== NULLAGBLOCK
? 0 : XFS_ABSDIFF(newbno1
, wantbno
);
230 * Fix up the length, based on mod and prod.
231 * len should be k * prod + mod for some k.
232 * If len is too small it is returned unchanged.
233 * If len hits maxlen it is left alone.
237 xfs_alloc_arg_t
*args
) /* allocation argument structure */
242 ASSERT(args
->mod
< args
->prod
);
244 ASSERT(rlen
>= args
->minlen
);
245 ASSERT(rlen
<= args
->maxlen
);
246 if (args
->prod
<= 1 || rlen
< args
->mod
|| rlen
== args
->maxlen
||
247 (args
->mod
== 0 && rlen
< args
->prod
))
249 k
= rlen
% args
->prod
;
253 if ((int)(rlen
= rlen
- k
- args
->mod
) < (int)args
->minlen
)
256 if ((int)(rlen
= rlen
- args
->prod
- (args
->mod
- k
)) <
260 ASSERT(rlen
>= args
->minlen
);
261 ASSERT(rlen
<= args
->maxlen
);
266 * Fix up length if there is too little space left in the a.g.
267 * Return 1 if ok, 0 if too little, should give up.
270 xfs_alloc_fix_minleft(
271 xfs_alloc_arg_t
*args
) /* allocation argument structure */
273 xfs_agf_t
*agf
; /* a.g. freelist header */
274 int diff
; /* free space difference */
276 if (args
->minleft
== 0)
278 agf
= XFS_BUF_TO_AGF(args
->agbp
);
279 diff
= be32_to_cpu(agf
->agf_freeblks
)
280 + be32_to_cpu(agf
->agf_flcount
)
281 - args
->len
- args
->minleft
;
284 args
->len
+= diff
; /* shrink the allocated space */
285 if (args
->len
>= args
->minlen
)
287 args
->agbno
= NULLAGBLOCK
;
292 * Update the two btrees, logically removing from freespace the extent
293 * starting at rbno, rlen blocks. The extent is contained within the
294 * actual (current) free extent fbno for flen blocks.
295 * Flags are passed in indicating whether the cursors are set to the
298 STATIC
int /* error code */
299 xfs_alloc_fixup_trees(
300 xfs_btree_cur_t
*cnt_cur
, /* cursor for by-size btree */
301 xfs_btree_cur_t
*bno_cur
, /* cursor for by-block btree */
302 xfs_agblock_t fbno
, /* starting block of free extent */
303 xfs_extlen_t flen
, /* length of free extent */
304 xfs_agblock_t rbno
, /* starting block of returned extent */
305 xfs_extlen_t rlen
, /* length of returned extent */
306 int flags
) /* flags, XFSA_FIXUP_... */
308 int error
; /* error code */
309 int i
; /* operation results */
310 xfs_agblock_t nfbno1
; /* first new free startblock */
311 xfs_agblock_t nfbno2
; /* second new free startblock */
312 xfs_extlen_t nflen1
=0; /* first new free length */
313 xfs_extlen_t nflen2
=0; /* second new free length */
316 * Look up the record in the by-size tree if necessary.
318 if (flags
& XFSA_FIXUP_CNT_OK
) {
320 if ((error
= xfs_alloc_get_rec(cnt_cur
, &nfbno1
, &nflen1
, &i
)))
322 XFS_WANT_CORRUPTED_RETURN(
323 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
326 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, fbno
, flen
, &i
)))
328 XFS_WANT_CORRUPTED_RETURN(i
== 1);
331 * Look up the record in the by-block tree if necessary.
333 if (flags
& XFSA_FIXUP_BNO_OK
) {
335 if ((error
= xfs_alloc_get_rec(bno_cur
, &nfbno1
, &nflen1
, &i
)))
337 XFS_WANT_CORRUPTED_RETURN(
338 i
== 1 && nfbno1
== fbno
&& nflen1
== flen
);
341 if ((error
= xfs_alloc_lookup_eq(bno_cur
, fbno
, flen
, &i
)))
343 XFS_WANT_CORRUPTED_RETURN(i
== 1);
347 xfs_alloc_block_t
*bnoblock
;
348 xfs_alloc_block_t
*cntblock
;
350 if (bno_cur
->bc_nlevels
== 1 &&
351 cnt_cur
->bc_nlevels
== 1) {
352 bnoblock
= XFS_BUF_TO_ALLOC_BLOCK(bno_cur
->bc_bufs
[0]);
353 cntblock
= XFS_BUF_TO_ALLOC_BLOCK(cnt_cur
->bc_bufs
[0]);
354 XFS_WANT_CORRUPTED_RETURN(
355 be16_to_cpu(bnoblock
->bb_numrecs
) ==
356 be16_to_cpu(cntblock
->bb_numrecs
));
361 * Deal with all four cases: the allocated record is contained
362 * within the freespace record, so we can have new freespace
363 * at either (or both) end, or no freespace remaining.
365 if (rbno
== fbno
&& rlen
== flen
)
366 nfbno1
= nfbno2
= NULLAGBLOCK
;
367 else if (rbno
== fbno
) {
368 nfbno1
= rbno
+ rlen
;
369 nflen1
= flen
- rlen
;
370 nfbno2
= NULLAGBLOCK
;
371 } else if (rbno
+ rlen
== fbno
+ flen
) {
373 nflen1
= flen
- rlen
;
374 nfbno2
= NULLAGBLOCK
;
377 nflen1
= rbno
- fbno
;
378 nfbno2
= rbno
+ rlen
;
379 nflen2
= (fbno
+ flen
) - nfbno2
;
382 * Delete the entry from the by-size btree.
384 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
386 XFS_WANT_CORRUPTED_RETURN(i
== 1);
388 * Add new by-size btree entry(s).
390 if (nfbno1
!= NULLAGBLOCK
) {
391 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno1
, nflen1
, &i
)))
393 XFS_WANT_CORRUPTED_RETURN(i
== 0);
394 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
396 XFS_WANT_CORRUPTED_RETURN(i
== 1);
398 if (nfbno2
!= NULLAGBLOCK
) {
399 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nfbno2
, nflen2
, &i
)))
401 XFS_WANT_CORRUPTED_RETURN(i
== 0);
402 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
404 XFS_WANT_CORRUPTED_RETURN(i
== 1);
407 * Fix up the by-block btree entry(s).
409 if (nfbno1
== NULLAGBLOCK
) {
411 * No remaining freespace, just delete the by-block tree entry.
413 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
415 XFS_WANT_CORRUPTED_RETURN(i
== 1);
418 * Update the by-block entry to start later|be shorter.
420 if ((error
= xfs_alloc_update(bno_cur
, nfbno1
, nflen1
)))
423 if (nfbno2
!= NULLAGBLOCK
) {
425 * 2 resulting free entries, need to add one.
427 if ((error
= xfs_alloc_lookup_eq(bno_cur
, nfbno2
, nflen2
, &i
)))
429 XFS_WANT_CORRUPTED_RETURN(i
== 0);
430 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
432 XFS_WANT_CORRUPTED_RETURN(i
== 1);
438 * Read in the allocation group free block array.
440 STATIC
int /* error */
442 xfs_mount_t
*mp
, /* mount point structure */
443 xfs_trans_t
*tp
, /* transaction pointer */
444 xfs_agnumber_t agno
, /* allocation group number */
445 xfs_buf_t
**bpp
) /* buffer for the ag free block array */
447 xfs_buf_t
*bp
; /* return value */
450 ASSERT(agno
!= NULLAGNUMBER
);
451 error
= xfs_trans_read_buf(
452 mp
, tp
, mp
->m_ddev_targp
,
453 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
454 XFS_FSS_TO_BB(mp
, 1), 0, &bp
);
458 ASSERT(!XFS_BUF_GETERROR(bp
));
459 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGFL
, XFS_AGFL_REF
);
464 #if defined(XFS_ALLOC_TRACE)
466 * Add an allocation trace entry for an alloc call.
469 xfs_alloc_trace_alloc(
470 const char *name
, /* function tag string */
471 char *str
, /* additional string */
472 xfs_alloc_arg_t
*args
, /* allocation argument structure */
473 int line
) /* source line number */
475 ktrace_enter(xfs_alloc_trace_buf
,
476 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_ALLOC
| (line
<< 16)),
480 (void *)(__psunsigned_t
)args
->agno
,
481 (void *)(__psunsigned_t
)args
->agbno
,
482 (void *)(__psunsigned_t
)args
->minlen
,
483 (void *)(__psunsigned_t
)args
->maxlen
,
484 (void *)(__psunsigned_t
)args
->mod
,
485 (void *)(__psunsigned_t
)args
->prod
,
486 (void *)(__psunsigned_t
)args
->minleft
,
487 (void *)(__psunsigned_t
)args
->total
,
488 (void *)(__psunsigned_t
)args
->alignment
,
489 (void *)(__psunsigned_t
)args
->len
,
490 (void *)((((__psint_t
)args
->type
) << 16) |
491 (__psint_t
)args
->otype
),
492 (void *)(__psint_t
)((args
->wasdel
<< 3) |
493 (args
->wasfromfl
<< 2) |
495 (args
->userdata
<< 0)));
499 * Add an allocation trace entry for a free call.
502 xfs_alloc_trace_free(
503 const char *name
, /* function tag string */
504 char *str
, /* additional string */
505 xfs_mount_t
*mp
, /* file system mount point */
506 xfs_agnumber_t agno
, /* allocation group number */
507 xfs_agblock_t agbno
, /* a.g. relative block number */
508 xfs_extlen_t len
, /* length of extent */
509 int isfl
, /* set if is freelist allocation/free */
510 int line
) /* source line number */
512 ktrace_enter(xfs_alloc_trace_buf
,
513 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_FREE
| (line
<< 16)),
517 (void *)(__psunsigned_t
)agno
,
518 (void *)(__psunsigned_t
)agbno
,
519 (void *)(__psunsigned_t
)len
,
520 (void *)(__psint_t
)isfl
,
521 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
525 * Add an allocation trace entry for modifying an agf.
528 xfs_alloc_trace_modagf(
529 const char *name
, /* function tag string */
530 char *str
, /* additional string */
531 xfs_mount_t
*mp
, /* file system mount point */
532 xfs_agf_t
*agf
, /* new agf value */
533 int flags
, /* logging flags for agf */
534 int line
) /* source line number */
536 ktrace_enter(xfs_alloc_trace_buf
,
537 (void *)(__psint_t
)(XFS_ALLOC_KTRACE_MODAGF
| (line
<< 16)),
541 (void *)(__psint_t
)flags
,
542 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_seqno
),
543 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_length
),
544 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]),
545 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]),
546 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]),
547 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]),
548 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_flfirst
),
549 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_fllast
),
550 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_flcount
),
551 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_freeblks
),
552 (void *)(__psunsigned_t
)be32_to_cpu(agf
->agf_longest
));
556 xfs_alloc_trace_busy(
557 const char *name
, /* function tag string */
558 char *str
, /* additional string */
559 xfs_mount_t
*mp
, /* file system mount point */
560 xfs_agnumber_t agno
, /* allocation group number */
561 xfs_agblock_t agbno
, /* a.g. relative block number */
562 xfs_extlen_t len
, /* length of extent */
563 int slot
, /* perag Busy slot */
565 int trtype
, /* type: add, delete, search */
566 int line
) /* source line number */
568 ktrace_enter(xfs_alloc_trace_buf
,
569 (void *)(__psint_t
)(trtype
| (line
<< 16)),
573 (void *)(__psunsigned_t
)agno
,
574 (void *)(__psunsigned_t
)agbno
,
575 (void *)(__psunsigned_t
)len
,
576 (void *)(__psint_t
)slot
,
578 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
580 #endif /* XFS_ALLOC_TRACE */
583 * Allocation group level functions.
587 * Allocate a variable extent in the allocation group agno.
588 * Type and bno are used to determine where in the allocation group the
590 * Extent's length (returned in *len) will be between minlen and maxlen,
591 * and of the form k * prod + mod unless there's nothing that large.
592 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
594 STATIC
int /* error */
595 xfs_alloc_ag_vextent(
596 xfs_alloc_arg_t
*args
) /* argument structure for allocation */
600 ASSERT(args
->minlen
> 0);
601 ASSERT(args
->maxlen
> 0);
602 ASSERT(args
->minlen
<= args
->maxlen
);
603 ASSERT(args
->mod
< args
->prod
);
604 ASSERT(args
->alignment
> 0);
606 * Branch to correct routine based on the type.
609 switch (args
->type
) {
610 case XFS_ALLOCTYPE_THIS_AG
:
611 error
= xfs_alloc_ag_vextent_size(args
);
613 case XFS_ALLOCTYPE_NEAR_BNO
:
614 error
= xfs_alloc_ag_vextent_near(args
);
616 case XFS_ALLOCTYPE_THIS_BNO
:
617 error
= xfs_alloc_ag_vextent_exact(args
);
626 * If the allocation worked, need to change the agf structure
627 * (and log it), and the superblock.
629 if (args
->agbno
!= NULLAGBLOCK
) {
630 xfs_agf_t
*agf
; /* allocation group freelist header */
631 #ifdef XFS_ALLOC_TRACE
632 xfs_mount_t
*mp
= args
->mp
;
634 long slen
= (long)args
->len
;
636 ASSERT(args
->len
>= args
->minlen
&& args
->len
<= args
->maxlen
);
637 ASSERT(!(args
->wasfromfl
) || !args
->isfl
);
638 ASSERT(args
->agbno
% args
->alignment
== 0);
639 if (!(args
->wasfromfl
)) {
641 agf
= XFS_BUF_TO_AGF(args
->agbp
);
642 be32_add_cpu(&agf
->agf_freeblks
, -(args
->len
));
643 xfs_trans_agblocks_delta(args
->tp
,
644 -((long)(args
->len
)));
645 args
->pag
->pagf_freeblks
-= args
->len
;
646 ASSERT(be32_to_cpu(agf
->agf_freeblks
) <=
647 be32_to_cpu(agf
->agf_length
));
648 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
649 xfs_alloc_log_agf(args
->tp
, args
->agbp
,
651 /* search the busylist for these blocks */
652 xfs_alloc_search_busy(args
->tp
, args
->agno
,
653 args
->agbno
, args
->len
);
656 xfs_trans_mod_sb(args
->tp
,
657 args
->wasdel
? XFS_TRANS_SB_RES_FDBLOCKS
:
658 XFS_TRANS_SB_FDBLOCKS
, -slen
);
659 XFS_STATS_INC(xs_allocx
);
660 XFS_STATS_ADD(xs_allocb
, args
->len
);
666 * Allocate a variable extent at exactly agno/bno.
667 * Extent's length (returned in *len) will be between minlen and maxlen,
668 * and of the form k * prod + mod unless there's nothing that large.
669 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
671 STATIC
int /* error */
672 xfs_alloc_ag_vextent_exact(
673 xfs_alloc_arg_t
*args
) /* allocation argument structure */
675 xfs_btree_cur_t
*bno_cur
;/* by block-number btree cursor */
676 xfs_btree_cur_t
*cnt_cur
;/* by count btree cursor */
677 xfs_agblock_t end
; /* end of allocated extent */
679 xfs_agblock_t fbno
; /* start block of found extent */
680 xfs_agblock_t fend
; /* end block of found extent */
681 xfs_extlen_t flen
; /* length of found extent */
682 int i
; /* success/failure of operation */
683 xfs_agblock_t maxend
; /* end of maximal extent */
684 xfs_agblock_t minend
; /* end of minimal extent */
685 xfs_extlen_t rlen
; /* length of returned extent */
687 ASSERT(args
->alignment
== 1);
689 * Allocate/initialize a cursor for the by-number freespace btree.
691 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
692 args
->agno
, XFS_BTNUM_BNO
);
694 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
695 * Look for the closest free block <= bno, it must contain bno
696 * if any free block does.
698 if ((error
= xfs_alloc_lookup_le(bno_cur
, args
->agbno
, args
->minlen
, &i
)))
702 * Didn't find it, return null.
704 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
705 args
->agbno
= NULLAGBLOCK
;
709 * Grab the freespace record.
711 if ((error
= xfs_alloc_get_rec(bno_cur
, &fbno
, &flen
, &i
)))
713 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
714 ASSERT(fbno
<= args
->agbno
);
715 minend
= args
->agbno
+ args
->minlen
;
716 maxend
= args
->agbno
+ args
->maxlen
;
719 * Give up if the freespace isn't long enough for the minimum request.
722 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
723 args
->agbno
= NULLAGBLOCK
;
727 * End of extent will be smaller of the freespace end and the
728 * maximal requested end.
730 end
= XFS_AGBLOCK_MIN(fend
, maxend
);
732 * Fix the length according to mod and prod if given.
734 args
->len
= end
- args
->agbno
;
735 xfs_alloc_fix_len(args
);
736 if (!xfs_alloc_fix_minleft(args
)) {
737 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
741 ASSERT(args
->agbno
+ rlen
<= fend
);
742 end
= args
->agbno
+ rlen
;
744 * We are allocating agbno for rlen [agbno .. end]
745 * Allocate/initialize a cursor for the by-size btree.
747 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
748 args
->agno
, XFS_BTNUM_CNT
);
749 ASSERT(args
->agbno
+ args
->len
<=
750 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
751 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
752 args
->agbno
, args
->len
, XFSA_FIXUP_BNO_OK
))) {
753 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
756 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
757 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
758 TRACE_ALLOC("normal", args
);
763 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
764 TRACE_ALLOC("error", args
);
769 * Allocate a variable extent near bno in the allocation group agno.
770 * Extent's length (returned in len) will be between minlen and maxlen,
771 * and of the form k * prod + mod unless there's nothing that large.
772 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
774 STATIC
int /* error */
775 xfs_alloc_ag_vextent_near(
776 xfs_alloc_arg_t
*args
) /* allocation argument structure */
778 xfs_btree_cur_t
*bno_cur_gt
; /* cursor for bno btree, right side */
779 xfs_btree_cur_t
*bno_cur_lt
; /* cursor for bno btree, left side */
780 xfs_btree_cur_t
*cnt_cur
; /* cursor for count btree */
781 xfs_agblock_t gtbno
; /* start bno of right side entry */
782 xfs_agblock_t gtbnoa
; /* aligned ... */
783 xfs_extlen_t gtdiff
; /* difference to right side entry */
784 xfs_extlen_t gtlen
; /* length of right side entry */
785 xfs_extlen_t gtlena
; /* aligned ... */
786 xfs_agblock_t gtnew
; /* useful start bno of right side */
787 int error
; /* error code */
788 int i
; /* result code, temporary */
789 int j
; /* result code, temporary */
790 xfs_agblock_t ltbno
; /* start bno of left side entry */
791 xfs_agblock_t ltbnoa
; /* aligned ... */
792 xfs_extlen_t ltdiff
; /* difference to left side entry */
794 xfs_agblock_t ltend
; /* end bno of left side entry */
795 xfs_extlen_t ltlen
; /* length of left side entry */
796 xfs_extlen_t ltlena
; /* aligned ... */
797 xfs_agblock_t ltnew
; /* useful start bno of left side */
798 xfs_extlen_t rlen
; /* length of returned extent */
799 #if defined(DEBUG) && defined(__KERNEL__)
801 * Randomly don't execute the first algorithm.
803 int dofirst
; /* set to do first algorithm */
805 dofirst
= random32() & 1;
808 * Get a cursor for the by-size btree.
810 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
811 args
->agno
, XFS_BTNUM_CNT
);
813 bno_cur_lt
= bno_cur_gt
= NULL
;
815 * See if there are any free extents as big as maxlen.
817 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0, args
->maxlen
, &i
)))
820 * If none, then pick up the last entry in the tree unless the
824 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, <bno
,
827 if (i
== 0 || ltlen
== 0) {
828 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
836 * If the requested extent is large wrt the freespaces available
837 * in this a.g., then the cursor will be pointing to a btree entry
838 * near the right edge of the tree. If it's in the last btree leaf
839 * block, then we just examine all the entries in that block
840 * that are big enough, and pick the best one.
841 * This is written as a while loop so we can break out of it,
842 * but we never loop back to the top.
844 while (xfs_btree_islastblock(cnt_cur
, 0)) {
848 xfs_agblock_t bnew
=0;
850 #if defined(DEBUG) && defined(__KERNEL__)
855 * Start from the entry that lookup found, sequence through
856 * all larger free blocks. If we're actually pointing at a
857 * record smaller than maxlen, go to the start of this block,
858 * and skip all those smaller than minlen.
860 if (ltlen
|| args
->alignment
> 1) {
861 cnt_cur
->bc_ptrs
[0] = 1;
863 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
,
866 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
867 if (ltlen
>= args
->minlen
)
869 if ((error
= xfs_btree_increment(cnt_cur
, 0, &i
)))
872 ASSERT(ltlen
>= args
->minlen
);
876 i
= cnt_cur
->bc_ptrs
[0];
877 for (j
= 1, blen
= 0, bdiff
= 0;
878 !error
&& j
&& (blen
< args
->maxlen
|| bdiff
> 0);
879 error
= xfs_btree_increment(cnt_cur
, 0, &j
)) {
881 * For each entry, decide if it's better than
882 * the previous best entry.
884 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
886 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
887 xfs_alloc_compute_aligned(ltbno
, ltlen
, args
->alignment
,
888 args
->minlen
, <bnoa
, <lena
);
889 if (ltlena
< args
->minlen
)
891 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
892 xfs_alloc_fix_len(args
);
893 ASSERT(args
->len
>= args
->minlen
);
894 if (args
->len
< blen
)
896 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, args
->len
,
897 args
->alignment
, ltbno
, ltlen
, <new
);
898 if (ltnew
!= NULLAGBLOCK
&&
899 (args
->len
> blen
|| ltdiff
< bdiff
)) {
903 besti
= cnt_cur
->bc_ptrs
[0];
907 * It didn't work. We COULD be in a case where
908 * there's a good record somewhere, so try again.
913 * Point at the best entry, and retrieve it again.
915 cnt_cur
->bc_ptrs
[0] = besti
;
916 if ((error
= xfs_alloc_get_rec(cnt_cur
, <bno
, <len
, &i
)))
918 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
919 ltend
= ltbno
+ ltlen
;
920 ASSERT(ltend
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
922 if (!xfs_alloc_fix_minleft(args
)) {
923 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
924 TRACE_ALLOC("nominleft", args
);
929 * We are allocating starting at bnew for blen blocks.
932 ASSERT(bnew
>= ltbno
);
933 ASSERT(bnew
+ blen
<= ltend
);
935 * Set up a cursor for the by-bno tree.
937 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
,
938 args
->agbp
, args
->agno
, XFS_BTNUM_BNO
);
940 * Fix up the btree entries.
942 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
,
943 ltlen
, bnew
, blen
, XFSA_FIXUP_CNT_OK
)))
945 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
946 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
947 TRACE_ALLOC("first", args
);
952 * Search in the by-bno tree to the left and to the right
953 * simultaneously, until in each case we find a space big enough,
954 * or run into the edge of the tree. When we run into the edge,
955 * we deallocate that cursor.
956 * If both searches succeed, we compare the two spaces and pick
958 * With alignment, it's possible for both to fail; the upper
959 * level algorithm that picks allocation groups for allocations
960 * is not supposed to do this.
963 * Allocate and initialize the cursor for the leftward search.
965 bno_cur_lt
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
966 args
->agno
, XFS_BTNUM_BNO
);
968 * Lookup <= bno to find the leftward search's starting point.
970 if ((error
= xfs_alloc_lookup_le(bno_cur_lt
, args
->agbno
, args
->maxlen
, &i
)))
974 * Didn't find anything; use this cursor for the rightward
977 bno_cur_gt
= bno_cur_lt
;
981 * Found something. Duplicate the cursor for the rightward search.
983 else if ((error
= xfs_btree_dup_cursor(bno_cur_lt
, &bno_cur_gt
)))
986 * Increment the cursor, so we will point at the entry just right
987 * of the leftward entry if any, or to the leftmost entry.
989 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
993 * It failed, there are no rightward entries.
995 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_NOERROR
);
999 * Loop going left with the leftward cursor, right with the
1000 * rightward cursor, until either both directions give up or
1001 * we find an entry at least as big as minlen.
1005 if ((error
= xfs_alloc_get_rec(bno_cur_lt
, <bno
, <len
, &i
)))
1007 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1008 xfs_alloc_compute_aligned(ltbno
, ltlen
, args
->alignment
,
1009 args
->minlen
, <bnoa
, <lena
);
1010 if (ltlena
>= args
->minlen
)
1012 if ((error
= xfs_btree_decrement(bno_cur_lt
, 0, &i
)))
1015 xfs_btree_del_cursor(bno_cur_lt
,
1021 if ((error
= xfs_alloc_get_rec(bno_cur_gt
, >bno
, >len
, &i
)))
1023 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1024 xfs_alloc_compute_aligned(gtbno
, gtlen
, args
->alignment
,
1025 args
->minlen
, >bnoa
, >lena
);
1026 if (gtlena
>= args
->minlen
)
1028 if ((error
= xfs_btree_increment(bno_cur_gt
, 0, &i
)))
1031 xfs_btree_del_cursor(bno_cur_gt
,
1036 } while (bno_cur_lt
|| bno_cur_gt
);
1038 * Got both cursors still active, need to find better entry.
1040 if (bno_cur_lt
&& bno_cur_gt
) {
1042 * Left side is long enough, look for a right side entry.
1044 if (ltlena
>= args
->minlen
) {
1046 * Fix up the length.
1048 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1049 xfs_alloc_fix_len(args
);
1051 ltdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
1052 args
->alignment
, ltbno
, ltlen
, <new
);
1058 * Look until we find a better one, run out of
1059 * space, or run off the end.
1061 while (bno_cur_lt
&& bno_cur_gt
) {
1062 if ((error
= xfs_alloc_get_rec(
1066 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1067 xfs_alloc_compute_aligned(gtbno
, gtlen
,
1068 args
->alignment
, args
->minlen
,
1071 * The left one is clearly better.
1073 if (gtbnoa
>= args
->agbno
+ ltdiff
) {
1074 xfs_btree_del_cursor(
1081 * If we reach a big enough entry,
1082 * compare the two and pick the best.
1084 if (gtlena
>= args
->minlen
) {
1086 XFS_EXTLEN_MIN(gtlena
,
1088 xfs_alloc_fix_len(args
);
1090 gtdiff
= xfs_alloc_compute_diff(
1093 gtbno
, gtlen
, >new
);
1095 * Right side is better.
1097 if (gtdiff
< ltdiff
) {
1098 xfs_btree_del_cursor(
1104 * Left side is better.
1107 xfs_btree_del_cursor(
1115 * Fell off the right end.
1117 if ((error
= xfs_btree_increment(
1118 bno_cur_gt
, 0, &i
)))
1121 xfs_btree_del_cursor(
1130 * The left side is perfect, trash the right side.
1133 xfs_btree_del_cursor(bno_cur_gt
,
1139 * It's the right side that was found first, look left.
1143 * Fix up the length.
1145 args
->len
= XFS_EXTLEN_MIN(gtlena
, args
->maxlen
);
1146 xfs_alloc_fix_len(args
);
1148 gtdiff
= xfs_alloc_compute_diff(args
->agbno
, rlen
,
1149 args
->alignment
, gtbno
, gtlen
, >new
);
1151 * Right side entry isn't perfect.
1155 * Look until we find a better one, run out of
1156 * space, or run off the end.
1158 while (bno_cur_lt
&& bno_cur_gt
) {
1159 if ((error
= xfs_alloc_get_rec(
1163 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1164 xfs_alloc_compute_aligned(ltbno
, ltlen
,
1165 args
->alignment
, args
->minlen
,
1168 * The right one is clearly better.
1170 if (ltbnoa
<= args
->agbno
- gtdiff
) {
1171 xfs_btree_del_cursor(
1178 * If we reach a big enough entry,
1179 * compare the two and pick the best.
1181 if (ltlena
>= args
->minlen
) {
1182 args
->len
= XFS_EXTLEN_MIN(
1183 ltlena
, args
->maxlen
);
1184 xfs_alloc_fix_len(args
);
1186 ltdiff
= xfs_alloc_compute_diff(
1189 ltbno
, ltlen
, <new
);
1191 * Left side is better.
1193 if (ltdiff
< gtdiff
) {
1194 xfs_btree_del_cursor(
1200 * Right side is better.
1203 xfs_btree_del_cursor(
1211 * Fell off the left end.
1213 if ((error
= xfs_btree_decrement(
1214 bno_cur_lt
, 0, &i
)))
1217 xfs_btree_del_cursor(bno_cur_lt
,
1225 * The right side is perfect, trash the left side.
1228 xfs_btree_del_cursor(bno_cur_lt
,
1235 * If we couldn't get anything, give up.
1237 if (bno_cur_lt
== NULL
&& bno_cur_gt
== NULL
) {
1238 TRACE_ALLOC("neither", args
);
1239 args
->agbno
= NULLAGBLOCK
;
1243 * At this point we have selected a freespace entry, either to the
1244 * left or to the right. If it's on the right, copy all the
1245 * useful variables to the "left" set so we only have one
1246 * copy of this code.
1249 bno_cur_lt
= bno_cur_gt
;
1259 * Fix up the length and compute the useful address.
1261 ltend
= ltbno
+ ltlen
;
1262 args
->len
= XFS_EXTLEN_MIN(ltlena
, args
->maxlen
);
1263 xfs_alloc_fix_len(args
);
1264 if (!xfs_alloc_fix_minleft(args
)) {
1265 TRACE_ALLOC("nominleft", args
);
1266 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1267 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1271 (void)xfs_alloc_compute_diff(args
->agbno
, rlen
, args
->alignment
, ltbno
,
1273 ASSERT(ltnew
>= ltbno
);
1274 ASSERT(ltnew
+ rlen
<= ltend
);
1275 ASSERT(ltnew
+ rlen
<= be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
));
1276 args
->agbno
= ltnew
;
1277 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur_lt
, ltbno
, ltlen
,
1278 ltnew
, rlen
, XFSA_FIXUP_BNO_OK
)))
1280 TRACE_ALLOC(j
? "gt" : "lt", args
);
1281 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1282 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_NOERROR
);
1286 TRACE_ALLOC("error", args
);
1287 if (cnt_cur
!= NULL
)
1288 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1289 if (bno_cur_lt
!= NULL
)
1290 xfs_btree_del_cursor(bno_cur_lt
, XFS_BTREE_ERROR
);
1291 if (bno_cur_gt
!= NULL
)
1292 xfs_btree_del_cursor(bno_cur_gt
, XFS_BTREE_ERROR
);
1297 * Allocate a variable extent anywhere in the allocation group agno.
1298 * Extent's length (returned in len) will be between minlen and maxlen,
1299 * and of the form k * prod + mod unless there's nothing that large.
1300 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1302 STATIC
int /* error */
1303 xfs_alloc_ag_vextent_size(
1304 xfs_alloc_arg_t
*args
) /* allocation argument structure */
1306 xfs_btree_cur_t
*bno_cur
; /* cursor for bno btree */
1307 xfs_btree_cur_t
*cnt_cur
; /* cursor for cnt btree */
1308 int error
; /* error result */
1309 xfs_agblock_t fbno
; /* start of found freespace */
1310 xfs_extlen_t flen
; /* length of found freespace */
1311 int i
; /* temp status variable */
1312 xfs_agblock_t rbno
; /* returned block number */
1313 xfs_extlen_t rlen
; /* length of returned extent */
1316 * Allocate and initialize a cursor for the by-size btree.
1318 cnt_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1319 args
->agno
, XFS_BTNUM_CNT
);
1322 * Look for an entry >= maxlen+alignment-1 blocks.
1324 if ((error
= xfs_alloc_lookup_ge(cnt_cur
, 0,
1325 args
->maxlen
+ args
->alignment
- 1, &i
)))
1328 * If none, then pick up the last entry in the tree unless the
1332 if ((error
= xfs_alloc_ag_vextent_small(args
, cnt_cur
, &fbno
,
1335 if (i
== 0 || flen
== 0) {
1336 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1337 TRACE_ALLOC("noentry", args
);
1343 * There's a freespace as big as maxlen+alignment-1, get it.
1346 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
, &i
)))
1348 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1351 * In the first case above, we got the last entry in the
1352 * by-size btree. Now we check to see if the space hits maxlen
1353 * once aligned; if not, we search left for something better.
1354 * This can't happen in the second case above.
1356 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
, args
->minlen
,
1358 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1359 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1360 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
), error0
);
1361 if (rlen
< args
->maxlen
) {
1362 xfs_agblock_t bestfbno
;
1363 xfs_extlen_t bestflen
;
1364 xfs_agblock_t bestrbno
;
1365 xfs_extlen_t bestrlen
;
1372 if ((error
= xfs_btree_decrement(cnt_cur
, 0, &i
)))
1376 if ((error
= xfs_alloc_get_rec(cnt_cur
, &fbno
, &flen
,
1379 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1380 if (flen
< bestrlen
)
1382 xfs_alloc_compute_aligned(fbno
, flen
, args
->alignment
,
1383 args
->minlen
, &rbno
, &rlen
);
1384 rlen
= XFS_EXTLEN_MIN(args
->maxlen
, rlen
);
1385 XFS_WANT_CORRUPTED_GOTO(rlen
== 0 ||
1386 (rlen
<= flen
&& rbno
+ rlen
<= fbno
+ flen
),
1388 if (rlen
> bestrlen
) {
1393 if (rlen
== args
->maxlen
)
1397 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, bestfbno
, bestflen
,
1400 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1406 args
->wasfromfl
= 0;
1408 * Fix up the length.
1411 xfs_alloc_fix_len(args
);
1412 if (rlen
< args
->minlen
|| !xfs_alloc_fix_minleft(args
)) {
1413 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1414 TRACE_ALLOC("nominleft", args
);
1415 args
->agbno
= NULLAGBLOCK
;
1419 XFS_WANT_CORRUPTED_GOTO(rlen
<= flen
, error0
);
1421 * Allocate and initialize a cursor for the by-block tree.
1423 bno_cur
= xfs_allocbt_init_cursor(args
->mp
, args
->tp
, args
->agbp
,
1424 args
->agno
, XFS_BTNUM_BNO
);
1425 if ((error
= xfs_alloc_fixup_trees(cnt_cur
, bno_cur
, fbno
, flen
,
1426 rbno
, rlen
, XFSA_FIXUP_CNT_OK
)))
1428 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1429 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1430 cnt_cur
= bno_cur
= NULL
;
1433 XFS_WANT_CORRUPTED_GOTO(
1434 args
->agbno
+ args
->len
<=
1435 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1437 TRACE_ALLOC("normal", args
);
1441 TRACE_ALLOC("error", args
);
1443 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1445 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1450 * Deal with the case where only small freespaces remain.
1451 * Either return the contents of the last freespace record,
1452 * or allocate space from the freelist if there is nothing in the tree.
1454 STATIC
int /* error */
1455 xfs_alloc_ag_vextent_small(
1456 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1457 xfs_btree_cur_t
*ccur
, /* by-size cursor */
1458 xfs_agblock_t
*fbnop
, /* result block number */
1459 xfs_extlen_t
*flenp
, /* result length */
1460 int *stat
) /* status: 0-freelist, 1-normal/none */
1467 if ((error
= xfs_btree_decrement(ccur
, 0, &i
)))
1470 if ((error
= xfs_alloc_get_rec(ccur
, &fbno
, &flen
, &i
)))
1472 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1475 * Nothing in the btree, try the freelist. Make sure
1476 * to respect minleft even when pulling from the
1479 else if (args
->minlen
== 1 && args
->alignment
== 1 && !args
->isfl
&&
1480 (be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_flcount
)
1482 error
= xfs_alloc_get_freelist(args
->tp
, args
->agbp
, &fbno
, 0);
1485 if (fbno
!= NULLAGBLOCK
) {
1486 if (args
->userdata
) {
1489 bp
= xfs_btree_get_bufs(args
->mp
, args
->tp
,
1490 args
->agno
, fbno
, 0);
1491 xfs_trans_binval(args
->tp
, bp
);
1495 XFS_WANT_CORRUPTED_GOTO(
1496 args
->agbno
+ args
->len
<=
1497 be32_to_cpu(XFS_BUF_TO_AGF(args
->agbp
)->agf_length
),
1499 args
->wasfromfl
= 1;
1500 TRACE_ALLOC("freelist", args
);
1505 * Nothing in the freelist.
1511 * Can't allocate from the freelist for some reason.
1518 * Can't do the allocation, give up.
1520 if (flen
< args
->minlen
) {
1521 args
->agbno
= NULLAGBLOCK
;
1522 TRACE_ALLOC("notenough", args
);
1528 TRACE_ALLOC("normal", args
);
1532 TRACE_ALLOC("error", args
);
1537 * Free the extent starting at agno/bno for length.
1539 STATIC
int /* error */
1541 xfs_trans_t
*tp
, /* transaction pointer */
1542 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
1543 xfs_agnumber_t agno
, /* allocation group number */
1544 xfs_agblock_t bno
, /* starting block number */
1545 xfs_extlen_t len
, /* length of extent */
1546 int isfl
) /* set if is freelist blocks - no sb acctg */
1548 xfs_btree_cur_t
*bno_cur
; /* cursor for by-block btree */
1549 xfs_btree_cur_t
*cnt_cur
; /* cursor for by-size btree */
1550 int error
; /* error return value */
1551 xfs_agblock_t gtbno
; /* start of right neighbor block */
1552 xfs_extlen_t gtlen
; /* length of right neighbor block */
1553 int haveleft
; /* have a left neighbor block */
1554 int haveright
; /* have a right neighbor block */
1555 int i
; /* temp, result code */
1556 xfs_agblock_t ltbno
; /* start of left neighbor block */
1557 xfs_extlen_t ltlen
; /* length of left neighbor block */
1558 xfs_mount_t
*mp
; /* mount point struct for filesystem */
1559 xfs_agblock_t nbno
; /* new starting block of freespace */
1560 xfs_extlen_t nlen
; /* new length of freespace */
1564 * Allocate and initialize a cursor for the by-block btree.
1566 bno_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_BNO
);
1569 * Look for a neighboring block on the left (lower block numbers)
1570 * that is contiguous with this space.
1572 if ((error
= xfs_alloc_lookup_le(bno_cur
, bno
, len
, &haveleft
)))
1576 * There is a block to our left.
1578 if ((error
= xfs_alloc_get_rec(bno_cur
, <bno
, <len
, &i
)))
1580 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1582 * It's not contiguous, though.
1584 if (ltbno
+ ltlen
< bno
)
1588 * If this failure happens the request to free this
1589 * space was invalid, it's (partly) already free.
1592 XFS_WANT_CORRUPTED_GOTO(ltbno
+ ltlen
<= bno
, error0
);
1596 * Look for a neighboring block on the right (higher block numbers)
1597 * that is contiguous with this space.
1599 if ((error
= xfs_btree_increment(bno_cur
, 0, &haveright
)))
1603 * There is a block to our right.
1605 if ((error
= xfs_alloc_get_rec(bno_cur
, >bno
, >len
, &i
)))
1607 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1609 * It's not contiguous, though.
1611 if (bno
+ len
< gtbno
)
1615 * If this failure happens the request to free this
1616 * space was invalid, it's (partly) already free.
1619 XFS_WANT_CORRUPTED_GOTO(gtbno
>= bno
+ len
, error0
);
1623 * Now allocate and initialize a cursor for the by-size tree.
1625 cnt_cur
= xfs_allocbt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_CNT
);
1627 * Have both left and right contiguous neighbors.
1628 * Merge all three into a single free block.
1630 if (haveleft
&& haveright
) {
1632 * Delete the old by-size entry on the left.
1634 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1636 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1637 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1639 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1641 * Delete the old by-size entry on the right.
1643 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1645 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1646 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1648 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1650 * Delete the old by-block entry for the right block.
1652 if ((error
= xfs_alloc_delete(bno_cur
, &i
)))
1654 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1656 * Move the by-block cursor back to the left neighbor.
1658 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1660 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1663 * Check that this is the right record: delete didn't
1664 * mangle the cursor.
1667 xfs_agblock_t xxbno
;
1670 if ((error
= xfs_alloc_get_rec(bno_cur
, &xxbno
, &xxlen
,
1673 XFS_WANT_CORRUPTED_GOTO(
1674 i
== 1 && xxbno
== ltbno
&& xxlen
== ltlen
,
1679 * Update remaining by-block entry to the new, joined block.
1682 nlen
= len
+ ltlen
+ gtlen
;
1683 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1687 * Have only a left contiguous neighbor.
1688 * Merge it together with the new freespace.
1690 else if (haveleft
) {
1692 * Delete the old by-size entry on the left.
1694 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, ltbno
, ltlen
, &i
)))
1696 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1697 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1699 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1701 * Back up the by-block cursor to the left neighbor, and
1702 * update its length.
1704 if ((error
= xfs_btree_decrement(bno_cur
, 0, &i
)))
1706 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1709 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1713 * Have only a right contiguous neighbor.
1714 * Merge it together with the new freespace.
1716 else if (haveright
) {
1718 * Delete the old by-size entry on the right.
1720 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, gtbno
, gtlen
, &i
)))
1722 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1723 if ((error
= xfs_alloc_delete(cnt_cur
, &i
)))
1725 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1727 * Update the starting block and length of the right
1728 * neighbor in the by-block tree.
1732 if ((error
= xfs_alloc_update(bno_cur
, nbno
, nlen
)))
1736 * No contiguous neighbors.
1737 * Insert the new freespace into the by-block tree.
1742 if ((error
= xfs_alloc_insert(bno_cur
, &i
)))
1744 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1746 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_NOERROR
);
1749 * In all cases we need to insert the new freespace in the by-size tree.
1751 if ((error
= xfs_alloc_lookup_eq(cnt_cur
, nbno
, nlen
, &i
)))
1753 XFS_WANT_CORRUPTED_GOTO(i
== 0, error0
);
1754 if ((error
= xfs_alloc_insert(cnt_cur
, &i
)))
1756 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1757 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_NOERROR
);
1760 * Update the freespace totals in the ag and superblock.
1764 xfs_perag_t
*pag
; /* per allocation group data */
1766 agf
= XFS_BUF_TO_AGF(agbp
);
1767 pag
= &mp
->m_perag
[agno
];
1768 be32_add_cpu(&agf
->agf_freeblks
, len
);
1769 xfs_trans_agblocks_delta(tp
, len
);
1770 pag
->pagf_freeblks
+= len
;
1771 XFS_WANT_CORRUPTED_GOTO(
1772 be32_to_cpu(agf
->agf_freeblks
) <=
1773 be32_to_cpu(agf
->agf_length
),
1775 TRACE_MODAGF(NULL
, agf
, XFS_AGF_FREEBLKS
);
1776 xfs_alloc_log_agf(tp
, agbp
, XFS_AGF_FREEBLKS
);
1778 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FDBLOCKS
, (long)len
);
1779 XFS_STATS_INC(xs_freex
);
1780 XFS_STATS_ADD(xs_freeb
, len
);
1782 TRACE_FREE(haveleft
?
1783 (haveright
? "both" : "left") :
1784 (haveright
? "right" : "none"),
1785 agno
, bno
, len
, isfl
);
1788 * Since blocks move to the free list without the coordination
1789 * used in xfs_bmap_finish, we can't allow block to be available
1790 * for reallocation and non-transaction writing (user data)
1791 * until we know that the transaction that moved it to the free
1792 * list is permanently on disk. We track the blocks by declaring
1793 * these blocks as "busy"; the busy list is maintained on a per-ag
1794 * basis and each transaction records which entries should be removed
1795 * when the iclog commits to disk. If a busy block is allocated,
1796 * the iclog is pushed up to the LSN that freed the block.
1798 xfs_alloc_mark_busy(tp
, agno
, bno
, len
);
1802 TRACE_FREE("error", agno
, bno
, len
, isfl
);
1804 xfs_btree_del_cursor(bno_cur
, XFS_BTREE_ERROR
);
1806 xfs_btree_del_cursor(cnt_cur
, XFS_BTREE_ERROR
);
1811 * Visible (exported) allocation/free functions.
1812 * Some of these are used just by xfs_alloc_btree.c and this file.
1816 * Compute and fill in value of m_ag_maxlevels.
1819 xfs_alloc_compute_maxlevels(
1820 xfs_mount_t
*mp
) /* file system mount structure */
1828 maxleafents
= (mp
->m_sb
.sb_agblocks
+ 1) / 2;
1829 minleafrecs
= mp
->m_alloc_mnr
[0];
1830 minnoderecs
= mp
->m_alloc_mnr
[1];
1831 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1832 for (level
= 1; maxblocks
> 1; level
++)
1833 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1834 mp
->m_ag_maxlevels
= level
;
1838 * Decide whether to use this allocation group for this allocation.
1839 * If so, fix up the btree freelist's size.
1841 STATIC
int /* error */
1842 xfs_alloc_fix_freelist(
1843 xfs_alloc_arg_t
*args
, /* allocation argument structure */
1844 int flags
) /* XFS_ALLOC_FLAG_... */
1846 xfs_buf_t
*agbp
; /* agf buffer pointer */
1847 xfs_agf_t
*agf
; /* a.g. freespace structure pointer */
1848 xfs_buf_t
*agflbp
;/* agfl buffer pointer */
1849 xfs_agblock_t bno
; /* freelist block */
1850 xfs_extlen_t delta
; /* new blocks needed in freelist */
1851 int error
; /* error result code */
1852 xfs_extlen_t longest
;/* longest extent in allocation group */
1853 xfs_mount_t
*mp
; /* file system mount point structure */
1854 xfs_extlen_t need
; /* total blocks needed in freelist */
1855 xfs_perag_t
*pag
; /* per-ag information structure */
1856 xfs_alloc_arg_t targs
; /* local allocation arguments */
1857 xfs_trans_t
*tp
; /* transaction pointer */
1863 if (!pag
->pagf_init
) {
1864 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1867 if (!pag
->pagf_init
) {
1868 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
1869 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
1877 * If this is a metadata preferred pag and we are user data
1878 * then try somewhere else if we are not being asked to
1879 * try harder at this point
1881 if (pag
->pagf_metadata
&& args
->userdata
&&
1882 (flags
& XFS_ALLOC_FLAG_TRYLOCK
)) {
1883 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
1888 if (!(flags
& XFS_ALLOC_FLAG_FREEING
)) {
1889 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
1890 delta
= need
> pag
->pagf_flcount
? need
- pag
->pagf_flcount
: 0;
1892 * If it looks like there isn't a long enough extent, or enough
1893 * total blocks, reject it.
1895 longest
= (pag
->pagf_longest
> delta
) ?
1896 (pag
->pagf_longest
- delta
) :
1897 (pag
->pagf_flcount
> 0 || pag
->pagf_longest
> 0);
1898 if ((args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1) >
1900 ((int)(pag
->pagf_freeblks
+ pag
->pagf_flcount
-
1901 need
- args
->total
) < (int)args
->minleft
)) {
1903 xfs_trans_brelse(tp
, agbp
);
1910 * Get the a.g. freespace buffer.
1911 * Can fail if we're not blocking on locks, and it's held.
1914 if ((error
= xfs_alloc_read_agf(mp
, tp
, args
->agno
, flags
,
1918 ASSERT(flags
& XFS_ALLOC_FLAG_TRYLOCK
);
1919 ASSERT(!(flags
& XFS_ALLOC_FLAG_FREEING
));
1925 * Figure out how many blocks we should have in the freelist.
1927 agf
= XFS_BUF_TO_AGF(agbp
);
1928 need
= XFS_MIN_FREELIST(agf
, mp
);
1930 * If there isn't enough total or single-extent, reject it.
1932 if (!(flags
& XFS_ALLOC_FLAG_FREEING
)) {
1933 delta
= need
> be32_to_cpu(agf
->agf_flcount
) ?
1934 (need
- be32_to_cpu(agf
->agf_flcount
)) : 0;
1935 longest
= be32_to_cpu(agf
->agf_longest
);
1936 longest
= (longest
> delta
) ? (longest
- delta
) :
1937 (be32_to_cpu(agf
->agf_flcount
) > 0 || longest
> 0);
1938 if ((args
->minlen
+ args
->alignment
+ args
->minalignslop
- 1) >
1940 ((int)(be32_to_cpu(agf
->agf_freeblks
) +
1941 be32_to_cpu(agf
->agf_flcount
) - need
- args
->total
) <
1942 (int)args
->minleft
)) {
1943 xfs_trans_brelse(tp
, agbp
);
1949 * Make the freelist shorter if it's too long.
1951 while (be32_to_cpu(agf
->agf_flcount
) > need
) {
1954 error
= xfs_alloc_get_freelist(tp
, agbp
, &bno
, 0);
1957 if ((error
= xfs_free_ag_extent(tp
, agbp
, args
->agno
, bno
, 1, 1)))
1959 bp
= xfs_btree_get_bufs(mp
, tp
, args
->agno
, bno
, 0);
1960 xfs_trans_binval(tp
, bp
);
1963 * Initialize the args structure.
1968 targs
.agno
= args
->agno
;
1969 targs
.mod
= targs
.minleft
= targs
.wasdel
= targs
.userdata
=
1970 targs
.minalignslop
= 0;
1971 targs
.alignment
= targs
.minlen
= targs
.prod
= targs
.isfl
= 1;
1972 targs
.type
= XFS_ALLOCTYPE_THIS_AG
;
1974 if ((error
= xfs_alloc_read_agfl(mp
, tp
, targs
.agno
, &agflbp
)))
1977 * Make the freelist longer if it's too short.
1979 while (be32_to_cpu(agf
->agf_flcount
) < need
) {
1981 targs
.maxlen
= need
- be32_to_cpu(agf
->agf_flcount
);
1983 * Allocate as many blocks as possible at once.
1985 if ((error
= xfs_alloc_ag_vextent(&targs
))) {
1986 xfs_trans_brelse(tp
, agflbp
);
1990 * Stop if we run out. Won't happen if callers are obeying
1991 * the restrictions correctly. Can happen for free calls
1992 * on a completely full ag.
1994 if (targs
.agbno
== NULLAGBLOCK
) {
1995 if (flags
& XFS_ALLOC_FLAG_FREEING
)
1997 xfs_trans_brelse(tp
, agflbp
);
2002 * Put each allocated block on the list.
2004 for (bno
= targs
.agbno
; bno
< targs
.agbno
+ targs
.len
; bno
++) {
2005 error
= xfs_alloc_put_freelist(tp
, agbp
,
2011 xfs_trans_brelse(tp
, agflbp
);
2017 * Get a block from the freelist.
2018 * Returns with the buffer for the block gotten.
2021 xfs_alloc_get_freelist(
2022 xfs_trans_t
*tp
, /* transaction pointer */
2023 xfs_buf_t
*agbp
, /* buffer containing the agf structure */
2024 xfs_agblock_t
*bnop
, /* block address retrieved from freelist */
2025 int btreeblk
) /* destination is a AGF btree */
2027 xfs_agf_t
*agf
; /* a.g. freespace structure */
2028 xfs_agfl_t
*agfl
; /* a.g. freelist structure */
2029 xfs_buf_t
*agflbp
;/* buffer for a.g. freelist structure */
2030 xfs_agblock_t bno
; /* block number returned */
2033 xfs_mount_t
*mp
; /* mount structure */
2034 xfs_perag_t
*pag
; /* per allocation group data */
2036 agf
= XFS_BUF_TO_AGF(agbp
);
2038 * Freelist is empty, give up.
2040 if (!agf
->agf_flcount
) {
2041 *bnop
= NULLAGBLOCK
;
2045 * Read the array of free blocks.
2048 if ((error
= xfs_alloc_read_agfl(mp
, tp
,
2049 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2051 agfl
= XFS_BUF_TO_AGFL(agflbp
);
2053 * Get the block number and update the data structures.
2055 bno
= be32_to_cpu(agfl
->agfl_bno
[be32_to_cpu(agf
->agf_flfirst
)]);
2056 be32_add_cpu(&agf
->agf_flfirst
, 1);
2057 xfs_trans_brelse(tp
, agflbp
);
2058 if (be32_to_cpu(agf
->agf_flfirst
) == XFS_AGFL_SIZE(mp
))
2059 agf
->agf_flfirst
= 0;
2060 pag
= &mp
->m_perag
[be32_to_cpu(agf
->agf_seqno
)];
2061 be32_add_cpu(&agf
->agf_flcount
, -1);
2062 xfs_trans_agflist_delta(tp
, -1);
2063 pag
->pagf_flcount
--;
2065 logflags
= XFS_AGF_FLFIRST
| XFS_AGF_FLCOUNT
;
2067 be32_add_cpu(&agf
->agf_btreeblks
, 1);
2068 pag
->pagf_btreeblks
++;
2069 logflags
|= XFS_AGF_BTREEBLKS
;
2072 TRACE_MODAGF(NULL
, agf
, logflags
);
2073 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2077 * As blocks are freed, they are added to the per-ag busy list
2078 * and remain there until the freeing transaction is committed to
2079 * disk. Now that we have allocated blocks, this list must be
2080 * searched to see if a block is being reused. If one is, then
2081 * the freeing transaction must be pushed to disk NOW by forcing
2082 * to disk all iclogs up that transaction's LSN.
2084 xfs_alloc_search_busy(tp
, be32_to_cpu(agf
->agf_seqno
), bno
, 1);
2089 * Log the given fields from the agf structure.
2093 xfs_trans_t
*tp
, /* transaction pointer */
2094 xfs_buf_t
*bp
, /* buffer for a.g. freelist header */
2095 int fields
) /* mask of fields to be logged (XFS_AGF_...) */
2097 int first
; /* first byte offset */
2098 int last
; /* last byte offset */
2099 static const short offsets
[] = {
2100 offsetof(xfs_agf_t
, agf_magicnum
),
2101 offsetof(xfs_agf_t
, agf_versionnum
),
2102 offsetof(xfs_agf_t
, agf_seqno
),
2103 offsetof(xfs_agf_t
, agf_length
),
2104 offsetof(xfs_agf_t
, agf_roots
[0]),
2105 offsetof(xfs_agf_t
, agf_levels
[0]),
2106 offsetof(xfs_agf_t
, agf_flfirst
),
2107 offsetof(xfs_agf_t
, agf_fllast
),
2108 offsetof(xfs_agf_t
, agf_flcount
),
2109 offsetof(xfs_agf_t
, agf_freeblks
),
2110 offsetof(xfs_agf_t
, agf_longest
),
2111 offsetof(xfs_agf_t
, agf_btreeblks
),
2115 xfs_btree_offsets(fields
, offsets
, XFS_AGF_NUM_BITS
, &first
, &last
);
2116 xfs_trans_log_buf(tp
, bp
, (uint
)first
, (uint
)last
);
2120 * Interface for inode allocation to force the pag data to be initialized.
2123 xfs_alloc_pagf_init(
2124 xfs_mount_t
*mp
, /* file system mount structure */
2125 xfs_trans_t
*tp
, /* transaction pointer */
2126 xfs_agnumber_t agno
, /* allocation group number */
2127 int flags
) /* XFS_ALLOC_FLAGS_... */
2132 if ((error
= xfs_alloc_read_agf(mp
, tp
, agno
, flags
, &bp
)))
2135 xfs_trans_brelse(tp
, bp
);
2140 * Put the block on the freelist for the allocation group.
2143 xfs_alloc_put_freelist(
2144 xfs_trans_t
*tp
, /* transaction pointer */
2145 xfs_buf_t
*agbp
, /* buffer for a.g. freelist header */
2146 xfs_buf_t
*agflbp
,/* buffer for a.g. free block array */
2147 xfs_agblock_t bno
, /* block being freed */
2148 int btreeblk
) /* block came from a AGF btree */
2150 xfs_agf_t
*agf
; /* a.g. freespace structure */
2151 xfs_agfl_t
*agfl
; /* a.g. free block array */
2152 __be32
*blockp
;/* pointer to array entry */
2155 xfs_mount_t
*mp
; /* mount structure */
2156 xfs_perag_t
*pag
; /* per allocation group data */
2158 agf
= XFS_BUF_TO_AGF(agbp
);
2161 if (!agflbp
&& (error
= xfs_alloc_read_agfl(mp
, tp
,
2162 be32_to_cpu(agf
->agf_seqno
), &agflbp
)))
2164 agfl
= XFS_BUF_TO_AGFL(agflbp
);
2165 be32_add_cpu(&agf
->agf_fllast
, 1);
2166 if (be32_to_cpu(agf
->agf_fllast
) == XFS_AGFL_SIZE(mp
))
2167 agf
->agf_fllast
= 0;
2168 pag
= &mp
->m_perag
[be32_to_cpu(agf
->agf_seqno
)];
2169 be32_add_cpu(&agf
->agf_flcount
, 1);
2170 xfs_trans_agflist_delta(tp
, 1);
2171 pag
->pagf_flcount
++;
2173 logflags
= XFS_AGF_FLLAST
| XFS_AGF_FLCOUNT
;
2175 be32_add_cpu(&agf
->agf_btreeblks
, -1);
2176 pag
->pagf_btreeblks
--;
2177 logflags
|= XFS_AGF_BTREEBLKS
;
2180 TRACE_MODAGF(NULL
, agf
, logflags
);
2181 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2183 ASSERT(be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
));
2184 blockp
= &agfl
->agfl_bno
[be32_to_cpu(agf
->agf_fllast
)];
2185 *blockp
= cpu_to_be32(bno
);
2186 TRACE_MODAGF(NULL
, agf
, logflags
);
2187 xfs_alloc_log_agf(tp
, agbp
, logflags
);
2188 xfs_trans_log_buf(tp
, agflbp
,
2189 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
),
2190 (int)((xfs_caddr_t
)blockp
- (xfs_caddr_t
)agfl
+
2191 sizeof(xfs_agblock_t
) - 1));
2196 * Read in the allocation group header (free/alloc section).
2200 xfs_mount_t
*mp
, /* mount point structure */
2201 xfs_trans_t
*tp
, /* transaction pointer */
2202 xfs_agnumber_t agno
, /* allocation group number */
2203 int flags
, /* XFS_ALLOC_FLAG_... */
2204 xfs_buf_t
**bpp
) /* buffer for the ag freelist header */
2206 xfs_agf_t
*agf
; /* ag freelist header */
2207 int agf_ok
; /* set if agf is consistent */
2208 xfs_buf_t
*bp
; /* return value */
2209 xfs_perag_t
*pag
; /* per allocation group data */
2212 ASSERT(agno
!= NULLAGNUMBER
);
2213 error
= xfs_trans_read_buf(
2214 mp
, tp
, mp
->m_ddev_targp
,
2215 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2216 XFS_FSS_TO_BB(mp
, 1),
2217 (flags
& XFS_ALLOC_FLAG_TRYLOCK
) ? XFS_BUF_TRYLOCK
: 0U,
2221 ASSERT(!bp
|| !XFS_BUF_GETERROR(bp
));
2227 * Validate the magic number of the agf block.
2229 agf
= XFS_BUF_TO_AGF(bp
);
2231 be32_to_cpu(agf
->agf_magicnum
) == XFS_AGF_MAGIC
&&
2232 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
)) &&
2233 be32_to_cpu(agf
->agf_freeblks
) <= be32_to_cpu(agf
->agf_length
) &&
2234 be32_to_cpu(agf
->agf_flfirst
) < XFS_AGFL_SIZE(mp
) &&
2235 be32_to_cpu(agf
->agf_fllast
) < XFS_AGFL_SIZE(mp
) &&
2236 be32_to_cpu(agf
->agf_flcount
) <= XFS_AGFL_SIZE(mp
);
2237 if (unlikely(XFS_TEST_ERROR(!agf_ok
, mp
, XFS_ERRTAG_ALLOC_READ_AGF
,
2238 XFS_RANDOM_ALLOC_READ_AGF
))) {
2239 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2240 XFS_ERRLEVEL_LOW
, mp
, agf
);
2241 xfs_trans_brelse(tp
, bp
);
2242 return XFS_ERROR(EFSCORRUPTED
);
2244 pag
= &mp
->m_perag
[agno
];
2245 if (!pag
->pagf_init
) {
2246 pag
->pagf_freeblks
= be32_to_cpu(agf
->agf_freeblks
);
2247 pag
->pagf_btreeblks
= be32_to_cpu(agf
->agf_btreeblks
);
2248 pag
->pagf_flcount
= be32_to_cpu(agf
->agf_flcount
);
2249 pag
->pagf_longest
= be32_to_cpu(agf
->agf_longest
);
2250 pag
->pagf_levels
[XFS_BTNUM_BNOi
] =
2251 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]);
2252 pag
->pagf_levels
[XFS_BTNUM_CNTi
] =
2253 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]);
2254 spin_lock_init(&pag
->pagb_lock
);
2255 pag
->pagb_list
= kmem_zalloc(XFS_PAGB_NUM_SLOTS
*
2256 sizeof(xfs_perag_busy_t
), KM_SLEEP
);
2260 else if (!XFS_FORCED_SHUTDOWN(mp
)) {
2261 ASSERT(pag
->pagf_freeblks
== be32_to_cpu(agf
->agf_freeblks
));
2262 ASSERT(pag
->pagf_flcount
== be32_to_cpu(agf
->agf_flcount
));
2263 ASSERT(pag
->pagf_longest
== be32_to_cpu(agf
->agf_longest
));
2264 ASSERT(pag
->pagf_levels
[XFS_BTNUM_BNOi
] ==
2265 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNOi
]));
2266 ASSERT(pag
->pagf_levels
[XFS_BTNUM_CNTi
] ==
2267 be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNTi
]));
2270 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGF
, XFS_AGF_REF
);
2276 * Allocate an extent (variable-size).
2277 * Depending on the allocation type, we either look in a single allocation
2278 * group or loop over the allocation groups to find the result.
2282 xfs_alloc_arg_t
*args
) /* allocation argument structure */
2284 xfs_agblock_t agsize
; /* allocation group size */
2286 int flags
; /* XFS_ALLOC_FLAG_... locking flags */
2287 xfs_extlen_t minleft
;/* minimum left value, temp copy */
2288 xfs_mount_t
*mp
; /* mount structure pointer */
2289 xfs_agnumber_t sagno
; /* starting allocation group number */
2290 xfs_alloctype_t type
; /* input allocation type */
2293 xfs_agnumber_t rotorstep
= xfs_rotorstep
; /* inode32 agf stepper */
2296 type
= args
->otype
= args
->type
;
2297 args
->agbno
= NULLAGBLOCK
;
2299 * Just fix this up, for the case where the last a.g. is shorter
2300 * (or there's only one a.g.) and the caller couldn't easily figure
2301 * that out (xfs_bmap_alloc).
2303 agsize
= mp
->m_sb
.sb_agblocks
;
2304 if (args
->maxlen
> agsize
)
2305 args
->maxlen
= agsize
;
2306 if (args
->alignment
== 0)
2307 args
->alignment
= 1;
2308 ASSERT(XFS_FSB_TO_AGNO(mp
, args
->fsbno
) < mp
->m_sb
.sb_agcount
);
2309 ASSERT(XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) < agsize
);
2310 ASSERT(args
->minlen
<= args
->maxlen
);
2311 ASSERT(args
->minlen
<= agsize
);
2312 ASSERT(args
->mod
< args
->prod
);
2313 if (XFS_FSB_TO_AGNO(mp
, args
->fsbno
) >= mp
->m_sb
.sb_agcount
||
2314 XFS_FSB_TO_AGBNO(mp
, args
->fsbno
) >= agsize
||
2315 args
->minlen
> args
->maxlen
|| args
->minlen
> agsize
||
2316 args
->mod
>= args
->prod
) {
2317 args
->fsbno
= NULLFSBLOCK
;
2318 TRACE_ALLOC("badargs", args
);
2321 minleft
= args
->minleft
;
2324 case XFS_ALLOCTYPE_THIS_AG
:
2325 case XFS_ALLOCTYPE_NEAR_BNO
:
2326 case XFS_ALLOCTYPE_THIS_BNO
:
2328 * These three force us into a single a.g.
2330 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2331 down_read(&mp
->m_peraglock
);
2332 args
->pag
= &mp
->m_perag
[args
->agno
];
2334 error
= xfs_alloc_fix_freelist(args
, 0);
2335 args
->minleft
= minleft
;
2337 TRACE_ALLOC("nofix", args
);
2341 up_read(&mp
->m_peraglock
);
2342 TRACE_ALLOC("noagbp", args
);
2345 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2346 if ((error
= xfs_alloc_ag_vextent(args
)))
2348 up_read(&mp
->m_peraglock
);
2350 case XFS_ALLOCTYPE_START_BNO
:
2352 * Try near allocation first, then anywhere-in-ag after
2353 * the first a.g. fails.
2355 if ((args
->userdata
== XFS_ALLOC_INITIAL_USER_DATA
) &&
2356 (mp
->m_flags
& XFS_MOUNT_32BITINODES
)) {
2357 args
->fsbno
= XFS_AGB_TO_FSB(mp
,
2358 ((mp
->m_agfrotor
/ rotorstep
) %
2359 mp
->m_sb
.sb_agcount
), 0);
2362 args
->agbno
= XFS_FSB_TO_AGBNO(mp
, args
->fsbno
);
2363 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2365 case XFS_ALLOCTYPE_ANY_AG
:
2366 case XFS_ALLOCTYPE_START_AG
:
2367 case XFS_ALLOCTYPE_FIRST_AG
:
2369 * Rotate through the allocation groups looking for a winner.
2371 if (type
== XFS_ALLOCTYPE_ANY_AG
) {
2373 * Start with the last place we left off.
2375 args
->agno
= sagno
= (mp
->m_agfrotor
/ rotorstep
) %
2376 mp
->m_sb
.sb_agcount
;
2377 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2378 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2379 } else if (type
== XFS_ALLOCTYPE_FIRST_AG
) {
2381 * Start with allocation group given by bno.
2383 args
->agno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2384 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2388 if (type
== XFS_ALLOCTYPE_START_AG
)
2389 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2391 * Start with the given allocation group.
2393 args
->agno
= sagno
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2394 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
2397 * Loop over allocation groups twice; first time with
2398 * trylock set, second time without.
2400 down_read(&mp
->m_peraglock
);
2402 args
->pag
= &mp
->m_perag
[args
->agno
];
2403 if (no_min
) args
->minleft
= 0;
2404 error
= xfs_alloc_fix_freelist(args
, flags
);
2405 args
->minleft
= minleft
;
2407 TRACE_ALLOC("nofix", args
);
2411 * If we get a buffer back then the allocation will fly.
2414 if ((error
= xfs_alloc_ag_vextent(args
)))
2418 TRACE_ALLOC("loopfailed", args
);
2420 * Didn't work, figure out the next iteration.
2422 if (args
->agno
== sagno
&&
2423 type
== XFS_ALLOCTYPE_START_BNO
)
2424 args
->type
= XFS_ALLOCTYPE_THIS_AG
;
2426 * For the first allocation, we can try any AG to get
2427 * space. However, if we already have allocated a
2428 * block, we don't want to try AGs whose number is below
2429 * sagno. Otherwise, we may end up with out-of-order
2430 * locking of AGF, which might cause deadlock.
2432 if (++(args
->agno
) == mp
->m_sb
.sb_agcount
) {
2433 if (args
->firstblock
!= NULLFSBLOCK
)
2439 * Reached the starting a.g., must either be done
2440 * or switch to non-trylock mode.
2442 if (args
->agno
== sagno
) {
2444 args
->agbno
= NULLAGBLOCK
;
2445 TRACE_ALLOC("allfailed", args
);
2452 if (type
== XFS_ALLOCTYPE_START_BNO
) {
2453 args
->agbno
= XFS_FSB_TO_AGBNO(mp
,
2455 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2460 up_read(&mp
->m_peraglock
);
2461 if (bump_rotor
|| (type
== XFS_ALLOCTYPE_ANY_AG
)) {
2462 if (args
->agno
== sagno
)
2463 mp
->m_agfrotor
= (mp
->m_agfrotor
+ 1) %
2464 (mp
->m_sb
.sb_agcount
* rotorstep
);
2466 mp
->m_agfrotor
= (args
->agno
* rotorstep
+ 1) %
2467 (mp
->m_sb
.sb_agcount
* rotorstep
);
2474 if (args
->agbno
== NULLAGBLOCK
)
2475 args
->fsbno
= NULLFSBLOCK
;
2477 args
->fsbno
= XFS_AGB_TO_FSB(mp
, args
->agno
, args
->agbno
);
2479 ASSERT(args
->len
>= args
->minlen
);
2480 ASSERT(args
->len
<= args
->maxlen
);
2481 ASSERT(args
->agbno
% args
->alignment
== 0);
2482 XFS_AG_CHECK_DADDR(mp
, XFS_FSB_TO_DADDR(mp
, args
->fsbno
),
2488 up_read(&mp
->m_peraglock
);
2494 * Just break up the extent address and hand off to xfs_free_ag_extent
2495 * after fixing up the freelist.
2499 xfs_trans_t
*tp
, /* transaction pointer */
2500 xfs_fsblock_t bno
, /* starting block number of extent */
2501 xfs_extlen_t len
) /* length of extent */
2503 xfs_alloc_arg_t args
;
2507 memset(&args
, 0, sizeof(xfs_alloc_arg_t
));
2509 args
.mp
= tp
->t_mountp
;
2510 args
.agno
= XFS_FSB_TO_AGNO(args
.mp
, bno
);
2511 ASSERT(args
.agno
< args
.mp
->m_sb
.sb_agcount
);
2512 args
.agbno
= XFS_FSB_TO_AGBNO(args
.mp
, bno
);
2513 down_read(&args
.mp
->m_peraglock
);
2514 args
.pag
= &args
.mp
->m_perag
[args
.agno
];
2515 if ((error
= xfs_alloc_fix_freelist(&args
, XFS_ALLOC_FLAG_FREEING
)))
2518 ASSERT(args
.agbp
!= NULL
);
2519 ASSERT((args
.agbno
+ len
) <=
2520 be32_to_cpu(XFS_BUF_TO_AGF(args
.agbp
)->agf_length
));
2522 error
= xfs_free_ag_extent(tp
, args
.agbp
, args
.agno
, args
.agbno
, len
, 0);
2524 up_read(&args
.mp
->m_peraglock
);
2530 * AG Busy list management
2531 * The busy list contains block ranges that have been freed but whose
2532 * transactions have not yet hit disk. If any block listed in a busy
2533 * list is reused, the transaction that freed it must be forced to disk
2534 * before continuing to use the block.
2536 * xfs_alloc_mark_busy - add to the per-ag busy list
2537 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2540 xfs_alloc_mark_busy(xfs_trans_t
*tp
,
2541 xfs_agnumber_t agno
,
2546 xfs_perag_busy_t
*bsy
;
2550 spin_lock(&mp
->m_perag
[agno
].pagb_lock
);
2552 /* search pagb_list for an open slot */
2553 for (bsy
= mp
->m_perag
[agno
].pagb_list
, n
= 0;
2554 n
< XFS_PAGB_NUM_SLOTS
;
2556 if (bsy
->busy_tp
== NULL
) {
2561 if (n
< XFS_PAGB_NUM_SLOTS
) {
2562 bsy
= &mp
->m_perag
[agno
].pagb_list
[n
];
2563 mp
->m_perag
[agno
].pagb_count
++;
2564 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno
, bno
, len
, n
, tp
);
2565 bsy
->busy_start
= bno
;
2566 bsy
->busy_length
= len
;
2568 xfs_trans_add_busy(tp
, agno
, n
);
2570 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno
, bno
, len
, -1, tp
);
2572 * The busy list is full! Since it is now not possible to
2573 * track the free block, make this a synchronous transaction
2574 * to insure that the block is not reused before this
2575 * transaction commits.
2577 xfs_trans_set_sync(tp
);
2580 spin_unlock(&mp
->m_perag
[agno
].pagb_lock
);
2584 xfs_alloc_clear_busy(xfs_trans_t
*tp
,
2585 xfs_agnumber_t agno
,
2589 xfs_perag_busy_t
*list
;
2593 spin_lock(&mp
->m_perag
[agno
].pagb_lock
);
2594 list
= mp
->m_perag
[agno
].pagb_list
;
2596 ASSERT(idx
< XFS_PAGB_NUM_SLOTS
);
2597 if (list
[idx
].busy_tp
== tp
) {
2598 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno
, idx
, tp
);
2599 list
[idx
].busy_tp
= NULL
;
2600 mp
->m_perag
[agno
].pagb_count
--;
2602 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno
, idx
, tp
);
2605 spin_unlock(&mp
->m_perag
[agno
].pagb_lock
);
2610 * If we find the extent in the busy list, force the log out to get the
2611 * extent out of the busy list so the caller can use it straight away.
2614 xfs_alloc_search_busy(xfs_trans_t
*tp
,
2615 xfs_agnumber_t agno
,
2620 xfs_perag_busy_t
*bsy
;
2621 xfs_agblock_t uend
, bend
;
2627 spin_lock(&mp
->m_perag
[agno
].pagb_lock
);
2628 cnt
= mp
->m_perag
[agno
].pagb_count
;
2630 uend
= bno
+ len
- 1;
2632 /* search pagb_list for this slot, skipping open slots */
2633 for (bsy
= mp
->m_perag
[agno
].pagb_list
; cnt
; bsy
++) {
2636 * (start1,length1) within (start2, length2)
2638 if (bsy
->busy_tp
!= NULL
) {
2639 bend
= bsy
->busy_start
+ bsy
->busy_length
- 1;
2640 if ((bno
> bend
) || (uend
< bsy
->busy_start
)) {
2643 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
2644 "found1", agno
, bno
, len
, tp
);
2651 * If a block was found, force the log through the LSN of the
2652 * transaction that freed the block
2655 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno
, bno
, len
, tp
);
2656 lsn
= bsy
->busy_tp
->t_commit_lsn
;
2657 spin_unlock(&mp
->m_perag
[agno
].pagb_lock
);
2658 xfs_log_force(mp
, lsn
, XFS_LOG_FORCE
|XFS_LOG_SYNC
);
2660 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno
, bno
, len
, tp
);
2661 spin_unlock(&mp
->m_perag
[agno
].pagb_lock
);