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 rwlock_t dirlock;
37 static int __init mount_param(char *str)
39 dev_mount = simple_strtoul(str, NULL, 0);
42 __setup("devtmpfs.mount=", mount_param);
44 static int dev_get_sb(struct file_system_type *fs_type, int flags,
45 const char *dev_name, void *data, struct vfsmount *mnt)
47 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
50 static struct file_system_type dev_fs_type = {
53 .kill_sb = kill_litter_super,
57 static inline int is_blockdev(struct device *dev)
59 return dev->class == &block_class;
62 static inline int is_blockdev(struct device *dev) { return 0; }
65 static int dev_mkdir(const char *name, mode_t mode)
68 struct dentry *dentry;
71 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
72 name, LOOKUP_PARENT, &nd);
76 dentry = lookup_create(&nd, 1);
77 if (!IS_ERR(dentry)) {
78 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
80 /* mark as kernel-created inode */
81 dentry->d_inode->i_private = &dev_mnt;
84 err = PTR_ERR(dentry);
87 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
92 static int create_path(const char *nodepath)
97 err = dev_mkdir(nodepath, 0755);
102 /* parent directories do not exist, create them */
103 path = kstrdup(nodepath, GFP_KERNEL);
112 err = dev_mkdir(path, 0755);
113 if (err && err != -EEXIST)
120 read_unlock(&dirlock);
124 int devtmpfs_create_node(struct device *dev)
126 const char *tmp = NULL;
127 const char *nodename;
128 const struct cred *curr_cred;
131 struct dentry *dentry;
137 nodename = device_get_devnode(dev, &mode, &tmp);
143 if (is_blockdev(dev))
148 curr_cred = override_creds(&init_cred);
150 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
151 nodename, LOOKUP_PARENT, &nd);
152 if (err == -ENOENT) {
153 create_path(nodename);
154 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
155 nodename, LOOKUP_PARENT, &nd);
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);
165 struct iattr newattrs;
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);
174 /* mark as kernel-created inode */
175 dentry->d_inode->i_private = &dev_mnt;
179 err = PTR_ERR(dentry);
182 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
186 revert_creds(curr_cred);
190 static int dev_rmdir(const char *name)
193 struct dentry *dentry;
196 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
197 name, LOOKUP_PARENT, &nd);
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)) {
204 if (dentry->d_inode) {
205 if (dentry->d_inode->i_private == &dev_mnt)
206 err = vfs_rmdir(nd.path.dentry->d_inode,
215 err = PTR_ERR(dentry);
218 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
223 static int delete_path(const char *nodepath)
228 path = kstrdup(nodepath, GFP_KERNEL);
232 write_lock(&dirlock);
236 base = strrchr(path, '/');
240 err = dev_rmdir(path);
244 write_unlock(&dirlock);
250 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
252 /* did we create it */
253 if (inode->i_private != &dev_mnt)
256 /* does the dev_t match */
257 if (is_blockdev(dev)) {
258 if (!S_ISBLK(stat->mode))
261 if (!S_ISCHR(stat->mode))
264 if (stat->rdev != dev->devt)
271 int devtmpfs_delete_node(struct device *dev)
273 const char *tmp = NULL;
274 const char *nodename;
275 const struct cred *curr_cred;
277 struct dentry *dentry;
285 nodename = device_get_devnode(dev, NULL, &tmp);
289 curr_cred = override_creds(&init_cred);
290 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
291 nodename, LOOKUP_PARENT, &nd);
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,
303 if (!err || err == -ENOENT)
311 err = PTR_ERR(dentry);
313 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
316 if (deleted && strchr(nodename, '/'))
317 delete_path(nodename);
320 revert_creds(curr_cred);
325 * If configured, or requested by the commandline, devtmpfs will be
326 * auto-mounted after the kernel mounted the root filesystem.
328 int devtmpfs_mount(const char *mntdir)
338 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
340 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
342 printk(KERN_INFO "devtmpfs: mounted\n");
347 * Create devtmpfs instance, driver-core devices will add their device
350 int __init devtmpfs_init(void)
353 struct vfsmount *mnt;
355 rwlock_init(&dirlock);
357 err = register_filesystem(&dev_fs_type);
359 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
364 mnt = kern_mount_data(&dev_fs_type, "mode=0755");
367 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
368 unregister_filesystem(&dev_fs_type);
373 printk(KERN_INFO "devtmpfs: initialized\n");