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 static int __initdata mount_dev
= IS_ENABLED(CONFIG_DEVTMPFS_MOUNT
);
35 static DEFINE_SPINLOCK(req_lock
);
39 struct completion done
;
42 umode_t mode
; /* 0 => delete */
48 static int __init
mount_param(char *str
)
50 mount_dev
= simple_strtoul(str
, NULL
, 0);
53 __setup("devtmpfs.mount=", mount_param
);
55 static struct vfsmount
*mnt
;
57 static struct dentry
*public_dev_mount(struct file_system_type
*fs_type
, int flags
,
58 const char *dev_name
, void *data
)
60 struct super_block
*s
= mnt
->mnt_sb
;
61 atomic_inc(&s
->s_active
);
62 down_write(&s
->s_umount
);
63 return dget(s
->s_root
);
66 static struct file_system_type internal_fs_type
= {
69 .init_fs_context
= shmem_init_fs_context
,
70 .parameters
= shmem_fs_parameters
,
72 .init_fs_context
= ramfs_init_fs_context
,
73 .parameters
= ramfs_fs_parameters
,
75 .kill_sb
= kill_litter_super
,
78 static struct file_system_type dev_fs_type
= {
80 .mount
= public_dev_mount
,
84 static inline int is_blockdev(struct device
*dev
)
86 return dev
->class == &block_class
;
89 static inline int is_blockdev(struct device
*dev
) { return 0; }
92 static int devtmpfs_submit_req(struct req
*req
, const char *tmp
)
94 init_completion(&req
->done
);
99 spin_unlock(&req_lock
);
101 wake_up_process(thread
);
102 wait_for_completion(&req
->done
);
109 int devtmpfs_create_node(struct device
*dev
)
111 const char *tmp
= NULL
;
118 req
.uid
= GLOBAL_ROOT_UID
;
119 req
.gid
= GLOBAL_ROOT_GID
;
120 req
.name
= device_get_devnode(dev
, &req
.mode
, &req
.uid
, &req
.gid
, &tmp
);
126 if (is_blockdev(dev
))
133 return devtmpfs_submit_req(&req
, tmp
);
136 int devtmpfs_delete_node(struct device
*dev
)
138 const char *tmp
= NULL
;
144 req
.name
= device_get_devnode(dev
, NULL
, NULL
, NULL
, &tmp
);
151 return devtmpfs_submit_req(&req
, tmp
);
154 static int dev_mkdir(const char *name
, umode_t mode
)
156 struct dentry
*dentry
;
160 dentry
= kern_path_create(AT_FDCWD
, name
, &path
, LOOKUP_DIRECTORY
);
162 return PTR_ERR(dentry
);
164 err
= vfs_mkdir(d_inode(path
.dentry
), dentry
, mode
);
166 /* mark as kernel-created inode */
167 d_inode(dentry
)->i_private
= &thread
;
168 done_path_create(&path
, dentry
);
172 static int create_path(const char *nodepath
)
178 /* parent directories do not exist, create them */
179 path
= kstrdup(nodepath
, GFP_KERNEL
);
189 err
= dev_mkdir(path
, 0755);
190 if (err
&& err
!= -EEXIST
)
199 static int handle_create(const char *nodename
, umode_t mode
, kuid_t uid
,
200 kgid_t gid
, struct device
*dev
)
202 struct dentry
*dentry
;
206 dentry
= kern_path_create(AT_FDCWD
, nodename
, &path
, 0);
207 if (dentry
== ERR_PTR(-ENOENT
)) {
208 create_path(nodename
);
209 dentry
= kern_path_create(AT_FDCWD
, nodename
, &path
, 0);
212 return PTR_ERR(dentry
);
214 err
= vfs_mknod(d_inode(path
.dentry
), dentry
, mode
, dev
->devt
);
216 struct iattr newattrs
;
218 newattrs
.ia_mode
= mode
;
219 newattrs
.ia_uid
= uid
;
220 newattrs
.ia_gid
= gid
;
221 newattrs
.ia_valid
= ATTR_MODE
|ATTR_UID
|ATTR_GID
;
222 inode_lock(d_inode(dentry
));
223 notify_change(dentry
, &newattrs
, NULL
);
224 inode_unlock(d_inode(dentry
));
226 /* mark as kernel-created inode */
227 d_inode(dentry
)->i_private
= &thread
;
229 done_path_create(&path
, dentry
);
233 static int dev_rmdir(const char *name
)
236 struct dentry
*dentry
;
239 dentry
= kern_path_locked(name
, &parent
);
241 return PTR_ERR(dentry
);
242 if (d_really_is_positive(dentry
)) {
243 if (d_inode(dentry
)->i_private
== &thread
)
244 err
= vfs_rmdir(d_inode(parent
.dentry
), dentry
);
251 inode_unlock(d_inode(parent
.dentry
));
256 static int delete_path(const char *nodepath
)
261 path
= kstrdup(nodepath
, GFP_KERNEL
);
268 base
= strrchr(path
, '/');
272 err
= dev_rmdir(path
);
281 static int dev_mynode(struct device
*dev
, struct inode
*inode
, struct kstat
*stat
)
283 /* did we create it */
284 if (inode
->i_private
!= &thread
)
287 /* does the dev_t match */
288 if (is_blockdev(dev
)) {
289 if (!S_ISBLK(stat
->mode
))
292 if (!S_ISCHR(stat
->mode
))
295 if (stat
->rdev
!= dev
->devt
)
302 static int handle_remove(const char *nodename
, struct device
*dev
)
305 struct dentry
*dentry
;
309 dentry
= kern_path_locked(nodename
, &parent
);
311 return PTR_ERR(dentry
);
313 if (d_really_is_positive(dentry
)) {
315 struct path p
= {.mnt
= parent
.mnt
, .dentry
= dentry
};
316 err
= vfs_getattr(&p
, &stat
, STATX_TYPE
| STATX_MODE
,
317 AT_STATX_SYNC_AS_STAT
);
318 if (!err
&& dev_mynode(dev
, d_inode(dentry
), &stat
)) {
319 struct iattr newattrs
;
321 * before unlinking this node, reset permissions
322 * of possible references like hardlinks
324 newattrs
.ia_uid
= GLOBAL_ROOT_UID
;
325 newattrs
.ia_gid
= GLOBAL_ROOT_GID
;
326 newattrs
.ia_mode
= stat
.mode
& ~0777;
328 ATTR_UID
|ATTR_GID
|ATTR_MODE
;
329 inode_lock(d_inode(dentry
));
330 notify_change(dentry
, &newattrs
, NULL
);
331 inode_unlock(d_inode(dentry
));
332 err
= vfs_unlink(d_inode(parent
.dentry
), dentry
, NULL
);
333 if (!err
|| err
== -ENOENT
)
340 inode_unlock(d_inode(parent
.dentry
));
343 if (deleted
&& strchr(nodename
, '/'))
344 delete_path(nodename
);
349 * If configured, or requested by the commandline, devtmpfs will be
350 * auto-mounted after the kernel mounted the root filesystem.
352 int __init
devtmpfs_mount(void)
362 err
= do_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT
, NULL
);
364 printk(KERN_INFO
"devtmpfs: error mounting %i\n", err
);
366 printk(KERN_INFO
"devtmpfs: mounted\n");
370 static DECLARE_COMPLETION(setup_done
);
372 static int handle(const char *name
, umode_t mode
, kuid_t uid
, kgid_t gid
,
376 return handle_create(name
, mode
, uid
, gid
, dev
);
378 return handle_remove(name
, dev
);
381 static int devtmpfs_setup(void *p
)
385 err
= ksys_unshare(CLONE_NEWNS
);
388 err
= do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT
, NULL
);
391 ksys_chdir("/.."); /* will traverse into overmounted root */
395 complete(&setup_done
);
399 static int devtmpfsd(void *p
)
401 int err
= devtmpfs_setup(p
);
406 spin_lock(&req_lock
);
408 struct req
*req
= requests
;
410 spin_unlock(&req_lock
);
412 struct req
*next
= req
->next
;
413 req
->err
= handle(req
->name
, req
->mode
,
414 req
->uid
, req
->gid
, req
->dev
);
415 complete(&req
->done
);
418 spin_lock(&req_lock
);
420 __set_current_state(TASK_INTERRUPTIBLE
);
421 spin_unlock(&req_lock
);
428 * Create devtmpfs instance, driver-core devices will add their device
431 int __init
devtmpfs_init(void)
433 char opts
[] = "mode=0755";
436 mnt
= vfs_kern_mount(&internal_fs_type
, 0, "devtmpfs", opts
);
438 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %ld\n",
442 err
= register_filesystem(&dev_fs_type
);
444 printk(KERN_ERR
"devtmpfs: unable to register devtmpfs "
449 thread
= kthread_run(devtmpfsd
, &err
, "kdevtmpfs");
450 if (!IS_ERR(thread
)) {
451 wait_for_completion(&setup_done
);
453 err
= PTR_ERR(thread
);
458 printk(KERN_ERR
"devtmpfs: unable to create devtmpfs %i\n", err
);
459 unregister_filesystem(&dev_fs_type
);
463 printk(KERN_INFO
"devtmpfs: initialized\n");