blob: 0a46f8488b8dc3791090d19ebbb51a54de3d3a4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
Dave Chinner70a98832013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_btree.h"
Darrick J. Wong673930c2016-08-03 11:33:43 +100030#include "xfs_rmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000033#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110035#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110037#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110038#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110039#include "xfs_log.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100040#include "xfs_ag_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Chinnerc999a222012-03-22 05:15:07 +000042struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
45
46#define XFSA_FIXUP_BNO_OK 1
47#define XFSA_FIXUP_CNT_OK 2
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
52STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000053 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070055unsigned int
56xfs_refc_block(
57 struct xfs_mount *mp)
58{
59 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
60 return XFS_RMAP_BLOCK(mp) + 1;
61 if (xfs_sb_version_hasfinobt(&mp->m_sb))
62 return XFS_FIBT_BLOCK(mp) + 1;
63 return XFS_IBT_BLOCK(mp) + 1;
64}
65
Darrick J. Wong80180262016-08-03 11:31:47 +100066xfs_extlen_t
67xfs_prealloc_blocks(
68 struct xfs_mount *mp)
69{
Darrick J. Wongaf30dfa2016-10-03 09:11:17 -070070 if (xfs_sb_version_hasreflink(&mp->m_sb))
71 return xfs_refc_block(mp) + 1;
Darrick J. Wong80180262016-08-03 11:31:47 +100072 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
73 return XFS_RMAP_BLOCK(mp) + 1;
74 if (xfs_sb_version_hasfinobt(&mp->m_sb))
75 return XFS_FIBT_BLOCK(mp) + 1;
76 return XFS_IBT_BLOCK(mp) + 1;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
Darrick J. Wong52548852016-08-03 11:38:24 +100080 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
81 * AGF buffer (PV 947395), we place constraints on the relationship among
82 * actual allocations for data blocks, freelist blocks, and potential file data
83 * bmap btree blocks. However, these restrictions may result in no actual space
84 * allocated for a delayed extent, for example, a data block in a certain AG is
85 * allocated but there is no additional block for the additional bmap btree
86 * block due to a split of the bmap btree of the file. The result of this may
87 * lead to an infinite loop when the file gets flushed to disk and all delayed
88 * extents need to be actually allocated. To get around this, we explicitly set
89 * aside a few blocks which will not be reserved in delayed allocation.
90 *
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100091 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
92 * potential split of the file's bmap btree.
Darrick J. Wong52548852016-08-03 11:38:24 +100093 */
94unsigned int
95xfs_alloc_set_aside(
96 struct xfs_mount *mp)
97{
Christoph Hellwig5149fd32017-01-09 13:36:30 -080098 return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
Darrick J. Wong52548852016-08-03 11:38:24 +100099}
100
101/*
102 * When deciding how much space to allocate out of an AG, we limit the
103 * allocation maximum size to the size the AG. However, we cannot use all the
104 * blocks in the AG - some are permanently used by metadata. These
105 * blocks are generally:
106 * - the AG superblock, AGF, AGI and AGFL
107 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
108 * the AGI free inode and rmap btree root blocks.
109 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
110 * - the rmapbt root block
111 *
112 * The AG headers are sector sized, so the amount of space they take up is
113 * dependent on filesystem geometry. The others are all single blocks.
114 */
115unsigned int
116xfs_alloc_ag_max_usable(
117 struct xfs_mount *mp)
118{
119 unsigned int blocks;
120
121 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
122 blocks += XFS_ALLOC_AGFL_RESERVE;
123 blocks += 3; /* AGF, AGI btree root blocks */
124 if (xfs_sb_version_hasfinobt(&mp->m_sb))
125 blocks++; /* finobt root block */
126 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
127 blocks++; /* rmap root block */
Darrick J. Wongd0e853f2016-10-03 09:11:24 -0700128 if (xfs_sb_version_hasreflink(&mp->m_sb))
129 blocks++; /* refcount root block */
Darrick J. Wong52548852016-08-03 11:38:24 +1000130
131 return mp->m_sb.sb_agblocks - blocks;
132}
133
134/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100135 * Lookup the record equal to [bno, len] in the btree given by cur.
136 */
137STATIC int /* error */
138xfs_alloc_lookup_eq(
139 struct xfs_btree_cur *cur, /* btree cursor */
140 xfs_agblock_t bno, /* starting block of extent */
141 xfs_extlen_t len, /* length of extent */
142 int *stat) /* success/failure */
143{
144 cur->bc_rec.a.ar_startblock = bno;
145 cur->bc_rec.a.ar_blockcount = len;
146 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
147}
148
149/*
150 * Lookup the first record greater than or equal to [bno, len]
151 * in the btree given by cur.
152 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000153int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100154xfs_alloc_lookup_ge(
155 struct xfs_btree_cur *cur, /* btree cursor */
156 xfs_agblock_t bno, /* starting block of extent */
157 xfs_extlen_t len, /* length of extent */
158 int *stat) /* success/failure */
159{
160 cur->bc_rec.a.ar_startblock = bno;
161 cur->bc_rec.a.ar_blockcount = len;
162 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
163}
164
165/*
166 * Lookup the first record less than or equal to [bno, len]
167 * in the btree given by cur.
168 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000169static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100170xfs_alloc_lookup_le(
171 struct xfs_btree_cur *cur, /* btree cursor */
172 xfs_agblock_t bno, /* starting block of extent */
173 xfs_extlen_t len, /* length of extent */
174 int *stat) /* success/failure */
175{
176 cur->bc_rec.a.ar_startblock = bno;
177 cur->bc_rec.a.ar_blockcount = len;
178 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
179}
180
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100181/*
182 * Update the record referred to by cur to the value given
183 * by [bno, len].
184 * This either works (return 0) or gets an EFSCORRUPTED error.
185 */
186STATIC int /* error */
187xfs_alloc_update(
188 struct xfs_btree_cur *cur, /* btree cursor */
189 xfs_agblock_t bno, /* starting block of extent */
190 xfs_extlen_t len) /* length of extent */
191{
192 union xfs_btree_rec rec;
193
194 rec.alloc.ar_startblock = cpu_to_be32(bno);
195 rec.alloc.ar_blockcount = cpu_to_be32(len);
196 return xfs_btree_update(cur, &rec);
197}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100198
199/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100200 * Get the data from the pointed-to record.
201 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000202int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100203xfs_alloc_get_rec(
204 struct xfs_btree_cur *cur, /* btree cursor */
205 xfs_agblock_t *bno, /* output: starting block of extent */
206 xfs_extlen_t *len, /* output: length of extent */
207 int *stat) /* output: success/failure */
208{
209 union xfs_btree_rec *rec;
210 int error;
211
212 error = xfs_btree_get_rec(cur, &rec, stat);
213 if (!error && *stat == 1) {
214 *bno = be32_to_cpu(rec->alloc.ar_startblock);
215 *len = be32_to_cpu(rec->alloc.ar_blockcount);
216 }
217 return error;
218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Compute aligned version of the found extent.
222 * Takes alignment and min length into account.
223 */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 args->len = rlen;
366}
367
368/*
369 * Fix up length if there is too little space left in the a.g.
370 * Return 1 if ok, 0 if too little, should give up.
371 */
372STATIC int
373xfs_alloc_fix_minleft(
374 xfs_alloc_arg_t *args) /* allocation argument structure */
375{
376 xfs_agf_t *agf; /* a.g. freelist header */
377 int diff; /* free space difference */
378
379 if (args->minleft == 0)
380 return 1;
381 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100382 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 - args->len - args->minleft;
384 if (diff >= 0)
385 return 1;
386 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100387 /* casts to (int) catch length underflows */
388 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390 args->agbno = NULLAGBLOCK;
391 return 0;
392}
393
394/*
395 * Update the two btrees, logically removing from freespace the extent
396 * starting at rbno, rlen blocks. The extent is contained within the
397 * actual (current) free extent fbno for flen blocks.
398 * Flags are passed in indicating whether the cursors are set to the
399 * relevant records.
400 */
401STATIC int /* error code */
402xfs_alloc_fixup_trees(
403 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
404 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
405 xfs_agblock_t fbno, /* starting block of free extent */
406 xfs_extlen_t flen, /* length of free extent */
407 xfs_agblock_t rbno, /* starting block of returned extent */
408 xfs_extlen_t rlen, /* length of returned extent */
409 int flags) /* flags, XFSA_FIXUP_... */
410{
411 int error; /* error code */
412 int i; /* operation results */
413 xfs_agblock_t nfbno1; /* first new free startblock */
414 xfs_agblock_t nfbno2; /* second new free startblock */
415 xfs_extlen_t nflen1=0; /* first new free length */
416 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100417 struct xfs_mount *mp;
418
419 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
422 * Look up the record in the by-size tree if necessary.
423 */
424 if (flags & XFSA_FIXUP_CNT_OK) {
425#ifdef DEBUG
426 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
427 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100428 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 i == 1 && nfbno1 == fbno && nflen1 == flen);
430#endif
431 } else {
432 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
433 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100434 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 /*
437 * Look up the record in the by-block tree if necessary.
438 */
439 if (flags & XFSA_FIXUP_BNO_OK) {
440#ifdef DEBUG
441 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
442 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100443 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 i == 1 && nfbno1 == fbno && nflen1 == flen);
445#endif
446 } else {
447 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
448 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100449 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100452#ifdef DEBUG
453 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
454 struct xfs_btree_block *bnoblock;
455 struct xfs_btree_block *cntblock;
456
457 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
458 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
459
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100460 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100461 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * Deal with all four cases: the allocated record is contained
467 * within the freespace record, so we can have new freespace
468 * at either (or both) end, or no freespace remaining.
469 */
470 if (rbno == fbno && rlen == flen)
471 nfbno1 = nfbno2 = NULLAGBLOCK;
472 else if (rbno == fbno) {
473 nfbno1 = rbno + rlen;
474 nflen1 = flen - rlen;
475 nfbno2 = NULLAGBLOCK;
476 } else if (rbno + rlen == fbno + flen) {
477 nfbno1 = fbno;
478 nflen1 = flen - rlen;
479 nfbno2 = NULLAGBLOCK;
480 } else {
481 nfbno1 = fbno;
482 nflen1 = rbno - fbno;
483 nfbno2 = rbno + rlen;
484 nflen2 = (fbno + flen) - nfbno2;
485 }
486 /*
487 * Delete the entry from the by-size btree.
488 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100489 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100491 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Add new by-size btree entry(s).
494 */
495 if (nfbno1 != NULLAGBLOCK) {
496 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
497 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100498 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100499 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100501 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 if (nfbno2 != NULLAGBLOCK) {
504 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
505 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100506 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100507 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100509 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 /*
512 * Fix up the by-block btree entry(s).
513 */
514 if (nfbno1 == NULLAGBLOCK) {
515 /*
516 * No remaining freespace, just delete the by-block tree entry.
517 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100518 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100520 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else {
522 /*
523 * Update the by-block entry to start later|be shorter.
524 */
525 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
526 return error;
527 }
528 if (nfbno2 != NULLAGBLOCK) {
529 /*
530 * 2 resulting free entries, need to add one.
531 */
532 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
533 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100534 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100535 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100537 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 return 0;
540}
541
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100542static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100543xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100544 struct xfs_buf *bp)
545{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100546 struct xfs_mount *mp = bp->b_target->bt_mount;
547 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100548 int i;
549
Eric Sandeence748ea2015-07-29 11:53:31 +1000550 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100551 return false;
552 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
553 return false;
554 /*
555 * during growfs operations, the perag is not fully initialised,
556 * so we can't use it for any useful checking. growfs ensures we can't
557 * use it by using uncached buffers that don't have the perag attached
558 * so we can detect and avoid this problem.
559 */
560 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
561 return false;
562
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100563 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100564 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100565 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100566 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100567 }
Brian Fostera45086e2015-10-12 15:59:25 +1100568
569 return xfs_log_check_lsn(mp,
570 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100571}
572
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100573static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100574xfs_agfl_read_verify(
575 struct xfs_buf *bp)
576{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100577 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100578
579 /*
580 * There is no verification of non-crc AGFLs because mkfs does not
581 * initialise the AGFL to zero or NULL. Hence the only valid part of the
582 * AGFL is what the AGF says is active. We can't get to the AGF, so we
583 * can't verify just those entries are valid.
584 */
585 if (!xfs_sb_version_hascrc(&mp->m_sb))
586 return;
587
Eric Sandeence5028c2014-02-27 15:23:10 +1100588 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000589 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100590 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000591 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100592
593 if (bp->b_error)
594 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100595}
596
597static void
598xfs_agfl_write_verify(
599 struct xfs_buf *bp)
600{
601 struct xfs_mount *mp = bp->b_target->bt_mount;
602 struct xfs_buf_log_item *bip = bp->b_fspriv;
603
604 /* no verification of non-crc AGFLs */
605 if (!xfs_sb_version_hascrc(&mp->m_sb))
606 return;
607
608 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000609 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100610 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100611 return;
612 }
613
614 if (bip)
615 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
616
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100617 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100618}
619
Dave Chinner1813dd62012-11-14 17:54:40 +1100620const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100621 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100622 .verify_read = xfs_agfl_read_verify,
623 .verify_write = xfs_agfl_write_verify,
624};
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Read in the allocation group free block array.
628 */
629STATIC int /* error */
630xfs_alloc_read_agfl(
631 xfs_mount_t *mp, /* mount point structure */
632 xfs_trans_t *tp, /* transaction pointer */
633 xfs_agnumber_t agno, /* allocation group number */
634 xfs_buf_t **bpp) /* buffer for the ag free block array */
635{
636 xfs_buf_t *bp; /* return value */
637 int error;
638
639 ASSERT(agno != NULLAGNUMBER);
640 error = xfs_trans_read_buf(
641 mp, tp, mp->m_ddev_targp,
642 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100643 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (error)
645 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000646 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *bpp = bp;
648 return 0;
649}
650
Christoph Hellwigecb69282011-03-04 12:59:55 +0000651STATIC int
652xfs_alloc_update_counters(
653 struct xfs_trans *tp,
654 struct xfs_perag *pag,
655 struct xfs_buf *agbp,
656 long len)
657{
658 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
659
660 pag->pagf_freeblks += len;
661 be32_add_cpu(&agf->agf_freeblks, len);
662
663 xfs_trans_agblocks_delta(tp, len);
664 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
665 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000666 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000667
668 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
669 return 0;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/*
673 * Allocation group level functions.
674 */
675
676/*
677 * Allocate a variable extent in the allocation group agno.
678 * Type and bno are used to determine where in the allocation group the
679 * extent will start.
680 * Extent's length (returned in *len) will be between minlen and maxlen,
681 * and of the form k * prod + mod unless there's nothing that large.
682 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
683 */
684STATIC int /* error */
685xfs_alloc_ag_vextent(
686 xfs_alloc_arg_t *args) /* argument structure for allocation */
687{
688 int error=0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000689 xfs_extlen_t reservation;
690 xfs_extlen_t oldmax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 ASSERT(args->minlen > 0);
693 ASSERT(args->maxlen > 0);
694 ASSERT(args->minlen <= args->maxlen);
695 ASSERT(args->mod < args->prod);
696 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000697
698 /*
699 * Clamp maxlen to the amount of free space minus any reservations
700 * that have been made.
701 */
702 oldmax = args->maxlen;
703 reservation = xfs_ag_resv_needed(args->pag, args->resv);
704 if (args->maxlen > args->pag->pagf_freeblks - reservation)
705 args->maxlen = args->pag->pagf_freeblks - reservation;
706 if (args->maxlen == 0) {
707 args->agbno = NULLAGBLOCK;
708 args->maxlen = oldmax;
709 return 0;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Branch to correct routine based on the type.
714 */
715 args->wasfromfl = 0;
716 switch (args->type) {
717 case XFS_ALLOCTYPE_THIS_AG:
718 error = xfs_alloc_ag_vextent_size(args);
719 break;
720 case XFS_ALLOCTYPE_NEAR_BNO:
721 error = xfs_alloc_ag_vextent_near(args);
722 break;
723 case XFS_ALLOCTYPE_THIS_BNO:
724 error = xfs_alloc_ag_vextent_exact(args);
725 break;
726 default:
727 ASSERT(0);
728 /* NOTREACHED */
729 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000730
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000731 args->maxlen = oldmax;
732
Christoph Hellwigecb69282011-03-04 12:59:55 +0000733 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Christoph Hellwigecb69282011-03-04 12:59:55 +0000736 ASSERT(args->len >= args->minlen);
737 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000738 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000739 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Darrick J. Wong673930c2016-08-03 11:33:43 +1000741 /* if not file data, insert new block into the reverse map btree */
742 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
743 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
744 args->agbno, args->len, &args->oinfo);
745 if (error)
746 return error;
747 }
748
Christoph Hellwigecb69282011-03-04 12:59:55 +0000749 if (!args->wasfromfl) {
750 error = xfs_alloc_update_counters(args->tp, args->pag,
751 args->agbp,
752 -((long)(args->len)));
753 if (error)
754 return error;
755
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000756 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000757 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000759
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000760 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000761
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100762 XFS_STATS_INC(args->mp, xs_allocx);
763 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000764 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
768 * Allocate a variable extent at exactly agno/bno.
769 * Extent's length (returned in *len) will be between minlen and maxlen,
770 * and of the form k * prod + mod unless there's nothing that large.
771 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
772 */
773STATIC int /* error */
774xfs_alloc_ag_vextent_exact(
775 xfs_alloc_arg_t *args) /* allocation argument structure */
776{
777 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
778 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int error;
780 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000782 xfs_agblock_t tbno; /* start block of trimmed extent */
783 xfs_extlen_t tlen; /* length of trimmed extent */
784 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * Allocate/initialize a cursor for the by-number freespace btree.
791 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100792 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000793 args->agno, XFS_BTNUM_BNO);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
797 * Look for the closest free block <= bno, it must contain bno
798 * if any free block does.
799 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000800 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000803 if (!i)
804 goto not_found;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Grab the freespace record.
808 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000809 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
810 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100812 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000816 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000818 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000819
820 /*
821 * Give up if the start of the extent is busy, or the freespace isn't
822 * long enough for the minimum request.
823 */
824 if (tbno > args->agbno)
825 goto not_found;
826 if (tlen < args->minlen)
827 goto not_found;
828 tend = tbno + tlen;
829 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000830 goto not_found;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * End of extent will be smaller of the freespace end and the
834 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000835 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * Fix the length according to mod and prod if given.
837 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000838 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
839 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000841 if (!xfs_alloc_fix_minleft(args))
842 goto not_found;
843
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000844 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000847 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * Allocate/initialize a cursor for the by-size btree.
849 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100850 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
851 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100853 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000854 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
855 args->len, XFSA_FIXUP_BNO_OK);
856 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
858 goto error0;
859 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
862 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000865 trace_xfs_alloc_exact_done(args);
866 return 0;
867
868not_found:
869 /* Didn't find it, return null. */
870 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
871 args->agbno = NULLAGBLOCK;
872 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
874
875error0:
876 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000877 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return error;
879}
880
881/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000882 * Search the btree in a given direction via the search cursor and compare
883 * the records found against the good extent we've already found.
884 */
885STATIC int
886xfs_alloc_find_best_extent(
887 struct xfs_alloc_arg *args, /* allocation argument structure */
888 struct xfs_btree_cur **gcur, /* good cursor */
889 struct xfs_btree_cur **scur, /* searching cursor */
890 xfs_agblock_t gdiff, /* difference for search comparison */
891 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000892 xfs_extlen_t *slen, /* extent length */
893 xfs_agblock_t *sbnoa, /* aligned extent found by search */
894 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000895 int dir) /* 0 = search right, 1 = search left */
896{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000897 xfs_agblock_t new;
898 xfs_agblock_t sdiff;
899 int error;
900 int i;
901
902 /* The good extent is perfect, no need to search. */
903 if (!gdiff)
904 goto out_use_good;
905
906 /*
907 * Look until we find a better one, run out of space or run off the end.
908 */
909 do {
910 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
911 if (error)
912 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100913 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000914 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000915
916 /*
917 * The good extent is closer than this one.
918 */
919 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000920 if (*sbnoa > args->max_agbno)
921 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000922 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000923 goto out_use_good;
924 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000925 if (*sbnoa < args->min_agbno)
926 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000927 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000928 goto out_use_good;
929 }
930
931 /*
932 * Same distance, compare length and pick the best.
933 */
934 if (*slena >= args->minlen) {
935 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
936 xfs_alloc_fix_len(args);
937
938 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200939 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000940 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000941 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000942
943 /*
944 * Choose closer size and invalidate other cursor.
945 */
946 if (sdiff < gdiff)
947 goto out_use_search;
948 goto out_use_good;
949 }
950
951 if (!dir)
952 error = xfs_btree_increment(*scur, 0, &i);
953 else
954 error = xfs_btree_decrement(*scur, 0, &i);
955 if (error)
956 goto error0;
957 } while (i);
958
959out_use_good:
960 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
961 *scur = NULL;
962 return 0;
963
964out_use_search:
965 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
966 *gcur = NULL;
967 return 0;
968
969error0:
970 /* caller invalidates cursors */
971 return error;
972}
973
974/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * Allocate a variable extent near bno in the allocation group agno.
976 * Extent's length (returned in len) will be between minlen and maxlen,
977 * and of the form k * prod + mod unless there's nothing that large.
978 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
979 */
980STATIC int /* error */
981xfs_alloc_ag_vextent_near(
982 xfs_alloc_arg_t *args) /* allocation argument structure */
983{
984 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
985 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
986 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 xfs_agblock_t gtbno; /* start bno of right side entry */
988 xfs_agblock_t gtbnoa; /* aligned ... */
989 xfs_extlen_t gtdiff; /* difference to right side entry */
990 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000991 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfs_agblock_t gtnew; /* useful start bno of right side */
993 int error; /* error code */
994 int i; /* result code, temporary */
995 int j; /* result code, temporary */
996 xfs_agblock_t ltbno; /* start bno of left side entry */
997 xfs_agblock_t ltbnoa; /* aligned ... */
998 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001000 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xfs_agblock_t ltnew; /* useful start bno of left side */
1002 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001003 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +10001004#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * Randomly don't execute the first algorithm.
1007 */
1008 int dofirst; /* set to do first algorithm */
1009
Akinobu Mitaecb34032013-03-04 21:58:20 +09001010 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +00001012
Brian Fosterbfe46d42015-05-29 08:53:00 +10001013 /* handle unitialized agbno range so caller doesn't have to */
1014 if (!args->min_agbno && !args->max_agbno)
1015 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1016 ASSERT(args->min_agbno <= args->max_agbno);
1017
1018 /* clamp agbno to the range if it's outside */
1019 if (args->agbno < args->min_agbno)
1020 args->agbno = args->min_agbno;
1021 if (args->agbno > args->max_agbno)
1022 args->agbno = args->max_agbno;
1023
Christoph Hellwige26f0502011-04-24 19:06:15 +00001024restart:
1025 bno_cur_lt = NULL;
1026 bno_cur_gt = NULL;
1027 ltlen = 0;
1028 gtlena = 0;
1029 ltlena = 0;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * Get a cursor for the by-size btree.
1033 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001034 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1035 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * See if there are any free extents as big as maxlen.
1039 */
1040 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1041 goto error0;
1042 /*
1043 * If none, then pick up the last entry in the tree unless the
1044 * tree is empty.
1045 */
1046 if (!i) {
1047 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1048 &ltlen, &i)))
1049 goto error0;
1050 if (i == 0 || ltlen == 0) {
1051 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001052 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 }
1055 ASSERT(i == 1);
1056 }
1057 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /*
1060 * First algorithm.
1061 * If the requested extent is large wrt the freespaces available
1062 * in this a.g., then the cursor will be pointing to a btree entry
1063 * near the right edge of the tree. If it's in the last btree leaf
1064 * block, then we just examine all the entries in that block
1065 * that are big enough, and pick the best one.
1066 * This is written as a while loop so we can break out of it,
1067 * but we never loop back to the top.
1068 */
1069 while (xfs_btree_islastblock(cnt_cur, 0)) {
1070 xfs_extlen_t bdiff;
1071 int besti=0;
1072 xfs_extlen_t blen=0;
1073 xfs_agblock_t bnew=0;
1074
Dave Chinner63d20d62013-08-12 20:49:50 +10001075#ifdef DEBUG
1076 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 break;
1078#endif
1079 /*
1080 * Start from the entry that lookup found, sequence through
1081 * all larger free blocks. If we're actually pointing at a
1082 * record smaller than maxlen, go to the start of this block,
1083 * and skip all those smaller than minlen.
1084 */
1085 if (ltlen || args->alignment > 1) {
1086 cnt_cur->bc_ptrs[0] = 1;
1087 do {
1088 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1089 &ltlen, &i)))
1090 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001091 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (ltlen >= args->minlen)
1093 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001094 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto error0;
1096 } while (i);
1097 ASSERT(ltlen >= args->minlen);
1098 if (!i)
1099 break;
1100 }
1101 i = cnt_cur->bc_ptrs[0];
1102 for (j = 1, blen = 0, bdiff = 0;
1103 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001104 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /*
1106 * For each entry, decide if it's better than
1107 * the previous best entry.
1108 */
1109 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1110 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001111 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001112 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1113 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001114 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001116 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1117 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1119 xfs_alloc_fix_len(args);
1120 ASSERT(args->len >= args->minlen);
1121 if (args->len < blen)
1122 continue;
1123 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001124 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001125 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (ltnew != NULLAGBLOCK &&
1127 (args->len > blen || ltdiff < bdiff)) {
1128 bdiff = ltdiff;
1129 bnew = ltnew;
1130 blen = args->len;
1131 besti = cnt_cur->bc_ptrs[0];
1132 }
1133 }
1134 /*
1135 * It didn't work. We COULD be in a case where
1136 * there's a good record somewhere, so try again.
1137 */
1138 if (blen == 0)
1139 break;
1140 /*
1141 * Point at the best entry, and retrieve it again.
1142 */
1143 cnt_cur->bc_ptrs[0] = besti;
1144 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1145 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001146 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001147 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 args->len = blen;
1149 if (!xfs_alloc_fix_minleft(args)) {
1150 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001151 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153 }
1154 blen = args->len;
1155 /*
1156 * We are allocating starting at bnew for blen blocks.
1157 */
1158 args->agbno = bnew;
1159 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001160 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Set up a cursor for the by-bno tree.
1163 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001164 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1165 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * Fix up the btree entries.
1168 */
1169 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1170 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1171 goto error0;
1172 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1173 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001174
1175 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177 }
1178 /*
1179 * Second algorithm.
1180 * Search in the by-bno tree to the left and to the right
1181 * simultaneously, until in each case we find a space big enough,
1182 * or run into the edge of the tree. When we run into the edge,
1183 * we deallocate that cursor.
1184 * If both searches succeed, we compare the two spaces and pick
1185 * the better one.
1186 * With alignment, it's possible for both to fail; the upper
1187 * level algorithm that picks allocation groups for allocations
1188 * is not supposed to do this.
1189 */
1190 /*
1191 * Allocate and initialize the cursor for the leftward search.
1192 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001193 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1194 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
1196 * Lookup <= bno to find the leftward search's starting point.
1197 */
1198 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1199 goto error0;
1200 if (!i) {
1201 /*
1202 * Didn't find anything; use this cursor for the rightward
1203 * search.
1204 */
1205 bno_cur_gt = bno_cur_lt;
1206 bno_cur_lt = NULL;
1207 }
1208 /*
1209 * Found something. Duplicate the cursor for the rightward search.
1210 */
1211 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1212 goto error0;
1213 /*
1214 * Increment the cursor, so we will point at the entry just right
1215 * of the leftward entry if any, or to the leftmost entry.
1216 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001217 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto error0;
1219 if (!i) {
1220 /*
1221 * It failed, there are no rightward entries.
1222 */
1223 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1224 bno_cur_gt = NULL;
1225 }
1226 /*
1227 * Loop going left with the leftward cursor, right with the
1228 * rightward cursor, until either both directions give up or
1229 * we find an entry at least as big as minlen.
1230 */
1231 do {
1232 if (bno_cur_lt) {
1233 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1234 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001235 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001236 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1237 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001238 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001240 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001242 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 xfs_btree_del_cursor(bno_cur_lt,
1244 XFS_BTREE_NOERROR);
1245 bno_cur_lt = NULL;
1246 }
1247 }
1248 if (bno_cur_gt) {
1249 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1250 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001251 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001252 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1253 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001254 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001256 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001258 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 xfs_btree_del_cursor(bno_cur_gt,
1260 XFS_BTREE_NOERROR);
1261 bno_cur_gt = NULL;
1262 }
1263 }
1264 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * Got both cursors still active, need to find better entry.
1268 */
1269 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (ltlena >= args->minlen) {
1271 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001272 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1275 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001276 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001277 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001278 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001279
1280 error = xfs_alloc_find_best_extent(args,
1281 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001282 ltdiff, &gtbno, &gtlen,
1283 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001284 0 /* search right */);
1285 } else {
1286 ASSERT(gtlena >= args->minlen);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001289 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1292 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001293 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001294 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001295 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001296
1297 error = xfs_alloc_find_best_extent(args,
1298 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001299 gtdiff, &ltbno, &ltlen,
1300 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001301 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001303
1304 if (error)
1305 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * If we couldn't get anything, give up.
1310 */
1311 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001312 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1313
Christoph Hellwige26f0502011-04-24 19:06:15 +00001314 if (!forced++) {
1315 trace_xfs_alloc_near_busy(args);
1316 xfs_log_force(args->mp, XFS_LOG_SYNC);
1317 goto restart;
1318 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001319 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 args->agbno = NULLAGBLOCK;
1321 return 0;
1322 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * At this point we have selected a freespace entry, either to the
1326 * left or to the right. If it's on the right, copy all the
1327 * useful variables to the "left" set so we only have one
1328 * copy of this code.
1329 */
1330 if (bno_cur_gt) {
1331 bno_cur_lt = bno_cur_gt;
1332 bno_cur_gt = NULL;
1333 ltbno = gtbno;
1334 ltbnoa = gtbnoa;
1335 ltlen = gtlen;
1336 ltlena = gtlena;
1337 j = 1;
1338 } else
1339 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * Fix up the length and compute the useful address.
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1345 xfs_alloc_fix_len(args);
1346 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001347 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1349 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1350 return 0;
1351 }
1352 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001353 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001354 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001356 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001357 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001358 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1362 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1363 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001364
1365 if (j)
1366 trace_xfs_alloc_near_greater(args);
1367 else
1368 trace_xfs_alloc_near_lesser(args);
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1371 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1372 return 0;
1373
1374 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001375 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (cnt_cur != NULL)
1377 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1378 if (bno_cur_lt != NULL)
1379 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1380 if (bno_cur_gt != NULL)
1381 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1382 return error;
1383}
1384
1385/*
1386 * Allocate a variable extent anywhere in the allocation group agno.
1387 * Extent's length (returned in len) will be between minlen and maxlen,
1388 * and of the form k * prod + mod unless there's nothing that large.
1389 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1390 */
1391STATIC int /* error */
1392xfs_alloc_ag_vextent_size(
1393 xfs_alloc_arg_t *args) /* allocation argument structure */
1394{
1395 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1396 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1397 int error; /* error result */
1398 xfs_agblock_t fbno; /* start of found freespace */
1399 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int i; /* temp status variable */
1401 xfs_agblock_t rbno; /* returned block number */
1402 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001403 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Christoph Hellwige26f0502011-04-24 19:06:15 +00001405restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /*
1407 * Allocate and initialize a cursor for the by-size btree.
1408 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001409 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1410 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * Look for an entry >= maxlen+alignment-1 blocks.
1415 */
1416 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1417 args->maxlen + args->alignment - 1, &i)))
1418 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001421 * If none or we have busy extents that we cannot allocate from, then
1422 * we have to settle for a smaller extent. In the case that there are
1423 * no large extents, this will return the last entry in the tree unless
1424 * the tree is empty. In the case that there are only busy large
1425 * extents, this will return the largest small extent unless there
1426 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001428 if (!i || forced > 1) {
1429 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1430 &fbno, &flen, &i);
1431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto error0;
1433 if (i == 0 || flen == 0) {
1434 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001435 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return 0;
1437 }
1438 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001439 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1440 } else {
1441 /*
1442 * Search for a non-busy extent that is large enough.
1443 * If we are at low space, don't check, or if we fall of
1444 * the end of the btree, turn off the busy check and
1445 * restart.
1446 */
1447 for (;;) {
1448 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1449 if (error)
1450 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001451 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001452
1453 xfs_alloc_compute_aligned(args, fbno, flen,
1454 &rbno, &rlen);
1455
1456 if (rlen >= args->maxlen)
1457 break;
1458
1459 error = xfs_btree_increment(cnt_cur, 0, &i);
1460 if (error)
1461 goto error0;
1462 if (i == 0) {
1463 /*
1464 * Our only valid extents must have been busy.
1465 * Make it unbusy by forcing the log out and
1466 * retrying. If we've been here before, forcing
1467 * the log isn't making the extents available,
1468 * which means they have probably been freed in
1469 * this transaction. In that case, we have to
1470 * give up on them and we'll attempt a minlen
1471 * allocation the next time around.
1472 */
1473 xfs_btree_del_cursor(cnt_cur,
1474 XFS_BTREE_NOERROR);
1475 trace_xfs_alloc_size_busy(args);
1476 if (!forced++)
1477 xfs_log_force(args->mp, XFS_LOG_SYNC);
1478 goto restart;
1479 }
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /*
1484 * In the first case above, we got the last entry in the
1485 * by-size btree. Now we check to see if the space hits maxlen
1486 * once aligned; if not, we search left for something better.
1487 * This can't happen in the second case above.
1488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001490 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1492 if (rlen < args->maxlen) {
1493 xfs_agblock_t bestfbno;
1494 xfs_extlen_t bestflen;
1495 xfs_agblock_t bestrbno;
1496 xfs_extlen_t bestrlen;
1497
1498 bestrlen = rlen;
1499 bestrbno = rbno;
1500 bestflen = flen;
1501 bestfbno = fbno;
1502 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001503 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto error0;
1505 if (i == 0)
1506 break;
1507 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1508 &i)))
1509 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001510 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (flen < bestrlen)
1512 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001513 xfs_alloc_compute_aligned(args, fbno, flen,
1514 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001516 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 (rlen <= flen && rbno + rlen <= fbno + flen),
1518 error0);
1519 if (rlen > bestrlen) {
1520 bestrlen = rlen;
1521 bestrbno = rbno;
1522 bestflen = flen;
1523 bestfbno = fbno;
1524 if (rlen == args->maxlen)
1525 break;
1526 }
1527 }
1528 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1529 &i)))
1530 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001531 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 rlen = bestrlen;
1533 rbno = bestrbno;
1534 flen = bestflen;
1535 fbno = bestfbno;
1536 }
1537 args->wasfromfl = 0;
1538 /*
1539 * Fix up the length.
1540 */
1541 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001542 if (rlen < args->minlen) {
1543 if (!forced++) {
1544 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1545 trace_xfs_alloc_size_busy(args);
1546 xfs_log_force(args->mp, XFS_LOG_SYNC);
1547 goto restart;
1548 }
1549 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001551 xfs_alloc_fix_len(args);
1552
1553 if (!xfs_alloc_fix_minleft(args))
1554 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001556 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /*
1558 * Allocate and initialize a cursor for the by-block tree.
1559 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001560 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1561 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1563 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1564 goto error0;
1565 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1566 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1567 cnt_cur = bno_cur = NULL;
1568 args->len = rlen;
1569 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001570 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001572 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001574 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576
1577error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001578 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (cnt_cur)
1580 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1581 if (bno_cur)
1582 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1583 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001584
1585out_nominleft:
1586 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1587 trace_xfs_alloc_size_nominleft(args);
1588 args->agbno = NULLAGBLOCK;
1589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/*
1593 * Deal with the case where only small freespaces remain.
1594 * Either return the contents of the last freespace record,
1595 * or allocate space from the freelist if there is nothing in the tree.
1596 */
1597STATIC int /* error */
1598xfs_alloc_ag_vextent_small(
1599 xfs_alloc_arg_t *args, /* allocation argument structure */
1600 xfs_btree_cur_t *ccur, /* by-size cursor */
1601 xfs_agblock_t *fbnop, /* result block number */
1602 xfs_extlen_t *flenp, /* result length */
1603 int *stat) /* status: 0-freelist, 1-normal/none */
1604{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001605 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001606 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int error;
1608 xfs_agblock_t fbno;
1609 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int i;
1611
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001612 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto error0;
1614 if (i) {
1615 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1616 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001617 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 /*
1620 * Nothing in the btree, try the freelist. Make sure
1621 * to respect minleft even when pulling from the
1622 * freelist.
1623 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001624 else if (args->minlen == 1 && args->alignment == 1 &&
1625 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001626 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1627 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001628 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1629 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto error0;
1631 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001632 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001633 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001634
Dave Chinner292378e2016-09-26 08:21:28 +10001635 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 xfs_buf_t *bp;
1637
1638 bp = xfs_btree_get_bufs(args->mp, args->tp,
1639 args->agno, fbno, 0);
1640 xfs_trans_binval(args->tp, bp);
1641 }
1642 args->len = 1;
1643 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001644 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001646 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 error0);
1648 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001649 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001650
1651 /*
1652 * If we're feeding an AGFL block to something that
1653 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001654 * out the OWN_AG rmap and add the block back to
1655 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001656 */
1657 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1658 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1659 fbno, 1, &oinfo);
1660 if (error)
1661 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001662 pag = xfs_perag_get(args->mp, args->agno);
1663 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1664 args->tp, 1);
1665 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 *stat = 0;
1668 return 0;
1669 }
1670 /*
1671 * Nothing in the freelist.
1672 */
1673 else
1674 flen = 0;
1675 }
1676 /*
1677 * Can't allocate from the freelist for some reason.
1678 */
Nathan Scottd432c802006-09-28 11:03:44 +10001679 else {
1680 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Can't do the allocation, give up.
1685 */
1686 if (flen < args->minlen) {
1687 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001688 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 flen = 0;
1690 }
1691 *fbnop = fbno;
1692 *flenp = flen;
1693 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001694 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696
1697error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001698 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return error;
1700}
1701
1702/*
1703 * Free the extent starting at agno/bno for length.
1704 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001705STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001707 xfs_trans_t *tp,
1708 xfs_buf_t *agbp,
1709 xfs_agnumber_t agno,
1710 xfs_agblock_t bno,
1711 xfs_extlen_t len,
1712 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001713 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1716 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1717 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xfs_agblock_t gtbno; /* start of right neighbor block */
1719 xfs_extlen_t gtlen; /* length of right neighbor block */
1720 int haveleft; /* have a left neighbor block */
1721 int haveright; /* have a right neighbor block */
1722 int i; /* temp, result code */
1723 xfs_agblock_t ltbno; /* start of left neighbor block */
1724 xfs_extlen_t ltlen; /* length of left neighbor block */
1725 xfs_mount_t *mp; /* mount point struct for filesystem */
1726 xfs_agblock_t nbno; /* new starting block of freespace */
1727 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001728 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Darrick J. Wong673930c2016-08-03 11:33:43 +10001730 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001732
1733 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1734 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1735 if (error)
1736 goto error0;
1737 }
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
1740 * Allocate and initialize a cursor for the by-block btree.
1741 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001742 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * Look for a neighboring block on the left (lower block numbers)
1745 * that is contiguous with this space.
1746 */
1747 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1748 goto error0;
1749 if (haveleft) {
1750 /*
1751 * There is a block to our left.
1752 */
1753 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1754 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * It's not contiguous, though.
1758 */
1759 if (ltbno + ltlen < bno)
1760 haveleft = 0;
1761 else {
1762 /*
1763 * If this failure happens the request to free this
1764 * space was invalid, it's (partly) already free.
1765 * Very bad.
1766 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001767 XFS_WANT_CORRUPTED_GOTO(mp,
1768 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
1771 /*
1772 * Look for a neighboring block on the right (higher block numbers)
1773 * that is contiguous with this space.
1774 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001775 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto error0;
1777 if (haveright) {
1778 /*
1779 * There is a block to our right.
1780 */
1781 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1782 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001783 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * It's not contiguous, though.
1786 */
1787 if (bno + len < gtbno)
1788 haveright = 0;
1789 else {
1790 /*
1791 * If this failure happens the request to free this
1792 * space was invalid, it's (partly) already free.
1793 * Very bad.
1794 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001795 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797 }
1798 /*
1799 * Now allocate and initialize a cursor for the by-size tree.
1800 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001801 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * Have both left and right contiguous neighbors.
1804 * Merge all three into a single free block.
1805 */
1806 if (haveleft && haveright) {
1807 /*
1808 * Delete the old by-size entry on the left.
1809 */
1810 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1811 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001813 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Delete the old by-size entry on the right.
1818 */
1819 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1820 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001821 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001822 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Delete the old by-block entry for the right block.
1827 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001828 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001830 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Move the by-block cursor back to the left neighbor.
1833 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001834 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001836 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837#ifdef DEBUG
1838 /*
1839 * Check that this is the right record: delete didn't
1840 * mangle the cursor.
1841 */
1842 {
1843 xfs_agblock_t xxbno;
1844 xfs_extlen_t xxlen;
1845
1846 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1847 &i)))
1848 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001849 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 i == 1 && xxbno == ltbno && xxlen == ltlen,
1851 error0);
1852 }
1853#endif
1854 /*
1855 * Update remaining by-block entry to the new, joined block.
1856 */
1857 nbno = ltbno;
1858 nlen = len + ltlen + gtlen;
1859 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1860 goto error0;
1861 }
1862 /*
1863 * Have only a left contiguous neighbor.
1864 * Merge it together with the new freespace.
1865 */
1866 else if (haveleft) {
1867 /*
1868 * Delete the old by-size entry on the left.
1869 */
1870 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1871 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001872 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001873 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001875 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * Back up the by-block cursor to the left neighbor, and
1878 * update its length.
1879 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001880 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 nbno = ltbno;
1884 nlen = len + ltlen;
1885 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1886 goto error0;
1887 }
1888 /*
1889 * Have only a right contiguous neighbor.
1890 * Merge it together with the new freespace.
1891 */
1892 else if (haveright) {
1893 /*
1894 * Delete the old by-size entry on the right.
1895 */
1896 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1897 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001898 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001899 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001901 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * Update the starting block and length of the right
1904 * neighbor in the by-block tree.
1905 */
1906 nbno = bno;
1907 nlen = len + gtlen;
1908 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1909 goto error0;
1910 }
1911 /*
1912 * No contiguous neighbors.
1913 * Insert the new freespace into the by-block tree.
1914 */
1915 else {
1916 nbno = bno;
1917 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001918 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001920 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1923 bno_cur = NULL;
1924 /*
1925 * In all cases we need to insert the new freespace in the by-size tree.
1926 */
1927 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1928 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001929 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001930 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001932 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1934 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /*
1937 * Update the freespace totals in the ag and superblock.
1938 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001939 pag = xfs_perag_get(mp, agno);
1940 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001941 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001942 xfs_perag_put(pag);
1943 if (error)
1944 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001946 XFS_STATS_INC(mp, xs_freex);
1947 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001948
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001949 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1950 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953
1954 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001955 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1956 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (bno_cur)
1958 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1959 if (cnt_cur)
1960 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1961 return error;
1962}
1963
1964/*
1965 * Visible (exported) allocation/free functions.
1966 * Some of these are used just by xfs_alloc_btree.c and this file.
1967 */
1968
1969/*
1970 * Compute and fill in value of m_ag_maxlevels.
1971 */
1972void
1973xfs_alloc_compute_maxlevels(
1974 xfs_mount_t *mp) /* file system mount structure */
1975{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001976 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1977 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001981 * Find the length of the longest extent in an AG. The 'need' parameter
1982 * specifies how much space we're going to need for the AGFL and the
1983 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1984 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001985 */
1986xfs_extlen_t
1987xfs_alloc_longest_free_extent(
1988 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001989 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001990 xfs_extlen_t need,
1991 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001992{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001993 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001994
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001995 /*
1996 * If the AGFL needs a recharge, we'll have to subtract that from the
1997 * longest extent.
1998 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001999 if (need > pag->pagf_flcount)
2000 delta = need - pag->pagf_flcount;
2001
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002002 /*
2003 * If we cannot maintain others' reservations with space from the
2004 * not-longest freesp extents, we'll have to subtract /that/ from
2005 * the longest extent too.
2006 */
2007 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2008 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2009
2010 /*
2011 * If the longest extent is long enough to satisfy all the
2012 * reservations and AGFL rules in place, we can return this extent.
2013 */
Dave Chinner6cc87642009-03-16 08:29:46 +01002014 if (pag->pagf_longest > delta)
2015 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002016
2017 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01002018 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2019}
2020
Dave Chinner496817b2015-06-22 10:13:30 +10002021unsigned int
2022xfs_alloc_min_freelist(
2023 struct xfs_mount *mp,
2024 struct xfs_perag *pag)
2025{
2026 unsigned int min_free;
2027
2028 /* space needed by-bno freespace btree */
2029 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2030 mp->m_ag_maxlevels);
2031 /* space needed by-size freespace btree */
2032 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2033 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10002034 /* space needed reverse mapping used space btree */
2035 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2036 min_free += min_t(unsigned int,
2037 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2038 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10002039
2040 return min_free;
2041}
2042
Dave Chinner6cc87642009-03-16 08:29:46 +01002043/*
Dave Chinner72d55282015-06-22 10:04:42 +10002044 * Check if the operation we are fixing up the freelist for should go ahead or
2045 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2046 * is dependent on whether the size and shape of free space available will
2047 * permit the requested allocation to take place.
2048 */
2049static bool
2050xfs_alloc_space_available(
2051 struct xfs_alloc_arg *args,
2052 xfs_extlen_t min_free,
2053 int flags)
2054{
2055 struct xfs_perag *pag = args->pag;
2056 xfs_extlen_t longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002057 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002058 int available;
2059
2060 if (flags & XFS_ALLOC_FLAG_FREEING)
2061 return true;
2062
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002063 reservation = xfs_ag_resv_needed(pag, args->resv);
2064
Dave Chinner72d55282015-06-22 10:04:42 +10002065 /* do we have enough contiguous free space for the allocation? */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002066 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2067 reservation);
Dave Chinner72d55282015-06-22 10:04:42 +10002068 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2069 return false;
2070
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002071 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002072 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002073 reservation - min_free - args->total);
2074 if (available < (int)args->minleft || available <= 0)
Dave Chinner72d55282015-06-22 10:04:42 +10002075 return false;
2076
2077 return true;
2078}
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080/*
2081 * Decide whether to use this allocation group for this allocation.
2082 * If so, fix up the btree freelist's size.
2083 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002084int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002086 struct xfs_alloc_arg *args, /* allocation argument structure */
2087 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Dave Chinner396503f2015-06-22 10:13:19 +10002089 struct xfs_mount *mp = args->mp;
2090 struct xfs_perag *pag = args->pag;
2091 struct xfs_trans *tp = args->tp;
2092 struct xfs_buf *agbp = NULL;
2093 struct xfs_buf *agflbp = NULL;
2094 struct xfs_alloc_arg targs; /* local allocation arguments */
2095 xfs_agblock_t bno; /* freelist block */
2096 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002097 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002100 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2101 if (error)
2102 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002104 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2105 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002106 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Dave Chinner396503f2015-06-22 10:13:19 +10002108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002110 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002111 * If this is a metadata preferred pag and we are user data then try
2112 * somewhere else if we are not being asked to try harder at this
2113 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Dave Chinner292378e2016-09-26 08:21:28 +10002115 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002116 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2117 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002118 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Dave Chinner496817b2015-06-22 10:13:30 +10002121 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002122 if (!xfs_alloc_space_available(args, need, flags))
2123 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /*
2126 * Get the a.g. freespace buffer.
2127 * Can fail if we're not blocking on locks, and it's held.
2128 */
Dave Chinner396503f2015-06-22 10:13:19 +10002129 if (!agbp) {
2130 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2131 if (error)
2132 goto out_no_agbp;
2133 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002134 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2135 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002136 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002139
Dave Chinner50adbcb2015-06-22 10:04:31 +10002140 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002141 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002142 if (!xfs_alloc_space_available(args, need, flags))
2143 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /*
2146 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002147 *
Dave Chinner396503f2015-06-22 10:13:19 +10002148 * Note that from this point onwards, we will always release the agf and
2149 * agfl buffers on error. This handles the case where we error out and
2150 * the buffers are clean or may not have been joined to the transaction
2151 * and hence need to be released manually. If they have been joined to
2152 * the transaction, then xfs_trans_brelse() will handle them
2153 * appropriately based on the recursion count and dirty state of the
2154 * buffer.
2155 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002156 * XXX (dgc): When we have lots of free space, does this buy us
2157 * anything other than extra overhead when we need to put more blocks
2158 * back on the free list? Maybe we should only do this when space is
2159 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002160 *
2161 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2162 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2163 * updating the rmapbt. Both flags are used in xfs_repair while we're
2164 * rebuilding the rmapbt, and neither are used by the kernel. They're
2165 * both required to ensure that rmaps are correctly recorded for the
2166 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2167 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002169 memset(&targs, 0, sizeof(targs));
2170 if (flags & XFS_ALLOC_FLAG_NORMAP)
2171 xfs_rmap_skip_owner_update(&targs.oinfo);
2172 else
2173 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2174 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002175 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
David Chinner92821e22007-05-24 15:26:31 +10002177 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2178 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002179 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002180 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002181 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002182 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002183 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2185 xfs_trans_binval(tp, bp);
2186 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 targs.tp = tp;
2189 targs.mp = mp;
2190 targs.agbp = agbp;
2191 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002192 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 targs.type = XFS_ALLOCTYPE_THIS_AG;
2194 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002195 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2196 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002197 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002198
2199 /* Make the freelist longer if it's too short. */
2200 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002202 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002203 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002204
2205 /* Allocate as many blocks as possible at once. */
2206 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002207 if (error)
2208 goto out_agflbp_relse;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /*
2211 * Stop if we run out. Won't happen if callers are obeying
2212 * the restrictions correctly. Can happen for free calls
2213 * on a completely full ag.
2214 */
Yingping Lud210a282006-06-09 14:55:18 +10002215 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002216 if (flags & XFS_ALLOC_FLAG_FREEING)
2217 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002218 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /*
2221 * Put each allocated block on the list.
2222 */
2223 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002224 error = xfs_alloc_put_freelist(tp, agbp,
2225 agflbp, bno, 0);
2226 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002227 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002230 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 args->agbp = agbp;
2232 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002233
2234out_agflbp_relse:
2235 xfs_trans_brelse(tp, agflbp);
2236out_agbp_relse:
2237 if (agbp)
2238 xfs_trans_brelse(tp, agbp);
2239out_no_agbp:
2240 args->agbp = NULL;
2241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244/*
2245 * Get a block from the freelist.
2246 * Returns with the buffer for the block gotten.
2247 */
2248int /* error */
2249xfs_alloc_get_freelist(
2250 xfs_trans_t *tp, /* transaction pointer */
2251 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002252 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2253 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2257 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002258 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int error;
David Chinner92821e22007-05-24 15:26:31 +10002260 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002261 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 xfs_perag_t *pag; /* per allocation group data */
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /*
2265 * Freelist is empty, give up.
2266 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002267 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!agf->agf_flcount) {
2269 *bnop = NULLAGBLOCK;
2270 return 0;
2271 }
2272 /*
2273 * Read the array of free blocks.
2274 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002275 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2276 &agflbp);
2277 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002279
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /*
2282 * Get the block number and update the data structures.
2283 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002284 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2285 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002286 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002288 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002290
2291 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002292 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 xfs_trans_agflist_delta(tp, -1);
2294 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002295 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002296
2297 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2298 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002299 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002300 pag->pagf_btreeblks++;
2301 logflags |= XFS_AGF_BTREEBLKS;
2302 }
2303
David Chinner92821e22007-05-24 15:26:31 +10002304 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 *bnop = bno;
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
2309
2310/*
2311 * Log the given fields from the agf structure.
2312 */
2313void
2314xfs_alloc_log_agf(
2315 xfs_trans_t *tp, /* transaction pointer */
2316 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2317 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2318{
2319 int first; /* first byte offset */
2320 int last; /* last byte offset */
2321 static const short offsets[] = {
2322 offsetof(xfs_agf_t, agf_magicnum),
2323 offsetof(xfs_agf_t, agf_versionnum),
2324 offsetof(xfs_agf_t, agf_seqno),
2325 offsetof(xfs_agf_t, agf_length),
2326 offsetof(xfs_agf_t, agf_roots[0]),
2327 offsetof(xfs_agf_t, agf_levels[0]),
2328 offsetof(xfs_agf_t, agf_flfirst),
2329 offsetof(xfs_agf_t, agf_fllast),
2330 offsetof(xfs_agf_t, agf_flcount),
2331 offsetof(xfs_agf_t, agf_freeblks),
2332 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002333 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002334 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002335 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002336 offsetof(xfs_agf_t, agf_refcount_blocks),
2337 offsetof(xfs_agf_t, agf_refcount_root),
2338 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002339 /* needed so that we don't log the whole rest of the structure: */
2340 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 sizeof(xfs_agf_t)
2342 };
2343
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002344 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2345
Dave Chinner61fe1352013-04-03 16:11:30 +11002346 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2349 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2350}
2351
2352/*
2353 * Interface for inode allocation to force the pag data to be initialized.
2354 */
2355int /* error */
2356xfs_alloc_pagf_init(
2357 xfs_mount_t *mp, /* file system mount structure */
2358 xfs_trans_t *tp, /* transaction pointer */
2359 xfs_agnumber_t agno, /* allocation group number */
2360 int flags) /* XFS_ALLOC_FLAGS_... */
2361{
2362 xfs_buf_t *bp;
2363 int error;
2364
2365 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2366 return error;
2367 if (bp)
2368 xfs_trans_brelse(tp, bp);
2369 return 0;
2370}
2371
2372/*
2373 * Put the block on the freelist for the allocation group.
2374 */
2375int /* error */
2376xfs_alloc_put_freelist(
2377 xfs_trans_t *tp, /* transaction pointer */
2378 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2379 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002380 xfs_agblock_t bno, /* block being freed */
2381 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002384 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 int error;
David Chinner92821e22007-05-24 15:26:31 +10002386 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 xfs_mount_t *mp; /* mount structure */
2388 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002389 __be32 *agfl_bno;
2390 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 agf = XFS_BUF_TO_AGF(agbp);
2393 mp = tp->t_mountp;
2394
2395 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002396 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002398 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002399 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002401
2402 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002403 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 xfs_trans_agflist_delta(tp, 1);
2405 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002406
2407 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2408 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002409 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002410 pag->pagf_btreeblks--;
2411 logflags |= XFS_AGF_BTREEBLKS;
2412 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002413 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002414
David Chinner92821e22007-05-24 15:26:31 +10002415 xfs_alloc_log_agf(tp, agbp, logflags);
2416
Christoph Hellwig16259e72005-11-02 15:11:25 +11002417 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002418
2419 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2420 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002421 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002422 startoff = (char *)blockp - (char *)agflbp->b_addr;
2423
David Chinner92821e22007-05-24 15:26:31 +10002424 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002425
Dave Chinner61fe1352013-04-03 16:11:30 +11002426 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002427 xfs_trans_log_buf(tp, agflbp, startoff,
2428 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Dave Chinner4e0e6042013-04-03 16:11:13 +11002432static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002433xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002434 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002435 struct xfs_buf *bp)
2436 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002437 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002438
Brian Fostera45086e2015-10-12 15:59:25 +11002439 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2440 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002441 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002442 if (!xfs_log_check_lsn(mp,
2443 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2444 return false;
2445 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002446
Dave Chinner4e0e6042013-04-03 16:11:13 +11002447 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2448 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2449 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2450 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2451 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2452 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2453 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002454
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002455 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2456 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2457 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002458 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2459 return false;
2460
Darrick J. Wongb8704942016-08-03 11:30:32 +10002461 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002462 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2463 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wongb8704942016-08-03 11:30:32 +10002464 return false;
2465
Dave Chinner5d5f5272012-11-14 17:44:56 +11002466 /*
2467 * during growfs operations, the perag is not fully initialised,
2468 * so we can't use it for any useful checking. growfs ensures we can't
2469 * use it by using uncached buffers that don't have the perag attached
2470 * so we can detect and avoid this problem.
2471 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002472 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2473 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002474
Dave Chinner4e0e6042013-04-03 16:11:13 +11002475 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2476 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2477 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002478
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002479 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002480 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2481 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002482 return false;
2483
Dave Chinner4e0e6042013-04-03 16:11:13 +11002484 return true;;
2485
Dave Chinner612cfbf2012-11-14 17:52:32 +11002486}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002487
Dave Chinner1813dd62012-11-14 17:54:40 +11002488static void
2489xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002490 struct xfs_buf *bp)
2491{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002492 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002493
Eric Sandeence5028c2014-02-27 15:23:10 +11002494 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2495 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002496 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002497 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2498 XFS_ERRTAG_ALLOC_READ_AGF,
2499 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002500 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002501
2502 if (bp->b_error)
2503 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002504}
2505
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002506static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002507xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002508 struct xfs_buf *bp)
2509{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002510 struct xfs_mount *mp = bp->b_target->bt_mount;
2511 struct xfs_buf_log_item *bip = bp->b_fspriv;
2512
2513 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002514 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002515 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002516 return;
2517 }
2518
2519 if (!xfs_sb_version_hascrc(&mp->m_sb))
2520 return;
2521
2522 if (bip)
2523 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2524
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002525 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002526}
2527
Dave Chinner1813dd62012-11-14 17:54:40 +11002528const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002529 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002530 .verify_read = xfs_agf_read_verify,
2531 .verify_write = xfs_agf_write_verify,
2532};
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534/*
2535 * Read in the allocation group header (free/alloc section).
2536 */
2537int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002538xfs_read_agf(
2539 struct xfs_mount *mp, /* mount point structure */
2540 struct xfs_trans *tp, /* transaction pointer */
2541 xfs_agnumber_t agno, /* allocation group number */
2542 int flags, /* XFS_BUF_ */
2543 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int error;
2546
Dave Chinnerd1230312013-11-01 15:27:19 +11002547 trace_xfs_read_agf(mp, agno);
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 ASSERT(agno != NULLAGNUMBER);
2550 error = xfs_trans_read_buf(
2551 mp, tp, mp->m_ddev_targp,
2552 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002553 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (error)
2555 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002556 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002558
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002559 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002560 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002561 return 0;
2562}
2563
2564/*
2565 * Read in the allocation group header (free/alloc section).
2566 */
2567int /* error */
2568xfs_alloc_read_agf(
2569 struct xfs_mount *mp, /* mount point structure */
2570 struct xfs_trans *tp, /* transaction pointer */
2571 xfs_agnumber_t agno, /* allocation group number */
2572 int flags, /* XFS_ALLOC_FLAG_... */
2573 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2574{
2575 struct xfs_agf *agf; /* ag freelist header */
2576 struct xfs_perag *pag; /* per allocation group data */
2577 int error;
2578
Dave Chinnerd1230312013-11-01 15:27:19 +11002579 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002580
Dave Chinnerd1230312013-11-01 15:27:19 +11002581 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002582 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002583 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002584 bpp);
2585 if (error)
2586 return error;
2587 if (!*bpp)
2588 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002589 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002590
2591 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002592 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002594 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002595 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002596 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2597 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002599 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002601 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002602 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2603 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002604 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002605 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002606 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002607 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 pag->pagf_init = 1;
2609 }
2610#ifdef DEBUG
2611 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002612 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002613 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002614 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2615 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002617 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002619 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002622 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return 0;
2624}
2625
2626/*
2627 * Allocate an extent (variable-size).
2628 * Depending on the allocation type, we either look in a single allocation
2629 * group or loop over the allocation groups to find the result.
2630 */
2631int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002632xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 xfs_alloc_arg_t *args) /* allocation argument structure */
2634{
2635 xfs_agblock_t agsize; /* allocation group size */
2636 int error;
2637 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 xfs_extlen_t minleft;/* minimum left value, temp copy */
2639 xfs_mount_t *mp; /* mount structure pointer */
2640 xfs_agnumber_t sagno; /* starting allocation group number */
2641 xfs_alloctype_t type; /* input allocation type */
2642 int bump_rotor = 0;
2643 int no_min = 0;
2644 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2645
2646 mp = args->mp;
2647 type = args->otype = args->type;
2648 args->agbno = NULLAGBLOCK;
2649 /*
2650 * Just fix this up, for the case where the last a.g. is shorter
2651 * (or there's only one a.g.) and the caller couldn't easily figure
2652 * that out (xfs_bmap_alloc).
2653 */
2654 agsize = mp->m_sb.sb_agblocks;
2655 if (args->maxlen > agsize)
2656 args->maxlen = agsize;
2657 if (args->alignment == 0)
2658 args->alignment = 1;
2659 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2660 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2661 ASSERT(args->minlen <= args->maxlen);
2662 ASSERT(args->minlen <= agsize);
2663 ASSERT(args->mod < args->prod);
2664 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2665 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2666 args->minlen > args->maxlen || args->minlen > agsize ||
2667 args->mod >= args->prod) {
2668 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002669 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return 0;
2671 }
2672 minleft = args->minleft;
2673
2674 switch (type) {
2675 case XFS_ALLOCTYPE_THIS_AG:
2676 case XFS_ALLOCTYPE_NEAR_BNO:
2677 case XFS_ALLOCTYPE_THIS_BNO:
2678 /*
2679 * These three force us into a single a.g.
2680 */
2681 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002682 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 args->minleft = 0;
2684 error = xfs_alloc_fix_freelist(args, 0);
2685 args->minleft = minleft;
2686 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002687 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto error0;
2689 }
2690 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002691 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 break;
2693 }
2694 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2695 if ((error = xfs_alloc_ag_vextent(args)))
2696 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 break;
2698 case XFS_ALLOCTYPE_START_BNO:
2699 /*
2700 * Try near allocation first, then anywhere-in-ag after
2701 * the first a.g. fails.
2702 */
Dave Chinner292378e2016-09-26 08:21:28 +10002703 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2705 args->fsbno = XFS_AGB_TO_FSB(mp,
2706 ((mp->m_agfrotor / rotorstep) %
2707 mp->m_sb.sb_agcount), 0);
2708 bump_rotor = 1;
2709 }
2710 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2711 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2712 /* FALLTHROUGH */
2713 case XFS_ALLOCTYPE_ANY_AG:
2714 case XFS_ALLOCTYPE_START_AG:
2715 case XFS_ALLOCTYPE_FIRST_AG:
2716 /*
2717 * Rotate through the allocation groups looking for a winner.
2718 */
2719 if (type == XFS_ALLOCTYPE_ANY_AG) {
2720 /*
2721 * Start with the last place we left off.
2722 */
2723 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2724 mp->m_sb.sb_agcount;
2725 args->type = XFS_ALLOCTYPE_THIS_AG;
2726 flags = XFS_ALLOC_FLAG_TRYLOCK;
2727 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2728 /*
2729 * Start with allocation group given by bno.
2730 */
2731 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2732 args->type = XFS_ALLOCTYPE_THIS_AG;
2733 sagno = 0;
2734 flags = 0;
2735 } else {
2736 if (type == XFS_ALLOCTYPE_START_AG)
2737 args->type = XFS_ALLOCTYPE_THIS_AG;
2738 /*
2739 * Start with the given allocation group.
2740 */
2741 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2742 flags = XFS_ALLOC_FLAG_TRYLOCK;
2743 }
2744 /*
2745 * Loop over allocation groups twice; first time with
2746 * trylock set, second time without.
2747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002749 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (no_min) args->minleft = 0;
2751 error = xfs_alloc_fix_freelist(args, flags);
2752 args->minleft = minleft;
2753 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002754 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 goto error0;
2756 }
2757 /*
2758 * If we get a buffer back then the allocation will fly.
2759 */
2760 if (args->agbp) {
2761 if ((error = xfs_alloc_ag_vextent(args)))
2762 goto error0;
2763 break;
2764 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002765
2766 trace_xfs_alloc_vextent_loopfailed(args);
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /*
2769 * Didn't work, figure out the next iteration.
2770 */
2771 if (args->agno == sagno &&
2772 type == XFS_ALLOCTYPE_START_BNO)
2773 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002774 /*
2775 * For the first allocation, we can try any AG to get
2776 * space. However, if we already have allocated a
2777 * block, we don't want to try AGs whose number is below
2778 * sagno. Otherwise, we may end up with out-of-order
2779 * locking of AGF, which might cause deadlock.
2780 */
2781 if (++(args->agno) == mp->m_sb.sb_agcount) {
2782 if (args->firstblock != NULLFSBLOCK)
2783 args->agno = sagno;
2784 else
2785 args->agno = 0;
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /*
2788 * Reached the starting a.g., must either be done
2789 * or switch to non-trylock mode.
2790 */
2791 if (args->agno == sagno) {
2792 if (no_min == 1) {
2793 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002794 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 break;
2796 }
2797 if (flags == 0) {
2798 no_min = 1;
2799 } else {
2800 flags = 0;
2801 if (type == XFS_ALLOCTYPE_START_BNO) {
2802 args->agbno = XFS_FSB_TO_AGBNO(mp,
2803 args->fsbno);
2804 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2805 }
2806 }
2807 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002808 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2811 if (args->agno == sagno)
2812 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2813 (mp->m_sb.sb_agcount * rotorstep);
2814 else
2815 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2816 (mp->m_sb.sb_agcount * rotorstep);
2817 }
2818 break;
2819 default:
2820 ASSERT(0);
2821 /* NOTREACHED */
2822 }
2823 if (args->agbno == NULLAGBLOCK)
2824 args->fsbno = NULLFSBLOCK;
2825 else {
2826 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2827#ifdef DEBUG
2828 ASSERT(args->len >= args->minlen);
2829 ASSERT(args->len <= args->maxlen);
2830 ASSERT(args->agbno % args->alignment == 0);
2831 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2832 args->len);
2833#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002834
2835 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002836 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002837 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2838 if (error)
2839 goto error0;
2840 }
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002843 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return 0;
2845error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002846 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return error;
2848}
2849
Dave Chinner4d89e202016-06-21 11:53:28 +10002850/* Ensure that the freelist is at full capacity. */
2851int
2852xfs_free_extent_fix_freelist(
2853 struct xfs_trans *tp,
2854 xfs_agnumber_t agno,
2855 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Dave Chinner4d89e202016-06-21 11:53:28 +10002857 struct xfs_alloc_arg args;
2858 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Dave Chinner4d89e202016-06-21 11:53:28 +10002860 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 args.tp = tp;
2862 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002863 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002864
2865 /*
2866 * validate that the block number is legal - the enables us to detect
2867 * and handle a silent filesystem corruption rather than crashing.
2868 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002869 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002870 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002871
Dave Chinnera862e0f2010-01-11 11:47:41 +00002872 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002873 ASSERT(args.pag);
2874
2875 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2876 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002877 goto out;
2878
2879 *agbp = args.agbp;
2880out:
2881 xfs_perag_put(args.pag);
2882 return error;
2883}
2884
2885/*
2886 * Free an extent.
2887 * Just break up the extent address and hand off to xfs_free_ag_extent
2888 * after fixing up the freelist.
2889 */
2890int /* error */
2891xfs_free_extent(
2892 struct xfs_trans *tp, /* transaction pointer */
2893 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002894 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002895 struct xfs_owner_info *oinfo, /* extent owner */
2896 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002897{
2898 struct xfs_mount *mp = tp->t_mountp;
2899 struct xfs_buf *agbp;
2900 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2901 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2902 int error;
2903
2904 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002905 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002906
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002907 if (XFS_TEST_ERROR(false, mp,
2908 XFS_ERRTAG_FREE_EXTENT,
2909 XFS_RANDOM_FREE_EXTENT))
2910 return -EIO;
2911
Dave Chinner4d89e202016-06-21 11:53:28 +10002912 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2913 if (error)
2914 return error;
2915
2916 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002917
2918 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002919 XFS_WANT_CORRUPTED_GOTO(mp,
2920 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2921 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002922
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002923 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002924 if (error)
2925 goto err;
2926
2927 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2928 return 0;
2929
2930err:
2931 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return error;
2933}