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