blob: 2bfca06e0ee71c92bc71dac65a0d0b1c74f9d704 [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"
21#include "xfs_shared.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
24#include "xfs_trans.h"
25#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 Chinner4e0e6042013-04-03 16:11:13 +110039#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dave Chinnerc999a222012-03-22 05:15:07 +000041struct workqueue_struct *xfs_alloc_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
44
45#define XFSA_FIXUP_BNO_OK 1
46#define XFSA_FIXUP_CNT_OK 2
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
49STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
Christoph Hellwige26f0502011-04-24 19:06:15 +000052 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110055 * Lookup the record equal to [bno, len] in the btree given by cur.
56 */
57STATIC int /* error */
58xfs_alloc_lookup_eq(
59 struct xfs_btree_cur *cur, /* btree cursor */
60 xfs_agblock_t bno, /* starting block of extent */
61 xfs_extlen_t len, /* length of extent */
62 int *stat) /* success/failure */
63{
64 cur->bc_rec.a.ar_startblock = bno;
65 cur->bc_rec.a.ar_blockcount = len;
66 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
67}
68
69/*
70 * Lookup the first record greater than or equal to [bno, len]
71 * in the btree given by cur.
72 */
Dave Chinnera66d6362012-03-22 05:15:12 +000073int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110074xfs_alloc_lookup_ge(
75 struct xfs_btree_cur *cur, /* btree cursor */
76 xfs_agblock_t bno, /* starting block of extent */
77 xfs_extlen_t len, /* length of extent */
78 int *stat) /* success/failure */
79{
80 cur->bc_rec.a.ar_startblock = bno;
81 cur->bc_rec.a.ar_blockcount = len;
82 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
83}
84
85/*
86 * Lookup the first record less than or equal to [bno, len]
87 * in the btree given by cur.
88 */
Christoph Hellwiga46db602011-01-07 13:02:04 +000089int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110090xfs_alloc_lookup_le(
91 struct xfs_btree_cur *cur, /* btree cursor */
92 xfs_agblock_t bno, /* starting block of extent */
93 xfs_extlen_t len, /* length of extent */
94 int *stat) /* success/failure */
95{
96 cur->bc_rec.a.ar_startblock = bno;
97 cur->bc_rec.a.ar_blockcount = len;
98 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
99}
100
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100101/*
102 * Update the record referred to by cur to the value given
103 * by [bno, len].
104 * This either works (return 0) or gets an EFSCORRUPTED error.
105 */
106STATIC int /* error */
107xfs_alloc_update(
108 struct xfs_btree_cur *cur, /* btree cursor */
109 xfs_agblock_t bno, /* starting block of extent */
110 xfs_extlen_t len) /* length of extent */
111{
112 union xfs_btree_rec rec;
113
114 rec.alloc.ar_startblock = cpu_to_be32(bno);
115 rec.alloc.ar_blockcount = cpu_to_be32(len);
116 return xfs_btree_update(cur, &rec);
117}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100118
119/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100120 * Get the data from the pointed-to record.
121 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000122int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100123xfs_alloc_get_rec(
124 struct xfs_btree_cur *cur, /* btree cursor */
125 xfs_agblock_t *bno, /* output: starting block of extent */
126 xfs_extlen_t *len, /* output: length of extent */
127 int *stat) /* output: success/failure */
128{
129 union xfs_btree_rec *rec;
130 int error;
131
132 error = xfs_btree_get_rec(cur, &rec, stat);
133 if (!error && *stat == 1) {
134 *bno = be32_to_cpu(rec->alloc.ar_startblock);
135 *len = be32_to_cpu(rec->alloc.ar_blockcount);
136 }
137 return error;
138}
139
140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * Compute aligned version of the found extent.
142 * Takes alignment and min length into account.
143 */
David Chinner12375c82008-04-10 12:21:32 +1000144STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000146 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 xfs_agblock_t foundbno, /* starting block in found extent */
148 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xfs_agblock_t *resbno, /* result block number */
150 xfs_extlen_t *reslen) /* result length */
151{
152 xfs_agblock_t bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 xfs_extlen_t len;
154
Christoph Hellwige26f0502011-04-24 19:06:15 +0000155 /* Trim busy sections out of found extent */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000156 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000157
158 if (args->alignment > 1 && len >= args->minlen) {
159 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
160 xfs_extlen_t diff = aligned_bno - bno;
161
162 *resbno = aligned_bno;
163 *reslen = diff >= len ? 0 : len - diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000165 *resbno = bno;
166 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/*
171 * Compute best start block and diff for "near" allocations.
172 * freelen >= wantlen already checked by caller.
173 */
174STATIC xfs_extlen_t /* difference value (absolute) */
175xfs_alloc_compute_diff(
176 xfs_agblock_t wantbno, /* target starting block */
177 xfs_extlen_t wantlen, /* target length */
178 xfs_extlen_t alignment, /* target alignment */
Jan Kara211d0222013-04-11 22:09:56 +0200179 char userdata, /* are we allocating data? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 xfs_agblock_t freebno, /* freespace's starting block */
181 xfs_extlen_t freelen, /* freespace's length */
182 xfs_agblock_t *newbnop) /* result: best start block from free */
183{
184 xfs_agblock_t freeend; /* end of freespace extent */
185 xfs_agblock_t newbno1; /* return block number */
186 xfs_agblock_t newbno2; /* other new block number */
187 xfs_extlen_t newlen1=0; /* length with newbno1 */
188 xfs_extlen_t newlen2=0; /* length with newbno2 */
189 xfs_agblock_t wantend; /* end of target extent */
190
191 ASSERT(freelen >= wantlen);
192 freeend = freebno + freelen;
193 wantend = wantbno + wantlen;
Jan Kara211d0222013-04-11 22:09:56 +0200194 /*
195 * We want to allocate from the start of a free extent if it is past
196 * the desired block or if we are allocating user data and the free
197 * extent is before desired block. The second case is there to allow
198 * for contiguous allocation from the remaining free space if the file
199 * grows in the short term.
200 */
201 if (freebno >= wantbno || (userdata && freeend < wantend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
203 newbno1 = NULLAGBLOCK;
204 } else if (freeend >= wantend && alignment > 1) {
205 newbno1 = roundup(wantbno, alignment);
206 newbno2 = newbno1 - alignment;
207 if (newbno1 >= freeend)
208 newbno1 = NULLAGBLOCK;
209 else
210 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
211 if (newbno2 < freebno)
212 newbno2 = NULLAGBLOCK;
213 else
214 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
215 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
216 if (newlen1 < newlen2 ||
217 (newlen1 == newlen2 &&
218 XFS_ABSDIFF(newbno1, wantbno) >
219 XFS_ABSDIFF(newbno2, wantbno)))
220 newbno1 = newbno2;
221 } else if (newbno2 != NULLAGBLOCK)
222 newbno1 = newbno2;
223 } else if (freeend >= wantend) {
224 newbno1 = wantbno;
225 } else if (alignment > 1) {
226 newbno1 = roundup(freeend - wantlen, alignment);
227 if (newbno1 > freeend - wantlen &&
228 newbno1 - alignment >= freebno)
229 newbno1 -= alignment;
230 else if (newbno1 >= freeend)
231 newbno1 = NULLAGBLOCK;
232 } else
233 newbno1 = freeend - wantlen;
234 *newbnop = newbno1;
235 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
236}
237
238/*
239 * Fix up the length, based on mod and prod.
240 * len should be k * prod + mod for some k.
241 * If len is too small it is returned unchanged.
242 * If len hits maxlen it is left alone.
243 */
244STATIC void
245xfs_alloc_fix_len(
246 xfs_alloc_arg_t *args) /* allocation argument structure */
247{
248 xfs_extlen_t k;
249 xfs_extlen_t rlen;
250
251 ASSERT(args->mod < args->prod);
252 rlen = args->len;
253 ASSERT(rlen >= args->minlen);
254 ASSERT(rlen <= args->maxlen);
255 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
256 (args->mod == 0 && rlen < args->prod))
257 return;
258 k = rlen % args->prod;
259 if (k == args->mod)
260 return;
261 if (k > args->mod) {
262 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
263 return;
264 } else {
265 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
266 (int)args->minlen)
267 return;
268 }
269 ASSERT(rlen >= args->minlen);
270 ASSERT(rlen <= args->maxlen);
271 args->len = rlen;
272}
273
274/*
275 * Fix up length if there is too little space left in the a.g.
276 * Return 1 if ok, 0 if too little, should give up.
277 */
278STATIC int
279xfs_alloc_fix_minleft(
280 xfs_alloc_arg_t *args) /* allocation argument structure */
281{
282 xfs_agf_t *agf; /* a.g. freelist header */
283 int diff; /* free space difference */
284
285 if (args->minleft == 0)
286 return 1;
287 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100288 diff = be32_to_cpu(agf->agf_freeblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 - args->len - args->minleft;
290 if (diff >= 0)
291 return 1;
292 args->len += diff; /* shrink the allocated space */
293 if (args->len >= args->minlen)
294 return 1;
295 args->agbno = NULLAGBLOCK;
296 return 0;
297}
298
299/*
300 * Update the two btrees, logically removing from freespace the extent
301 * starting at rbno, rlen blocks. The extent is contained within the
302 * actual (current) free extent fbno for flen blocks.
303 * Flags are passed in indicating whether the cursors are set to the
304 * relevant records.
305 */
306STATIC int /* error code */
307xfs_alloc_fixup_trees(
308 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
309 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
310 xfs_agblock_t fbno, /* starting block of free extent */
311 xfs_extlen_t flen, /* length of free extent */
312 xfs_agblock_t rbno, /* starting block of returned extent */
313 xfs_extlen_t rlen, /* length of returned extent */
314 int flags) /* flags, XFSA_FIXUP_... */
315{
316 int error; /* error code */
317 int i; /* operation results */
318 xfs_agblock_t nfbno1; /* first new free startblock */
319 xfs_agblock_t nfbno2; /* second new free startblock */
320 xfs_extlen_t nflen1=0; /* first new free length */
321 xfs_extlen_t nflen2=0; /* second new free length */
322
323 /*
324 * Look up the record in the by-size tree if necessary.
325 */
326 if (flags & XFSA_FIXUP_CNT_OK) {
327#ifdef DEBUG
328 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
329 return error;
330 XFS_WANT_CORRUPTED_RETURN(
331 i == 1 && nfbno1 == fbno && nflen1 == flen);
332#endif
333 } else {
334 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
335 return error;
336 XFS_WANT_CORRUPTED_RETURN(i == 1);
337 }
338 /*
339 * Look up the record in the by-block tree if necessary.
340 */
341 if (flags & XFSA_FIXUP_BNO_OK) {
342#ifdef DEBUG
343 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
344 return error;
345 XFS_WANT_CORRUPTED_RETURN(
346 i == 1 && nfbno1 == fbno && nflen1 == flen);
347#endif
348 } else {
349 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
350 return error;
351 XFS_WANT_CORRUPTED_RETURN(i == 1);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100354#ifdef DEBUG
355 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
356 struct xfs_btree_block *bnoblock;
357 struct xfs_btree_block *cntblock;
358
359 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
360 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
361
362 XFS_WANT_CORRUPTED_RETURN(
363 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
368 * Deal with all four cases: the allocated record is contained
369 * within the freespace record, so we can have new freespace
370 * at either (or both) end, or no freespace remaining.
371 */
372 if (rbno == fbno && rlen == flen)
373 nfbno1 = nfbno2 = NULLAGBLOCK;
374 else if (rbno == fbno) {
375 nfbno1 = rbno + rlen;
376 nflen1 = flen - rlen;
377 nfbno2 = NULLAGBLOCK;
378 } else if (rbno + rlen == fbno + flen) {
379 nfbno1 = fbno;
380 nflen1 = flen - rlen;
381 nfbno2 = NULLAGBLOCK;
382 } else {
383 nfbno1 = fbno;
384 nflen1 = rbno - fbno;
385 nfbno2 = rbno + rlen;
386 nflen2 = (fbno + flen) - nfbno2;
387 }
388 /*
389 * Delete the entry from the by-size btree.
390 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100391 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return error;
393 XFS_WANT_CORRUPTED_RETURN(i == 1);
394 /*
395 * Add new by-size btree entry(s).
396 */
397 if (nfbno1 != NULLAGBLOCK) {
398 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
399 return error;
400 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100401 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return error;
403 XFS_WANT_CORRUPTED_RETURN(i == 1);
404 }
405 if (nfbno2 != NULLAGBLOCK) {
406 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
407 return error;
408 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100409 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return error;
411 XFS_WANT_CORRUPTED_RETURN(i == 1);
412 }
413 /*
414 * Fix up the by-block btree entry(s).
415 */
416 if (nfbno1 == NULLAGBLOCK) {
417 /*
418 * No remaining freespace, just delete the by-block tree entry.
419 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100420 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return error;
422 XFS_WANT_CORRUPTED_RETURN(i == 1);
423 } else {
424 /*
425 * Update the by-block entry to start later|be shorter.
426 */
427 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
428 return error;
429 }
430 if (nfbno2 != NULLAGBLOCK) {
431 /*
432 * 2 resulting free entries, need to add one.
433 */
434 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
435 return error;
436 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100437 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return error;
439 XFS_WANT_CORRUPTED_RETURN(i == 1);
440 }
441 return 0;
442}
443
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100444static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +1100445xfs_agfl_verify(
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100446 struct xfs_buf *bp)
447{
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100448 struct xfs_mount *mp = bp->b_target->bt_mount;
449 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100450 int i;
451
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100452 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_uuid))
453 return false;
454 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
455 return false;
456 /*
457 * during growfs operations, the perag is not fully initialised,
458 * so we can't use it for any useful checking. growfs ensures we can't
459 * use it by using uncached buffers that don't have the perag attached
460 * so we can detect and avoid this problem.
461 */
462 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
463 return false;
464
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100465 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100466 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100467 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100468 return false;
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100469 }
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100470 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100471}
472
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100473static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100474xfs_agfl_read_verify(
475 struct xfs_buf *bp)
476{
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100477 struct xfs_mount *mp = bp->b_target->bt_mount;
478 int agfl_ok = 1;
479
480 /*
481 * There is no verification of non-crc AGFLs because mkfs does not
482 * initialise the AGFL to zero or NULL. Hence the only valid part of the
483 * AGFL is what the AGF says is active. We can't get to the AGF, so we
484 * can't verify just those entries are valid.
485 */
486 if (!xfs_sb_version_hascrc(&mp->m_sb))
487 return;
488
489 agfl_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
490 offsetof(struct xfs_agfl, agfl_crc));
491
492 agfl_ok = agfl_ok && xfs_agfl_verify(bp);
493
494 if (!agfl_ok) {
495 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
496 xfs_buf_ioerror(bp, EFSCORRUPTED);
497 }
498}
499
500static void
501xfs_agfl_write_verify(
502 struct xfs_buf *bp)
503{
504 struct xfs_mount *mp = bp->b_target->bt_mount;
505 struct xfs_buf_log_item *bip = bp->b_fspriv;
506
507 /* no verification of non-crc AGFLs */
508 if (!xfs_sb_version_hascrc(&mp->m_sb))
509 return;
510
511 if (!xfs_agfl_verify(bp)) {
512 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
513 xfs_buf_ioerror(bp, EFSCORRUPTED);
514 return;
515 }
516
517 if (bip)
518 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
519
520 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
521 offsetof(struct xfs_agfl, agfl_crc));
Dave Chinnerbb80c6d2012-11-12 22:54:06 +1100522}
523
Dave Chinner1813dd62012-11-14 17:54:40 +1100524const struct xfs_buf_ops xfs_agfl_buf_ops = {
525 .verify_read = xfs_agfl_read_verify,
526 .verify_write = xfs_agfl_write_verify,
527};
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
530 * Read in the allocation group free block array.
531 */
532STATIC int /* error */
533xfs_alloc_read_agfl(
534 xfs_mount_t *mp, /* mount point structure */
535 xfs_trans_t *tp, /* transaction pointer */
536 xfs_agnumber_t agno, /* allocation group number */
537 xfs_buf_t **bpp) /* buffer for the ag free block array */
538{
539 xfs_buf_t *bp; /* return value */
540 int error;
541
542 ASSERT(agno != NULLAGNUMBER);
543 error = xfs_trans_read_buf(
544 mp, tp, mp->m_ddev_targp,
545 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100546 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (error)
548 return error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000549 ASSERT(!xfs_buf_geterror(bp));
Christoph Hellwig38f23232011-10-10 16:52:45 +0000550 xfs_buf_set_ref(bp, XFS_AGFL_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *bpp = bp;
552 return 0;
553}
554
Christoph Hellwigecb69282011-03-04 12:59:55 +0000555STATIC int
556xfs_alloc_update_counters(
557 struct xfs_trans *tp,
558 struct xfs_perag *pag,
559 struct xfs_buf *agbp,
560 long len)
561{
562 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
563
564 pag->pagf_freeblks += len;
565 be32_add_cpu(&agf->agf_freeblks, len);
566
567 xfs_trans_agblocks_delta(tp, len);
568 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
569 be32_to_cpu(agf->agf_length)))
570 return EFSCORRUPTED;
571
572 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
573 return 0;
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Allocation group level functions.
578 */
579
580/*
581 * Allocate a variable extent in the allocation group agno.
582 * Type and bno are used to determine where in the allocation group the
583 * extent will start.
584 * Extent's length (returned in *len) will be between minlen and maxlen,
585 * and of the form k * prod + mod unless there's nothing that large.
586 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
587 */
588STATIC int /* error */
589xfs_alloc_ag_vextent(
590 xfs_alloc_arg_t *args) /* argument structure for allocation */
591{
592 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 ASSERT(args->minlen > 0);
595 ASSERT(args->maxlen > 0);
596 ASSERT(args->minlen <= args->maxlen);
597 ASSERT(args->mod < args->prod);
598 ASSERT(args->alignment > 0);
599 /*
600 * Branch to correct routine based on the type.
601 */
602 args->wasfromfl = 0;
603 switch (args->type) {
604 case XFS_ALLOCTYPE_THIS_AG:
605 error = xfs_alloc_ag_vextent_size(args);
606 break;
607 case XFS_ALLOCTYPE_NEAR_BNO:
608 error = xfs_alloc_ag_vextent_near(args);
609 break;
610 case XFS_ALLOCTYPE_THIS_BNO:
611 error = xfs_alloc_ag_vextent_exact(args);
612 break;
613 default:
614 ASSERT(0);
615 /* NOTREACHED */
616 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000617
618 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Christoph Hellwigecb69282011-03-04 12:59:55 +0000621 ASSERT(args->len >= args->minlen);
622 ASSERT(args->len <= args->maxlen);
623 ASSERT(!args->wasfromfl || !args->isfl);
624 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwigecb69282011-03-04 12:59:55 +0000626 if (!args->wasfromfl) {
627 error = xfs_alloc_update_counters(args->tp, args->pag,
628 args->agbp,
629 -((long)(args->len)));
630 if (error)
631 return error;
632
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000633 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000634 args->agbno, args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000636
637 if (!args->isfl) {
638 xfs_trans_mod_sb(args->tp, args->wasdel ?
639 XFS_TRANS_SB_RES_FDBLOCKS :
640 XFS_TRANS_SB_FDBLOCKS,
641 -((long)(args->len)));
642 }
643
644 XFS_STATS_INC(xs_allocx);
645 XFS_STATS_ADD(xs_allocb, args->len);
646 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
650 * Allocate a variable extent at exactly agno/bno.
651 * Extent's length (returned in *len) will be between minlen and maxlen,
652 * and of the form k * prod + mod unless there's nothing that large.
653 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
654 */
655STATIC int /* error */
656xfs_alloc_ag_vextent_exact(
657 xfs_alloc_arg_t *args) /* allocation argument structure */
658{
659 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
660 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int error;
662 xfs_agblock_t fbno; /* start block of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_extlen_t flen; /* length of found extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000664 xfs_agblock_t tbno; /* start block of trimmed extent */
665 xfs_extlen_t tlen; /* length of trimmed extent */
666 xfs_agblock_t tend; /* end block of trimmed extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int i; /* success/failure of operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * Allocate/initialize a cursor for the by-number freespace btree.
673 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100674 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000675 args->agno, XFS_BTNUM_BNO);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
679 * Look for the closest free block <= bno, it must contain bno
680 * if any free block does.
681 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000682 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
683 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000685 if (!i)
686 goto not_found;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Grab the freespace record.
690 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000691 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
692 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto error0;
694 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
695 ASSERT(fbno <= args->agbno);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +0000698 * Check for overlapping busy extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000700 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000701
702 /*
703 * Give up if the start of the extent is busy, or the freespace isn't
704 * long enough for the minimum request.
705 */
706 if (tbno > args->agbno)
707 goto not_found;
708 if (tlen < args->minlen)
709 goto not_found;
710 tend = tbno + tlen;
711 if (tend < args->agbno + args->minlen)
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000712 goto not_found;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * End of extent will be smaller of the freespace end and the
716 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000717 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * Fix the length according to mod and prod if given.
719 */
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000720 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
721 - args->agbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000723 if (!xfs_alloc_fix_minleft(args))
724 goto not_found;
725
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000726 ASSERT(args->agbno + args->len <= tend);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
Chandra Seetharaman81463b12011-06-09 16:47:49 +0000729 * We are allocating agbno for args->len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * Allocate/initialize a cursor for the by-size btree.
731 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100732 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
733 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100735 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000736 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
737 args->len, XFSA_FIXUP_BNO_OK);
738 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
740 goto error0;
741 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
744 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000747 trace_xfs_alloc_exact_done(args);
748 return 0;
749
750not_found:
751 /* Didn't find it, return null. */
752 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
753 args->agbno = NULLAGBLOCK;
754 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756
757error0:
758 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000759 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return error;
761}
762
763/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000764 * Search the btree in a given direction via the search cursor and compare
765 * the records found against the good extent we've already found.
766 */
767STATIC int
768xfs_alloc_find_best_extent(
769 struct xfs_alloc_arg *args, /* allocation argument structure */
770 struct xfs_btree_cur **gcur, /* good cursor */
771 struct xfs_btree_cur **scur, /* searching cursor */
772 xfs_agblock_t gdiff, /* difference for search comparison */
773 xfs_agblock_t *sbno, /* extent found by search */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000774 xfs_extlen_t *slen, /* extent length */
775 xfs_agblock_t *sbnoa, /* aligned extent found by search */
776 xfs_extlen_t *slena, /* aligned extent length */
Christoph Hellwig489a1502010-12-10 15:04:11 +0000777 int dir) /* 0 = search right, 1 = search left */
778{
Christoph Hellwig489a1502010-12-10 15:04:11 +0000779 xfs_agblock_t new;
780 xfs_agblock_t sdiff;
781 int error;
782 int i;
783
784 /* The good extent is perfect, no need to search. */
785 if (!gdiff)
786 goto out_use_good;
787
788 /*
789 * Look until we find a better one, run out of space or run off the end.
790 */
791 do {
792 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
793 if (error)
794 goto error0;
795 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000796 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000797
798 /*
799 * The good extent is closer than this one.
800 */
801 if (!dir) {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000802 if (*sbnoa >= args->agbno + gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000803 goto out_use_good;
804 } else {
Christoph Hellwige26f0502011-04-24 19:06:15 +0000805 if (*sbnoa <= args->agbno - gdiff)
Christoph Hellwig489a1502010-12-10 15:04:11 +0000806 goto out_use_good;
807 }
808
809 /*
810 * Same distance, compare length and pick the best.
811 */
812 if (*slena >= args->minlen) {
813 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
814 xfs_alloc_fix_len(args);
815
816 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200817 args->alignment,
818 args->userdata, *sbnoa,
Christoph Hellwige26f0502011-04-24 19:06:15 +0000819 *slena, &new);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000820
821 /*
822 * Choose closer size and invalidate other cursor.
823 */
824 if (sdiff < gdiff)
825 goto out_use_search;
826 goto out_use_good;
827 }
828
829 if (!dir)
830 error = xfs_btree_increment(*scur, 0, &i);
831 else
832 error = xfs_btree_decrement(*scur, 0, &i);
833 if (error)
834 goto error0;
835 } while (i);
836
837out_use_good:
838 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
839 *scur = NULL;
840 return 0;
841
842out_use_search:
843 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
844 *gcur = NULL;
845 return 0;
846
847error0:
848 /* caller invalidates cursors */
849 return error;
850}
851
852/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * Allocate a variable extent near bno in the allocation group agno.
854 * Extent's length (returned in len) will be between minlen and maxlen,
855 * and of the form k * prod + mod unless there's nothing that large.
856 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
857 */
858STATIC int /* error */
859xfs_alloc_ag_vextent_near(
860 xfs_alloc_arg_t *args) /* allocation argument structure */
861{
862 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
863 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
864 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xfs_agblock_t gtbno; /* start bno of right side entry */
866 xfs_agblock_t gtbnoa; /* aligned ... */
867 xfs_extlen_t gtdiff; /* difference to right side entry */
868 xfs_extlen_t gtlen; /* length of right side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000869 xfs_extlen_t gtlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 xfs_agblock_t gtnew; /* useful start bno of right side */
871 int error; /* error code */
872 int i; /* result code, temporary */
873 int j; /* result code, temporary */
874 xfs_agblock_t ltbno; /* start bno of left side entry */
875 xfs_agblock_t ltbnoa; /* aligned ... */
876 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 xfs_extlen_t ltlen; /* length of left side entry */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000878 xfs_extlen_t ltlena; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_agblock_t ltnew; /* useful start bno of left side */
880 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +0000881 int forced = 0;
Dave Chinner63d20d62013-08-12 20:49:50 +1000882#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * Randomly don't execute the first algorithm.
885 */
886 int dofirst; /* set to do first algorithm */
887
Akinobu Mitaecb34032013-03-04 21:58:20 +0900888 dofirst = prandom_u32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif
Christoph Hellwige26f0502011-04-24 19:06:15 +0000890
891restart:
892 bno_cur_lt = NULL;
893 bno_cur_gt = NULL;
894 ltlen = 0;
895 gtlena = 0;
896 ltlena = 0;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Get a cursor for the by-size btree.
900 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100901 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
902 args->agno, XFS_BTNUM_CNT);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * See if there are any free extents as big as maxlen.
906 */
907 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
908 goto error0;
909 /*
910 * If none, then pick up the last entry in the tree unless the
911 * tree is empty.
912 */
913 if (!i) {
914 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
915 &ltlen, &i)))
916 goto error0;
917 if (i == 0 || ltlen == 0) {
918 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwige26f0502011-04-24 19:06:15 +0000919 trace_xfs_alloc_near_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
922 ASSERT(i == 1);
923 }
924 args->wasfromfl = 0;
Christoph Hellwige26f0502011-04-24 19:06:15 +0000925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * First algorithm.
928 * If the requested extent is large wrt the freespaces available
929 * in this a.g., then the cursor will be pointing to a btree entry
930 * near the right edge of the tree. If it's in the last btree leaf
931 * block, then we just examine all the entries in that block
932 * that are big enough, and pick the best one.
933 * This is written as a while loop so we can break out of it,
934 * but we never loop back to the top.
935 */
936 while (xfs_btree_islastblock(cnt_cur, 0)) {
937 xfs_extlen_t bdiff;
938 int besti=0;
939 xfs_extlen_t blen=0;
940 xfs_agblock_t bnew=0;
941
Dave Chinner63d20d62013-08-12 20:49:50 +1000942#ifdef DEBUG
943 if (dofirst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945#endif
946 /*
947 * Start from the entry that lookup found, sequence through
948 * all larger free blocks. If we're actually pointing at a
949 * record smaller than maxlen, go to the start of this block,
950 * and skip all those smaller than minlen.
951 */
952 if (ltlen || args->alignment > 1) {
953 cnt_cur->bc_ptrs[0] = 1;
954 do {
955 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
956 &ltlen, &i)))
957 goto error0;
958 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
959 if (ltlen >= args->minlen)
960 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100961 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto error0;
963 } while (i);
964 ASSERT(ltlen >= args->minlen);
965 if (!i)
966 break;
967 }
968 i = cnt_cur->bc_ptrs[0];
969 for (j = 1, blen = 0, bdiff = 0;
970 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100971 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /*
973 * For each entry, decide if it's better than
974 * the previous best entry.
975 */
976 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
977 goto error0;
978 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000979 xfs_alloc_compute_aligned(args, ltbno, ltlen,
980 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +1000981 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 continue;
983 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
984 xfs_alloc_fix_len(args);
985 ASSERT(args->len >= args->minlen);
986 if (args->len < blen)
987 continue;
988 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +0200989 args->alignment, args->userdata, ltbnoa,
990 ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (ltnew != NULLAGBLOCK &&
992 (args->len > blen || ltdiff < bdiff)) {
993 bdiff = ltdiff;
994 bnew = ltnew;
995 blen = args->len;
996 besti = cnt_cur->bc_ptrs[0];
997 }
998 }
999 /*
1000 * It didn't work. We COULD be in a case where
1001 * there's a good record somewhere, so try again.
1002 */
1003 if (blen == 0)
1004 break;
1005 /*
1006 * Point at the best entry, and retrieve it again.
1007 */
1008 cnt_cur->bc_ptrs[0] = besti;
1009 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1010 goto error0;
1011 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001012 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 args->len = blen;
1014 if (!xfs_alloc_fix_minleft(args)) {
1015 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001016 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018 }
1019 blen = args->len;
1020 /*
1021 * We are allocating starting at bnew for blen blocks.
1022 */
1023 args->agbno = bnew;
1024 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001025 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /*
1027 * Set up a cursor for the by-bno tree.
1028 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001029 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1030 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * Fix up the btree entries.
1033 */
1034 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1035 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1036 goto error0;
1037 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1038 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001039
1040 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042 }
1043 /*
1044 * Second algorithm.
1045 * Search in the by-bno tree to the left and to the right
1046 * simultaneously, until in each case we find a space big enough,
1047 * or run into the edge of the tree. When we run into the edge,
1048 * we deallocate that cursor.
1049 * If both searches succeed, we compare the two spaces and pick
1050 * the better one.
1051 * With alignment, it's possible for both to fail; the upper
1052 * level algorithm that picks allocation groups for allocations
1053 * is not supposed to do this.
1054 */
1055 /*
1056 * Allocate and initialize the cursor for the leftward search.
1057 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001058 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1059 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
1061 * Lookup <= bno to find the leftward search's starting point.
1062 */
1063 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1064 goto error0;
1065 if (!i) {
1066 /*
1067 * Didn't find anything; use this cursor for the rightward
1068 * search.
1069 */
1070 bno_cur_gt = bno_cur_lt;
1071 bno_cur_lt = NULL;
1072 }
1073 /*
1074 * Found something. Duplicate the cursor for the rightward search.
1075 */
1076 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1077 goto error0;
1078 /*
1079 * Increment the cursor, so we will point at the entry just right
1080 * of the leftward entry if any, or to the leftmost entry.
1081 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001082 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto error0;
1084 if (!i) {
1085 /*
1086 * It failed, there are no rightward entries.
1087 */
1088 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1089 bno_cur_gt = NULL;
1090 }
1091 /*
1092 * Loop going left with the leftward cursor, right with the
1093 * rightward cursor, until either both directions give up or
1094 * we find an entry at least as big as minlen.
1095 */
1096 do {
1097 if (bno_cur_lt) {
1098 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1099 goto error0;
1100 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001101 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1102 &ltbnoa, &ltlena);
David Chinner12375c82008-04-10 12:21:32 +10001103 if (ltlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001105 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto error0;
1107 if (!i) {
1108 xfs_btree_del_cursor(bno_cur_lt,
1109 XFS_BTREE_NOERROR);
1110 bno_cur_lt = NULL;
1111 }
1112 }
1113 if (bno_cur_gt) {
1114 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1115 goto error0;
1116 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001117 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1118 &gtbnoa, &gtlena);
David Chinner12375c82008-04-10 12:21:32 +10001119 if (gtlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001121 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto error0;
1123 if (!i) {
1124 xfs_btree_del_cursor(bno_cur_gt,
1125 XFS_BTREE_NOERROR);
1126 bno_cur_gt = NULL;
1127 }
1128 }
1129 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * Got both cursors still active, need to find better entry.
1133 */
1134 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (ltlena >= args->minlen) {
1136 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001137 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1140 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001141 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001142 args->alignment, args->userdata, ltbnoa,
1143 ltlena, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001144
1145 error = xfs_alloc_find_best_extent(args,
1146 &bno_cur_lt, &bno_cur_gt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001147 ltdiff, &gtbno, &gtlen,
1148 &gtbnoa, &gtlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001149 0 /* search right */);
1150 } else {
1151 ASSERT(gtlena >= args->minlen);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001154 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1157 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001158 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Jan Kara211d0222013-04-11 22:09:56 +02001159 args->alignment, args->userdata, gtbnoa,
1160 gtlena, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001161
1162 error = xfs_alloc_find_best_extent(args,
1163 &bno_cur_gt, &bno_cur_lt,
Christoph Hellwige26f0502011-04-24 19:06:15 +00001164 gtdiff, &ltbno, &ltlen,
1165 &ltbnoa, &ltlena,
Christoph Hellwig489a1502010-12-10 15:04:11 +00001166 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001168
1169 if (error)
1170 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * If we couldn't get anything, give up.
1175 */
1176 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Dave Chinnere3a746f52012-07-12 07:40:42 +10001177 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1178
Christoph Hellwige26f0502011-04-24 19:06:15 +00001179 if (!forced++) {
1180 trace_xfs_alloc_near_busy(args);
1181 xfs_log_force(args->mp, XFS_LOG_SYNC);
1182 goto restart;
1183 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001184 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 args->agbno = NULLAGBLOCK;
1186 return 0;
1187 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /*
1190 * At this point we have selected a freespace entry, either to the
1191 * left or to the right. If it's on the right, copy all the
1192 * useful variables to the "left" set so we only have one
1193 * copy of this code.
1194 */
1195 if (bno_cur_gt) {
1196 bno_cur_lt = bno_cur_gt;
1197 bno_cur_gt = NULL;
1198 ltbno = gtbno;
1199 ltbnoa = gtbnoa;
1200 ltlen = gtlen;
1201 ltlena = gtlena;
1202 j = 1;
1203 } else
1204 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Fix up the length and compute the useful address.
1208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1210 xfs_alloc_fix_len(args);
1211 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001212 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1214 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1215 return 0;
1216 }
1217 rlen = args->len;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001218 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
Jan Kara211d0222013-04-11 22:09:56 +02001219 args->userdata, ltbnoa, ltlena, &ltnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 ASSERT(ltnew >= ltbno);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001221 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001222 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 args->agbno = ltnew;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1226 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1227 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001228
1229 if (j)
1230 trace_xfs_alloc_near_greater(args);
1231 else
1232 trace_xfs_alloc_near_lesser(args);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1235 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1236 return 0;
1237
1238 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001239 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (cnt_cur != NULL)
1241 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1242 if (bno_cur_lt != NULL)
1243 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1244 if (bno_cur_gt != NULL)
1245 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1246 return error;
1247}
1248
1249/*
1250 * Allocate a variable extent anywhere in the allocation group agno.
1251 * Extent's length (returned in len) will be between minlen and maxlen,
1252 * and of the form k * prod + mod unless there's nothing that large.
1253 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1254 */
1255STATIC int /* error */
1256xfs_alloc_ag_vextent_size(
1257 xfs_alloc_arg_t *args) /* allocation argument structure */
1258{
1259 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1260 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1261 int error; /* error result */
1262 xfs_agblock_t fbno; /* start of found freespace */
1263 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int i; /* temp status variable */
1265 xfs_agblock_t rbno; /* returned block number */
1266 xfs_extlen_t rlen; /* length of returned extent */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001267 int forced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Christoph Hellwige26f0502011-04-24 19:06:15 +00001269restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Allocate and initialize a cursor for the by-size btree.
1272 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001273 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1274 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 bno_cur = NULL;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * Look for an entry >= maxlen+alignment-1 blocks.
1279 */
1280 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1281 args->maxlen + args->alignment - 1, &i)))
1282 goto error0;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
Christoph Hellwige26f0502011-04-24 19:06:15 +00001285 * If none or we have busy extents that we cannot allocate from, then
1286 * we have to settle for a smaller extent. In the case that there are
1287 * no large extents, this will return the last entry in the tree unless
1288 * the tree is empty. In the case that there are only busy large
1289 * extents, this will return the largest small extent unless there
1290 * are no smaller extents available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Christoph Hellwige26f0502011-04-24 19:06:15 +00001292 if (!i || forced > 1) {
1293 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1294 &fbno, &flen, &i);
1295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto error0;
1297 if (i == 0 || flen == 0) {
1298 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001299 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301 }
1302 ASSERT(i == 1);
Christoph Hellwige26f0502011-04-24 19:06:15 +00001303 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1304 } else {
1305 /*
1306 * Search for a non-busy extent that is large enough.
1307 * If we are at low space, don't check, or if we fall of
1308 * the end of the btree, turn off the busy check and
1309 * restart.
1310 */
1311 for (;;) {
1312 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1313 if (error)
1314 goto error0;
1315 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1316
1317 xfs_alloc_compute_aligned(args, fbno, flen,
1318 &rbno, &rlen);
1319
1320 if (rlen >= args->maxlen)
1321 break;
1322
1323 error = xfs_btree_increment(cnt_cur, 0, &i);
1324 if (error)
1325 goto error0;
1326 if (i == 0) {
1327 /*
1328 * Our only valid extents must have been busy.
1329 * Make it unbusy by forcing the log out and
1330 * retrying. If we've been here before, forcing
1331 * the log isn't making the extents available,
1332 * which means they have probably been freed in
1333 * this transaction. In that case, we have to
1334 * give up on them and we'll attempt a minlen
1335 * allocation the next time around.
1336 */
1337 xfs_btree_del_cursor(cnt_cur,
1338 XFS_BTREE_NOERROR);
1339 trace_xfs_alloc_size_busy(args);
1340 if (!forced++)
1341 xfs_log_force(args->mp, XFS_LOG_SYNC);
1342 goto restart;
1343 }
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /*
1348 * In the first case above, we got the last entry in the
1349 * by-size btree. Now we check to see if the space hits maxlen
1350 * once aligned; if not, we search left for something better.
1351 * This can't happen in the second case above.
1352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1354 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1355 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1356 if (rlen < args->maxlen) {
1357 xfs_agblock_t bestfbno;
1358 xfs_extlen_t bestflen;
1359 xfs_agblock_t bestrbno;
1360 xfs_extlen_t bestrlen;
1361
1362 bestrlen = rlen;
1363 bestrbno = rbno;
1364 bestflen = flen;
1365 bestfbno = fbno;
1366 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001367 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto error0;
1369 if (i == 0)
1370 break;
1371 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1372 &i)))
1373 goto error0;
1374 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1375 if (flen < bestrlen)
1376 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001377 xfs_alloc_compute_aligned(args, fbno, flen,
1378 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1380 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1381 (rlen <= flen && rbno + rlen <= fbno + flen),
1382 error0);
1383 if (rlen > bestrlen) {
1384 bestrlen = rlen;
1385 bestrbno = rbno;
1386 bestflen = flen;
1387 bestfbno = fbno;
1388 if (rlen == args->maxlen)
1389 break;
1390 }
1391 }
1392 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1393 &i)))
1394 goto error0;
1395 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1396 rlen = bestrlen;
1397 rbno = bestrbno;
1398 flen = bestflen;
1399 fbno = bestfbno;
1400 }
1401 args->wasfromfl = 0;
1402 /*
1403 * Fix up the length.
1404 */
1405 args->len = rlen;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001406 if (rlen < args->minlen) {
1407 if (!forced++) {
1408 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1409 trace_xfs_alloc_size_busy(args);
1410 xfs_log_force(args->mp, XFS_LOG_SYNC);
1411 goto restart;
1412 }
1413 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Christoph Hellwige26f0502011-04-24 19:06:15 +00001415 xfs_alloc_fix_len(args);
1416
1417 if (!xfs_alloc_fix_minleft(args))
1418 goto out_nominleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 rlen = args->len;
1420 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1421 /*
1422 * Allocate and initialize a cursor for the by-block tree.
1423 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001424 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1425 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1427 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1428 goto error0;
1429 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1430 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1431 cnt_cur = bno_cur = NULL;
1432 args->len = rlen;
1433 args->agbno = rbno;
1434 XFS_WANT_CORRUPTED_GOTO(
1435 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001436 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001438 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
1440
1441error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001442 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (cnt_cur)
1444 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1445 if (bno_cur)
1446 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1447 return error;
Christoph Hellwige26f0502011-04-24 19:06:15 +00001448
1449out_nominleft:
1450 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1451 trace_xfs_alloc_size_nominleft(args);
1452 args->agbno = NULLAGBLOCK;
1453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456/*
1457 * Deal with the case where only small freespaces remain.
1458 * Either return the contents of the last freespace record,
1459 * or allocate space from the freelist if there is nothing in the tree.
1460 */
1461STATIC int /* error */
1462xfs_alloc_ag_vextent_small(
1463 xfs_alloc_arg_t *args, /* allocation argument structure */
1464 xfs_btree_cur_t *ccur, /* by-size cursor */
1465 xfs_agblock_t *fbnop, /* result block number */
1466 xfs_extlen_t *flenp, /* result length */
1467 int *stat) /* status: 0-freelist, 1-normal/none */
1468{
1469 int error;
1470 xfs_agblock_t fbno;
1471 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int i;
1473
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001474 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto error0;
1476 if (i) {
1477 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1478 goto error0;
1479 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1480 }
1481 /*
1482 * Nothing in the btree, try the freelist. Make sure
1483 * to respect minleft even when pulling from the
1484 * freelist.
1485 */
1486 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001487 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1488 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001489 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1490 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto error0;
1492 if (fbno != NULLAGBLOCK) {
Dave Chinner4ecbfe62012-04-29 10:41:10 +00001493 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
Christoph Hellwig97d3ac72011-04-24 19:06:16 +00001494 args->userdata);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (args->userdata) {
1497 xfs_buf_t *bp;
1498
1499 bp = xfs_btree_get_bufs(args->mp, args->tp,
1500 args->agno, fbno, 0);
1501 xfs_trans_binval(args->tp, bp);
1502 }
1503 args->len = 1;
1504 args->agbno = fbno;
1505 XFS_WANT_CORRUPTED_GOTO(
1506 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001507 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 error0);
1509 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001510 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *stat = 0;
1512 return 0;
1513 }
1514 /*
1515 * Nothing in the freelist.
1516 */
1517 else
1518 flen = 0;
1519 }
1520 /*
1521 * Can't allocate from the freelist for some reason.
1522 */
Nathan Scottd432c802006-09-28 11:03:44 +10001523 else {
1524 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Can't do the allocation, give up.
1529 */
1530 if (flen < args->minlen) {
1531 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001532 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 flen = 0;
1534 }
1535 *fbnop = fbno;
1536 *flenp = flen;
1537 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001538 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
1540
1541error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001542 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return error;
1544}
1545
1546/*
1547 * Free the extent starting at agno/bno for length.
1548 */
1549STATIC int /* error */
1550xfs_free_ag_extent(
1551 xfs_trans_t *tp, /* transaction pointer */
1552 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1553 xfs_agnumber_t agno, /* allocation group number */
1554 xfs_agblock_t bno, /* starting block number */
1555 xfs_extlen_t len, /* length of extent */
1556 int isfl) /* set if is freelist blocks - no sb acctg */
1557{
1558 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1559 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1560 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 xfs_agblock_t gtbno; /* start of right neighbor block */
1562 xfs_extlen_t gtlen; /* length of right neighbor block */
1563 int haveleft; /* have a left neighbor block */
1564 int haveright; /* have a right neighbor block */
1565 int i; /* temp, result code */
1566 xfs_agblock_t ltbno; /* start of left neighbor block */
1567 xfs_extlen_t ltlen; /* length of left neighbor block */
1568 xfs_mount_t *mp; /* mount point struct for filesystem */
1569 xfs_agblock_t nbno; /* new starting block of freespace */
1570 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001571 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 mp = tp->t_mountp;
1574 /*
1575 * Allocate and initialize a cursor for the by-block btree.
1576 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001577 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 cnt_cur = NULL;
1579 /*
1580 * Look for a neighboring block on the left (lower block numbers)
1581 * that is contiguous with this space.
1582 */
1583 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1584 goto error0;
1585 if (haveleft) {
1586 /*
1587 * There is a block to our left.
1588 */
1589 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1590 goto error0;
1591 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1592 /*
1593 * It's not contiguous, though.
1594 */
1595 if (ltbno + ltlen < bno)
1596 haveleft = 0;
1597 else {
1598 /*
1599 * If this failure happens the request to free this
1600 * space was invalid, it's (partly) already free.
1601 * Very bad.
1602 */
1603 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1604 }
1605 }
1606 /*
1607 * Look for a neighboring block on the right (higher block numbers)
1608 * that is contiguous with this space.
1609 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001610 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto error0;
1612 if (haveright) {
1613 /*
1614 * There is a block to our right.
1615 */
1616 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1617 goto error0;
1618 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1619 /*
1620 * It's not contiguous, though.
1621 */
1622 if (bno + len < gtbno)
1623 haveright = 0;
1624 else {
1625 /*
1626 * If this failure happens the request to free this
1627 * space was invalid, it's (partly) already free.
1628 * Very bad.
1629 */
1630 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1631 }
1632 }
1633 /*
1634 * Now allocate and initialize a cursor for the by-size tree.
1635 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001636 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /*
1638 * Have both left and right contiguous neighbors.
1639 * Merge all three into a single free block.
1640 */
1641 if (haveleft && haveright) {
1642 /*
1643 * Delete the old by-size entry on the left.
1644 */
1645 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1646 goto error0;
1647 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001648 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto error0;
1650 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1651 /*
1652 * Delete the old by-size entry on the right.
1653 */
1654 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1655 goto error0;
1656 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001657 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto error0;
1659 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1660 /*
1661 * Delete the old by-block entry for the right block.
1662 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001663 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto error0;
1665 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1666 /*
1667 * Move the by-block cursor back to the left neighbor.
1668 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001669 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto error0;
1671 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1672#ifdef DEBUG
1673 /*
1674 * Check that this is the right record: delete didn't
1675 * mangle the cursor.
1676 */
1677 {
1678 xfs_agblock_t xxbno;
1679 xfs_extlen_t xxlen;
1680
1681 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1682 &i)))
1683 goto error0;
1684 XFS_WANT_CORRUPTED_GOTO(
1685 i == 1 && xxbno == ltbno && xxlen == ltlen,
1686 error0);
1687 }
1688#endif
1689 /*
1690 * Update remaining by-block entry to the new, joined block.
1691 */
1692 nbno = ltbno;
1693 nlen = len + ltlen + gtlen;
1694 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1695 goto error0;
1696 }
1697 /*
1698 * Have only a left contiguous neighbor.
1699 * Merge it together with the new freespace.
1700 */
1701 else if (haveleft) {
1702 /*
1703 * Delete the old by-size entry on the left.
1704 */
1705 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1706 goto error0;
1707 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001708 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto error0;
1710 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1711 /*
1712 * Back up the by-block cursor to the left neighbor, and
1713 * update its length.
1714 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001715 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto error0;
1717 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1718 nbno = ltbno;
1719 nlen = len + ltlen;
1720 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1721 goto error0;
1722 }
1723 /*
1724 * Have only a right contiguous neighbor.
1725 * Merge it together with the new freespace.
1726 */
1727 else if (haveright) {
1728 /*
1729 * Delete the old by-size entry on the right.
1730 */
1731 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1732 goto error0;
1733 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001734 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto error0;
1736 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1737 /*
1738 * Update the starting block and length of the right
1739 * neighbor in the by-block tree.
1740 */
1741 nbno = bno;
1742 nlen = len + gtlen;
1743 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1744 goto error0;
1745 }
1746 /*
1747 * No contiguous neighbors.
1748 * Insert the new freespace into the by-block tree.
1749 */
1750 else {
1751 nbno = bno;
1752 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001753 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto error0;
1755 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1756 }
1757 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1758 bno_cur = NULL;
1759 /*
1760 * In all cases we need to insert the new freespace in the by-size tree.
1761 */
1762 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1763 goto error0;
1764 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001765 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto error0;
1767 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1768 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1769 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * Update the freespace totals in the ag and superblock.
1773 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001774 pag = xfs_perag_get(mp, agno);
1775 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1776 xfs_perag_put(pag);
1777 if (error)
1778 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Christoph Hellwigecb69282011-03-04 12:59:55 +00001780 if (!isfl)
1781 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1782 XFS_STATS_INC(xs_freex);
1783 XFS_STATS_ADD(xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001784
1785 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return 0;
1788
1789 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001790 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (bno_cur)
1792 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1793 if (cnt_cur)
1794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1795 return error;
1796}
1797
1798/*
1799 * Visible (exported) allocation/free functions.
1800 * Some of these are used just by xfs_alloc_btree.c and this file.
1801 */
1802
1803/*
1804 * Compute and fill in value of m_ag_maxlevels.
1805 */
1806void
1807xfs_alloc_compute_maxlevels(
1808 xfs_mount_t *mp) /* file system mount structure */
1809{
1810 int level;
1811 uint maxblocks;
1812 uint maxleafents;
1813 int minleafrecs;
1814 int minnoderecs;
1815
1816 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1817 minleafrecs = mp->m_alloc_mnr[0];
1818 minnoderecs = mp->m_alloc_mnr[1];
1819 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1820 for (level = 1; maxblocks > 1; level++)
1821 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1822 mp->m_ag_maxlevels = level;
1823}
1824
1825/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001826 * Find the length of the longest extent in an AG.
1827 */
1828xfs_extlen_t
1829xfs_alloc_longest_free_extent(
1830 struct xfs_mount *mp,
1831 struct xfs_perag *pag)
1832{
1833 xfs_extlen_t need, delta = 0;
1834
1835 need = XFS_MIN_FREELIST_PAG(pag, mp);
1836 if (need > pag->pagf_flcount)
1837 delta = need - pag->pagf_flcount;
1838
1839 if (pag->pagf_longest > delta)
1840 return pag->pagf_longest - delta;
1841 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1842}
1843
1844/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 * Decide whether to use this allocation group for this allocation.
1846 * If so, fix up the btree freelist's size.
1847 */
1848STATIC int /* error */
1849xfs_alloc_fix_freelist(
1850 xfs_alloc_arg_t *args, /* allocation argument structure */
1851 int flags) /* XFS_ALLOC_FLAG_... */
1852{
1853 xfs_buf_t *agbp; /* agf buffer pointer */
1854 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1855 xfs_buf_t *agflbp;/* agfl buffer pointer */
1856 xfs_agblock_t bno; /* freelist block */
1857 xfs_extlen_t delta; /* new blocks needed in freelist */
1858 int error; /* error result code */
1859 xfs_extlen_t longest;/* longest extent in allocation group */
1860 xfs_mount_t *mp; /* file system mount point structure */
1861 xfs_extlen_t need; /* total blocks needed in freelist */
1862 xfs_perag_t *pag; /* per-ag information structure */
1863 xfs_alloc_arg_t targs; /* local allocation arguments */
1864 xfs_trans_t *tp; /* transaction pointer */
1865
1866 mp = args->mp;
1867
1868 pag = args->pag;
1869 tp = args->tp;
1870 if (!pag->pagf_init) {
1871 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1872 &agbp)))
1873 return error;
1874 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001875 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1876 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 args->agbp = NULL;
1878 return 0;
1879 }
1880 } else
1881 agbp = NULL;
1882
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001883 /*
1884 * If this is a metadata preferred pag and we are user data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * then try somewhere else if we are not being asked to
1886 * try harder at this point
1887 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001888 if (pag->pagf_metadata && args->userdata &&
1889 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1890 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 args->agbp = NULL;
1892 return 0;
1893 }
1894
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001895 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001896 /*
1897 * If it looks like there isn't a long enough extent, or enough
1898 * total blocks, reject it.
1899 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001900 need = XFS_MIN_FREELIST_PAG(pag, mp);
1901 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001902 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1903 longest ||
1904 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1905 need - args->total) < (int)args->minleft)) {
1906 if (agbp)
1907 xfs_trans_brelse(tp, agbp);
1908 args->agbp = NULL;
1909 return 0;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
1914 * Get the a.g. freespace buffer.
1915 * Can fail if we're not blocking on locks, and it's held.
1916 */
1917 if (agbp == NULL) {
1918 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1919 &agbp)))
1920 return error;
1921 if (agbp == NULL) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001922 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1923 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 args->agbp = NULL;
1925 return 0;
1926 }
1927 }
1928 /*
1929 * Figure out how many blocks we should have in the freelist.
1930 */
1931 agf = XFS_BUF_TO_AGF(agbp);
1932 need = XFS_MIN_FREELIST(agf, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * If there isn't enough total or single-extent, reject it.
1935 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001936 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1937 delta = need > be32_to_cpu(agf->agf_flcount) ?
1938 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1939 longest = be32_to_cpu(agf->agf_longest);
1940 longest = (longest > delta) ? (longest - delta) :
1941 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1942 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1943 longest ||
1944 ((int)(be32_to_cpu(agf->agf_freeblks) +
1945 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1946 (int)args->minleft)) {
1947 xfs_trans_brelse(tp, agbp);
1948 args->agbp = NULL;
1949 return 0;
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 /*
1953 * Make the freelist shorter if it's too long.
1954 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001955 while (be32_to_cpu(agf->agf_flcount) > need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 xfs_buf_t *bp;
1957
David Chinner92821e22007-05-24 15:26:31 +10001958 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1959 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return error;
1961 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1962 return error;
1963 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1964 xfs_trans_binval(tp, bp);
1965 }
1966 /*
1967 * Initialize the args structure.
1968 */
Mark Tinguelya0041682012-09-20 13:16:45 -05001969 memset(&targs, 0, sizeof(targs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 targs.tp = tp;
1971 targs.mp = mp;
1972 targs.agbp = agbp;
1973 targs.agno = args->agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1975 targs.type = XFS_ALLOCTYPE_THIS_AG;
1976 targs.pag = pag;
1977 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1978 return error;
1979 /*
1980 * Make the freelist longer if it's too short.
1981 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001982 while (be32_to_cpu(agf->agf_flcount) < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 targs.agbno = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001984 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * Allocate as many blocks as possible at once.
1987 */
Nathan Scotte63a3692006-05-08 19:51:58 +10001988 if ((error = xfs_alloc_ag_vextent(&targs))) {
1989 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return error;
Nathan Scotte63a3692006-05-08 19:51:58 +10001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /*
1993 * Stop if we run out. Won't happen if callers are obeying
1994 * the restrictions correctly. Can happen for free calls
1995 * on a completely full ag.
1996 */
Yingping Lud210a282006-06-09 14:55:18 +10001997 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001998 if (flags & XFS_ALLOC_FLAG_FREEING)
1999 break;
2000 xfs_trans_brelse(tp, agflbp);
2001 args->agbp = NULL;
2002 return 0;
Yingping Lud210a282006-06-09 14:55:18 +10002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /*
2005 * Put each allocated block on the list.
2006 */
2007 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002008 error = xfs_alloc_put_freelist(tp, agbp,
2009 agflbp, bno, 0);
2010 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return error;
2012 }
2013 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002014 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 args->agbp = agbp;
2016 return 0;
2017}
2018
2019/*
2020 * Get a block from the freelist.
2021 * Returns with the buffer for the block gotten.
2022 */
2023int /* error */
2024xfs_alloc_get_freelist(
2025 xfs_trans_t *tp, /* transaction pointer */
2026 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002027 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2028 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 xfs_agf_t *agf; /* a.g. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2032 xfs_agblock_t bno; /* block number returned */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002033 __be32 *agfl_bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int error;
David Chinner92821e22007-05-24 15:26:31 +10002035 int logflags;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002036 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 xfs_perag_t *pag; /* per allocation group data */
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /*
2040 * Freelist is empty, give up.
2041 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002042 agf = XFS_BUF_TO_AGF(agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!agf->agf_flcount) {
2044 *bnop = NULLAGBLOCK;
2045 return 0;
2046 }
2047 /*
2048 * Read the array of free blocks.
2049 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002050 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2051 &agflbp);
2052 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return error;
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002054
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Get the block number and update the data structures.
2058 */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002059 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2060 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002061 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002063 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002065
2066 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002067 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 xfs_trans_agflist_delta(tp, -1);
2069 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002070 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002071
2072 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2073 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002074 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002075 pag->pagf_btreeblks++;
2076 logflags |= XFS_AGF_BTREEBLKS;
2077 }
2078
David Chinner92821e22007-05-24 15:26:31 +10002079 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 *bnop = bno;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
2084
2085/*
2086 * Log the given fields from the agf structure.
2087 */
2088void
2089xfs_alloc_log_agf(
2090 xfs_trans_t *tp, /* transaction pointer */
2091 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2092 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2093{
2094 int first; /* first byte offset */
2095 int last; /* last byte offset */
2096 static const short offsets[] = {
2097 offsetof(xfs_agf_t, agf_magicnum),
2098 offsetof(xfs_agf_t, agf_versionnum),
2099 offsetof(xfs_agf_t, agf_seqno),
2100 offsetof(xfs_agf_t, agf_length),
2101 offsetof(xfs_agf_t, agf_roots[0]),
2102 offsetof(xfs_agf_t, agf_levels[0]),
2103 offsetof(xfs_agf_t, agf_flfirst),
2104 offsetof(xfs_agf_t, agf_fllast),
2105 offsetof(xfs_agf_t, agf_flcount),
2106 offsetof(xfs_agf_t, agf_freeblks),
2107 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002108 offsetof(xfs_agf_t, agf_btreeblks),
Dave Chinner4e0e6042013-04-03 16:11:13 +11002109 offsetof(xfs_agf_t, agf_uuid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 sizeof(xfs_agf_t)
2111 };
2112
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002113 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2114
Dave Chinner61fe1352013-04-03 16:11:30 +11002115 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
Dave Chinner4e0e6042013-04-03 16:11:13 +11002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2118 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2119}
2120
2121/*
2122 * Interface for inode allocation to force the pag data to be initialized.
2123 */
2124int /* error */
2125xfs_alloc_pagf_init(
2126 xfs_mount_t *mp, /* file system mount structure */
2127 xfs_trans_t *tp, /* transaction pointer */
2128 xfs_agnumber_t agno, /* allocation group number */
2129 int flags) /* XFS_ALLOC_FLAGS_... */
2130{
2131 xfs_buf_t *bp;
2132 int error;
2133
2134 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2135 return error;
2136 if (bp)
2137 xfs_trans_brelse(tp, bp);
2138 return 0;
2139}
2140
2141/*
2142 * Put the block on the freelist for the allocation group.
2143 */
2144int /* error */
2145xfs_alloc_put_freelist(
2146 xfs_trans_t *tp, /* transaction pointer */
2147 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2148 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002149 xfs_agblock_t bno, /* block being freed */
2150 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 xfs_agf_t *agf; /* a.g. freespace structure */
Christoph Hellwige2101002006-09-28 10:56:51 +10002153 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int error;
David Chinner92821e22007-05-24 15:26:31 +10002155 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 xfs_mount_t *mp; /* mount structure */
2157 xfs_perag_t *pag; /* per allocation group data */
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002158 __be32 *agfl_bno;
2159 int startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 agf = XFS_BUF_TO_AGF(agbp);
2162 mp = tp->t_mountp;
2163
2164 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002165 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return error;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002167 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002168 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002170
2171 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002172 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 xfs_trans_agflist_delta(tp, 1);
2174 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002175
2176 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2177 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002178 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002179 pag->pagf_btreeblks--;
2180 logflags |= XFS_AGF_BTREEBLKS;
2181 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002182 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002183
David Chinner92821e22007-05-24 15:26:31 +10002184 xfs_alloc_log_agf(tp, agbp, logflags);
2185
Christoph Hellwig16259e72005-11-02 15:11:25 +11002186 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002187
2188 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2189 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002190 *blockp = cpu_to_be32(bno);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002191 startoff = (char *)blockp - (char *)agflbp->b_addr;
2192
David Chinner92821e22007-05-24 15:26:31 +10002193 xfs_alloc_log_agf(tp, agbp, logflags);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002194
Dave Chinner61fe1352013-04-03 16:11:30 +11002195 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +11002196 xfs_trans_log_buf(tp, agflbp, startoff,
2197 startoff + sizeof(xfs_agblock_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199}
2200
Dave Chinner4e0e6042013-04-03 16:11:13 +11002201static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002202xfs_agf_verify(
Dave Chinner4e0e6042013-04-03 16:11:13 +11002203 struct xfs_mount *mp,
Dave Chinner5d5f5272012-11-14 17:44:56 +11002204 struct xfs_buf *bp)
2205 {
Dave Chinner4e0e6042013-04-03 16:11:13 +11002206 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
Dave Chinner5d5f5272012-11-14 17:44:56 +11002207
Dave Chinner4e0e6042013-04-03 16:11:13 +11002208 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2209 !uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_uuid))
2210 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002211
Dave Chinner4e0e6042013-04-03 16:11:13 +11002212 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2213 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2214 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2215 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2216 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2217 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2218 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002219
2220 /*
2221 * during growfs operations, the perag is not fully initialised,
2222 * so we can't use it for any useful checking. growfs ensures we can't
2223 * use it by using uncached buffers that don't have the perag attached
2224 * so we can detect and avoid this problem.
2225 */
Dave Chinner4e0e6042013-04-03 16:11:13 +11002226 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2227 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002228
Dave Chinner4e0e6042013-04-03 16:11:13 +11002229 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2230 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2231 return false;
Dave Chinner5d5f5272012-11-14 17:44:56 +11002232
Dave Chinner4e0e6042013-04-03 16:11:13 +11002233 return true;;
2234
Dave Chinner612cfbf2012-11-14 17:52:32 +11002235}
Dave Chinner5d5f5272012-11-14 17:44:56 +11002236
Dave Chinner1813dd62012-11-14 17:54:40 +11002237static void
2238xfs_agf_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002239 struct xfs_buf *bp)
2240{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002241 struct xfs_mount *mp = bp->b_target->bt_mount;
2242 int agf_ok = 1;
2243
2244 if (xfs_sb_version_hascrc(&mp->m_sb))
2245 agf_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
2246 offsetof(struct xfs_agf, agf_crc));
2247
2248 agf_ok = agf_ok && xfs_agf_verify(mp, bp);
2249
2250 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2251 XFS_RANDOM_ALLOC_READ_AGF))) {
2252 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2253 xfs_buf_ioerror(bp, EFSCORRUPTED);
2254 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11002255}
2256
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002257static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002258xfs_agf_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002259 struct xfs_buf *bp)
2260{
Dave Chinner4e0e6042013-04-03 16:11:13 +11002261 struct xfs_mount *mp = bp->b_target->bt_mount;
2262 struct xfs_buf_log_item *bip = bp->b_fspriv;
2263
2264 if (!xfs_agf_verify(mp, bp)) {
2265 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
2266 xfs_buf_ioerror(bp, EFSCORRUPTED);
2267 return;
2268 }
2269
2270 if (!xfs_sb_version_hascrc(&mp->m_sb))
2271 return;
2272
2273 if (bip)
2274 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2275
2276 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
2277 offsetof(struct xfs_agf, agf_crc));
Dave Chinner5d5f5272012-11-14 17:44:56 +11002278}
2279
Dave Chinner1813dd62012-11-14 17:54:40 +11002280const struct xfs_buf_ops xfs_agf_buf_ops = {
2281 .verify_read = xfs_agf_read_verify,
2282 .verify_write = xfs_agf_write_verify,
2283};
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285/*
2286 * Read in the allocation group header (free/alloc section).
2287 */
2288int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002289xfs_read_agf(
2290 struct xfs_mount *mp, /* mount point structure */
2291 struct xfs_trans *tp, /* transaction pointer */
2292 xfs_agnumber_t agno, /* allocation group number */
2293 int flags, /* XFS_BUF_ */
2294 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 int error;
2297
2298 ASSERT(agno != NULLAGNUMBER);
2299 error = xfs_trans_read_buf(
2300 mp, tp, mp->m_ddev_targp,
2301 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002302 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (error)
2304 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002305 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002307
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002308 ASSERT(!(*bpp)->b_error);
Christoph Hellwig38f23232011-10-10 16:52:45 +00002309 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002310 return 0;
2311}
2312
2313/*
2314 * Read in the allocation group header (free/alloc section).
2315 */
2316int /* error */
2317xfs_alloc_read_agf(
2318 struct xfs_mount *mp, /* mount point structure */
2319 struct xfs_trans *tp, /* transaction pointer */
2320 xfs_agnumber_t agno, /* allocation group number */
2321 int flags, /* XFS_ALLOC_FLAG_... */
2322 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2323{
2324 struct xfs_agf *agf; /* ag freelist header */
2325 struct xfs_perag *pag; /* per allocation group data */
2326 int error;
2327
2328 ASSERT(agno != NULLAGNUMBER);
2329
2330 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002331 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002332 bpp);
2333 if (error)
2334 return error;
2335 if (!*bpp)
2336 return 0;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002337 ASSERT(!(*bpp)->b_error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002338
2339 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002340 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002342 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002343 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002344 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2345 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002347 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002349 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002350 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002351 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002352 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 pag->pagf_init = 1;
2354 }
2355#ifdef DEBUG
2356 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002357 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002358 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002359 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2360 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002362 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002364 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002367 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return 0;
2369}
2370
2371/*
2372 * Allocate an extent (variable-size).
2373 * Depending on the allocation type, we either look in a single allocation
2374 * group or loop over the allocation groups to find the result.
2375 */
2376int /* error */
Dave Chinnere04426b2012-10-05 11:06:59 +10002377xfs_alloc_vextent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 xfs_alloc_arg_t *args) /* allocation argument structure */
2379{
2380 xfs_agblock_t agsize; /* allocation group size */
2381 int error;
2382 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 xfs_extlen_t minleft;/* minimum left value, temp copy */
2384 xfs_mount_t *mp; /* mount structure pointer */
2385 xfs_agnumber_t sagno; /* starting allocation group number */
2386 xfs_alloctype_t type; /* input allocation type */
2387 int bump_rotor = 0;
2388 int no_min = 0;
2389 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2390
2391 mp = args->mp;
2392 type = args->otype = args->type;
2393 args->agbno = NULLAGBLOCK;
2394 /*
2395 * Just fix this up, for the case where the last a.g. is shorter
2396 * (or there's only one a.g.) and the caller couldn't easily figure
2397 * that out (xfs_bmap_alloc).
2398 */
2399 agsize = mp->m_sb.sb_agblocks;
2400 if (args->maxlen > agsize)
2401 args->maxlen = agsize;
2402 if (args->alignment == 0)
2403 args->alignment = 1;
2404 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2405 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2406 ASSERT(args->minlen <= args->maxlen);
2407 ASSERT(args->minlen <= agsize);
2408 ASSERT(args->mod < args->prod);
2409 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2410 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2411 args->minlen > args->maxlen || args->minlen > agsize ||
2412 args->mod >= args->prod) {
2413 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002414 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return 0;
2416 }
2417 minleft = args->minleft;
2418
2419 switch (type) {
2420 case XFS_ALLOCTYPE_THIS_AG:
2421 case XFS_ALLOCTYPE_NEAR_BNO:
2422 case XFS_ALLOCTYPE_THIS_BNO:
2423 /*
2424 * These three force us into a single a.g.
2425 */
2426 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002427 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 args->minleft = 0;
2429 error = xfs_alloc_fix_freelist(args, 0);
2430 args->minleft = minleft;
2431 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002432 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto error0;
2434 }
2435 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002436 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 break;
2438 }
2439 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2440 if ((error = xfs_alloc_ag_vextent(args)))
2441 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 break;
2443 case XFS_ALLOCTYPE_START_BNO:
2444 /*
2445 * Try near allocation first, then anywhere-in-ag after
2446 * the first a.g. fails.
2447 */
2448 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2449 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2450 args->fsbno = XFS_AGB_TO_FSB(mp,
2451 ((mp->m_agfrotor / rotorstep) %
2452 mp->m_sb.sb_agcount), 0);
2453 bump_rotor = 1;
2454 }
2455 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2456 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2457 /* FALLTHROUGH */
2458 case XFS_ALLOCTYPE_ANY_AG:
2459 case XFS_ALLOCTYPE_START_AG:
2460 case XFS_ALLOCTYPE_FIRST_AG:
2461 /*
2462 * Rotate through the allocation groups looking for a winner.
2463 */
2464 if (type == XFS_ALLOCTYPE_ANY_AG) {
2465 /*
2466 * Start with the last place we left off.
2467 */
2468 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2469 mp->m_sb.sb_agcount;
2470 args->type = XFS_ALLOCTYPE_THIS_AG;
2471 flags = XFS_ALLOC_FLAG_TRYLOCK;
2472 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2473 /*
2474 * Start with allocation group given by bno.
2475 */
2476 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2477 args->type = XFS_ALLOCTYPE_THIS_AG;
2478 sagno = 0;
2479 flags = 0;
2480 } else {
2481 if (type == XFS_ALLOCTYPE_START_AG)
2482 args->type = XFS_ALLOCTYPE_THIS_AG;
2483 /*
2484 * Start with the given allocation group.
2485 */
2486 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2487 flags = XFS_ALLOC_FLAG_TRYLOCK;
2488 }
2489 /*
2490 * Loop over allocation groups twice; first time with
2491 * trylock set, second time without.
2492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002494 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (no_min) args->minleft = 0;
2496 error = xfs_alloc_fix_freelist(args, flags);
2497 args->minleft = minleft;
2498 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002499 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto error0;
2501 }
2502 /*
2503 * If we get a buffer back then the allocation will fly.
2504 */
2505 if (args->agbp) {
2506 if ((error = xfs_alloc_ag_vextent(args)))
2507 goto error0;
2508 break;
2509 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002510
2511 trace_xfs_alloc_vextent_loopfailed(args);
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /*
2514 * Didn't work, figure out the next iteration.
2515 */
2516 if (args->agno == sagno &&
2517 type == XFS_ALLOCTYPE_START_BNO)
2518 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002519 /*
2520 * For the first allocation, we can try any AG to get
2521 * space. However, if we already have allocated a
2522 * block, we don't want to try AGs whose number is below
2523 * sagno. Otherwise, we may end up with out-of-order
2524 * locking of AGF, which might cause deadlock.
2525 */
2526 if (++(args->agno) == mp->m_sb.sb_agcount) {
2527 if (args->firstblock != NULLFSBLOCK)
2528 args->agno = sagno;
2529 else
2530 args->agno = 0;
2531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /*
2533 * Reached the starting a.g., must either be done
2534 * or switch to non-trylock mode.
2535 */
2536 if (args->agno == sagno) {
2537 if (no_min == 1) {
2538 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002539 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 break;
2541 }
2542 if (flags == 0) {
2543 no_min = 1;
2544 } else {
2545 flags = 0;
2546 if (type == XFS_ALLOCTYPE_START_BNO) {
2547 args->agbno = XFS_FSB_TO_AGBNO(mp,
2548 args->fsbno);
2549 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2550 }
2551 }
2552 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002553 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2556 if (args->agno == sagno)
2557 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2558 (mp->m_sb.sb_agcount * rotorstep);
2559 else
2560 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2561 (mp->m_sb.sb_agcount * rotorstep);
2562 }
2563 break;
2564 default:
2565 ASSERT(0);
2566 /* NOTREACHED */
2567 }
2568 if (args->agbno == NULLAGBLOCK)
2569 args->fsbno = NULLFSBLOCK;
2570 else {
2571 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2572#ifdef DEBUG
2573 ASSERT(args->len >= args->minlen);
2574 ASSERT(args->len <= args->maxlen);
2575 ASSERT(args->agbno % args->alignment == 0);
2576 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2577 args->len);
2578#endif
2579 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002580 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 0;
2582error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002583 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return error;
2585}
2586
2587/*
2588 * Free an extent.
2589 * Just break up the extent address and hand off to xfs_free_ag_extent
2590 * after fixing up the freelist.
2591 */
2592int /* error */
2593xfs_free_extent(
2594 xfs_trans_t *tp, /* transaction pointer */
2595 xfs_fsblock_t bno, /* starting block number of extent */
2596 xfs_extlen_t len) /* length of extent */
2597{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002598 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 int error;
2600
2601 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002602 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 args.tp = tp;
2604 args.mp = tp->t_mountp;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002605
2606 /*
2607 * validate that the block number is legal - the enables us to detect
2608 * and handle a silent filesystem corruption rather than crashing.
2609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002611 if (args.agno >= args.mp->m_sb.sb_agcount)
2612 return EFSCORRUPTED;
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002615 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2616 return EFSCORRUPTED;
2617
Dave Chinnera862e0f2010-01-11 11:47:41 +00002618 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002619 ASSERT(args.pag);
2620
2621 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2622 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 goto error0;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002624
2625 /* validate the extent size is legal now we have the agf locked */
2626 if (args.agbno + len >
2627 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2628 error = EFSCORRUPTED;
2629 goto error0;
2630 }
2631
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002632 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Christoph Hellwiga870acd2011-04-24 19:06:14 +00002633 if (!error)
Dave Chinner4ecbfe62012-04-29 10:41:10 +00002634 xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002636 xfs_perag_put(args.pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return error;
2638}