]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/base/devtmpfs.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[mirror_ubuntu-bionic-kernel.git] / drivers / base / devtmpfs.c
1 /*
2 * devtmpfs - kernel-maintained tmpfs-based /dev
3 *
4 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
5 *
6 * During bootup, before any driver core device is registered,
7 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
8 * device which requests a device node, will add a node in this
9 * filesystem.
10 * By default, all devices are named after the the name of the
11 * device, owned by root and have a default mode of 0600. Subsystems
12 * can overwrite the default setting if needed.
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/syscalls.h>
17 #include <linux/mount.h>
18 #include <linux/device.h>
19 #include <linux/genhd.h>
20 #include <linux/namei.h>
21 #include <linux/fs.h>
22 #include <linux/shmem_fs.h>
23 #include <linux/cred.h>
24 #include <linux/sched.h>
25 #include <linux/init_task.h>
26
27 static struct vfsmount *dev_mnt;
28
29 #if defined CONFIG_DEVTMPFS_MOUNT
30 static int dev_mount = 1;
31 #else
32 static int dev_mount;
33 #endif
34
35 static DEFINE_MUTEX(dirlock);
36
37 static int __init mount_param(char *str)
38 {
39 dev_mount = simple_strtoul(str, NULL, 0);
40 return 1;
41 }
42 __setup("devtmpfs.mount=", mount_param);
43
44 static int dev_get_sb(struct file_system_type *fs_type, int flags,
45 const char *dev_name, void *data, struct vfsmount *mnt)
46 {
47 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
48 }
49
50 static struct file_system_type dev_fs_type = {
51 .name = "devtmpfs",
52 .get_sb = dev_get_sb,
53 .kill_sb = kill_litter_super,
54 };
55
56 #ifdef CONFIG_BLOCK
57 static inline int is_blockdev(struct device *dev)
58 {
59 return dev->class == &block_class;
60 }
61 #else
62 static inline int is_blockdev(struct device *dev) { return 0; }
63 #endif
64
65 static int dev_mkdir(const char *name, mode_t mode)
66 {
67 struct nameidata nd;
68 struct dentry *dentry;
69 int err;
70
71 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
72 name, LOOKUP_PARENT, &nd);
73 if (err)
74 return err;
75
76 dentry = lookup_create(&nd, 1);
77 if (!IS_ERR(dentry)) {
78 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
79 if (!err)
80 /* mark as kernel-created inode */
81 dentry->d_inode->i_private = &dev_mnt;
82 dput(dentry);
83 } else {
84 err = PTR_ERR(dentry);
85 }
86
87 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
88 path_put(&nd.path);
89 return err;
90 }
91
92 static int create_path(const char *nodepath)
93 {
94 int err;
95
96 mutex_lock(&dirlock);
97 err = dev_mkdir(nodepath, 0755);
98 if (err == -ENOENT) {
99 char *path;
100 char *s;
101
102 /* parent directories do not exist, create them */
103 path = kstrdup(nodepath, GFP_KERNEL);
104 if (!path) {
105 err = -ENOMEM;
106 goto out;
107 }
108 s = path;
109 for (;;) {
110 s = strchr(s, '/');
111 if (!s)
112 break;
113 s[0] = '\0';
114 err = dev_mkdir(path, 0755);
115 if (err && err != -EEXIST)
116 break;
117 s[0] = '/';
118 s++;
119 }
120 kfree(path);
121 }
122 out:
123 mutex_unlock(&dirlock);
124 return err;
125 }
126
127 int devtmpfs_create_node(struct device *dev)
128 {
129 const char *tmp = NULL;
130 const char *nodename;
131 const struct cred *curr_cred;
132 mode_t mode = 0;
133 struct nameidata nd;
134 struct dentry *dentry;
135 int err;
136
137 if (!dev_mnt)
138 return 0;
139
140 nodename = device_get_devnode(dev, &mode, &tmp);
141 if (!nodename)
142 return -ENOMEM;
143
144 if (mode == 0)
145 mode = 0600;
146 if (is_blockdev(dev))
147 mode |= S_IFBLK;
148 else
149 mode |= S_IFCHR;
150
151 curr_cred = override_creds(&init_cred);
152
153 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
154 nodename, LOOKUP_PARENT, &nd);
155 if (err == -ENOENT) {
156 create_path(nodename);
157 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
158 nodename, LOOKUP_PARENT, &nd);
159 }
160 if (err)
161 goto out;
162
163 dentry = lookup_create(&nd, 0);
164 if (!IS_ERR(dentry)) {
165 err = vfs_mknod(nd.path.dentry->d_inode,
166 dentry, mode, dev->devt);
167 if (!err) {
168 struct iattr newattrs;
169
170 /* fixup possibly umasked mode */
171 newattrs.ia_mode = mode;
172 newattrs.ia_valid = ATTR_MODE;
173 mutex_lock(&dentry->d_inode->i_mutex);
174 notify_change(dentry, &newattrs);
175 mutex_unlock(&dentry->d_inode->i_mutex);
176
177 /* mark as kernel-created inode */
178 dentry->d_inode->i_private = &dev_mnt;
179 }
180 dput(dentry);
181 } else {
182 err = PTR_ERR(dentry);
183 }
184
185 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
186 path_put(&nd.path);
187 out:
188 kfree(tmp);
189 revert_creds(curr_cred);
190 return err;
191 }
192
193 static int dev_rmdir(const char *name)
194 {
195 struct nameidata nd;
196 struct dentry *dentry;
197 int err;
198
199 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
200 name, LOOKUP_PARENT, &nd);
201 if (err)
202 return err;
203
204 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
205 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
206 if (!IS_ERR(dentry)) {
207 if (dentry->d_inode) {
208 if (dentry->d_inode->i_private == &dev_mnt)
209 err = vfs_rmdir(nd.path.dentry->d_inode,
210 dentry);
211 else
212 err = -EPERM;
213 } else {
214 err = -ENOENT;
215 }
216 dput(dentry);
217 } else {
218 err = PTR_ERR(dentry);
219 }
220
221 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
222 path_put(&nd.path);
223 return err;
224 }
225
226 static int delete_path(const char *nodepath)
227 {
228 const char *path;
229 int err = 0;
230
231 path = kstrdup(nodepath, GFP_KERNEL);
232 if (!path)
233 return -ENOMEM;
234
235 mutex_lock(&dirlock);
236 for (;;) {
237 char *base;
238
239 base = strrchr(path, '/');
240 if (!base)
241 break;
242 base[0] = '\0';
243 err = dev_rmdir(path);
244 if (err)
245 break;
246 }
247 mutex_unlock(&dirlock);
248
249 kfree(path);
250 return err;
251 }
252
253 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
254 {
255 /* did we create it */
256 if (inode->i_private != &dev_mnt)
257 return 0;
258
259 /* does the dev_t match */
260 if (is_blockdev(dev)) {
261 if (!S_ISBLK(stat->mode))
262 return 0;
263 } else {
264 if (!S_ISCHR(stat->mode))
265 return 0;
266 }
267 if (stat->rdev != dev->devt)
268 return 0;
269
270 /* ours */
271 return 1;
272 }
273
274 int devtmpfs_delete_node(struct device *dev)
275 {
276 const char *tmp = NULL;
277 const char *nodename;
278 const struct cred *curr_cred;
279 struct nameidata nd;
280 struct dentry *dentry;
281 struct kstat stat;
282 int deleted = 1;
283 int err;
284
285 if (!dev_mnt)
286 return 0;
287
288 nodename = device_get_devnode(dev, NULL, &tmp);
289 if (!nodename)
290 return -ENOMEM;
291
292 curr_cred = override_creds(&init_cred);
293 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
294 nodename, LOOKUP_PARENT, &nd);
295 if (err)
296 goto out;
297
298 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
299 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
300 if (!IS_ERR(dentry)) {
301 if (dentry->d_inode) {
302 err = vfs_getattr(nd.path.mnt, dentry, &stat);
303 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
304 struct iattr newattrs;
305 /*
306 * before unlinking this node, reset permissions
307 * of possible references like hardlinks
308 */
309 newattrs.ia_uid = 0;
310 newattrs.ia_gid = 0;
311 newattrs.ia_mode = stat.mode & ~0777;
312 newattrs.ia_valid =
313 ATTR_UID|ATTR_GID|ATTR_MODE;
314 mutex_lock(&dentry->d_inode->i_mutex);
315 notify_change(dentry, &newattrs);
316 mutex_unlock(&dentry->d_inode->i_mutex);
317 err = vfs_unlink(nd.path.dentry->d_inode,
318 dentry);
319 if (!err || err == -ENOENT)
320 deleted = 1;
321 }
322 } else {
323 err = -ENOENT;
324 }
325 dput(dentry);
326 } else {
327 err = PTR_ERR(dentry);
328 }
329 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
330
331 path_put(&nd.path);
332 if (deleted && strchr(nodename, '/'))
333 delete_path(nodename);
334 out:
335 kfree(tmp);
336 revert_creds(curr_cred);
337 return err;
338 }
339
340 /*
341 * If configured, or requested by the commandline, devtmpfs will be
342 * auto-mounted after the kernel mounted the root filesystem.
343 */
344 int devtmpfs_mount(const char *mntdir)
345 {
346 int err;
347
348 if (!dev_mount)
349 return 0;
350
351 if (!dev_mnt)
352 return 0;
353
354 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
355 if (err)
356 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
357 else
358 printk(KERN_INFO "devtmpfs: mounted\n");
359 return err;
360 }
361
362 /*
363 * Create devtmpfs instance, driver-core devices will add their device
364 * nodes here.
365 */
366 int __init devtmpfs_init(void)
367 {
368 int err;
369 struct vfsmount *mnt;
370 char options[] = "mode=0755";
371
372 err = register_filesystem(&dev_fs_type);
373 if (err) {
374 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
375 "type %i\n", err);
376 return err;
377 }
378
379 mnt = kern_mount_data(&dev_fs_type, options);
380 if (IS_ERR(mnt)) {
381 err = PTR_ERR(mnt);
382 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
383 unregister_filesystem(&dev_fs_type);
384 return err;
385 }
386 dev_mnt = mnt;
387
388 printk(KERN_INFO "devtmpfs: initialized\n");
389 return 0;
390 }