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 (VMware) <rostedt@goodmis.org>
6 * Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com>
8 * eventfs is used to dynamically create inodes and dentries based on the
9 * meta data provided by the tracing system.
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.
16 #include <linux/fsnotify.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>
26 struct eventfs_inode {
27 struct list_head e_top_files;
31 * struct eventfs_file - hold the properties of the eventfs files and
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
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;
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
58 struct list_head del_list;
60 unsigned long is_freed;
66 static DEFINE_MUTEX(eventfs_mutex);
67 DEFINE_STATIC_SRCU(eventfs_srcu);
69 static struct dentry *eventfs_root_lookup(struct inode *dir,
70 struct dentry *dentry,
72 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file);
73 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx);
74 static int eventfs_release(struct inode *inode, struct file *file);
76 static const struct inode_operations eventfs_root_dir_inode_operations = {
77 .lookup = eventfs_root_lookup,
80 static const struct file_operations eventfs_file_operations = {
81 .open = dcache_dir_open_wrapper,
82 .read = generic_read_dir,
83 .iterate_shared = dcache_readdir_wrapper,
84 .llseek = generic_file_llseek,
85 .release = eventfs_release,
89 * create_file - create a file in the tracefs filesystem
90 * @name: the name of the file to create.
91 * @mode: the permission that the file should have.
92 * @parent: parent dentry for this file.
93 * @data: something that the caller will want to get to later on.
94 * @fop: struct file_operations that should be used for this file.
96 * This is the basic "create a file" function for tracefs. It allows for a
97 * wide range of flexibility in creating a file.
99 * This function will return a pointer to a dentry if it succeeds. This
100 * pointer must be passed to the tracefs_remove() function when the file is
101 * to be removed (no automatic cleanup happens if your module is unloaded,
102 * you are responsible here.) If an error occurs, %NULL will be returned.
104 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
107 static struct dentry *create_file(const char *name, umode_t mode,
108 struct dentry *parent, void *data,
109 const struct file_operations *fop)
111 struct tracefs_inode *ti;
112 struct dentry *dentry;
115 if (!(mode & S_IFMT))
118 if (WARN_ON_ONCE(!S_ISREG(mode)))
121 dentry = eventfs_start_creating(name, parent);
126 inode = tracefs_get_inode(dentry->d_sb);
127 if (unlikely(!inode))
128 return eventfs_failed_creating(dentry);
130 inode->i_mode = mode;
132 inode->i_private = data;
134 ti = get_tracefs(inode);
135 ti->flags |= TRACEFS_EVENT_INODE;
136 d_instantiate(dentry, inode);
137 fsnotify_create(dentry->d_parent->d_inode, dentry);
138 return eventfs_end_creating(dentry);
142 * create_dir - create a dir in the tracefs filesystem
143 * @name: the name of the file to create.
144 * @parent: parent dentry for this file.
145 * @data: something that the caller will want to get to later on.
147 * This is the basic "create a dir" function for eventfs. It allows for a
148 * wide range of flexibility in creating a dir.
150 * This function will return a pointer to a dentry if it succeeds. This
151 * pointer must be passed to the tracefs_remove() function when the file is
152 * to be removed (no automatic cleanup happens if your module is unloaded,
153 * you are responsible here.) If an error occurs, %NULL will be returned.
155 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
158 static struct dentry *create_dir(const char *name, struct dentry *parent, void *data)
160 struct tracefs_inode *ti;
161 struct dentry *dentry;
164 dentry = eventfs_start_creating(name, parent);
168 inode = tracefs_get_inode(dentry->d_sb);
169 if (unlikely(!inode))
170 return eventfs_failed_creating(dentry);
172 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
173 inode->i_op = &eventfs_root_dir_inode_operations;
174 inode->i_fop = &eventfs_file_operations;
175 inode->i_private = data;
177 ti = get_tracefs(inode);
178 ti->flags |= TRACEFS_EVENT_INODE;
181 d_instantiate(dentry, inode);
182 inc_nlink(dentry->d_parent->d_inode);
183 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
184 return eventfs_end_creating(dentry);
188 * eventfs_set_ef_status_free - set the ef->status to free
189 * @ti: the tracefs_inode of the dentry
190 * @dentry: dentry who's status to be freed
192 * eventfs_set_ef_status_free will be called if no more
195 void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry)
197 struct tracefs_inode *ti_parent;
198 struct eventfs_inode *ei;
199 struct eventfs_file *ef, *tmp;
201 /* The top level events directory may be freed by this */
202 if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) {
203 LIST_HEAD(ef_del_list);
205 mutex_lock(&eventfs_mutex);
209 /* Record all the top level files */
210 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
211 lockdep_is_held(&eventfs_mutex)) {
212 list_add_tail(&ef->del_list, &ef_del_list);
215 /* Nothing should access this, but just in case! */
218 mutex_unlock(&eventfs_mutex);
220 /* Now safely free the top level files and their children */
221 list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
222 list_del(&ef->del_list);
230 mutex_lock(&eventfs_mutex);
232 ti_parent = get_tracefs(dentry->d_parent->d_inode);
233 if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
236 ef = dentry->d_fsdata;
241 * If ef was freed, then the LSB bit is set for d_fsdata.
242 * But this should not happen, as it should still have a
243 * ref count that prevents it. Warn in case it does.
245 if (WARN_ON_ONCE((unsigned long)ef & 1))
248 dentry->d_fsdata = NULL;
251 mutex_unlock(&eventfs_mutex);
255 * eventfs_post_create_dir - post create dir routine
256 * @ef: eventfs_file of recently created dir
258 * Map the meta-data of files within an eventfs dir to their parent dentry
260 static void eventfs_post_create_dir(struct eventfs_file *ef)
262 struct eventfs_file *ef_child;
263 struct tracefs_inode *ti;
265 /* srcu lock already held */
266 /* fill parent-child relation */
267 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
268 srcu_read_lock_held(&eventfs_srcu)) {
269 ef_child->d_parent = ef->dentry;
272 ti = get_tracefs(ef->dentry->d_inode);
273 ti->private = ef->ei;
277 * create_dentry - helper function to create dentry
278 * @ef: eventfs_file of file or directory to create
279 * @parent: parent dentry
280 * @lookup: true if called from lookup routine
282 * Used to create a dentry for file/dir, executes post dentry creation routine
284 static struct dentry *
285 create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup)
287 bool invalidate = false;
288 struct dentry *dentry;
290 mutex_lock(&eventfs_mutex);
292 mutex_unlock(&eventfs_mutex);
297 /* On dir open, up the ref count */
300 mutex_unlock(&eventfs_mutex);
303 mutex_unlock(&eventfs_mutex);
306 inode_lock(parent->d_inode);
309 dentry = create_dir(ef->name, parent, ef->data);
311 dentry = create_file(ef->name, ef->mode, parent,
315 inode_unlock(parent->d_inode);
317 mutex_lock(&eventfs_mutex);
318 if (IS_ERR_OR_NULL(dentry)) {
319 /* If the ef was already updated get it */
321 if (dentry && !lookup)
323 mutex_unlock(&eventfs_mutex);
327 if (!ef->dentry && !ef->is_freed) {
330 eventfs_post_create_dir(ef);
331 dentry->d_fsdata = ef;
333 /* A race here, should try again (unless freed) */
337 * Should never happen unless we get here due to being freed.
338 * Otherwise it means two dentries exist with the same name.
340 WARN_ON_ONCE(!ef->is_freed);
342 mutex_unlock(&eventfs_mutex);
344 d_invalidate(dentry);
346 if (lookup || invalidate)
349 return invalidate ? NULL : dentry;
352 static bool match_event_file(struct eventfs_file *ef, const char *name)
356 mutex_lock(&eventfs_mutex);
357 ret = !ef->is_freed && strcmp(ef->name, name) == 0;
358 mutex_unlock(&eventfs_mutex);
364 * eventfs_root_lookup - lookup routine to create file/dir
365 * @dir: in which a lookup is being done
366 * @dentry: file/dir dentry
367 * @flags: to pass as flags parameter to simple lookup
369 * Used to create a dynamic file/dir within @dir. Use the eventfs_inode
370 * list of meta data to find the information needed to create the file/dir.
372 static struct dentry *eventfs_root_lookup(struct inode *dir,
373 struct dentry *dentry,
376 struct tracefs_inode *ti;
377 struct eventfs_inode *ei;
378 struct eventfs_file *ef;
379 struct dentry *ret = NULL;
382 ti = get_tracefs(dir);
383 if (!(ti->flags & TRACEFS_EVENT_INODE))
387 idx = srcu_read_lock(&eventfs_srcu);
388 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
389 srcu_read_lock_held(&eventfs_srcu)) {
390 if (!match_event_file(ef, dentry->d_name.name))
392 ret = simple_lookup(dir, dentry, flags);
393 create_dentry(ef, ef->d_parent, true);
396 srcu_read_unlock(&eventfs_srcu, idx);
402 struct dentry **dentries;
406 * eventfs_release - called to release eventfs file/dir
407 * @inode: inode to be released
408 * @file: file to be released (not used)
410 static int eventfs_release(struct inode *inode, struct file *file)
412 struct tracefs_inode *ti;
413 struct dentry_list *dlist = file->private_data;
417 ti = get_tracefs(inode);
418 if (!(ti->flags & TRACEFS_EVENT_INODE))
421 if (WARN_ON_ONCE(!dlist))
424 for (i = 0; dlist->dentries && dlist->dentries[i]; i++) {
425 dput(dlist->dentries[i]);
428 cursor = dlist->cursor;
429 kfree(dlist->dentries);
431 file->private_data = cursor;
432 return dcache_dir_close(inode, file);
436 * dcache_dir_open_wrapper - eventfs open wrapper
438 * @file: dir to be opened (to create its child)
440 * Used to dynamically create the file/dir within @file. @file is really a
441 * directory and all the files/dirs of the children within @file will be
442 * created. If any of the files/dirs have already been created, their
443 * reference count will be incremented.
445 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
447 struct tracefs_inode *ti;
448 struct eventfs_inode *ei;
449 struct eventfs_file *ef;
450 struct dentry_list *dlist;
451 struct dentry **dentries = NULL;
452 struct dentry *dentry = file_dentry(file);
454 struct inode *f_inode = file_inode(file);
459 ti = get_tracefs(f_inode);
460 if (!(ti->flags & TRACEFS_EVENT_INODE))
463 if (WARN_ON_ONCE(file->private_data))
466 dlist = kmalloc(sizeof(*dlist), GFP_KERNEL);
471 idx = srcu_read_lock(&eventfs_srcu);
472 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
473 srcu_read_lock_held(&eventfs_srcu)) {
474 d = create_dentry(ef, dentry, false);
478 tmp = krealloc(dentries, sizeof(d) * (cnt + 2), GFP_KERNEL);
487 srcu_read_unlock(&eventfs_srcu, idx);
488 ret = dcache_dir_open(inode, file);
491 * dcache_dir_open() sets file->private_data to a dentry cursor.
492 * Need to save that but also save all the dentries that were
493 * opened by this function.
495 dlist->cursor = file->private_data;
496 dlist->dentries = dentries;
497 file->private_data = dlist;
502 * This just sets the file->private_data back to the cursor and back.
504 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx)
506 struct dentry_list *dlist = file->private_data;
509 file->private_data = dlist->cursor;
510 ret = dcache_readdir(file, ctx);
511 dlist->cursor = file->private_data;
512 file->private_data = dlist;
517 * eventfs_prepare_ef - helper function to prepare eventfs_file
518 * @name: the name of the file/directory to create.
519 * @mode: the permission that the file should have.
520 * @fop: struct file_operations that should be used for this file/directory.
521 * @iop: struct inode_operations that should be used for this file/directory.
522 * @data: something that the caller will want to get to later on. The
523 * inode.i_private pointer will point to this value on the open() call.
525 * This function allocates and fills the eventfs_file structure.
527 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode,
528 const struct file_operations *fop,
529 const struct inode_operations *iop,
532 struct eventfs_file *ef;
534 ef = kzalloc(sizeof(*ef), GFP_KERNEL);
536 return ERR_PTR(-ENOMEM);
538 ef->name = kstrdup(name, GFP_KERNEL);
541 return ERR_PTR(-ENOMEM);
545 ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL);
549 return ERR_PTR(-ENOMEM);
551 INIT_LIST_HEAD(&ef->ei->e_top_files);
564 * eventfs_create_events_dir - create the trace event structure
565 * @name: the name of the directory to create.
566 * @parent: parent dentry for this file. This should be a directory dentry
567 * if set. If this parameter is NULL, then the directory will be
568 * created in the root of the tracefs filesystem.
570 * This function creates the top of the trace event directory.
572 struct dentry *eventfs_create_events_dir(const char *name,
573 struct dentry *parent)
575 struct dentry *dentry = tracefs_start_creating(name, parent);
576 struct eventfs_inode *ei;
577 struct tracefs_inode *ti;
580 if (security_locked_down(LOCKDOWN_TRACEFS))
586 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
588 return ERR_PTR(-ENOMEM);
589 inode = tracefs_get_inode(dentry->d_sb);
590 if (unlikely(!inode)) {
592 tracefs_failed_creating(dentry);
593 return ERR_PTR(-ENOMEM);
596 INIT_LIST_HEAD(&ei->e_top_files);
598 ti = get_tracefs(inode);
599 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
602 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
603 inode->i_op = &eventfs_root_dir_inode_operations;
604 inode->i_fop = &eventfs_file_operations;
606 /* directory inodes start off with i_nlink == 2 (for "." entry) */
608 d_instantiate(dentry, inode);
609 inc_nlink(dentry->d_parent->d_inode);
610 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
611 return tracefs_end_creating(dentry);
615 * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later
616 * @name: the name of the file to create.
617 * @parent: parent dentry for this dir.
619 * This function adds eventfs subsystem dir to list.
620 * And all these dirs are created on the fly when they are looked up,
621 * and the dentry and inodes will be removed when they are done.
623 struct eventfs_file *eventfs_add_subsystem_dir(const char *name,
624 struct dentry *parent)
626 struct tracefs_inode *ti_parent;
627 struct eventfs_inode *ei_parent;
628 struct eventfs_file *ef;
630 if (security_locked_down(LOCKDOWN_TRACEFS))
634 return ERR_PTR(-EINVAL);
636 ti_parent = get_tracefs(parent->d_inode);
637 ei_parent = ti_parent->private;
639 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
643 mutex_lock(&eventfs_mutex);
644 list_add_tail(&ef->list, &ei_parent->e_top_files);
645 ef->d_parent = parent;
646 mutex_unlock(&eventfs_mutex);
651 * eventfs_add_dir - add eventfs dir to list to create later
652 * @name: the name of the file to create.
653 * @ef_parent: parent eventfs_file for this dir.
655 * This function adds eventfs dir to list.
656 * And all these dirs are created on the fly when they are looked up,
657 * and the dentry and inodes will be removed when they are done.
659 struct eventfs_file *eventfs_add_dir(const char *name,
660 struct eventfs_file *ef_parent)
662 struct eventfs_file *ef;
664 if (security_locked_down(LOCKDOWN_TRACEFS))
668 return ERR_PTR(-EINVAL);
670 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
674 mutex_lock(&eventfs_mutex);
675 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
676 ef->d_parent = ef_parent->dentry;
677 mutex_unlock(&eventfs_mutex);
682 * eventfs_add_events_file - add the data needed to create a file for later reference
683 * @name: the name of the file to create.
684 * @mode: the permission that the file should have.
685 * @parent: parent dentry for this file.
686 * @data: something that the caller will want to get to later on.
687 * @fop: struct file_operations that should be used for this file.
689 * This function is used to add the information needed to create a
690 * dentry/inode within the top level events directory. The file created
691 * will have the @mode permissions. The @data will be used to fill the
692 * inode.i_private when the open() call is done. The dentry and inodes are
693 * all created when they are referenced, and removed when they are no
696 int eventfs_add_events_file(const char *name, umode_t mode,
697 struct dentry *parent, void *data,
698 const struct file_operations *fop)
700 struct tracefs_inode *ti;
701 struct eventfs_inode *ei;
702 struct eventfs_file *ef;
704 if (security_locked_down(LOCKDOWN_TRACEFS))
710 if (!(mode & S_IFMT))
713 if (!parent->d_inode)
716 ti = get_tracefs(parent->d_inode);
717 if (!(ti->flags & TRACEFS_EVENT_INODE))
721 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
726 mutex_lock(&eventfs_mutex);
727 list_add_tail(&ef->list, &ei->e_top_files);
728 ef->d_parent = parent;
729 mutex_unlock(&eventfs_mutex);
734 * eventfs_add_file - add eventfs file to list to create later
735 * @name: the name of the file to create.
736 * @mode: the permission that the file should have.
737 * @ef_parent: parent eventfs_file for this file.
738 * @data: something that the caller will want to get to later on.
739 * @fop: struct file_operations that should be used for this file.
741 * This function is used to add the information needed to create a
742 * file within a subdirectory of the events directory. The file created
743 * will have the @mode permissions. The @data will be used to fill the
744 * inode.i_private when the open() call is done. The dentry and inodes are
745 * all created when they are referenced, and removed when they are no
748 int eventfs_add_file(const char *name, umode_t mode,
749 struct eventfs_file *ef_parent,
751 const struct file_operations *fop)
753 struct eventfs_file *ef;
755 if (security_locked_down(LOCKDOWN_TRACEFS))
761 if (!(mode & S_IFMT))
764 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
768 mutex_lock(&eventfs_mutex);
769 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
770 ef->d_parent = ef_parent->dentry;
771 mutex_unlock(&eventfs_mutex);
775 static void free_ef(struct rcu_head *head)
777 struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu);
785 * eventfs_remove_rec - remove eventfs dir or file from list
786 * @ef: eventfs_file to be removed.
787 * @head: to create list of eventfs_file to be deleted
788 * @level: to check recursion depth
790 * The helper function eventfs_remove_rec() is used to clean up and free the
791 * associated data from eventfs for both of the added functions.
793 static void eventfs_remove_rec(struct eventfs_file *ef, struct list_head *head, int level)
795 struct eventfs_file *ef_child;
800 * Check recursion depth. It should never be greater than 3:
803 * 2 - events/group/event/
804 * 3 - events/group/event/file
806 if (WARN_ON_ONCE(level > 3))
810 /* search for nested folders or files */
811 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
812 lockdep_is_held(&eventfs_mutex)) {
813 eventfs_remove_rec(ef_child, head, level + 1);
817 list_del_rcu(&ef->list);
818 list_add_tail(&ef->del_list, head);
822 * eventfs_remove - remove eventfs dir or file from list
823 * @ef: eventfs_file to be removed.
825 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
827 void eventfs_remove(struct eventfs_file *ef)
829 struct eventfs_file *tmp;
830 LIST_HEAD(ef_del_list);
831 struct dentry *dentry_list = NULL;
832 struct dentry *dentry;
837 mutex_lock(&eventfs_mutex);
838 eventfs_remove_rec(ef, &ef_del_list, 0);
839 list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
841 unsigned long ptr = (unsigned long)dentry_list;
843 /* Keep the dentry from being freed yet */
847 * Paranoid: The dget() above should prevent the dentry
848 * from being freed and calling eventfs_set_ef_status_free().
849 * But just in case, set the link list LSB pointer to 1
850 * and have eventfs_set_ef_status_free() check that to
851 * make sure that if it does happen, it will not think
852 * the d_fsdata is an event_file.
854 * For this to work, no event_file should be allocated
855 * on a odd space, as the ef should always be allocated
856 * to be at least word aligned. Check for that too.
858 WARN_ON_ONCE(ptr & 1);
860 ef->dentry->d_fsdata = (void *)(ptr | 1);
861 dentry_list = ef->dentry;
864 call_srcu(&eventfs_srcu, &ef->rcu, free_ef);
866 mutex_unlock(&eventfs_mutex);
868 while (dentry_list) {
871 dentry = dentry_list;
872 ptr = (unsigned long)dentry->d_fsdata & ~1UL;
873 dentry_list = (struct dentry *)ptr;
874 dentry->d_fsdata = NULL;
875 d_invalidate(dentry);
876 mutex_lock(&eventfs_mutex);
877 /* dentry should now have at least a single reference */
878 WARN_ONCE((int)d_count(dentry) < 1,
879 "dentry %p less than one reference (%d) after invalidate\n",
880 dentry, d_count(dentry));
881 mutex_unlock(&eventfs_mutex);
887 * eventfs_remove_events_dir - remove eventfs dir or file from list
888 * @dentry: events's dentry to be removed.
890 * This function remove events main directory
892 void eventfs_remove_events_dir(struct dentry *dentry)
894 struct tracefs_inode *ti;
896 if (!dentry || !dentry->d_inode)
899 ti = get_tracefs(dentry->d_inode);
900 if (!ti || !(ti->flags & TRACEFS_EVENT_INODE))
903 d_invalidate(dentry);