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