blob: 055ffa8b7a5204b0aba33658e4aa37440b35fd9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
Dave Chinner70a98832013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_btree.h"
Darrick J. Wong673930c2016-08-03 11:33:43 +100030#include "xfs_rmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000033#include "xfs_extent_busy.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070034#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110036#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110039#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110040#include "xfs_log.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100041#include "xfs_ag_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Dave Chinnerc999a222012-03-22 05:15:07 +000043struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
46
47#define XFSA_FIXUP_BNO_OK 1
48#define XFSA_FIXUP_CNT_OK 2
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
52STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
53STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000054 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070056unsigned int
57xfs_refc_block(
58 struct xfs_mount *mp)
59{
60 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
61 return XFS_RMAP_BLOCK(mp) + 1;
62 if (xfs_sb_version_hasfinobt(&mp->m_sb))
63 return XFS_FIBT_BLOCK(mp) + 1;
64 return XFS_IBT_BLOCK(mp) + 1;
65}
66
Darrick J. Wong80180262016-08-03 11:31:47 +100067xfs_extlen_t
68xfs_prealloc_blocks(
69 struct xfs_mount *mp)
70{
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070071 if (xfs_sb_version_hasreflink(&mp->m_sb))
72 return xfs_refc_block(mp) + 1;
Darrick J. Wong80180262016-08-03 11:31:47 +100073 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
74 return XFS_RMAP_BLOCK(mp) + 1;
75 if (xfs_sb_version_hasfinobt(&mp->m_sb))
76 return XFS_FIBT_BLOCK(mp) + 1;
77 return XFS_IBT_BLOCK(mp) + 1;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Darrick J. Wong52548852016-08-03 11:38:24 +100081 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
82 * AGF buffer (PV 947395), we place constraints on the relationship among
83 * actual allocations for data blocks, freelist blocks, and potential file data
84 * bmap btree blocks. However, these restrictions may result in no actual space
85 * allocated for a delayed extent, for example, a data block in a certain AG is
86 * allocated but there is no additional block for the additional bmap btree
87 * block due to a split of the bmap btree of the file. The result of this may
88 * lead to an infinite loop when the file gets flushed to disk and all delayed
89 * extents need to be actually allocated. To get around this, we explicitly set
90 * aside a few blocks which will not be reserved in delayed allocation.
91 *
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100092 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
93 * potential split of the file's bmap btree.
Darrick J. Wong52548852016-08-03 11:38:24 +100094 */
95unsigned int
96xfs_alloc_set_aside(
97 struct xfs_mount *mp)
98{
Christoph Hellwig5149fd32017-01-09 13:36:30 -080099 return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
Darrick J. Wong52548852016-08-03 11:38:24 +1000100}
101
102/*
103 * When deciding how much space to allocate out of an AG, we limit the
104 * allocation maximum size to the size the AG. However, we cannot use all the
105 * blocks in the AG - some are permanently used by metadata. These
106 * blocks are generally:
107 * - the AG superblock, AGF, AGI and AGFL
108 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
109 * the AGI free inode and rmap btree root blocks.
110 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
111 * - the rmapbt root block
112 *
113 * The AG headers are sector sized, so the amount of space they take up is
114 * dependent on filesystem geometry. The others are all single blocks.
115 */
116unsigned int
117xfs_alloc_ag_max_usable(
118 struct xfs_mount *mp)
119{
120 unsigned int blocks;
121
122 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
123 blocks += XFS_ALLOC_AGFL_RESERVE;
124 blocks += 3; /* AGF, AGI btree root blocks */
125 if (xfs_sb_version_hasfinobt(&mp->m_sb))
126 blocks++; /* finobt root block */
127 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
128 blocks++; /* rmap root block */
Darrick J. Wongd0e853f2016-10-03 09:11:24 -0700129 if (xfs_sb_version_hasreflink(&mp->m_sb))
130 blocks++; /* refcount root block */
Darrick J. Wong52548852016-08-03 11:38:24 +1000131
132 return mp->m_sb.sb_agblocks - blocks;
133}
134
135/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100136 * Lookup the record equal to [bno, len] in the btree given by cur.
137 */
138STATIC int /* error */
139xfs_alloc_lookup_eq(
140 struct xfs_btree_cur *cur, /* btree cursor */
141 xfs_agblock_t bno, /* starting block of extent */
142 xfs_extlen_t len, /* length of extent */
143 int *stat) /* success/failure */
144{
145 cur->bc_rec.a.ar_startblock = bno;
146 cur->bc_rec.a.ar_blockcount = len;
147 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
148}
149
150/*
151 * Lookup the first record greater than or equal to [bno, len]
152 * in the btree given by cur.
153 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000154int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100155xfs_alloc_lookup_ge(
156 struct xfs_btree_cur *cur, /* btree cursor */
157 xfs_agblock_t bno, /* starting block of extent */
158 xfs_extlen_t len, /* length of extent */
159 int *stat) /* success/failure */
160{
161 cur->bc_rec.a.ar_startblock = bno;
162 cur->bc_rec.a.ar_blockcount = len;
163 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
164}
165
166/*
167 * Lookup the first record less than or equal to [bno, len]
168 * in the btree given by cur.
169 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000170static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100171xfs_alloc_lookup_le(
172 struct xfs_btree_cur *cur, /* btree cursor */
173 xfs_agblock_t bno, /* starting block of extent */
174 xfs_extlen_t len, /* length of extent */
175 int *stat) /* success/failure */
176{
177 cur->bc_rec.a.ar_startblock = bno;
178 cur->bc_rec.a.ar_blockcount = len;
179 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
180}
181
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100182/*
183 * Update the record referred to by cur to the value given
184 * by [bno, len].
185 * This either works (return 0) or gets an EFSCORRUPTED error.
186 */
187STATIC int /* error */
188xfs_alloc_update(
189 struct xfs_btree_cur *cur, /* btree cursor */
190 xfs_agblock_t bno, /* starting block of extent */
191 xfs_extlen_t len) /* length of extent */
192{
193 union xfs_btree_rec rec;
194
195 rec.alloc.ar_startblock = cpu_to_be32(bno);
196 rec.alloc.ar_blockcount = cpu_to_be32(len);
197 return xfs_btree_update(cur, &rec);
198}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100199
200/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100201 * Get the data from the pointed-to record.
202 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000203int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100204xfs_alloc_get_rec(
205 struct xfs_btree_cur *cur, /* btree cursor */
206 xfs_agblock_t *bno, /* output: starting block of extent */
207 xfs_extlen_t *len, /* output: length of extent */
208 int *stat) /* output: success/failure */
209{
210 union xfs_btree_rec *rec;
211 int error;
212
213 error = xfs_btree_get_rec(cur, &rec, stat);
214 if (!error && *stat == 1) {
215 *bno = be32_to_cpu(rec->alloc.ar_startblock);
216 *len = be32_to_cpu(rec->alloc.ar_blockcount);
217 }
218 return error;
219}
220
221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * Compute aligned version of the found extent.
223 * Takes alignment and min length into account.
224 */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800225STATIC bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000227 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xfs_agblock_t foundbno, /* starting block in found extent */
229 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_agblock_t *resbno, /* result block number */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800231 xfs_extlen_t *reslen, /* result length */
232 unsigned *busy_gen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Christoph Hellwigebf55872017-02-07 14:06:57 -0800234 xfs_agblock_t bno = foundbno;
235 xfs_extlen_t len = foundlen;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000236 xfs_extlen_t diff;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800237 bool busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Christoph Hellwige26f0502011-04-24 19:06:15 +0000239 /* Trim busy sections out of found extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800240 busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000241
Brian Fosterbfe46d42015-05-29 08:53:00 +1000242 /*
243 * If we have a largish extent that happens to start before min_agbno,
244 * see if we can shift it into range...
245 */
246 if (bno < args->min_agbno && bno + len > args->min_agbno) {
247 diff = args->min_agbno - bno;
248 if (len > diff) {
249 bno += diff;
250 len -= diff;
251 }
252 }
253
Christoph Hellwige26f0502011-04-24 19:06:15 +0000254 if (args->alignment > 1 && len >= args->minlen) {
255 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000256
257 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000258
259 *resbno = aligned_bno;
260 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000262 *resbno = bno;
263 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Christoph Hellwigebf55872017-02-07 14:06:57 -0800265
266 return busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*
270 * Compute best start block and diff for "near" allocations.
271 * freelen >= wantlen already checked by caller.
272 */
273STATIC xfs_extlen_t /* difference value (absolute) */
274xfs_alloc_compute_diff(
275 xfs_agblock_t wantbno, /* target starting block */
276 xfs_extlen_t wantlen, /* target length */
277 xfs_extlen_t alignment, /* target alignment */
Dave Chinner292378e2016-09-26 08:21:28 +1000278 int datatype, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 xfs_agblock_t freebno, /* freespace's starting block */
280 xfs_extlen_t freelen, /* freespace's length */
281 xfs_agblock_t *newbnop) /* result: best start block from free */
282{
283 xfs_agblock_t freeend; /* end of freespace extent */
284 xfs_agblock_t newbno1; /* return block number */
285 xfs_agblock_t newbno2; /* other new block number */
286 xfs_extlen_t newlen1=0; /* length with newbno1 */
287 xfs_extlen_t newlen2=0; /* length with newbno2 */
288 xfs_agblock_t wantend; /* end of target extent */
Dave Chinner292378e2016-09-26 08:21:28 +1000289 bool userdata = xfs_alloc_is_userdata(datatype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 ASSERT(freelen >= wantlen);
292 freeend = freebno + freelen;
293 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200294 /*
295 * We want to allocate from the start of a free extent if it is past
296 * the desired block or if we are allocating user data and the free
297 * extent is before desired block. The second case is there to allow
298 * for contiguous allocation from the remaining free space if the file
299 * grows in the short term.
300 */
301 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
303 newbno1 = NULLAGBLOCK;
304 } else if (freeend >= wantend && alignment > 1) {
305 newbno1 = roundup(wantbno, alignment);
306 newbno2 = newbno1 - alignment;
307 if (newbno1 >= freeend)
308 newbno1 = NULLAGBLOCK;
309 else
310 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
311 if (newbno2 < freebno)
312 newbno2 = NULLAGBLOCK;
313 else
314 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
315 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
316 if (newlen1 < newlen2 ||
317 (newlen1 == newlen2 &&
318 XFS_ABSDIFF(newbno1, wantbno) >
319 XFS_ABSDIFF(newbno2, wantbno)))
320 newbno1 = newbno2;
321 } else if (newbno2 != NULLAGBLOCK)
322 newbno1 = newbno2;
323 } else if (freeend >= wantend) {
324 newbno1 = wantbno;
325 } else if (alignment > 1) {
326 newbno1 = roundup(freeend - wantlen, alignment);
327 if (newbno1 > freeend - wantlen &&
328 newbno1 - alignment >= freebno)
329 newbno1 -= alignment;
330 else if (newbno1 >= freeend)
331 newbno1 = NULLAGBLOCK;
332 } else
333 newbno1 = freeend - wantlen;
334 *newbnop = newbno1;
335 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
336}
337
338/*
339 * Fix up the length, based on mod and prod.
340 * len should be k * prod + mod for some k.
341 * If len is too small it is returned unchanged.
342 * If len hits maxlen it is left alone.
343 */
344STATIC void
345xfs_alloc_fix_len(
346 xfs_alloc_arg_t *args) /* allocation argument structure */
347{
348 xfs_extlen_t k;
349 xfs_extlen_t rlen;
350
351 ASSERT(args->mod < args->prod);
352 rlen = args->len;
353 ASSERT(rlen >= args->minlen);
354 ASSERT(rlen <= args->maxlen);
355 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
356 (args->mod == 0 && rlen < args->prod))
357 return;
358 k = rlen % args->prod;
359 if (k == args->mod)
360 return;
Jan Kara30265112014-06-06 16:06:37 +1000361 if (k > args->mod)
362 rlen = rlen - (k - args->mod);
363 else
364 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100365 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000366 if ((int)rlen < (int)args->minlen)
367 return;
368 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
369 ASSERT(rlen % args->prod == args->mod);
Christoph Hellwig54fee132017-01-09 13:44:30 -0800370 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
371 rlen + args->minleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 args->len = rlen;
373}
374
375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Update the two btrees, logically removing from freespace the extent
377 * starting at rbno, rlen blocks. The extent is contained within the
378 * actual (current) free extent fbno for flen blocks.
379 * Flags are passed in indicating whether the cursors are set to the
380 * relevant records.
381 */
382STATIC int /* error code */
383xfs_alloc_fixup_trees(
384 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
385 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
386 xfs_agblock_t fbno, /* starting block of free extent */
387 xfs_extlen_t flen, /* length of free extent */
388 xfs_agblock_t rbno, /* starting block of returned extent */
389 xfs_extlen_t rlen, /* length of returned extent */
390 int flags) /* flags, XFSA_FIXUP_... */
391{
392 int error; /* error code */
393 int i; /* operation results */
394 xfs_agblock_t nfbno1; /* first new free startblock */
395 xfs_agblock_t nfbno2; /* second new free startblock */
396 xfs_extlen_t nflen1=0; /* first new free length */
397 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100398 struct xfs_mount *mp;
399
400 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
403 * Look up the record in the by-size tree if necessary.
404 */
405 if (flags & XFSA_FIXUP_CNT_OK) {
406#ifdef DEBUG
407 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
408 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100409 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 i == 1 && nfbno1 == fbno && nflen1 == flen);
411#endif
412 } else {
413 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
414 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100415 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 /*
418 * Look up the record in the by-block tree if necessary.
419 */
420 if (flags & XFSA_FIXUP_BNO_OK) {
421#ifdef DEBUG
422 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
423 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100424 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 i == 1 && nfbno1 == fbno && nflen1 == flen);
426#endif
427 } else {
428 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
429 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100430 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100433#ifdef DEBUG
434 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
435 struct xfs_btree_block *bnoblock;
436 struct xfs_btree_block *cntblock;
437
438 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
439 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
440
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100441 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100442 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Deal with all four cases: the allocated record is contained
448 * within the freespace record, so we can have new freespace
449 * at either (or both) end, or no freespace remaining.
450 */
451 if (rbno == fbno && rlen == flen)
452 nfbno1 = nfbno2 = NULLAGBLOCK;
453 else if (rbno == fbno) {
454 nfbno1 = rbno + rlen;
455 nflen1 = flen - rlen;
456 nfbno2 = NULLAGBLOCK;
457 } else if (rbno + rlen == fbno + flen) {
458 nfbno1 = fbno;
459 nflen1 = flen - rlen;
460 nfbno2 = NULLAGBLOCK;
461 } else {
462 nfbno1 = fbno;
463 nflen1 = rbno - fbno;
464 nfbno2 = rbno + rlen;
465 nflen2 = (fbno + flen) - nfbno2;
466 }
467 /*
468 * Delete the entry from the by-size btree.
469 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100470 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100472 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
474 * Add new by-size btree entry(s).
475 */
476 if (nfbno1 != NULLAGBLOCK) {
477 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
478 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100479 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100480 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100482 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 if (nfbno2 != NULLAGBLOCK) {
485 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
486 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100487 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100488 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100490 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 /*
493 * Fix up the by-block btree entry(s).
494 */
495 if (nfbno1 == NULLAGBLOCK) {
496 /*
497 * No remaining freespace, just delete the by-block tree entry.
498 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100499 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100501 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else {
503 /*
504 * Update the by-block entry to start later|be shorter.
505 */
506 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
507 return error;
508 }
509 if (nfbno2 != NULLAGBLOCK) {
510 /*
511 * 2 resulting free entries, need to add one.
512 */
513 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
514 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100515 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100516 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100518 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 return 0;
521}
522
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100523static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100524xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100525 struct xfs_buf *bp)
526{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100527 struct xfs_mount *mp = bp->b_target->bt_mount;
528 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100529 int i;
530
Eric Sandeence748ea2015-07-29 11:53:31 +1000531 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100532 return false;
533 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
534 return false;
535 /*
536 * during growfs operations, the perag is not fully initialised,
537 * so we can't use it for any useful checking. growfs ensures we can't
538 * use it by using uncached buffers that don't have the perag attached
539 * so we can detect and avoid this problem.
540 */
541 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
542 return false;
543
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100544 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100545 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100546 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100547 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100548 }
Brian Fostera45086e2015-10-12 15:59:25 +1100549
550 return xfs_log_check_lsn(mp,
551 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100552}
553
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100554static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100555xfs_agfl_read_verify(
556 struct xfs_buf *bp)
557{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100558 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100559
560 /*
561 * There is no verification of non-crc AGFLs because mkfs does not
562 * initialise the AGFL to zero or NULL. Hence the only valid part of the
563 * AGFL is what the AGF says is active. We can't get to the AGF, so we
564 * can't verify just those entries are valid.
565 */
566 if (!xfs_sb_version_hascrc(&mp->m_sb))
567 return;
568
Eric Sandeence5028c2014-02-27 15:23:10 +1100569 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800570 xfs_verifier_error(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100571 else if (!xfs_agfl_verify(bp))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800572 xfs_verifier_error(bp, -EFSCORRUPTED);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100573}
574
575static void
576xfs_agfl_write_verify(
577 struct xfs_buf *bp)
578{
579 struct xfs_mount *mp = bp->b_target->bt_mount;
580 struct xfs_buf_log_item *bip = bp->b_fspriv;
581
582 /* no verification of non-crc AGFLs */
583 if (!xfs_sb_version_hascrc(&mp->m_sb))
584 return;
585
586 if (!xfs_agfl_verify(bp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800587 xfs_verifier_error(bp, -EFSCORRUPTED);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100588 return;
589 }
590
591 if (bip)
592 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
593
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100594 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100595}
596
Dave Chinner1813dd62012-11-14 17:54:40 +1100597const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100598 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100599 .verify_read = xfs_agfl_read_verify,
600 .verify_write = xfs_agfl_write_verify,
601};
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
604 * Read in the allocation group free block array.
605 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700606int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607xfs_alloc_read_agfl(
608 xfs_mount_t *mp, /* mount point structure */
609 xfs_trans_t *tp, /* transaction pointer */
610 xfs_agnumber_t agno, /* allocation group number */
611 xfs_buf_t **bpp) /* buffer for the ag free block array */
612{
613 xfs_buf_t *bp; /* return value */
614 int error;
615
616 ASSERT(agno != NULLAGNUMBER);
617 error = xfs_trans_read_buf(
618 mp, tp, mp->m_ddev_targp,
619 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100620 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (error)
622 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000623 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *bpp = bp;
625 return 0;
626}
627
Christoph Hellwigecb69282011-03-04 12:59:55 +0000628STATIC int
629xfs_alloc_update_counters(
630 struct xfs_trans *tp,
631 struct xfs_perag *pag,
632 struct xfs_buf *agbp,
633 long len)
634{
635 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
636
637 pag->pagf_freeblks += len;
638 be32_add_cpu(&agf->agf_freeblks, len);
639
640 xfs_trans_agblocks_delta(tp, len);
641 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
642 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000643 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000644
645 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
646 return 0;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * Allocation group level functions.
651 */
652
653/*
654 * Allocate a variable extent in the allocation group agno.
655 * Type and bno are used to determine where in the allocation group the
656 * extent will start.
657 * Extent's length (returned in *len) will be between minlen and maxlen,
658 * and of the form k * prod + mod unless there's nothing that large.
659 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
660 */
661STATIC int /* error */
662xfs_alloc_ag_vextent(
663 xfs_alloc_arg_t *args) /* argument structure for allocation */
664{
665 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 ASSERT(args->minlen > 0);
668 ASSERT(args->maxlen > 0);
669 ASSERT(args->minlen <= args->maxlen);
670 ASSERT(args->mod < args->prod);
671 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000672
673 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * Branch to correct routine based on the type.
675 */
676 args->wasfromfl = 0;
677 switch (args->type) {
678 case XFS_ALLOCTYPE_THIS_AG:
679 error = xfs_alloc_ag_vextent_size(args);
680 break;
681 case XFS_ALLOCTYPE_NEAR_BNO:
682 error = xfs_alloc_ag_vextent_near(args);
683 break;
684 case XFS_ALLOCTYPE_THIS_BNO:
685 error = xfs_alloc_ag_vextent_exact(args);
686 break;
687 default:
688 ASSERT(0);
689 /* NOTREACHED */
690 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000691
692 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Christoph Hellwigecb69282011-03-04 12:59:55 +0000695 ASSERT(args->len >= args->minlen);
696 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000697 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000698 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Darrick J. Wong673930c2016-08-03 11:33:43 +1000700 /* if not file data, insert new block into the reverse map btree */
Darrick J. Wong33df3a92017-12-07 19:07:27 -0800701 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
Darrick J. Wong673930c2016-08-03 11:33:43 +1000702 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
703 args->agbno, args->len, &args->oinfo);
704 if (error)
705 return error;
706 }
707
Christoph Hellwigecb69282011-03-04 12:59:55 +0000708 if (!args->wasfromfl) {
709 error = xfs_alloc_update_counters(args->tp, args->pag,
710 args->agbp,
711 -((long)(args->len)));
712 if (error)
713 return error;
714
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000715 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000716 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000718
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000719 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000720
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100721 XFS_STATS_INC(args->mp, xs_allocx);
722 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000723 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
727 * Allocate a variable extent at exactly agno/bno.
728 * Extent's length (returned in *len) will be between minlen and maxlen,
729 * and of the form k * prod + mod unless there's nothing that large.
730 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
731 */
732STATIC int /* error */
733xfs_alloc_ag_vextent_exact(
734 xfs_alloc_arg_t *args) /* allocation argument structure */
735{
736 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
737 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int error;
739 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800741 xfs_agblock_t tbno; /* start block of busy extent */
742 xfs_extlen_t tlen; /* length of busy extent */
743 xfs_agblock_t tend; /* end block of busy extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int i; /* success/failure of operation */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800745 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Allocate/initialize a cursor for the by-number freespace btree.
751 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100752 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000753 args->agno, XFS_BTNUM_BNO);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
757 * Look for the closest free block <= bno, it must contain bno
758 * if any free block does.
759 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000760 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
761 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000763 if (!i)
764 goto not_found;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Grab the freespace record.
768 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000769 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100772 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000776 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800778 tbno = fbno;
779 tlen = flen;
780 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000781
782 /*
783 * Give up if the start of the extent is busy, or the freespace isn't
784 * long enough for the minimum request.
785 */
786 if (tbno > args->agbno)
787 goto not_found;
788 if (tlen < args->minlen)
789 goto not_found;
790 tend = tbno + tlen;
791 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000792 goto not_found;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
795 * End of extent will be smaller of the freespace end and the
796 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000797 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * Fix the length according to mod and prod if given.
799 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000800 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
801 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 xfs_alloc_fix_len(args);
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000803 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000806 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * Allocate/initialize a cursor for the by-size btree.
808 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100809 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
810 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100812 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000813 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
814 args->len, XFSA_FIXUP_BNO_OK);
815 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
817 goto error0;
818 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
821 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000824 trace_xfs_alloc_exact_done(args);
825 return 0;
826
827not_found:
828 /* Didn't find it, return null. */
829 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
830 args->agbno = NULLAGBLOCK;
831 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833
834error0:
835 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000836 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return error;
838}
839
840/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000841 * Search the btree in a given direction via the search cursor and compare
842 * the records found against the good extent we've already found.
843 */
844STATIC int
845xfs_alloc_find_best_extent(
846 struct xfs_alloc_arg *args, /* allocation argument structure */
847 struct xfs_btree_cur **gcur, /* good cursor */
848 struct xfs_btree_cur **scur, /* searching cursor */
849 xfs_agblock_t gdiff, /* difference for search comparison */
850 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000851 xfs_extlen_t *slen, /* extent length */
852 xfs_agblock_t *sbnoa, /* aligned extent found by search */
853 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000854 int dir) /* 0 = search right, 1 = search left */
855{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000856 xfs_agblock_t new;
857 xfs_agblock_t sdiff;
858 int error;
859 int i;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800860 unsigned busy_gen;
Christoph Hellwig489a1502010-12-10 15:04:11 +0000861
862 /* The good extent is perfect, no need to search. */
863 if (!gdiff)
864 goto out_use_good;
865
866 /*
867 * Look until we find a better one, run out of space or run off the end.
868 */
869 do {
870 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
871 if (error)
872 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100873 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -0800874 xfs_alloc_compute_aligned(args, *sbno, *slen,
875 sbnoa, slena, &busy_gen);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000876
877 /*
878 * The good extent is closer than this one.
879 */
880 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000881 if (*sbnoa > args->max_agbno)
882 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000883 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000884 goto out_use_good;
885 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000886 if (*sbnoa < args->min_agbno)
887 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000888 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000889 goto out_use_good;
890 }
891
892 /*
893 * Same distance, compare length and pick the best.
894 */
895 if (*slena >= args->minlen) {
896 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
897 xfs_alloc_fix_len(args);
898
899 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200900 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000901 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000902 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000903
904 /*
905 * Choose closer size and invalidate other cursor.
906 */
907 if (sdiff < gdiff)
908 goto out_use_search;
909 goto out_use_good;
910 }
911
912 if (!dir)
913 error = xfs_btree_increment(*scur, 0, &i);
914 else
915 error = xfs_btree_decrement(*scur, 0, &i);
916 if (error)
917 goto error0;
918 } while (i);
919
920out_use_good:
921 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
922 *scur = NULL;
923 return 0;
924
925out_use_search:
926 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
927 *gcur = NULL;
928 return 0;
929
930error0:
931 /* caller invalidates cursors */
932 return error;
933}
934
935/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Allocate a variable extent near bno in the allocation group agno.
937 * Extent's length (returned in len) will be between minlen and maxlen,
938 * and of the form k * prod + mod unless there's nothing that large.
939 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
940 */
941STATIC int /* error */
942xfs_alloc_ag_vextent_near(
943 xfs_alloc_arg_t *args) /* allocation argument structure */
944{
945 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
946 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
947 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 xfs_agblock_t gtbno; /* start bno of right side entry */
949 xfs_agblock_t gtbnoa; /* aligned ... */
950 xfs_extlen_t gtdiff; /* difference to right side entry */
951 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000952 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xfs_agblock_t gtnew; /* useful start bno of right side */
954 int error; /* error code */
955 int i; /* result code, temporary */
956 int j; /* result code, temporary */
957 xfs_agblock_t ltbno; /* start bno of left side entry */
958 xfs_agblock_t ltbnoa; /* aligned ... */
959 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000961 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 xfs_agblock_t ltnew; /* useful start bno of left side */
963 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800964 bool busy;
965 unsigned busy_gen;
Dave Chinner63d20d62013-08-12 20:49:50 +1000966#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * Randomly don't execute the first algorithm.
969 */
970 int dofirst; /* set to do first algorithm */
971
Akinobu Mitaecb34032013-03-04 21:58:20 +0900972 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000974
Brian Fosterbfe46d42015-05-29 08:53:00 +1000975 /* handle unitialized agbno range so caller doesn't have to */
976 if (!args->min_agbno && !args->max_agbno)
977 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
978 ASSERT(args->min_agbno <= args->max_agbno);
979
980 /* clamp agbno to the range if it's outside */
981 if (args->agbno < args->min_agbno)
982 args->agbno = args->min_agbno;
983 if (args->agbno > args->max_agbno)
984 args->agbno = args->max_agbno;
985
Christoph Hellwige26f0502011-04-24 19:06:15 +0000986restart:
987 bno_cur_lt = NULL;
988 bno_cur_gt = NULL;
989 ltlen = 0;
990 gtlena = 0;
991 ltlena = 0;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800992 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Get a cursor for the by-size btree.
996 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100997 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
998 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /*
1001 * See if there are any free extents as big as maxlen.
1002 */
1003 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1004 goto error0;
1005 /*
1006 * If none, then pick up the last entry in the tree unless the
1007 * tree is empty.
1008 */
1009 if (!i) {
1010 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1011 &ltlen, &i)))
1012 goto error0;
1013 if (i == 0 || ltlen == 0) {
1014 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001015 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017 }
1018 ASSERT(i == 1);
1019 }
1020 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * First algorithm.
1024 * If the requested extent is large wrt the freespaces available
1025 * in this a.g., then the cursor will be pointing to a btree entry
1026 * near the right edge of the tree. If it's in the last btree leaf
1027 * block, then we just examine all the entries in that block
1028 * that are big enough, and pick the best one.
1029 * This is written as a while loop so we can break out of it,
1030 * but we never loop back to the top.
1031 */
1032 while (xfs_btree_islastblock(cnt_cur, 0)) {
1033 xfs_extlen_t bdiff;
1034 int besti=0;
1035 xfs_extlen_t blen=0;
1036 xfs_agblock_t bnew=0;
1037
Dave Chinner63d20d62013-08-12 20:49:50 +10001038#ifdef DEBUG
1039 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041#endif
1042 /*
1043 * Start from the entry that lookup found, sequence through
1044 * all larger free blocks. If we're actually pointing at a
1045 * record smaller than maxlen, go to the start of this block,
1046 * and skip all those smaller than minlen.
1047 */
1048 if (ltlen || args->alignment > 1) {
1049 cnt_cur->bc_ptrs[0] = 1;
1050 do {
1051 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1052 &ltlen, &i)))
1053 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001054 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (ltlen >= args->minlen)
1056 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001057 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto error0;
1059 } while (i);
1060 ASSERT(ltlen >= args->minlen);
1061 if (!i)
1062 break;
1063 }
1064 i = cnt_cur->bc_ptrs[0];
1065 for (j = 1, blen = 0, bdiff = 0;
1066 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001067 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * For each entry, decide if it's better than
1070 * the previous best entry.
1071 */
1072 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1073 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001074 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001075 busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
1076 &ltbnoa, &ltlena, &busy_gen);
David Chinnere6430032008-04-17 16:49:49 +10001077 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001079 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1080 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1082 xfs_alloc_fix_len(args);
1083 ASSERT(args->len >= args->minlen);
1084 if (args->len < blen)
1085 continue;
1086 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001087 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001088 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (ltnew != NULLAGBLOCK &&
1090 (args->len > blen || ltdiff < bdiff)) {
1091 bdiff = ltdiff;
1092 bnew = ltnew;
1093 blen = args->len;
1094 besti = cnt_cur->bc_ptrs[0];
1095 }
1096 }
1097 /*
1098 * It didn't work. We COULD be in a case where
1099 * there's a good record somewhere, so try again.
1100 */
1101 if (blen == 0)
1102 break;
1103 /*
1104 * Point at the best entry, and retrieve it again.
1105 */
1106 cnt_cur->bc_ptrs[0] = besti;
1107 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1108 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001109 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001110 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 args->len = blen;
Christoph Hellwig54fee132017-01-09 13:44:30 -08001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * We are allocating starting at bnew for blen blocks.
1115 */
1116 args->agbno = bnew;
1117 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001118 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Set up a cursor for the by-bno tree.
1121 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001122 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1123 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * Fix up the btree entries.
1126 */
1127 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1128 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1129 goto error0;
1130 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1131 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001132
1133 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135 }
1136 /*
1137 * Second algorithm.
1138 * Search in the by-bno tree to the left and to the right
1139 * simultaneously, until in each case we find a space big enough,
1140 * or run into the edge of the tree. When we run into the edge,
1141 * we deallocate that cursor.
1142 * If both searches succeed, we compare the two spaces and pick
1143 * the better one.
1144 * With alignment, it's possible for both to fail; the upper
1145 * level algorithm that picks allocation groups for allocations
1146 * is not supposed to do this.
1147 */
1148 /*
1149 * Allocate and initialize the cursor for the leftward search.
1150 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001151 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1152 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Lookup <= bno to find the leftward search's starting point.
1155 */
1156 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1157 goto error0;
1158 if (!i) {
1159 /*
1160 * Didn't find anything; use this cursor for the rightward
1161 * search.
1162 */
1163 bno_cur_gt = bno_cur_lt;
1164 bno_cur_lt = NULL;
1165 }
1166 /*
1167 * Found something. Duplicate the cursor for the rightward search.
1168 */
1169 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1170 goto error0;
1171 /*
1172 * Increment the cursor, so we will point at the entry just right
1173 * of the leftward entry if any, or to the leftmost entry.
1174 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001175 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto error0;
1177 if (!i) {
1178 /*
1179 * It failed, there are no rightward entries.
1180 */
1181 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1182 bno_cur_gt = NULL;
1183 }
1184 /*
1185 * Loop going left with the leftward cursor, right with the
1186 * rightward cursor, until either both directions give up or
1187 * we find an entry at least as big as minlen.
1188 */
1189 do {
1190 if (bno_cur_lt) {
1191 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1192 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001193 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001194 busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
1195 &ltbnoa, &ltlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001196 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001198 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001200 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 xfs_btree_del_cursor(bno_cur_lt,
1202 XFS_BTREE_NOERROR);
1203 bno_cur_lt = NULL;
1204 }
1205 }
1206 if (bno_cur_gt) {
1207 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1208 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001209 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001210 busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
1211 &gtbnoa, &gtlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001212 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001214 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001216 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 xfs_btree_del_cursor(bno_cur_gt,
1218 XFS_BTREE_NOERROR);
1219 bno_cur_gt = NULL;
1220 }
1221 }
1222 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * Got both cursors still active, need to find better entry.
1226 */
1227 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ltlena >= args->minlen) {
1229 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001230 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1233 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001234 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001235 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001236 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001237
1238 error = xfs_alloc_find_best_extent(args,
1239 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001240 ltdiff, &gtbno, &gtlen,
1241 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001242 0 /* search right */);
1243 } else {
1244 ASSERT(gtlena >= args->minlen);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001247 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
1249 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1250 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001251 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001252 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001253 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001254
1255 error = xfs_alloc_find_best_extent(args,
1256 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001257 gtdiff, &ltbno, &ltlen,
1258 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001259 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001261
1262 if (error)
1263 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * If we couldn't get anything, give up.
1268 */
1269 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001270 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1271
Christoph Hellwigebf55872017-02-07 14:06:57 -08001272 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001273 trace_xfs_alloc_near_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001274 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001275 goto restart;
1276 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001277 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 args->agbno = NULLAGBLOCK;
1279 return 0;
1280 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * At this point we have selected a freespace entry, either to the
1284 * left or to the right. If it's on the right, copy all the
1285 * useful variables to the "left" set so we only have one
1286 * copy of this code.
1287 */
1288 if (bno_cur_gt) {
1289 bno_cur_lt = bno_cur_gt;
1290 bno_cur_gt = NULL;
1291 ltbno = gtbno;
1292 ltbnoa = gtbnoa;
1293 ltlen = gtlen;
1294 ltlena = gtlena;
1295 j = 1;
1296 } else
1297 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * Fix up the length and compute the useful address.
1301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1303 xfs_alloc_fix_len(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001305 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001306 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001308 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001309 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001310 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1314 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1315 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001316
1317 if (j)
1318 trace_xfs_alloc_near_greater(args);
1319 else
1320 trace_xfs_alloc_near_lesser(args);
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1323 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1324 return 0;
1325
1326 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001327 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (cnt_cur != NULL)
1329 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1330 if (bno_cur_lt != NULL)
1331 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1332 if (bno_cur_gt != NULL)
1333 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1334 return error;
1335}
1336
1337/*
1338 * Allocate a variable extent anywhere in the allocation group agno.
1339 * Extent's length (returned in len) will be between minlen and maxlen,
1340 * and of the form k * prod + mod unless there's nothing that large.
1341 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1342 */
1343STATIC int /* error */
1344xfs_alloc_ag_vextent_size(
1345 xfs_alloc_arg_t *args) /* allocation argument structure */
1346{
1347 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1348 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1349 int error; /* error result */
1350 xfs_agblock_t fbno; /* start of found freespace */
1351 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int i; /* temp status variable */
1353 xfs_agblock_t rbno; /* returned block number */
1354 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001355 bool busy;
1356 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Christoph Hellwige26f0502011-04-24 19:06:15 +00001358restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Allocate and initialize a cursor for the by-size btree.
1361 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001362 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1363 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 bno_cur = NULL;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001365 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * Look for an entry >= maxlen+alignment-1 blocks.
1369 */
1370 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1371 args->maxlen + args->alignment - 1, &i)))
1372 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
Christoph Hellwigebf55872017-02-07 14:06:57 -08001375 * If none then we have to settle for a smaller extent. In the case that
1376 * there are no large extents, this will return the last entry in the
1377 * tree unless the tree is empty. In the case that there are only busy
1378 * large extents, this will return the largest small extent unless there
Christoph Hellwige26f0502011-04-24 19:06:15 +00001379 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001381 if (!i) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001382 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1383 &fbno, &flen, &i);
1384 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto error0;
1386 if (i == 0 || flen == 0) {
1387 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001388 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
1390 }
1391 ASSERT(i == 1);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001392 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1393 &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001394 } else {
1395 /*
1396 * Search for a non-busy extent that is large enough.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001397 */
1398 for (;;) {
1399 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1400 if (error)
1401 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001402 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001403
Christoph Hellwigebf55872017-02-07 14:06:57 -08001404 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1405 &rbno, &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001406
1407 if (rlen >= args->maxlen)
1408 break;
1409
1410 error = xfs_btree_increment(cnt_cur, 0, &i);
1411 if (error)
1412 goto error0;
1413 if (i == 0) {
1414 /*
1415 * Our only valid extents must have been busy.
1416 * Make it unbusy by forcing the log out and
Christoph Hellwigebf55872017-02-07 14:06:57 -08001417 * retrying.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001418 */
1419 xfs_btree_del_cursor(cnt_cur,
1420 XFS_BTREE_NOERROR);
1421 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001422 xfs_extent_busy_flush(args->mp,
1423 args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001424 goto restart;
1425 }
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * In the first case above, we got the last entry in the
1431 * by-size btree. Now we check to see if the space hits maxlen
1432 * once aligned; if not, we search left for something better.
1433 * This can't happen in the second case above.
1434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001436 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1438 if (rlen < args->maxlen) {
1439 xfs_agblock_t bestfbno;
1440 xfs_extlen_t bestflen;
1441 xfs_agblock_t bestrbno;
1442 xfs_extlen_t bestrlen;
1443
1444 bestrlen = rlen;
1445 bestrbno = rbno;
1446 bestflen = flen;
1447 bestfbno = fbno;
1448 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001449 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto error0;
1451 if (i == 0)
1452 break;
1453 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1454 &i)))
1455 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001456 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (flen < bestrlen)
1458 break;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001459 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1460 &rbno, &rlen, &busy_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001462 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 (rlen <= flen && rbno + rlen <= fbno + flen),
1464 error0);
1465 if (rlen > bestrlen) {
1466 bestrlen = rlen;
1467 bestrbno = rbno;
1468 bestflen = flen;
1469 bestfbno = fbno;
1470 if (rlen == args->maxlen)
1471 break;
1472 }
1473 }
1474 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1475 &i)))
1476 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001477 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 rlen = bestrlen;
1479 rbno = bestrbno;
1480 flen = bestflen;
1481 fbno = bestfbno;
1482 }
1483 args->wasfromfl = 0;
1484 /*
1485 * Fix up the length.
1486 */
1487 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001488 if (rlen < args->minlen) {
Christoph Hellwigebf55872017-02-07 14:06:57 -08001489 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001490 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1491 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001492 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001493 goto restart;
1494 }
1495 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001497 xfs_alloc_fix_len(args);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001500 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /*
1502 * Allocate and initialize a cursor for the by-block tree.
1503 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001504 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1505 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1507 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1508 goto error0;
1509 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1510 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1511 cnt_cur = bno_cur = NULL;
1512 args->len = rlen;
1513 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001514 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001516 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001518 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520
1521error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001522 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (cnt_cur)
1524 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1525 if (bno_cur)
1526 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1527 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001528
1529out_nominleft:
1530 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1531 trace_xfs_alloc_size_nominleft(args);
1532 args->agbno = NULLAGBLOCK;
1533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/*
1537 * Deal with the case where only small freespaces remain.
1538 * Either return the contents of the last freespace record,
1539 * or allocate space from the freelist if there is nothing in the tree.
1540 */
1541STATIC int /* error */
1542xfs_alloc_ag_vextent_small(
1543 xfs_alloc_arg_t *args, /* allocation argument structure */
1544 xfs_btree_cur_t *ccur, /* by-size cursor */
1545 xfs_agblock_t *fbnop, /* result block number */
1546 xfs_extlen_t *flenp, /* result length */
1547 int *stat) /* status: 0-freelist, 1-normal/none */
1548{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001549 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001550 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int error;
1552 xfs_agblock_t fbno;
1553 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int i;
1555
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001556 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto error0;
1558 if (i) {
1559 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1560 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001561 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 /*
1564 * Nothing in the btree, try the freelist. Make sure
1565 * to respect minleft even when pulling from the
1566 * freelist.
1567 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001568 else if (args->minlen == 1 && args->alignment == 1 &&
1569 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001570 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1571 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001572 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1573 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto error0;
1575 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001576 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001577 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001578
Dave Chinner292378e2016-09-26 08:21:28 +10001579 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 xfs_buf_t *bp;
1581
1582 bp = xfs_btree_get_bufs(args->mp, args->tp,
1583 args->agno, fbno, 0);
Eric Sandeen93e8bef2017-10-09 21:08:06 -07001584 if (!bp) {
1585 error = -EFSCORRUPTED;
1586 goto error0;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 xfs_trans_binval(args->tp, bp);
1589 }
1590 args->len = 1;
1591 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001592 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001594 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 error0);
1596 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001597 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001598
1599 /*
1600 * If we're feeding an AGFL block to something that
1601 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001602 * out the OWN_AG rmap and add the block back to
1603 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001604 */
1605 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1606 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1607 fbno, 1, &oinfo);
1608 if (error)
1609 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001610 pag = xfs_perag_get(args->mp, args->agno);
1611 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1612 args->tp, 1);
1613 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *stat = 0;
1616 return 0;
1617 }
1618 /*
1619 * Nothing in the freelist.
1620 */
1621 else
1622 flen = 0;
1623 }
1624 /*
1625 * Can't allocate from the freelist for some reason.
1626 */
Nathan Scottd432c802006-09-28 11:03:44 +10001627 else {
1628 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Can't do the allocation, give up.
1633 */
1634 if (flen < args->minlen) {
1635 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001636 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 flen = 0;
1638 }
1639 *fbnop = fbno;
1640 *flenp = flen;
1641 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001642 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
1644
1645error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001646 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return error;
1648}
1649
1650/*
1651 * Free the extent starting at agno/bno for length.
1652 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001653STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001655 xfs_trans_t *tp,
1656 xfs_buf_t *agbp,
1657 xfs_agnumber_t agno,
1658 xfs_agblock_t bno,
1659 xfs_extlen_t len,
1660 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001661 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1664 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1665 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 xfs_agblock_t gtbno; /* start of right neighbor block */
1667 xfs_extlen_t gtlen; /* length of right neighbor block */
1668 int haveleft; /* have a left neighbor block */
1669 int haveright; /* have a right neighbor block */
1670 int i; /* temp, result code */
1671 xfs_agblock_t ltbno; /* start of left neighbor block */
1672 xfs_extlen_t ltlen; /* length of left neighbor block */
1673 xfs_mount_t *mp; /* mount point struct for filesystem */
1674 xfs_agblock_t nbno; /* new starting block of freespace */
1675 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001676 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Darrick J. Wong673930c2016-08-03 11:33:43 +10001678 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001680
Darrick J. Wong33df3a92017-12-07 19:07:27 -08001681 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
Darrick J. Wong673930c2016-08-03 11:33:43 +10001682 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1683 if (error)
1684 goto error0;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Allocate and initialize a cursor for the by-block btree.
1689 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001690 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /*
1692 * Look for a neighboring block on the left (lower block numbers)
1693 * that is contiguous with this space.
1694 */
1695 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1696 goto error0;
1697 if (haveleft) {
1698 /*
1699 * There is a block to our left.
1700 */
1701 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1702 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001703 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * It's not contiguous, though.
1706 */
1707 if (ltbno + ltlen < bno)
1708 haveleft = 0;
1709 else {
1710 /*
1711 * If this failure happens the request to free this
1712 * space was invalid, it's (partly) already free.
1713 * Very bad.
1714 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001715 XFS_WANT_CORRUPTED_GOTO(mp,
1716 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718 }
1719 /*
1720 * Look for a neighboring block on the right (higher block numbers)
1721 * that is contiguous with this space.
1722 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001723 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto error0;
1725 if (haveright) {
1726 /*
1727 * There is a block to our right.
1728 */
1729 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1730 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001731 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
1733 * It's not contiguous, though.
1734 */
1735 if (bno + len < gtbno)
1736 haveright = 0;
1737 else {
1738 /*
1739 * If this failure happens the request to free this
1740 * space was invalid, it's (partly) already free.
1741 * Very bad.
1742 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001743 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 }
1746 /*
1747 * Now allocate and initialize a cursor for the by-size tree.
1748 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001749 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /*
1751 * Have both left and right contiguous neighbors.
1752 * Merge all three into a single free block.
1753 */
1754 if (haveleft && haveright) {
1755 /*
1756 * Delete the old by-size entry on the left.
1757 */
1758 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1759 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001760 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001761 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001763 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Delete the old by-size entry on the right.
1766 */
1767 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1768 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001769 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001770 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001772 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
1774 * Delete the old by-block entry for the right block.
1775 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001776 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001778 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * Move the by-block cursor back to the left neighbor.
1781 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001782 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001784 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785#ifdef DEBUG
1786 /*
1787 * Check that this is the right record: delete didn't
1788 * mangle the cursor.
1789 */
1790 {
1791 xfs_agblock_t xxbno;
1792 xfs_extlen_t xxlen;
1793
1794 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1795 &i)))
1796 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001797 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 i == 1 && xxbno == ltbno && xxlen == ltlen,
1799 error0);
1800 }
1801#endif
1802 /*
1803 * Update remaining by-block entry to the new, joined block.
1804 */
1805 nbno = ltbno;
1806 nlen = len + ltlen + gtlen;
1807 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1808 goto error0;
1809 }
1810 /*
1811 * Have only a left contiguous neighbor.
1812 * Merge it together with the new freespace.
1813 */
1814 else if (haveleft) {
1815 /*
1816 * Delete the old by-size entry on the left.
1817 */
1818 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1819 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001820 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001821 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001823 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Back up the by-block cursor to the left neighbor, and
1826 * update its length.
1827 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001828 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001830 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 nbno = ltbno;
1832 nlen = len + ltlen;
1833 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1834 goto error0;
1835 }
1836 /*
1837 * Have only a right contiguous neighbor.
1838 * Merge it together with the new freespace.
1839 */
1840 else if (haveright) {
1841 /*
1842 * Delete the old by-size entry on the right.
1843 */
1844 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1845 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001847 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * Update the starting block and length of the right
1852 * neighbor in the by-block tree.
1853 */
1854 nbno = bno;
1855 nlen = len + gtlen;
1856 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1857 goto error0;
1858 }
1859 /*
1860 * No contiguous neighbors.
1861 * Insert the new freespace into the by-block tree.
1862 */
1863 else {
1864 nbno = bno;
1865 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001866 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001868 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1871 bno_cur = NULL;
1872 /*
1873 * In all cases we need to insert the new freespace in the by-size tree.
1874 */
1875 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1876 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001877 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001878 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001880 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1882 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /*
1885 * Update the freespace totals in the ag and superblock.
1886 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001887 pag = xfs_perag_get(mp, agno);
1888 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001889 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001890 xfs_perag_put(pag);
1891 if (error)
1892 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001894 XFS_STATS_INC(mp, xs_freex);
1895 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001896
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001897 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1898 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
1901
1902 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001903 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1904 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (bno_cur)
1906 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1907 if (cnt_cur)
1908 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1909 return error;
1910}
1911
1912/*
1913 * Visible (exported) allocation/free functions.
1914 * Some of these are used just by xfs_alloc_btree.c and this file.
1915 */
1916
1917/*
1918 * Compute and fill in value of m_ag_maxlevels.
1919 */
1920void
1921xfs_alloc_compute_maxlevels(
1922 xfs_mount_t *mp) /* file system mount structure */
1923{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001924 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1925 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
1928/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001929 * Find the length of the longest extent in an AG. The 'need' parameter
1930 * specifies how much space we're going to need for the AGFL and the
1931 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1932 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001933 */
1934xfs_extlen_t
1935xfs_alloc_longest_free_extent(
1936 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001937 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001938 xfs_extlen_t need,
1939 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001940{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001941 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001942
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001943 /*
1944 * If the AGFL needs a recharge, we'll have to subtract that from the
1945 * longest extent.
1946 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001947 if (need > pag->pagf_flcount)
1948 delta = need - pag->pagf_flcount;
1949
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001950 /*
1951 * If we cannot maintain others' reservations with space from the
1952 * not-longest freesp extents, we'll have to subtract /that/ from
1953 * the longest extent too.
1954 */
1955 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1956 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1957
1958 /*
1959 * If the longest extent is long enough to satisfy all the
1960 * reservations and AGFL rules in place, we can return this extent.
1961 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001962 if (pag->pagf_longest > delta)
1963 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001964
1965 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01001966 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1967}
1968
Dave Chinner496817b2015-06-22 10:13:30 +10001969unsigned int
1970xfs_alloc_min_freelist(
1971 struct xfs_mount *mp,
1972 struct xfs_perag *pag)
1973{
1974 unsigned int min_free;
1975
1976 /* space needed by-bno freespace btree */
1977 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1978 mp->m_ag_maxlevels);
1979 /* space needed by-size freespace btree */
1980 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1981 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10001982 /* space needed reverse mapping used space btree */
1983 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1984 min_free += min_t(unsigned int,
1985 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
1986 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10001987
1988 return min_free;
1989}
1990
Dave Chinner6cc87642009-03-16 08:29:46 +01001991/*
Dave Chinner72d55282015-06-22 10:04:42 +10001992 * Check if the operation we are fixing up the freelist for should go ahead or
1993 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1994 * is dependent on whether the size and shape of free space available will
1995 * permit the requested allocation to take place.
1996 */
1997static bool
1998xfs_alloc_space_available(
1999 struct xfs_alloc_arg *args,
2000 xfs_extlen_t min_free,
2001 int flags)
2002{
2003 struct xfs_perag *pag = args->pag;
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002004 xfs_extlen_t alloc_len, longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002005 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002006 int available;
2007
2008 if (flags & XFS_ALLOC_FLAG_FREEING)
2009 return true;
2010
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002011 reservation = xfs_ag_resv_needed(pag, args->resv);
2012
Dave Chinner72d55282015-06-22 10:04:42 +10002013 /* do we have enough contiguous free space for the allocation? */
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002014 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002015 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2016 reservation);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002017 if (longest < alloc_len)
Dave Chinner72d55282015-06-22 10:04:42 +10002018 return false;
2019
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002020 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002021 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Christoph Hellwig54fee132017-01-09 13:44:30 -08002022 reservation - min_free - args->minleft);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002023 if (available < (int)max(args->total, alloc_len))
Dave Chinner72d55282015-06-22 10:04:42 +10002024 return false;
2025
Christoph Hellwig54fee132017-01-09 13:44:30 -08002026 /*
2027 * Clamp maxlen to the amount of free space available for the actual
2028 * extent allocation.
2029 */
2030 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2031 args->maxlen = available;
2032 ASSERT(args->maxlen > 0);
2033 ASSERT(args->maxlen >= args->minlen);
2034 }
2035
Dave Chinner72d55282015-06-22 10:04:42 +10002036 return true;
2037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
2040 * Decide whether to use this allocation group for this allocation.
2041 * If so, fix up the btree freelist's size.
2042 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002043int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002045 struct xfs_alloc_arg *args, /* allocation argument structure */
2046 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Dave Chinner396503f2015-06-22 10:13:19 +10002048 struct xfs_mount *mp = args->mp;
2049 struct xfs_perag *pag = args->pag;
2050 struct xfs_trans *tp = args->tp;
2051 struct xfs_buf *agbp = NULL;
2052 struct xfs_buf *agflbp = NULL;
2053 struct xfs_alloc_arg targs; /* local allocation arguments */
2054 xfs_agblock_t bno; /* freelist block */
2055 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002056 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002059 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2060 if (error)
2061 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002063 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2064 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002065 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Dave Chinner396503f2015-06-22 10:13:19 +10002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002069 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002070 * If this is a metadata preferred pag and we are user data then try
2071 * somewhere else if we are not being asked to try harder at this
2072 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 */
Dave Chinner292378e2016-09-26 08:21:28 +10002074 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002075 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2076 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002077 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079
Dave Chinner496817b2015-06-22 10:13:30 +10002080 need = xfs_alloc_min_freelist(mp, pag);
Christoph Hellwig54fee132017-01-09 13:44:30 -08002081 if (!xfs_alloc_space_available(args, need, flags |
2082 XFS_ALLOC_FLAG_CHECK))
Dave Chinner396503f2015-06-22 10:13:19 +10002083 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Get the a.g. freespace buffer.
2087 * Can fail if we're not blocking on locks, and it's held.
2088 */
Dave Chinner396503f2015-06-22 10:13:19 +10002089 if (!agbp) {
2090 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2091 if (error)
2092 goto out_no_agbp;
2093 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002094 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2095 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002096 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002099
Dave Chinner50adbcb2015-06-22 10:04:31 +10002100 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002101 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002102 if (!xfs_alloc_space_available(args, need, flags))
2103 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002107 *
Dave Chinner396503f2015-06-22 10:13:19 +10002108 * Note that from this point onwards, we will always release the agf and
2109 * agfl buffers on error. This handles the case where we error out and
2110 * the buffers are clean or may not have been joined to the transaction
2111 * and hence need to be released manually. If they have been joined to
2112 * the transaction, then xfs_trans_brelse() will handle them
2113 * appropriately based on the recursion count and dirty state of the
2114 * buffer.
2115 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002116 * XXX (dgc): When we have lots of free space, does this buy us
2117 * anything other than extra overhead when we need to put more blocks
2118 * back on the free list? Maybe we should only do this when space is
2119 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002120 *
2121 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2122 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2123 * updating the rmapbt. Both flags are used in xfs_repair while we're
2124 * rebuilding the rmapbt, and neither are used by the kernel. They're
2125 * both required to ensure that rmaps are correctly recorded for the
2126 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2127 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002129 memset(&targs, 0, sizeof(targs));
2130 if (flags & XFS_ALLOC_FLAG_NORMAP)
2131 xfs_rmap_skip_owner_update(&targs.oinfo);
2132 else
2133 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2134 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002135 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
David Chinner92821e22007-05-24 15:26:31 +10002137 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2138 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002139 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002140 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002141 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002142 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002143 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
Eric Sandeen93e8bef2017-10-09 21:08:06 -07002145 if (!bp) {
2146 error = -EFSCORRUPTED;
2147 goto out_agbp_relse;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 xfs_trans_binval(tp, bp);
2150 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 targs.tp = tp;
2153 targs.mp = mp;
2154 targs.agbp = agbp;
2155 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002156 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 targs.type = XFS_ALLOCTYPE_THIS_AG;
2158 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002159 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2160 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002161 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002162
2163 /* Make the freelist longer if it's too short. */
2164 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002166 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002167 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002168
2169 /* Allocate as many blocks as possible at once. */
2170 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002171 if (error)
2172 goto out_agflbp_relse;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /*
2175 * Stop if we run out. Won't happen if callers are obeying
2176 * the restrictions correctly. Can happen for free calls
2177 * on a completely full ag.
2178 */
Yingping Lud210a282006-06-09 14:55:18 +10002179 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002180 if (flags & XFS_ALLOC_FLAG_FREEING)
2181 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002182 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
2185 * Put each allocated block on the list.
2186 */
2187 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002188 error = xfs_alloc_put_freelist(tp, agbp,
2189 agflbp, bno, 0);
2190 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002191 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002194 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 args->agbp = agbp;
2196 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002197
2198out_agflbp_relse:
2199 xfs_trans_brelse(tp, agflbp);
2200out_agbp_relse:
2201 if (agbp)
2202 xfs_trans_brelse(tp, agbp);
2203out_no_agbp:
2204 args->agbp = NULL;
2205 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208/*
2209 * Get a block from the freelist.
2210 * Returns with the buffer for the block gotten.
2211 */
2212int /* error */
2213xfs_alloc_get_freelist(
2214 xfs_trans_t *tp, /* transaction pointer */
2215 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002216 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2217 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2221 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002222 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int error;
David Chinner92821e22007-05-24 15:26:31 +10002224 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002225 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 xfs_perag_t *pag; /* per allocation group data */
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /*
2229 * Freelist is empty, give up.
2230 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002231 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (!agf->agf_flcount) {
2233 *bnop = NULLAGBLOCK;
2234 return 0;
2235 }
2236 /*
2237 * Read the array of free blocks.
2238 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002239 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2240 &agflbp);
2241 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002243
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /*
2246 * Get the block number and update the data structures.
2247 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002248 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2249 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002250 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002252 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002254
2255 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002256 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 xfs_trans_agflist_delta(tp, -1);
2258 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002259 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002260
2261 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2262 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002263 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002264 pag->pagf_btreeblks++;
2265 logflags |= XFS_AGF_BTREEBLKS;
2266 }
2267
David Chinner92821e22007-05-24 15:26:31 +10002268 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *bnop = bno;
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
2272}
2273
2274/*
2275 * Log the given fields from the agf structure.
2276 */
2277void
2278xfs_alloc_log_agf(
2279 xfs_trans_t *tp, /* transaction pointer */
2280 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2281 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2282{
2283 int first; /* first byte offset */
2284 int last; /* last byte offset */
2285 static const short offsets[] = {
2286 offsetof(xfs_agf_t, agf_magicnum),
2287 offsetof(xfs_agf_t, agf_versionnum),
2288 offsetof(xfs_agf_t, agf_seqno),
2289 offsetof(xfs_agf_t, agf_length),
2290 offsetof(xfs_agf_t, agf_roots[0]),
2291 offsetof(xfs_agf_t, agf_levels[0]),
2292 offsetof(xfs_agf_t, agf_flfirst),
2293 offsetof(xfs_agf_t, agf_fllast),
2294 offsetof(xfs_agf_t, agf_flcount),
2295 offsetof(xfs_agf_t, agf_freeblks),
2296 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002297 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002298 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002299 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002300 offsetof(xfs_agf_t, agf_refcount_blocks),
2301 offsetof(xfs_agf_t, agf_refcount_root),
2302 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002303 /* needed so that we don't log the whole rest of the structure: */
2304 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 sizeof(xfs_agf_t)
2306 };
2307
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002308 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2309
Dave Chinner61fe1352013-04-03 16:11:30 +11002310 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2313 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2314}
2315
2316/*
2317 * Interface for inode allocation to force the pag data to be initialized.
2318 */
2319int /* error */
2320xfs_alloc_pagf_init(
2321 xfs_mount_t *mp, /* file system mount structure */
2322 xfs_trans_t *tp, /* transaction pointer */
2323 xfs_agnumber_t agno, /* allocation group number */
2324 int flags) /* XFS_ALLOC_FLAGS_... */
2325{
2326 xfs_buf_t *bp;
2327 int error;
2328
2329 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2330 return error;
2331 if (bp)
2332 xfs_trans_brelse(tp, bp);
2333 return 0;
2334}
2335
2336/*
2337 * Put the block on the freelist for the allocation group.
2338 */
2339int /* error */
2340xfs_alloc_put_freelist(
2341 xfs_trans_t *tp, /* transaction pointer */
2342 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2343 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002344 xfs_agblock_t bno, /* block being freed */
2345 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002348 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int error;
David Chinner92821e22007-05-24 15:26:31 +10002350 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 xfs_mount_t *mp; /* mount structure */
2352 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002353 __be32 *agfl_bno;
2354 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 agf = XFS_BUF_TO_AGF(agbp);
2357 mp = tp->t_mountp;
2358
2359 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002360 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002362 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002363 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002365
2366 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002367 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 xfs_trans_agflist_delta(tp, 1);
2369 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002370
2371 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2372 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002373 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002374 pag->pagf_btreeblks--;
2375 logflags |= XFS_AGF_BTREEBLKS;
2376 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002377 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002378
David Chinner92821e22007-05-24 15:26:31 +10002379 xfs_alloc_log_agf(tp, agbp, logflags);
2380
Christoph Hellwig16259e72005-11-02 15:11:25 +11002381 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002382
2383 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2384 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002385 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002386 startoff = (char *)blockp - (char *)agflbp->b_addr;
2387
David Chinner92821e22007-05-24 15:26:31 +10002388 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002389
Dave Chinner61fe1352013-04-03 16:11:30 +11002390 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002391 xfs_trans_log_buf(tp, agflbp, startoff,
2392 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return 0;
2394}
2395
Dave Chinner4e0e6042013-04-03 16:11:13 +11002396static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002397xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002398 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002399 struct xfs_buf *bp)
2400 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002401 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002402
Brian Fostera45086e2015-10-12 15:59:25 +11002403 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2404 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002405 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002406 if (!xfs_log_check_lsn(mp,
2407 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2408 return false;
2409 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002410
Dave Chinner4e0e6042013-04-03 16:11:13 +11002411 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2412 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2413 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2414 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2415 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2416 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2417 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002418
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002419 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2420 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2421 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002422 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2423 return false;
2424
Darrick J. Wongb8704942016-08-03 11:30:32 +10002425 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002426 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2427 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wongb8704942016-08-03 11:30:32 +10002428 return false;
2429
Dave Chinner5d5f5272012-11-14 17:44:56 +11002430 /*
2431 * during growfs operations, the perag is not fully initialised,
2432 * so we can't use it for any useful checking. growfs ensures we can't
2433 * use it by using uncached buffers that don't have the perag attached
2434 * so we can detect and avoid this problem.
2435 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002436 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2437 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002438
Dave Chinner4e0e6042013-04-03 16:11:13 +11002439 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2440 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2441 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002442
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002443 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002444 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2445 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002446 return false;
2447
Dave Chinner4e0e6042013-04-03 16:11:13 +11002448 return true;;
2449
Dave Chinner612cfbf2012-11-14 17:52:32 +11002450}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002451
Dave Chinner1813dd62012-11-14 17:54:40 +11002452static void
2453xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002454 struct xfs_buf *bp)
2455{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002456 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002457
Eric Sandeence5028c2014-02-27 15:23:10 +11002458 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2459 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002460 xfs_verifier_error(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002461 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07002462 XFS_ERRTAG_ALLOC_READ_AGF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002463 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002464}
2465
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002466static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002467xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002468 struct xfs_buf *bp)
2469{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002470 struct xfs_mount *mp = bp->b_target->bt_mount;
2471 struct xfs_buf_log_item *bip = bp->b_fspriv;
2472
2473 if (!xfs_agf_verify(mp, bp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002474 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002475 return;
2476 }
2477
2478 if (!xfs_sb_version_hascrc(&mp->m_sb))
2479 return;
2480
2481 if (bip)
2482 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2483
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002484 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002485}
2486
Dave Chinner1813dd62012-11-14 17:54:40 +11002487const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002488 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002489 .verify_read = xfs_agf_read_verify,
2490 .verify_write = xfs_agf_write_verify,
2491};
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
2494 * Read in the allocation group header (free/alloc section).
2495 */
2496int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002497xfs_read_agf(
2498 struct xfs_mount *mp, /* mount point structure */
2499 struct xfs_trans *tp, /* transaction pointer */
2500 xfs_agnumber_t agno, /* allocation group number */
2501 int flags, /* XFS_BUF_ */
2502 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int error;
2505
Dave Chinnerd1230312013-11-01 15:27:19 +11002506 trace_xfs_read_agf(mp, agno);
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 ASSERT(agno != NULLAGNUMBER);
2509 error = xfs_trans_read_buf(
2510 mp, tp, mp->m_ddev_targp,
2511 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002512 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (error)
2514 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002515 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002517
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002518 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002519 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002520 return 0;
2521}
2522
2523/*
2524 * Read in the allocation group header (free/alloc section).
2525 */
2526int /* error */
2527xfs_alloc_read_agf(
2528 struct xfs_mount *mp, /* mount point structure */
2529 struct xfs_trans *tp, /* transaction pointer */
2530 xfs_agnumber_t agno, /* allocation group number */
2531 int flags, /* XFS_ALLOC_FLAG_... */
2532 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2533{
2534 struct xfs_agf *agf; /* ag freelist header */
2535 struct xfs_perag *pag; /* per allocation group data */
2536 int error;
2537
Dave Chinnerd1230312013-11-01 15:27:19 +11002538 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002539
Dave Chinnerd1230312013-11-01 15:27:19 +11002540 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002541 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002542 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002543 bpp);
2544 if (error)
2545 return error;
2546 if (!*bpp)
2547 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002548 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002549
2550 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002551 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002553 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002554 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002555 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2556 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002558 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002560 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002561 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2562 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002563 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002564 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002565 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002566 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 pag->pagf_init = 1;
2568 }
2569#ifdef DEBUG
2570 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002571 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002572 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002573 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2574 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002576 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002578 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002581 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583}
2584
2585/*
2586 * Allocate an extent (variable-size).
2587 * Depending on the allocation type, we either look in a single allocation
2588 * group or loop over the allocation groups to find the result.
2589 */
2590int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002591xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 xfs_alloc_arg_t *args) /* allocation argument structure */
2593{
2594 xfs_agblock_t agsize; /* allocation group size */
2595 int error;
2596 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 xfs_mount_t *mp; /* mount structure pointer */
2598 xfs_agnumber_t sagno; /* starting allocation group number */
2599 xfs_alloctype_t type; /* input allocation type */
2600 int bump_rotor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2602
2603 mp = args->mp;
2604 type = args->otype = args->type;
2605 args->agbno = NULLAGBLOCK;
2606 /*
2607 * Just fix this up, for the case where the last a.g. is shorter
2608 * (or there's only one a.g.) and the caller couldn't easily figure
2609 * that out (xfs_bmap_alloc).
2610 */
2611 agsize = mp->m_sb.sb_agblocks;
2612 if (args->maxlen > agsize)
2613 args->maxlen = agsize;
2614 if (args->alignment == 0)
2615 args->alignment = 1;
2616 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2617 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2618 ASSERT(args->minlen <= args->maxlen);
2619 ASSERT(args->minlen <= agsize);
2620 ASSERT(args->mod < args->prod);
2621 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2622 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2623 args->minlen > args->maxlen || args->minlen > agsize ||
2624 args->mod >= args->prod) {
2625 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002626 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return 0;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 switch (type) {
2631 case XFS_ALLOCTYPE_THIS_AG:
2632 case XFS_ALLOCTYPE_NEAR_BNO:
2633 case XFS_ALLOCTYPE_THIS_BNO:
2634 /*
2635 * These three force us into a single a.g.
2636 */
2637 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002638 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 error = xfs_alloc_fix_freelist(args, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002641 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 goto error0;
2643 }
2644 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002645 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 break;
2647 }
2648 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2649 if ((error = xfs_alloc_ag_vextent(args)))
2650 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 break;
2652 case XFS_ALLOCTYPE_START_BNO:
2653 /*
2654 * Try near allocation first, then anywhere-in-ag after
2655 * the first a.g. fails.
2656 */
Dave Chinner292378e2016-09-26 08:21:28 +10002657 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2659 args->fsbno = XFS_AGB_TO_FSB(mp,
2660 ((mp->m_agfrotor / rotorstep) %
2661 mp->m_sb.sb_agcount), 0);
2662 bump_rotor = 1;
2663 }
2664 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2665 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2666 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 case XFS_ALLOCTYPE_FIRST_AG:
2668 /*
2669 * Rotate through the allocation groups looking for a winner.
2670 */
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002671 if (type == XFS_ALLOCTYPE_FIRST_AG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /*
2673 * Start with allocation group given by bno.
2674 */
2675 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2676 args->type = XFS_ALLOCTYPE_THIS_AG;
2677 sagno = 0;
2678 flags = 0;
2679 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /*
2681 * Start with the given allocation group.
2682 */
2683 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2684 flags = XFS_ALLOC_FLAG_TRYLOCK;
2685 }
2686 /*
2687 * Loop over allocation groups twice; first time with
2688 * trylock set, second time without.
2689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002691 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 error = xfs_alloc_fix_freelist(args, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002694 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 goto error0;
2696 }
2697 /*
2698 * If we get a buffer back then the allocation will fly.
2699 */
2700 if (args->agbp) {
2701 if ((error = xfs_alloc_ag_vextent(args)))
2702 goto error0;
2703 break;
2704 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002705
2706 trace_xfs_alloc_vextent_loopfailed(args);
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /*
2709 * Didn't work, figure out the next iteration.
2710 */
2711 if (args->agno == sagno &&
2712 type == XFS_ALLOCTYPE_START_BNO)
2713 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002714 /*
2715 * For the first allocation, we can try any AG to get
2716 * space. However, if we already have allocated a
2717 * block, we don't want to try AGs whose number is below
2718 * sagno. Otherwise, we may end up with out-of-order
2719 * locking of AGF, which might cause deadlock.
2720 */
2721 if (++(args->agno) == mp->m_sb.sb_agcount) {
2722 if (args->firstblock != NULLFSBLOCK)
2723 args->agno = sagno;
2724 else
2725 args->agno = 0;
2726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * Reached the starting a.g., must either be done
2729 * or switch to non-trylock mode.
2730 */
2731 if (args->agno == sagno) {
Christoph Hellwig255c5162017-01-09 13:36:19 -08002732 if (flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002734 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 break;
2736 }
Christoph Hellwig255c5162017-01-09 13:36:19 -08002737
2738 flags = 0;
2739 if (type == XFS_ALLOCTYPE_START_BNO) {
2740 args->agbno = XFS_FSB_TO_AGBNO(mp,
2741 args->fsbno);
2742 args->type = XFS_ALLOCTYPE_NEAR_BNO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002745 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002747 if (bump_rotor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (args->agno == sagno)
2749 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2750 (mp->m_sb.sb_agcount * rotorstep);
2751 else
2752 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2753 (mp->m_sb.sb_agcount * rotorstep);
2754 }
2755 break;
2756 default:
2757 ASSERT(0);
2758 /* NOTREACHED */
2759 }
2760 if (args->agbno == NULLAGBLOCK)
2761 args->fsbno = NULLFSBLOCK;
2762 else {
2763 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2764#ifdef DEBUG
2765 ASSERT(args->len >= args->minlen);
2766 ASSERT(args->len <= args->maxlen);
2767 ASSERT(args->agbno % args->alignment == 0);
2768 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2769 args->len);
2770#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002771
2772 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002773 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002774 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2775 if (error)
2776 goto error0;
2777 }
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002780 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
2782error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002783 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return error;
2785}
2786
Dave Chinner4d89e202016-06-21 11:53:28 +10002787/* Ensure that the freelist is at full capacity. */
2788int
2789xfs_free_extent_fix_freelist(
2790 struct xfs_trans *tp,
2791 xfs_agnumber_t agno,
2792 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Dave Chinner4d89e202016-06-21 11:53:28 +10002794 struct xfs_alloc_arg args;
2795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Dave Chinner4d89e202016-06-21 11:53:28 +10002797 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 args.tp = tp;
2799 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002800 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002801
2802 /*
2803 * validate that the block number is legal - the enables us to detect
2804 * and handle a silent filesystem corruption rather than crashing.
2805 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002806 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002807 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002808
Dave Chinnera862e0f2010-01-11 11:47:41 +00002809 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002810 ASSERT(args.pag);
2811
2812 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2813 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002814 goto out;
2815
2816 *agbp = args.agbp;
2817out:
2818 xfs_perag_put(args.pag);
2819 return error;
2820}
2821
2822/*
2823 * Free an extent.
2824 * Just break up the extent address and hand off to xfs_free_ag_extent
2825 * after fixing up the freelist.
2826 */
2827int /* error */
2828xfs_free_extent(
2829 struct xfs_trans *tp, /* transaction pointer */
2830 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002831 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002832 struct xfs_owner_info *oinfo, /* extent owner */
2833 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002834{
2835 struct xfs_mount *mp = tp->t_mountp;
2836 struct xfs_buf *agbp;
2837 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2838 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2839 int error;
2840
2841 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002842 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002843
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002844 if (XFS_TEST_ERROR(false, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07002845 XFS_ERRTAG_FREE_EXTENT))
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002846 return -EIO;
2847
Dave Chinner4d89e202016-06-21 11:53:28 +10002848 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2849 if (error)
2850 return error;
2851
2852 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002853
2854 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002855 XFS_WANT_CORRUPTED_GOTO(mp,
2856 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2857 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002858
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002859 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002860 if (error)
2861 goto err;
2862
2863 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2864 return 0;
2865
2866err:
2867 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return error;
2869}
Darrick J. Wong2d520bf2017-03-28 14:56:35 -07002870
2871struct xfs_alloc_query_range_info {
2872 xfs_alloc_query_range_fn fn;
2873 void *priv;
2874};
2875
2876/* Format btree record and pass to our callback. */
2877STATIC int
2878xfs_alloc_query_range_helper(
2879 struct xfs_btree_cur *cur,
2880 union xfs_btree_rec *rec,
2881 void *priv)
2882{
2883 struct xfs_alloc_query_range_info *query = priv;
2884 struct xfs_alloc_rec_incore irec;
2885
2886 irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
2887 irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
2888 return query->fn(cur, &irec, query->priv);
2889}
2890
2891/* Find all free space within a given range of blocks. */
2892int
2893xfs_alloc_query_range(
2894 struct xfs_btree_cur *cur,
2895 struct xfs_alloc_rec_incore *low_rec,
2896 struct xfs_alloc_rec_incore *high_rec,
2897 xfs_alloc_query_range_fn fn,
2898 void *priv)
2899{
2900 union xfs_btree_irec low_brec;
2901 union xfs_btree_irec high_brec;
2902 struct xfs_alloc_query_range_info query;
2903
2904 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
2905 low_brec.a = *low_rec;
2906 high_brec.a = *high_rec;
2907 query.priv = priv;
2908 query.fn = fn;
2909 return xfs_btree_query_range(cur, &low_brec, &high_brec,
2910 xfs_alloc_query_range_helper, &query);
2911}
Darrick J. Wonge9a25992017-03-28 14:56:35 -07002912
2913/* Find all free space records. */
2914int
2915xfs_alloc_query_all(
2916 struct xfs_btree_cur *cur,
2917 xfs_alloc_query_range_fn fn,
2918 void *priv)
2919{
2920 struct xfs_alloc_query_range_info query;
2921
2922 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
2923 query.priv = priv;
2924 query.fn = fn;
2925 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
2926}
Darrick J. Wong21ec5412017-10-17 21:37:32 -07002927
2928/* Find the size of the AG, in blocks. */
2929xfs_agblock_t
2930xfs_ag_block_count(
2931 struct xfs_mount *mp,
2932 xfs_agnumber_t agno)
2933{
2934 ASSERT(agno < mp->m_sb.sb_agcount);
2935
2936 if (agno < mp->m_sb.sb_agcount - 1)
2937 return mp->m_sb.sb_agblocks;
2938 return mp->m_sb.sb_dblocks - (agno * mp->m_sb.sb_agblocks);
2939}
2940
2941/*
2942 * Verify that an AG block number pointer neither points outside the AG
2943 * nor points at static metadata.
2944 */
2945bool
2946xfs_verify_agbno(
2947 struct xfs_mount *mp,
2948 xfs_agnumber_t agno,
2949 xfs_agblock_t agbno)
2950{
2951 xfs_agblock_t eoag;
2952
2953 eoag = xfs_ag_block_count(mp, agno);
2954 if (agbno >= eoag)
2955 return false;
2956 if (agbno <= XFS_AGFL_BLOCK(mp))
2957 return false;
2958 return true;
2959}
2960
2961/*
2962 * Verify that an FS block number pointer neither points outside the
2963 * filesystem nor points at static AG metadata.
2964 */
2965bool
2966xfs_verify_fsbno(
2967 struct xfs_mount *mp,
2968 xfs_fsblock_t fsbno)
2969{
2970 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, fsbno);
2971
2972 if (agno >= mp->m_sb.sb_agcount)
2973 return false;
2974 return xfs_verify_agbno(mp, agno, XFS_FSB_TO_AGBNO(mp, fsbno));
2975}