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