2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.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"
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.
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 */
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);
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);
77 ASSERT(!xfs_buf_geterror(bp));
83 * Searching backward from start to limit, find the first block whose
84 * allocated/free state is different from start's.
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);
118 * Get the first word's index & point to it.
120 word = XFS_BITTOWORD(mp, start);
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
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) <<
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;
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);
169 word = XFS_BLOCKWMASK(mp);
173 * Go on to the previous word in the buffer.
179 * Starting on a word boundary, no partial word.
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;
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);
215 word = XFS_BLOCKWMASK(mp);
219 * Go on to the previous word in the buffer.
225 * If not ending on a word boundary, deal with the last
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;
250 * No match, return that we scanned the whole area.
252 xfs_trans_brelse(tp, bp);
253 *rtblock = start - i + 1;
258 * Searching forward from start to limit, find the first block whose
259 * allocated/free state is different from start's.
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);
293 * Get the first word's index & point to it.
295 word = XFS_BITTOWORD(mp, start);
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
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;
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);
342 b = bufp = bp->b_addr;
346 * Go on to the previous word in the buffer.
352 * Starting on a word boundary, no partial word.
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;
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);
387 b = bufp = bp->b_addr;
391 * Go on to the next word in the buffer.
397 * If not ending on a word boundary, deal with the last
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;
420 * No match, return that we scanned the whole area.
422 xfs_trans_brelse(tp, bp);
423 *rtblock = start + i - 1;
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)
463 * Otherwise we have to get the buffer.
467 * If there was an old one, get rid of it first.
470 xfs_trans_brelse(tp, *rbpp);
471 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
476 * Remember this buffer and block for the next call.
484 * Point to the summary information, modify and log it.
486 sp = XFS_SUMPTR(mp, bp, so);
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));
494 * Set the given range of bitmap bits to the given value.
495 * Do whatever I/O and logging is required.
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);
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.
540 * If not starting on a word boundary, deal with the first
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.
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.
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);
573 first = b = bufp = bp->b_addr;
577 * Go on to the next word in the buffer
583 * Starting on a word boundary, no partial word.
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.
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.
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);
613 first = b = bufp = bp->b_addr;
617 * Go on to the next word in the buffer
623 * If not ending on a word boundary, deal with the last
626 if ((lastbit = len - i)) {
628 * Compute a mask of relevant bits.
631 mask = ((xfs_rtword_t)1 << lastbit) - 1;
633 * Set/clear the active bits.
642 * Log any remaining changed bytes.
645 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
646 (uint)((char *)b - (char *)bufp - 1));
651 * Mark an extent specified by start and len freed.
652 * Updates all the summary information as well as the bitmap.
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);
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);
686 * Find the next allocated block (end of allocated extent).
688 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
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);
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);
717 * Increment the summary information corresponding to the entire
720 error = xfs_rtmodify_summary(mp, tp,
721 XFS_RTBLOCKLOG(postblock + 1 - preblock),
722 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
727 * Check that the given range is either all allocated (val = 0) or
728 * all free (val = 1).
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);
765 * Compute the starting word's address, and starting bit.
767 word = XFS_BITTOWORD(mp, start);
769 bit = (int)(start & (XFS_NBWORD - 1));
771 * 0 (allocated) => all zero's; 1 (free) => all one's.
775 * If not starting on a word boundary, deal with the first
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;
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);
814 b = bufp = bp->b_addr;
818 * Go on to the next word in the buffer.
824 * Starting on a word boundary, no partial word.
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);
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);
860 b = bufp = bp->b_addr;
864 * Go on to the next word in the buffer.
870 * If not ending on a word boundary, deal with the last
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);
894 * Successful, return.
896 xfs_trans_brelse(tp, bp);
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 */
917 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
924 #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
927 * Free an extent in the realtime subvolume. Length is expressed in
928 * realtime extents, as is the block number.
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 */
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);
951 * Free the range of realtime blocks.
953 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
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
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);