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