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