hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/base/devtmpfs.c
....@@ -25,15 +25,13 @@
2525 #include <linux/sched.h>
2626 #include <linux/slab.h>
2727 #include <linux/kthread.h>
28
+#include <linux/init_syscalls.h>
29
+#include <uapi/linux/mount.h>
2830 #include "base.h"
2931
3032 static struct task_struct *thread;
3133
32
-#if defined CONFIG_DEVTMPFS_MOUNT
33
-static int mount_dev = 1;
34
-#else
35
-static int mount_dev;
36
-#endif
34
+static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
3735
3836 static DEFINE_SPINLOCK(req_lock);
3937
....@@ -55,20 +53,39 @@
5553 }
5654 __setup("devtmpfs.mount=", mount_param);
5755
58
-static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
56
+static struct vfsmount *mnt;
57
+
58
+static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
5959 const char *dev_name, void *data)
6060 {
61
-#ifdef CONFIG_TMPFS
62
- return mount_single(fs_type, flags, data, shmem_fill_super);
63
-#else
64
- return mount_single(fs_type, flags, data, ramfs_fill_super);
65
-#endif
61
+ struct super_block *s = mnt->mnt_sb;
62
+ int err;
63
+
64
+ atomic_inc(&s->s_active);
65
+ down_write(&s->s_umount);
66
+ err = reconfigure_single(s, flags, data);
67
+ if (err < 0) {
68
+ deactivate_locked_super(s);
69
+ return ERR_PTR(err);
70
+ }
71
+ return dget(s->s_root);
6672 }
73
+
74
+static struct file_system_type internal_fs_type = {
75
+ .name = "devtmpfs",
76
+#ifdef CONFIG_TMPFS
77
+ .init_fs_context = shmem_init_fs_context,
78
+ .parameters = shmem_fs_parameters,
79
+#else
80
+ .init_fs_context = ramfs_init_fs_context,
81
+ .parameters = ramfs_fs_parameters,
82
+#endif
83
+ .kill_sb = kill_litter_super,
84
+};
6785
6886 static struct file_system_type dev_fs_type = {
6987 .name = "devtmpfs",
70
- .mount = dev_mount,
71
- .kill_sb = kill_litter_super,
88
+ .mount = public_dev_mount,
7289 };
7390
7491 #ifdef CONFIG_BLOCK
....@@ -79,6 +96,23 @@
7996 #else
8097 static inline int is_blockdev(struct device *dev) { return 0; }
8198 #endif
99
+
100
+static int devtmpfs_submit_req(struct req *req, const char *tmp)
101
+{
102
+ init_completion(&req->done);
103
+
104
+ spin_lock(&req_lock);
105
+ req->next = requests;
106
+ requests = req;
107
+ spin_unlock(&req_lock);
108
+
109
+ wake_up_process(thread);
110
+ wait_for_completion(&req->done);
111
+
112
+ kfree(tmp);
113
+
114
+ return req->err;
115
+}
82116
83117 int devtmpfs_create_node(struct device *dev)
84118 {
....@@ -104,19 +138,7 @@
104138
105139 req.dev = dev;
106140
107
- init_completion(&req.done);
108
-
109
- spin_lock(&req_lock);
110
- req.next = requests;
111
- requests = &req;
112
- spin_unlock(&req_lock);
113
-
114
- wake_up_process(thread);
115
- wait_for_completion(&req.done);
116
-
117
- kfree(tmp);
118
-
119
- return req.err;
141
+ return devtmpfs_submit_req(&req, tmp);
120142 }
121143
122144 int devtmpfs_delete_node(struct device *dev)
....@@ -134,18 +156,7 @@
134156 req.mode = 0;
135157 req.dev = dev;
136158
137
- init_completion(&req.done);
138
-
139
- spin_lock(&req_lock);
140
- req.next = requests;
141
- requests = &req;
142
- spin_unlock(&req_lock);
143
-
144
- wake_up_process(thread);
145
- wait_for_completion(&req.done);
146
-
147
- kfree(tmp);
148
- return req.err;
159
+ return devtmpfs_submit_req(&req, tmp);
149160 }
150161
151162 static int dev_mkdir(const char *name, umode_t mode)
....@@ -252,7 +263,7 @@
252263
253264 static int delete_path(const char *nodepath)
254265 {
255
- const char *path;
266
+ char *path;
256267 int err = 0;
257268
258269 path = kstrdup(nodepath, GFP_KERNEL);
....@@ -346,7 +357,7 @@
346357 * If configured, or requested by the commandline, devtmpfs will be
347358 * auto-mounted after the kernel mounted the root filesystem.
348359 */
349
-int devtmpfs_mount(const char *mntdir)
360
+int __init devtmpfs_mount(void)
350361 {
351362 int err;
352363
....@@ -356,8 +367,7 @@
356367 if (!thread)
357368 return 0;
358369
359
- err = ksys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT,
360
- NULL);
370
+ err = init_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
361371 if (err)
362372 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
363373 else
....@@ -376,19 +386,8 @@
376386 return handle_remove(name, dev);
377387 }
378388
379
-static int devtmpfsd(void *p)
389
+static void __noreturn devtmpfs_work_loop(void)
380390 {
381
- char options[] = "mode=0755";
382
- int *err = p;
383
- *err = ksys_unshare(CLONE_NEWNS);
384
- if (*err)
385
- goto out;
386
- *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
387
- if (*err)
388
- goto out;
389
- ksys_chdir("/.."); /* will traverse into overmounted root */
390
- ksys_chroot(".");
391
- complete(&setup_done);
392391 while (1) {
393392 spin_lock(&req_lock);
394393 while (requests) {
....@@ -408,10 +407,39 @@
408407 spin_unlock(&req_lock);
409408 schedule();
410409 }
411
- return 0;
410
+}
411
+
412
+static int __init devtmpfs_setup(void *p)
413
+{
414
+ int err;
415
+
416
+ err = ksys_unshare(CLONE_NEWNS);
417
+ if (err)
418
+ goto out;
419
+ err = init_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
420
+ if (err)
421
+ goto out;
422
+ init_chdir("/.."); /* will traverse into overmounted root */
423
+ init_chroot(".");
412424 out:
425
+ *(int *)p = err;
426
+ return err;
427
+}
428
+
429
+/*
430
+ * The __ref is because devtmpfs_setup needs to be __init for the routines it
431
+ * calls. That call is done while devtmpfs_init, which is marked __init,
432
+ * synchronously waits for it to complete.
433
+ */
434
+static int __ref devtmpfsd(void *p)
435
+{
436
+ int err = devtmpfs_setup(p);
437
+
413438 complete(&setup_done);
414
- return *err;
439
+ if (err)
440
+ return err;
441
+ devtmpfs_work_loop();
442
+ return 0;
415443 }
416444
417445 /*
....@@ -420,7 +448,16 @@
420448 */
421449 int __init devtmpfs_init(void)
422450 {
423
- int err = register_filesystem(&dev_fs_type);
451
+ char opts[] = "mode=0755";
452
+ int err;
453
+
454
+ mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
455
+ if (IS_ERR(mnt)) {
456
+ printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
457
+ PTR_ERR(mnt));
458
+ return PTR_ERR(mnt);
459
+ }
460
+ err = register_filesystem(&dev_fs_type);
424461 if (err) {
425462 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
426463 "type %i\n", err);