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 | |
| 33 | #if defined CONFIG_DEVTMPFS_MOUNT |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 34 | static int mount_dev = 1; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 35 | #else |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 36 | static int mount_dev; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 37 | #endif |
| 38 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 39 | static DEFINE_SPINLOCK(req_lock); |
| 40 | |
| 41 | static struct req { |
| 42 | struct req *next; |
| 43 | struct completion done; |
| 44 | int err; |
| 45 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 46 | umode_t mode; /* 0 => delete */ |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 47 | kuid_t uid; |
| 48 | kgid_t gid; |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 49 | struct device *dev; |
| 50 | } *requests; |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 51 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 52 | static int __init mount_param(char *str) |
| 53 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 54 | mount_dev = simple_strtoul(str, NULL, 0); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | __setup("devtmpfs.mount=", mount_param); |
| 58 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 59 | static struct dentry *dev_mount(struct file_system_type *fs_type, int flags, |
| 60 | const char *dev_name, void *data) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 61 | { |
Peter Korsgaard | da5e4ef | 2010-03-16 21:55:21 +0100 | [diff] [blame] | 62 | #ifdef CONFIG_TMPFS |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 63 | return mount_single(fs_type, flags, data, shmem_fill_super); |
Peter Korsgaard | da5e4ef | 2010-03-16 21:55:21 +0100 | [diff] [blame] | 64 | #else |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 65 | return mount_single(fs_type, flags, data, ramfs_fill_super); |
Peter Korsgaard | da5e4ef | 2010-03-16 21:55:21 +0100 | [diff] [blame] | 66 | #endif |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static struct file_system_type dev_fs_type = { |
| 70 | .name = "devtmpfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 71 | .mount = dev_mount, |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 72 | .kill_sb = kill_litter_super, |
| 73 | }; |
| 74 | |
| 75 | #ifdef CONFIG_BLOCK |
| 76 | static inline int is_blockdev(struct device *dev) |
| 77 | { |
| 78 | return dev->class == &block_class; |
| 79 | } |
| 80 | #else |
| 81 | static inline int is_blockdev(struct device *dev) { return 0; } |
| 82 | #endif |
| 83 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 84 | int 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-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 93 | req.uid = GLOBAL_ROOT_UID; |
| 94 | req.gid = GLOBAL_ROOT_GID; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 95 | 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] | 96 | 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 | |
| 123 | int 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 Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 131 | req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 132 | 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 Viro | fbd48a6 | 2011-07-24 10:47:56 -0400 | [diff] [blame] | 152 | static int dev_mkdir(const char *name, umode_t mode) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 153 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 154 | struct dentry *dentry; |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 155 | struct path path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 156 | int err; |
| 157 | |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 158 | dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 159 | if (IS_ERR(dentry)) |
| 160 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 161 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 162 | err = vfs_mkdir(d_inode(path.dentry), dentry, mode); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 163 | if (!err) |
| 164 | /* mark as kernel-created inode */ |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 165 | d_inode(dentry)->i_private = &thread; |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 166 | done_path_create(&path, dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 167 | return err; |
| 168 | } |
| 169 | |
| 170 | static int create_path(const char *nodepath) |
| 171 | { |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 172 | char *path; |
| 173 | char *s; |
Al Viro | 9d108d2 | 2011-07-27 22:27:33 -0400 | [diff] [blame] | 174 | int err = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 175 | |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 176 | /* parent directories do not exist, create them */ |
| 177 | path = kstrdup(nodepath, GFP_KERNEL); |
| 178 | if (!path) |
| 179 | return -ENOMEM; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 180 | |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 181 | 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 Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 192 | } |
Al Viro | 5da4e68 | 2011-06-27 16:37:12 -0400 | [diff] [blame] | 193 | kfree(path); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 194 | return err; |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 197 | static int handle_create(const char *nodename, umode_t mode, kuid_t uid, |
| 198 | kgid_t gid, struct device *dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 199 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 200 | struct dentry *dentry; |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 201 | struct path path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 202 | int err; |
| 203 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 204 | dentry = kern_path_create(AT_FDCWD, nodename, &path, 0); |
| 205 | if (dentry == ERR_PTR(-ENOENT)) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 206 | create_path(nodename); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 207 | dentry = kern_path_create(AT_FDCWD, nodename, &path, 0); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 208 | } |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 209 | if (IS_ERR(dentry)) |
| 210 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 211 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 212 | err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt); |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 213 | if (!err) { |
| 214 | struct iattr newattrs; |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 215 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 216 | newattrs.ia_mode = mode; |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 217 | newattrs.ia_uid = uid; |
| 218 | newattrs.ia_gid = gid; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 219 | newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 220 | inode_lock(d_inode(dentry)); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 221 | notify_change(dentry, &newattrs, NULL); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 222 | inode_unlock(d_inode(dentry)); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 223 | |
Al Viro | 69753a0 | 2011-06-27 16:35:45 -0400 | [diff] [blame] | 224 | /* mark as kernel-created inode */ |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 225 | d_inode(dentry)->i_private = &thread; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 226 | } |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 227 | done_path_create(&path, dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 228 | return err; |
| 229 | } |
| 230 | |
| 231 | static int dev_rmdir(const char *name) |
| 232 | { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 233 | struct path parent; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 234 | struct dentry *dentry; |
| 235 | int err; |
| 236 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 237 | dentry = kern_path_locked(name, &parent); |
| 238 | if (IS_ERR(dentry)) |
| 239 | return PTR_ERR(dentry); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 240 | if (d_really_is_positive(dentry)) { |
| 241 | if (d_inode(dentry)->i_private == &thread) |
| 242 | err = vfs_rmdir(d_inode(parent.dentry), dentry); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 243 | else |
| 244 | err = -EPERM; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 245 | } else { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 246 | err = -ENOENT; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 247 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 248 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 249 | inode_unlock(d_inode(parent.dentry)); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 250 | path_put(&parent); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 251 | return err; |
| 252 | } |
| 253 | |
| 254 | static int delete_path(const char *nodepath) |
| 255 | { |
Rasmus Villemoes | be6b1df | 2018-08-22 13:00:07 +0200 | [diff] [blame] | 256 | char *path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 257 | int err = 0; |
| 258 | |
| 259 | path = kstrdup(nodepath, GFP_KERNEL); |
| 260 | if (!path) |
| 261 | return -ENOMEM; |
| 262 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 263 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 264 | 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 | |
| 279 | static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat) |
| 280 | { |
| 281 | /* did we create it */ |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 282 | if (inode->i_private != &thread) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 283 | 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 Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 300 | static int handle_remove(const char *nodename, struct device *dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 301 | { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 302 | struct path parent; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 303 | struct dentry *dentry; |
Axel Lin | fbde7c6 | 2013-11-16 16:15:23 +0800 | [diff] [blame] | 304 | int deleted = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 305 | int err; |
| 306 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 307 | dentry = kern_path_locked(nodename, &parent); |
| 308 | if (IS_ERR(dentry)) |
| 309 | return PTR_ERR(dentry); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 310 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 311 | if (d_really_is_positive(dentry)) { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 312 | struct kstat stat; |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 313 | struct path p = {.mnt = parent.mnt, .dentry = dentry}; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 314 | err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE, |
| 315 | AT_STATX_SYNC_AS_STAT); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 316 | if (!err && dev_mynode(dev, d_inode(dentry), &stat)) { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 317 | struct iattr newattrs; |
| 318 | /* |
| 319 | * before unlinking this node, reset permissions |
| 320 | * of possible references like hardlinks |
| 321 | */ |
Eric W. Biederman | 91fa2cc | 2012-04-25 04:25:35 -0700 | [diff] [blame] | 322 | newattrs.ia_uid = GLOBAL_ROOT_UID; |
| 323 | newattrs.ia_gid = GLOBAL_ROOT_GID; |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 324 | newattrs.ia_mode = stat.mode & ~0777; |
| 325 | newattrs.ia_valid = |
| 326 | ATTR_UID|ATTR_GID|ATTR_MODE; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 327 | inode_lock(d_inode(dentry)); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 328 | notify_change(dentry, &newattrs, NULL); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 329 | inode_unlock(d_inode(dentry)); |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 330 | err = vfs_unlink(d_inode(parent.dentry), dentry, NULL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 331 | if (!err || err == -ENOENT) |
| 332 | deleted = 1; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 333 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 334 | } else { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 335 | err = -ENOENT; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 336 | } |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 337 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 338 | inode_unlock(d_inode(parent.dentry)); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 339 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 340 | path_put(&parent); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 341 | if (deleted && strchr(nodename, '/')) |
| 342 | delete_path(nodename); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 343 | 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 Sievers | 073120c | 2009-10-28 19:51:17 +0100 | [diff] [blame] | 350 | int devtmpfs_mount(const char *mntdir) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 351 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 352 | int err; |
| 353 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 354 | if (!mount_dev) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 357 | if (!thread) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 358 | return 0; |
| 359 | |
Dominik Brodowski | 312db1a | 2018-03-11 11:34:39 +0100 | [diff] [blame] | 360 | err = ksys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, |
| 361 | NULL); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 362 | if (err) |
| 363 | printk(KERN_INFO "devtmpfs: error mounting %i\n", err); |
| 364 | else |
| 365 | printk(KERN_INFO "devtmpfs: mounted\n"); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 366 | return err; |
| 367 | } |
| 368 | |
Arnaud Lacombe | f9e0b15 | 2011-07-21 13:16:19 -0400 | [diff] [blame] | 369 | static DECLARE_COMPLETION(setup_done); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 370 | |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 371 | 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] | 372 | struct device *dev) |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 373 | { |
| 374 | if (mode) |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 375 | return handle_create(name, mode, uid, gid, dev); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 376 | else |
| 377 | return handle_remove(name, dev); |
| 378 | } |
| 379 | |
| 380 | static int devtmpfsd(void *p) |
| 381 | { |
| 382 | char options[] = "mode=0755"; |
| 383 | int *err = p; |
Dominik Brodowski | 9b32105 | 2018-03-11 11:34:42 +0100 | [diff] [blame] | 384 | *err = ksys_unshare(CLONE_NEWNS); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 385 | if (*err) |
| 386 | goto out; |
Dominik Brodowski | 312db1a | 2018-03-11 11:34:39 +0100 | [diff] [blame] | 387 | *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 388 | if (*err) |
| 389 | goto out; |
Dominik Brodowski | 447016e | 2018-03-11 11:34:46 +0100 | [diff] [blame] | 390 | ksys_chdir("/.."); /* will traverse into overmounted root */ |
Dominik Brodowski | a16fe33 | 2018-03-11 11:34:41 +0100 | [diff] [blame] | 391 | ksys_chroot("."); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 392 | 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 Viro | e13889b | 2011-07-25 14:15:50 -0400 | [diff] [blame] | 400 | struct req *next = req->next; |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 401 | req->err = handle(req->name, req->mode, |
| 402 | req->uid, req->gid, req->dev); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 403 | complete(&req->done); |
Al Viro | e13889b | 2011-07-25 14:15:50 -0400 | [diff] [blame] | 404 | req = next; |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 405 | } |
| 406 | spin_lock(&req_lock); |
| 407 | } |
Kautuk Consul | 65e6757 | 2011-11-15 14:52:34 -0800 | [diff] [blame] | 408 | __set_current_state(TASK_INTERRUPTIBLE); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 409 | spin_unlock(&req_lock); |
| 410 | schedule(); |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 411 | } |
| 412 | return 0; |
| 413 | out: |
| 414 | complete(&setup_done); |
| 415 | return *err; |
| 416 | } |
| 417 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 418 | /* |
| 419 | * Create devtmpfs instance, driver-core devices will add their device |
| 420 | * nodes here. |
| 421 | */ |
| 422 | int __init devtmpfs_init(void) |
| 423 | { |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 424 | int err = register_filesystem(&dev_fs_type); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 425 | if (err) { |
| 426 | printk(KERN_ERR "devtmpfs: unable to register devtmpfs " |
| 427 | "type %i\n", err); |
| 428 | return err; |
| 429 | } |
| 430 | |
Al Viro | 2780f1f | 2011-06-27 16:25:29 -0400 | [diff] [blame] | 431 | 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 Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 440 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); |
| 441 | unregister_filesystem(&dev_fs_type); |
| 442 | return err; |
| 443 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 444 | |
| 445 | printk(KERN_INFO "devtmpfs: initialized\n"); |
| 446 | return 0; |
| 447 | } |