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 #ifdef CONFIG_DEVTMPFS_SAFE
33 #define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
35 #define DEVTMPFS_MFLAGS (MS_SILENT)
38 static struct task_struct *thread;
40 static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
42 static DEFINE_SPINLOCK(req_lock);
46 struct completion done;
49 umode_t mode; /* 0 => delete */
55 static int __init mount_param(char *str)
57 mount_dev = simple_strtoul(str, NULL, 0);
60 __setup("devtmpfs.mount=", mount_param);
62 static struct vfsmount *mnt;
64 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
65 const char *dev_name, void *data)
67 struct super_block *s = mnt->mnt_sb;
70 atomic_inc(&s->s_active);
71 down_write(&s->s_umount);
72 err = reconfigure_single(s, flags, data);
74 deactivate_locked_super(s);
77 return dget(s->s_root);
80 static struct file_system_type internal_fs_type = {
83 .init_fs_context = shmem_init_fs_context,
84 .parameters = shmem_fs_parameters,
86 .init_fs_context = ramfs_init_fs_context,
87 .parameters = ramfs_fs_parameters,
89 .kill_sb = kill_litter_super,
92 static struct file_system_type dev_fs_type = {
94 .mount = public_dev_mount,
98 static inline int is_blockdev(struct device *dev)
100 return dev->class == &block_class;
103 static inline int is_blockdev(struct device *dev) { return 0; }
106 static int devtmpfs_submit_req(struct req *req, const char *tmp)
108 init_completion(&req->done);
110 spin_lock(&req_lock);
111 req->next = requests;
113 spin_unlock(&req_lock);
115 wake_up_process(thread);
116 wait_for_completion(&req->done);
123 int devtmpfs_create_node(struct device *dev)
125 const char *tmp = NULL;
132 req.uid = GLOBAL_ROOT_UID;
133 req.gid = GLOBAL_ROOT_GID;
134 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
140 if (is_blockdev(dev))
147 return devtmpfs_submit_req(&req, tmp);
150 int devtmpfs_delete_node(struct device *dev)
152 const char *tmp = NULL;
158 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
165 return devtmpfs_submit_req(&req, tmp);
168 static int dev_mkdir(const char *name, umode_t mode)
170 struct dentry *dentry;
174 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
176 return PTR_ERR(dentry);
178 err = vfs_mkdir(&init_user_ns, d_inode(path.dentry), dentry, mode);
180 /* mark as kernel-created inode */
181 d_inode(dentry)->i_private = &thread;
182 done_path_create(&path, dentry);
186 static int create_path(const char *nodepath)
192 /* parent directories do not exist, create them */
193 path = kstrdup(nodepath, GFP_KERNEL);
203 err = dev_mkdir(path, 0755);
204 if (err && err != -EEXIST)
213 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
214 kgid_t gid, struct device *dev)
216 struct dentry *dentry;
220 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
221 if (dentry == ERR_PTR(-ENOENT)) {
222 create_path(nodename);
223 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
226 return PTR_ERR(dentry);
228 err = vfs_mknod(&init_user_ns, d_inode(path.dentry), dentry, mode,
231 struct iattr newattrs;
233 newattrs.ia_mode = mode;
234 newattrs.ia_uid = uid;
235 newattrs.ia_gid = gid;
236 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
237 inode_lock(d_inode(dentry));
238 notify_change(&init_user_ns, dentry, &newattrs, NULL);
239 inode_unlock(d_inode(dentry));
241 /* mark as kernel-created inode */
242 d_inode(dentry)->i_private = &thread;
244 done_path_create(&path, dentry);
248 static int dev_rmdir(const char *name)
251 struct dentry *dentry;
254 dentry = kern_path_locked(name, &parent);
256 return PTR_ERR(dentry);
257 if (d_really_is_positive(dentry)) {
258 if (d_inode(dentry)->i_private == &thread)
259 err = vfs_rmdir(&init_user_ns, d_inode(parent.dentry),
267 inode_unlock(d_inode(parent.dentry));
272 static int delete_path(const char *nodepath)
277 path = kstrdup(nodepath, GFP_KERNEL);
284 base = strrchr(path, '/');
288 err = dev_rmdir(path);
297 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
299 /* did we create it */
300 if (inode->i_private != &thread)
303 /* does the dev_t match */
304 if (is_blockdev(dev)) {
305 if (!S_ISBLK(stat->mode))
308 if (!S_ISCHR(stat->mode))
311 if (stat->rdev != dev->devt)
318 static int handle_remove(const char *nodename, struct device *dev)
321 struct dentry *dentry;
325 dentry = kern_path_locked(nodename, &parent);
327 return PTR_ERR(dentry);
329 if (d_really_is_positive(dentry)) {
331 struct path p = {.mnt = parent.mnt, .dentry = dentry};
332 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
333 AT_STATX_SYNC_AS_STAT);
334 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
335 struct iattr newattrs;
337 * before unlinking this node, reset permissions
338 * of possible references like hardlinks
340 newattrs.ia_uid = GLOBAL_ROOT_UID;
341 newattrs.ia_gid = GLOBAL_ROOT_GID;
342 newattrs.ia_mode = stat.mode & ~0777;
344 ATTR_UID|ATTR_GID|ATTR_MODE;
345 inode_lock(d_inode(dentry));
346 notify_change(&init_user_ns, dentry, &newattrs, NULL);
347 inode_unlock(d_inode(dentry));
348 err = vfs_unlink(&init_user_ns, d_inode(parent.dentry),
350 if (!err || err == -ENOENT)
357 inode_unlock(d_inode(parent.dentry));
360 if (deleted && strchr(nodename, '/'))
361 delete_path(nodename);
366 * If configured, or requested by the commandline, devtmpfs will be
367 * auto-mounted after the kernel mounted the root filesystem.
369 int __init devtmpfs_mount(void)
379 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
381 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
383 printk(KERN_INFO "devtmpfs: mounted\n");
387 static __initdata DECLARE_COMPLETION(setup_done);
389 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
393 return handle_create(name, mode, uid, gid, dev);
395 return handle_remove(name, dev);
398 static void __noreturn devtmpfs_work_loop(void)
401 spin_lock(&req_lock);
403 struct req *req = requests;
405 spin_unlock(&req_lock);
407 struct req *next = req->next;
408 req->err = handle(req->name, req->mode,
409 req->uid, req->gid, req->dev);
410 complete(&req->done);
413 spin_lock(&req_lock);
415 __set_current_state(TASK_INTERRUPTIBLE);
416 spin_unlock(&req_lock);
421 static noinline int __init devtmpfs_setup(void *p)
425 err = ksys_unshare(CLONE_NEWNS);
428 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
431 init_chdir("/.."); /* will traverse into overmounted root */
439 * The __ref is because devtmpfs_setup needs to be __init for the routines it
440 * calls. That call is done while devtmpfs_init, which is marked __init,
441 * synchronously waits for it to complete.
443 static int __ref devtmpfsd(void *p)
445 int err = devtmpfs_setup(p);
447 complete(&setup_done);
450 devtmpfs_work_loop();
455 * Create devtmpfs instance, driver-core devices will add their device
458 int __init devtmpfs_init(void)
460 char opts[] = "mode=0755";
463 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
465 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
469 err = register_filesystem(&dev_fs_type);
471 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
476 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
477 if (!IS_ERR(thread)) {
478 wait_for_completion(&setup_done);
480 err = PTR_ERR(thread);
485 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
486 unregister_filesystem(&dev_fs_type);
490 printk(KERN_INFO "devtmpfs: initialized\n");