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 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 35 | static rwlock_t dirlock; |
| 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 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 96 | read_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); |
| 104 | if (!path) |
| 105 | return -ENOMEM; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 106 | s = path; |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 107 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 108 | s = strchr(s, '/'); |
| 109 | if (!s) |
| 110 | break; |
| 111 | s[0] = '\0'; |
| 112 | err = dev_mkdir(path, 0755); |
| 113 | if (err && err != -EEXIST) |
| 114 | break; |
| 115 | s[0] = '/'; |
| 116 | s++; |
| 117 | } |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 118 | kfree(path); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 119 | } |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 120 | read_unlock(&dirlock); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 121 | return err; |
| 122 | } |
| 123 | |
| 124 | int devtmpfs_create_node(struct device *dev) |
| 125 | { |
| 126 | const char *tmp = NULL; |
| 127 | const char *nodename; |
| 128 | const struct cred *curr_cred; |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 129 | mode_t mode = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 130 | struct nameidata nd; |
| 131 | struct dentry *dentry; |
| 132 | int err; |
| 133 | |
| 134 | if (!dev_mnt) |
| 135 | return 0; |
| 136 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 137 | nodename = device_get_devnode(dev, &mode, &tmp); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 138 | if (!nodename) |
| 139 | return -ENOMEM; |
| 140 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 141 | if (mode == 0) |
| 142 | mode = 0600; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 143 | if (is_blockdev(dev)) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 144 | mode |= S_IFBLK; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 145 | else |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 146 | mode |= S_IFCHR; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 147 | |
| 148 | curr_cred = override_creds(&init_cred); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 149 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 150 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 151 | nodename, LOOKUP_PARENT, &nd); |
| 152 | if (err == -ENOENT) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 153 | create_path(nodename); |
| 154 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 155 | nodename, LOOKUP_PARENT, &nd); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 156 | } |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 157 | if (err) |
| 158 | goto out; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 159 | |
| 160 | dentry = lookup_create(&nd, 0); |
| 161 | if (!IS_ERR(dentry)) { |
| 162 | err = vfs_mknod(nd.path.dentry->d_inode, |
| 163 | dentry, mode, dev->devt); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 164 | if (!err) { |
| 165 | struct iattr newattrs; |
| 166 | |
| 167 | /* fixup possibly umasked mode */ |
| 168 | newattrs.ia_mode = mode; |
| 169 | newattrs.ia_valid = ATTR_MODE; |
| 170 | mutex_lock(&dentry->d_inode->i_mutex); |
| 171 | notify_change(dentry, &newattrs); |
| 172 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 173 | |
| 174 | /* mark as kernel-created inode */ |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 175 | dentry->d_inode->i_private = &dev_mnt; |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 176 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 177 | dput(dentry); |
| 178 | } else { |
| 179 | err = PTR_ERR(dentry); |
| 180 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 181 | |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame] | 182 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 183 | path_put(&nd.path); |
| 184 | out: |
| 185 | kfree(tmp); |
| 186 | revert_creds(curr_cred); |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | static int dev_rmdir(const char *name) |
| 191 | { |
| 192 | struct nameidata nd; |
| 193 | struct dentry *dentry; |
| 194 | int err; |
| 195 | |
| 196 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 197 | name, LOOKUP_PARENT, &nd); |
| 198 | if (err) |
| 199 | return err; |
| 200 | |
| 201 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 202 | dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); |
| 203 | if (!IS_ERR(dentry)) { |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 204 | if (dentry->d_inode) { |
| 205 | if (dentry->d_inode->i_private == &dev_mnt) |
| 206 | err = vfs_rmdir(nd.path.dentry->d_inode, |
| 207 | dentry); |
| 208 | else |
| 209 | err = -EPERM; |
| 210 | } else { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 211 | err = -ENOENT; |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 212 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 213 | dput(dentry); |
| 214 | } else { |
| 215 | err = PTR_ERR(dentry); |
| 216 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 217 | |
Kay Sievers | 015bf43 | 2009-10-28 19:51:26 +0100 | [diff] [blame] | 218 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 219 | path_put(&nd.path); |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | static int delete_path(const char *nodepath) |
| 224 | { |
| 225 | const char *path; |
| 226 | int err = 0; |
| 227 | |
| 228 | path = kstrdup(nodepath, GFP_KERNEL); |
| 229 | if (!path) |
| 230 | return -ENOMEM; |
| 231 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 232 | write_lock(&dirlock); |
| 233 | for (;;) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 234 | char *base; |
| 235 | |
| 236 | base = strrchr(path, '/'); |
| 237 | if (!base) |
| 238 | break; |
| 239 | base[0] = '\0'; |
| 240 | err = dev_rmdir(path); |
| 241 | if (err) |
| 242 | break; |
| 243 | } |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 244 | write_unlock(&dirlock); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 245 | |
| 246 | kfree(path); |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat) |
| 251 | { |
| 252 | /* did we create it */ |
| 253 | if (inode->i_private != &dev_mnt) |
| 254 | return 0; |
| 255 | |
| 256 | /* does the dev_t match */ |
| 257 | if (is_blockdev(dev)) { |
| 258 | if (!S_ISBLK(stat->mode)) |
| 259 | return 0; |
| 260 | } else { |
| 261 | if (!S_ISCHR(stat->mode)) |
| 262 | return 0; |
| 263 | } |
| 264 | if (stat->rdev != dev->devt) |
| 265 | return 0; |
| 266 | |
| 267 | /* ours */ |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | int devtmpfs_delete_node(struct device *dev) |
| 272 | { |
| 273 | const char *tmp = NULL; |
| 274 | const char *nodename; |
| 275 | const struct cred *curr_cred; |
| 276 | struct nameidata nd; |
| 277 | struct dentry *dentry; |
| 278 | struct kstat stat; |
| 279 | int deleted = 1; |
| 280 | int err; |
| 281 | |
| 282 | if (!dev_mnt) |
| 283 | return 0; |
| 284 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 285 | nodename = device_get_devnode(dev, NULL, &tmp); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 286 | if (!nodename) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | curr_cred = override_creds(&init_cred); |
| 290 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 291 | nodename, LOOKUP_PARENT, &nd); |
| 292 | if (err) |
| 293 | goto out; |
| 294 | |
| 295 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 296 | dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); |
| 297 | if (!IS_ERR(dentry)) { |
| 298 | if (dentry->d_inode) { |
| 299 | err = vfs_getattr(nd.path.mnt, dentry, &stat); |
| 300 | if (!err && dev_mynode(dev, dentry->d_inode, &stat)) { |
| 301 | err = vfs_unlink(nd.path.dentry->d_inode, |
| 302 | dentry); |
| 303 | if (!err || err == -ENOENT) |
| 304 | deleted = 1; |
| 305 | } |
| 306 | } else { |
| 307 | err = -ENOENT; |
| 308 | } |
| 309 | dput(dentry); |
| 310 | } else { |
| 311 | err = PTR_ERR(dentry); |
| 312 | } |
| 313 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 314 | |
| 315 | path_put(&nd.path); |
| 316 | if (deleted && strchr(nodename, '/')) |
| 317 | delete_path(nodename); |
| 318 | out: |
| 319 | kfree(tmp); |
| 320 | revert_creds(curr_cred); |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * If configured, or requested by the commandline, devtmpfs will be |
| 326 | * auto-mounted after the kernel mounted the root filesystem. |
| 327 | */ |
Kay Sievers | 073120c | 2009-10-28 19:51:17 +0100 | [diff] [blame] | 328 | int devtmpfs_mount(const char *mntdir) |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 329 | { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 330 | int err; |
| 331 | |
| 332 | if (!dev_mount) |
| 333 | return 0; |
| 334 | |
| 335 | if (!dev_mnt) |
| 336 | return 0; |
| 337 | |
Kay Sievers | 073120c | 2009-10-28 19:51:17 +0100 | [diff] [blame] | 338 | err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 339 | if (err) |
| 340 | printk(KERN_INFO "devtmpfs: error mounting %i\n", err); |
| 341 | else |
| 342 | printk(KERN_INFO "devtmpfs: mounted\n"); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 343 | return err; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Create devtmpfs instance, driver-core devices will add their device |
| 348 | * nodes here. |
| 349 | */ |
| 350 | int __init devtmpfs_init(void) |
| 351 | { |
| 352 | int err; |
| 353 | struct vfsmount *mnt; |
| 354 | |
Kay Sievers | ed413ae | 2009-10-28 19:51:06 +0100 | [diff] [blame] | 355 | rwlock_init(&dirlock); |
| 356 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 357 | err = register_filesystem(&dev_fs_type); |
| 358 | if (err) { |
| 359 | printk(KERN_ERR "devtmpfs: unable to register devtmpfs " |
| 360 | "type %i\n", err); |
| 361 | return err; |
| 362 | } |
| 363 | |
Kay Sievers | 03d673e | 2009-10-30 12:48:32 +0100 | [diff] [blame] | 364 | mnt = kern_mount_data(&dev_fs_type, "mode=0755"); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 365 | if (IS_ERR(mnt)) { |
| 366 | err = PTR_ERR(mnt); |
| 367 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); |
| 368 | unregister_filesystem(&dev_fs_type); |
| 369 | return err; |
| 370 | } |
| 371 | dev_mnt = mnt; |
| 372 | |
| 373 | printk(KERN_INFO "devtmpfs: initialized\n"); |
| 374 | return 0; |
| 375 | } |