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