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