perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_rtbitmap.c
blobb228c821bae6802c0aa8ab9b79069d703245bbe2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_bmap.h"
16 #include "xfs_bmap_util.h"
17 #include "xfs_bmap_btree.h"
18 #include "xfs_alloc.h"
19 #include "xfs_error.h"
20 #include "xfs_trans.h"
21 #include "xfs_trans_space.h"
22 #include "xfs_trace.h"
23 #include "xfs_buf.h"
24 #include "xfs_icache.h"
25 #include "xfs_rtalloc.h"
29 * Realtime allocator bitmap functions shared with userspace.
33 * Real time buffers need verifiers to avoid runtime warnings during IO.
34 * We don't have anything to verify, however, so these are just dummy
35 * operations.
37 static void
38 xfs_rtbuf_verify_read(
39 struct xfs_buf *bp)
41 return;
44 static void
45 xfs_rtbuf_verify_write(
46 struct xfs_buf *bp)
48 return;
51 const struct xfs_buf_ops xfs_rtbuf_ops = {
52 .name = "rtbuf",
53 .verify_read = xfs_rtbuf_verify_read,
54 .verify_write = xfs_rtbuf_verify_write,
58 * Get a buffer for the bitmap or summary file block specified.
59 * The buffer is returned read and locked.
61 int
62 xfs_rtbuf_get(
63 xfs_mount_t *mp, /* file system mount structure */
64 xfs_trans_t *tp, /* transaction pointer */
65 xfs_rtblock_t block, /* block number in bitmap or summary */
66 int issum, /* is summary not bitmap */
67 xfs_buf_t **bpp) /* output: buffer for the block */
69 xfs_buf_t *bp; /* block buffer, result */
70 xfs_inode_t *ip; /* bitmap or summary inode */
71 xfs_bmbt_irec_t map;
72 int nmap = 1;
73 int error; /* error value */
75 ip = issum ? mp->m_rsumip : mp->m_rbmip;
77 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
78 if (error)
79 return error;
81 if (nmap == 0 || !xfs_bmap_is_real_extent(&map))
82 return -EFSCORRUPTED;
84 ASSERT(map.br_startblock != NULLFSBLOCK);
85 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
86 XFS_FSB_TO_DADDR(mp, map.br_startblock),
87 mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
88 if (error)
89 return error;
91 xfs_trans_buf_set_type(tp, bp, issum ? XFS_BLFT_RTSUMMARY_BUF
92 : XFS_BLFT_RTBITMAP_BUF);
93 *bpp = bp;
94 return 0;
98 * Searching backward from start to limit, find the first block whose
99 * allocated/free state is different from start's.
102 xfs_rtfind_back(
103 xfs_mount_t *mp, /* file system mount point */
104 xfs_trans_t *tp, /* transaction pointer */
105 xfs_rtblock_t start, /* starting block to look at */
106 xfs_rtblock_t limit, /* last block to look at */
107 xfs_rtblock_t *rtblock) /* out: start block found */
109 xfs_rtword_t *b; /* current word in buffer */
110 int bit; /* bit number in the word */
111 xfs_rtblock_t block; /* bitmap block number */
112 xfs_buf_t *bp; /* buf for the block */
113 xfs_rtword_t *bufp; /* starting word in buffer */
114 int error; /* error value */
115 xfs_rtblock_t firstbit; /* first useful bit in the word */
116 xfs_rtblock_t i; /* current bit number rel. to start */
117 xfs_rtblock_t len; /* length of inspected area */
118 xfs_rtword_t mask; /* mask of relevant bits for value */
119 xfs_rtword_t want; /* mask for "good" values */
120 xfs_rtword_t wdiff; /* difference from wanted value */
121 int word; /* word number in the buffer */
124 * Compute and read in starting bitmap block for starting block.
126 block = XFS_BITTOBLOCK(mp, start);
127 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
128 if (error) {
129 return error;
131 bufp = bp->b_addr;
133 * Get the first word's index & point to it.
135 word = XFS_BITTOWORD(mp, start);
136 b = &bufp[word];
137 bit = (int)(start & (XFS_NBWORD - 1));
138 len = start - limit + 1;
140 * Compute match value, based on the bit at start: if 1 (free)
141 * then all-ones, else all-zeroes.
143 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
145 * If the starting position is not word-aligned, deal with the
146 * partial word.
148 if (bit < XFS_NBWORD - 1) {
150 * Calculate first (leftmost) bit number to look at,
151 * and mask for all the relevant bits in this word.
153 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
154 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
155 firstbit;
157 * Calculate the difference between the value there
158 * and what we're looking for.
160 if ((wdiff = (*b ^ want) & mask)) {
162 * Different. Mark where we are and return.
164 xfs_trans_brelse(tp, bp);
165 i = bit - XFS_RTHIBIT(wdiff);
166 *rtblock = start - i + 1;
167 return 0;
169 i = bit - firstbit + 1;
171 * Go on to previous block if that's where the previous word is
172 * and we need the previous word.
174 if (--word == -1 && i < len) {
176 * If done with this block, get the previous one.
178 xfs_trans_brelse(tp, bp);
179 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
180 if (error) {
181 return error;
183 bufp = bp->b_addr;
184 word = XFS_BLOCKWMASK(mp);
185 b = &bufp[word];
186 } else {
188 * Go on to the previous word in the buffer.
190 b--;
192 } else {
194 * Starting on a word boundary, no partial word.
196 i = 0;
199 * Loop over whole words in buffers. When we use up one buffer
200 * we move on to the previous one.
202 while (len - i >= XFS_NBWORD) {
204 * Compute difference between actual and desired value.
206 if ((wdiff = *b ^ want)) {
208 * Different, mark where we are and return.
210 xfs_trans_brelse(tp, bp);
211 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
212 *rtblock = start - i + 1;
213 return 0;
215 i += XFS_NBWORD;
217 * Go on to previous block if that's where the previous word is
218 * and we need the previous word.
220 if (--word == -1 && i < len) {
222 * If done with this block, get the previous one.
224 xfs_trans_brelse(tp, bp);
225 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
226 if (error) {
227 return error;
229 bufp = bp->b_addr;
230 word = XFS_BLOCKWMASK(mp);
231 b = &bufp[word];
232 } else {
234 * Go on to the previous word in the buffer.
236 b--;
240 * If not ending on a word boundary, deal with the last
241 * (partial) word.
243 if (len - i) {
245 * Calculate first (leftmost) bit number to look at,
246 * and mask for all the relevant bits in this word.
248 firstbit = XFS_NBWORD - (len - i);
249 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
251 * Compute difference between actual and desired value.
253 if ((wdiff = (*b ^ want) & mask)) {
255 * Different, mark where we are and return.
257 xfs_trans_brelse(tp, bp);
258 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
259 *rtblock = start - i + 1;
260 return 0;
261 } else
262 i = len;
265 * No match, return that we scanned the whole area.
267 xfs_trans_brelse(tp, bp);
268 *rtblock = start - i + 1;
269 return 0;
273 * Searching forward from start to limit, find the first block whose
274 * allocated/free state is different from start's.
277 xfs_rtfind_forw(
278 xfs_mount_t *mp, /* file system mount point */
279 xfs_trans_t *tp, /* transaction pointer */
280 xfs_rtblock_t start, /* starting block to look at */
281 xfs_rtblock_t limit, /* last block to look at */
282 xfs_rtblock_t *rtblock) /* out: start block found */
284 xfs_rtword_t *b; /* current word in buffer */
285 int bit; /* bit number in the word */
286 xfs_rtblock_t block; /* bitmap block number */
287 xfs_buf_t *bp; /* buf for the block */
288 xfs_rtword_t *bufp; /* starting word in buffer */
289 int error; /* error value */
290 xfs_rtblock_t i; /* current bit number rel. to start */
291 xfs_rtblock_t lastbit; /* last useful bit in the word */
292 xfs_rtblock_t len; /* length of inspected area */
293 xfs_rtword_t mask; /* mask of relevant bits for value */
294 xfs_rtword_t want; /* mask for "good" values */
295 xfs_rtword_t wdiff; /* difference from wanted value */
296 int word; /* word number in the buffer */
299 * Compute and read in starting bitmap block for starting block.
301 block = XFS_BITTOBLOCK(mp, start);
302 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
303 if (error) {
304 return error;
306 bufp = bp->b_addr;
308 * Get the first word's index & point to it.
310 word = XFS_BITTOWORD(mp, start);
311 b = &bufp[word];
312 bit = (int)(start & (XFS_NBWORD - 1));
313 len = limit - start + 1;
315 * Compute match value, based on the bit at start: if 1 (free)
316 * then all-ones, else all-zeroes.
318 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
320 * If the starting position is not word-aligned, deal with the
321 * partial word.
323 if (bit) {
325 * Calculate last (rightmost) bit number to look at,
326 * and mask for all the relevant bits in this word.
328 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
329 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
331 * Calculate the difference between the value there
332 * and what we're looking for.
334 if ((wdiff = (*b ^ want) & mask)) {
336 * Different. Mark where we are and return.
338 xfs_trans_brelse(tp, bp);
339 i = XFS_RTLOBIT(wdiff) - bit;
340 *rtblock = start + i - 1;
341 return 0;
343 i = lastbit - bit;
345 * Go on to next block if that's where the next word is
346 * and we need the next word.
348 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
350 * If done with this block, get the previous one.
352 xfs_trans_brelse(tp, bp);
353 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
354 if (error) {
355 return error;
357 b = bufp = bp->b_addr;
358 word = 0;
359 } else {
361 * Go on to the previous word in the buffer.
363 b++;
365 } else {
367 * Starting on a word boundary, no partial word.
369 i = 0;
372 * Loop over whole words in buffers. When we use up one buffer
373 * we move on to the next one.
375 while (len - i >= XFS_NBWORD) {
377 * Compute difference between actual and desired value.
379 if ((wdiff = *b ^ want)) {
381 * Different, mark where we are and return.
383 xfs_trans_brelse(tp, bp);
384 i += XFS_RTLOBIT(wdiff);
385 *rtblock = start + i - 1;
386 return 0;
388 i += XFS_NBWORD;
390 * Go on to next block if that's where the next word is
391 * and we need the next word.
393 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
395 * If done with this block, get the next one.
397 xfs_trans_brelse(tp, bp);
398 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
399 if (error) {
400 return error;
402 b = bufp = bp->b_addr;
403 word = 0;
404 } else {
406 * Go on to the next word in the buffer.
408 b++;
412 * If not ending on a word boundary, deal with the last
413 * (partial) word.
415 if ((lastbit = len - i)) {
417 * Calculate mask for all the relevant bits in this word.
419 mask = ((xfs_rtword_t)1 << lastbit) - 1;
421 * Compute difference between actual and desired value.
423 if ((wdiff = (*b ^ want) & mask)) {
425 * Different, mark where we are and return.
427 xfs_trans_brelse(tp, bp);
428 i += XFS_RTLOBIT(wdiff);
429 *rtblock = start + i - 1;
430 return 0;
431 } else
432 i = len;
435 * No match, return that we scanned the whole area.
437 xfs_trans_brelse(tp, bp);
438 *rtblock = start + i - 1;
439 return 0;
443 * Read and/or modify the summary information for a given extent size,
444 * bitmap block combination.
445 * Keeps track of a current summary block, so we don't keep reading
446 * it from the buffer cache.
448 * Summary information is returned in *sum if specified.
449 * If no delta is specified, returns summary only.
452 xfs_rtmodify_summary_int(
453 xfs_mount_t *mp, /* file system mount structure */
454 xfs_trans_t *tp, /* transaction pointer */
455 int log, /* log2 of extent size */
456 xfs_rtblock_t bbno, /* bitmap block number */
457 int delta, /* change to make to summary info */
458 xfs_buf_t **rbpp, /* in/out: summary block buffer */
459 xfs_fsblock_t *rsb, /* in/out: summary block number */
460 xfs_suminfo_t *sum) /* out: summary info for this block */
462 xfs_buf_t *bp; /* buffer for the summary block */
463 int error; /* error value */
464 xfs_fsblock_t sb; /* summary fsblock */
465 int so; /* index into the summary file */
466 xfs_suminfo_t *sp; /* pointer to returned data */
469 * Compute entry number in the summary file.
471 so = XFS_SUMOFFS(mp, log, bbno);
473 * Compute the block number in the summary file.
475 sb = XFS_SUMOFFSTOBLOCK(mp, so);
477 * If we have an old buffer, and the block number matches, use that.
479 if (*rbpp && *rsb == sb)
480 bp = *rbpp;
482 * Otherwise we have to get the buffer.
484 else {
486 * If there was an old one, get rid of it first.
488 if (*rbpp)
489 xfs_trans_brelse(tp, *rbpp);
490 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
491 if (error) {
492 return error;
495 * Remember this buffer and block for the next call.
497 *rbpp = bp;
498 *rsb = sb;
501 * Point to the summary information, modify/log it, and/or copy it out.
503 sp = XFS_SUMPTR(mp, bp, so);
504 if (delta) {
505 uint first = (uint)((char *)sp - (char *)bp->b_addr);
507 *sp += delta;
508 xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
510 if (sum)
511 *sum = *sp;
512 return 0;
516 xfs_rtmodify_summary(
517 xfs_mount_t *mp, /* file system mount structure */
518 xfs_trans_t *tp, /* transaction pointer */
519 int log, /* log2 of extent size */
520 xfs_rtblock_t bbno, /* bitmap block number */
521 int delta, /* change to make to summary info */
522 xfs_buf_t **rbpp, /* in/out: summary block buffer */
523 xfs_fsblock_t *rsb) /* in/out: summary block number */
525 return xfs_rtmodify_summary_int(mp, tp, log, bbno,
526 delta, rbpp, rsb, NULL);
530 * Set the given range of bitmap bits to the given value.
531 * Do whatever I/O and logging is required.
534 xfs_rtmodify_range(
535 xfs_mount_t *mp, /* file system mount point */
536 xfs_trans_t *tp, /* transaction pointer */
537 xfs_rtblock_t start, /* starting block to modify */
538 xfs_extlen_t len, /* length of extent to modify */
539 int val) /* 1 for free, 0 for allocated */
541 xfs_rtword_t *b; /* current word in buffer */
542 int bit; /* bit number in the word */
543 xfs_rtblock_t block; /* bitmap block number */
544 xfs_buf_t *bp; /* buf for the block */
545 xfs_rtword_t *bufp; /* starting word in buffer */
546 int error; /* error value */
547 xfs_rtword_t *first; /* first used word in the buffer */
548 int i; /* current bit number rel. to start */
549 int lastbit; /* last useful bit in word */
550 xfs_rtword_t mask; /* mask o frelevant bits for value */
551 int word; /* word number in the buffer */
554 * Compute starting bitmap block number.
556 block = XFS_BITTOBLOCK(mp, start);
558 * Read the bitmap block, and point to its data.
560 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
561 if (error) {
562 return error;
564 bufp = bp->b_addr;
566 * Compute the starting word's address, and starting bit.
568 word = XFS_BITTOWORD(mp, start);
569 first = b = &bufp[word];
570 bit = (int)(start & (XFS_NBWORD - 1));
572 * 0 (allocated) => all zeroes; 1 (free) => all ones.
574 val = -val;
576 * If not starting on a word boundary, deal with the first
577 * (partial) word.
579 if (bit) {
581 * Compute first bit not changed and mask of relevant bits.
583 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
584 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
586 * Set/clear the active bits.
588 if (val)
589 *b |= mask;
590 else
591 *b &= ~mask;
592 i = lastbit - bit;
594 * Go on to the next block if that's where the next word is
595 * and we need the next word.
597 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
599 * Log the changed part of this block.
600 * Get the next one.
602 xfs_trans_log_buf(tp, bp,
603 (uint)((char *)first - (char *)bufp),
604 (uint)((char *)b - (char *)bufp));
605 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
606 if (error) {
607 return error;
609 first = b = bufp = bp->b_addr;
610 word = 0;
611 } else {
613 * Go on to the next word in the buffer
615 b++;
617 } else {
619 * Starting on a word boundary, no partial word.
621 i = 0;
624 * Loop over whole words in buffers. When we use up one buffer
625 * we move on to the next one.
627 while (len - i >= XFS_NBWORD) {
629 * Set the word value correctly.
631 *b = val;
632 i += XFS_NBWORD;
634 * Go on to the next block if that's where the next word is
635 * and we need the next word.
637 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
639 * Log the changed part of this block.
640 * Get the next one.
642 xfs_trans_log_buf(tp, bp,
643 (uint)((char *)first - (char *)bufp),
644 (uint)((char *)b - (char *)bufp));
645 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
646 if (error) {
647 return error;
649 first = b = bufp = bp->b_addr;
650 word = 0;
651 } else {
653 * Go on to the next word in the buffer
655 b++;
659 * If not ending on a word boundary, deal with the last
660 * (partial) word.
662 if ((lastbit = len - i)) {
664 * Compute a mask of relevant bits.
666 mask = ((xfs_rtword_t)1 << lastbit) - 1;
668 * Set/clear the active bits.
670 if (val)
671 *b |= mask;
672 else
673 *b &= ~mask;
674 b++;
677 * Log any remaining changed bytes.
679 if (b > first)
680 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
681 (uint)((char *)b - (char *)bufp - 1));
682 return 0;
686 * Mark an extent specified by start and len freed.
687 * Updates all the summary information as well as the bitmap.
690 xfs_rtfree_range(
691 xfs_mount_t *mp, /* file system mount point */
692 xfs_trans_t *tp, /* transaction pointer */
693 xfs_rtblock_t start, /* starting block to free */
694 xfs_extlen_t len, /* length to free */
695 xfs_buf_t **rbpp, /* in/out: summary block buffer */
696 xfs_fsblock_t *rsb) /* in/out: summary block number */
698 xfs_rtblock_t end; /* end of the freed extent */
699 int error; /* error value */
700 xfs_rtblock_t postblock; /* first block freed > end */
701 xfs_rtblock_t preblock; /* first block freed < start */
703 end = start + len - 1;
705 * Modify the bitmap to mark this extent freed.
707 error = xfs_rtmodify_range(mp, tp, start, len, 1);
708 if (error) {
709 return error;
712 * Assume we're freeing out of the middle of an allocated extent.
713 * We need to find the beginning and end of the extent so we can
714 * properly update the summary.
716 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
717 if (error) {
718 return error;
721 * Find the next allocated block (end of allocated extent).
723 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
724 &postblock);
725 if (error)
726 return error;
728 * If there are blocks not being freed at the front of the
729 * old extent, add summary data for them to be allocated.
731 if (preblock < start) {
732 error = xfs_rtmodify_summary(mp, tp,
733 XFS_RTBLOCKLOG(start - preblock),
734 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
735 if (error) {
736 return error;
740 * If there are blocks not being freed at the end of the
741 * old extent, add summary data for them to be allocated.
743 if (postblock > end) {
744 error = xfs_rtmodify_summary(mp, tp,
745 XFS_RTBLOCKLOG(postblock - end),
746 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
747 if (error) {
748 return error;
752 * Increment the summary information corresponding to the entire
753 * (new) free extent.
755 error = xfs_rtmodify_summary(mp, tp,
756 XFS_RTBLOCKLOG(postblock + 1 - preblock),
757 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
758 return error;
762 * Check that the given range is either all allocated (val = 0) or
763 * all free (val = 1).
766 xfs_rtcheck_range(
767 xfs_mount_t *mp, /* file system mount point */
768 xfs_trans_t *tp, /* transaction pointer */
769 xfs_rtblock_t start, /* starting block number of extent */
770 xfs_extlen_t len, /* length of extent */
771 int val, /* 1 for free, 0 for allocated */
772 xfs_rtblock_t *new, /* out: first block not matching */
773 int *stat) /* out: 1 for matches, 0 for not */
775 xfs_rtword_t *b; /* current word in buffer */
776 int bit; /* bit number in the word */
777 xfs_rtblock_t block; /* bitmap block number */
778 xfs_buf_t *bp; /* buf for the block */
779 xfs_rtword_t *bufp; /* starting word in buffer */
780 int error; /* error value */
781 xfs_rtblock_t i; /* current bit number rel. to start */
782 xfs_rtblock_t lastbit; /* last useful bit in word */
783 xfs_rtword_t mask; /* mask of relevant bits for value */
784 xfs_rtword_t wdiff; /* difference from wanted value */
785 int word; /* word number in the buffer */
788 * Compute starting bitmap block number
790 block = XFS_BITTOBLOCK(mp, start);
792 * Read the bitmap block.
794 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
795 if (error) {
796 return error;
798 bufp = bp->b_addr;
800 * Compute the starting word's address, and starting bit.
802 word = XFS_BITTOWORD(mp, start);
803 b = &bufp[word];
804 bit = (int)(start & (XFS_NBWORD - 1));
806 * 0 (allocated) => all zero's; 1 (free) => all one's.
808 val = -val;
810 * If not starting on a word boundary, deal with the first
811 * (partial) word.
813 if (bit) {
815 * Compute first bit not examined.
817 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
819 * Mask of relevant bits.
821 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
823 * Compute difference between actual and desired value.
825 if ((wdiff = (*b ^ val) & mask)) {
827 * Different, compute first wrong bit and return.
829 xfs_trans_brelse(tp, bp);
830 i = XFS_RTLOBIT(wdiff) - bit;
831 *new = start + i;
832 *stat = 0;
833 return 0;
835 i = lastbit - bit;
837 * Go on to next block if that's where the next word is
838 * and we need the next word.
840 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
842 * If done with this block, get the next one.
844 xfs_trans_brelse(tp, bp);
845 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
846 if (error) {
847 return error;
849 b = bufp = bp->b_addr;
850 word = 0;
851 } else {
853 * Go on to the next word in the buffer.
855 b++;
857 } else {
859 * Starting on a word boundary, no partial word.
861 i = 0;
864 * Loop over whole words in buffers. When we use up one buffer
865 * we move on to the next one.
867 while (len - i >= XFS_NBWORD) {
869 * Compute difference between actual and desired value.
871 if ((wdiff = *b ^ val)) {
873 * Different, compute first wrong bit and return.
875 xfs_trans_brelse(tp, bp);
876 i += XFS_RTLOBIT(wdiff);
877 *new = start + i;
878 *stat = 0;
879 return 0;
881 i += XFS_NBWORD;
883 * Go on to next block if that's where the next word is
884 * and we need the next word.
886 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
888 * If done with this block, get the next one.
890 xfs_trans_brelse(tp, bp);
891 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
892 if (error) {
893 return error;
895 b = bufp = bp->b_addr;
896 word = 0;
897 } else {
899 * Go on to the next word in the buffer.
901 b++;
905 * If not ending on a word boundary, deal with the last
906 * (partial) word.
908 if ((lastbit = len - i)) {
910 * Mask of relevant bits.
912 mask = ((xfs_rtword_t)1 << lastbit) - 1;
914 * Compute difference between actual and desired value.
916 if ((wdiff = (*b ^ val) & mask)) {
918 * Different, compute first wrong bit and return.
920 xfs_trans_brelse(tp, bp);
921 i += XFS_RTLOBIT(wdiff);
922 *new = start + i;
923 *stat = 0;
924 return 0;
925 } else
926 i = len;
929 * Successful, return.
931 xfs_trans_brelse(tp, bp);
932 *new = start + i;
933 *stat = 1;
934 return 0;
937 #ifdef DEBUG
939 * Check that the given extent (block range) is allocated already.
941 STATIC int /* error */
942 xfs_rtcheck_alloc_range(
943 xfs_mount_t *mp, /* file system mount point */
944 xfs_trans_t *tp, /* transaction pointer */
945 xfs_rtblock_t bno, /* starting block number of extent */
946 xfs_extlen_t len) /* length of extent */
948 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
949 int stat;
950 int error;
952 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
953 if (error)
954 return error;
955 ASSERT(stat);
956 return 0;
958 #else
959 #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
960 #endif
962 * Free an extent in the realtime subvolume. Length is expressed in
963 * realtime extents, as is the block number.
965 int /* error */
966 xfs_rtfree_extent(
967 xfs_trans_t *tp, /* transaction pointer */
968 xfs_rtblock_t bno, /* starting block number to free */
969 xfs_extlen_t len) /* length of extent freed */
971 int error; /* error value */
972 xfs_mount_t *mp; /* file system mount structure */
973 xfs_fsblock_t sb; /* summary file block number */
974 xfs_buf_t *sumbp = NULL; /* summary file block buffer */
976 mp = tp->t_mountp;
978 ASSERT(mp->m_rbmip->i_itemp != NULL);
979 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
981 error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
982 if (error)
983 return error;
986 * Free the range of realtime blocks.
988 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
989 if (error) {
990 return error;
993 * Mark more blocks free in the superblock.
995 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
997 * If we've now freed all the blocks, reset the file sequence
998 * number to 0.
1000 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
1001 mp->m_sb.sb_rextents) {
1002 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
1003 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
1004 *(uint64_t *)&VFS_I(mp->m_rbmip)->i_atime = 0;
1005 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1007 return 0;
1010 /* Find all the free records within a given range. */
1012 xfs_rtalloc_query_range(
1013 struct xfs_trans *tp,
1014 struct xfs_rtalloc_rec *low_rec,
1015 struct xfs_rtalloc_rec *high_rec,
1016 xfs_rtalloc_query_range_fn fn,
1017 void *priv)
1019 struct xfs_rtalloc_rec rec;
1020 struct xfs_mount *mp = tp->t_mountp;
1021 xfs_rtblock_t rtstart;
1022 xfs_rtblock_t rtend;
1023 xfs_rtblock_t rem;
1024 int is_free;
1025 int error = 0;
1027 if (low_rec->ar_startext > high_rec->ar_startext)
1028 return -EINVAL;
1029 if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
1030 low_rec->ar_startext == high_rec->ar_startext)
1031 return 0;
1032 if (high_rec->ar_startext > mp->m_sb.sb_rextents)
1033 high_rec->ar_startext = mp->m_sb.sb_rextents;
1035 /* Iterate the bitmap, looking for discrepancies. */
1036 rtstart = low_rec->ar_startext;
1037 rem = high_rec->ar_startext - rtstart;
1038 while (rem) {
1039 /* Is the first block free? */
1040 error = xfs_rtcheck_range(mp, tp, rtstart, 1, 1, &rtend,
1041 &is_free);
1042 if (error)
1043 break;
1045 /* How long does the extent go for? */
1046 error = xfs_rtfind_forw(mp, tp, rtstart,
1047 high_rec->ar_startext - 1, &rtend);
1048 if (error)
1049 break;
1051 if (is_free) {
1052 rec.ar_startext = rtstart;
1053 rec.ar_extcount = rtend - rtstart + 1;
1055 error = fn(tp, &rec, priv);
1056 if (error)
1057 break;
1060 rem -= rtend - rtstart + 1;
1061 rtstart = rtend + 1;
1064 return error;
1067 /* Find all the free records. */
1069 xfs_rtalloc_query_all(
1070 struct xfs_trans *tp,
1071 xfs_rtalloc_query_range_fn fn,
1072 void *priv)
1074 struct xfs_rtalloc_rec keys[2];
1076 keys[0].ar_startext = 0;
1077 keys[1].ar_startext = tp->t_mountp->m_sb.sb_rextents - 1;
1078 keys[0].ar_extcount = keys[1].ar_extcount = 0;
1080 return xfs_rtalloc_query_range(tp, &keys[0], &keys[1], fn, priv);
1083 /* Is the given extent all free? */
1085 xfs_rtalloc_extent_is_free(
1086 struct xfs_mount *mp,
1087 struct xfs_trans *tp,
1088 xfs_rtblock_t start,
1089 xfs_extlen_t len,
1090 bool *is_free)
1092 xfs_rtblock_t end;
1093 int matches;
1094 int error;
1096 error = xfs_rtcheck_range(mp, tp, start, len, 1, &end, &matches);
1097 if (error)
1098 return error;
1100 *is_free = matches;
1101 return 0;