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(fname, s, a, __LINE__)
59 #define TRACE_FREE(s,a,b,x,f) \
60 xfs_alloc_trace_free(fname, s, mp, a, b, x, f, __LINE__)
61 #define TRACE_MODAGF(s,a,f) \
62 xfs_alloc_trace_modagf(fname, s, mp, a, f, __LINE__)
63 #define TRACE_BUSY(fname,s,ag,agb,l,sl,tp) \
64 xfs_alloc_trace_busy(fname, s, mp, ag, agb, l, sl, tp, XFS_ALLOC_KTRACE_BUSY, __LINE__)
65 #define TRACE_UNBUSY(fname,s,ag,sl,tp) \
66 xfs_alloc_trace_busy(fname, s, mp, ag, -1, -1, sl, tp, XFS_ALLOC_KTRACE_UNBUSY, __LINE__)
67 #define TRACE_BUSYSEARCH(fname,s,ag,agb,l,sl,tp) \
68 xfs_alloc_trace_busy(fname, s, mp, ag, agb, l, sl, 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,sl,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 * Compute aligned version of the found extent.
94 * Takes alignment and min length into account.
96 STATIC int /* success (>= minlen) */
97 xfs_alloc_compute_aligned(
98 xfs_agblock_t foundbno, /* starting block in found extent */
99 xfs_extlen_t foundlen, /* length in found extent */
100 xfs_extlen_t alignment, /* alignment for allocation */
101 xfs_extlen_t minlen, /* minimum length for allocation */
102 xfs_agblock_t *resbno, /* result block number */
103 xfs_extlen_t *reslen) /* result length */
109 if (alignment > 1 && foundlen >= minlen) {
110 bno = roundup(foundbno, alignment);
111 diff = bno - foundbno;
112 len = diff >= foundlen ? 0 : foundlen - diff;
119 return len >= minlen;
123 * Compute best start block and diff for "near" allocations.
124 * freelen >= wantlen already checked by caller.
126 STATIC xfs_extlen_t /* difference value (absolute) */
127 xfs_alloc_compute_diff(
128 xfs_agblock_t wantbno, /* target starting block */
129 xfs_extlen_t wantlen, /* target length */
130 xfs_extlen_t alignment, /* target alignment */
131 xfs_agblock_t freebno, /* freespace's starting block */
132 xfs_extlen_t freelen, /* freespace's length */
133 xfs_agblock_t *newbnop) /* result: best start block from free */
135 xfs_agblock_t freeend; /* end of freespace extent */
136 xfs_agblock_t newbno1; /* return block number */
137 xfs_agblock_t newbno2; /* other new block number */
138 xfs_extlen_t newlen1=0; /* length with newbno1 */
139 xfs_extlen_t newlen2=0; /* length with newbno2 */
140 xfs_agblock_t wantend; /* end of target extent */
142 ASSERT(freelen >= wantlen);
143 freeend = freebno + freelen;
144 wantend = wantbno + wantlen;
145 if (freebno >= wantbno) {
146 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
147 newbno1 = NULLAGBLOCK;
148 } else if (freeend >= wantend && alignment > 1) {
149 newbno1 = roundup(wantbno, alignment);
150 newbno2 = newbno1 - alignment;
151 if (newbno1 >= freeend)
152 newbno1 = NULLAGBLOCK;
154 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
155 if (newbno2 < freebno)
156 newbno2 = NULLAGBLOCK;
158 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
159 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
160 if (newlen1 < newlen2 ||
161 (newlen1 == newlen2 &&
162 XFS_ABSDIFF(newbno1, wantbno) >
163 XFS_ABSDIFF(newbno2, wantbno)))
165 } else if (newbno2 != NULLAGBLOCK)
167 } else if (freeend >= wantend) {
169 } else if (alignment > 1) {
170 newbno1 = roundup(freeend - wantlen, alignment);
171 if (newbno1 > freeend - wantlen &&
172 newbno1 - alignment >= freebno)
173 newbno1 -= alignment;
174 else if (newbno1 >= freeend)
175 newbno1 = NULLAGBLOCK;
177 newbno1 = freeend - wantlen;
179 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
183 * Fix up the length, based on mod and prod.
184 * len should be k * prod + mod for some k.
185 * If len is too small it is returned unchanged.
186 * If len hits maxlen it is left alone.
190 xfs_alloc_arg_t *args) /* allocation argument structure */
195 ASSERT(args->mod < args->prod);
197 ASSERT(rlen >= args->minlen);
198 ASSERT(rlen <= args->maxlen);
199 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
200 (args->mod == 0 && rlen < args->prod))
202 k = rlen % args->prod;
206 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
209 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
213 ASSERT(rlen >= args->minlen);
214 ASSERT(rlen <= args->maxlen);
219 * Fix up length if there is too little space left in the a.g.
220 * Return 1 if ok, 0 if too little, should give up.
223 xfs_alloc_fix_minleft(
224 xfs_alloc_arg_t *args) /* allocation argument structure */
226 xfs_agf_t *agf; /* a.g. freelist header */
227 int diff; /* free space difference */
229 if (args->minleft == 0)
231 agf = XFS_BUF_TO_AGF(args->agbp);
232 diff = be32_to_cpu(agf->agf_freeblks)
233 + be32_to_cpu(agf->agf_flcount)
234 - args->len - args->minleft;
237 args->len += diff; /* shrink the allocated space */
238 if (args->len >= args->minlen)
240 args->agbno = NULLAGBLOCK;
245 * Update the two btrees, logically removing from freespace the extent
246 * starting at rbno, rlen blocks. The extent is contained within the
247 * actual (current) free extent fbno for flen blocks.
248 * Flags are passed in indicating whether the cursors are set to the
251 STATIC int /* error code */
252 xfs_alloc_fixup_trees(
253 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
254 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
255 xfs_agblock_t fbno, /* starting block of free extent */
256 xfs_extlen_t flen, /* length of free extent */
257 xfs_agblock_t rbno, /* starting block of returned extent */
258 xfs_extlen_t rlen, /* length of returned extent */
259 int flags) /* flags, XFSA_FIXUP_... */
261 int error; /* error code */
262 int i; /* operation results */
263 xfs_agblock_t nfbno1; /* first new free startblock */
264 xfs_agblock_t nfbno2; /* second new free startblock */
265 xfs_extlen_t nflen1=0; /* first new free length */
266 xfs_extlen_t nflen2=0; /* second new free length */
269 * Look up the record in the by-size tree if necessary.
271 if (flags & XFSA_FIXUP_CNT_OK) {
273 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
275 XFS_WANT_CORRUPTED_RETURN(
276 i == 1 && nfbno1 == fbno && nflen1 == flen);
279 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
281 XFS_WANT_CORRUPTED_RETURN(i == 1);
284 * Look up the record in the by-block tree if necessary.
286 if (flags & XFSA_FIXUP_BNO_OK) {
288 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
290 XFS_WANT_CORRUPTED_RETURN(
291 i == 1 && nfbno1 == fbno && nflen1 == flen);
294 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
296 XFS_WANT_CORRUPTED_RETURN(i == 1);
300 xfs_alloc_block_t *bnoblock;
301 xfs_alloc_block_t *cntblock;
303 if (bno_cur->bc_nlevels == 1 &&
304 cnt_cur->bc_nlevels == 1) {
305 bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
306 cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
307 XFS_WANT_CORRUPTED_RETURN(
308 be16_to_cpu(bnoblock->bb_numrecs) ==
309 be16_to_cpu(cntblock->bb_numrecs));
314 * Deal with all four cases: the allocated record is contained
315 * within the freespace record, so we can have new freespace
316 * at either (or both) end, or no freespace remaining.
318 if (rbno == fbno && rlen == flen)
319 nfbno1 = nfbno2 = NULLAGBLOCK;
320 else if (rbno == fbno) {
321 nfbno1 = rbno + rlen;
322 nflen1 = flen - rlen;
323 nfbno2 = NULLAGBLOCK;
324 } else if (rbno + rlen == fbno + flen) {
326 nflen1 = flen - rlen;
327 nfbno2 = NULLAGBLOCK;
330 nflen1 = rbno - fbno;
331 nfbno2 = rbno + rlen;
332 nflen2 = (fbno + flen) - nfbno2;
335 * Delete the entry from the by-size btree.
337 if ((error = xfs_alloc_delete(cnt_cur, &i)))
339 XFS_WANT_CORRUPTED_RETURN(i == 1);
341 * Add new by-size btree entry(s).
343 if (nfbno1 != NULLAGBLOCK) {
344 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
346 XFS_WANT_CORRUPTED_RETURN(i == 0);
347 if ((error = xfs_alloc_insert(cnt_cur, &i)))
349 XFS_WANT_CORRUPTED_RETURN(i == 1);
351 if (nfbno2 != NULLAGBLOCK) {
352 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
354 XFS_WANT_CORRUPTED_RETURN(i == 0);
355 if ((error = xfs_alloc_insert(cnt_cur, &i)))
357 XFS_WANT_CORRUPTED_RETURN(i == 1);
360 * Fix up the by-block btree entry(s).
362 if (nfbno1 == NULLAGBLOCK) {
364 * No remaining freespace, just delete the by-block tree entry.
366 if ((error = xfs_alloc_delete(bno_cur, &i)))
368 XFS_WANT_CORRUPTED_RETURN(i == 1);
371 * Update the by-block entry to start later|be shorter.
373 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
376 if (nfbno2 != NULLAGBLOCK) {
378 * 2 resulting free entries, need to add one.
380 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
382 XFS_WANT_CORRUPTED_RETURN(i == 0);
383 if ((error = xfs_alloc_insert(bno_cur, &i)))
385 XFS_WANT_CORRUPTED_RETURN(i == 1);
391 * Read in the allocation group free block array.
393 STATIC int /* error */
395 xfs_mount_t *mp, /* mount point structure */
396 xfs_trans_t *tp, /* transaction pointer */
397 xfs_agnumber_t agno, /* allocation group number */
398 xfs_buf_t **bpp) /* buffer for the ag free block array */
400 xfs_buf_t *bp; /* return value */
403 ASSERT(agno != NULLAGNUMBER);
404 error = xfs_trans_read_buf(
405 mp, tp, mp->m_ddev_targp,
406 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
407 XFS_FSS_TO_BB(mp, 1), 0, &bp);
411 ASSERT(!XFS_BUF_GETERROR(bp));
412 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
417 #if defined(XFS_ALLOC_TRACE)
419 * Add an allocation trace entry for an alloc call.
422 xfs_alloc_trace_alloc(
423 char *name, /* function tag string */
424 char *str, /* additional string */
425 xfs_alloc_arg_t *args, /* allocation argument structure */
426 int line) /* source line number */
428 ktrace_enter(xfs_alloc_trace_buf,
429 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
433 (void *)(__psunsigned_t)args->agno,
434 (void *)(__psunsigned_t)args->agbno,
435 (void *)(__psunsigned_t)args->minlen,
436 (void *)(__psunsigned_t)args->maxlen,
437 (void *)(__psunsigned_t)args->mod,
438 (void *)(__psunsigned_t)args->prod,
439 (void *)(__psunsigned_t)args->minleft,
440 (void *)(__psunsigned_t)args->total,
441 (void *)(__psunsigned_t)args->alignment,
442 (void *)(__psunsigned_t)args->len,
443 (void *)((((__psint_t)args->type) << 16) |
444 (__psint_t)args->otype),
445 (void *)(__psint_t)((args->wasdel << 3) |
446 (args->wasfromfl << 2) |
448 (args->userdata << 0)));
452 * Add an allocation trace entry for a free call.
455 xfs_alloc_trace_free(
456 char *name, /* function tag string */
457 char *str, /* additional string */
458 xfs_mount_t *mp, /* file system mount point */
459 xfs_agnumber_t agno, /* allocation group number */
460 xfs_agblock_t agbno, /* a.g. relative block number */
461 xfs_extlen_t len, /* length of extent */
462 int isfl, /* set if is freelist allocation/free */
463 int line) /* source line number */
465 ktrace_enter(xfs_alloc_trace_buf,
466 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
470 (void *)(__psunsigned_t)agno,
471 (void *)(__psunsigned_t)agbno,
472 (void *)(__psunsigned_t)len,
473 (void *)(__psint_t)isfl,
474 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
478 * Add an allocation trace entry for modifying an agf.
481 xfs_alloc_trace_modagf(
482 char *name, /* function tag string */
483 char *str, /* additional string */
484 xfs_mount_t *mp, /* file system mount point */
485 xfs_agf_t *agf, /* new agf value */
486 int flags, /* logging flags for agf */
487 int line) /* source line number */
489 ktrace_enter(xfs_alloc_trace_buf,
490 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
494 (void *)(__psint_t)flags,
495 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
496 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
497 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
498 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
499 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
500 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
501 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
502 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
503 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
504 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
505 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
509 xfs_alloc_trace_busy(
510 char *name, /* function tag string */
511 char *str, /* additional string */
512 xfs_mount_t *mp, /* file system mount point */
513 xfs_agnumber_t agno, /* allocation group number */
514 xfs_agblock_t agbno, /* a.g. relative block number */
515 xfs_extlen_t len, /* length of extent */
516 int slot, /* perag Busy slot */
518 int trtype, /* type: add, delete, search */
519 int line) /* source line number */
521 ktrace_enter(xfs_alloc_trace_buf,
522 (void *)(__psint_t)(trtype | (line << 16)),
526 (void *)(__psunsigned_t)agno,
527 (void *)(__psunsigned_t)agbno,
528 (void *)(__psunsigned_t)len,
529 (void *)(__psint_t)slot,
531 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
533 #endif /* XFS_ALLOC_TRACE */
536 * Allocation group level functions.
540 * Allocate a variable extent in the allocation group agno.
541 * Type and bno are used to determine where in the allocation group the
543 * Extent's length (returned in *len) will be between minlen and maxlen,
544 * and of the form k * prod + mod unless there's nothing that large.
545 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
547 STATIC int /* error */
548 xfs_alloc_ag_vextent(
549 xfs_alloc_arg_t *args) /* argument structure for allocation */
552 #ifdef XFS_ALLOC_TRACE
553 static char fname[] = "xfs_alloc_ag_vextent";
556 ASSERT(args->minlen > 0);
557 ASSERT(args->maxlen > 0);
558 ASSERT(args->minlen <= args->maxlen);
559 ASSERT(args->mod < args->prod);
560 ASSERT(args->alignment > 0);
562 * Branch to correct routine based on the type.
565 switch (args->type) {
566 case XFS_ALLOCTYPE_THIS_AG:
567 error = xfs_alloc_ag_vextent_size(args);
569 case XFS_ALLOCTYPE_NEAR_BNO:
570 error = xfs_alloc_ag_vextent_near(args);
572 case XFS_ALLOCTYPE_THIS_BNO:
573 error = xfs_alloc_ag_vextent_exact(args);
582 * If the allocation worked, need to change the agf structure
583 * (and log it), and the superblock.
585 if (args->agbno != NULLAGBLOCK) {
586 xfs_agf_t *agf; /* allocation group freelist header */
587 #ifdef XFS_ALLOC_TRACE
588 xfs_mount_t *mp = args->mp;
590 long slen = (long)args->len;
592 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
593 ASSERT(!(args->wasfromfl) || !args->isfl);
594 ASSERT(args->agbno % args->alignment == 0);
595 if (!(args->wasfromfl)) {
597 agf = XFS_BUF_TO_AGF(args->agbp);
598 be32_add(&agf->agf_freeblks, -(args->len));
599 xfs_trans_agblocks_delta(args->tp,
600 -((long)(args->len)));
601 args->pag->pagf_freeblks -= args->len;
602 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
603 be32_to_cpu(agf->agf_length));
604 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
605 xfs_alloc_log_agf(args->tp, args->agbp,
607 /* search the busylist for these blocks */
608 xfs_alloc_search_busy(args->tp, args->agno,
609 args->agbno, args->len);
612 xfs_trans_mod_sb(args->tp,
613 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
614 XFS_TRANS_SB_FDBLOCKS, -slen);
615 XFS_STATS_INC(xs_allocx);
616 XFS_STATS_ADD(xs_allocb, args->len);
622 * Allocate a variable extent at exactly agno/bno.
623 * Extent's length (returned in *len) will be between minlen and maxlen,
624 * and of the form k * prod + mod unless there's nothing that large.
625 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
627 STATIC int /* error */
628 xfs_alloc_ag_vextent_exact(
629 xfs_alloc_arg_t *args) /* allocation argument structure */
631 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
632 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
633 xfs_agblock_t end; /* end of allocated extent */
635 xfs_agblock_t fbno; /* start block of found extent */
636 xfs_agblock_t fend; /* end block of found extent */
637 xfs_extlen_t flen; /* length of found extent */
638 #ifdef XFS_ALLOC_TRACE
639 static char fname[] = "xfs_alloc_ag_vextent_exact";
641 int i; /* success/failure of operation */
642 xfs_agblock_t maxend; /* end of maximal extent */
643 xfs_agblock_t minend; /* end of minimal extent */
644 xfs_extlen_t rlen; /* length of returned extent */
646 ASSERT(args->alignment == 1);
648 * Allocate/initialize a cursor for the by-number freespace btree.
650 bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
651 args->agno, XFS_BTNUM_BNO, NULL, 0);
653 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
654 * Look for the closest free block <= bno, it must contain bno
655 * if any free block does.
657 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
661 * Didn't find it, return null.
663 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
664 args->agbno = NULLAGBLOCK;
668 * Grab the freespace record.
670 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
672 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
673 ASSERT(fbno <= args->agbno);
674 minend = args->agbno + args->minlen;
675 maxend = args->agbno + args->maxlen;
678 * Give up if the freespace isn't long enough for the minimum request.
681 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
682 args->agbno = NULLAGBLOCK;
686 * End of extent will be smaller of the freespace end and the
687 * maximal requested end.
689 end = XFS_AGBLOCK_MIN(fend, maxend);
691 * Fix the length according to mod and prod if given.
693 args->len = end - args->agbno;
694 xfs_alloc_fix_len(args);
695 if (!xfs_alloc_fix_minleft(args)) {
696 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
700 ASSERT(args->agbno + rlen <= fend);
701 end = args->agbno + rlen;
703 * We are allocating agbno for rlen [agbno .. end]
704 * Allocate/initialize a cursor for the by-size btree.
706 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
707 args->agno, XFS_BTNUM_CNT, NULL, 0);
708 ASSERT(args->agbno + args->len <=
709 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
710 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
711 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
712 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
715 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
716 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
717 TRACE_ALLOC("normal", args);
722 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
723 TRACE_ALLOC("error", args);
728 * Allocate a variable extent near bno in the allocation group agno.
729 * Extent's length (returned in len) will be between minlen and maxlen,
730 * and of the form k * prod + mod unless there's nothing that large.
731 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
733 STATIC int /* error */
734 xfs_alloc_ag_vextent_near(
735 xfs_alloc_arg_t *args) /* allocation argument structure */
737 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
738 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
739 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
740 #ifdef XFS_ALLOC_TRACE
741 static char fname[] = "xfs_alloc_ag_vextent_near";
743 xfs_agblock_t gtbno; /* start bno of right side entry */
744 xfs_agblock_t gtbnoa; /* aligned ... */
745 xfs_extlen_t gtdiff; /* difference to right side entry */
746 xfs_extlen_t gtlen; /* length of right side entry */
747 xfs_extlen_t gtlena; /* aligned ... */
748 xfs_agblock_t gtnew; /* useful start bno of right side */
749 int error; /* error code */
750 int i; /* result code, temporary */
751 int j; /* result code, temporary */
752 xfs_agblock_t ltbno; /* start bno of left side entry */
753 xfs_agblock_t ltbnoa; /* aligned ... */
754 xfs_extlen_t ltdiff; /* difference to left side entry */
756 xfs_agblock_t ltend; /* end bno of left side entry */
757 xfs_extlen_t ltlen; /* length of left side entry */
758 xfs_extlen_t ltlena; /* aligned ... */
759 xfs_agblock_t ltnew; /* useful start bno of left side */
760 xfs_extlen_t rlen; /* length of returned extent */
761 #if defined(DEBUG) && defined(__KERNEL__)
763 * Randomly don't execute the first algorithm.
765 int dofirst; /* set to do first algorithm */
767 dofirst = random32() & 1;
770 * Get a cursor for the by-size btree.
772 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
773 args->agno, XFS_BTNUM_CNT, NULL, 0);
775 bno_cur_lt = bno_cur_gt = NULL;
777 * See if there are any free extents as big as maxlen.
779 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
782 * If none, then pick up the last entry in the tree unless the
786 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
789 if (i == 0 || ltlen == 0) {
790 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
798 * If the requested extent is large wrt the freespaces available
799 * in this a.g., then the cursor will be pointing to a btree entry
800 * near the right edge of the tree. If it's in the last btree leaf
801 * block, then we just examine all the entries in that block
802 * that are big enough, and pick the best one.
803 * This is written as a while loop so we can break out of it,
804 * but we never loop back to the top.
806 while (xfs_btree_islastblock(cnt_cur, 0)) {
810 xfs_agblock_t bnew=0;
812 #if defined(DEBUG) && defined(__KERNEL__)
817 * Start from the entry that lookup found, sequence through
818 * all larger free blocks. If we're actually pointing at a
819 * record smaller than maxlen, go to the start of this block,
820 * and skip all those smaller than minlen.
822 if (ltlen || args->alignment > 1) {
823 cnt_cur->bc_ptrs[0] = 1;
825 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
828 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
829 if (ltlen >= args->minlen)
831 if ((error = xfs_alloc_increment(cnt_cur, 0, &i)))
834 ASSERT(ltlen >= args->minlen);
838 i = cnt_cur->bc_ptrs[0];
839 for (j = 1, blen = 0, bdiff = 0;
840 !error && j && (blen < args->maxlen || bdiff > 0);
841 error = xfs_alloc_increment(cnt_cur, 0, &j)) {
843 * For each entry, decide if it's better than
844 * the previous best entry.
846 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
848 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
849 if (!xfs_alloc_compute_aligned(ltbno, ltlen,
850 args->alignment, args->minlen,
853 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
854 xfs_alloc_fix_len(args);
855 ASSERT(args->len >= args->minlen);
856 if (args->len < blen)
858 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
859 args->alignment, ltbno, ltlen, <new);
860 if (ltnew != NULLAGBLOCK &&
861 (args->len > blen || ltdiff < bdiff)) {
865 besti = cnt_cur->bc_ptrs[0];
869 * It didn't work. We COULD be in a case where
870 * there's a good record somewhere, so try again.
875 * Point at the best entry, and retrieve it again.
877 cnt_cur->bc_ptrs[0] = besti;
878 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
880 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
881 ltend = ltbno + ltlen;
882 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
884 if (!xfs_alloc_fix_minleft(args)) {
885 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
886 TRACE_ALLOC("nominleft", args);
891 * We are allocating starting at bnew for blen blocks.
894 ASSERT(bnew >= ltbno);
895 ASSERT(bnew + blen <= ltend);
897 * Set up a cursor for the by-bno tree.
899 bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp,
900 args->agbp, args->agno, XFS_BTNUM_BNO, NULL, 0);
902 * Fix up the btree entries.
904 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
905 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
907 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
908 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
909 TRACE_ALLOC("first", args);
914 * Search in the by-bno tree to the left and to the right
915 * simultaneously, until in each case we find a space big enough,
916 * or run into the edge of the tree. When we run into the edge,
917 * we deallocate that cursor.
918 * If both searches succeed, we compare the two spaces and pick
920 * With alignment, it's possible for both to fail; the upper
921 * level algorithm that picks allocation groups for allocations
922 * is not supposed to do this.
925 * Allocate and initialize the cursor for the leftward search.
927 bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
928 args->agno, XFS_BTNUM_BNO, NULL, 0);
930 * Lookup <= bno to find the leftward search's starting point.
932 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
936 * Didn't find anything; use this cursor for the rightward
939 bno_cur_gt = bno_cur_lt;
943 * Found something. Duplicate the cursor for the rightward search.
945 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
948 * Increment the cursor, so we will point at the entry just right
949 * of the leftward entry if any, or to the leftmost entry.
951 if ((error = xfs_alloc_increment(bno_cur_gt, 0, &i)))
955 * It failed, there are no rightward entries.
957 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
961 * Loop going left with the leftward cursor, right with the
962 * rightward cursor, until either both directions give up or
963 * we find an entry at least as big as minlen.
967 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
969 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
970 if (xfs_alloc_compute_aligned(ltbno, ltlen,
971 args->alignment, args->minlen,
974 if ((error = xfs_alloc_decrement(bno_cur_lt, 0, &i)))
977 xfs_btree_del_cursor(bno_cur_lt,
983 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
985 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
986 if (xfs_alloc_compute_aligned(gtbno, gtlen,
987 args->alignment, args->minlen,
990 if ((error = xfs_alloc_increment(bno_cur_gt, 0, &i)))
993 xfs_btree_del_cursor(bno_cur_gt,
998 } while (bno_cur_lt || bno_cur_gt);
1000 * Got both cursors still active, need to find better entry.
1002 if (bno_cur_lt && bno_cur_gt) {
1004 * Left side is long enough, look for a right side entry.
1006 if (ltlena >= args->minlen) {
1008 * Fix up the length.
1010 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1011 xfs_alloc_fix_len(args);
1013 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1014 args->alignment, ltbno, ltlen, <new);
1020 * Look until we find a better one, run out of
1021 * space, or run off the end.
1023 while (bno_cur_lt && bno_cur_gt) {
1024 if ((error = xfs_alloc_get_rec(
1028 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1029 xfs_alloc_compute_aligned(gtbno, gtlen,
1030 args->alignment, args->minlen,
1033 * The left one is clearly better.
1035 if (gtbnoa >= args->agbno + ltdiff) {
1036 xfs_btree_del_cursor(
1043 * If we reach a big enough entry,
1044 * compare the two and pick the best.
1046 if (gtlena >= args->minlen) {
1048 XFS_EXTLEN_MIN(gtlena,
1050 xfs_alloc_fix_len(args);
1052 gtdiff = xfs_alloc_compute_diff(
1055 gtbno, gtlen, >new);
1057 * Right side is better.
1059 if (gtdiff < ltdiff) {
1060 xfs_btree_del_cursor(
1066 * Left side is better.
1069 xfs_btree_del_cursor(
1077 * Fell off the right end.
1079 if ((error = xfs_alloc_increment(
1080 bno_cur_gt, 0, &i)))
1083 xfs_btree_del_cursor(
1092 * The left side is perfect, trash the right side.
1095 xfs_btree_del_cursor(bno_cur_gt,
1101 * It's the right side that was found first, look left.
1105 * Fix up the length.
1107 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1108 xfs_alloc_fix_len(args);
1110 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1111 args->alignment, gtbno, gtlen, >new);
1113 * Right side entry isn't perfect.
1117 * Look until we find a better one, run out of
1118 * space, or run off the end.
1120 while (bno_cur_lt && bno_cur_gt) {
1121 if ((error = xfs_alloc_get_rec(
1125 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1126 xfs_alloc_compute_aligned(ltbno, ltlen,
1127 args->alignment, args->minlen,
1130 * The right one is clearly better.
1132 if (ltbnoa <= args->agbno - gtdiff) {
1133 xfs_btree_del_cursor(
1140 * If we reach a big enough entry,
1141 * compare the two and pick the best.
1143 if (ltlena >= args->minlen) {
1144 args->len = XFS_EXTLEN_MIN(
1145 ltlena, args->maxlen);
1146 xfs_alloc_fix_len(args);
1148 ltdiff = xfs_alloc_compute_diff(
1151 ltbno, ltlen, <new);
1153 * Left side is better.
1155 if (ltdiff < gtdiff) {
1156 xfs_btree_del_cursor(
1162 * Right side is better.
1165 xfs_btree_del_cursor(
1173 * Fell off the left end.
1175 if ((error = xfs_alloc_decrement(
1176 bno_cur_lt, 0, &i)))
1179 xfs_btree_del_cursor(bno_cur_lt,
1187 * The right side is perfect, trash the left side.
1190 xfs_btree_del_cursor(bno_cur_lt,
1197 * If we couldn't get anything, give up.
1199 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1200 TRACE_ALLOC("neither", args);
1201 args->agbno = NULLAGBLOCK;
1205 * At this point we have selected a freespace entry, either to the
1206 * left or to the right. If it's on the right, copy all the
1207 * useful variables to the "left" set so we only have one
1208 * copy of this code.
1211 bno_cur_lt = bno_cur_gt;
1221 * Fix up the length and compute the useful address.
1223 ltend = ltbno + ltlen;
1224 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1225 xfs_alloc_fix_len(args);
1226 if (!xfs_alloc_fix_minleft(args)) {
1227 TRACE_ALLOC("nominleft", args);
1228 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1229 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1233 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1235 ASSERT(ltnew >= ltbno);
1236 ASSERT(ltnew + rlen <= ltend);
1237 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1238 args->agbno = ltnew;
1239 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1240 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1242 TRACE_ALLOC(j ? "gt" : "lt", args);
1243 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1244 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1248 TRACE_ALLOC("error", args);
1249 if (cnt_cur != NULL)
1250 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1251 if (bno_cur_lt != NULL)
1252 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1253 if (bno_cur_gt != NULL)
1254 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1259 * Allocate a variable extent anywhere in the allocation group agno.
1260 * Extent's length (returned in len) will be between minlen and maxlen,
1261 * and of the form k * prod + mod unless there's nothing that large.
1262 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1264 STATIC int /* error */
1265 xfs_alloc_ag_vextent_size(
1266 xfs_alloc_arg_t *args) /* allocation argument structure */
1268 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1269 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1270 int error; /* error result */
1271 xfs_agblock_t fbno; /* start of found freespace */
1272 xfs_extlen_t flen; /* length of found freespace */
1273 #ifdef XFS_ALLOC_TRACE
1274 static char fname[] = "xfs_alloc_ag_vextent_size";
1276 int i; /* temp status variable */
1277 xfs_agblock_t rbno; /* returned block number */
1278 xfs_extlen_t rlen; /* length of returned extent */
1281 * Allocate and initialize a cursor for the by-size btree.
1283 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
1284 args->agno, XFS_BTNUM_CNT, NULL, 0);
1287 * Look for an entry >= maxlen+alignment-1 blocks.
1289 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1290 args->maxlen + args->alignment - 1, &i)))
1293 * If none, then pick up the last entry in the tree unless the
1297 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1300 if (i == 0 || flen == 0) {
1301 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1302 TRACE_ALLOC("noentry", args);
1308 * There's a freespace as big as maxlen+alignment-1, get it.
1311 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1313 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1316 * In the first case above, we got the last entry in the
1317 * by-size btree. Now we check to see if the space hits maxlen
1318 * once aligned; if not, we search left for something better.
1319 * This can't happen in the second case above.
1321 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1323 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1324 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1325 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1326 if (rlen < args->maxlen) {
1327 xfs_agblock_t bestfbno;
1328 xfs_extlen_t bestflen;
1329 xfs_agblock_t bestrbno;
1330 xfs_extlen_t bestrlen;
1337 if ((error = xfs_alloc_decrement(cnt_cur, 0, &i)))
1341 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1344 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1345 if (flen < bestrlen)
1347 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1348 args->minlen, &rbno, &rlen);
1349 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1350 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1351 (rlen <= flen && rbno + rlen <= fbno + flen),
1353 if (rlen > bestrlen) {
1358 if (rlen == args->maxlen)
1362 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1365 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1371 args->wasfromfl = 0;
1373 * Fix up the length.
1376 xfs_alloc_fix_len(args);
1377 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1378 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1379 TRACE_ALLOC("nominleft", args);
1380 args->agbno = NULLAGBLOCK;
1384 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1386 * Allocate and initialize a cursor for the by-block tree.
1388 bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
1389 args->agno, XFS_BTNUM_BNO, NULL, 0);
1390 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1391 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1393 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1394 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1395 cnt_cur = bno_cur = NULL;
1398 XFS_WANT_CORRUPTED_GOTO(
1399 args->agbno + args->len <=
1400 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1402 TRACE_ALLOC("normal", args);
1406 TRACE_ALLOC("error", args);
1408 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1410 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1415 * Deal with the case where only small freespaces remain.
1416 * Either return the contents of the last freespace record,
1417 * or allocate space from the freelist if there is nothing in the tree.
1419 STATIC int /* error */
1420 xfs_alloc_ag_vextent_small(
1421 xfs_alloc_arg_t *args, /* allocation argument structure */
1422 xfs_btree_cur_t *ccur, /* by-size cursor */
1423 xfs_agblock_t *fbnop, /* result block number */
1424 xfs_extlen_t *flenp, /* result length */
1425 int *stat) /* status: 0-freelist, 1-normal/none */
1430 #ifdef XFS_ALLOC_TRACE
1431 static char fname[] = "xfs_alloc_ag_vextent_small";
1435 if ((error = xfs_alloc_decrement(ccur, 0, &i)))
1438 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1440 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1443 * Nothing in the btree, try the freelist. Make sure
1444 * to respect minleft even when pulling from the
1447 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1448 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1450 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1453 if (fbno != NULLAGBLOCK) {
1454 if (args->userdata) {
1457 bp = xfs_btree_get_bufs(args->mp, args->tp,
1458 args->agno, fbno, 0);
1459 xfs_trans_binval(args->tp, bp);
1463 XFS_WANT_CORRUPTED_GOTO(
1464 args->agbno + args->len <=
1465 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1467 args->wasfromfl = 1;
1468 TRACE_ALLOC("freelist", args);
1473 * Nothing in the freelist.
1479 * Can't allocate from the freelist for some reason.
1486 * Can't do the allocation, give up.
1488 if (flen < args->minlen) {
1489 args->agbno = NULLAGBLOCK;
1490 TRACE_ALLOC("notenough", args);
1496 TRACE_ALLOC("normal", args);
1500 TRACE_ALLOC("error", args);
1505 * Free the extent starting at agno/bno for length.
1507 STATIC int /* error */
1509 xfs_trans_t *tp, /* transaction pointer */
1510 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1511 xfs_agnumber_t agno, /* allocation group number */
1512 xfs_agblock_t bno, /* starting block number */
1513 xfs_extlen_t len, /* length of extent */
1514 int isfl) /* set if is freelist blocks - no sb acctg */
1516 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1517 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1518 int error; /* error return value */
1519 #ifdef XFS_ALLOC_TRACE
1520 static char fname[] = "xfs_free_ag_extent";
1522 xfs_agblock_t gtbno; /* start of right neighbor block */
1523 xfs_extlen_t gtlen; /* length of right neighbor block */
1524 int haveleft; /* have a left neighbor block */
1525 int haveright; /* have a right neighbor block */
1526 int i; /* temp, result code */
1527 xfs_agblock_t ltbno; /* start of left neighbor block */
1528 xfs_extlen_t ltlen; /* length of left neighbor block */
1529 xfs_mount_t *mp; /* mount point struct for filesystem */
1530 xfs_agblock_t nbno; /* new starting block of freespace */
1531 xfs_extlen_t nlen; /* new length of freespace */
1535 * Allocate and initialize a cursor for the by-block btree.
1537 bno_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO, NULL,
1541 * Look for a neighboring block on the left (lower block numbers)
1542 * that is contiguous with this space.
1544 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1548 * There is a block to our left.
1550 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1552 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1554 * It's not contiguous, though.
1556 if (ltbno + ltlen < bno)
1560 * If this failure happens the request to free this
1561 * space was invalid, it's (partly) already free.
1564 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1568 * Look for a neighboring block on the right (higher block numbers)
1569 * that is contiguous with this space.
1571 if ((error = xfs_alloc_increment(bno_cur, 0, &haveright)))
1575 * There is a block to our right.
1577 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1579 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1581 * It's not contiguous, though.
1583 if (bno + len < gtbno)
1587 * If this failure happens the request to free this
1588 * space was invalid, it's (partly) already free.
1591 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1595 * Now allocate and initialize a cursor for the by-size tree.
1597 cnt_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT, NULL,
1600 * Have both left and right contiguous neighbors.
1601 * Merge all three into a single free block.
1603 if (haveleft && haveright) {
1605 * Delete the old by-size entry on the left.
1607 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1609 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1610 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1612 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1614 * Delete the old by-size entry on the right.
1616 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1618 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1619 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1621 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1623 * Delete the old by-block entry for the right block.
1625 if ((error = xfs_alloc_delete(bno_cur, &i)))
1627 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1629 * Move the by-block cursor back to the left neighbor.
1631 if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
1633 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1636 * Check that this is the right record: delete didn't
1637 * mangle the cursor.
1640 xfs_agblock_t xxbno;
1643 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1646 XFS_WANT_CORRUPTED_GOTO(
1647 i == 1 && xxbno == ltbno && xxlen == ltlen,
1652 * Update remaining by-block entry to the new, joined block.
1655 nlen = len + ltlen + gtlen;
1656 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1660 * Have only a left contiguous neighbor.
1661 * Merge it together with the new freespace.
1663 else if (haveleft) {
1665 * Delete the old by-size entry on the left.
1667 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1669 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1670 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1672 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1674 * Back up the by-block cursor to the left neighbor, and
1675 * update its length.
1677 if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
1679 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1682 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1686 * Have only a right contiguous neighbor.
1687 * Merge it together with the new freespace.
1689 else if (haveright) {
1691 * Delete the old by-size entry on the right.
1693 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1695 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1696 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1698 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1700 * Update the starting block and length of the right
1701 * neighbor in the by-block tree.
1705 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1709 * No contiguous neighbors.
1710 * Insert the new freespace into the by-block tree.
1715 if ((error = xfs_alloc_insert(bno_cur, &i)))
1717 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1719 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1722 * In all cases we need to insert the new freespace in the by-size tree.
1724 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1726 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1727 if ((error = xfs_alloc_insert(cnt_cur, &i)))
1729 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1730 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1733 * Update the freespace totals in the ag and superblock.
1737 xfs_perag_t *pag; /* per allocation group data */
1739 agf = XFS_BUF_TO_AGF(agbp);
1740 pag = &mp->m_perag[agno];
1741 be32_add(&agf->agf_freeblks, len);
1742 xfs_trans_agblocks_delta(tp, len);
1743 pag->pagf_freeblks += len;
1744 XFS_WANT_CORRUPTED_GOTO(
1745 be32_to_cpu(agf->agf_freeblks) <=
1746 be32_to_cpu(agf->agf_length),
1748 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1749 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1751 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1752 XFS_STATS_INC(xs_freex);
1753 XFS_STATS_ADD(xs_freeb, len);
1755 TRACE_FREE(haveleft ?
1756 (haveright ? "both" : "left") :
1757 (haveright ? "right" : "none"),
1758 agno, bno, len, isfl);
1761 * Since blocks move to the free list without the coordination
1762 * used in xfs_bmap_finish, we can't allow block to be available
1763 * for reallocation and non-transaction writing (user data)
1764 * until we know that the transaction that moved it to the free
1765 * list is permanently on disk. We track the blocks by declaring
1766 * these blocks as "busy"; the busy list is maintained on a per-ag
1767 * basis and each transaction records which entries should be removed
1768 * when the iclog commits to disk. If a busy block is allocated,
1769 * the iclog is pushed up to the LSN that freed the block.
1771 xfs_alloc_mark_busy(tp, agno, bno, len);
1775 TRACE_FREE("error", agno, bno, len, isfl);
1777 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1779 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1784 * Visible (exported) allocation/free functions.
1785 * Some of these are used just by xfs_alloc_btree.c and this file.
1789 * Compute and fill in value of m_ag_maxlevels.
1792 xfs_alloc_compute_maxlevels(
1793 xfs_mount_t *mp) /* file system mount structure */
1801 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1802 minleafrecs = mp->m_alloc_mnr[0];
1803 minnoderecs = mp->m_alloc_mnr[1];
1804 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1805 for (level = 1; maxblocks > 1; level++)
1806 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1807 mp->m_ag_maxlevels = level;
1811 * Decide whether to use this allocation group for this allocation.
1812 * If so, fix up the btree freelist's size.
1814 STATIC int /* error */
1815 xfs_alloc_fix_freelist(
1816 xfs_alloc_arg_t *args, /* allocation argument structure */
1817 int flags) /* XFS_ALLOC_FLAG_... */
1819 xfs_buf_t *agbp; /* agf buffer pointer */
1820 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1821 xfs_buf_t *agflbp;/* agfl buffer pointer */
1822 xfs_agblock_t bno; /* freelist block */
1823 xfs_extlen_t delta; /* new blocks needed in freelist */
1824 int error; /* error result code */
1825 xfs_extlen_t longest;/* longest extent in allocation group */
1826 xfs_mount_t *mp; /* file system mount point structure */
1827 xfs_extlen_t need; /* total blocks needed in freelist */
1828 xfs_perag_t *pag; /* per-ag information structure */
1829 xfs_alloc_arg_t targs; /* local allocation arguments */
1830 xfs_trans_t *tp; /* transaction pointer */
1836 if (!pag->pagf_init) {
1837 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1840 if (!pag->pagf_init) {
1841 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1842 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1850 * If this is a metadata preferred pag and we are user data
1851 * then try somewhere else if we are not being asked to
1852 * try harder at this point
1854 if (pag->pagf_metadata && args->userdata &&
1855 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1856 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1861 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1862 need = XFS_MIN_FREELIST_PAG(pag, mp);
1863 delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
1865 * If it looks like there isn't a long enough extent, or enough
1866 * total blocks, reject it.
1868 longest = (pag->pagf_longest > delta) ?
1869 (pag->pagf_longest - delta) :
1870 (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
1871 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1873 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1874 need - args->total) < (int)args->minleft)) {
1876 xfs_trans_brelse(tp, agbp);
1883 * Get the a.g. freespace buffer.
1884 * Can fail if we're not blocking on locks, and it's held.
1887 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1891 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1892 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1898 * Figure out how many blocks we should have in the freelist.
1900 agf = XFS_BUF_TO_AGF(agbp);
1901 need = XFS_MIN_FREELIST(agf, mp);
1903 * If there isn't enough total or single-extent, reject it.
1905 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1906 delta = need > be32_to_cpu(agf->agf_flcount) ?
1907 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1908 longest = be32_to_cpu(agf->agf_longest);
1909 longest = (longest > delta) ? (longest - delta) :
1910 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1911 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1913 ((int)(be32_to_cpu(agf->agf_freeblks) +
1914 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1915 (int)args->minleft)) {
1916 xfs_trans_brelse(tp, agbp);
1922 * Make the freelist shorter if it's too long.
1924 while (be32_to_cpu(agf->agf_flcount) > need) {
1927 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1930 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1932 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1933 xfs_trans_binval(tp, bp);
1936 * Initialize the args structure.
1941 targs.agno = args->agno;
1942 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1943 targs.minalignslop = 0;
1944 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1945 targs.type = XFS_ALLOCTYPE_THIS_AG;
1947 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1950 * Make the freelist longer if it's too short.
1952 while (be32_to_cpu(agf->agf_flcount) < need) {
1954 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1956 * Allocate as many blocks as possible at once.
1958 if ((error = xfs_alloc_ag_vextent(&targs))) {
1959 xfs_trans_brelse(tp, agflbp);
1963 * Stop if we run out. Won't happen if callers are obeying
1964 * the restrictions correctly. Can happen for free calls
1965 * on a completely full ag.
1967 if (targs.agbno == NULLAGBLOCK) {
1968 if (flags & XFS_ALLOC_FLAG_FREEING)
1970 xfs_trans_brelse(tp, agflbp);
1975 * Put each allocated block on the list.
1977 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
1978 error = xfs_alloc_put_freelist(tp, agbp,
1984 xfs_trans_brelse(tp, agflbp);
1990 * Get a block from the freelist.
1991 * Returns with the buffer for the block gotten.
1994 xfs_alloc_get_freelist(
1995 xfs_trans_t *tp, /* transaction pointer */
1996 xfs_buf_t *agbp, /* buffer containing the agf structure */
1997 xfs_agblock_t *bnop, /* block address retrieved from freelist */
1998 int btreeblk) /* destination is a AGF btree */
2000 xfs_agf_t *agf; /* a.g. freespace structure */
2001 xfs_agfl_t *agfl; /* a.g. freelist structure */
2002 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2003 xfs_agblock_t bno; /* block number returned */
2006 #ifdef XFS_ALLOC_TRACE
2007 static char fname[] = "xfs_alloc_get_freelist";
2009 xfs_mount_t *mp; /* mount structure */
2010 xfs_perag_t *pag; /* per allocation group data */
2012 agf = XFS_BUF_TO_AGF(agbp);
2014 * Freelist is empty, give up.
2016 if (!agf->agf_flcount) {
2017 *bnop = NULLAGBLOCK;
2021 * Read the array of free blocks.
2024 if ((error = xfs_alloc_read_agfl(mp, tp,
2025 be32_to_cpu(agf->agf_seqno), &agflbp)))
2027 agfl = XFS_BUF_TO_AGFL(agflbp);
2029 * Get the block number and update the data structures.
2031 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2032 be32_add(&agf->agf_flfirst, 1);
2033 xfs_trans_brelse(tp, agflbp);
2034 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2035 agf->agf_flfirst = 0;
2036 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2037 be32_add(&agf->agf_flcount, -1);
2038 xfs_trans_agflist_delta(tp, -1);
2039 pag->pagf_flcount--;
2041 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2043 be32_add(&agf->agf_btreeblks, 1);
2044 pag->pagf_btreeblks++;
2045 logflags |= XFS_AGF_BTREEBLKS;
2048 TRACE_MODAGF(NULL, agf, logflags);
2049 xfs_alloc_log_agf(tp, agbp, logflags);
2053 * As blocks are freed, they are added to the per-ag busy list
2054 * and remain there until the freeing transaction is committed to
2055 * disk. Now that we have allocated blocks, this list must be
2056 * searched to see if a block is being reused. If one is, then
2057 * the freeing transaction must be pushed to disk NOW by forcing
2058 * to disk all iclogs up that transaction's LSN.
2060 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
2065 * Log the given fields from the agf structure.
2069 xfs_trans_t *tp, /* transaction pointer */
2070 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2071 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2073 int first; /* first byte offset */
2074 int last; /* last byte offset */
2075 static const short offsets[] = {
2076 offsetof(xfs_agf_t, agf_magicnum),
2077 offsetof(xfs_agf_t, agf_versionnum),
2078 offsetof(xfs_agf_t, agf_seqno),
2079 offsetof(xfs_agf_t, agf_length),
2080 offsetof(xfs_agf_t, agf_roots[0]),
2081 offsetof(xfs_agf_t, agf_levels[0]),
2082 offsetof(xfs_agf_t, agf_flfirst),
2083 offsetof(xfs_agf_t, agf_fllast),
2084 offsetof(xfs_agf_t, agf_flcount),
2085 offsetof(xfs_agf_t, agf_freeblks),
2086 offsetof(xfs_agf_t, agf_longest),
2087 offsetof(xfs_agf_t, agf_btreeblks),
2091 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2092 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2096 * Interface for inode allocation to force the pag data to be initialized.
2099 xfs_alloc_pagf_init(
2100 xfs_mount_t *mp, /* file system mount structure */
2101 xfs_trans_t *tp, /* transaction pointer */
2102 xfs_agnumber_t agno, /* allocation group number */
2103 int flags) /* XFS_ALLOC_FLAGS_... */
2108 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2111 xfs_trans_brelse(tp, bp);
2116 * Put the block on the freelist for the allocation group.
2119 xfs_alloc_put_freelist(
2120 xfs_trans_t *tp, /* transaction pointer */
2121 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2122 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2123 xfs_agblock_t bno, /* block being freed */
2124 int btreeblk) /* block came from a AGF btree */
2126 xfs_agf_t *agf; /* a.g. freespace structure */
2127 xfs_agfl_t *agfl; /* a.g. free block array */
2128 __be32 *blockp;/* pointer to array entry */
2131 #ifdef XFS_ALLOC_TRACE
2132 static char fname[] = "xfs_alloc_put_freelist";
2134 xfs_mount_t *mp; /* mount structure */
2135 xfs_perag_t *pag; /* per allocation group data */
2137 agf = XFS_BUF_TO_AGF(agbp);
2140 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2141 be32_to_cpu(agf->agf_seqno), &agflbp)))
2143 agfl = XFS_BUF_TO_AGFL(agflbp);
2144 be32_add(&agf->agf_fllast, 1);
2145 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2146 agf->agf_fllast = 0;
2147 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2148 be32_add(&agf->agf_flcount, 1);
2149 xfs_trans_agflist_delta(tp, 1);
2150 pag->pagf_flcount++;
2152 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2154 be32_add(&agf->agf_btreeblks, -1);
2155 pag->pagf_btreeblks--;
2156 logflags |= XFS_AGF_BTREEBLKS;
2159 TRACE_MODAGF(NULL, agf, logflags);
2160 xfs_alloc_log_agf(tp, agbp, logflags);
2162 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2163 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2164 *blockp = cpu_to_be32(bno);
2165 TRACE_MODAGF(NULL, agf, logflags);
2166 xfs_alloc_log_agf(tp, agbp, logflags);
2167 xfs_trans_log_buf(tp, agflbp,
2168 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2169 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2170 sizeof(xfs_agblock_t) - 1));
2175 * Read in the allocation group header (free/alloc section).
2179 xfs_mount_t *mp, /* mount point structure */
2180 xfs_trans_t *tp, /* transaction pointer */
2181 xfs_agnumber_t agno, /* allocation group number */
2182 int flags, /* XFS_ALLOC_FLAG_... */
2183 xfs_buf_t **bpp) /* buffer for the ag freelist header */
2185 xfs_agf_t *agf; /* ag freelist header */
2186 int agf_ok; /* set if agf is consistent */
2187 xfs_buf_t *bp; /* return value */
2188 xfs_perag_t *pag; /* per allocation group data */
2191 ASSERT(agno != NULLAGNUMBER);
2192 error = xfs_trans_read_buf(
2193 mp, tp, mp->m_ddev_targp,
2194 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2195 XFS_FSS_TO_BB(mp, 1),
2196 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
2200 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
2206 * Validate the magic number of the agf block.
2208 agf = XFS_BUF_TO_AGF(bp);
2210 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2211 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2212 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2213 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2214 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2215 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
2216 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2217 XFS_RANDOM_ALLOC_READ_AGF))) {
2218 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2219 XFS_ERRLEVEL_LOW, mp, agf);
2220 xfs_trans_brelse(tp, bp);
2221 return XFS_ERROR(EFSCORRUPTED);
2223 pag = &mp->m_perag[agno];
2224 if (!pag->pagf_init) {
2225 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2226 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2227 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2228 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2229 pag->pagf_levels[XFS_BTNUM_BNOi] =
2230 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2231 pag->pagf_levels[XFS_BTNUM_CNTi] =
2232 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2233 spinlock_init(&pag->pagb_lock, "xfspagb");
2234 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2235 sizeof(xfs_perag_busy_t), KM_SLEEP);
2239 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2240 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2241 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2242 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2243 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2244 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2245 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2246 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2249 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
2255 * Allocate an extent (variable-size).
2256 * Depending on the allocation type, we either look in a single allocation
2257 * group or loop over the allocation groups to find the result.
2261 xfs_alloc_arg_t *args) /* allocation argument structure */
2263 xfs_agblock_t agsize; /* allocation group size */
2265 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2266 #ifdef XFS_ALLOC_TRACE
2267 static char fname[] = "xfs_alloc_vextent";
2269 xfs_extlen_t minleft;/* minimum left value, temp copy */
2270 xfs_mount_t *mp; /* mount structure pointer */
2271 xfs_agnumber_t sagno; /* starting allocation group number */
2272 xfs_alloctype_t type; /* input allocation type */
2275 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2278 type = args->otype = args->type;
2279 args->agbno = NULLAGBLOCK;
2281 * Just fix this up, for the case where the last a.g. is shorter
2282 * (or there's only one a.g.) and the caller couldn't easily figure
2283 * that out (xfs_bmap_alloc).
2285 agsize = mp->m_sb.sb_agblocks;
2286 if (args->maxlen > agsize)
2287 args->maxlen = agsize;
2288 if (args->alignment == 0)
2289 args->alignment = 1;
2290 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2291 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2292 ASSERT(args->minlen <= args->maxlen);
2293 ASSERT(args->minlen <= agsize);
2294 ASSERT(args->mod < args->prod);
2295 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2296 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2297 args->minlen > args->maxlen || args->minlen > agsize ||
2298 args->mod >= args->prod) {
2299 args->fsbno = NULLFSBLOCK;
2300 TRACE_ALLOC("badargs", args);
2303 minleft = args->minleft;
2306 case XFS_ALLOCTYPE_THIS_AG:
2307 case XFS_ALLOCTYPE_NEAR_BNO:
2308 case XFS_ALLOCTYPE_THIS_BNO:
2310 * These three force us into a single a.g.
2312 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2313 down_read(&mp->m_peraglock);
2314 args->pag = &mp->m_perag[args->agno];
2316 error = xfs_alloc_fix_freelist(args, 0);
2317 args->minleft = minleft;
2319 TRACE_ALLOC("nofix", args);
2323 up_read(&mp->m_peraglock);
2324 TRACE_ALLOC("noagbp", args);
2327 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2328 if ((error = xfs_alloc_ag_vextent(args)))
2330 up_read(&mp->m_peraglock);
2332 case XFS_ALLOCTYPE_START_BNO:
2334 * Try near allocation first, then anywhere-in-ag after
2335 * the first a.g. fails.
2337 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2338 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2339 args->fsbno = XFS_AGB_TO_FSB(mp,
2340 ((mp->m_agfrotor / rotorstep) %
2341 mp->m_sb.sb_agcount), 0);
2344 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2345 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2347 case XFS_ALLOCTYPE_ANY_AG:
2348 case XFS_ALLOCTYPE_START_AG:
2349 case XFS_ALLOCTYPE_FIRST_AG:
2351 * Rotate through the allocation groups looking for a winner.
2353 if (type == XFS_ALLOCTYPE_ANY_AG) {
2355 * Start with the last place we left off.
2357 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2358 mp->m_sb.sb_agcount;
2359 args->type = XFS_ALLOCTYPE_THIS_AG;
2360 flags = XFS_ALLOC_FLAG_TRYLOCK;
2361 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2363 * Start with allocation group given by bno.
2365 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2366 args->type = XFS_ALLOCTYPE_THIS_AG;
2370 if (type == XFS_ALLOCTYPE_START_AG)
2371 args->type = XFS_ALLOCTYPE_THIS_AG;
2373 * Start with the given allocation group.
2375 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2376 flags = XFS_ALLOC_FLAG_TRYLOCK;
2379 * Loop over allocation groups twice; first time with
2380 * trylock set, second time without.
2382 down_read(&mp->m_peraglock);
2384 args->pag = &mp->m_perag[args->agno];
2385 if (no_min) args->minleft = 0;
2386 error = xfs_alloc_fix_freelist(args, flags);
2387 args->minleft = minleft;
2389 TRACE_ALLOC("nofix", args);
2393 * If we get a buffer back then the allocation will fly.
2396 if ((error = xfs_alloc_ag_vextent(args)))
2400 TRACE_ALLOC("loopfailed", args);
2402 * Didn't work, figure out the next iteration.
2404 if (args->agno == sagno &&
2405 type == XFS_ALLOCTYPE_START_BNO)
2406 args->type = XFS_ALLOCTYPE_THIS_AG;
2408 * For the first allocation, we can try any AG to get
2409 * space. However, if we already have allocated a
2410 * block, we don't want to try AGs whose number is below
2411 * sagno. Otherwise, we may end up with out-of-order
2412 * locking of AGF, which might cause deadlock.
2414 if (++(args->agno) == mp->m_sb.sb_agcount) {
2415 if (args->firstblock != NULLFSBLOCK)
2421 * Reached the starting a.g., must either be done
2422 * or switch to non-trylock mode.
2424 if (args->agno == sagno) {
2426 args->agbno = NULLAGBLOCK;
2427 TRACE_ALLOC("allfailed", args);
2434 if (type == XFS_ALLOCTYPE_START_BNO) {
2435 args->agbno = XFS_FSB_TO_AGBNO(mp,
2437 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2442 up_read(&mp->m_peraglock);
2443 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2444 if (args->agno == sagno)
2445 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2446 (mp->m_sb.sb_agcount * rotorstep);
2448 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2449 (mp->m_sb.sb_agcount * rotorstep);
2456 if (args->agbno == NULLAGBLOCK)
2457 args->fsbno = NULLFSBLOCK;
2459 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2461 ASSERT(args->len >= args->minlen);
2462 ASSERT(args->len <= args->maxlen);
2463 ASSERT(args->agbno % args->alignment == 0);
2464 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2470 up_read(&mp->m_peraglock);
2476 * Just break up the extent address and hand off to xfs_free_ag_extent
2477 * after fixing up the freelist.
2481 xfs_trans_t *tp, /* transaction pointer */
2482 xfs_fsblock_t bno, /* starting block number of extent */
2483 xfs_extlen_t len) /* length of extent */
2485 xfs_alloc_arg_t args;
2489 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2491 args.mp = tp->t_mountp;
2492 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2493 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2494 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2495 down_read(&args.mp->m_peraglock);
2496 args.pag = &args.mp->m_perag[args.agno];
2497 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2500 ASSERT(args.agbp != NULL);
2501 ASSERT((args.agbno + len) <=
2502 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2504 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2506 up_read(&args.mp->m_peraglock);
2512 * AG Busy list management
2513 * The busy list contains block ranges that have been freed but whose
2514 * transactions have not yet hit disk. If any block listed in a busy
2515 * list is reused, the transaction that freed it must be forced to disk
2516 * before continuing to use the block.
2518 * xfs_alloc_mark_busy - add to the per-ag busy list
2519 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2522 xfs_alloc_mark_busy(xfs_trans_t *tp,
2523 xfs_agnumber_t agno,
2528 xfs_perag_busy_t *bsy;
2533 s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
2535 /* search pagb_list for an open slot */
2536 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2537 n < XFS_PAGB_NUM_SLOTS;
2539 if (bsy->busy_tp == NULL) {
2544 if (n < XFS_PAGB_NUM_SLOTS) {
2545 bsy = &mp->m_perag[agno].pagb_list[n];
2546 mp->m_perag[agno].pagb_count++;
2547 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2548 bsy->busy_start = bno;
2549 bsy->busy_length = len;
2551 xfs_trans_add_busy(tp, agno, n);
2553 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2555 * The busy list is full! Since it is now not possible to
2556 * track the free block, make this a synchronous transaction
2557 * to insure that the block is not reused before this
2558 * transaction commits.
2560 xfs_trans_set_sync(tp);
2563 mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
2567 xfs_alloc_clear_busy(xfs_trans_t *tp,
2568 xfs_agnumber_t agno,
2572 xfs_perag_busy_t *list;
2577 s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
2578 list = mp->m_perag[agno].pagb_list;
2580 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2581 if (list[idx].busy_tp == tp) {
2582 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2583 list[idx].busy_tp = NULL;
2584 mp->m_perag[agno].pagb_count--;
2586 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2589 mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
2594 * returns non-zero if any of (agno,bno):len is in a busy list
2597 xfs_alloc_search_busy(xfs_trans_t *tp,
2598 xfs_agnumber_t agno,
2603 xfs_perag_busy_t *bsy;
2605 xfs_agblock_t uend, bend;
2612 s = mutex_spinlock(&mp->m_perag[agno].pagb_lock);
2613 cnt = mp->m_perag[agno].pagb_count;
2615 uend = bno + len - 1;
2617 /* search pagb_list for this slot, skipping open slots */
2618 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2622 * (start1,length1) within (start2, length2)
2624 if (bsy->busy_tp != NULL) {
2625 bend = bsy->busy_start + bsy->busy_length - 1;
2627 (uend < bsy->busy_start)) {
2630 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
2631 "found1", agno, bno, len, n,
2639 * If a block was found, force the log through the LSN of the
2640 * transaction that freed the block
2643 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, n, tp);
2644 lsn = bsy->busy_tp->t_commit_lsn;
2645 mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);
2646 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2648 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, n, tp);
2650 mutex_spinunlock(&mp->m_perag[agno].pagb_lock, s);