Skip to contentSkip to conversation

fs: allow statmount to fetch the sb->s_subtype field

PATCH Superseded Jeff Layton 3 participants 9 messages Nov 6, 2024 – Nov 7, 2024 VFS

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover fs: allow statmount to fetch the sb->s_subtype field

Meta has some internal logging that scrapes /proc/self/mountinfo today. I'd like to convert it to use listmount()/statmount(), so we can do a better job of monitoring with containers. We're missing some fields though. This patchset adds them.

---
Changes in v2:
- make statmount_fs_subtype
- return fast if no subtype is emitted
- new patch to allow statmount() to return devicename
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jeff Layton (2):
fs: add the ability for statmount() to report the fs_subtype
fs: add the ability for statmount() to report the mount devicename

 fs/namespace.c             | 42 +++++++++++++++++++++++++++++++++++++++++-
 include/uapi/linux/mount.h |  6 +++++-
 2 files changed, 46 insertions(+), 2 deletions(-)

---
base-commit: 26213e1a6caa5a7f508b919059b0122b451f4dfe
change-id: 20241106-statmount-3f91a7ed75fa

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw

PATCH v2 1/2 fs: add the ability for statmount() to report the fs_subtype

/proc/self/mountinfo prints out the sb->s_subtype after the type. This is particularly useful for disambiguating FUSE mounts (at least when the userland driver bothers to set it).

Add STATMOUNT_FS_SUBTYPE and claim one of the __spare2 fields to point to the offset into the str[] array. STATMOUNT_FS_SUBTYPE will only be set in the return mask if there is a subtype associated with the mount.

---
 fs/namespace.c             | 19 ++++++++++++++++++-
 include/uapi/linux/mount.h |  5 ++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
fs/namespace.c +18 -1
index ba77ce1c6788dfe461814b5826fcbb3aab68fad4..52ab892088f08ad71647eff533dd6f3025bbae03 100644
@@ -5006,6 +5006,14 @@ static int statmount_fs_type(struct kstatmount *s, struct seq_file *seq)
 	return 0;
 }
 
+static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq)
+{
+	struct super_block *sb = s->mnt->mnt_sb;
+
+	if (sb->s_subtype)
+		seq_puts(seq, sb->s_subtype);
+}
+
 static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
 {
 	s->sm.mask |= STATMOUNT_MNT_NS_ID;
@@ -5064,6 +5072,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		sm->mnt_opts = seq->count;
 		ret = statmount_mnt_opts(s, seq);
 		break;
+	case STATMOUNT_FS_SUBTYPE:
+		sm->fs_subtype = seq->count;
+		statmount_fs_subtype(s, seq);
+		if (seq->count == sm->fs_subtype)
+			return 0;
+		break;
 	default:
 		WARN_ON_ONCE(true);
 		return -EINVAL;
@@ -5203,6 +5217,9 @@ static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id,
 	if (!err && s->mask & STATMOUNT_MNT_OPTS)
 		err = statmount_string(s, STATMOUNT_MNT_OPTS);
 
+	if (!err && s->mask & STATMOUNT_FS_SUBTYPE)
+		err = statmount_string(s, STATMOUNT_FS_SUBTYPE);
+
 	if (!err && s->mask & STATMOUNT_MNT_NS_ID)
 		statmount_mnt_ns_id(s, ns);
 
@@ -5224,7 +5241,7 @@ 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_TYPE | STATMOUNT_MNT_OPTS | STATMOUNT_FS_SUBTYPE)
 
 static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
 			      struct statmount __user *buf, size_t bufsize,
include/uapi/linux/mount.h +4 -1
index 225bc366ffcbf0319929e2f55f1fbea88e4d7b81..fa206fb56b3b25cf80f7d430e1b6bab19c3220e4 100644
@@ -173,7 +173,9 @@ struct statmount {
 	__u32 mnt_root;		/* [str] Root of mount relative to root of fs */
 	__u32 mnt_point;	/* [str] Mountpoint relative to current root */
 	__u64 mnt_ns_id;	/* ID of the mount namespace */
-	__u64 __spare2[49];
+	__u32 fs_subtype;	/* [str] Subtype of fs_type (if any) */
+	__u32 __spare1[1];
+	__u64 __spare2[48];
 	char str[];		/* Variable size part containing strings */
 };
 
@@ -207,6 +209,7 @@ struct mnt_id_req {
 #define STATMOUNT_FS_TYPE		0x00000020U	/* Want/got fs_type */
 #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 subtype */
 
 /*
  * Special @mnt_id values that can be passed to listmount
Signature · 1 line
2.47.0
#2 lore raw

PATCH v2 2/2 fs: add the ability for statmount() to report the mount devicename

In reply to Jeff Layton

/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.

---
 fs/namespace.c             | 25 ++++++++++++++++++++++++-
 include/uapi/linux/mount.h |  3 ++-
 2 files changed, 26 insertions(+), 2 deletions(-)
fs/namespace.c +24 -1
index 52ab892088f08ad71647eff533dd6f3025bbae03..d4ed2cb5de12c86b4da58626441e072fc109b2ff 100644
@@ -5014,6 +5014,19 @@ 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)
+		return sb->s_op->show_devname(seq, s->mnt->mnt_root);
+
+	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;
@@ -5078,6 +5091,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		if (seq->count == sm->fs_subtype)
 			return 0;
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		if (seq->count == sm->mnt_devname)
+			return ret;
+		break;
 	default:
 		WARN_ON_ONCE(true);
 		return -EINVAL;
@@ -5220,6 +5239,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);
 
@@ -5241,7 +5263,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,
include/uapi/linux/mount.h +2 -1
index fa206fb56b3b25cf80f7d430e1b6bab19c3220e4..0f9ea2748f6376b5e71ba969598fc5b641a2c77f 100644
@@ -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 subtype */
+#define STATMOUNT_MNT_DEVNAME		0x00000200U	/* Want/got devname */
 
 /*
  * Special @mnt_id values that can be passed to listmount
Signature · 1 line
2.47.0
reviewer
#3 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

Quoted 7 lines · Jeff Layton

/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.

Just one question below:

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5097

Quoted 8 lines
fs/namespace.c +4 -0
@@ -5078,6 +5091,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		if (seq->count == sm->fs_subtype)
 			return 0;
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		if (seq->count == sm->mnt_devname)

Why this odd check? Why don't you rather do:
if (ret)
?

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5102

Quoted 5 lines
fs/namespace.c +2 -0
+			return ret;
+		break;
 	default:
 		WARN_ON_ONCE(true);
 		return -EINVAL;

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

Quoted 11 lines · Jan Kara
Quoted 7 lines · Jeff Layton

/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.

Just one question below:

Quoted 13 lines

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5097

Quoted 8 lines
fs/namespace.c +4 -0
@@ -5078,6 +5091,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		if (seq->count == sm->fs_subtype)
 			return 0;
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		if (seq->count == sm->mnt_devname)

Why this odd check? Why don't you rather do:
if (ret)
?

statmount_mnt_devname() can return without emitting anything to the seq if ->show_devname and r->mnt_devname are both NULL. In that case, we don't want statmount_string() to return an error, but we also don't want to do any further manipulation of the seq. So, the above handles either the case where show_devname returned an error and the case where there was nothing to emit.

I did consider having statmount_mnt_devname() return -ENOBUFS if there was nothing to emit, and then handle that in the caller, but checking to see whether the seq has advanced seemed like a cleaner solution.

I can add a comment to that effect since it does look a bit confusing.

Quoted 8 lines

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5102

Quoted 5 lines
fs/namespace.c +2 -0
+			return ret;
+		break;
 	default:
 		WARN_ON_ONCE(true);
 		return -EINVAL;

Honza

Signature · 1 line
Jeff Layton <[email protected]>
reviewer
#5 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

Quoted 33 lines · Jeff Layton
Quoted 11 lines · Jan Kara
Quoted 7 lines · Jeff Layton

/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.

Just one question below:

Quoted 13 lines

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5097

Quoted 8 lines
fs/namespace.c +4 -0
@@ -5078,6 +5091,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		if (seq->count == sm->fs_subtype)
 			return 0;
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		if (seq->count == sm->mnt_devname)

Why this odd check? Why don't you rather do:
if (ret)
?

statmount_mnt_devname() can return without emitting anything to the seq if ->show_devname and r->mnt_devname are both NULL. In that case, we don't want statmount_string() to return an error, but we also don't want to do any further manipulation of the seq. So, the above handles either the case where show_devname returned an error and the case where there was nothing to emit.

Quoted 3 lines

I did consider having statmount_mnt_devname() return -ENOBUFS if there was nothing to emit, and then handle that in the caller, but checking to see whether the seq has advanced seemed like a cleaner solution.

OK, but don't we want to emit an empty string - i.e., just \0 character in case r->mnt_devname is NULL and there's no ->show_devname? Because now we literaly emit nothing and it's going to be very confusing for userspace to parse this when this happens in the middle of other strings in the seq. And emitting \0 is exactly what will happen if we don't do anything special in STATMOUNT_MNT_DEVNAME case...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

Quoted 34 lines · Jan Kara
Quoted 33 lines · Jeff Layton
Quoted 11 lines · Jan Kara
Quoted 7 lines · Jeff Layton

/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.

Just one question below:

Quoted 13 lines

fs/namespace.c static int statmount_string(struct kstatmount *s, u64 flag) · line 5097

Quoted 8 lines
fs/namespace.c +4 -0
@@ -5078,6 +5091,12 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		if (seq->count == sm->fs_subtype)
 			return 0;
 		break;
+	case STATMOUNT_MNT_DEVNAME:
+		sm->mnt_devname = seq->count;
+		ret = statmount_mnt_devname(s, seq);
+		if (seq->count == sm->mnt_devname)

Why this odd check? Why don't you rather do:
if (ret)
?

statmount_mnt_devname() can return without emitting anything to the seq if ->show_devname and r->mnt_devname are both NULL. In that case, we don't want statmount_string() to return an error, but we also don't want to do any further manipulation of the seq. So, the above handles either the case where show_devname returned an error and the case where there was nothing to emit.

Quoted 7 lines
Quoted 3 lines

I did consider having statmount_mnt_devname() return -ENOBUFS if there was nothing to emit, and then handle that in the caller, but checking to see whether the seq has advanced seemed like a cleaner solution.

OK, but don't we want to emit an empty string - i.e., just \0 character in case r->mnt_devname is NULL and there's no ->show_devname?

Yes. I think it's better to just not set STATMOUNT_MNT_DEVNAME at all if it's not present than to emit an empty string.

Quoted 6 lines

Because now we
literaly emit nothing and it's going to be very confusing for userspace to parse this when this happens in the middle of other strings in the seq. And emitting \0 is exactly what will happen if we don't do anything special in STATMOUNT_MNT_DEVNAME case...

That's why I'm having statmount_string() return immediately if no string was emitted. That avoids the setting of the flag in the mask and the unneeded NULL termination in the buffer.

The caller will need to check whether STATMOUNT_MNT_DEVNAME is set in the returned mask. If it's not set, then no string was emitted and the sm->mnt_devname index is invalid. In that case, we will have emitted nothing into the buffer, so the other strings shouldn't be affected.

FWIW, here's the comment I added over that if statement:

+ /*

+                * If nothing was emitted, return immediately to avoid
+                * setting the flag and NULL terminating the buffer.
+                */

+ if (seq->count == sm->mnt_devname)
+ return ret;

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

In reply to Jeff Layton
Quoted 18 lines · Jeff Layton

/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.

---
 fs/namespace.c             | 25 ++++++++++++++++++++++++-
 include/uapi/linux/mount.h |  3 ++-
 2 files changed, 26 insertions(+), 2 deletions(-)
fs/namespace.c +0 -0
index 52ab892088f08ad71647eff533dd6f3025bbae03..d4ed2cb5de12c86b4da58626441e072fc109b2ff 100644

fs/namespace.c static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq) · line 5023

Quoted 10 lines
fs/namespace.c +7 -0
@@ -5014,6 +5014,19 @@ 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)
+               return sb->s_op->show_devname(seq, s->mnt->mnt_root);

I think the resulting string should be unescaped just like statmount_mnt_root().

The same goes for the option strings, which went in last cycle.

I see no reason to require users of this interface to implement unescaping themselves. Others beside libmount probably won't do it and will be surprised when encountering escaped strings because they are rare.

Thanks,
Miklos

#8 lore raw

Re: [PATCH v2 2/2] fs: add the ability for statmount() to report the mount devicename

Quoted 32 lines · Miklos Szeredi
Quoted 18 lines · Jeff Layton

/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.

---
 fs/namespace.c             | 25 ++++++++++++++++++++++++-
 include/uapi/linux/mount.h |  3 ++-
 2 files changed, 26 insertions(+), 2 deletions(-)
fs/namespace.c +0 -0
index 52ab892088f08ad71647eff533dd6f3025bbae03..d4ed2cb5de12c86b4da58626441e072fc109b2ff 100644

fs/namespace.c static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq) · line 5023

Quoted 10 lines
fs/namespace.c +7 -0
@@ -5014,6 +5014,19 @@ 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)
+               return sb->s_op->show_devname(seq, s->mnt->mnt_root);

I think the resulting string should be unescaped just like statmount_mnt_root().

Quoted 6 lines

The same goes for the option strings, which went in last cycle.

I see no reason to require users of this interface to implement unescaping themselves. Others beside libmount probably won't do it and will be surprised when encountering escaped strings because they are rare.

Good point. I'll fix that up for the next version.

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>