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/security.h>
20 #include <linux/seq_file.h>
21 #include <linux/parser.h>
22 #include <linux/magic.h>
23 #include <linux/slab.h>
26 #define TRACEFS_DEFAULT_MODE 0700
27 static struct kmem_cache *tracefs_inode_cachep __ro_after_init;
29 static struct vfsmount *tracefs_mount;
30 static int tracefs_mount_count;
31 static bool tracefs_registered;
33 static struct inode *tracefs_alloc_inode(struct super_block *sb)
35 struct tracefs_inode *ti;
37 ti = kmem_cache_alloc(tracefs_inode_cachep, GFP_KERNEL);
43 return &ti->vfs_inode;
46 static void tracefs_free_inode(struct inode *inode)
48 kmem_cache_free(tracefs_inode_cachep, get_tracefs(inode));
51 static ssize_t default_read_file(struct file *file, char __user *buf,
52 size_t count, loff_t *ppos)
57 static ssize_t default_write_file(struct file *file, const char __user *buf,
58 size_t count, loff_t *ppos)
63 static const struct file_operations tracefs_file_operations = {
64 .read = default_read_file,
65 .write = default_write_file,
67 .llseek = noop_llseek,
70 static struct tracefs_dir_ops {
71 int (*mkdir)(const char *name);
72 int (*rmdir)(const char *name);
73 } tracefs_ops __ro_after_init;
75 static char *get_dname(struct dentry *dentry)
79 int len = dentry->d_name.len;
81 dname = dentry->d_name.name;
82 name = kmalloc(len + 1, GFP_KERNEL);
85 memcpy(name, dname, len);
90 static int tracefs_syscall_mkdir(struct mnt_idmap *idmap,
91 struct inode *inode, struct dentry *dentry,
97 name = get_dname(dentry);
102 * The mkdir call can call the generic functions that create
103 * the files within the tracefs system. It is up to the individual
104 * mkdir routine to handle races.
107 ret = tracefs_ops.mkdir(name);
115 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
120 name = get_dname(dentry);
125 * The rmdir call can call the generic functions that create
126 * the files within the tracefs system. It is up to the individual
127 * rmdir routine to handle races.
128 * This time we need to unlock not only the parent (inode) but
129 * also the directory that is being deleted.
132 inode_unlock(d_inode(dentry));
134 ret = tracefs_ops.rmdir(name);
136 inode_lock_nested(inode, I_MUTEX_PARENT);
137 inode_lock(d_inode(dentry));
144 static const struct inode_operations tracefs_dir_inode_operations = {
145 .lookup = simple_lookup,
146 .mkdir = tracefs_syscall_mkdir,
147 .rmdir = tracefs_syscall_rmdir,
150 struct inode *tracefs_get_inode(struct super_block *sb)
152 struct inode *inode = new_inode(sb);
154 inode->i_ino = get_next_ino();
155 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
160 struct tracefs_mount_opts {
164 /* Opt_* bitfield. */
175 static const match_table_t tokens = {
178 {Opt_mode, "mode=%o"},
182 struct tracefs_fs_info {
183 struct tracefs_mount_opts mount_opts;
186 static void change_gid(struct dentry *dentry, kgid_t gid)
188 if (!dentry->d_inode)
190 dentry->d_inode->i_gid = gid;
194 * Taken from d_walk, but without he need for handling renames.
195 * Nothing can be renamed while walking the list, as tracefs
196 * does not support renames. This is only called when mounting
197 * or remounting the file system, to set all the files to
200 static void set_gid(struct dentry *parent, kgid_t gid)
202 struct dentry *this_parent;
203 struct list_head *next;
205 this_parent = parent;
206 spin_lock(&this_parent->d_lock);
208 change_gid(this_parent, gid);
210 next = this_parent->d_subdirs.next;
212 while (next != &this_parent->d_subdirs) {
213 struct list_head *tmp = next;
214 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
217 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
219 change_gid(dentry, gid);
221 if (!list_empty(&dentry->d_subdirs)) {
222 spin_unlock(&this_parent->d_lock);
223 spin_release(&dentry->d_lock.dep_map, _RET_IP_);
224 this_parent = dentry;
225 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
228 spin_unlock(&dentry->d_lock);
231 * All done at this level ... ascend and resume the search.
235 if (this_parent != parent) {
236 struct dentry *child = this_parent;
237 this_parent = child->d_parent;
239 spin_unlock(&child->d_lock);
240 spin_lock(&this_parent->d_lock);
242 /* go into the first sibling still alive */
244 next = child->d_child.next;
245 if (next == &this_parent->d_subdirs)
247 child = list_entry(next, struct dentry, d_child);
248 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
253 spin_unlock(&this_parent->d_lock);
257 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
259 substring_t args[MAX_OPT_ARGS];
267 opts->mode = TRACEFS_DEFAULT_MODE;
269 while ((p = strsep(&data, ",")) != NULL) {
273 token = match_token(p, tokens, args);
276 if (match_int(&args[0], &option))
278 uid = make_kuid(current_user_ns(), option);
284 if (match_int(&args[0], &option))
286 gid = make_kgid(current_user_ns(), option);
292 if (match_octal(&args[0], &option))
294 opts->mode = option & S_IALLUGO;
297 * We might like to report bad mount options here;
298 * but traditionally tracefs has ignored all mount options
302 opts->opts |= BIT(token);
308 static int tracefs_apply_options(struct super_block *sb, bool remount)
310 struct tracefs_fs_info *fsi = sb->s_fs_info;
311 struct inode *inode = d_inode(sb->s_root);
312 struct tracefs_mount_opts *opts = &fsi->mount_opts;
316 * On remount, only reset mode/uid/gid if they were provided as mount
320 if (!remount || opts->opts & BIT(Opt_mode)) {
321 tmp_mode = READ_ONCE(inode->i_mode) & ~S_IALLUGO;
322 tmp_mode |= opts->mode;
323 WRITE_ONCE(inode->i_mode, tmp_mode);
326 if (!remount || opts->opts & BIT(Opt_uid))
327 inode->i_uid = opts->uid;
329 if (!remount || opts->opts & BIT(Opt_gid)) {
330 /* Set all the group ids to the mount option */
331 set_gid(sb->s_root, opts->gid);
337 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
340 struct tracefs_fs_info *fsi = sb->s_fs_info;
343 err = tracefs_parse_options(data, &fsi->mount_opts);
347 tracefs_apply_options(sb, true);
353 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
355 struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
356 struct tracefs_mount_opts *opts = &fsi->mount_opts;
358 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
359 seq_printf(m, ",uid=%u",
360 from_kuid_munged(&init_user_ns, opts->uid));
361 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
362 seq_printf(m, ",gid=%u",
363 from_kgid_munged(&init_user_ns, opts->gid));
364 if (opts->mode != TRACEFS_DEFAULT_MODE)
365 seq_printf(m, ",mode=%o", opts->mode);
370 static const struct super_operations tracefs_super_operations = {
371 .alloc_inode = tracefs_alloc_inode,
372 .free_inode = tracefs_free_inode,
373 .drop_inode = generic_delete_inode,
374 .statfs = simple_statfs,
375 .remount_fs = tracefs_remount,
376 .show_options = tracefs_show_options,
379 static void tracefs_dentry_iput(struct dentry *dentry, struct inode *inode)
381 struct tracefs_inode *ti;
383 if (!dentry || !inode)
386 ti = get_tracefs(inode);
387 if (ti && ti->flags & TRACEFS_EVENT_INODE)
388 eventfs_set_ef_status_free(ti, dentry);
392 static const struct dentry_operations tracefs_dentry_operations = {
393 .d_iput = tracefs_dentry_iput,
396 static int trace_fill_super(struct super_block *sb, void *data, int silent)
398 static const struct tree_descr trace_files[] = {{""}};
399 struct tracefs_fs_info *fsi;
402 fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
409 err = tracefs_parse_options(data, &fsi->mount_opts);
413 err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
417 sb->s_op = &tracefs_super_operations;
418 sb->s_d_op = &tracefs_dentry_operations;
420 tracefs_apply_options(sb, false);
426 sb->s_fs_info = NULL;
430 static struct dentry *trace_mount(struct file_system_type *fs_type,
431 int flags, const char *dev_name,
434 return mount_single(fs_type, flags, data, trace_fill_super);
437 static struct file_system_type trace_fs_type = {
438 .owner = THIS_MODULE,
440 .mount = trace_mount,
441 .kill_sb = kill_litter_super,
443 MODULE_ALIAS_FS("tracefs");
445 struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
447 struct dentry *dentry;
450 pr_debug("tracefs: creating file '%s'\n",name);
452 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
453 &tracefs_mount_count);
455 return ERR_PTR(error);
457 /* If the parent is not specified, we create it in the root.
458 * We need the root dentry to do this, which is in the super
459 * block. A pointer to that is in the struct vfsmount that we
463 parent = tracefs_mount->mnt_root;
465 inode_lock(d_inode(parent));
466 if (unlikely(IS_DEADDIR(d_inode(parent))))
467 dentry = ERR_PTR(-ENOENT);
469 dentry = lookup_one_len(name, parent, strlen(name));
470 if (!IS_ERR(dentry) && d_inode(dentry)) {
472 dentry = ERR_PTR(-EEXIST);
475 if (IS_ERR(dentry)) {
476 inode_unlock(d_inode(parent));
477 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
483 struct dentry *tracefs_failed_creating(struct dentry *dentry)
485 inode_unlock(d_inode(dentry->d_parent));
487 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
491 struct dentry *tracefs_end_creating(struct dentry *dentry)
493 inode_unlock(d_inode(dentry->d_parent));
498 * eventfs_start_creating - start the process of creating a dentry
499 * @name: Name of the file created for the dentry
500 * @parent: The parent dentry where this dentry will be created
502 * This is a simple helper function for the dynamically created eventfs
503 * files. When the directory of the eventfs files are accessed, their
504 * dentries are created on the fly. This function is used to start that
507 struct dentry *eventfs_start_creating(const char *name, struct dentry *parent)
509 struct dentry *dentry;
512 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
513 &tracefs_mount_count);
515 return ERR_PTR(error);
518 * If the parent is not specified, we create it in the root.
519 * We need the root dentry to do this, which is in the super
520 * block. A pointer to that is in the struct vfsmount that we
524 parent = tracefs_mount->mnt_root;
526 if (unlikely(IS_DEADDIR(parent->d_inode)))
527 dentry = ERR_PTR(-ENOENT);
529 dentry = lookup_one_len(name, parent, strlen(name));
531 if (!IS_ERR(dentry) && dentry->d_inode) {
533 dentry = ERR_PTR(-EEXIST);
537 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
543 * eventfs_failed_creating - clean up a failed eventfs dentry creation
544 * @dentry: The dentry to clean up
546 * If after calling eventfs_start_creating(), a failure is detected, the
547 * resources created by eventfs_start_creating() needs to be cleaned up. In
548 * that case, this function should be called to perform that clean up.
550 struct dentry *eventfs_failed_creating(struct dentry *dentry)
553 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
558 * eventfs_end_creating - Finish the process of creating a eventfs dentry
559 * @dentry: The dentry that has successfully been created.
561 * This function is currently just a place holder to match
562 * eventfs_start_creating(). In case any synchronization needs to be added,
563 * this function will be used to implement that without having to modify
564 * the callers of eventfs_start_creating().
566 struct dentry *eventfs_end_creating(struct dentry *dentry)
572 * tracefs_create_file - create a file in the tracefs filesystem
573 * @name: a pointer to a string containing the name of the file to create.
574 * @mode: the permission that the file should have.
575 * @parent: a pointer to the parent dentry for this file. This should be a
576 * directory dentry if set. If this parameter is NULL, then the
577 * file will be created in the root of the tracefs filesystem.
578 * @data: a pointer to something that the caller will want to get to later
579 * on. The inode.i_private pointer will point to this value on
581 * @fops: a pointer to a struct file_operations that should be used for
584 * This is the basic "create a file" function for tracefs. It allows for a
585 * wide range of flexibility in creating a file, or a directory (if you want
586 * to create a directory, the tracefs_create_dir() function is
587 * recommended to be used instead.)
589 * This function will return a pointer to a dentry if it succeeds. This
590 * pointer must be passed to the tracefs_remove() function when the file is
591 * to be removed (no automatic cleanup happens if your module is unloaded,
592 * you are responsible here.) If an error occurs, %NULL will be returned.
594 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
597 struct dentry *tracefs_create_file(const char *name, umode_t mode,
598 struct dentry *parent, void *data,
599 const struct file_operations *fops)
601 struct dentry *dentry;
604 if (security_locked_down(LOCKDOWN_TRACEFS))
607 if (!(mode & S_IFMT))
609 BUG_ON(!S_ISREG(mode));
610 dentry = tracefs_start_creating(name, parent);
615 inode = tracefs_get_inode(dentry->d_sb);
616 if (unlikely(!inode))
617 return tracefs_failed_creating(dentry);
619 inode->i_mode = mode;
620 inode->i_fop = fops ? fops : &tracefs_file_operations;
621 inode->i_private = data;
622 inode->i_uid = d_inode(dentry->d_parent)->i_uid;
623 inode->i_gid = d_inode(dentry->d_parent)->i_gid;
624 d_instantiate(dentry, inode);
625 fsnotify_create(d_inode(dentry->d_parent), dentry);
626 return tracefs_end_creating(dentry);
629 static struct dentry *__create_dir(const char *name, struct dentry *parent,
630 const struct inode_operations *ops)
632 struct dentry *dentry = tracefs_start_creating(name, parent);
638 inode = tracefs_get_inode(dentry->d_sb);
639 if (unlikely(!inode))
640 return tracefs_failed_creating(dentry);
642 /* Do not set bits for OTH */
643 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
645 inode->i_fop = &simple_dir_operations;
646 inode->i_uid = d_inode(dentry->d_parent)->i_uid;
647 inode->i_gid = d_inode(dentry->d_parent)->i_gid;
649 /* directory inodes start off with i_nlink == 2 (for "." entry) */
651 d_instantiate(dentry, inode);
652 inc_nlink(d_inode(dentry->d_parent));
653 fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
654 return tracefs_end_creating(dentry);
658 * tracefs_create_dir - create a directory in the tracefs filesystem
659 * @name: a pointer to a string containing the name of the directory to
661 * @parent: a pointer to the parent dentry for this file. This should be a
662 * directory dentry if set. If this parameter is NULL, then the
663 * directory will be created in the root of the tracefs filesystem.
665 * This function creates a directory in tracefs with the given name.
667 * This function will return a pointer to a dentry if it succeeds. This
668 * pointer must be passed to the tracefs_remove() function when the file is
669 * to be removed. If an error occurs, %NULL will be returned.
671 * If tracing is not enabled in the kernel, the value -%ENODEV will be
674 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
676 if (security_locked_down(LOCKDOWN_TRACEFS))
679 return __create_dir(name, parent, &simple_dir_inode_operations);
683 * tracefs_create_instance_dir - create the tracing instances directory
684 * @name: The name of the instances directory to create
685 * @parent: The parent directory that the instances directory will exist
686 * @mkdir: The function to call when a mkdir is performed.
687 * @rmdir: The function to call when a rmdir is performed.
689 * Only one instances directory is allowed.
691 * The instances directory is special as it allows for mkdir and rmdir
692 * to be done by userspace. When a mkdir or rmdir is performed, the inode
693 * locks are released and the methods passed in (@mkdir and @rmdir) are
694 * called without locks and with the name of the directory being created
695 * within the instances directory.
697 * Returns the dentry of the instances directory.
699 __init struct dentry *tracefs_create_instance_dir(const char *name,
700 struct dentry *parent,
701 int (*mkdir)(const char *name),
702 int (*rmdir)(const char *name))
704 struct dentry *dentry;
706 /* Only allow one instance of the instances directory. */
707 if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
710 dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
714 tracefs_ops.mkdir = mkdir;
715 tracefs_ops.rmdir = rmdir;
720 static void remove_one(struct dentry *victim)
722 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
726 * tracefs_remove - recursively removes a directory
727 * @dentry: a pointer to a the dentry of the directory to be removed.
729 * This function recursively removes a directory tree in tracefs that
730 * was previously created with a call to another tracefs function
731 * (like tracefs_create_file() or variants thereof.)
733 void tracefs_remove(struct dentry *dentry)
735 if (IS_ERR_OR_NULL(dentry))
738 simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
739 simple_recursive_removal(dentry, remove_one);
740 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
744 * tracefs_initialized - Tells whether tracefs has been registered
746 bool tracefs_initialized(void)
748 return tracefs_registered;
751 static void init_once(void *foo)
753 struct tracefs_inode *ti = (struct tracefs_inode *) foo;
755 inode_init_once(&ti->vfs_inode);
758 static int __init tracefs_init(void)
762 tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
763 sizeof(struct tracefs_inode),
764 0, (SLAB_RECLAIM_ACCOUNT|
768 if (!tracefs_inode_cachep)
771 retval = sysfs_create_mount_point(kernel_kobj, "tracing");
775 retval = register_filesystem(&trace_fs_type);
777 tracefs_registered = true;
781 core_initcall(tracefs_init);