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>
24 #define TRACEFS_DEFAULT_MODE 0700
26 static struct vfsmount *tracefs_mount;
27 static int tracefs_mount_count;
28 static bool tracefs_registered;
30 static ssize_t default_read_file(struct file *file, char __user *buf,
31 size_t count, loff_t *ppos)
36 static ssize_t default_write_file(struct file *file, const char __user *buf,
37 size_t count, loff_t *ppos)
42 static const struct file_operations tracefs_file_operations = {
43 .read = default_read_file,
44 .write = default_write_file,
46 .llseek = noop_llseek,
49 static struct tracefs_dir_ops {
50 int (*mkdir)(const char *name);
51 int (*rmdir)(const char *name);
52 } tracefs_ops __ro_after_init;
54 static char *get_dname(struct dentry *dentry)
58 int len = dentry->d_name.len;
60 dname = dentry->d_name.name;
61 name = kmalloc(len + 1, GFP_KERNEL);
64 memcpy(name, dname, len);
69 static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
74 name = get_dname(dentry);
79 * The mkdir call can call the generic functions that create
80 * the files within the tracefs system. It is up to the individual
81 * mkdir routine to handle races.
84 ret = tracefs_ops.mkdir(name);
92 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
97 name = get_dname(dentry);
102 * The rmdir call can call the generic functions that create
103 * the files within the tracefs system. It is up to the individual
104 * rmdir routine to handle races.
105 * This time we need to unlock not only the parent (inode) but
106 * also the directory that is being deleted.
109 inode_unlock(dentry->d_inode);
111 ret = tracefs_ops.rmdir(name);
113 inode_lock_nested(inode, I_MUTEX_PARENT);
114 inode_lock(dentry->d_inode);
121 static const struct inode_operations tracefs_dir_inode_operations = {
122 .lookup = simple_lookup,
123 .mkdir = tracefs_syscall_mkdir,
124 .rmdir = tracefs_syscall_rmdir,
127 static struct inode *tracefs_get_inode(struct super_block *sb)
129 struct inode *inode = new_inode(sb);
131 inode->i_ino = get_next_ino();
132 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
137 struct tracefs_mount_opts {
150 static const match_table_t tokens = {
153 {Opt_mode, "mode=%o"},
157 struct tracefs_fs_info {
158 struct tracefs_mount_opts mount_opts;
161 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
163 substring_t args[MAX_OPT_ARGS];
170 opts->mode = TRACEFS_DEFAULT_MODE;
172 while ((p = strsep(&data, ",")) != NULL) {
176 token = match_token(p, tokens, args);
179 if (match_int(&args[0], &option))
181 uid = make_kuid(current_user_ns(), option);
187 if (match_int(&args[0], &option))
189 gid = make_kgid(current_user_ns(), option);
195 if (match_octal(&args[0], &option))
197 opts->mode = option & S_IALLUGO;
200 * We might like to report bad mount options here;
201 * but traditionally tracefs has ignored all mount options
209 static int tracefs_apply_options(struct super_block *sb)
211 struct tracefs_fs_info *fsi = sb->s_fs_info;
212 struct inode *inode = sb->s_root->d_inode;
213 struct tracefs_mount_opts *opts = &fsi->mount_opts;
215 inode->i_mode &= ~S_IALLUGO;
216 inode->i_mode |= opts->mode;
218 inode->i_uid = opts->uid;
219 inode->i_gid = opts->gid;
224 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
227 struct tracefs_fs_info *fsi = sb->s_fs_info;
230 err = tracefs_parse_options(data, &fsi->mount_opts);
234 tracefs_apply_options(sb);
240 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
242 struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
243 struct tracefs_mount_opts *opts = &fsi->mount_opts;
245 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
246 seq_printf(m, ",uid=%u",
247 from_kuid_munged(&init_user_ns, opts->uid));
248 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
249 seq_printf(m, ",gid=%u",
250 from_kgid_munged(&init_user_ns, opts->gid));
251 if (opts->mode != TRACEFS_DEFAULT_MODE)
252 seq_printf(m, ",mode=%o", opts->mode);
257 static const struct super_operations tracefs_super_operations = {
258 .statfs = simple_statfs,
259 .remount_fs = tracefs_remount,
260 .show_options = tracefs_show_options,
263 static int trace_fill_super(struct super_block *sb, void *data, int silent)
265 static const struct tree_descr trace_files[] = {{""}};
266 struct tracefs_fs_info *fsi;
269 fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
276 err = tracefs_parse_options(data, &fsi->mount_opts);
280 err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
284 sb->s_op = &tracefs_super_operations;
286 tracefs_apply_options(sb);
292 sb->s_fs_info = NULL;
296 static struct dentry *trace_mount(struct file_system_type *fs_type,
297 int flags, const char *dev_name,
300 return mount_single(fs_type, flags, data, trace_fill_super);
303 static struct file_system_type trace_fs_type = {
304 .owner = THIS_MODULE,
306 .mount = trace_mount,
307 .kill_sb = kill_litter_super,
309 MODULE_ALIAS_FS("tracefs");
311 static struct dentry *start_creating(const char *name, struct dentry *parent)
313 struct dentry *dentry;
316 pr_debug("tracefs: creating file '%s'\n",name);
318 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
319 &tracefs_mount_count);
321 return ERR_PTR(error);
323 /* If the parent is not specified, we create it in the root.
324 * We need the root dentry to do this, which is in the super
325 * block. A pointer to that is in the struct vfsmount that we
329 parent = tracefs_mount->mnt_root;
331 inode_lock(parent->d_inode);
332 dentry = lookup_one_len(name, parent, strlen(name));
333 if (!IS_ERR(dentry) && dentry->d_inode) {
335 dentry = ERR_PTR(-EEXIST);
338 if (IS_ERR(dentry)) {
339 inode_unlock(parent->d_inode);
340 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
346 static struct dentry *failed_creating(struct dentry *dentry)
348 inode_unlock(dentry->d_parent->d_inode);
350 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
354 static struct dentry *end_creating(struct dentry *dentry)
356 inode_unlock(dentry->d_parent->d_inode);
361 * tracefs_create_file - create a file in the tracefs filesystem
362 * @name: a pointer to a string containing the name of the file to create.
363 * @mode: the permission that the file should have.
364 * @parent: a pointer to the parent dentry for this file. This should be a
365 * directory dentry if set. If this parameter is NULL, then the
366 * file will be created in the root of the tracefs filesystem.
367 * @data: a pointer to something that the caller will want to get to later
368 * on. The inode.i_private pointer will point to this value on
370 * @fops: a pointer to a struct file_operations that should be used for
373 * This is the basic "create a file" function for tracefs. It allows for a
374 * wide range of flexibility in creating a file, or a directory (if you want
375 * to create a directory, the tracefs_create_dir() function is
376 * recommended to be used instead.)
378 * This function will return a pointer to a dentry if it succeeds. This
379 * pointer must be passed to the tracefs_remove() function when the file is
380 * to be removed (no automatic cleanup happens if your module is unloaded,
381 * you are responsible here.) If an error occurs, %NULL will be returned.
383 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
386 struct dentry *tracefs_create_file(const char *name, umode_t mode,
387 struct dentry *parent, void *data,
388 const struct file_operations *fops)
390 struct dentry *dentry;
393 if (!(mode & S_IFMT))
395 BUG_ON(!S_ISREG(mode));
396 dentry = start_creating(name, parent);
401 inode = tracefs_get_inode(dentry->d_sb);
402 if (unlikely(!inode))
403 return failed_creating(dentry);
405 inode->i_mode = mode;
406 inode->i_fop = fops ? fops : &tracefs_file_operations;
407 inode->i_private = data;
408 d_instantiate(dentry, inode);
409 fsnotify_create(dentry->d_parent->d_inode, dentry);
410 return end_creating(dentry);
413 static struct dentry *__create_dir(const char *name, struct dentry *parent,
414 const struct inode_operations *ops)
416 struct dentry *dentry = start_creating(name, parent);
422 inode = tracefs_get_inode(dentry->d_sb);
423 if (unlikely(!inode))
424 return failed_creating(dentry);
426 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
428 inode->i_fop = &simple_dir_operations;
430 /* directory inodes start off with i_nlink == 2 (for "." entry) */
432 d_instantiate(dentry, inode);
433 inc_nlink(dentry->d_parent->d_inode);
434 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
435 return end_creating(dentry);
439 * tracefs_create_dir - create a directory in the tracefs filesystem
440 * @name: a pointer to a string containing the name of the directory to
442 * @parent: a pointer to the parent dentry for this file. This should be a
443 * directory dentry if set. If this parameter is NULL, then the
444 * directory will be created in the root of the tracefs filesystem.
446 * This function creates a directory in tracefs with the given name.
448 * This function will return a pointer to a dentry if it succeeds. This
449 * pointer must be passed to the tracefs_remove() function when the file is
450 * to be removed. If an error occurs, %NULL will be returned.
452 * If tracing is not enabled in the kernel, the value -%ENODEV will be
455 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
457 return __create_dir(name, parent, &simple_dir_inode_operations);
461 * tracefs_create_instance_dir - create the tracing instances directory
462 * @name: The name of the instances directory to create
463 * @parent: The parent directory that the instances directory will exist
464 * @mkdir: The function to call when a mkdir is performed.
465 * @rmdir: The function to call when a rmdir is performed.
467 * Only one instances directory is allowed.
469 * The instances directory is special as it allows for mkdir and rmdir to
470 * to be done by userspace. When a mkdir or rmdir is performed, the inode
471 * locks are released and the methhods passed in (@mkdir and @rmdir) are
472 * called without locks and with the name of the directory being created
473 * within the instances directory.
475 * Returns the dentry of the instances directory.
477 __init struct dentry *tracefs_create_instance_dir(const char *name,
478 struct dentry *parent,
479 int (*mkdir)(const char *name),
480 int (*rmdir)(const char *name))
482 struct dentry *dentry;
484 /* Only allow one instance of the instances directory. */
485 if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
488 dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
492 tracefs_ops.mkdir = mkdir;
493 tracefs_ops.rmdir = rmdir;
498 static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
502 if (simple_positive(dentry)) {
503 if (dentry->d_inode) {
505 switch (dentry->d_inode->i_mode & S_IFMT) {
507 ret = simple_rmdir(parent->d_inode, dentry);
510 simple_unlink(parent->d_inode, dentry);
522 * tracefs_remove - removes a file or directory from the tracefs filesystem
523 * @dentry: a pointer to a the dentry of the file or directory to be
526 * This function removes a file or directory in tracefs that was previously
527 * created with a call to another tracefs function (like
528 * tracefs_create_file() or variants thereof.)
530 void tracefs_remove(struct dentry *dentry)
532 struct dentry *parent;
535 if (IS_ERR_OR_NULL(dentry))
538 parent = dentry->d_parent;
539 inode_lock(parent->d_inode);
540 ret = __tracefs_remove(dentry, parent);
541 inode_unlock(parent->d_inode);
543 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
547 * tracefs_remove_recursive - recursively removes a directory
548 * @dentry: a pointer to a the dentry of the directory to be removed.
550 * This function recursively removes a directory tree in tracefs that
551 * was previously created with a call to another tracefs function
552 * (like tracefs_create_file() or variants thereof.)
554 void tracefs_remove_recursive(struct dentry *dentry)
556 struct dentry *child, *parent;
558 if (IS_ERR_OR_NULL(dentry))
563 inode_lock(parent->d_inode);
566 * The parent->d_subdirs is protected by the d_lock. Outside that
567 * lock, the child can be unlinked and set to be freed which can
568 * use the d_u.d_child as the rcu head and corrupt this list.
570 spin_lock(&parent->d_lock);
571 list_for_each_entry(child, &parent->d_subdirs, d_child) {
572 if (!simple_positive(child))
575 /* perhaps simple_empty(child) makes more sense */
576 if (!list_empty(&child->d_subdirs)) {
577 spin_unlock(&parent->d_lock);
578 inode_unlock(parent->d_inode);
583 spin_unlock(&parent->d_lock);
585 if (!__tracefs_remove(child, parent))
586 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
589 * The parent->d_lock protects agaist child from unlinking
590 * from d_subdirs. When releasing the parent->d_lock we can
591 * no longer trust that the next pointer is valid.
592 * Restart the loop. We'll skip this one with the
593 * simple_positive() check.
597 spin_unlock(&parent->d_lock);
599 inode_unlock(parent->d_inode);
601 parent = parent->d_parent;
602 inode_lock(parent->d_inode);
608 if (!__tracefs_remove(child, parent))
609 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
610 inode_unlock(parent->d_inode);
614 * tracefs_initialized - Tells whether tracefs has been registered
616 bool tracefs_initialized(void)
618 return tracefs_registered;
621 static int __init tracefs_init(void)
625 retval = sysfs_create_mount_point(kernel_kobj, "tracing");
629 retval = register_filesystem(&trace_fs_type);
631 tracefs_registered = true;
635 core_initcall(tracefs_init);