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
);
159 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
160 nodename
, LOOKUP_PARENT
, &nd
);
161 if (err
== -ENOENT
) {
162 /* create missing parent directories */
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
)) {
174 umask
= sys_umask(0000);
175 err
= vfs_mknod(nd
.path
.dentry
->d_inode
,
176 dentry
, mode
, dev
->devt
);
178 /* mark as kernel created inode */
180 dentry
->d_inode
->i_private
= &dev_mnt
;
183 err
= PTR_ERR(dentry
);
185 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
190 revert_creds(curr_cred
);
194 static int dev_rmdir(const char *name
)
197 struct dentry
*dentry
;
200 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
201 name
, LOOKUP_PARENT
, &nd
);
205 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
206 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
207 if (!IS_ERR(dentry
)) {
209 err
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
214 err
= PTR_ERR(dentry
);
216 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
222 static int delete_path(const char *nodepath
)
227 path
= kstrdup(nodepath
, GFP_KERNEL
);
234 base
= strrchr(path
, '/');
238 err
= dev_rmdir(path
);
247 static int dev_mynode(struct device
*dev
, struct inode
*inode
, struct kstat
*stat
)
249 /* did we create it */
250 if (inode
->i_private
!= &dev_mnt
)
253 /* does the dev_t match */
254 if (is_blockdev(dev
)) {
255 if (!S_ISBLK(stat
->mode
))
258 if (!S_ISCHR(stat
->mode
))
261 if (stat
->rdev
!= dev
->devt
)
268 int devtmpfs_delete_node(struct device
*dev
)
270 const char *tmp
= NULL
;
271 const char *nodename
;
272 const struct cred
*curr_cred
;
274 struct dentry
*dentry
;
282 nodename
= device_get_devnode(dev
, NULL
, &tmp
);
286 curr_cred
= override_creds(&init_cred
);
287 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
288 nodename
, LOOKUP_PARENT
, &nd
);
292 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
293 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
294 if (!IS_ERR(dentry
)) {
295 if (dentry
->d_inode
) {
296 err
= vfs_getattr(nd
.path
.mnt
, dentry
, &stat
);
297 if (!err
&& dev_mynode(dev
, dentry
->d_inode
, &stat
)) {
298 struct iattr newattrs
;
300 * before unlinking this node, reset permissions
301 * of possible references like hardlinks
305 newattrs
.ia_mode
= stat
.mode
& ~0777;
307 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
308 mutex_lock(&dentry
->d_inode
->i_mutex
);
309 notify_change(dentry
, &newattrs
);
310 mutex_unlock(&dentry
->d_inode
->i_mutex
);
311 err
= vfs_unlink(nd
.path
.dentry
->d_inode
,
313 if (!err
|| err
== -ENOENT
)
321 err
= PTR_ERR(dentry
);
323 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
326 if (deleted
&& strchr(nodename
, '/'))
327 delete_path(nodename
);
330 revert_creds(curr_cred
);
335 * If configured, or requested by the commandline, devtmpfs will be
336 * auto-mounted after the kernel mounted the root filesystem.
338 int devtmpfs_mount(const char *mountpoint
)
349 err
= kern_path(mountpoint
, LOOKUP_FOLLOW
, &path
);
352 err
= do_add_mount(dev_mnt
, &path
, 0, NULL
);
354 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
356 printk(KERN_INFO
"devtmpfs: mounted\n");
362 * Create devtmpfs instance, driver-core devices will add their device
365 int __init
devtmpfs_init(void)
368 struct vfsmount
*mnt
;
369 char options
[] = "mode=0755";
371 err
= register_filesystem(&dev_fs_type
);
373 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
378 mnt
= kern_mount_data(&dev_fs_type
, options
);
381 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
382 unregister_filesystem(&dev_fs_type
);
387 printk(KERN_INFO
"devtmpfs: initialized\n");