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