xref: /linux-6.15/drivers/base/devtmpfs.c (revision e079d7c4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
22b2af54aSKay Sievers /*
32b2af54aSKay Sievers  * devtmpfs - kernel-maintained tmpfs-based /dev
42b2af54aSKay Sievers  *
52b2af54aSKay Sievers  * Copyright (C) 2009, Kay Sievers <[email protected]>
62b2af54aSKay Sievers  *
72b2af54aSKay Sievers  * During bootup, before any driver core device is registered,
82b2af54aSKay Sievers  * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
92b2af54aSKay Sievers  * device which requests a device node, will add a node in this
10e454cea2SKay Sievers  * filesystem.
1102fbe5e6SPeter Korsgaard  * By default, all devices are named after the name of the device,
1202fbe5e6SPeter Korsgaard  * owned by root and have a default mode of 0600. Subsystems can
1302fbe5e6SPeter Korsgaard  * overwrite the default setting if needed.
142b2af54aSKay Sievers  */
152b2af54aSKay Sievers 
165cdc03c5SLonglong Xia #define pr_fmt(fmt) "devtmpfs: " fmt
175cdc03c5SLonglong Xia 
182b2af54aSKay Sievers #include <linux/kernel.h>
192b2af54aSKay Sievers #include <linux/syscalls.h>
202b2af54aSKay Sievers #include <linux/mount.h>
212b2af54aSKay Sievers #include <linux/device.h>
22322cbb50SChristoph Hellwig #include <linux/blkdev.h>
232b2af54aSKay Sievers #include <linux/namei.h>
242b2af54aSKay Sievers #include <linux/fs.h>
252b2af54aSKay Sievers #include <linux/shmem_fs.h>
26da5e4ef7SPeter Korsgaard #include <linux/ramfs.h>
27e454cea2SKay Sievers #include <linux/sched.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
292780f1ffSAl Viro #include <linux/kthread.h>
30c60166f0SChristoph Hellwig #include <linux/init_syscalls.h>
31e262e32dSDavid Howells #include <uapi/linux/mount.h>
32c3a30420SGreg Kroah-Hartman #include "base.h"
332b2af54aSKay Sievers 
3428f0c335SKees Cook #ifdef CONFIG_DEVTMPFS_SAFE
3528f0c335SKees Cook #define DEVTMPFS_MFLAGS       (MS_SILENT | MS_NOEXEC | MS_NOSUID)
3628f0c335SKees Cook #else
3728f0c335SKees Cook #define DEVTMPFS_MFLAGS       (MS_SILENT)
3828f0c335SKees Cook #endif
3928f0c335SKees Cook 
402780f1ffSAl Viro static struct task_struct *thread;
412b2af54aSKay Sievers 
42fad1db8aSRasmus Villemoes static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
432b2af54aSKay Sievers 
442780f1ffSAl Viro static DEFINE_SPINLOCK(req_lock);
452780f1ffSAl Viro 
462780f1ffSAl Viro static struct req {
472780f1ffSAl Viro 	struct req *next;
482780f1ffSAl Viro 	struct completion done;
492780f1ffSAl Viro 	int err;
502780f1ffSAl Viro 	const char *name;
512c9ede55SAl Viro 	umode_t mode;	/* 0 => delete */
524e4098a3SGreg Kroah-Hartman 	kuid_t uid;
534e4098a3SGreg Kroah-Hartman 	kgid_t gid;
542780f1ffSAl Viro 	struct device *dev;
552780f1ffSAl Viro } *requests;
56ed413ae6SKay Sievers 
mount_param(char * str)572b2af54aSKay Sievers static int __init mount_param(char *str)
582b2af54aSKay Sievers {
59fc14f2feSAl Viro 	mount_dev = simple_strtoul(str, NULL, 0);
602b2af54aSKay Sievers 	return 1;
612b2af54aSKay Sievers }
622b2af54aSKay Sievers __setup("devtmpfs.mount=", mount_param);
632b2af54aSKay Sievers 
64d401727eSAl Viro static struct vfsmount *mnt;
65d401727eSAl Viro 
66d401727eSAl Viro static struct file_system_type internal_fs_type = {
672b2af54aSKay Sievers 	.name = "devtmpfs",
68f3235626SDavid Howells #ifdef CONFIG_TMPFS
69f3235626SDavid Howells 	.init_fs_context = shmem_init_fs_context,
70f3235626SDavid Howells #else
71f3235626SDavid Howells 	.init_fs_context = ramfs_init_fs_context,
72f3235626SDavid Howells #endif
732b2af54aSKay Sievers 	.kill_sb = kill_litter_super,
742b2af54aSKay Sievers };
752b2af54aSKay Sievers 
76cb0e0a8bSEric Sandeen /* Simply take a ref on the existing mount */
devtmpfs_get_tree(struct fs_context * fc)77cb0e0a8bSEric Sandeen static int devtmpfs_get_tree(struct fs_context *fc)
78cb0e0a8bSEric Sandeen {
79cb0e0a8bSEric Sandeen 	struct super_block *sb = mnt->mnt_sb;
80cb0e0a8bSEric Sandeen 
81cb0e0a8bSEric Sandeen 	atomic_inc(&sb->s_active);
82cb0e0a8bSEric Sandeen 	down_write(&sb->s_umount);
83cb0e0a8bSEric Sandeen 	fc->root = dget(sb->s_root);
84cb0e0a8bSEric Sandeen 	return 0;
85cb0e0a8bSEric Sandeen }
86cb0e0a8bSEric Sandeen 
87cb0e0a8bSEric Sandeen /* Ops are filled in during init depending on underlying shmem or ramfs type */
88cb0e0a8bSEric Sandeen struct fs_context_operations devtmpfs_context_ops = {};
89cb0e0a8bSEric Sandeen 
90cb0e0a8bSEric Sandeen /* Call the underlying initialization and set to our ops */
devtmpfs_init_fs_context(struct fs_context * fc)91cb0e0a8bSEric Sandeen static int devtmpfs_init_fs_context(struct fs_context *fc)
92cb0e0a8bSEric Sandeen {
93cb0e0a8bSEric Sandeen 	int ret;
94cb0e0a8bSEric Sandeen #ifdef CONFIG_TMPFS
95cb0e0a8bSEric Sandeen 	ret = shmem_init_fs_context(fc);
96cb0e0a8bSEric Sandeen #else
97cb0e0a8bSEric Sandeen 	ret = ramfs_init_fs_context(fc);
98cb0e0a8bSEric Sandeen #endif
99cb0e0a8bSEric Sandeen 	if (ret < 0)
100cb0e0a8bSEric Sandeen 		return ret;
101cb0e0a8bSEric Sandeen 
102cb0e0a8bSEric Sandeen 	fc->ops = &devtmpfs_context_ops;
103cb0e0a8bSEric Sandeen 
104cb0e0a8bSEric Sandeen 	return 0;
105cb0e0a8bSEric Sandeen }
106cb0e0a8bSEric Sandeen 
107d401727eSAl Viro static struct file_system_type dev_fs_type = {
108d401727eSAl Viro 	.name = "devtmpfs",
109cb0e0a8bSEric Sandeen 	.init_fs_context = devtmpfs_init_fs_context,
110d401727eSAl Viro };
111d401727eSAl Viro 
devtmpfs_submit_req(struct req * req,const char * tmp)11272a9cc95SRasmus Villemoes static int devtmpfs_submit_req(struct req *req, const char *tmp)
11372a9cc95SRasmus Villemoes {
11472a9cc95SRasmus Villemoes 	init_completion(&req->done);
11572a9cc95SRasmus Villemoes 
11672a9cc95SRasmus Villemoes 	spin_lock(&req_lock);
11772a9cc95SRasmus Villemoes 	req->next = requests;
11872a9cc95SRasmus Villemoes 	requests = req;
11972a9cc95SRasmus Villemoes 	spin_unlock(&req_lock);
12072a9cc95SRasmus Villemoes 
12172a9cc95SRasmus Villemoes 	wake_up_process(thread);
12272a9cc95SRasmus Villemoes 	wait_for_completion(&req->done);
12372a9cc95SRasmus Villemoes 
12472a9cc95SRasmus Villemoes 	kfree(tmp);
12572a9cc95SRasmus Villemoes 
12672a9cc95SRasmus Villemoes 	return req->err;
12772a9cc95SRasmus Villemoes }
12872a9cc95SRasmus Villemoes 
devtmpfs_create_node(struct device * dev)1292780f1ffSAl Viro int devtmpfs_create_node(struct device *dev)
1302780f1ffSAl Viro {
1312780f1ffSAl Viro 	const char *tmp = NULL;
1322780f1ffSAl Viro 	struct req req;
1332780f1ffSAl Viro 
1342780f1ffSAl Viro 	if (!thread)
1352780f1ffSAl Viro 		return 0;
1362780f1ffSAl Viro 
1372780f1ffSAl Viro 	req.mode = 0;
1384e4098a3SGreg Kroah-Hartman 	req.uid = GLOBAL_ROOT_UID;
1394e4098a3SGreg Kroah-Hartman 	req.gid = GLOBAL_ROOT_GID;
1403c2670e6SKay Sievers 	req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
1412780f1ffSAl Viro 	if (!req.name)
1422780f1ffSAl Viro 		return -ENOMEM;
1432780f1ffSAl Viro 
1442780f1ffSAl Viro 	if (req.mode == 0)
1452780f1ffSAl Viro 		req.mode = 0600;
1462780f1ffSAl Viro 	if (is_blockdev(dev))
1472780f1ffSAl Viro 		req.mode |= S_IFBLK;
1482780f1ffSAl Viro 	else
1492780f1ffSAl Viro 		req.mode |= S_IFCHR;
1502780f1ffSAl Viro 
1512780f1ffSAl Viro 	req.dev = dev;
1522780f1ffSAl Viro 
15372a9cc95SRasmus Villemoes 	return devtmpfs_submit_req(&req, tmp);
1542780f1ffSAl Viro }
1552780f1ffSAl Viro 
devtmpfs_delete_node(struct device * dev)1562780f1ffSAl Viro int devtmpfs_delete_node(struct device *dev)
1572780f1ffSAl Viro {
1582780f1ffSAl Viro 	const char *tmp = NULL;
1592780f1ffSAl Viro 	struct req req;
1602780f1ffSAl Viro 
1612780f1ffSAl Viro 	if (!thread)
1622780f1ffSAl Viro 		return 0;
1632780f1ffSAl Viro 
1643c2670e6SKay Sievers 	req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
1652780f1ffSAl Viro 	if (!req.name)
1662780f1ffSAl Viro 		return -ENOMEM;
1672780f1ffSAl Viro 
1682780f1ffSAl Viro 	req.mode = 0;
1692780f1ffSAl Viro 	req.dev = dev;
1702780f1ffSAl Viro 
17172a9cc95SRasmus Villemoes 	return devtmpfs_submit_req(&req, tmp);
1722780f1ffSAl Viro }
1732780f1ffSAl Viro 
dev_mkdir(const char * name,umode_t mode)174fbd48a69SAl Viro static int dev_mkdir(const char *name, umode_t mode)
1752b2af54aSKay Sievers {
1762b2af54aSKay Sievers 	struct dentry *dentry;
17769753a0fSAl Viro 	struct path path;
1782b2af54aSKay Sievers 
1791ac12b4bSJeff Layton 	dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
18069753a0fSAl Viro 	if (IS_ERR(dentry))
18169753a0fSAl Viro 		return PTR_ERR(dentry);
1822b2af54aSKay Sievers 
183c54b3869SNeilBrown 	dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode);
184c54b3869SNeilBrown 	if (!IS_ERR(dentry))
185015bf43bSKay Sievers 		/* mark as kernel-created inode */
18675c3cfa8SDavid Howells 		d_inode(dentry)->i_private = &thread;
187921a1650SAl Viro 	done_path_create(&path, dentry);
188c54b3869SNeilBrown 	return PTR_ERR_OR_ZERO(dentry);
1892b2af54aSKay Sievers }
1902b2af54aSKay Sievers 
create_path(const char * nodepath)1912b2af54aSKay Sievers static int create_path(const char *nodepath)
1922b2af54aSKay Sievers {
193ed413ae6SKay Sievers 	char *path;
1942b2af54aSKay Sievers 	char *s;
1959d108d25SAl Viro 	int err = 0;
1962b2af54aSKay Sievers 
1972b2af54aSKay Sievers 	/* parent directories do not exist, create them */
198ed413ae6SKay Sievers 	path = kstrdup(nodepath, GFP_KERNEL);
1995da4e689SAl Viro 	if (!path)
2005da4e689SAl Viro 		return -ENOMEM;
2015da4e689SAl Viro 
2022b2af54aSKay Sievers 	s = path;
203ed413ae6SKay Sievers 	for (;;) {
2042b2af54aSKay Sievers 		s = strchr(s, '/');
2052b2af54aSKay Sievers 		if (!s)
2062b2af54aSKay Sievers 			break;
2072b2af54aSKay Sievers 		s[0] = '\0';
2082b2af54aSKay Sievers 		err = dev_mkdir(path, 0755);
2092b2af54aSKay Sievers 		if (err && err != -EEXIST)
2102b2af54aSKay Sievers 			break;
2112b2af54aSKay Sievers 		s[0] = '/';
2122b2af54aSKay Sievers 		s++;
2132b2af54aSKay Sievers 	}
2142b2af54aSKay Sievers 	kfree(path);
2152b2af54aSKay Sievers 	return err;
2162b2af54aSKay Sievers }
2172b2af54aSKay Sievers 
handle_create(const char * nodename,umode_t mode,kuid_t uid,kgid_t gid,struct device * dev)2184e4098a3SGreg Kroah-Hartman static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
2194e4098a3SGreg Kroah-Hartman 			 kgid_t gid, struct device *dev)
2202b2af54aSKay Sievers {
2212b2af54aSKay Sievers 	struct dentry *dentry;
22269753a0fSAl Viro 	struct path path;
2232b2af54aSKay Sievers 	int err;
2242b2af54aSKay Sievers 
22569753a0fSAl Viro 	dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
22669753a0fSAl Viro 	if (dentry == ERR_PTR(-ENOENT)) {
2272b2af54aSKay Sievers 		create_path(nodename);
22869753a0fSAl Viro 		dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
22900926996SKay Sievers 	}
23069753a0fSAl Viro 	if (IS_ERR(dentry))
23169753a0fSAl Viro 		return PTR_ERR(dentry);
2322b2af54aSKay Sievers 
233abf08576SChristian Brauner 	err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
2346521f891SChristian Brauner 			dev->devt);
23500926996SKay Sievers 	if (!err) {
23600926996SKay Sievers 		struct iattr newattrs;
23700926996SKay Sievers 
23800926996SKay Sievers 		newattrs.ia_mode = mode;
2394e4098a3SGreg Kroah-Hartman 		newattrs.ia_uid = uid;
2404e4098a3SGreg Kroah-Hartman 		newattrs.ia_gid = gid;
2413c2670e6SKay Sievers 		newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
2425955102cSAl Viro 		inode_lock(d_inode(dentry));
243abf08576SChristian Brauner 		notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
2445955102cSAl Viro 		inode_unlock(d_inode(dentry));
24500926996SKay Sievers 
24600926996SKay Sievers 		/* mark as kernel-created inode */
24775c3cfa8SDavid Howells 		d_inode(dentry)->i_private = &thread;
24800926996SKay Sievers 	}
249921a1650SAl Viro 	done_path_create(&path, dentry);
2502b2af54aSKay Sievers 	return err;
2512b2af54aSKay Sievers }
2522b2af54aSKay Sievers 
dev_rmdir(const char * name)2532b2af54aSKay Sievers static int dev_rmdir(const char *name)
2542b2af54aSKay Sievers {
25579714f72SAl Viro 	struct path parent;
2562b2af54aSKay Sievers 	struct dentry *dentry;
2572b2af54aSKay Sievers 	int err;
2582b2af54aSKay Sievers 
25979714f72SAl Viro 	dentry = kern_path_locked(name, &parent);
26079714f72SAl Viro 	if (IS_ERR(dentry))
26179714f72SAl Viro 		return PTR_ERR(dentry);
26275c3cfa8SDavid Howells 	if (d_inode(dentry)->i_private == &thread)
263abf08576SChristian Brauner 		err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
2646521f891SChristian Brauner 				dentry);
2652b2af54aSKay Sievers 	else
266015bf43bSKay Sievers 		err = -EPERM;
2671c3cb50bSNeilBrown 
2682b2af54aSKay Sievers 	dput(dentry);
2695955102cSAl Viro 	inode_unlock(d_inode(parent.dentry));
27079714f72SAl Viro 	path_put(&parent);
2712b2af54aSKay Sievers 	return err;
2722b2af54aSKay Sievers }
2732b2af54aSKay Sievers 
delete_path(const char * nodepath)2742b2af54aSKay Sievers static int delete_path(const char *nodepath)
2752b2af54aSKay Sievers {
276be6b1dfeSRasmus Villemoes 	char *path;
2772b2af54aSKay Sievers 	int err = 0;
2782b2af54aSKay Sievers 
2792b2af54aSKay Sievers 	path = kstrdup(nodepath, GFP_KERNEL);
2802b2af54aSKay Sievers 	if (!path)
2812b2af54aSKay Sievers 		return -ENOMEM;
2822b2af54aSKay Sievers 
283ed413ae6SKay Sievers 	for (;;) {
2842b2af54aSKay Sievers 		char *base;
2852b2af54aSKay Sievers 
2862b2af54aSKay Sievers 		base = strrchr(path, '/');
2872b2af54aSKay Sievers 		if (!base)
2882b2af54aSKay Sievers 			break;
2892b2af54aSKay Sievers 		base[0] = '\0';
2902b2af54aSKay Sievers 		err = dev_rmdir(path);
2912b2af54aSKay Sievers 		if (err)
2922b2af54aSKay Sievers 			break;
2932b2af54aSKay Sievers 	}
2942b2af54aSKay Sievers 
2952b2af54aSKay Sievers 	kfree(path);
2962b2af54aSKay Sievers 	return err;
2972b2af54aSKay Sievers }
2982b2af54aSKay Sievers 
dev_mynode(struct device * dev,struct inode * inode)299*e079d7c4SChristoph Hellwig static int dev_mynode(struct device *dev, struct inode *inode)
3002b2af54aSKay Sievers {
3012b2af54aSKay Sievers 	/* did we create it */
3022780f1ffSAl Viro 	if (inode->i_private != &thread)
3032b2af54aSKay Sievers 		return 0;
3042b2af54aSKay Sievers 
3052b2af54aSKay Sievers 	/* does the dev_t match */
3062b2af54aSKay Sievers 	if (is_blockdev(dev)) {
307*e079d7c4SChristoph Hellwig 		if (!S_ISBLK(inode->i_mode))
3082b2af54aSKay Sievers 			return 0;
3092b2af54aSKay Sievers 	} else {
310*e079d7c4SChristoph Hellwig 		if (!S_ISCHR(inode->i_mode))
3112b2af54aSKay Sievers 			return 0;
3122b2af54aSKay Sievers 	}
313*e079d7c4SChristoph Hellwig 	if (inode->i_rdev != dev->devt)
3142b2af54aSKay Sievers 		return 0;
3152b2af54aSKay Sievers 
3162b2af54aSKay Sievers 	/* ours */
3172b2af54aSKay Sievers 	return 1;
3182b2af54aSKay Sievers }
3192b2af54aSKay Sievers 
handle_remove(const char * nodename,struct device * dev)3202780f1ffSAl Viro static int handle_remove(const char *nodename, struct device *dev)
3212b2af54aSKay Sievers {
32279714f72SAl Viro 	struct path parent;
3232b2af54aSKay Sievers 	struct dentry *dentry;
324*e079d7c4SChristoph Hellwig 	struct inode *inode;
325fbde7c61SAxel Lin 	int deleted = 0;
326*e079d7c4SChristoph Hellwig 	int err = 0;
3272b2af54aSKay Sievers 
32879714f72SAl Viro 	dentry = kern_path_locked(nodename, &parent);
32979714f72SAl Viro 	if (IS_ERR(dentry))
33079714f72SAl Viro 		return PTR_ERR(dentry);
3312b2af54aSKay Sievers 
332*e079d7c4SChristoph Hellwig 	inode = d_inode(dentry);
333*e079d7c4SChristoph Hellwig 	if (dev_mynode(dev, inode)) {
3345e31d76fSKay Sievers 		struct iattr newattrs;
3355e31d76fSKay Sievers 		/*
3365e31d76fSKay Sievers 		 * before unlinking this node, reset permissions
3375e31d76fSKay Sievers 		 * of possible references like hardlinks
3385e31d76fSKay Sievers 		 */
33991fa2ccaSEric W. Biederman 		newattrs.ia_uid = GLOBAL_ROOT_UID;
34091fa2ccaSEric W. Biederman 		newattrs.ia_gid = GLOBAL_ROOT_GID;
341*e079d7c4SChristoph Hellwig 		newattrs.ia_mode = inode->i_mode & ~0777;
3425e31d76fSKay Sievers 		newattrs.ia_valid =
3435e31d76fSKay Sievers 			ATTR_UID|ATTR_GID|ATTR_MODE;
3445955102cSAl Viro 		inode_lock(d_inode(dentry));
345abf08576SChristian Brauner 		notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
3465955102cSAl Viro 		inode_unlock(d_inode(dentry));
347abf08576SChristian Brauner 		err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
3486521f891SChristian Brauner 				 dentry, NULL);
3492b2af54aSKay Sievers 		if (!err || err == -ENOENT)
3502b2af54aSKay Sievers 			deleted = 1;
3512b2af54aSKay Sievers 	}
3522b2af54aSKay Sievers 	dput(dentry);
3535955102cSAl Viro 	inode_unlock(d_inode(parent.dentry));
3542b2af54aSKay Sievers 
35579714f72SAl Viro 	path_put(&parent);
3562b2af54aSKay Sievers 	if (deleted && strchr(nodename, '/'))
3572b2af54aSKay Sievers 		delete_path(nodename);
3582b2af54aSKay Sievers 	return err;
3592b2af54aSKay Sievers }
3602b2af54aSKay Sievers 
3612b2af54aSKay Sievers /*
3622b2af54aSKay Sievers  * If configured, or requested by the commandline, devtmpfs will be
3632b2af54aSKay Sievers  * auto-mounted after the kernel mounted the root filesystem.
3642b2af54aSKay Sievers  */
devtmpfs_mount(void)365fad1db8aSRasmus Villemoes int __init devtmpfs_mount(void)
3662b2af54aSKay Sievers {
3672b2af54aSKay Sievers 	int err;
3682b2af54aSKay Sievers 
369fc14f2feSAl Viro 	if (!mount_dev)
3702b2af54aSKay Sievers 		return 0;
3712b2af54aSKay Sievers 
3722780f1ffSAl Viro 	if (!thread)
3732b2af54aSKay Sievers 		return 0;
3742b2af54aSKay Sievers 
37528f0c335SKees Cook 	err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
3762b2af54aSKay Sievers 	if (err)
3775cdc03c5SLonglong Xia 		pr_info("error mounting %d\n", err);
3782b2af54aSKay Sievers 	else
3795cdc03c5SLonglong Xia 		pr_info("mounted\n");
3802b2af54aSKay Sievers 	return err;
3812b2af54aSKay Sievers }
3822b2af54aSKay Sievers 
38301085e24SRasmus Villemoes static __initdata DECLARE_COMPLETION(setup_done);
3842780f1ffSAl Viro 
handle(const char * name,umode_t mode,kuid_t uid,kgid_t gid,struct device * dev)3854e4098a3SGreg Kroah-Hartman static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
3863c2670e6SKay Sievers 		  struct device *dev)
3872780f1ffSAl Viro {
3882780f1ffSAl Viro 	if (mode)
3893c2670e6SKay Sievers 		return handle_create(name, mode, uid, gid, dev);
3902780f1ffSAl Viro 	else
3912780f1ffSAl Viro 		return handle_remove(name, dev);
3922780f1ffSAl Viro }
3932780f1ffSAl Viro 
devtmpfs_work_loop(void)394bcbacc49SChristoph Hellwig static void __noreturn devtmpfs_work_loop(void)
3952780f1ffSAl Viro {
3962780f1ffSAl Viro 	while (1) {
3972780f1ffSAl Viro 		spin_lock(&req_lock);
3982780f1ffSAl Viro 		while (requests) {
3992780f1ffSAl Viro 			struct req *req = requests;
4002780f1ffSAl Viro 			requests = NULL;
4012780f1ffSAl Viro 			spin_unlock(&req_lock);
4022780f1ffSAl Viro 			while (req) {
403e13889baSAl Viro 				struct req *next = req->next;
4043c2670e6SKay Sievers 				req->err = handle(req->name, req->mode,
4053c2670e6SKay Sievers 						  req->uid, req->gid, req->dev);
4062780f1ffSAl Viro 				complete(&req->done);
407e13889baSAl Viro 				req = next;
4082780f1ffSAl Viro 			}
4092780f1ffSAl Viro 			spin_lock(&req_lock);
4102780f1ffSAl Viro 		}
41165e6757bSKautuk Consul 		__set_current_state(TASK_INTERRUPTIBLE);
4122780f1ffSAl Viro 		spin_unlock(&req_lock);
4132780f1ffSAl Viro 		schedule();
4142780f1ffSAl Viro 	}
415bcbacc49SChristoph Hellwig }
416bcbacc49SChristoph Hellwig 
devtmpfs_setup(void * p)41701085e24SRasmus Villemoes static noinline int __init devtmpfs_setup(void *p)
418bcbacc49SChristoph Hellwig {
419bcbacc49SChristoph Hellwig 	int err;
420bcbacc49SChristoph Hellwig 
421bcbacc49SChristoph Hellwig 	err = ksys_unshare(CLONE_NEWNS);
422bcbacc49SChristoph Hellwig 	if (err)
423bcbacc49SChristoph Hellwig 		goto out;
42428f0c335SKees Cook 	err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
425bcbacc49SChristoph Hellwig 	if (err)
426bcbacc49SChristoph Hellwig 		goto out;
427db63f1e3SChristoph Hellwig 	init_chdir("/.."); /* will traverse into overmounted root */
4284b7ca501SChristoph Hellwig 	init_chroot(".");
429bcbacc49SChristoph Hellwig out:
430bcbacc49SChristoph Hellwig 	*(int *)p = err;
431bcbacc49SChristoph Hellwig 	return err;
432bcbacc49SChristoph Hellwig }
433bcbacc49SChristoph Hellwig 
434bcbacc49SChristoph Hellwig /*
435bcbacc49SChristoph Hellwig  * The __ref is because devtmpfs_setup needs to be __init for the routines it
436bcbacc49SChristoph Hellwig  * calls.  That call is done while devtmpfs_init, which is marked __init,
437bcbacc49SChristoph Hellwig  * synchronously waits for it to complete.
438bcbacc49SChristoph Hellwig  */
devtmpfsd(void * p)439bcbacc49SChristoph Hellwig static int __ref devtmpfsd(void *p)
440bcbacc49SChristoph Hellwig {
441bcbacc49SChristoph Hellwig 	int err = devtmpfs_setup(p);
442bcbacc49SChristoph Hellwig 
44338f087deSRasmus Villemoes 	complete(&setup_done);
444bcbacc49SChristoph Hellwig 	if (err)
445bcbacc49SChristoph Hellwig 		return err;
446bcbacc49SChristoph Hellwig 	devtmpfs_work_loop();
4472780f1ffSAl Viro 	return 0;
4482780f1ffSAl Viro }
4492780f1ffSAl Viro 
4502b2af54aSKay Sievers /*
451cb0e0a8bSEric Sandeen  * Get the underlying (shmem/ramfs) context ops to build ours
452cb0e0a8bSEric Sandeen  */
devtmpfs_configure_context(void)453cb0e0a8bSEric Sandeen static int devtmpfs_configure_context(void)
454cb0e0a8bSEric Sandeen {
455cb0e0a8bSEric Sandeen 	struct fs_context *fc;
456cb0e0a8bSEric Sandeen 
457cb0e0a8bSEric Sandeen 	fc = fs_context_for_reconfigure(mnt->mnt_root, mnt->mnt_sb->s_flags,
458cb0e0a8bSEric Sandeen 					MS_RMT_MASK);
459cb0e0a8bSEric Sandeen 	if (IS_ERR(fc))
460cb0e0a8bSEric Sandeen 		return PTR_ERR(fc);
461cb0e0a8bSEric Sandeen 
462cb0e0a8bSEric Sandeen 	/* Set up devtmpfs_context_ops based on underlying type */
463cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.free	      = fc->ops->free;
464cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.dup	      = fc->ops->dup;
465cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.parse_param      = fc->ops->parse_param;
466cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.parse_monolithic = fc->ops->parse_monolithic;
467cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.get_tree	      = &devtmpfs_get_tree;
468cb0e0a8bSEric Sandeen 	devtmpfs_context_ops.reconfigure      = fc->ops->reconfigure;
469cb0e0a8bSEric Sandeen 
470cb0e0a8bSEric Sandeen 	put_fs_context(fc);
471cb0e0a8bSEric Sandeen 
472cb0e0a8bSEric Sandeen 	return 0;
473cb0e0a8bSEric Sandeen }
474cb0e0a8bSEric Sandeen 
475cb0e0a8bSEric Sandeen /*
4762b2af54aSKay Sievers  * Create devtmpfs instance, driver-core devices will add their device
4772b2af54aSKay Sievers  * nodes here.
4782b2af54aSKay Sievers  */
devtmpfs_init(void)4792b2af54aSKay Sievers int __init devtmpfs_init(void)
4802b2af54aSKay Sievers {
481d401727eSAl Viro 	char opts[] = "mode=0755";
482d401727eSAl Viro 	int err;
483d401727eSAl Viro 
484d401727eSAl Viro 	mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
485d401727eSAl Viro 	if (IS_ERR(mnt)) {
4865cdc03c5SLonglong Xia 		pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
487d401727eSAl Viro 		return PTR_ERR(mnt);
488d401727eSAl Viro 	}
489cb0e0a8bSEric Sandeen 
490cb0e0a8bSEric Sandeen 	err = devtmpfs_configure_context();
491cb0e0a8bSEric Sandeen 	if (err) {
492cb0e0a8bSEric Sandeen 		pr_err("unable to configure devtmpfs type %d\n", err);
493cb0e0a8bSEric Sandeen 		return err;
494cb0e0a8bSEric Sandeen 	}
495cb0e0a8bSEric Sandeen 
496d401727eSAl Viro 	err = register_filesystem(&dev_fs_type);
4972b2af54aSKay Sievers 	if (err) {
4985cdc03c5SLonglong Xia 		pr_err("unable to register devtmpfs type %d\n", err);
4992b2af54aSKay Sievers 		return err;
5002b2af54aSKay Sievers 	}
5012b2af54aSKay Sievers 
5022780f1ffSAl Viro 	thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
5032780f1ffSAl Viro 	if (!IS_ERR(thread)) {
5042780f1ffSAl Viro 		wait_for_completion(&setup_done);
5052780f1ffSAl Viro 	} else {
5062780f1ffSAl Viro 		err = PTR_ERR(thread);
5072780f1ffSAl Viro 		thread = NULL;
5082780f1ffSAl Viro 	}
5092780f1ffSAl Viro 
5102780f1ffSAl Viro 	if (err) {
5115cdc03c5SLonglong Xia 		pr_err("unable to create devtmpfs %d\n", err);
5122b2af54aSKay Sievers 		unregister_filesystem(&dev_fs_type);
51331c779f2SYangxi Xiang 		thread = NULL;
5142b2af54aSKay Sievers 		return err;
5152b2af54aSKay Sievers 	}
5162b2af54aSKay Sievers 
5175cdc03c5SLonglong Xia 	pr_info("initialized\n");
5182b2af54aSKay Sievers 	return 0;
5192b2af54aSKay Sievers }
520