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);
35 /* Choose something "unique" ;-) */
36 #define EVENTFS_FILE_INODE_INO 0x12c4e37
38 /* Just try to make something consistent and unique */
39 static int eventfs_dir_ino(struct eventfs_inode *ei)
42 ei->ino = get_next_ino();
48 * The eventfs_inode (ei) itself is protected by SRCU. It is released from
49 * its parent's list and will have is_freed set (under eventfs_mutex).
50 * After the SRCU grace period is over and the last dput() is called
53 DEFINE_STATIC_SRCU(eventfs_srcu);
55 /* Mode is unsigned short, use the upper bits for flags */
57 EVENTFS_SAVE_MODE = BIT(16),
58 EVENTFS_SAVE_UID = BIT(17),
59 EVENTFS_SAVE_GID = BIT(18),
60 EVENTFS_TOPLEVEL = BIT(19),
63 #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
65 static struct dentry *eventfs_root_lookup(struct inode *dir,
66 struct dentry *dentry,
68 static int eventfs_iterate(struct file *file, struct dir_context *ctx);
70 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
72 unsigned int ia_valid = iattr->ia_valid;
74 if (ia_valid & ATTR_MODE) {
75 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
76 (iattr->ia_mode & EVENTFS_MODE_MASK) |
79 if (ia_valid & ATTR_UID) {
80 attr->mode |= EVENTFS_SAVE_UID;
81 attr->uid = iattr->ia_uid;
83 if (ia_valid & ATTR_GID) {
84 attr->mode |= EVENTFS_SAVE_GID;
85 attr->gid = iattr->ia_gid;
89 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
92 const struct eventfs_entry *entry;
93 struct eventfs_inode *ei;
97 mutex_lock(&eventfs_mutex);
98 ei = dentry->d_fsdata;
100 /* Do not allow changes if the event is about to be removed. */
101 mutex_unlock(&eventfs_mutex);
105 /* Preallocate the children mode array if necessary */
106 if (!(dentry->d_inode->i_mode & S_IFDIR)) {
107 if (!ei->entry_attrs) {
108 ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
110 if (!ei->entry_attrs) {
117 ret = simple_setattr(idmap, dentry, iattr);
122 * If this is a dir, then update the ei cache, only the file
123 * mode is saved in the ei->m_children, and the ownership is
124 * determined by the parent directory.
126 if (dentry->d_inode->i_mode & S_IFDIR) {
128 * The events directory dentry is never freed, unless its
129 * part of an instance that is deleted. It's attr is the
130 * default for its child files and directories.
131 * Do not update it. It's not used for its own mode or ownership.
134 /* But it still needs to know if it was modified */
135 if (iattr->ia_valid & ATTR_UID)
136 ei->attr.mode |= EVENTFS_SAVE_UID;
137 if (iattr->ia_valid & ATTR_GID)
138 ei->attr.mode |= EVENTFS_SAVE_GID;
140 update_attr(&ei->attr, iattr);
144 name = dentry->d_name.name;
146 for (int i = 0; i < ei->nr_entries; i++) {
147 entry = &ei->entries[i];
148 if (strcmp(name, entry->name) == 0) {
149 update_attr(&ei->entry_attrs[i], iattr);
155 mutex_unlock(&eventfs_mutex);
159 static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
163 /* Only update if the "events" was on the top level */
164 if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
167 /* Get the tracefs root inode. */
168 root = d_inode(sb->s_root);
169 ei->attr.uid = root->i_uid;
170 ei->attr.gid = root->i_gid;
173 static void set_top_events_ownership(struct inode *inode)
175 struct tracefs_inode *ti = get_tracefs(inode);
176 struct eventfs_inode *ei = ti->private;
178 /* The top events directory doesn't get automatically updated */
179 if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
182 update_top_events_attr(ei, inode->i_sb);
184 if (!(ei->attr.mode & EVENTFS_SAVE_UID))
185 inode->i_uid = ei->attr.uid;
187 if (!(ei->attr.mode & EVENTFS_SAVE_GID))
188 inode->i_gid = ei->attr.gid;
191 static int eventfs_get_attr(struct mnt_idmap *idmap,
192 const struct path *path, struct kstat *stat,
193 u32 request_mask, unsigned int flags)
195 struct dentry *dentry = path->dentry;
196 struct inode *inode = d_backing_inode(dentry);
198 set_top_events_ownership(inode);
200 generic_fillattr(idmap, request_mask, inode, stat);
204 static int eventfs_permission(struct mnt_idmap *idmap,
205 struct inode *inode, int mask)
207 set_top_events_ownership(inode);
208 return generic_permission(idmap, inode, mask);
211 static const struct inode_operations eventfs_root_dir_inode_operations = {
212 .lookup = eventfs_root_lookup,
213 .setattr = eventfs_set_attr,
214 .getattr = eventfs_get_attr,
215 .permission = eventfs_permission,
218 static const struct inode_operations eventfs_file_inode_operations = {
219 .setattr = eventfs_set_attr,
222 static const struct file_operations eventfs_file_operations = {
223 .read = generic_read_dir,
224 .iterate_shared = eventfs_iterate,
225 .llseek = generic_file_llseek,
228 /* Return the evenfs_inode of the "events" directory */
229 static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
231 struct eventfs_inode *ei;
233 mutex_lock(&eventfs_mutex);
235 // The parent is stable because we do not do renames
236 dentry = dentry->d_parent;
237 // ... and directories always have d_fsdata
238 ei = dentry->d_fsdata;
241 * If the ei is being freed, the ownership of the children
248 // Walk upwards until you find the events inode
249 } while (!ei->is_events);
250 mutex_unlock(&eventfs_mutex);
252 update_top_events_attr(ei, dentry->d_sb);
257 static void update_inode_attr(struct dentry *dentry, struct inode *inode,
258 struct eventfs_attr *attr, umode_t mode)
260 struct eventfs_inode *events_ei = eventfs_find_events(dentry);
265 inode->i_mode = mode;
266 inode->i_uid = events_ei->attr.uid;
267 inode->i_gid = events_ei->attr.gid;
272 if (attr->mode & EVENTFS_SAVE_MODE)
273 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
275 if (attr->mode & EVENTFS_SAVE_UID)
276 inode->i_uid = attr->uid;
278 if (attr->mode & EVENTFS_SAVE_GID)
279 inode->i_gid = attr->gid;
283 * create_file - create a file in the tracefs filesystem
284 * @name: the name of the file to create.
285 * @mode: the permission that the file should have.
286 * @attr: saved attributes changed by user
287 * @parent: parent dentry for this file.
288 * @data: something that the caller will want to get to later on.
289 * @fop: struct file_operations that should be used for this file.
291 * This function creates a dentry that represents a file in the eventsfs_inode
292 * directory. The inode.i_private pointer will point to @data in the open()
295 static struct dentry *create_file(const char *name, umode_t mode,
296 struct eventfs_attr *attr,
297 struct dentry *parent, void *data,
298 const struct file_operations *fop)
300 struct tracefs_inode *ti;
301 struct dentry *dentry;
304 if (!(mode & S_IFMT))
307 if (WARN_ON_ONCE(!S_ISREG(mode)))
310 WARN_ON_ONCE(!parent);
311 dentry = eventfs_start_creating(name, parent);
316 inode = tracefs_get_inode(dentry->d_sb);
317 if (unlikely(!inode))
318 return eventfs_failed_creating(dentry);
320 /* If the user updated the directory's attributes, use them */
321 update_inode_attr(dentry, inode, attr, mode);
323 inode->i_op = &eventfs_file_inode_operations;
325 inode->i_private = data;
327 /* All files will have the same inode number */
328 inode->i_ino = EVENTFS_FILE_INODE_INO;
330 ti = get_tracefs(inode);
331 ti->flags |= TRACEFS_EVENT_INODE;
332 d_instantiate(dentry, inode);
333 fsnotify_create(dentry->d_parent->d_inode, dentry);
334 return eventfs_end_creating(dentry);
338 * create_dir - create a dir in the tracefs filesystem
339 * @ei: the eventfs_inode that represents the directory to create
340 * @parent: parent dentry for this file.
342 * This function will create a dentry for a directory represented by
345 static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent)
347 struct tracefs_inode *ti;
348 struct dentry *dentry;
351 dentry = eventfs_start_creating(ei->name, parent);
355 inode = tracefs_get_inode(dentry->d_sb);
356 if (unlikely(!inode))
357 return eventfs_failed_creating(dentry);
359 /* If the user updated the directory's attributes, use them */
360 update_inode_attr(dentry, inode, &ei->attr,
361 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
363 inode->i_op = &eventfs_root_dir_inode_operations;
364 inode->i_fop = &eventfs_file_operations;
366 /* All directories will have the same inode number */
367 inode->i_ino = eventfs_dir_ino(ei);
369 ti = get_tracefs(inode);
370 ti->flags |= TRACEFS_EVENT_INODE;
371 /* Only directories have ti->private set to an ei, not files */
375 d_instantiate(dentry, inode);
376 inc_nlink(dentry->d_parent->d_inode);
377 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
378 return eventfs_end_creating(dentry);
381 static void free_ei(struct eventfs_inode *ei)
383 kfree_const(ei->name);
384 kfree(ei->d_children);
385 kfree(ei->entry_attrs);
390 * eventfs_set_ei_status_free - remove the dentry reference from an eventfs_inode
391 * @ti: the tracefs_inode of the dentry
392 * @dentry: dentry which has the reference to remove.
394 * Remove the association between a dentry from an eventfs_inode.
396 void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry)
398 struct eventfs_inode *ei;
401 mutex_lock(&eventfs_mutex);
403 ei = dentry->d_fsdata;
407 /* This could belong to one of the files of the ei */
408 if (ei->dentry != dentry) {
409 for (i = 0; i < ei->nr_entries; i++) {
410 if (ei->d_children[i] == dentry)
413 if (WARN_ON_ONCE(i == ei->nr_entries))
415 ei->d_children[i] = NULL;
416 } else if (ei->is_freed) {
422 dentry->d_fsdata = NULL;
424 mutex_unlock(&eventfs_mutex);
428 * create_file_dentry - create a dentry for a file of an eventfs_inode
429 * @ei: the eventfs_inode that the file will be created under
430 * @idx: the index into the d_children[] of the @ei
431 * @parent: The parent dentry of the created file.
432 * @name: The name of the file to create
433 * @mode: The mode of the file.
434 * @data: The data to use to set the inode of the file with on open()
435 * @fops: The fops of the file to be created.
437 * Create a dentry for a file of an eventfs_inode @ei and place it into the
438 * address located at @e_dentry.
440 static struct dentry *
441 create_file_dentry(struct eventfs_inode *ei, int idx,
442 struct dentry *parent, const char *name, umode_t mode, void *data,
443 const struct file_operations *fops)
445 struct eventfs_attr *attr = NULL;
446 struct dentry **e_dentry = &ei->d_children[idx];
447 struct dentry *dentry;
449 WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
451 mutex_lock(&eventfs_mutex);
453 mutex_unlock(&eventfs_mutex);
456 /* If the e_dentry already has a dentry, use it */
459 mutex_unlock(&eventfs_mutex);
463 /* ei->entry_attrs are protected by SRCU */
465 attr = &ei->entry_attrs[idx];
467 mutex_unlock(&eventfs_mutex);
469 dentry = create_file(name, mode, attr, parent, data, fops);
471 mutex_lock(&eventfs_mutex);
473 if (IS_ERR_OR_NULL(dentry)) {
475 * When the mutex was released, something else could have
476 * created the dentry for this e_dentry. In which case
479 * If ei->is_freed is set, the e_dentry is currently on its
480 * way to being freed, don't return it. If e_dentry is NULL
481 * it means it was already freed.
489 mutex_unlock(&eventfs_mutex);
493 if (!*e_dentry && !ei->is_freed) {
495 dentry->d_fsdata = ei;
498 * Should never happen unless we get here due to being freed.
499 * Otherwise it means two dentries exist with the same name.
501 WARN_ON_ONCE(!ei->is_freed);
504 mutex_unlock(&eventfs_mutex);
510 * eventfs_post_create_dir - post create dir routine
511 * @ei: eventfs_inode of recently created dir
513 * Map the meta-data of files within an eventfs dir to their parent dentry
515 static void eventfs_post_create_dir(struct eventfs_inode *ei)
517 struct eventfs_inode *ei_child;
519 lockdep_assert_held(&eventfs_mutex);
521 /* srcu lock already held */
522 /* fill parent-child relation */
523 list_for_each_entry_srcu(ei_child, &ei->children, list,
524 srcu_read_lock_held(&eventfs_srcu)) {
525 ei_child->d_parent = ei->dentry;
530 * create_dir_dentry - Create a directory dentry for the eventfs_inode
531 * @pei: The eventfs_inode parent of ei.
532 * @ei: The eventfs_inode to create the directory for
533 * @parent: The dentry of the parent of this directory
535 * This creates and attaches a directory dentry to the eventfs_inode @ei.
537 static struct dentry *
538 create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei,
539 struct dentry *parent)
541 struct dentry *dentry = NULL;
543 WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
545 mutex_lock(&eventfs_mutex);
546 if (pei->is_freed || ei->is_freed) {
547 mutex_unlock(&eventfs_mutex);
551 /* If the eventfs_inode already has a dentry, use it */
554 mutex_unlock(&eventfs_mutex);
557 mutex_unlock(&eventfs_mutex);
559 dentry = create_dir(ei, parent);
561 mutex_lock(&eventfs_mutex);
563 if (IS_ERR_OR_NULL(dentry) && !ei->is_freed) {
565 * When the mutex was released, something else could have
566 * created the dentry for this e_dentry. In which case
569 * If ei->is_freed is set, the e_dentry is currently on its
570 * way to being freed.
575 mutex_unlock(&eventfs_mutex);
579 if (!ei->dentry && !ei->is_freed) {
581 eventfs_post_create_dir(ei);
582 dentry->d_fsdata = ei;
585 * Should never happen unless we get here due to being freed.
586 * Otherwise it means two dentries exist with the same name.
588 WARN_ON_ONCE(!ei->is_freed);
591 mutex_unlock(&eventfs_mutex);
597 * eventfs_root_lookup - lookup routine to create file/dir
598 * @dir: in which a lookup is being done
599 * @dentry: file/dir dentry
600 * @flags: Just passed to simple_lookup()
602 * Used to create dynamic file/dir with-in @dir, search with-in @ei
603 * list, if @dentry found go ahead and create the file/dir
606 static struct dentry *eventfs_root_lookup(struct inode *dir,
607 struct dentry *dentry,
610 const struct file_operations *fops;
611 const struct eventfs_entry *entry;
612 struct eventfs_inode *ei_child;
613 struct tracefs_inode *ti;
614 struct eventfs_inode *ei;
615 struct dentry *ei_dentry = NULL;
616 struct dentry *ret = NULL;
618 const char *name = dentry->d_name.name;
625 ti = get_tracefs(dir);
626 if (!(ti->flags & TRACEFS_EVENT_INODE))
629 /* Grab srcu to prevent the ei from going away */
630 idx = srcu_read_lock(&eventfs_srcu);
633 * Grab the eventfs_mutex to consistent value from ti->private.
636 mutex_lock(&eventfs_mutex);
637 ei = READ_ONCE(ti->private);
638 if (ei && !ei->is_freed)
639 ei_dentry = READ_ONCE(ei->dentry);
640 mutex_unlock(&eventfs_mutex);
642 if (!ei || !ei_dentry)
647 list_for_each_entry_srcu(ei_child, &ei->children, list,
648 srcu_read_lock_held(&eventfs_srcu)) {
649 if (strcmp(ei_child->name, name) != 0)
651 ret = simple_lookup(dir, dentry, flags);
654 d = create_dir_dentry(ei, ei_child, ei_dentry);
659 for (i = 0; i < ei->nr_entries; i++) {
660 entry = &ei->entries[i];
661 if (strcmp(name, entry->name) == 0) {
663 mutex_lock(&eventfs_mutex);
664 /* If ei->is_freed, then the event itself may be too */
666 r = entry->callback(name, &mode, &cdata, &fops);
669 mutex_unlock(&eventfs_mutex);
672 ret = simple_lookup(dir, dentry, flags);
675 d = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops);
681 srcu_read_unlock(&eventfs_srcu, idx);
686 * Walk the children of a eventfs_inode to fill in getdents().
688 static int eventfs_iterate(struct file *file, struct dir_context *ctx)
690 const struct file_operations *fops;
691 struct inode *f_inode = file_inode(file);
692 const struct eventfs_entry *entry;
693 struct eventfs_inode *ei_child;
694 struct tracefs_inode *ti;
695 struct eventfs_inode *ei;
703 if (!dir_emit_dots(file, ctx))
706 ti = get_tracefs(f_inode);
707 if (!(ti->flags & TRACEFS_EVENT_INODE))
712 idx = srcu_read_lock(&eventfs_srcu);
714 mutex_lock(&eventfs_mutex);
715 ei = READ_ONCE(ti->private);
716 if (ei && ei->is_freed)
718 mutex_unlock(&eventfs_mutex);
724 * Need to create the dentries and inodes to have a consistent
729 /* Start at 'c' to jump over already read entries */
730 for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
731 void *cdata = ei->data;
733 entry = &ei->entries[i];
736 mutex_lock(&eventfs_mutex);
737 /* If ei->is_freed then just bail here, nothing more to do */
739 mutex_unlock(&eventfs_mutex);
742 r = entry->callback(name, &mode, &cdata, &fops);
743 mutex_unlock(&eventfs_mutex);
747 ino = EVENTFS_FILE_INODE_INO;
749 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
753 /* Subtract the skipped entries above */
754 c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
756 list_for_each_entry_srcu(ei_child, &ei->children, list,
757 srcu_read_lock_held(&eventfs_srcu)) {
766 if (ei_child->is_freed)
769 name = ei_child->name;
771 ino = eventfs_dir_ino(ei_child);
773 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
778 srcu_read_unlock(&eventfs_srcu, idx);
783 /* Incremented ctx->pos without adding something, reset it */
789 * eventfs_create_dir - Create the eventfs_inode for this directory
790 * @name: The name of the directory to create.
791 * @parent: The eventfs_inode of the parent directory.
792 * @entries: A list of entries that represent the files under this directory
793 * @size: The number of @entries
794 * @data: The default data to pass to the files (an entry may override it).
796 * This function creates the descriptor to represent a directory in the
797 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
798 * used to create other children underneath.
800 * The @entries is an array of eventfs_entry structures which has:
802 * eventfs_callback callback;
804 * The name is the name of the file, and the callback is a pointer to a function
805 * that will be called when the file is reference (either by lookup or by
806 * reading a directory). The callback is of the prototype:
808 * int callback(const char *name, umode_t *mode, void **data,
809 * const struct file_operations **fops);
811 * When a file needs to be created, this callback will be called with
812 * name = the name of the file being created (so that the same callback
813 * may be used for multiple files).
814 * mode = a place to set the file's mode
815 * data = A pointer to @data, and the callback may replace it, which will
816 * cause the file created to pass the new data to the open() call.
817 * fops = the fops to use for the created file.
819 * NB. @callback is called while holding internal locks of the eventfs
820 * system. The callback must not call any code that might also call into
821 * the tracefs or eventfs system or it will risk creating a deadlock.
823 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
824 const struct eventfs_entry *entries,
825 int size, void *data)
827 struct eventfs_inode *ei;
830 return ERR_PTR(-EINVAL);
832 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
834 return ERR_PTR(-ENOMEM);
836 ei->name = kstrdup_const(name, GFP_KERNEL);
839 return ERR_PTR(-ENOMEM);
843 ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL);
844 if (!ei->d_children) {
845 kfree_const(ei->name);
847 return ERR_PTR(-ENOMEM);
851 ei->entries = entries;
852 ei->nr_entries = size;
854 INIT_LIST_HEAD(&ei->children);
855 INIT_LIST_HEAD(&ei->list);
857 mutex_lock(&eventfs_mutex);
858 if (!parent->is_freed) {
859 list_add_tail(&ei->list, &parent->children);
860 ei->d_parent = parent->dentry;
862 mutex_unlock(&eventfs_mutex);
864 /* Was the parent freed? */
865 if (list_empty(&ei->list)) {
873 * eventfs_create_events_dir - create the top level events directory
874 * @name: The name of the top level directory to create.
875 * @parent: Parent dentry for this file in the tracefs directory.
876 * @entries: A list of entries that represent the files under this directory
877 * @size: The number of @entries
878 * @data: The default data to pass to the files (an entry may override it).
880 * This function creates the top of the trace event directory.
882 * See eventfs_create_dir() for use of @entries.
884 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
885 const struct eventfs_entry *entries,
886 int size, void *data)
888 struct dentry *dentry = tracefs_start_creating(name, parent);
889 struct eventfs_inode *ei;
890 struct tracefs_inode *ti;
895 if (security_locked_down(LOCKDOWN_TRACEFS))
899 return ERR_CAST(dentry);
901 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
905 inode = tracefs_get_inode(dentry->d_sb);
906 if (unlikely(!inode))
910 ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL);
916 ei->entries = entries;
917 ei->nr_entries = size;
920 ei->name = kstrdup_const(name, GFP_KERNEL);
924 /* Save the ownership of this directory */
925 uid = d_inode(dentry->d_parent)->i_uid;
926 gid = d_inode(dentry->d_parent)->i_gid;
929 * If the events directory is of the top instance, then parent
930 * is NULL. Set the attr.mode to reflect this and its permissions will
931 * default to the tracefs root dentry.
934 ei->attr.mode = EVENTFS_TOPLEVEL;
936 /* This is used as the default ownership of the files and directories */
940 INIT_LIST_HEAD(&ei->children);
941 INIT_LIST_HEAD(&ei->list);
943 ti = get_tracefs(inode);
944 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
947 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
950 inode->i_op = &eventfs_root_dir_inode_operations;
951 inode->i_fop = &eventfs_file_operations;
953 dentry->d_fsdata = ei;
955 /* directory inodes start off with i_nlink == 2 (for "." entry) */
957 d_instantiate(dentry, inode);
958 inc_nlink(dentry->d_parent->d_inode);
959 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
960 tracefs_end_creating(dentry);
965 kfree(ei->d_children);
968 tracefs_failed_creating(dentry);
969 return ERR_PTR(-ENOMEM);
972 static LLIST_HEAD(free_list);
974 static void eventfs_workfn(struct work_struct *work)
976 struct eventfs_inode *ei, *tmp;
977 struct llist_node *llnode;
979 llnode = llist_del_all(&free_list);
980 llist_for_each_entry_safe(ei, tmp, llnode, llist) {
981 /* This dput() matches the dget() from unhook_dentry() */
982 for (int i = 0; i < ei->nr_entries; i++) {
983 if (ei->d_children[i])
984 dput(ei->d_children[i]);
986 /* This should only get here if it had a dentry */
987 if (!WARN_ON_ONCE(!ei->dentry))
992 static DECLARE_WORK(eventfs_work, eventfs_workfn);
994 static void free_rcu_ei(struct rcu_head *head)
996 struct eventfs_inode *ei = container_of(head, struct eventfs_inode, rcu);
999 /* Do not free the ei until all references of dentry are gone */
1000 if (llist_add(&ei->llist, &free_list))
1001 queue_work(system_unbound_wq, &eventfs_work);
1005 /* If the ei doesn't have a dentry, neither should its children */
1006 for (int i = 0; i < ei->nr_entries; i++) {
1007 WARN_ON_ONCE(ei->d_children[i]);
1013 static void unhook_dentry(struct dentry *dentry)
1018 * Need to add a reference to the dentry that is expected by
1019 * simple_recursive_removal(), which will include a dput().
1024 * Also add a reference for the dput() in eventfs_workfn().
1025 * That is required as that dput() will free the ei after
1026 * the SRCU grace period is over.
1032 * eventfs_remove_rec - remove eventfs dir or file from list
1033 * @ei: eventfs_inode to be removed.
1034 * @level: prevent recursion from going more than 3 levels deep.
1036 * This function recursively removes eventfs_inodes which
1037 * contains info of files and/or directories.
1039 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
1041 struct eventfs_inode *ei_child;
1046 * Check recursion depth. It should never be greater than 3:
1049 * 2 - events/group/event/
1050 * 3 - events/group/event/file
1052 if (WARN_ON_ONCE(level > 3))
1055 /* search for nested folders or files */
1056 list_for_each_entry_srcu(ei_child, &ei->children, list,
1057 lockdep_is_held(&eventfs_mutex)) {
1058 /* Children only have dentry if parent does */
1059 WARN_ON_ONCE(ei_child->dentry && !ei->dentry);
1060 eventfs_remove_rec(ei_child, level + 1);
1066 for (int i = 0; i < ei->nr_entries; i++) {
1067 if (ei->d_children[i]) {
1068 /* Children only have dentry if parent does */
1069 WARN_ON_ONCE(!ei->dentry);
1070 unhook_dentry(ei->d_children[i]);
1074 unhook_dentry(ei->dentry);
1076 list_del_rcu(&ei->list);
1077 call_srcu(&eventfs_srcu, &ei->rcu, free_rcu_ei);
1081 * eventfs_remove_dir - remove eventfs dir or file from list
1082 * @ei: eventfs_inode to be removed.
1084 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
1086 void eventfs_remove_dir(struct eventfs_inode *ei)
1088 struct dentry *dentry;
1093 mutex_lock(&eventfs_mutex);
1094 dentry = ei->dentry;
1095 eventfs_remove_rec(ei, 0);
1096 mutex_unlock(&eventfs_mutex);
1099 * If any of the ei children has a dentry, then the ei itself
1100 * must have a dentry.
1103 simple_recursive_removal(dentry, NULL);
1107 * eventfs_remove_events_dir - remove the top level eventfs directory
1108 * @ei: the event_inode returned by eventfs_create_events_dir().
1110 * This function removes the events main directory
1112 void eventfs_remove_events_dir(struct eventfs_inode *ei)
1114 struct dentry *dentry;
1116 dentry = ei->dentry;
1117 eventfs_remove_dir(ei);
1120 * Matches the dget() done by tracefs_start_creating()
1121 * in eventfs_create_events_dir() when it the dentry was
1122 * created. In other words, it's a normal dentry that
1123 * sticks around while the other ei->dentry are created
1124 * and destroyed dynamically.