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