blob: 0dbc43068eeb3cd4658daa6a4b65b8bf6a9ea0cd [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 Virofc14f2f2010-07-25 01:48:30 +040059static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
60 const char *dev_name, void *data)
Kay Sievers2b2af542009-04-30 15:23:42 +020061{
Peter Korsgaardda5e4ef2010-03-16 21:55:21 +010062#ifdef CONFIG_TMPFS
Al Virofc14f2f2010-07-25 01:48:30 +040063 return mount_single(fs_type, flags, data, shmem_fill_super);
Peter Korsgaardda5e4ef2010-03-16 21:55:21 +010064#else
Al Virofc14f2f2010-07-25 01:48:30 +040065 return mount_single(fs_type, flags, data, ramfs_fill_super);
Peter Korsgaardda5e4ef2010-03-16 21:55:21 +010066#endif
Kay Sievers2b2af542009-04-30 15:23:42 +020067}
68
69static struct file_system_type dev_fs_type = {
70 .name = "devtmpfs",
Al Virofc14f2f2010-07-25 01:48:30 +040071 .mount = dev_mount,
Kay Sievers2b2af542009-04-30 15:23:42 +020072 .kill_sb = kill_litter_super,
73};
74
75#ifdef CONFIG_BLOCK
76static inline int is_blockdev(struct device *dev)
77{
78 return dev->class == &block_class;
79}
80#else
81static inline int is_blockdev(struct device *dev) { return 0; }
82#endif
83
Al Viro2780f1f2011-06-27 16:25:29 -040084int devtmpfs_create_node(struct device *dev)
85{
86 const char *tmp = NULL;
87 struct req req;
88
89 if (!thread)
90 return 0;
91
92 req.mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -070093 req.uid = GLOBAL_ROOT_UID;
94 req.gid = GLOBAL_ROOT_GID;
Kay Sievers3c2670e2013-04-06 09:56:00 -070095 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -040096 if (!req.name)
97 return -ENOMEM;
98
99 if (req.mode == 0)
100 req.mode = 0600;
101 if (is_blockdev(dev))
102 req.mode |= S_IFBLK;
103 else
104 req.mode |= S_IFCHR;
105
106 req.dev = dev;
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
123int devtmpfs_delete_node(struct device *dev)
124{
125 const char *tmp = NULL;
126 struct req req;
127
128 if (!thread)
129 return 0;
130
Kay Sievers3c2670e2013-04-06 09:56:00 -0700131 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
Al Viro2780f1f2011-06-27 16:25:29 -0400132 if (!req.name)
133 return -ENOMEM;
134
135 req.mode = 0;
136 req.dev = dev;
137
138 init_completion(&req.done);
139
140 spin_lock(&req_lock);
141 req.next = requests;
142 requests = &req;
143 spin_unlock(&req_lock);
144
145 wake_up_process(thread);
146 wait_for_completion(&req.done);
147
148 kfree(tmp);
149 return req.err;
150}
151
Al Virofbd48a62011-07-24 10:47:56 -0400152static int dev_mkdir(const char *name, umode_t mode)
Kay Sievers2b2af542009-04-30 15:23:42 +0200153{
Kay Sievers2b2af542009-04-30 15:23:42 +0200154 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400155 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200156 int err;
157
Jeff Layton1ac12b42012-12-11 12:10:06 -0500158 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
Al Viro69753a02011-06-27 16:35:45 -0400159 if (IS_ERR(dentry))
160 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200161
David Howells75c3cfa2015-03-17 22:26:12 +0000162 err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
Al Viro69753a02011-06-27 16:35:45 -0400163 if (!err)
164 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000165 d_inode(dentry)->i_private = &thread;
Al Viro921a1652012-07-20 01:15:31 +0400166 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200167 return err;
168}
169
170static int create_path(const char *nodepath)
171{
Al Viro5da4e682011-06-27 16:37:12 -0400172 char *path;
173 char *s;
Al Viro9d108d22011-07-27 22:27:33 -0400174 int err = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200175
Al Viro5da4e682011-06-27 16:37:12 -0400176 /* parent directories do not exist, create them */
177 path = kstrdup(nodepath, GFP_KERNEL);
178 if (!path)
179 return -ENOMEM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200180
Al Viro5da4e682011-06-27 16:37:12 -0400181 s = path;
182 for (;;) {
183 s = strchr(s, '/');
184 if (!s)
185 break;
186 s[0] = '\0';
187 err = dev_mkdir(path, 0755);
188 if (err && err != -EEXIST)
189 break;
190 s[0] = '/';
191 s++;
Kay Sievers2b2af542009-04-30 15:23:42 +0200192 }
Al Viro5da4e682011-06-27 16:37:12 -0400193 kfree(path);
Kay Sievers2b2af542009-04-30 15:23:42 +0200194 return err;
195}
196
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700197static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
198 kgid_t gid, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200199{
Kay Sievers2b2af542009-04-30 15:23:42 +0200200 struct dentry *dentry;
Al Viro69753a02011-06-27 16:35:45 -0400201 struct path path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200202 int err;
203
Al Viro69753a02011-06-27 16:35:45 -0400204 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
205 if (dentry == ERR_PTR(-ENOENT)) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200206 create_path(nodename);
Al Viro69753a02011-06-27 16:35:45 -0400207 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
Kay Sievers2b2af542009-04-30 15:23:42 +0200208 }
Al Viro69753a02011-06-27 16:35:45 -0400209 if (IS_ERR(dentry))
210 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200211
David Howells75c3cfa2015-03-17 22:26:12 +0000212 err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt);
Al Viro69753a02011-06-27 16:35:45 -0400213 if (!err) {
214 struct iattr newattrs;
Kay Sievers00926992009-10-28 19:50:57 +0100215
Al Viro69753a02011-06-27 16:35:45 -0400216 newattrs.ia_mode = mode;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700217 newattrs.ia_uid = uid;
218 newattrs.ia_gid = gid;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700219 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
Al Viro59551022016-01-22 15:40:57 -0500220 inode_lock(d_inode(dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400221 notify_change(dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500222 inode_unlock(d_inode(dentry));
Kay Sievers00926992009-10-28 19:50:57 +0100223
Al Viro69753a02011-06-27 16:35:45 -0400224 /* mark as kernel-created inode */
David Howells75c3cfa2015-03-17 22:26:12 +0000225 d_inode(dentry)->i_private = &thread;
Kay Sievers2b2af542009-04-30 15:23:42 +0200226 }
Al Viro921a1652012-07-20 01:15:31 +0400227 done_path_create(&path, dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200228 return err;
229}
230
231static int dev_rmdir(const char *name)
232{
Al Viro79714f72012-06-15 03:01:42 +0400233 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200234 struct dentry *dentry;
235 int err;
236
Al Viro79714f72012-06-15 03:01:42 +0400237 dentry = kern_path_locked(name, &parent);
238 if (IS_ERR(dentry))
239 return PTR_ERR(dentry);
David Howells75c3cfa2015-03-17 22:26:12 +0000240 if (d_really_is_positive(dentry)) {
241 if (d_inode(dentry)->i_private == &thread)
242 err = vfs_rmdir(d_inode(parent.dentry), dentry);
Al Viro79714f72012-06-15 03:01:42 +0400243 else
244 err = -EPERM;
Kay Sievers2b2af542009-04-30 15:23:42 +0200245 } else {
Al Viro79714f72012-06-15 03:01:42 +0400246 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200247 }
Al Viro79714f72012-06-15 03:01:42 +0400248 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500249 inode_unlock(d_inode(parent.dentry));
Al Viro79714f72012-06-15 03:01:42 +0400250 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200251 return err;
252}
253
254static int delete_path(const char *nodepath)
255{
Rasmus Villemoesbe6b1df2018-08-22 13:00:07 +0200256 char *path;
Kay Sievers2b2af542009-04-30 15:23:42 +0200257 int err = 0;
258
259 path = kstrdup(nodepath, GFP_KERNEL);
260 if (!path)
261 return -ENOMEM;
262
Kay Sieversed413ae2009-10-28 19:51:06 +0100263 for (;;) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200264 char *base;
265
266 base = strrchr(path, '/');
267 if (!base)
268 break;
269 base[0] = '\0';
270 err = dev_rmdir(path);
271 if (err)
272 break;
273 }
274
275 kfree(path);
276 return err;
277}
278
279static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
280{
281 /* did we create it */
Al Viro2780f1f2011-06-27 16:25:29 -0400282 if (inode->i_private != &thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200283 return 0;
284
285 /* does the dev_t match */
286 if (is_blockdev(dev)) {
287 if (!S_ISBLK(stat->mode))
288 return 0;
289 } else {
290 if (!S_ISCHR(stat->mode))
291 return 0;
292 }
293 if (stat->rdev != dev->devt)
294 return 0;
295
296 /* ours */
297 return 1;
298}
299
Al Viro2780f1f2011-06-27 16:25:29 -0400300static int handle_remove(const char *nodename, struct device *dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200301{
Al Viro79714f72012-06-15 03:01:42 +0400302 struct path parent;
Kay Sievers2b2af542009-04-30 15:23:42 +0200303 struct dentry *dentry;
Axel Linfbde7c62013-11-16 16:15:23 +0800304 int deleted = 0;
Kay Sievers2b2af542009-04-30 15:23:42 +0200305 int err;
306
Al Viro79714f72012-06-15 03:01:42 +0400307 dentry = kern_path_locked(nodename, &parent);
308 if (IS_ERR(dentry))
309 return PTR_ERR(dentry);
Kay Sievers2b2af542009-04-30 15:23:42 +0200310
David Howells75c3cfa2015-03-17 22:26:12 +0000311 if (d_really_is_positive(dentry)) {
Al Viro79714f72012-06-15 03:01:42 +0400312 struct kstat stat;
Al Viro3dadecc2013-01-24 02:18:08 -0500313 struct path p = {.mnt = parent.mnt, .dentry = dentry};
David Howellsa528d352017-01-31 16:46:22 +0000314 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
315 AT_STATX_SYNC_AS_STAT);
David Howells75c3cfa2015-03-17 22:26:12 +0000316 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
Al Viro79714f72012-06-15 03:01:42 +0400317 struct iattr newattrs;
318 /*
319 * before unlinking this node, reset permissions
320 * of possible references like hardlinks
321 */
Eric W. Biederman91fa2cc2012-04-25 04:25:35 -0700322 newattrs.ia_uid = GLOBAL_ROOT_UID;
323 newattrs.ia_gid = GLOBAL_ROOT_GID;
Al Viro79714f72012-06-15 03:01:42 +0400324 newattrs.ia_mode = stat.mode & ~0777;
325 newattrs.ia_valid =
326 ATTR_UID|ATTR_GID|ATTR_MODE;
Al Viro59551022016-01-22 15:40:57 -0500327 inode_lock(d_inode(dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400328 notify_change(dentry, &newattrs, NULL);
Al Viro59551022016-01-22 15:40:57 -0500329 inode_unlock(d_inode(dentry));
David Howells75c3cfa2015-03-17 22:26:12 +0000330 err = vfs_unlink(d_inode(parent.dentry), dentry, NULL);
Al Viro79714f72012-06-15 03:01:42 +0400331 if (!err || err == -ENOENT)
332 deleted = 1;
Kay Sievers2b2af542009-04-30 15:23:42 +0200333 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200334 } else {
Al Viro79714f72012-06-15 03:01:42 +0400335 err = -ENOENT;
Kay Sievers2b2af542009-04-30 15:23:42 +0200336 }
Al Viro79714f72012-06-15 03:01:42 +0400337 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -0500338 inode_unlock(d_inode(parent.dentry));
Kay Sievers2b2af542009-04-30 15:23:42 +0200339
Al Viro79714f72012-06-15 03:01:42 +0400340 path_put(&parent);
Kay Sievers2b2af542009-04-30 15:23:42 +0200341 if (deleted && strchr(nodename, '/'))
342 delete_path(nodename);
Kay Sievers2b2af542009-04-30 15:23:42 +0200343 return err;
344}
345
346/*
347 * If configured, or requested by the commandline, devtmpfs will be
348 * auto-mounted after the kernel mounted the root filesystem.
349 */
Kay Sievers073120c2009-10-28 19:51:17 +0100350int devtmpfs_mount(const char *mntdir)
Kay Sievers2b2af542009-04-30 15:23:42 +0200351{
Kay Sievers2b2af542009-04-30 15:23:42 +0200352 int err;
353
Al Virofc14f2f2010-07-25 01:48:30 +0400354 if (!mount_dev)
Kay Sievers2b2af542009-04-30 15:23:42 +0200355 return 0;
356
Al Viro2780f1f2011-06-27 16:25:29 -0400357 if (!thread)
Kay Sievers2b2af542009-04-30 15:23:42 +0200358 return 0;
359
Dominik Brodowski312db1a2018-03-11 11:34:39 +0100360 err = ksys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT,
361 NULL);
Kay Sievers2b2af542009-04-30 15:23:42 +0200362 if (err)
363 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
364 else
365 printk(KERN_INFO "devtmpfs: mounted\n");
Kay Sievers2b2af542009-04-30 15:23:42 +0200366 return err;
367}
368
Arnaud Lacombef9e0b152011-07-21 13:16:19 -0400369static DECLARE_COMPLETION(setup_done);
Al Viro2780f1f2011-06-27 16:25:29 -0400370
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700371static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -0700372 struct device *dev)
Al Viro2780f1f2011-06-27 16:25:29 -0400373{
374 if (mode)
Kay Sievers3c2670e2013-04-06 09:56:00 -0700375 return handle_create(name, mode, uid, gid, dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400376 else
377 return handle_remove(name, dev);
378}
379
380static int devtmpfsd(void *p)
381{
382 char options[] = "mode=0755";
383 int *err = p;
Dominik Brodowski9b321052018-03-11 11:34:42 +0100384 *err = ksys_unshare(CLONE_NEWNS);
Al Viro2780f1f2011-06-27 16:25:29 -0400385 if (*err)
386 goto out;
Dominik Brodowski312db1a2018-03-11 11:34:39 +0100387 *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
Al Viro2780f1f2011-06-27 16:25:29 -0400388 if (*err)
389 goto out;
Dominik Brodowski447016e2018-03-11 11:34:46 +0100390 ksys_chdir("/.."); /* will traverse into overmounted root */
Dominik Brodowskia16fe332018-03-11 11:34:41 +0100391 ksys_chroot(".");
Al Viro2780f1f2011-06-27 16:25:29 -0400392 complete(&setup_done);
393 while (1) {
394 spin_lock(&req_lock);
395 while (requests) {
396 struct req *req = requests;
397 requests = NULL;
398 spin_unlock(&req_lock);
399 while (req) {
Al Viroe13889b2011-07-25 14:15:50 -0400400 struct req *next = req->next;
Kay Sievers3c2670e2013-04-06 09:56:00 -0700401 req->err = handle(req->name, req->mode,
402 req->uid, req->gid, req->dev);
Al Viro2780f1f2011-06-27 16:25:29 -0400403 complete(&req->done);
Al Viroe13889b2011-07-25 14:15:50 -0400404 req = next;
Al Viro2780f1f2011-06-27 16:25:29 -0400405 }
406 spin_lock(&req_lock);
407 }
Kautuk Consul65e67572011-11-15 14:52:34 -0800408 __set_current_state(TASK_INTERRUPTIBLE);
Al Viro2780f1f2011-06-27 16:25:29 -0400409 spin_unlock(&req_lock);
410 schedule();
Al Viro2780f1f2011-06-27 16:25:29 -0400411 }
412 return 0;
413out:
414 complete(&setup_done);
415 return *err;
416}
417
Kay Sievers2b2af542009-04-30 15:23:42 +0200418/*
419 * Create devtmpfs instance, driver-core devices will add their device
420 * nodes here.
421 */
422int __init devtmpfs_init(void)
423{
Al Viro2780f1f2011-06-27 16:25:29 -0400424 int err = register_filesystem(&dev_fs_type);
Kay Sievers2b2af542009-04-30 15:23:42 +0200425 if (err) {
426 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
427 "type %i\n", err);
428 return err;
429 }
430
Al Viro2780f1f2011-06-27 16:25:29 -0400431 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
432 if (!IS_ERR(thread)) {
433 wait_for_completion(&setup_done);
434 } else {
435 err = PTR_ERR(thread);
436 thread = NULL;
437 }
438
439 if (err) {
Kay Sievers2b2af542009-04-30 15:23:42 +0200440 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
441 unregister_filesystem(&dev_fs_type);
442 return err;
443 }
Kay Sievers2b2af542009-04-30 15:23:42 +0200444
445 printk(KERN_INFO "devtmpfs: initialized\n");
446 return 0;
447}