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