Commit | Line | Data |
---|---|---|
b4d0d230 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
9bc61ab1 DH |
2 | /* Provide a way to create a superblock configuration context within the kernel |
3 | * that allows a superblock to be set up prior to mounting. | |
4 | * | |
5 | * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. | |
6 | * Written by David Howells (dhowells@redhat.com) | |
9bc61ab1 DH |
7 | */ |
8 | ||
9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
007ec26c | 10 | #include <linux/module.h> |
9bc61ab1 | 11 | #include <linux/fs_context.h> |
3e1aeb00 | 12 | #include <linux/fs_parser.h> |
9bc61ab1 DH |
13 | #include <linux/fs.h> |
14 | #include <linux/mount.h> | |
15 | #include <linux/nsproxy.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/magic.h> | |
18 | #include <linux/security.h> | |
19 | #include <linux/mnt_namespace.h> | |
20 | #include <linux/pid_namespace.h> | |
21 | #include <linux/user_namespace.h> | |
22 | #include <net/net_namespace.h> | |
007ec26c | 23 | #include <asm/sections.h> |
9bc61ab1 DH |
24 | #include "mount.h" |
25 | #include "internal.h" | |
26 | ||
3e1aeb00 DH |
27 | enum legacy_fs_param { |
28 | LEGACY_FS_UNSET_PARAMS, | |
29 | LEGACY_FS_MONOLITHIC_PARAMS, | |
30 | LEGACY_FS_INDIVIDUAL_PARAMS, | |
31 | }; | |
32 | ||
9bc61ab1 DH |
33 | struct legacy_fs_context { |
34 | char *legacy_data; /* Data page for legacy filesystems */ | |
35 | size_t data_size; | |
3e1aeb00 | 36 | enum legacy_fs_param param_type; |
9bc61ab1 DH |
37 | }; |
38 | ||
39 | static int legacy_init_fs_context(struct fs_context *fc); | |
40 | ||
3e1aeb00 DH |
41 | static const struct constant_table common_set_sb_flag[] = { |
42 | { "dirsync", SB_DIRSYNC }, | |
43 | { "lazytime", SB_LAZYTIME }, | |
44 | { "mand", SB_MANDLOCK }, | |
45 | { "posixacl", SB_POSIXACL }, | |
46 | { "ro", SB_RDONLY }, | |
47 | { "sync", SB_SYNCHRONOUS }, | |
34264ae3 | 48 | { }, |
3e1aeb00 DH |
49 | }; |
50 | ||
51 | static const struct constant_table common_clear_sb_flag[] = { | |
52 | { "async", SB_SYNCHRONOUS }, | |
53 | { "nolazytime", SB_LAZYTIME }, | |
54 | { "nomand", SB_MANDLOCK }, | |
55 | { "rw", SB_RDONLY }, | |
56 | { "silent", SB_SILENT }, | |
34264ae3 | 57 | { }, |
3e1aeb00 DH |
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, | |
0f89589a | 180 | .type = fs_value_is_flag, |
3e1aeb00 DH |
181 | .size = v_size, |
182 | }; | |
183 | ||
0f89589a | 184 | if (value) { |
3e1aeb00 DH |
185 | param.string = kmemdup_nul(value, v_size, GFP_KERNEL); |
186 | if (!param.string) | |
187 | return -ENOMEM; | |
0f89589a | 188 | param.type = fs_value_is_string; |
3e1aeb00 DH |
189 | } |
190 | ||
191 | ret = vfs_parse_fs_param(fc, ¶m); | |
192 | kfree(param.string); | |
193 | return ret; | |
194 | } | |
195 | EXPORT_SYMBOL(vfs_parse_fs_string); | |
196 | ||
197 | /** | |
198 | * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data | |
199 | * @ctx: The superblock configuration to fill in. | |
200 | * @data: The data to parse | |
201 | * | |
202 | * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be | |
203 | * called from the ->monolithic_mount_data() fs_context operation. | |
204 | * | |
205 | * Returns 0 on success or the error returned by the ->parse_option() fs_context | |
206 | * operation on failure. | |
207 | */ | |
208 | int generic_parse_monolithic(struct fs_context *fc, void *data) | |
209 | { | |
210 | char *options = data, *key; | |
211 | int ret = 0; | |
212 | ||
213 | if (!options) | |
214 | return 0; | |
215 | ||
216 | ret = security_sb_eat_lsm_opts(options, &fc->security); | |
217 | if (ret) | |
218 | return ret; | |
219 | ||
220 | while ((key = strsep(&options, ",")) != NULL) { | |
221 | if (*key) { | |
222 | size_t v_len = 0; | |
223 | char *value = strchr(key, '='); | |
224 | ||
225 | if (value) { | |
226 | if (value == key) | |
227 | continue; | |
228 | *value++ = 0; | |
229 | v_len = strlen(value); | |
230 | } | |
231 | ret = vfs_parse_fs_string(fc, key, value, v_len); | |
232 | if (ret < 0) | |
233 | break; | |
234 | } | |
235 | } | |
236 | ||
237 | return ret; | |
238 | } | |
239 | EXPORT_SYMBOL(generic_parse_monolithic); | |
240 | ||
9bc61ab1 DH |
241 | /** |
242 | * alloc_fs_context - Create a filesystem context. | |
243 | * @fs_type: The filesystem type. | |
244 | * @reference: The dentry from which this one derives (or NULL) | |
245 | * @sb_flags: Filesystem/superblock flags (SB_*) | |
246 | * @sb_flags_mask: Applicable members of @sb_flags | |
247 | * @purpose: The purpose that this configuration shall be used for. | |
248 | * | |
249 | * Open a filesystem and create a mount context. The mount context is | |
250 | * initialised with the supplied flags and, if a submount/automount from | |
251 | * another superblock (referred to by @reference) is supplied, may have | |
252 | * parameters such as namespaces copied across from that superblock. | |
253 | */ | |
254 | static struct fs_context *alloc_fs_context(struct file_system_type *fs_type, | |
255 | struct dentry *reference, | |
256 | unsigned int sb_flags, | |
257 | unsigned int sb_flags_mask, | |
258 | enum fs_context_purpose purpose) | |
259 | { | |
f3a09c92 | 260 | int (*init_fs_context)(struct fs_context *); |
9bc61ab1 DH |
261 | struct fs_context *fc; |
262 | int ret = -ENOMEM; | |
263 | ||
264 | fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL); | |
265 | if (!fc) | |
266 | return ERR_PTR(-ENOMEM); | |
267 | ||
268 | fc->purpose = purpose; | |
269 | fc->sb_flags = sb_flags; | |
270 | fc->sb_flags_mask = sb_flags_mask; | |
271 | fc->fs_type = get_filesystem(fs_type); | |
272 | fc->cred = get_current_cred(); | |
273 | fc->net_ns = get_net(current->nsproxy->net_ns); | |
274 | ||
24dcb3d9 DH |
275 | mutex_init(&fc->uapi_mutex); |
276 | ||
9bc61ab1 DH |
277 | switch (purpose) { |
278 | case FS_CONTEXT_FOR_MOUNT: | |
279 | fc->user_ns = get_user_ns(fc->cred->user_ns); | |
280 | break; | |
e1a91586 AV |
281 | case FS_CONTEXT_FOR_SUBMOUNT: |
282 | fc->user_ns = get_user_ns(reference->d_sb->s_user_ns); | |
283 | break; | |
8d0347f6 | 284 | case FS_CONTEXT_FOR_RECONFIGURE: |
8d0347f6 | 285 | atomic_inc(&reference->d_sb->s_active); |
1dd9bc08 | 286 | fc->user_ns = get_user_ns(reference->d_sb->s_user_ns); |
8d0347f6 DH |
287 | fc->root = dget(reference); |
288 | break; | |
9bc61ab1 DH |
289 | } |
290 | ||
f3a09c92 AV |
291 | /* TODO: Make all filesystems support this unconditionally */ |
292 | init_fs_context = fc->fs_type->init_fs_context; | |
293 | if (!init_fs_context) | |
294 | init_fs_context = legacy_init_fs_context; | |
295 | ||
296 | ret = init_fs_context(fc); | |
9bc61ab1 DH |
297 | if (ret < 0) |
298 | goto err_fc; | |
299 | fc->need_free = true; | |
300 | return fc; | |
301 | ||
302 | err_fc: | |
303 | put_fs_context(fc); | |
304 | return ERR_PTR(ret); | |
305 | } | |
306 | ||
307 | struct fs_context *fs_context_for_mount(struct file_system_type *fs_type, | |
308 | unsigned int sb_flags) | |
309 | { | |
310 | return alloc_fs_context(fs_type, NULL, sb_flags, 0, | |
311 | FS_CONTEXT_FOR_MOUNT); | |
312 | } | |
313 | EXPORT_SYMBOL(fs_context_for_mount); | |
314 | ||
8d0347f6 DH |
315 | struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, |
316 | unsigned int sb_flags, | |
317 | unsigned int sb_flags_mask) | |
318 | { | |
319 | return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags, | |
320 | sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE); | |
321 | } | |
322 | EXPORT_SYMBOL(fs_context_for_reconfigure); | |
323 | ||
e1a91586 AV |
324 | struct fs_context *fs_context_for_submount(struct file_system_type *type, |
325 | struct dentry *reference) | |
326 | { | |
327 | return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT); | |
328 | } | |
329 | EXPORT_SYMBOL(fs_context_for_submount); | |
330 | ||
c9ce29ed AV |
331 | void fc_drop_locked(struct fs_context *fc) |
332 | { | |
333 | struct super_block *sb = fc->root->d_sb; | |
334 | dput(fc->root); | |
335 | fc->root = NULL; | |
336 | deactivate_locked_super(sb); | |
337 | } | |
338 | ||
9bc61ab1 | 339 | static void legacy_fs_context_free(struct fs_context *fc); |
8d0347f6 | 340 | |
0b52075e AV |
341 | /** |
342 | * vfs_dup_fc_config: Duplicate a filesystem context. | |
343 | * @src_fc: The context to copy. | |
344 | */ | |
345 | struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc) | |
346 | { | |
347 | struct fs_context *fc; | |
348 | int ret; | |
349 | ||
350 | if (!src_fc->ops->dup) | |
351 | return ERR_PTR(-EOPNOTSUPP); | |
352 | ||
353 | fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL); | |
354 | if (!fc) | |
355 | return ERR_PTR(-ENOMEM); | |
356 | ||
24dcb3d9 DH |
357 | mutex_init(&fc->uapi_mutex); |
358 | ||
0b52075e AV |
359 | fc->fs_private = NULL; |
360 | fc->s_fs_info = NULL; | |
361 | fc->source = NULL; | |
362 | fc->security = NULL; | |
363 | get_filesystem(fc->fs_type); | |
364 | get_net(fc->net_ns); | |
365 | get_user_ns(fc->user_ns); | |
366 | get_cred(fc->cred); | |
007ec26c DH |
367 | if (fc->log) |
368 | refcount_inc(&fc->log->usage); | |
0b52075e AV |
369 | |
370 | /* Can't call put until we've called ->dup */ | |
371 | ret = fc->ops->dup(fc, src_fc); | |
372 | if (ret < 0) | |
373 | goto err_fc; | |
374 | ||
375 | ret = security_fs_context_dup(fc, src_fc); | |
376 | if (ret < 0) | |
377 | goto err_fc; | |
378 | return fc; | |
379 | ||
380 | err_fc: | |
381 | put_fs_context(fc); | |
382 | return ERR_PTR(ret); | |
383 | } | |
384 | EXPORT_SYMBOL(vfs_dup_fs_context); | |
385 | ||
e7582e16 DH |
386 | /** |
387 | * logfc - Log a message to a filesystem context | |
388 | * @fc: The filesystem context to log to. | |
389 | * @fmt: The format of the buffer. | |
390 | */ | |
9f09f649 | 391 | void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...) |
e7582e16 DH |
392 | { |
393 | va_list va; | |
9f09f649 | 394 | struct va_format vaf = {.fmt = fmt, .va = &va}; |
e7582e16 DH |
395 | |
396 | va_start(va, fmt); | |
007ec26c | 397 | if (!log) { |
9f09f649 | 398 | switch (level) { |
007ec26c | 399 | case 'w': |
9f09f649 AV |
400 | printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "", |
401 | prefix ? ": " : "", &vaf); | |
007ec26c DH |
402 | break; |
403 | case 'e': | |
9f09f649 AV |
404 | printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "", |
405 | prefix ? ": " : "", &vaf); | |
007ec26c DH |
406 | break; |
407 | default: | |
9f09f649 AV |
408 | printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "", |
409 | prefix ? ": " : "", &vaf); | |
007ec26c DH |
410 | break; |
411 | } | |
007ec26c DH |
412 | } else { |
413 | unsigned int logsize = ARRAY_SIZE(log->buffer); | |
414 | u8 index; | |
9f09f649 AV |
415 | char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level, |
416 | prefix ? prefix : "", | |
417 | prefix ? ": " : "", &vaf); | |
007ec26c DH |
418 | |
419 | index = log->head & (logsize - 1); | |
420 | BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) || | |
421 | sizeof(log->tail) != sizeof(u8)); | |
422 | if ((u8)(log->head - log->tail) == logsize) { | |
423 | /* The buffer is full, discard the oldest message */ | |
424 | if (log->need_free & (1 << index)) | |
425 | kfree(log->buffer[index]); | |
426 | log->tail++; | |
427 | } | |
e7582e16 | 428 | |
9f09f649 AV |
429 | log->buffer[index] = q ? q : "OOM: Can't store error string"; |
430 | if (q) | |
431 | log->need_free |= 1 << index; | |
432 | else | |
433 | log->need_free &= ~(1 << index); | |
007ec26c DH |
434 | log->head++; |
435 | } | |
e7582e16 DH |
436 | va_end(va); |
437 | } | |
438 | EXPORT_SYMBOL(logfc); | |
007ec26c DH |
439 | |
440 | /* | |
441 | * Free a logging structure. | |
442 | */ | |
443 | static void put_fc_log(struct fs_context *fc) | |
444 | { | |
445 | struct fc_log *log = fc->log; | |
446 | int i; | |
447 | ||
448 | if (log) { | |
449 | if (refcount_dec_and_test(&log->usage)) { | |
450 | fc->log = NULL; | |
451 | for (i = 0; i <= 7; i++) | |
452 | if (log->need_free & (1 << i)) | |
453 | kfree(log->buffer[i]); | |
454 | kfree(log); | |
455 | } | |
456 | } | |
457 | } | |
e7582e16 | 458 | |
9bc61ab1 DH |
459 | /** |
460 | * put_fs_context - Dispose of a superblock configuration context. | |
461 | * @fc: The context to dispose of. | |
462 | */ | |
463 | void put_fs_context(struct fs_context *fc) | |
464 | { | |
465 | struct super_block *sb; | |
466 | ||
467 | if (fc->root) { | |
468 | sb = fc->root->d_sb; | |
469 | dput(fc->root); | |
470 | fc->root = NULL; | |
471 | deactivate_super(sb); | |
472 | } | |
473 | ||
f3a09c92 AV |
474 | if (fc->need_free && fc->ops && fc->ops->free) |
475 | fc->ops->free(fc); | |
9bc61ab1 DH |
476 | |
477 | security_free_mnt_opts(&fc->security); | |
8d0347f6 | 478 | put_net(fc->net_ns); |
9bc61ab1 DH |
479 | put_user_ns(fc->user_ns); |
480 | put_cred(fc->cred); | |
007ec26c | 481 | put_fc_log(fc); |
9bc61ab1 DH |
482 | put_filesystem(fc->fs_type); |
483 | kfree(fc->source); | |
484 | kfree(fc); | |
485 | } | |
486 | EXPORT_SYMBOL(put_fs_context); | |
487 | ||
488 | /* | |
489 | * Free the config for a filesystem that doesn't support fs_context. | |
490 | */ | |
491 | static void legacy_fs_context_free(struct fs_context *fc) | |
492 | { | |
3e1aeb00 DH |
493 | struct legacy_fs_context *ctx = fc->fs_private; |
494 | ||
495 | if (ctx) { | |
496 | if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) | |
497 | kfree(ctx->legacy_data); | |
498 | kfree(ctx); | |
499 | } | |
500 | } | |
501 | ||
0b52075e AV |
502 | /* |
503 | * Duplicate a legacy config. | |
504 | */ | |
505 | static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) | |
506 | { | |
507 | struct legacy_fs_context *ctx; | |
508 | struct legacy_fs_context *src_ctx = src_fc->fs_private; | |
509 | ||
510 | ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL); | |
511 | if (!ctx) | |
512 | return -ENOMEM; | |
513 | ||
514 | if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) { | |
515 | ctx->legacy_data = kmemdup(src_ctx->legacy_data, | |
516 | src_ctx->data_size, GFP_KERNEL); | |
517 | if (!ctx->legacy_data) { | |
518 | kfree(ctx); | |
519 | return -ENOMEM; | |
520 | } | |
521 | } | |
522 | ||
523 | fc->fs_private = ctx; | |
524 | return 0; | |
525 | } | |
526 | ||
3e1aeb00 DH |
527 | /* |
528 | * Add a parameter to a legacy config. We build up a comma-separated list of | |
529 | * options. | |
530 | */ | |
531 | static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param) | |
532 | { | |
533 | struct legacy_fs_context *ctx = fc->fs_private; | |
534 | unsigned int size = ctx->data_size; | |
535 | size_t len = 0; | |
536 | ||
537 | if (strcmp(param->key, "source") == 0) { | |
538 | if (param->type != fs_value_is_string) | |
539 | return invalf(fc, "VFS: Legacy: Non-string source"); | |
540 | if (fc->source) | |
541 | return invalf(fc, "VFS: Legacy: Multiple sources"); | |
542 | fc->source = param->string; | |
543 | param->string = NULL; | |
544 | return 0; | |
545 | } | |
546 | ||
3e1aeb00 DH |
547 | if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS) |
548 | return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options"); | |
549 | ||
550 | switch (param->type) { | |
551 | case fs_value_is_string: | |
552 | len = 1 + param->size; | |
553 | /* Fall through */ | |
554 | case fs_value_is_flag: | |
555 | len += strlen(param->key); | |
556 | break; | |
557 | default: | |
558 | return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported", | |
559 | param->key); | |
560 | } | |
561 | ||
562 | if (len > PAGE_SIZE - 2 - size) | |
563 | return invalf(fc, "VFS: Legacy: Cumulative options too large"); | |
564 | if (strchr(param->key, ',') || | |
565 | (param->type == fs_value_is_string && | |
566 | memchr(param->string, ',', param->size))) | |
567 | return invalf(fc, "VFS: Legacy: Option '%s' contained comma", | |
568 | param->key); | |
569 | if (!ctx->legacy_data) { | |
570 | ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
571 | if (!ctx->legacy_data) | |
572 | return -ENOMEM; | |
573 | } | |
574 | ||
575 | ctx->legacy_data[size++] = ','; | |
576 | len = strlen(param->key); | |
577 | memcpy(ctx->legacy_data + size, param->key, len); | |
578 | size += len; | |
579 | if (param->type == fs_value_is_string) { | |
580 | ctx->legacy_data[size++] = '='; | |
581 | memcpy(ctx->legacy_data + size, param->string, param->size); | |
582 | size += param->size; | |
583 | } | |
584 | ctx->legacy_data[size] = '\0'; | |
585 | ctx->data_size = size; | |
586 | ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS; | |
587 | return 0; | |
9bc61ab1 DH |
588 | } |
589 | ||
590 | /* | |
591 | * Add monolithic mount data. | |
592 | */ | |
593 | static int legacy_parse_monolithic(struct fs_context *fc, void *data) | |
594 | { | |
595 | struct legacy_fs_context *ctx = fc->fs_private; | |
3e1aeb00 DH |
596 | |
597 | if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) { | |
598 | pr_warn("VFS: Can't mix monolithic and individual options\n"); | |
599 | return -EINVAL; | |
600 | } | |
601 | ||
9bc61ab1 | 602 | ctx->legacy_data = data; |
3e1aeb00 | 603 | ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS; |
9bc61ab1 DH |
604 | if (!ctx->legacy_data) |
605 | return 0; | |
3e1aeb00 | 606 | |
9bc61ab1 DH |
607 | if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA) |
608 | return 0; | |
609 | return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security); | |
610 | } | |
611 | ||
612 | /* | |
613 | * Get a mountable root with the legacy mount command. | |
614 | */ | |
f3a09c92 | 615 | static int legacy_get_tree(struct fs_context *fc) |
9bc61ab1 DH |
616 | { |
617 | struct legacy_fs_context *ctx = fc->fs_private; | |
618 | struct super_block *sb; | |
619 | struct dentry *root; | |
620 | ||
621 | root = fc->fs_type->mount(fc->fs_type, fc->sb_flags, | |
622 | fc->source, ctx->legacy_data); | |
623 | if (IS_ERR(root)) | |
624 | return PTR_ERR(root); | |
625 | ||
626 | sb = root->d_sb; | |
627 | BUG_ON(!sb); | |
628 | ||
629 | fc->root = root; | |
630 | return 0; | |
631 | } | |
632 | ||
8d0347f6 DH |
633 | /* |
634 | * Handle remount. | |
635 | */ | |
f3a09c92 | 636 | static int legacy_reconfigure(struct fs_context *fc) |
8d0347f6 DH |
637 | { |
638 | struct legacy_fs_context *ctx = fc->fs_private; | |
639 | struct super_block *sb = fc->root->d_sb; | |
640 | ||
641 | if (!sb->s_op->remount_fs) | |
642 | return 0; | |
643 | ||
644 | return sb->s_op->remount_fs(sb, &fc->sb_flags, | |
645 | ctx ? ctx->legacy_data : NULL); | |
646 | } | |
647 | ||
f3a09c92 AV |
648 | const struct fs_context_operations legacy_fs_context_ops = { |
649 | .free = legacy_fs_context_free, | |
0b52075e | 650 | .dup = legacy_fs_context_dup, |
3e1aeb00 | 651 | .parse_param = legacy_parse_param, |
f3a09c92 AV |
652 | .parse_monolithic = legacy_parse_monolithic, |
653 | .get_tree = legacy_get_tree, | |
654 | .reconfigure = legacy_reconfigure, | |
655 | }; | |
656 | ||
9bc61ab1 DH |
657 | /* |
658 | * Initialise a legacy context for a filesystem that doesn't support | |
659 | * fs_context. | |
660 | */ | |
661 | static int legacy_init_fs_context(struct fs_context *fc) | |
662 | { | |
663 | fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL); | |
664 | if (!fc->fs_private) | |
665 | return -ENOMEM; | |
f3a09c92 | 666 | fc->ops = &legacy_fs_context_ops; |
9bc61ab1 DH |
667 | return 0; |
668 | } | |
669 | ||
670 | int parse_monolithic_mount_data(struct fs_context *fc, void *data) | |
671 | { | |
f3a09c92 | 672 | int (*monolithic_mount_data)(struct fs_context *, void *); |
3e1aeb00 | 673 | |
f3a09c92 | 674 | monolithic_mount_data = fc->ops->parse_monolithic; |
3e1aeb00 DH |
675 | if (!monolithic_mount_data) |
676 | monolithic_mount_data = generic_parse_monolithic; | |
677 | ||
f3a09c92 | 678 | return monolithic_mount_data(fc, data); |
9bc61ab1 | 679 | } |
ecdab150 DH |
680 | |
681 | /* | |
682 | * Clean up a context after performing an action on it and put it into a state | |
683 | * from where it can be used to reconfigure a superblock. | |
684 | * | |
685 | * Note that here we do only the parts that can't fail; the rest is in | |
686 | * finish_clean_context() below and in between those fs_context is marked | |
687 | * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after | |
688 | * successful mount or remount we need to report success to userland. | |
689 | * Trying to do full reinit (for the sake of possible subsequent remount) | |
690 | * and failing to allocate memory would've put us into a nasty situation. | |
691 | * So here we only discard the old state and reinitialization is left | |
692 | * until we actually try to reconfigure. | |
693 | */ | |
694 | void vfs_clean_context(struct fs_context *fc) | |
695 | { | |
696 | if (fc->need_free && fc->ops && fc->ops->free) | |
697 | fc->ops->free(fc); | |
698 | fc->need_free = false; | |
699 | fc->fs_private = NULL; | |
700 | fc->s_fs_info = NULL; | |
701 | fc->sb_flags = 0; | |
702 | security_free_mnt_opts(&fc->security); | |
ecdab150 DH |
703 | kfree(fc->source); |
704 | fc->source = NULL; | |
705 | ||
706 | fc->purpose = FS_CONTEXT_FOR_RECONFIGURE; | |
707 | fc->phase = FS_CONTEXT_AWAITING_RECONF; | |
708 | } | |
709 | ||
710 | int finish_clean_context(struct fs_context *fc) | |
711 | { | |
712 | int error; | |
713 | ||
714 | if (fc->phase != FS_CONTEXT_AWAITING_RECONF) | |
715 | return 0; | |
716 | ||
717 | if (fc->fs_type->init_fs_context) | |
718 | error = fc->fs_type->init_fs_context(fc); | |
719 | else | |
720 | error = legacy_init_fs_context(fc); | |
721 | if (unlikely(error)) { | |
722 | fc->phase = FS_CONTEXT_FAILED; | |
723 | return error; | |
724 | } | |
725 | fc->need_free = true; | |
726 | fc->phase = FS_CONTEXT_RECONF_PARAMS; | |
727 | return 0; | |
728 | } |