1 // SPDX-License-Identifier: GPL-2.0-only
3 * inode.c - part of tracefs, a pseudo file system for activating tracing
5 * Based on debugfs by: Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2014 Red Hat Inc, author: Steven Rostedt <srostedt@redhat.com>
9 * tracefs is the file system that is used by the tracing infrastructure.
12 #include <linux/module.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>
25 #define TRACEFS_DEFAULT_MODE 0700
27 static struct vfsmount *tracefs_mount;
28 static int tracefs_mount_count;
29 static bool tracefs_registered;
31 static int default_open_file(struct inode *inode, struct file *filp)
33 struct dentry *dentry = filp->f_path.dentry;
34 struct file_operations *real_fops;
40 ret = security_locked_down(LOCKDOWN_TRACEFS);
44 real_fops = dentry->d_fsdata;
47 return real_fops->open(inode, filp);
50 static ssize_t default_read_file(struct file *file, char __user *buf,
51 size_t count, loff_t *ppos)
56 static ssize_t default_write_file(struct file *file, const char __user *buf,
57 size_t count, loff_t *ppos)
62 static const struct file_operations tracefs_file_operations = {
63 .read = default_read_file,
64 .write = default_write_file,
66 .llseek = noop_llseek,
69 static struct tracefs_dir_ops {
70 int (*mkdir)(const char *name);
71 int (*rmdir)(const char *name);
72 } tracefs_ops __ro_after_init;
74 static char *get_dname(struct dentry *dentry)
78 int len = dentry->d_name.len;
80 dname = dentry->d_name.name;
81 name = kmalloc(len + 1, GFP_KERNEL);
84 memcpy(name, dname, len);
89 static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
94 name = get_dname(dentry);
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.
104 ret = tracefs_ops.mkdir(name);
112 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
117 name = get_dname(dentry);
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.
129 inode_unlock(dentry->d_inode);
131 ret = tracefs_ops.rmdir(name);
133 inode_lock_nested(inode, I_MUTEX_PARENT);
134 inode_lock(dentry->d_inode);
141 static const struct inode_operations tracefs_dir_inode_operations = {
142 .lookup = simple_lookup,
143 .mkdir = tracefs_syscall_mkdir,
144 .rmdir = tracefs_syscall_rmdir,
147 static struct inode *tracefs_get_inode(struct super_block *sb)
149 struct inode *inode = new_inode(sb);
151 inode->i_ino = get_next_ino();
152 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
157 struct tracefs_mount_opts {
170 static const match_table_t tokens = {
173 {Opt_mode, "mode=%o"},
177 struct tracefs_fs_info {
178 struct tracefs_mount_opts mount_opts;
181 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
183 substring_t args[MAX_OPT_ARGS];
190 opts->mode = TRACEFS_DEFAULT_MODE;
192 while ((p = strsep(&data, ",")) != NULL) {
196 token = match_token(p, tokens, args);
199 if (match_int(&args[0], &option))
201 uid = make_kuid(current_user_ns(), option);
207 if (match_int(&args[0], &option))
209 gid = make_kgid(current_user_ns(), option);
215 if (match_octal(&args[0], &option))
217 opts->mode = option & S_IALLUGO;
220 * We might like to report bad mount options here;
221 * but traditionally tracefs has ignored all mount options
229 static int tracefs_apply_options(struct super_block *sb)
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;
235 inode->i_mode &= ~S_IALLUGO;
236 inode->i_mode |= opts->mode;
238 inode->i_uid = opts->uid;
239 inode->i_gid = opts->gid;
244 static void tracefs_destroy_inode(struct inode *inode)
246 if (S_ISREG(inode->i_mode))
250 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
253 struct tracefs_fs_info *fsi = sb->s_fs_info;
256 err = tracefs_parse_options(data, &fsi->mount_opts);
260 tracefs_apply_options(sb);
266 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
268 struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
269 struct tracefs_mount_opts *opts = &fsi->mount_opts;
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);
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,
290 static int trace_fill_super(struct super_block *sb, void *data, int silent)
292 static const struct tree_descr trace_files[] = {{""}};
293 struct tracefs_fs_info *fsi;
296 fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
303 err = tracefs_parse_options(data, &fsi->mount_opts);
307 err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
311 sb->s_op = &tracefs_super_operations;
313 tracefs_apply_options(sb);
319 sb->s_fs_info = NULL;
323 static struct dentry *trace_mount(struct file_system_type *fs_type,
324 int flags, const char *dev_name,
327 return mount_single(fs_type, flags, data, trace_fill_super);
330 static struct file_system_type trace_fs_type = {
331 .owner = THIS_MODULE,
333 .mount = trace_mount,
334 .kill_sb = kill_litter_super,
336 MODULE_ALIAS_FS("tracefs");
338 static struct dentry *start_creating(const char *name, struct dentry *parent)
340 struct dentry *dentry;
343 pr_debug("tracefs: creating file '%s'\n",name);
345 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
346 &tracefs_mount_count);
348 return ERR_PTR(error);
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
356 parent = tracefs_mount->mnt_root;
358 inode_lock(parent->d_inode);
359 dentry = lookup_one_len(name, parent, strlen(name));
360 if (!IS_ERR(dentry) && dentry->d_inode) {
362 dentry = ERR_PTR(-EEXIST);
365 if (IS_ERR(dentry)) {
366 inode_unlock(parent->d_inode);
367 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
373 static struct dentry *failed_creating(struct dentry *dentry)
375 inode_unlock(dentry->d_parent->d_inode);
377 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
381 static struct dentry *end_creating(struct dentry *dentry)
383 inode_unlock(dentry->d_parent->d_inode);
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
397 * @fops: a pointer to a struct file_operations that should be used for
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.)
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.
410 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
413 struct dentry *tracefs_create_file(const char *name, umode_t mode,
414 struct dentry *parent, void *data,
415 const struct file_operations *fops)
417 struct file_operations *proxy_fops;
418 struct dentry *dentry;
421 if (!(mode & S_IFMT))
423 BUG_ON(!S_ISREG(mode));
424 dentry = start_creating(name, parent);
429 inode = tracefs_get_inode(dentry->d_sb);
430 if (unlikely(!inode))
431 return failed_creating(dentry);
433 proxy_fops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
434 if (unlikely(!proxy_fops)) {
436 return failed_creating(dentry);
440 fops = &tracefs_file_operations;
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);
453 static struct dentry *__create_dir(const char *name, struct dentry *parent,
454 const struct inode_operations *ops)
456 struct dentry *dentry = start_creating(name, parent);
462 inode = tracefs_get_inode(dentry->d_sb);
463 if (unlikely(!inode))
464 return failed_creating(dentry);
466 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
468 inode->i_fop = &simple_dir_operations;
470 /* directory inodes start off with i_nlink == 2 (for "." entry) */
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);
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
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.
486 * This function creates a directory in tracefs with the given name.
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.
492 * If tracing is not enabled in the kernel, the value -%ENODEV will be
495 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
497 return __create_dir(name, parent, &simple_dir_inode_operations);
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.
507 * Only one instances directory is allowed.
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.
515 * Returns the dentry of the instances directory.
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))
522 struct dentry *dentry;
524 /* Only allow one instance of the instances directory. */
525 if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
528 dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
532 tracefs_ops.mkdir = mkdir;
533 tracefs_ops.rmdir = rmdir;
538 static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
542 if (simple_positive(dentry)) {
543 if (dentry->d_inode) {
545 switch (dentry->d_inode->i_mode & S_IFMT) {
547 ret = simple_rmdir(parent->d_inode, dentry);
549 fsnotify_rmdir(parent->d_inode, dentry);
552 simple_unlink(parent->d_inode, dentry);
553 fsnotify_unlink(parent->d_inode, dentry);
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
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.)
573 void tracefs_remove(struct dentry *dentry)
575 struct dentry *parent;
578 if (IS_ERR_OR_NULL(dentry))
581 parent = dentry->d_parent;
582 inode_lock(parent->d_inode);
583 ret = __tracefs_remove(dentry, parent);
584 inode_unlock(parent->d_inode);
586 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
590 * tracefs_remove_recursive - recursively removes a directory
591 * @dentry: a pointer to a the dentry of the directory to be removed.
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.)
597 void tracefs_remove_recursive(struct dentry *dentry)
599 struct dentry *child, *parent;
601 if (IS_ERR_OR_NULL(dentry))
606 inode_lock(parent->d_inode);
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.
613 spin_lock(&parent->d_lock);
614 list_for_each_entry(child, &parent->d_subdirs, d_child) {
615 if (!simple_positive(child))
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);
626 spin_unlock(&parent->d_lock);
628 if (!__tracefs_remove(child, parent))
629 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
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.
640 spin_unlock(&parent->d_lock);
642 inode_unlock(parent->d_inode);
644 parent = parent->d_parent;
645 inode_lock(parent->d_inode);
651 if (!__tracefs_remove(child, parent))
652 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
653 inode_unlock(parent->d_inode);
657 * tracefs_initialized - Tells whether tracefs has been registered
659 bool tracefs_initialized(void)
661 return tracefs_registered;
664 static int __init tracefs_init(void)
668 retval = sysfs_create_mount_point(kernel_kobj, "tracing");
672 retval = register_filesystem(&trace_fs_type);
674 tracefs_registered = true;
678 core_initcall(tracefs_init);