[PATCH 1/3] fs: define a new super operation to get fsid

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

If implemented, get_fsid() 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.

Suggested-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20230920110429.f4wkfuls73pd55pv@quack3/
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
Documentation/filesystems/locking.rst | 2 ++
Documentation/filesystems/vfs.rst | 4 ++++
fs/statfs.c | 14 ++++++++++++++
include/linux/fs.h | 1 +
include/linux/statfs.h | 2 ++
5 files changed, 23 insertions(+)

diff --git a/Documentation/filesystems/locking.rst b/Documentation/filesystems/locking.rst
index 7be2900806c8..a367950ee755 100644
--- a/Documentation/filesystems/locking.rst
+++ b/Documentation/filesystems/locking.rst
@@ -169,6 +169,7 @@ prototypes::
int (*freeze_fs) (struct super_block *);
int (*unfreeze_fs) (struct super_block *);
int (*statfs) (struct dentry *, struct kstatfs *);
+ int (*get_fsid) (struct inode *, __kernel_fsid_t *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*umount_begin) (struct super_block *);
int (*show_options)(struct seq_file *, struct dentry *);
@@ -193,6 +194,7 @@ sync_fs: read
freeze_fs: write
unfreeze_fs: write
statfs: maybe(read) (see below)
+get_fsid: no
remount_fs: write
umount_begin: no
show_options: no (namespace_sem)
diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst
index 99acc2e98673..f30f39f056ab 100644
--- a/Documentation/filesystems/vfs.rst
+++ b/Documentation/filesystems/vfs.rst
@@ -267,6 +267,7 @@ filesystem. The following members are defined:
enum freeze_wholder who);
int (*unfreeze_fs) (struct super_block *);
int (*statfs) (struct dentry *, struct kstatfs *);
+ int (*get_fsid) (struct inode *, __kernel_fsid_t *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*umount_begin) (struct super_block *);

@@ -374,6 +375,9 @@ or bottom half).
``statfs``
called when the VFS needs to get filesystem statistics.

+``get_fsid``
+ called when the VFS needs to get only the f_fsid member of statfs.
+
``remount_fs``
called when the filesystem is remounted. This is called with
the kernel lock held
diff --git a/fs/statfs.c b/fs/statfs.c
index 96d1c3edf289..60a6af7356b7 100644
--- a/fs/statfs.c
+++ b/fs/statfs.c
@@ -69,11 +69,25 @@ static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
return retval;
}

+int inode_get_fsid(struct inode *inode, __kernel_fsid_t *fsid)
+{
+ if (!inode->i_sb->s_op->get_fsid)
+ return -EOPNOTSUPP;
+
+ return inode->i_sb->s_op->get_fsid(inode, fsid);
+}
+EXPORT_SYMBOL(inode_get_fsid);
+
int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
{
struct kstatfs st;
int error;

+ /* Avoid statfs if fs supports cheaper get_fsid() */
+ error = inode_get_fsid(d_inode(dentry), fsid);
+ if (!error)
+ return 0;
+
error = statfs_by_dentry(dentry, &st);
if (error)
return error;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 4a40823c3c67..30d7d3347c49 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2008,6 +2008,7 @@ struct super_operations {
int (*thaw_super) (struct super_block *, enum freeze_holder who);
int (*unfreeze_fs) (struct super_block *);
int (*statfs) (struct dentry *, struct kstatfs *);
+ int (*get_fsid)(struct inode *, __kernel_fsid_t *);
int (*remount_fs) (struct super_block *, int *, char *);
void (*umount_begin) (struct super_block *);

diff --git a/include/linux/statfs.h b/include/linux/statfs.h
index 02c862686ea3..c07f6d726e39 100644
--- a/include/linux/statfs.h
+++ b/include/linux/statfs.h
@@ -43,7 +43,9 @@ struct kstatfs {
#define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */
#define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */

+struct inode;
struct dentry;
+extern int inode_get_fsid(struct inode *inode, __kernel_fsid_t *fsid);
extern int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid);

static inline __kernel_fsid_t u64_to_fsid(u64 v)
--
2.34.1