]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_alloc.c
New xfsprogs version - enabled unwritten extents by default, new commands
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_alloc.c
1 /*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <xfs.h>
34
35 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
36 #define XFSA_FIXUP_BNO_OK 1
37 #define XFSA_FIXUP_CNT_OK 2
38
39 /*
40 * Compute aligned version of the found extent.
41 * Takes alignment and min length into account.
42 */
43 STATIC int /* success (>= minlen) */
44 xfs_alloc_compute_aligned(
45 xfs_agblock_t foundbno, /* starting block in found extent */
46 xfs_extlen_t foundlen, /* length in found extent */
47 xfs_extlen_t alignment, /* alignment for allocation */
48 xfs_extlen_t minlen, /* minimum length for allocation */
49 xfs_agblock_t *resbno, /* result block number */
50 xfs_extlen_t *reslen) /* result length */
51 {
52 xfs_agblock_t bno;
53 xfs_extlen_t diff;
54 xfs_extlen_t len;
55
56 if (alignment > 1 && foundlen >= minlen) {
57 bno = roundup(foundbno, alignment);
58 diff = bno - foundbno;
59 len = diff >= foundlen ? 0 : foundlen - diff;
60 } else {
61 bno = foundbno;
62 len = foundlen;
63 }
64 *resbno = bno;
65 *reslen = len;
66 return len >= minlen;
67 }
68
69 /*
70 * Compute best start block and diff for "near" allocations.
71 * freelen >= wantlen already checked by caller.
72 */
73 STATIC xfs_extlen_t /* difference value (absolute) */
74 xfs_alloc_compute_diff(
75 xfs_agblock_t wantbno, /* target starting block */
76 xfs_extlen_t wantlen, /* target length */
77 xfs_extlen_t alignment, /* target alignment */
78 xfs_agblock_t freebno, /* freespace's starting block */
79 xfs_extlen_t freelen, /* freespace's length */
80 xfs_agblock_t *newbnop) /* result: best start block from free */
81 {
82 xfs_agblock_t freeend; /* end of freespace extent */
83 xfs_agblock_t newbno1; /* return block number */
84 xfs_agblock_t newbno2; /* other new block number */
85 xfs_extlen_t newlen1=0; /* length with newbno1 */
86 xfs_extlen_t newlen2=0; /* length with newbno2 */
87 xfs_agblock_t wantend; /* end of target extent */
88
89 ASSERT(freelen >= wantlen);
90 freeend = freebno + freelen;
91 wantend = wantbno + wantlen;
92 if (freebno >= wantbno) {
93 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
94 newbno1 = NULLAGBLOCK;
95 } else if (freeend >= wantend && alignment > 1) {
96 newbno1 = roundup(wantbno, alignment);
97 newbno2 = newbno1 - alignment;
98 if (newbno1 >= freeend)
99 newbno1 = NULLAGBLOCK;
100 else
101 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
102 if (newbno2 < freebno)
103 newbno2 = NULLAGBLOCK;
104 else
105 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
106 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
107 if (newlen1 < newlen2 ||
108 (newlen1 == newlen2 &&
109 XFS_ABSDIFF(newbno1, wantbno) >
110 XFS_ABSDIFF(newbno2, wantbno)))
111 newbno1 = newbno2;
112 } else if (newbno2 != NULLAGBLOCK)
113 newbno1 = newbno2;
114 } else if (freeend >= wantend) {
115 newbno1 = wantbno;
116 } else if (alignment > 1) {
117 newbno1 = roundup(freeend - wantlen, alignment);
118 if (newbno1 > freeend - wantlen &&
119 newbno1 - alignment >= freebno)
120 newbno1 -= alignment;
121 else if (newbno1 >= freeend)
122 newbno1 = NULLAGBLOCK;
123 } else
124 newbno1 = freeend - wantlen;
125 *newbnop = newbno1;
126 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
127 }
128
129 /*
130 * Fix up the length, based on mod and prod.
131 * len should be k * prod + mod for some k.
132 * If len is too small it is returned unchanged.
133 * If len hits maxlen it is left alone.
134 */
135 STATIC void
136 xfs_alloc_fix_len(
137 xfs_alloc_arg_t *args) /* allocation argument structure */
138 {
139 xfs_extlen_t k;
140 xfs_extlen_t rlen;
141
142 ASSERT(args->mod < args->prod);
143 rlen = args->len;
144 ASSERT(rlen >= args->minlen);
145 ASSERT(rlen <= args->maxlen);
146 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
147 (args->mod == 0 && rlen < args->prod))
148 return;
149 k = rlen % args->prod;
150 if (k == args->mod)
151 return;
152 if (k > args->mod) {
153 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
154 return;
155 } else {
156 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
157 (int)args->minlen)
158 return;
159 }
160 ASSERT(rlen >= args->minlen);
161 ASSERT(rlen <= args->maxlen);
162 args->len = rlen;
163 }
164
165 /*
166 * Fix up length if there is too little space left in the a.g.
167 * Return 1 if ok, 0 if too little, should give up.
168 */
169 STATIC int
170 xfs_alloc_fix_minleft(
171 xfs_alloc_arg_t *args) /* allocation argument structure */
172 {
173 xfs_agf_t *agf; /* a.g. freelist header */
174 int diff; /* free space difference */
175
176 if (args->minleft == 0)
177 return 1;
178 agf = XFS_BUF_TO_AGF(args->agbp);
179 diff = INT_GET(agf->agf_freeblks, ARCH_CONVERT)
180 + INT_GET(agf->agf_flcount, ARCH_CONVERT)
181 - args->len - args->minleft;
182 if (diff >= 0)
183 return 1;
184 args->len += diff; /* shrink the allocated space */
185 if (args->len >= args->minlen)
186 return 1;
187 args->agbno = NULLAGBLOCK;
188 return 0;
189 }
190
191 /*
192 * Update the two btrees, logically removing from freespace the extent
193 * starting at rbno, rlen blocks. The extent is contained within the
194 * actual (current) free extent fbno for flen blocks.
195 * Flags are passed in indicating whether the cursors are set to the
196 * relevant records.
197 */
198 STATIC int /* error code */
199 xfs_alloc_fixup_trees(
200 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
201 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
202 xfs_agblock_t fbno, /* starting block of free extent */
203 xfs_extlen_t flen, /* length of free extent */
204 xfs_agblock_t rbno, /* starting block of returned extent */
205 xfs_extlen_t rlen, /* length of returned extent */
206 int flags) /* flags, XFSA_FIXUP_... */
207 {
208 int error; /* error code */
209 int i; /* operation results */
210 xfs_agblock_t nfbno1; /* first new free startblock */
211 xfs_agblock_t nfbno2; /* second new free startblock */
212 xfs_extlen_t nflen1=0; /* first new free length */
213 xfs_extlen_t nflen2=0; /* second new free length */
214
215 /*
216 * Look up the record in the by-size tree if necessary.
217 */
218 if (flags & XFSA_FIXUP_CNT_OK) {
219 #ifdef DEBUG
220 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
221 return error;
222 XFS_WANT_CORRUPTED_RETURN(
223 i == 1 && nfbno1 == fbno && nflen1 == flen);
224 #endif
225 } else {
226 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
227 return error;
228 XFS_WANT_CORRUPTED_RETURN(i == 1);
229 }
230 /*
231 * Look up the record in the by-block tree if necessary.
232 */
233 if (flags & XFSA_FIXUP_BNO_OK) {
234 #ifdef DEBUG
235 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
236 return error;
237 XFS_WANT_CORRUPTED_RETURN(
238 i == 1 && nfbno1 == fbno && nflen1 == flen);
239 #endif
240 } else {
241 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
242 return error;
243 XFS_WANT_CORRUPTED_RETURN(i == 1);
244 }
245 #ifdef DEBUG
246 {
247 xfs_alloc_block_t *bnoblock;
248 xfs_alloc_block_t *cntblock;
249
250 if (bno_cur->bc_nlevels == 1 &&
251 cnt_cur->bc_nlevels == 1) {
252 bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
253 cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
254 XFS_WANT_CORRUPTED_RETURN(
255 INT_GET(bnoblock->bb_numrecs, ARCH_CONVERT) == INT_GET(cntblock->bb_numrecs, ARCH_CONVERT));
256 }
257 }
258 #endif
259 /*
260 * Deal with all four cases: the allocated record is contained
261 * within the freespace record, so we can have new freespace
262 * at either (or both) end, or no freespace remaining.
263 */
264 if (rbno == fbno && rlen == flen)
265 nfbno1 = nfbno2 = NULLAGBLOCK;
266 else if (rbno == fbno) {
267 nfbno1 = rbno + rlen;
268 nflen1 = flen - rlen;
269 nfbno2 = NULLAGBLOCK;
270 } else if (rbno + rlen == fbno + flen) {
271 nfbno1 = fbno;
272 nflen1 = flen - rlen;
273 nfbno2 = NULLAGBLOCK;
274 } else {
275 nfbno1 = fbno;
276 nflen1 = rbno - fbno;
277 nfbno2 = rbno + rlen;
278 nflen2 = (fbno + flen) - nfbno2;
279 }
280 /*
281 * Delete the entry from the by-size btree.
282 */
283 if ((error = xfs_alloc_delete(cnt_cur, &i)))
284 return error;
285 XFS_WANT_CORRUPTED_RETURN(i == 1);
286 /*
287 * Add new by-size btree entry(s).
288 */
289 if (nfbno1 != NULLAGBLOCK) {
290 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
291 return error;
292 XFS_WANT_CORRUPTED_RETURN(i == 0);
293 if ((error = xfs_alloc_insert(cnt_cur, &i)))
294 return error;
295 XFS_WANT_CORRUPTED_RETURN(i == 1);
296 }
297 if (nfbno2 != NULLAGBLOCK) {
298 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
299 return error;
300 XFS_WANT_CORRUPTED_RETURN(i == 0);
301 if ((error = xfs_alloc_insert(cnt_cur, &i)))
302 return error;
303 XFS_WANT_CORRUPTED_RETURN(i == 1);
304 }
305 /*
306 * Fix up the by-block btree entry(s).
307 */
308 if (nfbno1 == NULLAGBLOCK) {
309 /*
310 * No remaining freespace, just delete the by-block tree entry.
311 */
312 if ((error = xfs_alloc_delete(bno_cur, &i)))
313 return error;
314 XFS_WANT_CORRUPTED_RETURN(i == 1);
315 } else {
316 /*
317 * Update the by-block entry to start later|be shorter.
318 */
319 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
320 return error;
321 }
322 if (nfbno2 != NULLAGBLOCK) {
323 /*
324 * 2 resulting free entries, need to add one.
325 */
326 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
327 return error;
328 XFS_WANT_CORRUPTED_RETURN(i == 0);
329 if ((error = xfs_alloc_insert(bno_cur, &i)))
330 return error;
331 XFS_WANT_CORRUPTED_RETURN(i == 1);
332 }
333 return 0;
334 }
335
336 /*
337 * Read in the allocation group free block array.
338 */
339 STATIC int /* error */
340 xfs_alloc_read_agfl(
341 xfs_mount_t *mp, /* mount point structure */
342 xfs_trans_t *tp, /* transaction pointer */
343 xfs_agnumber_t agno, /* allocation group number */
344 xfs_buf_t **bpp) /* buffer for the ag free block array */
345 {
346 xfs_buf_t *bp; /* return value */
347 int error;
348
349 ASSERT(agno != NULLAGNUMBER);
350 error = xfs_trans_read_buf(
351 mp, tp, mp->m_ddev_targp,
352 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
353 XFS_FSS_TO_BB(mp, 1), 0, &bp);
354 if (error)
355 return error;
356 ASSERT(bp);
357 ASSERT(!XFS_BUF_GETERROR(bp));
358 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
359 *bpp = bp;
360 return 0;
361 }
362
363 #if defined(XFS_ALLOC_TRACE)
364 /*
365 * Add an allocation trace entry for an alloc call.
366 */
367 STATIC void
368 xfs_alloc_trace_alloc(
369 char *name, /* function tag string */
370 char *str, /* additional string */
371 xfs_alloc_arg_t *args, /* allocation argument structure */
372 int line) /* source line number */
373 {
374 ktrace_enter(xfs_alloc_trace_buf,
375 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
376 (void *)name,
377 (void *)str,
378 (void *)args->mp,
379 (void *)(__psunsigned_t)args->agno,
380 (void *)(__psunsigned_t)args->agbno,
381 (void *)(__psunsigned_t)args->minlen,
382 (void *)(__psunsigned_t)args->maxlen,
383 (void *)(__psunsigned_t)args->mod,
384 (void *)(__psunsigned_t)args->prod,
385 (void *)(__psunsigned_t)args->minleft,
386 (void *)(__psunsigned_t)args->total,
387 (void *)(__psunsigned_t)args->alignment,
388 (void *)(__psunsigned_t)args->len,
389 (void *)((((__psint_t)args->type) << 16) |
390 (__psint_t)args->otype),
391 (void *)(__psint_t)((args->wasdel << 3) |
392 (args->wasfromfl << 2) |
393 (args->isfl << 1) |
394 (args->userdata << 0)));
395 }
396
397 /*
398 * Add an allocation trace entry for a free call.
399 */
400 STATIC void
401 xfs_alloc_trace_free(
402 char *name, /* function tag string */
403 char *str, /* additional string */
404 xfs_mount_t *mp, /* file system mount point */
405 xfs_agnumber_t agno, /* allocation group number */
406 xfs_agblock_t agbno, /* a.g. relative block number */
407 xfs_extlen_t len, /* length of extent */
408 int isfl, /* set if is freelist allocation/free */
409 int line) /* source line number */
410 {
411 ktrace_enter(xfs_alloc_trace_buf,
412 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
413 (void *)name,
414 (void *)str,
415 (void *)mp,
416 (void *)(__psunsigned_t)agno,
417 (void *)(__psunsigned_t)agbno,
418 (void *)(__psunsigned_t)len,
419 (void *)(__psint_t)isfl,
420 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
421 }
422
423 /*
424 * Add an allocation trace entry for modifying an agf.
425 */
426 STATIC void
427 xfs_alloc_trace_modagf(
428 char *name, /* function tag string */
429 char *str, /* additional string */
430 xfs_mount_t *mp, /* file system mount point */
431 xfs_agf_t *agf, /* new agf value */
432 int flags, /* logging flags for agf */
433 int line) /* source line number */
434 {
435 ktrace_enter(xfs_alloc_trace_buf,
436 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
437 (void *)name,
438 (void *)str,
439 (void *)mp,
440 (void *)(__psint_t)flags,
441 (void *)(__psunsigned_t)INT_GET(agf->agf_seqno, ARCH_CONVERT),
442 (void *)(__psunsigned_t)INT_GET(agf->agf_length, ARCH_CONVERT),
443 (void *)(__psunsigned_t)INT_GET(agf->agf_roots[XFS_BTNUM_BNO],
444 ARCH_CONVERT),
445 (void *)(__psunsigned_t)INT_GET(agf->agf_roots[XFS_BTNUM_CNT],
446 ARCH_CONVERT),
447 (void *)(__psunsigned_t)INT_GET(agf->agf_levels[XFS_BTNUM_BNO],
448 ARCH_CONVERT),
449 (void *)(__psunsigned_t)INT_GET(agf->agf_levels[XFS_BTNUM_CNT],
450 ARCH_CONVERT),
451 (void *)(__psunsigned_t)INT_GET(agf->agf_flfirst, ARCH_CONVERT),
452 (void *)(__psunsigned_t)INT_GET(agf->agf_fllast, ARCH_CONVERT),
453 (void *)(__psunsigned_t)INT_GET(agf->agf_flcount, ARCH_CONVERT),
454 (void *)(__psunsigned_t)INT_GET(agf->agf_freeblks, ARCH_CONVERT),
455 (void *)(__psunsigned_t)INT_GET(agf->agf_longest, ARCH_CONVERT));
456 }
457 #endif /* XFS_ALLOC_TRACE */
458
459 /*
460 * Allocation group level functions.
461 */
462
463 /*
464 * Allocate a variable extent in the allocation group agno.
465 * Type and bno are used to determine where in the allocation group the
466 * extent will start.
467 * Extent's length (returned in *len) will be between minlen and maxlen,
468 * and of the form k * prod + mod unless there's nothing that large.
469 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
470 */
471 STATIC int /* error */
472 xfs_alloc_ag_vextent(
473 xfs_alloc_arg_t *args) /* argument structure for allocation */
474 {
475 int error=0;
476 #ifdef XFS_ALLOC_TRACE
477 static char fname[] = "xfs_alloc_ag_vextent";
478 #endif
479
480 ASSERT(args->minlen > 0);
481 ASSERT(args->maxlen > 0);
482 ASSERT(args->minlen <= args->maxlen);
483 ASSERT(args->mod < args->prod);
484 ASSERT(args->alignment > 0);
485 /*
486 * Branch to correct routine based on the type.
487 */
488 args->wasfromfl = 0;
489 switch (args->type) {
490 case XFS_ALLOCTYPE_THIS_AG:
491 error = xfs_alloc_ag_vextent_size(args);
492 break;
493 case XFS_ALLOCTYPE_NEAR_BNO:
494 error = xfs_alloc_ag_vextent_near(args);
495 break;
496 case XFS_ALLOCTYPE_THIS_BNO:
497 error = xfs_alloc_ag_vextent_exact(args);
498 break;
499 default:
500 ASSERT(0);
501 /* NOTREACHED */
502 }
503 if (error)
504 return error;
505 /*
506 * If the allocation worked, need to change the agf structure
507 * (and log it), and the superblock.
508 */
509 if (args->agbno != NULLAGBLOCK) {
510 xfs_agf_t *agf; /* allocation group freelist header */
511 #ifdef XFS_ALLOC_TRACE
512 xfs_mount_t *mp = args->mp;
513 #endif
514 long slen = (long)args->len;
515
516 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
517 ASSERT(!(args->wasfromfl) || !args->isfl);
518 ASSERT(args->agbno % args->alignment == 0);
519 if (!(args->wasfromfl)) {
520
521 agf = XFS_BUF_TO_AGF(args->agbp);
522 INT_MOD(agf->agf_freeblks, ARCH_CONVERT, -(args->len));
523 xfs_trans_agblocks_delta(args->tp,
524 -((long)(args->len)));
525 args->pag->pagf_freeblks -= args->len;
526 ASSERT(INT_GET(agf->agf_freeblks, ARCH_CONVERT)
527 <= INT_GET(agf->agf_length, ARCH_CONVERT));
528 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
529 xfs_alloc_log_agf(args->tp, args->agbp,
530 XFS_AGF_FREEBLKS);
531 /* search the busylist for these blocks */
532 xfs_alloc_search_busy(args->tp, args->agno,
533 args->agbno, args->len);
534 }
535 if (!args->isfl)
536 xfs_trans_mod_sb(args->tp,
537 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
538 XFS_TRANS_SB_FDBLOCKS, -slen);
539 XFS_STATS_INC(xfsstats.xs_allocx);
540 XFS_STATS_ADD(xfsstats.xs_allocb, args->len);
541 }
542 return 0;
543 }
544
545 /*
546 * Allocate a variable extent at exactly agno/bno.
547 * Extent's length (returned in *len) will be between minlen and maxlen,
548 * and of the form k * prod + mod unless there's nothing that large.
549 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
550 */
551 STATIC int /* error */
552 xfs_alloc_ag_vextent_exact(
553 xfs_alloc_arg_t *args) /* allocation argument structure */
554 {
555 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
556 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
557 xfs_agblock_t end; /* end of allocated extent */
558 int error;
559 xfs_agblock_t fbno; /* start block of found extent */
560 xfs_agblock_t fend; /* end block of found extent */
561 xfs_extlen_t flen; /* length of found extent */
562 #ifdef XFS_ALLOC_TRACE
563 static char fname[] = "xfs_alloc_ag_vextent_exact";
564 #endif
565 int i; /* success/failure of operation */
566 xfs_agblock_t maxend; /* end of maximal extent */
567 xfs_agblock_t minend; /* end of minimal extent */
568 xfs_extlen_t rlen; /* length of returned extent */
569
570 ASSERT(args->alignment == 1);
571 /*
572 * Allocate/initialize a cursor for the by-number freespace btree.
573 */
574 bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
575 args->agno, XFS_BTNUM_BNO, 0, 0);
576 /*
577 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
578 * Look for the closest free block <= bno, it must contain bno
579 * if any free block does.
580 */
581 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
582 goto error0;
583 if (!i) {
584 /*
585 * Didn't find it, return null.
586 */
587 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
588 args->agbno = NULLAGBLOCK;
589 return 0;
590 }
591 /*
592 * Grab the freespace record.
593 */
594 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
595 goto error0;
596 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
597 ASSERT(fbno <= args->agbno);
598 minend = args->agbno + args->minlen;
599 maxend = args->agbno + args->maxlen;
600 fend = fbno + flen;
601 /*
602 * Give up if the freespace isn't long enough for the minimum request.
603 */
604 if (fend < minend) {
605 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
606 args->agbno = NULLAGBLOCK;
607 return 0;
608 }
609 /*
610 * End of extent will be smaller of the freespace end and the
611 * maximal requested end.
612 */
613 end = XFS_AGBLOCK_MIN(fend, maxend);
614 /*
615 * Fix the length according to mod and prod if given.
616 */
617 args->len = end - args->agbno;
618 xfs_alloc_fix_len(args);
619 if (!xfs_alloc_fix_minleft(args)) {
620 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
621 return 0;
622 }
623 rlen = args->len;
624 ASSERT(args->agbno + rlen <= fend);
625 end = args->agbno + rlen;
626 /*
627 * We are allocating agbno for rlen [agbno .. end]
628 * Allocate/initialize a cursor for the by-size btree.
629 */
630 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
631 args->agno, XFS_BTNUM_CNT, 0, 0);
632 ASSERT(args->agbno + args->len <=
633 INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
634 ARCH_CONVERT));
635 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
636 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
637 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
638 goto error0;
639 }
640 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
641 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
642 TRACE_ALLOC("normal", args);
643 args->wasfromfl = 0;
644 return 0;
645
646 error0:
647 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
648 TRACE_ALLOC("error", args);
649 return error;
650 }
651
652 /*
653 * Allocate a variable extent near bno in the allocation group agno.
654 * Extent's length (returned in len) will be between minlen and maxlen,
655 * and of the form k * prod + mod unless there's nothing that large.
656 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
657 */
658 STATIC int /* error */
659 xfs_alloc_ag_vextent_near(
660 xfs_alloc_arg_t *args) /* allocation argument structure */
661 {
662 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
663 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
664 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
665 #ifdef XFS_ALLOC_TRACE
666 static char fname[] = "xfs_alloc_ag_vextent_near";
667 #endif
668 xfs_agblock_t gtbno; /* start bno of right side entry */
669 xfs_agblock_t gtbnoa; /* aligned ... */
670 xfs_extlen_t gtdiff; /* difference to right side entry */
671 xfs_extlen_t gtlen; /* length of right side entry */
672 xfs_extlen_t gtlena; /* aligned ... */
673 xfs_agblock_t gtnew; /* useful start bno of right side */
674 int error; /* error code */
675 int i; /* result code, temporary */
676 int j; /* result code, temporary */
677 xfs_agblock_t ltbno; /* start bno of left side entry */
678 xfs_agblock_t ltbnoa; /* aligned ... */
679 xfs_extlen_t ltdiff; /* difference to left side entry */
680 /*REFERENCED*/
681 xfs_agblock_t ltend; /* end bno of left side entry */
682 xfs_extlen_t ltlen; /* length of left side entry */
683 xfs_extlen_t ltlena; /* aligned ... */
684 xfs_agblock_t ltnew; /* useful start bno of left side */
685 xfs_extlen_t rlen; /* length of returned extent */
686 #if defined(DEBUG) && defined(__KERNEL__)
687 /*
688 * Randomly don't execute the first algorithm.
689 */
690 static int seed; /* randomizing seed value */
691 int dofirst; /* set to do first algorithm */
692 timespec_t now; /* current time */
693
694 if (!seed) {
695 nanotime(&now);
696 seed = (int)now.tv_sec ^ (int)now.tv_nsec;
697 }
698 dofirst = random() & 1;
699 #endif
700 /*
701 * Get a cursor for the by-size btree.
702 */
703 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
704 args->agno, XFS_BTNUM_CNT, 0, 0);
705 ltlen = 0;
706 bno_cur_lt = bno_cur_gt = NULL;
707 /*
708 * See if there are any free extents as big as maxlen.
709 */
710 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
711 goto error0;
712 /*
713 * If none, then pick up the last entry in the tree unless the
714 * tree is empty.
715 */
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;
723 }
724 ASSERT(i == 1);
725 }
726 args->wasfromfl = 0;
727 /*
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.
736 */
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;
742
743 #if defined(DEBUG) && defined(__KERNEL__)
744 if (!dofirst)
745 break;
746 #endif
747 /*
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.
752 */
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_alloc_increment(cnt_cur, 0, &i)))
763 goto error0;
764 } while (i);
765 ASSERT(ltlen >= args->minlen);
766 if (!i)
767 break;
768 }
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_alloc_increment(cnt_cur, 0, &j)) {
773 /*
774 * For each entry, decide if it's better than
775 * the previous best entry.
776 */
777 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
778 goto error0;
779 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
780 if (!xfs_alloc_compute_aligned(ltbno, ltlen,
781 args->alignment, args->minlen,
782 &ltbnoa, &ltlena))
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];
797 }
798 }
799 /*
800 * It didn't work. We COULD be in a case where
801 * there's a good record somewhere, so try again.
802 */
803 if (blen == 0)
804 break;
805 /*
806 * Point at the best entry, and retrieve it again.
807 */
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 <= INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
814 ARCH_CONVERT));
815 args->len = blen;
816 if (!xfs_alloc_fix_minleft(args)) {
817 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
818 TRACE_ALLOC("nominleft", args);
819 return 0;
820 }
821 blen = args->len;
822 /*
823 * We are allocating starting at bnew for blen blocks.
824 */
825 args->agbno = bnew;
826 ASSERT(bnew >= ltbno);
827 ASSERT(bnew + blen <= ltend);
828 /*
829 * Set up a cursor for the by-bno tree.
830 */
831 bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp,
832 args->agbp, args->agno, XFS_BTNUM_BNO, 0, 0);
833 /*
834 * Fix up the btree entries.
835 */
836 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
837 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
838 goto error0;
839 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
840 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
841 TRACE_ALLOC("first", args);
842 return 0;
843 }
844 /*
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.
855 */
856 /*
857 * Allocate and initialize the cursor for the leftward search.
858 */
859 bno_cur_lt = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
860 args->agno, XFS_BTNUM_BNO, 0, 0);
861 /*
862 * Lookup <= bno to find the leftward search's starting point.
863 */
864 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
865 goto error0;
866 if (!i) {
867 /*
868 * Didn't find anything; use this cursor for the rightward
869 * search.
870 */
871 bno_cur_gt = bno_cur_lt;
872 bno_cur_lt = 0;
873 }
874 /*
875 * Found something. Duplicate the cursor for the rightward search.
876 */
877 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
878 goto error0;
879 /*
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.
882 */
883 if ((error = xfs_alloc_increment(bno_cur_gt, 0, &i)))
884 goto error0;
885 if (!i) {
886 /*
887 * It failed, there are no rightward entries.
888 */
889 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
890 bno_cur_gt = NULL;
891 }
892 /*
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.
896 */
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 if (xfs_alloc_compute_aligned(ltbno, ltlen,
903 args->alignment, args->minlen,
904 &ltbnoa, &ltlena))
905 break;
906 if ((error = xfs_alloc_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;
912 }
913 }
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 if (xfs_alloc_compute_aligned(gtbno, gtlen,
919 args->alignment, args->minlen,
920 &gtbnoa, &gtlena))
921 break;
922 if ((error = xfs_alloc_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;
928 }
929 }
930 } while (bno_cur_lt || bno_cur_gt);
931 /*
932 * Got both cursors still active, need to find better entry.
933 */
934 if (bno_cur_lt && bno_cur_gt) {
935 /*
936 * Left side is long enough, look for a right side entry.
937 */
938 if (ltlena >= args->minlen) {
939 /*
940 * Fix up the length.
941 */
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);
947 /*
948 * Not perfect.
949 */
950 if (ltdiff) {
951 /*
952 * Look until we find a better one, run out of
953 * space, or run off the end.
954 */
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);
964 /*
965 * The left one is clearly better.
966 */
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;
973 }
974 /*
975 * If we reach a big enough entry,
976 * compare the two and pick the best.
977 */
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);
988 /*
989 * Right side is better.
990 */
991 if (gtdiff < ltdiff) {
992 xfs_btree_del_cursor(
993 bno_cur_lt,
994 XFS_BTREE_NOERROR);
995 bno_cur_lt = NULL;
996 }
997 /*
998 * Left side is better.
999 */
1000 else {
1001 xfs_btree_del_cursor(
1002 bno_cur_gt,
1003 XFS_BTREE_NOERROR);
1004 bno_cur_gt = NULL;
1005 }
1006 break;
1007 }
1008 /*
1009 * Fell off the right end.
1010 */
1011 if ((error = xfs_alloc_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;
1020 }
1021 }
1022 }
1023 /*
1024 * The left side is perfect, trash the right side.
1025 */
1026 else {
1027 xfs_btree_del_cursor(bno_cur_gt,
1028 XFS_BTREE_NOERROR);
1029 bno_cur_gt = NULL;
1030 }
1031 }
1032 /*
1033 * It's the right side that was found first, look left.
1034 */
1035 else {
1036 /*
1037 * Fix up the length.
1038 */
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);
1044 /*
1045 * Right side entry isn't perfect.
1046 */
1047 if (gtdiff) {
1048 /*
1049 * Look until we find a better one, run out of
1050 * space, or run off the end.
1051 */
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);
1061 /*
1062 * The right one is clearly better.
1063 */
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;
1070 }
1071 /*
1072 * If we reach a big enough entry,
1073 * compare the two and pick the best.
1074 */
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);
1084 /*
1085 * Left side is better.
1086 */
1087 if (ltdiff < gtdiff) {
1088 xfs_btree_del_cursor(
1089 bno_cur_gt,
1090 XFS_BTREE_NOERROR);
1091 bno_cur_gt = NULL;
1092 }
1093 /*
1094 * Right side is better.
1095 */
1096 else {
1097 xfs_btree_del_cursor(
1098 bno_cur_lt,
1099 XFS_BTREE_NOERROR);
1100 bno_cur_lt = NULL;
1101 }
1102 break;
1103 }
1104 /*
1105 * Fell off the left end.
1106 */
1107 if ((error = xfs_alloc_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;
1115 }
1116 }
1117 }
1118 /*
1119 * The right side is perfect, trash the left side.
1120 */
1121 else {
1122 xfs_btree_del_cursor(bno_cur_lt,
1123 XFS_BTREE_NOERROR);
1124 bno_cur_lt = NULL;
1125 }
1126 }
1127 }
1128 /*
1129 * If we couldn't get anything, give up.
1130 */
1131 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1132 TRACE_ALLOC("neither", args);
1133 args->agbno = NULLAGBLOCK;
1134 return 0;
1135 }
1136 /*
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.
1141 */
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;
1152 /*
1153 * Fix up the length and compute the useful address.
1154 */
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_ALLOC("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;
1163 }
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 <= INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
1170 ARCH_CONVERT));
1171 args->agbno = ltnew;
1172 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1173 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1174 goto error0;
1175 TRACE_ALLOC(j ? "gt" : "lt", args);
1176 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1177 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1178 return 0;
1179
1180 error0:
1181 TRACE_ALLOC("error", args);
1182 if (cnt_cur != NULL)
1183 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1184 if (bno_cur_lt != NULL)
1185 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1186 if (bno_cur_gt != NULL)
1187 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1188 return error;
1189 }
1190
1191 /*
1192 * Allocate a variable extent anywhere in the allocation group agno.
1193 * Extent's length (returned in len) will be between minlen and maxlen,
1194 * and of the form k * prod + mod unless there's nothing that large.
1195 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1196 */
1197 STATIC int /* error */
1198 xfs_alloc_ag_vextent_size(
1199 xfs_alloc_arg_t *args) /* allocation argument structure */
1200 {
1201 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1202 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1203 int error; /* error result */
1204 xfs_agblock_t fbno; /* start of found freespace */
1205 xfs_extlen_t flen; /* length of found freespace */
1206 #ifdef XFS_ALLOC_TRACE
1207 static char fname[] = "xfs_alloc_ag_vextent_size";
1208 #endif
1209 int i; /* temp status variable */
1210 xfs_agblock_t rbno; /* returned block number */
1211 xfs_extlen_t rlen; /* length of returned extent */
1212
1213 /*
1214 * Allocate and initialize a cursor for the by-size btree.
1215 */
1216 cnt_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
1217 args->agno, XFS_BTNUM_CNT, 0, 0);
1218 bno_cur = NULL;
1219 /*
1220 * Look for an entry >= maxlen+alignment-1 blocks.
1221 */
1222 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1223 args->maxlen + args->alignment - 1, &i)))
1224 goto error0;
1225 /*
1226 * If none, then pick up the last entry in the tree unless the
1227 * tree is empty.
1228 */
1229 if (!i) {
1230 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1231 &flen, &i)))
1232 goto error0;
1233 if (i == 0 || flen == 0) {
1234 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1235 TRACE_ALLOC("noentry", args);
1236 return 0;
1237 }
1238 ASSERT(i == 1);
1239 }
1240 /*
1241 * There's a freespace as big as maxlen+alignment-1, get it.
1242 */
1243 else {
1244 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1245 goto error0;
1246 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1247 }
1248 /*
1249 * In the first case above, we got the last entry in the
1250 * by-size btree. Now we check to see if the space hits maxlen
1251 * once aligned; if not, we search left for something better.
1252 * This can't happen in the second case above.
1253 */
1254 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1255 &rbno, &rlen);
1256 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1257 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1258 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1259 if (rlen < args->maxlen) {
1260 xfs_agblock_t bestfbno;
1261 xfs_extlen_t bestflen;
1262 xfs_agblock_t bestrbno;
1263 xfs_extlen_t bestrlen;
1264
1265 bestrlen = rlen;
1266 bestrbno = rbno;
1267 bestflen = flen;
1268 bestfbno = fbno;
1269 for (;;) {
1270 if ((error = xfs_alloc_decrement(cnt_cur, 0, &i)))
1271 goto error0;
1272 if (i == 0)
1273 break;
1274 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1275 &i)))
1276 goto error0;
1277 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1278 if (flen < bestrlen)
1279 break;
1280 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1281 args->minlen, &rbno, &rlen);
1282 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1283 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1284 (rlen <= flen && rbno + rlen <= fbno + flen),
1285 error0);
1286 if (rlen > bestrlen) {
1287 bestrlen = rlen;
1288 bestrbno = rbno;
1289 bestflen = flen;
1290 bestfbno = fbno;
1291 if (rlen == args->maxlen)
1292 break;
1293 }
1294 }
1295 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1296 &i)))
1297 goto error0;
1298 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1299 rlen = bestrlen;
1300 rbno = bestrbno;
1301 flen = bestflen;
1302 fbno = bestfbno;
1303 }
1304 args->wasfromfl = 0;
1305 /*
1306 * Fix up the length.
1307 */
1308 args->len = rlen;
1309 xfs_alloc_fix_len(args);
1310 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1311 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1312 TRACE_ALLOC("nominleft", args);
1313 args->agbno = NULLAGBLOCK;
1314 return 0;
1315 }
1316 rlen = args->len;
1317 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1318 /*
1319 * Allocate and initialize a cursor for the by-block tree.
1320 */
1321 bno_cur = xfs_btree_init_cursor(args->mp, args->tp, args->agbp,
1322 args->agno, XFS_BTNUM_BNO, 0, 0);
1323 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1324 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1325 goto error0;
1326 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1327 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1328 cnt_cur = bno_cur = NULL;
1329 args->len = rlen;
1330 args->agbno = rbno;
1331 XFS_WANT_CORRUPTED_GOTO(
1332 args->agbno + args->len <=
1333 INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
1334 ARCH_CONVERT),
1335 error0);
1336 TRACE_ALLOC("normal", args);
1337 return 0;
1338
1339 error0:
1340 TRACE_ALLOC("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;
1346 }
1347
1348 /*
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.
1352 */
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 */
1360 {
1361 int error;
1362 xfs_agblock_t fbno;
1363 xfs_extlen_t flen;
1364 #ifdef XFS_ALLOC_TRACE
1365 static char fname[] = "xfs_alloc_ag_vextent_small";
1366 #endif
1367 int i;
1368
1369 if ((error = xfs_alloc_decrement(ccur, 0, &i)))
1370 goto error0;
1371 if (i) {
1372 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1373 goto error0;
1374 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1375 }
1376 /*
1377 * Nothing in the btree, try the freelist. Make sure
1378 * to respect minleft even when pulling from the
1379 * freelist.
1380 */
1381 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
1382 (INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_flcount,
1383 ARCH_CONVERT) > args->minleft)) {
1384 if ((error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno)))
1385 goto error0;
1386 if (fbno != NULLAGBLOCK) {
1387 if (args->userdata) {
1388 xfs_buf_t *bp;
1389
1390 bp = xfs_btree_get_bufs(args->mp, args->tp,
1391 args->agno, fbno, 0);
1392 xfs_trans_binval(args->tp, bp);
1393 }
1394 args->len = 1;
1395 args->agbno = fbno;
1396 XFS_WANT_CORRUPTED_GOTO(
1397 args->agbno + args->len <=
1398 INT_GET(XFS_BUF_TO_AGF(args->agbp)->agf_length,
1399 ARCH_CONVERT),
1400 error0);
1401 args->wasfromfl = 1;
1402 TRACE_ALLOC("freelist", args);
1403 *stat = 0;
1404 return 0;
1405 }
1406 /*
1407 * Nothing in the freelist.
1408 */
1409 else
1410 flen = 0;
1411 }
1412 /*
1413 * Can't allocate from the freelist for some reason.
1414 */
1415 else
1416 flen = 0;
1417 /*
1418 * Can't do the allocation, give up.
1419 */
1420 if (flen < args->minlen) {
1421 args->agbno = NULLAGBLOCK;
1422 TRACE_ALLOC("notenough", args);
1423 flen = 0;
1424 }
1425 *fbnop = fbno;
1426 *flenp = flen;
1427 *stat = 1;
1428 TRACE_ALLOC("normal", args);
1429 return 0;
1430
1431 error0:
1432 TRACE_ALLOC("error", args);
1433 return error;
1434 }
1435
1436 /*
1437 * Free the extent starting at agno/bno for length.
1438 */
1439 STATIC int /* error */
1440 xfs_free_ag_extent(
1441 xfs_trans_t *tp, /* transaction pointer */
1442 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1443 xfs_agnumber_t agno, /* allocation group number */
1444 xfs_agblock_t bno, /* starting block number */
1445 xfs_extlen_t len, /* length of extent */
1446 int isfl) /* set if is freelist blocks - no sb acctg */
1447 {
1448 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1449 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1450 int error; /* error return value */
1451 #ifdef XFS_ALLOC_TRACE
1452 static char fname[] = "xfs_free_ag_extent";
1453 #endif
1454 xfs_agblock_t gtbno; /* start of right neighbor block */
1455 xfs_extlen_t gtlen; /* length of right neighbor block */
1456 int haveleft; /* have a left neighbor block */
1457 int haveright; /* have a right neighbor block */
1458 int i; /* temp, result code */
1459 xfs_agblock_t ltbno; /* start of left neighbor block */
1460 xfs_extlen_t ltlen; /* length of left neighbor block */
1461 xfs_mount_t *mp; /* mount point struct for filesystem */
1462 xfs_agblock_t nbno; /* new starting block of freespace */
1463 xfs_extlen_t nlen; /* new length of freespace */
1464
1465 mp = tp->t_mountp;
1466 /*
1467 * Allocate and initialize a cursor for the by-block btree.
1468 */
1469 bno_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO, 0,
1470 0);
1471 cnt_cur = NULL;
1472 /*
1473 * Look for a neighboring block on the left (lower block numbers)
1474 * that is contiguous with this space.
1475 */
1476 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1477 goto error0;
1478 if (haveleft) {
1479 /*
1480 * There is a block to our left.
1481 */
1482 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1483 goto error0;
1484 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1485 /*
1486 * It's not contiguous, though.
1487 */
1488 if (ltbno + ltlen < bno)
1489 haveleft = 0;
1490 else {
1491 /*
1492 * If this failure happens the request to free this
1493 * space was invalid, it's (partly) already free.
1494 * Very bad.
1495 */
1496 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1497 }
1498 }
1499 /*
1500 * Look for a neighboring block on the right (higher block numbers)
1501 * that is contiguous with this space.
1502 */
1503 if ((error = xfs_alloc_increment(bno_cur, 0, &haveright)))
1504 goto error0;
1505 if (haveright) {
1506 /*
1507 * There is a block to our right.
1508 */
1509 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1510 goto error0;
1511 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1512 /*
1513 * It's not contiguous, though.
1514 */
1515 if (bno + len < gtbno)
1516 haveright = 0;
1517 else {
1518 /*
1519 * If this failure happens the request to free this
1520 * space was invalid, it's (partly) already free.
1521 * Very bad.
1522 */
1523 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1524 }
1525 }
1526 /*
1527 * Now allocate and initialize a cursor for the by-size tree.
1528 */
1529 cnt_cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT, 0,
1530 0);
1531 /*
1532 * Have both left and right contiguous neighbors.
1533 * Merge all three into a single free block.
1534 */
1535 if (haveleft && haveright) {
1536 /*
1537 * Delete the old by-size entry on the left.
1538 */
1539 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1540 goto error0;
1541 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1542 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1543 goto error0;
1544 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1545 /*
1546 * Delete the old by-size entry on the right.
1547 */
1548 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1549 goto error0;
1550 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1551 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1552 goto error0;
1553 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1554 /*
1555 * Delete the old by-block entry for the right block.
1556 */
1557 if ((error = xfs_alloc_delete(bno_cur, &i)))
1558 goto error0;
1559 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1560 /*
1561 * Move the by-block cursor back to the left neighbor.
1562 */
1563 if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
1564 goto error0;
1565 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1566 #ifdef DEBUG
1567 /*
1568 * Check that this is the right record: delete didn't
1569 * mangle the cursor.
1570 */
1571 {
1572 xfs_agblock_t xxbno;
1573 xfs_extlen_t xxlen;
1574
1575 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1576 &i)))
1577 goto error0;
1578 XFS_WANT_CORRUPTED_GOTO(
1579 i == 1 && xxbno == ltbno && xxlen == ltlen,
1580 error0);
1581 }
1582 #endif
1583 /*
1584 * Update remaining by-block entry to the new, joined block.
1585 */
1586 nbno = ltbno;
1587 nlen = len + ltlen + gtlen;
1588 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1589 goto error0;
1590 }
1591 /*
1592 * Have only a left contiguous neighbor.
1593 * Merge it together with the new freespace.
1594 */
1595 else if (haveleft) {
1596 /*
1597 * Delete the old by-size entry on the left.
1598 */
1599 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1600 goto error0;
1601 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1602 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1603 goto error0;
1604 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1605 /*
1606 * Back up the by-block cursor to the left neighbor, and
1607 * update its length.
1608 */
1609 if ((error = xfs_alloc_decrement(bno_cur, 0, &i)))
1610 goto error0;
1611 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1612 nbno = ltbno;
1613 nlen = len + ltlen;
1614 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1615 goto error0;
1616 }
1617 /*
1618 * Have only a right contiguous neighbor.
1619 * Merge it together with the new freespace.
1620 */
1621 else if (haveright) {
1622 /*
1623 * Delete the old by-size entry on the right.
1624 */
1625 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1626 goto error0;
1627 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1628 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1629 goto error0;
1630 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1631 /*
1632 * Update the starting block and length of the right
1633 * neighbor in the by-block tree.
1634 */
1635 nbno = bno;
1636 nlen = len + gtlen;
1637 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1638 goto error0;
1639 }
1640 /*
1641 * No contiguous neighbors.
1642 * Insert the new freespace into the by-block tree.
1643 */
1644 else {
1645 nbno = bno;
1646 nlen = len;
1647 if ((error = xfs_alloc_insert(bno_cur, &i)))
1648 goto error0;
1649 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1650 }
1651 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1652 bno_cur = NULL;
1653 /*
1654 * In all cases we need to insert the new freespace in the by-size tree.
1655 */
1656 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1657 goto error0;
1658 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1659 if ((error = xfs_alloc_insert(cnt_cur, &i)))
1660 goto error0;
1661 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1662 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1663 cnt_cur = NULL;
1664 /*
1665 * Update the freespace totals in the ag and superblock.
1666 */
1667 {
1668 xfs_agf_t *agf;
1669 xfs_perag_t *pag; /* per allocation group data */
1670
1671 agf = XFS_BUF_TO_AGF(agbp);
1672 pag = &mp->m_perag[agno];
1673 INT_MOD(agf->agf_freeblks, ARCH_CONVERT, len);
1674 xfs_trans_agblocks_delta(tp, len);
1675 pag->pagf_freeblks += len;
1676 XFS_WANT_CORRUPTED_GOTO(
1677 INT_GET(agf->agf_freeblks, ARCH_CONVERT)
1678 <= INT_GET(agf->agf_length, ARCH_CONVERT),
1679 error0);
1680 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1681 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1682 if (!isfl)
1683 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1684 XFS_STATS_INC(xfsstats.xs_freex);
1685 XFS_STATS_ADD(xfsstats.xs_freeb, len);
1686 }
1687 TRACE_FREE(haveleft ?
1688 (haveright ? "both" : "left") :
1689 (haveright ? "right" : "none"),
1690 agno, bno, len, isfl);
1691
1692 /*
1693 * Since blocks move to the free list without the coordination
1694 * used in xfs_bmap_finish, we can't allow block to be available
1695 * for reallocation and non-transaction writing (user data)
1696 * until we know that the transaction that moved it to the free
1697 * list is permanently on disk. We track the blocks by declaring
1698 * these blocks as "busy"; the busy list is maintained on a per-ag
1699 * basis and each transaction records which entries should be removed
1700 * when the iclog commits to disk. If a busy block is allocated,
1701 * the iclog is pushed up to the LSN that freed the block.
1702 */
1703 xfs_alloc_mark_busy(tp, agno, bno, len);
1704 return 0;
1705
1706 error0:
1707 TRACE_FREE("error", agno, bno, len, isfl);
1708 if (bno_cur)
1709 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1710 if (cnt_cur)
1711 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1712 return error;
1713 }
1714
1715 /*
1716 * Visible (exported) allocation/free functions.
1717 * Some of these are used just by xfs_alloc_btree.c and this file.
1718 */
1719
1720 /*
1721 * Compute and fill in value of m_ag_maxlevels.
1722 */
1723 void
1724 xfs_alloc_compute_maxlevels(
1725 xfs_mount_t *mp) /* file system mount structure */
1726 {
1727 int level;
1728 uint maxblocks;
1729 uint maxleafents;
1730 int minleafrecs;
1731 int minnoderecs;
1732
1733 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1734 minleafrecs = mp->m_alloc_mnr[0];
1735 minnoderecs = mp->m_alloc_mnr[1];
1736 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1737 for (level = 1; maxblocks > 1; level++)
1738 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1739 mp->m_ag_maxlevels = level;
1740 }
1741
1742 /*
1743 * Decide whether to use this allocation group for this allocation.
1744 * If so, fix up the btree freelist's size.
1745 * This is external so mkfs can call it, too.
1746 */
1747 int /* error */
1748 xfs_alloc_fix_freelist(
1749 xfs_alloc_arg_t *args, /* allocation argument structure */
1750 int flags) /* XFS_ALLOC_FLAG_... */
1751 {
1752 xfs_buf_t *agbp; /* agf buffer pointer */
1753 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1754 xfs_buf_t *agflbp;/* agfl buffer pointer */
1755 xfs_agblock_t bno; /* freelist block */
1756 xfs_extlen_t delta; /* new blocks needed in freelist */
1757 int error; /* error result code */
1758 xfs_extlen_t longest;/* longest extent in allocation group */
1759 xfs_mount_t *mp; /* file system mount point structure */
1760 xfs_extlen_t need; /* total blocks needed in freelist */
1761 xfs_perag_t *pag; /* per-ag information structure */
1762 xfs_alloc_arg_t targs; /* local allocation arguments */
1763 xfs_trans_t *tp; /* transaction pointer */
1764
1765 mp = args->mp;
1766
1767 pag = args->pag;
1768 tp = args->tp;
1769 if (!pag->pagf_init) {
1770 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1771 &agbp)))
1772 return error;
1773 if (!pag->pagf_init) {
1774 args->agbp = NULL;
1775 return 0;
1776 }
1777 } else
1778 agbp = NULL;
1779
1780 /* If this is a metadata prefered pag and we are user data
1781 * then try somewhere else if we are not being asked to
1782 * try harder at this point
1783 */
1784 if (pag->pagf_metadata && args->userdata && flags) {
1785 args->agbp = NULL;
1786 return 0;
1787 }
1788
1789 need = XFS_MIN_FREELIST_PAG(pag, mp);
1790 delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
1791 /*
1792 * If it looks like there isn't a long enough extent, or enough
1793 * total blocks, reject it.
1794 */
1795 longest = (pag->pagf_longest > delta) ?
1796 (pag->pagf_longest - delta) :
1797 (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
1798 if (args->minlen + args->alignment + args->minalignslop - 1 > longest ||
1799 (args->minleft &&
1800 (int)(pag->pagf_freeblks + pag->pagf_flcount -
1801 need - args->total) <
1802 (int)args->minleft)) {
1803 if (agbp)
1804 xfs_trans_brelse(tp, agbp);
1805 args->agbp = NULL;
1806 return 0;
1807 }
1808 /*
1809 * Get the a.g. freespace buffer.
1810 * Can fail if we're not blocking on locks, and it's held.
1811 */
1812 if (agbp == NULL) {
1813 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1814 &agbp)))
1815 return error;
1816 if (agbp == NULL) {
1817 args->agbp = NULL;
1818 return 0;
1819 }
1820 }
1821 /*
1822 * Figure out how many blocks we should have in the freelist.
1823 */
1824 agf = XFS_BUF_TO_AGF(agbp);
1825 need = XFS_MIN_FREELIST(agf, mp);
1826 delta = need > INT_GET(agf->agf_flcount, ARCH_CONVERT) ?
1827 (need - INT_GET(agf->agf_flcount, ARCH_CONVERT)) : 0;
1828 /*
1829 * If there isn't enough total or single-extent, reject it.
1830 */
1831 longest = INT_GET(agf->agf_longest, ARCH_CONVERT);
1832 longest = (longest > delta) ? (longest - delta) :
1833 (INT_GET(agf->agf_flcount, ARCH_CONVERT) > 0 || longest > 0);
1834 if (args->minlen + args->alignment + args->minalignslop - 1 > longest ||
1835 (args->minleft &&
1836 (int)(INT_GET(agf->agf_freeblks, ARCH_CONVERT) +
1837 INT_GET(agf->agf_flcount, ARCH_CONVERT) - need - args->total) <
1838 (int)args->minleft)) {
1839 xfs_trans_brelse(tp, agbp);
1840 args->agbp = NULL;
1841 return 0;
1842 }
1843 /*
1844 * Make the freelist shorter if it's too long.
1845 */
1846 while (INT_GET(agf->agf_flcount, ARCH_CONVERT) > need) {
1847 xfs_buf_t *bp;
1848
1849 if ((error = xfs_alloc_get_freelist(tp, agbp, &bno)))
1850 return error;
1851 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1852 return error;
1853 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1854 xfs_trans_binval(tp, bp);
1855 }
1856 /*
1857 * Initialize the args structure.
1858 */
1859 targs.tp = tp;
1860 targs.mp = mp;
1861 targs.agbp = agbp;
1862 targs.agno = args->agno;
1863 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1864 targs.minalignslop = 0;
1865 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1866 targs.type = XFS_ALLOCTYPE_THIS_AG;
1867 targs.pag = pag;
1868 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1869 return error;
1870 /*
1871 * Make the freelist longer if it's too short.
1872 */
1873 while (INT_GET(agf->agf_flcount, ARCH_CONVERT) < need) {
1874 targs.agbno = 0;
1875 targs.maxlen = need - INT_GET(agf->agf_flcount, ARCH_CONVERT);
1876 /*
1877 * Allocate as many blocks as possible at once.
1878 */
1879 if ((error = xfs_alloc_ag_vextent(&targs)))
1880 return error;
1881 /*
1882 * Stop if we run out. Won't happen if callers are obeying
1883 * the restrictions correctly. Can happen for free calls
1884 * on a completely full ag.
1885 */
1886 if (targs.agbno == NULLAGBLOCK)
1887 break;
1888 /*
1889 * Put each allocated block on the list.
1890 */
1891 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
1892 if ((error = xfs_alloc_put_freelist(tp, agbp, agflbp,
1893 bno)))
1894 return error;
1895 }
1896 }
1897 args->agbp = agbp;
1898 return 0;
1899 }
1900
1901 /*
1902 * Get a block from the freelist.
1903 * Returns with the buffer for the block gotten.
1904 */
1905 int /* error */
1906 xfs_alloc_get_freelist(
1907 xfs_trans_t *tp, /* transaction pointer */
1908 xfs_buf_t *agbp, /* buffer containing the agf structure */
1909 xfs_agblock_t *bnop) /* block address retrieved from freelist */
1910 {
1911 xfs_agf_t *agf; /* a.g. freespace structure */
1912 xfs_agfl_t *agfl; /* a.g. freelist structure */
1913 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
1914 xfs_agblock_t bno; /* block number returned */
1915 int error;
1916 #ifdef XFS_ALLOC_TRACE
1917 static char fname[] = "xfs_alloc_get_freelist";
1918 #endif
1919 xfs_mount_t *mp; /* mount structure */
1920 xfs_perag_t *pag; /* per allocation group data */
1921
1922 agf = XFS_BUF_TO_AGF(agbp);
1923 /*
1924 * Freelist is empty, give up.
1925 */
1926 if (INT_ISZERO(agf->agf_flcount, ARCH_CONVERT)) {
1927 *bnop = NULLAGBLOCK;
1928 return 0;
1929 }
1930 /*
1931 * Read the array of free blocks.
1932 */
1933 mp = tp->t_mountp;
1934 if ((error = xfs_alloc_read_agfl(mp, tp,
1935 INT_GET(agf->agf_seqno, ARCH_CONVERT), &agflbp)))
1936 return error;
1937 agfl = XFS_BUF_TO_AGFL(agflbp);
1938 /*
1939 * Get the block number and update the data structures.
1940 */
1941 bno = INT_GET(agfl->agfl_bno[INT_GET(agf->agf_flfirst, ARCH_CONVERT)], ARCH_CONVERT);
1942 INT_MOD(agf->agf_flfirst, ARCH_CONVERT, 1);
1943 xfs_trans_brelse(tp, agflbp);
1944 if (INT_GET(agf->agf_flfirst, ARCH_CONVERT) == XFS_AGFL_SIZE(mp))
1945 INT_ZERO(agf->agf_flfirst, ARCH_CONVERT);
1946 pag = &mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)];
1947 INT_MOD(agf->agf_flcount, ARCH_CONVERT, -1);
1948 xfs_trans_agflist_delta(tp, -1);
1949 pag->pagf_flcount--;
1950 TRACE_MODAGF(NULL, agf, XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT);
1951 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT);
1952 *bnop = bno;
1953
1954 /*
1955 * As blocks are freed, they are added to the per-ag busy list
1956 * and remain there until the freeing transaction is committed to
1957 * disk. Now that we have allocated blocks, this list must be
1958 * searched to see if a block is being reused. If one is, then
1959 * the freeing transaction must be pushed to disk NOW by forcing
1960 * to disk all iclogs up that transaction's LSN.
1961 */
1962 xfs_alloc_search_busy(tp, INT_GET(agf->agf_seqno, ARCH_CONVERT), bno, 1);
1963 return 0;
1964 }
1965
1966 /*
1967 * Log the given fields from the agf structure.
1968 */
1969 void
1970 xfs_alloc_log_agf(
1971 xfs_trans_t *tp, /* transaction pointer */
1972 xfs_buf_t *bp, /* buffer for a.g. freelist header */
1973 int fields) /* mask of fields to be logged (XFS_AGF_...) */
1974 {
1975 int first; /* first byte offset */
1976 int last; /* last byte offset */
1977 static const short offsets[] = {
1978 offsetof(xfs_agf_t, agf_magicnum),
1979 offsetof(xfs_agf_t, agf_versionnum),
1980 offsetof(xfs_agf_t, agf_seqno),
1981 offsetof(xfs_agf_t, agf_length),
1982 offsetof(xfs_agf_t, agf_roots[0]),
1983 offsetof(xfs_agf_t, agf_levels[0]),
1984 offsetof(xfs_agf_t, agf_flfirst),
1985 offsetof(xfs_agf_t, agf_fllast),
1986 offsetof(xfs_agf_t, agf_flcount),
1987 offsetof(xfs_agf_t, agf_freeblks),
1988 offsetof(xfs_agf_t, agf_longest),
1989 sizeof(xfs_agf_t)
1990 };
1991
1992 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
1993 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
1994 }
1995
1996 /*
1997 * Interface for inode allocation to force the pag data to be initialized.
1998 */
1999 int /* error */
2000 xfs_alloc_pagf_init(
2001 xfs_mount_t *mp, /* file system mount structure */
2002 xfs_trans_t *tp, /* transaction pointer */
2003 xfs_agnumber_t agno, /* allocation group number */
2004 int flags) /* XFS_ALLOC_FLAGS_... */
2005 {
2006 xfs_buf_t *bp;
2007 int error;
2008
2009 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2010 return error;
2011 if (bp)
2012 xfs_trans_brelse(tp, bp);
2013 return 0;
2014 }
2015
2016 /*
2017 * Put the block on the freelist for the allocation group.
2018 */
2019 int /* error */
2020 xfs_alloc_put_freelist(
2021 xfs_trans_t *tp, /* transaction pointer */
2022 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2023 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2024 xfs_agblock_t bno) /* block being freed */
2025 {
2026 xfs_agf_t *agf; /* a.g. freespace structure */
2027 xfs_agfl_t *agfl; /* a.g. free block array */
2028 xfs_agblock_t *blockp;/* pointer to array entry */
2029 int error;
2030 #ifdef XFS_ALLOC_TRACE
2031 static char fname[] = "xfs_alloc_put_freelist";
2032 #endif
2033 xfs_mount_t *mp; /* mount structure */
2034 xfs_perag_t *pag; /* per allocation group data */
2035
2036 agf = XFS_BUF_TO_AGF(agbp);
2037 mp = tp->t_mountp;
2038
2039 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2040 INT_GET(agf->agf_seqno, ARCH_CONVERT), &agflbp)))
2041 return error;
2042 agfl = XFS_BUF_TO_AGFL(agflbp);
2043 INT_MOD(agf->agf_fllast, ARCH_CONVERT, 1);
2044 if (INT_GET(agf->agf_fllast, ARCH_CONVERT) == XFS_AGFL_SIZE(mp))
2045 INT_ZERO(agf->agf_fllast, ARCH_CONVERT);
2046 pag = &mp->m_perag[INT_GET(agf->agf_seqno, ARCH_CONVERT)];
2047 INT_MOD(agf->agf_flcount, ARCH_CONVERT, 1);
2048 xfs_trans_agflist_delta(tp, 1);
2049 pag->pagf_flcount++;
2050 ASSERT(INT_GET(agf->agf_flcount, ARCH_CONVERT) <= XFS_AGFL_SIZE(mp));
2051 blockp = &agfl->agfl_bno[INT_GET(agf->agf_fllast, ARCH_CONVERT)];
2052 INT_SET(*blockp, ARCH_CONVERT, bno);
2053 TRACE_MODAGF(NULL, agf, XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
2054 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
2055 xfs_trans_log_buf(tp, agflbp,
2056 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2057 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2058 sizeof(xfs_agblock_t) - 1));
2059 return 0;
2060 }
2061
2062 /*
2063 * Read in the allocation group header (free/alloc section).
2064 */
2065 int /* error */
2066 xfs_alloc_read_agf(
2067 xfs_mount_t *mp, /* mount point structure */
2068 xfs_trans_t *tp, /* transaction pointer */
2069 xfs_agnumber_t agno, /* allocation group number */
2070 int flags, /* XFS_ALLOC_FLAG_... */
2071 xfs_buf_t **bpp) /* buffer for the ag freelist header */
2072 {
2073 xfs_agf_t *agf; /* ag freelist header */
2074 int agf_ok; /* set if agf is consistent */
2075 xfs_buf_t *bp; /* return value */
2076 xfs_perag_t *pag; /* per allocation group data */
2077 int error;
2078
2079 ASSERT(agno != NULLAGNUMBER);
2080 error = xfs_trans_read_buf(
2081 mp, tp, mp->m_ddev_targp,
2082 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2083 XFS_FSS_TO_BB(mp, 1),
2084 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
2085 &bp);
2086 if (error)
2087 return error;
2088 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
2089 if (!bp) {
2090 *bpp = NULL;
2091 return 0;
2092 }
2093 /*
2094 * Validate the magic number of the agf block.
2095 */
2096 agf = XFS_BUF_TO_AGF(bp);
2097 agf_ok =
2098 INT_GET(agf->agf_magicnum, ARCH_CONVERT) == XFS_AGF_MAGIC &&
2099 XFS_AGF_GOOD_VERSION(
2100 INT_GET(agf->agf_versionnum, ARCH_CONVERT)) &&
2101 INT_GET(agf->agf_freeblks, ARCH_CONVERT) <=
2102 INT_GET(agf->agf_length, ARCH_CONVERT) &&
2103 INT_GET(agf->agf_flfirst, ARCH_CONVERT) < XFS_AGFL_SIZE(mp) &&
2104 INT_GET(agf->agf_fllast, ARCH_CONVERT) < XFS_AGFL_SIZE(mp) &&
2105 INT_GET(agf->agf_flcount, ARCH_CONVERT) <= XFS_AGFL_SIZE(mp);
2106 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2107 XFS_RANDOM_ALLOC_READ_AGF))) {
2108 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2109 XFS_ERRLEVEL_LOW, mp, agf);
2110 xfs_trans_brelse(tp, bp);
2111 return XFS_ERROR(EFSCORRUPTED);
2112 }
2113 pag = &mp->m_perag[agno];
2114 if (!pag->pagf_init) {
2115 pag->pagf_freeblks = INT_GET(agf->agf_freeblks, ARCH_CONVERT);
2116 pag->pagf_flcount = INT_GET(agf->agf_flcount, ARCH_CONVERT);
2117 pag->pagf_longest = INT_GET(agf->agf_longest, ARCH_CONVERT);
2118 pag->pagf_levels[XFS_BTNUM_BNOi] =
2119 INT_GET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT);
2120 pag->pagf_levels[XFS_BTNUM_CNTi] =
2121 INT_GET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT);
2122 spinlock_init(&pag->pagb_lock, "xfspagb");
2123 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2124 sizeof(xfs_perag_busy_t), KM_SLEEP);
2125 pag->pagf_init = 1;
2126 }
2127 #ifdef DEBUG
2128 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2129 ASSERT(pag->pagf_freeblks == INT_GET(agf->agf_freeblks, ARCH_CONVERT));
2130 ASSERT(pag->pagf_flcount == INT_GET(agf->agf_flcount, ARCH_CONVERT));
2131 ASSERT(pag->pagf_longest == INT_GET(agf->agf_longest, ARCH_CONVERT));
2132 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2133 INT_GET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT));
2134 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2135 INT_GET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT));
2136 }
2137 #endif
2138 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
2139 *bpp = bp;
2140 return 0;
2141 }
2142
2143 /*
2144 * Allocate an extent (variable-size).
2145 * Depending on the allocation type, we either look in a single allocation
2146 * group or loop over the allocation groups to find the result.
2147 */
2148 int /* error */
2149 xfs_alloc_vextent(
2150 xfs_alloc_arg_t *args) /* allocation argument structure */
2151 {
2152 xfs_agblock_t agsize; /* allocation group size */
2153 int error;
2154 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2155 #ifdef XFS_ALLOC_TRACE
2156 static char fname[] = "xfs_alloc_vextent";
2157 #endif
2158 xfs_extlen_t minleft;/* minimum left value, temp copy */
2159 xfs_mount_t *mp; /* mount structure pointer */
2160 xfs_agnumber_t sagno; /* starting allocation group number */
2161 xfs_alloctype_t type; /* input allocation type */
2162 int bump_rotor = 0;
2163 int no_min = 0;
2164
2165 mp = args->mp;
2166 type = args->otype = args->type;
2167 args->agbno = NULLAGBLOCK;
2168 /*
2169 * Just fix this up, for the case where the last a.g. is shorter
2170 * (or there's only one a.g.) and the caller couldn't easily figure
2171 * that out (xfs_bmap_alloc).
2172 */
2173 agsize = mp->m_sb.sb_agblocks;
2174 if (args->maxlen > agsize)
2175 args->maxlen = agsize;
2176 if (args->alignment == 0)
2177 args->alignment = 1;
2178 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2179 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2180 ASSERT(args->minlen <= args->maxlen);
2181 ASSERT(args->minlen <= agsize);
2182 ASSERT(args->mod < args->prod);
2183 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2184 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2185 args->minlen > args->maxlen || args->minlen > agsize ||
2186 args->mod >= args->prod) {
2187 args->fsbno = NULLFSBLOCK;
2188 TRACE_ALLOC("badargs", args);
2189 return 0;
2190 }
2191 minleft = args->minleft;
2192
2193 switch (type) {
2194 case XFS_ALLOCTYPE_THIS_AG:
2195 case XFS_ALLOCTYPE_NEAR_BNO:
2196 case XFS_ALLOCTYPE_THIS_BNO:
2197 /*
2198 * These three force us into a single a.g.
2199 */
2200 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2201 down_read(&mp->m_peraglock);
2202 args->pag = &mp->m_perag[args->agno];
2203 args->minleft = 0;
2204 error = xfs_alloc_fix_freelist(args, 0);
2205 args->minleft = minleft;
2206 if (error) {
2207 TRACE_ALLOC("nofix", args);
2208 goto error0;
2209 }
2210 if (!args->agbp) {
2211 up_read(&mp->m_peraglock);
2212 TRACE_ALLOC("noagbp", args);
2213 break;
2214 }
2215 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2216 if ((error = xfs_alloc_ag_vextent(args)))
2217 goto error0;
2218 up_read(&mp->m_peraglock);
2219 break;
2220 case XFS_ALLOCTYPE_START_BNO:
2221 /*
2222 * Try near allocation first, then anywhere-in-ag after
2223 * the first a.g. fails.
2224 */
2225 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2226 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2227 args->fsbno = XFS_AGB_TO_FSB(mp, mp->m_agfrotor, 0);
2228 bump_rotor = 1;
2229 }
2230 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2231 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2232 /* FALLTHROUGH */
2233 case XFS_ALLOCTYPE_ANY_AG:
2234 case XFS_ALLOCTYPE_START_AG:
2235 case XFS_ALLOCTYPE_FIRST_AG:
2236 /*
2237 * Rotate through the allocation groups looking for a winner.
2238 */
2239 if (type == XFS_ALLOCTYPE_ANY_AG) {
2240 /*
2241 * Start with the last place we left off.
2242 */
2243 args->agno = sagno = mp->m_agfrotor;
2244 args->type = XFS_ALLOCTYPE_THIS_AG;
2245 flags = XFS_ALLOC_FLAG_TRYLOCK;
2246 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2247 /*
2248 * Start with allocation group given by bno.
2249 */
2250 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2251 args->type = XFS_ALLOCTYPE_THIS_AG;
2252 sagno = 0;
2253 flags = 0;
2254 } else {
2255 if (type == XFS_ALLOCTYPE_START_AG)
2256 args->type = XFS_ALLOCTYPE_THIS_AG;
2257 /*
2258 * Start with the given allocation group.
2259 */
2260 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2261 flags = XFS_ALLOC_FLAG_TRYLOCK;
2262 }
2263 /*
2264 * Loop over allocation groups twice; first time with
2265 * trylock set, second time without.
2266 */
2267 down_read(&mp->m_peraglock);
2268 for (;;) {
2269 args->pag = &mp->m_perag[args->agno];
2270 if (no_min) args->minleft = 0;
2271 error = xfs_alloc_fix_freelist(args, flags);
2272 args->minleft = minleft;
2273 if (error) {
2274 TRACE_ALLOC("nofix", args);
2275 goto error0;
2276 }
2277 /*
2278 * If we get a buffer back then the allocation will fly.
2279 */
2280 if (args->agbp) {
2281 if ((error = xfs_alloc_ag_vextent(args)))
2282 goto error0;
2283 break;
2284 }
2285 TRACE_ALLOC("loopfailed", args);
2286 /*
2287 * Didn't work, figure out the next iteration.
2288 */
2289 if (args->agno == sagno &&
2290 type == XFS_ALLOCTYPE_START_BNO)
2291 args->type = XFS_ALLOCTYPE_THIS_AG;
2292 if (++(args->agno) == mp->m_sb.sb_agcount)
2293 args->agno = 0;
2294 /*
2295 * Reached the starting a.g., must either be done
2296 * or switch to non-trylock mode.
2297 */
2298 if (args->agno == sagno) {
2299 if (no_min == 1) {
2300 args->agbno = NULLAGBLOCK;
2301 TRACE_ALLOC("allfailed", args);
2302 break;
2303 }
2304 if (flags == 0) {
2305 no_min = 1;
2306 } else {
2307 flags = 0;
2308 if (type == XFS_ALLOCTYPE_START_BNO) {
2309 args->agbno = XFS_FSB_TO_AGBNO(mp,
2310 args->fsbno);
2311 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2312 }
2313 }
2314 }
2315 }
2316 up_read(&mp->m_peraglock);
2317 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG))
2318 mp->m_agfrotor = (args->agno + 1) % mp->m_sb.sb_agcount;
2319 break;
2320 default:
2321 ASSERT(0);
2322 /* NOTREACHED */
2323 }
2324 if (args->agbno == NULLAGBLOCK)
2325 args->fsbno = NULLFSBLOCK;
2326 else {
2327 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2328 #ifdef DEBUG
2329 ASSERT(args->len >= args->minlen);
2330 ASSERT(args->len <= args->maxlen);
2331 ASSERT(args->agbno % args->alignment == 0);
2332 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2333 args->len);
2334 #endif
2335 }
2336 return 0;
2337 error0:
2338 up_read(&mp->m_peraglock);
2339 return error;
2340 }
2341
2342 /*
2343 * Free an extent.
2344 * Just break up the extent address and hand off to xfs_free_ag_extent
2345 * after fixing up the freelist.
2346 */
2347 int /* error */
2348 xfs_free_extent(
2349 xfs_trans_t *tp, /* transaction pointer */
2350 xfs_fsblock_t bno, /* starting block number of extent */
2351 xfs_extlen_t len) /* length of extent */
2352 {
2353 #ifdef DEBUG
2354 xfs_agf_t *agf; /* a.g. freespace header */
2355 #endif
2356 xfs_alloc_arg_t args; /* allocation argument structure */
2357 int error;
2358
2359 ASSERT(len != 0);
2360 args.tp = tp;
2361 args.mp = tp->t_mountp;
2362 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2363 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2364 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
2365 args.alignment = 1;
2366 args.minlen = args.minleft = args.minalignslop = 0;
2367 down_read(&args.mp->m_peraglock);
2368 args.pag = &args.mp->m_perag[args.agno];
2369 if ((error = xfs_alloc_fix_freelist(&args, 0)))
2370 goto error0;
2371 #ifdef DEBUG
2372 ASSERT(args.agbp != NULL);
2373 agf = XFS_BUF_TO_AGF(args.agbp);
2374 ASSERT(args.agbno + len <= INT_GET(agf->agf_length, ARCH_CONVERT));
2375 #endif
2376 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno,
2377 len, 0);
2378 error0:
2379 up_read(&args.mp->m_peraglock);
2380 return error;
2381 }