1 // SPDX-License-Identifier: GPL-2.0
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 name of the device,
12 * owned by root and have a default mode of 0600. Subsystems can
13 * 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/sched.h>
26 #include <linux/slab.h>
27 #include <linux/kthread.h>
28 #include <uapi/linux/mount.h>
31 static struct task_struct
*thread
;
33 #if defined CONFIG_DEVTMPFS_MOUNT
34 static int mount_dev
= 1;
39 static DEFINE_SPINLOCK(req_lock
);
43 struct completion done
;
46 umode_t mode
; /* 0 => delete */
52 static int __init
mount_param(char *str
)
54 mount_dev
= simple_strtoul(str
, NULL
, 0);
57 __setup("devtmpfs.mount=", mount_param
);
59 static struct vfsmount
*mnt
;
61 static struct dentry
*public_dev_mount(struct file_system_type
*fs_type
, int flags
,
62 const char *dev_name
, void *data
)
64 struct super_block
*s
= mnt
->mnt_sb
;
65 atomic_inc(&s
->s_active
);
66 down_write(&s
->s_umount
);
67 return dget(s
->s_root
);
70 static struct file_system_type internal_fs_type
= {
73 .init_fs_context
= shmem_init_fs_context
,
74 .parameters
= &shmem_fs_parameters
,
76 .init_fs_context
= ramfs_init_fs_context
,
77 .parameters
= &ramfs_fs_parameters
,
79 .kill_sb
= kill_litter_super
,
82 static struct file_system_type dev_fs_type
= {
84 .mount
= public_dev_mount
,
88 static inline int is_blockdev(struct device
*dev
)
90 return dev
->class == &block_class
;
93 static inline int is_blockdev(struct device
*dev
) { return 0; }
96 int devtmpfs_create_node(struct device
*dev
)
98 const char *tmp
= NULL
;
105 req
.uid
= GLOBAL_ROOT_UID
;
106 req
.gid
= GLOBAL_ROOT_GID
;
107 req
.name
= device_get_devnode(dev
, &req
.mode
, &req
.uid
, &req
.gid
, &tmp
);
113 if (is_blockdev(dev
))
120 init_completion(&req
.done
);
122 spin_lock(&req_lock
);
125 spin_unlock(&req_lock
);
127 wake_up_process(thread
);
128 wait_for_completion(&req
.done
);
135 int devtmpfs_delete_node(struct device
*dev
)
137 const char *tmp
= NULL
;
143 req
.name
= device_get_devnode(dev
, NULL
, NULL
, NULL
, &tmp
);
150 init_completion(&req
.done
);
152 spin_lock(&req_lock
);
155 spin_unlock(&req_lock
);
157 wake_up_process(thread
);
158 wait_for_completion(&req
.done
);
164 static int dev_mkdir(const char *name
, umode_t mode
)
166 struct dentry
*dentry
;
170 dentry
= kern_path_create(AT_FDCWD
, name
, &path
, LOOKUP_DIRECTORY
);
172 return PTR_ERR(dentry
);
174 err
= vfs_mkdir(d_inode(path
.dentry
), dentry
, mode
);
176 /* mark as kernel-created inode */
177 d_inode(dentry
)->i_private
= &thread
;
178 done_path_create(&path
, dentry
);
182 static int create_path(const char *nodepath
)
188 /* parent directories do not exist, create them */
189 path
= kstrdup(nodepath
, GFP_KERNEL
);
199 err
= dev_mkdir(path
, 0755);
200 if (err
&& err
!= -EEXIST
)
209 static int handle_create(const char *nodename
, umode_t mode
, kuid_t uid
,
210 kgid_t gid
, struct device
*dev
)
212 struct dentry
*dentry
;
216 dentry
= kern_path_create(AT_FDCWD
, nodename
, &path
, 0);
217 if (dentry
== ERR_PTR(-ENOENT
)) {
218 create_path(nodename
);
219 dentry
= kern_path_create(AT_FDCWD
, nodename
, &path
, 0);
222 return PTR_ERR(dentry
);
224 err
= vfs_mknod(d_inode(path
.dentry
), dentry
, mode
, dev
->devt
);
226 struct iattr newattrs
;
228 newattrs
.ia_mode
= mode
;
229 newattrs
.ia_uid
= uid
;
230 newattrs
.ia_gid
= gid
;
231 newattrs
.ia_valid
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
232 inode_lock(d_inode(dentry
));
233 notify_change(dentry
, &newattrs
, NULL
);
234 inode_unlock(d_inode(dentry
));
236 /* mark as kernel-created inode */
237 d_inode(dentry
)->i_private
= &thread
;
239 done_path_create(&path
, dentry
);
243 static int dev_rmdir(const char *name
)
246 struct dentry
*dentry
;
249 dentry
= kern_path_locked(name
, &parent
);
251 return PTR_ERR(dentry
);
252 if (d_really_is_positive(dentry
)) {
253 if (d_inode(dentry
)->i_private
== &thread
)
254 err
= vfs_rmdir(d_inode(parent
.dentry
), dentry
);
261 inode_unlock(d_inode(parent
.dentry
));
266 static int delete_path(const char *nodepath
)
271 path
= kstrdup(nodepath
, GFP_KERNEL
);
278 base
= strrchr(path
, '/');
282 err
= dev_rmdir(path
);
291 static int dev_mynode(struct device
*dev
, struct inode
*inode
, struct kstat
*stat
)
293 /* did we create it */
294 if (inode
->i_private
!= &thread
)
297 /* does the dev_t match */
298 if (is_blockdev(dev
)) {
299 if (!S_ISBLK(stat
->mode
))
302 if (!S_ISCHR(stat
->mode
))
305 if (stat
->rdev
!= dev
->devt
)
312 static int handle_remove(const char *nodename
, struct device
*dev
)
315 struct dentry
*dentry
;
319 dentry
= kern_path_locked(nodename
, &parent
);
321 return PTR_ERR(dentry
);
323 if (d_really_is_positive(dentry
)) {
325 struct path p
= {.mnt
= parent
.mnt
, .dentry
= dentry
};
326 err
= vfs_getattr(&p
, &stat
, STATX_TYPE
| STATX_MODE
,
327 AT_STATX_SYNC_AS_STAT
);
328 if (!err
&& dev_mynode(dev
, d_inode(dentry
), &stat
)) {
329 struct iattr newattrs
;
331 * before unlinking this node, reset permissions
332 * of possible references like hardlinks
334 newattrs
.ia_uid
= GLOBAL_ROOT_UID
;
335 newattrs
.ia_gid
= GLOBAL_ROOT_GID
;
336 newattrs
.ia_mode
= stat
.mode
& ~0777;
338 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
339 inode_lock(d_inode(dentry
));
340 notify_change(dentry
, &newattrs
, NULL
);
341 inode_unlock(d_inode(dentry
));
342 err
= vfs_unlink(d_inode(parent
.dentry
), dentry
, NULL
);
343 if (!err
|| err
== -ENOENT
)
350 inode_unlock(d_inode(parent
.dentry
));
353 if (deleted
&& strchr(nodename
, '/'))
354 delete_path(nodename
);
359 * If configured, or requested by the commandline, devtmpfs will be
360 * auto-mounted after the kernel mounted the root filesystem.
362 int devtmpfs_mount(const char *mntdir
)
372 err
= ksys_mount("devtmpfs", mntdir
, "devtmpfs", MS_SILENT
, NULL
);
374 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
376 printk(KERN_INFO
"devtmpfs: mounted\n");
380 static DECLARE_COMPLETION(setup_done
);
382 static int handle(const char *name
, umode_t mode
, kuid_t uid
, kgid_t gid
,
386 return handle_create(name
, mode
, uid
, gid
, dev
);
388 return handle_remove(name
, dev
);
391 static int devtmpfsd(void *p
)
394 *err
= ksys_unshare(CLONE_NEWNS
);
397 *err
= ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT
, NULL
);
400 ksys_chdir("/.."); /* will traverse into overmounted root */
402 complete(&setup_done
);
404 spin_lock(&req_lock
);
406 struct req
*req
= requests
;
408 spin_unlock(&req_lock
);
410 struct req
*next
= req
->next
;
411 req
->err
= handle(req
->name
, req
->mode
,
412 req
->uid
, req
->gid
, req
->dev
);
413 complete(&req
->done
);
416 spin_lock(&req_lock
);
418 __set_current_state(TASK_INTERRUPTIBLE
);
419 spin_unlock(&req_lock
);
424 complete(&setup_done
);
429 * Create devtmpfs instance, driver-core devices will add their device
432 int __init
devtmpfs_init(void)
434 char opts
[] = "mode=0755";
437 mnt
= vfs_kern_mount(&internal_fs_type
, 0, "devtmpfs", opts
);
439 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %ld\n",
443 err
= register_filesystem(&dev_fs_type
);
445 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
450 thread
= kthread_run(devtmpfsd
, &err
, "kdevtmpfs");
451 if (!IS_ERR(thread
)) {
452 wait_for_completion(&setup_done
);
454 err
= PTR_ERR(thread
);
459 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
460 unregister_filesystem(&dev_fs_type
);
464 printk(KERN_INFO
"devtmpfs: initialized\n");