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