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/fs_context.h>
29 #include <uapi/linux/mount.h>
32 static struct task_struct *thread;
34 #if defined CONFIG_DEVTMPFS_MOUNT
35 static int mount_dev = 1;
40 static DEFINE_SPINLOCK(req_lock);
44 struct completion done;
47 umode_t mode; /* 0 => delete */
53 static int __init mount_param(char *str)
55 mount_dev = simple_strtoul(str, NULL, 0);
58 __setup("devtmpfs.mount=", mount_param);
60 static struct vfsmount *mnt;
62 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
63 const char *dev_name, void *data)
65 struct super_block *s = mnt->mnt_sb;
68 atomic_inc(&s->s_active);
69 down_write(&s->s_umount);
70 err = reconfigure_single(s, flags, data);
72 deactivate_locked_super(s);
75 return dget(s->s_root);
78 static struct file_system_type internal_fs_type = {
81 .init_fs_context = shmem_init_fs_context,
82 .parameters = &shmem_fs_parameters,
84 .init_fs_context = ramfs_init_fs_context,
85 .parameters = &ramfs_fs_parameters,
87 .kill_sb = kill_litter_super,
90 static struct file_system_type dev_fs_type = {
92 .mount = public_dev_mount,
96 static inline int is_blockdev(struct device *dev)
98 return dev->class == &block_class;
101 static inline int is_blockdev(struct device *dev) { return 0; }
104 int devtmpfs_create_node(struct device *dev)
106 const char *tmp = NULL;
113 req.uid = GLOBAL_ROOT_UID;
114 req.gid = GLOBAL_ROOT_GID;
115 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
121 if (is_blockdev(dev))
128 init_completion(&req.done);
130 spin_lock(&req_lock);
133 spin_unlock(&req_lock);
135 wake_up_process(thread);
136 wait_for_completion(&req.done);
143 int devtmpfs_delete_node(struct device *dev)
145 const char *tmp = NULL;
151 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
158 init_completion(&req.done);
160 spin_lock(&req_lock);
163 spin_unlock(&req_lock);
165 wake_up_process(thread);
166 wait_for_completion(&req.done);
172 static int dev_mkdir(const char *name, umode_t mode)
174 struct dentry *dentry;
178 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
180 return PTR_ERR(dentry);
182 err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
184 /* mark as kernel-created inode */
185 d_inode(dentry)->i_private = &thread;
186 done_path_create(&path, dentry);
190 static int create_path(const char *nodepath)
196 /* parent directories do not exist, create them */
197 path = kstrdup(nodepath, GFP_KERNEL);
207 err = dev_mkdir(path, 0755);
208 if (err && err != -EEXIST)
217 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
218 kgid_t gid, struct device *dev)
220 struct dentry *dentry;
224 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
225 if (dentry == ERR_PTR(-ENOENT)) {
226 create_path(nodename);
227 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
230 return PTR_ERR(dentry);
232 err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt);
234 struct iattr newattrs;
236 newattrs.ia_mode = mode;
237 newattrs.ia_uid = uid;
238 newattrs.ia_gid = gid;
239 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
240 inode_lock(d_inode(dentry));
241 notify_change(dentry, &newattrs, NULL);
242 inode_unlock(d_inode(dentry));
244 /* mark as kernel-created inode */
245 d_inode(dentry)->i_private = &thread;
247 done_path_create(&path, dentry);
251 static int dev_rmdir(const char *name)
254 struct dentry *dentry;
257 dentry = kern_path_locked(name, &parent);
259 return PTR_ERR(dentry);
260 if (d_really_is_positive(dentry)) {
261 if (d_inode(dentry)->i_private == &thread)
262 err = vfs_rmdir(d_inode(parent.dentry), 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, struct kstat *stat)
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(stat->mode))
310 if (!S_ISCHR(stat->mode))
313 if (stat->rdev != dev->devt)
320 static int handle_remove(const char *nodename, struct device *dev)
323 struct dentry *dentry;
327 dentry = kern_path_locked(nodename, &parent);
329 return PTR_ERR(dentry);
331 if (d_really_is_positive(dentry)) {
333 struct path p = {.mnt = parent.mnt, .dentry = dentry};
334 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
335 AT_STATX_SYNC_AS_STAT);
336 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
337 struct iattr newattrs;
339 * before unlinking this node, reset permissions
340 * of possible references like hardlinks
342 newattrs.ia_uid = GLOBAL_ROOT_UID;
343 newattrs.ia_gid = GLOBAL_ROOT_GID;
344 newattrs.ia_mode = stat.mode & ~0777;
346 ATTR_UID|ATTR_GID|ATTR_MODE;
347 inode_lock(d_inode(dentry));
348 notify_change(dentry, &newattrs, NULL);
349 inode_unlock(d_inode(dentry));
350 err = vfs_unlink(d_inode(parent.dentry), dentry, NULL);
351 if (!err || err == -ENOENT)
358 inode_unlock(d_inode(parent.dentry));
361 if (deleted && strchr(nodename, '/'))
362 delete_path(nodename);
367 * If configured, or requested by the commandline, devtmpfs will be
368 * auto-mounted after the kernel mounted the root filesystem.
370 int devtmpfs_mount(const char *mntdir)
380 err = ksys_mount("devtmpfs", mntdir, "devtmpfs", MS_SILENT, NULL);
382 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
384 printk(KERN_INFO "devtmpfs: mounted\n");
388 static DECLARE_COMPLETION(setup_done);
390 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
394 return handle_create(name, mode, uid, gid, dev);
396 return handle_remove(name, dev);
399 static int devtmpfsd(void *p)
402 *err = ksys_unshare(CLONE_NEWNS);
405 *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
408 ksys_chdir("/.."); /* will traverse into overmounted root */
410 complete(&setup_done);
412 spin_lock(&req_lock);
414 struct req *req = requests;
416 spin_unlock(&req_lock);
418 struct req *next = req->next;
419 req->err = handle(req->name, req->mode,
420 req->uid, req->gid, req->dev);
421 complete(&req->done);
424 spin_lock(&req_lock);
426 __set_current_state(TASK_INTERRUPTIBLE);
427 spin_unlock(&req_lock);
432 complete(&setup_done);
437 * Create devtmpfs instance, driver-core devices will add their device
440 int __init devtmpfs_init(void)
442 char opts[] = "mode=0755";
445 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
447 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
451 err = register_filesystem(&dev_fs_type);
453 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
458 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
459 if (!IS_ERR(thread)) {
460 wait_for_completion(&setup_done);
462 err = PTR_ERR(thread);
467 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
468 unregister_filesystem(&dev_fs_type);
472 printk(KERN_INFO "devtmpfs: initialized\n");