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