^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2000-2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xfs_bit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "xfs_rtalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Realtime allocator bitmap functions shared with userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Real time buffers need verifiers to avoid runtime warnings during IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * We don't have anything to verify, however, so these are just dummy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) xfs_rtbuf_verify_read(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct xfs_buf *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) xfs_rtbuf_verify_write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct xfs_buf *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const struct xfs_buf_ops xfs_rtbuf_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .name = "rtbuf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .verify_read = xfs_rtbuf_verify_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .verify_write = xfs_rtbuf_verify_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Get a buffer for the bitmap or summary file block specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The buffer is returned read and locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) xfs_rtbuf_get(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) xfs_mount_t *mp, /* file system mount structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) xfs_rtblock_t block, /* block number in bitmap or summary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int issum, /* is summary not bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) xfs_buf_t **bpp) /* output: buffer for the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) xfs_buf_t *bp; /* block buffer, result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) xfs_inode_t *ip; /* bitmap or summary inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) xfs_bmbt_irec_t map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int nmap = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ip = issum ? mp->m_rsumip : mp->m_rbmip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ASSERT(map.br_startblock != NULLFSBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) XFS_FSB_TO_DADDR(mp, map.br_startblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) xfs_trans_buf_set_type(tp, bp, issum ? XFS_BLFT_RTSUMMARY_BUF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) : XFS_BLFT_RTBITMAP_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *bpp = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Searching backward from start to limit, find the first block whose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * allocated/free state is different from start's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) xfs_rtfind_back(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) xfs_rtblock_t start, /* starting block to look at */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) xfs_rtblock_t limit, /* last block to look at */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) xfs_rtblock_t *rtblock) /* out: start block found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) xfs_rtword_t *b; /* current word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int bit; /* bit number in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) xfs_rtblock_t block; /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) xfs_buf_t *bp; /* buf for the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) xfs_rtword_t *bufp; /* starting word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) xfs_rtblock_t firstbit; /* first useful bit in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) xfs_rtblock_t i; /* current bit number rel. to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) xfs_rtblock_t len; /* length of inspected area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) xfs_rtword_t mask; /* mask of relevant bits for value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) xfs_rtword_t want; /* mask for "good" values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) xfs_rtword_t wdiff; /* difference from wanted value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int word; /* word number in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Compute and read in starting bitmap block for starting block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) block = XFS_BITTOBLOCK(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Get the first word's index & point to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) word = XFS_BITTOWORD(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) bit = (int)(start & (XFS_NBWORD - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) len = start - limit + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Compute match value, based on the bit at start: if 1 (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * then all-ones, else all-zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * If the starting position is not word-aligned, deal with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (bit < XFS_NBWORD - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Calculate first (leftmost) bit number to look at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * and mask for all the relevant bits in this word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) firstbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Calculate the difference between the value there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * and what we're looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if ((wdiff = (*b ^ want) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Different. Mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) i = bit - XFS_RTHIBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *rtblock = start - i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) i = bit - firstbit + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Go on to previous block if that's where the previous word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * and we need the previous word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (--word == -1 && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * If done with this block, get the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) word = XFS_BLOCKWMASK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Go on to the previous word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) b--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * Starting on a word boundary, no partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * Loop over whole words in buffers. When we use up one buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * we move on to the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) while (len - i >= XFS_NBWORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if ((wdiff = *b ^ want)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Different, mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *rtblock = start - i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) i += XFS_NBWORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Go on to previous block if that's where the previous word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * and we need the previous word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (--word == -1 && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * If done with this block, get the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) word = XFS_BLOCKWMASK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Go on to the previous word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) b--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * If not ending on a word boundary, deal with the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (len - i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * Calculate first (leftmost) bit number to look at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * and mask for all the relevant bits in this word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) firstbit = XFS_NBWORD - (len - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if ((wdiff = (*b ^ want) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * Different, mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *rtblock = start - i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) i = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * No match, return that we scanned the whole area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) *rtblock = start - i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * Searching forward from start to limit, find the first block whose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * allocated/free state is different from start's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) xfs_rtfind_forw(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) xfs_rtblock_t start, /* starting block to look at */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) xfs_rtblock_t limit, /* last block to look at */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) xfs_rtblock_t *rtblock) /* out: start block found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) xfs_rtword_t *b; /* current word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int bit; /* bit number in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) xfs_rtblock_t block; /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) xfs_buf_t *bp; /* buf for the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) xfs_rtword_t *bufp; /* starting word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) xfs_rtblock_t i; /* current bit number rel. to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) xfs_rtblock_t lastbit; /* last useful bit in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) xfs_rtblock_t len; /* length of inspected area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) xfs_rtword_t mask; /* mask of relevant bits for value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) xfs_rtword_t want; /* mask for "good" values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) xfs_rtword_t wdiff; /* difference from wanted value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int word; /* word number in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Compute and read in starting bitmap block for starting block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) block = XFS_BITTOBLOCK(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Get the first word's index & point to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) word = XFS_BITTOWORD(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) bit = (int)(start & (XFS_NBWORD - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) len = limit - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Compute match value, based on the bit at start: if 1 (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * then all-ones, else all-zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * If the starting position is not word-aligned, deal with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Calculate last (rightmost) bit number to look at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * and mask for all the relevant bits in this word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Calculate the difference between the value there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * and what we're looking for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if ((wdiff = (*b ^ want) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Different. Mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) i = XFS_RTLOBIT(wdiff) - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) *rtblock = start + i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) i = lastbit - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * Go on to next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * If done with this block, get the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * Go on to the previous word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Starting on a word boundary, no partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Loop over whole words in buffers. When we use up one buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * we move on to the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) while (len - i >= XFS_NBWORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if ((wdiff = *b ^ want)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * Different, mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) i += XFS_RTLOBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) *rtblock = start + i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) i += XFS_NBWORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * Go on to next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * If done with this block, get the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * Go on to the next word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * If not ending on a word boundary, deal with the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if ((lastbit = len - i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * Calculate mask for all the relevant bits in this word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) mask = ((xfs_rtword_t)1 << lastbit) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if ((wdiff = (*b ^ want) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * Different, mark where we are and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) i += XFS_RTLOBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) *rtblock = start + i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) i = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * No match, return that we scanned the whole area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) *rtblock = start + i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * Read and/or modify the summary information for a given extent size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * bitmap block combination.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Keeps track of a current summary block, so we don't keep reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * it from the buffer cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * Summary information is returned in *sum if specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * If no delta is specified, returns summary only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) xfs_rtmodify_summary_int(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) xfs_mount_t *mp, /* file system mount structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int log, /* log2 of extent size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) xfs_rtblock_t bbno, /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int delta, /* change to make to summary info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) xfs_buf_t **rbpp, /* in/out: summary block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) xfs_fsblock_t *rsb, /* in/out: summary block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) xfs_suminfo_t *sum) /* out: summary info for this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) xfs_buf_t *bp; /* buffer for the summary block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) xfs_fsblock_t sb; /* summary fsblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) int so; /* index into the summary file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) xfs_suminfo_t *sp; /* pointer to returned data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Compute entry number in the summary file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) so = XFS_SUMOFFS(mp, log, bbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * Compute the block number in the summary file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sb = XFS_SUMOFFSTOBLOCK(mp, so);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * If we have an old buffer, and the block number matches, use that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (*rbpp && *rsb == sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) bp = *rbpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * Otherwise we have to get the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * If there was an old one, get rid of it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (*rbpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) xfs_trans_brelse(tp, *rbpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * Remember this buffer and block for the next call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) *rbpp = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) *rsb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * Point to the summary information, modify/log it, and/or copy it out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sp = XFS_SUMPTR(mp, bp, so);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) uint first = (uint)((char *)sp - (char *)bp->b_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) *sp += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (mp->m_rsum_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (*sp == 0 && log == mp->m_rsum_cache[bbno])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) mp->m_rsum_cache[bbno]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (*sp != 0 && log < mp->m_rsum_cache[bbno])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) mp->m_rsum_cache[bbno] = log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) *sum = *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) xfs_rtmodify_summary(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) xfs_mount_t *mp, /* file system mount structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int log, /* log2 of extent size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) xfs_rtblock_t bbno, /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int delta, /* change to make to summary info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) xfs_buf_t **rbpp, /* in/out: summary block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) xfs_fsblock_t *rsb) /* in/out: summary block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return xfs_rtmodify_summary_int(mp, tp, log, bbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) delta, rbpp, rsb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * Set the given range of bitmap bits to the given value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * Do whatever I/O and logging is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) xfs_rtmodify_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) xfs_rtblock_t start, /* starting block to modify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) xfs_extlen_t len, /* length of extent to modify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) int val) /* 1 for free, 0 for allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) xfs_rtword_t *b; /* current word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int bit; /* bit number in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) xfs_rtblock_t block; /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) xfs_buf_t *bp; /* buf for the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) xfs_rtword_t *bufp; /* starting word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) xfs_rtword_t *first; /* first used word in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int i; /* current bit number rel. to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int lastbit; /* last useful bit in word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) xfs_rtword_t mask; /* mask o frelevant bits for value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) int word; /* word number in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * Compute starting bitmap block number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) block = XFS_BITTOBLOCK(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * Read the bitmap block, and point to its data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Compute the starting word's address, and starting bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) word = XFS_BITTOWORD(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) first = b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) bit = (int)(start & (XFS_NBWORD - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * 0 (allocated) => all zeroes; 1 (free) => all ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) val = -val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * If not starting on a word boundary, deal with the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * Compute first bit not changed and mask of relevant bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * Set/clear the active bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) *b |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) *b &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) i = lastbit - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * Go on to the next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * Log the changed part of this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * Get the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) xfs_trans_log_buf(tp, bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) (uint)((char *)first - (char *)bufp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) (uint)((char *)b - (char *)bufp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) first = b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * Go on to the next word in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * Starting on a word boundary, no partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * Loop over whole words in buffers. When we use up one buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * we move on to the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) while (len - i >= XFS_NBWORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * Set the word value correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) *b = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) i += XFS_NBWORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * Go on to the next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * Log the changed part of this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * Get the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) xfs_trans_log_buf(tp, bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) (uint)((char *)first - (char *)bufp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) (uint)((char *)b - (char *)bufp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) first = b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * Go on to the next word in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * If not ending on a word boundary, deal with the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if ((lastbit = len - i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * Compute a mask of relevant bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) mask = ((xfs_rtword_t)1 << lastbit) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Set/clear the active bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) *b |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) *b &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * Log any remaining changed bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (b > first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) (uint)((char *)b - (char *)bufp - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * Mark an extent specified by start and len freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * Updates all the summary information as well as the bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) xfs_rtfree_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) xfs_rtblock_t start, /* starting block to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) xfs_extlen_t len, /* length to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) xfs_buf_t **rbpp, /* in/out: summary block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) xfs_fsblock_t *rsb) /* in/out: summary block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) xfs_rtblock_t end; /* end of the freed extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) xfs_rtblock_t postblock; /* first block freed > end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) xfs_rtblock_t preblock; /* first block freed < start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * Modify the bitmap to mark this extent freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) error = xfs_rtmodify_range(mp, tp, start, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * Assume we're freeing out of the middle of an allocated extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * We need to find the beginning and end of the extent so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * properly update the summary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * Find the next allocated block (end of allocated extent).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) &postblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * If there are blocks not being freed at the front of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * old extent, add summary data for them to be allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (preblock < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) error = xfs_rtmodify_summary(mp, tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) XFS_RTBLOCKLOG(start - preblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * If there are blocks not being freed at the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * old extent, add summary data for them to be allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (postblock > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) error = xfs_rtmodify_summary(mp, tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) XFS_RTBLOCKLOG(postblock - end),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * Increment the summary information corresponding to the entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * (new) free extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) error = xfs_rtmodify_summary(mp, tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) XFS_RTBLOCKLOG(postblock + 1 - preblock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * Check that the given range is either all allocated (val = 0) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * all free (val = 1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) xfs_rtcheck_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) xfs_rtblock_t start, /* starting block number of extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) xfs_extlen_t len, /* length of extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) int val, /* 1 for free, 0 for allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) xfs_rtblock_t *new, /* out: first block not matching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) int *stat) /* out: 1 for matches, 0 for not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) xfs_rtword_t *b; /* current word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int bit; /* bit number in the word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) xfs_rtblock_t block; /* bitmap block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) xfs_buf_t *bp; /* buf for the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) xfs_rtword_t *bufp; /* starting word in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) xfs_rtblock_t i; /* current bit number rel. to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) xfs_rtblock_t lastbit; /* last useful bit in word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) xfs_rtword_t mask; /* mask of relevant bits for value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) xfs_rtword_t wdiff; /* difference from wanted value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) int word; /* word number in the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * Compute starting bitmap block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) block = XFS_BITTOBLOCK(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * Read the bitmap block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * Compute the starting word's address, and starting bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) word = XFS_BITTOWORD(mp, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) b = &bufp[word];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) bit = (int)(start & (XFS_NBWORD - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * 0 (allocated) => all zero's; 1 (free) => all one's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) val = -val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * If not starting on a word boundary, deal with the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * Compute first bit not examined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * Mask of relevant bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if ((wdiff = (*b ^ val) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * Different, compute first wrong bit and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) i = XFS_RTLOBIT(wdiff) - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) *new = start + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) *stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) i = lastbit - bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * Go on to next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * If done with this block, get the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * Go on to the next word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * Starting on a word boundary, no partial word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * Loop over whole words in buffers. When we use up one buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * we move on to the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) while (len - i >= XFS_NBWORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if ((wdiff = *b ^ val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * Different, compute first wrong bit and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) i += XFS_RTLOBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) *new = start + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) *stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) i += XFS_NBWORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * Go on to next block if that's where the next word is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * and we need the next word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * If done with this block, get the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) b = bufp = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) word = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * Go on to the next word in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * If not ending on a word boundary, deal with the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * (partial) word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if ((lastbit = len - i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * Mask of relevant bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) mask = ((xfs_rtword_t)1 << lastbit) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * Compute difference between actual and desired value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if ((wdiff = (*b ^ val) & mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * Different, compute first wrong bit and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) i += XFS_RTLOBIT(wdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) *new = start + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) *stat = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) i = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * Successful, return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) *new = start + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) *stat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * Check that the given extent (block range) is allocated already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) STATIC int /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) xfs_rtcheck_alloc_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) xfs_mount_t *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) xfs_rtblock_t bno, /* starting block number of extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) xfs_extlen_t len) /* length of extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) int stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) ASSERT(stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * Free an extent in the realtime subvolume. Length is expressed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * realtime extents, as is the block number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) int /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) xfs_rtfree_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) xfs_trans_t *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) xfs_rtblock_t bno, /* starting block number to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) xfs_extlen_t len) /* length of extent freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) int error; /* error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) xfs_mount_t *mp; /* file system mount structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) xfs_fsblock_t sb; /* summary file block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) xfs_buf_t *sumbp = NULL; /* summary file block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) ASSERT(mp->m_rbmip->i_itemp != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * Free the range of realtime blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * Mark more blocks free in the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * If we've now freed all the blocks, reset the file sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * number to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) mp->m_sb.sb_rextents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) *(uint64_t *)&VFS_I(mp->m_rbmip)->i_atime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) /* Find all the free records within a given range. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) xfs_rtalloc_query_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct xfs_rtalloc_rec *low_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct xfs_rtalloc_rec *high_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) xfs_rtalloc_query_range_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) struct xfs_rtalloc_rec rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) xfs_rtblock_t rtstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) xfs_rtblock_t rtend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) int is_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (low_rec->ar_startext > high_rec->ar_startext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) low_rec->ar_startext == high_rec->ar_startext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) high_rec->ar_startext = min(high_rec->ar_startext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) mp->m_sb.sb_rextents - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) /* Iterate the bitmap, looking for discrepancies. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) rtstart = low_rec->ar_startext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) while (rtstart <= high_rec->ar_startext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) /* Is the first block free? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) error = xfs_rtcheck_range(mp, tp, rtstart, 1, 1, &rtend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) &is_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) /* How long does the extent go for? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) error = xfs_rtfind_forw(mp, tp, rtstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) high_rec->ar_startext, &rtend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (is_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) rec.ar_startext = rtstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) rec.ar_extcount = rtend - rtstart + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) error = fn(tp, &rec, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) rtstart = rtend + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) /* Find all the free records. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) xfs_rtalloc_query_all(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) xfs_rtalloc_query_range_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct xfs_rtalloc_rec keys[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) keys[0].ar_startext = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) keys[1].ar_startext = tp->t_mountp->m_sb.sb_rextents - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) keys[0].ar_extcount = keys[1].ar_extcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return xfs_rtalloc_query_range(tp, &keys[0], &keys[1], fn, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) /* Is the given extent all free? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) xfs_rtalloc_extent_is_free(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) xfs_rtblock_t start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) xfs_extlen_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) bool *is_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) xfs_rtblock_t end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) int matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) error = xfs_rtcheck_range(mp, tp, start, len, 1, &end, &matches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) *is_free = matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) }