[PATCH 2/3] btrfs: implement super operation to get fsid

Amir Goldstein
2023-10-25T13:51:07Z

btrfs statfs can report a different fsid for different inodes.

Implement btrfs_get_fsid() that can be used as a cheaper way of getting
the f_fsid memeber of kstatfs, for callers that only care about fsid.

fanotify is going to make use of that to get btrfs fsid from inode on
every event.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
fs/btrfs/super.c | 33 ++++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 1a093ec0f7e3..fd08f7e81f72 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -2016,6 +2016,26 @@ static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
return 0;
}

+static int btrfs_get_fsid(struct inode *inode, __kernel_fsid_t *fsid)
+{
+ struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
+ __be32 *__fsid = (__be32 *)fs_info->fs_devices->fsid;
+
+ /*
+ * We treat it as constant endianness (it doesn't matter _which_)
+ * because we want the fsid to come out the same whether mounted
+ * on a big-endian or little-endian host
+ */
+ fsid->val[0] = be32_to_cpu(__fsid[0]) ^ be32_to_cpu(__fsid[2]);
+ fsid->val[1] = be32_to_cpu(__fsid[1]) ^ be32_to_cpu(__fsid[3]);
+
+ /* Mask in the root object ID too, to disambiguate subvols */
+ fsid->val[0] ^= BTRFS_I(inode)->root->root_key.objectid >> 32;
+ fsid->val[1] ^= BTRFS_I(inode)->root->root_key.objectid;
+
+ return 0;
+}
+
/*
* Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
*
@@ -2038,7 +2058,6 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
u64 total_free_data = 0;
u64 total_free_meta = 0;
u32 bits = fs_info->sectorsize_bits;
- __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
unsigned factor = 1;
struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
int ret;
@@ -2124,16 +2143,7 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
buf->f_bsize = dentry->d_sb->s_blocksize;
buf->f_namelen = BTRFS_NAME_LEN;

- /* We treat it as constant endianness (it doesn't matter _which_)
- because we want the fsid to come out the same whether mounted
- on a big-endian or little-endian host */
- buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
- buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
- /* Mask in the root object ID too, to disambiguate subvols */
- buf->f_fsid.val[0] ^=
- BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
- buf->f_fsid.val[1] ^=
- BTRFS_I(d_inode(dentry))->root->root_key.objectid;
+ btrfs_get_fsid(d_inode(dentry), &buf->f_fsid);

return 0;
}
@@ -2362,6 +2372,7 @@ static const struct super_operations btrfs_super_ops = {
.alloc_inode = btrfs_alloc_inode,
.destroy_inode = btrfs_destroy_inode,
.free_inode = btrfs_free_inode,
+ .get_fsid = btrfs_get_fsid,
.statfs = btrfs_statfs,
.remount_fs = btrfs_remount,
.freeze_fs = btrfs_freeze,
--
2.34.1