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