1 // SPDX-License-Identifier: GPL-2.0-only
3 * event_inode.c - part of tracefs, a pseudo file system for activating tracing
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 * @dentry: dentry who's status to be freed
190 * eventfs_set_ef_status_free will be called if no more
193 void eventfs_set_ef_status_free(struct dentry *dentry)
195 struct tracefs_inode *ti_parent;
196 struct eventfs_file *ef;
198 mutex_lock(&eventfs_mutex);
199 ti_parent = get_tracefs(dentry->d_parent->d_inode);
200 if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
203 ef = dentry->d_fsdata;
208 * If ef was freed, then the LSB bit is set for d_fsdata.
209 * But this should not happen, as it should still have a
210 * ref count that prevents it. Warn in case it does.
212 if (WARN_ON_ONCE((unsigned long)ef & 1))
215 dentry->d_fsdata = NULL;
218 mutex_unlock(&eventfs_mutex);
222 * eventfs_post_create_dir - post create dir routine
223 * @ef: eventfs_file of recently created dir
225 * Map the meta-data of files within an eventfs dir to their parent dentry
227 static void eventfs_post_create_dir(struct eventfs_file *ef)
229 struct eventfs_file *ef_child;
230 struct tracefs_inode *ti;
232 /* srcu lock already held */
233 /* fill parent-child relation */
234 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
235 srcu_read_lock_held(&eventfs_srcu)) {
236 ef_child->d_parent = ef->dentry;
239 ti = get_tracefs(ef->dentry->d_inode);
240 ti->private = ef->ei;
244 * create_dentry - helper function to create dentry
245 * @ef: eventfs_file of file or directory to create
246 * @parent: parent dentry
247 * @lookup: true if called from lookup routine
249 * Used to create a dentry for file/dir, executes post dentry creation routine
251 static struct dentry *
252 create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup)
254 bool invalidate = false;
255 struct dentry *dentry;
257 mutex_lock(&eventfs_mutex);
259 mutex_unlock(&eventfs_mutex);
264 /* On dir open, up the ref count */
267 mutex_unlock(&eventfs_mutex);
270 mutex_unlock(&eventfs_mutex);
273 inode_lock(parent->d_inode);
276 dentry = create_dir(ef->name, parent, ef->data);
278 dentry = create_file(ef->name, ef->mode, parent,
282 inode_unlock(parent->d_inode);
284 mutex_lock(&eventfs_mutex);
285 if (IS_ERR_OR_NULL(dentry)) {
286 /* If the ef was already updated get it */
288 if (dentry && !lookup)
290 mutex_unlock(&eventfs_mutex);
294 if (!ef->dentry && !ef->is_freed) {
297 eventfs_post_create_dir(ef);
298 dentry->d_fsdata = ef;
300 /* A race here, should try again (unless freed) */
304 * Should never happen unless we get here due to being freed.
305 * Otherwise it means two dentries exist with the same name.
307 WARN_ON_ONCE(!ef->is_freed);
309 mutex_unlock(&eventfs_mutex);
311 d_invalidate(dentry);
313 if (lookup || invalidate)
316 return invalidate ? NULL : dentry;
319 static bool match_event_file(struct eventfs_file *ef, const char *name)
323 mutex_lock(&eventfs_mutex);
324 ret = !ef->is_freed && strcmp(ef->name, name) == 0;
325 mutex_unlock(&eventfs_mutex);
331 * eventfs_root_lookup - lookup routine to create file/dir
332 * @dir: in which a lookup is being done
333 * @dentry: file/dir dentry
334 * @flags: to pass as flags parameter to simple lookup
336 * Used to create a dynamic file/dir within @dir. Use the eventfs_inode
337 * list of meta data to find the information needed to create the file/dir.
339 static struct dentry *eventfs_root_lookup(struct inode *dir,
340 struct dentry *dentry,
343 struct tracefs_inode *ti;
344 struct eventfs_inode *ei;
345 struct eventfs_file *ef;
346 struct dentry *ret = NULL;
349 ti = get_tracefs(dir);
350 if (!(ti->flags & TRACEFS_EVENT_INODE))
354 idx = srcu_read_lock(&eventfs_srcu);
355 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
356 srcu_read_lock_held(&eventfs_srcu)) {
357 if (!match_event_file(ef, dentry->d_name.name))
359 ret = simple_lookup(dir, dentry, flags);
360 create_dentry(ef, ef->d_parent, true);
363 srcu_read_unlock(&eventfs_srcu, idx);
368 * eventfs_release - called to release eventfs file/dir
369 * @inode: inode to be released
370 * @file: file to be released (not used)
372 static int eventfs_release(struct inode *inode, struct file *file)
374 struct tracefs_inode *ti;
375 struct eventfs_inode *ei;
376 struct eventfs_file *ef;
377 struct dentry *dentry;
380 ti = get_tracefs(inode);
381 if (!(ti->flags & TRACEFS_EVENT_INODE))
385 idx = srcu_read_lock(&eventfs_srcu);
386 list_for_each_entry_srcu(ef, &ei->e_top_files, list,
387 srcu_read_lock_held(&eventfs_srcu)) {
388 mutex_lock(&eventfs_mutex);
390 mutex_unlock(&eventfs_mutex);
394 srcu_read_unlock(&eventfs_srcu, idx);
395 return dcache_dir_close(inode, file);
399 * dcache_dir_open_wrapper - eventfs open wrapper
401 * @file: dir to be opened (to create its child)
403 * Used to dynamically create the file/dir within @file. @file is really a
404 * directory and all the files/dirs of the children within @file will be
405 * created. If any of the files/dirs have already been created, their
406 * reference count will be incremented.
408 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file)
410 struct tracefs_inode *ti;
411 struct eventfs_inode *ei;
412 struct eventfs_file *ef;
413 struct dentry *dentry = file_dentry(file);
414 struct inode *f_inode = file_inode(file);
417 ti = get_tracefs(f_inode);
418 if (!(ti->flags & TRACEFS_EVENT_INODE))
422 idx = srcu_read_lock(&eventfs_srcu);
423 list_for_each_entry_rcu(ef, &ei->e_top_files, list) {
424 create_dentry(ef, dentry, false);
426 srcu_read_unlock(&eventfs_srcu, idx);
427 return dcache_dir_open(inode, file);
431 * eventfs_prepare_ef - helper function to prepare eventfs_file
432 * @name: the name of the file/directory to create.
433 * @mode: the permission that the file should have.
434 * @fop: struct file_operations that should be used for this file/directory.
435 * @iop: struct inode_operations that should be used for this file/directory.
436 * @data: something that the caller will want to get to later on. The
437 * inode.i_private pointer will point to this value on the open() call.
439 * This function allocates and fills the eventfs_file structure.
441 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode,
442 const struct file_operations *fop,
443 const struct inode_operations *iop,
446 struct eventfs_file *ef;
448 ef = kzalloc(sizeof(*ef), GFP_KERNEL);
450 return ERR_PTR(-ENOMEM);
452 ef->name = kstrdup(name, GFP_KERNEL);
455 return ERR_PTR(-ENOMEM);
459 ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL);
463 return ERR_PTR(-ENOMEM);
465 INIT_LIST_HEAD(&ef->ei->e_top_files);
478 * eventfs_create_events_dir - create the trace event structure
479 * @name: the name of the directory to create.
480 * @parent: parent dentry for this file. This should be a directory dentry
481 * if set. If this parameter is NULL, then the directory will be
482 * created in the root of the tracefs filesystem.
484 * This function creates the top of the trace event directory.
486 struct dentry *eventfs_create_events_dir(const char *name,
487 struct dentry *parent)
489 struct dentry *dentry = tracefs_start_creating(name, parent);
490 struct eventfs_inode *ei;
491 struct tracefs_inode *ti;
497 ei = kzalloc(sizeof(*ei), GFP_KERNEL);
499 return ERR_PTR(-ENOMEM);
500 inode = tracefs_get_inode(dentry->d_sb);
501 if (unlikely(!inode)) {
503 tracefs_failed_creating(dentry);
504 return ERR_PTR(-ENOMEM);
507 INIT_LIST_HEAD(&ei->e_top_files);
509 ti = get_tracefs(inode);
510 ti->flags |= TRACEFS_EVENT_INODE;
513 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
514 inode->i_op = &eventfs_root_dir_inode_operations;
515 inode->i_fop = &eventfs_file_operations;
517 /* directory inodes start off with i_nlink == 2 (for "." entry) */
519 d_instantiate(dentry, inode);
520 inc_nlink(dentry->d_parent->d_inode);
521 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
522 return tracefs_end_creating(dentry);
526 * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later
527 * @name: the name of the file to create.
528 * @parent: parent dentry for this dir.
530 * This function adds eventfs subsystem dir to list.
531 * And all these dirs are created on the fly when they are looked up,
532 * and the dentry and inodes will be removed when they are done.
534 struct eventfs_file *eventfs_add_subsystem_dir(const char *name,
535 struct dentry *parent)
537 struct tracefs_inode *ti_parent;
538 struct eventfs_inode *ei_parent;
539 struct eventfs_file *ef;
542 return ERR_PTR(-EINVAL);
544 ti_parent = get_tracefs(parent->d_inode);
545 ei_parent = ti_parent->private;
547 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
551 mutex_lock(&eventfs_mutex);
552 list_add_tail(&ef->list, &ei_parent->e_top_files);
553 ef->d_parent = parent;
554 mutex_unlock(&eventfs_mutex);
559 * eventfs_add_dir - add eventfs dir to list to create later
560 * @name: the name of the file to create.
561 * @ef_parent: parent eventfs_file for this dir.
563 * This function adds eventfs dir to list.
564 * And all these dirs are created on the fly when they are looked up,
565 * and the dentry and inodes will be removed when they are done.
567 struct eventfs_file *eventfs_add_dir(const char *name,
568 struct eventfs_file *ef_parent)
570 struct eventfs_file *ef;
573 return ERR_PTR(-EINVAL);
575 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL);
579 mutex_lock(&eventfs_mutex);
580 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
581 ef->d_parent = ef_parent->dentry;
582 mutex_unlock(&eventfs_mutex);
587 * eventfs_add_events_file - add the data needed to create a file for later reference
588 * @name: the name of the file to create.
589 * @mode: the permission that the file should have.
590 * @parent: parent dentry for this file.
591 * @data: something that the caller will want to get to later on.
592 * @fop: struct file_operations that should be used for this file.
594 * This function is used to add the information needed to create a
595 * dentry/inode within the top level events directory. The file created
596 * will have the @mode permissions. The @data will be used to fill the
597 * inode.i_private when the open() call is done. The dentry and inodes are
598 * all created when they are referenced, and removed when they are no
601 int eventfs_add_events_file(const char *name, umode_t mode,
602 struct dentry *parent, void *data,
603 const struct file_operations *fop)
605 struct tracefs_inode *ti;
606 struct eventfs_inode *ei;
607 struct eventfs_file *ef;
612 if (!(mode & S_IFMT))
615 if (!parent->d_inode)
618 ti = get_tracefs(parent->d_inode);
619 if (!(ti->flags & TRACEFS_EVENT_INODE))
623 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
628 mutex_lock(&eventfs_mutex);
629 list_add_tail(&ef->list, &ei->e_top_files);
630 ef->d_parent = parent;
631 mutex_unlock(&eventfs_mutex);
636 * eventfs_add_file - add eventfs file to list to create later
637 * @name: the name of the file to create.
638 * @mode: the permission that the file should have.
639 * @ef_parent: parent eventfs_file for this file.
640 * @data: something that the caller will want to get to later on.
641 * @fop: struct file_operations that should be used for this file.
643 * This function is used to add the information needed to create a
644 * file within a subdirectory of the events directory. The file created
645 * will have the @mode permissions. The @data will be used to fill the
646 * inode.i_private when the open() call is done. The dentry and inodes are
647 * all created when they are referenced, and removed when they are no
650 int eventfs_add_file(const char *name, umode_t mode,
651 struct eventfs_file *ef_parent,
653 const struct file_operations *fop)
655 struct eventfs_file *ef;
660 if (!(mode & S_IFMT))
663 ef = eventfs_prepare_ef(name, mode, fop, NULL, data);
667 mutex_lock(&eventfs_mutex);
668 list_add_tail(&ef->list, &ef_parent->ei->e_top_files);
669 ef->d_parent = ef_parent->dentry;
670 mutex_unlock(&eventfs_mutex);
674 static void free_ef(struct rcu_head *head)
676 struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu);
684 * eventfs_remove_rec - remove eventfs dir or file from list
685 * @ef: eventfs_file to be removed.
686 * @head: to create list of eventfs_file to be deleted
687 * @level: to check recursion depth
689 * The helper function eventfs_remove_rec() is used to clean up and free the
690 * associated data from eventfs for both of the added functions.
692 static void eventfs_remove_rec(struct eventfs_file *ef, struct list_head *head, int level)
694 struct eventfs_file *ef_child;
699 * Check recursion depth. It should never be greater than 3:
702 * 2 - events/group/event/
703 * 3 - events/group/event/file
705 if (WARN_ON_ONCE(level > 3))
709 /* search for nested folders or files */
710 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list,
711 lockdep_is_held(&eventfs_mutex)) {
712 eventfs_remove_rec(ef_child, head, level + 1);
716 list_del_rcu(&ef->list);
717 list_add_tail(&ef->del_list, head);
721 * eventfs_remove - remove eventfs dir or file from list
722 * @ef: eventfs_file to be removed.
724 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
726 void eventfs_remove(struct eventfs_file *ef)
728 struct eventfs_file *tmp;
729 LIST_HEAD(ef_del_list);
730 struct dentry *dentry_list = NULL;
731 struct dentry *dentry;
736 mutex_lock(&eventfs_mutex);
737 eventfs_remove_rec(ef, &ef_del_list, 0);
738 list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
740 unsigned long ptr = (unsigned long)dentry_list;
742 /* Keep the dentry from being freed yet */
746 * Paranoid: The dget() above should prevent the dentry
747 * from being freed and calling eventfs_set_ef_status_free().
748 * But just in case, set the link list LSB pointer to 1
749 * and have eventfs_set_ef_status_free() check that to
750 * make sure that if it does happen, it will not think
751 * the d_fsdata is an event_file.
753 * For this to work, no event_file should be allocated
754 * on a odd space, as the ef should always be allocated
755 * to be at least word aligned. Check for that too.
757 WARN_ON_ONCE(ptr & 1);
759 ef->dentry->d_fsdata = (void *)(ptr | 1);
760 dentry_list = ef->dentry;
763 call_srcu(&eventfs_srcu, &ef->rcu, free_ef);
765 mutex_unlock(&eventfs_mutex);
767 while (dentry_list) {
770 dentry = dentry_list;
771 ptr = (unsigned long)dentry->d_fsdata & ~1UL;
772 dentry_list = (struct dentry *)ptr;
773 dentry->d_fsdata = NULL;
774 d_invalidate(dentry);
775 mutex_lock(&eventfs_mutex);
776 /* dentry should now have at least a single reference */
777 WARN_ONCE((int)d_count(dentry) < 1,
778 "dentry %p less than one reference (%d) after invalidate\n",
779 dentry, d_count(dentry));
780 mutex_unlock(&eventfs_mutex);
786 * eventfs_remove_events_dir - remove eventfs dir or file from list
787 * @dentry: events's dentry to be removed.
789 * This function remove events main directory
791 void eventfs_remove_events_dir(struct dentry *dentry)
793 struct tracefs_inode *ti;
794 struct eventfs_inode *ei;
796 if (!dentry || !dentry->d_inode)
799 ti = get_tracefs(dentry->d_inode);
800 if (!ti || !(ti->flags & TRACEFS_EVENT_INODE))
804 d_invalidate(dentry);