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