1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
3 * devtmpfs - kernel-maintained tmpfs-based /dev
5 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
7 * During bootup, before any driver core device is registered,
8 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
9 * device which requests a device node, will add a node in this
11 * By default, all devices are named after the the name of the
12 * device, owned by root and have a default mode of 0600. Subsystems
13 * can overwrite the default setting if needed.
16 #include <linux/kernel.h>
17 #include <linux/syscalls.h>
18 #include <linux/mount.h>
19 #include <linux/device.h>
20 #include <linux/genhd.h>
21 #include <linux/namei.h>
23 #include <linux/shmem_fs.h>
24 #include <linux/ramfs.h>
25 #include <linux/cred.h>
26 #include <linux/sched.h>
27 #include <linux/init_task.h>
28 #include <linux/slab.h>
30 static struct vfsmount
*dev_mnt
;
32 #if defined CONFIG_DEVTMPFS_MOUNT
33 static int dev_mount
= 1;
38 static DEFINE_MUTEX(dirlock
);
40 static int __init
mount_param(char *str
)
42 dev_mount
= simple_strtoul(str
, NULL
, 0);
45 __setup("devtmpfs.mount=", mount_param
);
47 static int dev_get_sb(struct file_system_type
*fs_type
, int flags
,
48 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
51 return get_sb_single(fs_type
, flags
, data
, shmem_fill_super
, mnt
);
53 return get_sb_single(fs_type
, flags
, data
, ramfs_fill_super
, mnt
);
57 static struct file_system_type dev_fs_type
= {
60 .kill_sb
= kill_litter_super
,
64 static inline int is_blockdev(struct device
*dev
)
66 return dev
->class == &block_class
;
69 static inline int is_blockdev(struct device
*dev
) { return 0; }
72 static int dev_mkdir(const char *name
, mode_t mode
)
75 struct dentry
*dentry
;
78 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
79 name
, LOOKUP_PARENT
, &nd
);
83 dentry
= lookup_create(&nd
, 1);
84 if (!IS_ERR(dentry
)) {
85 err
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
87 /* mark as kernel-created inode */
88 dentry
->d_inode
->i_private
= &dev_mnt
;
91 err
= PTR_ERR(dentry
);
94 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
99 static int create_path(const char *nodepath
)
103 mutex_lock(&dirlock
);
104 err
= dev_mkdir(nodepath
, 0755);
105 if (err
== -ENOENT
) {
109 /* parent directories do not exist, create them */
110 path
= kstrdup(nodepath
, GFP_KERNEL
);
121 err
= dev_mkdir(path
, 0755);
122 if (err
&& err
!= -EEXIST
)
130 mutex_unlock(&dirlock
);
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
);
160 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
161 nodename
, LOOKUP_PARENT
, &nd
);
162 if (err
== -ENOENT
) {
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
)) {
172 err
= vfs_mknod(nd
.path
.dentry
->d_inode
,
173 dentry
, mode
, dev
->devt
);
175 struct iattr newattrs
;
177 /* fixup possibly umasked mode */
178 newattrs
.ia_mode
= mode
;
179 newattrs
.ia_valid
= ATTR_MODE
;
180 mutex_lock(&dentry
->d_inode
->i_mutex
);
181 notify_change(dentry
, &newattrs
);
182 mutex_unlock(&dentry
->d_inode
->i_mutex
);
184 /* mark as kernel-created inode */
185 dentry
->d_inode
->i_private
= &dev_mnt
;
189 err
= PTR_ERR(dentry
);
192 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
196 revert_creds(curr_cred
);
200 static int dev_rmdir(const char *name
)
203 struct dentry
*dentry
;
206 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
207 name
, LOOKUP_PARENT
, &nd
);
211 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
212 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
213 if (!IS_ERR(dentry
)) {
214 if (dentry
->d_inode
) {
215 if (dentry
->d_inode
->i_private
== &dev_mnt
)
216 err
= vfs_rmdir(nd
.path
.dentry
->d_inode
,
225 err
= PTR_ERR(dentry
);
228 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
233 static int delete_path(const char *nodepath
)
238 path
= kstrdup(nodepath
, GFP_KERNEL
);
242 mutex_lock(&dirlock
);
246 base
= strrchr(path
, '/');
250 err
= dev_rmdir(path
);
254 mutex_unlock(&dirlock
);
260 static int dev_mynode(struct device
*dev
, struct inode
*inode
, struct kstat
*stat
)
262 /* did we create it */
263 if (inode
->i_private
!= &dev_mnt
)
266 /* does the dev_t match */
267 if (is_blockdev(dev
)) {
268 if (!S_ISBLK(stat
->mode
))
271 if (!S_ISCHR(stat
->mode
))
274 if (stat
->rdev
!= dev
->devt
)
281 int devtmpfs_delete_node(struct device
*dev
)
283 const char *tmp
= NULL
;
284 const char *nodename
;
285 const struct cred
*curr_cred
;
287 struct dentry
*dentry
;
295 nodename
= device_get_devnode(dev
, NULL
, &tmp
);
299 curr_cred
= override_creds(&init_cred
);
300 err
= vfs_path_lookup(dev_mnt
->mnt_root
, dev_mnt
,
301 nodename
, LOOKUP_PARENT
, &nd
);
305 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
306 dentry
= lookup_one_len(nd
.last
.name
, nd
.path
.dentry
, nd
.last
.len
);
307 if (!IS_ERR(dentry
)) {
308 if (dentry
->d_inode
) {
309 err
= vfs_getattr(nd
.path
.mnt
, dentry
, &stat
);
310 if (!err
&& dev_mynode(dev
, dentry
->d_inode
, &stat
)) {
311 struct iattr newattrs
;
313 * before unlinking this node, reset permissions
314 * of possible references like hardlinks
318 newattrs
.ia_mode
= stat
.mode
& ~0777;
320 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
321 mutex_lock(&dentry
->d_inode
->i_mutex
);
322 notify_change(dentry
, &newattrs
);
323 mutex_unlock(&dentry
->d_inode
->i_mutex
);
324 err
= vfs_unlink(nd
.path
.dentry
->d_inode
,
326 if (!err
|| err
== -ENOENT
)
334 err
= PTR_ERR(dentry
);
336 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
339 if (deleted
&& strchr(nodename
, '/'))
340 delete_path(nodename
);
343 revert_creds(curr_cred
);
348 * If configured, or requested by the commandline, devtmpfs will be
349 * auto-mounted after the kernel mounted the root filesystem.
351 int devtmpfs_mount(const char *mntdir
)
361 err
= sys_mount("devtmpfs", (char *)mntdir
, "devtmpfs", MS_SILENT
, NULL
);
363 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
365 printk(KERN_INFO
"devtmpfs: mounted\n");
370 * Create devtmpfs instance, driver-core devices will add their device
373 int __init
devtmpfs_init(void)
376 struct vfsmount
*mnt
;
377 char *options
= "mode=0755";
378 /* LR: char options[] = "..."; caused misaligned access, gcc issue */
380 err
= register_filesystem(&dev_fs_type
);
382 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
387 mnt
= kern_mount_data(&dev_fs_type
, options
);
390 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
391 unregister_filesystem(&dev_fs_type
);
396 printk(KERN_INFO
"devtmpfs: initialized\n");