2 * devtmpfs - kernel-maintained tmpfs-based /dev
4 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
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
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.
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>
22 #include <linux/shmem_fs.h>
23 #include <linux/cred.h>
24 #include <linux/sched.h>
25 #include <linux/init_task.h>
27 static struct vfsmount *dev_mnt;
29 #if defined CONFIG_DEVTMPFS_MOUNT
30 static int dev_mount = 1;
35 static int __init mount_param(char *str)
37 dev_mount = simple_strtoul(str, NULL, 0);
40 __setup("devtmpfs.mount=", mount_param);
42 static int dev_get_sb(struct file_system_type *fs_type, int flags,
43 const char *dev_name, void *data, struct vfsmount *mnt)
45 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
48 static struct file_system_type dev_fs_type = {
51 .kill_sb = kill_litter_super,
55 static inline int is_blockdev(struct device *dev)
57 return dev->class == &block_class;
60 static inline int is_blockdev(struct device *dev) { return 0; }
63 static int dev_mkdir(const char *name, mode_t mode)
66 struct dentry *dentry;
69 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
70 name, LOOKUP_PARENT, &nd);
74 dentry = lookup_create(&nd, 1);
75 if (!IS_ERR(dentry)) {
76 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
79 err = PTR_ERR(dentry);
81 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
87 static int create_path(const char *nodepath)
93 path = kstrdup(nodepath, GFP_KERNEL);
97 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
98 path, LOOKUP_PARENT, &nd);
100 struct dentry *dentry;
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,
109 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
112 } else if (err == -ENOENT) {
115 /* parent directories do not exist, create them */
122 err = dev_mkdir(path, 0755);
123 if (err && err != -EEXIST)
134 int devtmpfs_create_node(struct device *dev)
136 const char *tmp = NULL;
137 const char *nodename;
138 const struct cred *curr_cred;
141 struct dentry *dentry;
147 nodename = device_get_devnode(dev, &mode, &tmp);
153 if (is_blockdev(dev))
158 curr_cred = override_creds(&init_cred);
160 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
161 nodename, LOOKUP_PARENT, &nd);
162 if (err == -ENOENT) {
163 create_path(nodename);
164 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
165 nodename, LOOKUP_PARENT, &nd);
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);
175 struct iattr newattrs;
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);
184 /* mark as kernel-created inode */
185 dentry->d_inode->i_private = &dev_mnt;
189 err = PTR_ERR(dentry);
192 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
196 revert_creds(curr_cred);
200 static int dev_rmdir(const char *name)
203 struct dentry *dentry;
206 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
207 name, LOOKUP_PARENT, &nd);
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)) {
215 err = vfs_rmdir(nd.path.dentry->d_inode, dentry);
220 err = PTR_ERR(dentry);
222 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
228 static int delete_path(const char *nodepath)
233 path = kstrdup(nodepath, GFP_KERNEL);
240 base = strrchr(path, '/');
244 err = dev_rmdir(path);
253 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
255 /* did we create it */
256 if (inode->i_private != &dev_mnt)
259 /* does the dev_t match */
260 if (is_blockdev(dev)) {
261 if (!S_ISBLK(stat->mode))
264 if (!S_ISCHR(stat->mode))
267 if (stat->rdev != dev->devt)
274 int devtmpfs_delete_node(struct device *dev)
276 const char *tmp = NULL;
277 const char *nodename;
278 const struct cred *curr_cred;
280 struct dentry *dentry;
288 nodename = device_get_devnode(dev, NULL, &tmp);
292 curr_cred = override_creds(&init_cred);
293 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
294 nodename, LOOKUP_PARENT, &nd);
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,
306 if (!err || err == -ENOENT)
314 err = PTR_ERR(dentry);
316 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
319 if (deleted && strchr(nodename, '/'))
320 delete_path(nodename);
323 revert_creds(curr_cred);
328 * If configured, or requested by the commandline, devtmpfs will be
329 * auto-mounted after the kernel mounted the root filesystem.
331 int devtmpfs_mount(const char *mountpoint)
342 err = kern_path(mountpoint, LOOKUP_FOLLOW, &path);
345 err = do_add_mount(dev_mnt, &path, 0, NULL);
347 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
349 printk(KERN_INFO "devtmpfs: mounted\n");
355 * Create devtmpfs instance, driver-core devices will add their device
358 int __init devtmpfs_init(void)
361 struct vfsmount *mnt;
363 err = register_filesystem(&dev_fs_type);
365 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
370 mnt = kern_mount(&dev_fs_type);
373 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
374 unregister_filesystem(&dev_fs_type);
379 printk(KERN_INFO "devtmpfs: initialized\n");