powerpc/mm: Avoid calling arch_enter/leave_lazy_mmu() in set_ptes
[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_srcu(ef, &ei->e_top_files, list,
456                                  srcu_read_lock_held(&eventfs_srcu)) {
457                 create_dentry(ef, dentry, false);
458         }
459         srcu_read_unlock(&eventfs_srcu, idx);
460         return dcache_dir_open(inode, file);
461 }
462
463 /**
464  * eventfs_prepare_ef - helper function to prepare eventfs_file
465  * @name: the name of the file/directory to create.
466  * @mode: the permission that the file should have.
467  * @fop: struct file_operations that should be used for this file/directory.
468  * @iop: struct inode_operations that should be used for this file/directory.
469  * @data: something that the caller will want to get to later on. The
470  *        inode.i_private pointer will point to this value on the open() call.
471  *
472  * This function allocates and fills the eventfs_file structure.
473  */
474 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode,
475                                         const struct file_operations *fop,
476                                         const struct inode_operations *iop,
477                                         void *data)
478 {
479         struct eventfs_file *ef;
480
481         ef = kzalloc(sizeof(*ef), GFP_KERNEL);
482         if (!ef)
483                 return ERR_PTR(-ENOMEM);
484
485         ef->name = kstrdup(name, GFP_KERNEL);
486         if (!ef->name) {
487                 kfree(ef);
488                 return ERR_PTR(-ENOMEM);
489         }
490
491         if (S_ISDIR(mode)) {
492                 ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL);
493                 if (!ef->ei) {
494                         kfree(ef->name);
495                         kfree(ef);
496                         return ERR_PTR(-ENOMEM);
497                 }
498                 INIT_LIST_HEAD(&ef->ei->e_top_files);
499         } else {
500                 ef->ei = NULL;
501         }
502
503         ef->iop = iop;
504         ef->fop = fop;
505         ef->mode = mode;
506         ef->data = data;
507         return ef;
508 }
509
510 /**
511  * eventfs_create_events_dir - create the trace event structure
512  * @name: the name of the directory to create.
513  * @parent: parent dentry for this file.  This should be a directory dentry
514  *          if set.  If this parameter is NULL, then the directory will be
515  *          created in the root of the tracefs filesystem.
516  *
517  * This function creates the top of the trace event directory.
518  */
519 struct dentry *eventfs_create_events_dir(const char *name,
520                                          struct dentry *parent)
521 {
522         struct dentry *dentry = tracefs_start_creating(name, parent);
523         struct eventfs_inode *ei;
524         struct tracefs_inode *ti;
525         struct inode *inode;
526
527         if (security_locked_down(LOCKDOWN_TRACEFS))
528                 return NULL;
529
530         if (IS_ERR(dentry))
531                 return dentry;
532
533         ei = kzalloc(sizeof(*ei), GFP_KERNEL);
534         if (!ei)
535                 return ERR_PTR(-ENOMEM);
536         inode = tracefs_get_inode(dentry->d_sb);
537         if (unlikely(!inode)) {
538                 kfree(ei);
539                 tracefs_failed_creating(dentry);
540                 return ERR_PTR(-ENOMEM);
541         }
542
543         INIT_LIST_HEAD(&ei->e_top_files);
544
545         ti = get_tracefs(inode);
546         ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
547         ti->private = ei;
548
549         inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
550         inode->i_op = &eventfs_root_dir_inode_operations;
551         inode->i_fop = &eventfs_file_operations;
552
553         /* directory inodes start off with i_nlink == 2 (for "." entry) */
554         inc_nlink(inode);
555         d_instantiate(dentry, inode);
556         inc_nlink(dentry->d_parent->d_inode);
557         fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
558         return tracefs_end_creating(dentry);
559 }
560
561 /**
562  * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later
563  * @name: the name of the file to create.
564  * @parent: parent dentry for this dir.
565  *
566  * This function adds eventfs subsystem dir to list.
567  * And all these dirs are created on the fly when they are looked up,
568  * and the dentry and inodes will be removed when they are done.
569  */
570 struct eventfs_file *eventfs_add_subsystem_dir(const char *name,
571                                                struct dentry *parent)
572 {
573         struct tracefs_inode *ti_parent;
574         struct eventfs_inode *ei_parent;
575         struct eventfs_file *ef;
576
577         if (security_locked_down(LOCKDOWN_TRACEFS))
578                 return NULL;
579
580         if (!parent)
581                 return ERR_PTR(-EINVAL);
582
583         ti_parent = get_tracefs(parent->d_inode);
584         ei_parent = ti_parent->private;
585
586         ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
587         if (IS_ERR(ef))
588                 return ef;
589
590         mutex_lock(&eventfs_mutex);
591         list_add_tail(&ef->list, &ei_parent->e_top_files);
592         ef->d_parent = parent;
593         mutex_unlock(&eventfs_mutex);
594         return ef;
595 }
596
597 /**
598  * eventfs_add_dir - add eventfs dir to list to create later
599  * @name: the name of the file to create.
600  * @ef_parent: parent eventfs_file for this dir.
601  *
602  * This function adds eventfs dir to list.
603  * And all these dirs are created on the fly when they are looked up,
604  * and the dentry and inodes will be removed when they are done.
605  */
606 struct eventfs_file *eventfs_add_dir(const char *name,
607                                      struct eventfs_file *ef_parent)
608 {
609         struct eventfs_file *ef;
610
611         if (security_locked_down(LOCKDOWN_TRACEFS))
612                 return NULL;
613
614         if (!ef_parent)
615                 return ERR_PTR(-EINVAL);
616
617         ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
618         if (IS_ERR(ef))
619                 return ef;
620
621         mutex_lock(&eventfs_mutex);
622         list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
623         ef->d_parent = ef_parent->dentry;
624         mutex_unlock(&eventfs_mutex);
625         return ef;
626 }
627
628 /**
629  * eventfs_add_events_file - add the data needed to create a file for later reference
630  * @name: the name of the file to create.
631  * @mode: the permission that the file should have.
632  * @parent: parent dentry for this file.
633  * @data: something that the caller will want to get to later on.
634  * @fop: struct file_operations that should be used for this file.
635  *
636  * This function is used to add the information needed to create a
637  * dentry/inode within the top level events directory. The file created
638  * will have the @mode permissions. The @data will be used to fill the
639  * inode.i_private when the open() call is done. The dentry and inodes are
640  * all created when they are referenced, and removed when they are no
641  * longer referenced.
642  */
643 int eventfs_add_events_file(const char *name, umode_t mode,
644                          struct dentry *parent, void *data,
645                          const struct file_operations *fop)
646 {
647         struct tracefs_inode *ti;
648         struct eventfs_inode *ei;
649         struct eventfs_file *ef;
650
651         if (security_locked_down(LOCKDOWN_TRACEFS))
652                 return -ENODEV;
653
654         if (!parent)
655                 return -EINVAL;
656
657         if (!(mode & S_IFMT))
658                 mode |= S_IFREG;
659
660         if (!parent->d_inode)
661                 return -EINVAL;
662
663         ti = get_tracefs(parent->d_inode);
664         if (!(ti->flags & TRACEFS_EVENT_INODE))
665                 return -EINVAL;
666
667         ei = ti->private;
668         ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
669
670         if (IS_ERR(ef))
671                 return -ENOMEM;
672
673         mutex_lock(&eventfs_mutex);
674         list_add_tail(&ef->list, &ei->e_top_files);
675         ef->d_parent = parent;
676         mutex_unlock(&eventfs_mutex);
677         return 0;
678 }
679
680 /**
681  * eventfs_add_file - add eventfs file to list to create later
682  * @name: the name of the file to create.
683  * @mode: the permission that the file should have.
684  * @ef_parent: parent eventfs_file for this file.
685  * @data: something that the caller will want to get to later on.
686  * @fop: struct file_operations that should be used for this file.
687  *
688  * This function is used to add the information needed to create a
689  * file within a subdirectory of the events directory. The file created
690  * will have the @mode permissions. The @data will be used to fill the
691  * inode.i_private when the open() call is done. The dentry and inodes are
692  * all created when they are referenced, and removed when they are no
693  * longer referenced.
694  */
695 int eventfs_add_file(const char *name, umode_t mode,
696                      struct eventfs_file *ef_parent,
697                      void *data,
698                      const struct file_operations *fop)
699 {
700         struct eventfs_file *ef;
701
702         if (security_locked_down(LOCKDOWN_TRACEFS))
703                 return -ENODEV;
704
705         if (!ef_parent)
706                 return -EINVAL;
707
708         if (!(mode & S_IFMT))
709                 mode |= S_IFREG;
710
711         ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
712         if (IS_ERR(ef))
713                 return -ENOMEM;
714
715         mutex_lock(&eventfs_mutex);
716         list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
717         ef->d_parent = ef_parent->dentry;
718         mutex_unlock(&eventfs_mutex);
719         return 0;
720 }
721
722 static void free_ef(struct rcu_head *head)
723 {
724         struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu);
725
726         kfree(ef->name);
727         kfree(ef->ei);
728         kfree(ef);
729 }
730
731 /**
732  * eventfs_remove_rec - remove eventfs dir or file from list
733  * @ef: eventfs_file to be removed.
734  * @head: to create list of eventfs_file to be deleted
735  * @level: to check recursion depth
736  *
737  * The helper function eventfs_remove_rec() is used to clean up and free the
738  * associated data from eventfs for both of the added functions.
739  */
740 static void eventfs_remove_rec(struct eventfs_file *ef, struct list_head *head, int level)
741 {
742         struct eventfs_file *ef_child;
743
744         if (!ef)
745                 return;
746         /*
747          * Check recursion depth. It should never be greater than 3:
748          * 0 - events/
749          * 1 - events/group/
750          * 2 - events/group/event/
751          * 3 - events/group/event/file
752          */
753         if (WARN_ON_ONCE(level > 3))
754                 return;
755
756         if (ef->ei) {
757                 /* search for nested folders or files */
758                 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
759                                          lockdep_is_held(&eventfs_mutex)) {
760                         eventfs_remove_rec(ef_child, head, level + 1);
761                 }
762         }
763
764         list_del_rcu(&ef->list);
765         list_add_tail(&ef->del_list, head);
766 }
767
768 /**
769  * eventfs_remove - remove eventfs dir or file from list
770  * @ef: eventfs_file to be removed.
771  *
772  * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
773  */
774 void eventfs_remove(struct eventfs_file *ef)
775 {
776         struct eventfs_file *tmp;
777         LIST_HEAD(ef_del_list);
778         struct dentry *dentry_list = NULL;
779         struct dentry *dentry;
780
781         if (!ef)
782                 return;
783
784         mutex_lock(&eventfs_mutex);
785         eventfs_remove_rec(ef, &ef_del_list, 0);
786         list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
787                 if (ef->dentry) {
788                         unsigned long ptr = (unsigned long)dentry_list;
789
790                         /* Keep the dentry from being freed yet */
791                         dget(ef->dentry);
792
793                         /*
794                          * Paranoid: The dget() above should prevent the dentry
795                          * from being freed and calling eventfs_set_ef_status_free().
796                          * But just in case, set the link list LSB pointer to 1
797                          * and have eventfs_set_ef_status_free() check that to
798                          * make sure that if it does happen, it will not think
799                          * the d_fsdata is an event_file.
800                          *
801                          * For this to work, no event_file should be allocated
802                          * on a odd space, as the ef should always be allocated
803                          * to be at least word aligned. Check for that too.
804                          */
805                         WARN_ON_ONCE(ptr & 1);
806
807                         ef->dentry->d_fsdata = (void *)(ptr | 1);
808                         dentry_list = ef->dentry;
809                         ef->dentry = NULL;
810                 }
811                 call_srcu(&eventfs_srcu, &ef->rcu, free_ef);
812         }
813         mutex_unlock(&eventfs_mutex);
814
815         while (dentry_list) {
816                 unsigned long ptr;
817
818                 dentry = dentry_list;
819                 ptr = (unsigned long)dentry->d_fsdata & ~1UL;
820                 dentry_list = (struct dentry *)ptr;
821                 dentry->d_fsdata = NULL;
822                 d_invalidate(dentry);
823                 mutex_lock(&eventfs_mutex);
824                 /* dentry should now have at least a single reference */
825                 WARN_ONCE((int)d_count(dentry) < 1,
826                           "dentry %p less than one reference (%d) after invalidate\n",
827                           dentry, d_count(dentry));
828                 mutex_unlock(&eventfs_mutex);
829                 dput(dentry);
830         }
831 }
832
833 /**
834  * eventfs_remove_events_dir - remove eventfs dir or file from list
835  * @dentry: events's dentry to be removed.
836  *
837  * This function remove events main directory
838  */
839 void eventfs_remove_events_dir(struct dentry *dentry)
840 {
841         struct tracefs_inode *ti;
842
843         if (!dentry || !dentry->d_inode)
844                 return;
845
846         ti = get_tracefs(dentry->d_inode);
847         if (!ti || !(ti->flags & TRACEFS_EVENT_INODE))
848                 return;
849
850         d_invalidate(dentry);
851         dput(dentry);
852 }