xref: /linux/fs/tracefs/inode.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
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 	struct tracefs_inode *ti;
95 	char *name;
96 	int ret;
97 
98 	name = get_dname(dentry);
99 	if (!name)
100 		return -ENOMEM;
101 
102 	/*
103 	 * This is a new directory that does not take the default of
104 	 * the rootfs. It becomes the default permissions for all the
105 	 * files and directories underneath it.
106 	 */
107 	ti = get_tracefs(inode);
108 	ti->flags |= TRACEFS_INSTANCE_INODE;
109 	ti->private = inode;
110 
111 	/*
112 	 * The mkdir call can call the generic functions that create
113 	 * the files within the tracefs system. It is up to the individual
114 	 * mkdir routine to handle races.
115 	 */
116 	inode_unlock(inode);
117 	ret = tracefs_ops.mkdir(name);
118 	inode_lock(inode);
119 
120 	kfree(name);
121 
122 	return ret;
123 }
124 
125 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
126 {
127 	char *name;
128 	int ret;
129 
130 	name = get_dname(dentry);
131 	if (!name)
132 		return -ENOMEM;
133 
134 	/*
135 	 * The rmdir call can call the generic functions that create
136 	 * the files within the tracefs system. It is up to the individual
137 	 * rmdir routine to handle races.
138 	 * This time we need to unlock not only the parent (inode) but
139 	 * also the directory that is being deleted.
140 	 */
141 	inode_unlock(inode);
142 	inode_unlock(d_inode(dentry));
143 
144 	ret = tracefs_ops.rmdir(name);
145 
146 	inode_lock_nested(inode, I_MUTEX_PARENT);
147 	inode_lock(d_inode(dentry));
148 
149 	kfree(name);
150 
151 	return ret;
152 }
153 
154 static void set_tracefs_inode_owner(struct inode *inode)
155 {
156 	struct tracefs_inode *ti = get_tracefs(inode);
157 	struct inode *root_inode = ti->private;
158 
159 	/*
160 	 * If this inode has never been referenced, then update
161 	 * the permissions to the superblock.
162 	 */
163 	if (!(ti->flags & TRACEFS_UID_PERM_SET))
164 		inode->i_uid = root_inode->i_uid;
165 
166 	if (!(ti->flags & TRACEFS_GID_PERM_SET))
167 		inode->i_gid = root_inode->i_gid;
168 }
169 
170 static int tracefs_permission(struct mnt_idmap *idmap,
171 			      struct inode *inode, int mask)
172 {
173 	set_tracefs_inode_owner(inode);
174 	return generic_permission(idmap, inode, mask);
175 }
176 
177 static int tracefs_getattr(struct mnt_idmap *idmap,
178 			   const struct path *path, struct kstat *stat,
179 			   u32 request_mask, unsigned int flags)
180 {
181 	struct inode *inode = d_backing_inode(path->dentry);
182 
183 	set_tracefs_inode_owner(inode);
184 	generic_fillattr(idmap, request_mask, inode, stat);
185 	return 0;
186 }
187 
188 static int tracefs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
189 			   struct iattr *attr)
190 {
191 	unsigned int ia_valid = attr->ia_valid;
192 	struct inode *inode = d_inode(dentry);
193 	struct tracefs_inode *ti = get_tracefs(inode);
194 
195 	if (ia_valid & ATTR_UID)
196 		ti->flags |= TRACEFS_UID_PERM_SET;
197 
198 	if (ia_valid & ATTR_GID)
199 		ti->flags |= TRACEFS_GID_PERM_SET;
200 
201 	return simple_setattr(idmap, dentry, attr);
202 }
203 
204 static const struct inode_operations tracefs_instance_dir_inode_operations = {
205 	.lookup		= simple_lookup,
206 	.mkdir		= tracefs_syscall_mkdir,
207 	.rmdir		= tracefs_syscall_rmdir,
208 	.permission	= tracefs_permission,
209 	.getattr	= tracefs_getattr,
210 	.setattr	= tracefs_setattr,
211 };
212 
213 static const struct inode_operations tracefs_dir_inode_operations = {
214 	.lookup		= simple_lookup,
215 	.permission	= tracefs_permission,
216 	.getattr	= tracefs_getattr,
217 	.setattr	= tracefs_setattr,
218 };
219 
220 static const struct inode_operations tracefs_file_inode_operations = {
221 	.permission	= tracefs_permission,
222 	.getattr	= tracefs_getattr,
223 	.setattr	= tracefs_setattr,
224 };
225 
226 struct inode *tracefs_get_inode(struct super_block *sb)
227 {
228 	struct inode *inode = new_inode(sb);
229 	if (inode) {
230 		inode->i_ino = get_next_ino();
231 		simple_inode_init_ts(inode);
232 	}
233 	return inode;
234 }
235 
236 struct tracefs_mount_opts {
237 	kuid_t uid;
238 	kgid_t gid;
239 	umode_t mode;
240 	/* Opt_* bitfield. */
241 	unsigned int opts;
242 };
243 
244 enum {
245 	Opt_uid,
246 	Opt_gid,
247 	Opt_mode,
248 	Opt_err
249 };
250 
251 static const match_table_t tokens = {
252 	{Opt_uid, "uid=%u"},
253 	{Opt_gid, "gid=%u"},
254 	{Opt_mode, "mode=%o"},
255 	{Opt_err, NULL}
256 };
257 
258 struct tracefs_fs_info {
259 	struct tracefs_mount_opts mount_opts;
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 		inode->i_gid = opts->gid;
336 
337 	return 0;
338 }
339 
340 static int tracefs_remount(struct super_block *sb, int *flags, char *data)
341 {
342 	int err;
343 	struct tracefs_fs_info *fsi = sb->s_fs_info;
344 
345 	sync_filesystem(sb);
346 	err = tracefs_parse_options(data, &fsi->mount_opts);
347 	if (err)
348 		goto fail;
349 
350 	tracefs_apply_options(sb, true);
351 
352 fail:
353 	return err;
354 }
355 
356 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
357 {
358 	struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
359 	struct tracefs_mount_opts *opts = &fsi->mount_opts;
360 
361 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
362 		seq_printf(m, ",uid=%u",
363 			   from_kuid_munged(&init_user_ns, opts->uid));
364 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
365 		seq_printf(m, ",gid=%u",
366 			   from_kgid_munged(&init_user_ns, opts->gid));
367 	if (opts->mode != TRACEFS_DEFAULT_MODE)
368 		seq_printf(m, ",mode=%o", opts->mode);
369 
370 	return 0;
371 }
372 
373 static const struct super_operations tracefs_super_operations = {
374 	.alloc_inode    = tracefs_alloc_inode,
375 	.free_inode     = tracefs_free_inode,
376 	.drop_inode     = generic_delete_inode,
377 	.statfs		= simple_statfs,
378 	.remount_fs	= tracefs_remount,
379 	.show_options	= tracefs_show_options,
380 };
381 
382 static void tracefs_dentry_iput(struct dentry *dentry, struct inode *inode)
383 {
384 	struct tracefs_inode *ti;
385 
386 	if (!dentry || !inode)
387 		return;
388 
389 	ti = get_tracefs(inode);
390 	if (ti && ti->flags & TRACEFS_EVENT_INODE)
391 		eventfs_set_ei_status_free(ti, dentry);
392 	iput(inode);
393 }
394 
395 static const struct dentry_operations tracefs_dentry_operations = {
396 	.d_iput = tracefs_dentry_iput,
397 };
398 
399 static int trace_fill_super(struct super_block *sb, void *data, int silent)
400 {
401 	static const struct tree_descr trace_files[] = {{""}};
402 	struct tracefs_fs_info *fsi;
403 	int err;
404 
405 	fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
406 	sb->s_fs_info = fsi;
407 	if (!fsi) {
408 		err = -ENOMEM;
409 		goto fail;
410 	}
411 
412 	err = tracefs_parse_options(data, &fsi->mount_opts);
413 	if (err)
414 		goto fail;
415 
416 	err  =  simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
417 	if (err)
418 		goto fail;
419 
420 	sb->s_op = &tracefs_super_operations;
421 	sb->s_d_op = &tracefs_dentry_operations;
422 
423 	tracefs_apply_options(sb, false);
424 
425 	return 0;
426 
427 fail:
428 	kfree(fsi);
429 	sb->s_fs_info = NULL;
430 	return err;
431 }
432 
433 static struct dentry *trace_mount(struct file_system_type *fs_type,
434 			int flags, const char *dev_name,
435 			void *data)
436 {
437 	return mount_single(fs_type, flags, data, trace_fill_super);
438 }
439 
440 static struct file_system_type trace_fs_type = {
441 	.owner =	THIS_MODULE,
442 	.name =		"tracefs",
443 	.mount =	trace_mount,
444 	.kill_sb =	kill_litter_super,
445 };
446 MODULE_ALIAS_FS("tracefs");
447 
448 struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
449 {
450 	struct dentry *dentry;
451 	int error;
452 
453 	pr_debug("tracefs: creating file '%s'\n",name);
454 
455 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
456 			      &tracefs_mount_count);
457 	if (error)
458 		return ERR_PTR(error);
459 
460 	/* If the parent is not specified, we create it in the root.
461 	 * We need the root dentry to do this, which is in the super
462 	 * block. A pointer to that is in the struct vfsmount that we
463 	 * have around.
464 	 */
465 	if (!parent)
466 		parent = tracefs_mount->mnt_root;
467 
468 	inode_lock(d_inode(parent));
469 	if (unlikely(IS_DEADDIR(d_inode(parent))))
470 		dentry = ERR_PTR(-ENOENT);
471 	else
472 		dentry = lookup_one_len(name, parent, strlen(name));
473 	if (!IS_ERR(dentry) && d_inode(dentry)) {
474 		dput(dentry);
475 		dentry = ERR_PTR(-EEXIST);
476 	}
477 
478 	if (IS_ERR(dentry)) {
479 		inode_unlock(d_inode(parent));
480 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
481 	}
482 
483 	return dentry;
484 }
485 
486 struct dentry *tracefs_failed_creating(struct dentry *dentry)
487 {
488 	inode_unlock(d_inode(dentry->d_parent));
489 	dput(dentry);
490 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
491 	return NULL;
492 }
493 
494 struct dentry *tracefs_end_creating(struct dentry *dentry)
495 {
496 	inode_unlock(d_inode(dentry->d_parent));
497 	return dentry;
498 }
499 
500 /**
501  * eventfs_start_creating - start the process of creating a dentry
502  * @name: Name of the file created for the dentry
503  * @parent: The parent dentry where this dentry will be created
504  *
505  * This is a simple helper function for the dynamically created eventfs
506  * files. When the directory of the eventfs files are accessed, their
507  * dentries are created on the fly. This function is used to start that
508  * process.
509  */
510 struct dentry *eventfs_start_creating(const char *name, struct dentry *parent)
511 {
512 	struct dentry *dentry;
513 	int error;
514 
515 	/* Must always have a parent. */
516 	if (WARN_ON_ONCE(!parent))
517 		return ERR_PTR(-EINVAL);
518 
519 	error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
520 			      &tracefs_mount_count);
521 	if (error)
522 		return ERR_PTR(error);
523 
524 	if (unlikely(IS_DEADDIR(parent->d_inode)))
525 		dentry = ERR_PTR(-ENOENT);
526 	else
527 		dentry = lookup_one_len(name, parent, strlen(name));
528 
529 	if (!IS_ERR(dentry) && dentry->d_inode) {
530 		dput(dentry);
531 		dentry = ERR_PTR(-EEXIST);
532 	}
533 
534 	if (IS_ERR(dentry))
535 		simple_release_fs(&tracefs_mount, &tracefs_mount_count);
536 
537 	return dentry;
538 }
539 
540 /**
541  * eventfs_failed_creating - clean up a failed eventfs dentry creation
542  * @dentry: The dentry to clean up
543  *
544  * If after calling eventfs_start_creating(), a failure is detected, the
545  * resources created by eventfs_start_creating() needs to be cleaned up. In
546  * that case, this function should be called to perform that clean up.
547  */
548 struct dentry *eventfs_failed_creating(struct dentry *dentry)
549 {
550 	dput(dentry);
551 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
552 	return NULL;
553 }
554 
555 /**
556  * eventfs_end_creating - Finish the process of creating a eventfs dentry
557  * @dentry: The dentry that has successfully been created.
558  *
559  * This function is currently just a place holder to match
560  * eventfs_start_creating(). In case any synchronization needs to be added,
561  * this function will be used to implement that without having to modify
562  * the callers of eventfs_start_creating().
563  */
564 struct dentry *eventfs_end_creating(struct dentry *dentry)
565 {
566 	return dentry;
567 }
568 
569 /* Find the inode that this will use for default */
570 static struct inode *instance_inode(struct dentry *parent, struct inode *inode)
571 {
572 	struct tracefs_inode *ti;
573 
574 	/* If parent is NULL then use root inode */
575 	if (!parent)
576 		return d_inode(inode->i_sb->s_root);
577 
578 	/* Find the inode that is flagged as an instance or the root inode */
579 	while (!IS_ROOT(parent)) {
580 		ti = get_tracefs(d_inode(parent));
581 		if (ti->flags & TRACEFS_INSTANCE_INODE)
582 			break;
583 		parent = parent->d_parent;
584 	}
585 
586 	return d_inode(parent);
587 }
588 
589 /**
590  * tracefs_create_file - create a file in the tracefs filesystem
591  * @name: a pointer to a string containing the name of the file to create.
592  * @mode: the permission that the file should have.
593  * @parent: a pointer to the parent dentry for this file.  This should be a
594  *          directory dentry if set.  If this parameter is NULL, then the
595  *          file will be created in the root of the tracefs filesystem.
596  * @data: a pointer to something that the caller will want to get to later
597  *        on.  The inode.i_private pointer will point to this value on
598  *        the open() call.
599  * @fops: a pointer to a struct file_operations that should be used for
600  *        this file.
601  *
602  * This is the basic "create a file" function for tracefs.  It allows for a
603  * wide range of flexibility in creating a file, or a directory (if you want
604  * to create a directory, the tracefs_create_dir() function is
605  * recommended to be used instead.)
606  *
607  * This function will return a pointer to a dentry if it succeeds.  This
608  * pointer must be passed to the tracefs_remove() function when the file is
609  * to be removed (no automatic cleanup happens if your module is unloaded,
610  * you are responsible here.)  If an error occurs, %NULL will be returned.
611  *
612  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
613  * returned.
614  */
615 struct dentry *tracefs_create_file(const char *name, umode_t mode,
616 				   struct dentry *parent, void *data,
617 				   const struct file_operations *fops)
618 {
619 	struct tracefs_inode *ti;
620 	struct dentry *dentry;
621 	struct inode *inode;
622 
623 	if (security_locked_down(LOCKDOWN_TRACEFS))
624 		return NULL;
625 
626 	if (!(mode & S_IFMT))
627 		mode |= S_IFREG;
628 	BUG_ON(!S_ISREG(mode));
629 	dentry = tracefs_start_creating(name, parent);
630 
631 	if (IS_ERR(dentry))
632 		return NULL;
633 
634 	inode = tracefs_get_inode(dentry->d_sb);
635 	if (unlikely(!inode))
636 		return tracefs_failed_creating(dentry);
637 
638 	ti = get_tracefs(inode);
639 	ti->private = instance_inode(parent, inode);
640 
641 	inode->i_mode = mode;
642 	inode->i_op = &tracefs_file_inode_operations;
643 	inode->i_fop = fops ? fops : &tracefs_file_operations;
644 	inode->i_private = data;
645 	inode->i_uid = d_inode(dentry->d_parent)->i_uid;
646 	inode->i_gid = d_inode(dentry->d_parent)->i_gid;
647 	d_instantiate(dentry, inode);
648 	fsnotify_create(d_inode(dentry->d_parent), dentry);
649 	return tracefs_end_creating(dentry);
650 }
651 
652 static struct dentry *__create_dir(const char *name, struct dentry *parent,
653 				   const struct inode_operations *ops)
654 {
655 	struct tracefs_inode *ti;
656 	struct dentry *dentry = tracefs_start_creating(name, parent);
657 	struct inode *inode;
658 
659 	if (IS_ERR(dentry))
660 		return NULL;
661 
662 	inode = tracefs_get_inode(dentry->d_sb);
663 	if (unlikely(!inode))
664 		return tracefs_failed_creating(dentry);
665 
666 	/* Do not set bits for OTH */
667 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
668 	inode->i_op = ops;
669 	inode->i_fop = &simple_dir_operations;
670 	inode->i_uid = d_inode(dentry->d_parent)->i_uid;
671 	inode->i_gid = d_inode(dentry->d_parent)->i_gid;
672 
673 	ti = get_tracefs(inode);
674 	ti->private = instance_inode(parent, inode);
675 
676 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
677 	inc_nlink(inode);
678 	d_instantiate(dentry, inode);
679 	inc_nlink(d_inode(dentry->d_parent));
680 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
681 	return tracefs_end_creating(dentry);
682 }
683 
684 /**
685  * tracefs_create_dir - create a directory in the tracefs filesystem
686  * @name: a pointer to a string containing the name of the directory to
687  *        create.
688  * @parent: a pointer to the parent dentry for this file.  This should be a
689  *          directory dentry if set.  If this parameter is NULL, then the
690  *          directory will be created in the root of the tracefs filesystem.
691  *
692  * This function creates a directory in tracefs with the given name.
693  *
694  * This function will return a pointer to a dentry if it succeeds.  This
695  * pointer must be passed to the tracefs_remove() function when the file is
696  * to be removed. If an error occurs, %NULL will be returned.
697  *
698  * If tracing is not enabled in the kernel, the value -%ENODEV will be
699  * returned.
700  */
701 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
702 {
703 	if (security_locked_down(LOCKDOWN_TRACEFS))
704 		return NULL;
705 
706 	return __create_dir(name, parent, &tracefs_dir_inode_operations);
707 }
708 
709 /**
710  * tracefs_create_instance_dir - create the tracing instances directory
711  * @name: The name of the instances directory to create
712  * @parent: The parent directory that the instances directory will exist
713  * @mkdir: The function to call when a mkdir is performed.
714  * @rmdir: The function to call when a rmdir is performed.
715  *
716  * Only one instances directory is allowed.
717  *
718  * The instances directory is special as it allows for mkdir and rmdir
719  * to be done by userspace. When a mkdir or rmdir is performed, the inode
720  * locks are released and the methods passed in (@mkdir and @rmdir) are
721  * called without locks and with the name of the directory being created
722  * within the instances directory.
723  *
724  * Returns the dentry of the instances directory.
725  */
726 __init struct dentry *tracefs_create_instance_dir(const char *name,
727 					  struct dentry *parent,
728 					  int (*mkdir)(const char *name),
729 					  int (*rmdir)(const char *name))
730 {
731 	struct dentry *dentry;
732 
733 	/* Only allow one instance of the instances directory. */
734 	if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
735 		return NULL;
736 
737 	dentry = __create_dir(name, parent, &tracefs_instance_dir_inode_operations);
738 	if (!dentry)
739 		return NULL;
740 
741 	tracefs_ops.mkdir = mkdir;
742 	tracefs_ops.rmdir = rmdir;
743 
744 	return dentry;
745 }
746 
747 static void remove_one(struct dentry *victim)
748 {
749 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
750 }
751 
752 /**
753  * tracefs_remove - recursively removes a directory
754  * @dentry: a pointer to a the dentry of the directory to be removed.
755  *
756  * This function recursively removes a directory tree in tracefs that
757  * was previously created with a call to another tracefs function
758  * (like tracefs_create_file() or variants thereof.)
759  */
760 void tracefs_remove(struct dentry *dentry)
761 {
762 	if (IS_ERR_OR_NULL(dentry))
763 		return;
764 
765 	simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
766 	simple_recursive_removal(dentry, remove_one);
767 	simple_release_fs(&tracefs_mount, &tracefs_mount_count);
768 }
769 
770 /**
771  * tracefs_initialized - Tells whether tracefs has been registered
772  */
773 bool tracefs_initialized(void)
774 {
775 	return tracefs_registered;
776 }
777 
778 static void init_once(void *foo)
779 {
780 	struct tracefs_inode *ti = (struct tracefs_inode *) foo;
781 
782 	inode_init_once(&ti->vfs_inode);
783 }
784 
785 static int __init tracefs_init(void)
786 {
787 	int retval;
788 
789 	tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
790 						 sizeof(struct tracefs_inode),
791 						 0, (SLAB_RECLAIM_ACCOUNT|
792 						     SLAB_MEM_SPREAD|
793 						     SLAB_ACCOUNT),
794 						 init_once);
795 	if (!tracefs_inode_cachep)
796 		return -ENOMEM;
797 
798 	retval = sysfs_create_mount_point(kernel_kobj, "tracing");
799 	if (retval)
800 		return -EINVAL;
801 
802 	retval = register_filesystem(&trace_fs_type);
803 	if (!retval)
804 		tracefs_registered = true;
805 
806 	return retval;
807 }
808 core_initcall(tracefs_init);
809