1 // SPDX-License-Identifier: GPL-2.0
3 * devtmpfs - kernel-maintained tmpfs-based /dev
5 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
7 * During bootup, before any driver core device is registered,
8 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
9 * device which requests a device node, will add a node in this
11 * By default, all devices are named after the name of the device,
12 * owned by root and have a default mode of 0600. Subsystems can
13 * overwrite the default setting if needed.
16 #include <linux/kernel.h>
17 #include <linux/syscalls.h>
18 #include <linux/mount.h>
19 #include <linux/device.h>
20 #include <linux/genhd.h>
21 #include <linux/namei.h>
23 #include <linux/shmem_fs.h>
24 #include <linux/ramfs.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/kthread.h>
28 #include <linux/init_syscalls.h>
29 #include <uapi/linux/mount.h>
32 static struct task_struct *thread;
34 static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
36 static DEFINE_SPINLOCK(req_lock);
40 struct completion done;
43 umode_t mode; /* 0 => delete */
49 static int __init mount_param(char *str)
51 mount_dev = simple_strtoul(str, NULL, 0);
54 __setup("devtmpfs.mount=", mount_param);
56 static struct vfsmount *mnt;
58 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
59 const char *dev_name, void *data)
61 struct super_block *s = mnt->mnt_sb;
64 atomic_inc(&s->s_active);
65 down_write(&s->s_umount);
66 err = reconfigure_single(s, flags, data);
68 deactivate_locked_super(s);
71 return dget(s->s_root);
74 static struct file_system_type internal_fs_type = {
77 .init_fs_context = shmem_init_fs_context,
78 .parameters = shmem_fs_parameters,
80 .init_fs_context = ramfs_init_fs_context,
81 .parameters = ramfs_fs_parameters,
83 .kill_sb = kill_litter_super,
86 static struct file_system_type dev_fs_type = {
88 .mount = public_dev_mount,
92 static inline int is_blockdev(struct device *dev)
94 return dev->class == &block_class;
97 static inline int is_blockdev(struct device *dev) { return 0; }
100 static int devtmpfs_submit_req(struct req *req, const char *tmp)
102 init_completion(&req->done);
104 spin_lock(&req_lock);
105 req->next = requests;
107 spin_unlock(&req_lock);
109 wake_up_process(thread);
110 wait_for_completion(&req->done);
117 int devtmpfs_create_node(struct device *dev)
119 const char *tmp = NULL;
126 req.uid = GLOBAL_ROOT_UID;
127 req.gid = GLOBAL_ROOT_GID;
128 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
134 if (is_blockdev(dev))
141 return devtmpfs_submit_req(&req, tmp);
144 int devtmpfs_delete_node(struct device *dev)
146 const char *tmp = NULL;
152 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
159 return devtmpfs_submit_req(&req, tmp);
162 static int dev_mkdir(const char *name, umode_t mode)
164 struct dentry *dentry;
168 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
170 return PTR_ERR(dentry);
172 err = vfs_mkdir(&init_user_ns, d_inode(path.dentry), dentry, mode);
174 /* mark as kernel-created inode */
175 d_inode(dentry)->i_private = &thread;
176 done_path_create(&path, dentry);
180 static int create_path(const char *nodepath)
186 /* parent directories do not exist, create them */
187 path = kstrdup(nodepath, GFP_KERNEL);
197 err = dev_mkdir(path, 0755);
198 if (err && err != -EEXIST)
207 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
208 kgid_t gid, struct device *dev)
210 struct dentry *dentry;
214 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
215 if (dentry == ERR_PTR(-ENOENT)) {
216 create_path(nodename);
217 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
220 return PTR_ERR(dentry);
222 err = vfs_mknod(&init_user_ns, d_inode(path.dentry), dentry, mode,
225 struct iattr newattrs;
227 newattrs.ia_mode = mode;
228 newattrs.ia_uid = uid;
229 newattrs.ia_gid = gid;
230 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
231 inode_lock(d_inode(dentry));
232 notify_change(&init_user_ns, dentry, &newattrs, NULL);
233 inode_unlock(d_inode(dentry));
235 /* mark as kernel-created inode */
236 d_inode(dentry)->i_private = &thread;
238 done_path_create(&path, dentry);
242 static int dev_rmdir(const char *name)
245 struct dentry *dentry;
248 dentry = kern_path_locked(name, &parent);
250 return PTR_ERR(dentry);
251 if (d_really_is_positive(dentry)) {
252 if (d_inode(dentry)->i_private == &thread)
253 err = vfs_rmdir(&init_user_ns, d_inode(parent.dentry),
261 inode_unlock(d_inode(parent.dentry));
266 static int delete_path(const char *nodepath)
271 path = kstrdup(nodepath, GFP_KERNEL);
278 base = strrchr(path, '/');
282 err = dev_rmdir(path);
291 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
293 /* did we create it */
294 if (inode->i_private != &thread)
297 /* does the dev_t match */
298 if (is_blockdev(dev)) {
299 if (!S_ISBLK(stat->mode))
302 if (!S_ISCHR(stat->mode))
305 if (stat->rdev != dev->devt)
312 static int handle_remove(const char *nodename, struct device *dev)
315 struct dentry *dentry;
319 dentry = kern_path_locked(nodename, &parent);
321 return PTR_ERR(dentry);
323 if (d_really_is_positive(dentry)) {
325 struct path p = {.mnt = parent.mnt, .dentry = dentry};
326 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
327 AT_STATX_SYNC_AS_STAT);
328 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
329 struct iattr newattrs;
331 * before unlinking this node, reset permissions
332 * of possible references like hardlinks
334 newattrs.ia_uid = GLOBAL_ROOT_UID;
335 newattrs.ia_gid = GLOBAL_ROOT_GID;
336 newattrs.ia_mode = stat.mode & ~0777;
338 ATTR_UID|ATTR_GID|ATTR_MODE;
339 inode_lock(d_inode(dentry));
340 notify_change(&init_user_ns, dentry, &newattrs, NULL);
341 inode_unlock(d_inode(dentry));
342 err = vfs_unlink(&init_user_ns, d_inode(parent.dentry),
344 if (!err || err == -ENOENT)
351 inode_unlock(d_inode(parent.dentry));
354 if (deleted && strchr(nodename, '/'))
355 delete_path(nodename);
360 * If configured, or requested by the commandline, devtmpfs will be
361 * auto-mounted after the kernel mounted the root filesystem.
363 int __init devtmpfs_mount(void)
373 err = init_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
375 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
377 printk(KERN_INFO "devtmpfs: mounted\n");
381 static __initdata DECLARE_COMPLETION(setup_done);
383 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
387 return handle_create(name, mode, uid, gid, dev);
389 return handle_remove(name, dev);
392 static void __noreturn devtmpfs_work_loop(void)
395 spin_lock(&req_lock);
397 struct req *req = requests;
399 spin_unlock(&req_lock);
401 struct req *next = req->next;
402 req->err = handle(req->name, req->mode,
403 req->uid, req->gid, req->dev);
404 complete(&req->done);
407 spin_lock(&req_lock);
409 __set_current_state(TASK_INTERRUPTIBLE);
410 spin_unlock(&req_lock);
415 static noinline int __init devtmpfs_setup(void *p)
419 err = ksys_unshare(CLONE_NEWNS);
422 err = init_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
425 init_chdir("/.."); /* will traverse into overmounted root */
433 * The __ref is because devtmpfs_setup needs to be __init for the routines it
434 * calls. That call is done while devtmpfs_init, which is marked __init,
435 * synchronously waits for it to complete.
437 static int __ref devtmpfsd(void *p)
439 int err = devtmpfs_setup(p);
441 complete(&setup_done);
444 devtmpfs_work_loop();
449 * Create devtmpfs instance, driver-core devices will add their device
452 int __init devtmpfs_init(void)
454 char opts[] = "mode=0755";
457 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
459 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
463 err = register_filesystem(&dev_fs_type);
465 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
470 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
471 if (!IS_ERR(thread)) {
472 wait_for_completion(&setup_done);
474 err = PTR_ERR(thread);
479 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
480 unregister_filesystem(&dev_fs_type);
484 printk(KERN_INFO "devtmpfs: initialized\n");