blob: 37c4952c2f53a4b0fb26d49e778c91a630d99829 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
39#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
40
41#define XFSA_FIXUP_BNO_OK 1
42#define XFSA_FIXUP_CNT_OK 2
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Prototypes for per-ag allocation routines
46 */
47
48STATIC 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 *,
52 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
53
54/*
55 * Internal functions.
56 */
57
58/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110059 * Lookup the record equal to [bno, len] in the btree given by cur.
60 */
61STATIC int /* error */
62xfs_alloc_lookup_eq(
63 struct xfs_btree_cur *cur, /* btree cursor */
64 xfs_agblock_t bno, /* starting block of extent */
65 xfs_extlen_t len, /* length of extent */
66 int *stat) /* success/failure */
67{
68 cur->bc_rec.a.ar_startblock = bno;
69 cur->bc_rec.a.ar_blockcount = len;
70 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
71}
72
73/*
74 * Lookup the first record greater than or equal to [bno, len]
75 * in the btree given by cur.
76 */
77STATIC int /* error */
78xfs_alloc_lookup_ge(
79 struct xfs_btree_cur *cur, /* btree cursor */
80 xfs_agblock_t bno, /* starting block of extent */
81 xfs_extlen_t len, /* length of extent */
82 int *stat) /* success/failure */
83{
84 cur->bc_rec.a.ar_startblock = bno;
85 cur->bc_rec.a.ar_blockcount = len;
86 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
87}
88
89/*
90 * Lookup the first record less than or equal to [bno, len]
91 * in the btree given by cur.
92 */
Christoph Hellwiga46db602011-01-07 13:02:04 +000093int /* error */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110094xfs_alloc_lookup_le(
95 struct xfs_btree_cur *cur, /* btree cursor */
96 xfs_agblock_t bno, /* starting block of extent */
97 xfs_extlen_t len, /* length of extent */
98 int *stat) /* success/failure */
99{
100 cur->bc_rec.a.ar_startblock = bno;
101 cur->bc_rec.a.ar_blockcount = len;
102 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
103}
104
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100105/*
106 * Update the record referred to by cur to the value given
107 * by [bno, len].
108 * This either works (return 0) or gets an EFSCORRUPTED error.
109 */
110STATIC int /* error */
111xfs_alloc_update(
112 struct xfs_btree_cur *cur, /* btree cursor */
113 xfs_agblock_t bno, /* starting block of extent */
114 xfs_extlen_t len) /* length of extent */
115{
116 union xfs_btree_rec rec;
117
118 rec.alloc.ar_startblock = cpu_to_be32(bno);
119 rec.alloc.ar_blockcount = cpu_to_be32(len);
120 return xfs_btree_update(cur, &rec);
121}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100122
123/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100124 * Get the data from the pointed-to record.
125 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000126int /* error */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100127xfs_alloc_get_rec(
128 struct xfs_btree_cur *cur, /* btree cursor */
129 xfs_agblock_t *bno, /* output: starting block of extent */
130 xfs_extlen_t *len, /* output: length of extent */
131 int *stat) /* output: success/failure */
132{
133 union xfs_btree_rec *rec;
134 int error;
135
136 error = xfs_btree_get_rec(cur, &rec, stat);
137 if (!error && *stat == 1) {
138 *bno = be32_to_cpu(rec->alloc.ar_startblock);
139 *len = be32_to_cpu(rec->alloc.ar_blockcount);
140 }
141 return error;
142}
143
144/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * Compute aligned version of the found extent.
146 * Takes alignment and min length into account.
147 */
David Chinner12375c82008-04-10 12:21:32 +1000148STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149xfs_alloc_compute_aligned(
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000150 xfs_alloc_arg_t *args, /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xfs_agblock_t foundbno, /* starting block in found extent */
152 xfs_extlen_t foundlen, /* length in found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 xfs_agblock_t *resbno, /* result block number */
154 xfs_extlen_t *reslen) /* result length */
155{
156 xfs_agblock_t bno;
157 xfs_extlen_t diff;
158 xfs_extlen_t len;
159
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000160 if (args->alignment > 1 && foundlen >= args->minlen) {
161 bno = roundup(foundbno, args->alignment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 diff = bno - foundbno;
163 len = diff >= foundlen ? 0 : foundlen - diff;
164 } else {
165 bno = foundbno;
166 len = foundlen;
167 }
168 *resbno = bno;
169 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * Compute best start block and diff for "near" allocations.
174 * freelen >= wantlen already checked by caller.
175 */
176STATIC xfs_extlen_t /* difference value (absolute) */
177xfs_alloc_compute_diff(
178 xfs_agblock_t wantbno, /* target starting block */
179 xfs_extlen_t wantlen, /* target length */
180 xfs_extlen_t alignment, /* target alignment */
181 xfs_agblock_t freebno, /* freespace's starting block */
182 xfs_extlen_t freelen, /* freespace's length */
183 xfs_agblock_t *newbnop) /* result: best start block from free */
184{
185 xfs_agblock_t freeend; /* end of freespace extent */
186 xfs_agblock_t newbno1; /* return block number */
187 xfs_agblock_t newbno2; /* other new block number */
188 xfs_extlen_t newlen1=0; /* length with newbno1 */
189 xfs_extlen_t newlen2=0; /* length with newbno2 */
190 xfs_agblock_t wantend; /* end of target extent */
191
192 ASSERT(freelen >= wantlen);
193 freeend = freebno + freelen;
194 wantend = wantbno + wantlen;
195 if (freebno >= wantbno) {
196 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
197 newbno1 = NULLAGBLOCK;
198 } else if (freeend >= wantend && alignment > 1) {
199 newbno1 = roundup(wantbno, alignment);
200 newbno2 = newbno1 - alignment;
201 if (newbno1 >= freeend)
202 newbno1 = NULLAGBLOCK;
203 else
204 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
205 if (newbno2 < freebno)
206 newbno2 = NULLAGBLOCK;
207 else
208 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
209 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
210 if (newlen1 < newlen2 ||
211 (newlen1 == newlen2 &&
212 XFS_ABSDIFF(newbno1, wantbno) >
213 XFS_ABSDIFF(newbno2, wantbno)))
214 newbno1 = newbno2;
215 } else if (newbno2 != NULLAGBLOCK)
216 newbno1 = newbno2;
217 } else if (freeend >= wantend) {
218 newbno1 = wantbno;
219 } else if (alignment > 1) {
220 newbno1 = roundup(freeend - wantlen, alignment);
221 if (newbno1 > freeend - wantlen &&
222 newbno1 - alignment >= freebno)
223 newbno1 -= alignment;
224 else if (newbno1 >= freeend)
225 newbno1 = NULLAGBLOCK;
226 } else
227 newbno1 = freeend - wantlen;
228 *newbnop = newbno1;
229 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
230}
231
232/*
233 * Fix up the length, based on mod and prod.
234 * len should be k * prod + mod for some k.
235 * If len is too small it is returned unchanged.
236 * If len hits maxlen it is left alone.
237 */
238STATIC void
239xfs_alloc_fix_len(
240 xfs_alloc_arg_t *args) /* allocation argument structure */
241{
242 xfs_extlen_t k;
243 xfs_extlen_t rlen;
244
245 ASSERT(args->mod < args->prod);
246 rlen = args->len;
247 ASSERT(rlen >= args->minlen);
248 ASSERT(rlen <= args->maxlen);
249 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
250 (args->mod == 0 && rlen < args->prod))
251 return;
252 k = rlen % args->prod;
253 if (k == args->mod)
254 return;
255 if (k > args->mod) {
256 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
257 return;
258 } else {
259 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
260 (int)args->minlen)
261 return;
262 }
263 ASSERT(rlen >= args->minlen);
264 ASSERT(rlen <= args->maxlen);
265 args->len = rlen;
266}
267
268/*
269 * Fix up length if there is too little space left in the a.g.
270 * Return 1 if ok, 0 if too little, should give up.
271 */
272STATIC int
273xfs_alloc_fix_minleft(
274 xfs_alloc_arg_t *args) /* allocation argument structure */
275{
276 xfs_agf_t *agf; /* a.g. freelist header */
277 int diff; /* free space difference */
278
279 if (args->minleft == 0)
280 return 1;
281 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100282 diff = be32_to_cpu(agf->agf_freeblks)
283 + be32_to_cpu(agf->agf_flcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 - args->len - args->minleft;
285 if (diff >= 0)
286 return 1;
287 args->len += diff; /* shrink the allocated space */
288 if (args->len >= args->minlen)
289 return 1;
290 args->agbno = NULLAGBLOCK;
291 return 0;
292}
293
294/*
295 * Update the two btrees, logically removing from freespace the extent
296 * starting at rbno, rlen blocks. The extent is contained within the
297 * actual (current) free extent fbno for flen blocks.
298 * Flags are passed in indicating whether the cursors are set to the
299 * relevant records.
300 */
301STATIC int /* error code */
302xfs_alloc_fixup_trees(
303 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
304 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
305 xfs_agblock_t fbno, /* starting block of free extent */
306 xfs_extlen_t flen, /* length of free extent */
307 xfs_agblock_t rbno, /* starting block of returned extent */
308 xfs_extlen_t rlen, /* length of returned extent */
309 int flags) /* flags, XFSA_FIXUP_... */
310{
311 int error; /* error code */
312 int i; /* operation results */
313 xfs_agblock_t nfbno1; /* first new free startblock */
314 xfs_agblock_t nfbno2; /* second new free startblock */
315 xfs_extlen_t nflen1=0; /* first new free length */
316 xfs_extlen_t nflen2=0; /* second new free length */
317
318 /*
319 * Look up the record in the by-size tree if necessary.
320 */
321 if (flags & XFSA_FIXUP_CNT_OK) {
322#ifdef DEBUG
323 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
324 return error;
325 XFS_WANT_CORRUPTED_RETURN(
326 i == 1 && nfbno1 == fbno && nflen1 == flen);
327#endif
328 } else {
329 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
330 return error;
331 XFS_WANT_CORRUPTED_RETURN(i == 1);
332 }
333 /*
334 * Look up the record in the by-block tree if necessary.
335 */
336 if (flags & XFSA_FIXUP_BNO_OK) {
337#ifdef DEBUG
338 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
339 return error;
340 XFS_WANT_CORRUPTED_RETURN(
341 i == 1 && nfbno1 == fbno && nflen1 == flen);
342#endif
343 } else {
344 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
345 return error;
346 XFS_WANT_CORRUPTED_RETURN(i == 1);
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100349#ifdef DEBUG
350 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
351 struct xfs_btree_block *bnoblock;
352 struct xfs_btree_block *cntblock;
353
354 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
355 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
356
357 XFS_WANT_CORRUPTED_RETURN(
358 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * Deal with all four cases: the allocated record is contained
364 * within the freespace record, so we can have new freespace
365 * at either (or both) end, or no freespace remaining.
366 */
367 if (rbno == fbno && rlen == flen)
368 nfbno1 = nfbno2 = NULLAGBLOCK;
369 else if (rbno == fbno) {
370 nfbno1 = rbno + rlen;
371 nflen1 = flen - rlen;
372 nfbno2 = NULLAGBLOCK;
373 } else if (rbno + rlen == fbno + flen) {
374 nfbno1 = fbno;
375 nflen1 = flen - rlen;
376 nfbno2 = NULLAGBLOCK;
377 } else {
378 nfbno1 = fbno;
379 nflen1 = rbno - fbno;
380 nfbno2 = rbno + rlen;
381 nflen2 = (fbno + flen) - nfbno2;
382 }
383 /*
384 * Delete the entry from the by-size btree.
385 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100386 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return error;
388 XFS_WANT_CORRUPTED_RETURN(i == 1);
389 /*
390 * Add new by-size btree entry(s).
391 */
392 if (nfbno1 != NULLAGBLOCK) {
393 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
394 return error;
395 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100396 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return error;
398 XFS_WANT_CORRUPTED_RETURN(i == 1);
399 }
400 if (nfbno2 != NULLAGBLOCK) {
401 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
402 return error;
403 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100404 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return error;
406 XFS_WANT_CORRUPTED_RETURN(i == 1);
407 }
408 /*
409 * Fix up the by-block btree entry(s).
410 */
411 if (nfbno1 == NULLAGBLOCK) {
412 /*
413 * No remaining freespace, just delete the by-block tree entry.
414 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100415 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return error;
417 XFS_WANT_CORRUPTED_RETURN(i == 1);
418 } else {
419 /*
420 * Update the by-block entry to start later|be shorter.
421 */
422 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
423 return error;
424 }
425 if (nfbno2 != NULLAGBLOCK) {
426 /*
427 * 2 resulting free entries, need to add one.
428 */
429 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
430 return error;
431 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100432 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return error;
434 XFS_WANT_CORRUPTED_RETURN(i == 1);
435 }
436 return 0;
437}
438
439/*
440 * Read in the allocation group free block array.
441 */
442STATIC int /* error */
443xfs_alloc_read_agfl(
444 xfs_mount_t *mp, /* mount point structure */
445 xfs_trans_t *tp, /* transaction pointer */
446 xfs_agnumber_t agno, /* allocation group number */
447 xfs_buf_t **bpp) /* buffer for the ag free block array */
448{
449 xfs_buf_t *bp; /* return value */
450 int error;
451
452 ASSERT(agno != NULLAGNUMBER);
453 error = xfs_trans_read_buf(
454 mp, tp, mp->m_ddev_targp,
455 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
456 XFS_FSS_TO_BB(mp, 1), 0, &bp);
457 if (error)
458 return error;
459 ASSERT(bp);
460 ASSERT(!XFS_BUF_GETERROR(bp));
461 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
462 *bpp = bp;
463 return 0;
464}
465
Christoph Hellwigecb69282011-03-04 12:59:55 +0000466STATIC int
467xfs_alloc_update_counters(
468 struct xfs_trans *tp,
469 struct xfs_perag *pag,
470 struct xfs_buf *agbp,
471 long len)
472{
473 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
474
475 pag->pagf_freeblks += len;
476 be32_add_cpu(&agf->agf_freeblks, len);
477
478 xfs_trans_agblocks_delta(tp, len);
479 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
480 be32_to_cpu(agf->agf_length)))
481 return EFSCORRUPTED;
482
483 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
484 return 0;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * Allocation group level functions.
489 */
490
491/*
492 * Allocate a variable extent in the allocation group agno.
493 * Type and bno are used to determine where in the allocation group the
494 * extent will start.
495 * Extent's length (returned in *len) will be between minlen and maxlen,
496 * and of the form k * prod + mod unless there's nothing that large.
497 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
498 */
499STATIC int /* error */
500xfs_alloc_ag_vextent(
501 xfs_alloc_arg_t *args) /* argument structure for allocation */
502{
503 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 ASSERT(args->minlen > 0);
506 ASSERT(args->maxlen > 0);
507 ASSERT(args->minlen <= args->maxlen);
508 ASSERT(args->mod < args->prod);
509 ASSERT(args->alignment > 0);
510 /*
511 * Branch to correct routine based on the type.
512 */
513 args->wasfromfl = 0;
514 switch (args->type) {
515 case XFS_ALLOCTYPE_THIS_AG:
516 error = xfs_alloc_ag_vextent_size(args);
517 break;
518 case XFS_ALLOCTYPE_NEAR_BNO:
519 error = xfs_alloc_ag_vextent_near(args);
520 break;
521 case XFS_ALLOCTYPE_THIS_BNO:
522 error = xfs_alloc_ag_vextent_exact(args);
523 break;
524 default:
525 ASSERT(0);
526 /* NOTREACHED */
527 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000528
529 if (error || args->agbno == NULLAGBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Christoph Hellwigecb69282011-03-04 12:59:55 +0000532 ASSERT(args->len >= args->minlen);
533 ASSERT(args->len <= args->maxlen);
534 ASSERT(!args->wasfromfl || !args->isfl);
535 ASSERT(args->agbno % args->alignment == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Christoph Hellwigecb69282011-03-04 12:59:55 +0000537 if (!args->wasfromfl) {
538 error = xfs_alloc_update_counters(args->tp, args->pag,
539 args->agbp,
540 -((long)(args->len)));
541 if (error)
542 return error;
543
544 /*
545 * Search the busylist for these blocks and mark the
546 * transaction as synchronous if blocks are found. This
547 * avoids the need to block due to a synchronous log
548 * force to ensure correct ordering as the synchronous
549 * transaction will guarantee that for us.
550 */
551 if (xfs_alloc_busy_search(args->mp, args->agno,
552 args->agbno, args->len))
553 xfs_trans_set_sync(args->tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Christoph Hellwigecb69282011-03-04 12:59:55 +0000555
556 if (!args->isfl) {
557 xfs_trans_mod_sb(args->tp, args->wasdel ?
558 XFS_TRANS_SB_RES_FDBLOCKS :
559 XFS_TRANS_SB_FDBLOCKS,
560 -((long)(args->len)));
561 }
562
563 XFS_STATS_INC(xs_allocx);
564 XFS_STATS_ADD(xs_allocb, args->len);
565 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
569 * Allocate a variable extent at exactly agno/bno.
570 * Extent's length (returned in *len) will be between minlen and maxlen,
571 * and of the form k * prod + mod unless there's nothing that large.
572 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
573 */
574STATIC int /* error */
575xfs_alloc_ag_vextent_exact(
576 xfs_alloc_arg_t *args) /* allocation argument structure */
577{
578 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
579 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
580 xfs_agblock_t end; /* end of allocated extent */
581 int error;
582 xfs_agblock_t fbno; /* start block of found extent */
583 xfs_agblock_t fend; /* end block of found extent */
584 xfs_extlen_t flen; /* length of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int i; /* success/failure of operation */
586 xfs_agblock_t maxend; /* end of maximal extent */
587 xfs_agblock_t minend; /* end of minimal extent */
588 xfs_extlen_t rlen; /* length of returned extent */
589
590 ASSERT(args->alignment == 1);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * Allocate/initialize a cursor for the by-number freespace btree.
594 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100595 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000596 args->agno, XFS_BTNUM_BNO);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
600 * Look for the closest free block <= bno, it must contain bno
601 * if any free block does.
602 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000603 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
604 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto error0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000606 if (!i)
607 goto not_found;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * Grab the freespace record.
611 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000612 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
613 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto error0;
615 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
616 ASSERT(fbno <= args->agbno);
617 minend = args->agbno + args->minlen;
618 maxend = args->agbno + args->maxlen;
619 fend = fbno + flen;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Give up if the freespace isn't long enough for the minimum request.
623 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000624 if (fend < minend)
625 goto not_found;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * End of extent will be smaller of the freespace end and the
629 * maximal requested end.
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Fix the length according to mod and prod if given.
632 */
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000633 end = XFS_AGBLOCK_MIN(fend, maxend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 args->len = end - args->agbno;
635 xfs_alloc_fix_len(args);
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000636 if (!xfs_alloc_fix_minleft(args))
637 goto not_found;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 rlen = args->len;
640 ASSERT(args->agbno + rlen <= fend);
641 end = args->agbno + rlen;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * We are allocating agbno for rlen [agbno .. end]
645 * Allocate/initialize a cursor for the by-size btree.
646 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100647 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
648 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100650 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000651 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
652 args->len, XFSA_FIXUP_BNO_OK);
653 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
655 goto error0;
656 }
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
659 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 args->wasfromfl = 0;
Christoph Hellwig9f9baab2010-12-10 15:03:57 +0000662 trace_xfs_alloc_exact_done(args);
663 return 0;
664
665not_found:
666 /* Didn't find it, return null. */
667 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
668 args->agbno = NULLAGBLOCK;
669 trace_xfs_alloc_exact_notfound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671
672error0:
673 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000674 trace_xfs_alloc_exact_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return error;
676}
677
678/*
Christoph Hellwig489a1502010-12-10 15:04:11 +0000679 * Search the btree in a given direction via the search cursor and compare
680 * the records found against the good extent we've already found.
681 */
682STATIC int
683xfs_alloc_find_best_extent(
684 struct xfs_alloc_arg *args, /* allocation argument structure */
685 struct xfs_btree_cur **gcur, /* good cursor */
686 struct xfs_btree_cur **scur, /* searching cursor */
687 xfs_agblock_t gdiff, /* difference for search comparison */
688 xfs_agblock_t *sbno, /* extent found by search */
689 xfs_extlen_t *slen,
690 xfs_extlen_t *slena, /* aligned length */
691 int dir) /* 0 = search right, 1 = search left */
692{
693 xfs_agblock_t bno;
694 xfs_agblock_t new;
695 xfs_agblock_t sdiff;
696 int error;
697 int i;
698
699 /* The good extent is perfect, no need to search. */
700 if (!gdiff)
701 goto out_use_good;
702
703 /*
704 * Look until we find a better one, run out of space or run off the end.
705 */
706 do {
707 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
708 if (error)
709 goto error0;
710 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000711 xfs_alloc_compute_aligned(args, *sbno, *slen, &bno, slena);
Christoph Hellwig489a1502010-12-10 15:04:11 +0000712
713 /*
714 * The good extent is closer than this one.
715 */
716 if (!dir) {
717 if (bno >= args->agbno + gdiff)
718 goto out_use_good;
719 } else {
720 if (bno <= args->agbno - gdiff)
721 goto out_use_good;
722 }
723
724 /*
725 * Same distance, compare length and pick the best.
726 */
727 if (*slena >= args->minlen) {
728 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
729 xfs_alloc_fix_len(args);
730
731 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
732 args->alignment, *sbno,
733 *slen, &new);
734
735 /*
736 * Choose closer size and invalidate other cursor.
737 */
738 if (sdiff < gdiff)
739 goto out_use_search;
740 goto out_use_good;
741 }
742
743 if (!dir)
744 error = xfs_btree_increment(*scur, 0, &i);
745 else
746 error = xfs_btree_decrement(*scur, 0, &i);
747 if (error)
748 goto error0;
749 } while (i);
750
751out_use_good:
752 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
753 *scur = NULL;
754 return 0;
755
756out_use_search:
757 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
758 *gcur = NULL;
759 return 0;
760
761error0:
762 /* caller invalidates cursors */
763 return error;
764}
765
766/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * Allocate a variable extent near bno in the allocation group agno.
768 * Extent's length (returned in len) will be between minlen and maxlen,
769 * and of the form k * prod + mod unless there's nothing that large.
770 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
771 */
772STATIC int /* error */
773xfs_alloc_ag_vextent_near(
774 xfs_alloc_arg_t *args) /* allocation argument structure */
775{
776 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
777 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
778 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_agblock_t gtbno; /* start bno of right side entry */
780 xfs_agblock_t gtbnoa; /* aligned ... */
781 xfs_extlen_t gtdiff; /* difference to right side entry */
782 xfs_extlen_t gtlen; /* length of right side entry */
Poyo VL9c169912010-09-02 07:41:55 +0000783 xfs_extlen_t gtlena = 0; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_agblock_t gtnew; /* useful start bno of right side */
785 int error; /* error code */
786 int i; /* result code, temporary */
787 int j; /* result code, temporary */
788 xfs_agblock_t ltbno; /* start bno of left side entry */
789 xfs_agblock_t ltbnoa; /* aligned ... */
790 xfs_extlen_t ltdiff; /* difference to left side entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 xfs_extlen_t ltlen; /* length of left side entry */
Poyo VL9c169912010-09-02 07:41:55 +0000792 xfs_extlen_t ltlena = 0; /* aligned ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 xfs_agblock_t ltnew; /* useful start bno of left side */
794 xfs_extlen_t rlen; /* length of returned extent */
795#if defined(DEBUG) && defined(__KERNEL__)
796 /*
797 * Randomly don't execute the first algorithm.
798 */
799 int dofirst; /* set to do first algorithm */
800
Joe Perchese7a23a92007-05-08 13:49:03 +1000801 dofirst = random32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#endif
803 /*
804 * Get a cursor for the by-size btree.
805 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100806 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
807 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ltlen = 0;
809 bno_cur_lt = bno_cur_gt = NULL;
810 /*
811 * See if there are any free extents as big as maxlen.
812 */
813 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
814 goto error0;
815 /*
816 * If none, then pick up the last entry in the tree unless the
817 * tree is empty.
818 */
819 if (!i) {
820 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
821 &ltlen, &i)))
822 goto error0;
823 if (i == 0 || ltlen == 0) {
824 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
825 return 0;
826 }
827 ASSERT(i == 1);
828 }
829 args->wasfromfl = 0;
830 /*
831 * First algorithm.
832 * If the requested extent is large wrt the freespaces available
833 * in this a.g., then the cursor will be pointing to a btree entry
834 * near the right edge of the tree. If it's in the last btree leaf
835 * block, then we just examine all the entries in that block
836 * that are big enough, and pick the best one.
837 * This is written as a while loop so we can break out of it,
838 * but we never loop back to the top.
839 */
840 while (xfs_btree_islastblock(cnt_cur, 0)) {
841 xfs_extlen_t bdiff;
842 int besti=0;
843 xfs_extlen_t blen=0;
844 xfs_agblock_t bnew=0;
845
846#if defined(DEBUG) && defined(__KERNEL__)
847 if (!dofirst)
848 break;
849#endif
850 /*
851 * Start from the entry that lookup found, sequence through
852 * all larger free blocks. If we're actually pointing at a
853 * record smaller than maxlen, go to the start of this block,
854 * and skip all those smaller than minlen.
855 */
856 if (ltlen || args->alignment > 1) {
857 cnt_cur->bc_ptrs[0] = 1;
858 do {
859 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
860 &ltlen, &i)))
861 goto error0;
862 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
863 if (ltlen >= args->minlen)
864 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100865 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto error0;
867 } while (i);
868 ASSERT(ltlen >= args->minlen);
869 if (!i)
870 break;
871 }
872 i = cnt_cur->bc_ptrs[0];
873 for (j = 1, blen = 0, bdiff = 0;
874 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100875 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /*
877 * For each entry, decide if it's better than
878 * the previous best entry.
879 */
880 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
881 goto error0;
882 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +0000883 xfs_alloc_compute_aligned(args, ltbno, ltlen,
884 &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +1000885 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 continue;
887 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
888 xfs_alloc_fix_len(args);
889 ASSERT(args->len >= args->minlen);
890 if (args->len < blen)
891 continue;
892 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
893 args->alignment, ltbno, ltlen, &ltnew);
894 if (ltnew != NULLAGBLOCK &&
895 (args->len > blen || ltdiff < bdiff)) {
896 bdiff = ltdiff;
897 bnew = ltnew;
898 blen = args->len;
899 besti = cnt_cur->bc_ptrs[0];
900 }
901 }
902 /*
903 * It didn't work. We COULD be in a case where
904 * there's a good record somewhere, so try again.
905 */
906 if (blen == 0)
907 break;
908 /*
909 * Point at the best entry, and retrieve it again.
910 */
911 cnt_cur->bc_ptrs[0] = besti;
912 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
913 goto error0;
914 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig73523a22010-07-20 17:54:45 +1000915 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 args->len = blen;
917 if (!xfs_alloc_fix_minleft(args)) {
918 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000919 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
922 blen = args->len;
923 /*
924 * We are allocating starting at bnew for blen blocks.
925 */
926 args->agbno = bnew;
927 ASSERT(bnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +1000928 ASSERT(bnew + blen <= ltbno + ltlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /*
930 * Set up a cursor for the by-bno tree.
931 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100932 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
933 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /*
935 * Fix up the btree entries.
936 */
937 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
938 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
939 goto error0;
940 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
941 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000942
943 trace_xfs_alloc_near_first(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945 }
946 /*
947 * Second algorithm.
948 * Search in the by-bno tree to the left and to the right
949 * simultaneously, until in each case we find a space big enough,
950 * or run into the edge of the tree. When we run into the edge,
951 * we deallocate that cursor.
952 * If both searches succeed, we compare the two spaces and pick
953 * the better one.
954 * With alignment, it's possible for both to fail; the upper
955 * level algorithm that picks allocation groups for allocations
956 * is not supposed to do this.
957 */
958 /*
959 * Allocate and initialize the cursor for the leftward search.
960 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100961 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
962 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /*
964 * Lookup <= bno to find the leftward search's starting point.
965 */
966 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
967 goto error0;
968 if (!i) {
969 /*
970 * Didn't find anything; use this cursor for the rightward
971 * search.
972 */
973 bno_cur_gt = bno_cur_lt;
974 bno_cur_lt = NULL;
975 }
976 /*
977 * Found something. Duplicate the cursor for the rightward search.
978 */
979 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
980 goto error0;
981 /*
982 * Increment the cursor, so we will point at the entry just right
983 * of the leftward entry if any, or to the leftmost entry.
984 */
Christoph Hellwig637aa502008-10-30 16:55:45 +1100985 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto error0;
987 if (!i) {
988 /*
989 * It failed, there are no rightward entries.
990 */
991 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
992 bno_cur_gt = NULL;
993 }
994 /*
995 * Loop going left with the leftward cursor, right with the
996 * rightward cursor, until either both directions give up or
997 * we find an entry at least as big as minlen.
998 */
999 do {
1000 if (bno_cur_lt) {
1001 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1002 goto error0;
1003 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001004 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1005 &ltbnoa, &ltlena);
David Chinner12375c82008-04-10 12:21:32 +10001006 if (ltlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001008 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto error0;
1010 if (!i) {
1011 xfs_btree_del_cursor(bno_cur_lt,
1012 XFS_BTREE_NOERROR);
1013 bno_cur_lt = NULL;
1014 }
1015 }
1016 if (bno_cur_gt) {
1017 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1018 goto error0;
1019 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001020 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1021 &gtbnoa, &gtlena);
David Chinner12375c82008-04-10 12:21:32 +10001022 if (gtlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001024 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto error0;
1026 if (!i) {
1027 xfs_btree_del_cursor(bno_cur_gt,
1028 XFS_BTREE_NOERROR);
1029 bno_cur_gt = NULL;
1030 }
1031 }
1032 } while (bno_cur_lt || bno_cur_gt);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * Got both cursors still active, need to find better entry.
1036 */
1037 if (bno_cur_lt && bno_cur_gt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (ltlena >= args->minlen) {
1039 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001040 * Left side is good, look for a right side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1043 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001044 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 args->alignment, ltbno, ltlen, &ltnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001046
1047 error = xfs_alloc_find_best_extent(args,
1048 &bno_cur_lt, &bno_cur_gt,
1049 ltdiff, &gtbno, &gtlen, &gtlena,
1050 0 /* search right */);
1051 } else {
1052 ASSERT(gtlena >= args->minlen);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
Christoph Hellwig489a1502010-12-10 15:04:11 +00001055 * Right side is good, look for a left side entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1058 xfs_alloc_fix_len(args);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001059 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 args->alignment, gtbno, gtlen, &gtnew);
Christoph Hellwig489a1502010-12-10 15:04:11 +00001061
1062 error = xfs_alloc_find_best_extent(args,
1063 &bno_cur_gt, &bno_cur_lt,
1064 gtdiff, &ltbno, &ltlen, &ltlena,
1065 1 /* search left */);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001067
1068 if (error)
1069 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * If we couldn't get anything, give up.
1074 */
1075 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001076 trace_xfs_alloc_size_neither(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 args->agbno = NULLAGBLOCK;
1078 return 0;
1079 }
Christoph Hellwig489a1502010-12-10 15:04:11 +00001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * At this point we have selected a freespace entry, either to the
1083 * left or to the right. If it's on the right, copy all the
1084 * useful variables to the "left" set so we only have one
1085 * copy of this code.
1086 */
1087 if (bno_cur_gt) {
1088 bno_cur_lt = bno_cur_gt;
1089 bno_cur_gt = NULL;
1090 ltbno = gtbno;
1091 ltbnoa = gtbnoa;
1092 ltlen = gtlen;
1093 ltlena = gtlena;
1094 j = 1;
1095 } else
1096 j = 0;
Christoph Hellwig489a1502010-12-10 15:04:11 +00001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
1099 * Fix up the length and compute the useful address.
1100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1102 xfs_alloc_fix_len(args);
1103 if (!xfs_alloc_fix_minleft(args)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001104 trace_xfs_alloc_near_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1106 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1107 return 0;
1108 }
1109 rlen = args->len;
1110 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1111 ltlen, &ltnew);
1112 ASSERT(ltnew >= ltbno);
Christoph Hellwig73523a22010-07-20 17:54:45 +10001113 ASSERT(ltnew + rlen <= ltbno + ltlen);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001114 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 args->agbno = ltnew;
1116 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1117 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1118 goto error0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001119
1120 if (j)
1121 trace_xfs_alloc_near_greater(args);
1122 else
1123 trace_xfs_alloc_near_lesser(args);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1126 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1127 return 0;
1128
1129 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001130 trace_xfs_alloc_near_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (cnt_cur != NULL)
1132 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1133 if (bno_cur_lt != NULL)
1134 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1135 if (bno_cur_gt != NULL)
1136 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1137 return error;
1138}
1139
1140/*
1141 * Allocate a variable extent anywhere in the allocation group agno.
1142 * Extent's length (returned in len) will be between minlen and maxlen,
1143 * and of the form k * prod + mod unless there's nothing that large.
1144 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1145 */
1146STATIC int /* error */
1147xfs_alloc_ag_vextent_size(
1148 xfs_alloc_arg_t *args) /* allocation argument structure */
1149{
1150 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1151 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1152 int error; /* error result */
1153 xfs_agblock_t fbno; /* start of found freespace */
1154 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int i; /* temp status variable */
1156 xfs_agblock_t rbno; /* returned block number */
1157 xfs_extlen_t rlen; /* length of returned extent */
1158
1159 /*
1160 * Allocate and initialize a cursor for the by-size btree.
1161 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001162 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1163 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 bno_cur = NULL;
1165 /*
1166 * Look for an entry >= maxlen+alignment-1 blocks.
1167 */
1168 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1169 args->maxlen + args->alignment - 1, &i)))
1170 goto error0;
1171 /*
1172 * If none, then pick up the last entry in the tree unless the
1173 * tree is empty.
1174 */
1175 if (!i) {
1176 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1177 &flen, &i)))
1178 goto error0;
1179 if (i == 0 || flen == 0) {
1180 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001181 trace_xfs_alloc_size_noentry(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183 }
1184 ASSERT(i == 1);
1185 }
1186 /*
1187 * There's a freespace as big as maxlen+alignment-1, get it.
1188 */
1189 else {
1190 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1191 goto error0;
1192 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1193 }
1194 /*
1195 * In the first case above, we got the last entry in the
1196 * by-size btree. Now we check to see if the space hits maxlen
1197 * once aligned; if not, we search left for something better.
1198 * This can't happen in the second case above.
1199 */
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001200 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1202 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1203 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1204 if (rlen < args->maxlen) {
1205 xfs_agblock_t bestfbno;
1206 xfs_extlen_t bestflen;
1207 xfs_agblock_t bestrbno;
1208 xfs_extlen_t bestrlen;
1209
1210 bestrlen = rlen;
1211 bestrbno = rbno;
1212 bestflen = flen;
1213 bestfbno = fbno;
1214 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001215 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto error0;
1217 if (i == 0)
1218 break;
1219 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1220 &i)))
1221 goto error0;
1222 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1223 if (flen < bestrlen)
1224 break;
Christoph Hellwig86fa8af2011-03-04 12:59:54 +00001225 xfs_alloc_compute_aligned(args, fbno, flen,
1226 &rbno, &rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1228 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1229 (rlen <= flen && rbno + rlen <= fbno + flen),
1230 error0);
1231 if (rlen > bestrlen) {
1232 bestrlen = rlen;
1233 bestrbno = rbno;
1234 bestflen = flen;
1235 bestfbno = fbno;
1236 if (rlen == args->maxlen)
1237 break;
1238 }
1239 }
1240 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1241 &i)))
1242 goto error0;
1243 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1244 rlen = bestrlen;
1245 rbno = bestrbno;
1246 flen = bestflen;
1247 fbno = bestfbno;
1248 }
1249 args->wasfromfl = 0;
1250 /*
1251 * Fix up the length.
1252 */
1253 args->len = rlen;
1254 xfs_alloc_fix_len(args);
1255 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1256 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001257 trace_xfs_alloc_size_nominleft(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 args->agbno = NULLAGBLOCK;
1259 return 0;
1260 }
1261 rlen = args->len;
1262 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1263 /*
1264 * Allocate and initialize a cursor for the by-block tree.
1265 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001266 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1267 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1269 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1270 goto error0;
1271 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1272 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1273 cnt_cur = bno_cur = NULL;
1274 args->len = rlen;
1275 args->agbno = rbno;
1276 XFS_WANT_CORRUPTED_GOTO(
1277 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001278 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 error0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001280 trace_xfs_alloc_size_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282
1283error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001284 trace_xfs_alloc_size_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (cnt_cur)
1286 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1287 if (bno_cur)
1288 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1289 return error;
1290}
1291
1292/*
1293 * Deal with the case where only small freespaces remain.
1294 * Either return the contents of the last freespace record,
1295 * or allocate space from the freelist if there is nothing in the tree.
1296 */
1297STATIC int /* error */
1298xfs_alloc_ag_vextent_small(
1299 xfs_alloc_arg_t *args, /* allocation argument structure */
1300 xfs_btree_cur_t *ccur, /* by-size cursor */
1301 xfs_agblock_t *fbnop, /* result block number */
1302 xfs_extlen_t *flenp, /* result length */
1303 int *stat) /* status: 0-freelist, 1-normal/none */
1304{
1305 int error;
1306 xfs_agblock_t fbno;
1307 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int i;
1309
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001310 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto error0;
1312 if (i) {
1313 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1314 goto error0;
1315 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1316 }
1317 /*
1318 * Nothing in the btree, try the freelist. Make sure
1319 * to respect minleft even when pulling from the
1320 * freelist.
1321 */
1322 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001323 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1324 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001325 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1326 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto error0;
1328 if (fbno != NULLAGBLOCK) {
Christoph Hellwiga870acd2011-04-24 19:06:14 +00001329 if (xfs_alloc_busy_search(args->mp, args->agno, fbno, 1))
1330 xfs_trans_set_sync(args->tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (args->userdata) {
1332 xfs_buf_t *bp;
1333
1334 bp = xfs_btree_get_bufs(args->mp, args->tp,
1335 args->agno, fbno, 0);
1336 xfs_trans_binval(args->tp, bp);
1337 }
1338 args->len = 1;
1339 args->agbno = fbno;
1340 XFS_WANT_CORRUPTED_GOTO(
1341 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001342 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 error0);
1344 args->wasfromfl = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001345 trace_xfs_alloc_small_freelist(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *stat = 0;
1347 return 0;
1348 }
1349 /*
1350 * Nothing in the freelist.
1351 */
1352 else
1353 flen = 0;
1354 }
1355 /*
1356 * Can't allocate from the freelist for some reason.
1357 */
Nathan Scottd432c802006-09-28 11:03:44 +10001358 else {
1359 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
1363 * Can't do the allocation, give up.
1364 */
1365 if (flen < args->minlen) {
1366 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001367 trace_xfs_alloc_small_notenough(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 flen = 0;
1369 }
1370 *fbnop = fbno;
1371 *flenp = flen;
1372 *stat = 1;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001373 trace_xfs_alloc_small_done(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375
1376error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001377 trace_xfs_alloc_small_error(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return error;
1379}
1380
1381/*
1382 * Free the extent starting at agno/bno for length.
1383 */
1384STATIC int /* error */
1385xfs_free_ag_extent(
1386 xfs_trans_t *tp, /* transaction pointer */
1387 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1388 xfs_agnumber_t agno, /* allocation group number */
1389 xfs_agblock_t bno, /* starting block number */
1390 xfs_extlen_t len, /* length of extent */
1391 int isfl) /* set if is freelist blocks - no sb acctg */
1392{
1393 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1394 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1395 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 xfs_agblock_t gtbno; /* start of right neighbor block */
1397 xfs_extlen_t gtlen; /* length of right neighbor block */
1398 int haveleft; /* have a left neighbor block */
1399 int haveright; /* have a right neighbor block */
1400 int i; /* temp, result code */
1401 xfs_agblock_t ltbno; /* start of left neighbor block */
1402 xfs_extlen_t ltlen; /* length of left neighbor block */
1403 xfs_mount_t *mp; /* mount point struct for filesystem */
1404 xfs_agblock_t nbno; /* new starting block of freespace */
1405 xfs_extlen_t nlen; /* new length of freespace */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001406 xfs_perag_t *pag; /* per allocation group data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 mp = tp->t_mountp;
1409 /*
1410 * Allocate and initialize a cursor for the by-block btree.
1411 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001412 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 cnt_cur = NULL;
1414 /*
1415 * Look for a neighboring block on the left (lower block numbers)
1416 * that is contiguous with this space.
1417 */
1418 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1419 goto error0;
1420 if (haveleft) {
1421 /*
1422 * There is a block to our left.
1423 */
1424 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1425 goto error0;
1426 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1427 /*
1428 * It's not contiguous, though.
1429 */
1430 if (ltbno + ltlen < bno)
1431 haveleft = 0;
1432 else {
1433 /*
1434 * If this failure happens the request to free this
1435 * space was invalid, it's (partly) already free.
1436 * Very bad.
1437 */
1438 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1439 }
1440 }
1441 /*
1442 * Look for a neighboring block on the right (higher block numbers)
1443 * that is contiguous with this space.
1444 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001445 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto error0;
1447 if (haveright) {
1448 /*
1449 * There is a block to our right.
1450 */
1451 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1452 goto error0;
1453 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1454 /*
1455 * It's not contiguous, though.
1456 */
1457 if (bno + len < gtbno)
1458 haveright = 0;
1459 else {
1460 /*
1461 * If this failure happens the request to free this
1462 * space was invalid, it's (partly) already free.
1463 * Very bad.
1464 */
1465 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1466 }
1467 }
1468 /*
1469 * Now allocate and initialize a cursor for the by-size tree.
1470 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001471 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /*
1473 * Have both left and right contiguous neighbors.
1474 * Merge all three into a single free block.
1475 */
1476 if (haveleft && haveright) {
1477 /*
1478 * Delete the old by-size entry on the left.
1479 */
1480 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1481 goto error0;
1482 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001483 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto error0;
1485 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1486 /*
1487 * Delete the old by-size entry on the right.
1488 */
1489 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1490 goto error0;
1491 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001492 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto error0;
1494 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1495 /*
1496 * Delete the old by-block entry for the right block.
1497 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001498 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 goto error0;
1500 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1501 /*
1502 * Move the by-block cursor back to the left neighbor.
1503 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001504 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto error0;
1506 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1507#ifdef DEBUG
1508 /*
1509 * Check that this is the right record: delete didn't
1510 * mangle the cursor.
1511 */
1512 {
1513 xfs_agblock_t xxbno;
1514 xfs_extlen_t xxlen;
1515
1516 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1517 &i)))
1518 goto error0;
1519 XFS_WANT_CORRUPTED_GOTO(
1520 i == 1 && xxbno == ltbno && xxlen == ltlen,
1521 error0);
1522 }
1523#endif
1524 /*
1525 * Update remaining by-block entry to the new, joined block.
1526 */
1527 nbno = ltbno;
1528 nlen = len + ltlen + gtlen;
1529 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1530 goto error0;
1531 }
1532 /*
1533 * Have only a left contiguous neighbor.
1534 * Merge it together with the new freespace.
1535 */
1536 else if (haveleft) {
1537 /*
1538 * Delete the old by-size entry on the left.
1539 */
1540 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1541 goto error0;
1542 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001543 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto error0;
1545 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1546 /*
1547 * Back up the by-block cursor to the left neighbor, and
1548 * update its length.
1549 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001550 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto error0;
1552 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1553 nbno = ltbno;
1554 nlen = len + ltlen;
1555 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1556 goto error0;
1557 }
1558 /*
1559 * Have only a right contiguous neighbor.
1560 * Merge it together with the new freespace.
1561 */
1562 else if (haveright) {
1563 /*
1564 * Delete the old by-size entry on the right.
1565 */
1566 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1567 goto error0;
1568 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001569 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto error0;
1571 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1572 /*
1573 * Update the starting block and length of the right
1574 * neighbor in the by-block tree.
1575 */
1576 nbno = bno;
1577 nlen = len + gtlen;
1578 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1579 goto error0;
1580 }
1581 /*
1582 * No contiguous neighbors.
1583 * Insert the new freespace into the by-block tree.
1584 */
1585 else {
1586 nbno = bno;
1587 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001588 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto error0;
1590 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1591 }
1592 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1593 bno_cur = NULL;
1594 /*
1595 * In all cases we need to insert the new freespace in the by-size tree.
1596 */
1597 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1598 goto error0;
1599 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001600 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 goto error0;
1602 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1603 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1604 cnt_cur = NULL;
Christoph Hellwigecb69282011-03-04 12:59:55 +00001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * Update the freespace totals in the ag and superblock.
1608 */
Christoph Hellwigecb69282011-03-04 12:59:55 +00001609 pag = xfs_perag_get(mp, agno);
1610 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1611 xfs_perag_put(pag);
1612 if (error)
1613 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Christoph Hellwigecb69282011-03-04 12:59:55 +00001615 if (!isfl)
1616 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1617 XFS_STATS_INC(xs_freex);
1618 XFS_STATS_ADD(xs_freeb, len);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001619
1620 trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return 0;
1623
1624 error0:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001625 trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (bno_cur)
1627 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1628 if (cnt_cur)
1629 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1630 return error;
1631}
1632
1633/*
1634 * Visible (exported) allocation/free functions.
1635 * Some of these are used just by xfs_alloc_btree.c and this file.
1636 */
1637
1638/*
1639 * Compute and fill in value of m_ag_maxlevels.
1640 */
1641void
1642xfs_alloc_compute_maxlevels(
1643 xfs_mount_t *mp) /* file system mount structure */
1644{
1645 int level;
1646 uint maxblocks;
1647 uint maxleafents;
1648 int minleafrecs;
1649 int minnoderecs;
1650
1651 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1652 minleafrecs = mp->m_alloc_mnr[0];
1653 minnoderecs = mp->m_alloc_mnr[1];
1654 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1655 for (level = 1; maxblocks > 1; level++)
1656 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1657 mp->m_ag_maxlevels = level;
1658}
1659
1660/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001661 * Find the length of the longest extent in an AG.
1662 */
1663xfs_extlen_t
1664xfs_alloc_longest_free_extent(
1665 struct xfs_mount *mp,
1666 struct xfs_perag *pag)
1667{
1668 xfs_extlen_t need, delta = 0;
1669
1670 need = XFS_MIN_FREELIST_PAG(pag, mp);
1671 if (need > pag->pagf_flcount)
1672 delta = need - pag->pagf_flcount;
1673
1674 if (pag->pagf_longest > delta)
1675 return pag->pagf_longest - delta;
1676 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1677}
1678
1679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * Decide whether to use this allocation group for this allocation.
1681 * If so, fix up the btree freelist's size.
1682 */
1683STATIC int /* error */
1684xfs_alloc_fix_freelist(
1685 xfs_alloc_arg_t *args, /* allocation argument structure */
1686 int flags) /* XFS_ALLOC_FLAG_... */
1687{
1688 xfs_buf_t *agbp; /* agf buffer pointer */
1689 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1690 xfs_buf_t *agflbp;/* agfl buffer pointer */
1691 xfs_agblock_t bno; /* freelist block */
1692 xfs_extlen_t delta; /* new blocks needed in freelist */
1693 int error; /* error result code */
1694 xfs_extlen_t longest;/* longest extent in allocation group */
1695 xfs_mount_t *mp; /* file system mount point structure */
1696 xfs_extlen_t need; /* total blocks needed in freelist */
1697 xfs_perag_t *pag; /* per-ag information structure */
1698 xfs_alloc_arg_t targs; /* local allocation arguments */
1699 xfs_trans_t *tp; /* transaction pointer */
1700
1701 mp = args->mp;
1702
1703 pag = args->pag;
1704 tp = args->tp;
1705 if (!pag->pagf_init) {
1706 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1707 &agbp)))
1708 return error;
1709 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001710 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1711 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 args->agbp = NULL;
1713 return 0;
1714 }
1715 } else
1716 agbp = NULL;
1717
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001718 /*
1719 * If this is a metadata preferred pag and we are user data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * then try somewhere else if we are not being asked to
1721 * try harder at this point
1722 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001723 if (pag->pagf_metadata && args->userdata &&
1724 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1725 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 args->agbp = NULL;
1727 return 0;
1728 }
1729
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001730 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001731 /*
1732 * If it looks like there isn't a long enough extent, or enough
1733 * total blocks, reject it.
1734 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001735 need = XFS_MIN_FREELIST_PAG(pag, mp);
1736 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001737 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1738 longest ||
1739 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1740 need - args->total) < (int)args->minleft)) {
1741 if (agbp)
1742 xfs_trans_brelse(tp, agbp);
1743 args->agbp = NULL;
1744 return 0;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /*
1749 * Get the a.g. freespace buffer.
1750 * Can fail if we're not blocking on locks, and it's held.
1751 */
1752 if (agbp == NULL) {
1753 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1754 &agbp)))
1755 return error;
1756 if (agbp == NULL) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001757 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1758 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 args->agbp = NULL;
1760 return 0;
1761 }
1762 }
1763 /*
1764 * Figure out how many blocks we should have in the freelist.
1765 */
1766 agf = XFS_BUF_TO_AGF(agbp);
1767 need = XFS_MIN_FREELIST(agf, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /*
1769 * If there isn't enough total or single-extent, reject it.
1770 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001771 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1772 delta = need > be32_to_cpu(agf->agf_flcount) ?
1773 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1774 longest = be32_to_cpu(agf->agf_longest);
1775 longest = (longest > delta) ? (longest - delta) :
1776 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1777 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1778 longest ||
1779 ((int)(be32_to_cpu(agf->agf_freeblks) +
1780 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1781 (int)args->minleft)) {
1782 xfs_trans_brelse(tp, agbp);
1783 args->agbp = NULL;
1784 return 0;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 /*
1788 * Make the freelist shorter if it's too long.
1789 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001790 while (be32_to_cpu(agf->agf_flcount) > need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 xfs_buf_t *bp;
1792
David Chinner92821e22007-05-24 15:26:31 +10001793 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return error;
1796 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1797 return error;
1798 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1799 xfs_trans_binval(tp, bp);
1800 }
1801 /*
1802 * Initialize the args structure.
1803 */
1804 targs.tp = tp;
1805 targs.mp = mp;
1806 targs.agbp = agbp;
1807 targs.agno = args->agno;
1808 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1809 targs.minalignslop = 0;
1810 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1811 targs.type = XFS_ALLOCTYPE_THIS_AG;
1812 targs.pag = pag;
1813 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1814 return error;
1815 /*
1816 * Make the freelist longer if it's too short.
1817 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001818 while (be32_to_cpu(agf->agf_flcount) < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 targs.agbno = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001820 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * Allocate as many blocks as possible at once.
1823 */
Nathan Scotte63a3692006-05-08 19:51:58 +10001824 if ((error = xfs_alloc_ag_vextent(&targs))) {
1825 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return error;
Nathan Scotte63a3692006-05-08 19:51:58 +10001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * Stop if we run out. Won't happen if callers are obeying
1830 * the restrictions correctly. Can happen for free calls
1831 * on a completely full ag.
1832 */
Yingping Lud210a282006-06-09 14:55:18 +10001833 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001834 if (flags & XFS_ALLOC_FLAG_FREEING)
1835 break;
1836 xfs_trans_brelse(tp, agflbp);
1837 args->agbp = NULL;
1838 return 0;
Yingping Lud210a282006-06-09 14:55:18 +10001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * Put each allocated block on the list.
1842 */
1843 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10001844 error = xfs_alloc_put_freelist(tp, agbp,
1845 agflbp, bno, 0);
1846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return error;
1848 }
1849 }
Nathan Scotte63a3692006-05-08 19:51:58 +10001850 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 args->agbp = agbp;
1852 return 0;
1853}
1854
1855/*
1856 * Get a block from the freelist.
1857 * Returns with the buffer for the block gotten.
1858 */
1859int /* error */
1860xfs_alloc_get_freelist(
1861 xfs_trans_t *tp, /* transaction pointer */
1862 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10001863 xfs_agblock_t *bnop, /* block address retrieved from freelist */
1864 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 xfs_agf_t *agf; /* a.g. freespace structure */
1867 xfs_agfl_t *agfl; /* a.g. freelist structure */
1868 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
1869 xfs_agblock_t bno; /* block number returned */
1870 int error;
David Chinner92821e22007-05-24 15:26:31 +10001871 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 xfs_mount_t *mp; /* mount structure */
1873 xfs_perag_t *pag; /* per allocation group data */
1874
1875 agf = XFS_BUF_TO_AGF(agbp);
1876 /*
1877 * Freelist is empty, give up.
1878 */
1879 if (!agf->agf_flcount) {
1880 *bnop = NULLAGBLOCK;
1881 return 0;
1882 }
1883 /*
1884 * Read the array of free blocks.
1885 */
1886 mp = tp->t_mountp;
1887 if ((error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11001888 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return error;
1890 agfl = XFS_BUF_TO_AGFL(agflbp);
1891 /*
1892 * Get the block number and update the data structures.
1893 */
Christoph Hellwige2101002006-09-28 10:56:51 +10001894 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001895 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001897 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 agf->agf_flfirst = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00001899
1900 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001901 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 xfs_trans_agflist_delta(tp, -1);
1903 pag->pagf_flcount--;
Dave Chinnera862e0f2010-01-11 11:47:41 +00001904 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10001905
1906 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
1907 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001908 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10001909 pag->pagf_btreeblks++;
1910 logflags |= XFS_AGF_BTREEBLKS;
1911 }
1912
David Chinner92821e22007-05-24 15:26:31 +10001913 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 *bnop = bno;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
1917}
1918
1919/*
1920 * Log the given fields from the agf structure.
1921 */
1922void
1923xfs_alloc_log_agf(
1924 xfs_trans_t *tp, /* transaction pointer */
1925 xfs_buf_t *bp, /* buffer for a.g. freelist header */
1926 int fields) /* mask of fields to be logged (XFS_AGF_...) */
1927{
1928 int first; /* first byte offset */
1929 int last; /* last byte offset */
1930 static const short offsets[] = {
1931 offsetof(xfs_agf_t, agf_magicnum),
1932 offsetof(xfs_agf_t, agf_versionnum),
1933 offsetof(xfs_agf_t, agf_seqno),
1934 offsetof(xfs_agf_t, agf_length),
1935 offsetof(xfs_agf_t, agf_roots[0]),
1936 offsetof(xfs_agf_t, agf_levels[0]),
1937 offsetof(xfs_agf_t, agf_flfirst),
1938 offsetof(xfs_agf_t, agf_fllast),
1939 offsetof(xfs_agf_t, agf_flcount),
1940 offsetof(xfs_agf_t, agf_freeblks),
1941 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10001942 offsetof(xfs_agf_t, agf_btreeblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 sizeof(xfs_agf_t)
1944 };
1945
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001946 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
1949 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
1950}
1951
1952/*
1953 * Interface for inode allocation to force the pag data to be initialized.
1954 */
1955int /* error */
1956xfs_alloc_pagf_init(
1957 xfs_mount_t *mp, /* file system mount structure */
1958 xfs_trans_t *tp, /* transaction pointer */
1959 xfs_agnumber_t agno, /* allocation group number */
1960 int flags) /* XFS_ALLOC_FLAGS_... */
1961{
1962 xfs_buf_t *bp;
1963 int error;
1964
1965 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
1966 return error;
1967 if (bp)
1968 xfs_trans_brelse(tp, bp);
1969 return 0;
1970}
1971
1972/*
1973 * Put the block on the freelist for the allocation group.
1974 */
1975int /* error */
1976xfs_alloc_put_freelist(
1977 xfs_trans_t *tp, /* transaction pointer */
1978 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1979 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10001980 xfs_agblock_t bno, /* block being freed */
1981 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 xfs_agf_t *agf; /* a.g. freespace structure */
1984 xfs_agfl_t *agfl; /* a.g. free block array */
Christoph Hellwige2101002006-09-28 10:56:51 +10001985 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int error;
David Chinner92821e22007-05-24 15:26:31 +10001987 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 xfs_mount_t *mp; /* mount structure */
1989 xfs_perag_t *pag; /* per allocation group data */
1990
1991 agf = XFS_BUF_TO_AGF(agbp);
1992 mp = tp->t_mountp;
1993
1994 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11001995 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return error;
1997 agfl = XFS_BUF_TO_AGFL(agflbp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001998 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001999 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 agf->agf_fllast = 0;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002001
2002 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002003 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 xfs_trans_agflist_delta(tp, 1);
2005 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002006
2007 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2008 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002009 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002010 pag->pagf_btreeblks--;
2011 logflags |= XFS_AGF_BTREEBLKS;
2012 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002013 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10002014
David Chinner92821e22007-05-24 15:26:31 +10002015 xfs_alloc_log_agf(tp, agbp, logflags);
2016
Christoph Hellwig16259e72005-11-02 15:11:25 +11002017 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2018 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002019 *blockp = cpu_to_be32(bno);
David Chinner92821e22007-05-24 15:26:31 +10002020 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_trans_log_buf(tp, agflbp,
2022 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2023 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2024 sizeof(xfs_agblock_t) - 1));
2025 return 0;
2026}
2027
2028/*
2029 * Read in the allocation group header (free/alloc section).
2030 */
2031int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002032xfs_read_agf(
2033 struct xfs_mount *mp, /* mount point structure */
2034 struct xfs_trans *tp, /* transaction pointer */
2035 xfs_agnumber_t agno, /* allocation group number */
2036 int flags, /* XFS_BUF_ */
2037 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002039 struct xfs_agf *agf; /* ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 int agf_ok; /* set if agf is consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int error;
2042
2043 ASSERT(agno != NULLAGNUMBER);
2044 error = xfs_trans_read_buf(
2045 mp, tp, mp->m_ddev_targp,
2046 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002047 XFS_FSS_TO_BB(mp, 1), flags, bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (error)
2049 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002050 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002052
2053 ASSERT(!XFS_BUF_GETERROR(*bpp));
2054 agf = XFS_BUF_TO_AGF(*bpp);
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Validate the magic number of the agf block.
2058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 agf_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002060 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2061 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2062 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2063 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2064 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002065 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
2066 be32_to_cpu(agf->agf_seqno) == agno;
Barry Naujok89b28392008-10-30 17:05:49 +11002067 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2068 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2069 be32_to_cpu(agf->agf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2071 XFS_RANDOM_ALLOC_READ_AGF))) {
2072 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2073 XFS_ERRLEVEL_LOW, mp, agf);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002074 xfs_trans_brelse(tp, *bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return XFS_ERROR(EFSCORRUPTED);
2076 }
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002077 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
2078 return 0;
2079}
2080
2081/*
2082 * Read in the allocation group header (free/alloc section).
2083 */
2084int /* error */
2085xfs_alloc_read_agf(
2086 struct xfs_mount *mp, /* mount point structure */
2087 struct xfs_trans *tp, /* transaction pointer */
2088 xfs_agnumber_t agno, /* allocation group number */
2089 int flags, /* XFS_ALLOC_FLAG_... */
2090 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2091{
2092 struct xfs_agf *agf; /* ag freelist header */
2093 struct xfs_perag *pag; /* per allocation group data */
2094 int error;
2095
2096 ASSERT(agno != NULLAGNUMBER);
2097
2098 error = xfs_read_agf(mp, tp, agno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002099 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002100 bpp);
2101 if (error)
2102 return error;
2103 if (!*bpp)
2104 return 0;
2105 ASSERT(!XFS_BUF_GETERROR(*bpp));
2106
2107 agf = XFS_BUF_TO_AGF(*bpp);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002108 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002110 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002111 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002112 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2113 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002115 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002117 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002118 spin_lock_init(&pag->pagb_lock);
Dave Chinnere57336f2010-01-11 11:47:49 +00002119 pag->pagb_count = 0;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002120 pag->pagb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 pag->pagf_init = 1;
2122 }
2123#ifdef DEBUG
2124 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002125 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002126 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002127 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2128 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002130 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002132 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134#endif
Dave Chinnera862e0f2010-01-11 11:47:41 +00002135 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138
2139/*
2140 * Allocate an extent (variable-size).
2141 * Depending on the allocation type, we either look in a single allocation
2142 * group or loop over the allocation groups to find the result.
2143 */
2144int /* error */
2145xfs_alloc_vextent(
2146 xfs_alloc_arg_t *args) /* allocation argument structure */
2147{
2148 xfs_agblock_t agsize; /* allocation group size */
2149 int error;
2150 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 xfs_extlen_t minleft;/* minimum left value, temp copy */
2152 xfs_mount_t *mp; /* mount structure pointer */
2153 xfs_agnumber_t sagno; /* starting allocation group number */
2154 xfs_alloctype_t type; /* input allocation type */
2155 int bump_rotor = 0;
2156 int no_min = 0;
2157 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2158
2159 mp = args->mp;
2160 type = args->otype = args->type;
2161 args->agbno = NULLAGBLOCK;
2162 /*
2163 * Just fix this up, for the case where the last a.g. is shorter
2164 * (or there's only one a.g.) and the caller couldn't easily figure
2165 * that out (xfs_bmap_alloc).
2166 */
2167 agsize = mp->m_sb.sb_agblocks;
2168 if (args->maxlen > agsize)
2169 args->maxlen = agsize;
2170 if (args->alignment == 0)
2171 args->alignment = 1;
2172 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2173 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2174 ASSERT(args->minlen <= args->maxlen);
2175 ASSERT(args->minlen <= agsize);
2176 ASSERT(args->mod < args->prod);
2177 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2178 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2179 args->minlen > args->maxlen || args->minlen > agsize ||
2180 args->mod >= args->prod) {
2181 args->fsbno = NULLFSBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002182 trace_xfs_alloc_vextent_badargs(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184 }
2185 minleft = args->minleft;
2186
2187 switch (type) {
2188 case XFS_ALLOCTYPE_THIS_AG:
2189 case XFS_ALLOCTYPE_NEAR_BNO:
2190 case XFS_ALLOCTYPE_THIS_BNO:
2191 /*
2192 * These three force us into a single a.g.
2193 */
2194 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002195 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 args->minleft = 0;
2197 error = xfs_alloc_fix_freelist(args, 0);
2198 args->minleft = minleft;
2199 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002200 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto error0;
2202 }
2203 if (!args->agbp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002204 trace_xfs_alloc_vextent_noagbp(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 break;
2206 }
2207 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2208 if ((error = xfs_alloc_ag_vextent(args)))
2209 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 break;
2211 case XFS_ALLOCTYPE_START_BNO:
2212 /*
2213 * Try near allocation first, then anywhere-in-ag after
2214 * the first a.g. fails.
2215 */
2216 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2217 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2218 args->fsbno = XFS_AGB_TO_FSB(mp,
2219 ((mp->m_agfrotor / rotorstep) %
2220 mp->m_sb.sb_agcount), 0);
2221 bump_rotor = 1;
2222 }
2223 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2224 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2225 /* FALLTHROUGH */
2226 case XFS_ALLOCTYPE_ANY_AG:
2227 case XFS_ALLOCTYPE_START_AG:
2228 case XFS_ALLOCTYPE_FIRST_AG:
2229 /*
2230 * Rotate through the allocation groups looking for a winner.
2231 */
2232 if (type == XFS_ALLOCTYPE_ANY_AG) {
2233 /*
2234 * Start with the last place we left off.
2235 */
2236 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2237 mp->m_sb.sb_agcount;
2238 args->type = XFS_ALLOCTYPE_THIS_AG;
2239 flags = XFS_ALLOC_FLAG_TRYLOCK;
2240 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2241 /*
2242 * Start with allocation group given by bno.
2243 */
2244 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2245 args->type = XFS_ALLOCTYPE_THIS_AG;
2246 sagno = 0;
2247 flags = 0;
2248 } else {
2249 if (type == XFS_ALLOCTYPE_START_AG)
2250 args->type = XFS_ALLOCTYPE_THIS_AG;
2251 /*
2252 * Start with the given allocation group.
2253 */
2254 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2255 flags = XFS_ALLOC_FLAG_TRYLOCK;
2256 }
2257 /*
2258 * Loop over allocation groups twice; first time with
2259 * trylock set, second time without.
2260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 for (;;) {
Dave Chinnera862e0f2010-01-11 11:47:41 +00002262 args->pag = xfs_perag_get(mp, args->agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (no_min) args->minleft = 0;
2264 error = xfs_alloc_fix_freelist(args, flags);
2265 args->minleft = minleft;
2266 if (error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002267 trace_xfs_alloc_vextent_nofix(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 goto error0;
2269 }
2270 /*
2271 * If we get a buffer back then the allocation will fly.
2272 */
2273 if (args->agbp) {
2274 if ((error = xfs_alloc_ag_vextent(args)))
2275 goto error0;
2276 break;
2277 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
2279 trace_xfs_alloc_vextent_loopfailed(args);
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /*
2282 * Didn't work, figure out the next iteration.
2283 */
2284 if (args->agno == sagno &&
2285 type == XFS_ALLOCTYPE_START_BNO)
2286 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002287 /*
2288 * For the first allocation, we can try any AG to get
2289 * space. However, if we already have allocated a
2290 * block, we don't want to try AGs whose number is below
2291 * sagno. Otherwise, we may end up with out-of-order
2292 * locking of AGF, which might cause deadlock.
2293 */
2294 if (++(args->agno) == mp->m_sb.sb_agcount) {
2295 if (args->firstblock != NULLFSBLOCK)
2296 args->agno = sagno;
2297 else
2298 args->agno = 0;
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * Reached the starting a.g., must either be done
2302 * or switch to non-trylock mode.
2303 */
2304 if (args->agno == sagno) {
2305 if (no_min == 1) {
2306 args->agbno = NULLAGBLOCK;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002307 trace_xfs_alloc_vextent_allfailed(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 break;
2309 }
2310 if (flags == 0) {
2311 no_min = 1;
2312 } else {
2313 flags = 0;
2314 if (type == XFS_ALLOCTYPE_START_BNO) {
2315 args->agbno = XFS_FSB_TO_AGBNO(mp,
2316 args->fsbno);
2317 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2318 }
2319 }
2320 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002321 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2324 if (args->agno == sagno)
2325 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2326 (mp->m_sb.sb_agcount * rotorstep);
2327 else
2328 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2329 (mp->m_sb.sb_agcount * rotorstep);
2330 }
2331 break;
2332 default:
2333 ASSERT(0);
2334 /* NOTREACHED */
2335 }
2336 if (args->agbno == NULLAGBLOCK)
2337 args->fsbno = NULLFSBLOCK;
2338 else {
2339 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2340#ifdef DEBUG
2341 ASSERT(args->len >= args->minlen);
2342 ASSERT(args->len <= args->maxlen);
2343 ASSERT(args->agbno % args->alignment == 0);
2344 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2345 args->len);
2346#endif
2347 }
Dave Chinnera862e0f2010-01-11 11:47:41 +00002348 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002351 xfs_perag_put(args->pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return error;
2353}
2354
2355/*
2356 * Free an extent.
2357 * Just break up the extent address and hand off to xfs_free_ag_extent
2358 * after fixing up the freelist.
2359 */
2360int /* error */
2361xfs_free_extent(
2362 xfs_trans_t *tp, /* transaction pointer */
2363 xfs_fsblock_t bno, /* starting block number of extent */
2364 xfs_extlen_t len) /* length of extent */
2365{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002366 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int error;
2368
2369 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002370 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 args.tp = tp;
2372 args.mp = tp->t_mountp;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002373
2374 /*
2375 * validate that the block number is legal - the enables us to detect
2376 * and handle a silent filesystem corruption rather than crashing.
2377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002379 if (args.agno >= args.mp->m_sb.sb_agcount)
2380 return EFSCORRUPTED;
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002383 if (args.agbno >= args.mp->m_sb.sb_agblocks)
2384 return EFSCORRUPTED;
2385
Dave Chinnera862e0f2010-01-11 11:47:41 +00002386 args.pag = xfs_perag_get(args.mp, args.agno);
Dave Chinnerbe65b182011-04-08 12:45:07 +10002387 ASSERT(args.pag);
2388
2389 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2390 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto error0;
Dave Chinnerbe65b182011-04-08 12:45:07 +10002392
2393 /* validate the extent size is legal now we have the agf locked */
2394 if (args.agbno + len >
2395 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
2396 error = EFSCORRUPTED;
2397 goto error0;
2398 }
2399
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002400 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Christoph Hellwiga870acd2011-04-24 19:06:14 +00002401 if (!error)
2402 xfs_alloc_busy_insert(tp, args.agno, args.agbno, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403error0:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002404 xfs_perag_put(args.pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return error;
2406}
2407
2408
2409/*
2410 * AG Busy list management
2411 * The busy list contains block ranges that have been freed but whose
Nathan Scottc41564b2006-03-29 08:55:14 +10002412 * transactions have not yet hit disk. If any block listed in a busy
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 * list is reused, the transaction that freed it must be forced to disk
2414 * before continuing to use the block.
2415 *
Dave Chinnered3b4d62010-05-21 12:07:08 +10002416 * xfs_alloc_busy_insert - add to the per-ag busy list
2417 * xfs_alloc_busy_clear - remove an item from the per-ag busy list
2418 * xfs_alloc_busy_search - search for a busy extent
2419 */
2420
2421/*
2422 * Insert a new extent into the busy tree.
2423 *
2424 * The busy extent tree is indexed by the start block of the busy extent.
2425 * there can be multiple overlapping ranges in the busy extent tree but only
2426 * ever one entry at a given start block. The reason for this is that
2427 * multi-block extents can be freed, then smaller chunks of that extent
2428 * allocated and freed again before the first transaction commit is on disk.
2429 * If the exact same start block is freed a second time, we have to wait for
2430 * that busy extent to pass out of the tree before the new extent is inserted.
2431 * There are two main cases we have to handle here.
2432 *
2433 * The first case is a transaction that triggers a "free - allocate - free"
2434 * cycle. This can occur during btree manipulations as a btree block is freed
2435 * to the freelist, then allocated from the free list, then freed again. In
2436 * this case, the second extxpnet free is what triggers the duplicate and as
2437 * such the transaction IDs should match. Because the extent was allocated in
2438 * this transaction, the transaction must be marked as synchronous. This is
2439 * true for all cases where the free/alloc/free occurs in the one transaction,
2440 * hence the addition of the ASSERT(tp->t_flags & XFS_TRANS_SYNC) to this case.
2441 * This serves to catch violations of the second case quite effectively.
2442 *
2443 * The second case is where the free/alloc/free occur in different
2444 * transactions. In this case, the thread freeing the extent the second time
2445 * can't mark the extent busy immediately because it is already tracked in a
2446 * transaction that may be committing. When the log commit for the existing
2447 * busy extent completes, the busy extent will be removed from the tree. If we
2448 * allow the second busy insert to continue using that busy extent structure,
2449 * it can be freed before this transaction is safely in the log. Hence our
2450 * only option in this case is to force the log to remove the existing busy
2451 * extent from the list before we insert the new one with the current
2452 * transaction ID.
2453 *
2454 * The problem we are trying to avoid in the free-alloc-free in separate
2455 * transactions is most easily described with a timeline:
2456 *
2457 * Thread 1 Thread 2 Thread 3 xfslogd
2458 * xact alloc
2459 * free X
2460 * mark busy
2461 * commit xact
2462 * free xact
2463 * xact alloc
2464 * alloc X
2465 * busy search
2466 * mark xact sync
2467 * commit xact
2468 * free xact
2469 * force log
2470 * checkpoint starts
2471 * ....
2472 * xact alloc
2473 * free X
2474 * mark busy
2475 * finds match
2476 * *** KABOOM! ***
2477 * ....
2478 * log IO completes
2479 * unbusy X
2480 * checkpoint completes
2481 *
2482 * By issuing a log force in thread 3 @ "KABOOM", the thread will block until
2483 * the checkpoint completes, and the busy extent it matched will have been
2484 * removed from the tree when it is woken. Hence it can then continue safely.
2485 *
2486 * However, to ensure this matching process is robust, we need to use the
2487 * transaction ID for identifying transaction, as delayed logging results in
2488 * the busy extent and transaction lifecycles being different. i.e. the busy
2489 * extent is active for a lot longer than the transaction. Hence the
2490 * transaction structure can be freed and reallocated, then mark the same
2491 * extent busy again in the new transaction. In this case the new transaction
2492 * will have a different tid but can have the same address, and hence we need
2493 * to check against the tid.
2494 *
2495 * Future: for delayed logging, we could avoid the log force if the extent was
2496 * first freed in the current checkpoint sequence. This, however, requires the
2497 * ability to pin the current checkpoint in memory until this transaction
2498 * commits to ensure that both the original free and the current one combine
2499 * logically into the one checkpoint. If the checkpoint sequences are
2500 * different, however, we still need to wait on a log force.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 */
2502void
Dave Chinnered3b4d62010-05-21 12:07:08 +10002503xfs_alloc_busy_insert(
2504 struct xfs_trans *tp,
2505 xfs_agnumber_t agno,
2506 xfs_agblock_t bno,
2507 xfs_extlen_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Dave Chinnered3b4d62010-05-21 12:07:08 +10002509 struct xfs_busy_extent *new;
2510 struct xfs_busy_extent *busyp;
Dave Chinnera862e0f2010-01-11 11:47:41 +00002511 struct xfs_perag *pag;
Dave Chinnered3b4d62010-05-21 12:07:08 +10002512 struct rb_node **rbp;
2513 struct rb_node *parent;
2514 int match;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Dave Chinnered3b4d62010-05-21 12:07:08 +10002516
2517 new = kmem_zalloc(sizeof(struct xfs_busy_extent), KM_MAYFAIL);
2518 if (!new) {
2519 /*
2520 * No Memory! Since it is now not possible to track the free
2521 * block, make this a synchronous transaction to insure that
2522 * the block is not reused before this transaction commits.
2523 */
2524 trace_xfs_alloc_busy(tp, agno, bno, len, 1);
2525 xfs_trans_set_sync(tp);
2526 return;
2527 }
2528
2529 new->agno = agno;
2530 new->bno = bno;
2531 new->length = len;
2532 new->tid = xfs_log_get_trans_ident(tp);
2533
2534 INIT_LIST_HEAD(&new->list);
2535
2536 /* trace before insert to be able to see failed inserts */
2537 trace_xfs_alloc_busy(tp, agno, bno, len, 0);
2538
2539 pag = xfs_perag_get(tp->t_mountp, new->agno);
2540restart:
Dave Chinnera862e0f2010-01-11 11:47:41 +00002541 spin_lock(&pag->pagb_lock);
Dave Chinnered3b4d62010-05-21 12:07:08 +10002542 rbp = &pag->pagb_tree.rb_node;
2543 parent = NULL;
2544 busyp = NULL;
2545 match = 0;
2546 while (*rbp && match >= 0) {
2547 parent = *rbp;
2548 busyp = rb_entry(parent, struct xfs_busy_extent, rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Dave Chinnered3b4d62010-05-21 12:07:08 +10002550 if (new->bno < busyp->bno) {
2551 /* may overlap, but exact start block is lower */
2552 rbp = &(*rbp)->rb_left;
2553 if (new->bno + new->length > busyp->bno)
2554 match = busyp->tid == new->tid ? 1 : -1;
2555 } else if (new->bno > busyp->bno) {
2556 /* may overlap, but exact start block is higher */
2557 rbp = &(*rbp)->rb_right;
2558 if (bno < busyp->bno + busyp->length)
2559 match = busyp->tid == new->tid ? 1 : -1;
2560 } else {
2561 match = busyp->tid == new->tid ? 1 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 break;
2563 }
2564 }
Dave Chinnered3b4d62010-05-21 12:07:08 +10002565 if (match < 0) {
2566 /* overlap marked busy in different transaction */
2567 spin_unlock(&pag->pagb_lock);
2568 xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
2569 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Dave Chinnered3b4d62010-05-21 12:07:08 +10002571 if (match > 0) {
2572 /*
2573 * overlap marked busy in same transaction. Update if exact
2574 * start block match, otherwise combine the busy extents into
2575 * a single range.
2576 */
2577 if (busyp->bno == new->bno) {
2578 busyp->length = max(busyp->length, new->length);
2579 spin_unlock(&pag->pagb_lock);
2580 ASSERT(tp->t_flags & XFS_TRANS_SYNC);
2581 xfs_perag_put(pag);
2582 kmem_free(new);
2583 return;
2584 }
2585 rb_erase(&busyp->rb_node, &pag->pagb_tree);
2586 new->length = max(busyp->bno + busyp->length,
2587 new->bno + new->length) -
2588 min(busyp->bno, new->bno);
2589 new->bno = min(busyp->bno, new->bno);
2590 } else
2591 busyp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Dave Chinnered3b4d62010-05-21 12:07:08 +10002593 rb_link_node(&new->rb_node, parent, rbp);
2594 rb_insert_color(&new->rb_node, &pag->pagb_tree);
2595
2596 list_add(&new->list, &tp->t_busy);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002597 spin_unlock(&pag->pagb_lock);
2598 xfs_perag_put(pag);
Dave Chinnered3b4d62010-05-21 12:07:08 +10002599 kmem_free(busyp);
2600}
2601
2602/*
2603 * Search for a busy extent within the range of the extent we are about to
2604 * allocate. You need to be holding the busy extent tree lock when calling
2605 * xfs_alloc_busy_search(). This function returns 0 for no overlapping busy
2606 * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
2607 * match. This is done so that a non-zero return indicates an overlap that
2608 * will require a synchronous transaction, but it can still be
2609 * used to distinguish between a partial or exact match.
2610 */
Christoph Hellwiga46db602011-01-07 13:02:04 +00002611int
Dave Chinnered3b4d62010-05-21 12:07:08 +10002612xfs_alloc_busy_search(
2613 struct xfs_mount *mp,
2614 xfs_agnumber_t agno,
2615 xfs_agblock_t bno,
2616 xfs_extlen_t len)
2617{
2618 struct xfs_perag *pag;
2619 struct rb_node *rbp;
2620 struct xfs_busy_extent *busyp;
2621 int match = 0;
2622
2623 pag = xfs_perag_get(mp, agno);
2624 spin_lock(&pag->pagb_lock);
2625
2626 rbp = pag->pagb_tree.rb_node;
2627
2628 /* find closest start bno overlap */
2629 while (rbp) {
2630 busyp = rb_entry(rbp, struct xfs_busy_extent, rb_node);
2631 if (bno < busyp->bno) {
2632 /* may overlap, but exact start block is lower */
2633 if (bno + len > busyp->bno)
2634 match = -1;
2635 rbp = rbp->rb_left;
2636 } else if (bno > busyp->bno) {
2637 /* may overlap, but exact start block is higher */
2638 if (bno < busyp->bno + busyp->length)
2639 match = -1;
2640 rbp = rbp->rb_right;
2641 } else {
2642 /* bno matches busyp, length determines exact match */
2643 match = (busyp->length == len) ? 1 : -1;
2644 break;
2645 }
2646 }
2647 spin_unlock(&pag->pagb_lock);
2648 trace_xfs_alloc_busysearch(mp, agno, bno, len, !!match);
2649 xfs_perag_put(pag);
2650 return match;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653void
Dave Chinnered3b4d62010-05-21 12:07:08 +10002654xfs_alloc_busy_clear(
2655 struct xfs_mount *mp,
2656 struct xfs_busy_extent *busyp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Dave Chinnera862e0f2010-01-11 11:47:41 +00002658 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Dave Chinnered3b4d62010-05-21 12:07:08 +10002660 trace_xfs_alloc_unbusy(mp, busyp->agno, busyp->bno,
2661 busyp->length);
2662
2663 ASSERT(xfs_alloc_busy_search(mp, busyp->agno, busyp->bno,
2664 busyp->length) == 1);
2665
2666 list_del_init(&busyp->list);
2667
2668 pag = xfs_perag_get(mp, busyp->agno);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002669 spin_lock(&pag->pagb_lock);
Dave Chinnered3b4d62010-05-21 12:07:08 +10002670 rb_erase(&busyp->rb_node, &pag->pagb_tree);
Dave Chinnera862e0f2010-01-11 11:47:41 +00002671 spin_unlock(&pag->pagb_lock);
2672 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Dave Chinnered3b4d62010-05-21 12:07:08 +10002674 kmem_free(busyp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}