blob: 2cf944eb796daf4ca455865d3dd22c83ee6d13ee [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/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100158 * Get the data from the pointed-to record.
159 */
160STATIC int /* error */
161xfs_alloc_get_rec(
162 struct xfs_btree_cur *cur, /* btree cursor */
163 xfs_agblock_t *bno, /* output: starting block of extent */
164 xfs_extlen_t *len, /* output: length of extent */
165 int *stat) /* output: success/failure */
166{
167 union xfs_btree_rec *rec;
168 int error;
169
170 error = xfs_btree_get_rec(cur, &rec, stat);
171 if (!error && *stat == 1) {
172 *bno = be32_to_cpu(rec->alloc.ar_startblock);
173 *len = be32_to_cpu(rec->alloc.ar_blockcount);
174 }
175 return error;
176}
177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Compute aligned version of the found extent.
180 * Takes alignment and min length into account.
181 */
David Chinner12375c82008-04-10 12:21:32 +1000182STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183xfs_alloc_compute_aligned(
184 xfs_agblock_t foundbno, /* starting block in found extent */
185 xfs_extlen_t foundlen, /* length in found extent */
186 xfs_extlen_t alignment, /* alignment for allocation */
187 xfs_extlen_t minlen, /* minimum length for allocation */
188 xfs_agblock_t *resbno, /* result block number */
189 xfs_extlen_t *reslen) /* result length */
190{
191 xfs_agblock_t bno;
192 xfs_extlen_t diff;
193 xfs_extlen_t len;
194
195 if (alignment > 1 && foundlen >= minlen) {
196 bno = roundup(foundbno, alignment);
197 diff = bno - foundbno;
198 len = diff >= foundlen ? 0 : foundlen - diff;
199 } else {
200 bno = foundbno;
201 len = foundlen;
202 }
203 *resbno = bno;
204 *reslen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207/*
208 * Compute best start block and diff for "near" allocations.
209 * freelen >= wantlen already checked by caller.
210 */
211STATIC xfs_extlen_t /* difference value (absolute) */
212xfs_alloc_compute_diff(
213 xfs_agblock_t wantbno, /* target starting block */
214 xfs_extlen_t wantlen, /* target length */
215 xfs_extlen_t alignment, /* target alignment */
216 xfs_agblock_t freebno, /* freespace's starting block */
217 xfs_extlen_t freelen, /* freespace's length */
218 xfs_agblock_t *newbnop) /* result: best start block from free */
219{
220 xfs_agblock_t freeend; /* end of freespace extent */
221 xfs_agblock_t newbno1; /* return block number */
222 xfs_agblock_t newbno2; /* other new block number */
223 xfs_extlen_t newlen1=0; /* length with newbno1 */
224 xfs_extlen_t newlen2=0; /* length with newbno2 */
225 xfs_agblock_t wantend; /* end of target extent */
226
227 ASSERT(freelen >= wantlen);
228 freeend = freebno + freelen;
229 wantend = wantbno + wantlen;
230 if (freebno >= wantbno) {
231 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
232 newbno1 = NULLAGBLOCK;
233 } else if (freeend >= wantend && alignment > 1) {
234 newbno1 = roundup(wantbno, alignment);
235 newbno2 = newbno1 - alignment;
236 if (newbno1 >= freeend)
237 newbno1 = NULLAGBLOCK;
238 else
239 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
240 if (newbno2 < freebno)
241 newbno2 = NULLAGBLOCK;
242 else
243 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
244 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
245 if (newlen1 < newlen2 ||
246 (newlen1 == newlen2 &&
247 XFS_ABSDIFF(newbno1, wantbno) >
248 XFS_ABSDIFF(newbno2, wantbno)))
249 newbno1 = newbno2;
250 } else if (newbno2 != NULLAGBLOCK)
251 newbno1 = newbno2;
252 } else if (freeend >= wantend) {
253 newbno1 = wantbno;
254 } else if (alignment > 1) {
255 newbno1 = roundup(freeend - wantlen, alignment);
256 if (newbno1 > freeend - wantlen &&
257 newbno1 - alignment >= freebno)
258 newbno1 -= alignment;
259 else if (newbno1 >= freeend)
260 newbno1 = NULLAGBLOCK;
261 } else
262 newbno1 = freeend - wantlen;
263 *newbnop = newbno1;
264 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
265}
266
267/*
268 * Fix up the length, based on mod and prod.
269 * len should be k * prod + mod for some k.
270 * If len is too small it is returned unchanged.
271 * If len hits maxlen it is left alone.
272 */
273STATIC void
274xfs_alloc_fix_len(
275 xfs_alloc_arg_t *args) /* allocation argument structure */
276{
277 xfs_extlen_t k;
278 xfs_extlen_t rlen;
279
280 ASSERT(args->mod < args->prod);
281 rlen = args->len;
282 ASSERT(rlen >= args->minlen);
283 ASSERT(rlen <= args->maxlen);
284 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
285 (args->mod == 0 && rlen < args->prod))
286 return;
287 k = rlen % args->prod;
288 if (k == args->mod)
289 return;
290 if (k > args->mod) {
291 if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
292 return;
293 } else {
294 if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
295 (int)args->minlen)
296 return;
297 }
298 ASSERT(rlen >= args->minlen);
299 ASSERT(rlen <= args->maxlen);
300 args->len = rlen;
301}
302
303/*
304 * Fix up length if there is too little space left in the a.g.
305 * Return 1 if ok, 0 if too little, should give up.
306 */
307STATIC int
308xfs_alloc_fix_minleft(
309 xfs_alloc_arg_t *args) /* allocation argument structure */
310{
311 xfs_agf_t *agf; /* a.g. freelist header */
312 int diff; /* free space difference */
313
314 if (args->minleft == 0)
315 return 1;
316 agf = XFS_BUF_TO_AGF(args->agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100317 diff = be32_to_cpu(agf->agf_freeblks)
318 + be32_to_cpu(agf->agf_flcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 - args->len - args->minleft;
320 if (diff >= 0)
321 return 1;
322 args->len += diff; /* shrink the allocated space */
323 if (args->len >= args->minlen)
324 return 1;
325 args->agbno = NULLAGBLOCK;
326 return 0;
327}
328
329/*
330 * Update the two btrees, logically removing from freespace the extent
331 * starting at rbno, rlen blocks. The extent is contained within the
332 * actual (current) free extent fbno for flen blocks.
333 * Flags are passed in indicating whether the cursors are set to the
334 * relevant records.
335 */
336STATIC int /* error code */
337xfs_alloc_fixup_trees(
338 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
339 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
340 xfs_agblock_t fbno, /* starting block of free extent */
341 xfs_extlen_t flen, /* length of free extent */
342 xfs_agblock_t rbno, /* starting block of returned extent */
343 xfs_extlen_t rlen, /* length of returned extent */
344 int flags) /* flags, XFSA_FIXUP_... */
345{
346 int error; /* error code */
347 int i; /* operation results */
348 xfs_agblock_t nfbno1; /* first new free startblock */
349 xfs_agblock_t nfbno2; /* second new free startblock */
350 xfs_extlen_t nflen1=0; /* first new free length */
351 xfs_extlen_t nflen2=0; /* second new free length */
352
353 /*
354 * Look up the record in the by-size tree if necessary.
355 */
356 if (flags & XFSA_FIXUP_CNT_OK) {
357#ifdef DEBUG
358 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
359 return error;
360 XFS_WANT_CORRUPTED_RETURN(
361 i == 1 && nfbno1 == fbno && nflen1 == flen);
362#endif
363 } else {
364 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
365 return error;
366 XFS_WANT_CORRUPTED_RETURN(i == 1);
367 }
368 /*
369 * Look up the record in the by-block tree if necessary.
370 */
371 if (flags & XFSA_FIXUP_BNO_OK) {
372#ifdef DEBUG
373 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
374 return error;
375 XFS_WANT_CORRUPTED_RETURN(
376 i == 1 && nfbno1 == fbno && nflen1 == flen);
377#endif
378 } else {
379 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
380 return error;
381 XFS_WANT_CORRUPTED_RETURN(i == 1);
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100384#ifdef DEBUG
385 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
386 struct xfs_btree_block *bnoblock;
387 struct xfs_btree_block *cntblock;
388
389 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
390 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
391
392 XFS_WANT_CORRUPTED_RETURN(
393 bnoblock->bb_numrecs == cntblock->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395#endif
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Deal with all four cases: the allocated record is contained
399 * within the freespace record, so we can have new freespace
400 * at either (or both) end, or no freespace remaining.
401 */
402 if (rbno == fbno && rlen == flen)
403 nfbno1 = nfbno2 = NULLAGBLOCK;
404 else if (rbno == fbno) {
405 nfbno1 = rbno + rlen;
406 nflen1 = flen - rlen;
407 nfbno2 = NULLAGBLOCK;
408 } else if (rbno + rlen == fbno + flen) {
409 nfbno1 = fbno;
410 nflen1 = flen - rlen;
411 nfbno2 = NULLAGBLOCK;
412 } else {
413 nfbno1 = fbno;
414 nflen1 = rbno - fbno;
415 nfbno2 = rbno + rlen;
416 nflen2 = (fbno + flen) - nfbno2;
417 }
418 /*
419 * Delete the entry from the by-size btree.
420 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100421 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return error;
423 XFS_WANT_CORRUPTED_RETURN(i == 1);
424 /*
425 * Add new by-size btree entry(s).
426 */
427 if (nfbno1 != NULLAGBLOCK) {
428 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
429 return error;
430 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100431 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return error;
433 XFS_WANT_CORRUPTED_RETURN(i == 1);
434 }
435 if (nfbno2 != NULLAGBLOCK) {
436 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
437 return error;
438 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100439 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return error;
441 XFS_WANT_CORRUPTED_RETURN(i == 1);
442 }
443 /*
444 * Fix up the by-block btree entry(s).
445 */
446 if (nfbno1 == NULLAGBLOCK) {
447 /*
448 * No remaining freespace, just delete the by-block tree entry.
449 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100450 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return error;
452 XFS_WANT_CORRUPTED_RETURN(i == 1);
453 } else {
454 /*
455 * Update the by-block entry to start later|be shorter.
456 */
457 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
458 return error;
459 }
460 if (nfbno2 != NULLAGBLOCK) {
461 /*
462 * 2 resulting free entries, need to add one.
463 */
464 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
465 return error;
466 XFS_WANT_CORRUPTED_RETURN(i == 0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100467 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return error;
469 XFS_WANT_CORRUPTED_RETURN(i == 1);
470 }
471 return 0;
472}
473
474/*
475 * Read in the allocation group free block array.
476 */
477STATIC int /* error */
478xfs_alloc_read_agfl(
479 xfs_mount_t *mp, /* mount point structure */
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_agnumber_t agno, /* allocation group number */
482 xfs_buf_t **bpp) /* buffer for the ag free block array */
483{
484 xfs_buf_t *bp; /* return value */
485 int error;
486
487 ASSERT(agno != NULLAGNUMBER);
488 error = xfs_trans_read_buf(
489 mp, tp, mp->m_ddev_targp,
490 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
491 XFS_FSS_TO_BB(mp, 1), 0, &bp);
492 if (error)
493 return error;
494 ASSERT(bp);
495 ASSERT(!XFS_BUF_GETERROR(bp));
496 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
497 *bpp = bp;
498 return 0;
499}
500
501#if defined(XFS_ALLOC_TRACE)
502/*
503 * Add an allocation trace entry for an alloc call.
504 */
505STATIC void
506xfs_alloc_trace_alloc(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000507 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char *str, /* additional string */
509 xfs_alloc_arg_t *args, /* allocation argument structure */
510 int line) /* source line number */
511{
512 ktrace_enter(xfs_alloc_trace_buf,
513 (void *)(__psint_t)(XFS_ALLOC_KTRACE_ALLOC | (line << 16)),
514 (void *)name,
515 (void *)str,
516 (void *)args->mp,
517 (void *)(__psunsigned_t)args->agno,
518 (void *)(__psunsigned_t)args->agbno,
519 (void *)(__psunsigned_t)args->minlen,
520 (void *)(__psunsigned_t)args->maxlen,
521 (void *)(__psunsigned_t)args->mod,
522 (void *)(__psunsigned_t)args->prod,
523 (void *)(__psunsigned_t)args->minleft,
524 (void *)(__psunsigned_t)args->total,
525 (void *)(__psunsigned_t)args->alignment,
526 (void *)(__psunsigned_t)args->len,
527 (void *)((((__psint_t)args->type) << 16) |
528 (__psint_t)args->otype),
529 (void *)(__psint_t)((args->wasdel << 3) |
530 (args->wasfromfl << 2) |
531 (args->isfl << 1) |
532 (args->userdata << 0)));
533}
534
535/*
536 * Add an allocation trace entry for a free call.
537 */
538STATIC void
539xfs_alloc_trace_free(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000540 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 char *str, /* additional string */
542 xfs_mount_t *mp, /* file system mount point */
543 xfs_agnumber_t agno, /* allocation group number */
544 xfs_agblock_t agbno, /* a.g. relative block number */
545 xfs_extlen_t len, /* length of extent */
546 int isfl, /* set if is freelist allocation/free */
547 int line) /* source line number */
548{
549 ktrace_enter(xfs_alloc_trace_buf,
550 (void *)(__psint_t)(XFS_ALLOC_KTRACE_FREE | (line << 16)),
551 (void *)name,
552 (void *)str,
553 (void *)mp,
554 (void *)(__psunsigned_t)agno,
555 (void *)(__psunsigned_t)agbno,
556 (void *)(__psunsigned_t)len,
557 (void *)(__psint_t)isfl,
558 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
559}
560
561/*
562 * Add an allocation trace entry for modifying an agf.
563 */
564STATIC void
565xfs_alloc_trace_modagf(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000566 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 char *str, /* additional string */
568 xfs_mount_t *mp, /* file system mount point */
569 xfs_agf_t *agf, /* new agf value */
570 int flags, /* logging flags for agf */
571 int line) /* source line number */
572{
573 ktrace_enter(xfs_alloc_trace_buf,
574 (void *)(__psint_t)(XFS_ALLOC_KTRACE_MODAGF | (line << 16)),
575 (void *)name,
576 (void *)str,
577 (void *)mp,
578 (void *)(__psint_t)flags,
Christoph Hellwig16259e72005-11-02 15:11:25 +1100579 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_seqno),
580 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_length),
581 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]),
582 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]),
583 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]),
584 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]),
585 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flfirst),
586 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_fllast),
587 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_flcount),
588 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_freeblks),
589 (void *)(__psunsigned_t)be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592STATIC void
593xfs_alloc_trace_busy(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000594 const char *name, /* function tag string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 char *str, /* additional string */
Nathan Scottc41564b2006-03-29 08:55:14 +1000596 xfs_mount_t *mp, /* file system mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_agnumber_t agno, /* allocation group number */
598 xfs_agblock_t agbno, /* a.g. relative block number */
599 xfs_extlen_t len, /* length of extent */
600 int slot, /* perag Busy slot */
601 xfs_trans_t *tp,
602 int trtype, /* type: add, delete, search */
603 int line) /* source line number */
604{
605 ktrace_enter(xfs_alloc_trace_buf,
606 (void *)(__psint_t)(trtype | (line << 16)),
607 (void *)name,
608 (void *)str,
609 (void *)mp,
610 (void *)(__psunsigned_t)agno,
611 (void *)(__psunsigned_t)agbno,
612 (void *)(__psunsigned_t)len,
613 (void *)(__psint_t)slot,
614 (void *)tp,
615 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
616}
617#endif /* XFS_ALLOC_TRACE */
618
619/*
620 * Allocation group level functions.
621 */
622
623/*
624 * Allocate a variable extent in the allocation group agno.
625 * Type and bno are used to determine where in the allocation group the
626 * extent will start.
627 * Extent's length (returned in *len) will be between minlen and maxlen,
628 * and of the form k * prod + mod unless there's nothing that large.
629 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
630 */
631STATIC int /* error */
632xfs_alloc_ag_vextent(
633 xfs_alloc_arg_t *args) /* argument structure for allocation */
634{
635 int error=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 ASSERT(args->minlen > 0);
638 ASSERT(args->maxlen > 0);
639 ASSERT(args->minlen <= args->maxlen);
640 ASSERT(args->mod < args->prod);
641 ASSERT(args->alignment > 0);
642 /*
643 * Branch to correct routine based on the type.
644 */
645 args->wasfromfl = 0;
646 switch (args->type) {
647 case XFS_ALLOCTYPE_THIS_AG:
648 error = xfs_alloc_ag_vextent_size(args);
649 break;
650 case XFS_ALLOCTYPE_NEAR_BNO:
651 error = xfs_alloc_ag_vextent_near(args);
652 break;
653 case XFS_ALLOCTYPE_THIS_BNO:
654 error = xfs_alloc_ag_vextent_exact(args);
655 break;
656 default:
657 ASSERT(0);
658 /* NOTREACHED */
659 }
660 if (error)
661 return error;
662 /*
663 * If the allocation worked, need to change the agf structure
664 * (and log it), and the superblock.
665 */
666 if (args->agbno != NULLAGBLOCK) {
667 xfs_agf_t *agf; /* allocation group freelist header */
668#ifdef XFS_ALLOC_TRACE
669 xfs_mount_t *mp = args->mp;
670#endif
671 long slen = (long)args->len;
672
673 ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
674 ASSERT(!(args->wasfromfl) || !args->isfl);
675 ASSERT(args->agbno % args->alignment == 0);
676 if (!(args->wasfromfl)) {
677
678 agf = XFS_BUF_TO_AGF(args->agbp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800679 be32_add_cpu(&agf->agf_freeblks, -(args->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_trans_agblocks_delta(args->tp,
681 -((long)(args->len)));
682 args->pag->pagf_freeblks -= args->len;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100683 ASSERT(be32_to_cpu(agf->agf_freeblks) <=
684 be32_to_cpu(agf->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
686 xfs_alloc_log_agf(args->tp, args->agbp,
687 XFS_AGF_FREEBLKS);
688 /* search the busylist for these blocks */
689 xfs_alloc_search_busy(args->tp, args->agno,
690 args->agbno, args->len);
691 }
692 if (!args->isfl)
693 xfs_trans_mod_sb(args->tp,
694 args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
695 XFS_TRANS_SB_FDBLOCKS, -slen);
696 XFS_STATS_INC(xs_allocx);
697 XFS_STATS_ADD(xs_allocb, args->len);
698 }
699 return 0;
700}
701
702/*
703 * Allocate a variable extent at exactly agno/bno.
704 * Extent's length (returned in *len) will be between minlen and maxlen,
705 * and of the form k * prod + mod unless there's nothing that large.
706 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
707 */
708STATIC int /* error */
709xfs_alloc_ag_vextent_exact(
710 xfs_alloc_arg_t *args) /* allocation argument structure */
711{
712 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
713 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
714 xfs_agblock_t end; /* end of allocated extent */
715 int error;
716 xfs_agblock_t fbno; /* start block of found extent */
717 xfs_agblock_t fend; /* end block of found extent */
718 xfs_extlen_t flen; /* length of found extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int i; /* success/failure of operation */
720 xfs_agblock_t maxend; /* end of maximal extent */
721 xfs_agblock_t minend; /* end of minimal extent */
722 xfs_extlen_t rlen; /* length of returned extent */
723
724 ASSERT(args->alignment == 1);
725 /*
726 * Allocate/initialize a cursor for the by-number freespace btree.
727 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100728 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
729 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
732 * Look for the closest free block <= bno, it must contain bno
733 * if any free block does.
734 */
735 if ((error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i)))
736 goto error0;
737 if (!i) {
738 /*
739 * Didn't find it, return null.
740 */
741 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
742 args->agbno = NULLAGBLOCK;
743 return 0;
744 }
745 /*
746 * Grab the freespace record.
747 */
748 if ((error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i)))
749 goto error0;
750 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
751 ASSERT(fbno <= args->agbno);
752 minend = args->agbno + args->minlen;
753 maxend = args->agbno + args->maxlen;
754 fend = fbno + flen;
755 /*
756 * Give up if the freespace isn't long enough for the minimum request.
757 */
758 if (fend < minend) {
759 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
760 args->agbno = NULLAGBLOCK;
761 return 0;
762 }
763 /*
764 * End of extent will be smaller of the freespace end and the
765 * maximal requested end.
766 */
767 end = XFS_AGBLOCK_MIN(fend, maxend);
768 /*
769 * Fix the length according to mod and prod if given.
770 */
771 args->len = end - args->agbno;
772 xfs_alloc_fix_len(args);
773 if (!xfs_alloc_fix_minleft(args)) {
774 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
775 return 0;
776 }
777 rlen = args->len;
778 ASSERT(args->agbno + rlen <= fend);
779 end = args->agbno + rlen;
780 /*
781 * We are allocating agbno for rlen [agbno .. end]
782 * Allocate/initialize a cursor for the by-size btree.
783 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100784 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
785 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ASSERT(args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100787 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
789 args->agbno, args->len, XFSA_FIXUP_BNO_OK))) {
790 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
791 goto error0;
792 }
793 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
795 TRACE_ALLOC("normal", args);
796 args->wasfromfl = 0;
797 return 0;
798
799error0:
800 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
801 TRACE_ALLOC("error", args);
802 return error;
803}
804
805/*
806 * Allocate a variable extent near bno in the allocation group agno.
807 * Extent's length (returned in len) will be between minlen and maxlen,
808 * and of the form k * prod + mod unless there's nothing that large.
809 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
810 */
811STATIC int /* error */
812xfs_alloc_ag_vextent_near(
813 xfs_alloc_arg_t *args) /* allocation argument structure */
814{
815 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
816 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
817 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xfs_agblock_t gtbno; /* start bno of right side entry */
819 xfs_agblock_t gtbnoa; /* aligned ... */
820 xfs_extlen_t gtdiff; /* difference to right side entry */
821 xfs_extlen_t gtlen; /* length of right side entry */
822 xfs_extlen_t gtlena; /* aligned ... */
823 xfs_agblock_t gtnew; /* useful start bno of right side */
824 int error; /* error code */
825 int i; /* result code, temporary */
826 int j; /* result code, temporary */
827 xfs_agblock_t ltbno; /* start bno of left side entry */
828 xfs_agblock_t ltbnoa; /* aligned ... */
829 xfs_extlen_t ltdiff; /* difference to left side entry */
830 /*REFERENCED*/
831 xfs_agblock_t ltend; /* end bno of left side entry */
832 xfs_extlen_t ltlen; /* length of left side entry */
833 xfs_extlen_t ltlena; /* aligned ... */
834 xfs_agblock_t ltnew; /* useful start bno of left side */
835 xfs_extlen_t rlen; /* length of returned extent */
836#if defined(DEBUG) && defined(__KERNEL__)
837 /*
838 * Randomly don't execute the first algorithm.
839 */
840 int dofirst; /* set to do first algorithm */
841
Joe Perchese7a23a92007-05-08 13:49:03 +1000842 dofirst = random32() & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#endif
844 /*
845 * Get a cursor for the by-size btree.
846 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100847 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
848 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ltlen = 0;
850 bno_cur_lt = bno_cur_gt = NULL;
851 /*
852 * See if there are any free extents as big as maxlen.
853 */
854 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
855 goto error0;
856 /*
857 * If none, then pick up the last entry in the tree unless the
858 * tree is empty.
859 */
860 if (!i) {
861 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
862 &ltlen, &i)))
863 goto error0;
864 if (i == 0 || ltlen == 0) {
865 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
866 return 0;
867 }
868 ASSERT(i == 1);
869 }
870 args->wasfromfl = 0;
871 /*
872 * First algorithm.
873 * If the requested extent is large wrt the freespaces available
874 * in this a.g., then the cursor will be pointing to a btree entry
875 * near the right edge of the tree. If it's in the last btree leaf
876 * block, then we just examine all the entries in that block
877 * that are big enough, and pick the best one.
878 * This is written as a while loop so we can break out of it,
879 * but we never loop back to the top.
880 */
881 while (xfs_btree_islastblock(cnt_cur, 0)) {
882 xfs_extlen_t bdiff;
883 int besti=0;
884 xfs_extlen_t blen=0;
885 xfs_agblock_t bnew=0;
886
887#if defined(DEBUG) && defined(__KERNEL__)
888 if (!dofirst)
889 break;
890#endif
891 /*
892 * Start from the entry that lookup found, sequence through
893 * all larger free blocks. If we're actually pointing at a
894 * record smaller than maxlen, go to the start of this block,
895 * and skip all those smaller than minlen.
896 */
897 if (ltlen || args->alignment > 1) {
898 cnt_cur->bc_ptrs[0] = 1;
899 do {
900 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
901 &ltlen, &i)))
902 goto error0;
903 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
904 if (ltlen >= args->minlen)
905 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100906 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto error0;
908 } while (i);
909 ASSERT(ltlen >= args->minlen);
910 if (!i)
911 break;
912 }
913 i = cnt_cur->bc_ptrs[0];
914 for (j = 1, blen = 0, bdiff = 0;
915 !error && j && (blen < args->maxlen || bdiff > 0);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100916 error = xfs_btree_increment(cnt_cur, 0, &j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /*
918 * For each entry, decide if it's better than
919 * the previous best entry.
920 */
921 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
922 goto error0;
923 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +1000924 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
925 args->minlen, &ltbnoa, &ltlena);
David Chinnere6430032008-04-17 16:49:49 +1000926 if (ltlena < args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 continue;
928 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
929 xfs_alloc_fix_len(args);
930 ASSERT(args->len >= args->minlen);
931 if (args->len < blen)
932 continue;
933 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
934 args->alignment, ltbno, ltlen, &ltnew);
935 if (ltnew != NULLAGBLOCK &&
936 (args->len > blen || ltdiff < bdiff)) {
937 bdiff = ltdiff;
938 bnew = ltnew;
939 blen = args->len;
940 besti = cnt_cur->bc_ptrs[0];
941 }
942 }
943 /*
944 * It didn't work. We COULD be in a case where
945 * there's a good record somewhere, so try again.
946 */
947 if (blen == 0)
948 break;
949 /*
950 * Point at the best entry, and retrieve it again.
951 */
952 cnt_cur->bc_ptrs[0] = besti;
953 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
954 goto error0;
955 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
956 ltend = ltbno + ltlen;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100957 ASSERT(ltend <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 args->len = blen;
959 if (!xfs_alloc_fix_minleft(args)) {
960 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
961 TRACE_ALLOC("nominleft", args);
962 return 0;
963 }
964 blen = args->len;
965 /*
966 * We are allocating starting at bnew for blen blocks.
967 */
968 args->agbno = bnew;
969 ASSERT(bnew >= ltbno);
970 ASSERT(bnew + blen <= ltend);
971 /*
972 * Set up a cursor for the by-bno tree.
973 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100974 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
975 args->agbp, args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
977 * Fix up the btree entries.
978 */
979 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
980 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
981 goto error0;
982 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
983 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
984 TRACE_ALLOC("first", args);
985 return 0;
986 }
987 /*
988 * Second algorithm.
989 * Search in the by-bno tree to the left and to the right
990 * simultaneously, until in each case we find a space big enough,
991 * or run into the edge of the tree. When we run into the edge,
992 * we deallocate that cursor.
993 * If both searches succeed, we compare the two spaces and pick
994 * the better one.
995 * With alignment, it's possible for both to fail; the upper
996 * level algorithm that picks allocation groups for allocations
997 * is not supposed to do this.
998 */
999 /*
1000 * Allocate and initialize the cursor for the leftward search.
1001 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001002 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1003 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Lookup <= bno to find the leftward search's starting point.
1006 */
1007 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1008 goto error0;
1009 if (!i) {
1010 /*
1011 * Didn't find anything; use this cursor for the rightward
1012 * search.
1013 */
1014 bno_cur_gt = bno_cur_lt;
1015 bno_cur_lt = NULL;
1016 }
1017 /*
1018 * Found something. Duplicate the cursor for the rightward search.
1019 */
1020 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1021 goto error0;
1022 /*
1023 * Increment the cursor, so we will point at the entry just right
1024 * of the leftward entry if any, or to the leftmost entry.
1025 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001026 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 goto error0;
1028 if (!i) {
1029 /*
1030 * It failed, there are no rightward entries.
1031 */
1032 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1033 bno_cur_gt = NULL;
1034 }
1035 /*
1036 * Loop going left with the leftward cursor, right with the
1037 * rightward cursor, until either both directions give up or
1038 * we find an entry at least as big as minlen.
1039 */
1040 do {
1041 if (bno_cur_lt) {
1042 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1043 goto error0;
1044 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +10001045 xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
1046 args->minlen, &ltbnoa, &ltlena);
1047 if (ltlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001049 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto error0;
1051 if (!i) {
1052 xfs_btree_del_cursor(bno_cur_lt,
1053 XFS_BTREE_NOERROR);
1054 bno_cur_lt = NULL;
1055 }
1056 }
1057 if (bno_cur_gt) {
1058 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1059 goto error0;
1060 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
David Chinner12375c82008-04-10 12:21:32 +10001061 xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
1062 args->minlen, &gtbnoa, &gtlena);
1063 if (gtlena >= args->minlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001065 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto error0;
1067 if (!i) {
1068 xfs_btree_del_cursor(bno_cur_gt,
1069 XFS_BTREE_NOERROR);
1070 bno_cur_gt = NULL;
1071 }
1072 }
1073 } while (bno_cur_lt || bno_cur_gt);
1074 /*
1075 * Got both cursors still active, need to find better entry.
1076 */
1077 if (bno_cur_lt && bno_cur_gt) {
1078 /*
1079 * Left side is long enough, look for a right side entry.
1080 */
1081 if (ltlena >= args->minlen) {
1082 /*
1083 * Fix up the length.
1084 */
1085 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1086 xfs_alloc_fix_len(args);
1087 rlen = args->len;
1088 ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1089 args->alignment, ltbno, ltlen, &ltnew);
1090 /*
1091 * Not perfect.
1092 */
1093 if (ltdiff) {
1094 /*
1095 * Look until we find a better one, run out of
1096 * space, or run off the end.
1097 */
1098 while (bno_cur_lt && bno_cur_gt) {
1099 if ((error = xfs_alloc_get_rec(
1100 bno_cur_gt, &gtbno,
1101 &gtlen, &i)))
1102 goto error0;
1103 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1104 xfs_alloc_compute_aligned(gtbno, gtlen,
1105 args->alignment, args->minlen,
1106 &gtbnoa, &gtlena);
1107 /*
1108 * The left one is clearly better.
1109 */
1110 if (gtbnoa >= args->agbno + ltdiff) {
1111 xfs_btree_del_cursor(
1112 bno_cur_gt,
1113 XFS_BTREE_NOERROR);
1114 bno_cur_gt = NULL;
1115 break;
1116 }
1117 /*
1118 * If we reach a big enough entry,
1119 * compare the two and pick the best.
1120 */
1121 if (gtlena >= args->minlen) {
1122 args->len =
1123 XFS_EXTLEN_MIN(gtlena,
1124 args->maxlen);
1125 xfs_alloc_fix_len(args);
1126 rlen = args->len;
1127 gtdiff = xfs_alloc_compute_diff(
1128 args->agbno, rlen,
1129 args->alignment,
1130 gtbno, gtlen, &gtnew);
1131 /*
1132 * Right side is better.
1133 */
1134 if (gtdiff < ltdiff) {
1135 xfs_btree_del_cursor(
1136 bno_cur_lt,
1137 XFS_BTREE_NOERROR);
1138 bno_cur_lt = NULL;
1139 }
1140 /*
1141 * Left side is better.
1142 */
1143 else {
1144 xfs_btree_del_cursor(
1145 bno_cur_gt,
1146 XFS_BTREE_NOERROR);
1147 bno_cur_gt = NULL;
1148 }
1149 break;
1150 }
1151 /*
1152 * Fell off the right end.
1153 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001154 if ((error = xfs_btree_increment(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 bno_cur_gt, 0, &i)))
1156 goto error0;
1157 if (!i) {
1158 xfs_btree_del_cursor(
1159 bno_cur_gt,
1160 XFS_BTREE_NOERROR);
1161 bno_cur_gt = NULL;
1162 break;
1163 }
1164 }
1165 }
1166 /*
1167 * The left side is perfect, trash the right side.
1168 */
1169 else {
1170 xfs_btree_del_cursor(bno_cur_gt,
1171 XFS_BTREE_NOERROR);
1172 bno_cur_gt = NULL;
1173 }
1174 }
1175 /*
1176 * It's the right side that was found first, look left.
1177 */
1178 else {
1179 /*
1180 * Fix up the length.
1181 */
1182 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1183 xfs_alloc_fix_len(args);
1184 rlen = args->len;
1185 gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
1186 args->alignment, gtbno, gtlen, &gtnew);
1187 /*
1188 * Right side entry isn't perfect.
1189 */
1190 if (gtdiff) {
1191 /*
1192 * Look until we find a better one, run out of
1193 * space, or run off the end.
1194 */
1195 while (bno_cur_lt && bno_cur_gt) {
1196 if ((error = xfs_alloc_get_rec(
1197 bno_cur_lt, &ltbno,
1198 &ltlen, &i)))
1199 goto error0;
1200 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1201 xfs_alloc_compute_aligned(ltbno, ltlen,
1202 args->alignment, args->minlen,
1203 &ltbnoa, &ltlena);
1204 /*
1205 * The right one is clearly better.
1206 */
1207 if (ltbnoa <= args->agbno - gtdiff) {
1208 xfs_btree_del_cursor(
1209 bno_cur_lt,
1210 XFS_BTREE_NOERROR);
1211 bno_cur_lt = NULL;
1212 break;
1213 }
1214 /*
1215 * If we reach a big enough entry,
1216 * compare the two and pick the best.
1217 */
1218 if (ltlena >= args->minlen) {
1219 args->len = XFS_EXTLEN_MIN(
1220 ltlena, args->maxlen);
1221 xfs_alloc_fix_len(args);
1222 rlen = args->len;
1223 ltdiff = xfs_alloc_compute_diff(
1224 args->agbno, rlen,
1225 args->alignment,
1226 ltbno, ltlen, &ltnew);
1227 /*
1228 * Left side is better.
1229 */
1230 if (ltdiff < gtdiff) {
1231 xfs_btree_del_cursor(
1232 bno_cur_gt,
1233 XFS_BTREE_NOERROR);
1234 bno_cur_gt = NULL;
1235 }
1236 /*
1237 * Right side is better.
1238 */
1239 else {
1240 xfs_btree_del_cursor(
1241 bno_cur_lt,
1242 XFS_BTREE_NOERROR);
1243 bno_cur_lt = NULL;
1244 }
1245 break;
1246 }
1247 /*
1248 * Fell off the left end.
1249 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001250 if ((error = xfs_btree_decrement(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 bno_cur_lt, 0, &i)))
1252 goto error0;
1253 if (!i) {
1254 xfs_btree_del_cursor(bno_cur_lt,
1255 XFS_BTREE_NOERROR);
1256 bno_cur_lt = NULL;
1257 break;
1258 }
1259 }
1260 }
1261 /*
1262 * The right side is perfect, trash the left side.
1263 */
1264 else {
1265 xfs_btree_del_cursor(bno_cur_lt,
1266 XFS_BTREE_NOERROR);
1267 bno_cur_lt = NULL;
1268 }
1269 }
1270 }
1271 /*
1272 * If we couldn't get anything, give up.
1273 */
1274 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1275 TRACE_ALLOC("neither", args);
1276 args->agbno = NULLAGBLOCK;
1277 return 0;
1278 }
1279 /*
1280 * At this point we have selected a freespace entry, either to the
1281 * left or to the right. If it's on the right, copy all the
1282 * useful variables to the "left" set so we only have one
1283 * copy of this code.
1284 */
1285 if (bno_cur_gt) {
1286 bno_cur_lt = bno_cur_gt;
1287 bno_cur_gt = NULL;
1288 ltbno = gtbno;
1289 ltbnoa = gtbnoa;
1290 ltlen = gtlen;
1291 ltlena = gtlena;
1292 j = 1;
1293 } else
1294 j = 0;
1295 /*
1296 * Fix up the length and compute the useful address.
1297 */
1298 ltend = ltbno + ltlen;
1299 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1300 xfs_alloc_fix_len(args);
1301 if (!xfs_alloc_fix_minleft(args)) {
1302 TRACE_ALLOC("nominleft", args);
1303 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1304 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1305 return 0;
1306 }
1307 rlen = args->len;
1308 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
1309 ltlen, &ltnew);
1310 ASSERT(ltnew >= ltbno);
1311 ASSERT(ltnew + rlen <= ltend);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001312 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 args->agbno = ltnew;
1314 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1315 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1316 goto error0;
1317 TRACE_ALLOC(j ? "gt" : "lt", args);
1318 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1319 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1320 return 0;
1321
1322 error0:
1323 TRACE_ALLOC("error", args);
1324 if (cnt_cur != NULL)
1325 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1326 if (bno_cur_lt != NULL)
1327 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1328 if (bno_cur_gt != NULL)
1329 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1330 return error;
1331}
1332
1333/*
1334 * Allocate a variable extent anywhere in the allocation group agno.
1335 * Extent's length (returned in len) will be between minlen and maxlen,
1336 * and of the form k * prod + mod unless there's nothing that large.
1337 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1338 */
1339STATIC int /* error */
1340xfs_alloc_ag_vextent_size(
1341 xfs_alloc_arg_t *args) /* allocation argument structure */
1342{
1343 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1344 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1345 int error; /* error result */
1346 xfs_agblock_t fbno; /* start of found freespace */
1347 xfs_extlen_t flen; /* length of found freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int i; /* temp status variable */
1349 xfs_agblock_t rbno; /* returned block number */
1350 xfs_extlen_t rlen; /* length of returned extent */
1351
1352 /*
1353 * Allocate and initialize a cursor for the by-size btree.
1354 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001355 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1356 args->agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 bno_cur = NULL;
1358 /*
1359 * Look for an entry >= maxlen+alignment-1 blocks.
1360 */
1361 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1362 args->maxlen + args->alignment - 1, &i)))
1363 goto error0;
1364 /*
1365 * If none, then pick up the last entry in the tree unless the
1366 * tree is empty.
1367 */
1368 if (!i) {
1369 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
1370 &flen, &i)))
1371 goto error0;
1372 if (i == 0 || flen == 0) {
1373 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1374 TRACE_ALLOC("noentry", args);
1375 return 0;
1376 }
1377 ASSERT(i == 1);
1378 }
1379 /*
1380 * There's a freespace as big as maxlen+alignment-1, get it.
1381 */
1382 else {
1383 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
1384 goto error0;
1385 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1386 }
1387 /*
1388 * In the first case above, we got the last entry in the
1389 * by-size btree. Now we check to see if the space hits maxlen
1390 * once aligned; if not, we search left for something better.
1391 * This can't happen in the second case above.
1392 */
1393 xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
1394 &rbno, &rlen);
1395 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1396 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1397 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1398 if (rlen < args->maxlen) {
1399 xfs_agblock_t bestfbno;
1400 xfs_extlen_t bestflen;
1401 xfs_agblock_t bestrbno;
1402 xfs_extlen_t bestrlen;
1403
1404 bestrlen = rlen;
1405 bestrbno = rbno;
1406 bestflen = flen;
1407 bestfbno = fbno;
1408 for (;;) {
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001409 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 goto error0;
1411 if (i == 0)
1412 break;
1413 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1414 &i)))
1415 goto error0;
1416 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1417 if (flen < bestrlen)
1418 break;
1419 xfs_alloc_compute_aligned(fbno, flen, args->alignment,
1420 args->minlen, &rbno, &rlen);
1421 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1422 XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
1423 (rlen <= flen && rbno + rlen <= fbno + flen),
1424 error0);
1425 if (rlen > bestrlen) {
1426 bestrlen = rlen;
1427 bestrbno = rbno;
1428 bestflen = flen;
1429 bestfbno = fbno;
1430 if (rlen == args->maxlen)
1431 break;
1432 }
1433 }
1434 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1435 &i)))
1436 goto error0;
1437 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1438 rlen = bestrlen;
1439 rbno = bestrbno;
1440 flen = bestflen;
1441 fbno = bestfbno;
1442 }
1443 args->wasfromfl = 0;
1444 /*
1445 * Fix up the length.
1446 */
1447 args->len = rlen;
1448 xfs_alloc_fix_len(args);
1449 if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
1450 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1451 TRACE_ALLOC("nominleft", args);
1452 args->agbno = NULLAGBLOCK;
1453 return 0;
1454 }
1455 rlen = args->len;
1456 XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
1457 /*
1458 * Allocate and initialize a cursor for the by-block tree.
1459 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001460 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1461 args->agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1463 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1464 goto error0;
1465 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1466 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1467 cnt_cur = bno_cur = NULL;
1468 args->len = rlen;
1469 args->agbno = rbno;
1470 XFS_WANT_CORRUPTED_GOTO(
1471 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001472 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 error0);
1474 TRACE_ALLOC("normal", args);
1475 return 0;
1476
1477error0:
1478 TRACE_ALLOC("error", args);
1479 if (cnt_cur)
1480 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1481 if (bno_cur)
1482 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1483 return error;
1484}
1485
1486/*
1487 * Deal with the case where only small freespaces remain.
1488 * Either return the contents of the last freespace record,
1489 * or allocate space from the freelist if there is nothing in the tree.
1490 */
1491STATIC int /* error */
1492xfs_alloc_ag_vextent_small(
1493 xfs_alloc_arg_t *args, /* allocation argument structure */
1494 xfs_btree_cur_t *ccur, /* by-size cursor */
1495 xfs_agblock_t *fbnop, /* result block number */
1496 xfs_extlen_t *flenp, /* result length */
1497 int *stat) /* status: 0-freelist, 1-normal/none */
1498{
1499 int error;
1500 xfs_agblock_t fbno;
1501 xfs_extlen_t flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 int i;
1503
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001504 if ((error = xfs_btree_decrement(ccur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto error0;
1506 if (i) {
1507 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1508 goto error0;
1509 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1510 }
1511 /*
1512 * Nothing in the btree, try the freelist. Make sure
1513 * to respect minleft even when pulling from the
1514 * freelist.
1515 */
1516 else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
Christoph Hellwig16259e72005-11-02 15:11:25 +11001517 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1518 > args->minleft)) {
David Chinner92821e22007-05-24 15:26:31 +10001519 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1520 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 goto error0;
1522 if (fbno != NULLAGBLOCK) {
1523 if (args->userdata) {
1524 xfs_buf_t *bp;
1525
1526 bp = xfs_btree_get_bufs(args->mp, args->tp,
1527 args->agno, fbno, 0);
1528 xfs_trans_binval(args->tp, bp);
1529 }
1530 args->len = 1;
1531 args->agbno = fbno;
1532 XFS_WANT_CORRUPTED_GOTO(
1533 args->agbno + args->len <=
Christoph Hellwig16259e72005-11-02 15:11:25 +11001534 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 error0);
1536 args->wasfromfl = 1;
1537 TRACE_ALLOC("freelist", args);
1538 *stat = 0;
1539 return 0;
1540 }
1541 /*
1542 * Nothing in the freelist.
1543 */
1544 else
1545 flen = 0;
1546 }
1547 /*
1548 * Can't allocate from the freelist for some reason.
1549 */
Nathan Scottd432c802006-09-28 11:03:44 +10001550 else {
1551 fbno = NULLAGBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 flen = 0;
Nathan Scottd432c802006-09-28 11:03:44 +10001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * Can't do the allocation, give up.
1556 */
1557 if (flen < args->minlen) {
1558 args->agbno = NULLAGBLOCK;
1559 TRACE_ALLOC("notenough", args);
1560 flen = 0;
1561 }
1562 *fbnop = fbno;
1563 *flenp = flen;
1564 *stat = 1;
1565 TRACE_ALLOC("normal", args);
1566 return 0;
1567
1568error0:
1569 TRACE_ALLOC("error", args);
1570 return error;
1571}
1572
1573/*
1574 * Free the extent starting at agno/bno for length.
1575 */
1576STATIC int /* error */
1577xfs_free_ag_extent(
1578 xfs_trans_t *tp, /* transaction pointer */
1579 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
1580 xfs_agnumber_t agno, /* allocation group number */
1581 xfs_agblock_t bno, /* starting block number */
1582 xfs_extlen_t len, /* length of extent */
1583 int isfl) /* set if is freelist blocks - no sb acctg */
1584{
1585 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1586 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1587 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 xfs_agblock_t gtbno; /* start of right neighbor block */
1589 xfs_extlen_t gtlen; /* length of right neighbor block */
1590 int haveleft; /* have a left neighbor block */
1591 int haveright; /* have a right neighbor block */
1592 int i; /* temp, result code */
1593 xfs_agblock_t ltbno; /* start of left neighbor block */
1594 xfs_extlen_t ltlen; /* length of left neighbor block */
1595 xfs_mount_t *mp; /* mount point struct for filesystem */
1596 xfs_agblock_t nbno; /* new starting block of freespace */
1597 xfs_extlen_t nlen; /* new length of freespace */
1598
1599 mp = tp->t_mountp;
1600 /*
1601 * Allocate and initialize a cursor for the by-block btree.
1602 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001603 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 cnt_cur = NULL;
1605 /*
1606 * Look for a neighboring block on the left (lower block numbers)
1607 * that is contiguous with this space.
1608 */
1609 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1610 goto error0;
1611 if (haveleft) {
1612 /*
1613 * There is a block to our left.
1614 */
1615 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1616 goto error0;
1617 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1618 /*
1619 * It's not contiguous, though.
1620 */
1621 if (ltbno + ltlen < bno)
1622 haveleft = 0;
1623 else {
1624 /*
1625 * If this failure happens the request to free this
1626 * space was invalid, it's (partly) already free.
1627 * Very bad.
1628 */
1629 XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
1630 }
1631 }
1632 /*
1633 * Look for a neighboring block on the right (higher block numbers)
1634 * that is contiguous with this space.
1635 */
Christoph Hellwig637aa502008-10-30 16:55:45 +11001636 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto error0;
1638 if (haveright) {
1639 /*
1640 * There is a block to our right.
1641 */
1642 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1643 goto error0;
1644 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1645 /*
1646 * It's not contiguous, though.
1647 */
1648 if (bno + len < gtbno)
1649 haveright = 0;
1650 else {
1651 /*
1652 * If this failure happens the request to free this
1653 * space was invalid, it's (partly) already free.
1654 * Very bad.
1655 */
1656 XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
1657 }
1658 }
1659 /*
1660 * Now allocate and initialize a cursor for the by-size tree.
1661 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001662 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /*
1664 * Have both left and right contiguous neighbors.
1665 * Merge all three into a single free block.
1666 */
1667 if (haveleft && haveright) {
1668 /*
1669 * Delete the old by-size entry on the left.
1670 */
1671 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1672 goto error0;
1673 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001674 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto error0;
1676 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1677 /*
1678 * Delete the old by-size entry on the right.
1679 */
1680 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1681 goto error0;
1682 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001683 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto error0;
1685 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1686 /*
1687 * Delete the old by-block entry for the right block.
1688 */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001689 if ((error = xfs_btree_delete(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto error0;
1691 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1692 /*
1693 * Move the by-block cursor back to the left neighbor.
1694 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001695 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto error0;
1697 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1698#ifdef DEBUG
1699 /*
1700 * Check that this is the right record: delete didn't
1701 * mangle the cursor.
1702 */
1703 {
1704 xfs_agblock_t xxbno;
1705 xfs_extlen_t xxlen;
1706
1707 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1708 &i)))
1709 goto error0;
1710 XFS_WANT_CORRUPTED_GOTO(
1711 i == 1 && xxbno == ltbno && xxlen == ltlen,
1712 error0);
1713 }
1714#endif
1715 /*
1716 * Update remaining by-block entry to the new, joined block.
1717 */
1718 nbno = ltbno;
1719 nlen = len + ltlen + gtlen;
1720 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1721 goto error0;
1722 }
1723 /*
1724 * Have only a left contiguous neighbor.
1725 * Merge it together with the new freespace.
1726 */
1727 else if (haveleft) {
1728 /*
1729 * Delete the old by-size entry on the left.
1730 */
1731 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1732 goto error0;
1733 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001734 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto error0;
1736 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1737 /*
1738 * Back up the by-block cursor to the left neighbor, and
1739 * update its length.
1740 */
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001741 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 goto error0;
1743 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1744 nbno = ltbno;
1745 nlen = len + ltlen;
1746 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1747 goto error0;
1748 }
1749 /*
1750 * Have only a right contiguous neighbor.
1751 * Merge it together with the new freespace.
1752 */
1753 else if (haveright) {
1754 /*
1755 * Delete the old by-size entry on the right.
1756 */
1757 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1758 goto error0;
1759 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001760 if ((error = xfs_btree_delete(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto error0;
1762 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1763 /*
1764 * Update the starting block and length of the right
1765 * neighbor in the by-block tree.
1766 */
1767 nbno = bno;
1768 nlen = len + gtlen;
1769 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1770 goto error0;
1771 }
1772 /*
1773 * No contiguous neighbors.
1774 * Insert the new freespace into the by-block tree.
1775 */
1776 else {
1777 nbno = bno;
1778 nlen = len;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001779 if ((error = xfs_btree_insert(bno_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto error0;
1781 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1782 }
1783 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1784 bno_cur = NULL;
1785 /*
1786 * In all cases we need to insert the new freespace in the by-size tree.
1787 */
1788 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1789 goto error0;
1790 XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001791 if ((error = xfs_btree_insert(cnt_cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto error0;
1793 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1794 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1795 cnt_cur = NULL;
1796 /*
1797 * Update the freespace totals in the ag and superblock.
1798 */
1799 {
1800 xfs_agf_t *agf;
1801 xfs_perag_t *pag; /* per allocation group data */
1802
1803 agf = XFS_BUF_TO_AGF(agbp);
1804 pag = &mp->m_perag[agno];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001805 be32_add_cpu(&agf->agf_freeblks, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 xfs_trans_agblocks_delta(tp, len);
1807 pag->pagf_freeblks += len;
1808 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig16259e72005-11-02 15:11:25 +11001809 be32_to_cpu(agf->agf_freeblks) <=
1810 be32_to_cpu(agf->agf_length),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 error0);
1812 TRACE_MODAGF(NULL, agf, XFS_AGF_FREEBLKS);
1813 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
1814 if (!isfl)
1815 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
1816 XFS_STATS_INC(xs_freex);
1817 XFS_STATS_ADD(xs_freeb, len);
1818 }
1819 TRACE_FREE(haveleft ?
1820 (haveright ? "both" : "left") :
1821 (haveright ? "right" : "none"),
1822 agno, bno, len, isfl);
1823
1824 /*
1825 * Since blocks move to the free list without the coordination
1826 * used in xfs_bmap_finish, we can't allow block to be available
1827 * for reallocation and non-transaction writing (user data)
1828 * until we know that the transaction that moved it to the free
1829 * list is permanently on disk. We track the blocks by declaring
1830 * these blocks as "busy"; the busy list is maintained on a per-ag
1831 * basis and each transaction records which entries should be removed
1832 * when the iclog commits to disk. If a busy block is allocated,
1833 * the iclog is pushed up to the LSN that freed the block.
1834 */
1835 xfs_alloc_mark_busy(tp, agno, bno, len);
1836 return 0;
1837
1838 error0:
1839 TRACE_FREE("error", agno, bno, len, isfl);
1840 if (bno_cur)
1841 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1842 if (cnt_cur)
1843 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1844 return error;
1845}
1846
1847/*
1848 * Visible (exported) allocation/free functions.
1849 * Some of these are used just by xfs_alloc_btree.c and this file.
1850 */
1851
1852/*
1853 * Compute and fill in value of m_ag_maxlevels.
1854 */
1855void
1856xfs_alloc_compute_maxlevels(
1857 xfs_mount_t *mp) /* file system mount structure */
1858{
1859 int level;
1860 uint maxblocks;
1861 uint maxleafents;
1862 int minleafrecs;
1863 int minnoderecs;
1864
1865 maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
1866 minleafrecs = mp->m_alloc_mnr[0];
1867 minnoderecs = mp->m_alloc_mnr[1];
1868 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1869 for (level = 1; maxblocks > 1; level++)
1870 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1871 mp->m_ag_maxlevels = level;
1872}
1873
1874/*
Dave Chinner6cc87642009-03-16 08:29:46 +01001875 * Find the length of the longest extent in an AG.
1876 */
1877xfs_extlen_t
1878xfs_alloc_longest_free_extent(
1879 struct xfs_mount *mp,
1880 struct xfs_perag *pag)
1881{
1882 xfs_extlen_t need, delta = 0;
1883
1884 need = XFS_MIN_FREELIST_PAG(pag, mp);
1885 if (need > pag->pagf_flcount)
1886 delta = need - pag->pagf_flcount;
1887
1888 if (pag->pagf_longest > delta)
1889 return pag->pagf_longest - delta;
1890 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
1891}
1892
1893/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * Decide whether to use this allocation group for this allocation.
1895 * If so, fix up the btree freelist's size.
1896 */
1897STATIC int /* error */
1898xfs_alloc_fix_freelist(
1899 xfs_alloc_arg_t *args, /* allocation argument structure */
1900 int flags) /* XFS_ALLOC_FLAG_... */
1901{
1902 xfs_buf_t *agbp; /* agf buffer pointer */
1903 xfs_agf_t *agf; /* a.g. freespace structure pointer */
1904 xfs_buf_t *agflbp;/* agfl buffer pointer */
1905 xfs_agblock_t bno; /* freelist block */
1906 xfs_extlen_t delta; /* new blocks needed in freelist */
1907 int error; /* error result code */
1908 xfs_extlen_t longest;/* longest extent in allocation group */
1909 xfs_mount_t *mp; /* file system mount point structure */
1910 xfs_extlen_t need; /* total blocks needed in freelist */
1911 xfs_perag_t *pag; /* per-ag information structure */
1912 xfs_alloc_arg_t targs; /* local allocation arguments */
1913 xfs_trans_t *tp; /* transaction pointer */
1914
1915 mp = args->mp;
1916
1917 pag = args->pag;
1918 tp = args->tp;
1919 if (!pag->pagf_init) {
1920 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1921 &agbp)))
1922 return error;
1923 if (!pag->pagf_init) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001924 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1925 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 args->agbp = NULL;
1927 return 0;
1928 }
1929 } else
1930 agbp = NULL;
1931
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001932 /*
1933 * If this is a metadata preferred pag and we are user data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * then try somewhere else if we are not being asked to
1935 * try harder at this point
1936 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001937 if (pag->pagf_metadata && args->userdata &&
1938 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
1939 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 args->agbp = NULL;
1941 return 0;
1942 }
1943
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001944 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001945 /*
1946 * If it looks like there isn't a long enough extent, or enough
1947 * total blocks, reject it.
1948 */
Dave Chinner6cc87642009-03-16 08:29:46 +01001949 need = XFS_MIN_FREELIST_PAG(pag, mp);
1950 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001951 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1952 longest ||
1953 ((int)(pag->pagf_freeblks + pag->pagf_flcount -
1954 need - args->total) < (int)args->minleft)) {
1955 if (agbp)
1956 xfs_trans_brelse(tp, agbp);
1957 args->agbp = NULL;
1958 return 0;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * Get the a.g. freespace buffer.
1964 * Can fail if we're not blocking on locks, and it's held.
1965 */
1966 if (agbp == NULL) {
1967 if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
1968 &agbp)))
1969 return error;
1970 if (agbp == NULL) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001971 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
1972 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 args->agbp = NULL;
1974 return 0;
1975 }
1976 }
1977 /*
1978 * Figure out how many blocks we should have in the freelist.
1979 */
1980 agf = XFS_BUF_TO_AGF(agbp);
1981 need = XFS_MIN_FREELIST(agf, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * If there isn't enough total or single-extent, reject it.
1984 */
Nathan Scott0e1edbd2006-08-10 14:40:41 +10001985 if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
1986 delta = need > be32_to_cpu(agf->agf_flcount) ?
1987 (need - be32_to_cpu(agf->agf_flcount)) : 0;
1988 longest = be32_to_cpu(agf->agf_longest);
1989 longest = (longest > delta) ? (longest - delta) :
1990 (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
1991 if ((args->minlen + args->alignment + args->minalignslop - 1) >
1992 longest ||
1993 ((int)(be32_to_cpu(agf->agf_freeblks) +
1994 be32_to_cpu(agf->agf_flcount) - need - args->total) <
1995 (int)args->minleft)) {
1996 xfs_trans_brelse(tp, agbp);
1997 args->agbp = NULL;
1998 return 0;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 /*
2002 * Make the freelist shorter if it's too long.
2003 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002004 while (be32_to_cpu(agf->agf_flcount) > need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 xfs_buf_t *bp;
2006
David Chinner92821e22007-05-24 15:26:31 +10002007 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2008 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return error;
2010 if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
2011 return error;
2012 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2013 xfs_trans_binval(tp, bp);
2014 }
2015 /*
2016 * Initialize the args structure.
2017 */
2018 targs.tp = tp;
2019 targs.mp = mp;
2020 targs.agbp = agbp;
2021 targs.agno = args->agno;
2022 targs.mod = targs.minleft = targs.wasdel = targs.userdata =
2023 targs.minalignslop = 0;
2024 targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
2025 targs.type = XFS_ALLOCTYPE_THIS_AG;
2026 targs.pag = pag;
2027 if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
2028 return error;
2029 /*
2030 * Make the freelist longer if it's too short.
2031 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002032 while (be32_to_cpu(agf->agf_flcount) < need) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 targs.agbno = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002034 targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /*
2036 * Allocate as many blocks as possible at once.
2037 */
Nathan Scotte63a3692006-05-08 19:51:58 +10002038 if ((error = xfs_alloc_ag_vextent(&targs))) {
2039 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return error;
Nathan Scotte63a3692006-05-08 19:51:58 +10002041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Stop if we run out. Won't happen if callers are obeying
2044 * the restrictions correctly. Can happen for free calls
2045 * on a completely full ag.
2046 */
Yingping Lud210a282006-06-09 14:55:18 +10002047 if (targs.agbno == NULLAGBLOCK) {
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002048 if (flags & XFS_ALLOC_FLAG_FREEING)
2049 break;
2050 xfs_trans_brelse(tp, agflbp);
2051 args->agbp = NULL;
2052 return 0;
Yingping Lud210a282006-06-09 14:55:18 +10002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
2055 * Put each allocated block on the list.
2056 */
2057 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
David Chinner92821e22007-05-24 15:26:31 +10002058 error = xfs_alloc_put_freelist(tp, agbp,
2059 agflbp, bno, 0);
2060 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return error;
2062 }
2063 }
Nathan Scotte63a3692006-05-08 19:51:58 +10002064 xfs_trans_brelse(tp, agflbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 args->agbp = agbp;
2066 return 0;
2067}
2068
2069/*
2070 * Get a block from the freelist.
2071 * Returns with the buffer for the block gotten.
2072 */
2073int /* error */
2074xfs_alloc_get_freelist(
2075 xfs_trans_t *tp, /* transaction pointer */
2076 xfs_buf_t *agbp, /* buffer containing the agf structure */
David Chinner92821e22007-05-24 15:26:31 +10002077 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2078 int btreeblk) /* destination is a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 xfs_agf_t *agf; /* a.g. freespace structure */
2081 xfs_agfl_t *agfl; /* a.g. freelist structure */
2082 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2083 xfs_agblock_t bno; /* block number returned */
2084 int error;
David Chinner92821e22007-05-24 15:26:31 +10002085 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 xfs_mount_t *mp; /* mount structure */
2087 xfs_perag_t *pag; /* per allocation group data */
2088
2089 agf = XFS_BUF_TO_AGF(agbp);
2090 /*
2091 * Freelist is empty, give up.
2092 */
2093 if (!agf->agf_flcount) {
2094 *bnop = NULLAGBLOCK;
2095 return 0;
2096 }
2097 /*
2098 * Read the array of free blocks.
2099 */
2100 mp = tp->t_mountp;
2101 if ((error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002102 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return error;
2104 agfl = XFS_BUF_TO_AGFL(agflbp);
2105 /*
2106 * Get the block number and update the data structures.
2107 */
Christoph Hellwige2101002006-09-28 10:56:51 +10002108 bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002109 be32_add_cpu(&agf->agf_flfirst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 xfs_trans_brelse(tp, agflbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002111 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 agf->agf_flfirst = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002113 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002114 be32_add_cpu(&agf->agf_flcount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 xfs_trans_agflist_delta(tp, -1);
2116 pag->pagf_flcount--;
David Chinner92821e22007-05-24 15:26:31 +10002117
2118 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2119 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002120 be32_add_cpu(&agf->agf_btreeblks, 1);
David Chinner92821e22007-05-24 15:26:31 +10002121 pag->pagf_btreeblks++;
2122 logflags |= XFS_AGF_BTREEBLKS;
2123 }
2124
2125 TRACE_MODAGF(NULL, agf, logflags);
2126 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 *bnop = bno;
2128
2129 /*
2130 * As blocks are freed, they are added to the per-ag busy list
2131 * and remain there until the freeing transaction is committed to
2132 * disk. Now that we have allocated blocks, this list must be
2133 * searched to see if a block is being reused. If one is, then
2134 * the freeing transaction must be pushed to disk NOW by forcing
2135 * to disk all iclogs up that transaction's LSN.
2136 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002137 xfs_alloc_search_busy(tp, be32_to_cpu(agf->agf_seqno), bno, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140
2141/*
2142 * Log the given fields from the agf structure.
2143 */
2144void
2145xfs_alloc_log_agf(
2146 xfs_trans_t *tp, /* transaction pointer */
2147 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2148 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2149{
2150 int first; /* first byte offset */
2151 int last; /* last byte offset */
2152 static const short offsets[] = {
2153 offsetof(xfs_agf_t, agf_magicnum),
2154 offsetof(xfs_agf_t, agf_versionnum),
2155 offsetof(xfs_agf_t, agf_seqno),
2156 offsetof(xfs_agf_t, agf_length),
2157 offsetof(xfs_agf_t, agf_roots[0]),
2158 offsetof(xfs_agf_t, agf_levels[0]),
2159 offsetof(xfs_agf_t, agf_flfirst),
2160 offsetof(xfs_agf_t, agf_fllast),
2161 offsetof(xfs_agf_t, agf_flcount),
2162 offsetof(xfs_agf_t, agf_freeblks),
2163 offsetof(xfs_agf_t, agf_longest),
David Chinner92821e22007-05-24 15:26:31 +10002164 offsetof(xfs_agf_t, agf_btreeblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sizeof(xfs_agf_t)
2166 };
2167
2168 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2169 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2170}
2171
2172/*
2173 * Interface for inode allocation to force the pag data to be initialized.
2174 */
2175int /* error */
2176xfs_alloc_pagf_init(
2177 xfs_mount_t *mp, /* file system mount structure */
2178 xfs_trans_t *tp, /* transaction pointer */
2179 xfs_agnumber_t agno, /* allocation group number */
2180 int flags) /* XFS_ALLOC_FLAGS_... */
2181{
2182 xfs_buf_t *bp;
2183 int error;
2184
2185 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2186 return error;
2187 if (bp)
2188 xfs_trans_brelse(tp, bp);
2189 return 0;
2190}
2191
2192/*
2193 * Put the block on the freelist for the allocation group.
2194 */
2195int /* error */
2196xfs_alloc_put_freelist(
2197 xfs_trans_t *tp, /* transaction pointer */
2198 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2199 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
David Chinner92821e22007-05-24 15:26:31 +10002200 xfs_agblock_t bno, /* block being freed */
2201 int btreeblk) /* block came from a AGF btree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 xfs_agf_t *agf; /* a.g. freespace structure */
2204 xfs_agfl_t *agfl; /* a.g. free block array */
Christoph Hellwige2101002006-09-28 10:56:51 +10002205 __be32 *blockp;/* pointer to array entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int error;
David Chinner92821e22007-05-24 15:26:31 +10002207 int logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 xfs_mount_t *mp; /* mount structure */
2209 xfs_perag_t *pag; /* per allocation group data */
2210
2211 agf = XFS_BUF_TO_AGF(agbp);
2212 mp = tp->t_mountp;
2213
2214 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
Christoph Hellwig16259e72005-11-02 15:11:25 +11002215 be32_to_cpu(agf->agf_seqno), &agflbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return error;
2217 agfl = XFS_BUF_TO_AGFL(agflbp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002218 be32_add_cpu(&agf->agf_fllast, 1);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002219 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 agf->agf_fllast = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002221 pag = &mp->m_perag[be32_to_cpu(agf->agf_seqno)];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002222 be32_add_cpu(&agf->agf_flcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 xfs_trans_agflist_delta(tp, 1);
2224 pag->pagf_flcount++;
David Chinner92821e22007-05-24 15:26:31 +10002225
2226 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2227 if (btreeblk) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002228 be32_add_cpu(&agf->agf_btreeblks, -1);
David Chinner92821e22007-05-24 15:26:31 +10002229 pag->pagf_btreeblks--;
2230 logflags |= XFS_AGF_BTREEBLKS;
2231 }
2232
2233 TRACE_MODAGF(NULL, agf, logflags);
2234 xfs_alloc_log_agf(tp, agbp, logflags);
2235
Christoph Hellwig16259e72005-11-02 15:11:25 +11002236 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2237 blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
Christoph Hellwige2101002006-09-28 10:56:51 +10002238 *blockp = cpu_to_be32(bno);
David Chinner92821e22007-05-24 15:26:31 +10002239 TRACE_MODAGF(NULL, agf, logflags);
2240 xfs_alloc_log_agf(tp, agbp, logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 xfs_trans_log_buf(tp, agflbp,
2242 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
2243 (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
2244 sizeof(xfs_agblock_t) - 1));
2245 return 0;
2246}
2247
2248/*
2249 * Read in the allocation group header (free/alloc section).
2250 */
2251int /* error */
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002252xfs_read_agf(
2253 struct xfs_mount *mp, /* mount point structure */
2254 struct xfs_trans *tp, /* transaction pointer */
2255 xfs_agnumber_t agno, /* allocation group number */
2256 int flags, /* XFS_BUF_ */
2257 struct xfs_buf **bpp) /* buffer for the ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002259 struct xfs_agf *agf; /* ag freelist header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 int agf_ok; /* set if agf is consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 int error;
2262
2263 ASSERT(agno != NULLAGNUMBER);
2264 error = xfs_trans_read_buf(
2265 mp, tp, mp->m_ddev_targp,
2266 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002267 XFS_FSS_TO_BB(mp, 1), flags, bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (error)
2269 return error;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002270 if (!*bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002272
2273 ASSERT(!XFS_BUF_GETERROR(*bpp));
2274 agf = XFS_BUF_TO_AGF(*bpp);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /*
2277 * Validate the magic number of the agf block.
2278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 agf_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002280 be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
2281 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2282 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2283 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2284 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002285 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
2286 be32_to_cpu(agf->agf_seqno) == agno;
Barry Naujok89b28392008-10-30 17:05:49 +11002287 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
2288 agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
2289 be32_to_cpu(agf->agf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
2291 XFS_RANDOM_ALLOC_READ_AGF))) {
2292 XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
2293 XFS_ERRLEVEL_LOW, mp, agf);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002294 xfs_trans_brelse(tp, *bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return XFS_ERROR(EFSCORRUPTED);
2296 }
From: Christoph Hellwig48056212008-11-28 14:23:38 +11002297
2298 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
2299 return 0;
2300}
2301
2302/*
2303 * Read in the allocation group header (free/alloc section).
2304 */
2305int /* error */
2306xfs_alloc_read_agf(
2307 struct xfs_mount *mp, /* mount point structure */
2308 struct xfs_trans *tp, /* transaction pointer */
2309 xfs_agnumber_t agno, /* allocation group number */
2310 int flags, /* XFS_ALLOC_FLAG_... */
2311 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2312{
2313 struct xfs_agf *agf; /* ag freelist header */
2314 struct xfs_perag *pag; /* per allocation group data */
2315 int error;
2316
2317 ASSERT(agno != NULLAGNUMBER);
2318
2319 error = xfs_read_agf(mp, tp, agno,
2320 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XFS_BUF_TRYLOCK : 0,
2321 bpp);
2322 if (error)
2323 return error;
2324 if (!*bpp)
2325 return 0;
2326 ASSERT(!XFS_BUF_GETERROR(*bpp));
2327
2328 agf = XFS_BUF_TO_AGF(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 pag = &mp->m_perag[agno];
2330 if (!pag->pagf_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002331 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
David Chinner92821e22007-05-24 15:26:31 +10002332 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002333 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2334 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 pag->pagf_levels[XFS_BTNUM_BNOi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002336 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 pag->pagf_levels[XFS_BTNUM_CNTi] =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002338 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
Eric Sandeen007c61c2007-10-11 17:43:56 +10002339 spin_lock_init(&pag->pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 pag->pagb_list = kmem_zalloc(XFS_PAGB_NUM_SLOTS *
2341 sizeof(xfs_perag_busy_t), KM_SLEEP);
2342 pag->pagf_init = 1;
2343 }
2344#ifdef DEBUG
2345 else if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002346 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
Barry Naujok89b28392008-10-30 17:05:49 +11002347 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
Christoph Hellwig16259e72005-11-02 15:11:25 +11002348 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2349 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002351 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
Christoph Hellwig16259e72005-11-02 15:11:25 +11002353 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return 0;
2357}
2358
2359/*
2360 * Allocate an extent (variable-size).
2361 * Depending on the allocation type, we either look in a single allocation
2362 * group or loop over the allocation groups to find the result.
2363 */
2364int /* error */
2365xfs_alloc_vextent(
2366 xfs_alloc_arg_t *args) /* allocation argument structure */
2367{
2368 xfs_agblock_t agsize; /* allocation group size */
2369 int error;
2370 int flags; /* XFS_ALLOC_FLAG_... locking flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 xfs_extlen_t minleft;/* minimum left value, temp copy */
2372 xfs_mount_t *mp; /* mount structure pointer */
2373 xfs_agnumber_t sagno; /* starting allocation group number */
2374 xfs_alloctype_t type; /* input allocation type */
2375 int bump_rotor = 0;
2376 int no_min = 0;
2377 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2378
2379 mp = args->mp;
2380 type = args->otype = args->type;
2381 args->agbno = NULLAGBLOCK;
2382 /*
2383 * Just fix this up, for the case where the last a.g. is shorter
2384 * (or there's only one a.g.) and the caller couldn't easily figure
2385 * that out (xfs_bmap_alloc).
2386 */
2387 agsize = mp->m_sb.sb_agblocks;
2388 if (args->maxlen > agsize)
2389 args->maxlen = agsize;
2390 if (args->alignment == 0)
2391 args->alignment = 1;
2392 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2393 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2394 ASSERT(args->minlen <= args->maxlen);
2395 ASSERT(args->minlen <= agsize);
2396 ASSERT(args->mod < args->prod);
2397 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2398 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2399 args->minlen > args->maxlen || args->minlen > agsize ||
2400 args->mod >= args->prod) {
2401 args->fsbno = NULLFSBLOCK;
2402 TRACE_ALLOC("badargs", args);
2403 return 0;
2404 }
2405 minleft = args->minleft;
2406
2407 switch (type) {
2408 case XFS_ALLOCTYPE_THIS_AG:
2409 case XFS_ALLOCTYPE_NEAR_BNO:
2410 case XFS_ALLOCTYPE_THIS_BNO:
2411 /*
2412 * These three force us into a single a.g.
2413 */
2414 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2415 down_read(&mp->m_peraglock);
2416 args->pag = &mp->m_perag[args->agno];
2417 args->minleft = 0;
2418 error = xfs_alloc_fix_freelist(args, 0);
2419 args->minleft = minleft;
2420 if (error) {
2421 TRACE_ALLOC("nofix", args);
2422 goto error0;
2423 }
2424 if (!args->agbp) {
2425 up_read(&mp->m_peraglock);
2426 TRACE_ALLOC("noagbp", args);
2427 break;
2428 }
2429 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2430 if ((error = xfs_alloc_ag_vextent(args)))
2431 goto error0;
2432 up_read(&mp->m_peraglock);
2433 break;
2434 case XFS_ALLOCTYPE_START_BNO:
2435 /*
2436 * Try near allocation first, then anywhere-in-ag after
2437 * the first a.g. fails.
2438 */
2439 if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
2440 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2441 args->fsbno = XFS_AGB_TO_FSB(mp,
2442 ((mp->m_agfrotor / rotorstep) %
2443 mp->m_sb.sb_agcount), 0);
2444 bump_rotor = 1;
2445 }
2446 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2447 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2448 /* FALLTHROUGH */
2449 case XFS_ALLOCTYPE_ANY_AG:
2450 case XFS_ALLOCTYPE_START_AG:
2451 case XFS_ALLOCTYPE_FIRST_AG:
2452 /*
2453 * Rotate through the allocation groups looking for a winner.
2454 */
2455 if (type == XFS_ALLOCTYPE_ANY_AG) {
2456 /*
2457 * Start with the last place we left off.
2458 */
2459 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2460 mp->m_sb.sb_agcount;
2461 args->type = XFS_ALLOCTYPE_THIS_AG;
2462 flags = XFS_ALLOC_FLAG_TRYLOCK;
2463 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2464 /*
2465 * Start with allocation group given by bno.
2466 */
2467 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2468 args->type = XFS_ALLOCTYPE_THIS_AG;
2469 sagno = 0;
2470 flags = 0;
2471 } else {
2472 if (type == XFS_ALLOCTYPE_START_AG)
2473 args->type = XFS_ALLOCTYPE_THIS_AG;
2474 /*
2475 * Start with the given allocation group.
2476 */
2477 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2478 flags = XFS_ALLOC_FLAG_TRYLOCK;
2479 }
2480 /*
2481 * Loop over allocation groups twice; first time with
2482 * trylock set, second time without.
2483 */
2484 down_read(&mp->m_peraglock);
2485 for (;;) {
2486 args->pag = &mp->m_perag[args->agno];
2487 if (no_min) args->minleft = 0;
2488 error = xfs_alloc_fix_freelist(args, flags);
2489 args->minleft = minleft;
2490 if (error) {
2491 TRACE_ALLOC("nofix", args);
2492 goto error0;
2493 }
2494 /*
2495 * If we get a buffer back then the allocation will fly.
2496 */
2497 if (args->agbp) {
2498 if ((error = xfs_alloc_ag_vextent(args)))
2499 goto error0;
2500 break;
2501 }
2502 TRACE_ALLOC("loopfailed", args);
2503 /*
2504 * Didn't work, figure out the next iteration.
2505 */
2506 if (args->agno == sagno &&
2507 type == XFS_ALLOCTYPE_START_BNO)
2508 args->type = XFS_ALLOCTYPE_THIS_AG;
Yingping Lud210a282006-06-09 14:55:18 +10002509 /*
2510 * For the first allocation, we can try any AG to get
2511 * space. However, if we already have allocated a
2512 * block, we don't want to try AGs whose number is below
2513 * sagno. Otherwise, we may end up with out-of-order
2514 * locking of AGF, which might cause deadlock.
2515 */
2516 if (++(args->agno) == mp->m_sb.sb_agcount) {
2517 if (args->firstblock != NULLFSBLOCK)
2518 args->agno = sagno;
2519 else
2520 args->agno = 0;
2521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /*
2523 * Reached the starting a.g., must either be done
2524 * or switch to non-trylock mode.
2525 */
2526 if (args->agno == sagno) {
2527 if (no_min == 1) {
2528 args->agbno = NULLAGBLOCK;
2529 TRACE_ALLOC("allfailed", args);
2530 break;
2531 }
2532 if (flags == 0) {
2533 no_min = 1;
2534 } else {
2535 flags = 0;
2536 if (type == XFS_ALLOCTYPE_START_BNO) {
2537 args->agbno = XFS_FSB_TO_AGBNO(mp,
2538 args->fsbno);
2539 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2540 }
2541 }
2542 }
2543 }
2544 up_read(&mp->m_peraglock);
2545 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2546 if (args->agno == sagno)
2547 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2548 (mp->m_sb.sb_agcount * rotorstep);
2549 else
2550 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2551 (mp->m_sb.sb_agcount * rotorstep);
2552 }
2553 break;
2554 default:
2555 ASSERT(0);
2556 /* NOTREACHED */
2557 }
2558 if (args->agbno == NULLAGBLOCK)
2559 args->fsbno = NULLFSBLOCK;
2560 else {
2561 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2562#ifdef DEBUG
2563 ASSERT(args->len >= args->minlen);
2564 ASSERT(args->len <= args->maxlen);
2565 ASSERT(args->agbno % args->alignment == 0);
2566 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2567 args->len);
2568#endif
2569 }
2570 return 0;
2571error0:
2572 up_read(&mp->m_peraglock);
2573 return error;
2574}
2575
2576/*
2577 * Free an extent.
2578 * Just break up the extent address and hand off to xfs_free_ag_extent
2579 * after fixing up the freelist.
2580 */
2581int /* error */
2582xfs_free_extent(
2583 xfs_trans_t *tp, /* transaction pointer */
2584 xfs_fsblock_t bno, /* starting block number of extent */
2585 xfs_extlen_t len) /* length of extent */
2586{
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002587 xfs_alloc_arg_t args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int error;
2589
2590 ASSERT(len != 0);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002591 memset(&args, 0, sizeof(xfs_alloc_arg_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 args.tp = tp;
2593 args.mp = tp->t_mountp;
2594 args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
2595 ASSERT(args.agno < args.mp->m_sb.sb_agcount);
2596 args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 down_read(&args.mp->m_peraglock);
2598 args.pag = &args.mp->m_perag[args.agno];
Yingping Lud210a282006-06-09 14:55:18 +10002599 if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 goto error0;
2601#ifdef DEBUG
2602 ASSERT(args.agbp != NULL);
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002603 ASSERT((args.agbno + len) <=
2604 be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605#endif
Nathan Scott0e1edbd2006-08-10 14:40:41 +10002606 error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607error0:
2608 up_read(&args.mp->m_peraglock);
2609 return error;
2610}
2611
2612
2613/*
2614 * AG Busy list management
2615 * The busy list contains block ranges that have been freed but whose
Nathan Scottc41564b2006-03-29 08:55:14 +10002616 * transactions have not yet hit disk. If any block listed in a busy
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 * list is reused, the transaction that freed it must be forced to disk
2618 * before continuing to use the block.
2619 *
2620 * xfs_alloc_mark_busy - add to the per-ag busy list
2621 * xfs_alloc_clear_busy - remove an item from the per-ag busy list
2622 */
2623void
2624xfs_alloc_mark_busy(xfs_trans_t *tp,
2625 xfs_agnumber_t agno,
2626 xfs_agblock_t bno,
2627 xfs_extlen_t len)
2628{
2629 xfs_mount_t *mp;
2630 xfs_perag_busy_t *bsy;
2631 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 mp = tp->t_mountp;
Eric Sandeen64137e52007-10-11 17:38:28 +10002634 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* search pagb_list for an open slot */
2637 for (bsy = mp->m_perag[agno].pagb_list, n = 0;
2638 n < XFS_PAGB_NUM_SLOTS;
2639 bsy++, n++) {
2640 if (bsy->busy_tp == NULL) {
2641 break;
2642 }
2643 }
2644
2645 if (n < XFS_PAGB_NUM_SLOTS) {
2646 bsy = &mp->m_perag[agno].pagb_list[n];
2647 mp->m_perag[agno].pagb_count++;
2648 TRACE_BUSY("xfs_alloc_mark_busy", "got", agno, bno, len, n, tp);
2649 bsy->busy_start = bno;
2650 bsy->busy_length = len;
2651 bsy->busy_tp = tp;
2652 xfs_trans_add_busy(tp, agno, n);
2653 } else {
2654 TRACE_BUSY("xfs_alloc_mark_busy", "FULL", agno, bno, len, -1, tp);
2655 /*
2656 * The busy list is full! Since it is now not possible to
2657 * track the free block, make this a synchronous transaction
2658 * to insure that the block is not reused before this
2659 * transaction commits.
2660 */
2661 xfs_trans_set_sync(tp);
2662 }
2663
Eric Sandeen64137e52007-10-11 17:38:28 +10002664 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667void
2668xfs_alloc_clear_busy(xfs_trans_t *tp,
2669 xfs_agnumber_t agno,
2670 int idx)
2671{
2672 xfs_mount_t *mp;
2673 xfs_perag_busy_t *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 mp = tp->t_mountp;
2676
Eric Sandeen64137e52007-10-11 17:38:28 +10002677 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 list = mp->m_perag[agno].pagb_list;
2679
2680 ASSERT(idx < XFS_PAGB_NUM_SLOTS);
2681 if (list[idx].busy_tp == tp) {
2682 TRACE_UNBUSY("xfs_alloc_clear_busy", "found", agno, idx, tp);
2683 list[idx].busy_tp = NULL;
2684 mp->m_perag[agno].pagb_count--;
2685 } else {
2686 TRACE_UNBUSY("xfs_alloc_clear_busy", "missing", agno, idx, tp);
2687 }
2688
Eric Sandeen64137e52007-10-11 17:38:28 +10002689 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
2692
2693/*
David Chinnerf4586e42008-04-10 12:21:25 +10002694 * If we find the extent in the busy list, force the log out to get the
2695 * extent out of the busy list so the caller can use it straight away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 */
David Chinnerf4586e42008-04-10 12:21:25 +10002697STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698xfs_alloc_search_busy(xfs_trans_t *tp,
2699 xfs_agnumber_t agno,
2700 xfs_agblock_t bno,
2701 xfs_extlen_t len)
2702{
2703 xfs_mount_t *mp;
2704 xfs_perag_busy_t *bsy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 xfs_agblock_t uend, bend;
2706 xfs_lsn_t lsn;
2707 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 mp = tp->t_mountp;
2710
Eric Sandeen64137e52007-10-11 17:38:28 +10002711 spin_lock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 cnt = mp->m_perag[agno].pagb_count;
2713
2714 uend = bno + len - 1;
2715
2716 /* search pagb_list for this slot, skipping open slots */
David Chinnerf4586e42008-04-10 12:21:25 +10002717 for (bsy = mp->m_perag[agno].pagb_list; cnt; bsy++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 /*
2720 * (start1,length1) within (start2, length2)
2721 */
2722 if (bsy->busy_tp != NULL) {
2723 bend = bsy->busy_start + bsy->busy_length - 1;
David Chinnerf4586e42008-04-10 12:21:25 +10002724 if ((bno > bend) || (uend < bsy->busy_start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 cnt--;
2726 } else {
2727 TRACE_BUSYSEARCH("xfs_alloc_search_busy",
David Chinnerf4586e42008-04-10 12:21:25 +10002728 "found1", agno, bno, len, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
2730 }
2731 }
2732 }
2733
2734 /*
2735 * If a block was found, force the log through the LSN of the
2736 * transaction that freed the block
2737 */
2738 if (cnt) {
David Chinnerf4586e42008-04-10 12:21:25 +10002739 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "found", agno, bno, len, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 lsn = bsy->busy_tp->t_commit_lsn;
Eric Sandeen64137e52007-10-11 17:38:28 +10002741 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 xfs_log_force(mp, lsn, XFS_LOG_FORCE|XFS_LOG_SYNC);
2743 } else {
David Chinnerf4586e42008-04-10 12:21:25 +10002744 TRACE_BUSYSEARCH("xfs_alloc_search_busy", "not-found", agno, bno, len, tp);
Eric Sandeen64137e52007-10-11 17:38:28 +10002745 spin_unlock(&mp->m_perag[agno].pagb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}