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