diff mbox series

[v3,2/2] fs: add the ability for statmount() to report the mnt_devname

Message ID 20241107-statmount-v3-2-da5b9744c121@kernel.org (mailing list archive)
State New
Headers show
Series fs: allow statmount to fetch the subtype and devname | expand

Commit Message

Jeff Layton Nov. 7, 2024, 9 p.m. UTC
/proc/self/mountinfo displays the devicename for the mount, but
statmount() doesn't yet have a way to return it. Add a new
STATMOUNT_MNT_DEVNAME flag, claim the 32-bit __spare1 field to hold the
offset into the str[] array. STATMOUNT_MNT_DEVNAME will only be set in
the return mask if there is a device string.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/namespace.c             | 36 +++++++++++++++++++++++++++++++++++-
 include/uapi/linux/mount.h |  3 ++-
 2 files changed, 37 insertions(+), 2 deletions(-)

Comments

Jan Kara Nov. 8, 2024, 12:19 p.m. UTC | #1
On Thu 07-11-24 16:00:07, Jeff Layton wrote:
> /proc/self/mountinfo displays the devicename for the mount, but
> statmount() doesn't yet have a way to return it. Add a new
> STATMOUNT_MNT_DEVNAME flag, claim the 32-bit __spare1 field to hold the
> offset into the str[] array. STATMOUNT_MNT_DEVNAME will only be set in
> the return mask if there is a device string.
> 
> Signed-off-by: Jeff Layton <jlayton@kernel.org>

Looks good to me. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/namespace.c             | 36 +++++++++++++++++++++++++++++++++++-
>  include/uapi/linux/mount.h |  3 ++-
>  2 files changed, 37 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/namespace.c b/fs/namespace.c
> index fc4f81891d544305caf863904c0a6e16562fab49..56750fcc890271e22b3b722dc0b4af445686bb86 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
> @@ -5014,6 +5014,32 @@ static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq)
>  		seq_puts(seq, sb->s_subtype);
>  }
>  
> +static int statmount_mnt_devname(struct kstatmount *s, struct seq_file *seq)
> +{
> +	struct super_block *sb = s->mnt->mnt_sb;
> +	struct mount *r = real_mount(s->mnt);
> +
> +	if (sb->s_op->show_devname) {
> +		size_t start = seq->count;
> +		int ret;
> +
> +		ret = sb->s_op->show_devname(seq, s->mnt->mnt_root);
> +		if (ret)
> +			return ret;
> +
> +		if (unlikely(seq_has_overflowed(seq)))
> +			return -EAGAIN;
> +
> +		/* Unescape the result */
> +		seq->buf[seq->count] = '\0';
> +		seq->count = start;
> +		seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
> +	} else if (r->mnt_devname) {
> +		seq_puts(seq, r->mnt_devname);
> +	}
> +	return 0;
> +}
> +
>  static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
>  {
>  	s->sm.mask |= STATMOUNT_MNT_NS_ID;
> @@ -5077,6 +5103,10 @@ static int statmount_string(struct kstatmount *s, u64 flag)
>  		sm->fs_subtype = start;
>  		statmount_fs_subtype(s, seq);
>  		break;
> +	case STATMOUNT_MNT_DEVNAME:
> +		sm->mnt_devname = seq->count;
> +		ret = statmount_mnt_devname(s, seq);
> +		break;
>  	default:
>  		WARN_ON_ONCE(true);
>  		return -EINVAL;
> @@ -5225,6 +5255,9 @@ static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id,
>  	if (!err && s->mask & STATMOUNT_FS_SUBTYPE)
>  		err = statmount_string(s, STATMOUNT_FS_SUBTYPE);
>  
> +	if (!err && s->mask & STATMOUNT_MNT_DEVNAME)
> +		err = statmount_string(s, STATMOUNT_MNT_DEVNAME);
> +
>  	if (!err && s->mask & STATMOUNT_MNT_NS_ID)
>  		statmount_mnt_ns_id(s, ns);
>  
> @@ -5246,7 +5279,8 @@ static inline bool retry_statmount(const long ret, size_t *seq_size)
>  }
>  
>  #define STATMOUNT_STRING_REQ (STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | \
> -			      STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | STATMOUNT_FS_SUBTYPE)
> +			      STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | \
> +			      STATMOUNT_FS_SUBTYPE | STATMOUNT_MNT_DEVNAME)
>  
>  static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
>  			      struct statmount __user *buf, size_t bufsize,
> diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
> index 2e939dddf9cbabe574dafdb6cff9ad4cf9298a74..3de1b0231b639fb8ed739d65b5b5406021f74196 100644
> --- a/include/uapi/linux/mount.h
> +++ b/include/uapi/linux/mount.h
> @@ -174,7 +174,7 @@ struct statmount {
>  	__u32 mnt_point;	/* [str] Mountpoint relative to current root */
>  	__u64 mnt_ns_id;	/* ID of the mount namespace */
>  	__u32 fs_subtype;	/* [str] Subtype of fs_type (if any) */
> -	__u32 __spare1[1];
> +	__u32 mnt_devname;	/* [str] Device string for the mount */
>  	__u64 __spare2[48];
>  	char str[];		/* Variable size part containing strings */
>  };
> @@ -210,6 +210,7 @@ struct mnt_id_req {
>  #define STATMOUNT_MNT_NS_ID		0x00000040U	/* Want/got mnt_ns_id */
>  #define STATMOUNT_MNT_OPTS		0x00000080U	/* Want/got mnt_opts */
>  #define STATMOUNT_FS_SUBTYPE		0x00000100U	/* Want/got fs_subtype */
> +#define STATMOUNT_MNT_DEVNAME		0x00000200U	/* Want/got mnt_devname */
>  
>  /*
>   * Special @mnt_id values that can be passed to listmount
> 
> -- 
> 2.47.0
>
Miklos Szeredi Nov. 11, 2024, 2:31 p.m. UTC | #2
On Thu, 7 Nov 2024 at 22:00, Jeff Layton <jlayton@kernel.org> wrote:

> diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
> index 2e939dddf9cbabe574dafdb6cff9ad4cf9298a74..3de1b0231b639fb8ed739d65b5b5406021f74196 100644
> --- a/include/uapi/linux/mount.h
> +++ b/include/uapi/linux/mount.h
> @@ -174,7 +174,7 @@ struct statmount {
>         __u32 mnt_point;        /* [str] Mountpoint relative to current root */
>         __u64 mnt_ns_id;        /* ID of the mount namespace */
>         __u32 fs_subtype;       /* [str] Subtype of fs_type (if any) */
> -       __u32 __spare1[1];
> +       __u32 mnt_devname;      /* [str] Device string for the mount */

One more point:  this is called source in both the old mount(2) API
and in new the fsconfig(2) API, where it's handled just like a plain
option (i.e. "-osource=/dev/foo").

Also this is a sb property, not a mount property, so the naming is confusing.

So I'd call this "sb_source" for consistency.

Thanks,
Miklos
diff mbox series

Patch

diff --git a/fs/namespace.c b/fs/namespace.c
index fc4f81891d544305caf863904c0a6e16562fab49..56750fcc890271e22b3b722dc0b4af445686bb86 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -5014,6 +5014,32 @@  static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq)
 		seq_puts(seq, sb->s_subtype);
 }
 
+static int statmount_mnt_devname(struct kstatmount *s, struct seq_file *seq)
+{
+	struct super_block *sb = s->mnt->mnt_sb;
+	struct mount *r = real_mount(s->mnt);
+
+	if (sb->s_op->show_devname) {
+		size_t start = seq->count;
+		int ret;
+
+		ret = sb->s_op->show_devname(seq, s->mnt->mnt_root);
+		if (ret)
+			return ret;
+
+		if (unlikely(seq_has_overflowed(seq)))
+			return -EAGAIN;
+
+		/* Unescape the result */
+		seq->buf[seq->count] = '\0';
+		seq->count = start;
+		seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
+	} else if (r->mnt_devname) {
+		seq_puts(seq, r->mnt_devname);
+	}
+	return 0;
+}
+
 static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
 {
 	s->sm.mask |= STATMOUNT_MNT_NS_ID;
@@ -5077,6 +5103,10 @@  static int statmount_string(struct kstatmount *s, u64 flag)
 		sm->fs_subtype = start;
 		statmount_fs_subtype(s, seq);
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		break;
 	default:
 		WARN_ON_ONCE(true);
 		return -EINVAL;
@@ -5225,6 +5255,9 @@  static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id,
 	if (!err && s->mask & STATMOUNT_FS_SUBTYPE)
 		err = statmount_string(s, STATMOUNT_FS_SUBTYPE);
 
+	if (!err && s->mask & STATMOUNT_MNT_DEVNAME)
+		err = statmount_string(s, STATMOUNT_MNT_DEVNAME);
+
 	if (!err && s->mask & STATMOUNT_MNT_NS_ID)
 		statmount_mnt_ns_id(s, ns);
 
@@ -5246,7 +5279,8 @@  static inline bool retry_statmount(const long ret, size_t *seq_size)
 }
 
 #define STATMOUNT_STRING_REQ (STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | \
-			      STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | STATMOUNT_FS_SUBTYPE)
+			      STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | \
+			      STATMOUNT_FS_SUBTYPE | STATMOUNT_MNT_DEVNAME)
 
 static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
 			      struct statmount __user *buf, size_t bufsize,
diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
index 2e939dddf9cbabe574dafdb6cff9ad4cf9298a74..3de1b0231b639fb8ed739d65b5b5406021f74196 100644
--- a/include/uapi/linux/mount.h
+++ b/include/uapi/linux/mount.h
@@ -174,7 +174,7 @@  struct statmount {
 	__u32 mnt_point;	/* [str] Mountpoint relative to current root */
 	__u64 mnt_ns_id;	/* ID of the mount namespace */
 	__u32 fs_subtype;	/* [str] Subtype of fs_type (if any) */
-	__u32 __spare1[1];
+	__u32 mnt_devname;	/* [str] Device string for the mount */
 	__u64 __spare2[48];
 	char str[];		/* Variable size part containing strings */
 };
@@ -210,6 +210,7 @@  struct mnt_id_req {
 #define STATMOUNT_MNT_NS_ID		0x00000040U	/* Want/got mnt_ns_id */
 #define STATMOUNT_MNT_OPTS		0x00000080U	/* Want/got mnt_opts */
 #define STATMOUNT_FS_SUBTYPE		0x00000100U	/* Want/got fs_subtype */
+#define STATMOUNT_MNT_DEVNAME		0x00000200U	/* Want/got mnt_devname */
 
 /*
  * Special @mnt_id values that can be passed to listmount