1 /* Provide a way to create a superblock configuration context within the kernel
2 * that allows a superblock to be set up prior to mounting.
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/fs_context.h>
16 #include <linux/mount.h>
17 #include <linux/nsproxy.h>
18 #include <linux/slab.h>
19 #include <linux/magic.h>
20 #include <linux/security.h>
21 #include <linux/mnt_namespace.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/user_namespace.h>
24 #include <net/net_namespace.h>
28 struct legacy_fs_context {
29 char *legacy_data; /* Data page for legacy filesystems */
33 static int legacy_init_fs_context(struct fs_context *fc);
36 * alloc_fs_context - Create a filesystem context.
37 * @fs_type: The filesystem type.
38 * @reference: The dentry from which this one derives (or NULL)
39 * @sb_flags: Filesystem/superblock flags (SB_*)
40 * @sb_flags_mask: Applicable members of @sb_flags
41 * @purpose: The purpose that this configuration shall be used for.
43 * Open a filesystem and create a mount context. The mount context is
44 * initialised with the supplied flags and, if a submount/automount from
45 * another superblock (referred to by @reference) is supplied, may have
46 * parameters such as namespaces copied across from that superblock.
48 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
49 struct dentry *reference,
50 unsigned int sb_flags,
51 unsigned int sb_flags_mask,
52 enum fs_context_purpose purpose)
54 struct fs_context *fc;
57 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
59 return ERR_PTR(-ENOMEM);
61 fc->purpose = purpose;
62 fc->sb_flags = sb_flags;
63 fc->sb_flags_mask = sb_flags_mask;
64 fc->fs_type = get_filesystem(fs_type);
65 fc->cred = get_current_cred();
66 fc->net_ns = get_net(current->nsproxy->net_ns);
69 case FS_CONTEXT_FOR_MOUNT:
70 fc->user_ns = get_user_ns(fc->cred->user_ns);
74 ret = legacy_init_fs_context(fc);
85 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
86 unsigned int sb_flags)
88 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
89 FS_CONTEXT_FOR_MOUNT);
91 EXPORT_SYMBOL(fs_context_for_mount);
93 void fc_drop_locked(struct fs_context *fc)
95 struct super_block *sb = fc->root->d_sb;
98 deactivate_locked_super(sb);
101 static void legacy_fs_context_free(struct fs_context *fc);
103 * put_fs_context - Dispose of a superblock configuration context.
104 * @fc: The context to dispose of.
106 void put_fs_context(struct fs_context *fc)
108 struct super_block *sb;
114 deactivate_super(sb);
118 legacy_fs_context_free(fc);
120 security_free_mnt_opts(&fc->security);
123 put_user_ns(fc->user_ns);
126 put_filesystem(fc->fs_type);
130 EXPORT_SYMBOL(put_fs_context);
133 * Free the config for a filesystem that doesn't support fs_context.
135 static void legacy_fs_context_free(struct fs_context *fc)
137 kfree(fc->fs_private);
141 * Add monolithic mount data.
143 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
145 struct legacy_fs_context *ctx = fc->fs_private;
146 ctx->legacy_data = data;
147 if (!ctx->legacy_data)
149 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
151 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
155 * Get a mountable root with the legacy mount command.
157 int legacy_get_tree(struct fs_context *fc)
159 struct legacy_fs_context *ctx = fc->fs_private;
160 struct super_block *sb;
163 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
164 fc->source, ctx->legacy_data);
166 return PTR_ERR(root);
176 * Initialise a legacy context for a filesystem that doesn't support
179 static int legacy_init_fs_context(struct fs_context *fc)
181 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
187 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
189 return legacy_parse_monolithic(fc, data);