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