1 // SPDX-License-Identifier: GPL-2.0-or-later
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)
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/fs_context.h>
12 #include <linux/fs_parser.h>
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>
23 #include <asm/sections.h>
24 #include "mount.h"
25 #include "internal.h"
26
27 enum legacy_fs_param {
28 LEGACY_FS_UNSET_PARAMS,
29 LEGACY_FS_MONOLITHIC_PARAMS,
30 LEGACY_FS_INDIVIDUAL_PARAMS,
31 };
32
33 struct legacy_fs_context {
34 char *legacy_data; /* Data page for legacy filesystems */
35 size_t data_size;
36 enum legacy_fs_param param_type;
37 };
38
39 static int legacy_init_fs_context(struct fs_context *fc);
40
41 static const struct constant_table common_set_sb_flag[] = {
42 { "dirsync", SB_DIRSYNC },
43 { "lazytime", SB_LAZYTIME },
44 { "mand", SB_MANDLOCK },
45 { "ro", SB_RDONLY },
46 { "sync", SB_SYNCHRONOUS },
47 { },
48 };
49
50 static const struct constant_table common_clear_sb_flag[] = {
51 { "async", SB_SYNCHRONOUS },
52 { "nolazytime", SB_LAZYTIME },
53 { "nomand", SB_MANDLOCK },
54 { "rw", SB_RDONLY },
55 { },
56 };
57
58 /*
59 * Check for a common mount option that manipulates s_flags.
60 */
vfs_parse_sb_flag(struct fs_context * fc,const char * key)61 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
62 {
63 unsigned int token;
64
65 token = lookup_constant(common_set_sb_flag, key, 0);
66 if (token) {
67 fc->sb_flags |= token;
68 fc->sb_flags_mask |= token;
69 return 0;
70 }
71
72 token = lookup_constant(common_clear_sb_flag, key, 0);
73 if (token) {
74 fc->sb_flags &= ~token;
75 fc->sb_flags_mask |= token;
76 return 0;
77 }
78
79 return -ENOPARAM;
80 }
81
82 /**
83 * vfs_parse_fs_param_source - Handle setting "source" via parameter
84 * @fc: The filesystem context to modify
85 * @param: The parameter
86 *
87 * This is a simple helper for filesystems to verify that the "source" they
88 * accept is sane.
89 *
90 * Returns 0 on success, -ENOPARAM if this is not "source" parameter, and
91 * -EINVAL otherwise. In the event of failure, supplementary error information
92 * is logged.
93 */
vfs_parse_fs_param_source(struct fs_context * fc,struct fs_parameter * param)94 int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
95 {
96 if (strcmp(param->key, "source") != 0)
97 return -ENOPARAM;
98
99 if (param->type != fs_value_is_string)
100 return invalf(fc, "Non-string source");
101
102 if (fc->source)
103 return invalf(fc, "Multiple sources");
104
105 fc->source = param->string;
106 param->string = NULL;
107 return 0;
108 }
109 EXPORT_SYMBOL(vfs_parse_fs_param_source);
110
111 /**
112 * vfs_parse_fs_param - Add a single parameter to a superblock config
113 * @fc: The filesystem context to modify
114 * @param: The parameter
115 *
116 * A single mount option in string form is applied to the filesystem context
117 * being set up. Certain standard options (for example "ro") are translated
118 * into flag bits without going to the filesystem. The active security module
119 * is allowed to observe and poach options. Any other options are passed over
120 * to the filesystem to parse.
121 *
122 * This may be called multiple times for a context.
123 *
124 * Returns 0 on success and a negative error code on failure. In the event of
125 * failure, supplementary error information may have been set.
126 */
vfs_parse_fs_param(struct fs_context * fc,struct fs_parameter * param)127 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
128 {
129 int ret;
130
131 if (!param->key)
132 return invalf(fc, "Unnamed parameter\n");
133
134 ret = vfs_parse_sb_flag(fc, param->key);
135 if (ret != -ENOPARAM)
136 return ret;
137
138 ret = security_fs_context_parse_param(fc, param);
139 if (ret != -ENOPARAM)
140 /* Param belongs to the LSM or is disallowed by the LSM; so
141 * don't pass to the FS.
142 */
143 return ret;
144
145 if (fc->ops->parse_param) {
146 ret = fc->ops->parse_param(fc, param);
147 if (ret != -ENOPARAM)
148 return ret;
149 }
150
151 /* If the filesystem doesn't take any arguments, give it the
152 * default handling of source.
153 */
154 ret = vfs_parse_fs_param_source(fc, param);
155 if (ret != -ENOPARAM)
156 return ret;
157
158 return invalf(fc, "%s: Unknown parameter '%s'",
159 fc->fs_type->name, param->key);
160 }
161 EXPORT_SYMBOL(vfs_parse_fs_param);
162
163 /**
164 * vfs_parse_fs_qstr - Convenience function to just parse a string.
165 * @fc: Filesystem context.
166 * @key: Parameter name.
167 * @value: Default value.
168 */
vfs_parse_fs_qstr(struct fs_context * fc,const char * key,const struct qstr * value)169 int vfs_parse_fs_qstr(struct fs_context *fc, const char *key,
170 const struct qstr *value)
171 {
172 int ret;
173
174 struct fs_parameter param = {
175 .key = key,
176 .type = fs_value_is_flag,
177 .size = value ? value->len : 0,
178 };
179
180 if (value) {
181 param.string = kmemdup_nul(value->name, value->len, GFP_KERNEL);
182 if (!param.string)
183 return -ENOMEM;
184 param.type = fs_value_is_string;
185 }
186
187 ret = vfs_parse_fs_param(fc, ¶m);
188 kfree(param.string);
189 return ret;
190 }
191 EXPORT_SYMBOL(vfs_parse_fs_qstr);
192
193 /**
194 * vfs_parse_monolithic_sep - Parse key[=val][,key[=val]]* mount data
195 * @fc: The superblock configuration to fill in.
196 * @data: The data to parse
197 * @sep: callback for separating next option
198 *
199 * Parse a blob of data that's in key[=val][,key[=val]]* form with a custom
200 * option separator callback.
201 *
202 * Returns 0 on success or the error returned by the ->parse_option() fs_context
203 * operation on failure.
204 */
vfs_parse_monolithic_sep(struct fs_context * fc,void * data,char * (* sep)(char **))205 int vfs_parse_monolithic_sep(struct fs_context *fc, void *data,
206 char *(*sep)(char **))
207 {
208 char *options = data, *key;
209 int ret = 0;
210
211 if (!options)
212 return 0;
213
214 ret = security_sb_eat_lsm_opts(options, &fc->security);
215 if (ret)
216 return ret;
217
218 while ((key = sep(&options)) != NULL) {
219 if (*key) {
220 char *value = strchr(key, '=');
221
222 if (value) {
223 if (unlikely(value == key))
224 continue;
225 *value++ = 0;
226 }
227 ret = vfs_parse_fs_string(fc, key, value);
228 if (ret < 0)
229 break;
230 }
231 }
232
233 return ret;
234 }
235 EXPORT_SYMBOL(vfs_parse_monolithic_sep);
236
vfs_parse_comma_sep(char ** s)237 static char *vfs_parse_comma_sep(char **s)
238 {
239 return strsep(s, ",");
240 }
241
242 /**
243 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
244 * @fc: The superblock configuration to fill in.
245 * @data: The data to parse
246 *
247 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
248 * called from the ->monolithic_mount_data() fs_context operation.
249 *
250 * Returns 0 on success or the error returned by the ->parse_option() fs_context
251 * operation on failure.
252 */
generic_parse_monolithic(struct fs_context * fc,void * data)253 int generic_parse_monolithic(struct fs_context *fc, void *data)
254 {
255 return vfs_parse_monolithic_sep(fc, data, vfs_parse_comma_sep);
256 }
257 EXPORT_SYMBOL(generic_parse_monolithic);
258
259 /**
260 * alloc_fs_context - Create a filesystem context.
261 * @fs_type: The filesystem type.
262 * @reference: The dentry from which this one derives (or NULL)
263 * @sb_flags: Filesystem/superblock flags (SB_*)
264 * @sb_flags_mask: Applicable members of @sb_flags
265 * @purpose: The purpose that this configuration shall be used for.
266 *
267 * Open a filesystem and create a mount context. The mount context is
268 * initialised with the supplied flags and, if a submount/automount from
269 * another superblock (referred to by @reference) is supplied, may have
270 * parameters such as namespaces copied across from that superblock.
271 */
alloc_fs_context(struct file_system_type * fs_type,struct dentry * reference,unsigned int sb_flags,unsigned int sb_flags_mask,enum fs_context_purpose purpose)272 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
273 struct dentry *reference,
274 unsigned int sb_flags,
275 unsigned int sb_flags_mask,
276 enum fs_context_purpose purpose)
277 {
278 int (*init_fs_context)(struct fs_context *);
279 struct fs_context *fc;
280 int ret = -ENOMEM;
281
282 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT);
283 if (!fc)
284 return ERR_PTR(-ENOMEM);
285
286 fc->purpose = purpose;
287 fc->sb_flags = sb_flags;
288 fc->sb_flags_mask = sb_flags_mask;
289 fc->fs_type = get_filesystem(fs_type);
290 fc->cred = get_current_cred();
291 fc->net_ns = get_net(current->nsproxy->net_ns);
292 fc->log.prefix = fs_type->name;
293
294 mutex_init(&fc->uapi_mutex);
295
296 switch (purpose) {
297 case FS_CONTEXT_FOR_MOUNT:
298 fc->user_ns = get_user_ns(fc->cred->user_ns);
299 break;
300 case FS_CONTEXT_FOR_SUBMOUNT:
301 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
302 break;
303 case FS_CONTEXT_FOR_RECONFIGURE:
304 atomic_inc(&reference->d_sb->s_active);
305 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
306 fc->root = dget(reference);
307 break;
308 }
309
310 /* TODO: Make all filesystems support this unconditionally */
311 init_fs_context = fc->fs_type->init_fs_context;
312 if (!init_fs_context)
313 init_fs_context = legacy_init_fs_context;
314
315 ret = init_fs_context(fc);
316 if (ret < 0)
317 goto err_fc;
318 fc->need_free = true;
319 return fc;
320
321 err_fc:
322 put_fs_context(fc);
323 return ERR_PTR(ret);
324 }
325
fs_context_for_mount(struct file_system_type * fs_type,unsigned int sb_flags)326 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
327 unsigned int sb_flags)
328 {
329 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
330 FS_CONTEXT_FOR_MOUNT);
331 }
332 EXPORT_SYMBOL(fs_context_for_mount);
333
fs_context_for_reconfigure(struct dentry * dentry,unsigned int sb_flags,unsigned int sb_flags_mask)334 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
335 unsigned int sb_flags,
336 unsigned int sb_flags_mask)
337 {
338 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
339 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
340 }
341 EXPORT_SYMBOL(fs_context_for_reconfigure);
342
343 /**
344 * fs_context_for_submount: allocate a new fs_context for a submount
345 * @type: file_system_type of the new context
346 * @reference: reference dentry from which to copy relevant info
347 *
348 * Allocate a new fs_context suitable for a submount. This also ensures that
349 * the fc->security object is inherited from @reference (if needed).
350 */
fs_context_for_submount(struct file_system_type * type,struct dentry * reference)351 struct fs_context *fs_context_for_submount(struct file_system_type *type,
352 struct dentry *reference)
353 {
354 struct fs_context *fc;
355 int ret;
356
357 fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
358 if (IS_ERR(fc))
359 return fc;
360
361 ret = security_fs_context_submount(fc, reference->d_sb);
362 if (ret) {
363 put_fs_context(fc);
364 return ERR_PTR(ret);
365 }
366
367 return fc;
368 }
369 EXPORT_SYMBOL(fs_context_for_submount);
370
fc_drop_locked(struct fs_context * fc)371 void fc_drop_locked(struct fs_context *fc)
372 {
373 struct super_block *sb = fc->root->d_sb;
374 dput(fc->root);
375 fc->root = NULL;
376 deactivate_locked_super(sb);
377 }
378
379 static void legacy_fs_context_free(struct fs_context *fc);
380
381 /**
382 * vfs_dup_fs_context - Duplicate a filesystem context.
383 * @src_fc: The context to copy.
384 */
vfs_dup_fs_context(struct fs_context * src_fc)385 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
386 {
387 struct fs_context *fc;
388 int ret;
389
390 if (!src_fc->ops->dup)
391 return ERR_PTR(-EOPNOTSUPP);
392
393 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
394 if (!fc)
395 return ERR_PTR(-ENOMEM);
396
397 mutex_init(&fc->uapi_mutex);
398
399 fc->fs_private = NULL;
400 fc->s_fs_info = NULL;
401 fc->source = NULL;
402 fc->security = NULL;
403 get_filesystem(fc->fs_type);
404 get_net(fc->net_ns);
405 get_user_ns(fc->user_ns);
406 get_cred(fc->cred);
407 if (fc->log.log)
408 refcount_inc(&fc->log.log->usage);
409
410 /* Can't call put until we've called ->dup */
411 ret = fc->ops->dup(fc, src_fc);
412 if (ret < 0)
413 goto err_fc;
414
415 ret = security_fs_context_dup(fc, src_fc);
416 if (ret < 0)
417 goto err_fc;
418 return fc;
419
420 err_fc:
421 put_fs_context(fc);
422 return ERR_PTR(ret);
423 }
424 EXPORT_SYMBOL(vfs_dup_fs_context);
425
426 /**
427 * logfc - Log a message to a filesystem context
428 * @log: The filesystem context to log to, or NULL to use printk.
429 * @prefix: A string to prefix the output with, or NULL.
430 * @level: 'w' for a warning, 'e' for an error. Anything else is a notice.
431 * @fmt: The format of the buffer.
432 */
logfc(struct fc_log * log,const char * prefix,char level,const char * fmt,...)433 void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
434 {
435 va_list va;
436 struct va_format vaf = {.fmt = fmt, .va = &va};
437
438 va_start(va, fmt);
439 if (!log) {
440 switch (level) {
441 case 'w':
442 printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
443 prefix ? ": " : "", &vaf);
444 break;
445 case 'e':
446 printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
447 prefix ? ": " : "", &vaf);
448 break;
449 case 'i':
450 printk(KERN_INFO "%s%s%pV\n", prefix ? prefix : "",
451 prefix ? ": " : "", &vaf);
452 break;
453 default:
454 printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
455 prefix ? ": " : "", &vaf);
456 break;
457 }
458 } else {
459 unsigned int logsize = ARRAY_SIZE(log->buffer);
460 u8 index;
461 char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
462 prefix ? prefix : "",
463 prefix ? ": " : "", &vaf);
464
465 index = log->head & (logsize - 1);
466 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
467 sizeof(log->tail) != sizeof(u8));
468 if ((u8)(log->head - log->tail) == logsize) {
469 /* The buffer is full, discard the oldest message */
470 if (log->need_free & (1 << index))
471 kfree(log->buffer[index]);
472 log->tail++;
473 }
474
475 log->buffer[index] = q ? q : "OOM: Can't store error string";
476 if (q)
477 log->need_free |= 1 << index;
478 else
479 log->need_free &= ~(1 << index);
480 log->head++;
481 }
482 va_end(va);
483 }
484 EXPORT_SYMBOL(logfc);
485
486 /*
487 * Free a logging structure.
488 */
put_fc_log(struct fs_context * fc)489 static void put_fc_log(struct fs_context *fc)
490 {
491 struct fc_log *log = fc->log.log;
492 int i;
493
494 if (log) {
495 if (refcount_dec_and_test(&log->usage)) {
496 fc->log.log = NULL;
497 for (i = 0; i < ARRAY_SIZE(log->buffer) ; i++)
498 if (log->need_free & (1 << i))
499 kfree(log->buffer[i]);
500 kfree(log);
501 }
502 }
503 }
504
505 /**
506 * put_fs_context - Dispose of a superblock configuration context.
507 * @fc: The context to dispose of.
508 */
put_fs_context(struct fs_context * fc)509 void put_fs_context(struct fs_context *fc)
510 {
511 struct super_block *sb;
512
513 if (fc->root) {
514 sb = fc->root->d_sb;
515 dput(fc->root);
516 fc->root = NULL;
517 deactivate_super(sb);
518 }
519
520 if (fc->need_free && fc->ops && fc->ops->free)
521 fc->ops->free(fc);
522
523 security_free_mnt_opts(&fc->security);
524 put_net(fc->net_ns);
525 put_user_ns(fc->user_ns);
526 put_cred(fc->cred);
527 put_fc_log(fc);
528 put_filesystem(fc->fs_type);
529 kfree(fc->source);
530 kfree(fc);
531 }
532 EXPORT_SYMBOL(put_fs_context);
533
534 /*
535 * Free the config for a filesystem that doesn't support fs_context.
536 */
legacy_fs_context_free(struct fs_context * fc)537 static void legacy_fs_context_free(struct fs_context *fc)
538 {
539 struct legacy_fs_context *ctx = fc->fs_private;
540
541 if (ctx) {
542 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
543 kfree(ctx->legacy_data);
544 kfree(ctx);
545 }
546 }
547
548 /*
549 * Duplicate a legacy config.
550 */
legacy_fs_context_dup(struct fs_context * fc,struct fs_context * src_fc)551 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
552 {
553 struct legacy_fs_context *ctx;
554 struct legacy_fs_context *src_ctx = src_fc->fs_private;
555
556 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
557 if (!ctx)
558 return -ENOMEM;
559
560 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
561 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
562 src_ctx->data_size, GFP_KERNEL);
563 if (!ctx->legacy_data) {
564 kfree(ctx);
565 return -ENOMEM;
566 }
567 }
568
569 fc->fs_private = ctx;
570 return 0;
571 }
572
573 /*
574 * Add a parameter to a legacy config. We build up a comma-separated list of
575 * options.
576 */
legacy_parse_param(struct fs_context * fc,struct fs_parameter * param)577 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
578 {
579 struct legacy_fs_context *ctx = fc->fs_private;
580 unsigned int size = ctx->data_size;
581 size_t len = 0;
582 int ret;
583
584 ret = vfs_parse_fs_param_source(fc, param);
585 if (ret != -ENOPARAM)
586 return ret;
587
588 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
589 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
590
591 switch (param->type) {
592 case fs_value_is_string:
593 len = 1 + param->size;
594 fallthrough;
595 case fs_value_is_flag:
596 len += strlen(param->key);
597 break;
598 default:
599 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
600 param->key);
601 }
602
603 if (size + len + 2 > PAGE_SIZE)
604 return invalf(fc, "VFS: Legacy: Cumulative options too large");
605 if (strchr(param->key, ',') ||
606 (param->type == fs_value_is_string &&
607 memchr(param->string, ',', param->size)))
608 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
609 param->key);
610 if (!ctx->legacy_data) {
611 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
612 if (!ctx->legacy_data)
613 return -ENOMEM;
614 }
615
616 if (size)
617 ctx->legacy_data[size++] = ',';
618 len = strlen(param->key);
619 memcpy(ctx->legacy_data + size, param->key, len);
620 size += len;
621 if (param->type == fs_value_is_string) {
622 ctx->legacy_data[size++] = '=';
623 memcpy(ctx->legacy_data + size, param->string, param->size);
624 size += param->size;
625 }
626 ctx->legacy_data[size] = '\0';
627 ctx->data_size = size;
628 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
629 return 0;
630 }
631
632 /*
633 * Add monolithic mount data.
634 */
legacy_parse_monolithic(struct fs_context * fc,void * data)635 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
636 {
637 struct legacy_fs_context *ctx = fc->fs_private;
638
639 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
640 pr_warn("VFS: Can't mix monolithic and individual options\n");
641 return -EINVAL;
642 }
643
644 ctx->legacy_data = data;
645 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
646 if (!ctx->legacy_data)
647 return 0;
648
649 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
650 return 0;
651 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
652 }
653
654 /*
655 * Get a mountable root with the legacy mount command.
656 */
legacy_get_tree(struct fs_context * fc)657 static int legacy_get_tree(struct fs_context *fc)
658 {
659 struct legacy_fs_context *ctx = fc->fs_private;
660 struct super_block *sb;
661 struct dentry *root;
662
663 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
664 fc->source, ctx->legacy_data);
665 if (IS_ERR(root))
666 return PTR_ERR(root);
667
668 sb = root->d_sb;
669 BUG_ON(!sb);
670
671 fc->root = root;
672 return 0;
673 }
674
675 /*
676 * Handle remount.
677 */
legacy_reconfigure(struct fs_context * fc)678 static int legacy_reconfigure(struct fs_context *fc)
679 {
680 struct legacy_fs_context *ctx = fc->fs_private;
681 struct super_block *sb = fc->root->d_sb;
682
683 if (!sb->s_op->remount_fs)
684 return 0;
685
686 return sb->s_op->remount_fs(sb, &fc->sb_flags,
687 ctx ? ctx->legacy_data : NULL);
688 }
689
690 const struct fs_context_operations legacy_fs_context_ops = {
691 .free = legacy_fs_context_free,
692 .dup = legacy_fs_context_dup,
693 .parse_param = legacy_parse_param,
694 .parse_monolithic = legacy_parse_monolithic,
695 .get_tree = legacy_get_tree,
696 .reconfigure = legacy_reconfigure,
697 };
698
699 /*
700 * Initialise a legacy context for a filesystem that doesn't support
701 * fs_context.
702 */
legacy_init_fs_context(struct fs_context * fc)703 static int legacy_init_fs_context(struct fs_context *fc)
704 {
705 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT);
706 if (!fc->fs_private)
707 return -ENOMEM;
708 fc->ops = &legacy_fs_context_ops;
709 return 0;
710 }
711
parse_monolithic_mount_data(struct fs_context * fc,void * data)712 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
713 {
714 int (*monolithic_mount_data)(struct fs_context *, void *);
715
716 monolithic_mount_data = fc->ops->parse_monolithic;
717 if (!monolithic_mount_data)
718 monolithic_mount_data = generic_parse_monolithic;
719
720 return monolithic_mount_data(fc, data);
721 }
722
723 /*
724 * Clean up a context after performing an action on it and put it into a state
725 * from where it can be used to reconfigure a superblock.
726 *
727 * Note that here we do only the parts that can't fail; the rest is in
728 * finish_clean_context() below and in between those fs_context is marked
729 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
730 * successful mount or remount we need to report success to userland.
731 * Trying to do full reinit (for the sake of possible subsequent remount)
732 * and failing to allocate memory would've put us into a nasty situation.
733 * So here we only discard the old state and reinitialization is left
734 * until we actually try to reconfigure.
735 */
vfs_clean_context(struct fs_context * fc)736 void vfs_clean_context(struct fs_context *fc)
737 {
738 if (fc->need_free && fc->ops && fc->ops->free)
739 fc->ops->free(fc);
740 fc->need_free = false;
741 fc->fs_private = NULL;
742 fc->s_fs_info = NULL;
743 fc->sb_flags = 0;
744 security_free_mnt_opts(&fc->security);
745 kfree(fc->source);
746 fc->source = NULL;
747 fc->exclusive = false;
748
749 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
750 fc->phase = FS_CONTEXT_AWAITING_RECONF;
751 }
752
finish_clean_context(struct fs_context * fc)753 int finish_clean_context(struct fs_context *fc)
754 {
755 int error;
756
757 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
758 return 0;
759
760 if (fc->fs_type->init_fs_context)
761 error = fc->fs_type->init_fs_context(fc);
762 else
763 error = legacy_init_fs_context(fc);
764 if (unlikely(error)) {
765 fc->phase = FS_CONTEXT_FAILED;
766 return error;
767 }
768 fc->need_free = true;
769 fc->phase = FS_CONTEXT_RECONF_PARAMS;
770 return 0;
771 }
772