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);
140 * Update the record referred to by cur to the value given
142 * This either works (return 0) or gets an EFSCORRUPTED error.
144 STATIC int /* error */
146 struct xfs_btree_cur *cur, /* btree cursor */
147 xfs_agblock_t bno, /* starting block of extent */
148 xfs_extlen_t len) /* length of extent */
150 union xfs_btree_rec rec;
152 rec.alloc.ar_startblock = cpu_to_be32(bno);
153 rec.alloc.ar_blockcount = cpu_to_be32(len);
154 return xfs_btree_update(cur, &rec);
158 * Get the data from the pointed-to record.
160 STATIC int /* error */
162 struct xfs_btree_cur *cur, /* btree cursor */
163 xfs_agblock_t *bno, /* output: starting block of extent */
164 xfs_extlen_t *len, /* output: length of extent */
165 int *stat) /* output: success/failure */
167 union xfs_btree_rec *rec;
170 error = xfs_btree_get_rec(cur, &rec, stat);
171 if (!error && *stat == 1) {
172 *bno = be32_to_cpu(rec->alloc.ar_startblock);
173 *len = be32_to_cpu(rec->alloc.ar_blockcount);
179 * Compute aligned version of the found extent.
180 * Takes alignment and min length into account.
183 xfs_alloc_compute_aligned(
184 xfs_agblock_t foundbno, /* starting block in found extent */
185 xfs_extlen_t foundlen, /* length in found extent */
186 xfs_extlen_t alignment, /* alignment for allocation */
187 xfs_extlen_t minlen, /* minimum length for allocation */
188 xfs_agblock_t *resbno, /* result block number */
189 xfs_extlen_t *reslen) /* result length */
195 if (alignment > 1 && foundlen >= minlen) {
196 bno = roundup(foundbno, alignment);
197 diff = bno - foundbno;
198 len = diff >= foundlen ? 0 : foundlen - diff;
208 * Compute best start block and diff for "near" allocations.
209 * freelen >= wantlen already checked by caller.
211 STATIC xfs_extlen_t /* difference value (absolute) */
212 xfs_alloc_compute_diff(
213 xfs_agblock_t wantbno, /* target starting block */
214 xfs_extlen_t wantlen, /* target length */
215 xfs_extlen_t alignment, /* target alignment */
216 xfs_agblock_t freebno, /* freespace's starting block */
217 xfs_extlen_t freelen, /* freespace's length */
218 xfs_agblock_t *newbnop) /* result: best start block from free */
220 xfs_agblock_t freeend; /* end of freespace extent */
221 xfs_agblock_t newbno1; /* return block number */
222 xfs_agblock_t newbno2; /* other new block number */
223 xfs_extlen_t newlen1=0; /* length with newbno1 */
224 xfs_extlen_t newlen2=0; /* length with newbno2 */
225 xfs_agblock_t wantend; /* end of target extent */
227 ASSERT(freelen >= wantlen);
228 freeend = freebno + freelen;
229 wantend = wantbno + wantlen;
230 if (freebno >= wantbno) {
231 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
232 newbno1 = NULLAGBLOCK;
233 } else if (freeend >= wantend && alignment > 1) {
234 newbno1 = roundup(wantbno, alignment);
235 newbno2 = newbno1 - alignment;
236 if (newbno1 >= freeend)
237 newbno1 = NULLAGBLOCK;
239 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
240 if (newbno2 < freebno)
241 newbno2 = NULLAGBLOCK;
243 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
244 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
245 if (newlen1 < newlen2 ||
246 (newlen1 == newlen2 &&
247 XFS_ABSDIFF(newbno1, wantbno) >
248 XFS_ABSDIFF(newbno2, wantbno)))
250 } else if (newbno2 != NULLAGBLOCK)
252 } else if (freeend >= wantend) {
254 } else if (alignment > 1) {
255 newbno1 = roundup(freeend - wantlen, alignment);
256 if (newbno1 > freeend - wantlen &&
257 newbno1 - alignment >= freebno)
258 newbno1 -= alignment;
259 else if (newbno1 >= freeend)
260 newbno1 = NULLAGBLOCK;
262 newbno1 = freeend - wantlen;
264 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
268 * Fix up the length, based on mod and prod.
269 * len should be k * prod + mod for some k.
270 * If len is too small it is returned unchanged.
271 * If len hits maxlen it is left alone.
275 xfs_alloc_arg_t *args) /* allocation argument structure */
280 ASSERT(args->mod < args->prod);
282 ASSERT(rlen >= args->minlen);
283 ASSERT(rlen <= args->maxlen);
284 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
285 (args->mod == 0 && rlen < args->prod))
287 k = rlen % args->prod;
291 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
294 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
298 ASSERT(rlen >= args->minlen);
299 ASSERT(rlen <= args->maxlen);
304 * Fix up length if there is too little space left in the a.g.
305 * Return 1 if ok, 0 if too little, should give up.
308 xfs_alloc_fix_minleft(
309 xfs_alloc_arg_t *args) /* allocation argument structure */
311 xfs_agf_t *agf; /* a.g. freelist header */
312 int diff; /* free space difference */
314 if (args->minleft == 0)
316 agf = XFS_BUF_TO_AGF(args->agbp);
317 diff = be32_to_cpu(agf->agf_freeblks)
318 + be32_to_cpu(agf->agf_flcount)
319 - args->len - args->minleft;
322 args->len += diff; /* shrink the allocated space */
323 if (args->len >= args->minlen)
325 args->agbno = NULLAGBLOCK;
330 * Update the two btrees, logically removing from freespace the extent
331 * starting at rbno, rlen blocks. The extent is contained within the
332 * actual (current) free extent fbno for flen blocks.
333 * Flags are passed in indicating whether the cursors are set to the
336 STATIC int /* error code */
337 xfs_alloc_fixup_trees(
338 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
339 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
340 xfs_agblock_t fbno, /* starting block of free extent */
341 xfs_extlen_t flen, /* length of free extent */
342 xfs_agblock_t rbno, /* starting block of returned extent */
343 xfs_extlen_t rlen, /* length of returned extent */
344 int flags) /* flags, XFSA_FIXUP_... */
346 int error; /* error code */
347 int i; /* operation results */
348 xfs_agblock_t nfbno1; /* first new free startblock */
349 xfs_agblock_t nfbno2; /* second new free startblock */
350 xfs_extlen_t nflen1=0; /* first new free length */
351 xfs_extlen_t nflen2=0; /* second new free length */
354 * Look up the record in the by-size tree if necessary.
356 if (flags & XFSA_FIXUP_CNT_OK) {
358 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
360 XFS_WANT_CORRUPTED_RETURN(
361 i == 1 && nfbno1 == fbno && nflen1 == flen);
364 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
366 XFS_WANT_CORRUPTED_RETURN(i == 1);
369 * Look up the record in the by-block tree if necessary.
371 if (flags & XFSA_FIXUP_BNO_OK) {
373 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
375 XFS_WANT_CORRUPTED_RETURN(
376 i == 1 && nfbno1 == fbno && nflen1 == flen);
379 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
381 XFS_WANT_CORRUPTED_RETURN(i == 1);
385 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
386 struct xfs_btree_block *bnoblock;
387 struct xfs_btree_block *cntblock;
389 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
390 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
392 XFS_WANT_CORRUPTED_RETURN(
393 bnoblock->bb_numrecs == cntblock->bb_numrecs);
398 * Deal with all four cases: the allocated record is contained
399 * within the freespace record, so we can have new freespace
400 * at either (or both) end, or no freespace remaining.
402 if (rbno == fbno && rlen == flen)
403 nfbno1 = nfbno2 = NULLAGBLOCK;
404 else if (rbno == fbno) {
405 nfbno1 = rbno + rlen;
406 nflen1 = flen - rlen;
407 nfbno2 = NULLAGBLOCK;
408 } else if (rbno + rlen == fbno + flen) {
410 nflen1 = flen - rlen;
411 nfbno2 = NULLAGBLOCK;
414 nflen1 = rbno - fbno;
415 nfbno2 = rbno + rlen;
416 nflen2 = (fbno + flen) - nfbno2;
419 * Delete the entry from the by-size btree.
421 if ((error = xfs_btree_delete(cnt_cur, &i)))
423 XFS_WANT_CORRUPTED_RETURN(i == 1);
425 * Add new by-size btree entry(s).
427 if (nfbno1 != NULLAGBLOCK) {
428 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
430 XFS_WANT_CORRUPTED_RETURN(i == 0);
431 if ((error = xfs_btree_insert(cnt_cur, &i)))
433 XFS_WANT_CORRUPTED_RETURN(i == 1);
435 if (nfbno2 != NULLAGBLOCK) {
436 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
438 XFS_WANT_CORRUPTED_RETURN(i == 0);
439 if ((error = xfs_btree_insert(cnt_cur, &i)))
441 XFS_WANT_CORRUPTED_RETURN(i == 1);
444 * Fix up the by-block btree entry(s).
446 if (nfbno1 == NULLAGBLOCK) {
448 * No remaining freespace, just delete the by-block tree entry.
450 if ((error = xfs_btree_delete(bno_cur, &i)))
452 XFS_WANT_CORRUPTED_RETURN(i == 1);
455 * Update the by-block entry to start later|be shorter.
457 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
460 if (nfbno2 != NULLAGBLOCK) {
462 * 2 resulting free entries, need to add one.
464 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
466 XFS_WANT_CORRUPTED_RETURN(i == 0);
467 if ((error = xfs_btree_insert(bno_cur, &i)))
469 XFS_WANT_CORRUPTED_RETURN(i == 1);
475 * Read in the allocation group free block array.
477 STATIC int /* error */
479 xfs_mount_t *mp, /* mount point structure */
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_agnumber_t agno, /* allocation group number */
482 xfs_buf_t **bpp) /* buffer for the ag free block array */
484 xfs_buf_t *bp; /* return value */
487 ASSERT(agno != NULLAGNUMBER);
488 error = xfs_trans_read_buf(
489 mp, tp, mp->m_ddev_targp,
490 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
491 XFS_FSS_TO_BB(mp, 1), 0, &bp);
495 ASSERT(!XFS_BUF_GETERROR(bp));
496 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
501 #if defined(XFS_ALLOC_TRACE)
503 * Add an allocation trace entry for an alloc call.
506 xfs_alloc_trace_alloc(
507 const char *name, /* function tag string */
508 char *str, /* additional string */
509 xfs_alloc_arg_t *args, /* allocation argument structure */
510 int line) /* source line number */
512 ktrace_enter(xfs_alloc_trace_buf,
513 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
517 (void *)(__psunsigned_t)args->agno,
518 (void *)(__psunsigned_t)args->agbno,
519 (void *)(__psunsigned_t)args->minlen,
520 (void *)(__psunsigned_t)args->maxlen,
521 (void *)(__psunsigned_t)args->mod,
522 (void *)(__psunsigned_t)args->prod,
523 (void *)(__psunsigned_t)args->minleft,
524 (void *)(__psunsigned_t)args->total,
525 (void *)(__psunsigned_t)args->alignment,
526 (void *)(__psunsigned_t)args->len,
527 (void *)((((__psint_t)args->type) << 16) |
528 (__psint_t)args->otype),
529 (void *)(__psint_t)((args->wasdel << 3) |
530 (args->wasfromfl << 2) |
532 (args->userdata << 0)));
536 * Add an allocation trace entry for a free call.
539 xfs_alloc_trace_free(
540 const char *name, /* function tag string */
541 char *str, /* additional string */
542 xfs_mount_t *mp, /* file system mount point */
543 xfs_agnumber_t agno, /* allocation group number */
544 xfs_agblock_t agbno, /* a.g. relative block number */
545 xfs_extlen_t len, /* length of extent */
546 int isfl, /* set if is freelist allocation/free */
547 int line) /* source line number */
549 ktrace_enter(xfs_alloc_trace_buf,
550 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
554 (void *)(__psunsigned_t)agno,
555 (void *)(__psunsigned_t)agbno,
556 (void *)(__psunsigned_t)len,
557 (void *)(__psint_t)isfl,
558 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
562 * Add an allocation trace entry for modifying an agf.
565 xfs_alloc_trace_modagf(
566 const char *name, /* function tag string */
567 char *str, /* additional string */
568 xfs_mount_t *mp, /* file system mount point */
569 xfs_agf_t *agf, /* new agf value */
570 int flags, /* logging flags for agf */
571 int line) /* source line number */
573 ktrace_enter(xfs_alloc_trace_buf,
574 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
578 (void *)(__psint_t)flags,
579 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
580 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
581 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
582 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
583 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
584 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
585 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
586 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
587 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
588 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
589 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
593 xfs_alloc_trace_busy(
594 const char *name, /* function tag string */
595 char *str, /* additional string */
596 xfs_mount_t *mp, /* file system mount point */
597 xfs_agnumber_t agno, /* allocation group number */
598 xfs_agblock_t agbno, /* a.g. relative block number */
599 xfs_extlen_t len, /* length of extent */
600 int slot, /* perag Busy slot */
602 int trtype, /* type: add, delete, search */
603 int line) /* source line number */
605 ktrace_enter(xfs_alloc_trace_buf,
606 (void *)(__psint_t)(trtype | (line << 16)),
610 (void *)(__psunsigned_t)agno,
611 (void *)(__psunsigned_t)agbno,
612 (void *)(__psunsigned_t)len,
613 (void *)(__psint_t)slot,
615 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
617 #endif /* XFS_ALLOC_TRACE */
620 * Allocation group level functions.
624 * Allocate a variable extent in the allocation group agno.
625 * Type and bno are used to determine where in the allocation group the
627 * Extent's length (returned in *len) will be between minlen and maxlen,
628 * and of the form k * prod + mod unless there's nothing that large.
629 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
631 STATIC int /* error */
632 xfs_alloc_ag_vextent(
633 xfs_alloc_arg_t *args) /* argument structure for allocation */
637 ASSERT(args->minlen > 0);
638 ASSERT(args->maxlen > 0);
639 ASSERT(args->minlen <= args->maxlen);
640 ASSERT(args->mod < args->prod);
641 ASSERT(args->alignment > 0);
643 * Branch to correct routine based on the type.
646 switch (args->type) {
647 case XFS_ALLOCTYPE_THIS_AG:
648 error = xfs_alloc_ag_vextent_size(args);
650 case XFS_ALLOCTYPE_NEAR_BNO:
651 error = xfs_alloc_ag_vextent_near(args);
653 case XFS_ALLOCTYPE_THIS_BNO:
654 error = xfs_alloc_ag_vextent_exact(args);
663 * If the allocation worked, need to change the agf structure
664 * (and log it), and the superblock.
666 if (args->agbno != NULLAGBLOCK) {
667 xfs_agf_t *agf; /* allocation group freelist header */
668 #ifdef XFS_ALLOC_TRACE
669 xfs_mount_t *mp = args->mp;
671 long slen = (long)args->len;
673 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
674 ASSERT(!(args->wasfromfl) || !args->isfl);
675 ASSERT(args->agbno % args->alignment == 0);
676 if (!(args->wasfromfl)) {
678 agf = XFS_BUF_TO_AGF(args->agbp);
679 be32_add_cpu(&agf->agf_freeblks, -(args->len));
680 xfs_trans_agblocks_delta(args->tp,
681 -((long)(args->len)));
682 args->pag->pagf_freeblks -= args->len;
683 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
684 be32_to_cpu(agf->agf_length));
685 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
686 xfs_alloc_log_agf(args->tp, args->agbp,
688 /* search the busylist for these blocks */
689 xfs_alloc_search_busy(args->tp, args->agno,
690 args->agbno, args->len);
693 xfs_trans_mod_sb(args->tp,
694 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
695 XFS_TRANS_SB_FDBLOCKS, -slen);
696 XFS_STATS_INC(xs_allocx);
697 XFS_STATS_ADD(xs_allocb, args->len);
703 * Allocate a variable extent at exactly agno/bno.
704 * Extent's length (returned in *len) will be between minlen and maxlen,
705 * and of the form k * prod + mod unless there's nothing that large.
706 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
708 STATIC int /* error */
709 xfs_alloc_ag_vextent_exact(
710 xfs_alloc_arg_t *args) /* allocation argument structure */
712 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
713 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
714 xfs_agblock_t end; /* end of allocated extent */
716 xfs_agblock_t fbno; /* start block of found extent */
717 xfs_agblock_t fend; /* end block of found extent */
718 xfs_extlen_t flen; /* length of found extent */
719 int i; /* success/failure of operation */
720 xfs_agblock_t maxend; /* end of maximal extent */
721 xfs_agblock_t minend; /* end of minimal extent */
722 xfs_extlen_t rlen; /* length of returned extent */
724 ASSERT(args->alignment == 1);
726 * Allocate/initialize a cursor for the by-number freespace btree.
728 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
729 args->agno, XFS_BTNUM_BNO);
731 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
732 * Look for the closest free block <= bno, it must contain bno
733 * if any free block does.
735 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
739 * Didn't find it, return null.
741 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
742 args->agbno = NULLAGBLOCK;
746 * Grab the freespace record.
748 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
750 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
751 ASSERT(fbno <= args->agbno);
752 minend = args->agbno + args->minlen;
753 maxend = args->agbno + args->maxlen;
756 * Give up if the freespace isn't long enough for the minimum request.
759 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
760 args->agbno = NULLAGBLOCK;
764 * End of extent will be smaller of the freespace end and the
765 * maximal requested end.
767 end = XFS_AGBLOCK_MIN(fend, maxend);
769 * Fix the length according to mod and prod if given.
771 args->len = end - args->agbno;
772 xfs_alloc_fix_len(args);
773 if (!xfs_alloc_fix_minleft(args)) {
774 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
778 ASSERT(args->agbno + rlen <= fend);
779 end = args->agbno + rlen;
781 * We are allocating agbno for rlen [agbno .. end]
782 * Allocate/initialize a cursor for the by-size btree.
784 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
785 args->agno, XFS_BTNUM_CNT);
786 ASSERT(args->agbno + args->len <=
787 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
788 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
789 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
790 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
793 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
795 TRACE_ALLOC("normal", args);
800 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
801 TRACE_ALLOC("error", args);
806 * Allocate a variable extent near bno in the allocation group agno.
807 * Extent's length (returned in len) will be between minlen and maxlen,
808 * and of the form k * prod + mod unless there's nothing that large.
809 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
811 STATIC int /* error */
812 xfs_alloc_ag_vextent_near(
813 xfs_alloc_arg_t *args) /* allocation argument structure */
815 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
816 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
817 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
818 xfs_agblock_t gtbno; /* start bno of right side entry */
819 xfs_agblock_t gtbnoa; /* aligned ... */
820 xfs_extlen_t gtdiff; /* difference to right side entry */
821 xfs_extlen_t gtlen; /* length of right side entry */
822 xfs_extlen_t gtlena; /* aligned ... */
823 xfs_agblock_t gtnew; /* useful start bno of right side */
824 int error; /* error code */
825 int i; /* result code, temporary */
826 int j; /* result code, temporary */
827 xfs_agblock_t ltbno; /* start bno of left side entry */
828 xfs_agblock_t ltbnoa; /* aligned ... */
829 xfs_extlen_t ltdiff; /* difference to left side entry */
831 xfs_agblock_t ltend; /* end bno of left side entry */
832 xfs_extlen_t ltlen; /* length of left side entry */
833 xfs_extlen_t ltlena; /* aligned ... */
834 xfs_agblock_t ltnew; /* useful start bno of left side */
835 xfs_extlen_t rlen; /* length of returned extent */
836 #if defined(DEBUG) && defined(__KERNEL__)
838 * Randomly don't execute the first algorithm.
840 int dofirst; /* set to do first algorithm */
842 dofirst = random32() & 1;
845 * Get a cursor for the by-size btree.
847 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
848 args->agno, XFS_BTNUM_CNT);
850 bno_cur_lt = bno_cur_gt = NULL;
852 * See if there are any free extents as big as maxlen.
854 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
857 * If none, then pick up the last entry in the tree unless the
861 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
864 if (i == 0 || ltlen == 0) {
865 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
873 * If the requested extent is large wrt the freespaces available
874 * in this a.g., then the cursor will be pointing to a btree entry
875 * near the right edge of the tree. If it's in the last btree leaf
876 * block, then we just examine all the entries in that block
877 * that are big enough, and pick the best one.
878 * This is written as a while loop so we can break out of it,
879 * but we never loop back to the top.
881 while (xfs_btree_islastblock(cnt_cur, 0)) {
885 xfs_agblock_t bnew=0;
887 #if defined(DEBUG) && defined(__KERNEL__)
892 * Start from the entry that lookup found, sequence through
893 * all larger free blocks. If we're actually pointing at a
894 * record smaller than maxlen, go to the start of this block,
895 * and skip all those smaller than minlen.
897 if (ltlen || args->alignment > 1) {
898 cnt_cur->bc_ptrs[0] = 1;
900 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
903 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
904 if (ltlen >= args->minlen)
906 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
909 ASSERT(ltlen >= args->minlen);
913 i = cnt_cur->bc_ptrs[0];
914 for (j = 1, blen = 0, bdiff = 0;
915 !error && j && (blen < args->maxlen || bdiff > 0);
916 error = xfs_btree_increment(cnt_cur, 0, &j)) {
918 * For each entry, decide if it's better than
919 * the previous best entry.
921 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
923 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
924 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
925 args->minlen, <bnoa, <lena);
926 if (ltlena < args->minlen)
928 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
929 xfs_alloc_fix_len(args);
930 ASSERT(args->len >= args->minlen);
931 if (args->len < blen)
933 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
934 args->alignment, ltbno, ltlen, <new);
935 if (ltnew != NULLAGBLOCK &&
936 (args->len > blen || ltdiff < bdiff)) {
940 besti = cnt_cur->bc_ptrs[0];
944 * It didn't work. We COULD be in a case where
945 * there's a good record somewhere, so try again.
950 * Point at the best entry, and retrieve it again.
952 cnt_cur->bc_ptrs[0] = besti;
953 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
955 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
956 ltend = ltbno + ltlen;
957 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
959 if (!xfs_alloc_fix_minleft(args)) {
960 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
961 TRACE_ALLOC("nominleft", args);
966 * We are allocating starting at bnew for blen blocks.
969 ASSERT(bnew >= ltbno);
970 ASSERT(bnew + blen <= ltend);
972 * Set up a cursor for the by-bno tree.
974 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
975 args->agbp, args->agno, XFS_BTNUM_BNO);
977 * Fix up the btree entries.
979 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
980 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
982 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
983 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
984 TRACE_ALLOC("first", args);
989 * Search in the by-bno tree to the left and to the right
990 * simultaneously, until in each case we find a space big enough,
991 * or run into the edge of the tree. When we run into the edge,
992 * we deallocate that cursor.
993 * If both searches succeed, we compare the two spaces and pick
995 * With alignment, it's possible for both to fail; the upper
996 * level algorithm that picks allocation groups for allocations
997 * is not supposed to do this.
1000 * Allocate and initialize the cursor for the leftward search.
1002 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1003 args->agno, XFS_BTNUM_BNO);
1005 * Lookup <= bno to find the leftward search's starting point.
1007 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1011 * Didn't find anything; use this cursor for the rightward
1014 bno_cur_gt = bno_cur_lt;
1018 * Found something. Duplicate the cursor for the rightward search.
1020 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1023 * Increment the cursor, so we will point at the entry just right
1024 * of the leftward entry if any, or to the leftmost entry.
1026 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1030 * It failed, there are no rightward entries.
1032 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1036 * Loop going left with the leftward cursor, right with the
1037 * rightward cursor, until either both directions give up or
1038 * we find an entry at least as big as minlen.
1042 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1044 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1045 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
1046 args->minlen, <bnoa, <lena);
1047 if (ltlena >= args->minlen)
1049 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1052 xfs_btree_del_cursor(bno_cur_lt,
1058 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1060 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1061 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
1062 args->minlen, >bnoa, >lena);
1063 if (gtlena >= args->minlen)
1065 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1068 xfs_btree_del_cursor(bno_cur_gt,
1073 } while (bno_cur_lt || bno_cur_gt);
1075 * Got both cursors still active, need to find better entry.
1077 if (bno_cur_lt && bno_cur_gt) {
1079 * Left side is long enough, look for a right side entry.
1081 if (ltlena >= args->minlen) {
1083 * Fix up the length.
1085 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1086 xfs_alloc_fix_len(args);
1088 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1089 args->alignment, ltbno, ltlen, <new);
1095 * Look until we find a better one, run out of
1096 * space, or run off the end.
1098 while (bno_cur_lt && bno_cur_gt) {
1099 if ((error = xfs_alloc_get_rec(
1103 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1104 xfs_alloc_compute_aligned(gtbno, gtlen,
1105 args->alignment, args->minlen,
1108 * The left one is clearly better.
1110 if (gtbnoa >= args->agbno + ltdiff) {
1111 xfs_btree_del_cursor(
1118 * If we reach a big enough entry,
1119 * compare the two and pick the best.
1121 if (gtlena >= args->minlen) {
1123 XFS_EXTLEN_MIN(gtlena,
1125 xfs_alloc_fix_len(args);
1127 gtdiff = xfs_alloc_compute_diff(
1130 gtbno, gtlen, >new);
1132 * Right side is better.
1134 if (gtdiff < ltdiff) {
1135 xfs_btree_del_cursor(
1141 * Left side is better.
1144 xfs_btree_del_cursor(
1152 * Fell off the right end.
1154 if ((error = xfs_btree_increment(
1155 bno_cur_gt, 0, &i)))
1158 xfs_btree_del_cursor(
1167 * The left side is perfect, trash the right side.
1170 xfs_btree_del_cursor(bno_cur_gt,
1176 * It's the right side that was found first, look left.
1180 * Fix up the length.
1182 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1183 xfs_alloc_fix_len(args);
1185 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1186 args->alignment, gtbno, gtlen, >new);
1188 * Right side entry isn't perfect.
1192 * Look until we find a better one, run out of
1193 * space, or run off the end.
1195 while (bno_cur_lt && bno_cur_gt) {
1196 if ((error = xfs_alloc_get_rec(
1200 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1201 xfs_alloc_compute_aligned(ltbno, ltlen,
1202 args->alignment, args->minlen,
1205 * The right one is clearly better.
1207 if (ltbnoa <= args->agbno - gtdiff) {
1208 xfs_btree_del_cursor(
1215 * If we reach a big enough entry,
1216 * compare the two and pick the best.
1218 if (ltlena >= args->minlen) {
1219 args->len = XFS_EXTLEN_MIN(
1220 ltlena, args->maxlen);
1221 xfs_alloc_fix_len(args);
1223 ltdiff = xfs_alloc_compute_diff(
1226 ltbno, ltlen, <new);
1228 * Left side is better.
1230 if (ltdiff < gtdiff) {
1231 xfs_btree_del_cursor(
1237 * Right side is better.
1240 xfs_btree_del_cursor(
1248 * Fell off the left end.
1250 if ((error = xfs_btree_decrement(
1251 bno_cur_lt, 0, &i)))
1254 xfs_btree_del_cursor(bno_cur_lt,
1262 * The right side is perfect, trash the left side.
1265 xfs_btree_del_cursor(bno_cur_lt,
1272 * If we couldn't get anything, give up.
1274 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1275 TRACE_ALLOC("neither", args);
1276 args->agbno = NULLAGBLOCK;
1280 * At this point we have selected a freespace entry, either to the
1281 * left or to the right. If it's on the right, copy all the
1282 * useful variables to the "left" set so we only have one
1283 * copy of this code.
1286 bno_cur_lt = bno_cur_gt;
1296 * Fix up the length and compute the useful address.
1298 ltend = ltbno + ltlen;
1299 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1300 xfs_alloc_fix_len(args);
1301 if (!xfs_alloc_fix_minleft(args)) {
1302 TRACE_ALLOC("nominleft", args);
1303 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1304 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1308 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1310 ASSERT(ltnew >= ltbno);
1311 ASSERT(ltnew + rlen <= ltend);
1312 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1313 args->agbno = ltnew;
1314 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1315 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1317 TRACE_ALLOC(j ? "gt" : "lt", args);
1318 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1319 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1323 TRACE_ALLOC("error", args);
1324 if (cnt_cur != NULL)
1325 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1326 if (bno_cur_lt != NULL)
1327 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1328 if (bno_cur_gt != NULL)
1329 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1334 * Allocate a variable extent anywhere in the allocation group agno.
1335 * Extent's length (returned in len) will be between minlen and maxlen,
1336 * and of the form k * prod + mod unless there's nothing that large.
1337 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1339 STATIC int /* error */
1340 xfs_alloc_ag_vextent_size(
1341 xfs_alloc_arg_t *args) /* allocation argument structure */
1343 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1344 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1345 int error; /* error result */
1346 xfs_agblock_t fbno; /* start of found freespace */
1347 xfs_extlen_t flen; /* length of found freespace */
1348 int i; /* temp status variable */
1349 xfs_agblock_t rbno; /* returned block number */
1350 xfs_extlen_t rlen; /* length of returned extent */
1353 * Allocate and initialize a cursor for the by-size btree.
1355 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1356 args->agno, XFS_BTNUM_CNT);
1359 * Look for an entry >= maxlen+alignment-1 blocks.
1361 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1362 args->maxlen + args->alignment - 1, &i)))
1365 * If none, then pick up the last entry in the tree unless the
1369 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1372 if (i == 0 || flen == 0) {
1373 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1374 TRACE_ALLOC("noentry", args);
1380 * There's a freespace as big as maxlen+alignment-1, get it.
1383 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1385 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1388 * In the first case above, we got the last entry in the
1389 * by-size btree. Now we check to see if the space hits maxlen
1390 * once aligned; if not, we search left for something better.
1391 * This can't happen in the second case above.
1393 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1395 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1396 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1397 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1398 if (rlen < args->maxlen) {
1399 xfs_agblock_t bestfbno;
1400 xfs_extlen_t bestflen;
1401 xfs_agblock_t bestrbno;
1402 xfs_extlen_t bestrlen;
1409 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1413 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1416 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1417 if (flen < bestrlen)
1419 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1420 args->minlen, &rbno, &rlen);
1421 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1422 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1423 (rlen <= flen && rbno + rlen <= fbno + flen),
1425 if (rlen > bestrlen) {
1430 if (rlen == args->maxlen)
1434 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1437 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1443 args->wasfromfl = 0;
1445 * Fix up the length.
1448 xfs_alloc_fix_len(args);
1449 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1450 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1451 TRACE_ALLOC("nominleft", args);
1452 args->agbno = NULLAGBLOCK;
1456 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1458 * Allocate and initialize a cursor for the by-block tree.
1460 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1461 args->agno, XFS_BTNUM_BNO);
1462 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1463 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1465 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1466 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1467 cnt_cur = bno_cur = NULL;
1470 XFS_WANT_CORRUPTED_GOTO(
1471 args->agbno + args->len <=
1472 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1474 TRACE_ALLOC("normal", args);
1478 TRACE_ALLOC("error", args);
1480 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1482 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1487 * Deal with the case where only small freespaces remain.
1488 * Either return the contents of the last freespace record,
1489 * or allocate space from the freelist if there is nothing in the tree.
1491 STATIC int /* error */
1492 xfs_alloc_ag_vextent_small(
1493 xfs_alloc_arg_t *args, /* allocation argument structure */
1494 xfs_btree_cur_t *ccur, /* by-size cursor */
1495 xfs_agblock_t *fbnop, /* result block number */
1496 xfs_extlen_t *flenp, /* result length */
1497 int *stat) /* status: 0-freelist, 1-normal/none */
1504 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1507 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1509 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1512 * Nothing in the btree, try the freelist. Make sure
1513 * to respect minleft even when pulling from the
1516 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1517 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1519 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1522 if (fbno != NULLAGBLOCK) {
1523 if (args->userdata) {
1526 bp = xfs_btree_get_bufs(args->mp, args->tp,
1527 args->agno, fbno, 0);
1528 xfs_trans_binval(args->tp, bp);
1532 XFS_WANT_CORRUPTED_GOTO(
1533 args->agbno + args->len <=
1534 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1536 args->wasfromfl = 1;
1537 TRACE_ALLOC("freelist", args);
1542 * Nothing in the freelist.
1548 * Can't allocate from the freelist for some reason.
1555 * Can't do the allocation, give up.
1557 if (flen < args->minlen) {
1558 args->agbno = NULLAGBLOCK;
1559 TRACE_ALLOC("notenough", args);
1565 TRACE_ALLOC("normal", args);
1569 TRACE_ALLOC("error", args);
1574 * Free the extent starting at agno/bno for length.
1576 STATIC int /* error */
1578 xfs_trans_t *tp, /* transaction pointer */
1579 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1580 xfs_agnumber_t agno, /* allocation group number */
1581 xfs_agblock_t bno, /* starting block number */
1582 xfs_extlen_t len, /* length of extent */
1583 int isfl) /* set if is freelist blocks - no sb acctg */
1585 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1586 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1587 int error; /* error return value */
1588 xfs_agblock_t gtbno; /* start of right neighbor block */
1589 xfs_extlen_t gtlen; /* length of right neighbor block */
1590 int haveleft; /* have a left neighbor block */
1591 int haveright; /* have a right neighbor block */
1592 int i; /* temp, result code */
1593 xfs_agblock_t ltbno; /* start of left neighbor block */
1594 xfs_extlen_t ltlen; /* length of left neighbor block */
1595 xfs_mount_t *mp; /* mount point struct for filesystem */
1596 xfs_agblock_t nbno; /* new starting block of freespace */
1597 xfs_extlen_t nlen; /* new length of freespace */
1601 * Allocate and initialize a cursor for the by-block btree.
1603 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1606 * Look for a neighboring block on the left (lower block numbers)
1607 * that is contiguous with this space.
1609 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1613 * There is a block to our left.
1615 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1617 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1619 * It's not contiguous, though.
1621 if (ltbno + ltlen < bno)
1625 * If this failure happens the request to free this
1626 * space was invalid, it's (partly) already free.
1629 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1633 * Look for a neighboring block on the right (higher block numbers)
1634 * that is contiguous with this space.
1636 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1640 * There is a block to our right.
1642 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1644 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1646 * It's not contiguous, though.
1648 if (bno + len < gtbno)
1652 * If this failure happens the request to free this
1653 * space was invalid, it's (partly) already free.
1656 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1660 * Now allocate and initialize a cursor for the by-size tree.
1662 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1664 * Have both left and right contiguous neighbors.
1665 * Merge all three into a single free block.
1667 if (haveleft && haveright) {
1669 * Delete the old by-size entry on the left.
1671 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1673 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1674 if ((error = xfs_btree_delete(cnt_cur, &i)))
1676 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1678 * Delete the old by-size entry on the right.
1680 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1682 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1683 if ((error = xfs_btree_delete(cnt_cur, &i)))
1685 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1687 * Delete the old by-block entry for the right block.
1689 if ((error = xfs_btree_delete(bno_cur, &i)))
1691 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1693 * Move the by-block cursor back to the left neighbor.
1695 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1697 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1700 * Check that this is the right record: delete didn't
1701 * mangle the cursor.
1704 xfs_agblock_t xxbno;
1707 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1710 XFS_WANT_CORRUPTED_GOTO(
1711 i == 1 && xxbno == ltbno && xxlen == ltlen,
1716 * Update remaining by-block entry to the new, joined block.
1719 nlen = len + ltlen + gtlen;
1720 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1724 * Have only a left contiguous neighbor.
1725 * Merge it together with the new freespace.
1727 else if (haveleft) {
1729 * Delete the old by-size entry on the left.
1731 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1733 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1734 if ((error = xfs_btree_delete(cnt_cur, &i)))
1736 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1738 * Back up the by-block cursor to the left neighbor, and
1739 * update its length.
1741 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1743 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1746 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1750 * Have only a right contiguous neighbor.
1751 * Merge it together with the new freespace.
1753 else if (haveright) {
1755 * Delete the old by-size entry on the right.
1757 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1759 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1760 if ((error = xfs_btree_delete(cnt_cur, &i)))
1762 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1764 * Update the starting block and length of the right
1765 * neighbor in the by-block tree.
1769 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1773 * No contiguous neighbors.
1774 * Insert the new freespace into the by-block tree.
1779 if ((error = xfs_btree_insert(bno_cur, &i)))
1781 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1783 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1786 * In all cases we need to insert the new freespace in the by-size tree.
1788 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1790 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1791 if ((error = xfs_btree_insert(cnt_cur, &i)))
1793 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1797 * Update the freespace totals in the ag and superblock.
1801 xfs_perag_t *pag; /* per allocation group data */
1803 agf = XFS_BUF_TO_AGF(agbp);
1804 pag = &mp->m_perag[agno];
1805 be32_add_cpu(&agf->agf_freeblks, len);
1806 xfs_trans_agblocks_delta(tp, len);
1807 pag->pagf_freeblks += len;
1808 XFS_WANT_CORRUPTED_GOTO(
1809 be32_to_cpu(agf->agf_freeblks) <=
1810 be32_to_cpu(agf->agf_length),
1812 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1813 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1815 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1816 XFS_STATS_INC(xs_freex);
1817 XFS_STATS_ADD(xs_freeb, len);
1819 TRACE_FREE(haveleft ?
1820 (haveright ? "both" : "left") :
1821 (haveright ? "right" : "none"),
1822 agno, bno, len, isfl);
1825 * Since blocks move to the free list without the coordination
1826 * used in xfs_bmap_finish, we can't allow block to be available
1827 * for reallocation and non-transaction writing (user data)
1828 * until we know that the transaction that moved it to the free
1829 * list is permanently on disk. We track the blocks by declaring
1830 * these blocks as "busy"; the busy list is maintained on a per-ag
1831 * basis and each transaction records which entries should be removed
1832 * when the iclog commits to disk. If a busy block is allocated,
1833 * the iclog is pushed up to the LSN that freed the block.
1835 xfs_alloc_mark_busy(tp, agno, bno, len);
1839 TRACE_FREE("error", agno, bno, len, isfl);
1841 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1843 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1848 * Visible (exported) allocation/free functions.
1849 * Some of these are used just by xfs_alloc_btree.c and this file.
1853 * Compute and fill in value of m_ag_maxlevels.
1856 xfs_alloc_compute_maxlevels(
1857 xfs_mount_t *mp) /* file system mount structure */
1865 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1866 minleafrecs = mp->m_alloc_mnr[0];
1867 minnoderecs = mp->m_alloc_mnr[1];
1868 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1869 for (level = 1; maxblocks > 1; level++)
1870 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1871 mp->m_ag_maxlevels = level;
1875 * Find the length of the longest extent in an AG.
1878 xfs_alloc_longest_free_extent(
1879 struct xfs_mount *mp,
1880 struct xfs_perag *pag)
1882 xfs_extlen_t need, delta = 0;
1884 need = XFS_MIN_FREELIST_PAG(pag, mp);
1885 if (need > pag->pagf_flcount)
1886 delta = need - pag->pagf_flcount;
1888 if (pag->pagf_longest > delta)
1889 return pag->pagf_longest - delta;
1890 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1894 * Decide whether to use this allocation group for this allocation.
1895 * If so, fix up the btree freelist's size.
1897 STATIC int /* error */
1898 xfs_alloc_fix_freelist(
1899 xfs_alloc_arg_t *args, /* allocation argument structure */
1900 int flags) /* XFS_ALLOC_FLAG_... */
1902 xfs_buf_t *agbp; /* agf buffer pointer */
1903 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1904 xfs_buf_t *agflbp;/* agfl buffer pointer */
1905 xfs_agblock_t bno; /* freelist block */
1906 xfs_extlen_t delta; /* new blocks needed in freelist */
1907 int error; /* error result code */
1908 xfs_extlen_t longest;/* longest extent in allocation group */
1909 xfs_mount_t *mp; /* file system mount point structure */
1910 xfs_extlen_t need; /* total blocks needed in freelist */
1911 xfs_perag_t *pag; /* per-ag information structure */
1912 xfs_alloc_arg_t targs; /* local allocation arguments */
1913 xfs_trans_t *tp; /* transaction pointer */
1919 if (!pag->pagf_init) {
1920 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1923 if (!pag->pagf_init) {
1924 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1925 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1933 * If this is a metadata preferred pag and we are user data
1934 * then try somewhere else if we are not being asked to
1935 * try harder at this point
1937 if (pag->pagf_metadata && args->userdata &&
1938 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1939 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1944 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1946 * If it looks like there isn't a long enough extent, or enough
1947 * total blocks, reject it.
1949 need = XFS_MIN_FREELIST_PAG(pag, mp);
1950 longest = xfs_alloc_longest_free_extent(mp, pag);
1951 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1953 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1954 need - args->total) < (int)args->minleft)) {
1956 xfs_trans_brelse(tp, agbp);
1963 * Get the a.g. freespace buffer.
1964 * Can fail if we're not blocking on locks, and it's held.
1967 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1971 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1972 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1978 * Figure out how many blocks we should have in the freelist.
1980 agf = XFS_BUF_TO_AGF(agbp);
1981 need = XFS_MIN_FREELIST(agf, mp);
1983 * If there isn't enough total or single-extent, reject it.
1985 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1986 delta = need > be32_to_cpu(agf->agf_flcount) ?
1987 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1988 longest = be32_to_cpu(agf->agf_longest);
1989 longest = (longest > delta) ? (longest - delta) :
1990 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1991 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1993 ((int)(be32_to_cpu(agf->agf_freeblks) +
1994 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1995 (int)args->minleft)) {
1996 xfs_trans_brelse(tp, agbp);
2002 * Make the freelist shorter if it's too long.
2004 while (be32_to_cpu(agf->agf_flcount) > need) {
2007 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2010 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
2012 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2013 xfs_trans_binval(tp, bp);
2016 * Initialize the args structure.
2021 targs.agno = args->agno;
2022 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
2023 targs.minalignslop = 0;
2024 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2025 targs.type = XFS_ALLOCTYPE_THIS_AG;
2027 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
2030 * Make the freelist longer if it's too short.
2032 while (be32_to_cpu(agf->agf_flcount) < need) {
2034 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
2036 * Allocate as many blocks as possible at once.
2038 if ((error = xfs_alloc_ag_vextent(&targs))) {
2039 xfs_trans_brelse(tp, agflbp);
2043 * Stop if we run out. Won't happen if callers are obeying
2044 * the restrictions correctly. Can happen for free calls
2045 * on a completely full ag.
2047 if (targs.agbno == NULLAGBLOCK) {
2048 if (flags & XFS_ALLOC_FLAG_FREEING)
2050 xfs_trans_brelse(tp, agflbp);
2055 * Put each allocated block on the list.
2057 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2058 error = xfs_alloc_put_freelist(tp, agbp,
2064 xfs_trans_brelse(tp, agflbp);
2070 * Get a block from the freelist.
2071 * Returns with the buffer for the block gotten.
2074 xfs_alloc_get_freelist(
2075 xfs_trans_t *tp, /* transaction pointer */
2076 xfs_buf_t *agbp, /* buffer containing the agf structure */
2077 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2078 int btreeblk) /* destination is a AGF btree */
2080 xfs_agf_t *agf; /* a.g. freespace structure */
2081 xfs_agfl_t *agfl; /* a.g. freelist structure */
2082 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2083 xfs_agblock_t bno; /* block number returned */
2086 xfs_mount_t *mp; /* mount structure */
2087 xfs_perag_t *pag; /* per allocation group data */
2089 agf = XFS_BUF_TO_AGF(agbp);
2091 * Freelist is empty, give up.
2093 if (!agf->agf_flcount) {
2094 *bnop = NULLAGBLOCK;
2098 * Read the array of free blocks.
2101 if ((error = xfs_alloc_read_agfl(mp, tp,
2102 be32_to_cpu(agf->agf_seqno), &agflbp)))
2104 agfl = XFS_BUF_TO_AGFL(agflbp);
2106 * Get the block number and update the data structures.
2108 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2109 be32_add_cpu(&agf->agf_flfirst, 1);
2110 xfs_trans_brelse(tp, agflbp);
2111 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2112 agf->agf_flfirst = 0;
2113 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2114 be32_add_cpu(&agf->agf_flcount, -1);
2115 xfs_trans_agflist_delta(tp, -1);
2116 pag->pagf_flcount--;
2118 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2120 be32_add_cpu(&agf->agf_btreeblks, 1);
2121 pag->pagf_btreeblks++;
2122 logflags |= XFS_AGF_BTREEBLKS;
2125 TRACE_MODAGF(NULL, agf, logflags);
2126 xfs_alloc_log_agf(tp, agbp, logflags);
2130 * As blocks are freed, they are added to the per-ag busy list
2131 * and remain there until the freeing transaction is committed to
2132 * disk. Now that we have allocated blocks, this list must be
2133 * searched to see if a block is being reused. If one is, then
2134 * the freeing transaction must be pushed to disk NOW by forcing
2135 * to disk all iclogs up that transaction's LSN.
2137 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
2142 * Log the given fields from the agf structure.
2146 xfs_trans_t *tp, /* transaction pointer */
2147 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2148 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2150 int first; /* first byte offset */
2151 int last; /* last byte offset */
2152 static const short offsets[] = {
2153 offsetof(xfs_agf_t, agf_magicnum),
2154 offsetof(xfs_agf_t, agf_versionnum),
2155 offsetof(xfs_agf_t, agf_seqno),
2156 offsetof(xfs_agf_t, agf_length),
2157 offsetof(xfs_agf_t, agf_roots[0]),
2158 offsetof(xfs_agf_t, agf_levels[0]),
2159 offsetof(xfs_agf_t, agf_flfirst),
2160 offsetof(xfs_agf_t, agf_fllast),
2161 offsetof(xfs_agf_t, agf_flcount),
2162 offsetof(xfs_agf_t, agf_freeblks),
2163 offsetof(xfs_agf_t, agf_longest),
2164 offsetof(xfs_agf_t, agf_btreeblks),
2168 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2169 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2173 * Interface for inode allocation to force the pag data to be initialized.
2176 xfs_alloc_pagf_init(
2177 xfs_mount_t *mp, /* file system mount structure */
2178 xfs_trans_t *tp, /* transaction pointer */
2179 xfs_agnumber_t agno, /* allocation group number */
2180 int flags) /* XFS_ALLOC_FLAGS_... */
2185 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2188 xfs_trans_brelse(tp, bp);
2193 * Put the block on the freelist for the allocation group.
2196 xfs_alloc_put_freelist(
2197 xfs_trans_t *tp, /* transaction pointer */
2198 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2199 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2200 xfs_agblock_t bno, /* block being freed */
2201 int btreeblk) /* block came from a AGF btree */
2203 xfs_agf_t *agf; /* a.g. freespace structure */
2204 xfs_agfl_t *agfl; /* a.g. free block array */
2205 __be32 *blockp;/* pointer to array entry */
2208 xfs_mount_t *mp; /* mount structure */
2209 xfs_perag_t *pag; /* per allocation group data */
2211 agf = XFS_BUF_TO_AGF(agbp);
2214 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2215 be32_to_cpu(agf->agf_seqno), &agflbp)))
2217 agfl = XFS_BUF_TO_AGFL(agflbp);
2218 be32_add_cpu(&agf->agf_fllast, 1);
2219 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2220 agf->agf_fllast = 0;
2221 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2222 be32_add_cpu(&agf->agf_flcount, 1);
2223 xfs_trans_agflist_delta(tp, 1);
2224 pag->pagf_flcount++;
2226 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2228 be32_add_cpu(&agf->agf_btreeblks, -1);
2229 pag->pagf_btreeblks--;
2230 logflags |= XFS_AGF_BTREEBLKS;
2233 TRACE_MODAGF(NULL, agf, logflags);
2234 xfs_alloc_log_agf(tp, agbp, logflags);
2236 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2237 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2238 *blockp = cpu_to_be32(bno);
2239 TRACE_MODAGF(NULL, agf, logflags);
2240 xfs_alloc_log_agf(tp, agbp, logflags);
2241 xfs_trans_log_buf(tp, agflbp,
2242 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2243 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2244 sizeof(xfs_agblock_t) - 1));
2249 * Read in the allocation group header (free/alloc section).
2253 struct xfs_mount *mp, /* mount point structure */
2254 struct xfs_trans *tp, /* transaction pointer */
2255 xfs_agnumber_t agno, /* allocation group number */
2256 int flags, /* XFS_BUF_ */
2257 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2259 struct xfs_agf *agf; /* ag freelist header */
2260 int agf_ok; /* set if agf is consistent */
2263 ASSERT(agno != NULLAGNUMBER);
2264 error = xfs_trans_read_buf(
2265 mp, tp, mp->m_ddev_targp,
2266 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2267 XFS_FSS_TO_BB(mp, 1), flags, bpp);
2273 ASSERT(!XFS_BUF_GETERROR(*bpp));
2274 agf = XFS_BUF_TO_AGF(*bpp);
2277 * Validate the magic number of the agf block.
2280 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2281 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2282 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2283 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2284 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2285 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
2286 be32_to_cpu(agf->agf_seqno) == agno;
2287 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2288 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2289 be32_to_cpu(agf->agf_length);
2290 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2291 XFS_RANDOM_ALLOC_READ_AGF))) {
2292 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2293 XFS_ERRLEVEL_LOW, mp, agf);
2294 xfs_trans_brelse(tp, *bpp);
2295 return XFS_ERROR(EFSCORRUPTED);
2298 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
2303 * Read in the allocation group header (free/alloc section).
2307 struct xfs_mount *mp, /* mount point structure */
2308 struct xfs_trans *tp, /* transaction pointer */
2309 xfs_agnumber_t agno, /* allocation group number */
2310 int flags, /* XFS_ALLOC_FLAG_... */
2311 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2313 struct xfs_agf *agf; /* ag freelist header */
2314 struct xfs_perag *pag; /* per allocation group data */
2317 ASSERT(agno != NULLAGNUMBER);
2319 error = xfs_read_agf(mp, tp, agno,
2320 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0,
2326 ASSERT(!XFS_BUF_GETERROR(*bpp));
2328 agf = XFS_BUF_TO_AGF(*bpp);
2329 pag = &mp->m_perag[agno];
2330 if (!pag->pagf_init) {
2331 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2332 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2333 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2334 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2335 pag->pagf_levels[XFS_BTNUM_BNOi] =
2336 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2337 pag->pagf_levels[XFS_BTNUM_CNTi] =
2338 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2339 spin_lock_init(&pag->pagb_lock);
2340 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2341 sizeof(xfs_perag_busy_t), KM_SLEEP);
2345 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2346 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2347 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2348 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2349 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2350 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2351 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2352 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2353 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2360 * Allocate an extent (variable-size).
2361 * Depending on the allocation type, we either look in a single allocation
2362 * group or loop over the allocation groups to find the result.
2366 xfs_alloc_arg_t *args) /* allocation argument structure */
2368 xfs_agblock_t agsize; /* allocation group size */
2370 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2371 xfs_extlen_t minleft;/* minimum left value, temp copy */
2372 xfs_mount_t *mp; /* mount structure pointer */
2373 xfs_agnumber_t sagno; /* starting allocation group number */
2374 xfs_alloctype_t type; /* input allocation type */
2377 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2380 type = args->otype = args->type;
2381 args->agbno = NULLAGBLOCK;
2383 * Just fix this up, for the case where the last a.g. is shorter
2384 * (or there's only one a.g.) and the caller couldn't easily figure
2385 * that out (xfs_bmap_alloc).
2387 agsize = mp->m_sb.sb_agblocks;
2388 if (args->maxlen > agsize)
2389 args->maxlen = agsize;
2390 if (args->alignment == 0)
2391 args->alignment = 1;
2392 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2393 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2394 ASSERT(args->minlen <= args->maxlen);
2395 ASSERT(args->minlen <= agsize);
2396 ASSERT(args->mod < args->prod);
2397 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2398 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2399 args->minlen > args->maxlen || args->minlen > agsize ||
2400 args->mod >= args->prod) {
2401 args->fsbno = NULLFSBLOCK;
2402 TRACE_ALLOC("badargs", args);
2405 minleft = args->minleft;
2408 case XFS_ALLOCTYPE_THIS_AG:
2409 case XFS_ALLOCTYPE_NEAR_BNO:
2410 case XFS_ALLOCTYPE_THIS_BNO:
2412 * These three force us into a single a.g.
2414 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2415 down_read(&mp->m_peraglock);
2416 args->pag = &mp->m_perag[args->agno];
2418 error = xfs_alloc_fix_freelist(args, 0);
2419 args->minleft = minleft;
2421 TRACE_ALLOC("nofix", args);
2425 up_read(&mp->m_peraglock);
2426 TRACE_ALLOC("noagbp", args);
2429 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2430 if ((error = xfs_alloc_ag_vextent(args)))
2432 up_read(&mp->m_peraglock);
2434 case XFS_ALLOCTYPE_START_BNO:
2436 * Try near allocation first, then anywhere-in-ag after
2437 * the first a.g. fails.
2439 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2440 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2441 args->fsbno = XFS_AGB_TO_FSB(mp,
2442 ((mp->m_agfrotor / rotorstep) %
2443 mp->m_sb.sb_agcount), 0);
2446 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2447 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2449 case XFS_ALLOCTYPE_ANY_AG:
2450 case XFS_ALLOCTYPE_START_AG:
2451 case XFS_ALLOCTYPE_FIRST_AG:
2453 * Rotate through the allocation groups looking for a winner.
2455 if (type == XFS_ALLOCTYPE_ANY_AG) {
2457 * Start with the last place we left off.
2459 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2460 mp->m_sb.sb_agcount;
2461 args->type = XFS_ALLOCTYPE_THIS_AG;
2462 flags = XFS_ALLOC_FLAG_TRYLOCK;
2463 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2465 * Start with allocation group given by bno.
2467 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2468 args->type = XFS_ALLOCTYPE_THIS_AG;
2472 if (type == XFS_ALLOCTYPE_START_AG)
2473 args->type = XFS_ALLOCTYPE_THIS_AG;
2475 * Start with the given allocation group.
2477 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2478 flags = XFS_ALLOC_FLAG_TRYLOCK;
2481 * Loop over allocation groups twice; first time with
2482 * trylock set, second time without.
2484 down_read(&mp->m_peraglock);
2486 args->pag = &mp->m_perag[args->agno];
2487 if (no_min) args->minleft = 0;
2488 error = xfs_alloc_fix_freelist(args, flags);
2489 args->minleft = minleft;
2491 TRACE_ALLOC("nofix", args);
2495 * If we get a buffer back then the allocation will fly.
2498 if ((error = xfs_alloc_ag_vextent(args)))
2502 TRACE_ALLOC("loopfailed", args);
2504 * Didn't work, figure out the next iteration.
2506 if (args->agno == sagno &&
2507 type == XFS_ALLOCTYPE_START_BNO)
2508 args->type = XFS_ALLOCTYPE_THIS_AG;
2510 * For the first allocation, we can try any AG to get
2511 * space. However, if we already have allocated a
2512 * block, we don't want to try AGs whose number is below
2513 * sagno. Otherwise, we may end up with out-of-order
2514 * locking of AGF, which might cause deadlock.
2516 if (++(args->agno) == mp->m_sb.sb_agcount) {
2517 if (args->firstblock != NULLFSBLOCK)
2523 * Reached the starting a.g., must either be done
2524 * or switch to non-trylock mode.
2526 if (args->agno == sagno) {
2528 args->agbno = NULLAGBLOCK;
2529 TRACE_ALLOC("allfailed", args);
2536 if (type == XFS_ALLOCTYPE_START_BNO) {
2537 args->agbno = XFS_FSB_TO_AGBNO(mp,
2539 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2544 up_read(&mp->m_peraglock);
2545 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2546 if (args->agno == sagno)
2547 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2548 (mp->m_sb.sb_agcount * rotorstep);
2550 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2551 (mp->m_sb.sb_agcount * rotorstep);
2558 if (args->agbno == NULLAGBLOCK)
2559 args->fsbno = NULLFSBLOCK;
2561 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2563 ASSERT(args->len >= args->minlen);
2564 ASSERT(args->len <= args->maxlen);
2565 ASSERT(args->agbno % args->alignment == 0);
2566 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2572 up_read(&mp->m_peraglock);
2578 * Just break up the extent address and hand off to xfs_free_ag_extent
2579 * after fixing up the freelist.
2583 xfs_trans_t *tp, /* transaction pointer */
2584 xfs_fsblock_t bno, /* starting block number of extent */
2585 xfs_extlen_t len) /* length of extent */
2587 xfs_alloc_arg_t args;
2591 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2593 args.mp = tp->t_mountp;
2594 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2595 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2596 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2597 down_read(&args.mp->m_peraglock);
2598 args.pag = &args.mp->m_perag[args.agno];
2599 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2602 ASSERT(args.agbp != NULL);
2603 ASSERT((args.agbno + len) <=
2604 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2606 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2608 up_read(&args.mp->m_peraglock);
2614 * AG Busy list management
2615 * The busy list contains block ranges that have been freed but whose
2616 * transactions have not yet hit disk. If any block listed in a busy
2617 * list is reused, the transaction that freed it must be forced to disk
2618 * before continuing to use the block.
2620 * xfs_alloc_mark_busy - add to the per-ag busy list
2621 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2624 xfs_alloc_mark_busy(xfs_trans_t *tp,
2625 xfs_agnumber_t agno,
2630 xfs_perag_busy_t *bsy;
2634 spin_lock(&mp->m_perag[agno].pagb_lock);
2636 /* search pagb_list for an open slot */
2637 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2638 n < XFS_PAGB_NUM_SLOTS;
2640 if (bsy->busy_tp == NULL) {
2645 if (n < XFS_PAGB_NUM_SLOTS) {
2646 bsy = &mp->m_perag[agno].pagb_list[n];
2647 mp->m_perag[agno].pagb_count++;
2648 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2649 bsy->busy_start = bno;
2650 bsy->busy_length = len;
2652 xfs_trans_add_busy(tp, agno, n);
2654 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2656 * The busy list is full! Since it is now not possible to
2657 * track the free block, make this a synchronous transaction
2658 * to insure that the block is not reused before this
2659 * transaction commits.
2661 xfs_trans_set_sync(tp);
2664 spin_unlock(&mp->m_perag[agno].pagb_lock);
2668 xfs_alloc_clear_busy(xfs_trans_t *tp,
2669 xfs_agnumber_t agno,
2673 xfs_perag_busy_t *list;
2677 spin_lock(&mp->m_perag[agno].pagb_lock);
2678 list = mp->m_perag[agno].pagb_list;
2680 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2681 if (list[idx].busy_tp == tp) {
2682 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2683 list[idx].busy_tp = NULL;
2684 mp->m_perag[agno].pagb_count--;
2686 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2689 spin_unlock(&mp->m_perag[agno].pagb_lock);
2694 * If we find the extent in the busy list, force the log out to get the
2695 * extent out of the busy list so the caller can use it straight away.
2698 xfs_alloc_search_busy(xfs_trans_t *tp,
2699 xfs_agnumber_t agno,
2704 xfs_perag_busy_t *bsy;
2705 xfs_agblock_t uend, bend;
2711 spin_lock(&mp->m_perag[agno].pagb_lock);
2712 cnt = mp->m_perag[agno].pagb_count;
2714 uend = bno + len - 1;
2716 /* search pagb_list for this slot, skipping open slots */
2717 for (bsy = mp->m_perag[agno].pagb_list; cnt; bsy++) {
2720 * (start1,length1) within (start2, length2)
2722 if (bsy->busy_tp != NULL) {
2723 bend = bsy->busy_start + bsy->busy_length - 1;
2724 if ((bno > bend) || (uend < bsy->busy_start)) {
2727 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
2728 "found1", agno, bno, len, tp);
2735 * If a block was found, force the log through the LSN of the
2736 * transaction that freed the block
2739 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, tp);
2740 lsn = bsy->busy_tp->t_commit_lsn;
2741 spin_unlock(&mp->m_perag[agno].pagb_lock);
2742 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2744 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, tp);
2745 spin_unlock(&mp->m_perag[agno].pagb_lock);