Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
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 | */ | |
fa736a95 TH |
10 | |
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 | ||
a797bfc3 | 20 | struct kmem_cache *kernfs_node_cache; |
fa736a95 | 21 | |
a797bfc3 | 22 | static const struct super_operations kernfs_sops = { |
fa736a95 TH |
23 | .statfs = simple_statfs, |
24 | .drop_inode = generic_delete_inode, | |
c637b8ac | 25 | .evict_inode = kernfs_evict_inode, |
fa736a95 TH |
26 | }; |
27 | ||
c637b8ac | 28 | static int kernfs_fill_super(struct super_block *sb) |
fa736a95 | 29 | { |
c525aadd | 30 | struct kernfs_super_info *info = kernfs_info(sb); |
fa736a95 TH |
31 | 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; | |
a797bfc3 | 37 | sb->s_op = &kernfs_sops; |
fa736a95 TH |
38 | sb->s_time_gran = 1; |
39 | ||
40 | /* get root inode, initialize and unlock it */ | |
a797bfc3 | 41 | mutex_lock(&kernfs_mutex); |
c637b8ac | 42 | inode = kernfs_get_inode(sb, info->root->kn); |
a797bfc3 | 43 | mutex_unlock(&kernfs_mutex); |
fa736a95 | 44 | if (!inode) { |
c637b8ac | 45 | pr_debug("kernfs: could not get root inode\n"); |
fa736a95 TH |
46 | 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 | } | |
324a56e1 TH |
55 | kernfs_get(info->root->kn); |
56 | root->d_fsdata = info->root->kn; | |
fa736a95 | 57 | sb->s_root = root; |
a797bfc3 | 58 | sb->s_d_op = &kernfs_dops; |
fa736a95 TH |
59 | return 0; |
60 | } | |
61 | ||
c637b8ac | 62 | static int kernfs_test_super(struct super_block *sb, void *data) |
fa736a95 | 63 | { |
c525aadd TH |
64 | struct kernfs_super_info *sb_info = kernfs_info(sb); |
65 | struct kernfs_super_info *info = data; | |
fa736a95 TH |
66 | |
67 | return sb_info->root == info->root && sb_info->ns == info->ns; | |
68 | } | |
69 | ||
c637b8ac | 70 | static int kernfs_set_super(struct super_block *sb, void *data) |
fa736a95 TH |
71 | { |
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 | */ | |
85 | const void *kernfs_super_ns(struct super_block *sb) | |
86 | { | |
c525aadd | 87 | struct kernfs_super_info *info = kernfs_info(sb); |
fa736a95 TH |
88 | |
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 | |
97 | * @ns: optional namespace tag of the mount | |
98 | * | |
99 | * This is to be called from each kernfs user's file_system_type->mount() | |
100 | * implementation, which should pass through the specified @fs_type and | |
101 | * @flags, and specify the hierarchy and namespace tag to mount via @root | |
102 | * and @ns, respectively. | |
103 | * | |
104 | * The return value can be passed to the vfs layer verbatim. | |
105 | */ | |
106 | struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, | |
107 | struct kernfs_root *root, const void *ns) | |
108 | { | |
109 | struct super_block *sb; | |
c525aadd | 110 | struct kernfs_super_info *info; |
fa736a95 TH |
111 | int error; |
112 | ||
113 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
114 | if (!info) | |
115 | return ERR_PTR(-ENOMEM); | |
116 | ||
117 | info->root = root; | |
118 | info->ns = ns; | |
119 | ||
c637b8ac | 120 | sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info); |
fa736a95 TH |
121 | if (IS_ERR(sb) || sb->s_fs_info != info) |
122 | kfree(info); | |
123 | if (IS_ERR(sb)) | |
124 | return ERR_CAST(sb); | |
125 | if (!sb->s_root) { | |
c637b8ac | 126 | error = kernfs_fill_super(sb); |
fa736a95 TH |
127 | if (error) { |
128 | deactivate_locked_super(sb); | |
129 | return ERR_PTR(error); | |
130 | } | |
131 | sb->s_flags |= MS_ACTIVE; | |
132 | } | |
133 | ||
134 | return dget(sb->s_root); | |
135 | } | |
136 | ||
137 | /** | |
138 | * kernfs_kill_sb - kill_sb for kernfs | |
139 | * @sb: super_block being killed | |
140 | * | |
141 | * This can be used directly for file_system_type->kill_sb(). If a kernfs | |
142 | * user needs extra cleanup, it can implement its own kill_sb() and call | |
143 | * this function at the end. | |
144 | */ | |
145 | void kernfs_kill_sb(struct super_block *sb) | |
146 | { | |
c525aadd | 147 | struct kernfs_super_info *info = kernfs_info(sb); |
324a56e1 | 148 | struct kernfs_node *root_kn = sb->s_root->d_fsdata; |
fa736a95 TH |
149 | |
150 | /* | |
151 | * Remove the superblock from fs_supers/s_instances | |
c525aadd | 152 | * so we can't find it, before freeing kernfs_super_info. |
fa736a95 TH |
153 | */ |
154 | kill_anon_super(sb); | |
155 | kfree(info); | |
324a56e1 | 156 | kernfs_put(root_kn); |
fa736a95 TH |
157 | } |
158 | ||
159 | void __init kernfs_init(void) | |
160 | { | |
a797bfc3 | 161 | kernfs_node_cache = kmem_cache_create("kernfs_node_cache", |
324a56e1 | 162 | sizeof(struct kernfs_node), |
fa736a95 | 163 | 0, SLAB_PANIC, NULL); |
c637b8ac | 164 | kernfs_inode_init(); |
fa736a95 | 165 | } |