blob: 5050056a0b06445a93845987a52ec8356c499f80 [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{
98 unsigned int blocks;
99
100 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
Darrick J. Wong52548852016-08-03 11:38:24 +1000101 return blocks;
102}
103
104/*
105 * When deciding how much space to allocate out of an AG, we limit the
106 * allocation maximum size to the size the AG. However, we cannot use all the
107 * blocks in the AG - some are permanently used by metadata. These
108 * blocks are generally:
109 * - the AG superblock, AGF, AGI and AGFL
110 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
111 * the AGI free inode and rmap btree root blocks.
112 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
113 * - the rmapbt root block
114 *
115 * The AG headers are sector sized, so the amount of space they take up is
116 * dependent on filesystem geometry. The others are all single blocks.
117 */
118unsigned int
119xfs_alloc_ag_max_usable(
120 struct xfs_mount *mp)
121{
122 unsigned int blocks;
123
124 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
125 blocks += XFS_ALLOC_AGFL_RESERVE;
126 blocks += 3; /* AGF, AGI btree root blocks */
127 if (xfs_sb_version_hasfinobt(&mp->m_sb))
128 blocks++; /* finobt root block */
129 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
130 blocks++; /* rmap root block */
Darrick J. Wongd0e853f2016-10-03 09:11:24 -0700131 if (xfs_sb_version_hasreflink(&mp->m_sb))
132 blocks++; /* refcount root block */
Darrick J. Wong52548852016-08-03 11:38:24 +1000133
134 return mp->m_sb.sb_agblocks - blocks;
135}
136
137/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100138 * Lookup the record equal to [bno, len] in the btree given by cur.
139 */
140STATIC int /* error */
141xfs_alloc_lookup_eq(
142 struct xfs_btree_cur *cur, /* btree cursor */
143 xfs_agblock_t bno, /* starting block of extent */
144 xfs_extlen_t len, /* length of extent */
145 int *stat) /* success/failure */
146{
147 cur->bc_rec.a.ar_startblock = bno;
148 cur->bc_rec.a.ar_blockcount = len;
149 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
150}
151
152/*
153 * Lookup the first record greater than or equal to [bno, len]
154 * in the btree given by cur.
155 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000156int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100157xfs_alloc_lookup_ge(
158 struct xfs_btree_cur *cur, /* btree cursor */
159 xfs_agblock_t bno, /* starting block of extent */
160 xfs_extlen_t len, /* length of extent */
161 int *stat) /* success/failure */
162{
163 cur->bc_rec.a.ar_startblock = bno;
164 cur->bc_rec.a.ar_blockcount = len;
165 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
166}
167
168/*
169 * Lookup the first record less than or equal to [bno, len]
170 * in the btree given by cur.
171 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000172static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100173xfs_alloc_lookup_le(
174 struct xfs_btree_cur *cur, /* btree cursor */
175 xfs_agblock_t bno, /* starting block of extent */
176 xfs_extlen_t len, /* length of extent */
177 int *stat) /* success/failure */
178{
179 cur->bc_rec.a.ar_startblock = bno;
180 cur->bc_rec.a.ar_blockcount = len;
181 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
182}
183
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100184/*
185 * Update the record referred to by cur to the value given
186 * by [bno, len].
187 * This either works (return 0) or gets an EFSCORRUPTED error.
188 */
189STATIC int /* error */
190xfs_alloc_update(
191 struct xfs_btree_cur *cur, /* btree cursor */
192 xfs_agblock_t bno, /* starting block of extent */
193 xfs_extlen_t len) /* length of extent */
194{
195 union xfs_btree_rec rec;
196
197 rec.alloc.ar_startblock = cpu_to_be32(bno);
198 rec.alloc.ar_blockcount = cpu_to_be32(len);
199 return xfs_btree_update(cur, &rec);
200}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100201
202/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100203 * Get the data from the pointed-to record.
204 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000205int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100206xfs_alloc_get_rec(
207 struct xfs_btree_cur *cur, /* btree cursor */
208 xfs_agblock_t *bno, /* output: starting block of extent */
209 xfs_extlen_t *len, /* output: length of extent */
210 int *stat) /* output: success/failure */
211{
212 union xfs_btree_rec *rec;
213 int error;
214
215 error = xfs_btree_get_rec(cur, &rec, stat);
216 if (!error && *stat == 1) {
217 *bno = be32_to_cpu(rec->alloc.ar_startblock);
218 *len = be32_to_cpu(rec->alloc.ar_blockcount);
219 }
220 return error;
221}
222
223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Compute aligned version of the found extent.
225 * Takes alignment and min length into account.
226 */
David Chinner12375c82008-04-10 12:21:32 +1000227STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000229 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_agblock_t foundbno, /* starting block in found extent */
231 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 xfs_agblock_t *resbno, /* result block number */
233 xfs_extlen_t *reslen) /* result length */
234{
235 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000237 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Christoph Hellwige26f0502011-04-24 19:06:15 +0000239 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000240 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000241
Brian Fosterbfe46d42015-05-29 08:53:00 +1000242 /*
243 * If we have a largish extent that happens to start before min_agbno,
244 * see if we can shift it into range...
245 */
246 if (bno < args->min_agbno && bno + len > args->min_agbno) {
247 diff = args->min_agbno - bno;
248 if (len > diff) {
249 bno += diff;
250 len -= diff;
251 }
252 }
253
Christoph Hellwige26f0502011-04-24 19:06:15 +0000254 if (args->alignment > 1 && len >= args->minlen) {
255 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000256
257 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000258
259 *resbno = aligned_bno;
260 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000262 *resbno = bno;
263 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Compute best start block and diff for "near" allocations.
269 * freelen >= wantlen already checked by caller.
270 */
271STATIC xfs_extlen_t /* difference value (absolute) */
272xfs_alloc_compute_diff(
273 xfs_agblock_t wantbno, /* target starting block */
274 xfs_extlen_t wantlen, /* target length */
275 xfs_extlen_t alignment, /* target alignment */
Dave Chinner292378e2016-09-26 08:21:28 +1000276 int datatype, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_agblock_t freebno, /* freespace's starting block */
278 xfs_extlen_t freelen, /* freespace's length */
279 xfs_agblock_t *newbnop) /* result: best start block from free */
280{
281 xfs_agblock_t freeend; /* end of freespace extent */
282 xfs_agblock_t newbno1; /* return block number */
283 xfs_agblock_t newbno2; /* other new block number */
284 xfs_extlen_t newlen1=0; /* length with newbno1 */
285 xfs_extlen_t newlen2=0; /* length with newbno2 */
286 xfs_agblock_t wantend; /* end of target extent */
Dave Chinner292378e2016-09-26 08:21:28 +1000287 bool userdata = xfs_alloc_is_userdata(datatype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 ASSERT(freelen >= wantlen);
290 freeend = freebno + freelen;
291 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200292 /*
293 * We want to allocate from the start of a free extent if it is past
294 * the desired block or if we are allocating user data and the free
295 * extent is before desired block. The second case is there to allow
296 * for contiguous allocation from the remaining free space if the file
297 * grows in the short term.
298 */
299 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
301 newbno1 = NULLAGBLOCK;
302 } else if (freeend >= wantend && alignment > 1) {
303 newbno1 = roundup(wantbno, alignment);
304 newbno2 = newbno1 - alignment;
305 if (newbno1 >= freeend)
306 newbno1 = NULLAGBLOCK;
307 else
308 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
309 if (newbno2 < freebno)
310 newbno2 = NULLAGBLOCK;
311 else
312 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
313 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
314 if (newlen1 < newlen2 ||
315 (newlen1 == newlen2 &&
316 XFS_ABSDIFF(newbno1, wantbno) >
317 XFS_ABSDIFF(newbno2, wantbno)))
318 newbno1 = newbno2;
319 } else if (newbno2 != NULLAGBLOCK)
320 newbno1 = newbno2;
321 } else if (freeend >= wantend) {
322 newbno1 = wantbno;
323 } else if (alignment > 1) {
324 newbno1 = roundup(freeend - wantlen, alignment);
325 if (newbno1 > freeend - wantlen &&
326 newbno1 - alignment >= freebno)
327 newbno1 -= alignment;
328 else if (newbno1 >= freeend)
329 newbno1 = NULLAGBLOCK;
330 } else
331 newbno1 = freeend - wantlen;
332 *newbnop = newbno1;
333 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
334}
335
336/*
337 * Fix up the length, based on mod and prod.
338 * len should be k * prod + mod for some k.
339 * If len is too small it is returned unchanged.
340 * If len hits maxlen it is left alone.
341 */
342STATIC void
343xfs_alloc_fix_len(
344 xfs_alloc_arg_t *args) /* allocation argument structure */
345{
346 xfs_extlen_t k;
347 xfs_extlen_t rlen;
348
349 ASSERT(args->mod < args->prod);
350 rlen = args->len;
351 ASSERT(rlen >= args->minlen);
352 ASSERT(rlen <= args->maxlen);
353 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
354 (args->mod == 0 && rlen < args->prod))
355 return;
356 k = rlen % args->prod;
357 if (k == args->mod)
358 return;
Jan Kara30265112014-06-06 16:06:37 +1000359 if (k > args->mod)
360 rlen = rlen - (k - args->mod);
361 else
362 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100363 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000364 if ((int)rlen < (int)args->minlen)
365 return;
366 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
367 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 args->len = rlen;
369}
370
371/*
372 * Fix up length if there is too little space left in the a.g.
373 * Return 1 if ok, 0 if too little, should give up.
374 */
375STATIC int
376xfs_alloc_fix_minleft(
377 xfs_alloc_arg_t *args) /* allocation argument structure */
378{
379 xfs_agf_t *agf; /* a.g. freelist header */
380 int diff; /* free space difference */
381
382 if (args->minleft == 0)
383 return 1;
384 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100385 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 - args->len - args->minleft;
387 if (diff >= 0)
388 return 1;
389 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100390 /* casts to (int) catch length underflows */
391 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 1;
393 args->agbno = NULLAGBLOCK;
394 return 0;
395}
396
397/*
398 * Update the two btrees, logically removing from freespace the extent
399 * starting at rbno, rlen blocks. The extent is contained within the
400 * actual (current) free extent fbno for flen blocks.
401 * Flags are passed in indicating whether the cursors are set to the
402 * relevant records.
403 */
404STATIC int /* error code */
405xfs_alloc_fixup_trees(
406 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
407 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
408 xfs_agblock_t fbno, /* starting block of free extent */
409 xfs_extlen_t flen, /* length of free extent */
410 xfs_agblock_t rbno, /* starting block of returned extent */
411 xfs_extlen_t rlen, /* length of returned extent */
412 int flags) /* flags, XFSA_FIXUP_... */
413{
414 int error; /* error code */
415 int i; /* operation results */
416 xfs_agblock_t nfbno1; /* first new free startblock */
417 xfs_agblock_t nfbno2; /* second new free startblock */
418 xfs_extlen_t nflen1=0; /* first new free length */
419 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100420 struct xfs_mount *mp;
421
422 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
425 * Look up the record in the by-size tree if necessary.
426 */
427 if (flags & XFSA_FIXUP_CNT_OK) {
428#ifdef DEBUG
429 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
430 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100431 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 i == 1 && nfbno1 == fbno && nflen1 == flen);
433#endif
434 } else {
435 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
436 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100437 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 /*
440 * Look up the record in the by-block tree if necessary.
441 */
442 if (flags & XFSA_FIXUP_BNO_OK) {
443#ifdef DEBUG
444 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
445 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100446 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 i == 1 && nfbno1 == fbno && nflen1 == flen);
448#endif
449 } else {
450 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
451 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100452 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100455#ifdef DEBUG
456 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
457 struct xfs_btree_block *bnoblock;
458 struct xfs_btree_block *cntblock;
459
460 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
461 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
462
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100463 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100464 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Deal with all four cases: the allocated record is contained
470 * within the freespace record, so we can have new freespace
471 * at either (or both) end, or no freespace remaining.
472 */
473 if (rbno == fbno && rlen == flen)
474 nfbno1 = nfbno2 = NULLAGBLOCK;
475 else if (rbno == fbno) {
476 nfbno1 = rbno + rlen;
477 nflen1 = flen - rlen;
478 nfbno2 = NULLAGBLOCK;
479 } else if (rbno + rlen == fbno + flen) {
480 nfbno1 = fbno;
481 nflen1 = flen - rlen;
482 nfbno2 = NULLAGBLOCK;
483 } else {
484 nfbno1 = fbno;
485 nflen1 = rbno - fbno;
486 nfbno2 = rbno + rlen;
487 nflen2 = (fbno + flen) - nfbno2;
488 }
489 /*
490 * Delete the entry from the by-size btree.
491 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100492 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100494 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
496 * Add new by-size btree entry(s).
497 */
498 if (nfbno1 != NULLAGBLOCK) {
499 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
500 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100501 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100502 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100504 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 if (nfbno2 != NULLAGBLOCK) {
507 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
508 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100509 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100510 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100512 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 /*
515 * Fix up the by-block btree entry(s).
516 */
517 if (nfbno1 == NULLAGBLOCK) {
518 /*
519 * No remaining freespace, just delete the by-block tree entry.
520 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100521 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100523 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
525 /*
526 * Update the by-block entry to start later|be shorter.
527 */
528 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
529 return error;
530 }
531 if (nfbno2 != NULLAGBLOCK) {
532 /*
533 * 2 resulting free entries, need to add one.
534 */
535 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
536 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100537 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100538 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100540 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 return 0;
543}
544
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100545static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100546xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100547 struct xfs_buf *bp)
548{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100549 struct xfs_mount *mp = bp->b_target->bt_mount;
550 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100551 int i;
552
Eric Sandeence748ea2015-07-29 11:53:31 +1000553 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100554 return false;
555 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
556 return false;
557 /*
558 * during growfs operations, the perag is not fully initialised,
559 * so we can't use it for any useful checking. growfs ensures we can't
560 * use it by using uncached buffers that don't have the perag attached
561 * so we can detect and avoid this problem.
562 */
563 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
564 return false;
565
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100566 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100567 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100568 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100569 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100570 }
Brian Fostera45086e2015-10-12 15:59:25 +1100571
572 return xfs_log_check_lsn(mp,
573 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100574}
575
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100576static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100577xfs_agfl_read_verify(
578 struct xfs_buf *bp)
579{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100580 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100581
582 /*
583 * There is no verification of non-crc AGFLs because mkfs does not
584 * initialise the AGFL to zero or NULL. Hence the only valid part of the
585 * AGFL is what the AGF says is active. We can't get to the AGF, so we
586 * can't verify just those entries are valid.
587 */
588 if (!xfs_sb_version_hascrc(&mp->m_sb))
589 return;
590
Eric Sandeence5028c2014-02-27 15:23:10 +1100591 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000592 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100593 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000594 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100595
596 if (bp->b_error)
597 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100598}
599
600static void
601xfs_agfl_write_verify(
602 struct xfs_buf *bp)
603{
604 struct xfs_mount *mp = bp->b_target->bt_mount;
605 struct xfs_buf_log_item *bip = bp->b_fspriv;
606
607 /* no verification of non-crc AGFLs */
608 if (!xfs_sb_version_hascrc(&mp->m_sb))
609 return;
610
611 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000612 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100613 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100614 return;
615 }
616
617 if (bip)
618 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
619
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100620 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100621}
622
Dave Chinner1813dd62012-11-14 17:54:40 +1100623const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100624 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100625 .verify_read = xfs_agfl_read_verify,
626 .verify_write = xfs_agfl_write_verify,
627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * Read in the allocation group free block array.
631 */
632STATIC int /* error */
633xfs_alloc_read_agfl(
634 xfs_mount_t *mp, /* mount point structure */
635 xfs_trans_t *tp, /* transaction pointer */
636 xfs_agnumber_t agno, /* allocation group number */
637 xfs_buf_t **bpp) /* buffer for the ag free block array */
638{
639 xfs_buf_t *bp; /* return value */
640 int error;
641
642 ASSERT(agno != NULLAGNUMBER);
643 error = xfs_trans_read_buf(
644 mp, tp, mp->m_ddev_targp,
645 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100646 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (error)
648 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000649 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *bpp = bp;
651 return 0;
652}
653
Christoph Hellwigecb69282011-03-04 12:59:55 +0000654STATIC int
655xfs_alloc_update_counters(
656 struct xfs_trans *tp,
657 struct xfs_perag *pag,
658 struct xfs_buf *agbp,
659 long len)
660{
661 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
662
663 pag->pagf_freeblks += len;
664 be32_add_cpu(&agf->agf_freeblks, len);
665
666 xfs_trans_agblocks_delta(tp, len);
667 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
668 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000669 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000670
671 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
672 return 0;
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * Allocation group level functions.
677 */
678
679/*
680 * Allocate a variable extent in the allocation group agno.
681 * Type and bno are used to determine where in the allocation group the
682 * extent will start.
683 * Extent's length (returned in *len) will be between minlen and maxlen,
684 * and of the form k * prod + mod unless there's nothing that large.
685 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
686 */
687STATIC int /* error */
688xfs_alloc_ag_vextent(
689 xfs_alloc_arg_t *args) /* argument structure for allocation */
690{
691 int error=0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000692 xfs_extlen_t reservation;
693 xfs_extlen_t oldmax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 ASSERT(args->minlen > 0);
696 ASSERT(args->maxlen > 0);
697 ASSERT(args->minlen <= args->maxlen);
698 ASSERT(args->mod < args->prod);
699 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000700
701 /*
702 * Clamp maxlen to the amount of free space minus any reservations
703 * that have been made.
704 */
705 oldmax = args->maxlen;
706 reservation = xfs_ag_resv_needed(args->pag, args->resv);
707 if (args->maxlen > args->pag->pagf_freeblks - reservation)
708 args->maxlen = args->pag->pagf_freeblks - reservation;
709 if (args->maxlen == 0) {
710 args->agbno = NULLAGBLOCK;
711 args->maxlen = oldmax;
712 return 0;
713 }
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * Branch to correct routine based on the type.
717 */
718 args->wasfromfl = 0;
719 switch (args->type) {
720 case XFS_ALLOCTYPE_THIS_AG:
721 error = xfs_alloc_ag_vextent_size(args);
722 break;
723 case XFS_ALLOCTYPE_NEAR_BNO:
724 error = xfs_alloc_ag_vextent_near(args);
725 break;
726 case XFS_ALLOCTYPE_THIS_BNO:
727 error = xfs_alloc_ag_vextent_exact(args);
728 break;
729 default:
730 ASSERT(0);
731 /* NOTREACHED */
732 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000733
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000734 args->maxlen = oldmax;
735
Christoph Hellwigecb69282011-03-04 12:59:55 +0000736 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwigecb69282011-03-04 12:59:55 +0000739 ASSERT(args->len >= args->minlen);
740 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000741 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000742 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Darrick J. Wong673930c2016-08-03 11:33:43 +1000744 /* if not file data, insert new block into the reverse map btree */
745 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
746 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
747 args->agbno, args->len, &args->oinfo);
748 if (error)
749 return error;
750 }
751
Christoph Hellwigecb69282011-03-04 12:59:55 +0000752 if (!args->wasfromfl) {
753 error = xfs_alloc_update_counters(args->tp, args->pag,
754 args->agbp,
755 -((long)(args->len)));
756 if (error)
757 return error;
758
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000759 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000760 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000762
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000763 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000764
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100765 XFS_STATS_INC(args->mp, xs_allocx);
766 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000767 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * Allocate a variable extent at exactly agno/bno.
772 * Extent's length (returned in *len) will be between minlen and maxlen,
773 * and of the form k * prod + mod unless there's nothing that large.
774 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
775 */
776STATIC int /* error */
777xfs_alloc_ag_vextent_exact(
778 xfs_alloc_arg_t *args) /* allocation argument structure */
779{
780 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
781 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int error;
783 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000785 xfs_agblock_t tbno; /* start block of trimmed extent */
786 xfs_extlen_t tlen; /* length of trimmed extent */
787 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /*
793 * Allocate/initialize a cursor for the by-number freespace btree.
794 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100795 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000796 args->agno, XFS_BTNUM_BNO);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /*
799 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
800 * Look for the closest free block <= bno, it must contain bno
801 * if any free block does.
802 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000803 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
804 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000806 if (!i)
807 goto not_found;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * Grab the freespace record.
811 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000812 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100815 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000819 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000821 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000822
823 /*
824 * Give up if the start of the extent is busy, or the freespace isn't
825 * long enough for the minimum request.
826 */
827 if (tbno > args->agbno)
828 goto not_found;
829 if (tlen < args->minlen)
830 goto not_found;
831 tend = tbno + tlen;
832 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000833 goto not_found;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * End of extent will be smaller of the freespace end and the
837 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000838 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * Fix the length according to mod and prod if given.
840 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000841 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
842 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000844 if (!xfs_alloc_fix_minleft(args))
845 goto not_found;
846
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000847 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000850 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 * Allocate/initialize a cursor for the by-size btree.
852 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100853 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
854 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100856 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000857 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
858 args->len, XFSA_FIXUP_BNO_OK);
859 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
861 goto error0;
862 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
865 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000868 trace_xfs_alloc_exact_done(args);
869 return 0;
870
871not_found:
872 /* Didn't find it, return null. */
873 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
874 args->agbno = NULLAGBLOCK;
875 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877
878error0:
879 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000880 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return error;
882}
883
884/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000885 * Search the btree in a given direction via the search cursor and compare
886 * the records found against the good extent we've already found.
887 */
888STATIC int
889xfs_alloc_find_best_extent(
890 struct xfs_alloc_arg *args, /* allocation argument structure */
891 struct xfs_btree_cur **gcur, /* good cursor */
892 struct xfs_btree_cur **scur, /* searching cursor */
893 xfs_agblock_t gdiff, /* difference for search comparison */
894 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000895 xfs_extlen_t *slen, /* extent length */
896 xfs_agblock_t *sbnoa, /* aligned extent found by search */
897 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000898 int dir) /* 0 = search right, 1 = search left */
899{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000900 xfs_agblock_t new;
901 xfs_agblock_t sdiff;
902 int error;
903 int i;
904
905 /* The good extent is perfect, no need to search. */
906 if (!gdiff)
907 goto out_use_good;
908
909 /*
910 * Look until we find a better one, run out of space or run off the end.
911 */
912 do {
913 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
914 if (error)
915 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100916 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000917 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000918
919 /*
920 * The good extent is closer than this one.
921 */
922 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000923 if (*sbnoa > args->max_agbno)
924 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000925 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000926 goto out_use_good;
927 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000928 if (*sbnoa < args->min_agbno)
929 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000930 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000931 goto out_use_good;
932 }
933
934 /*
935 * Same distance, compare length and pick the best.
936 */
937 if (*slena >= args->minlen) {
938 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
939 xfs_alloc_fix_len(args);
940
941 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200942 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000943 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000944 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000945
946 /*
947 * Choose closer size and invalidate other cursor.
948 */
949 if (sdiff < gdiff)
950 goto out_use_search;
951 goto out_use_good;
952 }
953
954 if (!dir)
955 error = xfs_btree_increment(*scur, 0, &i);
956 else
957 error = xfs_btree_decrement(*scur, 0, &i);
958 if (error)
959 goto error0;
960 } while (i);
961
962out_use_good:
963 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
964 *scur = NULL;
965 return 0;
966
967out_use_search:
968 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
969 *gcur = NULL;
970 return 0;
971
972error0:
973 /* caller invalidates cursors */
974 return error;
975}
976
977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * Allocate a variable extent near bno in the allocation group agno.
979 * Extent's length (returned in len) will be between minlen and maxlen,
980 * and of the form k * prod + mod unless there's nothing that large.
981 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
982 */
983STATIC int /* error */
984xfs_alloc_ag_vextent_near(
985 xfs_alloc_arg_t *args) /* allocation argument structure */
986{
987 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
988 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
989 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xfs_agblock_t gtbno; /* start bno of right side entry */
991 xfs_agblock_t gtbnoa; /* aligned ... */
992 xfs_extlen_t gtdiff; /* difference to right side entry */
993 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000994 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 xfs_agblock_t gtnew; /* useful start bno of right side */
996 int error; /* error code */
997 int i; /* result code, temporary */
998 int j; /* result code, temporary */
999 xfs_agblock_t ltbno; /* start bno of left side entry */
1000 xfs_agblock_t ltbnoa; /* aligned ... */
1001 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001003 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xfs_agblock_t ltnew; /* useful start bno of left side */
1005 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001006 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +10001007#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * Randomly don't execute the first algorithm.
1010 */
1011 int dofirst; /* set to do first algorithm */
1012
Akinobu Mitaecb34032013-03-04 21:58:20 +09001013 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +00001015
Brian Fosterbfe46d42015-05-29 08:53:00 +10001016 /* handle unitialized agbno range so caller doesn't have to */
1017 if (!args->min_agbno && !args->max_agbno)
1018 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1019 ASSERT(args->min_agbno <= args->max_agbno);
1020
1021 /* clamp agbno to the range if it's outside */
1022 if (args->agbno < args->min_agbno)
1023 args->agbno = args->min_agbno;
1024 if (args->agbno > args->max_agbno)
1025 args->agbno = args->max_agbno;
1026
Christoph Hellwige26f0502011-04-24 19:06:15 +00001027restart:
1028 bno_cur_lt = NULL;
1029 bno_cur_gt = NULL;
1030 ltlen = 0;
1031 gtlena = 0;
1032 ltlena = 0;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * Get a cursor for the by-size btree.
1036 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001037 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1038 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * See if there are any free extents as big as maxlen.
1042 */
1043 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1044 goto error0;
1045 /*
1046 * If none, then pick up the last entry in the tree unless the
1047 * tree is empty.
1048 */
1049 if (!i) {
1050 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1051 &ltlen, &i)))
1052 goto error0;
1053 if (i == 0 || ltlen == 0) {
1054 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001055 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
1057 }
1058 ASSERT(i == 1);
1059 }
1060 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * First algorithm.
1064 * If the requested extent is large wrt the freespaces available
1065 * in this a.g., then the cursor will be pointing to a btree entry
1066 * near the right edge of the tree. If it's in the last btree leaf
1067 * block, then we just examine all the entries in that block
1068 * that are big enough, and pick the best one.
1069 * This is written as a while loop so we can break out of it,
1070 * but we never loop back to the top.
1071 */
1072 while (xfs_btree_islastblock(cnt_cur, 0)) {
1073 xfs_extlen_t bdiff;
1074 int besti=0;
1075 xfs_extlen_t blen=0;
1076 xfs_agblock_t bnew=0;
1077
Dave Chinner63d20d62013-08-12 20:49:50 +10001078#ifdef DEBUG
1079 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081#endif
1082 /*
1083 * Start from the entry that lookup found, sequence through
1084 * all larger free blocks. If we're actually pointing at a
1085 * record smaller than maxlen, go to the start of this block,
1086 * and skip all those smaller than minlen.
1087 */
1088 if (ltlen || args->alignment > 1) {
1089 cnt_cur->bc_ptrs[0] = 1;
1090 do {
1091 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1092 &ltlen, &i)))
1093 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001094 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (ltlen >= args->minlen)
1096 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001097 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto error0;
1099 } while (i);
1100 ASSERT(ltlen >= args->minlen);
1101 if (!i)
1102 break;
1103 }
1104 i = cnt_cur->bc_ptrs[0];
1105 for (j = 1, blen = 0, bdiff = 0;
1106 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001107 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /*
1109 * For each entry, decide if it's better than
1110 * the previous best entry.
1111 */
1112 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1113 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001114 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001115 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1116 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001117 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001119 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1120 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1122 xfs_alloc_fix_len(args);
1123 ASSERT(args->len >= args->minlen);
1124 if (args->len < blen)
1125 continue;
1126 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001127 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001128 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (ltnew != NULLAGBLOCK &&
1130 (args->len > blen || ltdiff < bdiff)) {
1131 bdiff = ltdiff;
1132 bnew = ltnew;
1133 blen = args->len;
1134 besti = cnt_cur->bc_ptrs[0];
1135 }
1136 }
1137 /*
1138 * It didn't work. We COULD be in a case where
1139 * there's a good record somewhere, so try again.
1140 */
1141 if (blen == 0)
1142 break;
1143 /*
1144 * Point at the best entry, and retrieve it again.
1145 */
1146 cnt_cur->bc_ptrs[0] = besti;
1147 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1148 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001149 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001150 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 args->len = blen;
1152 if (!xfs_alloc_fix_minleft(args)) {
1153 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001154 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156 }
1157 blen = args->len;
1158 /*
1159 * We are allocating starting at bnew for blen blocks.
1160 */
1161 args->agbno = bnew;
1162 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001163 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Set up a cursor for the by-bno tree.
1166 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001167 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1168 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * Fix up the btree entries.
1171 */
1172 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1173 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1174 goto error0;
1175 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1176 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001177
1178 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180 }
1181 /*
1182 * Second algorithm.
1183 * Search in the by-bno tree to the left and to the right
1184 * simultaneously, until in each case we find a space big enough,
1185 * or run into the edge of the tree. When we run into the edge,
1186 * we deallocate that cursor.
1187 * If both searches succeed, we compare the two spaces and pick
1188 * the better one.
1189 * With alignment, it's possible for both to fail; the upper
1190 * level algorithm that picks allocation groups for allocations
1191 * is not supposed to do this.
1192 */
1193 /*
1194 * Allocate and initialize the cursor for the leftward search.
1195 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001196 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1197 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
1199 * Lookup <= bno to find the leftward search's starting point.
1200 */
1201 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1202 goto error0;
1203 if (!i) {
1204 /*
1205 * Didn't find anything; use this cursor for the rightward
1206 * search.
1207 */
1208 bno_cur_gt = bno_cur_lt;
1209 bno_cur_lt = NULL;
1210 }
1211 /*
1212 * Found something. Duplicate the cursor for the rightward search.
1213 */
1214 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1215 goto error0;
1216 /*
1217 * Increment the cursor, so we will point at the entry just right
1218 * of the leftward entry if any, or to the leftmost entry.
1219 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001220 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto error0;
1222 if (!i) {
1223 /*
1224 * It failed, there are no rightward entries.
1225 */
1226 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1227 bno_cur_gt = NULL;
1228 }
1229 /*
1230 * Loop going left with the leftward cursor, right with the
1231 * rightward cursor, until either both directions give up or
1232 * we find an entry at least as big as minlen.
1233 */
1234 do {
1235 if (bno_cur_lt) {
1236 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1237 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001238 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001239 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1240 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001241 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001243 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001245 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 xfs_btree_del_cursor(bno_cur_lt,
1247 XFS_BTREE_NOERROR);
1248 bno_cur_lt = NULL;
1249 }
1250 }
1251 if (bno_cur_gt) {
1252 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1253 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001254 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001255 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1256 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001257 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001259 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001261 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 xfs_btree_del_cursor(bno_cur_gt,
1263 XFS_BTREE_NOERROR);
1264 bno_cur_gt = NULL;
1265 }
1266 }
1267 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * Got both cursors still active, need to find better entry.
1271 */
1272 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (ltlena >= args->minlen) {
1274 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001275 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
1277 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1278 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001279 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001280 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001281 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001282
1283 error = xfs_alloc_find_best_extent(args,
1284 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001285 ltdiff, &gtbno, &gtlen,
1286 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001287 0 /* search right */);
1288 } else {
1289 ASSERT(gtlena >= args->minlen);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001292 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
1294 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1295 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001296 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001297 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001298 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001299
1300 error = xfs_alloc_find_best_extent(args,
1301 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001302 gtdiff, &ltbno, &ltlen,
1303 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001304 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001306
1307 if (error)
1308 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * If we couldn't get anything, give up.
1313 */
1314 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001315 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1316
Christoph Hellwige26f0502011-04-24 19:06:15 +00001317 if (!forced++) {
1318 trace_xfs_alloc_near_busy(args);
1319 xfs_log_force(args->mp, XFS_LOG_SYNC);
1320 goto restart;
1321 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001322 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 args->agbno = NULLAGBLOCK;
1324 return 0;
1325 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * At this point we have selected a freespace entry, either to the
1329 * left or to the right. If it's on the right, copy all the
1330 * useful variables to the "left" set so we only have one
1331 * copy of this code.
1332 */
1333 if (bno_cur_gt) {
1334 bno_cur_lt = bno_cur_gt;
1335 bno_cur_gt = NULL;
1336 ltbno = gtbno;
1337 ltbnoa = gtbnoa;
1338 ltlen = gtlen;
1339 ltlena = gtlena;
1340 j = 1;
1341 } else
1342 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
1345 * Fix up the length and compute the useful address.
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1348 xfs_alloc_fix_len(args);
1349 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001350 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1352 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1353 return 0;
1354 }
1355 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001356 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001357 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001359 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001360 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001361 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1365 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1366 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001367
1368 if (j)
1369 trace_xfs_alloc_near_greater(args);
1370 else
1371 trace_xfs_alloc_near_lesser(args);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1374 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1375 return 0;
1376
1377 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001378 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (cnt_cur != NULL)
1380 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1381 if (bno_cur_lt != NULL)
1382 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1383 if (bno_cur_gt != NULL)
1384 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1385 return error;
1386}
1387
1388/*
1389 * Allocate a variable extent anywhere in the allocation group agno.
1390 * Extent's length (returned in len) will be between minlen and maxlen,
1391 * and of the form k * prod + mod unless there's nothing that large.
1392 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1393 */
1394STATIC int /* error */
1395xfs_alloc_ag_vextent_size(
1396 xfs_alloc_arg_t *args) /* allocation argument structure */
1397{
1398 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1399 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1400 int error; /* error result */
1401 xfs_agblock_t fbno; /* start of found freespace */
1402 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 int i; /* temp status variable */
1404 xfs_agblock_t rbno; /* returned block number */
1405 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001406 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Christoph Hellwige26f0502011-04-24 19:06:15 +00001408restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /*
1410 * Allocate and initialize a cursor for the by-size btree.
1411 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001412 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1413 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /*
1417 * Look for an entry >= maxlen+alignment-1 blocks.
1418 */
1419 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1420 args->maxlen + args->alignment - 1, &i)))
1421 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001424 * If none or we have busy extents that we cannot allocate from, then
1425 * we have to settle for a smaller extent. In the case that there are
1426 * no large extents, this will return the last entry in the tree unless
1427 * the tree is empty. In the case that there are only busy large
1428 * extents, this will return the largest small extent unless there
1429 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001431 if (!i || forced > 1) {
1432 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1433 &fbno, &flen, &i);
1434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto error0;
1436 if (i == 0 || flen == 0) {
1437 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001438 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
1440 }
1441 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001442 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1443 } else {
1444 /*
1445 * Search for a non-busy extent that is large enough.
1446 * If we are at low space, don't check, or if we fall of
1447 * the end of the btree, turn off the busy check and
1448 * restart.
1449 */
1450 for (;;) {
1451 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1452 if (error)
1453 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001454 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001455
1456 xfs_alloc_compute_aligned(args, fbno, flen,
1457 &rbno, &rlen);
1458
1459 if (rlen >= args->maxlen)
1460 break;
1461
1462 error = xfs_btree_increment(cnt_cur, 0, &i);
1463 if (error)
1464 goto error0;
1465 if (i == 0) {
1466 /*
1467 * Our only valid extents must have been busy.
1468 * Make it unbusy by forcing the log out and
1469 * retrying. If we've been here before, forcing
1470 * the log isn't making the extents available,
1471 * which means they have probably been freed in
1472 * this transaction. In that case, we have to
1473 * give up on them and we'll attempt a minlen
1474 * allocation the next time around.
1475 */
1476 xfs_btree_del_cursor(cnt_cur,
1477 XFS_BTREE_NOERROR);
1478 trace_xfs_alloc_size_busy(args);
1479 if (!forced++)
1480 xfs_log_force(args->mp, XFS_LOG_SYNC);
1481 goto restart;
1482 }
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * In the first case above, we got the last entry in the
1488 * by-size btree. Now we check to see if the space hits maxlen
1489 * once aligned; if not, we search left for something better.
1490 * This can't happen in the second case above.
1491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001493 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1495 if (rlen < args->maxlen) {
1496 xfs_agblock_t bestfbno;
1497 xfs_extlen_t bestflen;
1498 xfs_agblock_t bestrbno;
1499 xfs_extlen_t bestrlen;
1500
1501 bestrlen = rlen;
1502 bestrbno = rbno;
1503 bestflen = flen;
1504 bestfbno = fbno;
1505 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001506 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto error0;
1508 if (i == 0)
1509 break;
1510 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1511 &i)))
1512 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001513 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (flen < bestrlen)
1515 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001516 xfs_alloc_compute_aligned(args, fbno, flen,
1517 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001519 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 (rlen <= flen && rbno + rlen <= fbno + flen),
1521 error0);
1522 if (rlen > bestrlen) {
1523 bestrlen = rlen;
1524 bestrbno = rbno;
1525 bestflen = flen;
1526 bestfbno = fbno;
1527 if (rlen == args->maxlen)
1528 break;
1529 }
1530 }
1531 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1532 &i)))
1533 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001534 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 rlen = bestrlen;
1536 rbno = bestrbno;
1537 flen = bestflen;
1538 fbno = bestfbno;
1539 }
1540 args->wasfromfl = 0;
1541 /*
1542 * Fix up the length.
1543 */
1544 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001545 if (rlen < args->minlen) {
1546 if (!forced++) {
1547 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1548 trace_xfs_alloc_size_busy(args);
1549 xfs_log_force(args->mp, XFS_LOG_SYNC);
1550 goto restart;
1551 }
1552 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001554 xfs_alloc_fix_len(args);
1555
1556 if (!xfs_alloc_fix_minleft(args))
1557 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001559 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /*
1561 * Allocate and initialize a cursor for the by-block tree.
1562 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001563 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1564 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1566 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1567 goto error0;
1568 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1569 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1570 cnt_cur = bno_cur = NULL;
1571 args->len = rlen;
1572 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001573 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001575 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001577 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579
1580error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001581 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (cnt_cur)
1583 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1584 if (bno_cur)
1585 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1586 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001587
1588out_nominleft:
1589 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1590 trace_xfs_alloc_size_nominleft(args);
1591 args->agbno = NULLAGBLOCK;
1592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/*
1596 * Deal with the case where only small freespaces remain.
1597 * Either return the contents of the last freespace record,
1598 * or allocate space from the freelist if there is nothing in the tree.
1599 */
1600STATIC int /* error */
1601xfs_alloc_ag_vextent_small(
1602 xfs_alloc_arg_t *args, /* allocation argument structure */
1603 xfs_btree_cur_t *ccur, /* by-size cursor */
1604 xfs_agblock_t *fbnop, /* result block number */
1605 xfs_extlen_t *flenp, /* result length */
1606 int *stat) /* status: 0-freelist, 1-normal/none */
1607{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001608 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001609 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int error;
1611 xfs_agblock_t fbno;
1612 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int i;
1614
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001615 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto error0;
1617 if (i) {
1618 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1619 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001620 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 /*
1623 * Nothing in the btree, try the freelist. Make sure
1624 * to respect minleft even when pulling from the
1625 * freelist.
1626 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001627 else if (args->minlen == 1 && args->alignment == 1 &&
1628 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001629 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1630 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001631 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1632 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto error0;
1634 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001635 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001636 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001637
Dave Chinner292378e2016-09-26 08:21:28 +10001638 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 xfs_buf_t *bp;
1640
1641 bp = xfs_btree_get_bufs(args->mp, args->tp,
1642 args->agno, fbno, 0);
1643 xfs_trans_binval(args->tp, bp);
1644 }
1645 args->len = 1;
1646 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001647 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001649 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 error0);
1651 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001652 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001653
1654 /*
1655 * If we're feeding an AGFL block to something that
1656 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001657 * out the OWN_AG rmap and add the block back to
1658 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001659 */
1660 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1661 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1662 fbno, 1, &oinfo);
1663 if (error)
1664 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001665 pag = xfs_perag_get(args->mp, args->agno);
1666 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1667 args->tp, 1);
1668 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *stat = 0;
1671 return 0;
1672 }
1673 /*
1674 * Nothing in the freelist.
1675 */
1676 else
1677 flen = 0;
1678 }
1679 /*
1680 * Can't allocate from the freelist for some reason.
1681 */
Nathan Scottd432c802006-09-28 11:03:44 +10001682 else {
1683 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Can't do the allocation, give up.
1688 */
1689 if (flen < args->minlen) {
1690 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001691 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 flen = 0;
1693 }
1694 *fbnop = fbno;
1695 *flenp = flen;
1696 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001697 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699
1700error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001701 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return error;
1703}
1704
1705/*
1706 * Free the extent starting at agno/bno for length.
1707 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001708STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001710 xfs_trans_t *tp,
1711 xfs_buf_t *agbp,
1712 xfs_agnumber_t agno,
1713 xfs_agblock_t bno,
1714 xfs_extlen_t len,
1715 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001716 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1719 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1720 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 xfs_agblock_t gtbno; /* start of right neighbor block */
1722 xfs_extlen_t gtlen; /* length of right neighbor block */
1723 int haveleft; /* have a left neighbor block */
1724 int haveright; /* have a right neighbor block */
1725 int i; /* temp, result code */
1726 xfs_agblock_t ltbno; /* start of left neighbor block */
1727 xfs_extlen_t ltlen; /* length of left neighbor block */
1728 xfs_mount_t *mp; /* mount point struct for filesystem */
1729 xfs_agblock_t nbno; /* new starting block of freespace */
1730 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001731 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Darrick J. Wong673930c2016-08-03 11:33:43 +10001733 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001735
1736 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1737 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1738 if (error)
1739 goto error0;
1740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * Allocate and initialize a cursor for the by-block btree.
1744 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001745 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * Look for a neighboring block on the left (lower block numbers)
1748 * that is contiguous with this space.
1749 */
1750 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1751 goto error0;
1752 if (haveleft) {
1753 /*
1754 * There is a block to our left.
1755 */
1756 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1757 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001758 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * It's not contiguous, though.
1761 */
1762 if (ltbno + ltlen < bno)
1763 haveleft = 0;
1764 else {
1765 /*
1766 * If this failure happens the request to free this
1767 * space was invalid, it's (partly) already free.
1768 * Very bad.
1769 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001770 XFS_WANT_CORRUPTED_GOTO(mp,
1771 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 }
1774 /*
1775 * Look for a neighboring block on the right (higher block numbers)
1776 * that is contiguous with this space.
1777 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001778 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto error0;
1780 if (haveright) {
1781 /*
1782 * There is a block to our right.
1783 */
1784 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1785 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001786 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * It's not contiguous, though.
1789 */
1790 if (bno + len < gtbno)
1791 haveright = 0;
1792 else {
1793 /*
1794 * If this failure happens the request to free this
1795 * space was invalid, it's (partly) already free.
1796 * Very bad.
1797 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001798 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 }
1801 /*
1802 * Now allocate and initialize a cursor for the by-size tree.
1803 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001804 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /*
1806 * Have both left and right contiguous neighbors.
1807 * Merge all three into a single free block.
1808 */
1809 if (haveleft && haveright) {
1810 /*
1811 * Delete the old by-size entry on the left.
1812 */
1813 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1814 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001816 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Delete the old by-size entry on the right.
1821 */
1822 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1823 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001825 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001827 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * Delete the old by-block entry for the right block.
1830 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001831 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001833 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /*
1835 * Move the by-block cursor back to the left neighbor.
1836 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001837 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001839 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840#ifdef DEBUG
1841 /*
1842 * Check that this is the right record: delete didn't
1843 * mangle the cursor.
1844 */
1845 {
1846 xfs_agblock_t xxbno;
1847 xfs_extlen_t xxlen;
1848
1849 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1850 &i)))
1851 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001852 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 i == 1 && xxbno == ltbno && xxlen == ltlen,
1854 error0);
1855 }
1856#endif
1857 /*
1858 * Update remaining by-block entry to the new, joined block.
1859 */
1860 nbno = ltbno;
1861 nlen = len + ltlen + gtlen;
1862 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1863 goto error0;
1864 }
1865 /*
1866 * Have only a left contiguous neighbor.
1867 * Merge it together with the new freespace.
1868 */
1869 else if (haveleft) {
1870 /*
1871 * Delete the old by-size entry on the left.
1872 */
1873 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1874 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001875 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001876 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Back up the by-block cursor to the left neighbor, and
1881 * update its length.
1882 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001883 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001885 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 nbno = ltbno;
1887 nlen = len + ltlen;
1888 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1889 goto error0;
1890 }
1891 /*
1892 * Have only a right contiguous neighbor.
1893 * Merge it together with the new freespace.
1894 */
1895 else if (haveright) {
1896 /*
1897 * Delete the old by-size entry on the right.
1898 */
1899 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1900 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001901 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001902 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001904 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /*
1906 * Update the starting block and length of the right
1907 * neighbor in the by-block tree.
1908 */
1909 nbno = bno;
1910 nlen = len + gtlen;
1911 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1912 goto error0;
1913 }
1914 /*
1915 * No contiguous neighbors.
1916 * Insert the new freespace into the by-block tree.
1917 */
1918 else {
1919 nbno = bno;
1920 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001921 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001923 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1926 bno_cur = NULL;
1927 /*
1928 * In all cases we need to insert the new freespace in the by-size tree.
1929 */
1930 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1931 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001932 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001933 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001935 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1937 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /*
1940 * Update the freespace totals in the ag and superblock.
1941 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001942 pag = xfs_perag_get(mp, agno);
1943 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001944 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001945 xfs_perag_put(pag);
1946 if (error)
1947 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001949 XFS_STATS_INC(mp, xs_freex);
1950 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001951
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001952 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1953 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956
1957 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001958 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1959 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (bno_cur)
1961 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1962 if (cnt_cur)
1963 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1964 return error;
1965}
1966
1967/*
1968 * Visible (exported) allocation/free functions.
1969 * Some of these are used just by xfs_alloc_btree.c and this file.
1970 */
1971
1972/*
1973 * Compute and fill in value of m_ag_maxlevels.
1974 */
1975void
1976xfs_alloc_compute_maxlevels(
1977 xfs_mount_t *mp) /* file system mount structure */
1978{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001979 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1980 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001984 * Find the length of the longest extent in an AG. The 'need' parameter
1985 * specifies how much space we're going to need for the AGFL and the
1986 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1987 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001988 */
1989xfs_extlen_t
1990xfs_alloc_longest_free_extent(
1991 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001992 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001993 xfs_extlen_t need,
1994 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001995{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001996 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001997
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001998 /*
1999 * If the AGFL needs a recharge, we'll have to subtract that from the
2000 * longest extent.
2001 */
Dave Chinner6cc87642009-03-16 08:29:46 +01002002 if (need > pag->pagf_flcount)
2003 delta = need - pag->pagf_flcount;
2004
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002005 /*
2006 * If we cannot maintain others' reservations with space from the
2007 * not-longest freesp extents, we'll have to subtract /that/ from
2008 * the longest extent too.
2009 */
2010 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2011 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2012
2013 /*
2014 * If the longest extent is long enough to satisfy all the
2015 * reservations and AGFL rules in place, we can return this extent.
2016 */
Dave Chinner6cc87642009-03-16 08:29:46 +01002017 if (pag->pagf_longest > delta)
2018 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002019
2020 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01002021 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2022}
2023
Dave Chinner496817b2015-06-22 10:13:30 +10002024unsigned int
2025xfs_alloc_min_freelist(
2026 struct xfs_mount *mp,
2027 struct xfs_perag *pag)
2028{
2029 unsigned int min_free;
2030
2031 /* space needed by-bno freespace btree */
2032 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2033 mp->m_ag_maxlevels);
2034 /* space needed by-size freespace btree */
2035 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2036 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10002037 /* space needed reverse mapping used space btree */
2038 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2039 min_free += min_t(unsigned int,
2040 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2041 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10002042
2043 return min_free;
2044}
2045
Dave Chinner6cc87642009-03-16 08:29:46 +01002046/*
Dave Chinner72d55282015-06-22 10:04:42 +10002047 * Check if the operation we are fixing up the freelist for should go ahead or
2048 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2049 * is dependent on whether the size and shape of free space available will
2050 * permit the requested allocation to take place.
2051 */
2052static bool
2053xfs_alloc_space_available(
2054 struct xfs_alloc_arg *args,
2055 xfs_extlen_t min_free,
2056 int flags)
2057{
2058 struct xfs_perag *pag = args->pag;
2059 xfs_extlen_t longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002060 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002061 int available;
2062
2063 if (flags & XFS_ALLOC_FLAG_FREEING)
2064 return true;
2065
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002066 reservation = xfs_ag_resv_needed(pag, args->resv);
2067
Dave Chinner72d55282015-06-22 10:04:42 +10002068 /* do we have enough contiguous free space for the allocation? */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002069 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2070 reservation);
Dave Chinner72d55282015-06-22 10:04:42 +10002071 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2072 return false;
2073
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002074 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002075 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002076 reservation - min_free - args->total);
2077 if (available < (int)args->minleft || available <= 0)
Dave Chinner72d55282015-06-22 10:04:42 +10002078 return false;
2079
2080 return true;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * Decide whether to use this allocation group for this allocation.
2085 * If so, fix up the btree freelist's size.
2086 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002087int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002089 struct xfs_alloc_arg *args, /* allocation argument structure */
2090 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Dave Chinner396503f2015-06-22 10:13:19 +10002092 struct xfs_mount *mp = args->mp;
2093 struct xfs_perag *pag = args->pag;
2094 struct xfs_trans *tp = args->tp;
2095 struct xfs_buf *agbp = NULL;
2096 struct xfs_buf *agflbp = NULL;
2097 struct xfs_alloc_arg targs; /* local allocation arguments */
2098 xfs_agblock_t bno; /* freelist block */
2099 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002100 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002103 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2104 if (error)
2105 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002107 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2108 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002109 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Dave Chinner396503f2015-06-22 10:13:19 +10002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002113 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002114 * If this is a metadata preferred pag and we are user data then try
2115 * somewhere else if we are not being asked to try harder at this
2116 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 */
Dave Chinner292378e2016-09-26 08:21:28 +10002118 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002119 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2120 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002121 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
Dave Chinner496817b2015-06-22 10:13:30 +10002124 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002125 if (!xfs_alloc_space_available(args, need, flags))
2126 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Get the a.g. freespace buffer.
2130 * Can fail if we're not blocking on locks, and it's held.
2131 */
Dave Chinner396503f2015-06-22 10:13:19 +10002132 if (!agbp) {
2133 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2134 if (error)
2135 goto out_no_agbp;
2136 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002137 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2138 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002139 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002142
Dave Chinner50adbcb2015-06-22 10:04:31 +10002143 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002144 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002145 if (!xfs_alloc_space_available(args, need, flags))
2146 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /*
2149 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002150 *
Dave Chinner396503f2015-06-22 10:13:19 +10002151 * Note that from this point onwards, we will always release the agf and
2152 * agfl buffers on error. This handles the case where we error out and
2153 * the buffers are clean or may not have been joined to the transaction
2154 * and hence need to be released manually. If they have been joined to
2155 * the transaction, then xfs_trans_brelse() will handle them
2156 * appropriately based on the recursion count and dirty state of the
2157 * buffer.
2158 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002159 * XXX (dgc): When we have lots of free space, does this buy us
2160 * anything other than extra overhead when we need to put more blocks
2161 * back on the free list? Maybe we should only do this when space is
2162 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002163 *
2164 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2165 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2166 * updating the rmapbt. Both flags are used in xfs_repair while we're
2167 * rebuilding the rmapbt, and neither are used by the kernel. They're
2168 * both required to ensure that rmaps are correctly recorded for the
2169 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2170 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002172 memset(&targs, 0, sizeof(targs));
2173 if (flags & XFS_ALLOC_FLAG_NORMAP)
2174 xfs_rmap_skip_owner_update(&targs.oinfo);
2175 else
2176 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2177 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002178 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
David Chinner92821e22007-05-24 15:26:31 +10002180 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2181 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002182 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002183 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002184 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002185 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002186 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2188 xfs_trans_binval(tp, bp);
2189 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 targs.tp = tp;
2192 targs.mp = mp;
2193 targs.agbp = agbp;
2194 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002195 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 targs.type = XFS_ALLOCTYPE_THIS_AG;
2197 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002198 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2199 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002200 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002201
2202 /* Make the freelist longer if it's too short. */
2203 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002205 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002206 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002207
2208 /* Allocate as many blocks as possible at once. */
2209 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002210 if (error)
2211 goto out_agflbp_relse;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Stop if we run out. Won't happen if callers are obeying
2215 * the restrictions correctly. Can happen for free calls
2216 * on a completely full ag.
2217 */
Yingping Lud210a282006-06-09 14:55:18 +10002218 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002219 if (flags & XFS_ALLOC_FLAG_FREEING)
2220 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002221 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /*
2224 * Put each allocated block on the list.
2225 */
2226 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002227 error = xfs_alloc_put_freelist(tp, agbp,
2228 agflbp, bno, 0);
2229 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002230 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002233 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 args->agbp = agbp;
2235 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002236
2237out_agflbp_relse:
2238 xfs_trans_brelse(tp, agflbp);
2239out_agbp_relse:
2240 if (agbp)
2241 xfs_trans_brelse(tp, agbp);
2242out_no_agbp:
2243 args->agbp = NULL;
2244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247/*
2248 * Get a block from the freelist.
2249 * Returns with the buffer for the block gotten.
2250 */
2251int /* error */
2252xfs_alloc_get_freelist(
2253 xfs_trans_t *tp, /* transaction pointer */
2254 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002255 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2256 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2260 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002261 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int error;
David Chinner92821e22007-05-24 15:26:31 +10002263 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002264 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 xfs_perag_t *pag; /* per allocation group data */
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /*
2268 * Freelist is empty, give up.
2269 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002270 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (!agf->agf_flcount) {
2272 *bnop = NULLAGBLOCK;
2273 return 0;
2274 }
2275 /*
2276 * Read the array of free blocks.
2277 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002278 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2279 &agflbp);
2280 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002282
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /*
2285 * Get the block number and update the data structures.
2286 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002287 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2288 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002289 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002291 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002293
2294 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002295 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 xfs_trans_agflist_delta(tp, -1);
2297 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002298 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002299
2300 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2301 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002302 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002303 pag->pagf_btreeblks++;
2304 logflags |= XFS_AGF_BTREEBLKS;
2305 }
2306
David Chinner92821e22007-05-24 15:26:31 +10002307 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 *bnop = bno;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return 0;
2311}
2312
2313/*
2314 * Log the given fields from the agf structure.
2315 */
2316void
2317xfs_alloc_log_agf(
2318 xfs_trans_t *tp, /* transaction pointer */
2319 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2320 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2321{
2322 int first; /* first byte offset */
2323 int last; /* last byte offset */
2324 static const short offsets[] = {
2325 offsetof(xfs_agf_t, agf_magicnum),
2326 offsetof(xfs_agf_t, agf_versionnum),
2327 offsetof(xfs_agf_t, agf_seqno),
2328 offsetof(xfs_agf_t, agf_length),
2329 offsetof(xfs_agf_t, agf_roots[0]),
2330 offsetof(xfs_agf_t, agf_levels[0]),
2331 offsetof(xfs_agf_t, agf_flfirst),
2332 offsetof(xfs_agf_t, agf_fllast),
2333 offsetof(xfs_agf_t, agf_flcount),
2334 offsetof(xfs_agf_t, agf_freeblks),
2335 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002336 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002337 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002338 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongbdf28632016-10-03 09:11:19 -07002339 offsetof(xfs_agf_t, agf_refcount_blocks),
2340 offsetof(xfs_agf_t, agf_refcount_root),
2341 offsetof(xfs_agf_t, agf_refcount_level),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002342 /* needed so that we don't log the whole rest of the structure: */
2343 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 sizeof(xfs_agf_t)
2345 };
2346
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002347 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2348
Dave Chinner61fe1352013-04-03 16:11:30 +11002349 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2352 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2353}
2354
2355/*
2356 * Interface for inode allocation to force the pag data to be initialized.
2357 */
2358int /* error */
2359xfs_alloc_pagf_init(
2360 xfs_mount_t *mp, /* file system mount structure */
2361 xfs_trans_t *tp, /* transaction pointer */
2362 xfs_agnumber_t agno, /* allocation group number */
2363 int flags) /* XFS_ALLOC_FLAGS_... */
2364{
2365 xfs_buf_t *bp;
2366 int error;
2367
2368 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2369 return error;
2370 if (bp)
2371 xfs_trans_brelse(tp, bp);
2372 return 0;
2373}
2374
2375/*
2376 * Put the block on the freelist for the allocation group.
2377 */
2378int /* error */
2379xfs_alloc_put_freelist(
2380 xfs_trans_t *tp, /* transaction pointer */
2381 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2382 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002383 xfs_agblock_t bno, /* block being freed */
2384 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002387 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 int error;
David Chinner92821e22007-05-24 15:26:31 +10002389 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 xfs_mount_t *mp; /* mount structure */
2391 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002392 __be32 *agfl_bno;
2393 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 agf = XFS_BUF_TO_AGF(agbp);
2396 mp = tp->t_mountp;
2397
2398 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002399 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002401 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002402 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002404
2405 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002406 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 xfs_trans_agflist_delta(tp, 1);
2408 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002409
2410 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2411 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002412 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002413 pag->pagf_btreeblks--;
2414 logflags |= XFS_AGF_BTREEBLKS;
2415 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002416 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002417
David Chinner92821e22007-05-24 15:26:31 +10002418 xfs_alloc_log_agf(tp, agbp, logflags);
2419
Christoph Hellwig16259e72005-11-02 15:11:25 +11002420 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002421
2422 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2423 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002424 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002425 startoff = (char *)blockp - (char *)agflbp->b_addr;
2426
David Chinner92821e22007-05-24 15:26:31 +10002427 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002428
Dave Chinner61fe1352013-04-03 16:11:30 +11002429 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002430 xfs_trans_log_buf(tp, agflbp, startoff,
2431 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return 0;
2433}
2434
Dave Chinner4e0e6042013-04-03 16:11:13 +11002435static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002436xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002437 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002438 struct xfs_buf *bp)
2439 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002440 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002441
Brian Fostera45086e2015-10-12 15:59:25 +11002442 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2443 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002444 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002445 if (!xfs_log_check_lsn(mp,
2446 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2447 return false;
2448 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002449
Dave Chinner4e0e6042013-04-03 16:11:13 +11002450 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2451 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2452 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2453 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2454 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2455 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2456 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002457
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002458 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
2459 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
2460 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
Eric Sandeene1b05722014-09-09 11:47:24 +10002461 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2462 return false;
2463
Darrick J. Wongb8704942016-08-03 11:30:32 +10002464 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002465 (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
2466 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
Darrick J. Wongb8704942016-08-03 11:30:32 +10002467 return false;
2468
Dave Chinner5d5f5272012-11-14 17:44:56 +11002469 /*
2470 * during growfs operations, the perag is not fully initialised,
2471 * so we can't use it for any useful checking. growfs ensures we can't
2472 * use it by using uncached buffers that don't have the perag attached
2473 * so we can detect and avoid this problem.
2474 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002475 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2476 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002477
Dave Chinner4e0e6042013-04-03 16:11:13 +11002478 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2479 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2480 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002481
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002482 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002483 (be32_to_cpu(agf->agf_refcount_level) < 1 ||
2484 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002485 return false;
2486
Dave Chinner4e0e6042013-04-03 16:11:13 +11002487 return true;;
2488
Dave Chinner612cfbf2012-11-14 17:52:32 +11002489}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002490
Dave Chinner1813dd62012-11-14 17:54:40 +11002491static void
2492xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002493 struct xfs_buf *bp)
2494{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002495 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002496
Eric Sandeence5028c2014-02-27 15:23:10 +11002497 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2498 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002499 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002500 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2501 XFS_ERRTAG_ALLOC_READ_AGF,
2502 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002503 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002504
2505 if (bp->b_error)
2506 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002507}
2508
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002509static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002510xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002511 struct xfs_buf *bp)
2512{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002513 struct xfs_mount *mp = bp->b_target->bt_mount;
2514 struct xfs_buf_log_item *bip = bp->b_fspriv;
2515
2516 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002517 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002518 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002519 return;
2520 }
2521
2522 if (!xfs_sb_version_hascrc(&mp->m_sb))
2523 return;
2524
2525 if (bip)
2526 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2527
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002528 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002529}
2530
Dave Chinner1813dd62012-11-14 17:54:40 +11002531const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002532 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002533 .verify_read = xfs_agf_read_verify,
2534 .verify_write = xfs_agf_write_verify,
2535};
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/*
2538 * Read in the allocation group header (free/alloc section).
2539 */
2540int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002541xfs_read_agf(
2542 struct xfs_mount *mp, /* mount point structure */
2543 struct xfs_trans *tp, /* transaction pointer */
2544 xfs_agnumber_t agno, /* allocation group number */
2545 int flags, /* XFS_BUF_ */
2546 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int error;
2549
Dave Chinnerd1230312013-11-01 15:27:19 +11002550 trace_xfs_read_agf(mp, agno);
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 ASSERT(agno != NULLAGNUMBER);
2553 error = xfs_trans_read_buf(
2554 mp, tp, mp->m_ddev_targp,
2555 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002556 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (error)
2558 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002559 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002561
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002562 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002563 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002564 return 0;
2565}
2566
2567/*
2568 * Read in the allocation group header (free/alloc section).
2569 */
2570int /* error */
2571xfs_alloc_read_agf(
2572 struct xfs_mount *mp, /* mount point structure */
2573 struct xfs_trans *tp, /* transaction pointer */
2574 xfs_agnumber_t agno, /* allocation group number */
2575 int flags, /* XFS_ALLOC_FLAG_... */
2576 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2577{
2578 struct xfs_agf *agf; /* ag freelist header */
2579 struct xfs_perag *pag; /* per allocation group data */
2580 int error;
2581
Dave Chinnerd1230312013-11-01 15:27:19 +11002582 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002583
Dave Chinnerd1230312013-11-01 15:27:19 +11002584 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002585 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002586 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002587 bpp);
2588 if (error)
2589 return error;
2590 if (!*bpp)
2591 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002592 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002593
2594 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002595 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002597 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002598 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002599 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2600 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002602 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002604 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002605 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2606 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002607 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002608 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002609 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002610 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 pag->pagf_init = 1;
2612 }
2613#ifdef DEBUG
2614 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002615 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002616 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002617 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2618 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002620 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002622 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002625 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return 0;
2627}
2628
2629/*
2630 * Allocate an extent (variable-size).
2631 * Depending on the allocation type, we either look in a single allocation
2632 * group or loop over the allocation groups to find the result.
2633 */
2634int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002635xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 xfs_alloc_arg_t *args) /* allocation argument structure */
2637{
2638 xfs_agblock_t agsize; /* allocation group size */
2639 int error;
2640 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 xfs_extlen_t minleft;/* minimum left value, temp copy */
2642 xfs_mount_t *mp; /* mount structure pointer */
2643 xfs_agnumber_t sagno; /* starting allocation group number */
2644 xfs_alloctype_t type; /* input allocation type */
2645 int bump_rotor = 0;
2646 int no_min = 0;
2647 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2648
2649 mp = args->mp;
2650 type = args->otype = args->type;
2651 args->agbno = NULLAGBLOCK;
2652 /*
2653 * Just fix this up, for the case where the last a.g. is shorter
2654 * (or there's only one a.g.) and the caller couldn't easily figure
2655 * that out (xfs_bmap_alloc).
2656 */
2657 agsize = mp->m_sb.sb_agblocks;
2658 if (args->maxlen > agsize)
2659 args->maxlen = agsize;
2660 if (args->alignment == 0)
2661 args->alignment = 1;
2662 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2663 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2664 ASSERT(args->minlen <= args->maxlen);
2665 ASSERT(args->minlen <= agsize);
2666 ASSERT(args->mod < args->prod);
2667 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2668 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2669 args->minlen > args->maxlen || args->minlen > agsize ||
2670 args->mod >= args->prod) {
2671 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002672 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return 0;
2674 }
2675 minleft = args->minleft;
2676
2677 switch (type) {
2678 case XFS_ALLOCTYPE_THIS_AG:
2679 case XFS_ALLOCTYPE_NEAR_BNO:
2680 case XFS_ALLOCTYPE_THIS_BNO:
2681 /*
2682 * These three force us into a single a.g.
2683 */
2684 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002685 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 args->minleft = 0;
2687 error = xfs_alloc_fix_freelist(args, 0);
2688 args->minleft = minleft;
2689 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002690 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 goto error0;
2692 }
2693 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002694 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 break;
2696 }
2697 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2698 if ((error = xfs_alloc_ag_vextent(args)))
2699 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 break;
2701 case XFS_ALLOCTYPE_START_BNO:
2702 /*
2703 * Try near allocation first, then anywhere-in-ag after
2704 * the first a.g. fails.
2705 */
Dave Chinner292378e2016-09-26 08:21:28 +10002706 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2708 args->fsbno = XFS_AGB_TO_FSB(mp,
2709 ((mp->m_agfrotor / rotorstep) %
2710 mp->m_sb.sb_agcount), 0);
2711 bump_rotor = 1;
2712 }
2713 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2714 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2715 /* FALLTHROUGH */
2716 case XFS_ALLOCTYPE_ANY_AG:
2717 case XFS_ALLOCTYPE_START_AG:
2718 case XFS_ALLOCTYPE_FIRST_AG:
2719 /*
2720 * Rotate through the allocation groups looking for a winner.
2721 */
2722 if (type == XFS_ALLOCTYPE_ANY_AG) {
2723 /*
2724 * Start with the last place we left off.
2725 */
2726 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2727 mp->m_sb.sb_agcount;
2728 args->type = XFS_ALLOCTYPE_THIS_AG;
2729 flags = XFS_ALLOC_FLAG_TRYLOCK;
2730 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2731 /*
2732 * Start with allocation group given by bno.
2733 */
2734 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2735 args->type = XFS_ALLOCTYPE_THIS_AG;
2736 sagno = 0;
2737 flags = 0;
2738 } else {
2739 if (type == XFS_ALLOCTYPE_START_AG)
2740 args->type = XFS_ALLOCTYPE_THIS_AG;
2741 /*
2742 * Start with the given allocation group.
2743 */
2744 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2745 flags = XFS_ALLOC_FLAG_TRYLOCK;
2746 }
2747 /*
2748 * Loop over allocation groups twice; first time with
2749 * trylock set, second time without.
2750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002752 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (no_min) args->minleft = 0;
2754 error = xfs_alloc_fix_freelist(args, flags);
2755 args->minleft = minleft;
2756 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002757 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 goto error0;
2759 }
2760 /*
2761 * If we get a buffer back then the allocation will fly.
2762 */
2763 if (args->agbp) {
2764 if ((error = xfs_alloc_ag_vextent(args)))
2765 goto error0;
2766 break;
2767 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002768
2769 trace_xfs_alloc_vextent_loopfailed(args);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
2772 * Didn't work, figure out the next iteration.
2773 */
2774 if (args->agno == sagno &&
2775 type == XFS_ALLOCTYPE_START_BNO)
2776 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002777 /*
2778 * For the first allocation, we can try any AG to get
2779 * space. However, if we already have allocated a
2780 * block, we don't want to try AGs whose number is below
2781 * sagno. Otherwise, we may end up with out-of-order
2782 * locking of AGF, which might cause deadlock.
2783 */
2784 if (++(args->agno) == mp->m_sb.sb_agcount) {
2785 if (args->firstblock != NULLFSBLOCK)
2786 args->agno = sagno;
2787 else
2788 args->agno = 0;
2789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 /*
2791 * Reached the starting a.g., must either be done
2792 * or switch to non-trylock mode.
2793 */
2794 if (args->agno == sagno) {
2795 if (no_min == 1) {
2796 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002797 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 break;
2799 }
2800 if (flags == 0) {
2801 no_min = 1;
2802 } else {
2803 flags = 0;
2804 if (type == XFS_ALLOCTYPE_START_BNO) {
2805 args->agbno = XFS_FSB_TO_AGBNO(mp,
2806 args->fsbno);
2807 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2808 }
2809 }
2810 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002811 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2814 if (args->agno == sagno)
2815 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2816 (mp->m_sb.sb_agcount * rotorstep);
2817 else
2818 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2819 (mp->m_sb.sb_agcount * rotorstep);
2820 }
2821 break;
2822 default:
2823 ASSERT(0);
2824 /* NOTREACHED */
2825 }
2826 if (args->agbno == NULLAGBLOCK)
2827 args->fsbno = NULLFSBLOCK;
2828 else {
2829 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2830#ifdef DEBUG
2831 ASSERT(args->len >= args->minlen);
2832 ASSERT(args->len <= args->maxlen);
2833 ASSERT(args->agbno % args->alignment == 0);
2834 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2835 args->len);
2836#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002837
2838 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002839 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002840 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2841 if (error)
2842 goto error0;
2843 }
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002846 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return 0;
2848error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002849 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return error;
2851}
2852
Dave Chinner4d89e202016-06-21 11:53:28 +10002853/* Ensure that the freelist is at full capacity. */
2854int
2855xfs_free_extent_fix_freelist(
2856 struct xfs_trans *tp,
2857 xfs_agnumber_t agno,
2858 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Dave Chinner4d89e202016-06-21 11:53:28 +10002860 struct xfs_alloc_arg args;
2861 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Dave Chinner4d89e202016-06-21 11:53:28 +10002863 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 args.tp = tp;
2865 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002866 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002867
2868 /*
2869 * validate that the block number is legal - the enables us to detect
2870 * and handle a silent filesystem corruption rather than crashing.
2871 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002872 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002873 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002874
Dave Chinnera862e0f2010-01-11 11:47:41 +00002875 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002876 ASSERT(args.pag);
2877
2878 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2879 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002880 goto out;
2881
2882 *agbp = args.agbp;
2883out:
2884 xfs_perag_put(args.pag);
2885 return error;
2886}
2887
2888/*
2889 * Free an extent.
2890 * Just break up the extent address and hand off to xfs_free_ag_extent
2891 * after fixing up the freelist.
2892 */
2893int /* error */
2894xfs_free_extent(
2895 struct xfs_trans *tp, /* transaction pointer */
2896 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002897 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002898 struct xfs_owner_info *oinfo, /* extent owner */
2899 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002900{
2901 struct xfs_mount *mp = tp->t_mountp;
2902 struct xfs_buf *agbp;
2903 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2904 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2905 int error;
2906
2907 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002908 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002909
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002910 if (XFS_TEST_ERROR(false, mp,
2911 XFS_ERRTAG_FREE_EXTENT,
2912 XFS_RANDOM_FREE_EXTENT))
2913 return -EIO;
2914
Dave Chinner4d89e202016-06-21 11:53:28 +10002915 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2916 if (error)
2917 return error;
2918
2919 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002920
2921 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002922 XFS_WANT_CORRUPTED_GOTO(mp,
2923 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2924 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002925
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002926 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002927 if (error)
2928 goto err;
2929
2930 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2931 return 0;
2932
2933err:
2934 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return error;
2936}