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);
81 err = PTR_ERR(dentry);
83 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
89 static int create_path(const char *nodepath)
95 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
96 nodepath, LOOKUP_PARENT, &nd);
98 struct dentry *dentry;
100 /* create directory right away */
101 dentry = lookup_create(&nd, 1);
102 if (!IS_ERR(dentry)) {
103 err = vfs_mkdir(nd.path.dentry->d_inode,
107 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
109 } else if (err == -ENOENT) {
113 /* parent directories do not exist, create them */
114 path = kstrdup(nodepath, GFP_KERNEL);
123 err = dev_mkdir(path, 0755);
124 if (err && err != -EEXIST)
131 read_unlock(&dirlock);
136 int devtmpfs_create_node(struct device *dev)
138 const char *tmp = NULL;
139 const char *nodename;
140 const struct cred *curr_cred;
143 struct dentry *dentry;
149 nodename = device_get_devnode(dev, &mode, &tmp);
155 if (is_blockdev(dev))
160 curr_cred = override_creds(&init_cred);
162 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
163 nodename, LOOKUP_PARENT, &nd);
164 if (err == -ENOENT) {
165 create_path(nodename);
166 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
167 nodename, LOOKUP_PARENT, &nd);
172 dentry = lookup_create(&nd, 0);
173 if (!IS_ERR(dentry)) {
174 err = vfs_mknod(nd.path.dentry->d_inode,
175 dentry, mode, dev->devt);
177 struct iattr newattrs;
179 /* fixup possibly umasked mode */
180 newattrs.ia_mode = mode;
181 newattrs.ia_valid = ATTR_MODE;
182 mutex_lock(&dentry->d_inode->i_mutex);
183 notify_change(dentry, &newattrs);
184 mutex_unlock(&dentry->d_inode->i_mutex);
186 /* mark as kernel-created inode */
187 dentry->d_inode->i_private = &dev_mnt;
191 err = PTR_ERR(dentry);
194 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
198 revert_creds(curr_cred);
202 static int dev_rmdir(const char *name)
205 struct dentry *dentry;
208 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
209 name, LOOKUP_PARENT, &nd);
213 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
214 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
215 if (!IS_ERR(dentry)) {
217 err = vfs_rmdir(nd.path.dentry->d_inode, dentry);
222 err = PTR_ERR(dentry);
224 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
230 static int delete_path(const char *nodepath)
235 path = kstrdup(nodepath, GFP_KERNEL);
239 write_lock(&dirlock);
243 base = strrchr(path, '/');
247 err = dev_rmdir(path);
251 write_unlock(&dirlock);
257 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
259 /* did we create it */
260 if (inode->i_private != &dev_mnt)
263 /* does the dev_t match */
264 if (is_blockdev(dev)) {
265 if (!S_ISBLK(stat->mode))
268 if (!S_ISCHR(stat->mode))
271 if (stat->rdev != dev->devt)
278 int devtmpfs_delete_node(struct device *dev)
280 const char *tmp = NULL;
281 const char *nodename;
282 const struct cred *curr_cred;
284 struct dentry *dentry;
292 nodename = device_get_devnode(dev, NULL, &tmp);
296 curr_cred = override_creds(&init_cred);
297 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
298 nodename, LOOKUP_PARENT, &nd);
302 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
303 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
304 if (!IS_ERR(dentry)) {
305 if (dentry->d_inode) {
306 err = vfs_getattr(nd.path.mnt, dentry, &stat);
307 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
308 err = vfs_unlink(nd.path.dentry->d_inode,
310 if (!err || err == -ENOENT)
318 err = PTR_ERR(dentry);
320 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
323 if (deleted && strchr(nodename, '/'))
324 delete_path(nodename);
327 revert_creds(curr_cred);
332 * If configured, or requested by the commandline, devtmpfs will be
333 * auto-mounted after the kernel mounted the root filesystem.
335 int devtmpfs_mount(const char *mntdir)
345 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
347 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
349 printk(KERN_INFO "devtmpfs: mounted\n");
354 * Create devtmpfs instance, driver-core devices will add their device
357 int __init devtmpfs_init(void)
360 struct vfsmount *mnt;
362 rwlock_init(&dirlock);
364 err = register_filesystem(&dev_fs_type);
366 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
371 mnt = kern_mount(&dev_fs_type);
374 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
375 unregister_filesystem(&dev_fs_type);
380 printk(KERN_INFO "devtmpfs: initialized\n");