GNU Linux-libre 5.4.241-gnu1
[releases.git] / drivers / base / devtmpfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * devtmpfs - kernel-maintained tmpfs-based /dev
4  *
5  * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
6  *
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
10  * filesystem.
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.
14  */
15
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>
22 #include <linux/fs.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 <linux/fs_context.h>
29 #include <uapi/linux/mount.h>
30 #include "base.h"
31
32 static struct task_struct *thread;
33
34 #if defined CONFIG_DEVTMPFS_MOUNT
35 static int mount_dev = 1;
36 #else
37 static int mount_dev;
38 #endif
39
40 static DEFINE_SPINLOCK(req_lock);
41
42 static struct req {
43         struct req *next;
44         struct completion done;
45         int err;
46         const char *name;
47         umode_t mode;   /* 0 => delete */
48         kuid_t uid;
49         kgid_t gid;
50         struct device *dev;
51 } *requests;
52
53 static int __init mount_param(char *str)
54 {
55         mount_dev = simple_strtoul(str, NULL, 0);
56         return 1;
57 }
58 __setup("devtmpfs.mount=", mount_param);
59
60 static struct vfsmount *mnt;
61
62 static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
63                       const char *dev_name, void *data)
64 {
65         struct super_block *s = mnt->mnt_sb;
66         int err;
67
68         atomic_inc(&s->s_active);
69         down_write(&s->s_umount);
70         err = reconfigure_single(s, flags, data);
71         if (err < 0) {
72                 deactivate_locked_super(s);
73                 return ERR_PTR(err);
74         }
75         return dget(s->s_root);
76 }
77
78 static struct file_system_type internal_fs_type = {
79         .name = "devtmpfs",
80 #ifdef CONFIG_TMPFS
81         .init_fs_context = shmem_init_fs_context,
82         .parameters     = &shmem_fs_parameters,
83 #else
84         .init_fs_context = ramfs_init_fs_context,
85         .parameters     = &ramfs_fs_parameters,
86 #endif
87         .kill_sb = kill_litter_super,
88 };
89
90 static struct file_system_type dev_fs_type = {
91         .name = "devtmpfs",
92         .mount = public_dev_mount,
93 };
94
95 #ifdef CONFIG_BLOCK
96 static inline int is_blockdev(struct device *dev)
97 {
98         return dev->class == &block_class;
99 }
100 #else
101 static inline int is_blockdev(struct device *dev) { return 0; }
102 #endif
103
104 int devtmpfs_create_node(struct device *dev)
105 {
106         const char *tmp = NULL;
107         struct req req;
108
109         if (!thread)
110                 return 0;
111
112         req.mode = 0;
113         req.uid = GLOBAL_ROOT_UID;
114         req.gid = GLOBAL_ROOT_GID;
115         req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
116         if (!req.name)
117                 return -ENOMEM;
118
119         if (req.mode == 0)
120                 req.mode = 0600;
121         if (is_blockdev(dev))
122                 req.mode |= S_IFBLK;
123         else
124                 req.mode |= S_IFCHR;
125
126         req.dev = dev;
127
128         init_completion(&req.done);
129
130         spin_lock(&req_lock);
131         req.next = requests;
132         requests = &req;
133         spin_unlock(&req_lock);
134
135         wake_up_process(thread);
136         wait_for_completion(&req.done);
137
138         kfree(tmp);
139
140         return req.err;
141 }
142
143 int devtmpfs_delete_node(struct device *dev)
144 {
145         const char *tmp = NULL;
146         struct req req;
147
148         if (!thread)
149                 return 0;
150
151         req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
152         if (!req.name)
153                 return -ENOMEM;
154
155         req.mode = 0;
156         req.dev = dev;
157
158         init_completion(&req.done);
159
160         spin_lock(&req_lock);
161         req.next = requests;
162         requests = &req;
163         spin_unlock(&req_lock);
164
165         wake_up_process(thread);
166         wait_for_completion(&req.done);
167
168         kfree(tmp);
169         return req.err;
170 }
171
172 static int dev_mkdir(const char *name, umode_t mode)
173 {
174         struct dentry *dentry;
175         struct path path;
176         int err;
177
178         dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
179         if (IS_ERR(dentry))
180                 return PTR_ERR(dentry);
181
182         err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
183         if (!err)
184                 /* mark as kernel-created inode */
185                 d_inode(dentry)->i_private = &thread;
186         done_path_create(&path, dentry);
187         return err;
188 }
189
190 static int create_path(const char *nodepath)
191 {
192         char *path;
193         char *s;
194         int err = 0;
195
196         /* parent directories do not exist, create them */
197         path = kstrdup(nodepath, GFP_KERNEL);
198         if (!path)
199                 return -ENOMEM;
200
201         s = path;
202         for (;;) {
203                 s = strchr(s, '/');
204                 if (!s)
205                         break;
206                 s[0] = '\0';
207                 err = dev_mkdir(path, 0755);
208                 if (err && err != -EEXIST)
209                         break;
210                 s[0] = '/';
211                 s++;
212         }
213         kfree(path);
214         return err;
215 }
216
217 static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
218                          kgid_t gid, struct device *dev)
219 {
220         struct dentry *dentry;
221         struct path path;
222         int err;
223
224         dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
225         if (dentry == ERR_PTR(-ENOENT)) {
226                 create_path(nodename);
227                 dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
228         }
229         if (IS_ERR(dentry))
230                 return PTR_ERR(dentry);
231
232         err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt);
233         if (!err) {
234                 struct iattr newattrs;
235
236                 newattrs.ia_mode = mode;
237                 newattrs.ia_uid = uid;
238                 newattrs.ia_gid = gid;
239                 newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
240                 inode_lock(d_inode(dentry));
241                 notify_change(dentry, &newattrs, NULL);
242                 inode_unlock(d_inode(dentry));
243
244                 /* mark as kernel-created inode */
245                 d_inode(dentry)->i_private = &thread;
246         }
247         done_path_create(&path, dentry);
248         return err;
249 }
250
251 static int dev_rmdir(const char *name)
252 {
253         struct path parent;
254         struct dentry *dentry;
255         int err;
256
257         dentry = kern_path_locked(name, &parent);
258         if (IS_ERR(dentry))
259                 return PTR_ERR(dentry);
260         if (d_really_is_positive(dentry)) {
261                 if (d_inode(dentry)->i_private == &thread)
262                         err = vfs_rmdir(d_inode(parent.dentry), dentry);
263                 else
264                         err = -EPERM;
265         } else {
266                 err = -ENOENT;
267         }
268         dput(dentry);
269         inode_unlock(d_inode(parent.dentry));
270         path_put(&parent);
271         return err;
272 }
273
274 static int delete_path(const char *nodepath)
275 {
276         char *path;
277         int err = 0;
278
279         path = kstrdup(nodepath, GFP_KERNEL);
280         if (!path)
281                 return -ENOMEM;
282
283         for (;;) {
284                 char *base;
285
286                 base = strrchr(path, '/');
287                 if (!base)
288                         break;
289                 base[0] = '\0';
290                 err = dev_rmdir(path);
291                 if (err)
292                         break;
293         }
294
295         kfree(path);
296         return err;
297 }
298
299 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
300 {
301         /* did we create it */
302         if (inode->i_private != &thread)
303                 return 0;
304
305         /* does the dev_t match */
306         if (is_blockdev(dev)) {
307                 if (!S_ISBLK(stat->mode))
308                         return 0;
309         } else {
310                 if (!S_ISCHR(stat->mode))
311                         return 0;
312         }
313         if (stat->rdev != dev->devt)
314                 return 0;
315
316         /* ours */
317         return 1;
318 }
319
320 static int handle_remove(const char *nodename, struct device *dev)
321 {
322         struct path parent;
323         struct dentry *dentry;
324         int deleted = 0;
325         int err;
326
327         dentry = kern_path_locked(nodename, &parent);
328         if (IS_ERR(dentry))
329                 return PTR_ERR(dentry);
330
331         if (d_really_is_positive(dentry)) {
332                 struct kstat stat;
333                 struct path p = {.mnt = parent.mnt, .dentry = dentry};
334                 err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
335                                   AT_STATX_SYNC_AS_STAT);
336                 if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
337                         struct iattr newattrs;
338                         /*
339                          * before unlinking this node, reset permissions
340                          * of possible references like hardlinks
341                          */
342                         newattrs.ia_uid = GLOBAL_ROOT_UID;
343                         newattrs.ia_gid = GLOBAL_ROOT_GID;
344                         newattrs.ia_mode = stat.mode & ~0777;
345                         newattrs.ia_valid =
346                                 ATTR_UID|ATTR_GID|ATTR_MODE;
347                         inode_lock(d_inode(dentry));
348                         notify_change(dentry, &newattrs, NULL);
349                         inode_unlock(d_inode(dentry));
350                         err = vfs_unlink(d_inode(parent.dentry), dentry, NULL);
351                         if (!err || err == -ENOENT)
352                                 deleted = 1;
353                 }
354         } else {
355                 err = -ENOENT;
356         }
357         dput(dentry);
358         inode_unlock(d_inode(parent.dentry));
359
360         path_put(&parent);
361         if (deleted && strchr(nodename, '/'))
362                 delete_path(nodename);
363         return err;
364 }
365
366 /*
367  * If configured, or requested by the commandline, devtmpfs will be
368  * auto-mounted after the kernel mounted the root filesystem.
369  */
370 int devtmpfs_mount(const char *mntdir)
371 {
372         int err;
373
374         if (!mount_dev)
375                 return 0;
376
377         if (!thread)
378                 return 0;
379
380         err = ksys_mount("devtmpfs", mntdir, "devtmpfs", MS_SILENT, NULL);
381         if (err)
382                 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
383         else
384                 printk(KERN_INFO "devtmpfs: mounted\n");
385         return err;
386 }
387
388 static DECLARE_COMPLETION(setup_done);
389
390 static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
391                   struct device *dev)
392 {
393         if (mode)
394                 return handle_create(name, mode, uid, gid, dev);
395         else
396                 return handle_remove(name, dev);
397 }
398
399 static int devtmpfsd(void *p)
400 {
401         int *err = p;
402         *err = ksys_unshare(CLONE_NEWNS);
403         if (*err)
404                 goto out;
405         *err = ksys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
406         if (*err)
407                 goto out;
408         ksys_chdir("/.."); /* will traverse into overmounted root */
409         ksys_chroot(".");
410         complete(&setup_done);
411         while (1) {
412                 spin_lock(&req_lock);
413                 while (requests) {
414                         struct req *req = requests;
415                         requests = NULL;
416                         spin_unlock(&req_lock);
417                         while (req) {
418                                 struct req *next = req->next;
419                                 req->err = handle(req->name, req->mode,
420                                                   req->uid, req->gid, req->dev);
421                                 complete(&req->done);
422                                 req = next;
423                         }
424                         spin_lock(&req_lock);
425                 }
426                 __set_current_state(TASK_INTERRUPTIBLE);
427                 spin_unlock(&req_lock);
428                 schedule();
429         }
430         return 0;
431 out:
432         complete(&setup_done);
433         return *err;
434 }
435
436 /*
437  * Create devtmpfs instance, driver-core devices will add their device
438  * nodes here.
439  */
440 int __init devtmpfs_init(void)
441 {
442         char opts[] = "mode=0755";
443         int err;
444
445         mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
446         if (IS_ERR(mnt)) {
447                 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
448                                 PTR_ERR(mnt));
449                 return PTR_ERR(mnt);
450         }
451         err = register_filesystem(&dev_fs_type);
452         if (err) {
453                 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
454                        "type %i\n", err);
455                 return err;
456         }
457
458         thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
459         if (!IS_ERR(thread)) {
460                 wait_for_completion(&setup_done);
461         } else {
462                 err = PTR_ERR(thread);
463                 thread = NULL;
464         }
465
466         if (err) {
467                 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
468                 unregister_filesystem(&dev_fs_type);
469                 return err;
470         }
471
472         printk(KERN_INFO "devtmpfs: initialized\n");
473         return 0;
474 }