blob: 999db7a95663aa9f1e05254bfaf2542279e58670 [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"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000035#include "xfs_extent_busy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_error.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110037#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000038#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110039#include "xfs_trans.h"
Dave Chinner4e0e6042013-04-03 16:11:13 +110040#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110041#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Dave Chinnerc999a222012-03-22 05:15:07 +000043struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
46
47#define XFSA_FIXUP_BNO_OK 1
48#define XFSA_FIXUP_CNT_OK 2
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
52STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
53STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000054 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110057 * Lookup the record equal to [bno, len] in the btree given by cur.
58 */
59STATIC int /* error */
60xfs_alloc_lookup_eq(
61 struct xfs_btree_cur *cur, /* btree cursor */
62 xfs_agblock_t bno, /* starting block of extent */
63 xfs_extlen_t len, /* length of extent */
64 int *stat) /* success/failure */
65{
66 cur->bc_rec.a.ar_startblock = bno;
67 cur->bc_rec.a.ar_blockcount = len;
68 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
69}
70
71/*
72 * Lookup the first record greater than or equal to [bno, len]
73 * in the btree given by cur.
74 */
Dave Chinnera66d6362012-03-22 05:15:12 +000075int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110076xfs_alloc_lookup_ge(
77 struct xfs_btree_cur *cur, /* btree cursor */
78 xfs_agblock_t bno, /* starting block of extent */
79 xfs_extlen_t len, /* length of extent */
80 int *stat) /* success/failure */
81{
82 cur->bc_rec.a.ar_startblock = bno;
83 cur->bc_rec.a.ar_blockcount = len;
84 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
85}
86
87/*
88 * Lookup the first record less than or equal to [bno, len]
89 * in the btree given by cur.
90 */
Christoph Hellwiga46db602011-01-07 13:02:04 +000091int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110092xfs_alloc_lookup_le(
93 struct xfs_btree_cur *cur, /* btree cursor */
94 xfs_agblock_t bno, /* starting block of extent */
95 xfs_extlen_t len, /* length of extent */
96 int *stat) /* success/failure */
97{
98 cur->bc_rec.a.ar_startblock = bno;
99 cur->bc_rec.a.ar_blockcount = len;
100 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
101}
102
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100103/*
104 * Update the record referred to by cur to the value given
105 * by [bno, len].
106 * This either works (return 0) or gets an EFSCORRUPTED error.
107 */
108STATIC int /* error */
109xfs_alloc_update(
110 struct xfs_btree_cur *cur, /* btree cursor */
111 xfs_agblock_t bno, /* starting block of extent */
112 xfs_extlen_t len) /* length of extent */
113{
114 union xfs_btree_rec rec;
115
116 rec.alloc.ar_startblock = cpu_to_be32(bno);
117 rec.alloc.ar_blockcount = cpu_to_be32(len);
118 return xfs_btree_update(cur, &rec);
119}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100120
121/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100122 * Get the data from the pointed-to record.
123 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000124int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100125xfs_alloc_get_rec(
126 struct xfs_btree_cur *cur, /* btree cursor */
127 xfs_agblock_t *bno, /* output: starting block of extent */
128 xfs_extlen_t *len, /* output: length of extent */
129 int *stat) /* output: success/failure */
130{
131 union xfs_btree_rec *rec;
132 int error;
133
134 error = xfs_btree_get_rec(cur, &rec, stat);
135 if (!error && *stat == 1) {
136 *bno = be32_to_cpu(rec->alloc.ar_startblock);
137 *len = be32_to_cpu(rec->alloc.ar_blockcount);
138 }
139 return error;
140}
141
142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * Compute aligned version of the found extent.
144 * Takes alignment and min length into account.
145 */
David Chinner12375c82008-04-10 12:21:32 +1000146STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000148 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xfs_agblock_t foundbno, /* starting block in found extent */
150 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xfs_agblock_t *resbno, /* result block number */
152 xfs_extlen_t *reslen) /* result length */
153{
154 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 xfs_extlen_t len;
156
Christoph Hellwige26f0502011-04-24 19:06:15 +0000157 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000158 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000159
160 if (args->alignment > 1 && len >= args->minlen) {
161 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
162 xfs_extlen_t diff = aligned_bno - bno;
163
164 *resbno = aligned_bno;
165 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000167 *resbno = bno;
168 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * Compute best start block and diff for "near" allocations.
174 * freelen >= wantlen already checked by caller.
175 */
176STATIC xfs_extlen_t /* difference value (absolute) */
177xfs_alloc_compute_diff(
178 xfs_agblock_t wantbno, /* target starting block */
179 xfs_extlen_t wantlen, /* target length */
180 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200181 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 xfs_agblock_t freebno, /* freespace's starting block */
183 xfs_extlen_t freelen, /* freespace's length */
184 xfs_agblock_t *newbnop) /* result: best start block from free */
185{
186 xfs_agblock_t freeend; /* end of freespace extent */
187 xfs_agblock_t newbno1; /* return block number */
188 xfs_agblock_t newbno2; /* other new block number */
189 xfs_extlen_t newlen1=0; /* length with newbno1 */
190 xfs_extlen_t newlen2=0; /* length with newbno2 */
191 xfs_agblock_t wantend; /* end of target extent */
192
193 ASSERT(freelen >= wantlen);
194 freeend = freebno + freelen;
195 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200196 /*
197 * We want to allocate from the start of a free extent if it is past
198 * the desired block or if we are allocating user data and the free
199 * extent is before desired block. The second case is there to allow
200 * for contiguous allocation from the remaining free space if the file
201 * grows in the short term.
202 */
203 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
205 newbno1 = NULLAGBLOCK;
206 } else if (freeend >= wantend && alignment > 1) {
207 newbno1 = roundup(wantbno, alignment);
208 newbno2 = newbno1 - alignment;
209 if (newbno1 >= freeend)
210 newbno1 = NULLAGBLOCK;
211 else
212 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
213 if (newbno2 < freebno)
214 newbno2 = NULLAGBLOCK;
215 else
216 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
217 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
218 if (newlen1 < newlen2 ||
219 (newlen1 == newlen2 &&
220 XFS_ABSDIFF(newbno1, wantbno) >
221 XFS_ABSDIFF(newbno2, wantbno)))
222 newbno1 = newbno2;
223 } else if (newbno2 != NULLAGBLOCK)
224 newbno1 = newbno2;
225 } else if (freeend >= wantend) {
226 newbno1 = wantbno;
227 } else if (alignment > 1) {
228 newbno1 = roundup(freeend - wantlen, alignment);
229 if (newbno1 > freeend - wantlen &&
230 newbno1 - alignment >= freebno)
231 newbno1 -= alignment;
232 else if (newbno1 >= freeend)
233 newbno1 = NULLAGBLOCK;
234 } else
235 newbno1 = freeend - wantlen;
236 *newbnop = newbno1;
237 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
238}
239
240/*
241 * Fix up the length, based on mod and prod.
242 * len should be k * prod + mod for some k.
243 * If len is too small it is returned unchanged.
244 * If len hits maxlen it is left alone.
245 */
246STATIC void
247xfs_alloc_fix_len(
248 xfs_alloc_arg_t *args) /* allocation argument structure */
249{
250 xfs_extlen_t k;
251 xfs_extlen_t rlen;
252
253 ASSERT(args->mod < args->prod);
254 rlen = args->len;
255 ASSERT(rlen >= args->minlen);
256 ASSERT(rlen <= args->maxlen);
257 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
258 (args->mod == 0 && rlen < args->prod))
259 return;
260 k = rlen % args->prod;
261 if (k == args->mod)
262 return;
263 if (k > args->mod) {
264 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
265 return;
266 } else {
267 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
268 (int)args->minlen)
269 return;
270 }
271 ASSERT(rlen >= args->minlen);
272 ASSERT(rlen <= args->maxlen);
273 args->len = rlen;
274}
275
276/*
277 * Fix up length if there is too little space left in the a.g.
278 * Return 1 if ok, 0 if too little, should give up.
279 */
280STATIC int
281xfs_alloc_fix_minleft(
282 xfs_alloc_arg_t *args) /* allocation argument structure */
283{
284 xfs_agf_t *agf; /* a.g. freelist header */
285 int diff; /* free space difference */
286
287 if (args->minleft == 0)
288 return 1;
289 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100290 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 - args->len - args->minleft;
292 if (diff >= 0)
293 return 1;
294 args->len += diff; /* shrink the allocated space */
295 if (args->len >= args->minlen)
296 return 1;
297 args->agbno = NULLAGBLOCK;
298 return 0;
299}
300
301/*
302 * Update the two btrees, logically removing from freespace the extent
303 * starting at rbno, rlen blocks. The extent is contained within the
304 * actual (current) free extent fbno for flen blocks.
305 * Flags are passed in indicating whether the cursors are set to the
306 * relevant records.
307 */
308STATIC int /* error code */
309xfs_alloc_fixup_trees(
310 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
311 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
312 xfs_agblock_t fbno, /* starting block of free extent */
313 xfs_extlen_t flen, /* length of free extent */
314 xfs_agblock_t rbno, /* starting block of returned extent */
315 xfs_extlen_t rlen, /* length of returned extent */
316 int flags) /* flags, XFSA_FIXUP_... */
317{
318 int error; /* error code */
319 int i; /* operation results */
320 xfs_agblock_t nfbno1; /* first new free startblock */
321 xfs_agblock_t nfbno2; /* second new free startblock */
322 xfs_extlen_t nflen1=0; /* first new free length */
323 xfs_extlen_t nflen2=0; /* second new free length */
324
325 /*
326 * Look up the record in the by-size tree if necessary.
327 */
328 if (flags & XFSA_FIXUP_CNT_OK) {
329#ifdef DEBUG
330 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
331 return error;
332 XFS_WANT_CORRUPTED_RETURN(
333 i == 1 && nfbno1 == fbno && nflen1 == flen);
334#endif
335 } else {
336 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
337 return error;
338 XFS_WANT_CORRUPTED_RETURN(i == 1);
339 }
340 /*
341 * Look up the record in the by-block tree if necessary.
342 */
343 if (flags & XFSA_FIXUP_BNO_OK) {
344#ifdef DEBUG
345 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
346 return error;
347 XFS_WANT_CORRUPTED_RETURN(
348 i == 1 && nfbno1 == fbno && nflen1 == flen);
349#endif
350 } else {
351 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
352 return error;
353 XFS_WANT_CORRUPTED_RETURN(i == 1);
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100356#ifdef DEBUG
357 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
358 struct xfs_btree_block *bnoblock;
359 struct xfs_btree_block *cntblock;
360
361 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
362 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
363
364 XFS_WANT_CORRUPTED_RETURN(
365 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
370 * Deal with all four cases: the allocated record is contained
371 * within the freespace record, so we can have new freespace
372 * at either (or both) end, or no freespace remaining.
373 */
374 if (rbno == fbno && rlen == flen)
375 nfbno1 = nfbno2 = NULLAGBLOCK;
376 else if (rbno == fbno) {
377 nfbno1 = rbno + rlen;
378 nflen1 = flen - rlen;
379 nfbno2 = NULLAGBLOCK;
380 } else if (rbno + rlen == fbno + flen) {
381 nfbno1 = fbno;
382 nflen1 = flen - rlen;
383 nfbno2 = NULLAGBLOCK;
384 } else {
385 nfbno1 = fbno;
386 nflen1 = rbno - fbno;
387 nfbno2 = rbno + rlen;
388 nflen2 = (fbno + flen) - nfbno2;
389 }
390 /*
391 * Delete the entry from the by-size btree.
392 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100393 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return error;
395 XFS_WANT_CORRUPTED_RETURN(i == 1);
396 /*
397 * Add new by-size btree entry(s).
398 */
399 if (nfbno1 != NULLAGBLOCK) {
400 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
401 return error;
402 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100403 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return error;
405 XFS_WANT_CORRUPTED_RETURN(i == 1);
406 }
407 if (nfbno2 != NULLAGBLOCK) {
408 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
409 return error;
410 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100411 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return error;
413 XFS_WANT_CORRUPTED_RETURN(i == 1);
414 }
415 /*
416 * Fix up the by-block btree entry(s).
417 */
418 if (nfbno1 == NULLAGBLOCK) {
419 /*
420 * No remaining freespace, just delete the by-block tree entry.
421 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100422 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return error;
424 XFS_WANT_CORRUPTED_RETURN(i == 1);
425 } else {
426 /*
427 * Update the by-block entry to start later|be shorter.
428 */
429 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
430 return error;
431 }
432 if (nfbno2 != NULLAGBLOCK) {
433 /*
434 * 2 resulting free entries, need to add one.
435 */
436 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
437 return error;
438 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100439 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return error;
441 XFS_WANT_CORRUPTED_RETURN(i == 1);
442 }
443 return 0;
444}
445
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100446static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100447xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100448 struct xfs_buf *bp)
449{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100450 struct xfs_mount *mp = bp->b_target->bt_mount;
451 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100452 int i;
453
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100454 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_uuid))
455 return false;
456 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
457 return false;
458 /*
459 * during growfs operations, the perag is not fully initialised,
460 * so we can't use it for any useful checking. growfs ensures we can't
461 * use it by using uncached buffers that don't have the perag attached
462 * so we can detect and avoid this problem.
463 */
464 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
465 return false;
466
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100467 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100468 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100469 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100470 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100471 }
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100472 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100473}
474
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100475static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100476xfs_agfl_read_verify(
477 struct xfs_buf *bp)
478{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100479 struct xfs_mount *mp = bp->b_target->bt_mount;
480 int agfl_ok = 1;
481
482 /*
483 * There is no verification of non-crc AGFLs because mkfs does not
484 * initialise the AGFL to zero or NULL. Hence the only valid part of the
485 * AGFL is what the AGF says is active. We can't get to the AGF, so we
486 * can't verify just those entries are valid.
487 */
488 if (!xfs_sb_version_hascrc(&mp->m_sb))
489 return;
490
491 agfl_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
492 offsetof(struct xfs_agfl, agfl_crc));
493
494 agfl_ok = agfl_ok && xfs_agfl_verify(bp);
495
496 if (!agfl_ok) {
497 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
498 xfs_buf_ioerror(bp, EFSCORRUPTED);
499 }
500}
501
502static void
503xfs_agfl_write_verify(
504 struct xfs_buf *bp)
505{
506 struct xfs_mount *mp = bp->b_target->bt_mount;
507 struct xfs_buf_log_item *bip = bp->b_fspriv;
508
509 /* no verification of non-crc AGFLs */
510 if (!xfs_sb_version_hascrc(&mp->m_sb))
511 return;
512
513 if (!xfs_agfl_verify(bp)) {
514 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
515 xfs_buf_ioerror(bp, EFSCORRUPTED);
516 return;
517 }
518
519 if (bip)
520 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
521
522 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
523 offsetof(struct xfs_agfl, agfl_crc));
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100524}
525
Dave Chinner1813dd62012-11-14 17:54:40 +1100526const struct xfs_buf_ops xfs_agfl_buf_ops = {
527 .verify_read = xfs_agfl_read_verify,
528 .verify_write = xfs_agfl_write_verify,
529};
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * Read in the allocation group free block array.
533 */
534STATIC int /* error */
535xfs_alloc_read_agfl(
536 xfs_mount_t *mp, /* mount point structure */
537 xfs_trans_t *tp, /* transaction pointer */
538 xfs_agnumber_t agno, /* allocation group number */
539 xfs_buf_t **bpp) /* buffer for the ag free block array */
540{
541 xfs_buf_t *bp; /* return value */
542 int error;
543
544 ASSERT(agno != NULLAGNUMBER);
545 error = xfs_trans_read_buf(
546 mp, tp, mp->m_ddev_targp,
547 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100548 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (error)
550 return error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000551 ASSERT(!xfs_buf_geterror(bp));
Christoph Hellwig38f23232011-10-10 16:52:45 +0000552 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *bpp = bp;
554 return 0;
555}
556
Christoph Hellwigecb69282011-03-04 12:59:55 +0000557STATIC int
558xfs_alloc_update_counters(
559 struct xfs_trans *tp,
560 struct xfs_perag *pag,
561 struct xfs_buf *agbp,
562 long len)
563{
564 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
565
566 pag->pagf_freeblks += len;
567 be32_add_cpu(&agf->agf_freeblks, len);
568
569 xfs_trans_agblocks_delta(tp, len);
570 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
571 be32_to_cpu(agf->agf_length)))
572 return EFSCORRUPTED;
573
574 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
575 return 0;
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Allocation group level functions.
580 */
581
582/*
583 * Allocate a variable extent in the allocation group agno.
584 * Type and bno are used to determine where in the allocation group the
585 * extent will start.
586 * Extent's length (returned in *len) will be between minlen and maxlen,
587 * and of the form k * prod + mod unless there's nothing that large.
588 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
589 */
590STATIC int /* error */
591xfs_alloc_ag_vextent(
592 xfs_alloc_arg_t *args) /* argument structure for allocation */
593{
594 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 ASSERT(args->minlen > 0);
597 ASSERT(args->maxlen > 0);
598 ASSERT(args->minlen <= args->maxlen);
599 ASSERT(args->mod < args->prod);
600 ASSERT(args->alignment > 0);
601 /*
602 * Branch to correct routine based on the type.
603 */
604 args->wasfromfl = 0;
605 switch (args->type) {
606 case XFS_ALLOCTYPE_THIS_AG:
607 error = xfs_alloc_ag_vextent_size(args);
608 break;
609 case XFS_ALLOCTYPE_NEAR_BNO:
610 error = xfs_alloc_ag_vextent_near(args);
611 break;
612 case XFS_ALLOCTYPE_THIS_BNO:
613 error = xfs_alloc_ag_vextent_exact(args);
614 break;
615 default:
616 ASSERT(0);
617 /* NOTREACHED */
618 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000619
620 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Christoph Hellwigecb69282011-03-04 12:59:55 +0000623 ASSERT(args->len >= args->minlen);
624 ASSERT(args->len <= args->maxlen);
625 ASSERT(!args->wasfromfl || !args->isfl);
626 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Christoph Hellwigecb69282011-03-04 12:59:55 +0000628 if (!args->wasfromfl) {
629 error = xfs_alloc_update_counters(args->tp, args->pag,
630 args->agbp,
631 -((long)(args->len)));
632 if (error)
633 return error;
634
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000635 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000636 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000638
639 if (!args->isfl) {
640 xfs_trans_mod_sb(args->tp, args->wasdel ?
641 XFS_TRANS_SB_RES_FDBLOCKS :
642 XFS_TRANS_SB_FDBLOCKS,
643 -((long)(args->len)));
644 }
645
646 XFS_STATS_INC(xs_allocx);
647 XFS_STATS_ADD(xs_allocb, args->len);
648 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Allocate a variable extent at exactly agno/bno.
653 * Extent's length (returned in *len) will be between minlen and maxlen,
654 * and of the form k * prod + mod unless there's nothing that large.
655 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
656 */
657STATIC int /* error */
658xfs_alloc_ag_vextent_exact(
659 xfs_alloc_arg_t *args) /* allocation argument structure */
660{
661 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
662 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int error;
664 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000666 xfs_agblock_t tbno; /* start block of trimmed extent */
667 xfs_extlen_t tlen; /* length of trimmed extent */
668 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Allocate/initialize a cursor for the by-number freespace btree.
675 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100676 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000677 args->agno, XFS_BTNUM_BNO);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
681 * Look for the closest free block <= bno, it must contain bno
682 * if any free block does.
683 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000684 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
685 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000687 if (!i)
688 goto not_found;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Grab the freespace record.
692 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000693 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
694 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto error0;
696 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
697 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000700 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000702 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000703
704 /*
705 * Give up if the start of the extent is busy, or the freespace isn't
706 * long enough for the minimum request.
707 */
708 if (tbno > args->agbno)
709 goto not_found;
710 if (tlen < args->minlen)
711 goto not_found;
712 tend = tbno + tlen;
713 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000714 goto not_found;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * End of extent will be smaller of the freespace end and the
718 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * Fix the length according to mod and prod if given.
721 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000722 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
723 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000725 if (!xfs_alloc_fix_minleft(args))
726 goto not_found;
727
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000728 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000731 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * Allocate/initialize a cursor for the by-size btree.
733 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100734 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
735 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100737 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000738 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
739 args->len, XFSA_FIXUP_BNO_OK);
740 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
742 goto error0;
743 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
746 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000749 trace_xfs_alloc_exact_done(args);
750 return 0;
751
752not_found:
753 /* Didn't find it, return null. */
754 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
755 args->agbno = NULLAGBLOCK;
756 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758
759error0:
760 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000761 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return error;
763}
764
765/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000766 * Search the btree in a given direction via the search cursor and compare
767 * the records found against the good extent we've already found.
768 */
769STATIC int
770xfs_alloc_find_best_extent(
771 struct xfs_alloc_arg *args, /* allocation argument structure */
772 struct xfs_btree_cur **gcur, /* good cursor */
773 struct xfs_btree_cur **scur, /* searching cursor */
774 xfs_agblock_t gdiff, /* difference for search comparison */
775 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000776 xfs_extlen_t *slen, /* extent length */
777 xfs_agblock_t *sbnoa, /* aligned extent found by search */
778 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000779 int dir) /* 0 = search right, 1 = search left */
780{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000781 xfs_agblock_t new;
782 xfs_agblock_t sdiff;
783 int error;
784 int i;
785
786 /* The good extent is perfect, no need to search. */
787 if (!gdiff)
788 goto out_use_good;
789
790 /*
791 * Look until we find a better one, run out of space or run off the end.
792 */
793 do {
794 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
795 if (error)
796 goto error0;
797 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000798 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000799
800 /*
801 * The good extent is closer than this one.
802 */
803 if (!dir) {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000804 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000805 goto out_use_good;
806 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000807 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000808 goto out_use_good;
809 }
810
811 /*
812 * Same distance, compare length and pick the best.
813 */
814 if (*slena >= args->minlen) {
815 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
816 xfs_alloc_fix_len(args);
817
818 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200819 args->alignment,
820 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000821 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000822
823 /*
824 * Choose closer size and invalidate other cursor.
825 */
826 if (sdiff < gdiff)
827 goto out_use_search;
828 goto out_use_good;
829 }
830
831 if (!dir)
832 error = xfs_btree_increment(*scur, 0, &i);
833 else
834 error = xfs_btree_decrement(*scur, 0, &i);
835 if (error)
836 goto error0;
837 } while (i);
838
839out_use_good:
840 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
841 *scur = NULL;
842 return 0;
843
844out_use_search:
845 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
846 *gcur = NULL;
847 return 0;
848
849error0:
850 /* caller invalidates cursors */
851 return error;
852}
853
854/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * Allocate a variable extent near bno in the allocation group agno.
856 * Extent's length (returned in len) will be between minlen and maxlen,
857 * and of the form k * prod + mod unless there's nothing that large.
858 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
859 */
860STATIC int /* error */
861xfs_alloc_ag_vextent_near(
862 xfs_alloc_arg_t *args) /* allocation argument structure */
863{
864 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
865 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
866 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 xfs_agblock_t gtbno; /* start bno of right side entry */
868 xfs_agblock_t gtbnoa; /* aligned ... */
869 xfs_extlen_t gtdiff; /* difference to right side entry */
870 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000871 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 xfs_agblock_t gtnew; /* useful start bno of right side */
873 int error; /* error code */
874 int i; /* result code, temporary */
875 int j; /* result code, temporary */
876 xfs_agblock_t ltbno; /* start bno of left side entry */
877 xfs_agblock_t ltbnoa; /* aligned ... */
878 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000880 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xfs_agblock_t ltnew; /* useful start bno of left side */
882 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000883 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000884#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * Randomly don't execute the first algorithm.
887 */
888 int dofirst; /* set to do first algorithm */
889
Akinobu Mitaecb34032013-03-04 21:58:20 +0900890 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000892
893restart:
894 bno_cur_lt = NULL;
895 bno_cur_gt = NULL;
896 ltlen = 0;
897 gtlena = 0;
898 ltlena = 0;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Get a cursor for the by-size btree.
902 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100903 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
904 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * See if there are any free extents as big as maxlen.
908 */
909 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
910 goto error0;
911 /*
912 * If none, then pick up the last entry in the tree unless the
913 * tree is empty.
914 */
915 if (!i) {
916 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
917 &ltlen, &i)))
918 goto error0;
919 if (i == 0 || ltlen == 0) {
920 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000921 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
923 }
924 ASSERT(i == 1);
925 }
926 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * First algorithm.
930 * If the requested extent is large wrt the freespaces available
931 * in this a.g., then the cursor will be pointing to a btree entry
932 * near the right edge of the tree. If it's in the last btree leaf
933 * block, then we just examine all the entries in that block
934 * that are big enough, and pick the best one.
935 * This is written as a while loop so we can break out of it,
936 * but we never loop back to the top.
937 */
938 while (xfs_btree_islastblock(cnt_cur, 0)) {
939 xfs_extlen_t bdiff;
940 int besti=0;
941 xfs_extlen_t blen=0;
942 xfs_agblock_t bnew=0;
943
Dave Chinner63d20d62013-08-12 20:49:50 +1000944#ifdef DEBUG
945 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947#endif
948 /*
949 * Start from the entry that lookup found, sequence through
950 * all larger free blocks. If we're actually pointing at a
951 * record smaller than maxlen, go to the start of this block,
952 * and skip all those smaller than minlen.
953 */
954 if (ltlen || args->alignment > 1) {
955 cnt_cur->bc_ptrs[0] = 1;
956 do {
957 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
958 &ltlen, &i)))
959 goto error0;
960 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
961 if (ltlen >= args->minlen)
962 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100963 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto error0;
965 } while (i);
966 ASSERT(ltlen >= args->minlen);
967 if (!i)
968 break;
969 }
970 i = cnt_cur->bc_ptrs[0];
971 for (j = 1, blen = 0, bdiff = 0;
972 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100973 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * For each entry, decide if it's better than
976 * the previous best entry.
977 */
978 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
979 goto error0;
980 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000981 xfs_alloc_compute_aligned(args, ltbno, ltlen,
982 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +1000983 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 continue;
985 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
986 xfs_alloc_fix_len(args);
987 ASSERT(args->len >= args->minlen);
988 if (args->len < blen)
989 continue;
990 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200991 args->alignment, args->userdata, ltbnoa,
992 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (ltnew != NULLAGBLOCK &&
994 (args->len > blen || ltdiff < bdiff)) {
995 bdiff = ltdiff;
996 bnew = ltnew;
997 blen = args->len;
998 besti = cnt_cur->bc_ptrs[0];
999 }
1000 }
1001 /*
1002 * It didn't work. We COULD be in a case where
1003 * there's a good record somewhere, so try again.
1004 */
1005 if (blen == 0)
1006 break;
1007 /*
1008 * Point at the best entry, and retrieve it again.
1009 */
1010 cnt_cur->bc_ptrs[0] = besti;
1011 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1012 goto error0;
1013 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001014 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 args->len = blen;
1016 if (!xfs_alloc_fix_minleft(args)) {
1017 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001018 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020 }
1021 blen = args->len;
1022 /*
1023 * We are allocating starting at bnew for blen blocks.
1024 */
1025 args->agbno = bnew;
1026 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001027 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Set up a cursor for the by-bno tree.
1030 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001031 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1032 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * Fix up the btree entries.
1035 */
1036 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1037 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1038 goto error0;
1039 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1040 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001041
1042 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 0;
1044 }
1045 /*
1046 * Second algorithm.
1047 * Search in the by-bno tree to the left and to the right
1048 * simultaneously, until in each case we find a space big enough,
1049 * or run into the edge of the tree. When we run into the edge,
1050 * we deallocate that cursor.
1051 * If both searches succeed, we compare the two spaces and pick
1052 * the better one.
1053 * With alignment, it's possible for both to fail; the upper
1054 * level algorithm that picks allocation groups for allocations
1055 * is not supposed to do this.
1056 */
1057 /*
1058 * Allocate and initialize the cursor for the leftward search.
1059 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001060 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1061 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * Lookup <= bno to find the leftward search's starting point.
1064 */
1065 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1066 goto error0;
1067 if (!i) {
1068 /*
1069 * Didn't find anything; use this cursor for the rightward
1070 * search.
1071 */
1072 bno_cur_gt = bno_cur_lt;
1073 bno_cur_lt = NULL;
1074 }
1075 /*
1076 * Found something. Duplicate the cursor for the rightward search.
1077 */
1078 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1079 goto error0;
1080 /*
1081 * Increment the cursor, so we will point at the entry just right
1082 * of the leftward entry if any, or to the leftmost entry.
1083 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001084 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto error0;
1086 if (!i) {
1087 /*
1088 * It failed, there are no rightward entries.
1089 */
1090 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1091 bno_cur_gt = NULL;
1092 }
1093 /*
1094 * Loop going left with the leftward cursor, right with the
1095 * rightward cursor, until either both directions give up or
1096 * we find an entry at least as big as minlen.
1097 */
1098 do {
1099 if (bno_cur_lt) {
1100 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1101 goto error0;
1102 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001103 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1104 &ltbnoa, &ltlena);
David Chinner12375c82008-04-10 12:21:32 +10001105 if (ltlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001107 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto error0;
1109 if (!i) {
1110 xfs_btree_del_cursor(bno_cur_lt,
1111 XFS_BTREE_NOERROR);
1112 bno_cur_lt = NULL;
1113 }
1114 }
1115 if (bno_cur_gt) {
1116 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1117 goto error0;
1118 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001119 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1120 &gtbnoa, &gtlena);
David Chinner12375c82008-04-10 12:21:32 +10001121 if (gtlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001123 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto error0;
1125 if (!i) {
1126 xfs_btree_del_cursor(bno_cur_gt,
1127 XFS_BTREE_NOERROR);
1128 bno_cur_gt = NULL;
1129 }
1130 }
1131 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * Got both cursors still active, need to find better entry.
1135 */
1136 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (ltlena >= args->minlen) {
1138 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001139 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
1141 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1142 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001143 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001144 args->alignment, args->userdata, ltbnoa,
1145 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001146
1147 error = xfs_alloc_find_best_extent(args,
1148 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001149 ltdiff, &gtbno, &gtlen,
1150 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001151 0 /* search right */);
1152 } else {
1153 ASSERT(gtlena >= args->minlen);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001156 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
1158 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1159 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001160 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001161 args->alignment, args->userdata, gtbnoa,
1162 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001163
1164 error = xfs_alloc_find_best_extent(args,
1165 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001166 gtdiff, &ltbno, &ltlen,
1167 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001168 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001170
1171 if (error)
1172 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * If we couldn't get anything, give up.
1177 */
1178 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001179 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1180
Christoph Hellwige26f0502011-04-24 19:06:15 +00001181 if (!forced++) {
1182 trace_xfs_alloc_near_busy(args);
1183 xfs_log_force(args->mp, XFS_LOG_SYNC);
1184 goto restart;
1185 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001186 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 args->agbno = NULLAGBLOCK;
1188 return 0;
1189 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * At this point we have selected a freespace entry, either to the
1193 * left or to the right. If it's on the right, copy all the
1194 * useful variables to the "left" set so we only have one
1195 * copy of this code.
1196 */
1197 if (bno_cur_gt) {
1198 bno_cur_lt = bno_cur_gt;
1199 bno_cur_gt = NULL;
1200 ltbno = gtbno;
1201 ltbnoa = gtbnoa;
1202 ltlen = gtlen;
1203 ltlena = gtlena;
1204 j = 1;
1205 } else
1206 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Fix up the length and compute the useful address.
1210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1212 xfs_alloc_fix_len(args);
1213 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001214 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1216 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1217 return 0;
1218 }
1219 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001220 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001221 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001223 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001224 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1228 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1229 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001230
1231 if (j)
1232 trace_xfs_alloc_near_greater(args);
1233 else
1234 trace_xfs_alloc_near_lesser(args);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1237 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1238 return 0;
1239
1240 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001241 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (cnt_cur != NULL)
1243 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1244 if (bno_cur_lt != NULL)
1245 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1246 if (bno_cur_gt != NULL)
1247 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1248 return error;
1249}
1250
1251/*
1252 * Allocate a variable extent anywhere in the allocation group agno.
1253 * Extent's length (returned in len) will be between minlen and maxlen,
1254 * and of the form k * prod + mod unless there's nothing that large.
1255 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1256 */
1257STATIC int /* error */
1258xfs_alloc_ag_vextent_size(
1259 xfs_alloc_arg_t *args) /* allocation argument structure */
1260{
1261 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1262 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1263 int error; /* error result */
1264 xfs_agblock_t fbno; /* start of found freespace */
1265 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int i; /* temp status variable */
1267 xfs_agblock_t rbno; /* returned block number */
1268 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001269 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Christoph Hellwige26f0502011-04-24 19:06:15 +00001271restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * Allocate and initialize a cursor for the by-size btree.
1274 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001275 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1276 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Look for an entry >= maxlen+alignment-1 blocks.
1281 */
1282 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1283 args->maxlen + args->alignment - 1, &i)))
1284 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001287 * If none or we have busy extents that we cannot allocate from, then
1288 * we have to settle for a smaller extent. In the case that there are
1289 * no large extents, this will return the last entry in the tree unless
1290 * the tree is empty. In the case that there are only busy large
1291 * extents, this will return the largest small extent unless there
1292 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001294 if (!i || forced > 1) {
1295 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1296 &fbno, &flen, &i);
1297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto error0;
1299 if (i == 0 || flen == 0) {
1300 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001301 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303 }
1304 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001305 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1306 } else {
1307 /*
1308 * Search for a non-busy extent that is large enough.
1309 * If we are at low space, don't check, or if we fall of
1310 * the end of the btree, turn off the busy check and
1311 * restart.
1312 */
1313 for (;;) {
1314 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1315 if (error)
1316 goto error0;
1317 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1318
1319 xfs_alloc_compute_aligned(args, fbno, flen,
1320 &rbno, &rlen);
1321
1322 if (rlen >= args->maxlen)
1323 break;
1324
1325 error = xfs_btree_increment(cnt_cur, 0, &i);
1326 if (error)
1327 goto error0;
1328 if (i == 0) {
1329 /*
1330 * Our only valid extents must have been busy.
1331 * Make it unbusy by forcing the log out and
1332 * retrying. If we've been here before, forcing
1333 * the log isn't making the extents available,
1334 * which means they have probably been freed in
1335 * this transaction. In that case, we have to
1336 * give up on them and we'll attempt a minlen
1337 * allocation the next time around.
1338 */
1339 xfs_btree_del_cursor(cnt_cur,
1340 XFS_BTREE_NOERROR);
1341 trace_xfs_alloc_size_busy(args);
1342 if (!forced++)
1343 xfs_log_force(args->mp, XFS_LOG_SYNC);
1344 goto restart;
1345 }
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /*
1350 * In the first case above, we got the last entry in the
1351 * by-size btree. Now we check to see if the space hits maxlen
1352 * once aligned; if not, we search left for something better.
1353 * This can't happen in the second case above.
1354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1356 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1357 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1358 if (rlen < args->maxlen) {
1359 xfs_agblock_t bestfbno;
1360 xfs_extlen_t bestflen;
1361 xfs_agblock_t bestrbno;
1362 xfs_extlen_t bestrlen;
1363
1364 bestrlen = rlen;
1365 bestrbno = rbno;
1366 bestflen = flen;
1367 bestfbno = fbno;
1368 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001369 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto error0;
1371 if (i == 0)
1372 break;
1373 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1374 &i)))
1375 goto error0;
1376 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1377 if (flen < bestrlen)
1378 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001379 xfs_alloc_compute_aligned(args, fbno, flen,
1380 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1382 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1383 (rlen <= flen && rbno + rlen <= fbno + flen),
1384 error0);
1385 if (rlen > bestrlen) {
1386 bestrlen = rlen;
1387 bestrbno = rbno;
1388 bestflen = flen;
1389 bestfbno = fbno;
1390 if (rlen == args->maxlen)
1391 break;
1392 }
1393 }
1394 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1395 &i)))
1396 goto error0;
1397 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1398 rlen = bestrlen;
1399 rbno = bestrbno;
1400 flen = bestflen;
1401 fbno = bestfbno;
1402 }
1403 args->wasfromfl = 0;
1404 /*
1405 * Fix up the length.
1406 */
1407 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001408 if (rlen < args->minlen) {
1409 if (!forced++) {
1410 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1411 trace_xfs_alloc_size_busy(args);
1412 xfs_log_force(args->mp, XFS_LOG_SYNC);
1413 goto restart;
1414 }
1415 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001417 xfs_alloc_fix_len(args);
1418
1419 if (!xfs_alloc_fix_minleft(args))
1420 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 rlen = args->len;
1422 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1423 /*
1424 * Allocate and initialize a cursor for the by-block tree.
1425 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001426 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1427 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1429 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1430 goto error0;
1431 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1432 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1433 cnt_cur = bno_cur = NULL;
1434 args->len = rlen;
1435 args->agbno = rbno;
1436 XFS_WANT_CORRUPTED_GOTO(
1437 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001438 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001440 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442
1443error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001444 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (cnt_cur)
1446 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1447 if (bno_cur)
1448 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1449 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001450
1451out_nominleft:
1452 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1453 trace_xfs_alloc_size_nominleft(args);
1454 args->agbno = NULLAGBLOCK;
1455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458/*
1459 * Deal with the case where only small freespaces remain.
1460 * Either return the contents of the last freespace record,
1461 * or allocate space from the freelist if there is nothing in the tree.
1462 */
1463STATIC int /* error */
1464xfs_alloc_ag_vextent_small(
1465 xfs_alloc_arg_t *args, /* allocation argument structure */
1466 xfs_btree_cur_t *ccur, /* by-size cursor */
1467 xfs_agblock_t *fbnop, /* result block number */
1468 xfs_extlen_t *flenp, /* result length */
1469 int *stat) /* status: 0-freelist, 1-normal/none */
1470{
1471 int error;
1472 xfs_agblock_t fbno;
1473 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int i;
1475
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001476 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto error0;
1478 if (i) {
1479 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1480 goto error0;
1481 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1482 }
1483 /*
1484 * Nothing in the btree, try the freelist. Make sure
1485 * to respect minleft even when pulling from the
1486 * freelist.
1487 */
1488 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001489 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1490 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001491 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1492 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto error0;
1494 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001495 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001496 args->userdata);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (args->userdata) {
1499 xfs_buf_t *bp;
1500
1501 bp = xfs_btree_get_bufs(args->mp, args->tp,
1502 args->agno, fbno, 0);
1503 xfs_trans_binval(args->tp, bp);
1504 }
1505 args->len = 1;
1506 args->agbno = fbno;
1507 XFS_WANT_CORRUPTED_GOTO(
1508 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001509 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 error0);
1511 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001512 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 *stat = 0;
1514 return 0;
1515 }
1516 /*
1517 * Nothing in the freelist.
1518 */
1519 else
1520 flen = 0;
1521 }
1522 /*
1523 * Can't allocate from the freelist for some reason.
1524 */
Nathan Scottd432c802006-09-28 11:03:44 +10001525 else {
1526 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /*
1530 * Can't do the allocation, give up.
1531 */
1532 if (flen < args->minlen) {
1533 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001534 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 flen = 0;
1536 }
1537 *fbnop = fbno;
1538 *flenp = flen;
1539 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001540 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542
1543error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001544 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return error;
1546}
1547
1548/*
1549 * Free the extent starting at agno/bno for length.
1550 */
1551STATIC int /* error */
1552xfs_free_ag_extent(
1553 xfs_trans_t *tp, /* transaction pointer */
1554 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1555 xfs_agnumber_t agno, /* allocation group number */
1556 xfs_agblock_t bno, /* starting block number */
1557 xfs_extlen_t len, /* length of extent */
1558 int isfl) /* set if is freelist blocks - no sb acctg */
1559{
1560 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1561 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1562 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 xfs_agblock_t gtbno; /* start of right neighbor block */
1564 xfs_extlen_t gtlen; /* length of right neighbor block */
1565 int haveleft; /* have a left neighbor block */
1566 int haveright; /* have a right neighbor block */
1567 int i; /* temp, result code */
1568 xfs_agblock_t ltbno; /* start of left neighbor block */
1569 xfs_extlen_t ltlen; /* length of left neighbor block */
1570 xfs_mount_t *mp; /* mount point struct for filesystem */
1571 xfs_agblock_t nbno; /* new starting block of freespace */
1572 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001573 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 mp = tp->t_mountp;
1576 /*
1577 * Allocate and initialize a cursor for the by-block btree.
1578 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001579 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cnt_cur = NULL;
1581 /*
1582 * Look for a neighboring block on the left (lower block numbers)
1583 * that is contiguous with this space.
1584 */
1585 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1586 goto error0;
1587 if (haveleft) {
1588 /*
1589 * There is a block to our left.
1590 */
1591 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1592 goto error0;
1593 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1594 /*
1595 * It's not contiguous, though.
1596 */
1597 if (ltbno + ltlen < bno)
1598 haveleft = 0;
1599 else {
1600 /*
1601 * If this failure happens the request to free this
1602 * space was invalid, it's (partly) already free.
1603 * Very bad.
1604 */
1605 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1606 }
1607 }
1608 /*
1609 * Look for a neighboring block on the right (higher block numbers)
1610 * that is contiguous with this space.
1611 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001612 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto error0;
1614 if (haveright) {
1615 /*
1616 * There is a block to our right.
1617 */
1618 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1619 goto error0;
1620 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1621 /*
1622 * It's not contiguous, though.
1623 */
1624 if (bno + len < gtbno)
1625 haveright = 0;
1626 else {
1627 /*
1628 * If this failure happens the request to free this
1629 * space was invalid, it's (partly) already free.
1630 * Very bad.
1631 */
1632 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1633 }
1634 }
1635 /*
1636 * Now allocate and initialize a cursor for the by-size tree.
1637 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001638 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Have both left and right contiguous neighbors.
1641 * Merge all three into a single free block.
1642 */
1643 if (haveleft && haveright) {
1644 /*
1645 * Delete the old by-size entry on the left.
1646 */
1647 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1648 goto error0;
1649 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001650 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto error0;
1652 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1653 /*
1654 * Delete the old by-size entry on the right.
1655 */
1656 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1657 goto error0;
1658 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001659 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto error0;
1661 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1662 /*
1663 * Delete the old by-block entry for the right block.
1664 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001665 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto error0;
1667 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1668 /*
1669 * Move the by-block cursor back to the left neighbor.
1670 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001671 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto error0;
1673 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1674#ifdef DEBUG
1675 /*
1676 * Check that this is the right record: delete didn't
1677 * mangle the cursor.
1678 */
1679 {
1680 xfs_agblock_t xxbno;
1681 xfs_extlen_t xxlen;
1682
1683 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1684 &i)))
1685 goto error0;
1686 XFS_WANT_CORRUPTED_GOTO(
1687 i == 1 && xxbno == ltbno && xxlen == ltlen,
1688 error0);
1689 }
1690#endif
1691 /*
1692 * Update remaining by-block entry to the new, joined block.
1693 */
1694 nbno = ltbno;
1695 nlen = len + ltlen + gtlen;
1696 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1697 goto error0;
1698 }
1699 /*
1700 * Have only a left contiguous neighbor.
1701 * Merge it together with the new freespace.
1702 */
1703 else if (haveleft) {
1704 /*
1705 * Delete the old by-size entry on the left.
1706 */
1707 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1708 goto error0;
1709 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001710 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto error0;
1712 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1713 /*
1714 * Back up the by-block cursor to the left neighbor, and
1715 * update its length.
1716 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001717 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto error0;
1719 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1720 nbno = ltbno;
1721 nlen = len + ltlen;
1722 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1723 goto error0;
1724 }
1725 /*
1726 * Have only a right contiguous neighbor.
1727 * Merge it together with the new freespace.
1728 */
1729 else if (haveright) {
1730 /*
1731 * Delete the old by-size entry on the right.
1732 */
1733 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1734 goto error0;
1735 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001736 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto error0;
1738 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1739 /*
1740 * Update the starting block and length of the right
1741 * neighbor in the by-block tree.
1742 */
1743 nbno = bno;
1744 nlen = len + gtlen;
1745 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1746 goto error0;
1747 }
1748 /*
1749 * No contiguous neighbors.
1750 * Insert the new freespace into the by-block tree.
1751 */
1752 else {
1753 nbno = bno;
1754 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001755 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto error0;
1757 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1758 }
1759 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1760 bno_cur = NULL;
1761 /*
1762 * In all cases we need to insert the new freespace in the by-size tree.
1763 */
1764 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1765 goto error0;
1766 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001767 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto error0;
1769 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1770 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1771 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
1774 * Update the freespace totals in the ag and superblock.
1775 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001776 pag = xfs_perag_get(mp, agno);
1777 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1778 xfs_perag_put(pag);
1779 if (error)
1780 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Christoph Hellwigecb69282011-03-04 12:59:55 +00001782 if (!isfl)
1783 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1784 XFS_STATS_INC(xs_freex);
1785 XFS_STATS_ADD(xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001786
1787 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
1790
1791 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001792 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (bno_cur)
1794 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1795 if (cnt_cur)
1796 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1797 return error;
1798}
1799
1800/*
1801 * Visible (exported) allocation/free functions.
1802 * Some of these are used just by xfs_alloc_btree.c and this file.
1803 */
1804
1805/*
1806 * Compute and fill in value of m_ag_maxlevels.
1807 */
1808void
1809xfs_alloc_compute_maxlevels(
1810 xfs_mount_t *mp) /* file system mount structure */
1811{
1812 int level;
1813 uint maxblocks;
1814 uint maxleafents;
1815 int minleafrecs;
1816 int minnoderecs;
1817
1818 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1819 minleafrecs = mp->m_alloc_mnr[0];
1820 minnoderecs = mp->m_alloc_mnr[1];
1821 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1822 for (level = 1; maxblocks > 1; level++)
1823 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1824 mp->m_ag_maxlevels = level;
1825}
1826
1827/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001828 * Find the length of the longest extent in an AG.
1829 */
1830xfs_extlen_t
1831xfs_alloc_longest_free_extent(
1832 struct xfs_mount *mp,
1833 struct xfs_perag *pag)
1834{
1835 xfs_extlen_t need, delta = 0;
1836
1837 need = XFS_MIN_FREELIST_PAG(pag, mp);
1838 if (need > pag->pagf_flcount)
1839 delta = need - pag->pagf_flcount;
1840
1841 if (pag->pagf_longest > delta)
1842 return pag->pagf_longest - delta;
1843 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1844}
1845
1846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * Decide whether to use this allocation group for this allocation.
1848 * If so, fix up the btree freelist's size.
1849 */
1850STATIC int /* error */
1851xfs_alloc_fix_freelist(
1852 xfs_alloc_arg_t *args, /* allocation argument structure */
1853 int flags) /* XFS_ALLOC_FLAG_... */
1854{
1855 xfs_buf_t *agbp; /* agf buffer pointer */
1856 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1857 xfs_buf_t *agflbp;/* agfl buffer pointer */
1858 xfs_agblock_t bno; /* freelist block */
1859 xfs_extlen_t delta; /* new blocks needed in freelist */
1860 int error; /* error result code */
1861 xfs_extlen_t longest;/* longest extent in allocation group */
1862 xfs_mount_t *mp; /* file system mount point structure */
1863 xfs_extlen_t need; /* total blocks needed in freelist */
1864 xfs_perag_t *pag; /* per-ag information structure */
1865 xfs_alloc_arg_t targs; /* local allocation arguments */
1866 xfs_trans_t *tp; /* transaction pointer */
1867
1868 mp = args->mp;
1869
1870 pag = args->pag;
1871 tp = args->tp;
1872 if (!pag->pagf_init) {
1873 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1874 &agbp)))
1875 return error;
1876 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001877 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1878 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 args->agbp = NULL;
1880 return 0;
1881 }
1882 } else
1883 agbp = NULL;
1884
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001885 /*
1886 * If this is a metadata preferred pag and we are user data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * then try somewhere else if we are not being asked to
1888 * try harder at this point
1889 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001890 if (pag->pagf_metadata && args->userdata &&
1891 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1892 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 args->agbp = NULL;
1894 return 0;
1895 }
1896
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001897 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001898 /*
1899 * If it looks like there isn't a long enough extent, or enough
1900 * total blocks, reject it.
1901 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001902 need = XFS_MIN_FREELIST_PAG(pag, mp);
1903 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001904 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1905 longest ||
1906 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1907 need - args->total) < (int)args->minleft)) {
1908 if (agbp)
1909 xfs_trans_brelse(tp, agbp);
1910 args->agbp = NULL;
1911 return 0;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /*
1916 * Get the a.g. freespace buffer.
1917 * Can fail if we're not blocking on locks, and it's held.
1918 */
1919 if (agbp == NULL) {
1920 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1921 &agbp)))
1922 return error;
1923 if (agbp == NULL) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001924 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1925 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 args->agbp = NULL;
1927 return 0;
1928 }
1929 }
1930 /*
1931 * Figure out how many blocks we should have in the freelist.
1932 */
1933 agf = XFS_BUF_TO_AGF(agbp);
1934 need = XFS_MIN_FREELIST(agf, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /*
1936 * If there isn't enough total or single-extent, reject it.
1937 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001938 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1939 delta = need > be32_to_cpu(agf->agf_flcount) ?
1940 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1941 longest = be32_to_cpu(agf->agf_longest);
1942 longest = (longest > delta) ? (longest - delta) :
1943 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1944 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1945 longest ||
1946 ((int)(be32_to_cpu(agf->agf_freeblks) +
1947 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1948 (int)args->minleft)) {
1949 xfs_trans_brelse(tp, agbp);
1950 args->agbp = NULL;
1951 return 0;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 /*
1955 * Make the freelist shorter if it's too long.
1956 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001957 while (be32_to_cpu(agf->agf_flcount) > need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 xfs_buf_t *bp;
1959
David Chinner92821e22007-05-24 15:26:31 +10001960 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return error;
1963 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1964 return error;
1965 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1966 xfs_trans_binval(tp, bp);
1967 }
1968 /*
1969 * Initialize the args structure.
1970 */
Mark Tinguelya0041682012-09-20 13:16:45 -05001971 memset(&targs, 0, sizeof(targs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 targs.tp = tp;
1973 targs.mp = mp;
1974 targs.agbp = agbp;
1975 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1977 targs.type = XFS_ALLOCTYPE_THIS_AG;
1978 targs.pag = pag;
1979 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1980 return error;
1981 /*
1982 * Make the freelist longer if it's too short.
1983 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001984 while (be32_to_cpu(agf->agf_flcount) < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 targs.agbno = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001986 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /*
1988 * Allocate as many blocks as possible at once.
1989 */
Nathan Scotte63a3692006-05-08 19:51:58 +10001990 if ((error = xfs_alloc_ag_vextent(&targs))) {
1991 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return error;
Nathan Scotte63a3692006-05-08 19:51:58 +10001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
1995 * Stop if we run out. Won't happen if callers are obeying
1996 * the restrictions correctly. Can happen for free calls
1997 * on a completely full ag.
1998 */
Yingping Lud210a282006-06-09 14:55:18 +10001999 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002000 if (flags & XFS_ALLOC_FLAG_FREEING)
2001 break;
2002 xfs_trans_brelse(tp, agflbp);
2003 args->agbp = NULL;
2004 return 0;
Yingping Lud210a282006-06-09 14:55:18 +10002005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * Put each allocated block on the list.
2008 */
2009 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002010 error = xfs_alloc_put_freelist(tp, agbp,
2011 agflbp, bno, 0);
2012 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return error;
2014 }
2015 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002016 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 args->agbp = agbp;
2018 return 0;
2019}
2020
2021/*
2022 * Get a block from the freelist.
2023 * Returns with the buffer for the block gotten.
2024 */
2025int /* error */
2026xfs_alloc_get_freelist(
2027 xfs_trans_t *tp, /* transaction pointer */
2028 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002029 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2030 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2034 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002035 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int error;
David Chinner92821e22007-05-24 15:26:31 +10002037 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002038 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 xfs_perag_t *pag; /* per allocation group data */
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Freelist is empty, give up.
2043 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002044 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (!agf->agf_flcount) {
2046 *bnop = NULLAGBLOCK;
2047 return 0;
2048 }
2049 /*
2050 * Read the array of free blocks.
2051 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002052 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2053 &agflbp);
2054 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002056
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /*
2059 * Get the block number and update the data structures.
2060 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002061 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2062 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002063 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002065 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002067
2068 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002069 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 xfs_trans_agflist_delta(tp, -1);
2071 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002072 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002073
2074 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2075 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002076 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002077 pag->pagf_btreeblks++;
2078 logflags |= XFS_AGF_BTREEBLKS;
2079 }
2080
David Chinner92821e22007-05-24 15:26:31 +10002081 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 *bnop = bno;
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
2087/*
2088 * Log the given fields from the agf structure.
2089 */
2090void
2091xfs_alloc_log_agf(
2092 xfs_trans_t *tp, /* transaction pointer */
2093 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2094 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2095{
2096 int first; /* first byte offset */
2097 int last; /* last byte offset */
2098 static const short offsets[] = {
2099 offsetof(xfs_agf_t, agf_magicnum),
2100 offsetof(xfs_agf_t, agf_versionnum),
2101 offsetof(xfs_agf_t, agf_seqno),
2102 offsetof(xfs_agf_t, agf_length),
2103 offsetof(xfs_agf_t, agf_roots[0]),
2104 offsetof(xfs_agf_t, agf_levels[0]),
2105 offsetof(xfs_agf_t, agf_flfirst),
2106 offsetof(xfs_agf_t, agf_fllast),
2107 offsetof(xfs_agf_t, agf_flcount),
2108 offsetof(xfs_agf_t, agf_freeblks),
2109 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002110 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002111 offsetof(xfs_agf_t, agf_uuid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 sizeof(xfs_agf_t)
2113 };
2114
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002115 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2116
Dave Chinner61fe1352013-04-03 16:11:30 +11002117 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2120 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2121}
2122
2123/*
2124 * Interface for inode allocation to force the pag data to be initialized.
2125 */
2126int /* error */
2127xfs_alloc_pagf_init(
2128 xfs_mount_t *mp, /* file system mount structure */
2129 xfs_trans_t *tp, /* transaction pointer */
2130 xfs_agnumber_t agno, /* allocation group number */
2131 int flags) /* XFS_ALLOC_FLAGS_... */
2132{
2133 xfs_buf_t *bp;
2134 int error;
2135
2136 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2137 return error;
2138 if (bp)
2139 xfs_trans_brelse(tp, bp);
2140 return 0;
2141}
2142
2143/*
2144 * Put the block on the freelist for the allocation group.
2145 */
2146int /* error */
2147xfs_alloc_put_freelist(
2148 xfs_trans_t *tp, /* transaction pointer */
2149 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2150 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002151 xfs_agblock_t bno, /* block being freed */
2152 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002155 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int error;
David Chinner92821e22007-05-24 15:26:31 +10002157 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 xfs_mount_t *mp; /* mount structure */
2159 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002160 __be32 *agfl_bno;
2161 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 agf = XFS_BUF_TO_AGF(agbp);
2164 mp = tp->t_mountp;
2165
2166 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002167 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002169 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002170 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002172
2173 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002174 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_trans_agflist_delta(tp, 1);
2176 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002177
2178 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2179 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002180 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002181 pag->pagf_btreeblks--;
2182 logflags |= XFS_AGF_BTREEBLKS;
2183 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002184 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002185
David Chinner92821e22007-05-24 15:26:31 +10002186 xfs_alloc_log_agf(tp, agbp, logflags);
2187
Christoph Hellwig16259e72005-11-02 15:11:25 +11002188 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002189
2190 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2191 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002192 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002193 startoff = (char *)blockp - (char *)agflbp->b_addr;
2194
David Chinner92821e22007-05-24 15:26:31 +10002195 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002196
Dave Chinner61fe1352013-04-03 16:11:30 +11002197 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002198 xfs_trans_log_buf(tp, agflbp, startoff,
2199 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201}
2202
Dave Chinner4e0e6042013-04-03 16:11:13 +11002203static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002204xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002205 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002206 struct xfs_buf *bp)
2207 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002208 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002209
Dave Chinner4e0e6042013-04-03 16:11:13 +11002210 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2211 !uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_uuid))
2212 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002213
Dave Chinner4e0e6042013-04-03 16:11:13 +11002214 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2215 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2216 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2217 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2218 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2219 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2220 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002221
2222 /*
2223 * during growfs operations, the perag is not fully initialised,
2224 * so we can't use it for any useful checking. growfs ensures we can't
2225 * use it by using uncached buffers that don't have the perag attached
2226 * so we can detect and avoid this problem.
2227 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002228 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2229 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002230
Dave Chinner4e0e6042013-04-03 16:11:13 +11002231 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2232 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2233 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002234
Dave Chinner4e0e6042013-04-03 16:11:13 +11002235 return true;;
2236
Dave Chinner612cfbf2012-11-14 17:52:32 +11002237}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002238
Dave Chinner1813dd62012-11-14 17:54:40 +11002239static void
2240xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002241 struct xfs_buf *bp)
2242{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002243 struct xfs_mount *mp = bp->b_target->bt_mount;
2244 int agf_ok = 1;
2245
2246 if (xfs_sb_version_hascrc(&mp->m_sb))
2247 agf_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
2248 offsetof(struct xfs_agf, agf_crc));
2249
2250 agf_ok = agf_ok && xfs_agf_verify(mp, bp);
2251
2252 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2253 XFS_RANDOM_ALLOC_READ_AGF))) {
2254 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2255 xfs_buf_ioerror(bp, EFSCORRUPTED);
2256 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11002257}
2258
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002259static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002260xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002261 struct xfs_buf *bp)
2262{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002263 struct xfs_mount *mp = bp->b_target->bt_mount;
2264 struct xfs_buf_log_item *bip = bp->b_fspriv;
2265
2266 if (!xfs_agf_verify(mp, bp)) {
2267 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2268 xfs_buf_ioerror(bp, EFSCORRUPTED);
2269 return;
2270 }
2271
2272 if (!xfs_sb_version_hascrc(&mp->m_sb))
2273 return;
2274
2275 if (bip)
2276 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2277
2278 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
2279 offsetof(struct xfs_agf, agf_crc));
Dave Chinner5d5f5272012-11-14 17:44:56 +11002280}
2281
Dave Chinner1813dd62012-11-14 17:54:40 +11002282const struct xfs_buf_ops xfs_agf_buf_ops = {
2283 .verify_read = xfs_agf_read_verify,
2284 .verify_write = xfs_agf_write_verify,
2285};
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/*
2288 * Read in the allocation group header (free/alloc section).
2289 */
2290int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002291xfs_read_agf(
2292 struct xfs_mount *mp, /* mount point structure */
2293 struct xfs_trans *tp, /* transaction pointer */
2294 xfs_agnumber_t agno, /* allocation group number */
2295 int flags, /* XFS_BUF_ */
2296 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int error;
2299
2300 ASSERT(agno != NULLAGNUMBER);
2301 error = xfs_trans_read_buf(
2302 mp, tp, mp->m_ddev_targp,
2303 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002304 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (error)
2306 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002307 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002309
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002310 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002311 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002312 return 0;
2313}
2314
2315/*
2316 * Read in the allocation group header (free/alloc section).
2317 */
2318int /* error */
2319xfs_alloc_read_agf(
2320 struct xfs_mount *mp, /* mount point structure */
2321 struct xfs_trans *tp, /* transaction pointer */
2322 xfs_agnumber_t agno, /* allocation group number */
2323 int flags, /* XFS_ALLOC_FLAG_... */
2324 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2325{
2326 struct xfs_agf *agf; /* ag freelist header */
2327 struct xfs_perag *pag; /* per allocation group data */
2328 int error;
2329
2330 ASSERT(agno != NULLAGNUMBER);
2331
2332 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002333 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002334 bpp);
2335 if (error)
2336 return error;
2337 if (!*bpp)
2338 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002339 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002340
2341 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002342 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002344 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002345 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002346 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2347 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002349 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002351 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002352 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002353 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002354 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 pag->pagf_init = 1;
2356 }
2357#ifdef DEBUG
2358 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002359 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002360 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002361 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2362 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002364 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002366 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002369 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
2371}
2372
2373/*
2374 * Allocate an extent (variable-size).
2375 * Depending on the allocation type, we either look in a single allocation
2376 * group or loop over the allocation groups to find the result.
2377 */
2378int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002379xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 xfs_alloc_arg_t *args) /* allocation argument structure */
2381{
2382 xfs_agblock_t agsize; /* allocation group size */
2383 int error;
2384 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 xfs_extlen_t minleft;/* minimum left value, temp copy */
2386 xfs_mount_t *mp; /* mount structure pointer */
2387 xfs_agnumber_t sagno; /* starting allocation group number */
2388 xfs_alloctype_t type; /* input allocation type */
2389 int bump_rotor = 0;
2390 int no_min = 0;
2391 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2392
2393 mp = args->mp;
2394 type = args->otype = args->type;
2395 args->agbno = NULLAGBLOCK;
2396 /*
2397 * Just fix this up, for the case where the last a.g. is shorter
2398 * (or there's only one a.g.) and the caller couldn't easily figure
2399 * that out (xfs_bmap_alloc).
2400 */
2401 agsize = mp->m_sb.sb_agblocks;
2402 if (args->maxlen > agsize)
2403 args->maxlen = agsize;
2404 if (args->alignment == 0)
2405 args->alignment = 1;
2406 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2407 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2408 ASSERT(args->minlen <= args->maxlen);
2409 ASSERT(args->minlen <= agsize);
2410 ASSERT(args->mod < args->prod);
2411 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2412 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2413 args->minlen > args->maxlen || args->minlen > agsize ||
2414 args->mod >= args->prod) {
2415 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002416 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 0;
2418 }
2419 minleft = args->minleft;
2420
2421 switch (type) {
2422 case XFS_ALLOCTYPE_THIS_AG:
2423 case XFS_ALLOCTYPE_NEAR_BNO:
2424 case XFS_ALLOCTYPE_THIS_BNO:
2425 /*
2426 * These three force us into a single a.g.
2427 */
2428 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002429 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 args->minleft = 0;
2431 error = xfs_alloc_fix_freelist(args, 0);
2432 args->minleft = minleft;
2433 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002434 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 goto error0;
2436 }
2437 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002438 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
2440 }
2441 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2442 if ((error = xfs_alloc_ag_vextent(args)))
2443 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 break;
2445 case XFS_ALLOCTYPE_START_BNO:
2446 /*
2447 * Try near allocation first, then anywhere-in-ag after
2448 * the first a.g. fails.
2449 */
2450 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2451 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2452 args->fsbno = XFS_AGB_TO_FSB(mp,
2453 ((mp->m_agfrotor / rotorstep) %
2454 mp->m_sb.sb_agcount), 0);
2455 bump_rotor = 1;
2456 }
2457 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2458 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2459 /* FALLTHROUGH */
2460 case XFS_ALLOCTYPE_ANY_AG:
2461 case XFS_ALLOCTYPE_START_AG:
2462 case XFS_ALLOCTYPE_FIRST_AG:
2463 /*
2464 * Rotate through the allocation groups looking for a winner.
2465 */
2466 if (type == XFS_ALLOCTYPE_ANY_AG) {
2467 /*
2468 * Start with the last place we left off.
2469 */
2470 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2471 mp->m_sb.sb_agcount;
2472 args->type = XFS_ALLOCTYPE_THIS_AG;
2473 flags = XFS_ALLOC_FLAG_TRYLOCK;
2474 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2475 /*
2476 * Start with allocation group given by bno.
2477 */
2478 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2479 args->type = XFS_ALLOCTYPE_THIS_AG;
2480 sagno = 0;
2481 flags = 0;
2482 } else {
2483 if (type == XFS_ALLOCTYPE_START_AG)
2484 args->type = XFS_ALLOCTYPE_THIS_AG;
2485 /*
2486 * Start with the given allocation group.
2487 */
2488 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2489 flags = XFS_ALLOC_FLAG_TRYLOCK;
2490 }
2491 /*
2492 * Loop over allocation groups twice; first time with
2493 * trylock set, second time without.
2494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002496 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (no_min) args->minleft = 0;
2498 error = xfs_alloc_fix_freelist(args, flags);
2499 args->minleft = minleft;
2500 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002501 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 goto error0;
2503 }
2504 /*
2505 * If we get a buffer back then the allocation will fly.
2506 */
2507 if (args->agbp) {
2508 if ((error = xfs_alloc_ag_vextent(args)))
2509 goto error0;
2510 break;
2511 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002512
2513 trace_xfs_alloc_vextent_loopfailed(args);
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /*
2516 * Didn't work, figure out the next iteration.
2517 */
2518 if (args->agno == sagno &&
2519 type == XFS_ALLOCTYPE_START_BNO)
2520 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002521 /*
2522 * For the first allocation, we can try any AG to get
2523 * space. However, if we already have allocated a
2524 * block, we don't want to try AGs whose number is below
2525 * sagno. Otherwise, we may end up with out-of-order
2526 * locking of AGF, which might cause deadlock.
2527 */
2528 if (++(args->agno) == mp->m_sb.sb_agcount) {
2529 if (args->firstblock != NULLFSBLOCK)
2530 args->agno = sagno;
2531 else
2532 args->agno = 0;
2533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * Reached the starting a.g., must either be done
2536 * or switch to non-trylock mode.
2537 */
2538 if (args->agno == sagno) {
2539 if (no_min == 1) {
2540 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002541 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 break;
2543 }
2544 if (flags == 0) {
2545 no_min = 1;
2546 } else {
2547 flags = 0;
2548 if (type == XFS_ALLOCTYPE_START_BNO) {
2549 args->agbno = XFS_FSB_TO_AGBNO(mp,
2550 args->fsbno);
2551 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2552 }
2553 }
2554 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002555 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2558 if (args->agno == sagno)
2559 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2560 (mp->m_sb.sb_agcount * rotorstep);
2561 else
2562 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2563 (mp->m_sb.sb_agcount * rotorstep);
2564 }
2565 break;
2566 default:
2567 ASSERT(0);
2568 /* NOTREACHED */
2569 }
2570 if (args->agbno == NULLAGBLOCK)
2571 args->fsbno = NULLFSBLOCK;
2572 else {
2573 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2574#ifdef DEBUG
2575 ASSERT(args->len >= args->minlen);
2576 ASSERT(args->len <= args->maxlen);
2577 ASSERT(args->agbno % args->alignment == 0);
2578 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2579 args->len);
2580#endif
2581 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002582 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002585 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return error;
2587}
2588
2589/*
2590 * Free an extent.
2591 * Just break up the extent address and hand off to xfs_free_ag_extent
2592 * after fixing up the freelist.
2593 */
2594int /* error */
2595xfs_free_extent(
2596 xfs_trans_t *tp, /* transaction pointer */
2597 xfs_fsblock_t bno, /* starting block number of extent */
2598 xfs_extlen_t len) /* length of extent */
2599{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002600 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 int error;
2602
2603 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002604 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 args.tp = tp;
2606 args.mp = tp->t_mountp;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002607
2608 /*
2609 * validate that the block number is legal - the enables us to detect
2610 * and handle a silent filesystem corruption rather than crashing.
2611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002613 if (args.agno >= args.mp->m_sb.sb_agcount)
2614 return EFSCORRUPTED;
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002617 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2618 return EFSCORRUPTED;
2619
Dave Chinnera862e0f2010-01-11 11:47:41 +00002620 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002621 ASSERT(args.pag);
2622
2623 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2624 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto error0;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002626
2627 /* validate the extent size is legal now we have the agf locked */
2628 if (args.agbno + len >
2629 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2630 error = EFSCORRUPTED;
2631 goto error0;
2632 }
2633
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002634 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Christoph Hellwiga870acd2011-04-24 19:06:14 +00002635 if (!error)
Dave Chinner4ecbfe62012-04-29 10:41:10 +00002636 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002638 xfs_perag_put(args.pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return error;
2640}