Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * devtmpfs - kernel-maintained tmpfs-based /dev |
| 3 | * |
| 4 | * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org> |
| 5 | * |
| 6 | * During bootup, before any driver core device is registered, |
| 7 | * devtmpfs, a tmpfs-based filesystem is created. Every driver-core |
| 8 | * device which requests a device node, will add a node in this |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 9 | * filesystem. |
| 10 | * By default, all devices are named after the the name of the |
| 11 | * device, owned by root and have a default mode of 0600. Subsystems |
| 12 | * can overwrite the default setting if needed. |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/mount.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/genhd.h> |
| 20 | #include <linux/namei.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/shmem_fs.h> |
| 23 | #include <linux/cred.h> |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 24 | #include <linux/sched.h> |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 25 | #include <linux/init_task.h> |
| 26 | |
| 27 | static struct vfsmount *dev_mnt; |
| 28 | |
| 29 | #if defined CONFIG_DEVTMPFS_MOUNT |
| 30 | static int dev_mount = 1; |
| 31 | #else |
| 32 | static int dev_mount; |
| 33 | #endif |
| 34 | |
Thomas Gleixner | f1f76f8 | 2009-12-16 21:31:33 +0000 | [diff] [blame] | 35 | static DEFINE_MUTEX(dirlock); |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 36 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 37 | static int __init mount_param(char *str) |
| 38 | { |
| 39 | dev_mount = simple_strtoul(str, NULL, 0); |
| 40 | return 1; |
| 41 | } |
| 42 | __setup("devtmpfs.mount=", mount_param); |
| 43 | |
| 44 | static int dev_get_sb(struct file_system_type *fs_type, int flags, |
| 45 | const char *dev_name, void *data, struct vfsmount *mnt) |
| 46 | { |
| 47 | return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt); |
| 48 | } |
| 49 | |
| 50 | static struct file_system_type dev_fs_type = { |
| 51 | .name = "devtmpfs", |
| 52 | .get_sb = dev_get_sb, |
| 53 | .kill_sb = kill_litter_super, |
| 54 | }; |
| 55 | |
| 56 | #ifdef CONFIG_BLOCK |
| 57 | static inline int is_blockdev(struct device *dev) |
| 58 | { |
| 59 | return dev->class == &block_class; |
| 60 | } |
| 61 | #else |
| 62 | static inline int is_blockdev(struct device *dev) { return 0; } |
| 63 | #endif |
| 64 | |
| 65 | static int dev_mkdir(const char *name, mode_t mode) |
| 66 | { |
| 67 | struct nameidata nd; |
| 68 | struct dentry *dentry; |
| 69 | int err; |
| 70 | |
| 71 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 72 | name, LOOKUP_PARENT, &nd); |
| 73 | if (err) |
| 74 | return err; |
| 75 | |
| 76 | dentry = lookup_create(&nd, 1); |
| 77 | if (!IS_ERR(dentry)) { |
| 78 | err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 79 | if (!err) |
| 80 | /* mark as kernel-created inode */ |
| 81 | dentry->d_inode->i_private = &dev_mnt; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 82 | dput(dentry); |
| 83 | } else { |
| 84 | err = PTR_ERR(dentry); |
| 85 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 86 | |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 87 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 88 | path_put(&nd.path); |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | static int create_path(const char *nodepath) |
| 93 | { |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 94 | int err; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 95 | |
Thomas Gleixner | f1f76f8 | 2009-12-16 21:31:33 +0000 | [diff] [blame] | 96 | mutex_lock(&dirlock); |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 97 | err = dev_mkdir(nodepath, 0755); |
| 98 | if (err == -ENOENT) { |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 99 | char *path; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 100 | char *s; |
| 101 | |
| 102 | /* parent directories do not exist, create them */ |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 103 | path = kstrdup(nodepath, GFP_KERNEL); |
Kay Sievers | 8042273 | 2009-12-22 22:25:16 +0100 | [diff] [blame] | 104 | if (!path) { |
| 105 | err = -ENOMEM; |
| 106 | goto out; |
| 107 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 108 | s = path; |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 109 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 110 | s = strchr(s, '/'); |
| 111 | if (!s) |
| 112 | break; |
| 113 | s[0] = '\0'; |
| 114 | err = dev_mkdir(path, 0755); |
| 115 | if (err && err != -EEXIST) |
| 116 | break; |
| 117 | s[0] = '/'; |
| 118 | s++; |
| 119 | } |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 120 | kfree(path); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 121 | } |
Kay Sievers | 8042273 | 2009-12-22 22:25:16 +0100 | [diff] [blame] | 122 | out: |
Thomas Gleixner | f1f76f8 | 2009-12-16 21:31:33 +0000 | [diff] [blame] | 123 | mutex_unlock(&dirlock); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 124 | return err; |
| 125 | } |
| 126 | |
| 127 | int devtmpfs_create_node(struct device *dev) |
| 128 | { |
| 129 | const char *tmp = NULL; |
| 130 | const char *nodename; |
| 131 | const struct cred *curr_cred; |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 132 | mode_t mode = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 133 | struct nameidata nd; |
| 134 | struct dentry *dentry; |
| 135 | int err; |
| 136 | |
| 137 | if (!dev_mnt) |
| 138 | return 0; |
| 139 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 140 | nodename = device_get_devnode(dev, &mode, &tmp); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 141 | if (!nodename) |
| 142 | return -ENOMEM; |
| 143 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 144 | if (mode == 0) |
| 145 | mode = 0600; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 146 | if (is_blockdev(dev)) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 147 | mode |= S_IFBLK; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 148 | else |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 149 | mode |= S_IFCHR; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 150 | |
| 151 | curr_cred = override_creds(&init_cred); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 152 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 153 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 154 | nodename, LOOKUP_PARENT, &nd); |
| 155 | if (err == -ENOENT) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 156 | create_path(nodename); |
| 157 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 158 | nodename, LOOKUP_PARENT, &nd); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 159 | } |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 160 | if (err) |
| 161 | goto out; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 162 | |
| 163 | dentry = lookup_create(&nd, 0); |
| 164 | if (!IS_ERR(dentry)) { |
| 165 | err = vfs_mknod(nd.path.dentry->d_inode, |
| 166 | dentry, mode, dev->devt); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 167 | if (!err) { |
| 168 | struct iattr newattrs; |
| 169 | |
| 170 | /* fixup possibly umasked mode */ |
| 171 | newattrs.ia_mode = mode; |
| 172 | newattrs.ia_valid = ATTR_MODE; |
| 173 | mutex_lock(&dentry->d_inode->i_mutex); |
| 174 | notify_change(dentry, &newattrs); |
| 175 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 176 | |
| 177 | /* mark as kernel-created inode */ |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 178 | dentry->d_inode->i_private = &dev_mnt; |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 179 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 180 | dput(dentry); |
| 181 | } else { |
| 182 | err = PTR_ERR(dentry); |
| 183 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 184 | |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 185 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 186 | path_put(&nd.path); |
| 187 | out: |
| 188 | kfree(tmp); |
| 189 | revert_creds(curr_cred); |
| 190 | return err; |
| 191 | } |
| 192 | |
| 193 | static int dev_rmdir(const char *name) |
| 194 | { |
| 195 | struct nameidata nd; |
| 196 | struct dentry *dentry; |
| 197 | int err; |
| 198 | |
| 199 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 200 | name, LOOKUP_PARENT, &nd); |
| 201 | if (err) |
| 202 | return err; |
| 203 | |
| 204 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 205 | dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); |
| 206 | if (!IS_ERR(dentry)) { |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 207 | if (dentry->d_inode) { |
| 208 | if (dentry->d_inode->i_private == &dev_mnt) |
| 209 | err = vfs_rmdir(nd.path.dentry->d_inode, |
| 210 | dentry); |
| 211 | else |
| 212 | err = -EPERM; |
| 213 | } else { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 214 | err = -ENOENT; |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 215 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 216 | dput(dentry); |
| 217 | } else { |
| 218 | err = PTR_ERR(dentry); |
| 219 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 220 | |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 221 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 222 | path_put(&nd.path); |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | static int delete_path(const char *nodepath) |
| 227 | { |
| 228 | const char *path; |
| 229 | int err = 0; |
| 230 | |
| 231 | path = kstrdup(nodepath, GFP_KERNEL); |
| 232 | if (!path) |
| 233 | return -ENOMEM; |
| 234 | |
Thomas Gleixner | f1f76f8 | 2009-12-16 21:31:33 +0000 | [diff] [blame] | 235 | mutex_lock(&dirlock); |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 236 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 237 | char *base; |
| 238 | |
| 239 | base = strrchr(path, '/'); |
| 240 | if (!base) |
| 241 | break; |
| 242 | base[0] = '\0'; |
| 243 | err = dev_rmdir(path); |
| 244 | if (err) |
| 245 | break; |
| 246 | } |
Thomas Gleixner | f1f76f8 | 2009-12-16 21:31:33 +0000 | [diff] [blame] | 247 | mutex_unlock(&dirlock); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 248 | |
| 249 | kfree(path); |
| 250 | return err; |
| 251 | } |
| 252 | |
| 253 | static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat) |
| 254 | { |
| 255 | /* did we create it */ |
| 256 | if (inode->i_private != &dev_mnt) |
| 257 | return 0; |
| 258 | |
| 259 | /* does the dev_t match */ |
| 260 | if (is_blockdev(dev)) { |
| 261 | if (!S_ISBLK(stat->mode)) |
| 262 | return 0; |
| 263 | } else { |
| 264 | if (!S_ISCHR(stat->mode)) |
| 265 | return 0; |
| 266 | } |
| 267 | if (stat->rdev != dev->devt) |
| 268 | return 0; |
| 269 | |
| 270 | /* ours */ |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | int devtmpfs_delete_node(struct device *dev) |
| 275 | { |
| 276 | const char *tmp = NULL; |
| 277 | const char *nodename; |
| 278 | const struct cred *curr_cred; |
| 279 | struct nameidata nd; |
| 280 | struct dentry *dentry; |
| 281 | struct kstat stat; |
| 282 | int deleted = 1; |
| 283 | int err; |
| 284 | |
| 285 | if (!dev_mnt) |
| 286 | return 0; |
| 287 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 288 | nodename = device_get_devnode(dev, NULL, &tmp); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 289 | if (!nodename) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | curr_cred = override_creds(&init_cred); |
| 293 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 294 | nodename, LOOKUP_PARENT, &nd); |
| 295 | if (err) |
| 296 | goto out; |
| 297 | |
| 298 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 299 | dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); |
| 300 | if (!IS_ERR(dentry)) { |
| 301 | if (dentry->d_inode) { |
| 302 | err = vfs_getattr(nd.path.mnt, dentry, &stat); |
| 303 | if (!err && dev_mynode(dev, dentry->d_inode, &stat)) { |
Kay Sievers | 5e31d76 | 2010-01-13 14:16:36 +0100 | [diff] [blame] | 304 | struct iattr newattrs; |
| 305 | /* |
| 306 | * before unlinking this node, reset permissions |
| 307 | * of possible references like hardlinks |
| 308 | */ |
| 309 | newattrs.ia_uid = 0; |
| 310 | newattrs.ia_gid = 0; |
| 311 | newattrs.ia_mode = stat.mode & ~0777; |
| 312 | newattrs.ia_valid = |
| 313 | ATTR_UID|ATTR_GID|ATTR_MODE; |
| 314 | mutex_lock(&dentry->d_inode->i_mutex); |
| 315 | notify_change(dentry, &newattrs); |
| 316 | mutex_unlock(&dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 317 | err = vfs_unlink(nd.path.dentry->d_inode, |
| 318 | dentry); |
| 319 | if (!err || err == -ENOENT) |
| 320 | deleted = 1; |
| 321 | } |
| 322 | } else { |
| 323 | err = -ENOENT; |
| 324 | } |
| 325 | dput(dentry); |
| 326 | } else { |
| 327 | err = PTR_ERR(dentry); |
| 328 | } |
| 329 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 330 | |
| 331 | path_put(&nd.path); |
| 332 | if (deleted && strchr(nodename, '/')) |
| 333 | delete_path(nodename); |
| 334 | out: |
| 335 | kfree(tmp); |
| 336 | revert_creds(curr_cred); |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * If configured, or requested by the commandline, devtmpfs will be |
| 342 | * auto-mounted after the kernel mounted the root filesystem. |
| 343 | */ |
Kay Sievers | 073120c | 2009-10-28 19:51:17 +0100 | [diff] [blame] | 344 | int devtmpfs_mount(const char *mntdir) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 345 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 346 | int err; |
| 347 | |
| 348 | if (!dev_mount) |
| 349 | return 0; |
| 350 | |
| 351 | if (!dev_mnt) |
| 352 | return 0; |
| 353 | |
Kay Sievers | 073120c | 2009-10-28 19:51:17 +0100 | [diff] [blame] | 354 | err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 355 | if (err) |
| 356 | printk(KERN_INFO "devtmpfs: error mounting %i\n", err); |
| 357 | else |
| 358 | printk(KERN_INFO "devtmpfs: mounted\n"); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 359 | return err; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Create devtmpfs instance, driver-core devices will add their device |
| 364 | * nodes here. |
| 365 | */ |
| 366 | int __init devtmpfs_init(void) |
| 367 | { |
| 368 | int err; |
| 369 | struct vfsmount *mnt; |
Heiko Carstens | f776c5e | 2010-01-18 14:36:12 +0100 | [diff] [blame] | 370 | char options[] = "mode=0755"; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 371 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 372 | err = register_filesystem(&dev_fs_type); |
| 373 | if (err) { |
| 374 | printk(KERN_ERR "devtmpfs: unable to register devtmpfs " |
| 375 | "type %i\n", err); |
| 376 | return err; |
| 377 | } |
| 378 | |
Heiko Carstens | f776c5e | 2010-01-18 14:36:12 +0100 | [diff] [blame] | 379 | mnt = kern_mount_data(&dev_fs_type, options); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 380 | if (IS_ERR(mnt)) { |
| 381 | err = PTR_ERR(mnt); |
| 382 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); |
| 383 | unregister_filesystem(&dev_fs_type); |
| 384 | return err; |
| 385 | } |
| 386 | dev_mnt = mnt; |
| 387 | |
| 388 | printk(KERN_INFO "devtmpfs: initialized\n"); |
| 389 | return 0; |
| 390 | } |