blob: 43c7e53a6a8ee85a345730c49e097355593eabfb [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000032#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110034#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110036#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110037#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Dave Chinnerc999a222012-03-22 05:15:07 +000040struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
43
44#define XFSA_FIXUP_BNO_OK 1
45#define XFSA_FIXUP_CNT_OK 2
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
48STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
49STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000051 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Darrick J. Wong80180262016-08-03 11:31:47 +100053xfs_extlen_t
54xfs_prealloc_blocks(
55 struct xfs_mount *mp)
56{
57 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
58 return XFS_RMAP_BLOCK(mp) + 1;
59 if (xfs_sb_version_hasfinobt(&mp->m_sb))
60 return XFS_FIBT_BLOCK(mp) + 1;
61 return XFS_IBT_BLOCK(mp) + 1;
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110065 * Lookup the record equal to [bno, len] in the btree given by cur.
66 */
67STATIC int /* error */
68xfs_alloc_lookup_eq(
69 struct xfs_btree_cur *cur, /* btree cursor */
70 xfs_agblock_t bno, /* starting block of extent */
71 xfs_extlen_t len, /* length of extent */
72 int *stat) /* success/failure */
73{
74 cur->bc_rec.a.ar_startblock = bno;
75 cur->bc_rec.a.ar_blockcount = len;
76 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
77}
78
79/*
80 * Lookup the first record greater than or equal to [bno, len]
81 * in the btree given by cur.
82 */
Dave Chinnera66d6362012-03-22 05:15:12 +000083int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110084xfs_alloc_lookup_ge(
85 struct xfs_btree_cur *cur, /* btree cursor */
86 xfs_agblock_t bno, /* starting block of extent */
87 xfs_extlen_t len, /* length of extent */
88 int *stat) /* success/failure */
89{
90 cur->bc_rec.a.ar_startblock = bno;
91 cur->bc_rec.a.ar_blockcount = len;
92 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
93}
94
95/*
96 * Lookup the first record less than or equal to [bno, len]
97 * in the btree given by cur.
98 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +100099static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100100xfs_alloc_lookup_le(
101 struct xfs_btree_cur *cur, /* btree cursor */
102 xfs_agblock_t bno, /* starting block of extent */
103 xfs_extlen_t len, /* length of extent */
104 int *stat) /* success/failure */
105{
106 cur->bc_rec.a.ar_startblock = bno;
107 cur->bc_rec.a.ar_blockcount = len;
108 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
109}
110
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100111/*
112 * Update the record referred to by cur to the value given
113 * by [bno, len].
114 * This either works (return 0) or gets an EFSCORRUPTED error.
115 */
116STATIC int /* error */
117xfs_alloc_update(
118 struct xfs_btree_cur *cur, /* btree cursor */
119 xfs_agblock_t bno, /* starting block of extent */
120 xfs_extlen_t len) /* length of extent */
121{
122 union xfs_btree_rec rec;
123
124 rec.alloc.ar_startblock = cpu_to_be32(bno);
125 rec.alloc.ar_blockcount = cpu_to_be32(len);
126 return xfs_btree_update(cur, &rec);
127}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100128
129/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100130 * Get the data from the pointed-to record.
131 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000132int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100133xfs_alloc_get_rec(
134 struct xfs_btree_cur *cur, /* btree cursor */
135 xfs_agblock_t *bno, /* output: starting block of extent */
136 xfs_extlen_t *len, /* output: length of extent */
137 int *stat) /* output: success/failure */
138{
139 union xfs_btree_rec *rec;
140 int error;
141
142 error = xfs_btree_get_rec(cur, &rec, stat);
143 if (!error && *stat == 1) {
144 *bno = be32_to_cpu(rec->alloc.ar_startblock);
145 *len = be32_to_cpu(rec->alloc.ar_blockcount);
146 }
147 return error;
148}
149
150/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * Compute aligned version of the found extent.
152 * Takes alignment and min length into account.
153 */
David Chinner12375c82008-04-10 12:21:32 +1000154STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000156 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 xfs_agblock_t foundbno, /* starting block in found extent */
158 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 xfs_agblock_t *resbno, /* result block number */
160 xfs_extlen_t *reslen) /* result length */
161{
162 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000164 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Christoph Hellwige26f0502011-04-24 19:06:15 +0000166 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000167 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000168
Brian Fosterbfe46d42015-05-29 08:53:00 +1000169 /*
170 * If we have a largish extent that happens to start before min_agbno,
171 * see if we can shift it into range...
172 */
173 if (bno < args->min_agbno && bno + len > args->min_agbno) {
174 diff = args->min_agbno - bno;
175 if (len > diff) {
176 bno += diff;
177 len -= diff;
178 }
179 }
180
Christoph Hellwige26f0502011-04-24 19:06:15 +0000181 if (args->alignment > 1 && len >= args->minlen) {
182 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000183
184 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000185
186 *resbno = aligned_bno;
187 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000189 *resbno = bno;
190 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * Compute best start block and diff for "near" allocations.
196 * freelen >= wantlen already checked by caller.
197 */
198STATIC xfs_extlen_t /* difference value (absolute) */
199xfs_alloc_compute_diff(
200 xfs_agblock_t wantbno, /* target starting block */
201 xfs_extlen_t wantlen, /* target length */
202 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200203 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 xfs_agblock_t freebno, /* freespace's starting block */
205 xfs_extlen_t freelen, /* freespace's length */
206 xfs_agblock_t *newbnop) /* result: best start block from free */
207{
208 xfs_agblock_t freeend; /* end of freespace extent */
209 xfs_agblock_t newbno1; /* return block number */
210 xfs_agblock_t newbno2; /* other new block number */
211 xfs_extlen_t newlen1=0; /* length with newbno1 */
212 xfs_extlen_t newlen2=0; /* length with newbno2 */
213 xfs_agblock_t wantend; /* end of target extent */
214
215 ASSERT(freelen >= wantlen);
216 freeend = freebno + freelen;
217 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200218 /*
219 * We want to allocate from the start of a free extent if it is past
220 * the desired block or if we are allocating user data and the free
221 * extent is before desired block. The second case is there to allow
222 * for contiguous allocation from the remaining free space if the file
223 * grows in the short term.
224 */
225 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
227 newbno1 = NULLAGBLOCK;
228 } else if (freeend >= wantend && alignment > 1) {
229 newbno1 = roundup(wantbno, alignment);
230 newbno2 = newbno1 - alignment;
231 if (newbno1 >= freeend)
232 newbno1 = NULLAGBLOCK;
233 else
234 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
235 if (newbno2 < freebno)
236 newbno2 = NULLAGBLOCK;
237 else
238 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
239 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
240 if (newlen1 < newlen2 ||
241 (newlen1 == newlen2 &&
242 XFS_ABSDIFF(newbno1, wantbno) >
243 XFS_ABSDIFF(newbno2, wantbno)))
244 newbno1 = newbno2;
245 } else if (newbno2 != NULLAGBLOCK)
246 newbno1 = newbno2;
247 } else if (freeend >= wantend) {
248 newbno1 = wantbno;
249 } else if (alignment > 1) {
250 newbno1 = roundup(freeend - wantlen, alignment);
251 if (newbno1 > freeend - wantlen &&
252 newbno1 - alignment >= freebno)
253 newbno1 -= alignment;
254 else if (newbno1 >= freeend)
255 newbno1 = NULLAGBLOCK;
256 } else
257 newbno1 = freeend - wantlen;
258 *newbnop = newbno1;
259 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
260}
261
262/*
263 * Fix up the length, based on mod and prod.
264 * len should be k * prod + mod for some k.
265 * If len is too small it is returned unchanged.
266 * If len hits maxlen it is left alone.
267 */
268STATIC void
269xfs_alloc_fix_len(
270 xfs_alloc_arg_t *args) /* allocation argument structure */
271{
272 xfs_extlen_t k;
273 xfs_extlen_t rlen;
274
275 ASSERT(args->mod < args->prod);
276 rlen = args->len;
277 ASSERT(rlen >= args->minlen);
278 ASSERT(rlen <= args->maxlen);
279 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
280 (args->mod == 0 && rlen < args->prod))
281 return;
282 k = rlen % args->prod;
283 if (k == args->mod)
284 return;
Jan Kara30265112014-06-06 16:06:37 +1000285 if (k > args->mod)
286 rlen = rlen - (k - args->mod);
287 else
288 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100289 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000290 if ((int)rlen < (int)args->minlen)
291 return;
292 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
293 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 args->len = rlen;
295}
296
297/*
298 * Fix up length if there is too little space left in the a.g.
299 * Return 1 if ok, 0 if too little, should give up.
300 */
301STATIC int
302xfs_alloc_fix_minleft(
303 xfs_alloc_arg_t *args) /* allocation argument structure */
304{
305 xfs_agf_t *agf; /* a.g. freelist header */
306 int diff; /* free space difference */
307
308 if (args->minleft == 0)
309 return 1;
310 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100311 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 - args->len - args->minleft;
313 if (diff >= 0)
314 return 1;
315 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100316 /* casts to (int) catch length underflows */
317 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 1;
319 args->agbno = NULLAGBLOCK;
320 return 0;
321}
322
323/*
324 * Update the two btrees, logically removing from freespace the extent
325 * starting at rbno, rlen blocks. The extent is contained within the
326 * actual (current) free extent fbno for flen blocks.
327 * Flags are passed in indicating whether the cursors are set to the
328 * relevant records.
329 */
330STATIC int /* error code */
331xfs_alloc_fixup_trees(
332 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
333 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
334 xfs_agblock_t fbno, /* starting block of free extent */
335 xfs_extlen_t flen, /* length of free extent */
336 xfs_agblock_t rbno, /* starting block of returned extent */
337 xfs_extlen_t rlen, /* length of returned extent */
338 int flags) /* flags, XFSA_FIXUP_... */
339{
340 int error; /* error code */
341 int i; /* operation results */
342 xfs_agblock_t nfbno1; /* first new free startblock */
343 xfs_agblock_t nfbno2; /* second new free startblock */
344 xfs_extlen_t nflen1=0; /* first new free length */
345 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100346 struct xfs_mount *mp;
347
348 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * Look up the record in the by-size tree if necessary.
352 */
353 if (flags & XFSA_FIXUP_CNT_OK) {
354#ifdef DEBUG
355 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
356 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100357 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 i == 1 && nfbno1 == fbno && nflen1 == flen);
359#endif
360 } else {
361 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
362 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100363 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 /*
366 * Look up the record in the by-block tree if necessary.
367 */
368 if (flags & XFSA_FIXUP_BNO_OK) {
369#ifdef DEBUG
370 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
371 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100372 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 i == 1 && nfbno1 == fbno && nflen1 == flen);
374#endif
375 } else {
376 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
377 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100378 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100381#ifdef DEBUG
382 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
383 struct xfs_btree_block *bnoblock;
384 struct xfs_btree_block *cntblock;
385
386 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
387 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
388
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100389 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100390 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Deal with all four cases: the allocated record is contained
396 * within the freespace record, so we can have new freespace
397 * at either (or both) end, or no freespace remaining.
398 */
399 if (rbno == fbno && rlen == flen)
400 nfbno1 = nfbno2 = NULLAGBLOCK;
401 else if (rbno == fbno) {
402 nfbno1 = rbno + rlen;
403 nflen1 = flen - rlen;
404 nfbno2 = NULLAGBLOCK;
405 } else if (rbno + rlen == fbno + flen) {
406 nfbno1 = fbno;
407 nflen1 = flen - rlen;
408 nfbno2 = NULLAGBLOCK;
409 } else {
410 nfbno1 = fbno;
411 nflen1 = rbno - fbno;
412 nfbno2 = rbno + rlen;
413 nflen2 = (fbno + flen) - nfbno2;
414 }
415 /*
416 * Delete the entry from the by-size btree.
417 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100418 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100420 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
422 * Add new by-size btree entry(s).
423 */
424 if (nfbno1 != NULLAGBLOCK) {
425 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
426 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100427 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100428 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100430 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 if (nfbno2 != NULLAGBLOCK) {
433 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
434 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100435 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100436 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100438 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 /*
441 * Fix up the by-block btree entry(s).
442 */
443 if (nfbno1 == NULLAGBLOCK) {
444 /*
445 * No remaining freespace, just delete the by-block tree entry.
446 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100447 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100449 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
451 /*
452 * Update the by-block entry to start later|be shorter.
453 */
454 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
455 return error;
456 }
457 if (nfbno2 != NULLAGBLOCK) {
458 /*
459 * 2 resulting free entries, need to add one.
460 */
461 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
462 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100463 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100464 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100466 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 return 0;
469}
470
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100471static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100472xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100473 struct xfs_buf *bp)
474{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100475 struct xfs_mount *mp = bp->b_target->bt_mount;
476 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100477 int i;
478
Eric Sandeence748ea2015-07-29 11:53:31 +1000479 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100480 return false;
481 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
482 return false;
483 /*
484 * during growfs operations, the perag is not fully initialised,
485 * so we can't use it for any useful checking. growfs ensures we can't
486 * use it by using uncached buffers that don't have the perag attached
487 * so we can detect and avoid this problem.
488 */
489 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
490 return false;
491
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100492 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100493 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100494 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100495 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100496 }
Brian Fostera45086e2015-10-12 15:59:25 +1100497
498 return xfs_log_check_lsn(mp,
499 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100500}
501
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100502static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100503xfs_agfl_read_verify(
504 struct xfs_buf *bp)
505{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100506 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100507
508 /*
509 * There is no verification of non-crc AGFLs because mkfs does not
510 * initialise the AGFL to zero or NULL. Hence the only valid part of the
511 * AGFL is what the AGF says is active. We can't get to the AGF, so we
512 * can't verify just those entries are valid.
513 */
514 if (!xfs_sb_version_hascrc(&mp->m_sb))
515 return;
516
Eric Sandeence5028c2014-02-27 15:23:10 +1100517 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000518 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100519 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000520 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100521
522 if (bp->b_error)
523 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100524}
525
526static void
527xfs_agfl_write_verify(
528 struct xfs_buf *bp)
529{
530 struct xfs_mount *mp = bp->b_target->bt_mount;
531 struct xfs_buf_log_item *bip = bp->b_fspriv;
532
533 /* no verification of non-crc AGFLs */
534 if (!xfs_sb_version_hascrc(&mp->m_sb))
535 return;
536
537 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000538 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100539 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100540 return;
541 }
542
543 if (bip)
544 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
545
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100546 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100547}
548
Dave Chinner1813dd62012-11-14 17:54:40 +1100549const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100550 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100551 .verify_read = xfs_agfl_read_verify,
552 .verify_write = xfs_agfl_write_verify,
553};
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/*
556 * Read in the allocation group free block array.
557 */
558STATIC int /* error */
559xfs_alloc_read_agfl(
560 xfs_mount_t *mp, /* mount point structure */
561 xfs_trans_t *tp, /* transaction pointer */
562 xfs_agnumber_t agno, /* allocation group number */
563 xfs_buf_t **bpp) /* buffer for the ag free block array */
564{
565 xfs_buf_t *bp; /* return value */
566 int error;
567
568 ASSERT(agno != NULLAGNUMBER);
569 error = xfs_trans_read_buf(
570 mp, tp, mp->m_ddev_targp,
571 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100572 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (error)
574 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000575 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *bpp = bp;
577 return 0;
578}
579
Christoph Hellwigecb69282011-03-04 12:59:55 +0000580STATIC int
581xfs_alloc_update_counters(
582 struct xfs_trans *tp,
583 struct xfs_perag *pag,
584 struct xfs_buf *agbp,
585 long len)
586{
587 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
588
589 pag->pagf_freeblks += len;
590 be32_add_cpu(&agf->agf_freeblks, len);
591
592 xfs_trans_agblocks_delta(tp, len);
593 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
594 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000595 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000596
597 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
598 return 0;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * Allocation group level functions.
603 */
604
605/*
606 * Allocate a variable extent in the allocation group agno.
607 * Type and bno are used to determine where in the allocation group the
608 * extent will start.
609 * Extent's length (returned in *len) will be between minlen and maxlen,
610 * and of the form k * prod + mod unless there's nothing that large.
611 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
612 */
613STATIC int /* error */
614xfs_alloc_ag_vextent(
615 xfs_alloc_arg_t *args) /* argument structure for allocation */
616{
617 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 ASSERT(args->minlen > 0);
620 ASSERT(args->maxlen > 0);
621 ASSERT(args->minlen <= args->maxlen);
622 ASSERT(args->mod < args->prod);
623 ASSERT(args->alignment > 0);
624 /*
625 * Branch to correct routine based on the type.
626 */
627 args->wasfromfl = 0;
628 switch (args->type) {
629 case XFS_ALLOCTYPE_THIS_AG:
630 error = xfs_alloc_ag_vextent_size(args);
631 break;
632 case XFS_ALLOCTYPE_NEAR_BNO:
633 error = xfs_alloc_ag_vextent_near(args);
634 break;
635 case XFS_ALLOCTYPE_THIS_BNO:
636 error = xfs_alloc_ag_vextent_exact(args);
637 break;
638 default:
639 ASSERT(0);
640 /* NOTREACHED */
641 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000642
643 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Christoph Hellwigecb69282011-03-04 12:59:55 +0000646 ASSERT(args->len >= args->minlen);
647 ASSERT(args->len <= args->maxlen);
648 ASSERT(!args->wasfromfl || !args->isfl);
649 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Christoph Hellwigecb69282011-03-04 12:59:55 +0000651 if (!args->wasfromfl) {
652 error = xfs_alloc_update_counters(args->tp, args->pag,
653 args->agbp,
654 -((long)(args->len)));
655 if (error)
656 return error;
657
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000658 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000659 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000661
662 if (!args->isfl) {
663 xfs_trans_mod_sb(args->tp, args->wasdel ?
664 XFS_TRANS_SB_RES_FDBLOCKS :
665 XFS_TRANS_SB_FDBLOCKS,
666 -((long)(args->len)));
667 }
668
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100669 XFS_STATS_INC(args->mp, xs_allocx);
670 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000671 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
675 * Allocate a variable extent at exactly agno/bno.
676 * Extent's length (returned in *len) will be between minlen and maxlen,
677 * and of the form k * prod + mod unless there's nothing that large.
678 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
679 */
680STATIC int /* error */
681xfs_alloc_ag_vextent_exact(
682 xfs_alloc_arg_t *args) /* allocation argument structure */
683{
684 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
685 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int error;
687 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000689 xfs_agblock_t tbno; /* start block of trimmed extent */
690 xfs_extlen_t tlen; /* length of trimmed extent */
691 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
697 * Allocate/initialize a cursor for the by-number freespace btree.
698 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100699 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000700 args->agno, XFS_BTNUM_BNO);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
704 * Look for the closest free block <= bno, it must contain bno
705 * if any free block does.
706 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000707 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
708 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000710 if (!i)
711 goto not_found;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * Grab the freespace record.
715 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000716 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100719 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000723 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000725 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000726
727 /*
728 * Give up if the start of the extent is busy, or the freespace isn't
729 * long enough for the minimum request.
730 */
731 if (tbno > args->agbno)
732 goto not_found;
733 if (tlen < args->minlen)
734 goto not_found;
735 tend = tbno + tlen;
736 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000737 goto not_found;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * End of extent will be smaller of the freespace end and the
741 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000742 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * Fix the length according to mod and prod if given.
744 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000745 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
746 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000748 if (!xfs_alloc_fix_minleft(args))
749 goto not_found;
750
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000751 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000754 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * Allocate/initialize a cursor for the by-size btree.
756 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100757 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
758 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100760 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000761 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
762 args->len, XFSA_FIXUP_BNO_OK);
763 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
765 goto error0;
766 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
769 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000772 trace_xfs_alloc_exact_done(args);
773 return 0;
774
775not_found:
776 /* Didn't find it, return null. */
777 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
778 args->agbno = NULLAGBLOCK;
779 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781
782error0:
783 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000784 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return error;
786}
787
788/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000789 * Search the btree in a given direction via the search cursor and compare
790 * the records found against the good extent we've already found.
791 */
792STATIC int
793xfs_alloc_find_best_extent(
794 struct xfs_alloc_arg *args, /* allocation argument structure */
795 struct xfs_btree_cur **gcur, /* good cursor */
796 struct xfs_btree_cur **scur, /* searching cursor */
797 xfs_agblock_t gdiff, /* difference for search comparison */
798 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000799 xfs_extlen_t *slen, /* extent length */
800 xfs_agblock_t *sbnoa, /* aligned extent found by search */
801 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000802 int dir) /* 0 = search right, 1 = search left */
803{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000804 xfs_agblock_t new;
805 xfs_agblock_t sdiff;
806 int error;
807 int i;
808
809 /* The good extent is perfect, no need to search. */
810 if (!gdiff)
811 goto out_use_good;
812
813 /*
814 * Look until we find a better one, run out of space or run off the end.
815 */
816 do {
817 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
818 if (error)
819 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100820 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000821 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000822
823 /*
824 * The good extent is closer than this one.
825 */
826 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000827 if (*sbnoa > args->max_agbno)
828 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000829 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000830 goto out_use_good;
831 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000832 if (*sbnoa < args->min_agbno)
833 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000834 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000835 goto out_use_good;
836 }
837
838 /*
839 * Same distance, compare length and pick the best.
840 */
841 if (*slena >= args->minlen) {
842 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
843 xfs_alloc_fix_len(args);
844
845 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200846 args->alignment,
847 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000848 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000849
850 /*
851 * Choose closer size and invalidate other cursor.
852 */
853 if (sdiff < gdiff)
854 goto out_use_search;
855 goto out_use_good;
856 }
857
858 if (!dir)
859 error = xfs_btree_increment(*scur, 0, &i);
860 else
861 error = xfs_btree_decrement(*scur, 0, &i);
862 if (error)
863 goto error0;
864 } while (i);
865
866out_use_good:
867 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
868 *scur = NULL;
869 return 0;
870
871out_use_search:
872 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
873 *gcur = NULL;
874 return 0;
875
876error0:
877 /* caller invalidates cursors */
878 return error;
879}
880
881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * Allocate a variable extent near bno in the allocation group agno.
883 * Extent's length (returned in len) will be between minlen and maxlen,
884 * and of the form k * prod + mod unless there's nothing that large.
885 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
886 */
887STATIC int /* error */
888xfs_alloc_ag_vextent_near(
889 xfs_alloc_arg_t *args) /* allocation argument structure */
890{
891 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
892 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
893 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 xfs_agblock_t gtbno; /* start bno of right side entry */
895 xfs_agblock_t gtbnoa; /* aligned ... */
896 xfs_extlen_t gtdiff; /* difference to right side entry */
897 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000898 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 xfs_agblock_t gtnew; /* useful start bno of right side */
900 int error; /* error code */
901 int i; /* result code, temporary */
902 int j; /* result code, temporary */
903 xfs_agblock_t ltbno; /* start bno of left side entry */
904 xfs_agblock_t ltbnoa; /* aligned ... */
905 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000907 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xfs_agblock_t ltnew; /* useful start bno of left side */
909 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000910 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000911#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * Randomly don't execute the first algorithm.
914 */
915 int dofirst; /* set to do first algorithm */
916
Akinobu Mitaecb34032013-03-04 21:58:20 +0900917 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000919
Brian Fosterbfe46d42015-05-29 08:53:00 +1000920 /* handle unitialized agbno range so caller doesn't have to */
921 if (!args->min_agbno && !args->max_agbno)
922 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
923 ASSERT(args->min_agbno <= args->max_agbno);
924
925 /* clamp agbno to the range if it's outside */
926 if (args->agbno < args->min_agbno)
927 args->agbno = args->min_agbno;
928 if (args->agbno > args->max_agbno)
929 args->agbno = args->max_agbno;
930
Christoph Hellwige26f0502011-04-24 19:06:15 +0000931restart:
932 bno_cur_lt = NULL;
933 bno_cur_gt = NULL;
934 ltlen = 0;
935 gtlena = 0;
936 ltlena = 0;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * Get a cursor for the by-size btree.
940 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100941 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
942 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * See if there are any free extents as big as maxlen.
946 */
947 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
948 goto error0;
949 /*
950 * If none, then pick up the last entry in the tree unless the
951 * tree is empty.
952 */
953 if (!i) {
954 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
955 &ltlen, &i)))
956 goto error0;
957 if (i == 0 || ltlen == 0) {
958 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000959 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961 }
962 ASSERT(i == 1);
963 }
964 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /*
967 * First algorithm.
968 * If the requested extent is large wrt the freespaces available
969 * in this a.g., then the cursor will be pointing to a btree entry
970 * near the right edge of the tree. If it's in the last btree leaf
971 * block, then we just examine all the entries in that block
972 * that are big enough, and pick the best one.
973 * This is written as a while loop so we can break out of it,
974 * but we never loop back to the top.
975 */
976 while (xfs_btree_islastblock(cnt_cur, 0)) {
977 xfs_extlen_t bdiff;
978 int besti=0;
979 xfs_extlen_t blen=0;
980 xfs_agblock_t bnew=0;
981
Dave Chinner63d20d62013-08-12 20:49:50 +1000982#ifdef DEBUG
983 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985#endif
986 /*
987 * Start from the entry that lookup found, sequence through
988 * all larger free blocks. If we're actually pointing at a
989 * record smaller than maxlen, go to the start of this block,
990 * and skip all those smaller than minlen.
991 */
992 if (ltlen || args->alignment > 1) {
993 cnt_cur->bc_ptrs[0] = 1;
994 do {
995 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
996 &ltlen, &i)))
997 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100998 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (ltlen >= args->minlen)
1000 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001001 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 goto error0;
1003 } while (i);
1004 ASSERT(ltlen >= args->minlen);
1005 if (!i)
1006 break;
1007 }
1008 i = cnt_cur->bc_ptrs[0];
1009 for (j = 1, blen = 0, bdiff = 0;
1010 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001011 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * For each entry, decide if it's better than
1014 * the previous best entry.
1015 */
1016 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1017 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001018 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001019 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1020 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001021 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001023 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1024 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1026 xfs_alloc_fix_len(args);
1027 ASSERT(args->len >= args->minlen);
1028 if (args->len < blen)
1029 continue;
1030 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001031 args->alignment, args->userdata, ltbnoa,
1032 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ltnew != NULLAGBLOCK &&
1034 (args->len > blen || ltdiff < bdiff)) {
1035 bdiff = ltdiff;
1036 bnew = ltnew;
1037 blen = args->len;
1038 besti = cnt_cur->bc_ptrs[0];
1039 }
1040 }
1041 /*
1042 * It didn't work. We COULD be in a case where
1043 * there's a good record somewhere, so try again.
1044 */
1045 if (blen == 0)
1046 break;
1047 /*
1048 * Point at the best entry, and retrieve it again.
1049 */
1050 cnt_cur->bc_ptrs[0] = besti;
1051 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1052 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001053 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001054 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 args->len = blen;
1056 if (!xfs_alloc_fix_minleft(args)) {
1057 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001058 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060 }
1061 blen = args->len;
1062 /*
1063 * We are allocating starting at bnew for blen blocks.
1064 */
1065 args->agbno = bnew;
1066 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001067 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * Set up a cursor for the by-bno tree.
1070 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001071 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1072 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * Fix up the btree entries.
1075 */
1076 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1077 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1078 goto error0;
1079 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1080 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001081
1082 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084 }
1085 /*
1086 * Second algorithm.
1087 * Search in the by-bno tree to the left and to the right
1088 * simultaneously, until in each case we find a space big enough,
1089 * or run into the edge of the tree. When we run into the edge,
1090 * we deallocate that cursor.
1091 * If both searches succeed, we compare the two spaces and pick
1092 * the better one.
1093 * With alignment, it's possible for both to fail; the upper
1094 * level algorithm that picks allocation groups for allocations
1095 * is not supposed to do this.
1096 */
1097 /*
1098 * Allocate and initialize the cursor for the leftward search.
1099 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001100 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1101 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Lookup <= bno to find the leftward search's starting point.
1104 */
1105 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1106 goto error0;
1107 if (!i) {
1108 /*
1109 * Didn't find anything; use this cursor for the rightward
1110 * search.
1111 */
1112 bno_cur_gt = bno_cur_lt;
1113 bno_cur_lt = NULL;
1114 }
1115 /*
1116 * Found something. Duplicate the cursor for the rightward search.
1117 */
1118 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1119 goto error0;
1120 /*
1121 * Increment the cursor, so we will point at the entry just right
1122 * of the leftward entry if any, or to the leftmost entry.
1123 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001124 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto error0;
1126 if (!i) {
1127 /*
1128 * It failed, there are no rightward entries.
1129 */
1130 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1131 bno_cur_gt = NULL;
1132 }
1133 /*
1134 * Loop going left with the leftward cursor, right with the
1135 * rightward cursor, until either both directions give up or
1136 * we find an entry at least as big as minlen.
1137 */
1138 do {
1139 if (bno_cur_lt) {
1140 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1141 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001142 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001143 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1144 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001145 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001147 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001149 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 xfs_btree_del_cursor(bno_cur_lt,
1151 XFS_BTREE_NOERROR);
1152 bno_cur_lt = NULL;
1153 }
1154 }
1155 if (bno_cur_gt) {
1156 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1157 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001158 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001159 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1160 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001161 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001163 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001165 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_btree_del_cursor(bno_cur_gt,
1167 XFS_BTREE_NOERROR);
1168 bno_cur_gt = NULL;
1169 }
1170 }
1171 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Got both cursors still active, need to find better entry.
1175 */
1176 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (ltlena >= args->minlen) {
1178 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001179 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
1181 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1182 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001183 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001184 args->alignment, args->userdata, ltbnoa,
1185 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001186
1187 error = xfs_alloc_find_best_extent(args,
1188 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001189 ltdiff, &gtbno, &gtlen,
1190 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001191 0 /* search right */);
1192 } else {
1193 ASSERT(gtlena >= args->minlen);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001196 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1199 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001200 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001201 args->alignment, args->userdata, gtbnoa,
1202 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001203
1204 error = xfs_alloc_find_best_extent(args,
1205 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001206 gtdiff, &ltbno, &ltlen,
1207 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001208 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001210
1211 if (error)
1212 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * If we couldn't get anything, give up.
1217 */
1218 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001219 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1220
Christoph Hellwige26f0502011-04-24 19:06:15 +00001221 if (!forced++) {
1222 trace_xfs_alloc_near_busy(args);
1223 xfs_log_force(args->mp, XFS_LOG_SYNC);
1224 goto restart;
1225 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001226 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 args->agbno = NULLAGBLOCK;
1228 return 0;
1229 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * At this point we have selected a freespace entry, either to the
1233 * left or to the right. If it's on the right, copy all the
1234 * useful variables to the "left" set so we only have one
1235 * copy of this code.
1236 */
1237 if (bno_cur_gt) {
1238 bno_cur_lt = bno_cur_gt;
1239 bno_cur_gt = NULL;
1240 ltbno = gtbno;
1241 ltbnoa = gtbnoa;
1242 ltlen = gtlen;
1243 ltlena = gtlena;
1244 j = 1;
1245 } else
1246 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Fix up the length and compute the useful address.
1250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1252 xfs_alloc_fix_len(args);
1253 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001254 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1256 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1257 return 0;
1258 }
1259 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001260 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001261 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001263 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001264 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001265 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1269 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1270 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001271
1272 if (j)
1273 trace_xfs_alloc_near_greater(args);
1274 else
1275 trace_xfs_alloc_near_lesser(args);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1278 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1279 return 0;
1280
1281 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001282 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (cnt_cur != NULL)
1284 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1285 if (bno_cur_lt != NULL)
1286 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1287 if (bno_cur_gt != NULL)
1288 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1289 return error;
1290}
1291
1292/*
1293 * Allocate a variable extent anywhere in the allocation group agno.
1294 * Extent's length (returned in len) will be between minlen and maxlen,
1295 * and of the form k * prod + mod unless there's nothing that large.
1296 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1297 */
1298STATIC int /* error */
1299xfs_alloc_ag_vextent_size(
1300 xfs_alloc_arg_t *args) /* allocation argument structure */
1301{
1302 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1303 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1304 int error; /* error result */
1305 xfs_agblock_t fbno; /* start of found freespace */
1306 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int i; /* temp status variable */
1308 xfs_agblock_t rbno; /* returned block number */
1309 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001310 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Christoph Hellwige26f0502011-04-24 19:06:15 +00001312restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
1314 * Allocate and initialize a cursor for the by-size btree.
1315 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001316 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1317 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * Look for an entry >= maxlen+alignment-1 blocks.
1322 */
1323 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1324 args->maxlen + args->alignment - 1, &i)))
1325 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001328 * If none or we have busy extents that we cannot allocate from, then
1329 * we have to settle for a smaller extent. In the case that there are
1330 * no large extents, this will return the last entry in the tree unless
1331 * the tree is empty. In the case that there are only busy large
1332 * extents, this will return the largest small extent unless there
1333 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001335 if (!i || forced > 1) {
1336 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1337 &fbno, &flen, &i);
1338 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto error0;
1340 if (i == 0 || flen == 0) {
1341 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001342 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344 }
1345 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001346 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1347 } else {
1348 /*
1349 * Search for a non-busy extent that is large enough.
1350 * If we are at low space, don't check, or if we fall of
1351 * the end of the btree, turn off the busy check and
1352 * restart.
1353 */
1354 for (;;) {
1355 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1356 if (error)
1357 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001358 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001359
1360 xfs_alloc_compute_aligned(args, fbno, flen,
1361 &rbno, &rlen);
1362
1363 if (rlen >= args->maxlen)
1364 break;
1365
1366 error = xfs_btree_increment(cnt_cur, 0, &i);
1367 if (error)
1368 goto error0;
1369 if (i == 0) {
1370 /*
1371 * Our only valid extents must have been busy.
1372 * Make it unbusy by forcing the log out and
1373 * retrying. If we've been here before, forcing
1374 * the log isn't making the extents available,
1375 * which means they have probably been freed in
1376 * this transaction. In that case, we have to
1377 * give up on them and we'll attempt a minlen
1378 * allocation the next time around.
1379 */
1380 xfs_btree_del_cursor(cnt_cur,
1381 XFS_BTREE_NOERROR);
1382 trace_xfs_alloc_size_busy(args);
1383 if (!forced++)
1384 xfs_log_force(args->mp, XFS_LOG_SYNC);
1385 goto restart;
1386 }
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * In the first case above, we got the last entry in the
1392 * by-size btree. Now we check to see if the space hits maxlen
1393 * once aligned; if not, we search left for something better.
1394 * This can't happen in the second case above.
1395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001397 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1399 if (rlen < args->maxlen) {
1400 xfs_agblock_t bestfbno;
1401 xfs_extlen_t bestflen;
1402 xfs_agblock_t bestrbno;
1403 xfs_extlen_t bestrlen;
1404
1405 bestrlen = rlen;
1406 bestrbno = rbno;
1407 bestflen = flen;
1408 bestfbno = fbno;
1409 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001410 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 goto error0;
1412 if (i == 0)
1413 break;
1414 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1415 &i)))
1416 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001417 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (flen < bestrlen)
1419 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001420 xfs_alloc_compute_aligned(args, fbno, flen,
1421 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001423 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 (rlen <= flen && rbno + rlen <= fbno + flen),
1425 error0);
1426 if (rlen > bestrlen) {
1427 bestrlen = rlen;
1428 bestrbno = rbno;
1429 bestflen = flen;
1430 bestfbno = fbno;
1431 if (rlen == args->maxlen)
1432 break;
1433 }
1434 }
1435 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1436 &i)))
1437 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001438 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 rlen = bestrlen;
1440 rbno = bestrbno;
1441 flen = bestflen;
1442 fbno = bestfbno;
1443 }
1444 args->wasfromfl = 0;
1445 /*
1446 * Fix up the length.
1447 */
1448 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001449 if (rlen < args->minlen) {
1450 if (!forced++) {
1451 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1452 trace_xfs_alloc_size_busy(args);
1453 xfs_log_force(args->mp, XFS_LOG_SYNC);
1454 goto restart;
1455 }
1456 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001458 xfs_alloc_fix_len(args);
1459
1460 if (!xfs_alloc_fix_minleft(args))
1461 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001463 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * Allocate and initialize a cursor for the by-block tree.
1466 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001467 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1468 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1470 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1471 goto error0;
1472 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1473 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1474 cnt_cur = bno_cur = NULL;
1475 args->len = rlen;
1476 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001477 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001479 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001481 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 0;
1483
1484error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001485 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (cnt_cur)
1487 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1488 if (bno_cur)
1489 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1490 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001491
1492out_nominleft:
1493 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1494 trace_xfs_alloc_size_nominleft(args);
1495 args->agbno = NULLAGBLOCK;
1496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499/*
1500 * Deal with the case where only small freespaces remain.
1501 * Either return the contents of the last freespace record,
1502 * or allocate space from the freelist if there is nothing in the tree.
1503 */
1504STATIC int /* error */
1505xfs_alloc_ag_vextent_small(
1506 xfs_alloc_arg_t *args, /* allocation argument structure */
1507 xfs_btree_cur_t *ccur, /* by-size cursor */
1508 xfs_agblock_t *fbnop, /* result block number */
1509 xfs_extlen_t *flenp, /* result length */
1510 int *stat) /* status: 0-freelist, 1-normal/none */
1511{
1512 int error;
1513 xfs_agblock_t fbno;
1514 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int i;
1516
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001517 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 goto error0;
1519 if (i) {
1520 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1521 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001522 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 /*
1525 * Nothing in the btree, try the freelist. Make sure
1526 * to respect minleft even when pulling from the
1527 * freelist.
1528 */
1529 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001530 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1531 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001532 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1533 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto error0;
1535 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001536 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001537 args->userdata);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (args->userdata) {
1540 xfs_buf_t *bp;
1541
1542 bp = xfs_btree_get_bufs(args->mp, args->tp,
1543 args->agno, fbno, 0);
1544 xfs_trans_binval(args->tp, bp);
1545 }
1546 args->len = 1;
1547 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001548 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001550 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 error0);
1552 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001553 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 *stat = 0;
1555 return 0;
1556 }
1557 /*
1558 * Nothing in the freelist.
1559 */
1560 else
1561 flen = 0;
1562 }
1563 /*
1564 * Can't allocate from the freelist for some reason.
1565 */
Nathan Scottd432c802006-09-28 11:03:44 +10001566 else {
1567 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Can't do the allocation, give up.
1572 */
1573 if (flen < args->minlen) {
1574 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001575 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 flen = 0;
1577 }
1578 *fbnop = fbno;
1579 *flenp = flen;
1580 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001581 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
1583
1584error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001585 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return error;
1587}
1588
1589/*
1590 * Free the extent starting at agno/bno for length.
1591 */
1592STATIC int /* error */
1593xfs_free_ag_extent(
1594 xfs_trans_t *tp, /* transaction pointer */
1595 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1596 xfs_agnumber_t agno, /* allocation group number */
1597 xfs_agblock_t bno, /* starting block number */
1598 xfs_extlen_t len, /* length of extent */
1599 int isfl) /* set if is freelist blocks - no sb acctg */
1600{
1601 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1602 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1603 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 xfs_agblock_t gtbno; /* start of right neighbor block */
1605 xfs_extlen_t gtlen; /* length of right neighbor block */
1606 int haveleft; /* have a left neighbor block */
1607 int haveright; /* have a right neighbor block */
1608 int i; /* temp, result code */
1609 xfs_agblock_t ltbno; /* start of left neighbor block */
1610 xfs_extlen_t ltlen; /* length of left neighbor block */
1611 xfs_mount_t *mp; /* mount point struct for filesystem */
1612 xfs_agblock_t nbno; /* new starting block of freespace */
1613 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001614 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 mp = tp->t_mountp;
1617 /*
1618 * Allocate and initialize a cursor for the by-block btree.
1619 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001620 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 cnt_cur = NULL;
1622 /*
1623 * Look for a neighboring block on the left (lower block numbers)
1624 * that is contiguous with this space.
1625 */
1626 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1627 goto error0;
1628 if (haveleft) {
1629 /*
1630 * There is a block to our left.
1631 */
1632 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1633 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001634 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * It's not contiguous, though.
1637 */
1638 if (ltbno + ltlen < bno)
1639 haveleft = 0;
1640 else {
1641 /*
1642 * If this failure happens the request to free this
1643 * space was invalid, it's (partly) already free.
1644 * Very bad.
1645 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001646 XFS_WANT_CORRUPTED_GOTO(mp,
1647 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649 }
1650 /*
1651 * Look for a neighboring block on the right (higher block numbers)
1652 * that is contiguous with this space.
1653 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001654 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto error0;
1656 if (haveright) {
1657 /*
1658 * There is a block to our right.
1659 */
1660 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1661 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /*
1664 * It's not contiguous, though.
1665 */
1666 if (bno + len < gtbno)
1667 haveright = 0;
1668 else {
1669 /*
1670 * If this failure happens the request to free this
1671 * space was invalid, it's (partly) already free.
1672 * Very bad.
1673 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001674 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 }
1677 /*
1678 * Now allocate and initialize a cursor for the by-size tree.
1679 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001680 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * Have both left and right contiguous neighbors.
1683 * Merge all three into a single free block.
1684 */
1685 if (haveleft && haveright) {
1686 /*
1687 * Delete the old by-size entry on the left.
1688 */
1689 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1690 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001691 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001692 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001694 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Delete the old by-size entry on the right.
1697 */
1698 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1699 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001700 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001701 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001703 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * Delete the old by-block entry for the right block.
1706 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001707 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001709 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /*
1711 * Move the by-block cursor back to the left neighbor.
1712 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001713 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001715 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716#ifdef DEBUG
1717 /*
1718 * Check that this is the right record: delete didn't
1719 * mangle the cursor.
1720 */
1721 {
1722 xfs_agblock_t xxbno;
1723 xfs_extlen_t xxlen;
1724
1725 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1726 &i)))
1727 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001728 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 i == 1 && xxbno == ltbno && xxlen == ltlen,
1730 error0);
1731 }
1732#endif
1733 /*
1734 * Update remaining by-block entry to the new, joined block.
1735 */
1736 nbno = ltbno;
1737 nlen = len + ltlen + gtlen;
1738 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1739 goto error0;
1740 }
1741 /*
1742 * Have only a left contiguous neighbor.
1743 * Merge it together with the new freespace.
1744 */
1745 else if (haveleft) {
1746 /*
1747 * Delete the old by-size entry on the left.
1748 */
1749 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1750 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001752 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * Back up the by-block cursor to the left neighbor, and
1757 * update its length.
1758 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001759 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 nbno = ltbno;
1763 nlen = len + ltlen;
1764 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1765 goto error0;
1766 }
1767 /*
1768 * Have only a right contiguous neighbor.
1769 * Merge it together with the new freespace.
1770 */
1771 else if (haveright) {
1772 /*
1773 * Delete the old by-size entry on the right.
1774 */
1775 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1776 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001778 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * Update the starting block and length of the right
1783 * neighbor in the by-block tree.
1784 */
1785 nbno = bno;
1786 nlen = len + gtlen;
1787 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1788 goto error0;
1789 }
1790 /*
1791 * No contiguous neighbors.
1792 * Insert the new freespace into the by-block tree.
1793 */
1794 else {
1795 nbno = bno;
1796 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001797 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001799 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1802 bno_cur = NULL;
1803 /*
1804 * In all cases we need to insert the new freespace in the by-size tree.
1805 */
1806 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1807 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001808 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001809 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001811 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1813 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Update the freespace totals in the ag and superblock.
1817 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001818 pag = xfs_perag_get(mp, agno);
1819 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1820 xfs_perag_put(pag);
1821 if (error)
1822 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Christoph Hellwigecb69282011-03-04 12:59:55 +00001824 if (!isfl)
1825 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001826 XFS_STATS_INC(mp, xs_freex);
1827 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001828
1829 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return 0;
1832
1833 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001834 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (bno_cur)
1836 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1837 if (cnt_cur)
1838 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1839 return error;
1840}
1841
1842/*
1843 * Visible (exported) allocation/free functions.
1844 * Some of these are used just by xfs_alloc_btree.c and this file.
1845 */
1846
1847/*
1848 * Compute and fill in value of m_ag_maxlevels.
1849 */
1850void
1851xfs_alloc_compute_maxlevels(
1852 xfs_mount_t *mp) /* file system mount structure */
1853{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001854 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1855 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
1858/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001859 * Find the length of the longest extent in an AG.
1860 */
1861xfs_extlen_t
1862xfs_alloc_longest_free_extent(
1863 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001864 struct xfs_perag *pag,
1865 xfs_extlen_t need)
Dave Chinner6cc87642009-03-16 08:29:46 +01001866{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001867 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001868
Dave Chinner6cc87642009-03-16 08:29:46 +01001869 if (need > pag->pagf_flcount)
1870 delta = need - pag->pagf_flcount;
1871
1872 if (pag->pagf_longest > delta)
1873 return pag->pagf_longest - delta;
1874 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1875}
1876
Dave Chinner496817b2015-06-22 10:13:30 +10001877unsigned int
1878xfs_alloc_min_freelist(
1879 struct xfs_mount *mp,
1880 struct xfs_perag *pag)
1881{
1882 unsigned int min_free;
1883
1884 /* space needed by-bno freespace btree */
1885 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1886 mp->m_ag_maxlevels);
1887 /* space needed by-size freespace btree */
1888 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1889 mp->m_ag_maxlevels);
1890
1891 return min_free;
1892}
1893
Dave Chinner6cc87642009-03-16 08:29:46 +01001894/*
Dave Chinner72d55282015-06-22 10:04:42 +10001895 * Check if the operation we are fixing up the freelist for should go ahead or
1896 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1897 * is dependent on whether the size and shape of free space available will
1898 * permit the requested allocation to take place.
1899 */
1900static bool
1901xfs_alloc_space_available(
1902 struct xfs_alloc_arg *args,
1903 xfs_extlen_t min_free,
1904 int flags)
1905{
1906 struct xfs_perag *pag = args->pag;
1907 xfs_extlen_t longest;
1908 int available;
1909
1910 if (flags & XFS_ALLOC_FLAG_FREEING)
1911 return true;
1912
1913 /* do we have enough contiguous free space for the allocation? */
1914 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free);
1915 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
1916 return false;
1917
1918 /* do have enough free space remaining for the allocation? */
1919 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
1920 min_free - args->total);
1921 if (available < (int)args->minleft)
1922 return false;
1923
1924 return true;
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/*
1928 * Decide whether to use this allocation group for this allocation.
1929 * If so, fix up the btree freelist's size.
1930 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11001931int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10001933 struct xfs_alloc_arg *args, /* allocation argument structure */
1934 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Dave Chinner396503f2015-06-22 10:13:19 +10001936 struct xfs_mount *mp = args->mp;
1937 struct xfs_perag *pag = args->pag;
1938 struct xfs_trans *tp = args->tp;
1939 struct xfs_buf *agbp = NULL;
1940 struct xfs_buf *agflbp = NULL;
1941 struct xfs_alloc_arg targs; /* local allocation arguments */
1942 xfs_agblock_t bno; /* freelist block */
1943 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10001944 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10001947 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1948 if (error)
1949 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001951 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1952 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001953 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Dave Chinner396503f2015-06-22 10:13:19 +10001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001957 /*
Dave Chinner396503f2015-06-22 10:13:19 +10001958 * If this is a metadata preferred pag and we are user data then try
1959 * somewhere else if we are not being asked to try harder at this
1960 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001962 if (pag->pagf_metadata && args->userdata &&
1963 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1964 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001965 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
Dave Chinner496817b2015-06-22 10:13:30 +10001968 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001969 if (!xfs_alloc_space_available(args, need, flags))
1970 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
1973 * Get the a.g. freespace buffer.
1974 * Can fail if we're not blocking on locks, and it's held.
1975 */
Dave Chinner396503f2015-06-22 10:13:19 +10001976 if (!agbp) {
1977 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1978 if (error)
1979 goto out_no_agbp;
1980 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001981 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1982 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001983 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10001986
Dave Chinner50adbcb2015-06-22 10:04:31 +10001987 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10001988 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001989 if (!xfs_alloc_space_available(args, need, flags))
1990 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /*
1993 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10001994 *
Dave Chinner396503f2015-06-22 10:13:19 +10001995 * Note that from this point onwards, we will always release the agf and
1996 * agfl buffers on error. This handles the case where we error out and
1997 * the buffers are clean or may not have been joined to the transaction
1998 * and hence need to be released manually. If they have been joined to
1999 * the transaction, then xfs_trans_brelse() will handle them
2000 * appropriately based on the recursion count and dirty state of the
2001 * buffer.
2002 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002003 * XXX (dgc): When we have lots of free space, does this buy us
2004 * anything other than extra overhead when we need to put more blocks
2005 * back on the free list? Maybe we should only do this when space is
2006 * getting low or the AGFL is more than half full?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Dave Chinner50adbcb2015-06-22 10:04:31 +10002008 while (pag->pagf_flcount > need) {
2009 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
David Chinner92821e22007-05-24 15:26:31 +10002011 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2012 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002013 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002014 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1);
2015 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002016 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2018 xfs_trans_binval(tp, bp);
2019 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002020
Mark Tinguelya0041682012-09-20 13:16:45 -05002021 memset(&targs, 0, sizeof(targs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 targs.tp = tp;
2023 targs.mp = mp;
2024 targs.agbp = agbp;
2025 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2027 targs.type = XFS_ALLOCTYPE_THIS_AG;
2028 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002029 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2030 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002031 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002032
2033 /* Make the freelist longer if it's too short. */
2034 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002036 targs.maxlen = need - pag->pagf_flcount;
2037
2038 /* Allocate as many blocks as possible at once. */
2039 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002040 if (error)
2041 goto out_agflbp_relse;
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
2044 * Stop if we run out. Won't happen if callers are obeying
2045 * the restrictions correctly. Can happen for free calls
2046 * on a completely full ag.
2047 */
Yingping Lud210a282006-06-09 14:55:18 +10002048 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002049 if (flags & XFS_ALLOC_FLAG_FREEING)
2050 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002051 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /*
2054 * Put each allocated block on the list.
2055 */
2056 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002057 error = xfs_alloc_put_freelist(tp, agbp,
2058 agflbp, bno, 0);
2059 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002060 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002063 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 args->agbp = agbp;
2065 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002066
2067out_agflbp_relse:
2068 xfs_trans_brelse(tp, agflbp);
2069out_agbp_relse:
2070 if (agbp)
2071 xfs_trans_brelse(tp, agbp);
2072out_no_agbp:
2073 args->agbp = NULL;
2074 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077/*
2078 * Get a block from the freelist.
2079 * Returns with the buffer for the block gotten.
2080 */
2081int /* error */
2082xfs_alloc_get_freelist(
2083 xfs_trans_t *tp, /* transaction pointer */
2084 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002085 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2086 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2090 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002091 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int error;
David Chinner92821e22007-05-24 15:26:31 +10002093 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002094 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 xfs_perag_t *pag; /* per allocation group data */
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /*
2098 * Freelist is empty, give up.
2099 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002100 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (!agf->agf_flcount) {
2102 *bnop = NULLAGBLOCK;
2103 return 0;
2104 }
2105 /*
2106 * Read the array of free blocks.
2107 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002108 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2109 &agflbp);
2110 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002112
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * Get the block number and update the data structures.
2116 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002117 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2118 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002119 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002121 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002123
2124 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002125 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 xfs_trans_agflist_delta(tp, -1);
2127 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002128 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002129
2130 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2131 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002132 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002133 pag->pagf_btreeblks++;
2134 logflags |= XFS_AGF_BTREEBLKS;
2135 }
2136
David Chinner92821e22007-05-24 15:26:31 +10002137 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *bnop = bno;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141}
2142
2143/*
2144 * Log the given fields from the agf structure.
2145 */
2146void
2147xfs_alloc_log_agf(
2148 xfs_trans_t *tp, /* transaction pointer */
2149 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2150 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2151{
2152 int first; /* first byte offset */
2153 int last; /* last byte offset */
2154 static const short offsets[] = {
2155 offsetof(xfs_agf_t, agf_magicnum),
2156 offsetof(xfs_agf_t, agf_versionnum),
2157 offsetof(xfs_agf_t, agf_seqno),
2158 offsetof(xfs_agf_t, agf_length),
2159 offsetof(xfs_agf_t, agf_roots[0]),
2160 offsetof(xfs_agf_t, agf_levels[0]),
2161 offsetof(xfs_agf_t, agf_flfirst),
2162 offsetof(xfs_agf_t, agf_fllast),
2163 offsetof(xfs_agf_t, agf_flcount),
2164 offsetof(xfs_agf_t, agf_freeblks),
2165 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002166 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002167 offsetof(xfs_agf_t, agf_uuid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 sizeof(xfs_agf_t)
2169 };
2170
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002171 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2172
Dave Chinner61fe1352013-04-03 16:11:30 +11002173 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2176 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2177}
2178
2179/*
2180 * Interface for inode allocation to force the pag data to be initialized.
2181 */
2182int /* error */
2183xfs_alloc_pagf_init(
2184 xfs_mount_t *mp, /* file system mount structure */
2185 xfs_trans_t *tp, /* transaction pointer */
2186 xfs_agnumber_t agno, /* allocation group number */
2187 int flags) /* XFS_ALLOC_FLAGS_... */
2188{
2189 xfs_buf_t *bp;
2190 int error;
2191
2192 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2193 return error;
2194 if (bp)
2195 xfs_trans_brelse(tp, bp);
2196 return 0;
2197}
2198
2199/*
2200 * Put the block on the freelist for the allocation group.
2201 */
2202int /* error */
2203xfs_alloc_put_freelist(
2204 xfs_trans_t *tp, /* transaction pointer */
2205 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2206 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002207 xfs_agblock_t bno, /* block being freed */
2208 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002211 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int error;
David Chinner92821e22007-05-24 15:26:31 +10002213 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_mount_t *mp; /* mount structure */
2215 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002216 __be32 *agfl_bno;
2217 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 agf = XFS_BUF_TO_AGF(agbp);
2220 mp = tp->t_mountp;
2221
2222 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002223 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002225 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002226 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002228
2229 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002230 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 xfs_trans_agflist_delta(tp, 1);
2232 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002233
2234 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2235 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002236 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002237 pag->pagf_btreeblks--;
2238 logflags |= XFS_AGF_BTREEBLKS;
2239 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002240 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002241
David Chinner92821e22007-05-24 15:26:31 +10002242 xfs_alloc_log_agf(tp, agbp, logflags);
2243
Christoph Hellwig16259e72005-11-02 15:11:25 +11002244 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002245
2246 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2247 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002248 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002249 startoff = (char *)blockp - (char *)agflbp->b_addr;
2250
David Chinner92821e22007-05-24 15:26:31 +10002251 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002252
Dave Chinner61fe1352013-04-03 16:11:30 +11002253 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002254 xfs_trans_log_buf(tp, agflbp, startoff,
2255 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
2257}
2258
Dave Chinner4e0e6042013-04-03 16:11:13 +11002259static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002260xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002261 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002262 struct xfs_buf *bp)
2263 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002264 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002265
Brian Fostera45086e2015-10-12 15:59:25 +11002266 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2267 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002268 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002269 if (!xfs_log_check_lsn(mp,
2270 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2271 return false;
2272 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002273
Dave Chinner4e0e6042013-04-03 16:11:13 +11002274 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2275 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2276 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2277 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2278 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2279 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2280 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002281
Eric Sandeene1b05722014-09-09 11:47:24 +10002282 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2283 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2284 return false;
2285
Darrick J. Wongb8704942016-08-03 11:30:32 +10002286 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2287 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2288 return false;
2289
Dave Chinner5d5f5272012-11-14 17:44:56 +11002290 /*
2291 * during growfs operations, the perag is not fully initialised,
2292 * so we can't use it for any useful checking. growfs ensures we can't
2293 * use it by using uncached buffers that don't have the perag attached
2294 * so we can detect and avoid this problem.
2295 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002296 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2297 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002298
Dave Chinner4e0e6042013-04-03 16:11:13 +11002299 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2300 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2301 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002302
Dave Chinner4e0e6042013-04-03 16:11:13 +11002303 return true;;
2304
Dave Chinner612cfbf2012-11-14 17:52:32 +11002305}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002306
Dave Chinner1813dd62012-11-14 17:54:40 +11002307static void
2308xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002309 struct xfs_buf *bp)
2310{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002311 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002312
Eric Sandeence5028c2014-02-27 15:23:10 +11002313 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2314 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002315 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002316 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2317 XFS_ERRTAG_ALLOC_READ_AGF,
2318 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002319 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002320
2321 if (bp->b_error)
2322 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002323}
2324
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002325static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002326xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002327 struct xfs_buf *bp)
2328{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002329 struct xfs_mount *mp = bp->b_target->bt_mount;
2330 struct xfs_buf_log_item *bip = bp->b_fspriv;
2331
2332 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002333 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002334 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002335 return;
2336 }
2337
2338 if (!xfs_sb_version_hascrc(&mp->m_sb))
2339 return;
2340
2341 if (bip)
2342 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2343
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002344 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002345}
2346
Dave Chinner1813dd62012-11-14 17:54:40 +11002347const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002348 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002349 .verify_read = xfs_agf_read_verify,
2350 .verify_write = xfs_agf_write_verify,
2351};
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/*
2354 * Read in the allocation group header (free/alloc section).
2355 */
2356int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002357xfs_read_agf(
2358 struct xfs_mount *mp, /* mount point structure */
2359 struct xfs_trans *tp, /* transaction pointer */
2360 xfs_agnumber_t agno, /* allocation group number */
2361 int flags, /* XFS_BUF_ */
2362 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int error;
2365
Dave Chinnerd1230312013-11-01 15:27:19 +11002366 trace_xfs_read_agf(mp, agno);
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 ASSERT(agno != NULLAGNUMBER);
2369 error = xfs_trans_read_buf(
2370 mp, tp, mp->m_ddev_targp,
2371 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002372 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (error)
2374 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002375 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002377
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002378 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002379 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002380 return 0;
2381}
2382
2383/*
2384 * Read in the allocation group header (free/alloc section).
2385 */
2386int /* error */
2387xfs_alloc_read_agf(
2388 struct xfs_mount *mp, /* mount point structure */
2389 struct xfs_trans *tp, /* transaction pointer */
2390 xfs_agnumber_t agno, /* allocation group number */
2391 int flags, /* XFS_ALLOC_FLAG_... */
2392 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2393{
2394 struct xfs_agf *agf; /* ag freelist header */
2395 struct xfs_perag *pag; /* per allocation group data */
2396 int error;
2397
Dave Chinnerd1230312013-11-01 15:27:19 +11002398 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002399
Dave Chinnerd1230312013-11-01 15:27:19 +11002400 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002401 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002402 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002403 bpp);
2404 if (error)
2405 return error;
2406 if (!*bpp)
2407 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002408 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002409
2410 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002411 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002413 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002414 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002415 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2416 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002418 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002420 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002421 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2422 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002423 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002424 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002425 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 pag->pagf_init = 1;
2427 }
2428#ifdef DEBUG
2429 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002430 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002431 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002432 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2433 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002435 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002437 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002440 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
2442}
2443
2444/*
2445 * Allocate an extent (variable-size).
2446 * Depending on the allocation type, we either look in a single allocation
2447 * group or loop over the allocation groups to find the result.
2448 */
2449int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002450xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 xfs_alloc_arg_t *args) /* allocation argument structure */
2452{
2453 xfs_agblock_t agsize; /* allocation group size */
2454 int error;
2455 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 xfs_extlen_t minleft;/* minimum left value, temp copy */
2457 xfs_mount_t *mp; /* mount structure pointer */
2458 xfs_agnumber_t sagno; /* starting allocation group number */
2459 xfs_alloctype_t type; /* input allocation type */
2460 int bump_rotor = 0;
2461 int no_min = 0;
2462 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2463
2464 mp = args->mp;
2465 type = args->otype = args->type;
2466 args->agbno = NULLAGBLOCK;
2467 /*
2468 * Just fix this up, for the case where the last a.g. is shorter
2469 * (or there's only one a.g.) and the caller couldn't easily figure
2470 * that out (xfs_bmap_alloc).
2471 */
2472 agsize = mp->m_sb.sb_agblocks;
2473 if (args->maxlen > agsize)
2474 args->maxlen = agsize;
2475 if (args->alignment == 0)
2476 args->alignment = 1;
2477 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2478 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2479 ASSERT(args->minlen <= args->maxlen);
2480 ASSERT(args->minlen <= agsize);
2481 ASSERT(args->mod < args->prod);
2482 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2483 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2484 args->minlen > args->maxlen || args->minlen > agsize ||
2485 args->mod >= args->prod) {
2486 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002487 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return 0;
2489 }
2490 minleft = args->minleft;
2491
2492 switch (type) {
2493 case XFS_ALLOCTYPE_THIS_AG:
2494 case XFS_ALLOCTYPE_NEAR_BNO:
2495 case XFS_ALLOCTYPE_THIS_BNO:
2496 /*
2497 * These three force us into a single a.g.
2498 */
2499 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002500 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 args->minleft = 0;
2502 error = xfs_alloc_fix_freelist(args, 0);
2503 args->minleft = minleft;
2504 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002505 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 goto error0;
2507 }
2508 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002509 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
2511 }
2512 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2513 if ((error = xfs_alloc_ag_vextent(args)))
2514 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 break;
2516 case XFS_ALLOCTYPE_START_BNO:
2517 /*
2518 * Try near allocation first, then anywhere-in-ag after
2519 * the first a.g. fails.
2520 */
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002521 if ((args->userdata & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2523 args->fsbno = XFS_AGB_TO_FSB(mp,
2524 ((mp->m_agfrotor / rotorstep) %
2525 mp->m_sb.sb_agcount), 0);
2526 bump_rotor = 1;
2527 }
2528 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2529 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2530 /* FALLTHROUGH */
2531 case XFS_ALLOCTYPE_ANY_AG:
2532 case XFS_ALLOCTYPE_START_AG:
2533 case XFS_ALLOCTYPE_FIRST_AG:
2534 /*
2535 * Rotate through the allocation groups looking for a winner.
2536 */
2537 if (type == XFS_ALLOCTYPE_ANY_AG) {
2538 /*
2539 * Start with the last place we left off.
2540 */
2541 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2542 mp->m_sb.sb_agcount;
2543 args->type = XFS_ALLOCTYPE_THIS_AG;
2544 flags = XFS_ALLOC_FLAG_TRYLOCK;
2545 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2546 /*
2547 * Start with allocation group given by bno.
2548 */
2549 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2550 args->type = XFS_ALLOCTYPE_THIS_AG;
2551 sagno = 0;
2552 flags = 0;
2553 } else {
2554 if (type == XFS_ALLOCTYPE_START_AG)
2555 args->type = XFS_ALLOCTYPE_THIS_AG;
2556 /*
2557 * Start with the given allocation group.
2558 */
2559 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2560 flags = XFS_ALLOC_FLAG_TRYLOCK;
2561 }
2562 /*
2563 * Loop over allocation groups twice; first time with
2564 * trylock set, second time without.
2565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002567 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (no_min) args->minleft = 0;
2569 error = xfs_alloc_fix_freelist(args, flags);
2570 args->minleft = minleft;
2571 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002572 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 goto error0;
2574 }
2575 /*
2576 * If we get a buffer back then the allocation will fly.
2577 */
2578 if (args->agbp) {
2579 if ((error = xfs_alloc_ag_vextent(args)))
2580 goto error0;
2581 break;
2582 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002583
2584 trace_xfs_alloc_vextent_loopfailed(args);
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /*
2587 * Didn't work, figure out the next iteration.
2588 */
2589 if (args->agno == sagno &&
2590 type == XFS_ALLOCTYPE_START_BNO)
2591 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002592 /*
2593 * For the first allocation, we can try any AG to get
2594 * space. However, if we already have allocated a
2595 * block, we don't want to try AGs whose number is below
2596 * sagno. Otherwise, we may end up with out-of-order
2597 * locking of AGF, which might cause deadlock.
2598 */
2599 if (++(args->agno) == mp->m_sb.sb_agcount) {
2600 if (args->firstblock != NULLFSBLOCK)
2601 args->agno = sagno;
2602 else
2603 args->agno = 0;
2604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * Reached the starting a.g., must either be done
2607 * or switch to non-trylock mode.
2608 */
2609 if (args->agno == sagno) {
2610 if (no_min == 1) {
2611 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002612 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 break;
2614 }
2615 if (flags == 0) {
2616 no_min = 1;
2617 } else {
2618 flags = 0;
2619 if (type == XFS_ALLOCTYPE_START_BNO) {
2620 args->agbno = XFS_FSB_TO_AGBNO(mp,
2621 args->fsbno);
2622 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2623 }
2624 }
2625 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002626 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2629 if (args->agno == sagno)
2630 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2631 (mp->m_sb.sb_agcount * rotorstep);
2632 else
2633 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2634 (mp->m_sb.sb_agcount * rotorstep);
2635 }
2636 break;
2637 default:
2638 ASSERT(0);
2639 /* NOTREACHED */
2640 }
2641 if (args->agbno == NULLAGBLOCK)
2642 args->fsbno = NULLFSBLOCK;
2643 else {
2644 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2645#ifdef DEBUG
2646 ASSERT(args->len >= args->minlen);
2647 ASSERT(args->len <= args->maxlen);
2648 ASSERT(args->agbno % args->alignment == 0);
2649 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2650 args->len);
2651#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002652
2653 /* Zero the extent if we were asked to do so */
2654 if (args->userdata & XFS_ALLOC_USERDATA_ZERO) {
2655 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2656 if (error)
2657 goto error0;
2658 }
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002661 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
2663error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002664 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return error;
2666}
2667
Dave Chinner4d89e202016-06-21 11:53:28 +10002668/* Ensure that the freelist is at full capacity. */
2669int
2670xfs_free_extent_fix_freelist(
2671 struct xfs_trans *tp,
2672 xfs_agnumber_t agno,
2673 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Dave Chinner4d89e202016-06-21 11:53:28 +10002675 struct xfs_alloc_arg args;
2676 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Dave Chinner4d89e202016-06-21 11:53:28 +10002678 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 args.tp = tp;
2680 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002681 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002682
2683 /*
2684 * validate that the block number is legal - the enables us to detect
2685 * and handle a silent filesystem corruption rather than crashing.
2686 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002687 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002688 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002689
Dave Chinnera862e0f2010-01-11 11:47:41 +00002690 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002691 ASSERT(args.pag);
2692
2693 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2694 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002695 goto out;
2696
2697 *agbp = args.agbp;
2698out:
2699 xfs_perag_put(args.pag);
2700 return error;
2701}
2702
2703/*
2704 * Free an extent.
2705 * Just break up the extent address and hand off to xfs_free_ag_extent
2706 * after fixing up the freelist.
2707 */
2708int /* error */
2709xfs_free_extent(
2710 struct xfs_trans *tp, /* transaction pointer */
2711 xfs_fsblock_t bno, /* starting block number of extent */
2712 xfs_extlen_t len) /* length of extent */
2713{
2714 struct xfs_mount *mp = tp->t_mountp;
2715 struct xfs_buf *agbp;
2716 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2717 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2718 int error;
2719
2720 ASSERT(len != 0);
2721
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002722 trace_xfs_bmap_free_deferred(mp, agno, 0, agbno, len);
2723
2724 if (XFS_TEST_ERROR(false, mp,
2725 XFS_ERRTAG_FREE_EXTENT,
2726 XFS_RANDOM_FREE_EXTENT))
2727 return -EIO;
2728
Dave Chinner4d89e202016-06-21 11:53:28 +10002729 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2730 if (error)
2731 return error;
2732
2733 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002734
2735 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002736 XFS_WANT_CORRUPTED_GOTO(mp,
2737 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2738 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002739
Dave Chinner4d89e202016-06-21 11:53:28 +10002740 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, 0);
2741 if (error)
2742 goto err;
2743
2744 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2745 return 0;
2746
2747err:
2748 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return error;
2750}