Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
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
10 * filesystem.
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.
14 */
15
16#define pr_fmt(fmt) "devtmpfs: " fmt
17
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>
24#include <linux/fs.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>
32#include "base.h"
33
34#ifdef CONFIG_DEVTMPFS_SAFE
35#define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
36#else
37#define DEVTMPFS_MFLAGS (MS_SILENT)
38#endif
39
40static struct task_struct *thread;
41
42static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
43
44static DEFINE_SPINLOCK(req_lock);
45
46static struct req {
47 struct req *next;
48 struct completion done;
49 int err;
50 const char *name;
51 umode_t mode; /* 0 => delete */
52 kuid_t uid;
53 kgid_t gid;
54 struct device *dev;
55} *requests;
56
57static int __init mount_param(char *str)
58{
59 mount_dev = simple_strtoul(str, NULL, 0);
60 return 1;
61}
62__setup("devtmpfs.mount=", mount_param);
63
64static struct vfsmount *mnt;
65
66static struct file_system_type internal_fs_type = {
67 .name = "devtmpfs",
68#ifdef CONFIG_TMPFS
69 .init_fs_context = shmem_init_fs_context,
70#else
71 .init_fs_context = ramfs_init_fs_context,
72#endif
73 .kill_sb = kill_litter_super,
74};
75
76/* Simply take a ref on the existing mount */
77static int devtmpfs_get_tree(struct fs_context *fc)
78{
79 struct super_block *sb = mnt->mnt_sb;
80
81 atomic_inc(&sb->s_active);
82 down_write(&sb->s_umount);
83 fc->root = dget(sb->s_root);
84 return 0;
85}
86
87/* Ops are filled in during init depending on underlying shmem or ramfs type */
88struct fs_context_operations devtmpfs_context_ops = {};
89
90/* Call the underlying initialization and set to our ops */
91static int devtmpfs_init_fs_context(struct fs_context *fc)
92{
93 int ret;
94#ifdef CONFIG_TMPFS
95 ret = shmem_init_fs_context(fc);
96#else
97 ret = ramfs_init_fs_context(fc);
98#endif
99 if (ret < 0)
100 return ret;
101
102 fc->ops = &devtmpfs_context_ops;
103
104 return 0;
105}
106
107static struct file_system_type dev_fs_type = {
108 .name = "devtmpfs",
109 .init_fs_context = devtmpfs_init_fs_context,
110};
111
112static int devtmpfs_submit_req(struct req *req, const char *tmp)
113{
114 init_completion(&req->done);
115
116 spin_lock(&req_lock);
117 req->next = requests;
118 requests = req;
119 spin_unlock(&req_lock);
120
121 wake_up_process(thread);
122 wait_for_completion(&req->done);
123
124 kfree(tmp);
125
126 return req->err;
127}
128
129int devtmpfs_create_node(struct device *dev)
130{
131 const char *tmp = NULL;
132 struct req req;
133
134 if (!thread)
135 return 0;
136
137 req.mode = 0;
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);
141 if (!req.name)
142 return -ENOMEM;
143
144 if (req.mode == 0)
145 req.mode = 0600;
146 if (is_blockdev(dev))
147 req.mode |= S_IFBLK;
148 else
149 req.mode |= S_IFCHR;
150
151 req.dev = dev;
152
153 return devtmpfs_submit_req(&req, tmp);
154}
155
156int devtmpfs_delete_node(struct device *dev)
157{
158 const char *tmp = NULL;
159 struct req req;
160
161 if (!thread)
162 return 0;
163
164 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
165 if (!req.name)
166 return -ENOMEM;
167
168 req.mode = 0;
169 req.dev = dev;
170
171 return devtmpfs_submit_req(&req, tmp);
172}
173
174static int dev_mkdir(const char *name, umode_t mode)
175{
176 struct dentry *dentry;
177 struct path path;
178
179 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
180 if (IS_ERR(dentry))
181 return PTR_ERR(dentry);
182
183 dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode);
184 if (!IS_ERR(dentry))
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);
189}
190
191static int create_path(const char *nodepath)
192{
193 char *path;
194 char *s;
195 int err = 0;
196
197 /* parent directories do not exist, create them */
198 path = kstrdup(nodepath, GFP_KERNEL);
199 if (!path)
200 return -ENOMEM;
201
202 s = path;
203 for (;;) {
204 s = strchr(s, '/');
205 if (!s)
206 break;
207 s[0] = '\0';
208 err = dev_mkdir(path, 0755);
209 if (err && err != -EEXIST)
210 break;
211 s[0] = '/';
212 s++;
213 }
214 kfree(path);
215 return err;
216}
217
218static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
219 kgid_t gid, struct device *dev)
220{
221 struct dentry *dentry;
222 struct path path;
223 int err;
224
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);
229 }
230 if (IS_ERR(dentry))
231 return PTR_ERR(dentry);
232
233 err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
234 dev->devt);
235 if (!err) {
236 struct iattr newattrs;
237
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));
245
246 /* mark as kernel-created inode */
247 d_inode(dentry)->i_private = &thread;
248 }
249 done_path_create(&path, dentry);
250 return err;
251}
252
253static int dev_rmdir(const char *name)
254{
255 struct path parent;
256 struct dentry *dentry;
257 int err;
258
259 dentry = kern_path_locked(name, &parent);
260 if (IS_ERR(dentry))
261 return PTR_ERR(dentry);
262 if (d_inode(dentry)->i_private == &thread)
263 err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
264 dentry);
265 else
266 err = -EPERM;
267
268 dput(dentry);
269 inode_unlock(d_inode(parent.dentry));
270 path_put(&parent);
271 return err;
272}
273
274static int delete_path(const char *nodepath)
275{
276 char *path;
277 int err = 0;
278
279 path = kstrdup(nodepath, GFP_KERNEL);
280 if (!path)
281 return -ENOMEM;
282
283 for (;;) {
284 char *base;
285
286 base = strrchr(path, '/');
287 if (!base)
288 break;
289 base[0] = '\0';
290 err = dev_rmdir(path);
291 if (err)
292 break;
293 }
294
295 kfree(path);
296 return err;
297}
298
299static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
300{
301 /* did we create it */
302 if (inode->i_private != &thread)
303 return 0;
304
305 /* does the dev_t match */
306 if (is_blockdev(dev)) {
307 if (!S_ISBLK(stat->mode))
308 return 0;
309 } else {
310 if (!S_ISCHR(stat->mode))
311 return 0;
312 }
313 if (stat->rdev != dev->devt)
314 return 0;
315
316 /* ours */
317 return 1;
318}
319
320static int handle_remove(const char *nodename, struct device *dev)
321{
322 struct path parent;
323 struct dentry *dentry;
324 struct kstat stat;
325 struct path p;
326 int deleted = 0;
327 int err;
328
329 dentry = kern_path_locked(nodename, &parent);
330 if (IS_ERR(dentry))
331 return PTR_ERR(dentry);
332
333 p.mnt = parent.mnt;
334 p.dentry = dentry;
335 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
336 AT_STATX_SYNC_AS_STAT);
337 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
338 struct iattr newattrs;
339 /*
340 * before unlinking this node, reset permissions
341 * of possible references like hardlinks
342 */
343 newattrs.ia_uid = GLOBAL_ROOT_UID;
344 newattrs.ia_gid = GLOBAL_ROOT_GID;
345 newattrs.ia_mode = stat.mode & ~0777;
346 newattrs.ia_valid =
347 ATTR_UID|ATTR_GID|ATTR_MODE;
348 inode_lock(d_inode(dentry));
349 notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
350 inode_unlock(d_inode(dentry));
351 err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
352 dentry, NULL);
353 if (!err || err == -ENOENT)
354 deleted = 1;
355 }
356 dput(dentry);
357 inode_unlock(d_inode(parent.dentry));
358
359 path_put(&parent);
360 if (deleted && strchr(nodename, '/'))
361 delete_path(nodename);
362 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 */
369int __init devtmpfs_mount(void)
370{
371 int err;
372
373 if (!mount_dev)
374 return 0;
375
376 if (!thread)
377 return 0;
378
379 err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
380 if (err)
381 pr_info("error mounting %d\n", err);
382 else
383 pr_info("mounted\n");
384 return err;
385}
386
387static __initdata DECLARE_COMPLETION(setup_done);
388
389static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
390 struct device *dev)
391{
392 if (mode)
393 return handle_create(name, mode, uid, gid, dev);
394 else
395 return handle_remove(name, dev);
396}
397
398static void __noreturn devtmpfs_work_loop(void)
399{
400 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) {
407 struct req *next = req->next;
408 req->err = handle(req->name, req->mode,
409 req->uid, req->gid, req->dev);
410 complete(&req->done);
411 req = next;
412 }
413 spin_lock(&req_lock);
414 }
415 __set_current_state(TASK_INTERRUPTIBLE);
416 spin_unlock(&req_lock);
417 schedule();
418 }
419}
420
421static noinline int __init devtmpfs_setup(void *p)
422{
423 int err;
424
425 err = ksys_unshare(CLONE_NEWNS);
426 if (err)
427 goto out;
428 err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
429 if (err)
430 goto out;
431 init_chdir("/.."); /* will traverse into overmounted root */
432 init_chroot(".");
433out:
434 *(int *)p = err;
435 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
447 complete(&setup_done);
448 if (err)
449 return err;
450 devtmpfs_work_loop();
451 return 0;
452}
453
454/*
455 * Get the underlying (shmem/ramfs) context ops to build ours
456 */
457static int devtmpfs_configure_context(void)
458{
459 struct fs_context *fc;
460
461 fc = fs_context_for_reconfigure(mnt->mnt_root, mnt->mnt_sb->s_flags,
462 MS_RMT_MASK);
463 if (IS_ERR(fc))
464 return PTR_ERR(fc);
465
466 /* Set up devtmpfs_context_ops based on underlying type */
467 devtmpfs_context_ops.free = fc->ops->free;
468 devtmpfs_context_ops.dup = fc->ops->dup;
469 devtmpfs_context_ops.parse_param = fc->ops->parse_param;
470 devtmpfs_context_ops.parse_monolithic = fc->ops->parse_monolithic;
471 devtmpfs_context_ops.get_tree = &devtmpfs_get_tree;
472 devtmpfs_context_ops.reconfigure = fc->ops->reconfigure;
473
474 put_fs_context(fc);
475
476 return 0;
477}
478
479/*
480 * Create devtmpfs instance, driver-core devices will add their device
481 * nodes here.
482 */
483int __init devtmpfs_init(void)
484{
485 char opts[] = "mode=0755";
486 int err;
487
488 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
489 if (IS_ERR(mnt)) {
490 pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
491 return PTR_ERR(mnt);
492 }
493
494 err = devtmpfs_configure_context();
495 if (err) {
496 pr_err("unable to configure devtmpfs type %d\n", err);
497 return err;
498 }
499
500 err = register_filesystem(&dev_fs_type);
501 if (err) {
502 pr_err("unable to register devtmpfs type %d\n", err);
503 return err;
504 }
505
506 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
507 if (!IS_ERR(thread)) {
508 wait_for_completion(&setup_done);
509 } else {
510 err = PTR_ERR(thread);
511 thread = NULL;
512 }
513
514 if (err) {
515 pr_err("unable to create devtmpfs %d\n", err);
516 unregister_filesystem(&dev_fs_type);
517 thread = NULL;
518 return err;
519 }
520
521 pr_info("initialized\n");
522 return 0;
523}