blob: 275d3459e9e8cdf17efdcb4e5b72825209cffc55 [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. Wong80180262016-08-03 11:31:47 +100055xfs_extlen_t
56xfs_prealloc_blocks(
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Darrick J. Wong52548852016-08-03 11:38:24 +100067 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
68 * AGF buffer (PV 947395), we place constraints on the relationship among
69 * actual allocations for data blocks, freelist blocks, and potential file data
70 * bmap btree blocks. However, these restrictions may result in no actual space
71 * allocated for a delayed extent, for example, a data block in a certain AG is
72 * allocated but there is no additional block for the additional bmap btree
73 * block due to a split of the bmap btree of the file. The result of this may
74 * lead to an infinite loop when the file gets flushed to disk and all delayed
75 * extents need to be actually allocated. To get around this, we explicitly set
76 * aside a few blocks which will not be reserved in delayed allocation.
77 *
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100078 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
79 * potential split of the file's bmap btree.
Darrick J. Wong52548852016-08-03 11:38:24 +100080 */
81unsigned int
82xfs_alloc_set_aside(
83 struct xfs_mount *mp)
84{
85 unsigned int blocks;
86
87 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
Darrick J. Wong52548852016-08-03 11:38:24 +100088 return blocks;
89}
90
91/*
92 * When deciding how much space to allocate out of an AG, we limit the
93 * allocation maximum size to the size the AG. However, we cannot use all the
94 * blocks in the AG - some are permanently used by metadata. These
95 * blocks are generally:
96 * - the AG superblock, AGF, AGI and AGFL
97 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
98 * the AGI free inode and rmap btree root blocks.
99 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
100 * - the rmapbt root block
101 *
102 * The AG headers are sector sized, so the amount of space they take up is
103 * dependent on filesystem geometry. The others are all single blocks.
104 */
105unsigned int
106xfs_alloc_ag_max_usable(
107 struct xfs_mount *mp)
108{
109 unsigned int blocks;
110
111 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
112 blocks += XFS_ALLOC_AGFL_RESERVE;
113 blocks += 3; /* AGF, AGI btree root blocks */
114 if (xfs_sb_version_hasfinobt(&mp->m_sb))
115 blocks++; /* finobt root block */
116 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
117 blocks++; /* rmap root block */
118
119 return mp->m_sb.sb_agblocks - blocks;
120}
121
122/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100123 * Lookup the record equal to [bno, len] in the btree given by cur.
124 */
125STATIC int /* error */
126xfs_alloc_lookup_eq(
127 struct xfs_btree_cur *cur, /* btree cursor */
128 xfs_agblock_t bno, /* starting block of extent */
129 xfs_extlen_t len, /* length of extent */
130 int *stat) /* success/failure */
131{
132 cur->bc_rec.a.ar_startblock = bno;
133 cur->bc_rec.a.ar_blockcount = len;
134 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
135}
136
137/*
138 * Lookup the first record greater than or equal to [bno, len]
139 * in the btree given by cur.
140 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000141int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100142xfs_alloc_lookup_ge(
143 struct xfs_btree_cur *cur, /* btree cursor */
144 xfs_agblock_t bno, /* starting block of extent */
145 xfs_extlen_t len, /* length of extent */
146 int *stat) /* success/failure */
147{
148 cur->bc_rec.a.ar_startblock = bno;
149 cur->bc_rec.a.ar_blockcount = len;
150 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
151}
152
153/*
154 * Lookup the first record less than or equal to [bno, len]
155 * in the btree given by cur.
156 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000157static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100158xfs_alloc_lookup_le(
159 struct xfs_btree_cur *cur, /* btree cursor */
160 xfs_agblock_t bno, /* starting block of extent */
161 xfs_extlen_t len, /* length of extent */
162 int *stat) /* success/failure */
163{
164 cur->bc_rec.a.ar_startblock = bno;
165 cur->bc_rec.a.ar_blockcount = len;
166 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
167}
168
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100169/*
170 * Update the record referred to by cur to the value given
171 * by [bno, len].
172 * This either works (return 0) or gets an EFSCORRUPTED error.
173 */
174STATIC int /* error */
175xfs_alloc_update(
176 struct xfs_btree_cur *cur, /* btree cursor */
177 xfs_agblock_t bno, /* starting block of extent */
178 xfs_extlen_t len) /* length of extent */
179{
180 union xfs_btree_rec rec;
181
182 rec.alloc.ar_startblock = cpu_to_be32(bno);
183 rec.alloc.ar_blockcount = cpu_to_be32(len);
184 return xfs_btree_update(cur, &rec);
185}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100186
187/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100188 * Get the data from the pointed-to record.
189 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000190int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100191xfs_alloc_get_rec(
192 struct xfs_btree_cur *cur, /* btree cursor */
193 xfs_agblock_t *bno, /* output: starting block of extent */
194 xfs_extlen_t *len, /* output: length of extent */
195 int *stat) /* output: success/failure */
196{
197 union xfs_btree_rec *rec;
198 int error;
199
200 error = xfs_btree_get_rec(cur, &rec, stat);
201 if (!error && *stat == 1) {
202 *bno = be32_to_cpu(rec->alloc.ar_startblock);
203 *len = be32_to_cpu(rec->alloc.ar_blockcount);
204 }
205 return error;
206}
207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Compute aligned version of the found extent.
210 * Takes alignment and min length into account.
211 */
David Chinner12375c82008-04-10 12:21:32 +1000212STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000214 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 xfs_agblock_t foundbno, /* starting block in found extent */
216 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 xfs_agblock_t *resbno, /* result block number */
218 xfs_extlen_t *reslen) /* result length */
219{
220 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000222 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Christoph Hellwige26f0502011-04-24 19:06:15 +0000224 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000225 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000226
Brian Fosterbfe46d42015-05-29 08:53:00 +1000227 /*
228 * If we have a largish extent that happens to start before min_agbno,
229 * see if we can shift it into range...
230 */
231 if (bno < args->min_agbno && bno + len > args->min_agbno) {
232 diff = args->min_agbno - bno;
233 if (len > diff) {
234 bno += diff;
235 len -= diff;
236 }
237 }
238
Christoph Hellwige26f0502011-04-24 19:06:15 +0000239 if (args->alignment > 1 && len >= args->minlen) {
240 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000241
242 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000243
244 *resbno = aligned_bno;
245 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000247 *resbno = bno;
248 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * Compute best start block and diff for "near" allocations.
254 * freelen >= wantlen already checked by caller.
255 */
256STATIC xfs_extlen_t /* difference value (absolute) */
257xfs_alloc_compute_diff(
258 xfs_agblock_t wantbno, /* target starting block */
259 xfs_extlen_t wantlen, /* target length */
260 xfs_extlen_t alignment, /* target alignment */
Dave Chinner292378e2016-09-26 08:21:28 +1000261 int datatype, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 xfs_agblock_t freebno, /* freespace's starting block */
263 xfs_extlen_t freelen, /* freespace's length */
264 xfs_agblock_t *newbnop) /* result: best start block from free */
265{
266 xfs_agblock_t freeend; /* end of freespace extent */
267 xfs_agblock_t newbno1; /* return block number */
268 xfs_agblock_t newbno2; /* other new block number */
269 xfs_extlen_t newlen1=0; /* length with newbno1 */
270 xfs_extlen_t newlen2=0; /* length with newbno2 */
271 xfs_agblock_t wantend; /* end of target extent */
Dave Chinner292378e2016-09-26 08:21:28 +1000272 bool userdata = xfs_alloc_is_userdata(datatype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 ASSERT(freelen >= wantlen);
275 freeend = freebno + freelen;
276 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200277 /*
278 * We want to allocate from the start of a free extent if it is past
279 * the desired block or if we are allocating user data and the free
280 * extent is before desired block. The second case is there to allow
281 * for contiguous allocation from the remaining free space if the file
282 * grows in the short term.
283 */
284 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
286 newbno1 = NULLAGBLOCK;
287 } else if (freeend >= wantend && alignment > 1) {
288 newbno1 = roundup(wantbno, alignment);
289 newbno2 = newbno1 - alignment;
290 if (newbno1 >= freeend)
291 newbno1 = NULLAGBLOCK;
292 else
293 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
294 if (newbno2 < freebno)
295 newbno2 = NULLAGBLOCK;
296 else
297 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
298 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
299 if (newlen1 < newlen2 ||
300 (newlen1 == newlen2 &&
301 XFS_ABSDIFF(newbno1, wantbno) >
302 XFS_ABSDIFF(newbno2, wantbno)))
303 newbno1 = newbno2;
304 } else if (newbno2 != NULLAGBLOCK)
305 newbno1 = newbno2;
306 } else if (freeend >= wantend) {
307 newbno1 = wantbno;
308 } else if (alignment > 1) {
309 newbno1 = roundup(freeend - wantlen, alignment);
310 if (newbno1 > freeend - wantlen &&
311 newbno1 - alignment >= freebno)
312 newbno1 -= alignment;
313 else if (newbno1 >= freeend)
314 newbno1 = NULLAGBLOCK;
315 } else
316 newbno1 = freeend - wantlen;
317 *newbnop = newbno1;
318 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
319}
320
321/*
322 * Fix up the length, based on mod and prod.
323 * len should be k * prod + mod for some k.
324 * If len is too small it is returned unchanged.
325 * If len hits maxlen it is left alone.
326 */
327STATIC void
328xfs_alloc_fix_len(
329 xfs_alloc_arg_t *args) /* allocation argument structure */
330{
331 xfs_extlen_t k;
332 xfs_extlen_t rlen;
333
334 ASSERT(args->mod < args->prod);
335 rlen = args->len;
336 ASSERT(rlen >= args->minlen);
337 ASSERT(rlen <= args->maxlen);
338 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
339 (args->mod == 0 && rlen < args->prod))
340 return;
341 k = rlen % args->prod;
342 if (k == args->mod)
343 return;
Jan Kara30265112014-06-06 16:06:37 +1000344 if (k > args->mod)
345 rlen = rlen - (k - args->mod);
346 else
347 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100348 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000349 if ((int)rlen < (int)args->minlen)
350 return;
351 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
352 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 args->len = rlen;
354}
355
356/*
357 * Fix up length if there is too little space left in the a.g.
358 * Return 1 if ok, 0 if too little, should give up.
359 */
360STATIC int
361xfs_alloc_fix_minleft(
362 xfs_alloc_arg_t *args) /* allocation argument structure */
363{
364 xfs_agf_t *agf; /* a.g. freelist header */
365 int diff; /* free space difference */
366
367 if (args->minleft == 0)
368 return 1;
369 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100370 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 - args->len - args->minleft;
372 if (diff >= 0)
373 return 1;
374 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100375 /* casts to (int) catch length underflows */
376 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 1;
378 args->agbno = NULLAGBLOCK;
379 return 0;
380}
381
382/*
383 * Update the two btrees, logically removing from freespace the extent
384 * starting at rbno, rlen blocks. The extent is contained within the
385 * actual (current) free extent fbno for flen blocks.
386 * Flags are passed in indicating whether the cursors are set to the
387 * relevant records.
388 */
389STATIC int /* error code */
390xfs_alloc_fixup_trees(
391 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
392 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
393 xfs_agblock_t fbno, /* starting block of free extent */
394 xfs_extlen_t flen, /* length of free extent */
395 xfs_agblock_t rbno, /* starting block of returned extent */
396 xfs_extlen_t rlen, /* length of returned extent */
397 int flags) /* flags, XFSA_FIXUP_... */
398{
399 int error; /* error code */
400 int i; /* operation results */
401 xfs_agblock_t nfbno1; /* first new free startblock */
402 xfs_agblock_t nfbno2; /* second new free startblock */
403 xfs_extlen_t nflen1=0; /* first new free length */
404 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100405 struct xfs_mount *mp;
406
407 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * Look up the record in the by-size tree if necessary.
411 */
412 if (flags & XFSA_FIXUP_CNT_OK) {
413#ifdef DEBUG
414 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
415 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100416 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 i == 1 && nfbno1 == fbno && nflen1 == flen);
418#endif
419 } else {
420 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
421 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100422 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 /*
425 * Look up the record in the by-block tree if necessary.
426 */
427 if (flags & XFSA_FIXUP_BNO_OK) {
428#ifdef DEBUG
429 if ((error = xfs_alloc_get_rec(bno_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(bno_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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100440#ifdef DEBUG
441 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
442 struct xfs_btree_block *bnoblock;
443 struct xfs_btree_block *cntblock;
444
445 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
446 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
447
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100448 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100449 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * Deal with all four cases: the allocated record is contained
455 * within the freespace record, so we can have new freespace
456 * at either (or both) end, or no freespace remaining.
457 */
458 if (rbno == fbno && rlen == flen)
459 nfbno1 = nfbno2 = NULLAGBLOCK;
460 else if (rbno == fbno) {
461 nfbno1 = rbno + rlen;
462 nflen1 = flen - rlen;
463 nfbno2 = NULLAGBLOCK;
464 } else if (rbno + rlen == fbno + flen) {
465 nfbno1 = fbno;
466 nflen1 = flen - rlen;
467 nfbno2 = NULLAGBLOCK;
468 } else {
469 nfbno1 = fbno;
470 nflen1 = rbno - fbno;
471 nfbno2 = rbno + rlen;
472 nflen2 = (fbno + flen) - nfbno2;
473 }
474 /*
475 * Delete the entry from the by-size btree.
476 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100477 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100479 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * Add new by-size btree entry(s).
482 */
483 if (nfbno1 != NULLAGBLOCK) {
484 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
485 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100486 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100487 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100489 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 if (nfbno2 != NULLAGBLOCK) {
492 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
493 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100494 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100495 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100497 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 /*
500 * Fix up the by-block btree entry(s).
501 */
502 if (nfbno1 == NULLAGBLOCK) {
503 /*
504 * No remaining freespace, just delete the by-block tree entry.
505 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100506 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100508 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else {
510 /*
511 * Update the by-block entry to start later|be shorter.
512 */
513 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
514 return error;
515 }
516 if (nfbno2 != NULLAGBLOCK) {
517 /*
518 * 2 resulting free entries, need to add one.
519 */
520 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
521 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100522 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100523 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100525 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 return 0;
528}
529
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100530static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100531xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100532 struct xfs_buf *bp)
533{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100534 struct xfs_mount *mp = bp->b_target->bt_mount;
535 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100536 int i;
537
Eric Sandeence748ea2015-07-29 11:53:31 +1000538 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100539 return false;
540 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
541 return false;
542 /*
543 * during growfs operations, the perag is not fully initialised,
544 * so we can't use it for any useful checking. growfs ensures we can't
545 * use it by using uncached buffers that don't have the perag attached
546 * so we can detect and avoid this problem.
547 */
548 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
549 return false;
550
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100551 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100552 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100553 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100554 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100555 }
Brian Fostera45086e2015-10-12 15:59:25 +1100556
557 return xfs_log_check_lsn(mp,
558 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100559}
560
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100561static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100562xfs_agfl_read_verify(
563 struct xfs_buf *bp)
564{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100565 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100566
567 /*
568 * There is no verification of non-crc AGFLs because mkfs does not
569 * initialise the AGFL to zero or NULL. Hence the only valid part of the
570 * AGFL is what the AGF says is active. We can't get to the AGF, so we
571 * can't verify just those entries are valid.
572 */
573 if (!xfs_sb_version_hascrc(&mp->m_sb))
574 return;
575
Eric Sandeence5028c2014-02-27 15:23:10 +1100576 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000577 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100578 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000579 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100580
581 if (bp->b_error)
582 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100583}
584
585static void
586xfs_agfl_write_verify(
587 struct xfs_buf *bp)
588{
589 struct xfs_mount *mp = bp->b_target->bt_mount;
590 struct xfs_buf_log_item *bip = bp->b_fspriv;
591
592 /* no verification of non-crc AGFLs */
593 if (!xfs_sb_version_hascrc(&mp->m_sb))
594 return;
595
596 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000597 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100598 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100599 return;
600 }
601
602 if (bip)
603 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
604
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100605 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100606}
607
Dave Chinner1813dd62012-11-14 17:54:40 +1100608const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100609 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100610 .verify_read = xfs_agfl_read_verify,
611 .verify_write = xfs_agfl_write_verify,
612};
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/*
615 * Read in the allocation group free block array.
616 */
617STATIC int /* error */
618xfs_alloc_read_agfl(
619 xfs_mount_t *mp, /* mount point structure */
620 xfs_trans_t *tp, /* transaction pointer */
621 xfs_agnumber_t agno, /* allocation group number */
622 xfs_buf_t **bpp) /* buffer for the ag free block array */
623{
624 xfs_buf_t *bp; /* return value */
625 int error;
626
627 ASSERT(agno != NULLAGNUMBER);
628 error = xfs_trans_read_buf(
629 mp, tp, mp->m_ddev_targp,
630 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100631 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (error)
633 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000634 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *bpp = bp;
636 return 0;
637}
638
Christoph Hellwigecb69282011-03-04 12:59:55 +0000639STATIC int
640xfs_alloc_update_counters(
641 struct xfs_trans *tp,
642 struct xfs_perag *pag,
643 struct xfs_buf *agbp,
644 long len)
645{
646 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
647
648 pag->pagf_freeblks += len;
649 be32_add_cpu(&agf->agf_freeblks, len);
650
651 xfs_trans_agblocks_delta(tp, len);
652 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
653 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000654 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000655
656 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
657 return 0;
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * Allocation group level functions.
662 */
663
664/*
665 * Allocate a variable extent in the allocation group agno.
666 * Type and bno are used to determine where in the allocation group the
667 * extent will start.
668 * Extent's length (returned in *len) will be between minlen and maxlen,
669 * and of the form k * prod + mod unless there's nothing that large.
670 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
671 */
672STATIC int /* error */
673xfs_alloc_ag_vextent(
674 xfs_alloc_arg_t *args) /* argument structure for allocation */
675{
676 int error=0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000677 xfs_extlen_t reservation;
678 xfs_extlen_t oldmax;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ASSERT(args->minlen > 0);
681 ASSERT(args->maxlen > 0);
682 ASSERT(args->minlen <= args->maxlen);
683 ASSERT(args->mod < args->prod);
684 ASSERT(args->alignment > 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000685
686 /*
687 * Clamp maxlen to the amount of free space minus any reservations
688 * that have been made.
689 */
690 oldmax = args->maxlen;
691 reservation = xfs_ag_resv_needed(args->pag, args->resv);
692 if (args->maxlen > args->pag->pagf_freeblks - reservation)
693 args->maxlen = args->pag->pagf_freeblks - reservation;
694 if (args->maxlen == 0) {
695 args->agbno = NULLAGBLOCK;
696 args->maxlen = oldmax;
697 return 0;
698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * Branch to correct routine based on the type.
702 */
703 args->wasfromfl = 0;
704 switch (args->type) {
705 case XFS_ALLOCTYPE_THIS_AG:
706 error = xfs_alloc_ag_vextent_size(args);
707 break;
708 case XFS_ALLOCTYPE_NEAR_BNO:
709 error = xfs_alloc_ag_vextent_near(args);
710 break;
711 case XFS_ALLOCTYPE_THIS_BNO:
712 error = xfs_alloc_ag_vextent_exact(args);
713 break;
714 default:
715 ASSERT(0);
716 /* NOTREACHED */
717 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000718
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000719 args->maxlen = oldmax;
720
Christoph Hellwigecb69282011-03-04 12:59:55 +0000721 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Christoph Hellwigecb69282011-03-04 12:59:55 +0000724 ASSERT(args->len >= args->minlen);
725 ASSERT(args->len <= args->maxlen);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000726 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000727 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Darrick J. Wong673930c2016-08-03 11:33:43 +1000729 /* if not file data, insert new block into the reverse map btree */
730 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
731 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
732 args->agbno, args->len, &args->oinfo);
733 if (error)
734 return error;
735 }
736
Christoph Hellwigecb69282011-03-04 12:59:55 +0000737 if (!args->wasfromfl) {
738 error = xfs_alloc_update_counters(args->tp, args->pag,
739 args->agbp,
740 -((long)(args->len)));
741 if (error)
742 return error;
743
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000744 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000745 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000747
Darrick J. Wong3fd129b2016-09-19 10:30:52 +1000748 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000749
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100750 XFS_STATS_INC(args->mp, xs_allocx);
751 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000752 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
756 * Allocate a variable extent at exactly agno/bno.
757 * Extent's length (returned in *len) will be between minlen and maxlen,
758 * and of the form k * prod + mod unless there's nothing that large.
759 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
760 */
761STATIC int /* error */
762xfs_alloc_ag_vextent_exact(
763 xfs_alloc_arg_t *args) /* allocation argument structure */
764{
765 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
766 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int error;
768 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000770 xfs_agblock_t tbno; /* start block of trimmed extent */
771 xfs_extlen_t tlen; /* length of trimmed extent */
772 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * Allocate/initialize a cursor for the by-number freespace btree.
779 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100780 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000781 args->agno, XFS_BTNUM_BNO);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
785 * Look for the closest free block <= bno, it must contain bno
786 * if any free block does.
787 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000788 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000791 if (!i)
792 goto not_found;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
795 * Grab the freespace record.
796 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000797 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100800 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000804 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000806 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000807
808 /*
809 * Give up if the start of the extent is busy, or the freespace isn't
810 * long enough for the minimum request.
811 */
812 if (tbno > args->agbno)
813 goto not_found;
814 if (tlen < args->minlen)
815 goto not_found;
816 tend = tbno + tlen;
817 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000818 goto not_found;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * End of extent will be smaller of the freespace end and the
822 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000823 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * Fix the length according to mod and prod if given.
825 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000826 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
827 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000829 if (!xfs_alloc_fix_minleft(args))
830 goto not_found;
831
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000832 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000835 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * Allocate/initialize a cursor for the by-size btree.
837 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100838 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
839 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100841 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000842 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
843 args->len, XFSA_FIXUP_BNO_OK);
844 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
846 goto error0;
847 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
850 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000853 trace_xfs_alloc_exact_done(args);
854 return 0;
855
856not_found:
857 /* Didn't find it, return null. */
858 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
859 args->agbno = NULLAGBLOCK;
860 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
862
863error0:
864 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000865 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return error;
867}
868
869/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000870 * Search the btree in a given direction via the search cursor and compare
871 * the records found against the good extent we've already found.
872 */
873STATIC int
874xfs_alloc_find_best_extent(
875 struct xfs_alloc_arg *args, /* allocation argument structure */
876 struct xfs_btree_cur **gcur, /* good cursor */
877 struct xfs_btree_cur **scur, /* searching cursor */
878 xfs_agblock_t gdiff, /* difference for search comparison */
879 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000880 xfs_extlen_t *slen, /* extent length */
881 xfs_agblock_t *sbnoa, /* aligned extent found by search */
882 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000883 int dir) /* 0 = search right, 1 = search left */
884{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000885 xfs_agblock_t new;
886 xfs_agblock_t sdiff;
887 int error;
888 int i;
889
890 /* The good extent is perfect, no need to search. */
891 if (!gdiff)
892 goto out_use_good;
893
894 /*
895 * Look until we find a better one, run out of space or run off the end.
896 */
897 do {
898 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
899 if (error)
900 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100901 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000902 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000903
904 /*
905 * The good extent is closer than this one.
906 */
907 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000908 if (*sbnoa > args->max_agbno)
909 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000910 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000911 goto out_use_good;
912 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000913 if (*sbnoa < args->min_agbno)
914 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000915 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000916 goto out_use_good;
917 }
918
919 /*
920 * Same distance, compare length and pick the best.
921 */
922 if (*slena >= args->minlen) {
923 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
924 xfs_alloc_fix_len(args);
925
926 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200927 args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +1000928 args->datatype, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000929 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000930
931 /*
932 * Choose closer size and invalidate other cursor.
933 */
934 if (sdiff < gdiff)
935 goto out_use_search;
936 goto out_use_good;
937 }
938
939 if (!dir)
940 error = xfs_btree_increment(*scur, 0, &i);
941 else
942 error = xfs_btree_decrement(*scur, 0, &i);
943 if (error)
944 goto error0;
945 } while (i);
946
947out_use_good:
948 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
949 *scur = NULL;
950 return 0;
951
952out_use_search:
953 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
954 *gcur = NULL;
955 return 0;
956
957error0:
958 /* caller invalidates cursors */
959 return error;
960}
961
962/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * Allocate a variable extent near bno in the allocation group agno.
964 * Extent's length (returned in len) will be between minlen and maxlen,
965 * and of the form k * prod + mod unless there's nothing that large.
966 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
967 */
968STATIC int /* error */
969xfs_alloc_ag_vextent_near(
970 xfs_alloc_arg_t *args) /* allocation argument structure */
971{
972 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
973 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
974 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_agblock_t gtbno; /* start bno of right side entry */
976 xfs_agblock_t gtbnoa; /* aligned ... */
977 xfs_extlen_t gtdiff; /* difference to right side entry */
978 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000979 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xfs_agblock_t gtnew; /* useful start bno of right side */
981 int error; /* error code */
982 int i; /* result code, temporary */
983 int j; /* result code, temporary */
984 xfs_agblock_t ltbno; /* start bno of left side entry */
985 xfs_agblock_t ltbnoa; /* aligned ... */
986 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000988 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 xfs_agblock_t ltnew; /* useful start bno of left side */
990 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000991 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000992#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /*
994 * Randomly don't execute the first algorithm.
995 */
996 int dofirst; /* set to do first algorithm */
997
Akinobu Mitaecb34032013-03-04 21:58:20 +0900998 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +00001000
Brian Fosterbfe46d42015-05-29 08:53:00 +10001001 /* handle unitialized agbno range so caller doesn't have to */
1002 if (!args->min_agbno && !args->max_agbno)
1003 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1004 ASSERT(args->min_agbno <= args->max_agbno);
1005
1006 /* clamp agbno to the range if it's outside */
1007 if (args->agbno < args->min_agbno)
1008 args->agbno = args->min_agbno;
1009 if (args->agbno > args->max_agbno)
1010 args->agbno = args->max_agbno;
1011
Christoph Hellwige26f0502011-04-24 19:06:15 +00001012restart:
1013 bno_cur_lt = NULL;
1014 bno_cur_gt = NULL;
1015 ltlen = 0;
1016 gtlena = 0;
1017 ltlena = 0;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * Get a cursor for the by-size btree.
1021 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001022 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1023 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * See if there are any free extents as big as maxlen.
1027 */
1028 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1029 goto error0;
1030 /*
1031 * If none, then pick up the last entry in the tree unless the
1032 * tree is empty.
1033 */
1034 if (!i) {
1035 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1036 &ltlen, &i)))
1037 goto error0;
1038 if (i == 0 || ltlen == 0) {
1039 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001040 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042 }
1043 ASSERT(i == 1);
1044 }
1045 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * First algorithm.
1049 * If the requested extent is large wrt the freespaces available
1050 * in this a.g., then the cursor will be pointing to a btree entry
1051 * near the right edge of the tree. If it's in the last btree leaf
1052 * block, then we just examine all the entries in that block
1053 * that are big enough, and pick the best one.
1054 * This is written as a while loop so we can break out of it,
1055 * but we never loop back to the top.
1056 */
1057 while (xfs_btree_islastblock(cnt_cur, 0)) {
1058 xfs_extlen_t bdiff;
1059 int besti=0;
1060 xfs_extlen_t blen=0;
1061 xfs_agblock_t bnew=0;
1062
Dave Chinner63d20d62013-08-12 20:49:50 +10001063#ifdef DEBUG
1064 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066#endif
1067 /*
1068 * Start from the entry that lookup found, sequence through
1069 * all larger free blocks. If we're actually pointing at a
1070 * record smaller than maxlen, go to the start of this block,
1071 * and skip all those smaller than minlen.
1072 */
1073 if (ltlen || args->alignment > 1) {
1074 cnt_cur->bc_ptrs[0] = 1;
1075 do {
1076 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1077 &ltlen, &i)))
1078 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001079 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (ltlen >= args->minlen)
1081 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001082 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto error0;
1084 } while (i);
1085 ASSERT(ltlen >= args->minlen);
1086 if (!i)
1087 break;
1088 }
1089 i = cnt_cur->bc_ptrs[0];
1090 for (j = 1, blen = 0, bdiff = 0;
1091 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001092 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * For each entry, decide if it's better than
1095 * the previous best entry.
1096 */
1097 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1098 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001099 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001100 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1101 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001102 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001104 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1105 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1107 xfs_alloc_fix_len(args);
1108 ASSERT(args->len >= args->minlen);
1109 if (args->len < blen)
1110 continue;
1111 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001112 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001113 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (ltnew != NULLAGBLOCK &&
1115 (args->len > blen || ltdiff < bdiff)) {
1116 bdiff = ltdiff;
1117 bnew = ltnew;
1118 blen = args->len;
1119 besti = cnt_cur->bc_ptrs[0];
1120 }
1121 }
1122 /*
1123 * It didn't work. We COULD be in a case where
1124 * there's a good record somewhere, so try again.
1125 */
1126 if (blen == 0)
1127 break;
1128 /*
1129 * Point at the best entry, and retrieve it again.
1130 */
1131 cnt_cur->bc_ptrs[0] = besti;
1132 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1133 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001134 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001135 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 args->len = blen;
1137 if (!xfs_alloc_fix_minleft(args)) {
1138 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001139 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
1141 }
1142 blen = args->len;
1143 /*
1144 * We are allocating starting at bnew for blen blocks.
1145 */
1146 args->agbno = bnew;
1147 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001148 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Set up a cursor for the by-bno tree.
1151 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001152 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1153 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * Fix up the btree entries.
1156 */
1157 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1158 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1159 goto error0;
1160 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1161 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001162
1163 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return 0;
1165 }
1166 /*
1167 * Second algorithm.
1168 * Search in the by-bno tree to the left and to the right
1169 * simultaneously, until in each case we find a space big enough,
1170 * or run into the edge of the tree. When we run into the edge,
1171 * we deallocate that cursor.
1172 * If both searches succeed, we compare the two spaces and pick
1173 * the better one.
1174 * With alignment, it's possible for both to fail; the upper
1175 * level algorithm that picks allocation groups for allocations
1176 * is not supposed to do this.
1177 */
1178 /*
1179 * Allocate and initialize the cursor for the leftward search.
1180 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001181 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1182 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Lookup <= bno to find the leftward search's starting point.
1185 */
1186 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1187 goto error0;
1188 if (!i) {
1189 /*
1190 * Didn't find anything; use this cursor for the rightward
1191 * search.
1192 */
1193 bno_cur_gt = bno_cur_lt;
1194 bno_cur_lt = NULL;
1195 }
1196 /*
1197 * Found something. Duplicate the cursor for the rightward search.
1198 */
1199 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1200 goto error0;
1201 /*
1202 * Increment the cursor, so we will point at the entry just right
1203 * of the leftward entry if any, or to the leftmost entry.
1204 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001205 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto error0;
1207 if (!i) {
1208 /*
1209 * It failed, there are no rightward entries.
1210 */
1211 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1212 bno_cur_gt = NULL;
1213 }
1214 /*
1215 * Loop going left with the leftward cursor, right with the
1216 * rightward cursor, until either both directions give up or
1217 * we find an entry at least as big as minlen.
1218 */
1219 do {
1220 if (bno_cur_lt) {
1221 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1222 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001223 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001224 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1225 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001226 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001228 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001230 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 xfs_btree_del_cursor(bno_cur_lt,
1232 XFS_BTREE_NOERROR);
1233 bno_cur_lt = NULL;
1234 }
1235 }
1236 if (bno_cur_gt) {
1237 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1238 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001239 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001240 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1241 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001242 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001244 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001246 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 xfs_btree_del_cursor(bno_cur_gt,
1248 XFS_BTREE_NOERROR);
1249 bno_cur_gt = NULL;
1250 }
1251 }
1252 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Got both cursors still active, need to find better entry.
1256 */
1257 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (ltlena >= args->minlen) {
1259 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001260 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
1262 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1263 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001264 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001265 args->alignment, args->datatype, ltbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001266 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001267
1268 error = xfs_alloc_find_best_extent(args,
1269 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001270 ltdiff, &gtbno, &gtlen,
1271 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001272 0 /* search right */);
1273 } else {
1274 ASSERT(gtlena >= args->minlen);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001277 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
1279 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1280 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001281 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Dave Chinner292378e2016-09-26 08:21:28 +10001282 args->alignment, args->datatype, gtbnoa,
Jan Kara211d0222013-04-11 22:09:56 +02001283 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001284
1285 error = xfs_alloc_find_best_extent(args,
1286 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001287 gtdiff, &ltbno, &ltlen,
1288 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001289 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001291
1292 if (error)
1293 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /*
1297 * If we couldn't get anything, give up.
1298 */
1299 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001300 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1301
Christoph Hellwige26f0502011-04-24 19:06:15 +00001302 if (!forced++) {
1303 trace_xfs_alloc_near_busy(args);
1304 xfs_log_force(args->mp, XFS_LOG_SYNC);
1305 goto restart;
1306 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001307 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 args->agbno = NULLAGBLOCK;
1309 return 0;
1310 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * At this point we have selected a freespace entry, either to the
1314 * left or to the right. If it's on the right, copy all the
1315 * useful variables to the "left" set so we only have one
1316 * copy of this code.
1317 */
1318 if (bno_cur_gt) {
1319 bno_cur_lt = bno_cur_gt;
1320 bno_cur_gt = NULL;
1321 ltbno = gtbno;
1322 ltbnoa = gtbnoa;
1323 ltlen = gtlen;
1324 ltlena = gtlena;
1325 j = 1;
1326 } else
1327 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * Fix up the length and compute the useful address.
1331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1333 xfs_alloc_fix_len(args);
1334 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001335 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1337 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1338 return 0;
1339 }
1340 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001341 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Dave Chinner292378e2016-09-26 08:21:28 +10001342 args->datatype, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001344 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001345 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001346 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1350 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1351 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001352
1353 if (j)
1354 trace_xfs_alloc_near_greater(args);
1355 else
1356 trace_xfs_alloc_near_lesser(args);
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1359 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1360 return 0;
1361
1362 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001363 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (cnt_cur != NULL)
1365 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1366 if (bno_cur_lt != NULL)
1367 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1368 if (bno_cur_gt != NULL)
1369 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1370 return error;
1371}
1372
1373/*
1374 * Allocate a variable extent anywhere in the allocation group agno.
1375 * Extent's length (returned in len) will be between minlen and maxlen,
1376 * and of the form k * prod + mod unless there's nothing that large.
1377 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1378 */
1379STATIC int /* error */
1380xfs_alloc_ag_vextent_size(
1381 xfs_alloc_arg_t *args) /* allocation argument structure */
1382{
1383 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1384 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1385 int error; /* error result */
1386 xfs_agblock_t fbno; /* start of found freespace */
1387 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int i; /* temp status variable */
1389 xfs_agblock_t rbno; /* returned block number */
1390 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001391 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Christoph Hellwige26f0502011-04-24 19:06:15 +00001393restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /*
1395 * Allocate and initialize a cursor for the by-size btree.
1396 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001397 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1398 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /*
1402 * Look for an entry >= maxlen+alignment-1 blocks.
1403 */
1404 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1405 args->maxlen + args->alignment - 1, &i)))
1406 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001409 * If none or we have busy extents that we cannot allocate from, then
1410 * we have to settle for a smaller extent. In the case that there are
1411 * no large extents, this will return the last entry in the tree unless
1412 * the tree is empty. In the case that there are only busy large
1413 * extents, this will return the largest small extent unless there
1414 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001416 if (!i || forced > 1) {
1417 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1418 &fbno, &flen, &i);
1419 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 goto error0;
1421 if (i == 0 || flen == 0) {
1422 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001423 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425 }
1426 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001427 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1428 } else {
1429 /*
1430 * Search for a non-busy extent that is large enough.
1431 * If we are at low space, don't check, or if we fall of
1432 * the end of the btree, turn off the busy check and
1433 * restart.
1434 */
1435 for (;;) {
1436 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1437 if (error)
1438 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001439 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001440
1441 xfs_alloc_compute_aligned(args, fbno, flen,
1442 &rbno, &rlen);
1443
1444 if (rlen >= args->maxlen)
1445 break;
1446
1447 error = xfs_btree_increment(cnt_cur, 0, &i);
1448 if (error)
1449 goto error0;
1450 if (i == 0) {
1451 /*
1452 * Our only valid extents must have been busy.
1453 * Make it unbusy by forcing the log out and
1454 * retrying. If we've been here before, forcing
1455 * the log isn't making the extents available,
1456 * which means they have probably been freed in
1457 * this transaction. In that case, we have to
1458 * give up on them and we'll attempt a minlen
1459 * allocation the next time around.
1460 */
1461 xfs_btree_del_cursor(cnt_cur,
1462 XFS_BTREE_NOERROR);
1463 trace_xfs_alloc_size_busy(args);
1464 if (!forced++)
1465 xfs_log_force(args->mp, XFS_LOG_SYNC);
1466 goto restart;
1467 }
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * In the first case above, we got the last entry in the
1473 * by-size btree. Now we check to see if the space hits maxlen
1474 * once aligned; if not, we search left for something better.
1475 * This can't happen in the second case above.
1476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001478 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1480 if (rlen < args->maxlen) {
1481 xfs_agblock_t bestfbno;
1482 xfs_extlen_t bestflen;
1483 xfs_agblock_t bestrbno;
1484 xfs_extlen_t bestrlen;
1485
1486 bestrlen = rlen;
1487 bestrbno = rbno;
1488 bestflen = flen;
1489 bestfbno = fbno;
1490 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001491 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto error0;
1493 if (i == 0)
1494 break;
1495 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1496 &i)))
1497 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001498 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (flen < bestrlen)
1500 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001501 xfs_alloc_compute_aligned(args, fbno, flen,
1502 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001504 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 (rlen <= flen && rbno + rlen <= fbno + flen),
1506 error0);
1507 if (rlen > bestrlen) {
1508 bestrlen = rlen;
1509 bestrbno = rbno;
1510 bestflen = flen;
1511 bestfbno = fbno;
1512 if (rlen == args->maxlen)
1513 break;
1514 }
1515 }
1516 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1517 &i)))
1518 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001519 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 rlen = bestrlen;
1521 rbno = bestrbno;
1522 flen = bestflen;
1523 fbno = bestfbno;
1524 }
1525 args->wasfromfl = 0;
1526 /*
1527 * Fix up the length.
1528 */
1529 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001530 if (rlen < args->minlen) {
1531 if (!forced++) {
1532 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1533 trace_xfs_alloc_size_busy(args);
1534 xfs_log_force(args->mp, XFS_LOG_SYNC);
1535 goto restart;
1536 }
1537 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001539 xfs_alloc_fix_len(args);
1540
1541 if (!xfs_alloc_fix_minleft(args))
1542 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001544 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
1546 * Allocate and initialize a cursor for the by-block tree.
1547 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001548 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1549 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1551 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1552 goto error0;
1553 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1554 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1555 cnt_cur = bno_cur = NULL;
1556 args->len = rlen;
1557 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001558 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001560 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001562 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564
1565error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001566 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (cnt_cur)
1568 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1569 if (bno_cur)
1570 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1571 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001572
1573out_nominleft:
1574 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1575 trace_xfs_alloc_size_nominleft(args);
1576 args->agbno = NULLAGBLOCK;
1577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/*
1581 * Deal with the case where only small freespaces remain.
1582 * Either return the contents of the last freespace record,
1583 * or allocate space from the freelist if there is nothing in the tree.
1584 */
1585STATIC int /* error */
1586xfs_alloc_ag_vextent_small(
1587 xfs_alloc_arg_t *args, /* allocation argument structure */
1588 xfs_btree_cur_t *ccur, /* by-size cursor */
1589 xfs_agblock_t *fbnop, /* result block number */
1590 xfs_extlen_t *flenp, /* result length */
1591 int *stat) /* status: 0-freelist, 1-normal/none */
1592{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001593 struct xfs_owner_info oinfo;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001594 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int error;
1596 xfs_agblock_t fbno;
1597 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int i;
1599
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001600 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto error0;
1602 if (i) {
1603 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1604 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001605 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 /*
1608 * Nothing in the btree, try the freelist. Make sure
1609 * to respect minleft even when pulling from the
1610 * freelist.
1611 */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001612 else if (args->minlen == 1 && args->alignment == 1 &&
1613 args->resv != XFS_AG_RESV_AGFL &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001614 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1615 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001616 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1617 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto error0;
1619 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001620 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Dave Chinner292378e2016-09-26 08:21:28 +10001621 xfs_alloc_allow_busy_reuse(args->datatype));
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001622
Dave Chinner292378e2016-09-26 08:21:28 +10001623 if (xfs_alloc_is_userdata(args->datatype)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 xfs_buf_t *bp;
1625
1626 bp = xfs_btree_get_bufs(args->mp, args->tp,
1627 args->agno, fbno, 0);
1628 xfs_trans_binval(args->tp, bp);
1629 }
1630 args->len = 1;
1631 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001632 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001634 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 error0);
1636 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001637 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001638
1639 /*
1640 * If we're feeding an AGFL block to something that
1641 * doesn't live in the free space, we need to clear
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001642 * out the OWN_AG rmap and add the block back to
1643 * the AGFL per-AG reservation.
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001644 */
1645 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1646 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1647 fbno, 1, &oinfo);
1648 if (error)
1649 goto error0;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001650 pag = xfs_perag_get(args->mp, args->agno);
1651 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1652 args->tp, 1);
1653 xfs_perag_put(pag);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 *stat = 0;
1656 return 0;
1657 }
1658 /*
1659 * Nothing in the freelist.
1660 */
1661 else
1662 flen = 0;
1663 }
1664 /*
1665 * Can't allocate from the freelist for some reason.
1666 */
Nathan Scottd432c802006-09-28 11:03:44 +10001667 else {
1668 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * Can't do the allocation, give up.
1673 */
1674 if (flen < args->minlen) {
1675 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001676 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 flen = 0;
1678 }
1679 *fbnop = fbno;
1680 *flenp = flen;
1681 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001682 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684
1685error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001686 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return error;
1688}
1689
1690/*
1691 * Free the extent starting at agno/bno for length.
1692 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001693STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001695 xfs_trans_t *tp,
1696 xfs_buf_t *agbp,
1697 xfs_agnumber_t agno,
1698 xfs_agblock_t bno,
1699 xfs_extlen_t len,
1700 struct xfs_owner_info *oinfo,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001701 enum xfs_ag_resv_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1704 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1705 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 xfs_agblock_t gtbno; /* start of right neighbor block */
1707 xfs_extlen_t gtlen; /* length of right neighbor block */
1708 int haveleft; /* have a left neighbor block */
1709 int haveright; /* have a right neighbor block */
1710 int i; /* temp, result code */
1711 xfs_agblock_t ltbno; /* start of left neighbor block */
1712 xfs_extlen_t ltlen; /* length of left neighbor block */
1713 xfs_mount_t *mp; /* mount point struct for filesystem */
1714 xfs_agblock_t nbno; /* new starting block of freespace */
1715 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001716 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Darrick J. Wong673930c2016-08-03 11:33:43 +10001718 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001720
1721 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1722 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1723 if (error)
1724 goto error0;
1725 }
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * Allocate and initialize a cursor for the by-block btree.
1729 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001730 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * Look for a neighboring block on the left (lower block numbers)
1733 * that is contiguous with this space.
1734 */
1735 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1736 goto error0;
1737 if (haveleft) {
1738 /*
1739 * There is a block to our left.
1740 */
1741 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1742 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001743 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /*
1745 * It's not contiguous, though.
1746 */
1747 if (ltbno + ltlen < bno)
1748 haveleft = 0;
1749 else {
1750 /*
1751 * If this failure happens the request to free this
1752 * space was invalid, it's (partly) already free.
1753 * Very bad.
1754 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001755 XFS_WANT_CORRUPTED_GOTO(mp,
1756 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 }
1759 /*
1760 * Look for a neighboring block on the right (higher block numbers)
1761 * that is contiguous with this space.
1762 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001763 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto error0;
1765 if (haveright) {
1766 /*
1767 * There is a block to our right.
1768 */
1769 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1770 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
1773 * It's not contiguous, though.
1774 */
1775 if (bno + len < gtbno)
1776 haveright = 0;
1777 else {
1778 /*
1779 * If this failure happens the request to free this
1780 * space was invalid, it's (partly) already free.
1781 * Very bad.
1782 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001783 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 }
1786 /*
1787 * Now allocate and initialize a cursor for the by-size tree.
1788 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001789 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Have both left and right contiguous neighbors.
1792 * Merge all three into a single free block.
1793 */
1794 if (haveleft && haveright) {
1795 /*
1796 * Delete the old by-size entry on the left.
1797 */
1798 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1799 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001800 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001801 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001803 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * Delete the old by-size entry on the right.
1806 */
1807 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1808 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001809 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001810 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /*
1814 * Delete the old by-block entry for the right block.
1815 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001816 if ((error = xfs_btree_delete(bno_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 * Move the by-block cursor back to the left neighbor.
1821 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001822 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825#ifdef DEBUG
1826 /*
1827 * Check that this is the right record: delete didn't
1828 * mangle the cursor.
1829 */
1830 {
1831 xfs_agblock_t xxbno;
1832 xfs_extlen_t xxlen;
1833
1834 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1835 &i)))
1836 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001837 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 i == 1 && xxbno == ltbno && xxlen == ltlen,
1839 error0);
1840 }
1841#endif
1842 /*
1843 * Update remaining by-block entry to the new, joined block.
1844 */
1845 nbno = ltbno;
1846 nlen = len + ltlen + gtlen;
1847 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1848 goto error0;
1849 }
1850 /*
1851 * Have only a left contiguous neighbor.
1852 * Merge it together with the new freespace.
1853 */
1854 else if (haveleft) {
1855 /*
1856 * Delete the old by-size entry on the left.
1857 */
1858 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1859 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001860 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001861 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001863 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * Back up the by-block cursor to the left neighbor, and
1866 * update its length.
1867 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001868 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001870 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 nbno = ltbno;
1872 nlen = len + ltlen;
1873 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1874 goto error0;
1875 }
1876 /*
1877 * Have only a right contiguous neighbor.
1878 * Merge it together with the new freespace.
1879 */
1880 else if (haveright) {
1881 /*
1882 * Delete the old by-size entry on the right.
1883 */
1884 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1885 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001886 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001887 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001889 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * Update the starting block and length of the right
1892 * neighbor in the by-block tree.
1893 */
1894 nbno = bno;
1895 nlen = len + gtlen;
1896 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1897 goto error0;
1898 }
1899 /*
1900 * No contiguous neighbors.
1901 * Insert the new freespace into the by-block tree.
1902 */
1903 else {
1904 nbno = bno;
1905 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001906 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001908 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1911 bno_cur = NULL;
1912 /*
1913 * In all cases we need to insert the new freespace in the by-size tree.
1914 */
1915 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1916 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001917 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001918 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001920 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1922 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
1925 * Update the freespace totals in the ag and superblock.
1926 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001927 pag = xfs_perag_get(mp, agno);
1928 error = xfs_alloc_update_counters(tp, pag, agbp, len);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001929 xfs_ag_resv_free_extent(pag, type, tp, len);
Christoph Hellwigecb69282011-03-04 12:59:55 +00001930 xfs_perag_put(pag);
1931 if (error)
1932 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001934 XFS_STATS_INC(mp, xs_freex);
1935 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001936
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001937 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1938 haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return 0;
1941
1942 error0:
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001943 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1944 -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (bno_cur)
1946 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1947 if (cnt_cur)
1948 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1949 return error;
1950}
1951
1952/*
1953 * Visible (exported) allocation/free functions.
1954 * Some of these are used just by xfs_alloc_btree.c and this file.
1955 */
1956
1957/*
1958 * Compute and fill in value of m_ag_maxlevels.
1959 */
1960void
1961xfs_alloc_compute_maxlevels(
1962 xfs_mount_t *mp) /* file system mount structure */
1963{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001964 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1965 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/*
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001969 * Find the length of the longest extent in an AG. The 'need' parameter
1970 * specifies how much space we're going to need for the AGFL and the
1971 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1972 * other callers.
Dave Chinner6cc87642009-03-16 08:29:46 +01001973 */
1974xfs_extlen_t
1975xfs_alloc_longest_free_extent(
1976 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001977 struct xfs_perag *pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001978 xfs_extlen_t need,
1979 xfs_extlen_t reserved)
Dave Chinner6cc87642009-03-16 08:29:46 +01001980{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001981 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001982
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001983 /*
1984 * If the AGFL needs a recharge, we'll have to subtract that from the
1985 * longest extent.
1986 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001987 if (need > pag->pagf_flcount)
1988 delta = need - pag->pagf_flcount;
1989
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10001990 /*
1991 * If we cannot maintain others' reservations with space from the
1992 * not-longest freesp extents, we'll have to subtract /that/ from
1993 * the longest extent too.
1994 */
1995 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
1996 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
1997
1998 /*
1999 * If the longest extent is long enough to satisfy all the
2000 * reservations and AGFL rules in place, we can return this extent.
2001 */
Dave Chinner6cc87642009-03-16 08:29:46 +01002002 if (pag->pagf_longest > delta)
2003 return pag->pagf_longest - delta;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002004
2005 /* Otherwise, let the caller try for 1 block if there's space. */
Dave Chinner6cc87642009-03-16 08:29:46 +01002006 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2007}
2008
Dave Chinner496817b2015-06-22 10:13:30 +10002009unsigned int
2010xfs_alloc_min_freelist(
2011 struct xfs_mount *mp,
2012 struct xfs_perag *pag)
2013{
2014 unsigned int min_free;
2015
2016 /* space needed by-bno freespace btree */
2017 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2018 mp->m_ag_maxlevels);
2019 /* space needed by-size freespace btree */
2020 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2021 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10002022 /* space needed reverse mapping used space btree */
2023 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2024 min_free += min_t(unsigned int,
2025 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2026 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10002027
2028 return min_free;
2029}
2030
Dave Chinner6cc87642009-03-16 08:29:46 +01002031/*
Dave Chinner72d55282015-06-22 10:04:42 +10002032 * Check if the operation we are fixing up the freelist for should go ahead or
2033 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2034 * is dependent on whether the size and shape of free space available will
2035 * permit the requested allocation to take place.
2036 */
2037static bool
2038xfs_alloc_space_available(
2039 struct xfs_alloc_arg *args,
2040 xfs_extlen_t min_free,
2041 int flags)
2042{
2043 struct xfs_perag *pag = args->pag;
2044 xfs_extlen_t longest;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002045 xfs_extlen_t reservation; /* blocks that are still reserved */
Dave Chinner72d55282015-06-22 10:04:42 +10002046 int available;
2047
2048 if (flags & XFS_ALLOC_FLAG_FREEING)
2049 return true;
2050
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002051 reservation = xfs_ag_resv_needed(pag, args->resv);
2052
Dave Chinner72d55282015-06-22 10:04:42 +10002053 /* do we have enough contiguous free space for the allocation? */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002054 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2055 reservation);
Dave Chinner72d55282015-06-22 10:04:42 +10002056 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2057 return false;
2058
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002059 /* do we have enough free space remaining for the allocation? */
Dave Chinner72d55282015-06-22 10:04:42 +10002060 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002061 reservation - min_free - args->total);
2062 if (available < (int)args->minleft || available <= 0)
Dave Chinner72d55282015-06-22 10:04:42 +10002063 return false;
2064
2065 return true;
2066}
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068/*
2069 * Decide whether to use this allocation group for this allocation.
2070 * If so, fix up the btree freelist's size.
2071 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002072int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002074 struct xfs_alloc_arg *args, /* allocation argument structure */
2075 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Dave Chinner396503f2015-06-22 10:13:19 +10002077 struct xfs_mount *mp = args->mp;
2078 struct xfs_perag *pag = args->pag;
2079 struct xfs_trans *tp = args->tp;
2080 struct xfs_buf *agbp = NULL;
2081 struct xfs_buf *agflbp = NULL;
2082 struct xfs_alloc_arg targs; /* local allocation arguments */
2083 xfs_agblock_t bno; /* freelist block */
2084 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002085 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002088 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2089 if (error)
2090 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002092 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2093 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002094 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Dave Chinner396503f2015-06-22 10:13:19 +10002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002098 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002099 * If this is a metadata preferred pag and we are user data then try
2100 * somewhere else if we are not being asked to try harder at this
2101 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
Dave Chinner292378e2016-09-26 08:21:28 +10002103 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002104 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2105 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002106 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Dave Chinner496817b2015-06-22 10:13:30 +10002109 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002110 if (!xfs_alloc_space_available(args, need, flags))
2111 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /*
2114 * Get the a.g. freespace buffer.
2115 * Can fail if we're not blocking on locks, and it's held.
2116 */
Dave Chinner396503f2015-06-22 10:13:19 +10002117 if (!agbp) {
2118 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2119 if (error)
2120 goto out_no_agbp;
2121 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002122 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2123 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002124 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002127
Dave Chinner50adbcb2015-06-22 10:04:31 +10002128 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002129 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002130 if (!xfs_alloc_space_available(args, need, flags))
2131 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /*
2134 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002135 *
Dave Chinner396503f2015-06-22 10:13:19 +10002136 * Note that from this point onwards, we will always release the agf and
2137 * agfl buffers on error. This handles the case where we error out and
2138 * the buffers are clean or may not have been joined to the transaction
2139 * and hence need to be released manually. If they have been joined to
2140 * the transaction, then xfs_trans_brelse() will handle them
2141 * appropriately based on the recursion count and dirty state of the
2142 * buffer.
2143 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002144 * XXX (dgc): When we have lots of free space, does this buy us
2145 * anything other than extra overhead when we need to put more blocks
2146 * back on the free list? Maybe we should only do this when space is
2147 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002148 *
2149 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2150 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2151 * updating the rmapbt. Both flags are used in xfs_repair while we're
2152 * rebuilding the rmapbt, and neither are used by the kernel. They're
2153 * both required to ensure that rmaps are correctly recorded for the
2154 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2155 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002157 memset(&targs, 0, sizeof(targs));
2158 if (flags & XFS_ALLOC_FLAG_NORMAP)
2159 xfs_rmap_skip_owner_update(&targs.oinfo);
2160 else
2161 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2162 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002163 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
David Chinner92821e22007-05-24 15:26:31 +10002165 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2166 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002167 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002168 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002169 &targs.oinfo, XFS_AG_RESV_AGFL);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002170 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002171 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2173 xfs_trans_binval(tp, bp);
2174 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 targs.tp = tp;
2177 targs.mp = mp;
2178 targs.agbp = agbp;
2179 targs.agno = args->agno;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002180 targs.alignment = targs.minlen = targs.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 targs.type = XFS_ALLOCTYPE_THIS_AG;
2182 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002183 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2184 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002185 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002186
2187 /* Make the freelist longer if it's too short. */
2188 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002190 targs.maxlen = need - pag->pagf_flcount;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002191 targs.resv = XFS_AG_RESV_AGFL;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002192
2193 /* Allocate as many blocks as possible at once. */
2194 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002195 if (error)
2196 goto out_agflbp_relse;
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Stop if we run out. Won't happen if callers are obeying
2200 * the restrictions correctly. Can happen for free calls
2201 * on a completely full ag.
2202 */
Yingping Lud210a282006-06-09 14:55:18 +10002203 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002204 if (flags & XFS_ALLOC_FLAG_FREEING)
2205 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002206 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Put each allocated block on the list.
2210 */
2211 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002212 error = xfs_alloc_put_freelist(tp, agbp,
2213 agflbp, bno, 0);
2214 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002215 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002218 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 args->agbp = agbp;
2220 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002221
2222out_agflbp_relse:
2223 xfs_trans_brelse(tp, agflbp);
2224out_agbp_relse:
2225 if (agbp)
2226 xfs_trans_brelse(tp, agbp);
2227out_no_agbp:
2228 args->agbp = NULL;
2229 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/*
2233 * Get a block from the freelist.
2234 * Returns with the buffer for the block gotten.
2235 */
2236int /* error */
2237xfs_alloc_get_freelist(
2238 xfs_trans_t *tp, /* transaction pointer */
2239 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002240 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2241 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2245 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002246 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int error;
David Chinner92821e22007-05-24 15:26:31 +10002248 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002249 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 xfs_perag_t *pag; /* per allocation group data */
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Freelist is empty, give up.
2254 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002255 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!agf->agf_flcount) {
2257 *bnop = NULLAGBLOCK;
2258 return 0;
2259 }
2260 /*
2261 * Read the array of free blocks.
2262 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002263 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2264 &agflbp);
2265 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002267
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /*
2270 * Get the block number and update the data structures.
2271 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002272 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2273 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002274 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002276 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002278
2279 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002280 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 xfs_trans_agflist_delta(tp, -1);
2282 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002283 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002284
2285 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2286 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002287 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002288 pag->pagf_btreeblks++;
2289 logflags |= XFS_AGF_BTREEBLKS;
2290 }
2291
David Chinner92821e22007-05-24 15:26:31 +10002292 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *bnop = bno;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
2296}
2297
2298/*
2299 * Log the given fields from the agf structure.
2300 */
2301void
2302xfs_alloc_log_agf(
2303 xfs_trans_t *tp, /* transaction pointer */
2304 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2305 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2306{
2307 int first; /* first byte offset */
2308 int last; /* last byte offset */
2309 static const short offsets[] = {
2310 offsetof(xfs_agf_t, agf_magicnum),
2311 offsetof(xfs_agf_t, agf_versionnum),
2312 offsetof(xfs_agf_t, agf_seqno),
2313 offsetof(xfs_agf_t, agf_length),
2314 offsetof(xfs_agf_t, agf_roots[0]),
2315 offsetof(xfs_agf_t, agf_levels[0]),
2316 offsetof(xfs_agf_t, agf_flfirst),
2317 offsetof(xfs_agf_t, agf_fllast),
2318 offsetof(xfs_agf_t, agf_flcount),
2319 offsetof(xfs_agf_t, agf_freeblks),
2320 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002321 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002322 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002323 offsetof(xfs_agf_t, agf_rmap_blocks),
Darrick J. Wongda1f0392016-08-26 15:59:31 +10002324 /* needed so that we don't log the whole rest of the structure: */
2325 offsetof(xfs_agf_t, agf_spare64),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 sizeof(xfs_agf_t)
2327 };
2328
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002329 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2330
Dave Chinner61fe1352013-04-03 16:11:30 +11002331 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2334 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2335}
2336
2337/*
2338 * Interface for inode allocation to force the pag data to be initialized.
2339 */
2340int /* error */
2341xfs_alloc_pagf_init(
2342 xfs_mount_t *mp, /* file system mount structure */
2343 xfs_trans_t *tp, /* transaction pointer */
2344 xfs_agnumber_t agno, /* allocation group number */
2345 int flags) /* XFS_ALLOC_FLAGS_... */
2346{
2347 xfs_buf_t *bp;
2348 int error;
2349
2350 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2351 return error;
2352 if (bp)
2353 xfs_trans_brelse(tp, bp);
2354 return 0;
2355}
2356
2357/*
2358 * Put the block on the freelist for the allocation group.
2359 */
2360int /* error */
2361xfs_alloc_put_freelist(
2362 xfs_trans_t *tp, /* transaction pointer */
2363 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2364 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002365 xfs_agblock_t bno, /* block being freed */
2366 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002369 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 int error;
David Chinner92821e22007-05-24 15:26:31 +10002371 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 xfs_mount_t *mp; /* mount structure */
2373 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002374 __be32 *agfl_bno;
2375 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 agf = XFS_BUF_TO_AGF(agbp);
2378 mp = tp->t_mountp;
2379
2380 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002381 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002383 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002384 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002386
2387 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002388 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 xfs_trans_agflist_delta(tp, 1);
2390 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002391
2392 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2393 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002394 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002395 pag->pagf_btreeblks--;
2396 logflags |= XFS_AGF_BTREEBLKS;
2397 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002398 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002399
David Chinner92821e22007-05-24 15:26:31 +10002400 xfs_alloc_log_agf(tp, agbp, logflags);
2401
Christoph Hellwig16259e72005-11-02 15:11:25 +11002402 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002403
2404 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2405 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002406 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002407 startoff = (char *)blockp - (char *)agflbp->b_addr;
2408
David Chinner92821e22007-05-24 15:26:31 +10002409 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002410
Dave Chinner61fe1352013-04-03 16:11:30 +11002411 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002412 xfs_trans_log_buf(tp, agflbp, startoff,
2413 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return 0;
2415}
2416
Dave Chinner4e0e6042013-04-03 16:11:13 +11002417static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002418xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002419 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002420 struct xfs_buf *bp)
2421 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002422 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002423
Brian Fostera45086e2015-10-12 15:59:25 +11002424 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2425 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002426 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002427 if (!xfs_log_check_lsn(mp,
2428 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2429 return false;
2430 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002431
Dave Chinner4e0e6042013-04-03 16:11:13 +11002432 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2433 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2434 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2435 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2436 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2437 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2438 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002439
Eric Sandeene1b05722014-09-09 11:47:24 +10002440 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2441 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2442 return false;
2443
Darrick J. Wongb8704942016-08-03 11:30:32 +10002444 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2445 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2446 return false;
2447
Dave Chinner5d5f5272012-11-14 17:44:56 +11002448 /*
2449 * during growfs operations, the perag is not fully initialised,
2450 * so we can't use it for any useful checking. growfs ensures we can't
2451 * use it by using uncached buffers that don't have the perag attached
2452 * so we can detect and avoid this problem.
2453 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002454 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2455 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002456
Dave Chinner4e0e6042013-04-03 16:11:13 +11002457 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2458 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2459 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002460
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002461 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
2462 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS)
2463 return false;
2464
Dave Chinner4e0e6042013-04-03 16:11:13 +11002465 return true;;
2466
Dave Chinner612cfbf2012-11-14 17:52:32 +11002467}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002468
Dave Chinner1813dd62012-11-14 17:54:40 +11002469static void
2470xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002471 struct xfs_buf *bp)
2472{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002473 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002474
Eric Sandeence5028c2014-02-27 15:23:10 +11002475 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2476 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002477 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002478 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2479 XFS_ERRTAG_ALLOC_READ_AGF,
2480 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002481 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002482
2483 if (bp->b_error)
2484 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002485}
2486
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002487static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002488xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002489 struct xfs_buf *bp)
2490{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002491 struct xfs_mount *mp = bp->b_target->bt_mount;
2492 struct xfs_buf_log_item *bip = bp->b_fspriv;
2493
2494 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002495 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002496 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002497 return;
2498 }
2499
2500 if (!xfs_sb_version_hascrc(&mp->m_sb))
2501 return;
2502
2503 if (bip)
2504 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2505
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002506 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002507}
2508
Dave Chinner1813dd62012-11-14 17:54:40 +11002509const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002510 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002511 .verify_read = xfs_agf_read_verify,
2512 .verify_write = xfs_agf_write_verify,
2513};
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515/*
2516 * Read in the allocation group header (free/alloc section).
2517 */
2518int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002519xfs_read_agf(
2520 struct xfs_mount *mp, /* mount point structure */
2521 struct xfs_trans *tp, /* transaction pointer */
2522 xfs_agnumber_t agno, /* allocation group number */
2523 int flags, /* XFS_BUF_ */
2524 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 int error;
2527
Dave Chinnerd1230312013-11-01 15:27:19 +11002528 trace_xfs_read_agf(mp, agno);
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 ASSERT(agno != NULLAGNUMBER);
2531 error = xfs_trans_read_buf(
2532 mp, tp, mp->m_ddev_targp,
2533 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002534 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (error)
2536 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002537 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002539
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002540 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002541 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002542 return 0;
2543}
2544
2545/*
2546 * Read in the allocation group header (free/alloc section).
2547 */
2548int /* error */
2549xfs_alloc_read_agf(
2550 struct xfs_mount *mp, /* mount point structure */
2551 struct xfs_trans *tp, /* transaction pointer */
2552 xfs_agnumber_t agno, /* allocation group number */
2553 int flags, /* XFS_ALLOC_FLAG_... */
2554 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2555{
2556 struct xfs_agf *agf; /* ag freelist header */
2557 struct xfs_perag *pag; /* per allocation group data */
2558 int error;
2559
Dave Chinnerd1230312013-11-01 15:27:19 +11002560 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002561
Dave Chinnerd1230312013-11-01 15:27:19 +11002562 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002563 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002564 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002565 bpp);
2566 if (error)
2567 return error;
2568 if (!*bpp)
2569 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002570 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002571
2572 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002573 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002575 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002576 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002577 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2578 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002580 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002582 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002583 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2584 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Darrick J. Wong46eeb522016-10-03 09:11:16 -07002585 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002586 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002587 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002588 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 pag->pagf_init = 1;
2590 }
2591#ifdef DEBUG
2592 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002593 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002594 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002595 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2596 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002598 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002600 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002603 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return 0;
2605}
2606
2607/*
2608 * Allocate an extent (variable-size).
2609 * Depending on the allocation type, we either look in a single allocation
2610 * group or loop over the allocation groups to find the result.
2611 */
2612int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002613xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 xfs_alloc_arg_t *args) /* allocation argument structure */
2615{
2616 xfs_agblock_t agsize; /* allocation group size */
2617 int error;
2618 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 xfs_extlen_t minleft;/* minimum left value, temp copy */
2620 xfs_mount_t *mp; /* mount structure pointer */
2621 xfs_agnumber_t sagno; /* starting allocation group number */
2622 xfs_alloctype_t type; /* input allocation type */
2623 int bump_rotor = 0;
2624 int no_min = 0;
2625 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2626
2627 mp = args->mp;
2628 type = args->otype = args->type;
2629 args->agbno = NULLAGBLOCK;
2630 /*
2631 * Just fix this up, for the case where the last a.g. is shorter
2632 * (or there's only one a.g.) and the caller couldn't easily figure
2633 * that out (xfs_bmap_alloc).
2634 */
2635 agsize = mp->m_sb.sb_agblocks;
2636 if (args->maxlen > agsize)
2637 args->maxlen = agsize;
2638 if (args->alignment == 0)
2639 args->alignment = 1;
2640 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2641 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2642 ASSERT(args->minlen <= args->maxlen);
2643 ASSERT(args->minlen <= agsize);
2644 ASSERT(args->mod < args->prod);
2645 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2646 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2647 args->minlen > args->maxlen || args->minlen > agsize ||
2648 args->mod >= args->prod) {
2649 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002650 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return 0;
2652 }
2653 minleft = args->minleft;
2654
2655 switch (type) {
2656 case XFS_ALLOCTYPE_THIS_AG:
2657 case XFS_ALLOCTYPE_NEAR_BNO:
2658 case XFS_ALLOCTYPE_THIS_BNO:
2659 /*
2660 * These three force us into a single a.g.
2661 */
2662 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002663 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 args->minleft = 0;
2665 error = xfs_alloc_fix_freelist(args, 0);
2666 args->minleft = minleft;
2667 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002668 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 goto error0;
2670 }
2671 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002672 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 break;
2674 }
2675 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2676 if ((error = xfs_alloc_ag_vextent(args)))
2677 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 case XFS_ALLOCTYPE_START_BNO:
2680 /*
2681 * Try near allocation first, then anywhere-in-ag after
2682 * the first a.g. fails.
2683 */
Dave Chinner292378e2016-09-26 08:21:28 +10002684 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2686 args->fsbno = XFS_AGB_TO_FSB(mp,
2687 ((mp->m_agfrotor / rotorstep) %
2688 mp->m_sb.sb_agcount), 0);
2689 bump_rotor = 1;
2690 }
2691 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2692 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2693 /* FALLTHROUGH */
2694 case XFS_ALLOCTYPE_ANY_AG:
2695 case XFS_ALLOCTYPE_START_AG:
2696 case XFS_ALLOCTYPE_FIRST_AG:
2697 /*
2698 * Rotate through the allocation groups looking for a winner.
2699 */
2700 if (type == XFS_ALLOCTYPE_ANY_AG) {
2701 /*
2702 * Start with the last place we left off.
2703 */
2704 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2705 mp->m_sb.sb_agcount;
2706 args->type = XFS_ALLOCTYPE_THIS_AG;
2707 flags = XFS_ALLOC_FLAG_TRYLOCK;
2708 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2709 /*
2710 * Start with allocation group given by bno.
2711 */
2712 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2713 args->type = XFS_ALLOCTYPE_THIS_AG;
2714 sagno = 0;
2715 flags = 0;
2716 } else {
2717 if (type == XFS_ALLOCTYPE_START_AG)
2718 args->type = XFS_ALLOCTYPE_THIS_AG;
2719 /*
2720 * Start with the given allocation group.
2721 */
2722 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2723 flags = XFS_ALLOC_FLAG_TRYLOCK;
2724 }
2725 /*
2726 * Loop over allocation groups twice; first time with
2727 * trylock set, second time without.
2728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002730 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (no_min) args->minleft = 0;
2732 error = xfs_alloc_fix_freelist(args, flags);
2733 args->minleft = minleft;
2734 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002735 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 goto error0;
2737 }
2738 /*
2739 * If we get a buffer back then the allocation will fly.
2740 */
2741 if (args->agbp) {
2742 if ((error = xfs_alloc_ag_vextent(args)))
2743 goto error0;
2744 break;
2745 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002746
2747 trace_xfs_alloc_vextent_loopfailed(args);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*
2750 * Didn't work, figure out the next iteration.
2751 */
2752 if (args->agno == sagno &&
2753 type == XFS_ALLOCTYPE_START_BNO)
2754 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002755 /*
2756 * For the first allocation, we can try any AG to get
2757 * space. However, if we already have allocated a
2758 * block, we don't want to try AGs whose number is below
2759 * sagno. Otherwise, we may end up with out-of-order
2760 * locking of AGF, which might cause deadlock.
2761 */
2762 if (++(args->agno) == mp->m_sb.sb_agcount) {
2763 if (args->firstblock != NULLFSBLOCK)
2764 args->agno = sagno;
2765 else
2766 args->agno = 0;
2767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /*
2769 * Reached the starting a.g., must either be done
2770 * or switch to non-trylock mode.
2771 */
2772 if (args->agno == sagno) {
2773 if (no_min == 1) {
2774 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002775 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 break;
2777 }
2778 if (flags == 0) {
2779 no_min = 1;
2780 } else {
2781 flags = 0;
2782 if (type == XFS_ALLOCTYPE_START_BNO) {
2783 args->agbno = XFS_FSB_TO_AGBNO(mp,
2784 args->fsbno);
2785 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2786 }
2787 }
2788 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002789 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2792 if (args->agno == sagno)
2793 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2794 (mp->m_sb.sb_agcount * rotorstep);
2795 else
2796 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2797 (mp->m_sb.sb_agcount * rotorstep);
2798 }
2799 break;
2800 default:
2801 ASSERT(0);
2802 /* NOTREACHED */
2803 }
2804 if (args->agbno == NULLAGBLOCK)
2805 args->fsbno = NULLFSBLOCK;
2806 else {
2807 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2808#ifdef DEBUG
2809 ASSERT(args->len >= args->minlen);
2810 ASSERT(args->len <= args->maxlen);
2811 ASSERT(args->agbno % args->alignment == 0);
2812 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2813 args->len);
2814#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002815
2816 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +10002817 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002818 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2819 if (error)
2820 goto error0;
2821 }
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002824 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return 0;
2826error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002827 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return error;
2829}
2830
Dave Chinner4d89e202016-06-21 11:53:28 +10002831/* Ensure that the freelist is at full capacity. */
2832int
2833xfs_free_extent_fix_freelist(
2834 struct xfs_trans *tp,
2835 xfs_agnumber_t agno,
2836 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Dave Chinner4d89e202016-06-21 11:53:28 +10002838 struct xfs_alloc_arg args;
2839 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Dave Chinner4d89e202016-06-21 11:53:28 +10002841 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 args.tp = tp;
2843 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002844 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002845
2846 /*
2847 * validate that the block number is legal - the enables us to detect
2848 * and handle a silent filesystem corruption rather than crashing.
2849 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002850 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002851 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002852
Dave Chinnera862e0f2010-01-11 11:47:41 +00002853 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002854 ASSERT(args.pag);
2855
2856 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2857 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002858 goto out;
2859
2860 *agbp = args.agbp;
2861out:
2862 xfs_perag_put(args.pag);
2863 return error;
2864}
2865
2866/*
2867 * Free an extent.
2868 * Just break up the extent address and hand off to xfs_free_ag_extent
2869 * after fixing up the freelist.
2870 */
2871int /* error */
2872xfs_free_extent(
2873 struct xfs_trans *tp, /* transaction pointer */
2874 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002875 xfs_extlen_t len, /* length of extent */
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002876 struct xfs_owner_info *oinfo, /* extent owner */
2877 enum xfs_ag_resv_type type) /* block reservation type */
Dave Chinner4d89e202016-06-21 11:53:28 +10002878{
2879 struct xfs_mount *mp = tp->t_mountp;
2880 struct xfs_buf *agbp;
2881 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2882 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2883 int error;
2884
2885 ASSERT(len != 0);
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002886 ASSERT(type != XFS_AG_RESV_AGFL);
Dave Chinner4d89e202016-06-21 11:53:28 +10002887
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002888 if (XFS_TEST_ERROR(false, mp,
2889 XFS_ERRTAG_FREE_EXTENT,
2890 XFS_RANDOM_FREE_EXTENT))
2891 return -EIO;
2892
Dave Chinner4d89e202016-06-21 11:53:28 +10002893 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2894 if (error)
2895 return error;
2896
2897 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002898
2899 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002900 XFS_WANT_CORRUPTED_GOTO(mp,
2901 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2902 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002903
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10002904 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
Dave Chinner4d89e202016-06-21 11:53:28 +10002905 if (error)
2906 goto err;
2907
2908 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2909 return 0;
2910
2911err:
2912 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return error;
2914}