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