]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/libxfs/xfs_alloc.c
xfs: xfs_is_shutdown vs xlog_is_shutdown cage fight
[people/ms/linux.git] / fs / xfs / libxfs / xfs_alloc.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
a844f451 7#include "xfs_fs.h"
70a9883c 8#include "xfs_format.h"
239880ef 9#include "xfs_log_format.h"
70a9883c 10#include "xfs_shared.h"
239880ef 11#include "xfs_trans_resv.h"
a844f451 12#include "xfs_bit.h"
1da177e4 13#include "xfs_mount.h"
3ab78df2 14#include "xfs_defer.h"
1da177e4 15#include "xfs_btree.h"
673930c3 16#include "xfs_rmap.h"
a4fbe6ab 17#include "xfs_alloc_btree.h"
1da177e4 18#include "xfs_alloc.h"
efc27b52 19#include "xfs_extent_busy.h"
e9e899a2 20#include "xfs_errortag.h"
1da177e4 21#include "xfs_error.h"
0b1b213f 22#include "xfs_trace.h"
239880ef 23#include "xfs_trans.h"
4e0e6040 24#include "xfs_buf_item.h"
239880ef 25#include "xfs_log.h"
9bbafc71 26#include "xfs_ag.h"
3fd129b6 27#include "xfs_ag_resv.h"
f8f2835a
BF
28#include "xfs_bmap.h"
29
c201d9ca 30struct kmem_cache *xfs_extfree_item_cache;
1da177e4 31
c999a223 32struct workqueue_struct *xfs_alloc_wq;
1da177e4
LT
33
34#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
35
36#define XFSA_FIXUP_BNO_OK 1
37#define XFSA_FIXUP_CNT_OK 2
38
1da177e4
LT
39STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
40STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
41STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
1da177e4 42
a78ee256
DC
43/*
44 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
45 * the beginning of the block for a proper header with the location information
46 * and CRC.
47 */
48unsigned int
49xfs_agfl_size(
50 struct xfs_mount *mp)
51{
52 unsigned int size = mp->m_sb.sb_sectsize;
53
38c26bfd 54 if (xfs_has_crc(mp))
a78ee256
DC
55 size -= sizeof(struct xfs_agfl);
56
57 return size / sizeof(xfs_agblock_t);
58}
59
af30dfa1
DW
60unsigned int
61xfs_refc_block(
62 struct xfs_mount *mp)
63{
38c26bfd 64 if (xfs_has_rmapbt(mp))
af30dfa1 65 return XFS_RMAP_BLOCK(mp) + 1;
38c26bfd 66 if (xfs_has_finobt(mp))
af30dfa1
DW
67 return XFS_FIBT_BLOCK(mp) + 1;
68 return XFS_IBT_BLOCK(mp) + 1;
69}
70
8018026e
DW
71xfs_extlen_t
72xfs_prealloc_blocks(
73 struct xfs_mount *mp)
74{
38c26bfd 75 if (xfs_has_reflink(mp))
af30dfa1 76 return xfs_refc_block(mp) + 1;
38c26bfd 77 if (xfs_has_rmapbt(mp))
8018026e 78 return XFS_RMAP_BLOCK(mp) + 1;
38c26bfd 79 if (xfs_has_finobt(mp))
8018026e
DW
80 return XFS_FIBT_BLOCK(mp) + 1;
81 return XFS_IBT_BLOCK(mp) + 1;
82}
83
52548852
DW
84/*
85 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
86 * AGF buffer (PV 947395), we place constraints on the relationship among
87 * actual allocations for data blocks, freelist blocks, and potential file data
88 * bmap btree blocks. However, these restrictions may result in no actual space
89 * allocated for a delayed extent, for example, a data block in a certain AG is
90 * allocated but there is no additional block for the additional bmap btree
91 * block due to a split of the bmap btree of the file. The result of this may
92 * lead to an infinite loop when the file gets flushed to disk and all delayed
93 * extents need to be actually allocated. To get around this, we explicitly set
94 * aside a few blocks which will not be reserved in delayed allocation.
95 *
3fd129b6
DW
96 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
97 * potential split of the file's bmap btree.
52548852
DW
98 */
99unsigned int
100xfs_alloc_set_aside(
101 struct xfs_mount *mp)
102{
5149fd32 103 return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
52548852
DW
104}
105
106/*
107 * When deciding how much space to allocate out of an AG, we limit the
108 * allocation maximum size to the size the AG. However, we cannot use all the
109 * blocks in the AG - some are permanently used by metadata. These
110 * blocks are generally:
111 * - the AG superblock, AGF, AGI and AGFL
112 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
113 * the AGI free inode and rmap btree root blocks.
114 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
115 * - the rmapbt root block
116 *
117 * The AG headers are sector sized, so the amount of space they take up is
118 * dependent on filesystem geometry. The others are all single blocks.
119 */
120unsigned int
121xfs_alloc_ag_max_usable(
122 struct xfs_mount *mp)
123{
124 unsigned int blocks;
125
126 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
127 blocks += XFS_ALLOC_AGFL_RESERVE;
128 blocks += 3; /* AGF, AGI btree root blocks */
38c26bfd 129 if (xfs_has_finobt(mp))
52548852 130 blocks++; /* finobt root block */
38c26bfd 131 if (xfs_has_rmapbt(mp))
52548852 132 blocks++; /* rmap root block */
38c26bfd 133 if (xfs_has_reflink(mp))
d0e853f3 134 blocks++; /* refcount root block */
52548852
DW
135
136 return mp->m_sb.sb_agblocks - blocks;
137}
138
fe033cc8
CH
139/*
140 * Lookup the record equal to [bno, len] in the btree given by cur.
141 */
142STATIC int /* error */
143xfs_alloc_lookup_eq(
144 struct xfs_btree_cur *cur, /* btree cursor */
145 xfs_agblock_t bno, /* starting block of extent */
146 xfs_extlen_t len, /* length of extent */
147 int *stat) /* success/failure */
148{
f6b428a4
BF
149 int error;
150
fe033cc8
CH
151 cur->bc_rec.a.ar_startblock = bno;
152 cur->bc_rec.a.ar_blockcount = len;
f6b428a4 153 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
c4aa10d0 154 cur->bc_ag.abt.active = (*stat == 1);
f6b428a4 155 return error;
fe033cc8
CH
156}
157
158/*
159 * Lookup the first record greater than or equal to [bno, len]
160 * in the btree given by cur.
161 */
a66d6363 162int /* error */
fe033cc8
CH
163xfs_alloc_lookup_ge(
164 struct xfs_btree_cur *cur, /* btree cursor */
165 xfs_agblock_t bno, /* starting block of extent */
166 xfs_extlen_t len, /* length of extent */
167 int *stat) /* success/failure */
168{
f6b428a4
BF
169 int error;
170
fe033cc8
CH
171 cur->bc_rec.a.ar_startblock = bno;
172 cur->bc_rec.a.ar_blockcount = len;
f6b428a4 173 error = xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
c4aa10d0 174 cur->bc_ag.abt.active = (*stat == 1);
f6b428a4 175 return error;
fe033cc8
CH
176}
177
178/*
179 * Lookup the first record less than or equal to [bno, len]
180 * in the btree given by cur.
181 */
ce1d802e 182int /* error */
fe033cc8
CH
183xfs_alloc_lookup_le(
184 struct xfs_btree_cur *cur, /* btree cursor */
185 xfs_agblock_t bno, /* starting block of extent */
186 xfs_extlen_t len, /* length of extent */
187 int *stat) /* success/failure */
188{
f6b428a4 189 int error;
fe033cc8
CH
190 cur->bc_rec.a.ar_startblock = bno;
191 cur->bc_rec.a.ar_blockcount = len;
f6b428a4 192 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
c4aa10d0 193 cur->bc_ag.abt.active = (*stat == 1);
f6b428a4
BF
194 return error;
195}
196
197static inline bool
198xfs_alloc_cur_active(
199 struct xfs_btree_cur *cur)
200{
c4aa10d0 201 return cur && cur->bc_ag.abt.active;
fe033cc8
CH
202}
203
278d0ca1
CH
204/*
205 * Update the record referred to by cur to the value given
206 * by [bno, len].
207 * This either works (return 0) or gets an EFSCORRUPTED error.
208 */
209STATIC int /* error */
210xfs_alloc_update(
211 struct xfs_btree_cur *cur, /* btree cursor */
212 xfs_agblock_t bno, /* starting block of extent */
213 xfs_extlen_t len) /* length of extent */
214{
215 union xfs_btree_rec rec;
216
217 rec.alloc.ar_startblock = cpu_to_be32(bno);
218 rec.alloc.ar_blockcount = cpu_to_be32(len);
219 return xfs_btree_update(cur, &rec);
220}
fe033cc8 221
8cc938fe
CH
222/*
223 * Get the data from the pointed-to record.
224 */
a46db608 225int /* error */
8cc938fe
CH
226xfs_alloc_get_rec(
227 struct xfs_btree_cur *cur, /* btree cursor */
228 xfs_agblock_t *bno, /* output: starting block of extent */
229 xfs_extlen_t *len, /* output: length of extent */
230 int *stat) /* output: success/failure */
231{
9e6c08d4 232 struct xfs_mount *mp = cur->bc_mp;
50f02fe3 233 xfs_agnumber_t agno = cur->bc_ag.pag->pag_agno;
8cc938fe
CH
234 union xfs_btree_rec *rec;
235 int error;
236
237 error = xfs_btree_get_rec(cur, &rec, stat);
a37f7b12
DW
238 if (error || !(*stat))
239 return error;
a37f7b12
DW
240
241 *bno = be32_to_cpu(rec->alloc.ar_startblock);
242 *len = be32_to_cpu(rec->alloc.ar_blockcount);
243
efe80327
CM
244 if (*len == 0)
245 goto out_bad_rec;
246
9e6c08d4
DC
247 /* check for valid extent range, including overflow */
248 if (!xfs_verify_agbno(mp, agno, *bno))
249 goto out_bad_rec;
250 if (*bno > *bno + *len)
251 goto out_bad_rec;
252 if (!xfs_verify_agbno(mp, agno, *bno + *len - 1))
253 goto out_bad_rec;
254
255 return 0;
256
257out_bad_rec:
258 xfs_warn(mp,
259 "%s Freespace BTree record corruption in AG %d detected!",
260 cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size", agno);
261 xfs_warn(mp,
262 "start block 0x%x block count 0x%x", *bno, *len);
263 return -EFSCORRUPTED;
8cc938fe
CH
264}
265
1da177e4
LT
266/*
267 * Compute aligned version of the found extent.
268 * Takes alignment and min length into account.
269 */
ebf55872 270STATIC bool
1da177e4 271xfs_alloc_compute_aligned(
86fa8af6 272 xfs_alloc_arg_t *args, /* allocation argument structure */
1da177e4
LT
273 xfs_agblock_t foundbno, /* starting block in found extent */
274 xfs_extlen_t foundlen, /* length in found extent */
1da177e4 275 xfs_agblock_t *resbno, /* result block number */
ebf55872
CH
276 xfs_extlen_t *reslen, /* result length */
277 unsigned *busy_gen)
1da177e4 278{
ebf55872
CH
279 xfs_agblock_t bno = foundbno;
280 xfs_extlen_t len = foundlen;
bfe46d4e 281 xfs_extlen_t diff;
ebf55872 282 bool busy;
1da177e4 283
e26f0501 284 /* Trim busy sections out of found extent */
ebf55872 285 busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
e26f0501 286
bfe46d4e
BF
287 /*
288 * If we have a largish extent that happens to start before min_agbno,
289 * see if we can shift it into range...
290 */
291 if (bno < args->min_agbno && bno + len > args->min_agbno) {
292 diff = args->min_agbno - bno;
293 if (len > diff) {
294 bno += diff;
295 len -= diff;
296 }
297 }
298
e26f0501
CH
299 if (args->alignment > 1 && len >= args->minlen) {
300 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
bfe46d4e
BF
301
302 diff = aligned_bno - bno;
e26f0501
CH
303
304 *resbno = aligned_bno;
305 *reslen = diff >= len ? 0 : len - diff;
1da177e4 306 } else {
e26f0501
CH
307 *resbno = bno;
308 *reslen = len;
1da177e4 309 }
ebf55872
CH
310
311 return busy;
1da177e4
LT
312}
313
314/*
315 * Compute best start block and diff for "near" allocations.
316 * freelen >= wantlen already checked by caller.
317 */
318STATIC xfs_extlen_t /* difference value (absolute) */
319xfs_alloc_compute_diff(
320 xfs_agblock_t wantbno, /* target starting block */
321 xfs_extlen_t wantlen, /* target length */
322 xfs_extlen_t alignment, /* target alignment */
292378ed 323 int datatype, /* are we allocating data? */
1da177e4
LT
324 xfs_agblock_t freebno, /* freespace's starting block */
325 xfs_extlen_t freelen, /* freespace's length */
326 xfs_agblock_t *newbnop) /* result: best start block from free */
327{
328 xfs_agblock_t freeend; /* end of freespace extent */
329 xfs_agblock_t newbno1; /* return block number */
330 xfs_agblock_t newbno2; /* other new block number */
331 xfs_extlen_t newlen1=0; /* length with newbno1 */
332 xfs_extlen_t newlen2=0; /* length with newbno2 */
333 xfs_agblock_t wantend; /* end of target extent */
c34d570d 334 bool userdata = datatype & XFS_ALLOC_USERDATA;
1da177e4
LT
335
336 ASSERT(freelen >= wantlen);
337 freeend = freebno + freelen;
338 wantend = wantbno + wantlen;
211d022c
JK
339 /*
340 * We want to allocate from the start of a free extent if it is past
341 * the desired block or if we are allocating user data and the free
342 * extent is before desired block. The second case is there to allow
343 * for contiguous allocation from the remaining free space if the file
344 * grows in the short term.
345 */
346 if (freebno >= wantbno || (userdata && freeend < wantend)) {
1da177e4
LT
347 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
348 newbno1 = NULLAGBLOCK;
349 } else if (freeend >= wantend && alignment > 1) {
350 newbno1 = roundup(wantbno, alignment);
351 newbno2 = newbno1 - alignment;
352 if (newbno1 >= freeend)
353 newbno1 = NULLAGBLOCK;
354 else
355 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
356 if (newbno2 < freebno)
357 newbno2 = NULLAGBLOCK;
358 else
359 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
360 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
361 if (newlen1 < newlen2 ||
362 (newlen1 == newlen2 &&
363 XFS_ABSDIFF(newbno1, wantbno) >
364 XFS_ABSDIFF(newbno2, wantbno)))
365 newbno1 = newbno2;
366 } else if (newbno2 != NULLAGBLOCK)
367 newbno1 = newbno2;
368 } else if (freeend >= wantend) {
369 newbno1 = wantbno;
370 } else if (alignment > 1) {
371 newbno1 = roundup(freeend - wantlen, alignment);
372 if (newbno1 > freeend - wantlen &&
373 newbno1 - alignment >= freebno)
374 newbno1 -= alignment;
375 else if (newbno1 >= freeend)
376 newbno1 = NULLAGBLOCK;
377 } else
378 newbno1 = freeend - wantlen;
379 *newbnop = newbno1;
380 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
381}
382
383/*
384 * Fix up the length, based on mod and prod.
385 * len should be k * prod + mod for some k.
386 * If len is too small it is returned unchanged.
387 * If len hits maxlen it is left alone.
388 */
389STATIC void
390xfs_alloc_fix_len(
391 xfs_alloc_arg_t *args) /* allocation argument structure */
392{
393 xfs_extlen_t k;
394 xfs_extlen_t rlen;
395
396 ASSERT(args->mod < args->prod);
397 rlen = args->len;
398 ASSERT(rlen >= args->minlen);
399 ASSERT(rlen <= args->maxlen);
400 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
401 (args->mod == 0 && rlen < args->prod))
402 return;
403 k = rlen % args->prod;
404 if (k == args->mod)
405 return;
30265117
JK
406 if (k > args->mod)
407 rlen = rlen - (k - args->mod);
408 else
409 rlen = rlen - args->prod + (args->mod - k);
3790a8cd 410 /* casts to (int) catch length underflows */
30265117
JK
411 if ((int)rlen < (int)args->minlen)
412 return;
413 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
414 ASSERT(rlen % args->prod == args->mod);
54fee133
CH
415 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
416 rlen + args->minleft);
1da177e4
LT
417 args->len = rlen;
418}
419
1da177e4
LT
420/*
421 * Update the two btrees, logically removing from freespace the extent
422 * starting at rbno, rlen blocks. The extent is contained within the
423 * actual (current) free extent fbno for flen blocks.
424 * Flags are passed in indicating whether the cursors are set to the
425 * relevant records.
426 */
427STATIC int /* error code */
428xfs_alloc_fixup_trees(
ae127f08
DW
429 struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */
430 struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */
1da177e4
LT
431 xfs_agblock_t fbno, /* starting block of free extent */
432 xfs_extlen_t flen, /* length of free extent */
433 xfs_agblock_t rbno, /* starting block of returned extent */
434 xfs_extlen_t rlen, /* length of returned extent */
435 int flags) /* flags, XFSA_FIXUP_... */
436{
437 int error; /* error code */
438 int i; /* operation results */
439 xfs_agblock_t nfbno1; /* first new free startblock */
440 xfs_agblock_t nfbno2; /* second new free startblock */
441 xfs_extlen_t nflen1=0; /* first new free length */
442 xfs_extlen_t nflen2=0; /* second new free length */
5fb5aeee
ES
443 struct xfs_mount *mp;
444
445 mp = cnt_cur->bc_mp;
1da177e4
LT
446
447 /*
448 * Look up the record in the by-size tree if necessary.
449 */
450 if (flags & XFSA_FIXUP_CNT_OK) {
451#ifdef DEBUG
452 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
453 return error;
f9e03706
DW
454 if (XFS_IS_CORRUPT(mp,
455 i != 1 ||
456 nfbno1 != fbno ||
457 nflen1 != flen))
458 return -EFSCORRUPTED;
1da177e4
LT
459#endif
460 } else {
461 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
462 return error;
f9e03706
DW
463 if (XFS_IS_CORRUPT(mp, i != 1))
464 return -EFSCORRUPTED;
1da177e4
LT
465 }
466 /*
467 * Look up the record in the by-block tree if necessary.
468 */
469 if (flags & XFSA_FIXUP_BNO_OK) {
470#ifdef DEBUG
471 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
472 return error;
f9e03706
DW
473 if (XFS_IS_CORRUPT(mp,
474 i != 1 ||
475 nfbno1 != fbno ||
476 nflen1 != flen))
477 return -EFSCORRUPTED;
1da177e4
LT
478#endif
479 } else {
480 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
481 return error;
f9e03706
DW
482 if (XFS_IS_CORRUPT(mp, i != 1))
483 return -EFSCORRUPTED;
1da177e4 484 }
7cc95a82 485
1da177e4 486#ifdef DEBUG
7cc95a82
CH
487 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
488 struct xfs_btree_block *bnoblock;
489 struct xfs_btree_block *cntblock;
490
6ca444cf
DW
491 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp);
492 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp);
1da177e4 493
f9e03706
DW
494 if (XFS_IS_CORRUPT(mp,
495 bnoblock->bb_numrecs !=
496 cntblock->bb_numrecs))
497 return -EFSCORRUPTED;
1da177e4
LT
498 }
499#endif
7cc95a82 500
1da177e4
LT
501 /*
502 * Deal with all four cases: the allocated record is contained
503 * within the freespace record, so we can have new freespace
504 * at either (or both) end, or no freespace remaining.
505 */
506 if (rbno == fbno && rlen == flen)
507 nfbno1 = nfbno2 = NULLAGBLOCK;
508 else if (rbno == fbno) {
509 nfbno1 = rbno + rlen;
510 nflen1 = flen - rlen;
511 nfbno2 = NULLAGBLOCK;
512 } else if (rbno + rlen == fbno + flen) {
513 nfbno1 = fbno;
514 nflen1 = flen - rlen;
515 nfbno2 = NULLAGBLOCK;
516 } else {
517 nfbno1 = fbno;
518 nflen1 = rbno - fbno;
519 nfbno2 = rbno + rlen;
520 nflen2 = (fbno + flen) - nfbno2;
521 }
522 /*
523 * Delete the entry from the by-size btree.
524 */
91cca5df 525 if ((error = xfs_btree_delete(cnt_cur, &i)))
1da177e4 526 return error;
f9e03706
DW
527 if (XFS_IS_CORRUPT(mp, i != 1))
528 return -EFSCORRUPTED;
1da177e4
LT
529 /*
530 * Add new by-size btree entry(s).
531 */
532 if (nfbno1 != NULLAGBLOCK) {
533 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
534 return error;
f9e03706
DW
535 if (XFS_IS_CORRUPT(mp, i != 0))
536 return -EFSCORRUPTED;
4b22a571 537 if ((error = xfs_btree_insert(cnt_cur, &i)))
1da177e4 538 return error;
f9e03706
DW
539 if (XFS_IS_CORRUPT(mp, i != 1))
540 return -EFSCORRUPTED;
1da177e4
LT
541 }
542 if (nfbno2 != NULLAGBLOCK) {
543 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
544 return error;
f9e03706
DW
545 if (XFS_IS_CORRUPT(mp, i != 0))
546 return -EFSCORRUPTED;
4b22a571 547 if ((error = xfs_btree_insert(cnt_cur, &i)))
1da177e4 548 return error;
f9e03706
DW
549 if (XFS_IS_CORRUPT(mp, i != 1))
550 return -EFSCORRUPTED;
1da177e4
LT
551 }
552 /*
553 * Fix up the by-block btree entry(s).
554 */
555 if (nfbno1 == NULLAGBLOCK) {
556 /*
557 * No remaining freespace, just delete the by-block tree entry.
558 */
91cca5df 559 if ((error = xfs_btree_delete(bno_cur, &i)))
1da177e4 560 return error;
f9e03706
DW
561 if (XFS_IS_CORRUPT(mp, i != 1))
562 return -EFSCORRUPTED;
1da177e4
LT
563 } else {
564 /*
565 * Update the by-block entry to start later|be shorter.
566 */
567 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
568 return error;
569 }
570 if (nfbno2 != NULLAGBLOCK) {
571 /*
572 * 2 resulting free entries, need to add one.
573 */
574 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
575 return error;
f9e03706
DW
576 if (XFS_IS_CORRUPT(mp, i != 0))
577 return -EFSCORRUPTED;
4b22a571 578 if ((error = xfs_btree_insert(bno_cur, &i)))
1da177e4 579 return error;
f9e03706
DW
580 if (XFS_IS_CORRUPT(mp, i != 1))
581 return -EFSCORRUPTED;
1da177e4
LT
582 }
583 return 0;
584}
585
a6a781a5 586static xfs_failaddr_t
612cfbfe 587xfs_agfl_verify(
bb80c6d7
DC
588 struct xfs_buf *bp)
589{
dbd329f1 590 struct xfs_mount *mp = bp->b_mount;
bb80c6d7 591 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
183606d8 592 __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp);
bb80c6d7
DC
593 int i;
594
b5572597
DW
595 /*
596 * There is no verification of non-crc AGFLs because mkfs does not
597 * initialise the AGFL to zero or NULL. Hence the only valid part of the
598 * AGFL is what the AGF says is active. We can't get to the AGF, so we
599 * can't verify just those entries are valid.
600 */
38c26bfd 601 if (!xfs_has_crc(mp))
b5572597
DW
602 return NULL;
603
39708c20 604 if (!xfs_verify_magic(bp, agfl->agfl_magicnum))
a6a781a5 605 return __this_address;
39708c20 606 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
a6a781a5 607 return __this_address;
77c95bba
CH
608 /*
609 * during growfs operations, the perag is not fully initialised,
610 * so we can't use it for any useful checking. growfs ensures we can't
611 * use it by using uncached buffers that don't have the perag attached
612 * so we can detect and avoid this problem.
613 */
614 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
a6a781a5 615 return __this_address;
77c95bba 616
a78ee256 617 for (i = 0; i < xfs_agfl_size(mp); i++) {
183606d8
CH
618 if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK &&
619 be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks)
a6a781a5 620 return __this_address;
bb80c6d7 621 }
a45086e2 622
a6a781a5
DW
623 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
624 return __this_address;
625 return NULL;
77c95bba
CH
626}
627
628static void
629xfs_agfl_read_verify(
630 struct xfs_buf *bp)
631{
dbd329f1 632 struct xfs_mount *mp = bp->b_mount;
bc1a09b8 633 xfs_failaddr_t fa;
77c95bba
CH
634
635 /*
636 * There is no verification of non-crc AGFLs because mkfs does not
637 * initialise the AGFL to zero or NULL. Hence the only valid part of the
638 * AGFL is what the AGF says is active. We can't get to the AGF, so we
639 * can't verify just those entries are valid.
640 */
38c26bfd 641 if (!xfs_has_crc(mp))
77c95bba
CH
642 return;
643
ce5028cf 644 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
bc1a09b8
DW
645 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
646 else {
647 fa = xfs_agfl_verify(bp);
648 if (fa)
649 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
650 }
612cfbfe
DC
651}
652
1813dd64 653static void
612cfbfe
DC
654xfs_agfl_write_verify(
655 struct xfs_buf *bp)
656{
dbd329f1 657 struct xfs_mount *mp = bp->b_mount;
fb1755a6 658 struct xfs_buf_log_item *bip = bp->b_log_item;
bc1a09b8 659 xfs_failaddr_t fa;
612cfbfe 660
77c95bba 661 /* no verification of non-crc AGFLs */
38c26bfd 662 if (!xfs_has_crc(mp))
77c95bba
CH
663 return;
664
bc1a09b8
DW
665 fa = xfs_agfl_verify(bp);
666 if (fa) {
667 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
77c95bba
CH
668 return;
669 }
670
671 if (bip)
672 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
673
f1dbcd7e 674 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
bb80c6d7
DC
675}
676
1813dd64 677const struct xfs_buf_ops xfs_agfl_buf_ops = {
233135b7 678 .name = "xfs_agfl",
39708c20 679 .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) },
1813dd64
DC
680 .verify_read = xfs_agfl_read_verify,
681 .verify_write = xfs_agfl_write_verify,
b5572597 682 .verify_struct = xfs_agfl_verify,
1813dd64
DC
683};
684
1da177e4
LT
685/*
686 * Read in the allocation group free block array.
687 */
26788097 688int /* error */
1da177e4
LT
689xfs_alloc_read_agfl(
690 xfs_mount_t *mp, /* mount point structure */
691 xfs_trans_t *tp, /* transaction pointer */
692 xfs_agnumber_t agno, /* allocation group number */
e8222613 693 struct xfs_buf **bpp) /* buffer for the ag free block array */
1da177e4 694{
e8222613 695 struct xfs_buf *bp; /* return value */
1da177e4
LT
696 int error;
697
698 ASSERT(agno != NULLAGNUMBER);
699 error = xfs_trans_read_buf(
700 mp, tp, mp->m_ddev_targp,
701 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
1813dd64 702 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
1da177e4
LT
703 if (error)
704 return error;
38f23232 705 xfs_buf_set_ref(bp, XFS_AGFL_REF);
1da177e4
LT
706 *bpp = bp;
707 return 0;
708}
709
ecb6928f
CH
710STATIC int
711xfs_alloc_update_counters(
712 struct xfs_trans *tp,
ecb6928f
CH
713 struct xfs_buf *agbp,
714 long len)
715{
9798f615 716 struct xfs_agf *agf = agbp->b_addr;
ecb6928f 717
92a00544 718 agbp->b_pag->pagf_freeblks += len;
ecb6928f
CH
719 be32_add_cpu(&agf->agf_freeblks, len);
720
ecb6928f 721 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
a5155b87 722 be32_to_cpu(agf->agf_length))) {
8d57c216 723 xfs_buf_mark_corrupt(agbp);
2451337d 724 return -EFSCORRUPTED;
a5155b87 725 }
ecb6928f
CH
726
727 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
728 return 0;
729}
730
1da177e4 731/*
f5e7dbea 732 * Block allocation algorithm and data structures.
1da177e4 733 */
f5e7dbea
BF
734struct xfs_alloc_cur {
735 struct xfs_btree_cur *cnt; /* btree cursors */
736 struct xfs_btree_cur *bnolt;
737 struct xfs_btree_cur *bnogt;
dc8e69bd 738 xfs_extlen_t cur_len;/* current search length */
c62321a2
BF
739 xfs_agblock_t rec_bno;/* extent startblock */
740 xfs_extlen_t rec_len;/* extent length */
741 xfs_agblock_t bno; /* alloc bno */
742 xfs_extlen_t len; /* alloc len */
743 xfs_extlen_t diff; /* diff from search bno */
d6d3aff2
BF
744 unsigned int busy_gen;/* busy state */
745 bool busy;
f5e7dbea
BF
746};
747
748/*
749 * Set up cursors, etc. in the extent allocation cursor. This function can be
750 * called multiple times to reset an initialized structure without having to
751 * reallocate cursors.
752 */
753static int
754xfs_alloc_cur_setup(
755 struct xfs_alloc_arg *args,
756 struct xfs_alloc_cur *acur)
757{
758 int error;
759 int i;
760
761 ASSERT(args->alignment == 1 || args->type != XFS_ALLOCTYPE_THIS_BNO);
762
dc8e69bd 763 acur->cur_len = args->maxlen;
c62321a2
BF
764 acur->rec_bno = 0;
765 acur->rec_len = 0;
766 acur->bno = 0;
767 acur->len = 0;
396bbf3c 768 acur->diff = -1;
d6d3aff2
BF
769 acur->busy = false;
770 acur->busy_gen = 0;
771
f5e7dbea
BF
772 /*
773 * Perform an initial cntbt lookup to check for availability of maxlen
774 * extents. If this fails, we'll return -ENOSPC to signal the caller to
775 * attempt a small allocation.
776 */
777 if (!acur->cnt)
778 acur->cnt = xfs_allocbt_init_cursor(args->mp, args->tp,
289d38d2 779 args->agbp, args->pag, XFS_BTNUM_CNT);
f5e7dbea
BF
780 error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i);
781 if (error)
782 return error;
783
784 /*
785 * Allocate the bnobt left and right search cursors.
786 */
787 if (!acur->bnolt)
788 acur->bnolt = xfs_allocbt_init_cursor(args->mp, args->tp,
289d38d2 789 args->agbp, args->pag, XFS_BTNUM_BNO);
f5e7dbea
BF
790 if (!acur->bnogt)
791 acur->bnogt = xfs_allocbt_init_cursor(args->mp, args->tp,
289d38d2 792 args->agbp, args->pag, XFS_BTNUM_BNO);
f5e7dbea
BF
793 return i == 1 ? 0 : -ENOSPC;
794}
795
796static void
797xfs_alloc_cur_close(
798 struct xfs_alloc_cur *acur,
799 bool error)
800{
801 int cur_error = XFS_BTREE_NOERROR;
802
803 if (error)
804 cur_error = XFS_BTREE_ERROR;
805
806 if (acur->cnt)
807 xfs_btree_del_cursor(acur->cnt, cur_error);
808 if (acur->bnolt)
809 xfs_btree_del_cursor(acur->bnolt, cur_error);
810 if (acur->bnogt)
811 xfs_btree_del_cursor(acur->bnogt, cur_error);
812 acur->cnt = acur->bnolt = acur->bnogt = NULL;
813}
1da177e4 814
396bbf3c
BF
815/*
816 * Check an extent for allocation and track the best available candidate in the
817 * allocation structure. The cursor is deactivated if it has entered an out of
818 * range state based on allocation arguments. Optionally return the extent
819 * extent geometry and allocation status if requested by the caller.
820 */
821static int
822xfs_alloc_cur_check(
823 struct xfs_alloc_arg *args,
824 struct xfs_alloc_cur *acur,
825 struct xfs_btree_cur *cur,
826 int *new)
827{
828 int error, i;
829 xfs_agblock_t bno, bnoa, bnew;
830 xfs_extlen_t len, lena, diff = -1;
831 bool busy;
832 unsigned busy_gen = 0;
833 bool deactivate = false;
fec0afda 834 bool isbnobt = cur->bc_btnum == XFS_BTNUM_BNO;
396bbf3c
BF
835
836 *new = 0;
837
838 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
839 if (error)
840 return error;
f9e03706
DW
841 if (XFS_IS_CORRUPT(args->mp, i != 1))
842 return -EFSCORRUPTED;
396bbf3c
BF
843
844 /*
845 * Check minlen and deactivate a cntbt cursor if out of acceptable size
846 * range (i.e., walking backwards looking for a minlen extent).
847 */
848 if (len < args->minlen) {
fec0afda 849 deactivate = !isbnobt;
396bbf3c
BF
850 goto out;
851 }
852
853 busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena,
854 &busy_gen);
855 acur->busy |= busy;
856 if (busy)
857 acur->busy_gen = busy_gen;
858 /* deactivate a bnobt cursor outside of locality range */
fec0afda
BF
859 if (bnoa < args->min_agbno || bnoa > args->max_agbno) {
860 deactivate = isbnobt;
396bbf3c 861 goto out;
fec0afda 862 }
396bbf3c
BF
863 if (lena < args->minlen)
864 goto out;
865
866 args->len = XFS_EXTLEN_MIN(lena, args->maxlen);
867 xfs_alloc_fix_len(args);
868 ASSERT(args->len >= args->minlen);
869 if (args->len < acur->len)
870 goto out;
871
872 /*
873 * We have an aligned record that satisfies minlen and beats or matches
874 * the candidate extent size. Compare locality for near allocation mode.
875 */
876 ASSERT(args->type == XFS_ALLOCTYPE_NEAR_BNO);
877 diff = xfs_alloc_compute_diff(args->agbno, args->len,
878 args->alignment, args->datatype,
879 bnoa, lena, &bnew);
880 if (bnew == NULLAGBLOCK)
881 goto out;
fec0afda
BF
882
883 /*
884 * Deactivate a bnobt cursor with worse locality than the current best.
885 */
886 if (diff > acur->diff) {
887 deactivate = isbnobt;
396bbf3c 888 goto out;
fec0afda 889 }
396bbf3c
BF
890
891 ASSERT(args->len > acur->len ||
892 (args->len == acur->len && diff <= acur->diff));
893 acur->rec_bno = bno;
894 acur->rec_len = len;
895 acur->bno = bnew;
896 acur->len = args->len;
897 acur->diff = diff;
898 *new = 1;
899
78d7aabd
BF
900 /*
901 * We're done if we found a perfect allocation. This only deactivates
902 * the current cursor, but this is just an optimization to terminate a
903 * cntbt search that otherwise runs to the edge of the tree.
904 */
905 if (acur->diff == 0 && acur->len == args->maxlen)
906 deactivate = true;
396bbf3c
BF
907out:
908 if (deactivate)
c4aa10d0 909 cur->bc_ag.abt.active = false;
396bbf3c
BF
910 trace_xfs_alloc_cur_check(args->mp, cur->bc_btnum, bno, len, diff,
911 *new);
912 return 0;
913}
914
d2968825
BF
915/*
916 * Complete an allocation of a candidate extent. Remove the extent from both
917 * trees and update the args structure.
918 */
919STATIC int
920xfs_alloc_cur_finish(
921 struct xfs_alloc_arg *args,
922 struct xfs_alloc_cur *acur)
923{
9798f615 924 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
d2968825
BF
925 int error;
926
927 ASSERT(acur->cnt && acur->bnolt);
928 ASSERT(acur->bno >= acur->rec_bno);
929 ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len);
9798f615 930 ASSERT(acur->rec_bno + acur->rec_len <= be32_to_cpu(agf->agf_length));
d2968825
BF
931
932 error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno,
933 acur->rec_len, acur->bno, acur->len, 0);
934 if (error)
935 return error;
936
937 args->agbno = acur->bno;
938 args->len = acur->len;
939 args->wasfromfl = 0;
940
941 trace_xfs_alloc_cur(args);
942 return 0;
943}
944
dc8e69bd
BF
945/*
946 * Locality allocation lookup algorithm. This expects a cntbt cursor and uses
947 * bno optimized lookup to search for extents with ideal size and locality.
948 */
949STATIC int
950xfs_alloc_cntbt_iter(
951 struct xfs_alloc_arg *args,
952 struct xfs_alloc_cur *acur)
953{
954 struct xfs_btree_cur *cur = acur->cnt;
955 xfs_agblock_t bno;
956 xfs_extlen_t len, cur_len;
957 int error;
958 int i;
959
960 if (!xfs_alloc_cur_active(cur))
961 return 0;
962
963 /* locality optimized lookup */
964 cur_len = acur->cur_len;
965 error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i);
966 if (error)
967 return error;
968 if (i == 0)
969 return 0;
970 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
971 if (error)
972 return error;
973
974 /* check the current record and update search length from it */
975 error = xfs_alloc_cur_check(args, acur, cur, &i);
976 if (error)
977 return error;
978 ASSERT(len >= acur->cur_len);
979 acur->cur_len = len;
980
981 /*
982 * We looked up the first record >= [agbno, len] above. The agbno is a
983 * secondary key and so the current record may lie just before or after
984 * agbno. If it is past agbno, check the previous record too so long as
985 * the length matches as it may be closer. Don't check a smaller record
986 * because that could deactivate our cursor.
987 */
988 if (bno > args->agbno) {
989 error = xfs_btree_decrement(cur, 0, &i);
990 if (!error && i) {
991 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
992 if (!error && i && len == acur->cur_len)
993 error = xfs_alloc_cur_check(args, acur, cur,
994 &i);
995 }
996 if (error)
997 return error;
998 }
999
1000 /*
1001 * Increment the search key until we find at least one allocation
1002 * candidate or if the extent we found was larger. Otherwise, double the
1003 * search key to optimize the search. Efficiency is more important here
1004 * than absolute best locality.
1005 */
1006 cur_len <<= 1;
1007 if (!acur->len || acur->cur_len >= cur_len)
1008 acur->cur_len++;
1009 else
1010 acur->cur_len = cur_len;
1011
1012 return error;
1013}
1014
c63cdd4f
BF
1015/*
1016 * Deal with the case where only small freespaces remain. Either return the
1017 * contents of the last freespace record, or allocate space from the freelist if
1018 * there is nothing in the tree.
1019 */
1020STATIC int /* error */
1021xfs_alloc_ag_vextent_small(
1022 struct xfs_alloc_arg *args, /* allocation argument structure */
1023 struct xfs_btree_cur *ccur, /* optional by-size cursor */
1024 xfs_agblock_t *fbnop, /* result block number */
1025 xfs_extlen_t *flenp, /* result length */
1026 int *stat) /* status: 0-freelist, 1-normal/none */
1027{
9798f615 1028 struct xfs_agf *agf = args->agbp->b_addr;
c63cdd4f
BF
1029 int error = 0;
1030 xfs_agblock_t fbno = NULLAGBLOCK;
1031 xfs_extlen_t flen = 0;
6691cd92 1032 int i = 0;
c63cdd4f 1033
6691cd92
BF
1034 /*
1035 * If a cntbt cursor is provided, try to allocate the largest record in
1036 * the tree. Try the AGFL if the cntbt is empty, otherwise fail the
1037 * allocation. Make sure to respect minleft even when pulling from the
1038 * freelist.
1039 */
1040 if (ccur)
1041 error = xfs_btree_decrement(ccur, 0, &i);
c63cdd4f
BF
1042 if (error)
1043 goto error;
1044 if (i) {
1045 error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i);
1046 if (error)
1047 goto error;
f9e03706
DW
1048 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1049 error = -EFSCORRUPTED;
1050 goto error;
1051 }
c63cdd4f
BF
1052 goto out;
1053 }
1054
1055 if (args->minlen != 1 || args->alignment != 1 ||
1056 args->resv == XFS_AG_RESV_AGFL ||
9798f615 1057 be32_to_cpu(agf->agf_flcount) <= args->minleft)
c63cdd4f
BF
1058 goto out;
1059
1060 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1061 if (error)
1062 goto error;
1063 if (fbno == NULLAGBLOCK)
1064 goto out;
1065
45d06621 1066 xfs_extent_busy_reuse(args->mp, args->pag, fbno, 1,
c34d570d 1067 (args->datatype & XFS_ALLOC_NOBUSY));
c63cdd4f 1068
c34d570d 1069 if (args->datatype & XFS_ALLOC_USERDATA) {
c63cdd4f
BF
1070 struct xfs_buf *bp;
1071
ee647f85
DW
1072 error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp,
1073 XFS_AGB_TO_DADDR(args->mp, args->agno, fbno),
1074 args->mp->m_bsize, 0, &bp);
1075 if (error)
c63cdd4f 1076 goto error;
c63cdd4f
BF
1077 xfs_trans_binval(args->tp, bp);
1078 }
7e36a3a6
BF
1079 *fbnop = args->agbno = fbno;
1080 *flenp = args->len = 1;
9798f615 1081 if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) {
f9e03706
DW
1082 error = -EFSCORRUPTED;
1083 goto error;
1084 }
c63cdd4f
BF
1085 args->wasfromfl = 1;
1086 trace_xfs_alloc_small_freelist(args);
1087
1088 /*
1089 * If we're feeding an AGFL block to something that doesn't live in the
1090 * free space, we need to clear out the OWN_AG rmap.
1091 */
fa9c3c19 1092 error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1,
c63cdd4f
BF
1093 &XFS_RMAP_OINFO_AG);
1094 if (error)
1095 goto error;
1096
1097 *stat = 0;
1098 return 0;
1099
1100out:
1101 /*
1102 * Can't do the allocation, give up.
1103 */
1104 if (flen < args->minlen) {
1105 args->agbno = NULLAGBLOCK;
1106 trace_xfs_alloc_small_notenough(args);
1107 flen = 0;
1108 }
1109 *fbnop = fbno;
1110 *flenp = flen;
1111 *stat = 1;
1112 trace_xfs_alloc_small_done(args);
1113 return 0;
1114
1115error:
1116 trace_xfs_alloc_small_error(args);
1117 return error;
1118}
1119
1da177e4
LT
1120/*
1121 * Allocate a variable extent in the allocation group agno.
1122 * Type and bno are used to determine where in the allocation group the
1123 * extent will start.
1124 * Extent's length (returned in *len) will be between minlen and maxlen,
1125 * and of the form k * prod + mod unless there's nothing that large.
1126 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1127 */
1128STATIC int /* error */
1129xfs_alloc_ag_vextent(
1130 xfs_alloc_arg_t *args) /* argument structure for allocation */
1131{
1132 int error=0;
1da177e4
LT
1133
1134 ASSERT(args->minlen > 0);
1135 ASSERT(args->maxlen > 0);
1136 ASSERT(args->minlen <= args->maxlen);
1137 ASSERT(args->mod < args->prod);
1138 ASSERT(args->alignment > 0);
3fd129b6 1139
1da177e4
LT
1140 /*
1141 * Branch to correct routine based on the type.
1142 */
1143 args->wasfromfl = 0;
1144 switch (args->type) {
1145 case XFS_ALLOCTYPE_THIS_AG:
1146 error = xfs_alloc_ag_vextent_size(args);
1147 break;
1148 case XFS_ALLOCTYPE_NEAR_BNO:
1149 error = xfs_alloc_ag_vextent_near(args);
1150 break;
1151 case XFS_ALLOCTYPE_THIS_BNO:
1152 error = xfs_alloc_ag_vextent_exact(args);
1153 break;
1154 default:
1155 ASSERT(0);
1156 /* NOTREACHED */
1157 }
ecb6928f
CH
1158
1159 if (error || args->agbno == NULLAGBLOCK)
1da177e4 1160 return error;
ecb6928f
CH
1161
1162 ASSERT(args->len >= args->minlen);
1163 ASSERT(args->len <= args->maxlen);
0ab32086 1164 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
ecb6928f
CH
1165 ASSERT(args->agbno % args->alignment == 0);
1166
673930c3 1167 /* if not file data, insert new block into the reverse map btree */
33df3a9c 1168 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
fa9c3c19 1169 error = xfs_rmap_alloc(args->tp, args->agbp, args->pag,
673930c3
DW
1170 args->agbno, args->len, &args->oinfo);
1171 if (error)
1172 return error;
1173 }
1174
ecb6928f 1175 if (!args->wasfromfl) {
92a00544 1176 error = xfs_alloc_update_counters(args->tp, args->agbp,
ecb6928f
CH
1177 -((long)(args->len)));
1178 if (error)
1179 return error;
1180
45d06621 1181 ASSERT(!xfs_extent_busy_search(args->mp, args->pag,
e26f0501 1182 args->agbno, args->len));
1da177e4 1183 }
ecb6928f 1184
3fd129b6 1185 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
ecb6928f 1186
ff6d6af2
BD
1187 XFS_STATS_INC(args->mp, xs_allocx);
1188 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
ecb6928f 1189 return error;
1da177e4
LT
1190}
1191
1192/*
1193 * Allocate a variable extent at exactly agno/bno.
1194 * Extent's length (returned in *len) will be between minlen and maxlen,
1195 * and of the form k * prod + mod unless there's nothing that large.
1196 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
1197 */
1198STATIC int /* error */
1199xfs_alloc_ag_vextent_exact(
1200 xfs_alloc_arg_t *args) /* allocation argument structure */
1201{
9798f615 1202 struct xfs_agf __maybe_unused *agf = args->agbp->b_addr;
ae127f08
DW
1203 struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */
1204 struct xfs_btree_cur *cnt_cur;/* by count btree cursor */
1da177e4
LT
1205 int error;
1206 xfs_agblock_t fbno; /* start block of found extent */
1da177e4 1207 xfs_extlen_t flen; /* length of found extent */
ebf55872
CH
1208 xfs_agblock_t tbno; /* start block of busy extent */
1209 xfs_extlen_t tlen; /* length of busy extent */
1210 xfs_agblock_t tend; /* end block of busy extent */
1da177e4 1211 int i; /* success/failure of operation */
ebf55872 1212 unsigned busy_gen;
1da177e4
LT
1213
1214 ASSERT(args->alignment == 1);
9f9baab3 1215
1da177e4
LT
1216 /*
1217 * Allocate/initialize a cursor for the by-number freespace btree.
1218 */
561f7d17 1219 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
289d38d2 1220 args->pag, XFS_BTNUM_BNO);
9f9baab3 1221
1da177e4
LT
1222 /*
1223 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
1224 * Look for the closest free block <= bno, it must contain bno
1225 * if any free block does.
1226 */
9f9baab3
CH
1227 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
1228 if (error)
1da177e4 1229 goto error0;
9f9baab3
CH
1230 if (!i)
1231 goto not_found;
1232
1da177e4
LT
1233 /*
1234 * Grab the freespace record.
1235 */
9f9baab3
CH
1236 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
1237 if (error)
1da177e4 1238 goto error0;
f9e03706
DW
1239 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1240 error = -EFSCORRUPTED;
1241 goto error0;
1242 }
1da177e4 1243 ASSERT(fbno <= args->agbno);
9f9baab3 1244
1da177e4 1245 /*
e26f0501 1246 * Check for overlapping busy extents.
1da177e4 1247 */
ebf55872
CH
1248 tbno = fbno;
1249 tlen = flen;
1250 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
e26f0501
CH
1251
1252 /*
1253 * Give up if the start of the extent is busy, or the freespace isn't
1254 * long enough for the minimum request.
1255 */
1256 if (tbno > args->agbno)
1257 goto not_found;
1258 if (tlen < args->minlen)
1259 goto not_found;
1260 tend = tbno + tlen;
1261 if (tend < args->agbno + args->minlen)
9f9baab3
CH
1262 goto not_found;
1263
1da177e4
LT
1264 /*
1265 * End of extent will be smaller of the freespace end and the
1266 * maximal requested end.
9f9baab3 1267 *
1da177e4
LT
1268 * Fix the length according to mod and prod if given.
1269 */
81463b1c
CS
1270 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
1271 - args->agbno;
1da177e4 1272 xfs_alloc_fix_len(args);
81463b1c 1273 ASSERT(args->agbno + args->len <= tend);
9f9baab3 1274
1da177e4 1275 /*
81463b1c 1276 * We are allocating agbno for args->len
1da177e4
LT
1277 * Allocate/initialize a cursor for the by-size btree.
1278 */
561f7d17 1279 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
289d38d2 1280 args->pag, XFS_BTNUM_CNT);
9798f615 1281 ASSERT(args->agbno + args->len <= be32_to_cpu(agf->agf_length));
9f9baab3
CH
1282 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
1283 args->len, XFSA_FIXUP_BNO_OK);
1284 if (error) {
1da177e4
LT
1285 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1286 goto error0;
1287 }
9f9baab3 1288
1da177e4
LT
1289 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1290 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
0b1b213f 1291
1da177e4 1292 args->wasfromfl = 0;
9f9baab3
CH
1293 trace_xfs_alloc_exact_done(args);
1294 return 0;
1295
1296not_found:
1297 /* Didn't find it, return null. */
1298 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1299 args->agbno = NULLAGBLOCK;
1300 trace_xfs_alloc_exact_notfound(args);
1da177e4
LT
1301 return 0;
1302
1303error0:
1304 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
0b1b213f 1305 trace_xfs_alloc_exact_error(args);
1da177e4
LT
1306 return error;
1307}
1308
489a150f 1309/*
78d7aabd
BF
1310 * Search a given number of btree records in a given direction. Check each
1311 * record against the good extent we've already found.
489a150f
CH
1312 */
1313STATIC int
78d7aabd 1314xfs_alloc_walk_iter(
fec0afda
BF
1315 struct xfs_alloc_arg *args,
1316 struct xfs_alloc_cur *acur,
1317 struct xfs_btree_cur *cur,
78d7aabd
BF
1318 bool increment,
1319 bool find_one, /* quit on first candidate */
1320 int count, /* rec count (-1 for infinite) */
1321 int *stat)
489a150f 1322{
489a150f
CH
1323 int error;
1324 int i;
489a150f 1325
78d7aabd
BF
1326 *stat = 0;
1327
489a150f 1328 /*
fec0afda
BF
1329 * Search so long as the cursor is active or we find a better extent.
1330 * The cursor is deactivated if it extends beyond the range of the
1331 * current allocation candidate.
489a150f 1332 */
78d7aabd 1333 while (xfs_alloc_cur_active(cur) && count) {
fec0afda 1334 error = xfs_alloc_cur_check(args, acur, cur, &i);
489a150f 1335 if (error)
fec0afda 1336 return error;
78d7aabd
BF
1337 if (i == 1) {
1338 *stat = 1;
1339 if (find_one)
1340 break;
1341 }
fec0afda
BF
1342 if (!xfs_alloc_cur_active(cur))
1343 break;
489a150f 1344
fec0afda
BF
1345 if (increment)
1346 error = xfs_btree_increment(cur, 0, &i);
489a150f 1347 else
fec0afda 1348 error = xfs_btree_decrement(cur, 0, &i);
489a150f 1349 if (error)
fec0afda
BF
1350 return error;
1351 if (i == 0)
c4aa10d0 1352 cur->bc_ag.abt.active = false;
78d7aabd
BF
1353
1354 if (count > 0)
1355 count--;
fec0afda 1356 }
489a150f 1357
489a150f 1358 return 0;
489a150f
CH
1359}
1360
0e26d5ca 1361/*
dc8e69bd
BF
1362 * Search the by-bno and by-size btrees in parallel in search of an extent with
1363 * ideal locality based on the NEAR mode ->agbno locality hint.
0e26d5ca
BF
1364 */
1365STATIC int
dc8e69bd 1366xfs_alloc_ag_vextent_locality(
0e26d5ca
BF
1367 struct xfs_alloc_arg *args,
1368 struct xfs_alloc_cur *acur,
1369 int *stat)
1370{
1371 struct xfs_btree_cur *fbcur = NULL;
1372 int error;
1373 int i;
1374 bool fbinc;
1375
1376 ASSERT(acur->len == 0);
1377 ASSERT(args->type == XFS_ALLOCTYPE_NEAR_BNO);
1378
1379 *stat = 0;
1380
dc8e69bd
BF
1381 error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i);
1382 if (error)
1383 return error;
0e26d5ca
BF
1384 error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i);
1385 if (error)
1386 return error;
1387 error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i);
1388 if (error)
1389 return error;
1390
1391 /*
dc8e69bd
BF
1392 * Search the bnobt and cntbt in parallel. Search the bnobt left and
1393 * right and lookup the closest extent to the locality hint for each
1394 * extent size key in the cntbt. The entire search terminates
1395 * immediately on a bnobt hit because that means we've found best case
1396 * locality. Otherwise the search continues until the cntbt cursor runs
1397 * off the end of the tree. If no allocation candidate is found at this
1398 * point, give up on locality, walk backwards from the end of the cntbt
1399 * and take the first available extent.
1400 *
1401 * The parallel tree searches balance each other out to provide fairly
1402 * consistent performance for various situations. The bnobt search can
1403 * have pathological behavior in the worst case scenario of larger
1404 * allocation requests and fragmented free space. On the other hand, the
1405 * bnobt is able to satisfy most smaller allocation requests much more
1406 * quickly than the cntbt. The cntbt search can sift through fragmented
1407 * free space and sets of free extents for larger allocation requests
1408 * more quickly than the bnobt. Since the locality hint is just a hint
1409 * and we don't want to scan the entire bnobt for perfect locality, the
1410 * cntbt search essentially bounds the bnobt search such that we can
1411 * find good enough locality at reasonable performance in most cases.
0e26d5ca
BF
1412 */
1413 while (xfs_alloc_cur_active(acur->bnolt) ||
dc8e69bd
BF
1414 xfs_alloc_cur_active(acur->bnogt) ||
1415 xfs_alloc_cur_active(acur->cnt)) {
1416
1417 trace_xfs_alloc_cur_lookup(args);
1418
1419 /*
1420 * Search the bnobt left and right. In the case of a hit, finish
1421 * the search in the opposite direction and we're done.
1422 */
0e26d5ca
BF
1423 error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false,
1424 true, 1, &i);
1425 if (error)
1426 return error;
1427 if (i == 1) {
1428 trace_xfs_alloc_cur_left(args);
1429 fbcur = acur->bnogt;
1430 fbinc = true;
1431 break;
1432 }
0e26d5ca
BF
1433 error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true,
1434 1, &i);
1435 if (error)
1436 return error;
1437 if (i == 1) {
1438 trace_xfs_alloc_cur_right(args);
1439 fbcur = acur->bnolt;
1440 fbinc = false;
1441 break;
1442 }
dc8e69bd
BF
1443
1444 /*
1445 * Check the extent with best locality based on the current
1446 * extent size search key and keep track of the best candidate.
1447 */
1448 error = xfs_alloc_cntbt_iter(args, acur);
1449 if (error)
1450 return error;
1451 if (!xfs_alloc_cur_active(acur->cnt)) {
1452 trace_xfs_alloc_cur_lookup_done(args);
1453 break;
1454 }
1455 }
1456
1457 /*
1458 * If we failed to find anything due to busy extents, return empty
1459 * handed so the caller can flush and retry. If no busy extents were
1460 * found, walk backwards from the end of the cntbt as a last resort.
1461 */
1462 if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) {
1463 error = xfs_btree_decrement(acur->cnt, 0, &i);
1464 if (error)
1465 return error;
1466 if (i) {
c4aa10d0 1467 acur->cnt->bc_ag.abt.active = true;
dc8e69bd
BF
1468 fbcur = acur->cnt;
1469 fbinc = false;
1470 }
0e26d5ca
BF
1471 }
1472
dc8e69bd
BF
1473 /*
1474 * Search in the opposite direction for a better entry in the case of
1475 * a bnobt hit or walk backwards from the end of the cntbt.
1476 */
0e26d5ca
BF
1477 if (fbcur) {
1478 error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1,
1479 &i);
1480 if (error)
1481 return error;
1482 }
1483
1484 if (acur->len)
1485 *stat = 1;
1486
1487 return 0;
1488}
1489
5113f8ec
DW
1490/* Check the last block of the cnt btree for allocations. */
1491static int
1492xfs_alloc_ag_vextent_lastblock(
1493 struct xfs_alloc_arg *args,
1494 struct xfs_alloc_cur *acur,
1495 xfs_agblock_t *bno,
1496 xfs_extlen_t *len,
1497 bool *allocated)
1498{
1499 int error;
1500 int i;
1501
1502#ifdef DEBUG
1503 /* Randomly don't execute the first algorithm. */
1504 if (prandom_u32() & 1)
1505 return 0;
1506#endif
1507
1508 /*
1509 * Start from the entry that lookup found, sequence through all larger
1510 * free blocks. If we're actually pointing at a record smaller than
1511 * maxlen, go to the start of this block, and skip all those smaller
1512 * than minlen.
1513 */
77ca1eed 1514 if (*len || args->alignment > 1) {
6ca444cf 1515 acur->cnt->bc_levels[0].ptr = 1;
5113f8ec
DW
1516 do {
1517 error = xfs_alloc_get_rec(acur->cnt, bno, len, &i);
1518 if (error)
1519 return error;
f9e03706
DW
1520 if (XFS_IS_CORRUPT(args->mp, i != 1))
1521 return -EFSCORRUPTED;
5113f8ec
DW
1522 if (*len >= args->minlen)
1523 break;
1524 error = xfs_btree_increment(acur->cnt, 0, &i);
1525 if (error)
1526 return error;
1527 } while (i);
1528 ASSERT(*len >= args->minlen);
1529 if (!i)
1530 return 0;
1531 }
1532
1533 error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i);
1534 if (error)
1535 return error;
1536
1537 /*
1538 * It didn't work. We COULD be in a case where there's a good record
1539 * somewhere, so try again.
1540 */
1541 if (acur->len == 0)
1542 return 0;
1543
1544 trace_xfs_alloc_near_first(args);
1545 *allocated = true;
1546 return 0;
1547}
1548
1da177e4
LT
1549/*
1550 * Allocate a variable extent near bno in the allocation group agno.
1551 * Extent's length (returned in len) will be between minlen and maxlen,
1552 * and of the form k * prod + mod unless there's nothing that large.
1553 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1554 */
f5e7dbea 1555STATIC int
1da177e4 1556xfs_alloc_ag_vextent_near(
f5e7dbea 1557 struct xfs_alloc_arg *args)
1da177e4 1558{
f5e7dbea 1559 struct xfs_alloc_cur acur = {};
fec0afda
BF
1560 int error; /* error code */
1561 int i; /* result code, temporary */
fec0afda
BF
1562 xfs_agblock_t bno;
1563 xfs_extlen_t len;
e26f0501 1564
cf085a1b 1565 /* handle uninitialized agbno range so caller doesn't have to */
bfe46d4e
BF
1566 if (!args->min_agbno && !args->max_agbno)
1567 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1568 ASSERT(args->min_agbno <= args->max_agbno);
1569
1570 /* clamp agbno to the range if it's outside */
1571 if (args->agbno < args->min_agbno)
1572 args->agbno = args->min_agbno;
1573 if (args->agbno > args->max_agbno)
1574 args->agbno = args->max_agbno;
1575
e26f0501 1576restart:
fec0afda 1577 len = 0;
e26f0501 1578
1da177e4 1579 /*
f5e7dbea
BF
1580 * Set up cursors and see if there are any free extents as big as
1581 * maxlen. If not, pick the last entry in the tree unless the tree is
1582 * empty.
1da177e4 1583 */
f5e7dbea
BF
1584 error = xfs_alloc_cur_setup(args, &acur);
1585 if (error == -ENOSPC) {
fec0afda
BF
1586 error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno,
1587 &len, &i);
f5e7dbea
BF
1588 if (error)
1589 goto out;
fec0afda 1590 if (i == 0 || len == 0) {
e26f0501 1591 trace_xfs_alloc_near_noentry(args);
f5e7dbea 1592 goto out;
1da177e4
LT
1593 }
1594 ASSERT(i == 1);
f5e7dbea
BF
1595 } else if (error) {
1596 goto out;
1da177e4 1597 }
e26f0501 1598
1da177e4
LT
1599 /*
1600 * First algorithm.
1601 * If the requested extent is large wrt the freespaces available
1602 * in this a.g., then the cursor will be pointing to a btree entry
1603 * near the right edge of the tree. If it's in the last btree leaf
1604 * block, then we just examine all the entries in that block
1605 * that are big enough, and pick the best one.
1da177e4 1606 */
5113f8ec
DW
1607 if (xfs_btree_islastblock(acur.cnt, 0)) {
1608 bool allocated = false;
78d7aabd 1609
5113f8ec
DW
1610 error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len,
1611 &allocated);
78d7aabd
BF
1612 if (error)
1613 goto out;
5113f8ec
DW
1614 if (allocated)
1615 goto alloc_finish;
1da177e4 1616 }
f5e7dbea 1617
1da177e4 1618 /*
dc8e69bd
BF
1619 * Second algorithm. Combined cntbt and bnobt search to find ideal
1620 * locality.
1da177e4 1621 */
dc8e69bd 1622 error = xfs_alloc_ag_vextent_locality(args, &acur, &i);
f5e7dbea
BF
1623 if (error)
1624 goto out;
1625
1da177e4
LT
1626 /*
1627 * If we couldn't get anything, give up.
1628 */
fec0afda 1629 if (!acur.len) {
d6d3aff2 1630 if (acur.busy) {
e26f0501 1631 trace_xfs_alloc_near_busy(args);
d6d3aff2
BF
1632 xfs_extent_busy_flush(args->mp, args->pag,
1633 acur.busy_gen);
e26f0501
CH
1634 goto restart;
1635 }
0b1b213f 1636 trace_xfs_alloc_size_neither(args);
1da177e4 1637 args->agbno = NULLAGBLOCK;
f5e7dbea 1638 goto out;
1da177e4 1639 }
489a150f 1640
5113f8ec 1641alloc_finish:
d2968825
BF
1642 /* fix up btrees on a successful allocation */
1643 error = xfs_alloc_cur_finish(args, &acur);
0b1b213f 1644
f5e7dbea
BF
1645out:
1646 xfs_alloc_cur_close(&acur, error);
1da177e4
LT
1647 return error;
1648}
1649
1650/*
1651 * Allocate a variable extent anywhere in the allocation group agno.
1652 * Extent's length (returned in len) will be between minlen and maxlen,
1653 * and of the form k * prod + mod unless there's nothing that large.
1654 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1655 */
1656STATIC int /* error */
1657xfs_alloc_ag_vextent_size(
1658 xfs_alloc_arg_t *args) /* allocation argument structure */
1659{
9798f615 1660 struct xfs_agf *agf = args->agbp->b_addr;
ae127f08
DW
1661 struct xfs_btree_cur *bno_cur; /* cursor for bno btree */
1662 struct xfs_btree_cur *cnt_cur; /* cursor for cnt btree */
1da177e4
LT
1663 int error; /* error result */
1664 xfs_agblock_t fbno; /* start of found freespace */
1665 xfs_extlen_t flen; /* length of found freespace */
1da177e4
LT
1666 int i; /* temp status variable */
1667 xfs_agblock_t rbno; /* returned block number */
1668 xfs_extlen_t rlen; /* length of returned extent */
ebf55872
CH
1669 bool busy;
1670 unsigned busy_gen;
1da177e4 1671
e26f0501 1672restart:
1da177e4
LT
1673 /*
1674 * Allocate and initialize a cursor for the by-size btree.
1675 */
561f7d17 1676 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
289d38d2 1677 args->pag, XFS_BTNUM_CNT);
1da177e4 1678 bno_cur = NULL;
e26f0501 1679
1da177e4
LT
1680 /*
1681 * Look for an entry >= maxlen+alignment-1 blocks.
1682 */
1683 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1684 args->maxlen + args->alignment - 1, &i)))
1685 goto error0;
e26f0501 1686
1da177e4 1687 /*
ebf55872
CH
1688 * If none then we have to settle for a smaller extent. In the case that
1689 * there are no large extents, this will return the last entry in the
1690 * tree unless the tree is empty. In the case that there are only busy
1691 * large extents, this will return the largest small extent unless there
e26f0501 1692 * are no smaller extents available.
1da177e4 1693 */
ebf55872 1694 if (!i) {
e26f0501
CH
1695 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1696 &fbno, &flen, &i);
1697 if (error)
1da177e4
LT
1698 goto error0;
1699 if (i == 0 || flen == 0) {
1700 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
0b1b213f 1701 trace_xfs_alloc_size_noentry(args);
1da177e4
LT
1702 return 0;
1703 }
1704 ASSERT(i == 1);
ebf55872
CH
1705 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1706 &rlen, &busy_gen);
e26f0501
CH
1707 } else {
1708 /*
1709 * Search for a non-busy extent that is large enough.
e26f0501
CH
1710 */
1711 for (;;) {
1712 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1713 if (error)
1714 goto error0;
f9e03706
DW
1715 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1716 error = -EFSCORRUPTED;
1717 goto error0;
1718 }
e26f0501 1719
ebf55872
CH
1720 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1721 &rbno, &rlen, &busy_gen);
e26f0501
CH
1722
1723 if (rlen >= args->maxlen)
1724 break;
1725
1726 error = xfs_btree_increment(cnt_cur, 0, &i);
1727 if (error)
1728 goto error0;
1729 if (i == 0) {
1730 /*
1731 * Our only valid extents must have been busy.
1732 * Make it unbusy by forcing the log out and
ebf55872 1733 * retrying.
e26f0501
CH
1734 */
1735 xfs_btree_del_cursor(cnt_cur,
1736 XFS_BTREE_NOERROR);
1737 trace_xfs_alloc_size_busy(args);
ebf55872
CH
1738 xfs_extent_busy_flush(args->mp,
1739 args->pag, busy_gen);
e26f0501
CH
1740 goto restart;
1741 }
1742 }
1da177e4 1743 }
e26f0501 1744
1da177e4
LT
1745 /*
1746 * In the first case above, we got the last entry in the
1747 * by-size btree. Now we check to see if the space hits maxlen
1748 * once aligned; if not, we search left for something better.
1749 * This can't happen in the second case above.
1750 */
1da177e4 1751 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
f9e03706
DW
1752 if (XFS_IS_CORRUPT(args->mp,
1753 rlen != 0 &&
1754 (rlen > flen ||
1755 rbno + rlen > fbno + flen))) {
1756 error = -EFSCORRUPTED;
1757 goto error0;
1758 }
1da177e4
LT
1759 if (rlen < args->maxlen) {
1760 xfs_agblock_t bestfbno;
1761 xfs_extlen_t bestflen;
1762 xfs_agblock_t bestrbno;
1763 xfs_extlen_t bestrlen;
1764
1765 bestrlen = rlen;
1766 bestrbno = rbno;
1767 bestflen = flen;
1768 bestfbno = fbno;
1769 for (;;) {
8df4da4a 1770 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1da177e4
LT
1771 goto error0;
1772 if (i == 0)
1773 break;
1774 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1775 &i)))
1776 goto error0;
f9e03706
DW
1777 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1778 error = -EFSCORRUPTED;
1779 goto error0;
1780 }
1da177e4
LT
1781 if (flen < bestrlen)
1782 break;
ebf55872
CH
1783 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1784 &rbno, &rlen, &busy_gen);
1da177e4 1785 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
f9e03706
DW
1786 if (XFS_IS_CORRUPT(args->mp,
1787 rlen != 0 &&
1788 (rlen > flen ||
1789 rbno + rlen > fbno + flen))) {
1790 error = -EFSCORRUPTED;
1791 goto error0;
1792 }
1da177e4
LT
1793 if (rlen > bestrlen) {
1794 bestrlen = rlen;
1795 bestrbno = rbno;
1796 bestflen = flen;
1797 bestfbno = fbno;
1798 if (rlen == args->maxlen)
1799 break;
1800 }
1801 }
1802 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1803 &i)))
1804 goto error0;
f9e03706
DW
1805 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1806 error = -EFSCORRUPTED;
1807 goto error0;
1808 }
1da177e4
LT
1809 rlen = bestrlen;
1810 rbno = bestrbno;
1811 flen = bestflen;
1812 fbno = bestfbno;
1813 }
1814 args->wasfromfl = 0;
1815 /*
1816 * Fix up the length.
1817 */
1818 args->len = rlen;
e26f0501 1819 if (rlen < args->minlen) {
ebf55872 1820 if (busy) {
e26f0501
CH
1821 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1822 trace_xfs_alloc_size_busy(args);
ebf55872 1823 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
e26f0501
CH
1824 goto restart;
1825 }
1826 goto out_nominleft;
1da177e4 1827 }
e26f0501
CH
1828 xfs_alloc_fix_len(args);
1829
1da177e4 1830 rlen = args->len;
f9e03706
DW
1831 if (XFS_IS_CORRUPT(args->mp, rlen > flen)) {
1832 error = -EFSCORRUPTED;
1833 goto error0;
1834 }
1da177e4
LT
1835 /*
1836 * Allocate and initialize a cursor for the by-block tree.
1837 */
561f7d17 1838 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
289d38d2 1839 args->pag, XFS_BTNUM_BNO);
1da177e4
LT
1840 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1841 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1842 goto error0;
1843 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1844 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1845 cnt_cur = bno_cur = NULL;
1846 args->len = rlen;
1847 args->agbno = rbno;
f9e03706
DW
1848 if (XFS_IS_CORRUPT(args->mp,
1849 args->agbno + args->len >
9798f615 1850 be32_to_cpu(agf->agf_length))) {
f9e03706
DW
1851 error = -EFSCORRUPTED;
1852 goto error0;
1853 }
0b1b213f 1854 trace_xfs_alloc_size_done(args);
1da177e4
LT
1855 return 0;
1856
1857error0:
0b1b213f 1858 trace_xfs_alloc_size_error(args);
1da177e4
LT
1859 if (cnt_cur)
1860 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1861 if (bno_cur)
1862 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1863 return error;
e26f0501
CH
1864
1865out_nominleft:
1866 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1867 trace_xfs_alloc_size_nominleft(args);
1868 args->agbno = NULLAGBLOCK;
1869 return 0;
1da177e4
LT
1870}
1871
1da177e4
LT
1872/*
1873 * Free the extent starting at agno/bno for length.
1874 */
340785cc 1875STATIC int
1da177e4 1876xfs_free_ag_extent(
66e3237e
DW
1877 struct xfs_trans *tp,
1878 struct xfs_buf *agbp,
1879 xfs_agnumber_t agno,
1880 xfs_agblock_t bno,
1881 xfs_extlen_t len,
1882 const struct xfs_owner_info *oinfo,
1883 enum xfs_ag_resv_type type)
1da177e4 1884{
66e3237e 1885 struct xfs_mount *mp;
66e3237e
DW
1886 struct xfs_btree_cur *bno_cur;
1887 struct xfs_btree_cur *cnt_cur;
1888 xfs_agblock_t gtbno; /* start of right neighbor */
1889 xfs_extlen_t gtlen; /* length of right neighbor */
1890 xfs_agblock_t ltbno; /* start of left neighbor */
1891 xfs_extlen_t ltlen; /* length of left neighbor */
1892 xfs_agblock_t nbno; /* new starting block of freesp */
1893 xfs_extlen_t nlen; /* new length of freespace */
1894 int haveleft; /* have a left neighbor */
1895 int haveright; /* have a right neighbor */
1896 int i;
1897 int error;
fa9c3c19 1898 struct xfs_perag *pag = agbp->b_pag;
1da177e4 1899
673930c3 1900 bno_cur = cnt_cur = NULL;
1da177e4 1901 mp = tp->t_mountp;
673930c3 1902
33df3a9c 1903 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
fa9c3c19 1904 error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo);
673930c3
DW
1905 if (error)
1906 goto error0;
1907 }
1908
1da177e4
LT
1909 /*
1910 * Allocate and initialize a cursor for the by-block btree.
1911 */
289d38d2 1912 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_BNO);
1da177e4
LT
1913 /*
1914 * Look for a neighboring block on the left (lower block numbers)
1915 * that is contiguous with this space.
1916 */
1917 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1918 goto error0;
1919 if (haveleft) {
1920 /*
1921 * There is a block to our left.
1922 */
1923 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1924 goto error0;
f9e03706
DW
1925 if (XFS_IS_CORRUPT(mp, i != 1)) {
1926 error = -EFSCORRUPTED;
1927 goto error0;
1928 }
1da177e4
LT
1929 /*
1930 * It's not contiguous, though.
1931 */
1932 if (ltbno + ltlen < bno)
1933 haveleft = 0;
1934 else {
1935 /*
1936 * If this failure happens the request to free this
1937 * space was invalid, it's (partly) already free.
1938 * Very bad.
1939 */
f9e03706
DW
1940 if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) {
1941 error = -EFSCORRUPTED;
1942 goto error0;
1943 }
1da177e4
LT
1944 }
1945 }
1946 /*
1947 * Look for a neighboring block on the right (higher block numbers)
1948 * that is contiguous with this space.
1949 */
637aa50f 1950 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1da177e4
LT
1951 goto error0;
1952 if (haveright) {
1953 /*
1954 * There is a block to our right.
1955 */
1956 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1957 goto error0;
f9e03706
DW
1958 if (XFS_IS_CORRUPT(mp, i != 1)) {
1959 error = -EFSCORRUPTED;
1960 goto error0;
1961 }
1da177e4
LT
1962 /*
1963 * It's not contiguous, though.
1964 */
1965 if (bno + len < gtbno)
1966 haveright = 0;
1967 else {
1968 /*
1969 * If this failure happens the request to free this
1970 * space was invalid, it's (partly) already free.
1971 * Very bad.
1972 */
f9e03706
DW
1973 if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) {
1974 error = -EFSCORRUPTED;
1975 goto error0;
1976 }
1da177e4
LT
1977 }
1978 }
1979 /*
1980 * Now allocate and initialize a cursor for the by-size tree.
1981 */
289d38d2 1982 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, pag, XFS_BTNUM_CNT);
1da177e4
LT
1983 /*
1984 * Have both left and right contiguous neighbors.
1985 * Merge all three into a single free block.
1986 */
1987 if (haveleft && haveright) {
1988 /*
1989 * Delete the old by-size entry on the left.
1990 */
1991 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1992 goto error0;
f9e03706
DW
1993 if (XFS_IS_CORRUPT(mp, i != 1)) {
1994 error = -EFSCORRUPTED;
1995 goto error0;
1996 }
91cca5df 1997 if ((error = xfs_btree_delete(cnt_cur, &i)))
1da177e4 1998 goto error0;
f9e03706
DW
1999 if (XFS_IS_CORRUPT(mp, i != 1)) {
2000 error = -EFSCORRUPTED;
2001 goto error0;
2002 }
1da177e4
LT
2003 /*
2004 * Delete the old by-size entry on the right.
2005 */
2006 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2007 goto error0;
f9e03706
DW
2008 if (XFS_IS_CORRUPT(mp, i != 1)) {
2009 error = -EFSCORRUPTED;
2010 goto error0;
2011 }
91cca5df 2012 if ((error = xfs_btree_delete(cnt_cur, &i)))
1da177e4 2013 goto error0;
f9e03706
DW
2014 if (XFS_IS_CORRUPT(mp, i != 1)) {
2015 error = -EFSCORRUPTED;
2016 goto error0;
2017 }
1da177e4
LT
2018 /*
2019 * Delete the old by-block entry for the right block.
2020 */
91cca5df 2021 if ((error = xfs_btree_delete(bno_cur, &i)))
1da177e4 2022 goto error0;
f9e03706
DW
2023 if (XFS_IS_CORRUPT(mp, i != 1)) {
2024 error = -EFSCORRUPTED;
2025 goto error0;
2026 }
1da177e4
LT
2027 /*
2028 * Move the by-block cursor back to the left neighbor.
2029 */
8df4da4a 2030 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1da177e4 2031 goto error0;
f9e03706
DW
2032 if (XFS_IS_CORRUPT(mp, i != 1)) {
2033 error = -EFSCORRUPTED;
2034 goto error0;
2035 }
1da177e4
LT
2036#ifdef DEBUG
2037 /*
2038 * Check that this is the right record: delete didn't
2039 * mangle the cursor.
2040 */
2041 {
2042 xfs_agblock_t xxbno;
2043 xfs_extlen_t xxlen;
2044
2045 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
2046 &i)))
2047 goto error0;
f9e03706
DW
2048 if (XFS_IS_CORRUPT(mp,
2049 i != 1 ||
2050 xxbno != ltbno ||
2051 xxlen != ltlen)) {
2052 error = -EFSCORRUPTED;
2053 goto error0;
2054 }
1da177e4
LT
2055 }
2056#endif
2057 /*
2058 * Update remaining by-block entry to the new, joined block.
2059 */
2060 nbno = ltbno;
2061 nlen = len + ltlen + gtlen;
2062 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2063 goto error0;
2064 }
2065 /*
2066 * Have only a left contiguous neighbor.
2067 * Merge it together with the new freespace.
2068 */
2069 else if (haveleft) {
2070 /*
2071 * Delete the old by-size entry on the left.
2072 */
2073 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2074 goto error0;
f9e03706
DW
2075 if (XFS_IS_CORRUPT(mp, i != 1)) {
2076 error = -EFSCORRUPTED;
2077 goto error0;
2078 }
91cca5df 2079 if ((error = xfs_btree_delete(cnt_cur, &i)))
1da177e4 2080 goto error0;
f9e03706
DW
2081 if (XFS_IS_CORRUPT(mp, i != 1)) {
2082 error = -EFSCORRUPTED;
2083 goto error0;
2084 }
1da177e4
LT
2085 /*
2086 * Back up the by-block cursor to the left neighbor, and
2087 * update its length.
2088 */
8df4da4a 2089 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1da177e4 2090 goto error0;
f9e03706
DW
2091 if (XFS_IS_CORRUPT(mp, i != 1)) {
2092 error = -EFSCORRUPTED;
2093 goto error0;
2094 }
1da177e4
LT
2095 nbno = ltbno;
2096 nlen = len + ltlen;
2097 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2098 goto error0;
2099 }
2100 /*
2101 * Have only a right contiguous neighbor.
2102 * Merge it together with the new freespace.
2103 */
2104 else if (haveright) {
2105 /*
2106 * Delete the old by-size entry on the right.
2107 */
2108 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2109 goto error0;
f9e03706
DW
2110 if (XFS_IS_CORRUPT(mp, i != 1)) {
2111 error = -EFSCORRUPTED;
2112 goto error0;
2113 }
91cca5df 2114 if ((error = xfs_btree_delete(cnt_cur, &i)))
1da177e4 2115 goto error0;
f9e03706
DW
2116 if (XFS_IS_CORRUPT(mp, i != 1)) {
2117 error = -EFSCORRUPTED;
2118 goto error0;
2119 }
1da177e4
LT
2120 /*
2121 * Update the starting block and length of the right
2122 * neighbor in the by-block tree.
2123 */
2124 nbno = bno;
2125 nlen = len + gtlen;
2126 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2127 goto error0;
2128 }
2129 /*
2130 * No contiguous neighbors.
2131 * Insert the new freespace into the by-block tree.
2132 */
2133 else {
2134 nbno = bno;
2135 nlen = len;
4b22a571 2136 if ((error = xfs_btree_insert(bno_cur, &i)))
1da177e4 2137 goto error0;
f9e03706
DW
2138 if (XFS_IS_CORRUPT(mp, i != 1)) {
2139 error = -EFSCORRUPTED;
2140 goto error0;
2141 }
1da177e4
LT
2142 }
2143 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
2144 bno_cur = NULL;
2145 /*
2146 * In all cases we need to insert the new freespace in the by-size tree.
2147 */
2148 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
2149 goto error0;
f9e03706
DW
2150 if (XFS_IS_CORRUPT(mp, i != 0)) {
2151 error = -EFSCORRUPTED;
2152 goto error0;
2153 }
4b22a571 2154 if ((error = xfs_btree_insert(cnt_cur, &i)))
1da177e4 2155 goto error0;
f9e03706
DW
2156 if (XFS_IS_CORRUPT(mp, i != 1)) {
2157 error = -EFSCORRUPTED;
2158 goto error0;
2159 }
1da177e4
LT
2160 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2161 cnt_cur = NULL;
ecb6928f 2162
1da177e4
LT
2163 /*
2164 * Update the freespace totals in the ag and superblock.
2165 */
92a00544
GX
2166 error = xfs_alloc_update_counters(tp, agbp, len);
2167 xfs_ag_resv_free_extent(agbp->b_pag, type, tp, len);
ecb6928f
CH
2168 if (error)
2169 goto error0;
2170
ff6d6af2
BD
2171 XFS_STATS_INC(mp, xs_freex);
2172 XFS_STATS_ADD(mp, xs_freeb, len);
0b1b213f 2173
21592863 2174 trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
1da177e4 2175
1da177e4
LT
2176 return 0;
2177
2178 error0:
21592863 2179 trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
1da177e4
LT
2180 if (bno_cur)
2181 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
2182 if (cnt_cur)
2183 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
2184 return error;
2185}
2186
2187/*
2188 * Visible (exported) allocation/free functions.
2189 * Some of these are used just by xfs_alloc_btree.c and this file.
2190 */
2191
2192/*
7cb3efb4 2193 * Compute and fill in value of m_alloc_maxlevels.
1da177e4
LT
2194 */
2195void
2196xfs_alloc_compute_maxlevels(
2197 xfs_mount_t *mp) /* file system mount structure */
2198{
7cb3efb4 2199 mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
19b54ee6 2200 (mp->m_sb.sb_agblocks + 1) / 2);
0ed5f735 2201 ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
1da177e4
LT
2202}
2203
6cc87645 2204/*
3fd129b6
DW
2205 * Find the length of the longest extent in an AG. The 'need' parameter
2206 * specifies how much space we're going to need for the AGFL and the
2207 * 'reserved' parameter tells us how many blocks in this AG are reserved for
2208 * other callers.
6cc87645
DC
2209 */
2210xfs_extlen_t
2211xfs_alloc_longest_free_extent(
50adbcb4 2212 struct xfs_perag *pag,
3fd129b6
DW
2213 xfs_extlen_t need,
2214 xfs_extlen_t reserved)
6cc87645 2215{
50adbcb4 2216 xfs_extlen_t delta = 0;
6cc87645 2217
3fd129b6
DW
2218 /*
2219 * If the AGFL needs a recharge, we'll have to subtract that from the
2220 * longest extent.
2221 */
6cc87645
DC
2222 if (need > pag->pagf_flcount)
2223 delta = need - pag->pagf_flcount;
2224
3fd129b6
DW
2225 /*
2226 * If we cannot maintain others' reservations with space from the
2227 * not-longest freesp extents, we'll have to subtract /that/ from
2228 * the longest extent too.
2229 */
2230 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2231 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2232
2233 /*
2234 * If the longest extent is long enough to satisfy all the
2235 * reservations and AGFL rules in place, we can return this extent.
2236 */
6cc87645 2237 if (pag->pagf_longest > delta)
1c743574
DC
2238 return min_t(xfs_extlen_t, pag->pag_mount->m_ag_max_usable,
2239 pag->pagf_longest - delta);
3fd129b6
DW
2240
2241 /* Otherwise, let the caller try for 1 block if there's space. */
6cc87645
DC
2242 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2243}
2244
1cac233c
DW
2245/*
2246 * Compute the minimum length of the AGFL in the given AG. If @pag is NULL,
2247 * return the largest possible minimum length.
2248 */
496817b4
DC
2249unsigned int
2250xfs_alloc_min_freelist(
2251 struct xfs_mount *mp,
2252 struct xfs_perag *pag)
2253{
1cac233c
DW
2254 /* AG btrees have at least 1 level. */
2255 static const uint8_t fake_levels[XFS_BTNUM_AGF] = {1, 1, 1};
2256 const uint8_t *levels = pag ? pag->pagf_levels : fake_levels;
496817b4
DC
2257 unsigned int min_free;
2258
7cb3efb4 2259 ASSERT(mp->m_alloc_maxlevels > 0);
1cac233c 2260
496817b4 2261 /* space needed by-bno freespace btree */
1cac233c 2262 min_free = min_t(unsigned int, levels[XFS_BTNUM_BNOi] + 1,
7cb3efb4 2263 mp->m_alloc_maxlevels);
496817b4 2264 /* space needed by-size freespace btree */
1cac233c 2265 min_free += min_t(unsigned int, levels[XFS_BTNUM_CNTi] + 1,
7cb3efb4 2266 mp->m_alloc_maxlevels);
52548852 2267 /* space needed reverse mapping used space btree */
ebd9027d 2268 if (xfs_has_rmapbt(mp))
1cac233c
DW
2269 min_free += min_t(unsigned int, levels[XFS_BTNUM_RMAPi] + 1,
2270 mp->m_rmap_maxlevels);
496817b4
DC
2271
2272 return min_free;
2273}
2274
72d55285
DC
2275/*
2276 * Check if the operation we are fixing up the freelist for should go ahead or
2277 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2278 * is dependent on whether the size and shape of free space available will
2279 * permit the requested allocation to take place.
2280 */
2281static bool
2282xfs_alloc_space_available(
2283 struct xfs_alloc_arg *args,
2284 xfs_extlen_t min_free,
2285 int flags)
2286{
2287 struct xfs_perag *pag = args->pag;
12ef8301 2288 xfs_extlen_t alloc_len, longest;
3fd129b6 2289 xfs_extlen_t reservation; /* blocks that are still reserved */
72d55285 2290 int available;
1ca89fbc 2291 xfs_extlen_t agflcount;
72d55285
DC
2292
2293 if (flags & XFS_ALLOC_FLAG_FREEING)
2294 return true;
2295
3fd129b6
DW
2296 reservation = xfs_ag_resv_needed(pag, args->resv);
2297
72d55285 2298 /* do we have enough contiguous free space for the allocation? */
12ef8301 2299 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
a1f69417 2300 longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
12ef8301 2301 if (longest < alloc_len)
72d55285
DC
2302 return false;
2303
1ca89fbc
BF
2304 /*
2305 * Do we have enough free space remaining for the allocation? Don't
2306 * account extra agfl blocks because we are about to defer free them,
2307 * making them unavailable until the current transaction commits.
2308 */
2309 agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free);
2310 available = (int)(pag->pagf_freeblks + agflcount -
54fee133 2311 reservation - min_free - args->minleft);
12ef8301 2312 if (available < (int)max(args->total, alloc_len))
72d55285
DC
2313 return false;
2314
54fee133
CH
2315 /*
2316 * Clamp maxlen to the amount of free space available for the actual
2317 * extent allocation.
2318 */
2319 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2320 args->maxlen = available;
2321 ASSERT(args->maxlen > 0);
2322 ASSERT(args->maxlen >= args->minlen);
2323 }
2324
72d55285
DC
2325 return true;
2326}
2327
4223f659
BF
2328int
2329xfs_free_agfl_block(
2330 struct xfs_trans *tp,
2331 xfs_agnumber_t agno,
2332 xfs_agblock_t agbno,
2333 struct xfs_buf *agbp,
2334 struct xfs_owner_info *oinfo)
2335{
2336 int error;
2337 struct xfs_buf *bp;
2338
2339 error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
2340 XFS_AG_RESV_AGFL);
2341 if (error)
2342 return error;
2343
ee647f85
DW
2344 error = xfs_trans_get_buf(tp, tp->t_mountp->m_ddev_targp,
2345 XFS_AGB_TO_DADDR(tp->t_mountp, agno, agbno),
2346 tp->t_mountp->m_bsize, 0, &bp);
2347 if (error)
2348 return error;
4223f659
BF
2349 xfs_trans_binval(tp, bp);
2350
2351 return 0;
2352}
2353
a27ba260
BF
2354/*
2355 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2356 * is to detect an agfl header padding mismatch between current and early v5
2357 * kernels. This problem manifests as a 1-slot size difference between the
2358 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2359 * may also catch variants of agfl count corruption unrelated to padding. Either
2360 * way, we'll reset the agfl and warn the user.
2361 *
2362 * Return true if a reset is required before the agfl can be used, false
2363 * otherwise.
2364 */
2365static bool
2366xfs_agfl_needs_reset(
2367 struct xfs_mount *mp,
2368 struct xfs_agf *agf)
2369{
2370 uint32_t f = be32_to_cpu(agf->agf_flfirst);
2371 uint32_t l = be32_to_cpu(agf->agf_fllast);
2372 uint32_t c = be32_to_cpu(agf->agf_flcount);
2373 int agfl_size = xfs_agfl_size(mp);
2374 int active;
2375
2376 /* no agfl header on v4 supers */
38c26bfd 2377 if (!xfs_has_crc(mp))
a27ba260
BF
2378 return false;
2379
2380 /*
2381 * The agf read verifier catches severe corruption of these fields.
2382 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2383 * the verifier allows it.
2384 */
2385 if (f >= agfl_size || l >= agfl_size)
2386 return true;
2387 if (c > agfl_size)
2388 return true;
2389
2390 /*
2391 * Check consistency between the on-disk count and the active range. An
2392 * agfl padding mismatch manifests as an inconsistent flcount.
2393 */
2394 if (c && l >= f)
2395 active = l - f + 1;
2396 else if (c)
2397 active = agfl_size - f + l + 1;
2398 else
2399 active = 0;
2400
2401 return active != c;
2402}
2403
2404/*
2405 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2406 * agfl content cannot be trusted. Warn the user that a repair is required to
2407 * recover leaked blocks.
2408 *
2409 * The purpose of this mechanism is to handle filesystems affected by the agfl
2410 * header padding mismatch problem. A reset keeps the filesystem online with a
2411 * relatively minor free space accounting inconsistency rather than suffer the
2412 * inevitable crash from use of an invalid agfl block.
2413 */
2414static void
2415xfs_agfl_reset(
2416 struct xfs_trans *tp,
2417 struct xfs_buf *agbp,
2418 struct xfs_perag *pag)
2419{
2420 struct xfs_mount *mp = tp->t_mountp;
9798f615 2421 struct xfs_agf *agf = agbp->b_addr;
a27ba260
BF
2422
2423 ASSERT(pag->pagf_agflreset);
2424 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
2425
2426 xfs_warn(mp,
2427 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2428 "Please unmount and run xfs_repair.",
2429 pag->pag_agno, pag->pagf_flcount);
2430
2431 agf->agf_flfirst = 0;
2432 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
2433 agf->agf_flcount = 0;
2434 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
2435 XFS_AGF_FLCOUNT);
2436
2437 pag->pagf_flcount = 0;
2438 pag->pagf_agflreset = false;
2439}
2440
f8f2835a
BF
2441/*
2442 * Defer an AGFL block free. This is effectively equivalent to
c201d9ca 2443 * xfs_free_extent_later() with some special handling particular to AGFL blocks.
f8f2835a
BF
2444 *
2445 * Deferring AGFL frees helps prevent log reservation overruns due to too many
2446 * allocation operations in a transaction. AGFL frees are prone to this problem
2447 * because for one they are always freed one at a time. Further, an immediate
2448 * AGFL block free can cause a btree join and require another block free before
2449 * the real allocation can proceed. Deferring the free disconnects freeing up
2450 * the AGFL slot from freeing the block.
2451 */
2452STATIC void
2453xfs_defer_agfl_block(
0f37d178 2454 struct xfs_trans *tp,
f8f2835a
BF
2455 xfs_agnumber_t agno,
2456 xfs_fsblock_t agbno,
2457 struct xfs_owner_info *oinfo)
2458{
0f37d178 2459 struct xfs_mount *mp = tp->t_mountp;
f8f2835a
BF
2460 struct xfs_extent_free_item *new; /* new element */
2461
c201d9ca 2462 ASSERT(xfs_extfree_item_cache != NULL);
f8f2835a
BF
2463 ASSERT(oinfo != NULL);
2464
b3b5ff41 2465 new = kmem_cache_zalloc(xfs_extfree_item_cache,
3050bd0b 2466 GFP_KERNEL | __GFP_NOFAIL);
f8f2835a
BF
2467 new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
2468 new->xefi_blockcount = 1;
b3b5ff41 2469 new->xefi_owner = oinfo->oi_owner;
f8f2835a
BF
2470
2471 trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
2472
0f37d178 2473 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &new->xefi_list);
f8f2835a
BF
2474}
2475
c201d9ca
DW
2476/*
2477 * Add the extent to the list of extents to be free at transaction end.
2478 * The list is maintained sorted (by block number).
2479 */
2480void
2481__xfs_free_extent_later(
2482 struct xfs_trans *tp,
2483 xfs_fsblock_t bno,
2484 xfs_filblks_t len,
2485 const struct xfs_owner_info *oinfo,
2486 bool skip_discard)
2487{
2488 struct xfs_extent_free_item *new; /* new element */
2489#ifdef DEBUG
2490 struct xfs_mount *mp = tp->t_mountp;
2491 xfs_agnumber_t agno;
2492 xfs_agblock_t agbno;
2493
2494 ASSERT(bno != NULLFSBLOCK);
2495 ASSERT(len > 0);
2496 ASSERT(len <= MAXEXTLEN);
2497 ASSERT(!isnullstartblock(bno));
2498 agno = XFS_FSB_TO_AGNO(mp, bno);
2499 agbno = XFS_FSB_TO_AGBNO(mp, bno);
2500 ASSERT(agno < mp->m_sb.sb_agcount);
2501 ASSERT(agbno < mp->m_sb.sb_agblocks);
2502 ASSERT(len < mp->m_sb.sb_agblocks);
2503 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
2504#endif
2505 ASSERT(xfs_extfree_item_cache != NULL);
2506
b3b5ff41 2507 new = kmem_cache_zalloc(xfs_extfree_item_cache,
c201d9ca
DW
2508 GFP_KERNEL | __GFP_NOFAIL);
2509 new->xefi_startblock = bno;
2510 new->xefi_blockcount = (xfs_extlen_t)len;
b3b5ff41
DW
2511 if (skip_discard)
2512 new->xefi_flags |= XFS_EFI_SKIP_DISCARD;
2513 if (oinfo) {
2514 ASSERT(oinfo->oi_offset == 0);
2515
2516 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
2517 new->xefi_flags |= XFS_EFI_ATTR_FORK;
2518 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
2519 new->xefi_flags |= XFS_EFI_BMBT_BLOCK;
2520 new->xefi_owner = oinfo->oi_owner;
2521 } else {
2522 new->xefi_owner = XFS_RMAP_OWN_NULL;
2523 }
c201d9ca
DW
2524 trace_xfs_bmap_free_defer(tp->t_mountp,
2525 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
2526 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
2527 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
2528}
2529
30151967
CB
2530#ifdef DEBUG
2531/*
2532 * Check if an AGF has a free extent record whose length is equal to
2533 * args->minlen.
2534 */
2535STATIC int
2536xfs_exact_minlen_extent_available(
2537 struct xfs_alloc_arg *args,
2538 struct xfs_buf *agbp,
2539 int *stat)
2540{
2541 struct xfs_btree_cur *cnt_cur;
2542 xfs_agblock_t fbno;
2543 xfs_extlen_t flen;
2544 int error = 0;
2545
2546 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, agbp,
289d38d2 2547 args->pag, XFS_BTNUM_CNT);
30151967
CB
2548 error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat);
2549 if (error)
2550 goto out;
2551
2552 if (*stat == 0) {
2553 error = -EFSCORRUPTED;
2554 goto out;
2555 }
2556
2557 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat);
2558 if (error)
2559 goto out;
2560
2561 if (*stat == 1 && flen != args->minlen)
2562 *stat = 0;
2563
2564out:
2565 xfs_btree_del_cursor(cnt_cur, error);
2566
2567 return error;
2568}
2569#endif
2570
1da177e4
LT
2571/*
2572 * Decide whether to use this allocation group for this allocation.
2573 * If so, fix up the btree freelist's size.
2574 */
2e9101da 2575int /* error */
1da177e4 2576xfs_alloc_fix_freelist(
396503fc
DC
2577 struct xfs_alloc_arg *args, /* allocation argument structure */
2578 int flags) /* XFS_ALLOC_FLAG_... */
1da177e4 2579{
396503fc
DC
2580 struct xfs_mount *mp = args->mp;
2581 struct xfs_perag *pag = args->pag;
2582 struct xfs_trans *tp = args->tp;
2583 struct xfs_buf *agbp = NULL;
2584 struct xfs_buf *agflbp = NULL;
2585 struct xfs_alloc_arg targs; /* local allocation arguments */
2586 xfs_agblock_t bno; /* freelist block */
2587 xfs_extlen_t need; /* total blocks needed in freelist */
c184f855 2588 int error = 0;
396503fc 2589
362f5e74
BF
2590 /* deferred ops (AGFL block frees) require permanent transactions */
2591 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
2592
1da177e4 2593 if (!pag->pagf_init) {
396503fc 2594 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
f48e2df8
DW
2595 if (error) {
2596 /* Couldn't lock the AGF so skip this AG. */
2597 if (error == -EAGAIN)
2598 error = 0;
396503fc 2599 goto out_no_agbp;
1da177e4 2600 }
396503fc 2601 }
1da177e4 2602
0e1edbd9 2603 /*
396503fc
DC
2604 * If this is a metadata preferred pag and we are user data then try
2605 * somewhere else if we are not being asked to try harder at this
2606 * point
1da177e4 2607 */
c34d570d 2608 if (pag->pagf_metadata && (args->datatype & XFS_ALLOC_USERDATA) &&
0e1edbd9
NS
2609 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2610 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
396503fc 2611 goto out_agbp_relse;
1da177e4
LT
2612 }
2613
496817b4 2614 need = xfs_alloc_min_freelist(mp, pag);
54fee133
CH
2615 if (!xfs_alloc_space_available(args, need, flags |
2616 XFS_ALLOC_FLAG_CHECK))
396503fc 2617 goto out_agbp_relse;
0e1edbd9 2618
1da177e4
LT
2619 /*
2620 * Get the a.g. freespace buffer.
2621 * Can fail if we're not blocking on locks, and it's held.
2622 */
396503fc
DC
2623 if (!agbp) {
2624 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
f48e2df8
DW
2625 if (error) {
2626 /* Couldn't lock the AGF so skip this AG. */
2627 if (error == -EAGAIN)
2628 error = 0;
396503fc 2629 goto out_no_agbp;
0e1edbd9 2630 }
1da177e4 2631 }
50adbcb4 2632
a27ba260
BF
2633 /* reset a padding mismatched agfl before final free space check */
2634 if (pag->pagf_agflreset)
2635 xfs_agfl_reset(tp, agbp, pag);
2636
50adbcb4 2637 /* If there isn't enough total space or single-extent, reject it. */
496817b4 2638 need = xfs_alloc_min_freelist(mp, pag);
396503fc
DC
2639 if (!xfs_alloc_space_available(args, need, flags))
2640 goto out_agbp_relse;
72d55285 2641
30151967
CB
2642#ifdef DEBUG
2643 if (args->alloc_minlen_only) {
2644 int stat;
2645
2646 error = xfs_exact_minlen_extent_available(args, agbp, &stat);
2647 if (error || !stat)
2648 goto out_agbp_relse;
2649 }
2650#endif
1da177e4
LT
2651 /*
2652 * Make the freelist shorter if it's too long.
50adbcb4 2653 *
396503fc
DC
2654 * Note that from this point onwards, we will always release the agf and
2655 * agfl buffers on error. This handles the case where we error out and
2656 * the buffers are clean or may not have been joined to the transaction
2657 * and hence need to be released manually. If they have been joined to
2658 * the transaction, then xfs_trans_brelse() will handle them
2659 * appropriately based on the recursion count and dirty state of the
2660 * buffer.
2661 *
50adbcb4
DC
2662 * XXX (dgc): When we have lots of free space, does this buy us
2663 * anything other than extra overhead when we need to put more blocks
2664 * back on the free list? Maybe we should only do this when space is
2665 * getting low or the AGFL is more than half full?
04f13060
DW
2666 *
2667 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2668 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2669 * updating the rmapbt. Both flags are used in xfs_repair while we're
2670 * rebuilding the rmapbt, and neither are used by the kernel. They're
2671 * both required to ensure that rmaps are correctly recorded for the
2672 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2673 * repair/rmap.c in xfsprogs for details.
1da177e4 2674 */
04f13060 2675 memset(&targs, 0, sizeof(targs));
7280feda 2676 /* struct copy below */
04f13060 2677 if (flags & XFS_ALLOC_FLAG_NORMAP)
7280feda 2678 targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
04f13060 2679 else
7280feda 2680 targs.oinfo = XFS_RMAP_OINFO_AG;
04f13060 2681 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
92821e2b
DC
2682 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2683 if (error)
396503fc 2684 goto out_agbp_relse;
4223f659 2685
c03edc9e
BF
2686 /* defer agfl frees */
2687 xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
1da177e4 2688 }
50adbcb4 2689
1da177e4
LT
2690 targs.tp = tp;
2691 targs.mp = mp;
2692 targs.agbp = agbp;
2693 targs.agno = args->agno;
3fd129b6 2694 targs.alignment = targs.minlen = targs.prod = 1;
1da177e4
LT
2695 targs.type = XFS_ALLOCTYPE_THIS_AG;
2696 targs.pag = pag;
50adbcb4
DC
2697 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2698 if (error)
396503fc 2699 goto out_agbp_relse;
50adbcb4
DC
2700
2701 /* Make the freelist longer if it's too short. */
2702 while (pag->pagf_flcount < need) {
1da177e4 2703 targs.agbno = 0;
50adbcb4 2704 targs.maxlen = need - pag->pagf_flcount;
0ab32086 2705 targs.resv = XFS_AG_RESV_AGFL;
50adbcb4
DC
2706
2707 /* Allocate as many blocks as possible at once. */
2708 error = xfs_alloc_ag_vextent(&targs);
396503fc
DC
2709 if (error)
2710 goto out_agflbp_relse;
2711
1da177e4
LT
2712 /*
2713 * Stop if we run out. Won't happen if callers are obeying
2714 * the restrictions correctly. Can happen for free calls
2715 * on a completely full ag.
2716 */
d210a28c 2717 if (targs.agbno == NULLAGBLOCK) {
0e1edbd9
NS
2718 if (flags & XFS_ALLOC_FLAG_FREEING)
2719 break;
396503fc 2720 goto out_agflbp_relse;
d210a28c 2721 }
1da177e4
LT
2722 /*
2723 * Put each allocated block on the list.
2724 */
2725 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
92821e2b
DC
2726 error = xfs_alloc_put_freelist(tp, agbp,
2727 agflbp, bno, 0);
2728 if (error)
396503fc 2729 goto out_agflbp_relse;
1da177e4
LT
2730 }
2731 }
e63a3690 2732 xfs_trans_brelse(tp, agflbp);
1da177e4
LT
2733 args->agbp = agbp;
2734 return 0;
396503fc
DC
2735
2736out_agflbp_relse:
2737 xfs_trans_brelse(tp, agflbp);
2738out_agbp_relse:
2739 if (agbp)
2740 xfs_trans_brelse(tp, agbp);
2741out_no_agbp:
2742 args->agbp = NULL;
2743 return error;
1da177e4
LT
2744}
2745
2746/*
2747 * Get a block from the freelist.
2748 * Returns with the buffer for the block gotten.
2749 */
50920116 2750int
1da177e4 2751xfs_alloc_get_freelist(
50920116
DC
2752 struct xfs_trans *tp,
2753 struct xfs_buf *agbp,
2754 xfs_agblock_t *bnop,
2755 int btreeblk)
1da177e4 2756{
50920116
DC
2757 struct xfs_agf *agf = agbp->b_addr;
2758 struct xfs_buf *agflbp;
2759 xfs_agblock_t bno;
2760 __be32 *agfl_bno;
2761 int error;
2762 int logflags;
2763 struct xfs_mount *mp = tp->t_mountp;
2764 struct xfs_perag *pag;
1da177e4 2765
1da177e4
LT
2766 /*
2767 * Freelist is empty, give up.
2768 */
2769 if (!agf->agf_flcount) {
2770 *bnop = NULLAGBLOCK;
2771 return 0;
2772 }
2773 /*
2774 * Read the array of free blocks.
2775 */
77c95bba
CH
2776 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2777 &agflbp);
2778 if (error)
1da177e4 2779 return error;
77c95bba
CH
2780
2781
1da177e4
LT
2782 /*
2783 * Get the block number and update the data structures.
2784 */
183606d8 2785 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
77c95bba 2786 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
413d57c9 2787 be32_add_cpu(&agf->agf_flfirst, 1);
1da177e4 2788 xfs_trans_brelse(tp, agflbp);
a78ee256 2789 if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
1da177e4 2790 agf->agf_flfirst = 0;
a862e0fd 2791
92a00544 2792 pag = agbp->b_pag;
a27ba260 2793 ASSERT(!pag->pagf_agflreset);
413d57c9 2794 be32_add_cpu(&agf->agf_flcount, -1);
1da177e4 2795 pag->pagf_flcount--;
92821e2b
DC
2796
2797 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2798 if (btreeblk) {
413d57c9 2799 be32_add_cpu(&agf->agf_btreeblks, 1);
92821e2b
DC
2800 pag->pagf_btreeblks++;
2801 logflags |= XFS_AGF_BTREEBLKS;
2802 }
2803
92821e2b 2804 xfs_alloc_log_agf(tp, agbp, logflags);
1da177e4
LT
2805 *bnop = bno;
2806
1da177e4
LT
2807 return 0;
2808}
2809
2810/*
2811 * Log the given fields from the agf structure.
2812 */
2813void
2814xfs_alloc_log_agf(
2815 xfs_trans_t *tp, /* transaction pointer */
e8222613 2816 struct xfs_buf *bp, /* buffer for a.g. freelist header */
1da177e4
LT
2817 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2818{
2819 int first; /* first byte offset */
2820 int last; /* last byte offset */
2821 static const short offsets[] = {
2822 offsetof(xfs_agf_t, agf_magicnum),
2823 offsetof(xfs_agf_t, agf_versionnum),
2824 offsetof(xfs_agf_t, agf_seqno),
2825 offsetof(xfs_agf_t, agf_length),
2826 offsetof(xfs_agf_t, agf_roots[0]),
2827 offsetof(xfs_agf_t, agf_levels[0]),
2828 offsetof(xfs_agf_t, agf_flfirst),
2829 offsetof(xfs_agf_t, agf_fllast),
2830 offsetof(xfs_agf_t, agf_flcount),
2831 offsetof(xfs_agf_t, agf_freeblks),
2832 offsetof(xfs_agf_t, agf_longest),
92821e2b 2833 offsetof(xfs_agf_t, agf_btreeblks),
4e0e6040 2834 offsetof(xfs_agf_t, agf_uuid),
f32866fd 2835 offsetof(xfs_agf_t, agf_rmap_blocks),
bdf28630
DW
2836 offsetof(xfs_agf_t, agf_refcount_blocks),
2837 offsetof(xfs_agf_t, agf_refcount_root),
2838 offsetof(xfs_agf_t, agf_refcount_level),
da1f039d
DW
2839 /* needed so that we don't log the whole rest of the structure: */
2840 offsetof(xfs_agf_t, agf_spare64),
1da177e4
LT
2841 sizeof(xfs_agf_t)
2842 };
2843
9798f615 2844 trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_);
0b1b213f 2845
61fe135c 2846 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
4e0e6040 2847
1da177e4
LT
2848 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2849 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2850}
2851
2852/*
2853 * Interface for inode allocation to force the pag data to be initialized.
2854 */
2855int /* error */
2856xfs_alloc_pagf_init(
2857 xfs_mount_t *mp, /* file system mount structure */
2858 xfs_trans_t *tp, /* transaction pointer */
2859 xfs_agnumber_t agno, /* allocation group number */
2860 int flags) /* XFS_ALLOC_FLAGS_... */
2861{
e8222613 2862 struct xfs_buf *bp;
1da177e4
LT
2863 int error;
2864
f48e2df8
DW
2865 error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp);
2866 if (!error)
1da177e4 2867 xfs_trans_brelse(tp, bp);
f48e2df8 2868 return error;
1da177e4
LT
2869}
2870
2871/*
2872 * Put the block on the freelist for the allocation group.
2873 */
50920116 2874int
1da177e4 2875xfs_alloc_put_freelist(
50920116
DC
2876 struct xfs_trans *tp,
2877 struct xfs_buf *agbp,
2878 struct xfs_buf *agflbp,
2879 xfs_agblock_t bno,
2880 int btreeblk)
1da177e4 2881{
9798f615
CH
2882 struct xfs_mount *mp = tp->t_mountp;
2883 struct xfs_agf *agf = agbp->b_addr;
50920116
DC
2884 struct xfs_perag *pag;
2885 __be32 *blockp;
1da177e4 2886 int error;
92821e2b 2887 int logflags;
77c95bba
CH
2888 __be32 *agfl_bno;
2889 int startoff;
1da177e4 2890
1da177e4 2891 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
16259e7d 2892 be32_to_cpu(agf->agf_seqno), &agflbp)))
1da177e4 2893 return error;
413d57c9 2894 be32_add_cpu(&agf->agf_fllast, 1);
a78ee256 2895 if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
1da177e4 2896 agf->agf_fllast = 0;
a862e0fd 2897
92a00544 2898 pag = agbp->b_pag;
a27ba260 2899 ASSERT(!pag->pagf_agflreset);
413d57c9 2900 be32_add_cpu(&agf->agf_flcount, 1);
1da177e4 2901 pag->pagf_flcount++;
92821e2b
DC
2902
2903 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2904 if (btreeblk) {
413d57c9 2905 be32_add_cpu(&agf->agf_btreeblks, -1);
92821e2b
DC
2906 pag->pagf_btreeblks--;
2907 logflags |= XFS_AGF_BTREEBLKS;
2908 }
2909
92821e2b
DC
2910 xfs_alloc_log_agf(tp, agbp, logflags);
2911
a78ee256 2912 ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
77c95bba 2913
183606d8 2914 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
77c95bba 2915 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
e2101005 2916 *blockp = cpu_to_be32(bno);
77c95bba
CH
2917 startoff = (char *)blockp - (char *)agflbp->b_addr;
2918
92821e2b 2919 xfs_alloc_log_agf(tp, agbp, logflags);
77c95bba 2920
61fe135c 2921 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
77c95bba
CH
2922 xfs_trans_log_buf(tp, agflbp, startoff,
2923 startoff + sizeof(xfs_agblock_t) - 1);
1da177e4
LT
2924 return 0;
2925}
2926
a6a781a5 2927static xfs_failaddr_t
612cfbfe 2928xfs_agf_verify(
b5572597
DW
2929 struct xfs_buf *bp)
2930{
dbd329f1 2931 struct xfs_mount *mp = bp->b_mount;
9798f615 2932 struct xfs_agf *agf = bp->b_addr;
5d5f527d 2933
38c26bfd 2934 if (xfs_has_crc(mp)) {
a45086e2 2935 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
a6a781a5 2936 return __this_address;
9798f615 2937 if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn)))
a6a781a5 2938 return __this_address;
a45086e2 2939 }
5d5f527d 2940
39708c20
BF
2941 if (!xfs_verify_magic(bp, agf->agf_magicnum))
2942 return __this_address;
2943
2944 if (!(XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
4e0e6040 2945 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
a78ee256
DC
2946 be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
2947 be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
2948 be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
a6a781a5 2949 return __this_address;
5d5f527d 2950
d0c7feaf
ZB
2951 if (be32_to_cpu(agf->agf_length) > mp->m_sb.sb_dblocks)
2952 return __this_address;
2953
2954 if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
2955 be32_to_cpu(agf->agf_freeblks) > be32_to_cpu(agf->agf_length))
2956 return __this_address;
2957
d2a047f3
DW
2958 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2959 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
7cb3efb4
DW
2960 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) >
2961 mp->m_alloc_maxlevels ||
2962 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) >
2963 mp->m_alloc_maxlevels)
a6a781a5 2964 return __this_address;
e1b05723 2965
38c26bfd 2966 if (xfs_has_rmapbt(mp) &&
d2a047f3 2967 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
7cb3efb4
DW
2968 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) >
2969 mp->m_rmap_maxlevels))
a6a781a5 2970 return __this_address;
b8704944 2971
ebd9027d 2972 if (xfs_has_rmapbt(mp) &&
d0c7feaf
ZB
2973 be32_to_cpu(agf->agf_rmap_blocks) > be32_to_cpu(agf->agf_length))
2974 return __this_address;
2975
5d5f527d
DC
2976 /*
2977 * during growfs operations, the perag is not fully initialised,
2978 * so we can't use it for any useful checking. growfs ensures we can't
2979 * use it by using uncached buffers that don't have the perag attached
2980 * so we can detect and avoid this problem.
2981 */
4e0e6040 2982 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
a6a781a5 2983 return __this_address;
5d5f527d 2984
ebd9027d 2985 if (xfs_has_lazysbcount(mp) &&
4e0e6040 2986 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
a6a781a5 2987 return __this_address;
4e0e6040 2988
ebd9027d 2989 if (xfs_has_reflink(mp) &&
d0c7feaf
ZB
2990 be32_to_cpu(agf->agf_refcount_blocks) >
2991 be32_to_cpu(agf->agf_length))
2992 return __this_address;
2993
ebd9027d 2994 if (xfs_has_reflink(mp) &&
d2a047f3 2995 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
973975b7 2996 be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels))
a6a781a5 2997 return __this_address;
46eeb521 2998
a6a781a5 2999 return NULL;
5d5f527d 3000
612cfbfe
DC
3001}
3002
1813dd64
DC
3003static void
3004xfs_agf_read_verify(
612cfbfe
DC
3005 struct xfs_buf *bp)
3006{
dbd329f1 3007 struct xfs_mount *mp = bp->b_mount;
bc1a09b8 3008 xfs_failaddr_t fa;
4e0e6040 3009
38c26bfd 3010 if (xfs_has_crc(mp) &&
ce5028cf 3011 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
bc1a09b8
DW
3012 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
3013 else {
b5572597 3014 fa = xfs_agf_verify(bp);
bc1a09b8
DW
3015 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
3016 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
3017 }
612cfbfe 3018}
5d5f527d 3019
b0f539de 3020static void
1813dd64 3021xfs_agf_write_verify(
612cfbfe
DC
3022 struct xfs_buf *bp)
3023{
dbd329f1 3024 struct xfs_mount *mp = bp->b_mount;
fb1755a6 3025 struct xfs_buf_log_item *bip = bp->b_log_item;
9798f615 3026 struct xfs_agf *agf = bp->b_addr;
bc1a09b8 3027 xfs_failaddr_t fa;
4e0e6040 3028
b5572597 3029 fa = xfs_agf_verify(bp);
bc1a09b8
DW
3030 if (fa) {
3031 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
4e0e6040
DC
3032 return;
3033 }
3034
38c26bfd 3035 if (!xfs_has_crc(mp))
4e0e6040
DC
3036 return;
3037
3038 if (bip)
9798f615 3039 agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
4e0e6040 3040
f1dbcd7e 3041 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
5d5f527d
DC
3042}
3043
1813dd64 3044const struct xfs_buf_ops xfs_agf_buf_ops = {
233135b7 3045 .name = "xfs_agf",
39708c20 3046 .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) },
1813dd64
DC
3047 .verify_read = xfs_agf_read_verify,
3048 .verify_write = xfs_agf_write_verify,
b5572597 3049 .verify_struct = xfs_agf_verify,
1813dd64
DC
3050};
3051
1da177e4
LT
3052/*
3053 * Read in the allocation group header (free/alloc section).
3054 */
3055int /* error */
4805621a
CH
3056xfs_read_agf(
3057 struct xfs_mount *mp, /* mount point structure */
3058 struct xfs_trans *tp, /* transaction pointer */
3059 xfs_agnumber_t agno, /* allocation group number */
3060 int flags, /* XFS_BUF_ */
3061 struct xfs_buf **bpp) /* buffer for the ag freelist header */
1da177e4 3062{
1da177e4
LT
3063 int error;
3064
d123031a
DC
3065 trace_xfs_read_agf(mp, agno);
3066
1da177e4 3067 ASSERT(agno != NULLAGNUMBER);
4ed8e27b 3068 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1da177e4 3069 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
1813dd64 3070 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
1da177e4
LT
3071 if (error)
3072 return error;
4805621a 3073
5a52c2a5 3074 ASSERT(!(*bpp)->b_error);
38f23232 3075 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
4805621a
CH
3076 return 0;
3077}
3078
3079/*
3080 * Read in the allocation group header (free/alloc section).
3081 */
3082int /* error */
3083xfs_alloc_read_agf(
3084 struct xfs_mount *mp, /* mount point structure */
3085 struct xfs_trans *tp, /* transaction pointer */
3086 xfs_agnumber_t agno, /* allocation group number */
3087 int flags, /* XFS_ALLOC_FLAG_... */
3088 struct xfs_buf **bpp) /* buffer for the ag freelist header */
3089{
3090 struct xfs_agf *agf; /* ag freelist header */
3091 struct xfs_perag *pag; /* per allocation group data */
3092 int error;
16eaab83 3093 int allocbt_blks;
4805621a 3094
d123031a 3095 trace_xfs_alloc_read_agf(mp, agno);
4805621a 3096
f48e2df8
DW
3097 /* We don't support trylock when freeing. */
3098 ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) !=
3099 (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK));
d123031a 3100 ASSERT(agno != NULLAGNUMBER);
4805621a 3101 error = xfs_read_agf(mp, tp, agno,
0cadda1c 3102 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
4805621a
CH
3103 bpp);
3104 if (error)
3105 return error;
5a52c2a5 3106 ASSERT(!(*bpp)->b_error);
4805621a 3107
9798f615 3108 agf = (*bpp)->b_addr;
92a00544 3109 pag = (*bpp)->b_pag;
1da177e4 3110 if (!pag->pagf_init) {
16259e7d 3111 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
92821e2b 3112 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
16259e7d
CH
3113 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
3114 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
1da177e4 3115 pag->pagf_levels[XFS_BTNUM_BNOi] =
16259e7d 3116 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
1da177e4 3117 pag->pagf_levels[XFS_BTNUM_CNTi] =
16259e7d 3118 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
b8704944
DW
3119 pag->pagf_levels[XFS_BTNUM_RMAPi] =
3120 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
46eeb521 3121 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
1da177e4 3122 pag->pagf_init = 1;
a27ba260 3123 pag->pagf_agflreset = xfs_agfl_needs_reset(mp, agf);
16eaab83
BF
3124
3125 /*
3126 * Update the in-core allocbt counter. Filter out the rmapbt
3127 * subset of the btreeblks counter because the rmapbt is managed
3128 * by perag reservation. Subtract one for the rmapbt root block
3129 * because the rmap counter includes it while the btreeblks
3130 * counter only tracks non-root blocks.
3131 */
3132 allocbt_blks = pag->pagf_btreeblks;
ebd9027d 3133 if (xfs_has_rmapbt(mp))
16eaab83
BF
3134 allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1;
3135 if (allocbt_blks > 0)
3136 atomic64_add(allocbt_blks, &mp->m_allocbt_blks);
1da177e4
LT
3137 }
3138#ifdef DEBUG
75c8c50f 3139 else if (!xfs_is_shutdown(mp)) {
16259e7d 3140 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
89b28393 3141 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
16259e7d
CH
3142 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
3143 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
1da177e4 3144 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
16259e7d 3145 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
1da177e4 3146 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
16259e7d 3147 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
1da177e4
LT
3148 }
3149#endif
1da177e4
LT
3150 return 0;
3151}
3152
3153/*
3154 * Allocate an extent (variable-size).
3155 * Depending on the allocation type, we either look in a single allocation
3156 * group or loop over the allocation groups to find the result.
3157 */
3158int /* error */
e04426b9 3159xfs_alloc_vextent(
64396ff2 3160 struct xfs_alloc_arg *args) /* allocation argument structure */
1da177e4 3161{
64396ff2
BF
3162 xfs_agblock_t agsize; /* allocation group size */
3163 int error;
3164 int flags; /* XFS_ALLOC_FLAG_... locking flags */
3165 struct xfs_mount *mp; /* mount structure pointer */
3166 xfs_agnumber_t sagno; /* starting allocation group number */
3167 xfs_alloctype_t type; /* input allocation type */
3168 int bump_rotor = 0;
3169 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
1da177e4
LT
3170
3171 mp = args->mp;
3172 type = args->otype = args->type;
3173 args->agbno = NULLAGBLOCK;
3174 /*
3175 * Just fix this up, for the case where the last a.g. is shorter
3176 * (or there's only one a.g.) and the caller couldn't easily figure
3177 * that out (xfs_bmap_alloc).
3178 */
3179 agsize = mp->m_sb.sb_agblocks;
3180 if (args->maxlen > agsize)
3181 args->maxlen = agsize;
3182 if (args->alignment == 0)
3183 args->alignment = 1;
3184 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
3185 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
3186 ASSERT(args->minlen <= args->maxlen);
3187 ASSERT(args->minlen <= agsize);
3188 ASSERT(args->mod < args->prod);
3189 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
3190 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
3191 args->minlen > args->maxlen || args->minlen > agsize ||
3192 args->mod >= args->prod) {
3193 args->fsbno = NULLFSBLOCK;
0b1b213f 3194 trace_xfs_alloc_vextent_badargs(args);
1da177e4
LT
3195 return 0;
3196 }
1da177e4
LT
3197
3198 switch (type) {
3199 case XFS_ALLOCTYPE_THIS_AG:
3200 case XFS_ALLOCTYPE_NEAR_BNO:
3201 case XFS_ALLOCTYPE_THIS_BNO:
3202 /*
3203 * These three force us into a single a.g.
3204 */
3205 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
a862e0fd 3206 args->pag = xfs_perag_get(mp, args->agno);
1da177e4 3207 error = xfs_alloc_fix_freelist(args, 0);
1da177e4 3208 if (error) {
0b1b213f 3209 trace_xfs_alloc_vextent_nofix(args);
1da177e4
LT
3210 goto error0;
3211 }
3212 if (!args->agbp) {
0b1b213f 3213 trace_xfs_alloc_vextent_noagbp(args);
1da177e4
LT
3214 break;
3215 }
3216 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
3217 if ((error = xfs_alloc_ag_vextent(args)))
3218 goto error0;
1da177e4
LT
3219 break;
3220 case XFS_ALLOCTYPE_START_BNO:
3221 /*
3222 * Try near allocation first, then anywhere-in-ag after
3223 * the first a.g. fails.
3224 */
292378ed 3225 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
2e973b2c 3226 xfs_is_inode32(mp)) {
1da177e4
LT
3227 args->fsbno = XFS_AGB_TO_FSB(mp,
3228 ((mp->m_agfrotor / rotorstep) %
3229 mp->m_sb.sb_agcount), 0);
3230 bump_rotor = 1;
3231 }
3232 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
3233 args->type = XFS_ALLOCTYPE_NEAR_BNO;
53004ee7 3234 fallthrough;
1da177e4
LT
3235 case XFS_ALLOCTYPE_FIRST_AG:
3236 /*
3237 * Rotate through the allocation groups looking for a winner.
3238 */
8d242e93 3239 if (type == XFS_ALLOCTYPE_FIRST_AG) {
1da177e4
LT
3240 /*
3241 * Start with allocation group given by bno.
3242 */
3243 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
3244 args->type = XFS_ALLOCTYPE_THIS_AG;
3245 sagno = 0;
3246 flags = 0;
3247 } else {
1da177e4
LT
3248 /*
3249 * Start with the given allocation group.
3250 */
3251 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
3252 flags = XFS_ALLOC_FLAG_TRYLOCK;
3253 }
3254 /*
3255 * Loop over allocation groups twice; first time with
3256 * trylock set, second time without.
3257 */
1da177e4 3258 for (;;) {
a862e0fd 3259 args->pag = xfs_perag_get(mp, args->agno);
1da177e4 3260 error = xfs_alloc_fix_freelist(args, flags);
1da177e4 3261 if (error) {
0b1b213f 3262 trace_xfs_alloc_vextent_nofix(args);
1da177e4
LT
3263 goto error0;
3264 }
3265 /*
3266 * If we get a buffer back then the allocation will fly.
3267 */
3268 if (args->agbp) {
3269 if ((error = xfs_alloc_ag_vextent(args)))
3270 goto error0;
3271 break;
3272 }
0b1b213f
CH
3273
3274 trace_xfs_alloc_vextent_loopfailed(args);
3275
1da177e4
LT
3276 /*
3277 * Didn't work, figure out the next iteration.
3278 */
3279 if (args->agno == sagno &&
3280 type == XFS_ALLOCTYPE_START_BNO)
3281 args->type = XFS_ALLOCTYPE_THIS_AG;
d210a28c
YL
3282 /*
3283 * For the first allocation, we can try any AG to get
3284 * space. However, if we already have allocated a
3285 * block, we don't want to try AGs whose number is below
3286 * sagno. Otherwise, we may end up with out-of-order
3287 * locking of AGF, which might cause deadlock.
3288 */
3289 if (++(args->agno) == mp->m_sb.sb_agcount) {
64396ff2 3290 if (args->tp->t_firstblock != NULLFSBLOCK)
d210a28c
YL
3291 args->agno = sagno;
3292 else
3293 args->agno = 0;
3294 }
1da177e4
LT
3295 /*
3296 * Reached the starting a.g., must either be done
3297 * or switch to non-trylock mode.
3298 */
3299 if (args->agno == sagno) {
255c5162 3300 if (flags == 0) {
1da177e4 3301 args->agbno = NULLAGBLOCK;
0b1b213f 3302 trace_xfs_alloc_vextent_allfailed(args);
1da177e4
LT
3303 break;
3304 }
255c5162
CH
3305
3306 flags = 0;
3307 if (type == XFS_ALLOCTYPE_START_BNO) {
3308 args->agbno = XFS_FSB_TO_AGBNO(mp,
3309 args->fsbno);
3310 args->type = XFS_ALLOCTYPE_NEAR_BNO;
1da177e4
LT
3311 }
3312 }
a862e0fd 3313 xfs_perag_put(args->pag);
1da177e4 3314 }
8d242e93 3315 if (bump_rotor) {
1da177e4
LT
3316 if (args->agno == sagno)
3317 mp->m_agfrotor = (mp->m_agfrotor + 1) %
3318 (mp->m_sb.sb_agcount * rotorstep);
3319 else
3320 mp->m_agfrotor = (args->agno * rotorstep + 1) %
3321 (mp->m_sb.sb_agcount * rotorstep);
3322 }
3323 break;
3324 default:
3325 ASSERT(0);
3326 /* NOTREACHED */
3327 }
3328 if (args->agbno == NULLAGBLOCK)
3329 args->fsbno = NULLFSBLOCK;
3330 else {
3331 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
3332#ifdef DEBUG
3333 ASSERT(args->len >= args->minlen);
3334 ASSERT(args->len <= args->maxlen);
3335 ASSERT(args->agbno % args->alignment == 0);
3336 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
3337 args->len);
3338#endif
3fbbbea3 3339
1da177e4 3340 }
a862e0fd 3341 xfs_perag_put(args->pag);
1da177e4
LT
3342 return 0;
3343error0:
a862e0fd 3344 xfs_perag_put(args->pag);
1da177e4
LT
3345 return error;
3346}
3347
4d89e20b
DC
3348/* Ensure that the freelist is at full capacity. */
3349int
3350xfs_free_extent_fix_freelist(
3351 struct xfs_trans *tp,
45d06621 3352 struct xfs_perag *pag,
4d89e20b 3353 struct xfs_buf **agbp)
1da177e4 3354{
4d89e20b
DC
3355 struct xfs_alloc_arg args;
3356 int error;
1da177e4 3357
4d89e20b 3358 memset(&args, 0, sizeof(struct xfs_alloc_arg));
1da177e4
LT
3359 args.tp = tp;
3360 args.mp = tp->t_mountp;
45d06621
DC
3361 args.agno = pag->pag_agno;
3362 args.pag = pag;
be65b18a
DC
3363
3364 /*
3365 * validate that the block number is legal - the enables us to detect
3366 * and handle a silent filesystem corruption rather than crashing.
3367 */
be65b18a 3368 if (args.agno >= args.mp->m_sb.sb_agcount)
2451337d 3369 return -EFSCORRUPTED;
be65b18a 3370
be65b18a
DC
3371 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3372 if (error)
45d06621 3373 return error;
4d89e20b
DC
3374
3375 *agbp = args.agbp;
45d06621 3376 return 0;
4d89e20b
DC
3377}
3378
3379/*
3380 * Free an extent.
3381 * Just break up the extent address and hand off to xfs_free_ag_extent
3382 * after fixing up the freelist.
3383 */
66e3237e 3384int
fcb762f5 3385__xfs_free_extent(
66e3237e
DW
3386 struct xfs_trans *tp,
3387 xfs_fsblock_t bno,
3388 xfs_extlen_t len,
3389 const struct xfs_owner_info *oinfo,
3390 enum xfs_ag_resv_type type,
3391 bool skip_discard)
4d89e20b 3392{
66e3237e
DW
3393 struct xfs_mount *mp = tp->t_mountp;
3394 struct xfs_buf *agbp;
3395 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
3396 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
9798f615 3397 struct xfs_agf *agf;
66e3237e
DW
3398 int error;
3399 unsigned int busy_flags = 0;
45d06621 3400 struct xfs_perag *pag;
4d89e20b
DC
3401
3402 ASSERT(len != 0);
0ab32086 3403 ASSERT(type != XFS_AG_RESV_AGFL);
4d89e20b 3404
ba9e7802 3405 if (XFS_TEST_ERROR(false, mp,
9e24cfd0 3406 XFS_ERRTAG_FREE_EXTENT))
ba9e7802
DW
3407 return -EIO;
3408
45d06621
DC
3409 pag = xfs_perag_get(mp, agno);
3410 error = xfs_free_extent_fix_freelist(tp, pag, &agbp);
4d89e20b 3411 if (error)
45d06621 3412 goto err;
9798f615 3413 agf = agbp->b_addr;
4d89e20b 3414
f9e03706
DW
3415 if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) {
3416 error = -EFSCORRUPTED;
45d06621 3417 goto err_release;
f9e03706 3418 }
be65b18a
DC
3419
3420 /* validate the extent size is legal now we have the agf locked */
9798f615 3421 if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) {
f9e03706 3422 error = -EFSCORRUPTED;
45d06621 3423 goto err_release;
f9e03706 3424 }
be65b18a 3425
3fd129b6 3426 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
4d89e20b 3427 if (error)
45d06621 3428 goto err_release;
4d89e20b 3429
fcb762f5
BF
3430 if (skip_discard)
3431 busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
45d06621
DC
3432 xfs_extent_busy_insert(tp, pag, agbno, len, busy_flags);
3433 xfs_perag_put(pag);
4d89e20b
DC
3434 return 0;
3435
45d06621 3436err_release:
4d89e20b 3437 xfs_trans_brelse(tp, agbp);
45d06621
DC
3438err:
3439 xfs_perag_put(pag);
1da177e4
LT
3440 return error;
3441}
2d520bfa
DW
3442
3443struct xfs_alloc_query_range_info {
3444 xfs_alloc_query_range_fn fn;
3445 void *priv;
3446};
3447
3448/* Format btree record and pass to our callback. */
3449STATIC int
3450xfs_alloc_query_range_helper(
3451 struct xfs_btree_cur *cur,
159eb69d 3452 const union xfs_btree_rec *rec,
2d520bfa
DW
3453 void *priv)
3454{
3455 struct xfs_alloc_query_range_info *query = priv;
3456 struct xfs_alloc_rec_incore irec;
3457
3458 irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
3459 irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
3460 return query->fn(cur, &irec, query->priv);
3461}
3462
3463/* Find all free space within a given range of blocks. */
3464int
3465xfs_alloc_query_range(
3466 struct xfs_btree_cur *cur,
04dcb474
DW
3467 const struct xfs_alloc_rec_incore *low_rec,
3468 const struct xfs_alloc_rec_incore *high_rec,
2d520bfa
DW
3469 xfs_alloc_query_range_fn fn,
3470 void *priv)
3471{
3472 union xfs_btree_irec low_brec;
3473 union xfs_btree_irec high_brec;
3474 struct xfs_alloc_query_range_info query;
3475
3476 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3477 low_brec.a = *low_rec;
3478 high_brec.a = *high_rec;
3479 query.priv = priv;
3480 query.fn = fn;
3481 return xfs_btree_query_range(cur, &low_brec, &high_brec,
3482 xfs_alloc_query_range_helper, &query);
3483}
e9a2599a
DW
3484
3485/* Find all free space records. */
3486int
3487xfs_alloc_query_all(
3488 struct xfs_btree_cur *cur,
3489 xfs_alloc_query_range_fn fn,
3490 void *priv)
3491{
3492 struct xfs_alloc_query_range_info query;
3493
3494 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
3495 query.priv = priv;
3496 query.fn = fn;
3497 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
3498}
21ec5416 3499
ce1d802e
DW
3500/* Is there a record covering a given extent? */
3501int
3502xfs_alloc_has_record(
3503 struct xfs_btree_cur *cur,
3504 xfs_agblock_t bno,
3505 xfs_extlen_t len,
3506 bool *exists)
3507{
3508 union xfs_btree_irec low;
3509 union xfs_btree_irec high;
3510
3511 memset(&low, 0, sizeof(low));
3512 low.a.ar_startblock = bno;
3513 memset(&high, 0xFF, sizeof(high));
3514 high.a.ar_startblock = bno + len - 1;
3515
3516 return xfs_btree_has_record(cur, &low, &high, exists);
3517}
9f3a080e
DW
3518
3519/*
3520 * Walk all the blocks in the AGFL. The @walk_fn can return any negative
5bb46e3e 3521 * error code or XFS_ITER_*.
9f3a080e
DW
3522 */
3523int
3524xfs_agfl_walk(
3525 struct xfs_mount *mp,
3526 struct xfs_agf *agf,
3527 struct xfs_buf *agflbp,
3528 xfs_agfl_walk_fn walk_fn,
3529 void *priv)
3530{
3531 __be32 *agfl_bno;
3532 unsigned int i;
3533 int error;
3534
183606d8 3535 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
9f3a080e
DW
3536 i = be32_to_cpu(agf->agf_flfirst);
3537
3538 /* Nothing to walk in an empty AGFL. */
3539 if (agf->agf_flcount == cpu_to_be32(0))
3540 return 0;
3541
3542 /* Otherwise, walk from first to last, wrapping as needed. */
3543 for (;;) {
3544 error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
3545 if (error)
3546 return error;
3547 if (i == be32_to_cpu(agf->agf_fllast))
3548 break;
3549 if (++i == xfs_agfl_size(mp))
3550 i = 0;
3551 }
3552
3553 return 0;
3554}
c201d9ca
DW
3555
3556int __init
3557xfs_extfree_intent_init_cache(void)
3558{
3559 xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent",
3560 sizeof(struct xfs_extent_free_item),
3561 0, 0, NULL);
3562
3563 return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM;
3564}
3565
3566void
3567xfs_extfree_intent_destroy_cache(void)
3568{
3569 kmem_cache_destroy(xfs_extfree_item_cache);
3570 xfs_extfree_item_cache = NULL;
3571}