blob: 3dd8f1d5449877ed45aa0fb391c1e9048c5c1893 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_btree.h"
Darrick J. Wong673930c2016-08-03 11:33:43 +100030#include "xfs_rmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000033#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110035#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110037#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110038#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110039#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dave Chinnerc999a222012-03-22 05:15:07 +000041struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
44
45#define XFSA_FIXUP_BNO_OK 1
46#define XFSA_FIXUP_CNT_OK 2
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
49STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000052 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Darrick J. Wong80180262016-08-03 11:31:47 +100054xfs_extlen_t
55xfs_prealloc_blocks(
56 struct xfs_mount *mp)
57{
58 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
59 return XFS_RMAP_BLOCK(mp) + 1;
60 if (xfs_sb_version_hasfinobt(&mp->m_sb))
61 return XFS_FIBT_BLOCK(mp) + 1;
62 return XFS_IBT_BLOCK(mp) + 1;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Darrick J. Wong52548852016-08-03 11:38:24 +100066 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
67 * AGF buffer (PV 947395), we place constraints on the relationship among
68 * actual allocations for data blocks, freelist blocks, and potential file data
69 * bmap btree blocks. However, these restrictions may result in no actual space
70 * allocated for a delayed extent, for example, a data block in a certain AG is
71 * allocated but there is no additional block for the additional bmap btree
72 * block due to a split of the bmap btree of the file. The result of this may
73 * lead to an infinite loop when the file gets flushed to disk and all delayed
74 * extents need to be actually allocated. To get around this, we explicitly set
75 * aside a few blocks which will not be reserved in delayed allocation.
76 *
77 * When rmap is disabled, we need to reserve 4 fsbs _per AG_ for the freelist
78 * and 4 more to handle a potential split of the file's bmap btree.
79 *
80 * When rmap is enabled, we must also be able to handle two rmap btree inserts
81 * to record both the file data extent and a new bmbt block. The bmbt block
82 * might not be in the same AG as the file data extent. In the worst case
83 * the bmap btree splits multiple levels and all the new blocks come from
84 * different AGs, so set aside enough to handle rmap btree splits in all AGs.
85 */
86unsigned int
87xfs_alloc_set_aside(
88 struct xfs_mount *mp)
89{
90 unsigned int blocks;
91
92 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
93 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
94 blocks += mp->m_sb.sb_agcount * mp->m_rmap_maxlevels;
95 return blocks;
96}
97
98/*
99 * When deciding how much space to allocate out of an AG, we limit the
100 * allocation maximum size to the size the AG. However, we cannot use all the
101 * blocks in the AG - some are permanently used by metadata. These
102 * blocks are generally:
103 * - the AG superblock, AGF, AGI and AGFL
104 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
105 * the AGI free inode and rmap btree root blocks.
106 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
107 * - the rmapbt root block
108 *
109 * The AG headers are sector sized, so the amount of space they take up is
110 * dependent on filesystem geometry. The others are all single blocks.
111 */
112unsigned int
113xfs_alloc_ag_max_usable(
114 struct xfs_mount *mp)
115{
116 unsigned int blocks;
117
118 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
119 blocks += XFS_ALLOC_AGFL_RESERVE;
120 blocks += 3; /* AGF, AGI btree root blocks */
121 if (xfs_sb_version_hasfinobt(&mp->m_sb))
122 blocks++; /* finobt root block */
123 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
124 blocks++; /* rmap root block */
125
126 return mp->m_sb.sb_agblocks - blocks;
127}
128
129/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100130 * Lookup the record equal to [bno, len] in the btree given by cur.
131 */
132STATIC int /* error */
133xfs_alloc_lookup_eq(
134 struct xfs_btree_cur *cur, /* btree cursor */
135 xfs_agblock_t bno, /* starting block of extent */
136 xfs_extlen_t len, /* length of extent */
137 int *stat) /* success/failure */
138{
139 cur->bc_rec.a.ar_startblock = bno;
140 cur->bc_rec.a.ar_blockcount = len;
141 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
142}
143
144/*
145 * Lookup the first record greater than or equal to [bno, len]
146 * in the btree given by cur.
147 */
Dave Chinnera66d6362012-03-22 05:15:12 +0000148int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100149xfs_alloc_lookup_ge(
150 struct xfs_btree_cur *cur, /* btree cursor */
151 xfs_agblock_t bno, /* starting block of extent */
152 xfs_extlen_t len, /* length of extent */
153 int *stat) /* success/failure */
154{
155 cur->bc_rec.a.ar_startblock = bno;
156 cur->bc_rec.a.ar_blockcount = len;
157 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
158}
159
160/*
161 * Lookup the first record less than or equal to [bno, len]
162 * in the btree given by cur.
163 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000164static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100165xfs_alloc_lookup_le(
166 struct xfs_btree_cur *cur, /* btree cursor */
167 xfs_agblock_t bno, /* starting block of extent */
168 xfs_extlen_t len, /* length of extent */
169 int *stat) /* success/failure */
170{
171 cur->bc_rec.a.ar_startblock = bno;
172 cur->bc_rec.a.ar_blockcount = len;
173 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
174}
175
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100176/*
177 * Update the record referred to by cur to the value given
178 * by [bno, len].
179 * This either works (return 0) or gets an EFSCORRUPTED error.
180 */
181STATIC int /* error */
182xfs_alloc_update(
183 struct xfs_btree_cur *cur, /* btree cursor */
184 xfs_agblock_t bno, /* starting block of extent */
185 xfs_extlen_t len) /* length of extent */
186{
187 union xfs_btree_rec rec;
188
189 rec.alloc.ar_startblock = cpu_to_be32(bno);
190 rec.alloc.ar_blockcount = cpu_to_be32(len);
191 return xfs_btree_update(cur, &rec);
192}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100193
194/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100195 * Get the data from the pointed-to record.
196 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000197int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100198xfs_alloc_get_rec(
199 struct xfs_btree_cur *cur, /* btree cursor */
200 xfs_agblock_t *bno, /* output: starting block of extent */
201 xfs_extlen_t *len, /* output: length of extent */
202 int *stat) /* output: success/failure */
203{
204 union xfs_btree_rec *rec;
205 int error;
206
207 error = xfs_btree_get_rec(cur, &rec, stat);
208 if (!error && *stat == 1) {
209 *bno = be32_to_cpu(rec->alloc.ar_startblock);
210 *len = be32_to_cpu(rec->alloc.ar_blockcount);
211 }
212 return error;
213}
214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Compute aligned version of the found extent.
217 * Takes alignment and min length into account.
218 */
David Chinner12375c82008-04-10 12:21:32 +1000219STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000221 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 xfs_agblock_t foundbno, /* starting block in found extent */
223 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 xfs_agblock_t *resbno, /* result block number */
225 xfs_extlen_t *reslen) /* result length */
226{
227 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000229 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Christoph Hellwige26f0502011-04-24 19:06:15 +0000231 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000232 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000233
Brian Fosterbfe46d42015-05-29 08:53:00 +1000234 /*
235 * If we have a largish extent that happens to start before min_agbno,
236 * see if we can shift it into range...
237 */
238 if (bno < args->min_agbno && bno + len > args->min_agbno) {
239 diff = args->min_agbno - bno;
240 if (len > diff) {
241 bno += diff;
242 len -= diff;
243 }
244 }
245
Christoph Hellwige26f0502011-04-24 19:06:15 +0000246 if (args->alignment > 1 && len >= args->minlen) {
247 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000248
249 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000250
251 *resbno = aligned_bno;
252 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000254 *resbno = bno;
255 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/*
260 * Compute best start block and diff for "near" allocations.
261 * freelen >= wantlen already checked by caller.
262 */
263STATIC xfs_extlen_t /* difference value (absolute) */
264xfs_alloc_compute_diff(
265 xfs_agblock_t wantbno, /* target starting block */
266 xfs_extlen_t wantlen, /* target length */
267 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200268 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 xfs_agblock_t freebno, /* freespace's starting block */
270 xfs_extlen_t freelen, /* freespace's length */
271 xfs_agblock_t *newbnop) /* result: best start block from free */
272{
273 xfs_agblock_t freeend; /* end of freespace extent */
274 xfs_agblock_t newbno1; /* return block number */
275 xfs_agblock_t newbno2; /* other new block number */
276 xfs_extlen_t newlen1=0; /* length with newbno1 */
277 xfs_extlen_t newlen2=0; /* length with newbno2 */
278 xfs_agblock_t wantend; /* end of target extent */
279
280 ASSERT(freelen >= wantlen);
281 freeend = freebno + freelen;
282 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200283 /*
284 * We want to allocate from the start of a free extent if it is past
285 * the desired block or if we are allocating user data and the free
286 * extent is before desired block. The second case is there to allow
287 * for contiguous allocation from the remaining free space if the file
288 * grows in the short term.
289 */
290 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
292 newbno1 = NULLAGBLOCK;
293 } else if (freeend >= wantend && alignment > 1) {
294 newbno1 = roundup(wantbno, alignment);
295 newbno2 = newbno1 - alignment;
296 if (newbno1 >= freeend)
297 newbno1 = NULLAGBLOCK;
298 else
299 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
300 if (newbno2 < freebno)
301 newbno2 = NULLAGBLOCK;
302 else
303 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
304 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
305 if (newlen1 < newlen2 ||
306 (newlen1 == newlen2 &&
307 XFS_ABSDIFF(newbno1, wantbno) >
308 XFS_ABSDIFF(newbno2, wantbno)))
309 newbno1 = newbno2;
310 } else if (newbno2 != NULLAGBLOCK)
311 newbno1 = newbno2;
312 } else if (freeend >= wantend) {
313 newbno1 = wantbno;
314 } else if (alignment > 1) {
315 newbno1 = roundup(freeend - wantlen, alignment);
316 if (newbno1 > freeend - wantlen &&
317 newbno1 - alignment >= freebno)
318 newbno1 -= alignment;
319 else if (newbno1 >= freeend)
320 newbno1 = NULLAGBLOCK;
321 } else
322 newbno1 = freeend - wantlen;
323 *newbnop = newbno1;
324 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
325}
326
327/*
328 * Fix up the length, based on mod and prod.
329 * len should be k * prod + mod for some k.
330 * If len is too small it is returned unchanged.
331 * If len hits maxlen it is left alone.
332 */
333STATIC void
334xfs_alloc_fix_len(
335 xfs_alloc_arg_t *args) /* allocation argument structure */
336{
337 xfs_extlen_t k;
338 xfs_extlen_t rlen;
339
340 ASSERT(args->mod < args->prod);
341 rlen = args->len;
342 ASSERT(rlen >= args->minlen);
343 ASSERT(rlen <= args->maxlen);
344 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
345 (args->mod == 0 && rlen < args->prod))
346 return;
347 k = rlen % args->prod;
348 if (k == args->mod)
349 return;
Jan Kara30265112014-06-06 16:06:37 +1000350 if (k > args->mod)
351 rlen = rlen - (k - args->mod);
352 else
353 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100354 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000355 if ((int)rlen < (int)args->minlen)
356 return;
357 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
358 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 args->len = rlen;
360}
361
362/*
363 * Fix up length if there is too little space left in the a.g.
364 * Return 1 if ok, 0 if too little, should give up.
365 */
366STATIC int
367xfs_alloc_fix_minleft(
368 xfs_alloc_arg_t *args) /* allocation argument structure */
369{
370 xfs_agf_t *agf; /* a.g. freelist header */
371 int diff; /* free space difference */
372
373 if (args->minleft == 0)
374 return 1;
375 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100376 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 - args->len - args->minleft;
378 if (diff >= 0)
379 return 1;
380 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100381 /* casts to (int) catch length underflows */
382 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 1;
384 args->agbno = NULLAGBLOCK;
385 return 0;
386}
387
388/*
389 * Update the two btrees, logically removing from freespace the extent
390 * starting at rbno, rlen blocks. The extent is contained within the
391 * actual (current) free extent fbno for flen blocks.
392 * Flags are passed in indicating whether the cursors are set to the
393 * relevant records.
394 */
395STATIC int /* error code */
396xfs_alloc_fixup_trees(
397 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
398 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
399 xfs_agblock_t fbno, /* starting block of free extent */
400 xfs_extlen_t flen, /* length of free extent */
401 xfs_agblock_t rbno, /* starting block of returned extent */
402 xfs_extlen_t rlen, /* length of returned extent */
403 int flags) /* flags, XFSA_FIXUP_... */
404{
405 int error; /* error code */
406 int i; /* operation results */
407 xfs_agblock_t nfbno1; /* first new free startblock */
408 xfs_agblock_t nfbno2; /* second new free startblock */
409 xfs_extlen_t nflen1=0; /* first new free length */
410 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100411 struct xfs_mount *mp;
412
413 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
416 * Look up the record in the by-size tree if necessary.
417 */
418 if (flags & XFSA_FIXUP_CNT_OK) {
419#ifdef DEBUG
420 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
421 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100422 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 i == 1 && nfbno1 == fbno && nflen1 == flen);
424#endif
425 } else {
426 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
427 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100428 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 /*
431 * Look up the record in the by-block tree if necessary.
432 */
433 if (flags & XFSA_FIXUP_BNO_OK) {
434#ifdef DEBUG
435 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
436 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100437 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 i == 1 && nfbno1 == fbno && nflen1 == flen);
439#endif
440 } else {
441 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
442 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100443 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100446#ifdef DEBUG
447 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
448 struct xfs_btree_block *bnoblock;
449 struct xfs_btree_block *cntblock;
450
451 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
452 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
453
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100454 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100455 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * Deal with all four cases: the allocated record is contained
461 * within the freespace record, so we can have new freespace
462 * at either (or both) end, or no freespace remaining.
463 */
464 if (rbno == fbno && rlen == flen)
465 nfbno1 = nfbno2 = NULLAGBLOCK;
466 else if (rbno == fbno) {
467 nfbno1 = rbno + rlen;
468 nflen1 = flen - rlen;
469 nfbno2 = NULLAGBLOCK;
470 } else if (rbno + rlen == fbno + flen) {
471 nfbno1 = fbno;
472 nflen1 = flen - rlen;
473 nfbno2 = NULLAGBLOCK;
474 } else {
475 nfbno1 = fbno;
476 nflen1 = rbno - fbno;
477 nfbno2 = rbno + rlen;
478 nflen2 = (fbno + flen) - nfbno2;
479 }
480 /*
481 * Delete the entry from the by-size btree.
482 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100483 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100485 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Add new by-size btree entry(s).
488 */
489 if (nfbno1 != NULLAGBLOCK) {
490 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
491 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100492 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100493 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100495 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 if (nfbno2 != NULLAGBLOCK) {
498 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
499 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100500 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100501 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100503 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 /*
506 * Fix up the by-block btree entry(s).
507 */
508 if (nfbno1 == NULLAGBLOCK) {
509 /*
510 * No remaining freespace, just delete the by-block tree entry.
511 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100512 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100514 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else {
516 /*
517 * Update the by-block entry to start later|be shorter.
518 */
519 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
520 return error;
521 }
522 if (nfbno2 != NULLAGBLOCK) {
523 /*
524 * 2 resulting free entries, need to add one.
525 */
526 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
527 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100528 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100529 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100531 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 return 0;
534}
535
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100536static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100537xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100538 struct xfs_buf *bp)
539{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100540 struct xfs_mount *mp = bp->b_target->bt_mount;
541 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100542 int i;
543
Eric Sandeence748ea2015-07-29 11:53:31 +1000544 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100545 return false;
546 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
547 return false;
548 /*
549 * during growfs operations, the perag is not fully initialised,
550 * so we can't use it for any useful checking. growfs ensures we can't
551 * use it by using uncached buffers that don't have the perag attached
552 * so we can detect and avoid this problem.
553 */
554 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
555 return false;
556
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100557 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100558 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100559 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100560 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100561 }
Brian Fostera45086e2015-10-12 15:59:25 +1100562
563 return xfs_log_check_lsn(mp,
564 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100565}
566
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100567static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100568xfs_agfl_read_verify(
569 struct xfs_buf *bp)
570{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100571 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100572
573 /*
574 * There is no verification of non-crc AGFLs because mkfs does not
575 * initialise the AGFL to zero or NULL. Hence the only valid part of the
576 * AGFL is what the AGF says is active. We can't get to the AGF, so we
577 * can't verify just those entries are valid.
578 */
579 if (!xfs_sb_version_hascrc(&mp->m_sb))
580 return;
581
Eric Sandeence5028c2014-02-27 15:23:10 +1100582 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000583 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100584 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000585 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100586
587 if (bp->b_error)
588 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100589}
590
591static void
592xfs_agfl_write_verify(
593 struct xfs_buf *bp)
594{
595 struct xfs_mount *mp = bp->b_target->bt_mount;
596 struct xfs_buf_log_item *bip = bp->b_fspriv;
597
598 /* no verification of non-crc AGFLs */
599 if (!xfs_sb_version_hascrc(&mp->m_sb))
600 return;
601
602 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000603 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100604 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100605 return;
606 }
607
608 if (bip)
609 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
610
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100611 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100612}
613
Dave Chinner1813dd62012-11-14 17:54:40 +1100614const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100615 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100616 .verify_read = xfs_agfl_read_verify,
617 .verify_write = xfs_agfl_write_verify,
618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * Read in the allocation group free block array.
622 */
623STATIC int /* error */
624xfs_alloc_read_agfl(
625 xfs_mount_t *mp, /* mount point structure */
626 xfs_trans_t *tp, /* transaction pointer */
627 xfs_agnumber_t agno, /* allocation group number */
628 xfs_buf_t **bpp) /* buffer for the ag free block array */
629{
630 xfs_buf_t *bp; /* return value */
631 int error;
632
633 ASSERT(agno != NULLAGNUMBER);
634 error = xfs_trans_read_buf(
635 mp, tp, mp->m_ddev_targp,
636 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100637 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (error)
639 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000640 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *bpp = bp;
642 return 0;
643}
644
Christoph Hellwigecb69282011-03-04 12:59:55 +0000645STATIC int
646xfs_alloc_update_counters(
647 struct xfs_trans *tp,
648 struct xfs_perag *pag,
649 struct xfs_buf *agbp,
650 long len)
651{
652 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
653
654 pag->pagf_freeblks += len;
655 be32_add_cpu(&agf->agf_freeblks, len);
656
657 xfs_trans_agblocks_delta(tp, len);
658 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
659 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000660 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000661
662 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
663 return 0;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
667 * Allocation group level functions.
668 */
669
670/*
671 * Allocate a variable extent in the allocation group agno.
672 * Type and bno are used to determine where in the allocation group the
673 * extent will start.
674 * Extent's length (returned in *len) will be between minlen and maxlen,
675 * and of the form k * prod + mod unless there's nothing that large.
676 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
677 */
678STATIC int /* error */
679xfs_alloc_ag_vextent(
680 xfs_alloc_arg_t *args) /* argument structure for allocation */
681{
682 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 ASSERT(args->minlen > 0);
685 ASSERT(args->maxlen > 0);
686 ASSERT(args->minlen <= args->maxlen);
687 ASSERT(args->mod < args->prod);
688 ASSERT(args->alignment > 0);
689 /*
690 * Branch to correct routine based on the type.
691 */
692 args->wasfromfl = 0;
693 switch (args->type) {
694 case XFS_ALLOCTYPE_THIS_AG:
695 error = xfs_alloc_ag_vextent_size(args);
696 break;
697 case XFS_ALLOCTYPE_NEAR_BNO:
698 error = xfs_alloc_ag_vextent_near(args);
699 break;
700 case XFS_ALLOCTYPE_THIS_BNO:
701 error = xfs_alloc_ag_vextent_exact(args);
702 break;
703 default:
704 ASSERT(0);
705 /* NOTREACHED */
706 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000707
708 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Christoph Hellwigecb69282011-03-04 12:59:55 +0000711 ASSERT(args->len >= args->minlen);
712 ASSERT(args->len <= args->maxlen);
713 ASSERT(!args->wasfromfl || !args->isfl);
714 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Darrick J. Wong673930c2016-08-03 11:33:43 +1000716 /* if not file data, insert new block into the reverse map btree */
717 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
718 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
719 args->agbno, args->len, &args->oinfo);
720 if (error)
721 return error;
722 }
723
Christoph Hellwigecb69282011-03-04 12:59:55 +0000724 if (!args->wasfromfl) {
725 error = xfs_alloc_update_counters(args->tp, args->pag,
726 args->agbp,
727 -((long)(args->len)));
728 if (error)
729 return error;
730
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000731 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000732 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000734
735 if (!args->isfl) {
736 xfs_trans_mod_sb(args->tp, args->wasdel ?
737 XFS_TRANS_SB_RES_FDBLOCKS :
738 XFS_TRANS_SB_FDBLOCKS,
739 -((long)(args->len)));
740 }
741
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100742 XFS_STATS_INC(args->mp, xs_allocx);
743 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000744 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/*
748 * Allocate a variable extent at exactly agno/bno.
749 * Extent's length (returned in *len) will be between minlen and maxlen,
750 * and of the form k * prod + mod unless there's nothing that large.
751 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
752 */
753STATIC int /* error */
754xfs_alloc_ag_vextent_exact(
755 xfs_alloc_arg_t *args) /* allocation argument structure */
756{
757 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
758 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int error;
760 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000762 xfs_agblock_t tbno; /* start block of trimmed extent */
763 xfs_extlen_t tlen; /* length of trimmed extent */
764 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * Allocate/initialize a cursor for the by-number freespace btree.
771 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100772 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000773 args->agno, XFS_BTNUM_BNO);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
777 * Look for the closest free block <= bno, it must contain bno
778 * if any free block does.
779 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000780 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
781 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000783 if (!i)
784 goto not_found;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * Grab the freespace record.
788 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000789 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
790 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100792 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000796 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000798 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000799
800 /*
801 * Give up if the start of the extent is busy, or the freespace isn't
802 * long enough for the minimum request.
803 */
804 if (tbno > args->agbno)
805 goto not_found;
806 if (tlen < args->minlen)
807 goto not_found;
808 tend = tbno + tlen;
809 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000810 goto not_found;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /*
813 * End of extent will be smaller of the freespace end and the
814 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000815 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * Fix the length according to mod and prod if given.
817 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000818 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
819 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000821 if (!xfs_alloc_fix_minleft(args))
822 goto not_found;
823
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000824 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000827 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Allocate/initialize a cursor for the by-size btree.
829 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100830 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
831 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100833 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000834 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
835 args->len, XFSA_FIXUP_BNO_OK);
836 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
838 goto error0;
839 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
842 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000845 trace_xfs_alloc_exact_done(args);
846 return 0;
847
848not_found:
849 /* Didn't find it, return null. */
850 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
851 args->agbno = NULLAGBLOCK;
852 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854
855error0:
856 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000857 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return error;
859}
860
861/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000862 * Search the btree in a given direction via the search cursor and compare
863 * the records found against the good extent we've already found.
864 */
865STATIC int
866xfs_alloc_find_best_extent(
867 struct xfs_alloc_arg *args, /* allocation argument structure */
868 struct xfs_btree_cur **gcur, /* good cursor */
869 struct xfs_btree_cur **scur, /* searching cursor */
870 xfs_agblock_t gdiff, /* difference for search comparison */
871 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000872 xfs_extlen_t *slen, /* extent length */
873 xfs_agblock_t *sbnoa, /* aligned extent found by search */
874 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000875 int dir) /* 0 = search right, 1 = search left */
876{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000877 xfs_agblock_t new;
878 xfs_agblock_t sdiff;
879 int error;
880 int i;
881
882 /* The good extent is perfect, no need to search. */
883 if (!gdiff)
884 goto out_use_good;
885
886 /*
887 * Look until we find a better one, run out of space or run off the end.
888 */
889 do {
890 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
891 if (error)
892 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100893 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000894 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000895
896 /*
897 * The good extent is closer than this one.
898 */
899 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000900 if (*sbnoa > args->max_agbno)
901 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000902 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000903 goto out_use_good;
904 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000905 if (*sbnoa < args->min_agbno)
906 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000907 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000908 goto out_use_good;
909 }
910
911 /*
912 * Same distance, compare length and pick the best.
913 */
914 if (*slena >= args->minlen) {
915 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
916 xfs_alloc_fix_len(args);
917
918 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200919 args->alignment,
920 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000921 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000922
923 /*
924 * Choose closer size and invalidate other cursor.
925 */
926 if (sdiff < gdiff)
927 goto out_use_search;
928 goto out_use_good;
929 }
930
931 if (!dir)
932 error = xfs_btree_increment(*scur, 0, &i);
933 else
934 error = xfs_btree_decrement(*scur, 0, &i);
935 if (error)
936 goto error0;
937 } while (i);
938
939out_use_good:
940 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
941 *scur = NULL;
942 return 0;
943
944out_use_search:
945 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
946 *gcur = NULL;
947 return 0;
948
949error0:
950 /* caller invalidates cursors */
951 return error;
952}
953
954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * Allocate a variable extent near bno in the allocation group agno.
956 * Extent's length (returned in len) will be between minlen and maxlen,
957 * and of the form k * prod + mod unless there's nothing that large.
958 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
959 */
960STATIC int /* error */
961xfs_alloc_ag_vextent_near(
962 xfs_alloc_arg_t *args) /* allocation argument structure */
963{
964 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
965 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
966 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 xfs_agblock_t gtbno; /* start bno of right side entry */
968 xfs_agblock_t gtbnoa; /* aligned ... */
969 xfs_extlen_t gtdiff; /* difference to right side entry */
970 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000971 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 xfs_agblock_t gtnew; /* useful start bno of right side */
973 int error; /* error code */
974 int i; /* result code, temporary */
975 int j; /* result code, temporary */
976 xfs_agblock_t ltbno; /* start bno of left side entry */
977 xfs_agblock_t ltbnoa; /* aligned ... */
978 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000980 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 xfs_agblock_t ltnew; /* useful start bno of left side */
982 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000983 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000984#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /*
986 * Randomly don't execute the first algorithm.
987 */
988 int dofirst; /* set to do first algorithm */
989
Akinobu Mitaecb34032013-03-04 21:58:20 +0900990 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000992
Brian Fosterbfe46d42015-05-29 08:53:00 +1000993 /* handle unitialized agbno range so caller doesn't have to */
994 if (!args->min_agbno && !args->max_agbno)
995 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
996 ASSERT(args->min_agbno <= args->max_agbno);
997
998 /* clamp agbno to the range if it's outside */
999 if (args->agbno < args->min_agbno)
1000 args->agbno = args->min_agbno;
1001 if (args->agbno > args->max_agbno)
1002 args->agbno = args->max_agbno;
1003
Christoph Hellwige26f0502011-04-24 19:06:15 +00001004restart:
1005 bno_cur_lt = NULL;
1006 bno_cur_gt = NULL;
1007 ltlen = 0;
1008 gtlena = 0;
1009 ltlena = 0;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /*
1012 * Get a cursor for the by-size btree.
1013 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001014 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1015 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * See if there are any free extents as big as maxlen.
1019 */
1020 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1021 goto error0;
1022 /*
1023 * If none, then pick up the last entry in the tree unless the
1024 * tree is empty.
1025 */
1026 if (!i) {
1027 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1028 &ltlen, &i)))
1029 goto error0;
1030 if (i == 0 || ltlen == 0) {
1031 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001032 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034 }
1035 ASSERT(i == 1);
1036 }
1037 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * First algorithm.
1041 * If the requested extent is large wrt the freespaces available
1042 * in this a.g., then the cursor will be pointing to a btree entry
1043 * near the right edge of the tree. If it's in the last btree leaf
1044 * block, then we just examine all the entries in that block
1045 * that are big enough, and pick the best one.
1046 * This is written as a while loop so we can break out of it,
1047 * but we never loop back to the top.
1048 */
1049 while (xfs_btree_islastblock(cnt_cur, 0)) {
1050 xfs_extlen_t bdiff;
1051 int besti=0;
1052 xfs_extlen_t blen=0;
1053 xfs_agblock_t bnew=0;
1054
Dave Chinner63d20d62013-08-12 20:49:50 +10001055#ifdef DEBUG
1056 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058#endif
1059 /*
1060 * Start from the entry that lookup found, sequence through
1061 * all larger free blocks. If we're actually pointing at a
1062 * record smaller than maxlen, go to the start of this block,
1063 * and skip all those smaller than minlen.
1064 */
1065 if (ltlen || args->alignment > 1) {
1066 cnt_cur->bc_ptrs[0] = 1;
1067 do {
1068 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1069 &ltlen, &i)))
1070 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001071 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (ltlen >= args->minlen)
1073 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001074 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto error0;
1076 } while (i);
1077 ASSERT(ltlen >= args->minlen);
1078 if (!i)
1079 break;
1080 }
1081 i = cnt_cur->bc_ptrs[0];
1082 for (j = 1, blen = 0, bdiff = 0;
1083 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001084 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /*
1086 * For each entry, decide if it's better than
1087 * the previous best entry.
1088 */
1089 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1090 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001091 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001092 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1093 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001094 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001096 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1097 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1099 xfs_alloc_fix_len(args);
1100 ASSERT(args->len >= args->minlen);
1101 if (args->len < blen)
1102 continue;
1103 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001104 args->alignment, args->userdata, ltbnoa,
1105 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ltnew != NULLAGBLOCK &&
1107 (args->len > blen || ltdiff < bdiff)) {
1108 bdiff = ltdiff;
1109 bnew = ltnew;
1110 blen = args->len;
1111 besti = cnt_cur->bc_ptrs[0];
1112 }
1113 }
1114 /*
1115 * It didn't work. We COULD be in a case where
1116 * there's a good record somewhere, so try again.
1117 */
1118 if (blen == 0)
1119 break;
1120 /*
1121 * Point at the best entry, and retrieve it again.
1122 */
1123 cnt_cur->bc_ptrs[0] = besti;
1124 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1125 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001126 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001127 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 args->len = blen;
1129 if (!xfs_alloc_fix_minleft(args)) {
1130 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001131 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 0;
1133 }
1134 blen = args->len;
1135 /*
1136 * We are allocating starting at bnew for blen blocks.
1137 */
1138 args->agbno = bnew;
1139 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001140 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /*
1142 * Set up a cursor for the by-bno tree.
1143 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001144 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1145 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /*
1147 * Fix up the btree entries.
1148 */
1149 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1150 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1151 goto error0;
1152 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1153 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001154
1155 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157 }
1158 /*
1159 * Second algorithm.
1160 * Search in the by-bno tree to the left and to the right
1161 * simultaneously, until in each case we find a space big enough,
1162 * or run into the edge of the tree. When we run into the edge,
1163 * we deallocate that cursor.
1164 * If both searches succeed, we compare the two spaces and pick
1165 * the better one.
1166 * With alignment, it's possible for both to fail; the upper
1167 * level algorithm that picks allocation groups for allocations
1168 * is not supposed to do this.
1169 */
1170 /*
1171 * Allocate and initialize the cursor for the leftward search.
1172 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001173 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1174 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * Lookup <= bno to find the leftward search's starting point.
1177 */
1178 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1179 goto error0;
1180 if (!i) {
1181 /*
1182 * Didn't find anything; use this cursor for the rightward
1183 * search.
1184 */
1185 bno_cur_gt = bno_cur_lt;
1186 bno_cur_lt = NULL;
1187 }
1188 /*
1189 * Found something. Duplicate the cursor for the rightward search.
1190 */
1191 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1192 goto error0;
1193 /*
1194 * Increment the cursor, so we will point at the entry just right
1195 * of the leftward entry if any, or to the leftmost entry.
1196 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001197 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto error0;
1199 if (!i) {
1200 /*
1201 * It failed, there are no rightward entries.
1202 */
1203 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1204 bno_cur_gt = NULL;
1205 }
1206 /*
1207 * Loop going left with the leftward cursor, right with the
1208 * rightward cursor, until either both directions give up or
1209 * we find an entry at least as big as minlen.
1210 */
1211 do {
1212 if (bno_cur_lt) {
1213 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1214 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001215 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001216 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1217 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001218 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001220 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001222 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_btree_del_cursor(bno_cur_lt,
1224 XFS_BTREE_NOERROR);
1225 bno_cur_lt = NULL;
1226 }
1227 }
1228 if (bno_cur_gt) {
1229 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1230 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001231 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001232 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1233 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001234 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001236 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001238 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 xfs_btree_del_cursor(bno_cur_gt,
1240 XFS_BTREE_NOERROR);
1241 bno_cur_gt = NULL;
1242 }
1243 }
1244 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Got both cursors still active, need to find better entry.
1248 */
1249 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (ltlena >= args->minlen) {
1251 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001252 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1255 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001256 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001257 args->alignment, args->userdata, ltbnoa,
1258 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001259
1260 error = xfs_alloc_find_best_extent(args,
1261 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001262 ltdiff, &gtbno, &gtlen,
1263 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001264 0 /* search right */);
1265 } else {
1266 ASSERT(gtlena >= args->minlen);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001269 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
1271 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1272 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001273 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001274 args->alignment, args->userdata, gtbnoa,
1275 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001276
1277 error = xfs_alloc_find_best_extent(args,
1278 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001279 gtdiff, &ltbno, &ltlen,
1280 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001281 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001283
1284 if (error)
1285 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
1289 * If we couldn't get anything, give up.
1290 */
1291 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001292 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1293
Christoph Hellwige26f0502011-04-24 19:06:15 +00001294 if (!forced++) {
1295 trace_xfs_alloc_near_busy(args);
1296 xfs_log_force(args->mp, XFS_LOG_SYNC);
1297 goto restart;
1298 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001299 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 args->agbno = NULLAGBLOCK;
1301 return 0;
1302 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * At this point we have selected a freespace entry, either to the
1306 * left or to the right. If it's on the right, copy all the
1307 * useful variables to the "left" set so we only have one
1308 * copy of this code.
1309 */
1310 if (bno_cur_gt) {
1311 bno_cur_lt = bno_cur_gt;
1312 bno_cur_gt = NULL;
1313 ltbno = gtbno;
1314 ltbnoa = gtbnoa;
1315 ltlen = gtlen;
1316 ltlena = gtlena;
1317 j = 1;
1318 } else
1319 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Fix up the length and compute the useful address.
1323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1325 xfs_alloc_fix_len(args);
1326 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001327 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1329 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1330 return 0;
1331 }
1332 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001333 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001334 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001336 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001337 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001338 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1342 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1343 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001344
1345 if (j)
1346 trace_xfs_alloc_near_greater(args);
1347 else
1348 trace_xfs_alloc_near_lesser(args);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1351 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1352 return 0;
1353
1354 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001355 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (cnt_cur != NULL)
1357 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1358 if (bno_cur_lt != NULL)
1359 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1360 if (bno_cur_gt != NULL)
1361 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1362 return error;
1363}
1364
1365/*
1366 * Allocate a variable extent anywhere in the allocation group agno.
1367 * Extent's length (returned in len) will be between minlen and maxlen,
1368 * and of the form k * prod + mod unless there's nothing that large.
1369 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1370 */
1371STATIC int /* error */
1372xfs_alloc_ag_vextent_size(
1373 xfs_alloc_arg_t *args) /* allocation argument structure */
1374{
1375 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1376 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1377 int error; /* error result */
1378 xfs_agblock_t fbno; /* start of found freespace */
1379 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int i; /* temp status variable */
1381 xfs_agblock_t rbno; /* returned block number */
1382 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001383 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Christoph Hellwige26f0502011-04-24 19:06:15 +00001385restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Allocate and initialize a cursor for the by-size btree.
1388 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001389 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1390 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /*
1394 * Look for an entry >= maxlen+alignment-1 blocks.
1395 */
1396 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1397 args->maxlen + args->alignment - 1, &i)))
1398 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001401 * If none or we have busy extents that we cannot allocate from, then
1402 * we have to settle for a smaller extent. In the case that there are
1403 * no large extents, this will return the last entry in the tree unless
1404 * the tree is empty. In the case that there are only busy large
1405 * extents, this will return the largest small extent unless there
1406 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001408 if (!i || forced > 1) {
1409 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1410 &fbno, &flen, &i);
1411 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 goto error0;
1413 if (i == 0 || flen == 0) {
1414 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001415 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
1417 }
1418 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001419 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1420 } else {
1421 /*
1422 * Search for a non-busy extent that is large enough.
1423 * If we are at low space, don't check, or if we fall of
1424 * the end of the btree, turn off the busy check and
1425 * restart.
1426 */
1427 for (;;) {
1428 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1429 if (error)
1430 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001431 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001432
1433 xfs_alloc_compute_aligned(args, fbno, flen,
1434 &rbno, &rlen);
1435
1436 if (rlen >= args->maxlen)
1437 break;
1438
1439 error = xfs_btree_increment(cnt_cur, 0, &i);
1440 if (error)
1441 goto error0;
1442 if (i == 0) {
1443 /*
1444 * Our only valid extents must have been busy.
1445 * Make it unbusy by forcing the log out and
1446 * retrying. If we've been here before, forcing
1447 * the log isn't making the extents available,
1448 * which means they have probably been freed in
1449 * this transaction. In that case, we have to
1450 * give up on them and we'll attempt a minlen
1451 * allocation the next time around.
1452 */
1453 xfs_btree_del_cursor(cnt_cur,
1454 XFS_BTREE_NOERROR);
1455 trace_xfs_alloc_size_busy(args);
1456 if (!forced++)
1457 xfs_log_force(args->mp, XFS_LOG_SYNC);
1458 goto restart;
1459 }
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
1464 * In the first case above, we got the last entry in the
1465 * by-size btree. Now we check to see if the space hits maxlen
1466 * once aligned; if not, we search left for something better.
1467 * This can't happen in the second case above.
1468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001470 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1472 if (rlen < args->maxlen) {
1473 xfs_agblock_t bestfbno;
1474 xfs_extlen_t bestflen;
1475 xfs_agblock_t bestrbno;
1476 xfs_extlen_t bestrlen;
1477
1478 bestrlen = rlen;
1479 bestrbno = rbno;
1480 bestflen = flen;
1481 bestfbno = fbno;
1482 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001483 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto error0;
1485 if (i == 0)
1486 break;
1487 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1488 &i)))
1489 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001490 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (flen < bestrlen)
1492 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001493 xfs_alloc_compute_aligned(args, fbno, flen,
1494 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001496 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 (rlen <= flen && rbno + rlen <= fbno + flen),
1498 error0);
1499 if (rlen > bestrlen) {
1500 bestrlen = rlen;
1501 bestrbno = rbno;
1502 bestflen = flen;
1503 bestfbno = fbno;
1504 if (rlen == args->maxlen)
1505 break;
1506 }
1507 }
1508 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1509 &i)))
1510 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001511 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 rlen = bestrlen;
1513 rbno = bestrbno;
1514 flen = bestflen;
1515 fbno = bestfbno;
1516 }
1517 args->wasfromfl = 0;
1518 /*
1519 * Fix up the length.
1520 */
1521 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001522 if (rlen < args->minlen) {
1523 if (!forced++) {
1524 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1525 trace_xfs_alloc_size_busy(args);
1526 xfs_log_force(args->mp, XFS_LOG_SYNC);
1527 goto restart;
1528 }
1529 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001531 xfs_alloc_fix_len(args);
1532
1533 if (!xfs_alloc_fix_minleft(args))
1534 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001536 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /*
1538 * Allocate and initialize a cursor for the by-block tree.
1539 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001540 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1541 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1543 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1544 goto error0;
1545 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1546 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1547 cnt_cur = bno_cur = NULL;
1548 args->len = rlen;
1549 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001550 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001552 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
1556
1557error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001558 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (cnt_cur)
1560 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1561 if (bno_cur)
1562 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1563 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001564
1565out_nominleft:
1566 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1567 trace_xfs_alloc_size_nominleft(args);
1568 args->agbno = NULLAGBLOCK;
1569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
1572/*
1573 * Deal with the case where only small freespaces remain.
1574 * Either return the contents of the last freespace record,
1575 * or allocate space from the freelist if there is nothing in the tree.
1576 */
1577STATIC int /* error */
1578xfs_alloc_ag_vextent_small(
1579 xfs_alloc_arg_t *args, /* allocation argument structure */
1580 xfs_btree_cur_t *ccur, /* by-size cursor */
1581 xfs_agblock_t *fbnop, /* result block number */
1582 xfs_extlen_t *flenp, /* result length */
1583 int *stat) /* status: 0-freelist, 1-normal/none */
1584{
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001585 struct xfs_owner_info oinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int error;
1587 xfs_agblock_t fbno;
1588 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int i;
1590
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001591 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 goto error0;
1593 if (i) {
1594 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1595 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001596 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 /*
1599 * Nothing in the btree, try the freelist. Make sure
1600 * to respect minleft even when pulling from the
1601 * freelist.
1602 */
1603 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001604 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1605 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001606 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1607 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto error0;
1609 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001610 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001611 args->userdata);
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (args->userdata) {
1614 xfs_buf_t *bp;
1615
1616 bp = xfs_btree_get_bufs(args->mp, args->tp,
1617 args->agno, fbno, 0);
1618 xfs_trans_binval(args->tp, bp);
1619 }
1620 args->len = 1;
1621 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001622 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001624 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 error0);
1626 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001627 trace_xfs_alloc_small_freelist(args);
Darrick J. Wonga03f1a62016-08-17 11:12:57 +10001628
1629 /*
1630 * If we're feeding an AGFL block to something that
1631 * doesn't live in the free space, we need to clear
1632 * out the OWN_AG rmap.
1633 */
1634 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1635 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1636 fbno, 1, &oinfo);
1637 if (error)
1638 goto error0;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 *stat = 0;
1641 return 0;
1642 }
1643 /*
1644 * Nothing in the freelist.
1645 */
1646 else
1647 flen = 0;
1648 }
1649 /*
1650 * Can't allocate from the freelist for some reason.
1651 */
Nathan Scottd432c802006-09-28 11:03:44 +10001652 else {
1653 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /*
1657 * Can't do the allocation, give up.
1658 */
1659 if (flen < args->minlen) {
1660 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001661 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 flen = 0;
1663 }
1664 *fbnop = fbno;
1665 *flenp = flen;
1666 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001667 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669
1670error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001671 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return error;
1673}
1674
1675/*
1676 * Free the extent starting at agno/bno for length.
1677 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001678STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001680 xfs_trans_t *tp,
1681 xfs_buf_t *agbp,
1682 xfs_agnumber_t agno,
1683 xfs_agblock_t bno,
1684 xfs_extlen_t len,
1685 struct xfs_owner_info *oinfo,
1686 int isfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1689 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1690 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 xfs_agblock_t gtbno; /* start of right neighbor block */
1692 xfs_extlen_t gtlen; /* length of right neighbor block */
1693 int haveleft; /* have a left neighbor block */
1694 int haveright; /* have a right neighbor block */
1695 int i; /* temp, result code */
1696 xfs_agblock_t ltbno; /* start of left neighbor block */
1697 xfs_extlen_t ltlen; /* length of left neighbor block */
1698 xfs_mount_t *mp; /* mount point struct for filesystem */
1699 xfs_agblock_t nbno; /* new starting block of freespace */
1700 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001701 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Darrick J. Wong673930c2016-08-03 11:33:43 +10001703 bno_cur = cnt_cur = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 mp = tp->t_mountp;
Darrick J. Wong673930c2016-08-03 11:33:43 +10001705
1706 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1707 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1708 if (error)
1709 goto error0;
1710 }
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
1713 * Allocate and initialize a cursor for the by-block btree.
1714 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001715 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * Look for a neighboring block on the left (lower block numbers)
1718 * that is contiguous with this space.
1719 */
1720 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1721 goto error0;
1722 if (haveleft) {
1723 /*
1724 * There is a block to our left.
1725 */
1726 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1727 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001728 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /*
1730 * It's not contiguous, though.
1731 */
1732 if (ltbno + ltlen < bno)
1733 haveleft = 0;
1734 else {
1735 /*
1736 * If this failure happens the request to free this
1737 * space was invalid, it's (partly) already free.
1738 * Very bad.
1739 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001740 XFS_WANT_CORRUPTED_GOTO(mp,
1741 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
1744 /*
1745 * Look for a neighboring block on the right (higher block numbers)
1746 * that is contiguous with this space.
1747 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001748 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto error0;
1750 if (haveright) {
1751 /*
1752 * There is a block to our right.
1753 */
1754 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1755 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001756 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * It's not contiguous, though.
1759 */
1760 if (bno + len < gtbno)
1761 haveright = 0;
1762 else {
1763 /*
1764 * If this failure happens the request to free this
1765 * space was invalid, it's (partly) already free.
1766 * Very bad.
1767 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001768 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
1771 /*
1772 * Now allocate and initialize a cursor for the by-size tree.
1773 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001774 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /*
1776 * Have both left and right contiguous neighbors.
1777 * Merge all three into a single free block.
1778 */
1779 if (haveleft && haveright) {
1780 /*
1781 * Delete the old by-size entry on the left.
1782 */
1783 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1784 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001786 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001788 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Delete the old by-size entry on the right.
1791 */
1792 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1793 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001794 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001795 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001797 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
1799 * Delete the old by-block entry for the right block.
1800 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001801 if ((error = xfs_btree_delete(bno_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 * Move the by-block cursor back to the left neighbor.
1806 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001807 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001809 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810#ifdef DEBUG
1811 /*
1812 * Check that this is the right record: delete didn't
1813 * mangle the cursor.
1814 */
1815 {
1816 xfs_agblock_t xxbno;
1817 xfs_extlen_t xxlen;
1818
1819 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1820 &i)))
1821 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001822 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 i == 1 && xxbno == ltbno && xxlen == ltlen,
1824 error0);
1825 }
1826#endif
1827 /*
1828 * Update remaining by-block entry to the new, joined block.
1829 */
1830 nbno = ltbno;
1831 nlen = len + ltlen + gtlen;
1832 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1833 goto error0;
1834 }
1835 /*
1836 * Have only a left contiguous neighbor.
1837 * Merge it together with the new freespace.
1838 */
1839 else if (haveleft) {
1840 /*
1841 * Delete the old by-size entry on the left.
1842 */
1843 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1844 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001846 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /*
1850 * Back up the by-block cursor to the left neighbor, and
1851 * update its length.
1852 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001853 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001855 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 nbno = ltbno;
1857 nlen = len + ltlen;
1858 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1859 goto error0;
1860 }
1861 /*
1862 * Have only a right contiguous neighbor.
1863 * Merge it together with the new freespace.
1864 */
1865 else if (haveright) {
1866 /*
1867 * Delete the old by-size entry on the right.
1868 */
1869 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1870 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001871 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001872 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001874 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /*
1876 * Update the starting block and length of the right
1877 * neighbor in the by-block tree.
1878 */
1879 nbno = bno;
1880 nlen = len + gtlen;
1881 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1882 goto error0;
1883 }
1884 /*
1885 * No contiguous neighbors.
1886 * Insert the new freespace into the by-block tree.
1887 */
1888 else {
1889 nbno = bno;
1890 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001891 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001893 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1896 bno_cur = NULL;
1897 /*
1898 * In all cases we need to insert the new freespace in the by-size tree.
1899 */
1900 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1901 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001902 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001903 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001905 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1907 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * Update the freespace totals in the ag and superblock.
1911 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001912 pag = xfs_perag_get(mp, agno);
1913 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1914 xfs_perag_put(pag);
1915 if (error)
1916 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Christoph Hellwigecb69282011-03-04 12:59:55 +00001918 if (!isfl)
1919 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001920 XFS_STATS_INC(mp, xs_freex);
1921 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001922
1923 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 0;
1926
1927 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001928 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (bno_cur)
1930 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1931 if (cnt_cur)
1932 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1933 return error;
1934}
1935
1936/*
1937 * Visible (exported) allocation/free functions.
1938 * Some of these are used just by xfs_alloc_btree.c and this file.
1939 */
1940
1941/*
1942 * Compute and fill in value of m_ag_maxlevels.
1943 */
1944void
1945xfs_alloc_compute_maxlevels(
1946 xfs_mount_t *mp) /* file system mount structure */
1947{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001948 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1949 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001953 * Find the length of the longest extent in an AG.
1954 */
1955xfs_extlen_t
1956xfs_alloc_longest_free_extent(
1957 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001958 struct xfs_perag *pag,
1959 xfs_extlen_t need)
Dave Chinner6cc87642009-03-16 08:29:46 +01001960{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001961 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001962
Dave Chinner6cc87642009-03-16 08:29:46 +01001963 if (need > pag->pagf_flcount)
1964 delta = need - pag->pagf_flcount;
1965
1966 if (pag->pagf_longest > delta)
1967 return pag->pagf_longest - delta;
1968 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1969}
1970
Dave Chinner496817b2015-06-22 10:13:30 +10001971unsigned int
1972xfs_alloc_min_freelist(
1973 struct xfs_mount *mp,
1974 struct xfs_perag *pag)
1975{
1976 unsigned int min_free;
1977
1978 /* space needed by-bno freespace btree */
1979 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1980 mp->m_ag_maxlevels);
1981 /* space needed by-size freespace btree */
1982 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1983 mp->m_ag_maxlevels);
Darrick J. Wong52548852016-08-03 11:38:24 +10001984 /* space needed reverse mapping used space btree */
1985 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1986 min_free += min_t(unsigned int,
1987 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
1988 mp->m_rmap_maxlevels);
Dave Chinner496817b2015-06-22 10:13:30 +10001989
1990 return min_free;
1991}
1992
Dave Chinner6cc87642009-03-16 08:29:46 +01001993/*
Dave Chinner72d55282015-06-22 10:04:42 +10001994 * Check if the operation we are fixing up the freelist for should go ahead or
1995 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1996 * is dependent on whether the size and shape of free space available will
1997 * permit the requested allocation to take place.
1998 */
1999static bool
2000xfs_alloc_space_available(
2001 struct xfs_alloc_arg *args,
2002 xfs_extlen_t min_free,
2003 int flags)
2004{
2005 struct xfs_perag *pag = args->pag;
2006 xfs_extlen_t longest;
2007 int available;
2008
2009 if (flags & XFS_ALLOC_FLAG_FREEING)
2010 return true;
2011
2012 /* do we have enough contiguous free space for the allocation? */
2013 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free);
2014 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2015 return false;
2016
2017 /* do have enough free space remaining for the allocation? */
2018 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
2019 min_free - args->total);
2020 if (available < (int)args->minleft)
2021 return false;
2022
2023 return true;
2024}
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026/*
2027 * Decide whether to use this allocation group for this allocation.
2028 * If so, fix up the btree freelist's size.
2029 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11002030int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10002032 struct xfs_alloc_arg *args, /* allocation argument structure */
2033 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Dave Chinner396503f2015-06-22 10:13:19 +10002035 struct xfs_mount *mp = args->mp;
2036 struct xfs_perag *pag = args->pag;
2037 struct xfs_trans *tp = args->tp;
2038 struct xfs_buf *agbp = NULL;
2039 struct xfs_buf *agflbp = NULL;
2040 struct xfs_alloc_arg targs; /* local allocation arguments */
2041 xfs_agblock_t bno; /* freelist block */
2042 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10002043 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10002046 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2047 if (error)
2048 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002050 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2051 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002052 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Dave Chinner396503f2015-06-22 10:13:19 +10002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002056 /*
Dave Chinner396503f2015-06-22 10:13:19 +10002057 * If this is a metadata preferred pag and we are user data then try
2058 * somewhere else if we are not being asked to try harder at this
2059 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002061 if (pag->pagf_metadata && args->userdata &&
2062 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2063 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002064 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
Dave Chinner496817b2015-06-22 10:13:30 +10002067 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002068 if (!xfs_alloc_space_available(args, need, flags))
2069 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /*
2072 * Get the a.g. freespace buffer.
2073 * Can fail if we're not blocking on locks, and it's held.
2074 */
Dave Chinner396503f2015-06-22 10:13:19 +10002075 if (!agbp) {
2076 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2077 if (error)
2078 goto out_no_agbp;
2079 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002080 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2081 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10002082 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002085
Dave Chinner50adbcb2015-06-22 10:04:31 +10002086 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10002087 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10002088 if (!xfs_alloc_space_available(args, need, flags))
2089 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10002093 *
Dave Chinner396503f2015-06-22 10:13:19 +10002094 * Note that from this point onwards, we will always release the agf and
2095 * agfl buffers on error. This handles the case where we error out and
2096 * the buffers are clean or may not have been joined to the transaction
2097 * and hence need to be released manually. If they have been joined to
2098 * the transaction, then xfs_trans_brelse() will handle them
2099 * appropriately based on the recursion count and dirty state of the
2100 * buffer.
2101 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002102 * XXX (dgc): When we have lots of free space, does this buy us
2103 * anything other than extra overhead when we need to put more blocks
2104 * back on the free list? Maybe we should only do this when space is
2105 * getting low or the AGFL is more than half full?
Darrick J. Wong04f13062016-08-03 12:19:53 +10002106 *
2107 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2108 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2109 * updating the rmapbt. Both flags are used in xfs_repair while we're
2110 * rebuilding the rmapbt, and neither are used by the kernel. They're
2111 * both required to ensure that rmaps are correctly recorded for the
2112 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2113 * repair/rmap.c in xfsprogs for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Darrick J. Wong04f13062016-08-03 12:19:53 +10002115 memset(&targs, 0, sizeof(targs));
2116 if (flags & XFS_ALLOC_FLAG_NORMAP)
2117 xfs_rmap_skip_owner_update(&targs.oinfo);
2118 else
2119 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2120 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
Dave Chinner50adbcb2015-06-22 10:04:31 +10002121 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
David Chinner92821e22007-05-24 15:26:31 +10002123 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2124 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002125 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002126 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
2127 &targs.oinfo, 1);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002128 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002129 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2131 xfs_trans_binval(tp, bp);
2132 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 targs.tp = tp;
2135 targs.mp = mp;
2136 targs.agbp = agbp;
2137 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2139 targs.type = XFS_ALLOCTYPE_THIS_AG;
2140 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002141 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2142 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002143 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002144
2145 /* Make the freelist longer if it's too short. */
2146 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002148 targs.maxlen = need - pag->pagf_flcount;
2149
2150 /* Allocate as many blocks as possible at once. */
2151 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002152 if (error)
2153 goto out_agflbp_relse;
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /*
2156 * Stop if we run out. Won't happen if callers are obeying
2157 * the restrictions correctly. Can happen for free calls
2158 * on a completely full ag.
2159 */
Yingping Lud210a282006-06-09 14:55:18 +10002160 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002161 if (flags & XFS_ALLOC_FLAG_FREEING)
2162 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002163 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /*
2166 * Put each allocated block on the list.
2167 */
2168 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002169 error = xfs_alloc_put_freelist(tp, agbp,
2170 agflbp, bno, 0);
2171 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002172 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002175 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 args->agbp = agbp;
2177 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002178
2179out_agflbp_relse:
2180 xfs_trans_brelse(tp, agflbp);
2181out_agbp_relse:
2182 if (agbp)
2183 xfs_trans_brelse(tp, agbp);
2184out_no_agbp:
2185 args->agbp = NULL;
2186 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189/*
2190 * Get a block from the freelist.
2191 * Returns with the buffer for the block gotten.
2192 */
2193int /* error */
2194xfs_alloc_get_freelist(
2195 xfs_trans_t *tp, /* transaction pointer */
2196 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002197 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2198 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2202 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002203 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int error;
David Chinner92821e22007-05-24 15:26:31 +10002205 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002206 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 xfs_perag_t *pag; /* per allocation group data */
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * Freelist is empty, give up.
2211 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002212 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (!agf->agf_flcount) {
2214 *bnop = NULLAGBLOCK;
2215 return 0;
2216 }
2217 /*
2218 * Read the array of free blocks.
2219 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002220 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2221 &agflbp);
2222 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002224
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Get the block number and update the data structures.
2228 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002229 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2230 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002231 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002233 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002235
2236 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002237 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 xfs_trans_agflist_delta(tp, -1);
2239 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002240 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002241
2242 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2243 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002244 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002245 pag->pagf_btreeblks++;
2246 logflags |= XFS_AGF_BTREEBLKS;
2247 }
2248
David Chinner92821e22007-05-24 15:26:31 +10002249 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 *bnop = bno;
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return 0;
2253}
2254
2255/*
2256 * Log the given fields from the agf structure.
2257 */
2258void
2259xfs_alloc_log_agf(
2260 xfs_trans_t *tp, /* transaction pointer */
2261 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2262 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2263{
2264 int first; /* first byte offset */
2265 int last; /* last byte offset */
2266 static const short offsets[] = {
2267 offsetof(xfs_agf_t, agf_magicnum),
2268 offsetof(xfs_agf_t, agf_versionnum),
2269 offsetof(xfs_agf_t, agf_seqno),
2270 offsetof(xfs_agf_t, agf_length),
2271 offsetof(xfs_agf_t, agf_roots[0]),
2272 offsetof(xfs_agf_t, agf_levels[0]),
2273 offsetof(xfs_agf_t, agf_flfirst),
2274 offsetof(xfs_agf_t, agf_fllast),
2275 offsetof(xfs_agf_t, agf_flcount),
2276 offsetof(xfs_agf_t, agf_freeblks),
2277 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002278 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002279 offsetof(xfs_agf_t, agf_uuid),
Darrick J. Wongf32866fd2016-08-17 08:31:49 +10002280 offsetof(xfs_agf_t, agf_rmap_blocks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 sizeof(xfs_agf_t)
2282 };
2283
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002284 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2285
Dave Chinner61fe1352013-04-03 16:11:30 +11002286 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2289 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2290}
2291
2292/*
2293 * Interface for inode allocation to force the pag data to be initialized.
2294 */
2295int /* error */
2296xfs_alloc_pagf_init(
2297 xfs_mount_t *mp, /* file system mount structure */
2298 xfs_trans_t *tp, /* transaction pointer */
2299 xfs_agnumber_t agno, /* allocation group number */
2300 int flags) /* XFS_ALLOC_FLAGS_... */
2301{
2302 xfs_buf_t *bp;
2303 int error;
2304
2305 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2306 return error;
2307 if (bp)
2308 xfs_trans_brelse(tp, bp);
2309 return 0;
2310}
2311
2312/*
2313 * Put the block on the freelist for the allocation group.
2314 */
2315int /* error */
2316xfs_alloc_put_freelist(
2317 xfs_trans_t *tp, /* transaction pointer */
2318 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2319 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002320 xfs_agblock_t bno, /* block being freed */
2321 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002324 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 int error;
David Chinner92821e22007-05-24 15:26:31 +10002326 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 xfs_mount_t *mp; /* mount structure */
2328 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002329 __be32 *agfl_bno;
2330 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 agf = XFS_BUF_TO_AGF(agbp);
2333 mp = tp->t_mountp;
2334
2335 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002336 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002338 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002339 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002341
2342 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002343 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 xfs_trans_agflist_delta(tp, 1);
2345 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002346
2347 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2348 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002349 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002350 pag->pagf_btreeblks--;
2351 logflags |= XFS_AGF_BTREEBLKS;
2352 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002353 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002354
David Chinner92821e22007-05-24 15:26:31 +10002355 xfs_alloc_log_agf(tp, agbp, logflags);
2356
Christoph Hellwig16259e72005-11-02 15:11:25 +11002357 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002358
2359 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2360 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002361 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002362 startoff = (char *)blockp - (char *)agflbp->b_addr;
2363
David Chinner92821e22007-05-24 15:26:31 +10002364 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002365
Dave Chinner61fe1352013-04-03 16:11:30 +11002366 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002367 xfs_trans_log_buf(tp, agflbp, startoff,
2368 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return 0;
2370}
2371
Dave Chinner4e0e6042013-04-03 16:11:13 +11002372static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002373xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002374 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002375 struct xfs_buf *bp)
2376 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002377 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002378
Brian Fostera45086e2015-10-12 15:59:25 +11002379 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2380 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002381 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002382 if (!xfs_log_check_lsn(mp,
2383 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2384 return false;
2385 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002386
Dave Chinner4e0e6042013-04-03 16:11:13 +11002387 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2388 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2389 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2390 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2391 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2392 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2393 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002394
Eric Sandeene1b05722014-09-09 11:47:24 +10002395 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2396 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2397 return false;
2398
Darrick J. Wongb8704942016-08-03 11:30:32 +10002399 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2400 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2401 return false;
2402
Dave Chinner5d5f5272012-11-14 17:44:56 +11002403 /*
2404 * during growfs operations, the perag is not fully initialised,
2405 * so we can't use it for any useful checking. growfs ensures we can't
2406 * use it by using uncached buffers that don't have the perag attached
2407 * so we can detect and avoid this problem.
2408 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002409 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2410 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002411
Dave Chinner4e0e6042013-04-03 16:11:13 +11002412 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2413 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2414 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002415
Dave Chinner4e0e6042013-04-03 16:11:13 +11002416 return true;;
2417
Dave Chinner612cfbf2012-11-14 17:52:32 +11002418}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002419
Dave Chinner1813dd62012-11-14 17:54:40 +11002420static void
2421xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002422 struct xfs_buf *bp)
2423{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002424 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002425
Eric Sandeence5028c2014-02-27 15:23:10 +11002426 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2427 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002428 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002429 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2430 XFS_ERRTAG_ALLOC_READ_AGF,
2431 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002432 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002433
2434 if (bp->b_error)
2435 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002436}
2437
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002438static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002439xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002440 struct xfs_buf *bp)
2441{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002442 struct xfs_mount *mp = bp->b_target->bt_mount;
2443 struct xfs_buf_log_item *bip = bp->b_fspriv;
2444
2445 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002446 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002447 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002448 return;
2449 }
2450
2451 if (!xfs_sb_version_hascrc(&mp->m_sb))
2452 return;
2453
2454 if (bip)
2455 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2456
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002457 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002458}
2459
Dave Chinner1813dd62012-11-14 17:54:40 +11002460const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002461 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002462 .verify_read = xfs_agf_read_verify,
2463 .verify_write = xfs_agf_write_verify,
2464};
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466/*
2467 * Read in the allocation group header (free/alloc section).
2468 */
2469int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002470xfs_read_agf(
2471 struct xfs_mount *mp, /* mount point structure */
2472 struct xfs_trans *tp, /* transaction pointer */
2473 xfs_agnumber_t agno, /* allocation group number */
2474 int flags, /* XFS_BUF_ */
2475 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int error;
2478
Dave Chinnerd1230312013-11-01 15:27:19 +11002479 trace_xfs_read_agf(mp, agno);
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 ASSERT(agno != NULLAGNUMBER);
2482 error = xfs_trans_read_buf(
2483 mp, tp, mp->m_ddev_targp,
2484 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002485 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (error)
2487 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002488 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002490
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002491 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002492 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002493 return 0;
2494}
2495
2496/*
2497 * Read in the allocation group header (free/alloc section).
2498 */
2499int /* error */
2500xfs_alloc_read_agf(
2501 struct xfs_mount *mp, /* mount point structure */
2502 struct xfs_trans *tp, /* transaction pointer */
2503 xfs_agnumber_t agno, /* allocation group number */
2504 int flags, /* XFS_ALLOC_FLAG_... */
2505 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2506{
2507 struct xfs_agf *agf; /* ag freelist header */
2508 struct xfs_perag *pag; /* per allocation group data */
2509 int error;
2510
Dave Chinnerd1230312013-11-01 15:27:19 +11002511 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002512
Dave Chinnerd1230312013-11-01 15:27:19 +11002513 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002514 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002515 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002516 bpp);
2517 if (error)
2518 return error;
2519 if (!*bpp)
2520 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002521 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002522
2523 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002524 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002526 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002527 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002528 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2529 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002531 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002533 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002534 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2535 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002536 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002537 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002538 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 pag->pagf_init = 1;
2540 }
2541#ifdef DEBUG
2542 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002543 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002544 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002545 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2546 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002548 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002550 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002553 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return 0;
2555}
2556
2557/*
2558 * Allocate an extent (variable-size).
2559 * Depending on the allocation type, we either look in a single allocation
2560 * group or loop over the allocation groups to find the result.
2561 */
2562int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002563xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 xfs_alloc_arg_t *args) /* allocation argument structure */
2565{
2566 xfs_agblock_t agsize; /* allocation group size */
2567 int error;
2568 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 xfs_extlen_t minleft;/* minimum left value, temp copy */
2570 xfs_mount_t *mp; /* mount structure pointer */
2571 xfs_agnumber_t sagno; /* starting allocation group number */
2572 xfs_alloctype_t type; /* input allocation type */
2573 int bump_rotor = 0;
2574 int no_min = 0;
2575 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2576
2577 mp = args->mp;
2578 type = args->otype = args->type;
2579 args->agbno = NULLAGBLOCK;
2580 /*
2581 * Just fix this up, for the case where the last a.g. is shorter
2582 * (or there's only one a.g.) and the caller couldn't easily figure
2583 * that out (xfs_bmap_alloc).
2584 */
2585 agsize = mp->m_sb.sb_agblocks;
2586 if (args->maxlen > agsize)
2587 args->maxlen = agsize;
2588 if (args->alignment == 0)
2589 args->alignment = 1;
2590 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2591 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2592 ASSERT(args->minlen <= args->maxlen);
2593 ASSERT(args->minlen <= agsize);
2594 ASSERT(args->mod < args->prod);
2595 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2596 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2597 args->minlen > args->maxlen || args->minlen > agsize ||
2598 args->mod >= args->prod) {
2599 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002600 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602 }
2603 minleft = args->minleft;
2604
2605 switch (type) {
2606 case XFS_ALLOCTYPE_THIS_AG:
2607 case XFS_ALLOCTYPE_NEAR_BNO:
2608 case XFS_ALLOCTYPE_THIS_BNO:
2609 /*
2610 * These three force us into a single a.g.
2611 */
2612 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002613 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 args->minleft = 0;
2615 error = xfs_alloc_fix_freelist(args, 0);
2616 args->minleft = minleft;
2617 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002618 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 goto error0;
2620 }
2621 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002622 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 break;
2624 }
2625 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2626 if ((error = xfs_alloc_ag_vextent(args)))
2627 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 break;
2629 case XFS_ALLOCTYPE_START_BNO:
2630 /*
2631 * Try near allocation first, then anywhere-in-ag after
2632 * the first a.g. fails.
2633 */
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002634 if ((args->userdata & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2636 args->fsbno = XFS_AGB_TO_FSB(mp,
2637 ((mp->m_agfrotor / rotorstep) %
2638 mp->m_sb.sb_agcount), 0);
2639 bump_rotor = 1;
2640 }
2641 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2642 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2643 /* FALLTHROUGH */
2644 case XFS_ALLOCTYPE_ANY_AG:
2645 case XFS_ALLOCTYPE_START_AG:
2646 case XFS_ALLOCTYPE_FIRST_AG:
2647 /*
2648 * Rotate through the allocation groups looking for a winner.
2649 */
2650 if (type == XFS_ALLOCTYPE_ANY_AG) {
2651 /*
2652 * Start with the last place we left off.
2653 */
2654 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2655 mp->m_sb.sb_agcount;
2656 args->type = XFS_ALLOCTYPE_THIS_AG;
2657 flags = XFS_ALLOC_FLAG_TRYLOCK;
2658 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2659 /*
2660 * Start with allocation group given by bno.
2661 */
2662 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2663 args->type = XFS_ALLOCTYPE_THIS_AG;
2664 sagno = 0;
2665 flags = 0;
2666 } else {
2667 if (type == XFS_ALLOCTYPE_START_AG)
2668 args->type = XFS_ALLOCTYPE_THIS_AG;
2669 /*
2670 * Start with the given allocation group.
2671 */
2672 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2673 flags = XFS_ALLOC_FLAG_TRYLOCK;
2674 }
2675 /*
2676 * Loop over allocation groups twice; first time with
2677 * trylock set, second time without.
2678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002680 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (no_min) args->minleft = 0;
2682 error = xfs_alloc_fix_freelist(args, flags);
2683 args->minleft = minleft;
2684 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002685 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto error0;
2687 }
2688 /*
2689 * If we get a buffer back then the allocation will fly.
2690 */
2691 if (args->agbp) {
2692 if ((error = xfs_alloc_ag_vextent(args)))
2693 goto error0;
2694 break;
2695 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002696
2697 trace_xfs_alloc_vextent_loopfailed(args);
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * Didn't work, figure out the next iteration.
2701 */
2702 if (args->agno == sagno &&
2703 type == XFS_ALLOCTYPE_START_BNO)
2704 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002705 /*
2706 * For the first allocation, we can try any AG to get
2707 * space. However, if we already have allocated a
2708 * block, we don't want to try AGs whose number is below
2709 * sagno. Otherwise, we may end up with out-of-order
2710 * locking of AGF, which might cause deadlock.
2711 */
2712 if (++(args->agno) == mp->m_sb.sb_agcount) {
2713 if (args->firstblock != NULLFSBLOCK)
2714 args->agno = sagno;
2715 else
2716 args->agno = 0;
2717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /*
2719 * Reached the starting a.g., must either be done
2720 * or switch to non-trylock mode.
2721 */
2722 if (args->agno == sagno) {
2723 if (no_min == 1) {
2724 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002725 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 break;
2727 }
2728 if (flags == 0) {
2729 no_min = 1;
2730 } else {
2731 flags = 0;
2732 if (type == XFS_ALLOCTYPE_START_BNO) {
2733 args->agbno = XFS_FSB_TO_AGBNO(mp,
2734 args->fsbno);
2735 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2736 }
2737 }
2738 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002739 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2742 if (args->agno == sagno)
2743 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2744 (mp->m_sb.sb_agcount * rotorstep);
2745 else
2746 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2747 (mp->m_sb.sb_agcount * rotorstep);
2748 }
2749 break;
2750 default:
2751 ASSERT(0);
2752 /* NOTREACHED */
2753 }
2754 if (args->agbno == NULLAGBLOCK)
2755 args->fsbno = NULLFSBLOCK;
2756 else {
2757 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2758#ifdef DEBUG
2759 ASSERT(args->len >= args->minlen);
2760 ASSERT(args->len <= args->maxlen);
2761 ASSERT(args->agbno % args->alignment == 0);
2762 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2763 args->len);
2764#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002765
2766 /* Zero the extent if we were asked to do so */
2767 if (args->userdata & XFS_ALLOC_USERDATA_ZERO) {
2768 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2769 if (error)
2770 goto error0;
2771 }
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002774 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002777 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return error;
2779}
2780
Dave Chinner4d89e202016-06-21 11:53:28 +10002781/* Ensure that the freelist is at full capacity. */
2782int
2783xfs_free_extent_fix_freelist(
2784 struct xfs_trans *tp,
2785 xfs_agnumber_t agno,
2786 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Dave Chinner4d89e202016-06-21 11:53:28 +10002788 struct xfs_alloc_arg args;
2789 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Dave Chinner4d89e202016-06-21 11:53:28 +10002791 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 args.tp = tp;
2793 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002794 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002795
2796 /*
2797 * validate that the block number is legal - the enables us to detect
2798 * and handle a silent filesystem corruption rather than crashing.
2799 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002800 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002801 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002802
Dave Chinnera862e0f2010-01-11 11:47:41 +00002803 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002804 ASSERT(args.pag);
2805
2806 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2807 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002808 goto out;
2809
2810 *agbp = args.agbp;
2811out:
2812 xfs_perag_put(args.pag);
2813 return error;
2814}
2815
2816/*
2817 * Free an extent.
2818 * Just break up the extent address and hand off to xfs_free_ag_extent
2819 * after fixing up the freelist.
2820 */
2821int /* error */
2822xfs_free_extent(
2823 struct xfs_trans *tp, /* transaction pointer */
2824 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002825 xfs_extlen_t len, /* length of extent */
2826 struct xfs_owner_info *oinfo) /* extent owner */
Dave Chinner4d89e202016-06-21 11:53:28 +10002827{
2828 struct xfs_mount *mp = tp->t_mountp;
2829 struct xfs_buf *agbp;
2830 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2831 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2832 int error;
2833
2834 ASSERT(len != 0);
2835
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002836 if (XFS_TEST_ERROR(false, mp,
2837 XFS_ERRTAG_FREE_EXTENT,
2838 XFS_RANDOM_FREE_EXTENT))
2839 return -EIO;
2840
Dave Chinner4d89e202016-06-21 11:53:28 +10002841 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2842 if (error)
2843 return error;
2844
2845 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002846
2847 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002848 XFS_WANT_CORRUPTED_GOTO(mp,
2849 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2850 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002851
Darrick J. Wong340785c2016-08-03 11:33:42 +10002852 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, 0);
Dave Chinner4d89e202016-06-21 11:53:28 +10002853 if (error)
2854 goto err;
2855
2856 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2857 return 0;
2858
2859err:
2860 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return error;
2862}