xref: /openbmc/linux/fs/tracefs/inode.c (revision 86db9f28)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  inode.c - part of tracefs, a pseudo file system for activating tracing
4  *
5  * Based on debugfs by: Greg Kroah-Hartman <greg@kroah.com>
6  *
7  *  Copyright (C) 2014 Red Hat Inc, author: Steven Rostedt <srostedt@redhat.com>
8  *
9  * tracefs is the file system that is used by the tracing infrastructure.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/kobject.h>
16 #include <linux/namei.h>
17 #include <linux/tracefs.h>
18 #include <linux/fsnotify.h>
19 #include <linux/seq_file.h>
20 #include <linux/parser.h>
21 #include <linux/magic.h>
22 #include <linux/slab.h>
23 #include <linux/security.h>
24 
25 #define TRACEFS_DEFAULT_MODE	0700
26 
27 static struct vfsmount *tracefs_mount;
28 static int tracefs_mount_count;
29 static bool tracefs_registered;
30 
31 static int default_open_file(struct inode *inode, struct file *filp)
32 {
33 	struct dentry *dentry = filp->f_path.dentry;
34 	struct file_operations *real_fops;
35 	int ret;
36 
37 	if (!dentry)
38 		return -EINVAL;
39 
40 	ret = security_locked_down(LOCKDOWN_TRACEFS);
41 	if (ret)
42 		return ret;
43 
44 	real_fops = dentry->d_fsdata;
45 	if (!real_fops->open)
46 		return 0;
47 	return real_fops->open(inode, filp);
48 }
49 
50 static ssize_t default_read_file(struct file *file, char __user *buf,
51 				 size_t count, loff_t *ppos)
52 {
53 	return 0;
54 }
55 
56 static ssize_t default_write_file(struct file *file, const char __user *buf,
57 				   size_t count, loff_t *ppos)
58 {
59 	return count;
60 }
61 
62 static const struct file_operations tracefs_file_operations = {
63 	.read =		default_read_file,
64 	.write =	default_write_file,
65 	.open =		simple_open,
66 	.llseek =	noop_llseek,
67 };
68 
69 static struct tracefs_dir_ops {
70 	int (*mkdir)(const char *name);
71 	int (*rmdir)(const char *name);
72 } tracefs_ops __ro_after_init;
73 
74 static char *get_dname(struct dentry *dentry)
75 {
76 	const char *dname;
77 	char *name;
78 	int len = dentry->d_name.len;
79 
80 	dname = dentry->d_name.name;
81 	name = kmalloc(len + 1, GFP_KERNEL);
82 	if (!name)
83 		return NULL;
84 	memcpy(name, dname, len);
85 	name[len] = 0;
86 	return name;
87 }
88 
89 static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
90 {
91 	char *name;
92 	int ret;
93 
94 	name = get_dname(dentry);
95 	if (!name)
96 		return -ENOMEM;
97 
98 	/*
99 	 * The mkdir call can call the generic functions that create
100 	 * the files within the tracefs system. It is up to the individual
101 	 * mkdir routine to handle races.
102 	 */
103 	inode_unlock(inode);
104 	ret = tracefs_ops.mkdir(name);
105 	inode_lock(inode);
106 
107 	kfree(name);
108 
109 	return ret;
110 }
111 
112 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
113 {
114 	char *name;
115 	int ret;
116 
117 	name = get_dname(dentry);
118 	if (!name)
119 		return -ENOMEM;
120 
121 	/*
122 	 * The rmdir call can call the generic functions that create
123 	 * the files within the tracefs system. It is up to the individual
124 	 * rmdir routine to handle races.
125 	 * This time we need to unlock not only the parent (inode) but
126 	 * also the directory that is being deleted.
127 	 */
128 	inode_unlock(inode);
129 	inode_unlock(dentry->d_inode);
130 
131 	ret = tracefs_ops.rmdir(name);
132 
133 	inode_lock_nested(inode, I_MUTEX_PARENT);
134 	inode_lock(dentry->d_inode);
135 
136 	kfree(name);
137 
138 	return ret;
139 }
140 
141 static const struct inode_operations tracefs_dir_inode_operations = {
142 	.lookup		= simple_lookup,
143 	.mkdir		= tracefs_syscall_mkdir,
144 	.rmdir		= tracefs_syscall_rmdir,
145 };
146 
147 static struct inode *tracefs_get_inode(struct super_block *sb)
148 {
149 	struct inode *inode = new_inode(sb);
150 	if (inode) {
151 		inode->i_ino = get_next_ino();
152 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
153 	}
154 	return inode;
155 }
156 
157 struct tracefs_mount_opts {
158 	kuid_t uid;
159 	kgid_t gid;
160 	umode_t mode;
161 };
162 
163 enum {
164 	Opt_uid,
165 	Opt_gid,
166 	Opt_mode,
167 	Opt_err
168 };
169 
170 static const match_table_t tokens = {
171 	{Opt_uid, "uid=%u"},
172 	{Opt_gid, "gid=%u"},
173 	{Opt_mode, "mode=%o"},
174 	{Opt_err, NULL}
175 };
176 
177 struct tracefs_fs_info {
178 	struct tracefs_mount_opts mount_opts;
179 };
180 
181 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
182 {
183 	substring_t args[MAX_OPT_ARGS];
184 	int option;
185 	int token;
186 	kuid_t uid;
187 	kgid_t gid;
188 	char *p;
189 
190 	opts->mode = TRACEFS_DEFAULT_MODE;
191 
192 	while ((p = strsep(&data, ",")) != NULL) {
193 		if (!*p)
194 			continue;
195 
196 		token = match_token(p, tokens, args);
197 		switch (token) {
198 		case Opt_uid:
199 			if (match_int(&args[0], &option))
200 				return -EINVAL;
201 			uid = make_kuid(current_user_ns(), option);
202 			if (!uid_valid(uid))
203 				return -EINVAL;
204 			opts->uid = uid;
205 			break;
206 		case Opt_gid:
207 			if (match_int(&args[0], &option))
208 				return -EINVAL;
209 			gid = make_kgid(current_user_ns(), option);
210 			if (!gid_valid(gid))
211 				return -EINVAL;
212 			opts->gid = gid;
213 			break;
214 		case Opt_mode:
215 			if (match_octal(&args[0], &option))
216 				return -EINVAL;
217 			opts->mode = option & S_IALLUGO;
218 			break;
219 		/*
220 		 * We might like to report bad mount options here;
221 		 * but traditionally tracefs has ignored all mount options
222 		 */
223 		}
224 	}
225 
226 	return 0;
227 }
228 
229 static int tracefs_apply_options(struct super_block *sb)
230 {
231 	struct tracefs_fs_info *fsi = sb->s_fs_info;
232 	struct inode *inode = sb->s_root->d_inode;
233 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
234 
235 	inode->i_mode &= ~S_IALLUGO;
236 	inode->i_mode |= opts->mode;
237 
238 	inode->i_uid = opts->uid;
239 	inode->i_gid = opts->gid;
240 
241 	return 0;
242 }
243 
244 static void tracefs_destroy_inode(struct inode *inode)
245 {
246 	if (S_ISREG(inode->i_mode))
247 		kfree(inode->i_fop);
248 }
249 
250 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
251 {
252 	int err;
253 	struct tracefs_fs_info *fsi = sb->s_fs_info;
254 
255 	sync_filesystem(sb);
256 	err = tracefs_parse_options(data, &fsi->mount_opts);
257 	if (err)
258 		goto fail;
259 
260 	tracefs_apply_options(sb);
261 
262 fail:
263 	return err;
264 }
265 
266 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
267 {
268 	struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
269 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
270 
271 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
272 		seq_printf(m, ",uid=%u",
273 			   from_kuid_munged(&init_user_ns, opts->uid));
274 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
275 		seq_printf(m, ",gid=%u",
276 			   from_kgid_munged(&init_user_ns, opts->gid));
277 	if (opts->mode != TRACEFS_DEFAULT_MODE)
278 		seq_printf(m, ",mode=%o", opts->mode);
279 
280 	return 0;
281 }
282 
283 static const struct super_operations tracefs_super_operations = {
284 	.statfs		= simple_statfs,
285 	.remount_fs	= tracefs_remount,
286 	.destroy_inode  = tracefs_destroy_inode,
287 	.show_options	= tracefs_show_options,
288 };
289 
290 static int trace_fill_super(struct super_block *sb, void *data, int silent)
291 {
292 	static const struct tree_descr trace_files[] = {{""}};
293 	struct tracefs_fs_info *fsi;
294 	int err;
295 
296 	fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
297 	sb->s_fs_info = fsi;
298 	if (!fsi) {
299 		err = -ENOMEM;
300 		goto fail;
301 	}
302 
303 	err = tracefs_parse_options(data, &fsi->mount_opts);
304 	if (err)
305 		goto fail;
306 
307 	err  =  simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
308 	if (err)
309 		goto fail;
310 
311 	sb->s_op = &tracefs_super_operations;
312 
313 	tracefs_apply_options(sb);
314 
315 	return 0;
316 
317 fail:
318 	kfree(fsi);
319 	sb->s_fs_info = NULL;
320 	return err;
321 }
322 
323 static struct dentry *trace_mount(struct file_system_type *fs_type,
324 			int flags, const char *dev_name,
325 			void *data)
326 {
327 	return mount_single(fs_type, flags, data, trace_fill_super);
328 }
329 
330 static struct file_system_type trace_fs_type = {
331 	.owner =	THIS_MODULE,
332 	.name =		"tracefs",
333 	.mount =	trace_mount,
334 	.kill_sb =	kill_litter_super,
335 };
336 MODULE_ALIAS_FS("tracefs");
337 
338 static struct dentry *start_creating(const char *name, struct dentry *parent)
339 {
340 	struct dentry *dentry;
341 	int error;
342 
343 	pr_debug("tracefs: creating file '%s'\n",name);
344 
345 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
346 			      &tracefs_mount_count);
347 	if (error)
348 		return ERR_PTR(error);
349 
350 	/* If the parent is not specified, we create it in the root.
351 	 * We need the root dentry to do this, which is in the super
352 	 * block. A pointer to that is in the struct vfsmount that we
353 	 * have around.
354 	 */
355 	if (!parent)
356 		parent = tracefs_mount->mnt_root;
357 
358 	inode_lock(parent->d_inode);
359 	dentry = lookup_one_len(name, parent, strlen(name));
360 	if (!IS_ERR(dentry) && dentry->d_inode) {
361 		dput(dentry);
362 		dentry = ERR_PTR(-EEXIST);
363 	}
364 
365 	if (IS_ERR(dentry)) {
366 		inode_unlock(parent->d_inode);
367 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
368 	}
369 
370 	return dentry;
371 }
372 
373 static struct dentry *failed_creating(struct dentry *dentry)
374 {
375 	inode_unlock(dentry->d_parent->d_inode);
376 	dput(dentry);
377 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
378 	return NULL;
379 }
380 
381 static struct dentry *end_creating(struct dentry *dentry)
382 {
383 	inode_unlock(dentry->d_parent->d_inode);
384 	return dentry;
385 }
386 
387 /**
388  * tracefs_create_file - create a file in the tracefs filesystem
389  * @name: a pointer to a string containing the name of the file to create.
390  * @mode: the permission that the file should have.
391  * @parent: a pointer to the parent dentry for this file.  This should be a
392  *          directory dentry if set.  If this parameter is NULL, then the
393  *          file will be created in the root of the tracefs filesystem.
394  * @data: a pointer to something that the caller will want to get to later
395  *        on.  The inode.i_private pointer will point to this value on
396  *        the open() call.
397  * @fops: a pointer to a struct file_operations that should be used for
398  *        this file.
399  *
400  * This is the basic "create a file" function for tracefs.  It allows for a
401  * wide range of flexibility in creating a file, or a directory (if you want
402  * to create a directory, the tracefs_create_dir() function is
403  * recommended to be used instead.)
404  *
405  * This function will return a pointer to a dentry if it succeeds.  This
406  * pointer must be passed to the tracefs_remove() function when the file is
407  * to be removed (no automatic cleanup happens if your module is unloaded,
408  * you are responsible here.)  If an error occurs, %NULL will be returned.
409  *
410  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
411  * returned.
412  */
413 struct dentry *tracefs_create_file(const char *name, umode_t mode,
414 				   struct dentry *parent, void *data,
415 				   const struct file_operations *fops)
416 {
417 	struct file_operations *proxy_fops;
418 	struct dentry *dentry;
419 	struct inode *inode;
420 
421 	if (!(mode & S_IFMT))
422 		mode |= S_IFREG;
423 	BUG_ON(!S_ISREG(mode));
424 	dentry = start_creating(name, parent);
425 
426 	if (IS_ERR(dentry))
427 		return NULL;
428 
429 	inode = tracefs_get_inode(dentry->d_sb);
430 	if (unlikely(!inode))
431 		return failed_creating(dentry);
432 
433 	proxy_fops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
434 	if (unlikely(!proxy_fops)) {
435 		iput(inode);
436 		return failed_creating(dentry);
437 	}
438 
439 	if (!fops)
440 		fops = &tracefs_file_operations;
441 
442 	dentry->d_fsdata = (void *)fops;
443 	memcpy(proxy_fops, fops, sizeof(*proxy_fops));
444 	proxy_fops->open = default_open_file;
445 	inode->i_mode = mode;
446 	inode->i_fop = proxy_fops;
447 	inode->i_private = data;
448 	d_instantiate(dentry, inode);
449 	fsnotify_create(dentry->d_parent->d_inode, dentry);
450 	return end_creating(dentry);
451 }
452 
453 static struct dentry *__create_dir(const char *name, struct dentry *parent,
454 				   const struct inode_operations *ops)
455 {
456 	struct dentry *dentry = start_creating(name, parent);
457 	struct inode *inode;
458 
459 	if (IS_ERR(dentry))
460 		return NULL;
461 
462 	inode = tracefs_get_inode(dentry->d_sb);
463 	if (unlikely(!inode))
464 		return failed_creating(dentry);
465 
466 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
467 	inode->i_op = ops;
468 	inode->i_fop = &simple_dir_operations;
469 
470 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
471 	inc_nlink(inode);
472 	d_instantiate(dentry, inode);
473 	inc_nlink(dentry->d_parent->d_inode);
474 	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
475 	return end_creating(dentry);
476 }
477 
478 /**
479  * tracefs_create_dir - create a directory in the tracefs filesystem
480  * @name: a pointer to a string containing the name of the directory to
481  *        create.
482  * @parent: a pointer to the parent dentry for this file.  This should be a
483  *          directory dentry if set.  If this parameter is NULL, then the
484  *          directory will be created in the root of the tracefs filesystem.
485  *
486  * This function creates a directory in tracefs with the given name.
487  *
488  * This function will return a pointer to a dentry if it succeeds.  This
489  * pointer must be passed to the tracefs_remove() function when the file is
490  * to be removed. If an error occurs, %NULL will be returned.
491  *
492  * If tracing is not enabled in the kernel, the value -%ENODEV will be
493  * returned.
494  */
495 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
496 {
497 	return __create_dir(name, parent, &simple_dir_inode_operations);
498 }
499 
500 /**
501  * tracefs_create_instance_dir - create the tracing instances directory
502  * @name: The name of the instances directory to create
503  * @parent: The parent directory that the instances directory will exist
504  * @mkdir: The function to call when a mkdir is performed.
505  * @rmdir: The function to call when a rmdir is performed.
506  *
507  * Only one instances directory is allowed.
508  *
509  * The instances directory is special as it allows for mkdir and rmdir to
510  * to be done by userspace. When a mkdir or rmdir is performed, the inode
511  * locks are released and the methhods passed in (@mkdir and @rmdir) are
512  * called without locks and with the name of the directory being created
513  * within the instances directory.
514  *
515  * Returns the dentry of the instances directory.
516  */
517 __init struct dentry *tracefs_create_instance_dir(const char *name,
518 					  struct dentry *parent,
519 					  int (*mkdir)(const char *name),
520 					  int (*rmdir)(const char *name))
521 {
522 	struct dentry *dentry;
523 
524 	/* Only allow one instance of the instances directory. */
525 	if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
526 		return NULL;
527 
528 	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
529 	if (!dentry)
530 		return NULL;
531 
532 	tracefs_ops.mkdir = mkdir;
533 	tracefs_ops.rmdir = rmdir;
534 
535 	return dentry;
536 }
537 
538 static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
539 {
540 	int ret = 0;
541 
542 	if (simple_positive(dentry)) {
543 		if (dentry->d_inode) {
544 			dget(dentry);
545 			switch (dentry->d_inode->i_mode & S_IFMT) {
546 			case S_IFDIR:
547 				ret = simple_rmdir(parent->d_inode, dentry);
548 				if (!ret)
549 					fsnotify_rmdir(parent->d_inode, dentry);
550 				break;
551 			default:
552 				simple_unlink(parent->d_inode, dentry);
553 				fsnotify_unlink(parent->d_inode, dentry);
554 				break;
555 			}
556 			if (!ret)
557 				d_delete(dentry);
558 			dput(dentry);
559 		}
560 	}
561 	return ret;
562 }
563 
564 /**
565  * tracefs_remove - removes a file or directory from the tracefs filesystem
566  * @dentry: a pointer to a the dentry of the file or directory to be
567  *          removed.
568  *
569  * This function removes a file or directory in tracefs that was previously
570  * created with a call to another tracefs function (like
571  * tracefs_create_file() or variants thereof.)
572  */
573 void tracefs_remove(struct dentry *dentry)
574 {
575 	struct dentry *parent;
576 	int ret;
577 
578 	if (IS_ERR_OR_NULL(dentry))
579 		return;
580 
581 	parent = dentry->d_parent;
582 	inode_lock(parent->d_inode);
583 	ret = __tracefs_remove(dentry, parent);
584 	inode_unlock(parent->d_inode);
585 	if (!ret)
586 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
587 }
588 
589 /**
590  * tracefs_remove_recursive - recursively removes a directory
591  * @dentry: a pointer to a the dentry of the directory to be removed.
592  *
593  * This function recursively removes a directory tree in tracefs that
594  * was previously created with a call to another tracefs function
595  * (like tracefs_create_file() or variants thereof.)
596  */
597 void tracefs_remove_recursive(struct dentry *dentry)
598 {
599 	struct dentry *child, *parent;
600 
601 	if (IS_ERR_OR_NULL(dentry))
602 		return;
603 
604 	parent = dentry;
605  down:
606 	inode_lock(parent->d_inode);
607  loop:
608 	/*
609 	 * The parent->d_subdirs is protected by the d_lock. Outside that
610 	 * lock, the child can be unlinked and set to be freed which can
611 	 * use the d_u.d_child as the rcu head and corrupt this list.
612 	 */
613 	spin_lock(&parent->d_lock);
614 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
615 		if (!simple_positive(child))
616 			continue;
617 
618 		/* perhaps simple_empty(child) makes more sense */
619 		if (!list_empty(&child->d_subdirs)) {
620 			spin_unlock(&parent->d_lock);
621 			inode_unlock(parent->d_inode);
622 			parent = child;
623 			goto down;
624 		}
625 
626 		spin_unlock(&parent->d_lock);
627 
628 		if (!__tracefs_remove(child, parent))
629 			simple_release_fs(&tracefs_mount, &tracefs_mount_count);
630 
631 		/*
632 		 * The parent->d_lock protects agaist child from unlinking
633 		 * from d_subdirs. When releasing the parent->d_lock we can
634 		 * no longer trust that the next pointer is valid.
635 		 * Restart the loop. We'll skip this one with the
636 		 * simple_positive() check.
637 		 */
638 		goto loop;
639 	}
640 	spin_unlock(&parent->d_lock);
641 
642 	inode_unlock(parent->d_inode);
643 	child = parent;
644 	parent = parent->d_parent;
645 	inode_lock(parent->d_inode);
646 
647 	if (child != dentry)
648 		/* go up */
649 		goto loop;
650 
651 	if (!__tracefs_remove(child, parent))
652 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
653 	inode_unlock(parent->d_inode);
654 }
655 
656 /**
657  * tracefs_initialized - Tells whether tracefs has been registered
658  */
659 bool tracefs_initialized(void)
660 {
661 	return tracefs_registered;
662 }
663 
664 static int __init tracefs_init(void)
665 {
666 	int retval;
667 
668 	retval = sysfs_create_mount_point(kernel_kobj, "tracing");
669 	if (retval)
670 		return -EINVAL;
671 
672 	retval = register_filesystem(&trace_fs_type);
673 	if (!retval)
674 		tracefs_registered = true;
675 
676 	return retval;
677 }
678 core_initcall(tracefs_init);
679