xref: /linux/fs/fs_context.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29bc61ab1SDavid Howells /* Provide a way to create a superblock configuration context within the kernel
39bc61ab1SDavid Howells  * that allows a superblock to be set up prior to mounting.
49bc61ab1SDavid Howells  *
59bc61ab1SDavid Howells  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
69bc61ab1SDavid Howells  * Written by David Howells (dhowells@redhat.com)
79bc61ab1SDavid Howells  */
89bc61ab1SDavid Howells 
99bc61ab1SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10007ec26cSDavid Howells #include <linux/module.h>
119bc61ab1SDavid Howells #include <linux/fs_context.h>
123e1aeb00SDavid Howells #include <linux/fs_parser.h>
139bc61ab1SDavid Howells #include <linux/fs.h>
149bc61ab1SDavid Howells #include <linux/mount.h>
159bc61ab1SDavid Howells #include <linux/nsproxy.h>
169bc61ab1SDavid Howells #include <linux/slab.h>
179bc61ab1SDavid Howells #include <linux/magic.h>
189bc61ab1SDavid Howells #include <linux/security.h>
199bc61ab1SDavid Howells #include <linux/mnt_namespace.h>
209bc61ab1SDavid Howells #include <linux/pid_namespace.h>
219bc61ab1SDavid Howells #include <linux/user_namespace.h>
229bc61ab1SDavid Howells #include <net/net_namespace.h>
23007ec26cSDavid Howells #include <asm/sections.h>
249bc61ab1SDavid Howells #include "mount.h"
259bc61ab1SDavid Howells #include "internal.h"
269bc61ab1SDavid Howells 
273e1aeb00SDavid Howells enum legacy_fs_param {
283e1aeb00SDavid Howells 	LEGACY_FS_UNSET_PARAMS,
293e1aeb00SDavid Howells 	LEGACY_FS_MONOLITHIC_PARAMS,
303e1aeb00SDavid Howells 	LEGACY_FS_INDIVIDUAL_PARAMS,
313e1aeb00SDavid Howells };
323e1aeb00SDavid Howells 
339bc61ab1SDavid Howells struct legacy_fs_context {
349bc61ab1SDavid Howells 	char			*legacy_data;	/* Data page for legacy filesystems */
359bc61ab1SDavid Howells 	size_t			data_size;
363e1aeb00SDavid Howells 	enum legacy_fs_param	param_type;
379bc61ab1SDavid Howells };
389bc61ab1SDavid Howells 
399bc61ab1SDavid Howells static int legacy_init_fs_context(struct fs_context *fc);
409bc61ab1SDavid Howells 
413e1aeb00SDavid Howells static const struct constant_table common_set_sb_flag[] = {
423e1aeb00SDavid Howells 	{ "dirsync",	SB_DIRSYNC },
433e1aeb00SDavid Howells 	{ "lazytime",	SB_LAZYTIME },
443e1aeb00SDavid Howells 	{ "mand",	SB_MANDLOCK },
453e1aeb00SDavid Howells 	{ "ro",		SB_RDONLY },
463e1aeb00SDavid Howells 	{ "sync",	SB_SYNCHRONOUS },
4734264ae3SAl Viro 	{ },
483e1aeb00SDavid Howells };
493e1aeb00SDavid Howells 
503e1aeb00SDavid Howells static const struct constant_table common_clear_sb_flag[] = {
513e1aeb00SDavid Howells 	{ "async",	SB_SYNCHRONOUS },
523e1aeb00SDavid Howells 	{ "nolazytime",	SB_LAZYTIME },
533e1aeb00SDavid Howells 	{ "nomand",	SB_MANDLOCK },
543e1aeb00SDavid Howells 	{ "rw",		SB_RDONLY },
5534264ae3SAl Viro 	{ },
563e1aeb00SDavid Howells };
573e1aeb00SDavid Howells 
583e1aeb00SDavid Howells /*
593e1aeb00SDavid Howells  * Check for a common mount option that manipulates s_flags.
603e1aeb00SDavid Howells  */
vfs_parse_sb_flag(struct fs_context * fc,const char * key)613e1aeb00SDavid Howells static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
623e1aeb00SDavid Howells {
633e1aeb00SDavid Howells 	unsigned int token;
643e1aeb00SDavid Howells 
653e1aeb00SDavid Howells 	token = lookup_constant(common_set_sb_flag, key, 0);
663e1aeb00SDavid Howells 	if (token) {
673e1aeb00SDavid Howells 		fc->sb_flags |= token;
683e1aeb00SDavid Howells 		fc->sb_flags_mask |= token;
693e1aeb00SDavid Howells 		return 0;
703e1aeb00SDavid Howells 	}
713e1aeb00SDavid Howells 
723e1aeb00SDavid Howells 	token = lookup_constant(common_clear_sb_flag, key, 0);
733e1aeb00SDavid Howells 	if (token) {
743e1aeb00SDavid Howells 		fc->sb_flags &= ~token;
753e1aeb00SDavid Howells 		fc->sb_flags_mask |= token;
763e1aeb00SDavid Howells 		return 0;
773e1aeb00SDavid Howells 	}
783e1aeb00SDavid Howells 
793e1aeb00SDavid Howells 	return -ENOPARAM;
803e1aeb00SDavid Howells }
813e1aeb00SDavid Howells 
823e1aeb00SDavid Howells /**
83d1d488d8SChristian Brauner  * vfs_parse_fs_param_source - Handle setting "source" via parameter
84d1d488d8SChristian Brauner  * @fc: The filesystem context to modify
85d1d488d8SChristian Brauner  * @param: The parameter
86d1d488d8SChristian Brauner  *
87d1d488d8SChristian Brauner  * This is a simple helper for filesystems to verify that the "source" they
88d1d488d8SChristian Brauner  * accept is sane.
89d1d488d8SChristian Brauner  *
90d1d488d8SChristian Brauner  * Returns 0 on success, -ENOPARAM if this is not  "source" parameter, and
91d1d488d8SChristian Brauner  * -EINVAL otherwise. In the event of failure, supplementary error information
92d1d488d8SChristian Brauner  *  is logged.
93d1d488d8SChristian Brauner  */
vfs_parse_fs_param_source(struct fs_context * fc,struct fs_parameter * param)94d1d488d8SChristian Brauner int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
95d1d488d8SChristian Brauner {
96d1d488d8SChristian Brauner 	if (strcmp(param->key, "source") != 0)
97d1d488d8SChristian Brauner 		return -ENOPARAM;
98d1d488d8SChristian Brauner 
99d1d488d8SChristian Brauner 	if (param->type != fs_value_is_string)
100d1d488d8SChristian Brauner 		return invalf(fc, "Non-string source");
101d1d488d8SChristian Brauner 
102d1d488d8SChristian Brauner 	if (fc->source)
103d1d488d8SChristian Brauner 		return invalf(fc, "Multiple sources");
104d1d488d8SChristian Brauner 
105d1d488d8SChristian Brauner 	fc->source = param->string;
106d1d488d8SChristian Brauner 	param->string = NULL;
107d1d488d8SChristian Brauner 	return 0;
108d1d488d8SChristian Brauner }
109d1d488d8SChristian Brauner EXPORT_SYMBOL(vfs_parse_fs_param_source);
110d1d488d8SChristian Brauner 
111d1d488d8SChristian Brauner /**
1123e1aeb00SDavid Howells  * vfs_parse_fs_param - Add a single parameter to a superblock config
1133e1aeb00SDavid Howells  * @fc: The filesystem context to modify
1143e1aeb00SDavid Howells  * @param: The parameter
1153e1aeb00SDavid Howells  *
1163e1aeb00SDavid Howells  * A single mount option in string form is applied to the filesystem context
1173e1aeb00SDavid Howells  * being set up.  Certain standard options (for example "ro") are translated
1183e1aeb00SDavid Howells  * into flag bits without going to the filesystem.  The active security module
1193e1aeb00SDavid Howells  * is allowed to observe and poach options.  Any other options are passed over
1203e1aeb00SDavid Howells  * to the filesystem to parse.
1213e1aeb00SDavid Howells  *
1223e1aeb00SDavid Howells  * This may be called multiple times for a context.
1233e1aeb00SDavid Howells  *
1243e1aeb00SDavid Howells  * Returns 0 on success and a negative error code on failure.  In the event of
1253e1aeb00SDavid Howells  * failure, supplementary error information may have been set.
1263e1aeb00SDavid Howells  */
vfs_parse_fs_param(struct fs_context * fc,struct fs_parameter * param)1273e1aeb00SDavid Howells int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
1283e1aeb00SDavid Howells {
1293e1aeb00SDavid Howells 	int ret;
1303e1aeb00SDavid Howells 
1313e1aeb00SDavid Howells 	if (!param->key)
1323e1aeb00SDavid Howells 		return invalf(fc, "Unnamed parameter\n");
1333e1aeb00SDavid Howells 
1343e1aeb00SDavid Howells 	ret = vfs_parse_sb_flag(fc, param->key);
1353e1aeb00SDavid Howells 	if (ret != -ENOPARAM)
1363e1aeb00SDavid Howells 		return ret;
1373e1aeb00SDavid Howells 
1383e1aeb00SDavid Howells 	ret = security_fs_context_parse_param(fc, param);
1393e1aeb00SDavid Howells 	if (ret != -ENOPARAM)
1403e1aeb00SDavid Howells 		/* Param belongs to the LSM or is disallowed by the LSM; so
1413e1aeb00SDavid Howells 		 * don't pass to the FS.
1423e1aeb00SDavid Howells 		 */
1433e1aeb00SDavid Howells 		return ret;
1443e1aeb00SDavid Howells 
1453e1aeb00SDavid Howells 	if (fc->ops->parse_param) {
1463e1aeb00SDavid Howells 		ret = fc->ops->parse_param(fc, param);
1473e1aeb00SDavid Howells 		if (ret != -ENOPARAM)
1483e1aeb00SDavid Howells 			return ret;
1493e1aeb00SDavid Howells 	}
1503e1aeb00SDavid Howells 
1513e1aeb00SDavid Howells 	/* If the filesystem doesn't take any arguments, give it the
1523e1aeb00SDavid Howells 	 * default handling of source.
1533e1aeb00SDavid Howells 	 */
154d1d488d8SChristian Brauner 	ret = vfs_parse_fs_param_source(fc, param);
155d1d488d8SChristian Brauner 	if (ret != -ENOPARAM)
156d1d488d8SChristian Brauner 		return ret;
1573e1aeb00SDavid Howells 
1583e1aeb00SDavid Howells 	return invalf(fc, "%s: Unknown parameter '%s'",
1593e1aeb00SDavid Howells 		      fc->fs_type->name, param->key);
1603e1aeb00SDavid Howells }
1613e1aeb00SDavid Howells EXPORT_SYMBOL(vfs_parse_fs_param);
1623e1aeb00SDavid Howells 
1633e1aeb00SDavid Howells /**
1643e1aeb00SDavid Howells  * vfs_parse_fs_string - Convenience function to just parse a string.
16535931eb3SMatthew Wilcox (Oracle)  * @fc: Filesystem context.
16635931eb3SMatthew Wilcox (Oracle)  * @key: Parameter name.
16735931eb3SMatthew Wilcox (Oracle)  * @value: Default value.
16835931eb3SMatthew Wilcox (Oracle)  * @v_size: Maximum number of bytes in the value.
1693e1aeb00SDavid Howells  */
vfs_parse_fs_string(struct fs_context * fc,const char * key,const char * value,size_t v_size)1703e1aeb00SDavid Howells int vfs_parse_fs_string(struct fs_context *fc, const char *key,
1713e1aeb00SDavid Howells 			const char *value, size_t v_size)
1723e1aeb00SDavid Howells {
1733e1aeb00SDavid Howells 	int ret;
1743e1aeb00SDavid Howells 
1753e1aeb00SDavid Howells 	struct fs_parameter param = {
1763e1aeb00SDavid Howells 		.key	= key,
1770f89589aSAl Viro 		.type	= fs_value_is_flag,
1783e1aeb00SDavid Howells 		.size	= v_size,
1793e1aeb00SDavid Howells 	};
1803e1aeb00SDavid Howells 
1810f89589aSAl Viro 	if (value) {
1823e1aeb00SDavid Howells 		param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
1833e1aeb00SDavid Howells 		if (!param.string)
1843e1aeb00SDavid Howells 			return -ENOMEM;
1850f89589aSAl Viro 		param.type = fs_value_is_string;
1863e1aeb00SDavid Howells 	}
1873e1aeb00SDavid Howells 
1883e1aeb00SDavid Howells 	ret = vfs_parse_fs_param(fc, &param);
1893e1aeb00SDavid Howells 	kfree(param.string);
1903e1aeb00SDavid Howells 	return ret;
1913e1aeb00SDavid Howells }
1923e1aeb00SDavid Howells EXPORT_SYMBOL(vfs_parse_fs_string);
1933e1aeb00SDavid Howells 
1943e1aeb00SDavid Howells /**
195*e001d144SAmir Goldstein  * vfs_parse_monolithic_sep - Parse key[=val][,key[=val]]* mount data
19635931eb3SMatthew Wilcox (Oracle)  * @fc: The superblock configuration to fill in.
1973e1aeb00SDavid Howells  * @data: The data to parse
198*e001d144SAmir Goldstein  * @sep: callback for separating next option
1993e1aeb00SDavid Howells  *
200*e001d144SAmir Goldstein  * Parse a blob of data that's in key[=val][,key[=val]]* form with a custom
201*e001d144SAmir Goldstein  * option separator callback.
2023e1aeb00SDavid Howells  *
2033e1aeb00SDavid Howells  * Returns 0 on success or the error returned by the ->parse_option() fs_context
2043e1aeb00SDavid Howells  * operation on failure.
2053e1aeb00SDavid Howells  */
vfs_parse_monolithic_sep(struct fs_context * fc,void * data,char * (* sep)(char **))206*e001d144SAmir Goldstein int vfs_parse_monolithic_sep(struct fs_context *fc, void *data,
207*e001d144SAmir Goldstein 			     char *(*sep)(char **))
2083e1aeb00SDavid Howells {
2093e1aeb00SDavid Howells 	char *options = data, *key;
2103e1aeb00SDavid Howells 	int ret = 0;
2113e1aeb00SDavid Howells 
2123e1aeb00SDavid Howells 	if (!options)
2133e1aeb00SDavid Howells 		return 0;
2143e1aeb00SDavid Howells 
2153e1aeb00SDavid Howells 	ret = security_sb_eat_lsm_opts(options, &fc->security);
2163e1aeb00SDavid Howells 	if (ret)
2173e1aeb00SDavid Howells 		return ret;
2183e1aeb00SDavid Howells 
219*e001d144SAmir Goldstein 	while ((key = sep(&options)) != NULL) {
2203e1aeb00SDavid Howells 		if (*key) {
2213e1aeb00SDavid Howells 			size_t v_len = 0;
2223e1aeb00SDavid Howells 			char *value = strchr(key, '=');
2233e1aeb00SDavid Howells 
2243e1aeb00SDavid Howells 			if (value) {
2253e1aeb00SDavid Howells 				if (value == key)
2263e1aeb00SDavid Howells 					continue;
2273e1aeb00SDavid Howells 				*value++ = 0;
2283e1aeb00SDavid Howells 				v_len = strlen(value);
2293e1aeb00SDavid Howells 			}
2303e1aeb00SDavid Howells 			ret = vfs_parse_fs_string(fc, key, value, v_len);
2313e1aeb00SDavid Howells 			if (ret < 0)
2323e1aeb00SDavid Howells 				break;
2333e1aeb00SDavid Howells 		}
2343e1aeb00SDavid Howells 	}
2353e1aeb00SDavid Howells 
2363e1aeb00SDavid Howells 	return ret;
2373e1aeb00SDavid Howells }
238*e001d144SAmir Goldstein EXPORT_SYMBOL(vfs_parse_monolithic_sep);
239*e001d144SAmir Goldstein 
vfs_parse_comma_sep(char ** s)240*e001d144SAmir Goldstein static char *vfs_parse_comma_sep(char **s)
241*e001d144SAmir Goldstein {
242*e001d144SAmir Goldstein 	return strsep(s, ",");
243*e001d144SAmir Goldstein }
244*e001d144SAmir Goldstein 
245*e001d144SAmir Goldstein /**
246*e001d144SAmir Goldstein  * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
247*e001d144SAmir Goldstein  * @fc: The superblock configuration to fill in.
248*e001d144SAmir Goldstein  * @data: The data to parse
249*e001d144SAmir Goldstein  *
250*e001d144SAmir Goldstein  * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
251*e001d144SAmir Goldstein  * called from the ->monolithic_mount_data() fs_context operation.
252*e001d144SAmir Goldstein  *
253*e001d144SAmir Goldstein  * Returns 0 on success or the error returned by the ->parse_option() fs_context
254*e001d144SAmir Goldstein  * operation on failure.
255*e001d144SAmir Goldstein  */
generic_parse_monolithic(struct fs_context * fc,void * data)256*e001d144SAmir Goldstein int generic_parse_monolithic(struct fs_context *fc, void *data)
257*e001d144SAmir Goldstein {
258*e001d144SAmir Goldstein 	return vfs_parse_monolithic_sep(fc, data, vfs_parse_comma_sep);
259*e001d144SAmir Goldstein }
2603e1aeb00SDavid Howells EXPORT_SYMBOL(generic_parse_monolithic);
2613e1aeb00SDavid Howells 
2629bc61ab1SDavid Howells /**
2639bc61ab1SDavid Howells  * alloc_fs_context - Create a filesystem context.
2649bc61ab1SDavid Howells  * @fs_type: The filesystem type.
2659bc61ab1SDavid Howells  * @reference: The dentry from which this one derives (or NULL)
2669bc61ab1SDavid Howells  * @sb_flags: Filesystem/superblock flags (SB_*)
2679bc61ab1SDavid Howells  * @sb_flags_mask: Applicable members of @sb_flags
2689bc61ab1SDavid Howells  * @purpose: The purpose that this configuration shall be used for.
2699bc61ab1SDavid Howells  *
2709bc61ab1SDavid Howells  * Open a filesystem and create a mount context.  The mount context is
2719bc61ab1SDavid Howells  * initialised with the supplied flags and, if a submount/automount from
2729bc61ab1SDavid Howells  * another superblock (referred to by @reference) is supplied, may have
2739bc61ab1SDavid Howells  * parameters such as namespaces copied across from that superblock.
2749bc61ab1SDavid Howells  */
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)2759bc61ab1SDavid Howells static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
2769bc61ab1SDavid Howells 				      struct dentry *reference,
2779bc61ab1SDavid Howells 				      unsigned int sb_flags,
2789bc61ab1SDavid Howells 				      unsigned int sb_flags_mask,
2799bc61ab1SDavid Howells 				      enum fs_context_purpose purpose)
2809bc61ab1SDavid Howells {
281f3a09c92SAl Viro 	int (*init_fs_context)(struct fs_context *);
2829bc61ab1SDavid Howells 	struct fs_context *fc;
2839bc61ab1SDavid Howells 	int ret = -ENOMEM;
2849bc61ab1SDavid Howells 
285bb902cb4SYutian Yang 	fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT);
2869bc61ab1SDavid Howells 	if (!fc)
2879bc61ab1SDavid Howells 		return ERR_PTR(-ENOMEM);
2889bc61ab1SDavid Howells 
2899bc61ab1SDavid Howells 	fc->purpose	= purpose;
2909bc61ab1SDavid Howells 	fc->sb_flags	= sb_flags;
2919bc61ab1SDavid Howells 	fc->sb_flags_mask = sb_flags_mask;
2929bc61ab1SDavid Howells 	fc->fs_type	= get_filesystem(fs_type);
2939bc61ab1SDavid Howells 	fc->cred	= get_current_cred();
2949bc61ab1SDavid Howells 	fc->net_ns	= get_net(current->nsproxy->net_ns);
295cc3c0b53SAl Viro 	fc->log.prefix	= fs_type->name;
2969bc61ab1SDavid Howells 
29724dcb3d9SDavid Howells 	mutex_init(&fc->uapi_mutex);
29824dcb3d9SDavid Howells 
2999bc61ab1SDavid Howells 	switch (purpose) {
3009bc61ab1SDavid Howells 	case FS_CONTEXT_FOR_MOUNT:
3019bc61ab1SDavid Howells 		fc->user_ns = get_user_ns(fc->cred->user_ns);
3029bc61ab1SDavid Howells 		break;
303e1a91586SAl Viro 	case FS_CONTEXT_FOR_SUBMOUNT:
304e1a91586SAl Viro 		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
305e1a91586SAl Viro 		break;
3068d0347f6SDavid Howells 	case FS_CONTEXT_FOR_RECONFIGURE:
3078d0347f6SDavid Howells 		atomic_inc(&reference->d_sb->s_active);
3081dd9bc08SEric Biggers 		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
3098d0347f6SDavid Howells 		fc->root = dget(reference);
3108d0347f6SDavid Howells 		break;
3119bc61ab1SDavid Howells 	}
3129bc61ab1SDavid Howells 
313f3a09c92SAl Viro 	/* TODO: Make all filesystems support this unconditionally */
314f3a09c92SAl Viro 	init_fs_context = fc->fs_type->init_fs_context;
315f3a09c92SAl Viro 	if (!init_fs_context)
316f3a09c92SAl Viro 		init_fs_context = legacy_init_fs_context;
317f3a09c92SAl Viro 
318f3a09c92SAl Viro 	ret = init_fs_context(fc);
3199bc61ab1SDavid Howells 	if (ret < 0)
3209bc61ab1SDavid Howells 		goto err_fc;
3219bc61ab1SDavid Howells 	fc->need_free = true;
3229bc61ab1SDavid Howells 	return fc;
3239bc61ab1SDavid Howells 
3249bc61ab1SDavid Howells err_fc:
3259bc61ab1SDavid Howells 	put_fs_context(fc);
3269bc61ab1SDavid Howells 	return ERR_PTR(ret);
3279bc61ab1SDavid Howells }
3289bc61ab1SDavid Howells 
fs_context_for_mount(struct file_system_type * fs_type,unsigned int sb_flags)3299bc61ab1SDavid Howells struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
3309bc61ab1SDavid Howells 					unsigned int sb_flags)
3319bc61ab1SDavid Howells {
3329bc61ab1SDavid Howells 	return alloc_fs_context(fs_type, NULL, sb_flags, 0,
3339bc61ab1SDavid Howells 					FS_CONTEXT_FOR_MOUNT);
3349bc61ab1SDavid Howells }
3359bc61ab1SDavid Howells EXPORT_SYMBOL(fs_context_for_mount);
3369bc61ab1SDavid Howells 
fs_context_for_reconfigure(struct dentry * dentry,unsigned int sb_flags,unsigned int sb_flags_mask)3378d0347f6SDavid Howells struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
3388d0347f6SDavid Howells 					unsigned int sb_flags,
3398d0347f6SDavid Howells 					unsigned int sb_flags_mask)
3408d0347f6SDavid Howells {
3418d0347f6SDavid Howells 	return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
3428d0347f6SDavid Howells 				sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
3438d0347f6SDavid Howells }
3448d0347f6SDavid Howells EXPORT_SYMBOL(fs_context_for_reconfigure);
3458d0347f6SDavid Howells 
346d80a8f1bSDavid Howells /**
347d80a8f1bSDavid Howells  * fs_context_for_submount: allocate a new fs_context for a submount
348d80a8f1bSDavid Howells  * @type: file_system_type of the new context
349d80a8f1bSDavid Howells  * @reference: reference dentry from which to copy relevant info
350d80a8f1bSDavid Howells  *
351d80a8f1bSDavid Howells  * Allocate a new fs_context suitable for a submount. This also ensures that
352d80a8f1bSDavid Howells  * the fc->security object is inherited from @reference (if needed).
353d80a8f1bSDavid Howells  */
fs_context_for_submount(struct file_system_type * type,struct dentry * reference)354e1a91586SAl Viro struct fs_context *fs_context_for_submount(struct file_system_type *type,
355e1a91586SAl Viro 					   struct dentry *reference)
356e1a91586SAl Viro {
357d80a8f1bSDavid Howells 	struct fs_context *fc;
358d80a8f1bSDavid Howells 	int ret;
359d80a8f1bSDavid Howells 
360d80a8f1bSDavid Howells 	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
361d80a8f1bSDavid Howells 	if (IS_ERR(fc))
362d80a8f1bSDavid Howells 		return fc;
363d80a8f1bSDavid Howells 
364d80a8f1bSDavid Howells 	ret = security_fs_context_submount(fc, reference->d_sb);
365d80a8f1bSDavid Howells 	if (ret) {
366d80a8f1bSDavid Howells 		put_fs_context(fc);
367d80a8f1bSDavid Howells 		return ERR_PTR(ret);
368d80a8f1bSDavid Howells 	}
369d80a8f1bSDavid Howells 
370d80a8f1bSDavid Howells 	return fc;
371e1a91586SAl Viro }
372e1a91586SAl Viro EXPORT_SYMBOL(fs_context_for_submount);
373e1a91586SAl Viro 
fc_drop_locked(struct fs_context * fc)374c9ce29edSAl Viro void fc_drop_locked(struct fs_context *fc)
375c9ce29edSAl Viro {
376c9ce29edSAl Viro 	struct super_block *sb = fc->root->d_sb;
377c9ce29edSAl Viro 	dput(fc->root);
378c9ce29edSAl Viro 	fc->root = NULL;
379c9ce29edSAl Viro 	deactivate_locked_super(sb);
380c9ce29edSAl Viro }
381c9ce29edSAl Viro 
3829bc61ab1SDavid Howells static void legacy_fs_context_free(struct fs_context *fc);
3838d0347f6SDavid Howells 
3849bc61ab1SDavid Howells /**
38535931eb3SMatthew Wilcox (Oracle)  * vfs_dup_fs_context - Duplicate a filesystem context.
3860b52075eSAl Viro  * @src_fc: The context to copy.
3870b52075eSAl Viro  */
vfs_dup_fs_context(struct fs_context * src_fc)3880b52075eSAl Viro struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
3890b52075eSAl Viro {
3900b52075eSAl Viro 	struct fs_context *fc;
3910b52075eSAl Viro 	int ret;
3920b52075eSAl Viro 
3930b52075eSAl Viro 	if (!src_fc->ops->dup)
3940b52075eSAl Viro 		return ERR_PTR(-EOPNOTSUPP);
3950b52075eSAl Viro 
3960b52075eSAl Viro 	fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
3970b52075eSAl Viro 	if (!fc)
3980b52075eSAl Viro 		return ERR_PTR(-ENOMEM);
3990b52075eSAl Viro 
40024dcb3d9SDavid Howells 	mutex_init(&fc->uapi_mutex);
40124dcb3d9SDavid Howells 
4020b52075eSAl Viro 	fc->fs_private	= NULL;
4030b52075eSAl Viro 	fc->s_fs_info	= NULL;
4040b52075eSAl Viro 	fc->source	= NULL;
4050b52075eSAl Viro 	fc->security	= NULL;
4060b52075eSAl Viro 	get_filesystem(fc->fs_type);
4070b52075eSAl Viro 	get_net(fc->net_ns);
4080b52075eSAl Viro 	get_user_ns(fc->user_ns);
4090b52075eSAl Viro 	get_cred(fc->cred);
410cc3c0b53SAl Viro 	if (fc->log.log)
411cc3c0b53SAl Viro 		refcount_inc(&fc->log.log->usage);
4120b52075eSAl Viro 
4130b52075eSAl Viro 	/* Can't call put until we've called ->dup */
4140b52075eSAl Viro 	ret = fc->ops->dup(fc, src_fc);
4150b52075eSAl Viro 	if (ret < 0)
4160b52075eSAl Viro 		goto err_fc;
4170b52075eSAl Viro 
4180b52075eSAl Viro 	ret = security_fs_context_dup(fc, src_fc);
4190b52075eSAl Viro 	if (ret < 0)
4200b52075eSAl Viro 		goto err_fc;
4210b52075eSAl Viro 	return fc;
4220b52075eSAl Viro 
4230b52075eSAl Viro err_fc:
4240b52075eSAl Viro 	put_fs_context(fc);
4250b52075eSAl Viro 	return ERR_PTR(ret);
4260b52075eSAl Viro }
4270b52075eSAl Viro EXPORT_SYMBOL(vfs_dup_fs_context);
4280b52075eSAl Viro 
429e7582e16SDavid Howells /**
430e7582e16SDavid Howells  * logfc - Log a message to a filesystem context
43135931eb3SMatthew Wilcox (Oracle)  * @log: The filesystem context to log to, or NULL to use printk.
43235931eb3SMatthew Wilcox (Oracle)  * @prefix: A string to prefix the output with, or NULL.
43335931eb3SMatthew Wilcox (Oracle)  * @level: 'w' for a warning, 'e' for an error.  Anything else is a notice.
434e7582e16SDavid Howells  * @fmt: The format of the buffer.
435e7582e16SDavid Howells  */
logfc(struct fc_log * log,const char * prefix,char level,const char * fmt,...)4369f09f649SAl Viro void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
437e7582e16SDavid Howells {
438e7582e16SDavid Howells 	va_list va;
4399f09f649SAl Viro 	struct va_format vaf = {.fmt = fmt, .va = &va};
440e7582e16SDavid Howells 
441e7582e16SDavid Howells 	va_start(va, fmt);
442007ec26cSDavid Howells 	if (!log) {
4439f09f649SAl Viro 		switch (level) {
444007ec26cSDavid Howells 		case 'w':
4459f09f649SAl Viro 			printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
4469f09f649SAl Viro 						prefix ? ": " : "", &vaf);
447007ec26cSDavid Howells 			break;
448007ec26cSDavid Howells 		case 'e':
4499f09f649SAl Viro 			printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
4509f09f649SAl Viro 						prefix ? ": " : "", &vaf);
451007ec26cSDavid Howells 			break;
452007ec26cSDavid Howells 		default:
4539f09f649SAl Viro 			printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
4549f09f649SAl Viro 						prefix ? ": " : "", &vaf);
455007ec26cSDavid Howells 			break;
456007ec26cSDavid Howells 		}
457007ec26cSDavid Howells 	} else {
458007ec26cSDavid Howells 		unsigned int logsize = ARRAY_SIZE(log->buffer);
459007ec26cSDavid Howells 		u8 index;
4609f09f649SAl Viro 		char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
4619f09f649SAl Viro 						prefix ? prefix : "",
4629f09f649SAl Viro 						prefix ? ": " : "", &vaf);
463007ec26cSDavid Howells 
464007ec26cSDavid Howells 		index = log->head & (logsize - 1);
465007ec26cSDavid Howells 		BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
466007ec26cSDavid Howells 			     sizeof(log->tail) != sizeof(u8));
467007ec26cSDavid Howells 		if ((u8)(log->head - log->tail) == logsize) {
468007ec26cSDavid Howells 			/* The buffer is full, discard the oldest message */
469007ec26cSDavid Howells 			if (log->need_free & (1 << index))
470007ec26cSDavid Howells 				kfree(log->buffer[index]);
471007ec26cSDavid Howells 			log->tail++;
472007ec26cSDavid Howells 		}
473007ec26cSDavid Howells 
4749f09f649SAl Viro 		log->buffer[index] = q ? q : "OOM: Can't store error string";
4759f09f649SAl Viro 		if (q)
4769f09f649SAl Viro 			log->need_free |= 1 << index;
4779f09f649SAl Viro 		else
478007ec26cSDavid Howells 			log->need_free &= ~(1 << index);
479007ec26cSDavid Howells 		log->head++;
480007ec26cSDavid Howells 	}
481e7582e16SDavid Howells 	va_end(va);
482e7582e16SDavid Howells }
483e7582e16SDavid Howells EXPORT_SYMBOL(logfc);
484007ec26cSDavid Howells 
485007ec26cSDavid Howells /*
486007ec26cSDavid Howells  * Free a logging structure.
487007ec26cSDavid Howells  */
put_fc_log(struct fs_context * fc)488007ec26cSDavid Howells static void put_fc_log(struct fs_context *fc)
489007ec26cSDavid Howells {
490cc3c0b53SAl Viro 	struct fc_log *log = fc->log.log;
491007ec26cSDavid Howells 	int i;
492007ec26cSDavid Howells 
493007ec26cSDavid Howells 	if (log) {
494007ec26cSDavid Howells 		if (refcount_dec_and_test(&log->usage)) {
495cc3c0b53SAl Viro 			fc->log.log = NULL;
496007ec26cSDavid Howells 			for (i = 0; i <= 7; i++)
497007ec26cSDavid Howells 				if (log->need_free & (1 << i))
498007ec26cSDavid Howells 					kfree(log->buffer[i]);
499007ec26cSDavid Howells 			kfree(log);
500007ec26cSDavid Howells 		}
501007ec26cSDavid Howells 	}
502007ec26cSDavid Howells }
503e7582e16SDavid Howells 
5040b52075eSAl Viro /**
5059bc61ab1SDavid Howells  * put_fs_context - Dispose of a superblock configuration context.
5069bc61ab1SDavid Howells  * @fc: The context to dispose of.
5079bc61ab1SDavid Howells  */
put_fs_context(struct fs_context * fc)5089bc61ab1SDavid Howells void put_fs_context(struct fs_context *fc)
5099bc61ab1SDavid Howells {
5109bc61ab1SDavid Howells 	struct super_block *sb;
5119bc61ab1SDavid Howells 
5129bc61ab1SDavid Howells 	if (fc->root) {
5139bc61ab1SDavid Howells 		sb = fc->root->d_sb;
5149bc61ab1SDavid Howells 		dput(fc->root);
5159bc61ab1SDavid Howells 		fc->root = NULL;
5169bc61ab1SDavid Howells 		deactivate_super(sb);
5179bc61ab1SDavid Howells 	}
5189bc61ab1SDavid Howells 
519f3a09c92SAl Viro 	if (fc->need_free && fc->ops && fc->ops->free)
520f3a09c92SAl Viro 		fc->ops->free(fc);
5219bc61ab1SDavid Howells 
5229bc61ab1SDavid Howells 	security_free_mnt_opts(&fc->security);
5239bc61ab1SDavid Howells 	put_net(fc->net_ns);
5249bc61ab1SDavid Howells 	put_user_ns(fc->user_ns);
5259bc61ab1SDavid Howells 	put_cred(fc->cred);
526007ec26cSDavid Howells 	put_fc_log(fc);
5279bc61ab1SDavid Howells 	put_filesystem(fc->fs_type);
5289bc61ab1SDavid Howells 	kfree(fc->source);
5299bc61ab1SDavid Howells 	kfree(fc);
5309bc61ab1SDavid Howells }
5319bc61ab1SDavid Howells EXPORT_SYMBOL(put_fs_context);
5329bc61ab1SDavid Howells 
5339bc61ab1SDavid Howells /*
5349bc61ab1SDavid Howells  * Free the config for a filesystem that doesn't support fs_context.
5359bc61ab1SDavid Howells  */
legacy_fs_context_free(struct fs_context * fc)5369bc61ab1SDavid Howells static void legacy_fs_context_free(struct fs_context *fc)
5379bc61ab1SDavid Howells {
5383e1aeb00SDavid Howells 	struct legacy_fs_context *ctx = fc->fs_private;
5393e1aeb00SDavid Howells 
5403e1aeb00SDavid Howells 	if (ctx) {
5413e1aeb00SDavid Howells 		if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
5423e1aeb00SDavid Howells 			kfree(ctx->legacy_data);
5433e1aeb00SDavid Howells 		kfree(ctx);
5443e1aeb00SDavid Howells 	}
5453e1aeb00SDavid Howells }
5463e1aeb00SDavid Howells 
5473e1aeb00SDavid Howells /*
5480b52075eSAl Viro  * Duplicate a legacy config.
5490b52075eSAl Viro  */
legacy_fs_context_dup(struct fs_context * fc,struct fs_context * src_fc)5500b52075eSAl Viro static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
5510b52075eSAl Viro {
5520b52075eSAl Viro 	struct legacy_fs_context *ctx;
5530b52075eSAl Viro 	struct legacy_fs_context *src_ctx = src_fc->fs_private;
5540b52075eSAl Viro 
5550b52075eSAl Viro 	ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
5560b52075eSAl Viro 	if (!ctx)
5570b52075eSAl Viro 		return -ENOMEM;
5580b52075eSAl Viro 
5590b52075eSAl Viro 	if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
5600b52075eSAl Viro 		ctx->legacy_data = kmemdup(src_ctx->legacy_data,
5610b52075eSAl Viro 					   src_ctx->data_size, GFP_KERNEL);
5620b52075eSAl Viro 		if (!ctx->legacy_data) {
5630b52075eSAl Viro 			kfree(ctx);
5640b52075eSAl Viro 			return -ENOMEM;
5650b52075eSAl Viro 		}
5660b52075eSAl Viro 	}
5670b52075eSAl Viro 
5680b52075eSAl Viro 	fc->fs_private = ctx;
5690b52075eSAl Viro 	return 0;
5700b52075eSAl Viro }
5710b52075eSAl Viro 
5720b52075eSAl Viro /*
5733e1aeb00SDavid Howells  * Add a parameter to a legacy config.  We build up a comma-separated list of
5743e1aeb00SDavid Howells  * options.
5753e1aeb00SDavid Howells  */
legacy_parse_param(struct fs_context * fc,struct fs_parameter * param)5763e1aeb00SDavid Howells static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
5773e1aeb00SDavid Howells {
5783e1aeb00SDavid Howells 	struct legacy_fs_context *ctx = fc->fs_private;
5793e1aeb00SDavid Howells 	unsigned int size = ctx->data_size;
5803e1aeb00SDavid Howells 	size_t len = 0;
581d1d488d8SChristian Brauner 	int ret;
5823e1aeb00SDavid Howells 
583d1d488d8SChristian Brauner 	ret = vfs_parse_fs_param_source(fc, param);
584d1d488d8SChristian Brauner 	if (ret != -ENOPARAM)
585d1d488d8SChristian Brauner 		return ret;
5863e1aeb00SDavid Howells 
5873e1aeb00SDavid Howells 	if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
5883e1aeb00SDavid Howells 		return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
5893e1aeb00SDavid Howells 
5903e1aeb00SDavid Howells 	switch (param->type) {
5913e1aeb00SDavid Howells 	case fs_value_is_string:
5923e1aeb00SDavid Howells 		len = 1 + param->size;
593df561f66SGustavo A. R. Silva 		fallthrough;
5943e1aeb00SDavid Howells 	case fs_value_is_flag:
5953e1aeb00SDavid Howells 		len += strlen(param->key);
5963e1aeb00SDavid Howells 		break;
5973e1aeb00SDavid Howells 	default:
5983e1aeb00SDavid Howells 		return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
5993e1aeb00SDavid Howells 			      param->key);
6003e1aeb00SDavid Howells 	}
6013e1aeb00SDavid Howells 
602722d9484SJamie Hill-Daniel 	if (size + len + 2 > PAGE_SIZE)
6033e1aeb00SDavid Howells 		return invalf(fc, "VFS: Legacy: Cumulative options too large");
6043e1aeb00SDavid Howells 	if (strchr(param->key, ',') ||
6053e1aeb00SDavid Howells 	    (param->type == fs_value_is_string &&
6063e1aeb00SDavid Howells 	     memchr(param->string, ',', param->size)))
6073e1aeb00SDavid Howells 		return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
6083e1aeb00SDavid Howells 			      param->key);
6093e1aeb00SDavid Howells 	if (!ctx->legacy_data) {
6103e1aeb00SDavid Howells 		ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
6113e1aeb00SDavid Howells 		if (!ctx->legacy_data)
6123e1aeb00SDavid Howells 			return -ENOMEM;
6133e1aeb00SDavid Howells 	}
6143e1aeb00SDavid Howells 
61562176420SThomas Weißschuh 	if (size)
6163e1aeb00SDavid Howells 		ctx->legacy_data[size++] = ',';
6173e1aeb00SDavid Howells 	len = strlen(param->key);
6183e1aeb00SDavid Howells 	memcpy(ctx->legacy_data + size, param->key, len);
6193e1aeb00SDavid Howells 	size += len;
6203e1aeb00SDavid Howells 	if (param->type == fs_value_is_string) {
6213e1aeb00SDavid Howells 		ctx->legacy_data[size++] = '=';
6223e1aeb00SDavid Howells 		memcpy(ctx->legacy_data + size, param->string, param->size);
6233e1aeb00SDavid Howells 		size += param->size;
6243e1aeb00SDavid Howells 	}
6253e1aeb00SDavid Howells 	ctx->legacy_data[size] = '\0';
6263e1aeb00SDavid Howells 	ctx->data_size = size;
6273e1aeb00SDavid Howells 	ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
6283e1aeb00SDavid Howells 	return 0;
6299bc61ab1SDavid Howells }
6309bc61ab1SDavid Howells 
6319bc61ab1SDavid Howells /*
6329bc61ab1SDavid Howells  * Add monolithic mount data.
6339bc61ab1SDavid Howells  */
legacy_parse_monolithic(struct fs_context * fc,void * data)6349bc61ab1SDavid Howells static int legacy_parse_monolithic(struct fs_context *fc, void *data)
6359bc61ab1SDavid Howells {
6369bc61ab1SDavid Howells 	struct legacy_fs_context *ctx = fc->fs_private;
6373e1aeb00SDavid Howells 
6383e1aeb00SDavid Howells 	if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
6393e1aeb00SDavid Howells 		pr_warn("VFS: Can't mix monolithic and individual options\n");
6403e1aeb00SDavid Howells 		return -EINVAL;
6413e1aeb00SDavid Howells 	}
6423e1aeb00SDavid Howells 
6439bc61ab1SDavid Howells 	ctx->legacy_data = data;
6443e1aeb00SDavid Howells 	ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
6459bc61ab1SDavid Howells 	if (!ctx->legacy_data)
6469bc61ab1SDavid Howells 		return 0;
6473e1aeb00SDavid Howells 
6489bc61ab1SDavid Howells 	if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
6499bc61ab1SDavid Howells 		return 0;
6509bc61ab1SDavid Howells 	return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
6519bc61ab1SDavid Howells }
6529bc61ab1SDavid Howells 
6539bc61ab1SDavid Howells /*
6549bc61ab1SDavid Howells  * Get a mountable root with the legacy mount command.
6559bc61ab1SDavid Howells  */
legacy_get_tree(struct fs_context * fc)656f3a09c92SAl Viro static int legacy_get_tree(struct fs_context *fc)
6579bc61ab1SDavid Howells {
6589bc61ab1SDavid Howells 	struct legacy_fs_context *ctx = fc->fs_private;
6599bc61ab1SDavid Howells 	struct super_block *sb;
6609bc61ab1SDavid Howells 	struct dentry *root;
6619bc61ab1SDavid Howells 
6629bc61ab1SDavid Howells 	root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
6639bc61ab1SDavid Howells 				      fc->source, ctx->legacy_data);
6649bc61ab1SDavid Howells 	if (IS_ERR(root))
6659bc61ab1SDavid Howells 		return PTR_ERR(root);
6669bc61ab1SDavid Howells 
6679bc61ab1SDavid Howells 	sb = root->d_sb;
6689bc61ab1SDavid Howells 	BUG_ON(!sb);
6699bc61ab1SDavid Howells 
6709bc61ab1SDavid Howells 	fc->root = root;
6719bc61ab1SDavid Howells 	return 0;
6729bc61ab1SDavid Howells }
6739bc61ab1SDavid Howells 
6749bc61ab1SDavid Howells /*
6758d0347f6SDavid Howells  * Handle remount.
6768d0347f6SDavid Howells  */
legacy_reconfigure(struct fs_context * fc)677f3a09c92SAl Viro static int legacy_reconfigure(struct fs_context *fc)
6788d0347f6SDavid Howells {
6798d0347f6SDavid Howells 	struct legacy_fs_context *ctx = fc->fs_private;
6808d0347f6SDavid Howells 	struct super_block *sb = fc->root->d_sb;
6818d0347f6SDavid Howells 
6828d0347f6SDavid Howells 	if (!sb->s_op->remount_fs)
6838d0347f6SDavid Howells 		return 0;
6848d0347f6SDavid Howells 
6858d0347f6SDavid Howells 	return sb->s_op->remount_fs(sb, &fc->sb_flags,
6868d0347f6SDavid Howells 				    ctx ? ctx->legacy_data : NULL);
6878d0347f6SDavid Howells }
6888d0347f6SDavid Howells 
689f3a09c92SAl Viro const struct fs_context_operations legacy_fs_context_ops = {
690f3a09c92SAl Viro 	.free			= legacy_fs_context_free,
6910b52075eSAl Viro 	.dup			= legacy_fs_context_dup,
6923e1aeb00SDavid Howells 	.parse_param		= legacy_parse_param,
693f3a09c92SAl Viro 	.parse_monolithic	= legacy_parse_monolithic,
694f3a09c92SAl Viro 	.get_tree		= legacy_get_tree,
695f3a09c92SAl Viro 	.reconfigure		= legacy_reconfigure,
696f3a09c92SAl Viro };
697f3a09c92SAl Viro 
6988d0347f6SDavid Howells /*
6999bc61ab1SDavid Howells  * Initialise a legacy context for a filesystem that doesn't support
7009bc61ab1SDavid Howells  * fs_context.
7019bc61ab1SDavid Howells  */
legacy_init_fs_context(struct fs_context * fc)7029bc61ab1SDavid Howells static int legacy_init_fs_context(struct fs_context *fc)
7039bc61ab1SDavid Howells {
704bb902cb4SYutian Yang 	fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT);
7059bc61ab1SDavid Howells 	if (!fc->fs_private)
7069bc61ab1SDavid Howells 		return -ENOMEM;
707f3a09c92SAl Viro 	fc->ops = &legacy_fs_context_ops;
7089bc61ab1SDavid Howells 	return 0;
7099bc61ab1SDavid Howells }
7109bc61ab1SDavid Howells 
parse_monolithic_mount_data(struct fs_context * fc,void * data)7119bc61ab1SDavid Howells int parse_monolithic_mount_data(struct fs_context *fc, void *data)
7129bc61ab1SDavid Howells {
713f3a09c92SAl Viro 	int (*monolithic_mount_data)(struct fs_context *, void *);
7143e1aeb00SDavid Howells 
715f3a09c92SAl Viro 	monolithic_mount_data = fc->ops->parse_monolithic;
7163e1aeb00SDavid Howells 	if (!monolithic_mount_data)
7173e1aeb00SDavid Howells 		monolithic_mount_data = generic_parse_monolithic;
7183e1aeb00SDavid Howells 
719f3a09c92SAl Viro 	return monolithic_mount_data(fc, data);
7209bc61ab1SDavid Howells }
721ecdab150SDavid Howells 
722ecdab150SDavid Howells /*
723ecdab150SDavid Howells  * Clean up a context after performing an action on it and put it into a state
724ecdab150SDavid Howells  * from where it can be used to reconfigure a superblock.
725ecdab150SDavid Howells  *
726ecdab150SDavid Howells  * Note that here we do only the parts that can't fail; the rest is in
727ecdab150SDavid Howells  * finish_clean_context() below and in between those fs_context is marked
728ecdab150SDavid Howells  * FS_CONTEXT_AWAITING_RECONF.  The reason for splitup is that after
729ecdab150SDavid Howells  * successful mount or remount we need to report success to userland.
730ecdab150SDavid Howells  * Trying to do full reinit (for the sake of possible subsequent remount)
731ecdab150SDavid Howells  * and failing to allocate memory would've put us into a nasty situation.
732ecdab150SDavid Howells  * So here we only discard the old state and reinitialization is left
733ecdab150SDavid Howells  * until we actually try to reconfigure.
734ecdab150SDavid Howells  */
vfs_clean_context(struct fs_context * fc)735ecdab150SDavid Howells void vfs_clean_context(struct fs_context *fc)
736ecdab150SDavid Howells {
737ecdab150SDavid Howells 	if (fc->need_free && fc->ops && fc->ops->free)
738ecdab150SDavid Howells 		fc->ops->free(fc);
739ecdab150SDavid Howells 	fc->need_free = false;
740ecdab150SDavid Howells 	fc->fs_private = NULL;
741ecdab150SDavid Howells 	fc->s_fs_info = NULL;
742ecdab150SDavid Howells 	fc->sb_flags = 0;
743ecdab150SDavid Howells 	security_free_mnt_opts(&fc->security);
744ecdab150SDavid Howells 	kfree(fc->source);
745ecdab150SDavid Howells 	fc->source = NULL;
74622ed7ecdSChristian Brauner 	fc->exclusive = false;
747ecdab150SDavid Howells 
748ecdab150SDavid Howells 	fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
749ecdab150SDavid Howells 	fc->phase = FS_CONTEXT_AWAITING_RECONF;
750ecdab150SDavid Howells }
751ecdab150SDavid Howells 
finish_clean_context(struct fs_context * fc)752ecdab150SDavid Howells int finish_clean_context(struct fs_context *fc)
753ecdab150SDavid Howells {
754ecdab150SDavid Howells 	int error;
755ecdab150SDavid Howells 
756ecdab150SDavid Howells 	if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
757ecdab150SDavid Howells 		return 0;
758ecdab150SDavid Howells 
759ecdab150SDavid Howells 	if (fc->fs_type->init_fs_context)
760ecdab150SDavid Howells 		error = fc->fs_type->init_fs_context(fc);
761ecdab150SDavid Howells 	else
762ecdab150SDavid Howells 		error = legacy_init_fs_context(fc);
763ecdab150SDavid Howells 	if (unlikely(error)) {
764ecdab150SDavid Howells 		fc->phase = FS_CONTEXT_FAILED;
765ecdab150SDavid Howells 		return error;
766ecdab150SDavid Howells 	}
767ecdab150SDavid Howells 	fc->need_free = true;
768ecdab150SDavid Howells 	fc->phase = FS_CONTEXT_RECONF_PARAMS;
769ecdab150SDavid Howells 	return 0;
770ecdab150SDavid Howells }
771