blob: 875e1bae19417a1824bb0118db2b455375aafba4 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
39#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_error.h"
41
42
43#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
44
45#define XFSA_FIXUP_BNO_OK 1
46#define XFSA_FIXUP_CNT_OK 2
47
David Chinnerf4586e42008-04-10 12:21:25 +100048STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070049xfs_alloc_search_busy(xfs_trans_t *tp,
50 xfs_agnumber_t agno,
51 xfs_agblock_t bno,
52 xfs_extlen_t len);
53
54#if defined(XFS_ALLOC_TRACE)
55ktrace_t *xfs_alloc_trace_buf;
56
57#define TRACE_ALLOC(s,a) \
Harvey Harrison34a622b2008-04-10 12:19:21 +100058 xfs_alloc_trace_alloc(__func__, s, a, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define TRACE_FREE(s,a,b,x,f) \
Harvey Harrison34a622b2008-04-10 12:19:21 +100060 xfs_alloc_trace_free(__func__, s, mp, a, b, x, f, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define TRACE_MODAGF(s,a,f) \
Harvey Harrison34a622b2008-04-10 12:19:21 +100062 xfs_alloc_trace_modagf(__func__, s, mp, a, f, __LINE__)
63#define TRACE_BUSY(__func__,s,ag,agb,l,sl,tp) \
64 xfs_alloc_trace_busy(__func__, s, mp, ag, agb, l, sl, tp, XFS_ALLOC_KTRACE_BUSY, __LINE__)
65#define TRACE_UNBUSY(__func__,s,ag,sl,tp) \
66 xfs_alloc_trace_busy(__func__, s, mp, ag, -1, -1, sl, tp, XFS_ALLOC_KTRACE_UNBUSY, __LINE__)
David Chinnerf4586e42008-04-10 12:21:25 +100067#define TRACE_BUSYSEARCH(__func__,s,ag,agb,l,tp) \
68 xfs_alloc_trace_busy(__func__, s, mp, ag, agb, l, 0, tp, XFS_ALLOC_KTRACE_BUSYSEARCH, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#else
70#define TRACE_ALLOC(s,a)
71#define TRACE_FREE(s,a,b,x,f)
72#define TRACE_MODAGF(s,a,f)
73#define TRACE_BUSY(s,a,ag,agb,l,sl,tp)
74#define TRACE_UNBUSY(fname,s,ag,sl,tp)
David Chinnerf4586e42008-04-10 12:21:25 +100075#define TRACE_BUSYSEARCH(fname,s,ag,agb,l,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif /* XFS_ALLOC_TRACE */
77
78/*
79 * Prototypes for per-ag allocation routines
80 */
81
82STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
83STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
84STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
85STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
86 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
87
88/*
89 * Internal functions.
90 */
91
92/*
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110093 * Lookup the record equal to [bno, len] in the btree given by cur.
94 */
95STATIC int /* error */
96xfs_alloc_lookup_eq(
97 struct xfs_btree_cur *cur, /* btree cursor */
98 xfs_agblock_t bno, /* starting block of extent */
99 xfs_extlen_t len, /* length of extent */
100 int *stat) /* success/failure */
101{
102 cur->bc_rec.a.ar_startblock = bno;
103 cur->bc_rec.a.ar_blockcount = len;
104 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
105}
106
107/*
108 * Lookup the first record greater than or equal to [bno, len]
109 * in the btree given by cur.
110 */
111STATIC int /* error */
112xfs_alloc_lookup_ge(
113 struct xfs_btree_cur *cur, /* btree cursor */
114 xfs_agblock_t bno, /* starting block of extent */
115 xfs_extlen_t len, /* length of extent */
116 int *stat) /* success/failure */
117{
118 cur->bc_rec.a.ar_startblock = bno;
119 cur->bc_rec.a.ar_blockcount = len;
120 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
121}
122
123/*
124 * Lookup the first record less than or equal to [bno, len]
125 * in the btree given by cur.
126 */
127STATIC int /* error */
128xfs_alloc_lookup_le(
129 struct xfs_btree_cur *cur, /* btree cursor */
130 xfs_agblock_t bno, /* starting block of extent */
131 xfs_extlen_t len, /* length of extent */
132 int *stat) /* success/failure */
133{
134 cur->bc_rec.a.ar_startblock = bno;
135 cur->bc_rec.a.ar_blockcount = len;
136 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
137}
138
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100139/*
140 * Update the record referred to by cur to the value given
141 * by [bno, len].
142 * This either works (return 0) or gets an EFSCORRUPTED error.
143 */
144STATIC int /* error */
145xfs_alloc_update(
146 struct xfs_btree_cur *cur, /* btree cursor */
147 xfs_agblock_t bno, /* starting block of extent */
148 xfs_extlen_t len) /* length of extent */
149{
150 union xfs_btree_rec rec;
151
152 rec.alloc.ar_startblock = cpu_to_be32(bno);
153 rec.alloc.ar_blockcount = cpu_to_be32(len);
154 return xfs_btree_update(cur, &rec);
155}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100156
157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * Compute aligned version of the found extent.
159 * Takes alignment and min length into account.
160 */
David Chinner12375c82008-04-10 12:21:32 +1000161STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162xfs_alloc_compute_aligned(
163 xfs_agblock_t foundbno, /* starting block in found extent */
164 xfs_extlen_t foundlen, /* length in found extent */
165 xfs_extlen_t alignment, /* alignment for allocation */
166 xfs_extlen_t minlen, /* minimum length for allocation */
167 xfs_agblock_t *resbno, /* result block number */
168 xfs_extlen_t *reslen) /* result length */
169{
170 xfs_agblock_t bno;
171 xfs_extlen_t diff;
172 xfs_extlen_t len;
173
174 if (alignment > 1 && foundlen >= minlen) {
175 bno = roundup(foundbno, alignment);
176 diff = bno - foundbno;
177 len = diff >= foundlen ? 0 : foundlen - diff;
178 } else {
179 bno = foundbno;
180 len = foundlen;
181 }
182 *resbno = bno;
183 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * Compute best start block and diff for "near" allocations.
188 * freelen >= wantlen already checked by caller.
189 */
190STATIC xfs_extlen_t /* difference value (absolute) */
191xfs_alloc_compute_diff(
192 xfs_agblock_t wantbno, /* target starting block */
193 xfs_extlen_t wantlen, /* target length */
194 xfs_extlen_t alignment, /* target alignment */
195 xfs_agblock_t freebno, /* freespace's starting block */
196 xfs_extlen_t freelen, /* freespace's length */
197 xfs_agblock_t *newbnop) /* result: best start block from free */
198{
199 xfs_agblock_t freeend; /* end of freespace extent */
200 xfs_agblock_t newbno1; /* return block number */
201 xfs_agblock_t newbno2; /* other new block number */
202 xfs_extlen_t newlen1=0; /* length with newbno1 */
203 xfs_extlen_t newlen2=0; /* length with newbno2 */
204 xfs_agblock_t wantend; /* end of target extent */
205
206 ASSERT(freelen >= wantlen);
207 freeend = freebno + freelen;
208 wantend = wantbno + wantlen;
209 if (freebno >= wantbno) {
210 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
211 newbno1 = NULLAGBLOCK;
212 } else if (freeend >= wantend && alignment > 1) {
213 newbno1 = roundup(wantbno, alignment);
214 newbno2 = newbno1 - alignment;
215 if (newbno1 >= freeend)
216 newbno1 = NULLAGBLOCK;
217 else
218 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
219 if (newbno2 < freebno)
220 newbno2 = NULLAGBLOCK;
221 else
222 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
223 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
224 if (newlen1 < newlen2 ||
225 (newlen1 == newlen2 &&
226 XFS_ABSDIFF(newbno1, wantbno) >
227 XFS_ABSDIFF(newbno2, wantbno)))
228 newbno1 = newbno2;
229 } else if (newbno2 != NULLAGBLOCK)
230 newbno1 = newbno2;
231 } else if (freeend >= wantend) {
232 newbno1 = wantbno;
233 } else if (alignment > 1) {
234 newbno1 = roundup(freeend - wantlen, alignment);
235 if (newbno1 > freeend - wantlen &&
236 newbno1 - alignment >= freebno)
237 newbno1 -= alignment;
238 else if (newbno1 >= freeend)
239 newbno1 = NULLAGBLOCK;
240 } else
241 newbno1 = freeend - wantlen;
242 *newbnop = newbno1;
243 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
244}
245
246/*
247 * Fix up the length, based on mod and prod.
248 * len should be k * prod + mod for some k.
249 * If len is too small it is returned unchanged.
250 * If len hits maxlen it is left alone.
251 */
252STATIC void
253xfs_alloc_fix_len(
254 xfs_alloc_arg_t *args) /* allocation argument structure */
255{
256 xfs_extlen_t k;
257 xfs_extlen_t rlen;
258
259 ASSERT(args->mod < args->prod);
260 rlen = args->len;
261 ASSERT(rlen >= args->minlen);
262 ASSERT(rlen <= args->maxlen);
263 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
264 (args->mod == 0 && rlen < args->prod))
265 return;
266 k = rlen % args->prod;
267 if (k == args->mod)
268 return;
269 if (k > args->mod) {
270 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
271 return;
272 } else {
273 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
274 (int)args->minlen)
275 return;
276 }
277 ASSERT(rlen >= args->minlen);
278 ASSERT(rlen <= args->maxlen);
279 args->len = rlen;
280}
281
282/*
283 * Fix up length if there is too little space left in the a.g.
284 * Return 1 if ok, 0 if too little, should give up.
285 */
286STATIC int
287xfs_alloc_fix_minleft(
288 xfs_alloc_arg_t *args) /* allocation argument structure */
289{
290 xfs_agf_t *agf; /* a.g. freelist header */
291 int diff; /* free space difference */
292
293 if (args->minleft == 0)
294 return 1;
295 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100296 diff = be32_to_cpu(agf->agf_freeblks)
297 + be32_to_cpu(agf->agf_flcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 - args->len - args->minleft;
299 if (diff >= 0)
300 return 1;
301 args->len += diff; /* shrink the allocated space */
302 if (args->len >= args->minlen)
303 return 1;
304 args->agbno = NULLAGBLOCK;
305 return 0;
306}
307
308/*
309 * Update the two btrees, logically removing from freespace the extent
310 * starting at rbno, rlen blocks. The extent is contained within the
311 * actual (current) free extent fbno for flen blocks.
312 * Flags are passed in indicating whether the cursors are set to the
313 * relevant records.
314 */
315STATIC int /* error code */
316xfs_alloc_fixup_trees(
317 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
318 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
319 xfs_agblock_t fbno, /* starting block of free extent */
320 xfs_extlen_t flen, /* length of free extent */
321 xfs_agblock_t rbno, /* starting block of returned extent */
322 xfs_extlen_t rlen, /* length of returned extent */
323 int flags) /* flags, XFSA_FIXUP_... */
324{
325 int error; /* error code */
326 int i; /* operation results */
327 xfs_agblock_t nfbno1; /* first new free startblock */
328 xfs_agblock_t nfbno2; /* second new free startblock */
329 xfs_extlen_t nflen1=0; /* first new free length */
330 xfs_extlen_t nflen2=0; /* second new free length */
331
332 /*
333 * Look up the record in the by-size tree if necessary.
334 */
335 if (flags & XFSA_FIXUP_CNT_OK) {
336#ifdef DEBUG
337 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
338 return error;
339 XFS_WANT_CORRUPTED_RETURN(
340 i == 1 && nfbno1 == fbno && nflen1 == flen);
341#endif
342 } else {
343 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
344 return error;
345 XFS_WANT_CORRUPTED_RETURN(i == 1);
346 }
347 /*
348 * Look up the record in the by-block tree if necessary.
349 */
350 if (flags & XFSA_FIXUP_BNO_OK) {
351#ifdef DEBUG
352 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
353 return error;
354 XFS_WANT_CORRUPTED_RETURN(
355 i == 1 && nfbno1 == fbno && nflen1 == flen);
356#endif
357 } else {
358 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
359 return error;
360 XFS_WANT_CORRUPTED_RETURN(i == 1);
361 }
362#ifdef DEBUG
363 {
364 xfs_alloc_block_t *bnoblock;
365 xfs_alloc_block_t *cntblock;
366
367 if (bno_cur->bc_nlevels == 1 &&
368 cnt_cur->bc_nlevels == 1) {
369 bnoblock = XFS_BUF_TO_ALLOC_BLOCK(bno_cur->bc_bufs[0]);
370 cntblock = XFS_BUF_TO_ALLOC_BLOCK(cnt_cur->bc_bufs[0]);
371 XFS_WANT_CORRUPTED_RETURN(
Christoph Hellwig16259e72005-11-02 15:11:25 +1100372 be16_to_cpu(bnoblock->bb_numrecs) ==
373 be16_to_cpu(cntblock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 }
376#endif
377 /*
378 * Deal with all four cases: the allocated record is contained
379 * within the freespace record, so we can have new freespace
380 * at either (or both) end, or no freespace remaining.
381 */
382 if (rbno == fbno && rlen == flen)
383 nfbno1 = nfbno2 = NULLAGBLOCK;
384 else if (rbno == fbno) {
385 nfbno1 = rbno + rlen;
386 nflen1 = flen - rlen;
387 nfbno2 = NULLAGBLOCK;
388 } else if (rbno + rlen == fbno + flen) {
389 nfbno1 = fbno;
390 nflen1 = flen - rlen;
391 nfbno2 = NULLAGBLOCK;
392 } else {
393 nfbno1 = fbno;
394 nflen1 = rbno - fbno;
395 nfbno2 = rbno + rlen;
396 nflen2 = (fbno + flen) - nfbno2;
397 }
398 /*
399 * Delete the entry from the by-size btree.
400 */
401 if ((error = xfs_alloc_delete(cnt_cur, &i)))
402 return error;
403 XFS_WANT_CORRUPTED_RETURN(i == 1);
404 /*
405 * Add new by-size btree entry(s).
406 */
407 if (nfbno1 != NULLAGBLOCK) {
408 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
409 return error;
410 XFS_WANT_CORRUPTED_RETURN(i == 0);
411 if ((error = xfs_alloc_insert(cnt_cur, &i)))
412 return error;
413 XFS_WANT_CORRUPTED_RETURN(i == 1);
414 }
415 if (nfbno2 != NULLAGBLOCK) {
416 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
417 return error;
418 XFS_WANT_CORRUPTED_RETURN(i == 0);
419 if ((error = xfs_alloc_insert(cnt_cur, &i)))
420 return error;
421 XFS_WANT_CORRUPTED_RETURN(i == 1);
422 }
423 /*
424 * Fix up the by-block btree entry(s).
425 */
426 if (nfbno1 == NULLAGBLOCK) {
427 /*
428 * No remaining freespace, just delete the by-block tree entry.
429 */
430 if ((error = xfs_alloc_delete(bno_cur, &i)))
431 return error;
432 XFS_WANT_CORRUPTED_RETURN(i == 1);
433 } else {
434 /*
435 * Update the by-block entry to start later|be shorter.
436 */
437 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
438 return error;
439 }
440 if (nfbno2 != NULLAGBLOCK) {
441 /*
442 * 2 resulting free entries, need to add one.
443 */
444 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
445 return error;
446 XFS_WANT_CORRUPTED_RETURN(i == 0);
447 if ((error = xfs_alloc_insert(bno_cur, &i)))
448 return error;
449 XFS_WANT_CORRUPTED_RETURN(i == 1);
450 }
451 return 0;
452}
453
454/*
455 * Read in the allocation group free block array.
456 */
457STATIC int /* error */
458xfs_alloc_read_agfl(
459 xfs_mount_t *mp, /* mount point structure */
460 xfs_trans_t *tp, /* transaction pointer */
461 xfs_agnumber_t agno, /* allocation group number */
462 xfs_buf_t **bpp) /* buffer for the ag free block array */
463{
464 xfs_buf_t *bp; /* return value */
465 int error;
466
467 ASSERT(agno != NULLAGNUMBER);
468 error = xfs_trans_read_buf(
469 mp, tp, mp->m_ddev_targp,
470 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
471 XFS_FSS_TO_BB(mp, 1), 0, &bp);
472 if (error)
473 return error;
474 ASSERT(bp);
475 ASSERT(!XFS_BUF_GETERROR(bp));
476 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
477 *bpp = bp;
478 return 0;
479}
480
481#if defined(XFS_ALLOC_TRACE)
482/*
483 * Add an allocation trace entry for an alloc call.
484 */
485STATIC void
486xfs_alloc_trace_alloc(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000487 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 char *str, /* additional string */
489 xfs_alloc_arg_t *args, /* allocation argument structure */
490 int line) /* source line number */
491{
492 ktrace_enter(xfs_alloc_trace_buf,
493 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
494 (void *)name,
495 (void *)str,
496 (void *)args->mp,
497 (void *)(__psunsigned_t)args->agno,
498 (void *)(__psunsigned_t)args->agbno,
499 (void *)(__psunsigned_t)args->minlen,
500 (void *)(__psunsigned_t)args->maxlen,
501 (void *)(__psunsigned_t)args->mod,
502 (void *)(__psunsigned_t)args->prod,
503 (void *)(__psunsigned_t)args->minleft,
504 (void *)(__psunsigned_t)args->total,
505 (void *)(__psunsigned_t)args->alignment,
506 (void *)(__psunsigned_t)args->len,
507 (void *)((((__psint_t)args->type) << 16) |
508 (__psint_t)args->otype),
509 (void *)(__psint_t)((args->wasdel << 3) |
510 (args->wasfromfl << 2) |
511 (args->isfl << 1) |
512 (args->userdata << 0)));
513}
514
515/*
516 * Add an allocation trace entry for a free call.
517 */
518STATIC void
519xfs_alloc_trace_free(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000520 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char *str, /* additional string */
522 xfs_mount_t *mp, /* file system mount point */
523 xfs_agnumber_t agno, /* allocation group number */
524 xfs_agblock_t agbno, /* a.g. relative block number */
525 xfs_extlen_t len, /* length of extent */
526 int isfl, /* set if is freelist allocation/free */
527 int line) /* source line number */
528{
529 ktrace_enter(xfs_alloc_trace_buf,
530 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
531 (void *)name,
532 (void *)str,
533 (void *)mp,
534 (void *)(__psunsigned_t)agno,
535 (void *)(__psunsigned_t)agbno,
536 (void *)(__psunsigned_t)len,
537 (void *)(__psint_t)isfl,
538 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
539}
540
541/*
542 * Add an allocation trace entry for modifying an agf.
543 */
544STATIC void
545xfs_alloc_trace_modagf(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000546 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 char *str, /* additional string */
548 xfs_mount_t *mp, /* file system mount point */
549 xfs_agf_t *agf, /* new agf value */
550 int flags, /* logging flags for agf */
551 int line) /* source line number */
552{
553 ktrace_enter(xfs_alloc_trace_buf,
554 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
555 (void *)name,
556 (void *)str,
557 (void *)mp,
558 (void *)(__psint_t)flags,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100559 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
560 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
561 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
562 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
563 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
564 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
565 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
566 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
567 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
568 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
569 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572STATIC void
573xfs_alloc_trace_busy(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000574 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 char *str, /* additional string */
Nathan Scottc41564b2006-03-29 08:55:14 +1000576 xfs_mount_t *mp, /* file system mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 xfs_agnumber_t agno, /* allocation group number */
578 xfs_agblock_t agbno, /* a.g. relative block number */
579 xfs_extlen_t len, /* length of extent */
580 int slot, /* perag Busy slot */
581 xfs_trans_t *tp,
582 int trtype, /* type: add, delete, search */
583 int line) /* source line number */
584{
585 ktrace_enter(xfs_alloc_trace_buf,
586 (void *)(__psint_t)(trtype | (line << 16)),
587 (void *)name,
588 (void *)str,
589 (void *)mp,
590 (void *)(__psunsigned_t)agno,
591 (void *)(__psunsigned_t)agbno,
592 (void *)(__psunsigned_t)len,
593 (void *)(__psint_t)slot,
594 (void *)tp,
595 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
596}
597#endif /* XFS_ALLOC_TRACE */
598
599/*
600 * Allocation group level functions.
601 */
602
603/*
604 * Allocate a variable extent in the allocation group agno.
605 * Type and bno are used to determine where in the allocation group the
606 * extent will start.
607 * Extent's length (returned in *len) will be between minlen and maxlen,
608 * and of the form k * prod + mod unless there's nothing that large.
609 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
610 */
611STATIC int /* error */
612xfs_alloc_ag_vextent(
613 xfs_alloc_arg_t *args) /* argument structure for allocation */
614{
615 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 ASSERT(args->minlen > 0);
618 ASSERT(args->maxlen > 0);
619 ASSERT(args->minlen <= args->maxlen);
620 ASSERT(args->mod < args->prod);
621 ASSERT(args->alignment > 0);
622 /*
623 * Branch to correct routine based on the type.
624 */
625 args->wasfromfl = 0;
626 switch (args->type) {
627 case XFS_ALLOCTYPE_THIS_AG:
628 error = xfs_alloc_ag_vextent_size(args);
629 break;
630 case XFS_ALLOCTYPE_NEAR_BNO:
631 error = xfs_alloc_ag_vextent_near(args);
632 break;
633 case XFS_ALLOCTYPE_THIS_BNO:
634 error = xfs_alloc_ag_vextent_exact(args);
635 break;
636 default:
637 ASSERT(0);
638 /* NOTREACHED */
639 }
640 if (error)
641 return error;
642 /*
643 * If the allocation worked, need to change the agf structure
644 * (and log it), and the superblock.
645 */
646 if (args->agbno != NULLAGBLOCK) {
647 xfs_agf_t *agf; /* allocation group freelist header */
648#ifdef XFS_ALLOC_TRACE
649 xfs_mount_t *mp = args->mp;
650#endif
651 long slen = (long)args->len;
652
653 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
654 ASSERT(!(args->wasfromfl) || !args->isfl);
655 ASSERT(args->agbno % args->alignment == 0);
656 if (!(args->wasfromfl)) {
657
658 agf = XFS_BUF_TO_AGF(args->agbp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800659 be32_add_cpu(&agf->agf_freeblks, -(args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfs_trans_agblocks_delta(args->tp,
661 -((long)(args->len)));
662 args->pag->pagf_freeblks -= args->len;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100663 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
664 be32_to_cpu(agf->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
666 xfs_alloc_log_agf(args->tp, args->agbp,
667 XFS_AGF_FREEBLKS);
668 /* search the busylist for these blocks */
669 xfs_alloc_search_busy(args->tp, args->agno,
670 args->agbno, args->len);
671 }
672 if (!args->isfl)
673 xfs_trans_mod_sb(args->tp,
674 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
675 XFS_TRANS_SB_FDBLOCKS, -slen);
676 XFS_STATS_INC(xs_allocx);
677 XFS_STATS_ADD(xs_allocb, args->len);
678 }
679 return 0;
680}
681
682/*
683 * Allocate a variable extent at exactly agno/bno.
684 * Extent's length (returned in *len) will be between minlen and maxlen,
685 * and of the form k * prod + mod unless there's nothing that large.
686 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
687 */
688STATIC int /* error */
689xfs_alloc_ag_vextent_exact(
690 xfs_alloc_arg_t *args) /* allocation argument structure */
691{
692 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
693 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
694 xfs_agblock_t end; /* end of allocated extent */
695 int error;
696 xfs_agblock_t fbno; /* start block of found extent */
697 xfs_agblock_t fend; /* end block of found extent */
698 xfs_extlen_t flen; /* length of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int i; /* success/failure of operation */
700 xfs_agblock_t maxend; /* end of maximal extent */
701 xfs_agblock_t minend; /* end of minimal extent */
702 xfs_extlen_t rlen; /* length of returned extent */
703
704 ASSERT(args->alignment == 1);
705 /*
706 * Allocate/initialize a cursor for the by-number freespace btree.
707 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100708 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
709 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
712 * Look for the closest free block <= bno, it must contain bno
713 * if any free block does.
714 */
715 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
716 goto error0;
717 if (!i) {
718 /*
719 * Didn't find it, return null.
720 */
721 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
722 args->agbno = NULLAGBLOCK;
723 return 0;
724 }
725 /*
726 * Grab the freespace record.
727 */
728 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
729 goto error0;
730 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
731 ASSERT(fbno <= args->agbno);
732 minend = args->agbno + args->minlen;
733 maxend = args->agbno + args->maxlen;
734 fend = fbno + flen;
735 /*
736 * Give up if the freespace isn't long enough for the minimum request.
737 */
738 if (fend < minend) {
739 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
740 args->agbno = NULLAGBLOCK;
741 return 0;
742 }
743 /*
744 * End of extent will be smaller of the freespace end and the
745 * maximal requested end.
746 */
747 end = XFS_AGBLOCK_MIN(fend, maxend);
748 /*
749 * Fix the length according to mod and prod if given.
750 */
751 args->len = end - args->agbno;
752 xfs_alloc_fix_len(args);
753 if (!xfs_alloc_fix_minleft(args)) {
754 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
755 return 0;
756 }
757 rlen = args->len;
758 ASSERT(args->agbno + rlen <= fend);
759 end = args->agbno + rlen;
760 /*
761 * We are allocating agbno for rlen [agbno .. end]
762 * Allocate/initialize a cursor for the by-size btree.
763 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100764 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
765 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100767 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
769 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
770 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
771 goto error0;
772 }
773 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
774 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
775 TRACE_ALLOC("normal", args);
776 args->wasfromfl = 0;
777 return 0;
778
779error0:
780 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
781 TRACE_ALLOC("error", args);
782 return error;
783}
784
785/*
786 * Allocate a variable extent near bno in the allocation group agno.
787 * Extent's length (returned in len) will be between minlen and maxlen,
788 * and of the form k * prod + mod unless there's nothing that large.
789 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
790 */
791STATIC int /* error */
792xfs_alloc_ag_vextent_near(
793 xfs_alloc_arg_t *args) /* allocation argument structure */
794{
795 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
796 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
797 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_agblock_t gtbno; /* start bno of right side entry */
799 xfs_agblock_t gtbnoa; /* aligned ... */
800 xfs_extlen_t gtdiff; /* difference to right side entry */
801 xfs_extlen_t gtlen; /* length of right side entry */
802 xfs_extlen_t gtlena; /* aligned ... */
803 xfs_agblock_t gtnew; /* useful start bno of right side */
804 int error; /* error code */
805 int i; /* result code, temporary */
806 int j; /* result code, temporary */
807 xfs_agblock_t ltbno; /* start bno of left side entry */
808 xfs_agblock_t ltbnoa; /* aligned ... */
809 xfs_extlen_t ltdiff; /* difference to left side entry */
810 /*REFERENCED*/
811 xfs_agblock_t ltend; /* end bno of left side entry */
812 xfs_extlen_t ltlen; /* length of left side entry */
813 xfs_extlen_t ltlena; /* aligned ... */
814 xfs_agblock_t ltnew; /* useful start bno of left side */
815 xfs_extlen_t rlen; /* length of returned extent */
816#if defined(DEBUG) && defined(__KERNEL__)
817 /*
818 * Randomly don't execute the first algorithm.
819 */
820 int dofirst; /* set to do first algorithm */
821
Joe Perchese7a23a92007-05-08 13:49:03 +1000822 dofirst = random32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#endif
824 /*
825 * Get a cursor for the by-size btree.
826 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100827 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
828 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ltlen = 0;
830 bno_cur_lt = bno_cur_gt = NULL;
831 /*
832 * See if there are any free extents as big as maxlen.
833 */
834 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
835 goto error0;
836 /*
837 * If none, then pick up the last entry in the tree unless the
838 * tree is empty.
839 */
840 if (!i) {
841 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
842 &ltlen, &i)))
843 goto error0;
844 if (i == 0 || ltlen == 0) {
845 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
846 return 0;
847 }
848 ASSERT(i == 1);
849 }
850 args->wasfromfl = 0;
851 /*
852 * First algorithm.
853 * If the requested extent is large wrt the freespaces available
854 * in this a.g., then the cursor will be pointing to a btree entry
855 * near the right edge of the tree. If it's in the last btree leaf
856 * block, then we just examine all the entries in that block
857 * that are big enough, and pick the best one.
858 * This is written as a while loop so we can break out of it,
859 * but we never loop back to the top.
860 */
861 while (xfs_btree_islastblock(cnt_cur, 0)) {
862 xfs_extlen_t bdiff;
863 int besti=0;
864 xfs_extlen_t blen=0;
865 xfs_agblock_t bnew=0;
866
867#if defined(DEBUG) && defined(__KERNEL__)
868 if (!dofirst)
869 break;
870#endif
871 /*
872 * Start from the entry that lookup found, sequence through
873 * all larger free blocks. If we're actually pointing at a
874 * record smaller than maxlen, go to the start of this block,
875 * and skip all those smaller than minlen.
876 */
877 if (ltlen || args->alignment > 1) {
878 cnt_cur->bc_ptrs[0] = 1;
879 do {
880 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
881 &ltlen, &i)))
882 goto error0;
883 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
884 if (ltlen >= args->minlen)
885 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100886 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto error0;
888 } while (i);
889 ASSERT(ltlen >= args->minlen);
890 if (!i)
891 break;
892 }
893 i = cnt_cur->bc_ptrs[0];
894 for (j = 1, blen = 0, bdiff = 0;
895 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100896 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * For each entry, decide if it's better than
899 * the previous best entry.
900 */
901 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
902 goto error0;
903 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +1000904 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
905 args->minlen, &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +1000906 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 continue;
908 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
909 xfs_alloc_fix_len(args);
910 ASSERT(args->len >= args->minlen);
911 if (args->len < blen)
912 continue;
913 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
914 args->alignment, ltbno, ltlen, &ltnew);
915 if (ltnew != NULLAGBLOCK &&
916 (args->len > blen || ltdiff < bdiff)) {
917 bdiff = ltdiff;
918 bnew = ltnew;
919 blen = args->len;
920 besti = cnt_cur->bc_ptrs[0];
921 }
922 }
923 /*
924 * It didn't work. We COULD be in a case where
925 * there's a good record somewhere, so try again.
926 */
927 if (blen == 0)
928 break;
929 /*
930 * Point at the best entry, and retrieve it again.
931 */
932 cnt_cur->bc_ptrs[0] = besti;
933 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
934 goto error0;
935 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
936 ltend = ltbno + ltlen;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100937 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 args->len = blen;
939 if (!xfs_alloc_fix_minleft(args)) {
940 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
941 TRACE_ALLOC("nominleft", args);
942 return 0;
943 }
944 blen = args->len;
945 /*
946 * We are allocating starting at bnew for blen blocks.
947 */
948 args->agbno = bnew;
949 ASSERT(bnew >= ltbno);
950 ASSERT(bnew + blen <= ltend);
951 /*
952 * Set up a cursor for the by-bno tree.
953 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100954 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
955 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * Fix up the btree entries.
958 */
959 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
960 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
961 goto error0;
962 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
963 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
964 TRACE_ALLOC("first", args);
965 return 0;
966 }
967 /*
968 * Second algorithm.
969 * Search in the by-bno tree to the left and to the right
970 * simultaneously, until in each case we find a space big enough,
971 * or run into the edge of the tree. When we run into the edge,
972 * we deallocate that cursor.
973 * If both searches succeed, we compare the two spaces and pick
974 * the better one.
975 * With alignment, it's possible for both to fail; the upper
976 * level algorithm that picks allocation groups for allocations
977 * is not supposed to do this.
978 */
979 /*
980 * Allocate and initialize the cursor for the leftward search.
981 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100982 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
983 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * Lookup <= bno to find the leftward search's starting point.
986 */
987 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
988 goto error0;
989 if (!i) {
990 /*
991 * Didn't find anything; use this cursor for the rightward
992 * search.
993 */
994 bno_cur_gt = bno_cur_lt;
995 bno_cur_lt = NULL;
996 }
997 /*
998 * Found something. Duplicate the cursor for the rightward search.
999 */
1000 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1001 goto error0;
1002 /*
1003 * Increment the cursor, so we will point at the entry just right
1004 * of the leftward entry if any, or to the leftmost entry.
1005 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001006 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto error0;
1008 if (!i) {
1009 /*
1010 * It failed, there are no rightward entries.
1011 */
1012 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1013 bno_cur_gt = NULL;
1014 }
1015 /*
1016 * Loop going left with the leftward cursor, right with the
1017 * rightward cursor, until either both directions give up or
1018 * we find an entry at least as big as minlen.
1019 */
1020 do {
1021 if (bno_cur_lt) {
1022 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1023 goto error0;
1024 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +10001025 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
1026 args->minlen, &ltbnoa, &ltlena);
1027 if (ltlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001029 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto error0;
1031 if (!i) {
1032 xfs_btree_del_cursor(bno_cur_lt,
1033 XFS_BTREE_NOERROR);
1034 bno_cur_lt = NULL;
1035 }
1036 }
1037 if (bno_cur_gt) {
1038 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1039 goto error0;
1040 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +10001041 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
1042 args->minlen, &gtbnoa, &gtlena);
1043 if (gtlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001045 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 goto error0;
1047 if (!i) {
1048 xfs_btree_del_cursor(bno_cur_gt,
1049 XFS_BTREE_NOERROR);
1050 bno_cur_gt = NULL;
1051 }
1052 }
1053 } while (bno_cur_lt || bno_cur_gt);
1054 /*
1055 * Got both cursors still active, need to find better entry.
1056 */
1057 if (bno_cur_lt && bno_cur_gt) {
1058 /*
1059 * Left side is long enough, look for a right side entry.
1060 */
1061 if (ltlena >= args->minlen) {
1062 /*
1063 * Fix up the length.
1064 */
1065 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1066 xfs_alloc_fix_len(args);
1067 rlen = args->len;
1068 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1069 args->alignment, ltbno, ltlen, &ltnew);
1070 /*
1071 * Not perfect.
1072 */
1073 if (ltdiff) {
1074 /*
1075 * Look until we find a better one, run out of
1076 * space, or run off the end.
1077 */
1078 while (bno_cur_lt && bno_cur_gt) {
1079 if ((error = xfs_alloc_get_rec(
1080 bno_cur_gt, &gtbno,
1081 &gtlen, &i)))
1082 goto error0;
1083 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1084 xfs_alloc_compute_aligned(gtbno, gtlen,
1085 args->alignment, args->minlen,
1086 &gtbnoa, &gtlena);
1087 /*
1088 * The left one is clearly better.
1089 */
1090 if (gtbnoa >= args->agbno + ltdiff) {
1091 xfs_btree_del_cursor(
1092 bno_cur_gt,
1093 XFS_BTREE_NOERROR);
1094 bno_cur_gt = NULL;
1095 break;
1096 }
1097 /*
1098 * If we reach a big enough entry,
1099 * compare the two and pick the best.
1100 */
1101 if (gtlena >= args->minlen) {
1102 args->len =
1103 XFS_EXTLEN_MIN(gtlena,
1104 args->maxlen);
1105 xfs_alloc_fix_len(args);
1106 rlen = args->len;
1107 gtdiff = xfs_alloc_compute_diff(
1108 args->agbno, rlen,
1109 args->alignment,
1110 gtbno, gtlen, &gtnew);
1111 /*
1112 * Right side is better.
1113 */
1114 if (gtdiff < ltdiff) {
1115 xfs_btree_del_cursor(
1116 bno_cur_lt,
1117 XFS_BTREE_NOERROR);
1118 bno_cur_lt = NULL;
1119 }
1120 /*
1121 * Left side is better.
1122 */
1123 else {
1124 xfs_btree_del_cursor(
1125 bno_cur_gt,
1126 XFS_BTREE_NOERROR);
1127 bno_cur_gt = NULL;
1128 }
1129 break;
1130 }
1131 /*
1132 * Fell off the right end.
1133 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001134 if ((error = xfs_btree_increment(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bno_cur_gt, 0, &i)))
1136 goto error0;
1137 if (!i) {
1138 xfs_btree_del_cursor(
1139 bno_cur_gt,
1140 XFS_BTREE_NOERROR);
1141 bno_cur_gt = NULL;
1142 break;
1143 }
1144 }
1145 }
1146 /*
1147 * The left side is perfect, trash the right side.
1148 */
1149 else {
1150 xfs_btree_del_cursor(bno_cur_gt,
1151 XFS_BTREE_NOERROR);
1152 bno_cur_gt = NULL;
1153 }
1154 }
1155 /*
1156 * It's the right side that was found first, look left.
1157 */
1158 else {
1159 /*
1160 * Fix up the length.
1161 */
1162 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1163 xfs_alloc_fix_len(args);
1164 rlen = args->len;
1165 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1166 args->alignment, gtbno, gtlen, &gtnew);
1167 /*
1168 * Right side entry isn't perfect.
1169 */
1170 if (gtdiff) {
1171 /*
1172 * Look until we find a better one, run out of
1173 * space, or run off the end.
1174 */
1175 while (bno_cur_lt && bno_cur_gt) {
1176 if ((error = xfs_alloc_get_rec(
1177 bno_cur_lt, &ltbno,
1178 &ltlen, &i)))
1179 goto error0;
1180 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1181 xfs_alloc_compute_aligned(ltbno, ltlen,
1182 args->alignment, args->minlen,
1183 &ltbnoa, &ltlena);
1184 /*
1185 * The right one is clearly better.
1186 */
1187 if (ltbnoa <= args->agbno - gtdiff) {
1188 xfs_btree_del_cursor(
1189 bno_cur_lt,
1190 XFS_BTREE_NOERROR);
1191 bno_cur_lt = NULL;
1192 break;
1193 }
1194 /*
1195 * If we reach a big enough entry,
1196 * compare the two and pick the best.
1197 */
1198 if (ltlena >= args->minlen) {
1199 args->len = XFS_EXTLEN_MIN(
1200 ltlena, args->maxlen);
1201 xfs_alloc_fix_len(args);
1202 rlen = args->len;
1203 ltdiff = xfs_alloc_compute_diff(
1204 args->agbno, rlen,
1205 args->alignment,
1206 ltbno, ltlen, &ltnew);
1207 /*
1208 * Left side is better.
1209 */
1210 if (ltdiff < gtdiff) {
1211 xfs_btree_del_cursor(
1212 bno_cur_gt,
1213 XFS_BTREE_NOERROR);
1214 bno_cur_gt = NULL;
1215 }
1216 /*
1217 * Right side is better.
1218 */
1219 else {
1220 xfs_btree_del_cursor(
1221 bno_cur_lt,
1222 XFS_BTREE_NOERROR);
1223 bno_cur_lt = NULL;
1224 }
1225 break;
1226 }
1227 /*
1228 * Fell off the left end.
1229 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001230 if ((error = xfs_btree_decrement(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 bno_cur_lt, 0, &i)))
1232 goto error0;
1233 if (!i) {
1234 xfs_btree_del_cursor(bno_cur_lt,
1235 XFS_BTREE_NOERROR);
1236 bno_cur_lt = NULL;
1237 break;
1238 }
1239 }
1240 }
1241 /*
1242 * The right side is perfect, trash the left side.
1243 */
1244 else {
1245 xfs_btree_del_cursor(bno_cur_lt,
1246 XFS_BTREE_NOERROR);
1247 bno_cur_lt = NULL;
1248 }
1249 }
1250 }
1251 /*
1252 * If we couldn't get anything, give up.
1253 */
1254 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1255 TRACE_ALLOC("neither", args);
1256 args->agbno = NULLAGBLOCK;
1257 return 0;
1258 }
1259 /*
1260 * At this point we have selected a freespace entry, either to the
1261 * left or to the right. If it's on the right, copy all the
1262 * useful variables to the "left" set so we only have one
1263 * copy of this code.
1264 */
1265 if (bno_cur_gt) {
1266 bno_cur_lt = bno_cur_gt;
1267 bno_cur_gt = NULL;
1268 ltbno = gtbno;
1269 ltbnoa = gtbnoa;
1270 ltlen = gtlen;
1271 ltlena = gtlena;
1272 j = 1;
1273 } else
1274 j = 0;
1275 /*
1276 * Fix up the length and compute the useful address.
1277 */
1278 ltend = ltbno + ltlen;
1279 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1280 xfs_alloc_fix_len(args);
1281 if (!xfs_alloc_fix_minleft(args)) {
1282 TRACE_ALLOC("nominleft", args);
1283 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1284 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1285 return 0;
1286 }
1287 rlen = args->len;
1288 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1289 ltlen, &ltnew);
1290 ASSERT(ltnew >= ltbno);
1291 ASSERT(ltnew + rlen <= ltend);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001292 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 args->agbno = ltnew;
1294 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1295 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1296 goto error0;
1297 TRACE_ALLOC(j ? "gt" : "lt", args);
1298 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1299 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1300 return 0;
1301
1302 error0:
1303 TRACE_ALLOC("error", args);
1304 if (cnt_cur != NULL)
1305 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1306 if (bno_cur_lt != NULL)
1307 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1308 if (bno_cur_gt != NULL)
1309 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1310 return error;
1311}
1312
1313/*
1314 * Allocate a variable extent anywhere in the allocation group agno.
1315 * Extent's length (returned in len) will be between minlen and maxlen,
1316 * and of the form k * prod + mod unless there's nothing that large.
1317 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1318 */
1319STATIC int /* error */
1320xfs_alloc_ag_vextent_size(
1321 xfs_alloc_arg_t *args) /* allocation argument structure */
1322{
1323 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1324 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1325 int error; /* error result */
1326 xfs_agblock_t fbno; /* start of found freespace */
1327 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int i; /* temp status variable */
1329 xfs_agblock_t rbno; /* returned block number */
1330 xfs_extlen_t rlen; /* length of returned extent */
1331
1332 /*
1333 * Allocate and initialize a cursor for the by-size btree.
1334 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001335 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1336 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 bno_cur = NULL;
1338 /*
1339 * Look for an entry >= maxlen+alignment-1 blocks.
1340 */
1341 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1342 args->maxlen + args->alignment - 1, &i)))
1343 goto error0;
1344 /*
1345 * If none, then pick up the last entry in the tree unless the
1346 * tree is empty.
1347 */
1348 if (!i) {
1349 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1350 &flen, &i)))
1351 goto error0;
1352 if (i == 0 || flen == 0) {
1353 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1354 TRACE_ALLOC("noentry", args);
1355 return 0;
1356 }
1357 ASSERT(i == 1);
1358 }
1359 /*
1360 * There's a freespace as big as maxlen+alignment-1, get it.
1361 */
1362 else {
1363 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1364 goto error0;
1365 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1366 }
1367 /*
1368 * In the first case above, we got the last entry in the
1369 * by-size btree. Now we check to see if the space hits maxlen
1370 * once aligned; if not, we search left for something better.
1371 * This can't happen in the second case above.
1372 */
1373 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1374 &rbno, &rlen);
1375 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1376 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1377 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1378 if (rlen < args->maxlen) {
1379 xfs_agblock_t bestfbno;
1380 xfs_extlen_t bestflen;
1381 xfs_agblock_t bestrbno;
1382 xfs_extlen_t bestrlen;
1383
1384 bestrlen = rlen;
1385 bestrbno = rbno;
1386 bestflen = flen;
1387 bestfbno = fbno;
1388 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001389 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto error0;
1391 if (i == 0)
1392 break;
1393 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1394 &i)))
1395 goto error0;
1396 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1397 if (flen < bestrlen)
1398 break;
1399 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1400 args->minlen, &rbno, &rlen);
1401 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1402 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1403 (rlen <= flen && rbno + rlen <= fbno + flen),
1404 error0);
1405 if (rlen > bestrlen) {
1406 bestrlen = rlen;
1407 bestrbno = rbno;
1408 bestflen = flen;
1409 bestfbno = fbno;
1410 if (rlen == args->maxlen)
1411 break;
1412 }
1413 }
1414 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1415 &i)))
1416 goto error0;
1417 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1418 rlen = bestrlen;
1419 rbno = bestrbno;
1420 flen = bestflen;
1421 fbno = bestfbno;
1422 }
1423 args->wasfromfl = 0;
1424 /*
1425 * Fix up the length.
1426 */
1427 args->len = rlen;
1428 xfs_alloc_fix_len(args);
1429 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1430 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1431 TRACE_ALLOC("nominleft", args);
1432 args->agbno = NULLAGBLOCK;
1433 return 0;
1434 }
1435 rlen = args->len;
1436 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1437 /*
1438 * Allocate and initialize a cursor for the by-block tree.
1439 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001440 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1441 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1443 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1444 goto error0;
1445 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1446 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1447 cnt_cur = bno_cur = NULL;
1448 args->len = rlen;
1449 args->agbno = rbno;
1450 XFS_WANT_CORRUPTED_GOTO(
1451 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001452 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 error0);
1454 TRACE_ALLOC("normal", args);
1455 return 0;
1456
1457error0:
1458 TRACE_ALLOC("error", args);
1459 if (cnt_cur)
1460 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1461 if (bno_cur)
1462 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1463 return error;
1464}
1465
1466/*
1467 * Deal with the case where only small freespaces remain.
1468 * Either return the contents of the last freespace record,
1469 * or allocate space from the freelist if there is nothing in the tree.
1470 */
1471STATIC int /* error */
1472xfs_alloc_ag_vextent_small(
1473 xfs_alloc_arg_t *args, /* allocation argument structure */
1474 xfs_btree_cur_t *ccur, /* by-size cursor */
1475 xfs_agblock_t *fbnop, /* result block number */
1476 xfs_extlen_t *flenp, /* result length */
1477 int *stat) /* status: 0-freelist, 1-normal/none */
1478{
1479 int error;
1480 xfs_agblock_t fbno;
1481 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int i;
1483
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001484 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto error0;
1486 if (i) {
1487 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1488 goto error0;
1489 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1490 }
1491 /*
1492 * Nothing in the btree, try the freelist. Make sure
1493 * to respect minleft even when pulling from the
1494 * freelist.
1495 */
1496 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001497 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1498 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001499 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1500 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 goto error0;
1502 if (fbno != NULLAGBLOCK) {
1503 if (args->userdata) {
1504 xfs_buf_t *bp;
1505
1506 bp = xfs_btree_get_bufs(args->mp, args->tp,
1507 args->agno, fbno, 0);
1508 xfs_trans_binval(args->tp, bp);
1509 }
1510 args->len = 1;
1511 args->agbno = fbno;
1512 XFS_WANT_CORRUPTED_GOTO(
1513 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001514 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 error0);
1516 args->wasfromfl = 1;
1517 TRACE_ALLOC("freelist", args);
1518 *stat = 0;
1519 return 0;
1520 }
1521 /*
1522 * Nothing in the freelist.
1523 */
1524 else
1525 flen = 0;
1526 }
1527 /*
1528 * Can't allocate from the freelist for some reason.
1529 */
Nathan Scottd432c802006-09-28 11:03:44 +10001530 else {
1531 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
1535 * Can't do the allocation, give up.
1536 */
1537 if (flen < args->minlen) {
1538 args->agbno = NULLAGBLOCK;
1539 TRACE_ALLOC("notenough", args);
1540 flen = 0;
1541 }
1542 *fbnop = fbno;
1543 *flenp = flen;
1544 *stat = 1;
1545 TRACE_ALLOC("normal", args);
1546 return 0;
1547
1548error0:
1549 TRACE_ALLOC("error", args);
1550 return error;
1551}
1552
1553/*
1554 * Free the extent starting at agno/bno for length.
1555 */
1556STATIC int /* error */
1557xfs_free_ag_extent(
1558 xfs_trans_t *tp, /* transaction pointer */
1559 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1560 xfs_agnumber_t agno, /* allocation group number */
1561 xfs_agblock_t bno, /* starting block number */
1562 xfs_extlen_t len, /* length of extent */
1563 int isfl) /* set if is freelist blocks - no sb acctg */
1564{
1565 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1566 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1567 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 xfs_agblock_t gtbno; /* start of right neighbor block */
1569 xfs_extlen_t gtlen; /* length of right neighbor block */
1570 int haveleft; /* have a left neighbor block */
1571 int haveright; /* have a right neighbor block */
1572 int i; /* temp, result code */
1573 xfs_agblock_t ltbno; /* start of left neighbor block */
1574 xfs_extlen_t ltlen; /* length of left neighbor block */
1575 xfs_mount_t *mp; /* mount point struct for filesystem */
1576 xfs_agblock_t nbno; /* new starting block of freespace */
1577 xfs_extlen_t nlen; /* new length of freespace */
1578
1579 mp = tp->t_mountp;
1580 /*
1581 * Allocate and initialize a cursor for the by-block btree.
1582 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001583 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 cnt_cur = NULL;
1585 /*
1586 * Look for a neighboring block on the left (lower block numbers)
1587 * that is contiguous with this space.
1588 */
1589 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1590 goto error0;
1591 if (haveleft) {
1592 /*
1593 * There is a block to our left.
1594 */
1595 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1596 goto error0;
1597 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1598 /*
1599 * It's not contiguous, though.
1600 */
1601 if (ltbno + ltlen < bno)
1602 haveleft = 0;
1603 else {
1604 /*
1605 * If this failure happens the request to free this
1606 * space was invalid, it's (partly) already free.
1607 * Very bad.
1608 */
1609 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1610 }
1611 }
1612 /*
1613 * Look for a neighboring block on the right (higher block numbers)
1614 * that is contiguous with this space.
1615 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001616 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto error0;
1618 if (haveright) {
1619 /*
1620 * There is a block to our right.
1621 */
1622 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1623 goto error0;
1624 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1625 /*
1626 * It's not contiguous, though.
1627 */
1628 if (bno + len < gtbno)
1629 haveright = 0;
1630 else {
1631 /*
1632 * If this failure happens the request to free this
1633 * space was invalid, it's (partly) already free.
1634 * Very bad.
1635 */
1636 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1637 }
1638 }
1639 /*
1640 * Now allocate and initialize a cursor for the by-size tree.
1641 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001642 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * Have both left and right contiguous neighbors.
1645 * Merge all three into a single free block.
1646 */
1647 if (haveleft && haveright) {
1648 /*
1649 * Delete the old by-size entry on the left.
1650 */
1651 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1652 goto error0;
1653 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1654 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1655 goto error0;
1656 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1657 /*
1658 * Delete the old by-size entry on the right.
1659 */
1660 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1661 goto error0;
1662 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1663 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1664 goto error0;
1665 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1666 /*
1667 * Delete the old by-block entry for the right block.
1668 */
1669 if ((error = xfs_alloc_delete(bno_cur, &i)))
1670 goto error0;
1671 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1672 /*
1673 * Move the by-block cursor back to the left neighbor.
1674 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001675 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto error0;
1677 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1678#ifdef DEBUG
1679 /*
1680 * Check that this is the right record: delete didn't
1681 * mangle the cursor.
1682 */
1683 {
1684 xfs_agblock_t xxbno;
1685 xfs_extlen_t xxlen;
1686
1687 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1688 &i)))
1689 goto error0;
1690 XFS_WANT_CORRUPTED_GOTO(
1691 i == 1 && xxbno == ltbno && xxlen == ltlen,
1692 error0);
1693 }
1694#endif
1695 /*
1696 * Update remaining by-block entry to the new, joined block.
1697 */
1698 nbno = ltbno;
1699 nlen = len + ltlen + gtlen;
1700 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1701 goto error0;
1702 }
1703 /*
1704 * Have only a left contiguous neighbor.
1705 * Merge it together with the new freespace.
1706 */
1707 else if (haveleft) {
1708 /*
1709 * Delete the old by-size entry on the left.
1710 */
1711 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1712 goto error0;
1713 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1714 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1715 goto error0;
1716 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1717 /*
1718 * Back up the by-block cursor to the left neighbor, and
1719 * update its length.
1720 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001721 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto error0;
1723 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1724 nbno = ltbno;
1725 nlen = len + ltlen;
1726 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1727 goto error0;
1728 }
1729 /*
1730 * Have only a right contiguous neighbor.
1731 * Merge it together with the new freespace.
1732 */
1733 else if (haveright) {
1734 /*
1735 * Delete the old by-size entry on the right.
1736 */
1737 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1738 goto error0;
1739 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1740 if ((error = xfs_alloc_delete(cnt_cur, &i)))
1741 goto error0;
1742 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1743 /*
1744 * Update the starting block and length of the right
1745 * neighbor in the by-block tree.
1746 */
1747 nbno = bno;
1748 nlen = len + gtlen;
1749 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1750 goto error0;
1751 }
1752 /*
1753 * No contiguous neighbors.
1754 * Insert the new freespace into the by-block tree.
1755 */
1756 else {
1757 nbno = bno;
1758 nlen = len;
1759 if ((error = xfs_alloc_insert(bno_cur, &i)))
1760 goto error0;
1761 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1762 }
1763 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1764 bno_cur = NULL;
1765 /*
1766 * In all cases we need to insert the new freespace in the by-size tree.
1767 */
1768 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1769 goto error0;
1770 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
1771 if ((error = xfs_alloc_insert(cnt_cur, &i)))
1772 goto error0;
1773 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1774 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1775 cnt_cur = NULL;
1776 /*
1777 * Update the freespace totals in the ag and superblock.
1778 */
1779 {
1780 xfs_agf_t *agf;
1781 xfs_perag_t *pag; /* per allocation group data */
1782
1783 agf = XFS_BUF_TO_AGF(agbp);
1784 pag = &mp->m_perag[agno];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001785 be32_add_cpu(&agf->agf_freeblks, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 xfs_trans_agblocks_delta(tp, len);
1787 pag->pagf_freeblks += len;
1788 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig16259e72005-11-02 15:11:25 +11001789 be32_to_cpu(agf->agf_freeblks) <=
1790 be32_to_cpu(agf->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 error0);
1792 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1793 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1794 if (!isfl)
1795 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1796 XFS_STATS_INC(xs_freex);
1797 XFS_STATS_ADD(xs_freeb, len);
1798 }
1799 TRACE_FREE(haveleft ?
1800 (haveright ? "both" : "left") :
1801 (haveright ? "right" : "none"),
1802 agno, bno, len, isfl);
1803
1804 /*
1805 * Since blocks move to the free list without the coordination
1806 * used in xfs_bmap_finish, we can't allow block to be available
1807 * for reallocation and non-transaction writing (user data)
1808 * until we know that the transaction that moved it to the free
1809 * list is permanently on disk. We track the blocks by declaring
1810 * these blocks as "busy"; the busy list is maintained on a per-ag
1811 * basis and each transaction records which entries should be removed
1812 * when the iclog commits to disk. If a busy block is allocated,
1813 * the iclog is pushed up to the LSN that freed the block.
1814 */
1815 xfs_alloc_mark_busy(tp, agno, bno, len);
1816 return 0;
1817
1818 error0:
1819 TRACE_FREE("error", agno, bno, len, isfl);
1820 if (bno_cur)
1821 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1822 if (cnt_cur)
1823 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1824 return error;
1825}
1826
1827/*
1828 * Visible (exported) allocation/free functions.
1829 * Some of these are used just by xfs_alloc_btree.c and this file.
1830 */
1831
1832/*
1833 * Compute and fill in value of m_ag_maxlevels.
1834 */
1835void
1836xfs_alloc_compute_maxlevels(
1837 xfs_mount_t *mp) /* file system mount structure */
1838{
1839 int level;
1840 uint maxblocks;
1841 uint maxleafents;
1842 int minleafrecs;
1843 int minnoderecs;
1844
1845 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1846 minleafrecs = mp->m_alloc_mnr[0];
1847 minnoderecs = mp->m_alloc_mnr[1];
1848 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1849 for (level = 1; maxblocks > 1; level++)
1850 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1851 mp->m_ag_maxlevels = level;
1852}
1853
1854/*
1855 * Decide whether to use this allocation group for this allocation.
1856 * If so, fix up the btree freelist's size.
1857 */
1858STATIC int /* error */
1859xfs_alloc_fix_freelist(
1860 xfs_alloc_arg_t *args, /* allocation argument structure */
1861 int flags) /* XFS_ALLOC_FLAG_... */
1862{
1863 xfs_buf_t *agbp; /* agf buffer pointer */
1864 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1865 xfs_buf_t *agflbp;/* agfl buffer pointer */
1866 xfs_agblock_t bno; /* freelist block */
1867 xfs_extlen_t delta; /* new blocks needed in freelist */
1868 int error; /* error result code */
1869 xfs_extlen_t longest;/* longest extent in allocation group */
1870 xfs_mount_t *mp; /* file system mount point structure */
1871 xfs_extlen_t need; /* total blocks needed in freelist */
1872 xfs_perag_t *pag; /* per-ag information structure */
1873 xfs_alloc_arg_t targs; /* local allocation arguments */
1874 xfs_trans_t *tp; /* transaction pointer */
1875
1876 mp = args->mp;
1877
1878 pag = args->pag;
1879 tp = args->tp;
1880 if (!pag->pagf_init) {
1881 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1882 &agbp)))
1883 return error;
1884 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001885 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1886 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 args->agbp = NULL;
1888 return 0;
1889 }
1890 } else
1891 agbp = NULL;
1892
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001893 /*
1894 * If this is a metadata preferred pag and we are user data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * then try somewhere else if we are not being asked to
1896 * try harder at this point
1897 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001898 if (pag->pagf_metadata && args->userdata &&
1899 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1900 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 args->agbp = NULL;
1902 return 0;
1903 }
1904
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001905 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1906 need = XFS_MIN_FREELIST_PAG(pag, mp);
1907 delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
1908 /*
1909 * If it looks like there isn't a long enough extent, or enough
1910 * total blocks, reject it.
1911 */
1912 longest = (pag->pagf_longest > delta) ?
1913 (pag->pagf_longest - delta) :
1914 (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
1915 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1916 longest ||
1917 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1918 need - args->total) < (int)args->minleft)) {
1919 if (agbp)
1920 xfs_trans_brelse(tp, agbp);
1921 args->agbp = NULL;
1922 return 0;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
1927 * Get the a.g. freespace buffer.
1928 * Can fail if we're not blocking on locks, and it's held.
1929 */
1930 if (agbp == NULL) {
1931 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1932 &agbp)))
1933 return error;
1934 if (agbp == NULL) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001935 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1936 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 args->agbp = NULL;
1938 return 0;
1939 }
1940 }
1941 /*
1942 * Figure out how many blocks we should have in the freelist.
1943 */
1944 agf = XFS_BUF_TO_AGF(agbp);
1945 need = XFS_MIN_FREELIST(agf, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * If there isn't enough total or single-extent, reject it.
1948 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001949 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1950 delta = need > be32_to_cpu(agf->agf_flcount) ?
1951 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1952 longest = be32_to_cpu(agf->agf_longest);
1953 longest = (longest > delta) ? (longest - delta) :
1954 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1955 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1956 longest ||
1957 ((int)(be32_to_cpu(agf->agf_freeblks) +
1958 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1959 (int)args->minleft)) {
1960 xfs_trans_brelse(tp, agbp);
1961 args->agbp = NULL;
1962 return 0;
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 /*
1966 * Make the freelist shorter if it's too long.
1967 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001968 while (be32_to_cpu(agf->agf_flcount) > need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 xfs_buf_t *bp;
1970
David Chinner92821e22007-05-24 15:26:31 +10001971 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
1972 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return error;
1974 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
1975 return error;
1976 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
1977 xfs_trans_binval(tp, bp);
1978 }
1979 /*
1980 * Initialize the args structure.
1981 */
1982 targs.tp = tp;
1983 targs.mp = mp;
1984 targs.agbp = agbp;
1985 targs.agno = args->agno;
1986 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
1987 targs.minalignslop = 0;
1988 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
1989 targs.type = XFS_ALLOCTYPE_THIS_AG;
1990 targs.pag = pag;
1991 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
1992 return error;
1993 /*
1994 * Make the freelist longer if it's too short.
1995 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001996 while (be32_to_cpu(agf->agf_flcount) < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 targs.agbno = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11001998 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /*
2000 * Allocate as many blocks as possible at once.
2001 */
Nathan Scotte63a3692006-05-08 19:51:58 +10002002 if ((error = xfs_alloc_ag_vextent(&targs))) {
2003 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return error;
Nathan Scotte63a3692006-05-08 19:51:58 +10002005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * Stop if we run out. Won't happen if callers are obeying
2008 * the restrictions correctly. Can happen for free calls
2009 * on a completely full ag.
2010 */
Yingping Lud210a282006-06-09 14:55:18 +10002011 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002012 if (flags & XFS_ALLOC_FLAG_FREEING)
2013 break;
2014 xfs_trans_brelse(tp, agflbp);
2015 args->agbp = NULL;
2016 return 0;
Yingping Lud210a282006-06-09 14:55:18 +10002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * Put each allocated block on the list.
2020 */
2021 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002022 error = xfs_alloc_put_freelist(tp, agbp,
2023 agflbp, bno, 0);
2024 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return error;
2026 }
2027 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002028 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 args->agbp = agbp;
2030 return 0;
2031}
2032
2033/*
2034 * Get a block from the freelist.
2035 * Returns with the buffer for the block gotten.
2036 */
2037int /* error */
2038xfs_alloc_get_freelist(
2039 xfs_trans_t *tp, /* transaction pointer */
2040 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002041 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2042 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 xfs_agf_t *agf; /* a.g. freespace structure */
2045 xfs_agfl_t *agfl; /* a.g. freelist structure */
2046 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2047 xfs_agblock_t bno; /* block number returned */
2048 int error;
David Chinner92821e22007-05-24 15:26:31 +10002049 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_mount_t *mp; /* mount structure */
2051 xfs_perag_t *pag; /* per allocation group data */
2052
2053 agf = XFS_BUF_TO_AGF(agbp);
2054 /*
2055 * Freelist is empty, give up.
2056 */
2057 if (!agf->agf_flcount) {
2058 *bnop = NULLAGBLOCK;
2059 return 0;
2060 }
2061 /*
2062 * Read the array of free blocks.
2063 */
2064 mp = tp->t_mountp;
2065 if ((error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002066 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return error;
2068 agfl = XFS_BUF_TO_AGFL(agflbp);
2069 /*
2070 * Get the block number and update the data structures.
2071 */
Christoph Hellwige2101002006-09-28 10:56:51 +10002072 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002073 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002075 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 agf->agf_flfirst = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002077 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002078 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 xfs_trans_agflist_delta(tp, -1);
2080 pag->pagf_flcount--;
David Chinner92821e22007-05-24 15:26:31 +10002081
2082 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2083 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002084 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002085 pag->pagf_btreeblks++;
2086 logflags |= XFS_AGF_BTREEBLKS;
2087 }
2088
2089 TRACE_MODAGF(NULL, agf, logflags);
2090 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 *bnop = bno;
2092
2093 /*
2094 * As blocks are freed, they are added to the per-ag busy list
2095 * and remain there until the freeing transaction is committed to
2096 * disk. Now that we have allocated blocks, this list must be
2097 * searched to see if a block is being reused. If one is, then
2098 * the freeing transaction must be pushed to disk NOW by forcing
2099 * to disk all iclogs up that transaction's LSN.
2100 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002101 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return 0;
2103}
2104
2105/*
2106 * Log the given fields from the agf structure.
2107 */
2108void
2109xfs_alloc_log_agf(
2110 xfs_trans_t *tp, /* transaction pointer */
2111 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2112 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2113{
2114 int first; /* first byte offset */
2115 int last; /* last byte offset */
2116 static const short offsets[] = {
2117 offsetof(xfs_agf_t, agf_magicnum),
2118 offsetof(xfs_agf_t, agf_versionnum),
2119 offsetof(xfs_agf_t, agf_seqno),
2120 offsetof(xfs_agf_t, agf_length),
2121 offsetof(xfs_agf_t, agf_roots[0]),
2122 offsetof(xfs_agf_t, agf_levels[0]),
2123 offsetof(xfs_agf_t, agf_flfirst),
2124 offsetof(xfs_agf_t, agf_fllast),
2125 offsetof(xfs_agf_t, agf_flcount),
2126 offsetof(xfs_agf_t, agf_freeblks),
2127 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002128 offsetof(xfs_agf_t, agf_btreeblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sizeof(xfs_agf_t)
2130 };
2131
2132 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2133 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2134}
2135
2136/*
2137 * Interface for inode allocation to force the pag data to be initialized.
2138 */
2139int /* error */
2140xfs_alloc_pagf_init(
2141 xfs_mount_t *mp, /* file system mount structure */
2142 xfs_trans_t *tp, /* transaction pointer */
2143 xfs_agnumber_t agno, /* allocation group number */
2144 int flags) /* XFS_ALLOC_FLAGS_... */
2145{
2146 xfs_buf_t *bp;
2147 int error;
2148
2149 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2150 return error;
2151 if (bp)
2152 xfs_trans_brelse(tp, bp);
2153 return 0;
2154}
2155
2156/*
2157 * Put the block on the freelist for the allocation group.
2158 */
2159int /* error */
2160xfs_alloc_put_freelist(
2161 xfs_trans_t *tp, /* transaction pointer */
2162 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2163 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002164 xfs_agblock_t bno, /* block being freed */
2165 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 xfs_agf_t *agf; /* a.g. freespace structure */
2168 xfs_agfl_t *agfl; /* a.g. free block array */
Christoph Hellwige2101002006-09-28 10:56:51 +10002169 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 int error;
David Chinner92821e22007-05-24 15:26:31 +10002171 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 xfs_mount_t *mp; /* mount structure */
2173 xfs_perag_t *pag; /* per allocation group data */
2174
2175 agf = XFS_BUF_TO_AGF(agbp);
2176 mp = tp->t_mountp;
2177
2178 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002179 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return error;
2181 agfl = XFS_BUF_TO_AGFL(agflbp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002182 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002183 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 agf->agf_fllast = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002185 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002186 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 xfs_trans_agflist_delta(tp, 1);
2188 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002189
2190 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2191 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002192 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002193 pag->pagf_btreeblks--;
2194 logflags |= XFS_AGF_BTREEBLKS;
2195 }
2196
2197 TRACE_MODAGF(NULL, agf, logflags);
2198 xfs_alloc_log_agf(tp, agbp, logflags);
2199
Christoph Hellwig16259e72005-11-02 15:11:25 +11002200 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2201 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002202 *blockp = cpu_to_be32(bno);
David Chinner92821e22007-05-24 15:26:31 +10002203 TRACE_MODAGF(NULL, agf, logflags);
2204 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xfs_trans_log_buf(tp, agflbp,
2206 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2207 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2208 sizeof(xfs_agblock_t) - 1));
2209 return 0;
2210}
2211
2212/*
2213 * Read in the allocation group header (free/alloc section).
2214 */
2215int /* error */
2216xfs_alloc_read_agf(
2217 xfs_mount_t *mp, /* mount point structure */
2218 xfs_trans_t *tp, /* transaction pointer */
2219 xfs_agnumber_t agno, /* allocation group number */
2220 int flags, /* XFS_ALLOC_FLAG_... */
2221 xfs_buf_t **bpp) /* buffer for the ag freelist header */
2222{
2223 xfs_agf_t *agf; /* ag freelist header */
2224 int agf_ok; /* set if agf is consistent */
2225 xfs_buf_t *bp; /* return value */
2226 xfs_perag_t *pag; /* per allocation group data */
2227 int error;
2228
2229 ASSERT(agno != NULLAGNUMBER);
2230 error = xfs_trans_read_buf(
2231 mp, tp, mp->m_ddev_targp,
2232 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2233 XFS_FSS_TO_BB(mp, 1),
2234 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0U,
2235 &bp);
2236 if (error)
2237 return error;
2238 ASSERT(!bp || !XFS_BUF_GETERROR(bp));
2239 if (!bp) {
2240 *bpp = NULL;
2241 return 0;
2242 }
2243 /*
2244 * Validate the magic number of the agf block.
2245 */
2246 agf = XFS_BUF_TO_AGF(bp);
2247 agf_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002248 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2249 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2250 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2251 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2252 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2253 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2255 XFS_RANDOM_ALLOC_READ_AGF))) {
2256 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2257 XFS_ERRLEVEL_LOW, mp, agf);
2258 xfs_trans_brelse(tp, bp);
2259 return XFS_ERROR(EFSCORRUPTED);
2260 }
2261 pag = &mp->m_perag[agno];
2262 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002263 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002264 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002265 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2266 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002268 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002270 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002271 spin_lock_init(&pag->pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2273 sizeof(xfs_perag_busy_t), KM_SLEEP);
2274 pag->pagf_init = 1;
2275 }
2276#ifdef DEBUG
2277 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002278 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2279 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2280 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002282 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002284 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286#endif
2287 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGF, XFS_AGF_REF);
2288 *bpp = bp;
2289 return 0;
2290}
2291
2292/*
2293 * Allocate an extent (variable-size).
2294 * Depending on the allocation type, we either look in a single allocation
2295 * group or loop over the allocation groups to find the result.
2296 */
2297int /* error */
2298xfs_alloc_vextent(
2299 xfs_alloc_arg_t *args) /* allocation argument structure */
2300{
2301 xfs_agblock_t agsize; /* allocation group size */
2302 int error;
2303 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 xfs_extlen_t minleft;/* minimum left value, temp copy */
2305 xfs_mount_t *mp; /* mount structure pointer */
2306 xfs_agnumber_t sagno; /* starting allocation group number */
2307 xfs_alloctype_t type; /* input allocation type */
2308 int bump_rotor = 0;
2309 int no_min = 0;
2310 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2311
2312 mp = args->mp;
2313 type = args->otype = args->type;
2314 args->agbno = NULLAGBLOCK;
2315 /*
2316 * Just fix this up, for the case where the last a.g. is shorter
2317 * (or there's only one a.g.) and the caller couldn't easily figure
2318 * that out (xfs_bmap_alloc).
2319 */
2320 agsize = mp->m_sb.sb_agblocks;
2321 if (args->maxlen > agsize)
2322 args->maxlen = agsize;
2323 if (args->alignment == 0)
2324 args->alignment = 1;
2325 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2326 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2327 ASSERT(args->minlen <= args->maxlen);
2328 ASSERT(args->minlen <= agsize);
2329 ASSERT(args->mod < args->prod);
2330 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2331 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2332 args->minlen > args->maxlen || args->minlen > agsize ||
2333 args->mod >= args->prod) {
2334 args->fsbno = NULLFSBLOCK;
2335 TRACE_ALLOC("badargs", args);
2336 return 0;
2337 }
2338 minleft = args->minleft;
2339
2340 switch (type) {
2341 case XFS_ALLOCTYPE_THIS_AG:
2342 case XFS_ALLOCTYPE_NEAR_BNO:
2343 case XFS_ALLOCTYPE_THIS_BNO:
2344 /*
2345 * These three force us into a single a.g.
2346 */
2347 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2348 down_read(&mp->m_peraglock);
2349 args->pag = &mp->m_perag[args->agno];
2350 args->minleft = 0;
2351 error = xfs_alloc_fix_freelist(args, 0);
2352 args->minleft = minleft;
2353 if (error) {
2354 TRACE_ALLOC("nofix", args);
2355 goto error0;
2356 }
2357 if (!args->agbp) {
2358 up_read(&mp->m_peraglock);
2359 TRACE_ALLOC("noagbp", args);
2360 break;
2361 }
2362 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2363 if ((error = xfs_alloc_ag_vextent(args)))
2364 goto error0;
2365 up_read(&mp->m_peraglock);
2366 break;
2367 case XFS_ALLOCTYPE_START_BNO:
2368 /*
2369 * Try near allocation first, then anywhere-in-ag after
2370 * the first a.g. fails.
2371 */
2372 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2373 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2374 args->fsbno = XFS_AGB_TO_FSB(mp,
2375 ((mp->m_agfrotor / rotorstep) %
2376 mp->m_sb.sb_agcount), 0);
2377 bump_rotor = 1;
2378 }
2379 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2380 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2381 /* FALLTHROUGH */
2382 case XFS_ALLOCTYPE_ANY_AG:
2383 case XFS_ALLOCTYPE_START_AG:
2384 case XFS_ALLOCTYPE_FIRST_AG:
2385 /*
2386 * Rotate through the allocation groups looking for a winner.
2387 */
2388 if (type == XFS_ALLOCTYPE_ANY_AG) {
2389 /*
2390 * Start with the last place we left off.
2391 */
2392 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2393 mp->m_sb.sb_agcount;
2394 args->type = XFS_ALLOCTYPE_THIS_AG;
2395 flags = XFS_ALLOC_FLAG_TRYLOCK;
2396 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2397 /*
2398 * Start with allocation group given by bno.
2399 */
2400 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2401 args->type = XFS_ALLOCTYPE_THIS_AG;
2402 sagno = 0;
2403 flags = 0;
2404 } else {
2405 if (type == XFS_ALLOCTYPE_START_AG)
2406 args->type = XFS_ALLOCTYPE_THIS_AG;
2407 /*
2408 * Start with the given allocation group.
2409 */
2410 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2411 flags = XFS_ALLOC_FLAG_TRYLOCK;
2412 }
2413 /*
2414 * Loop over allocation groups twice; first time with
2415 * trylock set, second time without.
2416 */
2417 down_read(&mp->m_peraglock);
2418 for (;;) {
2419 args->pag = &mp->m_perag[args->agno];
2420 if (no_min) args->minleft = 0;
2421 error = xfs_alloc_fix_freelist(args, flags);
2422 args->minleft = minleft;
2423 if (error) {
2424 TRACE_ALLOC("nofix", args);
2425 goto error0;
2426 }
2427 /*
2428 * If we get a buffer back then the allocation will fly.
2429 */
2430 if (args->agbp) {
2431 if ((error = xfs_alloc_ag_vextent(args)))
2432 goto error0;
2433 break;
2434 }
2435 TRACE_ALLOC("loopfailed", args);
2436 /*
2437 * Didn't work, figure out the next iteration.
2438 */
2439 if (args->agno == sagno &&
2440 type == XFS_ALLOCTYPE_START_BNO)
2441 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002442 /*
2443 * For the first allocation, we can try any AG to get
2444 * space. However, if we already have allocated a
2445 * block, we don't want to try AGs whose number is below
2446 * sagno. Otherwise, we may end up with out-of-order
2447 * locking of AGF, which might cause deadlock.
2448 */
2449 if (++(args->agno) == mp->m_sb.sb_agcount) {
2450 if (args->firstblock != NULLFSBLOCK)
2451 args->agno = sagno;
2452 else
2453 args->agno = 0;
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /*
2456 * Reached the starting a.g., must either be done
2457 * or switch to non-trylock mode.
2458 */
2459 if (args->agno == sagno) {
2460 if (no_min == 1) {
2461 args->agbno = NULLAGBLOCK;
2462 TRACE_ALLOC("allfailed", args);
2463 break;
2464 }
2465 if (flags == 0) {
2466 no_min = 1;
2467 } else {
2468 flags = 0;
2469 if (type == XFS_ALLOCTYPE_START_BNO) {
2470 args->agbno = XFS_FSB_TO_AGBNO(mp,
2471 args->fsbno);
2472 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2473 }
2474 }
2475 }
2476 }
2477 up_read(&mp->m_peraglock);
2478 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2479 if (args->agno == sagno)
2480 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2481 (mp->m_sb.sb_agcount * rotorstep);
2482 else
2483 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2484 (mp->m_sb.sb_agcount * rotorstep);
2485 }
2486 break;
2487 default:
2488 ASSERT(0);
2489 /* NOTREACHED */
2490 }
2491 if (args->agbno == NULLAGBLOCK)
2492 args->fsbno = NULLFSBLOCK;
2493 else {
2494 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2495#ifdef DEBUG
2496 ASSERT(args->len >= args->minlen);
2497 ASSERT(args->len <= args->maxlen);
2498 ASSERT(args->agbno % args->alignment == 0);
2499 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2500 args->len);
2501#endif
2502 }
2503 return 0;
2504error0:
2505 up_read(&mp->m_peraglock);
2506 return error;
2507}
2508
2509/*
2510 * Free an extent.
2511 * Just break up the extent address and hand off to xfs_free_ag_extent
2512 * after fixing up the freelist.
2513 */
2514int /* error */
2515xfs_free_extent(
2516 xfs_trans_t *tp, /* transaction pointer */
2517 xfs_fsblock_t bno, /* starting block number of extent */
2518 xfs_extlen_t len) /* length of extent */
2519{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002520 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 int error;
2522
2523 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002524 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 args.tp = tp;
2526 args.mp = tp->t_mountp;
2527 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2528 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2529 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 down_read(&args.mp->m_peraglock);
2531 args.pag = &args.mp->m_perag[args.agno];
Yingping Lud210a282006-06-09 14:55:18 +10002532 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto error0;
2534#ifdef DEBUG
2535 ASSERT(args.agbp != NULL);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002536 ASSERT((args.agbno + len) <=
2537 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538#endif
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002539 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540error0:
2541 up_read(&args.mp->m_peraglock);
2542 return error;
2543}
2544
2545
2546/*
2547 * AG Busy list management
2548 * The busy list contains block ranges that have been freed but whose
Nathan Scottc41564b2006-03-29 08:55:14 +10002549 * transactions have not yet hit disk. If any block listed in a busy
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 * list is reused, the transaction that freed it must be forced to disk
2551 * before continuing to use the block.
2552 *
2553 * xfs_alloc_mark_busy - add to the per-ag busy list
2554 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2555 */
2556void
2557xfs_alloc_mark_busy(xfs_trans_t *tp,
2558 xfs_agnumber_t agno,
2559 xfs_agblock_t bno,
2560 xfs_extlen_t len)
2561{
2562 xfs_mount_t *mp;
2563 xfs_perag_busy_t *bsy;
2564 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 mp = tp->t_mountp;
Eric Sandeen64137e52007-10-11 17:38:28 +10002567 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 /* search pagb_list for an open slot */
2570 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2571 n < XFS_PAGB_NUM_SLOTS;
2572 bsy++, n++) {
2573 if (bsy->busy_tp == NULL) {
2574 break;
2575 }
2576 }
2577
2578 if (n < XFS_PAGB_NUM_SLOTS) {
2579 bsy = &mp->m_perag[agno].pagb_list[n];
2580 mp->m_perag[agno].pagb_count++;
2581 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2582 bsy->busy_start = bno;
2583 bsy->busy_length = len;
2584 bsy->busy_tp = tp;
2585 xfs_trans_add_busy(tp, agno, n);
2586 } else {
2587 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2588 /*
2589 * The busy list is full! Since it is now not possible to
2590 * track the free block, make this a synchronous transaction
2591 * to insure that the block is not reused before this
2592 * transaction commits.
2593 */
2594 xfs_trans_set_sync(tp);
2595 }
2596
Eric Sandeen64137e52007-10-11 17:38:28 +10002597 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
2600void
2601xfs_alloc_clear_busy(xfs_trans_t *tp,
2602 xfs_agnumber_t agno,
2603 int idx)
2604{
2605 xfs_mount_t *mp;
2606 xfs_perag_busy_t *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 mp = tp->t_mountp;
2609
Eric Sandeen64137e52007-10-11 17:38:28 +10002610 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 list = mp->m_perag[agno].pagb_list;
2612
2613 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2614 if (list[idx].busy_tp == tp) {
2615 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2616 list[idx].busy_tp = NULL;
2617 mp->m_perag[agno].pagb_count--;
2618 } else {
2619 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2620 }
2621
Eric Sandeen64137e52007-10-11 17:38:28 +10002622 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
2625
2626/*
David Chinnerf4586e42008-04-10 12:21:25 +10002627 * If we find the extent in the busy list, force the log out to get the
2628 * extent out of the busy list so the caller can use it straight away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 */
David Chinnerf4586e42008-04-10 12:21:25 +10002630STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631xfs_alloc_search_busy(xfs_trans_t *tp,
2632 xfs_agnumber_t agno,
2633 xfs_agblock_t bno,
2634 xfs_extlen_t len)
2635{
2636 xfs_mount_t *mp;
2637 xfs_perag_busy_t *bsy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 xfs_agblock_t uend, bend;
2639 xfs_lsn_t lsn;
2640 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 mp = tp->t_mountp;
2643
Eric Sandeen64137e52007-10-11 17:38:28 +10002644 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 cnt = mp->m_perag[agno].pagb_count;
2646
2647 uend = bno + len - 1;
2648
2649 /* search pagb_list for this slot, skipping open slots */
David Chinnerf4586e42008-04-10 12:21:25 +10002650 for (bsy = mp->m_perag[agno].pagb_list; cnt; bsy++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /*
2653 * (start1,length1) within (start2, length2)
2654 */
2655 if (bsy->busy_tp != NULL) {
2656 bend = bsy->busy_start + bsy->busy_length - 1;
David Chinnerf4586e42008-04-10 12:21:25 +10002657 if ((bno > bend) || (uend < bsy->busy_start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 cnt--;
2659 } else {
2660 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
David Chinnerf4586e42008-04-10 12:21:25 +10002661 "found1", agno, bno, len, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 break;
2663 }
2664 }
2665 }
2666
2667 /*
2668 * If a block was found, force the log through the LSN of the
2669 * transaction that freed the block
2670 */
2671 if (cnt) {
David Chinnerf4586e42008-04-10 12:21:25 +10002672 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 lsn = bsy->busy_tp->t_commit_lsn;
Eric Sandeen64137e52007-10-11 17:38:28 +10002674 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2676 } else {
David Chinnerf4586e42008-04-10 12:21:25 +10002677 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, tp);
Eric Sandeen64137e52007-10-11 17:38:28 +10002678 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}