Commit | Line | Data |
---|---|---|
9bc61ab1 DH |
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. | |
3 | * | |
4 | * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
6 | * | |
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. | |
11 | */ | |
12 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | #include <linux/fs_context.h> | |
15 | #include <linux/fs.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> | |
25 | #include "mount.h" | |
26 | #include "internal.h" | |
27 | ||
28 | struct legacy_fs_context { | |
29 | char *legacy_data; /* Data page for legacy filesystems */ | |
30 | size_t data_size; | |
31 | }; | |
32 | ||
33 | static int legacy_init_fs_context(struct fs_context *fc); | |
34 | ||
35 | /** | |
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. | |
42 | * | |
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. | |
47 | */ | |
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) | |
53 | { | |
54 | struct fs_context *fc; | |
55 | int ret = -ENOMEM; | |
56 | ||
57 | fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL); | |
58 | if (!fc) | |
59 | return ERR_PTR(-ENOMEM); | |
60 | ||
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); | |
67 | ||
68 | switch (purpose) { | |
69 | case FS_CONTEXT_FOR_MOUNT: | |
70 | fc->user_ns = get_user_ns(fc->cred->user_ns); | |
71 | break; | |
8d0347f6 DH |
72 | case FS_CONTEXT_FOR_RECONFIGURE: |
73 | /* We don't pin any namespaces as the superblock's | |
74 | * subscriptions cannot be changed at this point. | |
75 | */ | |
76 | atomic_inc(&reference->d_sb->s_active); | |
77 | fc->root = dget(reference); | |
78 | break; | |
9bc61ab1 DH |
79 | } |
80 | ||
81 | ret = legacy_init_fs_context(fc); | |
82 | if (ret < 0) | |
83 | goto err_fc; | |
84 | fc->need_free = true; | |
85 | return fc; | |
86 | ||
87 | err_fc: | |
88 | put_fs_context(fc); | |
89 | return ERR_PTR(ret); | |
90 | } | |
91 | ||
92 | struct fs_context *fs_context_for_mount(struct file_system_type *fs_type, | |
93 | unsigned int sb_flags) | |
94 | { | |
95 | return alloc_fs_context(fs_type, NULL, sb_flags, 0, | |
96 | FS_CONTEXT_FOR_MOUNT); | |
97 | } | |
98 | EXPORT_SYMBOL(fs_context_for_mount); | |
99 | ||
8d0347f6 DH |
100 | struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, |
101 | unsigned int sb_flags, | |
102 | unsigned int sb_flags_mask) | |
103 | { | |
104 | return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags, | |
105 | sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE); | |
106 | } | |
107 | EXPORT_SYMBOL(fs_context_for_reconfigure); | |
108 | ||
c9ce29ed AV |
109 | void fc_drop_locked(struct fs_context *fc) |
110 | { | |
111 | struct super_block *sb = fc->root->d_sb; | |
112 | dput(fc->root); | |
113 | fc->root = NULL; | |
114 | deactivate_locked_super(sb); | |
115 | } | |
116 | ||
9bc61ab1 | 117 | static void legacy_fs_context_free(struct fs_context *fc); |
8d0347f6 | 118 | |
9bc61ab1 DH |
119 | /** |
120 | * put_fs_context - Dispose of a superblock configuration context. | |
121 | * @fc: The context to dispose of. | |
122 | */ | |
123 | void put_fs_context(struct fs_context *fc) | |
124 | { | |
125 | struct super_block *sb; | |
126 | ||
127 | if (fc->root) { | |
128 | sb = fc->root->d_sb; | |
129 | dput(fc->root); | |
130 | fc->root = NULL; | |
131 | deactivate_super(sb); | |
132 | } | |
133 | ||
134 | if (fc->need_free) | |
135 | legacy_fs_context_free(fc); | |
136 | ||
137 | security_free_mnt_opts(&fc->security); | |
8d0347f6 | 138 | put_net(fc->net_ns); |
9bc61ab1 DH |
139 | put_user_ns(fc->user_ns); |
140 | put_cred(fc->cred); | |
141 | kfree(fc->subtype); | |
142 | put_filesystem(fc->fs_type); | |
143 | kfree(fc->source); | |
144 | kfree(fc); | |
145 | } | |
146 | EXPORT_SYMBOL(put_fs_context); | |
147 | ||
148 | /* | |
149 | * Free the config for a filesystem that doesn't support fs_context. | |
150 | */ | |
151 | static void legacy_fs_context_free(struct fs_context *fc) | |
152 | { | |
153 | kfree(fc->fs_private); | |
154 | } | |
155 | ||
156 | /* | |
157 | * Add monolithic mount data. | |
158 | */ | |
159 | static int legacy_parse_monolithic(struct fs_context *fc, void *data) | |
160 | { | |
161 | struct legacy_fs_context *ctx = fc->fs_private; | |
162 | ctx->legacy_data = data; | |
163 | if (!ctx->legacy_data) | |
164 | return 0; | |
165 | if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA) | |
166 | return 0; | |
167 | return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security); | |
168 | } | |
169 | ||
170 | /* | |
171 | * Get a mountable root with the legacy mount command. | |
172 | */ | |
173 | int legacy_get_tree(struct fs_context *fc) | |
174 | { | |
175 | struct legacy_fs_context *ctx = fc->fs_private; | |
176 | struct super_block *sb; | |
177 | struct dentry *root; | |
178 | ||
179 | root = fc->fs_type->mount(fc->fs_type, fc->sb_flags, | |
180 | fc->source, ctx->legacy_data); | |
181 | if (IS_ERR(root)) | |
182 | return PTR_ERR(root); | |
183 | ||
184 | sb = root->d_sb; | |
185 | BUG_ON(!sb); | |
186 | ||
187 | fc->root = root; | |
188 | return 0; | |
189 | } | |
190 | ||
8d0347f6 DH |
191 | /* |
192 | * Handle remount. | |
193 | */ | |
194 | int legacy_reconfigure(struct fs_context *fc) | |
195 | { | |
196 | struct legacy_fs_context *ctx = fc->fs_private; | |
197 | struct super_block *sb = fc->root->d_sb; | |
198 | ||
199 | if (!sb->s_op->remount_fs) | |
200 | return 0; | |
201 | ||
202 | return sb->s_op->remount_fs(sb, &fc->sb_flags, | |
203 | ctx ? ctx->legacy_data : NULL); | |
204 | } | |
205 | ||
9bc61ab1 DH |
206 | /* |
207 | * Initialise a legacy context for a filesystem that doesn't support | |
208 | * fs_context. | |
209 | */ | |
210 | static int legacy_init_fs_context(struct fs_context *fc) | |
211 | { | |
212 | fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL); | |
213 | if (!fc->fs_private) | |
214 | return -ENOMEM; | |
215 | return 0; | |
216 | } | |
217 | ||
218 | int parse_monolithic_mount_data(struct fs_context *fc, void *data) | |
219 | { | |
220 | return legacy_parse_monolithic(fc, data); | |
221 | } |