Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 | 27 | #include "xfs_dir2.h" |
1da177e4 | 28 | #include "xfs_mount.h" |
1da177e4 | 29 | #include "xfs_bmap_btree.h" |
1da177e4 LT |
30 | #include "xfs_dinode.h" |
31 | #include "xfs_inode.h" | |
32 | #include "xfs_alloc.h" | |
33 | #include "xfs_bmap.h" | |
1da177e4 LT |
34 | #include "xfs_rtalloc.h" |
35 | #include "xfs_fsops.h" | |
36 | #include "xfs_error.h" | |
37 | #include "xfs_rw.h" | |
38 | #include "xfs_inode_item.h" | |
39 | #include "xfs_trans_space.h" | |
43355099 | 40 | #include "xfs_utils.h" |
0b1b213f | 41 | #include "xfs_trace.h" |
1922c949 | 42 | #include "xfs_buf.h" |
1da177e4 LT |
43 | |
44 | ||
45 | /* | |
46 | * Prototypes for internal functions. | |
47 | */ | |
48 | ||
49 | ||
50 | STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t, | |
51 | xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *); | |
52 | STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int, | |
53 | xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *); | |
54 | STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t, | |
55 | xfs_extlen_t, int, xfs_rtblock_t *, int *); | |
56 | STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t, | |
57 | xfs_rtblock_t, xfs_rtblock_t *); | |
58 | STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t, | |
59 | xfs_rtblock_t, xfs_rtblock_t *); | |
60 | STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int, | |
61 | xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *); | |
62 | STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t, | |
63 | xfs_extlen_t, int); | |
64 | STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int, | |
65 | xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *); | |
66 | ||
67 | /* | |
68 | * Internal functions. | |
69 | */ | |
70 | ||
1da177e4 LT |
71 | /* |
72 | * Allocate space to the bitmap or summary file, and zero it, for growfs. | |
73 | */ | |
74 | STATIC int /* error */ | |
75 | xfs_growfs_rt_alloc( | |
76 | xfs_mount_t *mp, /* file system mount point */ | |
77 | xfs_extlen_t oblocks, /* old count of blocks */ | |
78 | xfs_extlen_t nblocks, /* new count of blocks */ | |
79 | xfs_ino_t ino) /* inode number (bitmap/summary) */ | |
80 | { | |
81 | xfs_fileoff_t bno; /* block number in file */ | |
82 | xfs_buf_t *bp; /* temporary buffer for zeroing */ | |
1da177e4 LT |
83 | int committed; /* transaction committed flag */ |
84 | xfs_daddr_t d; /* disk block address */ | |
85 | int error; /* error return value */ | |
86 | xfs_fsblock_t firstblock; /* first block allocated in xaction */ | |
87 | xfs_bmap_free_t flist; /* list of freed blocks */ | |
88 | xfs_fsblock_t fsbno; /* filesystem block for bno */ | |
89 | xfs_inode_t *ip; /* pointer to incore inode */ | |
90 | xfs_bmbt_irec_t map; /* block map output */ | |
91 | int nmap; /* number of block maps */ | |
92 | int resblks; /* space reservation */ | |
1da177e4 LT |
93 | |
94 | /* | |
95 | * Allocate space to the file, as necessary. | |
96 | */ | |
97 | while (oblocks < nblocks) { | |
0924b585 DC |
98 | int cancelflags = 0; |
99 | xfs_trans_t *tp; | |
100 | ||
1da177e4 LT |
101 | tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC); |
102 | resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks); | |
1da177e4 LT |
103 | /* |
104 | * Reserve space & log for one extent added to the file. | |
105 | */ | |
106 | if ((error = xfs_trans_reserve(tp, resblks, | |
107 | XFS_GROWRTALLOC_LOG_RES(mp), 0, | |
108 | XFS_TRANS_PERM_LOG_RES, | |
109 | XFS_DEFAULT_PERM_LOG_COUNT))) | |
e5720eec | 110 | goto error_cancel; |
1da177e4 LT |
111 | cancelflags = XFS_TRANS_RELEASE_LOG_RES; |
112 | /* | |
113 | * Lock the inode. | |
114 | */ | |
07ff2fa8 NS |
115 | if ((error = xfs_trans_iget(mp, tp, ino, 0, |
116 | XFS_ILOCK_EXCL, &ip))) | |
e5720eec | 117 | goto error_cancel; |
9d87c319 | 118 | xfs_bmap_init(&flist, &firstblock); |
1da177e4 LT |
119 | /* |
120 | * Allocate blocks to the bitmap file. | |
121 | */ | |
122 | nmap = 1; | |
123 | cancelflags |= XFS_TRANS_ABORT; | |
124 | error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks, | |
125 | XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock, | |
b4e9181e | 126 | resblks, &map, &nmap, &flist); |
1da177e4 LT |
127 | if (!error && nmap < 1) |
128 | error = XFS_ERROR(ENOSPC); | |
129 | if (error) | |
e5720eec | 130 | goto error_cancel; |
1da177e4 LT |
131 | /* |
132 | * Free any blocks freed up in the transaction, then commit. | |
133 | */ | |
f7c99b6f | 134 | error = xfs_bmap_finish(&tp, &flist, &committed); |
1da177e4 | 135 | if (error) |
e5720eec DC |
136 | goto error_cancel; |
137 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
138 | if (error) | |
139 | goto error; | |
1da177e4 LT |
140 | /* |
141 | * Now we need to clear the allocated blocks. | |
142 | * Do this one block per transaction, to keep it simple. | |
143 | */ | |
144 | cancelflags = 0; | |
145 | for (bno = map.br_startoff, fsbno = map.br_startblock; | |
146 | bno < map.br_startoff + map.br_blockcount; | |
147 | bno++, fsbno++) { | |
148 | tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO); | |
149 | /* | |
150 | * Reserve log for one block zeroing. | |
151 | */ | |
152 | if ((error = xfs_trans_reserve(tp, 0, | |
153 | XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0))) | |
e5720eec | 154 | goto error_cancel; |
1da177e4 LT |
155 | /* |
156 | * Lock the bitmap inode. | |
157 | */ | |
07ff2fa8 NS |
158 | if ((error = xfs_trans_iget(mp, tp, ino, 0, |
159 | XFS_ILOCK_EXCL, &ip))) | |
e5720eec | 160 | goto error_cancel; |
1da177e4 LT |
161 | /* |
162 | * Get a buffer for the block. | |
163 | */ | |
164 | d = XFS_FSB_TO_DADDR(mp, fsbno); | |
165 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, | |
166 | mp->m_bsize, 0); | |
167 | if (bp == NULL) { | |
168 | error = XFS_ERROR(EIO); | |
0924b585 DC |
169 | error_cancel: |
170 | xfs_trans_cancel(tp, cancelflags); | |
171 | goto error; | |
1da177e4 LT |
172 | } |
173 | memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize); | |
174 | xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1); | |
175 | /* | |
176 | * Commit the transaction. | |
177 | */ | |
e5720eec DC |
178 | error = xfs_trans_commit(tp, 0); |
179 | if (error) | |
180 | goto error; | |
1da177e4 LT |
181 | } |
182 | /* | |
183 | * Go on to the next extent, if any. | |
184 | */ | |
185 | oblocks = map.br_startoff + map.br_blockcount; | |
186 | } | |
187 | return 0; | |
e5720eec | 188 | error: |
1da177e4 LT |
189 | return error; |
190 | } | |
191 | ||
192 | /* | |
193 | * Attempt to allocate an extent minlen<=len<=maxlen starting from | |
194 | * bitmap block bbno. If we don't get maxlen then use prod to trim | |
195 | * the length, if given. Returns error; returns starting block in *rtblock. | |
196 | * The lengths are all in rtextents. | |
197 | */ | |
198 | STATIC int /* error */ | |
199 | xfs_rtallocate_extent_block( | |
200 | xfs_mount_t *mp, /* file system mount point */ | |
201 | xfs_trans_t *tp, /* transaction pointer */ | |
202 | xfs_rtblock_t bbno, /* bitmap block number */ | |
203 | xfs_extlen_t minlen, /* minimum length to allocate */ | |
204 | xfs_extlen_t maxlen, /* maximum length to allocate */ | |
205 | xfs_extlen_t *len, /* out: actual length allocated */ | |
206 | xfs_rtblock_t *nextp, /* out: next block to try */ | |
207 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
208 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
209 | xfs_extlen_t prod, /* extent product factor */ | |
210 | xfs_rtblock_t *rtblock) /* out: start block allocated */ | |
211 | { | |
212 | xfs_rtblock_t besti; /* best rtblock found so far */ | |
213 | xfs_rtblock_t bestlen; /* best length found so far */ | |
214 | xfs_rtblock_t end; /* last rtblock in chunk */ | |
215 | int error; /* error value */ | |
216 | xfs_rtblock_t i; /* current rtblock trying */ | |
217 | xfs_rtblock_t next; /* next rtblock to try */ | |
218 | int stat; /* status from internal calls */ | |
219 | ||
220 | /* | |
221 | * Loop over all the extents starting in this bitmap block, | |
222 | * looking for one that's long enough. | |
223 | */ | |
224 | for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0, | |
225 | end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1; | |
226 | i <= end; | |
227 | i++) { | |
228 | /* | |
229 | * See if there's a free extent of maxlen starting at i. | |
230 | * If it's not so then next will contain the first non-free. | |
231 | */ | |
232 | error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat); | |
233 | if (error) { | |
234 | return error; | |
235 | } | |
236 | if (stat) { | |
237 | /* | |
238 | * i for maxlen is all free, allocate and return that. | |
239 | */ | |
240 | error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp, | |
241 | rsb); | |
242 | if (error) { | |
243 | return error; | |
244 | } | |
245 | *len = maxlen; | |
246 | *rtblock = i; | |
247 | return 0; | |
248 | } | |
249 | /* | |
250 | * In the case where we have a variable-sized allocation | |
251 | * request, figure out how big this free piece is, | |
252 | * and if it's big enough for the minimum, and the best | |
253 | * so far, remember it. | |
254 | */ | |
255 | if (minlen < maxlen) { | |
256 | xfs_rtblock_t thislen; /* this extent size */ | |
257 | ||
258 | thislen = next - i; | |
259 | if (thislen >= minlen && thislen > bestlen) { | |
260 | besti = i; | |
261 | bestlen = thislen; | |
262 | } | |
263 | } | |
264 | /* | |
265 | * If not done yet, find the start of the next free space. | |
266 | */ | |
267 | if (next < end) { | |
268 | error = xfs_rtfind_forw(mp, tp, next, end, &i); | |
269 | if (error) { | |
270 | return error; | |
271 | } | |
272 | } else | |
273 | break; | |
274 | } | |
275 | /* | |
276 | * Searched the whole thing & didn't find a maxlen free extent. | |
277 | */ | |
278 | if (minlen < maxlen && besti != -1) { | |
279 | xfs_extlen_t p; /* amount to trim length by */ | |
280 | ||
281 | /* | |
282 | * If size should be a multiple of prod, make that so. | |
283 | */ | |
284 | if (prod > 1 && (p = do_mod(bestlen, prod))) | |
285 | bestlen -= p; | |
286 | /* | |
287 | * Allocate besti for bestlen & return that. | |
288 | */ | |
289 | error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb); | |
290 | if (error) { | |
291 | return error; | |
292 | } | |
293 | *len = bestlen; | |
294 | *rtblock = besti; | |
295 | return 0; | |
296 | } | |
297 | /* | |
298 | * Allocation failed. Set *nextp to the next block to try. | |
299 | */ | |
300 | *nextp = next; | |
301 | *rtblock = NULLRTBLOCK; | |
302 | return 0; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Allocate an extent of length minlen<=len<=maxlen, starting at block | |
307 | * bno. If we don't get maxlen then use prod to trim the length, if given. | |
308 | * Returns error; returns starting block in *rtblock. | |
309 | * The lengths are all in rtextents. | |
310 | */ | |
311 | STATIC int /* error */ | |
312 | xfs_rtallocate_extent_exact( | |
313 | xfs_mount_t *mp, /* file system mount point */ | |
314 | xfs_trans_t *tp, /* transaction pointer */ | |
315 | xfs_rtblock_t bno, /* starting block number to allocate */ | |
316 | xfs_extlen_t minlen, /* minimum length to allocate */ | |
317 | xfs_extlen_t maxlen, /* maximum length to allocate */ | |
318 | xfs_extlen_t *len, /* out: actual length allocated */ | |
319 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
320 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
321 | xfs_extlen_t prod, /* extent product factor */ | |
322 | xfs_rtblock_t *rtblock) /* out: start block allocated */ | |
323 | { | |
324 | int error; /* error value */ | |
325 | xfs_extlen_t i; /* extent length trimmed due to prod */ | |
326 | int isfree; /* extent is free */ | |
327 | xfs_rtblock_t next; /* next block to try (dummy) */ | |
328 | ||
329 | ASSERT(minlen % prod == 0 && maxlen % prod == 0); | |
330 | /* | |
331 | * Check if the range in question (for maxlen) is free. | |
332 | */ | |
333 | error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree); | |
334 | if (error) { | |
335 | return error; | |
336 | } | |
337 | if (isfree) { | |
338 | /* | |
339 | * If it is, allocate it and return success. | |
340 | */ | |
341 | error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb); | |
342 | if (error) { | |
343 | return error; | |
344 | } | |
345 | *len = maxlen; | |
346 | *rtblock = bno; | |
347 | return 0; | |
348 | } | |
349 | /* | |
350 | * If not, allocate what there is, if it's at least minlen. | |
351 | */ | |
352 | maxlen = next - bno; | |
353 | if (maxlen < minlen) { | |
354 | /* | |
355 | * Failed, return failure status. | |
356 | */ | |
357 | *rtblock = NULLRTBLOCK; | |
358 | return 0; | |
359 | } | |
360 | /* | |
361 | * Trim off tail of extent, if prod is specified. | |
362 | */ | |
363 | if (prod > 1 && (i = maxlen % prod)) { | |
364 | maxlen -= i; | |
365 | if (maxlen < minlen) { | |
366 | /* | |
367 | * Now we can't do it, return failure status. | |
368 | */ | |
369 | *rtblock = NULLRTBLOCK; | |
370 | return 0; | |
371 | } | |
372 | } | |
373 | /* | |
374 | * Allocate what we can and return it. | |
375 | */ | |
376 | error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb); | |
377 | if (error) { | |
378 | return error; | |
379 | } | |
380 | *len = maxlen; | |
381 | *rtblock = bno; | |
382 | return 0; | |
383 | } | |
384 | ||
385 | /* | |
386 | * Allocate an extent of length minlen<=len<=maxlen, starting as near | |
387 | * to bno as possible. If we don't get maxlen then use prod to trim | |
388 | * the length, if given. The lengths are all in rtextents. | |
389 | */ | |
390 | STATIC int /* error */ | |
391 | xfs_rtallocate_extent_near( | |
392 | xfs_mount_t *mp, /* file system mount point */ | |
393 | xfs_trans_t *tp, /* transaction pointer */ | |
394 | xfs_rtblock_t bno, /* starting block number to allocate */ | |
395 | xfs_extlen_t minlen, /* minimum length to allocate */ | |
396 | xfs_extlen_t maxlen, /* maximum length to allocate */ | |
397 | xfs_extlen_t *len, /* out: actual length allocated */ | |
398 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
399 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
400 | xfs_extlen_t prod, /* extent product factor */ | |
401 | xfs_rtblock_t *rtblock) /* out: start block allocated */ | |
402 | { | |
403 | int any; /* any useful extents from summary */ | |
404 | xfs_rtblock_t bbno; /* bitmap block number */ | |
405 | int error; /* error value */ | |
406 | int i; /* bitmap block offset (loop control) */ | |
407 | int j; /* secondary loop control */ | |
408 | int log2len; /* log2 of minlen */ | |
409 | xfs_rtblock_t n; /* next block to try */ | |
410 | xfs_rtblock_t r; /* result block */ | |
411 | ||
412 | ASSERT(minlen % prod == 0 && maxlen % prod == 0); | |
413 | /* | |
414 | * If the block number given is off the end, silently set it to | |
415 | * the last block. | |
416 | */ | |
417 | if (bno >= mp->m_sb.sb_rextents) | |
418 | bno = mp->m_sb.sb_rextents - 1; | |
419 | /* | |
420 | * Try the exact allocation first. | |
421 | */ | |
422 | error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len, | |
423 | rbpp, rsb, prod, &r); | |
424 | if (error) { | |
425 | return error; | |
426 | } | |
427 | /* | |
428 | * If the exact allocation worked, return that. | |
429 | */ | |
430 | if (r != NULLRTBLOCK) { | |
431 | *rtblock = r; | |
432 | return 0; | |
433 | } | |
434 | bbno = XFS_BITTOBLOCK(mp, bno); | |
435 | i = 0; | |
79071eb0 | 436 | ASSERT(minlen != 0); |
1da177e4 LT |
437 | log2len = xfs_highbit32(minlen); |
438 | /* | |
439 | * Loop over all bitmap blocks (bbno + i is current block). | |
440 | */ | |
441 | for (;;) { | |
442 | /* | |
443 | * Get summary information of extents of all useful levels | |
444 | * starting in this bitmap block. | |
445 | */ | |
446 | error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1, | |
447 | bbno + i, rbpp, rsb, &any); | |
448 | if (error) { | |
449 | return error; | |
450 | } | |
451 | /* | |
452 | * If there are any useful extents starting here, try | |
453 | * allocating one. | |
454 | */ | |
455 | if (any) { | |
456 | /* | |
457 | * On the positive side of the starting location. | |
458 | */ | |
459 | if (i >= 0) { | |
460 | /* | |
461 | * Try to allocate an extent starting in | |
462 | * this block. | |
463 | */ | |
464 | error = xfs_rtallocate_extent_block(mp, tp, | |
465 | bbno + i, minlen, maxlen, len, &n, rbpp, | |
466 | rsb, prod, &r); | |
467 | if (error) { | |
468 | return error; | |
469 | } | |
470 | /* | |
471 | * If it worked, return it. | |
472 | */ | |
473 | if (r != NULLRTBLOCK) { | |
474 | *rtblock = r; | |
475 | return 0; | |
476 | } | |
477 | } | |
478 | /* | |
479 | * On the negative side of the starting location. | |
480 | */ | |
481 | else { /* i < 0 */ | |
482 | /* | |
483 | * Loop backwards through the bitmap blocks from | |
484 | * the starting point-1 up to where we are now. | |
485 | * There should be an extent which ends in this | |
486 | * bitmap block and is long enough. | |
487 | */ | |
488 | for (j = -1; j > i; j--) { | |
489 | /* | |
490 | * Grab the summary information for | |
491 | * this bitmap block. | |
492 | */ | |
493 | error = xfs_rtany_summary(mp, tp, | |
494 | log2len, mp->m_rsumlevels - 1, | |
495 | bbno + j, rbpp, rsb, &any); | |
496 | if (error) { | |
497 | return error; | |
498 | } | |
499 | /* | |
500 | * If there's no extent given in the | |
501 | * summary that means the extent we | |
502 | * found must carry over from an | |
503 | * earlier block. If there is an | |
504 | * extent given, we've already tried | |
505 | * that allocation, don't do it again. | |
506 | */ | |
507 | if (any) | |
508 | continue; | |
509 | error = xfs_rtallocate_extent_block(mp, | |
510 | tp, bbno + j, minlen, maxlen, | |
511 | len, &n, rbpp, rsb, prod, &r); | |
512 | if (error) { | |
513 | return error; | |
514 | } | |
515 | /* | |
516 | * If it works, return the extent. | |
517 | */ | |
518 | if (r != NULLRTBLOCK) { | |
519 | *rtblock = r; | |
520 | return 0; | |
521 | } | |
522 | } | |
523 | /* | |
524 | * There weren't intervening bitmap blocks | |
525 | * with a long enough extent, or the | |
526 | * allocation didn't work for some reason | |
527 | * (i.e. it's a little * too short). | |
528 | * Try to allocate from the summary block | |
529 | * that we found. | |
530 | */ | |
531 | error = xfs_rtallocate_extent_block(mp, tp, | |
532 | bbno + i, minlen, maxlen, len, &n, rbpp, | |
533 | rsb, prod, &r); | |
534 | if (error) { | |
535 | return error; | |
536 | } | |
537 | /* | |
538 | * If it works, return the extent. | |
539 | */ | |
540 | if (r != NULLRTBLOCK) { | |
541 | *rtblock = r; | |
542 | return 0; | |
543 | } | |
544 | } | |
545 | } | |
546 | /* | |
547 | * Loop control. If we were on the positive side, and there's | |
548 | * still more blocks on the negative side, go there. | |
549 | */ | |
550 | if (i > 0 && (int)bbno - i >= 0) | |
551 | i = -i; | |
552 | /* | |
553 | * If positive, and no more negative, but there are more | |
554 | * positive, go there. | |
555 | */ | |
556 | else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1) | |
557 | i++; | |
558 | /* | |
559 | * If negative or 0 (just started), and there are positive | |
560 | * blocks to go, go there. The 0 case moves to block 1. | |
561 | */ | |
562 | else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1) | |
563 | i = 1 - i; | |
564 | /* | |
565 | * If negative or 0 and there are more negative blocks, | |
566 | * go there. | |
567 | */ | |
568 | else if (i <= 0 && (int)bbno + i > 0) | |
569 | i--; | |
570 | /* | |
571 | * Must be done. Return failure. | |
572 | */ | |
573 | else | |
574 | break; | |
575 | } | |
576 | *rtblock = NULLRTBLOCK; | |
577 | return 0; | |
578 | } | |
579 | ||
580 | /* | |
581 | * Allocate an extent of length minlen<=len<=maxlen, with no position | |
582 | * specified. If we don't get maxlen then use prod to trim | |
583 | * the length, if given. The lengths are all in rtextents. | |
584 | */ | |
585 | STATIC int /* error */ | |
586 | xfs_rtallocate_extent_size( | |
587 | xfs_mount_t *mp, /* file system mount point */ | |
588 | xfs_trans_t *tp, /* transaction pointer */ | |
589 | xfs_extlen_t minlen, /* minimum length to allocate */ | |
590 | xfs_extlen_t maxlen, /* maximum length to allocate */ | |
591 | xfs_extlen_t *len, /* out: actual length allocated */ | |
592 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
593 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
594 | xfs_extlen_t prod, /* extent product factor */ | |
595 | xfs_rtblock_t *rtblock) /* out: start block allocated */ | |
596 | { | |
597 | int error; /* error value */ | |
598 | int i; /* bitmap block number */ | |
599 | int l; /* level number (loop control) */ | |
600 | xfs_rtblock_t n; /* next block to be tried */ | |
601 | xfs_rtblock_t r; /* result block number */ | |
602 | xfs_suminfo_t sum; /* summary information for extents */ | |
603 | ||
604 | ASSERT(minlen % prod == 0 && maxlen % prod == 0); | |
79071eb0 DC |
605 | ASSERT(maxlen != 0); |
606 | ||
1da177e4 LT |
607 | /* |
608 | * Loop over all the levels starting with maxlen. | |
609 | * At each level, look at all the bitmap blocks, to see if there | |
610 | * are extents starting there that are long enough (>= maxlen). | |
611 | * Note, only on the initial level can the allocation fail if | |
612 | * the summary says there's an extent. | |
613 | */ | |
614 | for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) { | |
615 | /* | |
616 | * Loop over all the bitmap blocks. | |
617 | */ | |
618 | for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) { | |
619 | /* | |
620 | * Get the summary for this level/block. | |
621 | */ | |
622 | error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb, | |
623 | &sum); | |
624 | if (error) { | |
625 | return error; | |
626 | } | |
627 | /* | |
628 | * Nothing there, on to the next block. | |
629 | */ | |
630 | if (!sum) | |
631 | continue; | |
632 | /* | |
633 | * Try allocating the extent. | |
634 | */ | |
635 | error = xfs_rtallocate_extent_block(mp, tp, i, maxlen, | |
636 | maxlen, len, &n, rbpp, rsb, prod, &r); | |
637 | if (error) { | |
638 | return error; | |
639 | } | |
640 | /* | |
641 | * If it worked, return that. | |
642 | */ | |
643 | if (r != NULLRTBLOCK) { | |
644 | *rtblock = r; | |
645 | return 0; | |
646 | } | |
647 | /* | |
648 | * If the "next block to try" returned from the | |
649 | * allocator is beyond the next bitmap block, | |
650 | * skip to that bitmap block. | |
651 | */ | |
652 | if (XFS_BITTOBLOCK(mp, n) > i + 1) | |
653 | i = XFS_BITTOBLOCK(mp, n) - 1; | |
654 | } | |
655 | } | |
656 | /* | |
657 | * Didn't find any maxlen blocks. Try smaller ones, unless | |
658 | * we're asking for a fixed size extent. | |
659 | */ | |
660 | if (minlen > --maxlen) { | |
661 | *rtblock = NULLRTBLOCK; | |
662 | return 0; | |
663 | } | |
79071eb0 DC |
664 | ASSERT(minlen != 0); |
665 | ASSERT(maxlen != 0); | |
666 | ||
1da177e4 LT |
667 | /* |
668 | * Loop over sizes, from maxlen down to minlen. | |
669 | * This time, when we do the allocations, allow smaller ones | |
670 | * to succeed. | |
671 | */ | |
672 | for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) { | |
673 | /* | |
674 | * Loop over all the bitmap blocks, try an allocation | |
675 | * starting in that block. | |
676 | */ | |
677 | for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) { | |
678 | /* | |
679 | * Get the summary information for this level/block. | |
680 | */ | |
681 | error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb, | |
682 | &sum); | |
683 | if (error) { | |
684 | return error; | |
685 | } | |
686 | /* | |
687 | * If nothing there, go on to next. | |
688 | */ | |
689 | if (!sum) | |
690 | continue; | |
691 | /* | |
692 | * Try the allocation. Make sure the specified | |
693 | * minlen/maxlen are in the possible range for | |
694 | * this summary level. | |
695 | */ | |
696 | error = xfs_rtallocate_extent_block(mp, tp, i, | |
697 | XFS_RTMAX(minlen, 1 << l), | |
698 | XFS_RTMIN(maxlen, (1 << (l + 1)) - 1), | |
699 | len, &n, rbpp, rsb, prod, &r); | |
700 | if (error) { | |
701 | return error; | |
702 | } | |
703 | /* | |
704 | * If it worked, return that extent. | |
705 | */ | |
706 | if (r != NULLRTBLOCK) { | |
707 | *rtblock = r; | |
708 | return 0; | |
709 | } | |
710 | /* | |
711 | * If the "next block to try" returned from the | |
712 | * allocator is beyond the next bitmap block, | |
713 | * skip to that bitmap block. | |
714 | */ | |
715 | if (XFS_BITTOBLOCK(mp, n) > i + 1) | |
716 | i = XFS_BITTOBLOCK(mp, n) - 1; | |
717 | } | |
718 | } | |
719 | /* | |
720 | * Got nothing, return failure. | |
721 | */ | |
722 | *rtblock = NULLRTBLOCK; | |
723 | return 0; | |
724 | } | |
725 | ||
726 | /* | |
727 | * Mark an extent specified by start and len allocated. | |
728 | * Updates all the summary information as well as the bitmap. | |
729 | */ | |
730 | STATIC int /* error */ | |
731 | xfs_rtallocate_range( | |
732 | xfs_mount_t *mp, /* file system mount point */ | |
733 | xfs_trans_t *tp, /* transaction pointer */ | |
734 | xfs_rtblock_t start, /* start block to allocate */ | |
735 | xfs_extlen_t len, /* length to allocate */ | |
736 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
737 | xfs_fsblock_t *rsb) /* in/out: summary block number */ | |
738 | { | |
739 | xfs_rtblock_t end; /* end of the allocated extent */ | |
740 | int error; /* error value */ | |
741 | xfs_rtblock_t postblock; /* first block allocated > end */ | |
742 | xfs_rtblock_t preblock; /* first block allocated < start */ | |
743 | ||
744 | end = start + len - 1; | |
745 | /* | |
746 | * Assume we're allocating out of the middle of a free extent. | |
747 | * We need to find the beginning and end of the extent so we can | |
748 | * properly update the summary. | |
749 | */ | |
750 | error = xfs_rtfind_back(mp, tp, start, 0, &preblock); | |
751 | if (error) { | |
752 | return error; | |
753 | } | |
754 | /* | |
755 | * Find the next allocated block (end of free extent). | |
756 | */ | |
757 | error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1, | |
758 | &postblock); | |
759 | if (error) { | |
760 | return error; | |
761 | } | |
762 | /* | |
763 | * Decrement the summary information corresponding to the entire | |
764 | * (old) free extent. | |
765 | */ | |
766 | error = xfs_rtmodify_summary(mp, tp, | |
767 | XFS_RTBLOCKLOG(postblock + 1 - preblock), | |
768 | XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb); | |
769 | if (error) { | |
770 | return error; | |
771 | } | |
772 | /* | |
773 | * If there are blocks not being allocated at the front of the | |
774 | * old extent, add summary data for them to be free. | |
775 | */ | |
776 | if (preblock < start) { | |
777 | error = xfs_rtmodify_summary(mp, tp, | |
778 | XFS_RTBLOCKLOG(start - preblock), | |
779 | XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb); | |
780 | if (error) { | |
781 | return error; | |
782 | } | |
783 | } | |
784 | /* | |
785 | * If there are blocks not being allocated at the end of the | |
786 | * old extent, add summary data for them to be free. | |
787 | */ | |
788 | if (postblock > end) { | |
789 | error = xfs_rtmodify_summary(mp, tp, | |
790 | XFS_RTBLOCKLOG(postblock - end), | |
791 | XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb); | |
792 | if (error) { | |
793 | return error; | |
794 | } | |
795 | } | |
796 | /* | |
797 | * Modify the bitmap to mark this extent allocated. | |
798 | */ | |
799 | error = xfs_rtmodify_range(mp, tp, start, len, 0); | |
800 | return error; | |
801 | } | |
802 | ||
803 | /* | |
804 | * Return whether there are any free extents in the size range given | |
805 | * by low and high, for the bitmap block bbno. | |
806 | */ | |
807 | STATIC int /* error */ | |
808 | xfs_rtany_summary( | |
809 | xfs_mount_t *mp, /* file system mount structure */ | |
810 | xfs_trans_t *tp, /* transaction pointer */ | |
811 | int low, /* low log2 extent size */ | |
812 | int high, /* high log2 extent size */ | |
813 | xfs_rtblock_t bbno, /* bitmap block number */ | |
814 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
815 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
816 | int *stat) /* out: any good extents here? */ | |
817 | { | |
818 | int error; /* error value */ | |
819 | int log; /* loop counter, log2 of ext. size */ | |
820 | xfs_suminfo_t sum; /* summary data */ | |
821 | ||
822 | /* | |
823 | * Loop over logs of extent sizes. Order is irrelevant. | |
824 | */ | |
825 | for (log = low; log <= high; log++) { | |
826 | /* | |
827 | * Get one summary datum. | |
828 | */ | |
829 | error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum); | |
830 | if (error) { | |
831 | return error; | |
832 | } | |
833 | /* | |
834 | * If there are any, return success. | |
835 | */ | |
836 | if (sum) { | |
837 | *stat = 1; | |
838 | return 0; | |
839 | } | |
840 | } | |
841 | /* | |
842 | * Found nothing, return failure. | |
843 | */ | |
844 | *stat = 0; | |
845 | return 0; | |
846 | } | |
847 | ||
848 | /* | |
849 | * Get a buffer for the bitmap or summary file block specified. | |
850 | * The buffer is returned read and locked. | |
851 | */ | |
852 | STATIC int /* error */ | |
853 | xfs_rtbuf_get( | |
854 | xfs_mount_t *mp, /* file system mount structure */ | |
855 | xfs_trans_t *tp, /* transaction pointer */ | |
856 | xfs_rtblock_t block, /* block number in bitmap or summary */ | |
857 | int issum, /* is summary not bitmap */ | |
858 | xfs_buf_t **bpp) /* output: buffer for the block */ | |
859 | { | |
860 | xfs_buf_t *bp; /* block buffer, result */ | |
861 | xfs_daddr_t d; /* disk addr of block */ | |
862 | int error; /* error value */ | |
863 | xfs_fsblock_t fsb; /* fs block number for block */ | |
864 | xfs_inode_t *ip; /* bitmap or summary inode */ | |
865 | ||
866 | ip = issum ? mp->m_rsumip : mp->m_rbmip; | |
867 | /* | |
868 | * Map from the file offset (block) and inode number to the | |
869 | * file system block. | |
870 | */ | |
871 | error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block); | |
872 | if (error) { | |
873 | return error; | |
874 | } | |
875 | ASSERT(fsb != NULLFSBLOCK); | |
876 | /* | |
877 | * Convert to disk address for buffer cache. | |
878 | */ | |
879 | d = XFS_FSB_TO_DADDR(mp, fsb); | |
880 | /* | |
881 | * Read the buffer. | |
882 | */ | |
883 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, | |
884 | mp->m_bsize, 0, &bp); | |
885 | if (error) { | |
886 | return error; | |
887 | } | |
888 | ASSERT(bp && !XFS_BUF_GETERROR(bp)); | |
889 | *bpp = bp; | |
890 | return 0; | |
891 | } | |
892 | ||
893 | #ifdef DEBUG | |
894 | /* | |
895 | * Check that the given extent (block range) is allocated already. | |
896 | */ | |
897 | STATIC int /* error */ | |
898 | xfs_rtcheck_alloc_range( | |
899 | xfs_mount_t *mp, /* file system mount point */ | |
900 | xfs_trans_t *tp, /* transaction pointer */ | |
901 | xfs_rtblock_t bno, /* starting block number of extent */ | |
902 | xfs_extlen_t len, /* length of extent */ | |
903 | int *stat) /* out: 1 for allocated, 0 for not */ | |
904 | { | |
905 | xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */ | |
906 | ||
907 | return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat); | |
908 | } | |
909 | #endif | |
910 | ||
1da177e4 LT |
911 | /* |
912 | * Check that the given range is either all allocated (val = 0) or | |
913 | * all free (val = 1). | |
914 | */ | |
915 | STATIC int /* error */ | |
916 | xfs_rtcheck_range( | |
917 | xfs_mount_t *mp, /* file system mount point */ | |
918 | xfs_trans_t *tp, /* transaction pointer */ | |
919 | xfs_rtblock_t start, /* starting block number of extent */ | |
920 | xfs_extlen_t len, /* length of extent */ | |
921 | int val, /* 1 for free, 0 for allocated */ | |
922 | xfs_rtblock_t *new, /* out: first block not matching */ | |
923 | int *stat) /* out: 1 for matches, 0 for not */ | |
924 | { | |
925 | xfs_rtword_t *b; /* current word in buffer */ | |
926 | int bit; /* bit number in the word */ | |
927 | xfs_rtblock_t block; /* bitmap block number */ | |
928 | xfs_buf_t *bp; /* buf for the block */ | |
929 | xfs_rtword_t *bufp; /* starting word in buffer */ | |
930 | int error; /* error value */ | |
931 | xfs_rtblock_t i; /* current bit number rel. to start */ | |
932 | xfs_rtblock_t lastbit; /* last useful bit in word */ | |
933 | xfs_rtword_t mask; /* mask of relevant bits for value */ | |
934 | xfs_rtword_t wdiff; /* difference from wanted value */ | |
935 | int word; /* word number in the buffer */ | |
936 | ||
937 | /* | |
938 | * Compute starting bitmap block number | |
939 | */ | |
940 | block = XFS_BITTOBLOCK(mp, start); | |
941 | /* | |
942 | * Read the bitmap block. | |
943 | */ | |
944 | error = xfs_rtbuf_get(mp, tp, block, 0, &bp); | |
945 | if (error) { | |
946 | return error; | |
947 | } | |
948 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
949 | /* | |
950 | * Compute the starting word's address, and starting bit. | |
951 | */ | |
952 | word = XFS_BITTOWORD(mp, start); | |
953 | b = &bufp[word]; | |
954 | bit = (int)(start & (XFS_NBWORD - 1)); | |
955 | /* | |
956 | * 0 (allocated) => all zero's; 1 (free) => all one's. | |
957 | */ | |
958 | val = -val; | |
959 | /* | |
960 | * If not starting on a word boundary, deal with the first | |
961 | * (partial) word. | |
962 | */ | |
963 | if (bit) { | |
964 | /* | |
965 | * Compute first bit not examined. | |
966 | */ | |
967 | lastbit = XFS_RTMIN(bit + len, XFS_NBWORD); | |
968 | /* | |
969 | * Mask of relevant bits. | |
970 | */ | |
971 | mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; | |
972 | /* | |
973 | * Compute difference between actual and desired value. | |
974 | */ | |
975 | if ((wdiff = (*b ^ val) & mask)) { | |
976 | /* | |
977 | * Different, compute first wrong bit and return. | |
978 | */ | |
979 | xfs_trans_brelse(tp, bp); | |
980 | i = XFS_RTLOBIT(wdiff) - bit; | |
981 | *new = start + i; | |
982 | *stat = 0; | |
983 | return 0; | |
984 | } | |
985 | i = lastbit - bit; | |
986 | /* | |
987 | * Go on to next block if that's where the next word is | |
988 | * and we need the next word. | |
989 | */ | |
990 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
991 | /* | |
992 | * If done with this block, get the next one. | |
993 | */ | |
994 | xfs_trans_brelse(tp, bp); | |
995 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
996 | if (error) { | |
997 | return error; | |
998 | } | |
999 | b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1000 | word = 0; | |
1001 | } else { | |
1002 | /* | |
1003 | * Go on to the next word in the buffer. | |
1004 | */ | |
1005 | b++; | |
1006 | } | |
1007 | } else { | |
1008 | /* | |
1009 | * Starting on a word boundary, no partial word. | |
1010 | */ | |
1011 | i = 0; | |
1012 | } | |
1013 | /* | |
1014 | * Loop over whole words in buffers. When we use up one buffer | |
1015 | * we move on to the next one. | |
1016 | */ | |
1017 | while (len - i >= XFS_NBWORD) { | |
1018 | /* | |
1019 | * Compute difference between actual and desired value. | |
1020 | */ | |
1021 | if ((wdiff = *b ^ val)) { | |
1022 | /* | |
1023 | * Different, compute first wrong bit and return. | |
1024 | */ | |
1025 | xfs_trans_brelse(tp, bp); | |
1026 | i += XFS_RTLOBIT(wdiff); | |
1027 | *new = start + i; | |
1028 | *stat = 0; | |
1029 | return 0; | |
1030 | } | |
1031 | i += XFS_NBWORD; | |
1032 | /* | |
1033 | * Go on to next block if that's where the next word is | |
1034 | * and we need the next word. | |
1035 | */ | |
1036 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
1037 | /* | |
1038 | * If done with this block, get the next one. | |
1039 | */ | |
1040 | xfs_trans_brelse(tp, bp); | |
1041 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
1042 | if (error) { | |
1043 | return error; | |
1044 | } | |
1045 | b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1046 | word = 0; | |
1047 | } else { | |
1048 | /* | |
1049 | * Go on to the next word in the buffer. | |
1050 | */ | |
1051 | b++; | |
1052 | } | |
1053 | } | |
1054 | /* | |
1055 | * If not ending on a word boundary, deal with the last | |
1056 | * (partial) word. | |
1057 | */ | |
1058 | if ((lastbit = len - i)) { | |
1059 | /* | |
1060 | * Mask of relevant bits. | |
1061 | */ | |
1062 | mask = ((xfs_rtword_t)1 << lastbit) - 1; | |
1063 | /* | |
1064 | * Compute difference between actual and desired value. | |
1065 | */ | |
1066 | if ((wdiff = (*b ^ val) & mask)) { | |
1067 | /* | |
1068 | * Different, compute first wrong bit and return. | |
1069 | */ | |
1070 | xfs_trans_brelse(tp, bp); | |
1071 | i += XFS_RTLOBIT(wdiff); | |
1072 | *new = start + i; | |
1073 | *stat = 0; | |
1074 | return 0; | |
1075 | } else | |
1076 | i = len; | |
1077 | } | |
1078 | /* | |
1079 | * Successful, return. | |
1080 | */ | |
1081 | xfs_trans_brelse(tp, bp); | |
1082 | *new = start + i; | |
1083 | *stat = 1; | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | /* | |
1088 | * Copy and transform the summary file, given the old and new | |
1089 | * parameters in the mount structures. | |
1090 | */ | |
1091 | STATIC int /* error */ | |
1092 | xfs_rtcopy_summary( | |
1093 | xfs_mount_t *omp, /* old file system mount point */ | |
1094 | xfs_mount_t *nmp, /* new file system mount point */ | |
1095 | xfs_trans_t *tp) /* transaction pointer */ | |
1096 | { | |
1097 | xfs_rtblock_t bbno; /* bitmap block number */ | |
1098 | xfs_buf_t *bp; /* summary buffer */ | |
1099 | int error; /* error return value */ | |
1100 | int log; /* summary level number (log length) */ | |
1101 | xfs_suminfo_t sum; /* summary data */ | |
1102 | xfs_fsblock_t sumbno; /* summary block number */ | |
1103 | ||
1104 | bp = NULL; | |
1105 | for (log = omp->m_rsumlevels - 1; log >= 0; log--) { | |
1106 | for (bbno = omp->m_sb.sb_rbmblocks - 1; | |
1107 | (xfs_srtblock_t)bbno >= 0; | |
1108 | bbno--) { | |
1109 | error = xfs_rtget_summary(omp, tp, log, bbno, &bp, | |
1110 | &sumbno, &sum); | |
1111 | if (error) | |
1112 | return error; | |
1113 | if (sum == 0) | |
1114 | continue; | |
1115 | error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum, | |
1116 | &bp, &sumbno); | |
1117 | if (error) | |
1118 | return error; | |
1119 | error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum, | |
1120 | &bp, &sumbno); | |
1121 | if (error) | |
1122 | return error; | |
1123 | ASSERT(sum > 0); | |
1124 | } | |
1125 | } | |
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Searching backward from start to limit, find the first block whose | |
1131 | * allocated/free state is different from start's. | |
1132 | */ | |
1133 | STATIC int /* error */ | |
1134 | xfs_rtfind_back( | |
1135 | xfs_mount_t *mp, /* file system mount point */ | |
1136 | xfs_trans_t *tp, /* transaction pointer */ | |
1137 | xfs_rtblock_t start, /* starting block to look at */ | |
1138 | xfs_rtblock_t limit, /* last block to look at */ | |
1139 | xfs_rtblock_t *rtblock) /* out: start block found */ | |
1140 | { | |
1141 | xfs_rtword_t *b; /* current word in buffer */ | |
1142 | int bit; /* bit number in the word */ | |
1143 | xfs_rtblock_t block; /* bitmap block number */ | |
1144 | xfs_buf_t *bp; /* buf for the block */ | |
1145 | xfs_rtword_t *bufp; /* starting word in buffer */ | |
1146 | int error; /* error value */ | |
1147 | xfs_rtblock_t firstbit; /* first useful bit in the word */ | |
1148 | xfs_rtblock_t i; /* current bit number rel. to start */ | |
1149 | xfs_rtblock_t len; /* length of inspected area */ | |
1150 | xfs_rtword_t mask; /* mask of relevant bits for value */ | |
1151 | xfs_rtword_t want; /* mask for "good" values */ | |
1152 | xfs_rtword_t wdiff; /* difference from wanted value */ | |
1153 | int word; /* word number in the buffer */ | |
1154 | ||
1155 | /* | |
1156 | * Compute and read in starting bitmap block for starting block. | |
1157 | */ | |
1158 | block = XFS_BITTOBLOCK(mp, start); | |
1159 | error = xfs_rtbuf_get(mp, tp, block, 0, &bp); | |
1160 | if (error) { | |
1161 | return error; | |
1162 | } | |
1163 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1164 | /* | |
1165 | * Get the first word's index & point to it. | |
1166 | */ | |
1167 | word = XFS_BITTOWORD(mp, start); | |
1168 | b = &bufp[word]; | |
1169 | bit = (int)(start & (XFS_NBWORD - 1)); | |
1170 | len = start - limit + 1; | |
1171 | /* | |
1172 | * Compute match value, based on the bit at start: if 1 (free) | |
1173 | * then all-ones, else all-zeroes. | |
1174 | */ | |
1175 | want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0; | |
1176 | /* | |
1177 | * If the starting position is not word-aligned, deal with the | |
1178 | * partial word. | |
1179 | */ | |
1180 | if (bit < XFS_NBWORD - 1) { | |
1181 | /* | |
1182 | * Calculate first (leftmost) bit number to look at, | |
1183 | * and mask for all the relevant bits in this word. | |
1184 | */ | |
1185 | firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0); | |
1186 | mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) << | |
1187 | firstbit; | |
1188 | /* | |
1189 | * Calculate the difference between the value there | |
1190 | * and what we're looking for. | |
1191 | */ | |
1192 | if ((wdiff = (*b ^ want) & mask)) { | |
1193 | /* | |
1194 | * Different. Mark where we are and return. | |
1195 | */ | |
1196 | xfs_trans_brelse(tp, bp); | |
1197 | i = bit - XFS_RTHIBIT(wdiff); | |
1198 | *rtblock = start - i + 1; | |
1199 | return 0; | |
1200 | } | |
1201 | i = bit - firstbit + 1; | |
1202 | /* | |
1203 | * Go on to previous block if that's where the previous word is | |
1204 | * and we need the previous word. | |
1205 | */ | |
1206 | if (--word == -1 && i < len) { | |
1207 | /* | |
1208 | * If done with this block, get the previous one. | |
1209 | */ | |
1210 | xfs_trans_brelse(tp, bp); | |
1211 | error = xfs_rtbuf_get(mp, tp, --block, 0, &bp); | |
1212 | if (error) { | |
1213 | return error; | |
1214 | } | |
1215 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1216 | word = XFS_BLOCKWMASK(mp); | |
1217 | b = &bufp[word]; | |
1218 | } else { | |
1219 | /* | |
1220 | * Go on to the previous word in the buffer. | |
1221 | */ | |
1222 | b--; | |
1223 | } | |
1224 | } else { | |
1225 | /* | |
1226 | * Starting on a word boundary, no partial word. | |
1227 | */ | |
1228 | i = 0; | |
1229 | } | |
1230 | /* | |
1231 | * Loop over whole words in buffers. When we use up one buffer | |
1232 | * we move on to the previous one. | |
1233 | */ | |
1234 | while (len - i >= XFS_NBWORD) { | |
1235 | /* | |
1236 | * Compute difference between actual and desired value. | |
1237 | */ | |
1238 | if ((wdiff = *b ^ want)) { | |
1239 | /* | |
1240 | * Different, mark where we are and return. | |
1241 | */ | |
1242 | xfs_trans_brelse(tp, bp); | |
1243 | i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff); | |
1244 | *rtblock = start - i + 1; | |
1245 | return 0; | |
1246 | } | |
1247 | i += XFS_NBWORD; | |
1248 | /* | |
1249 | * Go on to previous block if that's where the previous word is | |
1250 | * and we need the previous word. | |
1251 | */ | |
1252 | if (--word == -1 && i < len) { | |
1253 | /* | |
1254 | * If done with this block, get the previous one. | |
1255 | */ | |
1256 | xfs_trans_brelse(tp, bp); | |
1257 | error = xfs_rtbuf_get(mp, tp, --block, 0, &bp); | |
1258 | if (error) { | |
1259 | return error; | |
1260 | } | |
1261 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1262 | word = XFS_BLOCKWMASK(mp); | |
1263 | b = &bufp[word]; | |
1264 | } else { | |
1265 | /* | |
1266 | * Go on to the previous word in the buffer. | |
1267 | */ | |
1268 | b--; | |
1269 | } | |
1270 | } | |
1271 | /* | |
1272 | * If not ending on a word boundary, deal with the last | |
1273 | * (partial) word. | |
1274 | */ | |
1275 | if (len - i) { | |
1276 | /* | |
1277 | * Calculate first (leftmost) bit number to look at, | |
1278 | * and mask for all the relevant bits in this word. | |
1279 | */ | |
1280 | firstbit = XFS_NBWORD - (len - i); | |
1281 | mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit; | |
1282 | /* | |
1283 | * Compute difference between actual and desired value. | |
1284 | */ | |
1285 | if ((wdiff = (*b ^ want) & mask)) { | |
1286 | /* | |
1287 | * Different, mark where we are and return. | |
1288 | */ | |
1289 | xfs_trans_brelse(tp, bp); | |
1290 | i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff); | |
1291 | *rtblock = start - i + 1; | |
1292 | return 0; | |
1293 | } else | |
1294 | i = len; | |
1295 | } | |
1296 | /* | |
1297 | * No match, return that we scanned the whole area. | |
1298 | */ | |
1299 | xfs_trans_brelse(tp, bp); | |
1300 | *rtblock = start - i + 1; | |
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | /* | |
1305 | * Searching forward from start to limit, find the first block whose | |
1306 | * allocated/free state is different from start's. | |
1307 | */ | |
1308 | STATIC int /* error */ | |
1309 | xfs_rtfind_forw( | |
1310 | xfs_mount_t *mp, /* file system mount point */ | |
1311 | xfs_trans_t *tp, /* transaction pointer */ | |
1312 | xfs_rtblock_t start, /* starting block to look at */ | |
1313 | xfs_rtblock_t limit, /* last block to look at */ | |
1314 | xfs_rtblock_t *rtblock) /* out: start block found */ | |
1315 | { | |
1316 | xfs_rtword_t *b; /* current word in buffer */ | |
1317 | int bit; /* bit number in the word */ | |
1318 | xfs_rtblock_t block; /* bitmap block number */ | |
1319 | xfs_buf_t *bp; /* buf for the block */ | |
1320 | xfs_rtword_t *bufp; /* starting word in buffer */ | |
1321 | int error; /* error value */ | |
1322 | xfs_rtblock_t i; /* current bit number rel. to start */ | |
1323 | xfs_rtblock_t lastbit; /* last useful bit in the word */ | |
1324 | xfs_rtblock_t len; /* length of inspected area */ | |
1325 | xfs_rtword_t mask; /* mask of relevant bits for value */ | |
1326 | xfs_rtword_t want; /* mask for "good" values */ | |
1327 | xfs_rtword_t wdiff; /* difference from wanted value */ | |
1328 | int word; /* word number in the buffer */ | |
1329 | ||
1330 | /* | |
1331 | * Compute and read in starting bitmap block for starting block. | |
1332 | */ | |
1333 | block = XFS_BITTOBLOCK(mp, start); | |
1334 | error = xfs_rtbuf_get(mp, tp, block, 0, &bp); | |
1335 | if (error) { | |
1336 | return error; | |
1337 | } | |
1338 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1339 | /* | |
1340 | * Get the first word's index & point to it. | |
1341 | */ | |
1342 | word = XFS_BITTOWORD(mp, start); | |
1343 | b = &bufp[word]; | |
1344 | bit = (int)(start & (XFS_NBWORD - 1)); | |
1345 | len = limit - start + 1; | |
1346 | /* | |
1347 | * Compute match value, based on the bit at start: if 1 (free) | |
1348 | * then all-ones, else all-zeroes. | |
1349 | */ | |
1350 | want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0; | |
1351 | /* | |
1352 | * If the starting position is not word-aligned, deal with the | |
1353 | * partial word. | |
1354 | */ | |
1355 | if (bit) { | |
1356 | /* | |
1357 | * Calculate last (rightmost) bit number to look at, | |
1358 | * and mask for all the relevant bits in this word. | |
1359 | */ | |
1360 | lastbit = XFS_RTMIN(bit + len, XFS_NBWORD); | |
1361 | mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; | |
1362 | /* | |
1363 | * Calculate the difference between the value there | |
1364 | * and what we're looking for. | |
1365 | */ | |
1366 | if ((wdiff = (*b ^ want) & mask)) { | |
1367 | /* | |
1368 | * Different. Mark where we are and return. | |
1369 | */ | |
1370 | xfs_trans_brelse(tp, bp); | |
1371 | i = XFS_RTLOBIT(wdiff) - bit; | |
1372 | *rtblock = start + i - 1; | |
1373 | return 0; | |
1374 | } | |
1375 | i = lastbit - bit; | |
1376 | /* | |
1377 | * Go on to next block if that's where the next word is | |
1378 | * and we need the next word. | |
1379 | */ | |
1380 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
1381 | /* | |
1382 | * If done with this block, get the previous one. | |
1383 | */ | |
1384 | xfs_trans_brelse(tp, bp); | |
1385 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
1386 | if (error) { | |
1387 | return error; | |
1388 | } | |
1389 | b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1390 | word = 0; | |
1391 | } else { | |
1392 | /* | |
1393 | * Go on to the previous word in the buffer. | |
1394 | */ | |
1395 | b++; | |
1396 | } | |
1397 | } else { | |
1398 | /* | |
1399 | * Starting on a word boundary, no partial word. | |
1400 | */ | |
1401 | i = 0; | |
1402 | } | |
1403 | /* | |
1404 | * Loop over whole words in buffers. When we use up one buffer | |
1405 | * we move on to the next one. | |
1406 | */ | |
1407 | while (len - i >= XFS_NBWORD) { | |
1408 | /* | |
1409 | * Compute difference between actual and desired value. | |
1410 | */ | |
1411 | if ((wdiff = *b ^ want)) { | |
1412 | /* | |
1413 | * Different, mark where we are and return. | |
1414 | */ | |
1415 | xfs_trans_brelse(tp, bp); | |
1416 | i += XFS_RTLOBIT(wdiff); | |
1417 | *rtblock = start + i - 1; | |
1418 | return 0; | |
1419 | } | |
1420 | i += XFS_NBWORD; | |
1421 | /* | |
1422 | * Go on to next block if that's where the next word is | |
1423 | * and we need the next word. | |
1424 | */ | |
1425 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
1426 | /* | |
1427 | * If done with this block, get the next one. | |
1428 | */ | |
1429 | xfs_trans_brelse(tp, bp); | |
1430 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
1431 | if (error) { | |
1432 | return error; | |
1433 | } | |
1434 | b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1435 | word = 0; | |
1436 | } else { | |
1437 | /* | |
1438 | * Go on to the next word in the buffer. | |
1439 | */ | |
1440 | b++; | |
1441 | } | |
1442 | } | |
1443 | /* | |
1444 | * If not ending on a word boundary, deal with the last | |
1445 | * (partial) word. | |
1446 | */ | |
1447 | if ((lastbit = len - i)) { | |
1448 | /* | |
1449 | * Calculate mask for all the relevant bits in this word. | |
1450 | */ | |
1451 | mask = ((xfs_rtword_t)1 << lastbit) - 1; | |
1452 | /* | |
1453 | * Compute difference between actual and desired value. | |
1454 | */ | |
1455 | if ((wdiff = (*b ^ want) & mask)) { | |
1456 | /* | |
1457 | * Different, mark where we are and return. | |
1458 | */ | |
1459 | xfs_trans_brelse(tp, bp); | |
1460 | i += XFS_RTLOBIT(wdiff); | |
1461 | *rtblock = start + i - 1; | |
1462 | return 0; | |
1463 | } else | |
1464 | i = len; | |
1465 | } | |
1466 | /* | |
1467 | * No match, return that we scanned the whole area. | |
1468 | */ | |
1469 | xfs_trans_brelse(tp, bp); | |
1470 | *rtblock = start + i - 1; | |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | /* | |
1475 | * Mark an extent specified by start and len freed. | |
1476 | * Updates all the summary information as well as the bitmap. | |
1477 | */ | |
1478 | STATIC int /* error */ | |
1479 | xfs_rtfree_range( | |
1480 | xfs_mount_t *mp, /* file system mount point */ | |
1481 | xfs_trans_t *tp, /* transaction pointer */ | |
1482 | xfs_rtblock_t start, /* starting block to free */ | |
1483 | xfs_extlen_t len, /* length to free */ | |
1484 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
1485 | xfs_fsblock_t *rsb) /* in/out: summary block number */ | |
1486 | { | |
1487 | xfs_rtblock_t end; /* end of the freed extent */ | |
1488 | int error; /* error value */ | |
1489 | xfs_rtblock_t postblock; /* first block freed > end */ | |
1490 | xfs_rtblock_t preblock; /* first block freed < start */ | |
1491 | ||
1492 | end = start + len - 1; | |
1493 | /* | |
1494 | * Modify the bitmap to mark this extent freed. | |
1495 | */ | |
1496 | error = xfs_rtmodify_range(mp, tp, start, len, 1); | |
1497 | if (error) { | |
1498 | return error; | |
1499 | } | |
1500 | /* | |
1501 | * Assume we're freeing out of the middle of an allocated extent. | |
1502 | * We need to find the beginning and end of the extent so we can | |
1503 | * properly update the summary. | |
1504 | */ | |
1505 | error = xfs_rtfind_back(mp, tp, start, 0, &preblock); | |
1506 | if (error) { | |
1507 | return error; | |
1508 | } | |
1509 | /* | |
1510 | * Find the next allocated block (end of allocated extent). | |
1511 | */ | |
1512 | error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1, | |
1513 | &postblock); | |
3daeb42c DC |
1514 | if (error) |
1515 | return error; | |
1da177e4 LT |
1516 | /* |
1517 | * If there are blocks not being freed at the front of the | |
1518 | * old extent, add summary data for them to be allocated. | |
1519 | */ | |
1520 | if (preblock < start) { | |
1521 | error = xfs_rtmodify_summary(mp, tp, | |
1522 | XFS_RTBLOCKLOG(start - preblock), | |
1523 | XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb); | |
1524 | if (error) { | |
1525 | return error; | |
1526 | } | |
1527 | } | |
1528 | /* | |
1529 | * If there are blocks not being freed at the end of the | |
1530 | * old extent, add summary data for them to be allocated. | |
1531 | */ | |
1532 | if (postblock > end) { | |
1533 | error = xfs_rtmodify_summary(mp, tp, | |
1534 | XFS_RTBLOCKLOG(postblock - end), | |
1535 | XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb); | |
1536 | if (error) { | |
1537 | return error; | |
1538 | } | |
1539 | } | |
1540 | /* | |
1541 | * Increment the summary information corresponding to the entire | |
1542 | * (new) free extent. | |
1543 | */ | |
1544 | error = xfs_rtmodify_summary(mp, tp, | |
1545 | XFS_RTBLOCKLOG(postblock + 1 - preblock), | |
1546 | XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb); | |
1547 | return error; | |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * Read and return the summary information for a given extent size, | |
1552 | * bitmap block combination. | |
1553 | * Keeps track of a current summary block, so we don't keep reading | |
1554 | * it from the buffer cache. | |
1555 | */ | |
1556 | STATIC int /* error */ | |
1557 | xfs_rtget_summary( | |
1558 | xfs_mount_t *mp, /* file system mount structure */ | |
1559 | xfs_trans_t *tp, /* transaction pointer */ | |
1560 | int log, /* log2 of extent size */ | |
1561 | xfs_rtblock_t bbno, /* bitmap block number */ | |
1562 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
1563 | xfs_fsblock_t *rsb, /* in/out: summary block number */ | |
1564 | xfs_suminfo_t *sum) /* out: summary info for this block */ | |
1565 | { | |
1566 | xfs_buf_t *bp; /* buffer for summary block */ | |
1567 | int error; /* error value */ | |
1568 | xfs_fsblock_t sb; /* summary fsblock */ | |
1569 | int so; /* index into the summary file */ | |
1570 | xfs_suminfo_t *sp; /* pointer to returned data */ | |
1571 | ||
1572 | /* | |
1573 | * Compute entry number in the summary file. | |
1574 | */ | |
1575 | so = XFS_SUMOFFS(mp, log, bbno); | |
1576 | /* | |
1577 | * Compute the block number in the summary file. | |
1578 | */ | |
1579 | sb = XFS_SUMOFFSTOBLOCK(mp, so); | |
1580 | /* | |
1581 | * If we have an old buffer, and the block number matches, use that. | |
1582 | */ | |
1583 | if (rbpp && *rbpp && *rsb == sb) | |
1584 | bp = *rbpp; | |
1585 | /* | |
1586 | * Otherwise we have to get the buffer. | |
1587 | */ | |
1588 | else { | |
1589 | /* | |
1590 | * If there was an old one, get rid of it first. | |
1591 | */ | |
1592 | if (rbpp && *rbpp) | |
1593 | xfs_trans_brelse(tp, *rbpp); | |
1594 | error = xfs_rtbuf_get(mp, tp, sb, 1, &bp); | |
1595 | if (error) { | |
1596 | return error; | |
1597 | } | |
1598 | /* | |
1599 | * Remember this buffer and block for the next call. | |
1600 | */ | |
1601 | if (rbpp) { | |
1602 | *rbpp = bp; | |
1603 | *rsb = sb; | |
1604 | } | |
1605 | } | |
1606 | /* | |
1607 | * Point to the summary information & copy it out. | |
1608 | */ | |
1609 | sp = XFS_SUMPTR(mp, bp, so); | |
1610 | *sum = *sp; | |
1611 | /* | |
1612 | * Drop the buffer if we're not asked to remember it. | |
1613 | */ | |
1614 | if (!rbpp) | |
1615 | xfs_trans_brelse(tp, bp); | |
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | /* | |
1620 | * Set the given range of bitmap bits to the given value. | |
1621 | * Do whatever I/O and logging is required. | |
1622 | */ | |
1623 | STATIC int /* error */ | |
1624 | xfs_rtmodify_range( | |
1625 | xfs_mount_t *mp, /* file system mount point */ | |
1626 | xfs_trans_t *tp, /* transaction pointer */ | |
1627 | xfs_rtblock_t start, /* starting block to modify */ | |
1628 | xfs_extlen_t len, /* length of extent to modify */ | |
1629 | int val) /* 1 for free, 0 for allocated */ | |
1630 | { | |
1631 | xfs_rtword_t *b; /* current word in buffer */ | |
1632 | int bit; /* bit number in the word */ | |
1633 | xfs_rtblock_t block; /* bitmap block number */ | |
1634 | xfs_buf_t *bp; /* buf for the block */ | |
1635 | xfs_rtword_t *bufp; /* starting word in buffer */ | |
1636 | int error; /* error value */ | |
1637 | xfs_rtword_t *first; /* first used word in the buffer */ | |
1638 | int i; /* current bit number rel. to start */ | |
1639 | int lastbit; /* last useful bit in word */ | |
1640 | xfs_rtword_t mask; /* mask o frelevant bits for value */ | |
1641 | int word; /* word number in the buffer */ | |
1642 | ||
1643 | /* | |
1644 | * Compute starting bitmap block number. | |
1645 | */ | |
1646 | block = XFS_BITTOBLOCK(mp, start); | |
1647 | /* | |
1648 | * Read the bitmap block, and point to its data. | |
1649 | */ | |
1650 | error = xfs_rtbuf_get(mp, tp, block, 0, &bp); | |
1651 | if (error) { | |
1652 | return error; | |
1653 | } | |
1654 | bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1655 | /* | |
1656 | * Compute the starting word's address, and starting bit. | |
1657 | */ | |
1658 | word = XFS_BITTOWORD(mp, start); | |
1659 | first = b = &bufp[word]; | |
1660 | bit = (int)(start & (XFS_NBWORD - 1)); | |
1661 | /* | |
1662 | * 0 (allocated) => all zeroes; 1 (free) => all ones. | |
1663 | */ | |
1664 | val = -val; | |
1665 | /* | |
1666 | * If not starting on a word boundary, deal with the first | |
1667 | * (partial) word. | |
1668 | */ | |
1669 | if (bit) { | |
1670 | /* | |
1671 | * Compute first bit not changed and mask of relevant bits. | |
1672 | */ | |
1673 | lastbit = XFS_RTMIN(bit + len, XFS_NBWORD); | |
1674 | mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit; | |
1675 | /* | |
1676 | * Set/clear the active bits. | |
1677 | */ | |
1678 | if (val) | |
1679 | *b |= mask; | |
1680 | else | |
1681 | *b &= ~mask; | |
1682 | i = lastbit - bit; | |
1683 | /* | |
1684 | * Go on to the next block if that's where the next word is | |
1685 | * and we need the next word. | |
1686 | */ | |
1687 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
1688 | /* | |
1689 | * Log the changed part of this block. | |
1690 | * Get the next one. | |
1691 | */ | |
1692 | xfs_trans_log_buf(tp, bp, | |
1693 | (uint)((char *)first - (char *)bufp), | |
1694 | (uint)((char *)b - (char *)bufp)); | |
1695 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
1696 | if (error) { | |
1697 | return error; | |
1698 | } | |
1699 | first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1700 | word = 0; | |
1701 | } else { | |
1702 | /* | |
1703 | * Go on to the next word in the buffer | |
1704 | */ | |
1705 | b++; | |
1706 | } | |
1707 | } else { | |
1708 | /* | |
1709 | * Starting on a word boundary, no partial word. | |
1710 | */ | |
1711 | i = 0; | |
1712 | } | |
1713 | /* | |
1714 | * Loop over whole words in buffers. When we use up one buffer | |
1715 | * we move on to the next one. | |
1716 | */ | |
1717 | while (len - i >= XFS_NBWORD) { | |
1718 | /* | |
1719 | * Set the word value correctly. | |
1720 | */ | |
1721 | *b = val; | |
1722 | i += XFS_NBWORD; | |
1723 | /* | |
1724 | * Go on to the next block if that's where the next word is | |
1725 | * and we need the next word. | |
1726 | */ | |
1727 | if (++word == XFS_BLOCKWSIZE(mp) && i < len) { | |
1728 | /* | |
1729 | * Log the changed part of this block. | |
1730 | * Get the next one. | |
1731 | */ | |
1732 | xfs_trans_log_buf(tp, bp, | |
1733 | (uint)((char *)first - (char *)bufp), | |
1734 | (uint)((char *)b - (char *)bufp)); | |
1735 | error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); | |
1736 | if (error) { | |
1737 | return error; | |
1738 | } | |
1739 | first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp); | |
1740 | word = 0; | |
1741 | } else { | |
1742 | /* | |
1743 | * Go on to the next word in the buffer | |
1744 | */ | |
1745 | b++; | |
1746 | } | |
1747 | } | |
1748 | /* | |
1749 | * If not ending on a word boundary, deal with the last | |
1750 | * (partial) word. | |
1751 | */ | |
1752 | if ((lastbit = len - i)) { | |
1753 | /* | |
1754 | * Compute a mask of relevant bits. | |
1755 | */ | |
1756 | bit = 0; | |
1757 | mask = ((xfs_rtword_t)1 << lastbit) - 1; | |
1758 | /* | |
1759 | * Set/clear the active bits. | |
1760 | */ | |
1761 | if (val) | |
1762 | *b |= mask; | |
1763 | else | |
1764 | *b &= ~mask; | |
1765 | b++; | |
1766 | } | |
1767 | /* | |
1768 | * Log any remaining changed bytes. | |
1769 | */ | |
1770 | if (b > first) | |
1771 | xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp), | |
1772 | (uint)((char *)b - (char *)bufp - 1)); | |
1773 | return 0; | |
1774 | } | |
1775 | ||
1776 | /* | |
1777 | * Read and modify the summary information for a given extent size, | |
1778 | * bitmap block combination. | |
1779 | * Keeps track of a current summary block, so we don't keep reading | |
1780 | * it from the buffer cache. | |
1781 | */ | |
1782 | STATIC int /* error */ | |
1783 | xfs_rtmodify_summary( | |
1784 | xfs_mount_t *mp, /* file system mount point */ | |
1785 | xfs_trans_t *tp, /* transaction pointer */ | |
1786 | int log, /* log2 of extent size */ | |
1787 | xfs_rtblock_t bbno, /* bitmap block number */ | |
1788 | int delta, /* change to make to summary info */ | |
1789 | xfs_buf_t **rbpp, /* in/out: summary block buffer */ | |
1790 | xfs_fsblock_t *rsb) /* in/out: summary block number */ | |
1791 | { | |
1792 | xfs_buf_t *bp; /* buffer for the summary block */ | |
1793 | int error; /* error value */ | |
1794 | xfs_fsblock_t sb; /* summary fsblock */ | |
1795 | int so; /* index into the summary file */ | |
1796 | xfs_suminfo_t *sp; /* pointer to returned data */ | |
1797 | ||
1798 | /* | |
1799 | * Compute entry number in the summary file. | |
1800 | */ | |
1801 | so = XFS_SUMOFFS(mp, log, bbno); | |
1802 | /* | |
1803 | * Compute the block number in the summary file. | |
1804 | */ | |
1805 | sb = XFS_SUMOFFSTOBLOCK(mp, so); | |
1806 | /* | |
1807 | * If we have an old buffer, and the block number matches, use that. | |
1808 | */ | |
1809 | if (rbpp && *rbpp && *rsb == sb) | |
1810 | bp = *rbpp; | |
1811 | /* | |
1812 | * Otherwise we have to get the buffer. | |
1813 | */ | |
1814 | else { | |
1815 | /* | |
1816 | * If there was an old one, get rid of it first. | |
1817 | */ | |
1818 | if (rbpp && *rbpp) | |
1819 | xfs_trans_brelse(tp, *rbpp); | |
1820 | error = xfs_rtbuf_get(mp, tp, sb, 1, &bp); | |
1821 | if (error) { | |
1822 | return error; | |
1823 | } | |
1824 | /* | |
1825 | * Remember this buffer and block for the next call. | |
1826 | */ | |
1827 | if (rbpp) { | |
1828 | *rbpp = bp; | |
1829 | *rsb = sb; | |
1830 | } | |
1831 | } | |
1832 | /* | |
1833 | * Point to the summary information, modify and log it. | |
1834 | */ | |
1835 | sp = XFS_SUMPTR(mp, bp, so); | |
1836 | *sp += delta; | |
1837 | xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)), | |
1838 | (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1)); | |
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | /* | |
1843 | * Visible (exported) functions. | |
1844 | */ | |
1845 | ||
1846 | /* | |
1847 | * Grow the realtime area of the filesystem. | |
1848 | */ | |
1849 | int | |
1850 | xfs_growfs_rt( | |
1851 | xfs_mount_t *mp, /* mount point for filesystem */ | |
1852 | xfs_growfs_rt_t *in) /* growfs rt input struct */ | |
1853 | { | |
1854 | xfs_rtblock_t bmbno; /* bitmap block number */ | |
1855 | xfs_buf_t *bp; /* temporary buffer */ | |
1da177e4 LT |
1856 | int error; /* error return value */ |
1857 | xfs_inode_t *ip; /* bitmap inode, used as lock */ | |
1858 | xfs_mount_t *nmp; /* new (fake) mount structure */ | |
1859 | xfs_drfsbno_t nrblocks; /* new number of realtime blocks */ | |
1860 | xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */ | |
1861 | xfs_drtbno_t nrextents; /* new number of realtime extents */ | |
1862 | uint8_t nrextslog; /* new log2 of sb_rextents */ | |
1863 | xfs_extlen_t nrsumblocks; /* new number of summary blocks */ | |
1864 | uint nrsumlevels; /* new rt summary levels */ | |
1865 | uint nrsumsize; /* new size of rt summary, bytes */ | |
1866 | xfs_sb_t *nsbp; /* new superblock */ | |
1867 | xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */ | |
1868 | xfs_extlen_t rsumblocks; /* current number of rt summary blks */ | |
1869 | xfs_sb_t *sbp; /* old superblock */ | |
1870 | xfs_fsblock_t sumbno; /* summary block number */ | |
1da177e4 LT |
1871 | |
1872 | sbp = &mp->m_sb; | |
1873 | /* | |
1874 | * Initial error checking. | |
1875 | */ | |
743bb465 | 1876 | if (!capable(CAP_SYS_ADMIN)) |
1877 | return XFS_ERROR(EPERM); | |
73024cf1 | 1878 | if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL || |
1da177e4 LT |
1879 | (nrblocks = in->newblocks) <= sbp->sb_rblocks || |
1880 | (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize))) | |
1881 | return XFS_ERROR(EINVAL); | |
4cc929ee NS |
1882 | if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks))) |
1883 | return error; | |
1da177e4 LT |
1884 | /* |
1885 | * Read in the last block of the device, make sure it exists. | |
1886 | */ | |
1922c949 DC |
1887 | bp = xfs_buf_read_uncached(mp, mp->m_rtdev_targp, |
1888 | XFS_FSB_TO_BB(mp, nrblocks - 1), | |
1889 | XFS_FSB_TO_B(mp, 1), 0); | |
1890 | if (!bp) | |
1891 | return EIO; | |
1da177e4 | 1892 | xfs_buf_relse(bp); |
1922c949 | 1893 | |
1da177e4 LT |
1894 | /* |
1895 | * Calculate new parameters. These are the final values to be reached. | |
1896 | */ | |
1897 | nrextents = nrblocks; | |
1898 | do_div(nrextents, in->extsize); | |
68c32715 | 1899 | nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize); |
1da177e4 LT |
1900 | nrextslog = xfs_highbit32(nrextents); |
1901 | nrsumlevels = nrextslog + 1; | |
1902 | nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks; | |
1903 | nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize); | |
1904 | nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks); | |
1905 | /* | |
1906 | * New summary size can't be more than half the size of | |
1907 | * the log. This prevents us from getting a log overflow, | |
1908 | * since we'll log basically the whole summary file at once. | |
1909 | */ | |
1910 | if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1)) | |
1911 | return XFS_ERROR(EINVAL); | |
1912 | /* | |
1913 | * Get the old block counts for bitmap and summary inodes. | |
1914 | * These can't change since other growfs callers are locked out. | |
1915 | */ | |
1916 | rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size); | |
1917 | rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size); | |
1918 | /* | |
1919 | * Allocate space to the bitmap and summary files, as necessary. | |
1920 | */ | |
1921 | if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, | |
1922 | mp->m_sb.sb_rbmino))) | |
1923 | return error; | |
1924 | if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, | |
1925 | mp->m_sb.sb_rsumino))) | |
1926 | return error; | |
d432c80e NS |
1927 | /* |
1928 | * Allocate a new (fake) mount/sb. | |
1929 | */ | |
1930 | nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP); | |
1da177e4 LT |
1931 | /* |
1932 | * Loop over the bitmap blocks. | |
1933 | * We will do everything one bitmap block at a time. | |
1934 | * Skip the current block if it is exactly full. | |
1935 | * This also deals with the case where there were no rtextents before. | |
1936 | */ | |
1937 | for (bmbno = sbp->sb_rbmblocks - | |
1938 | ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0); | |
1939 | bmbno < nrbmblocks; | |
1940 | bmbno++) { | |
0924b585 DC |
1941 | xfs_trans_t *tp; |
1942 | int cancelflags = 0; | |
1943 | ||
1da177e4 LT |
1944 | *nmp = *mp; |
1945 | nsbp = &nmp->m_sb; | |
1946 | /* | |
1947 | * Calculate new sb and mount fields for this round. | |
1948 | */ | |
1949 | nsbp->sb_rextsize = in->extsize; | |
1950 | nsbp->sb_rbmblocks = bmbno + 1; | |
1951 | nsbp->sb_rblocks = | |
1952 | XFS_RTMIN(nrblocks, | |
1953 | nsbp->sb_rbmblocks * NBBY * | |
1954 | nsbp->sb_blocksize * nsbp->sb_rextsize); | |
1955 | nsbp->sb_rextents = nsbp->sb_rblocks; | |
1956 | do_div(nsbp->sb_rextents, nsbp->sb_rextsize); | |
79071eb0 | 1957 | ASSERT(nsbp->sb_rextents != 0); |
1da177e4 LT |
1958 | nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents); |
1959 | nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1; | |
1960 | nrsumsize = | |
1961 | (uint)sizeof(xfs_suminfo_t) * nrsumlevels * | |
1962 | nsbp->sb_rbmblocks; | |
1963 | nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize); | |
1964 | nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks); | |
1965 | /* | |
1966 | * Start a transaction, get the log reservation. | |
1967 | */ | |
1968 | tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE); | |
1da177e4 LT |
1969 | if ((error = xfs_trans_reserve(tp, 0, |
1970 | XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0))) | |
0924b585 | 1971 | goto error_cancel; |
1da177e4 LT |
1972 | /* |
1973 | * Lock out other callers by grabbing the bitmap inode lock. | |
1974 | */ | |
07ff2fa8 NS |
1975 | if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, |
1976 | XFS_ILOCK_EXCL, &ip))) | |
0924b585 | 1977 | goto error_cancel; |
1da177e4 LT |
1978 | ASSERT(ip == mp->m_rbmip); |
1979 | /* | |
1980 | * Update the bitmap inode's size. | |
1981 | */ | |
1982 | mp->m_rbmip->i_d.di_size = | |
1983 | nsbp->sb_rbmblocks * nsbp->sb_blocksize; | |
1984 | xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE); | |
1985 | cancelflags |= XFS_TRANS_ABORT; | |
1986 | /* | |
1987 | * Get the summary inode into the transaction. | |
1988 | */ | |
07ff2fa8 NS |
1989 | if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0, |
1990 | XFS_ILOCK_EXCL, &ip))) | |
0924b585 | 1991 | goto error_cancel; |
1da177e4 LT |
1992 | ASSERT(ip == mp->m_rsumip); |
1993 | /* | |
1994 | * Update the summary inode's size. | |
1995 | */ | |
1996 | mp->m_rsumip->i_d.di_size = nmp->m_rsumsize; | |
1997 | xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE); | |
1998 | /* | |
1999 | * Copy summary data from old to new sizes. | |
2000 | * Do this when the real size (not block-aligned) changes. | |
2001 | */ | |
2002 | if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks || | |
2003 | mp->m_rsumlevels != nmp->m_rsumlevels) { | |
2004 | error = xfs_rtcopy_summary(mp, nmp, tp); | |
2005 | if (error) | |
0924b585 | 2006 | goto error_cancel; |
1da177e4 LT |
2007 | } |
2008 | /* | |
2009 | * Update superblock fields. | |
2010 | */ | |
2011 | if (nsbp->sb_rextsize != sbp->sb_rextsize) | |
2012 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE, | |
2013 | nsbp->sb_rextsize - sbp->sb_rextsize); | |
2014 | if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks) | |
2015 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS, | |
2016 | nsbp->sb_rbmblocks - sbp->sb_rbmblocks); | |
2017 | if (nsbp->sb_rblocks != sbp->sb_rblocks) | |
2018 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS, | |
2019 | nsbp->sb_rblocks - sbp->sb_rblocks); | |
2020 | if (nsbp->sb_rextents != sbp->sb_rextents) | |
2021 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS, | |
2022 | nsbp->sb_rextents - sbp->sb_rextents); | |
2023 | if (nsbp->sb_rextslog != sbp->sb_rextslog) | |
2024 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG, | |
2025 | nsbp->sb_rextslog - sbp->sb_rextslog); | |
2026 | /* | |
2027 | * Free new extent. | |
2028 | */ | |
2029 | bp = NULL; | |
2030 | error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents, | |
2031 | nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno); | |
0924b585 DC |
2032 | if (error) { |
2033 | error_cancel: | |
2034 | xfs_trans_cancel(tp, cancelflags); | |
d432c80e | 2035 | break; |
0924b585 | 2036 | } |
1da177e4 LT |
2037 | /* |
2038 | * Mark more blocks free in the superblock. | |
2039 | */ | |
2040 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, | |
2041 | nsbp->sb_rextents - sbp->sb_rextents); | |
1da177e4 LT |
2042 | /* |
2043 | * Update mp values into the real mp structure. | |
2044 | */ | |
2045 | mp->m_rsumlevels = nrsumlevels; | |
2046 | mp->m_rsumsize = nrsumsize; | |
e5720eec DC |
2047 | |
2048 | error = xfs_trans_commit(tp, 0); | |
0924b585 | 2049 | if (error) |
e5720eec | 2050 | break; |
1da177e4 | 2051 | } |
d432c80e | 2052 | |
1da177e4 | 2053 | /* |
d432c80e | 2054 | * Free the fake mp structure. |
1da177e4 | 2055 | */ |
f0e2d93c | 2056 | kmem_free(nmp); |
d432c80e | 2057 | |
1da177e4 LT |
2058 | return error; |
2059 | } | |
2060 | ||
2061 | /* | |
2062 | * Allocate an extent in the realtime subvolume, with the usual allocation | |
2063 | * parameters. The length units are all in realtime extents, as is the | |
2064 | * result block number. | |
2065 | */ | |
2066 | int /* error */ | |
2067 | xfs_rtallocate_extent( | |
2068 | xfs_trans_t *tp, /* transaction pointer */ | |
2069 | xfs_rtblock_t bno, /* starting block number to allocate */ | |
2070 | xfs_extlen_t minlen, /* minimum length to allocate */ | |
2071 | xfs_extlen_t maxlen, /* maximum length to allocate */ | |
2072 | xfs_extlen_t *len, /* out: actual length allocated */ | |
2073 | xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */ | |
2074 | int wasdel, /* was a delayed allocation extent */ | |
2075 | xfs_extlen_t prod, /* extent product factor */ | |
2076 | xfs_rtblock_t *rtblock) /* out: start block allocated */ | |
2077 | { | |
2078 | int error; /* error value */ | |
2079 | xfs_inode_t *ip; /* inode for bitmap file */ | |
2080 | xfs_mount_t *mp; /* file system mount structure */ | |
2081 | xfs_rtblock_t r; /* result allocated block */ | |
2082 | xfs_fsblock_t sb; /* summary file block number */ | |
2083 | xfs_buf_t *sumbp; /* summary file block buffer */ | |
2084 | ||
2085 | ASSERT(minlen > 0 && minlen <= maxlen); | |
2086 | mp = tp->t_mountp; | |
2087 | /* | |
2088 | * If prod is set then figure out what to do to minlen and maxlen. | |
2089 | */ | |
2090 | if (prod > 1) { | |
2091 | xfs_extlen_t i; | |
2092 | ||
2093 | if ((i = maxlen % prod)) | |
2094 | maxlen -= i; | |
2095 | if ((i = minlen % prod)) | |
2096 | minlen += prod - i; | |
2097 | if (maxlen < minlen) { | |
2098 | *rtblock = NULLRTBLOCK; | |
2099 | return 0; | |
2100 | } | |
2101 | } | |
2102 | /* | |
2103 | * Lock out other callers by grabbing the bitmap inode lock. | |
2104 | */ | |
07ff2fa8 NS |
2105 | if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, |
2106 | XFS_ILOCK_EXCL, &ip))) | |
1da177e4 | 2107 | return error; |
1da177e4 LT |
2108 | sumbp = NULL; |
2109 | /* | |
2110 | * Allocate by size, or near another block, or exactly at some block. | |
2111 | */ | |
2112 | switch (type) { | |
2113 | case XFS_ALLOCTYPE_ANY_AG: | |
2114 | error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len, | |
2115 | &sumbp, &sb, prod, &r); | |
2116 | break; | |
2117 | case XFS_ALLOCTYPE_NEAR_BNO: | |
2118 | error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen, | |
2119 | len, &sumbp, &sb, prod, &r); | |
2120 | break; | |
2121 | case XFS_ALLOCTYPE_THIS_BNO: | |
2122 | error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, | |
2123 | len, &sumbp, &sb, prod, &r); | |
2124 | break; | |
2125 | default: | |
2126 | ASSERT(0); | |
2127 | } | |
2128 | if (error) { | |
2129 | return error; | |
2130 | } | |
2131 | /* | |
2132 | * If it worked, update the superblock. | |
2133 | */ | |
2134 | if (r != NULLRTBLOCK) { | |
2135 | long slen = (long)*len; | |
2136 | ||
2137 | ASSERT(*len >= minlen && *len <= maxlen); | |
2138 | if (wasdel) | |
2139 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen); | |
2140 | else | |
2141 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen); | |
2142 | } | |
2143 | *rtblock = r; | |
2144 | return 0; | |
2145 | } | |
2146 | ||
2147 | /* | |
2148 | * Free an extent in the realtime subvolume. Length is expressed in | |
2149 | * realtime extents, as is the block number. | |
2150 | */ | |
2151 | int /* error */ | |
2152 | xfs_rtfree_extent( | |
2153 | xfs_trans_t *tp, /* transaction pointer */ | |
2154 | xfs_rtblock_t bno, /* starting block number to free */ | |
2155 | xfs_extlen_t len) /* length of extent freed */ | |
2156 | { | |
2157 | int error; /* error value */ | |
2158 | xfs_inode_t *ip; /* bitmap file inode */ | |
2159 | xfs_mount_t *mp; /* file system mount structure */ | |
2160 | xfs_fsblock_t sb; /* summary file block number */ | |
2161 | xfs_buf_t *sumbp; /* summary file block buffer */ | |
2162 | ||
2163 | mp = tp->t_mountp; | |
2164 | /* | |
2165 | * Synchronize by locking the bitmap inode. | |
2166 | */ | |
07ff2fa8 NS |
2167 | if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, |
2168 | XFS_ILOCK_EXCL, &ip))) | |
1da177e4 | 2169 | return error; |
1da177e4 LT |
2170 | #if defined(__KERNEL__) && defined(DEBUG) |
2171 | /* | |
2172 | * Check to see that this whole range is currently allocated. | |
2173 | */ | |
2174 | { | |
2175 | int stat; /* result from checking range */ | |
2176 | ||
2177 | error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat); | |
2178 | if (error) { | |
2179 | return error; | |
2180 | } | |
2181 | ASSERT(stat); | |
2182 | } | |
2183 | #endif | |
2184 | sumbp = NULL; | |
2185 | /* | |
2186 | * Free the range of realtime blocks. | |
2187 | */ | |
2188 | error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb); | |
2189 | if (error) { | |
2190 | return error; | |
2191 | } | |
2192 | /* | |
2193 | * Mark more blocks free in the superblock. | |
2194 | */ | |
2195 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len); | |
2196 | /* | |
2197 | * If we've now freed all the blocks, reset the file sequence | |
2198 | * number to 0. | |
2199 | */ | |
2200 | if (tp->t_frextents_delta + mp->m_sb.sb_frextents == | |
2201 | mp->m_sb.sb_rextents) { | |
2202 | if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) | |
2203 | ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM; | |
2204 | *(__uint64_t *)&ip->i_d.di_atime = 0; | |
2205 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
2206 | } | |
2207 | return 0; | |
2208 | } | |
2209 | ||
2210 | /* | |
2211 | * Initialize realtime fields in the mount structure. | |
2212 | */ | |
2213 | int /* error */ | |
2214 | xfs_rtmount_init( | |
2215 | xfs_mount_t *mp) /* file system mount structure */ | |
2216 | { | |
2217 | xfs_buf_t *bp; /* buffer for last block of subvolume */ | |
2218 | xfs_daddr_t d; /* address of last block of subvolume */ | |
1da177e4 LT |
2219 | xfs_sb_t *sbp; /* filesystem superblock copy in mount */ |
2220 | ||
2221 | sbp = &mp->m_sb; | |
2222 | if (sbp->sb_rblocks == 0) | |
2223 | return 0; | |
2224 | if (mp->m_rtdev_targp == NULL) { | |
2225 | cmn_err(CE_WARN, | |
2226 | "XFS: This filesystem has a realtime volume, use rtdev=device option"); | |
2227 | return XFS_ERROR(ENODEV); | |
2228 | } | |
2229 | mp->m_rsumlevels = sbp->sb_rextslog + 1; | |
2230 | mp->m_rsumsize = | |
2231 | (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels * | |
2232 | sbp->sb_rbmblocks; | |
2233 | mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize); | |
2234 | mp->m_rbmip = mp->m_rsumip = NULL; | |
2235 | /* | |
2236 | * Check that the realtime section is an ok size. | |
2237 | */ | |
2238 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks); | |
2239 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) { | |
2240 | cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu", | |
2241 | (unsigned long long) XFS_BB_TO_FSB(mp, d), | |
2242 | (unsigned long long) mp->m_sb.sb_rblocks); | |
657a4cff | 2243 | return XFS_ERROR(EFBIG); |
1da177e4 | 2244 | } |
1922c949 DC |
2245 | bp = xfs_buf_read_uncached(mp, mp->m_rtdev_targp, |
2246 | d - XFS_FSB_TO_BB(mp, 1), | |
2247 | XFS_FSB_TO_B(mp, 1), 0); | |
2248 | if (!bp) { | |
2249 | cmn_err(CE_WARN, "XFS: realtime device size check failed"); | |
2250 | return EIO; | |
1da177e4 LT |
2251 | } |
2252 | xfs_buf_relse(bp); | |
2253 | return 0; | |
2254 | } | |
2255 | ||
2256 | /* | |
2257 | * Get the bitmap and summary inodes into the mount structure | |
2258 | * at mount time. | |
2259 | */ | |
2260 | int /* error */ | |
2261 | xfs_rtmount_inodes( | |
2262 | xfs_mount_t *mp) /* file system mount structure */ | |
2263 | { | |
2264 | int error; /* error return value */ | |
2265 | xfs_sb_t *sbp; | |
2266 | ||
2267 | sbp = &mp->m_sb; | |
2268 | if (sbp->sb_rbmino == NULLFSINO) | |
2269 | return 0; | |
7b6259e7 | 2270 | error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip); |
1da177e4 LT |
2271 | if (error) |
2272 | return error; | |
2273 | ASSERT(mp->m_rbmip != NULL); | |
2274 | ASSERT(sbp->sb_rsumino != NULLFSINO); | |
7b6259e7 | 2275 | error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip); |
1da177e4 | 2276 | if (error) { |
43355099 | 2277 | IRELE(mp->m_rbmip); |
1da177e4 LT |
2278 | return error; |
2279 | } | |
2280 | ASSERT(mp->m_rsumip != NULL); | |
2281 | return 0; | |
2282 | } | |
2283 | ||
b93b6e43 CH |
2284 | void |
2285 | xfs_rtunmount_inodes( | |
2286 | struct xfs_mount *mp) | |
2287 | { | |
2288 | if (mp->m_rbmip) | |
2289 | IRELE(mp->m_rbmip); | |
2290 | if (mp->m_rsumip) | |
2291 | IRELE(mp->m_rsumip); | |
2292 | } | |
2293 | ||
1da177e4 LT |
2294 | /* |
2295 | * Pick an extent for allocation at the start of a new realtime file. | |
2296 | * Use the sequence number stored in the atime field of the bitmap inode. | |
2297 | * Translate this to a fraction of the rtextents, and return the product | |
2298 | * of rtextents and the fraction. | |
2299 | * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ... | |
2300 | */ | |
2301 | int /* error */ | |
2302 | xfs_rtpick_extent( | |
2303 | xfs_mount_t *mp, /* file system mount point */ | |
2304 | xfs_trans_t *tp, /* transaction pointer */ | |
2305 | xfs_extlen_t len, /* allocation length (rtextents) */ | |
2306 | xfs_rtblock_t *pick) /* result rt extent */ | |
2307 | { | |
2308 | xfs_rtblock_t b; /* result block */ | |
2309 | int error; /* error return value */ | |
2310 | xfs_inode_t *ip; /* bitmap incore inode */ | |
2311 | int log2; /* log of sequence number */ | |
2312 | __uint64_t resid; /* residual after log removed */ | |
2313 | __uint64_t seq; /* sequence number of file creation */ | |
2314 | __uint64_t *seqp; /* pointer to seqno in inode */ | |
2315 | ||
07ff2fa8 NS |
2316 | if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, |
2317 | XFS_ILOCK_EXCL, &ip))) | |
1da177e4 LT |
2318 | return error; |
2319 | ASSERT(ip == mp->m_rbmip); | |
2320 | seqp = (__uint64_t *)&ip->i_d.di_atime; | |
2321 | if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) { | |
2322 | ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM; | |
2323 | *seqp = 0; | |
2324 | } | |
2325 | seq = *seqp; | |
2326 | if ((log2 = xfs_highbit64(seq)) == -1) | |
2327 | b = 0; | |
2328 | else { | |
2329 | resid = seq - (1ULL << log2); | |
2330 | b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >> | |
2331 | (log2 + 1); | |
2332 | if (b >= mp->m_sb.sb_rextents) | |
2333 | b = do_mod(b, mp->m_sb.sb_rextents); | |
2334 | if (b + len > mp->m_sb.sb_rextents) | |
2335 | b = mp->m_sb.sb_rextents - len; | |
2336 | } | |
2337 | *seqp = seq + 1; | |
2338 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
2339 | *pick = b; | |
2340 | return 0; | |
2341 | } |