]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_alloc.c
08d98f3aa2b70be9c2a0d7761c8c60be05025549
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_alloc.c
1 /*
2 * Copyright (c) 2000-2002,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 #include "libxfs_priv.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
30 #include "xfs_rmap.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_cksum.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_ag_resv.h"
37
38 struct workqueue_struct *xfs_alloc_wq;
39
40 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
41
42 #define XFSA_FIXUP_BNO_OK 1
43 #define XFSA_FIXUP_CNT_OK 2
44
45 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
46 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
47 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
48 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
49 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
50
51 xfs_extlen_t
52 xfs_prealloc_blocks(
53 struct xfs_mount *mp)
54 {
55 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
56 return XFS_RMAP_BLOCK(mp) + 1;
57 if (xfs_sb_version_hasfinobt(&mp->m_sb))
58 return XFS_FIBT_BLOCK(mp) + 1;
59 return XFS_IBT_BLOCK(mp) + 1;
60 }
61
62 /*
63 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
64 * AGF buffer (PV 947395), we place constraints on the relationship among
65 * actual allocations for data blocks, freelist blocks, and potential file data
66 * bmap btree blocks. However, these restrictions may result in no actual space
67 * allocated for a delayed extent, for example, a data block in a certain AG is
68 * allocated but there is no additional block for the additional bmap btree
69 * block due to a split of the bmap btree of the file. The result of this may
70 * lead to an infinite loop when the file gets flushed to disk and all delayed
71 * extents need to be actually allocated. To get around this, we explicitly set
72 * aside a few blocks which will not be reserved in delayed allocation.
73 *
74 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
75 * potential split of the file's bmap btree.
76 */
77 unsigned int
78 xfs_alloc_set_aside(
79 struct xfs_mount *mp)
80 {
81 unsigned int blocks;
82
83 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
84 return blocks;
85 }
86
87 /*
88 * When deciding how much space to allocate out of an AG, we limit the
89 * allocation maximum size to the size the AG. However, we cannot use all the
90 * blocks in the AG - some are permanently used by metadata. These
91 * blocks are generally:
92 * - the AG superblock, AGF, AGI and AGFL
93 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
94 * the AGI free inode and rmap btree root blocks.
95 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
96 * - the rmapbt root block
97 *
98 * The AG headers are sector sized, so the amount of space they take up is
99 * dependent on filesystem geometry. The others are all single blocks.
100 */
101 unsigned int
102 xfs_alloc_ag_max_usable(
103 struct xfs_mount *mp)
104 {
105 unsigned int blocks;
106
107 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
108 blocks += XFS_ALLOC_AGFL_RESERVE;
109 blocks += 3; /* AGF, AGI btree root blocks */
110 if (xfs_sb_version_hasfinobt(&mp->m_sb))
111 blocks++; /* finobt root block */
112 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
113 blocks++; /* rmap root block */
114
115 return mp->m_sb.sb_agblocks - blocks;
116 }
117
118 /*
119 * Lookup the record equal to [bno, len] in the btree given by cur.
120 */
121 STATIC int /* error */
122 xfs_alloc_lookup_eq(
123 struct xfs_btree_cur *cur, /* btree cursor */
124 xfs_agblock_t bno, /* starting block of extent */
125 xfs_extlen_t len, /* length of extent */
126 int *stat) /* success/failure */
127 {
128 cur->bc_rec.a.ar_startblock = bno;
129 cur->bc_rec.a.ar_blockcount = len;
130 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
131 }
132
133 /*
134 * Lookup the first record greater than or equal to [bno, len]
135 * in the btree given by cur.
136 */
137 int /* error */
138 xfs_alloc_lookup_ge(
139 struct xfs_btree_cur *cur, /* btree cursor */
140 xfs_agblock_t bno, /* starting block of extent */
141 xfs_extlen_t len, /* length of extent */
142 int *stat) /* success/failure */
143 {
144 cur->bc_rec.a.ar_startblock = bno;
145 cur->bc_rec.a.ar_blockcount = len;
146 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
147 }
148
149 /*
150 * Lookup the first record less than or equal to [bno, len]
151 * in the btree given by cur.
152 */
153 static int /* error */
154 xfs_alloc_lookup_le(
155 struct xfs_btree_cur *cur, /* btree cursor */
156 xfs_agblock_t bno, /* starting block of extent */
157 xfs_extlen_t len, /* length of extent */
158 int *stat) /* success/failure */
159 {
160 cur->bc_rec.a.ar_startblock = bno;
161 cur->bc_rec.a.ar_blockcount = len;
162 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
163 }
164
165 /*
166 * Update the record referred to by cur to the value given
167 * by [bno, len].
168 * This either works (return 0) or gets an EFSCORRUPTED error.
169 */
170 STATIC int /* error */
171 xfs_alloc_update(
172 struct xfs_btree_cur *cur, /* btree cursor */
173 xfs_agblock_t bno, /* starting block of extent */
174 xfs_extlen_t len) /* length of extent */
175 {
176 union xfs_btree_rec rec;
177
178 rec.alloc.ar_startblock = cpu_to_be32(bno);
179 rec.alloc.ar_blockcount = cpu_to_be32(len);
180 return xfs_btree_update(cur, &rec);
181 }
182
183 /*
184 * Get the data from the pointed-to record.
185 */
186 int /* error */
187 xfs_alloc_get_rec(
188 struct xfs_btree_cur *cur, /* btree cursor */
189 xfs_agblock_t *bno, /* output: starting block of extent */
190 xfs_extlen_t *len, /* output: length of extent */
191 int *stat) /* output: success/failure */
192 {
193 union xfs_btree_rec *rec;
194 int error;
195
196 error = xfs_btree_get_rec(cur, &rec, stat);
197 if (!error && *stat == 1) {
198 *bno = be32_to_cpu(rec->alloc.ar_startblock);
199 *len = be32_to_cpu(rec->alloc.ar_blockcount);
200 }
201 return error;
202 }
203
204 /*
205 * Compute aligned version of the found extent.
206 * Takes alignment and min length into account.
207 */
208 STATIC void
209 xfs_alloc_compute_aligned(
210 xfs_alloc_arg_t *args, /* allocation argument structure */
211 xfs_agblock_t foundbno, /* starting block in found extent */
212 xfs_extlen_t foundlen, /* length in found extent */
213 xfs_agblock_t *resbno, /* result block number */
214 xfs_extlen_t *reslen) /* result length */
215 {
216 xfs_agblock_t bno;
217 xfs_extlen_t len;
218 xfs_extlen_t diff;
219
220 /* Trim busy sections out of found extent */
221 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
222
223 /*
224 * If we have a largish extent that happens to start before min_agbno,
225 * see if we can shift it into range...
226 */
227 if (bno < args->min_agbno && bno + len > args->min_agbno) {
228 diff = args->min_agbno - bno;
229 if (len > diff) {
230 bno += diff;
231 len -= diff;
232 }
233 }
234
235 if (args->alignment > 1 && len >= args->minlen) {
236 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
237
238 diff = aligned_bno - bno;
239
240 *resbno = aligned_bno;
241 *reslen = diff >= len ? 0 : len - diff;
242 } else {
243 *resbno = bno;
244 *reslen = len;
245 }
246 }
247
248 /*
249 * Compute best start block and diff for "near" allocations.
250 * freelen >= wantlen already checked by caller.
251 */
252 STATIC xfs_extlen_t /* difference value (absolute) */
253 xfs_alloc_compute_diff(
254 xfs_agblock_t wantbno, /* target starting block */
255 xfs_extlen_t wantlen, /* target length */
256 xfs_extlen_t alignment, /* target alignment */
257 char userdata, /* are we allocating data? */
258 xfs_agblock_t freebno, /* freespace's starting block */
259 xfs_extlen_t freelen, /* freespace's length */
260 xfs_agblock_t *newbnop) /* result: best start block from free */
261 {
262 xfs_agblock_t freeend; /* end of freespace extent */
263 xfs_agblock_t newbno1; /* return block number */
264 xfs_agblock_t newbno2; /* other new block number */
265 xfs_extlen_t newlen1=0; /* length with newbno1 */
266 xfs_extlen_t newlen2=0; /* length with newbno2 */
267 xfs_agblock_t wantend; /* end of target extent */
268
269 ASSERT(freelen >= wantlen);
270 freeend = freebno + freelen;
271 wantend = wantbno + wantlen;
272 /*
273 * We want to allocate from the start of a free extent if it is past
274 * the desired block or if we are allocating user data and the free
275 * extent is before desired block. The second case is there to allow
276 * for contiguous allocation from the remaining free space if the file
277 * grows in the short term.
278 */
279 if (freebno >= wantbno || (userdata && freeend < wantend)) {
280 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
281 newbno1 = NULLAGBLOCK;
282 } else if (freeend >= wantend && alignment > 1) {
283 newbno1 = roundup(wantbno, alignment);
284 newbno2 = newbno1 - alignment;
285 if (newbno1 >= freeend)
286 newbno1 = NULLAGBLOCK;
287 else
288 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
289 if (newbno2 < freebno)
290 newbno2 = NULLAGBLOCK;
291 else
292 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
293 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
294 if (newlen1 < newlen2 ||
295 (newlen1 == newlen2 &&
296 XFS_ABSDIFF(newbno1, wantbno) >
297 XFS_ABSDIFF(newbno2, wantbno)))
298 newbno1 = newbno2;
299 } else if (newbno2 != NULLAGBLOCK)
300 newbno1 = newbno2;
301 } else if (freeend >= wantend) {
302 newbno1 = wantbno;
303 } else if (alignment > 1) {
304 newbno1 = roundup(freeend - wantlen, alignment);
305 if (newbno1 > freeend - wantlen &&
306 newbno1 - alignment >= freebno)
307 newbno1 -= alignment;
308 else if (newbno1 >= freeend)
309 newbno1 = NULLAGBLOCK;
310 } else
311 newbno1 = freeend - wantlen;
312 *newbnop = newbno1;
313 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
314 }
315
316 /*
317 * Fix up the length, based on mod and prod.
318 * len should be k * prod + mod for some k.
319 * If len is too small it is returned unchanged.
320 * If len hits maxlen it is left alone.
321 */
322 STATIC void
323 xfs_alloc_fix_len(
324 xfs_alloc_arg_t *args) /* allocation argument structure */
325 {
326 xfs_extlen_t k;
327 xfs_extlen_t rlen;
328
329 ASSERT(args->mod < args->prod);
330 rlen = args->len;
331 ASSERT(rlen >= args->minlen);
332 ASSERT(rlen <= args->maxlen);
333 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
334 (args->mod == 0 && rlen < args->prod))
335 return;
336 k = rlen % args->prod;
337 if (k == args->mod)
338 return;
339 if (k > args->mod)
340 rlen = rlen - (k - args->mod);
341 else
342 rlen = rlen - args->prod + (args->mod - k);
343 /* casts to (int) catch length underflows */
344 if ((int)rlen < (int)args->minlen)
345 return;
346 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
347 ASSERT(rlen % args->prod == args->mod);
348 args->len = rlen;
349 }
350
351 /*
352 * Fix up length if there is too little space left in the a.g.
353 * Return 1 if ok, 0 if too little, should give up.
354 */
355 STATIC int
356 xfs_alloc_fix_minleft(
357 xfs_alloc_arg_t *args) /* allocation argument structure */
358 {
359 xfs_agf_t *agf; /* a.g. freelist header */
360 int diff; /* free space difference */
361
362 if (args->minleft == 0)
363 return 1;
364 agf = XFS_BUF_TO_AGF(args->agbp);
365 diff = be32_to_cpu(agf->agf_freeblks)
366 - args->len - args->minleft;
367 if (diff >= 0)
368 return 1;
369 args->len += diff; /* shrink the allocated space */
370 /* casts to (int) catch length underflows */
371 if ((int)args->len >= (int)args->minlen)
372 return 1;
373 args->agbno = NULLAGBLOCK;
374 return 0;
375 }
376
377 /*
378 * Update the two btrees, logically removing from freespace the extent
379 * starting at rbno, rlen blocks. The extent is contained within the
380 * actual (current) free extent fbno for flen blocks.
381 * Flags are passed in indicating whether the cursors are set to the
382 * relevant records.
383 */
384 STATIC int /* error code */
385 xfs_alloc_fixup_trees(
386 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
387 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
388 xfs_agblock_t fbno, /* starting block of free extent */
389 xfs_extlen_t flen, /* length of free extent */
390 xfs_agblock_t rbno, /* starting block of returned extent */
391 xfs_extlen_t rlen, /* length of returned extent */
392 int flags) /* flags, XFSA_FIXUP_... */
393 {
394 int error; /* error code */
395 int i; /* operation results */
396 xfs_agblock_t nfbno1; /* first new free startblock */
397 xfs_agblock_t nfbno2; /* second new free startblock */
398 xfs_extlen_t nflen1=0; /* first new free length */
399 xfs_extlen_t nflen2=0; /* second new free length */
400 struct xfs_mount *mp;
401
402 mp = cnt_cur->bc_mp;
403
404 /*
405 * Look up the record in the by-size tree if necessary.
406 */
407 if (flags & XFSA_FIXUP_CNT_OK) {
408 #ifdef DEBUG
409 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
410 return error;
411 XFS_WANT_CORRUPTED_RETURN(mp,
412 i == 1 && nfbno1 == fbno && nflen1 == flen);
413 #endif
414 } else {
415 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
416 return error;
417 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
418 }
419 /*
420 * Look up the record in the by-block tree if necessary.
421 */
422 if (flags & XFSA_FIXUP_BNO_OK) {
423 #ifdef DEBUG
424 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
425 return error;
426 XFS_WANT_CORRUPTED_RETURN(mp,
427 i == 1 && nfbno1 == fbno && nflen1 == flen);
428 #endif
429 } else {
430 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
431 return error;
432 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
433 }
434
435 #ifdef DEBUG
436 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
437 struct xfs_btree_block *bnoblock;
438 struct xfs_btree_block *cntblock;
439
440 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
441 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
442
443 XFS_WANT_CORRUPTED_RETURN(mp,
444 bnoblock->bb_numrecs == cntblock->bb_numrecs);
445 }
446 #endif
447
448 /*
449 * Deal with all four cases: the allocated record is contained
450 * within the freespace record, so we can have new freespace
451 * at either (or both) end, or no freespace remaining.
452 */
453 if (rbno == fbno && rlen == flen)
454 nfbno1 = nfbno2 = NULLAGBLOCK;
455 else if (rbno == fbno) {
456 nfbno1 = rbno + rlen;
457 nflen1 = flen - rlen;
458 nfbno2 = NULLAGBLOCK;
459 } else if (rbno + rlen == fbno + flen) {
460 nfbno1 = fbno;
461 nflen1 = flen - rlen;
462 nfbno2 = NULLAGBLOCK;
463 } else {
464 nfbno1 = fbno;
465 nflen1 = rbno - fbno;
466 nfbno2 = rbno + rlen;
467 nflen2 = (fbno + flen) - nfbno2;
468 }
469 /*
470 * Delete the entry from the by-size btree.
471 */
472 if ((error = xfs_btree_delete(cnt_cur, &i)))
473 return error;
474 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
475 /*
476 * Add new by-size btree entry(s).
477 */
478 if (nfbno1 != NULLAGBLOCK) {
479 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
480 return error;
481 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
482 if ((error = xfs_btree_insert(cnt_cur, &i)))
483 return error;
484 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
485 }
486 if (nfbno2 != NULLAGBLOCK) {
487 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
488 return error;
489 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
490 if ((error = xfs_btree_insert(cnt_cur, &i)))
491 return error;
492 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
493 }
494 /*
495 * Fix up the by-block btree entry(s).
496 */
497 if (nfbno1 == NULLAGBLOCK) {
498 /*
499 * No remaining freespace, just delete the by-block tree entry.
500 */
501 if ((error = xfs_btree_delete(bno_cur, &i)))
502 return error;
503 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
504 } else {
505 /*
506 * Update the by-block entry to start later|be shorter.
507 */
508 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
509 return error;
510 }
511 if (nfbno2 != NULLAGBLOCK) {
512 /*
513 * 2 resulting free entries, need to add one.
514 */
515 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
516 return error;
517 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
518 if ((error = xfs_btree_insert(bno_cur, &i)))
519 return error;
520 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
521 }
522 return 0;
523 }
524
525 static bool
526 xfs_agfl_verify(
527 struct xfs_buf *bp)
528 {
529 struct xfs_mount *mp = bp->b_target->bt_mount;
530 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
531 int i;
532
533 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
534 return false;
535 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
536 return false;
537 /*
538 * during growfs operations, the perag is not fully initialised,
539 * so we can't use it for any useful checking. growfs ensures we can't
540 * use it by using uncached buffers that don't have the perag attached
541 * so we can detect and avoid this problem.
542 */
543 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
544 return false;
545
546 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
547 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
548 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
549 return false;
550 }
551
552 return xfs_log_check_lsn(mp,
553 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
554 }
555
556 static void
557 xfs_agfl_read_verify(
558 struct xfs_buf *bp)
559 {
560 struct xfs_mount *mp = bp->b_target->bt_mount;
561
562 /*
563 * There is no verification of non-crc AGFLs because mkfs does not
564 * initialise the AGFL to zero or NULL. Hence the only valid part of the
565 * AGFL is what the AGF says is active. We can't get to the AGF, so we
566 * can't verify just those entries are valid.
567 */
568 if (!xfs_sb_version_hascrc(&mp->m_sb))
569 return;
570
571 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
572 xfs_buf_ioerror(bp, -EFSBADCRC);
573 else if (!xfs_agfl_verify(bp))
574 xfs_buf_ioerror(bp, -EFSCORRUPTED);
575
576 if (bp->b_error)
577 xfs_verifier_error(bp);
578 }
579
580 static void
581 xfs_agfl_write_verify(
582 struct xfs_buf *bp)
583 {
584 struct xfs_mount *mp = bp->b_target->bt_mount;
585 struct xfs_buf_log_item *bip = bp->b_fspriv;
586
587 /* no verification of non-crc AGFLs */
588 if (!xfs_sb_version_hascrc(&mp->m_sb))
589 return;
590
591 if (!xfs_agfl_verify(bp)) {
592 xfs_buf_ioerror(bp, -EFSCORRUPTED);
593 xfs_verifier_error(bp);
594 return;
595 }
596
597 if (bip)
598 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
599
600 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
601 }
602
603 const struct xfs_buf_ops xfs_agfl_buf_ops = {
604 .name = "xfs_agfl",
605 .verify_read = xfs_agfl_read_verify,
606 .verify_write = xfs_agfl_write_verify,
607 };
608
609 /*
610 * Read in the allocation group free block array.
611 */
612 STATIC int /* error */
613 xfs_alloc_read_agfl(
614 xfs_mount_t *mp, /* mount point structure */
615 xfs_trans_t *tp, /* transaction pointer */
616 xfs_agnumber_t agno, /* allocation group number */
617 xfs_buf_t **bpp) /* buffer for the ag free block array */
618 {
619 xfs_buf_t *bp; /* return value */
620 int error;
621
622 ASSERT(agno != NULLAGNUMBER);
623 error = xfs_trans_read_buf(
624 mp, tp, mp->m_ddev_targp,
625 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
626 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
627 if (error)
628 return error;
629 xfs_buf_set_ref(bp, XFS_AGFL_REF);
630 *bpp = bp;
631 return 0;
632 }
633
634 STATIC int
635 xfs_alloc_update_counters(
636 struct xfs_trans *tp,
637 struct xfs_perag *pag,
638 struct xfs_buf *agbp,
639 long len)
640 {
641 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
642
643 pag->pagf_freeblks += len;
644 be32_add_cpu(&agf->agf_freeblks, len);
645
646 xfs_trans_agblocks_delta(tp, len);
647 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
648 be32_to_cpu(agf->agf_length)))
649 return -EFSCORRUPTED;
650
651 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
652 return 0;
653 }
654
655 /*
656 * Allocation group level functions.
657 */
658
659 /*
660 * Allocate a variable extent in the allocation group agno.
661 * Type and bno are used to determine where in the allocation group the
662 * extent will start.
663 * Extent's length (returned in *len) will be between minlen and maxlen,
664 * and of the form k * prod + mod unless there's nothing that large.
665 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
666 */
667 STATIC int /* error */
668 xfs_alloc_ag_vextent(
669 xfs_alloc_arg_t *args) /* argument structure for allocation */
670 {
671 int error=0;
672 xfs_extlen_t reservation;
673 xfs_extlen_t oldmax;
674
675 ASSERT(args->minlen > 0);
676 ASSERT(args->maxlen > 0);
677 ASSERT(args->minlen <= args->maxlen);
678 ASSERT(args->mod < args->prod);
679 ASSERT(args->alignment > 0);
680
681 /*
682 * Clamp maxlen to the amount of free space minus any reservations
683 * that have been made.
684 */
685 oldmax = args->maxlen;
686 reservation = xfs_ag_resv_needed(args->pag, args->resv);
687 if (args->maxlen > args->pag->pagf_freeblks - reservation)
688 args->maxlen = args->pag->pagf_freeblks - reservation;
689 if (args->maxlen == 0) {
690 args->agbno = NULLAGBLOCK;
691 args->maxlen = oldmax;
692 return 0;
693 }
694
695 /*
696 * Branch to correct routine based on the type.
697 */
698 args->wasfromfl = 0;
699 switch (args->type) {
700 case XFS_ALLOCTYPE_THIS_AG:
701 error = xfs_alloc_ag_vextent_size(args);
702 break;
703 case XFS_ALLOCTYPE_NEAR_BNO:
704 error = xfs_alloc_ag_vextent_near(args);
705 break;
706 case XFS_ALLOCTYPE_THIS_BNO:
707 error = xfs_alloc_ag_vextent_exact(args);
708 break;
709 default:
710 ASSERT(0);
711 /* NOTREACHED */
712 }
713
714 args->maxlen = oldmax;
715
716 if (error || args->agbno == NULLAGBLOCK)
717 return error;
718
719 ASSERT(args->len >= args->minlen);
720 ASSERT(args->len <= args->maxlen);
721 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
722 ASSERT(args->agbno % args->alignment == 0);
723
724 /* if not file data, insert new block into the reverse map btree */
725 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
726 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
727 args->agbno, args->len, &args->oinfo);
728 if (error)
729 return error;
730 }
731
732 if (!args->wasfromfl) {
733 error = xfs_alloc_update_counters(args->tp, args->pag,
734 args->agbp,
735 -((long)(args->len)));
736 if (error)
737 return error;
738
739 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
740 args->agbno, args->len));
741 }
742
743 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
744
745 XFS_STATS_INC(args->mp, xs_allocx);
746 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
747 return error;
748 }
749
750 /*
751 * Allocate a variable extent at exactly agno/bno.
752 * Extent's length (returned in *len) will be between minlen and maxlen,
753 * and of the form k * prod + mod unless there's nothing that large.
754 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
755 */
756 STATIC int /* error */
757 xfs_alloc_ag_vextent_exact(
758 xfs_alloc_arg_t *args) /* allocation argument structure */
759 {
760 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
761 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
762 int error;
763 xfs_agblock_t fbno; /* start block of found extent */
764 xfs_extlen_t flen; /* length of found extent */
765 xfs_agblock_t tbno; /* start block of trimmed extent */
766 xfs_extlen_t tlen; /* length of trimmed extent */
767 xfs_agblock_t tend; /* end block of trimmed extent */
768 int i; /* success/failure of operation */
769
770 ASSERT(args->alignment == 1);
771
772 /*
773 * Allocate/initialize a cursor for the by-number freespace btree.
774 */
775 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
776 args->agno, XFS_BTNUM_BNO);
777
778 /*
779 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
780 * Look for the closest free block <= bno, it must contain bno
781 * if any free block does.
782 */
783 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
784 if (error)
785 goto error0;
786 if (!i)
787 goto not_found;
788
789 /*
790 * Grab the freespace record.
791 */
792 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
793 if (error)
794 goto error0;
795 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
796 ASSERT(fbno <= args->agbno);
797
798 /*
799 * Check for overlapping busy extents.
800 */
801 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
802
803 /*
804 * Give up if the start of the extent is busy, or the freespace isn't
805 * long enough for the minimum request.
806 */
807 if (tbno > args->agbno)
808 goto not_found;
809 if (tlen < args->minlen)
810 goto not_found;
811 tend = tbno + tlen;
812 if (tend < args->agbno + args->minlen)
813 goto not_found;
814
815 /*
816 * End of extent will be smaller of the freespace end and the
817 * maximal requested end.
818 *
819 * Fix the length according to mod and prod if given.
820 */
821 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
822 - args->agbno;
823 xfs_alloc_fix_len(args);
824 if (!xfs_alloc_fix_minleft(args))
825 goto not_found;
826
827 ASSERT(args->agbno + args->len <= tend);
828
829 /*
830 * We are allocating agbno for args->len
831 * Allocate/initialize a cursor for the by-size btree.
832 */
833 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
834 args->agno, XFS_BTNUM_CNT);
835 ASSERT(args->agbno + args->len <=
836 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
837 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
838 args->len, XFSA_FIXUP_BNO_OK);
839 if (error) {
840 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
841 goto error0;
842 }
843
844 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
845 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
846
847 args->wasfromfl = 0;
848 trace_xfs_alloc_exact_done(args);
849 return 0;
850
851 not_found:
852 /* Didn't find it, return null. */
853 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
854 args->agbno = NULLAGBLOCK;
855 trace_xfs_alloc_exact_notfound(args);
856 return 0;
857
858 error0:
859 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
860 trace_xfs_alloc_exact_error(args);
861 return error;
862 }
863
864 /*
865 * Search the btree in a given direction via the search cursor and compare
866 * the records found against the good extent we've already found.
867 */
868 STATIC int
869 xfs_alloc_find_best_extent(
870 struct xfs_alloc_arg *args, /* allocation argument structure */
871 struct xfs_btree_cur **gcur, /* good cursor */
872 struct xfs_btree_cur **scur, /* searching cursor */
873 xfs_agblock_t gdiff, /* difference for search comparison */
874 xfs_agblock_t *sbno, /* extent found by search */
875 xfs_extlen_t *slen, /* extent length */
876 xfs_agblock_t *sbnoa, /* aligned extent found by search */
877 xfs_extlen_t *slena, /* aligned extent length */
878 int dir) /* 0 = search right, 1 = search left */
879 {
880 xfs_agblock_t new;
881 xfs_agblock_t sdiff;
882 int error;
883 int i;
884
885 /* The good extent is perfect, no need to search. */
886 if (!gdiff)
887 goto out_use_good;
888
889 /*
890 * Look until we find a better one, run out of space or run off the end.
891 */
892 do {
893 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
894 if (error)
895 goto error0;
896 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
897 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
898
899 /*
900 * The good extent is closer than this one.
901 */
902 if (!dir) {
903 if (*sbnoa > args->max_agbno)
904 goto out_use_good;
905 if (*sbnoa >= args->agbno + gdiff)
906 goto out_use_good;
907 } else {
908 if (*sbnoa < args->min_agbno)
909 goto out_use_good;
910 if (*sbnoa <= args->agbno - gdiff)
911 goto out_use_good;
912 }
913
914 /*
915 * Same distance, compare length and pick the best.
916 */
917 if (*slena >= args->minlen) {
918 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
919 xfs_alloc_fix_len(args);
920
921 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
922 args->alignment,
923 args->userdata, *sbnoa,
924 *slena, &new);
925
926 /*
927 * Choose closer size and invalidate other cursor.
928 */
929 if (sdiff < gdiff)
930 goto out_use_search;
931 goto out_use_good;
932 }
933
934 if (!dir)
935 error = xfs_btree_increment(*scur, 0, &i);
936 else
937 error = xfs_btree_decrement(*scur, 0, &i);
938 if (error)
939 goto error0;
940 } while (i);
941
942 out_use_good:
943 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
944 *scur = NULL;
945 return 0;
946
947 out_use_search:
948 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
949 *gcur = NULL;
950 return 0;
951
952 error0:
953 /* caller invalidates cursors */
954 return error;
955 }
956
957 /*
958 * Allocate a variable extent near bno in the allocation group agno.
959 * Extent's length (returned in len) will be between minlen and maxlen,
960 * and of the form k * prod + mod unless there's nothing that large.
961 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
962 */
963 STATIC int /* error */
964 xfs_alloc_ag_vextent_near(
965 xfs_alloc_arg_t *args) /* allocation argument structure */
966 {
967 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
968 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
969 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
970 xfs_agblock_t gtbno; /* start bno of right side entry */
971 xfs_agblock_t gtbnoa; /* aligned ... */
972 xfs_extlen_t gtdiff; /* difference to right side entry */
973 xfs_extlen_t gtlen; /* length of right side entry */
974 xfs_extlen_t gtlena; /* aligned ... */
975 xfs_agblock_t gtnew; /* useful start bno of right side */
976 int error; /* error code */
977 int i; /* result code, temporary */
978 int j; /* result code, temporary */
979 xfs_agblock_t ltbno; /* start bno of left side entry */
980 xfs_agblock_t ltbnoa; /* aligned ... */
981 xfs_extlen_t ltdiff; /* difference to left side entry */
982 xfs_extlen_t ltlen; /* length of left side entry */
983 xfs_extlen_t ltlena; /* aligned ... */
984 xfs_agblock_t ltnew; /* useful start bno of left side */
985 xfs_extlen_t rlen; /* length of returned extent */
986 int forced = 0;
987 #ifdef DEBUG
988 /*
989 * Randomly don't execute the first algorithm.
990 */
991 int dofirst; /* set to do first algorithm */
992
993 dofirst = prandom_u32() & 1;
994 #endif
995
996 /* handle unitialized agbno range so caller doesn't have to */
997 if (!args->min_agbno && !args->max_agbno)
998 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
999 ASSERT(args->min_agbno <= args->max_agbno);
1000
1001 /* clamp agbno to the range if it's outside */
1002 if (args->agbno < args->min_agbno)
1003 args->agbno = args->min_agbno;
1004 if (args->agbno > args->max_agbno)
1005 args->agbno = args->max_agbno;
1006
1007 restart:
1008 bno_cur_lt = NULL;
1009 bno_cur_gt = NULL;
1010 ltlen = 0;
1011 gtlena = 0;
1012 ltlena = 0;
1013
1014 /*
1015 * Get a cursor for the by-size btree.
1016 */
1017 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1018 args->agno, XFS_BTNUM_CNT);
1019
1020 /*
1021 * See if there are any free extents as big as maxlen.
1022 */
1023 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1024 goto error0;
1025 /*
1026 * If none, then pick up the last entry in the tree unless the
1027 * tree is empty.
1028 */
1029 if (!i) {
1030 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1031 &ltlen, &i)))
1032 goto error0;
1033 if (i == 0 || ltlen == 0) {
1034 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1035 trace_xfs_alloc_near_noentry(args);
1036 return 0;
1037 }
1038 ASSERT(i == 1);
1039 }
1040 args->wasfromfl = 0;
1041
1042 /*
1043 * First algorithm.
1044 * If the requested extent is large wrt the freespaces available
1045 * in this a.g., then the cursor will be pointing to a btree entry
1046 * near the right edge of the tree. If it's in the last btree leaf
1047 * block, then we just examine all the entries in that block
1048 * that are big enough, and pick the best one.
1049 * This is written as a while loop so we can break out of it,
1050 * but we never loop back to the top.
1051 */
1052 while (xfs_btree_islastblock(cnt_cur, 0)) {
1053 xfs_extlen_t bdiff;
1054 int besti=0;
1055 xfs_extlen_t blen=0;
1056 xfs_agblock_t bnew=0;
1057
1058 #ifdef DEBUG
1059 if (dofirst)
1060 break;
1061 #endif
1062 /*
1063 * Start from the entry that lookup found, sequence through
1064 * all larger free blocks. If we're actually pointing at a
1065 * record smaller than maxlen, go to the start of this block,
1066 * and skip all those smaller than minlen.
1067 */
1068 if (ltlen || args->alignment > 1) {
1069 cnt_cur->bc_ptrs[0] = 1;
1070 do {
1071 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1072 &ltlen, &i)))
1073 goto error0;
1074 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1075 if (ltlen >= args->minlen)
1076 break;
1077 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
1078 goto error0;
1079 } while (i);
1080 ASSERT(ltlen >= args->minlen);
1081 if (!i)
1082 break;
1083 }
1084 i = cnt_cur->bc_ptrs[0];
1085 for (j = 1, blen = 0, bdiff = 0;
1086 !error && j && (blen < args->maxlen || bdiff > 0);
1087 error = xfs_btree_increment(cnt_cur, 0, &j)) {
1088 /*
1089 * For each entry, decide if it's better than
1090 * the previous best entry.
1091 */
1092 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1093 goto error0;
1094 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1095 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1096 &ltbnoa, &ltlena);
1097 if (ltlena < args->minlen)
1098 continue;
1099 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1100 continue;
1101 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1102 xfs_alloc_fix_len(args);
1103 ASSERT(args->len >= args->minlen);
1104 if (args->len < blen)
1105 continue;
1106 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1107 args->alignment, args->userdata, ltbnoa,
1108 ltlena, &ltnew);
1109 if (ltnew != NULLAGBLOCK &&
1110 (args->len > blen || ltdiff < bdiff)) {
1111 bdiff = ltdiff;
1112 bnew = ltnew;
1113 blen = args->len;
1114 besti = cnt_cur->bc_ptrs[0];
1115 }
1116 }
1117 /*
1118 * It didn't work. We COULD be in a case where
1119 * there's a good record somewhere, so try again.
1120 */
1121 if (blen == 0)
1122 break;
1123 /*
1124 * Point at the best entry, and retrieve it again.
1125 */
1126 cnt_cur->bc_ptrs[0] = besti;
1127 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1128 goto error0;
1129 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1130 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1131 args->len = blen;
1132 if (!xfs_alloc_fix_minleft(args)) {
1133 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1134 trace_xfs_alloc_near_nominleft(args);
1135 return 0;
1136 }
1137 blen = args->len;
1138 /*
1139 * We are allocating starting at bnew for blen blocks.
1140 */
1141 args->agbno = bnew;
1142 ASSERT(bnew >= ltbno);
1143 ASSERT(bnew + blen <= ltbno + ltlen);
1144 /*
1145 * Set up a cursor for the by-bno tree.
1146 */
1147 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1148 args->agbp, args->agno, XFS_BTNUM_BNO);
1149 /*
1150 * Fix up the btree entries.
1151 */
1152 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1153 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1154 goto error0;
1155 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1156 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1157
1158 trace_xfs_alloc_near_first(args);
1159 return 0;
1160 }
1161 /*
1162 * Second algorithm.
1163 * Search in the by-bno tree to the left and to the right
1164 * simultaneously, until in each case we find a space big enough,
1165 * or run into the edge of the tree. When we run into the edge,
1166 * we deallocate that cursor.
1167 * If both searches succeed, we compare the two spaces and pick
1168 * the better one.
1169 * With alignment, it's possible for both to fail; the upper
1170 * level algorithm that picks allocation groups for allocations
1171 * is not supposed to do this.
1172 */
1173 /*
1174 * Allocate and initialize the cursor for the leftward search.
1175 */
1176 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1177 args->agno, XFS_BTNUM_BNO);
1178 /*
1179 * Lookup <= bno to find the leftward search's starting point.
1180 */
1181 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1182 goto error0;
1183 if (!i) {
1184 /*
1185 * Didn't find anything; use this cursor for the rightward
1186 * search.
1187 */
1188 bno_cur_gt = bno_cur_lt;
1189 bno_cur_lt = NULL;
1190 }
1191 /*
1192 * Found something. Duplicate the cursor for the rightward search.
1193 */
1194 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1195 goto error0;
1196 /*
1197 * Increment the cursor, so we will point at the entry just right
1198 * of the leftward entry if any, or to the leftmost entry.
1199 */
1200 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1201 goto error0;
1202 if (!i) {
1203 /*
1204 * It failed, there are no rightward entries.
1205 */
1206 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1207 bno_cur_gt = NULL;
1208 }
1209 /*
1210 * Loop going left with the leftward cursor, right with the
1211 * rightward cursor, until either both directions give up or
1212 * we find an entry at least as big as minlen.
1213 */
1214 do {
1215 if (bno_cur_lt) {
1216 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1217 goto error0;
1218 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1219 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1220 &ltbnoa, &ltlena);
1221 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
1222 break;
1223 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1224 goto error0;
1225 if (!i || ltbnoa < args->min_agbno) {
1226 xfs_btree_del_cursor(bno_cur_lt,
1227 XFS_BTREE_NOERROR);
1228 bno_cur_lt = NULL;
1229 }
1230 }
1231 if (bno_cur_gt) {
1232 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1233 goto error0;
1234 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1235 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1236 &gtbnoa, &gtlena);
1237 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
1238 break;
1239 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1240 goto error0;
1241 if (!i || gtbnoa > args->max_agbno) {
1242 xfs_btree_del_cursor(bno_cur_gt,
1243 XFS_BTREE_NOERROR);
1244 bno_cur_gt = NULL;
1245 }
1246 }
1247 } while (bno_cur_lt || bno_cur_gt);
1248
1249 /*
1250 * Got both cursors still active, need to find better entry.
1251 */
1252 if (bno_cur_lt && bno_cur_gt) {
1253 if (ltlena >= args->minlen) {
1254 /*
1255 * Left side is good, look for a right side entry.
1256 */
1257 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1258 xfs_alloc_fix_len(args);
1259 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1260 args->alignment, args->userdata, ltbnoa,
1261 ltlena, &ltnew);
1262
1263 error = xfs_alloc_find_best_extent(args,
1264 &bno_cur_lt, &bno_cur_gt,
1265 ltdiff, &gtbno, &gtlen,
1266 &gtbnoa, &gtlena,
1267 0 /* search right */);
1268 } else {
1269 ASSERT(gtlena >= args->minlen);
1270
1271 /*
1272 * Right side is good, look for a left side entry.
1273 */
1274 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1275 xfs_alloc_fix_len(args);
1276 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1277 args->alignment, args->userdata, gtbnoa,
1278 gtlena, &gtnew);
1279
1280 error = xfs_alloc_find_best_extent(args,
1281 &bno_cur_gt, &bno_cur_lt,
1282 gtdiff, &ltbno, &ltlen,
1283 &ltbnoa, &ltlena,
1284 1 /* search left */);
1285 }
1286
1287 if (error)
1288 goto error0;
1289 }
1290
1291 /*
1292 * If we couldn't get anything, give up.
1293 */
1294 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1295 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1296
1297 if (!forced++) {
1298 trace_xfs_alloc_near_busy(args);
1299 xfs_log_force(args->mp, XFS_LOG_SYNC);
1300 goto restart;
1301 }
1302 trace_xfs_alloc_size_neither(args);
1303 args->agbno = NULLAGBLOCK;
1304 return 0;
1305 }
1306
1307 /*
1308 * At this point we have selected a freespace entry, either to the
1309 * left or to the right. If it's on the right, copy all the
1310 * useful variables to the "left" set so we only have one
1311 * copy of this code.
1312 */
1313 if (bno_cur_gt) {
1314 bno_cur_lt = bno_cur_gt;
1315 bno_cur_gt = NULL;
1316 ltbno = gtbno;
1317 ltbnoa = gtbnoa;
1318 ltlen = gtlen;
1319 ltlena = gtlena;
1320 j = 1;
1321 } else
1322 j = 0;
1323
1324 /*
1325 * Fix up the length and compute the useful address.
1326 */
1327 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1328 xfs_alloc_fix_len(args);
1329 if (!xfs_alloc_fix_minleft(args)) {
1330 trace_xfs_alloc_near_nominleft(args);
1331 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1332 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1333 return 0;
1334 }
1335 rlen = args->len;
1336 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1337 args->userdata, ltbnoa, ltlena, &ltnew);
1338 ASSERT(ltnew >= ltbno);
1339 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1340 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1341 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
1342 args->agbno = ltnew;
1343
1344 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1345 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1346 goto error0;
1347
1348 if (j)
1349 trace_xfs_alloc_near_greater(args);
1350 else
1351 trace_xfs_alloc_near_lesser(args);
1352
1353 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1354 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1355 return 0;
1356
1357 error0:
1358 trace_xfs_alloc_near_error(args);
1359 if (cnt_cur != NULL)
1360 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1361 if (bno_cur_lt != NULL)
1362 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1363 if (bno_cur_gt != NULL)
1364 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1365 return error;
1366 }
1367
1368 /*
1369 * Allocate a variable extent anywhere in the allocation group agno.
1370 * Extent's length (returned in len) will be between minlen and maxlen,
1371 * and of the form k * prod + mod unless there's nothing that large.
1372 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1373 */
1374 STATIC int /* error */
1375 xfs_alloc_ag_vextent_size(
1376 xfs_alloc_arg_t *args) /* allocation argument structure */
1377 {
1378 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1379 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1380 int error; /* error result */
1381 xfs_agblock_t fbno; /* start of found freespace */
1382 xfs_extlen_t flen; /* length of found freespace */
1383 int i; /* temp status variable */
1384 xfs_agblock_t rbno; /* returned block number */
1385 xfs_extlen_t rlen; /* length of returned extent */
1386 int forced = 0;
1387
1388 restart:
1389 /*
1390 * Allocate and initialize a cursor for the by-size btree.
1391 */
1392 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1393 args->agno, XFS_BTNUM_CNT);
1394 bno_cur = NULL;
1395
1396 /*
1397 * Look for an entry >= maxlen+alignment-1 blocks.
1398 */
1399 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1400 args->maxlen + args->alignment - 1, &i)))
1401 goto error0;
1402
1403 /*
1404 * If none or we have busy extents that we cannot allocate from, then
1405 * we have to settle for a smaller extent. In the case that there are
1406 * no large extents, this will return the last entry in the tree unless
1407 * the tree is empty. In the case that there are only busy large
1408 * extents, this will return the largest small extent unless there
1409 * are no smaller extents available.
1410 */
1411 if (!i || forced > 1) {
1412 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1413 &fbno, &flen, &i);
1414 if (error)
1415 goto error0;
1416 if (i == 0 || flen == 0) {
1417 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1418 trace_xfs_alloc_size_noentry(args);
1419 return 0;
1420 }
1421 ASSERT(i == 1);
1422 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1423 } else {
1424 /*
1425 * Search for a non-busy extent that is large enough.
1426 * If we are at low space, don't check, or if we fall of
1427 * the end of the btree, turn off the busy check and
1428 * restart.
1429 */
1430 for (;;) {
1431 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1432 if (error)
1433 goto error0;
1434 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1435
1436 xfs_alloc_compute_aligned(args, fbno, flen,
1437 &rbno, &rlen);
1438
1439 if (rlen >= args->maxlen)
1440 break;
1441
1442 error = xfs_btree_increment(cnt_cur, 0, &i);
1443 if (error)
1444 goto error0;
1445 if (i == 0) {
1446 /*
1447 * Our only valid extents must have been busy.
1448 * Make it unbusy by forcing the log out and
1449 * retrying. If we've been here before, forcing
1450 * the log isn't making the extents available,
1451 * which means they have probably been freed in
1452 * this transaction. In that case, we have to
1453 * give up on them and we'll attempt a minlen
1454 * allocation the next time around.
1455 */
1456 xfs_btree_del_cursor(cnt_cur,
1457 XFS_BTREE_NOERROR);
1458 trace_xfs_alloc_size_busy(args);
1459 if (!forced++)
1460 xfs_log_force(args->mp, XFS_LOG_SYNC);
1461 goto restart;
1462 }
1463 }
1464 }
1465
1466 /*
1467 * In the first case above, we got the last entry in the
1468 * by-size btree. Now we check to see if the space hits maxlen
1469 * once aligned; if not, we search left for something better.
1470 * This can't happen in the second case above.
1471 */
1472 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1473 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1474 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1475 if (rlen < args->maxlen) {
1476 xfs_agblock_t bestfbno;
1477 xfs_extlen_t bestflen;
1478 xfs_agblock_t bestrbno;
1479 xfs_extlen_t bestrlen;
1480
1481 bestrlen = rlen;
1482 bestrbno = rbno;
1483 bestflen = flen;
1484 bestfbno = fbno;
1485 for (;;) {
1486 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1487 goto error0;
1488 if (i == 0)
1489 break;
1490 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1491 &i)))
1492 goto error0;
1493 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1494 if (flen < bestrlen)
1495 break;
1496 xfs_alloc_compute_aligned(args, fbno, flen,
1497 &rbno, &rlen);
1498 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1499 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1500 (rlen <= flen && rbno + rlen <= fbno + flen),
1501 error0);
1502 if (rlen > bestrlen) {
1503 bestrlen = rlen;
1504 bestrbno = rbno;
1505 bestflen = flen;
1506 bestfbno = fbno;
1507 if (rlen == args->maxlen)
1508 break;
1509 }
1510 }
1511 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1512 &i)))
1513 goto error0;
1514 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1515 rlen = bestrlen;
1516 rbno = bestrbno;
1517 flen = bestflen;
1518 fbno = bestfbno;
1519 }
1520 args->wasfromfl = 0;
1521 /*
1522 * Fix up the length.
1523 */
1524 args->len = rlen;
1525 if (rlen < args->minlen) {
1526 if (!forced++) {
1527 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1528 trace_xfs_alloc_size_busy(args);
1529 xfs_log_force(args->mp, XFS_LOG_SYNC);
1530 goto restart;
1531 }
1532 goto out_nominleft;
1533 }
1534 xfs_alloc_fix_len(args);
1535
1536 if (!xfs_alloc_fix_minleft(args))
1537 goto out_nominleft;
1538 rlen = args->len;
1539 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
1540 /*
1541 * Allocate and initialize a cursor for the by-block tree.
1542 */
1543 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1544 args->agno, XFS_BTNUM_BNO);
1545 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1546 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1547 goto error0;
1548 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1549 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1550 cnt_cur = bno_cur = NULL;
1551 args->len = rlen;
1552 args->agbno = rbno;
1553 XFS_WANT_CORRUPTED_GOTO(args->mp,
1554 args->agbno + args->len <=
1555 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1556 error0);
1557 trace_xfs_alloc_size_done(args);
1558 return 0;
1559
1560 error0:
1561 trace_xfs_alloc_size_error(args);
1562 if (cnt_cur)
1563 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1564 if (bno_cur)
1565 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1566 return error;
1567
1568 out_nominleft:
1569 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1570 trace_xfs_alloc_size_nominleft(args);
1571 args->agbno = NULLAGBLOCK;
1572 return 0;
1573 }
1574
1575 /*
1576 * Deal with the case where only small freespaces remain.
1577 * Either return the contents of the last freespace record,
1578 * or allocate space from the freelist if there is nothing in the tree.
1579 */
1580 STATIC int /* error */
1581 xfs_alloc_ag_vextent_small(
1582 xfs_alloc_arg_t *args, /* allocation argument structure */
1583 xfs_btree_cur_t *ccur, /* by-size cursor */
1584 xfs_agblock_t *fbnop, /* result block number */
1585 xfs_extlen_t *flenp, /* result length */
1586 int *stat) /* status: 0-freelist, 1-normal/none */
1587 {
1588 struct xfs_owner_info oinfo;
1589 struct xfs_perag *pag;
1590 int error;
1591 xfs_agblock_t fbno;
1592 xfs_extlen_t flen;
1593 int i;
1594
1595 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1596 goto error0;
1597 if (i) {
1598 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1599 goto error0;
1600 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1601 }
1602 /*
1603 * Nothing in the btree, try the freelist. Make sure
1604 * to respect minleft even when pulling from the
1605 * freelist.
1606 */
1607 else if (args->minlen == 1 && args->alignment == 1 &&
1608 args->resv != XFS_AG_RESV_AGFL &&
1609 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1610 > args->minleft)) {
1611 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1612 if (error)
1613 goto error0;
1614 if (fbno != NULLAGBLOCK) {
1615 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1616 args->userdata);
1617
1618 if (args->userdata) {
1619 xfs_buf_t *bp;
1620
1621 bp = xfs_btree_get_bufs(args->mp, args->tp,
1622 args->agno, fbno, 0);
1623 xfs_trans_binval(args->tp, bp);
1624 }
1625 args->len = 1;
1626 args->agbno = fbno;
1627 XFS_WANT_CORRUPTED_GOTO(args->mp,
1628 args->agbno + args->len <=
1629 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1630 error0);
1631 args->wasfromfl = 1;
1632 trace_xfs_alloc_small_freelist(args);
1633
1634 /*
1635 * If we're feeding an AGFL block to something that
1636 * doesn't live in the free space, we need to clear
1637 * out the OWN_AG rmap and add the block back to
1638 * the AGFL per-AG reservation.
1639 */
1640 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1641 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1642 fbno, 1, &oinfo);
1643 if (error)
1644 goto error0;
1645 pag = xfs_perag_get(args->mp, args->agno);
1646 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1647 args->tp, 1);
1648 xfs_perag_put(pag);
1649
1650 *stat = 0;
1651 return 0;
1652 }
1653 /*
1654 * Nothing in the freelist.
1655 */
1656 else
1657 flen = 0;
1658 }
1659 /*
1660 * Can't allocate from the freelist for some reason.
1661 */
1662 else {
1663 fbno = NULLAGBLOCK;
1664 flen = 0;
1665 }
1666 /*
1667 * Can't do the allocation, give up.
1668 */
1669 if (flen < args->minlen) {
1670 args->agbno = NULLAGBLOCK;
1671 trace_xfs_alloc_small_notenough(args);
1672 flen = 0;
1673 }
1674 *fbnop = fbno;
1675 *flenp = flen;
1676 *stat = 1;
1677 trace_xfs_alloc_small_done(args);
1678 return 0;
1679
1680 error0:
1681 trace_xfs_alloc_small_error(args);
1682 return error;
1683 }
1684
1685 /*
1686 * Free the extent starting at agno/bno for length.
1687 */
1688 STATIC int
1689 xfs_free_ag_extent(
1690 xfs_trans_t *tp,
1691 xfs_buf_t *agbp,
1692 xfs_agnumber_t agno,
1693 xfs_agblock_t bno,
1694 xfs_extlen_t len,
1695 struct xfs_owner_info *oinfo,
1696 enum xfs_ag_resv_type type)
1697 {
1698 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1699 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1700 int error; /* error return value */
1701 xfs_agblock_t gtbno; /* start of right neighbor block */
1702 xfs_extlen_t gtlen; /* length of right neighbor block */
1703 int haveleft; /* have a left neighbor block */
1704 int haveright; /* have a right neighbor block */
1705 int i; /* temp, result code */
1706 xfs_agblock_t ltbno; /* start of left neighbor block */
1707 xfs_extlen_t ltlen; /* length of left neighbor block */
1708 xfs_mount_t *mp; /* mount point struct for filesystem */
1709 xfs_agblock_t nbno; /* new starting block of freespace */
1710 xfs_extlen_t nlen; /* new length of freespace */
1711 xfs_perag_t *pag; /* per allocation group data */
1712
1713 bno_cur = cnt_cur = NULL;
1714 mp = tp->t_mountp;
1715
1716 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1717 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1718 if (error)
1719 goto error0;
1720 }
1721
1722 /*
1723 * Allocate and initialize a cursor for the by-block btree.
1724 */
1725 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1726 /*
1727 * Look for a neighboring block on the left (lower block numbers)
1728 * that is contiguous with this space.
1729 */
1730 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1731 goto error0;
1732 if (haveleft) {
1733 /*
1734 * There is a block to our left.
1735 */
1736 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1737 goto error0;
1738 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1739 /*
1740 * It's not contiguous, though.
1741 */
1742 if (ltbno + ltlen < bno)
1743 haveleft = 0;
1744 else {
1745 /*
1746 * If this failure happens the request to free this
1747 * space was invalid, it's (partly) already free.
1748 * Very bad.
1749 */
1750 XFS_WANT_CORRUPTED_GOTO(mp,
1751 ltbno + ltlen <= bno, error0);
1752 }
1753 }
1754 /*
1755 * Look for a neighboring block on the right (higher block numbers)
1756 * that is contiguous with this space.
1757 */
1758 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1759 goto error0;
1760 if (haveright) {
1761 /*
1762 * There is a block to our right.
1763 */
1764 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1765 goto error0;
1766 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1767 /*
1768 * It's not contiguous, though.
1769 */
1770 if (bno + len < gtbno)
1771 haveright = 0;
1772 else {
1773 /*
1774 * If this failure happens the request to free this
1775 * space was invalid, it's (partly) already free.
1776 * Very bad.
1777 */
1778 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
1779 }
1780 }
1781 /*
1782 * Now allocate and initialize a cursor for the by-size tree.
1783 */
1784 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1785 /*
1786 * Have both left and right contiguous neighbors.
1787 * Merge all three into a single free block.
1788 */
1789 if (haveleft && haveright) {
1790 /*
1791 * Delete the old by-size entry on the left.
1792 */
1793 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1794 goto error0;
1795 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1796 if ((error = xfs_btree_delete(cnt_cur, &i)))
1797 goto error0;
1798 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1799 /*
1800 * Delete the old by-size entry on the right.
1801 */
1802 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1803 goto error0;
1804 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1805 if ((error = xfs_btree_delete(cnt_cur, &i)))
1806 goto error0;
1807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1808 /*
1809 * Delete the old by-block entry for the right block.
1810 */
1811 if ((error = xfs_btree_delete(bno_cur, &i)))
1812 goto error0;
1813 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1814 /*
1815 * Move the by-block cursor back to the left neighbor.
1816 */
1817 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1818 goto error0;
1819 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1820 #ifdef DEBUG
1821 /*
1822 * Check that this is the right record: delete didn't
1823 * mangle the cursor.
1824 */
1825 {
1826 xfs_agblock_t xxbno;
1827 xfs_extlen_t xxlen;
1828
1829 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1830 &i)))
1831 goto error0;
1832 XFS_WANT_CORRUPTED_GOTO(mp,
1833 i == 1 && xxbno == ltbno && xxlen == ltlen,
1834 error0);
1835 }
1836 #endif
1837 /*
1838 * Update remaining by-block entry to the new, joined block.
1839 */
1840 nbno = ltbno;
1841 nlen = len + ltlen + gtlen;
1842 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1843 goto error0;
1844 }
1845 /*
1846 * Have only a left contiguous neighbor.
1847 * Merge it together with the new freespace.
1848 */
1849 else if (haveleft) {
1850 /*
1851 * Delete the old by-size entry on the left.
1852 */
1853 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1854 goto error0;
1855 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1856 if ((error = xfs_btree_delete(cnt_cur, &i)))
1857 goto error0;
1858 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1859 /*
1860 * Back up the by-block cursor to the left neighbor, and
1861 * update its length.
1862 */
1863 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1864 goto error0;
1865 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1866 nbno = ltbno;
1867 nlen = len + ltlen;
1868 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1869 goto error0;
1870 }
1871 /*
1872 * Have only a right contiguous neighbor.
1873 * Merge it together with the new freespace.
1874 */
1875 else if (haveright) {
1876 /*
1877 * Delete the old by-size entry on the right.
1878 */
1879 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1880 goto error0;
1881 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1882 if ((error = xfs_btree_delete(cnt_cur, &i)))
1883 goto error0;
1884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1885 /*
1886 * Update the starting block and length of the right
1887 * neighbor in the by-block tree.
1888 */
1889 nbno = bno;
1890 nlen = len + gtlen;
1891 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1892 goto error0;
1893 }
1894 /*
1895 * No contiguous neighbors.
1896 * Insert the new freespace into the by-block tree.
1897 */
1898 else {
1899 nbno = bno;
1900 nlen = len;
1901 if ((error = xfs_btree_insert(bno_cur, &i)))
1902 goto error0;
1903 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1904 }
1905 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1906 bno_cur = NULL;
1907 /*
1908 * In all cases we need to insert the new freespace in the by-size tree.
1909 */
1910 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1911 goto error0;
1912 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
1913 if ((error = xfs_btree_insert(cnt_cur, &i)))
1914 goto error0;
1915 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1916 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1917 cnt_cur = NULL;
1918
1919 /*
1920 * Update the freespace totals in the ag and superblock.
1921 */
1922 pag = xfs_perag_get(mp, agno);
1923 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1924 xfs_ag_resv_free_extent(pag, type, tp, len);
1925 xfs_perag_put(pag);
1926 if (error)
1927 goto error0;
1928
1929 XFS_STATS_INC(mp, xs_freex);
1930 XFS_STATS_ADD(mp, xs_freeb, len);
1931
1932 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1933 haveleft, haveright);
1934
1935 return 0;
1936
1937 error0:
1938 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1939 -1, -1);
1940 if (bno_cur)
1941 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1942 if (cnt_cur)
1943 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1944 return error;
1945 }
1946
1947 /*
1948 * Visible (exported) allocation/free functions.
1949 * Some of these are used just by xfs_alloc_btree.c and this file.
1950 */
1951
1952 /*
1953 * Compute and fill in value of m_ag_maxlevels.
1954 */
1955 void
1956 xfs_alloc_compute_maxlevels(
1957 xfs_mount_t *mp) /* file system mount structure */
1958 {
1959 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1960 (mp->m_sb.sb_agblocks + 1) / 2);
1961 }
1962
1963 /*
1964 * Find the length of the longest extent in an AG. The 'need' parameter
1965 * specifies how much space we're going to need for the AGFL and the
1966 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1967 * other callers.
1968 */
1969 xfs_extlen_t
1970 xfs_alloc_longest_free_extent(
1971 struct xfs_mount *mp,
1972 struct xfs_perag *pag,
1973 xfs_extlen_t need,
1974 xfs_extlen_t reserved)
1975 {
1976 xfs_extlen_t delta = 0;
1977
1978 /*
1979 * If the AGFL needs a recharge, we'll have to subtract that from the
1980 * longest extent.
1981 */
1982 if (need > pag->pagf_flcount)
1983 delta = need - pag->pagf_flcount;
1984
1985 /*
1986 * If we cannot maintain others' reservations with space from the
1987 * not-longest freesp extents, we'll have to subtract /that/ from
1988 * the longest extent too.
1989 */
1990 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1991 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1992
1993 /*
1994 * If the longest extent is long enough to satisfy all the
1995 * reservations and AGFL rules in place, we can return this extent.
1996 */
1997 if (pag->pagf_longest > delta)
1998 return pag->pagf_longest - delta;
1999
2000 /* Otherwise, let the caller try for 1 block if there's space. */
2001 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2002 }
2003
2004 unsigned int
2005 xfs_alloc_min_freelist(
2006 struct xfs_mount *mp,
2007 struct xfs_perag *pag)
2008 {
2009 unsigned int min_free;
2010
2011 /* space needed by-bno freespace btree */
2012 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2013 mp->m_ag_maxlevels);
2014 /* space needed by-size freespace btree */
2015 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2016 mp->m_ag_maxlevels);
2017 /* space needed reverse mapping used space btree */
2018 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2019 min_free += min_t(unsigned int,
2020 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2021 mp->m_rmap_maxlevels);
2022
2023 return min_free;
2024 }
2025
2026 /*
2027 * Check if the operation we are fixing up the freelist for should go ahead or
2028 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2029 * is dependent on whether the size and shape of free space available will
2030 * permit the requested allocation to take place.
2031 */
2032 static bool
2033 xfs_alloc_space_available(
2034 struct xfs_alloc_arg *args,
2035 xfs_extlen_t min_free,
2036 int flags)
2037 {
2038 struct xfs_perag *pag = args->pag;
2039 xfs_extlen_t longest;
2040 xfs_extlen_t reservation; /* blocks that are still reserved */
2041 int available;
2042
2043 if (flags & XFS_ALLOC_FLAG_FREEING)
2044 return true;
2045
2046 reservation = xfs_ag_resv_needed(pag, args->resv);
2047
2048 /* do we have enough contiguous free space for the allocation? */
2049 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2050 reservation);
2051 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2052 return false;
2053
2054 /* do we have enough free space remaining for the allocation? */
2055 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
2056 reservation - min_free - args->total);
2057 if (available < (int)args->minleft || available <= 0)
2058 return false;
2059
2060 return true;
2061 }
2062
2063 /*
2064 * Decide whether to use this allocation group for this allocation.
2065 * If so, fix up the btree freelist's size.
2066 */
2067 int /* error */
2068 xfs_alloc_fix_freelist(
2069 struct xfs_alloc_arg *args, /* allocation argument structure */
2070 int flags) /* XFS_ALLOC_FLAG_... */
2071 {
2072 struct xfs_mount *mp = args->mp;
2073 struct xfs_perag *pag = args->pag;
2074 struct xfs_trans *tp = args->tp;
2075 struct xfs_buf *agbp = NULL;
2076 struct xfs_buf *agflbp = NULL;
2077 struct xfs_alloc_arg targs; /* local allocation arguments */
2078 xfs_agblock_t bno; /* freelist block */
2079 xfs_extlen_t need; /* total blocks needed in freelist */
2080 int error = 0;
2081
2082 if (!pag->pagf_init) {
2083 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2084 if (error)
2085 goto out_no_agbp;
2086 if (!pag->pagf_init) {
2087 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2088 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2089 goto out_agbp_relse;
2090 }
2091 }
2092
2093 /*
2094 * If this is a metadata preferred pag and we are user data then try
2095 * somewhere else if we are not being asked to try harder at this
2096 * point
2097 */
2098 if (pag->pagf_metadata && args->userdata &&
2099 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2100 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2101 goto out_agbp_relse;
2102 }
2103
2104 need = xfs_alloc_min_freelist(mp, pag);
2105 if (!xfs_alloc_space_available(args, need, flags))
2106 goto out_agbp_relse;
2107
2108 /*
2109 * Get the a.g. freespace buffer.
2110 * Can fail if we're not blocking on locks, and it's held.
2111 */
2112 if (!agbp) {
2113 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2114 if (error)
2115 goto out_no_agbp;
2116 if (!agbp) {
2117 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2118 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2119 goto out_no_agbp;
2120 }
2121 }
2122
2123 /* If there isn't enough total space or single-extent, reject it. */
2124 need = xfs_alloc_min_freelist(mp, pag);
2125 if (!xfs_alloc_space_available(args, need, flags))
2126 goto out_agbp_relse;
2127
2128 /*
2129 * Make the freelist shorter if it's too long.
2130 *
2131 * Note that from this point onwards, we will always release the agf and
2132 * agfl buffers on error. This handles the case where we error out and
2133 * the buffers are clean or may not have been joined to the transaction
2134 * and hence need to be released manually. If they have been joined to
2135 * the transaction, then xfs_trans_brelse() will handle them
2136 * appropriately based on the recursion count and dirty state of the
2137 * buffer.
2138 *
2139 * XXX (dgc): When we have lots of free space, does this buy us
2140 * anything other than extra overhead when we need to put more blocks
2141 * back on the free list? Maybe we should only do this when space is
2142 * getting low or the AGFL is more than half full?
2143 *
2144 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2145 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2146 * updating the rmapbt. Both flags are used in xfs_repair while we're
2147 * rebuilding the rmapbt, and neither are used by the kernel. They're
2148 * both required to ensure that rmaps are correctly recorded for the
2149 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2150 * repair/rmap.c in xfsprogs for details.
2151 */
2152 memset(&targs, 0, sizeof(targs));
2153 if (flags & XFS_ALLOC_FLAG_NORMAP)
2154 xfs_rmap_skip_owner_update(&targs.oinfo);
2155 else
2156 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2157 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
2158 struct xfs_buf *bp;
2159
2160 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2161 if (error)
2162 goto out_agbp_relse;
2163 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
2164 &targs.oinfo, XFS_AG_RESV_AGFL);
2165 if (error)
2166 goto out_agbp_relse;
2167 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2168 xfs_trans_binval(tp, bp);
2169 }
2170
2171 targs.tp = tp;
2172 targs.mp = mp;
2173 targs.agbp = agbp;
2174 targs.agno = args->agno;
2175 targs.alignment = targs.minlen = targs.prod = 1;
2176 targs.type = XFS_ALLOCTYPE_THIS_AG;
2177 targs.pag = pag;
2178 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2179 if (error)
2180 goto out_agbp_relse;
2181
2182 /* Make the freelist longer if it's too short. */
2183 while (pag->pagf_flcount < need) {
2184 targs.agbno = 0;
2185 targs.maxlen = need - pag->pagf_flcount;
2186 targs.resv = XFS_AG_RESV_AGFL;
2187
2188 /* Allocate as many blocks as possible at once. */
2189 error = xfs_alloc_ag_vextent(&targs);
2190 if (error)
2191 goto out_agflbp_relse;
2192
2193 /*
2194 * Stop if we run out. Won't happen if callers are obeying
2195 * the restrictions correctly. Can happen for free calls
2196 * on a completely full ag.
2197 */
2198 if (targs.agbno == NULLAGBLOCK) {
2199 if (flags & XFS_ALLOC_FLAG_FREEING)
2200 break;
2201 goto out_agflbp_relse;
2202 }
2203 /*
2204 * Put each allocated block on the list.
2205 */
2206 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2207 error = xfs_alloc_put_freelist(tp, agbp,
2208 agflbp, bno, 0);
2209 if (error)
2210 goto out_agflbp_relse;
2211 }
2212 }
2213 xfs_trans_brelse(tp, agflbp);
2214 args->agbp = agbp;
2215 return 0;
2216
2217 out_agflbp_relse:
2218 xfs_trans_brelse(tp, agflbp);
2219 out_agbp_relse:
2220 if (agbp)
2221 xfs_trans_brelse(tp, agbp);
2222 out_no_agbp:
2223 args->agbp = NULL;
2224 return error;
2225 }
2226
2227 /*
2228 * Get a block from the freelist.
2229 * Returns with the buffer for the block gotten.
2230 */
2231 int /* error */
2232 xfs_alloc_get_freelist(
2233 xfs_trans_t *tp, /* transaction pointer */
2234 xfs_buf_t *agbp, /* buffer containing the agf structure */
2235 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2236 int btreeblk) /* destination is a AGF btree */
2237 {
2238 xfs_agf_t *agf; /* a.g. freespace structure */
2239 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2240 xfs_agblock_t bno; /* block number returned */
2241 __be32 *agfl_bno;
2242 int error;
2243 int logflags;
2244 xfs_mount_t *mp = tp->t_mountp;
2245 xfs_perag_t *pag; /* per allocation group data */
2246
2247 /*
2248 * Freelist is empty, give up.
2249 */
2250 agf = XFS_BUF_TO_AGF(agbp);
2251 if (!agf->agf_flcount) {
2252 *bnop = NULLAGBLOCK;
2253 return 0;
2254 }
2255 /*
2256 * Read the array of free blocks.
2257 */
2258 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2259 &agflbp);
2260 if (error)
2261 return error;
2262
2263
2264 /*
2265 * Get the block number and update the data structures.
2266 */
2267 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2268 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2269 be32_add_cpu(&agf->agf_flfirst, 1);
2270 xfs_trans_brelse(tp, agflbp);
2271 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2272 agf->agf_flfirst = 0;
2273
2274 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2275 be32_add_cpu(&agf->agf_flcount, -1);
2276 xfs_trans_agflist_delta(tp, -1);
2277 pag->pagf_flcount--;
2278 xfs_perag_put(pag);
2279
2280 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2281 if (btreeblk) {
2282 be32_add_cpu(&agf->agf_btreeblks, 1);
2283 pag->pagf_btreeblks++;
2284 logflags |= XFS_AGF_BTREEBLKS;
2285 }
2286
2287 xfs_alloc_log_agf(tp, agbp, logflags);
2288 *bnop = bno;
2289
2290 return 0;
2291 }
2292
2293 /*
2294 * Log the given fields from the agf structure.
2295 */
2296 void
2297 xfs_alloc_log_agf(
2298 xfs_trans_t *tp, /* transaction pointer */
2299 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2300 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2301 {
2302 int first; /* first byte offset */
2303 int last; /* last byte offset */
2304 static const short offsets[] = {
2305 offsetof(xfs_agf_t, agf_magicnum),
2306 offsetof(xfs_agf_t, agf_versionnum),
2307 offsetof(xfs_agf_t, agf_seqno),
2308 offsetof(xfs_agf_t, agf_length),
2309 offsetof(xfs_agf_t, agf_roots[0]),
2310 offsetof(xfs_agf_t, agf_levels[0]),
2311 offsetof(xfs_agf_t, agf_flfirst),
2312 offsetof(xfs_agf_t, agf_fllast),
2313 offsetof(xfs_agf_t, agf_flcount),
2314 offsetof(xfs_agf_t, agf_freeblks),
2315 offsetof(xfs_agf_t, agf_longest),
2316 offsetof(xfs_agf_t, agf_btreeblks),
2317 offsetof(xfs_agf_t, agf_uuid),
2318 offsetof(xfs_agf_t, agf_rmap_blocks),
2319 /* needed so that we don't log the whole rest of the structure: */
2320 offsetof(xfs_agf_t, agf_spare64),
2321 sizeof(xfs_agf_t)
2322 };
2323
2324 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2325
2326 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2327
2328 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2329 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2330 }
2331
2332 /*
2333 * Interface for inode allocation to force the pag data to be initialized.
2334 */
2335 int /* error */
2336 xfs_alloc_pagf_init(
2337 xfs_mount_t *mp, /* file system mount structure */
2338 xfs_trans_t *tp, /* transaction pointer */
2339 xfs_agnumber_t agno, /* allocation group number */
2340 int flags) /* XFS_ALLOC_FLAGS_... */
2341 {
2342 xfs_buf_t *bp;
2343 int error;
2344
2345 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2346 return error;
2347 if (bp)
2348 xfs_trans_brelse(tp, bp);
2349 return 0;
2350 }
2351
2352 /*
2353 * Put the block on the freelist for the allocation group.
2354 */
2355 int /* error */
2356 xfs_alloc_put_freelist(
2357 xfs_trans_t *tp, /* transaction pointer */
2358 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2359 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2360 xfs_agblock_t bno, /* block being freed */
2361 int btreeblk) /* block came from a AGF btree */
2362 {
2363 xfs_agf_t *agf; /* a.g. freespace structure */
2364 __be32 *blockp;/* pointer to array entry */
2365 int error;
2366 int logflags;
2367 xfs_mount_t *mp; /* mount structure */
2368 xfs_perag_t *pag; /* per allocation group data */
2369 __be32 *agfl_bno;
2370 int startoff;
2371
2372 agf = XFS_BUF_TO_AGF(agbp);
2373 mp = tp->t_mountp;
2374
2375 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2376 be32_to_cpu(agf->agf_seqno), &agflbp)))
2377 return error;
2378 be32_add_cpu(&agf->agf_fllast, 1);
2379 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2380 agf->agf_fllast = 0;
2381
2382 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2383 be32_add_cpu(&agf->agf_flcount, 1);
2384 xfs_trans_agflist_delta(tp, 1);
2385 pag->pagf_flcount++;
2386
2387 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2388 if (btreeblk) {
2389 be32_add_cpu(&agf->agf_btreeblks, -1);
2390 pag->pagf_btreeblks--;
2391 logflags |= XFS_AGF_BTREEBLKS;
2392 }
2393 xfs_perag_put(pag);
2394
2395 xfs_alloc_log_agf(tp, agbp, logflags);
2396
2397 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2398
2399 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2400 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2401 *blockp = cpu_to_be32(bno);
2402 startoff = (char *)blockp - (char *)agflbp->b_addr;
2403
2404 xfs_alloc_log_agf(tp, agbp, logflags);
2405
2406 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2407 xfs_trans_log_buf(tp, agflbp, startoff,
2408 startoff + sizeof(xfs_agblock_t) - 1);
2409 return 0;
2410 }
2411
2412 static bool
2413 xfs_agf_verify(
2414 struct xfs_mount *mp,
2415 struct xfs_buf *bp)
2416 {
2417 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
2418
2419 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2420 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
2421 return false;
2422 if (!xfs_log_check_lsn(mp,
2423 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2424 return false;
2425 }
2426
2427 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2428 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2429 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2430 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2431 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2432 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2433 return false;
2434
2435 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2436 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2437 return false;
2438
2439 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2440 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2441 return false;
2442
2443 /*
2444 * during growfs operations, the perag is not fully initialised,
2445 * so we can't use it for any useful checking. growfs ensures we can't
2446 * use it by using uncached buffers that don't have the perag attached
2447 * so we can detect and avoid this problem.
2448 */
2449 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2450 return false;
2451
2452 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2453 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2454 return false;
2455
2456 return true;;
2457
2458 }
2459
2460 static void
2461 xfs_agf_read_verify(
2462 struct xfs_buf *bp)
2463 {
2464 struct xfs_mount *mp = bp->b_target->bt_mount;
2465
2466 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2467 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
2468 xfs_buf_ioerror(bp, -EFSBADCRC);
2469 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2470 XFS_ERRTAG_ALLOC_READ_AGF,
2471 XFS_RANDOM_ALLOC_READ_AGF))
2472 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2473
2474 if (bp->b_error)
2475 xfs_verifier_error(bp);
2476 }
2477
2478 static void
2479 xfs_agf_write_verify(
2480 struct xfs_buf *bp)
2481 {
2482 struct xfs_mount *mp = bp->b_target->bt_mount;
2483 struct xfs_buf_log_item *bip = bp->b_fspriv;
2484
2485 if (!xfs_agf_verify(mp, bp)) {
2486 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2487 xfs_verifier_error(bp);
2488 return;
2489 }
2490
2491 if (!xfs_sb_version_hascrc(&mp->m_sb))
2492 return;
2493
2494 if (bip)
2495 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2496
2497 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
2498 }
2499
2500 const struct xfs_buf_ops xfs_agf_buf_ops = {
2501 .name = "xfs_agf",
2502 .verify_read = xfs_agf_read_verify,
2503 .verify_write = xfs_agf_write_verify,
2504 };
2505
2506 /*
2507 * Read in the allocation group header (free/alloc section).
2508 */
2509 int /* error */
2510 xfs_read_agf(
2511 struct xfs_mount *mp, /* mount point structure */
2512 struct xfs_trans *tp, /* transaction pointer */
2513 xfs_agnumber_t agno, /* allocation group number */
2514 int flags, /* XFS_BUF_ */
2515 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2516 {
2517 int error;
2518
2519 trace_xfs_read_agf(mp, agno);
2520
2521 ASSERT(agno != NULLAGNUMBER);
2522 error = xfs_trans_read_buf(
2523 mp, tp, mp->m_ddev_targp,
2524 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2525 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2526 if (error)
2527 return error;
2528 if (!*bpp)
2529 return 0;
2530
2531 ASSERT(!(*bpp)->b_error);
2532 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2533 return 0;
2534 }
2535
2536 /*
2537 * Read in the allocation group header (free/alloc section).
2538 */
2539 int /* error */
2540 xfs_alloc_read_agf(
2541 struct xfs_mount *mp, /* mount point structure */
2542 struct xfs_trans *tp, /* transaction pointer */
2543 xfs_agnumber_t agno, /* allocation group number */
2544 int flags, /* XFS_ALLOC_FLAG_... */
2545 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2546 {
2547 struct xfs_agf *agf; /* ag freelist header */
2548 struct xfs_perag *pag; /* per allocation group data */
2549 int error;
2550
2551 trace_xfs_alloc_read_agf(mp, agno);
2552
2553 ASSERT(agno != NULLAGNUMBER);
2554 error = xfs_read_agf(mp, tp, agno,
2555 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2556 bpp);
2557 if (error)
2558 return error;
2559 if (!*bpp)
2560 return 0;
2561 ASSERT(!(*bpp)->b_error);
2562
2563 agf = XFS_BUF_TO_AGF(*bpp);
2564 pag = xfs_perag_get(mp, agno);
2565 if (!pag->pagf_init) {
2566 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2567 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2568 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2569 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2570 pag->pagf_levels[XFS_BTNUM_BNOi] =
2571 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2572 pag->pagf_levels[XFS_BTNUM_CNTi] =
2573 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2574 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2575 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
2576 spin_lock_init(&pag->pagb_lock);
2577 pag->pagb_count = 0;
2578 /* XXX: pagb_tree doesn't exist in userspace */
2579 //pag->pagb_tree = RB_ROOT;
2580 pag->pagf_init = 1;
2581 }
2582 #ifdef DEBUG
2583 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2584 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2585 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2586 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2587 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2588 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2589 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2590 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2591 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2592 }
2593 #endif
2594 xfs_perag_put(pag);
2595 return 0;
2596 }
2597
2598 /*
2599 * Allocate an extent (variable-size).
2600 * Depending on the allocation type, we either look in a single allocation
2601 * group or loop over the allocation groups to find the result.
2602 */
2603 int /* error */
2604 xfs_alloc_vextent(
2605 xfs_alloc_arg_t *args) /* allocation argument structure */
2606 {
2607 xfs_agblock_t agsize; /* allocation group size */
2608 int error;
2609 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2610 xfs_extlen_t minleft;/* minimum left value, temp copy */
2611 xfs_mount_t *mp; /* mount structure pointer */
2612 xfs_agnumber_t sagno; /* starting allocation group number */
2613 xfs_alloctype_t type; /* input allocation type */
2614 int bump_rotor = 0;
2615 int no_min = 0;
2616 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2617
2618 mp = args->mp;
2619 type = args->otype = args->type;
2620 args->agbno = NULLAGBLOCK;
2621 /*
2622 * Just fix this up, for the case where the last a.g. is shorter
2623 * (or there's only one a.g.) and the caller couldn't easily figure
2624 * that out (xfs_bmap_alloc).
2625 */
2626 agsize = mp->m_sb.sb_agblocks;
2627 if (args->maxlen > agsize)
2628 args->maxlen = agsize;
2629 if (args->alignment == 0)
2630 args->alignment = 1;
2631 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2632 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2633 ASSERT(args->minlen <= args->maxlen);
2634 ASSERT(args->minlen <= agsize);
2635 ASSERT(args->mod < args->prod);
2636 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2637 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2638 args->minlen > args->maxlen || args->minlen > agsize ||
2639 args->mod >= args->prod) {
2640 args->fsbno = NULLFSBLOCK;
2641 trace_xfs_alloc_vextent_badargs(args);
2642 return 0;
2643 }
2644 minleft = args->minleft;
2645
2646 switch (type) {
2647 case XFS_ALLOCTYPE_THIS_AG:
2648 case XFS_ALLOCTYPE_NEAR_BNO:
2649 case XFS_ALLOCTYPE_THIS_BNO:
2650 /*
2651 * These three force us into a single a.g.
2652 */
2653 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2654 args->pag = xfs_perag_get(mp, args->agno);
2655 args->minleft = 0;
2656 error = xfs_alloc_fix_freelist(args, 0);
2657 args->minleft = minleft;
2658 if (error) {
2659 trace_xfs_alloc_vextent_nofix(args);
2660 goto error0;
2661 }
2662 if (!args->agbp) {
2663 trace_xfs_alloc_vextent_noagbp(args);
2664 break;
2665 }
2666 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2667 if ((error = xfs_alloc_ag_vextent(args)))
2668 goto error0;
2669 break;
2670 case XFS_ALLOCTYPE_START_BNO:
2671 /*
2672 * Try near allocation first, then anywhere-in-ag after
2673 * the first a.g. fails.
2674 */
2675 if ((args->userdata & XFS_ALLOC_INITIAL_USER_DATA) &&
2676 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2677 args->fsbno = XFS_AGB_TO_FSB(mp,
2678 ((mp->m_agfrotor / rotorstep) %
2679 mp->m_sb.sb_agcount), 0);
2680 bump_rotor = 1;
2681 }
2682 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2683 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2684 /* FALLTHROUGH */
2685 case XFS_ALLOCTYPE_ANY_AG:
2686 case XFS_ALLOCTYPE_START_AG:
2687 case XFS_ALLOCTYPE_FIRST_AG:
2688 /*
2689 * Rotate through the allocation groups looking for a winner.
2690 */
2691 if (type == XFS_ALLOCTYPE_ANY_AG) {
2692 /*
2693 * Start with the last place we left off.
2694 */
2695 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2696 mp->m_sb.sb_agcount;
2697 args->type = XFS_ALLOCTYPE_THIS_AG;
2698 flags = XFS_ALLOC_FLAG_TRYLOCK;
2699 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2700 /*
2701 * Start with allocation group given by bno.
2702 */
2703 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2704 args->type = XFS_ALLOCTYPE_THIS_AG;
2705 sagno = 0;
2706 flags = 0;
2707 } else {
2708 if (type == XFS_ALLOCTYPE_START_AG)
2709 args->type = XFS_ALLOCTYPE_THIS_AG;
2710 /*
2711 * Start with the given allocation group.
2712 */
2713 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2714 flags = XFS_ALLOC_FLAG_TRYLOCK;
2715 }
2716 /*
2717 * Loop over allocation groups twice; first time with
2718 * trylock set, second time without.
2719 */
2720 for (;;) {
2721 args->pag = xfs_perag_get(mp, args->agno);
2722 if (no_min) args->minleft = 0;
2723 error = xfs_alloc_fix_freelist(args, flags);
2724 args->minleft = minleft;
2725 if (error) {
2726 trace_xfs_alloc_vextent_nofix(args);
2727 goto error0;
2728 }
2729 /*
2730 * If we get a buffer back then the allocation will fly.
2731 */
2732 if (args->agbp) {
2733 if ((error = xfs_alloc_ag_vextent(args)))
2734 goto error0;
2735 break;
2736 }
2737
2738 trace_xfs_alloc_vextent_loopfailed(args);
2739
2740 /*
2741 * Didn't work, figure out the next iteration.
2742 */
2743 if (args->agno == sagno &&
2744 type == XFS_ALLOCTYPE_START_BNO)
2745 args->type = XFS_ALLOCTYPE_THIS_AG;
2746 /*
2747 * For the first allocation, we can try any AG to get
2748 * space. However, if we already have allocated a
2749 * block, we don't want to try AGs whose number is below
2750 * sagno. Otherwise, we may end up with out-of-order
2751 * locking of AGF, which might cause deadlock.
2752 */
2753 if (++(args->agno) == mp->m_sb.sb_agcount) {
2754 if (args->firstblock != NULLFSBLOCK)
2755 args->agno = sagno;
2756 else
2757 args->agno = 0;
2758 }
2759 /*
2760 * Reached the starting a.g., must either be done
2761 * or switch to non-trylock mode.
2762 */
2763 if (args->agno == sagno) {
2764 if (no_min == 1) {
2765 args->agbno = NULLAGBLOCK;
2766 trace_xfs_alloc_vextent_allfailed(args);
2767 break;
2768 }
2769 if (flags == 0) {
2770 no_min = 1;
2771 } else {
2772 flags = 0;
2773 if (type == XFS_ALLOCTYPE_START_BNO) {
2774 args->agbno = XFS_FSB_TO_AGBNO(mp,
2775 args->fsbno);
2776 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2777 }
2778 }
2779 }
2780 xfs_perag_put(args->pag);
2781 }
2782 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2783 if (args->agno == sagno)
2784 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2785 (mp->m_sb.sb_agcount * rotorstep);
2786 else
2787 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2788 (mp->m_sb.sb_agcount * rotorstep);
2789 }
2790 break;
2791 default:
2792 ASSERT(0);
2793 /* NOTREACHED */
2794 }
2795 if (args->agbno == NULLAGBLOCK)
2796 args->fsbno = NULLFSBLOCK;
2797 else {
2798 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2799 #ifdef DEBUG
2800 ASSERT(args->len >= args->minlen);
2801 ASSERT(args->len <= args->maxlen);
2802 ASSERT(args->agbno % args->alignment == 0);
2803 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2804 args->len);
2805 #endif
2806
2807 /* Zero the extent if we were asked to do so */
2808 if (args->userdata & XFS_ALLOC_USERDATA_ZERO) {
2809 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2810 if (error)
2811 goto error0;
2812 }
2813
2814 }
2815 xfs_perag_put(args->pag);
2816 return 0;
2817 error0:
2818 xfs_perag_put(args->pag);
2819 return error;
2820 }
2821
2822 /* Ensure that the freelist is at full capacity. */
2823 int
2824 xfs_free_extent_fix_freelist(
2825 struct xfs_trans *tp,
2826 xfs_agnumber_t agno,
2827 struct xfs_buf **agbp)
2828 {
2829 struct xfs_alloc_arg args;
2830 int error;
2831
2832 memset(&args, 0, sizeof(struct xfs_alloc_arg));
2833 args.tp = tp;
2834 args.mp = tp->t_mountp;
2835 args.agno = agno;
2836
2837 /*
2838 * validate that the block number is legal - the enables us to detect
2839 * and handle a silent filesystem corruption rather than crashing.
2840 */
2841 if (args.agno >= args.mp->m_sb.sb_agcount)
2842 return -EFSCORRUPTED;
2843
2844 args.pag = xfs_perag_get(args.mp, args.agno);
2845 ASSERT(args.pag);
2846
2847 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2848 if (error)
2849 goto out;
2850
2851 *agbp = args.agbp;
2852 out:
2853 xfs_perag_put(args.pag);
2854 return error;
2855 }
2856
2857 /*
2858 * Free an extent.
2859 * Just break up the extent address and hand off to xfs_free_ag_extent
2860 * after fixing up the freelist.
2861 */
2862 int /* error */
2863 xfs_free_extent(
2864 struct xfs_trans *tp, /* transaction pointer */
2865 xfs_fsblock_t bno, /* starting block number of extent */
2866 xfs_extlen_t len, /* length of extent */
2867 struct xfs_owner_info *oinfo, /* extent owner */
2868 enum xfs_ag_resv_type type) /* block reservation type */
2869 {
2870 struct xfs_mount *mp = tp->t_mountp;
2871 struct xfs_buf *agbp;
2872 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2873 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2874 int error;
2875
2876 ASSERT(len != 0);
2877 ASSERT(type != XFS_AG_RESV_AGFL);
2878
2879 if (XFS_TEST_ERROR(false, mp,
2880 XFS_ERRTAG_FREE_EXTENT,
2881 XFS_RANDOM_FREE_EXTENT))
2882 return -EIO;
2883
2884 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2885 if (error)
2886 return error;
2887
2888 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
2889
2890 /* validate the extent size is legal now we have the agf locked */
2891 XFS_WANT_CORRUPTED_GOTO(mp,
2892 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2893 err);
2894
2895 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
2896 if (error)
2897 goto err;
2898
2899 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2900 return 0;
2901
2902 err:
2903 xfs_trans_brelse(tp, agbp);
2904 return error;
2905 }