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