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