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