]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_rtalloc.c
Update copyright dates (again)
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_rtalloc.c
1 /*
2 * Copyright (c) 2000-2002 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 #include <xfs.h>
37
38
39 /*
40 * Get a buffer for the bitmap or summary file block specified.
41 * The buffer is returned read and locked.
42 */
43 STATIC int /* error */
44 xfs_rtbuf_get(
45 xfs_mount_t *mp, /* file system mount structure */
46 xfs_trans_t *tp, /* transaction pointer */
47 xfs_rtblock_t block, /* block number in bitmap or summary */
48 int issum, /* is summary not bitmap */
49 xfs_buf_t **bpp) /* output: buffer for the block */
50 {
51 xfs_buf_t *bp; /* block buffer, result */
52 xfs_daddr_t d; /* disk addr of block */
53 int error; /* error value */
54 xfs_fsblock_t fsb; /* fs block number for block */
55 xfs_inode_t *ip; /* bitmap or summary inode */
56
57 ip = issum ? mp->m_rsumip : mp->m_rbmip;
58 /*
59 * Map from the file offset (block) and inode number to the
60 * file system block.
61 */
62 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
63 if (error) {
64 return error;
65 }
66 ASSERT(fsb != NULLFSBLOCK);
67 /*
68 * Convert to disk address for buffer cache.
69 */
70 d = XFS_FSB_TO_DADDR(mp, fsb);
71 /*
72 * Read the buffer.
73 */
74 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
75 mp->m_bsize, 0, &bp);
76 if (error) {
77 return error;
78 }
79 ASSERT(bp && !XFS_BUF_GETERROR(bp));
80 *bpp = bp;
81 return 0;
82 }
83
84 /*
85 * Searching backward from start to limit, find the first block whose
86 * allocated/free state is different from start's.
87 */
88 STATIC int /* error */
89 xfs_rtfind_back(
90 xfs_mount_t *mp, /* file system mount point */
91 xfs_trans_t *tp, /* transaction pointer */
92 xfs_rtblock_t start, /* starting block to look at */
93 xfs_rtblock_t limit, /* last block to look at */
94 xfs_rtblock_t *rtblock) /* out: start block found */
95 {
96 xfs_rtword_t *b; /* current word in buffer */
97 int bit; /* bit number in the word */
98 xfs_rtblock_t block; /* bitmap block number */
99 xfs_buf_t *bp; /* buf for the block */
100 xfs_rtword_t *bufp; /* starting word in buffer */
101 int error; /* error value */
102 xfs_rtblock_t firstbit; /* first useful bit in the word */
103 xfs_rtblock_t i; /* current bit number rel. to start */
104 xfs_rtblock_t len; /* length of inspected area */
105 xfs_rtword_t mask; /* mask of relevant bits for value */
106 xfs_rtword_t want; /* mask for "good" values */
107 xfs_rtword_t wdiff; /* difference from wanted value */
108 int word; /* word number in the buffer */
109
110 /*
111 * Compute and read in starting bitmap block for starting block.
112 */
113 block = XFS_BITTOBLOCK(mp, start);
114 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
115 if (error) {
116 return error;
117 }
118 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
119 /*
120 * Get the first word's index & point to it.
121 */
122 word = XFS_BITTOWORD(mp, start);
123 b = &bufp[word];
124 bit = (int)(start & (XFS_NBWORD - 1));
125 len = start - limit + 1;
126 /*
127 * Compute match value, based on the bit at start: if 1 (free)
128 * then all-ones, else all-zeroes.
129 */
130 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
131 /*
132 * If the starting position is not word-aligned, deal with the
133 * partial word.
134 */
135 if (bit < XFS_NBWORD - 1) {
136 /*
137 * Calculate first (leftmost) bit number to look at,
138 * and mask for all the relevant bits in this word.
139 */
140 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
141 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
142 firstbit;
143 /*
144 * Calculate the difference between the value there
145 * and what we're looking for.
146 */
147 if ((wdiff = (*b ^ want) & mask)) {
148 /*
149 * Different. Mark where we are and return.
150 */
151 xfs_trans_brelse(tp, bp);
152 i = bit - XFS_RTHIBIT(wdiff);
153 *rtblock = start - i + 1;
154 return 0;
155 }
156 i = bit - firstbit + 1;
157 /*
158 * Go on to previous block if that's where the previous word is
159 * and we need the previous word.
160 */
161 if (--word == -1 && i < len) {
162 /*
163 * If done with this block, get the previous one.
164 */
165 xfs_trans_brelse(tp, bp);
166 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
167 if (error) {
168 return error;
169 }
170 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
171 word = XFS_BLOCKWMASK(mp);
172 b = &bufp[word];
173 } else {
174 /*
175 * Go on to the previous word in the buffer.
176 */
177 b--;
178 }
179 } else {
180 /*
181 * Starting on a word boundary, no partial word.
182 */
183 i = 0;
184 }
185 /*
186 * Loop over whole words in buffers. When we use up one buffer
187 * we move on to the previous one.
188 */
189 while (len - i >= XFS_NBWORD) {
190 /*
191 * Compute difference between actual and desired value.
192 */
193 if ((wdiff = *b ^ want)) {
194 /*
195 * Different, mark where we are and return.
196 */
197 xfs_trans_brelse(tp, bp);
198 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
199 *rtblock = start - i + 1;
200 return 0;
201 }
202 i += XFS_NBWORD;
203 /*
204 * Go on to previous block if that's where the previous word is
205 * and we need the previous word.
206 */
207 if (--word == -1 && i < len) {
208 /*
209 * If done with this block, get the previous one.
210 */
211 xfs_trans_brelse(tp, bp);
212 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
213 if (error) {
214 return error;
215 }
216 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
217 word = XFS_BLOCKWMASK(mp);
218 b = &bufp[word];
219 } else {
220 /*
221 * Go on to the previous word in the buffer.
222 */
223 b--;
224 }
225 }
226 /*
227 * If not ending on a word boundary, deal with the last
228 * (partial) word.
229 */
230 if (len - i) {
231 /*
232 * Calculate first (leftmost) bit number to look at,
233 * and mask for all the relevant bits in this word.
234 */
235 firstbit = XFS_NBWORD - (len - i);
236 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
237 /*
238 * Compute difference between actual and desired value.
239 */
240 if ((wdiff = (*b ^ want) & mask)) {
241 /*
242 * Different, mark where we are and return.
243 */
244 xfs_trans_brelse(tp, bp);
245 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
246 *rtblock = start - i + 1;
247 return 0;
248 } else
249 i = len;
250 }
251 /*
252 * No match, return that we scanned the whole area.
253 */
254 xfs_trans_brelse(tp, bp);
255 *rtblock = start - i + 1;
256 return 0;
257 }
258
259 /*
260 * Searching forward from start to limit, find the first block whose
261 * allocated/free state is different from start's.
262 */
263 STATIC int /* error */
264 xfs_rtfind_forw(
265 xfs_mount_t *mp, /* file system mount point */
266 xfs_trans_t *tp, /* transaction pointer */
267 xfs_rtblock_t start, /* starting block to look at */
268 xfs_rtblock_t limit, /* last block to look at */
269 xfs_rtblock_t *rtblock) /* out: start block found */
270 {
271 xfs_rtword_t *b; /* current word in buffer */
272 int bit; /* bit number in the word */
273 xfs_rtblock_t block; /* bitmap block number */
274 xfs_buf_t *bp; /* buf for the block */
275 xfs_rtword_t *bufp; /* starting word in buffer */
276 int error; /* error value */
277 xfs_rtblock_t i; /* current bit number rel. to start */
278 xfs_rtblock_t lastbit; /* last useful bit in the word */
279 xfs_rtblock_t len; /* length of inspected area */
280 xfs_rtword_t mask; /* mask of relevant bits for value */
281 xfs_rtword_t want; /* mask for "good" values */
282 xfs_rtword_t wdiff; /* difference from wanted value */
283 int word; /* word number in the buffer */
284
285 /*
286 * Compute and read in starting bitmap block for starting block.
287 */
288 block = XFS_BITTOBLOCK(mp, start);
289 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
290 if (error) {
291 return error;
292 }
293 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
294 /*
295 * Get the first word's index & point to it.
296 */
297 word = XFS_BITTOWORD(mp, start);
298 b = &bufp[word];
299 bit = (int)(start & (XFS_NBWORD - 1));
300 len = limit - start + 1;
301 /*
302 * Compute match value, based on the bit at start: if 1 (free)
303 * then all-ones, else all-zeroes.
304 */
305 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
306 /*
307 * If the starting position is not word-aligned, deal with the
308 * partial word.
309 */
310 if (bit) {
311 /*
312 * Calculate last (rightmost) bit number to look at,
313 * and mask for all the relevant bits in this word.
314 */
315 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
316 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
317 /*
318 * Calculate the difference between the value there
319 * and what we're looking for.
320 */
321 if ((wdiff = (*b ^ want) & mask)) {
322 /*
323 * Different. Mark where we are and return.
324 */
325 xfs_trans_brelse(tp, bp);
326 i = XFS_RTLOBIT(wdiff) - bit;
327 *rtblock = start + i - 1;
328 return 0;
329 }
330 i = lastbit - bit;
331 /*
332 * Go on to next block if that's where the next word is
333 * and we need the next word.
334 */
335 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
336 /*
337 * If done with this block, get the previous one.
338 */
339 xfs_trans_brelse(tp, bp);
340 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
341 if (error) {
342 return error;
343 }
344 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
345 word = 0;
346 } else {
347 /*
348 * Go on to the previous word in the buffer.
349 */
350 b++;
351 }
352 } else {
353 /*
354 * Starting on a word boundary, no partial word.
355 */
356 i = 0;
357 }
358 /*
359 * Loop over whole words in buffers. When we use up one buffer
360 * we move on to the next one.
361 */
362 while (len - i >= XFS_NBWORD) {
363 /*
364 * Compute difference between actual and desired value.
365 */
366 if ((wdiff = *b ^ want)) {
367 /*
368 * Different, mark where we are and return.
369 */
370 xfs_trans_brelse(tp, bp);
371 i += XFS_RTLOBIT(wdiff);
372 *rtblock = start + i - 1;
373 return 0;
374 }
375 i += XFS_NBWORD;
376 /*
377 * Go on to next block if that's where the next word is
378 * and we need the next word.
379 */
380 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
381 /*
382 * If done with this block, get the next one.
383 */
384 xfs_trans_brelse(tp, bp);
385 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
386 if (error) {
387 return error;
388 }
389 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
390 word = 0;
391 } else {
392 /*
393 * Go on to the next word in the buffer.
394 */
395 b++;
396 }
397 }
398 /*
399 * If not ending on a word boundary, deal with the last
400 * (partial) word.
401 */
402 if ((lastbit = len - i)) {
403 /*
404 * Calculate mask for all the relevant bits in this word.
405 */
406 mask = ((xfs_rtword_t)1 << lastbit) - 1;
407 /*
408 * Compute difference between actual and desired value.
409 */
410 if ((wdiff = (*b ^ want) & mask)) {
411 /*
412 * Different, mark where we are and return.
413 */
414 xfs_trans_brelse(tp, bp);
415 i += XFS_RTLOBIT(wdiff);
416 *rtblock = start + i - 1;
417 return 0;
418 } else
419 i = len;
420 }
421 /*
422 * No match, return that we scanned the whole area.
423 */
424 xfs_trans_brelse(tp, bp);
425 *rtblock = start + i - 1;
426 return 0;
427 }
428
429 /*
430 * Mark an extent specified by start and len freed.
431 * Updates all the summary information as well as the bitmap.
432 */
433 STATIC int /* error */
434 xfs_rtfree_range(
435 xfs_mount_t *mp, /* file system mount point */
436 xfs_trans_t *tp, /* transaction pointer */
437 xfs_rtblock_t start, /* starting block to free */
438 xfs_extlen_t len, /* length to free */
439 xfs_buf_t **rbpp, /* in/out: summary block buffer */
440 xfs_fsblock_t *rsb) /* in/out: summary block number */
441 {
442 xfs_rtblock_t end; /* end of the freed extent */
443 int error; /* error value */
444 xfs_rtblock_t postblock; /* first block freed > end */
445 xfs_rtblock_t preblock; /* first block freed < start */
446
447 end = start + len - 1;
448 /*
449 * Modify the bitmap to mark this extent freed.
450 */
451 error = xfs_rtmodify_range(mp, tp, start, len, 1);
452 if (error) {
453 return error;
454 }
455 /*
456 * Assume we're freeing out of the middle of an allocated extent.
457 * We need to find the beginning and end of the extent so we can
458 * properly update the summary.
459 */
460 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
461 if (error) {
462 return error;
463 }
464 /*
465 * Find the next allocated block (end of allocated extent).
466 */
467 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
468 &postblock);
469 /*
470 * If there are blocks not being freed at the front of the
471 * old extent, add summary data for them to be allocated.
472 */
473 if (preblock < start) {
474 error = xfs_rtmodify_summary(mp, tp,
475 XFS_RTBLOCKLOG(start - preblock),
476 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
477 if (error) {
478 return error;
479 }
480 }
481 /*
482 * If there are blocks not being freed at the end of the
483 * old extent, add summary data for them to be allocated.
484 */
485 if (postblock > end) {
486 error = xfs_rtmodify_summary(mp, tp,
487 XFS_RTBLOCKLOG(postblock - end),
488 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
489 if (error) {
490 return error;
491 }
492 }
493 /*
494 * Increment the summary information corresponding to the entire
495 * (new) free extent.
496 */
497 error = xfs_rtmodify_summary(mp, tp,
498 XFS_RTBLOCKLOG(postblock + 1 - preblock),
499 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
500 return error;
501 }
502
503 /*
504 * Set the given range of bitmap bits to the given value.
505 * Do whatever I/O and logging is required.
506 */
507 STATIC int /* error */
508 xfs_rtmodify_range(
509 xfs_mount_t *mp, /* file system mount point */
510 xfs_trans_t *tp, /* transaction pointer */
511 xfs_rtblock_t start, /* starting block to modify */
512 xfs_extlen_t len, /* length of extent to modify */
513 int val) /* 1 for free, 0 for allocated */
514 {
515 xfs_rtword_t *b; /* current word in buffer */
516 int bit; /* bit number in the word */
517 xfs_rtblock_t block; /* bitmap block number */
518 xfs_buf_t *bp; /* buf for the block */
519 xfs_rtword_t *bufp; /* starting word in buffer */
520 int error; /* error value */
521 xfs_rtword_t *first; /* first used word in the buffer */
522 int i; /* current bit number rel. to start */
523 int lastbit; /* last useful bit in word */
524 xfs_rtword_t mask; /* mask o frelevant bits for value */
525 int word; /* word number in the buffer */
526
527 /*
528 * Compute starting bitmap block number.
529 */
530 block = XFS_BITTOBLOCK(mp, start);
531 /*
532 * Read the bitmap block, and point to its data.
533 */
534 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
535 if (error) {
536 return error;
537 }
538 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
539 /*
540 * Compute the starting word's address, and starting bit.
541 */
542 word = XFS_BITTOWORD(mp, start);
543 first = b = &bufp[word];
544 bit = (int)(start & (XFS_NBWORD - 1));
545 /*
546 * 0 (allocated) => all zeroes; 1 (free) => all ones.
547 */
548 val = -val;
549 /*
550 * If not starting on a word boundary, deal with the first
551 * (partial) word.
552 */
553 if (bit) {
554 /*
555 * Compute first bit not changed and mask of relevant bits.
556 */
557 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
558 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
559 /*
560 * Set/clear the active bits.
561 */
562 if (val)
563 *b |= mask;
564 else
565 *b &= ~mask;
566 i = lastbit - bit;
567 /*
568 * Go on to the next block if that's where the next word is
569 * and we need the next word.
570 */
571 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
572 /*
573 * Log the changed part of this block.
574 * Get the next one.
575 */
576 xfs_trans_log_buf(tp, bp,
577 (uint)((char *)first - (char *)bufp),
578 (uint)((char *)b - (char *)bufp));
579 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
580 if (error) {
581 return error;
582 }
583 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
584 word = 0;
585 } else {
586 /*
587 * Go on to the next word in the buffer
588 */
589 b++;
590 }
591 } else {
592 /*
593 * Starting on a word boundary, no partial word.
594 */
595 i = 0;
596 }
597 /*
598 * Loop over whole words in buffers. When we use up one buffer
599 * we move on to the next one.
600 */
601 while (len - i >= XFS_NBWORD) {
602 /*
603 * Set the word value correctly.
604 */
605 *b = val;
606 i += XFS_NBWORD;
607 /*
608 * Go on to the next block if that's where the next word is
609 * and we need the next word.
610 */
611 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
612 /*
613 * Log the changed part of this block.
614 * Get the next one.
615 */
616 xfs_trans_log_buf(tp, bp,
617 (uint)((char *)first - (char *)bufp),
618 (uint)((char *)b - (char *)bufp));
619 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
620 if (error) {
621 return error;
622 }
623 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
624 word = 0;
625 } else {
626 /*
627 * Go on to the next word in the buffer
628 */
629 b++;
630 }
631 }
632 /*
633 * If not ending on a word boundary, deal with the last
634 * (partial) word.
635 */
636 if ((lastbit = len - i)) {
637 /*
638 * Compute a mask of relevant bits.
639 */
640 bit = 0;
641 mask = ((xfs_rtword_t)1 << lastbit) - 1;
642 /*
643 * Set/clear the active bits.
644 */
645 if (val)
646 *b |= mask;
647 else
648 *b &= ~mask;
649 b++;
650 }
651 /*
652 * Log any remaining changed bytes.
653 */
654 if (b > first)
655 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
656 (uint)((char *)b - (char *)bufp - 1));
657 return 0;
658 }
659
660 /*
661 * Read and modify the summary information for a given extent size,
662 * bitmap block combination.
663 * Keeps track of a current summary block, so we don't keep reading
664 * it from the buffer cache.
665 */
666 STATIC int /* error */
667 xfs_rtmodify_summary(
668 xfs_mount_t *mp, /* file system mount point */
669 xfs_trans_t *tp, /* transaction pointer */
670 int log, /* log2 of extent size */
671 xfs_rtblock_t bbno, /* bitmap block number */
672 int delta, /* change to make to summary info */
673 xfs_buf_t **rbpp, /* in/out: summary block buffer */
674 xfs_fsblock_t *rsb) /* in/out: summary block number */
675 {
676 xfs_buf_t *bp; /* buffer for the summary block */
677 int error; /* error value */
678 xfs_fsblock_t sb; /* summary fsblock */
679 int so; /* index into the summary file */
680 xfs_suminfo_t *sp; /* pointer to returned data */
681
682 /*
683 * Compute entry number in the summary file.
684 */
685 so = XFS_SUMOFFS(mp, log, bbno);
686 /*
687 * Compute the block number in the summary file.
688 */
689 sb = XFS_SUMOFFSTOBLOCK(mp, so);
690 /*
691 * If we have an old buffer, and the block number matches, use that.
692 */
693 if (rbpp && *rbpp && *rsb == sb)
694 bp = *rbpp;
695 /*
696 * Otherwise we have to get the buffer.
697 */
698 else {
699 /*
700 * If there was an old one, get rid of it first.
701 */
702 if (rbpp && *rbpp)
703 xfs_trans_brelse(tp, *rbpp);
704 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
705 if (error) {
706 return error;
707 }
708 /*
709 * Remember this buffer and block for the next call.
710 */
711 if (rbpp) {
712 *rbpp = bp;
713 *rsb = sb;
714 }
715 }
716 /*
717 * Point to the summary information, modify and log it.
718 */
719 sp = XFS_SUMPTR(mp, bp, so);
720 *sp += delta;
721 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
722 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
723 return 0;
724 }
725
726 /*
727 * Free an extent in the realtime subvolume. Length is expressed in
728 * realtime extents, as is the block number.
729 */
730 int /* error */
731 xfs_rtfree_extent(
732 xfs_trans_t *tp, /* transaction pointer */
733 xfs_rtblock_t bno, /* starting block number to free */
734 xfs_extlen_t len) /* length of extent freed */
735 {
736 int error; /* error value */
737 xfs_inode_t *ip; /* bitmap file inode */
738 xfs_mount_t *mp; /* file system mount structure */
739 xfs_fsblock_t sb; /* summary file block number */
740 xfs_buf_t *sumbp; /* summary file block buffer */
741
742 mp = tp->t_mountp;
743 /*
744 * Synchronize by locking the bitmap inode.
745 */
746 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, XFS_ILOCK_EXCL, &ip);
747 if (error) {
748 return error;
749 }
750 #if defined(__KERNEL__) && defined(DEBUG)
751 /*
752 * Check to see that this whole range is currently allocated.
753 */
754 {
755 int stat; /* result from checking range */
756
757 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
758 if (error) {
759 return error;
760 }
761 ASSERT(stat);
762 }
763 #endif
764 sumbp = NULL;
765 /*
766 * Free the range of realtime blocks.
767 */
768 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
769 if (error) {
770 return error;
771 }
772 /*
773 * Mark more blocks free in the superblock.
774 */
775 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
776 /*
777 * If we've now freed all the blocks, reset the file sequence
778 * number to 0.
779 */
780 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
781 mp->m_sb.sb_rextents) {
782 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
783 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
784 *(__uint64_t *)&ip->i_d.di_atime = 0;
785 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
786 }
787 return 0;
788 }
789
790 /*
791 * Initialize realtime fields in the mount structure.
792 */
793 int /* error */
794 xfs_rtmount_init(
795 xfs_mount_t *mp) /* file system mount structure */
796 {
797 xfs_buf_t *bp; /* buffer for last block of subvolume */
798 xfs_daddr_t d; /* address of last block of subvolume */
799 int error; /* error return value */
800 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
801
802 sbp = &mp->m_sb;
803 if (sbp->sb_rblocks == 0)
804 return 0;
805 if (kdev_none(mp->m_rtdev)) {
806 printk(KERN_WARNING
807 "XFS: This FS has an RT subvol - specify -o rtdev on mount\n");
808 return XFS_ERROR(ENODEV);
809 }
810 mp->m_rsumlevels = sbp->sb_rextslog + 1;
811 mp->m_rsumsize =
812 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
813 sbp->sb_rbmblocks;
814 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
815 mp->m_rbmip = mp->m_rsumip = NULL;
816 /*
817 * Check that the realtime section is an ok size.
818 */
819 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
820 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
821 printk(KERN_WARNING "XFS: RT mount - %llu != %llu\n",
822 (unsigned long long) XFS_BB_TO_FSB(mp, d),
823 (unsigned long long) mp->m_sb.sb_rblocks);
824 return XFS_ERROR(E2BIG);
825 }
826 error = xfs_read_buf(mp, m_rtdev_targp(mp), d - 1, 1, 0, &bp);
827 if (error) {
828 printk(KERN_WARNING
829 "XFS: RT mount - xfs_read_buf returned %d\n", error);
830 if (error == ENOSPC)
831 return XFS_ERROR(E2BIG);
832 return error;
833 }
834 xfs_buf_relse(bp);
835 return 0;
836 }