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 eventfs_release(struct inode *inode, struct file *file);
75 static const struct inode_operations eventfs_root_dir_inode_operations = {
76 .lookup = eventfs_root_lookup,
79 static const struct file_operations eventfs_file_operations = {
80 .open = dcache_dir_open_wrapper,
81 .read = generic_read_dir,
82 .iterate_shared = dcache_readdir,
83 .llseek = generic_file_llseek,
84 .release = eventfs_release,
88 * create_file - create a file in the tracefs filesystem
89 * @name: the name of the file to create.
90 * @mode: the permission that the file should have.
91 * @parent: parent dentry for this file.
92 * @data: something that the caller will want to get to later on.
93 * @fop: struct file_operations that should be used for this file.
95 * This is the basic "create a file" function for tracefs. It allows for a
96 * wide range of flexibility in creating a file.
98 * This function will return a pointer to a dentry if it succeeds. This
99 * pointer must be passed to the tracefs_remove() function when the file is
100 * to be removed (no automatic cleanup happens if your module is unloaded,
101 * you are responsible here.) If an error occurs, %NULL will be returned.
103 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
106 static struct dentry *create_file(const char *name, umode_t mode,
107 struct dentry *parent, void *data,
108 const struct file_operations *fop)
110 struct tracefs_inode *ti;
111 struct dentry *dentry;
114 if (!(mode & S_IFMT))
117 if (WARN_ON_ONCE(!S_ISREG(mode)))
120 dentry = eventfs_start_creating(name, parent);
125 inode = tracefs_get_inode(dentry->d_sb);
126 if (unlikely(!inode))
127 return eventfs_failed_creating(dentry);
129 inode->i_mode = mode;
131 inode->i_private = data;
133 ti = get_tracefs(inode);
134 ti->flags |= TRACEFS_EVENT_INODE;
135 d_instantiate(dentry, inode);
136 fsnotify_create(dentry->d_parent->d_inode, dentry);
137 return eventfs_end_creating(dentry);
141 * create_dir - create a dir in the tracefs filesystem
142 * @name: the name of the file to create.
143 * @parent: parent dentry for this file.
144 * @data: something that the caller will want to get to later on.
146 * This is the basic "create a dir" function for eventfs. It allows for a
147 * wide range of flexibility in creating a dir.
149 * This function will return a pointer to a dentry if it succeeds. This
150 * pointer must be passed to the tracefs_remove() function when the file is
151 * to be removed (no automatic cleanup happens if your module is unloaded,
152 * you are responsible here.) If an error occurs, %NULL will be returned.
154 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
157 static struct dentry *create_dir(const char *name, struct dentry *parent, void *data)
159 struct tracefs_inode *ti;
160 struct dentry *dentry;
163 dentry = eventfs_start_creating(name, parent);
167 inode = tracefs_get_inode(dentry->d_sb);
168 if (unlikely(!inode))
169 return eventfs_failed_creating(dentry);
171 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
172 inode->i_op = &eventfs_root_dir_inode_operations;
173 inode->i_fop = &eventfs_file_operations;
174 inode->i_private = data;
176 ti = get_tracefs(inode);
177 ti->flags |= TRACEFS_EVENT_INODE;
180 d_instantiate(dentry, inode);
181 inc_nlink(dentry->d_parent->d_inode);
182 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
183 return eventfs_end_creating(dentry);
187 * eventfs_set_ef_status_free - set the ef->status to free
188 * @ti: the tracefs_inode of the dentry
189 * @dentry: dentry who's status to be freed
191 * eventfs_set_ef_status_free will be called if no more
194 void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry)
196 struct tracefs_inode *ti_parent;
197 struct eventfs_inode *ei;
198 struct eventfs_file *ef, *tmp;
200 /* The top level events directory may be freed by this */
201 if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) {
202 LIST_HEAD(ef_del_list);
204 mutex_lock(&eventfs_mutex);
208 /* Record all the top level files */
209 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
210 lockdep_is_held(&eventfs_mutex)) {
211 list_add_tail(&ef->del_list, &ef_del_list);
214 /* Nothing should access this, but just in case! */
217 mutex_unlock(&eventfs_mutex);
219 /* Now safely free the top level files and their children */
220 list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
221 list_del(&ef->del_list);
229 mutex_lock(&eventfs_mutex);
231 ti_parent = get_tracefs(dentry->d_parent->d_inode);
232 if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
235 ef = dentry->d_fsdata;
240 * If ef was freed, then the LSB bit is set for d_fsdata.
241 * But this should not happen, as it should still have a
242 * ref count that prevents it. Warn in case it does.
244 if (WARN_ON_ONCE((unsigned long)ef & 1))
247 dentry->d_fsdata = NULL;
250 mutex_unlock(&eventfs_mutex);
254 * eventfs_post_create_dir - post create dir routine
255 * @ef: eventfs_file of recently created dir
257 * Map the meta-data of files within an eventfs dir to their parent dentry
259 static void eventfs_post_create_dir(struct eventfs_file *ef)
261 struct eventfs_file *ef_child;
262 struct tracefs_inode *ti;
264 /* srcu lock already held */
265 /* fill parent-child relation */
266 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
267 srcu_read_lock_held(&eventfs_srcu)) {
268 ef_child->d_parent = ef->dentry;
271 ti = get_tracefs(ef->dentry->d_inode);
272 ti->private = ef->ei;
276 * create_dentry - helper function to create dentry
277 * @ef: eventfs_file of file or directory to create
278 * @parent: parent dentry
279 * @lookup: true if called from lookup routine
281 * Used to create a dentry for file/dir, executes post dentry creation routine
283 static struct dentry *
284 create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup)
286 bool invalidate = false;
287 struct dentry *dentry;
289 mutex_lock(&eventfs_mutex);
291 mutex_unlock(&eventfs_mutex);
296 /* On dir open, up the ref count */
299 mutex_unlock(&eventfs_mutex);
302 mutex_unlock(&eventfs_mutex);
305 inode_lock(parent->d_inode);
308 dentry = create_dir(ef->name, parent, ef->data);
310 dentry = create_file(ef->name, ef->mode, parent,
314 inode_unlock(parent->d_inode);
316 mutex_lock(&eventfs_mutex);
317 if (IS_ERR_OR_NULL(dentry)) {
318 /* If the ef was already updated get it */
320 if (dentry && !lookup)
322 mutex_unlock(&eventfs_mutex);
326 if (!ef->dentry && !ef->is_freed) {
329 eventfs_post_create_dir(ef);
330 dentry->d_fsdata = ef;
332 /* A race here, should try again (unless freed) */
336 * Should never happen unless we get here due to being freed.
337 * Otherwise it means two dentries exist with the same name.
339 WARN_ON_ONCE(!ef->is_freed);
341 mutex_unlock(&eventfs_mutex);
343 d_invalidate(dentry);
345 if (lookup || invalidate)
348 return invalidate ? NULL : dentry;
351 static bool match_event_file(struct eventfs_file *ef, const char *name)
355 mutex_lock(&eventfs_mutex);
356 ret = !ef->is_freed && strcmp(ef->name, name) == 0;
357 mutex_unlock(&eventfs_mutex);
363 * eventfs_root_lookup - lookup routine to create file/dir
364 * @dir: in which a lookup is being done
365 * @dentry: file/dir dentry
366 * @flags: to pass as flags parameter to simple lookup
368 * Used to create a dynamic file/dir within @dir. Use the eventfs_inode
369 * list of meta data to find the information needed to create the file/dir.
371 static struct dentry *eventfs_root_lookup(struct inode *dir,
372 struct dentry *dentry,
375 struct tracefs_inode *ti;
376 struct eventfs_inode *ei;
377 struct eventfs_file *ef;
378 struct dentry *ret = NULL;
381 ti = get_tracefs(dir);
382 if (!(ti->flags & TRACEFS_EVENT_INODE))
386 idx = srcu_read_lock(&eventfs_srcu);
387 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
388 srcu_read_lock_held(&eventfs_srcu)) {
389 if (!match_event_file(ef, dentry->d_name.name))
391 ret = simple_lookup(dir, dentry, flags);
392 create_dentry(ef, ef->d_parent, true);
395 srcu_read_unlock(&eventfs_srcu, idx);
400 * eventfs_release - called to release eventfs file/dir
401 * @inode: inode to be released
402 * @file: file to be released (not used)
404 static int eventfs_release(struct inode *inode, struct file *file)
406 struct tracefs_inode *ti;
407 struct eventfs_inode *ei;
408 struct eventfs_file *ef;
409 struct dentry *dentry;
412 ti = get_tracefs(inode);
413 if (!(ti->flags & TRACEFS_EVENT_INODE))
417 idx = srcu_read_lock(&eventfs_srcu);
418 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
419 srcu_read_lock_held(&eventfs_srcu)) {
420 mutex_lock(&eventfs_mutex);
422 mutex_unlock(&eventfs_mutex);
426 srcu_read_unlock(&eventfs_srcu, idx);
427 return dcache_dir_close(inode, file);
431 * dcache_dir_open_wrapper - eventfs open wrapper
433 * @file: dir to be opened (to create its child)
435 * Used to dynamically create the file/dir within @file. @file is really a
436 * directory and all the files/dirs of the children within @file will be
437 * created. If any of the files/dirs have already been created, their
438 * reference count will be incremented.
440 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
442 struct tracefs_inode *ti;
443 struct eventfs_inode *ei;
444 struct eventfs_file *ef;
445 struct dentry *dentry = file_dentry(file);
446 struct inode *f_inode = file_inode(file);
449 ti = get_tracefs(f_inode);
450 if (!(ti->flags & TRACEFS_EVENT_INODE))
454 idx = srcu_read_lock(&eventfs_srcu);
455 list_for_each_entry_rcu(ef, &ei->e_top_files, list) {
456 create_dentry(ef, dentry, false);
458 srcu_read_unlock(&eventfs_srcu, idx);
459 return dcache_dir_open(inode, file);
463 * eventfs_prepare_ef - helper function to prepare eventfs_file
464 * @name: the name of the file/directory to create.
465 * @mode: the permission that the file should have.
466 * @fop: struct file_operations that should be used for this file/directory.
467 * @iop: struct inode_operations that should be used for this file/directory.
468 * @data: something that the caller will want to get to later on. The
469 * inode.i_private pointer will point to this value on the open() call.
471 * This function allocates and fills the eventfs_file structure.
473 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode,
474 const struct file_operations *fop,
475 const struct inode_operations *iop,
478 struct eventfs_file *ef;
480 ef = kzalloc(sizeof(*ef), GFP_KERNEL);
482 return ERR_PTR(-ENOMEM);
484 ef->name = kstrdup(name, GFP_KERNEL);
487 return ERR_PTR(-ENOMEM);
491 ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL);
495 return ERR_PTR(-ENOMEM);
497 INIT_LIST_HEAD(&ef->ei->e_top_files);
510 * eventfs_create_events_dir - create the trace event structure
511 * @name: the name of the directory to create.
512 * @parent: parent dentry for this file. This should be a directory dentry
513 * if set. If this parameter is NULL, then the directory will be
514 * created in the root of the tracefs filesystem.
516 * This function creates the top of the trace event directory.
518 struct dentry *eventfs_create_events_dir(const char *name,
519 struct dentry *parent)
521 struct dentry *dentry = tracefs_start_creating(name, parent);
522 struct eventfs_inode *ei;
523 struct tracefs_inode *ti;
526 if (security_locked_down(LOCKDOWN_TRACEFS))
532 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
534 return ERR_PTR(-ENOMEM);
535 inode = tracefs_get_inode(dentry->d_sb);
536 if (unlikely(!inode)) {
538 tracefs_failed_creating(dentry);
539 return ERR_PTR(-ENOMEM);
542 INIT_LIST_HEAD(&ei->e_top_files);
544 ti = get_tracefs(inode);
545 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
548 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
549 inode->i_op = &eventfs_root_dir_inode_operations;
550 inode->i_fop = &eventfs_file_operations;
552 /* directory inodes start off with i_nlink == 2 (for "." entry) */
554 d_instantiate(dentry, inode);
555 inc_nlink(dentry->d_parent->d_inode);
556 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
557 return tracefs_end_creating(dentry);
561 * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later
562 * @name: the name of the file to create.
563 * @parent: parent dentry for this dir.
565 * This function adds eventfs subsystem dir to list.
566 * And all these dirs are created on the fly when they are looked up,
567 * and the dentry and inodes will be removed when they are done.
569 struct eventfs_file *eventfs_add_subsystem_dir(const char *name,
570 struct dentry *parent)
572 struct tracefs_inode *ti_parent;
573 struct eventfs_inode *ei_parent;
574 struct eventfs_file *ef;
576 if (security_locked_down(LOCKDOWN_TRACEFS))
580 return ERR_PTR(-EINVAL);
582 ti_parent = get_tracefs(parent->d_inode);
583 ei_parent = ti_parent->private;
585 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
589 mutex_lock(&eventfs_mutex);
590 list_add_tail(&ef->list, &ei_parent->e_top_files);
591 ef->d_parent = parent;
592 mutex_unlock(&eventfs_mutex);
597 * eventfs_add_dir - add eventfs dir to list to create later
598 * @name: the name of the file to create.
599 * @ef_parent: parent eventfs_file for this dir.
601 * This function adds eventfs dir to list.
602 * And all these dirs are created on the fly when they are looked up,
603 * and the dentry and inodes will be removed when they are done.
605 struct eventfs_file *eventfs_add_dir(const char *name,
606 struct eventfs_file *ef_parent)
608 struct eventfs_file *ef;
610 if (security_locked_down(LOCKDOWN_TRACEFS))
614 return ERR_PTR(-EINVAL);
616 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
620 mutex_lock(&eventfs_mutex);
621 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
622 ef->d_parent = ef_parent->dentry;
623 mutex_unlock(&eventfs_mutex);
628 * eventfs_add_events_file - add the data needed to create a file for later reference
629 * @name: the name of the file to create.
630 * @mode: the permission that the file should have.
631 * @parent: parent dentry for this file.
632 * @data: something that the caller will want to get to later on.
633 * @fop: struct file_operations that should be used for this file.
635 * This function is used to add the information needed to create a
636 * dentry/inode within the top level events directory. The file created
637 * will have the @mode permissions. The @data will be used to fill the
638 * inode.i_private when the open() call is done. The dentry and inodes are
639 * all created when they are referenced, and removed when they are no
642 int eventfs_add_events_file(const char *name, umode_t mode,
643 struct dentry *parent, void *data,
644 const struct file_operations *fop)
646 struct tracefs_inode *ti;
647 struct eventfs_inode *ei;
648 struct eventfs_file *ef;
650 if (security_locked_down(LOCKDOWN_TRACEFS))
656 if (!(mode & S_IFMT))
659 if (!parent->d_inode)
662 ti = get_tracefs(parent->d_inode);
663 if (!(ti->flags & TRACEFS_EVENT_INODE))
667 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
672 mutex_lock(&eventfs_mutex);
673 list_add_tail(&ef->list, &ei->e_top_files);
674 ef->d_parent = parent;
675 mutex_unlock(&eventfs_mutex);
680 * eventfs_add_file - add eventfs file to list to create later
681 * @name: the name of the file to create.
682 * @mode: the permission that the file should have.
683 * @ef_parent: parent eventfs_file for this file.
684 * @data: something that the caller will want to get to later on.
685 * @fop: struct file_operations that should be used for this file.
687 * This function is used to add the information needed to create a
688 * file within a subdirectory of the events directory. The file created
689 * will have the @mode permissions. The @data will be used to fill the
690 * inode.i_private when the open() call is done. The dentry and inodes are
691 * all created when they are referenced, and removed when they are no
694 int eventfs_add_file(const char *name, umode_t mode,
695 struct eventfs_file *ef_parent,
697 const struct file_operations *fop)
699 struct eventfs_file *ef;
701 if (security_locked_down(LOCKDOWN_TRACEFS))
707 if (!(mode & S_IFMT))
710 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
714 mutex_lock(&eventfs_mutex);
715 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
716 ef->d_parent = ef_parent->dentry;
717 mutex_unlock(&eventfs_mutex);
721 static void free_ef(struct rcu_head *head)
723 struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu);
731 * eventfs_remove_rec - remove eventfs dir or file from list
732 * @ef: eventfs_file to be removed.
733 * @head: to create list of eventfs_file to be deleted
734 * @level: to check recursion depth
736 * The helper function eventfs_remove_rec() is used to clean up and free the
737 * associated data from eventfs for both of the added functions.
739 static void eventfs_remove_rec(struct eventfs_file *ef, struct list_head *head, int level)
741 struct eventfs_file *ef_child;
746 * Check recursion depth. It should never be greater than 3:
749 * 2 - events/group/event/
750 * 3 - events/group/event/file
752 if (WARN_ON_ONCE(level > 3))
756 /* search for nested folders or files */
757 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
758 lockdep_is_held(&eventfs_mutex)) {
759 eventfs_remove_rec(ef_child, head, level + 1);
763 list_del_rcu(&ef->list);
764 list_add_tail(&ef->del_list, head);
768 * eventfs_remove - remove eventfs dir or file from list
769 * @ef: eventfs_file to be removed.
771 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
773 void eventfs_remove(struct eventfs_file *ef)
775 struct eventfs_file *tmp;
776 LIST_HEAD(ef_del_list);
777 struct dentry *dentry_list = NULL;
778 struct dentry *dentry;
783 mutex_lock(&eventfs_mutex);
784 eventfs_remove_rec(ef, &ef_del_list, 0);
785 list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
787 unsigned long ptr = (unsigned long)dentry_list;
789 /* Keep the dentry from being freed yet */
793 * Paranoid: The dget() above should prevent the dentry
794 * from being freed and calling eventfs_set_ef_status_free().
795 * But just in case, set the link list LSB pointer to 1
796 * and have eventfs_set_ef_status_free() check that to
797 * make sure that if it does happen, it will not think
798 * the d_fsdata is an event_file.
800 * For this to work, no event_file should be allocated
801 * on a odd space, as the ef should always be allocated
802 * to be at least word aligned. Check for that too.
804 WARN_ON_ONCE(ptr & 1);
806 ef->dentry->d_fsdata = (void *)(ptr | 1);
807 dentry_list = ef->dentry;
810 call_srcu(&eventfs_srcu, &ef->rcu, free_ef);
812 mutex_unlock(&eventfs_mutex);
814 while (dentry_list) {
817 dentry = dentry_list;
818 ptr = (unsigned long)dentry->d_fsdata & ~1UL;
819 dentry_list = (struct dentry *)ptr;
820 dentry->d_fsdata = NULL;
821 d_invalidate(dentry);
822 mutex_lock(&eventfs_mutex);
823 /* dentry should now have at least a single reference */
824 WARN_ONCE((int)d_count(dentry) < 1,
825 "dentry %p less than one reference (%d) after invalidate\n",
826 dentry, d_count(dentry));
827 mutex_unlock(&eventfs_mutex);
833 * eventfs_remove_events_dir - remove eventfs dir or file from list
834 * @dentry: events's dentry to be removed.
836 * This function remove events main directory
838 void eventfs_remove_events_dir(struct dentry *dentry)
840 struct tracefs_inode *ti;
842 if (!dentry || !dentry->d_inode)
845 ti = get_tracefs(dentry->d_inode);
846 if (!ti || !(ti->flags & TRACEFS_EVENT_INODE))
849 d_invalidate(dentry);