Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 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 |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 10 | * filesystem. |
Peter Korsgaard | 02fbe5e | 2012-04-17 12:13:18 +0200 | [diff] [blame] | 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. |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 14 | */ |
| 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 Korsgaard | da5e4ef | 2010-03-16 21:55:21 +0100 | [diff] [blame] | 24 | #include <linux/ramfs.h> |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 25 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 27 | #include <linux/kthread.h> |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 28 | #include <uapi/linux/mount.h> |
Greg Kroah-Hartman | c3a3042 | 2013-04-10 09:00:15 -0700 | [diff] [blame] | 29 | #include "base.h" |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 30 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 31 | static struct task_struct *thread; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 32 | |
Rasmus Villemoes | fad1db8 | 2020-01-15 19:41:52 +0100 | [diff] [blame] | 33 | static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 34 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(req_lock); |
| 36 | |
| 37 | static struct req { |
| 38 | struct req *next; |
| 39 | struct completion done; |
| 40 | int err; |
| 41 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 42 | umode_t mode; /* 0 => delete */ |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 43 | kuid_t uid; |
| 44 | kgid_t gid; |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 45 | struct device *dev; |
| 46 | } *requests; |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 47 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 48 | static int __init mount_param(char *str) |
| 49 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 50 | mount_dev = simple_strtoul(str, NULL, 0); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | __setup("devtmpfs.mount=", mount_param); |
| 54 | |
Al Viro | d401727 | 2019-06-01 18:43:09 -0400 | [diff] [blame] | 55 | static struct vfsmount *mnt; |
| 56 | |
| 57 | static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags, |
| 58 | const char *dev_name, void *data) |
| 59 | { |
| 60 | struct super_block *s = mnt->mnt_sb; |
| 61 | atomic_inc(&s->s_active); |
| 62 | down_write(&s->s_umount); |
| 63 | return dget(s->s_root); |
| 64 | } |
| 65 | |
Al Viro | d401727 | 2019-06-01 18:43:09 -0400 | [diff] [blame] | 66 | static struct file_system_type internal_fs_type = { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 67 | .name = "devtmpfs", |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 68 | #ifdef CONFIG_TMPFS |
| 69 | .init_fs_context = shmem_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 70 | .parameters = shmem_fs_parameters, |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 71 | #else |
| 72 | .init_fs_context = ramfs_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 73 | .parameters = ramfs_fs_parameters, |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 74 | #endif |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 75 | .kill_sb = kill_litter_super, |
| 76 | }; |
| 77 | |
Al Viro | d401727 | 2019-06-01 18:43:09 -0400 | [diff] [blame] | 78 | static struct file_system_type dev_fs_type = { |
| 79 | .name = "devtmpfs", |
| 80 | .mount = public_dev_mount, |
| 81 | }; |
| 82 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 83 | #ifdef CONFIG_BLOCK |
| 84 | static inline int is_blockdev(struct device *dev) |
| 85 | { |
| 86 | return dev->class == &block_class; |
| 87 | } |
| 88 | #else |
| 89 | static inline int is_blockdev(struct device *dev) { return 0; } |
| 90 | #endif |
| 91 | |
Rasmus Villemoes | 72a9cc9 | 2020-01-15 19:41:53 +0100 | [diff] [blame] | 92 | static int devtmpfs_submit_req(struct req *req, const char *tmp) |
| 93 | { |
| 94 | init_completion(&req->done); |
| 95 | |
| 96 | spin_lock(&req_lock); |
| 97 | req->next = requests; |
| 98 | requests = req; |
| 99 | spin_unlock(&req_lock); |
| 100 | |
| 101 | wake_up_process(thread); |
| 102 | wait_for_completion(&req->done); |
| 103 | |
| 104 | kfree(tmp); |
| 105 | |
| 106 | return req->err; |
| 107 | } |
| 108 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 109 | int devtmpfs_create_node(struct device *dev) |
| 110 | { |
| 111 | const char *tmp = NULL; |
| 112 | struct req req; |
| 113 | |
| 114 | if (!thread) |
| 115 | return 0; |
| 116 | |
| 117 | req.mode = 0; |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 118 | req.uid = GLOBAL_ROOT_UID; |
| 119 | req.gid = GLOBAL_ROOT_GID; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 120 | req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 121 | if (!req.name) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | if (req.mode == 0) |
| 125 | req.mode = 0600; |
| 126 | if (is_blockdev(dev)) |
| 127 | req.mode |= S_IFBLK; |
| 128 | else |
| 129 | req.mode |= S_IFCHR; |
| 130 | |
| 131 | req.dev = dev; |
| 132 | |
Rasmus Villemoes | 72a9cc9 | 2020-01-15 19:41:53 +0100 | [diff] [blame] | 133 | return devtmpfs_submit_req(&req, tmp); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int devtmpfs_delete_node(struct device *dev) |
| 137 | { |
| 138 | const char *tmp = NULL; |
| 139 | struct req req; |
| 140 | |
| 141 | if (!thread) |
| 142 | return 0; |
| 143 | |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 144 | req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 145 | if (!req.name) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | req.mode = 0; |
| 149 | req.dev = dev; |
| 150 | |
Rasmus Villemoes | 72a9cc9 | 2020-01-15 19:41:53 +0100 | [diff] [blame] | 151 | return devtmpfs_submit_req(&req, tmp); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Al Viro | fbd48a6 | 2011-07-24 10:47:56 -0400 | [diff] [blame] | 154 | static int dev_mkdir(const char *name, umode_t mode) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 155 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 156 | struct dentry *dentry; |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 157 | struct path path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 158 | int err; |
| 159 | |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 160 | dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 161 | if (IS_ERR(dentry)) |
| 162 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 163 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 164 | err = vfs_mkdir(d_inode(path.dentry), dentry, mode); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 165 | if (!err) |
| 166 | /* mark as kernel-created inode */ |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 167 | d_inode(dentry)->i_private = &thread; |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 168 | done_path_create(&path, dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 169 | return err; |
| 170 | } |
| 171 | |
| 172 | static int create_path(const char *nodepath) |
| 173 | { |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 174 | char *path; |
| 175 | char *s; |
Al Viro | 9d108d2 | 2011-07-27 22:27:33 -0400 | [diff] [blame] | 176 | int err = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 177 | |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 178 | /* parent directories do not exist, create them */ |
| 179 | path = kstrdup(nodepath, GFP_KERNEL); |
| 180 | if (!path) |
| 181 | return -ENOMEM; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 182 | |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 183 | s = path; |
| 184 | for (;;) { |
| 185 | s = strchr(s, '/'); |
| 186 | if (!s) |
| 187 | break; |
| 188 | s[0] = '\0'; |
| 189 | err = dev_mkdir(path, 0755); |
| 190 | if (err && err != -EEXIST) |
| 191 | break; |
| 192 | s[0] = '/'; |
| 193 | s++; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 194 | } |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 195 | kfree(path); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 196 | return err; |
| 197 | } |
| 198 | |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 199 | static int handle_create(const char *nodename, umode_t mode, kuid_t uid, |
| 200 | kgid_t gid, struct device *dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 201 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 202 | struct dentry *dentry; |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 203 | struct path path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 204 | int err; |
| 205 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 206 | dentry = kern_path_create(AT_FDCWD, nodename, &path, 0); |
| 207 | if (dentry == ERR_PTR(-ENOENT)) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 208 | create_path(nodename); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 209 | dentry = kern_path_create(AT_FDCWD, nodename, &path, 0); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 210 | } |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 211 | if (IS_ERR(dentry)) |
| 212 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 213 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 214 | err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 215 | if (!err) { |
| 216 | struct iattr newattrs; |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 217 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 218 | newattrs.ia_mode = mode; |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 219 | newattrs.ia_uid = uid; |
| 220 | newattrs.ia_gid = gid; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 221 | newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 222 | inode_lock(d_inode(dentry)); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 223 | notify_change(dentry, &newattrs, NULL); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 224 | inode_unlock(d_inode(dentry)); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 225 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 226 | /* mark as kernel-created inode */ |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 227 | d_inode(dentry)->i_private = &thread; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 228 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 229 | done_path_create(&path, dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 230 | return err; |
| 231 | } |
| 232 | |
| 233 | static int dev_rmdir(const char *name) |
| 234 | { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 235 | struct path parent; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 236 | struct dentry *dentry; |
| 237 | int err; |
| 238 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 239 | dentry = kern_path_locked(name, &parent); |
| 240 | if (IS_ERR(dentry)) |
| 241 | return PTR_ERR(dentry); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 242 | if (d_really_is_positive(dentry)) { |
| 243 | if (d_inode(dentry)->i_private == &thread) |
| 244 | err = vfs_rmdir(d_inode(parent.dentry), dentry); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 245 | else |
| 246 | err = -EPERM; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 247 | } else { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 248 | err = -ENOENT; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 249 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 250 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 251 | inode_unlock(d_inode(parent.dentry)); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 252 | path_put(&parent); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 253 | return err; |
| 254 | } |
| 255 | |
| 256 | static int delete_path(const char *nodepath) |
| 257 | { |
Rasmus Villemoes | be6b1df | 2018-08-22 13:00:07 +0200 | [diff] [blame] | 258 | char *path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 259 | int err = 0; |
| 260 | |
| 261 | path = kstrdup(nodepath, GFP_KERNEL); |
| 262 | if (!path) |
| 263 | return -ENOMEM; |
| 264 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 265 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 266 | char *base; |
| 267 | |
| 268 | base = strrchr(path, '/'); |
| 269 | if (!base) |
| 270 | break; |
| 271 | base[0] = '\0'; |
| 272 | err = dev_rmdir(path); |
| 273 | if (err) |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | kfree(path); |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat) |
| 282 | { |
| 283 | /* did we create it */ |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 284 | if (inode->i_private != &thread) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 285 | return 0; |
| 286 | |
| 287 | /* does the dev_t match */ |
| 288 | if (is_blockdev(dev)) { |
| 289 | if (!S_ISBLK(stat->mode)) |
| 290 | return 0; |
| 291 | } else { |
| 292 | if (!S_ISCHR(stat->mode)) |
| 293 | return 0; |
| 294 | } |
| 295 | if (stat->rdev != dev->devt) |
| 296 | return 0; |
| 297 | |
| 298 | /* ours */ |
| 299 | return 1; |
| 300 | } |
| 301 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 302 | static int handle_remove(const char *nodename, struct device *dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 303 | { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 304 | struct path parent; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 305 | struct dentry *dentry; |
Axel Lin | fbde7c6 | 2013-11-16 16:15:23 +0800 | [diff] [blame] | 306 | int deleted = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 307 | int err; |
| 308 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 309 | dentry = kern_path_locked(nodename, &parent); |
| 310 | if (IS_ERR(dentry)) |
| 311 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 312 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 313 | if (d_really_is_positive(dentry)) { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 314 | struct kstat stat; |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 315 | struct path p = {.mnt = parent.mnt, .dentry = dentry}; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 316 | err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE, |
| 317 | AT_STATX_SYNC_AS_STAT); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 318 | if (!err && dev_mynode(dev, d_inode(dentry), &stat)) { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 319 | struct iattr newattrs; |
| 320 | /* |
| 321 | * before unlinking this node, reset permissions |
| 322 | * of possible references like hardlinks |
| 323 | */ |
Eric W. Biederman | 91fa2cc | 2012-04-25 04:25:35 -0700 | [diff] [blame] | 324 | newattrs.ia_uid = GLOBAL_ROOT_UID; |
| 325 | newattrs.ia_gid = GLOBAL_ROOT_GID; |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 326 | newattrs.ia_mode = stat.mode & ~0777; |
| 327 | newattrs.ia_valid = |
| 328 | ATTR_UID|ATTR_GID|ATTR_MODE; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 329 | inode_lock(d_inode(dentry)); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 330 | notify_change(dentry, &newattrs, NULL); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 331 | inode_unlock(d_inode(dentry)); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 332 | err = vfs_unlink(d_inode(parent.dentry), dentry, NULL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 333 | if (!err || err == -ENOENT) |
| 334 | deleted = 1; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 335 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 336 | } else { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 337 | err = -ENOENT; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 338 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 339 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 340 | inode_unlock(d_inode(parent.dentry)); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 341 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 342 | path_put(&parent); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 343 | if (deleted && strchr(nodename, '/')) |
| 344 | delete_path(nodename); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 345 | return err; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * If configured, or requested by the commandline, devtmpfs will be |
| 350 | * auto-mounted after the kernel mounted the root filesystem. |
| 351 | */ |
Rasmus Villemoes | fad1db8 | 2020-01-15 19:41:52 +0100 | [diff] [blame] | 352 | int __init devtmpfs_mount(void) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 353 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 354 | int err; |
| 355 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 356 | if (!mount_dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 359 | if (!thread) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | |
Dominik Brodowski | 5e787db | 2018-10-23 22:10:35 +0200 | [diff] [blame] | 362 | err = do_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 363 | if (err) |
| 364 | printk(KERN_INFO "devtmpfs: error mounting %i\n", err); |
| 365 | else |
| 366 | printk(KERN_INFO "devtmpfs: mounted\n"); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 367 | return err; |
| 368 | } |
| 369 | |
Arnaud Lacombe | f9e0b15 | 2011-07-21 13:16:19 -0400 | [diff] [blame] | 370 | static DECLARE_COMPLETION(setup_done); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 371 | |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 372 | static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid, |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 373 | struct device *dev) |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 374 | { |
| 375 | if (mode) |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 376 | return handle_create(name, mode, uid, gid, dev); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 377 | else |
| 378 | return handle_remove(name, dev); |
| 379 | } |
| 380 | |
Christoph Hellwig | bcbacc4 | 2020-07-22 10:48:25 +0200 | [diff] [blame^] | 381 | static void __noreturn devtmpfs_work_loop(void) |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 382 | { |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 383 | while (1) { |
| 384 | spin_lock(&req_lock); |
| 385 | while (requests) { |
| 386 | struct req *req = requests; |
| 387 | requests = NULL; |
| 388 | spin_unlock(&req_lock); |
| 389 | while (req) { |
Al Viro | e13889b | 2011-07-25 14:15:50 -0400 | [diff] [blame] | 390 | struct req *next = req->next; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 391 | req->err = handle(req->name, req->mode, |
| 392 | req->uid, req->gid, req->dev); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 393 | complete(&req->done); |
Al Viro | e13889b | 2011-07-25 14:15:50 -0400 | [diff] [blame] | 394 | req = next; |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 395 | } |
| 396 | spin_lock(&req_lock); |
| 397 | } |
Kautuk Consul | 65e6757 | 2011-11-15 14:52:34 -0800 | [diff] [blame] | 398 | __set_current_state(TASK_INTERRUPTIBLE); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 399 | spin_unlock(&req_lock); |
| 400 | schedule(); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 401 | } |
Christoph Hellwig | bcbacc4 | 2020-07-22 10:48:25 +0200 | [diff] [blame^] | 402 | } |
| 403 | |
| 404 | static int __init devtmpfs_setup(void *p) |
| 405 | { |
| 406 | int err; |
| 407 | |
| 408 | err = ksys_unshare(CLONE_NEWNS); |
| 409 | if (err) |
| 410 | goto out; |
| 411 | err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL); |
| 412 | if (err) |
| 413 | goto out; |
| 414 | ksys_chdir("/.."); /* will traverse into overmounted root */ |
| 415 | ksys_chroot("."); |
| 416 | out: |
| 417 | *(int *)p = err; |
| 418 | complete(&setup_done); |
| 419 | return err; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * The __ref is because devtmpfs_setup needs to be __init for the routines it |
| 424 | * calls. That call is done while devtmpfs_init, which is marked __init, |
| 425 | * synchronously waits for it to complete. |
| 426 | */ |
| 427 | static int __ref devtmpfsd(void *p) |
| 428 | { |
| 429 | int err = devtmpfs_setup(p); |
| 430 | |
| 431 | if (err) |
| 432 | return err; |
| 433 | devtmpfs_work_loop(); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 434 | return 0; |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 437 | /* |
| 438 | * Create devtmpfs instance, driver-core devices will add their device |
| 439 | * nodes here. |
| 440 | */ |
| 441 | int __init devtmpfs_init(void) |
| 442 | { |
Al Viro | d401727 | 2019-06-01 18:43:09 -0400 | [diff] [blame] | 443 | char opts[] = "mode=0755"; |
| 444 | int err; |
| 445 | |
| 446 | mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts); |
| 447 | if (IS_ERR(mnt)) { |
| 448 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n", |
| 449 | PTR_ERR(mnt)); |
| 450 | return PTR_ERR(mnt); |
| 451 | } |
| 452 | err = register_filesystem(&dev_fs_type); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 453 | if (err) { |
| 454 | printk(KERN_ERR "devtmpfs: unable to register devtmpfs " |
| 455 | "type %i\n", err); |
| 456 | return err; |
| 457 | } |
| 458 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 459 | thread = kthread_run(devtmpfsd, &err, "kdevtmpfs"); |
| 460 | if (!IS_ERR(thread)) { |
| 461 | wait_for_completion(&setup_done); |
| 462 | } else { |
| 463 | err = PTR_ERR(thread); |
| 464 | thread = NULL; |
| 465 | } |
| 466 | |
| 467 | if (err) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 468 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); |
| 469 | unregister_filesystem(&dev_fs_type); |
| 470 | return err; |
| 471 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 472 | |
| 473 | printk(KERN_INFO "devtmpfs: initialized\n"); |
| 474 | return 0; |
| 475 | } |