1 // SPDX-License-Identifier: GPL-2.0-only
3 * event_inode.c - part of tracefs, a pseudo file system for activating tracing
5 * Copyright (C) 2020-23 VMware Inc, author: Steven Rostedt <rostedt@goodmis.org>
6 * Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com>
7 * Copyright (C) 2023 Google, author: Steven Rostedt <rostedt@goodmis.org>
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. When ei->is_freed is set, the dentry
31 * is on its way to being freed after the last dput() is made on it.
33 static DEFINE_MUTEX(eventfs_mutex);
36 * The eventfs_inode (ei) itself is protected by SRCU. It is released from
37 * its parent's list and will have is_freed set (under eventfs_mutex).
38 * After the SRCU grace period is over and the last dput() is called
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 dentry *dentry, struct inode *inode,
152 struct eventfs_attr *attr, umode_t mode)
155 inode->i_mode = mode;
159 if (attr->mode & EVENTFS_SAVE_MODE)
160 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
162 inode->i_mode = mode;
164 if (attr->mode & EVENTFS_SAVE_UID)
165 inode->i_uid = attr->uid;
167 inode->i_uid = d_inode(dentry->d_parent)->i_uid;
169 if (attr->mode & EVENTFS_SAVE_GID)
170 inode->i_gid = attr->gid;
172 inode->i_gid = d_inode(dentry->d_parent)->i_gid;
176 * create_file - create a file in the tracefs filesystem
177 * @name: the name of the file to create.
178 * @mode: the permission that the file should have.
179 * @attr: saved attributes changed by user
180 * @parent: parent dentry for this file.
181 * @data: something that the caller will want to get to later on.
182 * @fop: struct file_operations that should be used for this file.
184 * This function creates a dentry that represents a file in the eventsfs_inode
185 * directory. The inode.i_private pointer will point to @data in the open()
188 static struct dentry *create_file(const char *name, umode_t mode,
189 struct eventfs_attr *attr,
190 struct dentry *parent, void *data,
191 const struct file_operations *fop)
193 struct tracefs_inode *ti;
194 struct dentry *dentry;
197 if (!(mode & S_IFMT))
200 if (WARN_ON_ONCE(!S_ISREG(mode)))
203 WARN_ON_ONCE(!parent);
204 dentry = eventfs_start_creating(name, parent);
209 inode = tracefs_get_inode(dentry->d_sb);
210 if (unlikely(!inode))
211 return eventfs_failed_creating(dentry);
213 /* If the user updated the directory's attributes, use them */
214 update_inode_attr(dentry, inode, attr, mode);
216 inode->i_op = &eventfs_file_inode_operations;
218 inode->i_private = data;
220 ti = get_tracefs(inode);
221 ti->flags |= TRACEFS_EVENT_INODE;
222 d_instantiate(dentry, inode);
223 fsnotify_create(dentry->d_parent->d_inode, dentry);
224 return eventfs_end_creating(dentry);
228 * create_dir - create a dir in the tracefs filesystem
229 * @ei: the eventfs_inode that represents the directory to create
230 * @parent: parent dentry for this file.
232 * This function will create a dentry for a directory represented by
235 static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent)
237 struct tracefs_inode *ti;
238 struct dentry *dentry;
241 dentry = eventfs_start_creating(ei->name, parent);
245 inode = tracefs_get_inode(dentry->d_sb);
246 if (unlikely(!inode))
247 return eventfs_failed_creating(dentry);
249 /* If the user updated the directory's attributes, use them */
250 update_inode_attr(dentry, inode, &ei->attr,
251 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
253 inode->i_op = &eventfs_root_dir_inode_operations;
254 inode->i_fop = &eventfs_file_operations;
256 ti = get_tracefs(inode);
257 ti->flags |= TRACEFS_EVENT_INODE;
260 d_instantiate(dentry, inode);
261 inc_nlink(dentry->d_parent->d_inode);
262 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
263 return eventfs_end_creating(dentry);
266 static void free_ei(struct eventfs_inode *ei)
268 kfree_const(ei->name);
269 kfree(ei->d_children);
270 kfree(ei->entry_attrs);
275 * eventfs_set_ei_status_free - remove the dentry reference from an eventfs_inode
276 * @ti: the tracefs_inode of the dentry
277 * @dentry: dentry which has the reference to remove.
279 * Remove the association between a dentry from an eventfs_inode.
281 void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry)
283 struct eventfs_inode *ei;
286 mutex_lock(&eventfs_mutex);
288 ei = dentry->d_fsdata;
292 /* This could belong to one of the files of the ei */
293 if (ei->dentry != dentry) {
294 for (i = 0; i < ei->nr_entries; i++) {
295 if (ei->d_children[i] == dentry)
298 if (WARN_ON_ONCE(i == ei->nr_entries))
300 ei->d_children[i] = NULL;
301 } else if (ei->is_freed) {
307 dentry->d_fsdata = NULL;
309 mutex_unlock(&eventfs_mutex);
313 * create_file_dentry - create a dentry for a file of an eventfs_inode
314 * @ei: the eventfs_inode that the file will be created under
315 * @idx: the index into the d_children[] of the @ei
316 * @parent: The parent dentry of the created file.
317 * @name: The name of the file to create
318 * @mode: The mode of the file.
319 * @data: The data to use to set the inode of the file with on open()
320 * @fops: The fops of the file to be created.
321 * @lookup: If called by the lookup routine, in which case, dput() the created dentry.
323 * Create a dentry for a file of an eventfs_inode @ei and place it into the
324 * address located at @e_dentry. If the @e_dentry already has a dentry, then
325 * just do a dget() on it and return. Otherwise create the dentry and attach it.
327 static struct dentry *
328 create_file_dentry(struct eventfs_inode *ei, int idx,
329 struct dentry *parent, const char *name, umode_t mode, void *data,
330 const struct file_operations *fops, bool lookup)
332 struct eventfs_attr *attr = NULL;
333 struct dentry **e_dentry = &ei->d_children[idx];
334 struct dentry *dentry;
336 WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
338 mutex_lock(&eventfs_mutex);
340 mutex_unlock(&eventfs_mutex);
343 /* If the e_dentry already has a dentry, use it */
345 /* lookup does not need to up the ref count */
348 mutex_unlock(&eventfs_mutex);
352 /* ei->entry_attrs are protected by SRCU */
354 attr = &ei->entry_attrs[idx];
356 mutex_unlock(&eventfs_mutex);
358 dentry = create_file(name, mode, attr, parent, data, fops);
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 * If ei->is_freed is set, the e_dentry is currently on its
369 * way to being freed, don't return it. If e_dentry is NULL
370 * it means it was already freed.
376 /* The lookup does not need to up the dentry refcount */
377 if (dentry && !lookup)
379 mutex_unlock(&eventfs_mutex);
383 if (!*e_dentry && !ei->is_freed) {
385 dentry->d_fsdata = ei;
388 * Should never happen unless we get here due to being freed.
389 * Otherwise it means two dentries exist with the same name.
391 WARN_ON_ONCE(!ei->is_freed);
394 mutex_unlock(&eventfs_mutex);
403 * eventfs_post_create_dir - post create dir routine
404 * @ei: eventfs_inode of recently created dir
406 * Map the meta-data of files within an eventfs dir to their parent dentry
408 static void eventfs_post_create_dir(struct eventfs_inode *ei)
410 struct eventfs_inode *ei_child;
411 struct tracefs_inode *ti;
413 lockdep_assert_held(&eventfs_mutex);
415 /* srcu lock already held */
416 /* fill parent-child relation */
417 list_for_each_entry_srcu(ei_child, &ei->children, list,
418 srcu_read_lock_held(&eventfs_srcu)) {
419 ei_child->d_parent = ei->dentry;
422 ti = get_tracefs(ei->dentry->d_inode);
427 * create_dir_dentry - Create a directory dentry for the eventfs_inode
428 * @pei: The eventfs_inode parent of ei.
429 * @ei: The eventfs_inode to create the directory for
430 * @parent: The dentry of the parent of this directory
431 * @lookup: True if this is called by the lookup code
433 * This creates and attaches a directory dentry to the eventfs_inode @ei.
435 static struct dentry *
436 create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei,
437 struct dentry *parent, bool lookup)
439 struct dentry *dentry = NULL;
441 WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
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 dentry = create_dir(ei, parent);
461 mutex_lock(&eventfs_mutex);
463 if (IS_ERR_OR_NULL(dentry) && !ei->is_freed) {
465 * When the mutex was released, something else could have
466 * created the dentry for this e_dentry. In which case
469 * If ei->is_freed is set, the e_dentry is currently on its
470 * way to being freed.
473 if (dentry && !lookup)
475 mutex_unlock(&eventfs_mutex);
479 if (!ei->dentry && !ei->is_freed) {
481 eventfs_post_create_dir(ei);
482 dentry->d_fsdata = ei;
485 * Should never happen unless we get here due to being freed.
486 * Otherwise it means two dentries exist with the same name.
488 WARN_ON_ONCE(!ei->is_freed);
491 mutex_unlock(&eventfs_mutex);
500 * eventfs_root_lookup - lookup routine to create file/dir
501 * @dir: in which a lookup is being done
502 * @dentry: file/dir dentry
503 * @flags: Just passed to simple_lookup()
505 * Used to create dynamic file/dir with-in @dir, search with-in @ei
506 * list, if @dentry found go ahead and create the file/dir
509 static struct dentry *eventfs_root_lookup(struct inode *dir,
510 struct dentry *dentry,
513 const struct file_operations *fops;
514 const struct eventfs_entry *entry;
515 struct eventfs_inode *ei_child;
516 struct tracefs_inode *ti;
517 struct eventfs_inode *ei;
518 struct dentry *ei_dentry = NULL;
519 struct dentry *ret = NULL;
520 const char *name = dentry->d_name.name;
521 bool created = false;
528 ti = get_tracefs(dir);
529 if (!(ti->flags & TRACEFS_EVENT_INODE))
532 /* Grab srcu to prevent the ei from going away */
533 idx = srcu_read_lock(&eventfs_srcu);
536 * Grab the eventfs_mutex to consistent value from ti->private.
539 mutex_lock(&eventfs_mutex);
540 ei = READ_ONCE(ti->private);
541 if (ei && !ei->is_freed)
542 ei_dentry = READ_ONCE(ei->dentry);
543 mutex_unlock(&eventfs_mutex);
545 if (!ei || !ei_dentry)
550 list_for_each_entry_srcu(ei_child, &ei->children, list,
551 srcu_read_lock_held(&eventfs_srcu)) {
552 if (strcmp(ei_child->name, name) != 0)
554 ret = simple_lookup(dir, dentry, flags);
557 create_dir_dentry(ei, ei_child, ei_dentry, true);
565 for (i = 0; i < ei->nr_entries; i++) {
566 entry = &ei->entries[i];
567 if (strcmp(name, entry->name) == 0) {
569 mutex_lock(&eventfs_mutex);
570 /* If ei->is_freed, then the event itself may be too */
572 r = entry->callback(name, &mode, &cdata, &fops);
575 mutex_unlock(&eventfs_mutex);
578 ret = simple_lookup(dir, dentry, flags);
581 create_file_dentry(ei, i, ei_dentry, name, mode, cdata,
587 srcu_read_unlock(&eventfs_srcu, idx);
593 struct dentry **dentries;
597 * eventfs_release - called to release eventfs file/dir
598 * @inode: inode to be released
599 * @file: file to be released (not used)
601 static int eventfs_release(struct inode *inode, struct file *file)
603 struct tracefs_inode *ti;
604 struct dentry_list *dlist = file->private_data;
608 ti = get_tracefs(inode);
609 if (!(ti->flags & TRACEFS_EVENT_INODE))
612 if (WARN_ON_ONCE(!dlist))
615 for (i = 0; dlist->dentries && dlist->dentries[i]; i++) {
616 dput(dlist->dentries[i]);
619 cursor = dlist->cursor;
620 kfree(dlist->dentries);
622 file->private_data = cursor;
623 return dcache_dir_close(inode, file);
626 static int add_dentries(struct dentry ***dentries, struct dentry *d, int cnt)
630 tmp = krealloc(*dentries, sizeof(d) * (cnt + 2), GFP_NOFS);
640 * dcache_dir_open_wrapper - eventfs open wrapper
642 * @file: dir to be opened (to create it's children)
644 * Used to dynamic create file/dir with-in @file, all the
645 * file/dir will be created. If already created then references
648 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
650 const struct file_operations *fops;
651 const struct eventfs_entry *entry;
652 struct eventfs_inode *ei_child;
653 struct tracefs_inode *ti;
654 struct eventfs_inode *ei;
655 struct dentry_list *dlist;
656 struct dentry **dentries = NULL;
657 struct dentry *parent = file_dentry(file);
659 struct inode *f_inode = file_inode(file);
660 const char *name = parent->d_name.name;
669 ti = get_tracefs(f_inode);
670 if (!(ti->flags & TRACEFS_EVENT_INODE))
673 if (WARN_ON_ONCE(file->private_data))
676 idx = srcu_read_lock(&eventfs_srcu);
678 mutex_lock(&eventfs_mutex);
679 ei = READ_ONCE(ti->private);
680 mutex_unlock(&eventfs_mutex);
683 srcu_read_unlock(&eventfs_srcu, idx);
690 dlist = kmalloc(sizeof(*dlist), GFP_KERNEL);
692 srcu_read_unlock(&eventfs_srcu, idx);
696 inode_lock(parent->d_inode);
697 list_for_each_entry_srcu(ei_child, &ei->children, list,
698 srcu_read_lock_held(&eventfs_srcu)) {
699 d = create_dir_dentry(ei, ei_child, parent, false);
701 ret = add_dentries(&dentries, d, cnt);
708 for (i = 0; i < ei->nr_entries; i++) {
710 entry = &ei->entries[i];
712 mutex_lock(&eventfs_mutex);
713 /* If ei->is_freed, then the event itself may be too */
715 r = entry->callback(name, &mode, &cdata, &fops);
718 mutex_unlock(&eventfs_mutex);
721 d = create_file_dentry(ei, i, parent, name, mode, cdata, fops, false);
723 ret = add_dentries(&dentries, d, cnt);
729 inode_unlock(parent->d_inode);
730 srcu_read_unlock(&eventfs_srcu, idx);
731 ret = dcache_dir_open(inode, file);
734 * dcache_dir_open() sets file->private_data to a dentry cursor.
735 * Need to save that but also save all the dentries that were
736 * opened by this function.
738 dlist->cursor = file->private_data;
739 dlist->dentries = dentries;
740 file->private_data = dlist;
745 * This just sets the file->private_data back to the cursor and back.
747 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx)
749 struct dentry_list *dlist = file->private_data;
752 file->private_data = dlist->cursor;
753 ret = dcache_readdir(file, ctx);
754 dlist->cursor = file->private_data;
755 file->private_data = dlist;
760 * eventfs_create_dir - Create the eventfs_inode for this directory
761 * @name: The name of the directory to create.
762 * @parent: The eventfs_inode of the parent directory.
763 * @entries: A list of entries that represent the files under this directory
764 * @size: The number of @entries
765 * @data: The default data to pass to the files (an entry may override it).
767 * This function creates the descriptor to represent a directory in the
768 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
769 * used to create other children underneath.
771 * The @entries is an array of eventfs_entry structures which has:
773 * eventfs_callback callback;
775 * The name is the name of the file, and the callback is a pointer to a function
776 * that will be called when the file is reference (either by lookup or by
777 * reading a directory). The callback is of the prototype:
779 * int callback(const char *name, umode_t *mode, void **data,
780 * const struct file_operations **fops);
782 * When a file needs to be created, this callback will be called with
783 * name = the name of the file being created (so that the same callback
784 * may be used for multiple files).
785 * mode = a place to set the file's mode
786 * data = A pointer to @data, and the callback may replace it, which will
787 * cause the file created to pass the new data to the open() call.
788 * fops = the fops to use for the created file.
790 * NB. @callback is called while holding internal locks of the eventfs
791 * system. The callback must not call any code that might also call into
792 * the tracefs or eventfs system or it will risk creating a deadlock.
794 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
795 const struct eventfs_entry *entries,
796 int size, void *data)
798 struct eventfs_inode *ei;
801 return ERR_PTR(-EINVAL);
803 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
805 return ERR_PTR(-ENOMEM);
807 ei->name = kstrdup_const(name, GFP_KERNEL);
810 return ERR_PTR(-ENOMEM);
814 ei->d_children = kzalloc(sizeof(*ei->d_children) * size, GFP_KERNEL);
815 if (!ei->d_children) {
816 kfree_const(ei->name);
818 return ERR_PTR(-ENOMEM);
822 ei->entries = entries;
823 ei->nr_entries = size;
825 INIT_LIST_HEAD(&ei->children);
826 INIT_LIST_HEAD(&ei->list);
828 mutex_lock(&eventfs_mutex);
829 if (!parent->is_freed) {
830 list_add_tail(&ei->list, &parent->children);
831 ei->d_parent = parent->dentry;
833 mutex_unlock(&eventfs_mutex);
835 /* Was the parent freed? */
836 if (list_empty(&ei->list)) {
844 * eventfs_create_events_dir - create the top level events directory
845 * @name: The name of the top level directory to create.
846 * @parent: Parent dentry for this file in the tracefs directory.
847 * @entries: A list of entries that represent the files under this directory
848 * @size: The number of @entries
849 * @data: The default data to pass to the files (an entry may override it).
851 * This function creates the top of the trace event directory.
853 * See eventfs_create_dir() for use of @entries.
855 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
856 const struct eventfs_entry *entries,
857 int size, void *data)
859 struct dentry *dentry = tracefs_start_creating(name, parent);
860 struct eventfs_inode *ei;
861 struct tracefs_inode *ti;
864 if (security_locked_down(LOCKDOWN_TRACEFS))
868 return ERR_CAST(dentry);
870 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
874 inode = tracefs_get_inode(dentry->d_sb);
875 if (unlikely(!inode))
879 ei->d_children = kzalloc(sizeof(*ei->d_children) * size, GFP_KERNEL);
885 ei->entries = entries;
886 ei->nr_entries = size;
888 ei->name = kstrdup_const(name, GFP_KERNEL);
892 INIT_LIST_HEAD(&ei->children);
893 INIT_LIST_HEAD(&ei->list);
895 ti = get_tracefs(inode);
896 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
899 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
900 inode->i_op = &eventfs_root_dir_inode_operations;
901 inode->i_fop = &eventfs_file_operations;
903 dentry->d_fsdata = ei;
905 /* directory inodes start off with i_nlink == 2 (for "." entry) */
907 d_instantiate(dentry, inode);
908 inc_nlink(dentry->d_parent->d_inode);
909 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
910 tracefs_end_creating(dentry);
915 kfree(ei->d_children);
918 tracefs_failed_creating(dentry);
919 return ERR_PTR(-ENOMEM);
922 static LLIST_HEAD(free_list);
924 static void eventfs_workfn(struct work_struct *work)
926 struct eventfs_inode *ei, *tmp;
927 struct llist_node *llnode;
929 llnode = llist_del_all(&free_list);
930 llist_for_each_entry_safe(ei, tmp, llnode, llist) {
931 /* This dput() matches the dget() from unhook_dentry() */
932 for (int i = 0; i < ei->nr_entries; i++) {
933 if (ei->d_children[i])
934 dput(ei->d_children[i]);
936 /* This should only get here if it had a dentry */
937 if (!WARN_ON_ONCE(!ei->dentry))
942 static DECLARE_WORK(eventfs_work, eventfs_workfn);
944 static void free_rcu_ei(struct rcu_head *head)
946 struct eventfs_inode *ei = container_of(head, struct eventfs_inode, rcu);
949 /* Do not free the ei until all references of dentry are gone */
950 if (llist_add(&ei->llist, &free_list))
951 queue_work(system_unbound_wq, &eventfs_work);
955 /* If the ei doesn't have a dentry, neither should its children */
956 for (int i = 0; i < ei->nr_entries; i++) {
957 WARN_ON_ONCE(ei->d_children[i]);
963 static void unhook_dentry(struct dentry *dentry)
968 * Need to add a reference to the dentry that is expected by
969 * simple_recursive_removal(), which will include a dput().
974 * Also add a reference for the dput() in eventfs_workfn().
975 * That is required as that dput() will free the ei after
976 * the SRCU grace period is over.
982 * eventfs_remove_rec - remove eventfs dir or file from list
983 * @ei: eventfs_inode to be removed.
984 * @level: prevent recursion from going more than 3 levels deep.
986 * This function recursively removes eventfs_inodes which
987 * contains info of files and/or directories.
989 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
991 struct eventfs_inode *ei_child;
996 * Check recursion depth. It should never be greater than 3:
999 * 2 - events/group/event/
1000 * 3 - events/group/event/file
1002 if (WARN_ON_ONCE(level > 3))
1005 /* search for nested folders or files */
1006 list_for_each_entry_srcu(ei_child, &ei->children, list,
1007 lockdep_is_held(&eventfs_mutex)) {
1008 /* Children only have dentry if parent does */
1009 WARN_ON_ONCE(ei_child->dentry && !ei->dentry);
1010 eventfs_remove_rec(ei_child, level + 1);
1016 for (int i = 0; i < ei->nr_entries; i++) {
1017 if (ei->d_children[i]) {
1018 /* Children only have dentry if parent does */
1019 WARN_ON_ONCE(!ei->dentry);
1020 unhook_dentry(ei->d_children[i]);
1024 unhook_dentry(ei->dentry);
1026 list_del_rcu(&ei->list);
1027 call_srcu(&eventfs_srcu, &ei->rcu, free_rcu_ei);
1031 * eventfs_remove_dir - remove eventfs dir or file from list
1032 * @ei: eventfs_inode to be removed.
1034 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
1036 void eventfs_remove_dir(struct eventfs_inode *ei)
1038 struct dentry *dentry;
1043 mutex_lock(&eventfs_mutex);
1044 dentry = ei->dentry;
1045 eventfs_remove_rec(ei, 0);
1046 mutex_unlock(&eventfs_mutex);
1049 * If any of the ei children has a dentry, then the ei itself
1050 * must have a dentry.
1053 simple_recursive_removal(dentry, NULL);
1057 * eventfs_remove_events_dir - remove the top level eventfs directory
1058 * @ei: the event_inode returned by eventfs_create_events_dir().
1060 * This function removes the events main directory
1062 void eventfs_remove_events_dir(struct eventfs_inode *ei)
1064 struct dentry *dentry;
1066 dentry = ei->dentry;
1067 eventfs_remove_dir(ei);
1070 * Matches the dget() done by tracefs_start_creating()
1071 * in eventfs_create_events_dir() when it the dentry was
1072 * created. In other words, it's a normal dentry that
1073 * sticks around while the other ei->dentry are created
1074 * and destroyed dynamically.