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 * Compute aligned version of the found extent.
159 * Takes alignment and min length into account.
162 xfs_alloc_compute_aligned(
163 xfs_agblock_t foundbno, /* starting block in found extent */
164 xfs_extlen_t foundlen, /* length in found extent */
165 xfs_extlen_t alignment, /* alignment for allocation */
166 xfs_extlen_t minlen, /* minimum length for allocation */
167 xfs_agblock_t *resbno, /* result block number */
168 xfs_extlen_t *reslen) /* result length */
174 if (alignment > 1 && foundlen >= minlen) {
175 bno = roundup(foundbno, alignment);
176 diff = bno - foundbno;
177 len = diff >= foundlen ? 0 : foundlen - diff;
187 * Compute best start block and diff for "near" allocations.
188 * freelen >= wantlen already checked by caller.
190 STATIC xfs_extlen_t /* difference value (absolute) */
191 xfs_alloc_compute_diff(
192 xfs_agblock_t wantbno, /* target starting block */
193 xfs_extlen_t wantlen, /* target length */
194 xfs_extlen_t alignment, /* target alignment */
195 xfs_agblock_t freebno, /* freespace's starting block */
196 xfs_extlen_t freelen, /* freespace's length */
197 xfs_agblock_t *newbnop) /* result: best start block from free */
199 xfs_agblock_t freeend; /* end of freespace extent */
200 xfs_agblock_t newbno1; /* return block number */
201 xfs_agblock_t newbno2; /* other new block number */
202 xfs_extlen_t newlen1=0; /* length with newbno1 */
203 xfs_extlen_t newlen2=0; /* length with newbno2 */
204 xfs_agblock_t wantend; /* end of target extent */
206 ASSERT(freelen >= wantlen);
207 freeend = freebno + freelen;
208 wantend = wantbno + wantlen;
209 if (freebno >= wantbno) {
210 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
211 newbno1 = NULLAGBLOCK;
212 } else if (freeend >= wantend && alignment > 1) {
213 newbno1 = roundup(wantbno, alignment);
214 newbno2 = newbno1 - alignment;
215 if (newbno1 >= freeend)
216 newbno1 = NULLAGBLOCK;
218 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
219 if (newbno2 < freebno)
220 newbno2 = NULLAGBLOCK;
222 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
223 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
224 if (newlen1 < newlen2 ||
225 (newlen1 == newlen2 &&
226 XFS_ABSDIFF(newbno1, wantbno) >
227 XFS_ABSDIFF(newbno2, wantbno)))
229 } else if (newbno2 != NULLAGBLOCK)
231 } else if (freeend >= wantend) {
233 } else if (alignment > 1) {
234 newbno1 = roundup(freeend - wantlen, alignment);
235 if (newbno1 > freeend - wantlen &&
236 newbno1 - alignment >= freebno)
237 newbno1 -= alignment;
238 else if (newbno1 >= freeend)
239 newbno1 = NULLAGBLOCK;
241 newbno1 = freeend - wantlen;
243 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
247 * Fix up the length, based on mod and prod.
248 * len should be k * prod + mod for some k.
249 * If len is too small it is returned unchanged.
250 * If len hits maxlen it is left alone.
254 xfs_alloc_arg_t *args) /* allocation argument structure */
259 ASSERT(args->mod < args->prod);
261 ASSERT(rlen >= args->minlen);
262 ASSERT(rlen <= args->maxlen);
263 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
264 (args->mod == 0 && rlen < args->prod))
266 k = rlen % args->prod;
270 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
273 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
277 ASSERT(rlen >= args->minlen);
278 ASSERT(rlen <= args->maxlen);
283 * Fix up length if there is too little space left in the a.g.
284 * Return 1 if ok, 0 if too little, should give up.
287 xfs_alloc_fix_minleft(
288 xfs_alloc_arg_t *args) /* allocation argument structure */
290 xfs_agf_t *agf; /* a.g. freelist header */
291 int diff; /* free space difference */
293 if (args->minleft == 0)
295 agf = XFS_BUF_TO_AGF(args->agbp);
296 diff = be32_to_cpu(agf->agf_freeblks)
297 + be32_to_cpu(agf->agf_flcount)
298 - args->len - args->minleft;
301 args->len += diff; /* shrink the allocated space */
302 if (args->len >= args->minlen)
304 args->agbno = NULLAGBLOCK;
309 * Update the two btrees, logically removing from freespace the extent
310 * starting at rbno, rlen blocks. The extent is contained within the
311 * actual (current) free extent fbno for flen blocks.
312 * Flags are passed in indicating whether the cursors are set to the
315 STATIC int /* error code */
316 xfs_alloc_fixup_trees(
317 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
318 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
319 xfs_agblock_t fbno, /* starting block of free extent */
320 xfs_extlen_t flen, /* length of free extent */
321 xfs_agblock_t rbno, /* starting block of returned extent */
322 xfs_extlen_t rlen, /* length of returned extent */
323 int flags) /* flags, XFSA_FIXUP_... */
325 int error; /* error code */
326 int i; /* operation results */
327 xfs_agblock_t nfbno1; /* first new free startblock */
328 xfs_agblock_t nfbno2; /* second new free startblock */
329 xfs_extlen_t nflen1=0; /* first new free length */
330 xfs_extlen_t nflen2=0; /* second new free length */
333 * Look up the record in the by-size tree if necessary.
335 if (flags & XFSA_FIXUP_CNT_OK) {
337 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
339 XFS_WANT_CORRUPTED_RETURN(
340 i == 1 && nfbno1 == fbno && nflen1 == flen);
343 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
345 XFS_WANT_CORRUPTED_RETURN(i == 1);
348 * Look up the record in the by-block tree if necessary.
350 if (flags & XFSA_FIXUP_BNO_OK) {
352 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
354 XFS_WANT_CORRUPTED_RETURN(
355 i == 1 && nfbno1 == fbno && nflen1 == flen);
358 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
360 XFS_WANT_CORRUPTED_RETURN(i == 1);
364 xfs_alloc_block_t *bnoblock;
365 xfs_alloc_block_t *cntblock;
367 if (bno_cur->bc_nlevels == 1 &&
368 cnt_cur->bc_nlevels == 1) {
369 bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
370 cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
371 XFS_WANT_CORRUPTED_RETURN(
372 be16_to_cpu(bnoblock->bb_numrecs) ==
373 be16_to_cpu(cntblock->bb_numrecs));
378 * Deal with all four cases: the allocated record is contained
379 * within the freespace record, so we can have new freespace
380 * at either (or both) end, or no freespace remaining.
382 if (rbno == fbno && rlen == flen)
383 nfbno1 = nfbno2 = NULLAGBLOCK;
384 else if (rbno == fbno) {
385 nfbno1 = rbno + rlen;
386 nflen1 = flen - rlen;
387 nfbno2 = NULLAGBLOCK;
388 } else if (rbno + rlen == fbno + flen) {
390 nflen1 = flen - rlen;
391 nfbno2 = NULLAGBLOCK;
394 nflen1 = rbno - fbno;
395 nfbno2 = rbno + rlen;
396 nflen2 = (fbno + flen) - nfbno2;
399 * Delete the entry from the by-size btree.
401 if ((error = xfs_alloc_delete(cnt_cur, &i)))
403 XFS_WANT_CORRUPTED_RETURN(i == 1);
405 * Add new by-size btree entry(s).
407 if (nfbno1 != NULLAGBLOCK) {
408 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
410 XFS_WANT_CORRUPTED_RETURN(i == 0);
411 if ((error = xfs_btree_insert(cnt_cur, &i)))
413 XFS_WANT_CORRUPTED_RETURN(i == 1);
415 if (nfbno2 != NULLAGBLOCK) {
416 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
418 XFS_WANT_CORRUPTED_RETURN(i == 0);
419 if ((error = xfs_btree_insert(cnt_cur, &i)))
421 XFS_WANT_CORRUPTED_RETURN(i == 1);
424 * Fix up the by-block btree entry(s).
426 if (nfbno1 == NULLAGBLOCK) {
428 * No remaining freespace, just delete the by-block tree entry.
430 if ((error = xfs_alloc_delete(bno_cur, &i)))
432 XFS_WANT_CORRUPTED_RETURN(i == 1);
435 * Update the by-block entry to start later|be shorter.
437 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
440 if (nfbno2 != NULLAGBLOCK) {
442 * 2 resulting free entries, need to add one.
444 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
446 XFS_WANT_CORRUPTED_RETURN(i == 0);
447 if ((error = xfs_btree_insert(bno_cur, &i)))
449 XFS_WANT_CORRUPTED_RETURN(i == 1);
455 * Read in the allocation group free block array.
457 STATIC int /* error */
459 xfs_mount_t *mp, /* mount point structure */
460 xfs_trans_t *tp, /* transaction pointer */
461 xfs_agnumber_t agno, /* allocation group number */
462 xfs_buf_t **bpp) /* buffer for the ag free block array */
464 xfs_buf_t *bp; /* return value */
467 ASSERT(agno != NULLAGNUMBER);
468 error = xfs_trans_read_buf(
469 mp, tp, mp->m_ddev_targp,
470 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
471 XFS_FSS_TO_BB(mp, 1), 0, &bp);
475 ASSERT(!XFS_BUF_GETERROR(bp));
476 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
481 #if defined(XFS_ALLOC_TRACE)
483 * Add an allocation trace entry for an alloc call.
486 xfs_alloc_trace_alloc(
487 const char *name, /* function tag string */
488 char *str, /* additional string */
489 xfs_alloc_arg_t *args, /* allocation argument structure */
490 int line) /* source line number */
492 ktrace_enter(xfs_alloc_trace_buf,
493 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
497 (void *)(__psunsigned_t)args->agno,
498 (void *)(__psunsigned_t)args->agbno,
499 (void *)(__psunsigned_t)args->minlen,
500 (void *)(__psunsigned_t)args->maxlen,
501 (void *)(__psunsigned_t)args->mod,
502 (void *)(__psunsigned_t)args->prod,
503 (void *)(__psunsigned_t)args->minleft,
504 (void *)(__psunsigned_t)args->total,
505 (void *)(__psunsigned_t)args->alignment,
506 (void *)(__psunsigned_t)args->len,
507 (void *)((((__psint_t)args->type) << 16) |
508 (__psint_t)args->otype),
509 (void *)(__psint_t)((args->wasdel << 3) |
510 (args->wasfromfl << 2) |
512 (args->userdata << 0)));
516 * Add an allocation trace entry for a free call.
519 xfs_alloc_trace_free(
520 const char *name, /* function tag string */
521 char *str, /* additional string */
522 xfs_mount_t *mp, /* file system mount point */
523 xfs_agnumber_t agno, /* allocation group number */
524 xfs_agblock_t agbno, /* a.g. relative block number */
525 xfs_extlen_t len, /* length of extent */
526 int isfl, /* set if is freelist allocation/free */
527 int line) /* source line number */
529 ktrace_enter(xfs_alloc_trace_buf,
530 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
534 (void *)(__psunsigned_t)agno,
535 (void *)(__psunsigned_t)agbno,
536 (void *)(__psunsigned_t)len,
537 (void *)(__psint_t)isfl,
538 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
542 * Add an allocation trace entry for modifying an agf.
545 xfs_alloc_trace_modagf(
546 const char *name, /* function tag string */
547 char *str, /* additional string */
548 xfs_mount_t *mp, /* file system mount point */
549 xfs_agf_t *agf, /* new agf value */
550 int flags, /* logging flags for agf */
551 int line) /* source line number */
553 ktrace_enter(xfs_alloc_trace_buf,
554 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
558 (void *)(__psint_t)flags,
559 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
560 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
561 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
562 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
563 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
564 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
565 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
566 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
567 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
568 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
569 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
573 xfs_alloc_trace_busy(
574 const char *name, /* function tag string */
575 char *str, /* additional string */
576 xfs_mount_t *mp, /* file system mount point */
577 xfs_agnumber_t agno, /* allocation group number */
578 xfs_agblock_t agbno, /* a.g. relative block number */
579 xfs_extlen_t len, /* length of extent */
580 int slot, /* perag Busy slot */
582 int trtype, /* type: add, delete, search */
583 int line) /* source line number */
585 ktrace_enter(xfs_alloc_trace_buf,
586 (void *)(__psint_t)(trtype | (line << 16)),
590 (void *)(__psunsigned_t)agno,
591 (void *)(__psunsigned_t)agbno,
592 (void *)(__psunsigned_t)len,
593 (void *)(__psint_t)slot,
595 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
597 #endif /* XFS_ALLOC_TRACE */
600 * Allocation group level functions.
604 * Allocate a variable extent in the allocation group agno.
605 * Type and bno are used to determine where in the allocation group the
607 * Extent's length (returned in *len) will be between minlen and maxlen,
608 * and of the form k * prod + mod unless there's nothing that large.
609 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
611 STATIC int /* error */
612 xfs_alloc_ag_vextent(
613 xfs_alloc_arg_t *args) /* argument structure for allocation */
617 ASSERT(args->minlen > 0);
618 ASSERT(args->maxlen > 0);
619 ASSERT(args->minlen <= args->maxlen);
620 ASSERT(args->mod < args->prod);
621 ASSERT(args->alignment > 0);
623 * Branch to correct routine based on the type.
626 switch (args->type) {
627 case XFS_ALLOCTYPE_THIS_AG:
628 error = xfs_alloc_ag_vextent_size(args);
630 case XFS_ALLOCTYPE_NEAR_BNO:
631 error = xfs_alloc_ag_vextent_near(args);
633 case XFS_ALLOCTYPE_THIS_BNO:
634 error = xfs_alloc_ag_vextent_exact(args);
643 * If the allocation worked, need to change the agf structure
644 * (and log it), and the superblock.
646 if (args->agbno != NULLAGBLOCK) {
647 xfs_agf_t *agf; /* allocation group freelist header */
648 #ifdef XFS_ALLOC_TRACE
649 xfs_mount_t *mp = args->mp;
651 long slen = (long)args->len;
653 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
654 ASSERT(!(args->wasfromfl) || !args->isfl);
655 ASSERT(args->agbno % args->alignment == 0);
656 if (!(args->wasfromfl)) {
658 agf = XFS_BUF_TO_AGF(args->agbp);
659 be32_add_cpu(&agf->agf_freeblks, -(args->len));
660 xfs_trans_agblocks_delta(args->tp,
661 -((long)(args->len)));
662 args->pag->pagf_freeblks -= args->len;
663 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
664 be32_to_cpu(agf->agf_length));
665 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
666 xfs_alloc_log_agf(args->tp, args->agbp,
668 /* search the busylist for these blocks */
669 xfs_alloc_search_busy(args->tp, args->agno,
670 args->agbno, args->len);
673 xfs_trans_mod_sb(args->tp,
674 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
675 XFS_TRANS_SB_FDBLOCKS, -slen);
676 XFS_STATS_INC(xs_allocx);
677 XFS_STATS_ADD(xs_allocb, args->len);
683 * Allocate a variable extent at exactly agno/bno.
684 * Extent's length (returned in *len) will be between minlen and maxlen,
685 * and of the form k * prod + mod unless there's nothing that large.
686 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
688 STATIC int /* error */
689 xfs_alloc_ag_vextent_exact(
690 xfs_alloc_arg_t *args) /* allocation argument structure */
692 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
693 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
694 xfs_agblock_t end; /* end of allocated extent */
696 xfs_agblock_t fbno; /* start block of found extent */
697 xfs_agblock_t fend; /* end block of found extent */
698 xfs_extlen_t flen; /* length of found extent */
699 int i; /* success/failure of operation */
700 xfs_agblock_t maxend; /* end of maximal extent */
701 xfs_agblock_t minend; /* end of minimal extent */
702 xfs_extlen_t rlen; /* length of returned extent */
704 ASSERT(args->alignment == 1);
706 * Allocate/initialize a cursor for the by-number freespace btree.
708 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
709 args->agno, XFS_BTNUM_BNO);
711 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
712 * Look for the closest free block <= bno, it must contain bno
713 * if any free block does.
715 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
719 * Didn't find it, return null.
721 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
722 args->agbno = NULLAGBLOCK;
726 * Grab the freespace record.
728 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
730 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
731 ASSERT(fbno <= args->agbno);
732 minend = args->agbno + args->minlen;
733 maxend = args->agbno + args->maxlen;
736 * Give up if the freespace isn't long enough for the minimum request.
739 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
740 args->agbno = NULLAGBLOCK;
744 * End of extent will be smaller of the freespace end and the
745 * maximal requested end.
747 end = XFS_AGBLOCK_MIN(fend, maxend);
749 * Fix the length according to mod and prod if given.
751 args->len = end - args->agbno;
752 xfs_alloc_fix_len(args);
753 if (!xfs_alloc_fix_minleft(args)) {
754 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
758 ASSERT(args->agbno + rlen <= fend);
759 end = args->agbno + rlen;
761 * We are allocating agbno for rlen [agbno .. end]
762 * Allocate/initialize a cursor for the by-size btree.
764 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
765 args->agno, XFS_BTNUM_CNT);
766 ASSERT(args->agbno + args->len <=
767 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
768 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
769 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
770 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
773 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
774 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
775 TRACE_ALLOC("normal", args);
780 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
781 TRACE_ALLOC("error", args);
786 * Allocate a variable extent near bno in the allocation group agno.
787 * Extent's length (returned in len) will be between minlen and maxlen,
788 * and of the form k * prod + mod unless there's nothing that large.
789 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
791 STATIC int /* error */
792 xfs_alloc_ag_vextent_near(
793 xfs_alloc_arg_t *args) /* allocation argument structure */
795 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
796 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
797 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
798 xfs_agblock_t gtbno; /* start bno of right side entry */
799 xfs_agblock_t gtbnoa; /* aligned ... */
800 xfs_extlen_t gtdiff; /* difference to right side entry */
801 xfs_extlen_t gtlen; /* length of right side entry */
802 xfs_extlen_t gtlena; /* aligned ... */
803 xfs_agblock_t gtnew; /* useful start bno of right side */
804 int error; /* error code */
805 int i; /* result code, temporary */
806 int j; /* result code, temporary */
807 xfs_agblock_t ltbno; /* start bno of left side entry */
808 xfs_agblock_t ltbnoa; /* aligned ... */
809 xfs_extlen_t ltdiff; /* difference to left side entry */
811 xfs_agblock_t ltend; /* end bno of left side entry */
812 xfs_extlen_t ltlen; /* length of left side entry */
813 xfs_extlen_t ltlena; /* aligned ... */
814 xfs_agblock_t ltnew; /* useful start bno of left side */
815 xfs_extlen_t rlen; /* length of returned extent */
816 #if defined(DEBUG) && defined(__KERNEL__)
818 * Randomly don't execute the first algorithm.
820 int dofirst; /* set to do first algorithm */
822 dofirst = random32() & 1;
825 * Get a cursor for the by-size btree.
827 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
828 args->agno, XFS_BTNUM_CNT);
830 bno_cur_lt = bno_cur_gt = NULL;
832 * See if there are any free extents as big as maxlen.
834 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
837 * If none, then pick up the last entry in the tree unless the
841 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
844 if (i == 0 || ltlen == 0) {
845 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
853 * If the requested extent is large wrt the freespaces available
854 * in this a.g., then the cursor will be pointing to a btree entry
855 * near the right edge of the tree. If it's in the last btree leaf
856 * block, then we just examine all the entries in that block
857 * that are big enough, and pick the best one.
858 * This is written as a while loop so we can break out of it,
859 * but we never loop back to the top.
861 while (xfs_btree_islastblock(cnt_cur, 0)) {
865 xfs_agblock_t bnew=0;
867 #if defined(DEBUG) && defined(__KERNEL__)
872 * Start from the entry that lookup found, sequence through
873 * all larger free blocks. If we're actually pointing at a
874 * record smaller than maxlen, go to the start of this block,
875 * and skip all those smaller than minlen.
877 if (ltlen || args->alignment > 1) {
878 cnt_cur->bc_ptrs[0] = 1;
880 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
883 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
884 if (ltlen >= args->minlen)
886 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
889 ASSERT(ltlen >= args->minlen);
893 i = cnt_cur->bc_ptrs[0];
894 for (j = 1, blen = 0, bdiff = 0;
895 !error && j && (blen < args->maxlen || bdiff > 0);
896 error = xfs_btree_increment(cnt_cur, 0, &j)) {
898 * For each entry, decide if it's better than
899 * the previous best entry.
901 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
903 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
904 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
905 args->minlen, <bnoa, <lena);
906 if (ltlena < args->minlen)
908 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
909 xfs_alloc_fix_len(args);
910 ASSERT(args->len >= args->minlen);
911 if (args->len < blen)
913 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
914 args->alignment, ltbno, ltlen, <new);
915 if (ltnew != NULLAGBLOCK &&
916 (args->len > blen || ltdiff < bdiff)) {
920 besti = cnt_cur->bc_ptrs[0];
924 * It didn't work. We COULD be in a case where
925 * there's a good record somewhere, so try again.
930 * Point at the best entry, and retrieve it again.
932 cnt_cur->bc_ptrs[0] = besti;
933 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
935 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
936 ltend = ltbno + ltlen;
937 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
939 if (!xfs_alloc_fix_minleft(args)) {
940 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
941 TRACE_ALLOC("nominleft", args);
946 * We are allocating starting at bnew for blen blocks.
949 ASSERT(bnew >= ltbno);
950 ASSERT(bnew + blen <= ltend);
952 * Set up a cursor for the by-bno tree.
954 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
955 args->agbp, args->agno, XFS_BTNUM_BNO);
957 * Fix up the btree entries.
959 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
960 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
962 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
963 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
964 TRACE_ALLOC("first", args);
969 * Search in the by-bno tree to the left and to the right
970 * simultaneously, until in each case we find a space big enough,
971 * or run into the edge of the tree. When we run into the edge,
972 * we deallocate that cursor.
973 * If both searches succeed, we compare the two spaces and pick
975 * With alignment, it's possible for both to fail; the upper
976 * level algorithm that picks allocation groups for allocations
977 * is not supposed to do this.
980 * Allocate and initialize the cursor for the leftward search.
982 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
983 args->agno, XFS_BTNUM_BNO);
985 * Lookup <= bno to find the leftward search's starting point.
987 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
991 * Didn't find anything; use this cursor for the rightward
994 bno_cur_gt = bno_cur_lt;
998 * Found something. Duplicate the cursor for the rightward search.
1000 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1003 * Increment the cursor, so we will point at the entry just right
1004 * of the leftward entry if any, or to the leftmost entry.
1006 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1010 * It failed, there are no rightward entries.
1012 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1016 * Loop going left with the leftward cursor, right with the
1017 * rightward cursor, until either both directions give up or
1018 * we find an entry at least as big as minlen.
1022 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1024 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1025 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
1026 args->minlen, <bnoa, <lena);
1027 if (ltlena >= args->minlen)
1029 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1032 xfs_btree_del_cursor(bno_cur_lt,
1038 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1040 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1041 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
1042 args->minlen, >bnoa, >lena);
1043 if (gtlena >= args->minlen)
1045 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1048 xfs_btree_del_cursor(bno_cur_gt,
1053 } while (bno_cur_lt || bno_cur_gt);
1055 * Got both cursors still active, need to find better entry.
1057 if (bno_cur_lt && bno_cur_gt) {
1059 * Left side is long enough, look for a right side entry.
1061 if (ltlena >= args->minlen) {
1063 * Fix up the length.
1065 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1066 xfs_alloc_fix_len(args);
1068 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1069 args->alignment, ltbno, ltlen, <new);
1075 * Look until we find a better one, run out of
1076 * space, or run off the end.
1078 while (bno_cur_lt && bno_cur_gt) {
1079 if ((error = xfs_alloc_get_rec(
1083 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1084 xfs_alloc_compute_aligned(gtbno, gtlen,
1085 args->alignment, args->minlen,
1088 * The left one is clearly better.
1090 if (gtbnoa >= args->agbno + ltdiff) {
1091 xfs_btree_del_cursor(
1098 * If we reach a big enough entry,
1099 * compare the two and pick the best.
1101 if (gtlena >= args->minlen) {
1103 XFS_EXTLEN_MIN(gtlena,
1105 xfs_alloc_fix_len(args);
1107 gtdiff = xfs_alloc_compute_diff(
1110 gtbno, gtlen, >new);
1112 * Right side is better.
1114 if (gtdiff < ltdiff) {
1115 xfs_btree_del_cursor(
1121 * Left side is better.
1124 xfs_btree_del_cursor(
1132 * Fell off the right end.
1134 if ((error = xfs_btree_increment(
1135 bno_cur_gt, 0, &i)))
1138 xfs_btree_del_cursor(
1147 * The left side is perfect, trash the right side.
1150 xfs_btree_del_cursor(bno_cur_gt,
1156 * It's the right side that was found first, look left.
1160 * Fix up the length.
1162 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1163 xfs_alloc_fix_len(args);
1165 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1166 args->alignment, gtbno, gtlen, >new);
1168 * Right side entry isn't perfect.
1172 * Look until we find a better one, run out of
1173 * space, or run off the end.
1175 while (bno_cur_lt && bno_cur_gt) {
1176 if ((error = xfs_alloc_get_rec(
1180 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1181 xfs_alloc_compute_aligned(ltbno, ltlen,
1182 args->alignment, args->minlen,
1185 * The right one is clearly better.
1187 if (ltbnoa <= args->agbno - gtdiff) {
1188 xfs_btree_del_cursor(
1195 * If we reach a big enough entry,
1196 * compare the two and pick the best.
1198 if (ltlena >= args->minlen) {
1199 args->len = XFS_EXTLEN_MIN(
1200 ltlena, args->maxlen);
1201 xfs_alloc_fix_len(args);
1203 ltdiff = xfs_alloc_compute_diff(
1206 ltbno, ltlen, <new);
1208 * Left side is better.
1210 if (ltdiff < gtdiff) {
1211 xfs_btree_del_cursor(
1217 * Right side is better.
1220 xfs_btree_del_cursor(
1228 * Fell off the left end.
1230 if ((error = xfs_btree_decrement(
1231 bno_cur_lt, 0, &i)))
1234 xfs_btree_del_cursor(bno_cur_lt,
1242 * The right side is perfect, trash the left side.
1245 xfs_btree_del_cursor(bno_cur_lt,
1252 * If we couldn't get anything, give up.
1254 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1255 TRACE_ALLOC("neither", args);
1256 args->agbno = NULLAGBLOCK;
1260 * At this point we have selected a freespace entry, either to the
1261 * left or to the right. If it's on the right, copy all the
1262 * useful variables to the "left" set so we only have one
1263 * copy of this code.
1266 bno_cur_lt = bno_cur_gt;
1276 * Fix up the length and compute the useful address.
1278 ltend = ltbno + ltlen;
1279 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1280 xfs_alloc_fix_len(args);
1281 if (!xfs_alloc_fix_minleft(args)) {
1282 TRACE_ALLOC("nominleft", args);
1283 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1284 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1288 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1290 ASSERT(ltnew >= ltbno);
1291 ASSERT(ltnew + rlen <= ltend);
1292 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1293 args->agbno = ltnew;
1294 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1295 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1297 TRACE_ALLOC(j ? "gt" : "lt", args);
1298 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1299 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1303 TRACE_ALLOC("error", args);
1304 if (cnt_cur != NULL)
1305 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1306 if (bno_cur_lt != NULL)
1307 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1308 if (bno_cur_gt != NULL)
1309 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1314 * Allocate a variable extent anywhere in the allocation group agno.
1315 * Extent's length (returned in len) will be between minlen and maxlen,
1316 * and of the form k * prod + mod unless there's nothing that large.
1317 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1319 STATIC int /* error */
1320 xfs_alloc_ag_vextent_size(
1321 xfs_alloc_arg_t *args) /* allocation argument structure */
1323 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1324 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1325 int error; /* error result */
1326 xfs_agblock_t fbno; /* start of found freespace */
1327 xfs_extlen_t flen; /* length of found freespace */
1328 int i; /* temp status variable */
1329 xfs_agblock_t rbno; /* returned block number */
1330 xfs_extlen_t rlen; /* length of returned extent */
1333 * Allocate and initialize a cursor for the by-size btree.
1335 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1336 args->agno, XFS_BTNUM_CNT);
1339 * Look for an entry >= maxlen+alignment-1 blocks.
1341 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1342 args->maxlen + args->alignment - 1, &i)))
1345 * If none, then pick up the last entry in the tree unless the
1349 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1352 if (i == 0 || flen == 0) {
1353 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1354 TRACE_ALLOC("noentry", args);
1360 * There's a freespace as big as maxlen+alignment-1, get it.
1363 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1365 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1368 * In the first case above, we got the last entry in the
1369 * by-size btree. Now we check to see if the space hits maxlen
1370 * once aligned; if not, we search left for something better.
1371 * This can't happen in the second case above.
1373 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1375 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1376 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1377 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1378 if (rlen < args->maxlen) {
1379 xfs_agblock_t bestfbno;
1380 xfs_extlen_t bestflen;
1381 xfs_agblock_t bestrbno;
1382 xfs_extlen_t bestrlen;
1389 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1393 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1396 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1397 if (flen < bestrlen)
1399 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1400 args->minlen, &rbno, &rlen);
1401 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1402 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1403 (rlen <= flen && rbno + rlen <= fbno + flen),
1405 if (rlen > bestrlen) {
1410 if (rlen == args->maxlen)
1414 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1417 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1423 args->wasfromfl = 0;
1425 * Fix up the length.
1428 xfs_alloc_fix_len(args);
1429 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1430 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1431 TRACE_ALLOC("nominleft", args);
1432 args->agbno = NULLAGBLOCK;
1436 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1438 * Allocate and initialize a cursor for the by-block tree.
1440 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1441 args->agno, XFS_BTNUM_BNO);
1442 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1443 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1445 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1446 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1447 cnt_cur = bno_cur = NULL;
1450 XFS_WANT_CORRUPTED_GOTO(
1451 args->agbno + args->len <=
1452 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1454 TRACE_ALLOC("normal", args);
1458 TRACE_ALLOC("error", args);
1460 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1462 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1467 * Deal with the case where only small freespaces remain.
1468 * Either return the contents of the last freespace record,
1469 * or allocate space from the freelist if there is nothing in the tree.
1471 STATIC int /* error */
1472 xfs_alloc_ag_vextent_small(
1473 xfs_alloc_arg_t *args, /* allocation argument structure */
1474 xfs_btree_cur_t *ccur, /* by-size cursor */
1475 xfs_agblock_t *fbnop, /* result block number */
1476 xfs_extlen_t *flenp, /* result length */
1477 int *stat) /* status: 0-freelist, 1-normal/none */
1484 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1487 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1489 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1492 * Nothing in the btree, try the freelist. Make sure
1493 * to respect minleft even when pulling from the
1496 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1497 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1499 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1502 if (fbno != NULLAGBLOCK) {
1503 if (args->userdata) {
1506 bp = xfs_btree_get_bufs(args->mp, args->tp,
1507 args->agno, fbno, 0);
1508 xfs_trans_binval(args->tp, bp);
1512 XFS_WANT_CORRUPTED_GOTO(
1513 args->agbno + args->len <=
1514 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1516 args->wasfromfl = 1;
1517 TRACE_ALLOC("freelist", args);
1522 * Nothing in the freelist.
1528 * Can't allocate from the freelist for some reason.
1535 * Can't do the allocation, give up.
1537 if (flen < args->minlen) {
1538 args->agbno = NULLAGBLOCK;
1539 TRACE_ALLOC("notenough", args);
1545 TRACE_ALLOC("normal", args);
1549 TRACE_ALLOC("error", args);
1554 * Free the extent starting at agno/bno for length.
1556 STATIC int /* error */
1558 xfs_trans_t *tp, /* transaction pointer */
1559 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1560 xfs_agnumber_t agno, /* allocation group number */
1561 xfs_agblock_t bno, /* starting block number */
1562 xfs_extlen_t len, /* length of extent */
1563 int isfl) /* set if is freelist blocks - no sb acctg */
1565 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1566 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1567 int error; /* error return value */
1568 xfs_agblock_t gtbno; /* start of right neighbor block */
1569 xfs_extlen_t gtlen; /* length of right neighbor block */
1570 int haveleft; /* have a left neighbor block */
1571 int haveright; /* have a right neighbor block */
1572 int i; /* temp, result code */
1573 xfs_agblock_t ltbno; /* start of left neighbor block */
1574 xfs_extlen_t ltlen; /* length of left neighbor block */
1575 xfs_mount_t *mp; /* mount point struct for filesystem */
1576 xfs_agblock_t nbno; /* new starting block of freespace */
1577 xfs_extlen_t nlen; /* new length of freespace */
1581 * Allocate and initialize a cursor for the by-block btree.
1583 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1586 * Look for a neighboring block on the left (lower block numbers)
1587 * that is contiguous with this space.
1589 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1593 * There is a block to our left.
1595 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1597 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1599 * It's not contiguous, though.
1601 if (ltbno + ltlen < bno)
1605 * If this failure happens the request to free this
1606 * space was invalid, it's (partly) already free.
1609 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1613 * Look for a neighboring block on the right (higher block numbers)
1614 * that is contiguous with this space.
1616 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1620 * There is a block to our right.
1622 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1624 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1626 * It's not contiguous, though.
1628 if (bno + len < gtbno)
1632 * If this failure happens the request to free this
1633 * space was invalid, it's (partly) already free.
1636 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1640 * Now allocate and initialize a cursor for the by-size tree.
1642 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1644 * Have both left and right contiguous neighbors.
1645 * Merge all three into a single free block.
1647 if (haveleft && haveright) {
1649 * Delete the old by-size entry on the left.
1651 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1653 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1654 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1656 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1658 * Delete the old by-size entry on the right.
1660 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1662 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1663 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1665 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1667 * Delete the old by-block entry for the right block.
1669 if ((error = xfs_alloc_delete(bno_cur, &i)))
1671 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1673 * Move the by-block cursor back to the left neighbor.
1675 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1677 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1680 * Check that this is the right record: delete didn't
1681 * mangle the cursor.
1684 xfs_agblock_t xxbno;
1687 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1690 XFS_WANT_CORRUPTED_GOTO(
1691 i == 1 && xxbno == ltbno && xxlen == ltlen,
1696 * Update remaining by-block entry to the new, joined block.
1699 nlen = len + ltlen + gtlen;
1700 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1704 * Have only a left contiguous neighbor.
1705 * Merge it together with the new freespace.
1707 else if (haveleft) {
1709 * Delete the old by-size entry on the left.
1711 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1713 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1714 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1716 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1718 * Back up the by-block cursor to the left neighbor, and
1719 * update its length.
1721 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1723 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1726 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1730 * Have only a right contiguous neighbor.
1731 * Merge it together with the new freespace.
1733 else if (haveright) {
1735 * Delete the old by-size entry on the right.
1737 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1739 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1740 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1742 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1744 * Update the starting block and length of the right
1745 * neighbor in the by-block tree.
1749 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1753 * No contiguous neighbors.
1754 * Insert the new freespace into the by-block tree.
1759 if ((error = xfs_btree_insert(bno_cur, &i)))
1761 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1763 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1766 * In all cases we need to insert the new freespace in the by-size tree.
1768 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1770 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1771 if ((error = xfs_btree_insert(cnt_cur, &i)))
1773 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1774 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1777 * Update the freespace totals in the ag and superblock.
1781 xfs_perag_t *pag; /* per allocation group data */
1783 agf = XFS_BUF_TO_AGF(agbp);
1784 pag = &mp->m_perag[agno];
1785 be32_add_cpu(&agf->agf_freeblks, len);
1786 xfs_trans_agblocks_delta(tp, len);
1787 pag->pagf_freeblks += len;
1788 XFS_WANT_CORRUPTED_GOTO(
1789 be32_to_cpu(agf->agf_freeblks) <=
1790 be32_to_cpu(agf->agf_length),
1792 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1793 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1795 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1796 XFS_STATS_INC(xs_freex);
1797 XFS_STATS_ADD(xs_freeb, len);
1799 TRACE_FREE(haveleft ?
1800 (haveright ? "both" : "left") :
1801 (haveright ? "right" : "none"),
1802 agno, bno, len, isfl);
1805 * Since blocks move to the free list without the coordination
1806 * used in xfs_bmap_finish, we can't allow block to be available
1807 * for reallocation and non-transaction writing (user data)
1808 * until we know that the transaction that moved it to the free
1809 * list is permanently on disk. We track the blocks by declaring
1810 * these blocks as "busy"; the busy list is maintained on a per-ag
1811 * basis and each transaction records which entries should be removed
1812 * when the iclog commits to disk. If a busy block is allocated,
1813 * the iclog is pushed up to the LSN that freed the block.
1815 xfs_alloc_mark_busy(tp, agno, bno, len);
1819 TRACE_FREE("error", agno, bno, len, isfl);
1821 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1823 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1828 * Visible (exported) allocation/free functions.
1829 * Some of these are used just by xfs_alloc_btree.c and this file.
1833 * Compute and fill in value of m_ag_maxlevels.
1836 xfs_alloc_compute_maxlevels(
1837 xfs_mount_t *mp) /* file system mount structure */
1845 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1846 minleafrecs = mp->m_alloc_mnr[0];
1847 minnoderecs = mp->m_alloc_mnr[1];
1848 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1849 for (level = 1; maxblocks > 1; level++)
1850 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1851 mp->m_ag_maxlevels = level;
1855 * Decide whether to use this allocation group for this allocation.
1856 * If so, fix up the btree freelist's size.
1858 STATIC int /* error */
1859 xfs_alloc_fix_freelist(
1860 xfs_alloc_arg_t *args, /* allocation argument structure */
1861 int flags) /* XFS_ALLOC_FLAG_... */
1863 xfs_buf_t *agbp; /* agf buffer pointer */
1864 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1865 xfs_buf_t *agflbp;/* agfl buffer pointer */
1866 xfs_agblock_t bno; /* freelist block */
1867 xfs_extlen_t delta; /* new blocks needed in freelist */
1868 int error; /* error result code */
1869 xfs_extlen_t longest;/* longest extent in allocation group */
1870 xfs_mount_t *mp; /* file system mount point structure */
1871 xfs_extlen_t need; /* total blocks needed in freelist */
1872 xfs_perag_t *pag; /* per-ag information structure */
1873 xfs_alloc_arg_t targs; /* local allocation arguments */
1874 xfs_trans_t *tp; /* transaction pointer */
1880 if (!pag->pagf_init) {
1881 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1884 if (!pag->pagf_init) {
1885 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1886 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1894 * If this is a metadata preferred pag and we are user data
1895 * then try somewhere else if we are not being asked to
1896 * try harder at this point
1898 if (pag->pagf_metadata && args->userdata &&
1899 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1900 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1905 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1906 need = XFS_MIN_FREELIST_PAG(pag, mp);
1907 delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
1909 * If it looks like there isn't a long enough extent, or enough
1910 * total blocks, reject it.
1912 longest = (pag->pagf_longest > delta) ?
1913 (pag->pagf_longest - delta) :
1914 (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
1915 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1917 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1918 need - args->total) < (int)args->minleft)) {
1920 xfs_trans_brelse(tp, agbp);
1927 * Get the a.g. freespace buffer.
1928 * Can fail if we're not blocking on locks, and it's held.
1931 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1935 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1936 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1942 * Figure out how many blocks we should have in the freelist.
1944 agf = XFS_BUF_TO_AGF(agbp);
1945 need = XFS_MIN_FREELIST(agf, mp);
1947 * If there isn't enough total or single-extent, reject it.
1949 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1950 delta = need > be32_to_cpu(agf->agf_flcount) ?
1951 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1952 longest = be32_to_cpu(agf->agf_longest);
1953 longest = (longest > delta) ? (longest - delta) :
1954 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1955 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1957 ((int)(be32_to_cpu(agf->agf_freeblks) +
1958 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1959 (int)args->minleft)) {
1960 xfs_trans_brelse(tp, agbp);
1966 * Make the freelist shorter if it's too long.
1968 while (be32_to_cpu(agf->agf_flcount) > need) {
1971 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1974 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1976 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1977 xfs_trans_binval(tp, bp);
1980 * Initialize the args structure.
1985 targs.agno = args->agno;
1986 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1987 targs.minalignslop = 0;
1988 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1989 targs.type = XFS_ALLOCTYPE_THIS_AG;
1991 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1994 * Make the freelist longer if it's too short.
1996 while (be32_to_cpu(agf->agf_flcount) < need) {
1998 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
2000 * Allocate as many blocks as possible at once.
2002 if ((error = xfs_alloc_ag_vextent(&targs))) {
2003 xfs_trans_brelse(tp, agflbp);
2007 * Stop if we run out. Won't happen if callers are obeying
2008 * the restrictions correctly. Can happen for free calls
2009 * on a completely full ag.
2011 if (targs.agbno == NULLAGBLOCK) {
2012 if (flags & XFS_ALLOC_FLAG_FREEING)
2014 xfs_trans_brelse(tp, agflbp);
2019 * Put each allocated block on the list.
2021 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2022 error = xfs_alloc_put_freelist(tp, agbp,
2028 xfs_trans_brelse(tp, agflbp);
2034 * Get a block from the freelist.
2035 * Returns with the buffer for the block gotten.
2038 xfs_alloc_get_freelist(
2039 xfs_trans_t *tp, /* transaction pointer */
2040 xfs_buf_t *agbp, /* buffer containing the agf structure */
2041 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2042 int btreeblk) /* destination is a AGF btree */
2044 xfs_agf_t *agf; /* a.g. freespace structure */
2045 xfs_agfl_t *agfl; /* a.g. freelist structure */
2046 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2047 xfs_agblock_t bno; /* block number returned */
2050 xfs_mount_t *mp; /* mount structure */
2051 xfs_perag_t *pag; /* per allocation group data */
2053 agf = XFS_BUF_TO_AGF(agbp);
2055 * Freelist is empty, give up.
2057 if (!agf->agf_flcount) {
2058 *bnop = NULLAGBLOCK;
2062 * Read the array of free blocks.
2065 if ((error = xfs_alloc_read_agfl(mp, tp,
2066 be32_to_cpu(agf->agf_seqno), &agflbp)))
2068 agfl = XFS_BUF_TO_AGFL(agflbp);
2070 * Get the block number and update the data structures.
2072 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2073 be32_add_cpu(&agf->agf_flfirst, 1);
2074 xfs_trans_brelse(tp, agflbp);
2075 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2076 agf->agf_flfirst = 0;
2077 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2078 be32_add_cpu(&agf->agf_flcount, -1);
2079 xfs_trans_agflist_delta(tp, -1);
2080 pag->pagf_flcount--;
2082 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2084 be32_add_cpu(&agf->agf_btreeblks, 1);
2085 pag->pagf_btreeblks++;
2086 logflags |= XFS_AGF_BTREEBLKS;
2089 TRACE_MODAGF(NULL, agf, logflags);
2090 xfs_alloc_log_agf(tp, agbp, logflags);
2094 * As blocks are freed, they are added to the per-ag busy list
2095 * and remain there until the freeing transaction is committed to
2096 * disk. Now that we have allocated blocks, this list must be
2097 * searched to see if a block is being reused. If one is, then
2098 * the freeing transaction must be pushed to disk NOW by forcing
2099 * to disk all iclogs up that transaction's LSN.
2101 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
2106 * Log the given fields from the agf structure.
2110 xfs_trans_t *tp, /* transaction pointer */
2111 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2112 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2114 int first; /* first byte offset */
2115 int last; /* last byte offset */
2116 static const short offsets[] = {
2117 offsetof(xfs_agf_t, agf_magicnum),
2118 offsetof(xfs_agf_t, agf_versionnum),
2119 offsetof(xfs_agf_t, agf_seqno),
2120 offsetof(xfs_agf_t, agf_length),
2121 offsetof(xfs_agf_t, agf_roots[0]),
2122 offsetof(xfs_agf_t, agf_levels[0]),
2123 offsetof(xfs_agf_t, agf_flfirst),
2124 offsetof(xfs_agf_t, agf_fllast),
2125 offsetof(xfs_agf_t, agf_flcount),
2126 offsetof(xfs_agf_t, agf_freeblks),
2127 offsetof(xfs_agf_t, agf_longest),
2128 offsetof(xfs_agf_t, agf_btreeblks),
2132 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2133 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2137 * Interface for inode allocation to force the pag data to be initialized.
2140 xfs_alloc_pagf_init(
2141 xfs_mount_t *mp, /* file system mount structure */
2142 xfs_trans_t *tp, /* transaction pointer */
2143 xfs_agnumber_t agno, /* allocation group number */
2144 int flags) /* XFS_ALLOC_FLAGS_... */
2149 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2152 xfs_trans_brelse(tp, bp);
2157 * Put the block on the freelist for the allocation group.
2160 xfs_alloc_put_freelist(
2161 xfs_trans_t *tp, /* transaction pointer */
2162 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2163 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2164 xfs_agblock_t bno, /* block being freed */
2165 int btreeblk) /* block came from a AGF btree */
2167 xfs_agf_t *agf; /* a.g. freespace structure */
2168 xfs_agfl_t *agfl; /* a.g. free block array */
2169 __be32 *blockp;/* pointer to array entry */
2172 xfs_mount_t *mp; /* mount structure */
2173 xfs_perag_t *pag; /* per allocation group data */
2175 agf = XFS_BUF_TO_AGF(agbp);
2178 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2179 be32_to_cpu(agf->agf_seqno), &agflbp)))
2181 agfl = XFS_BUF_TO_AGFL(agflbp);
2182 be32_add_cpu(&agf->agf_fllast, 1);
2183 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2184 agf->agf_fllast = 0;
2185 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2186 be32_add_cpu(&agf->agf_flcount, 1);
2187 xfs_trans_agflist_delta(tp, 1);
2188 pag->pagf_flcount++;
2190 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2192 be32_add_cpu(&agf->agf_btreeblks, -1);
2193 pag->pagf_btreeblks--;
2194 logflags |= XFS_AGF_BTREEBLKS;
2197 TRACE_MODAGF(NULL, agf, logflags);
2198 xfs_alloc_log_agf(tp, agbp, logflags);
2200 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2201 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2202 *blockp = cpu_to_be32(bno);
2203 TRACE_MODAGF(NULL, agf, logflags);
2204 xfs_alloc_log_agf(tp, agbp, logflags);
2205 xfs_trans_log_buf(tp, agflbp,
2206 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2207 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2208 sizeof(xfs_agblock_t) - 1));
2213 * Read in the allocation group header (free/alloc section).
2217 xfs_mount_t *mp, /* mount point structure */
2218 xfs_trans_t *tp, /* transaction pointer */
2219 xfs_agnumber_t agno, /* allocation group number */
2220 int flags, /* XFS_ALLOC_FLAG_... */
2221 xfs_buf_t **bpp) /* buffer for the ag freelist header */
2223 xfs_agf_t *agf; /* ag freelist header */
2224 int agf_ok; /* set if agf is consistent */
2225 xfs_buf_t *bp; /* return value */
2226 xfs_perag_t *pag; /* per allocation group data */
2229 ASSERT(agno != NULLAGNUMBER);
2230 error = xfs_trans_read_buf(
2231 mp, tp, mp->m_ddev_targp,
2232 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2233 XFS_FSS_TO_BB(mp, 1),
2234 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
2238 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
2244 * Validate the magic number of the agf block.
2246 agf = XFS_BUF_TO_AGF(bp);
2248 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2249 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2250 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2251 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2252 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2253 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
2254 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2255 XFS_RANDOM_ALLOC_READ_AGF))) {
2256 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2257 XFS_ERRLEVEL_LOW, mp, agf);
2258 xfs_trans_brelse(tp, bp);
2259 return XFS_ERROR(EFSCORRUPTED);
2261 pag = &mp->m_perag[agno];
2262 if (!pag->pagf_init) {
2263 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2264 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2265 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2266 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2267 pag->pagf_levels[XFS_BTNUM_BNOi] =
2268 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2269 pag->pagf_levels[XFS_BTNUM_CNTi] =
2270 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2271 spin_lock_init(&pag->pagb_lock);
2272 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2273 sizeof(xfs_perag_busy_t), KM_SLEEP);
2277 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2278 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2279 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2280 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2281 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2282 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2283 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2284 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2287 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
2293 * Allocate an extent (variable-size).
2294 * Depending on the allocation type, we either look in a single allocation
2295 * group or loop over the allocation groups to find the result.
2299 xfs_alloc_arg_t *args) /* allocation argument structure */
2301 xfs_agblock_t agsize; /* allocation group size */
2303 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2304 xfs_extlen_t minleft;/* minimum left value, temp copy */
2305 xfs_mount_t *mp; /* mount structure pointer */
2306 xfs_agnumber_t sagno; /* starting allocation group number */
2307 xfs_alloctype_t type; /* input allocation type */
2310 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2313 type = args->otype = args->type;
2314 args->agbno = NULLAGBLOCK;
2316 * Just fix this up, for the case where the last a.g. is shorter
2317 * (or there's only one a.g.) and the caller couldn't easily figure
2318 * that out (xfs_bmap_alloc).
2320 agsize = mp->m_sb.sb_agblocks;
2321 if (args->maxlen > agsize)
2322 args->maxlen = agsize;
2323 if (args->alignment == 0)
2324 args->alignment = 1;
2325 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2326 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2327 ASSERT(args->minlen <= args->maxlen);
2328 ASSERT(args->minlen <= agsize);
2329 ASSERT(args->mod < args->prod);
2330 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2331 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2332 args->minlen > args->maxlen || args->minlen > agsize ||
2333 args->mod >= args->prod) {
2334 args->fsbno = NULLFSBLOCK;
2335 TRACE_ALLOC("badargs", args);
2338 minleft = args->minleft;
2341 case XFS_ALLOCTYPE_THIS_AG:
2342 case XFS_ALLOCTYPE_NEAR_BNO:
2343 case XFS_ALLOCTYPE_THIS_BNO:
2345 * These three force us into a single a.g.
2347 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2348 down_read(&mp->m_peraglock);
2349 args->pag = &mp->m_perag[args->agno];
2351 error = xfs_alloc_fix_freelist(args, 0);
2352 args->minleft = minleft;
2354 TRACE_ALLOC("nofix", args);
2358 up_read(&mp->m_peraglock);
2359 TRACE_ALLOC("noagbp", args);
2362 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2363 if ((error = xfs_alloc_ag_vextent(args)))
2365 up_read(&mp->m_peraglock);
2367 case XFS_ALLOCTYPE_START_BNO:
2369 * Try near allocation first, then anywhere-in-ag after
2370 * the first a.g. fails.
2372 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2373 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2374 args->fsbno = XFS_AGB_TO_FSB(mp,
2375 ((mp->m_agfrotor / rotorstep) %
2376 mp->m_sb.sb_agcount), 0);
2379 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2380 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2382 case XFS_ALLOCTYPE_ANY_AG:
2383 case XFS_ALLOCTYPE_START_AG:
2384 case XFS_ALLOCTYPE_FIRST_AG:
2386 * Rotate through the allocation groups looking for a winner.
2388 if (type == XFS_ALLOCTYPE_ANY_AG) {
2390 * Start with the last place we left off.
2392 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2393 mp->m_sb.sb_agcount;
2394 args->type = XFS_ALLOCTYPE_THIS_AG;
2395 flags = XFS_ALLOC_FLAG_TRYLOCK;
2396 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2398 * Start with allocation group given by bno.
2400 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2401 args->type = XFS_ALLOCTYPE_THIS_AG;
2405 if (type == XFS_ALLOCTYPE_START_AG)
2406 args->type = XFS_ALLOCTYPE_THIS_AG;
2408 * Start with the given allocation group.
2410 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2411 flags = XFS_ALLOC_FLAG_TRYLOCK;
2414 * Loop over allocation groups twice; first time with
2415 * trylock set, second time without.
2417 down_read(&mp->m_peraglock);
2419 args->pag = &mp->m_perag[args->agno];
2420 if (no_min) args->minleft = 0;
2421 error = xfs_alloc_fix_freelist(args, flags);
2422 args->minleft = minleft;
2424 TRACE_ALLOC("nofix", args);
2428 * If we get a buffer back then the allocation will fly.
2431 if ((error = xfs_alloc_ag_vextent(args)))
2435 TRACE_ALLOC("loopfailed", args);
2437 * Didn't work, figure out the next iteration.
2439 if (args->agno == sagno &&
2440 type == XFS_ALLOCTYPE_START_BNO)
2441 args->type = XFS_ALLOCTYPE_THIS_AG;
2443 * For the first allocation, we can try any AG to get
2444 * space. However, if we already have allocated a
2445 * block, we don't want to try AGs whose number is below
2446 * sagno. Otherwise, we may end up with out-of-order
2447 * locking of AGF, which might cause deadlock.
2449 if (++(args->agno) == mp->m_sb.sb_agcount) {
2450 if (args->firstblock != NULLFSBLOCK)
2456 * Reached the starting a.g., must either be done
2457 * or switch to non-trylock mode.
2459 if (args->agno == sagno) {
2461 args->agbno = NULLAGBLOCK;
2462 TRACE_ALLOC("allfailed", args);
2469 if (type == XFS_ALLOCTYPE_START_BNO) {
2470 args->agbno = XFS_FSB_TO_AGBNO(mp,
2472 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2477 up_read(&mp->m_peraglock);
2478 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2479 if (args->agno == sagno)
2480 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2481 (mp->m_sb.sb_agcount * rotorstep);
2483 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2484 (mp->m_sb.sb_agcount * rotorstep);
2491 if (args->agbno == NULLAGBLOCK)
2492 args->fsbno = NULLFSBLOCK;
2494 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2496 ASSERT(args->len >= args->minlen);
2497 ASSERT(args->len <= args->maxlen);
2498 ASSERT(args->agbno % args->alignment == 0);
2499 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2505 up_read(&mp->m_peraglock);
2511 * Just break up the extent address and hand off to xfs_free_ag_extent
2512 * after fixing up the freelist.
2516 xfs_trans_t *tp, /* transaction pointer */
2517 xfs_fsblock_t bno, /* starting block number of extent */
2518 xfs_extlen_t len) /* length of extent */
2520 xfs_alloc_arg_t args;
2524 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2526 args.mp = tp->t_mountp;
2527 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2528 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2529 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2530 down_read(&args.mp->m_peraglock);
2531 args.pag = &args.mp->m_perag[args.agno];
2532 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2535 ASSERT(args.agbp != NULL);
2536 ASSERT((args.agbno + len) <=
2537 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2539 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2541 up_read(&args.mp->m_peraglock);
2547 * AG Busy list management
2548 * The busy list contains block ranges that have been freed but whose
2549 * transactions have not yet hit disk. If any block listed in a busy
2550 * list is reused, the transaction that freed it must be forced to disk
2551 * before continuing to use the block.
2553 * xfs_alloc_mark_busy - add to the per-ag busy list
2554 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2557 xfs_alloc_mark_busy(xfs_trans_t *tp,
2558 xfs_agnumber_t agno,
2563 xfs_perag_busy_t *bsy;
2567 spin_lock(&mp->m_perag[agno].pagb_lock);
2569 /* search pagb_list for an open slot */
2570 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2571 n < XFS_PAGB_NUM_SLOTS;
2573 if (bsy->busy_tp == NULL) {
2578 if (n < XFS_PAGB_NUM_SLOTS) {
2579 bsy = &mp->m_perag[agno].pagb_list[n];
2580 mp->m_perag[agno].pagb_count++;
2581 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2582 bsy->busy_start = bno;
2583 bsy->busy_length = len;
2585 xfs_trans_add_busy(tp, agno, n);
2587 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2589 * The busy list is full! Since it is now not possible to
2590 * track the free block, make this a synchronous transaction
2591 * to insure that the block is not reused before this
2592 * transaction commits.
2594 xfs_trans_set_sync(tp);
2597 spin_unlock(&mp->m_perag[agno].pagb_lock);
2601 xfs_alloc_clear_busy(xfs_trans_t *tp,
2602 xfs_agnumber_t agno,
2606 xfs_perag_busy_t *list;
2610 spin_lock(&mp->m_perag[agno].pagb_lock);
2611 list = mp->m_perag[agno].pagb_list;
2613 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2614 if (list[idx].busy_tp == tp) {
2615 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2616 list[idx].busy_tp = NULL;
2617 mp->m_perag[agno].pagb_count--;
2619 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2622 spin_unlock(&mp->m_perag[agno].pagb_lock);
2627 * If we find the extent in the busy list, force the log out to get the
2628 * extent out of the busy list so the caller can use it straight away.
2631 xfs_alloc_search_busy(xfs_trans_t *tp,
2632 xfs_agnumber_t agno,
2637 xfs_perag_busy_t *bsy;
2638 xfs_agblock_t uend, bend;
2644 spin_lock(&mp->m_perag[agno].pagb_lock);
2645 cnt = mp->m_perag[agno].pagb_count;
2647 uend = bno + len - 1;
2649 /* search pagb_list for this slot, skipping open slots */
2650 for (bsy = mp->m_perag[agno].pagb_list; cnt; bsy++) {
2653 * (start1,length1) within (start2, length2)
2655 if (bsy->busy_tp != NULL) {
2656 bend = bsy->busy_start + bsy->busy_length - 1;
2657 if ((bno > bend) || (uend < bsy->busy_start)) {
2660 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
2661 "found1", agno, bno, len, tp);
2668 * If a block was found, force the log through the LSN of the
2669 * transaction that freed the block
2672 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, tp);
2673 lsn = bsy->busy_tp->t_commit_lsn;
2674 spin_unlock(&mp->m_perag[agno].pagb_lock);
2675 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2677 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, tp);
2678 spin_unlock(&mp->m_perag[agno].pagb_lock);