xref: /linux/fs/tracefs/inode.c (revision c4b5140c6eac2f757d9706c6c783b60554c48cb7)
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 		inode->i_atime = inode->i_mtime = inode_set_ctime_current(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_ef_status_free(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 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
513 			      &tracefs_mount_count);
514 	if (error)
515 		return ERR_PTR(error);
516 
517 	/*
518 	 * If the parent is not specified, we create it in the root.
519 	 * We need the root dentry to do this, which is in the super
520 	 * block. A pointer to that is in the struct vfsmount that we
521 	 * have around.
522 	 */
523 	if (!parent)
524 		parent = tracefs_mount->mnt_root;
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 	return __create_dir(name, parent, &simple_dir_inode_operations);
677 }
678 
679 /**
680  * tracefs_create_instance_dir - create the tracing instances directory
681  * @name: The name of the instances directory to create
682  * @parent: The parent directory that the instances directory will exist
683  * @mkdir: The function to call when a mkdir is performed.
684  * @rmdir: The function to call when a rmdir is performed.
685  *
686  * Only one instances directory is allowed.
687  *
688  * The instances directory is special as it allows for mkdir and rmdir
689  * to be done by userspace. When a mkdir or rmdir is performed, the inode
690  * locks are released and the methods passed in (@mkdir and @rmdir) are
691  * called without locks and with the name of the directory being created
692  * within the instances directory.
693  *
694  * Returns the dentry of the instances directory.
695  */
696 __init struct dentry *tracefs_create_instance_dir(const char *name,
697 					  struct dentry *parent,
698 					  int (*mkdir)(const char *name),
699 					  int (*rmdir)(const char *name))
700 {
701 	struct dentry *dentry;
702 
703 	/* Only allow one instance of the instances directory. */
704 	if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
705 		return NULL;
706 
707 	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
708 	if (!dentry)
709 		return NULL;
710 
711 	tracefs_ops.mkdir = mkdir;
712 	tracefs_ops.rmdir = rmdir;
713 
714 	return dentry;
715 }
716 
717 static void remove_one(struct dentry *victim)
718 {
719 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
720 }
721 
722 /**
723  * tracefs_remove - recursively removes a directory
724  * @dentry: a pointer to a the dentry of the directory to be removed.
725  *
726  * This function recursively removes a directory tree in tracefs that
727  * was previously created with a call to another tracefs function
728  * (like tracefs_create_file() or variants thereof.)
729  */
730 void tracefs_remove(struct dentry *dentry)
731 {
732 	if (IS_ERR_OR_NULL(dentry))
733 		return;
734 
735 	simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
736 	simple_recursive_removal(dentry, remove_one);
737 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
738 }
739 
740 /**
741  * tracefs_initialized - Tells whether tracefs has been registered
742  */
743 bool tracefs_initialized(void)
744 {
745 	return tracefs_registered;
746 }
747 
748 static void init_once(void *foo)
749 {
750 	struct tracefs_inode *ti = (struct tracefs_inode *) foo;
751 
752 	inode_init_once(&ti->vfs_inode);
753 }
754 
755 static int __init tracefs_init(void)
756 {
757 	int retval;
758 
759 	tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
760 						 sizeof(struct tracefs_inode),
761 						 0, (SLAB_RECLAIM_ACCOUNT|
762 						     SLAB_MEM_SPREAD|
763 						     SLAB_ACCOUNT),
764 						 init_once);
765 	if (!tracefs_inode_cachep)
766 		return -ENOMEM;
767 
768 	retval = sysfs_create_mount_point(kernel_kobj, "tracing");
769 	if (retval)
770 		return -EINVAL;
771 
772 	retval = register_filesystem(&trace_fs_type);
773 	if (!retval)
774 		tracefs_registered = true;
775 
776 	return retval;
777 }
778 core_initcall(tracefs_init);
779