Linux 3.16-rc2
[linux/fpc-iii.git] / fs / xfs / xfs_rtbitmap.c
blobf4dd697cac08720f057b58c094bc4bfe3346ca5b
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_bmap.h"
30 #include "xfs_bmap_util.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_error.h"
34 #include "xfs_trans.h"
35 #include "xfs_trans_space.h"
36 #include "xfs_trace.h"
37 #include "xfs_buf.h"
38 #include "xfs_icache.h"
39 #include "xfs_dinode.h"
40 #include "xfs_rtalloc.h"
44 * Realtime allocator bitmap functions shared with userspace.
48 * Get a buffer for the bitmap or summary file block specified.
49 * The buffer is returned read and locked.
51 int
52 xfs_rtbuf_get(
53 xfs_mount_t *mp, /* file system mount structure */
54 xfs_trans_t *tp, /* transaction pointer */
55 xfs_rtblock_t block, /* block number in bitmap or summary */
56 int issum, /* is summary not bitmap */
57 xfs_buf_t **bpp) /* output: buffer for the block */
59 xfs_buf_t *bp; /* block buffer, result */
60 xfs_inode_t *ip; /* bitmap or summary inode */
61 xfs_bmbt_irec_t map;
62 int nmap = 1;
63 int error; /* error value */
65 ip = issum ? mp->m_rsumip : mp->m_rbmip;
67 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
68 if (error)
69 return error;
71 ASSERT(map.br_startblock != NULLFSBLOCK);
72 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
73 XFS_FSB_TO_DADDR(mp, map.br_startblock),
74 mp->m_bsize, 0, &bp, NULL);
75 if (error)
76 return error;
77 *bpp = bp;
78 return 0;
82 * Searching backward from start to limit, find the first block whose
83 * allocated/free state is different from start's.
85 int
86 xfs_rtfind_back(
87 xfs_mount_t *mp, /* file system mount point */
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_rtblock_t start, /* starting block to look at */
90 xfs_rtblock_t limit, /* last block to look at */
91 xfs_rtblock_t *rtblock) /* out: start block found */
93 xfs_rtword_t *b; /* current word in buffer */
94 int bit; /* bit number in the word */
95 xfs_rtblock_t block; /* bitmap block number */
96 xfs_buf_t *bp; /* buf for the block */
97 xfs_rtword_t *bufp; /* starting word in buffer */
98 int error; /* error value */
99 xfs_rtblock_t firstbit; /* first useful bit in the word */
100 xfs_rtblock_t i; /* current bit number rel. to start */
101 xfs_rtblock_t len; /* length of inspected area */
102 xfs_rtword_t mask; /* mask of relevant bits for value */
103 xfs_rtword_t want; /* mask for "good" values */
104 xfs_rtword_t wdiff; /* difference from wanted value */
105 int word; /* word number in the buffer */
108 * Compute and read in starting bitmap block for starting block.
110 block = XFS_BITTOBLOCK(mp, start);
111 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
112 if (error) {
113 return error;
115 bufp = bp->b_addr;
117 * Get the first word's index & point to it.
119 word = XFS_BITTOWORD(mp, start);
120 b = &bufp[word];
121 bit = (int)(start & (XFS_NBWORD - 1));
122 len = start - limit + 1;
124 * Compute match value, based on the bit at start: if 1 (free)
125 * then all-ones, else all-zeroes.
127 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
129 * If the starting position is not word-aligned, deal with the
130 * partial word.
132 if (bit < XFS_NBWORD - 1) {
134 * Calculate first (leftmost) bit number to look at,
135 * and mask for all the relevant bits in this word.
137 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
138 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
139 firstbit;
141 * Calculate the difference between the value there
142 * and what we're looking for.
144 if ((wdiff = (*b ^ want) & mask)) {
146 * Different. Mark where we are and return.
148 xfs_trans_brelse(tp, bp);
149 i = bit - XFS_RTHIBIT(wdiff);
150 *rtblock = start - i + 1;
151 return 0;
153 i = bit - firstbit + 1;
155 * Go on to previous block if that's where the previous word is
156 * and we need the previous word.
158 if (--word == -1 && i < len) {
160 * If done with this block, get the previous one.
162 xfs_trans_brelse(tp, bp);
163 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
164 if (error) {
165 return error;
167 bufp = bp->b_addr;
168 word = XFS_BLOCKWMASK(mp);
169 b = &bufp[word];
170 } else {
172 * Go on to the previous word in the buffer.
174 b--;
176 } else {
178 * Starting on a word boundary, no partial word.
180 i = 0;
183 * Loop over whole words in buffers. When we use up one buffer
184 * we move on to the previous one.
186 while (len - i >= XFS_NBWORD) {
188 * Compute difference between actual and desired value.
190 if ((wdiff = *b ^ want)) {
192 * Different, mark where we are and return.
194 xfs_trans_brelse(tp, bp);
195 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
196 *rtblock = start - i + 1;
197 return 0;
199 i += XFS_NBWORD;
201 * Go on to previous block if that's where the previous word is
202 * and we need the previous word.
204 if (--word == -1 && i < len) {
206 * If done with this block, get the previous one.
208 xfs_trans_brelse(tp, bp);
209 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
210 if (error) {
211 return error;
213 bufp = bp->b_addr;
214 word = XFS_BLOCKWMASK(mp);
215 b = &bufp[word];
216 } else {
218 * Go on to the previous word in the buffer.
220 b--;
224 * If not ending on a word boundary, deal with the last
225 * (partial) word.
227 if (len - i) {
229 * Calculate first (leftmost) bit number to look at,
230 * and mask for all the relevant bits in this word.
232 firstbit = XFS_NBWORD - (len - i);
233 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
235 * Compute difference between actual and desired value.
237 if ((wdiff = (*b ^ want) & mask)) {
239 * Different, mark where we are and return.
241 xfs_trans_brelse(tp, bp);
242 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
243 *rtblock = start - i + 1;
244 return 0;
245 } else
246 i = len;
249 * No match, return that we scanned the whole area.
251 xfs_trans_brelse(tp, bp);
252 *rtblock = start - i + 1;
253 return 0;
257 * Searching forward from start to limit, find the first block whose
258 * allocated/free state is different from start's.
261 xfs_rtfind_forw(
262 xfs_mount_t *mp, /* file system mount point */
263 xfs_trans_t *tp, /* transaction pointer */
264 xfs_rtblock_t start, /* starting block to look at */
265 xfs_rtblock_t limit, /* last block to look at */
266 xfs_rtblock_t *rtblock) /* out: start block found */
268 xfs_rtword_t *b; /* current word in buffer */
269 int bit; /* bit number in the word */
270 xfs_rtblock_t block; /* bitmap block number */
271 xfs_buf_t *bp; /* buf for the block */
272 xfs_rtword_t *bufp; /* starting word in buffer */
273 int error; /* error value */
274 xfs_rtblock_t i; /* current bit number rel. to start */
275 xfs_rtblock_t lastbit; /* last useful bit in the word */
276 xfs_rtblock_t len; /* length of inspected area */
277 xfs_rtword_t mask; /* mask of relevant bits for value */
278 xfs_rtword_t want; /* mask for "good" values */
279 xfs_rtword_t wdiff; /* difference from wanted value */
280 int word; /* word number in the buffer */
283 * Compute and read in starting bitmap block for starting block.
285 block = XFS_BITTOBLOCK(mp, start);
286 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
287 if (error) {
288 return error;
290 bufp = bp->b_addr;
292 * Get the first word's index & point to it.
294 word = XFS_BITTOWORD(mp, start);
295 b = &bufp[word];
296 bit = (int)(start & (XFS_NBWORD - 1));
297 len = limit - start + 1;
299 * Compute match value, based on the bit at start: if 1 (free)
300 * then all-ones, else all-zeroes.
302 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
304 * If the starting position is not word-aligned, deal with the
305 * partial word.
307 if (bit) {
309 * Calculate last (rightmost) bit number to look at,
310 * and mask for all the relevant bits in this word.
312 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
313 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
315 * Calculate the difference between the value there
316 * and what we're looking for.
318 if ((wdiff = (*b ^ want) & mask)) {
320 * Different. Mark where we are and return.
322 xfs_trans_brelse(tp, bp);
323 i = XFS_RTLOBIT(wdiff) - bit;
324 *rtblock = start + i - 1;
325 return 0;
327 i = lastbit - bit;
329 * Go on to next block if that's where the next word is
330 * and we need the next word.
332 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
334 * If done with this block, get the previous one.
336 xfs_trans_brelse(tp, bp);
337 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
338 if (error) {
339 return error;
341 b = bufp = bp->b_addr;
342 word = 0;
343 } else {
345 * Go on to the previous word in the buffer.
347 b++;
349 } else {
351 * Starting on a word boundary, no partial word.
353 i = 0;
356 * Loop over whole words in buffers. When we use up one buffer
357 * we move on to the next one.
359 while (len - i >= XFS_NBWORD) {
361 * Compute difference between actual and desired value.
363 if ((wdiff = *b ^ want)) {
365 * Different, mark where we are and return.
367 xfs_trans_brelse(tp, bp);
368 i += XFS_RTLOBIT(wdiff);
369 *rtblock = start + i - 1;
370 return 0;
372 i += XFS_NBWORD;
374 * Go on to next block if that's where the next word is
375 * and we need the next word.
377 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
379 * If done with this block, get the next one.
381 xfs_trans_brelse(tp, bp);
382 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
383 if (error) {
384 return error;
386 b = bufp = bp->b_addr;
387 word = 0;
388 } else {
390 * Go on to the next word in the buffer.
392 b++;
396 * If not ending on a word boundary, deal with the last
397 * (partial) word.
399 if ((lastbit = len - i)) {
401 * Calculate mask for all the relevant bits in this word.
403 mask = ((xfs_rtword_t)1 << lastbit) - 1;
405 * Compute difference between actual and desired value.
407 if ((wdiff = (*b ^ want) & mask)) {
409 * Different, mark where we are and return.
411 xfs_trans_brelse(tp, bp);
412 i += XFS_RTLOBIT(wdiff);
413 *rtblock = start + i - 1;
414 return 0;
415 } else
416 i = len;
419 * No match, return that we scanned the whole area.
421 xfs_trans_brelse(tp, bp);
422 *rtblock = start + i - 1;
423 return 0;
427 * Read and modify the summary information for a given extent size,
428 * bitmap block combination.
429 * Keeps track of a current summary block, so we don't keep reading
430 * it from the buffer cache.
433 xfs_rtmodify_summary(
434 xfs_mount_t *mp, /* file system mount point */
435 xfs_trans_t *tp, /* transaction pointer */
436 int log, /* log2 of extent size */
437 xfs_rtblock_t bbno, /* bitmap block number */
438 int delta, /* change to make to summary info */
439 xfs_buf_t **rbpp, /* in/out: summary block buffer */
440 xfs_fsblock_t *rsb) /* in/out: summary block number */
442 xfs_buf_t *bp; /* buffer for the summary block */
443 int error; /* error value */
444 xfs_fsblock_t sb; /* summary fsblock */
445 int so; /* index into the summary file */
446 xfs_suminfo_t *sp; /* pointer to returned data */
449 * Compute entry number in the summary file.
451 so = XFS_SUMOFFS(mp, log, bbno);
453 * Compute the block number in the summary file.
455 sb = XFS_SUMOFFSTOBLOCK(mp, so);
457 * If we have an old buffer, and the block number matches, use that.
459 if (rbpp && *rbpp && *rsb == sb)
460 bp = *rbpp;
462 * Otherwise we have to get the buffer.
464 else {
466 * If there was an old one, get rid of it first.
468 if (rbpp && *rbpp)
469 xfs_trans_brelse(tp, *rbpp);
470 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
471 if (error) {
472 return error;
475 * Remember this buffer and block for the next call.
477 if (rbpp) {
478 *rbpp = bp;
479 *rsb = sb;
483 * Point to the summary information, modify and log it.
485 sp = XFS_SUMPTR(mp, bp, so);
486 *sp += delta;
487 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
488 (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
489 return 0;
493 * Set the given range of bitmap bits to the given value.
494 * Do whatever I/O and logging is required.
497 xfs_rtmodify_range(
498 xfs_mount_t *mp, /* file system mount point */
499 xfs_trans_t *tp, /* transaction pointer */
500 xfs_rtblock_t start, /* starting block to modify */
501 xfs_extlen_t len, /* length of extent to modify */
502 int val) /* 1 for free, 0 for allocated */
504 xfs_rtword_t *b; /* current word in buffer */
505 int bit; /* bit number in the word */
506 xfs_rtblock_t block; /* bitmap block number */
507 xfs_buf_t *bp; /* buf for the block */
508 xfs_rtword_t *bufp; /* starting word in buffer */
509 int error; /* error value */
510 xfs_rtword_t *first; /* first used word in the buffer */
511 int i; /* current bit number rel. to start */
512 int lastbit; /* last useful bit in word */
513 xfs_rtword_t mask; /* mask o frelevant bits for value */
514 int word; /* word number in the buffer */
517 * Compute starting bitmap block number.
519 block = XFS_BITTOBLOCK(mp, start);
521 * Read the bitmap block, and point to its data.
523 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
524 if (error) {
525 return error;
527 bufp = bp->b_addr;
529 * Compute the starting word's address, and starting bit.
531 word = XFS_BITTOWORD(mp, start);
532 first = b = &bufp[word];
533 bit = (int)(start & (XFS_NBWORD - 1));
535 * 0 (allocated) => all zeroes; 1 (free) => all ones.
537 val = -val;
539 * If not starting on a word boundary, deal with the first
540 * (partial) word.
542 if (bit) {
544 * Compute first bit not changed and mask of relevant bits.
546 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
547 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
549 * Set/clear the active bits.
551 if (val)
552 *b |= mask;
553 else
554 *b &= ~mask;
555 i = lastbit - bit;
557 * Go on to the next block if that's where the next word is
558 * and we need the next word.
560 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
562 * Log the changed part of this block.
563 * Get the next one.
565 xfs_trans_log_buf(tp, bp,
566 (uint)((char *)first - (char *)bufp),
567 (uint)((char *)b - (char *)bufp));
568 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
569 if (error) {
570 return error;
572 first = b = bufp = bp->b_addr;
573 word = 0;
574 } else {
576 * Go on to the next word in the buffer
578 b++;
580 } else {
582 * Starting on a word boundary, no partial word.
584 i = 0;
587 * Loop over whole words in buffers. When we use up one buffer
588 * we move on to the next one.
590 while (len - i >= XFS_NBWORD) {
592 * Set the word value correctly.
594 *b = val;
595 i += XFS_NBWORD;
597 * Go on to the next block if that's where the next word is
598 * and we need the next word.
600 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
602 * Log the changed part of this block.
603 * Get the next one.
605 xfs_trans_log_buf(tp, bp,
606 (uint)((char *)first - (char *)bufp),
607 (uint)((char *)b - (char *)bufp));
608 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
609 if (error) {
610 return error;
612 first = b = bufp = bp->b_addr;
613 word = 0;
614 } else {
616 * Go on to the next word in the buffer
618 b++;
622 * If not ending on a word boundary, deal with the last
623 * (partial) word.
625 if ((lastbit = len - i)) {
627 * Compute a mask of relevant bits.
629 bit = 0;
630 mask = ((xfs_rtword_t)1 << lastbit) - 1;
632 * Set/clear the active bits.
634 if (val)
635 *b |= mask;
636 else
637 *b &= ~mask;
638 b++;
641 * Log any remaining changed bytes.
643 if (b > first)
644 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
645 (uint)((char *)b - (char *)bufp - 1));
646 return 0;
650 * Mark an extent specified by start and len freed.
651 * Updates all the summary information as well as the bitmap.
654 xfs_rtfree_range(
655 xfs_mount_t *mp, /* file system mount point */
656 xfs_trans_t *tp, /* transaction pointer */
657 xfs_rtblock_t start, /* starting block to free */
658 xfs_extlen_t len, /* length to free */
659 xfs_buf_t **rbpp, /* in/out: summary block buffer */
660 xfs_fsblock_t *rsb) /* in/out: summary block number */
662 xfs_rtblock_t end; /* end of the freed extent */
663 int error; /* error value */
664 xfs_rtblock_t postblock; /* first block freed > end */
665 xfs_rtblock_t preblock; /* first block freed < start */
667 end = start + len - 1;
669 * Modify the bitmap to mark this extent freed.
671 error = xfs_rtmodify_range(mp, tp, start, len, 1);
672 if (error) {
673 return error;
676 * Assume we're freeing out of the middle of an allocated extent.
677 * We need to find the beginning and end of the extent so we can
678 * properly update the summary.
680 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
681 if (error) {
682 return error;
685 * Find the next allocated block (end of allocated extent).
687 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
688 &postblock);
689 if (error)
690 return error;
692 * If there are blocks not being freed at the front of the
693 * old extent, add summary data for them to be allocated.
695 if (preblock < start) {
696 error = xfs_rtmodify_summary(mp, tp,
697 XFS_RTBLOCKLOG(start - preblock),
698 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
699 if (error) {
700 return error;
704 * If there are blocks not being freed at the end of the
705 * old extent, add summary data for them to be allocated.
707 if (postblock > end) {
708 error = xfs_rtmodify_summary(mp, tp,
709 XFS_RTBLOCKLOG(postblock - end),
710 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
711 if (error) {
712 return error;
716 * Increment the summary information corresponding to the entire
717 * (new) free extent.
719 error = xfs_rtmodify_summary(mp, tp,
720 XFS_RTBLOCKLOG(postblock + 1 - preblock),
721 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
722 return error;
726 * Check that the given range is either all allocated (val = 0) or
727 * all free (val = 1).
730 xfs_rtcheck_range(
731 xfs_mount_t *mp, /* file system mount point */
732 xfs_trans_t *tp, /* transaction pointer */
733 xfs_rtblock_t start, /* starting block number of extent */
734 xfs_extlen_t len, /* length of extent */
735 int val, /* 1 for free, 0 for allocated */
736 xfs_rtblock_t *new, /* out: first block not matching */
737 int *stat) /* out: 1 for matches, 0 for not */
739 xfs_rtword_t *b; /* current word in buffer */
740 int bit; /* bit number in the word */
741 xfs_rtblock_t block; /* bitmap block number */
742 xfs_buf_t *bp; /* buf for the block */
743 xfs_rtword_t *bufp; /* starting word in buffer */
744 int error; /* error value */
745 xfs_rtblock_t i; /* current bit number rel. to start */
746 xfs_rtblock_t lastbit; /* last useful bit in word */
747 xfs_rtword_t mask; /* mask of relevant bits for value */
748 xfs_rtword_t wdiff; /* difference from wanted value */
749 int word; /* word number in the buffer */
752 * Compute starting bitmap block number
754 block = XFS_BITTOBLOCK(mp, start);
756 * Read the bitmap block.
758 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
759 if (error) {
760 return error;
762 bufp = bp->b_addr;
764 * Compute the starting word's address, and starting bit.
766 word = XFS_BITTOWORD(mp, start);
767 b = &bufp[word];
768 bit = (int)(start & (XFS_NBWORD - 1));
770 * 0 (allocated) => all zero's; 1 (free) => all one's.
772 val = -val;
774 * If not starting on a word boundary, deal with the first
775 * (partial) word.
777 if (bit) {
779 * Compute first bit not examined.
781 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
783 * Mask of relevant bits.
785 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
787 * Compute difference between actual and desired value.
789 if ((wdiff = (*b ^ val) & mask)) {
791 * Different, compute first wrong bit and return.
793 xfs_trans_brelse(tp, bp);
794 i = XFS_RTLOBIT(wdiff) - bit;
795 *new = start + i;
796 *stat = 0;
797 return 0;
799 i = lastbit - bit;
801 * Go on to next block if that's where the next word is
802 * and we need the next word.
804 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
806 * If done with this block, get the next one.
808 xfs_trans_brelse(tp, bp);
809 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
810 if (error) {
811 return error;
813 b = bufp = bp->b_addr;
814 word = 0;
815 } else {
817 * Go on to the next word in the buffer.
819 b++;
821 } else {
823 * Starting on a word boundary, no partial word.
825 i = 0;
828 * Loop over whole words in buffers. When we use up one buffer
829 * we move on to the next one.
831 while (len - i >= XFS_NBWORD) {
833 * Compute difference between actual and desired value.
835 if ((wdiff = *b ^ val)) {
837 * Different, compute first wrong bit and return.
839 xfs_trans_brelse(tp, bp);
840 i += XFS_RTLOBIT(wdiff);
841 *new = start + i;
842 *stat = 0;
843 return 0;
845 i += XFS_NBWORD;
847 * Go on to next block if that's where the next word is
848 * and we need the next word.
850 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
852 * If done with this block, get the next one.
854 xfs_trans_brelse(tp, bp);
855 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
856 if (error) {
857 return error;
859 b = bufp = bp->b_addr;
860 word = 0;
861 } else {
863 * Go on to the next word in the buffer.
865 b++;
869 * If not ending on a word boundary, deal with the last
870 * (partial) word.
872 if ((lastbit = len - i)) {
874 * Mask of relevant bits.
876 mask = ((xfs_rtword_t)1 << lastbit) - 1;
878 * Compute difference between actual and desired value.
880 if ((wdiff = (*b ^ val) & mask)) {
882 * Different, compute first wrong bit and return.
884 xfs_trans_brelse(tp, bp);
885 i += XFS_RTLOBIT(wdiff);
886 *new = start + i;
887 *stat = 0;
888 return 0;
889 } else
890 i = len;
893 * Successful, return.
895 xfs_trans_brelse(tp, bp);
896 *new = start + i;
897 *stat = 1;
898 return 0;
901 #ifdef DEBUG
903 * Check that the given extent (block range) is allocated already.
905 STATIC int /* error */
906 xfs_rtcheck_alloc_range(
907 xfs_mount_t *mp, /* file system mount point */
908 xfs_trans_t *tp, /* transaction pointer */
909 xfs_rtblock_t bno, /* starting block number of extent */
910 xfs_extlen_t len) /* length of extent */
912 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
913 int stat;
914 int error;
916 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
917 if (error)
918 return error;
919 ASSERT(stat);
920 return 0;
922 #else
923 #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
924 #endif
926 * Free an extent in the realtime subvolume. Length is expressed in
927 * realtime extents, as is the block number.
929 int /* error */
930 xfs_rtfree_extent(
931 xfs_trans_t *tp, /* transaction pointer */
932 xfs_rtblock_t bno, /* starting block number to free */
933 xfs_extlen_t len) /* length of extent freed */
935 int error; /* error value */
936 xfs_mount_t *mp; /* file system mount structure */
937 xfs_fsblock_t sb; /* summary file block number */
938 xfs_buf_t *sumbp = NULL; /* summary file block buffer */
940 mp = tp->t_mountp;
942 ASSERT(mp->m_rbmip->i_itemp != NULL);
943 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
945 error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
946 if (error)
947 return error;
950 * Free the range of realtime blocks.
952 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
953 if (error) {
954 return error;
957 * Mark more blocks free in the superblock.
959 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
961 * If we've now freed all the blocks, reset the file sequence
962 * number to 0.
964 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
965 mp->m_sb.sb_rextents) {
966 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
967 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
968 *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
969 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
971 return 0;