1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_btree.h"
16 #include "xfs_rmap.h"
17 #include "xfs_alloc_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_extent_busy.h"
20 #include "xfs_errortag.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_trans.h"
24 #include "xfs_buf_item.h"
25 #include "xfs_log.h"
26 #include "xfs_ag.h"
27 #include "xfs_ag_resv.h"
28 #include "xfs_bmap.h"
29 #include "xfs_health.h"
30 #include "xfs_extfree_item.h"
31
32 struct kmem_cache *xfs_extfree_item_cache;
33
34 struct workqueue_struct *xfs_alloc_wq;
35
36 #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
37
38 #define XFSA_FIXUP_BNO_OK 1
39 #define XFSA_FIXUP_CNT_OK 2
40
41 /*
42 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
43 * the beginning of the block for a proper header with the location information
44 * and CRC.
45 */
46 unsigned int
xfs_agfl_size(struct xfs_mount * mp)47 xfs_agfl_size(
48 struct xfs_mount *mp)
49 {
50 unsigned int size = mp->m_sb.sb_sectsize;
51
52 if (xfs_has_crc(mp))
53 size -= sizeof(struct xfs_agfl);
54
55 return size / sizeof(xfs_agblock_t);
56 }
57
58 unsigned int
xfs_refc_block(struct xfs_mount * mp)59 xfs_refc_block(
60 struct xfs_mount *mp)
61 {
62 if (xfs_has_rmapbt(mp))
63 return XFS_RMAP_BLOCK(mp) + 1;
64 if (xfs_has_finobt(mp))
65 return XFS_FIBT_BLOCK(mp) + 1;
66 return XFS_IBT_BLOCK(mp) + 1;
67 }
68
69 xfs_extlen_t
xfs_prealloc_blocks(struct xfs_mount * mp)70 xfs_prealloc_blocks(
71 struct xfs_mount *mp)
72 {
73 if (xfs_has_reflink(mp))
74 return xfs_refc_block(mp) + 1;
75 if (xfs_has_rmapbt(mp))
76 return XFS_RMAP_BLOCK(mp) + 1;
77 if (xfs_has_finobt(mp))
78 return XFS_FIBT_BLOCK(mp) + 1;
79 return XFS_IBT_BLOCK(mp) + 1;
80 }
81
82 /*
83 * The number of blocks per AG that we withhold from xfs_dec_fdblocks to
84 * guarantee that we can refill the AGFL prior to allocating space in a nearly
85 * full AG. Although the space described by the free space btrees, the
86 * blocks used by the freesp btrees themselves, and the blocks owned by the
87 * AGFL are counted in the ondisk fdblocks, it's a mistake to let the ondisk
88 * free space in the AG drop so low that the free space btrees cannot refill an
89 * empty AGFL up to the minimum level. Rather than grind through empty AGs
90 * until the fs goes down, we subtract this many AG blocks from the incore
91 * fdblocks to ensure user allocation does not overcommit the space the
92 * filesystem needs for the AGFLs. The rmap btree uses a per-AG reservation to
93 * withhold space from xfs_dec_fdblocks, so we do not account for that here.
94 */
95 #define XFS_ALLOCBT_AGFL_RESERVE 4
96
97 /*
98 * Compute the number of blocks that we set aside to guarantee the ability to
99 * refill the AGFL and handle a full bmap btree split.
100 *
101 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
102 * AGF buffer (PV 947395), we place constraints on the relationship among
103 * actual allocations for data blocks, freelist blocks, and potential file data
104 * bmap btree blocks. However, these restrictions may result in no actual space
105 * allocated for a delayed extent, for example, a data block in a certain AG is
106 * allocated but there is no additional block for the additional bmap btree
107 * block due to a split of the bmap btree of the file. The result of this may
108 * lead to an infinite loop when the file gets flushed to disk and all delayed
109 * extents need to be actually allocated. To get around this, we explicitly set
110 * aside a few blocks which will not be reserved in delayed allocation.
111 *
112 * For each AG, we need to reserve enough blocks to replenish a totally empty
113 * AGFL and 4 more to handle a potential split of the file's bmap btree.
114 */
115 unsigned int
xfs_alloc_set_aside(struct xfs_mount * mp)116 xfs_alloc_set_aside(
117 struct xfs_mount *mp)
118 {
119 return mp->m_sb.sb_agcount * (XFS_ALLOCBT_AGFL_RESERVE + 4);
120 }
121
122 /*
123 * When deciding how much space to allocate out of an AG, we limit the
124 * allocation maximum size to the size the AG. However, we cannot use all the
125 * blocks in the AG - some are permanently used by metadata. These
126 * blocks are generally:
127 * - the AG superblock, AGF, AGI and AGFL
128 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
129 * the AGI free inode and rmap btree root blocks.
130 * - blocks on the AGFL according to xfs_alloc_set_aside() limits
131 * - the rmapbt root block
132 *
133 * The AG headers are sector sized, so the amount of space they take up is
134 * dependent on filesystem geometry. The others are all single blocks.
135 */
136 unsigned int
xfs_alloc_ag_max_usable(struct xfs_mount * mp)137 xfs_alloc_ag_max_usable(
138 struct xfs_mount *mp)
139 {
140 unsigned int blocks;
141
142 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
143 blocks += XFS_ALLOCBT_AGFL_RESERVE;
144 blocks += 3; /* AGF, AGI btree root blocks */
145 if (xfs_has_finobt(mp))
146 blocks++; /* finobt root block */
147 if (xfs_has_rmapbt(mp))
148 blocks++; /* rmap root block */
149 if (xfs_has_reflink(mp))
150 blocks++; /* refcount root block */
151
152 return mp->m_sb.sb_agblocks - blocks;
153 }
154
155
156 static int
xfs_alloc_lookup(struct xfs_btree_cur * cur,xfs_lookup_t dir,xfs_agblock_t bno,xfs_extlen_t len,int * stat)157 xfs_alloc_lookup(
158 struct xfs_btree_cur *cur,
159 xfs_lookup_t dir,
160 xfs_agblock_t bno,
161 xfs_extlen_t len,
162 int *stat)
163 {
164 int error;
165
166 cur->bc_rec.a.ar_startblock = bno;
167 cur->bc_rec.a.ar_blockcount = len;
168 error = xfs_btree_lookup(cur, dir, stat);
169 if (*stat == 1)
170 cur->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE;
171 else
172 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
173 return error;
174 }
175
176 /*
177 * Lookup the record equal to [bno, len] in the btree given by cur.
178 */
179 static inline int /* error */
xfs_alloc_lookup_eq(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,int * stat)180 xfs_alloc_lookup_eq(
181 struct xfs_btree_cur *cur, /* btree cursor */
182 xfs_agblock_t bno, /* starting block of extent */
183 xfs_extlen_t len, /* length of extent */
184 int *stat) /* success/failure */
185 {
186 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, bno, len, stat);
187 }
188
189 /*
190 * Lookup the first record greater than or equal to [bno, len]
191 * in the btree given by cur.
192 */
193 int /* error */
xfs_alloc_lookup_ge(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,int * stat)194 xfs_alloc_lookup_ge(
195 struct xfs_btree_cur *cur, /* btree cursor */
196 xfs_agblock_t bno, /* starting block of extent */
197 xfs_extlen_t len, /* length of extent */
198 int *stat) /* success/failure */
199 {
200 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, bno, len, stat);
201 }
202
203 /*
204 * Lookup the first record less than or equal to [bno, len]
205 * in the btree given by cur.
206 */
207 int /* error */
xfs_alloc_lookup_le(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,int * stat)208 xfs_alloc_lookup_le(
209 struct xfs_btree_cur *cur, /* btree cursor */
210 xfs_agblock_t bno, /* starting block of extent */
211 xfs_extlen_t len, /* length of extent */
212 int *stat) /* success/failure */
213 {
214 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, bno, len, stat);
215 }
216
217 static inline bool
xfs_alloc_cur_active(struct xfs_btree_cur * cur)218 xfs_alloc_cur_active(
219 struct xfs_btree_cur *cur)
220 {
221 return cur && (cur->bc_flags & XFS_BTREE_ALLOCBT_ACTIVE);
222 }
223
224 /*
225 * Update the record referred to by cur to the value given
226 * by [bno, len].
227 * This either works (return 0) or gets an EFSCORRUPTED error.
228 */
229 STATIC int /* error */
xfs_alloc_update(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len)230 xfs_alloc_update(
231 struct xfs_btree_cur *cur, /* btree cursor */
232 xfs_agblock_t bno, /* starting block of extent */
233 xfs_extlen_t len) /* length of extent */
234 {
235 union xfs_btree_rec rec;
236
237 rec.alloc.ar_startblock = cpu_to_be32(bno);
238 rec.alloc.ar_blockcount = cpu_to_be32(len);
239 return xfs_btree_update(cur, &rec);
240 }
241
242 /* Convert the ondisk btree record to its incore representation. */
243 void
xfs_alloc_btrec_to_irec(const union xfs_btree_rec * rec,struct xfs_alloc_rec_incore * irec)244 xfs_alloc_btrec_to_irec(
245 const union xfs_btree_rec *rec,
246 struct xfs_alloc_rec_incore *irec)
247 {
248 irec->ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
249 irec->ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
250 }
251
252 /* Simple checks for free space records. */
253 xfs_failaddr_t
xfs_alloc_check_irec(struct xfs_perag * pag,const struct xfs_alloc_rec_incore * irec)254 xfs_alloc_check_irec(
255 struct xfs_perag *pag,
256 const struct xfs_alloc_rec_incore *irec)
257 {
258 if (irec->ar_blockcount == 0)
259 return __this_address;
260
261 /* check for valid extent range, including overflow */
262 if (!xfs_verify_agbext(pag, irec->ar_startblock, irec->ar_blockcount))
263 return __this_address;
264
265 return NULL;
266 }
267
268 static inline int
xfs_alloc_complain_bad_rec(struct xfs_btree_cur * cur,xfs_failaddr_t fa,const struct xfs_alloc_rec_incore * irec)269 xfs_alloc_complain_bad_rec(
270 struct xfs_btree_cur *cur,
271 xfs_failaddr_t fa,
272 const struct xfs_alloc_rec_incore *irec)
273 {
274 struct xfs_mount *mp = cur->bc_mp;
275
276 xfs_warn(mp,
277 "%sbt record corruption in AG %d detected at %pS!",
278 cur->bc_ops->name, cur->bc_group->xg_gno, fa);
279 xfs_warn(mp,
280 "start block 0x%x block count 0x%x", irec->ar_startblock,
281 irec->ar_blockcount);
282 xfs_btree_mark_sick(cur);
283 return -EFSCORRUPTED;
284 }
285
286 /*
287 * Get the data from the pointed-to record.
288 */
289 int /* error */
xfs_alloc_get_rec(struct xfs_btree_cur * cur,xfs_agblock_t * bno,xfs_extlen_t * len,int * stat)290 xfs_alloc_get_rec(
291 struct xfs_btree_cur *cur, /* btree cursor */
292 xfs_agblock_t *bno, /* output: starting block of extent */
293 xfs_extlen_t *len, /* output: length of extent */
294 int *stat) /* output: success/failure */
295 {
296 struct xfs_alloc_rec_incore irec;
297 union xfs_btree_rec *rec;
298 xfs_failaddr_t fa;
299 int error;
300
301 error = xfs_btree_get_rec(cur, &rec, stat);
302 if (error || !(*stat))
303 return error;
304
305 xfs_alloc_btrec_to_irec(rec, &irec);
306 fa = xfs_alloc_check_irec(to_perag(cur->bc_group), &irec);
307 if (fa)
308 return xfs_alloc_complain_bad_rec(cur, fa, &irec);
309
310 *bno = irec.ar_startblock;
311 *len = irec.ar_blockcount;
312 return 0;
313 }
314
315 /*
316 * Compute aligned version of the found extent.
317 * Takes alignment and min length into account.
318 */
319 STATIC bool
xfs_alloc_compute_aligned(xfs_alloc_arg_t * args,xfs_agblock_t foundbno,xfs_extlen_t foundlen,xfs_agblock_t * resbno,xfs_extlen_t * reslen,unsigned * busy_gen)320 xfs_alloc_compute_aligned(
321 xfs_alloc_arg_t *args, /* allocation argument structure */
322 xfs_agblock_t foundbno, /* starting block in found extent */
323 xfs_extlen_t foundlen, /* length in found extent */
324 xfs_agblock_t *resbno, /* result block number */
325 xfs_extlen_t *reslen, /* result length */
326 unsigned *busy_gen)
327 {
328 xfs_agblock_t bno = foundbno;
329 xfs_extlen_t len = foundlen;
330 xfs_extlen_t diff;
331 bool busy;
332
333 /* Trim busy sections out of found extent */
334 busy = xfs_extent_busy_trim(pag_group(args->pag), args->minlen,
335 args->maxlen, &bno, &len, busy_gen);
336
337 /*
338 * If we have a largish extent that happens to start before min_agbno,
339 * see if we can shift it into range...
340 */
341 if (bno < args->min_agbno && bno + len > args->min_agbno) {
342 diff = args->min_agbno - bno;
343 if (len > diff) {
344 bno += diff;
345 len -= diff;
346 }
347 }
348
349 if (args->alignment > 1 && len >= args->minlen) {
350 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
351
352 diff = aligned_bno - bno;
353
354 *resbno = aligned_bno;
355 *reslen = diff >= len ? 0 : len - diff;
356 } else {
357 *resbno = bno;
358 *reslen = len;
359 }
360
361 return busy;
362 }
363
364 /*
365 * Compute best start block and diff for "near" allocations.
366 * freelen >= wantlen already checked by caller.
367 */
368 STATIC xfs_extlen_t /* difference value (absolute) */
xfs_alloc_compute_diff(xfs_agblock_t wantbno,xfs_extlen_t wantlen,xfs_extlen_t alignment,int datatype,xfs_agblock_t freebno,xfs_extlen_t freelen,xfs_agblock_t * newbnop)369 xfs_alloc_compute_diff(
370 xfs_agblock_t wantbno, /* target starting block */
371 xfs_extlen_t wantlen, /* target length */
372 xfs_extlen_t alignment, /* target alignment */
373 int datatype, /* are we allocating data? */
374 xfs_agblock_t freebno, /* freespace's starting block */
375 xfs_extlen_t freelen, /* freespace's length */
376 xfs_agblock_t *newbnop) /* result: best start block from free */
377 {
378 xfs_agblock_t freeend; /* end of freespace extent */
379 xfs_agblock_t newbno1; /* return block number */
380 xfs_agblock_t newbno2; /* other new block number */
381 xfs_extlen_t newlen1=0; /* length with newbno1 */
382 xfs_extlen_t newlen2=0; /* length with newbno2 */
383 xfs_agblock_t wantend; /* end of target extent */
384 bool userdata = datatype & XFS_ALLOC_USERDATA;
385
386 ASSERT(freelen >= wantlen);
387 freeend = freebno + freelen;
388 wantend = wantbno + wantlen;
389 /*
390 * We want to allocate from the start of a free extent if it is past
391 * the desired block or if we are allocating user data and the free
392 * extent is before desired block. The second case is there to allow
393 * for contiguous allocation from the remaining free space if the file
394 * grows in the short term.
395 */
396 if (freebno >= wantbno || (userdata && freeend < wantend)) {
397 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
398 newbno1 = NULLAGBLOCK;
399 } else if (freeend >= wantend && alignment > 1) {
400 newbno1 = roundup(wantbno, alignment);
401 newbno2 = newbno1 - alignment;
402 if (newbno1 >= freeend)
403 newbno1 = NULLAGBLOCK;
404 else
405 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
406 if (newbno2 < freebno)
407 newbno2 = NULLAGBLOCK;
408 else
409 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
410 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
411 if (newlen1 < newlen2 ||
412 (newlen1 == newlen2 &&
413 XFS_ABSDIFF(newbno1, wantbno) >
414 XFS_ABSDIFF(newbno2, wantbno)))
415 newbno1 = newbno2;
416 } else if (newbno2 != NULLAGBLOCK)
417 newbno1 = newbno2;
418 } else if (freeend >= wantend) {
419 newbno1 = wantbno;
420 } else if (alignment > 1) {
421 newbno1 = roundup(freeend - wantlen, alignment);
422 if (newbno1 > freeend - wantlen &&
423 newbno1 - alignment >= freebno)
424 newbno1 -= alignment;
425 else if (newbno1 >= freeend)
426 newbno1 = NULLAGBLOCK;
427 } else
428 newbno1 = freeend - wantlen;
429 *newbnop = newbno1;
430 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
431 }
432
433 /*
434 * Fix up the length, based on mod and prod.
435 * len should be k * prod + mod for some k.
436 * If len is too small it is returned unchanged.
437 * If len hits maxlen it is left alone.
438 */
439 STATIC void
xfs_alloc_fix_len(xfs_alloc_arg_t * args)440 xfs_alloc_fix_len(
441 xfs_alloc_arg_t *args) /* allocation argument structure */
442 {
443 xfs_extlen_t k;
444 xfs_extlen_t rlen;
445
446 ASSERT(args->mod < args->prod);
447 rlen = args->len;
448 ASSERT(rlen >= args->minlen);
449 ASSERT(rlen <= args->maxlen);
450 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
451 (args->mod == 0 && rlen < args->prod))
452 return;
453 k = rlen % args->prod;
454 if (k == args->mod)
455 return;
456 if (k > args->mod)
457 rlen = rlen - (k - args->mod);
458 else
459 rlen = rlen - args->prod + (args->mod - k);
460 /* casts to (int) catch length underflows */
461 if ((int)rlen < (int)args->minlen)
462 return;
463 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
464 ASSERT(rlen % args->prod == args->mod);
465 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
466 rlen + args->minleft);
467 args->len = rlen;
468 }
469
470 /*
471 * Determine if the cursor points to the block that contains the right-most
472 * block of records in the by-count btree. This block contains the largest
473 * contiguous free extent in the AG, so if we modify a record in this block we
474 * need to call xfs_alloc_fixup_longest() once the modifications are done to
475 * ensure the agf->agf_longest field is kept up to date with the longest free
476 * extent tracked by the by-count btree.
477 */
478 static bool
xfs_alloc_cursor_at_lastrec(struct xfs_btree_cur * cnt_cur)479 xfs_alloc_cursor_at_lastrec(
480 struct xfs_btree_cur *cnt_cur)
481 {
482 struct xfs_btree_block *block;
483 union xfs_btree_ptr ptr;
484 struct xfs_buf *bp;
485
486 block = xfs_btree_get_block(cnt_cur, 0, &bp);
487
488 xfs_btree_get_sibling(cnt_cur, block, &ptr, XFS_BB_RIGHTSIB);
489 return xfs_btree_ptr_is_null(cnt_cur, &ptr);
490 }
491
492 /*
493 * Find the rightmost record of the cntbt, and return the longest free space
494 * recorded in it. Simply set both the block number and the length to their
495 * maximum values before searching.
496 */
497 static int
xfs_cntbt_longest(struct xfs_btree_cur * cnt_cur,xfs_extlen_t * longest)498 xfs_cntbt_longest(
499 struct xfs_btree_cur *cnt_cur,
500 xfs_extlen_t *longest)
501 {
502 struct xfs_alloc_rec_incore irec;
503 union xfs_btree_rec *rec;
504 int stat = 0;
505 int error;
506
507 memset(&cnt_cur->bc_rec, 0xFF, sizeof(cnt_cur->bc_rec));
508 error = xfs_btree_lookup(cnt_cur, XFS_LOOKUP_LE, &stat);
509 if (error)
510 return error;
511 if (!stat) {
512 /* totally empty tree */
513 *longest = 0;
514 return 0;
515 }
516
517 error = xfs_btree_get_rec(cnt_cur, &rec, &stat);
518 if (error)
519 return error;
520 if (XFS_IS_CORRUPT(cnt_cur->bc_mp, !stat)) {
521 xfs_btree_mark_sick(cnt_cur);
522 return -EFSCORRUPTED;
523 }
524
525 xfs_alloc_btrec_to_irec(rec, &irec);
526 *longest = irec.ar_blockcount;
527 return 0;
528 }
529
530 /*
531 * Update the longest contiguous free extent in the AG from the by-count cursor
532 * that is passed to us. This should be done at the end of any allocation or
533 * freeing operation that touches the longest extent in the btree.
534 *
535 * Needing to update the longest extent can be determined by calling
536 * xfs_alloc_cursor_at_lastrec() after the cursor is positioned for record
537 * modification but before the modification begins.
538 */
539 static int
xfs_alloc_fixup_longest(struct xfs_btree_cur * cnt_cur)540 xfs_alloc_fixup_longest(
541 struct xfs_btree_cur *cnt_cur)
542 {
543 struct xfs_perag *pag = to_perag(cnt_cur->bc_group);
544 struct xfs_buf *bp = cnt_cur->bc_ag.agbp;
545 struct xfs_agf *agf = bp->b_addr;
546 xfs_extlen_t longest = 0;
547 int error;
548
549 /* Lookup last rec in order to update AGF. */
550 error = xfs_cntbt_longest(cnt_cur, &longest);
551 if (error)
552 return error;
553
554 pag->pagf_longest = longest;
555 agf->agf_longest = cpu_to_be32(pag->pagf_longest);
556 xfs_alloc_log_agf(cnt_cur->bc_tp, bp, XFS_AGF_LONGEST);
557
558 return 0;
559 }
560
561 /*
562 * Update the two btrees, logically removing from freespace the extent
563 * starting at rbno, rlen blocks. The extent is contained within the
564 * actual (current) free extent fbno for flen blocks.
565 * Flags are passed in indicating whether the cursors are set to the
566 * relevant records.
567 */
568 STATIC int /* error code */
xfs_alloc_fixup_trees(struct xfs_btree_cur * cnt_cur,struct xfs_btree_cur * bno_cur,xfs_agblock_t fbno,xfs_extlen_t flen,xfs_agblock_t rbno,xfs_extlen_t rlen,int flags)569 xfs_alloc_fixup_trees(
570 struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */
571 struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */
572 xfs_agblock_t fbno, /* starting block of free extent */
573 xfs_extlen_t flen, /* length of free extent */
574 xfs_agblock_t rbno, /* starting block of returned extent */
575 xfs_extlen_t rlen, /* length of returned extent */
576 int flags) /* flags, XFSA_FIXUP_... */
577 {
578 int error; /* error code */
579 int i; /* operation results */
580 xfs_agblock_t nfbno1; /* first new free startblock */
581 xfs_agblock_t nfbno2; /* second new free startblock */
582 xfs_extlen_t nflen1=0; /* first new free length */
583 xfs_extlen_t nflen2=0; /* second new free length */
584 struct xfs_mount *mp;
585 bool fixup_longest = false;
586
587 mp = cnt_cur->bc_mp;
588
589 /*
590 * Look up the record in the by-size tree if necessary.
591 */
592 if (flags & XFSA_FIXUP_CNT_OK) {
593 #ifdef DEBUG
594 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
595 return error;
596 if (XFS_IS_CORRUPT(mp,
597 i != 1 ||
598 nfbno1 != fbno ||
599 nflen1 != flen)) {
600 xfs_btree_mark_sick(cnt_cur);
601 return -EFSCORRUPTED;
602 }
603 #endif
604 } else {
605 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
606 return error;
607 if (XFS_IS_CORRUPT(mp, i != 1)) {
608 xfs_btree_mark_sick(cnt_cur);
609 return -EFSCORRUPTED;
610 }
611 }
612 /*
613 * Look up the record in the by-block tree if necessary.
614 */
615 if (flags & XFSA_FIXUP_BNO_OK) {
616 #ifdef DEBUG
617 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
618 return error;
619 if (XFS_IS_CORRUPT(mp,
620 i != 1 ||
621 nfbno1 != fbno ||
622 nflen1 != flen)) {
623 xfs_btree_mark_sick(bno_cur);
624 return -EFSCORRUPTED;
625 }
626 #endif
627 } else {
628 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
629 return error;
630 if (XFS_IS_CORRUPT(mp, i != 1)) {
631 xfs_btree_mark_sick(bno_cur);
632 return -EFSCORRUPTED;
633 }
634 }
635
636 #ifdef DEBUG
637 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
638 struct xfs_btree_block *bnoblock;
639 struct xfs_btree_block *cntblock;
640
641 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp);
642 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp);
643
644 if (XFS_IS_CORRUPT(mp,
645 bnoblock->bb_numrecs !=
646 cntblock->bb_numrecs)) {
647 xfs_btree_mark_sick(bno_cur);
648 return -EFSCORRUPTED;
649 }
650 }
651 #endif
652
653 /*
654 * Deal with all four cases: the allocated record is contained
655 * within the freespace record, so we can have new freespace
656 * at either (or both) end, or no freespace remaining.
657 */
658 if (rbno == fbno && rlen == flen)
659 nfbno1 = nfbno2 = NULLAGBLOCK;
660 else if (rbno == fbno) {
661 nfbno1 = rbno + rlen;
662 nflen1 = flen - rlen;
663 nfbno2 = NULLAGBLOCK;
664 } else if (rbno + rlen == fbno + flen) {
665 nfbno1 = fbno;
666 nflen1 = flen - rlen;
667 nfbno2 = NULLAGBLOCK;
668 } else {
669 nfbno1 = fbno;
670 nflen1 = rbno - fbno;
671 nfbno2 = rbno + rlen;
672 nflen2 = (fbno + flen) - nfbno2;
673 }
674
675 if (xfs_alloc_cursor_at_lastrec(cnt_cur))
676 fixup_longest = true;
677
678 /*
679 * Delete the entry from the by-size btree.
680 */
681 if ((error = xfs_btree_delete(cnt_cur, &i)))
682 return error;
683 if (XFS_IS_CORRUPT(mp, i != 1)) {
684 xfs_btree_mark_sick(cnt_cur);
685 return -EFSCORRUPTED;
686 }
687 /*
688 * Add new by-size btree entry(s).
689 */
690 if (nfbno1 != NULLAGBLOCK) {
691 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
692 return error;
693 if (XFS_IS_CORRUPT(mp, i != 0)) {
694 xfs_btree_mark_sick(cnt_cur);
695 return -EFSCORRUPTED;
696 }
697 if ((error = xfs_btree_insert(cnt_cur, &i)))
698 return error;
699 if (XFS_IS_CORRUPT(mp, i != 1)) {
700 xfs_btree_mark_sick(cnt_cur);
701 return -EFSCORRUPTED;
702 }
703 }
704 if (nfbno2 != NULLAGBLOCK) {
705 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
706 return error;
707 if (XFS_IS_CORRUPT(mp, i != 0)) {
708 xfs_btree_mark_sick(cnt_cur);
709 return -EFSCORRUPTED;
710 }
711 if ((error = xfs_btree_insert(cnt_cur, &i)))
712 return error;
713 if (XFS_IS_CORRUPT(mp, i != 1)) {
714 xfs_btree_mark_sick(cnt_cur);
715 return -EFSCORRUPTED;
716 }
717 }
718 /*
719 * Fix up the by-block btree entry(s).
720 */
721 if (nfbno1 == NULLAGBLOCK) {
722 /*
723 * No remaining freespace, just delete the by-block tree entry.
724 */
725 if ((error = xfs_btree_delete(bno_cur, &i)))
726 return error;
727 if (XFS_IS_CORRUPT(mp, i != 1)) {
728 xfs_btree_mark_sick(bno_cur);
729 return -EFSCORRUPTED;
730 }
731 } else {
732 /*
733 * Update the by-block entry to start later|be shorter.
734 */
735 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
736 return error;
737 }
738 if (nfbno2 != NULLAGBLOCK) {
739 /*
740 * 2 resulting free entries, need to add one.
741 */
742 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
743 return error;
744 if (XFS_IS_CORRUPT(mp, i != 0)) {
745 xfs_btree_mark_sick(bno_cur);
746 return -EFSCORRUPTED;
747 }
748 if ((error = xfs_btree_insert(bno_cur, &i)))
749 return error;
750 if (XFS_IS_CORRUPT(mp, i != 1)) {
751 xfs_btree_mark_sick(bno_cur);
752 return -EFSCORRUPTED;
753 }
754 }
755
756 if (fixup_longest)
757 return xfs_alloc_fixup_longest(cnt_cur);
758
759 return 0;
760 }
761
762 /*
763 * We do not verify the AGFL contents against AGF-based index counters here,
764 * even though we may have access to the perag that contains shadow copies. We
765 * don't know if the AGF based counters have been checked, and if they have they
766 * still may be inconsistent because they haven't yet been reset on the first
767 * allocation after the AGF has been read in.
768 *
769 * This means we can only check that all agfl entries contain valid or null
770 * values because we can't reliably determine the active range to exclude
771 * NULLAGBNO as a valid value.
772 *
773 * However, we can't even do that for v4 format filesystems because there are
774 * old versions of mkfs out there that does not initialise the AGFL to known,
775 * verifiable values. HEnce we can't tell the difference between a AGFL block
776 * allocated by mkfs and a corrupted AGFL block here on v4 filesystems.
777 *
778 * As a result, we can only fully validate AGFL block numbers when we pull them
779 * from the freelist in xfs_alloc_get_freelist().
780 */
781 static xfs_failaddr_t
xfs_agfl_verify(struct xfs_buf * bp)782 xfs_agfl_verify(
783 struct xfs_buf *bp)
784 {
785 struct xfs_mount *mp = bp->b_mount;
786 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
787 __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp);
788 int i;
789
790 if (!xfs_has_crc(mp))
791 return NULL;
792
793 if (!xfs_verify_magic(bp, agfl->agfl_magicnum))
794 return __this_address;
795 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
796 return __this_address;
797 /*
798 * during growfs operations, the perag is not fully initialised,
799 * so we can't use it for any useful checking. growfs ensures we can't
800 * use it by using uncached buffers that don't have the perag attached
801 * so we can detect and avoid this problem.
802 */
803 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != pag_agno((bp->b_pag)))
804 return __this_address;
805
806 for (i = 0; i < xfs_agfl_size(mp); i++) {
807 if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK &&
808 be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks)
809 return __this_address;
810 }
811
812 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
813 return __this_address;
814 return NULL;
815 }
816
817 static void
xfs_agfl_read_verify(struct xfs_buf * bp)818 xfs_agfl_read_verify(
819 struct xfs_buf *bp)
820 {
821 struct xfs_mount *mp = bp->b_mount;
822 xfs_failaddr_t fa;
823
824 /*
825 * There is no verification of non-crc AGFLs because mkfs does not
826 * initialise the AGFL to zero or NULL. Hence the only valid part of the
827 * AGFL is what the AGF says is active. We can't get to the AGF, so we
828 * can't verify just those entries are valid.
829 */
830 if (!xfs_has_crc(mp))
831 return;
832
833 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
834 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
835 else {
836 fa = xfs_agfl_verify(bp);
837 if (fa)
838 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
839 }
840 }
841
842 static void
xfs_agfl_write_verify(struct xfs_buf * bp)843 xfs_agfl_write_verify(
844 struct xfs_buf *bp)
845 {
846 struct xfs_mount *mp = bp->b_mount;
847 struct xfs_buf_log_item *bip = bp->b_log_item;
848 xfs_failaddr_t fa;
849
850 /* no verification of non-crc AGFLs */
851 if (!xfs_has_crc(mp))
852 return;
853
854 fa = xfs_agfl_verify(bp);
855 if (fa) {
856 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
857 return;
858 }
859
860 if (bip)
861 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
862
863 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
864 }
865
866 const struct xfs_buf_ops xfs_agfl_buf_ops = {
867 .name = "xfs_agfl",
868 .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) },
869 .verify_read = xfs_agfl_read_verify,
870 .verify_write = xfs_agfl_write_verify,
871 .verify_struct = xfs_agfl_verify,
872 };
873
874 /*
875 * Read in the allocation group free block array.
876 */
877 int
xfs_alloc_read_agfl(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf ** bpp)878 xfs_alloc_read_agfl(
879 struct xfs_perag *pag,
880 struct xfs_trans *tp,
881 struct xfs_buf **bpp)
882 {
883 struct xfs_mount *mp = pag_mount(pag);
884 struct xfs_buf *bp;
885 int error;
886
887 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
888 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGFL_DADDR(mp)),
889 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
890 if (xfs_metadata_is_sick(error))
891 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGFL);
892 if (error)
893 return error;
894 xfs_buf_set_ref(bp, XFS_AGFL_REF);
895 *bpp = bp;
896 return 0;
897 }
898
899 STATIC int
xfs_alloc_update_counters(struct xfs_trans * tp,struct xfs_buf * agbp,long len)900 xfs_alloc_update_counters(
901 struct xfs_trans *tp,
902 struct xfs_buf *agbp,
903 long len)
904 {
905 struct xfs_agf *agf = agbp->b_addr;
906
907 agbp->b_pag->pagf_freeblks += len;
908 be32_add_cpu(&agf->agf_freeblks, len);
909
910 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
911 be32_to_cpu(agf->agf_length))) {
912 xfs_buf_mark_corrupt(agbp);
913 xfs_ag_mark_sick(agbp->b_pag, XFS_SICK_AG_AGF);
914 return -EFSCORRUPTED;
915 }
916
917 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
918 return 0;
919 }
920
921 /*
922 * Block allocation algorithm and data structures.
923 */
924 struct xfs_alloc_cur {
925 struct xfs_btree_cur *cnt; /* btree cursors */
926 struct xfs_btree_cur *bnolt;
927 struct xfs_btree_cur *bnogt;
928 xfs_extlen_t cur_len;/* current search length */
929 xfs_agblock_t rec_bno;/* extent startblock */
930 xfs_extlen_t rec_len;/* extent length */
931 xfs_agblock_t bno; /* alloc bno */
932 xfs_extlen_t len; /* alloc len */
933 xfs_extlen_t diff; /* diff from search bno */
934 unsigned int busy_gen;/* busy state */
935 bool busy;
936 };
937
938 /*
939 * Set up cursors, etc. in the extent allocation cursor. This function can be
940 * called multiple times to reset an initialized structure without having to
941 * reallocate cursors.
942 */
943 static int
xfs_alloc_cur_setup(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur)944 xfs_alloc_cur_setup(
945 struct xfs_alloc_arg *args,
946 struct xfs_alloc_cur *acur)
947 {
948 int error;
949 int i;
950
951 acur->cur_len = args->maxlen;
952 acur->rec_bno = 0;
953 acur->rec_len = 0;
954 acur->bno = 0;
955 acur->len = 0;
956 acur->diff = -1;
957 acur->busy = false;
958 acur->busy_gen = 0;
959
960 /*
961 * Perform an initial cntbt lookup to check for availability of maxlen
962 * extents. If this fails, we'll return -ENOSPC to signal the caller to
963 * attempt a small allocation.
964 */
965 if (!acur->cnt)
966 acur->cnt = xfs_cntbt_init_cursor(args->mp, args->tp,
967 args->agbp, args->pag);
968 error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i);
969 if (error)
970 return error;
971
972 /*
973 * Allocate the bnobt left and right search cursors.
974 */
975 if (!acur->bnolt)
976 acur->bnolt = xfs_bnobt_init_cursor(args->mp, args->tp,
977 args->agbp, args->pag);
978 if (!acur->bnogt)
979 acur->bnogt = xfs_bnobt_init_cursor(args->mp, args->tp,
980 args->agbp, args->pag);
981 return i == 1 ? 0 : -ENOSPC;
982 }
983
984 static void
xfs_alloc_cur_close(struct xfs_alloc_cur * acur,bool error)985 xfs_alloc_cur_close(
986 struct xfs_alloc_cur *acur,
987 bool error)
988 {
989 int cur_error = XFS_BTREE_NOERROR;
990
991 if (error)
992 cur_error = XFS_BTREE_ERROR;
993
994 if (acur->cnt)
995 xfs_btree_del_cursor(acur->cnt, cur_error);
996 if (acur->bnolt)
997 xfs_btree_del_cursor(acur->bnolt, cur_error);
998 if (acur->bnogt)
999 xfs_btree_del_cursor(acur->bnogt, cur_error);
1000 acur->cnt = acur->bnolt = acur->bnogt = NULL;
1001 }
1002
1003 /*
1004 * Check an extent for allocation and track the best available candidate in the
1005 * allocation structure. The cursor is deactivated if it has entered an out of
1006 * range state based on allocation arguments. Optionally return the extent
1007 * extent geometry and allocation status if requested by the caller.
1008 */
1009 static int
xfs_alloc_cur_check(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur,struct xfs_btree_cur * cur,int * new)1010 xfs_alloc_cur_check(
1011 struct xfs_alloc_arg *args,
1012 struct xfs_alloc_cur *acur,
1013 struct xfs_btree_cur *cur,
1014 int *new)
1015 {
1016 int error, i;
1017 xfs_agblock_t bno, bnoa, bnew;
1018 xfs_extlen_t len, lena, diff = -1;
1019 bool busy;
1020 unsigned busy_gen = 0;
1021 bool deactivate = false;
1022 bool isbnobt = xfs_btree_is_bno(cur->bc_ops);
1023
1024 *new = 0;
1025
1026 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
1027 if (error)
1028 return error;
1029 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1030 xfs_btree_mark_sick(cur);
1031 return -EFSCORRUPTED;
1032 }
1033
1034 /*
1035 * Check minlen and deactivate a cntbt cursor if out of acceptable size
1036 * range (i.e., walking backwards looking for a minlen extent).
1037 */
1038 if (len < args->minlen) {
1039 deactivate = !isbnobt;
1040 goto out;
1041 }
1042
1043 busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena,
1044 &busy_gen);
1045 acur->busy |= busy;
1046 if (busy)
1047 acur->busy_gen = busy_gen;
1048 /* deactivate a bnobt cursor outside of locality range */
1049 if (bnoa < args->min_agbno || bnoa > args->max_agbno) {
1050 deactivate = isbnobt;
1051 goto out;
1052 }
1053 if (lena < args->minlen)
1054 goto out;
1055
1056 args->len = XFS_EXTLEN_MIN(lena, args->maxlen);
1057 xfs_alloc_fix_len(args);
1058 ASSERT(args->len >= args->minlen);
1059 if (args->len < acur->len)
1060 goto out;
1061
1062 /*
1063 * We have an aligned record that satisfies minlen and beats or matches
1064 * the candidate extent size. Compare locality for near allocation mode.
1065 */
1066 diff = xfs_alloc_compute_diff(args->agbno, args->len,
1067 args->alignment, args->datatype,
1068 bnoa, lena, &bnew);
1069 if (bnew == NULLAGBLOCK)
1070 goto out;
1071
1072 /*
1073 * Deactivate a bnobt cursor with worse locality than the current best.
1074 */
1075 if (diff > acur->diff) {
1076 deactivate = isbnobt;
1077 goto out;
1078 }
1079
1080 ASSERT(args->len > acur->len ||
1081 (args->len == acur->len && diff <= acur->diff));
1082 acur->rec_bno = bno;
1083 acur->rec_len = len;
1084 acur->bno = bnew;
1085 acur->len = args->len;
1086 acur->diff = diff;
1087 *new = 1;
1088
1089 /*
1090 * We're done if we found a perfect allocation. This only deactivates
1091 * the current cursor, but this is just an optimization to terminate a
1092 * cntbt search that otherwise runs to the edge of the tree.
1093 */
1094 if (acur->diff == 0 && acur->len == args->maxlen)
1095 deactivate = true;
1096 out:
1097 if (deactivate)
1098 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
1099 trace_xfs_alloc_cur_check(cur, bno, len, diff, *new);
1100 return 0;
1101 }
1102
1103 /*
1104 * Complete an allocation of a candidate extent. Remove the extent from both
1105 * trees and update the args structure.
1106 */
1107 STATIC int
xfs_alloc_cur_finish(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur)1108 xfs_alloc_cur_finish(
1109 struct xfs_alloc_arg *args,
1110 struct xfs_alloc_cur *acur)
1111 {
1112 int error;
1113
1114 ASSERT(acur->cnt && acur->bnolt);
1115 ASSERT(acur->bno >= acur->rec_bno);
1116 ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len);
1117 ASSERT(xfs_verify_agbext(args->pag, acur->rec_bno, acur->rec_len));
1118
1119 error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno,
1120 acur->rec_len, acur->bno, acur->len, 0);
1121 if (error)
1122 return error;
1123
1124 args->agbno = acur->bno;
1125 args->len = acur->len;
1126 args->wasfromfl = 0;
1127
1128 trace_xfs_alloc_cur(args);
1129 return 0;
1130 }
1131
1132 /*
1133 * Locality allocation lookup algorithm. This expects a cntbt cursor and uses
1134 * bno optimized lookup to search for extents with ideal size and locality.
1135 */
1136 STATIC int
xfs_alloc_cntbt_iter(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur)1137 xfs_alloc_cntbt_iter(
1138 struct xfs_alloc_arg *args,
1139 struct xfs_alloc_cur *acur)
1140 {
1141 struct xfs_btree_cur *cur = acur->cnt;
1142 xfs_agblock_t bno;
1143 xfs_extlen_t len, cur_len;
1144 int error;
1145 int i;
1146
1147 if (!xfs_alloc_cur_active(cur))
1148 return 0;
1149
1150 /* locality optimized lookup */
1151 cur_len = acur->cur_len;
1152 error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i);
1153 if (error)
1154 return error;
1155 if (i == 0)
1156 return 0;
1157 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
1158 if (error)
1159 return error;
1160
1161 /* check the current record and update search length from it */
1162 error = xfs_alloc_cur_check(args, acur, cur, &i);
1163 if (error)
1164 return error;
1165 ASSERT(len >= acur->cur_len);
1166 acur->cur_len = len;
1167
1168 /*
1169 * We looked up the first record >= [agbno, len] above. The agbno is a
1170 * secondary key and so the current record may lie just before or after
1171 * agbno. If it is past agbno, check the previous record too so long as
1172 * the length matches as it may be closer. Don't check a smaller record
1173 * because that could deactivate our cursor.
1174 */
1175 if (bno > args->agbno) {
1176 error = xfs_btree_decrement(cur, 0, &i);
1177 if (!error && i) {
1178 error = xfs_alloc_get_rec(cur, &bno, &len, &i);
1179 if (!error && i && len == acur->cur_len)
1180 error = xfs_alloc_cur_check(args, acur, cur,
1181 &i);
1182 }
1183 if (error)
1184 return error;
1185 }
1186
1187 /*
1188 * Increment the search key until we find at least one allocation
1189 * candidate or if the extent we found was larger. Otherwise, double the
1190 * search key to optimize the search. Efficiency is more important here
1191 * than absolute best locality.
1192 */
1193 cur_len <<= 1;
1194 if (!acur->len || acur->cur_len >= cur_len)
1195 acur->cur_len++;
1196 else
1197 acur->cur_len = cur_len;
1198
1199 return error;
1200 }
1201
1202 /*
1203 * Deal with the case where only small freespaces remain. Either return the
1204 * contents of the last freespace record, or allocate space from the freelist if
1205 * there is nothing in the tree.
1206 */
1207 STATIC int /* error */
xfs_alloc_ag_vextent_small(struct xfs_alloc_arg * args,struct xfs_btree_cur * ccur,xfs_agblock_t * fbnop,xfs_extlen_t * flenp,int * stat)1208 xfs_alloc_ag_vextent_small(
1209 struct xfs_alloc_arg *args, /* allocation argument structure */
1210 struct xfs_btree_cur *ccur, /* optional by-size cursor */
1211 xfs_agblock_t *fbnop, /* result block number */
1212 xfs_extlen_t *flenp, /* result length */
1213 int *stat) /* status: 0-freelist, 1-normal/none */
1214 {
1215 struct xfs_agf *agf = args->agbp->b_addr;
1216 int error = 0;
1217 xfs_agblock_t fbno = NULLAGBLOCK;
1218 xfs_extlen_t flen = 0;
1219 int i = 0;
1220
1221 /*
1222 * If a cntbt cursor is provided, try to allocate the largest record in
1223 * the tree. Try the AGFL if the cntbt is empty, otherwise fail the
1224 * allocation. Make sure to respect minleft even when pulling from the
1225 * freelist.
1226 */
1227 if (ccur)
1228 error = xfs_btree_decrement(ccur, 0, &i);
1229 if (error)
1230 goto error;
1231 if (i) {
1232 error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i);
1233 if (error)
1234 goto error;
1235 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1236 xfs_btree_mark_sick(ccur);
1237 error = -EFSCORRUPTED;
1238 goto error;
1239 }
1240 goto out;
1241 }
1242
1243 if (args->minlen != 1 || args->alignment != 1 ||
1244 args->resv == XFS_AG_RESV_AGFL ||
1245 be32_to_cpu(agf->agf_flcount) <= args->minleft)
1246 goto out;
1247
1248 error = xfs_alloc_get_freelist(args->pag, args->tp, args->agbp,
1249 &fbno, 0);
1250 if (error)
1251 goto error;
1252 if (fbno == NULLAGBLOCK)
1253 goto out;
1254
1255 xfs_extent_busy_reuse(pag_group(args->pag), fbno, 1,
1256 (args->datatype & XFS_ALLOC_NOBUSY));
1257
1258 if (args->datatype & XFS_ALLOC_USERDATA) {
1259 struct xfs_buf *bp;
1260
1261 error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp,
1262 xfs_agbno_to_daddr(args->pag, fbno),
1263 args->mp->m_bsize, 0, &bp);
1264 if (error)
1265 goto error;
1266 xfs_trans_binval(args->tp, bp);
1267 }
1268 *fbnop = args->agbno = fbno;
1269 *flenp = args->len = 1;
1270 if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) {
1271 xfs_btree_mark_sick(ccur);
1272 error = -EFSCORRUPTED;
1273 goto error;
1274 }
1275 args->wasfromfl = 1;
1276 trace_xfs_alloc_small_freelist(args);
1277
1278 /*
1279 * If we're feeding an AGFL block to something that doesn't live in the
1280 * free space, we need to clear out the OWN_AG rmap.
1281 */
1282 error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1,
1283 &XFS_RMAP_OINFO_AG);
1284 if (error)
1285 goto error;
1286
1287 *stat = 0;
1288 return 0;
1289
1290 out:
1291 /*
1292 * Can't do the allocation, give up.
1293 */
1294 if (flen < args->minlen) {
1295 args->agbno = NULLAGBLOCK;
1296 trace_xfs_alloc_small_notenough(args);
1297 flen = 0;
1298 }
1299 *fbnop = fbno;
1300 *flenp = flen;
1301 *stat = 1;
1302 trace_xfs_alloc_small_done(args);
1303 return 0;
1304
1305 error:
1306 trace_xfs_alloc_small_error(args);
1307 return error;
1308 }
1309
1310 /*
1311 * Allocate a variable extent at exactly agno/bno.
1312 * Extent's length (returned in *len) will be between minlen and maxlen,
1313 * and of the form k * prod + mod unless there's nothing that large.
1314 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
1315 */
1316 STATIC int /* error */
xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t * args)1317 xfs_alloc_ag_vextent_exact(
1318 xfs_alloc_arg_t *args) /* allocation argument structure */
1319 {
1320 struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */
1321 struct xfs_btree_cur *cnt_cur;/* by count btree cursor */
1322 int error;
1323 xfs_agblock_t fbno; /* start block of found extent */
1324 xfs_extlen_t flen; /* length of found extent */
1325 xfs_agblock_t tbno; /* start block of busy extent */
1326 xfs_extlen_t tlen; /* length of busy extent */
1327 xfs_agblock_t tend; /* end block of busy extent */
1328 int i; /* success/failure of operation */
1329 unsigned busy_gen;
1330
1331 ASSERT(args->alignment == 1);
1332
1333 /*
1334 * Allocate/initialize a cursor for the by-number freespace btree.
1335 */
1336 bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp,
1337 args->pag);
1338
1339 /*
1340 * Lookup bno and minlen in the btree (minlen is irrelevant, really).
1341 * Look for the closest free block <= bno, it must contain bno
1342 * if any free block does.
1343 */
1344 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
1345 if (error)
1346 goto error0;
1347 if (!i)
1348 goto not_found;
1349
1350 /*
1351 * Grab the freespace record.
1352 */
1353 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
1354 if (error)
1355 goto error0;
1356 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1357 xfs_btree_mark_sick(bno_cur);
1358 error = -EFSCORRUPTED;
1359 goto error0;
1360 }
1361 ASSERT(fbno <= args->agbno);
1362
1363 /*
1364 * Check for overlapping busy extents.
1365 */
1366 tbno = fbno;
1367 tlen = flen;
1368 xfs_extent_busy_trim(pag_group(args->pag), args->minlen, args->maxlen,
1369 &tbno, &tlen, &busy_gen);
1370
1371 /*
1372 * Give up if the start of the extent is busy, or the freespace isn't
1373 * long enough for the minimum request.
1374 */
1375 if (tbno > args->agbno)
1376 goto not_found;
1377 if (tlen < args->minlen)
1378 goto not_found;
1379 tend = tbno + tlen;
1380 if (tend < args->agbno + args->minlen)
1381 goto not_found;
1382
1383 /*
1384 * End of extent will be smaller of the freespace end and the
1385 * maximal requested end.
1386 *
1387 * Fix the length according to mod and prod if given.
1388 */
1389 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
1390 - args->agbno;
1391 xfs_alloc_fix_len(args);
1392 ASSERT(args->agbno + args->len <= tend);
1393
1394 /*
1395 * We are allocating agbno for args->len
1396 * Allocate/initialize a cursor for the by-size btree.
1397 */
1398 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp,
1399 args->pag);
1400 ASSERT(xfs_verify_agbext(args->pag, args->agbno, args->len));
1401 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
1402 args->len, XFSA_FIXUP_BNO_OK);
1403 if (error) {
1404 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1405 goto error0;
1406 }
1407
1408 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1409 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1410
1411 args->wasfromfl = 0;
1412 trace_xfs_alloc_exact_done(args);
1413 return 0;
1414
1415 not_found:
1416 /* Didn't find it, return null. */
1417 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1418 args->agbno = NULLAGBLOCK;
1419 trace_xfs_alloc_exact_notfound(args);
1420 return 0;
1421
1422 error0:
1423 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1424 trace_xfs_alloc_exact_error(args);
1425 return error;
1426 }
1427
1428 /*
1429 * Search a given number of btree records in a given direction. Check each
1430 * record against the good extent we've already found.
1431 */
1432 STATIC int
xfs_alloc_walk_iter(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur,struct xfs_btree_cur * cur,bool increment,bool find_one,int count,int * stat)1433 xfs_alloc_walk_iter(
1434 struct xfs_alloc_arg *args,
1435 struct xfs_alloc_cur *acur,
1436 struct xfs_btree_cur *cur,
1437 bool increment,
1438 bool find_one, /* quit on first candidate */
1439 int count, /* rec count (-1 for infinite) */
1440 int *stat)
1441 {
1442 int error;
1443 int i;
1444
1445 *stat = 0;
1446
1447 /*
1448 * Search so long as the cursor is active or we find a better extent.
1449 * The cursor is deactivated if it extends beyond the range of the
1450 * current allocation candidate.
1451 */
1452 while (xfs_alloc_cur_active(cur) && count) {
1453 error = xfs_alloc_cur_check(args, acur, cur, &i);
1454 if (error)
1455 return error;
1456 if (i == 1) {
1457 *stat = 1;
1458 if (find_one)
1459 break;
1460 }
1461 if (!xfs_alloc_cur_active(cur))
1462 break;
1463
1464 if (increment)
1465 error = xfs_btree_increment(cur, 0, &i);
1466 else
1467 error = xfs_btree_decrement(cur, 0, &i);
1468 if (error)
1469 return error;
1470 if (i == 0)
1471 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE;
1472
1473 if (count > 0)
1474 count--;
1475 }
1476
1477 return 0;
1478 }
1479
1480 /*
1481 * Search the by-bno and by-size btrees in parallel in search of an extent with
1482 * ideal locality based on the NEAR mode ->agbno locality hint.
1483 */
1484 STATIC int
xfs_alloc_ag_vextent_locality(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur,int * stat)1485 xfs_alloc_ag_vextent_locality(
1486 struct xfs_alloc_arg *args,
1487 struct xfs_alloc_cur *acur,
1488 int *stat)
1489 {
1490 struct xfs_btree_cur *fbcur = NULL;
1491 int error;
1492 int i;
1493 bool fbinc;
1494
1495 ASSERT(acur->len == 0);
1496
1497 *stat = 0;
1498
1499 error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i);
1500 if (error)
1501 return error;
1502 error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i);
1503 if (error)
1504 return error;
1505 error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i);
1506 if (error)
1507 return error;
1508
1509 /*
1510 * Search the bnobt and cntbt in parallel. Search the bnobt left and
1511 * right and lookup the closest extent to the locality hint for each
1512 * extent size key in the cntbt. The entire search terminates
1513 * immediately on a bnobt hit because that means we've found best case
1514 * locality. Otherwise the search continues until the cntbt cursor runs
1515 * off the end of the tree. If no allocation candidate is found at this
1516 * point, give up on locality, walk backwards from the end of the cntbt
1517 * and take the first available extent.
1518 *
1519 * The parallel tree searches balance each other out to provide fairly
1520 * consistent performance for various situations. The bnobt search can
1521 * have pathological behavior in the worst case scenario of larger
1522 * allocation requests and fragmented free space. On the other hand, the
1523 * bnobt is able to satisfy most smaller allocation requests much more
1524 * quickly than the cntbt. The cntbt search can sift through fragmented
1525 * free space and sets of free extents for larger allocation requests
1526 * more quickly than the bnobt. Since the locality hint is just a hint
1527 * and we don't want to scan the entire bnobt for perfect locality, the
1528 * cntbt search essentially bounds the bnobt search such that we can
1529 * find good enough locality at reasonable performance in most cases.
1530 */
1531 while (xfs_alloc_cur_active(acur->bnolt) ||
1532 xfs_alloc_cur_active(acur->bnogt) ||
1533 xfs_alloc_cur_active(acur->cnt)) {
1534
1535 trace_xfs_alloc_cur_lookup(args);
1536
1537 /*
1538 * Search the bnobt left and right. In the case of a hit, finish
1539 * the search in the opposite direction and we're done.
1540 */
1541 error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false,
1542 true, 1, &i);
1543 if (error)
1544 return error;
1545 if (i == 1) {
1546 trace_xfs_alloc_cur_left(args);
1547 fbcur = acur->bnogt;
1548 fbinc = true;
1549 break;
1550 }
1551 error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true,
1552 1, &i);
1553 if (error)
1554 return error;
1555 if (i == 1) {
1556 trace_xfs_alloc_cur_right(args);
1557 fbcur = acur->bnolt;
1558 fbinc = false;
1559 break;
1560 }
1561
1562 /*
1563 * Check the extent with best locality based on the current
1564 * extent size search key and keep track of the best candidate.
1565 */
1566 error = xfs_alloc_cntbt_iter(args, acur);
1567 if (error)
1568 return error;
1569 if (!xfs_alloc_cur_active(acur->cnt)) {
1570 trace_xfs_alloc_cur_lookup_done(args);
1571 break;
1572 }
1573 }
1574
1575 /*
1576 * If we failed to find anything due to busy extents, return empty
1577 * handed so the caller can flush and retry. If no busy extents were
1578 * found, walk backwards from the end of the cntbt as a last resort.
1579 */
1580 if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) {
1581 error = xfs_btree_decrement(acur->cnt, 0, &i);
1582 if (error)
1583 return error;
1584 if (i) {
1585 acur->cnt->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE;
1586 fbcur = acur->cnt;
1587 fbinc = false;
1588 }
1589 }
1590
1591 /*
1592 * Search in the opposite direction for a better entry in the case of
1593 * a bnobt hit or walk backwards from the end of the cntbt.
1594 */
1595 if (fbcur) {
1596 error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1,
1597 &i);
1598 if (error)
1599 return error;
1600 }
1601
1602 if (acur->len)
1603 *stat = 1;
1604
1605 return 0;
1606 }
1607
1608 /* Check the last block of the cnt btree for allocations. */
1609 static int
xfs_alloc_ag_vextent_lastblock(struct xfs_alloc_arg * args,struct xfs_alloc_cur * acur,xfs_agblock_t * bno,xfs_extlen_t * len,bool * allocated)1610 xfs_alloc_ag_vextent_lastblock(
1611 struct xfs_alloc_arg *args,
1612 struct xfs_alloc_cur *acur,
1613 xfs_agblock_t *bno,
1614 xfs_extlen_t *len,
1615 bool *allocated)
1616 {
1617 int error;
1618 int i;
1619
1620 #ifdef DEBUG
1621 /* Randomly don't execute the first algorithm. */
1622 if (get_random_u32_below(2))
1623 return 0;
1624 #endif
1625
1626 /*
1627 * Start from the entry that lookup found, sequence through all larger
1628 * free blocks. If we're actually pointing at a record smaller than
1629 * maxlen, go to the start of this block, and skip all those smaller
1630 * than minlen.
1631 */
1632 if (*len || args->alignment > 1) {
1633 acur->cnt->bc_levels[0].ptr = 1;
1634 do {
1635 error = xfs_alloc_get_rec(acur->cnt, bno, len, &i);
1636 if (error)
1637 return error;
1638 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1639 xfs_btree_mark_sick(acur->cnt);
1640 return -EFSCORRUPTED;
1641 }
1642 if (*len >= args->minlen)
1643 break;
1644 error = xfs_btree_increment(acur->cnt, 0, &i);
1645 if (error)
1646 return error;
1647 } while (i);
1648 ASSERT(*len >= args->minlen);
1649 if (!i)
1650 return 0;
1651 }
1652
1653 error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i);
1654 if (error)
1655 return error;
1656
1657 /*
1658 * It didn't work. We COULD be in a case where there's a good record
1659 * somewhere, so try again.
1660 */
1661 if (acur->len == 0)
1662 return 0;
1663
1664 trace_xfs_alloc_near_first(args);
1665 *allocated = true;
1666 return 0;
1667 }
1668
1669 /*
1670 * Allocate a variable extent near bno in the allocation group agno.
1671 * Extent's length (returned in len) will be between minlen and maxlen,
1672 * and of the form k * prod + mod unless there's nothing that large.
1673 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1674 */
1675 STATIC int
xfs_alloc_ag_vextent_near(struct xfs_alloc_arg * args,uint32_t alloc_flags)1676 xfs_alloc_ag_vextent_near(
1677 struct xfs_alloc_arg *args,
1678 uint32_t alloc_flags)
1679 {
1680 struct xfs_alloc_cur acur = {};
1681 int error; /* error code */
1682 int i; /* result code, temporary */
1683 xfs_agblock_t bno;
1684 xfs_extlen_t len;
1685
1686 /* handle uninitialized agbno range so caller doesn't have to */
1687 if (!args->min_agbno && !args->max_agbno)
1688 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1689 ASSERT(args->min_agbno <= args->max_agbno);
1690
1691 /* clamp agbno to the range if it's outside */
1692 if (args->agbno < args->min_agbno)
1693 args->agbno = args->min_agbno;
1694 if (args->agbno > args->max_agbno)
1695 args->agbno = args->max_agbno;
1696
1697 /* Retry once quickly if we find busy extents before blocking. */
1698 alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH;
1699 restart:
1700 len = 0;
1701
1702 /*
1703 * Set up cursors and see if there are any free extents as big as
1704 * maxlen. If not, pick the last entry in the tree unless the tree is
1705 * empty.
1706 */
1707 error = xfs_alloc_cur_setup(args, &acur);
1708 if (error == -ENOSPC) {
1709 error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno,
1710 &len, &i);
1711 if (error)
1712 goto out;
1713 if (i == 0 || len == 0) {
1714 trace_xfs_alloc_near_noentry(args);
1715 goto out;
1716 }
1717 ASSERT(i == 1);
1718 } else if (error) {
1719 goto out;
1720 }
1721
1722 /*
1723 * First algorithm.
1724 * If the requested extent is large wrt the freespaces available
1725 * in this a.g., then the cursor will be pointing to a btree entry
1726 * near the right edge of the tree. If it's in the last btree leaf
1727 * block, then we just examine all the entries in that block
1728 * that are big enough, and pick the best one.
1729 */
1730 if (xfs_btree_islastblock(acur.cnt, 0)) {
1731 bool allocated = false;
1732
1733 error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len,
1734 &allocated);
1735 if (error)
1736 goto out;
1737 if (allocated)
1738 goto alloc_finish;
1739 }
1740
1741 /*
1742 * Second algorithm. Combined cntbt and bnobt search to find ideal
1743 * locality.
1744 */
1745 error = xfs_alloc_ag_vextent_locality(args, &acur, &i);
1746 if (error)
1747 goto out;
1748
1749 /*
1750 * If we couldn't get anything, give up.
1751 */
1752 if (!acur.len) {
1753 if (acur.busy) {
1754 /*
1755 * Our only valid extents must have been busy. Flush and
1756 * retry the allocation again. If we get an -EAGAIN
1757 * error, we're being told that a deadlock was avoided
1758 * and the current transaction needs committing before
1759 * the allocation can be retried.
1760 */
1761 trace_xfs_alloc_near_busy(args);
1762 error = xfs_extent_busy_flush(args->tp,
1763 pag_group(args->pag), acur.busy_gen,
1764 alloc_flags);
1765 if (error)
1766 goto out;
1767
1768 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
1769 goto restart;
1770 }
1771 trace_xfs_alloc_size_neither(args);
1772 args->agbno = NULLAGBLOCK;
1773 goto out;
1774 }
1775
1776 alloc_finish:
1777 /* fix up btrees on a successful allocation */
1778 error = xfs_alloc_cur_finish(args, &acur);
1779
1780 out:
1781 xfs_alloc_cur_close(&acur, error);
1782 return error;
1783 }
1784
1785 /*
1786 * Allocate a variable extent anywhere in the allocation group agno.
1787 * Extent's length (returned in len) will be between minlen and maxlen,
1788 * and of the form k * prod + mod unless there's nothing that large.
1789 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
1790 */
1791 static int
xfs_alloc_ag_vextent_size(struct xfs_alloc_arg * args,uint32_t alloc_flags)1792 xfs_alloc_ag_vextent_size(
1793 struct xfs_alloc_arg *args,
1794 uint32_t alloc_flags)
1795 {
1796 struct xfs_agf *agf = args->agbp->b_addr;
1797 struct xfs_btree_cur *bno_cur;
1798 struct xfs_btree_cur *cnt_cur;
1799 xfs_agblock_t fbno; /* start of found freespace */
1800 xfs_extlen_t flen; /* length of found freespace */
1801 xfs_agblock_t rbno; /* returned block number */
1802 xfs_extlen_t rlen; /* length of returned extent */
1803 bool busy;
1804 unsigned busy_gen;
1805 int error;
1806 int i;
1807
1808 /* Retry once quickly if we find busy extents before blocking. */
1809 alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH;
1810 restart:
1811 /*
1812 * Allocate and initialize a cursor for the by-size btree.
1813 */
1814 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp,
1815 args->pag);
1816 bno_cur = NULL;
1817
1818 /*
1819 * Look for an entry >= maxlen+alignment-1 blocks.
1820 */
1821 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1822 args->maxlen + args->alignment - 1, &i)))
1823 goto error0;
1824
1825 /*
1826 * If none then we have to settle for a smaller extent. In the case that
1827 * there are no large extents, this will return the last entry in the
1828 * tree unless the tree is empty. In the case that there are only busy
1829 * large extents, this will return the largest small extent unless there
1830 * are no smaller extents available.
1831 */
1832 if (!i) {
1833 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1834 &fbno, &flen, &i);
1835 if (error)
1836 goto error0;
1837 if (i == 0 || flen == 0) {
1838 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1839 trace_xfs_alloc_size_noentry(args);
1840 return 0;
1841 }
1842 ASSERT(i == 1);
1843 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
1844 &rlen, &busy_gen);
1845 } else {
1846 /*
1847 * Search for a non-busy extent that is large enough.
1848 */
1849 for (;;) {
1850 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1851 if (error)
1852 goto error0;
1853 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1854 xfs_btree_mark_sick(cnt_cur);
1855 error = -EFSCORRUPTED;
1856 goto error0;
1857 }
1858
1859 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1860 &rbno, &rlen, &busy_gen);
1861
1862 if (rlen >= args->maxlen)
1863 break;
1864
1865 error = xfs_btree_increment(cnt_cur, 0, &i);
1866 if (error)
1867 goto error0;
1868 if (i)
1869 continue;
1870
1871 /*
1872 * Our only valid extents must have been busy. Flush and
1873 * retry the allocation again. If we get an -EAGAIN
1874 * error, we're being told that a deadlock was avoided
1875 * and the current transaction needs committing before
1876 * the allocation can be retried.
1877 */
1878 trace_xfs_alloc_size_busy(args);
1879 error = xfs_extent_busy_flush(args->tp,
1880 pag_group(args->pag), busy_gen,
1881 alloc_flags);
1882 if (error)
1883 goto error0;
1884
1885 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
1886 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1887 goto restart;
1888 }
1889 }
1890
1891 /*
1892 * In the first case above, we got the last entry in the
1893 * by-size btree. Now we check to see if the space hits maxlen
1894 * once aligned; if not, we search left for something better.
1895 * This can't happen in the second case above.
1896 */
1897 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1898 if (XFS_IS_CORRUPT(args->mp,
1899 rlen != 0 &&
1900 (rlen > flen ||
1901 rbno + rlen > fbno + flen))) {
1902 xfs_btree_mark_sick(cnt_cur);
1903 error = -EFSCORRUPTED;
1904 goto error0;
1905 }
1906 if (rlen < args->maxlen) {
1907 xfs_agblock_t bestfbno;
1908 xfs_extlen_t bestflen;
1909 xfs_agblock_t bestrbno;
1910 xfs_extlen_t bestrlen;
1911
1912 bestrlen = rlen;
1913 bestrbno = rbno;
1914 bestflen = flen;
1915 bestfbno = fbno;
1916 for (;;) {
1917 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1918 goto error0;
1919 if (i == 0)
1920 break;
1921 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1922 &i)))
1923 goto error0;
1924 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1925 xfs_btree_mark_sick(cnt_cur);
1926 error = -EFSCORRUPTED;
1927 goto error0;
1928 }
1929 if (flen <= bestrlen)
1930 break;
1931 busy = xfs_alloc_compute_aligned(args, fbno, flen,
1932 &rbno, &rlen, &busy_gen);
1933 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1934 if (XFS_IS_CORRUPT(args->mp,
1935 rlen != 0 &&
1936 (rlen > flen ||
1937 rbno + rlen > fbno + flen))) {
1938 xfs_btree_mark_sick(cnt_cur);
1939 error = -EFSCORRUPTED;
1940 goto error0;
1941 }
1942 if (rlen > bestrlen) {
1943 bestrlen = rlen;
1944 bestrbno = rbno;
1945 bestflen = flen;
1946 bestfbno = fbno;
1947 if (rlen == args->maxlen)
1948 break;
1949 }
1950 }
1951 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1952 &i)))
1953 goto error0;
1954 if (XFS_IS_CORRUPT(args->mp, i != 1)) {
1955 xfs_btree_mark_sick(cnt_cur);
1956 error = -EFSCORRUPTED;
1957 goto error0;
1958 }
1959 rlen = bestrlen;
1960 rbno = bestrbno;
1961 flen = bestflen;
1962 fbno = bestfbno;
1963 }
1964 args->wasfromfl = 0;
1965 /*
1966 * Fix up the length.
1967 */
1968 args->len = rlen;
1969 if (rlen < args->minlen) {
1970 if (busy) {
1971 /*
1972 * Our only valid extents must have been busy. Flush and
1973 * retry the allocation again. If we get an -EAGAIN
1974 * error, we're being told that a deadlock was avoided
1975 * and the current transaction needs committing before
1976 * the allocation can be retried.
1977 */
1978 trace_xfs_alloc_size_busy(args);
1979 error = xfs_extent_busy_flush(args->tp,
1980 pag_group(args->pag), busy_gen,
1981 alloc_flags);
1982 if (error)
1983 goto error0;
1984
1985 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH;
1986 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1987 goto restart;
1988 }
1989 goto out_nominleft;
1990 }
1991 xfs_alloc_fix_len(args);
1992
1993 rlen = args->len;
1994 if (XFS_IS_CORRUPT(args->mp, rlen > flen)) {
1995 xfs_btree_mark_sick(cnt_cur);
1996 error = -EFSCORRUPTED;
1997 goto error0;
1998 }
1999 /*
2000 * Allocate and initialize a cursor for the by-block tree.
2001 */
2002 bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp,
2003 args->pag);
2004 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
2005 rbno, rlen, XFSA_FIXUP_CNT_OK)))
2006 goto error0;
2007 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2008 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
2009 cnt_cur = bno_cur = NULL;
2010 args->len = rlen;
2011 args->agbno = rbno;
2012 if (XFS_IS_CORRUPT(args->mp,
2013 args->agbno + args->len >
2014 be32_to_cpu(agf->agf_length))) {
2015 xfs_ag_mark_sick(args->pag, XFS_SICK_AG_BNOBT);
2016 error = -EFSCORRUPTED;
2017 goto error0;
2018 }
2019 trace_xfs_alloc_size_done(args);
2020 return 0;
2021
2022 error0:
2023 trace_xfs_alloc_size_error(args);
2024 if (cnt_cur)
2025 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
2026 if (bno_cur)
2027 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
2028 return error;
2029
2030 out_nominleft:
2031 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2032 trace_xfs_alloc_size_nominleft(args);
2033 args->agbno = NULLAGBLOCK;
2034 return 0;
2035 }
2036
2037 /*
2038 * Free the extent starting at agno/bno for length.
2039 */
2040 int
xfs_free_ag_extent(struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agblock_t bno,xfs_extlen_t len,const struct xfs_owner_info * oinfo,enum xfs_ag_resv_type type)2041 xfs_free_ag_extent(
2042 struct xfs_trans *tp,
2043 struct xfs_buf *agbp,
2044 xfs_agblock_t bno,
2045 xfs_extlen_t len,
2046 const struct xfs_owner_info *oinfo,
2047 enum xfs_ag_resv_type type)
2048 {
2049 struct xfs_mount *mp;
2050 struct xfs_btree_cur *bno_cur;
2051 struct xfs_btree_cur *cnt_cur;
2052 xfs_agblock_t gtbno; /* start of right neighbor */
2053 xfs_extlen_t gtlen; /* length of right neighbor */
2054 xfs_agblock_t ltbno; /* start of left neighbor */
2055 xfs_extlen_t ltlen; /* length of left neighbor */
2056 xfs_agblock_t nbno; /* new starting block of freesp */
2057 xfs_extlen_t nlen; /* new length of freespace */
2058 int haveleft; /* have a left neighbor */
2059 int haveright; /* have a right neighbor */
2060 int i;
2061 int error;
2062 struct xfs_perag *pag = agbp->b_pag;
2063 bool fixup_longest = false;
2064
2065 bno_cur = cnt_cur = NULL;
2066 mp = tp->t_mountp;
2067
2068 if (!xfs_rmap_should_skip_owner_update(oinfo)) {
2069 error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo);
2070 if (error)
2071 goto error0;
2072 }
2073
2074 /*
2075 * Allocate and initialize a cursor for the by-block btree.
2076 */
2077 bno_cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
2078 /*
2079 * Look for a neighboring block on the left (lower block numbers)
2080 * that is contiguous with this space.
2081 */
2082 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
2083 goto error0;
2084 if (haveleft) {
2085 /*
2086 * There is a block to our left.
2087 */
2088 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
2089 goto error0;
2090 if (XFS_IS_CORRUPT(mp, i != 1)) {
2091 xfs_btree_mark_sick(bno_cur);
2092 error = -EFSCORRUPTED;
2093 goto error0;
2094 }
2095 /*
2096 * It's not contiguous, though.
2097 */
2098 if (ltbno + ltlen < bno)
2099 haveleft = 0;
2100 else {
2101 /*
2102 * If this failure happens the request to free this
2103 * space was invalid, it's (partly) already free.
2104 * Very bad.
2105 */
2106 if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) {
2107 xfs_btree_mark_sick(bno_cur);
2108 error = -EFSCORRUPTED;
2109 goto error0;
2110 }
2111 }
2112 }
2113 /*
2114 * Look for a neighboring block on the right (higher block numbers)
2115 * that is contiguous with this space.
2116 */
2117 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
2118 goto error0;
2119 if (haveright) {
2120 /*
2121 * There is a block to our right.
2122 */
2123 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
2124 goto error0;
2125 if (XFS_IS_CORRUPT(mp, i != 1)) {
2126 xfs_btree_mark_sick(bno_cur);
2127 error = -EFSCORRUPTED;
2128 goto error0;
2129 }
2130 /*
2131 * It's not contiguous, though.
2132 */
2133 if (bno + len < gtbno)
2134 haveright = 0;
2135 else {
2136 /*
2137 * If this failure happens the request to free this
2138 * space was invalid, it's (partly) already free.
2139 * Very bad.
2140 */
2141 if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) {
2142 xfs_btree_mark_sick(bno_cur);
2143 error = -EFSCORRUPTED;
2144 goto error0;
2145 }
2146 }
2147 }
2148 /*
2149 * Now allocate and initialize a cursor for the by-size tree.
2150 */
2151 cnt_cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag);
2152 /*
2153 * Have both left and right contiguous neighbors.
2154 * Merge all three into a single free block.
2155 */
2156 if (haveleft && haveright) {
2157 /*
2158 * Delete the old by-size entry on the left.
2159 */
2160 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2161 goto error0;
2162 if (XFS_IS_CORRUPT(mp, i != 1)) {
2163 xfs_btree_mark_sick(cnt_cur);
2164 error = -EFSCORRUPTED;
2165 goto error0;
2166 }
2167 if ((error = xfs_btree_delete(cnt_cur, &i)))
2168 goto error0;
2169 if (XFS_IS_CORRUPT(mp, i != 1)) {
2170 xfs_btree_mark_sick(cnt_cur);
2171 error = -EFSCORRUPTED;
2172 goto error0;
2173 }
2174 /*
2175 * Delete the old by-size entry on the right.
2176 */
2177 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2178 goto error0;
2179 if (XFS_IS_CORRUPT(mp, i != 1)) {
2180 xfs_btree_mark_sick(cnt_cur);
2181 error = -EFSCORRUPTED;
2182 goto error0;
2183 }
2184 if ((error = xfs_btree_delete(cnt_cur, &i)))
2185 goto error0;
2186 if (XFS_IS_CORRUPT(mp, i != 1)) {
2187 xfs_btree_mark_sick(cnt_cur);
2188 error = -EFSCORRUPTED;
2189 goto error0;
2190 }
2191 /*
2192 * Delete the old by-block entry for the right block.
2193 */
2194 if ((error = xfs_btree_delete(bno_cur, &i)))
2195 goto error0;
2196 if (XFS_IS_CORRUPT(mp, i != 1)) {
2197 xfs_btree_mark_sick(bno_cur);
2198 error = -EFSCORRUPTED;
2199 goto error0;
2200 }
2201 /*
2202 * Move the by-block cursor back to the left neighbor.
2203 */
2204 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
2205 goto error0;
2206 if (XFS_IS_CORRUPT(mp, i != 1)) {
2207 xfs_btree_mark_sick(bno_cur);
2208 error = -EFSCORRUPTED;
2209 goto error0;
2210 }
2211 #ifdef DEBUG
2212 /*
2213 * Check that this is the right record: delete didn't
2214 * mangle the cursor.
2215 */
2216 {
2217 xfs_agblock_t xxbno;
2218 xfs_extlen_t xxlen;
2219
2220 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
2221 &i)))
2222 goto error0;
2223 if (XFS_IS_CORRUPT(mp,
2224 i != 1 ||
2225 xxbno != ltbno ||
2226 xxlen != ltlen)) {
2227 xfs_btree_mark_sick(bno_cur);
2228 error = -EFSCORRUPTED;
2229 goto error0;
2230 }
2231 }
2232 #endif
2233 /*
2234 * Update remaining by-block entry to the new, joined block.
2235 */
2236 nbno = ltbno;
2237 nlen = len + ltlen + gtlen;
2238 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2239 goto error0;
2240 }
2241 /*
2242 * Have only a left contiguous neighbor.
2243 * Merge it together with the new freespace.
2244 */
2245 else if (haveleft) {
2246 /*
2247 * Delete the old by-size entry on the left.
2248 */
2249 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
2250 goto error0;
2251 if (XFS_IS_CORRUPT(mp, i != 1)) {
2252 xfs_btree_mark_sick(cnt_cur);
2253 error = -EFSCORRUPTED;
2254 goto error0;
2255 }
2256 if ((error = xfs_btree_delete(cnt_cur, &i)))
2257 goto error0;
2258 if (XFS_IS_CORRUPT(mp, i != 1)) {
2259 xfs_btree_mark_sick(cnt_cur);
2260 error = -EFSCORRUPTED;
2261 goto error0;
2262 }
2263 /*
2264 * Back up the by-block cursor to the left neighbor, and
2265 * update its length.
2266 */
2267 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
2268 goto error0;
2269 if (XFS_IS_CORRUPT(mp, i != 1)) {
2270 xfs_btree_mark_sick(bno_cur);
2271 error = -EFSCORRUPTED;
2272 goto error0;
2273 }
2274 nbno = ltbno;
2275 nlen = len + ltlen;
2276 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2277 goto error0;
2278 }
2279 /*
2280 * Have only a right contiguous neighbor.
2281 * Merge it together with the new freespace.
2282 */
2283 else if (haveright) {
2284 /*
2285 * Delete the old by-size entry on the right.
2286 */
2287 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
2288 goto error0;
2289 if (XFS_IS_CORRUPT(mp, i != 1)) {
2290 xfs_btree_mark_sick(cnt_cur);
2291 error = -EFSCORRUPTED;
2292 goto error0;
2293 }
2294 if ((error = xfs_btree_delete(cnt_cur, &i)))
2295 goto error0;
2296 if (XFS_IS_CORRUPT(mp, i != 1)) {
2297 xfs_btree_mark_sick(cnt_cur);
2298 error = -EFSCORRUPTED;
2299 goto error0;
2300 }
2301 /*
2302 * Update the starting block and length of the right
2303 * neighbor in the by-block tree.
2304 */
2305 nbno = bno;
2306 nlen = len + gtlen;
2307 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
2308 goto error0;
2309 }
2310 /*
2311 * No contiguous neighbors.
2312 * Insert the new freespace into the by-block tree.
2313 */
2314 else {
2315 nbno = bno;
2316 nlen = len;
2317 if ((error = xfs_btree_insert(bno_cur, &i)))
2318 goto error0;
2319 if (XFS_IS_CORRUPT(mp, i != 1)) {
2320 xfs_btree_mark_sick(bno_cur);
2321 error = -EFSCORRUPTED;
2322 goto error0;
2323 }
2324 }
2325 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
2326 bno_cur = NULL;
2327
2328 /*
2329 * In all cases we need to insert the new freespace in the by-size tree.
2330 *
2331 * If this new freespace is being inserted in the block that contains
2332 * the largest free space in the btree, make sure we also fix up the
2333 * agf->agf-longest tracker field.
2334 */
2335 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
2336 goto error0;
2337 if (XFS_IS_CORRUPT(mp, i != 0)) {
2338 xfs_btree_mark_sick(cnt_cur);
2339 error = -EFSCORRUPTED;
2340 goto error0;
2341 }
2342 if (xfs_alloc_cursor_at_lastrec(cnt_cur))
2343 fixup_longest = true;
2344 if ((error = xfs_btree_insert(cnt_cur, &i)))
2345 goto error0;
2346 if (XFS_IS_CORRUPT(mp, i != 1)) {
2347 xfs_btree_mark_sick(cnt_cur);
2348 error = -EFSCORRUPTED;
2349 goto error0;
2350 }
2351 if (fixup_longest) {
2352 error = xfs_alloc_fixup_longest(cnt_cur);
2353 if (error)
2354 goto error0;
2355 }
2356
2357 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
2358 cnt_cur = NULL;
2359
2360 /*
2361 * Update the freespace totals in the ag and superblock.
2362 */
2363 error = xfs_alloc_update_counters(tp, agbp, len);
2364 xfs_ag_resv_free_extent(pag, type, tp, len);
2365 if (error)
2366 goto error0;
2367
2368 XFS_STATS_INC(mp, xs_freex);
2369 XFS_STATS_ADD(mp, xs_freeb, len);
2370
2371 trace_xfs_free_extent(pag, bno, len, type, haveleft, haveright);
2372
2373 return 0;
2374
2375 error0:
2376 trace_xfs_free_extent(pag, bno, len, type, -1, -1);
2377 if (bno_cur)
2378 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
2379 if (cnt_cur)
2380 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
2381 return error;
2382 }
2383
2384 /*
2385 * Visible (exported) allocation/free functions.
2386 * Some of these are used just by xfs_alloc_btree.c and this file.
2387 */
2388
2389 /*
2390 * Compute and fill in value of m_alloc_maxlevels.
2391 */
2392 void
xfs_alloc_compute_maxlevels(xfs_mount_t * mp)2393 xfs_alloc_compute_maxlevels(
2394 xfs_mount_t *mp) /* file system mount structure */
2395 {
2396 mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
2397 (mp->m_sb.sb_agblocks + 1) / 2);
2398 ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
2399 }
2400
2401 /*
2402 * Find the length of the longest extent in an AG. The 'need' parameter
2403 * specifies how much space we're going to need for the AGFL and the
2404 * 'reserved' parameter tells us how many blocks in this AG are reserved for
2405 * other callers.
2406 */
2407 xfs_extlen_t
xfs_alloc_longest_free_extent(struct xfs_perag * pag,xfs_extlen_t need,xfs_extlen_t reserved)2408 xfs_alloc_longest_free_extent(
2409 struct xfs_perag *pag,
2410 xfs_extlen_t need,
2411 xfs_extlen_t reserved)
2412 {
2413 xfs_extlen_t delta = 0;
2414
2415 /*
2416 * If the AGFL needs a recharge, we'll have to subtract that from the
2417 * longest extent.
2418 */
2419 if (need > pag->pagf_flcount)
2420 delta = need - pag->pagf_flcount;
2421
2422 /*
2423 * If we cannot maintain others' reservations with space from the
2424 * not-longest freesp extents, we'll have to subtract /that/ from
2425 * the longest extent too.
2426 */
2427 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2428 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2429
2430 /*
2431 * If the longest extent is long enough to satisfy all the
2432 * reservations and AGFL rules in place, we can return this extent.
2433 */
2434 if (pag->pagf_longest > delta)
2435 return min_t(xfs_extlen_t, pag_mount(pag)->m_ag_max_usable,
2436 pag->pagf_longest - delta);
2437
2438 /* Otherwise, let the caller try for 1 block if there's space. */
2439 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2440 }
2441
2442 /*
2443 * Compute the minimum length of the AGFL in the given AG. If @pag is NULL,
2444 * return the largest possible minimum length.
2445 */
2446 unsigned int
xfs_alloc_min_freelist(struct xfs_mount * mp,struct xfs_perag * pag)2447 xfs_alloc_min_freelist(
2448 struct xfs_mount *mp,
2449 struct xfs_perag *pag)
2450 {
2451 /* AG btrees have at least 1 level. */
2452 const unsigned int bno_level = pag ? pag->pagf_bno_level : 1;
2453 const unsigned int cnt_level = pag ? pag->pagf_cnt_level : 1;
2454 const unsigned int rmap_level = pag ? pag->pagf_rmap_level : 1;
2455 unsigned int min_free;
2456
2457 ASSERT(mp->m_alloc_maxlevels > 0);
2458
2459 /*
2460 * For a btree shorter than the maximum height, the worst case is that
2461 * every level gets split and a new level is added, then while inserting
2462 * another entry to refill the AGFL, every level under the old root gets
2463 * split again. This is:
2464 *
2465 * (full height split reservation) + (AGFL refill split height)
2466 * = (current height + 1) + (current height - 1)
2467 * = (new height) + (new height - 2)
2468 * = 2 * new height - 2
2469 *
2470 * For a btree of maximum height, the worst case is that every level
2471 * under the root gets split, then while inserting another entry to
2472 * refill the AGFL, every level under the root gets split again. This is
2473 * also:
2474 *
2475 * 2 * (current height - 1)
2476 * = 2 * (new height - 1)
2477 * = 2 * new height - 2
2478 */
2479
2480 /* space needed by-bno freespace btree */
2481 min_free = min(bno_level + 1, mp->m_alloc_maxlevels) * 2 - 2;
2482 /* space needed by-size freespace btree */
2483 min_free += min(cnt_level + 1, mp->m_alloc_maxlevels) * 2 - 2;
2484 /* space needed reverse mapping used space btree */
2485 if (xfs_has_rmapbt(mp))
2486 min_free += min(rmap_level + 1, mp->m_rmap_maxlevels) * 2 - 2;
2487 return min_free;
2488 }
2489
2490 /*
2491 * Check if the operation we are fixing up the freelist for should go ahead or
2492 * not. If we are freeing blocks, we always allow it, otherwise the allocation
2493 * is dependent on whether the size and shape of free space available will
2494 * permit the requested allocation to take place.
2495 */
2496 static bool
xfs_alloc_space_available(struct xfs_alloc_arg * args,xfs_extlen_t min_free,int flags)2497 xfs_alloc_space_available(
2498 struct xfs_alloc_arg *args,
2499 xfs_extlen_t min_free,
2500 int flags)
2501 {
2502 struct xfs_perag *pag = args->pag;
2503 xfs_extlen_t alloc_len, longest;
2504 xfs_extlen_t reservation; /* blocks that are still reserved */
2505 int available;
2506 xfs_extlen_t agflcount;
2507
2508 if (flags & XFS_ALLOC_FLAG_FREEING)
2509 return true;
2510
2511 reservation = xfs_ag_resv_needed(pag, args->resv);
2512
2513 /* do we have enough contiguous free space for the allocation? */
2514 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
2515 longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
2516 if (longest < alloc_len)
2517 return false;
2518
2519 /*
2520 * Do we have enough free space remaining for the allocation? Don't
2521 * account extra agfl blocks because we are about to defer free them,
2522 * making them unavailable until the current transaction commits.
2523 */
2524 agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free);
2525 available = (int)(pag->pagf_freeblks + agflcount -
2526 reservation - min_free - args->minleft);
2527 if (available < (int)max(args->total, alloc_len))
2528 return false;
2529
2530 /*
2531 * Clamp maxlen to the amount of free space available for the actual
2532 * extent allocation.
2533 */
2534 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
2535 args->maxlen = available;
2536 ASSERT(args->maxlen > 0);
2537 ASSERT(args->maxlen >= args->minlen);
2538 }
2539
2540 return true;
2541 }
2542
2543 /*
2544 * Check the agfl fields of the agf for inconsistency or corruption.
2545 *
2546 * The original purpose was to detect an agfl header padding mismatch between
2547 * current and early v5 kernels. This problem manifests as a 1-slot size
2548 * difference between the on-disk flcount and the active [first, last] range of
2549 * a wrapped agfl.
2550 *
2551 * However, we need to use these same checks to catch agfl count corruptions
2552 * unrelated to padding. This could occur on any v4 or v5 filesystem, so either
2553 * way, we need to reset the agfl and warn the user.
2554 *
2555 * Return true if a reset is required before the agfl can be used, false
2556 * otherwise.
2557 */
2558 static bool
xfs_agfl_needs_reset(struct xfs_mount * mp,struct xfs_agf * agf)2559 xfs_agfl_needs_reset(
2560 struct xfs_mount *mp,
2561 struct xfs_agf *agf)
2562 {
2563 uint32_t f = be32_to_cpu(agf->agf_flfirst);
2564 uint32_t l = be32_to_cpu(agf->agf_fllast);
2565 uint32_t c = be32_to_cpu(agf->agf_flcount);
2566 int agfl_size = xfs_agfl_size(mp);
2567 int active;
2568
2569 /*
2570 * The agf read verifier catches severe corruption of these fields.
2571 * Repeat some sanity checks to cover a packed -> unpacked mismatch if
2572 * the verifier allows it.
2573 */
2574 if (f >= agfl_size || l >= agfl_size)
2575 return true;
2576 if (c > agfl_size)
2577 return true;
2578
2579 /*
2580 * Check consistency between the on-disk count and the active range. An
2581 * agfl padding mismatch manifests as an inconsistent flcount.
2582 */
2583 if (c && l >= f)
2584 active = l - f + 1;
2585 else if (c)
2586 active = agfl_size - f + l + 1;
2587 else
2588 active = 0;
2589
2590 return active != c;
2591 }
2592
2593 /*
2594 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
2595 * agfl content cannot be trusted. Warn the user that a repair is required to
2596 * recover leaked blocks.
2597 *
2598 * The purpose of this mechanism is to handle filesystems affected by the agfl
2599 * header padding mismatch problem. A reset keeps the filesystem online with a
2600 * relatively minor free space accounting inconsistency rather than suffer the
2601 * inevitable crash from use of an invalid agfl block.
2602 */
2603 static void
xfs_agfl_reset(struct xfs_trans * tp,struct xfs_buf * agbp,struct xfs_perag * pag)2604 xfs_agfl_reset(
2605 struct xfs_trans *tp,
2606 struct xfs_buf *agbp,
2607 struct xfs_perag *pag)
2608 {
2609 struct xfs_mount *mp = tp->t_mountp;
2610 struct xfs_agf *agf = agbp->b_addr;
2611
2612 ASSERT(xfs_perag_agfl_needs_reset(pag));
2613 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
2614
2615 xfs_warn(mp,
2616 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
2617 "Please unmount and run xfs_repair.",
2618 pag_agno(pag), pag->pagf_flcount);
2619
2620 agf->agf_flfirst = 0;
2621 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
2622 agf->agf_flcount = 0;
2623 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
2624 XFS_AGF_FLCOUNT);
2625
2626 pag->pagf_flcount = 0;
2627 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
2628 }
2629
2630 /*
2631 * Add the extent to the list of extents to be free at transaction end.
2632 * The list is maintained sorted (by block number).
2633 */
2634 static int
xfs_defer_extent_free(struct xfs_trans * tp,xfs_fsblock_t bno,xfs_filblks_t len,const struct xfs_owner_info * oinfo,enum xfs_ag_resv_type type,unsigned int free_flags,struct xfs_defer_pending ** dfpp)2635 xfs_defer_extent_free(
2636 struct xfs_trans *tp,
2637 xfs_fsblock_t bno,
2638 xfs_filblks_t len,
2639 const struct xfs_owner_info *oinfo,
2640 enum xfs_ag_resv_type type,
2641 unsigned int free_flags,
2642 struct xfs_defer_pending **dfpp)
2643 {
2644 struct xfs_extent_free_item *xefi;
2645 struct xfs_mount *mp = tp->t_mountp;
2646
2647 ASSERT(len <= XFS_MAX_BMBT_EXTLEN);
2648 ASSERT(!isnullstartblock(bno));
2649 ASSERT(!(free_flags & ~XFS_FREE_EXTENT_ALL_FLAGS));
2650
2651 if (free_flags & XFS_FREE_EXTENT_REALTIME) {
2652 if (type != XFS_AG_RESV_NONE) {
2653 ASSERT(type == XFS_AG_RESV_NONE);
2654 return -EFSCORRUPTED;
2655 }
2656 if (XFS_IS_CORRUPT(mp, !xfs_verify_rtbext(mp, bno, len)))
2657 return -EFSCORRUPTED;
2658 } else {
2659 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbext(mp, bno, len)))
2660 return -EFSCORRUPTED;
2661 }
2662
2663 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
2664 GFP_KERNEL | __GFP_NOFAIL);
2665 xefi->xefi_startblock = bno;
2666 xefi->xefi_blockcount = (xfs_extlen_t)len;
2667 xefi->xefi_agresv = type;
2668 if (free_flags & XFS_FREE_EXTENT_SKIP_DISCARD)
2669 xefi->xefi_flags |= XFS_EFI_SKIP_DISCARD;
2670 if (free_flags & XFS_FREE_EXTENT_REALTIME)
2671 xefi->xefi_flags |= XFS_EFI_REALTIME;
2672 if (oinfo) {
2673 ASSERT(oinfo->oi_offset == 0);
2674
2675 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
2676 xefi->xefi_flags |= XFS_EFI_ATTR_FORK;
2677 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
2678 xefi->xefi_flags |= XFS_EFI_BMBT_BLOCK;
2679 xefi->xefi_owner = oinfo->oi_owner;
2680 } else {
2681 xefi->xefi_owner = XFS_RMAP_OWN_NULL;
2682 }
2683
2684 xfs_extent_free_defer_add(tp, xefi, dfpp);
2685 return 0;
2686 }
2687
2688 int
xfs_free_extent_later(struct xfs_trans * tp,xfs_fsblock_t bno,xfs_filblks_t len,const struct xfs_owner_info * oinfo,enum xfs_ag_resv_type type,unsigned int free_flags)2689 xfs_free_extent_later(
2690 struct xfs_trans *tp,
2691 xfs_fsblock_t bno,
2692 xfs_filblks_t len,
2693 const struct xfs_owner_info *oinfo,
2694 enum xfs_ag_resv_type type,
2695 unsigned int free_flags)
2696 {
2697 struct xfs_defer_pending *dontcare = NULL;
2698
2699 return xfs_defer_extent_free(tp, bno, len, oinfo, type, free_flags,
2700 &dontcare);
2701 }
2702
2703 /*
2704 * Set up automatic freeing of unwritten space in the filesystem.
2705 *
2706 * This function attached a paused deferred extent free item to the
2707 * transaction. Pausing means that the EFI will be logged in the next
2708 * transaction commit, but the pending EFI will not be finished until the
2709 * pending item is unpaused.
2710 *
2711 * If the system goes down after the EFI has been persisted to the log but
2712 * before the pending item is unpaused, log recovery will find the EFI, fail to
2713 * find the EFD, and free the space.
2714 *
2715 * If the pending item is unpaused, the next transaction commit will log an EFD
2716 * without freeing the space.
2717 *
2718 * Caller must ensure that the tp, fsbno, len, oinfo, and resv flags of the
2719 * @args structure are set to the relevant values.
2720 */
2721 int
xfs_alloc_schedule_autoreap(const struct xfs_alloc_arg * args,unsigned int free_flags,struct xfs_alloc_autoreap * aarp)2722 xfs_alloc_schedule_autoreap(
2723 const struct xfs_alloc_arg *args,
2724 unsigned int free_flags,
2725 struct xfs_alloc_autoreap *aarp)
2726 {
2727 int error;
2728
2729 error = xfs_defer_extent_free(args->tp, args->fsbno, args->len,
2730 &args->oinfo, args->resv, free_flags, &aarp->dfp);
2731 if (error)
2732 return error;
2733
2734 xfs_defer_item_pause(args->tp, aarp->dfp);
2735 return 0;
2736 }
2737
2738 /*
2739 * Cancel automatic freeing of unwritten space in the filesystem.
2740 *
2741 * Earlier, we created a paused deferred extent free item and attached it to
2742 * this transaction so that we could automatically roll back a new space
2743 * allocation if the system went down. Now we want to cancel the paused work
2744 * item by marking the EFI stale so we don't actually free the space, unpausing
2745 * the pending item and logging an EFD.
2746 *
2747 * The caller generally should have already mapped the space into the ondisk
2748 * filesystem. If the reserved space was partially used, the caller must call
2749 * xfs_free_extent_later to create a new EFI to free the unused space.
2750 */
2751 void
xfs_alloc_cancel_autoreap(struct xfs_trans * tp,struct xfs_alloc_autoreap * aarp)2752 xfs_alloc_cancel_autoreap(
2753 struct xfs_trans *tp,
2754 struct xfs_alloc_autoreap *aarp)
2755 {
2756 struct xfs_defer_pending *dfp = aarp->dfp;
2757 struct xfs_extent_free_item *xefi;
2758
2759 if (!dfp)
2760 return;
2761
2762 list_for_each_entry(xefi, &dfp->dfp_work, xefi_list)
2763 xefi->xefi_flags |= XFS_EFI_CANCELLED;
2764
2765 xfs_defer_item_unpause(tp, dfp);
2766 }
2767
2768 /*
2769 * Commit automatic freeing of unwritten space in the filesystem.
2770 *
2771 * This unpauses an earlier _schedule_autoreap and commits to freeing the
2772 * allocated space. Call this if none of the reserved space was used.
2773 */
2774 void
xfs_alloc_commit_autoreap(struct xfs_trans * tp,struct xfs_alloc_autoreap * aarp)2775 xfs_alloc_commit_autoreap(
2776 struct xfs_trans *tp,
2777 struct xfs_alloc_autoreap *aarp)
2778 {
2779 if (aarp->dfp)
2780 xfs_defer_item_unpause(tp, aarp->dfp);
2781 }
2782
2783 /*
2784 * Check if an AGF has a free extent record whose length is equal to
2785 * args->minlen.
2786 */
2787 STATIC int
xfs_exact_minlen_extent_available(struct xfs_alloc_arg * args,struct xfs_buf * agbp,int * stat)2788 xfs_exact_minlen_extent_available(
2789 struct xfs_alloc_arg *args,
2790 struct xfs_buf *agbp,
2791 int *stat)
2792 {
2793 struct xfs_btree_cur *cnt_cur;
2794 xfs_agblock_t fbno;
2795 xfs_extlen_t flen;
2796 int error = 0;
2797
2798 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, agbp,
2799 args->pag);
2800 error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat);
2801 if (error)
2802 goto out;
2803
2804 if (*stat == 0) {
2805 xfs_btree_mark_sick(cnt_cur);
2806 error = -EFSCORRUPTED;
2807 goto out;
2808 }
2809
2810 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat);
2811 if (error)
2812 goto out;
2813
2814 if (*stat == 1 && flen != args->minlen)
2815 *stat = 0;
2816
2817 out:
2818 xfs_btree_del_cursor(cnt_cur, error);
2819
2820 return error;
2821 }
2822
2823 /*
2824 * Decide whether to use this allocation group for this allocation.
2825 * If so, fix up the btree freelist's size.
2826 */
2827 int /* error */
xfs_alloc_fix_freelist(struct xfs_alloc_arg * args,uint32_t alloc_flags)2828 xfs_alloc_fix_freelist(
2829 struct xfs_alloc_arg *args, /* allocation argument structure */
2830 uint32_t alloc_flags)
2831 {
2832 struct xfs_mount *mp = args->mp;
2833 struct xfs_perag *pag = args->pag;
2834 struct xfs_trans *tp = args->tp;
2835 struct xfs_buf *agbp = NULL;
2836 struct xfs_buf *agflbp = NULL;
2837 struct xfs_alloc_arg targs; /* local allocation arguments */
2838 xfs_agblock_t bno; /* freelist block */
2839 xfs_extlen_t need; /* total blocks needed in freelist */
2840 int error = 0;
2841
2842 /* deferred ops (AGFL block frees) require permanent transactions */
2843 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
2844
2845 if (!xfs_perag_initialised_agf(pag)) {
2846 error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp);
2847 if (error) {
2848 /* Couldn't lock the AGF so skip this AG. */
2849 if (error == -EAGAIN)
2850 error = 0;
2851 goto out_no_agbp;
2852 }
2853 }
2854
2855 /*
2856 * If this is a metadata preferred pag and we are user data then try
2857 * somewhere else if we are not being asked to try harder at this
2858 * point
2859 */
2860 if (xfs_perag_prefers_metadata(pag) &&
2861 (args->datatype & XFS_ALLOC_USERDATA) &&
2862 (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2863 ASSERT(!(alloc_flags & XFS_ALLOC_FLAG_FREEING));
2864 goto out_agbp_relse;
2865 }
2866
2867 need = xfs_alloc_min_freelist(mp, pag);
2868 if (!xfs_alloc_space_available(args, need, alloc_flags |
2869 XFS_ALLOC_FLAG_CHECK))
2870 goto out_agbp_relse;
2871
2872 /*
2873 * Get the a.g. freespace buffer.
2874 * Can fail if we're not blocking on locks, and it's held.
2875 */
2876 if (!agbp) {
2877 error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp);
2878 if (error) {
2879 /* Couldn't lock the AGF so skip this AG. */
2880 if (error == -EAGAIN)
2881 error = 0;
2882 goto out_no_agbp;
2883 }
2884 }
2885
2886 /* reset a padding mismatched agfl before final free space check */
2887 if (xfs_perag_agfl_needs_reset(pag))
2888 xfs_agfl_reset(tp, agbp, pag);
2889
2890 /* If there isn't enough total space or single-extent, reject it. */
2891 need = xfs_alloc_min_freelist(mp, pag);
2892 if (!xfs_alloc_space_available(args, need, alloc_flags))
2893 goto out_agbp_relse;
2894
2895 if (IS_ENABLED(CONFIG_XFS_DEBUG) && args->alloc_minlen_only) {
2896 int stat;
2897
2898 error = xfs_exact_minlen_extent_available(args, agbp, &stat);
2899 if (error || !stat)
2900 goto out_agbp_relse;
2901 }
2902
2903 /*
2904 * Make the freelist shorter if it's too long.
2905 *
2906 * Note that from this point onwards, we will always release the agf and
2907 * agfl buffers on error. This handles the case where we error out and
2908 * the buffers are clean or may not have been joined to the transaction
2909 * and hence need to be released manually. If they have been joined to
2910 * the transaction, then xfs_trans_brelse() will handle them
2911 * appropriately based on the recursion count and dirty state of the
2912 * buffer.
2913 *
2914 * XXX (dgc): When we have lots of free space, does this buy us
2915 * anything other than extra overhead when we need to put more blocks
2916 * back on the free list? Maybe we should only do this when space is
2917 * getting low or the AGFL is more than half full?
2918 *
2919 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
2920 * big; the NORMAP flag prevents AGFL expand/shrink operations from
2921 * updating the rmapbt. Both flags are used in xfs_repair while we're
2922 * rebuilding the rmapbt, and neither are used by the kernel. They're
2923 * both required to ensure that rmaps are correctly recorded for the
2924 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
2925 * repair/rmap.c in xfsprogs for details.
2926 */
2927 memset(&targs, 0, sizeof(targs));
2928 /* struct copy below */
2929 if (alloc_flags & XFS_ALLOC_FLAG_NORMAP)
2930 targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
2931 else
2932 targs.oinfo = XFS_RMAP_OINFO_AG;
2933 while (!(alloc_flags & XFS_ALLOC_FLAG_NOSHRINK) &&
2934 pag->pagf_flcount > need) {
2935 error = xfs_alloc_get_freelist(pag, tp, agbp, &bno, 0);
2936 if (error)
2937 goto out_agbp_relse;
2938
2939 /*
2940 * Defer the AGFL block free.
2941 *
2942 * This helps to prevent log reservation overruns due to too
2943 * many allocation operations in a transaction. AGFL frees are
2944 * prone to this problem because for one they are always freed
2945 * one at a time. Further, an immediate AGFL block free can
2946 * cause a btree join and require another block free before the
2947 * real allocation can proceed.
2948 * Deferring the free disconnects freeing up the AGFL slot from
2949 * freeing the block.
2950 */
2951 error = xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, bno),
2952 1, &targs.oinfo, XFS_AG_RESV_AGFL, 0);
2953 if (error)
2954 goto out_agbp_relse;
2955 }
2956
2957 targs.tp = tp;
2958 targs.mp = mp;
2959 targs.agbp = agbp;
2960 targs.agno = args->agno;
2961 targs.alignment = targs.minlen = targs.prod = 1;
2962 targs.pag = pag;
2963 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
2964 if (error)
2965 goto out_agbp_relse;
2966
2967 /* Make the freelist longer if it's too short. */
2968 while (pag->pagf_flcount < need) {
2969 targs.agbno = 0;
2970 targs.maxlen = need - pag->pagf_flcount;
2971 targs.resv = XFS_AG_RESV_AGFL;
2972
2973 /* Allocate as many blocks as possible at once. */
2974 error = xfs_alloc_ag_vextent_size(&targs, alloc_flags);
2975 if (error)
2976 goto out_agflbp_relse;
2977
2978 /*
2979 * Stop if we run out. Won't happen if callers are obeying
2980 * the restrictions correctly. Can happen for free calls
2981 * on a completely full ag.
2982 */
2983 if (targs.agbno == NULLAGBLOCK) {
2984 if (alloc_flags & XFS_ALLOC_FLAG_FREEING)
2985 break;
2986 goto out_agflbp_relse;
2987 }
2988
2989 if (!xfs_rmap_should_skip_owner_update(&targs.oinfo)) {
2990 error = xfs_rmap_alloc(tp, agbp, pag,
2991 targs.agbno, targs.len, &targs.oinfo);
2992 if (error)
2993 goto out_agflbp_relse;
2994 }
2995 error = xfs_alloc_update_counters(tp, agbp,
2996 -((long)(targs.len)));
2997 if (error)
2998 goto out_agflbp_relse;
2999
3000 /*
3001 * Put each allocated block on the list.
3002 */
3003 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
3004 error = xfs_alloc_put_freelist(pag, tp, agbp,
3005 agflbp, bno, 0);
3006 if (error)
3007 goto out_agflbp_relse;
3008 }
3009 }
3010 xfs_trans_brelse(tp, agflbp);
3011 args->agbp = agbp;
3012 return 0;
3013
3014 out_agflbp_relse:
3015 xfs_trans_brelse(tp, agflbp);
3016 out_agbp_relse:
3017 if (agbp)
3018 xfs_trans_brelse(tp, agbp);
3019 out_no_agbp:
3020 args->agbp = NULL;
3021 return error;
3022 }
3023
3024 /*
3025 * Get a block from the freelist.
3026 * Returns with the buffer for the block gotten.
3027 */
3028 int
xfs_alloc_get_freelist(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agblock_t * bnop,int btreeblk)3029 xfs_alloc_get_freelist(
3030 struct xfs_perag *pag,
3031 struct xfs_trans *tp,
3032 struct xfs_buf *agbp,
3033 xfs_agblock_t *bnop,
3034 int btreeblk)
3035 {
3036 struct xfs_agf *agf = agbp->b_addr;
3037 struct xfs_buf *agflbp;
3038 xfs_agblock_t bno;
3039 __be32 *agfl_bno;
3040 int error;
3041 uint32_t logflags;
3042 struct xfs_mount *mp = tp->t_mountp;
3043
3044 /*
3045 * Freelist is empty, give up.
3046 */
3047 if (!agf->agf_flcount) {
3048 *bnop = NULLAGBLOCK;
3049 return 0;
3050 }
3051 /*
3052 * Read the array of free blocks.
3053 */
3054 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
3055 if (error)
3056 return error;
3057
3058
3059 /*
3060 * Get the block number and update the data structures.
3061 */
3062 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
3063 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
3064 if (XFS_IS_CORRUPT(tp->t_mountp, !xfs_verify_agbno(pag, bno)))
3065 return -EFSCORRUPTED;
3066
3067 be32_add_cpu(&agf->agf_flfirst, 1);
3068 xfs_trans_brelse(tp, agflbp);
3069 if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
3070 agf->agf_flfirst = 0;
3071
3072 ASSERT(!xfs_perag_agfl_needs_reset(pag));
3073 be32_add_cpu(&agf->agf_flcount, -1);
3074 pag->pagf_flcount--;
3075
3076 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
3077 if (btreeblk) {
3078 be32_add_cpu(&agf->agf_btreeblks, 1);
3079 pag->pagf_btreeblks++;
3080 logflags |= XFS_AGF_BTREEBLKS;
3081 }
3082
3083 xfs_alloc_log_agf(tp, agbp, logflags);
3084 *bnop = bno;
3085
3086 return 0;
3087 }
3088
3089 /*
3090 * Log the given fields from the agf structure.
3091 */
3092 void
xfs_alloc_log_agf(struct xfs_trans * tp,struct xfs_buf * bp,uint32_t fields)3093 xfs_alloc_log_agf(
3094 struct xfs_trans *tp,
3095 struct xfs_buf *bp,
3096 uint32_t fields)
3097 {
3098 int first; /* first byte offset */
3099 int last; /* last byte offset */
3100 static const short offsets[] = {
3101 offsetof(xfs_agf_t, agf_magicnum),
3102 offsetof(xfs_agf_t, agf_versionnum),
3103 offsetof(xfs_agf_t, agf_seqno),
3104 offsetof(xfs_agf_t, agf_length),
3105 offsetof(xfs_agf_t, agf_bno_root), /* also cnt/rmap root */
3106 offsetof(xfs_agf_t, agf_bno_level), /* also cnt/rmap levels */
3107 offsetof(xfs_agf_t, agf_flfirst),
3108 offsetof(xfs_agf_t, agf_fllast),
3109 offsetof(xfs_agf_t, agf_flcount),
3110 offsetof(xfs_agf_t, agf_freeblks),
3111 offsetof(xfs_agf_t, agf_longest),
3112 offsetof(xfs_agf_t, agf_btreeblks),
3113 offsetof(xfs_agf_t, agf_uuid),
3114 offsetof(xfs_agf_t, agf_rmap_blocks),
3115 offsetof(xfs_agf_t, agf_refcount_blocks),
3116 offsetof(xfs_agf_t, agf_refcount_root),
3117 offsetof(xfs_agf_t, agf_refcount_level),
3118 /* needed so that we don't log the whole rest of the structure: */
3119 offsetof(xfs_agf_t, agf_spare64),
3120 sizeof(xfs_agf_t)
3121 };
3122
3123 trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_);
3124
3125 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
3126
3127 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
3128 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
3129 }
3130
3131 /*
3132 * Put the block on the freelist for the allocation group.
3133 */
3134 int
xfs_alloc_put_freelist(struct xfs_perag * pag,struct xfs_trans * tp,struct xfs_buf * agbp,struct xfs_buf * agflbp,xfs_agblock_t bno,int btreeblk)3135 xfs_alloc_put_freelist(
3136 struct xfs_perag *pag,
3137 struct xfs_trans *tp,
3138 struct xfs_buf *agbp,
3139 struct xfs_buf *agflbp,
3140 xfs_agblock_t bno,
3141 int btreeblk)
3142 {
3143 struct xfs_mount *mp = tp->t_mountp;
3144 struct xfs_agf *agf = agbp->b_addr;
3145 __be32 *blockp;
3146 int error;
3147 uint32_t logflags;
3148 __be32 *agfl_bno;
3149 int startoff;
3150
3151 if (!agflbp) {
3152 error = xfs_alloc_read_agfl(pag, tp, &agflbp);
3153 if (error)
3154 return error;
3155 }
3156
3157 be32_add_cpu(&agf->agf_fllast, 1);
3158 if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
3159 agf->agf_fllast = 0;
3160
3161 ASSERT(!xfs_perag_agfl_needs_reset(pag));
3162 be32_add_cpu(&agf->agf_flcount, 1);
3163 pag->pagf_flcount++;
3164
3165 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
3166 if (btreeblk) {
3167 be32_add_cpu(&agf->agf_btreeblks, -1);
3168 pag->pagf_btreeblks--;
3169 logflags |= XFS_AGF_BTREEBLKS;
3170 }
3171
3172 ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
3173
3174 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
3175 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
3176 *blockp = cpu_to_be32(bno);
3177 startoff = (char *)blockp - (char *)agflbp->b_addr;
3178
3179 xfs_alloc_log_agf(tp, agbp, logflags);
3180
3181 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
3182 xfs_trans_log_buf(tp, agflbp, startoff,
3183 startoff + sizeof(xfs_agblock_t) - 1);
3184 return 0;
3185 }
3186
3187 /*
3188 * Check that this AGF/AGI header's sequence number and length matches the AG
3189 * number and size in fsblocks.
3190 */
3191 xfs_failaddr_t
xfs_validate_ag_length(struct xfs_buf * bp,uint32_t seqno,uint32_t length)3192 xfs_validate_ag_length(
3193 struct xfs_buf *bp,
3194 uint32_t seqno,
3195 uint32_t length)
3196 {
3197 struct xfs_mount *mp = bp->b_mount;
3198 /*
3199 * During growfs operations, the perag is not fully initialised,
3200 * so we can't use it for any useful checking. growfs ensures we can't
3201 * use it by using uncached buffers that don't have the perag attached
3202 * so we can detect and avoid this problem.
3203 */
3204 if (bp->b_pag && seqno != pag_agno(bp->b_pag))
3205 return __this_address;
3206
3207 /*
3208 * Only the last AG in the filesystem is allowed to be shorter
3209 * than the AG size recorded in the superblock.
3210 */
3211 if (length != mp->m_sb.sb_agblocks) {
3212 /*
3213 * During growfs, the new last AG can get here before we
3214 * have updated the superblock. Give it a pass on the seqno
3215 * check.
3216 */
3217 if (bp->b_pag && seqno != mp->m_sb.sb_agcount - 1)
3218 return __this_address;
3219 if (length < XFS_MIN_AG_BLOCKS)
3220 return __this_address;
3221 if (length > mp->m_sb.sb_agblocks)
3222 return __this_address;
3223 }
3224
3225 return NULL;
3226 }
3227
3228 /*
3229 * Verify the AGF is consistent.
3230 *
3231 * We do not verify the AGFL indexes in the AGF are fully consistent here
3232 * because of issues with variable on-disk structure sizes. Instead, we check
3233 * the agfl indexes for consistency when we initialise the perag from the AGF
3234 * information after a read completes.
3235 *
3236 * If the index is inconsistent, then we mark the perag as needing an AGFL
3237 * reset. The first AGFL update performed then resets the AGFL indexes and
3238 * refills the AGFL with known good free blocks, allowing the filesystem to
3239 * continue operating normally at the cost of a few leaked free space blocks.
3240 */
3241 static xfs_failaddr_t
xfs_agf_verify(struct xfs_buf * bp)3242 xfs_agf_verify(
3243 struct xfs_buf *bp)
3244 {
3245 struct xfs_mount *mp = bp->b_mount;
3246 struct xfs_agf *agf = bp->b_addr;
3247 xfs_failaddr_t fa;
3248 uint32_t agf_seqno = be32_to_cpu(agf->agf_seqno);
3249 uint32_t agf_length = be32_to_cpu(agf->agf_length);
3250
3251 if (xfs_has_crc(mp)) {
3252 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
3253 return __this_address;
3254 if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn)))
3255 return __this_address;
3256 }
3257
3258 if (!xfs_verify_magic(bp, agf->agf_magicnum))
3259 return __this_address;
3260
3261 if (!XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)))
3262 return __this_address;
3263
3264 /*
3265 * Both agf_seqno and agf_length need to validated before anything else
3266 * block number related in the AGF or AGFL can be checked.
3267 */
3268 fa = xfs_validate_ag_length(bp, agf_seqno, agf_length);
3269 if (fa)
3270 return fa;
3271
3272 if (be32_to_cpu(agf->agf_flfirst) >= xfs_agfl_size(mp))
3273 return __this_address;
3274 if (be32_to_cpu(agf->agf_fllast) >= xfs_agfl_size(mp))
3275 return __this_address;
3276 if (be32_to_cpu(agf->agf_flcount) > xfs_agfl_size(mp))
3277 return __this_address;
3278
3279 if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
3280 be32_to_cpu(agf->agf_freeblks) > agf_length)
3281 return __this_address;
3282
3283 if (be32_to_cpu(agf->agf_bno_level) < 1 ||
3284 be32_to_cpu(agf->agf_cnt_level) < 1 ||
3285 be32_to_cpu(agf->agf_bno_level) > mp->m_alloc_maxlevels ||
3286 be32_to_cpu(agf->agf_cnt_level) > mp->m_alloc_maxlevels)
3287 return __this_address;
3288
3289 if (xfs_has_lazysbcount(mp) &&
3290 be32_to_cpu(agf->agf_btreeblks) > agf_length)
3291 return __this_address;
3292
3293 if (xfs_has_rmapbt(mp)) {
3294 if (be32_to_cpu(agf->agf_rmap_blocks) > agf_length)
3295 return __this_address;
3296
3297 if (be32_to_cpu(agf->agf_rmap_level) < 1 ||
3298 be32_to_cpu(agf->agf_rmap_level) > mp->m_rmap_maxlevels)
3299 return __this_address;
3300 }
3301
3302 if (xfs_has_reflink(mp)) {
3303 if (be32_to_cpu(agf->agf_refcount_blocks) > agf_length)
3304 return __this_address;
3305
3306 if (be32_to_cpu(agf->agf_refcount_level) < 1 ||
3307 be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels)
3308 return __this_address;
3309 }
3310
3311 return NULL;
3312 }
3313
3314 static void
xfs_agf_read_verify(struct xfs_buf * bp)3315 xfs_agf_read_verify(
3316 struct xfs_buf *bp)
3317 {
3318 struct xfs_mount *mp = bp->b_mount;
3319 xfs_failaddr_t fa;
3320
3321 if (xfs_has_crc(mp) &&
3322 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
3323 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
3324 else {
3325 fa = xfs_agf_verify(bp);
3326 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
3327 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
3328 }
3329 }
3330
3331 static void
xfs_agf_write_verify(struct xfs_buf * bp)3332 xfs_agf_write_verify(
3333 struct xfs_buf *bp)
3334 {
3335 struct xfs_mount *mp = bp->b_mount;
3336 struct xfs_buf_log_item *bip = bp->b_log_item;
3337 struct xfs_agf *agf = bp->b_addr;
3338 xfs_failaddr_t fa;
3339
3340 fa = xfs_agf_verify(bp);
3341 if (fa) {
3342 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
3343 return;
3344 }
3345
3346 if (!xfs_has_crc(mp))
3347 return;
3348
3349 if (bip)
3350 agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
3351
3352 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
3353 }
3354
3355 const struct xfs_buf_ops xfs_agf_buf_ops = {
3356 .name = "xfs_agf",
3357 .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) },
3358 .verify_read = xfs_agf_read_verify,
3359 .verify_write = xfs_agf_write_verify,
3360 .verify_struct = xfs_agf_verify,
3361 };
3362
3363 /*
3364 * Read in the allocation group header (free/alloc section).
3365 */
3366 int
xfs_read_agf(struct xfs_perag * pag,struct xfs_trans * tp,int flags,struct xfs_buf ** agfbpp)3367 xfs_read_agf(
3368 struct xfs_perag *pag,
3369 struct xfs_trans *tp,
3370 int flags,
3371 struct xfs_buf **agfbpp)
3372 {
3373 struct xfs_mount *mp = pag_mount(pag);
3374 int error;
3375
3376 trace_xfs_read_agf(pag);
3377
3378 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
3379 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGF_DADDR(mp)),
3380 XFS_FSS_TO_BB(mp, 1), flags, agfbpp, &xfs_agf_buf_ops);
3381 if (xfs_metadata_is_sick(error))
3382 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF);
3383 if (error)
3384 return error;
3385
3386 xfs_buf_set_ref(*agfbpp, XFS_AGF_REF);
3387 return 0;
3388 }
3389
3390 /*
3391 * Read in the allocation group header (free/alloc section) and initialise the
3392 * perag structure if necessary. If the caller provides @agfbpp, then return the
3393 * locked buffer to the caller, otherwise free it.
3394 */
3395 int
xfs_alloc_read_agf(struct xfs_perag * pag,struct xfs_trans * tp,int flags,struct xfs_buf ** agfbpp)3396 xfs_alloc_read_agf(
3397 struct xfs_perag *pag,
3398 struct xfs_trans *tp,
3399 int flags,
3400 struct xfs_buf **agfbpp)
3401 {
3402 struct xfs_mount *mp = pag_mount(pag);
3403 struct xfs_buf *agfbp;
3404 struct xfs_agf *agf;
3405 int error;
3406 int allocbt_blks;
3407
3408 trace_xfs_alloc_read_agf(pag);
3409
3410 /* We don't support trylock when freeing. */
3411 ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) !=
3412 (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK));
3413 error = xfs_read_agf(pag, tp,
3414 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
3415 &agfbp);
3416 if (error)
3417 return error;
3418
3419 agf = agfbp->b_addr;
3420 if (!xfs_perag_initialised_agf(pag)) {
3421 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
3422 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
3423 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
3424 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
3425 pag->pagf_bno_level = be32_to_cpu(agf->agf_bno_level);
3426 pag->pagf_cnt_level = be32_to_cpu(agf->agf_cnt_level);
3427 pag->pagf_rmap_level = be32_to_cpu(agf->agf_rmap_level);
3428 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
3429 if (xfs_agfl_needs_reset(mp, agf))
3430 set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
3431 else
3432 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate);
3433
3434 /*
3435 * Update the in-core allocbt counter. Filter out the rmapbt
3436 * subset of the btreeblks counter because the rmapbt is managed
3437 * by perag reservation. Subtract one for the rmapbt root block
3438 * because the rmap counter includes it while the btreeblks
3439 * counter only tracks non-root blocks.
3440 */
3441 allocbt_blks = pag->pagf_btreeblks;
3442 if (xfs_has_rmapbt(mp))
3443 allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1;
3444 if (allocbt_blks > 0)
3445 atomic64_add(allocbt_blks, &mp->m_allocbt_blks);
3446
3447 set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
3448 }
3449 #ifdef DEBUG
3450 else if (!xfs_is_shutdown(mp)) {
3451 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
3452 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
3453 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
3454 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
3455 ASSERT(pag->pagf_bno_level == be32_to_cpu(agf->agf_bno_level));
3456 ASSERT(pag->pagf_cnt_level == be32_to_cpu(agf->agf_cnt_level));
3457 }
3458 #endif
3459 if (agfbpp)
3460 *agfbpp = agfbp;
3461 else
3462 xfs_trans_brelse(tp, agfbp);
3463 return 0;
3464 }
3465
3466 /*
3467 * Pre-proces allocation arguments to set initial state that we don't require
3468 * callers to set up correctly, as well as bounds check the allocation args
3469 * that are set up.
3470 */
3471 static int
xfs_alloc_vextent_check_args(struct xfs_alloc_arg * args,xfs_fsblock_t target,xfs_agnumber_t * minimum_agno)3472 xfs_alloc_vextent_check_args(
3473 struct xfs_alloc_arg *args,
3474 xfs_fsblock_t target,
3475 xfs_agnumber_t *minimum_agno)
3476 {
3477 struct xfs_mount *mp = args->mp;
3478 xfs_agblock_t agsize;
3479
3480 args->fsbno = NULLFSBLOCK;
3481
3482 *minimum_agno = 0;
3483 if (args->tp->t_highest_agno != NULLAGNUMBER)
3484 *minimum_agno = args->tp->t_highest_agno;
3485
3486 /*
3487 * Just fix this up, for the case where the last a.g. is shorter
3488 * (or there's only one a.g.) and the caller couldn't easily figure
3489 * that out (xfs_bmap_alloc).
3490 */
3491 agsize = mp->m_sb.sb_agblocks;
3492 if (args->maxlen > agsize)
3493 args->maxlen = agsize;
3494 if (args->alignment == 0)
3495 args->alignment = 1;
3496
3497 ASSERT(args->minlen > 0);
3498 ASSERT(args->maxlen > 0);
3499 ASSERT(args->alignment > 0);
3500 ASSERT(args->resv != XFS_AG_RESV_AGFL);
3501
3502 ASSERT(XFS_FSB_TO_AGNO(mp, target) < mp->m_sb.sb_agcount);
3503 ASSERT(XFS_FSB_TO_AGBNO(mp, target) < agsize);
3504 ASSERT(args->minlen <= args->maxlen);
3505 ASSERT(args->minlen <= agsize);
3506 ASSERT(args->mod < args->prod);
3507
3508 if (XFS_FSB_TO_AGNO(mp, target) >= mp->m_sb.sb_agcount ||
3509 XFS_FSB_TO_AGBNO(mp, target) >= agsize ||
3510 args->minlen > args->maxlen || args->minlen > agsize ||
3511 args->mod >= args->prod) {
3512 trace_xfs_alloc_vextent_badargs(args);
3513 return -ENOSPC;
3514 }
3515
3516 if (args->agno != NULLAGNUMBER && *minimum_agno > args->agno) {
3517 trace_xfs_alloc_vextent_skip_deadlock(args);
3518 return -ENOSPC;
3519 }
3520 return 0;
3521
3522 }
3523
3524 /*
3525 * Prepare an AG for allocation. If the AG is not prepared to accept the
3526 * allocation, return failure.
3527 *
3528 * XXX(dgc): The complexity of "need_pag" will go away as all caller paths are
3529 * modified to hold their own perag references.
3530 */
3531 static int
xfs_alloc_vextent_prepare_ag(struct xfs_alloc_arg * args,uint32_t alloc_flags)3532 xfs_alloc_vextent_prepare_ag(
3533 struct xfs_alloc_arg *args,
3534 uint32_t alloc_flags)
3535 {
3536 bool need_pag = !args->pag;
3537 int error;
3538
3539 if (need_pag)
3540 args->pag = xfs_perag_get(args->mp, args->agno);
3541
3542 args->agbp = NULL;
3543 error = xfs_alloc_fix_freelist(args, alloc_flags);
3544 if (error) {
3545 trace_xfs_alloc_vextent_nofix(args);
3546 if (need_pag)
3547 xfs_perag_put(args->pag);
3548 args->agbno = NULLAGBLOCK;
3549 return error;
3550 }
3551 if (!args->agbp) {
3552 /* cannot allocate in this AG at all */
3553 trace_xfs_alloc_vextent_noagbp(args);
3554 args->agbno = NULLAGBLOCK;
3555 return 0;
3556 }
3557 args->wasfromfl = 0;
3558 return 0;
3559 }
3560
3561 /*
3562 * Post-process allocation results to account for the allocation if it succeed
3563 * and set the allocated block number correctly for the caller.
3564 *
3565 * XXX: we should really be returning ENOSPC for ENOSPC, not
3566 * hiding it behind a "successful" NULLFSBLOCK allocation.
3567 */
3568 static int
xfs_alloc_vextent_finish(struct xfs_alloc_arg * args,xfs_agnumber_t minimum_agno,int alloc_error,bool drop_perag)3569 xfs_alloc_vextent_finish(
3570 struct xfs_alloc_arg *args,
3571 xfs_agnumber_t minimum_agno,
3572 int alloc_error,
3573 bool drop_perag)
3574 {
3575 struct xfs_mount *mp = args->mp;
3576 int error = 0;
3577
3578 /*
3579 * We can end up here with a locked AGF. If we failed, the caller is
3580 * likely going to try to allocate again with different parameters, and
3581 * that can widen the AGs that are searched for free space. If we have
3582 * to do BMBT block allocation, we have to do a new allocation.
3583 *
3584 * Hence leaving this function with the AGF locked opens up potential
3585 * ABBA AGF deadlocks because a future allocation attempt in this
3586 * transaction may attempt to lock a lower number AGF.
3587 *
3588 * We can't release the AGF until the transaction is commited, so at
3589 * this point we must update the "first allocation" tracker to point at
3590 * this AG if the tracker is empty or points to a lower AG. This allows
3591 * the next allocation attempt to be modified appropriately to avoid
3592 * deadlocks.
3593 */
3594 if (args->agbp &&
3595 (args->tp->t_highest_agno == NULLAGNUMBER ||
3596 args->agno > minimum_agno))
3597 args->tp->t_highest_agno = args->agno;
3598
3599 /*
3600 * If the allocation failed with an error or we had an ENOSPC result,
3601 * preserve the returned error whilst also marking the allocation result
3602 * as "no extent allocated". This ensures that callers that fail to
3603 * capture the error will still treat it as a failed allocation.
3604 */
3605 if (alloc_error || args->agbno == NULLAGBLOCK) {
3606 args->fsbno = NULLFSBLOCK;
3607 error = alloc_error;
3608 goto out_drop_perag;
3609 }
3610
3611 args->fsbno = xfs_agbno_to_fsb(args->pag, args->agbno);
3612
3613 ASSERT(args->len >= args->minlen);
3614 ASSERT(args->len <= args->maxlen);
3615 ASSERT(args->agbno % args->alignment == 0);
3616 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno), args->len);
3617
3618 /* if not file data, insert new block into the reverse map btree */
3619 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
3620 error = xfs_rmap_alloc(args->tp, args->agbp, args->pag,
3621 args->agbno, args->len, &args->oinfo);
3622 if (error)
3623 goto out_drop_perag;
3624 }
3625
3626 if (!args->wasfromfl) {
3627 error = xfs_alloc_update_counters(args->tp, args->agbp,
3628 -((long)(args->len)));
3629 if (error)
3630 goto out_drop_perag;
3631
3632 ASSERT(!xfs_extent_busy_search(pag_group(args->pag),
3633 args->agbno, args->len));
3634 }
3635
3636 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
3637
3638 XFS_STATS_INC(mp, xs_allocx);
3639 XFS_STATS_ADD(mp, xs_allocb, args->len);
3640
3641 trace_xfs_alloc_vextent_finish(args);
3642
3643 out_drop_perag:
3644 if (drop_perag && args->pag) {
3645 xfs_perag_rele(args->pag);
3646 args->pag = NULL;
3647 }
3648 return error;
3649 }
3650
3651 /*
3652 * Allocate within a single AG only. This uses a best-fit length algorithm so if
3653 * you need an exact sized allocation without locality constraints, this is the
3654 * fastest way to do it.
3655 *
3656 * Caller is expected to hold a perag reference in args->pag.
3657 */
3658 int
xfs_alloc_vextent_this_ag(struct xfs_alloc_arg * args,xfs_agnumber_t agno)3659 xfs_alloc_vextent_this_ag(
3660 struct xfs_alloc_arg *args,
3661 xfs_agnumber_t agno)
3662 {
3663 xfs_agnumber_t minimum_agno;
3664 uint32_t alloc_flags = 0;
3665 int error;
3666
3667 ASSERT(args->pag != NULL);
3668 ASSERT(pag_agno(args->pag) == agno);
3669
3670 args->agno = agno;
3671 args->agbno = 0;
3672
3673 trace_xfs_alloc_vextent_this_ag(args);
3674
3675 error = xfs_alloc_vextent_check_args(args,
3676 xfs_agbno_to_fsb(args->pag, 0), &minimum_agno);
3677 if (error) {
3678 if (error == -ENOSPC)
3679 return 0;
3680 return error;
3681 }
3682
3683 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
3684 if (!error && args->agbp)
3685 error = xfs_alloc_ag_vextent_size(args, alloc_flags);
3686
3687 return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
3688 }
3689
3690 /*
3691 * Iterate all AGs trying to allocate an extent starting from @start_ag.
3692 *
3693 * If the incoming allocation type is XFS_ALLOCTYPE_NEAR_BNO, it means the
3694 * allocation attempts in @start_agno have locality information. If we fail to
3695 * allocate in that AG, then we revert to anywhere-in-AG for all the other AGs
3696 * we attempt to allocation in as there is no locality optimisation possible for
3697 * those allocations.
3698 *
3699 * On return, args->pag may be left referenced if we finish before the "all
3700 * failed" return point. The allocation finish still needs the perag, and
3701 * so the caller will release it once they've finished the allocation.
3702 *
3703 * When we wrap the AG iteration at the end of the filesystem, we have to be
3704 * careful not to wrap into AGs below ones we already have locked in the
3705 * transaction if we are doing a blocking iteration. This will result in an
3706 * out-of-order locking of AGFs and hence can cause deadlocks.
3707 */
3708 static int
xfs_alloc_vextent_iterate_ags(struct xfs_alloc_arg * args,xfs_agnumber_t minimum_agno,xfs_agnumber_t start_agno,xfs_agblock_t target_agbno,uint32_t alloc_flags)3709 xfs_alloc_vextent_iterate_ags(
3710 struct xfs_alloc_arg *args,
3711 xfs_agnumber_t minimum_agno,
3712 xfs_agnumber_t start_agno,
3713 xfs_agblock_t target_agbno,
3714 uint32_t alloc_flags)
3715 {
3716 struct xfs_mount *mp = args->mp;
3717 xfs_agnumber_t restart_agno = minimum_agno;
3718 xfs_agnumber_t agno;
3719 int error = 0;
3720
3721 if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK)
3722 restart_agno = 0;
3723 restart:
3724 for_each_perag_wrap_range(mp, start_agno, restart_agno,
3725 mp->m_sb.sb_agcount, agno, args->pag) {
3726 args->agno = agno;
3727 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
3728 if (error)
3729 break;
3730 if (!args->agbp) {
3731 trace_xfs_alloc_vextent_loopfailed(args);
3732 continue;
3733 }
3734
3735 /*
3736 * Allocation is supposed to succeed now, so break out of the
3737 * loop regardless of whether we succeed or not.
3738 */
3739 if (args->agno == start_agno && target_agbno) {
3740 args->agbno = target_agbno;
3741 error = xfs_alloc_ag_vextent_near(args, alloc_flags);
3742 } else {
3743 args->agbno = 0;
3744 error = xfs_alloc_ag_vextent_size(args, alloc_flags);
3745 }
3746 break;
3747 }
3748 if (error) {
3749 xfs_perag_rele(args->pag);
3750 args->pag = NULL;
3751 return error;
3752 }
3753 if (args->agbp)
3754 return 0;
3755
3756 /*
3757 * We didn't find an AG we can alloation from. If we were given
3758 * constraining flags by the caller, drop them and retry the allocation
3759 * without any constraints being set.
3760 */
3761 if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK) {
3762 alloc_flags &= ~XFS_ALLOC_FLAG_TRYLOCK;
3763 restart_agno = minimum_agno;
3764 goto restart;
3765 }
3766
3767 ASSERT(args->pag == NULL);
3768 trace_xfs_alloc_vextent_allfailed(args);
3769 return 0;
3770 }
3771
3772 /*
3773 * Iterate from the AGs from the start AG to the end of the filesystem, trying
3774 * to allocate blocks. It starts with a near allocation attempt in the initial
3775 * AG, then falls back to anywhere-in-ag after the first AG fails. It will wrap
3776 * back to zero if allowed by previous allocations in this transaction,
3777 * otherwise will wrap back to the start AG and run a second blocking pass to
3778 * the end of the filesystem.
3779 */
3780 int
xfs_alloc_vextent_start_ag(struct xfs_alloc_arg * args,xfs_fsblock_t target)3781 xfs_alloc_vextent_start_ag(
3782 struct xfs_alloc_arg *args,
3783 xfs_fsblock_t target)
3784 {
3785 struct xfs_mount *mp = args->mp;
3786 xfs_agnumber_t minimum_agno;
3787 xfs_agnumber_t start_agno;
3788 xfs_agnumber_t rotorstep = xfs_rotorstep;
3789 bool bump_rotor = false;
3790 uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK;
3791 int error;
3792
3793 ASSERT(args->pag == NULL);
3794
3795 args->agno = NULLAGNUMBER;
3796 args->agbno = NULLAGBLOCK;
3797
3798 trace_xfs_alloc_vextent_start_ag(args);
3799
3800 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3801 if (error) {
3802 if (error == -ENOSPC)
3803 return 0;
3804 return error;
3805 }
3806
3807 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
3808 xfs_is_inode32(mp)) {
3809 target = XFS_AGB_TO_FSB(mp,
3810 ((mp->m_agfrotor / rotorstep) %
3811 mp->m_sb.sb_agcount), 0);
3812 bump_rotor = 1;
3813 }
3814
3815 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
3816 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
3817 XFS_FSB_TO_AGBNO(mp, target), alloc_flags);
3818
3819 if (bump_rotor) {
3820 if (args->agno == start_agno)
3821 mp->m_agfrotor = (mp->m_agfrotor + 1) %
3822 (mp->m_sb.sb_agcount * rotorstep);
3823 else
3824 mp->m_agfrotor = (args->agno * rotorstep + 1) %
3825 (mp->m_sb.sb_agcount * rotorstep);
3826 }
3827
3828 return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
3829 }
3830
3831 /*
3832 * Iterate from the agno indicated via @target through to the end of the
3833 * filesystem attempting blocking allocation. This does not wrap or try a second
3834 * pass, so will not recurse into AGs lower than indicated by the target.
3835 */
3836 int
xfs_alloc_vextent_first_ag(struct xfs_alloc_arg * args,xfs_fsblock_t target)3837 xfs_alloc_vextent_first_ag(
3838 struct xfs_alloc_arg *args,
3839 xfs_fsblock_t target)
3840 {
3841 struct xfs_mount *mp = args->mp;
3842 xfs_agnumber_t minimum_agno;
3843 xfs_agnumber_t start_agno;
3844 uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK;
3845 int error;
3846
3847 ASSERT(args->pag == NULL);
3848
3849 args->agno = NULLAGNUMBER;
3850 args->agbno = NULLAGBLOCK;
3851
3852 trace_xfs_alloc_vextent_first_ag(args);
3853
3854 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3855 if (error) {
3856 if (error == -ENOSPC)
3857 return 0;
3858 return error;
3859 }
3860
3861 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target));
3862 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno,
3863 XFS_FSB_TO_AGBNO(mp, target), alloc_flags);
3864 return xfs_alloc_vextent_finish(args, minimum_agno, error, true);
3865 }
3866
3867 /*
3868 * Allocate at the exact block target or fail. Caller is expected to hold a
3869 * perag reference in args->pag.
3870 */
3871 int
xfs_alloc_vextent_exact_bno(struct xfs_alloc_arg * args,xfs_fsblock_t target)3872 xfs_alloc_vextent_exact_bno(
3873 struct xfs_alloc_arg *args,
3874 xfs_fsblock_t target)
3875 {
3876 struct xfs_mount *mp = args->mp;
3877 xfs_agnumber_t minimum_agno;
3878 int error;
3879
3880 ASSERT(args->pag != NULL);
3881 ASSERT(pag_agno(args->pag) == XFS_FSB_TO_AGNO(mp, target));
3882
3883 args->agno = XFS_FSB_TO_AGNO(mp, target);
3884 args->agbno = XFS_FSB_TO_AGBNO(mp, target);
3885
3886 trace_xfs_alloc_vextent_exact_bno(args);
3887
3888 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3889 if (error) {
3890 if (error == -ENOSPC)
3891 return 0;
3892 return error;
3893 }
3894
3895 error = xfs_alloc_vextent_prepare_ag(args, 0);
3896 if (!error && args->agbp)
3897 error = xfs_alloc_ag_vextent_exact(args);
3898
3899 return xfs_alloc_vextent_finish(args, minimum_agno, error, false);
3900 }
3901
3902 /*
3903 * Allocate an extent as close to the target as possible. If there are not
3904 * viable candidates in the AG, then fail the allocation.
3905 *
3906 * Caller may or may not have a per-ag reference in args->pag.
3907 */
3908 int
xfs_alloc_vextent_near_bno(struct xfs_alloc_arg * args,xfs_fsblock_t target)3909 xfs_alloc_vextent_near_bno(
3910 struct xfs_alloc_arg *args,
3911 xfs_fsblock_t target)
3912 {
3913 struct xfs_mount *mp = args->mp;
3914 xfs_agnumber_t minimum_agno;
3915 bool needs_perag = args->pag == NULL;
3916 uint32_t alloc_flags = 0;
3917 int error;
3918
3919 if (!needs_perag)
3920 ASSERT(pag_agno(args->pag) == XFS_FSB_TO_AGNO(mp, target));
3921
3922 args->agno = XFS_FSB_TO_AGNO(mp, target);
3923 args->agbno = XFS_FSB_TO_AGBNO(mp, target);
3924
3925 trace_xfs_alloc_vextent_near_bno(args);
3926
3927 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno);
3928 if (error) {
3929 if (error == -ENOSPC)
3930 return 0;
3931 return error;
3932 }
3933
3934 if (needs_perag)
3935 args->pag = xfs_perag_grab(mp, args->agno);
3936
3937 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags);
3938 if (!error && args->agbp)
3939 error = xfs_alloc_ag_vextent_near(args, alloc_flags);
3940
3941 return xfs_alloc_vextent_finish(args, minimum_agno, error, needs_perag);
3942 }
3943
3944 /* Ensure that the freelist is at full capacity. */
3945 int
xfs_free_extent_fix_freelist(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_buf ** agbp)3946 xfs_free_extent_fix_freelist(
3947 struct xfs_trans *tp,
3948 struct xfs_perag *pag,
3949 struct xfs_buf **agbp)
3950 {
3951 struct xfs_alloc_arg args;
3952 int error;
3953
3954 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3955 args.tp = tp;
3956 args.mp = tp->t_mountp;
3957 args.agno = pag_agno(pag);
3958 args.pag = pag;
3959
3960 /*
3961 * validate that the block number is legal - the enables us to detect
3962 * and handle a silent filesystem corruption rather than crashing.
3963 */
3964 if (args.agno >= args.mp->m_sb.sb_agcount)
3965 return -EFSCORRUPTED;
3966
3967 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3968 if (error)
3969 return error;
3970
3971 *agbp = args.agbp;
3972 return 0;
3973 }
3974
3975 /*
3976 * Free an extent.
3977 * Just break up the extent address and hand off to xfs_free_ag_extent
3978 * after fixing up the freelist.
3979 */
3980 int
__xfs_free_extent(struct xfs_trans * tp,struct xfs_perag * pag,xfs_agblock_t agbno,xfs_extlen_t len,const struct xfs_owner_info * oinfo,enum xfs_ag_resv_type type,bool skip_discard)3981 __xfs_free_extent(
3982 struct xfs_trans *tp,
3983 struct xfs_perag *pag,
3984 xfs_agblock_t agbno,
3985 xfs_extlen_t len,
3986 const struct xfs_owner_info *oinfo,
3987 enum xfs_ag_resv_type type,
3988 bool skip_discard)
3989 {
3990 struct xfs_mount *mp = tp->t_mountp;
3991 struct xfs_buf *agbp;
3992 struct xfs_agf *agf;
3993 int error;
3994 unsigned int busy_flags = 0;
3995
3996 ASSERT(len != 0);
3997 ASSERT(type != XFS_AG_RESV_AGFL);
3998
3999 if (XFS_TEST_ERROR(false, mp,
4000 XFS_ERRTAG_FREE_EXTENT))
4001 return -EIO;
4002
4003 error = xfs_free_extent_fix_freelist(tp, pag, &agbp);
4004 if (error) {
4005 if (xfs_metadata_is_sick(error))
4006 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
4007 return error;
4008 }
4009
4010 agf = agbp->b_addr;
4011
4012 if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) {
4013 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
4014 error = -EFSCORRUPTED;
4015 goto err_release;
4016 }
4017
4018 /* validate the extent size is legal now we have the agf locked */
4019 if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) {
4020 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT);
4021 error = -EFSCORRUPTED;
4022 goto err_release;
4023 }
4024
4025 error = xfs_free_ag_extent(tp, agbp, agbno, len, oinfo, type);
4026 if (error)
4027 goto err_release;
4028
4029 if (skip_discard)
4030 busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
4031 xfs_extent_busy_insert(tp, pag_group(pag), agbno, len, busy_flags);
4032 return 0;
4033
4034 err_release:
4035 xfs_trans_brelse(tp, agbp);
4036 return error;
4037 }
4038
4039 struct xfs_alloc_query_range_info {
4040 xfs_alloc_query_range_fn fn;
4041 void *priv;
4042 };
4043
4044 /* Format btree record and pass to our callback. */
4045 STATIC int
xfs_alloc_query_range_helper(struct xfs_btree_cur * cur,const union xfs_btree_rec * rec,void * priv)4046 xfs_alloc_query_range_helper(
4047 struct xfs_btree_cur *cur,
4048 const union xfs_btree_rec *rec,
4049 void *priv)
4050 {
4051 struct xfs_alloc_query_range_info *query = priv;
4052 struct xfs_alloc_rec_incore irec;
4053 xfs_failaddr_t fa;
4054
4055 xfs_alloc_btrec_to_irec(rec, &irec);
4056 fa = xfs_alloc_check_irec(to_perag(cur->bc_group), &irec);
4057 if (fa)
4058 return xfs_alloc_complain_bad_rec(cur, fa, &irec);
4059
4060 return query->fn(cur, &irec, query->priv);
4061 }
4062
4063 /* Find all free space within a given range of blocks. */
4064 int
xfs_alloc_query_range(struct xfs_btree_cur * cur,const struct xfs_alloc_rec_incore * low_rec,const struct xfs_alloc_rec_incore * high_rec,xfs_alloc_query_range_fn fn,void * priv)4065 xfs_alloc_query_range(
4066 struct xfs_btree_cur *cur,
4067 const struct xfs_alloc_rec_incore *low_rec,
4068 const struct xfs_alloc_rec_incore *high_rec,
4069 xfs_alloc_query_range_fn fn,
4070 void *priv)
4071 {
4072 union xfs_btree_irec low_brec = { .a = *low_rec };
4073 union xfs_btree_irec high_brec = { .a = *high_rec };
4074 struct xfs_alloc_query_range_info query = { .priv = priv, .fn = fn };
4075
4076 ASSERT(xfs_btree_is_bno(cur->bc_ops));
4077 return xfs_btree_query_range(cur, &low_brec, &high_brec,
4078 xfs_alloc_query_range_helper, &query);
4079 }
4080
4081 /* Find all free space records. */
4082 int
xfs_alloc_query_all(struct xfs_btree_cur * cur,xfs_alloc_query_range_fn fn,void * priv)4083 xfs_alloc_query_all(
4084 struct xfs_btree_cur *cur,
4085 xfs_alloc_query_range_fn fn,
4086 void *priv)
4087 {
4088 struct xfs_alloc_query_range_info query;
4089
4090 ASSERT(xfs_btree_is_bno(cur->bc_ops));
4091 query.priv = priv;
4092 query.fn = fn;
4093 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
4094 }
4095
4096 /*
4097 * Scan part of the keyspace of the free space and tell us if the area has no
4098 * records, is fully mapped by records, or is partially filled.
4099 */
4100 int
xfs_alloc_has_records(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,enum xbtree_recpacking * outcome)4101 xfs_alloc_has_records(
4102 struct xfs_btree_cur *cur,
4103 xfs_agblock_t bno,
4104 xfs_extlen_t len,
4105 enum xbtree_recpacking *outcome)
4106 {
4107 union xfs_btree_irec low;
4108 union xfs_btree_irec high;
4109
4110 memset(&low, 0, sizeof(low));
4111 low.a.ar_startblock = bno;
4112 memset(&high, 0xFF, sizeof(high));
4113 high.a.ar_startblock = bno + len - 1;
4114
4115 return xfs_btree_has_records(cur, &low, &high, NULL, outcome);
4116 }
4117
4118 /*
4119 * Walk all the blocks in the AGFL. The @walk_fn can return any negative
4120 * error code or XFS_ITER_*.
4121 */
4122 int
xfs_agfl_walk(struct xfs_mount * mp,struct xfs_agf * agf,struct xfs_buf * agflbp,xfs_agfl_walk_fn walk_fn,void * priv)4123 xfs_agfl_walk(
4124 struct xfs_mount *mp,
4125 struct xfs_agf *agf,
4126 struct xfs_buf *agflbp,
4127 xfs_agfl_walk_fn walk_fn,
4128 void *priv)
4129 {
4130 __be32 *agfl_bno;
4131 unsigned int i;
4132 int error;
4133
4134 agfl_bno = xfs_buf_to_agfl_bno(agflbp);
4135 i = be32_to_cpu(agf->agf_flfirst);
4136
4137 /* Nothing to walk in an empty AGFL. */
4138 if (agf->agf_flcount == cpu_to_be32(0))
4139 return 0;
4140
4141 /* Otherwise, walk from first to last, wrapping as needed. */
4142 for (;;) {
4143 error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
4144 if (error)
4145 return error;
4146 if (i == be32_to_cpu(agf->agf_fllast))
4147 break;
4148 if (++i == xfs_agfl_size(mp))
4149 i = 0;
4150 }
4151
4152 return 0;
4153 }
4154
4155 int __init
xfs_extfree_intent_init_cache(void)4156 xfs_extfree_intent_init_cache(void)
4157 {
4158 xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent",
4159 sizeof(struct xfs_extent_free_item),
4160 0, 0, NULL);
4161
4162 return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM;
4163 }
4164
4165 void
xfs_extfree_intent_destroy_cache(void)4166 xfs_extfree_intent_destroy_cache(void)
4167 {
4168 kmem_cache_destroy(xfs_extfree_item_cache);
4169 xfs_extfree_item_cache = NULL;
4170 }
4171