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