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 name of the device,
11 * owned by root and have a default mode of 0600. Subsystems can
12 * 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/sched.h>
25 #include <linux/slab.h>
26 #include <linux/kthread.h>
28 static struct task_struct *thread;
30 #if defined CONFIG_DEVTMPFS_MOUNT
31 static int mount_dev = 1;
36 static DEFINE_SPINLOCK(req_lock);
40 struct completion done;
43 umode_t mode; /* 0 => delete */
49 static int __init mount_param(char *str)
51 mount_dev = simple_strtoul(str, NULL, 0);
54 __setup("devtmpfs.mount=", mount_param);
56 static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
57 const char *dev_name, void *data)
60 return mount_single(fs_type, flags, data, shmem_fill_super);
62 return mount_single(fs_type, flags, data, ramfs_fill_super);
66 static struct file_system_type dev_fs_type = {
69 .kill_sb = kill_litter_super,
73 static inline int is_blockdev(struct device *dev)
75 return dev->class == &block_class;
78 static inline int is_blockdev(struct device *dev) { return 0; }
81 int devtmpfs_create_node(struct device *dev)
83 const char *tmp = NULL;
92 req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
105 init_completion(&req.done);
107 spin_lock(&req_lock);
110 spin_unlock(&req_lock);
112 wake_up_process(thread);
113 wait_for_completion(&req.done);
120 int devtmpfs_delete_node(struct device *dev)
122 const char *tmp = NULL;
128 req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
135 init_completion(&req.done);
137 spin_lock(&req_lock);
140 spin_unlock(&req_lock);
142 wake_up_process(thread);
143 wait_for_completion(&req.done);
149 static int dev_mkdir(const char *name, umode_t mode)
151 struct dentry *dentry;
155 dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
157 return PTR_ERR(dentry);
159 err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
161 /* mark as kernel-created inode */
162 dentry->d_inode->i_private = &thread;
163 done_path_create(&path, dentry);
167 static int create_path(const char *nodepath)
173 /* parent directories do not exist, create them */
174 path = kstrdup(nodepath, GFP_KERNEL);
184 err = dev_mkdir(path, 0755);
185 if (err && err != -EEXIST)
194 static int handle_create(const char *nodename, umode_t mode, uid_t uid,
195 gid_t gid, struct device *dev)
197 struct dentry *dentry;
201 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
202 if (dentry == ERR_PTR(-ENOENT)) {
203 create_path(nodename);
204 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
207 return PTR_ERR(dentry);
209 err = vfs_mknod(path.dentry->d_inode, dentry, mode, dev->devt);
211 struct iattr newattrs;
213 newattrs.ia_mode = mode;
214 newattrs.ia_uid = uid;
215 newattrs.ia_gid = gid;
216 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
217 mutex_lock(&dentry->d_inode->i_mutex);
218 notify_change(dentry, &newattrs);
219 mutex_unlock(&dentry->d_inode->i_mutex);
221 /* mark as kernel-created inode */
222 dentry->d_inode->i_private = &thread;
224 done_path_create(&path, dentry);
228 static int dev_rmdir(const char *name)
231 struct dentry *dentry;
234 dentry = kern_path_locked(name, &parent);
236 return PTR_ERR(dentry);
237 if (dentry->d_inode) {
238 if (dentry->d_inode->i_private == &thread)
239 err = vfs_rmdir(parent.dentry->d_inode, dentry);
246 mutex_unlock(&parent.dentry->d_inode->i_mutex);
251 static int delete_path(const char *nodepath)
256 path = kstrdup(nodepath, GFP_KERNEL);
263 base = strrchr(path, '/');
267 err = dev_rmdir(path);
276 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
278 /* did we create it */
279 if (inode->i_private != &thread)
282 /* does the dev_t match */
283 if (is_blockdev(dev)) {
284 if (!S_ISBLK(stat->mode))
287 if (!S_ISCHR(stat->mode))
290 if (stat->rdev != dev->devt)
297 static int handle_remove(const char *nodename, struct device *dev)
300 struct dentry *dentry;
304 dentry = kern_path_locked(nodename, &parent);
306 return PTR_ERR(dentry);
308 if (dentry->d_inode) {
310 struct path p = {.mnt = parent.mnt, .dentry = dentry};
311 err = vfs_getattr(&p, &stat);
312 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
313 struct iattr newattrs;
315 * before unlinking this node, reset permissions
316 * of possible references like hardlinks
318 newattrs.ia_uid = GLOBAL_ROOT_UID;
319 newattrs.ia_gid = GLOBAL_ROOT_GID;
320 newattrs.ia_mode = stat.mode & ~0777;
322 ATTR_UID|ATTR_GID|ATTR_MODE;
323 mutex_lock(&dentry->d_inode->i_mutex);
324 notify_change(dentry, &newattrs);
325 mutex_unlock(&dentry->d_inode->i_mutex);
326 err = vfs_unlink(parent.dentry->d_inode, dentry);
327 if (!err || err == -ENOENT)
334 mutex_unlock(&parent.dentry->d_inode->i_mutex);
337 if (deleted && strchr(nodename, '/'))
338 delete_path(nodename);
343 * If configured, or requested by the commandline, devtmpfs will be
344 * auto-mounted after the kernel mounted the root filesystem.
346 int devtmpfs_mount(const char *mntdir)
356 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
358 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
360 printk(KERN_INFO "devtmpfs: mounted\n");
364 static DECLARE_COMPLETION(setup_done);
366 static int handle(const char *name, umode_t mode, uid_t uid, gid_t gid,
370 return handle_create(name, mode, uid, gid, dev);
372 return handle_remove(name, dev);
375 static int devtmpfsd(void *p)
377 char options[] = "mode=0755";
379 *err = sys_unshare(CLONE_NEWNS);
382 *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
385 sys_chdir("/.."); /* will traverse into overmounted root */
387 complete(&setup_done);
389 spin_lock(&req_lock);
391 struct req *req = requests;
393 spin_unlock(&req_lock);
395 struct req *next = req->next;
396 req->err = handle(req->name, req->mode,
397 req->uid, req->gid, req->dev);
398 complete(&req->done);
401 spin_lock(&req_lock);
403 __set_current_state(TASK_INTERRUPTIBLE);
404 spin_unlock(&req_lock);
409 complete(&setup_done);
414 * Create devtmpfs instance, driver-core devices will add their device
417 int __init devtmpfs_init(void)
419 int err = register_filesystem(&dev_fs_type);
421 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
426 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
427 if (!IS_ERR(thread)) {
428 wait_for_completion(&setup_done);
430 err = PTR_ERR(thread);
435 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
436 unregister_filesystem(&dev_fs_type);
440 printk(KERN_INFO "devtmpfs: initialized\n");