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 struct eventfs_root_inode {
39 struct eventfs_inode ei;
40 struct dentry *events_dir;
43 static struct eventfs_root_inode *get_root_inode(struct eventfs_inode *ei)
45 WARN_ON_ONCE(!ei->is_events);
46 return container_of(ei, struct eventfs_root_inode, ei);
49 /* Just try to make something consistent and unique */
50 static int eventfs_dir_ino(struct eventfs_inode *ei)
53 ei->ino = get_next_ino();
59 * The eventfs_inode (ei) itself is protected by SRCU. It is released from
60 * its parent's list and will have is_freed set (under eventfs_mutex).
61 * After the SRCU grace period is over and the last dput() is called
64 DEFINE_STATIC_SRCU(eventfs_srcu);
66 /* Mode is unsigned short, use the upper bits for flags */
68 EVENTFS_SAVE_MODE = BIT(16),
69 EVENTFS_SAVE_UID = BIT(17),
70 EVENTFS_SAVE_GID = BIT(18),
71 EVENTFS_TOPLEVEL = BIT(19),
74 #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
77 * eventfs_inode reference count management.
79 * NOTE! We count only references from dentries, in the
80 * form 'dentry->d_fsdata'. There are also references from
81 * directory inodes ('ti->private'), but the dentry reference
82 * count is always a superset of the inode reference count.
84 static void release_ei(struct kref *ref)
86 struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
87 struct eventfs_root_inode *rei;
89 WARN_ON_ONCE(!ei->is_freed);
91 kfree(ei->entry_attrs);
92 kfree_const(ei->name);
94 rei = get_root_inode(ei);
95 kfree_rcu(rei, ei.rcu);
101 static inline void put_ei(struct eventfs_inode *ei)
104 kref_put(&ei->kref, release_ei);
107 static inline void free_ei(struct eventfs_inode *ei)
115 static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
122 static struct dentry *eventfs_root_lookup(struct inode *dir,
123 struct dentry *dentry,
125 static int eventfs_iterate(struct file *file, struct dir_context *ctx);
127 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
129 unsigned int ia_valid = iattr->ia_valid;
131 if (ia_valid & ATTR_MODE) {
132 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
133 (iattr->ia_mode & EVENTFS_MODE_MASK) |
136 if (ia_valid & ATTR_UID) {
137 attr->mode |= EVENTFS_SAVE_UID;
138 attr->uid = iattr->ia_uid;
140 if (ia_valid & ATTR_GID) {
141 attr->mode |= EVENTFS_SAVE_GID;
142 attr->gid = iattr->ia_gid;
146 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
149 const struct eventfs_entry *entry;
150 struct eventfs_inode *ei;
154 mutex_lock(&eventfs_mutex);
155 ei = dentry->d_fsdata;
157 /* Do not allow changes if the event is about to be removed. */
158 mutex_unlock(&eventfs_mutex);
162 /* Preallocate the children mode array if necessary */
163 if (!(dentry->d_inode->i_mode & S_IFDIR)) {
164 if (!ei->entry_attrs) {
165 ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
167 if (!ei->entry_attrs) {
174 ret = simple_setattr(idmap, dentry, iattr);
179 * If this is a dir, then update the ei cache, only the file
180 * mode is saved in the ei->m_children, and the ownership is
181 * determined by the parent directory.
183 if (dentry->d_inode->i_mode & S_IFDIR) {
185 * The events directory dentry is never freed, unless its
186 * part of an instance that is deleted. It's attr is the
187 * default for its child files and directories.
188 * Do not update it. It's not used for its own mode or ownership.
191 /* But it still needs to know if it was modified */
192 if (iattr->ia_valid & ATTR_UID)
193 ei->attr.mode |= EVENTFS_SAVE_UID;
194 if (iattr->ia_valid & ATTR_GID)
195 ei->attr.mode |= EVENTFS_SAVE_GID;
197 update_attr(&ei->attr, iattr);
201 name = dentry->d_name.name;
203 for (int i = 0; i < ei->nr_entries; i++) {
204 entry = &ei->entries[i];
205 if (strcmp(name, entry->name) == 0) {
206 update_attr(&ei->entry_attrs[i], iattr);
212 mutex_unlock(&eventfs_mutex);
216 static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
220 /* Only update if the "events" was on the top level */
221 if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
224 /* Get the tracefs root inode. */
225 root = d_inode(sb->s_root);
226 ei->attr.uid = root->i_uid;
227 ei->attr.gid = root->i_gid;
230 static void set_top_events_ownership(struct inode *inode)
232 struct tracefs_inode *ti = get_tracefs(inode);
233 struct eventfs_inode *ei = ti->private;
235 /* The top events directory doesn't get automatically updated */
236 if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
239 update_top_events_attr(ei, inode->i_sb);
241 if (!(ei->attr.mode & EVENTFS_SAVE_UID))
242 inode->i_uid = ei->attr.uid;
244 if (!(ei->attr.mode & EVENTFS_SAVE_GID))
245 inode->i_gid = ei->attr.gid;
248 static int eventfs_get_attr(struct mnt_idmap *idmap,
249 const struct path *path, struct kstat *stat,
250 u32 request_mask, unsigned int flags)
252 struct dentry *dentry = path->dentry;
253 struct inode *inode = d_backing_inode(dentry);
255 set_top_events_ownership(inode);
257 generic_fillattr(idmap, request_mask, inode, stat);
261 static int eventfs_permission(struct mnt_idmap *idmap,
262 struct inode *inode, int mask)
264 set_top_events_ownership(inode);
265 return generic_permission(idmap, inode, mask);
268 static const struct inode_operations eventfs_root_dir_inode_operations = {
269 .lookup = eventfs_root_lookup,
270 .setattr = eventfs_set_attr,
271 .getattr = eventfs_get_attr,
272 .permission = eventfs_permission,
275 static const struct inode_operations eventfs_file_inode_operations = {
276 .setattr = eventfs_set_attr,
279 static const struct file_operations eventfs_file_operations = {
280 .read = generic_read_dir,
281 .iterate_shared = eventfs_iterate,
282 .llseek = generic_file_llseek,
285 /* Return the evenfs_inode of the "events" directory */
286 static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
288 struct eventfs_inode *ei;
291 // The parent is stable because we do not do renames
292 dentry = dentry->d_parent;
293 // ... and directories always have d_fsdata
294 ei = dentry->d_fsdata;
297 * If the ei is being freed, the ownership of the children
304 // Walk upwards until you find the events inode
305 } while (!ei->is_events);
307 update_top_events_attr(ei, dentry->d_sb);
312 static void update_inode_attr(struct dentry *dentry, struct inode *inode,
313 struct eventfs_attr *attr, umode_t mode)
315 struct eventfs_inode *events_ei = eventfs_find_events(dentry);
320 inode->i_mode = mode;
321 inode->i_uid = events_ei->attr.uid;
322 inode->i_gid = events_ei->attr.gid;
327 if (attr->mode & EVENTFS_SAVE_MODE)
328 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
330 if (attr->mode & EVENTFS_SAVE_UID)
331 inode->i_uid = attr->uid;
333 if (attr->mode & EVENTFS_SAVE_GID)
334 inode->i_gid = attr->gid;
338 * lookup_file - look up a file in the tracefs filesystem
339 * @dentry: the dentry to look up
340 * @mode: the permission that the file should have.
341 * @attr: saved attributes changed by user
342 * @data: something that the caller will want to get to later on.
343 * @fop: struct file_operations that should be used for this file.
345 * This function creates a dentry that represents a file in the eventsfs_inode
346 * directory. The inode.i_private pointer will point to @data in the open()
349 static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
350 struct dentry *dentry,
352 struct eventfs_attr *attr,
354 const struct file_operations *fop)
356 struct tracefs_inode *ti;
359 if (!(mode & S_IFMT))
362 if (WARN_ON_ONCE(!S_ISREG(mode)))
363 return ERR_PTR(-EIO);
365 inode = tracefs_get_inode(dentry->d_sb);
366 if (unlikely(!inode))
367 return ERR_PTR(-ENOMEM);
369 /* If the user updated the directory's attributes, use them */
370 update_inode_attr(dentry, inode, attr, mode);
372 inode->i_op = &eventfs_file_inode_operations;
374 inode->i_private = data;
376 /* All files will have the same inode number */
377 inode->i_ino = EVENTFS_FILE_INODE_INO;
379 ti = get_tracefs(inode);
380 ti->flags |= TRACEFS_EVENT_INODE;
382 // Files have their parent's ei as their fsdata
383 dentry->d_fsdata = get_ei(parent_ei);
385 d_add(dentry, inode);
390 * lookup_dir_entry - look up a dir in the tracefs filesystem
391 * @dentry: the directory to look up
392 * @ei: the eventfs_inode that represents the directory to create
394 * This function will look up a dentry for a directory represented by
397 static struct dentry *lookup_dir_entry(struct dentry *dentry,
398 struct eventfs_inode *pei, struct eventfs_inode *ei)
400 struct tracefs_inode *ti;
403 inode = tracefs_get_inode(dentry->d_sb);
404 if (unlikely(!inode))
405 return ERR_PTR(-ENOMEM);
407 /* If the user updated the directory's attributes, use them */
408 update_inode_attr(dentry, inode, &ei->attr,
409 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
411 inode->i_op = &eventfs_root_dir_inode_operations;
412 inode->i_fop = &eventfs_file_operations;
414 /* All directories will have the same inode number */
415 inode->i_ino = eventfs_dir_ino(ei);
417 ti = get_tracefs(inode);
418 ti->flags |= TRACEFS_EVENT_INODE;
419 /* Only directories have ti->private set to an ei, not files */
422 dentry->d_fsdata = get_ei(ei);
424 d_add(dentry, inode);
428 static inline struct eventfs_inode *init_ei(struct eventfs_inode *ei, const char *name)
430 ei->name = kstrdup_const(name, GFP_KERNEL);
433 kref_init(&ei->kref);
437 static inline struct eventfs_inode *alloc_ei(const char *name)
439 struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
440 struct eventfs_inode *result;
445 result = init_ei(ei, name);
452 static inline struct eventfs_inode *alloc_root_ei(const char *name)
454 struct eventfs_root_inode *rei = kzalloc(sizeof(*rei), GFP_KERNEL);
455 struct eventfs_inode *ei;
460 rei->ei.is_events = 1;
461 ei = init_ei(&rei->ei, name);
469 * eventfs_d_release - dentry is going away
470 * @dentry: dentry which has the reference to remove.
472 * Remove the association between a dentry from an eventfs_inode.
474 void eventfs_d_release(struct dentry *dentry)
476 put_ei(dentry->d_fsdata);
480 * lookup_file_dentry - create a dentry for a file of an eventfs_inode
481 * @ei: the eventfs_inode that the file will be created under
482 * @idx: the index into the entry_attrs[] of the @ei
483 * @parent: The parent dentry of the created file.
484 * @name: The name of the file to create
485 * @mode: The mode of the file.
486 * @data: The data to use to set the inode of the file with on open()
487 * @fops: The fops of the file to be created.
489 * Create a dentry for a file of an eventfs_inode @ei and place it into the
490 * address located at @e_dentry.
492 static struct dentry *
493 lookup_file_dentry(struct dentry *dentry,
494 struct eventfs_inode *ei, int idx,
495 umode_t mode, void *data,
496 const struct file_operations *fops)
498 struct eventfs_attr *attr = NULL;
501 attr = &ei->entry_attrs[idx];
503 return lookup_file(ei, dentry, mode, attr, data, fops);
507 * eventfs_root_lookup - lookup routine to create file/dir
508 * @dir: in which a lookup is being done
509 * @dentry: file/dir dentry
510 * @flags: Just passed to simple_lookup()
512 * Used to create dynamic file/dir with-in @dir, search with-in @ei
513 * list, if @dentry found go ahead and create the file/dir
516 static struct dentry *eventfs_root_lookup(struct inode *dir,
517 struct dentry *dentry,
520 struct eventfs_inode *ei_child;
521 struct tracefs_inode *ti;
522 struct eventfs_inode *ei;
523 const char *name = dentry->d_name.name;
524 struct dentry *result = NULL;
526 ti = get_tracefs(dir);
527 if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
528 return ERR_PTR(-EIO);
530 mutex_lock(&eventfs_mutex);
533 if (!ei || ei->is_freed)
536 list_for_each_entry(ei_child, &ei->children, list) {
537 if (strcmp(ei_child->name, name) != 0)
539 /* A child is freed and removed from the list at the same time */
540 if (WARN_ON_ONCE(ei_child->is_freed))
542 result = lookup_dir_entry(dentry, ei, ei_child);
546 for (int i = 0; i < ei->nr_entries; i++) {
549 const struct file_operations *fops;
550 const struct eventfs_entry *entry = &ei->entries[i];
552 if (strcmp(name, entry->name) != 0)
556 if (entry->callback(name, &mode, &data, &fops) <= 0)
559 result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
563 mutex_unlock(&eventfs_mutex);
568 * Walk the children of a eventfs_inode to fill in getdents().
570 static int eventfs_iterate(struct file *file, struct dir_context *ctx)
572 const struct file_operations *fops;
573 struct inode *f_inode = file_inode(file);
574 const struct eventfs_entry *entry;
575 struct eventfs_inode *ei_child;
576 struct tracefs_inode *ti;
577 struct eventfs_inode *ei;
585 if (!dir_emit_dots(file, ctx))
588 ti = get_tracefs(f_inode);
589 if (!(ti->flags & TRACEFS_EVENT_INODE))
594 idx = srcu_read_lock(&eventfs_srcu);
596 mutex_lock(&eventfs_mutex);
597 ei = READ_ONCE(ti->private);
598 if (ei && ei->is_freed)
600 mutex_unlock(&eventfs_mutex);
606 * Need to create the dentries and inodes to have a consistent
611 /* Start at 'c' to jump over already read entries */
612 for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
613 void *cdata = ei->data;
615 entry = &ei->entries[i];
618 mutex_lock(&eventfs_mutex);
619 /* If ei->is_freed then just bail here, nothing more to do */
621 mutex_unlock(&eventfs_mutex);
624 r = entry->callback(name, &mode, &cdata, &fops);
625 mutex_unlock(&eventfs_mutex);
629 ino = EVENTFS_FILE_INODE_INO;
631 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
635 /* Subtract the skipped entries above */
636 c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
638 list_for_each_entry_srcu(ei_child, &ei->children, list,
639 srcu_read_lock_held(&eventfs_srcu)) {
648 if (ei_child->is_freed)
651 name = ei_child->name;
653 ino = eventfs_dir_ino(ei_child);
655 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
660 srcu_read_unlock(&eventfs_srcu, idx);
665 /* Incremented ctx->pos without adding something, reset it */
671 * eventfs_create_dir - Create the eventfs_inode for this directory
672 * @name: The name of the directory to create.
673 * @parent: The eventfs_inode of the parent directory.
674 * @entries: A list of entries that represent the files under this directory
675 * @size: The number of @entries
676 * @data: The default data to pass to the files (an entry may override it).
678 * This function creates the descriptor to represent a directory in the
679 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
680 * used to create other children underneath.
682 * The @entries is an array of eventfs_entry structures which has:
684 * eventfs_callback callback;
686 * The name is the name of the file, and the callback is a pointer to a function
687 * that will be called when the file is reference (either by lookup or by
688 * reading a directory). The callback is of the prototype:
690 * int callback(const char *name, umode_t *mode, void **data,
691 * const struct file_operations **fops);
693 * When a file needs to be created, this callback will be called with
694 * name = the name of the file being created (so that the same callback
695 * may be used for multiple files).
696 * mode = a place to set the file's mode
697 * data = A pointer to @data, and the callback may replace it, which will
698 * cause the file created to pass the new data to the open() call.
699 * fops = the fops to use for the created file.
701 * NB. @callback is called while holding internal locks of the eventfs
702 * system. The callback must not call any code that might also call into
703 * the tracefs or eventfs system or it will risk creating a deadlock.
705 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
706 const struct eventfs_entry *entries,
707 int size, void *data)
709 struct eventfs_inode *ei;
712 return ERR_PTR(-EINVAL);
716 return ERR_PTR(-ENOMEM);
718 ei->entries = entries;
719 ei->nr_entries = size;
721 INIT_LIST_HEAD(&ei->children);
722 INIT_LIST_HEAD(&ei->list);
724 mutex_lock(&eventfs_mutex);
725 if (!parent->is_freed)
726 list_add_tail(&ei->list, &parent->children);
727 mutex_unlock(&eventfs_mutex);
729 /* Was the parent freed? */
730 if (list_empty(&ei->list)) {
738 * eventfs_create_events_dir - create the top level events directory
739 * @name: The name of the top level directory to create.
740 * @parent: Parent dentry for this file in the tracefs directory.
741 * @entries: A list of entries that represent the files under this directory
742 * @size: The number of @entries
743 * @data: The default data to pass to the files (an entry may override it).
745 * This function creates the top of the trace event directory.
747 * See eventfs_create_dir() for use of @entries.
749 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
750 const struct eventfs_entry *entries,
751 int size, void *data)
753 struct dentry *dentry = tracefs_start_creating(name, parent);
754 struct eventfs_root_inode *rei;
755 struct eventfs_inode *ei;
756 struct tracefs_inode *ti;
761 if (security_locked_down(LOCKDOWN_TRACEFS))
765 return ERR_CAST(dentry);
767 ei = alloc_root_ei(name);
771 inode = tracefs_get_inode(dentry->d_sb);
772 if (unlikely(!inode))
775 // Note: we have a ref to the dentry from tracefs_start_creating()
776 rei = get_root_inode(ei);
777 rei->events_dir = dentry;
779 ei->entries = entries;
780 ei->nr_entries = size;
783 /* Save the ownership of this directory */
784 uid = d_inode(dentry->d_parent)->i_uid;
785 gid = d_inode(dentry->d_parent)->i_gid;
788 * If the events directory is of the top instance, then parent
789 * is NULL. Set the attr.mode to reflect this and its permissions will
790 * default to the tracefs root dentry.
793 ei->attr.mode = EVENTFS_TOPLEVEL;
795 /* This is used as the default ownership of the files and directories */
799 INIT_LIST_HEAD(&ei->children);
800 INIT_LIST_HEAD(&ei->list);
802 ti = get_tracefs(inode);
803 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
806 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
809 inode->i_op = &eventfs_root_dir_inode_operations;
810 inode->i_fop = &eventfs_file_operations;
812 dentry->d_fsdata = get_ei(ei);
815 * Keep all eventfs directories with i_nlink == 1.
816 * Due to the dynamic nature of the dentry creations and not
817 * wanting to add a pointer to the parent eventfs_inode in the
818 * eventfs_inode structure, keeping the i_nlink in sync with the
819 * number of directories would cause too much complexity for
820 * something not worth much. Keeping directory links at 1
821 * tells userspace not to trust the link number.
823 d_instantiate(dentry, inode);
824 /* The dentry of the "events" parent does keep track though */
825 inc_nlink(dentry->d_parent->d_inode);
826 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
827 tracefs_end_creating(dentry);
833 tracefs_failed_creating(dentry);
834 return ERR_PTR(-ENOMEM);
838 * eventfs_remove_rec - remove eventfs dir or file from list
839 * @ei: eventfs_inode to be removed.
840 * @level: prevent recursion from going more than 3 levels deep.
842 * This function recursively removes eventfs_inodes which
843 * contains info of files and/or directories.
845 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
847 struct eventfs_inode *ei_child;
850 * Check recursion depth. It should never be greater than 3:
853 * 2 - events/group/event/
854 * 3 - events/group/event/file
856 if (WARN_ON_ONCE(level > 3))
859 /* search for nested folders or files */
860 list_for_each_entry(ei_child, &ei->children, list)
861 eventfs_remove_rec(ei_child, level + 1);
868 * eventfs_remove_dir - remove eventfs dir or file from list
869 * @ei: eventfs_inode to be removed.
871 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
873 void eventfs_remove_dir(struct eventfs_inode *ei)
878 mutex_lock(&eventfs_mutex);
879 eventfs_remove_rec(ei, 0);
880 mutex_unlock(&eventfs_mutex);
884 * eventfs_remove_events_dir - remove the top level eventfs directory
885 * @ei: the event_inode returned by eventfs_create_events_dir().
887 * This function removes the events main directory
889 void eventfs_remove_events_dir(struct eventfs_inode *ei)
891 struct eventfs_root_inode *rei;
892 struct dentry *dentry;
894 rei = get_root_inode(ei);
895 dentry = rei->events_dir;
899 rei->events_dir = NULL;
900 eventfs_remove_dir(ei);
903 * Matches the dget() done by tracefs_start_creating()
904 * in eventfs_create_events_dir() when it the dentry was
905 * created. In other words, it's a normal dentry that
906 * sticks around while the other ei->dentry are created
907 * and destroyed dynamically.
909 d_invalidate(dentry);