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)
66 * eventfs_inode reference count management.
68 * NOTE! We count only references from dentries, in the
69 * form 'dentry->d_fsdata'. There are also references from
70 * directory inodes ('ti->private'), but the dentry reference
71 * count is always a superset of the inode reference count.
73 static void release_ei(struct kref *ref)
75 struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
77 WARN_ON_ONCE(!ei->is_freed);
79 kfree(ei->entry_attrs);
80 kfree_const(ei->name);
84 static inline void put_ei(struct eventfs_inode *ei)
87 kref_put(&ei->kref, release_ei);
90 static inline void free_ei(struct eventfs_inode *ei)
98 static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
105 static struct dentry *eventfs_root_lookup(struct inode *dir,
106 struct dentry *dentry,
108 static int eventfs_iterate(struct file *file, struct dir_context *ctx);
110 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
112 unsigned int ia_valid = iattr->ia_valid;
114 if (ia_valid & ATTR_MODE) {
115 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
116 (iattr->ia_mode & EVENTFS_MODE_MASK) |
119 if (ia_valid & ATTR_UID) {
120 attr->mode |= EVENTFS_SAVE_UID;
121 attr->uid = iattr->ia_uid;
123 if (ia_valid & ATTR_GID) {
124 attr->mode |= EVENTFS_SAVE_GID;
125 attr->gid = iattr->ia_gid;
129 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
132 const struct eventfs_entry *entry;
133 struct eventfs_inode *ei;
137 mutex_lock(&eventfs_mutex);
138 ei = dentry->d_fsdata;
140 /* Do not allow changes if the event is about to be removed. */
141 mutex_unlock(&eventfs_mutex);
145 /* Preallocate the children mode array if necessary */
146 if (!(dentry->d_inode->i_mode & S_IFDIR)) {
147 if (!ei->entry_attrs) {
148 ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
150 if (!ei->entry_attrs) {
157 ret = simple_setattr(idmap, dentry, iattr);
162 * If this is a dir, then update the ei cache, only the file
163 * mode is saved in the ei->m_children, and the ownership is
164 * determined by the parent directory.
166 if (dentry->d_inode->i_mode & S_IFDIR) {
168 * The events directory dentry is never freed, unless its
169 * part of an instance that is deleted. It's attr is the
170 * default for its child files and directories.
171 * Do not update it. It's not used for its own mode or ownership.
174 /* But it still needs to know if it was modified */
175 if (iattr->ia_valid & ATTR_UID)
176 ei->attr.mode |= EVENTFS_SAVE_UID;
177 if (iattr->ia_valid & ATTR_GID)
178 ei->attr.mode |= EVENTFS_SAVE_GID;
180 update_attr(&ei->attr, iattr);
184 name = dentry->d_name.name;
186 for (int i = 0; i < ei->nr_entries; i++) {
187 entry = &ei->entries[i];
188 if (strcmp(name, entry->name) == 0) {
189 update_attr(&ei->entry_attrs[i], iattr);
195 mutex_unlock(&eventfs_mutex);
199 static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
203 /* Only update if the "events" was on the top level */
204 if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
207 /* Get the tracefs root inode. */
208 root = d_inode(sb->s_root);
209 ei->attr.uid = root->i_uid;
210 ei->attr.gid = root->i_gid;
213 static void set_top_events_ownership(struct inode *inode)
215 struct tracefs_inode *ti = get_tracefs(inode);
216 struct eventfs_inode *ei = ti->private;
218 /* The top events directory doesn't get automatically updated */
219 if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
222 update_top_events_attr(ei, inode->i_sb);
224 if (!(ei->attr.mode & EVENTFS_SAVE_UID))
225 inode->i_uid = ei->attr.uid;
227 if (!(ei->attr.mode & EVENTFS_SAVE_GID))
228 inode->i_gid = ei->attr.gid;
231 static int eventfs_get_attr(struct mnt_idmap *idmap,
232 const struct path *path, struct kstat *stat,
233 u32 request_mask, unsigned int flags)
235 struct dentry *dentry = path->dentry;
236 struct inode *inode = d_backing_inode(dentry);
238 set_top_events_ownership(inode);
240 generic_fillattr(idmap, request_mask, inode, stat);
244 static int eventfs_permission(struct mnt_idmap *idmap,
245 struct inode *inode, int mask)
247 set_top_events_ownership(inode);
248 return generic_permission(idmap, inode, mask);
251 static const struct inode_operations eventfs_root_dir_inode_operations = {
252 .lookup = eventfs_root_lookup,
253 .setattr = eventfs_set_attr,
254 .getattr = eventfs_get_attr,
255 .permission = eventfs_permission,
258 static const struct inode_operations eventfs_file_inode_operations = {
259 .setattr = eventfs_set_attr,
262 static const struct file_operations eventfs_file_operations = {
263 .read = generic_read_dir,
264 .iterate_shared = eventfs_iterate,
265 .llseek = generic_file_llseek,
268 /* Return the evenfs_inode of the "events" directory */
269 static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
271 struct eventfs_inode *ei;
274 // The parent is stable because we do not do renames
275 dentry = dentry->d_parent;
276 // ... and directories always have d_fsdata
277 ei = dentry->d_fsdata;
280 * If the ei is being freed, the ownership of the children
287 // Walk upwards until you find the events inode
288 } while (!ei->is_events);
290 update_top_events_attr(ei, dentry->d_sb);
295 static void update_inode_attr(struct dentry *dentry, struct inode *inode,
296 struct eventfs_attr *attr, umode_t mode)
298 struct eventfs_inode *events_ei = eventfs_find_events(dentry);
303 inode->i_mode = mode;
304 inode->i_uid = events_ei->attr.uid;
305 inode->i_gid = events_ei->attr.gid;
310 if (attr->mode & EVENTFS_SAVE_MODE)
311 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
313 if (attr->mode & EVENTFS_SAVE_UID)
314 inode->i_uid = attr->uid;
316 if (attr->mode & EVENTFS_SAVE_GID)
317 inode->i_gid = attr->gid;
321 * lookup_file - look up a file in the tracefs filesystem
322 * @dentry: the dentry to look up
323 * @mode: the permission that the file should have.
324 * @attr: saved attributes changed by user
325 * @data: something that the caller will want to get to later on.
326 * @fop: struct file_operations that should be used for this file.
328 * This function creates a dentry that represents a file in the eventsfs_inode
329 * directory. The inode.i_private pointer will point to @data in the open()
332 static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
333 struct dentry *dentry,
335 struct eventfs_attr *attr,
337 const struct file_operations *fop)
339 struct tracefs_inode *ti;
342 if (!(mode & S_IFMT))
345 if (WARN_ON_ONCE(!S_ISREG(mode)))
346 return ERR_PTR(-EIO);
348 inode = tracefs_get_inode(dentry->d_sb);
349 if (unlikely(!inode))
350 return ERR_PTR(-ENOMEM);
352 /* If the user updated the directory's attributes, use them */
353 update_inode_attr(dentry, inode, attr, mode);
355 inode->i_op = &eventfs_file_inode_operations;
357 inode->i_private = data;
359 /* All files will have the same inode number */
360 inode->i_ino = EVENTFS_FILE_INODE_INO;
362 ti = get_tracefs(inode);
363 ti->flags |= TRACEFS_EVENT_INODE;
365 // Files have their parent's ei as their fsdata
366 dentry->d_fsdata = get_ei(parent_ei);
368 d_add(dentry, inode);
373 * lookup_dir_entry - look up a dir in the tracefs filesystem
374 * @dentry: the directory to look up
375 * @ei: the eventfs_inode that represents the directory to create
377 * This function will look up a dentry for a directory represented by
380 static struct dentry *lookup_dir_entry(struct dentry *dentry,
381 struct eventfs_inode *pei, struct eventfs_inode *ei)
383 struct tracefs_inode *ti;
386 inode = tracefs_get_inode(dentry->d_sb);
387 if (unlikely(!inode))
388 return ERR_PTR(-ENOMEM);
390 /* If the user updated the directory's attributes, use them */
391 update_inode_attr(dentry, inode, &ei->attr,
392 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
394 inode->i_op = &eventfs_root_dir_inode_operations;
395 inode->i_fop = &eventfs_file_operations;
397 /* All directories will have the same inode number */
398 inode->i_ino = eventfs_dir_ino(ei);
400 ti = get_tracefs(inode);
401 ti->flags |= TRACEFS_EVENT_INODE;
402 /* Only directories have ti->private set to an ei, not files */
405 dentry->d_fsdata = get_ei(ei);
407 d_add(dentry, inode);
411 static inline struct eventfs_inode *alloc_ei(const char *name)
413 struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
418 ei->name = kstrdup_const(name, GFP_KERNEL);
423 kref_init(&ei->kref);
428 * eventfs_d_release - dentry is going away
429 * @dentry: dentry which has the reference to remove.
431 * Remove the association between a dentry from an eventfs_inode.
433 void eventfs_d_release(struct dentry *dentry)
435 put_ei(dentry->d_fsdata);
439 * lookup_file_dentry - create a dentry for a file of an eventfs_inode
440 * @ei: the eventfs_inode that the file will be created under
441 * @idx: the index into the entry_attrs[] of the @ei
442 * @parent: The parent dentry of the created file.
443 * @name: The name of the file to create
444 * @mode: The mode of the file.
445 * @data: The data to use to set the inode of the file with on open()
446 * @fops: The fops of the file to be created.
448 * Create a dentry for a file of an eventfs_inode @ei and place it into the
449 * address located at @e_dentry.
451 static struct dentry *
452 lookup_file_dentry(struct dentry *dentry,
453 struct eventfs_inode *ei, int idx,
454 umode_t mode, void *data,
455 const struct file_operations *fops)
457 struct eventfs_attr *attr = NULL;
460 attr = &ei->entry_attrs[idx];
462 return lookup_file(ei, dentry, mode, attr, data, fops);
466 * eventfs_root_lookup - lookup routine to create file/dir
467 * @dir: in which a lookup is being done
468 * @dentry: file/dir dentry
469 * @flags: Just passed to simple_lookup()
471 * Used to create dynamic file/dir with-in @dir, search with-in @ei
472 * list, if @dentry found go ahead and create the file/dir
475 static struct dentry *eventfs_root_lookup(struct inode *dir,
476 struct dentry *dentry,
479 struct eventfs_inode *ei_child;
480 struct tracefs_inode *ti;
481 struct eventfs_inode *ei;
482 const char *name = dentry->d_name.name;
483 struct dentry *result = NULL;
485 ti = get_tracefs(dir);
486 if (!(ti->flags & TRACEFS_EVENT_INODE))
487 return ERR_PTR(-EIO);
489 mutex_lock(&eventfs_mutex);
492 if (!ei || ei->is_freed)
495 list_for_each_entry(ei_child, &ei->children, list) {
496 if (strcmp(ei_child->name, name) != 0)
498 if (ei_child->is_freed)
500 result = lookup_dir_entry(dentry, ei, ei_child);
504 for (int i = 0; i < ei->nr_entries; i++) {
507 const struct file_operations *fops;
508 const struct eventfs_entry *entry = &ei->entries[i];
510 if (strcmp(name, entry->name) != 0)
514 if (entry->callback(name, &mode, &data, &fops) <= 0)
517 result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
521 mutex_unlock(&eventfs_mutex);
526 * Walk the children of a eventfs_inode to fill in getdents().
528 static int eventfs_iterate(struct file *file, struct dir_context *ctx)
530 const struct file_operations *fops;
531 struct inode *f_inode = file_inode(file);
532 const struct eventfs_entry *entry;
533 struct eventfs_inode *ei_child;
534 struct tracefs_inode *ti;
535 struct eventfs_inode *ei;
543 if (!dir_emit_dots(file, ctx))
546 ti = get_tracefs(f_inode);
547 if (!(ti->flags & TRACEFS_EVENT_INODE))
552 idx = srcu_read_lock(&eventfs_srcu);
554 mutex_lock(&eventfs_mutex);
555 ei = READ_ONCE(ti->private);
556 if (ei && ei->is_freed)
558 mutex_unlock(&eventfs_mutex);
564 * Need to create the dentries and inodes to have a consistent
569 /* Start at 'c' to jump over already read entries */
570 for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
571 void *cdata = ei->data;
573 entry = &ei->entries[i];
576 mutex_lock(&eventfs_mutex);
577 /* If ei->is_freed then just bail here, nothing more to do */
579 mutex_unlock(&eventfs_mutex);
582 r = entry->callback(name, &mode, &cdata, &fops);
583 mutex_unlock(&eventfs_mutex);
587 ino = EVENTFS_FILE_INODE_INO;
589 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
593 /* Subtract the skipped entries above */
594 c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
596 list_for_each_entry_srcu(ei_child, &ei->children, list,
597 srcu_read_lock_held(&eventfs_srcu)) {
606 if (ei_child->is_freed)
609 name = ei_child->name;
611 ino = eventfs_dir_ino(ei_child);
613 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
618 srcu_read_unlock(&eventfs_srcu, idx);
623 /* Incremented ctx->pos without adding something, reset it */
629 * eventfs_create_dir - Create the eventfs_inode for this directory
630 * @name: The name of the directory to create.
631 * @parent: The eventfs_inode of the parent directory.
632 * @entries: A list of entries that represent the files under this directory
633 * @size: The number of @entries
634 * @data: The default data to pass to the files (an entry may override it).
636 * This function creates the descriptor to represent a directory in the
637 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
638 * used to create other children underneath.
640 * The @entries is an array of eventfs_entry structures which has:
642 * eventfs_callback callback;
644 * The name is the name of the file, and the callback is a pointer to a function
645 * that will be called when the file is reference (either by lookup or by
646 * reading a directory). The callback is of the prototype:
648 * int callback(const char *name, umode_t *mode, void **data,
649 * const struct file_operations **fops);
651 * When a file needs to be created, this callback will be called with
652 * name = the name of the file being created (so that the same callback
653 * may be used for multiple files).
654 * mode = a place to set the file's mode
655 * data = A pointer to @data, and the callback may replace it, which will
656 * cause the file created to pass the new data to the open() call.
657 * fops = the fops to use for the created file.
659 * NB. @callback is called while holding internal locks of the eventfs
660 * system. The callback must not call any code that might also call into
661 * the tracefs or eventfs system or it will risk creating a deadlock.
663 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
664 const struct eventfs_entry *entries,
665 int size, void *data)
667 struct eventfs_inode *ei;
670 return ERR_PTR(-EINVAL);
674 return ERR_PTR(-ENOMEM);
676 ei->entries = entries;
677 ei->nr_entries = size;
679 INIT_LIST_HEAD(&ei->children);
680 INIT_LIST_HEAD(&ei->list);
682 mutex_lock(&eventfs_mutex);
683 if (!parent->is_freed)
684 list_add_tail(&ei->list, &parent->children);
685 mutex_unlock(&eventfs_mutex);
687 /* Was the parent freed? */
688 if (list_empty(&ei->list)) {
696 * eventfs_create_events_dir - create the top level events directory
697 * @name: The name of the top level directory to create.
698 * @parent: Parent dentry for this file in the tracefs directory.
699 * @entries: A list of entries that represent the files under this directory
700 * @size: The number of @entries
701 * @data: The default data to pass to the files (an entry may override it).
703 * This function creates the top of the trace event directory.
705 * See eventfs_create_dir() for use of @entries.
707 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
708 const struct eventfs_entry *entries,
709 int size, void *data)
711 struct dentry *dentry = tracefs_start_creating(name, parent);
712 struct eventfs_inode *ei;
713 struct tracefs_inode *ti;
718 if (security_locked_down(LOCKDOWN_TRACEFS))
722 return ERR_CAST(dentry);
728 inode = tracefs_get_inode(dentry->d_sb);
729 if (unlikely(!inode))
732 // Note: we have a ref to the dentry from tracefs_start_creating()
733 ei->events_dir = dentry;
734 ei->entries = entries;
735 ei->nr_entries = size;
739 /* Save the ownership of this directory */
740 uid = d_inode(dentry->d_parent)->i_uid;
741 gid = d_inode(dentry->d_parent)->i_gid;
744 * If the events directory is of the top instance, then parent
745 * is NULL. Set the attr.mode to reflect this and its permissions will
746 * default to the tracefs root dentry.
749 ei->attr.mode = EVENTFS_TOPLEVEL;
751 /* This is used as the default ownership of the files and directories */
755 INIT_LIST_HEAD(&ei->children);
756 INIT_LIST_HEAD(&ei->list);
758 ti = get_tracefs(inode);
759 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
762 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
765 inode->i_op = &eventfs_root_dir_inode_operations;
766 inode->i_fop = &eventfs_file_operations;
768 dentry->d_fsdata = get_ei(ei);
771 * Keep all eventfs directories with i_nlink == 1.
772 * Due to the dynamic nature of the dentry creations and not
773 * wanting to add a pointer to the parent eventfs_inode in the
774 * eventfs_inode structure, keeping the i_nlink in sync with the
775 * number of directories would cause too much complexity for
776 * something not worth much. Keeping directory links at 1
777 * tells userspace not to trust the link number.
779 d_instantiate(dentry, inode);
780 /* The dentry of the "events" parent does keep track though */
781 inc_nlink(dentry->d_parent->d_inode);
782 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
783 tracefs_end_creating(dentry);
789 tracefs_failed_creating(dentry);
790 return ERR_PTR(-ENOMEM);
794 * eventfs_remove_rec - remove eventfs dir or file from list
795 * @ei: eventfs_inode to be removed.
796 * @level: prevent recursion from going more than 3 levels deep.
798 * This function recursively removes eventfs_inodes which
799 * contains info of files and/or directories.
801 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
803 struct eventfs_inode *ei_child;
806 * Check recursion depth. It should never be greater than 3:
809 * 2 - events/group/event/
810 * 3 - events/group/event/file
812 if (WARN_ON_ONCE(level > 3))
815 /* search for nested folders or files */
816 list_for_each_entry(ei_child, &ei->children, list)
817 eventfs_remove_rec(ei_child, level + 1);
824 * eventfs_remove_dir - remove eventfs dir or file from list
825 * @ei: eventfs_inode to be removed.
827 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
829 void eventfs_remove_dir(struct eventfs_inode *ei)
834 mutex_lock(&eventfs_mutex);
835 eventfs_remove_rec(ei, 0);
836 mutex_unlock(&eventfs_mutex);
840 * eventfs_remove_events_dir - remove the top level eventfs directory
841 * @ei: the event_inode returned by eventfs_create_events_dir().
843 * This function removes the events main directory
845 void eventfs_remove_events_dir(struct eventfs_inode *ei)
847 struct dentry *dentry;
849 dentry = ei->events_dir;
853 ei->events_dir = NULL;
854 eventfs_remove_dir(ei);
857 * Matches the dget() done by tracefs_start_creating()
858 * in eventfs_create_events_dir() when it the dentry was
859 * created. In other words, it's a normal dentry that
860 * sticks around while the other ei->dentry are created
861 * and destroyed dynamically.
863 d_invalidate(dentry);