xref: /linux/fs/tracefs/inode.c (revision 3e0bc2855b573bcffa2a52955a878f537f5ac0cd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  inode.c - part of tracefs, a pseudo file system for activating tracing
4  *
5  * Based on debugfs by: Greg Kroah-Hartman <greg@kroah.com>
6  *
7  *  Copyright (C) 2014 Red Hat Inc, author: Steven Rostedt <srostedt@redhat.com>
8  *
9  * tracefs is the file system that is used by the tracing infrastructure.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/fs.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>
24 #include "internal.h"
25 
26 #define TRACEFS_DEFAULT_MODE	0700
27 static struct kmem_cache *tracefs_inode_cachep __ro_after_init;
28 
29 static struct vfsmount *tracefs_mount;
30 static int tracefs_mount_count;
31 static bool tracefs_registered;
32 
33 static struct inode *tracefs_alloc_inode(struct super_block *sb)
34 {
35 	struct tracefs_inode *ti;
36 
37 	ti = kmem_cache_alloc(tracefs_inode_cachep, GFP_KERNEL);
38 	if (!ti)
39 		return NULL;
40 
41 	ti->flags = 0;
42 
43 	return &ti->vfs_inode;
44 }
45 
46 static void tracefs_free_inode(struct inode *inode)
47 {
48 	kmem_cache_free(tracefs_inode_cachep, get_tracefs(inode));
49 }
50 
51 static ssize_t default_read_file(struct file *file, char __user *buf,
52 				 size_t count, loff_t *ppos)
53 {
54 	return 0;
55 }
56 
57 static ssize_t default_write_file(struct file *file, const char __user *buf,
58 				   size_t count, loff_t *ppos)
59 {
60 	return count;
61 }
62 
63 static const struct file_operations tracefs_file_operations = {
64 	.read =		default_read_file,
65 	.write =	default_write_file,
66 	.open =		simple_open,
67 	.llseek =	noop_llseek,
68 };
69 
70 static struct tracefs_dir_ops {
71 	int (*mkdir)(const char *name);
72 	int (*rmdir)(const char *name);
73 } tracefs_ops __ro_after_init;
74 
75 static char *get_dname(struct dentry *dentry)
76 {
77 	const char *dname;
78 	char *name;
79 	int len = dentry->d_name.len;
80 
81 	dname = dentry->d_name.name;
82 	name = kmalloc(len + 1, GFP_KERNEL);
83 	if (!name)
84 		return NULL;
85 	memcpy(name, dname, len);
86 	name[len] = 0;
87 	return name;
88 }
89 
90 static int tracefs_syscall_mkdir(struct mnt_idmap *idmap,
91 				 struct inode *inode, struct dentry *dentry,
92 				 umode_t mode)
93 {
94 	char *name;
95 	int ret;
96 
97 	name = get_dname(dentry);
98 	if (!name)
99 		return -ENOMEM;
100 
101 	/*
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.
105 	 */
106 	inode_unlock(inode);
107 	ret = tracefs_ops.mkdir(name);
108 	inode_lock(inode);
109 
110 	kfree(name);
111 
112 	return ret;
113 }
114 
115 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
116 {
117 	char *name;
118 	int ret;
119 
120 	name = get_dname(dentry);
121 	if (!name)
122 		return -ENOMEM;
123 
124 	/*
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.
130 	 */
131 	inode_unlock(inode);
132 	inode_unlock(d_inode(dentry));
133 
134 	ret = tracefs_ops.rmdir(name);
135 
136 	inode_lock_nested(inode, I_MUTEX_PARENT);
137 	inode_lock(d_inode(dentry));
138 
139 	kfree(name);
140 
141 	return ret;
142 }
143 
144 static const struct inode_operations tracefs_dir_inode_operations = {
145 	.lookup		= simple_lookup,
146 	.mkdir		= tracefs_syscall_mkdir,
147 	.rmdir		= tracefs_syscall_rmdir,
148 };
149 
150 struct inode *tracefs_get_inode(struct super_block *sb)
151 {
152 	struct inode *inode = new_inode(sb);
153 	if (inode) {
154 		inode->i_ino = get_next_ino();
155 		simple_inode_init_ts(inode);
156 	}
157 	return inode;
158 }
159 
160 struct tracefs_mount_opts {
161 	kuid_t uid;
162 	kgid_t gid;
163 	umode_t mode;
164 	/* Opt_* bitfield. */
165 	unsigned int opts;
166 };
167 
168 enum {
169 	Opt_uid,
170 	Opt_gid,
171 	Opt_mode,
172 	Opt_err
173 };
174 
175 static const match_table_t tokens = {
176 	{Opt_uid, "uid=%u"},
177 	{Opt_gid, "gid=%u"},
178 	{Opt_mode, "mode=%o"},
179 	{Opt_err, NULL}
180 };
181 
182 struct tracefs_fs_info {
183 	struct tracefs_mount_opts mount_opts;
184 };
185 
186 static void change_gid(struct dentry *dentry, kgid_t gid)
187 {
188 	if (!dentry->d_inode)
189 		return;
190 	dentry->d_inode->i_gid = gid;
191 }
192 
193 /*
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
198  * the given gid.
199  */
200 static void set_gid(struct dentry *parent, kgid_t gid)
201 {
202 	struct dentry *this_parent, *dentry;
203 
204 	this_parent = parent;
205 	spin_lock(&this_parent->d_lock);
206 
207 	change_gid(this_parent, gid);
208 repeat:
209 	dentry = d_first_child(this_parent);
210 resume:
211 	hlist_for_each_entry_from(dentry, d_sib) {
212 		struct tracefs_inode *ti;
213 
214 		/* Note, getdents() can add a cursor dentry with no inode */
215 		if (!dentry->d_inode)
216 			continue;
217 
218 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
219 
220 		change_gid(dentry, gid);
221 
222 		/* If this is the events directory, update that too */
223 		ti = get_tracefs(dentry->d_inode);
224 		if (ti && (ti->flags & TRACEFS_EVENT_INODE))
225 			eventfs_update_gid(dentry, gid);
226 
227 		if (!hlist_empty(&dentry->d_children)) {
228 			spin_unlock(&this_parent->d_lock);
229 			spin_release(&dentry->d_lock.dep_map, _RET_IP_);
230 			this_parent = dentry;
231 			spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
232 			goto repeat;
233 		}
234 		spin_unlock(&dentry->d_lock);
235 	}
236 	/*
237 	 * All done at this level ... ascend and resume the search.
238 	 */
239 	rcu_read_lock();
240 ascend:
241 	if (this_parent != parent) {
242 		dentry = this_parent;
243 		this_parent = dentry->d_parent;
244 
245 		spin_unlock(&dentry->d_lock);
246 		spin_lock(&this_parent->d_lock);
247 
248 		/* go into the first sibling still alive */
249 		hlist_for_each_entry_continue(dentry, d_sib) {
250 			if (likely(!(dentry->d_flags & DCACHE_DENTRY_KILLED))) {
251 				rcu_read_unlock();
252 				goto resume;
253 			}
254 		}
255 		goto ascend;
256 	}
257 	rcu_read_unlock();
258 	spin_unlock(&this_parent->d_lock);
259 	return;
260 }
261 
262 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
263 {
264 	substring_t args[MAX_OPT_ARGS];
265 	int option;
266 	int token;
267 	kuid_t uid;
268 	kgid_t gid;
269 	char *p;
270 
271 	opts->opts = 0;
272 	opts->mode = TRACEFS_DEFAULT_MODE;
273 
274 	while ((p = strsep(&data, ",")) != NULL) {
275 		if (!*p)
276 			continue;
277 
278 		token = match_token(p, tokens, args);
279 		switch (token) {
280 		case Opt_uid:
281 			if (match_int(&args[0], &option))
282 				return -EINVAL;
283 			uid = make_kuid(current_user_ns(), option);
284 			if (!uid_valid(uid))
285 				return -EINVAL;
286 			opts->uid = uid;
287 			break;
288 		case Opt_gid:
289 			if (match_int(&args[0], &option))
290 				return -EINVAL;
291 			gid = make_kgid(current_user_ns(), option);
292 			if (!gid_valid(gid))
293 				return -EINVAL;
294 			opts->gid = gid;
295 			break;
296 		case Opt_mode:
297 			if (match_octal(&args[0], &option))
298 				return -EINVAL;
299 			opts->mode = option & S_IALLUGO;
300 			break;
301 		/*
302 		 * We might like to report bad mount options here;
303 		 * but traditionally tracefs has ignored all mount options
304 		 */
305 		}
306 
307 		opts->opts |= BIT(token);
308 	}
309 
310 	return 0;
311 }
312 
313 static int tracefs_apply_options(struct super_block *sb, bool remount)
314 {
315 	struct tracefs_fs_info *fsi = sb->s_fs_info;
316 	struct inode *inode = d_inode(sb->s_root);
317 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
318 	umode_t tmp_mode;
319 
320 	/*
321 	 * On remount, only reset mode/uid/gid if they were provided as mount
322 	 * options.
323 	 */
324 
325 	if (!remount || opts->opts & BIT(Opt_mode)) {
326 		tmp_mode = READ_ONCE(inode->i_mode) & ~S_IALLUGO;
327 		tmp_mode |= opts->mode;
328 		WRITE_ONCE(inode->i_mode, tmp_mode);
329 	}
330 
331 	if (!remount || opts->opts & BIT(Opt_uid))
332 		inode->i_uid = opts->uid;
333 
334 	if (!remount || opts->opts & BIT(Opt_gid)) {
335 		/* Set all the group ids to the mount option */
336 		set_gid(sb->s_root, opts->gid);
337 	}
338 
339 	return 0;
340 }
341 
342 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
343 {
344 	int err;
345 	struct tracefs_fs_info *fsi = sb->s_fs_info;
346 
347 	sync_filesystem(sb);
348 	err = tracefs_parse_options(data, &fsi->mount_opts);
349 	if (err)
350 		goto fail;
351 
352 	tracefs_apply_options(sb, true);
353 
354 fail:
355 	return err;
356 }
357 
358 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
359 {
360 	struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
361 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
362 
363 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
364 		seq_printf(m, ",uid=%u",
365 			   from_kuid_munged(&init_user_ns, opts->uid));
366 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
367 		seq_printf(m, ",gid=%u",
368 			   from_kgid_munged(&init_user_ns, opts->gid));
369 	if (opts->mode != TRACEFS_DEFAULT_MODE)
370 		seq_printf(m, ",mode=%o", opts->mode);
371 
372 	return 0;
373 }
374 
375 static const struct super_operations tracefs_super_operations = {
376 	.alloc_inode    = tracefs_alloc_inode,
377 	.free_inode     = tracefs_free_inode,
378 	.drop_inode     = generic_delete_inode,
379 	.statfs		= simple_statfs,
380 	.remount_fs	= tracefs_remount,
381 	.show_options	= tracefs_show_options,
382 };
383 
384 static void tracefs_dentry_iput(struct dentry *dentry, struct inode *inode)
385 {
386 	struct tracefs_inode *ti;
387 
388 	if (!dentry || !inode)
389 		return;
390 
391 	ti = get_tracefs(inode);
392 	if (ti && ti->flags & TRACEFS_EVENT_INODE)
393 		eventfs_set_ei_status_free(ti, dentry);
394 	iput(inode);
395 }
396 
397 static const struct dentry_operations tracefs_dentry_operations = {
398 	.d_iput = tracefs_dentry_iput,
399 };
400 
401 static int trace_fill_super(struct super_block *sb, void *data, int silent)
402 {
403 	static const struct tree_descr trace_files[] = {{""}};
404 	struct tracefs_fs_info *fsi;
405 	int err;
406 
407 	fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
408 	sb->s_fs_info = fsi;
409 	if (!fsi) {
410 		err = -ENOMEM;
411 		goto fail;
412 	}
413 
414 	err = tracefs_parse_options(data, &fsi->mount_opts);
415 	if (err)
416 		goto fail;
417 
418 	err  =  simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
419 	if (err)
420 		goto fail;
421 
422 	sb->s_op = &tracefs_super_operations;
423 	sb->s_d_op = &tracefs_dentry_operations;
424 
425 	tracefs_apply_options(sb, false);
426 
427 	return 0;
428 
429 fail:
430 	kfree(fsi);
431 	sb->s_fs_info = NULL;
432 	return err;
433 }
434 
435 static struct dentry *trace_mount(struct file_system_type *fs_type,
436 			int flags, const char *dev_name,
437 			void *data)
438 {
439 	return mount_single(fs_type, flags, data, trace_fill_super);
440 }
441 
442 static struct file_system_type trace_fs_type = {
443 	.owner =	THIS_MODULE,
444 	.name =		"tracefs",
445 	.mount =	trace_mount,
446 	.kill_sb =	kill_litter_super,
447 };
448 MODULE_ALIAS_FS("tracefs");
449 
450 struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
451 {
452 	struct dentry *dentry;
453 	int error;
454 
455 	pr_debug("tracefs: creating file '%s'\n",name);
456 
457 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
458 			      &tracefs_mount_count);
459 	if (error)
460 		return ERR_PTR(error);
461 
462 	/* If the parent is not specified, we create it in the root.
463 	 * We need the root dentry to do this, which is in the super
464 	 * block. A pointer to that is in the struct vfsmount that we
465 	 * have around.
466 	 */
467 	if (!parent)
468 		parent = tracefs_mount->mnt_root;
469 
470 	inode_lock(d_inode(parent));
471 	if (unlikely(IS_DEADDIR(d_inode(parent))))
472 		dentry = ERR_PTR(-ENOENT);
473 	else
474 		dentry = lookup_one_len(name, parent, strlen(name));
475 	if (!IS_ERR(dentry) && d_inode(dentry)) {
476 		dput(dentry);
477 		dentry = ERR_PTR(-EEXIST);
478 	}
479 
480 	if (IS_ERR(dentry)) {
481 		inode_unlock(d_inode(parent));
482 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
483 	}
484 
485 	return dentry;
486 }
487 
488 struct dentry *tracefs_failed_creating(struct dentry *dentry)
489 {
490 	inode_unlock(d_inode(dentry->d_parent));
491 	dput(dentry);
492 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
493 	return NULL;
494 }
495 
496 struct dentry *tracefs_end_creating(struct dentry *dentry)
497 {
498 	inode_unlock(d_inode(dentry->d_parent));
499 	return dentry;
500 }
501 
502 /**
503  * eventfs_start_creating - start the process of creating a dentry
504  * @name: Name of the file created for the dentry
505  * @parent: The parent dentry where this dentry will be created
506  *
507  * This is a simple helper function for the dynamically created eventfs
508  * files. When the directory of the eventfs files are accessed, their
509  * dentries are created on the fly. This function is used to start that
510  * process.
511  */
512 struct dentry *eventfs_start_creating(const char *name, struct dentry *parent)
513 {
514 	struct dentry *dentry;
515 	int error;
516 
517 	/* Must always have a parent. */
518 	if (WARN_ON_ONCE(!parent))
519 		return ERR_PTR(-EINVAL);
520 
521 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
522 			      &tracefs_mount_count);
523 	if (error)
524 		return ERR_PTR(error);
525 
526 	if (unlikely(IS_DEADDIR(parent->d_inode)))
527 		dentry = ERR_PTR(-ENOENT);
528 	else
529 		dentry = lookup_one_len(name, parent, strlen(name));
530 
531 	if (!IS_ERR(dentry) && dentry->d_inode) {
532 		dput(dentry);
533 		dentry = ERR_PTR(-EEXIST);
534 	}
535 
536 	if (IS_ERR(dentry))
537 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
538 
539 	return dentry;
540 }
541 
542 /**
543  * eventfs_failed_creating - clean up a failed eventfs dentry creation
544  * @dentry: The dentry to clean up
545  *
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.
549  */
550 struct dentry *eventfs_failed_creating(struct dentry *dentry)
551 {
552 	dput(dentry);
553 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
554 	return NULL;
555 }
556 
557 /**
558  * eventfs_end_creating - Finish the process of creating a eventfs dentry
559  * @dentry: The dentry that has successfully been created.
560  *
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().
565  */
566 struct dentry *eventfs_end_creating(struct dentry *dentry)
567 {
568 	return dentry;
569 }
570 
571 /**
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
580  *        the open() call.
581  * @fops: a pointer to a struct file_operations that should be used for
582  *        this file.
583  *
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.)
588  *
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.
593  *
594  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
595  * returned.
596  */
597 struct dentry *tracefs_create_file(const char *name, umode_t mode,
598 				   struct dentry *parent, void *data,
599 				   const struct file_operations *fops)
600 {
601 	struct dentry *dentry;
602 	struct inode *inode;
603 
604 	if (security_locked_down(LOCKDOWN_TRACEFS))
605 		return NULL;
606 
607 	if (!(mode & S_IFMT))
608 		mode |= S_IFREG;
609 	BUG_ON(!S_ISREG(mode));
610 	dentry = tracefs_start_creating(name, parent);
611 
612 	if (IS_ERR(dentry))
613 		return NULL;
614 
615 	inode = tracefs_get_inode(dentry->d_sb);
616 	if (unlikely(!inode))
617 		return tracefs_failed_creating(dentry);
618 
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);
627 }
628 
629 static struct dentry *__create_dir(const char *name, struct dentry *parent,
630 				   const struct inode_operations *ops)
631 {
632 	struct dentry *dentry = tracefs_start_creating(name, parent);
633 	struct inode *inode;
634 
635 	if (IS_ERR(dentry))
636 		return NULL;
637 
638 	inode = tracefs_get_inode(dentry->d_sb);
639 	if (unlikely(!inode))
640 		return tracefs_failed_creating(dentry);
641 
642 	/* Do not set bits for OTH */
643 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
644 	inode->i_op = ops;
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;
648 
649 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
650 	inc_nlink(inode);
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);
655 }
656 
657 /**
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
660  *        create.
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.
664  *
665  * This function creates a directory in tracefs with the given name.
666  *
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.
670  *
671  * If tracing is not enabled in the kernel, the value -%ENODEV will be
672  * returned.
673  */
674 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
675 {
676 	if (security_locked_down(LOCKDOWN_TRACEFS))
677 		return NULL;
678 
679 	return __create_dir(name, parent, &simple_dir_inode_operations);
680 }
681 
682 /**
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.
688  *
689  * Only one instances directory is allowed.
690  *
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.
696  *
697  * Returns the dentry of the instances directory.
698  */
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))
703 {
704 	struct dentry *dentry;
705 
706 	/* Only allow one instance of the instances directory. */
707 	if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
708 		return NULL;
709 
710 	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
711 	if (!dentry)
712 		return NULL;
713 
714 	tracefs_ops.mkdir = mkdir;
715 	tracefs_ops.rmdir = rmdir;
716 
717 	return dentry;
718 }
719 
720 static void remove_one(struct dentry *victim)
721 {
722 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
723 }
724 
725 /**
726  * tracefs_remove - recursively removes a directory
727  * @dentry: a pointer to a the dentry of the directory to be removed.
728  *
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.)
732  */
733 void tracefs_remove(struct dentry *dentry)
734 {
735 	if (IS_ERR_OR_NULL(dentry))
736 		return;
737 
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);
741 }
742 
743 /**
744  * tracefs_initialized - Tells whether tracefs has been registered
745  */
746 bool tracefs_initialized(void)
747 {
748 	return tracefs_registered;
749 }
750 
751 static void init_once(void *foo)
752 {
753 	struct tracefs_inode *ti = (struct tracefs_inode *) foo;
754 
755 	inode_init_once(&ti->vfs_inode);
756 }
757 
758 static int __init tracefs_init(void)
759 {
760 	int retval;
761 
762 	tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
763 						 sizeof(struct tracefs_inode),
764 						 0, (SLAB_RECLAIM_ACCOUNT|
765 						     SLAB_MEM_SPREAD|
766 						     SLAB_ACCOUNT),
767 						 init_once);
768 	if (!tracefs_inode_cachep)
769 		return -ENOMEM;
770 
771 	retval = sysfs_create_mount_point(kernel_kobj, "tracing");
772 	if (retval)
773 		return -EINVAL;
774 
775 	retval = register_filesystem(&trace_fs_type);
776 	if (!retval)
777 		tracefs_registered = true;
778 
779 	return retval;
780 }
781 core_initcall(tracefs_init);
782