1 // SPDX-License-Identifier: GPL-2.0
3 * devtmpfs - kernel-maintained tmpfs-based /dev
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 dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
67 const char *dev_name, void *data)
69 struct super_block *s = mnt->mnt_sb;
72 atomic_inc(&s->s_active);
73 down_write(&s->s_umount);
74 err = reconfigure_single(s, flags, data);
76 deactivate_locked_super(s);
79 return dget(s->s_root);
82 static struct file_system_type internal_fs_type = {
85 .init_fs_context = shmem_init_fs_context,
87 .init_fs_context = ramfs_init_fs_context,
89 .kill_sb = kill_litter_super,
92 static struct file_system_type dev_fs_type = {
94 .mount = public_dev_mount,
97 static int devtmpfs_submit_req(struct req *req, const char *tmp)
99 init_completion(&req->done);
101 spin_lock(&req_lock);
102 req->next = requests;
104 spin_unlock(&req_lock);
106 wake_up_process(thread);
107 wait_for_completion(&req->done);
114 int devtmpfs_create_node(struct device *dev)
116 const char *tmp = NULL;
123 req.uid = GLOBAL_ROOT_UID;
124 req.gid = GLOBAL_ROOT_GID;
125 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
131 if (is_blockdev(dev))
138 return devtmpfs_submit_req(&req, tmp);
141 int devtmpfs_delete_node(struct device *dev)
143 const char *tmp = NULL;
149 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
156 return devtmpfs_submit_req(&req, tmp);
159 static int dev_mkdir(const char *name, umode_t mode)
161 struct dentry *dentry;
165 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
167 return PTR_ERR(dentry);
169 err = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode);
171 /* mark as kernel-created inode */
172 d_inode(dentry)->i_private = &thread;
173 done_path_create(&path, dentry);
177 static int create_path(const char *nodepath)
183 /* parent directories do not exist, create them */
184 path = kstrdup(nodepath, GFP_KERNEL);
194 err = dev_mkdir(path, 0755);
195 if (err && err != -EEXIST)
204 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
205 kgid_t gid, struct device *dev)
207 struct dentry *dentry;
211 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
212 if (dentry == ERR_PTR(-ENOENT)) {
213 create_path(nodename);
214 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
217 return PTR_ERR(dentry);
219 err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
222 struct iattr newattrs;
224 newattrs.ia_mode = mode;
225 newattrs.ia_uid = uid;
226 newattrs.ia_gid = gid;
227 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
228 inode_lock(d_inode(dentry));
229 notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
230 inode_unlock(d_inode(dentry));
232 /* mark as kernel-created inode */
233 d_inode(dentry)->i_private = &thread;
235 done_path_create(&path, dentry);
239 static int dev_rmdir(const char *name)
242 struct dentry *dentry;
245 dentry = kern_path_locked(name, &parent);
247 return PTR_ERR(dentry);
248 if (d_really_is_positive(dentry)) {
249 if (d_inode(dentry)->i_private == &thread)
250 err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
258 inode_unlock(d_inode(parent.dentry));
263 static int delete_path(const char *nodepath)
268 path = kstrdup(nodepath, GFP_KERNEL);
275 base = strrchr(path, '/');
279 err = dev_rmdir(path);
288 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
290 /* did we create it */
291 if (inode->i_private != &thread)
294 /* does the dev_t match */
295 if (is_blockdev(dev)) {
296 if (!S_ISBLK(stat->mode))
299 if (!S_ISCHR(stat->mode))
302 if (stat->rdev != dev->devt)
309 static int handle_remove(const char *nodename, struct device *dev)
312 struct dentry *dentry;
316 dentry = kern_path_locked(nodename, &parent);
318 return PTR_ERR(dentry);
320 if (d_really_is_positive(dentry)) {
322 struct path p = {.mnt = parent.mnt, .dentry = dentry};
323 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
324 AT_STATX_SYNC_AS_STAT);
325 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
326 struct iattr newattrs;
328 * before unlinking this node, reset permissions
329 * of possible references like hardlinks
331 newattrs.ia_uid = GLOBAL_ROOT_UID;
332 newattrs.ia_gid = GLOBAL_ROOT_GID;
333 newattrs.ia_mode = stat.mode & ~0777;
335 ATTR_UID|ATTR_GID|ATTR_MODE;
336 inode_lock(d_inode(dentry));
337 notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
338 inode_unlock(d_inode(dentry));
339 err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
341 if (!err || err == -ENOENT)
348 inode_unlock(d_inode(parent.dentry));
351 if (deleted && strchr(nodename, '/'))
352 delete_path(nodename);
357 * If configured, or requested by the commandline, devtmpfs will be
358 * auto-mounted after the kernel mounted the root filesystem.
360 int __init devtmpfs_mount(void)
370 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
372 pr_info("error mounting %d\n", err);
374 pr_info("mounted\n");
378 static __initdata DECLARE_COMPLETION(setup_done);
380 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
384 return handle_create(name, mode, uid, gid, dev);
386 return handle_remove(name, dev);
389 static void __noreturn devtmpfs_work_loop(void)
392 spin_lock(&req_lock);
394 struct req *req = requests;
396 spin_unlock(&req_lock);
398 struct req *next = req->next;
399 req->err = handle(req->name, req->mode,
400 req->uid, req->gid, req->dev);
401 complete(&req->done);
404 spin_lock(&req_lock);
406 __set_current_state(TASK_INTERRUPTIBLE);
407 spin_unlock(&req_lock);
412 static noinline int __init devtmpfs_setup(void *p)
416 err = ksys_unshare(CLONE_NEWNS);
419 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
422 init_chdir("/.."); /* will traverse into overmounted root */
430 * The __ref is because devtmpfs_setup needs to be __init for the routines it
431 * calls. That call is done while devtmpfs_init, which is marked __init,
432 * synchronously waits for it to complete.
434 static int __ref devtmpfsd(void *p)
436 int err = devtmpfs_setup(p);
438 complete(&setup_done);
441 devtmpfs_work_loop();
446 * Create devtmpfs instance, driver-core devices will add their device
449 int __init devtmpfs_init(void)
451 char opts[] = "mode=0755";
454 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
456 pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
459 err = register_filesystem(&dev_fs_type);
461 pr_err("unable to register devtmpfs type %d\n", err);
465 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
466 if (!IS_ERR(thread)) {
467 wait_for_completion(&setup_done);
469 err = PTR_ERR(thread);
474 pr_err("unable to create devtmpfs %d\n", err);
475 unregister_filesystem(&dev_fs_type);
480 pr_info("initialized\n");