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 #define pr_fmt(fmt) "devtmpfs: " fmt
18 #include <linux/kernel.h>
19 #include <linux/syscalls.h>
20 #include <linux/mount.h>
21 #include <linux/device.h>
22 #include <linux/blkdev.h>
23 #include <linux/namei.h>
25 #include <linux/shmem_fs.h>
26 #include <linux/ramfs.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/kthread.h>
30 #include <linux/init_syscalls.h>
31 #include <uapi/linux/mount.h>
34 #ifdef CONFIG_DEVTMPFS_SAFE
35 #define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
37 #define DEVTMPFS_MFLAGS (MS_SILENT)
40 static struct task_struct *thread;
42 static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
44 static DEFINE_SPINLOCK(req_lock);
48 struct completion done;
51 umode_t mode; /* 0 => delete */
57 static int __init mount_param(char *str)
59 mount_dev = simple_strtoul(str, NULL, 0);
62 __setup("devtmpfs.mount=", mount_param);
64 static struct vfsmount *mnt;
66 static struct file_system_type internal_fs_type = {
69 .init_fs_context = shmem_init_fs_context,
71 .init_fs_context = ramfs_init_fs_context,
73 .kill_sb = kill_litter_super,
76 /* Simply take a ref on the existing mount */
77 static int devtmpfs_get_tree(struct fs_context *fc)
79 struct super_block *sb = mnt->mnt_sb;
81 atomic_inc(&sb->s_active);
82 down_write(&sb->s_umount);
83 fc->root = dget(sb->s_root);
87 /* Ops are filled in during init depending on underlying shmem or ramfs type */
88 struct fs_context_operations devtmpfs_context_ops = {};
90 /* Call the underlying initialization and set to our ops */
91 static int devtmpfs_init_fs_context(struct fs_context *fc)
95 ret = shmem_init_fs_context(fc);
97 ret = ramfs_init_fs_context(fc);
102 fc->ops = &devtmpfs_context_ops;
107 static struct file_system_type dev_fs_type = {
109 .init_fs_context = devtmpfs_init_fs_context,
112 static int devtmpfs_submit_req(struct req *req, const char *tmp)
114 init_completion(&req->done);
116 spin_lock(&req_lock);
117 req->next = requests;
119 spin_unlock(&req_lock);
121 wake_up_process(thread);
122 wait_for_completion(&req->done);
129 int devtmpfs_create_node(struct device *dev)
131 const char *tmp = NULL;
138 req.uid = GLOBAL_ROOT_UID;
139 req.gid = GLOBAL_ROOT_GID;
140 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
146 if (is_blockdev(dev))
153 return devtmpfs_submit_req(&req, tmp);
156 int devtmpfs_delete_node(struct device *dev)
158 const char *tmp = NULL;
164 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
171 return devtmpfs_submit_req(&req, tmp);
174 static int dev_mkdir(const char *name, umode_t mode)
176 struct dentry *dentry;
179 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
181 return PTR_ERR(dentry);
183 dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode);
185 /* mark as kernel-created inode */
186 d_inode(dentry)->i_private = &thread;
187 done_path_create(&path, dentry);
188 return PTR_ERR_OR_ZERO(dentry);
191 static int create_path(const char *nodepath)
197 /* parent directories do not exist, create them */
198 path = kstrdup(nodepath, GFP_KERNEL);
208 err = dev_mkdir(path, 0755);
209 if (err && err != -EEXIST)
218 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
219 kgid_t gid, struct device *dev)
221 struct dentry *dentry;
225 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
226 if (dentry == ERR_PTR(-ENOENT)) {
227 create_path(nodename);
228 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
231 return PTR_ERR(dentry);
233 err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
236 struct iattr newattrs;
238 newattrs.ia_mode = mode;
239 newattrs.ia_uid = uid;
240 newattrs.ia_gid = gid;
241 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
242 inode_lock(d_inode(dentry));
243 notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
244 inode_unlock(d_inode(dentry));
246 /* mark as kernel-created inode */
247 d_inode(dentry)->i_private = &thread;
249 done_path_create(&path, dentry);
253 static int dev_rmdir(const char *name)
256 struct dentry *dentry;
259 dentry = kern_path_locked(name, &parent);
261 return PTR_ERR(dentry);
262 if (d_inode(dentry)->i_private == &thread)
263 err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
269 inode_unlock(d_inode(parent.dentry));
274 static int delete_path(const char *nodepath)
279 path = kstrdup(nodepath, GFP_KERNEL);
286 base = strrchr(path, '/');
290 err = dev_rmdir(path);
299 static int dev_mynode(struct device *dev, struct inode *inode)
301 /* did we create it */
302 if (inode->i_private != &thread)
305 /* does the dev_t match */
306 if (is_blockdev(dev)) {
307 if (!S_ISBLK(inode->i_mode))
310 if (!S_ISCHR(inode->i_mode))
313 if (inode->i_rdev != dev->devt)
320 static int handle_remove(const char *nodename, struct device *dev)
323 struct dentry *dentry;
328 dentry = kern_path_locked(nodename, &parent);
330 return PTR_ERR(dentry);
332 inode = d_inode(dentry);
333 if (dev_mynode(dev, inode)) {
334 struct iattr newattrs;
336 * before unlinking this node, reset permissions
337 * of possible references like hardlinks
339 newattrs.ia_uid = GLOBAL_ROOT_UID;
340 newattrs.ia_gid = GLOBAL_ROOT_GID;
341 newattrs.ia_mode = inode->i_mode & ~0777;
343 ATTR_UID|ATTR_GID|ATTR_MODE;
344 inode_lock(d_inode(dentry));
345 notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
346 inode_unlock(d_inode(dentry));
347 err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
349 if (!err || err == -ENOENT)
353 inode_unlock(d_inode(parent.dentry));
356 if (deleted && strchr(nodename, '/'))
357 delete_path(nodename);
362 * If configured, or requested by the commandline, devtmpfs will be
363 * auto-mounted after the kernel mounted the root filesystem.
365 int __init devtmpfs_mount(void)
375 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
377 pr_info("error mounting %d\n", err);
379 pr_info("mounted\n");
383 static __initdata DECLARE_COMPLETION(setup_done);
385 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
389 return handle_create(name, mode, uid, gid, dev);
391 return handle_remove(name, dev);
394 static void __noreturn devtmpfs_work_loop(void)
397 spin_lock(&req_lock);
399 struct req *req = requests;
401 spin_unlock(&req_lock);
403 struct req *next = req->next;
404 req->err = handle(req->name, req->mode,
405 req->uid, req->gid, req->dev);
406 complete(&req->done);
409 spin_lock(&req_lock);
411 __set_current_state(TASK_INTERRUPTIBLE);
412 spin_unlock(&req_lock);
417 static noinline int __init devtmpfs_setup(void *p)
421 err = ksys_unshare(CLONE_NEWNS);
424 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
427 init_chdir("/.."); /* will traverse into overmounted root */
435 * The __ref is because devtmpfs_setup needs to be __init for the routines it
436 * calls. That call is done while devtmpfs_init, which is marked __init,
437 * synchronously waits for it to complete.
439 static int __ref devtmpfsd(void *p)
441 int err = devtmpfs_setup(p);
443 complete(&setup_done);
446 devtmpfs_work_loop();
451 * Get the underlying (shmem/ramfs) context ops to build ours
453 static int devtmpfs_configure_context(void)
455 struct fs_context *fc;
457 fc = fs_context_for_reconfigure(mnt->mnt_root, mnt->mnt_sb->s_flags,
462 /* Set up devtmpfs_context_ops based on underlying type */
463 devtmpfs_context_ops.free = fc->ops->free;
464 devtmpfs_context_ops.dup = fc->ops->dup;
465 devtmpfs_context_ops.parse_param = fc->ops->parse_param;
466 devtmpfs_context_ops.parse_monolithic = fc->ops->parse_monolithic;
467 devtmpfs_context_ops.get_tree = &devtmpfs_get_tree;
468 devtmpfs_context_ops.reconfigure = fc->ops->reconfigure;
476 * Create devtmpfs instance, driver-core devices will add their device
479 int __init devtmpfs_init(void)
481 char opts[] = "mode=0755";
484 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
486 pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
490 err = devtmpfs_configure_context();
492 pr_err("unable to configure devtmpfs type %d\n", err);
496 err = register_filesystem(&dev_fs_type);
498 pr_err("unable to register devtmpfs type %d\n", err);
502 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
503 if (!IS_ERR(thread)) {
504 wait_for_completion(&setup_done);
506 err = PTR_ERR(thread);
511 pr_err("unable to create devtmpfs %d\n", err);
512 unregister_filesystem(&dev_fs_type);
517 pr_info("initialized\n");