]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_alloc.c
ee72d137f5ddcced669e5a95ff089a2db9094a73
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_alloc.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * 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.
13 *
14 * 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
17 */
18 #include "libxfs_priv.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_shared.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
30 #include "xfs_rmap.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_cksum.h"
34 #include "xfs_trace.h"
35 #include "xfs_trans.h"
36 #include "xfs_ag_resv.h"
37
38 struct workqueue_struct *xfs_alloc_wq;
39
40 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
41
42 #define XFSA_FIXUP_BNO_OK 1
43 #define XFSA_FIXUP_CNT_OK 2
44
45 STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
46 STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
47 STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
48 STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
49 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
50
51 unsigned int
52 xfs_refc_block(
53 struct xfs_mount *mp)
54 {
55 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
56 return XFS_RMAP_BLOCK(mp) + 1;
57 if (xfs_sb_version_hasfinobt(&mp->m_sb))
58 return XFS_FIBT_BLOCK(mp) + 1;
59 return XFS_IBT_BLOCK(mp) + 1;
60 }
61
62 xfs_extlen_t
63 xfs_prealloc_blocks(
64 struct xfs_mount *mp)
65 {
66 if (xfs_sb_version_hasreflink(&mp->m_sb))
67 return xfs_refc_block(mp) + 1;
68 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
69 return XFS_RMAP_BLOCK(mp) + 1;
70 if (xfs_sb_version_hasfinobt(&mp->m_sb))
71 return XFS_FIBT_BLOCK(mp) + 1;
72 return XFS_IBT_BLOCK(mp) + 1;
73 }
74
75 /*
76 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
77 * AGF buffer (PV 947395), we place constraints on the relationship among
78 * actual allocations for data blocks, freelist blocks, and potential file data
79 * bmap btree blocks. However, these restrictions may result in no actual space
80 * allocated for a delayed extent, for example, a data block in a certain AG is
81 * allocated but there is no additional block for the additional bmap btree
82 * block due to a split of the bmap btree of the file. The result of this may
83 * lead to an infinite loop when the file gets flushed to disk and all delayed
84 * extents need to be actually allocated. To get around this, we explicitly set
85 * aside a few blocks which will not be reserved in delayed allocation.
86 *
87 * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
88 * potential split of the file's bmap btree.
89 */
90 unsigned int
91 xfs_alloc_set_aside(
92 struct xfs_mount *mp)
93 {
94 unsigned int blocks;
95
96 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
97 return blocks;
98 }
99
100 /*
101 * When deciding how much space to allocate out of an AG, we limit the
102 * allocation maximum size to the size the AG. However, we cannot use all the
103 * blocks in the AG - some are permanently used by metadata. These
104 * blocks are generally:
105 * - the AG superblock, AGF, AGI and AGFL
106 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
107 * the AGI free inode and rmap btree root blocks.
108 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
109 * - the rmapbt root block
110 *
111 * The AG headers are sector sized, so the amount of space they take up is
112 * dependent on filesystem geometry. The others are all single blocks.
113 */
114 unsigned int
115 xfs_alloc_ag_max_usable(
116 struct xfs_mount *mp)
117 {
118 unsigned int blocks;
119
120 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
121 blocks += XFS_ALLOC_AGFL_RESERVE;
122 blocks += 3; /* AGF, AGI btree root blocks */
123 if (xfs_sb_version_hasfinobt(&mp->m_sb))
124 blocks++; /* finobt root block */
125 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
126 blocks++; /* rmap root block */
127
128 return mp->m_sb.sb_agblocks - blocks;
129 }
130
131 /*
132 * Lookup the record equal to [bno, len] in the btree given by cur.
133 */
134 STATIC int /* error */
135 xfs_alloc_lookup_eq(
136 struct xfs_btree_cur *cur, /* btree cursor */
137 xfs_agblock_t bno, /* starting block of extent */
138 xfs_extlen_t len, /* length of extent */
139 int *stat) /* success/failure */
140 {
141 cur->bc_rec.a.ar_startblock = bno;
142 cur->bc_rec.a.ar_blockcount = len;
143 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
144 }
145
146 /*
147 * Lookup the first record greater than or equal to [bno, len]
148 * in the btree given by cur.
149 */
150 int /* error */
151 xfs_alloc_lookup_ge(
152 struct xfs_btree_cur *cur, /* btree cursor */
153 xfs_agblock_t bno, /* starting block of extent */
154 xfs_extlen_t len, /* length of extent */
155 int *stat) /* success/failure */
156 {
157 cur->bc_rec.a.ar_startblock = bno;
158 cur->bc_rec.a.ar_blockcount = len;
159 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
160 }
161
162 /*
163 * Lookup the first record less than or equal to [bno, len]
164 * in the btree given by cur.
165 */
166 static int /* error */
167 xfs_alloc_lookup_le(
168 struct xfs_btree_cur *cur, /* btree cursor */
169 xfs_agblock_t bno, /* starting block of extent */
170 xfs_extlen_t len, /* length of extent */
171 int *stat) /* success/failure */
172 {
173 cur->bc_rec.a.ar_startblock = bno;
174 cur->bc_rec.a.ar_blockcount = len;
175 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
176 }
177
178 /*
179 * Update the record referred to by cur to the value given
180 * by [bno, len].
181 * This either works (return 0) or gets an EFSCORRUPTED error.
182 */
183 STATIC int /* error */
184 xfs_alloc_update(
185 struct xfs_btree_cur *cur, /* btree cursor */
186 xfs_agblock_t bno, /* starting block of extent */
187 xfs_extlen_t len) /* length of extent */
188 {
189 union xfs_btree_rec rec;
190
191 rec.alloc.ar_startblock = cpu_to_be32(bno);
192 rec.alloc.ar_blockcount = cpu_to_be32(len);
193 return xfs_btree_update(cur, &rec);
194 }
195
196 /*
197 * Get the data from the pointed-to record.
198 */
199 int /* error */
200 xfs_alloc_get_rec(
201 struct xfs_btree_cur *cur, /* btree cursor */
202 xfs_agblock_t *bno, /* output: starting block of extent */
203 xfs_extlen_t *len, /* output: length of extent */
204 int *stat) /* output: success/failure */
205 {
206 union xfs_btree_rec *rec;
207 int error;
208
209 error = xfs_btree_get_rec(cur, &rec, stat);
210 if (!error && *stat == 1) {
211 *bno = be32_to_cpu(rec->alloc.ar_startblock);
212 *len = be32_to_cpu(rec->alloc.ar_blockcount);
213 }
214 return error;
215 }
216
217 /*
218 * Compute aligned version of the found extent.
219 * Takes alignment and min length into account.
220 */
221 STATIC void
222 xfs_alloc_compute_aligned(
223 xfs_alloc_arg_t *args, /* allocation argument structure */
224 xfs_agblock_t foundbno, /* starting block in found extent */
225 xfs_extlen_t foundlen, /* length in found extent */
226 xfs_agblock_t *resbno, /* result block number */
227 xfs_extlen_t *reslen) /* result length */
228 {
229 xfs_agblock_t bno;
230 xfs_extlen_t len;
231 xfs_extlen_t diff;
232
233 /* Trim busy sections out of found extent */
234 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
235
236 /*
237 * If we have a largish extent that happens to start before min_agbno,
238 * see if we can shift it into range...
239 */
240 if (bno < args->min_agbno && bno + len > args->min_agbno) {
241 diff = args->min_agbno - bno;
242 if (len > diff) {
243 bno += diff;
244 len -= diff;
245 }
246 }
247
248 if (args->alignment > 1 && len >= args->minlen) {
249 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
250
251 diff = aligned_bno - bno;
252
253 *resbno = aligned_bno;
254 *reslen = diff >= len ? 0 : len - diff;
255 } else {
256 *resbno = bno;
257 *reslen = len;
258 }
259 }
260
261 /*
262 * Compute best start block and diff for "near" allocations.
263 * freelen >= wantlen already checked by caller.
264 */
265 STATIC xfs_extlen_t /* difference value (absolute) */
266 xfs_alloc_compute_diff(
267 xfs_agblock_t wantbno, /* target starting block */
268 xfs_extlen_t wantlen, /* target length */
269 xfs_extlen_t alignment, /* target alignment */
270 int datatype, /* are we allocating data? */
271 xfs_agblock_t freebno, /* freespace's starting block */
272 xfs_extlen_t freelen, /* freespace's length */
273 xfs_agblock_t *newbnop) /* result: best start block from free */
274 {
275 xfs_agblock_t freeend; /* end of freespace extent */
276 xfs_agblock_t newbno1; /* return block number */
277 xfs_agblock_t newbno2; /* other new block number */
278 xfs_extlen_t newlen1=0; /* length with newbno1 */
279 xfs_extlen_t newlen2=0; /* length with newbno2 */
280 xfs_agblock_t wantend; /* end of target extent */
281 bool userdata = xfs_alloc_is_userdata(datatype);
282
283 ASSERT(freelen >= wantlen);
284 freeend = freebno + freelen;
285 wantend = wantbno + wantlen;
286 /*
287 * We want to allocate from the start of a free extent if it is past
288 * the desired block or if we are allocating user data and the free
289 * extent is before desired block. The second case is there to allow
290 * for contiguous allocation from the remaining free space if the file
291 * grows in the short term.
292 */
293 if (freebno >= wantbno || (userdata && freeend < wantend)) {
294 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
295 newbno1 = NULLAGBLOCK;
296 } else if (freeend >= wantend && alignment > 1) {
297 newbno1 = roundup(wantbno, alignment);
298 newbno2 = newbno1 - alignment;
299 if (newbno1 >= freeend)
300 newbno1 = NULLAGBLOCK;
301 else
302 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
303 if (newbno2 < freebno)
304 newbno2 = NULLAGBLOCK;
305 else
306 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
307 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
308 if (newlen1 < newlen2 ||
309 (newlen1 == newlen2 &&
310 XFS_ABSDIFF(newbno1, wantbno) >
311 XFS_ABSDIFF(newbno2, wantbno)))
312 newbno1 = newbno2;
313 } else if (newbno2 != NULLAGBLOCK)
314 newbno1 = newbno2;
315 } else if (freeend >= wantend) {
316 newbno1 = wantbno;
317 } else if (alignment > 1) {
318 newbno1 = roundup(freeend - wantlen, alignment);
319 if (newbno1 > freeend - wantlen &&
320 newbno1 - alignment >= freebno)
321 newbno1 -= alignment;
322 else if (newbno1 >= freeend)
323 newbno1 = NULLAGBLOCK;
324 } else
325 newbno1 = freeend - wantlen;
326 *newbnop = newbno1;
327 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
328 }
329
330 /*
331 * Fix up the length, based on mod and prod.
332 * len should be k * prod + mod for some k.
333 * If len is too small it is returned unchanged.
334 * If len hits maxlen it is left alone.
335 */
336 STATIC void
337 xfs_alloc_fix_len(
338 xfs_alloc_arg_t *args) /* allocation argument structure */
339 {
340 xfs_extlen_t k;
341 xfs_extlen_t rlen;
342
343 ASSERT(args->mod < args->prod);
344 rlen = args->len;
345 ASSERT(rlen >= args->minlen);
346 ASSERT(rlen <= args->maxlen);
347 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
348 (args->mod == 0 && rlen < args->prod))
349 return;
350 k = rlen % args->prod;
351 if (k == args->mod)
352 return;
353 if (k > args->mod)
354 rlen = rlen - (k - args->mod);
355 else
356 rlen = rlen - args->prod + (args->mod - k);
357 /* casts to (int) catch length underflows */
358 if ((int)rlen < (int)args->minlen)
359 return;
360 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
361 ASSERT(rlen % args->prod == args->mod);
362 args->len = rlen;
363 }
364
365 /*
366 * Fix up length if there is too little space left in the a.g.
367 * Return 1 if ok, 0 if too little, should give up.
368 */
369 STATIC int
370 xfs_alloc_fix_minleft(
371 xfs_alloc_arg_t *args) /* allocation argument structure */
372 {
373 xfs_agf_t *agf; /* a.g. freelist header */
374 int diff; /* free space difference */
375
376 if (args->minleft == 0)
377 return 1;
378 agf = XFS_BUF_TO_AGF(args->agbp);
379 diff = be32_to_cpu(agf->agf_freeblks)
380 - args->len - args->minleft;
381 if (diff >= 0)
382 return 1;
383 args->len += diff; /* shrink the allocated space */
384 /* casts to (int) catch length underflows */
385 if ((int)args->len >= (int)args->minlen)
386 return 1;
387 args->agbno = NULLAGBLOCK;
388 return 0;
389 }
390
391 /*
392 * Update the two btrees, logically removing from freespace the extent
393 * starting at rbno, rlen blocks. The extent is contained within the
394 * actual (current) free extent fbno for flen blocks.
395 * Flags are passed in indicating whether the cursors are set to the
396 * relevant records.
397 */
398 STATIC int /* error code */
399 xfs_alloc_fixup_trees(
400 xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
401 xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
402 xfs_agblock_t fbno, /* starting block of free extent */
403 xfs_extlen_t flen, /* length of free extent */
404 xfs_agblock_t rbno, /* starting block of returned extent */
405 xfs_extlen_t rlen, /* length of returned extent */
406 int flags) /* flags, XFSA_FIXUP_... */
407 {
408 int error; /* error code */
409 int i; /* operation results */
410 xfs_agblock_t nfbno1; /* first new free startblock */
411 xfs_agblock_t nfbno2; /* second new free startblock */
412 xfs_extlen_t nflen1=0; /* first new free length */
413 xfs_extlen_t nflen2=0; /* second new free length */
414 struct xfs_mount *mp;
415
416 mp = cnt_cur->bc_mp;
417
418 /*
419 * Look up the record in the by-size tree if necessary.
420 */
421 if (flags & XFSA_FIXUP_CNT_OK) {
422 #ifdef DEBUG
423 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
424 return error;
425 XFS_WANT_CORRUPTED_RETURN(mp,
426 i == 1 && nfbno1 == fbno && nflen1 == flen);
427 #endif
428 } else {
429 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
430 return error;
431 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
432 }
433 /*
434 * Look up the record in the by-block tree if necessary.
435 */
436 if (flags & XFSA_FIXUP_BNO_OK) {
437 #ifdef DEBUG
438 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
439 return error;
440 XFS_WANT_CORRUPTED_RETURN(mp,
441 i == 1 && nfbno1 == fbno && nflen1 == flen);
442 #endif
443 } else {
444 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
445 return error;
446 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
447 }
448
449 #ifdef DEBUG
450 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
451 struct xfs_btree_block *bnoblock;
452 struct xfs_btree_block *cntblock;
453
454 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
455 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
456
457 XFS_WANT_CORRUPTED_RETURN(mp,
458 bnoblock->bb_numrecs == cntblock->bb_numrecs);
459 }
460 #endif
461
462 /*
463 * Deal with all four cases: the allocated record is contained
464 * within the freespace record, so we can have new freespace
465 * at either (or both) end, or no freespace remaining.
466 */
467 if (rbno == fbno && rlen == flen)
468 nfbno1 = nfbno2 = NULLAGBLOCK;
469 else if (rbno == fbno) {
470 nfbno1 = rbno + rlen;
471 nflen1 = flen - rlen;
472 nfbno2 = NULLAGBLOCK;
473 } else if (rbno + rlen == fbno + flen) {
474 nfbno1 = fbno;
475 nflen1 = flen - rlen;
476 nfbno2 = NULLAGBLOCK;
477 } else {
478 nfbno1 = fbno;
479 nflen1 = rbno - fbno;
480 nfbno2 = rbno + rlen;
481 nflen2 = (fbno + flen) - nfbno2;
482 }
483 /*
484 * Delete the entry from the by-size btree.
485 */
486 if ((error = xfs_btree_delete(cnt_cur, &i)))
487 return error;
488 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
489 /*
490 * Add new by-size btree entry(s).
491 */
492 if (nfbno1 != NULLAGBLOCK) {
493 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
494 return error;
495 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
496 if ((error = xfs_btree_insert(cnt_cur, &i)))
497 return error;
498 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
499 }
500 if (nfbno2 != NULLAGBLOCK) {
501 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
502 return error;
503 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
504 if ((error = xfs_btree_insert(cnt_cur, &i)))
505 return error;
506 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
507 }
508 /*
509 * Fix up the by-block btree entry(s).
510 */
511 if (nfbno1 == NULLAGBLOCK) {
512 /*
513 * No remaining freespace, just delete the by-block tree entry.
514 */
515 if ((error = xfs_btree_delete(bno_cur, &i)))
516 return error;
517 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
518 } else {
519 /*
520 * Update the by-block entry to start later|be shorter.
521 */
522 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
523 return error;
524 }
525 if (nfbno2 != NULLAGBLOCK) {
526 /*
527 * 2 resulting free entries, need to add one.
528 */
529 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
530 return error;
531 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
532 if ((error = xfs_btree_insert(bno_cur, &i)))
533 return error;
534 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
535 }
536 return 0;
537 }
538
539 static bool
540 xfs_agfl_verify(
541 struct xfs_buf *bp)
542 {
543 struct xfs_mount *mp = bp->b_target->bt_mount;
544 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
545 int i;
546
547 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
548 return false;
549 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
550 return false;
551 /*
552 * during growfs operations, the perag is not fully initialised,
553 * so we can't use it for any useful checking. growfs ensures we can't
554 * use it by using uncached buffers that don't have the perag attached
555 * so we can detect and avoid this problem.
556 */
557 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
558 return false;
559
560 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
561 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
562 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
563 return false;
564 }
565
566 return xfs_log_check_lsn(mp,
567 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
568 }
569
570 static void
571 xfs_agfl_read_verify(
572 struct xfs_buf *bp)
573 {
574 struct xfs_mount *mp = bp->b_target->bt_mount;
575
576 /*
577 * There is no verification of non-crc AGFLs because mkfs does not
578 * initialise the AGFL to zero or NULL. Hence the only valid part of the
579 * AGFL is what the AGF says is active. We can't get to the AGF, so we
580 * can't verify just those entries are valid.
581 */
582 if (!xfs_sb_version_hascrc(&mp->m_sb))
583 return;
584
585 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
586 xfs_buf_ioerror(bp, -EFSBADCRC);
587 else if (!xfs_agfl_verify(bp))
588 xfs_buf_ioerror(bp, -EFSCORRUPTED);
589
590 if (bp->b_error)
591 xfs_verifier_error(bp);
592 }
593
594 static void
595 xfs_agfl_write_verify(
596 struct xfs_buf *bp)
597 {
598 struct xfs_mount *mp = bp->b_target->bt_mount;
599 struct xfs_buf_log_item *bip = bp->b_fspriv;
600
601 /* no verification of non-crc AGFLs */
602 if (!xfs_sb_version_hascrc(&mp->m_sb))
603 return;
604
605 if (!xfs_agfl_verify(bp)) {
606 xfs_buf_ioerror(bp, -EFSCORRUPTED);
607 xfs_verifier_error(bp);
608 return;
609 }
610
611 if (bip)
612 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
613
614 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
615 }
616
617 const struct xfs_buf_ops xfs_agfl_buf_ops = {
618 .name = "xfs_agfl",
619 .verify_read = xfs_agfl_read_verify,
620 .verify_write = xfs_agfl_write_verify,
621 };
622
623 /*
624 * Read in the allocation group free block array.
625 */
626 STATIC int /* error */
627 xfs_alloc_read_agfl(
628 xfs_mount_t *mp, /* mount point structure */
629 xfs_trans_t *tp, /* transaction pointer */
630 xfs_agnumber_t agno, /* allocation group number */
631 xfs_buf_t **bpp) /* buffer for the ag free block array */
632 {
633 xfs_buf_t *bp; /* return value */
634 int error;
635
636 ASSERT(agno != NULLAGNUMBER);
637 error = xfs_trans_read_buf(
638 mp, tp, mp->m_ddev_targp,
639 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
640 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
641 if (error)
642 return error;
643 xfs_buf_set_ref(bp, XFS_AGFL_REF);
644 *bpp = bp;
645 return 0;
646 }
647
648 STATIC int
649 xfs_alloc_update_counters(
650 struct xfs_trans *tp,
651 struct xfs_perag *pag,
652 struct xfs_buf *agbp,
653 long len)
654 {
655 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
656
657 pag->pagf_freeblks += len;
658 be32_add_cpu(&agf->agf_freeblks, len);
659
660 xfs_trans_agblocks_delta(tp, len);
661 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
662 be32_to_cpu(agf->agf_length)))
663 return -EFSCORRUPTED;
664
665 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
666 return 0;
667 }
668
669 /*
670 * Allocation group level functions.
671 */
672
673 /*
674 * Allocate a variable extent in the allocation group agno.
675 * Type and bno are used to determine where in the allocation group the
676 * extent will start.
677 * Extent's length (returned in *len) will be between minlen and maxlen,
678 * and of the form k * prod + mod unless there's nothing that large.
679 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
680 */
681 STATIC int /* error */
682 xfs_alloc_ag_vextent(
683 xfs_alloc_arg_t *args) /* argument structure for allocation */
684 {
685 int error=0;
686 xfs_extlen_t reservation;
687 xfs_extlen_t oldmax;
688
689 ASSERT(args->minlen > 0);
690 ASSERT(args->maxlen > 0);
691 ASSERT(args->minlen <= args->maxlen);
692 ASSERT(args->mod < args->prod);
693 ASSERT(args->alignment > 0);
694
695 /*
696 * Clamp maxlen to the amount of free space minus any reservations
697 * that have been made.
698 */
699 oldmax = args->maxlen;
700 reservation = xfs_ag_resv_needed(args->pag, args->resv);
701 if (args->maxlen > args->pag->pagf_freeblks - reservation)
702 args->maxlen = args->pag->pagf_freeblks - reservation;
703 if (args->maxlen == 0) {
704 args->agbno = NULLAGBLOCK;
705 args->maxlen = oldmax;
706 return 0;
707 }
708
709 /*
710 * Branch to correct routine based on the type.
711 */
712 args->wasfromfl = 0;
713 switch (args->type) {
714 case XFS_ALLOCTYPE_THIS_AG:
715 error = xfs_alloc_ag_vextent_size(args);
716 break;
717 case XFS_ALLOCTYPE_NEAR_BNO:
718 error = xfs_alloc_ag_vextent_near(args);
719 break;
720 case XFS_ALLOCTYPE_THIS_BNO:
721 error = xfs_alloc_ag_vextent_exact(args);
722 break;
723 default:
724 ASSERT(0);
725 /* NOTREACHED */
726 }
727
728 args->maxlen = oldmax;
729
730 if (error || args->agbno == NULLAGBLOCK)
731 return error;
732
733 ASSERT(args->len >= args->minlen);
734 ASSERT(args->len <= args->maxlen);
735 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
736 ASSERT(args->agbno % args->alignment == 0);
737
738 /* if not file data, insert new block into the reverse map btree */
739 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
740 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
741 args->agbno, args->len, &args->oinfo);
742 if (error)
743 return error;
744 }
745
746 if (!args->wasfromfl) {
747 error = xfs_alloc_update_counters(args->tp, args->pag,
748 args->agbp,
749 -((long)(args->len)));
750 if (error)
751 return error;
752
753 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
754 args->agbno, args->len));
755 }
756
757 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
758
759 XFS_STATS_INC(args->mp, xs_allocx);
760 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
761 return error;
762 }
763
764 /*
765 * Allocate a variable extent at exactly agno/bno.
766 * Extent's length (returned in *len) will be between minlen and maxlen,
767 * and of the form k * prod + mod unless there's nothing that large.
768 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
769 */
770 STATIC int /* error */
771 xfs_alloc_ag_vextent_exact(
772 xfs_alloc_arg_t *args) /* allocation argument structure */
773 {
774 xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
775 xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
776 int error;
777 xfs_agblock_t fbno; /* start block of found extent */
778 xfs_extlen_t flen; /* length of found extent */
779 xfs_agblock_t tbno; /* start block of trimmed extent */
780 xfs_extlen_t tlen; /* length of trimmed extent */
781 xfs_agblock_t tend; /* end block of trimmed extent */
782 int i; /* success/failure of operation */
783
784 ASSERT(args->alignment == 1);
785
786 /*
787 * Allocate/initialize a cursor for the by-number freespace btree.
788 */
789 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
790 args->agno, XFS_BTNUM_BNO);
791
792 /*
793 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
794 * Look for the closest free block <= bno, it must contain bno
795 * if any free block does.
796 */
797 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
798 if (error)
799 goto error0;
800 if (!i)
801 goto not_found;
802
803 /*
804 * Grab the freespace record.
805 */
806 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
807 if (error)
808 goto error0;
809 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
810 ASSERT(fbno <= args->agbno);
811
812 /*
813 * Check for overlapping busy extents.
814 */
815 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
816
817 /*
818 * Give up if the start of the extent is busy, or the freespace isn't
819 * long enough for the minimum request.
820 */
821 if (tbno > args->agbno)
822 goto not_found;
823 if (tlen < args->minlen)
824 goto not_found;
825 tend = tbno + tlen;
826 if (tend < args->agbno + args->minlen)
827 goto not_found;
828
829 /*
830 * End of extent will be smaller of the freespace end and the
831 * maximal requested end.
832 *
833 * Fix the length according to mod and prod if given.
834 */
835 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
836 - args->agbno;
837 xfs_alloc_fix_len(args);
838 if (!xfs_alloc_fix_minleft(args))
839 goto not_found;
840
841 ASSERT(args->agbno + args->len <= tend);
842
843 /*
844 * We are allocating agbno for args->len
845 * Allocate/initialize a cursor for the by-size btree.
846 */
847 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
848 args->agno, XFS_BTNUM_CNT);
849 ASSERT(args->agbno + args->len <=
850 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
851 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
852 args->len, XFSA_FIXUP_BNO_OK);
853 if (error) {
854 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
855 goto error0;
856 }
857
858 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
859 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
860
861 args->wasfromfl = 0;
862 trace_xfs_alloc_exact_done(args);
863 return 0;
864
865 not_found:
866 /* Didn't find it, return null. */
867 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
868 args->agbno = NULLAGBLOCK;
869 trace_xfs_alloc_exact_notfound(args);
870 return 0;
871
872 error0:
873 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
874 trace_xfs_alloc_exact_error(args);
875 return error;
876 }
877
878 /*
879 * Search the btree in a given direction via the search cursor and compare
880 * the records found against the good extent we've already found.
881 */
882 STATIC int
883 xfs_alloc_find_best_extent(
884 struct xfs_alloc_arg *args, /* allocation argument structure */
885 struct xfs_btree_cur **gcur, /* good cursor */
886 struct xfs_btree_cur **scur, /* searching cursor */
887 xfs_agblock_t gdiff, /* difference for search comparison */
888 xfs_agblock_t *sbno, /* extent found by search */
889 xfs_extlen_t *slen, /* extent length */
890 xfs_agblock_t *sbnoa, /* aligned extent found by search */
891 xfs_extlen_t *slena, /* aligned extent length */
892 int dir) /* 0 = search right, 1 = search left */
893 {
894 xfs_agblock_t new;
895 xfs_agblock_t sdiff;
896 int error;
897 int i;
898
899 /* The good extent is perfect, no need to search. */
900 if (!gdiff)
901 goto out_use_good;
902
903 /*
904 * Look until we find a better one, run out of space or run off the end.
905 */
906 do {
907 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
908 if (error)
909 goto error0;
910 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
911 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
912
913 /*
914 * The good extent is closer than this one.
915 */
916 if (!dir) {
917 if (*sbnoa > args->max_agbno)
918 goto out_use_good;
919 if (*sbnoa >= args->agbno + gdiff)
920 goto out_use_good;
921 } else {
922 if (*sbnoa < args->min_agbno)
923 goto out_use_good;
924 if (*sbnoa <= args->agbno - gdiff)
925 goto out_use_good;
926 }
927
928 /*
929 * Same distance, compare length and pick the best.
930 */
931 if (*slena >= args->minlen) {
932 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
933 xfs_alloc_fix_len(args);
934
935 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
936 args->alignment,
937 args->datatype, *sbnoa,
938 *slena, &new);
939
940 /*
941 * Choose closer size and invalidate other cursor.
942 */
943 if (sdiff < gdiff)
944 goto out_use_search;
945 goto out_use_good;
946 }
947
948 if (!dir)
949 error = xfs_btree_increment(*scur, 0, &i);
950 else
951 error = xfs_btree_decrement(*scur, 0, &i);
952 if (error)
953 goto error0;
954 } while (i);
955
956 out_use_good:
957 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
958 *scur = NULL;
959 return 0;
960
961 out_use_search:
962 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
963 *gcur = NULL;
964 return 0;
965
966 error0:
967 /* caller invalidates cursors */
968 return error;
969 }
970
971 /*
972 * Allocate a variable extent near bno in the allocation group agno.
973 * Extent's length (returned in len) will be between minlen and maxlen,
974 * and of the form k * prod + mod unless there's nothing that large.
975 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
976 */
977 STATIC int /* error */
978 xfs_alloc_ag_vextent_near(
979 xfs_alloc_arg_t *args) /* allocation argument structure */
980 {
981 xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
982 xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
983 xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
984 xfs_agblock_t gtbno; /* start bno of right side entry */
985 xfs_agblock_t gtbnoa; /* aligned ... */
986 xfs_extlen_t gtdiff; /* difference to right side entry */
987 xfs_extlen_t gtlen; /* length of right side entry */
988 xfs_extlen_t gtlena; /* aligned ... */
989 xfs_agblock_t gtnew; /* useful start bno of right side */
990 int error; /* error code */
991 int i; /* result code, temporary */
992 int j; /* result code, temporary */
993 xfs_agblock_t ltbno; /* start bno of left side entry */
994 xfs_agblock_t ltbnoa; /* aligned ... */
995 xfs_extlen_t ltdiff; /* difference to left side entry */
996 xfs_extlen_t ltlen; /* length of left side entry */
997 xfs_extlen_t ltlena; /* aligned ... */
998 xfs_agblock_t ltnew; /* useful start bno of left side */
999 xfs_extlen_t rlen; /* length of returned extent */
1000 int forced = 0;
1001 #ifdef DEBUG
1002 /*
1003 * Randomly don't execute the first algorithm.
1004 */
1005 int dofirst; /* set to do first algorithm */
1006
1007 dofirst = prandom_u32() & 1;
1008 #endif
1009
1010 /* handle unitialized agbno range so caller doesn't have to */
1011 if (!args->min_agbno && !args->max_agbno)
1012 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1013 ASSERT(args->min_agbno <= args->max_agbno);
1014
1015 /* clamp agbno to the range if it's outside */
1016 if (args->agbno < args->min_agbno)
1017 args->agbno = args->min_agbno;
1018 if (args->agbno > args->max_agbno)
1019 args->agbno = args->max_agbno;
1020
1021 restart:
1022 bno_cur_lt = NULL;
1023 bno_cur_gt = NULL;
1024 ltlen = 0;
1025 gtlena = 0;
1026 ltlena = 0;
1027
1028 /*
1029 * Get a cursor for the by-size btree.
1030 */
1031 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1032 args->agno, XFS_BTNUM_CNT);
1033
1034 /*
1035 * See if there are any free extents as big as maxlen.
1036 */
1037 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1038 goto error0;
1039 /*
1040 * If none, then pick up the last entry in the tree unless the
1041 * tree is empty.
1042 */
1043 if (!i) {
1044 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
1045 &ltlen, &i)))
1046 goto error0;
1047 if (i == 0 || ltlen == 0) {
1048 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1049 trace_xfs_alloc_near_noentry(args);
1050 return 0;
1051 }
1052 ASSERT(i == 1);
1053 }
1054 args->wasfromfl = 0;
1055
1056 /*
1057 * First algorithm.
1058 * If the requested extent is large wrt the freespaces available
1059 * in this a.g., then the cursor will be pointing to a btree entry
1060 * near the right edge of the tree. If it's in the last btree leaf
1061 * block, then we just examine all the entries in that block
1062 * that are big enough, and pick the best one.
1063 * This is written as a while loop so we can break out of it,
1064 * but we never loop back to the top.
1065 */
1066 while (xfs_btree_islastblock(cnt_cur, 0)) {
1067 xfs_extlen_t bdiff;
1068 int besti=0;
1069 xfs_extlen_t blen=0;
1070 xfs_agblock_t bnew=0;
1071
1072 #ifdef DEBUG
1073 if (dofirst)
1074 break;
1075 #endif
1076 /*
1077 * Start from the entry that lookup found, sequence through
1078 * all larger free blocks. If we're actually pointing at a
1079 * record smaller than maxlen, go to the start of this block,
1080 * and skip all those smaller than minlen.
1081 */
1082 if (ltlen || args->alignment > 1) {
1083 cnt_cur->bc_ptrs[0] = 1;
1084 do {
1085 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
1086 &ltlen, &i)))
1087 goto error0;
1088 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1089 if (ltlen >= args->minlen)
1090 break;
1091 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
1092 goto error0;
1093 } while (i);
1094 ASSERT(ltlen >= args->minlen);
1095 if (!i)
1096 break;
1097 }
1098 i = cnt_cur->bc_ptrs[0];
1099 for (j = 1, blen = 0, bdiff = 0;
1100 !error && j && (blen < args->maxlen || bdiff > 0);
1101 error = xfs_btree_increment(cnt_cur, 0, &j)) {
1102 /*
1103 * For each entry, decide if it's better than
1104 * the previous best entry.
1105 */
1106 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1107 goto error0;
1108 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1109 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1110 &ltbnoa, &ltlena);
1111 if (ltlena < args->minlen)
1112 continue;
1113 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1114 continue;
1115 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1116 xfs_alloc_fix_len(args);
1117 ASSERT(args->len >= args->minlen);
1118 if (args->len < blen)
1119 continue;
1120 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1121 args->alignment, args->datatype, ltbnoa,
1122 ltlena, &ltnew);
1123 if (ltnew != NULLAGBLOCK &&
1124 (args->len > blen || ltdiff < bdiff)) {
1125 bdiff = ltdiff;
1126 bnew = ltnew;
1127 blen = args->len;
1128 besti = cnt_cur->bc_ptrs[0];
1129 }
1130 }
1131 /*
1132 * It didn't work. We COULD be in a case where
1133 * there's a good record somewhere, so try again.
1134 */
1135 if (blen == 0)
1136 break;
1137 /*
1138 * Point at the best entry, and retrieve it again.
1139 */
1140 cnt_cur->bc_ptrs[0] = besti;
1141 if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
1142 goto error0;
1143 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1144 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1145 args->len = blen;
1146 if (!xfs_alloc_fix_minleft(args)) {
1147 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1148 trace_xfs_alloc_near_nominleft(args);
1149 return 0;
1150 }
1151 blen = args->len;
1152 /*
1153 * We are allocating starting at bnew for blen blocks.
1154 */
1155 args->agbno = bnew;
1156 ASSERT(bnew >= ltbno);
1157 ASSERT(bnew + blen <= ltbno + ltlen);
1158 /*
1159 * Set up a cursor for the by-bno tree.
1160 */
1161 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1162 args->agbp, args->agno, XFS_BTNUM_BNO);
1163 /*
1164 * Fix up the btree entries.
1165 */
1166 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1167 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1168 goto error0;
1169 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1170 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1171
1172 trace_xfs_alloc_near_first(args);
1173 return 0;
1174 }
1175 /*
1176 * Second algorithm.
1177 * Search in the by-bno tree to the left and to the right
1178 * simultaneously, until in each case we find a space big enough,
1179 * or run into the edge of the tree. When we run into the edge,
1180 * we deallocate that cursor.
1181 * If both searches succeed, we compare the two spaces and pick
1182 * the better one.
1183 * With alignment, it's possible for both to fail; the upper
1184 * level algorithm that picks allocation groups for allocations
1185 * is not supposed to do this.
1186 */
1187 /*
1188 * Allocate and initialize the cursor for the leftward search.
1189 */
1190 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1191 args->agno, XFS_BTNUM_BNO);
1192 /*
1193 * Lookup <= bno to find the leftward search's starting point.
1194 */
1195 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1196 goto error0;
1197 if (!i) {
1198 /*
1199 * Didn't find anything; use this cursor for the rightward
1200 * search.
1201 */
1202 bno_cur_gt = bno_cur_lt;
1203 bno_cur_lt = NULL;
1204 }
1205 /*
1206 * Found something. Duplicate the cursor for the rightward search.
1207 */
1208 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1209 goto error0;
1210 /*
1211 * Increment the cursor, so we will point at the entry just right
1212 * of the leftward entry if any, or to the leftmost entry.
1213 */
1214 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1215 goto error0;
1216 if (!i) {
1217 /*
1218 * It failed, there are no rightward entries.
1219 */
1220 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1221 bno_cur_gt = NULL;
1222 }
1223 /*
1224 * Loop going left with the leftward cursor, right with the
1225 * rightward cursor, until either both directions give up or
1226 * we find an entry at least as big as minlen.
1227 */
1228 do {
1229 if (bno_cur_lt) {
1230 if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
1231 goto error0;
1232 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1233 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1234 &ltbnoa, &ltlena);
1235 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
1236 break;
1237 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1238 goto error0;
1239 if (!i || ltbnoa < args->min_agbno) {
1240 xfs_btree_del_cursor(bno_cur_lt,
1241 XFS_BTREE_NOERROR);
1242 bno_cur_lt = NULL;
1243 }
1244 }
1245 if (bno_cur_gt) {
1246 if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
1247 goto error0;
1248 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1249 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1250 &gtbnoa, &gtlena);
1251 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
1252 break;
1253 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1254 goto error0;
1255 if (!i || gtbnoa > args->max_agbno) {
1256 xfs_btree_del_cursor(bno_cur_gt,
1257 XFS_BTREE_NOERROR);
1258 bno_cur_gt = NULL;
1259 }
1260 }
1261 } while (bno_cur_lt || bno_cur_gt);
1262
1263 /*
1264 * Got both cursors still active, need to find better entry.
1265 */
1266 if (bno_cur_lt && bno_cur_gt) {
1267 if (ltlena >= args->minlen) {
1268 /*
1269 * Left side is good, look for a right side entry.
1270 */
1271 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1272 xfs_alloc_fix_len(args);
1273 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1274 args->alignment, args->datatype, ltbnoa,
1275 ltlena, &ltnew);
1276
1277 error = xfs_alloc_find_best_extent(args,
1278 &bno_cur_lt, &bno_cur_gt,
1279 ltdiff, &gtbno, &gtlen,
1280 &gtbnoa, &gtlena,
1281 0 /* search right */);
1282 } else {
1283 ASSERT(gtlena >= args->minlen);
1284
1285 /*
1286 * Right side is good, look for a left side entry.
1287 */
1288 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1289 xfs_alloc_fix_len(args);
1290 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1291 args->alignment, args->datatype, gtbnoa,
1292 gtlena, &gtnew);
1293
1294 error = xfs_alloc_find_best_extent(args,
1295 &bno_cur_gt, &bno_cur_lt,
1296 gtdiff, &ltbno, &ltlen,
1297 &ltbnoa, &ltlena,
1298 1 /* search left */);
1299 }
1300
1301 if (error)
1302 goto error0;
1303 }
1304
1305 /*
1306 * If we couldn't get anything, give up.
1307 */
1308 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1309 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1310
1311 if (!forced++) {
1312 trace_xfs_alloc_near_busy(args);
1313 xfs_log_force(args->mp, XFS_LOG_SYNC);
1314 goto restart;
1315 }
1316 trace_xfs_alloc_size_neither(args);
1317 args->agbno = NULLAGBLOCK;
1318 return 0;
1319 }
1320
1321 /*
1322 * At this point we have selected a freespace entry, either to the
1323 * left or to the right. If it's on the right, copy all the
1324 * useful variables to the "left" set so we only have one
1325 * copy of this code.
1326 */
1327 if (bno_cur_gt) {
1328 bno_cur_lt = bno_cur_gt;
1329 bno_cur_gt = NULL;
1330 ltbno = gtbno;
1331 ltbnoa = gtbnoa;
1332 ltlen = gtlen;
1333 ltlena = gtlena;
1334 j = 1;
1335 } else
1336 j = 0;
1337
1338 /*
1339 * Fix up the length and compute the useful address.
1340 */
1341 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1342 xfs_alloc_fix_len(args);
1343 if (!xfs_alloc_fix_minleft(args)) {
1344 trace_xfs_alloc_near_nominleft(args);
1345 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1346 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1347 return 0;
1348 }
1349 rlen = args->len;
1350 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1351 args->datatype, ltbnoa, ltlena, &ltnew);
1352 ASSERT(ltnew >= ltbno);
1353 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1354 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1355 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
1356 args->agbno = ltnew;
1357
1358 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1359 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1360 goto error0;
1361
1362 if (j)
1363 trace_xfs_alloc_near_greater(args);
1364 else
1365 trace_xfs_alloc_near_lesser(args);
1366
1367 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1368 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1369 return 0;
1370
1371 error0:
1372 trace_xfs_alloc_near_error(args);
1373 if (cnt_cur != NULL)
1374 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1375 if (bno_cur_lt != NULL)
1376 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1377 if (bno_cur_gt != NULL)
1378 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1379 return error;
1380 }
1381
1382 /*
1383 * Allocate a variable extent anywhere in the allocation group agno.
1384 * Extent's length (returned in len) will be between minlen and maxlen,
1385 * and of the form k * prod + mod unless there's nothing that large.
1386 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1387 */
1388 STATIC int /* error */
1389 xfs_alloc_ag_vextent_size(
1390 xfs_alloc_arg_t *args) /* allocation argument structure */
1391 {
1392 xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
1393 xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
1394 int error; /* error result */
1395 xfs_agblock_t fbno; /* start of found freespace */
1396 xfs_extlen_t flen; /* length of found freespace */
1397 int i; /* temp status variable */
1398 xfs_agblock_t rbno; /* returned block number */
1399 xfs_extlen_t rlen; /* length of returned extent */
1400 int forced = 0;
1401
1402 restart:
1403 /*
1404 * Allocate and initialize a cursor for the by-size btree.
1405 */
1406 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1407 args->agno, XFS_BTNUM_CNT);
1408 bno_cur = NULL;
1409
1410 /*
1411 * Look for an entry >= maxlen+alignment-1 blocks.
1412 */
1413 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1414 args->maxlen + args->alignment - 1, &i)))
1415 goto error0;
1416
1417 /*
1418 * If none or we have busy extents that we cannot allocate from, then
1419 * we have to settle for a smaller extent. In the case that there are
1420 * no large extents, this will return the last entry in the tree unless
1421 * the tree is empty. In the case that there are only busy large
1422 * extents, this will return the largest small extent unless there
1423 * are no smaller extents available.
1424 */
1425 if (!i || forced > 1) {
1426 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1427 &fbno, &flen, &i);
1428 if (error)
1429 goto error0;
1430 if (i == 0 || flen == 0) {
1431 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1432 trace_xfs_alloc_size_noentry(args);
1433 return 0;
1434 }
1435 ASSERT(i == 1);
1436 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1437 } else {
1438 /*
1439 * Search for a non-busy extent that is large enough.
1440 * If we are at low space, don't check, or if we fall of
1441 * the end of the btree, turn off the busy check and
1442 * restart.
1443 */
1444 for (;;) {
1445 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1446 if (error)
1447 goto error0;
1448 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1449
1450 xfs_alloc_compute_aligned(args, fbno, flen,
1451 &rbno, &rlen);
1452
1453 if (rlen >= args->maxlen)
1454 break;
1455
1456 error = xfs_btree_increment(cnt_cur, 0, &i);
1457 if (error)
1458 goto error0;
1459 if (i == 0) {
1460 /*
1461 * Our only valid extents must have been busy.
1462 * Make it unbusy by forcing the log out and
1463 * retrying. If we've been here before, forcing
1464 * the log isn't making the extents available,
1465 * which means they have probably been freed in
1466 * this transaction. In that case, we have to
1467 * give up on them and we'll attempt a minlen
1468 * allocation the next time around.
1469 */
1470 xfs_btree_del_cursor(cnt_cur,
1471 XFS_BTREE_NOERROR);
1472 trace_xfs_alloc_size_busy(args);
1473 if (!forced++)
1474 xfs_log_force(args->mp, XFS_LOG_SYNC);
1475 goto restart;
1476 }
1477 }
1478 }
1479
1480 /*
1481 * In the first case above, we got the last entry in the
1482 * by-size btree. Now we check to see if the space hits maxlen
1483 * once aligned; if not, we search left for something better.
1484 * This can't happen in the second case above.
1485 */
1486 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1487 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1488 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1489 if (rlen < args->maxlen) {
1490 xfs_agblock_t bestfbno;
1491 xfs_extlen_t bestflen;
1492 xfs_agblock_t bestrbno;
1493 xfs_extlen_t bestrlen;
1494
1495 bestrlen = rlen;
1496 bestrbno = rbno;
1497 bestflen = flen;
1498 bestfbno = fbno;
1499 for (;;) {
1500 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1501 goto error0;
1502 if (i == 0)
1503 break;
1504 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1505 &i)))
1506 goto error0;
1507 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1508 if (flen < bestrlen)
1509 break;
1510 xfs_alloc_compute_aligned(args, fbno, flen,
1511 &rbno, &rlen);
1512 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1513 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1514 (rlen <= flen && rbno + rlen <= fbno + flen),
1515 error0);
1516 if (rlen > bestrlen) {
1517 bestrlen = rlen;
1518 bestrbno = rbno;
1519 bestflen = flen;
1520 bestfbno = fbno;
1521 if (rlen == args->maxlen)
1522 break;
1523 }
1524 }
1525 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1526 &i)))
1527 goto error0;
1528 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1529 rlen = bestrlen;
1530 rbno = bestrbno;
1531 flen = bestflen;
1532 fbno = bestfbno;
1533 }
1534 args->wasfromfl = 0;
1535 /*
1536 * Fix up the length.
1537 */
1538 args->len = rlen;
1539 if (rlen < args->minlen) {
1540 if (!forced++) {
1541 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1542 trace_xfs_alloc_size_busy(args);
1543 xfs_log_force(args->mp, XFS_LOG_SYNC);
1544 goto restart;
1545 }
1546 goto out_nominleft;
1547 }
1548 xfs_alloc_fix_len(args);
1549
1550 if (!xfs_alloc_fix_minleft(args))
1551 goto out_nominleft;
1552 rlen = args->len;
1553 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
1554 /*
1555 * Allocate and initialize a cursor for the by-block tree.
1556 */
1557 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1558 args->agno, XFS_BTNUM_BNO);
1559 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1560 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1561 goto error0;
1562 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1563 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1564 cnt_cur = bno_cur = NULL;
1565 args->len = rlen;
1566 args->agbno = rbno;
1567 XFS_WANT_CORRUPTED_GOTO(args->mp,
1568 args->agbno + args->len <=
1569 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1570 error0);
1571 trace_xfs_alloc_size_done(args);
1572 return 0;
1573
1574 error0:
1575 trace_xfs_alloc_size_error(args);
1576 if (cnt_cur)
1577 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1578 if (bno_cur)
1579 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1580 return error;
1581
1582 out_nominleft:
1583 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1584 trace_xfs_alloc_size_nominleft(args);
1585 args->agbno = NULLAGBLOCK;
1586 return 0;
1587 }
1588
1589 /*
1590 * Deal with the case where only small freespaces remain.
1591 * Either return the contents of the last freespace record,
1592 * or allocate space from the freelist if there is nothing in the tree.
1593 */
1594 STATIC int /* error */
1595 xfs_alloc_ag_vextent_small(
1596 xfs_alloc_arg_t *args, /* allocation argument structure */
1597 xfs_btree_cur_t *ccur, /* by-size cursor */
1598 xfs_agblock_t *fbnop, /* result block number */
1599 xfs_extlen_t *flenp, /* result length */
1600 int *stat) /* status: 0-freelist, 1-normal/none */
1601 {
1602 struct xfs_owner_info oinfo;
1603 struct xfs_perag *pag;
1604 int error;
1605 xfs_agblock_t fbno;
1606 xfs_extlen_t flen;
1607 int i;
1608
1609 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1610 goto error0;
1611 if (i) {
1612 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1613 goto error0;
1614 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1615 }
1616 /*
1617 * Nothing in the btree, try the freelist. Make sure
1618 * to respect minleft even when pulling from the
1619 * freelist.
1620 */
1621 else if (args->minlen == 1 && args->alignment == 1 &&
1622 args->resv != XFS_AG_RESV_AGFL &&
1623 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1624 > args->minleft)) {
1625 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1626 if (error)
1627 goto error0;
1628 if (fbno != NULLAGBLOCK) {
1629 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1630 xfs_alloc_allow_busy_reuse(args->datatype));
1631
1632 if (xfs_alloc_is_userdata(args->datatype)) {
1633 xfs_buf_t *bp;
1634
1635 bp = xfs_btree_get_bufs(args->mp, args->tp,
1636 args->agno, fbno, 0);
1637 xfs_trans_binval(args->tp, bp);
1638 }
1639 args->len = 1;
1640 args->agbno = fbno;
1641 XFS_WANT_CORRUPTED_GOTO(args->mp,
1642 args->agbno + args->len <=
1643 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1644 error0);
1645 args->wasfromfl = 1;
1646 trace_xfs_alloc_small_freelist(args);
1647
1648 /*
1649 * If we're feeding an AGFL block to something that
1650 * doesn't live in the free space, we need to clear
1651 * out the OWN_AG rmap and add the block back to
1652 * the AGFL per-AG reservation.
1653 */
1654 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1655 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1656 fbno, 1, &oinfo);
1657 if (error)
1658 goto error0;
1659 pag = xfs_perag_get(args->mp, args->agno);
1660 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1661 args->tp, 1);
1662 xfs_perag_put(pag);
1663
1664 *stat = 0;
1665 return 0;
1666 }
1667 /*
1668 * Nothing in the freelist.
1669 */
1670 else
1671 flen = 0;
1672 }
1673 /*
1674 * Can't allocate from the freelist for some reason.
1675 */
1676 else {
1677 fbno = NULLAGBLOCK;
1678 flen = 0;
1679 }
1680 /*
1681 * Can't do the allocation, give up.
1682 */
1683 if (flen < args->minlen) {
1684 args->agbno = NULLAGBLOCK;
1685 trace_xfs_alloc_small_notenough(args);
1686 flen = 0;
1687 }
1688 *fbnop = fbno;
1689 *flenp = flen;
1690 *stat = 1;
1691 trace_xfs_alloc_small_done(args);
1692 return 0;
1693
1694 error0:
1695 trace_xfs_alloc_small_error(args);
1696 return error;
1697 }
1698
1699 /*
1700 * Free the extent starting at agno/bno for length.
1701 */
1702 STATIC int
1703 xfs_free_ag_extent(
1704 xfs_trans_t *tp,
1705 xfs_buf_t *agbp,
1706 xfs_agnumber_t agno,
1707 xfs_agblock_t bno,
1708 xfs_extlen_t len,
1709 struct xfs_owner_info *oinfo,
1710 enum xfs_ag_resv_type type)
1711 {
1712 xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
1713 xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
1714 int error; /* error return value */
1715 xfs_agblock_t gtbno; /* start of right neighbor block */
1716 xfs_extlen_t gtlen; /* length of right neighbor block */
1717 int haveleft; /* have a left neighbor block */
1718 int haveright; /* have a right neighbor block */
1719 int i; /* temp, result code */
1720 xfs_agblock_t ltbno; /* start of left neighbor block */
1721 xfs_extlen_t ltlen; /* length of left neighbor block */
1722 xfs_mount_t *mp; /* mount point struct for filesystem */
1723 xfs_agblock_t nbno; /* new starting block of freespace */
1724 xfs_extlen_t nlen; /* new length of freespace */
1725 xfs_perag_t *pag; /* per allocation group data */
1726
1727 bno_cur = cnt_cur = NULL;
1728 mp = tp->t_mountp;
1729
1730 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1731 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1732 if (error)
1733 goto error0;
1734 }
1735
1736 /*
1737 * Allocate and initialize a cursor for the by-block btree.
1738 */
1739 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1740 /*
1741 * Look for a neighboring block on the left (lower block numbers)
1742 * that is contiguous with this space.
1743 */
1744 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1745 goto error0;
1746 if (haveleft) {
1747 /*
1748 * There is a block to our left.
1749 */
1750 if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
1751 goto error0;
1752 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1753 /*
1754 * It's not contiguous, though.
1755 */
1756 if (ltbno + ltlen < bno)
1757 haveleft = 0;
1758 else {
1759 /*
1760 * If this failure happens the request to free this
1761 * space was invalid, it's (partly) already free.
1762 * Very bad.
1763 */
1764 XFS_WANT_CORRUPTED_GOTO(mp,
1765 ltbno + ltlen <= bno, error0);
1766 }
1767 }
1768 /*
1769 * Look for a neighboring block on the right (higher block numbers)
1770 * that is contiguous with this space.
1771 */
1772 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1773 goto error0;
1774 if (haveright) {
1775 /*
1776 * There is a block to our right.
1777 */
1778 if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
1779 goto error0;
1780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1781 /*
1782 * It's not contiguous, though.
1783 */
1784 if (bno + len < gtbno)
1785 haveright = 0;
1786 else {
1787 /*
1788 * If this failure happens the request to free this
1789 * space was invalid, it's (partly) already free.
1790 * Very bad.
1791 */
1792 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
1793 }
1794 }
1795 /*
1796 * Now allocate and initialize a cursor for the by-size tree.
1797 */
1798 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1799 /*
1800 * Have both left and right contiguous neighbors.
1801 * Merge all three into a single free block.
1802 */
1803 if (haveleft && haveright) {
1804 /*
1805 * Delete the old by-size entry on the left.
1806 */
1807 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1808 goto error0;
1809 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1810 if ((error = xfs_btree_delete(cnt_cur, &i)))
1811 goto error0;
1812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1813 /*
1814 * Delete the old by-size entry on the right.
1815 */
1816 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1817 goto error0;
1818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1819 if ((error = xfs_btree_delete(cnt_cur, &i)))
1820 goto error0;
1821 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1822 /*
1823 * Delete the old by-block entry for the right block.
1824 */
1825 if ((error = xfs_btree_delete(bno_cur, &i)))
1826 goto error0;
1827 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1828 /*
1829 * Move the by-block cursor back to the left neighbor.
1830 */
1831 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1832 goto error0;
1833 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1834 #ifdef DEBUG
1835 /*
1836 * Check that this is the right record: delete didn't
1837 * mangle the cursor.
1838 */
1839 {
1840 xfs_agblock_t xxbno;
1841 xfs_extlen_t xxlen;
1842
1843 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1844 &i)))
1845 goto error0;
1846 XFS_WANT_CORRUPTED_GOTO(mp,
1847 i == 1 && xxbno == ltbno && xxlen == ltlen,
1848 error0);
1849 }
1850 #endif
1851 /*
1852 * Update remaining by-block entry to the new, joined block.
1853 */
1854 nbno = ltbno;
1855 nlen = len + ltlen + gtlen;
1856 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1857 goto error0;
1858 }
1859 /*
1860 * Have only a left contiguous neighbor.
1861 * Merge it together with the new freespace.
1862 */
1863 else if (haveleft) {
1864 /*
1865 * Delete the old by-size entry on the left.
1866 */
1867 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1868 goto error0;
1869 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1870 if ((error = xfs_btree_delete(cnt_cur, &i)))
1871 goto error0;
1872 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1873 /*
1874 * Back up the by-block cursor to the left neighbor, and
1875 * update its length.
1876 */
1877 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1878 goto error0;
1879 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1880 nbno = ltbno;
1881 nlen = len + ltlen;
1882 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1883 goto error0;
1884 }
1885 /*
1886 * Have only a right contiguous neighbor.
1887 * Merge it together with the new freespace.
1888 */
1889 else if (haveright) {
1890 /*
1891 * Delete the old by-size entry on the right.
1892 */
1893 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1894 goto error0;
1895 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1896 if ((error = xfs_btree_delete(cnt_cur, &i)))
1897 goto error0;
1898 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1899 /*
1900 * Update the starting block and length of the right
1901 * neighbor in the by-block tree.
1902 */
1903 nbno = bno;
1904 nlen = len + gtlen;
1905 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1906 goto error0;
1907 }
1908 /*
1909 * No contiguous neighbors.
1910 * Insert the new freespace into the by-block tree.
1911 */
1912 else {
1913 nbno = bno;
1914 nlen = len;
1915 if ((error = xfs_btree_insert(bno_cur, &i)))
1916 goto error0;
1917 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1918 }
1919 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1920 bno_cur = NULL;
1921 /*
1922 * In all cases we need to insert the new freespace in the by-size tree.
1923 */
1924 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1925 goto error0;
1926 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
1927 if ((error = xfs_btree_insert(cnt_cur, &i)))
1928 goto error0;
1929 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1930 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1931 cnt_cur = NULL;
1932
1933 /*
1934 * Update the freespace totals in the ag and superblock.
1935 */
1936 pag = xfs_perag_get(mp, agno);
1937 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1938 xfs_ag_resv_free_extent(pag, type, tp, len);
1939 xfs_perag_put(pag);
1940 if (error)
1941 goto error0;
1942
1943 XFS_STATS_INC(mp, xs_freex);
1944 XFS_STATS_ADD(mp, xs_freeb, len);
1945
1946 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1947 haveleft, haveright);
1948
1949 return 0;
1950
1951 error0:
1952 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1953 -1, -1);
1954 if (bno_cur)
1955 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1956 if (cnt_cur)
1957 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1958 return error;
1959 }
1960
1961 /*
1962 * Visible (exported) allocation/free functions.
1963 * Some of these are used just by xfs_alloc_btree.c and this file.
1964 */
1965
1966 /*
1967 * Compute and fill in value of m_ag_maxlevels.
1968 */
1969 void
1970 xfs_alloc_compute_maxlevels(
1971 xfs_mount_t *mp) /* file system mount structure */
1972 {
1973 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1974 (mp->m_sb.sb_agblocks + 1) / 2);
1975 }
1976
1977 /*
1978 * Find the length of the longest extent in an AG. The 'need' parameter
1979 * specifies how much space we're going to need for the AGFL and the
1980 * 'reserved' parameter tells us how many blocks in this AG are reserved for
1981 * other callers.
1982 */
1983 xfs_extlen_t
1984 xfs_alloc_longest_free_extent(
1985 struct xfs_mount *mp,
1986 struct xfs_perag *pag,
1987 xfs_extlen_t need,
1988 xfs_extlen_t reserved)
1989 {
1990 xfs_extlen_t delta = 0;
1991
1992 /*
1993 * If the AGFL needs a recharge, we'll have to subtract that from the
1994 * longest extent.
1995 */
1996 if (need > pag->pagf_flcount)
1997 delta = need - pag->pagf_flcount;
1998
1999 /*
2000 * If we cannot maintain others' reservations with space from the
2001 * not-longest freesp extents, we'll have to subtract /that/ from
2002 * the longest extent too.
2003 */
2004 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2005 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2006
2007 /*
2008 * If the longest extent is long enough to satisfy all the
2009 * reservations and AGFL rules in place, we can return this extent.
2010 */
2011 if (pag->pagf_longest > delta)
2012 return pag->pagf_longest - delta;
2013
2014 /* Otherwise, let the caller try for 1 block if there's space. */
2015 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2016 }
2017
2018 unsigned int
2019 xfs_alloc_min_freelist(
2020 struct xfs_mount *mp,
2021 struct xfs_perag *pag)
2022 {
2023 unsigned int min_free;
2024
2025 /* space needed by-bno freespace btree */
2026 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2027 mp->m_ag_maxlevels);
2028 /* space needed by-size freespace btree */
2029 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2030 mp->m_ag_maxlevels);
2031 /* space needed reverse mapping used space btree */
2032 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2033 min_free += min_t(unsigned int,
2034 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2035 mp->m_rmap_maxlevels);
2036
2037 return min_free;
2038 }
2039
2040 /*
2041 * Check if the operation we are fixing up the freelist for should go ahead or
2042 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2043 * is dependent on whether the size and shape of free space available will
2044 * permit the requested allocation to take place.
2045 */
2046 static bool
2047 xfs_alloc_space_available(
2048 struct xfs_alloc_arg *args,
2049 xfs_extlen_t min_free,
2050 int flags)
2051 {
2052 struct xfs_perag *pag = args->pag;
2053 xfs_extlen_t longest;
2054 xfs_extlen_t reservation; /* blocks that are still reserved */
2055 int available;
2056
2057 if (flags & XFS_ALLOC_FLAG_FREEING)
2058 return true;
2059
2060 reservation = xfs_ag_resv_needed(pag, args->resv);
2061
2062 /* do we have enough contiguous free space for the allocation? */
2063 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2064 reservation);
2065 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2066 return false;
2067
2068 /* do we have enough free space remaining for the allocation? */
2069 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
2070 reservation - min_free - args->total);
2071 if (available < (int)args->minleft || available <= 0)
2072 return false;
2073
2074 return true;
2075 }
2076
2077 /*
2078 * Decide whether to use this allocation group for this allocation.
2079 * If so, fix up the btree freelist's size.
2080 */
2081 int /* error */
2082 xfs_alloc_fix_freelist(
2083 struct xfs_alloc_arg *args, /* allocation argument structure */
2084 int flags) /* XFS_ALLOC_FLAG_... */
2085 {
2086 struct xfs_mount *mp = args->mp;
2087 struct xfs_perag *pag = args->pag;
2088 struct xfs_trans *tp = args->tp;
2089 struct xfs_buf *agbp = NULL;
2090 struct xfs_buf *agflbp = NULL;
2091 struct xfs_alloc_arg targs; /* local allocation arguments */
2092 xfs_agblock_t bno; /* freelist block */
2093 xfs_extlen_t need; /* total blocks needed in freelist */
2094 int error = 0;
2095
2096 if (!pag->pagf_init) {
2097 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2098 if (error)
2099 goto out_no_agbp;
2100 if (!pag->pagf_init) {
2101 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2102 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2103 goto out_agbp_relse;
2104 }
2105 }
2106
2107 /*
2108 * If this is a metadata preferred pag and we are user data then try
2109 * somewhere else if we are not being asked to try harder at this
2110 * point
2111 */
2112 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
2113 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2114 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2115 goto out_agbp_relse;
2116 }
2117
2118 need = xfs_alloc_min_freelist(mp, pag);
2119 if (!xfs_alloc_space_available(args, need, flags))
2120 goto out_agbp_relse;
2121
2122 /*
2123 * Get the a.g. freespace buffer.
2124 * Can fail if we're not blocking on locks, and it's held.
2125 */
2126 if (!agbp) {
2127 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2128 if (error)
2129 goto out_no_agbp;
2130 if (!agbp) {
2131 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2132 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2133 goto out_no_agbp;
2134 }
2135 }
2136
2137 /* If there isn't enough total space or single-extent, reject it. */
2138 need = xfs_alloc_min_freelist(mp, pag);
2139 if (!xfs_alloc_space_available(args, need, flags))
2140 goto out_agbp_relse;
2141
2142 /*
2143 * Make the freelist shorter if it's too long.
2144 *
2145 * Note that from this point onwards, we will always release the agf and
2146 * agfl buffers on error. This handles the case where we error out and
2147 * the buffers are clean or may not have been joined to the transaction
2148 * and hence need to be released manually. If they have been joined to
2149 * the transaction, then xfs_trans_brelse() will handle them
2150 * appropriately based on the recursion count and dirty state of the
2151 * buffer.
2152 *
2153 * XXX (dgc): When we have lots of free space, does this buy us
2154 * anything other than extra overhead when we need to put more blocks
2155 * back on the free list? Maybe we should only do this when space is
2156 * getting low or the AGFL is more than half full?
2157 *
2158 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2159 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2160 * updating the rmapbt. Both flags are used in xfs_repair while we're
2161 * rebuilding the rmapbt, and neither are used by the kernel. They're
2162 * both required to ensure that rmaps are correctly recorded for the
2163 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2164 * repair/rmap.c in xfsprogs for details.
2165 */
2166 memset(&targs, 0, sizeof(targs));
2167 if (flags & XFS_ALLOC_FLAG_NORMAP)
2168 xfs_rmap_skip_owner_update(&targs.oinfo);
2169 else
2170 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2171 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
2172 struct xfs_buf *bp;
2173
2174 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2175 if (error)
2176 goto out_agbp_relse;
2177 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
2178 &targs.oinfo, XFS_AG_RESV_AGFL);
2179 if (error)
2180 goto out_agbp_relse;
2181 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2182 xfs_trans_binval(tp, bp);
2183 }
2184
2185 targs.tp = tp;
2186 targs.mp = mp;
2187 targs.agbp = agbp;
2188 targs.agno = args->agno;
2189 targs.alignment = targs.minlen = targs.prod = 1;
2190 targs.type = XFS_ALLOCTYPE_THIS_AG;
2191 targs.pag = pag;
2192 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2193 if (error)
2194 goto out_agbp_relse;
2195
2196 /* Make the freelist longer if it's too short. */
2197 while (pag->pagf_flcount < need) {
2198 targs.agbno = 0;
2199 targs.maxlen = need - pag->pagf_flcount;
2200 targs.resv = XFS_AG_RESV_AGFL;
2201
2202 /* Allocate as many blocks as possible at once. */
2203 error = xfs_alloc_ag_vextent(&targs);
2204 if (error)
2205 goto out_agflbp_relse;
2206
2207 /*
2208 * Stop if we run out. Won't happen if callers are obeying
2209 * the restrictions correctly. Can happen for free calls
2210 * on a completely full ag.
2211 */
2212 if (targs.agbno == NULLAGBLOCK) {
2213 if (flags & XFS_ALLOC_FLAG_FREEING)
2214 break;
2215 goto out_agflbp_relse;
2216 }
2217 /*
2218 * Put each allocated block on the list.
2219 */
2220 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2221 error = xfs_alloc_put_freelist(tp, agbp,
2222 agflbp, bno, 0);
2223 if (error)
2224 goto out_agflbp_relse;
2225 }
2226 }
2227 xfs_trans_brelse(tp, agflbp);
2228 args->agbp = agbp;
2229 return 0;
2230
2231 out_agflbp_relse:
2232 xfs_trans_brelse(tp, agflbp);
2233 out_agbp_relse:
2234 if (agbp)
2235 xfs_trans_brelse(tp, agbp);
2236 out_no_agbp:
2237 args->agbp = NULL;
2238 return error;
2239 }
2240
2241 /*
2242 * Get a block from the freelist.
2243 * Returns with the buffer for the block gotten.
2244 */
2245 int /* error */
2246 xfs_alloc_get_freelist(
2247 xfs_trans_t *tp, /* transaction pointer */
2248 xfs_buf_t *agbp, /* buffer containing the agf structure */
2249 xfs_agblock_t *bnop, /* block address retrieved from freelist */
2250 int btreeblk) /* destination is a AGF btree */
2251 {
2252 xfs_agf_t *agf; /* a.g. freespace structure */
2253 xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
2254 xfs_agblock_t bno; /* block number returned */
2255 __be32 *agfl_bno;
2256 int error;
2257 int logflags;
2258 xfs_mount_t *mp = tp->t_mountp;
2259 xfs_perag_t *pag; /* per allocation group data */
2260
2261 /*
2262 * Freelist is empty, give up.
2263 */
2264 agf = XFS_BUF_TO_AGF(agbp);
2265 if (!agf->agf_flcount) {
2266 *bnop = NULLAGBLOCK;
2267 return 0;
2268 }
2269 /*
2270 * Read the array of free blocks.
2271 */
2272 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2273 &agflbp);
2274 if (error)
2275 return error;
2276
2277
2278 /*
2279 * Get the block number and update the data structures.
2280 */
2281 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2282 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2283 be32_add_cpu(&agf->agf_flfirst, 1);
2284 xfs_trans_brelse(tp, agflbp);
2285 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2286 agf->agf_flfirst = 0;
2287
2288 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2289 be32_add_cpu(&agf->agf_flcount, -1);
2290 xfs_trans_agflist_delta(tp, -1);
2291 pag->pagf_flcount--;
2292 xfs_perag_put(pag);
2293
2294 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2295 if (btreeblk) {
2296 be32_add_cpu(&agf->agf_btreeblks, 1);
2297 pag->pagf_btreeblks++;
2298 logflags |= XFS_AGF_BTREEBLKS;
2299 }
2300
2301 xfs_alloc_log_agf(tp, agbp, logflags);
2302 *bnop = bno;
2303
2304 return 0;
2305 }
2306
2307 /*
2308 * Log the given fields from the agf structure.
2309 */
2310 void
2311 xfs_alloc_log_agf(
2312 xfs_trans_t *tp, /* transaction pointer */
2313 xfs_buf_t *bp, /* buffer for a.g. freelist header */
2314 int fields) /* mask of fields to be logged (XFS_AGF_...) */
2315 {
2316 int first; /* first byte offset */
2317 int last; /* last byte offset */
2318 static const short offsets[] = {
2319 offsetof(xfs_agf_t, agf_magicnum),
2320 offsetof(xfs_agf_t, agf_versionnum),
2321 offsetof(xfs_agf_t, agf_seqno),
2322 offsetof(xfs_agf_t, agf_length),
2323 offsetof(xfs_agf_t, agf_roots[0]),
2324 offsetof(xfs_agf_t, agf_levels[0]),
2325 offsetof(xfs_agf_t, agf_flfirst),
2326 offsetof(xfs_agf_t, agf_fllast),
2327 offsetof(xfs_agf_t, agf_flcount),
2328 offsetof(xfs_agf_t, agf_freeblks),
2329 offsetof(xfs_agf_t, agf_longest),
2330 offsetof(xfs_agf_t, agf_btreeblks),
2331 offsetof(xfs_agf_t, agf_uuid),
2332 offsetof(xfs_agf_t, agf_rmap_blocks),
2333 /* needed so that we don't log the whole rest of the structure: */
2334 offsetof(xfs_agf_t, agf_spare64),
2335 sizeof(xfs_agf_t)
2336 };
2337
2338 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2339
2340 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2341
2342 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2343 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2344 }
2345
2346 /*
2347 * Interface for inode allocation to force the pag data to be initialized.
2348 */
2349 int /* error */
2350 xfs_alloc_pagf_init(
2351 xfs_mount_t *mp, /* file system mount structure */
2352 xfs_trans_t *tp, /* transaction pointer */
2353 xfs_agnumber_t agno, /* allocation group number */
2354 int flags) /* XFS_ALLOC_FLAGS_... */
2355 {
2356 xfs_buf_t *bp;
2357 int error;
2358
2359 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2360 return error;
2361 if (bp)
2362 xfs_trans_brelse(tp, bp);
2363 return 0;
2364 }
2365
2366 /*
2367 * Put the block on the freelist for the allocation group.
2368 */
2369 int /* error */
2370 xfs_alloc_put_freelist(
2371 xfs_trans_t *tp, /* transaction pointer */
2372 xfs_buf_t *agbp, /* buffer for a.g. freelist header */
2373 xfs_buf_t *agflbp,/* buffer for a.g. free block array */
2374 xfs_agblock_t bno, /* block being freed */
2375 int btreeblk) /* block came from a AGF btree */
2376 {
2377 xfs_agf_t *agf; /* a.g. freespace structure */
2378 __be32 *blockp;/* pointer to array entry */
2379 int error;
2380 int logflags;
2381 xfs_mount_t *mp; /* mount structure */
2382 xfs_perag_t *pag; /* per allocation group data */
2383 __be32 *agfl_bno;
2384 int startoff;
2385
2386 agf = XFS_BUF_TO_AGF(agbp);
2387 mp = tp->t_mountp;
2388
2389 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2390 be32_to_cpu(agf->agf_seqno), &agflbp)))
2391 return error;
2392 be32_add_cpu(&agf->agf_fllast, 1);
2393 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2394 agf->agf_fllast = 0;
2395
2396 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2397 be32_add_cpu(&agf->agf_flcount, 1);
2398 xfs_trans_agflist_delta(tp, 1);
2399 pag->pagf_flcount++;
2400
2401 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2402 if (btreeblk) {
2403 be32_add_cpu(&agf->agf_btreeblks, -1);
2404 pag->pagf_btreeblks--;
2405 logflags |= XFS_AGF_BTREEBLKS;
2406 }
2407 xfs_perag_put(pag);
2408
2409 xfs_alloc_log_agf(tp, agbp, logflags);
2410
2411 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2412
2413 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2414 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2415 *blockp = cpu_to_be32(bno);
2416 startoff = (char *)blockp - (char *)agflbp->b_addr;
2417
2418 xfs_alloc_log_agf(tp, agbp, logflags);
2419
2420 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2421 xfs_trans_log_buf(tp, agflbp, startoff,
2422 startoff + sizeof(xfs_agblock_t) - 1);
2423 return 0;
2424 }
2425
2426 static bool
2427 xfs_agf_verify(
2428 struct xfs_mount *mp,
2429 struct xfs_buf *bp)
2430 {
2431 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
2432
2433 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2434 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
2435 return false;
2436 if (!xfs_log_check_lsn(mp,
2437 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2438 return false;
2439 }
2440
2441 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2442 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2443 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2444 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2445 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2446 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2447 return false;
2448
2449 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2450 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2451 return false;
2452
2453 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2454 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2455 return false;
2456
2457 /*
2458 * during growfs operations, the perag is not fully initialised,
2459 * so we can't use it for any useful checking. growfs ensures we can't
2460 * use it by using uncached buffers that don't have the perag attached
2461 * so we can detect and avoid this problem.
2462 */
2463 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2464 return false;
2465
2466 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2467 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2468 return false;
2469
2470 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
2471 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS)
2472 return false;
2473
2474 return true;;
2475
2476 }
2477
2478 static void
2479 xfs_agf_read_verify(
2480 struct xfs_buf *bp)
2481 {
2482 struct xfs_mount *mp = bp->b_target->bt_mount;
2483
2484 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2485 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
2486 xfs_buf_ioerror(bp, -EFSBADCRC);
2487 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2488 XFS_ERRTAG_ALLOC_READ_AGF,
2489 XFS_RANDOM_ALLOC_READ_AGF))
2490 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2491
2492 if (bp->b_error)
2493 xfs_verifier_error(bp);
2494 }
2495
2496 static void
2497 xfs_agf_write_verify(
2498 struct xfs_buf *bp)
2499 {
2500 struct xfs_mount *mp = bp->b_target->bt_mount;
2501 struct xfs_buf_log_item *bip = bp->b_fspriv;
2502
2503 if (!xfs_agf_verify(mp, bp)) {
2504 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2505 xfs_verifier_error(bp);
2506 return;
2507 }
2508
2509 if (!xfs_sb_version_hascrc(&mp->m_sb))
2510 return;
2511
2512 if (bip)
2513 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2514
2515 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
2516 }
2517
2518 const struct xfs_buf_ops xfs_agf_buf_ops = {
2519 .name = "xfs_agf",
2520 .verify_read = xfs_agf_read_verify,
2521 .verify_write = xfs_agf_write_verify,
2522 };
2523
2524 /*
2525 * Read in the allocation group header (free/alloc section).
2526 */
2527 int /* error */
2528 xfs_read_agf(
2529 struct xfs_mount *mp, /* mount point structure */
2530 struct xfs_trans *tp, /* transaction pointer */
2531 xfs_agnumber_t agno, /* allocation group number */
2532 int flags, /* XFS_BUF_ */
2533 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2534 {
2535 int error;
2536
2537 trace_xfs_read_agf(mp, agno);
2538
2539 ASSERT(agno != NULLAGNUMBER);
2540 error = xfs_trans_read_buf(
2541 mp, tp, mp->m_ddev_targp,
2542 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2543 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2544 if (error)
2545 return error;
2546 if (!*bpp)
2547 return 0;
2548
2549 ASSERT(!(*bpp)->b_error);
2550 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2551 return 0;
2552 }
2553
2554 /*
2555 * Read in the allocation group header (free/alloc section).
2556 */
2557 int /* error */
2558 xfs_alloc_read_agf(
2559 struct xfs_mount *mp, /* mount point structure */
2560 struct xfs_trans *tp, /* transaction pointer */
2561 xfs_agnumber_t agno, /* allocation group number */
2562 int flags, /* XFS_ALLOC_FLAG_... */
2563 struct xfs_buf **bpp) /* buffer for the ag freelist header */
2564 {
2565 struct xfs_agf *agf; /* ag freelist header */
2566 struct xfs_perag *pag; /* per allocation group data */
2567 int error;
2568
2569 trace_xfs_alloc_read_agf(mp, agno);
2570
2571 ASSERT(agno != NULLAGNUMBER);
2572 error = xfs_read_agf(mp, tp, agno,
2573 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2574 bpp);
2575 if (error)
2576 return error;
2577 if (!*bpp)
2578 return 0;
2579 ASSERT(!(*bpp)->b_error);
2580
2581 agf = XFS_BUF_TO_AGF(*bpp);
2582 pag = xfs_perag_get(mp, agno);
2583 if (!pag->pagf_init) {
2584 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2585 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2586 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2587 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2588 pag->pagf_levels[XFS_BTNUM_BNOi] =
2589 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2590 pag->pagf_levels[XFS_BTNUM_CNTi] =
2591 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2592 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2593 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
2594 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
2595 spin_lock_init(&pag->pagb_lock);
2596 pag->pagb_count = 0;
2597 /* XXX: pagb_tree doesn't exist in userspace */
2598 //pag->pagb_tree = RB_ROOT;
2599 pag->pagf_init = 1;
2600 }
2601 #ifdef DEBUG
2602 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2603 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2604 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2605 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2606 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2607 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2608 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2609 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2610 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2611 }
2612 #endif
2613 xfs_perag_put(pag);
2614 return 0;
2615 }
2616
2617 /*
2618 * Allocate an extent (variable-size).
2619 * Depending on the allocation type, we either look in a single allocation
2620 * group or loop over the allocation groups to find the result.
2621 */
2622 int /* error */
2623 xfs_alloc_vextent(
2624 xfs_alloc_arg_t *args) /* allocation argument structure */
2625 {
2626 xfs_agblock_t agsize; /* allocation group size */
2627 int error;
2628 int flags; /* XFS_ALLOC_FLAG_... locking flags */
2629 xfs_extlen_t minleft;/* minimum left value, temp copy */
2630 xfs_mount_t *mp; /* mount structure pointer */
2631 xfs_agnumber_t sagno; /* starting allocation group number */
2632 xfs_alloctype_t type; /* input allocation type */
2633 int bump_rotor = 0;
2634 int no_min = 0;
2635 xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
2636
2637 mp = args->mp;
2638 type = args->otype = args->type;
2639 args->agbno = NULLAGBLOCK;
2640 /*
2641 * Just fix this up, for the case where the last a.g. is shorter
2642 * (or there's only one a.g.) and the caller couldn't easily figure
2643 * that out (xfs_bmap_alloc).
2644 */
2645 agsize = mp->m_sb.sb_agblocks;
2646 if (args->maxlen > agsize)
2647 args->maxlen = agsize;
2648 if (args->alignment == 0)
2649 args->alignment = 1;
2650 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2651 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2652 ASSERT(args->minlen <= args->maxlen);
2653 ASSERT(args->minlen <= agsize);
2654 ASSERT(args->mod < args->prod);
2655 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2656 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2657 args->minlen > args->maxlen || args->minlen > agsize ||
2658 args->mod >= args->prod) {
2659 args->fsbno = NULLFSBLOCK;
2660 trace_xfs_alloc_vextent_badargs(args);
2661 return 0;
2662 }
2663 minleft = args->minleft;
2664
2665 switch (type) {
2666 case XFS_ALLOCTYPE_THIS_AG:
2667 case XFS_ALLOCTYPE_NEAR_BNO:
2668 case XFS_ALLOCTYPE_THIS_BNO:
2669 /*
2670 * These three force us into a single a.g.
2671 */
2672 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2673 args->pag = xfs_perag_get(mp, args->agno);
2674 args->minleft = 0;
2675 error = xfs_alloc_fix_freelist(args, 0);
2676 args->minleft = minleft;
2677 if (error) {
2678 trace_xfs_alloc_vextent_nofix(args);
2679 goto error0;
2680 }
2681 if (!args->agbp) {
2682 trace_xfs_alloc_vextent_noagbp(args);
2683 break;
2684 }
2685 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2686 if ((error = xfs_alloc_ag_vextent(args)))
2687 goto error0;
2688 break;
2689 case XFS_ALLOCTYPE_START_BNO:
2690 /*
2691 * Try near allocation first, then anywhere-in-ag after
2692 * the first a.g. fails.
2693 */
2694 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
2695 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2696 args->fsbno = XFS_AGB_TO_FSB(mp,
2697 ((mp->m_agfrotor / rotorstep) %
2698 mp->m_sb.sb_agcount), 0);
2699 bump_rotor = 1;
2700 }
2701 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2702 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2703 /* FALLTHROUGH */
2704 case XFS_ALLOCTYPE_ANY_AG:
2705 case XFS_ALLOCTYPE_START_AG:
2706 case XFS_ALLOCTYPE_FIRST_AG:
2707 /*
2708 * Rotate through the allocation groups looking for a winner.
2709 */
2710 if (type == XFS_ALLOCTYPE_ANY_AG) {
2711 /*
2712 * Start with the last place we left off.
2713 */
2714 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2715 mp->m_sb.sb_agcount;
2716 args->type = XFS_ALLOCTYPE_THIS_AG;
2717 flags = XFS_ALLOC_FLAG_TRYLOCK;
2718 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2719 /*
2720 * Start with allocation group given by bno.
2721 */
2722 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2723 args->type = XFS_ALLOCTYPE_THIS_AG;
2724 sagno = 0;
2725 flags = 0;
2726 } else {
2727 if (type == XFS_ALLOCTYPE_START_AG)
2728 args->type = XFS_ALLOCTYPE_THIS_AG;
2729 /*
2730 * Start with the given allocation group.
2731 */
2732 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2733 flags = XFS_ALLOC_FLAG_TRYLOCK;
2734 }
2735 /*
2736 * Loop over allocation groups twice; first time with
2737 * trylock set, second time without.
2738 */
2739 for (;;) {
2740 args->pag = xfs_perag_get(mp, args->agno);
2741 if (no_min) args->minleft = 0;
2742 error = xfs_alloc_fix_freelist(args, flags);
2743 args->minleft = minleft;
2744 if (error) {
2745 trace_xfs_alloc_vextent_nofix(args);
2746 goto error0;
2747 }
2748 /*
2749 * If we get a buffer back then the allocation will fly.
2750 */
2751 if (args->agbp) {
2752 if ((error = xfs_alloc_ag_vextent(args)))
2753 goto error0;
2754 break;
2755 }
2756
2757 trace_xfs_alloc_vextent_loopfailed(args);
2758
2759 /*
2760 * Didn't work, figure out the next iteration.
2761 */
2762 if (args->agno == sagno &&
2763 type == XFS_ALLOCTYPE_START_BNO)
2764 args->type = XFS_ALLOCTYPE_THIS_AG;
2765 /*
2766 * For the first allocation, we can try any AG to get
2767 * space. However, if we already have allocated a
2768 * block, we don't want to try AGs whose number is below
2769 * sagno. Otherwise, we may end up with out-of-order
2770 * locking of AGF, which might cause deadlock.
2771 */
2772 if (++(args->agno) == mp->m_sb.sb_agcount) {
2773 if (args->firstblock != NULLFSBLOCK)
2774 args->agno = sagno;
2775 else
2776 args->agno = 0;
2777 }
2778 /*
2779 * Reached the starting a.g., must either be done
2780 * or switch to non-trylock mode.
2781 */
2782 if (args->agno == sagno) {
2783 if (no_min == 1) {
2784 args->agbno = NULLAGBLOCK;
2785 trace_xfs_alloc_vextent_allfailed(args);
2786 break;
2787 }
2788 if (flags == 0) {
2789 no_min = 1;
2790 } else {
2791 flags = 0;
2792 if (type == XFS_ALLOCTYPE_START_BNO) {
2793 args->agbno = XFS_FSB_TO_AGBNO(mp,
2794 args->fsbno);
2795 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2796 }
2797 }
2798 }
2799 xfs_perag_put(args->pag);
2800 }
2801 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2802 if (args->agno == sagno)
2803 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2804 (mp->m_sb.sb_agcount * rotorstep);
2805 else
2806 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2807 (mp->m_sb.sb_agcount * rotorstep);
2808 }
2809 break;
2810 default:
2811 ASSERT(0);
2812 /* NOTREACHED */
2813 }
2814 if (args->agbno == NULLAGBLOCK)
2815 args->fsbno = NULLFSBLOCK;
2816 else {
2817 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2818 #ifdef DEBUG
2819 ASSERT(args->len >= args->minlen);
2820 ASSERT(args->len <= args->maxlen);
2821 ASSERT(args->agbno % args->alignment == 0);
2822 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2823 args->len);
2824 #endif
2825
2826 /* Zero the extent if we were asked to do so */
2827 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
2828 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2829 if (error)
2830 goto error0;
2831 }
2832
2833 }
2834 xfs_perag_put(args->pag);
2835 return 0;
2836 error0:
2837 xfs_perag_put(args->pag);
2838 return error;
2839 }
2840
2841 /* Ensure that the freelist is at full capacity. */
2842 int
2843 xfs_free_extent_fix_freelist(
2844 struct xfs_trans *tp,
2845 xfs_agnumber_t agno,
2846 struct xfs_buf **agbp)
2847 {
2848 struct xfs_alloc_arg args;
2849 int error;
2850
2851 memset(&args, 0, sizeof(struct xfs_alloc_arg));
2852 args.tp = tp;
2853 args.mp = tp->t_mountp;
2854 args.agno = agno;
2855
2856 /*
2857 * validate that the block number is legal - the enables us to detect
2858 * and handle a silent filesystem corruption rather than crashing.
2859 */
2860 if (args.agno >= args.mp->m_sb.sb_agcount)
2861 return -EFSCORRUPTED;
2862
2863 args.pag = xfs_perag_get(args.mp, args.agno);
2864 ASSERT(args.pag);
2865
2866 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2867 if (error)
2868 goto out;
2869
2870 *agbp = args.agbp;
2871 out:
2872 xfs_perag_put(args.pag);
2873 return error;
2874 }
2875
2876 /*
2877 * Free an extent.
2878 * Just break up the extent address and hand off to xfs_free_ag_extent
2879 * after fixing up the freelist.
2880 */
2881 int /* error */
2882 xfs_free_extent(
2883 struct xfs_trans *tp, /* transaction pointer */
2884 xfs_fsblock_t bno, /* starting block number of extent */
2885 xfs_extlen_t len, /* length of extent */
2886 struct xfs_owner_info *oinfo, /* extent owner */
2887 enum xfs_ag_resv_type type) /* block reservation type */
2888 {
2889 struct xfs_mount *mp = tp->t_mountp;
2890 struct xfs_buf *agbp;
2891 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2892 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2893 int error;
2894
2895 ASSERT(len != 0);
2896 ASSERT(type != XFS_AG_RESV_AGFL);
2897
2898 if (XFS_TEST_ERROR(false, mp,
2899 XFS_ERRTAG_FREE_EXTENT,
2900 XFS_RANDOM_FREE_EXTENT))
2901 return -EIO;
2902
2903 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2904 if (error)
2905 return error;
2906
2907 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
2908
2909 /* validate the extent size is legal now we have the agf locked */
2910 XFS_WANT_CORRUPTED_GOTO(mp,
2911 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2912 err);
2913
2914 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
2915 if (error)
2916 goto err;
2917
2918 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2919 return 0;
2920
2921 err:
2922 xfs_trans_brelse(tp, agbp);
2923 return error;
2924 }