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