blob: 4d7ef74f5783050dc3b01feeec8fa8b3ca7714d3 [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
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800523static xfs_failaddr_t
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))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800532 return __this_address;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100533 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800534 return __this_address;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100535 /*
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)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800542 return __this_address;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100543
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)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800547 return __this_address;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100548 }
Brian Fostera45086e2015-10-12 15:59:25 +1100549
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800550 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
551 return __this_address;
552 return NULL;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100553}
554
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100555static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100556xfs_agfl_read_verify(
557 struct xfs_buf *bp)
558{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100559 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100560
561 /*
562 * There is no verification of non-crc AGFLs because mkfs does not
563 * initialise the AGFL to zero or NULL. Hence the only valid part of the
564 * AGFL is what the AGF says is active. We can't get to the AGF, so we
565 * can't verify just those entries are valid.
566 */
567 if (!xfs_sb_version_hascrc(&mp->m_sb))
568 return;
569
Eric Sandeence5028c2014-02-27 15:23:10 +1100570 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800571 xfs_verifier_error(bp, -EFSBADCRC);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800572 else if (xfs_agfl_verify(bp))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800573 xfs_verifier_error(bp, -EFSCORRUPTED);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100574}
575
576static void
577xfs_agfl_write_verify(
578 struct xfs_buf *bp)
579{
580 struct xfs_mount *mp = bp->b_target->bt_mount;
581 struct xfs_buf_log_item *bip = bp->b_fspriv;
582
583 /* no verification of non-crc AGFLs */
584 if (!xfs_sb_version_hascrc(&mp->m_sb))
585 return;
586
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800587 if (xfs_agfl_verify(bp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800588 xfs_verifier_error(bp, -EFSCORRUPTED);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100589 return;
590 }
591
592 if (bip)
593 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
594
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100595 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100596}
597
Dave Chinner1813dd62012-11-14 17:54:40 +1100598const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100599 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100600 .verify_read = xfs_agfl_read_verify,
601 .verify_write = xfs_agfl_write_verify,
602};
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
605 * Read in the allocation group free block array.
606 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700607int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608xfs_alloc_read_agfl(
609 xfs_mount_t *mp, /* mount point structure */
610 xfs_trans_t *tp, /* transaction pointer */
611 xfs_agnumber_t agno, /* allocation group number */
612 xfs_buf_t **bpp) /* buffer for the ag free block array */
613{
614 xfs_buf_t *bp; /* return value */
615 int error;
616
617 ASSERT(agno != NULLAGNUMBER);
618 error = xfs_trans_read_buf(
619 mp, tp, mp->m_ddev_targp,
620 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100621 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (error)
623 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000624 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *bpp = bp;
626 return 0;
627}
628
Christoph Hellwigecb69282011-03-04 12:59:55 +0000629STATIC int
630xfs_alloc_update_counters(
631 struct xfs_trans *tp,
632 struct xfs_perag *pag,
633 struct xfs_buf *agbp,
634 long len)
635{
636 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
637
638 pag->pagf_freeblks += len;
639 be32_add_cpu(&agf->agf_freeblks, len);
640
641 xfs_trans_agblocks_delta(tp, len);
642 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
643 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000644 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000645
646 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
647 return 0;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
651 * Allocation group level functions.
652 */
653
654/*
655 * Allocate a variable extent in the allocation group agno.
656 * Type and bno are used to determine where in the allocation group the
657 * extent will start.
658 * Extent's length (returned in *len) will be between minlen and maxlen,
659 * and of the form k * prod + mod unless there's nothing that large.
660 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
661 */
662STATIC int /* error */
663xfs_alloc_ag_vextent(
664 xfs_alloc_arg_t *args) /* argument structure for allocation */
665{
666 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 ASSERT(args->minlen > 0);
669 ASSERT(args->maxlen > 0);
670 ASSERT(args->minlen <= args->maxlen);
671 ASSERT(args->mod < args->prod);
672 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000673
674 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * Branch to correct routine based on the type.
676 */
677 args->wasfromfl = 0;
678 switch (args->type) {
679 case XFS_ALLOCTYPE_THIS_AG:
680 error = xfs_alloc_ag_vextent_size(args);
681 break;
682 case XFS_ALLOCTYPE_NEAR_BNO:
683 error = xfs_alloc_ag_vextent_near(args);
684 break;
685 case XFS_ALLOCTYPE_THIS_BNO:
686 error = xfs_alloc_ag_vextent_exact(args);
687 break;
688 default:
689 ASSERT(0);
690 /* NOTREACHED */
691 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000692
693 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Christoph Hellwigecb69282011-03-04 12:59:55 +0000696 ASSERT(args->len >= args->minlen);
697 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000698 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000699 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Darrick J. Wong673930c2016-08-03 11:33:43 +1000701 /* if not file data, insert new block into the reverse map btree */
Darrick J. Wong33df3a92017-12-07 19:07:27 -0800702 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
Darrick J. Wong673930c2016-08-03 11:33:43 +1000703 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
704 args->agbno, args->len, &args->oinfo);
705 if (error)
706 return error;
707 }
708
Christoph Hellwigecb69282011-03-04 12:59:55 +0000709 if (!args->wasfromfl) {
710 error = xfs_alloc_update_counters(args->tp, args->pag,
711 args->agbp,
712 -((long)(args->len)));
713 if (error)
714 return error;
715
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000716 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000717 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000719
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000720 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000721
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100722 XFS_STATS_INC(args->mp, xs_allocx);
723 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000724 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
728 * Allocate a variable extent at exactly agno/bno.
729 * Extent's length (returned in *len) will be between minlen and maxlen,
730 * and of the form k * prod + mod unless there's nothing that large.
731 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
732 */
733STATIC int /* error */
734xfs_alloc_ag_vextent_exact(
735 xfs_alloc_arg_t *args) /* allocation argument structure */
736{
737 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
738 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int error;
740 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800742 xfs_agblock_t tbno; /* start block of busy extent */
743 xfs_extlen_t tlen; /* length of busy extent */
744 xfs_agblock_t tend; /* end block of busy extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int i; /* success/failure of operation */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800746 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /*
751 * Allocate/initialize a cursor for the by-number freespace btree.
752 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100753 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000754 args->agno, XFS_BTNUM_BNO);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
758 * Look for the closest free block <= bno, it must contain bno
759 * if any free block does.
760 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000761 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
762 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000764 if (!i)
765 goto not_found;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Grab the freespace record.
769 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000770 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
771 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100773 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000777 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800779 tbno = fbno;
780 tlen = flen;
781 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000782
783 /*
784 * Give up if the start of the extent is busy, or the freespace isn't
785 * long enough for the minimum request.
786 */
787 if (tbno > args->agbno)
788 goto not_found;
789 if (tlen < args->minlen)
790 goto not_found;
791 tend = tbno + tlen;
792 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000793 goto not_found;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * End of extent will be smaller of the freespace end and the
797 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * Fix the length according to mod and prod if given.
800 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000801 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
802 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 xfs_alloc_fix_len(args);
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000804 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000807 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * Allocate/initialize a cursor for the by-size btree.
809 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100810 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
811 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100813 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000814 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
815 args->len, XFSA_FIXUP_BNO_OK);
816 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
818 goto error0;
819 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
822 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000825 trace_xfs_alloc_exact_done(args);
826 return 0;
827
828not_found:
829 /* Didn't find it, return null. */
830 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
831 args->agbno = NULLAGBLOCK;
832 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
834
835error0:
836 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000837 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return error;
839}
840
841/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000842 * Search the btree in a given direction via the search cursor and compare
843 * the records found against the good extent we've already found.
844 */
845STATIC int
846xfs_alloc_find_best_extent(
847 struct xfs_alloc_arg *args, /* allocation argument structure */
848 struct xfs_btree_cur **gcur, /* good cursor */
849 struct xfs_btree_cur **scur, /* searching cursor */
850 xfs_agblock_t gdiff, /* difference for search comparison */
851 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000852 xfs_extlen_t *slen, /* extent length */
853 xfs_agblock_t *sbnoa, /* aligned extent found by search */
854 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000855 int dir) /* 0 = search right, 1 = search left */
856{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000857 xfs_agblock_t new;
858 xfs_agblock_t sdiff;
859 int error;
860 int i;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800861 unsigned busy_gen;
Christoph Hellwig489a1502010-12-10 15:04:11 +0000862
863 /* The good extent is perfect, no need to search. */
864 if (!gdiff)
865 goto out_use_good;
866
867 /*
868 * Look until we find a better one, run out of space or run off the end.
869 */
870 do {
871 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
872 if (error)
873 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100874 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -0800875 xfs_alloc_compute_aligned(args, *sbno, *slen,
876 sbnoa, slena, &busy_gen);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000877
878 /*
879 * The good extent is closer than this one.
880 */
881 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000882 if (*sbnoa > args->max_agbno)
883 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000884 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000885 goto out_use_good;
886 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000887 if (*sbnoa < args->min_agbno)
888 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000889 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000890 goto out_use_good;
891 }
892
893 /*
894 * Same distance, compare length and pick the best.
895 */
896 if (*slena >= args->minlen) {
897 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
898 xfs_alloc_fix_len(args);
899
900 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200901 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000902 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000903 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000904
905 /*
906 * Choose closer size and invalidate other cursor.
907 */
908 if (sdiff < gdiff)
909 goto out_use_search;
910 goto out_use_good;
911 }
912
913 if (!dir)
914 error = xfs_btree_increment(*scur, 0, &i);
915 else
916 error = xfs_btree_decrement(*scur, 0, &i);
917 if (error)
918 goto error0;
919 } while (i);
920
921out_use_good:
922 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
923 *scur = NULL;
924 return 0;
925
926out_use_search:
927 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
928 *gcur = NULL;
929 return 0;
930
931error0:
932 /* caller invalidates cursors */
933 return error;
934}
935
936/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * Allocate a variable extent near bno in the allocation group agno.
938 * Extent's length (returned in len) will be between minlen and maxlen,
939 * and of the form k * prod + mod unless there's nothing that large.
940 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
941 */
942STATIC int /* error */
943xfs_alloc_ag_vextent_near(
944 xfs_alloc_arg_t *args) /* allocation argument structure */
945{
946 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
947 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
948 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 xfs_agblock_t gtbno; /* start bno of right side entry */
950 xfs_agblock_t gtbnoa; /* aligned ... */
951 xfs_extlen_t gtdiff; /* difference to right side entry */
952 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000953 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xfs_agblock_t gtnew; /* useful start bno of right side */
955 int error; /* error code */
956 int i; /* result code, temporary */
957 int j; /* result code, temporary */
958 xfs_agblock_t ltbno; /* start bno of left side entry */
959 xfs_agblock_t ltbnoa; /* aligned ... */
960 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000962 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_agblock_t ltnew; /* useful start bno of left side */
964 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800965 bool busy;
966 unsigned busy_gen;
Dave Chinner63d20d62013-08-12 20:49:50 +1000967#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
969 * Randomly don't execute the first algorithm.
970 */
971 int dofirst; /* set to do first algorithm */
972
Akinobu Mitaecb34032013-03-04 21:58:20 +0900973 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000975
Brian Fosterbfe46d42015-05-29 08:53:00 +1000976 /* handle unitialized agbno range so caller doesn't have to */
977 if (!args->min_agbno && !args->max_agbno)
978 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
979 ASSERT(args->min_agbno <= args->max_agbno);
980
981 /* clamp agbno to the range if it's outside */
982 if (args->agbno < args->min_agbno)
983 args->agbno = args->min_agbno;
984 if (args->agbno > args->max_agbno)
985 args->agbno = args->max_agbno;
986
Christoph Hellwige26f0502011-04-24 19:06:15 +0000987restart:
988 bno_cur_lt = NULL;
989 bno_cur_gt = NULL;
990 ltlen = 0;
991 gtlena = 0;
992 ltlena = 0;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800993 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * Get a cursor for the by-size btree.
997 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100998 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
999 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * See if there are any free extents as big as maxlen.
1003 */
1004 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1005 goto error0;
1006 /*
1007 * If none, then pick up the last entry in the tree unless the
1008 * tree is empty.
1009 */
1010 if (!i) {
1011 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1012 &ltlen, &i)))
1013 goto error0;
1014 if (i == 0 || ltlen == 0) {
1015 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001016 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018 }
1019 ASSERT(i == 1);
1020 }
1021 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * First algorithm.
1025 * If the requested extent is large wrt the freespaces available
1026 * in this a.g., then the cursor will be pointing to a btree entry
1027 * near the right edge of the tree. If it's in the last btree leaf
1028 * block, then we just examine all the entries in that block
1029 * that are big enough, and pick the best one.
1030 * This is written as a while loop so we can break out of it,
1031 * but we never loop back to the top.
1032 */
1033 while (xfs_btree_islastblock(cnt_cur, 0)) {
1034 xfs_extlen_t bdiff;
1035 int besti=0;
1036 xfs_extlen_t blen=0;
1037 xfs_agblock_t bnew=0;
1038
Dave Chinner63d20d62013-08-12 20:49:50 +10001039#ifdef DEBUG
1040 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042#endif
1043 /*
1044 * Start from the entry that lookup found, sequence through
1045 * all larger free blocks. If we're actually pointing at a
1046 * record smaller than maxlen, go to the start of this block,
1047 * and skip all those smaller than minlen.
1048 */
1049 if (ltlen || args->alignment > 1) {
1050 cnt_cur->bc_ptrs[0] = 1;
1051 do {
1052 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1053 &ltlen, &i)))
1054 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001055 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (ltlen >= args->minlen)
1057 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001058 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto error0;
1060 } while (i);
1061 ASSERT(ltlen >= args->minlen);
1062 if (!i)
1063 break;
1064 }
1065 i = cnt_cur->bc_ptrs[0];
1066 for (j = 1, blen = 0, bdiff = 0;
1067 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001068 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * For each entry, decide if it's better than
1071 * the previous best entry.
1072 */
1073 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1074 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001075 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001076 busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
1077 &ltbnoa, &ltlena, &busy_gen);
David Chinnere6430032008-04-17 16:49:49 +10001078 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001080 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1081 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1083 xfs_alloc_fix_len(args);
1084 ASSERT(args->len >= args->minlen);
1085 if (args->len < blen)
1086 continue;
1087 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001088 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001089 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (ltnew != NULLAGBLOCK &&
1091 (args->len > blen || ltdiff < bdiff)) {
1092 bdiff = ltdiff;
1093 bnew = ltnew;
1094 blen = args->len;
1095 besti = cnt_cur->bc_ptrs[0];
1096 }
1097 }
1098 /*
1099 * It didn't work. We COULD be in a case where
1100 * there's a good record somewhere, so try again.
1101 */
1102 if (blen == 0)
1103 break;
1104 /*
1105 * Point at the best entry, and retrieve it again.
1106 */
1107 cnt_cur->bc_ptrs[0] = besti;
1108 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1109 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001110 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001111 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 args->len = blen;
Christoph Hellwig54fee132017-01-09 13:44:30 -08001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * We are allocating starting at bnew for blen blocks.
1116 */
1117 args->agbno = bnew;
1118 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001119 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Set up a cursor for the by-bno tree.
1122 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001123 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1124 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * Fix up the btree entries.
1127 */
1128 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1129 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1130 goto error0;
1131 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1132 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001133
1134 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 0;
1136 }
1137 /*
1138 * Second algorithm.
1139 * Search in the by-bno tree to the left and to the right
1140 * simultaneously, until in each case we find a space big enough,
1141 * or run into the edge of the tree. When we run into the edge,
1142 * we deallocate that cursor.
1143 * If both searches succeed, we compare the two spaces and pick
1144 * the better one.
1145 * With alignment, it's possible for both to fail; the upper
1146 * level algorithm that picks allocation groups for allocations
1147 * is not supposed to do this.
1148 */
1149 /*
1150 * Allocate and initialize the cursor for the leftward search.
1151 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001152 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1153 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * Lookup <= bno to find the leftward search's starting point.
1156 */
1157 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1158 goto error0;
1159 if (!i) {
1160 /*
1161 * Didn't find anything; use this cursor for the rightward
1162 * search.
1163 */
1164 bno_cur_gt = bno_cur_lt;
1165 bno_cur_lt = NULL;
1166 }
1167 /*
1168 * Found something. Duplicate the cursor for the rightward search.
1169 */
1170 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1171 goto error0;
1172 /*
1173 * Increment the cursor, so we will point at the entry just right
1174 * of the leftward entry if any, or to the leftmost entry.
1175 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001176 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto error0;
1178 if (!i) {
1179 /*
1180 * It failed, there are no rightward entries.
1181 */
1182 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1183 bno_cur_gt = NULL;
1184 }
1185 /*
1186 * Loop going left with the leftward cursor, right with the
1187 * rightward cursor, until either both directions give up or
1188 * we find an entry at least as big as minlen.
1189 */
1190 do {
1191 if (bno_cur_lt) {
1192 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1193 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001194 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001195 busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
1196 &ltbnoa, &ltlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001197 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001199 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001201 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfs_btree_del_cursor(bno_cur_lt,
1203 XFS_BTREE_NOERROR);
1204 bno_cur_lt = NULL;
1205 }
1206 }
1207 if (bno_cur_gt) {
1208 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1209 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001210 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001211 busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
1212 &gtbnoa, &gtlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001213 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001215 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001217 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 xfs_btree_del_cursor(bno_cur_gt,
1219 XFS_BTREE_NOERROR);
1220 bno_cur_gt = NULL;
1221 }
1222 }
1223 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Got both cursors still active, need to find better entry.
1227 */
1228 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ltlena >= args->minlen) {
1230 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001231 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1234 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001235 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001236 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001237 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001238
1239 error = xfs_alloc_find_best_extent(args,
1240 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001241 ltdiff, &gtbno, &gtlen,
1242 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001243 0 /* search right */);
1244 } else {
1245 ASSERT(gtlena >= args->minlen);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001248 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1251 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001252 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001253 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001254 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001255
1256 error = xfs_alloc_find_best_extent(args,
1257 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001258 gtdiff, &ltbno, &ltlen,
1259 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001260 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001262
1263 if (error)
1264 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * If we couldn't get anything, give up.
1269 */
1270 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001271 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1272
Christoph Hellwigebf55872017-02-07 14:06:57 -08001273 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001274 trace_xfs_alloc_near_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001275 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001276 goto restart;
1277 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001278 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 args->agbno = NULLAGBLOCK;
1280 return 0;
1281 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /*
1284 * At this point we have selected a freespace entry, either to the
1285 * left or to the right. If it's on the right, copy all the
1286 * useful variables to the "left" set so we only have one
1287 * copy of this code.
1288 */
1289 if (bno_cur_gt) {
1290 bno_cur_lt = bno_cur_gt;
1291 bno_cur_gt = NULL;
1292 ltbno = gtbno;
1293 ltbnoa = gtbnoa;
1294 ltlen = gtlen;
1295 ltlena = gtlena;
1296 j = 1;
1297 } else
1298 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /*
1301 * Fix up the length and compute the useful address.
1302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1304 xfs_alloc_fix_len(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001306 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001307 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001309 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001310 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001311 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1315 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1316 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001317
1318 if (j)
1319 trace_xfs_alloc_near_greater(args);
1320 else
1321 trace_xfs_alloc_near_lesser(args);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1324 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1325 return 0;
1326
1327 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001328 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (cnt_cur != NULL)
1330 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1331 if (bno_cur_lt != NULL)
1332 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1333 if (bno_cur_gt != NULL)
1334 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1335 return error;
1336}
1337
1338/*
1339 * Allocate a variable extent anywhere in the allocation group agno.
1340 * Extent's length (returned in len) will be between minlen and maxlen,
1341 * and of the form k * prod + mod unless there's nothing that large.
1342 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1343 */
1344STATIC int /* error */
1345xfs_alloc_ag_vextent_size(
1346 xfs_alloc_arg_t *args) /* allocation argument structure */
1347{
1348 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1349 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1350 int error; /* error result */
1351 xfs_agblock_t fbno; /* start of found freespace */
1352 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int i; /* temp status variable */
1354 xfs_agblock_t rbno; /* returned block number */
1355 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001356 bool busy;
1357 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Christoph Hellwige26f0502011-04-24 19:06:15 +00001359restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * Allocate and initialize a cursor for the by-size btree.
1362 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001363 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1364 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 bno_cur = NULL;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001366 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * Look for an entry >= maxlen+alignment-1 blocks.
1370 */
1371 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1372 args->maxlen + args->alignment - 1, &i)))
1373 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
Christoph Hellwigebf55872017-02-07 14:06:57 -08001376 * If none then we have to settle for a smaller extent. In the case that
1377 * there are no large extents, this will return the last entry in the
1378 * tree unless the tree is empty. In the case that there are only busy
1379 * large extents, this will return the largest small extent unless there
Christoph Hellwige26f0502011-04-24 19:06:15 +00001380 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001382 if (!i) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001383 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1384 &fbno, &flen, &i);
1385 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 goto error0;
1387 if (i == 0 || flen == 0) {
1388 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001389 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
1391 }
1392 ASSERT(i == 1);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001393 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1394 &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001395 } else {
1396 /*
1397 * Search for a non-busy extent that is large enough.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001398 */
1399 for (;;) {
1400 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1401 if (error)
1402 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001403 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001404
Christoph Hellwigebf55872017-02-07 14:06:57 -08001405 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1406 &rbno, &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001407
1408 if (rlen >= args->maxlen)
1409 break;
1410
1411 error = xfs_btree_increment(cnt_cur, 0, &i);
1412 if (error)
1413 goto error0;
1414 if (i == 0) {
1415 /*
1416 * Our only valid extents must have been busy.
1417 * Make it unbusy by forcing the log out and
Christoph Hellwigebf55872017-02-07 14:06:57 -08001418 * retrying.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001419 */
1420 xfs_btree_del_cursor(cnt_cur,
1421 XFS_BTREE_NOERROR);
1422 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001423 xfs_extent_busy_flush(args->mp,
1424 args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001425 goto restart;
1426 }
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * In the first case above, we got the last entry in the
1432 * by-size btree. Now we check to see if the space hits maxlen
1433 * once aligned; if not, we search left for something better.
1434 * This can't happen in the second case above.
1435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001437 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1439 if (rlen < args->maxlen) {
1440 xfs_agblock_t bestfbno;
1441 xfs_extlen_t bestflen;
1442 xfs_agblock_t bestrbno;
1443 xfs_extlen_t bestrlen;
1444
1445 bestrlen = rlen;
1446 bestrbno = rbno;
1447 bestflen = flen;
1448 bestfbno = fbno;
1449 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001450 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto error0;
1452 if (i == 0)
1453 break;
1454 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1455 &i)))
1456 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001457 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (flen < bestrlen)
1459 break;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001460 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1461 &rbno, &rlen, &busy_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001463 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 (rlen <= flen && rbno + rlen <= fbno + flen),
1465 error0);
1466 if (rlen > bestrlen) {
1467 bestrlen = rlen;
1468 bestrbno = rbno;
1469 bestflen = flen;
1470 bestfbno = fbno;
1471 if (rlen == args->maxlen)
1472 break;
1473 }
1474 }
1475 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1476 &i)))
1477 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001478 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 rlen = bestrlen;
1480 rbno = bestrbno;
1481 flen = bestflen;
1482 fbno = bestfbno;
1483 }
1484 args->wasfromfl = 0;
1485 /*
1486 * Fix up the length.
1487 */
1488 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001489 if (rlen < args->minlen) {
Christoph Hellwigebf55872017-02-07 14:06:57 -08001490 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001491 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1492 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001493 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001494 goto restart;
1495 }
1496 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001498 xfs_alloc_fix_len(args);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001501 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
1503 * Allocate and initialize a cursor for the by-block tree.
1504 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001505 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1506 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1508 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1509 goto error0;
1510 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1511 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1512 cnt_cur = bno_cur = NULL;
1513 args->len = rlen;
1514 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001515 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001517 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001519 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521
1522error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001523 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (cnt_cur)
1525 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1526 if (bno_cur)
1527 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1528 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001529
1530out_nominleft:
1531 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1532 trace_xfs_alloc_size_nominleft(args);
1533 args->agbno = NULLAGBLOCK;
1534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537/*
1538 * Deal with the case where only small freespaces remain.
1539 * Either return the contents of the last freespace record,
1540 * or allocate space from the freelist if there is nothing in the tree.
1541 */
1542STATIC int /* error */
1543xfs_alloc_ag_vextent_small(
1544 xfs_alloc_arg_t *args, /* allocation argument structure */
1545 xfs_btree_cur_t *ccur, /* by-size cursor */
1546 xfs_agblock_t *fbnop, /* result block number */
1547 xfs_extlen_t *flenp, /* result length */
1548 int *stat) /* status: 0-freelist, 1-normal/none */
1549{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001550 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001551 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int error;
1553 xfs_agblock_t fbno;
1554 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int i;
1556
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001557 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 goto error0;
1559 if (i) {
1560 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1561 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001562 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 /*
1565 * Nothing in the btree, try the freelist. Make sure
1566 * to respect minleft even when pulling from the
1567 * freelist.
1568 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001569 else if (args->minlen == 1 && args->alignment == 1 &&
1570 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001571 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1572 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001573 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1574 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto error0;
1576 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001577 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001578 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001579
Dave Chinner292378e2016-09-26 08:21:28 +10001580 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 xfs_buf_t *bp;
1582
1583 bp = xfs_btree_get_bufs(args->mp, args->tp,
1584 args->agno, fbno, 0);
Eric Sandeen93e8bef2017-10-09 21:08:06 -07001585 if (!bp) {
1586 error = -EFSCORRUPTED;
1587 goto error0;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 xfs_trans_binval(args->tp, bp);
1590 }
1591 args->len = 1;
1592 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001593 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001595 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 error0);
1597 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001598 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001599
1600 /*
1601 * If we're feeding an AGFL block to something that
1602 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001603 * out the OWN_AG rmap and add the block back to
1604 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001605 */
1606 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1607 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1608 fbno, 1, &oinfo);
1609 if (error)
1610 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001611 pag = xfs_perag_get(args->mp, args->agno);
1612 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1613 args->tp, 1);
1614 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 *stat = 0;
1617 return 0;
1618 }
1619 /*
1620 * Nothing in the freelist.
1621 */
1622 else
1623 flen = 0;
1624 }
1625 /*
1626 * Can't allocate from the freelist for some reason.
1627 */
Nathan Scottd432c802006-09-28 11:03:44 +10001628 else {
1629 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * Can't do the allocation, give up.
1634 */
1635 if (flen < args->minlen) {
1636 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001637 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 flen = 0;
1639 }
1640 *fbnop = fbno;
1641 *flenp = flen;
1642 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001643 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645
1646error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001647 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return error;
1649}
1650
1651/*
1652 * Free the extent starting at agno/bno for length.
1653 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001654STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001656 xfs_trans_t *tp,
1657 xfs_buf_t *agbp,
1658 xfs_agnumber_t agno,
1659 xfs_agblock_t bno,
1660 xfs_extlen_t len,
1661 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001662 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1665 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1666 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 xfs_agblock_t gtbno; /* start of right neighbor block */
1668 xfs_extlen_t gtlen; /* length of right neighbor block */
1669 int haveleft; /* have a left neighbor block */
1670 int haveright; /* have a right neighbor block */
1671 int i; /* temp, result code */
1672 xfs_agblock_t ltbno; /* start of left neighbor block */
1673 xfs_extlen_t ltlen; /* length of left neighbor block */
1674 xfs_mount_t *mp; /* mount point struct for filesystem */
1675 xfs_agblock_t nbno; /* new starting block of freespace */
1676 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001677 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Darrick J. Wong673930c2016-08-03 11:33:43 +10001679 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001681
Darrick J. Wong33df3a92017-12-07 19:07:27 -08001682 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
Darrick J. Wong673930c2016-08-03 11:33:43 +10001683 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1684 if (error)
1685 goto error0;
1686 }
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Allocate and initialize a cursor for the by-block btree.
1690 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001691 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Look for a neighboring block on the left (lower block numbers)
1694 * that is contiguous with this space.
1695 */
1696 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1697 goto error0;
1698 if (haveleft) {
1699 /*
1700 * There is a block to our left.
1701 */
1702 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1703 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001704 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * It's not contiguous, though.
1707 */
1708 if (ltbno + ltlen < bno)
1709 haveleft = 0;
1710 else {
1711 /*
1712 * If this failure happens the request to free this
1713 * space was invalid, it's (partly) already free.
1714 * Very bad.
1715 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001716 XFS_WANT_CORRUPTED_GOTO(mp,
1717 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 }
1720 /*
1721 * Look for a neighboring block on the right (higher block numbers)
1722 * that is contiguous with this space.
1723 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001724 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto error0;
1726 if (haveright) {
1727 /*
1728 * There is a block to our right.
1729 */
1730 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1731 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001732 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /*
1734 * It's not contiguous, though.
1735 */
1736 if (bno + len < gtbno)
1737 haveright = 0;
1738 else {
1739 /*
1740 * If this failure happens the request to free this
1741 * space was invalid, it's (partly) already free.
1742 * Very bad.
1743 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001744 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 }
1747 /*
1748 * Now allocate and initialize a cursor for the by-size tree.
1749 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001750 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Have both left and right contiguous neighbors.
1753 * Merge all three into a single free block.
1754 */
1755 if (haveleft && haveright) {
1756 /*
1757 * Delete the old by-size entry on the left.
1758 */
1759 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1760 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001762 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001764 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /*
1766 * Delete the old by-size entry on the right.
1767 */
1768 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1769 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001770 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001771 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001773 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /*
1775 * Delete the old by-block entry for the right block.
1776 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001777 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * Move the by-block cursor back to the left neighbor.
1782 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001783 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786#ifdef DEBUG
1787 /*
1788 * Check that this is the right record: delete didn't
1789 * mangle the cursor.
1790 */
1791 {
1792 xfs_agblock_t xxbno;
1793 xfs_extlen_t xxlen;
1794
1795 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1796 &i)))
1797 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001798 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 i == 1 && xxbno == ltbno && xxlen == ltlen,
1800 error0);
1801 }
1802#endif
1803 /*
1804 * Update remaining by-block entry to the new, joined block.
1805 */
1806 nbno = ltbno;
1807 nlen = len + ltlen + gtlen;
1808 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1809 goto error0;
1810 }
1811 /*
1812 * Have only a left contiguous neighbor.
1813 * Merge it together with the new freespace.
1814 */
1815 else if (haveleft) {
1816 /*
1817 * Delete the old by-size entry on the left.
1818 */
1819 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1820 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001821 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001822 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Back up the by-block cursor to the left neighbor, and
1827 * update its length.
1828 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001829 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001831 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 nbno = ltbno;
1833 nlen = len + ltlen;
1834 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1835 goto error0;
1836 }
1837 /*
1838 * Have only a right contiguous neighbor.
1839 * Merge it together with the new freespace.
1840 */
1841 else if (haveright) {
1842 /*
1843 * Delete the old by-size entry on the right.
1844 */
1845 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1846 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001848 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
1852 * Update the starting block and length of the right
1853 * neighbor in the by-block tree.
1854 */
1855 nbno = bno;
1856 nlen = len + gtlen;
1857 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1858 goto error0;
1859 }
1860 /*
1861 * No contiguous neighbors.
1862 * Insert the new freespace into the by-block tree.
1863 */
1864 else {
1865 nbno = bno;
1866 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001867 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001869 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1872 bno_cur = NULL;
1873 /*
1874 * In all cases we need to insert the new freespace in the by-size tree.
1875 */
1876 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1877 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001879 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001881 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1883 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * Update the freespace totals in the ag and superblock.
1887 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001888 pag = xfs_perag_get(mp, agno);
1889 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001890 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001891 xfs_perag_put(pag);
1892 if (error)
1893 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001895 XFS_STATS_INC(mp, xs_freex);
1896 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001897
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001898 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1899 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902
1903 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001904 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1905 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (bno_cur)
1907 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1908 if (cnt_cur)
1909 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1910 return error;
1911}
1912
1913/*
1914 * Visible (exported) allocation/free functions.
1915 * Some of these are used just by xfs_alloc_btree.c and this file.
1916 */
1917
1918/*
1919 * Compute and fill in value of m_ag_maxlevels.
1920 */
1921void
1922xfs_alloc_compute_maxlevels(
1923 xfs_mount_t *mp) /* file system mount structure */
1924{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001925 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1926 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001930 * Find the length of the longest extent in an AG. The 'need' parameter
1931 * specifies how much space we're going to need for the AGFL and the
1932 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1933 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001934 */
1935xfs_extlen_t
1936xfs_alloc_longest_free_extent(
1937 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001938 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001939 xfs_extlen_t need,
1940 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001941{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001942 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001943
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001944 /*
1945 * If the AGFL needs a recharge, we'll have to subtract that from the
1946 * longest extent.
1947 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001948 if (need > pag->pagf_flcount)
1949 delta = need - pag->pagf_flcount;
1950
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001951 /*
1952 * If we cannot maintain others' reservations with space from the
1953 * not-longest freesp extents, we'll have to subtract /that/ from
1954 * the longest extent too.
1955 */
1956 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1957 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1958
1959 /*
1960 * If the longest extent is long enough to satisfy all the
1961 * reservations and AGFL rules in place, we can return this extent.
1962 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001963 if (pag->pagf_longest > delta)
1964 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001965
1966 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01001967 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1968}
1969
Dave Chinner496817b2015-06-22 10:13:30 +10001970unsigned int
1971xfs_alloc_min_freelist(
1972 struct xfs_mount *mp,
1973 struct xfs_perag *pag)
1974{
1975 unsigned int min_free;
1976
1977 /* space needed by-bno freespace btree */
1978 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1979 mp->m_ag_maxlevels);
1980 /* space needed by-size freespace btree */
1981 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1982 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10001983 /* space needed reverse mapping used space btree */
1984 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1985 min_free += min_t(unsigned int,
1986 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
1987 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10001988
1989 return min_free;
1990}
1991
Dave Chinner6cc87642009-03-16 08:29:46 +01001992/*
Dave Chinner72d55282015-06-22 10:04:42 +10001993 * Check if the operation we are fixing up the freelist for should go ahead or
1994 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1995 * is dependent on whether the size and shape of free space available will
1996 * permit the requested allocation to take place.
1997 */
1998static bool
1999xfs_alloc_space_available(
2000 struct xfs_alloc_arg *args,
2001 xfs_extlen_t min_free,
2002 int flags)
2003{
2004 struct xfs_perag *pag = args->pag;
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002005 xfs_extlen_t alloc_len, longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002006 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002007 int available;
2008
2009 if (flags & XFS_ALLOC_FLAG_FREEING)
2010 return true;
2011
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002012 reservation = xfs_ag_resv_needed(pag, args->resv);
2013
Dave Chinner72d55282015-06-22 10:04:42 +10002014 /* do we have enough contiguous free space for the allocation? */
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002015 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002016 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2017 reservation);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002018 if (longest < alloc_len)
Dave Chinner72d55282015-06-22 10:04:42 +10002019 return false;
2020
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002021 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002022 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Christoph Hellwig54fee132017-01-09 13:44:30 -08002023 reservation - min_free - args->minleft);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002024 if (available < (int)max(args->total, alloc_len))
Dave Chinner72d55282015-06-22 10:04:42 +10002025 return false;
2026
Christoph Hellwig54fee132017-01-09 13:44:30 -08002027 /*
2028 * Clamp maxlen to the amount of free space available for the actual
2029 * extent allocation.
2030 */
2031 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2032 args->maxlen = available;
2033 ASSERT(args->maxlen > 0);
2034 ASSERT(args->maxlen >= args->minlen);
2035 }
2036
Dave Chinner72d55282015-06-22 10:04:42 +10002037 return true;
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040/*
2041 * Decide whether to use this allocation group for this allocation.
2042 * If so, fix up the btree freelist's size.
2043 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002044int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002046 struct xfs_alloc_arg *args, /* allocation argument structure */
2047 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Dave Chinner396503f2015-06-22 10:13:19 +10002049 struct xfs_mount *mp = args->mp;
2050 struct xfs_perag *pag = args->pag;
2051 struct xfs_trans *tp = args->tp;
2052 struct xfs_buf *agbp = NULL;
2053 struct xfs_buf *agflbp = NULL;
2054 struct xfs_alloc_arg targs; /* local allocation arguments */
2055 xfs_agblock_t bno; /* freelist block */
2056 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002057 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002060 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2061 if (error)
2062 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002064 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2065 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002066 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Dave Chinner396503f2015-06-22 10:13:19 +10002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002070 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002071 * If this is a metadata preferred pag and we are user data then try
2072 * somewhere else if we are not being asked to try harder at this
2073 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Dave Chinner292378e2016-09-26 08:21:28 +10002075 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002076 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2077 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002078 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080
Dave Chinner496817b2015-06-22 10:13:30 +10002081 need = xfs_alloc_min_freelist(mp, pag);
Christoph Hellwig54fee132017-01-09 13:44:30 -08002082 if (!xfs_alloc_space_available(args, need, flags |
2083 XFS_ALLOC_FLAG_CHECK))
Dave Chinner396503f2015-06-22 10:13:19 +10002084 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Get the a.g. freespace buffer.
2088 * Can fail if we're not blocking on locks, and it's held.
2089 */
Dave Chinner396503f2015-06-22 10:13:19 +10002090 if (!agbp) {
2091 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2092 if (error)
2093 goto out_no_agbp;
2094 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002095 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2096 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002097 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002100
Dave Chinner50adbcb2015-06-22 10:04:31 +10002101 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002102 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002103 if (!xfs_alloc_space_available(args, need, flags))
2104 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002108 *
Dave Chinner396503f2015-06-22 10:13:19 +10002109 * Note that from this point onwards, we will always release the agf and
2110 * agfl buffers on error. This handles the case where we error out and
2111 * the buffers are clean or may not have been joined to the transaction
2112 * and hence need to be released manually. If they have been joined to
2113 * the transaction, then xfs_trans_brelse() will handle them
2114 * appropriately based on the recursion count and dirty state of the
2115 * buffer.
2116 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002117 * XXX (dgc): When we have lots of free space, does this buy us
2118 * anything other than extra overhead when we need to put more blocks
2119 * back on the free list? Maybe we should only do this when space is
2120 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002121 *
2122 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2123 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2124 * updating the rmapbt. Both flags are used in xfs_repair while we're
2125 * rebuilding the rmapbt, and neither are used by the kernel. They're
2126 * both required to ensure that rmaps are correctly recorded for the
2127 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2128 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002130 memset(&targs, 0, sizeof(targs));
2131 if (flags & XFS_ALLOC_FLAG_NORMAP)
2132 xfs_rmap_skip_owner_update(&targs.oinfo);
2133 else
2134 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2135 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002136 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
David Chinner92821e22007-05-24 15:26:31 +10002138 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2139 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002140 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002141 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002142 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002143 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002144 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
Eric Sandeen93e8bef2017-10-09 21:08:06 -07002146 if (!bp) {
2147 error = -EFSCORRUPTED;
2148 goto out_agbp_relse;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_trans_binval(tp, bp);
2151 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 targs.tp = tp;
2154 targs.mp = mp;
2155 targs.agbp = agbp;
2156 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002157 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 targs.type = XFS_ALLOCTYPE_THIS_AG;
2159 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002160 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2161 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002162 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002163
2164 /* Make the freelist longer if it's too short. */
2165 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002167 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002168 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002169
2170 /* Allocate as many blocks as possible at once. */
2171 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002172 if (error)
2173 goto out_agflbp_relse;
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * Stop if we run out. Won't happen if callers are obeying
2177 * the restrictions correctly. Can happen for free calls
2178 * on a completely full ag.
2179 */
Yingping Lud210a282006-06-09 14:55:18 +10002180 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002181 if (flags & XFS_ALLOC_FLAG_FREEING)
2182 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002183 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * Put each allocated block on the list.
2187 */
2188 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002189 error = xfs_alloc_put_freelist(tp, agbp,
2190 agflbp, bno, 0);
2191 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002192 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002195 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 args->agbp = agbp;
2197 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002198
2199out_agflbp_relse:
2200 xfs_trans_brelse(tp, agflbp);
2201out_agbp_relse:
2202 if (agbp)
2203 xfs_trans_brelse(tp, agbp);
2204out_no_agbp:
2205 args->agbp = NULL;
2206 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
2209/*
2210 * Get a block from the freelist.
2211 * Returns with the buffer for the block gotten.
2212 */
2213int /* error */
2214xfs_alloc_get_freelist(
2215 xfs_trans_t *tp, /* transaction pointer */
2216 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002217 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2218 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2222 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002223 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 int error;
David Chinner92821e22007-05-24 15:26:31 +10002225 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002226 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 xfs_perag_t *pag; /* per allocation group data */
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /*
2230 * Freelist is empty, give up.
2231 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002232 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!agf->agf_flcount) {
2234 *bnop = NULLAGBLOCK;
2235 return 0;
2236 }
2237 /*
2238 * Read the array of free blocks.
2239 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002240 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2241 &agflbp);
2242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002244
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /*
2247 * Get the block number and update the data structures.
2248 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002249 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2250 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002251 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002253 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002255
2256 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002257 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xfs_trans_agflist_delta(tp, -1);
2259 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002260 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002261
2262 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2263 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002264 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002265 pag->pagf_btreeblks++;
2266 logflags |= XFS_AGF_BTREEBLKS;
2267 }
2268
David Chinner92821e22007-05-24 15:26:31 +10002269 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 *bnop = bno;
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return 0;
2273}
2274
2275/*
2276 * Log the given fields from the agf structure.
2277 */
2278void
2279xfs_alloc_log_agf(
2280 xfs_trans_t *tp, /* transaction pointer */
2281 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2282 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2283{
2284 int first; /* first byte offset */
2285 int last; /* last byte offset */
2286 static const short offsets[] = {
2287 offsetof(xfs_agf_t, agf_magicnum),
2288 offsetof(xfs_agf_t, agf_versionnum),
2289 offsetof(xfs_agf_t, agf_seqno),
2290 offsetof(xfs_agf_t, agf_length),
2291 offsetof(xfs_agf_t, agf_roots[0]),
2292 offsetof(xfs_agf_t, agf_levels[0]),
2293 offsetof(xfs_agf_t, agf_flfirst),
2294 offsetof(xfs_agf_t, agf_fllast),
2295 offsetof(xfs_agf_t, agf_flcount),
2296 offsetof(xfs_agf_t, agf_freeblks),
2297 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002298 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002299 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002300 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002301 offsetof(xfs_agf_t, agf_refcount_blocks),
2302 offsetof(xfs_agf_t, agf_refcount_root),
2303 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002304 /* needed so that we don't log the whole rest of the structure: */
2305 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 sizeof(xfs_agf_t)
2307 };
2308
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002309 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2310
Dave Chinner61fe1352013-04-03 16:11:30 +11002311 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2314 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2315}
2316
2317/*
2318 * Interface for inode allocation to force the pag data to be initialized.
2319 */
2320int /* error */
2321xfs_alloc_pagf_init(
2322 xfs_mount_t *mp, /* file system mount structure */
2323 xfs_trans_t *tp, /* transaction pointer */
2324 xfs_agnumber_t agno, /* allocation group number */
2325 int flags) /* XFS_ALLOC_FLAGS_... */
2326{
2327 xfs_buf_t *bp;
2328 int error;
2329
2330 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2331 return error;
2332 if (bp)
2333 xfs_trans_brelse(tp, bp);
2334 return 0;
2335}
2336
2337/*
2338 * Put the block on the freelist for the allocation group.
2339 */
2340int /* error */
2341xfs_alloc_put_freelist(
2342 xfs_trans_t *tp, /* transaction pointer */
2343 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2344 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002345 xfs_agblock_t bno, /* block being freed */
2346 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002349 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int error;
David Chinner92821e22007-05-24 15:26:31 +10002351 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 xfs_mount_t *mp; /* mount structure */
2353 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002354 __be32 *agfl_bno;
2355 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 agf = XFS_BUF_TO_AGF(agbp);
2358 mp = tp->t_mountp;
2359
2360 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002361 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002363 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002364 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002366
2367 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002368 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 xfs_trans_agflist_delta(tp, 1);
2370 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002371
2372 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2373 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002374 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002375 pag->pagf_btreeblks--;
2376 logflags |= XFS_AGF_BTREEBLKS;
2377 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002378 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002379
David Chinner92821e22007-05-24 15:26:31 +10002380 xfs_alloc_log_agf(tp, agbp, logflags);
2381
Christoph Hellwig16259e72005-11-02 15:11:25 +11002382 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002383
2384 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2385 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002386 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002387 startoff = (char *)blockp - (char *)agflbp->b_addr;
2388
David Chinner92821e22007-05-24 15:26:31 +10002389 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002390
Dave Chinner61fe1352013-04-03 16:11:30 +11002391 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002392 xfs_trans_log_buf(tp, agflbp, startoff,
2393 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return 0;
2395}
2396
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002397static xfs_failaddr_t
Dave Chinner612cfbf2012-11-14 17:52:32 +11002398xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002399 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002400 struct xfs_buf *bp)
2401 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002402 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002403
Brian Fostera45086e2015-10-12 15:59:25 +11002404 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2405 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002406 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +11002407 if (!xfs_log_check_lsn(mp,
2408 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002409 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +11002410 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002411
Dave Chinner4e0e6042013-04-03 16:11:13 +11002412 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2413 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2414 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2415 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2416 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2417 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002418 return __this_address;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002419
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002420 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2421 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2422 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002423 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002424 return __this_address;
Eric Sandeene1b05722014-09-09 11:47:24 +10002425
Darrick J. Wongb8704942016-08-03 11:30:32 +10002426 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002427 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2428 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002429 return __this_address;
Darrick J. Wongb8704942016-08-03 11:30:32 +10002430
Dave Chinner5d5f5272012-11-14 17:44:56 +11002431 /*
2432 * during growfs operations, the perag is not fully initialised,
2433 * so we can't use it for any useful checking. growfs ensures we can't
2434 * use it by using uncached buffers that don't have the perag attached
2435 * so we can detect and avoid this problem.
2436 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002437 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002438 return __this_address;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002439
Dave Chinner4e0e6042013-04-03 16:11:13 +11002440 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2441 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002442 return __this_address;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002443
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002444 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002445 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2446 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002447 return __this_address;
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002448
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002449 return NULL;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002450
Dave Chinner612cfbf2012-11-14 17:52:32 +11002451}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002452
Dave Chinner1813dd62012-11-14 17:54:40 +11002453static void
2454xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002455 struct xfs_buf *bp)
2456{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002457 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002458
Eric Sandeence5028c2014-02-27 15:23:10 +11002459 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2460 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002461 xfs_verifier_error(bp, -EFSBADCRC);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002462 else if (XFS_TEST_ERROR(xfs_agf_verify(mp, bp), mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07002463 XFS_ERRTAG_ALLOC_READ_AGF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002464 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002465}
2466
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002467static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002468xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002469 struct xfs_buf *bp)
2470{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002471 struct xfs_mount *mp = bp->b_target->bt_mount;
2472 struct xfs_buf_log_item *bip = bp->b_fspriv;
2473
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002474 if (xfs_agf_verify(mp, bp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08002475 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002476 return;
2477 }
2478
2479 if (!xfs_sb_version_hascrc(&mp->m_sb))
2480 return;
2481
2482 if (bip)
2483 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2484
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002485 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002486}
2487
Dave Chinner1813dd62012-11-14 17:54:40 +11002488const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002489 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002490 .verify_read = xfs_agf_read_verify,
2491 .verify_write = xfs_agf_write_verify,
2492};
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/*
2495 * Read in the allocation group header (free/alloc section).
2496 */
2497int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002498xfs_read_agf(
2499 struct xfs_mount *mp, /* mount point structure */
2500 struct xfs_trans *tp, /* transaction pointer */
2501 xfs_agnumber_t agno, /* allocation group number */
2502 int flags, /* XFS_BUF_ */
2503 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int error;
2506
Dave Chinnerd1230312013-11-01 15:27:19 +11002507 trace_xfs_read_agf(mp, agno);
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 ASSERT(agno != NULLAGNUMBER);
2510 error = xfs_trans_read_buf(
2511 mp, tp, mp->m_ddev_targp,
2512 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002513 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (error)
2515 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002516 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002518
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002519 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002520 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002521 return 0;
2522}
2523
2524/*
2525 * Read in the allocation group header (free/alloc section).
2526 */
2527int /* error */
2528xfs_alloc_read_agf(
2529 struct xfs_mount *mp, /* mount point structure */
2530 struct xfs_trans *tp, /* transaction pointer */
2531 xfs_agnumber_t agno, /* allocation group number */
2532 int flags, /* XFS_ALLOC_FLAG_... */
2533 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2534{
2535 struct xfs_agf *agf; /* ag freelist header */
2536 struct xfs_perag *pag; /* per allocation group data */
2537 int error;
2538
Dave Chinnerd1230312013-11-01 15:27:19 +11002539 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002540
Dave Chinnerd1230312013-11-01 15:27:19 +11002541 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002542 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002543 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002544 bpp);
2545 if (error)
2546 return error;
2547 if (!*bpp)
2548 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002549 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002550
2551 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002552 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002554 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002555 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002556 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2557 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002559 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002561 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002562 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2563 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002564 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002565 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002566 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002567 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 pag->pagf_init = 1;
2569 }
2570#ifdef DEBUG
2571 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002572 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002573 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002574 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2575 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002577 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002579 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002582 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584}
2585
2586/*
2587 * Allocate an extent (variable-size).
2588 * Depending on the allocation type, we either look in a single allocation
2589 * group or loop over the allocation groups to find the result.
2590 */
2591int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002592xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 xfs_alloc_arg_t *args) /* allocation argument structure */
2594{
2595 xfs_agblock_t agsize; /* allocation group size */
2596 int error;
2597 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 xfs_mount_t *mp; /* mount structure pointer */
2599 xfs_agnumber_t sagno; /* starting allocation group number */
2600 xfs_alloctype_t type; /* input allocation type */
2601 int bump_rotor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2603
2604 mp = args->mp;
2605 type = args->otype = args->type;
2606 args->agbno = NULLAGBLOCK;
2607 /*
2608 * Just fix this up, for the case where the last a.g. is shorter
2609 * (or there's only one a.g.) and the caller couldn't easily figure
2610 * that out (xfs_bmap_alloc).
2611 */
2612 agsize = mp->m_sb.sb_agblocks;
2613 if (args->maxlen > agsize)
2614 args->maxlen = agsize;
2615 if (args->alignment == 0)
2616 args->alignment = 1;
2617 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2618 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2619 ASSERT(args->minlen <= args->maxlen);
2620 ASSERT(args->minlen <= agsize);
2621 ASSERT(args->mod < args->prod);
2622 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2623 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2624 args->minlen > args->maxlen || args->minlen > agsize ||
2625 args->mod >= args->prod) {
2626 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002627 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return 0;
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 switch (type) {
2632 case XFS_ALLOCTYPE_THIS_AG:
2633 case XFS_ALLOCTYPE_NEAR_BNO:
2634 case XFS_ALLOCTYPE_THIS_BNO:
2635 /*
2636 * These three force us into a single a.g.
2637 */
2638 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002639 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 error = xfs_alloc_fix_freelist(args, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002642 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 goto error0;
2644 }
2645 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002646 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 break;
2648 }
2649 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2650 if ((error = xfs_alloc_ag_vextent(args)))
2651 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 break;
2653 case XFS_ALLOCTYPE_START_BNO:
2654 /*
2655 * Try near allocation first, then anywhere-in-ag after
2656 * the first a.g. fails.
2657 */
Dave Chinner292378e2016-09-26 08:21:28 +10002658 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2660 args->fsbno = XFS_AGB_TO_FSB(mp,
2661 ((mp->m_agfrotor / rotorstep) %
2662 mp->m_sb.sb_agcount), 0);
2663 bump_rotor = 1;
2664 }
2665 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2666 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2667 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 case XFS_ALLOCTYPE_FIRST_AG:
2669 /*
2670 * Rotate through the allocation groups looking for a winner.
2671 */
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002672 if (type == XFS_ALLOCTYPE_FIRST_AG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
2674 * Start with allocation group given by bno.
2675 */
2676 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2677 args->type = XFS_ALLOCTYPE_THIS_AG;
2678 sagno = 0;
2679 flags = 0;
2680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /*
2682 * Start with the given allocation group.
2683 */
2684 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2685 flags = XFS_ALLOC_FLAG_TRYLOCK;
2686 }
2687 /*
2688 * Loop over allocation groups twice; first time with
2689 * trylock set, second time without.
2690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002692 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 error = xfs_alloc_fix_freelist(args, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002695 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 goto error0;
2697 }
2698 /*
2699 * If we get a buffer back then the allocation will fly.
2700 */
2701 if (args->agbp) {
2702 if ((error = xfs_alloc_ag_vextent(args)))
2703 goto error0;
2704 break;
2705 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002706
2707 trace_xfs_alloc_vextent_loopfailed(args);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /*
2710 * Didn't work, figure out the next iteration.
2711 */
2712 if (args->agno == sagno &&
2713 type == XFS_ALLOCTYPE_START_BNO)
2714 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002715 /*
2716 * For the first allocation, we can try any AG to get
2717 * space. However, if we already have allocated a
2718 * block, we don't want to try AGs whose number is below
2719 * sagno. Otherwise, we may end up with out-of-order
2720 * locking of AGF, which might cause deadlock.
2721 */
2722 if (++(args->agno) == mp->m_sb.sb_agcount) {
2723 if (args->firstblock != NULLFSBLOCK)
2724 args->agno = sagno;
2725 else
2726 args->agno = 0;
2727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /*
2729 * Reached the starting a.g., must either be done
2730 * or switch to non-trylock mode.
2731 */
2732 if (args->agno == sagno) {
Christoph Hellwig255c5162017-01-09 13:36:19 -08002733 if (flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002735 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 break;
2737 }
Christoph Hellwig255c5162017-01-09 13:36:19 -08002738
2739 flags = 0;
2740 if (type == XFS_ALLOCTYPE_START_BNO) {
2741 args->agbno = XFS_FSB_TO_AGBNO(mp,
2742 args->fsbno);
2743 args->type = XFS_ALLOCTYPE_NEAR_BNO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002746 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002748 if (bump_rotor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (args->agno == sagno)
2750 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2751 (mp->m_sb.sb_agcount * rotorstep);
2752 else
2753 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2754 (mp->m_sb.sb_agcount * rotorstep);
2755 }
2756 break;
2757 default:
2758 ASSERT(0);
2759 /* NOTREACHED */
2760 }
2761 if (args->agbno == NULLAGBLOCK)
2762 args->fsbno = NULLFSBLOCK;
2763 else {
2764 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2765#ifdef DEBUG
2766 ASSERT(args->len >= args->minlen);
2767 ASSERT(args->len <= args->maxlen);
2768 ASSERT(args->agbno % args->alignment == 0);
2769 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2770 args->len);
2771#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002772
2773 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002774 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002775 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2776 if (error)
2777 goto error0;
2778 }
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002781 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002784 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return error;
2786}
2787
Dave Chinner4d89e202016-06-21 11:53:28 +10002788/* Ensure that the freelist is at full capacity. */
2789int
2790xfs_free_extent_fix_freelist(
2791 struct xfs_trans *tp,
2792 xfs_agnumber_t agno,
2793 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Dave Chinner4d89e202016-06-21 11:53:28 +10002795 struct xfs_alloc_arg args;
2796 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Dave Chinner4d89e202016-06-21 11:53:28 +10002798 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 args.tp = tp;
2800 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002801 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002802
2803 /*
2804 * validate that the block number is legal - the enables us to detect
2805 * and handle a silent filesystem corruption rather than crashing.
2806 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002807 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002808 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002809
Dave Chinnera862e0f2010-01-11 11:47:41 +00002810 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002811 ASSERT(args.pag);
2812
2813 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2814 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002815 goto out;
2816
2817 *agbp = args.agbp;
2818out:
2819 xfs_perag_put(args.pag);
2820 return error;
2821}
2822
2823/*
2824 * Free an extent.
2825 * Just break up the extent address and hand off to xfs_free_ag_extent
2826 * after fixing up the freelist.
2827 */
2828int /* error */
2829xfs_free_extent(
2830 struct xfs_trans *tp, /* transaction pointer */
2831 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002832 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002833 struct xfs_owner_info *oinfo, /* extent owner */
2834 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002835{
2836 struct xfs_mount *mp = tp->t_mountp;
2837 struct xfs_buf *agbp;
2838 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2839 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2840 int error;
2841
2842 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002843 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002844
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002845 if (XFS_TEST_ERROR(false, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07002846 XFS_ERRTAG_FREE_EXTENT))
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002847 return -EIO;
2848
Dave Chinner4d89e202016-06-21 11:53:28 +10002849 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2850 if (error)
2851 return error;
2852
2853 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002854
2855 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002856 XFS_WANT_CORRUPTED_GOTO(mp,
2857 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2858 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002859
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002860 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002861 if (error)
2862 goto err;
2863
2864 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2865 return 0;
2866
2867err:
2868 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return error;
2870}
Darrick J. Wong2d520bf2017-03-28 14:56:35 -07002871
2872struct xfs_alloc_query_range_info {
2873 xfs_alloc_query_range_fn fn;
2874 void *priv;
2875};
2876
2877/* Format btree record and pass to our callback. */
2878STATIC int
2879xfs_alloc_query_range_helper(
2880 struct xfs_btree_cur *cur,
2881 union xfs_btree_rec *rec,
2882 void *priv)
2883{
2884 struct xfs_alloc_query_range_info *query = priv;
2885 struct xfs_alloc_rec_incore irec;
2886
2887 irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
2888 irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
2889 return query->fn(cur, &irec, query->priv);
2890}
2891
2892/* Find all free space within a given range of blocks. */
2893int
2894xfs_alloc_query_range(
2895 struct xfs_btree_cur *cur,
2896 struct xfs_alloc_rec_incore *low_rec,
2897 struct xfs_alloc_rec_incore *high_rec,
2898 xfs_alloc_query_range_fn fn,
2899 void *priv)
2900{
2901 union xfs_btree_irec low_brec;
2902 union xfs_btree_irec high_brec;
2903 struct xfs_alloc_query_range_info query;
2904
2905 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
2906 low_brec.a = *low_rec;
2907 high_brec.a = *high_rec;
2908 query.priv = priv;
2909 query.fn = fn;
2910 return xfs_btree_query_range(cur, &low_brec, &high_brec,
2911 xfs_alloc_query_range_helper, &query);
2912}
Darrick J. Wonge9a25992017-03-28 14:56:35 -07002913
2914/* Find all free space records. */
2915int
2916xfs_alloc_query_all(
2917 struct xfs_btree_cur *cur,
2918 xfs_alloc_query_range_fn fn,
2919 void *priv)
2920{
2921 struct xfs_alloc_query_range_info query;
2922
2923 ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
2924 query.priv = priv;
2925 query.fn = fn;
2926 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
2927}
Darrick J. Wong21ec5412017-10-17 21:37:32 -07002928
2929/* Find the size of the AG, in blocks. */
2930xfs_agblock_t
2931xfs_ag_block_count(
2932 struct xfs_mount *mp,
2933 xfs_agnumber_t agno)
2934{
2935 ASSERT(agno < mp->m_sb.sb_agcount);
2936
2937 if (agno < mp->m_sb.sb_agcount - 1)
2938 return mp->m_sb.sb_agblocks;
2939 return mp->m_sb.sb_dblocks - (agno * mp->m_sb.sb_agblocks);
2940}
2941
2942/*
2943 * Verify that an AG block number pointer neither points outside the AG
2944 * nor points at static metadata.
2945 */
2946bool
2947xfs_verify_agbno(
2948 struct xfs_mount *mp,
2949 xfs_agnumber_t agno,
2950 xfs_agblock_t agbno)
2951{
2952 xfs_agblock_t eoag;
2953
2954 eoag = xfs_ag_block_count(mp, agno);
2955 if (agbno >= eoag)
2956 return false;
2957 if (agbno <= XFS_AGFL_BLOCK(mp))
2958 return false;
2959 return true;
2960}
2961
2962/*
2963 * Verify that an FS block number pointer neither points outside the
2964 * filesystem nor points at static AG metadata.
2965 */
2966bool
2967xfs_verify_fsbno(
2968 struct xfs_mount *mp,
2969 xfs_fsblock_t fsbno)
2970{
2971 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, fsbno);
2972
2973 if (agno >= mp->m_sb.sb_agcount)
2974 return false;
2975 return xfs_verify_agbno(mp, agno, XFS_FSB_TO_AGBNO(mp, fsbno));
2976}