xref: /openbmc/linux/fs/tracefs/event_inode.c (revision f8a3db47)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  event_inode.c - part of tracefs, a pseudo file system for activating tracing
4  *
5  *  Copyright (C) 2020-23 VMware Inc, author: Steven Rostedt (VMware) <rostedt@goodmis.org>
6  *  Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com>
7  *
8  *  eventfs is used to dynamically create inodes and dentries based on the
9  *  meta data provided by the tracing system.
10  *
11  *  eventfs stores the meta-data of files/dirs and holds off on creating
12  *  inodes/dentries of the files. When accessed, the eventfs will create the
13  *  inodes/dentries in a just-in-time (JIT) manner. The eventfs will clean up
14  *  and delete the inodes/dentries when they are no longer referenced.
15  */
16 #include <linux/fsnotify.h>
17 #include <linux/fs.h>
18 #include <linux/namei.h>
19 #include <linux/workqueue.h>
20 #include <linux/security.h>
21 #include <linux/tracefs.h>
22 #include <linux/kref.h>
23 #include <linux/delay.h>
24 #include "internal.h"
25 
26 struct eventfs_inode {
27 	struct list_head	e_top_files;
28 };
29 
30 /*
31  * struct eventfs_file - hold the properties of the eventfs files and
32  *                       directories.
33  * @name:	the name of the file or directory to create
34  * @d_parent:   holds parent's dentry
35  * @dentry:     once accessed holds dentry
36  * @list:	file or directory to be added to parent directory
37  * @ei:		list of files and directories within directory
38  * @fop:	file_operations for file or directory
39  * @iop:	inode_operations for file or directory
40  * @data:	something that the caller will want to get to later on
41  * @mode:	the permission that the file or directory should have
42  */
43 struct eventfs_file {
44 	const char			*name;
45 	struct dentry			*d_parent;
46 	struct dentry			*dentry;
47 	struct list_head		list;
48 	struct eventfs_inode		*ei;
49 	const struct file_operations	*fop;
50 	const struct inode_operations	*iop;
51 	/*
52 	 * Union - used for deletion
53 	 * @del_list:	list of eventfs_file to delete
54 	 * @rcu:	eventfs_file to delete in RCU
55 	 * @is_freed:	node is freed if one of the above is set
56 	 */
57 	union {
58 		struct list_head	del_list;
59 		struct rcu_head		rcu;
60 		unsigned long		is_freed;
61 	};
62 	void				*data;
63 	umode_t				mode;
64 };
65 
66 static DEFINE_MUTEX(eventfs_mutex);
67 DEFINE_STATIC_SRCU(eventfs_srcu);
68 
69 static struct dentry *eventfs_root_lookup(struct inode *dir,
70 					  struct dentry *dentry,
71 					  unsigned int flags);
72 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file);
73 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx);
74 static int eventfs_release(struct inode *inode, struct file *file);
75 
76 static const struct inode_operations eventfs_root_dir_inode_operations = {
77 	.lookup		= eventfs_root_lookup,
78 };
79 
80 static const struct file_operations eventfs_file_operations = {
81 	.open		= dcache_dir_open_wrapper,
82 	.read		= generic_read_dir,
83 	.iterate_shared	= dcache_readdir_wrapper,
84 	.llseek		= generic_file_llseek,
85 	.release	= eventfs_release,
86 };
87 
88 /**
89  * create_file - create a file in the tracefs filesystem
90  * @name: the name of the file to create.
91  * @mode: the permission that the file should have.
92  * @parent: parent dentry for this file.
93  * @data: something that the caller will want to get to later on.
94  * @fop: struct file_operations that should be used for this file.
95  *
96  * This is the basic "create a file" function for tracefs.  It allows for a
97  * wide range of flexibility in creating a file.
98  *
99  * This function will return a pointer to a dentry if it succeeds.  This
100  * pointer must be passed to the tracefs_remove() function when the file is
101  * to be removed (no automatic cleanup happens if your module is unloaded,
102  * you are responsible here.)  If an error occurs, %NULL will be returned.
103  *
104  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
105  * returned.
106  */
107 static struct dentry *create_file(const char *name, umode_t mode,
108 				  struct dentry *parent, void *data,
109 				  const struct file_operations *fop)
110 {
111 	struct tracefs_inode *ti;
112 	struct dentry *dentry;
113 	struct inode *inode;
114 
115 	if (!(mode & S_IFMT))
116 		mode |= S_IFREG;
117 
118 	if (WARN_ON_ONCE(!S_ISREG(mode)))
119 		return NULL;
120 
121 	dentry = eventfs_start_creating(name, parent);
122 
123 	if (IS_ERR(dentry))
124 		return dentry;
125 
126 	inode = tracefs_get_inode(dentry->d_sb);
127 	if (unlikely(!inode))
128 		return eventfs_failed_creating(dentry);
129 
130 	inode->i_mode = mode;
131 	inode->i_fop = fop;
132 	inode->i_private = data;
133 
134 	ti = get_tracefs(inode);
135 	ti->flags |= TRACEFS_EVENT_INODE;
136 	d_instantiate(dentry, inode);
137 	fsnotify_create(dentry->d_parent->d_inode, dentry);
138 	return eventfs_end_creating(dentry);
139 };
140 
141 /**
142  * create_dir - create a dir in the tracefs filesystem
143  * @name: the name of the file to create.
144  * @parent: parent dentry for this file.
145  * @data: something that the caller will want to get to later on.
146  *
147  * This is the basic "create a dir" function for eventfs.  It allows for a
148  * wide range of flexibility in creating a dir.
149  *
150  * This function will return a pointer to a dentry if it succeeds.  This
151  * pointer must be passed to the tracefs_remove() function when the file is
152  * to be removed (no automatic cleanup happens if your module is unloaded,
153  * you are responsible here.)  If an error occurs, %NULL will be returned.
154  *
155  * If tracefs is not enabled in the kernel, the value -%ENODEV will be
156  * returned.
157  */
158 static struct dentry *create_dir(const char *name, struct dentry *parent, void *data)
159 {
160 	struct tracefs_inode *ti;
161 	struct dentry *dentry;
162 	struct inode *inode;
163 
164 	dentry = eventfs_start_creating(name, parent);
165 	if (IS_ERR(dentry))
166 		return dentry;
167 
168 	inode = tracefs_get_inode(dentry->d_sb);
169 	if (unlikely(!inode))
170 		return eventfs_failed_creating(dentry);
171 
172 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
173 	inode->i_op = &eventfs_root_dir_inode_operations;
174 	inode->i_fop = &eventfs_file_operations;
175 	inode->i_private = data;
176 
177 	ti = get_tracefs(inode);
178 	ti->flags |= TRACEFS_EVENT_INODE;
179 
180 	inc_nlink(inode);
181 	d_instantiate(dentry, inode);
182 	inc_nlink(dentry->d_parent->d_inode);
183 	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
184 	return eventfs_end_creating(dentry);
185 }
186 
187 /**
188  * eventfs_set_ef_status_free - set the ef->status to free
189  * @ti: the tracefs_inode of the dentry
190  * @dentry: dentry who's status to be freed
191  *
192  * eventfs_set_ef_status_free will be called if no more
193  * references remain
194  */
195 void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry)
196 {
197 	struct tracefs_inode *ti_parent;
198 	struct eventfs_inode *ei;
199 	struct eventfs_file *ef, *tmp;
200 
201 	/* The top level events directory may be freed by this */
202 	if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) {
203 		LIST_HEAD(ef_del_list);
204 
205 		mutex_lock(&eventfs_mutex);
206 
207 		ei = ti->private;
208 
209 		/* Record all the top level files */
210 		list_for_each_entry_srcu(ef, &ei->e_top_files, list,
211 					 lockdep_is_held(&eventfs_mutex)) {
212 			list_add_tail(&ef->del_list, &ef_del_list);
213 		}
214 
215 		/* Nothing should access this, but just in case! */
216 		ti->private = NULL;
217 
218 		mutex_unlock(&eventfs_mutex);
219 
220 		/* Now safely free the top level files and their children */
221 		list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
222 			list_del(&ef->del_list);
223 			eventfs_remove(ef);
224 		}
225 
226 		kfree(ei);
227 		return;
228 	}
229 
230 	mutex_lock(&eventfs_mutex);
231 
232 	ti_parent = get_tracefs(dentry->d_parent->d_inode);
233 	if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
234 		goto out;
235 
236 	ef = dentry->d_fsdata;
237 	if (!ef)
238 		goto out;
239 
240 	/*
241 	 * If ef was freed, then the LSB bit is set for d_fsdata.
242 	 * But this should not happen, as it should still have a
243 	 * ref count that prevents it. Warn in case it does.
244 	 */
245 	if (WARN_ON_ONCE((unsigned long)ef & 1))
246 		goto out;
247 
248 	dentry->d_fsdata = NULL;
249 	ef->dentry = NULL;
250 out:
251 	mutex_unlock(&eventfs_mutex);
252 }
253 
254 /**
255  * eventfs_post_create_dir - post create dir routine
256  * @ef: eventfs_file of recently created dir
257  *
258  * Map the meta-data of files within an eventfs dir to their parent dentry
259  */
260 static void eventfs_post_create_dir(struct eventfs_file *ef)
261 {
262 	struct eventfs_file *ef_child;
263 	struct tracefs_inode *ti;
264 
265 	/* srcu lock already held */
266 	/* fill parent-child relation */
267 	list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
268 				 srcu_read_lock_held(&eventfs_srcu)) {
269 		ef_child->d_parent = ef->dentry;
270 	}
271 
272 	ti = get_tracefs(ef->dentry->d_inode);
273 	ti->private = ef->ei;
274 }
275 
276 /**
277  * create_dentry - helper function to create dentry
278  * @ef: eventfs_file of file or directory to create
279  * @parent: parent dentry
280  * @lookup: true if called from lookup routine
281  *
282  * Used to create a dentry for file/dir, executes post dentry creation routine
283  */
284 static struct dentry *
285 create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup)
286 {
287 	bool invalidate = false;
288 	struct dentry *dentry;
289 
290 	mutex_lock(&eventfs_mutex);
291 	if (ef->is_freed) {
292 		mutex_unlock(&eventfs_mutex);
293 		return NULL;
294 	}
295 	if (ef->dentry) {
296 		dentry = ef->dentry;
297 		/* On dir open, up the ref count */
298 		if (!lookup)
299 			dget(dentry);
300 		mutex_unlock(&eventfs_mutex);
301 		return dentry;
302 	}
303 	mutex_unlock(&eventfs_mutex);
304 
305 	if (!lookup)
306 		inode_lock(parent->d_inode);
307 
308 	if (ef->ei)
309 		dentry = create_dir(ef->name, parent, ef->data);
310 	else
311 		dentry = create_file(ef->name, ef->mode, parent,
312 				     ef->data, ef->fop);
313 
314 	if (!lookup)
315 		inode_unlock(parent->d_inode);
316 
317 	mutex_lock(&eventfs_mutex);
318 	if (IS_ERR_OR_NULL(dentry)) {
319 		/* If the ef was already updated get it */
320 		dentry = ef->dentry;
321 		if (dentry && !lookup)
322 			dget(dentry);
323 		mutex_unlock(&eventfs_mutex);
324 		return dentry;
325 	}
326 
327 	if (!ef->dentry && !ef->is_freed) {
328 		ef->dentry = dentry;
329 		if (ef->ei)
330 			eventfs_post_create_dir(ef);
331 		dentry->d_fsdata = ef;
332 	} else {
333 		/* A race here, should try again (unless freed) */
334 		invalidate = true;
335 
336 		/*
337 		 * Should never happen unless we get here due to being freed.
338 		 * Otherwise it means two dentries exist with the same name.
339 		 */
340 		WARN_ON_ONCE(!ef->is_freed);
341 	}
342 	mutex_unlock(&eventfs_mutex);
343 	if (invalidate)
344 		d_invalidate(dentry);
345 
346 	if (lookup || invalidate)
347 		dput(dentry);
348 
349 	return invalidate ? NULL : dentry;
350 }
351 
352 static bool match_event_file(struct eventfs_file *ef, const char *name)
353 {
354 	bool ret;
355 
356 	mutex_lock(&eventfs_mutex);
357 	ret = !ef->is_freed && strcmp(ef->name, name) == 0;
358 	mutex_unlock(&eventfs_mutex);
359 
360 	return ret;
361 }
362 
363 /**
364  * eventfs_root_lookup - lookup routine to create file/dir
365  * @dir: in which a lookup is being done
366  * @dentry: file/dir dentry
367  * @flags: to pass as flags parameter to simple lookup
368  *
369  * Used to create a dynamic file/dir within @dir. Use the eventfs_inode
370  * list of meta data to find the information needed to create the file/dir.
371  */
372 static struct dentry *eventfs_root_lookup(struct inode *dir,
373 					  struct dentry *dentry,
374 					  unsigned int flags)
375 {
376 	struct tracefs_inode *ti;
377 	struct eventfs_inode *ei;
378 	struct eventfs_file *ef;
379 	struct dentry *ret = NULL;
380 	int idx;
381 
382 	ti = get_tracefs(dir);
383 	if (!(ti->flags & TRACEFS_EVENT_INODE))
384 		return NULL;
385 
386 	ei = ti->private;
387 	idx = srcu_read_lock(&eventfs_srcu);
388 	list_for_each_entry_srcu(ef, &ei->e_top_files, list,
389 				 srcu_read_lock_held(&eventfs_srcu)) {
390 		if (!match_event_file(ef, dentry->d_name.name))
391 			continue;
392 		ret = simple_lookup(dir, dentry, flags);
393 		create_dentry(ef, ef->d_parent, true);
394 		break;
395 	}
396 	srcu_read_unlock(&eventfs_srcu, idx);
397 	return ret;
398 }
399 
400 struct dentry_list {
401 	void			*cursor;
402 	struct dentry		**dentries;
403 };
404 
405 /**
406  * eventfs_release - called to release eventfs file/dir
407  * @inode: inode to be released
408  * @file: file to be released (not used)
409  */
410 static int eventfs_release(struct inode *inode, struct file *file)
411 {
412 	struct tracefs_inode *ti;
413 	struct dentry_list *dlist = file->private_data;
414 	void *cursor;
415 	int i;
416 
417 	ti = get_tracefs(inode);
418 	if (!(ti->flags & TRACEFS_EVENT_INODE))
419 		return -EINVAL;
420 
421 	if (WARN_ON_ONCE(!dlist))
422 		return -EINVAL;
423 
424 	for (i = 0; dlist->dentries && dlist->dentries[i]; i++) {
425 		dput(dlist->dentries[i]);
426 	}
427 
428 	cursor = dlist->cursor;
429 	kfree(dlist->dentries);
430 	kfree(dlist);
431 	file->private_data = cursor;
432 	return dcache_dir_close(inode, file);
433 }
434 
435 /**
436  * dcache_dir_open_wrapper - eventfs open wrapper
437  * @inode: not used
438  * @file: dir to be opened (to create its child)
439  *
440  * Used to dynamically create the file/dir within @file. @file is really a
441  * directory and all the files/dirs of the children within @file will be
442  * created. If any of the files/dirs have already been created, their
443  * reference count will be incremented.
444  */
445 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
446 {
447 	struct tracefs_inode *ti;
448 	struct eventfs_inode *ei;
449 	struct eventfs_file *ef;
450 	struct dentry_list *dlist;
451 	struct dentry **dentries = NULL;
452 	struct dentry *dentry = file_dentry(file);
453 	struct dentry *d;
454 	struct inode *f_inode = file_inode(file);
455 	int cnt = 0;
456 	int idx;
457 	int ret;
458 
459 	ti = get_tracefs(f_inode);
460 	if (!(ti->flags & TRACEFS_EVENT_INODE))
461 		return -EINVAL;
462 
463 	if (WARN_ON_ONCE(file->private_data))
464 		return -EINVAL;
465 
466 	dlist = kmalloc(sizeof(*dlist), GFP_KERNEL);
467 	if (!dlist)
468 		return -ENOMEM;
469 
470 	ei = ti->private;
471 	idx = srcu_read_lock(&eventfs_srcu);
472 	list_for_each_entry_srcu(ef, &ei->e_top_files, list,
473 				 srcu_read_lock_held(&eventfs_srcu)) {
474 		d = create_dentry(ef, dentry, false);
475 		if (d) {
476 			struct dentry **tmp;
477 
478 			tmp = krealloc(dentries, sizeof(d) * (cnt + 2), GFP_KERNEL);
479 			if (!tmp)
480 				break;
481 			tmp[cnt] = d;
482 			tmp[cnt + 1] = NULL;
483 			cnt++;
484 			dentries = tmp;
485 		}
486 	}
487 	srcu_read_unlock(&eventfs_srcu, idx);
488 	ret = dcache_dir_open(inode, file);
489 
490 	/*
491 	 * dcache_dir_open() sets file->private_data to a dentry cursor.
492 	 * Need to save that but also save all the dentries that were
493 	 * opened by this function.
494 	 */
495 	dlist->cursor = file->private_data;
496 	dlist->dentries = dentries;
497 	file->private_data = dlist;
498 	return ret;
499 }
500 
501 /*
502  * This just sets the file->private_data back to the cursor and back.
503  */
504 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx)
505 {
506 	struct dentry_list *dlist = file->private_data;
507 	int ret;
508 
509 	file->private_data = dlist->cursor;
510 	ret = dcache_readdir(file, ctx);
511 	dlist->cursor = file->private_data;
512 	file->private_data = dlist;
513 	return ret;
514 }
515 
516 /**
517  * eventfs_prepare_ef - helper function to prepare eventfs_file
518  * @name: the name of the file/directory to create.
519  * @mode: the permission that the file should have.
520  * @fop: struct file_operations that should be used for this file/directory.
521  * @iop: struct inode_operations that should be used for this file/directory.
522  * @data: something that the caller will want to get to later on. The
523  *        inode.i_private pointer will point to this value on the open() call.
524  *
525  * This function allocates and fills the eventfs_file structure.
526  */
527 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode,
528 					const struct file_operations *fop,
529 					const struct inode_operations *iop,
530 					void *data)
531 {
532 	struct eventfs_file *ef;
533 
534 	ef = kzalloc(sizeof(*ef), GFP_KERNEL);
535 	if (!ef)
536 		return ERR_PTR(-ENOMEM);
537 
538 	ef->name = kstrdup(name, GFP_KERNEL);
539 	if (!ef->name) {
540 		kfree(ef);
541 		return ERR_PTR(-ENOMEM);
542 	}
543 
544 	if (S_ISDIR(mode)) {
545 		ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL);
546 		if (!ef->ei) {
547 			kfree(ef->name);
548 			kfree(ef);
549 			return ERR_PTR(-ENOMEM);
550 		}
551 		INIT_LIST_HEAD(&ef->ei->e_top_files);
552 	} else {
553 		ef->ei = NULL;
554 	}
555 
556 	ef->iop = iop;
557 	ef->fop = fop;
558 	ef->mode = mode;
559 	ef->data = data;
560 	return ef;
561 }
562 
563 /**
564  * eventfs_create_events_dir - create the trace event structure
565  * @name: the name of the directory to create.
566  * @parent: parent dentry for this file.  This should be a directory dentry
567  *          if set.  If this parameter is NULL, then the directory will be
568  *          created in the root of the tracefs filesystem.
569  *
570  * This function creates the top of the trace event directory.
571  */
572 struct dentry *eventfs_create_events_dir(const char *name,
573 					 struct dentry *parent)
574 {
575 	struct dentry *dentry = tracefs_start_creating(name, parent);
576 	struct eventfs_inode *ei;
577 	struct tracefs_inode *ti;
578 	struct inode *inode;
579 
580 	if (security_locked_down(LOCKDOWN_TRACEFS))
581 		return NULL;
582 
583 	if (IS_ERR(dentry))
584 		return dentry;
585 
586 	ei = kzalloc(sizeof(*ei), GFP_KERNEL);
587 	if (!ei)
588 		return ERR_PTR(-ENOMEM);
589 	inode = tracefs_get_inode(dentry->d_sb);
590 	if (unlikely(!inode)) {
591 		kfree(ei);
592 		tracefs_failed_creating(dentry);
593 		return ERR_PTR(-ENOMEM);
594 	}
595 
596 	INIT_LIST_HEAD(&ei->e_top_files);
597 
598 	ti = get_tracefs(inode);
599 	ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
600 	ti->private = ei;
601 
602 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
603 	inode->i_op = &eventfs_root_dir_inode_operations;
604 	inode->i_fop = &eventfs_file_operations;
605 
606 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
607 	inc_nlink(inode);
608 	d_instantiate(dentry, inode);
609 	inc_nlink(dentry->d_parent->d_inode);
610 	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
611 	return tracefs_end_creating(dentry);
612 }
613 
614 /**
615  * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later
616  * @name: the name of the file to create.
617  * @parent: parent dentry for this dir.
618  *
619  * This function adds eventfs subsystem dir to list.
620  * And all these dirs are created on the fly when they are looked up,
621  * and the dentry and inodes will be removed when they are done.
622  */
623 struct eventfs_file *eventfs_add_subsystem_dir(const char *name,
624 					       struct dentry *parent)
625 {
626 	struct tracefs_inode *ti_parent;
627 	struct eventfs_inode *ei_parent;
628 	struct eventfs_file *ef;
629 
630 	if (security_locked_down(LOCKDOWN_TRACEFS))
631 		return NULL;
632 
633 	if (!parent)
634 		return ERR_PTR(-EINVAL);
635 
636 	ti_parent = get_tracefs(parent->d_inode);
637 	ei_parent = ti_parent->private;
638 
639 	ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
640 	if (IS_ERR(ef))
641 		return ef;
642 
643 	mutex_lock(&eventfs_mutex);
644 	list_add_tail(&ef->list, &ei_parent->e_top_files);
645 	ef->d_parent = parent;
646 	mutex_unlock(&eventfs_mutex);
647 	return ef;
648 }
649 
650 /**
651  * eventfs_add_dir - add eventfs dir to list to create later
652  * @name: the name of the file to create.
653  * @ef_parent: parent eventfs_file for this dir.
654  *
655  * This function adds eventfs dir to list.
656  * And all these dirs are created on the fly when they are looked up,
657  * and the dentry and inodes will be removed when they are done.
658  */
659 struct eventfs_file *eventfs_add_dir(const char *name,
660 				     struct eventfs_file *ef_parent)
661 {
662 	struct eventfs_file *ef;
663 
664 	if (security_locked_down(LOCKDOWN_TRACEFS))
665 		return NULL;
666 
667 	if (!ef_parent)
668 		return ERR_PTR(-EINVAL);
669 
670 	ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
671 	if (IS_ERR(ef))
672 		return ef;
673 
674 	mutex_lock(&eventfs_mutex);
675 	list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
676 	ef->d_parent = ef_parent->dentry;
677 	mutex_unlock(&eventfs_mutex);
678 	return ef;
679 }
680 
681 /**
682  * eventfs_add_events_file - add the data needed to create a file for later reference
683  * @name: the name of the file to create.
684  * @mode: the permission that the file should have.
685  * @parent: parent dentry for this file.
686  * @data: something that the caller will want to get to later on.
687  * @fop: struct file_operations that should be used for this file.
688  *
689  * This function is used to add the information needed to create a
690  * dentry/inode within the top level events directory. The file created
691  * will have the @mode permissions. The @data will be used to fill the
692  * inode.i_private when the open() call is done. The dentry and inodes are
693  * all created when they are referenced, and removed when they are no
694  * longer referenced.
695  */
696 int eventfs_add_events_file(const char *name, umode_t mode,
697 			 struct dentry *parent, void *data,
698 			 const struct file_operations *fop)
699 {
700 	struct tracefs_inode *ti;
701 	struct eventfs_inode *ei;
702 	struct eventfs_file *ef;
703 
704 	if (security_locked_down(LOCKDOWN_TRACEFS))
705 		return -ENODEV;
706 
707 	if (!parent)
708 		return -EINVAL;
709 
710 	if (!(mode & S_IFMT))
711 		mode |= S_IFREG;
712 
713 	if (!parent->d_inode)
714 		return -EINVAL;
715 
716 	ti = get_tracefs(parent->d_inode);
717 	if (!(ti->flags & TRACEFS_EVENT_INODE))
718 		return -EINVAL;
719 
720 	ei = ti->private;
721 	ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
722 
723 	if (IS_ERR(ef))
724 		return -ENOMEM;
725 
726 	mutex_lock(&eventfs_mutex);
727 	list_add_tail(&ef->list, &ei->e_top_files);
728 	ef->d_parent = parent;
729 	mutex_unlock(&eventfs_mutex);
730 	return 0;
731 }
732 
733 /**
734  * eventfs_add_file - add eventfs file to list to create later
735  * @name: the name of the file to create.
736  * @mode: the permission that the file should have.
737  * @ef_parent: parent eventfs_file for this file.
738  * @data: something that the caller will want to get to later on.
739  * @fop: struct file_operations that should be used for this file.
740  *
741  * This function is used to add the information needed to create a
742  * file within a subdirectory of the events directory. The file created
743  * will have the @mode permissions. The @data will be used to fill the
744  * inode.i_private when the open() call is done. The dentry and inodes are
745  * all created when they are referenced, and removed when they are no
746  * longer referenced.
747  */
748 int eventfs_add_file(const char *name, umode_t mode,
749 		     struct eventfs_file *ef_parent,
750 		     void *data,
751 		     const struct file_operations *fop)
752 {
753 	struct eventfs_file *ef;
754 
755 	if (security_locked_down(LOCKDOWN_TRACEFS))
756 		return -ENODEV;
757 
758 	if (!ef_parent)
759 		return -EINVAL;
760 
761 	if (!(mode & S_IFMT))
762 		mode |= S_IFREG;
763 
764 	ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
765 	if (IS_ERR(ef))
766 		return -ENOMEM;
767 
768 	mutex_lock(&eventfs_mutex);
769 	list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
770 	ef->d_parent = ef_parent->dentry;
771 	mutex_unlock(&eventfs_mutex);
772 	return 0;
773 }
774 
775 static void free_ef(struct rcu_head *head)
776 {
777 	struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu);
778 
779 	kfree(ef->name);
780 	kfree(ef->ei);
781 	kfree(ef);
782 }
783 
784 /**
785  * eventfs_remove_rec - remove eventfs dir or file from list
786  * @ef: eventfs_file to be removed.
787  * @head: to create list of eventfs_file to be deleted
788  * @level: to check recursion depth
789  *
790  * The helper function eventfs_remove_rec() is used to clean up and free the
791  * associated data from eventfs for both of the added functions.
792  */
793 static void eventfs_remove_rec(struct eventfs_file *ef, struct list_head *head, int level)
794 {
795 	struct eventfs_file *ef_child;
796 
797 	if (!ef)
798 		return;
799 	/*
800 	 * Check recursion depth. It should never be greater than 3:
801 	 * 0 - events/
802 	 * 1 - events/group/
803 	 * 2 - events/group/event/
804 	 * 3 - events/group/event/file
805 	 */
806 	if (WARN_ON_ONCE(level > 3))
807 		return;
808 
809 	if (ef->ei) {
810 		/* search for nested folders or files */
811 		list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
812 					 lockdep_is_held(&eventfs_mutex)) {
813 			eventfs_remove_rec(ef_child, head, level + 1);
814 		}
815 	}
816 
817 	list_del_rcu(&ef->list);
818 	list_add_tail(&ef->del_list, head);
819 }
820 
821 /**
822  * eventfs_remove - remove eventfs dir or file from list
823  * @ef: eventfs_file to be removed.
824  *
825  * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
826  */
827 void eventfs_remove(struct eventfs_file *ef)
828 {
829 	struct eventfs_file *tmp;
830 	LIST_HEAD(ef_del_list);
831 	struct dentry *dentry_list = NULL;
832 	struct dentry *dentry;
833 
834 	if (!ef)
835 		return;
836 
837 	mutex_lock(&eventfs_mutex);
838 	eventfs_remove_rec(ef, &ef_del_list, 0);
839 	list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
840 		if (ef->dentry) {
841 			unsigned long ptr = (unsigned long)dentry_list;
842 
843 			/* Keep the dentry from being freed yet */
844 			dget(ef->dentry);
845 
846 			/*
847 			 * Paranoid: The dget() above should prevent the dentry
848 			 * from being freed and calling eventfs_set_ef_status_free().
849 			 * But just in case, set the link list LSB pointer to 1
850 			 * and have eventfs_set_ef_status_free() check that to
851 			 * make sure that if it does happen, it will not think
852 			 * the d_fsdata is an event_file.
853 			 *
854 			 * For this to work, no event_file should be allocated
855 			 * on a odd space, as the ef should always be allocated
856 			 * to be at least word aligned. Check for that too.
857 			 */
858 			WARN_ON_ONCE(ptr & 1);
859 
860 			ef->dentry->d_fsdata = (void *)(ptr | 1);
861 			dentry_list = ef->dentry;
862 			ef->dentry = NULL;
863 		}
864 		call_srcu(&eventfs_srcu, &ef->rcu, free_ef);
865 	}
866 	mutex_unlock(&eventfs_mutex);
867 
868 	while (dentry_list) {
869 		unsigned long ptr;
870 
871 		dentry = dentry_list;
872 		ptr = (unsigned long)dentry->d_fsdata & ~1UL;
873 		dentry_list = (struct dentry *)ptr;
874 		dentry->d_fsdata = NULL;
875 		d_invalidate(dentry);
876 		mutex_lock(&eventfs_mutex);
877 		/* dentry should now have at least a single reference */
878 		WARN_ONCE((int)d_count(dentry) < 1,
879 			  "dentry %p less than one reference (%d) after invalidate\n",
880 			  dentry, d_count(dentry));
881 		mutex_unlock(&eventfs_mutex);
882 		dput(dentry);
883 	}
884 }
885 
886 /**
887  * eventfs_remove_events_dir - remove eventfs dir or file from list
888  * @dentry: events's dentry to be removed.
889  *
890  * This function remove events main directory
891  */
892 void eventfs_remove_events_dir(struct dentry *dentry)
893 {
894 	struct tracefs_inode *ti;
895 
896 	if (!dentry || !dentry->d_inode)
897 		return;
898 
899 	ti = get_tracefs(dentry->d_inode);
900 	if (!ti || !(ti->flags & TRACEFS_EVENT_INODE))
901 		return;
902 
903 	d_invalidate(dentry);
904 	dput(dentry);
905 }
906