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
16#include <linux/module.h>16#include <linux/module.h>
17#include <linux/slab.h>17#include <linux/slab.h>
18#include <linux/uaccess.h>18#include <linux/uaccess.h>
19#include <linux/fs_parser.h>
19 20
20/*21/*
21 * Handling of filesystem drivers list.22 * Handling of filesystem drivers list.
@@ -73,6 +74,9 @@ int register_filesystem(struct file_system_type * fs) · 51 unmodified lines
73 int res = 0;74 int res = 0;
74 struct file_system_type ** p;75 struct file_system_type ** p;
75 76
77 if (fs->parameters && !fs_validate_description(fs->parameters))
78 return -EINVAL;
79
76 BUG_ON(strchr(fs->name, '.'));80 BUG_ON(strchr(fs->name, '.'));
77 if (fs->next)81 if (fs->next)
78 return -EBUSY;82 return -EBUSY;
fs/fs_context.c+299−1
@@ -12,6 +12,7 @@ · 11 unmodified lines
12 12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/fs_context.h>14#include <linux/fs_context.h>
15#include <linux/fs_parser.h>
15#include <linux/fs.h>16#include <linux/fs.h>
16#include <linux/mount.h>17#include <linux/mount.h>
17#include <linux/nsproxy.h>18#include <linux/nsproxy.h>
@@ -25,13 +26,217 @@ · 7 unmodified lines
25#include "mount.h"26#include "mount.h"
26#include "internal.h"27#include "internal.h"
27 28
29enum legacy_fs_param {
30 LEGACY_FS_UNSET_PARAMS,
31 LEGACY_FS_MONOLITHIC_PARAMS,
32 LEGACY_FS_INDIVIDUAL_PARAMS,
33};
34
28struct legacy_fs_context {35struct legacy_fs_context {
29 char *legacy_data; /* Data page for legacy filesystems */36 char *legacy_data; /* Data page for legacy filesystems */
30 size_t data_size;37 size_t data_size;
38 enum legacy_fs_param param_type;
31};39};
32 40
33static int legacy_init_fs_context(struct fs_context *fc);41static int legacy_init_fs_context(struct fs_context *fc);
34 42
43static 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
52static 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
60static 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 */
86static 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 */
128int 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}
168EXPORT_SYMBOL(vfs_parse_fs_param);
169
170/**
171 * vfs_parse_fs_string - Convenience function to just parse a string.
172 */
173int 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}
194EXPORT_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 */
207int 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}
238EXPORT_SYMBOL(generic_parse_monolithic);
239
35/**240/**
36 * alloc_fs_context - Create a filesystem context.241 * alloc_fs_context - Create a filesystem context.
37 * @fs_type: The filesystem type.242 * @fs_type: The filesystem type.
@@ -166,7 +371,87 @@ EXPORT_SYMBOL(put_fs_context); · 128 unmodified lines
166 */371 */
167static void legacy_fs_context_free(struct fs_context *fc)372static void legacy_fs_context_free(struct fs_context *fc)
168{373{
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 */
387static 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;
170}455}
171 456
172/*457/*
@@ -175,9 +460,17 @@ static void legacy_fs_context_free(struct fs_context *fc) · 2 unmodified lines
175static int legacy_parse_monolithic(struct fs_context *fc, void *data)460static int legacy_parse_monolithic(struct fs_context *fc, void *data)
176{461{
177 struct legacy_fs_context *ctx = fc->fs_private;462 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
178 ctx->legacy_data = data;469 ctx->legacy_data = data;
470 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
179 if (!ctx->legacy_data)471 if (!ctx->legacy_data)
180 return 0;472 return 0;
473
181 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)474 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
182 return 0;475 return 0;
183 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);476 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
221 514
222const struct fs_context_operations legacy_fs_context_ops = {515const struct fs_context_operations legacy_fs_context_ops = {
223 .free = legacy_fs_context_free,516 .free = legacy_fs_context_free,
517 .parse_param = legacy_parse_param,
224 .parse_monolithic = legacy_parse_monolithic,518 .parse_monolithic = legacy_parse_monolithic,
225 .get_tree = legacy_get_tree,519 .get_tree = legacy_get_tree,
226 .reconfigure = legacy_reconfigure,520 .reconfigure = legacy_reconfigure,
@@ -242,6 +536,10 @@ static int legacy_init_fs_context(struct fs_context *fc) · 15 unmodified lines
242int parse_monolithic_mount_data(struct fs_context *fc, void *data)536int parse_monolithic_mount_data(struct fs_context *fc, void *data)
243{537{
244 int (*monolithic_mount_data)(struct fs_context *, void *);538 int (*monolithic_mount_data)(struct fs_context *, void *);
539
245 monolithic_mount_data = fc->ops->parse_monolithic;540 monolithic_mount_data = fc->ops->parse_monolithic;
541 if (!monolithic_mount_data)
542 monolithic_mount_data = generic_parse_monolithic;
543
246 return monolithic_mount_data(fc, data);544 return monolithic_mount_data(fc, data);
247}545}
fs/namespace.c+9−16
@@ -997,17 +997,15 @@ struct vfsmount *vfs_kern_mount(struct file_system_type *type, · 996 unmodified lines
997 int ret = 0;997 int ret = 0;
998 998
999 if (!type)999 if (!type)
1000 return ERR_PTR(-ENODEV);1000 return ERR_PTR(-EINVAL);
1001 1001
1002 fc = fs_context_for_mount(type, flags);1002 fc = fs_context_for_mount(type, flags);
1003 if (IS_ERR(fc))1003 if (IS_ERR(fc))
1004 return ERR_CAST(fc);1004 return ERR_CAST(fc);
1005 1005
1006 if (name) {1006 if (name)
1007 fc->source = kstrdup(name, GFP_KERNEL);1007 ret = vfs_parse_fs_string(fc, "source",
1008 if (!fc->source)1008 name, strlen(name));
1009 ret = -ENOMEM;
1010 }
1011 if (!ret)1009 if (!ret)
1012 ret = parse_monolithic_mount_data(fc, data);1010 ret = parse_monolithic_mount_data(fc, data);
1013 if (!ret)1011 if (!ret)
@@ -2611,16 +2609,11 @@ static int do_new_mount(struct path *path, const char *fstype, int sb_flags, · 1597 unmodified lines
2611 if (IS_ERR(fc))2609 if (IS_ERR(fc))
2612 return PTR_ERR(fc);2610 return PTR_ERR(fc);
2613 2611
2614 if (subtype) {2612 if (subtype)
2615 fc->subtype = kstrdup(subtype, GFP_KERNEL);2613 err = vfs_parse_fs_string(fc, "subtype",
2616 if (!fc->subtype)2614 subtype, strlen(subtype));
2617 err = -ENOMEM;2615 if (!err && name)
2618 }2616 err = vfs_parse_fs_string(fc, "source", name, strlen(name));
2619 if (!err && name) {
2620 fc->source = kstrdup(name, GFP_KERNEL);
2621 if (!fc->source)
2622 err = -ENOMEM;
2623 }
2624 if (!err)2617 if (!err)
2625 err = parse_monolithic_mount_data(fc, data);2618 err = parse_monolithic_mount_data(fc, data);
2626 if (!err)2619 if (!err)
include/linux/fs.h+2−0
@@ -62,6 +62,7 @@ struct iov_iter; · 61 unmodified lines
62struct fscrypt_info;62struct fscrypt_info;
63struct fscrypt_operations;63struct fscrypt_operations;
64struct fs_context;64struct fs_context;
65struct fs_parameter_description;
65 66
66extern void __init inode_init(void);67extern void __init inode_init(void);
67extern void __init inode_init_early(void);68extern void __init inode_init_early(void);
@@ -2175,6 +2176,7 @@ struct file_system_type { · 2107 unmodified lines
2175#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */2176#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
2176#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */2177#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
2177 int (*init_fs_context)(struct fs_context *);2178 int (*init_fs_context)(struct fs_context *);
2179 const struct fs_parameter_description *parameters;
2178 struct dentry *(*mount) (struct file_system_type *, int,2180 struct dentry *(*mount) (struct file_system_type *, int,
2179 const char *, void *);2181 const char *, void *);
2180 void (*kill_sb) (struct super_block *);2182 void (*kill_sb) (struct super_block *);
include/linux/fs_context.h+5−0
@@ -92,6 +92,7 @@ struct fs_context { · 91 unmodified lines
92 92
93struct fs_context_operations {93struct fs_context_operations {
94 void (*free)(struct fs_context *fc);94 void (*free)(struct fs_context *fc);
95 int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
95 int (*parse_monolithic)(struct fs_context *fc, void *data);96 int (*parse_monolithic)(struct fs_context *fc, void *data);
96 int (*get_tree)(struct fs_context *fc);97 int (*get_tree)(struct fs_context *fc);
97 int (*reconfigure)(struct fs_context *fc);98 int (*reconfigure)(struct fs_context *fc);
@@ -108,6 +109,10 @@ extern struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, · 10 unmodified lines
108extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,109extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,
109 struct dentry *reference);110 struct dentry *reference);
110 111
112extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);
113extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
114 const char *value, size_t v_size);
115extern int generic_parse_monolithic(struct fs_context *fc, void *data);
111extern int vfs_get_tree(struct fs_context *fc);116extern int vfs_get_tree(struct fs_context *fc);
112extern void put_fs_context(struct fs_context *fc);117extern void put_fs_context(struct fs_context *fc);
113 118

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