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