blob: f65b8d49ec16120ea6e69da504f61a5da3f1708e [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110035#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110037#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110038#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110039#include "xfs_log.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100040#include "xfs_ag_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Chinnerc999a222012-03-22 05:15:07 +000042struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
45
46#define XFSA_FIXUP_BNO_OK 1
47#define XFSA_FIXUP_CNT_OK 2
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
52STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000053 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070055unsigned int
56xfs_refc_block(
57 struct xfs_mount *mp)
58{
59 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
60 return XFS_RMAP_BLOCK(mp) + 1;
61 if (xfs_sb_version_hasfinobt(&mp->m_sb))
62 return XFS_FIBT_BLOCK(mp) + 1;
63 return XFS_IBT_BLOCK(mp) + 1;
64}
65
Darrick J. Wong80180262016-08-03 11:31:47 +100066xfs_extlen_t
67xfs_prealloc_blocks(
68 struct xfs_mount *mp)
69{
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070070 if (xfs_sb_version_hasreflink(&mp->m_sb))
71 return xfs_refc_block(mp) + 1;
Darrick J. Wong80180262016-08-03 11:31:47 +100072 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
73 return XFS_RMAP_BLOCK(mp) + 1;
74 if (xfs_sb_version_hasfinobt(&mp->m_sb))
75 return XFS_FIBT_BLOCK(mp) + 1;
76 return XFS_IBT_BLOCK(mp) + 1;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
Darrick J. Wong52548852016-08-03 11:38:24 +100080 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
81 * AGF buffer (PV 947395), we place constraints on the relationship among
82 * actual allocations for data blocks, freelist blocks, and potential file data
83 * bmap btree blocks. However, these restrictions may result in no actual space
84 * allocated for a delayed extent, for example, a data block in a certain AG is
85 * allocated but there is no additional block for the additional bmap btree
86 * block due to a split of the bmap btree of the file. The result of this may
87 * lead to an infinite loop when the file gets flushed to disk and all delayed
88 * extents need to be actually allocated. To get around this, we explicitly set
89 * aside a few blocks which will not be reserved in delayed allocation.
90 *
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100091 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
92 * potential split of the file's bmap btree.
Darrick J. Wong52548852016-08-03 11:38:24 +100093 */
94unsigned int
95xfs_alloc_set_aside(
96 struct xfs_mount *mp)
97{
Christoph Hellwig5149fd32017-01-09 13:36:30 -080098 return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
Darrick J. Wong52548852016-08-03 11:38:24 +100099}
100
101/*
102 * When deciding how much space to allocate out of an AG, we limit the
103 * allocation maximum size to the size the AG. However, we cannot use all the
104 * blocks in the AG - some are permanently used by metadata. These
105 * blocks are generally:
106 * - the AG superblock, AGF, AGI and AGFL
107 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
108 * the AGI free inode and rmap btree root blocks.
109 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
110 * - the rmapbt root block
111 *
112 * The AG headers are sector sized, so the amount of space they take up is
113 * dependent on filesystem geometry. The others are all single blocks.
114 */
115unsigned int
116xfs_alloc_ag_max_usable(
117 struct xfs_mount *mp)
118{
119 unsigned int blocks;
120
121 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
122 blocks += XFS_ALLOC_AGFL_RESERVE;
123 blocks += 3; /* AGF, AGI btree root blocks */
124 if (xfs_sb_version_hasfinobt(&mp->m_sb))
125 blocks++; /* finobt root block */
126 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
127 blocks++; /* rmap root block */
Darrick J. Wongd0e853f2016-10-03 09:11:24 -0700128 if (xfs_sb_version_hasreflink(&mp->m_sb))
129 blocks++; /* refcount root block */
Darrick J. Wong52548852016-08-03 11:38:24 +1000130
131 return mp->m_sb.sb_agblocks - blocks;
132}
133
134/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100135 * Lookup the record equal to [bno, len] in the btree given by cur.
136 */
137STATIC int /* error */
138xfs_alloc_lookup_eq(
139 struct xfs_btree_cur *cur, /* btree cursor */
140 xfs_agblock_t bno, /* starting block of extent */
141 xfs_extlen_t len, /* length of extent */
142 int *stat) /* success/failure */
143{
144 cur->bc_rec.a.ar_startblock = bno;
145 cur->bc_rec.a.ar_blockcount = len;
146 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
147}
148
149/*
150 * Lookup the first record greater than or equal to [bno, len]
151 * in the btree given by cur.
152 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000153int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100154xfs_alloc_lookup_ge(
155 struct xfs_btree_cur *cur, /* btree cursor */
156 xfs_agblock_t bno, /* starting block of extent */
157 xfs_extlen_t len, /* length of extent */
158 int *stat) /* success/failure */
159{
160 cur->bc_rec.a.ar_startblock = bno;
161 cur->bc_rec.a.ar_blockcount = len;
162 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
163}
164
165/*
166 * Lookup the first record less than or equal to [bno, len]
167 * in the btree given by cur.
168 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000169static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100170xfs_alloc_lookup_le(
171 struct xfs_btree_cur *cur, /* btree cursor */
172 xfs_agblock_t bno, /* starting block of extent */
173 xfs_extlen_t len, /* length of extent */
174 int *stat) /* success/failure */
175{
176 cur->bc_rec.a.ar_startblock = bno;
177 cur->bc_rec.a.ar_blockcount = len;
178 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
179}
180
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100181/*
182 * Update the record referred to by cur to the value given
183 * by [bno, len].
184 * This either works (return 0) or gets an EFSCORRUPTED error.
185 */
186STATIC int /* error */
187xfs_alloc_update(
188 struct xfs_btree_cur *cur, /* btree cursor */
189 xfs_agblock_t bno, /* starting block of extent */
190 xfs_extlen_t len) /* length of extent */
191{
192 union xfs_btree_rec rec;
193
194 rec.alloc.ar_startblock = cpu_to_be32(bno);
195 rec.alloc.ar_blockcount = cpu_to_be32(len);
196 return xfs_btree_update(cur, &rec);
197}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100198
199/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100200 * Get the data from the pointed-to record.
201 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000202int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100203xfs_alloc_get_rec(
204 struct xfs_btree_cur *cur, /* btree cursor */
205 xfs_agblock_t *bno, /* output: starting block of extent */
206 xfs_extlen_t *len, /* output: length of extent */
207 int *stat) /* output: success/failure */
208{
209 union xfs_btree_rec *rec;
210 int error;
211
212 error = xfs_btree_get_rec(cur, &rec, stat);
213 if (!error && *stat == 1) {
214 *bno = be32_to_cpu(rec->alloc.ar_startblock);
215 *len = be32_to_cpu(rec->alloc.ar_blockcount);
216 }
217 return error;
218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Compute aligned version of the found extent.
222 * Takes alignment and min length into account.
223 */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800224STATIC bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000226 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 xfs_agblock_t foundbno, /* starting block in found extent */
228 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 xfs_agblock_t *resbno, /* result block number */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800230 xfs_extlen_t *reslen, /* result length */
231 unsigned *busy_gen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Christoph Hellwigebf55872017-02-07 14:06:57 -0800233 xfs_agblock_t bno = foundbno;
234 xfs_extlen_t len = foundlen;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000235 xfs_extlen_t diff;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800236 bool busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Christoph Hellwige26f0502011-04-24 19:06:15 +0000238 /* Trim busy sections out of found extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800239 busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000240
Brian Fosterbfe46d42015-05-29 08:53:00 +1000241 /*
242 * If we have a largish extent that happens to start before min_agbno,
243 * see if we can shift it into range...
244 */
245 if (bno < args->min_agbno && bno + len > args->min_agbno) {
246 diff = args->min_agbno - bno;
247 if (len > diff) {
248 bno += diff;
249 len -= diff;
250 }
251 }
252
Christoph Hellwige26f0502011-04-24 19:06:15 +0000253 if (args->alignment > 1 && len >= args->minlen) {
254 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000255
256 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000257
258 *resbno = aligned_bno;
259 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000261 *resbno = bno;
262 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Christoph Hellwigebf55872017-02-07 14:06:57 -0800264
265 return busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * Compute best start block and diff for "near" allocations.
270 * freelen >= wantlen already checked by caller.
271 */
272STATIC xfs_extlen_t /* difference value (absolute) */
273xfs_alloc_compute_diff(
274 xfs_agblock_t wantbno, /* target starting block */
275 xfs_extlen_t wantlen, /* target length */
276 xfs_extlen_t alignment, /* target alignment */
Dave Chinner292378e2016-09-26 08:21:28 +1000277 int datatype, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 xfs_agblock_t freebno, /* freespace's starting block */
279 xfs_extlen_t freelen, /* freespace's length */
280 xfs_agblock_t *newbnop) /* result: best start block from free */
281{
282 xfs_agblock_t freeend; /* end of freespace extent */
283 xfs_agblock_t newbno1; /* return block number */
284 xfs_agblock_t newbno2; /* other new block number */
285 xfs_extlen_t newlen1=0; /* length with newbno1 */
286 xfs_extlen_t newlen2=0; /* length with newbno2 */
287 xfs_agblock_t wantend; /* end of target extent */
Dave Chinner292378e2016-09-26 08:21:28 +1000288 bool userdata = xfs_alloc_is_userdata(datatype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 ASSERT(freelen >= wantlen);
291 freeend = freebno + freelen;
292 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200293 /*
294 * We want to allocate from the start of a free extent if it is past
295 * the desired block or if we are allocating user data and the free
296 * extent is before desired block. The second case is there to allow
297 * for contiguous allocation from the remaining free space if the file
298 * grows in the short term.
299 */
300 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
302 newbno1 = NULLAGBLOCK;
303 } else if (freeend >= wantend && alignment > 1) {
304 newbno1 = roundup(wantbno, alignment);
305 newbno2 = newbno1 - alignment;
306 if (newbno1 >= freeend)
307 newbno1 = NULLAGBLOCK;
308 else
309 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
310 if (newbno2 < freebno)
311 newbno2 = NULLAGBLOCK;
312 else
313 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
314 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
315 if (newlen1 < newlen2 ||
316 (newlen1 == newlen2 &&
317 XFS_ABSDIFF(newbno1, wantbno) >
318 XFS_ABSDIFF(newbno2, wantbno)))
319 newbno1 = newbno2;
320 } else if (newbno2 != NULLAGBLOCK)
321 newbno1 = newbno2;
322 } else if (freeend >= wantend) {
323 newbno1 = wantbno;
324 } else if (alignment > 1) {
325 newbno1 = roundup(freeend - wantlen, alignment);
326 if (newbno1 > freeend - wantlen &&
327 newbno1 - alignment >= freebno)
328 newbno1 -= alignment;
329 else if (newbno1 >= freeend)
330 newbno1 = NULLAGBLOCK;
331 } else
332 newbno1 = freeend - wantlen;
333 *newbnop = newbno1;
334 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
335}
336
337/*
338 * Fix up the length, based on mod and prod.
339 * len should be k * prod + mod for some k.
340 * If len is too small it is returned unchanged.
341 * If len hits maxlen it is left alone.
342 */
343STATIC void
344xfs_alloc_fix_len(
345 xfs_alloc_arg_t *args) /* allocation argument structure */
346{
347 xfs_extlen_t k;
348 xfs_extlen_t rlen;
349
350 ASSERT(args->mod < args->prod);
351 rlen = args->len;
352 ASSERT(rlen >= args->minlen);
353 ASSERT(rlen <= args->maxlen);
354 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
355 (args->mod == 0 && rlen < args->prod))
356 return;
357 k = rlen % args->prod;
358 if (k == args->mod)
359 return;
Jan Kara30265112014-06-06 16:06:37 +1000360 if (k > args->mod)
361 rlen = rlen - (k - args->mod);
362 else
363 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100364 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000365 if ((int)rlen < (int)args->minlen)
366 return;
367 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
368 ASSERT(rlen % args->prod == args->mod);
Christoph Hellwig54fee132017-01-09 13:44:30 -0800369 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
370 rlen + args->minleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 args->len = rlen;
372}
373
374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * Update the two btrees, logically removing from freespace the extent
376 * starting at rbno, rlen blocks. The extent is contained within the
377 * actual (current) free extent fbno for flen blocks.
378 * Flags are passed in indicating whether the cursors are set to the
379 * relevant records.
380 */
381STATIC int /* error code */
382xfs_alloc_fixup_trees(
383 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
384 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
385 xfs_agblock_t fbno, /* starting block of free extent */
386 xfs_extlen_t flen, /* length of free extent */
387 xfs_agblock_t rbno, /* starting block of returned extent */
388 xfs_extlen_t rlen, /* length of returned extent */
389 int flags) /* flags, XFSA_FIXUP_... */
390{
391 int error; /* error code */
392 int i; /* operation results */
393 xfs_agblock_t nfbno1; /* first new free startblock */
394 xfs_agblock_t nfbno2; /* second new free startblock */
395 xfs_extlen_t nflen1=0; /* first new free length */
396 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100397 struct xfs_mount *mp;
398
399 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * Look up the record in the by-size tree if necessary.
403 */
404 if (flags & XFSA_FIXUP_CNT_OK) {
405#ifdef DEBUG
406 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
407 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100408 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 i == 1 && nfbno1 == fbno && nflen1 == flen);
410#endif
411 } else {
412 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
413 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100414 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 /*
417 * Look up the record in the by-block tree if necessary.
418 */
419 if (flags & XFSA_FIXUP_BNO_OK) {
420#ifdef DEBUG
421 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
422 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100423 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 i == 1 && nfbno1 == fbno && nflen1 == flen);
425#endif
426 } else {
427 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
428 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100429 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100432#ifdef DEBUG
433 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
434 struct xfs_btree_block *bnoblock;
435 struct xfs_btree_block *cntblock;
436
437 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
438 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
439
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100440 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100441 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Deal with all four cases: the allocated record is contained
447 * within the freespace record, so we can have new freespace
448 * at either (or both) end, or no freespace remaining.
449 */
450 if (rbno == fbno && rlen == flen)
451 nfbno1 = nfbno2 = NULLAGBLOCK;
452 else if (rbno == fbno) {
453 nfbno1 = rbno + rlen;
454 nflen1 = flen - rlen;
455 nfbno2 = NULLAGBLOCK;
456 } else if (rbno + rlen == fbno + flen) {
457 nfbno1 = fbno;
458 nflen1 = flen - rlen;
459 nfbno2 = NULLAGBLOCK;
460 } else {
461 nfbno1 = fbno;
462 nflen1 = rbno - fbno;
463 nfbno2 = rbno + rlen;
464 nflen2 = (fbno + flen) - nfbno2;
465 }
466 /*
467 * Delete the entry from the by-size btree.
468 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100469 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100471 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Add new by-size btree entry(s).
474 */
475 if (nfbno1 != NULLAGBLOCK) {
476 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
477 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100478 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100479 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100481 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 if (nfbno2 != NULLAGBLOCK) {
484 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
485 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100486 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100487 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100489 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 /*
492 * Fix up the by-block btree entry(s).
493 */
494 if (nfbno1 == NULLAGBLOCK) {
495 /*
496 * No remaining freespace, just delete the by-block tree entry.
497 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100498 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100500 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } else {
502 /*
503 * Update the by-block entry to start later|be shorter.
504 */
505 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
506 return error;
507 }
508 if (nfbno2 != NULLAGBLOCK) {
509 /*
510 * 2 resulting free entries, need to add one.
511 */
512 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
513 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100514 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100515 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100517 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 return 0;
520}
521
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100522static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100523xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100524 struct xfs_buf *bp)
525{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100526 struct xfs_mount *mp = bp->b_target->bt_mount;
527 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100528 int i;
529
Eric Sandeence748ea2015-07-29 11:53:31 +1000530 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100531 return false;
532 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
533 return false;
534 /*
535 * during growfs operations, the perag is not fully initialised,
536 * so we can't use it for any useful checking. growfs ensures we can't
537 * use it by using uncached buffers that don't have the perag attached
538 * so we can detect and avoid this problem.
539 */
540 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
541 return false;
542
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100543 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100544 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100545 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100546 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100547 }
Brian Fostera45086e2015-10-12 15:59:25 +1100548
549 return xfs_log_check_lsn(mp,
550 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100551}
552
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100553static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100554xfs_agfl_read_verify(
555 struct xfs_buf *bp)
556{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100557 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100558
559 /*
560 * There is no verification of non-crc AGFLs because mkfs does not
561 * initialise the AGFL to zero or NULL. Hence the only valid part of the
562 * AGFL is what the AGF says is active. We can't get to the AGF, so we
563 * can't verify just those entries are valid.
564 */
565 if (!xfs_sb_version_hascrc(&mp->m_sb))
566 return;
567
Eric Sandeence5028c2014-02-27 15:23:10 +1100568 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000569 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100570 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000571 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100572
573 if (bp->b_error)
574 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100575}
576
577static void
578xfs_agfl_write_verify(
579 struct xfs_buf *bp)
580{
581 struct xfs_mount *mp = bp->b_target->bt_mount;
582 struct xfs_buf_log_item *bip = bp->b_fspriv;
583
584 /* no verification of non-crc AGFLs */
585 if (!xfs_sb_version_hascrc(&mp->m_sb))
586 return;
587
588 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000589 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100590 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100591 return;
592 }
593
594 if (bip)
595 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
596
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100597 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100598}
599
Dave Chinner1813dd62012-11-14 17:54:40 +1100600const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100601 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100602 .verify_read = xfs_agfl_read_verify,
603 .verify_write = xfs_agfl_write_verify,
604};
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * Read in the allocation group free block array.
608 */
609STATIC int /* error */
610xfs_alloc_read_agfl(
611 xfs_mount_t *mp, /* mount point structure */
612 xfs_trans_t *tp, /* transaction pointer */
613 xfs_agnumber_t agno, /* allocation group number */
614 xfs_buf_t **bpp) /* buffer for the ag free block array */
615{
616 xfs_buf_t *bp; /* return value */
617 int error;
618
619 ASSERT(agno != NULLAGNUMBER);
620 error = xfs_trans_read_buf(
621 mp, tp, mp->m_ddev_targp,
622 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100623 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (error)
625 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000626 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *bpp = bp;
628 return 0;
629}
630
Christoph Hellwigecb69282011-03-04 12:59:55 +0000631STATIC int
632xfs_alloc_update_counters(
633 struct xfs_trans *tp,
634 struct xfs_perag *pag,
635 struct xfs_buf *agbp,
636 long len)
637{
638 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
639
640 pag->pagf_freeblks += len;
641 be32_add_cpu(&agf->agf_freeblks, len);
642
643 xfs_trans_agblocks_delta(tp, len);
644 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
645 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000646 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000647
648 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
649 return 0;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/*
653 * Allocation group level functions.
654 */
655
656/*
657 * Allocate a variable extent in the allocation group agno.
658 * Type and bno are used to determine where in the allocation group the
659 * extent will start.
660 * Extent's length (returned in *len) will be between minlen and maxlen,
661 * and of the form k * prod + mod unless there's nothing that large.
662 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
663 */
664STATIC int /* error */
665xfs_alloc_ag_vextent(
666 xfs_alloc_arg_t *args) /* argument structure for allocation */
667{
668 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 ASSERT(args->minlen > 0);
671 ASSERT(args->maxlen > 0);
672 ASSERT(args->minlen <= args->maxlen);
673 ASSERT(args->mod < args->prod);
674 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000675
676 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * Branch to correct routine based on the type.
678 */
679 args->wasfromfl = 0;
680 switch (args->type) {
681 case XFS_ALLOCTYPE_THIS_AG:
682 error = xfs_alloc_ag_vextent_size(args);
683 break;
684 case XFS_ALLOCTYPE_NEAR_BNO:
685 error = xfs_alloc_ag_vextent_near(args);
686 break;
687 case XFS_ALLOCTYPE_THIS_BNO:
688 error = xfs_alloc_ag_vextent_exact(args);
689 break;
690 default:
691 ASSERT(0);
692 /* NOTREACHED */
693 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000694
695 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Christoph Hellwigecb69282011-03-04 12:59:55 +0000698 ASSERT(args->len >= args->minlen);
699 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000700 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000701 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Darrick J. Wong673930c2016-08-03 11:33:43 +1000703 /* if not file data, insert new block into the reverse map btree */
704 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
705 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
706 args->agbno, args->len, &args->oinfo);
707 if (error)
708 return error;
709 }
710
Christoph Hellwigecb69282011-03-04 12:59:55 +0000711 if (!args->wasfromfl) {
712 error = xfs_alloc_update_counters(args->tp, args->pag,
713 args->agbp,
714 -((long)(args->len)));
715 if (error)
716 return error;
717
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000718 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000719 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000721
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000722 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000723
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100724 XFS_STATS_INC(args->mp, xs_allocx);
725 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000726 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/*
730 * Allocate a variable extent at exactly agno/bno.
731 * Extent's length (returned in *len) will be between minlen and maxlen,
732 * and of the form k * prod + mod unless there's nothing that large.
733 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
734 */
735STATIC int /* error */
736xfs_alloc_ag_vextent_exact(
737 xfs_alloc_arg_t *args) /* allocation argument structure */
738{
739 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
740 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int error;
742 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800744 xfs_agblock_t tbno; /* start block of busy extent */
745 xfs_extlen_t tlen; /* length of busy extent */
746 xfs_agblock_t tend; /* end block of busy extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int i; /* success/failure of operation */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800748 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Allocate/initialize a cursor for the by-number freespace btree.
754 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100755 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000756 args->agno, XFS_BTNUM_BNO);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /*
759 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
760 * Look for the closest free block <= bno, it must contain bno
761 * if any free block does.
762 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000763 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
764 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000766 if (!i)
767 goto not_found;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * Grab the freespace record.
771 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000772 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100775 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000779 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800781 tbno = fbno;
782 tlen = flen;
783 xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000784
785 /*
786 * Give up if the start of the extent is busy, or the freespace isn't
787 * long enough for the minimum request.
788 */
789 if (tbno > args->agbno)
790 goto not_found;
791 if (tlen < args->minlen)
792 goto not_found;
793 tend = tbno + tlen;
794 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000795 goto not_found;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * End of extent will be smaller of the freespace end and the
799 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000800 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * Fix the length according to mod and prod if given.
802 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000803 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
804 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 xfs_alloc_fix_len(args);
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000806 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000809 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * Allocate/initialize a cursor for the by-size btree.
811 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100812 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
813 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100815 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000816 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
817 args->len, XFSA_FIXUP_BNO_OK);
818 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
820 goto error0;
821 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
824 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000827 trace_xfs_alloc_exact_done(args);
828 return 0;
829
830not_found:
831 /* Didn't find it, return null. */
832 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
833 args->agbno = NULLAGBLOCK;
834 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836
837error0:
838 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000839 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return error;
841}
842
843/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000844 * Search the btree in a given direction via the search cursor and compare
845 * the records found against the good extent we've already found.
846 */
847STATIC int
848xfs_alloc_find_best_extent(
849 struct xfs_alloc_arg *args, /* allocation argument structure */
850 struct xfs_btree_cur **gcur, /* good cursor */
851 struct xfs_btree_cur **scur, /* searching cursor */
852 xfs_agblock_t gdiff, /* difference for search comparison */
853 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000854 xfs_extlen_t *slen, /* extent length */
855 xfs_agblock_t *sbnoa, /* aligned extent found by search */
856 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000857 int dir) /* 0 = search right, 1 = search left */
858{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000859 xfs_agblock_t new;
860 xfs_agblock_t sdiff;
861 int error;
862 int i;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800863 unsigned busy_gen;
Christoph Hellwig489a1502010-12-10 15:04:11 +0000864
865 /* The good extent is perfect, no need to search. */
866 if (!gdiff)
867 goto out_use_good;
868
869 /*
870 * Look until we find a better one, run out of space or run off the end.
871 */
872 do {
873 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
874 if (error)
875 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100876 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -0800877 xfs_alloc_compute_aligned(args, *sbno, *slen,
878 sbnoa, slena, &busy_gen);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000879
880 /*
881 * The good extent is closer than this one.
882 */
883 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000884 if (*sbnoa > args->max_agbno)
885 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000886 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000887 goto out_use_good;
888 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000889 if (*sbnoa < args->min_agbno)
890 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000891 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000892 goto out_use_good;
893 }
894
895 /*
896 * Same distance, compare length and pick the best.
897 */
898 if (*slena >= args->minlen) {
899 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
900 xfs_alloc_fix_len(args);
901
902 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200903 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000904 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000905 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000906
907 /*
908 * Choose closer size and invalidate other cursor.
909 */
910 if (sdiff < gdiff)
911 goto out_use_search;
912 goto out_use_good;
913 }
914
915 if (!dir)
916 error = xfs_btree_increment(*scur, 0, &i);
917 else
918 error = xfs_btree_decrement(*scur, 0, &i);
919 if (error)
920 goto error0;
921 } while (i);
922
923out_use_good:
924 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
925 *scur = NULL;
926 return 0;
927
928out_use_search:
929 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
930 *gcur = NULL;
931 return 0;
932
933error0:
934 /* caller invalidates cursors */
935 return error;
936}
937
938/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * Allocate a variable extent near bno in the allocation group agno.
940 * Extent's length (returned in len) will be between minlen and maxlen,
941 * and of the form k * prod + mod unless there's nothing that large.
942 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
943 */
944STATIC int /* error */
945xfs_alloc_ag_vextent_near(
946 xfs_alloc_arg_t *args) /* allocation argument structure */
947{
948 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
949 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
950 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 xfs_agblock_t gtbno; /* start bno of right side entry */
952 xfs_agblock_t gtbnoa; /* aligned ... */
953 xfs_extlen_t gtdiff; /* difference to right side entry */
954 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000955 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_agblock_t gtnew; /* useful start bno of right side */
957 int error; /* error code */
958 int i; /* result code, temporary */
959 int j; /* result code, temporary */
960 xfs_agblock_t ltbno; /* start bno of left side entry */
961 xfs_agblock_t ltbnoa; /* aligned ... */
962 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000964 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 xfs_agblock_t ltnew; /* useful start bno of left side */
966 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -0800967 bool busy;
968 unsigned busy_gen;
Dave Chinner63d20d62013-08-12 20:49:50 +1000969#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * Randomly don't execute the first algorithm.
972 */
973 int dofirst; /* set to do first algorithm */
974
Akinobu Mitaecb34032013-03-04 21:58:20 +0900975 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000977
Brian Fosterbfe46d42015-05-29 08:53:00 +1000978 /* handle unitialized agbno range so caller doesn't have to */
979 if (!args->min_agbno && !args->max_agbno)
980 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
981 ASSERT(args->min_agbno <= args->max_agbno);
982
983 /* clamp agbno to the range if it's outside */
984 if (args->agbno < args->min_agbno)
985 args->agbno = args->min_agbno;
986 if (args->agbno > args->max_agbno)
987 args->agbno = args->max_agbno;
988
Christoph Hellwige26f0502011-04-24 19:06:15 +0000989restart:
990 bno_cur_lt = NULL;
991 bno_cur_gt = NULL;
992 ltlen = 0;
993 gtlena = 0;
994 ltlena = 0;
Christoph Hellwigebf55872017-02-07 14:06:57 -0800995 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * Get a cursor for the by-size btree.
999 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001000 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1001 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * See if there are any free extents as big as maxlen.
1005 */
1006 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1007 goto error0;
1008 /*
1009 * If none, then pick up the last entry in the tree unless the
1010 * tree is empty.
1011 */
1012 if (!i) {
1013 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1014 &ltlen, &i)))
1015 goto error0;
1016 if (i == 0 || ltlen == 0) {
1017 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001018 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020 }
1021 ASSERT(i == 1);
1022 }
1023 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * First algorithm.
1027 * If the requested extent is large wrt the freespaces available
1028 * in this a.g., then the cursor will be pointing to a btree entry
1029 * near the right edge of the tree. If it's in the last btree leaf
1030 * block, then we just examine all the entries in that block
1031 * that are big enough, and pick the best one.
1032 * This is written as a while loop so we can break out of it,
1033 * but we never loop back to the top.
1034 */
1035 while (xfs_btree_islastblock(cnt_cur, 0)) {
1036 xfs_extlen_t bdiff;
1037 int besti=0;
1038 xfs_extlen_t blen=0;
1039 xfs_agblock_t bnew=0;
1040
Dave Chinner63d20d62013-08-12 20:49:50 +10001041#ifdef DEBUG
1042 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044#endif
1045 /*
1046 * Start from the entry that lookup found, sequence through
1047 * all larger free blocks. If we're actually pointing at a
1048 * record smaller than maxlen, go to the start of this block,
1049 * and skip all those smaller than minlen.
1050 */
1051 if (ltlen || args->alignment > 1) {
1052 cnt_cur->bc_ptrs[0] = 1;
1053 do {
1054 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1055 &ltlen, &i)))
1056 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001057 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (ltlen >= args->minlen)
1059 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001060 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto error0;
1062 } while (i);
1063 ASSERT(ltlen >= args->minlen);
1064 if (!i)
1065 break;
1066 }
1067 i = cnt_cur->bc_ptrs[0];
1068 for (j = 1, blen = 0, bdiff = 0;
1069 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001070 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
1072 * For each entry, decide if it's better than
1073 * the previous best entry.
1074 */
1075 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1076 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001077 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001078 busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
1079 &ltbnoa, &ltlena, &busy_gen);
David Chinnere6430032008-04-17 16:49:49 +10001080 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001082 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1083 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1085 xfs_alloc_fix_len(args);
1086 ASSERT(args->len >= args->minlen);
1087 if (args->len < blen)
1088 continue;
1089 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001090 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001091 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (ltnew != NULLAGBLOCK &&
1093 (args->len > blen || ltdiff < bdiff)) {
1094 bdiff = ltdiff;
1095 bnew = ltnew;
1096 blen = args->len;
1097 besti = cnt_cur->bc_ptrs[0];
1098 }
1099 }
1100 /*
1101 * It didn't work. We COULD be in a case where
1102 * there's a good record somewhere, so try again.
1103 */
1104 if (blen == 0)
1105 break;
1106 /*
1107 * Point at the best entry, and retrieve it again.
1108 */
1109 cnt_cur->bc_ptrs[0] = besti;
1110 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1111 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001112 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001113 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 args->len = blen;
Christoph Hellwig54fee132017-01-09 13:44:30 -08001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * We are allocating starting at bnew for blen blocks.
1118 */
1119 args->agbno = bnew;
1120 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001121 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * Set up a cursor for the by-bno tree.
1124 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001125 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1126 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Fix up the btree entries.
1129 */
1130 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1131 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1132 goto error0;
1133 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1134 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001135
1136 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
1138 }
1139 /*
1140 * Second algorithm.
1141 * Search in the by-bno tree to the left and to the right
1142 * simultaneously, until in each case we find a space big enough,
1143 * or run into the edge of the tree. When we run into the edge,
1144 * we deallocate that cursor.
1145 * If both searches succeed, we compare the two spaces and pick
1146 * the better one.
1147 * With alignment, it's possible for both to fail; the upper
1148 * level algorithm that picks allocation groups for allocations
1149 * is not supposed to do this.
1150 */
1151 /*
1152 * Allocate and initialize the cursor for the leftward search.
1153 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001154 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1155 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Lookup <= bno to find the leftward search's starting point.
1158 */
1159 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1160 goto error0;
1161 if (!i) {
1162 /*
1163 * Didn't find anything; use this cursor for the rightward
1164 * search.
1165 */
1166 bno_cur_gt = bno_cur_lt;
1167 bno_cur_lt = NULL;
1168 }
1169 /*
1170 * Found something. Duplicate the cursor for the rightward search.
1171 */
1172 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1173 goto error0;
1174 /*
1175 * Increment the cursor, so we will point at the entry just right
1176 * of the leftward entry if any, or to the leftmost entry.
1177 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001178 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto error0;
1180 if (!i) {
1181 /*
1182 * It failed, there are no rightward entries.
1183 */
1184 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1185 bno_cur_gt = NULL;
1186 }
1187 /*
1188 * Loop going left with the leftward cursor, right with the
1189 * rightward cursor, until either both directions give up or
1190 * we find an entry at least as big as minlen.
1191 */
1192 do {
1193 if (bno_cur_lt) {
1194 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1195 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001196 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001197 busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
1198 &ltbnoa, &ltlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001199 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001201 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001203 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 xfs_btree_del_cursor(bno_cur_lt,
1205 XFS_BTREE_NOERROR);
1206 bno_cur_lt = NULL;
1207 }
1208 }
1209 if (bno_cur_gt) {
1210 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1211 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001212 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001213 busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
1214 &gtbnoa, &gtlena, &busy_gen);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001215 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001217 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001219 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_btree_del_cursor(bno_cur_gt,
1221 XFS_BTREE_NOERROR);
1222 bno_cur_gt = NULL;
1223 }
1224 }
1225 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Got both cursors still active, need to find better entry.
1229 */
1230 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (ltlena >= args->minlen) {
1232 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001233 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
1235 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1236 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001237 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001238 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001239 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001240
1241 error = xfs_alloc_find_best_extent(args,
1242 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001243 ltdiff, &gtbno, &gtlen,
1244 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001245 0 /* search right */);
1246 } else {
1247 ASSERT(gtlena >= args->minlen);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001250 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
1252 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1253 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001254 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001255 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001256 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001257
1258 error = xfs_alloc_find_best_extent(args,
1259 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001260 gtdiff, &ltbno, &ltlen,
1261 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001262 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001264
1265 if (error)
1266 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * If we couldn't get anything, give up.
1271 */
1272 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001273 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1274
Christoph Hellwigebf55872017-02-07 14:06:57 -08001275 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001276 trace_xfs_alloc_near_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001277 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001278 goto restart;
1279 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001280 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 args->agbno = NULLAGBLOCK;
1282 return 0;
1283 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
1286 * At this point we have selected a freespace entry, either to the
1287 * left or to the right. If it's on the right, copy all the
1288 * useful variables to the "left" set so we only have one
1289 * copy of this code.
1290 */
1291 if (bno_cur_gt) {
1292 bno_cur_lt = bno_cur_gt;
1293 bno_cur_gt = NULL;
1294 ltbno = gtbno;
1295 ltbnoa = gtbnoa;
1296 ltlen = gtlen;
1297 ltlena = gtlena;
1298 j = 1;
1299 } else
1300 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Fix up the length and compute the useful address.
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1306 xfs_alloc_fix_len(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001308 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001309 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001311 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001312 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001313 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1317 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1318 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001319
1320 if (j)
1321 trace_xfs_alloc_near_greater(args);
1322 else
1323 trace_xfs_alloc_near_lesser(args);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1326 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1327 return 0;
1328
1329 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001330 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (cnt_cur != NULL)
1332 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1333 if (bno_cur_lt != NULL)
1334 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1335 if (bno_cur_gt != NULL)
1336 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1337 return error;
1338}
1339
1340/*
1341 * Allocate a variable extent anywhere in the allocation group agno.
1342 * Extent's length (returned in len) will be between minlen and maxlen,
1343 * and of the form k * prod + mod unless there's nothing that large.
1344 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1345 */
1346STATIC int /* error */
1347xfs_alloc_ag_vextent_size(
1348 xfs_alloc_arg_t *args) /* allocation argument structure */
1349{
1350 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1351 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1352 int error; /* error result */
1353 xfs_agblock_t fbno; /* start of found freespace */
1354 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int i; /* temp status variable */
1356 xfs_agblock_t rbno; /* returned block number */
1357 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001358 bool busy;
1359 unsigned busy_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Christoph Hellwige26f0502011-04-24 19:06:15 +00001361restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
1363 * Allocate and initialize a cursor for the by-size btree.
1364 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001365 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1366 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 bno_cur = NULL;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001368 busy = false;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * Look for an entry >= maxlen+alignment-1 blocks.
1372 */
1373 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1374 args->maxlen + args->alignment - 1, &i)))
1375 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
Christoph Hellwigebf55872017-02-07 14:06:57 -08001378 * If none then we have to settle for a smaller extent. In the case that
1379 * there are no large extents, this will return the last entry in the
1380 * tree unless the tree is empty. In the case that there are only busy
1381 * large extents, this will return the largest small extent unless there
Christoph Hellwige26f0502011-04-24 19:06:15 +00001382 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Christoph Hellwigebf55872017-02-07 14:06:57 -08001384 if (!i) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001385 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1386 &fbno, &flen, &i);
1387 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto error0;
1389 if (i == 0 || flen == 0) {
1390 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001391 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 0;
1393 }
1394 ASSERT(i == 1);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001395 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1396 &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001397 } else {
1398 /*
1399 * Search for a non-busy extent that is large enough.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001400 */
1401 for (;;) {
1402 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1403 if (error)
1404 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001405 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001406
Christoph Hellwigebf55872017-02-07 14:06:57 -08001407 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1408 &rbno, &rlen, &busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001409
1410 if (rlen >= args->maxlen)
1411 break;
1412
1413 error = xfs_btree_increment(cnt_cur, 0, &i);
1414 if (error)
1415 goto error0;
1416 if (i == 0) {
1417 /*
1418 * Our only valid extents must have been busy.
1419 * Make it unbusy by forcing the log out and
Christoph Hellwigebf55872017-02-07 14:06:57 -08001420 * retrying.
Christoph Hellwige26f0502011-04-24 19:06:15 +00001421 */
1422 xfs_btree_del_cursor(cnt_cur,
1423 XFS_BTREE_NOERROR);
1424 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001425 xfs_extent_busy_flush(args->mp,
1426 args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001427 goto restart;
1428 }
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * In the first case above, we got the last entry in the
1434 * by-size btree. Now we check to see if the space hits maxlen
1435 * once aligned; if not, we search left for something better.
1436 * This can't happen in the second case above.
1437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001439 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1441 if (rlen < args->maxlen) {
1442 xfs_agblock_t bestfbno;
1443 xfs_extlen_t bestflen;
1444 xfs_agblock_t bestrbno;
1445 xfs_extlen_t bestrlen;
1446
1447 bestrlen = rlen;
1448 bestrbno = rbno;
1449 bestflen = flen;
1450 bestfbno = fbno;
1451 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001452 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto error0;
1454 if (i == 0)
1455 break;
1456 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1457 &i)))
1458 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001459 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (flen < bestrlen)
1461 break;
Christoph Hellwigebf55872017-02-07 14:06:57 -08001462 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1463 &rbno, &rlen, &busy_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001465 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 (rlen <= flen && rbno + rlen <= fbno + flen),
1467 error0);
1468 if (rlen > bestrlen) {
1469 bestrlen = rlen;
1470 bestrbno = rbno;
1471 bestflen = flen;
1472 bestfbno = fbno;
1473 if (rlen == args->maxlen)
1474 break;
1475 }
1476 }
1477 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1478 &i)))
1479 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001480 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 rlen = bestrlen;
1482 rbno = bestrbno;
1483 flen = bestflen;
1484 fbno = bestfbno;
1485 }
1486 args->wasfromfl = 0;
1487 /*
1488 * Fix up the length.
1489 */
1490 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001491 if (rlen < args->minlen) {
Christoph Hellwigebf55872017-02-07 14:06:57 -08001492 if (busy) {
Christoph Hellwige26f0502011-04-24 19:06:15 +00001493 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1494 trace_xfs_alloc_size_busy(args);
Christoph Hellwigebf55872017-02-07 14:06:57 -08001495 xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001496 goto restart;
1497 }
1498 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001500 xfs_alloc_fix_len(args);
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001503 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /*
1505 * Allocate and initialize a cursor for the by-block tree.
1506 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001507 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1508 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1510 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1511 goto error0;
1512 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1513 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1514 cnt_cur = bno_cur = NULL;
1515 args->len = rlen;
1516 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001517 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001519 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001521 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 0;
1523
1524error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001525 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (cnt_cur)
1527 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1528 if (bno_cur)
1529 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1530 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001531
1532out_nominleft:
1533 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1534 trace_xfs_alloc_size_nominleft(args);
1535 args->agbno = NULLAGBLOCK;
1536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
1540 * Deal with the case where only small freespaces remain.
1541 * Either return the contents of the last freespace record,
1542 * or allocate space from the freelist if there is nothing in the tree.
1543 */
1544STATIC int /* error */
1545xfs_alloc_ag_vextent_small(
1546 xfs_alloc_arg_t *args, /* allocation argument structure */
1547 xfs_btree_cur_t *ccur, /* by-size cursor */
1548 xfs_agblock_t *fbnop, /* result block number */
1549 xfs_extlen_t *flenp, /* result length */
1550 int *stat) /* status: 0-freelist, 1-normal/none */
1551{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001552 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001553 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int error;
1555 xfs_agblock_t fbno;
1556 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 int i;
1558
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001559 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto error0;
1561 if (i) {
1562 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1563 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001564 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566 /*
1567 * Nothing in the btree, try the freelist. Make sure
1568 * to respect minleft even when pulling from the
1569 * freelist.
1570 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001571 else if (args->minlen == 1 && args->alignment == 1 &&
1572 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001573 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1574 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001575 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto error0;
1578 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001579 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001580 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001581
Dave Chinner292378e2016-09-26 08:21:28 +10001582 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 xfs_buf_t *bp;
1584
1585 bp = xfs_btree_get_bufs(args->mp, args->tp,
1586 args->agno, fbno, 0);
1587 xfs_trans_binval(args->tp, bp);
1588 }
1589 args->len = 1;
1590 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001591 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001593 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 error0);
1595 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001596 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001597
1598 /*
1599 * If we're feeding an AGFL block to something that
1600 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001601 * out the OWN_AG rmap and add the block back to
1602 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001603 */
1604 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1605 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1606 fbno, 1, &oinfo);
1607 if (error)
1608 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001609 pag = xfs_perag_get(args->mp, args->agno);
1610 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1611 args->tp, 1);
1612 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 *stat = 0;
1615 return 0;
1616 }
1617 /*
1618 * Nothing in the freelist.
1619 */
1620 else
1621 flen = 0;
1622 }
1623 /*
1624 * Can't allocate from the freelist for some reason.
1625 */
Nathan Scottd432c802006-09-28 11:03:44 +10001626 else {
1627 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Can't do the allocation, give up.
1632 */
1633 if (flen < args->minlen) {
1634 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001635 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 flen = 0;
1637 }
1638 *fbnop = fbno;
1639 *flenp = flen;
1640 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001641 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 0;
1643
1644error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001645 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return error;
1647}
1648
1649/*
1650 * Free the extent starting at agno/bno for length.
1651 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001652STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001654 xfs_trans_t *tp,
1655 xfs_buf_t *agbp,
1656 xfs_agnumber_t agno,
1657 xfs_agblock_t bno,
1658 xfs_extlen_t len,
1659 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001660 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1663 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1664 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 xfs_agblock_t gtbno; /* start of right neighbor block */
1666 xfs_extlen_t gtlen; /* length of right neighbor block */
1667 int haveleft; /* have a left neighbor block */
1668 int haveright; /* have a right neighbor block */
1669 int i; /* temp, result code */
1670 xfs_agblock_t ltbno; /* start of left neighbor block */
1671 xfs_extlen_t ltlen; /* length of left neighbor block */
1672 xfs_mount_t *mp; /* mount point struct for filesystem */
1673 xfs_agblock_t nbno; /* new starting block of freespace */
1674 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001675 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Darrick J. Wong673930c2016-08-03 11:33:43 +10001677 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001679
1680 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1681 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1682 if (error)
1683 goto error0;
1684 }
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Allocate and initialize a cursor for the by-block btree.
1688 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001689 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * Look for a neighboring block on the left (lower block numbers)
1692 * that is contiguous with this space.
1693 */
1694 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1695 goto error0;
1696 if (haveleft) {
1697 /*
1698 * There is a block to our left.
1699 */
1700 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1701 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001702 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
1704 * It's not contiguous, though.
1705 */
1706 if (ltbno + ltlen < bno)
1707 haveleft = 0;
1708 else {
1709 /*
1710 * If this failure happens the request to free this
1711 * space was invalid, it's (partly) already free.
1712 * Very bad.
1713 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001714 XFS_WANT_CORRUPTED_GOTO(mp,
1715 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 }
1718 /*
1719 * Look for a neighboring block on the right (higher block numbers)
1720 * that is contiguous with this space.
1721 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001722 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto error0;
1724 if (haveright) {
1725 /*
1726 * There is a block to our right.
1727 */
1728 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1729 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001730 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * It's not contiguous, though.
1733 */
1734 if (bno + len < gtbno)
1735 haveright = 0;
1736 else {
1737 /*
1738 * If this failure happens the request to free this
1739 * space was invalid, it's (partly) already free.
1740 * Very bad.
1741 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001742 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 }
1745 /*
1746 * Now allocate and initialize a cursor for the by-size tree.
1747 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001748 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * Have both left and right contiguous neighbors.
1751 * Merge all three into a single free block.
1752 */
1753 if (haveleft && haveright) {
1754 /*
1755 * Delete the old by-size entry on the left.
1756 */
1757 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1758 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001760 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001762 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
1764 * Delete the old by-size entry on the right.
1765 */
1766 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1767 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001768 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001769 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
1773 * Delete the old by-block entry for the right block.
1774 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001775 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /*
1779 * Move the by-block cursor back to the left neighbor.
1780 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001781 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001783 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#ifdef DEBUG
1785 /*
1786 * Check that this is the right record: delete didn't
1787 * mangle the cursor.
1788 */
1789 {
1790 xfs_agblock_t xxbno;
1791 xfs_extlen_t xxlen;
1792
1793 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1794 &i)))
1795 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001796 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 i == 1 && xxbno == ltbno && xxlen == ltlen,
1798 error0);
1799 }
1800#endif
1801 /*
1802 * Update remaining by-block entry to the new, joined block.
1803 */
1804 nbno = ltbno;
1805 nlen = len + ltlen + gtlen;
1806 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1807 goto error0;
1808 }
1809 /*
1810 * Have only a left contiguous neighbor.
1811 * Merge it together with the new freespace.
1812 */
1813 else if (haveleft) {
1814 /*
1815 * Delete the old by-size entry on the left.
1816 */
1817 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1818 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001819 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001820 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001822 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * Back up the by-block cursor to the left neighbor, and
1825 * update its length.
1826 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001827 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001829 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 nbno = ltbno;
1831 nlen = len + ltlen;
1832 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1833 goto error0;
1834 }
1835 /*
1836 * Have only a right contiguous neighbor.
1837 * Merge it together with the new freespace.
1838 */
1839 else if (haveright) {
1840 /*
1841 * Delete the old by-size entry on the right.
1842 */
1843 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1844 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001846 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /*
1850 * Update the starting block and length of the right
1851 * neighbor in the by-block tree.
1852 */
1853 nbno = bno;
1854 nlen = len + gtlen;
1855 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1856 goto error0;
1857 }
1858 /*
1859 * No contiguous neighbors.
1860 * Insert the new freespace into the by-block tree.
1861 */
1862 else {
1863 nbno = bno;
1864 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001865 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001867 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1870 bno_cur = NULL;
1871 /*
1872 * In all cases we need to insert the new freespace in the by-size tree.
1873 */
1874 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1875 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001876 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001877 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001879 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1881 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /*
1884 * Update the freespace totals in the ag and superblock.
1885 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001886 pag = xfs_perag_get(mp, agno);
1887 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001888 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001889 xfs_perag_put(pag);
1890 if (error)
1891 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001893 XFS_STATS_INC(mp, xs_freex);
1894 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001895
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001896 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1897 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900
1901 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001902 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1903 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (bno_cur)
1905 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1906 if (cnt_cur)
1907 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1908 return error;
1909}
1910
1911/*
1912 * Visible (exported) allocation/free functions.
1913 * Some of these are used just by xfs_alloc_btree.c and this file.
1914 */
1915
1916/*
1917 * Compute and fill in value of m_ag_maxlevels.
1918 */
1919void
1920xfs_alloc_compute_maxlevels(
1921 xfs_mount_t *mp) /* file system mount structure */
1922{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001923 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1924 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001928 * Find the length of the longest extent in an AG. The 'need' parameter
1929 * specifies how much space we're going to need for the AGFL and the
1930 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1931 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001932 */
1933xfs_extlen_t
1934xfs_alloc_longest_free_extent(
1935 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001936 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001937 xfs_extlen_t need,
1938 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001939{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001940 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001941
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001942 /*
1943 * If the AGFL needs a recharge, we'll have to subtract that from the
1944 * longest extent.
1945 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001946 if (need > pag->pagf_flcount)
1947 delta = need - pag->pagf_flcount;
1948
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001949 /*
1950 * If we cannot maintain others' reservations with space from the
1951 * not-longest freesp extents, we'll have to subtract /that/ from
1952 * the longest extent too.
1953 */
1954 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1955 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1956
1957 /*
1958 * If the longest extent is long enough to satisfy all the
1959 * reservations and AGFL rules in place, we can return this extent.
1960 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001961 if (pag->pagf_longest > delta)
1962 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001963
1964 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01001965 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1966}
1967
Dave Chinner496817b2015-06-22 10:13:30 +10001968unsigned int
1969xfs_alloc_min_freelist(
1970 struct xfs_mount *mp,
1971 struct xfs_perag *pag)
1972{
1973 unsigned int min_free;
1974
1975 /* space needed by-bno freespace btree */
1976 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1977 mp->m_ag_maxlevels);
1978 /* space needed by-size freespace btree */
1979 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1980 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10001981 /* space needed reverse mapping used space btree */
1982 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1983 min_free += min_t(unsigned int,
1984 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
1985 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10001986
1987 return min_free;
1988}
1989
Dave Chinner6cc87642009-03-16 08:29:46 +01001990/*
Dave Chinner72d55282015-06-22 10:04:42 +10001991 * Check if the operation we are fixing up the freelist for should go ahead or
1992 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1993 * is dependent on whether the size and shape of free space available will
1994 * permit the requested allocation to take place.
1995 */
1996static bool
1997xfs_alloc_space_available(
1998 struct xfs_alloc_arg *args,
1999 xfs_extlen_t min_free,
2000 int flags)
2001{
2002 struct xfs_perag *pag = args->pag;
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002003 xfs_extlen_t alloc_len, longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002004 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002005 int available;
2006
2007 if (flags & XFS_ALLOC_FLAG_FREEING)
2008 return true;
2009
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002010 reservation = xfs_ag_resv_needed(pag, args->resv);
2011
Dave Chinner72d55282015-06-22 10:04:42 +10002012 /* do we have enough contiguous free space for the allocation? */
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002013 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002014 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2015 reservation);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002016 if (longest < alloc_len)
Dave Chinner72d55282015-06-22 10:04:42 +10002017 return false;
2018
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002019 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002020 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Christoph Hellwig54fee132017-01-09 13:44:30 -08002021 reservation - min_free - args->minleft);
Christoph Hellwig12ef8302017-01-09 13:39:35 -08002022 if (available < (int)max(args->total, alloc_len))
Dave Chinner72d55282015-06-22 10:04:42 +10002023 return false;
2024
Christoph Hellwig54fee132017-01-09 13:44:30 -08002025 /*
2026 * Clamp maxlen to the amount of free space available for the actual
2027 * extent allocation.
2028 */
2029 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2030 args->maxlen = available;
2031 ASSERT(args->maxlen > 0);
2032 ASSERT(args->maxlen >= args->minlen);
2033 }
2034
Dave Chinner72d55282015-06-22 10:04:42 +10002035 return true;
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/*
2039 * Decide whether to use this allocation group for this allocation.
2040 * If so, fix up the btree freelist's size.
2041 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002042int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002044 struct xfs_alloc_arg *args, /* allocation argument structure */
2045 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Dave Chinner396503f2015-06-22 10:13:19 +10002047 struct xfs_mount *mp = args->mp;
2048 struct xfs_perag *pag = args->pag;
2049 struct xfs_trans *tp = args->tp;
2050 struct xfs_buf *agbp = NULL;
2051 struct xfs_buf *agflbp = NULL;
2052 struct xfs_alloc_arg targs; /* local allocation arguments */
2053 xfs_agblock_t bno; /* freelist block */
2054 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002055 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002058 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2059 if (error)
2060 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002062 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2063 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002064 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Dave Chinner396503f2015-06-22 10:13:19 +10002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002068 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002069 * If this is a metadata preferred pag and we are user data then try
2070 * somewhere else if we are not being asked to try harder at this
2071 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
Dave Chinner292378e2016-09-26 08:21:28 +10002073 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002074 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2075 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002076 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078
Dave Chinner496817b2015-06-22 10:13:30 +10002079 need = xfs_alloc_min_freelist(mp, pag);
Christoph Hellwig54fee132017-01-09 13:44:30 -08002080 if (!xfs_alloc_space_available(args, need, flags |
2081 XFS_ALLOC_FLAG_CHECK))
Dave Chinner396503f2015-06-22 10:13:19 +10002082 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Get the a.g. freespace buffer.
2086 * Can fail if we're not blocking on locks, and it's held.
2087 */
Dave Chinner396503f2015-06-22 10:13:19 +10002088 if (!agbp) {
2089 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2090 if (error)
2091 goto out_no_agbp;
2092 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002093 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2094 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002095 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002098
Dave Chinner50adbcb2015-06-22 10:04:31 +10002099 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002100 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002101 if (!xfs_alloc_space_available(args, need, flags))
2102 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /*
2105 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002106 *
Dave Chinner396503f2015-06-22 10:13:19 +10002107 * Note that from this point onwards, we will always release the agf and
2108 * agfl buffers on error. This handles the case where we error out and
2109 * the buffers are clean or may not have been joined to the transaction
2110 * and hence need to be released manually. If they have been joined to
2111 * the transaction, then xfs_trans_brelse() will handle them
2112 * appropriately based on the recursion count and dirty state of the
2113 * buffer.
2114 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002115 * XXX (dgc): When we have lots of free space, does this buy us
2116 * anything other than extra overhead when we need to put more blocks
2117 * back on the free list? Maybe we should only do this when space is
2118 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002119 *
2120 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2121 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2122 * updating the rmapbt. Both flags are used in xfs_repair while we're
2123 * rebuilding the rmapbt, and neither are used by the kernel. They're
2124 * both required to ensure that rmaps are correctly recorded for the
2125 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2126 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002128 memset(&targs, 0, sizeof(targs));
2129 if (flags & XFS_ALLOC_FLAG_NORMAP)
2130 xfs_rmap_skip_owner_update(&targs.oinfo);
2131 else
2132 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2133 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002134 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
David Chinner92821e22007-05-24 15:26:31 +10002136 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2137 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002138 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002139 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002140 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002141 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002142 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2144 xfs_trans_binval(tp, bp);
2145 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 targs.tp = tp;
2148 targs.mp = mp;
2149 targs.agbp = agbp;
2150 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002151 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 targs.type = XFS_ALLOCTYPE_THIS_AG;
2153 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002154 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2155 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002156 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002157
2158 /* Make the freelist longer if it's too short. */
2159 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002161 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002162 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002163
2164 /* Allocate as many blocks as possible at once. */
2165 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002166 if (error)
2167 goto out_agflbp_relse;
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * Stop if we run out. Won't happen if callers are obeying
2171 * the restrictions correctly. Can happen for free calls
2172 * on a completely full ag.
2173 */
Yingping Lud210a282006-06-09 14:55:18 +10002174 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002175 if (flags & XFS_ALLOC_FLAG_FREEING)
2176 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002177 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * Put each allocated block on the list.
2181 */
2182 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002183 error = xfs_alloc_put_freelist(tp, agbp,
2184 agflbp, bno, 0);
2185 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002186 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002189 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 args->agbp = agbp;
2191 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002192
2193out_agflbp_relse:
2194 xfs_trans_brelse(tp, agflbp);
2195out_agbp_relse:
2196 if (agbp)
2197 xfs_trans_brelse(tp, agbp);
2198out_no_agbp:
2199 args->agbp = NULL;
2200 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203/*
2204 * Get a block from the freelist.
2205 * Returns with the buffer for the block gotten.
2206 */
2207int /* error */
2208xfs_alloc_get_freelist(
2209 xfs_trans_t *tp, /* transaction pointer */
2210 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002211 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2212 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2216 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002217 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int error;
David Chinner92821e22007-05-24 15:26:31 +10002219 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002220 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 xfs_perag_t *pag; /* per allocation group data */
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /*
2224 * Freelist is empty, give up.
2225 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002226 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!agf->agf_flcount) {
2228 *bnop = NULLAGBLOCK;
2229 return 0;
2230 }
2231 /*
2232 * Read the array of free blocks.
2233 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002234 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2235 &agflbp);
2236 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002238
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /*
2241 * Get the block number and update the data structures.
2242 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002243 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2244 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002245 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002247 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002249
2250 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002251 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 xfs_trans_agflist_delta(tp, -1);
2253 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002254 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002255
2256 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2257 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002258 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002259 pag->pagf_btreeblks++;
2260 logflags |= XFS_AGF_BTREEBLKS;
2261 }
2262
David Chinner92821e22007-05-24 15:26:31 +10002263 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 *bnop = bno;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267}
2268
2269/*
2270 * Log the given fields from the agf structure.
2271 */
2272void
2273xfs_alloc_log_agf(
2274 xfs_trans_t *tp, /* transaction pointer */
2275 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2276 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2277{
2278 int first; /* first byte offset */
2279 int last; /* last byte offset */
2280 static const short offsets[] = {
2281 offsetof(xfs_agf_t, agf_magicnum),
2282 offsetof(xfs_agf_t, agf_versionnum),
2283 offsetof(xfs_agf_t, agf_seqno),
2284 offsetof(xfs_agf_t, agf_length),
2285 offsetof(xfs_agf_t, agf_roots[0]),
2286 offsetof(xfs_agf_t, agf_levels[0]),
2287 offsetof(xfs_agf_t, agf_flfirst),
2288 offsetof(xfs_agf_t, agf_fllast),
2289 offsetof(xfs_agf_t, agf_flcount),
2290 offsetof(xfs_agf_t, agf_freeblks),
2291 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002292 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002293 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002294 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002295 offsetof(xfs_agf_t, agf_refcount_blocks),
2296 offsetof(xfs_agf_t, agf_refcount_root),
2297 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002298 /* needed so that we don't log the whole rest of the structure: */
2299 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 sizeof(xfs_agf_t)
2301 };
2302
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002303 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2304
Dave Chinner61fe1352013-04-03 16:11:30 +11002305 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2308 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2309}
2310
2311/*
2312 * Interface for inode allocation to force the pag data to be initialized.
2313 */
2314int /* error */
2315xfs_alloc_pagf_init(
2316 xfs_mount_t *mp, /* file system mount structure */
2317 xfs_trans_t *tp, /* transaction pointer */
2318 xfs_agnumber_t agno, /* allocation group number */
2319 int flags) /* XFS_ALLOC_FLAGS_... */
2320{
2321 xfs_buf_t *bp;
2322 int error;
2323
2324 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2325 return error;
2326 if (bp)
2327 xfs_trans_brelse(tp, bp);
2328 return 0;
2329}
2330
2331/*
2332 * Put the block on the freelist for the allocation group.
2333 */
2334int /* error */
2335xfs_alloc_put_freelist(
2336 xfs_trans_t *tp, /* transaction pointer */
2337 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2338 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002339 xfs_agblock_t bno, /* block being freed */
2340 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002343 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 int error;
David Chinner92821e22007-05-24 15:26:31 +10002345 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 xfs_mount_t *mp; /* mount structure */
2347 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002348 __be32 *agfl_bno;
2349 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 agf = XFS_BUF_TO_AGF(agbp);
2352 mp = tp->t_mountp;
2353
2354 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002355 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002357 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002358 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002360
2361 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002362 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 xfs_trans_agflist_delta(tp, 1);
2364 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002365
2366 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2367 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002368 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002369 pag->pagf_btreeblks--;
2370 logflags |= XFS_AGF_BTREEBLKS;
2371 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002372 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002373
David Chinner92821e22007-05-24 15:26:31 +10002374 xfs_alloc_log_agf(tp, agbp, logflags);
2375
Christoph Hellwig16259e72005-11-02 15:11:25 +11002376 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002377
2378 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2379 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002380 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002381 startoff = (char *)blockp - (char *)agflbp->b_addr;
2382
David Chinner92821e22007-05-24 15:26:31 +10002383 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002384
Dave Chinner61fe1352013-04-03 16:11:30 +11002385 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002386 xfs_trans_log_buf(tp, agflbp, startoff,
2387 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return 0;
2389}
2390
Dave Chinner4e0e6042013-04-03 16:11:13 +11002391static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002392xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002393 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002394 struct xfs_buf *bp)
2395 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002396 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002397
Brian Fostera45086e2015-10-12 15:59:25 +11002398 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2399 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002400 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002401 if (!xfs_log_check_lsn(mp,
2402 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2403 return false;
2404 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002405
Dave Chinner4e0e6042013-04-03 16:11:13 +11002406 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2407 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2408 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2409 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2410 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2411 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2412 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002413
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002414 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2415 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2416 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002417 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2418 return false;
2419
Darrick J. Wongb8704942016-08-03 11:30:32 +10002420 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002421 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2422 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wongb8704942016-08-03 11:30:32 +10002423 return false;
2424
Dave Chinner5d5f5272012-11-14 17:44:56 +11002425 /*
2426 * during growfs operations, the perag is not fully initialised,
2427 * so we can't use it for any useful checking. growfs ensures we can't
2428 * use it by using uncached buffers that don't have the perag attached
2429 * so we can detect and avoid this problem.
2430 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002431 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2432 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002433
Dave Chinner4e0e6042013-04-03 16:11:13 +11002434 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2435 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2436 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002437
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002438 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002439 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2440 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002441 return false;
2442
Dave Chinner4e0e6042013-04-03 16:11:13 +11002443 return true;;
2444
Dave Chinner612cfbf2012-11-14 17:52:32 +11002445}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002446
Dave Chinner1813dd62012-11-14 17:54:40 +11002447static void
2448xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002449 struct xfs_buf *bp)
2450{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002451 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002452
Eric Sandeence5028c2014-02-27 15:23:10 +11002453 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2454 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002455 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002456 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2457 XFS_ERRTAG_ALLOC_READ_AGF,
2458 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002459 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002460
2461 if (bp->b_error)
2462 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002463}
2464
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002465static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002466xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002467 struct xfs_buf *bp)
2468{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002469 struct xfs_mount *mp = bp->b_target->bt_mount;
2470 struct xfs_buf_log_item *bip = bp->b_fspriv;
2471
2472 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002473 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002474 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002475 return;
2476 }
2477
2478 if (!xfs_sb_version_hascrc(&mp->m_sb))
2479 return;
2480
2481 if (bip)
2482 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2483
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002484 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002485}
2486
Dave Chinner1813dd62012-11-14 17:54:40 +11002487const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002488 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002489 .verify_read = xfs_agf_read_verify,
2490 .verify_write = xfs_agf_write_verify,
2491};
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
2494 * Read in the allocation group header (free/alloc section).
2495 */
2496int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002497xfs_read_agf(
2498 struct xfs_mount *mp, /* mount point structure */
2499 struct xfs_trans *tp, /* transaction pointer */
2500 xfs_agnumber_t agno, /* allocation group number */
2501 int flags, /* XFS_BUF_ */
2502 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int error;
2505
Dave Chinnerd1230312013-11-01 15:27:19 +11002506 trace_xfs_read_agf(mp, agno);
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 ASSERT(agno != NULLAGNUMBER);
2509 error = xfs_trans_read_buf(
2510 mp, tp, mp->m_ddev_targp,
2511 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002512 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (error)
2514 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002515 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002517
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002518 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002519 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002520 return 0;
2521}
2522
2523/*
2524 * Read in the allocation group header (free/alloc section).
2525 */
2526int /* error */
2527xfs_alloc_read_agf(
2528 struct xfs_mount *mp, /* mount point structure */
2529 struct xfs_trans *tp, /* transaction pointer */
2530 xfs_agnumber_t agno, /* allocation group number */
2531 int flags, /* XFS_ALLOC_FLAG_... */
2532 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2533{
2534 struct xfs_agf *agf; /* ag freelist header */
2535 struct xfs_perag *pag; /* per allocation group data */
2536 int error;
2537
Dave Chinnerd1230312013-11-01 15:27:19 +11002538 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002539
Dave Chinnerd1230312013-11-01 15:27:19 +11002540 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002541 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002542 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002543 bpp);
2544 if (error)
2545 return error;
2546 if (!*bpp)
2547 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002548 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002549
2550 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002551 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002553 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002554 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002555 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2556 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002558 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002560 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002561 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2562 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002563 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002564 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002565 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002566 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 pag->pagf_init = 1;
2568 }
2569#ifdef DEBUG
2570 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002571 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002572 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002573 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2574 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002576 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002578 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002581 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583}
2584
2585/*
2586 * Allocate an extent (variable-size).
2587 * Depending on the allocation type, we either look in a single allocation
2588 * group or loop over the allocation groups to find the result.
2589 */
2590int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002591xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 xfs_alloc_arg_t *args) /* allocation argument structure */
2593{
2594 xfs_agblock_t agsize; /* allocation group size */
2595 int error;
2596 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 xfs_mount_t *mp; /* mount structure pointer */
2598 xfs_agnumber_t sagno; /* starting allocation group number */
2599 xfs_alloctype_t type; /* input allocation type */
2600 int bump_rotor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2602
2603 mp = args->mp;
2604 type = args->otype = args->type;
2605 args->agbno = NULLAGBLOCK;
2606 /*
2607 * Just fix this up, for the case where the last a.g. is shorter
2608 * (or there's only one a.g.) and the caller couldn't easily figure
2609 * that out (xfs_bmap_alloc).
2610 */
2611 agsize = mp->m_sb.sb_agblocks;
2612 if (args->maxlen > agsize)
2613 args->maxlen = agsize;
2614 if (args->alignment == 0)
2615 args->alignment = 1;
2616 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2617 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2618 ASSERT(args->minlen <= args->maxlen);
2619 ASSERT(args->minlen <= agsize);
2620 ASSERT(args->mod < args->prod);
2621 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2622 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2623 args->minlen > args->maxlen || args->minlen > agsize ||
2624 args->mod >= args->prod) {
2625 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002626 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return 0;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 switch (type) {
2631 case XFS_ALLOCTYPE_THIS_AG:
2632 case XFS_ALLOCTYPE_NEAR_BNO:
2633 case XFS_ALLOCTYPE_THIS_BNO:
2634 /*
2635 * These three force us into a single a.g.
2636 */
2637 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002638 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 error = xfs_alloc_fix_freelist(args, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002641 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 goto error0;
2643 }
2644 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002645 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 break;
2647 }
2648 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2649 if ((error = xfs_alloc_ag_vextent(args)))
2650 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 break;
2652 case XFS_ALLOCTYPE_START_BNO:
2653 /*
2654 * Try near allocation first, then anywhere-in-ag after
2655 * the first a.g. fails.
2656 */
Dave Chinner292378e2016-09-26 08:21:28 +10002657 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2659 args->fsbno = XFS_AGB_TO_FSB(mp,
2660 ((mp->m_agfrotor / rotorstep) %
2661 mp->m_sb.sb_agcount), 0);
2662 bump_rotor = 1;
2663 }
2664 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2665 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2666 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 case XFS_ALLOCTYPE_FIRST_AG:
2668 /*
2669 * Rotate through the allocation groups looking for a winner.
2670 */
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002671 if (type == XFS_ALLOCTYPE_FIRST_AG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /*
2673 * Start with allocation group given by bno.
2674 */
2675 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2676 args->type = XFS_ALLOCTYPE_THIS_AG;
2677 sagno = 0;
2678 flags = 0;
2679 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /*
2681 * Start with the given allocation group.
2682 */
2683 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2684 flags = XFS_ALLOC_FLAG_TRYLOCK;
2685 }
2686 /*
2687 * Loop over allocation groups twice; first time with
2688 * trylock set, second time without.
2689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002691 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 error = xfs_alloc_fix_freelist(args, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002694 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 goto error0;
2696 }
2697 /*
2698 * If we get a buffer back then the allocation will fly.
2699 */
2700 if (args->agbp) {
2701 if ((error = xfs_alloc_ag_vextent(args)))
2702 goto error0;
2703 break;
2704 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002705
2706 trace_xfs_alloc_vextent_loopfailed(args);
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /*
2709 * Didn't work, figure out the next iteration.
2710 */
2711 if (args->agno == sagno &&
2712 type == XFS_ALLOCTYPE_START_BNO)
2713 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002714 /*
2715 * For the first allocation, we can try any AG to get
2716 * space. However, if we already have allocated a
2717 * block, we don't want to try AGs whose number is below
2718 * sagno. Otherwise, we may end up with out-of-order
2719 * locking of AGF, which might cause deadlock.
2720 */
2721 if (++(args->agno) == mp->m_sb.sb_agcount) {
2722 if (args->firstblock != NULLFSBLOCK)
2723 args->agno = sagno;
2724 else
2725 args->agno = 0;
2726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * Reached the starting a.g., must either be done
2729 * or switch to non-trylock mode.
2730 */
2731 if (args->agno == sagno) {
Christoph Hellwig255c5162017-01-09 13:36:19 -08002732 if (flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002734 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 break;
2736 }
Christoph Hellwig255c5162017-01-09 13:36:19 -08002737
2738 flags = 0;
2739 if (type == XFS_ALLOCTYPE_START_BNO) {
2740 args->agbno = XFS_FSB_TO_AGBNO(mp,
2741 args->fsbno);
2742 args->type = XFS_ALLOCTYPE_NEAR_BNO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002745 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Christoph Hellwig8d242e92017-02-17 08:21:15 -08002747 if (bump_rotor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (args->agno == sagno)
2749 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2750 (mp->m_sb.sb_agcount * rotorstep);
2751 else
2752 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2753 (mp->m_sb.sb_agcount * rotorstep);
2754 }
2755 break;
2756 default:
2757 ASSERT(0);
2758 /* NOTREACHED */
2759 }
2760 if (args->agbno == NULLAGBLOCK)
2761 args->fsbno = NULLFSBLOCK;
2762 else {
2763 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2764#ifdef DEBUG
2765 ASSERT(args->len >= args->minlen);
2766 ASSERT(args->len <= args->maxlen);
2767 ASSERT(args->agbno % args->alignment == 0);
2768 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2769 args->len);
2770#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002771
2772 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002773 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002774 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2775 if (error)
2776 goto error0;
2777 }
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002780 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
2782error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002783 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return error;
2785}
2786
Dave Chinner4d89e202016-06-21 11:53:28 +10002787/* Ensure that the freelist is at full capacity. */
2788int
2789xfs_free_extent_fix_freelist(
2790 struct xfs_trans *tp,
2791 xfs_agnumber_t agno,
2792 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Dave Chinner4d89e202016-06-21 11:53:28 +10002794 struct xfs_alloc_arg args;
2795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Dave Chinner4d89e202016-06-21 11:53:28 +10002797 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 args.tp = tp;
2799 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002800 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002801
2802 /*
2803 * validate that the block number is legal - the enables us to detect
2804 * and handle a silent filesystem corruption rather than crashing.
2805 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002806 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002807 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002808
Dave Chinnera862e0f2010-01-11 11:47:41 +00002809 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002810 ASSERT(args.pag);
2811
2812 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2813 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002814 goto out;
2815
2816 *agbp = args.agbp;
2817out:
2818 xfs_perag_put(args.pag);
2819 return error;
2820}
2821
2822/*
2823 * Free an extent.
2824 * Just break up the extent address and hand off to xfs_free_ag_extent
2825 * after fixing up the freelist.
2826 */
2827int /* error */
2828xfs_free_extent(
2829 struct xfs_trans *tp, /* transaction pointer */
2830 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002831 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002832 struct xfs_owner_info *oinfo, /* extent owner */
2833 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002834{
2835 struct xfs_mount *mp = tp->t_mountp;
2836 struct xfs_buf *agbp;
2837 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2838 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2839 int error;
2840
2841 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002842 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002843
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002844 if (XFS_TEST_ERROR(false, mp,
2845 XFS_ERRTAG_FREE_EXTENT,
2846 XFS_RANDOM_FREE_EXTENT))
2847 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}