blob: 0f4152defe7b6f96afa752acdbc075a512bc8c5d [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>
17
18#include "kernfs-internal.h"
19
Tejun Heoa797bfc2013-12-11 14:11:57 -050020struct kmem_cache *kernfs_node_cache;
Tejun Heofa736a92013-11-28 14:54:44 -050021
Tejun Heoa797bfc2013-12-11 14:11:57 -050022static const struct super_operations kernfs_sops = {
Tejun Heofa736a92013-11-28 14:54:44 -050023 .statfs = simple_statfs,
24 .drop_inode = generic_delete_inode,
Tejun Heoc637b8a2013-12-11 14:11:58 -050025 .evict_inode = kernfs_evict_inode,
Tejun Heofa736a92013-11-28 14:54:44 -050026};
27
Tejun Heoc637b8a2013-12-11 14:11:58 -050028static int kernfs_fill_super(struct super_block *sb)
Tejun Heofa736a92013-11-28 14:54:44 -050029{
Tejun Heoc525aad2013-12-11 14:11:55 -050030 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heofa736a92013-11-28 14:54:44 -050031 struct inode *inode;
32 struct dentry *root;
33
34 sb->s_blocksize = PAGE_CACHE_SIZE;
35 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
36 sb->s_magic = SYSFS_MAGIC;
Tejun Heoa797bfc2013-12-11 14:11:57 -050037 sb->s_op = &kernfs_sops;
Tejun Heofa736a92013-11-28 14:54:44 -050038 sb->s_time_gran = 1;
39
40 /* get root inode, initialize and unlock it */
Tejun Heoa797bfc2013-12-11 14:11:57 -050041 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -050042 inode = kernfs_get_inode(sb, info->root->kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -050043 mutex_unlock(&kernfs_mutex);
Tejun Heofa736a92013-11-28 14:54:44 -050044 if (!inode) {
Tejun Heoc637b8a2013-12-11 14:11:58 -050045 pr_debug("kernfs: could not get root inode\n");
Tejun Heofa736a92013-11-28 14:54:44 -050046 return -ENOMEM;
47 }
48
49 /* instantiate and link root dentry */
50 root = d_make_root(inode);
51 if (!root) {
52 pr_debug("%s: could not get root dentry!\n", __func__);
53 return -ENOMEM;
54 }
Tejun Heo324a56e2013-12-11 14:11:53 -050055 kernfs_get(info->root->kn);
56 root->d_fsdata = info->root->kn;
Tejun Heofa736a92013-11-28 14:54:44 -050057 sb->s_root = root;
Tejun Heoa797bfc2013-12-11 14:11:57 -050058 sb->s_d_op = &kernfs_dops;
Tejun Heofa736a92013-11-28 14:54:44 -050059 return 0;
60}
61
Tejun Heoc637b8a2013-12-11 14:11:58 -050062static int kernfs_test_super(struct super_block *sb, void *data)
Tejun Heofa736a92013-11-28 14:54:44 -050063{
Tejun Heoc525aad2013-12-11 14:11:55 -050064 struct kernfs_super_info *sb_info = kernfs_info(sb);
65 struct kernfs_super_info *info = data;
Tejun Heofa736a92013-11-28 14:54:44 -050066
67 return sb_info->root == info->root && sb_info->ns == info->ns;
68}
69
Tejun Heoc637b8a2013-12-11 14:11:58 -050070static int kernfs_set_super(struct super_block *sb, void *data)
Tejun Heofa736a92013-11-28 14:54:44 -050071{
72 int error;
73 error = set_anon_super(sb, data);
74 if (!error)
75 sb->s_fs_info = data;
76 return error;
77}
78
79/**
80 * kernfs_super_ns - determine the namespace tag of a kernfs super_block
81 * @sb: super_block of interest
82 *
83 * Return the namespace tag associated with kernfs super_block @sb.
84 */
85const void *kernfs_super_ns(struct super_block *sb)
86{
Tejun Heoc525aad2013-12-11 14:11:55 -050087 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heofa736a92013-11-28 14:54:44 -050088
89 return info->ns;
90}
91
92/**
93 * kernfs_mount_ns - kernfs mount helper
94 * @fs_type: file_system_type of the fs being mounted
95 * @flags: mount flags specified for the mount
96 * @root: kernfs_root of the hierarchy being mounted
Li Zefanfed95ba2014-02-25 19:28:44 +080097 * @new_sb_created: tell the caller if we allocated a new superblock
Tejun Heofa736a92013-11-28 14:54:44 -050098 * @ns: optional namespace tag of the mount
99 *
100 * This is to be called from each kernfs user's file_system_type->mount()
101 * implementation, which should pass through the specified @fs_type and
102 * @flags, and specify the hierarchy and namespace tag to mount via @root
103 * and @ns, respectively.
104 *
105 * The return value can be passed to the vfs layer verbatim.
106 */
107struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
Li Zefanfed95ba2014-02-25 19:28:44 +0800108 struct kernfs_root *root, bool *new_sb_created,
109 const void *ns)
Tejun Heofa736a92013-11-28 14:54:44 -0500110{
111 struct super_block *sb;
Tejun Heoc525aad2013-12-11 14:11:55 -0500112 struct kernfs_super_info *info;
Tejun Heofa736a92013-11-28 14:54:44 -0500113 int error;
114
115 info = kzalloc(sizeof(*info), GFP_KERNEL);
116 if (!info)
117 return ERR_PTR(-ENOMEM);
118
119 info->root = root;
120 info->ns = ns;
121
Tejun Heoc637b8a2013-12-11 14:11:58 -0500122 sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
Tejun Heofa736a92013-11-28 14:54:44 -0500123 if (IS_ERR(sb) || sb->s_fs_info != info)
124 kfree(info);
125 if (IS_ERR(sb))
126 return ERR_CAST(sb);
Li Zefanfed95ba2014-02-25 19:28:44 +0800127
128 if (new_sb_created)
129 *new_sb_created = !sb->s_root;
130
Tejun Heofa736a92013-11-28 14:54:44 -0500131 if (!sb->s_root) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500132 error = kernfs_fill_super(sb);
Tejun Heofa736a92013-11-28 14:54:44 -0500133 if (error) {
134 deactivate_locked_super(sb);
135 return ERR_PTR(error);
136 }
137 sb->s_flags |= MS_ACTIVE;
138 }
139
140 return dget(sb->s_root);
141}
142
143/**
144 * kernfs_kill_sb - kill_sb for kernfs
145 * @sb: super_block being killed
146 *
147 * This can be used directly for file_system_type->kill_sb(). If a kernfs
148 * user needs extra cleanup, it can implement its own kill_sb() and call
149 * this function at the end.
150 */
151void kernfs_kill_sb(struct super_block *sb)
152{
Tejun Heoc525aad2013-12-11 14:11:55 -0500153 struct kernfs_super_info *info = kernfs_info(sb);
Tejun Heo324a56e2013-12-11 14:11:53 -0500154 struct kernfs_node *root_kn = sb->s_root->d_fsdata;
Tejun Heofa736a92013-11-28 14:54:44 -0500155
156 /*
157 * Remove the superblock from fs_supers/s_instances
Tejun Heoc525aad2013-12-11 14:11:55 -0500158 * so we can't find it, before freeing kernfs_super_info.
Tejun Heofa736a92013-11-28 14:54:44 -0500159 */
160 kill_anon_super(sb);
161 kfree(info);
Tejun Heo324a56e2013-12-11 14:11:53 -0500162 kernfs_put(root_kn);
Tejun Heofa736a92013-11-28 14:54:44 -0500163}
164
165void __init kernfs_init(void)
166{
Tejun Heoa797bfc2013-12-11 14:11:57 -0500167 kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
Tejun Heo324a56e2013-12-11 14:11:53 -0500168 sizeof(struct kernfs_node),
Tejun Heofa736a92013-11-28 14:54:44 -0500169 0, SLAB_PANIC, NULL);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500170 kernfs_inode_init();
Tejun Heofa736a92013-11-28 14:54:44 -0500171}