blob: e1e7fe3b5424c8e619be4fe25930eb6ca414c6ea [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"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000031#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110033#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110035#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110036#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110037#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Dave Chinnerc999a222012-03-22 05:15:07 +000039struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
42
43#define XFSA_FIXUP_BNO_OK 1
44#define XFSA_FIXUP_CNT_OK 2
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
47STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
48STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
49STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000050 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110053 * Lookup the record equal to [bno, len] in the btree given by cur.
54 */
55STATIC int /* error */
56xfs_alloc_lookup_eq(
57 struct xfs_btree_cur *cur, /* btree cursor */
58 xfs_agblock_t bno, /* starting block of extent */
59 xfs_extlen_t len, /* length of extent */
60 int *stat) /* success/failure */
61{
62 cur->bc_rec.a.ar_startblock = bno;
63 cur->bc_rec.a.ar_blockcount = len;
64 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
65}
66
67/*
68 * Lookup the first record greater than or equal to [bno, len]
69 * in the btree given by cur.
70 */
Dave Chinnera66d6362012-03-22 05:15:12 +000071int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110072xfs_alloc_lookup_ge(
73 struct xfs_btree_cur *cur, /* btree cursor */
74 xfs_agblock_t bno, /* starting block of extent */
75 xfs_extlen_t len, /* length of extent */
76 int *stat) /* success/failure */
77{
78 cur->bc_rec.a.ar_startblock = bno;
79 cur->bc_rec.a.ar_blockcount = len;
80 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
81}
82
83/*
84 * Lookup the first record less than or equal to [bno, len]
85 * in the btree given by cur.
86 */
Christoph Hellwiga46db602011-01-07 13:02:04 +000087int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110088xfs_alloc_lookup_le(
89 struct xfs_btree_cur *cur, /* btree cursor */
90 xfs_agblock_t bno, /* starting block of extent */
91 xfs_extlen_t len, /* length of extent */
92 int *stat) /* success/failure */
93{
94 cur->bc_rec.a.ar_startblock = bno;
95 cur->bc_rec.a.ar_blockcount = len;
96 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
97}
98
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110099/*
100 * Update the record referred to by cur to the value given
101 * by [bno, len].
102 * This either works (return 0) or gets an EFSCORRUPTED error.
103 */
104STATIC int /* error */
105xfs_alloc_update(
106 struct xfs_btree_cur *cur, /* btree cursor */
107 xfs_agblock_t bno, /* starting block of extent */
108 xfs_extlen_t len) /* length of extent */
109{
110 union xfs_btree_rec rec;
111
112 rec.alloc.ar_startblock = cpu_to_be32(bno);
113 rec.alloc.ar_blockcount = cpu_to_be32(len);
114 return xfs_btree_update(cur, &rec);
115}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100116
117/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100118 * Get the data from the pointed-to record.
119 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000120int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100121xfs_alloc_get_rec(
122 struct xfs_btree_cur *cur, /* btree cursor */
123 xfs_agblock_t *bno, /* output: starting block of extent */
124 xfs_extlen_t *len, /* output: length of extent */
125 int *stat) /* output: success/failure */
126{
127 union xfs_btree_rec *rec;
128 int error;
129
130 error = xfs_btree_get_rec(cur, &rec, stat);
131 if (!error && *stat == 1) {
132 *bno = be32_to_cpu(rec->alloc.ar_startblock);
133 *len = be32_to_cpu(rec->alloc.ar_blockcount);
134 }
135 return error;
136}
137
138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Compute aligned version of the found extent.
140 * Takes alignment and min length into account.
141 */
David Chinner12375c82008-04-10 12:21:32 +1000142STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000144 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 xfs_agblock_t foundbno, /* starting block in found extent */
146 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 xfs_agblock_t *resbno, /* result block number */
148 xfs_extlen_t *reslen) /* result length */
149{
150 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xfs_extlen_t len;
Brian Fosterbfe46d42015-05-29 08:53:00 +1000152 xfs_extlen_t diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Hellwige26f0502011-04-24 19:06:15 +0000154 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000155 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000156
Brian Fosterbfe46d42015-05-29 08:53:00 +1000157 /*
158 * If we have a largish extent that happens to start before min_agbno,
159 * see if we can shift it into range...
160 */
161 if (bno < args->min_agbno && bno + len > args->min_agbno) {
162 diff = args->min_agbno - bno;
163 if (len > diff) {
164 bno += diff;
165 len -= diff;
166 }
167 }
168
Christoph Hellwige26f0502011-04-24 19:06:15 +0000169 if (args->alignment > 1 && len >= args->minlen) {
170 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
Brian Fosterbfe46d42015-05-29 08:53:00 +1000171
172 diff = aligned_bno - bno;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000173
174 *resbno = aligned_bno;
175 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000177 *resbno = bno;
178 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * Compute best start block and diff for "near" allocations.
184 * freelen >= wantlen already checked by caller.
185 */
186STATIC xfs_extlen_t /* difference value (absolute) */
187xfs_alloc_compute_diff(
188 xfs_agblock_t wantbno, /* target starting block */
189 xfs_extlen_t wantlen, /* target length */
190 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200191 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 xfs_agblock_t freebno, /* freespace's starting block */
193 xfs_extlen_t freelen, /* freespace's length */
194 xfs_agblock_t *newbnop) /* result: best start block from free */
195{
196 xfs_agblock_t freeend; /* end of freespace extent */
197 xfs_agblock_t newbno1; /* return block number */
198 xfs_agblock_t newbno2; /* other new block number */
199 xfs_extlen_t newlen1=0; /* length with newbno1 */
200 xfs_extlen_t newlen2=0; /* length with newbno2 */
201 xfs_agblock_t wantend; /* end of target extent */
202
203 ASSERT(freelen >= wantlen);
204 freeend = freebno + freelen;
205 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200206 /*
207 * We want to allocate from the start of a free extent if it is past
208 * the desired block or if we are allocating user data and the free
209 * extent is before desired block. The second case is there to allow
210 * for contiguous allocation from the remaining free space if the file
211 * grows in the short term.
212 */
213 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
215 newbno1 = NULLAGBLOCK;
216 } else if (freeend >= wantend && alignment > 1) {
217 newbno1 = roundup(wantbno, alignment);
218 newbno2 = newbno1 - alignment;
219 if (newbno1 >= freeend)
220 newbno1 = NULLAGBLOCK;
221 else
222 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
223 if (newbno2 < freebno)
224 newbno2 = NULLAGBLOCK;
225 else
226 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
227 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
228 if (newlen1 < newlen2 ||
229 (newlen1 == newlen2 &&
230 XFS_ABSDIFF(newbno1, wantbno) >
231 XFS_ABSDIFF(newbno2, wantbno)))
232 newbno1 = newbno2;
233 } else if (newbno2 != NULLAGBLOCK)
234 newbno1 = newbno2;
235 } else if (freeend >= wantend) {
236 newbno1 = wantbno;
237 } else if (alignment > 1) {
238 newbno1 = roundup(freeend - wantlen, alignment);
239 if (newbno1 > freeend - wantlen &&
240 newbno1 - alignment >= freebno)
241 newbno1 -= alignment;
242 else if (newbno1 >= freeend)
243 newbno1 = NULLAGBLOCK;
244 } else
245 newbno1 = freeend - wantlen;
246 *newbnop = newbno1;
247 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
248}
249
250/*
251 * Fix up the length, based on mod and prod.
252 * len should be k * prod + mod for some k.
253 * If len is too small it is returned unchanged.
254 * If len hits maxlen it is left alone.
255 */
256STATIC void
257xfs_alloc_fix_len(
258 xfs_alloc_arg_t *args) /* allocation argument structure */
259{
260 xfs_extlen_t k;
261 xfs_extlen_t rlen;
262
263 ASSERT(args->mod < args->prod);
264 rlen = args->len;
265 ASSERT(rlen >= args->minlen);
266 ASSERT(rlen <= args->maxlen);
267 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
268 (args->mod == 0 && rlen < args->prod))
269 return;
270 k = rlen % args->prod;
271 if (k == args->mod)
272 return;
Jan Kara30265112014-06-06 16:06:37 +1000273 if (k > args->mod)
274 rlen = rlen - (k - args->mod);
275 else
276 rlen = rlen - args->prod + (args->mod - k);
Dave Chinner3790a8c2015-02-24 10:16:04 +1100277 /* casts to (int) catch length underflows */
Jan Kara30265112014-06-06 16:06:37 +1000278 if ((int)rlen < (int)args->minlen)
279 return;
280 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
281 ASSERT(rlen % args->prod == args->mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 args->len = rlen;
283}
284
285/*
286 * Fix up length if there is too little space left in the a.g.
287 * Return 1 if ok, 0 if too little, should give up.
288 */
289STATIC int
290xfs_alloc_fix_minleft(
291 xfs_alloc_arg_t *args) /* allocation argument structure */
292{
293 xfs_agf_t *agf; /* a.g. freelist header */
294 int diff; /* free space difference */
295
296 if (args->minleft == 0)
297 return 1;
298 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100299 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 - args->len - args->minleft;
301 if (diff >= 0)
302 return 1;
303 args->len += diff; /* shrink the allocated space */
Dave Chinner3790a8c2015-02-24 10:16:04 +1100304 /* casts to (int) catch length underflows */
305 if ((int)args->len >= (int)args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 1;
307 args->agbno = NULLAGBLOCK;
308 return 0;
309}
310
311/*
312 * Update the two btrees, logically removing from freespace the extent
313 * starting at rbno, rlen blocks. The extent is contained within the
314 * actual (current) free extent fbno for flen blocks.
315 * Flags are passed in indicating whether the cursors are set to the
316 * relevant records.
317 */
318STATIC int /* error code */
319xfs_alloc_fixup_trees(
320 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
321 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
322 xfs_agblock_t fbno, /* starting block of free extent */
323 xfs_extlen_t flen, /* length of free extent */
324 xfs_agblock_t rbno, /* starting block of returned extent */
325 xfs_extlen_t rlen, /* length of returned extent */
326 int flags) /* flags, XFSA_FIXUP_... */
327{
328 int error; /* error code */
329 int i; /* operation results */
330 xfs_agblock_t nfbno1; /* first new free startblock */
331 xfs_agblock_t nfbno2; /* second new free startblock */
332 xfs_extlen_t nflen1=0; /* first new free length */
333 xfs_extlen_t nflen2=0; /* second new free length */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100334 struct xfs_mount *mp;
335
336 mp = cnt_cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
339 * Look up the record in the by-size tree if necessary.
340 */
341 if (flags & XFSA_FIXUP_CNT_OK) {
342#ifdef DEBUG
343 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
344 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100345 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 i == 1 && nfbno1 == fbno && nflen1 == flen);
347#endif
348 } else {
349 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
350 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100351 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 /*
354 * Look up the record in the by-block tree if necessary.
355 */
356 if (flags & XFSA_FIXUP_BNO_OK) {
357#ifdef DEBUG
358 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
359 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100360 XFS_WANT_CORRUPTED_RETURN(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 i == 1 && nfbno1 == fbno && nflen1 == flen);
362#endif
363 } else {
364 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
365 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100366 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100369#ifdef DEBUG
370 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
371 struct xfs_btree_block *bnoblock;
372 struct xfs_btree_block *cntblock;
373
374 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
375 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
376
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100377 XFS_WANT_CORRUPTED_RETURN(mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100378 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * Deal with all four cases: the allocated record is contained
384 * within the freespace record, so we can have new freespace
385 * at either (or both) end, or no freespace remaining.
386 */
387 if (rbno == fbno && rlen == flen)
388 nfbno1 = nfbno2 = NULLAGBLOCK;
389 else if (rbno == fbno) {
390 nfbno1 = rbno + rlen;
391 nflen1 = flen - rlen;
392 nfbno2 = NULLAGBLOCK;
393 } else if (rbno + rlen == fbno + flen) {
394 nfbno1 = fbno;
395 nflen1 = flen - rlen;
396 nfbno2 = NULLAGBLOCK;
397 } else {
398 nfbno1 = fbno;
399 nflen1 = rbno - fbno;
400 nfbno2 = rbno + rlen;
401 nflen2 = (fbno + flen) - nfbno2;
402 }
403 /*
404 * Delete the entry from the by-size btree.
405 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100406 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100408 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Add new by-size btree entry(s).
411 */
412 if (nfbno1 != NULLAGBLOCK) {
413 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
414 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100415 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100416 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100418 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 if (nfbno2 != NULLAGBLOCK) {
421 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
422 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100423 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100424 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100426 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 /*
429 * Fix up the by-block btree entry(s).
430 */
431 if (nfbno1 == NULLAGBLOCK) {
432 /*
433 * No remaining freespace, just delete the by-block tree entry.
434 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100435 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100437 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } else {
439 /*
440 * Update the by-block entry to start later|be shorter.
441 */
442 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
443 return error;
444 }
445 if (nfbno2 != NULLAGBLOCK) {
446 /*
447 * 2 resulting free entries, need to add one.
448 */
449 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
450 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100451 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100452 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100454 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 return 0;
457}
458
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100459static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100460xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100461 struct xfs_buf *bp)
462{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100463 struct xfs_mount *mp = bp->b_target->bt_mount;
464 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100465 int i;
466
Eric Sandeence748ea2015-07-29 11:53:31 +1000467 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100468 return false;
469 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
470 return false;
471 /*
472 * during growfs operations, the perag is not fully initialised,
473 * so we can't use it for any useful checking. growfs ensures we can't
474 * use it by using uncached buffers that don't have the perag attached
475 * so we can detect and avoid this problem.
476 */
477 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
478 return false;
479
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100480 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100481 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100482 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100483 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100484 }
Brian Fostera45086e2015-10-12 15:59:25 +1100485
486 return xfs_log_check_lsn(mp,
487 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
Dave Chinner612cfbf2012-11-14 17:52:32 +1100488}
489
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100490static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100491xfs_agfl_read_verify(
492 struct xfs_buf *bp)
493{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100494 struct xfs_mount *mp = bp->b_target->bt_mount;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100495
496 /*
497 * There is no verification of non-crc AGFLs because mkfs does not
498 * initialise the AGFL to zero or NULL. Hence the only valid part of the
499 * AGFL is what the AGF says is active. We can't get to the AGF, so we
500 * can't verify just those entries are valid.
501 */
502 if (!xfs_sb_version_hascrc(&mp->m_sb))
503 return;
504
Eric Sandeence5028c2014-02-27 15:23:10 +1100505 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000506 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100507 else if (!xfs_agfl_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000508 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100509
510 if (bp->b_error)
511 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100512}
513
514static void
515xfs_agfl_write_verify(
516 struct xfs_buf *bp)
517{
518 struct xfs_mount *mp = bp->b_target->bt_mount;
519 struct xfs_buf_log_item *bip = bp->b_fspriv;
520
521 /* no verification of non-crc AGFLs */
522 if (!xfs_sb_version_hascrc(&mp->m_sb))
523 return;
524
525 if (!xfs_agfl_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000526 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100527 xfs_verifier_error(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100528 return;
529 }
530
531 if (bip)
532 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
533
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100534 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100535}
536
Dave Chinner1813dd62012-11-14 17:54:40 +1100537const struct xfs_buf_ops xfs_agfl_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100538 .name = "xfs_agfl",
Dave Chinner1813dd62012-11-14 17:54:40 +1100539 .verify_read = xfs_agfl_read_verify,
540 .verify_write = xfs_agfl_write_verify,
541};
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * Read in the allocation group free block array.
545 */
546STATIC int /* error */
547xfs_alloc_read_agfl(
548 xfs_mount_t *mp, /* mount point structure */
549 xfs_trans_t *tp, /* transaction pointer */
550 xfs_agnumber_t agno, /* allocation group number */
551 xfs_buf_t **bpp) /* buffer for the ag free block array */
552{
553 xfs_buf_t *bp; /* return value */
554 int error;
555
556 ASSERT(agno != NULLAGNUMBER);
557 error = xfs_trans_read_buf(
558 mp, tp, mp->m_ddev_targp,
559 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100560 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (error)
562 return error;
Christoph Hellwig38f23232011-10-10 16:52:45 +0000563 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *bpp = bp;
565 return 0;
566}
567
Christoph Hellwigecb69282011-03-04 12:59:55 +0000568STATIC int
569xfs_alloc_update_counters(
570 struct xfs_trans *tp,
571 struct xfs_perag *pag,
572 struct xfs_buf *agbp,
573 long len)
574{
575 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
576
577 pag->pagf_freeblks += len;
578 be32_add_cpu(&agf->agf_freeblks, len);
579
580 xfs_trans_agblocks_delta(tp, len);
581 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
582 be32_to_cpu(agf->agf_length)))
Dave Chinner24513372014-06-25 14:58:08 +1000583 return -EFSCORRUPTED;
Christoph Hellwigecb69282011-03-04 12:59:55 +0000584
585 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
586 return 0;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * Allocation group level functions.
591 */
592
593/*
594 * Allocate a variable extent in the allocation group agno.
595 * Type and bno are used to determine where in the allocation group the
596 * extent will start.
597 * Extent's length (returned in *len) will be between minlen and maxlen,
598 * and of the form k * prod + mod unless there's nothing that large.
599 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
600 */
601STATIC int /* error */
602xfs_alloc_ag_vextent(
603 xfs_alloc_arg_t *args) /* argument structure for allocation */
604{
605 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 ASSERT(args->minlen > 0);
608 ASSERT(args->maxlen > 0);
609 ASSERT(args->minlen <= args->maxlen);
610 ASSERT(args->mod < args->prod);
611 ASSERT(args->alignment > 0);
612 /*
613 * Branch to correct routine based on the type.
614 */
615 args->wasfromfl = 0;
616 switch (args->type) {
617 case XFS_ALLOCTYPE_THIS_AG:
618 error = xfs_alloc_ag_vextent_size(args);
619 break;
620 case XFS_ALLOCTYPE_NEAR_BNO:
621 error = xfs_alloc_ag_vextent_near(args);
622 break;
623 case XFS_ALLOCTYPE_THIS_BNO:
624 error = xfs_alloc_ag_vextent_exact(args);
625 break;
626 default:
627 ASSERT(0);
628 /* NOTREACHED */
629 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000630
631 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Christoph Hellwigecb69282011-03-04 12:59:55 +0000634 ASSERT(args->len >= args->minlen);
635 ASSERT(args->len <= args->maxlen);
636 ASSERT(!args->wasfromfl || !args->isfl);
637 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Christoph Hellwigecb69282011-03-04 12:59:55 +0000639 if (!args->wasfromfl) {
640 error = xfs_alloc_update_counters(args->tp, args->pag,
641 args->agbp,
642 -((long)(args->len)));
643 if (error)
644 return error;
645
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000646 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000647 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000649
650 if (!args->isfl) {
651 xfs_trans_mod_sb(args->tp, args->wasdel ?
652 XFS_TRANS_SB_RES_FDBLOCKS :
653 XFS_TRANS_SB_FDBLOCKS,
654 -((long)(args->len)));
655 }
656
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100657 XFS_STATS_INC(args->mp, xs_allocx);
658 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
Christoph Hellwigecb69282011-03-04 12:59:55 +0000659 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
663 * Allocate a variable extent at exactly agno/bno.
664 * Extent's length (returned in *len) will be between minlen and maxlen,
665 * and of the form k * prod + mod unless there's nothing that large.
666 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
667 */
668STATIC int /* error */
669xfs_alloc_ag_vextent_exact(
670 xfs_alloc_arg_t *args) /* allocation argument structure */
671{
672 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
673 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int error;
675 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000677 xfs_agblock_t tbno; /* start block of trimmed extent */
678 xfs_extlen_t tlen; /* length of trimmed extent */
679 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Allocate/initialize a cursor for the by-number freespace btree.
686 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100687 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000688 args->agno, XFS_BTNUM_BNO);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
692 * Look for the closest free block <= bno, it must contain bno
693 * if any free block does.
694 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000695 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
696 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000698 if (!i)
699 goto not_found;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * Grab the freespace record.
703 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000704 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
705 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100707 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000711 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000713 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000714
715 /*
716 * Give up if the start of the extent is busy, or the freespace isn't
717 * long enough for the minimum request.
718 */
719 if (tbno > args->agbno)
720 goto not_found;
721 if (tlen < args->minlen)
722 goto not_found;
723 tend = tbno + tlen;
724 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000725 goto not_found;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
728 * End of extent will be smaller of the freespace end and the
729 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * Fix the length according to mod and prod if given.
732 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000733 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
734 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000736 if (!xfs_alloc_fix_minleft(args))
737 goto not_found;
738
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000739 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000742 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * Allocate/initialize a cursor for the by-size btree.
744 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100745 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
746 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100748 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000749 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
750 args->len, XFSA_FIXUP_BNO_OK);
751 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
753 goto error0;
754 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
757 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000760 trace_xfs_alloc_exact_done(args);
761 return 0;
762
763not_found:
764 /* Didn't find it, return null. */
765 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
766 args->agbno = NULLAGBLOCK;
767 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769
770error0:
771 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000772 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return error;
774}
775
776/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000777 * Search the btree in a given direction via the search cursor and compare
778 * the records found against the good extent we've already found.
779 */
780STATIC int
781xfs_alloc_find_best_extent(
782 struct xfs_alloc_arg *args, /* allocation argument structure */
783 struct xfs_btree_cur **gcur, /* good cursor */
784 struct xfs_btree_cur **scur, /* searching cursor */
785 xfs_agblock_t gdiff, /* difference for search comparison */
786 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000787 xfs_extlen_t *slen, /* extent length */
788 xfs_agblock_t *sbnoa, /* aligned extent found by search */
789 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000790 int dir) /* 0 = search right, 1 = search left */
791{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000792 xfs_agblock_t new;
793 xfs_agblock_t sdiff;
794 int error;
795 int i;
796
797 /* The good extent is perfect, no need to search. */
798 if (!gdiff)
799 goto out_use_good;
800
801 /*
802 * Look until we find a better one, run out of space or run off the end.
803 */
804 do {
805 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
806 if (error)
807 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100808 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000809 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000810
811 /*
812 * The good extent is closer than this one.
813 */
814 if (!dir) {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000815 if (*sbnoa > args->max_agbno)
816 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000817 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000818 goto out_use_good;
819 } else {
Brian Fosterbfe46d42015-05-29 08:53:00 +1000820 if (*sbnoa < args->min_agbno)
821 goto out_use_good;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000822 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000823 goto out_use_good;
824 }
825
826 /*
827 * Same distance, compare length and pick the best.
828 */
829 if (*slena >= args->minlen) {
830 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
831 xfs_alloc_fix_len(args);
832
833 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200834 args->alignment,
835 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000836 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000837
838 /*
839 * Choose closer size and invalidate other cursor.
840 */
841 if (sdiff < gdiff)
842 goto out_use_search;
843 goto out_use_good;
844 }
845
846 if (!dir)
847 error = xfs_btree_increment(*scur, 0, &i);
848 else
849 error = xfs_btree_decrement(*scur, 0, &i);
850 if (error)
851 goto error0;
852 } while (i);
853
854out_use_good:
855 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
856 *scur = NULL;
857 return 0;
858
859out_use_search:
860 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
861 *gcur = NULL;
862 return 0;
863
864error0:
865 /* caller invalidates cursors */
866 return error;
867}
868
869/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * Allocate a variable extent near bno in the allocation group agno.
871 * Extent's length (returned in len) will be between minlen and maxlen,
872 * and of the form k * prod + mod unless there's nothing that large.
873 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
874 */
875STATIC int /* error */
876xfs_alloc_ag_vextent_near(
877 xfs_alloc_arg_t *args) /* allocation argument structure */
878{
879 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
880 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
881 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 xfs_agblock_t gtbno; /* start bno of right side entry */
883 xfs_agblock_t gtbnoa; /* aligned ... */
884 xfs_extlen_t gtdiff; /* difference to right side entry */
885 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000886 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xfs_agblock_t gtnew; /* useful start bno of right side */
888 int error; /* error code */
889 int i; /* result code, temporary */
890 int j; /* result code, temporary */
891 xfs_agblock_t ltbno; /* start bno of left side entry */
892 xfs_agblock_t ltbnoa; /* aligned ... */
893 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000895 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 xfs_agblock_t ltnew; /* useful start bno of left side */
897 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000898 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000899#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Randomly don't execute the first algorithm.
902 */
903 int dofirst; /* set to do first algorithm */
904
Akinobu Mitaecb34032013-03-04 21:58:20 +0900905 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000907
Brian Fosterbfe46d42015-05-29 08:53:00 +1000908 /* handle unitialized agbno range so caller doesn't have to */
909 if (!args->min_agbno && !args->max_agbno)
910 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
911 ASSERT(args->min_agbno <= args->max_agbno);
912
913 /* clamp agbno to the range if it's outside */
914 if (args->agbno < args->min_agbno)
915 args->agbno = args->min_agbno;
916 if (args->agbno > args->max_agbno)
917 args->agbno = args->max_agbno;
918
Christoph Hellwige26f0502011-04-24 19:06:15 +0000919restart:
920 bno_cur_lt = NULL;
921 bno_cur_gt = NULL;
922 ltlen = 0;
923 gtlena = 0;
924 ltlena = 0;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * Get a cursor for the by-size btree.
928 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100929 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
930 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * See if there are any free extents as big as maxlen.
934 */
935 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
936 goto error0;
937 /*
938 * If none, then pick up the last entry in the tree unless the
939 * tree is empty.
940 */
941 if (!i) {
942 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
943 &ltlen, &i)))
944 goto error0;
945 if (i == 0 || ltlen == 0) {
946 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000947 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949 }
950 ASSERT(i == 1);
951 }
952 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * First algorithm.
956 * If the requested extent is large wrt the freespaces available
957 * in this a.g., then the cursor will be pointing to a btree entry
958 * near the right edge of the tree. If it's in the last btree leaf
959 * block, then we just examine all the entries in that block
960 * that are big enough, and pick the best one.
961 * This is written as a while loop so we can break out of it,
962 * but we never loop back to the top.
963 */
964 while (xfs_btree_islastblock(cnt_cur, 0)) {
965 xfs_extlen_t bdiff;
966 int besti=0;
967 xfs_extlen_t blen=0;
968 xfs_agblock_t bnew=0;
969
Dave Chinner63d20d62013-08-12 20:49:50 +1000970#ifdef DEBUG
971 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973#endif
974 /*
975 * Start from the entry that lookup found, sequence through
976 * all larger free blocks. If we're actually pointing at a
977 * record smaller than maxlen, go to the start of this block,
978 * and skip all those smaller than minlen.
979 */
980 if (ltlen || args->alignment > 1) {
981 cnt_cur->bc_ptrs[0] = 1;
982 do {
983 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
984 &ltlen, &i)))
985 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100986 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ltlen >= args->minlen)
988 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100989 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto error0;
991 } while (i);
992 ASSERT(ltlen >= args->minlen);
993 if (!i)
994 break;
995 }
996 i = cnt_cur->bc_ptrs[0];
997 for (j = 1, blen = 0, bdiff = 0;
998 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100999 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /*
1001 * For each entry, decide if it's better than
1002 * the previous best entry.
1003 */
1004 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1005 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001006 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001007 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1008 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +10001009 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 continue;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001011 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1012 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1014 xfs_alloc_fix_len(args);
1015 ASSERT(args->len >= args->minlen);
1016 if (args->len < blen)
1017 continue;
1018 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001019 args->alignment, args->userdata, ltbnoa,
1020 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (ltnew != NULLAGBLOCK &&
1022 (args->len > blen || ltdiff < bdiff)) {
1023 bdiff = ltdiff;
1024 bnew = ltnew;
1025 blen = args->len;
1026 besti = cnt_cur->bc_ptrs[0];
1027 }
1028 }
1029 /*
1030 * It didn't work. We COULD be in a case where
1031 * there's a good record somewhere, so try again.
1032 */
1033 if (blen == 0)
1034 break;
1035 /*
1036 * Point at the best entry, and retrieve it again.
1037 */
1038 cnt_cur->bc_ptrs[0] = besti;
1039 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1040 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001041 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001042 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 args->len = blen;
1044 if (!xfs_alloc_fix_minleft(args)) {
1045 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001046 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048 }
1049 blen = args->len;
1050 /*
1051 * We are allocating starting at bnew for blen blocks.
1052 */
1053 args->agbno = bnew;
1054 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001055 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * Set up a cursor for the by-bno tree.
1058 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001059 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1060 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /*
1062 * Fix up the btree entries.
1063 */
1064 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1065 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1066 goto error0;
1067 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1068 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001069
1070 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
1072 }
1073 /*
1074 * Second algorithm.
1075 * Search in the by-bno tree to the left and to the right
1076 * simultaneously, until in each case we find a space big enough,
1077 * or run into the edge of the tree. When we run into the edge,
1078 * we deallocate that cursor.
1079 * If both searches succeed, we compare the two spaces and pick
1080 * the better one.
1081 * With alignment, it's possible for both to fail; the upper
1082 * level algorithm that picks allocation groups for allocations
1083 * is not supposed to do this.
1084 */
1085 /*
1086 * Allocate and initialize the cursor for the leftward search.
1087 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001088 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1089 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /*
1091 * Lookup <= bno to find the leftward search's starting point.
1092 */
1093 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1094 goto error0;
1095 if (!i) {
1096 /*
1097 * Didn't find anything; use this cursor for the rightward
1098 * search.
1099 */
1100 bno_cur_gt = bno_cur_lt;
1101 bno_cur_lt = NULL;
1102 }
1103 /*
1104 * Found something. Duplicate the cursor for the rightward search.
1105 */
1106 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1107 goto error0;
1108 /*
1109 * Increment the cursor, so we will point at the entry just right
1110 * of the leftward entry if any, or to the leftmost entry.
1111 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001112 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto error0;
1114 if (!i) {
1115 /*
1116 * It failed, there are no rightward entries.
1117 */
1118 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1119 bno_cur_gt = NULL;
1120 }
1121 /*
1122 * Loop going left with the leftward cursor, right with the
1123 * rightward cursor, until either both directions give up or
1124 * we find an entry at least as big as minlen.
1125 */
1126 do {
1127 if (bno_cur_lt) {
1128 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1129 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001130 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001131 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1132 &ltbnoa, &ltlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001133 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001135 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001137 if (!i || ltbnoa < args->min_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 xfs_btree_del_cursor(bno_cur_lt,
1139 XFS_BTREE_NOERROR);
1140 bno_cur_lt = NULL;
1141 }
1142 }
1143 if (bno_cur_gt) {
1144 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1145 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001146 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001147 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1148 &gtbnoa, &gtlena);
Brian Fosterbfe46d42015-05-29 08:53:00 +10001149 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001151 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto error0;
Brian Fosterbfe46d42015-05-29 08:53:00 +10001153 if (!i || gtbnoa > args->max_agbno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_btree_del_cursor(bno_cur_gt,
1155 XFS_BTREE_NOERROR);
1156 bno_cur_gt = NULL;
1157 }
1158 }
1159 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Got both cursors still active, need to find better entry.
1163 */
1164 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (ltlena >= args->minlen) {
1166 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001167 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
1169 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1170 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001171 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001172 args->alignment, args->userdata, ltbnoa,
1173 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001174
1175 error = xfs_alloc_find_best_extent(args,
1176 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001177 ltdiff, &gtbno, &gtlen,
1178 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001179 0 /* search right */);
1180 } else {
1181 ASSERT(gtlena >= args->minlen);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001184 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 */
1186 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1187 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001188 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001189 args->alignment, args->userdata, gtbnoa,
1190 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001191
1192 error = xfs_alloc_find_best_extent(args,
1193 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001194 gtdiff, &ltbno, &ltlen,
1195 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001196 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001198
1199 if (error)
1200 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
1204 * If we couldn't get anything, give up.
1205 */
1206 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001207 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1208
Christoph Hellwige26f0502011-04-24 19:06:15 +00001209 if (!forced++) {
1210 trace_xfs_alloc_near_busy(args);
1211 xfs_log_force(args->mp, XFS_LOG_SYNC);
1212 goto restart;
1213 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001214 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 args->agbno = NULLAGBLOCK;
1216 return 0;
1217 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * At this point we have selected a freespace entry, either to the
1221 * left or to the right. If it's on the right, copy all the
1222 * useful variables to the "left" set so we only have one
1223 * copy of this code.
1224 */
1225 if (bno_cur_gt) {
1226 bno_cur_lt = bno_cur_gt;
1227 bno_cur_gt = NULL;
1228 ltbno = gtbno;
1229 ltbnoa = gtbnoa;
1230 ltlen = gtlen;
1231 ltlena = gtlena;
1232 j = 1;
1233 } else
1234 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Fix up the length and compute the useful address.
1238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1240 xfs_alloc_fix_len(args);
1241 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001242 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1244 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1245 return 0;
1246 }
1247 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001248 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001249 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001251 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001252 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Brian Fosterbfe46d42015-05-29 08:53:00 +10001253 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1257 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1258 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001259
1260 if (j)
1261 trace_xfs_alloc_near_greater(args);
1262 else
1263 trace_xfs_alloc_near_lesser(args);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1266 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1267 return 0;
1268
1269 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001270 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (cnt_cur != NULL)
1272 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1273 if (bno_cur_lt != NULL)
1274 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1275 if (bno_cur_gt != NULL)
1276 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1277 return error;
1278}
1279
1280/*
1281 * Allocate a variable extent anywhere in the allocation group agno.
1282 * Extent's length (returned in len) will be between minlen and maxlen,
1283 * and of the form k * prod + mod unless there's nothing that large.
1284 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1285 */
1286STATIC int /* error */
1287xfs_alloc_ag_vextent_size(
1288 xfs_alloc_arg_t *args) /* allocation argument structure */
1289{
1290 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1291 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1292 int error; /* error result */
1293 xfs_agblock_t fbno; /* start of found freespace */
1294 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int i; /* temp status variable */
1296 xfs_agblock_t rbno; /* returned block number */
1297 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001298 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Christoph Hellwige26f0502011-04-24 19:06:15 +00001300restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Allocate and initialize a cursor for the by-size btree.
1303 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001304 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1305 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * Look for an entry >= maxlen+alignment-1 blocks.
1310 */
1311 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1312 args->maxlen + args->alignment - 1, &i)))
1313 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001316 * If none or we have busy extents that we cannot allocate from, then
1317 * we have to settle for a smaller extent. In the case that there are
1318 * no large extents, this will return the last entry in the tree unless
1319 * the tree is empty. In the case that there are only busy large
1320 * extents, this will return the largest small extent unless there
1321 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001323 if (!i || forced > 1) {
1324 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1325 &fbno, &flen, &i);
1326 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto error0;
1328 if (i == 0 || flen == 0) {
1329 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001330 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332 }
1333 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001334 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1335 } else {
1336 /*
1337 * Search for a non-busy extent that is large enough.
1338 * If we are at low space, don't check, or if we fall of
1339 * the end of the btree, turn off the busy check and
1340 * restart.
1341 */
1342 for (;;) {
1343 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1344 if (error)
1345 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001346 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001347
1348 xfs_alloc_compute_aligned(args, fbno, flen,
1349 &rbno, &rlen);
1350
1351 if (rlen >= args->maxlen)
1352 break;
1353
1354 error = xfs_btree_increment(cnt_cur, 0, &i);
1355 if (error)
1356 goto error0;
1357 if (i == 0) {
1358 /*
1359 * Our only valid extents must have been busy.
1360 * Make it unbusy by forcing the log out and
1361 * retrying. If we've been here before, forcing
1362 * the log isn't making the extents available,
1363 * which means they have probably been freed in
1364 * this transaction. In that case, we have to
1365 * give up on them and we'll attempt a minlen
1366 * allocation the next time around.
1367 */
1368 xfs_btree_del_cursor(cnt_cur,
1369 XFS_BTREE_NOERROR);
1370 trace_xfs_alloc_size_busy(args);
1371 if (!forced++)
1372 xfs_log_force(args->mp, XFS_LOG_SYNC);
1373 goto restart;
1374 }
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * In the first case above, we got the last entry in the
1380 * by-size btree. Now we check to see if the space hits maxlen
1381 * once aligned; if not, we search left for something better.
1382 * This can't happen in the second case above.
1383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001385 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1387 if (rlen < args->maxlen) {
1388 xfs_agblock_t bestfbno;
1389 xfs_extlen_t bestflen;
1390 xfs_agblock_t bestrbno;
1391 xfs_extlen_t bestrlen;
1392
1393 bestrlen = rlen;
1394 bestrbno = rbno;
1395 bestflen = flen;
1396 bestfbno = fbno;
1397 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001398 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto error0;
1400 if (i == 0)
1401 break;
1402 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1403 &i)))
1404 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001405 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (flen < bestrlen)
1407 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001408 xfs_alloc_compute_aligned(args, fbno, flen,
1409 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001411 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 (rlen <= flen && rbno + rlen <= fbno + flen),
1413 error0);
1414 if (rlen > bestrlen) {
1415 bestrlen = rlen;
1416 bestrbno = rbno;
1417 bestflen = flen;
1418 bestfbno = fbno;
1419 if (rlen == args->maxlen)
1420 break;
1421 }
1422 }
1423 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1424 &i)))
1425 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001426 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 rlen = bestrlen;
1428 rbno = bestrbno;
1429 flen = bestflen;
1430 fbno = bestfbno;
1431 }
1432 args->wasfromfl = 0;
1433 /*
1434 * Fix up the length.
1435 */
1436 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001437 if (rlen < args->minlen) {
1438 if (!forced++) {
1439 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1440 trace_xfs_alloc_size_busy(args);
1441 xfs_log_force(args->mp, XFS_LOG_SYNC);
1442 goto restart;
1443 }
1444 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001446 xfs_alloc_fix_len(args);
1447
1448 if (!xfs_alloc_fix_minleft(args))
1449 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rlen = args->len;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001451 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /*
1453 * Allocate and initialize a cursor for the by-block tree.
1454 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001455 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1456 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1458 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1459 goto error0;
1460 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1461 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1462 cnt_cur = bno_cur = NULL;
1463 args->len = rlen;
1464 args->agbno = rbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001465 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001467 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001469 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471
1472error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001473 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (cnt_cur)
1475 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1476 if (bno_cur)
1477 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1478 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001479
1480out_nominleft:
1481 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1482 trace_xfs_alloc_size_nominleft(args);
1483 args->agbno = NULLAGBLOCK;
1484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/*
1488 * Deal with the case where only small freespaces remain.
1489 * Either return the contents of the last freespace record,
1490 * or allocate space from the freelist if there is nothing in the tree.
1491 */
1492STATIC int /* error */
1493xfs_alloc_ag_vextent_small(
1494 xfs_alloc_arg_t *args, /* allocation argument structure */
1495 xfs_btree_cur_t *ccur, /* by-size cursor */
1496 xfs_agblock_t *fbnop, /* result block number */
1497 xfs_extlen_t *flenp, /* result length */
1498 int *stat) /* status: 0-freelist, 1-normal/none */
1499{
1500 int error;
1501 xfs_agblock_t fbno;
1502 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int i;
1504
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001505 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto error0;
1507 if (i) {
1508 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1509 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001510 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 /*
1513 * Nothing in the btree, try the freelist. Make sure
1514 * to respect minleft even when pulling from the
1515 * freelist.
1516 */
1517 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001518 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1519 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001520 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1521 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 goto error0;
1523 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001524 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001525 args->userdata);
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (args->userdata) {
1528 xfs_buf_t *bp;
1529
1530 bp = xfs_btree_get_bufs(args->mp, args->tp,
1531 args->agno, fbno, 0);
1532 xfs_trans_binval(args->tp, bp);
1533 }
1534 args->len = 1;
1535 args->agbno = fbno;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001536 XFS_WANT_CORRUPTED_GOTO(args->mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001538 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 error0);
1540 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001541 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *stat = 0;
1543 return 0;
1544 }
1545 /*
1546 * Nothing in the freelist.
1547 */
1548 else
1549 flen = 0;
1550 }
1551 /*
1552 * Can't allocate from the freelist for some reason.
1553 */
Nathan Scottd432c802006-09-28 11:03:44 +10001554 else {
1555 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /*
1559 * Can't do the allocation, give up.
1560 */
1561 if (flen < args->minlen) {
1562 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001563 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 flen = 0;
1565 }
1566 *fbnop = fbno;
1567 *flenp = flen;
1568 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001569 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return 0;
1571
1572error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001573 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return error;
1575}
1576
1577/*
1578 * Free the extent starting at agno/bno for length.
1579 */
1580STATIC int /* error */
1581xfs_free_ag_extent(
1582 xfs_trans_t *tp, /* transaction pointer */
1583 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1584 xfs_agnumber_t agno, /* allocation group number */
1585 xfs_agblock_t bno, /* starting block number */
1586 xfs_extlen_t len, /* length of extent */
1587 int isfl) /* set if is freelist blocks - no sb acctg */
1588{
1589 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1590 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1591 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 xfs_agblock_t gtbno; /* start of right neighbor block */
1593 xfs_extlen_t gtlen; /* length of right neighbor block */
1594 int haveleft; /* have a left neighbor block */
1595 int haveright; /* have a right neighbor block */
1596 int i; /* temp, result code */
1597 xfs_agblock_t ltbno; /* start of left neighbor block */
1598 xfs_extlen_t ltlen; /* length of left neighbor block */
1599 xfs_mount_t *mp; /* mount point struct for filesystem */
1600 xfs_agblock_t nbno; /* new starting block of freespace */
1601 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001602 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 mp = tp->t_mountp;
1605 /*
1606 * Allocate and initialize a cursor for the by-block btree.
1607 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001608 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 cnt_cur = NULL;
1610 /*
1611 * Look for a neighboring block on the left (lower block numbers)
1612 * that is contiguous with this space.
1613 */
1614 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1615 goto error0;
1616 if (haveleft) {
1617 /*
1618 * There is a block to our left.
1619 */
1620 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1621 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001622 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 /*
1624 * It's not contiguous, though.
1625 */
1626 if (ltbno + ltlen < bno)
1627 haveleft = 0;
1628 else {
1629 /*
1630 * If this failure happens the request to free this
1631 * space was invalid, it's (partly) already free.
1632 * Very bad.
1633 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001634 XFS_WANT_CORRUPTED_GOTO(mp,
1635 ltbno + ltlen <= bno, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 }
1638 /*
1639 * Look for a neighboring block on the right (higher block numbers)
1640 * that is contiguous with this space.
1641 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001642 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto error0;
1644 if (haveright) {
1645 /*
1646 * There is a block to our right.
1647 */
1648 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1649 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001650 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * It's not contiguous, though.
1653 */
1654 if (bno + len < gtbno)
1655 haveright = 0;
1656 else {
1657 /*
1658 * If this failure happens the request to free this
1659 * space was invalid, it's (partly) already free.
1660 * Very bad.
1661 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001662 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 }
1665 /*
1666 * Now allocate and initialize a cursor for the by-size tree.
1667 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001668 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Have both left and right contiguous neighbors.
1671 * Merge all three into a single free block.
1672 */
1673 if (haveleft && haveright) {
1674 /*
1675 * Delete the old by-size entry on the left.
1676 */
1677 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1678 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001679 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001680 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001682 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Delete the old by-size entry on the right.
1685 */
1686 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1687 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001688 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001689 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001691 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Delete the old by-block entry for the right block.
1694 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001695 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001697 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /*
1699 * Move the by-block cursor back to the left neighbor.
1700 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001701 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001703 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#ifdef DEBUG
1705 /*
1706 * Check that this is the right record: delete didn't
1707 * mangle the cursor.
1708 */
1709 {
1710 xfs_agblock_t xxbno;
1711 xfs_extlen_t xxlen;
1712
1713 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1714 &i)))
1715 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001716 XFS_WANT_CORRUPTED_GOTO(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 i == 1 && xxbno == ltbno && xxlen == ltlen,
1718 error0);
1719 }
1720#endif
1721 /*
1722 * Update remaining by-block entry to the new, joined block.
1723 */
1724 nbno = ltbno;
1725 nlen = len + ltlen + gtlen;
1726 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1727 goto error0;
1728 }
1729 /*
1730 * Have only a left contiguous neighbor.
1731 * Merge it together with the new freespace.
1732 */
1733 else if (haveleft) {
1734 /*
1735 * Delete the old by-size entry on the left.
1736 */
1737 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1738 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001739 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001740 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001742 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * Back up the by-block cursor to the left neighbor, and
1745 * update its length.
1746 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001747 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001749 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 nbno = ltbno;
1751 nlen = len + ltlen;
1752 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1753 goto error0;
1754 }
1755 /*
1756 * Have only a right contiguous neighbor.
1757 * Merge it together with the new freespace.
1758 */
1759 else if (haveright) {
1760 /*
1761 * Delete the old by-size entry on the right.
1762 */
1763 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1764 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001765 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001766 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001768 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * Update the starting block and length of the right
1771 * neighbor in the by-block tree.
1772 */
1773 nbno = bno;
1774 nlen = len + gtlen;
1775 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1776 goto error0;
1777 }
1778 /*
1779 * No contiguous neighbors.
1780 * Insert the new freespace into the by-block tree.
1781 */
1782 else {
1783 nbno = bno;
1784 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001785 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001787 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1790 bno_cur = NULL;
1791 /*
1792 * In all cases we need to insert the new freespace in the by-size tree.
1793 */
1794 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1795 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001796 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001797 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001799 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1801 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /*
1804 * Update the freespace totals in the ag and superblock.
1805 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001806 pag = xfs_perag_get(mp, agno);
1807 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1808 xfs_perag_put(pag);
1809 if (error)
1810 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Christoph Hellwigecb69282011-03-04 12:59:55 +00001812 if (!isfl)
1813 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001814 XFS_STATS_INC(mp, xs_freex);
1815 XFS_STATS_ADD(mp, xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001816
1817 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
1820
1821 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001822 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (bno_cur)
1824 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1825 if (cnt_cur)
1826 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1827 return error;
1828}
1829
1830/*
1831 * Visible (exported) allocation/free functions.
1832 * Some of these are used just by xfs_alloc_btree.c and this file.
1833 */
1834
1835/*
1836 * Compute and fill in value of m_ag_maxlevels.
1837 */
1838void
1839xfs_alloc_compute_maxlevels(
1840 xfs_mount_t *mp) /* file system mount structure */
1841{
1842 int level;
1843 uint maxblocks;
1844 uint maxleafents;
1845 int minleafrecs;
1846 int minnoderecs;
1847
1848 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1849 minleafrecs = mp->m_alloc_mnr[0];
1850 minnoderecs = mp->m_alloc_mnr[1];
1851 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1852 for (level = 1; maxblocks > 1; level++)
1853 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1854 mp->m_ag_maxlevels = level;
1855}
1856
1857/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001858 * Find the length of the longest extent in an AG.
1859 */
1860xfs_extlen_t
1861xfs_alloc_longest_free_extent(
1862 struct xfs_mount *mp,
Dave Chinner50adbcb2015-06-22 10:04:31 +10001863 struct xfs_perag *pag,
1864 xfs_extlen_t need)
Dave Chinner6cc87642009-03-16 08:29:46 +01001865{
Dave Chinner50adbcb2015-06-22 10:04:31 +10001866 xfs_extlen_t delta = 0;
Dave Chinner6cc87642009-03-16 08:29:46 +01001867
Dave Chinner6cc87642009-03-16 08:29:46 +01001868 if (need > pag->pagf_flcount)
1869 delta = need - pag->pagf_flcount;
1870
1871 if (pag->pagf_longest > delta)
1872 return pag->pagf_longest - delta;
1873 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1874}
1875
Dave Chinner496817b2015-06-22 10:13:30 +10001876unsigned int
1877xfs_alloc_min_freelist(
1878 struct xfs_mount *mp,
1879 struct xfs_perag *pag)
1880{
1881 unsigned int min_free;
1882
1883 /* space needed by-bno freespace btree */
1884 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
1885 mp->m_ag_maxlevels);
1886 /* space needed by-size freespace btree */
1887 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
1888 mp->m_ag_maxlevels);
1889
1890 return min_free;
1891}
1892
Dave Chinner6cc87642009-03-16 08:29:46 +01001893/*
Dave Chinner72d55282015-06-22 10:04:42 +10001894 * Check if the operation we are fixing up the freelist for should go ahead or
1895 * not. If we are freeing blocks, we always allow it, otherwise the allocation
1896 * is dependent on whether the size and shape of free space available will
1897 * permit the requested allocation to take place.
1898 */
1899static bool
1900xfs_alloc_space_available(
1901 struct xfs_alloc_arg *args,
1902 xfs_extlen_t min_free,
1903 int flags)
1904{
1905 struct xfs_perag *pag = args->pag;
1906 xfs_extlen_t longest;
1907 int available;
1908
1909 if (flags & XFS_ALLOC_FLAG_FREEING)
1910 return true;
1911
1912 /* do we have enough contiguous free space for the allocation? */
1913 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free);
1914 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
1915 return false;
1916
1917 /* do have enough free space remaining for the allocation? */
1918 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
1919 min_free - args->total);
1920 if (available < (int)args->minleft)
1921 return false;
1922
1923 return true;
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/*
1927 * Decide whether to use this allocation group for this allocation.
1928 * If so, fix up the btree freelist's size.
1929 */
1930STATIC int /* error */
1931xfs_alloc_fix_freelist(
Dave Chinner396503f2015-06-22 10:13:19 +10001932 struct xfs_alloc_arg *args, /* allocation argument structure */
1933 int flags) /* XFS_ALLOC_FLAG_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Dave Chinner396503f2015-06-22 10:13:19 +10001935 struct xfs_mount *mp = args->mp;
1936 struct xfs_perag *pag = args->pag;
1937 struct xfs_trans *tp = args->tp;
1938 struct xfs_buf *agbp = NULL;
1939 struct xfs_buf *agflbp = NULL;
1940 struct xfs_alloc_arg targs; /* local allocation arguments */
1941 xfs_agblock_t bno; /* freelist block */
1942 xfs_extlen_t need; /* total blocks needed in freelist */
Jan Karac184f852015-08-25 10:05:13 +10001943 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (!pag->pagf_init) {
Dave Chinner396503f2015-06-22 10:13:19 +10001946 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1947 if (error)
1948 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001950 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1951 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001952 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Dave Chinner396503f2015-06-22 10:13:19 +10001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001956 /*
Dave Chinner396503f2015-06-22 10:13:19 +10001957 * If this is a metadata preferred pag and we are user data then try
1958 * somewhere else if we are not being asked to try harder at this
1959 * point
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001961 if (pag->pagf_metadata && args->userdata &&
1962 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1963 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001964 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
Dave Chinner496817b2015-06-22 10:13:30 +10001967 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001968 if (!xfs_alloc_space_available(args, need, flags))
1969 goto out_agbp_relse;
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * Get the a.g. freespace buffer.
1973 * Can fail if we're not blocking on locks, and it's held.
1974 */
Dave Chinner396503f2015-06-22 10:13:19 +10001975 if (!agbp) {
1976 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
1977 if (error)
1978 goto out_no_agbp;
1979 if (!agbp) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001980 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1981 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Dave Chinner396503f2015-06-22 10:13:19 +10001982 goto out_no_agbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10001985
Dave Chinner50adbcb2015-06-22 10:04:31 +10001986 /* If there isn't enough total space or single-extent, reject it. */
Dave Chinner496817b2015-06-22 10:13:30 +10001987 need = xfs_alloc_min_freelist(mp, pag);
Dave Chinner396503f2015-06-22 10:13:19 +10001988 if (!xfs_alloc_space_available(args, need, flags))
1989 goto out_agbp_relse;
Dave Chinner72d55282015-06-22 10:04:42 +10001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Make the freelist shorter if it's too long.
Dave Chinner50adbcb2015-06-22 10:04:31 +10001993 *
Dave Chinner396503f2015-06-22 10:13:19 +10001994 * Note that from this point onwards, we will always release the agf and
1995 * agfl buffers on error. This handles the case where we error out and
1996 * the buffers are clean or may not have been joined to the transaction
1997 * and hence need to be released manually. If they have been joined to
1998 * the transaction, then xfs_trans_brelse() will handle them
1999 * appropriately based on the recursion count and dirty state of the
2000 * buffer.
2001 *
Dave Chinner50adbcb2015-06-22 10:04:31 +10002002 * XXX (dgc): When we have lots of free space, does this buy us
2003 * anything other than extra overhead when we need to put more blocks
2004 * back on the free list? Maybe we should only do this when space is
2005 * getting low or the AGFL is more than half full?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 */
Dave Chinner50adbcb2015-06-22 10:04:31 +10002007 while (pag->pagf_flcount > need) {
2008 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
David Chinner92821e22007-05-24 15:26:31 +10002010 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2011 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002012 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002013 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1);
2014 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002015 goto out_agbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2017 xfs_trans_binval(tp, bp);
2018 }
Dave Chinner50adbcb2015-06-22 10:04:31 +10002019
Mark Tinguelya0041682012-09-20 13:16:45 -05002020 memset(&targs, 0, sizeof(targs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 targs.tp = tp;
2022 targs.mp = mp;
2023 targs.agbp = agbp;
2024 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2026 targs.type = XFS_ALLOCTYPE_THIS_AG;
2027 targs.pag = pag;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002028 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2029 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002030 goto out_agbp_relse;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002031
2032 /* Make the freelist longer if it's too short. */
2033 while (pag->pagf_flcount < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 targs.agbno = 0;
Dave Chinner50adbcb2015-06-22 10:04:31 +10002035 targs.maxlen = need - pag->pagf_flcount;
2036
2037 /* Allocate as many blocks as possible at once. */
2038 error = xfs_alloc_ag_vextent(&targs);
Dave Chinner396503f2015-06-22 10:13:19 +10002039 if (error)
2040 goto out_agflbp_relse;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Stop if we run out. Won't happen if callers are obeying
2044 * the restrictions correctly. Can happen for free calls
2045 * on a completely full ag.
2046 */
Yingping Lud210a282006-06-09 14:55:18 +10002047 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002048 if (flags & XFS_ALLOC_FLAG_FREEING)
2049 break;
Dave Chinner396503f2015-06-22 10:13:19 +10002050 goto out_agflbp_relse;
Yingping Lud210a282006-06-09 14:55:18 +10002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /*
2053 * Put each allocated block on the list.
2054 */
2055 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002056 error = xfs_alloc_put_freelist(tp, agbp,
2057 agflbp, bno, 0);
2058 if (error)
Dave Chinner396503f2015-06-22 10:13:19 +10002059 goto out_agflbp_relse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002062 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 args->agbp = agbp;
2064 return 0;
Dave Chinner396503f2015-06-22 10:13:19 +10002065
2066out_agflbp_relse:
2067 xfs_trans_brelse(tp, agflbp);
2068out_agbp_relse:
2069 if (agbp)
2070 xfs_trans_brelse(tp, agbp);
2071out_no_agbp:
2072 args->agbp = NULL;
2073 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
2076/*
2077 * Get a block from the freelist.
2078 * Returns with the buffer for the block gotten.
2079 */
2080int /* error */
2081xfs_alloc_get_freelist(
2082 xfs_trans_t *tp, /* transaction pointer */
2083 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002084 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2085 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2089 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002090 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int error;
David Chinner92821e22007-05-24 15:26:31 +10002092 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002093 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 xfs_perag_t *pag; /* per allocation group data */
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
2097 * Freelist is empty, give up.
2098 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002099 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (!agf->agf_flcount) {
2101 *bnop = NULLAGBLOCK;
2102 return 0;
2103 }
2104 /*
2105 * Read the array of free blocks.
2106 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002107 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2108 &agflbp);
2109 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002111
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /*
2114 * Get the block number and update the data structures.
2115 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002116 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2117 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002118 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002120 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002122
2123 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002124 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 xfs_trans_agflist_delta(tp, -1);
2126 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002127 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002128
2129 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2130 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002131 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002132 pag->pagf_btreeblks++;
2133 logflags |= XFS_AGF_BTREEBLKS;
2134 }
2135
David Chinner92821e22007-05-24 15:26:31 +10002136 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 *bnop = bno;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
2141
2142/*
2143 * Log the given fields from the agf structure.
2144 */
2145void
2146xfs_alloc_log_agf(
2147 xfs_trans_t *tp, /* transaction pointer */
2148 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2149 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2150{
2151 int first; /* first byte offset */
2152 int last; /* last byte offset */
2153 static const short offsets[] = {
2154 offsetof(xfs_agf_t, agf_magicnum),
2155 offsetof(xfs_agf_t, agf_versionnum),
2156 offsetof(xfs_agf_t, agf_seqno),
2157 offsetof(xfs_agf_t, agf_length),
2158 offsetof(xfs_agf_t, agf_roots[0]),
2159 offsetof(xfs_agf_t, agf_levels[0]),
2160 offsetof(xfs_agf_t, agf_flfirst),
2161 offsetof(xfs_agf_t, agf_fllast),
2162 offsetof(xfs_agf_t, agf_flcount),
2163 offsetof(xfs_agf_t, agf_freeblks),
2164 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002165 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002166 offsetof(xfs_agf_t, agf_uuid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 sizeof(xfs_agf_t)
2168 };
2169
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002170 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2171
Dave Chinner61fe1352013-04-03 16:11:30 +11002172 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2175 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2176}
2177
2178/*
2179 * Interface for inode allocation to force the pag data to be initialized.
2180 */
2181int /* error */
2182xfs_alloc_pagf_init(
2183 xfs_mount_t *mp, /* file system mount structure */
2184 xfs_trans_t *tp, /* transaction pointer */
2185 xfs_agnumber_t agno, /* allocation group number */
2186 int flags) /* XFS_ALLOC_FLAGS_... */
2187{
2188 xfs_buf_t *bp;
2189 int error;
2190
2191 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2192 return error;
2193 if (bp)
2194 xfs_trans_brelse(tp, bp);
2195 return 0;
2196}
2197
2198/*
2199 * Put the block on the freelist for the allocation group.
2200 */
2201int /* error */
2202xfs_alloc_put_freelist(
2203 xfs_trans_t *tp, /* transaction pointer */
2204 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2205 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002206 xfs_agblock_t bno, /* block being freed */
2207 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002210 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 int error;
David Chinner92821e22007-05-24 15:26:31 +10002212 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 xfs_mount_t *mp; /* mount structure */
2214 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002215 __be32 *agfl_bno;
2216 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 agf = XFS_BUF_TO_AGF(agbp);
2219 mp = tp->t_mountp;
2220
2221 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002222 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002224 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002225 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002227
2228 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002229 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xfs_trans_agflist_delta(tp, 1);
2231 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002232
2233 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2234 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002235 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002236 pag->pagf_btreeblks--;
2237 logflags |= XFS_AGF_BTREEBLKS;
2238 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002239 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002240
David Chinner92821e22007-05-24 15:26:31 +10002241 xfs_alloc_log_agf(tp, agbp, logflags);
2242
Christoph Hellwig16259e72005-11-02 15:11:25 +11002243 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002244
2245 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2246 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002247 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002248 startoff = (char *)blockp - (char *)agflbp->b_addr;
2249
David Chinner92821e22007-05-24 15:26:31 +10002250 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002251
Dave Chinner61fe1352013-04-03 16:11:30 +11002252 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002253 xfs_trans_log_buf(tp, agflbp, startoff,
2254 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return 0;
2256}
2257
Dave Chinner4e0e6042013-04-03 16:11:13 +11002258static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002259xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002260 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002261 struct xfs_buf *bp)
2262 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002263 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002264
Brian Fostera45086e2015-10-12 15:59:25 +11002265 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2266 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner4e0e6042013-04-03 16:11:13 +11002267 return false;
Brian Fostera45086e2015-10-12 15:59:25 +11002268 if (!xfs_log_check_lsn(mp,
2269 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2270 return false;
2271 }
Dave Chinner5d5f5272012-11-14 17:44:56 +11002272
Dave Chinner4e0e6042013-04-03 16:11:13 +11002273 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2274 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2275 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2276 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2277 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2278 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2279 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002280
Eric Sandeene1b05722014-09-09 11:47:24 +10002281 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2282 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2283 return false;
2284
Dave Chinner5d5f5272012-11-14 17:44:56 +11002285 /*
2286 * during growfs operations, the perag is not fully initialised,
2287 * so we can't use it for any useful checking. growfs ensures we can't
2288 * use it by using uncached buffers that don't have the perag attached
2289 * so we can detect and avoid this problem.
2290 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002291 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2292 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002293
Dave Chinner4e0e6042013-04-03 16:11:13 +11002294 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2295 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2296 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002297
Dave Chinner4e0e6042013-04-03 16:11:13 +11002298 return true;;
2299
Dave Chinner612cfbf2012-11-14 17:52:32 +11002300}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002301
Dave Chinner1813dd62012-11-14 17:54:40 +11002302static void
2303xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002304 struct xfs_buf *bp)
2305{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002306 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner4e0e6042013-04-03 16:11:13 +11002307
Eric Sandeence5028c2014-02-27 15:23:10 +11002308 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2309 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002310 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002311 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2312 XFS_ERRTAG_ALLOC_READ_AGF,
2313 XFS_RANDOM_ALLOC_READ_AGF))
Dave Chinner24513372014-06-25 14:58:08 +10002314 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002315
2316 if (bp->b_error)
2317 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002318}
2319
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002320static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002321xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002322 struct xfs_buf *bp)
2323{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002324 struct xfs_mount *mp = bp->b_target->bt_mount;
2325 struct xfs_buf_log_item *bip = bp->b_fspriv;
2326
2327 if (!xfs_agf_verify(mp, bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002328 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002329 xfs_verifier_error(bp);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002330 return;
2331 }
2332
2333 if (!xfs_sb_version_hascrc(&mp->m_sb))
2334 return;
2335
2336 if (bip)
2337 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2338
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002339 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002340}
2341
Dave Chinner1813dd62012-11-14 17:54:40 +11002342const struct xfs_buf_ops xfs_agf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002343 .name = "xfs_agf",
Dave Chinner1813dd62012-11-14 17:54:40 +11002344 .verify_read = xfs_agf_read_verify,
2345 .verify_write = xfs_agf_write_verify,
2346};
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
2349 * Read in the allocation group header (free/alloc section).
2350 */
2351int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002352xfs_read_agf(
2353 struct xfs_mount *mp, /* mount point structure */
2354 struct xfs_trans *tp, /* transaction pointer */
2355 xfs_agnumber_t agno, /* allocation group number */
2356 int flags, /* XFS_BUF_ */
2357 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int error;
2360
Dave Chinnerd1230312013-11-01 15:27:19 +11002361 trace_xfs_read_agf(mp, agno);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 ASSERT(agno != NULLAGNUMBER);
2364 error = xfs_trans_read_buf(
2365 mp, tp, mp->m_ddev_targp,
2366 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002367 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (error)
2369 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002370 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002372
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002373 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002374 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002375 return 0;
2376}
2377
2378/*
2379 * Read in the allocation group header (free/alloc section).
2380 */
2381int /* error */
2382xfs_alloc_read_agf(
2383 struct xfs_mount *mp, /* mount point structure */
2384 struct xfs_trans *tp, /* transaction pointer */
2385 xfs_agnumber_t agno, /* allocation group number */
2386 int flags, /* XFS_ALLOC_FLAG_... */
2387 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2388{
2389 struct xfs_agf *agf; /* ag freelist header */
2390 struct xfs_perag *pag; /* per allocation group data */
2391 int error;
2392
Dave Chinnerd1230312013-11-01 15:27:19 +11002393 trace_xfs_alloc_read_agf(mp, agno);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002394
Dave Chinnerd1230312013-11-01 15:27:19 +11002395 ASSERT(agno != NULLAGNUMBER);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002396 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002397 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002398 bpp);
2399 if (error)
2400 return error;
2401 if (!*bpp)
2402 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002403 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002404
2405 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002406 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002408 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002409 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002410 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2411 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002413 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002415 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002416 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002417 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002418 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 pag->pagf_init = 1;
2420 }
2421#ifdef DEBUG
2422 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002423 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002424 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002425 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2426 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002428 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002430 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002433 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return 0;
2435}
2436
2437/*
2438 * Allocate an extent (variable-size).
2439 * Depending on the allocation type, we either look in a single allocation
2440 * group or loop over the allocation groups to find the result.
2441 */
2442int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002443xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 xfs_alloc_arg_t *args) /* allocation argument structure */
2445{
2446 xfs_agblock_t agsize; /* allocation group size */
2447 int error;
2448 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 xfs_extlen_t minleft;/* minimum left value, temp copy */
2450 xfs_mount_t *mp; /* mount structure pointer */
2451 xfs_agnumber_t sagno; /* starting allocation group number */
2452 xfs_alloctype_t type; /* input allocation type */
2453 int bump_rotor = 0;
2454 int no_min = 0;
2455 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2456
2457 mp = args->mp;
2458 type = args->otype = args->type;
2459 args->agbno = NULLAGBLOCK;
2460 /*
2461 * Just fix this up, for the case where the last a.g. is shorter
2462 * (or there's only one a.g.) and the caller couldn't easily figure
2463 * that out (xfs_bmap_alloc).
2464 */
2465 agsize = mp->m_sb.sb_agblocks;
2466 if (args->maxlen > agsize)
2467 args->maxlen = agsize;
2468 if (args->alignment == 0)
2469 args->alignment = 1;
2470 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2471 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2472 ASSERT(args->minlen <= args->maxlen);
2473 ASSERT(args->minlen <= agsize);
2474 ASSERT(args->mod < args->prod);
2475 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2476 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2477 args->minlen > args->maxlen || args->minlen > agsize ||
2478 args->mod >= args->prod) {
2479 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002480 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return 0;
2482 }
2483 minleft = args->minleft;
2484
2485 switch (type) {
2486 case XFS_ALLOCTYPE_THIS_AG:
2487 case XFS_ALLOCTYPE_NEAR_BNO:
2488 case XFS_ALLOCTYPE_THIS_BNO:
2489 /*
2490 * These three force us into a single a.g.
2491 */
2492 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002493 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 args->minleft = 0;
2495 error = xfs_alloc_fix_freelist(args, 0);
2496 args->minleft = minleft;
2497 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002498 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto error0;
2500 }
2501 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002502 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 break;
2504 }
2505 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2506 if ((error = xfs_alloc_ag_vextent(args)))
2507 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 break;
2509 case XFS_ALLOCTYPE_START_BNO:
2510 /*
2511 * Try near allocation first, then anywhere-in-ag after
2512 * the first a.g. fails.
2513 */
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002514 if ((args->userdata & XFS_ALLOC_INITIAL_USER_DATA) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2516 args->fsbno = XFS_AGB_TO_FSB(mp,
2517 ((mp->m_agfrotor / rotorstep) %
2518 mp->m_sb.sb_agcount), 0);
2519 bump_rotor = 1;
2520 }
2521 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2522 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2523 /* FALLTHROUGH */
2524 case XFS_ALLOCTYPE_ANY_AG:
2525 case XFS_ALLOCTYPE_START_AG:
2526 case XFS_ALLOCTYPE_FIRST_AG:
2527 /*
2528 * Rotate through the allocation groups looking for a winner.
2529 */
2530 if (type == XFS_ALLOCTYPE_ANY_AG) {
2531 /*
2532 * Start with the last place we left off.
2533 */
2534 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2535 mp->m_sb.sb_agcount;
2536 args->type = XFS_ALLOCTYPE_THIS_AG;
2537 flags = XFS_ALLOC_FLAG_TRYLOCK;
2538 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2539 /*
2540 * Start with allocation group given by bno.
2541 */
2542 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2543 args->type = XFS_ALLOCTYPE_THIS_AG;
2544 sagno = 0;
2545 flags = 0;
2546 } else {
2547 if (type == XFS_ALLOCTYPE_START_AG)
2548 args->type = XFS_ALLOCTYPE_THIS_AG;
2549 /*
2550 * Start with the given allocation group.
2551 */
2552 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2553 flags = XFS_ALLOC_FLAG_TRYLOCK;
2554 }
2555 /*
2556 * Loop over allocation groups twice; first time with
2557 * trylock set, second time without.
2558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002560 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (no_min) args->minleft = 0;
2562 error = xfs_alloc_fix_freelist(args, flags);
2563 args->minleft = minleft;
2564 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002565 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto error0;
2567 }
2568 /*
2569 * If we get a buffer back then the allocation will fly.
2570 */
2571 if (args->agbp) {
2572 if ((error = xfs_alloc_ag_vextent(args)))
2573 goto error0;
2574 break;
2575 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002576
2577 trace_xfs_alloc_vextent_loopfailed(args);
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 /*
2580 * Didn't work, figure out the next iteration.
2581 */
2582 if (args->agno == sagno &&
2583 type == XFS_ALLOCTYPE_START_BNO)
2584 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002585 /*
2586 * For the first allocation, we can try any AG to get
2587 * space. However, if we already have allocated a
2588 * block, we don't want to try AGs whose number is below
2589 * sagno. Otherwise, we may end up with out-of-order
2590 * locking of AGF, which might cause deadlock.
2591 */
2592 if (++(args->agno) == mp->m_sb.sb_agcount) {
2593 if (args->firstblock != NULLFSBLOCK)
2594 args->agno = sagno;
2595 else
2596 args->agno = 0;
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /*
2599 * Reached the starting a.g., must either be done
2600 * or switch to non-trylock mode.
2601 */
2602 if (args->agno == sagno) {
2603 if (no_min == 1) {
2604 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002605 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 break;
2607 }
2608 if (flags == 0) {
2609 no_min = 1;
2610 } else {
2611 flags = 0;
2612 if (type == XFS_ALLOCTYPE_START_BNO) {
2613 args->agbno = XFS_FSB_TO_AGBNO(mp,
2614 args->fsbno);
2615 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2616 }
2617 }
2618 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002619 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2622 if (args->agno == sagno)
2623 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2624 (mp->m_sb.sb_agcount * rotorstep);
2625 else
2626 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2627 (mp->m_sb.sb_agcount * rotorstep);
2628 }
2629 break;
2630 default:
2631 ASSERT(0);
2632 /* NOTREACHED */
2633 }
2634 if (args->agbno == NULLAGBLOCK)
2635 args->fsbno = NULLFSBLOCK;
2636 else {
2637 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2638#ifdef DEBUG
2639 ASSERT(args->len >= args->minlen);
2640 ASSERT(args->len <= args->maxlen);
2641 ASSERT(args->agbno % args->alignment == 0);
2642 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2643 args->len);
2644#endif
Dave Chinner3fbbbea2015-11-03 12:27:22 +11002645
2646 /* Zero the extent if we were asked to do so */
2647 if (args->userdata & XFS_ALLOC_USERDATA_ZERO) {
2648 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2649 if (error)
2650 goto error0;
2651 }
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002654 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return 0;
2656error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002657 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return error;
2659}
2660
2661/*
2662 * Free an extent.
2663 * Just break up the extent address and hand off to xfs_free_ag_extent
2664 * after fixing up the freelist.
2665 */
2666int /* error */
2667xfs_free_extent(
2668 xfs_trans_t *tp, /* transaction pointer */
2669 xfs_fsblock_t bno, /* starting block number of extent */
2670 xfs_extlen_t len) /* length of extent */
2671{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002672 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int error;
2674
2675 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002676 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 args.tp = tp;
2678 args.mp = tp->t_mountp;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002679
2680 /*
2681 * validate that the block number is legal - the enables us to detect
2682 * and handle a silent filesystem corruption rather than crashing.
2683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002685 if (args.agno >= args.mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +10002686 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002689 if (args.agbno >= args.mp->m_sb.sb_agblocks)
Dave Chinner24513372014-06-25 14:58:08 +10002690 return -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002691
Dave Chinnera862e0f2010-01-11 11:47:41 +00002692 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002693 ASSERT(args.pag);
2694
2695 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2696 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 goto error0;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002698
2699 /* validate the extent size is legal now we have the agf locked */
2700 if (args.agbno + len >
2701 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
Dave Chinner24513372014-06-25 14:58:08 +10002702 error = -EFSCORRUPTED;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002703 goto error0;
2704 }
2705
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002706 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Christoph Hellwiga870acd2011-04-24 19:06:14 +00002707 if (!error)
Dave Chinner4ecbfe62012-04-29 10:41:10 +00002708 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002710 xfs_perag_put(args.pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return error;
2712}