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 xfs_alloc_block_t *bnoblock;
386 xfs_alloc_block_t *cntblock;
388 if (bno_cur->bc_nlevels == 1 &&
389 cnt_cur->bc_nlevels == 1) {
390 bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
391 cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
392 XFS_WANT_CORRUPTED_RETURN(
393 be16_to_cpu(bnoblock->bb_numrecs) ==
394 be16_to_cpu(cntblock->bb_numrecs));
399 * Deal with all four cases: the allocated record is contained
400 * within the freespace record, so we can have new freespace
401 * at either (or both) end, or no freespace remaining.
403 if (rbno == fbno && rlen == flen)
404 nfbno1 = nfbno2 = NULLAGBLOCK;
405 else if (rbno == fbno) {
406 nfbno1 = rbno + rlen;
407 nflen1 = flen - rlen;
408 nfbno2 = NULLAGBLOCK;
409 } else if (rbno + rlen == fbno + flen) {
411 nflen1 = flen - rlen;
412 nfbno2 = NULLAGBLOCK;
415 nflen1 = rbno - fbno;
416 nfbno2 = rbno + rlen;
417 nflen2 = (fbno + flen) - nfbno2;
420 * Delete the entry from the by-size btree.
422 if ((error = xfs_btree_delete(cnt_cur, &i)))
424 XFS_WANT_CORRUPTED_RETURN(i == 1);
426 * Add new by-size btree entry(s).
428 if (nfbno1 != NULLAGBLOCK) {
429 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
431 XFS_WANT_CORRUPTED_RETURN(i == 0);
432 if ((error = xfs_btree_insert(cnt_cur, &i)))
434 XFS_WANT_CORRUPTED_RETURN(i == 1);
436 if (nfbno2 != NULLAGBLOCK) {
437 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
439 XFS_WANT_CORRUPTED_RETURN(i == 0);
440 if ((error = xfs_btree_insert(cnt_cur, &i)))
442 XFS_WANT_CORRUPTED_RETURN(i == 1);
445 * Fix up the by-block btree entry(s).
447 if (nfbno1 == NULLAGBLOCK) {
449 * No remaining freespace, just delete the by-block tree entry.
451 if ((error = xfs_btree_delete(bno_cur, &i)))
453 XFS_WANT_CORRUPTED_RETURN(i == 1);
456 * Update the by-block entry to start later|be shorter.
458 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
461 if (nfbno2 != NULLAGBLOCK) {
463 * 2 resulting free entries, need to add one.
465 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
467 XFS_WANT_CORRUPTED_RETURN(i == 0);
468 if ((error = xfs_btree_insert(bno_cur, &i)))
470 XFS_WANT_CORRUPTED_RETURN(i == 1);
476 * Read in the allocation group free block array.
478 STATIC int /* error */
480 xfs_mount_t *mp, /* mount point structure */
481 xfs_trans_t *tp, /* transaction pointer */
482 xfs_agnumber_t agno, /* allocation group number */
483 xfs_buf_t **bpp) /* buffer for the ag free block array */
485 xfs_buf_t *bp; /* return value */
488 ASSERT(agno != NULLAGNUMBER);
489 error = xfs_trans_read_buf(
490 mp, tp, mp->m_ddev_targp,
491 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
492 XFS_FSS_TO_BB(mp, 1), 0, &bp);
496 ASSERT(!XFS_BUF_GETERROR(bp));
497 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
502 #if defined(XFS_ALLOC_TRACE)
504 * Add an allocation trace entry for an alloc call.
507 xfs_alloc_trace_alloc(
508 const char *name, /* function tag string */
509 char *str, /* additional string */
510 xfs_alloc_arg_t *args, /* allocation argument structure */
511 int line) /* source line number */
513 ktrace_enter(xfs_alloc_trace_buf,
514 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
518 (void *)(__psunsigned_t)args->agno,
519 (void *)(__psunsigned_t)args->agbno,
520 (void *)(__psunsigned_t)args->minlen,
521 (void *)(__psunsigned_t)args->maxlen,
522 (void *)(__psunsigned_t)args->mod,
523 (void *)(__psunsigned_t)args->prod,
524 (void *)(__psunsigned_t)args->minleft,
525 (void *)(__psunsigned_t)args->total,
526 (void *)(__psunsigned_t)args->alignment,
527 (void *)(__psunsigned_t)args->len,
528 (void *)((((__psint_t)args->type) << 16) |
529 (__psint_t)args->otype),
530 (void *)(__psint_t)((args->wasdel << 3) |
531 (args->wasfromfl << 2) |
533 (args->userdata << 0)));
537 * Add an allocation trace entry for a free call.
540 xfs_alloc_trace_free(
541 const char *name, /* function tag string */
542 char *str, /* additional string */
543 xfs_mount_t *mp, /* file system mount point */
544 xfs_agnumber_t agno, /* allocation group number */
545 xfs_agblock_t agbno, /* a.g. relative block number */
546 xfs_extlen_t len, /* length of extent */
547 int isfl, /* set if is freelist allocation/free */
548 int line) /* source line number */
550 ktrace_enter(xfs_alloc_trace_buf,
551 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
555 (void *)(__psunsigned_t)agno,
556 (void *)(__psunsigned_t)agbno,
557 (void *)(__psunsigned_t)len,
558 (void *)(__psint_t)isfl,
559 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
563 * Add an allocation trace entry for modifying an agf.
566 xfs_alloc_trace_modagf(
567 const char *name, /* function tag string */
568 char *str, /* additional string */
569 xfs_mount_t *mp, /* file system mount point */
570 xfs_agf_t *agf, /* new agf value */
571 int flags, /* logging flags for agf */
572 int line) /* source line number */
574 ktrace_enter(xfs_alloc_trace_buf,
575 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
579 (void *)(__psint_t)flags,
580 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
581 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
582 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
583 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
584 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
585 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
586 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
587 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
588 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
589 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
590 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
594 xfs_alloc_trace_busy(
595 const char *name, /* function tag string */
596 char *str, /* additional string */
597 xfs_mount_t *mp, /* file system mount point */
598 xfs_agnumber_t agno, /* allocation group number */
599 xfs_agblock_t agbno, /* a.g. relative block number */
600 xfs_extlen_t len, /* length of extent */
601 int slot, /* perag Busy slot */
603 int trtype, /* type: add, delete, search */
604 int line) /* source line number */
606 ktrace_enter(xfs_alloc_trace_buf,
607 (void *)(__psint_t)(trtype | (line << 16)),
611 (void *)(__psunsigned_t)agno,
612 (void *)(__psunsigned_t)agbno,
613 (void *)(__psunsigned_t)len,
614 (void *)(__psint_t)slot,
616 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
618 #endif /* XFS_ALLOC_TRACE */
621 * Allocation group level functions.
625 * Allocate a variable extent in the allocation group agno.
626 * Type and bno are used to determine where in the allocation group the
628 * Extent's length (returned in *len) will be between minlen and maxlen,
629 * and of the form k * prod + mod unless there's nothing that large.
630 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
632 STATIC int /* error */
633 xfs_alloc_ag_vextent(
634 xfs_alloc_arg_t *args) /* argument structure for allocation */
638 ASSERT(args->minlen > 0);
639 ASSERT(args->maxlen > 0);
640 ASSERT(args->minlen <= args->maxlen);
641 ASSERT(args->mod < args->prod);
642 ASSERT(args->alignment > 0);
644 * Branch to correct routine based on the type.
647 switch (args->type) {
648 case XFS_ALLOCTYPE_THIS_AG:
649 error = xfs_alloc_ag_vextent_size(args);
651 case XFS_ALLOCTYPE_NEAR_BNO:
652 error = xfs_alloc_ag_vextent_near(args);
654 case XFS_ALLOCTYPE_THIS_BNO:
655 error = xfs_alloc_ag_vextent_exact(args);
664 * If the allocation worked, need to change the agf structure
665 * (and log it), and the superblock.
667 if (args->agbno != NULLAGBLOCK) {
668 xfs_agf_t *agf; /* allocation group freelist header */
669 #ifdef XFS_ALLOC_TRACE
670 xfs_mount_t *mp = args->mp;
672 long slen = (long)args->len;
674 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
675 ASSERT(!(args->wasfromfl) || !args->isfl);
676 ASSERT(args->agbno % args->alignment == 0);
677 if (!(args->wasfromfl)) {
679 agf = XFS_BUF_TO_AGF(args->agbp);
680 be32_add_cpu(&agf->agf_freeblks, -(args->len));
681 xfs_trans_agblocks_delta(args->tp,
682 -((long)(args->len)));
683 args->pag->pagf_freeblks -= args->len;
684 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
685 be32_to_cpu(agf->agf_length));
686 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
687 xfs_alloc_log_agf(args->tp, args->agbp,
689 /* search the busylist for these blocks */
690 xfs_alloc_search_busy(args->tp, args->agno,
691 args->agbno, args->len);
694 xfs_trans_mod_sb(args->tp,
695 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
696 XFS_TRANS_SB_FDBLOCKS, -slen);
697 XFS_STATS_INC(xs_allocx);
698 XFS_STATS_ADD(xs_allocb, args->len);
704 * Allocate a variable extent at exactly agno/bno.
705 * Extent's length (returned in *len) will be between minlen and maxlen,
706 * and of the form k * prod + mod unless there's nothing that large.
707 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
709 STATIC int /* error */
710 xfs_alloc_ag_vextent_exact(
711 xfs_alloc_arg_t *args) /* allocation argument structure */
713 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
714 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
715 xfs_agblock_t end; /* end of allocated extent */
717 xfs_agblock_t fbno; /* start block of found extent */
718 xfs_agblock_t fend; /* end block of found extent */
719 xfs_extlen_t flen; /* length of found extent */
720 int i; /* success/failure of operation */
721 xfs_agblock_t maxend; /* end of maximal extent */
722 xfs_agblock_t minend; /* end of minimal extent */
723 xfs_extlen_t rlen; /* length of returned extent */
725 ASSERT(args->alignment == 1);
727 * Allocate/initialize a cursor for the by-number freespace btree.
729 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
730 args->agno, XFS_BTNUM_BNO);
732 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
733 * Look for the closest free block <= bno, it must contain bno
734 * if any free block does.
736 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
740 * Didn't find it, return null.
742 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
743 args->agbno = NULLAGBLOCK;
747 * Grab the freespace record.
749 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
751 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
752 ASSERT(fbno <= args->agbno);
753 minend = args->agbno + args->minlen;
754 maxend = args->agbno + args->maxlen;
757 * Give up if the freespace isn't long enough for the minimum request.
760 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
761 args->agbno = NULLAGBLOCK;
765 * End of extent will be smaller of the freespace end and the
766 * maximal requested end.
768 end = XFS_AGBLOCK_MIN(fend, maxend);
770 * Fix the length according to mod and prod if given.
772 args->len = end - args->agbno;
773 xfs_alloc_fix_len(args);
774 if (!xfs_alloc_fix_minleft(args)) {
775 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
779 ASSERT(args->agbno + rlen <= fend);
780 end = args->agbno + rlen;
782 * We are allocating agbno for rlen [agbno .. end]
783 * Allocate/initialize a cursor for the by-size btree.
785 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
786 args->agno, XFS_BTNUM_CNT);
787 ASSERT(args->agbno + args->len <=
788 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
789 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
790 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
791 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
794 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
795 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
796 TRACE_ALLOC("normal", args);
801 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
802 TRACE_ALLOC("error", args);
807 * Allocate a variable extent near bno in the allocation group agno.
808 * Extent's length (returned in len) will be between minlen and maxlen,
809 * and of the form k * prod + mod unless there's nothing that large.
810 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
812 STATIC int /* error */
813 xfs_alloc_ag_vextent_near(
814 xfs_alloc_arg_t *args) /* allocation argument structure */
816 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
817 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
818 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
819 xfs_agblock_t gtbno; /* start bno of right side entry */
820 xfs_agblock_t gtbnoa; /* aligned ... */
821 xfs_extlen_t gtdiff; /* difference to right side entry */
822 xfs_extlen_t gtlen; /* length of right side entry */
823 xfs_extlen_t gtlena; /* aligned ... */
824 xfs_agblock_t gtnew; /* useful start bno of right side */
825 int error; /* error code */
826 int i; /* result code, temporary */
827 int j; /* result code, temporary */
828 xfs_agblock_t ltbno; /* start bno of left side entry */
829 xfs_agblock_t ltbnoa; /* aligned ... */
830 xfs_extlen_t ltdiff; /* difference to left side entry */
832 xfs_agblock_t ltend; /* end bno of left side entry */
833 xfs_extlen_t ltlen; /* length of left side entry */
834 xfs_extlen_t ltlena; /* aligned ... */
835 xfs_agblock_t ltnew; /* useful start bno of left side */
836 xfs_extlen_t rlen; /* length of returned extent */
837 #if defined(DEBUG) && defined(__KERNEL__)
839 * Randomly don't execute the first algorithm.
841 int dofirst; /* set to do first algorithm */
843 dofirst = random32() & 1;
846 * Get a cursor for the by-size btree.
848 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
849 args->agno, XFS_BTNUM_CNT);
851 bno_cur_lt = bno_cur_gt = NULL;
853 * See if there are any free extents as big as maxlen.
855 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
858 * If none, then pick up the last entry in the tree unless the
862 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
865 if (i == 0 || ltlen == 0) {
866 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
874 * If the requested extent is large wrt the freespaces available
875 * in this a.g., then the cursor will be pointing to a btree entry
876 * near the right edge of the tree. If it's in the last btree leaf
877 * block, then we just examine all the entries in that block
878 * that are big enough, and pick the best one.
879 * This is written as a while loop so we can break out of it,
880 * but we never loop back to the top.
882 while (xfs_btree_islastblock(cnt_cur, 0)) {
886 xfs_agblock_t bnew=0;
888 #if defined(DEBUG) && defined(__KERNEL__)
893 * Start from the entry that lookup found, sequence through
894 * all larger free blocks. If we're actually pointing at a
895 * record smaller than maxlen, go to the start of this block,
896 * and skip all those smaller than minlen.
898 if (ltlen || args->alignment > 1) {
899 cnt_cur->bc_ptrs[0] = 1;
901 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
904 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
905 if (ltlen >= args->minlen)
907 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
910 ASSERT(ltlen >= args->minlen);
914 i = cnt_cur->bc_ptrs[0];
915 for (j = 1, blen = 0, bdiff = 0;
916 !error && j && (blen < args->maxlen || bdiff > 0);
917 error = xfs_btree_increment(cnt_cur, 0, &j)) {
919 * For each entry, decide if it's better than
920 * the previous best entry.
922 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
924 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
925 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
926 args->minlen, <bnoa, <lena);
927 if (ltlena < args->minlen)
929 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
930 xfs_alloc_fix_len(args);
931 ASSERT(args->len >= args->minlen);
932 if (args->len < blen)
934 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
935 args->alignment, ltbno, ltlen, <new);
936 if (ltnew != NULLAGBLOCK &&
937 (args->len > blen || ltdiff < bdiff)) {
941 besti = cnt_cur->bc_ptrs[0];
945 * It didn't work. We COULD be in a case where
946 * there's a good record somewhere, so try again.
951 * Point at the best entry, and retrieve it again.
953 cnt_cur->bc_ptrs[0] = besti;
954 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
956 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
957 ltend = ltbno + ltlen;
958 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
960 if (!xfs_alloc_fix_minleft(args)) {
961 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
962 TRACE_ALLOC("nominleft", args);
967 * We are allocating starting at bnew for blen blocks.
970 ASSERT(bnew >= ltbno);
971 ASSERT(bnew + blen <= ltend);
973 * Set up a cursor for the by-bno tree.
975 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
976 args->agbp, args->agno, XFS_BTNUM_BNO);
978 * Fix up the btree entries.
980 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
981 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
983 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
984 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
985 TRACE_ALLOC("first", args);
990 * Search in the by-bno tree to the left and to the right
991 * simultaneously, until in each case we find a space big enough,
992 * or run into the edge of the tree. When we run into the edge,
993 * we deallocate that cursor.
994 * If both searches succeed, we compare the two spaces and pick
996 * With alignment, it's possible for both to fail; the upper
997 * level algorithm that picks allocation groups for allocations
998 * is not supposed to do this.
1001 * Allocate and initialize the cursor for the leftward search.
1003 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1004 args->agno, XFS_BTNUM_BNO);
1006 * Lookup <= bno to find the leftward search's starting point.
1008 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1012 * Didn't find anything; use this cursor for the rightward
1015 bno_cur_gt = bno_cur_lt;
1019 * Found something. Duplicate the cursor for the rightward search.
1021 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1024 * Increment the cursor, so we will point at the entry just right
1025 * of the leftward entry if any, or to the leftmost entry.
1027 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1031 * It failed, there are no rightward entries.
1033 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1037 * Loop going left with the leftward cursor, right with the
1038 * rightward cursor, until either both directions give up or
1039 * we find an entry at least as big as minlen.
1043 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1045 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1046 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
1047 args->minlen, <bnoa, <lena);
1048 if (ltlena >= args->minlen)
1050 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1053 xfs_btree_del_cursor(bno_cur_lt,
1059 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1061 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1062 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
1063 args->minlen, >bnoa, >lena);
1064 if (gtlena >= args->minlen)
1066 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1069 xfs_btree_del_cursor(bno_cur_gt,
1074 } while (bno_cur_lt || bno_cur_gt);
1076 * Got both cursors still active, need to find better entry.
1078 if (bno_cur_lt && bno_cur_gt) {
1080 * Left side is long enough, look for a right side entry.
1082 if (ltlena >= args->minlen) {
1084 * Fix up the length.
1086 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1087 xfs_alloc_fix_len(args);
1089 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1090 args->alignment, ltbno, ltlen, <new);
1096 * Look until we find a better one, run out of
1097 * space, or run off the end.
1099 while (bno_cur_lt && bno_cur_gt) {
1100 if ((error = xfs_alloc_get_rec(
1104 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1105 xfs_alloc_compute_aligned(gtbno, gtlen,
1106 args->alignment, args->minlen,
1109 * The left one is clearly better.
1111 if (gtbnoa >= args->agbno + ltdiff) {
1112 xfs_btree_del_cursor(
1119 * If we reach a big enough entry,
1120 * compare the two and pick the best.
1122 if (gtlena >= args->minlen) {
1124 XFS_EXTLEN_MIN(gtlena,
1126 xfs_alloc_fix_len(args);
1128 gtdiff = xfs_alloc_compute_diff(
1131 gtbno, gtlen, >new);
1133 * Right side is better.
1135 if (gtdiff < ltdiff) {
1136 xfs_btree_del_cursor(
1142 * Left side is better.
1145 xfs_btree_del_cursor(
1153 * Fell off the right end.
1155 if ((error = xfs_btree_increment(
1156 bno_cur_gt, 0, &i)))
1159 xfs_btree_del_cursor(
1168 * The left side is perfect, trash the right side.
1171 xfs_btree_del_cursor(bno_cur_gt,
1177 * It's the right side that was found first, look left.
1181 * Fix up the length.
1183 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1184 xfs_alloc_fix_len(args);
1186 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1187 args->alignment, gtbno, gtlen, >new);
1189 * Right side entry isn't perfect.
1193 * Look until we find a better one, run out of
1194 * space, or run off the end.
1196 while (bno_cur_lt && bno_cur_gt) {
1197 if ((error = xfs_alloc_get_rec(
1201 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1202 xfs_alloc_compute_aligned(ltbno, ltlen,
1203 args->alignment, args->minlen,
1206 * The right one is clearly better.
1208 if (ltbnoa <= args->agbno - gtdiff) {
1209 xfs_btree_del_cursor(
1216 * If we reach a big enough entry,
1217 * compare the two and pick the best.
1219 if (ltlena >= args->minlen) {
1220 args->len = XFS_EXTLEN_MIN(
1221 ltlena, args->maxlen);
1222 xfs_alloc_fix_len(args);
1224 ltdiff = xfs_alloc_compute_diff(
1227 ltbno, ltlen, <new);
1229 * Left side is better.
1231 if (ltdiff < gtdiff) {
1232 xfs_btree_del_cursor(
1238 * Right side is better.
1241 xfs_btree_del_cursor(
1249 * Fell off the left end.
1251 if ((error = xfs_btree_decrement(
1252 bno_cur_lt, 0, &i)))
1255 xfs_btree_del_cursor(bno_cur_lt,
1263 * The right side is perfect, trash the left side.
1266 xfs_btree_del_cursor(bno_cur_lt,
1273 * If we couldn't get anything, give up.
1275 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1276 TRACE_ALLOC("neither", args);
1277 args->agbno = NULLAGBLOCK;
1281 * At this point we have selected a freespace entry, either to the
1282 * left or to the right. If it's on the right, copy all the
1283 * useful variables to the "left" set so we only have one
1284 * copy of this code.
1287 bno_cur_lt = bno_cur_gt;
1297 * Fix up the length and compute the useful address.
1299 ltend = ltbno + ltlen;
1300 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1301 xfs_alloc_fix_len(args);
1302 if (!xfs_alloc_fix_minleft(args)) {
1303 TRACE_ALLOC("nominleft", args);
1304 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1305 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1309 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1311 ASSERT(ltnew >= ltbno);
1312 ASSERT(ltnew + rlen <= ltend);
1313 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1314 args->agbno = ltnew;
1315 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1316 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1318 TRACE_ALLOC(j ? "gt" : "lt", args);
1319 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1320 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1324 TRACE_ALLOC("error", args);
1325 if (cnt_cur != NULL)
1326 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1327 if (bno_cur_lt != NULL)
1328 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1329 if (bno_cur_gt != NULL)
1330 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1335 * Allocate a variable extent anywhere in the allocation group agno.
1336 * Extent's length (returned in len) will be between minlen and maxlen,
1337 * and of the form k * prod + mod unless there's nothing that large.
1338 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1340 STATIC int /* error */
1341 xfs_alloc_ag_vextent_size(
1342 xfs_alloc_arg_t *args) /* allocation argument structure */
1344 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1345 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1346 int error; /* error result */
1347 xfs_agblock_t fbno; /* start of found freespace */
1348 xfs_extlen_t flen; /* length of found freespace */
1349 int i; /* temp status variable */
1350 xfs_agblock_t rbno; /* returned block number */
1351 xfs_extlen_t rlen; /* length of returned extent */
1354 * Allocate and initialize a cursor for the by-size btree.
1356 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1357 args->agno, XFS_BTNUM_CNT);
1360 * Look for an entry >= maxlen+alignment-1 blocks.
1362 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1363 args->maxlen + args->alignment - 1, &i)))
1366 * If none, then pick up the last entry in the tree unless the
1370 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1373 if (i == 0 || flen == 0) {
1374 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1375 TRACE_ALLOC("noentry", args);
1381 * There's a freespace as big as maxlen+alignment-1, get it.
1384 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1386 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1389 * In the first case above, we got the last entry in the
1390 * by-size btree. Now we check to see if the space hits maxlen
1391 * once aligned; if not, we search left for something better.
1392 * This can't happen in the second case above.
1394 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1396 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1397 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1398 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1399 if (rlen < args->maxlen) {
1400 xfs_agblock_t bestfbno;
1401 xfs_extlen_t bestflen;
1402 xfs_agblock_t bestrbno;
1403 xfs_extlen_t bestrlen;
1410 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1414 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1417 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1418 if (flen < bestrlen)
1420 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1421 args->minlen, &rbno, &rlen);
1422 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1423 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1424 (rlen <= flen && rbno + rlen <= fbno + flen),
1426 if (rlen > bestrlen) {
1431 if (rlen == args->maxlen)
1435 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1438 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1444 args->wasfromfl = 0;
1446 * Fix up the length.
1449 xfs_alloc_fix_len(args);
1450 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1451 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1452 TRACE_ALLOC("nominleft", args);
1453 args->agbno = NULLAGBLOCK;
1457 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1459 * Allocate and initialize a cursor for the by-block tree.
1461 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1462 args->agno, XFS_BTNUM_BNO);
1463 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1464 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1466 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1467 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1468 cnt_cur = bno_cur = NULL;
1471 XFS_WANT_CORRUPTED_GOTO(
1472 args->agbno + args->len <=
1473 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1475 TRACE_ALLOC("normal", args);
1479 TRACE_ALLOC("error", args);
1481 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1483 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1488 * Deal with the case where only small freespaces remain.
1489 * Either return the contents of the last freespace record,
1490 * or allocate space from the freelist if there is nothing in the tree.
1492 STATIC int /* error */
1493 xfs_alloc_ag_vextent_small(
1494 xfs_alloc_arg_t *args, /* allocation argument structure */
1495 xfs_btree_cur_t *ccur, /* by-size cursor */
1496 xfs_agblock_t *fbnop, /* result block number */
1497 xfs_extlen_t *flenp, /* result length */
1498 int *stat) /* status: 0-freelist, 1-normal/none */
1505 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1508 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1510 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1513 * Nothing in the btree, try the freelist. Make sure
1514 * to respect minleft even when pulling from the
1517 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1518 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1520 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1523 if (fbno != NULLAGBLOCK) {
1524 if (args->userdata) {
1527 bp = xfs_btree_get_bufs(args->mp, args->tp,
1528 args->agno, fbno, 0);
1529 xfs_trans_binval(args->tp, bp);
1533 XFS_WANT_CORRUPTED_GOTO(
1534 args->agbno + args->len <=
1535 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1537 args->wasfromfl = 1;
1538 TRACE_ALLOC("freelist", args);
1543 * Nothing in the freelist.
1549 * Can't allocate from the freelist for some reason.
1556 * Can't do the allocation, give up.
1558 if (flen < args->minlen) {
1559 args->agbno = NULLAGBLOCK;
1560 TRACE_ALLOC("notenough", args);
1566 TRACE_ALLOC("normal", args);
1570 TRACE_ALLOC("error", args);
1575 * Free the extent starting at agno/bno for length.
1577 STATIC int /* error */
1579 xfs_trans_t *tp, /* transaction pointer */
1580 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1581 xfs_agnumber_t agno, /* allocation group number */
1582 xfs_agblock_t bno, /* starting block number */
1583 xfs_extlen_t len, /* length of extent */
1584 int isfl) /* set if is freelist blocks - no sb acctg */
1586 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1587 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1588 int error; /* error return value */
1589 xfs_agblock_t gtbno; /* start of right neighbor block */
1590 xfs_extlen_t gtlen; /* length of right neighbor block */
1591 int haveleft; /* have a left neighbor block */
1592 int haveright; /* have a right neighbor block */
1593 int i; /* temp, result code */
1594 xfs_agblock_t ltbno; /* start of left neighbor block */
1595 xfs_extlen_t ltlen; /* length of left neighbor block */
1596 xfs_mount_t *mp; /* mount point struct for filesystem */
1597 xfs_agblock_t nbno; /* new starting block of freespace */
1598 xfs_extlen_t nlen; /* new length of freespace */
1602 * Allocate and initialize a cursor for the by-block btree.
1604 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1607 * Look for a neighboring block on the left (lower block numbers)
1608 * that is contiguous with this space.
1610 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1614 * There is a block to our left.
1616 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1618 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1620 * It's not contiguous, though.
1622 if (ltbno + ltlen < bno)
1626 * If this failure happens the request to free this
1627 * space was invalid, it's (partly) already free.
1630 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1634 * Look for a neighboring block on the right (higher block numbers)
1635 * that is contiguous with this space.
1637 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1641 * There is a block to our right.
1643 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1645 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1647 * It's not contiguous, though.
1649 if (bno + len < gtbno)
1653 * If this failure happens the request to free this
1654 * space was invalid, it's (partly) already free.
1657 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1661 * Now allocate and initialize a cursor for the by-size tree.
1663 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1665 * Have both left and right contiguous neighbors.
1666 * Merge all three into a single free block.
1668 if (haveleft && haveright) {
1670 * Delete the old by-size entry on the left.
1672 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1674 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1675 if ((error = xfs_btree_delete(cnt_cur, &i)))
1677 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1679 * Delete the old by-size entry on the right.
1681 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1683 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1684 if ((error = xfs_btree_delete(cnt_cur, &i)))
1686 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1688 * Delete the old by-block entry for the right block.
1690 if ((error = xfs_btree_delete(bno_cur, &i)))
1692 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1694 * Move the by-block cursor back to the left neighbor.
1696 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1698 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1701 * Check that this is the right record: delete didn't
1702 * mangle the cursor.
1705 xfs_agblock_t xxbno;
1708 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1711 XFS_WANT_CORRUPTED_GOTO(
1712 i == 1 && xxbno == ltbno && xxlen == ltlen,
1717 * Update remaining by-block entry to the new, joined block.
1720 nlen = len + ltlen + gtlen;
1721 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1725 * Have only a left contiguous neighbor.
1726 * Merge it together with the new freespace.
1728 else if (haveleft) {
1730 * Delete the old by-size entry on the left.
1732 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1734 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1735 if ((error = xfs_btree_delete(cnt_cur, &i)))
1737 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1739 * Back up the by-block cursor to the left neighbor, and
1740 * update its length.
1742 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1744 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1747 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1751 * Have only a right contiguous neighbor.
1752 * Merge it together with the new freespace.
1754 else if (haveright) {
1756 * Delete the old by-size entry on the right.
1758 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1760 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1761 if ((error = xfs_btree_delete(cnt_cur, &i)))
1763 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1765 * Update the starting block and length of the right
1766 * neighbor in the by-block tree.
1770 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1774 * No contiguous neighbors.
1775 * Insert the new freespace into the by-block tree.
1780 if ((error = xfs_btree_insert(bno_cur, &i)))
1782 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1784 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1787 * In all cases we need to insert the new freespace in the by-size tree.
1789 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1791 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1792 if ((error = xfs_btree_insert(cnt_cur, &i)))
1794 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1795 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1798 * Update the freespace totals in the ag and superblock.
1802 xfs_perag_t *pag; /* per allocation group data */
1804 agf = XFS_BUF_TO_AGF(agbp);
1805 pag = &mp->m_perag[agno];
1806 be32_add_cpu(&agf->agf_freeblks, len);
1807 xfs_trans_agblocks_delta(tp, len);
1808 pag->pagf_freeblks += len;
1809 XFS_WANT_CORRUPTED_GOTO(
1810 be32_to_cpu(agf->agf_freeblks) <=
1811 be32_to_cpu(agf->agf_length),
1813 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1814 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1816 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1817 XFS_STATS_INC(xs_freex);
1818 XFS_STATS_ADD(xs_freeb, len);
1820 TRACE_FREE(haveleft ?
1821 (haveright ? "both" : "left") :
1822 (haveright ? "right" : "none"),
1823 agno, bno, len, isfl);
1826 * Since blocks move to the free list without the coordination
1827 * used in xfs_bmap_finish, we can't allow block to be available
1828 * for reallocation and non-transaction writing (user data)
1829 * until we know that the transaction that moved it to the free
1830 * list is permanently on disk. We track the blocks by declaring
1831 * these blocks as "busy"; the busy list is maintained on a per-ag
1832 * basis and each transaction records which entries should be removed
1833 * when the iclog commits to disk. If a busy block is allocated,
1834 * the iclog is pushed up to the LSN that freed the block.
1836 xfs_alloc_mark_busy(tp, agno, bno, len);
1840 TRACE_FREE("error", agno, bno, len, isfl);
1842 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1844 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1849 * Visible (exported) allocation/free functions.
1850 * Some of these are used just by xfs_alloc_btree.c and this file.
1854 * Compute and fill in value of m_ag_maxlevels.
1857 xfs_alloc_compute_maxlevels(
1858 xfs_mount_t *mp) /* file system mount structure */
1866 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1867 minleafrecs = mp->m_alloc_mnr[0];
1868 minnoderecs = mp->m_alloc_mnr[1];
1869 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1870 for (level = 1; maxblocks > 1; level++)
1871 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1872 mp->m_ag_maxlevels = level;
1876 * Decide whether to use this allocation group for this allocation.
1877 * If so, fix up the btree freelist's size.
1879 STATIC int /* error */
1880 xfs_alloc_fix_freelist(
1881 xfs_alloc_arg_t *args, /* allocation argument structure */
1882 int flags) /* XFS_ALLOC_FLAG_... */
1884 xfs_buf_t *agbp; /* agf buffer pointer */
1885 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1886 xfs_buf_t *agflbp;/* agfl buffer pointer */
1887 xfs_agblock_t bno; /* freelist block */
1888 xfs_extlen_t delta; /* new blocks needed in freelist */
1889 int error; /* error result code */
1890 xfs_extlen_t longest;/* longest extent in allocation group */
1891 xfs_mount_t *mp; /* file system mount point structure */
1892 xfs_extlen_t need; /* total blocks needed in freelist */
1893 xfs_perag_t *pag; /* per-ag information structure */
1894 xfs_alloc_arg_t targs; /* local allocation arguments */
1895 xfs_trans_t *tp; /* transaction pointer */
1901 if (!pag->pagf_init) {
1902 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1905 if (!pag->pagf_init) {
1906 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1907 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1915 * If this is a metadata preferred pag and we are user data
1916 * then try somewhere else if we are not being asked to
1917 * try harder at this point
1919 if (pag->pagf_metadata && args->userdata &&
1920 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1921 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1926 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1927 need = XFS_MIN_FREELIST_PAG(pag, mp);
1928 delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
1930 * If it looks like there isn't a long enough extent, or enough
1931 * total blocks, reject it.
1933 longest = (pag->pagf_longest > delta) ?
1934 (pag->pagf_longest - delta) :
1935 (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
1936 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1938 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1939 need - args->total) < (int)args->minleft)) {
1941 xfs_trans_brelse(tp, agbp);
1948 * Get the a.g. freespace buffer.
1949 * Can fail if we're not blocking on locks, and it's held.
1952 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1956 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1957 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1963 * Figure out how many blocks we should have in the freelist.
1965 agf = XFS_BUF_TO_AGF(agbp);
1966 need = XFS_MIN_FREELIST(agf, mp);
1968 * If there isn't enough total or single-extent, reject it.
1970 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1971 delta = need > be32_to_cpu(agf->agf_flcount) ?
1972 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1973 longest = be32_to_cpu(agf->agf_longest);
1974 longest = (longest > delta) ? (longest - delta) :
1975 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1976 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1978 ((int)(be32_to_cpu(agf->agf_freeblks) +
1979 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1980 (int)args->minleft)) {
1981 xfs_trans_brelse(tp, agbp);
1987 * Make the freelist shorter if it's too long.
1989 while (be32_to_cpu(agf->agf_flcount) > need) {
1992 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1995 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1997 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1998 xfs_trans_binval(tp, bp);
2001 * Initialize the args structure.
2006 targs.agno = args->agno;
2007 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
2008 targs.minalignslop = 0;
2009 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2010 targs.type = XFS_ALLOCTYPE_THIS_AG;
2012 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
2015 * Make the freelist longer if it's too short.
2017 while (be32_to_cpu(agf->agf_flcount) < need) {
2019 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
2021 * Allocate as many blocks as possible at once.
2023 if ((error = xfs_alloc_ag_vextent(&targs))) {
2024 xfs_trans_brelse(tp, agflbp);
2028 * Stop if we run out. Won't happen if callers are obeying
2029 * the restrictions correctly. Can happen for free calls
2030 * on a completely full ag.
2032 if (targs.agbno == NULLAGBLOCK) {
2033 if (flags & XFS_ALLOC_FLAG_FREEING)
2035 xfs_trans_brelse(tp, agflbp);
2040 * Put each allocated block on the list.
2042 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2043 error = xfs_alloc_put_freelist(tp, agbp,
2049 xfs_trans_brelse(tp, agflbp);
2055 * Get a block from the freelist.
2056 * Returns with the buffer for the block gotten.
2059 xfs_alloc_get_freelist(
2060 xfs_trans_t *tp, /* transaction pointer */
2061 xfs_buf_t *agbp, /* buffer containing the agf structure */
2062 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2063 int btreeblk) /* destination is a AGF btree */
2065 xfs_agf_t *agf; /* a.g. freespace structure */
2066 xfs_agfl_t *agfl; /* a.g. freelist structure */
2067 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2068 xfs_agblock_t bno; /* block number returned */
2071 xfs_mount_t *mp; /* mount structure */
2072 xfs_perag_t *pag; /* per allocation group data */
2074 agf = XFS_BUF_TO_AGF(agbp);
2076 * Freelist is empty, give up.
2078 if (!agf->agf_flcount) {
2079 *bnop = NULLAGBLOCK;
2083 * Read the array of free blocks.
2086 if ((error = xfs_alloc_read_agfl(mp, tp,
2087 be32_to_cpu(agf->agf_seqno), &agflbp)))
2089 agfl = XFS_BUF_TO_AGFL(agflbp);
2091 * Get the block number and update the data structures.
2093 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2094 be32_add_cpu(&agf->agf_flfirst, 1);
2095 xfs_trans_brelse(tp, agflbp);
2096 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2097 agf->agf_flfirst = 0;
2098 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2099 be32_add_cpu(&agf->agf_flcount, -1);
2100 xfs_trans_agflist_delta(tp, -1);
2101 pag->pagf_flcount--;
2103 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2105 be32_add_cpu(&agf->agf_btreeblks, 1);
2106 pag->pagf_btreeblks++;
2107 logflags |= XFS_AGF_BTREEBLKS;
2110 TRACE_MODAGF(NULL, agf, logflags);
2111 xfs_alloc_log_agf(tp, agbp, logflags);
2115 * As blocks are freed, they are added to the per-ag busy list
2116 * and remain there until the freeing transaction is committed to
2117 * disk. Now that we have allocated blocks, this list must be
2118 * searched to see if a block is being reused. If one is, then
2119 * the freeing transaction must be pushed to disk NOW by forcing
2120 * to disk all iclogs up that transaction's LSN.
2122 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
2127 * Log the given fields from the agf structure.
2131 xfs_trans_t *tp, /* transaction pointer */
2132 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2133 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2135 int first; /* first byte offset */
2136 int last; /* last byte offset */
2137 static const short offsets[] = {
2138 offsetof(xfs_agf_t, agf_magicnum),
2139 offsetof(xfs_agf_t, agf_versionnum),
2140 offsetof(xfs_agf_t, agf_seqno),
2141 offsetof(xfs_agf_t, agf_length),
2142 offsetof(xfs_agf_t, agf_roots[0]),
2143 offsetof(xfs_agf_t, agf_levels[0]),
2144 offsetof(xfs_agf_t, agf_flfirst),
2145 offsetof(xfs_agf_t, agf_fllast),
2146 offsetof(xfs_agf_t, agf_flcount),
2147 offsetof(xfs_agf_t, agf_freeblks),
2148 offsetof(xfs_agf_t, agf_longest),
2149 offsetof(xfs_agf_t, agf_btreeblks),
2153 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2154 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2158 * Interface for inode allocation to force the pag data to be initialized.
2161 xfs_alloc_pagf_init(
2162 xfs_mount_t *mp, /* file system mount structure */
2163 xfs_trans_t *tp, /* transaction pointer */
2164 xfs_agnumber_t agno, /* allocation group number */
2165 int flags) /* XFS_ALLOC_FLAGS_... */
2170 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2173 xfs_trans_brelse(tp, bp);
2178 * Put the block on the freelist for the allocation group.
2181 xfs_alloc_put_freelist(
2182 xfs_trans_t *tp, /* transaction pointer */
2183 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2184 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2185 xfs_agblock_t bno, /* block being freed */
2186 int btreeblk) /* block came from a AGF btree */
2188 xfs_agf_t *agf; /* a.g. freespace structure */
2189 xfs_agfl_t *agfl; /* a.g. free block array */
2190 __be32 *blockp;/* pointer to array entry */
2193 xfs_mount_t *mp; /* mount structure */
2194 xfs_perag_t *pag; /* per allocation group data */
2196 agf = XFS_BUF_TO_AGF(agbp);
2199 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2200 be32_to_cpu(agf->agf_seqno), &agflbp)))
2202 agfl = XFS_BUF_TO_AGFL(agflbp);
2203 be32_add_cpu(&agf->agf_fllast, 1);
2204 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2205 agf->agf_fllast = 0;
2206 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2207 be32_add_cpu(&agf->agf_flcount, 1);
2208 xfs_trans_agflist_delta(tp, 1);
2209 pag->pagf_flcount++;
2211 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2213 be32_add_cpu(&agf->agf_btreeblks, -1);
2214 pag->pagf_btreeblks--;
2215 logflags |= XFS_AGF_BTREEBLKS;
2218 TRACE_MODAGF(NULL, agf, logflags);
2219 xfs_alloc_log_agf(tp, agbp, logflags);
2221 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2222 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2223 *blockp = cpu_to_be32(bno);
2224 TRACE_MODAGF(NULL, agf, logflags);
2225 xfs_alloc_log_agf(tp, agbp, logflags);
2226 xfs_trans_log_buf(tp, agflbp,
2227 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2228 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2229 sizeof(xfs_agblock_t) - 1));
2234 * Read in the allocation group header (free/alloc section).
2238 xfs_mount_t *mp, /* mount point structure */
2239 xfs_trans_t *tp, /* transaction pointer */
2240 xfs_agnumber_t agno, /* allocation group number */
2241 int flags, /* XFS_ALLOC_FLAG_... */
2242 xfs_buf_t **bpp) /* buffer for the ag freelist header */
2244 xfs_agf_t *agf; /* ag freelist header */
2245 int agf_ok; /* set if agf is consistent */
2246 xfs_buf_t *bp; /* return value */
2247 xfs_perag_t *pag; /* per allocation group data */
2250 ASSERT(agno != NULLAGNUMBER);
2251 error = xfs_trans_read_buf(
2252 mp, tp, mp->m_ddev_targp,
2253 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2254 XFS_FSS_TO_BB(mp, 1),
2255 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
2259 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
2265 * Validate the magic number of the agf block.
2267 agf = XFS_BUF_TO_AGF(bp);
2269 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2270 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2271 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2272 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2273 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2274 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
2275 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2276 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2277 be32_to_cpu(agf->agf_length);
2278 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2279 XFS_RANDOM_ALLOC_READ_AGF))) {
2280 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2281 XFS_ERRLEVEL_LOW, mp, agf);
2282 xfs_trans_brelse(tp, bp);
2283 return XFS_ERROR(EFSCORRUPTED);
2285 pag = &mp->m_perag[agno];
2286 if (!pag->pagf_init) {
2287 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2288 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2289 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2290 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2291 pag->pagf_levels[XFS_BTNUM_BNOi] =
2292 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2293 pag->pagf_levels[XFS_BTNUM_CNTi] =
2294 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2295 spin_lock_init(&pag->pagb_lock);
2296 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2297 sizeof(xfs_perag_busy_t), KM_SLEEP);
2301 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2302 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2303 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2304 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2305 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2306 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2307 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2308 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2309 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2312 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
2318 * Allocate an extent (variable-size).
2319 * Depending on the allocation type, we either look in a single allocation
2320 * group or loop over the allocation groups to find the result.
2324 xfs_alloc_arg_t *args) /* allocation argument structure */
2326 xfs_agblock_t agsize; /* allocation group size */
2328 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2329 xfs_extlen_t minleft;/* minimum left value, temp copy */
2330 xfs_mount_t *mp; /* mount structure pointer */
2331 xfs_agnumber_t sagno; /* starting allocation group number */
2332 xfs_alloctype_t type; /* input allocation type */
2335 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2338 type = args->otype = args->type;
2339 args->agbno = NULLAGBLOCK;
2341 * Just fix this up, for the case where the last a.g. is shorter
2342 * (or there's only one a.g.) and the caller couldn't easily figure
2343 * that out (xfs_bmap_alloc).
2345 agsize = mp->m_sb.sb_agblocks;
2346 if (args->maxlen > agsize)
2347 args->maxlen = agsize;
2348 if (args->alignment == 0)
2349 args->alignment = 1;
2350 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2351 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2352 ASSERT(args->minlen <= args->maxlen);
2353 ASSERT(args->minlen <= agsize);
2354 ASSERT(args->mod < args->prod);
2355 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2356 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2357 args->minlen > args->maxlen || args->minlen > agsize ||
2358 args->mod >= args->prod) {
2359 args->fsbno = NULLFSBLOCK;
2360 TRACE_ALLOC("badargs", args);
2363 minleft = args->minleft;
2366 case XFS_ALLOCTYPE_THIS_AG:
2367 case XFS_ALLOCTYPE_NEAR_BNO:
2368 case XFS_ALLOCTYPE_THIS_BNO:
2370 * These three force us into a single a.g.
2372 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2373 down_read(&mp->m_peraglock);
2374 args->pag = &mp->m_perag[args->agno];
2376 error = xfs_alloc_fix_freelist(args, 0);
2377 args->minleft = minleft;
2379 TRACE_ALLOC("nofix", args);
2383 up_read(&mp->m_peraglock);
2384 TRACE_ALLOC("noagbp", args);
2387 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2388 if ((error = xfs_alloc_ag_vextent(args)))
2390 up_read(&mp->m_peraglock);
2392 case XFS_ALLOCTYPE_START_BNO:
2394 * Try near allocation first, then anywhere-in-ag after
2395 * the first a.g. fails.
2397 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2398 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2399 args->fsbno = XFS_AGB_TO_FSB(mp,
2400 ((mp->m_agfrotor / rotorstep) %
2401 mp->m_sb.sb_agcount), 0);
2404 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2405 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2407 case XFS_ALLOCTYPE_ANY_AG:
2408 case XFS_ALLOCTYPE_START_AG:
2409 case XFS_ALLOCTYPE_FIRST_AG:
2411 * Rotate through the allocation groups looking for a winner.
2413 if (type == XFS_ALLOCTYPE_ANY_AG) {
2415 * Start with the last place we left off.
2417 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2418 mp->m_sb.sb_agcount;
2419 args->type = XFS_ALLOCTYPE_THIS_AG;
2420 flags = XFS_ALLOC_FLAG_TRYLOCK;
2421 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2423 * Start with allocation group given by bno.
2425 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2426 args->type = XFS_ALLOCTYPE_THIS_AG;
2430 if (type == XFS_ALLOCTYPE_START_AG)
2431 args->type = XFS_ALLOCTYPE_THIS_AG;
2433 * Start with the given allocation group.
2435 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2436 flags = XFS_ALLOC_FLAG_TRYLOCK;
2439 * Loop over allocation groups twice; first time with
2440 * trylock set, second time without.
2442 down_read(&mp->m_peraglock);
2444 args->pag = &mp->m_perag[args->agno];
2445 if (no_min) args->minleft = 0;
2446 error = xfs_alloc_fix_freelist(args, flags);
2447 args->minleft = minleft;
2449 TRACE_ALLOC("nofix", args);
2453 * If we get a buffer back then the allocation will fly.
2456 if ((error = xfs_alloc_ag_vextent(args)))
2460 TRACE_ALLOC("loopfailed", args);
2462 * Didn't work, figure out the next iteration.
2464 if (args->agno == sagno &&
2465 type == XFS_ALLOCTYPE_START_BNO)
2466 args->type = XFS_ALLOCTYPE_THIS_AG;
2468 * For the first allocation, we can try any AG to get
2469 * space. However, if we already have allocated a
2470 * block, we don't want to try AGs whose number is below
2471 * sagno. Otherwise, we may end up with out-of-order
2472 * locking of AGF, which might cause deadlock.
2474 if (++(args->agno) == mp->m_sb.sb_agcount) {
2475 if (args->firstblock != NULLFSBLOCK)
2481 * Reached the starting a.g., must either be done
2482 * or switch to non-trylock mode.
2484 if (args->agno == sagno) {
2486 args->agbno = NULLAGBLOCK;
2487 TRACE_ALLOC("allfailed", args);
2494 if (type == XFS_ALLOCTYPE_START_BNO) {
2495 args->agbno = XFS_FSB_TO_AGBNO(mp,
2497 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2502 up_read(&mp->m_peraglock);
2503 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2504 if (args->agno == sagno)
2505 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2506 (mp->m_sb.sb_agcount * rotorstep);
2508 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2509 (mp->m_sb.sb_agcount * rotorstep);
2516 if (args->agbno == NULLAGBLOCK)
2517 args->fsbno = NULLFSBLOCK;
2519 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2521 ASSERT(args->len >= args->minlen);
2522 ASSERT(args->len <= args->maxlen);
2523 ASSERT(args->agbno % args->alignment == 0);
2524 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2530 up_read(&mp->m_peraglock);
2536 * Just break up the extent address and hand off to xfs_free_ag_extent
2537 * after fixing up the freelist.
2541 xfs_trans_t *tp, /* transaction pointer */
2542 xfs_fsblock_t bno, /* starting block number of extent */
2543 xfs_extlen_t len) /* length of extent */
2545 xfs_alloc_arg_t args;
2549 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2551 args.mp = tp->t_mountp;
2552 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2553 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2554 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2555 down_read(&args.mp->m_peraglock);
2556 args.pag = &args.mp->m_perag[args.agno];
2557 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2560 ASSERT(args.agbp != NULL);
2561 ASSERT((args.agbno + len) <=
2562 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2564 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2566 up_read(&args.mp->m_peraglock);
2572 * AG Busy list management
2573 * The busy list contains block ranges that have been freed but whose
2574 * transactions have not yet hit disk. If any block listed in a busy
2575 * list is reused, the transaction that freed it must be forced to disk
2576 * before continuing to use the block.
2578 * xfs_alloc_mark_busy - add to the per-ag busy list
2579 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2582 xfs_alloc_mark_busy(xfs_trans_t *tp,
2583 xfs_agnumber_t agno,
2588 xfs_perag_busy_t *bsy;
2592 spin_lock(&mp->m_perag[agno].pagb_lock);
2594 /* search pagb_list for an open slot */
2595 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2596 n < XFS_PAGB_NUM_SLOTS;
2598 if (bsy->busy_tp == NULL) {
2603 if (n < XFS_PAGB_NUM_SLOTS) {
2604 bsy = &mp->m_perag[agno].pagb_list[n];
2605 mp->m_perag[agno].pagb_count++;
2606 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2607 bsy->busy_start = bno;
2608 bsy->busy_length = len;
2610 xfs_trans_add_busy(tp, agno, n);
2612 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2614 * The busy list is full! Since it is now not possible to
2615 * track the free block, make this a synchronous transaction
2616 * to insure that the block is not reused before this
2617 * transaction commits.
2619 xfs_trans_set_sync(tp);
2622 spin_unlock(&mp->m_perag[agno].pagb_lock);
2626 xfs_alloc_clear_busy(xfs_trans_t *tp,
2627 xfs_agnumber_t agno,
2631 xfs_perag_busy_t *list;
2635 spin_lock(&mp->m_perag[agno].pagb_lock);
2636 list = mp->m_perag[agno].pagb_list;
2638 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2639 if (list[idx].busy_tp == tp) {
2640 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2641 list[idx].busy_tp = NULL;
2642 mp->m_perag[agno].pagb_count--;
2644 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2647 spin_unlock(&mp->m_perag[agno].pagb_lock);
2652 * If we find the extent in the busy list, force the log out to get the
2653 * extent out of the busy list so the caller can use it straight away.
2656 xfs_alloc_search_busy(xfs_trans_t *tp,
2657 xfs_agnumber_t agno,
2662 xfs_perag_busy_t *bsy;
2663 xfs_agblock_t uend, bend;
2669 spin_lock(&mp->m_perag[agno].pagb_lock);
2670 cnt = mp->m_perag[agno].pagb_count;
2672 uend = bno + len - 1;
2674 /* search pagb_list for this slot, skipping open slots */
2675 for (bsy = mp->m_perag[agno].pagb_list; cnt; bsy++) {
2678 * (start1,length1) within (start2, length2)
2680 if (bsy->busy_tp != NULL) {
2681 bend = bsy->busy_start + bsy->busy_length - 1;
2682 if ((bno > bend) || (uend < bsy->busy_start)) {
2685 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
2686 "found1", agno, bno, len, tp);
2693 * If a block was found, force the log through the LSN of the
2694 * transaction that freed the block
2697 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, tp);
2698 lsn = bsy->busy_tp->t_commit_lsn;
2699 spin_unlock(&mp->m_perag[agno].pagb_lock);
2700 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2702 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, tp);
2703 spin_unlock(&mp->m_perag[agno].pagb_lock);