blob: e567551402a65785a74967c6945f6db155c23d79 [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 Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
Dave Chinner70a9883c2013-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 Hellwigd20e4ad2017-02-02 08:55:53 +010098 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 */
David Chinner12375c82008-04-10 12:21:32 +1000224STATIC void
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 */
230 xfs_extlen_t *reslen) /* result length */
231{
232 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000234 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Christoph Hellwige26f0502011-04-24 19:06:15 +0000236 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000237 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000238
Brian Fosterbfe46d42015-05-29 08:53:00 +1000239 /*
240 * If we have a largish extent that happens to start before min_agbno,
241 * see if we can shift it into range...
242 */
243 if (bno < args->min_agbno && bno + len > args->min_agbno) {
244 diff = args->min_agbno - bno;
245 if (len > diff) {
246 bno += diff;
247 len -= diff;
248 }
249 }
250
Christoph Hellwige26f0502011-04-24 19:06:15 +0000251 if (args->alignment > 1 && len >= args->minlen) {
252 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000253
254 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000255
256 *resbno = aligned_bno;
257 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000259 *resbno = bno;
260 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * Compute best start block and diff for "near" allocations.
266 * freelen >= wantlen already checked by caller.
267 */
268STATIC xfs_extlen_t /* difference value (absolute) */
269xfs_alloc_compute_diff(
270 xfs_agblock_t wantbno, /* target starting block */
271 xfs_extlen_t wantlen, /* target length */
272 xfs_extlen_t alignment, /* target alignment */
Dave Chinner292378e2016-09-26 08:21:28 +1000273 int datatype, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfs_agblock_t freebno, /* freespace's starting block */
275 xfs_extlen_t freelen, /* freespace's length */
276 xfs_agblock_t *newbnop) /* result: best start block from free */
277{
278 xfs_agblock_t freeend; /* end of freespace extent */
279 xfs_agblock_t newbno1; /* return block number */
280 xfs_agblock_t newbno2; /* other new block number */
281 xfs_extlen_t newlen1=0; /* length with newbno1 */
282 xfs_extlen_t newlen2=0; /* length with newbno2 */
283 xfs_agblock_t wantend; /* end of target extent */
Dave Chinner292378e2016-09-26 08:21:28 +1000284 bool userdata = xfs_alloc_is_userdata(datatype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 ASSERT(freelen >= wantlen);
287 freeend = freebno + freelen;
288 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200289 /*
290 * We want to allocate from the start of a free extent if it is past
291 * the desired block or if we are allocating user data and the free
292 * extent is before desired block. The second case is there to allow
293 * for contiguous allocation from the remaining free space if the file
294 * grows in the short term.
295 */
296 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
298 newbno1 = NULLAGBLOCK;
299 } else if (freeend >= wantend && alignment > 1) {
300 newbno1 = roundup(wantbno, alignment);
301 newbno2 = newbno1 - alignment;
302 if (newbno1 >= freeend)
303 newbno1 = NULLAGBLOCK;
304 else
305 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
306 if (newbno2 < freebno)
307 newbno2 = NULLAGBLOCK;
308 else
309 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
310 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
311 if (newlen1 < newlen2 ||
312 (newlen1 == newlen2 &&
313 XFS_ABSDIFF(newbno1, wantbno) >
314 XFS_ABSDIFF(newbno2, wantbno)))
315 newbno1 = newbno2;
316 } else if (newbno2 != NULLAGBLOCK)
317 newbno1 = newbno2;
318 } else if (freeend >= wantend) {
319 newbno1 = wantbno;
320 } else if (alignment > 1) {
321 newbno1 = roundup(freeend - wantlen, alignment);
322 if (newbno1 > freeend - wantlen &&
323 newbno1 - alignment >= freebno)
324 newbno1 -= alignment;
325 else if (newbno1 >= freeend)
326 newbno1 = NULLAGBLOCK;
327 } else
328 newbno1 = freeend - wantlen;
329 *newbnop = newbno1;
330 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
331}
332
333/*
334 * Fix up the length, based on mod and prod.
335 * len should be k * prod + mod for some k.
336 * If len is too small it is returned unchanged.
337 * If len hits maxlen it is left alone.
338 */
339STATIC void
340xfs_alloc_fix_len(
341 xfs_alloc_arg_t *args) /* allocation argument structure */
342{
343 xfs_extlen_t k;
344 xfs_extlen_t rlen;
345
346 ASSERT(args->mod < args->prod);
347 rlen = args->len;
348 ASSERT(rlen >= args->minlen);
349 ASSERT(rlen <= args->maxlen);
350 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
351 (args->mod == 0 && rlen < args->prod))
352 return;
353 k = rlen % args->prod;
354 if (k == args->mod)
355 return;
Jan Kara30265112014-06-06 16:06:37 +1000356 if (k > args->mod)
357 rlen = rlen - (k - args->mod);
358 else
359 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100360 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000361 if ((int)rlen < (int)args->minlen)
362 return;
363 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
364 ASSERT(rlen % args->prod == args->mod);
Christoph Hellwig6b813652017-02-02 08:55:55 +0100365 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
366 rlen + args->minleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 args->len = rlen;
368}
369
370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Update the two btrees, logically removing from freespace the extent
372 * starting at rbno, rlen blocks. The extent is contained within the
373 * actual (current) free extent fbno for flen blocks.
374 * Flags are passed in indicating whether the cursors are set to the
375 * relevant records.
376 */
377STATIC int /* error code */
378xfs_alloc_fixup_trees(
379 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
380 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
381 xfs_agblock_t fbno, /* starting block of free extent */
382 xfs_extlen_t flen, /* length of free extent */
383 xfs_agblock_t rbno, /* starting block of returned extent */
384 xfs_extlen_t rlen, /* length of returned extent */
385 int flags) /* flags, XFSA_FIXUP_... */
386{
387 int error; /* error code */
388 int i; /* operation results */
389 xfs_agblock_t nfbno1; /* first new free startblock */
390 xfs_agblock_t nfbno2; /* second new free startblock */
391 xfs_extlen_t nflen1=0; /* first new free length */
392 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100393 struct xfs_mount *mp;
394
395 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Look up the record in the by-size tree if necessary.
399 */
400 if (flags & XFSA_FIXUP_CNT_OK) {
401#ifdef DEBUG
402 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
403 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100404 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 i == 1 && nfbno1 == fbno && nflen1 == flen);
406#endif
407 } else {
408 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
409 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100410 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 /*
413 * Look up the record in the by-block tree if necessary.
414 */
415 if (flags & XFSA_FIXUP_BNO_OK) {
416#ifdef DEBUG
417 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
418 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100419 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 i == 1 && nfbno1 == fbno && nflen1 == flen);
421#endif
422 } else {
423 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
424 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100425 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100428#ifdef DEBUG
429 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
430 struct xfs_btree_block *bnoblock;
431 struct xfs_btree_block *cntblock;
432
433 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
434 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
435
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100436 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100437 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Deal with all four cases: the allocated record is contained
443 * within the freespace record, so we can have new freespace
444 * at either (or both) end, or no freespace remaining.
445 */
446 if (rbno == fbno && rlen == flen)
447 nfbno1 = nfbno2 = NULLAGBLOCK;
448 else if (rbno == fbno) {
449 nfbno1 = rbno + rlen;
450 nflen1 = flen - rlen;
451 nfbno2 = NULLAGBLOCK;
452 } else if (rbno + rlen == fbno + flen) {
453 nfbno1 = fbno;
454 nflen1 = flen - rlen;
455 nfbno2 = NULLAGBLOCK;
456 } else {
457 nfbno1 = fbno;
458 nflen1 = rbno - fbno;
459 nfbno2 = rbno + rlen;
460 nflen2 = (fbno + flen) - nfbno2;
461 }
462 /*
463 * Delete the entry from the by-size btree.
464 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100465 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100467 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Add new by-size btree entry(s).
470 */
471 if (nfbno1 != NULLAGBLOCK) {
472 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
473 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100474 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100475 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100477 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 if (nfbno2 != NULLAGBLOCK) {
480 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
481 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100482 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100483 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100485 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 /*
488 * Fix up the by-block btree entry(s).
489 */
490 if (nfbno1 == NULLAGBLOCK) {
491 /*
492 * No remaining freespace, just delete the by-block tree entry.
493 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100494 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100496 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
498 /*
499 * Update the by-block entry to start later|be shorter.
500 */
501 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
502 return error;
503 }
504 if (nfbno2 != NULLAGBLOCK) {
505 /*
506 * 2 resulting free entries, need to add one.
507 */
508 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
509 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100510 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100511 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100513 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return 0;
516}
517
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100518static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100519xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100520 struct xfs_buf *bp)
521{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100522 struct xfs_mount *mp = bp->b_target->bt_mount;
523 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100524 int i;
525
Eric Sandeence748ea2015-07-29 11:53:31 +1000526 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100527 return false;
528 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
529 return false;
530 /*
531 * during growfs operations, the perag is not fully initialised,
532 * so we can't use it for any useful checking. growfs ensures we can't
533 * use it by using uncached buffers that don't have the perag attached
534 * so we can detect and avoid this problem.
535 */
536 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
537 return false;
538
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100539 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100540 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100541 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100542 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100543 }
Brian Fostera45086e2015-10-12 15:59:25 +1100544
545 return xfs_log_check_lsn(mp,
546 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100547}
548
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100549static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100550xfs_agfl_read_verify(
551 struct xfs_buf *bp)
552{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100553 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100554
555 /*
556 * There is no verification of non-crc AGFLs because mkfs does not
557 * initialise the AGFL to zero or NULL. Hence the only valid part of the
558 * AGFL is what the AGF says is active. We can't get to the AGF, so we
559 * can't verify just those entries are valid.
560 */
561 if (!xfs_sb_version_hascrc(&mp->m_sb))
562 return;
563
Eric Sandeence5028c2014-02-27 15:23:10 +1100564 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000565 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100566 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000567 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100568
569 if (bp->b_error)
570 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100571}
572
573static void
574xfs_agfl_write_verify(
575 struct xfs_buf *bp)
576{
577 struct xfs_mount *mp = bp->b_target->bt_mount;
578 struct xfs_buf_log_item *bip = bp->b_fspriv;
579
580 /* no verification of non-crc AGFLs */
581 if (!xfs_sb_version_hascrc(&mp->m_sb))
582 return;
583
584 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000585 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100586 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100587 return;
588 }
589
590 if (bip)
591 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
592
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100593 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100594}
595
Dave Chinner1813dd62012-11-14 17:54:40 +1100596const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100597 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100598 .verify_read = xfs_agfl_read_verify,
599 .verify_write = xfs_agfl_write_verify,
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * Read in the allocation group free block array.
604 */
605STATIC int /* error */
606xfs_alloc_read_agfl(
607 xfs_mount_t *mp, /* mount point structure */
608 xfs_trans_t *tp, /* transaction pointer */
609 xfs_agnumber_t agno, /* allocation group number */
610 xfs_buf_t **bpp) /* buffer for the ag free block array */
611{
612 xfs_buf_t *bp; /* return value */
613 int error;
614
615 ASSERT(agno != NULLAGNUMBER);
616 error = xfs_trans_read_buf(
617 mp, tp, mp->m_ddev_targp,
618 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100619 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (error)
621 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000622 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *bpp = bp;
624 return 0;
625}
626
Christoph Hellwigecb69282011-03-04 12:59:55 +0000627STATIC int
628xfs_alloc_update_counters(
629 struct xfs_trans *tp,
630 struct xfs_perag *pag,
631 struct xfs_buf *agbp,
632 long len)
633{
634 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
635
636 pag->pagf_freeblks += len;
637 be32_add_cpu(&agf->agf_freeblks, len);
638
639 xfs_trans_agblocks_delta(tp, len);
640 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
641 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000642 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000643
644 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
645 return 0;
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * Allocation group level functions.
650 */
651
652/*
653 * Allocate a variable extent in the allocation group agno.
654 * Type and bno are used to determine where in the allocation group the
655 * extent will start.
656 * Extent's length (returned in *len) will be between minlen and maxlen,
657 * and of the form k * prod + mod unless there's nothing that large.
658 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
659 */
660STATIC int /* error */
661xfs_alloc_ag_vextent(
662 xfs_alloc_arg_t *args) /* argument structure for allocation */
663{
664 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 ASSERT(args->minlen > 0);
667 ASSERT(args->maxlen > 0);
668 ASSERT(args->minlen <= args->maxlen);
669 ASSERT(args->mod < args->prod);
670 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000671
672 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * Branch to correct routine based on the type.
674 */
675 args->wasfromfl = 0;
676 switch (args->type) {
677 case XFS_ALLOCTYPE_THIS_AG:
678 error = xfs_alloc_ag_vextent_size(args);
679 break;
680 case XFS_ALLOCTYPE_NEAR_BNO:
681 error = xfs_alloc_ag_vextent_near(args);
682 break;
683 case XFS_ALLOCTYPE_THIS_BNO:
684 error = xfs_alloc_ag_vextent_exact(args);
685 break;
686 default:
687 ASSERT(0);
688 /* NOTREACHED */
689 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000690
691 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Christoph Hellwigecb69282011-03-04 12:59:55 +0000694 ASSERT(args->len >= args->minlen);
695 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000696 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000697 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Darrick J. Wong673930c2016-08-03 11:33:43 +1000699 /* if not file data, insert new block into the reverse map btree */
700 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
701 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
702 args->agbno, args->len, &args->oinfo);
703 if (error)
704 return error;
705 }
706
Christoph Hellwigecb69282011-03-04 12:59:55 +0000707 if (!args->wasfromfl) {
708 error = xfs_alloc_update_counters(args->tp, args->pag,
709 args->agbp,
710 -((long)(args->len)));
711 if (error)
712 return error;
713
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000714 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000715 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000717
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000718 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000719
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100720 XFS_STATS_INC(args->mp, xs_allocx);
721 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000722 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
726 * Allocate a variable extent at exactly agno/bno.
727 * Extent's length (returned in *len) will be between minlen and maxlen,
728 * and of the form k * prod + mod unless there's nothing that large.
729 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
730 */
731STATIC int /* error */
732xfs_alloc_ag_vextent_exact(
733 xfs_alloc_arg_t *args) /* allocation argument structure */
734{
735 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
736 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int error;
738 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000740 xfs_agblock_t tbno; /* start block of trimmed extent */
741 xfs_extlen_t tlen; /* length of trimmed extent */
742 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /*
748 * Allocate/initialize a cursor for the by-number freespace btree.
749 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100750 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000751 args->agno, XFS_BTNUM_BNO);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
755 * Look for the closest free block <= bno, it must contain bno
756 * if any free block does.
757 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000758 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
759 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000761 if (!i)
762 goto not_found;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Grab the freespace record.
766 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000767 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
768 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100770 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000774 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000776 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000777
778 /*
779 * Give up if the start of the extent is busy, or the freespace isn't
780 * long enough for the minimum request.
781 */
782 if (tbno > args->agbno)
783 goto not_found;
784 if (tlen < args->minlen)
785 goto not_found;
786 tend = tbno + tlen;
787 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000788 goto not_found;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * End of extent will be smaller of the freespace end and the
792 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * Fix the length according to mod and prod if given.
795 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000796 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
797 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_alloc_fix_len(args);
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000799 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000802 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Allocate/initialize a cursor for the by-size btree.
804 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100805 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
806 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100808 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000809 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
810 args->len, XFSA_FIXUP_BNO_OK);
811 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
813 goto error0;
814 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
817 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000820 trace_xfs_alloc_exact_done(args);
821 return 0;
822
823not_found:
824 /* Didn't find it, return null. */
825 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
826 args->agbno = NULLAGBLOCK;
827 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829
830error0:
831 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000832 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return error;
834}
835
836/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000837 * Search the btree in a given direction via the search cursor and compare
838 * the records found against the good extent we've already found.
839 */
840STATIC int
841xfs_alloc_find_best_extent(
842 struct xfs_alloc_arg *args, /* allocation argument structure */
843 struct xfs_btree_cur **gcur, /* good cursor */
844 struct xfs_btree_cur **scur, /* searching cursor */
845 xfs_agblock_t gdiff, /* difference for search comparison */
846 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000847 xfs_extlen_t *slen, /* extent length */
848 xfs_agblock_t *sbnoa, /* aligned extent found by search */
849 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000850 int dir) /* 0 = search right, 1 = search left */
851{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000852 xfs_agblock_t new;
853 xfs_agblock_t sdiff;
854 int error;
855 int i;
856
857 /* The good extent is perfect, no need to search. */
858 if (!gdiff)
859 goto out_use_good;
860
861 /*
862 * Look until we find a better one, run out of space or run off the end.
863 */
864 do {
865 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
866 if (error)
867 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100868 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000869 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000870
871 /*
872 * The good extent is closer than this one.
873 */
874 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000875 if (*sbnoa > args->max_agbno)
876 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000877 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000878 goto out_use_good;
879 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000880 if (*sbnoa < args->min_agbno)
881 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000882 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000883 goto out_use_good;
884 }
885
886 /*
887 * Same distance, compare length and pick the best.
888 */
889 if (*slena >= args->minlen) {
890 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
891 xfs_alloc_fix_len(args);
892
893 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200894 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000895 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000896 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000897
898 /*
899 * Choose closer size and invalidate other cursor.
900 */
901 if (sdiff < gdiff)
902 goto out_use_search;
903 goto out_use_good;
904 }
905
906 if (!dir)
907 error = xfs_btree_increment(*scur, 0, &i);
908 else
909 error = xfs_btree_decrement(*scur, 0, &i);
910 if (error)
911 goto error0;
912 } while (i);
913
914out_use_good:
915 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
916 *scur = NULL;
917 return 0;
918
919out_use_search:
920 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
921 *gcur = NULL;
922 return 0;
923
924error0:
925 /* caller invalidates cursors */
926 return error;
927}
928
929/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * Allocate a variable extent near bno in the allocation group agno.
931 * Extent's length (returned in len) will be between minlen and maxlen,
932 * and of the form k * prod + mod unless there's nothing that large.
933 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
934 */
935STATIC int /* error */
936xfs_alloc_ag_vextent_near(
937 xfs_alloc_arg_t *args) /* allocation argument structure */
938{
939 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
940 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
941 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 xfs_agblock_t gtbno; /* start bno of right side entry */
943 xfs_agblock_t gtbnoa; /* aligned ... */
944 xfs_extlen_t gtdiff; /* difference to right side entry */
945 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000946 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 xfs_agblock_t gtnew; /* useful start bno of right side */
948 int error; /* error code */
949 int i; /* result code, temporary */
950 int j; /* result code, temporary */
951 xfs_agblock_t ltbno; /* start bno of left side entry */
952 xfs_agblock_t ltbnoa; /* aligned ... */
953 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000955 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_agblock_t ltnew; /* useful start bno of left side */
957 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000958 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000959#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /*
961 * Randomly don't execute the first algorithm.
962 */
963 int dofirst; /* set to do first algorithm */
964
Akinobu Mitaecb34032013-03-04 21:58:20 +0900965 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000967
Brian Fosterbfe46d42015-05-29 08:53:00 +1000968 /* handle unitialized agbno range so caller doesn't have to */
969 if (!args->min_agbno && !args->max_agbno)
970 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
971 ASSERT(args->min_agbno <= args->max_agbno);
972
973 /* clamp agbno to the range if it's outside */
974 if (args->agbno < args->min_agbno)
975 args->agbno = args->min_agbno;
976 if (args->agbno > args->max_agbno)
977 args->agbno = args->max_agbno;
978
Christoph Hellwige26f0502011-04-24 19:06:15 +0000979restart:
980 bno_cur_lt = NULL;
981 bno_cur_gt = NULL;
982 ltlen = 0;
983 gtlena = 0;
984 ltlena = 0;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /*
987 * Get a cursor for the by-size btree.
988 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100989 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
990 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
993 * See if there are any free extents as big as maxlen.
994 */
995 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
996 goto error0;
997 /*
998 * If none, then pick up the last entry in the tree unless the
999 * tree is empty.
1000 */
1001 if (!i) {
1002 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1003 &ltlen, &i)))
1004 goto error0;
1005 if (i == 0 || ltlen == 0) {
1006 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001007 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009 }
1010 ASSERT(i == 1);
1011 }
1012 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /*
1015 * First algorithm.
1016 * If the requested extent is large wrt the freespaces available
1017 * in this a.g., then the cursor will be pointing to a btree entry
1018 * near the right edge of the tree. If it's in the last btree leaf
1019 * block, then we just examine all the entries in that block
1020 * that are big enough, and pick the best one.
1021 * This is written as a while loop so we can break out of it,
1022 * but we never loop back to the top.
1023 */
1024 while (xfs_btree_islastblock(cnt_cur, 0)) {
1025 xfs_extlen_t bdiff;
1026 int besti=0;
1027 xfs_extlen_t blen=0;
1028 xfs_agblock_t bnew=0;
1029
Dave Chinner63d20d62013-08-12 20:49:50 +10001030#ifdef DEBUG
1031 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033#endif
1034 /*
1035 * Start from the entry that lookup found, sequence through
1036 * all larger free blocks. If we're actually pointing at a
1037 * record smaller than maxlen, go to the start of this block,
1038 * and skip all those smaller than minlen.
1039 */
1040 if (ltlen || args->alignment > 1) {
1041 cnt_cur->bc_ptrs[0] = 1;
1042 do {
1043 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1044 &ltlen, &i)))
1045 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001046 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (ltlen >= args->minlen)
1048 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001049 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto error0;
1051 } while (i);
1052 ASSERT(ltlen >= args->minlen);
1053 if (!i)
1054 break;
1055 }
1056 i = cnt_cur->bc_ptrs[0];
1057 for (j = 1, blen = 0, bdiff = 0;
1058 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001059 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
1061 * For each entry, decide if it's better than
1062 * the previous best entry.
1063 */
1064 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1065 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001066 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001067 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1068 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001069 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001071 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1072 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1074 xfs_alloc_fix_len(args);
1075 ASSERT(args->len >= args->minlen);
1076 if (args->len < blen)
1077 continue;
1078 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001079 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001080 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (ltnew != NULLAGBLOCK &&
1082 (args->len > blen || ltdiff < bdiff)) {
1083 bdiff = ltdiff;
1084 bnew = ltnew;
1085 blen = args->len;
1086 besti = cnt_cur->bc_ptrs[0];
1087 }
1088 }
1089 /*
1090 * It didn't work. We COULD be in a case where
1091 * there's a good record somewhere, so try again.
1092 */
1093 if (blen == 0)
1094 break;
1095 /*
1096 * Point at the best entry, and retrieve it again.
1097 */
1098 cnt_cur->bc_ptrs[0] = besti;
1099 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1100 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001101 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001102 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 args->len = blen;
Christoph Hellwig6b813652017-02-02 08:55:55 +01001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /*
1106 * We are allocating starting at bnew for blen blocks.
1107 */
1108 args->agbno = bnew;
1109 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001110 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /*
1112 * Set up a cursor for the by-bno tree.
1113 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001114 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1115 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Fix up the btree entries.
1118 */
1119 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1120 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1121 goto error0;
1122 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1123 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001124
1125 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127 }
1128 /*
1129 * Second algorithm.
1130 * Search in the by-bno tree to the left and to the right
1131 * simultaneously, until in each case we find a space big enough,
1132 * or run into the edge of the tree. When we run into the edge,
1133 * we deallocate that cursor.
1134 * If both searches succeed, we compare the two spaces and pick
1135 * the better one.
1136 * With alignment, it's possible for both to fail; the upper
1137 * level algorithm that picks allocation groups for allocations
1138 * is not supposed to do this.
1139 */
1140 /*
1141 * Allocate and initialize the cursor for the leftward search.
1142 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001143 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1144 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /*
1146 * Lookup <= bno to find the leftward search's starting point.
1147 */
1148 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1149 goto error0;
1150 if (!i) {
1151 /*
1152 * Didn't find anything; use this cursor for the rightward
1153 * search.
1154 */
1155 bno_cur_gt = bno_cur_lt;
1156 bno_cur_lt = NULL;
1157 }
1158 /*
1159 * Found something. Duplicate the cursor for the rightward search.
1160 */
1161 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1162 goto error0;
1163 /*
1164 * Increment the cursor, so we will point at the entry just right
1165 * of the leftward entry if any, or to the leftmost entry.
1166 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001167 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto error0;
1169 if (!i) {
1170 /*
1171 * It failed, there are no rightward entries.
1172 */
1173 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1174 bno_cur_gt = NULL;
1175 }
1176 /*
1177 * Loop going left with the leftward cursor, right with the
1178 * rightward cursor, until either both directions give up or
1179 * we find an entry at least as big as minlen.
1180 */
1181 do {
1182 if (bno_cur_lt) {
1183 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1184 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001185 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001186 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1187 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001188 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001190 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001192 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 xfs_btree_del_cursor(bno_cur_lt,
1194 XFS_BTREE_NOERROR);
1195 bno_cur_lt = NULL;
1196 }
1197 }
1198 if (bno_cur_gt) {
1199 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1200 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001201 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001202 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1203 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001204 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001206 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001208 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 xfs_btree_del_cursor(bno_cur_gt,
1210 XFS_BTREE_NOERROR);
1211 bno_cur_gt = NULL;
1212 }
1213 }
1214 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * Got both cursors still active, need to find better entry.
1218 */
1219 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (ltlena >= args->minlen) {
1221 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001222 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1225 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001226 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001227 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001228 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001229
1230 error = xfs_alloc_find_best_extent(args,
1231 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001232 ltdiff, &gtbno, &gtlen,
1233 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001234 0 /* search right */);
1235 } else {
1236 ASSERT(gtlena >= args->minlen);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001239 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
1241 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1242 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001243 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001244 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001245 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001246
1247 error = xfs_alloc_find_best_extent(args,
1248 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001249 gtdiff, &ltbno, &ltlen,
1250 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001251 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001253
1254 if (error)
1255 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * If we couldn't get anything, give up.
1260 */
1261 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001262 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1263
Christoph Hellwige26f0502011-04-24 19:06:15 +00001264 if (!forced++) {
1265 trace_xfs_alloc_near_busy(args);
1266 xfs_log_force(args->mp, XFS_LOG_SYNC);
1267 goto restart;
1268 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001269 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 args->agbno = NULLAGBLOCK;
1271 return 0;
1272 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /*
1275 * At this point we have selected a freespace entry, either to the
1276 * left or to the right. If it's on the right, copy all the
1277 * useful variables to the "left" set so we only have one
1278 * copy of this code.
1279 */
1280 if (bno_cur_gt) {
1281 bno_cur_lt = bno_cur_gt;
1282 bno_cur_gt = NULL;
1283 ltbno = gtbno;
1284 ltbnoa = gtbnoa;
1285 ltlen = gtlen;
1286 ltlena = gtlena;
1287 j = 1;
1288 } else
1289 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * Fix up the length and compute the useful address.
1293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1295 xfs_alloc_fix_len(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001297 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001298 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001300 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001301 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001302 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1306 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1307 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001308
1309 if (j)
1310 trace_xfs_alloc_near_greater(args);
1311 else
1312 trace_xfs_alloc_near_lesser(args);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1315 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1316 return 0;
1317
1318 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001319 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (cnt_cur != NULL)
1321 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1322 if (bno_cur_lt != NULL)
1323 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1324 if (bno_cur_gt != NULL)
1325 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1326 return error;
1327}
1328
1329/*
1330 * Allocate a variable extent anywhere in the allocation group agno.
1331 * Extent's length (returned in len) will be between minlen and maxlen,
1332 * and of the form k * prod + mod unless there's nothing that large.
1333 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1334 */
1335STATIC int /* error */
1336xfs_alloc_ag_vextent_size(
1337 xfs_alloc_arg_t *args) /* allocation argument structure */
1338{
1339 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1340 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1341 int error; /* error result */
1342 xfs_agblock_t fbno; /* start of found freespace */
1343 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int i; /* temp status variable */
1345 xfs_agblock_t rbno; /* returned block number */
1346 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001347 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Christoph Hellwige26f0502011-04-24 19:06:15 +00001349restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /*
1351 * Allocate and initialize a cursor for the by-size btree.
1352 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001353 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1354 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Look for an entry >= maxlen+alignment-1 blocks.
1359 */
1360 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1361 args->maxlen + args->alignment - 1, &i)))
1362 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001365 * If none or we have busy extents that we cannot allocate from, then
1366 * we have to settle for a smaller extent. In the case that there are
1367 * no large extents, this will return the last entry in the tree unless
1368 * the tree is empty. In the case that there are only busy large
1369 * extents, this will return the largest small extent unless there
1370 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001372 if (!i || forced > 1) {
1373 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1374 &fbno, &flen, &i);
1375 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto error0;
1377 if (i == 0 || flen == 0) {
1378 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001379 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return 0;
1381 }
1382 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001383 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1384 } else {
1385 /*
1386 * Search for a non-busy extent that is large enough.
1387 * If we are at low space, don't check, or if we fall of
1388 * the end of the btree, turn off the busy check and
1389 * restart.
1390 */
1391 for (;;) {
1392 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1393 if (error)
1394 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001395 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001396
1397 xfs_alloc_compute_aligned(args, fbno, flen,
1398 &rbno, &rlen);
1399
1400 if (rlen >= args->maxlen)
1401 break;
1402
1403 error = xfs_btree_increment(cnt_cur, 0, &i);
1404 if (error)
1405 goto error0;
1406 if (i == 0) {
1407 /*
1408 * Our only valid extents must have been busy.
1409 * Make it unbusy by forcing the log out and
1410 * retrying. If we've been here before, forcing
1411 * the log isn't making the extents available,
1412 * which means they have probably been freed in
1413 * this transaction. In that case, we have to
1414 * give up on them and we'll attempt a minlen
1415 * allocation the next time around.
1416 */
1417 xfs_btree_del_cursor(cnt_cur,
1418 XFS_BTREE_NOERROR);
1419 trace_xfs_alloc_size_busy(args);
1420 if (!forced++)
1421 xfs_log_force(args->mp, XFS_LOG_SYNC);
1422 goto restart;
1423 }
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * In the first case above, we got the last entry in the
1429 * by-size btree. Now we check to see if the space hits maxlen
1430 * once aligned; if not, we search left for something better.
1431 * This can't happen in the second case above.
1432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001434 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1436 if (rlen < args->maxlen) {
1437 xfs_agblock_t bestfbno;
1438 xfs_extlen_t bestflen;
1439 xfs_agblock_t bestrbno;
1440 xfs_extlen_t bestrlen;
1441
1442 bestrlen = rlen;
1443 bestrbno = rbno;
1444 bestflen = flen;
1445 bestfbno = fbno;
1446 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001447 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto error0;
1449 if (i == 0)
1450 break;
1451 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1452 &i)))
1453 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001454 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (flen < bestrlen)
1456 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001457 xfs_alloc_compute_aligned(args, fbno, flen,
1458 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001460 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 (rlen <= flen && rbno + rlen <= fbno + flen),
1462 error0);
1463 if (rlen > bestrlen) {
1464 bestrlen = rlen;
1465 bestrbno = rbno;
1466 bestflen = flen;
1467 bestfbno = fbno;
1468 if (rlen == args->maxlen)
1469 break;
1470 }
1471 }
1472 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1473 &i)))
1474 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001475 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 rlen = bestrlen;
1477 rbno = bestrbno;
1478 flen = bestflen;
1479 fbno = bestfbno;
1480 }
1481 args->wasfromfl = 0;
1482 /*
1483 * Fix up the length.
1484 */
1485 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001486 if (rlen < args->minlen) {
1487 if (!forced++) {
1488 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1489 trace_xfs_alloc_size_busy(args);
1490 xfs_log_force(args->mp, XFS_LOG_SYNC);
1491 goto restart;
1492 }
1493 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001495 xfs_alloc_fix_len(args);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001498 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Allocate and initialize a cursor for the by-block tree.
1501 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001502 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1503 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1505 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1506 goto error0;
1507 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1508 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1509 cnt_cur = bno_cur = NULL;
1510 args->len = rlen;
1511 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001512 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001514 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001516 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
1518
1519error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001520 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (cnt_cur)
1522 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1523 if (bno_cur)
1524 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1525 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001526
1527out_nominleft:
1528 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1529 trace_xfs_alloc_size_nominleft(args);
1530 args->agbno = NULLAGBLOCK;
1531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/*
1535 * Deal with the case where only small freespaces remain.
1536 * Either return the contents of the last freespace record,
1537 * or allocate space from the freelist if there is nothing in the tree.
1538 */
1539STATIC int /* error */
1540xfs_alloc_ag_vextent_small(
1541 xfs_alloc_arg_t *args, /* allocation argument structure */
1542 xfs_btree_cur_t *ccur, /* by-size cursor */
1543 xfs_agblock_t *fbnop, /* result block number */
1544 xfs_extlen_t *flenp, /* result length */
1545 int *stat) /* status: 0-freelist, 1-normal/none */
1546{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001547 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001548 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int error;
1550 xfs_agblock_t fbno;
1551 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int i;
1553
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001554 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto error0;
1556 if (i) {
1557 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1558 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001559 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 /*
1562 * Nothing in the btree, try the freelist. Make sure
1563 * to respect minleft even when pulling from the
1564 * freelist.
1565 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001566 else if (args->minlen == 1 && args->alignment == 1 &&
1567 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001568 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1569 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001570 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1571 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 goto error0;
1573 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001574 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001575 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001576
Dave Chinner292378e2016-09-26 08:21:28 +10001577 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 xfs_buf_t *bp;
1579
1580 bp = xfs_btree_get_bufs(args->mp, args->tp,
1581 args->agno, fbno, 0);
Eric Sandeen67d73f42017-10-09 21:08:06 -07001582 if (!bp) {
1583 error = -EFSCORRUPTED;
1584 goto error0;
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfs_trans_binval(args->tp, bp);
1587 }
1588 args->len = 1;
1589 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001590 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001592 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 error0);
1594 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001595 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001596
1597 /*
1598 * If we're feeding an AGFL block to something that
1599 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001600 * out the OWN_AG rmap and add the block back to
1601 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001602 */
1603 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1604 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1605 fbno, 1, &oinfo);
1606 if (error)
1607 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001608 pag = xfs_perag_get(args->mp, args->agno);
1609 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1610 args->tp, 1);
1611 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 *stat = 0;
1614 return 0;
1615 }
1616 /*
1617 * Nothing in the freelist.
1618 */
1619 else
1620 flen = 0;
1621 }
1622 /*
1623 * Can't allocate from the freelist for some reason.
1624 */
Nathan Scottd432c802006-09-28 11:03:44 +10001625 else {
1626 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /*
1630 * Can't do the allocation, give up.
1631 */
1632 if (flen < args->minlen) {
1633 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001634 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 flen = 0;
1636 }
1637 *fbnop = fbno;
1638 *flenp = flen;
1639 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001640 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return 0;
1642
1643error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001644 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return error;
1646}
1647
1648/*
1649 * Free the extent starting at agno/bno for length.
1650 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001651STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001653 xfs_trans_t *tp,
1654 xfs_buf_t *agbp,
1655 xfs_agnumber_t agno,
1656 xfs_agblock_t bno,
1657 xfs_extlen_t len,
1658 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001659 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1662 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1663 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 xfs_agblock_t gtbno; /* start of right neighbor block */
1665 xfs_extlen_t gtlen; /* length of right neighbor block */
1666 int haveleft; /* have a left neighbor block */
1667 int haveright; /* have a right neighbor block */
1668 int i; /* temp, result code */
1669 xfs_agblock_t ltbno; /* start of left neighbor block */
1670 xfs_extlen_t ltlen; /* length of left neighbor block */
1671 xfs_mount_t *mp; /* mount point struct for filesystem */
1672 xfs_agblock_t nbno; /* new starting block of freespace */
1673 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001674 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Darrick J. Wong673930c2016-08-03 11:33:43 +10001676 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001678
1679 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1680 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1681 if (error)
1682 goto error0;
1683 }
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
1686 * Allocate and initialize a cursor for the by-block btree.
1687 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001688 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
1690 * Look for a neighboring block on the left (lower block numbers)
1691 * that is contiguous with this space.
1692 */
1693 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1694 goto error0;
1695 if (haveleft) {
1696 /*
1697 * There is a block to our left.
1698 */
1699 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1700 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001701 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * It's not contiguous, though.
1704 */
1705 if (ltbno + ltlen < bno)
1706 haveleft = 0;
1707 else {
1708 /*
1709 * If this failure happens the request to free this
1710 * space was invalid, it's (partly) already free.
1711 * Very bad.
1712 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001713 XFS_WANT_CORRUPTED_GOTO(mp,
1714 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 }
1717 /*
1718 * Look for a neighboring block on the right (higher block numbers)
1719 * that is contiguous with this space.
1720 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001721 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto error0;
1723 if (haveright) {
1724 /*
1725 * There is a block to our right.
1726 */
1727 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1728 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001729 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * It's not contiguous, though.
1732 */
1733 if (bno + len < gtbno)
1734 haveright = 0;
1735 else {
1736 /*
1737 * If this failure happens the request to free this
1738 * space was invalid, it's (partly) already free.
1739 * Very bad.
1740 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001741 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
1744 /*
1745 * Now allocate and initialize a cursor for the by-size tree.
1746 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001747 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /*
1749 * Have both left and right contiguous neighbors.
1750 * Merge all three into a single free block.
1751 */
1752 if (haveleft && haveright) {
1753 /*
1754 * Delete the old by-size entry on the left.
1755 */
1756 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1757 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001758 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001759 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
1763 * Delete the old by-size entry on the right.
1764 */
1765 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1766 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001767 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001768 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001770 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * Delete the old by-block entry for the right block.
1773 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001774 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001776 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /*
1778 * Move the by-block cursor back to the left neighbor.
1779 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001780 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783#ifdef DEBUG
1784 /*
1785 * Check that this is the right record: delete didn't
1786 * mangle the cursor.
1787 */
1788 {
1789 xfs_agblock_t xxbno;
1790 xfs_extlen_t xxlen;
1791
1792 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1793 &i)))
1794 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001795 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 i == 1 && xxbno == ltbno && xxlen == ltlen,
1797 error0);
1798 }
1799#endif
1800 /*
1801 * Update remaining by-block entry to the new, joined block.
1802 */
1803 nbno = ltbno;
1804 nlen = len + ltlen + gtlen;
1805 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1806 goto error0;
1807 }
1808 /*
1809 * Have only a left contiguous neighbor.
1810 * Merge it together with the new freespace.
1811 */
1812 else if (haveleft) {
1813 /*
1814 * Delete the old by-size entry on the left.
1815 */
1816 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1817 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001819 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001821 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * Back up the by-block cursor to the left neighbor, and
1824 * update its length.
1825 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001826 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001828 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 nbno = ltbno;
1830 nlen = len + ltlen;
1831 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1832 goto error0;
1833 }
1834 /*
1835 * Have only a right contiguous neighbor.
1836 * Merge it together with the new freespace.
1837 */
1838 else if (haveright) {
1839 /*
1840 * Delete the old by-size entry on the right.
1841 */
1842 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1843 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001844 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001845 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 /*
1849 * Update the starting block and length of the right
1850 * neighbor in the by-block tree.
1851 */
1852 nbno = bno;
1853 nlen = len + gtlen;
1854 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1855 goto error0;
1856 }
1857 /*
1858 * No contiguous neighbors.
1859 * Insert the new freespace into the by-block tree.
1860 */
1861 else {
1862 nbno = bno;
1863 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001864 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001866 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1869 bno_cur = NULL;
1870 /*
1871 * In all cases we need to insert the new freespace in the by-size tree.
1872 */
1873 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1874 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001875 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001876 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1880 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /*
1883 * Update the freespace totals in the ag and superblock.
1884 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001885 pag = xfs_perag_get(mp, agno);
1886 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001887 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001888 xfs_perag_put(pag);
1889 if (error)
1890 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001892 XFS_STATS_INC(mp, xs_freex);
1893 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001894
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001895 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1896 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return 0;
1899
1900 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001901 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1902 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (bno_cur)
1904 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1905 if (cnt_cur)
1906 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1907 return error;
1908}
1909
1910/*
1911 * Visible (exported) allocation/free functions.
1912 * Some of these are used just by xfs_alloc_btree.c and this file.
1913 */
1914
1915/*
1916 * Compute and fill in value of m_ag_maxlevels.
1917 */
1918void
1919xfs_alloc_compute_maxlevels(
1920 xfs_mount_t *mp) /* file system mount structure */
1921{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001922 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1923 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001927 * Find the length of the longest extent in an AG. The 'need' parameter
1928 * specifies how much space we're going to need for the AGFL and the
1929 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1930 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001931 */
1932xfs_extlen_t
1933xfs_alloc_longest_free_extent(
1934 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001935 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001936 xfs_extlen_t need,
1937 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001938{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001939 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001940
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001941 /*
1942 * If the AGFL needs a recharge, we'll have to subtract that from the
1943 * longest extent.
1944 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001945 if (need > pag->pagf_flcount)
1946 delta = need - pag->pagf_flcount;
1947
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001948 /*
1949 * If we cannot maintain others' reservations with space from the
1950 * not-longest freesp extents, we'll have to subtract /that/ from
1951 * the longest extent too.
1952 */
1953 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1954 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1955
1956 /*
1957 * If the longest extent is long enough to satisfy all the
1958 * reservations and AGFL rules in place, we can return this extent.
1959 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001960 if (pag->pagf_longest > delta)
1961 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001962
1963 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01001964 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1965}
1966
Dave Chinner496817b2015-06-22 10:13:30 +10001967unsigned int
1968xfs_alloc_min_freelist(
1969 struct xfs_mount *mp,
1970 struct xfs_perag *pag)
1971{
1972 unsigned int min_free;
1973
1974 /* space needed by-bno freespace btree */
1975 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1976 mp->m_ag_maxlevels);
1977 /* space needed by-size freespace btree */
1978 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1979 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10001980 /* space needed reverse mapping used space btree */
1981 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1982 min_free += min_t(unsigned int,
1983 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
1984 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10001985
1986 return min_free;
1987}
1988
Dave Chinner6cc87642009-03-16 08:29:46 +01001989/*
Dave Chinner72d55282015-06-22 10:04:42 +10001990 * Check if the operation we are fixing up the freelist for should go ahead or
1991 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1992 * is dependent on whether the size and shape of free space available will
1993 * permit the requested allocation to take place.
1994 */
1995static bool
1996xfs_alloc_space_available(
1997 struct xfs_alloc_arg *args,
1998 xfs_extlen_t min_free,
1999 int flags)
2000{
2001 struct xfs_perag *pag = args->pag;
Christoph Hellwige9b77652017-02-02 08:55:56 +01002002 xfs_extlen_t alloc_len, longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002003 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002004 int available;
2005
2006 if (flags & XFS_ALLOC_FLAG_FREEING)
2007 return true;
2008
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002009 reservation = xfs_ag_resv_needed(pag, args->resv);
2010
Dave Chinner72d55282015-06-22 10:04:42 +10002011 /* do we have enough contiguous free space for the allocation? */
Christoph Hellwige9b77652017-02-02 08:55:56 +01002012 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002013 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2014 reservation);
Christoph Hellwige9b77652017-02-02 08:55:56 +01002015 if (longest < alloc_len)
Dave Chinner72d55282015-06-22 10:04:42 +10002016 return false;
2017
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002018 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002019 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Christoph Hellwig6b813652017-02-02 08:55:55 +01002020 reservation - min_free - args->minleft);
Christoph Hellwige9b77652017-02-02 08:55:56 +01002021 if (available < (int)max(args->total, alloc_len))
Dave Chinner72d55282015-06-22 10:04:42 +10002022 return false;
2023
Christoph Hellwig6b813652017-02-02 08:55:55 +01002024 /*
2025 * Clamp maxlen to the amount of free space available for the actual
2026 * extent allocation.
2027 */
2028 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2029 args->maxlen = available;
2030 ASSERT(args->maxlen > 0);
2031 ASSERT(args->maxlen >= args->minlen);
2032 }
2033
Dave Chinner72d55282015-06-22 10:04:42 +10002034 return true;
2035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
Brian Foster0f929c92018-03-15 10:51:58 -07002038 * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2039 * is to detect an agfl header padding mismatch between current and early v5
2040 * kernels. This problem manifests as a 1-slot size difference between the
2041 * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2042 * may also catch variants of agfl count corruption unrelated to padding. Either
2043 * way, we'll reset the agfl and warn the user.
2044 *
2045 * Return true if a reset is required before the agfl can be used, false
2046 * otherwise.
2047 */
2048static bool
2049xfs_agfl_needs_reset(
2050 struct xfs_mount *mp,
2051 struct xfs_agf *agf)
2052{
2053 uint32_t f = be32_to_cpu(agf->agf_flfirst);
2054 uint32_t l = be32_to_cpu(agf->agf_fllast);
2055 uint32_t c = be32_to_cpu(agf->agf_flcount);
2056 int agfl_size = XFS_AGFL_SIZE(mp);
2057 int active;
2058
2059 /* no agfl header on v4 supers */
2060 if (!xfs_sb_version_hascrc(&mp->m_sb))
2061 return false;
2062
2063 /*
2064 * The agf read verifier catches severe corruption of these fields.
2065 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2066 * the verifier allows it.
2067 */
2068 if (f >= agfl_size || l >= agfl_size)
2069 return true;
2070 if (c > agfl_size)
2071 return true;
2072
2073 /*
2074 * Check consistency between the on-disk count and the active range. An
2075 * agfl padding mismatch manifests as an inconsistent flcount.
2076 */
2077 if (c && l >= f)
2078 active = l - f + 1;
2079 else if (c)
2080 active = agfl_size - f + l + 1;
2081 else
2082 active = 0;
2083
2084 return active != c;
2085}
2086
2087/*
2088 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2089 * agfl content cannot be trusted. Warn the user that a repair is required to
2090 * recover leaked blocks.
2091 *
2092 * The purpose of this mechanism is to handle filesystems affected by the agfl
2093 * header padding mismatch problem. A reset keeps the filesystem online with a
2094 * relatively minor free space accounting inconsistency rather than suffer the
2095 * inevitable crash from use of an invalid agfl block.
2096 */
2097static void
2098xfs_agfl_reset(
2099 struct xfs_trans *tp,
2100 struct xfs_buf *agbp,
2101 struct xfs_perag *pag)
2102{
2103 struct xfs_mount *mp = tp->t_mountp;
2104 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
2105
2106 ASSERT(pag->pagf_agflreset);
2107 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
2108
2109 xfs_warn(mp,
2110 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2111 "Please unmount and run xfs_repair.",
2112 pag->pag_agno, pag->pagf_flcount);
2113
2114 agf->agf_flfirst = 0;
2115 agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
2116 agf->agf_flcount = 0;
2117 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
2118 XFS_AGF_FLCOUNT);
2119
2120 pag->pagf_flcount = 0;
2121 pag->pagf_agflreset = false;
2122}
2123
2124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * Decide whether to use this allocation group for this allocation.
2126 * If so, fix up the btree freelist's size.
2127 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002128int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002130 struct xfs_alloc_arg *args, /* allocation argument structure */
2131 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Dave Chinner396503f2015-06-22 10:13:19 +10002133 struct xfs_mount *mp = args->mp;
2134 struct xfs_perag *pag = args->pag;
2135 struct xfs_trans *tp = args->tp;
2136 struct xfs_buf *agbp = NULL;
2137 struct xfs_buf *agflbp = NULL;
2138 struct xfs_alloc_arg targs; /* local allocation arguments */
2139 xfs_agblock_t bno; /* freelist block */
2140 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002141 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002144 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2145 if (error)
2146 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002148 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2149 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002150 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Dave Chinner396503f2015-06-22 10:13:19 +10002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002154 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002155 * If this is a metadata preferred pag and we are user data then try
2156 * somewhere else if we are not being asked to try harder at this
2157 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 */
Dave Chinner292378e2016-09-26 08:21:28 +10002159 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002160 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2161 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002162 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
Dave Chinner496817b2015-06-22 10:13:30 +10002165 need = xfs_alloc_min_freelist(mp, pag);
Christoph Hellwig6b813652017-02-02 08:55:55 +01002166 if (!xfs_alloc_space_available(args, need, flags |
2167 XFS_ALLOC_FLAG_CHECK))
Dave Chinner396503f2015-06-22 10:13:19 +10002168 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Get the a.g. freespace buffer.
2172 * Can fail if we're not blocking on locks, and it's held.
2173 */
Dave Chinner396503f2015-06-22 10:13:19 +10002174 if (!agbp) {
2175 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2176 if (error)
2177 goto out_no_agbp;
2178 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002179 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2180 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002181 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002184
Brian Foster0f929c92018-03-15 10:51:58 -07002185 /* reset a padding mismatched agfl before final free space check */
2186 if (pag->pagf_agflreset)
2187 xfs_agfl_reset(tp, agbp, pag);
2188
Dave Chinner50adbcb2015-06-22 10:04:31 +10002189 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002190 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002191 if (!xfs_alloc_space_available(args, need, flags))
2192 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /*
2195 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002196 *
Dave Chinner396503f2015-06-22 10:13:19 +10002197 * Note that from this point onwards, we will always release the agf and
2198 * agfl buffers on error. This handles the case where we error out and
2199 * the buffers are clean or may not have been joined to the transaction
2200 * and hence need to be released manually. If they have been joined to
2201 * the transaction, then xfs_trans_brelse() will handle them
2202 * appropriately based on the recursion count and dirty state of the
2203 * buffer.
2204 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002205 * XXX (dgc): When we have lots of free space, does this buy us
2206 * anything other than extra overhead when we need to put more blocks
2207 * back on the free list? Maybe we should only do this when space is
2208 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002209 *
2210 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2211 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2212 * updating the rmapbt. Both flags are used in xfs_repair while we're
2213 * rebuilding the rmapbt, and neither are used by the kernel. They're
2214 * both required to ensure that rmaps are correctly recorded for the
2215 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2216 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002218 memset(&targs, 0, sizeof(targs));
2219 if (flags & XFS_ALLOC_FLAG_NORMAP)
2220 xfs_rmap_skip_owner_update(&targs.oinfo);
2221 else
2222 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2223 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002224 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
David Chinner92821e22007-05-24 15:26:31 +10002226 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2227 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002228 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002229 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002230 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002231 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002232 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
Eric Sandeen67d73f42017-10-09 21:08:06 -07002234 if (!bp) {
2235 error = -EFSCORRUPTED;
2236 goto out_agbp_relse;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 xfs_trans_binval(tp, bp);
2239 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 targs.tp = tp;
2242 targs.mp = mp;
2243 targs.agbp = agbp;
2244 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002245 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 targs.type = XFS_ALLOCTYPE_THIS_AG;
2247 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002248 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2249 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002250 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002251
2252 /* Make the freelist longer if it's too short. */
2253 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002255 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002256 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002257
2258 /* Allocate as many blocks as possible at once. */
2259 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002260 if (error)
2261 goto out_agflbp_relse;
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /*
2264 * Stop if we run out. Won't happen if callers are obeying
2265 * the restrictions correctly. Can happen for free calls
2266 * on a completely full ag.
2267 */
Yingping Lud210a282006-06-09 14:55:18 +10002268 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002269 if (flags & XFS_ALLOC_FLAG_FREEING)
2270 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002271 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /*
2274 * Put each allocated block on the list.
2275 */
2276 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002277 error = xfs_alloc_put_freelist(tp, agbp,
2278 agflbp, bno, 0);
2279 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002280 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002283 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 args->agbp = agbp;
2285 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002286
2287out_agflbp_relse:
2288 xfs_trans_brelse(tp, agflbp);
2289out_agbp_relse:
2290 if (agbp)
2291 xfs_trans_brelse(tp, agbp);
2292out_no_agbp:
2293 args->agbp = NULL;
2294 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
2297/*
2298 * Get a block from the freelist.
2299 * Returns with the buffer for the block gotten.
2300 */
2301int /* error */
2302xfs_alloc_get_freelist(
2303 xfs_trans_t *tp, /* transaction pointer */
2304 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002305 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2306 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2310 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002311 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 int error;
David Chinner92821e22007-05-24 15:26:31 +10002313 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002314 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 xfs_perag_t *pag; /* per allocation group data */
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
2318 * Freelist is empty, give up.
2319 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002320 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (!agf->agf_flcount) {
2322 *bnop = NULLAGBLOCK;
2323 return 0;
2324 }
2325 /*
2326 * Read the array of free blocks.
2327 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002328 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2329 &agflbp);
2330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002332
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /*
2335 * Get the block number and update the data structures.
2336 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002337 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2338 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002339 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002341 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002343
2344 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Brian Foster0f929c92018-03-15 10:51:58 -07002345 ASSERT(!pag->pagf_agflreset);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002346 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 xfs_trans_agflist_delta(tp, -1);
2348 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002349 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002350
2351 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2352 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002353 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002354 pag->pagf_btreeblks++;
2355 logflags |= XFS_AGF_BTREEBLKS;
2356 }
2357
David Chinner92821e22007-05-24 15:26:31 +10002358 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *bnop = bno;
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 0;
2362}
2363
2364/*
2365 * Log the given fields from the agf structure.
2366 */
2367void
2368xfs_alloc_log_agf(
2369 xfs_trans_t *tp, /* transaction pointer */
2370 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2371 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2372{
2373 int first; /* first byte offset */
2374 int last; /* last byte offset */
2375 static const short offsets[] = {
2376 offsetof(xfs_agf_t, agf_magicnum),
2377 offsetof(xfs_agf_t, agf_versionnum),
2378 offsetof(xfs_agf_t, agf_seqno),
2379 offsetof(xfs_agf_t, agf_length),
2380 offsetof(xfs_agf_t, agf_roots[0]),
2381 offsetof(xfs_agf_t, agf_levels[0]),
2382 offsetof(xfs_agf_t, agf_flfirst),
2383 offsetof(xfs_agf_t, agf_fllast),
2384 offsetof(xfs_agf_t, agf_flcount),
2385 offsetof(xfs_agf_t, agf_freeblks),
2386 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002387 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002388 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002389 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002390 offsetof(xfs_agf_t, agf_refcount_blocks),
2391 offsetof(xfs_agf_t, agf_refcount_root),
2392 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002393 /* needed so that we don't log the whole rest of the structure: */
2394 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 sizeof(xfs_agf_t)
2396 };
2397
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002398 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2399
Dave Chinner61fe1352013-04-03 16:11:30 +11002400 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2403 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2404}
2405
2406/*
2407 * Interface for inode allocation to force the pag data to be initialized.
2408 */
2409int /* error */
2410xfs_alloc_pagf_init(
2411 xfs_mount_t *mp, /* file system mount structure */
2412 xfs_trans_t *tp, /* transaction pointer */
2413 xfs_agnumber_t agno, /* allocation group number */
2414 int flags) /* XFS_ALLOC_FLAGS_... */
2415{
2416 xfs_buf_t *bp;
2417 int error;
2418
2419 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2420 return error;
2421 if (bp)
2422 xfs_trans_brelse(tp, bp);
2423 return 0;
2424}
2425
2426/*
2427 * Put the block on the freelist for the allocation group.
2428 */
2429int /* error */
2430xfs_alloc_put_freelist(
2431 xfs_trans_t *tp, /* transaction pointer */
2432 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2433 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002434 xfs_agblock_t bno, /* block being freed */
2435 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002438 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 int error;
David Chinner92821e22007-05-24 15:26:31 +10002440 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 xfs_mount_t *mp; /* mount structure */
2442 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002443 __be32 *agfl_bno;
2444 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 agf = XFS_BUF_TO_AGF(agbp);
2447 mp = tp->t_mountp;
2448
2449 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002450 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002452 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002453 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002455
2456 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Brian Foster0f929c92018-03-15 10:51:58 -07002457 ASSERT(!pag->pagf_agflreset);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002458 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 xfs_trans_agflist_delta(tp, 1);
2460 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002461
2462 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2463 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002464 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002465 pag->pagf_btreeblks--;
2466 logflags |= XFS_AGF_BTREEBLKS;
2467 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002468 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002469
David Chinner92821e22007-05-24 15:26:31 +10002470 xfs_alloc_log_agf(tp, agbp, logflags);
2471
Christoph Hellwig16259e72005-11-02 15:11:25 +11002472 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002473
2474 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2475 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002476 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002477 startoff = (char *)blockp - (char *)agflbp->b_addr;
2478
David Chinner92821e22007-05-24 15:26:31 +10002479 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002480
Dave Chinner61fe1352013-04-03 16:11:30 +11002481 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002482 xfs_trans_log_buf(tp, agflbp, startoff,
2483 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return 0;
2485}
2486
Dave Chinner4e0e6042013-04-03 16:11:13 +11002487static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002488xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002489 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002490 struct xfs_buf *bp)
2491 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002492 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002493
Brian Fostera45086e2015-10-12 15:59:25 +11002494 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2495 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002496 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002497 if (!xfs_log_check_lsn(mp,
2498 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2499 return false;
2500 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002501
Dave Chinner4e0e6042013-04-03 16:11:13 +11002502 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2503 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2504 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2505 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2506 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2507 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2508 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002509
Darrick J. Wongb85f3242017-01-09 16:38:51 +01002510 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2511 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2512 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002513 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2514 return false;
2515
Darrick J. Wongb8704942016-08-03 11:30:32 +10002516 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongb85f3242017-01-09 16:38:51 +01002517 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2518 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wongb8704942016-08-03 11:30:32 +10002519 return false;
2520
Dave Chinner5d5f5272012-11-14 17:44:56 +11002521 /*
2522 * during growfs operations, the perag is not fully initialised,
2523 * so we can't use it for any useful checking. growfs ensures we can't
2524 * use it by using uncached buffers that don't have the perag attached
2525 * so we can detect and avoid this problem.
2526 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002527 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2528 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002529
Dave Chinner4e0e6042013-04-03 16:11:13 +11002530 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2531 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2532 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002533
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002534 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongb85f3242017-01-09 16:38:51 +01002535 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2536 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002537 return false;
2538
Dave Chinner4e0e6042013-04-03 16:11:13 +11002539 return true;;
2540
Dave Chinner612cfbf2012-11-14 17:52:32 +11002541}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002542
Dave Chinner1813dd62012-11-14 17:54:40 +11002543static void
2544xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002545 struct xfs_buf *bp)
2546{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002547 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002548
Eric Sandeence5028c2014-02-27 15:23:10 +11002549 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2550 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002551 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002552 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2553 XFS_ERRTAG_ALLOC_READ_AGF,
2554 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002555 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002556
2557 if (bp->b_error)
2558 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002559}
2560
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002561static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002562xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002563 struct xfs_buf *bp)
2564{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002565 struct xfs_mount *mp = bp->b_target->bt_mount;
2566 struct xfs_buf_log_item *bip = bp->b_fspriv;
2567
2568 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002569 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002570 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002571 return;
2572 }
2573
2574 if (!xfs_sb_version_hascrc(&mp->m_sb))
2575 return;
2576
2577 if (bip)
2578 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2579
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002580 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002581}
2582
Dave Chinner1813dd62012-11-14 17:54:40 +11002583const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002584 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002585 .verify_read = xfs_agf_read_verify,
2586 .verify_write = xfs_agf_write_verify,
2587};
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589/*
2590 * Read in the allocation group header (free/alloc section).
2591 */
2592int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002593xfs_read_agf(
2594 struct xfs_mount *mp, /* mount point structure */
2595 struct xfs_trans *tp, /* transaction pointer */
2596 xfs_agnumber_t agno, /* allocation group number */
2597 int flags, /* XFS_BUF_ */
2598 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int error;
2601
Dave Chinnerd1230312013-11-01 15:27:19 +11002602 trace_xfs_read_agf(mp, agno);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 ASSERT(agno != NULLAGNUMBER);
2605 error = xfs_trans_read_buf(
2606 mp, tp, mp->m_ddev_targp,
2607 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002608 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (error)
2610 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002611 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002613
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002614 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002615 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002616 return 0;
2617}
2618
2619/*
2620 * Read in the allocation group header (free/alloc section).
2621 */
2622int /* error */
2623xfs_alloc_read_agf(
2624 struct xfs_mount *mp, /* mount point structure */
2625 struct xfs_trans *tp, /* transaction pointer */
2626 xfs_agnumber_t agno, /* allocation group number */
2627 int flags, /* XFS_ALLOC_FLAG_... */
2628 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2629{
2630 struct xfs_agf *agf; /* ag freelist header */
2631 struct xfs_perag *pag; /* per allocation group data */
2632 int error;
2633
Dave Chinnerd1230312013-11-01 15:27:19 +11002634 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002635
Dave Chinnerd1230312013-11-01 15:27:19 +11002636 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002637 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002638 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002639 bpp);
2640 if (error)
2641 return error;
2642 if (!*bpp)
2643 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002644 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002645
2646 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002647 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002649 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002650 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002651 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2652 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002654 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002656 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002657 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2658 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002659 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002660 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002661 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002662 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 pag->pagf_init = 1;
Brian Foster0f929c92018-03-15 10:51:58 -07002664 pag->pagf_agflreset = xfs_agfl_needs_reset(mp, agf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666#ifdef DEBUG
2667 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002668 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002669 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002670 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2671 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002673 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002675 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002678 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return 0;
2680}
2681
2682/*
2683 * Allocate an extent (variable-size).
2684 * Depending on the allocation type, we either look in a single allocation
2685 * group or loop over the allocation groups to find the result.
2686 */
2687int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002688xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 xfs_alloc_arg_t *args) /* allocation argument structure */
2690{
2691 xfs_agblock_t agsize; /* allocation group size */
2692 int error;
2693 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 xfs_mount_t *mp; /* mount structure pointer */
2695 xfs_agnumber_t sagno; /* starting allocation group number */
2696 xfs_alloctype_t type; /* input allocation type */
2697 int bump_rotor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2699
2700 mp = args->mp;
2701 type = args->otype = args->type;
2702 args->agbno = NULLAGBLOCK;
2703 /*
2704 * Just fix this up, for the case where the last a.g. is shorter
2705 * (or there's only one a.g.) and the caller couldn't easily figure
2706 * that out (xfs_bmap_alloc).
2707 */
2708 agsize = mp->m_sb.sb_agblocks;
2709 if (args->maxlen > agsize)
2710 args->maxlen = agsize;
2711 if (args->alignment == 0)
2712 args->alignment = 1;
2713 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2714 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2715 ASSERT(args->minlen <= args->maxlen);
2716 ASSERT(args->minlen <= agsize);
2717 ASSERT(args->mod < args->prod);
2718 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2719 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2720 args->minlen > args->maxlen || args->minlen > agsize ||
2721 args->mod >= args->prod) {
2722 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002723 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return 0;
2725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 switch (type) {
2728 case XFS_ALLOCTYPE_THIS_AG:
2729 case XFS_ALLOCTYPE_NEAR_BNO:
2730 case XFS_ALLOCTYPE_THIS_BNO:
2731 /*
2732 * These three force us into a single a.g.
2733 */
2734 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002735 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 error = xfs_alloc_fix_freelist(args, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002738 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 goto error0;
2740 }
2741 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002742 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 break;
2744 }
2745 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2746 if ((error = xfs_alloc_ag_vextent(args)))
2747 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 break;
2749 case XFS_ALLOCTYPE_START_BNO:
2750 /*
2751 * Try near allocation first, then anywhere-in-ag after
2752 * the first a.g. fails.
2753 */
Dave Chinner292378e2016-09-26 08:21:28 +10002754 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2756 args->fsbno = XFS_AGB_TO_FSB(mp,
2757 ((mp->m_agfrotor / rotorstep) %
2758 mp->m_sb.sb_agcount), 0);
2759 bump_rotor = 1;
2760 }
2761 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2762 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2763 /* FALLTHROUGH */
2764 case XFS_ALLOCTYPE_ANY_AG:
2765 case XFS_ALLOCTYPE_START_AG:
2766 case XFS_ALLOCTYPE_FIRST_AG:
2767 /*
2768 * Rotate through the allocation groups looking for a winner.
2769 */
2770 if (type == XFS_ALLOCTYPE_ANY_AG) {
2771 /*
2772 * Start with the last place we left off.
2773 */
2774 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2775 mp->m_sb.sb_agcount;
2776 args->type = XFS_ALLOCTYPE_THIS_AG;
2777 flags = XFS_ALLOC_FLAG_TRYLOCK;
2778 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2779 /*
2780 * Start with allocation group given by bno.
2781 */
2782 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2783 args->type = XFS_ALLOCTYPE_THIS_AG;
2784 sagno = 0;
2785 flags = 0;
2786 } else {
2787 if (type == XFS_ALLOCTYPE_START_AG)
2788 args->type = XFS_ALLOCTYPE_THIS_AG;
2789 /*
2790 * Start with the given allocation group.
2791 */
2792 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2793 flags = XFS_ALLOC_FLAG_TRYLOCK;
2794 }
2795 /*
2796 * Loop over allocation groups twice; first time with
2797 * trylock set, second time without.
2798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002800 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 error = xfs_alloc_fix_freelist(args, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002803 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 goto error0;
2805 }
2806 /*
2807 * If we get a buffer back then the allocation will fly.
2808 */
2809 if (args->agbp) {
2810 if ((error = xfs_alloc_ag_vextent(args)))
2811 goto error0;
2812 break;
2813 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002814
2815 trace_xfs_alloc_vextent_loopfailed(args);
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /*
2818 * Didn't work, figure out the next iteration.
2819 */
2820 if (args->agno == sagno &&
2821 type == XFS_ALLOCTYPE_START_BNO)
2822 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002823 /*
2824 * For the first allocation, we can try any AG to get
2825 * space. However, if we already have allocated a
2826 * block, we don't want to try AGs whose number is below
2827 * sagno. Otherwise, we may end up with out-of-order
2828 * locking of AGF, which might cause deadlock.
2829 */
2830 if (++(args->agno) == mp->m_sb.sb_agcount) {
2831 if (args->firstblock != NULLFSBLOCK)
2832 args->agno = sagno;
2833 else
2834 args->agno = 0;
2835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /*
2837 * Reached the starting a.g., must either be done
2838 * or switch to non-trylock mode.
2839 */
2840 if (args->agno == sagno) {
Christoph Hellwigc63f4d32017-02-02 08:55:54 +01002841 if (flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002843 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 break;
2845 }
Christoph Hellwigc63f4d32017-02-02 08:55:54 +01002846
2847 flags = 0;
2848 if (type == XFS_ALLOCTYPE_START_BNO) {
2849 args->agbno = XFS_FSB_TO_AGBNO(mp,
2850 args->fsbno);
2851 args->type = XFS_ALLOCTYPE_NEAR_BNO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002854 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2857 if (args->agno == sagno)
2858 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2859 (mp->m_sb.sb_agcount * rotorstep);
2860 else
2861 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2862 (mp->m_sb.sb_agcount * rotorstep);
2863 }
2864 break;
2865 default:
2866 ASSERT(0);
2867 /* NOTREACHED */
2868 }
2869 if (args->agbno == NULLAGBLOCK)
2870 args->fsbno = NULLFSBLOCK;
2871 else {
2872 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2873#ifdef DEBUG
2874 ASSERT(args->len >= args->minlen);
2875 ASSERT(args->len <= args->maxlen);
2876 ASSERT(args->agbno % args->alignment == 0);
2877 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2878 args->len);
2879#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002880
2881 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002882 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002883 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2884 if (error)
2885 goto error0;
2886 }
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002889 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return 0;
2891error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002892 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return error;
2894}
2895
Dave Chinner4d89e202016-06-21 11:53:28 +10002896/* Ensure that the freelist is at full capacity. */
2897int
2898xfs_free_extent_fix_freelist(
2899 struct xfs_trans *tp,
2900 xfs_agnumber_t agno,
2901 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Dave Chinner4d89e202016-06-21 11:53:28 +10002903 struct xfs_alloc_arg args;
2904 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Dave Chinner4d89e202016-06-21 11:53:28 +10002906 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 args.tp = tp;
2908 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002909 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002910
2911 /*
2912 * validate that the block number is legal - the enables us to detect
2913 * and handle a silent filesystem corruption rather than crashing.
2914 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002915 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002916 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002917
Dave Chinnera862e0f2010-01-11 11:47:41 +00002918 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002919 ASSERT(args.pag);
2920
2921 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2922 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002923 goto out;
2924
2925 *agbp = args.agbp;
2926out:
2927 xfs_perag_put(args.pag);
2928 return error;
2929}
2930
2931/*
2932 * Free an extent.
2933 * Just break up the extent address and hand off to xfs_free_ag_extent
2934 * after fixing up the freelist.
2935 */
2936int /* error */
2937xfs_free_extent(
2938 struct xfs_trans *tp, /* transaction pointer */
2939 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002940 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002941 struct xfs_owner_info *oinfo, /* extent owner */
2942 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002943{
2944 struct xfs_mount *mp = tp->t_mountp;
2945 struct xfs_buf *agbp;
2946 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2947 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2948 int error;
2949
2950 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002951 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002952
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002953 if (XFS_TEST_ERROR(false, mp,
2954 XFS_ERRTAG_FREE_EXTENT,
2955 XFS_RANDOM_FREE_EXTENT))
2956 return -EIO;
2957
Dave Chinner4d89e202016-06-21 11:53:28 +10002958 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2959 if (error)
2960 return error;
2961
2962 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002963
2964 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002965 XFS_WANT_CORRUPTED_GOTO(mp,
2966 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2967 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002968
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002969 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002970 if (error)
2971 goto err;
2972
2973 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2974 return 0;
2975
2976err:
2977 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return error;
2979}