blob: 6335b8b85349707c16fbe81e4b5cec37cf838036 [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"
Darrick J. Wong340785c2016-08-03 11:33:42 +100039#include "xfs_rmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dave Chinnerc999a222012-03-22 05:15:07 +000041struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
44
45#define XFSA_FIXUP_BNO_OK 1
46#define XFSA_FIXUP_CNT_OK 2
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
49STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000052 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Darrick J. Wong80180262016-08-03 11:31:47 +100054xfs_extlen_t
55xfs_prealloc_blocks(
56 struct xfs_mount *mp)
57{
58 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
59 return XFS_RMAP_BLOCK(mp) + 1;
60 if (xfs_sb_version_hasfinobt(&mp->m_sb))
61 return XFS_FIBT_BLOCK(mp) + 1;
62 return XFS_IBT_BLOCK(mp) + 1;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110066 * Lookup the record equal to [bno, len] in the btree given by cur.
67 */
68STATIC int /* error */
69xfs_alloc_lookup_eq(
70 struct xfs_btree_cur *cur, /* btree cursor */
71 xfs_agblock_t bno, /* starting block of extent */
72 xfs_extlen_t len, /* length of extent */
73 int *stat) /* success/failure */
74{
75 cur->bc_rec.a.ar_startblock = bno;
76 cur->bc_rec.a.ar_blockcount = len;
77 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
78}
79
80/*
81 * Lookup the first record greater than or equal to [bno, len]
82 * in the btree given by cur.
83 */
Dave Chinnera66d6362012-03-22 05:15:12 +000084int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110085xfs_alloc_lookup_ge(
86 struct xfs_btree_cur *cur, /* btree cursor */
87 xfs_agblock_t bno, /* starting block of extent */
88 xfs_extlen_t len, /* length of extent */
89 int *stat) /* success/failure */
90{
91 cur->bc_rec.a.ar_startblock = bno;
92 cur->bc_rec.a.ar_blockcount = len;
93 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
94}
95
96/*
97 * Lookup the first record less than or equal to [bno, len]
98 * in the btree given by cur.
99 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000100static int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100101xfs_alloc_lookup_le(
102 struct xfs_btree_cur *cur, /* btree cursor */
103 xfs_agblock_t bno, /* starting block of extent */
104 xfs_extlen_t len, /* length of extent */
105 int *stat) /* success/failure */
106{
107 cur->bc_rec.a.ar_startblock = bno;
108 cur->bc_rec.a.ar_blockcount = len;
109 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
110}
111
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100112/*
113 * Update the record referred to by cur to the value given
114 * by [bno, len].
115 * This either works (return 0) or gets an EFSCORRUPTED error.
116 */
117STATIC int /* error */
118xfs_alloc_update(
119 struct xfs_btree_cur *cur, /* btree cursor */
120 xfs_agblock_t bno, /* starting block of extent */
121 xfs_extlen_t len) /* length of extent */
122{
123 union xfs_btree_rec rec;
124
125 rec.alloc.ar_startblock = cpu_to_be32(bno);
126 rec.alloc.ar_blockcount = cpu_to_be32(len);
127 return xfs_btree_update(cur, &rec);
128}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100129
130/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100131 * Get the data from the pointed-to record.
132 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000133int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100134xfs_alloc_get_rec(
135 struct xfs_btree_cur *cur, /* btree cursor */
136 xfs_agblock_t *bno, /* output: starting block of extent */
137 xfs_extlen_t *len, /* output: length of extent */
138 int *stat) /* output: success/failure */
139{
140 union xfs_btree_rec *rec;
141 int error;
142
143 error = xfs_btree_get_rec(cur, &rec, stat);
144 if (!error && *stat == 1) {
145 *bno = be32_to_cpu(rec->alloc.ar_startblock);
146 *len = be32_to_cpu(rec->alloc.ar_blockcount);
147 }
148 return error;
149}
150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Compute aligned version of the found extent.
153 * Takes alignment and min length into account.
154 */
David Chinner12375c82008-04-10 12:21:32 +1000155STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000157 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 xfs_agblock_t foundbno, /* starting block in found extent */
159 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 xfs_agblock_t *resbno, /* result block number */
161 xfs_extlen_t *reslen) /* result length */
162{
163 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000165 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwige26f0502011-04-24 19:06:15 +0000167 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000168 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000169
Brian Fosterbfe46d42015-05-29 08:53:00 +1000170 /*
171 * If we have a largish extent that happens to start before min_agbno,
172 * see if we can shift it into range...
173 */
174 if (bno < args->min_agbno && bno + len > args->min_agbno) {
175 diff = args->min_agbno - bno;
176 if (len > diff) {
177 bno += diff;
178 len -= diff;
179 }
180 }
181
Christoph Hellwige26f0502011-04-24 19:06:15 +0000182 if (args->alignment > 1 && len >= args->minlen) {
183 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000184
185 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000186
187 *resbno = aligned_bno;
188 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000190 *resbno = bno;
191 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * Compute best start block and diff for "near" allocations.
197 * freelen >= wantlen already checked by caller.
198 */
199STATIC xfs_extlen_t /* difference value (absolute) */
200xfs_alloc_compute_diff(
201 xfs_agblock_t wantbno, /* target starting block */
202 xfs_extlen_t wantlen, /* target length */
203 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200204 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 xfs_agblock_t freebno, /* freespace's starting block */
206 xfs_extlen_t freelen, /* freespace's length */
207 xfs_agblock_t *newbnop) /* result: best start block from free */
208{
209 xfs_agblock_t freeend; /* end of freespace extent */
210 xfs_agblock_t newbno1; /* return block number */
211 xfs_agblock_t newbno2; /* other new block number */
212 xfs_extlen_t newlen1=0; /* length with newbno1 */
213 xfs_extlen_t newlen2=0; /* length with newbno2 */
214 xfs_agblock_t wantend; /* end of target extent */
215
216 ASSERT(freelen >= wantlen);
217 freeend = freebno + freelen;
218 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200219 /*
220 * We want to allocate from the start of a free extent if it is past
221 * the desired block or if we are allocating user data and the free
222 * extent is before desired block. The second case is there to allow
223 * for contiguous allocation from the remaining free space if the file
224 * grows in the short term.
225 */
226 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
228 newbno1 = NULLAGBLOCK;
229 } else if (freeend >= wantend && alignment > 1) {
230 newbno1 = roundup(wantbno, alignment);
231 newbno2 = newbno1 - alignment;
232 if (newbno1 >= freeend)
233 newbno1 = NULLAGBLOCK;
234 else
235 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
236 if (newbno2 < freebno)
237 newbno2 = NULLAGBLOCK;
238 else
239 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
240 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
241 if (newlen1 < newlen2 ||
242 (newlen1 == newlen2 &&
243 XFS_ABSDIFF(newbno1, wantbno) >
244 XFS_ABSDIFF(newbno2, wantbno)))
245 newbno1 = newbno2;
246 } else if (newbno2 != NULLAGBLOCK)
247 newbno1 = newbno2;
248 } else if (freeend >= wantend) {
249 newbno1 = wantbno;
250 } else if (alignment > 1) {
251 newbno1 = roundup(freeend - wantlen, alignment);
252 if (newbno1 > freeend - wantlen &&
253 newbno1 - alignment >= freebno)
254 newbno1 -= alignment;
255 else if (newbno1 >= freeend)
256 newbno1 = NULLAGBLOCK;
257 } else
258 newbno1 = freeend - wantlen;
259 *newbnop = newbno1;
260 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
261}
262
263/*
264 * Fix up the length, based on mod and prod.
265 * len should be k * prod + mod for some k.
266 * If len is too small it is returned unchanged.
267 * If len hits maxlen it is left alone.
268 */
269STATIC void
270xfs_alloc_fix_len(
271 xfs_alloc_arg_t *args) /* allocation argument structure */
272{
273 xfs_extlen_t k;
274 xfs_extlen_t rlen;
275
276 ASSERT(args->mod < args->prod);
277 rlen = args->len;
278 ASSERT(rlen >= args->minlen);
279 ASSERT(rlen <= args->maxlen);
280 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
281 (args->mod == 0 && rlen < args->prod))
282 return;
283 k = rlen % args->prod;
284 if (k == args->mod)
285 return;
Jan Kara30265112014-06-06 16:06:37 +1000286 if (k > args->mod)
287 rlen = rlen - (k - args->mod);
288 else
289 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100290 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000291 if ((int)rlen < (int)args->minlen)
292 return;
293 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
294 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 args->len = rlen;
296}
297
298/*
299 * Fix up length if there is too little space left in the a.g.
300 * Return 1 if ok, 0 if too little, should give up.
301 */
302STATIC int
303xfs_alloc_fix_minleft(
304 xfs_alloc_arg_t *args) /* allocation argument structure */
305{
306 xfs_agf_t *agf; /* a.g. freelist header */
307 int diff; /* free space difference */
308
309 if (args->minleft == 0)
310 return 1;
311 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100312 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 - args->len - args->minleft;
314 if (diff >= 0)
315 return 1;
316 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100317 /* casts to (int) catch length underflows */
318 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return 1;
320 args->agbno = NULLAGBLOCK;
321 return 0;
322}
323
324/*
325 * Update the two btrees, logically removing from freespace the extent
326 * starting at rbno, rlen blocks. The extent is contained within the
327 * actual (current) free extent fbno for flen blocks.
328 * Flags are passed in indicating whether the cursors are set to the
329 * relevant records.
330 */
331STATIC int /* error code */
332xfs_alloc_fixup_trees(
333 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
334 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
335 xfs_agblock_t fbno, /* starting block of free extent */
336 xfs_extlen_t flen, /* length of free extent */
337 xfs_agblock_t rbno, /* starting block of returned extent */
338 xfs_extlen_t rlen, /* length of returned extent */
339 int flags) /* flags, XFSA_FIXUP_... */
340{
341 int error; /* error code */
342 int i; /* operation results */
343 xfs_agblock_t nfbno1; /* first new free startblock */
344 xfs_agblock_t nfbno2; /* second new free startblock */
345 xfs_extlen_t nflen1=0; /* first new free length */
346 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100347 struct xfs_mount *mp;
348
349 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /*
352 * Look up the record in the by-size tree if necessary.
353 */
354 if (flags & XFSA_FIXUP_CNT_OK) {
355#ifdef DEBUG
356 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
357 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100358 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 i == 1 && nfbno1 == fbno && nflen1 == flen);
360#endif
361 } else {
362 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
363 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100364 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 /*
367 * Look up the record in the by-block tree if necessary.
368 */
369 if (flags & XFSA_FIXUP_BNO_OK) {
370#ifdef DEBUG
371 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
372 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100373 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 i == 1 && nfbno1 == fbno && nflen1 == flen);
375#endif
376 } else {
377 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
378 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100379 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100382#ifdef DEBUG
383 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
384 struct xfs_btree_block *bnoblock;
385 struct xfs_btree_block *cntblock;
386
387 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
388 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
389
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100390 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100391 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * Deal with all four cases: the allocated record is contained
397 * within the freespace record, so we can have new freespace
398 * at either (or both) end, or no freespace remaining.
399 */
400 if (rbno == fbno && rlen == flen)
401 nfbno1 = nfbno2 = NULLAGBLOCK;
402 else if (rbno == fbno) {
403 nfbno1 = rbno + rlen;
404 nflen1 = flen - rlen;
405 nfbno2 = NULLAGBLOCK;
406 } else if (rbno + rlen == fbno + flen) {
407 nfbno1 = fbno;
408 nflen1 = flen - rlen;
409 nfbno2 = NULLAGBLOCK;
410 } else {
411 nfbno1 = fbno;
412 nflen1 = rbno - fbno;
413 nfbno2 = rbno + rlen;
414 nflen2 = (fbno + flen) - nfbno2;
415 }
416 /*
417 * Delete the entry from the by-size btree.
418 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100419 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100421 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * Add new by-size btree entry(s).
424 */
425 if (nfbno1 != NULLAGBLOCK) {
426 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
427 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100428 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100429 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100431 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 if (nfbno2 != NULLAGBLOCK) {
434 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
435 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100436 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100437 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100439 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 /*
442 * Fix up the by-block btree entry(s).
443 */
444 if (nfbno1 == NULLAGBLOCK) {
445 /*
446 * No remaining freespace, just delete the by-block tree entry.
447 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100448 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100450 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else {
452 /*
453 * Update the by-block entry to start later|be shorter.
454 */
455 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
456 return error;
457 }
458 if (nfbno2 != NULLAGBLOCK) {
459 /*
460 * 2 resulting free entries, need to add one.
461 */
462 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
463 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100464 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100465 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100467 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 return 0;
470}
471
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100472static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100473xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100474 struct xfs_buf *bp)
475{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100476 struct xfs_mount *mp = bp->b_target->bt_mount;
477 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100478 int i;
479
Eric Sandeence748ea2015-07-29 11:53:31 +1000480 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100481 return false;
482 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
483 return false;
484 /*
485 * during growfs operations, the perag is not fully initialised,
486 * so we can't use it for any useful checking. growfs ensures we can't
487 * use it by using uncached buffers that don't have the perag attached
488 * so we can detect and avoid this problem.
489 */
490 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
491 return false;
492
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100493 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100494 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100495 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100496 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100497 }
Brian Fostera45086e2015-10-12 15:59:25 +1100498
499 return xfs_log_check_lsn(mp,
500 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100501}
502
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100503static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100504xfs_agfl_read_verify(
505 struct xfs_buf *bp)
506{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100507 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100508
509 /*
510 * There is no verification of non-crc AGFLs because mkfs does not
511 * initialise the AGFL to zero or NULL. Hence the only valid part of the
512 * AGFL is what the AGF says is active. We can't get to the AGF, so we
513 * can't verify just those entries are valid.
514 */
515 if (!xfs_sb_version_hascrc(&mp->m_sb))
516 return;
517
Eric Sandeence5028c2014-02-27 15:23:10 +1100518 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000519 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100520 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000521 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100522
523 if (bp->b_error)
524 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100525}
526
527static void
528xfs_agfl_write_verify(
529 struct xfs_buf *bp)
530{
531 struct xfs_mount *mp = bp->b_target->bt_mount;
532 struct xfs_buf_log_item *bip = bp->b_fspriv;
533
534 /* no verification of non-crc AGFLs */
535 if (!xfs_sb_version_hascrc(&mp->m_sb))
536 return;
537
538 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000539 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100540 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100541 return;
542 }
543
544 if (bip)
545 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
546
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100547 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100548}
549
Dave Chinner1813dd62012-11-14 17:54:40 +1100550const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100551 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100552 .verify_read = xfs_agfl_read_verify,
553 .verify_write = xfs_agfl_write_verify,
554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * Read in the allocation group free block array.
558 */
559STATIC int /* error */
560xfs_alloc_read_agfl(
561 xfs_mount_t *mp, /* mount point structure */
562 xfs_trans_t *tp, /* transaction pointer */
563 xfs_agnumber_t agno, /* allocation group number */
564 xfs_buf_t **bpp) /* buffer for the ag free block array */
565{
566 xfs_buf_t *bp; /* return value */
567 int error;
568
569 ASSERT(agno != NULLAGNUMBER);
570 error = xfs_trans_read_buf(
571 mp, tp, mp->m_ddev_targp,
572 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100573 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (error)
575 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000576 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *bpp = bp;
578 return 0;
579}
580
Christoph Hellwigecb69282011-03-04 12:59:55 +0000581STATIC int
582xfs_alloc_update_counters(
583 struct xfs_trans *tp,
584 struct xfs_perag *pag,
585 struct xfs_buf *agbp,
586 long len)
587{
588 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
589
590 pag->pagf_freeblks += len;
591 be32_add_cpu(&agf->agf_freeblks, len);
592
593 xfs_trans_agblocks_delta(tp, len);
594 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
595 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000596 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000597
598 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
599 return 0;
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * Allocation group level functions.
604 */
605
606/*
607 * Allocate a variable extent in the allocation group agno.
608 * Type and bno are used to determine where in the allocation group the
609 * extent will start.
610 * Extent's length (returned in *len) will be between minlen and maxlen,
611 * and of the form k * prod + mod unless there's nothing that large.
612 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
613 */
614STATIC int /* error */
615xfs_alloc_ag_vextent(
616 xfs_alloc_arg_t *args) /* argument structure for allocation */
617{
618 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 ASSERT(args->minlen > 0);
621 ASSERT(args->maxlen > 0);
622 ASSERT(args->minlen <= args->maxlen);
623 ASSERT(args->mod < args->prod);
624 ASSERT(args->alignment > 0);
625 /*
626 * Branch to correct routine based on the type.
627 */
628 args->wasfromfl = 0;
629 switch (args->type) {
630 case XFS_ALLOCTYPE_THIS_AG:
631 error = xfs_alloc_ag_vextent_size(args);
632 break;
633 case XFS_ALLOCTYPE_NEAR_BNO:
634 error = xfs_alloc_ag_vextent_near(args);
635 break;
636 case XFS_ALLOCTYPE_THIS_BNO:
637 error = xfs_alloc_ag_vextent_exact(args);
638 break;
639 default:
640 ASSERT(0);
641 /* NOTREACHED */
642 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000643
644 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Christoph Hellwigecb69282011-03-04 12:59:55 +0000647 ASSERT(args->len >= args->minlen);
648 ASSERT(args->len <= args->maxlen);
649 ASSERT(!args->wasfromfl || !args->isfl);
650 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Christoph Hellwigecb69282011-03-04 12:59:55 +0000652 if (!args->wasfromfl) {
653 error = xfs_alloc_update_counters(args->tp, args->pag,
654 args->agbp,
655 -((long)(args->len)));
656 if (error)
657 return error;
658
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000659 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000660 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000662
663 if (!args->isfl) {
664 xfs_trans_mod_sb(args->tp, args->wasdel ?
665 XFS_TRANS_SB_RES_FDBLOCKS :
666 XFS_TRANS_SB_FDBLOCKS,
667 -((long)(args->len)));
668 }
669
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100670 XFS_STATS_INC(args->mp, xs_allocx);
671 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000672 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Allocate a variable extent at exactly agno/bno.
677 * Extent's length (returned in *len) will be between minlen and maxlen,
678 * and of the form k * prod + mod unless there's nothing that large.
679 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
680 */
681STATIC int /* error */
682xfs_alloc_ag_vextent_exact(
683 xfs_alloc_arg_t *args) /* allocation argument structure */
684{
685 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
686 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int error;
688 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000690 xfs_agblock_t tbno; /* start block of trimmed extent */
691 xfs_extlen_t tlen; /* length of trimmed extent */
692 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * Allocate/initialize a cursor for the by-number freespace btree.
699 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100700 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000701 args->agno, XFS_BTNUM_BNO);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
704 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
705 * Look for the closest free block <= bno, it must contain bno
706 * if any free block does.
707 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000708 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
709 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000711 if (!i)
712 goto not_found;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Grab the freespace record.
716 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000717 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
718 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100720 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000724 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000726 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000727
728 /*
729 * Give up if the start of the extent is busy, or the freespace isn't
730 * long enough for the minimum request.
731 */
732 if (tbno > args->agbno)
733 goto not_found;
734 if (tlen < args->minlen)
735 goto not_found;
736 tend = tbno + tlen;
737 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000738 goto not_found;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /*
741 * End of extent will be smaller of the freespace end and the
742 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000743 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * Fix the length according to mod and prod if given.
745 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000746 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
747 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000749 if (!xfs_alloc_fix_minleft(args))
750 goto not_found;
751
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000752 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000755 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * Allocate/initialize a cursor for the by-size btree.
757 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100758 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
759 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100761 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000762 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
763 args->len, XFSA_FIXUP_BNO_OK);
764 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
766 goto error0;
767 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
770 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000773 trace_xfs_alloc_exact_done(args);
774 return 0;
775
776not_found:
777 /* Didn't find it, return null. */
778 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
779 args->agbno = NULLAGBLOCK;
780 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
782
783error0:
784 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000785 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return error;
787}
788
789/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000790 * Search the btree in a given direction via the search cursor and compare
791 * the records found against the good extent we've already found.
792 */
793STATIC int
794xfs_alloc_find_best_extent(
795 struct xfs_alloc_arg *args, /* allocation argument structure */
796 struct xfs_btree_cur **gcur, /* good cursor */
797 struct xfs_btree_cur **scur, /* searching cursor */
798 xfs_agblock_t gdiff, /* difference for search comparison */
799 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000800 xfs_extlen_t *slen, /* extent length */
801 xfs_agblock_t *sbnoa, /* aligned extent found by search */
802 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000803 int dir) /* 0 = search right, 1 = search left */
804{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000805 xfs_agblock_t new;
806 xfs_agblock_t sdiff;
807 int error;
808 int i;
809
810 /* The good extent is perfect, no need to search. */
811 if (!gdiff)
812 goto out_use_good;
813
814 /*
815 * Look until we find a better one, run out of space or run off the end.
816 */
817 do {
818 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
819 if (error)
820 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100821 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000822 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000823
824 /*
825 * The good extent is closer than this one.
826 */
827 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000828 if (*sbnoa > args->max_agbno)
829 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000830 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000831 goto out_use_good;
832 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000833 if (*sbnoa < args->min_agbno)
834 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000835 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000836 goto out_use_good;
837 }
838
839 /*
840 * Same distance, compare length and pick the best.
841 */
842 if (*slena >= args->minlen) {
843 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
844 xfs_alloc_fix_len(args);
845
846 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200847 args->alignment,
848 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000849 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000850
851 /*
852 * Choose closer size and invalidate other cursor.
853 */
854 if (sdiff < gdiff)
855 goto out_use_search;
856 goto out_use_good;
857 }
858
859 if (!dir)
860 error = xfs_btree_increment(*scur, 0, &i);
861 else
862 error = xfs_btree_decrement(*scur, 0, &i);
863 if (error)
864 goto error0;
865 } while (i);
866
867out_use_good:
868 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
869 *scur = NULL;
870 return 0;
871
872out_use_search:
873 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
874 *gcur = NULL;
875 return 0;
876
877error0:
878 /* caller invalidates cursors */
879 return error;
880}
881
882/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Allocate a variable extent near bno in the allocation group agno.
884 * Extent's length (returned in len) will be between minlen and maxlen,
885 * and of the form k * prod + mod unless there's nothing that large.
886 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
887 */
888STATIC int /* error */
889xfs_alloc_ag_vextent_near(
890 xfs_alloc_arg_t *args) /* allocation argument structure */
891{
892 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
893 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
894 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 xfs_agblock_t gtbno; /* start bno of right side entry */
896 xfs_agblock_t gtbnoa; /* aligned ... */
897 xfs_extlen_t gtdiff; /* difference to right side entry */
898 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000899 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 xfs_agblock_t gtnew; /* useful start bno of right side */
901 int error; /* error code */
902 int i; /* result code, temporary */
903 int j; /* result code, temporary */
904 xfs_agblock_t ltbno; /* start bno of left side entry */
905 xfs_agblock_t ltbnoa; /* aligned ... */
906 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000908 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xfs_agblock_t ltnew; /* useful start bno of left side */
910 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000911 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000912#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Randomly don't execute the first algorithm.
915 */
916 int dofirst; /* set to do first algorithm */
917
Akinobu Mitaecb34032013-03-04 21:58:20 +0900918 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000920
Brian Fosterbfe46d42015-05-29 08:53:00 +1000921 /* handle unitialized agbno range so caller doesn't have to */
922 if (!args->min_agbno && !args->max_agbno)
923 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
924 ASSERT(args->min_agbno <= args->max_agbno);
925
926 /* clamp agbno to the range if it's outside */
927 if (args->agbno < args->min_agbno)
928 args->agbno = args->min_agbno;
929 if (args->agbno > args->max_agbno)
930 args->agbno = args->max_agbno;
931
Christoph Hellwige26f0502011-04-24 19:06:15 +0000932restart:
933 bno_cur_lt = NULL;
934 bno_cur_gt = NULL;
935 ltlen = 0;
936 gtlena = 0;
937 ltlena = 0;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
940 * Get a cursor for the by-size btree.
941 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100942 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
943 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /*
946 * See if there are any free extents as big as maxlen.
947 */
948 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
949 goto error0;
950 /*
951 * If none, then pick up the last entry in the tree unless the
952 * tree is empty.
953 */
954 if (!i) {
955 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
956 &ltlen, &i)))
957 goto error0;
958 if (i == 0 || ltlen == 0) {
959 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000960 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962 }
963 ASSERT(i == 1);
964 }
965 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * First algorithm.
969 * If the requested extent is large wrt the freespaces available
970 * in this a.g., then the cursor will be pointing to a btree entry
971 * near the right edge of the tree. If it's in the last btree leaf
972 * block, then we just examine all the entries in that block
973 * that are big enough, and pick the best one.
974 * This is written as a while loop so we can break out of it,
975 * but we never loop back to the top.
976 */
977 while (xfs_btree_islastblock(cnt_cur, 0)) {
978 xfs_extlen_t bdiff;
979 int besti=0;
980 xfs_extlen_t blen=0;
981 xfs_agblock_t bnew=0;
982
Dave Chinner63d20d62013-08-12 20:49:50 +1000983#ifdef DEBUG
984 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986#endif
987 /*
988 * Start from the entry that lookup found, sequence through
989 * all larger free blocks. If we're actually pointing at a
990 * record smaller than maxlen, go to the start of this block,
991 * and skip all those smaller than minlen.
992 */
993 if (ltlen || args->alignment > 1) {
994 cnt_cur->bc_ptrs[0] = 1;
995 do {
996 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
997 &ltlen, &i)))
998 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100999 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (ltlen >= args->minlen)
1001 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001002 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 goto error0;
1004 } while (i);
1005 ASSERT(ltlen >= args->minlen);
1006 if (!i)
1007 break;
1008 }
1009 i = cnt_cur->bc_ptrs[0];
1010 for (j = 1, blen = 0, bdiff = 0;
1011 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001012 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * For each entry, decide if it's better than
1015 * the previous best entry.
1016 */
1017 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1018 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001019 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001020 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1021 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001022 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001024 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1025 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1027 xfs_alloc_fix_len(args);
1028 ASSERT(args->len >= args->minlen);
1029 if (args->len < blen)
1030 continue;
1031 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001032 args->alignment, args->userdata, ltbnoa,
1033 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (ltnew != NULLAGBLOCK &&
1035 (args->len > blen || ltdiff < bdiff)) {
1036 bdiff = ltdiff;
1037 bnew = ltnew;
1038 blen = args->len;
1039 besti = cnt_cur->bc_ptrs[0];
1040 }
1041 }
1042 /*
1043 * It didn't work. We COULD be in a case where
1044 * there's a good record somewhere, so try again.
1045 */
1046 if (blen == 0)
1047 break;
1048 /*
1049 * Point at the best entry, and retrieve it again.
1050 */
1051 cnt_cur->bc_ptrs[0] = besti;
1052 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1053 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001054 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001055 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 args->len = blen;
1057 if (!xfs_alloc_fix_minleft(args)) {
1058 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001059 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061 }
1062 blen = args->len;
1063 /*
1064 * We are allocating starting at bnew for blen blocks.
1065 */
1066 args->agbno = bnew;
1067 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001068 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * Set up a cursor for the by-bno tree.
1071 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001072 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1073 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Fix up the btree entries.
1076 */
1077 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1078 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1079 goto error0;
1080 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1081 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001082
1083 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085 }
1086 /*
1087 * Second algorithm.
1088 * Search in the by-bno tree to the left and to the right
1089 * simultaneously, until in each case we find a space big enough,
1090 * or run into the edge of the tree. When we run into the edge,
1091 * we deallocate that cursor.
1092 * If both searches succeed, we compare the two spaces and pick
1093 * the better one.
1094 * With alignment, it's possible for both to fail; the upper
1095 * level algorithm that picks allocation groups for allocations
1096 * is not supposed to do this.
1097 */
1098 /*
1099 * Allocate and initialize the cursor for the leftward search.
1100 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001101 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1102 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * Lookup <= bno to find the leftward search's starting point.
1105 */
1106 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1107 goto error0;
1108 if (!i) {
1109 /*
1110 * Didn't find anything; use this cursor for the rightward
1111 * search.
1112 */
1113 bno_cur_gt = bno_cur_lt;
1114 bno_cur_lt = NULL;
1115 }
1116 /*
1117 * Found something. Duplicate the cursor for the rightward search.
1118 */
1119 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1120 goto error0;
1121 /*
1122 * Increment the cursor, so we will point at the entry just right
1123 * of the leftward entry if any, or to the leftmost entry.
1124 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001125 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto error0;
1127 if (!i) {
1128 /*
1129 * It failed, there are no rightward entries.
1130 */
1131 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1132 bno_cur_gt = NULL;
1133 }
1134 /*
1135 * Loop going left with the leftward cursor, right with the
1136 * rightward cursor, until either both directions give up or
1137 * we find an entry at least as big as minlen.
1138 */
1139 do {
1140 if (bno_cur_lt) {
1141 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1142 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001143 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001144 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1145 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001146 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001148 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001150 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 xfs_btree_del_cursor(bno_cur_lt,
1152 XFS_BTREE_NOERROR);
1153 bno_cur_lt = NULL;
1154 }
1155 }
1156 if (bno_cur_gt) {
1157 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1158 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001159 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001160 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1161 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001162 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001164 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001166 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 xfs_btree_del_cursor(bno_cur_gt,
1168 XFS_BTREE_NOERROR);
1169 bno_cur_gt = NULL;
1170 }
1171 }
1172 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Got both cursors still active, need to find better entry.
1176 */
1177 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (ltlena >= args->minlen) {
1179 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001180 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1183 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001184 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001185 args->alignment, args->userdata, ltbnoa,
1186 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001187
1188 error = xfs_alloc_find_best_extent(args,
1189 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001190 ltdiff, &gtbno, &gtlen,
1191 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001192 0 /* search right */);
1193 } else {
1194 ASSERT(gtlena >= args->minlen);
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001197 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
1199 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1200 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001201 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001202 args->alignment, args->userdata, gtbnoa,
1203 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001204
1205 error = xfs_alloc_find_best_extent(args,
1206 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001207 gtdiff, &ltbno, &ltlen,
1208 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001209 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001211
1212 if (error)
1213 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * If we couldn't get anything, give up.
1218 */
1219 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001220 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1221
Christoph Hellwige26f0502011-04-24 19:06:15 +00001222 if (!forced++) {
1223 trace_xfs_alloc_near_busy(args);
1224 xfs_log_force(args->mp, XFS_LOG_SYNC);
1225 goto restart;
1226 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001227 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 args->agbno = NULLAGBLOCK;
1229 return 0;
1230 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * At this point we have selected a freespace entry, either to the
1234 * left or to the right. If it's on the right, copy all the
1235 * useful variables to the "left" set so we only have one
1236 * copy of this code.
1237 */
1238 if (bno_cur_gt) {
1239 bno_cur_lt = bno_cur_gt;
1240 bno_cur_gt = NULL;
1241 ltbno = gtbno;
1242 ltbnoa = gtbnoa;
1243 ltlen = gtlen;
1244 ltlena = gtlena;
1245 j = 1;
1246 } else
1247 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * Fix up the length and compute the useful address.
1251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1253 xfs_alloc_fix_len(args);
1254 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001255 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1257 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1258 return 0;
1259 }
1260 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001261 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001262 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001264 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001265 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001266 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1270 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1271 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001272
1273 if (j)
1274 trace_xfs_alloc_near_greater(args);
1275 else
1276 trace_xfs_alloc_near_lesser(args);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1279 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1280 return 0;
1281
1282 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001283 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (cnt_cur != NULL)
1285 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1286 if (bno_cur_lt != NULL)
1287 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1288 if (bno_cur_gt != NULL)
1289 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1290 return error;
1291}
1292
1293/*
1294 * Allocate a variable extent anywhere in the allocation group agno.
1295 * Extent's length (returned in len) will be between minlen and maxlen,
1296 * and of the form k * prod + mod unless there's nothing that large.
1297 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1298 */
1299STATIC int /* error */
1300xfs_alloc_ag_vextent_size(
1301 xfs_alloc_arg_t *args) /* allocation argument structure */
1302{
1303 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1304 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1305 int error; /* error result */
1306 xfs_agblock_t fbno; /* start of found freespace */
1307 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int i; /* temp status variable */
1309 xfs_agblock_t rbno; /* returned block number */
1310 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001311 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Christoph Hellwige26f0502011-04-24 19:06:15 +00001313restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
1315 * Allocate and initialize a cursor for the by-size btree.
1316 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001317 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1318 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Look for an entry >= maxlen+alignment-1 blocks.
1323 */
1324 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1325 args->maxlen + args->alignment - 1, &i)))
1326 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001329 * If none or we have busy extents that we cannot allocate from, then
1330 * we have to settle for a smaller extent. In the case that there are
1331 * no large extents, this will return the last entry in the tree unless
1332 * the tree is empty. In the case that there are only busy large
1333 * extents, this will return the largest small extent unless there
1334 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001336 if (!i || forced > 1) {
1337 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1338 &fbno, &flen, &i);
1339 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 goto error0;
1341 if (i == 0 || flen == 0) {
1342 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001343 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345 }
1346 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001347 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1348 } else {
1349 /*
1350 * Search for a non-busy extent that is large enough.
1351 * If we are at low space, don't check, or if we fall of
1352 * the end of the btree, turn off the busy check and
1353 * restart.
1354 */
1355 for (;;) {
1356 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1357 if (error)
1358 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001359 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001360
1361 xfs_alloc_compute_aligned(args, fbno, flen,
1362 &rbno, &rlen);
1363
1364 if (rlen >= args->maxlen)
1365 break;
1366
1367 error = xfs_btree_increment(cnt_cur, 0, &i);
1368 if (error)
1369 goto error0;
1370 if (i == 0) {
1371 /*
1372 * Our only valid extents must have been busy.
1373 * Make it unbusy by forcing the log out and
1374 * retrying. If we've been here before, forcing
1375 * the log isn't making the extents available,
1376 * which means they have probably been freed in
1377 * this transaction. In that case, we have to
1378 * give up on them and we'll attempt a minlen
1379 * allocation the next time around.
1380 */
1381 xfs_btree_del_cursor(cnt_cur,
1382 XFS_BTREE_NOERROR);
1383 trace_xfs_alloc_size_busy(args);
1384 if (!forced++)
1385 xfs_log_force(args->mp, XFS_LOG_SYNC);
1386 goto restart;
1387 }
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * In the first case above, we got the last entry in the
1393 * by-size btree. Now we check to see if the space hits maxlen
1394 * once aligned; if not, we search left for something better.
1395 * This can't happen in the second case above.
1396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001398 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1400 if (rlen < args->maxlen) {
1401 xfs_agblock_t bestfbno;
1402 xfs_extlen_t bestflen;
1403 xfs_agblock_t bestrbno;
1404 xfs_extlen_t bestrlen;
1405
1406 bestrlen = rlen;
1407 bestrbno = rbno;
1408 bestflen = flen;
1409 bestfbno = fbno;
1410 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001411 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 goto error0;
1413 if (i == 0)
1414 break;
1415 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1416 &i)))
1417 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001418 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (flen < bestrlen)
1420 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001421 xfs_alloc_compute_aligned(args, fbno, flen,
1422 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001424 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 (rlen <= flen && rbno + rlen <= fbno + flen),
1426 error0);
1427 if (rlen > bestrlen) {
1428 bestrlen = rlen;
1429 bestrbno = rbno;
1430 bestflen = flen;
1431 bestfbno = fbno;
1432 if (rlen == args->maxlen)
1433 break;
1434 }
1435 }
1436 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1437 &i)))
1438 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001439 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 rlen = bestrlen;
1441 rbno = bestrbno;
1442 flen = bestflen;
1443 fbno = bestfbno;
1444 }
1445 args->wasfromfl = 0;
1446 /*
1447 * Fix up the length.
1448 */
1449 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001450 if (rlen < args->minlen) {
1451 if (!forced++) {
1452 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1453 trace_xfs_alloc_size_busy(args);
1454 xfs_log_force(args->mp, XFS_LOG_SYNC);
1455 goto restart;
1456 }
1457 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001459 xfs_alloc_fix_len(args);
1460
1461 if (!xfs_alloc_fix_minleft(args))
1462 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001464 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /*
1466 * Allocate and initialize a cursor for the by-block tree.
1467 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001468 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1469 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1471 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1472 goto error0;
1473 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1474 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1475 cnt_cur = bno_cur = NULL;
1476 args->len = rlen;
1477 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001478 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001480 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001482 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484
1485error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001486 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (cnt_cur)
1488 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1489 if (bno_cur)
1490 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1491 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001492
1493out_nominleft:
1494 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1495 trace_xfs_alloc_size_nominleft(args);
1496 args->agbno = NULLAGBLOCK;
1497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/*
1501 * Deal with the case where only small freespaces remain.
1502 * Either return the contents of the last freespace record,
1503 * or allocate space from the freelist if there is nothing in the tree.
1504 */
1505STATIC int /* error */
1506xfs_alloc_ag_vextent_small(
1507 xfs_alloc_arg_t *args, /* allocation argument structure */
1508 xfs_btree_cur_t *ccur, /* by-size cursor */
1509 xfs_agblock_t *fbnop, /* result block number */
1510 xfs_extlen_t *flenp, /* result length */
1511 int *stat) /* status: 0-freelist, 1-normal/none */
1512{
1513 int error;
1514 xfs_agblock_t fbno;
1515 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int i;
1517
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001518 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto error0;
1520 if (i) {
1521 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1522 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001523 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 /*
1526 * Nothing in the btree, try the freelist. Make sure
1527 * to respect minleft even when pulling from the
1528 * freelist.
1529 */
1530 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001531 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1532 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001533 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1534 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto error0;
1536 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001537 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001538 args->userdata);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (args->userdata) {
1541 xfs_buf_t *bp;
1542
1543 bp = xfs_btree_get_bufs(args->mp, args->tp,
1544 args->agno, fbno, 0);
1545 xfs_trans_binval(args->tp, bp);
1546 }
1547 args->len = 1;
1548 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001549 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001551 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 error0);
1553 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 *stat = 0;
1556 return 0;
1557 }
1558 /*
1559 * Nothing in the freelist.
1560 */
1561 else
1562 flen = 0;
1563 }
1564 /*
1565 * Can't allocate from the freelist for some reason.
1566 */
Nathan Scottd432c802006-09-28 11:03:44 +10001567 else {
1568 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * Can't do the allocation, give up.
1573 */
1574 if (flen < args->minlen) {
1575 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001576 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 flen = 0;
1578 }
1579 *fbnop = fbno;
1580 *flenp = flen;
1581 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001582 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return 0;
1584
1585error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001586 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return error;
1588}
1589
1590/*
1591 * Free the extent starting at agno/bno for length.
1592 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10001593STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594xfs_free_ag_extent(
Darrick J. Wong340785c2016-08-03 11:33:42 +10001595 xfs_trans_t *tp,
1596 xfs_buf_t *agbp,
1597 xfs_agnumber_t agno,
1598 xfs_agblock_t bno,
1599 xfs_extlen_t len,
1600 struct xfs_owner_info *oinfo,
1601 int isfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1604 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1605 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 xfs_agblock_t gtbno; /* start of right neighbor block */
1607 xfs_extlen_t gtlen; /* length of right neighbor block */
1608 int haveleft; /* have a left neighbor block */
1609 int haveright; /* have a right neighbor block */
1610 int i; /* temp, result code */
1611 xfs_agblock_t ltbno; /* start of left neighbor block */
1612 xfs_extlen_t ltlen; /* length of left neighbor block */
1613 xfs_mount_t *mp; /* mount point struct for filesystem */
1614 xfs_agblock_t nbno; /* new starting block of freespace */
1615 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001616 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 mp = tp->t_mountp;
1619 /*
1620 * Allocate and initialize a cursor for the by-block btree.
1621 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001622 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 cnt_cur = NULL;
1624 /*
1625 * Look for a neighboring block on the left (lower block numbers)
1626 * that is contiguous with this space.
1627 */
1628 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1629 goto error0;
1630 if (haveleft) {
1631 /*
1632 * There is a block to our left.
1633 */
1634 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1635 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001636 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /*
1638 * It's not contiguous, though.
1639 */
1640 if (ltbno + ltlen < bno)
1641 haveleft = 0;
1642 else {
1643 /*
1644 * If this failure happens the request to free this
1645 * space was invalid, it's (partly) already free.
1646 * Very bad.
1647 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001648 XFS_WANT_CORRUPTED_GOTO(mp,
1649 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 }
1652 /*
1653 * Look for a neighboring block on the right (higher block numbers)
1654 * that is contiguous with this space.
1655 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001656 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto error0;
1658 if (haveright) {
1659 /*
1660 * There is a block to our right.
1661 */
1662 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1663 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001664 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * It's not contiguous, though.
1667 */
1668 if (bno + len < gtbno)
1669 haveright = 0;
1670 else {
1671 /*
1672 * If this failure happens the request to free this
1673 * space was invalid, it's (partly) already free.
1674 * Very bad.
1675 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001676 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 }
1679 /*
1680 * Now allocate and initialize a cursor for the by-size tree.
1681 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001682 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Have both left and right contiguous neighbors.
1685 * Merge all three into a single free block.
1686 */
1687 if (haveleft && haveright) {
1688 /*
1689 * Delete the old by-size entry on the left.
1690 */
1691 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1692 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001693 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001694 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001696 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * Delete the old by-size entry on the right.
1699 */
1700 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1701 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001702 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001703 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001705 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * Delete the old by-block entry for the right block.
1708 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001709 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001711 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
1713 * Move the by-block cursor back to the left neighbor.
1714 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001715 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001717 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718#ifdef DEBUG
1719 /*
1720 * Check that this is the right record: delete didn't
1721 * mangle the cursor.
1722 */
1723 {
1724 xfs_agblock_t xxbno;
1725 xfs_extlen_t xxlen;
1726
1727 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1728 &i)))
1729 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001730 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 i == 1 && xxbno == ltbno && xxlen == ltlen,
1732 error0);
1733 }
1734#endif
1735 /*
1736 * Update remaining by-block entry to the new, joined block.
1737 */
1738 nbno = ltbno;
1739 nlen = len + ltlen + gtlen;
1740 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1741 goto error0;
1742 }
1743 /*
1744 * Have only a left contiguous neighbor.
1745 * Merge it together with the new freespace.
1746 */
1747 else if (haveleft) {
1748 /*
1749 * Delete the old by-size entry on the left.
1750 */
1751 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1752 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001753 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001754 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001756 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * Back up the by-block cursor to the left neighbor, and
1759 * update its length.
1760 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001761 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001763 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 nbno = ltbno;
1765 nlen = len + ltlen;
1766 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1767 goto error0;
1768 }
1769 /*
1770 * Have only a right contiguous neighbor.
1771 * Merge it together with the new freespace.
1772 */
1773 else if (haveright) {
1774 /*
1775 * Delete the old by-size entry on the right.
1776 */
1777 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1778 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001780 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Update the starting block and length of the right
1785 * neighbor in the by-block tree.
1786 */
1787 nbno = bno;
1788 nlen = len + gtlen;
1789 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1790 goto error0;
1791 }
1792 /*
1793 * No contiguous neighbors.
1794 * Insert the new freespace into the by-block tree.
1795 */
1796 else {
1797 nbno = bno;
1798 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001799 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001801 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1804 bno_cur = NULL;
1805 /*
1806 * In all cases we need to insert the new freespace in the by-size tree.
1807 */
1808 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1809 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001810 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001811 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001813 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1815 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /*
1818 * Update the freespace totals in the ag and superblock.
1819 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001820 pag = xfs_perag_get(mp, agno);
1821 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1822 xfs_perag_put(pag);
1823 if (error)
1824 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Christoph Hellwigecb69282011-03-04 12:59:55 +00001826 if (!isfl)
1827 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001828 XFS_STATS_INC(mp, xs_freex);
1829 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001830
1831 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834
1835 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001836 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (bno_cur)
1838 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1839 if (cnt_cur)
1840 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1841 return error;
1842}
1843
1844/*
1845 * Visible (exported) allocation/free functions.
1846 * Some of these are used just by xfs_alloc_btree.c and this file.
1847 */
1848
1849/*
1850 * Compute and fill in value of m_ag_maxlevels.
1851 */
1852void
1853xfs_alloc_compute_maxlevels(
1854 xfs_mount_t *mp) /* file system mount structure */
1855{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10001856 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1857 (mp->m_sb.sb_agblocks + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001861 * Find the length of the longest extent in an AG.
1862 */
1863xfs_extlen_t
1864xfs_alloc_longest_free_extent(
1865 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001866 struct xfs_perag *pag,
1867 xfs_extlen_t need)
Dave Chinner6cc87642009-03-16 08:29:46 +01001868{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001869 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001870
Dave Chinner6cc87642009-03-16 08:29:46 +01001871 if (need > pag->pagf_flcount)
1872 delta = need - pag->pagf_flcount;
1873
1874 if (pag->pagf_longest > delta)
1875 return pag->pagf_longest - delta;
1876 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1877}
1878
Dave Chinner496817b2015-06-22 10:13:30 +10001879unsigned int
1880xfs_alloc_min_freelist(
1881 struct xfs_mount *mp,
1882 struct xfs_perag *pag)
1883{
1884 unsigned int min_free;
1885
1886 /* space needed by-bno freespace btree */
1887 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1888 mp->m_ag_maxlevels);
1889 /* space needed by-size freespace btree */
1890 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1891 mp->m_ag_maxlevels);
1892
1893 return min_free;
1894}
1895
Dave Chinner6cc87642009-03-16 08:29:46 +01001896/*
Dave Chinner72d55282015-06-22 10:04:42 +10001897 * Check if the operation we are fixing up the freelist for should go ahead or
1898 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1899 * is dependent on whether the size and shape of free space available will
1900 * permit the requested allocation to take place.
1901 */
1902static bool
1903xfs_alloc_space_available(
1904 struct xfs_alloc_arg *args,
1905 xfs_extlen_t min_free,
1906 int flags)
1907{
1908 struct xfs_perag *pag = args->pag;
1909 xfs_extlen_t longest;
1910 int available;
1911
1912 if (flags & XFS_ALLOC_FLAG_FREEING)
1913 return true;
1914
1915 /* do we have enough contiguous free space for the allocation? */
1916 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free);
1917 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
1918 return false;
1919
1920 /* do have enough free space remaining for the allocation? */
1921 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
1922 min_free - args->total);
1923 if (available < (int)args->minleft)
1924 return false;
1925
1926 return true;
1927}
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929/*
1930 * Decide whether to use this allocation group for this allocation.
1931 * If so, fix up the btree freelist's size.
1932 */
Darrick J. Wong2e9101d2016-01-04 16:10:42 +11001933int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10001935 struct xfs_alloc_arg *args, /* allocation argument structure */
1936 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Dave Chinner396503f2015-06-22 10:13:19 +10001938 struct xfs_mount *mp = args->mp;
1939 struct xfs_perag *pag = args->pag;
1940 struct xfs_trans *tp = args->tp;
1941 struct xfs_buf *agbp = NULL;
1942 struct xfs_buf *agflbp = NULL;
1943 struct xfs_alloc_arg targs; /* local allocation arguments */
1944 xfs_agblock_t bno; /* freelist block */
1945 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10001946 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10001949 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1950 if (error)
1951 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001953 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1954 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001955 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Dave Chinner396503f2015-06-22 10:13:19 +10001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001959 /*
Dave Chinner396503f2015-06-22 10:13:19 +10001960 * If this is a metadata preferred pag and we are user data then try
1961 * somewhere else if we are not being asked to try harder at this
1962 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001964 if (pag->pagf_metadata && args->userdata &&
1965 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1966 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001967 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
Dave Chinner496817b2015-06-22 10:13:30 +10001970 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001971 if (!xfs_alloc_space_available(args, need, flags))
1972 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Get the a.g. freespace buffer.
1976 * Can fail if we're not blocking on locks, and it's held.
1977 */
Dave Chinner396503f2015-06-22 10:13:19 +10001978 if (!agbp) {
1979 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1980 if (error)
1981 goto out_no_agbp;
1982 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001983 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1984 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001985 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10001988
Dave Chinner50adbcb2015-06-22 10:04:31 +10001989 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10001990 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001991 if (!xfs_alloc_space_available(args, need, flags))
1992 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
1995 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10001996 *
Dave Chinner396503f2015-06-22 10:13:19 +10001997 * Note that from this point onwards, we will always release the agf and
1998 * agfl buffers on error. This handles the case where we error out and
1999 * the buffers are clean or may not have been joined to the transaction
2000 * and hence need to be released manually. If they have been joined to
2001 * the transaction, then xfs_trans_brelse() will handle them
2002 * appropriately based on the recursion count and dirty state of the
2003 * buffer.
2004 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002005 * XXX (dgc): When we have lots of free space, does this buy us
2006 * anything other than extra overhead when we need to put more blocks
2007 * back on the free list? Maybe we should only do this when space is
2008 * getting low or the AGFL is more than half full?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002010 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002011 while (pag->pagf_flcount > need) {
2012 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
David Chinner92821e22007-05-24 15:26:31 +10002014 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2015 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002016 goto out_agbp_relse;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002017 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
2018 &targs.oinfo, 1);
Dave Chinner50adbcb2015-06-22 10:04:31 +10002019 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002020 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2022 xfs_trans_binval(tp, bp);
2023 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002024
Mark Tinguelya0041682012-09-20 13:16:45 -05002025 memset(&targs, 0, sizeof(targs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 targs.tp = tp;
2027 targs.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +10002028 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 targs.agbp = agbp;
2030 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2032 targs.type = XFS_ALLOCTYPE_THIS_AG;
2033 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002034 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2035 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002036 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002037
2038 /* Make the freelist longer if it's too short. */
2039 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002041 targs.maxlen = need - pag->pagf_flcount;
2042
2043 /* Allocate as many blocks as possible at once. */
2044 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002045 if (error)
2046 goto out_agflbp_relse;
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
2049 * Stop if we run out. Won't happen if callers are obeying
2050 * the restrictions correctly. Can happen for free calls
2051 * on a completely full ag.
2052 */
Yingping Lud210a282006-06-09 14:55:18 +10002053 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002054 if (flags & XFS_ALLOC_FLAG_FREEING)
2055 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002056 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /*
2059 * Put each allocated block on the list.
2060 */
2061 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002062 error = xfs_alloc_put_freelist(tp, agbp,
2063 agflbp, bno, 0);
2064 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002065 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002068 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 args->agbp = agbp;
2070 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002071
2072out_agflbp_relse:
2073 xfs_trans_brelse(tp, agflbp);
2074out_agbp_relse:
2075 if (agbp)
2076 xfs_trans_brelse(tp, agbp);
2077out_no_agbp:
2078 args->agbp = NULL;
2079 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
2082/*
2083 * Get a block from the freelist.
2084 * Returns with the buffer for the block gotten.
2085 */
2086int /* error */
2087xfs_alloc_get_freelist(
2088 xfs_trans_t *tp, /* transaction pointer */
2089 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002090 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2091 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2095 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002096 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 int error;
David Chinner92821e22007-05-24 15:26:31 +10002098 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002099 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 xfs_perag_t *pag; /* per allocation group data */
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /*
2103 * Freelist is empty, give up.
2104 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002105 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!agf->agf_flcount) {
2107 *bnop = NULLAGBLOCK;
2108 return 0;
2109 }
2110 /*
2111 * Read the array of free blocks.
2112 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002113 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2114 &agflbp);
2115 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002117
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /*
2120 * Get the block number and update the data structures.
2121 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002122 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2123 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002124 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002126 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002128
2129 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002130 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 xfs_trans_agflist_delta(tp, -1);
2132 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002133 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002134
2135 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2136 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002137 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002138 pag->pagf_btreeblks++;
2139 logflags |= XFS_AGF_BTREEBLKS;
2140 }
2141
David Chinner92821e22007-05-24 15:26:31 +10002142 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 *bnop = bno;
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146}
2147
2148/*
2149 * Log the given fields from the agf structure.
2150 */
2151void
2152xfs_alloc_log_agf(
2153 xfs_trans_t *tp, /* transaction pointer */
2154 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2155 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2156{
2157 int first; /* first byte offset */
2158 int last; /* last byte offset */
2159 static const short offsets[] = {
2160 offsetof(xfs_agf_t, agf_magicnum),
2161 offsetof(xfs_agf_t, agf_versionnum),
2162 offsetof(xfs_agf_t, agf_seqno),
2163 offsetof(xfs_agf_t, agf_length),
2164 offsetof(xfs_agf_t, agf_roots[0]),
2165 offsetof(xfs_agf_t, agf_levels[0]),
2166 offsetof(xfs_agf_t, agf_flfirst),
2167 offsetof(xfs_agf_t, agf_fllast),
2168 offsetof(xfs_agf_t, agf_flcount),
2169 offsetof(xfs_agf_t, agf_freeblks),
2170 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002171 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002172 offsetof(xfs_agf_t, agf_uuid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 sizeof(xfs_agf_t)
2174 };
2175
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002176 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2177
Dave Chinner61fe1352013-04-03 16:11:30 +11002178 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2181 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2182}
2183
2184/*
2185 * Interface for inode allocation to force the pag data to be initialized.
2186 */
2187int /* error */
2188xfs_alloc_pagf_init(
2189 xfs_mount_t *mp, /* file system mount structure */
2190 xfs_trans_t *tp, /* transaction pointer */
2191 xfs_agnumber_t agno, /* allocation group number */
2192 int flags) /* XFS_ALLOC_FLAGS_... */
2193{
2194 xfs_buf_t *bp;
2195 int error;
2196
2197 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2198 return error;
2199 if (bp)
2200 xfs_trans_brelse(tp, bp);
2201 return 0;
2202}
2203
2204/*
2205 * Put the block on the freelist for the allocation group.
2206 */
2207int /* error */
2208xfs_alloc_put_freelist(
2209 xfs_trans_t *tp, /* transaction pointer */
2210 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2211 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002212 xfs_agblock_t bno, /* block being freed */
2213 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002216 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 int error;
David Chinner92821e22007-05-24 15:26:31 +10002218 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 xfs_mount_t *mp; /* mount structure */
2220 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002221 __be32 *agfl_bno;
2222 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 agf = XFS_BUF_TO_AGF(agbp);
2225 mp = tp->t_mountp;
2226
2227 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002228 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002230 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002231 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002233
2234 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002235 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 xfs_trans_agflist_delta(tp, 1);
2237 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002238
2239 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2240 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002241 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002242 pag->pagf_btreeblks--;
2243 logflags |= XFS_AGF_BTREEBLKS;
2244 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002245 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002246
David Chinner92821e22007-05-24 15:26:31 +10002247 xfs_alloc_log_agf(tp, agbp, logflags);
2248
Christoph Hellwig16259e72005-11-02 15:11:25 +11002249 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002250
2251 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2252 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002253 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002254 startoff = (char *)blockp - (char *)agflbp->b_addr;
2255
David Chinner92821e22007-05-24 15:26:31 +10002256 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002257
Dave Chinner61fe1352013-04-03 16:11:30 +11002258 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002259 xfs_trans_log_buf(tp, agflbp, startoff,
2260 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 0;
2262}
2263
Dave Chinner4e0e6042013-04-03 16:11:13 +11002264static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002265xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002266 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002267 struct xfs_buf *bp)
2268 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002269 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002270
Brian Fostera45086e2015-10-12 15:59:25 +11002271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2272 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002273 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002274 if (!xfs_log_check_lsn(mp,
2275 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2276 return false;
2277 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002278
Dave Chinner4e0e6042013-04-03 16:11:13 +11002279 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2280 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2281 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2282 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2283 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2284 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2285 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002286
Eric Sandeene1b05722014-09-09 11:47:24 +10002287 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2288 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2289 return false;
2290
Darrick J. Wongb8704942016-08-03 11:30:32 +10002291 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2292 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2293 return false;
2294
Dave Chinner5d5f5272012-11-14 17:44:56 +11002295 /*
2296 * during growfs operations, the perag is not fully initialised,
2297 * so we can't use it for any useful checking. growfs ensures we can't
2298 * use it by using uncached buffers that don't have the perag attached
2299 * so we can detect and avoid this problem.
2300 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002301 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2302 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002303
Dave Chinner4e0e6042013-04-03 16:11:13 +11002304 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2305 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2306 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002307
Dave Chinner4e0e6042013-04-03 16:11:13 +11002308 return true;;
2309
Dave Chinner612cfbf2012-11-14 17:52:32 +11002310}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002311
Dave Chinner1813dd62012-11-14 17:54:40 +11002312static void
2313xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002314 struct xfs_buf *bp)
2315{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002316 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002317
Eric Sandeence5028c2014-02-27 15:23:10 +11002318 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2319 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002320 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002321 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2322 XFS_ERRTAG_ALLOC_READ_AGF,
2323 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002324 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002325
2326 if (bp->b_error)
2327 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002328}
2329
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002330static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002331xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002332 struct xfs_buf *bp)
2333{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002334 struct xfs_mount *mp = bp->b_target->bt_mount;
2335 struct xfs_buf_log_item *bip = bp->b_fspriv;
2336
2337 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002338 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002339 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002340 return;
2341 }
2342
2343 if (!xfs_sb_version_hascrc(&mp->m_sb))
2344 return;
2345
2346 if (bip)
2347 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2348
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002349 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002350}
2351
Dave Chinner1813dd62012-11-14 17:54:40 +11002352const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002353 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002354 .verify_read = xfs_agf_read_verify,
2355 .verify_write = xfs_agf_write_verify,
2356};
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/*
2359 * Read in the allocation group header (free/alloc section).
2360 */
2361int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002362xfs_read_agf(
2363 struct xfs_mount *mp, /* mount point structure */
2364 struct xfs_trans *tp, /* transaction pointer */
2365 xfs_agnumber_t agno, /* allocation group number */
2366 int flags, /* XFS_BUF_ */
2367 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 int error;
2370
Dave Chinnerd1230312013-11-01 15:27:19 +11002371 trace_xfs_read_agf(mp, agno);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 ASSERT(agno != NULLAGNUMBER);
2374 error = xfs_trans_read_buf(
2375 mp, tp, mp->m_ddev_targp,
2376 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002377 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (error)
2379 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002380 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002382
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002383 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002384 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002385 return 0;
2386}
2387
2388/*
2389 * Read in the allocation group header (free/alloc section).
2390 */
2391int /* error */
2392xfs_alloc_read_agf(
2393 struct xfs_mount *mp, /* mount point structure */
2394 struct xfs_trans *tp, /* transaction pointer */
2395 xfs_agnumber_t agno, /* allocation group number */
2396 int flags, /* XFS_ALLOC_FLAG_... */
2397 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2398{
2399 struct xfs_agf *agf; /* ag freelist header */
2400 struct xfs_perag *pag; /* per allocation group data */
2401 int error;
2402
Dave Chinnerd1230312013-11-01 15:27:19 +11002403 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002404
Dave Chinnerd1230312013-11-01 15:27:19 +11002405 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002406 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002407 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002408 bpp);
2409 if (error)
2410 return error;
2411 if (!*bpp)
2412 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002413 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002414
2415 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002416 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002418 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002419 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002420 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2421 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002423 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002425 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Darrick J. Wongb8704942016-08-03 11:30:32 +10002426 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2427 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002428 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002429 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002430 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 pag->pagf_init = 1;
2432 }
2433#ifdef DEBUG
2434 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002435 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002436 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002437 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2438 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002440 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002442 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002445 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 0;
2447}
2448
2449/*
2450 * Allocate an extent (variable-size).
2451 * Depending on the allocation type, we either look in a single allocation
2452 * group or loop over the allocation groups to find the result.
2453 */
2454int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002455xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 xfs_alloc_arg_t *args) /* allocation argument structure */
2457{
2458 xfs_agblock_t agsize; /* allocation group size */
2459 int error;
2460 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 xfs_extlen_t minleft;/* minimum left value, temp copy */
2462 xfs_mount_t *mp; /* mount structure pointer */
2463 xfs_agnumber_t sagno; /* starting allocation group number */
2464 xfs_alloctype_t type; /* input allocation type */
2465 int bump_rotor = 0;
2466 int no_min = 0;
2467 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2468
2469 mp = args->mp;
2470 type = args->otype = args->type;
2471 args->agbno = NULLAGBLOCK;
2472 /*
2473 * Just fix this up, for the case where the last a.g. is shorter
2474 * (or there's only one a.g.) and the caller couldn't easily figure
2475 * that out (xfs_bmap_alloc).
2476 */
2477 agsize = mp->m_sb.sb_agblocks;
2478 if (args->maxlen > agsize)
2479 args->maxlen = agsize;
2480 if (args->alignment == 0)
2481 args->alignment = 1;
2482 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2483 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2484 ASSERT(args->minlen <= args->maxlen);
2485 ASSERT(args->minlen <= agsize);
2486 ASSERT(args->mod < args->prod);
2487 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2488 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2489 args->minlen > args->maxlen || args->minlen > agsize ||
2490 args->mod >= args->prod) {
2491 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002492 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return 0;
2494 }
2495 minleft = args->minleft;
2496
2497 switch (type) {
2498 case XFS_ALLOCTYPE_THIS_AG:
2499 case XFS_ALLOCTYPE_NEAR_BNO:
2500 case XFS_ALLOCTYPE_THIS_BNO:
2501 /*
2502 * These three force us into a single a.g.
2503 */
2504 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002505 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 args->minleft = 0;
2507 error = xfs_alloc_fix_freelist(args, 0);
2508 args->minleft = minleft;
2509 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002510 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 goto error0;
2512 }
2513 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002514 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 break;
2516 }
2517 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2518 if ((error = xfs_alloc_ag_vextent(args)))
2519 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 break;
2521 case XFS_ALLOCTYPE_START_BNO:
2522 /*
2523 * Try near allocation first, then anywhere-in-ag after
2524 * the first a.g. fails.
2525 */
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002526 if ((args->userdata & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2528 args->fsbno = XFS_AGB_TO_FSB(mp,
2529 ((mp->m_agfrotor / rotorstep) %
2530 mp->m_sb.sb_agcount), 0);
2531 bump_rotor = 1;
2532 }
2533 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2534 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2535 /* FALLTHROUGH */
2536 case XFS_ALLOCTYPE_ANY_AG:
2537 case XFS_ALLOCTYPE_START_AG:
2538 case XFS_ALLOCTYPE_FIRST_AG:
2539 /*
2540 * Rotate through the allocation groups looking for a winner.
2541 */
2542 if (type == XFS_ALLOCTYPE_ANY_AG) {
2543 /*
2544 * Start with the last place we left off.
2545 */
2546 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2547 mp->m_sb.sb_agcount;
2548 args->type = XFS_ALLOCTYPE_THIS_AG;
2549 flags = XFS_ALLOC_FLAG_TRYLOCK;
2550 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2551 /*
2552 * Start with allocation group given by bno.
2553 */
2554 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2555 args->type = XFS_ALLOCTYPE_THIS_AG;
2556 sagno = 0;
2557 flags = 0;
2558 } else {
2559 if (type == XFS_ALLOCTYPE_START_AG)
2560 args->type = XFS_ALLOCTYPE_THIS_AG;
2561 /*
2562 * Start with the given allocation group.
2563 */
2564 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2565 flags = XFS_ALLOC_FLAG_TRYLOCK;
2566 }
2567 /*
2568 * Loop over allocation groups twice; first time with
2569 * trylock set, second time without.
2570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002572 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (no_min) args->minleft = 0;
2574 error = xfs_alloc_fix_freelist(args, flags);
2575 args->minleft = minleft;
2576 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002577 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto error0;
2579 }
2580 /*
2581 * If we get a buffer back then the allocation will fly.
2582 */
2583 if (args->agbp) {
2584 if ((error = xfs_alloc_ag_vextent(args)))
2585 goto error0;
2586 break;
2587 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002588
2589 trace_xfs_alloc_vextent_loopfailed(args);
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /*
2592 * Didn't work, figure out the next iteration.
2593 */
2594 if (args->agno == sagno &&
2595 type == XFS_ALLOCTYPE_START_BNO)
2596 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002597 /*
2598 * For the first allocation, we can try any AG to get
2599 * space. However, if we already have allocated a
2600 * block, we don't want to try AGs whose number is below
2601 * sagno. Otherwise, we may end up with out-of-order
2602 * locking of AGF, which might cause deadlock.
2603 */
2604 if (++(args->agno) == mp->m_sb.sb_agcount) {
2605 if (args->firstblock != NULLFSBLOCK)
2606 args->agno = sagno;
2607 else
2608 args->agno = 0;
2609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
2611 * Reached the starting a.g., must either be done
2612 * or switch to non-trylock mode.
2613 */
2614 if (args->agno == sagno) {
2615 if (no_min == 1) {
2616 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002617 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 break;
2619 }
2620 if (flags == 0) {
2621 no_min = 1;
2622 } else {
2623 flags = 0;
2624 if (type == XFS_ALLOCTYPE_START_BNO) {
2625 args->agbno = XFS_FSB_TO_AGBNO(mp,
2626 args->fsbno);
2627 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2628 }
2629 }
2630 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002631 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2634 if (args->agno == sagno)
2635 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2636 (mp->m_sb.sb_agcount * rotorstep);
2637 else
2638 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2639 (mp->m_sb.sb_agcount * rotorstep);
2640 }
2641 break;
2642 default:
2643 ASSERT(0);
2644 /* NOTREACHED */
2645 }
2646 if (args->agbno == NULLAGBLOCK)
2647 args->fsbno = NULLFSBLOCK;
2648 else {
2649 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2650#ifdef DEBUG
2651 ASSERT(args->len >= args->minlen);
2652 ASSERT(args->len <= args->maxlen);
2653 ASSERT(args->agbno % args->alignment == 0);
2654 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2655 args->len);
2656#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002657
2658 /* Zero the extent if we were asked to do so */
2659 if (args->userdata & XFS_ALLOC_USERDATA_ZERO) {
2660 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2661 if (error)
2662 goto error0;
2663 }
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002666 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return 0;
2668error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002669 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return error;
2671}
2672
Dave Chinner4d89e202016-06-21 11:53:28 +10002673/* Ensure that the freelist is at full capacity. */
2674int
2675xfs_free_extent_fix_freelist(
2676 struct xfs_trans *tp,
2677 xfs_agnumber_t agno,
2678 struct xfs_buf **agbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Dave Chinner4d89e202016-06-21 11:53:28 +10002680 struct xfs_alloc_arg args;
2681 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Dave Chinner4d89e202016-06-21 11:53:28 +10002683 memset(&args, 0, sizeof(struct xfs_alloc_arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 args.tp = tp;
2685 args.mp = tp->t_mountp;
Dave Chinner4d89e202016-06-21 11:53:28 +10002686 args.agno = agno;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002687
2688 /*
2689 * validate that the block number is legal - the enables us to detect
2690 * and handle a silent filesystem corruption rather than crashing.
2691 */
Dave Chinnerbe65b182011-04-08 12:45:07 +10002692 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002693 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002694
Dave Chinnera862e0f2010-01-11 11:47:41 +00002695 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002696 ASSERT(args.pag);
2697
2698 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2699 if (error)
Dave Chinner4d89e202016-06-21 11:53:28 +10002700 goto out;
2701
2702 *agbp = args.agbp;
2703out:
2704 xfs_perag_put(args.pag);
2705 return error;
2706}
2707
2708/*
2709 * Free an extent.
2710 * Just break up the extent address and hand off to xfs_free_ag_extent
2711 * after fixing up the freelist.
2712 */
2713int /* error */
2714xfs_free_extent(
2715 struct xfs_trans *tp, /* transaction pointer */
2716 xfs_fsblock_t bno, /* starting block number of extent */
Darrick J. Wong340785c2016-08-03 11:33:42 +10002717 xfs_extlen_t len, /* length of extent */
2718 struct xfs_owner_info *oinfo) /* extent owner */
Dave Chinner4d89e202016-06-21 11:53:28 +10002719{
2720 struct xfs_mount *mp = tp->t_mountp;
2721 struct xfs_buf *agbp;
2722 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2723 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2724 int error;
2725
2726 ASSERT(len != 0);
2727
Darrick J. Wongba9e7802016-08-03 11:26:33 +10002728 trace_xfs_bmap_free_deferred(mp, agno, 0, agbno, len);
2729
2730 if (XFS_TEST_ERROR(false, mp,
2731 XFS_ERRTAG_FREE_EXTENT,
2732 XFS_RANDOM_FREE_EXTENT))
2733 return -EIO;
2734
Dave Chinner4d89e202016-06-21 11:53:28 +10002735 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2736 if (error)
2737 return error;
2738
2739 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002740
2741 /* validate the extent size is legal now we have the agf locked */
Dave Chinner4d89e202016-06-21 11:53:28 +10002742 XFS_WANT_CORRUPTED_GOTO(mp,
2743 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2744 err);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002745
Darrick J. Wong340785c2016-08-03 11:33:42 +10002746 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, 0);
Dave Chinner4d89e202016-06-21 11:53:28 +10002747 if (error)
2748 goto err;
2749
2750 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2751 return 0;
2752
2753err:
2754 xfs_trans_brelse(tp, agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return error;
2756}