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 DEFINE_MUTEX(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
);
114 err
= dev_mkdir(path
, 0755);
115 if (err
&& err
!= -EEXIST
)
123 mutex_unlock(&dirlock
);
127 int devtmpfs_create_node(struct device
*dev
)
129 const char *tmp
= NULL
;
130 const char *nodename
;
131 const struct cred
*curr_cred
;
134 struct dentry
*dentry
;
140 nodename
= device_get_devnode(dev
, &mode
, &tmp
);
146 if (is_blockdev(dev
))
151 curr_cred
= override_creds(&init_cred
);
153 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
154 nodename
, LOOKUP_PARENT
, &nd
);
155 if (err
== -ENOENT
) {
156 create_path(nodename
);
157 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
158 nodename
, LOOKUP_PARENT
, &nd
);
163 dentry
= lookup_create(&nd
, 0);
164 if (!IS_ERR(dentry
)) {
165 err
= vfs_mknod(nd
.path
.dentry
->d_inode
,
166 dentry
, mode
, dev
->devt
);
168 struct iattr newattrs
;
170 /* fixup possibly umasked mode */
171 newattrs
.ia_mode
= mode
;
172 newattrs
.ia_valid
= ATTR_MODE
;
173 mutex_lock(&dentry
->d_inode
->i_mutex
);
174 notify_change(dentry
, &newattrs
);
175 mutex_unlock(&dentry
->d_inode
->i_mutex
);
177 /* mark as kernel-created inode */
178 dentry
->d_inode
->i_private
= &dev_mnt
;
182 err
= PTR_ERR(dentry
);
185 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
189 revert_creds(curr_cred
);
193 static int dev_rmdir(const char *name
)
196 struct dentry
*dentry
;
199 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
200 name
, LOOKUP_PARENT
, &nd
);
204 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
205 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
206 if (!IS_ERR(dentry
)) {
207 if (dentry
->d_inode
) {
208 if (dentry
->d_inode
->i_private
== &dev_mnt
)
209 err
= vfs_rmdir(nd
.path
.dentry
->d_inode
,
218 err
= PTR_ERR(dentry
);
221 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
226 static int delete_path(const char *nodepath
)
231 path
= kstrdup(nodepath
, GFP_KERNEL
);
235 mutex_lock(&dirlock
);
239 base
= strrchr(path
, '/');
243 err
= dev_rmdir(path
);
247 mutex_unlock(&dirlock
);
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 struct iattr newattrs
;
306 * before unlinking this node, reset permissions
307 * of possible references like hardlinks
311 newattrs
.ia_mode
= stat
.mode
& ~0777;
313 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
314 mutex_lock(&dentry
->d_inode
->i_mutex
);
315 notify_change(dentry
, &newattrs
);
316 mutex_unlock(&dentry
->d_inode
->i_mutex
);
317 err
= vfs_unlink(nd
.path
.dentry
->d_inode
,
319 if (!err
|| err
== -ENOENT
)
327 err
= PTR_ERR(dentry
);
329 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
332 if (deleted
&& strchr(nodename
, '/'))
333 delete_path(nodename
);
336 revert_creds(curr_cred
);
341 * If configured, or requested by the commandline, devtmpfs will be
342 * auto-mounted after the kernel mounted the root filesystem.
344 int devtmpfs_mount(const char *mntdir
)
354 err
= sys_mount("devtmpfs", (char *)mntdir
, "devtmpfs", MS_SILENT
, NULL
);
356 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
358 printk(KERN_INFO
"devtmpfs: mounted\n");
363 * Create devtmpfs instance, driver-core devices will add their device
366 int __init
devtmpfs_init(void)
369 struct vfsmount
*mnt
;
370 char options
[] = "mode=0755";
372 err
= register_filesystem(&dev_fs_type
);
374 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
379 mnt
= kern_mount_data(&dev_fs_type
, options
);
382 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
383 unregister_filesystem(&dev_fs_type
);
388 printk(KERN_INFO
"devtmpfs: initialized\n");