// SPDX-License-Identifier: GPL-2.0-or-later /* Provide a way to create a superblock configuration context within the kernel * that allows a superblock to be set up prior to mounting. * * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com)
*/
/* * Check for a common mount option that manipulates s_flags.
*/ staticint vfs_parse_sb_flag(struct fs_context *fc, constchar *key)
{ unsignedint token;
/** * vfs_parse_fs_param_source - Handle setting "source" via parameter * @fc: The filesystem context to modify * @param: The parameter * * This is a simple helper for filesystems to verify that the "source" they * accept is sane. * * Returns 0 on success, -ENOPARAM if this is not "source" parameter, and * -EINVAL otherwise. In the event of failure, supplementary error information * is logged.
*/ int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
{ if (strcmp(param->key, "source") != 0) return -ENOPARAM;
if (param->type != fs_value_is_string) return invalf(fc, "Non-string source");
if (fc->source) return invalf(fc, "Multiple sources");
/** * vfs_parse_fs_param - Add a single parameter to a superblock config * @fc: The filesystem context to modify * @param: The parameter * * A single mount option in string form is applied to the filesystem context * being set up. Certain standard options (for example "ro") are translated * into flag bits without going to the filesystem. The active security module * is allowed to observe and poach options. Any other options are passed over * to the filesystem to parse. * * This may be called multiple times for a context. * * Returns 0 on success and a negative error code on failure. In the event of * failure, supplementary error information may have been set.
*/ int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
{ int ret;
if (!param->key) return invalf(fc, "Unnamed parameter\n");
ret = vfs_parse_sb_flag(fc, param->key); if (ret != -ENOPARAM) return ret;
ret = security_fs_context_parse_param(fc, param); if (ret != -ENOPARAM) /* Param belongs to the LSM or is disallowed by the LSM; so * don't pass to the FS.
*/ return ret;
if (fc->ops->parse_param) {
ret = fc->ops->parse_param(fc, param); if (ret != -ENOPARAM) return ret;
}
/* If the filesystem doesn't take any arguments, give it the * default handling of source.
*/
ret = vfs_parse_fs_param_source(fc, param); if (ret != -ENOPARAM) return ret;
/** * vfs_parse_fs_string - Convenience function to just parse a string. * @fc: Filesystem context. * @key: Parameter name. * @value: Default value. * @v_size: Maximum number of bytes in the value.
*/ int vfs_parse_fs_string(struct fs_context *fc, constchar *key, constchar *value, size_t v_size)
{ int ret;
if (value) {
param.string = kmemdup_nul(value, v_size, GFP_KERNEL); if (!param.string) return -ENOMEM;
param.type = fs_value_is_string;
}
ret = vfs_parse_fs_param(fc, ¶m);
kfree(param.string); return ret;
}
EXPORT_SYMBOL(vfs_parse_fs_string);
/** * vfs_parse_monolithic_sep - Parse key[=val][,key[=val]]* mount data * @fc: The superblock configuration to fill in. * @data: The data to parse * @sep: callback for separating next option * * Parse a blob of data that's in key[=val][,key[=val]]* form with a custom * option separator callback. * * Returns 0 on success or the error returned by the ->parse_option() fs_context * operation on failure.
*/ int vfs_parse_monolithic_sep(struct fs_context *fc, void *data, char *(*sep)(char **))
{ char *options = data, *key; int ret = 0;
if (!options) return 0;
ret = security_sb_eat_lsm_opts(options, &fc->security); if (ret) return ret;
while ((key = sep(&options)) != NULL) { if (*key) {
size_t v_len = 0; char *value = strchr(key, '=');
if (value) { if (unlikely(value == key)) continue;
*value++ = 0;
v_len = strlen(value);
}
ret = vfs_parse_fs_string(fc, key, value, v_len); if (ret < 0) break;
}
}
/** * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data * @fc: The superblock configuration to fill in. * @data: The data to parse * * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be * called from the ->monolithic_mount_data() fs_context operation. * * Returns 0 on success or the error returned by the ->parse_option() fs_context * operation on failure.
*/ int generic_parse_monolithic(struct fs_context *fc, void *data)
{ return vfs_parse_monolithic_sep(fc, data, vfs_parse_comma_sep);
}
EXPORT_SYMBOL(generic_parse_monolithic);
/** * alloc_fs_context - Create a filesystem context. * @fs_type: The filesystem type. * @reference: The dentry from which this one derives (or NULL) * @sb_flags: Filesystem/superblock flags (SB_*) * @sb_flags_mask: Applicable members of @sb_flags * @purpose: The purpose that this configuration shall be used for. * * Open a filesystem and create a mount context. The mount context is * initialised with the supplied flags and, if a submount/automount from * another superblock (referred to by @reference) is supplied, may have * parameters such as namespaces copied across from that superblock.
*/ staticstruct fs_context *alloc_fs_context(struct file_system_type *fs_type, struct dentry *reference, unsignedint sb_flags, unsignedint sb_flags_mask, enum fs_context_purpose purpose)
{ int (*init_fs_context)(struct fs_context *); struct fs_context *fc; int ret = -ENOMEM;
fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT); if (!fc) return ERR_PTR(-ENOMEM);
switch (purpose) { case FS_CONTEXT_FOR_MOUNT:
fc->user_ns = get_user_ns(fc->cred->user_ns); break; case FS_CONTEXT_FOR_SUBMOUNT:
fc->user_ns = get_user_ns(reference->d_sb->s_user_ns); break; case FS_CONTEXT_FOR_RECONFIGURE:
atomic_inc(&reference->d_sb->s_active);
fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
fc->root = dget(reference); break;
}
/* TODO: Make all filesystems support this unconditionally */
init_fs_context = fc->fs_type->init_fs_context; if (!init_fs_context)
init_fs_context = legacy_init_fs_context;
ret = init_fs_context(fc); if (ret < 0) goto err_fc;
fc->need_free = true; return fc;
/** * fs_context_for_submount: allocate a new fs_context for a submount * @type: file_system_type of the new context * @reference: reference dentry from which to copy relevant info * * Allocate a new fs_context suitable for a submount. This also ensures that * the fc->security object is inherited from @reference (if needed).
*/ struct fs_context *fs_context_for_submount(struct file_system_type *type, struct dentry *reference)
{ struct fs_context *fc; int ret;
fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT); if (IS_ERR(fc)) return fc;
ret = security_fs_context_submount(fc, reference->d_sb); if (ret) {
put_fs_context(fc); return ERR_PTR(ret);
}
/** * logfc - Log a message to a filesystem context * @log: The filesystem context to log to, or NULL to use printk. * @prefix: A string to prefix the output with, or NULL. * @level: 'w' for a warning, 'e' for an error. Anything else is a notice. * @fmt: The format of the buffer.
*/ void logfc(struct fc_log *log, constchar *prefix, char level, constchar *fmt, ...)
{
va_list va; struct va_format vaf = {.fmt = fmt, .va = &va};
/* * Free a logging structure.
*/ staticvoid put_fc_log(struct fs_context *fc)
{ struct fc_log *log = fc->log.log; int i;
if (log) { if (refcount_dec_and_test(&log->usage)) {
fc->log.log = NULL; for (i = 0; i < ARRAY_SIZE(log->buffer) ; i++) if (log->need_free & (1 << i))
kfree(log->buffer[i]);
kfree(log);
}
}
}
/** * put_fs_context - Dispose of a superblock configuration context. * @fc: The context to dispose of.
*/ void put_fs_context(struct fs_context *fc)
{ struct super_block *sb;
/* * Free the config for a filesystem that doesn't support fs_context.
*/ staticvoid legacy_fs_context_free(struct fs_context *fc)
{ struct legacy_fs_context *ctx = fc->fs_private;
if (ctx) { if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
kfree(ctx->legacy_data);
kfree(ctx);
}
}
ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL); if (!ctx) return -ENOMEM;
if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
ctx->legacy_data = kmemdup(src_ctx->legacy_data,
src_ctx->data_size, GFP_KERNEL); if (!ctx->legacy_data) {
kfree(ctx); return -ENOMEM;
}
}
fc->fs_private = ctx; return 0;
}
/* * Add a parameter to a legacy config. We build up a comma-separated list of * options.
*/ staticint legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
{ struct legacy_fs_context *ctx = fc->fs_private; unsignedint size = ctx->data_size;
size_t len = 0; int ret;
ret = vfs_parse_fs_param_source(fc, param); if (ret != -ENOPARAM) return ret;
if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS) return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
switch (param->type) { case fs_value_is_string:
len = 1 + param->size;
fallthrough; case fs_value_is_flag:
len += strlen(param->key); break; default: return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
param->key);
}
if (size + len + 2 > PAGE_SIZE) return invalf(fc, "VFS: Legacy: Cumulative options too large"); if (strchr(param->key, ',') ||
(param->type == fs_value_is_string &&
memchr(param->string, ',', param->size))) return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
param->key); if (!ctx->legacy_data) {
ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!ctx->legacy_data) return -ENOMEM;
}
/* * Initialise a legacy context for a filesystem that doesn't support * fs_context.
*/ staticint legacy_init_fs_context(struct fs_context *fc)
{
fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT); if (!fc->fs_private) return -ENOMEM;
fc->ops = &legacy_fs_context_ops; return 0;
}
int parse_monolithic_mount_data(struct fs_context *fc, void *data)
{ int (*monolithic_mount_data)(struct fs_context *, void *);
monolithic_mount_data = fc->ops->parse_monolithic; if (!monolithic_mount_data)
monolithic_mount_data = generic_parse_monolithic;
return monolithic_mount_data(fc, data);
}
/* * Clean up a context after performing an action on it and put it into a state * from where it can be used to reconfigure a superblock. * * Note that here we do only the parts that can't fail; the rest is in * finish_clean_context() below and in between those fs_context is marked * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after * successful mount or remount we need to report success to userland. * Trying to do full reinit (for the sake of possible subsequent remount) * and failing to allocate memory would've put us into a nasty situation. * So here we only discard the old state and reinitialization is left * until we actually try to reconfigure.
*/ void vfs_clean_context(struct fs_context *fc)
{ if (fc->need_free && fc->ops && fc->ops->free)
fc->ops->free(fc);
fc->need_free = false;
fc->fs_private = NULL;
fc->s_fs_info = NULL;
fc->sb_flags = 0;
security_free_mnt_opts(&fc->security);
kfree(fc->source);
fc->source = NULL;
fc->exclusive = false;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.