1 // SPDX-License-Identifier: GPL-2.0-only
3 * event_inode.c - part of tracefs, a pseudo file system for activating tracing
9 * eventfs is used to dynamically create inodes and dentries based on the
10 * meta data provided by the tracing system.
12 * eventfs stores the meta-data of files/dirs and holds off on creating
13 * inodes/dentries of the files. When accessed, the eventfs will create the
14 * inodes/dentries in a just-in-time (JIT) manner. The eventfs will clean up
15 * and delete the inodes/dentries when they are no longer referenced.
17 #include <linux/fsnotify.h>
19 #include <linux/namei.h>
20 #include <linux/workqueue.h>
21 #include <linux/security.h>
22 #include <linux/tracefs.h>
23 #include <linux/kref.h>
24 #include <linux/delay.h>
28 * eventfs_mutex protects the eventfs_inode (ei) dentry. Any access
29 * to the ei->dentry must be done under this mutex and after checking
30 * if ei->is_freed is not set. The ei->dentry is released under the
31 * mutex at the same time ei->is_freed is set. If ei->is_freed is set
32 * then the ei->dentry is invalid.
34 static DEFINE_MUTEX(eventfs_mutex);
37 * The eventfs_inode (ei) itself is protected by SRCU. It is released from
38 * its parent's list and will have is_freed set (under eventfs_mutex).
39 * After the SRCU grace period is over, the ei may be freed.
41 DEFINE_STATIC_SRCU(eventfs_srcu);
43 /* Mode is unsigned short, use the upper bits for flags */
45 EVENTFS_SAVE_MODE = BIT(16),
46 EVENTFS_SAVE_UID = BIT(17),
47 EVENTFS_SAVE_GID = BIT(18),
50 #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
52 static struct dentry *eventfs_root_lookup(struct inode *dir,
53 struct dentry *dentry,
55 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file);
56 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx);
57 static int eventfs_release(struct inode *inode, struct file *file);
59 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
61 unsigned int ia_valid = iattr->ia_valid;
63 if (ia_valid & ATTR_MODE) {
64 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
65 (iattr->ia_mode & EVENTFS_MODE_MASK) |
68 if (ia_valid & ATTR_UID) {
69 attr->mode |= EVENTFS_SAVE_UID;
70 attr->uid = iattr->ia_uid;
72 if (ia_valid & ATTR_GID) {
73 attr->mode |= EVENTFS_SAVE_GID;
74 attr->gid = iattr->ia_gid;
78 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
81 const struct eventfs_entry *entry;
82 struct eventfs_inode *ei;
86 mutex_lock(&eventfs_mutex);
87 ei = dentry->d_fsdata;
89 /* Do not allow changes if the event is about to be removed. */
90 mutex_unlock(&eventfs_mutex);
94 /* Preallocate the children mode array if necessary */
95 if (!(dentry->d_inode->i_mode & S_IFDIR)) {
96 if (!ei->entry_attrs) {
97 ei->entry_attrs = kzalloc(sizeof(*ei->entry_attrs) * ei->nr_entries,
99 if (!ei->entry_attrs) {
106 ret = simple_setattr(idmap, dentry, iattr);
111 * If this is a dir, then update the ei cache, only the file
112 * mode is saved in the ei->m_children, and the ownership is
113 * determined by the parent directory.
115 if (dentry->d_inode->i_mode & S_IFDIR) {
116 update_attr(&ei->attr, iattr);
119 name = dentry->d_name.name;
121 for (int i = 0; i < ei->nr_entries; i++) {
122 entry = &ei->entries[i];
123 if (strcmp(name, entry->name) == 0) {
124 update_attr(&ei->entry_attrs[i], iattr);
130 mutex_unlock(&eventfs_mutex);
134 static const struct inode_operations eventfs_root_dir_inode_operations = {
135 .lookup = eventfs_root_lookup,
136 .setattr = eventfs_set_attr,
139 static const struct inode_operations eventfs_file_inode_operations = {
140 .setattr = eventfs_set_attr,
143 static const struct file_operations eventfs_file_operations = {
144 .open = dcache_dir_open_wrapper,
145 .read = generic_read_dir,
146 .iterate_shared = dcache_readdir_wrapper,
147 .llseek = generic_file_llseek,
148 .release = eventfs_release,
151 static void update_inode_attr(struct inode *inode, struct eventfs_attr *attr, umode_t mode)
154 inode->i_mode = mode;
158 if (attr->mode & EVENTFS_SAVE_MODE)
159 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
161 inode->i_mode = mode;
163 if (attr->mode & EVENTFS_SAVE_UID)
164 inode->i_uid = attr->uid;
166 if (attr->mode & EVENTFS_SAVE_GID)
167 inode->i_gid = attr->gid;
171 * create_file - create a file in the tracefs filesystem
172 * @name: the name of the file to create.
173 * @mode: the permission that the file should have.
174 * @attr: saved attributes changed by user
175 * @parent: parent dentry for this file.
176 * @data: something that the caller will want to get to later on.
177 * @fop: struct file_operations that should be used for this file.
179 * This function creates a dentry that represents a file in the eventsfs_inode
180 * directory. The inode.i_private pointer will point to @data in the open()
183 static struct dentry *create_file(const char *name, umode_t mode,
184 struct eventfs_attr *attr,
185 struct dentry *parent, void *data,
186 const struct file_operations *fop)
188 struct tracefs_inode *ti;
189 struct dentry *dentry;
192 if (!(mode & S_IFMT))
195 if (WARN_ON_ONCE(!S_ISREG(mode)))
198 WARN_ON_ONCE(!parent);
199 dentry = eventfs_start_creating(name, parent);
204 inode = tracefs_get_inode(dentry->d_sb);
205 if (unlikely(!inode))
206 return eventfs_failed_creating(dentry);
208 /* If the user updated the directory's attributes, use them */
209 update_inode_attr(inode, attr, mode);
211 inode->i_op = &eventfs_file_inode_operations;
213 inode->i_private = data;
215 ti = get_tracefs(inode);
216 ti->flags |= TRACEFS_EVENT_INODE;
217 d_instantiate(dentry, inode);
218 fsnotify_create(dentry->d_parent->d_inode, dentry);
219 return eventfs_end_creating(dentry);
223 * create_dir - create a dir in the tracefs filesystem
224 * @ei: the eventfs_inode that represents the directory to create
225 * @parent: parent dentry for this file.
227 * This function will create a dentry for a directory represented by
230 static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent)
232 struct tracefs_inode *ti;
233 struct dentry *dentry;
236 dentry = eventfs_start_creating(ei->name, parent);
240 inode = tracefs_get_inode(dentry->d_sb);
241 if (unlikely(!inode))
242 return eventfs_failed_creating(dentry);
244 /* If the user updated the directory's attributes, use them */
245 update_inode_attr(inode, &ei->attr, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
247 inode->i_op = &eventfs_root_dir_inode_operations;
248 inode->i_fop = &eventfs_file_operations;
250 ti = get_tracefs(inode);
251 ti->flags |= TRACEFS_EVENT_INODE;
254 d_instantiate(dentry, inode);
255 inc_nlink(dentry->d_parent->d_inode);
256 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
257 return eventfs_end_creating(dentry);
260 static void free_ei(struct eventfs_inode *ei)
262 kfree_const(ei->name);
263 kfree(ei->d_children);
264 kfree(ei->entry_attrs);
269 * eventfs_set_ei_status_free - remove the dentry reference from an eventfs_inode
270 * @ti: the tracefs_inode of the dentry
271 * @dentry: dentry which has the reference to remove.
273 * Remove the association between a dentry from an eventfs_inode.
275 void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry)
277 struct eventfs_inode *ei;
280 mutex_lock(&eventfs_mutex);
282 ei = dentry->d_fsdata;
286 /* This could belong to one of the files of the ei */
287 if (ei->dentry != dentry) {
288 for (i = 0; i < ei->nr_entries; i++) {
289 if (ei->d_children[i] == dentry)
292 if (WARN_ON_ONCE(i == ei->nr_entries))
294 ei->d_children[i] = NULL;
295 } else if (ei->is_freed) {
301 dentry->d_fsdata = NULL;
303 mutex_unlock(&eventfs_mutex);
307 * create_file_dentry - create a dentry for a file of an eventfs_inode
308 * @ei: the eventfs_inode that the file will be created under
309 * @idx: the index into the d_children[] of the @ei
310 * @parent: The parent dentry of the created file.
311 * @name: The name of the file to create
312 * @mode: The mode of the file.
313 * @data: The data to use to set the inode of the file with on open()
314 * @fops: The fops of the file to be created.
315 * @lookup: If called by the lookup routine, in which case, dput() the created dentry.
317 * Create a dentry for a file of an eventfs_inode @ei and place it into the
318 * address located at @e_dentry. If the @e_dentry already has a dentry, then
319 * just do a dget() on it and return. Otherwise create the dentry and attach it.
321 static struct dentry *
322 create_file_dentry(struct eventfs_inode *ei, int idx,
323 struct dentry *parent, const char *name, umode_t mode, void *data,
324 const struct file_operations *fops, bool lookup)
326 struct eventfs_attr *attr = NULL;
327 struct dentry **e_dentry = &ei->d_children[idx];
328 struct dentry *dentry;
329 bool invalidate = false;
331 mutex_lock(&eventfs_mutex);
333 mutex_unlock(&eventfs_mutex);
336 /* If the e_dentry already has a dentry, use it */
338 /* lookup does not need to up the ref count */
341 mutex_unlock(&eventfs_mutex);
345 /* ei->entry_attrs are protected by SRCU */
347 attr = &ei->entry_attrs[idx];
349 mutex_unlock(&eventfs_mutex);
351 /* The lookup already has the parent->d_inode locked */
353 inode_lock(parent->d_inode);
355 dentry = create_file(name, mode, attr, parent, data, fops);
358 inode_unlock(parent->d_inode);
360 mutex_lock(&eventfs_mutex);
362 if (IS_ERR_OR_NULL(dentry)) {
364 * When the mutex was released, something else could have
365 * created the dentry for this e_dentry. In which case
368 * Note, with the mutex held, the e_dentry cannot have content
369 * and the ei->is_freed be true at the same time.
372 if (WARN_ON_ONCE(dentry && ei->is_freed))
374 /* The lookup does not need to up the dentry refcount */
375 if (dentry && !lookup)
377 mutex_unlock(&eventfs_mutex);
381 if (!*e_dentry && !ei->is_freed) {
383 dentry->d_fsdata = ei;
386 * Should never happen unless we get here due to being freed.
387 * Otherwise it means two dentries exist with the same name.
389 WARN_ON_ONCE(!ei->is_freed);
392 mutex_unlock(&eventfs_mutex);
395 d_invalidate(dentry);
397 if (lookup || invalidate)
400 return invalidate ? NULL : dentry;
404 * eventfs_post_create_dir - post create dir routine
405 * @ei: eventfs_inode of recently created dir
407 * Map the meta-data of files within an eventfs dir to their parent dentry
409 static void eventfs_post_create_dir(struct eventfs_inode *ei)
411 struct eventfs_inode *ei_child;
412 struct tracefs_inode *ti;
414 lockdep_assert_held(&eventfs_mutex);
416 /* srcu lock already held */
417 /* fill parent-child relation */
418 list_for_each_entry_srcu(ei_child, &ei->children, list,
419 srcu_read_lock_held(&eventfs_srcu)) {
420 ei_child->d_parent = ei->dentry;
423 ti = get_tracefs(ei->dentry->d_inode);
428 * create_dir_dentry - Create a directory dentry for the eventfs_inode
429 * @pei: The eventfs_inode parent of ei.
430 * @ei: The eventfs_inode to create the directory for
431 * @parent: The dentry of the parent of this directory
432 * @lookup: True if this is called by the lookup code
434 * This creates and attaches a directory dentry to the eventfs_inode @ei.
436 static struct dentry *
437 create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei,
438 struct dentry *parent, bool lookup)
440 bool invalidate = false;
441 struct dentry *dentry = NULL;
443 mutex_lock(&eventfs_mutex);
444 if (pei->is_freed || ei->is_freed) {
445 mutex_unlock(&eventfs_mutex);
449 /* If the dentry already has a dentry, use it */
451 /* lookup does not need to up the ref count */
454 mutex_unlock(&eventfs_mutex);
457 mutex_unlock(&eventfs_mutex);
459 /* The lookup already has the parent->d_inode locked */
461 inode_lock(parent->d_inode);
463 dentry = create_dir(ei, parent);
466 inode_unlock(parent->d_inode);
468 mutex_lock(&eventfs_mutex);
470 if (IS_ERR_OR_NULL(dentry) && !ei->is_freed) {
472 * When the mutex was released, something else could have
473 * created the dentry for this e_dentry. In which case
476 * Note, with the mutex held, the e_dentry cannot have content
477 * and the ei->is_freed be true at the same time.
480 if (dentry && !lookup)
482 mutex_unlock(&eventfs_mutex);
486 if (!ei->dentry && !ei->is_freed) {
488 eventfs_post_create_dir(ei);
489 dentry->d_fsdata = ei;
492 * Should never happen unless we get here due to being freed.
493 * Otherwise it means two dentries exist with the same name.
495 WARN_ON_ONCE(!ei->is_freed);
498 mutex_unlock(&eventfs_mutex);
500 d_invalidate(dentry);
502 if (lookup || invalidate)
505 return invalidate ? NULL : dentry;
509 * eventfs_root_lookup - lookup routine to create file/dir
510 * @dir: in which a lookup is being done
511 * @dentry: file/dir dentry
512 * @flags: Just passed to simple_lookup()
514 * Used to create dynamic file/dir with-in @dir, search with-in @ei
515 * list, if @dentry found go ahead and create the file/dir
518 static struct dentry *eventfs_root_lookup(struct inode *dir,
519 struct dentry *dentry,
522 const struct file_operations *fops;
523 const struct eventfs_entry *entry;
524 struct eventfs_inode *ei_child;
525 struct tracefs_inode *ti;
526 struct eventfs_inode *ei;
527 struct dentry *ei_dentry = NULL;
528 struct dentry *ret = NULL;
529 const char *name = dentry->d_name.name;
530 bool created = false;
537 ti = get_tracefs(dir);
538 if (!(ti->flags & TRACEFS_EVENT_INODE))
541 /* Grab srcu to prevent the ei from going away */
542 idx = srcu_read_lock(&eventfs_srcu);
545 * Grab the eventfs_mutex to consistent value from ti->private.
548 mutex_lock(&eventfs_mutex);
549 ei = READ_ONCE(ti->private);
550 if (ei && !ei->is_freed)
551 ei_dentry = READ_ONCE(ei->dentry);
552 mutex_unlock(&eventfs_mutex);
554 if (!ei || !ei_dentry)
559 list_for_each_entry_srcu(ei_child, &ei->children, list,
560 srcu_read_lock_held(&eventfs_srcu)) {
561 if (strcmp(ei_child->name, name) != 0)
563 ret = simple_lookup(dir, dentry, flags);
564 create_dir_dentry(ei, ei_child, ei_dentry, true);
572 for (i = 0; i < ei->nr_entries; i++) {
573 entry = &ei->entries[i];
574 if (strcmp(name, entry->name) == 0) {
576 mutex_lock(&eventfs_mutex);
577 /* If ei->is_freed, then the event itself may be too */
579 r = entry->callback(name, &mode, &cdata, &fops);
582 mutex_unlock(&eventfs_mutex);
585 ret = simple_lookup(dir, dentry, flags);
586 create_file_dentry(ei, i, ei_dentry, name, mode, cdata,
592 srcu_read_unlock(&eventfs_srcu, idx);
598 struct dentry **dentries;
602 * eventfs_release - called to release eventfs file/dir
603 * @inode: inode to be released
604 * @file: file to be released (not used)
606 static int eventfs_release(struct inode *inode, struct file *file)
608 struct tracefs_inode *ti;
609 struct dentry_list *dlist = file->private_data;
613 ti = get_tracefs(inode);
614 if (!(ti->flags & TRACEFS_EVENT_INODE))
617 if (WARN_ON_ONCE(!dlist))
620 for (i = 0; dlist->dentries && dlist->dentries[i]; i++) {
621 dput(dlist->dentries[i]);
624 cursor = dlist->cursor;
625 kfree(dlist->dentries);
627 file->private_data = cursor;
628 return dcache_dir_close(inode, file);
631 static int add_dentries(struct dentry ***dentries, struct dentry *d, int cnt)
635 tmp = krealloc(*dentries, sizeof(d) * (cnt + 2), GFP_KERNEL);
645 * dcache_dir_open_wrapper - eventfs open wrapper
647 * @file: dir to be opened (to create it's children)
649 * Used to dynamic create file/dir with-in @file, all the
650 * file/dir will be created. If already created then references
653 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
655 const struct file_operations *fops;
656 const struct eventfs_entry *entry;
657 struct eventfs_inode *ei_child;
658 struct tracefs_inode *ti;
659 struct eventfs_inode *ei;
660 struct dentry_list *dlist;
661 struct dentry **dentries = NULL;
662 struct dentry *parent = file_dentry(file);
664 struct inode *f_inode = file_inode(file);
665 const char *name = parent->d_name.name;
674 ti = get_tracefs(f_inode);
675 if (!(ti->flags & TRACEFS_EVENT_INODE))
678 if (WARN_ON_ONCE(file->private_data))
681 idx = srcu_read_lock(&eventfs_srcu);
683 mutex_lock(&eventfs_mutex);
684 ei = READ_ONCE(ti->private);
685 mutex_unlock(&eventfs_mutex);
688 srcu_read_unlock(&eventfs_srcu, idx);
695 dlist = kmalloc(sizeof(*dlist), GFP_KERNEL);
697 srcu_read_unlock(&eventfs_srcu, idx);
701 list_for_each_entry_srcu(ei_child, &ei->children, list,
702 srcu_read_lock_held(&eventfs_srcu)) {
703 d = create_dir_dentry(ei, ei_child, parent, false);
705 ret = add_dentries(&dentries, d, cnt);
712 for (i = 0; i < ei->nr_entries; i++) {
714 entry = &ei->entries[i];
716 mutex_lock(&eventfs_mutex);
717 /* If ei->is_freed, then the event itself may be too */
719 r = entry->callback(name, &mode, &cdata, &fops);
722 mutex_unlock(&eventfs_mutex);
725 d = create_file_dentry(ei, i, parent, name, mode, cdata, fops, false);
727 ret = add_dentries(&dentries, d, cnt);
733 srcu_read_unlock(&eventfs_srcu, idx);
734 ret = dcache_dir_open(inode, file);
737 * dcache_dir_open() sets file->private_data to a dentry cursor.
738 * Need to save that but also save all the dentries that were
739 * opened by this function.
741 dlist->cursor = file->private_data;
742 dlist->dentries = dentries;
743 file->private_data = dlist;
748 * This just sets the file->private_data back to the cursor and back.
750 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx)
752 struct dentry_list *dlist = file->private_data;
755 file->private_data = dlist->cursor;
756 ret = dcache_readdir(file, ctx);
757 dlist->cursor = file->private_data;
758 file->private_data = dlist;
763 * eventfs_create_dir - Create the eventfs_inode for this directory
764 * @name: The name of the directory to create.
765 * @parent: The eventfs_inode of the parent directory.
766 * @entries: A list of entries that represent the files under this directory
767 * @size: The number of @entries
768 * @data: The default data to pass to the files (an entry may override it).
770 * This function creates the descriptor to represent a directory in the
771 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
772 * used to create other children underneath.
774 * The @entries is an array of eventfs_entry structures which has:
776 * eventfs_callback callback;
778 * The name is the name of the file, and the callback is a pointer to a function
779 * that will be called when the file is reference (either by lookup or by
780 * reading a directory). The callback is of the prototype:
782 * int callback(const char *name, umode_t *mode, void **data,
783 * const struct file_operations **fops);
785 * When a file needs to be created, this callback will be called with
786 * name = the name of the file being created (so that the same callback
787 * may be used for multiple files).
788 * mode = a place to set the file's mode
789 * data = A pointer to @data, and the callback may replace it, which will
790 * cause the file created to pass the new data to the open() call.
791 * fops = the fops to use for the created file.
793 * NB. @callback is called while holding internal locks of the eventfs
794 * system. The callback must not call any code that might also call into
795 * the tracefs or eventfs system or it will risk creating a deadlock.
797 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
798 const struct eventfs_entry *entries,
799 int size, void *data)
801 struct eventfs_inode *ei;
804 return ERR_PTR(-EINVAL);
806 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
808 return ERR_PTR(-ENOMEM);
810 ei->name = kstrdup_const(name, GFP_KERNEL);
813 return ERR_PTR(-ENOMEM);
817 ei->d_children = kzalloc(sizeof(*ei->d_children) * size, GFP_KERNEL);
818 if (!ei->d_children) {
819 kfree_const(ei->name);
821 return ERR_PTR(-ENOMEM);
825 ei->entries = entries;
826 ei->nr_entries = size;
828 INIT_LIST_HEAD(&ei->children);
829 INIT_LIST_HEAD(&ei->list);
831 mutex_lock(&eventfs_mutex);
832 if (!parent->is_freed) {
833 list_add_tail(&ei->list, &parent->children);
834 ei->d_parent = parent->dentry;
836 mutex_unlock(&eventfs_mutex);
838 /* Was the parent freed? */
839 if (list_empty(&ei->list)) {
847 * eventfs_create_events_dir - create the top level events directory
848 * @name: The name of the top level directory to create.
849 * @parent: Parent dentry for this file in the tracefs directory.
850 * @entries: A list of entries that represent the files under this directory
851 * @size: The number of @entries
852 * @data: The default data to pass to the files (an entry may override it).
854 * This function creates the top of the trace event directory.
856 * See eventfs_create_dir() for use of @entries.
858 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
859 const struct eventfs_entry *entries,
860 int size, void *data)
862 struct dentry *dentry = tracefs_start_creating(name, parent);
863 struct eventfs_inode *ei;
864 struct tracefs_inode *ti;
867 if (security_locked_down(LOCKDOWN_TRACEFS))
871 return ERR_CAST(dentry);
873 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
877 inode = tracefs_get_inode(dentry->d_sb);
878 if (unlikely(!inode))
882 ei->d_children = kzalloc(sizeof(*ei->d_children) * size, GFP_KERNEL);
888 ei->entries = entries;
889 ei->nr_entries = size;
891 ei->name = kstrdup_const(name, GFP_KERNEL);
895 INIT_LIST_HEAD(&ei->children);
896 INIT_LIST_HEAD(&ei->list);
898 ti = get_tracefs(inode);
899 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
902 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
903 inode->i_op = &eventfs_root_dir_inode_operations;
904 inode->i_fop = &eventfs_file_operations;
906 dentry->d_fsdata = ei;
908 /* directory inodes start off with i_nlink == 2 (for "." entry) */
910 d_instantiate(dentry, inode);
911 inc_nlink(dentry->d_parent->d_inode);
912 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
913 tracefs_end_creating(dentry);
918 kfree(ei->d_children);
921 tracefs_failed_creating(dentry);
922 return ERR_PTR(-ENOMEM);
925 static LLIST_HEAD(free_list);
927 static void eventfs_workfn(struct work_struct *work)
929 struct eventfs_inode *ei, *tmp;
930 struct llist_node *llnode;
932 llnode = llist_del_all(&free_list);
933 llist_for_each_entry_safe(ei, tmp, llnode, llist) {
934 /* This dput() matches the dget() from unhook_dentry() */
935 for (int i = 0; i < ei->nr_entries; i++) {
936 if (ei->d_children[i])
937 dput(ei->d_children[i]);
939 /* This should only get here if it had a dentry */
940 if (!WARN_ON_ONCE(!ei->dentry))
945 static DECLARE_WORK(eventfs_work, eventfs_workfn);
947 static void free_rcu_ei(struct rcu_head *head)
949 struct eventfs_inode *ei = container_of(head, struct eventfs_inode, rcu);
952 /* Do not free the ei until all references of dentry are gone */
953 if (llist_add(&ei->llist, &free_list))
954 queue_work(system_unbound_wq, &eventfs_work);
958 /* If the ei doesn't have a dentry, neither should its children */
959 for (int i = 0; i < ei->nr_entries; i++) {
960 WARN_ON_ONCE(ei->d_children[i]);
966 static void unhook_dentry(struct dentry *dentry)
971 * Need to add a reference to the dentry that is expected by
972 * simple_recursive_removal(), which will include a dput().
977 * Also add a reference for the dput() in eventfs_workfn().
978 * That is required as that dput() will free the ei after
979 * the SRCU grace period is over.
985 * eventfs_remove_rec - remove eventfs dir or file from list
986 * @ei: eventfs_inode to be removed.
987 * @level: prevent recursion from going more than 3 levels deep.
989 * This function recursively removes eventfs_inodes which
990 * contains info of files and/or directories.
992 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
994 struct eventfs_inode *ei_child;
999 * Check recursion depth. It should never be greater than 3:
1002 * 2 - events/group/event/
1003 * 3 - events/group/event/file
1005 if (WARN_ON_ONCE(level > 3))
1008 /* search for nested folders or files */
1009 list_for_each_entry_srcu(ei_child, &ei->children, list,
1010 lockdep_is_held(&eventfs_mutex)) {
1011 /* Children only have dentry if parent does */
1012 WARN_ON_ONCE(ei_child->dentry && !ei->dentry);
1013 eventfs_remove_rec(ei_child, level + 1);
1019 for (int i = 0; i < ei->nr_entries; i++) {
1020 if (ei->d_children[i]) {
1021 /* Children only have dentry if parent does */
1022 WARN_ON_ONCE(!ei->dentry);
1023 unhook_dentry(ei->d_children[i]);
1027 unhook_dentry(ei->dentry);
1029 list_del_rcu(&ei->list);
1030 call_srcu(&eventfs_srcu, &ei->rcu, free_rcu_ei);
1034 * eventfs_remove_dir - remove eventfs dir or file from list
1035 * @ei: eventfs_inode to be removed.
1037 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
1039 void eventfs_remove_dir(struct eventfs_inode *ei)
1041 struct dentry *dentry;
1046 mutex_lock(&eventfs_mutex);
1047 dentry = ei->dentry;
1048 eventfs_remove_rec(ei, 0);
1049 mutex_unlock(&eventfs_mutex);
1052 * If any of the ei children has a dentry, then the ei itself
1053 * must have a dentry.
1056 simple_recursive_removal(dentry, NULL);
1060 * eventfs_remove_events_dir - remove the top level eventfs directory
1061 * @ei: the event_inode returned by eventfs_create_events_dir().
1063 * This function removes the events main directory
1065 void eventfs_remove_events_dir(struct eventfs_inode *ei)
1067 struct dentry *dentry;
1069 dentry = ei->dentry;
1070 eventfs_remove_dir(ei);
1073 * Matches the dget() done by tracefs_start_creating()
1074 * in eventfs_create_events_dir() when it the dentry was
1075 * created. In other words, it's a normal dentry that
1076 * sticks around while the other ei->dentry are created
1077 * and destroyed dynamically.