blob: 30d0523014e0d49e0ac6e16880a7bb08ecfbef27 [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>
David Howellse262e32d2018-11-01 23:07:23 +000028#include <uapi/linux/mount.h>
Greg Kroah-Hartmanc3a30422013-04-10 09:00:15 -070029#include "base.h"
Kay Sievers2b2af542009-04-30 15:23:42 +020030
Al Viro2780f1f2011-06-27 16:25:29 -040031static struct task_struct *thread;
Kay Sievers2b2af542009-04-30 15:23:42 +020032
33#if defined CONFIG_DEVTMPFS_MOUNT
Al Virofc14f2f2010-07-25 01:48:30 +040034static int mount_dev = 1;
Kay Sievers2b2af542009-04-30 15:23:42 +020035#else
Al Virofc14f2f2010-07-25 01:48:30 +040036static int mount_dev;
Kay Sievers2b2af542009-04-30 15:23:42 +020037#endif
38
Al Viro2780f1f2011-06-27 16:25:29 -040039static DEFINE_SPINLOCK(req_lock);
40
41static struct req {
42 struct req *next;
43 struct completion done;
44 int err;
45 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -040046 umode_t mode; /* 0 => delete */
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -070047 kuid_t uid;
48 kgid_t gid;
Al Viro2780f1f2011-06-27 16:25:29 -040049 struct device *dev;
50} *requests;
Kay Sieversed413ae2009-10-28 19:51:06 +010051
Kay Sievers2b2af542009-04-30 15:23:42 +020052static int __init mount_param(char *str)
53{
Al Virofc14f2f2010-07-25 01:48:30 +040054 mount_dev = simple_strtoul(str, NULL, 0);
Kay Sievers2b2af542009-04-30 15:23:42 +020055 return 1;
56}
57__setup("devtmpfs.mount=", mount_param);
58
Al Virod4017272019-06-01 18:43:09 -040059static struct vfsmount *mnt;
60
61static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
62 const char *dev_name, void *data)
63{
64 struct super_block *s = mnt->mnt_sb;
65 atomic_inc(&s->s_active);
66 down_write(&s->s_umount);
67 return dget(s->s_root);
68}
69
Al Virod4017272019-06-01 18:43:09 -040070static struct file_system_type internal_fs_type = {
Kay Sievers2b2af542009-04-30 15:23:42 +020071 .name = "devtmpfs",
David Howellsf3235622019-03-25 16:38:31 +000072#ifdef CONFIG_TMPFS
73 .init_fs_context = shmem_init_fs_context,
74 .parameters = &shmem_fs_parameters,
75#else
76 .init_fs_context = ramfs_init_fs_context,
77 .parameters = &ramfs_fs_parameters,
78#endif
Kay Sievers2b2af542009-04-30 15:23:42 +020079 .kill_sb = kill_litter_super,
80};
81
Al Virod4017272019-06-01 18:43:09 -040082static struct file_system_type dev_fs_type = {
83 .name = "devtmpfs",
84 .mount = public_dev_mount,
85};
86
Kay Sievers2b2af542009-04-30 15:23:42 +020087#ifdef CONFIG_BLOCK
88static inline int is_blockdev(struct device *dev)
89{
90 return dev->class == &block_class;
91}
92#else
93static inline int is_blockdev(struct device *dev) { return 0; }
94#endif
95
Al Viro2780f1f2011-06-27 16:25:29 -040096int devtmpfs_create_node(struct device *dev)
97{
98 const char *tmp = NULL;
99 struct req req;
100
101 if (!thread)
102 return 0;
103
104 req.mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700105 req.uid = GLOBAL_ROOT_UID;
106 req.gid = GLOBAL_ROOT_GID;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700107 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400108 if (!req.name)
109 return -ENOMEM;
110
111 if (req.mode == 0)
112 req.mode = 0600;
113 if (is_blockdev(dev))
114 req.mode |= S_IFBLK;
115 else
116 req.mode |= S_IFCHR;
117
118 req.dev = dev;
119
120 init_completion(&req.done);
121
122 spin_lock(&req_lock);
123 req.next = requests;
124 requests = &req;
125 spin_unlock(&req_lock);
126
127 wake_up_process(thread);
128 wait_for_completion(&req.done);
129
130 kfree(tmp);
131
132 return req.err;
133}
134
135int devtmpfs_delete_node(struct device *dev)
136{
137 const char *tmp = NULL;
138 struct req req;
139
140 if (!thread)
141 return 0;
142
Kay Sievers3c2670e2013-04-06 09:56:00 -0700143 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400144 if (!req.name)
145 return -ENOMEM;
146
147 req.mode = 0;
148 req.dev = dev;
149
150 init_completion(&req.done);
151
152 spin_lock(&req_lock);
153 req.next = requests;
154 requests = &req;
155 spin_unlock(&req_lock);
156
157 wake_up_process(thread);
158 wait_for_completion(&req.done);
159
160 kfree(tmp);
161 return req.err;
162}
163
Al Virofbd48a62011-07-24 10:47:56 -0400164static int dev_mkdir(const char *name, umode_t mode)
Kay Sievers2b2af542009-04-30 15:23:42 +0200165{
Kay Sievers2b2af542009-04-30 15:23:42 +0200166 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400167 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200168 int err;
169
Jeff Layton1ac12b42012-12-11 12:10:06 -0500170 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
Al Viro69753a02011-06-27 16:35:45 -0400171 if (IS_ERR(dentry))
172 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200173
David Howells75c3cfa2015-03-17 22:26:12 +0000174 err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
Al Viro69753a02011-06-27 16:35:45 -0400175 if (!err)
176 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000177 d_inode(dentry)->i_private = &thread;
Al Viro921a1652012-07-20 01:15:31 +0400178 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200179 return err;
180}
181
182static int create_path(const char *nodepath)
183{
Al Viro5da4e682011-06-27 16:37:12 -0400184 char *path;
185 char *s;
Al Viro9d108d22011-07-27 22:27:33 -0400186 int err = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200187
Al Viro5da4e682011-06-27 16:37:12 -0400188 /* parent directories do not exist, create them */
189 path = kstrdup(nodepath, GFP_KERNEL);
190 if (!path)
191 return -ENOMEM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200192
Al Viro5da4e682011-06-27 16:37:12 -0400193 s = path;
194 for (;;) {
195 s = strchr(s, '/');
196 if (!s)
197 break;
198 s[0] = '\0';
199 err = dev_mkdir(path, 0755);
200 if (err && err != -EEXIST)
201 break;
202 s[0] = '/';
203 s++;
Kay Sievers2b2af542009-04-30 15:23:42 +0200204 }
Al Viro5da4e682011-06-27 16:37:12 -0400205 kfree(path);
Kay Sievers2b2af542009-04-30 15:23:42 +0200206 return err;
207}
208
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700209static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
210 kgid_t gid, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200211{
Kay Sievers2b2af542009-04-30 15:23:42 +0200212 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400213 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200214 int err;
215
Al Viro69753a02011-06-27 16:35:45 -0400216 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
217 if (dentry == ERR_PTR(-ENOENT)) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200218 create_path(nodename);
Al Viro69753a02011-06-27 16:35:45 -0400219 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
Kay Sievers2b2af542009-04-30 15:23:42 +0200220 }
Al Viro69753a02011-06-27 16:35:45 -0400221 if (IS_ERR(dentry))
222 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200223
David Howells75c3cfa2015-03-17 22:26:12 +0000224 err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt);
Al Viro69753a02011-06-27 16:35:45 -0400225 if (!err) {
226 struct iattr newattrs;
Kay Sievers00926992009-10-28 19:50:57 +0100227
Al Viro69753a02011-06-27 16:35:45 -0400228 newattrs.ia_mode = mode;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700229 newattrs.ia_uid = uid;
230 newattrs.ia_gid = gid;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700231 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
Al Viro59551022016-01-22 15:40:57 -0500232 inode_lock(d_inode(dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400233 notify_change(dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500234 inode_unlock(d_inode(dentry));
Kay Sievers00926992009-10-28 19:50:57 +0100235
Al Viro69753a02011-06-27 16:35:45 -0400236 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000237 d_inode(dentry)->i_private = &thread;
Kay Sievers2b2af542009-04-30 15:23:42 +0200238 }
Al Viro921a1652012-07-20 01:15:31 +0400239 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200240 return err;
241}
242
243static int dev_rmdir(const char *name)
244{
Al Viro79714f72012-06-15 03:01:42 +0400245 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200246 struct dentry *dentry;
247 int err;
248
Al Viro79714f72012-06-15 03:01:42 +0400249 dentry = kern_path_locked(name, &parent);
250 if (IS_ERR(dentry))
251 return PTR_ERR(dentry);
David Howells75c3cfa2015-03-17 22:26:12 +0000252 if (d_really_is_positive(dentry)) {
253 if (d_inode(dentry)->i_private == &thread)
254 err = vfs_rmdir(d_inode(parent.dentry), dentry);
Al Viro79714f72012-06-15 03:01:42 +0400255 else
256 err = -EPERM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200257 } else {
Al Viro79714f72012-06-15 03:01:42 +0400258 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200259 }
Al Viro79714f72012-06-15 03:01:42 +0400260 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500261 inode_unlock(d_inode(parent.dentry));
Al Viro79714f72012-06-15 03:01:42 +0400262 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200263 return err;
264}
265
266static int delete_path(const char *nodepath)
267{
Rasmus Villemoesbe6b1df2018-08-22 13:00:07 +0200268 char *path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200269 int err = 0;
270
271 path = kstrdup(nodepath, GFP_KERNEL);
272 if (!path)
273 return -ENOMEM;
274
Kay Sieversed413ae2009-10-28 19:51:06 +0100275 for (;;) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200276 char *base;
277
278 base = strrchr(path, '/');
279 if (!base)
280 break;
281 base[0] = '\0';
282 err = dev_rmdir(path);
283 if (err)
284 break;
285 }
286
287 kfree(path);
288 return err;
289}
290
291static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
292{
293 /* did we create it */
Al Viro2780f1f2011-06-27 16:25:29 -0400294 if (inode->i_private != &thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200295 return 0;
296
297 /* does the dev_t match */
298 if (is_blockdev(dev)) {
299 if (!S_ISBLK(stat->mode))
300 return 0;
301 } else {
302 if (!S_ISCHR(stat->mode))
303 return 0;
304 }
305 if (stat->rdev != dev->devt)
306 return 0;
307
308 /* ours */
309 return 1;
310}
311
Al Viro2780f1f2011-06-27 16:25:29 -0400312static int handle_remove(const char *nodename, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200313{
Al Viro79714f72012-06-15 03:01:42 +0400314 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200315 struct dentry *dentry;
Axel Linfbde7c62013-11-16 16:15:23 +0800316 int deleted = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200317 int err;
318
Al Viro79714f72012-06-15 03:01:42 +0400319 dentry = kern_path_locked(nodename, &parent);
320 if (IS_ERR(dentry))
321 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200322
David Howells75c3cfa2015-03-17 22:26:12 +0000323 if (d_really_is_positive(dentry)) {
Al Viro79714f72012-06-15 03:01:42 +0400324 struct kstat stat;
Al Viro3dadecc2013-01-24 02:18:08 -0500325 struct path p = {.mnt = parent.mnt, .dentry = dentry};
David Howellsa528d352017-01-31 16:46:22 +0000326 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
327 AT_STATX_SYNC_AS_STAT);
David Howells75c3cfa2015-03-17 22:26:12 +0000328 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
Al Viro79714f72012-06-15 03:01:42 +0400329 struct iattr newattrs;
330 /*
331 * before unlinking this node, reset permissions
332 * of possible references like hardlinks
333 */
Eric W. Biederman91fa2cc2012-04-25 04:25:35 -0700334 newattrs.ia_uid = GLOBAL_ROOT_UID;
335 newattrs.ia_gid = GLOBAL_ROOT_GID;
Al Viro79714f72012-06-15 03:01:42 +0400336 newattrs.ia_mode = stat.mode & ~0777;
337 newattrs.ia_valid =
338 ATTR_UID|ATTR_GID|ATTR_MODE;
Al Viro59551022016-01-22 15:40:57 -0500339 inode_lock(d_inode(dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400340 notify_change(dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500341 inode_unlock(d_inode(dentry));
David Howells75c3cfa2015-03-17 22:26:12 +0000342 err = vfs_unlink(d_inode(parent.dentry), dentry, NULL);
Al Viro79714f72012-06-15 03:01:42 +0400343 if (!err || err == -ENOENT)
344 deleted = 1;
Kay Sievers2b2af542009-04-30 15:23:42 +0200345 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200346 } else {
Al Viro79714f72012-06-15 03:01:42 +0400347 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200348 }
Al Viro79714f72012-06-15 03:01:42 +0400349 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500350 inode_unlock(d_inode(parent.dentry));
Kay Sievers2b2af542009-04-30 15:23:42 +0200351
Al Viro79714f72012-06-15 03:01:42 +0400352 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200353 if (deleted && strchr(nodename, '/'))
354 delete_path(nodename);
Kay Sievers2b2af542009-04-30 15:23:42 +0200355 return err;
356}
357
358/*
359 * If configured, or requested by the commandline, devtmpfs will be
360 * auto-mounted after the kernel mounted the root filesystem.
361 */
Kay Sievers073120c2009-10-28 19:51:17 +0100362int devtmpfs_mount(const char *mntdir)
Kay Sievers2b2af542009-04-30 15:23:42 +0200363{
Kay Sievers2b2af542009-04-30 15:23:42 +0200364 int err;
365
Al Virofc14f2f2010-07-25 01:48:30 +0400366 if (!mount_dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200367 return 0;
368
Al Viro2780f1f2011-06-27 16:25:29 -0400369 if (!thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200370 return 0;
371
Al Viro33488842019-05-31 20:09:15 -0400372 err = ksys_mount("devtmpfs", mntdir, "devtmpfs", MS_SILENT, NULL);
Kay Sievers2b2af542009-04-30 15:23:42 +0200373 if (err)
374 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
375 else
376 printk(KERN_INFO "devtmpfs: mounted\n");
Kay Sievers2b2af542009-04-30 15:23:42 +0200377 return err;
378}
379
Arnaud Lacombef9e0b152011-07-21 13:16:19 -0400380static DECLARE_COMPLETION(setup_done);
Al Viro2780f1f2011-06-27 16:25:29 -0400381
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700382static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -0700383 struct device *dev)
Al Viro2780f1f2011-06-27 16:25:29 -0400384{
385 if (mode)
Kay Sievers3c2670e2013-04-06 09:56:00 -0700386 return handle_create(name, mode, uid, gid, dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400387 else
388 return handle_remove(name, dev);
389}
390
391static int devtmpfsd(void *p)
392{
Al Viro2780f1f2011-06-27 16:25:29 -0400393 int *err = p;
Dominik Brodowski9b321052018-03-11 11:34:42 +0100394 *err = ksys_unshare(CLONE_NEWNS);
Al Viro2780f1f2011-06-27 16:25:29 -0400395 if (*err)
396 goto out;
Al Virod4017272019-06-01 18:43:09 -0400397 *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
Al Viro2780f1f2011-06-27 16:25:29 -0400398 if (*err)
399 goto out;
Dominik Brodowski447016e2018-03-11 11:34:46 +0100400 ksys_chdir("/.."); /* will traverse into overmounted root */
Dominik Brodowskia16fe332018-03-11 11:34:41 +0100401 ksys_chroot(".");
Al Viro2780f1f2011-06-27 16:25:29 -0400402 complete(&setup_done);
403 while (1) {
404 spin_lock(&req_lock);
405 while (requests) {
406 struct req *req = requests;
407 requests = NULL;
408 spin_unlock(&req_lock);
409 while (req) {
Al Viroe13889b2011-07-25 14:15:50 -0400410 struct req *next = req->next;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700411 req->err = handle(req->name, req->mode,
412 req->uid, req->gid, req->dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400413 complete(&req->done);
Al Viroe13889b2011-07-25 14:15:50 -0400414 req = next;
Al Viro2780f1f2011-06-27 16:25:29 -0400415 }
416 spin_lock(&req_lock);
417 }
Kautuk Consul65e67572011-11-15 14:52:34 -0800418 __set_current_state(TASK_INTERRUPTIBLE);
Al Viro2780f1f2011-06-27 16:25:29 -0400419 spin_unlock(&req_lock);
420 schedule();
Al Viro2780f1f2011-06-27 16:25:29 -0400421 }
422 return 0;
423out:
424 complete(&setup_done);
425 return *err;
426}
427
Kay Sievers2b2af542009-04-30 15:23:42 +0200428/*
429 * Create devtmpfs instance, driver-core devices will add their device
430 * nodes here.
431 */
432int __init devtmpfs_init(void)
433{
Al Virod4017272019-06-01 18:43:09 -0400434 char opts[] = "mode=0755";
435 int err;
436
437 mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
438 if (IS_ERR(mnt)) {
439 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
440 PTR_ERR(mnt));
441 return PTR_ERR(mnt);
442 }
443 err = register_filesystem(&dev_fs_type);
Kay Sievers2b2af542009-04-30 15:23:42 +0200444 if (err) {
445 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
446 "type %i\n", err);
447 return err;
448 }
449
Al Viro2780f1f2011-06-27 16:25:29 -0400450 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
451 if (!IS_ERR(thread)) {
452 wait_for_completion(&setup_done);
453 } else {
454 err = PTR_ERR(thread);
455 thread = NULL;
456 }
457
458 if (err) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200459 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
460 unregister_filesystem(&dev_fs_type);
461 return err;
462 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200463
464 printk(KERN_INFO "devtmpfs: initialized\n");
465 return 0;
466}