blob: d05bff4504c999e9ed9e52602f864847cf49e2b8 [file] [log] [blame]
Lorenz Brun5999e922021-01-27 18:53:54 +01001From 69b33d464bcadd6c60018f5d0bde8ad3e7530dc7 Mon Sep 17 00:00:00 2001
Lorenz Brun1d801752020-04-02 09:24:51 +02002From: David Howells <dhowells@redhat.com>
3Date: Tue, 3 Mar 2020 14:29:27 +0000
Lorenz Brun5999e922021-01-27 18:53:54 +01004Subject: [PATCH 3/3] fsinfo: Allow retrieval of superblock devname, options
Lorenz Brun1d801752020-04-02 09:24:51 +02005 and stats
6
7Provide fsinfo() attributes to retrieve superblock device name, options,
8and statistics in string form. The following attributes are defined:
9
10 FSINFO_ATTR_SOURCE - Mount-specific device name
11 FSINFO_ATTR_CONFIGURATION - Mount options
12 FSINFO_ATTR_FS_STATISTICS - Filesystem statistics
13
14FSINFO_ATTR_SOURCE could be made indexable by params->Nth to handle the
15case where there is more than one source (e.g. the bcachefs filesystem).
16
17Signed-off-by: David Howells <dhowells@redhat.com>
18---
Lorenz Brun5999e922021-01-27 18:53:54 +010019 fs/fsinfo.c | 39 +++++++++++++++++++++++++++++++++++
Lorenz Brun1d801752020-04-02 09:24:51 +020020 fs/internal.h | 2 ++
Lorenz Brun5999e922021-01-27 18:53:54 +010021 fs/namespace.c | 41 +++++++++++++++++++++++++++++++++++++
Lorenz Brun1d801752020-04-02 09:24:51 +020022 include/uapi/linux/fsinfo.h | 3 +++
23 samples/vfs/test-fsinfo.c | 4 ++++
24 5 files changed, 89 insertions(+)
25
26diff --git a/fs/fsinfo.c b/fs/fsinfo.c
Lorenz Brun5999e922021-01-27 18:53:54 +010027index 7d9c73e9cbdef..7e2d871eb5dfe 100644
Lorenz Brun1d801752020-04-02 09:24:51 +020028--- a/fs/fsinfo.c
29+++ b/fs/fsinfo.c
Lorenz Brun5999e922021-01-27 18:53:54 +010030@@ -198,6 +198,42 @@ static int fsinfo_generic_volume_id(struct path *path, struct fsinfo_context *ct
Lorenz Brun1d801752020-04-02 09:24:51 +020031 return fsinfo_string(path->dentry->d_sb->s_id, ctx);
32 }
33
34+/*
35+ * Retrieve the superblock configuration (mount options) as a comma-separated
Lorenz Brun5999e922021-01-27 18:53:54 +010036+ * string. The initial comma is stripped off and NUL termination is added.
Lorenz Brun1d801752020-04-02 09:24:51 +020037+ */
38+static int fsinfo_generic_seq_read(struct path *path, struct fsinfo_context *ctx)
39+{
40+ struct super_block *sb = path->dentry->d_sb;
41+ struct seq_file m = {
42+ .buf = ctx->buffer,
Lorenz Brun5999e922021-01-27 18:53:54 +010043+ .size = ctx->buf_size - 1,
Lorenz Brun1d801752020-04-02 09:24:51 +020044+ };
45+ int ret = 0;
46+
47+ switch (ctx->requested_attr) {
48+ case FSINFO_ATTR_CONFIGURATION:
Lorenz Brun5999e922021-01-27 18:53:54 +010049+ seq_puts(&m, sb_rdonly(sb) ? "ro" : "rw");
50+ ret = security_sb_show_options(&m, sb);
51+ if (!ret && sb->s_op->show_options)
Lorenz Brun1d801752020-04-02 09:24:51 +020052+ ret = sb->s_op->show_options(&m, path->mnt->mnt_root);
53+ break;
54+
55+ case FSINFO_ATTR_FS_STATISTICS:
56+ if (sb->s_op->show_stats)
57+ ret = sb->s_op->show_stats(&m, path->mnt->mnt_root);
58+ break;
59+ }
60+
61+ if (ret < 0)
62+ return ret;
63+ if (seq_has_overflowed(&m))
64+ return ctx->buf_size + PAGE_SIZE;
Lorenz Brun5999e922021-01-27 18:53:54 +010065+
66+ ((char *)ctx->buffer)[ctx->skip + m.count] = 0;
67+ return m.count + 1;
Lorenz Brun1d801752020-04-02 09:24:51 +020068+}
69+
70 static const struct fsinfo_attribute fsinfo_common_attributes[] = {
71 FSINFO_VSTRUCT (FSINFO_ATTR_STATFS, fsinfo_generic_statfs),
72 FSINFO_VSTRUCT (FSINFO_ATTR_IDS, fsinfo_generic_ids),
Lorenz Brun5999e922021-01-27 18:53:54 +010073@@ -206,6 +242,9 @@ static const struct fsinfo_attribute fsinfo_common_attributes[] = {
Lorenz Brun1d801752020-04-02 09:24:51 +020074 FSINFO_VSTRUCT (FSINFO_ATTR_TIMESTAMP_INFO, fsinfo_generic_timestamp_info),
75 FSINFO_STRING (FSINFO_ATTR_VOLUME_ID, fsinfo_generic_volume_id),
76 FSINFO_VSTRUCT (FSINFO_ATTR_VOLUME_UUID, fsinfo_generic_volume_uuid),
77+ FSINFO_STRING (FSINFO_ATTR_SOURCE, fsinfo_generic_mount_source),
78+ FSINFO_STRING (FSINFO_ATTR_CONFIGURATION, fsinfo_generic_seq_read),
79+ FSINFO_STRING (FSINFO_ATTR_FS_STATISTICS, fsinfo_generic_seq_read),
80
81 FSINFO_LIST (FSINFO_ATTR_FSINFO_ATTRIBUTES, (void *)123UL),
82 FSINFO_VSTRUCT_N(FSINFO_ATTR_FSINFO_ATTRIBUTE_INFO, (void *)123UL),
83diff --git a/fs/internal.h b/fs/internal.h
Lorenz Brun5999e922021-01-27 18:53:54 +010084index 1b7460f055a3b..a2a529da7e673 100644
Lorenz Brun1d801752020-04-02 09:24:51 +020085--- a/fs/internal.h
86+++ b/fs/internal.h
Lorenz Brun5999e922021-01-27 18:53:54 +010087@@ -98,6 +98,8 @@ int path_mount(const char *dev_name, struct path *path,
88 const char *type_page, unsigned long flags, void *data_page);
89 int path_umount(struct path *path, int flags);
Lorenz Brun1d801752020-04-02 09:24:51 +020090
Lorenz Brun1d801752020-04-02 09:24:51 +020091+extern int fsinfo_generic_mount_source(struct path *, struct fsinfo_context *);
92+
93 /*
94 * fs_struct.c
95 */
96diff --git a/fs/namespace.c b/fs/namespace.c
Lorenz Brun5999e922021-01-27 18:53:54 +010097index cebaa3e817940..33218f67ad5b1 100644
Lorenz Brun1d801752020-04-02 09:24:51 +020098--- a/fs/namespace.c
99+++ b/fs/namespace.c
100@@ -30,6 +30,7 @@
101 #include <uapi/linux/mount.h>
102 #include <linux/fs_context.h>
103 #include <linux/shmem_fs.h>
104+#include <linux/fsinfo.h>
105
106 #include "pnode.h"
107 #include "internal.h"
Lorenz Brun5999e922021-01-27 18:53:54 +0100108@@ -4105,3 +4106,43 @@ const struct proc_ns_operations mntns_operations = {
Lorenz Brun1d801752020-04-02 09:24:51 +0200109 .install = mntns_install,
110 .owner = mntns_owner,
111 };
112+
113+#ifdef CONFIG_FSINFO
114+static inline void mangle(struct seq_file *m, const char *s)
115+{
116+ seq_escape(m, s, " \t\n\\");
117+}
118+
119+/*
120+ * Return the mount source/device name as seen from this mountpoint. Shared
121+ * mounts may vary here and the filesystem is permitted to substitute its own
122+ * rendering.
123+ */
124+int fsinfo_generic_mount_source(struct path *path, struct fsinfo_context *ctx)
125+{
126+ struct super_block *sb = path->mnt->mnt_sb;
127+ struct mount *mnt = real_mount(path->mnt);
128+ struct seq_file m = {
129+ .buf = ctx->buffer,
Lorenz Brun5999e922021-01-27 18:53:54 +0100130+ .size = ctx->buf_size - 1,
Lorenz Brun1d801752020-04-02 09:24:51 +0200131+ };
132+ int ret;
133+
134+ if (sb->s_op->show_devname) {
135+ ret = sb->s_op->show_devname(&m, mnt->mnt.mnt_root);
136+ if (ret < 0)
137+ return ret;
138+ } else {
139+ if (!mnt->mnt_devname)
140+ return fsinfo_string("none", ctx);
141+ mangle(&m, mnt->mnt_devname);
142+ }
143+
144+ if (seq_has_overflowed(&m))
145+ return ctx->buf_size + PAGE_SIZE;
Lorenz Brun5999e922021-01-27 18:53:54 +0100146+
147+ ((char *)ctx->buffer)[m.count] = 0;
148+ return m.count + 1;
Lorenz Brun1d801752020-04-02 09:24:51 +0200149+}
150+
151+#endif /* CONFIG_FSINFO */
152diff --git a/include/uapi/linux/fsinfo.h b/include/uapi/linux/fsinfo.h
Lorenz Brun5999e922021-01-27 18:53:54 +0100153index 65892239ba86c..45d9f4b758787 100644
Lorenz Brun1d801752020-04-02 09:24:51 +0200154--- a/include/uapi/linux/fsinfo.h
155+++ b/include/uapi/linux/fsinfo.h
156@@ -23,6 +23,9 @@
157 #define FSINFO_ATTR_VOLUME_ID 0x05 /* Volume ID (string) */
158 #define FSINFO_ATTR_VOLUME_UUID 0x06 /* Volume UUID (LE uuid) */
159 #define FSINFO_ATTR_VOLUME_NAME 0x07 /* Volume name (string) */
160+#define FSINFO_ATTR_SOURCE 0x09 /* Superblock source/device name (string) */
161+#define FSINFO_ATTR_CONFIGURATION 0x0a /* Superblock configuration/options (string) */
162+#define FSINFO_ATTR_FS_STATISTICS 0x0b /* Superblock filesystem statistics (string) */
163
164 #define FSINFO_ATTR_FSINFO_ATTRIBUTE_INFO 0x100 /* Information about attr N (for path) */
165 #define FSINFO_ATTR_FSINFO_ATTRIBUTES 0x101 /* List of supported attrs (for path) */
166diff --git a/samples/vfs/test-fsinfo.c b/samples/vfs/test-fsinfo.c
Lorenz Brun5999e922021-01-27 18:53:54 +0100167index 934b25399ffed..cf849c9778f71 100644
Lorenz Brun1d801752020-04-02 09:24:51 +0200168--- a/samples/vfs/test-fsinfo.c
169+++ b/samples/vfs/test-fsinfo.c
Lorenz Brun5999e922021-01-27 18:53:54 +0100170@@ -294,6 +294,10 @@ static const struct fsinfo_attribute fsinfo_attributes[] = {
Lorenz Brun1d801752020-04-02 09:24:51 +0200171 FSINFO_STRING (FSINFO_ATTR_VOLUME_ID, string),
172 FSINFO_VSTRUCT (FSINFO_ATTR_VOLUME_UUID, fsinfo_generic_volume_uuid),
173 FSINFO_STRING (FSINFO_ATTR_VOLUME_NAME, string),
174+ FSINFO_STRING (FSINFO_ATTR_SOURCE, string),
175+ FSINFO_STRING (FSINFO_ATTR_CONFIGURATION, string),
176+ FSINFO_STRING (FSINFO_ATTR_FS_STATISTICS, string),
177+
178 FSINFO_VSTRUCT_N(FSINFO_ATTR_FSINFO_ATTRIBUTE_INFO, fsinfo_meta_attribute_info),
179 FSINFO_LIST (FSINFO_ATTR_FSINFO_ATTRIBUTES, fsinfo_meta_attributes),
180 {}
181--
Lorenz Brun5999e922021-01-27 18:53:54 +01001822.25.1
Lorenz Brun1d801752020-04-02 09:24:51 +0200183