History

vfs: Implement a filesystem superblock creation/configuration context

[AV - unfuck kern_mount_data(); we want non-NULL ->mnt_ns on long-living mounts] [AV - reordering fs/namespace.c is badly overdue, but let's keep it separate from that series] [AV - drop simple_pin_fs() change] [AV - clean vfs_kern_mount() failure exits up] Implement a filesystem context concept to be used during superblock creation for mount and superblock reconfiguration for remount. The mounting procedure then becomes: (1) Allocate new fs_context context. (2) Configure the context. (3) Create superblock. (4) Query the superblock. (5) Create a mount for the superblock. (6) Destroy the context. Rather than calling fs_type->mount(), an fs_context struct is created and fs_type->init_fs_context() is called to set it up. Pointers exist for the filesystem and LSM to hang their private data off. A set of operations has to be set by ->init_fs_context() to provide freeing, duplication, option parsing, binary data parsing, validation, mounting and superblock filling. Legacy filesystems are supported by the provision of a set of legacy fs_context operations that build up a list of mount options and then invoke fs_type->mount() from within the fs_context ->get_tree() operation. This allows all filesystems to be accessed using fs_context. It should be noted that, whilst this patch adds a lot of lines of code, there is quite a bit of duplication with existing code that can be eliminated should all filesystems be converted over.

David Howells committed 8 years ago

Commit 3e1aeb01 parent 846e5665 files · +319 −17Browse files

Unified · Split

5 files
  1. fs/filesystems.c +4 −0
  2. fs/fs_context.c +299 −1
  3. fs/namespace.c +9 −16
  4. include/linux/fs.h +2 −0
  5. include/linux/fs_context.h +5 −0
fs/filesystems.c+4−0
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616 #include <linux/module.h>
1717 #include <linux/slab.h>
1818 #include <linux/uaccess.h>
19+#include <linux/fs_parser.h>
1920
2021 /*
2122 * Handling of filesystem drivers list.
@@ -73,6 +74,9 @@ int register_filesystem(struct file_system_type * fs) · 51 unmodified lines
7374 int res = 0;
7475 struct file_system_type ** p;
7576
77+ if (fs->parameters && !fs_validate_description(fs->parameters))
78+ return -EINVAL;
79+
7680 BUG_ON(strchr(fs->name, '.'));
7781 if (fs->next)
7882 return -EBUSY;
fs/fs_context.c+299−1
@@ -12,6 +12,7 @@ · 11 unmodified lines
1212
1313 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1414 #include <linux/fs_context.h>
15+#include <linux/fs_parser.h>
1516 #include <linux/fs.h>
1617 #include <linux/mount.h>
1718 #include <linux/nsproxy.h>
@@ -25,13 +26,217 @@ · 7 unmodified lines
2526 #include "mount.h"
2627 #include "internal.h"
2728
29+enum legacy_fs_param {
30+ LEGACY_FS_UNSET_PARAMS,
31+ LEGACY_FS_MONOLITHIC_PARAMS,
32+ LEGACY_FS_INDIVIDUAL_PARAMS,
33+};
34+
2835 struct legacy_fs_context {
2936 char *legacy_data; /* Data page for legacy filesystems */
3037 size_t data_size;
38+ enum legacy_fs_param param_type;
3139 };
3240
3341 static int legacy_init_fs_context(struct fs_context *fc);
3442
43+static const struct constant_table common_set_sb_flag[] = {
44+ { "dirsync", SB_DIRSYNC },
45+ { "lazytime", SB_LAZYTIME },
46+ { "mand", SB_MANDLOCK },
47+ { "posixacl", SB_POSIXACL },
48+ { "ro", SB_RDONLY },
49+ { "sync", SB_SYNCHRONOUS },
50+};
51+
52+static const struct constant_table common_clear_sb_flag[] = {
53+ { "async", SB_SYNCHRONOUS },
54+ { "nolazytime", SB_LAZYTIME },
55+ { "nomand", SB_MANDLOCK },
56+ { "rw", SB_RDONLY },
57+ { "silent", SB_SILENT },
58+};
59+
60+static const char *const forbidden_sb_flag[] = {
61+ "bind",
62+ "dev",
63+ "exec",
64+ "move",
65+ "noatime",
66+ "nodev",
67+ "nodiratime",
68+ "noexec",
69+ "norelatime",
70+ "nostrictatime",
71+ "nosuid",
72+ "private",
73+ "rec",
74+ "relatime",
75+ "remount",
76+ "shared",
77+ "slave",
78+ "strictatime",
79+ "suid",
80+ "unbindable",
81+};
82+
83+/*
84+ * Check for a common mount option that manipulates s_flags.
85+ */
86+static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
87+{
88+ unsigned int token;
89+ unsigned int i;
90+
91+ for (i = 0; i < ARRAY_SIZE(forbidden_sb_flag); i++)
92+ if (strcmp(key, forbidden_sb_flag[i]) == 0)
93+ return -EINVAL;
94+
95+ token = lookup_constant(common_set_sb_flag, key, 0);
96+ if (token) {
97+ fc->sb_flags |= token;
98+ fc->sb_flags_mask |= token;
99+ return 0;
100+ }
101+
102+ token = lookup_constant(common_clear_sb_flag, key, 0);
103+ if (token) {
104+ fc->sb_flags &= ~token;
105+ fc->sb_flags_mask |= token;
106+ return 0;
107+ }
108+
109+ return -ENOPARAM;
110+}
111+
112+/**
113+ * vfs_parse_fs_param - Add a single parameter to a superblock config
114+ * @fc: The filesystem context to modify
115+ * @param: The parameter
116+ *
117+ * A single mount option in string form is applied to the filesystem context
118+ * being set up. Certain standard options (for example "ro") are translated
119+ * into flag bits without going to the filesystem. The active security module
120+ * is allowed to observe and poach options. Any other options are passed over
121+ * to the filesystem to parse.
122+ *
123+ * This may be called multiple times for a context.
124+ *
125+ * Returns 0 on success and a negative error code on failure. In the event of
126+ * failure, supplementary error information may have been set.
127+ */
128+int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
129+{
130+ int ret;
131+
132+ if (!param->key)
133+ return invalf(fc, "Unnamed parameter\n");
134+
135+ ret = vfs_parse_sb_flag(fc, param->key);
136+ if (ret != -ENOPARAM)
137+ return ret;
138+
139+ ret = security_fs_context_parse_param(fc, param);
140+ if (ret != -ENOPARAM)
141+ /* Param belongs to the LSM or is disallowed by the LSM; so
142+ * don't pass to the FS.
143+ */
144+ return ret;
145+
146+ if (fc->ops->parse_param) {
147+ ret = fc->ops->parse_param(fc, param);
148+ if (ret != -ENOPARAM)
149+ return ret;
150+ }
151+
152+ /* If the filesystem doesn't take any arguments, give it the
153+ * default handling of source.
154+ */
155+ if (strcmp(param->key, "source") == 0) {
156+ if (param->type != fs_value_is_string)
157+ return invalf(fc, "VFS: Non-string source");
158+ if (fc->source)
159+ return invalf(fc, "VFS: Multiple sources");
160+ fc->source = param->string;
161+ param->string = NULL;
162+ return 0;
163+ }
164+
165+ return invalf(fc, "%s: Unknown parameter '%s'",
166+ fc->fs_type->name, param->key);
167+}
168+EXPORT_SYMBOL(vfs_parse_fs_param);
169+
170+/**
171+ * vfs_parse_fs_string - Convenience function to just parse a string.
172+ */
173+int vfs_parse_fs_string(struct fs_context *fc, const char *key,
174+ const char *value, size_t v_size)
175+{
176+ int ret;
177+
178+ struct fs_parameter param = {
179+ .key = key,
180+ .type = fs_value_is_string,
181+ .size = v_size,
182+ };
183+
184+ if (v_size > 0) {
185+ param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
186+ if (!param.string)
187+ return -ENOMEM;
188+ }
189+
190+ ret = vfs_parse_fs_param(fc, &param);
191+ kfree(param.string);
192+ return ret;
193+}
194+EXPORT_SYMBOL(vfs_parse_fs_string);
195+
196+/**
197+ * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
198+ * @ctx: The superblock configuration to fill in.
199+ * @data: The data to parse
200+ *
201+ * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
202+ * called from the ->monolithic_mount_data() fs_context operation.
203+ *
204+ * Returns 0 on success or the error returned by the ->parse_option() fs_context
205+ * operation on failure.
206+ */
207+int generic_parse_monolithic(struct fs_context *fc, void *data)
208+{
209+ char *options = data, *key;
210+ int ret = 0;
211+
212+ if (!options)
213+ return 0;
214+
215+ ret = security_sb_eat_lsm_opts(options, &fc->security);
216+ if (ret)
217+ return ret;
218+
219+ while ((key = strsep(&options, ",")) != NULL) {
220+ if (*key) {
221+ size_t v_len = 0;
222+ char *value = strchr(key, '=');
223+
224+ if (value) {
225+ if (value == key)
226+ continue;
227+ *value++ = 0;
228+ v_len = strlen(value);
229+ }
230+ ret = vfs_parse_fs_string(fc, key, value, v_len);
231+ if (ret < 0)
232+ break;
233+ }
234+ }
235+
236+ return ret;
237+}
238+EXPORT_SYMBOL(generic_parse_monolithic);
239+
35240 /**
36241 * alloc_fs_context - Create a filesystem context.
37242 * @fs_type: The filesystem type.
@@ -166,7 +371,87 @@ EXPORT_SYMBOL(put_fs_context); · 128 unmodified lines
166371 */
167372 static void legacy_fs_context_free(struct fs_context *fc)
168373 {
169− kfree(fc->fs_private);
374+ struct legacy_fs_context *ctx = fc->fs_private;
375+
376+ if (ctx) {
377+ if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
378+ kfree(ctx->legacy_data);
379+ kfree(ctx);
380+ }
381+}
382+
383+/*
384+ * Add a parameter to a legacy config. We build up a comma-separated list of
385+ * options.
386+ */
387+static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
388+{
389+ struct legacy_fs_context *ctx = fc->fs_private;
390+ unsigned int size = ctx->data_size;
391+ size_t len = 0;
392+
393+ if (strcmp(param->key, "source") == 0) {
394+ if (param->type != fs_value_is_string)
395+ return invalf(fc, "VFS: Legacy: Non-string source");
396+ if (fc->source)
397+ return invalf(fc, "VFS: Legacy: Multiple sources");
398+ fc->source = param->string;
399+ param->string = NULL;
400+ return 0;
401+ }
402+
403+ if ((fc->fs_type->fs_flags & FS_HAS_SUBTYPE) &&
404+ strcmp(param->key, "subtype") == 0) {
405+ if (param->type != fs_value_is_string)
406+ return invalf(fc, "VFS: Legacy: Non-string subtype");
407+ if (fc->subtype)
408+ return invalf(fc, "VFS: Legacy: Multiple subtype");
409+ fc->subtype = param->string;
410+ param->string = NULL;
411+ return 0;
412+ }
413+
414+ if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
415+ return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
416+
417+ switch (param->type) {
418+ case fs_value_is_string:
419+ len = 1 + param->size;
420+ /* Fall through */
421+ case fs_value_is_flag:
422+ len += strlen(param->key);
423+ break;
424+ default:
425+ return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
426+ param->key);
427+ }
428+
429+ if (len > PAGE_SIZE - 2 - size)
430+ return invalf(fc, "VFS: Legacy: Cumulative options too large");
431+ if (strchr(param->key, ',') ||
432+ (param->type == fs_value_is_string &&
433+ memchr(param->string, ',', param->size)))
434+ return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
435+ param->key);
436+ if (!ctx->legacy_data) {
437+ ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
438+ if (!ctx->legacy_data)
439+ return -ENOMEM;
440+ }
441+
442+ ctx->legacy_data[size++] = ',';
443+ len = strlen(param->key);
444+ memcpy(ctx->legacy_data + size, param->key, len);
445+ size += len;
446+ if (param->type == fs_value_is_string) {
447+ ctx->legacy_data[size++] = '=';
448+ memcpy(ctx->legacy_data + size, param->string, param->size);
449+ size += param->size;
450+ }
451+ ctx->legacy_data[size] = '\0';
452+ ctx->data_size = size;
453+ ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
454+ return 0;
170455 }
171456
172457 /*
@@ -175,9 +460,17 @@ static void legacy_fs_context_free(struct fs_context *fc) · 2 unmodified lines
175460 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
176461 {
177462 struct legacy_fs_context *ctx = fc->fs_private;
463+
464+ if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
465+ pr_warn("VFS: Can't mix monolithic and individual options\n");
466+ return -EINVAL;
467+ }
468+
178469 ctx->legacy_data = data;
470+ ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
179471 if (!ctx->legacy_data)
180472 return 0;
473+
181474 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
182475 return 0;
183476 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
@@ -221,6 +514,7 @@ static int legacy_reconfigure(struct fs_context *fc) · 37 unmodified lines
221514
222515 const struct fs_context_operations legacy_fs_context_ops = {
223516 .free = legacy_fs_context_free,
517+ .parse_param = legacy_parse_param,
224518 .parse_monolithic = legacy_parse_monolithic,
225519 .get_tree = legacy_get_tree,
226520 .reconfigure = legacy_reconfigure,
@@ -242,6 +536,10 @@ static int legacy_init_fs_context(struct fs_context *fc) · 15 unmodified lines
242536 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
243537 {
244538 int (*monolithic_mount_data)(struct fs_context *, void *);
539+
245540 monolithic_mount_data = fc->ops->parse_monolithic;
541+ if (!monolithic_mount_data)
542+ monolithic_mount_data = generic_parse_monolithic;
543+
246544 return monolithic_mount_data(fc, data);
247545 }
fs/namespace.c+9−16
@@ -997,17 +997,15 @@ struct vfsmount *vfs_kern_mount(struct file_system_type *type, · 996 unmodified lines
997997 int ret = 0;
998998
999999 if (!type)
1000− return ERR_PTR(-ENODEV);
1000+ return ERR_PTR(-EINVAL);
10011001
10021002 fc = fs_context_for_mount(type, flags);
10031003 if (IS_ERR(fc))
10041004 return ERR_CAST(fc);
10051005
1006− if (name) {
1007− fc->source = kstrdup(name, GFP_KERNEL);
1008− if (!fc->source)
1009− ret = -ENOMEM;
1010− }
1006+ if (name)
1007+ ret = vfs_parse_fs_string(fc, "source",
1008+ name, strlen(name));
10111009 if (!ret)
10121010 ret = parse_monolithic_mount_data(fc, data);
10131011 if (!ret)
@@ -2611,16 +2609,11 @@ static int do_new_mount(struct path *path, const char *fstype, int sb_flags, · 1597 unmodified lines
26112609 if (IS_ERR(fc))
26122610 return PTR_ERR(fc);
26132611
2614− if (subtype) {
2615− fc->subtype = kstrdup(subtype, GFP_KERNEL);
2616− if (!fc->subtype)
2617− err = -ENOMEM;
2618− }
2619− if (!err && name) {
2620− fc->source = kstrdup(name, GFP_KERNEL);
2621− if (!fc->source)
2622− err = -ENOMEM;
2623− }
2612+ if (subtype)
2613+ err = vfs_parse_fs_string(fc, "subtype",
2614+ subtype, strlen(subtype));
2615+ if (!err && name)
2616+ err = vfs_parse_fs_string(fc, "source", name, strlen(name));
26242617 if (!err)
26252618 err = parse_monolithic_mount_data(fc, data);
26262619 if (!err)
include/linux/fs.h+2−0
@@ -62,6 +62,7 @@ struct iov_iter; · 61 unmodified lines
6262 struct fscrypt_info;
6363 struct fscrypt_operations;
6464 struct fs_context;
65+struct fs_parameter_description;
6566
6667 extern void __init inode_init(void);
6768 extern void __init inode_init_early(void);
@@ -2175,6 +2176,7 @@ struct file_system_type { · 2107 unmodified lines
21752176 #define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
21762177 #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
21772178 int (*init_fs_context)(struct fs_context *);
2179+ const struct fs_parameter_description *parameters;
21782180 struct dentry *(*mount) (struct file_system_type *, int,
21792181 const char *, void *);
21802182 void (*kill_sb) (struct super_block *);
include/linux/fs_context.h+5−0
@@ -92,6 +92,7 @@ struct fs_context { · 91 unmodified lines
9292
9393 struct fs_context_operations {
9494 void (*free)(struct fs_context *fc);
95+ int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
9596 int (*parse_monolithic)(struct fs_context *fc, void *data);
9697 int (*get_tree)(struct fs_context *fc);
9798 int (*reconfigure)(struct fs_context *fc);
@@ -108,6 +109,10 @@ extern struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, · 10 unmodified lines
108109 extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,
109110 struct dentry *reference);
110111
112+extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);
113+extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
114+ const char *value, size_t v_size);
115+extern int generic_parse_monolithic(struct fs_context *fc, void *data);
111116 extern int vfs_get_tree(struct fs_context *fc);
112117 extern void put_fs_context(struct fs_context *fc);
113118

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t