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/ramfs.h>
24 #include <linux/cred.h>
25 #include <linux/sched.h>
26 #include <linux/init_task.h>
27 #include <linux/slab.h>
29 static struct vfsmount *dev_mnt;
31 #if defined CONFIG_DEVTMPFS_MOUNT
32 static int mount_dev = 1;
37 static DEFINE_MUTEX(dirlock);
39 static int __init mount_param(char *str)
41 mount_dev = simple_strtoul(str, NULL, 0);
44 __setup("devtmpfs.mount=", mount_param);
46 static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
47 const char *dev_name, void *data)
50 return mount_single(fs_type, flags, data, shmem_fill_super);
52 return mount_single(fs_type, flags, data, ramfs_fill_super);
56 static struct file_system_type dev_fs_type = {
59 .kill_sb = kill_litter_super,
63 static inline int is_blockdev(struct device *dev)
65 return dev->class == &block_class;
68 static inline int is_blockdev(struct device *dev) { return 0; }
71 static int dev_mkdir(const char *name, mode_t mode)
74 struct dentry *dentry;
77 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
78 name, LOOKUP_PARENT, &nd);
82 dentry = lookup_create(&nd, 1);
83 if (!IS_ERR(dentry)) {
84 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
86 /* mark as kernel-created inode */
87 dentry->d_inode->i_private = &dev_mnt;
90 err = PTR_ERR(dentry);
93 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
98 static int create_path(const char *nodepath)
102 mutex_lock(&dirlock);
103 err = dev_mkdir(nodepath, 0755);
104 if (err == -ENOENT) {
108 /* parent directories do not exist, create them */
109 path = kstrdup(nodepath, GFP_KERNEL);
120 err = dev_mkdir(path, 0755);
121 if (err && err != -EEXIST)
129 mutex_unlock(&dirlock);
133 int devtmpfs_create_node(struct device *dev)
135 const char *tmp = NULL;
136 const char *nodename;
137 const struct cred *curr_cred;
140 struct dentry *dentry;
146 nodename = device_get_devnode(dev, &mode, &tmp);
152 if (is_blockdev(dev))
157 curr_cred = override_creds(&init_cred);
159 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
160 nodename, LOOKUP_PARENT, &nd);
161 if (err == -ENOENT) {
162 create_path(nodename);
163 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
164 nodename, LOOKUP_PARENT, &nd);
169 dentry = lookup_create(&nd, 0);
170 if (!IS_ERR(dentry)) {
171 err = vfs_mknod(nd.path.dentry->d_inode,
172 dentry, mode, dev->devt);
174 struct iattr newattrs;
176 /* fixup possibly umasked mode */
177 newattrs.ia_mode = mode;
178 newattrs.ia_valid = ATTR_MODE;
179 mutex_lock(&dentry->d_inode->i_mutex);
180 notify_change(dentry, &newattrs);
181 mutex_unlock(&dentry->d_inode->i_mutex);
183 /* mark as kernel-created inode */
184 dentry->d_inode->i_private = &dev_mnt;
188 err = PTR_ERR(dentry);
191 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
195 revert_creds(curr_cred);
199 static int dev_rmdir(const char *name)
202 struct dentry *dentry;
205 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
206 name, LOOKUP_PARENT, &nd);
210 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
211 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
212 if (!IS_ERR(dentry)) {
213 if (dentry->d_inode) {
214 if (dentry->d_inode->i_private == &dev_mnt)
215 err = vfs_rmdir(nd.path.dentry->d_inode,
224 err = PTR_ERR(dentry);
227 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
232 static int delete_path(const char *nodepath)
237 path = kstrdup(nodepath, GFP_KERNEL);
241 mutex_lock(&dirlock);
245 base = strrchr(path, '/');
249 err = dev_rmdir(path);
253 mutex_unlock(&dirlock);
259 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
261 /* did we create it */
262 if (inode->i_private != &dev_mnt)
265 /* does the dev_t match */
266 if (is_blockdev(dev)) {
267 if (!S_ISBLK(stat->mode))
270 if (!S_ISCHR(stat->mode))
273 if (stat->rdev != dev->devt)
280 int devtmpfs_delete_node(struct device *dev)
282 const char *tmp = NULL;
283 const char *nodename;
284 const struct cred *curr_cred;
286 struct dentry *dentry;
294 nodename = device_get_devnode(dev, NULL, &tmp);
298 curr_cred = override_creds(&init_cred);
299 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
300 nodename, LOOKUP_PARENT, &nd);
304 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
305 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
306 if (!IS_ERR(dentry)) {
307 if (dentry->d_inode) {
308 err = vfs_getattr(nd.path.mnt, dentry, &stat);
309 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
310 struct iattr newattrs;
312 * before unlinking this node, reset permissions
313 * of possible references like hardlinks
317 newattrs.ia_mode = stat.mode & ~0777;
319 ATTR_UID|ATTR_GID|ATTR_MODE;
320 mutex_lock(&dentry->d_inode->i_mutex);
321 notify_change(dentry, &newattrs);
322 mutex_unlock(&dentry->d_inode->i_mutex);
323 err = vfs_unlink(nd.path.dentry->d_inode,
325 if (!err || err == -ENOENT)
333 err = PTR_ERR(dentry);
335 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
338 if (deleted && strchr(nodename, '/'))
339 delete_path(nodename);
342 revert_creds(curr_cred);
347 * If configured, or requested by the commandline, devtmpfs will be
348 * auto-mounted after the kernel mounted the root filesystem.
350 int devtmpfs_mount(const char *mntdir)
360 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
362 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
364 printk(KERN_INFO "devtmpfs: mounted\n");
369 * Create devtmpfs instance, driver-core devices will add their device
372 int __init devtmpfs_init(void)
375 struct vfsmount *mnt;
376 char options[] = "mode=0755";
378 err = register_filesystem(&dev_fs_type);
380 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
385 mnt = kern_mount_data(&dev_fs_type, options);
388 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
389 unregister_filesystem(&dev_fs_type);
394 printk(KERN_INFO "devtmpfs: initialized\n");