fix O_SYNC|O_APPEND syncing the wrong range on write()
[linux/fpc-iii.git] / fs / xfs / xfs_rtbitmap.c
blobb1f2fe8af4a8d27db5ca6848711e841073ef092f
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 ASSERT(!xfs_buf_geterror(bp));
78 *bpp = bp;
79 return 0;
83 * Searching backward from start to limit, find the first block whose
84 * allocated/free state is different from start's.
86 int
87 xfs_rtfind_back(
88 xfs_mount_t *mp, /* file system mount point */
89 xfs_trans_t *tp, /* transaction pointer */
90 xfs_rtblock_t start, /* starting block to look at */
91 xfs_rtblock_t limit, /* last block to look at */
92 xfs_rtblock_t *rtblock) /* out: start block found */
94 xfs_rtword_t *b; /* current word in buffer */
95 int bit; /* bit number in the word */
96 xfs_rtblock_t block; /* bitmap block number */
97 xfs_buf_t *bp; /* buf for the block */
98 xfs_rtword_t *bufp; /* starting word in buffer */
99 int error; /* error value */
100 xfs_rtblock_t firstbit; /* first useful bit in the word */
101 xfs_rtblock_t i; /* current bit number rel. to start */
102 xfs_rtblock_t len; /* length of inspected area */
103 xfs_rtword_t mask; /* mask of relevant bits for value */
104 xfs_rtword_t want; /* mask for "good" values */
105 xfs_rtword_t wdiff; /* difference from wanted value */
106 int word; /* word number in the buffer */
109 * Compute and read in starting bitmap block for starting block.
111 block = XFS_BITTOBLOCK(mp, start);
112 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
113 if (error) {
114 return error;
116 bufp = bp->b_addr;
118 * Get the first word's index & point to it.
120 word = XFS_BITTOWORD(mp, start);
121 b = &bufp[word];
122 bit = (int)(start & (XFS_NBWORD - 1));
123 len = start - limit + 1;
125 * Compute match value, based on the bit at start: if 1 (free)
126 * then all-ones, else all-zeroes.
128 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
130 * If the starting position is not word-aligned, deal with the
131 * partial word.
133 if (bit < XFS_NBWORD - 1) {
135 * Calculate first (leftmost) bit number to look at,
136 * and mask for all the relevant bits in this word.
138 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
139 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
140 firstbit;
142 * Calculate the difference between the value there
143 * and what we're looking for.
145 if ((wdiff = (*b ^ want) & mask)) {
147 * Different. Mark where we are and return.
149 xfs_trans_brelse(tp, bp);
150 i = bit - XFS_RTHIBIT(wdiff);
151 *rtblock = start - i + 1;
152 return 0;
154 i = bit - firstbit + 1;
156 * Go on to previous block if that's where the previous word is
157 * and we need the previous word.
159 if (--word == -1 && i < len) {
161 * If done with this block, get the previous one.
163 xfs_trans_brelse(tp, bp);
164 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
165 if (error) {
166 return error;
168 bufp = bp->b_addr;
169 word = XFS_BLOCKWMASK(mp);
170 b = &bufp[word];
171 } else {
173 * Go on to the previous word in the buffer.
175 b--;
177 } else {
179 * Starting on a word boundary, no partial word.
181 i = 0;
184 * Loop over whole words in buffers. When we use up one buffer
185 * we move on to the previous one.
187 while (len - i >= XFS_NBWORD) {
189 * Compute difference between actual and desired value.
191 if ((wdiff = *b ^ want)) {
193 * Different, mark where we are and return.
195 xfs_trans_brelse(tp, bp);
196 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
197 *rtblock = start - i + 1;
198 return 0;
200 i += XFS_NBWORD;
202 * Go on to previous block if that's where the previous word is
203 * and we need the previous word.
205 if (--word == -1 && i < len) {
207 * If done with this block, get the previous one.
209 xfs_trans_brelse(tp, bp);
210 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
211 if (error) {
212 return error;
214 bufp = bp->b_addr;
215 word = XFS_BLOCKWMASK(mp);
216 b = &bufp[word];
217 } else {
219 * Go on to the previous word in the buffer.
221 b--;
225 * If not ending on a word boundary, deal with the last
226 * (partial) word.
228 if (len - i) {
230 * Calculate first (leftmost) bit number to look at,
231 * and mask for all the relevant bits in this word.
233 firstbit = XFS_NBWORD - (len - i);
234 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
236 * Compute difference between actual and desired value.
238 if ((wdiff = (*b ^ want) & mask)) {
240 * Different, mark where we are and return.
242 xfs_trans_brelse(tp, bp);
243 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
244 *rtblock = start - i + 1;
245 return 0;
246 } else
247 i = len;
250 * No match, return that we scanned the whole area.
252 xfs_trans_brelse(tp, bp);
253 *rtblock = start - i + 1;
254 return 0;
258 * Searching forward from start to limit, find the first block whose
259 * allocated/free state is different from start's.
262 xfs_rtfind_forw(
263 xfs_mount_t *mp, /* file system mount point */
264 xfs_trans_t *tp, /* transaction pointer */
265 xfs_rtblock_t start, /* starting block to look at */
266 xfs_rtblock_t limit, /* last block to look at */
267 xfs_rtblock_t *rtblock) /* out: start block found */
269 xfs_rtword_t *b; /* current word in buffer */
270 int bit; /* bit number in the word */
271 xfs_rtblock_t block; /* bitmap block number */
272 xfs_buf_t *bp; /* buf for the block */
273 xfs_rtword_t *bufp; /* starting word in buffer */
274 int error; /* error value */
275 xfs_rtblock_t i; /* current bit number rel. to start */
276 xfs_rtblock_t lastbit; /* last useful bit in the word */
277 xfs_rtblock_t len; /* length of inspected area */
278 xfs_rtword_t mask; /* mask of relevant bits for value */
279 xfs_rtword_t want; /* mask for "good" values */
280 xfs_rtword_t wdiff; /* difference from wanted value */
281 int word; /* word number in the buffer */
284 * Compute and read in starting bitmap block for starting block.
286 block = XFS_BITTOBLOCK(mp, start);
287 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
288 if (error) {
289 return error;
291 bufp = bp->b_addr;
293 * Get the first word's index & point to it.
295 word = XFS_BITTOWORD(mp, start);
296 b = &bufp[word];
297 bit = (int)(start & (XFS_NBWORD - 1));
298 len = limit - start + 1;
300 * Compute match value, based on the bit at start: if 1 (free)
301 * then all-ones, else all-zeroes.
303 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
305 * If the starting position is not word-aligned, deal with the
306 * partial word.
308 if (bit) {
310 * Calculate last (rightmost) bit number to look at,
311 * and mask for all the relevant bits in this word.
313 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
314 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
316 * Calculate the difference between the value there
317 * and what we're looking for.
319 if ((wdiff = (*b ^ want) & mask)) {
321 * Different. Mark where we are and return.
323 xfs_trans_brelse(tp, bp);
324 i = XFS_RTLOBIT(wdiff) - bit;
325 *rtblock = start + i - 1;
326 return 0;
328 i = lastbit - bit;
330 * Go on to next block if that's where the next word is
331 * and we need the next word.
333 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
335 * If done with this block, get the previous one.
337 xfs_trans_brelse(tp, bp);
338 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
339 if (error) {
340 return error;
342 b = bufp = bp->b_addr;
343 word = 0;
344 } else {
346 * Go on to the previous word in the buffer.
348 b++;
350 } else {
352 * Starting on a word boundary, no partial word.
354 i = 0;
357 * Loop over whole words in buffers. When we use up one buffer
358 * we move on to the next one.
360 while (len - i >= XFS_NBWORD) {
362 * Compute difference between actual and desired value.
364 if ((wdiff = *b ^ want)) {
366 * Different, mark where we are and return.
368 xfs_trans_brelse(tp, bp);
369 i += XFS_RTLOBIT(wdiff);
370 *rtblock = start + i - 1;
371 return 0;
373 i += XFS_NBWORD;
375 * Go on to next block if that's where the next word is
376 * and we need the next word.
378 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
380 * If done with this block, get the next one.
382 xfs_trans_brelse(tp, bp);
383 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
384 if (error) {
385 return error;
387 b = bufp = bp->b_addr;
388 word = 0;
389 } else {
391 * Go on to the next word in the buffer.
393 b++;
397 * If not ending on a word boundary, deal with the last
398 * (partial) word.
400 if ((lastbit = len - i)) {
402 * Calculate mask for all the relevant bits in this word.
404 mask = ((xfs_rtword_t)1 << lastbit) - 1;
406 * Compute difference between actual and desired value.
408 if ((wdiff = (*b ^ want) & mask)) {
410 * Different, mark where we are and return.
412 xfs_trans_brelse(tp, bp);
413 i += XFS_RTLOBIT(wdiff);
414 *rtblock = start + i - 1;
415 return 0;
416 } else
417 i = len;
420 * No match, return that we scanned the whole area.
422 xfs_trans_brelse(tp, bp);
423 *rtblock = start + i - 1;
424 return 0;
428 * Read and modify the summary information for a given extent size,
429 * bitmap block combination.
430 * Keeps track of a current summary block, so we don't keep reading
431 * it from the buffer cache.
434 xfs_rtmodify_summary(
435 xfs_mount_t *mp, /* file system mount point */
436 xfs_trans_t *tp, /* transaction pointer */
437 int log, /* log2 of extent size */
438 xfs_rtblock_t bbno, /* bitmap block number */
439 int delta, /* change to make to summary info */
440 xfs_buf_t **rbpp, /* in/out: summary block buffer */
441 xfs_fsblock_t *rsb) /* in/out: summary block number */
443 xfs_buf_t *bp; /* buffer for the summary block */
444 int error; /* error value */
445 xfs_fsblock_t sb; /* summary fsblock */
446 int so; /* index into the summary file */
447 xfs_suminfo_t *sp; /* pointer to returned data */
450 * Compute entry number in the summary file.
452 so = XFS_SUMOFFS(mp, log, bbno);
454 * Compute the block number in the summary file.
456 sb = XFS_SUMOFFSTOBLOCK(mp, so);
458 * If we have an old buffer, and the block number matches, use that.
460 if (rbpp && *rbpp && *rsb == sb)
461 bp = *rbpp;
463 * Otherwise we have to get the buffer.
465 else {
467 * If there was an old one, get rid of it first.
469 if (rbpp && *rbpp)
470 xfs_trans_brelse(tp, *rbpp);
471 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
472 if (error) {
473 return error;
476 * Remember this buffer and block for the next call.
478 if (rbpp) {
479 *rbpp = bp;
480 *rsb = sb;
484 * Point to the summary information, modify and log it.
486 sp = XFS_SUMPTR(mp, bp, so);
487 *sp += delta;
488 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
489 (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
490 return 0;
494 * Set the given range of bitmap bits to the given value.
495 * Do whatever I/O and logging is required.
498 xfs_rtmodify_range(
499 xfs_mount_t *mp, /* file system mount point */
500 xfs_trans_t *tp, /* transaction pointer */
501 xfs_rtblock_t start, /* starting block to modify */
502 xfs_extlen_t len, /* length of extent to modify */
503 int val) /* 1 for free, 0 for allocated */
505 xfs_rtword_t *b; /* current word in buffer */
506 int bit; /* bit number in the word */
507 xfs_rtblock_t block; /* bitmap block number */
508 xfs_buf_t *bp; /* buf for the block */
509 xfs_rtword_t *bufp; /* starting word in buffer */
510 int error; /* error value */
511 xfs_rtword_t *first; /* first used word in the buffer */
512 int i; /* current bit number rel. to start */
513 int lastbit; /* last useful bit in word */
514 xfs_rtword_t mask; /* mask o frelevant bits for value */
515 int word; /* word number in the buffer */
518 * Compute starting bitmap block number.
520 block = XFS_BITTOBLOCK(mp, start);
522 * Read the bitmap block, and point to its data.
524 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
525 if (error) {
526 return error;
528 bufp = bp->b_addr;
530 * Compute the starting word's address, and starting bit.
532 word = XFS_BITTOWORD(mp, start);
533 first = b = &bufp[word];
534 bit = (int)(start & (XFS_NBWORD - 1));
536 * 0 (allocated) => all zeroes; 1 (free) => all ones.
538 val = -val;
540 * If not starting on a word boundary, deal with the first
541 * (partial) word.
543 if (bit) {
545 * Compute first bit not changed and mask of relevant bits.
547 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
548 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
550 * Set/clear the active bits.
552 if (val)
553 *b |= mask;
554 else
555 *b &= ~mask;
556 i = lastbit - bit;
558 * Go on to the next block if that's where the next word is
559 * and we need the next word.
561 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
563 * Log the changed part of this block.
564 * Get the next one.
566 xfs_trans_log_buf(tp, bp,
567 (uint)((char *)first - (char *)bufp),
568 (uint)((char *)b - (char *)bufp));
569 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
570 if (error) {
571 return error;
573 first = b = bufp = bp->b_addr;
574 word = 0;
575 } else {
577 * Go on to the next word in the buffer
579 b++;
581 } else {
583 * Starting on a word boundary, no partial word.
585 i = 0;
588 * Loop over whole words in buffers. When we use up one buffer
589 * we move on to the next one.
591 while (len - i >= XFS_NBWORD) {
593 * Set the word value correctly.
595 *b = val;
596 i += XFS_NBWORD;
598 * Go on to the next block if that's where the next word is
599 * and we need the next word.
601 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
603 * Log the changed part of this block.
604 * Get the next one.
606 xfs_trans_log_buf(tp, bp,
607 (uint)((char *)first - (char *)bufp),
608 (uint)((char *)b - (char *)bufp));
609 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
610 if (error) {
611 return error;
613 first = b = bufp = bp->b_addr;
614 word = 0;
615 } else {
617 * Go on to the next word in the buffer
619 b++;
623 * If not ending on a word boundary, deal with the last
624 * (partial) word.
626 if ((lastbit = len - i)) {
628 * Compute a mask of relevant bits.
630 bit = 0;
631 mask = ((xfs_rtword_t)1 << lastbit) - 1;
633 * Set/clear the active bits.
635 if (val)
636 *b |= mask;
637 else
638 *b &= ~mask;
639 b++;
642 * Log any remaining changed bytes.
644 if (b > first)
645 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
646 (uint)((char *)b - (char *)bufp - 1));
647 return 0;
651 * Mark an extent specified by start and len freed.
652 * Updates all the summary information as well as the bitmap.
655 xfs_rtfree_range(
656 xfs_mount_t *mp, /* file system mount point */
657 xfs_trans_t *tp, /* transaction pointer */
658 xfs_rtblock_t start, /* starting block to free */
659 xfs_extlen_t len, /* length to free */
660 xfs_buf_t **rbpp, /* in/out: summary block buffer */
661 xfs_fsblock_t *rsb) /* in/out: summary block number */
663 xfs_rtblock_t end; /* end of the freed extent */
664 int error; /* error value */
665 xfs_rtblock_t postblock; /* first block freed > end */
666 xfs_rtblock_t preblock; /* first block freed < start */
668 end = start + len - 1;
670 * Modify the bitmap to mark this extent freed.
672 error = xfs_rtmodify_range(mp, tp, start, len, 1);
673 if (error) {
674 return error;
677 * Assume we're freeing out of the middle of an allocated extent.
678 * We need to find the beginning and end of the extent so we can
679 * properly update the summary.
681 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
682 if (error) {
683 return error;
686 * Find the next allocated block (end of allocated extent).
688 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
689 &postblock);
690 if (error)
691 return error;
693 * If there are blocks not being freed at the front of the
694 * old extent, add summary data for them to be allocated.
696 if (preblock < start) {
697 error = xfs_rtmodify_summary(mp, tp,
698 XFS_RTBLOCKLOG(start - preblock),
699 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
700 if (error) {
701 return error;
705 * If there are blocks not being freed at the end of the
706 * old extent, add summary data for them to be allocated.
708 if (postblock > end) {
709 error = xfs_rtmodify_summary(mp, tp,
710 XFS_RTBLOCKLOG(postblock - end),
711 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
712 if (error) {
713 return error;
717 * Increment the summary information corresponding to the entire
718 * (new) free extent.
720 error = xfs_rtmodify_summary(mp, tp,
721 XFS_RTBLOCKLOG(postblock + 1 - preblock),
722 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
723 return error;
727 * Check that the given range is either all allocated (val = 0) or
728 * all free (val = 1).
731 xfs_rtcheck_range(
732 xfs_mount_t *mp, /* file system mount point */
733 xfs_trans_t *tp, /* transaction pointer */
734 xfs_rtblock_t start, /* starting block number of extent */
735 xfs_extlen_t len, /* length of extent */
736 int val, /* 1 for free, 0 for allocated */
737 xfs_rtblock_t *new, /* out: first block not matching */
738 int *stat) /* out: 1 for matches, 0 for not */
740 xfs_rtword_t *b; /* current word in buffer */
741 int bit; /* bit number in the word */
742 xfs_rtblock_t block; /* bitmap block number */
743 xfs_buf_t *bp; /* buf for the block */
744 xfs_rtword_t *bufp; /* starting word in buffer */
745 int error; /* error value */
746 xfs_rtblock_t i; /* current bit number rel. to start */
747 xfs_rtblock_t lastbit; /* last useful bit in word */
748 xfs_rtword_t mask; /* mask of relevant bits for value */
749 xfs_rtword_t wdiff; /* difference from wanted value */
750 int word; /* word number in the buffer */
753 * Compute starting bitmap block number
755 block = XFS_BITTOBLOCK(mp, start);
757 * Read the bitmap block.
759 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
760 if (error) {
761 return error;
763 bufp = bp->b_addr;
765 * Compute the starting word's address, and starting bit.
767 word = XFS_BITTOWORD(mp, start);
768 b = &bufp[word];
769 bit = (int)(start & (XFS_NBWORD - 1));
771 * 0 (allocated) => all zero's; 1 (free) => all one's.
773 val = -val;
775 * If not starting on a word boundary, deal with the first
776 * (partial) word.
778 if (bit) {
780 * Compute first bit not examined.
782 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
784 * Mask of relevant bits.
786 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
788 * Compute difference between actual and desired value.
790 if ((wdiff = (*b ^ val) & mask)) {
792 * Different, compute first wrong bit and return.
794 xfs_trans_brelse(tp, bp);
795 i = XFS_RTLOBIT(wdiff) - bit;
796 *new = start + i;
797 *stat = 0;
798 return 0;
800 i = lastbit - bit;
802 * Go on to next block if that's where the next word is
803 * and we need the next word.
805 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
807 * If done with this block, get the next one.
809 xfs_trans_brelse(tp, bp);
810 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
811 if (error) {
812 return error;
814 b = bufp = bp->b_addr;
815 word = 0;
816 } else {
818 * Go on to the next word in the buffer.
820 b++;
822 } else {
824 * Starting on a word boundary, no partial word.
826 i = 0;
829 * Loop over whole words in buffers. When we use up one buffer
830 * we move on to the next one.
832 while (len - i >= XFS_NBWORD) {
834 * Compute difference between actual and desired value.
836 if ((wdiff = *b ^ val)) {
838 * Different, compute first wrong bit and return.
840 xfs_trans_brelse(tp, bp);
841 i += XFS_RTLOBIT(wdiff);
842 *new = start + i;
843 *stat = 0;
844 return 0;
846 i += XFS_NBWORD;
848 * Go on to next block if that's where the next word is
849 * and we need the next word.
851 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
853 * If done with this block, get the next one.
855 xfs_trans_brelse(tp, bp);
856 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
857 if (error) {
858 return error;
860 b = bufp = bp->b_addr;
861 word = 0;
862 } else {
864 * Go on to the next word in the buffer.
866 b++;
870 * If not ending on a word boundary, deal with the last
871 * (partial) word.
873 if ((lastbit = len - i)) {
875 * Mask of relevant bits.
877 mask = ((xfs_rtword_t)1 << lastbit) - 1;
879 * Compute difference between actual and desired value.
881 if ((wdiff = (*b ^ val) & mask)) {
883 * Different, compute first wrong bit and return.
885 xfs_trans_brelse(tp, bp);
886 i += XFS_RTLOBIT(wdiff);
887 *new = start + i;
888 *stat = 0;
889 return 0;
890 } else
891 i = len;
894 * Successful, return.
896 xfs_trans_brelse(tp, bp);
897 *new = start + i;
898 *stat = 1;
899 return 0;
902 #ifdef DEBUG
904 * Check that the given extent (block range) is allocated already.
906 STATIC int /* error */
907 xfs_rtcheck_alloc_range(
908 xfs_mount_t *mp, /* file system mount point */
909 xfs_trans_t *tp, /* transaction pointer */
910 xfs_rtblock_t bno, /* starting block number of extent */
911 xfs_extlen_t len) /* length of extent */
913 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
914 int stat;
915 int error;
917 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
918 if (error)
919 return error;
920 ASSERT(stat);
921 return 0;
923 #else
924 #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
925 #endif
927 * Free an extent in the realtime subvolume. Length is expressed in
928 * realtime extents, as is the block number.
930 int /* error */
931 xfs_rtfree_extent(
932 xfs_trans_t *tp, /* transaction pointer */
933 xfs_rtblock_t bno, /* starting block number to free */
934 xfs_extlen_t len) /* length of extent freed */
936 int error; /* error value */
937 xfs_mount_t *mp; /* file system mount structure */
938 xfs_fsblock_t sb; /* summary file block number */
939 xfs_buf_t *sumbp = NULL; /* summary file block buffer */
941 mp = tp->t_mountp;
943 ASSERT(mp->m_rbmip->i_itemp != NULL);
944 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
946 error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
947 if (error)
948 return error;
951 * Free the range of realtime blocks.
953 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
954 if (error) {
955 return error;
958 * Mark more blocks free in the superblock.
960 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
962 * If we've now freed all the blocks, reset the file sequence
963 * number to 0.
965 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
966 mp->m_sb.sb_rextents) {
967 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
968 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
969 *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
970 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
972 return 0;