tags: Add the ability to make tags for all archs using "all"
[firewire-audio.git] / fs / xfs / xfs_alloc.c
blob275b1f4f9430d5c214fba1703b3724b0de631f8a
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.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"
41 #include "xfs_trace.h"
44 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
46 #define XFSA_FIXUP_BNO_OK 1
47 #define XFSA_FIXUP_CNT_OK 2
49 STATIC void
50 xfs_alloc_search_busy(xfs_trans_t *tp,
51 xfs_agnumber_t agno,
52 xfs_agblock_t bno,
53 xfs_extlen_t len);
56 * Prototypes for per-ag allocation routines
59 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
60 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
61 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
62 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
63 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
66 * Internal functions.
70 * Lookup the record equal to [bno, len] in the btree given by cur.
72 STATIC int /* error */
73 xfs_alloc_lookup_eq(
74 struct xfs_btree_cur *cur, /* btree cursor */
75 xfs_agblock_t bno, /* starting block of extent */
76 xfs_extlen_t len, /* length of extent */
77 int *stat) /* success/failure */
79 cur->bc_rec.a.ar_startblock = bno;
80 cur->bc_rec.a.ar_blockcount = len;
81 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
85 * Lookup the first record greater than or equal to [bno, len]
86 * in the btree given by cur.
88 STATIC int /* error */
89 xfs_alloc_lookup_ge(
90 struct xfs_btree_cur *cur, /* btree cursor */
91 xfs_agblock_t bno, /* starting block of extent */
92 xfs_extlen_t len, /* length of extent */
93 int *stat) /* success/failure */
95 cur->bc_rec.a.ar_startblock = bno;
96 cur->bc_rec.a.ar_blockcount = len;
97 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
101 * Lookup the first record less than or equal to [bno, len]
102 * in the btree given by cur.
104 STATIC int /* error */
105 xfs_alloc_lookup_le(
106 struct xfs_btree_cur *cur, /* btree cursor */
107 xfs_agblock_t bno, /* starting block of extent */
108 xfs_extlen_t len, /* length of extent */
109 int *stat) /* success/failure */
111 cur->bc_rec.a.ar_startblock = bno;
112 cur->bc_rec.a.ar_blockcount = len;
113 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
117 * Update the record referred to by cur to the value given
118 * by [bno, len].
119 * This either works (return 0) or gets an EFSCORRUPTED error.
121 STATIC int /* error */
122 xfs_alloc_update(
123 struct xfs_btree_cur *cur, /* btree cursor */
124 xfs_agblock_t bno, /* starting block of extent */
125 xfs_extlen_t len) /* length of extent */
127 union xfs_btree_rec rec;
129 rec.alloc.ar_startblock = cpu_to_be32(bno);
130 rec.alloc.ar_blockcount = cpu_to_be32(len);
131 return xfs_btree_update(cur, &rec);
135 * Get the data from the pointed-to record.
137 STATIC int /* error */
138 xfs_alloc_get_rec(
139 struct xfs_btree_cur *cur, /* btree cursor */
140 xfs_agblock_t *bno, /* output: starting block of extent */
141 xfs_extlen_t *len, /* output: length of extent */
142 int *stat) /* output: success/failure */
144 union xfs_btree_rec *rec;
145 int error;
147 error = xfs_btree_get_rec(cur, &rec, stat);
148 if (!error && *stat == 1) {
149 *bno = be32_to_cpu(rec->alloc.ar_startblock);
150 *len = be32_to_cpu(rec->alloc.ar_blockcount);
152 return error;
156 * Compute aligned version of the found extent.
157 * Takes alignment and min length into account.
159 STATIC void
160 xfs_alloc_compute_aligned(
161 xfs_agblock_t foundbno, /* starting block in found extent */
162 xfs_extlen_t foundlen, /* length in found extent */
163 xfs_extlen_t alignment, /* alignment for allocation */
164 xfs_extlen_t minlen, /* minimum length for allocation */
165 xfs_agblock_t *resbno, /* result block number */
166 xfs_extlen_t *reslen) /* result length */
168 xfs_agblock_t bno;
169 xfs_extlen_t diff;
170 xfs_extlen_t len;
172 if (alignment > 1 && foundlen >= minlen) {
173 bno = roundup(foundbno, alignment);
174 diff = bno - foundbno;
175 len = diff >= foundlen ? 0 : foundlen - diff;
176 } else {
177 bno = foundbno;
178 len = foundlen;
180 *resbno = bno;
181 *reslen = len;
185 * Compute best start block and diff for "near" allocations.
186 * freelen >= wantlen already checked by caller.
188 STATIC xfs_extlen_t /* difference value (absolute) */
189 xfs_alloc_compute_diff(
190 xfs_agblock_t wantbno, /* target starting block */
191 xfs_extlen_t wantlen, /* target length */
192 xfs_extlen_t alignment, /* target alignment */
193 xfs_agblock_t freebno, /* freespace's starting block */
194 xfs_extlen_t freelen, /* freespace's length */
195 xfs_agblock_t *newbnop) /* result: best start block from free */
197 xfs_agblock_t freeend; /* end of freespace extent */
198 xfs_agblock_t newbno1; /* return block number */
199 xfs_agblock_t newbno2; /* other new block number */
200 xfs_extlen_t newlen1=0; /* length with newbno1 */
201 xfs_extlen_t newlen2=0; /* length with newbno2 */
202 xfs_agblock_t wantend; /* end of target extent */
204 ASSERT(freelen >= wantlen);
205 freeend = freebno + freelen;
206 wantend = wantbno + wantlen;
207 if (freebno >= wantbno) {
208 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
209 newbno1 = NULLAGBLOCK;
210 } else if (freeend >= wantend && alignment > 1) {
211 newbno1 = roundup(wantbno, alignment);
212 newbno2 = newbno1 - alignment;
213 if (newbno1 >= freeend)
214 newbno1 = NULLAGBLOCK;
215 else
216 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
217 if (newbno2 < freebno)
218 newbno2 = NULLAGBLOCK;
219 else
220 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
221 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
222 if (newlen1 < newlen2 ||
223 (newlen1 == newlen2 &&
224 XFS_ABSDIFF(newbno1, wantbno) >
225 XFS_ABSDIFF(newbno2, wantbno)))
226 newbno1 = newbno2;
227 } else if (newbno2 != NULLAGBLOCK)
228 newbno1 = newbno2;
229 } else if (freeend >= wantend) {
230 newbno1 = wantbno;
231 } else if (alignment > 1) {
232 newbno1 = roundup(freeend - wantlen, alignment);
233 if (newbno1 > freeend - wantlen &&
234 newbno1 - alignment >= freebno)
235 newbno1 -= alignment;
236 else if (newbno1 >= freeend)
237 newbno1 = NULLAGBLOCK;
238 } else
239 newbno1 = freeend - wantlen;
240 *newbnop = newbno1;
241 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
245 * Fix up the length, based on mod and prod.
246 * len should be k * prod + mod for some k.
247 * If len is too small it is returned unchanged.
248 * If len hits maxlen it is left alone.
250 STATIC void
251 xfs_alloc_fix_len(
252 xfs_alloc_arg_t *args) /* allocation argument structure */
254 xfs_extlen_t k;
255 xfs_extlen_t rlen;
257 ASSERT(args->mod < args->prod);
258 rlen = args->len;
259 ASSERT(rlen >= args->minlen);
260 ASSERT(rlen <= args->maxlen);
261 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
262 (args->mod == 0 && rlen < args->prod))
263 return;
264 k = rlen % args->prod;
265 if (k == args->mod)
266 return;
267 if (k > args->mod) {
268 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
269 return;
270 } else {
271 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
272 (int)args->minlen)
273 return;
275 ASSERT(rlen >= args->minlen);
276 ASSERT(rlen <= args->maxlen);
277 args->len = rlen;
281 * Fix up length if there is too little space left in the a.g.
282 * Return 1 if ok, 0 if too little, should give up.
284 STATIC int
285 xfs_alloc_fix_minleft(
286 xfs_alloc_arg_t *args) /* allocation argument structure */
288 xfs_agf_t *agf; /* a.g. freelist header */
289 int diff; /* free space difference */
291 if (args->minleft == 0)
292 return 1;
293 agf = XFS_BUF_TO_AGF(args->agbp);
294 diff = be32_to_cpu(agf->agf_freeblks)
295 + be32_to_cpu(agf->agf_flcount)
296 - args->len - args->minleft;
297 if (diff >= 0)
298 return 1;
299 args->len += diff; /* shrink the allocated space */
300 if (args->len >= args->minlen)
301 return 1;
302 args->agbno = NULLAGBLOCK;
303 return 0;
307 * Update the two btrees, logically removing from freespace the extent
308 * starting at rbno, rlen blocks. The extent is contained within the
309 * actual (current) free extent fbno for flen blocks.
310 * Flags are passed in indicating whether the cursors are set to the
311 * relevant records.
313 STATIC int /* error code */
314 xfs_alloc_fixup_trees(
315 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
316 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
317 xfs_agblock_t fbno, /* starting block of free extent */
318 xfs_extlen_t flen, /* length of free extent */
319 xfs_agblock_t rbno, /* starting block of returned extent */
320 xfs_extlen_t rlen, /* length of returned extent */
321 int flags) /* flags, XFSA_FIXUP_... */
323 int error; /* error code */
324 int i; /* operation results */
325 xfs_agblock_t nfbno1; /* first new free startblock */
326 xfs_agblock_t nfbno2; /* second new free startblock */
327 xfs_extlen_t nflen1=0; /* first new free length */
328 xfs_extlen_t nflen2=0; /* second new free length */
331 * Look up the record in the by-size tree if necessary.
333 if (flags & XFSA_FIXUP_CNT_OK) {
334 #ifdef DEBUG
335 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
336 return error;
337 XFS_WANT_CORRUPTED_RETURN(
338 i == 1 && nfbno1 == fbno && nflen1 == flen);
339 #endif
340 } else {
341 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
342 return error;
343 XFS_WANT_CORRUPTED_RETURN(i == 1);
346 * Look up the record in the by-block tree if necessary.
348 if (flags & XFSA_FIXUP_BNO_OK) {
349 #ifdef DEBUG
350 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
351 return error;
352 XFS_WANT_CORRUPTED_RETURN(
353 i == 1 && nfbno1 == fbno && nflen1 == flen);
354 #endif
355 } else {
356 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
357 return error;
358 XFS_WANT_CORRUPTED_RETURN(i == 1);
361 #ifdef DEBUG
362 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
363 struct xfs_btree_block *bnoblock;
364 struct xfs_btree_block *cntblock;
366 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
367 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
369 XFS_WANT_CORRUPTED_RETURN(
370 bnoblock->bb_numrecs == cntblock->bb_numrecs);
372 #endif
375 * Deal with all four cases: the allocated record is contained
376 * within the freespace record, so we can have new freespace
377 * at either (or both) end, or no freespace remaining.
379 if (rbno == fbno && rlen == flen)
380 nfbno1 = nfbno2 = NULLAGBLOCK;
381 else if (rbno == fbno) {
382 nfbno1 = rbno + rlen;
383 nflen1 = flen - rlen;
384 nfbno2 = NULLAGBLOCK;
385 } else if (rbno + rlen == fbno + flen) {
386 nfbno1 = fbno;
387 nflen1 = flen - rlen;
388 nfbno2 = NULLAGBLOCK;
389 } else {
390 nfbno1 = fbno;
391 nflen1 = rbno - fbno;
392 nfbno2 = rbno + rlen;
393 nflen2 = (fbno + flen) - nfbno2;
396 * Delete the entry from the by-size btree.
398 if ((error = xfs_btree_delete(cnt_cur, &i)))
399 return error;
400 XFS_WANT_CORRUPTED_RETURN(i == 1);
402 * Add new by-size btree entry(s).
404 if (nfbno1 != NULLAGBLOCK) {
405 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
406 return error;
407 XFS_WANT_CORRUPTED_RETURN(i == 0);
408 if ((error = xfs_btree_insert(cnt_cur, &i)))
409 return error;
410 XFS_WANT_CORRUPTED_RETURN(i == 1);
412 if (nfbno2 != NULLAGBLOCK) {
413 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
414 return error;
415 XFS_WANT_CORRUPTED_RETURN(i == 0);
416 if ((error = xfs_btree_insert(cnt_cur, &i)))
417 return error;
418 XFS_WANT_CORRUPTED_RETURN(i == 1);
421 * Fix up the by-block btree entry(s).
423 if (nfbno1 == NULLAGBLOCK) {
425 * No remaining freespace, just delete the by-block tree entry.
427 if ((error = xfs_btree_delete(bno_cur, &i)))
428 return error;
429 XFS_WANT_CORRUPTED_RETURN(i == 1);
430 } else {
432 * Update the by-block entry to start later|be shorter.
434 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
435 return error;
437 if (nfbno2 != NULLAGBLOCK) {
439 * 2 resulting free entries, need to add one.
441 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
442 return error;
443 XFS_WANT_CORRUPTED_RETURN(i == 0);
444 if ((error = xfs_btree_insert(bno_cur, &i)))
445 return error;
446 XFS_WANT_CORRUPTED_RETURN(i == 1);
448 return 0;
452 * Read in the allocation group free block array.
454 STATIC int /* error */
455 xfs_alloc_read_agfl(
456 xfs_mount_t *mp, /* mount point structure */
457 xfs_trans_t *tp, /* transaction pointer */
458 xfs_agnumber_t agno, /* allocation group number */
459 xfs_buf_t **bpp) /* buffer for the ag free block array */
461 xfs_buf_t *bp; /* return value */
462 int error;
464 ASSERT(agno != NULLAGNUMBER);
465 error = xfs_trans_read_buf(
466 mp, tp, mp->m_ddev_targp,
467 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
468 XFS_FSS_TO_BB(mp, 1), 0, &bp);
469 if (error)
470 return error;
471 ASSERT(bp);
472 ASSERT(!XFS_BUF_GETERROR(bp));
473 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
474 *bpp = bp;
475 return 0;
479 * Allocation group level functions.
483 * Allocate a variable extent in the allocation group agno.
484 * Type and bno are used to determine where in the allocation group the
485 * extent will start.
486 * Extent's length (returned in *len) will be between minlen and maxlen,
487 * and of the form k * prod + mod unless there's nothing that large.
488 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
490 STATIC int /* error */
491 xfs_alloc_ag_vextent(
492 xfs_alloc_arg_t *args) /* argument structure for allocation */
494 int error=0;
496 ASSERT(args->minlen > 0);
497 ASSERT(args->maxlen > 0);
498 ASSERT(args->minlen <= args->maxlen);
499 ASSERT(args->mod < args->prod);
500 ASSERT(args->alignment > 0);
502 * Branch to correct routine based on the type.
504 args->wasfromfl = 0;
505 switch (args->type) {
506 case XFS_ALLOCTYPE_THIS_AG:
507 error = xfs_alloc_ag_vextent_size(args);
508 break;
509 case XFS_ALLOCTYPE_NEAR_BNO:
510 error = xfs_alloc_ag_vextent_near(args);
511 break;
512 case XFS_ALLOCTYPE_THIS_BNO:
513 error = xfs_alloc_ag_vextent_exact(args);
514 break;
515 default:
516 ASSERT(0);
517 /* NOTREACHED */
519 if (error)
520 return error;
522 * If the allocation worked, need to change the agf structure
523 * (and log it), and the superblock.
525 if (args->agbno != NULLAGBLOCK) {
526 xfs_agf_t *agf; /* allocation group freelist header */
527 long slen = (long)args->len;
529 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
530 ASSERT(!(args->wasfromfl) || !args->isfl);
531 ASSERT(args->agbno % args->alignment == 0);
532 if (!(args->wasfromfl)) {
534 agf = XFS_BUF_TO_AGF(args->agbp);
535 be32_add_cpu(&agf->agf_freeblks, -(args->len));
536 xfs_trans_agblocks_delta(args->tp,
537 -((long)(args->len)));
538 args->pag->pagf_freeblks -= args->len;
539 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
540 be32_to_cpu(agf->agf_length));
541 xfs_alloc_log_agf(args->tp, args->agbp,
542 XFS_AGF_FREEBLKS);
543 /* search the busylist for these blocks */
544 xfs_alloc_search_busy(args->tp, args->agno,
545 args->agbno, args->len);
547 if (!args->isfl)
548 xfs_trans_mod_sb(args->tp,
549 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
550 XFS_TRANS_SB_FDBLOCKS, -slen);
551 XFS_STATS_INC(xs_allocx);
552 XFS_STATS_ADD(xs_allocb, args->len);
554 return 0;
558 * Allocate a variable extent at exactly agno/bno.
559 * Extent's length (returned in *len) will be between minlen and maxlen,
560 * and of the form k * prod + mod unless there's nothing that large.
561 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
563 STATIC int /* error */
564 xfs_alloc_ag_vextent_exact(
565 xfs_alloc_arg_t *args) /* allocation argument structure */
567 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
568 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
569 xfs_agblock_t end; /* end of allocated extent */
570 int error;
571 xfs_agblock_t fbno; /* start block of found extent */
572 xfs_agblock_t fend; /* end block of found extent */
573 xfs_extlen_t flen; /* length of found extent */
574 int i; /* success/failure of operation */
575 xfs_agblock_t maxend; /* end of maximal extent */
576 xfs_agblock_t minend; /* end of minimal extent */
577 xfs_extlen_t rlen; /* length of returned extent */
579 ASSERT(args->alignment == 1);
581 * Allocate/initialize a cursor for the by-number freespace btree.
583 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
584 args->agno, XFS_BTNUM_BNO);
586 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
587 * Look for the closest free block <= bno, it must contain bno
588 * if any free block does.
590 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
591 goto error0;
592 if (!i) {
594 * Didn't find it, return null.
596 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
597 args->agbno = NULLAGBLOCK;
598 return 0;
601 * Grab the freespace record.
603 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
604 goto error0;
605 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
606 ASSERT(fbno <= args->agbno);
607 minend = args->agbno + args->minlen;
608 maxend = args->agbno + args->maxlen;
609 fend = fbno + flen;
611 * Give up if the freespace isn't long enough for the minimum request.
613 if (fend < minend) {
614 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
615 args->agbno = NULLAGBLOCK;
616 return 0;
619 * End of extent will be smaller of the freespace end and the
620 * maximal requested end.
622 end = XFS_AGBLOCK_MIN(fend, maxend);
624 * Fix the length according to mod and prod if given.
626 args->len = end - args->agbno;
627 xfs_alloc_fix_len(args);
628 if (!xfs_alloc_fix_minleft(args)) {
629 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
630 return 0;
632 rlen = args->len;
633 ASSERT(args->agbno + rlen <= fend);
634 end = args->agbno + rlen;
636 * We are allocating agbno for rlen [agbno .. end]
637 * Allocate/initialize a cursor for the by-size btree.
639 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
640 args->agno, XFS_BTNUM_CNT);
641 ASSERT(args->agbno + args->len <=
642 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
643 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
644 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
645 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
646 goto error0;
648 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
649 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
651 trace_xfs_alloc_exact_done(args);
652 args->wasfromfl = 0;
653 return 0;
655 error0:
656 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
657 trace_xfs_alloc_exact_error(args);
658 return error;
662 * Allocate a variable extent near bno in the allocation group agno.
663 * Extent's length (returned in len) will be between minlen and maxlen,
664 * and of the form k * prod + mod unless there's nothing that large.
665 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
667 STATIC int /* error */
668 xfs_alloc_ag_vextent_near(
669 xfs_alloc_arg_t *args) /* allocation argument structure */
671 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
672 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
673 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
674 xfs_agblock_t gtbno; /* start bno of right side entry */
675 xfs_agblock_t gtbnoa; /* aligned ... */
676 xfs_extlen_t gtdiff; /* difference to right side entry */
677 xfs_extlen_t gtlen; /* length of right side entry */
678 xfs_extlen_t gtlena; /* aligned ... */
679 xfs_agblock_t gtnew; /* useful start bno of right side */
680 int error; /* error code */
681 int i; /* result code, temporary */
682 int j; /* result code, temporary */
683 xfs_agblock_t ltbno; /* start bno of left side entry */
684 xfs_agblock_t ltbnoa; /* aligned ... */
685 xfs_extlen_t ltdiff; /* difference to left side entry */
686 /*REFERENCED*/
687 xfs_agblock_t ltend; /* end bno of left side entry */
688 xfs_extlen_t ltlen; /* length of left side entry */
689 xfs_extlen_t ltlena; /* aligned ... */
690 xfs_agblock_t ltnew; /* useful start bno of left side */
691 xfs_extlen_t rlen; /* length of returned extent */
692 #if defined(DEBUG) && defined(__KERNEL__)
694 * Randomly don't execute the first algorithm.
696 int dofirst; /* set to do first algorithm */
698 dofirst = random32() & 1;
699 #endif
701 * Get a cursor for the by-size btree.
703 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
704 args->agno, XFS_BTNUM_CNT);
705 ltlen = 0;
706 bno_cur_lt = bno_cur_gt = NULL;
708 * See if there are any free extents as big as maxlen.
710 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
711 goto error0;
713 * If none, then pick up the last entry in the tree unless the
714 * tree is empty.
716 if (!i) {
717 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
718 &ltlen, &i)))
719 goto error0;
720 if (i == 0 || ltlen == 0) {
721 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
722 return 0;
724 ASSERT(i == 1);
726 args->wasfromfl = 0;
728 * First algorithm.
729 * If the requested extent is large wrt the freespaces available
730 * in this a.g., then the cursor will be pointing to a btree entry
731 * near the right edge of the tree. If it's in the last btree leaf
732 * block, then we just examine all the entries in that block
733 * that are big enough, and pick the best one.
734 * This is written as a while loop so we can break out of it,
735 * but we never loop back to the top.
737 while (xfs_btree_islastblock(cnt_cur, 0)) {
738 xfs_extlen_t bdiff;
739 int besti=0;
740 xfs_extlen_t blen=0;
741 xfs_agblock_t bnew=0;
743 #if defined(DEBUG) && defined(__KERNEL__)
744 if (!dofirst)
745 break;
746 #endif
748 * Start from the entry that lookup found, sequence through
749 * all larger free blocks. If we're actually pointing at a
750 * record smaller than maxlen, go to the start of this block,
751 * and skip all those smaller than minlen.
753 if (ltlen || args->alignment > 1) {
754 cnt_cur->bc_ptrs[0] = 1;
755 do {
756 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
757 &ltlen, &i)))
758 goto error0;
759 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
760 if (ltlen >= args->minlen)
761 break;
762 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
763 goto error0;
764 } while (i);
765 ASSERT(ltlen >= args->minlen);
766 if (!i)
767 break;
769 i = cnt_cur->bc_ptrs[0];
770 for (j = 1, blen = 0, bdiff = 0;
771 !error && j && (blen < args->maxlen || bdiff > 0);
772 error = xfs_btree_increment(cnt_cur, 0, &j)) {
774 * For each entry, decide if it's better than
775 * the previous best entry.
777 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
778 goto error0;
779 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
780 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
781 args->minlen, &ltbnoa, &ltlena);
782 if (ltlena < args->minlen)
783 continue;
784 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
785 xfs_alloc_fix_len(args);
786 ASSERT(args->len >= args->minlen);
787 if (args->len < blen)
788 continue;
789 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
790 args->alignment, ltbno, ltlen, &ltnew);
791 if (ltnew != NULLAGBLOCK &&
792 (args->len > blen || ltdiff < bdiff)) {
793 bdiff = ltdiff;
794 bnew = ltnew;
795 blen = args->len;
796 besti = cnt_cur->bc_ptrs[0];
800 * It didn't work. We COULD be in a case where
801 * there's a good record somewhere, so try again.
803 if (blen == 0)
804 break;
806 * Point at the best entry, and retrieve it again.
808 cnt_cur->bc_ptrs[0] = besti;
809 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
810 goto error0;
811 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
812 ltend = ltbno + ltlen;
813 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
814 args->len = blen;
815 if (!xfs_alloc_fix_minleft(args)) {
816 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
817 trace_xfs_alloc_near_nominleft(args);
818 return 0;
820 blen = args->len;
822 * We are allocating starting at bnew for blen blocks.
824 args->agbno = bnew;
825 ASSERT(bnew >= ltbno);
826 ASSERT(bnew + blen <= ltend);
828 * Set up a cursor for the by-bno tree.
830 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
831 args->agbp, args->agno, XFS_BTNUM_BNO);
833 * Fix up the btree entries.
835 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
836 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
837 goto error0;
838 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
839 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
841 trace_xfs_alloc_near_first(args);
842 return 0;
845 * Second algorithm.
846 * Search in the by-bno tree to the left and to the right
847 * simultaneously, until in each case we find a space big enough,
848 * or run into the edge of the tree. When we run into the edge,
849 * we deallocate that cursor.
850 * If both searches succeed, we compare the two spaces and pick
851 * the better one.
852 * With alignment, it's possible for both to fail; the upper
853 * level algorithm that picks allocation groups for allocations
854 * is not supposed to do this.
857 * Allocate and initialize the cursor for the leftward search.
859 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
860 args->agno, XFS_BTNUM_BNO);
862 * Lookup <= bno to find the leftward search's starting point.
864 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
865 goto error0;
866 if (!i) {
868 * Didn't find anything; use this cursor for the rightward
869 * search.
871 bno_cur_gt = bno_cur_lt;
872 bno_cur_lt = NULL;
875 * Found something. Duplicate the cursor for the rightward search.
877 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
878 goto error0;
880 * Increment the cursor, so we will point at the entry just right
881 * of the leftward entry if any, or to the leftmost entry.
883 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
884 goto error0;
885 if (!i) {
887 * It failed, there are no rightward entries.
889 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
890 bno_cur_gt = NULL;
893 * Loop going left with the leftward cursor, right with the
894 * rightward cursor, until either both directions give up or
895 * we find an entry at least as big as minlen.
897 do {
898 if (bno_cur_lt) {
899 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
900 goto error0;
901 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
902 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
903 args->minlen, &ltbnoa, &ltlena);
904 if (ltlena >= args->minlen)
905 break;
906 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
907 goto error0;
908 if (!i) {
909 xfs_btree_del_cursor(bno_cur_lt,
910 XFS_BTREE_NOERROR);
911 bno_cur_lt = NULL;
914 if (bno_cur_gt) {
915 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
916 goto error0;
917 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
918 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
919 args->minlen, &gtbnoa, &gtlena);
920 if (gtlena >= args->minlen)
921 break;
922 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
923 goto error0;
924 if (!i) {
925 xfs_btree_del_cursor(bno_cur_gt,
926 XFS_BTREE_NOERROR);
927 bno_cur_gt = NULL;
930 } while (bno_cur_lt || bno_cur_gt);
932 * Got both cursors still active, need to find better entry.
934 if (bno_cur_lt && bno_cur_gt) {
936 * Left side is long enough, look for a right side entry.
938 if (ltlena >= args->minlen) {
940 * Fix up the length.
942 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
943 xfs_alloc_fix_len(args);
944 rlen = args->len;
945 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
946 args->alignment, ltbno, ltlen, &ltnew);
948 * Not perfect.
950 if (ltdiff) {
952 * Look until we find a better one, run out of
953 * space, or run off the end.
955 while (bno_cur_lt && bno_cur_gt) {
956 if ((error = xfs_alloc_get_rec(
957 bno_cur_gt, &gtbno,
958 &gtlen, &i)))
959 goto error0;
960 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
961 xfs_alloc_compute_aligned(gtbno, gtlen,
962 args->alignment, args->minlen,
963 &gtbnoa, &gtlena);
965 * The left one is clearly better.
967 if (gtbnoa >= args->agbno + ltdiff) {
968 xfs_btree_del_cursor(
969 bno_cur_gt,
970 XFS_BTREE_NOERROR);
971 bno_cur_gt = NULL;
972 break;
975 * If we reach a big enough entry,
976 * compare the two and pick the best.
978 if (gtlena >= args->minlen) {
979 args->len =
980 XFS_EXTLEN_MIN(gtlena,
981 args->maxlen);
982 xfs_alloc_fix_len(args);
983 rlen = args->len;
984 gtdiff = xfs_alloc_compute_diff(
985 args->agbno, rlen,
986 args->alignment,
987 gtbno, gtlen, &gtnew);
989 * Right side is better.
991 if (gtdiff < ltdiff) {
992 xfs_btree_del_cursor(
993 bno_cur_lt,
994 XFS_BTREE_NOERROR);
995 bno_cur_lt = NULL;
998 * Left side is better.
1000 else {
1001 xfs_btree_del_cursor(
1002 bno_cur_gt,
1003 XFS_BTREE_NOERROR);
1004 bno_cur_gt = NULL;
1006 break;
1009 * Fell off the right end.
1011 if ((error = xfs_btree_increment(
1012 bno_cur_gt, 0, &i)))
1013 goto error0;
1014 if (!i) {
1015 xfs_btree_del_cursor(
1016 bno_cur_gt,
1017 XFS_BTREE_NOERROR);
1018 bno_cur_gt = NULL;
1019 break;
1024 * The left side is perfect, trash the right side.
1026 else {
1027 xfs_btree_del_cursor(bno_cur_gt,
1028 XFS_BTREE_NOERROR);
1029 bno_cur_gt = NULL;
1033 * It's the right side that was found first, look left.
1035 else {
1037 * Fix up the length.
1039 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1040 xfs_alloc_fix_len(args);
1041 rlen = args->len;
1042 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1043 args->alignment, gtbno, gtlen, &gtnew);
1045 * Right side entry isn't perfect.
1047 if (gtdiff) {
1049 * Look until we find a better one, run out of
1050 * space, or run off the end.
1052 while (bno_cur_lt && bno_cur_gt) {
1053 if ((error = xfs_alloc_get_rec(
1054 bno_cur_lt, &ltbno,
1055 &ltlen, &i)))
1056 goto error0;
1057 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1058 xfs_alloc_compute_aligned(ltbno, ltlen,
1059 args->alignment, args->minlen,
1060 &ltbnoa, &ltlena);
1062 * The right one is clearly better.
1064 if (ltbnoa <= args->agbno - gtdiff) {
1065 xfs_btree_del_cursor(
1066 bno_cur_lt,
1067 XFS_BTREE_NOERROR);
1068 bno_cur_lt = NULL;
1069 break;
1072 * If we reach a big enough entry,
1073 * compare the two and pick the best.
1075 if (ltlena >= args->minlen) {
1076 args->len = XFS_EXTLEN_MIN(
1077 ltlena, args->maxlen);
1078 xfs_alloc_fix_len(args);
1079 rlen = args->len;
1080 ltdiff = xfs_alloc_compute_diff(
1081 args->agbno, rlen,
1082 args->alignment,
1083 ltbno, ltlen, &ltnew);
1085 * Left side is better.
1087 if (ltdiff < gtdiff) {
1088 xfs_btree_del_cursor(
1089 bno_cur_gt,
1090 XFS_BTREE_NOERROR);
1091 bno_cur_gt = NULL;
1094 * Right side is better.
1096 else {
1097 xfs_btree_del_cursor(
1098 bno_cur_lt,
1099 XFS_BTREE_NOERROR);
1100 bno_cur_lt = NULL;
1102 break;
1105 * Fell off the left end.
1107 if ((error = xfs_btree_decrement(
1108 bno_cur_lt, 0, &i)))
1109 goto error0;
1110 if (!i) {
1111 xfs_btree_del_cursor(bno_cur_lt,
1112 XFS_BTREE_NOERROR);
1113 bno_cur_lt = NULL;
1114 break;
1119 * The right side is perfect, trash the left side.
1121 else {
1122 xfs_btree_del_cursor(bno_cur_lt,
1123 XFS_BTREE_NOERROR);
1124 bno_cur_lt = NULL;
1129 * If we couldn't get anything, give up.
1131 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1132 trace_xfs_alloc_size_neither(args);
1133 args->agbno = NULLAGBLOCK;
1134 return 0;
1137 * At this point we have selected a freespace entry, either to the
1138 * left or to the right. If it's on the right, copy all the
1139 * useful variables to the "left" set so we only have one
1140 * copy of this code.
1142 if (bno_cur_gt) {
1143 bno_cur_lt = bno_cur_gt;
1144 bno_cur_gt = NULL;
1145 ltbno = gtbno;
1146 ltbnoa = gtbnoa;
1147 ltlen = gtlen;
1148 ltlena = gtlena;
1149 j = 1;
1150 } else
1151 j = 0;
1153 * Fix up the length and compute the useful address.
1155 ltend = ltbno + ltlen;
1156 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1157 xfs_alloc_fix_len(args);
1158 if (!xfs_alloc_fix_minleft(args)) {
1159 trace_xfs_alloc_near_nominleft(args);
1160 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1161 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1162 return 0;
1164 rlen = args->len;
1165 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1166 ltlen, &ltnew);
1167 ASSERT(ltnew >= ltbno);
1168 ASSERT(ltnew + rlen <= ltend);
1169 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1170 args->agbno = ltnew;
1171 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1172 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1173 goto error0;
1175 if (j)
1176 trace_xfs_alloc_near_greater(args);
1177 else
1178 trace_xfs_alloc_near_lesser(args);
1180 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1181 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1182 return 0;
1184 error0:
1185 trace_xfs_alloc_near_error(args);
1186 if (cnt_cur != NULL)
1187 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1188 if (bno_cur_lt != NULL)
1189 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1190 if (bno_cur_gt != NULL)
1191 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1192 return error;
1196 * Allocate a variable extent anywhere in the allocation group agno.
1197 * Extent's length (returned in len) will be between minlen and maxlen,
1198 * and of the form k * prod + mod unless there's nothing that large.
1199 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1201 STATIC int /* error */
1202 xfs_alloc_ag_vextent_size(
1203 xfs_alloc_arg_t *args) /* allocation argument structure */
1205 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1206 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1207 int error; /* error result */
1208 xfs_agblock_t fbno; /* start of found freespace */
1209 xfs_extlen_t flen; /* length of found freespace */
1210 int i; /* temp status variable */
1211 xfs_agblock_t rbno; /* returned block number */
1212 xfs_extlen_t rlen; /* length of returned extent */
1215 * Allocate and initialize a cursor for the by-size btree.
1217 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1218 args->agno, XFS_BTNUM_CNT);
1219 bno_cur = NULL;
1221 * Look for an entry >= maxlen+alignment-1 blocks.
1223 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1224 args->maxlen + args->alignment - 1, &i)))
1225 goto error0;
1227 * If none, then pick up the last entry in the tree unless the
1228 * tree is empty.
1230 if (!i) {
1231 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1232 &flen, &i)))
1233 goto error0;
1234 if (i == 0 || flen == 0) {
1235 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1236 trace_xfs_alloc_size_noentry(args);
1237 return 0;
1239 ASSERT(i == 1);
1242 * There's a freespace as big as maxlen+alignment-1, get it.
1244 else {
1245 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1246 goto error0;
1247 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1250 * In the first case above, we got the last entry in the
1251 * by-size btree. Now we check to see if the space hits maxlen
1252 * once aligned; if not, we search left for something better.
1253 * This can't happen in the second case above.
1255 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1256 &rbno, &rlen);
1257 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1258 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1259 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1260 if (rlen < args->maxlen) {
1261 xfs_agblock_t bestfbno;
1262 xfs_extlen_t bestflen;
1263 xfs_agblock_t bestrbno;
1264 xfs_extlen_t bestrlen;
1266 bestrlen = rlen;
1267 bestrbno = rbno;
1268 bestflen = flen;
1269 bestfbno = fbno;
1270 for (;;) {
1271 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1272 goto error0;
1273 if (i == 0)
1274 break;
1275 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1276 &i)))
1277 goto error0;
1278 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1279 if (flen < bestrlen)
1280 break;
1281 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1282 args->minlen, &rbno, &rlen);
1283 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1284 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1285 (rlen <= flen && rbno + rlen <= fbno + flen),
1286 error0);
1287 if (rlen > bestrlen) {
1288 bestrlen = rlen;
1289 bestrbno = rbno;
1290 bestflen = flen;
1291 bestfbno = fbno;
1292 if (rlen == args->maxlen)
1293 break;
1296 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1297 &i)))
1298 goto error0;
1299 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1300 rlen = bestrlen;
1301 rbno = bestrbno;
1302 flen = bestflen;
1303 fbno = bestfbno;
1305 args->wasfromfl = 0;
1307 * Fix up the length.
1309 args->len = rlen;
1310 xfs_alloc_fix_len(args);
1311 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1312 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1313 trace_xfs_alloc_size_nominleft(args);
1314 args->agbno = NULLAGBLOCK;
1315 return 0;
1317 rlen = args->len;
1318 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1320 * Allocate and initialize a cursor for the by-block tree.
1322 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1323 args->agno, XFS_BTNUM_BNO);
1324 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1325 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1326 goto error0;
1327 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1328 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1329 cnt_cur = bno_cur = NULL;
1330 args->len = rlen;
1331 args->agbno = rbno;
1332 XFS_WANT_CORRUPTED_GOTO(
1333 args->agbno + args->len <=
1334 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1335 error0);
1336 trace_xfs_alloc_size_done(args);
1337 return 0;
1339 error0:
1340 trace_xfs_alloc_size_error(args);
1341 if (cnt_cur)
1342 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1343 if (bno_cur)
1344 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1345 return error;
1349 * Deal with the case where only small freespaces remain.
1350 * Either return the contents of the last freespace record,
1351 * or allocate space from the freelist if there is nothing in the tree.
1353 STATIC int /* error */
1354 xfs_alloc_ag_vextent_small(
1355 xfs_alloc_arg_t *args, /* allocation argument structure */
1356 xfs_btree_cur_t *ccur, /* by-size cursor */
1357 xfs_agblock_t *fbnop, /* result block number */
1358 xfs_extlen_t *flenp, /* result length */
1359 int *stat) /* status: 0-freelist, 1-normal/none */
1361 int error;
1362 xfs_agblock_t fbno;
1363 xfs_extlen_t flen;
1364 int i;
1366 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1367 goto error0;
1368 if (i) {
1369 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1370 goto error0;
1371 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1374 * Nothing in the btree, try the freelist. Make sure
1375 * to respect minleft even when pulling from the
1376 * freelist.
1378 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1379 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1380 > args->minleft)) {
1381 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1382 if (error)
1383 goto error0;
1384 if (fbno != NULLAGBLOCK) {
1385 if (args->userdata) {
1386 xfs_buf_t *bp;
1388 bp = xfs_btree_get_bufs(args->mp, args->tp,
1389 args->agno, fbno, 0);
1390 xfs_trans_binval(args->tp, bp);
1392 args->len = 1;
1393 args->agbno = fbno;
1394 XFS_WANT_CORRUPTED_GOTO(
1395 args->agbno + args->len <=
1396 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1397 error0);
1398 args->wasfromfl = 1;
1399 trace_xfs_alloc_small_freelist(args);
1400 *stat = 0;
1401 return 0;
1404 * Nothing in the freelist.
1406 else
1407 flen = 0;
1410 * Can't allocate from the freelist for some reason.
1412 else {
1413 fbno = NULLAGBLOCK;
1414 flen = 0;
1417 * Can't do the allocation, give up.
1419 if (flen < args->minlen) {
1420 args->agbno = NULLAGBLOCK;
1421 trace_xfs_alloc_small_notenough(args);
1422 flen = 0;
1424 *fbnop = fbno;
1425 *flenp = flen;
1426 *stat = 1;
1427 trace_xfs_alloc_small_done(args);
1428 return 0;
1430 error0:
1431 trace_xfs_alloc_small_error(args);
1432 return error;
1436 * Free the extent starting at agno/bno for length.
1438 STATIC int /* error */
1439 xfs_free_ag_extent(
1440 xfs_trans_t *tp, /* transaction pointer */
1441 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1442 xfs_agnumber_t agno, /* allocation group number */
1443 xfs_agblock_t bno, /* starting block number */
1444 xfs_extlen_t len, /* length of extent */
1445 int isfl) /* set if is freelist blocks - no sb acctg */
1447 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1448 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1449 int error; /* error return value */
1450 xfs_agblock_t gtbno; /* start of right neighbor block */
1451 xfs_extlen_t gtlen; /* length of right neighbor block */
1452 int haveleft; /* have a left neighbor block */
1453 int haveright; /* have a right neighbor block */
1454 int i; /* temp, result code */
1455 xfs_agblock_t ltbno; /* start of left neighbor block */
1456 xfs_extlen_t ltlen; /* length of left neighbor block */
1457 xfs_mount_t *mp; /* mount point struct for filesystem */
1458 xfs_agblock_t nbno; /* new starting block of freespace */
1459 xfs_extlen_t nlen; /* new length of freespace */
1461 mp = tp->t_mountp;
1463 * Allocate and initialize a cursor for the by-block btree.
1465 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1466 cnt_cur = NULL;
1468 * Look for a neighboring block on the left (lower block numbers)
1469 * that is contiguous with this space.
1471 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1472 goto error0;
1473 if (haveleft) {
1475 * There is a block to our left.
1477 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1478 goto error0;
1479 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1481 * It's not contiguous, though.
1483 if (ltbno + ltlen < bno)
1484 haveleft = 0;
1485 else {
1487 * If this failure happens the request to free this
1488 * space was invalid, it's (partly) already free.
1489 * Very bad.
1491 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1495 * Look for a neighboring block on the right (higher block numbers)
1496 * that is contiguous with this space.
1498 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1499 goto error0;
1500 if (haveright) {
1502 * There is a block to our right.
1504 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1505 goto error0;
1506 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1508 * It's not contiguous, though.
1510 if (bno + len < gtbno)
1511 haveright = 0;
1512 else {
1514 * If this failure happens the request to free this
1515 * space was invalid, it's (partly) already free.
1516 * Very bad.
1518 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1522 * Now allocate and initialize a cursor for the by-size tree.
1524 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1526 * Have both left and right contiguous neighbors.
1527 * Merge all three into a single free block.
1529 if (haveleft && haveright) {
1531 * Delete the old by-size entry on the left.
1533 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1534 goto error0;
1535 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1536 if ((error = xfs_btree_delete(cnt_cur, &i)))
1537 goto error0;
1538 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1540 * Delete the old by-size entry on the right.
1542 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1543 goto error0;
1544 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1545 if ((error = xfs_btree_delete(cnt_cur, &i)))
1546 goto error0;
1547 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1549 * Delete the old by-block entry for the right block.
1551 if ((error = xfs_btree_delete(bno_cur, &i)))
1552 goto error0;
1553 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1555 * Move the by-block cursor back to the left neighbor.
1557 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1558 goto error0;
1559 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1560 #ifdef DEBUG
1562 * Check that this is the right record: delete didn't
1563 * mangle the cursor.
1566 xfs_agblock_t xxbno;
1567 xfs_extlen_t xxlen;
1569 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1570 &i)))
1571 goto error0;
1572 XFS_WANT_CORRUPTED_GOTO(
1573 i == 1 && xxbno == ltbno && xxlen == ltlen,
1574 error0);
1576 #endif
1578 * Update remaining by-block entry to the new, joined block.
1580 nbno = ltbno;
1581 nlen = len + ltlen + gtlen;
1582 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1583 goto error0;
1586 * Have only a left contiguous neighbor.
1587 * Merge it together with the new freespace.
1589 else if (haveleft) {
1591 * Delete the old by-size entry on the left.
1593 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1594 goto error0;
1595 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1596 if ((error = xfs_btree_delete(cnt_cur, &i)))
1597 goto error0;
1598 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1600 * Back up the by-block cursor to the left neighbor, and
1601 * update its length.
1603 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1604 goto error0;
1605 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1606 nbno = ltbno;
1607 nlen = len + ltlen;
1608 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1609 goto error0;
1612 * Have only a right contiguous neighbor.
1613 * Merge it together with the new freespace.
1615 else if (haveright) {
1617 * Delete the old by-size entry on the right.
1619 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1620 goto error0;
1621 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1622 if ((error = xfs_btree_delete(cnt_cur, &i)))
1623 goto error0;
1624 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1626 * Update the starting block and length of the right
1627 * neighbor in the by-block tree.
1629 nbno = bno;
1630 nlen = len + gtlen;
1631 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1632 goto error0;
1635 * No contiguous neighbors.
1636 * Insert the new freespace into the by-block tree.
1638 else {
1639 nbno = bno;
1640 nlen = len;
1641 if ((error = xfs_btree_insert(bno_cur, &i)))
1642 goto error0;
1643 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1645 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1646 bno_cur = NULL;
1648 * In all cases we need to insert the new freespace in the by-size tree.
1650 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1651 goto error0;
1652 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1653 if ((error = xfs_btree_insert(cnt_cur, &i)))
1654 goto error0;
1655 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1656 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1657 cnt_cur = NULL;
1659 * Update the freespace totals in the ag and superblock.
1662 xfs_agf_t *agf;
1663 xfs_perag_t *pag; /* per allocation group data */
1665 agf = XFS_BUF_TO_AGF(agbp);
1666 pag = &mp->m_perag[agno];
1667 be32_add_cpu(&agf->agf_freeblks, len);
1668 xfs_trans_agblocks_delta(tp, len);
1669 pag->pagf_freeblks += len;
1670 XFS_WANT_CORRUPTED_GOTO(
1671 be32_to_cpu(agf->agf_freeblks) <=
1672 be32_to_cpu(agf->agf_length),
1673 error0);
1674 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1675 if (!isfl)
1676 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1677 XFS_STATS_INC(xs_freex);
1678 XFS_STATS_ADD(xs_freeb, len);
1681 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
1684 * Since blocks move to the free list without the coordination
1685 * used in xfs_bmap_finish, we can't allow block to be available
1686 * for reallocation and non-transaction writing (user data)
1687 * until we know that the transaction that moved it to the free
1688 * list is permanently on disk. We track the blocks by declaring
1689 * these blocks as "busy"; the busy list is maintained on a per-ag
1690 * basis and each transaction records which entries should be removed
1691 * when the iclog commits to disk. If a busy block is allocated,
1692 * the iclog is pushed up to the LSN that freed the block.
1694 xfs_alloc_mark_busy(tp, agno, bno, len);
1695 return 0;
1697 error0:
1698 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
1699 if (bno_cur)
1700 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1701 if (cnt_cur)
1702 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1703 return error;
1707 * Visible (exported) allocation/free functions.
1708 * Some of these are used just by xfs_alloc_btree.c and this file.
1712 * Compute and fill in value of m_ag_maxlevels.
1714 void
1715 xfs_alloc_compute_maxlevels(
1716 xfs_mount_t *mp) /* file system mount structure */
1718 int level;
1719 uint maxblocks;
1720 uint maxleafents;
1721 int minleafrecs;
1722 int minnoderecs;
1724 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1725 minleafrecs = mp->m_alloc_mnr[0];
1726 minnoderecs = mp->m_alloc_mnr[1];
1727 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1728 for (level = 1; maxblocks > 1; level++)
1729 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1730 mp->m_ag_maxlevels = level;
1734 * Find the length of the longest extent in an AG.
1736 xfs_extlen_t
1737 xfs_alloc_longest_free_extent(
1738 struct xfs_mount *mp,
1739 struct xfs_perag *pag)
1741 xfs_extlen_t need, delta = 0;
1743 need = XFS_MIN_FREELIST_PAG(pag, mp);
1744 if (need > pag->pagf_flcount)
1745 delta = need - pag->pagf_flcount;
1747 if (pag->pagf_longest > delta)
1748 return pag->pagf_longest - delta;
1749 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1753 * Decide whether to use this allocation group for this allocation.
1754 * If so, fix up the btree freelist's size.
1756 STATIC int /* error */
1757 xfs_alloc_fix_freelist(
1758 xfs_alloc_arg_t *args, /* allocation argument structure */
1759 int flags) /* XFS_ALLOC_FLAG_... */
1761 xfs_buf_t *agbp; /* agf buffer pointer */
1762 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1763 xfs_buf_t *agflbp;/* agfl buffer pointer */
1764 xfs_agblock_t bno; /* freelist block */
1765 xfs_extlen_t delta; /* new blocks needed in freelist */
1766 int error; /* error result code */
1767 xfs_extlen_t longest;/* longest extent in allocation group */
1768 xfs_mount_t *mp; /* file system mount point structure */
1769 xfs_extlen_t need; /* total blocks needed in freelist */
1770 xfs_perag_t *pag; /* per-ag information structure */
1771 xfs_alloc_arg_t targs; /* local allocation arguments */
1772 xfs_trans_t *tp; /* transaction pointer */
1774 mp = args->mp;
1776 pag = args->pag;
1777 tp = args->tp;
1778 if (!pag->pagf_init) {
1779 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1780 &agbp)))
1781 return error;
1782 if (!pag->pagf_init) {
1783 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1784 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1785 args->agbp = NULL;
1786 return 0;
1788 } else
1789 agbp = NULL;
1792 * If this is a metadata preferred pag and we are user data
1793 * then try somewhere else if we are not being asked to
1794 * try harder at this point
1796 if (pag->pagf_metadata && args->userdata &&
1797 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1798 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1799 args->agbp = NULL;
1800 return 0;
1803 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1805 * If it looks like there isn't a long enough extent, or enough
1806 * total blocks, reject it.
1808 need = XFS_MIN_FREELIST_PAG(pag, mp);
1809 longest = xfs_alloc_longest_free_extent(mp, pag);
1810 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1811 longest ||
1812 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1813 need - args->total) < (int)args->minleft)) {
1814 if (agbp)
1815 xfs_trans_brelse(tp, agbp);
1816 args->agbp = NULL;
1817 return 0;
1822 * Get the a.g. freespace buffer.
1823 * Can fail if we're not blocking on locks, and it's held.
1825 if (agbp == NULL) {
1826 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1827 &agbp)))
1828 return error;
1829 if (agbp == NULL) {
1830 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1831 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
1832 args->agbp = NULL;
1833 return 0;
1837 * Figure out how many blocks we should have in the freelist.
1839 agf = XFS_BUF_TO_AGF(agbp);
1840 need = XFS_MIN_FREELIST(agf, mp);
1842 * If there isn't enough total or single-extent, reject it.
1844 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1845 delta = need > be32_to_cpu(agf->agf_flcount) ?
1846 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1847 longest = be32_to_cpu(agf->agf_longest);
1848 longest = (longest > delta) ? (longest - delta) :
1849 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1850 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1851 longest ||
1852 ((int)(be32_to_cpu(agf->agf_freeblks) +
1853 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1854 (int)args->minleft)) {
1855 xfs_trans_brelse(tp, agbp);
1856 args->agbp = NULL;
1857 return 0;
1861 * Make the freelist shorter if it's too long.
1863 while (be32_to_cpu(agf->agf_flcount) > need) {
1864 xfs_buf_t *bp;
1866 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1867 if (error)
1868 return error;
1869 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1870 return error;
1871 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1872 xfs_trans_binval(tp, bp);
1875 * Initialize the args structure.
1877 targs.tp = tp;
1878 targs.mp = mp;
1879 targs.agbp = agbp;
1880 targs.agno = args->agno;
1881 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1882 targs.minalignslop = 0;
1883 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1884 targs.type = XFS_ALLOCTYPE_THIS_AG;
1885 targs.pag = pag;
1886 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1887 return error;
1889 * Make the freelist longer if it's too short.
1891 while (be32_to_cpu(agf->agf_flcount) < need) {
1892 targs.agbno = 0;
1893 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
1895 * Allocate as many blocks as possible at once.
1897 if ((error = xfs_alloc_ag_vextent(&targs))) {
1898 xfs_trans_brelse(tp, agflbp);
1899 return error;
1902 * Stop if we run out. Won't happen if callers are obeying
1903 * the restrictions correctly. Can happen for free calls
1904 * on a completely full ag.
1906 if (targs.agbno == NULLAGBLOCK) {
1907 if (flags & XFS_ALLOC_FLAG_FREEING)
1908 break;
1909 xfs_trans_brelse(tp, agflbp);
1910 args->agbp = NULL;
1911 return 0;
1914 * Put each allocated block on the list.
1916 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
1917 error = xfs_alloc_put_freelist(tp, agbp,
1918 agflbp, bno, 0);
1919 if (error)
1920 return error;
1923 xfs_trans_brelse(tp, agflbp);
1924 args->agbp = agbp;
1925 return 0;
1929 * Get a block from the freelist.
1930 * Returns with the buffer for the block gotten.
1932 int /* error */
1933 xfs_alloc_get_freelist(
1934 xfs_trans_t *tp, /* transaction pointer */
1935 xfs_buf_t *agbp, /* buffer containing the agf structure */
1936 xfs_agblock_t *bnop, /* block address retrieved from freelist */
1937 int btreeblk) /* destination is a AGF btree */
1939 xfs_agf_t *agf; /* a.g. freespace structure */
1940 xfs_agfl_t *agfl; /* a.g. freelist structure */
1941 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
1942 xfs_agblock_t bno; /* block number returned */
1943 int error;
1944 int logflags;
1945 xfs_mount_t *mp; /* mount structure */
1946 xfs_perag_t *pag; /* per allocation group data */
1948 agf = XFS_BUF_TO_AGF(agbp);
1950 * Freelist is empty, give up.
1952 if (!agf->agf_flcount) {
1953 *bnop = NULLAGBLOCK;
1954 return 0;
1957 * Read the array of free blocks.
1959 mp = tp->t_mountp;
1960 if ((error = xfs_alloc_read_agfl(mp, tp,
1961 be32_to_cpu(agf->agf_seqno), &agflbp)))
1962 return error;
1963 agfl = XFS_BUF_TO_AGFL(agflbp);
1965 * Get the block number and update the data structures.
1967 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
1968 be32_add_cpu(&agf->agf_flfirst, 1);
1969 xfs_trans_brelse(tp, agflbp);
1970 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
1971 agf->agf_flfirst = 0;
1972 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
1973 be32_add_cpu(&agf->agf_flcount, -1);
1974 xfs_trans_agflist_delta(tp, -1);
1975 pag->pagf_flcount--;
1977 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
1978 if (btreeblk) {
1979 be32_add_cpu(&agf->agf_btreeblks, 1);
1980 pag->pagf_btreeblks++;
1981 logflags |= XFS_AGF_BTREEBLKS;
1984 xfs_alloc_log_agf(tp, agbp, logflags);
1985 *bnop = bno;
1988 * As blocks are freed, they are added to the per-ag busy list
1989 * and remain there until the freeing transaction is committed to
1990 * disk. Now that we have allocated blocks, this list must be
1991 * searched to see if a block is being reused. If one is, then
1992 * the freeing transaction must be pushed to disk NOW by forcing
1993 * to disk all iclogs up that transaction's LSN.
1995 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
1996 return 0;
2000 * Log the given fields from the agf structure.
2002 void
2003 xfs_alloc_log_agf(
2004 xfs_trans_t *tp, /* transaction pointer */
2005 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2006 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2008 int first; /* first byte offset */
2009 int last; /* last byte offset */
2010 static const short offsets[] = {
2011 offsetof(xfs_agf_t, agf_magicnum),
2012 offsetof(xfs_agf_t, agf_versionnum),
2013 offsetof(xfs_agf_t, agf_seqno),
2014 offsetof(xfs_agf_t, agf_length),
2015 offsetof(xfs_agf_t, agf_roots[0]),
2016 offsetof(xfs_agf_t, agf_levels[0]),
2017 offsetof(xfs_agf_t, agf_flfirst),
2018 offsetof(xfs_agf_t, agf_fllast),
2019 offsetof(xfs_agf_t, agf_flcount),
2020 offsetof(xfs_agf_t, agf_freeblks),
2021 offsetof(xfs_agf_t, agf_longest),
2022 offsetof(xfs_agf_t, agf_btreeblks),
2023 sizeof(xfs_agf_t)
2026 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2028 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2029 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2033 * Interface for inode allocation to force the pag data to be initialized.
2035 int /* error */
2036 xfs_alloc_pagf_init(
2037 xfs_mount_t *mp, /* file system mount structure */
2038 xfs_trans_t *tp, /* transaction pointer */
2039 xfs_agnumber_t agno, /* allocation group number */
2040 int flags) /* XFS_ALLOC_FLAGS_... */
2042 xfs_buf_t *bp;
2043 int error;
2045 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2046 return error;
2047 if (bp)
2048 xfs_trans_brelse(tp, bp);
2049 return 0;
2053 * Put the block on the freelist for the allocation group.
2055 int /* error */
2056 xfs_alloc_put_freelist(
2057 xfs_trans_t *tp, /* transaction pointer */
2058 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2059 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2060 xfs_agblock_t bno, /* block being freed */
2061 int btreeblk) /* block came from a AGF btree */
2063 xfs_agf_t *agf; /* a.g. freespace structure */
2064 xfs_agfl_t *agfl; /* a.g. free block array */
2065 __be32 *blockp;/* pointer to array entry */
2066 int error;
2067 int logflags;
2068 xfs_mount_t *mp; /* mount structure */
2069 xfs_perag_t *pag; /* per allocation group data */
2071 agf = XFS_BUF_TO_AGF(agbp);
2072 mp = tp->t_mountp;
2074 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2075 be32_to_cpu(agf->agf_seqno), &agflbp)))
2076 return error;
2077 agfl = XFS_BUF_TO_AGFL(agflbp);
2078 be32_add_cpu(&agf->agf_fllast, 1);
2079 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2080 agf->agf_fllast = 0;
2081 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
2082 be32_add_cpu(&agf->agf_flcount, 1);
2083 xfs_trans_agflist_delta(tp, 1);
2084 pag->pagf_flcount++;
2086 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2087 if (btreeblk) {
2088 be32_add_cpu(&agf->agf_btreeblks, -1);
2089 pag->pagf_btreeblks--;
2090 logflags |= XFS_AGF_BTREEBLKS;
2093 xfs_alloc_log_agf(tp, agbp, logflags);
2095 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2096 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
2097 *blockp = cpu_to_be32(bno);
2098 xfs_alloc_log_agf(tp, agbp, logflags);
2099 xfs_trans_log_buf(tp, agflbp,
2100 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2101 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2102 sizeof(xfs_agblock_t) - 1));
2103 return 0;
2107 * Read in the allocation group header (free/alloc section).
2109 int /* error */
2110 xfs_read_agf(
2111 struct xfs_mount *mp, /* mount point structure */
2112 struct xfs_trans *tp, /* transaction pointer */
2113 xfs_agnumber_t agno, /* allocation group number */
2114 int flags, /* XFS_BUF_ */
2115 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2117 struct xfs_agf *agf; /* ag freelist header */
2118 int agf_ok; /* set if agf is consistent */
2119 int error;
2121 ASSERT(agno != NULLAGNUMBER);
2122 error = xfs_trans_read_buf(
2123 mp, tp, mp->m_ddev_targp,
2124 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2125 XFS_FSS_TO_BB(mp, 1), flags, bpp);
2126 if (error)
2127 return error;
2128 if (!*bpp)
2129 return 0;
2131 ASSERT(!XFS_BUF_GETERROR(*bpp));
2132 agf = XFS_BUF_TO_AGF(*bpp);
2135 * Validate the magic number of the agf block.
2137 agf_ok =
2138 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2139 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2140 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2141 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2142 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2143 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
2144 be32_to_cpu(agf->agf_seqno) == agno;
2145 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2146 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2147 be32_to_cpu(agf->agf_length);
2148 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2149 XFS_RANDOM_ALLOC_READ_AGF))) {
2150 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2151 XFS_ERRLEVEL_LOW, mp, agf);
2152 xfs_trans_brelse(tp, *bpp);
2153 return XFS_ERROR(EFSCORRUPTED);
2156 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
2157 return 0;
2161 * Read in the allocation group header (free/alloc section).
2163 int /* error */
2164 xfs_alloc_read_agf(
2165 struct xfs_mount *mp, /* mount point structure */
2166 struct xfs_trans *tp, /* transaction pointer */
2167 xfs_agnumber_t agno, /* allocation group number */
2168 int flags, /* XFS_ALLOC_FLAG_... */
2169 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2171 struct xfs_agf *agf; /* ag freelist header */
2172 struct xfs_perag *pag; /* per allocation group data */
2173 int error;
2175 ASSERT(agno != NULLAGNUMBER);
2177 error = xfs_read_agf(mp, tp, agno,
2178 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0,
2179 bpp);
2180 if (error)
2181 return error;
2182 if (!*bpp)
2183 return 0;
2184 ASSERT(!XFS_BUF_GETERROR(*bpp));
2186 agf = XFS_BUF_TO_AGF(*bpp);
2187 pag = &mp->m_perag[agno];
2188 if (!pag->pagf_init) {
2189 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2190 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2191 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2192 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2193 pag->pagf_levels[XFS_BTNUM_BNOi] =
2194 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2195 pag->pagf_levels[XFS_BTNUM_CNTi] =
2196 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2197 spin_lock_init(&pag->pagb_lock);
2198 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2199 sizeof(xfs_perag_busy_t), KM_SLEEP);
2200 pag->pagf_init = 1;
2202 #ifdef DEBUG
2203 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2204 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2205 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2206 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2207 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2208 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2209 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2210 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2211 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2213 #endif
2214 return 0;
2218 * Allocate an extent (variable-size).
2219 * Depending on the allocation type, we either look in a single allocation
2220 * group or loop over the allocation groups to find the result.
2222 int /* error */
2223 xfs_alloc_vextent(
2224 xfs_alloc_arg_t *args) /* allocation argument structure */
2226 xfs_agblock_t agsize; /* allocation group size */
2227 int error;
2228 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2229 xfs_extlen_t minleft;/* minimum left value, temp copy */
2230 xfs_mount_t *mp; /* mount structure pointer */
2231 xfs_agnumber_t sagno; /* starting allocation group number */
2232 xfs_alloctype_t type; /* input allocation type */
2233 int bump_rotor = 0;
2234 int no_min = 0;
2235 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2237 mp = args->mp;
2238 type = args->otype = args->type;
2239 args->agbno = NULLAGBLOCK;
2241 * Just fix this up, for the case where the last a.g. is shorter
2242 * (or there's only one a.g.) and the caller couldn't easily figure
2243 * that out (xfs_bmap_alloc).
2245 agsize = mp->m_sb.sb_agblocks;
2246 if (args->maxlen > agsize)
2247 args->maxlen = agsize;
2248 if (args->alignment == 0)
2249 args->alignment = 1;
2250 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2251 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2252 ASSERT(args->minlen <= args->maxlen);
2253 ASSERT(args->minlen <= agsize);
2254 ASSERT(args->mod < args->prod);
2255 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2256 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2257 args->minlen > args->maxlen || args->minlen > agsize ||
2258 args->mod >= args->prod) {
2259 args->fsbno = NULLFSBLOCK;
2260 trace_xfs_alloc_vextent_badargs(args);
2261 return 0;
2263 minleft = args->minleft;
2265 switch (type) {
2266 case XFS_ALLOCTYPE_THIS_AG:
2267 case XFS_ALLOCTYPE_NEAR_BNO:
2268 case XFS_ALLOCTYPE_THIS_BNO:
2270 * These three force us into a single a.g.
2272 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2273 down_read(&mp->m_peraglock);
2274 args->pag = &mp->m_perag[args->agno];
2275 args->minleft = 0;
2276 error = xfs_alloc_fix_freelist(args, 0);
2277 args->minleft = minleft;
2278 if (error) {
2279 trace_xfs_alloc_vextent_nofix(args);
2280 goto error0;
2282 if (!args->agbp) {
2283 up_read(&mp->m_peraglock);
2284 trace_xfs_alloc_vextent_noagbp(args);
2285 break;
2287 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2288 if ((error = xfs_alloc_ag_vextent(args)))
2289 goto error0;
2290 up_read(&mp->m_peraglock);
2291 break;
2292 case XFS_ALLOCTYPE_START_BNO:
2294 * Try near allocation first, then anywhere-in-ag after
2295 * the first a.g. fails.
2297 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2298 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2299 args->fsbno = XFS_AGB_TO_FSB(mp,
2300 ((mp->m_agfrotor / rotorstep) %
2301 mp->m_sb.sb_agcount), 0);
2302 bump_rotor = 1;
2304 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2305 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2306 /* FALLTHROUGH */
2307 case XFS_ALLOCTYPE_ANY_AG:
2308 case XFS_ALLOCTYPE_START_AG:
2309 case XFS_ALLOCTYPE_FIRST_AG:
2311 * Rotate through the allocation groups looking for a winner.
2313 if (type == XFS_ALLOCTYPE_ANY_AG) {
2315 * Start with the last place we left off.
2317 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2318 mp->m_sb.sb_agcount;
2319 args->type = XFS_ALLOCTYPE_THIS_AG;
2320 flags = XFS_ALLOC_FLAG_TRYLOCK;
2321 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2323 * Start with allocation group given by bno.
2325 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2326 args->type = XFS_ALLOCTYPE_THIS_AG;
2327 sagno = 0;
2328 flags = 0;
2329 } else {
2330 if (type == XFS_ALLOCTYPE_START_AG)
2331 args->type = XFS_ALLOCTYPE_THIS_AG;
2333 * Start with the given allocation group.
2335 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2336 flags = XFS_ALLOC_FLAG_TRYLOCK;
2339 * Loop over allocation groups twice; first time with
2340 * trylock set, second time without.
2342 down_read(&mp->m_peraglock);
2343 for (;;) {
2344 args->pag = &mp->m_perag[args->agno];
2345 if (no_min) args->minleft = 0;
2346 error = xfs_alloc_fix_freelist(args, flags);
2347 args->minleft = minleft;
2348 if (error) {
2349 trace_xfs_alloc_vextent_nofix(args);
2350 goto error0;
2353 * If we get a buffer back then the allocation will fly.
2355 if (args->agbp) {
2356 if ((error = xfs_alloc_ag_vextent(args)))
2357 goto error0;
2358 break;
2361 trace_xfs_alloc_vextent_loopfailed(args);
2364 * Didn't work, figure out the next iteration.
2366 if (args->agno == sagno &&
2367 type == XFS_ALLOCTYPE_START_BNO)
2368 args->type = XFS_ALLOCTYPE_THIS_AG;
2370 * For the first allocation, we can try any AG to get
2371 * space. However, if we already have allocated a
2372 * block, we don't want to try AGs whose number is below
2373 * sagno. Otherwise, we may end up with out-of-order
2374 * locking of AGF, which might cause deadlock.
2376 if (++(args->agno) == mp->m_sb.sb_agcount) {
2377 if (args->firstblock != NULLFSBLOCK)
2378 args->agno = sagno;
2379 else
2380 args->agno = 0;
2383 * Reached the starting a.g., must either be done
2384 * or switch to non-trylock mode.
2386 if (args->agno == sagno) {
2387 if (no_min == 1) {
2388 args->agbno = NULLAGBLOCK;
2389 trace_xfs_alloc_vextent_allfailed(args);
2390 break;
2392 if (flags == 0) {
2393 no_min = 1;
2394 } else {
2395 flags = 0;
2396 if (type == XFS_ALLOCTYPE_START_BNO) {
2397 args->agbno = XFS_FSB_TO_AGBNO(mp,
2398 args->fsbno);
2399 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2404 up_read(&mp->m_peraglock);
2405 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2406 if (args->agno == sagno)
2407 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2408 (mp->m_sb.sb_agcount * rotorstep);
2409 else
2410 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2411 (mp->m_sb.sb_agcount * rotorstep);
2413 break;
2414 default:
2415 ASSERT(0);
2416 /* NOTREACHED */
2418 if (args->agbno == NULLAGBLOCK)
2419 args->fsbno = NULLFSBLOCK;
2420 else {
2421 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2422 #ifdef DEBUG
2423 ASSERT(args->len >= args->minlen);
2424 ASSERT(args->len <= args->maxlen);
2425 ASSERT(args->agbno % args->alignment == 0);
2426 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2427 args->len);
2428 #endif
2430 return 0;
2431 error0:
2432 up_read(&mp->m_peraglock);
2433 return error;
2437 * Free an extent.
2438 * Just break up the extent address and hand off to xfs_free_ag_extent
2439 * after fixing up the freelist.
2441 int /* error */
2442 xfs_free_extent(
2443 xfs_trans_t *tp, /* transaction pointer */
2444 xfs_fsblock_t bno, /* starting block number of extent */
2445 xfs_extlen_t len) /* length of extent */
2447 xfs_alloc_arg_t args;
2448 int error;
2450 ASSERT(len != 0);
2451 memset(&args, 0, sizeof(xfs_alloc_arg_t));
2452 args.tp = tp;
2453 args.mp = tp->t_mountp;
2454 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2455 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2456 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2457 down_read(&args.mp->m_peraglock);
2458 args.pag = &args.mp->m_perag[args.agno];
2459 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
2460 goto error0;
2461 #ifdef DEBUG
2462 ASSERT(args.agbp != NULL);
2463 ASSERT((args.agbno + len) <=
2464 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
2465 #endif
2466 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
2467 error0:
2468 up_read(&args.mp->m_peraglock);
2469 return error;
2474 * AG Busy list management
2475 * The busy list contains block ranges that have been freed but whose
2476 * transactions have not yet hit disk. If any block listed in a busy
2477 * list is reused, the transaction that freed it must be forced to disk
2478 * before continuing to use the block.
2480 * xfs_alloc_mark_busy - add to the per-ag busy list
2481 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2483 void
2484 xfs_alloc_mark_busy(xfs_trans_t *tp,
2485 xfs_agnumber_t agno,
2486 xfs_agblock_t bno,
2487 xfs_extlen_t len)
2489 xfs_mount_t *mp;
2490 xfs_perag_busy_t *bsy;
2491 int n;
2493 mp = tp->t_mountp;
2494 spin_lock(&mp->m_perag[agno].pagb_lock);
2496 /* search pagb_list for an open slot */
2497 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2498 n < XFS_PAGB_NUM_SLOTS;
2499 bsy++, n++) {
2500 if (bsy->busy_tp == NULL) {
2501 break;
2505 trace_xfs_alloc_busy(mp, agno, bno, len, n);
2507 if (n < XFS_PAGB_NUM_SLOTS) {
2508 bsy = &mp->m_perag[agno].pagb_list[n];
2509 mp->m_perag[agno].pagb_count++;
2510 bsy->busy_start = bno;
2511 bsy->busy_length = len;
2512 bsy->busy_tp = tp;
2513 xfs_trans_add_busy(tp, agno, n);
2514 } else {
2516 * The busy list is full! Since it is now not possible to
2517 * track the free block, make this a synchronous transaction
2518 * to insure that the block is not reused before this
2519 * transaction commits.
2521 xfs_trans_set_sync(tp);
2524 spin_unlock(&mp->m_perag[agno].pagb_lock);
2527 void
2528 xfs_alloc_clear_busy(xfs_trans_t *tp,
2529 xfs_agnumber_t agno,
2530 int idx)
2532 xfs_mount_t *mp;
2533 xfs_perag_busy_t *list;
2535 mp = tp->t_mountp;
2537 spin_lock(&mp->m_perag[agno].pagb_lock);
2538 list = mp->m_perag[agno].pagb_list;
2540 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2542 trace_xfs_alloc_unbusy(mp, agno, idx, list[idx].busy_tp == tp);
2544 if (list[idx].busy_tp == tp) {
2545 list[idx].busy_tp = NULL;
2546 mp->m_perag[agno].pagb_count--;
2549 spin_unlock(&mp->m_perag[agno].pagb_lock);
2554 * If we find the extent in the busy list, force the log out to get the
2555 * extent out of the busy list so the caller can use it straight away.
2557 STATIC void
2558 xfs_alloc_search_busy(xfs_trans_t *tp,
2559 xfs_agnumber_t agno,
2560 xfs_agblock_t bno,
2561 xfs_extlen_t len)
2563 xfs_mount_t *mp;
2564 xfs_perag_busy_t *bsy;
2565 xfs_agblock_t uend, bend;
2566 xfs_lsn_t lsn = 0;
2567 int cnt;
2569 mp = tp->t_mountp;
2571 spin_lock(&mp->m_perag[agno].pagb_lock);
2573 uend = bno + len - 1;
2576 * search pagb_list for this slot, skipping open slots. We have to
2577 * search the entire array as there may be multiple overlaps and
2578 * we have to get the most recent LSN for the log force to push out
2579 * all the transactions that span the range.
2581 for (cnt = 0; cnt < mp->m_perag[agno].pagb_count; cnt++) {
2582 bsy = &mp->m_perag[agno].pagb_list[cnt];
2583 if (!bsy->busy_tp)
2584 continue;
2586 bend = bsy->busy_start + bsy->busy_length - 1;
2587 if (bno > bend || uend < bsy->busy_start)
2588 continue;
2590 /* (start1,length1) within (start2, length2) */
2591 if (XFS_LSN_CMP(bsy->busy_tp->t_commit_lsn, lsn) > 0)
2592 lsn = bsy->busy_tp->t_commit_lsn;
2594 spin_unlock(&mp->m_perag[agno].pagb_lock);
2595 trace_xfs_alloc_busysearch(tp->t_mountp, agno, bno, len, lsn);
2598 * If a block was found, force the log through the LSN of the
2599 * transaction that freed the block
2601 if (lsn)
2602 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);