Commit ae8c5117 authored by Kent Overstreet's avatar Kent Overstreet Committed by Christian Brauner

fs: add FS_IOC_GETFSSYSFSPATH

Add a new ioctl for getting the sysfs name of a filesystem - the path
under /sys/fs.

This is going to let us standardize exporting data from sysfs across
filesystems, e.g. time stats.

The returned path will always be of the form "$FSTYP/$SYSFS_IDENTIFIER",
where the sysfs identifier may be a UUID (for bcachefs) or a device name
(xfs).

Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
Link: https://lore.kernel.org/r/20240207025624.1019754-6-kent.overstreet@linux.devSigned-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent e2f7dd6e
...@@ -776,6 +776,20 @@ static int ioctl_getfsuuid(struct file *file, void __user *argp) ...@@ -776,6 +776,20 @@ static int ioctl_getfsuuid(struct file *file, void __user *argp)
return copy_to_user(argp, &u, sizeof(u)) ? -EFAULT : 0; return copy_to_user(argp, &u, sizeof(u)) ? -EFAULT : 0;
} }
static int ioctl_get_fs_sysfs_path(struct file *file, void __user *argp)
{
struct super_block *sb = file_inode(file)->i_sb;
if (!strlen(sb->s_sysfs_name))
return -ENOIOCTLCMD;
struct fs_sysfs_path u = {};
u.len = scnprintf(u.name, sizeof(u.name), "%s/%s", sb->s_type->name, sb->s_sysfs_name);
return copy_to_user(argp, &u, sizeof(u)) ? -EFAULT : 0;
}
/* /*
* do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
* It's just a simple helper for sys_ioctl and compat_sys_ioctl. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
...@@ -861,6 +875,9 @@ static int do_vfs_ioctl(struct file *filp, unsigned int fd, ...@@ -861,6 +875,9 @@ static int do_vfs_ioctl(struct file *filp, unsigned int fd,
case FS_IOC_GETFSUUID: case FS_IOC_GETFSUUID:
return ioctl_getfsuuid(filp, argp); return ioctl_getfsuuid(filp, argp);
case FS_IOC_GETFSSYSFSPATH:
return ioctl_get_fs_sysfs_path(filp, argp);
default: default:
if (S_ISREG(inode->i_mode)) if (S_ISREG(inode->i_mode))
return file_ioctl(filp, cmd, argp); return file_ioctl(filp, cmd, argp);
......
...@@ -1255,10 +1255,23 @@ struct super_block { ...@@ -1255,10 +1255,23 @@ struct super_block {
struct fsnotify_mark_connector __rcu *s_fsnotify_marks; struct fsnotify_mark_connector __rcu *s_fsnotify_marks;
#endif #endif
/*
* q: why are s_id and s_sysfs_name not the same? both are human
* readable strings that identify the filesystem
* a: s_id is allowed to change at runtime; it's used in log messages,
* and we want to when a device starts out as single device (s_id is dev
* name) but then a device is hot added and we have to switch to
* identifying it by UUID
* but s_sysfs_name is a handle for programmatic access, and can't
* change at runtime
*/
char s_id[32]; /* Informational name */ char s_id[32]; /* Informational name */
uuid_t s_uuid; /* UUID */ uuid_t s_uuid; /* UUID */
u8 s_uuid_len; /* Default 16, possibly smaller for weird filesystems */ u8 s_uuid_len; /* Default 16, possibly smaller for weird filesystems */
/* if set, fs shows up under sysfs at /sys/fs/$FSTYP/s_sysfs_name */
char s_sysfs_name[UUID_STRING_LEN + 1];
unsigned int s_max_links; unsigned int s_max_links;
/* /*
...@@ -2541,6 +2554,36 @@ static inline void super_set_uuid(struct super_block *sb, const u8 *uuid, unsign ...@@ -2541,6 +2554,36 @@ static inline void super_set_uuid(struct super_block *sb, const u8 *uuid, unsign
memcpy(&sb->s_uuid, uuid, len); memcpy(&sb->s_uuid, uuid, len);
} }
/* set sb sysfs name based on sb->s_bdev */
static inline void super_set_sysfs_name_bdev(struct super_block *sb)
{
snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pg", sb->s_bdev);
}
/* set sb sysfs name based on sb->s_uuid */
static inline void super_set_sysfs_name_uuid(struct super_block *sb)
{
WARN_ON(sb->s_uuid_len != sizeof(sb->s_uuid));
snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pU", sb->s_uuid.b);
}
/* set sb sysfs name based on sb->s_id */
static inline void super_set_sysfs_name_id(struct super_block *sb)
{
strscpy(sb->s_sysfs_name, sb->s_id, sizeof(sb->s_sysfs_name));
}
/* try to use something standard before you use this */
__printf(2, 3)
static inline void super_set_sysfs_name_generic(struct super_block *sb, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), fmt, args);
va_end(args);
}
extern int current_umask(void); extern int current_umask(void);
extern void ihold(struct inode * inode); extern void ihold(struct inode * inode);
......
...@@ -77,6 +77,11 @@ struct fsuuid2 { ...@@ -77,6 +77,11 @@ struct fsuuid2 {
__u8 uuid[16]; __u8 uuid[16];
}; };
struct fs_sysfs_path {
__u8 len;
__u8 name[128];
};
/* extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions */ /* extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions */
#define FILE_DEDUPE_RANGE_SAME 0 #define FILE_DEDUPE_RANGE_SAME 0
#define FILE_DEDUPE_RANGE_DIFFERS 1 #define FILE_DEDUPE_RANGE_DIFFERS 1
...@@ -230,6 +235,11 @@ struct fsxattr { ...@@ -230,6 +235,11 @@ struct fsxattr {
#define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX]) #define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX])
/* Returns the external filesystem UUID, the same one blkid returns */ /* Returns the external filesystem UUID, the same one blkid returns */
#define FS_IOC_GETFSUUID _IOR(0x15, 0, struct fsuuid2) #define FS_IOC_GETFSUUID _IOR(0x15, 0, struct fsuuid2)
/*
* Returns the path component under /sys/fs/ that refers to this filesystem;
* also /sys/kernel/debug/ for filesystems with debugfs exports
*/
#define FS_IOC_GETFSSYSFSPATH _IOR(0x15, 1, struct fs_sysfs_path)
/* /*
* Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment