blob: f41063ac1aee478e668913187d8571b0b4317f21 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kay Sievers2b2af542009-04-30 15:23:42 +02002/*
3 * devtmpfs - kernel-maintained tmpfs-based /dev
4 *
5 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
6 *
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
Kay Sieverse454cea2009-09-18 23:01:12 +020010 * filesystem.
Peter Korsgaard02fbe5e2012-04-17 12:13:18 +020011 * 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.
Kay Sievers2b2af542009-04-30 15:23:42 +020014 */
15
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>
22#include <linux/fs.h>
23#include <linux/shmem_fs.h>
Peter Korsgaardda5e4ef2010-03-16 21:55:21 +010024#include <linux/ramfs.h>
Kay Sieverse454cea2009-09-18 23:01:12 +020025#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Al Viro2780f1f2011-06-27 16:25:29 -040027#include <linux/kthread.h>
Christoph Hellwigc60166f2020-07-21 11:12:08 +020028#include <linux/init_syscalls.h>
David Howellse262e32d2018-11-01 23:07:23 +000029#include <uapi/linux/mount.h>
Greg Kroah-Hartmanc3a30422013-04-10 09:00:15 -070030#include "base.h"
Kay Sievers2b2af542009-04-30 15:23:42 +020031
Kees Cook28f0c332021-12-22 17:50:20 +050032#ifdef CONFIG_DEVTMPFS_SAFE
33#define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
34#else
35#define DEVTMPFS_MFLAGS (MS_SILENT)
36#endif
37
Al Viro2780f1f2011-06-27 16:25:29 -040038static struct task_struct *thread;
Kay Sievers2b2af542009-04-30 15:23:42 +020039
Rasmus Villemoesfad1db82020-01-15 19:41:52 +010040static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
Kay Sievers2b2af542009-04-30 15:23:42 +020041
Al Viro2780f1f2011-06-27 16:25:29 -040042static DEFINE_SPINLOCK(req_lock);
43
44static struct req {
45 struct req *next;
46 struct completion done;
47 int err;
48 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -040049 umode_t mode; /* 0 => delete */
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -070050 kuid_t uid;
51 kgid_t gid;
Al Viro2780f1f2011-06-27 16:25:29 -040052 struct device *dev;
53} *requests;
Kay Sieversed413ae2009-10-28 19:51:06 +010054
Kay Sievers2b2af542009-04-30 15:23:42 +020055static int __init mount_param(char *str)
56{
Al Virofc14f2f2010-07-25 01:48:30 +040057 mount_dev = simple_strtoul(str, NULL, 0);
Kay Sievers2b2af542009-04-30 15:23:42 +020058 return 1;
59}
60__setup("devtmpfs.mount=", mount_param);
61
Al Virod4017272019-06-01 18:43:09 -040062static struct vfsmount *mnt;
63
64static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
65 const char *dev_name, void *data)
66{
67 struct super_block *s = mnt->mnt_sb;
NeilBrowna6097182022-01-17 09:07:26 +110068 int err;
69
Al Virod4017272019-06-01 18:43:09 -040070 atomic_inc(&s->s_active);
71 down_write(&s->s_umount);
NeilBrowna6097182022-01-17 09:07:26 +110072 err = reconfigure_single(s, flags, data);
73 if (err < 0) {
74 deactivate_locked_super(s);
75 return ERR_PTR(err);
76 }
Al Virod4017272019-06-01 18:43:09 -040077 return dget(s->s_root);
78}
79
Al Virod4017272019-06-01 18:43:09 -040080static struct file_system_type internal_fs_type = {
Kay Sievers2b2af542009-04-30 15:23:42 +020081 .name = "devtmpfs",
David Howellsf3235622019-03-25 16:38:31 +000082#ifdef CONFIG_TMPFS
83 .init_fs_context = shmem_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -040084 .parameters = shmem_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +000085#else
86 .init_fs_context = ramfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -040087 .parameters = ramfs_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +000088#endif
Kay Sievers2b2af542009-04-30 15:23:42 +020089 .kill_sb = kill_litter_super,
90};
91
Al Virod4017272019-06-01 18:43:09 -040092static struct file_system_type dev_fs_type = {
93 .name = "devtmpfs",
94 .mount = public_dev_mount,
95};
96
Kay Sievers2b2af542009-04-30 15:23:42 +020097#ifdef CONFIG_BLOCK
98static inline int is_blockdev(struct device *dev)
99{
100 return dev->class == &block_class;
101}
102#else
103static inline int is_blockdev(struct device *dev) { return 0; }
104#endif
105
Rasmus Villemoes72a9cc92020-01-15 19:41:53 +0100106static int devtmpfs_submit_req(struct req *req, const char *tmp)
107{
108 init_completion(&req->done);
109
110 spin_lock(&req_lock);
111 req->next = requests;
112 requests = req;
113 spin_unlock(&req_lock);
114
115 wake_up_process(thread);
116 wait_for_completion(&req->done);
117
118 kfree(tmp);
119
120 return req->err;
121}
122
Al Viro2780f1f2011-06-27 16:25:29 -0400123int devtmpfs_create_node(struct device *dev)
124{
125 const char *tmp = NULL;
126 struct req req;
127
128 if (!thread)
129 return 0;
130
131 req.mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700132 req.uid = GLOBAL_ROOT_UID;
133 req.gid = GLOBAL_ROOT_GID;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700134 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400135 if (!req.name)
136 return -ENOMEM;
137
138 if (req.mode == 0)
139 req.mode = 0600;
140 if (is_blockdev(dev))
141 req.mode |= S_IFBLK;
142 else
143 req.mode |= S_IFCHR;
144
145 req.dev = dev;
146
Rasmus Villemoes72a9cc92020-01-15 19:41:53 +0100147 return devtmpfs_submit_req(&req, tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400148}
149
150int devtmpfs_delete_node(struct device *dev)
151{
152 const char *tmp = NULL;
153 struct req req;
154
155 if (!thread)
156 return 0;
157
Kay Sievers3c2670e2013-04-06 09:56:00 -0700158 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400159 if (!req.name)
160 return -ENOMEM;
161
162 req.mode = 0;
163 req.dev = dev;
164
Rasmus Villemoes72a9cc92020-01-15 19:41:53 +0100165 return devtmpfs_submit_req(&req, tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400166}
167
Al Virofbd48a62011-07-24 10:47:56 -0400168static int dev_mkdir(const char *name, umode_t mode)
Kay Sievers2b2af542009-04-30 15:23:42 +0200169{
Kay Sievers2b2af542009-04-30 15:23:42 +0200170 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400171 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200172 int err;
173
Jeff Layton1ac12b42012-12-11 12:10:06 -0500174 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
Al Viro69753a02011-06-27 16:35:45 -0400175 if (IS_ERR(dentry))
176 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200177
Christian Brauner6521f892021-01-21 14:19:33 +0100178 err = vfs_mkdir(&init_user_ns, d_inode(path.dentry), dentry, mode);
Al Viro69753a02011-06-27 16:35:45 -0400179 if (!err)
180 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000181 d_inode(dentry)->i_private = &thread;
Al Viro921a1652012-07-20 01:15:31 +0400182 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200183 return err;
184}
185
186static int create_path(const char *nodepath)
187{
Al Viro5da4e682011-06-27 16:37:12 -0400188 char *path;
189 char *s;
Al Viro9d108d22011-07-27 22:27:33 -0400190 int err = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200191
Al Viro5da4e682011-06-27 16:37:12 -0400192 /* parent directories do not exist, create them */
193 path = kstrdup(nodepath, GFP_KERNEL);
194 if (!path)
195 return -ENOMEM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200196
Al Viro5da4e682011-06-27 16:37:12 -0400197 s = path;
198 for (;;) {
199 s = strchr(s, '/');
200 if (!s)
201 break;
202 s[0] = '\0';
203 err = dev_mkdir(path, 0755);
204 if (err && err != -EEXIST)
205 break;
206 s[0] = '/';
207 s++;
Kay Sievers2b2af542009-04-30 15:23:42 +0200208 }
Al Viro5da4e682011-06-27 16:37:12 -0400209 kfree(path);
Kay Sievers2b2af542009-04-30 15:23:42 +0200210 return err;
211}
212
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700213static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
214 kgid_t gid, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200215{
Kay Sievers2b2af542009-04-30 15:23:42 +0200216 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400217 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200218 int err;
219
Al Viro69753a02011-06-27 16:35:45 -0400220 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
221 if (dentry == ERR_PTR(-ENOENT)) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200222 create_path(nodename);
Al Viro69753a02011-06-27 16:35:45 -0400223 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
Kay Sievers2b2af542009-04-30 15:23:42 +0200224 }
Al Viro69753a02011-06-27 16:35:45 -0400225 if (IS_ERR(dentry))
226 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200227
Christian Brauner6521f892021-01-21 14:19:33 +0100228 err = vfs_mknod(&init_user_ns, d_inode(path.dentry), dentry, mode,
229 dev->devt);
Al Viro69753a02011-06-27 16:35:45 -0400230 if (!err) {
231 struct iattr newattrs;
Kay Sievers00926992009-10-28 19:50:57 +0100232
Al Viro69753a02011-06-27 16:35:45 -0400233 newattrs.ia_mode = mode;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700234 newattrs.ia_uid = uid;
235 newattrs.ia_gid = gid;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700236 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
Al Viro59551022016-01-22 15:40:57 -0500237 inode_lock(d_inode(dentry));
Christian Brauner2f221d62021-01-21 14:19:26 +0100238 notify_change(&init_user_ns, dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500239 inode_unlock(d_inode(dentry));
Kay Sievers00926992009-10-28 19:50:57 +0100240
Al Viro69753a02011-06-27 16:35:45 -0400241 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000242 d_inode(dentry)->i_private = &thread;
Kay Sievers2b2af542009-04-30 15:23:42 +0200243 }
Al Viro921a1652012-07-20 01:15:31 +0400244 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200245 return err;
246}
247
248static int dev_rmdir(const char *name)
249{
Al Viro79714f72012-06-15 03:01:42 +0400250 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200251 struct dentry *dentry;
252 int err;
253
Al Viro79714f72012-06-15 03:01:42 +0400254 dentry = kern_path_locked(name, &parent);
255 if (IS_ERR(dentry))
256 return PTR_ERR(dentry);
David Howells75c3cfa2015-03-17 22:26:12 +0000257 if (d_really_is_positive(dentry)) {
258 if (d_inode(dentry)->i_private == &thread)
Christian Brauner6521f892021-01-21 14:19:33 +0100259 err = vfs_rmdir(&init_user_ns, d_inode(parent.dentry),
260 dentry);
Al Viro79714f72012-06-15 03:01:42 +0400261 else
262 err = -EPERM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200263 } else {
Al Viro79714f72012-06-15 03:01:42 +0400264 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200265 }
Al Viro79714f72012-06-15 03:01:42 +0400266 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500267 inode_unlock(d_inode(parent.dentry));
Al Viro79714f72012-06-15 03:01:42 +0400268 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200269 return err;
270}
271
272static int delete_path(const char *nodepath)
273{
Rasmus Villemoesbe6b1df2018-08-22 13:00:07 +0200274 char *path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200275 int err = 0;
276
277 path = kstrdup(nodepath, GFP_KERNEL);
278 if (!path)
279 return -ENOMEM;
280
Kay Sieversed413ae2009-10-28 19:51:06 +0100281 for (;;) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200282 char *base;
283
284 base = strrchr(path, '/');
285 if (!base)
286 break;
287 base[0] = '\0';
288 err = dev_rmdir(path);
289 if (err)
290 break;
291 }
292
293 kfree(path);
294 return err;
295}
296
297static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
298{
299 /* did we create it */
Al Viro2780f1f2011-06-27 16:25:29 -0400300 if (inode->i_private != &thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200301 return 0;
302
303 /* does the dev_t match */
304 if (is_blockdev(dev)) {
305 if (!S_ISBLK(stat->mode))
306 return 0;
307 } else {
308 if (!S_ISCHR(stat->mode))
309 return 0;
310 }
311 if (stat->rdev != dev->devt)
312 return 0;
313
314 /* ours */
315 return 1;
316}
317
Al Viro2780f1f2011-06-27 16:25:29 -0400318static int handle_remove(const char *nodename, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200319{
Al Viro79714f72012-06-15 03:01:42 +0400320 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200321 struct dentry *dentry;
Axel Linfbde7c62013-11-16 16:15:23 +0800322 int deleted = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200323 int err;
324
Al Viro79714f72012-06-15 03:01:42 +0400325 dentry = kern_path_locked(nodename, &parent);
326 if (IS_ERR(dentry))
327 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200328
David Howells75c3cfa2015-03-17 22:26:12 +0000329 if (d_really_is_positive(dentry)) {
Al Viro79714f72012-06-15 03:01:42 +0400330 struct kstat stat;
Al Viro3dadecc2013-01-24 02:18:08 -0500331 struct path p = {.mnt = parent.mnt, .dentry = dentry};
David Howellsa528d352017-01-31 16:46:22 +0000332 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
333 AT_STATX_SYNC_AS_STAT);
David Howells75c3cfa2015-03-17 22:26:12 +0000334 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
Al Viro79714f72012-06-15 03:01:42 +0400335 struct iattr newattrs;
336 /*
337 * before unlinking this node, reset permissions
338 * of possible references like hardlinks
339 */
Eric W. Biederman91fa2cc2012-04-25 04:25:35 -0700340 newattrs.ia_uid = GLOBAL_ROOT_UID;
341 newattrs.ia_gid = GLOBAL_ROOT_GID;
Al Viro79714f72012-06-15 03:01:42 +0400342 newattrs.ia_mode = stat.mode & ~0777;
343 newattrs.ia_valid =
344 ATTR_UID|ATTR_GID|ATTR_MODE;
Al Viro59551022016-01-22 15:40:57 -0500345 inode_lock(d_inode(dentry));
Christian Brauner2f221d62021-01-21 14:19:26 +0100346 notify_change(&init_user_ns, dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500347 inode_unlock(d_inode(dentry));
Christian Brauner6521f892021-01-21 14:19:33 +0100348 err = vfs_unlink(&init_user_ns, d_inode(parent.dentry),
349 dentry, NULL);
Al Viro79714f72012-06-15 03:01:42 +0400350 if (!err || err == -ENOENT)
351 deleted = 1;
Kay Sievers2b2af542009-04-30 15:23:42 +0200352 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200353 } else {
Al Viro79714f72012-06-15 03:01:42 +0400354 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200355 }
Al Viro79714f72012-06-15 03:01:42 +0400356 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500357 inode_unlock(d_inode(parent.dentry));
Kay Sievers2b2af542009-04-30 15:23:42 +0200358
Al Viro79714f72012-06-15 03:01:42 +0400359 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200360 if (deleted && strchr(nodename, '/'))
361 delete_path(nodename);
Kay Sievers2b2af542009-04-30 15:23:42 +0200362 return err;
363}
364
365/*
366 * If configured, or requested by the commandline, devtmpfs will be
367 * auto-mounted after the kernel mounted the root filesystem.
368 */
Rasmus Villemoesfad1db82020-01-15 19:41:52 +0100369int __init devtmpfs_mount(void)
Kay Sievers2b2af542009-04-30 15:23:42 +0200370{
Kay Sievers2b2af542009-04-30 15:23:42 +0200371 int err;
372
Al Virofc14f2f2010-07-25 01:48:30 +0400373 if (!mount_dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200374 return 0;
375
Al Viro2780f1f2011-06-27 16:25:29 -0400376 if (!thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200377 return 0;
378
Kees Cook28f0c332021-12-22 17:50:20 +0500379 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
Kay Sievers2b2af542009-04-30 15:23:42 +0200380 if (err)
381 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
382 else
383 printk(KERN_INFO "devtmpfs: mounted\n");
Kay Sievers2b2af542009-04-30 15:23:42 +0200384 return err;
385}
386
Rasmus Villemoes01085e22021-03-12 11:30:27 +0100387static __initdata DECLARE_COMPLETION(setup_done);
Al Viro2780f1f2011-06-27 16:25:29 -0400388
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700389static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -0700390 struct device *dev)
Al Viro2780f1f2011-06-27 16:25:29 -0400391{
392 if (mode)
Kay Sievers3c2670e2013-04-06 09:56:00 -0700393 return handle_create(name, mode, uid, gid, dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400394 else
395 return handle_remove(name, dev);
396}
397
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200398static void __noreturn devtmpfs_work_loop(void)
Al Viro2780f1f2011-06-27 16:25:29 -0400399{
Al Viro2780f1f2011-06-27 16:25:29 -0400400 while (1) {
401 spin_lock(&req_lock);
402 while (requests) {
403 struct req *req = requests;
404 requests = NULL;
405 spin_unlock(&req_lock);
406 while (req) {
Al Viroe13889b2011-07-25 14:15:50 -0400407 struct req *next = req->next;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700408 req->err = handle(req->name, req->mode,
409 req->uid, req->gid, req->dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400410 complete(&req->done);
Al Viroe13889b2011-07-25 14:15:50 -0400411 req = next;
Al Viro2780f1f2011-06-27 16:25:29 -0400412 }
413 spin_lock(&req_lock);
414 }
Kautuk Consul65e67572011-11-15 14:52:34 -0800415 __set_current_state(TASK_INTERRUPTIBLE);
Al Viro2780f1f2011-06-27 16:25:29 -0400416 spin_unlock(&req_lock);
417 schedule();
Al Viro2780f1f2011-06-27 16:25:29 -0400418 }
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200419}
420
Rasmus Villemoes01085e22021-03-12 11:30:27 +0100421static noinline int __init devtmpfs_setup(void *p)
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200422{
423 int err;
424
425 err = ksys_unshare(CLONE_NEWNS);
426 if (err)
427 goto out;
Kees Cook28f0c332021-12-22 17:50:20 +0500428 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200429 if (err)
430 goto out;
Christoph Hellwigdb63f1e2020-07-22 11:25:21 +0200431 init_chdir("/.."); /* will traverse into overmounted root */
Christoph Hellwig4b7ca502020-07-22 11:26:13 +0200432 init_chroot(".");
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200433out:
434 *(int *)p = err;
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200435 return err;
436}
437
438/*
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.
442 */
443static int __ref devtmpfsd(void *p)
444{
445 int err = devtmpfs_setup(p);
446
Rasmus Villemoes38f087de2021-03-12 11:30:26 +0100447 complete(&setup_done);
Christoph Hellwigbcbacc42020-07-22 10:48:25 +0200448 if (err)
449 return err;
450 devtmpfs_work_loop();
Al Viro2780f1f2011-06-27 16:25:29 -0400451 return 0;
Al Viro2780f1f2011-06-27 16:25:29 -0400452}
453
Kay Sievers2b2af542009-04-30 15:23:42 +0200454/*
455 * Create devtmpfs instance, driver-core devices will add their device
456 * nodes here.
457 */
458int __init devtmpfs_init(void)
459{
Al Virod4017272019-06-01 18:43:09 -0400460 char opts[] = "mode=0755";
461 int err;
462
463 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
464 if (IS_ERR(mnt)) {
465 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
466 PTR_ERR(mnt));
467 return PTR_ERR(mnt);
468 }
469 err = register_filesystem(&dev_fs_type);
Kay Sievers2b2af542009-04-30 15:23:42 +0200470 if (err) {
471 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
472 "type %i\n", err);
473 return err;
474 }
475
Al Viro2780f1f2011-06-27 16:25:29 -0400476 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
477 if (!IS_ERR(thread)) {
478 wait_for_completion(&setup_done);
479 } else {
480 err = PTR_ERR(thread);
481 thread = NULL;
482 }
483
484 if (err) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200485 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
486 unregister_filesystem(&dev_fs_type);
487 return err;
488 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200489
490 printk(KERN_INFO "devtmpfs: initialized\n");
491 return 0;
492}