blob: 9a4646eecb71bbaababa17828ebde0d5f9bbd700 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * fs/kernfs/mount.c - kernfs mount implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
Tejun Heofa736a92013-11-28 14:54:44 -050010
11#include <linux/fs.h>
12#include <linux/mount.h>
13#include <linux/init.h>
14#include <linux/magic.h>
15#include <linux/slab.h>
16#include <linux/pagemap.h>
Aditya Kalifb3c8312016-01-29 02:54:08 -060017#include <linux/namei.h>
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -050018#include <linux/seq_file.h>
Shaohua Liaa818822017-07-12 11:49:51 -070019#include <linux/exportfs.h>
Tejun Heofa736a92013-11-28 14:54:44 -050020
21#include "kernfs-internal.h"
22
Ayush Mittal26e28d62019-02-06 10:25:42 +053023struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
Tejun Heofa736a92013-11-28 14:54:44 -050024
Tejun Heo6a7fed42014-02-03 14:09:10 -050025static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
26{
Shaohua Li319ba912017-07-12 11:49:49 -070027 struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
Tejun Heo6a7fed42014-02-03 14:09:10 -050028 struct kernfs_syscall_ops *scops = root->syscall_ops;
29
30 if (scops && scops->show_options)
31 return scops->show_options(sf, root);
32 return 0;
33}
34
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -050035static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
36{
Shaohua Li319ba912017-07-12 11:49:49 -070037 struct kernfs_node *node = kernfs_dentry_node(dentry);
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -050038 struct kernfs_root *root = kernfs_root(node);
39 struct kernfs_syscall_ops *scops = root->syscall_ops;
40
41 if (scops && scops->show_path)
42 return scops->show_path(sf, node, root);
43
Serge E. Hallyn3cc9b232016-05-12 00:29:45 -050044 seq_dentry(sf, dentry, " \t\n\\");
45 return 0;
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -050046}
47
Li Zefanf41c5932014-02-14 16:57:27 +080048const struct super_operations kernfs_sops = {
Tejun Heofa736a92013-11-28 14:54:44 -050049 .statfs = simple_statfs,
50 .drop_inode = generic_delete_inode,
Tejun Heoc637b8a2013-12-11 14:11:58 -050051 .evict_inode = kernfs_evict_inode,
Tejun Heo6a7fed42014-02-03 14:09:10 -050052
Tejun Heo6a7fed42014-02-03 14:09:10 -050053 .show_options = kernfs_sop_show_options,
Serge E. Hallyn4f41fc52016-05-09 09:59:55 -050054 .show_path = kernfs_sop_show_path,
Tejun Heofa736a92013-11-28 14:54:44 -050055};
56
Shaohua Li69fd5c32017-07-12 11:49:55 -070057/*
58 * Similar to kernfs_fh_get_inode, this one gets kernfs node from inode
59 * number and generation
60 */
61struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
62 const union kernfs_node_id *id)
63{
64 struct kernfs_node *kn;
65
66 kn = kernfs_find_and_get_node_by_ino(root, id->ino);
67 if (!kn)
68 return NULL;
69 if (kn->id.generation != id->generation) {
70 kernfs_put(kn);
71 return NULL;
72 }
73 return kn;
74}
75
Shaohua Liaa818822017-07-12 11:49:51 -070076static struct inode *kernfs_fh_get_inode(struct super_block *sb,
77 u64 ino, u32 generation)
78{
79 struct kernfs_super_info *info = kernfs_info(sb);
80 struct inode *inode;
81 struct kernfs_node *kn;
82
83 if (ino == 0)
84 return ERR_PTR(-ESTALE);
85
86 kn = kernfs_find_and_get_node_by_ino(info->root, ino);
87 if (!kn)
88 return ERR_PTR(-ESTALE);
89 inode = kernfs_get_inode(sb, kn);
90 kernfs_put(kn);
Dan Carpenteref13ecb2017-08-30 17:04:56 +030091 if (!inode)
92 return ERR_PTR(-ESTALE);
Shaohua Liaa818822017-07-12 11:49:51 -070093
94 if (generation && inode->i_generation != generation) {
95 /* we didn't find the right inode.. */
96 iput(inode);
97 return ERR_PTR(-ESTALE);
98 }
99 return inode;
100}
101
102static struct dentry *kernfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
103 int fh_len, int fh_type)
104{
105 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
106 kernfs_fh_get_inode);
107}
108
109static struct dentry *kernfs_fh_to_parent(struct super_block *sb, struct fid *fid,
110 int fh_len, int fh_type)
111{
112 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
113 kernfs_fh_get_inode);
114}
115
116static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
117{
118 struct kernfs_node *kn = kernfs_dentry_node(child);
119
120 return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
121}
122
123static const struct export_operations kernfs_export_ops = {
124 .fh_to_dentry = kernfs_fh_to_dentry,
125 .fh_to_parent = kernfs_fh_to_parent,
126 .get_parent = kernfs_get_parent_dentry,
127};
128
Tejun Heo0c23b222014-02-03 14:09:15 -0500129/**
130 * kernfs_root_from_sb - determine kernfs_root associated with a super_block
131 * @sb: the super_block in question
132 *
133 * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
134 * %NULL is returned.
135 */
136struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
137{
138 if (sb->s_op == &kernfs_sops)
139 return kernfs_info(sb)->root;
140 return NULL;
141}
142
Aditya Kalifb3c8312016-01-29 02:54:08 -0600143/*
144 * find the next ancestor in the path down to @child, where @parent was the
145 * ancestor whose descendant we want to find.
146 *
147 * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root
148 * node. If @parent is b, then we return the node for c.
149 * Passing in d as @parent is not ok.
150 */
151static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
152 struct kernfs_node *parent)
153{
154 if (child == parent) {
155 pr_crit_once("BUG in find_next_ancestor: called with parent == child");
156 return NULL;
157 }
158
159 while (child->parent != parent) {
160 if (!child->parent)
161 return NULL;
162 child = child->parent;
163 }
164
165 return child;
166}
167
168/**
169 * kernfs_node_dentry - get a dentry for the given kernfs_node
170 * @kn: kernfs_node for which a dentry is needed
171 * @sb: the kernfs super_block
172 */
173struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
174 struct super_block *sb)
175{
176 struct dentry *dentry;
177 struct kernfs_node *knparent = NULL;
178
179 BUG_ON(sb->s_op != &kernfs_sops);
180
181 dentry = dget(sb->s_root);
182
183 /* Check if this is the root kernfs_node */
184 if (!kn->parent)
185 return dentry;
186
187 knparent = find_next_ancestor(kn, NULL);
Al Viro399504e2019-01-06 11:41:29 -0500188 if (WARN_ON(!knparent)) {
189 dput(dentry);
Aditya Kalifb3c8312016-01-29 02:54:08 -0600190 return ERR_PTR(-EINVAL);
Al Viro399504e2019-01-06 11:41:29 -0500191 }
Aditya Kalifb3c8312016-01-29 02:54:08 -0600192
193 do {
194 struct dentry *dtmp;
195 struct kernfs_node *kntmp;
196
197 if (kn == knparent)
198 return dentry;
199 kntmp = find_next_ancestor(kn, knparent);
Al Viro399504e2019-01-06 11:41:29 -0500200 if (WARN_ON(!kntmp)) {
201 dput(dentry);
Aditya Kalifb3c8312016-01-29 02:54:08 -0600202 return ERR_PTR(-EINVAL);
Al Viro399504e2019-01-06 11:41:29 -0500203 }
Al Viro779b8392016-04-11 08:42:55 -0400204 dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
205 strlen(kntmp->name));
Aditya Kalifb3c8312016-01-29 02:54:08 -0600206 dput(dentry);
207 if (IS_ERR(dtmp))
208 return dtmp;
209 knparent = kntmp;
210 dentry = dtmp;
211 } while (true);
212}
213
David Howells23bf1b62018-11-01 23:07:26 +0000214static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
Tejun Heofa736a92013-11-28 14:54:44 -0500215{
Tejun Heoc525aad2013-12-11 14:11:55 -0500216 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heofa736a92013-11-28 14:54:44 -0500217 struct inode *inode;
218 struct dentry *root;
219
Tejun Heo7d568a82014-04-09 11:07:30 -0400220 info->sb = sb;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -0500221 /* Userspace would break if executables or devices appear on sysfs */
222 sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300223 sb->s_blocksize = PAGE_SIZE;
224 sb->s_blocksize_bits = PAGE_SHIFT;
David Howells23bf1b62018-11-01 23:07:26 +0000225 sb->s_magic = kfc->magic;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500226 sb->s_op = &kernfs_sops;
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200227 sb->s_xattr = kernfs_xattr_handlers;
Shaohua Liaa818822017-07-12 11:49:51 -0700228 if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
229 sb->s_export_op = &kernfs_export_ops;
Tejun Heofa736a92013-11-28 14:54:44 -0500230 sb->s_time_gran = 1;
231
Johannes Weiner4b85afb2018-10-26 15:06:42 -0700232 /* sysfs dentries and inodes don't require IO to create */
233 sb->s_shrink.seeks = 0;
234
Tejun Heofa736a92013-11-28 14:54:44 -0500235 /* get root inode, initialize and unlock it */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500236 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500237 inode = kernfs_get_inode(sb, info->root->kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500238 mutex_unlock(&kernfs_mutex);
Tejun Heofa736a92013-11-28 14:54:44 -0500239 if (!inode) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500240 pr_debug("kernfs: could not get root inode\n");
Tejun Heofa736a92013-11-28 14:54:44 -0500241 return -ENOMEM;
242 }
243
244 /* instantiate and link root dentry */
245 root = d_make_root(inode);
246 if (!root) {
247 pr_debug("%s: could not get root dentry!\n", __func__);
248 return -ENOMEM;
249 }
Tejun Heofa736a92013-11-28 14:54:44 -0500250 sb->s_root = root;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500251 sb->s_d_op = &kernfs_dops;
Tejun Heofa736a92013-11-28 14:54:44 -0500252 return 0;
253}
254
David Howells23bf1b62018-11-01 23:07:26 +0000255static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
Tejun Heofa736a92013-11-28 14:54:44 -0500256{
Tejun Heoc525aad2013-12-11 14:11:55 -0500257 struct kernfs_super_info *sb_info = kernfs_info(sb);
David Howells23bf1b62018-11-01 23:07:26 +0000258 struct kernfs_super_info *info = fc->s_fs_info;
Tejun Heofa736a92013-11-28 14:54:44 -0500259
260 return sb_info->root == info->root && sb_info->ns == info->ns;
261}
262
David Howells23bf1b62018-11-01 23:07:26 +0000263static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
Tejun Heofa736a92013-11-28 14:54:44 -0500264{
David Howells23bf1b62018-11-01 23:07:26 +0000265 struct kernfs_fs_context *kfc = fc->fs_private;
266
267 kfc->ns_tag = NULL;
268 return set_anon_super_fc(sb, fc);
Tejun Heofa736a92013-11-28 14:54:44 -0500269}
270
271/**
272 * kernfs_super_ns - determine the namespace tag of a kernfs super_block
273 * @sb: super_block of interest
274 *
275 * Return the namespace tag associated with kernfs super_block @sb.
276 */
277const void *kernfs_super_ns(struct super_block *sb)
278{
Tejun Heoc525aad2013-12-11 14:11:55 -0500279 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heofa736a92013-11-28 14:54:44 -0500280
281 return info->ns;
282}
283
284/**
David Howells23bf1b62018-11-01 23:07:26 +0000285 * kernfs_get_tree - kernfs filesystem access/retrieval helper
286 * @fc: The filesystem context.
Tejun Heofa736a92013-11-28 14:54:44 -0500287 *
David Howells23bf1b62018-11-01 23:07:26 +0000288 * This is to be called from each kernfs user's fs_context->ops->get_tree()
289 * implementation, which should set the specified ->@fs_type and ->@flags, and
290 * specify the hierarchy and namespace tag to mount via ->@root and ->@ns,
291 * respectively.
Tejun Heofa736a92013-11-28 14:54:44 -0500292 */
David Howells23bf1b62018-11-01 23:07:26 +0000293int kernfs_get_tree(struct fs_context *fc)
Tejun Heofa736a92013-11-28 14:54:44 -0500294{
David Howells23bf1b62018-11-01 23:07:26 +0000295 struct kernfs_fs_context *kfc = fc->fs_private;
Tejun Heofa736a92013-11-28 14:54:44 -0500296 struct super_block *sb;
Tejun Heoc525aad2013-12-11 14:11:55 -0500297 struct kernfs_super_info *info;
Tejun Heofa736a92013-11-28 14:54:44 -0500298 int error;
299
300 info = kzalloc(sizeof(*info), GFP_KERNEL);
301 if (!info)
David Howells23bf1b62018-11-01 23:07:26 +0000302 return -ENOMEM;
Tejun Heofa736a92013-11-28 14:54:44 -0500303
David Howells23bf1b62018-11-01 23:07:26 +0000304 info->root = kfc->root;
305 info->ns = kfc->ns_tag;
Al Viro82382ac2018-04-03 00:22:29 -0400306 INIT_LIST_HEAD(&info->node);
Tejun Heofa736a92013-11-28 14:54:44 -0500307
David Howells23bf1b62018-11-01 23:07:26 +0000308 fc->s_fs_info = info;
309 sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
Tejun Heofa736a92013-11-28 14:54:44 -0500310 if (IS_ERR(sb))
David Howells23bf1b62018-11-01 23:07:26 +0000311 return PTR_ERR(sb);
Li Zefanfed95ba2014-02-25 19:28:44 +0800312
Tejun Heofa736a92013-11-28 14:54:44 -0500313 if (!sb->s_root) {
Tejun Heo7d568a82014-04-09 11:07:30 -0400314 struct kernfs_super_info *info = kernfs_info(sb);
315
David Howells23bf1b62018-11-01 23:07:26 +0000316 kfc->new_sb_created = true;
317
318 error = kernfs_fill_super(sb, kfc);
Tejun Heofa736a92013-11-28 14:54:44 -0500319 if (error) {
320 deactivate_locked_super(sb);
David Howells23bf1b62018-11-01 23:07:26 +0000321 return error;
Tejun Heofa736a92013-11-28 14:54:44 -0500322 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800323 sb->s_flags |= SB_ACTIVE;
Tejun Heo7d568a82014-04-09 11:07:30 -0400324
325 mutex_lock(&kernfs_mutex);
David Howells23bf1b62018-11-01 23:07:26 +0000326 list_add(&info->node, &info->root->supers);
Tejun Heo7d568a82014-04-09 11:07:30 -0400327 mutex_unlock(&kernfs_mutex);
Tejun Heofa736a92013-11-28 14:54:44 -0500328 }
329
David Howells23bf1b62018-11-01 23:07:26 +0000330 fc->root = dget(sb->s_root);
331 return 0;
332}
333
334void kernfs_free_fs_context(struct fs_context *fc)
335{
336 /* Note that we don't deal with kfc->ns_tag here. */
337 kfree(fc->s_fs_info);
338 fc->s_fs_info = NULL;
Tejun Heofa736a92013-11-28 14:54:44 -0500339}
340
341/**
342 * kernfs_kill_sb - kill_sb for kernfs
343 * @sb: super_block being killed
344 *
345 * This can be used directly for file_system_type->kill_sb(). If a kernfs
346 * user needs extra cleanup, it can implement its own kill_sb() and call
347 * this function at the end.
348 */
349void kernfs_kill_sb(struct super_block *sb)
350{
Tejun Heoc525aad2013-12-11 14:11:55 -0500351 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heofa736a92013-11-28 14:54:44 -0500352
Tejun Heo7d568a82014-04-09 11:07:30 -0400353 mutex_lock(&kernfs_mutex);
354 list_del(&info->node);
355 mutex_unlock(&kernfs_mutex);
356
Tejun Heofa736a92013-11-28 14:54:44 -0500357 /*
358 * Remove the superblock from fs_supers/s_instances
Tejun Heoc525aad2013-12-11 14:11:55 -0500359 * so we can't find it, before freeing kernfs_super_info.
Tejun Heofa736a92013-11-28 14:54:44 -0500360 */
361 kill_anon_super(sb);
362 kfree(info);
Tejun Heofa736a92013-11-28 14:54:44 -0500363}
364
365void __init kernfs_init(void)
366{
Shaohua Liba16b282017-07-12 11:49:48 -0700367
368 /*
369 * the slab is freed in RCU context, so kernfs_find_and_get_node_by_ino
370 * can access the slab lock free. This could introduce stale nodes,
371 * please see how kernfs_find_and_get_node_by_ino filters out stale
372 * nodes.
373 */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500374 kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
Tejun Heo324a56e2013-12-11 14:11:53 -0500375 sizeof(struct kernfs_node),
Shaohua Liba16b282017-07-12 11:49:48 -0700376 0,
377 SLAB_PANIC | SLAB_TYPESAFE_BY_RCU,
378 NULL);
Ayush Mittal26e28d62019-02-06 10:25:42 +0530379
380 /* Creates slab cache for kernfs inode attributes */
381 kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",
382 sizeof(struct kernfs_iattrs),
383 0, SLAB_PANIC, NULL);
Tejun Heofa736a92013-11-28 14:54:44 -0500384}