xref: /linux/fs/tracefs/inode.c (revision 6b93f350e55f3f2ee071dd41109d936abfba8ebf)
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;
203 	struct list_head *next;
204 
205 	this_parent = parent;
206 	spin_lock(&this_parent->d_lock);
207 
208 	change_gid(this_parent, gid);
209 repeat:
210 	next = this_parent->d_subdirs.next;
211 resume:
212 	while (next != &this_parent->d_subdirs) {
213 		struct list_head *tmp = next;
214 		struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
215 		next = tmp->next;
216 
217 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
218 
219 		change_gid(dentry, gid);
220 
221 		if (!list_empty(&dentry->d_subdirs)) {
222 			spin_unlock(&this_parent->d_lock);
223 			spin_release(&dentry->d_lock.dep_map, _RET_IP_);
224 			this_parent = dentry;
225 			spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
226 			goto repeat;
227 		}
228 		spin_unlock(&dentry->d_lock);
229 	}
230 	/*
231 	 * All done at this level ... ascend and resume the search.
232 	 */
233 	rcu_read_lock();
234 ascend:
235 	if (this_parent != parent) {
236 		struct dentry *child = this_parent;
237 		this_parent = child->d_parent;
238 
239 		spin_unlock(&child->d_lock);
240 		spin_lock(&this_parent->d_lock);
241 
242 		/* go into the first sibling still alive */
243 		do {
244 			next = child->d_child.next;
245 			if (next == &this_parent->d_subdirs)
246 				goto ascend;
247 			child = list_entry(next, struct dentry, d_child);
248 		} while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
249 		rcu_read_unlock();
250 		goto resume;
251 	}
252 	rcu_read_unlock();
253 	spin_unlock(&this_parent->d_lock);
254 	return;
255 }
256 
257 static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
258 {
259 	substring_t args[MAX_OPT_ARGS];
260 	int option;
261 	int token;
262 	kuid_t uid;
263 	kgid_t gid;
264 	char *p;
265 
266 	opts->opts = 0;
267 	opts->mode = TRACEFS_DEFAULT_MODE;
268 
269 	while ((p = strsep(&data, ",")) != NULL) {
270 		if (!*p)
271 			continue;
272 
273 		token = match_token(p, tokens, args);
274 		switch (token) {
275 		case Opt_uid:
276 			if (match_int(&args[0], &option))
277 				return -EINVAL;
278 			uid = make_kuid(current_user_ns(), option);
279 			if (!uid_valid(uid))
280 				return -EINVAL;
281 			opts->uid = uid;
282 			break;
283 		case Opt_gid:
284 			if (match_int(&args[0], &option))
285 				return -EINVAL;
286 			gid = make_kgid(current_user_ns(), option);
287 			if (!gid_valid(gid))
288 				return -EINVAL;
289 			opts->gid = gid;
290 			break;
291 		case Opt_mode:
292 			if (match_octal(&args[0], &option))
293 				return -EINVAL;
294 			opts->mode = option & S_IALLUGO;
295 			break;
296 		/*
297 		 * We might like to report bad mount options here;
298 		 * but traditionally tracefs has ignored all mount options
299 		 */
300 		}
301 
302 		opts->opts |= BIT(token);
303 	}
304 
305 	return 0;
306 }
307 
308 static int tracefs_apply_options(struct super_block *sb, bool remount)
309 {
310 	struct tracefs_fs_info *fsi = sb->s_fs_info;
311 	struct inode *inode = d_inode(sb->s_root);
312 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
313 	umode_t tmp_mode;
314 
315 	/*
316 	 * On remount, only reset mode/uid/gid if they were provided as mount
317 	 * options.
318 	 */
319 
320 	if (!remount || opts->opts & BIT(Opt_mode)) {
321 		tmp_mode = READ_ONCE(inode->i_mode) & ~S_IALLUGO;
322 		tmp_mode |= opts->mode;
323 		WRITE_ONCE(inode->i_mode, tmp_mode);
324 	}
325 
326 	if (!remount || opts->opts & BIT(Opt_uid))
327 		inode->i_uid = opts->uid;
328 
329 	if (!remount || opts->opts & BIT(Opt_gid)) {
330 		/* Set all the group ids to the mount option */
331 		set_gid(sb->s_root, opts->gid);
332 	}
333 
334 	return 0;
335 }
336 
337 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
338 {
339 	int err;
340 	struct tracefs_fs_info *fsi = sb->s_fs_info;
341 
342 	sync_filesystem(sb);
343 	err = tracefs_parse_options(data, &fsi->mount_opts);
344 	if (err)
345 		goto fail;
346 
347 	tracefs_apply_options(sb, true);
348 
349 fail:
350 	return err;
351 }
352 
353 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
354 {
355 	struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
356 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
357 
358 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
359 		seq_printf(m, ",uid=%u",
360 			   from_kuid_munged(&init_user_ns, opts->uid));
361 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
362 		seq_printf(m, ",gid=%u",
363 			   from_kgid_munged(&init_user_ns, opts->gid));
364 	if (opts->mode != TRACEFS_DEFAULT_MODE)
365 		seq_printf(m, ",mode=%o", opts->mode);
366 
367 	return 0;
368 }
369 
370 static const struct super_operations tracefs_super_operations = {
371 	.alloc_inode    = tracefs_alloc_inode,
372 	.free_inode     = tracefs_free_inode,
373 	.drop_inode     = generic_delete_inode,
374 	.statfs		= simple_statfs,
375 	.remount_fs	= tracefs_remount,
376 	.show_options	= tracefs_show_options,
377 };
378 
379 static void tracefs_dentry_iput(struct dentry *dentry, struct inode *inode)
380 {
381 	struct tracefs_inode *ti;
382 
383 	if (!dentry || !inode)
384 		return;
385 
386 	ti = get_tracefs(inode);
387 	if (ti && ti->flags & TRACEFS_EVENT_INODE)
388 		eventfs_set_ei_status_free(ti, dentry);
389 	iput(inode);
390 }
391 
392 static const struct dentry_operations tracefs_dentry_operations = {
393 	.d_iput = tracefs_dentry_iput,
394 };
395 
396 static int trace_fill_super(struct super_block *sb, void *data, int silent)
397 {
398 	static const struct tree_descr trace_files[] = {{""}};
399 	struct tracefs_fs_info *fsi;
400 	int err;
401 
402 	fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
403 	sb->s_fs_info = fsi;
404 	if (!fsi) {
405 		err = -ENOMEM;
406 		goto fail;
407 	}
408 
409 	err = tracefs_parse_options(data, &fsi->mount_opts);
410 	if (err)
411 		goto fail;
412 
413 	err  =  simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
414 	if (err)
415 		goto fail;
416 
417 	sb->s_op = &tracefs_super_operations;
418 	sb->s_d_op = &tracefs_dentry_operations;
419 
420 	tracefs_apply_options(sb, false);
421 
422 	return 0;
423 
424 fail:
425 	kfree(fsi);
426 	sb->s_fs_info = NULL;
427 	return err;
428 }
429 
430 static struct dentry *trace_mount(struct file_system_type *fs_type,
431 			int flags, const char *dev_name,
432 			void *data)
433 {
434 	return mount_single(fs_type, flags, data, trace_fill_super);
435 }
436 
437 static struct file_system_type trace_fs_type = {
438 	.owner =	THIS_MODULE,
439 	.name =		"tracefs",
440 	.mount =	trace_mount,
441 	.kill_sb =	kill_litter_super,
442 };
443 MODULE_ALIAS_FS("tracefs");
444 
445 struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
446 {
447 	struct dentry *dentry;
448 	int error;
449 
450 	pr_debug("tracefs: creating file '%s'\n",name);
451 
452 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
453 			      &tracefs_mount_count);
454 	if (error)
455 		return ERR_PTR(error);
456 
457 	/* If the parent is not specified, we create it in the root.
458 	 * We need the root dentry to do this, which is in the super
459 	 * block. A pointer to that is in the struct vfsmount that we
460 	 * have around.
461 	 */
462 	if (!parent)
463 		parent = tracefs_mount->mnt_root;
464 
465 	inode_lock(d_inode(parent));
466 	if (unlikely(IS_DEADDIR(d_inode(parent))))
467 		dentry = ERR_PTR(-ENOENT);
468 	else
469 		dentry = lookup_one_len(name, parent, strlen(name));
470 	if (!IS_ERR(dentry) && d_inode(dentry)) {
471 		dput(dentry);
472 		dentry = ERR_PTR(-EEXIST);
473 	}
474 
475 	if (IS_ERR(dentry)) {
476 		inode_unlock(d_inode(parent));
477 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
478 	}
479 
480 	return dentry;
481 }
482 
483 struct dentry *tracefs_failed_creating(struct dentry *dentry)
484 {
485 	inode_unlock(d_inode(dentry->d_parent));
486 	dput(dentry);
487 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
488 	return NULL;
489 }
490 
491 struct dentry *tracefs_end_creating(struct dentry *dentry)
492 {
493 	inode_unlock(d_inode(dentry->d_parent));
494 	return dentry;
495 }
496 
497 /**
498  * eventfs_start_creating - start the process of creating a dentry
499  * @name: Name of the file created for the dentry
500  * @parent: The parent dentry where this dentry will be created
501  *
502  * This is a simple helper function for the dynamically created eventfs
503  * files. When the directory of the eventfs files are accessed, their
504  * dentries are created on the fly. This function is used to start that
505  * process.
506  */
507 struct dentry *eventfs_start_creating(const char *name, struct dentry *parent)
508 {
509 	struct dentry *dentry;
510 	int error;
511 
512 	/* Must always have a parent. */
513 	if (WARN_ON_ONCE(!parent))
514 		return ERR_PTR(-EINVAL);
515 
516 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
517 			      &tracefs_mount_count);
518 	if (error)
519 		return ERR_PTR(error);
520 
521 	if (unlikely(IS_DEADDIR(parent->d_inode)))
522 		dentry = ERR_PTR(-ENOENT);
523 	else
524 		dentry = lookup_one_len(name, parent, strlen(name));
525 
526 	if (!IS_ERR(dentry) && dentry->d_inode) {
527 		dput(dentry);
528 		dentry = ERR_PTR(-EEXIST);
529 	}
530 
531 	if (IS_ERR(dentry))
532 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
533 
534 	return dentry;
535 }
536 
537 /**
538  * eventfs_failed_creating - clean up a failed eventfs dentry creation
539  * @dentry: The dentry to clean up
540  *
541  * If after calling eventfs_start_creating(), a failure is detected, the
542  * resources created by eventfs_start_creating() needs to be cleaned up. In
543  * that case, this function should be called to perform that clean up.
544  */
545 struct dentry *eventfs_failed_creating(struct dentry *dentry)
546 {
547 	dput(dentry);
548 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
549 	return NULL;
550 }
551 
552 /**
553  * eventfs_end_creating - Finish the process of creating a eventfs dentry
554  * @dentry: The dentry that has successfully been created.
555  *
556  * This function is currently just a place holder to match
557  * eventfs_start_creating(). In case any synchronization needs to be added,
558  * this function will be used to implement that without having to modify
559  * the callers of eventfs_start_creating().
560  */
561 struct dentry *eventfs_end_creating(struct dentry *dentry)
562 {
563 	return dentry;
564 }
565 
566 /**
567  * tracefs_create_file - create a file in the tracefs filesystem
568  * @name: a pointer to a string containing the name of the file to create.
569  * @mode: the permission that the file should have.
570  * @parent: a pointer to the parent dentry for this file.  This should be a
571  *          directory dentry if set.  If this parameter is NULL, then the
572  *          file will be created in the root of the tracefs filesystem.
573  * @data: a pointer to something that the caller will want to get to later
574  *        on.  The inode.i_private pointer will point to this value on
575  *        the open() call.
576  * @fops: a pointer to a struct file_operations that should be used for
577  *        this file.
578  *
579  * This is the basic "create a file" function for tracefs.  It allows for a
580  * wide range of flexibility in creating a file, or a directory (if you want
581  * to create a directory, the tracefs_create_dir() function is
582  * recommended to be used instead.)
583  *
584  * This function will return a pointer to a dentry if it succeeds.  This
585  * pointer must be passed to the tracefs_remove() function when the file is
586  * to be removed (no automatic cleanup happens if your module is unloaded,
587  * you are responsible here.)  If an error occurs, %NULL will be returned.
588  *
589  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
590  * returned.
591  */
592 struct dentry *tracefs_create_file(const char *name, umode_t mode,
593 				   struct dentry *parent, void *data,
594 				   const struct file_operations *fops)
595 {
596 	struct dentry *dentry;
597 	struct inode *inode;
598 
599 	if (security_locked_down(LOCKDOWN_TRACEFS))
600 		return NULL;
601 
602 	if (!(mode & S_IFMT))
603 		mode |= S_IFREG;
604 	BUG_ON(!S_ISREG(mode));
605 	dentry = tracefs_start_creating(name, parent);
606 
607 	if (IS_ERR(dentry))
608 		return NULL;
609 
610 	inode = tracefs_get_inode(dentry->d_sb);
611 	if (unlikely(!inode))
612 		return tracefs_failed_creating(dentry);
613 
614 	inode->i_mode = mode;
615 	inode->i_fop = fops ? fops : &tracefs_file_operations;
616 	inode->i_private = data;
617 	inode->i_uid = d_inode(dentry->d_parent)->i_uid;
618 	inode->i_gid = d_inode(dentry->d_parent)->i_gid;
619 	d_instantiate(dentry, inode);
620 	fsnotify_create(d_inode(dentry->d_parent), dentry);
621 	return tracefs_end_creating(dentry);
622 }
623 
624 static struct dentry *__create_dir(const char *name, struct dentry *parent,
625 				   const struct inode_operations *ops)
626 {
627 	struct dentry *dentry = tracefs_start_creating(name, parent);
628 	struct inode *inode;
629 
630 	if (IS_ERR(dentry))
631 		return NULL;
632 
633 	inode = tracefs_get_inode(dentry->d_sb);
634 	if (unlikely(!inode))
635 		return tracefs_failed_creating(dentry);
636 
637 	/* Do not set bits for OTH */
638 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
639 	inode->i_op = ops;
640 	inode->i_fop = &simple_dir_operations;
641 	inode->i_uid = d_inode(dentry->d_parent)->i_uid;
642 	inode->i_gid = d_inode(dentry->d_parent)->i_gid;
643 
644 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
645 	inc_nlink(inode);
646 	d_instantiate(dentry, inode);
647 	inc_nlink(d_inode(dentry->d_parent));
648 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
649 	return tracefs_end_creating(dentry);
650 }
651 
652 /**
653  * tracefs_create_dir - create a directory in the tracefs filesystem
654  * @name: a pointer to a string containing the name of the directory to
655  *        create.
656  * @parent: a pointer to the parent dentry for this file.  This should be a
657  *          directory dentry if set.  If this parameter is NULL, then the
658  *          directory will be created in the root of the tracefs filesystem.
659  *
660  * This function creates a directory in tracefs with the given name.
661  *
662  * This function will return a pointer to a dentry if it succeeds.  This
663  * pointer must be passed to the tracefs_remove() function when the file is
664  * to be removed. If an error occurs, %NULL will be returned.
665  *
666  * If tracing is not enabled in the kernel, the value -%ENODEV will be
667  * returned.
668  */
669 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
670 {
671 	if (security_locked_down(LOCKDOWN_TRACEFS))
672 		return NULL;
673 
674 	return __create_dir(name, parent, &simple_dir_inode_operations);
675 }
676 
677 /**
678  * tracefs_create_instance_dir - create the tracing instances directory
679  * @name: The name of the instances directory to create
680  * @parent: The parent directory that the instances directory will exist
681  * @mkdir: The function to call when a mkdir is performed.
682  * @rmdir: The function to call when a rmdir is performed.
683  *
684  * Only one instances directory is allowed.
685  *
686  * The instances directory is special as it allows for mkdir and rmdir
687  * to be done by userspace. When a mkdir or rmdir is performed, the inode
688  * locks are released and the methods passed in (@mkdir and @rmdir) are
689  * called without locks and with the name of the directory being created
690  * within the instances directory.
691  *
692  * Returns the dentry of the instances directory.
693  */
694 __init struct dentry *tracefs_create_instance_dir(const char *name,
695 					  struct dentry *parent,
696 					  int (*mkdir)(const char *name),
697 					  int (*rmdir)(const char *name))
698 {
699 	struct dentry *dentry;
700 
701 	/* Only allow one instance of the instances directory. */
702 	if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
703 		return NULL;
704 
705 	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
706 	if (!dentry)
707 		return NULL;
708 
709 	tracefs_ops.mkdir = mkdir;
710 	tracefs_ops.rmdir = rmdir;
711 
712 	return dentry;
713 }
714 
715 static void remove_one(struct dentry *victim)
716 {
717 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
718 }
719 
720 /**
721  * tracefs_remove - recursively removes a directory
722  * @dentry: a pointer to a the dentry of the directory to be removed.
723  *
724  * This function recursively removes a directory tree in tracefs that
725  * was previously created with a call to another tracefs function
726  * (like tracefs_create_file() or variants thereof.)
727  */
728 void tracefs_remove(struct dentry *dentry)
729 {
730 	if (IS_ERR_OR_NULL(dentry))
731 		return;
732 
733 	simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
734 	simple_recursive_removal(dentry, remove_one);
735 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
736 }
737 
738 /**
739  * tracefs_initialized - Tells whether tracefs has been registered
740  */
741 bool tracefs_initialized(void)
742 {
743 	return tracefs_registered;
744 }
745 
746 static void init_once(void *foo)
747 {
748 	struct tracefs_inode *ti = (struct tracefs_inode *) foo;
749 
750 	inode_init_once(&ti->vfs_inode);
751 }
752 
753 static int __init tracefs_init(void)
754 {
755 	int retval;
756 
757 	tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
758 						 sizeof(struct tracefs_inode),
759 						 0, (SLAB_RECLAIM_ACCOUNT|
760 						     SLAB_MEM_SPREAD|
761 						     SLAB_ACCOUNT),
762 						 init_once);
763 	if (!tracefs_inode_cachep)
764 		return -ENOMEM;
765 
766 	retval = sysfs_create_mount_point(kernel_kobj, "tracing");
767 	if (retval)
768 		return -EINVAL;
769 
770 	retval = register_filesystem(&trace_fs_type);
771 	if (!retval)
772 		tracefs_registered = true;
773 
774 	return retval;
775 }
776 core_initcall(tracefs_init);
777