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