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>
26 #include <linux/slab.h>
28 static struct vfsmount
*dev_mnt
;
30 #if defined CONFIG_DEVTMPFS_MOUNT
31 static int dev_mount
= 1;
36 static DEFINE_MUTEX(dirlock
);
38 static int __init
mount_param(char *str
)
40 dev_mount
= simple_strtoul(str
, NULL
, 0);
43 __setup("devtmpfs.mount=", mount_param
);
45 static int dev_get_sb(struct file_system_type
*fs_type
, int flags
,
46 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
48 return get_sb_single(fs_type
, flags
, data
, shmem_fill_super
, mnt
);
51 static struct file_system_type dev_fs_type
= {
54 .kill_sb
= kill_litter_super
,
58 static inline int is_blockdev(struct device
*dev
)
60 return dev
->class == &block_class
;
63 static inline int is_blockdev(struct device
*dev
) { return 0; }
66 static int dev_mkdir(const char *name
, mode_t mode
)
69 struct dentry
*dentry
;
72 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
73 name
, LOOKUP_PARENT
, &nd
);
77 dentry
= lookup_create(&nd
, 1);
78 if (!IS_ERR(dentry
)) {
79 err
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
81 /* mark as kernel-created inode */
82 dentry
->d_inode
->i_private
= &dev_mnt
;
85 err
= PTR_ERR(dentry
);
88 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
93 static int create_path(const char *nodepath
)
98 err
= dev_mkdir(nodepath
, 0755);
103 /* parent directories do not exist, create them */
104 path
= kstrdup(nodepath
, GFP_KERNEL
);
115 err
= dev_mkdir(path
, 0755);
116 if (err
&& err
!= -EEXIST
)
124 mutex_unlock(&dirlock
);
128 int devtmpfs_create_node(struct device
*dev
)
130 const char *tmp
= NULL
;
131 const char *nodename
;
132 const struct cred
*curr_cred
;
135 struct dentry
*dentry
;
141 nodename
= device_get_devnode(dev
, &mode
, &tmp
);
147 if (is_blockdev(dev
))
152 curr_cred
= override_creds(&init_cred
);
154 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
155 nodename
, LOOKUP_PARENT
, &nd
);
156 if (err
== -ENOENT
) {
157 create_path(nodename
);
158 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
159 nodename
, LOOKUP_PARENT
, &nd
);
164 dentry
= lookup_create(&nd
, 0);
165 if (!IS_ERR(dentry
)) {
166 err
= vfs_mknod(nd
.path
.dentry
->d_inode
,
167 dentry
, mode
, dev
->devt
);
169 struct iattr newattrs
;
171 /* fixup possibly umasked mode */
172 newattrs
.ia_mode
= mode
;
173 newattrs
.ia_valid
= ATTR_MODE
;
174 mutex_lock(&dentry
->d_inode
->i_mutex
);
175 notify_change(dentry
, &newattrs
);
176 mutex_unlock(&dentry
->d_inode
->i_mutex
);
178 /* mark as kernel-created inode */
179 dentry
->d_inode
->i_private
= &dev_mnt
;
183 err
= PTR_ERR(dentry
);
186 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
)) {
208 if (dentry
->d_inode
) {
209 if (dentry
->d_inode
->i_private
== &dev_mnt
)
210 err
= vfs_rmdir(nd
.path
.dentry
->d_inode
,
219 err
= PTR_ERR(dentry
);
222 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
227 static int delete_path(const char *nodepath
)
232 path
= kstrdup(nodepath
, GFP_KERNEL
);
236 mutex_lock(&dirlock
);
240 base
= strrchr(path
, '/');
244 err
= dev_rmdir(path
);
248 mutex_unlock(&dirlock
);
254 static int dev_mynode(struct device
*dev
, struct inode
*inode
, struct kstat
*stat
)
256 /* did we create it */
257 if (inode
->i_private
!= &dev_mnt
)
260 /* does the dev_t match */
261 if (is_blockdev(dev
)) {
262 if (!S_ISBLK(stat
->mode
))
265 if (!S_ISCHR(stat
->mode
))
268 if (stat
->rdev
!= dev
->devt
)
275 int devtmpfs_delete_node(struct device
*dev
)
277 const char *tmp
= NULL
;
278 const char *nodename
;
279 const struct cred
*curr_cred
;
281 struct dentry
*dentry
;
289 nodename
= device_get_devnode(dev
, NULL
, &tmp
);
293 curr_cred
= override_creds(&init_cred
);
294 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
295 nodename
, LOOKUP_PARENT
, &nd
);
299 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
300 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
301 if (!IS_ERR(dentry
)) {
302 if (dentry
->d_inode
) {
303 err
= vfs_getattr(nd
.path
.mnt
, dentry
, &stat
);
304 if (!err
&& dev_mynode(dev
, dentry
->d_inode
, &stat
)) {
305 struct iattr newattrs
;
307 * before unlinking this node, reset permissions
308 * of possible references like hardlinks
312 newattrs
.ia_mode
= stat
.mode
& ~0777;
314 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
315 mutex_lock(&dentry
->d_inode
->i_mutex
);
316 notify_change(dentry
, &newattrs
);
317 mutex_unlock(&dentry
->d_inode
->i_mutex
);
318 err
= vfs_unlink(nd
.path
.dentry
->d_inode
,
320 if (!err
|| err
== -ENOENT
)
328 err
= PTR_ERR(dentry
);
330 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
333 if (deleted
&& strchr(nodename
, '/'))
334 delete_path(nodename
);
337 revert_creds(curr_cred
);
342 * If configured, or requested by the commandline, devtmpfs will be
343 * auto-mounted after the kernel mounted the root filesystem.
345 int devtmpfs_mount(const char *mntdir
)
355 err
= sys_mount("devtmpfs", (char *)mntdir
, "devtmpfs", MS_SILENT
, NULL
);
357 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
359 printk(KERN_INFO
"devtmpfs: mounted\n");
364 * Create devtmpfs instance, driver-core devices will add their device
367 int __init
devtmpfs_init(void)
370 struct vfsmount
*mnt
;
371 char options
[] = "mode=0755";
373 err
= register_filesystem(&dev_fs_type
);
375 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
380 mnt
= kern_mount_data(&dev_fs_type
, options
);
383 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
384 unregister_filesystem(&dev_fs_type
);
389 printk(KERN_INFO
"devtmpfs: initialized\n");