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 | |
| 35 | static int __init mount_param(char *str) |
| 36 | { |
| 37 | dev_mount = simple_strtoul(str, NULL, 0); |
| 38 | return 1; |
| 39 | } |
| 40 | __setup("devtmpfs.mount=", mount_param); |
| 41 | |
| 42 | static int dev_get_sb(struct file_system_type *fs_type, int flags, |
| 43 | const char *dev_name, void *data, struct vfsmount *mnt) |
| 44 | { |
| 45 | return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt); |
| 46 | } |
| 47 | |
| 48 | static struct file_system_type dev_fs_type = { |
| 49 | .name = "devtmpfs", |
| 50 | .get_sb = dev_get_sb, |
| 51 | .kill_sb = kill_litter_super, |
| 52 | }; |
| 53 | |
| 54 | #ifdef CONFIG_BLOCK |
| 55 | static inline int is_blockdev(struct device *dev) |
| 56 | { |
| 57 | return dev->class == &block_class; |
| 58 | } |
| 59 | #else |
| 60 | static inline int is_blockdev(struct device *dev) { return 0; } |
| 61 | #endif |
| 62 | |
| 63 | static int dev_mkdir(const char *name, mode_t mode) |
| 64 | { |
| 65 | struct nameidata nd; |
| 66 | struct dentry *dentry; |
| 67 | int err; |
| 68 | |
| 69 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 70 | name, LOOKUP_PARENT, &nd); |
| 71 | if (err) |
| 72 | return err; |
| 73 | |
| 74 | dentry = lookup_create(&nd, 1); |
| 75 | if (!IS_ERR(dentry)) { |
| 76 | err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); |
| 77 | dput(dentry); |
| 78 | } else { |
| 79 | err = PTR_ERR(dentry); |
| 80 | } |
| 81 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 82 | |
| 83 | path_put(&nd.path); |
| 84 | return err; |
| 85 | } |
| 86 | |
| 87 | static int create_path(const char *nodepath) |
| 88 | { |
| 89 | char *path; |
| 90 | struct nameidata nd; |
| 91 | int err = 0; |
| 92 | |
| 93 | path = kstrdup(nodepath, GFP_KERNEL); |
| 94 | if (!path) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 98 | path, LOOKUP_PARENT, &nd); |
| 99 | if (err == 0) { |
| 100 | struct dentry *dentry; |
| 101 | |
| 102 | /* create directory right away */ |
| 103 | dentry = lookup_create(&nd, 1); |
| 104 | if (!IS_ERR(dentry)) { |
| 105 | err = vfs_mkdir(nd.path.dentry->d_inode, |
| 106 | dentry, 0755); |
| 107 | dput(dentry); |
| 108 | } |
| 109 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 110 | |
| 111 | path_put(&nd.path); |
| 112 | } else if (err == -ENOENT) { |
| 113 | char *s; |
| 114 | |
| 115 | /* parent directories do not exist, create them */ |
| 116 | s = path; |
| 117 | while (1) { |
| 118 | s = strchr(s, '/'); |
| 119 | if (!s) |
| 120 | break; |
| 121 | s[0] = '\0'; |
| 122 | err = dev_mkdir(path, 0755); |
| 123 | if (err && err != -EEXIST) |
| 124 | break; |
| 125 | s[0] = '/'; |
| 126 | s++; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | kfree(path); |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | int devtmpfs_create_node(struct device *dev) |
| 135 | { |
| 136 | const char *tmp = NULL; |
| 137 | const char *nodename; |
| 138 | const struct cred *curr_cred; |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 139 | mode_t mode = 0; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 140 | struct nameidata nd; |
| 141 | struct dentry *dentry; |
| 142 | int err; |
| 143 | |
| 144 | if (!dev_mnt) |
| 145 | return 0; |
| 146 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 147 | nodename = device_get_devnode(dev, &mode, &tmp); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 148 | if (!nodename) |
| 149 | return -ENOMEM; |
| 150 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 151 | if (mode == 0) |
| 152 | mode = 0600; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 153 | if (is_blockdev(dev)) |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 154 | mode |= S_IFBLK; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 155 | else |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 156 | mode |= S_IFCHR; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 157 | |
| 158 | curr_cred = override_creds(&init_cred); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame^] | 159 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 160 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 161 | nodename, LOOKUP_PARENT, &nd); |
| 162 | if (err == -ENOENT) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 163 | create_path(nodename); |
| 164 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 165 | nodename, LOOKUP_PARENT, &nd); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 166 | } |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame^] | 167 | if (err) |
| 168 | goto out; |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 169 | |
| 170 | dentry = lookup_create(&nd, 0); |
| 171 | if (!IS_ERR(dentry)) { |
| 172 | err = vfs_mknod(nd.path.dentry->d_inode, |
| 173 | dentry, mode, dev->devt); |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame^] | 174 | if (!err) { |
| 175 | struct iattr newattrs; |
| 176 | |
| 177 | /* fixup possibly umasked mode */ |
| 178 | newattrs.ia_mode = mode; |
| 179 | newattrs.ia_valid = ATTR_MODE; |
| 180 | mutex_lock(&dentry->d_inode->i_mutex); |
| 181 | notify_change(dentry, &newattrs); |
| 182 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 183 | |
| 184 | /* mark as kernel-created inode */ |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 185 | dentry->d_inode->i_private = &dev_mnt; |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame^] | 186 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 187 | dput(dentry); |
| 188 | } else { |
| 189 | err = PTR_ERR(dentry); |
| 190 | } |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 191 | |
Kay Sievers | 0092699 | 2009-10-28 19:50:57 +0100 | [diff] [blame^] | 192 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 193 | path_put(&nd.path); |
| 194 | out: |
| 195 | kfree(tmp); |
| 196 | revert_creds(curr_cred); |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | static int dev_rmdir(const char *name) |
| 201 | { |
| 202 | struct nameidata nd; |
| 203 | struct dentry *dentry; |
| 204 | int err; |
| 205 | |
| 206 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
| 207 | name, LOOKUP_PARENT, &nd); |
| 208 | if (err) |
| 209 | return err; |
| 210 | |
| 211 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 212 | dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); |
| 213 | if (!IS_ERR(dentry)) { |
| 214 | if (dentry->d_inode) |
| 215 | err = vfs_rmdir(nd.path.dentry->d_inode, dentry); |
| 216 | else |
| 217 | err = -ENOENT; |
| 218 | dput(dentry); |
| 219 | } else { |
| 220 | err = PTR_ERR(dentry); |
| 221 | } |
| 222 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 223 | |
| 224 | path_put(&nd.path); |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | static int delete_path(const char *nodepath) |
| 229 | { |
| 230 | const char *path; |
| 231 | int err = 0; |
| 232 | |
| 233 | path = kstrdup(nodepath, GFP_KERNEL); |
| 234 | if (!path) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | while (1) { |
| 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 | } |
| 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)) { |
| 304 | err = vfs_unlink(nd.path.dentry->d_inode, |
| 305 | dentry); |
| 306 | if (!err || err == -ENOENT) |
| 307 | deleted = 1; |
| 308 | } |
| 309 | } else { |
| 310 | err = -ENOENT; |
| 311 | } |
| 312 | dput(dentry); |
| 313 | } else { |
| 314 | err = PTR_ERR(dentry); |
| 315 | } |
| 316 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 317 | |
| 318 | path_put(&nd.path); |
| 319 | if (deleted && strchr(nodename, '/')) |
| 320 | delete_path(nodename); |
| 321 | out: |
| 322 | kfree(tmp); |
| 323 | revert_creds(curr_cred); |
| 324 | return err; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * If configured, or requested by the commandline, devtmpfs will be |
| 329 | * auto-mounted after the kernel mounted the root filesystem. |
| 330 | */ |
| 331 | int devtmpfs_mount(const char *mountpoint) |
| 332 | { |
| 333 | struct path path; |
| 334 | int err; |
| 335 | |
| 336 | if (!dev_mount) |
| 337 | return 0; |
| 338 | |
| 339 | if (!dev_mnt) |
| 340 | return 0; |
| 341 | |
| 342 | err = kern_path(mountpoint, LOOKUP_FOLLOW, &path); |
| 343 | if (err) |
| 344 | return err; |
| 345 | err = do_add_mount(dev_mnt, &path, 0, NULL); |
| 346 | if (err) |
| 347 | printk(KERN_INFO "devtmpfs: error mounting %i\n", err); |
| 348 | else |
| 349 | printk(KERN_INFO "devtmpfs: mounted\n"); |
| 350 | path_put(&path); |
| 351 | return err; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Create devtmpfs instance, driver-core devices will add their device |
| 356 | * nodes here. |
| 357 | */ |
| 358 | int __init devtmpfs_init(void) |
| 359 | { |
| 360 | int err; |
| 361 | struct vfsmount *mnt; |
| 362 | |
| 363 | err = register_filesystem(&dev_fs_type); |
| 364 | if (err) { |
| 365 | printk(KERN_ERR "devtmpfs: unable to register devtmpfs " |
| 366 | "type %i\n", err); |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | mnt = kern_mount(&dev_fs_type); |
| 371 | if (IS_ERR(mnt)) { |
| 372 | err = PTR_ERR(mnt); |
| 373 | printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); |
| 374 | unregister_filesystem(&dev_fs_type); |
| 375 | return err; |
| 376 | } |
| 377 | dev_mnt = mnt; |
| 378 | |
| 379 | printk(KERN_INFO "devtmpfs: initialized\n"); |
| 380 | return 0; |
| 381 | } |