Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * event_inode.c - part of tracefs, a pseudo file system for activating tracing
4 *
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>
8 *
9 * eventfs is used to dynamically create inodes and dentries based on the
10 * meta data provided by the tracing system.
11 *
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.
16 */
17#include <linux/fsnotify.h>
18#include <linux/fs.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>
25#include "internal.h"
26
27/*
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.
32 */
33static DEFINE_MUTEX(eventfs_mutex);
34
35/* Choose something "unique" ;-) */
36#define EVENTFS_FILE_INODE_INO 0x12c4e37
37
38/* Just try to make something consistent and unique */
39static int eventfs_dir_ino(struct eventfs_inode *ei)
40{
41 if (!ei->ino)
42 ei->ino = get_next_ino();
43
44 return ei->ino;
45}
46
47/*
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
51 * the ei is freed.
52 */
53DEFINE_STATIC_SRCU(eventfs_srcu);
54
55/* Mode is unsigned short, use the upper bits for flags */
56enum {
57 EVENTFS_SAVE_MODE = BIT(16),
58 EVENTFS_SAVE_UID = BIT(17),
59 EVENTFS_SAVE_GID = BIT(18),
60 EVENTFS_TOPLEVEL = BIT(19),
61};
62
63#define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
64
65/*
66 * eventfs_inode reference count management.
67 *
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.
72 */
73static void release_ei(struct kref *ref)
74{
75 struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
76
77 WARN_ON_ONCE(!ei->is_freed);
78
79 kfree(ei->entry_attrs);
80 kfree_const(ei->name);
81 kfree_rcu(ei, rcu);
82}
83
84static inline void put_ei(struct eventfs_inode *ei)
85{
86 if (ei)
87 kref_put(&ei->kref, release_ei);
88}
89
90static inline void free_ei(struct eventfs_inode *ei)
91{
92 if (ei) {
93 ei->is_freed = 1;
94 put_ei(ei);
95 }
96}
97
98static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
99{
100 if (ei)
101 kref_get(&ei->kref);
102 return ei;
103}
104
105static struct dentry *eventfs_root_lookup(struct inode *dir,
106 struct dentry *dentry,
107 unsigned int flags);
108static int eventfs_iterate(struct file *file, struct dir_context *ctx);
109
110static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
111{
112 unsigned int ia_valid = iattr->ia_valid;
113
114 if (ia_valid & ATTR_MODE) {
115 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
116 (iattr->ia_mode & EVENTFS_MODE_MASK) |
117 EVENTFS_SAVE_MODE;
118 }
119 if (ia_valid & ATTR_UID) {
120 attr->mode |= EVENTFS_SAVE_UID;
121 attr->uid = iattr->ia_uid;
122 }
123 if (ia_valid & ATTR_GID) {
124 attr->mode |= EVENTFS_SAVE_GID;
125 attr->gid = iattr->ia_gid;
126 }
127}
128
129static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
130 struct iattr *iattr)
131{
132 const struct eventfs_entry *entry;
133 struct eventfs_inode *ei;
134 const char *name;
135 int ret;
136
137 mutex_lock(&eventfs_mutex);
138 ei = dentry->d_fsdata;
139 if (ei->is_freed) {
140 /* Do not allow changes if the event is about to be removed. */
141 mutex_unlock(&eventfs_mutex);
142 return -ENODEV;
143 }
144
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),
149 GFP_NOFS);
150 if (!ei->entry_attrs) {
151 ret = -ENOMEM;
152 goto out;
153 }
154 }
155 }
156
157 ret = simple_setattr(idmap, dentry, iattr);
158 if (ret < 0)
159 goto out;
160
161 /*
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.
165 */
166 if (dentry->d_inode->i_mode & S_IFDIR) {
167 /*
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.
172 */
173 if (ei->is_events) {
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;
179 } else {
180 update_attr(&ei->attr, iattr);
181 }
182
183 } else {
184 name = dentry->d_name.name;
185
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);
190 break;
191 }
192 }
193 }
194 out:
195 mutex_unlock(&eventfs_mutex);
196 return ret;
197}
198
199static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
200{
201 struct inode *root;
202
203 /* Only update if the "events" was on the top level */
204 if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
205 return;
206
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;
211}
212
213static void set_top_events_ownership(struct inode *inode)
214{
215 struct tracefs_inode *ti = get_tracefs(inode);
216 struct eventfs_inode *ei = ti->private;
217
218 /* The top events directory doesn't get automatically updated */
219 if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
220 return;
221
222 update_top_events_attr(ei, inode->i_sb);
223
224 if (!(ei->attr.mode & EVENTFS_SAVE_UID))
225 inode->i_uid = ei->attr.uid;
226
227 if (!(ei->attr.mode & EVENTFS_SAVE_GID))
228 inode->i_gid = ei->attr.gid;
229}
230
231static int eventfs_get_attr(struct mnt_idmap *idmap,
232 const struct path *path, struct kstat *stat,
233 u32 request_mask, unsigned int flags)
234{
235 struct dentry *dentry = path->dentry;
236 struct inode *inode = d_backing_inode(dentry);
237
238 set_top_events_ownership(inode);
239
240 generic_fillattr(idmap, request_mask, inode, stat);
241 return 0;
242}
243
244static int eventfs_permission(struct mnt_idmap *idmap,
245 struct inode *inode, int mask)
246{
247 set_top_events_ownership(inode);
248 return generic_permission(idmap, inode, mask);
249}
250
251static 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,
256};
257
258static const struct inode_operations eventfs_file_inode_operations = {
259 .setattr = eventfs_set_attr,
260};
261
262static const struct file_operations eventfs_file_operations = {
263 .read = generic_read_dir,
264 .iterate_shared = eventfs_iterate,
265 .llseek = generic_file_llseek,
266};
267
268/* Return the evenfs_inode of the "events" directory */
269static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
270{
271 struct eventfs_inode *ei;
272
273 do {
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;
278
279 /*
280 * If the ei is being freed, the ownership of the children
281 * doesn't matter.
282 */
283 if (ei->is_freed) {
284 ei = NULL;
285 break;
286 }
287 // Walk upwards until you find the events inode
288 } while (!ei->is_events);
289
290 update_top_events_attr(ei, dentry->d_sb);
291
292 return ei;
293}
294
295static void update_inode_attr(struct dentry *dentry, struct inode *inode,
296 struct eventfs_attr *attr, umode_t mode)
297{
298 struct eventfs_inode *events_ei = eventfs_find_events(dentry);
299
300 if (!events_ei)
301 return;
302
303 inode->i_mode = mode;
304 inode->i_uid = events_ei->attr.uid;
305 inode->i_gid = events_ei->attr.gid;
306
307 if (!attr)
308 return;
309
310 if (attr->mode & EVENTFS_SAVE_MODE)
311 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
312
313 if (attr->mode & EVENTFS_SAVE_UID)
314 inode->i_uid = attr->uid;
315
316 if (attr->mode & EVENTFS_SAVE_GID)
317 inode->i_gid = attr->gid;
318}
319
320/**
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.
327 *
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()
330 * call.
331 */
332static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
333 struct dentry *dentry,
334 umode_t mode,
335 struct eventfs_attr *attr,
336 void *data,
337 const struct file_operations *fop)
338{
339 struct tracefs_inode *ti;
340 struct inode *inode;
341
342 if (!(mode & S_IFMT))
343 mode |= S_IFREG;
344
345 if (WARN_ON_ONCE(!S_ISREG(mode)))
346 return ERR_PTR(-EIO);
347
348 inode = tracefs_get_inode(dentry->d_sb);
349 if (unlikely(!inode))
350 return ERR_PTR(-ENOMEM);
351
352 /* If the user updated the directory's attributes, use them */
353 update_inode_attr(dentry, inode, attr, mode);
354
355 inode->i_op = &eventfs_file_inode_operations;
356 inode->i_fop = fop;
357 inode->i_private = data;
358
359 /* All files will have the same inode number */
360 inode->i_ino = EVENTFS_FILE_INODE_INO;
361
362 ti = get_tracefs(inode);
363 ti->flags |= TRACEFS_EVENT_INODE;
364
365 // Files have their parent's ei as their fsdata
366 dentry->d_fsdata = get_ei(parent_ei);
367
368 d_add(dentry, inode);
369 return NULL;
370};
371
372/**
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
376 *
377 * This function will look up a dentry for a directory represented by
378 * a eventfs_inode.
379 */
380static struct dentry *lookup_dir_entry(struct dentry *dentry,
381 struct eventfs_inode *pei, struct eventfs_inode *ei)
382{
383 struct tracefs_inode *ti;
384 struct inode *inode;
385
386 inode = tracefs_get_inode(dentry->d_sb);
387 if (unlikely(!inode))
388 return ERR_PTR(-ENOMEM);
389
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);
393
394 inode->i_op = &eventfs_root_dir_inode_operations;
395 inode->i_fop = &eventfs_file_operations;
396
397 /* All directories will have the same inode number */
398 inode->i_ino = eventfs_dir_ino(ei);
399
400 ti = get_tracefs(inode);
401 ti->flags |= TRACEFS_EVENT_INODE;
402 /* Only directories have ti->private set to an ei, not files */
403 ti->private = ei;
404
405 dentry->d_fsdata = get_ei(ei);
406
407 d_add(dentry, inode);
408 return NULL;
409}
410
411static inline struct eventfs_inode *alloc_ei(const char *name)
412{
413 struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
414
415 if (!ei)
416 return NULL;
417
418 ei->name = kstrdup_const(name, GFP_KERNEL);
419 if (!ei->name) {
420 kfree(ei);
421 return NULL;
422 }
423 kref_init(&ei->kref);
424 return ei;
425}
426
427/**
428 * eventfs_d_release - dentry is going away
429 * @dentry: dentry which has the reference to remove.
430 *
431 * Remove the association between a dentry from an eventfs_inode.
432 */
433void eventfs_d_release(struct dentry *dentry)
434{
435 put_ei(dentry->d_fsdata);
436}
437
438/**
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.
447 *
448 * Create a dentry for a file of an eventfs_inode @ei and place it into the
449 * address located at @e_dentry.
450 */
451static struct dentry *
452lookup_file_dentry(struct dentry *dentry,
453 struct eventfs_inode *ei, int idx,
454 umode_t mode, void *data,
455 const struct file_operations *fops)
456{
457 struct eventfs_attr *attr = NULL;
458
459 if (ei->entry_attrs)
460 attr = &ei->entry_attrs[idx];
461
462 return lookup_file(ei, dentry, mode, attr, data, fops);
463}
464
465/**
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()
470 *
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
473 */
474
475static struct dentry *eventfs_root_lookup(struct inode *dir,
476 struct dentry *dentry,
477 unsigned int flags)
478{
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;
484
485 ti = get_tracefs(dir);
486 if (!(ti->flags & TRACEFS_EVENT_INODE))
487 return ERR_PTR(-EIO);
488
489 mutex_lock(&eventfs_mutex);
490
491 ei = ti->private;
492 if (!ei || ei->is_freed)
493 goto out;
494
495 list_for_each_entry(ei_child, &ei->children, list) {
496 if (strcmp(ei_child->name, name) != 0)
497 continue;
498 if (ei_child->is_freed)
499 goto out;
500 result = lookup_dir_entry(dentry, ei, ei_child);
501 goto out;
502 }
503
504 for (int i = 0; i < ei->nr_entries; i++) {
505 void *data;
506 umode_t mode;
507 const struct file_operations *fops;
508 const struct eventfs_entry *entry = &ei->entries[i];
509
510 if (strcmp(name, entry->name) != 0)
511 continue;
512
513 data = ei->data;
514 if (entry->callback(name, &mode, &data, &fops) <= 0)
515 goto out;
516
517 result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
518 goto out;
519 }
520 out:
521 mutex_unlock(&eventfs_mutex);
522 return result;
523}
524
525/*
526 * Walk the children of a eventfs_inode to fill in getdents().
527 */
528static int eventfs_iterate(struct file *file, struct dir_context *ctx)
529{
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;
536 const char *name;
537 umode_t mode;
538 int idx;
539 int ret = -EINVAL;
540 int ino;
541 int i, r, c;
542
543 if (!dir_emit_dots(file, ctx))
544 return 0;
545
546 ti = get_tracefs(f_inode);
547 if (!(ti->flags & TRACEFS_EVENT_INODE))
548 return -EINVAL;
549
550 c = ctx->pos - 2;
551
552 idx = srcu_read_lock(&eventfs_srcu);
553
554 mutex_lock(&eventfs_mutex);
555 ei = READ_ONCE(ti->private);
556 if (ei && ei->is_freed)
557 ei = NULL;
558 mutex_unlock(&eventfs_mutex);
559
560 if (!ei)
561 goto out;
562
563 /*
564 * Need to create the dentries and inodes to have a consistent
565 * inode number.
566 */
567 ret = 0;
568
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;
572
573 entry = &ei->entries[i];
574 name = entry->name;
575
576 mutex_lock(&eventfs_mutex);
577 /* If ei->is_freed then just bail here, nothing more to do */
578 if (ei->is_freed) {
579 mutex_unlock(&eventfs_mutex);
580 goto out;
581 }
582 r = entry->callback(name, &mode, &cdata, &fops);
583 mutex_unlock(&eventfs_mutex);
584 if (r <= 0)
585 continue;
586
587 ino = EVENTFS_FILE_INODE_INO;
588
589 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
590 goto out;
591 }
592
593 /* Subtract the skipped entries above */
594 c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
595
596 list_for_each_entry_srcu(ei_child, &ei->children, list,
597 srcu_read_lock_held(&eventfs_srcu)) {
598
599 if (c > 0) {
600 c--;
601 continue;
602 }
603
604 ctx->pos++;
605
606 if (ei_child->is_freed)
607 continue;
608
609 name = ei_child->name;
610
611 ino = eventfs_dir_ino(ei_child);
612
613 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
614 goto out_dec;
615 }
616 ret = 1;
617 out:
618 srcu_read_unlock(&eventfs_srcu, idx);
619
620 return ret;
621
622 out_dec:
623 /* Incremented ctx->pos without adding something, reset it */
624 ctx->pos--;
625 goto out;
626}
627
628/**
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).
635 *
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.
639 *
640 * The @entries is an array of eventfs_entry structures which has:
641 * const char *name
642 * eventfs_callback callback;
643 *
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:
647 *
648 * int callback(const char *name, umode_t *mode, void **data,
649 * const struct file_operations **fops);
650 *
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.
658 *
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.
662 */
663struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
664 const struct eventfs_entry *entries,
665 int size, void *data)
666{
667 struct eventfs_inode *ei;
668
669 if (!parent)
670 return ERR_PTR(-EINVAL);
671
672 ei = alloc_ei(name);
673 if (!ei)
674 return ERR_PTR(-ENOMEM);
675
676 ei->entries = entries;
677 ei->nr_entries = size;
678 ei->data = data;
679 INIT_LIST_HEAD(&ei->children);
680 INIT_LIST_HEAD(&ei->list);
681
682 mutex_lock(&eventfs_mutex);
683 if (!parent->is_freed)
684 list_add_tail(&ei->list, &parent->children);
685 mutex_unlock(&eventfs_mutex);
686
687 /* Was the parent freed? */
688 if (list_empty(&ei->list)) {
689 free_ei(ei);
690 ei = NULL;
691 }
692 return ei;
693}
694
695/**
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).
702 *
703 * This function creates the top of the trace event directory.
704 *
705 * See eventfs_create_dir() for use of @entries.
706 */
707struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
708 const struct eventfs_entry *entries,
709 int size, void *data)
710{
711 struct dentry *dentry = tracefs_start_creating(name, parent);
712 struct eventfs_inode *ei;
713 struct tracefs_inode *ti;
714 struct inode *inode;
715 kuid_t uid;
716 kgid_t gid;
717
718 if (security_locked_down(LOCKDOWN_TRACEFS))
719 return NULL;
720
721 if (IS_ERR(dentry))
722 return ERR_CAST(dentry);
723
724 ei = alloc_ei(name);
725 if (!ei)
726 goto fail;
727
728 inode = tracefs_get_inode(dentry->d_sb);
729 if (unlikely(!inode))
730 goto fail;
731
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;
736 ei->is_events = 1;
737 ei->data = data;
738
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;
742
743 /*
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.
747 */
748 if (!parent)
749 ei->attr.mode = EVENTFS_TOPLEVEL;
750
751 /* This is used as the default ownership of the files and directories */
752 ei->attr.uid = uid;
753 ei->attr.gid = gid;
754
755 INIT_LIST_HEAD(&ei->children);
756 INIT_LIST_HEAD(&ei->list);
757
758 ti = get_tracefs(inode);
759 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
760 ti->private = ei;
761
762 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
763 inode->i_uid = uid;
764 inode->i_gid = gid;
765 inode->i_op = &eventfs_root_dir_inode_operations;
766 inode->i_fop = &eventfs_file_operations;
767
768 dentry->d_fsdata = get_ei(ei);
769
770 /*
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.
778 */
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);
784
785 return ei;
786
787 fail:
788 free_ei(ei);
789 tracefs_failed_creating(dentry);
790 return ERR_PTR(-ENOMEM);
791}
792
793/**
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.
797 *
798 * This function recursively removes eventfs_inodes which
799 * contains info of files and/or directories.
800 */
801static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
802{
803 struct eventfs_inode *ei_child;
804
805 /*
806 * Check recursion depth. It should never be greater than 3:
807 * 0 - events/
808 * 1 - events/group/
809 * 2 - events/group/event/
810 * 3 - events/group/event/file
811 */
812 if (WARN_ON_ONCE(level > 3))
813 return;
814
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);
818
819 list_del(&ei->list);
820 free_ei(ei);
821}
822
823/**
824 * eventfs_remove_dir - remove eventfs dir or file from list
825 * @ei: eventfs_inode to be removed.
826 *
827 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
828 */
829void eventfs_remove_dir(struct eventfs_inode *ei)
830{
831 if (!ei)
832 return;
833
834 mutex_lock(&eventfs_mutex);
835 eventfs_remove_rec(ei, 0);
836 mutex_unlock(&eventfs_mutex);
837}
838
839/**
840 * eventfs_remove_events_dir - remove the top level eventfs directory
841 * @ei: the event_inode returned by eventfs_create_events_dir().
842 *
843 * This function removes the events main directory
844 */
845void eventfs_remove_events_dir(struct eventfs_inode *ei)
846{
847 struct dentry *dentry;
848
849 dentry = ei->events_dir;
850 if (!dentry)
851 return;
852
853 ei->events_dir = NULL;
854 eventfs_remove_dir(ei);
855
856 /*
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.
862 */
863 d_invalidate(dentry);
864 dput(dentry);
865}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * event_inode.c - part of tracefs, a pseudo file system for activating tracing
4 *
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>
8 *
9 * eventfs is used to dynamically create inodes and dentries based on the
10 * meta data provided by the tracing system.
11 *
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.
16 */
17#include <linux/fsnotify.h>
18#include <linux/fs.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>
25#include "internal.h"
26
27/*
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.
32 */
33static DEFINE_MUTEX(eventfs_mutex);
34
35/* Choose something "unique" ;-) */
36#define EVENTFS_FILE_INODE_INO 0x12c4e37
37
38struct eventfs_root_inode {
39 struct eventfs_inode ei;
40 struct inode *parent_inode;
41 struct dentry *events_dir;
42};
43
44static struct eventfs_root_inode *get_root_inode(struct eventfs_inode *ei)
45{
46 WARN_ON_ONCE(!ei->is_events);
47 return container_of(ei, struct eventfs_root_inode, ei);
48}
49
50/* Just try to make something consistent and unique */
51static int eventfs_dir_ino(struct eventfs_inode *ei)
52{
53 if (!ei->ino)
54 ei->ino = get_next_ino();
55
56 return ei->ino;
57}
58
59/*
60 * The eventfs_inode (ei) itself is protected by SRCU. It is released from
61 * its parent's list and will have is_freed set (under eventfs_mutex).
62 * After the SRCU grace period is over and the last dput() is called
63 * the ei is freed.
64 */
65DEFINE_STATIC_SRCU(eventfs_srcu);
66
67/* Mode is unsigned short, use the upper bits for flags */
68enum {
69 EVENTFS_SAVE_MODE = BIT(16),
70 EVENTFS_SAVE_UID = BIT(17),
71 EVENTFS_SAVE_GID = BIT(18),
72};
73
74#define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
75
76static void free_ei_rcu(struct rcu_head *rcu)
77{
78 struct eventfs_inode *ei = container_of(rcu, struct eventfs_inode, rcu);
79 struct eventfs_root_inode *rei;
80
81 kfree(ei->entry_attrs);
82 kfree_const(ei->name);
83 if (ei->is_events) {
84 rei = get_root_inode(ei);
85 kfree(rei);
86 } else {
87 kfree(ei);
88 }
89}
90
91/*
92 * eventfs_inode reference count management.
93 *
94 * NOTE! We count only references from dentries, in the
95 * form 'dentry->d_fsdata'. There are also references from
96 * directory inodes ('ti->private'), but the dentry reference
97 * count is always a superset of the inode reference count.
98 */
99static void release_ei(struct kref *ref)
100{
101 struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
102 const struct eventfs_entry *entry;
103
104 WARN_ON_ONCE(!ei->is_freed);
105
106 for (int i = 0; i < ei->nr_entries; i++) {
107 entry = &ei->entries[i];
108 if (entry->release)
109 entry->release(entry->name, ei->data);
110 }
111
112 call_rcu(&ei->rcu, free_ei_rcu);
113}
114
115static inline void put_ei(struct eventfs_inode *ei)
116{
117 if (ei)
118 kref_put(&ei->kref, release_ei);
119}
120
121static inline void free_ei(struct eventfs_inode *ei)
122{
123 if (ei) {
124 ei->is_freed = 1;
125 put_ei(ei);
126 }
127}
128
129/*
130 * Called when creation of an ei fails, do not call release() functions.
131 */
132static inline void cleanup_ei(struct eventfs_inode *ei)
133{
134 if (ei) {
135 /* Set nr_entries to 0 to prevent release() function being called */
136 ei->nr_entries = 0;
137 free_ei(ei);
138 }
139}
140
141static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
142{
143 if (ei)
144 kref_get(&ei->kref);
145 return ei;
146}
147
148static struct dentry *eventfs_root_lookup(struct inode *dir,
149 struct dentry *dentry,
150 unsigned int flags);
151static int eventfs_iterate(struct file *file, struct dir_context *ctx);
152
153static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
154{
155 unsigned int ia_valid = iattr->ia_valid;
156
157 if (ia_valid & ATTR_MODE) {
158 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
159 (iattr->ia_mode & EVENTFS_MODE_MASK) |
160 EVENTFS_SAVE_MODE;
161 }
162 if (ia_valid & ATTR_UID) {
163 attr->mode |= EVENTFS_SAVE_UID;
164 attr->uid = iattr->ia_uid;
165 }
166 if (ia_valid & ATTR_GID) {
167 attr->mode |= EVENTFS_SAVE_GID;
168 attr->gid = iattr->ia_gid;
169 }
170}
171
172static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
173 struct iattr *iattr)
174{
175 const struct eventfs_entry *entry;
176 struct eventfs_inode *ei;
177 const char *name;
178 int ret;
179
180 mutex_lock(&eventfs_mutex);
181 ei = dentry->d_fsdata;
182 if (ei->is_freed) {
183 /* Do not allow changes if the event is about to be removed. */
184 mutex_unlock(&eventfs_mutex);
185 return -ENODEV;
186 }
187
188 /* Preallocate the children mode array if necessary */
189 if (!(dentry->d_inode->i_mode & S_IFDIR)) {
190 if (!ei->entry_attrs) {
191 ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
192 GFP_NOFS);
193 if (!ei->entry_attrs) {
194 ret = -ENOMEM;
195 goto out;
196 }
197 }
198 }
199
200 ret = simple_setattr(idmap, dentry, iattr);
201 if (ret < 0)
202 goto out;
203
204 /*
205 * If this is a dir, then update the ei cache, only the file
206 * mode is saved in the ei->m_children, and the ownership is
207 * determined by the parent directory.
208 */
209 if (dentry->d_inode->i_mode & S_IFDIR) {
210 update_attr(&ei->attr, iattr);
211
212 } else {
213 name = dentry->d_name.name;
214
215 for (int i = 0; i < ei->nr_entries; i++) {
216 entry = &ei->entries[i];
217 if (strcmp(name, entry->name) == 0) {
218 update_attr(&ei->entry_attrs[i], iattr);
219 break;
220 }
221 }
222 }
223 out:
224 mutex_unlock(&eventfs_mutex);
225 return ret;
226}
227
228static void update_events_attr(struct eventfs_inode *ei, struct super_block *sb)
229{
230 struct eventfs_root_inode *rei;
231 struct inode *parent;
232
233 rei = get_root_inode(ei);
234
235 /* Use the parent inode permissions unless root set its permissions */
236 parent = rei->parent_inode;
237
238 if (rei->ei.attr.mode & EVENTFS_SAVE_UID)
239 ei->attr.uid = rei->ei.attr.uid;
240 else
241 ei->attr.uid = parent->i_uid;
242
243 if (rei->ei.attr.mode & EVENTFS_SAVE_GID)
244 ei->attr.gid = rei->ei.attr.gid;
245 else
246 ei->attr.gid = parent->i_gid;
247}
248
249static void set_top_events_ownership(struct inode *inode)
250{
251 struct tracefs_inode *ti = get_tracefs(inode);
252 struct eventfs_inode *ei = ti->private;
253
254 /* The top events directory doesn't get automatically updated */
255 if (!ei || !ei->is_events)
256 return;
257
258 update_events_attr(ei, inode->i_sb);
259
260 if (!(ei->attr.mode & EVENTFS_SAVE_UID))
261 inode->i_uid = ei->attr.uid;
262
263 if (!(ei->attr.mode & EVENTFS_SAVE_GID))
264 inode->i_gid = ei->attr.gid;
265}
266
267static int eventfs_get_attr(struct mnt_idmap *idmap,
268 const struct path *path, struct kstat *stat,
269 u32 request_mask, unsigned int flags)
270{
271 struct dentry *dentry = path->dentry;
272 struct inode *inode = d_backing_inode(dentry);
273
274 set_top_events_ownership(inode);
275
276 generic_fillattr(idmap, request_mask, inode, stat);
277 return 0;
278}
279
280static int eventfs_permission(struct mnt_idmap *idmap,
281 struct inode *inode, int mask)
282{
283 set_top_events_ownership(inode);
284 return generic_permission(idmap, inode, mask);
285}
286
287static const struct inode_operations eventfs_dir_inode_operations = {
288 .lookup = eventfs_root_lookup,
289 .setattr = eventfs_set_attr,
290 .getattr = eventfs_get_attr,
291 .permission = eventfs_permission,
292};
293
294static const struct inode_operations eventfs_file_inode_operations = {
295 .setattr = eventfs_set_attr,
296};
297
298static const struct file_operations eventfs_file_operations = {
299 .read = generic_read_dir,
300 .iterate_shared = eventfs_iterate,
301 .llseek = generic_file_llseek,
302};
303
304/*
305 * On a remount of tracefs, if UID or GID options are set, then
306 * the mount point inode permissions should be used.
307 * Reset the saved permission flags appropriately.
308 */
309void eventfs_remount(struct tracefs_inode *ti, bool update_uid, bool update_gid)
310{
311 struct eventfs_inode *ei = ti->private;
312
313 if (!ei)
314 return;
315
316 if (update_uid)
317 ei->attr.mode &= ~EVENTFS_SAVE_UID;
318
319 if (update_gid)
320 ei->attr.mode &= ~EVENTFS_SAVE_GID;
321
322 if (!ei->entry_attrs)
323 return;
324
325 for (int i = 0; i < ei->nr_entries; i++) {
326 if (update_uid)
327 ei->entry_attrs[i].mode &= ~EVENTFS_SAVE_UID;
328 if (update_gid)
329 ei->entry_attrs[i].mode &= ~EVENTFS_SAVE_GID;
330 }
331}
332
333/* Return the evenfs_inode of the "events" directory */
334static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
335{
336 struct eventfs_inode *ei;
337
338 do {
339 // The parent is stable because we do not do renames
340 dentry = dentry->d_parent;
341 // ... and directories always have d_fsdata
342 ei = dentry->d_fsdata;
343
344 /*
345 * If the ei is being freed, the ownership of the children
346 * doesn't matter.
347 */
348 if (ei->is_freed) {
349 ei = NULL;
350 break;
351 }
352 // Walk upwards until you find the events inode
353 } while (!ei->is_events);
354
355 update_events_attr(ei, dentry->d_sb);
356
357 return ei;
358}
359
360static void update_inode_attr(struct dentry *dentry, struct inode *inode,
361 struct eventfs_attr *attr, umode_t mode)
362{
363 struct eventfs_inode *events_ei = eventfs_find_events(dentry);
364
365 if (!events_ei)
366 return;
367
368 inode->i_mode = mode;
369 inode->i_uid = events_ei->attr.uid;
370 inode->i_gid = events_ei->attr.gid;
371
372 if (!attr)
373 return;
374
375 if (attr->mode & EVENTFS_SAVE_MODE)
376 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
377
378 if (attr->mode & EVENTFS_SAVE_UID)
379 inode->i_uid = attr->uid;
380
381 if (attr->mode & EVENTFS_SAVE_GID)
382 inode->i_gid = attr->gid;
383}
384
385/**
386 * lookup_file - look up a file in the tracefs filesystem
387 * @parent_ei: Pointer to the eventfs_inode that represents parent of the file
388 * @dentry: the dentry to look up
389 * @mode: the permission that the file should have.
390 * @attr: saved attributes changed by user
391 * @data: something that the caller will want to get to later on.
392 * @fop: struct file_operations that should be used for this file.
393 *
394 * This function creates a dentry that represents a file in the eventsfs_inode
395 * directory. The inode.i_private pointer will point to @data in the open()
396 * call.
397 */
398static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
399 struct dentry *dentry,
400 umode_t mode,
401 struct eventfs_attr *attr,
402 void *data,
403 const struct file_operations *fop)
404{
405 struct tracefs_inode *ti;
406 struct inode *inode;
407
408 if (!(mode & S_IFMT))
409 mode |= S_IFREG;
410
411 if (WARN_ON_ONCE(!S_ISREG(mode)))
412 return ERR_PTR(-EIO);
413
414 inode = tracefs_get_inode(dentry->d_sb);
415 if (unlikely(!inode))
416 return ERR_PTR(-ENOMEM);
417
418 /* If the user updated the directory's attributes, use them */
419 update_inode_attr(dentry, inode, attr, mode);
420
421 inode->i_op = &eventfs_file_inode_operations;
422 inode->i_fop = fop;
423 inode->i_private = data;
424
425 /* All files will have the same inode number */
426 inode->i_ino = EVENTFS_FILE_INODE_INO;
427
428 ti = get_tracefs(inode);
429 ti->flags |= TRACEFS_EVENT_INODE;
430
431 // Files have their parent's ei as their fsdata
432 dentry->d_fsdata = get_ei(parent_ei);
433
434 d_add(dentry, inode);
435 return NULL;
436};
437
438/**
439 * lookup_dir_entry - look up a dir in the tracefs filesystem
440 * @dentry: the directory to look up
441 * @pei: Pointer to the parent eventfs_inode if available
442 * @ei: the eventfs_inode that represents the directory to create
443 *
444 * This function will look up a dentry for a directory represented by
445 * a eventfs_inode.
446 */
447static struct dentry *lookup_dir_entry(struct dentry *dentry,
448 struct eventfs_inode *pei, struct eventfs_inode *ei)
449{
450 struct tracefs_inode *ti;
451 struct inode *inode;
452
453 inode = tracefs_get_inode(dentry->d_sb);
454 if (unlikely(!inode))
455 return ERR_PTR(-ENOMEM);
456
457 /* If the user updated the directory's attributes, use them */
458 update_inode_attr(dentry, inode, &ei->attr,
459 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
460
461 inode->i_op = &eventfs_dir_inode_operations;
462 inode->i_fop = &eventfs_file_operations;
463
464 /* All directories will have the same inode number */
465 inode->i_ino = eventfs_dir_ino(ei);
466
467 ti = get_tracefs(inode);
468 ti->flags |= TRACEFS_EVENT_INODE;
469 /* Only directories have ti->private set to an ei, not files */
470 ti->private = ei;
471
472 dentry->d_fsdata = get_ei(ei);
473
474 d_add(dentry, inode);
475 return NULL;
476}
477
478static inline struct eventfs_inode *init_ei(struct eventfs_inode *ei, const char *name)
479{
480 ei->name = kstrdup_const(name, GFP_KERNEL);
481 if (!ei->name)
482 return NULL;
483 kref_init(&ei->kref);
484 return ei;
485}
486
487static inline struct eventfs_inode *alloc_ei(const char *name)
488{
489 struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
490 struct eventfs_inode *result;
491
492 if (!ei)
493 return NULL;
494
495 result = init_ei(ei, name);
496 if (!result)
497 kfree(ei);
498
499 return result;
500}
501
502static inline struct eventfs_inode *alloc_root_ei(const char *name)
503{
504 struct eventfs_root_inode *rei = kzalloc(sizeof(*rei), GFP_KERNEL);
505 struct eventfs_inode *ei;
506
507 if (!rei)
508 return NULL;
509
510 rei->ei.is_events = 1;
511 ei = init_ei(&rei->ei, name);
512 if (!ei)
513 kfree(rei);
514
515 return ei;
516}
517
518/**
519 * eventfs_d_release - dentry is going away
520 * @dentry: dentry which has the reference to remove.
521 *
522 * Remove the association between a dentry from an eventfs_inode.
523 */
524void eventfs_d_release(struct dentry *dentry)
525{
526 put_ei(dentry->d_fsdata);
527}
528
529/**
530 * lookup_file_dentry - create a dentry for a file of an eventfs_inode
531 * @dentry: The parent dentry under which the new file's dentry will be created
532 * @ei: the eventfs_inode that the file will be created under
533 * @idx: the index into the entry_attrs[] of the @ei
534 * @mode: The mode of the file.
535 * @data: The data to use to set the inode of the file with on open()
536 * @fops: The fops of the file to be created.
537 *
538 * This function creates a dentry for a file associated with an
539 * eventfs_inode @ei. It uses the entry attributes specified by @idx,
540 * if available. The file will have the specified @mode and its inode will be
541 * set up with @data upon open. The file operations will be set to @fops.
542 *
543 * Return: Returns a pointer to the newly created file's dentry or an error
544 * pointer.
545 */
546static struct dentry *
547lookup_file_dentry(struct dentry *dentry,
548 struct eventfs_inode *ei, int idx,
549 umode_t mode, void *data,
550 const struct file_operations *fops)
551{
552 struct eventfs_attr *attr = NULL;
553
554 if (ei->entry_attrs)
555 attr = &ei->entry_attrs[idx];
556
557 return lookup_file(ei, dentry, mode, attr, data, fops);
558}
559
560/**
561 * eventfs_root_lookup - lookup routine to create file/dir
562 * @dir: in which a lookup is being done
563 * @dentry: file/dir dentry
564 * @flags: Just passed to simple_lookup()
565 *
566 * Used to create dynamic file/dir with-in @dir, search with-in @ei
567 * list, if @dentry found go ahead and create the file/dir
568 */
569
570static struct dentry *eventfs_root_lookup(struct inode *dir,
571 struct dentry *dentry,
572 unsigned int flags)
573{
574 struct eventfs_inode *ei_child;
575 struct tracefs_inode *ti;
576 struct eventfs_inode *ei;
577 const char *name = dentry->d_name.name;
578 struct dentry *result = NULL;
579
580 ti = get_tracefs(dir);
581 if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
582 return ERR_PTR(-EIO);
583
584 mutex_lock(&eventfs_mutex);
585
586 ei = ti->private;
587 if (!ei || ei->is_freed)
588 goto out;
589
590 list_for_each_entry(ei_child, &ei->children, list) {
591 if (strcmp(ei_child->name, name) != 0)
592 continue;
593 /* A child is freed and removed from the list at the same time */
594 if (WARN_ON_ONCE(ei_child->is_freed))
595 goto out;
596 result = lookup_dir_entry(dentry, ei, ei_child);
597 goto out;
598 }
599
600 for (int i = 0; i < ei->nr_entries; i++) {
601 void *data;
602 umode_t mode;
603 const struct file_operations *fops;
604 const struct eventfs_entry *entry = &ei->entries[i];
605
606 if (strcmp(name, entry->name) != 0)
607 continue;
608
609 data = ei->data;
610 if (entry->callback(name, &mode, &data, &fops) <= 0)
611 goto out;
612
613 result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
614 goto out;
615 }
616 out:
617 mutex_unlock(&eventfs_mutex);
618 return result;
619}
620
621/*
622 * Walk the children of a eventfs_inode to fill in getdents().
623 */
624static int eventfs_iterate(struct file *file, struct dir_context *ctx)
625{
626 const struct file_operations *fops;
627 struct inode *f_inode = file_inode(file);
628 const struct eventfs_entry *entry;
629 struct eventfs_inode *ei_child;
630 struct tracefs_inode *ti;
631 struct eventfs_inode *ei;
632 const char *name;
633 umode_t mode;
634 int idx;
635 int ret = -EINVAL;
636 int ino;
637 int i, r, c;
638
639 if (!dir_emit_dots(file, ctx))
640 return 0;
641
642 ti = get_tracefs(f_inode);
643 if (!(ti->flags & TRACEFS_EVENT_INODE))
644 return -EINVAL;
645
646 c = ctx->pos - 2;
647
648 idx = srcu_read_lock(&eventfs_srcu);
649
650 mutex_lock(&eventfs_mutex);
651 ei = READ_ONCE(ti->private);
652 if (ei && ei->is_freed)
653 ei = NULL;
654 mutex_unlock(&eventfs_mutex);
655
656 if (!ei)
657 goto out;
658
659 /*
660 * Need to create the dentries and inodes to have a consistent
661 * inode number.
662 */
663 ret = 0;
664
665 /* Start at 'c' to jump over already read entries */
666 for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
667 void *cdata = ei->data;
668
669 entry = &ei->entries[i];
670 name = entry->name;
671
672 mutex_lock(&eventfs_mutex);
673 /* If ei->is_freed then just bail here, nothing more to do */
674 if (ei->is_freed) {
675 mutex_unlock(&eventfs_mutex);
676 goto out;
677 }
678 r = entry->callback(name, &mode, &cdata, &fops);
679 mutex_unlock(&eventfs_mutex);
680 if (r <= 0)
681 continue;
682
683 ino = EVENTFS_FILE_INODE_INO;
684
685 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
686 goto out;
687 }
688
689 /* Subtract the skipped entries above */
690 c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
691
692 list_for_each_entry_srcu(ei_child, &ei->children, list,
693 srcu_read_lock_held(&eventfs_srcu)) {
694
695 if (c > 0) {
696 c--;
697 continue;
698 }
699
700 ctx->pos++;
701
702 if (ei_child->is_freed)
703 continue;
704
705 name = ei_child->name;
706
707 ino = eventfs_dir_ino(ei_child);
708
709 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
710 goto out_dec;
711 }
712 ret = 1;
713 out:
714 srcu_read_unlock(&eventfs_srcu, idx);
715
716 return ret;
717
718 out_dec:
719 /* Incremented ctx->pos without adding something, reset it */
720 ctx->pos--;
721 goto out;
722}
723
724/**
725 * eventfs_create_dir - Create the eventfs_inode for this directory
726 * @name: The name of the directory to create.
727 * @parent: The eventfs_inode of the parent directory.
728 * @entries: A list of entries that represent the files under this directory
729 * @size: The number of @entries
730 * @data: The default data to pass to the files (an entry may override it).
731 *
732 * This function creates the descriptor to represent a directory in the
733 * eventfs. This descriptor is an eventfs_inode, and it is returned to be
734 * used to create other children underneath.
735 *
736 * The @entries is an array of eventfs_entry structures which has:
737 * const char *name
738 * eventfs_callback callback;
739 *
740 * The name is the name of the file, and the callback is a pointer to a function
741 * that will be called when the file is reference (either by lookup or by
742 * reading a directory). The callback is of the prototype:
743 *
744 * int callback(const char *name, umode_t *mode, void **data,
745 * const struct file_operations **fops);
746 *
747 * When a file needs to be created, this callback will be called with
748 * name = the name of the file being created (so that the same callback
749 * may be used for multiple files).
750 * mode = a place to set the file's mode
751 * data = A pointer to @data, and the callback may replace it, which will
752 * cause the file created to pass the new data to the open() call.
753 * fops = the fops to use for the created file.
754 *
755 * NB. @callback is called while holding internal locks of the eventfs
756 * system. The callback must not call any code that might also call into
757 * the tracefs or eventfs system or it will risk creating a deadlock.
758 */
759struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
760 const struct eventfs_entry *entries,
761 int size, void *data)
762{
763 struct eventfs_inode *ei;
764
765 if (!parent)
766 return ERR_PTR(-EINVAL);
767
768 ei = alloc_ei(name);
769 if (!ei)
770 return ERR_PTR(-ENOMEM);
771
772 ei->entries = entries;
773 ei->nr_entries = size;
774 ei->data = data;
775 INIT_LIST_HEAD(&ei->children);
776 INIT_LIST_HEAD(&ei->list);
777
778 mutex_lock(&eventfs_mutex);
779 if (!parent->is_freed)
780 list_add_tail(&ei->list, &parent->children);
781 mutex_unlock(&eventfs_mutex);
782
783 /* Was the parent freed? */
784 if (list_empty(&ei->list)) {
785 cleanup_ei(ei);
786 ei = NULL;
787 }
788 return ei;
789}
790
791/**
792 * eventfs_create_events_dir - create the top level events directory
793 * @name: The name of the top level directory to create.
794 * @parent: Parent dentry for this file in the tracefs directory.
795 * @entries: A list of entries that represent the files under this directory
796 * @size: The number of @entries
797 * @data: The default data to pass to the files (an entry may override it).
798 *
799 * This function creates the top of the trace event directory.
800 *
801 * See eventfs_create_dir() for use of @entries.
802 */
803struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
804 const struct eventfs_entry *entries,
805 int size, void *data)
806{
807 struct dentry *dentry = tracefs_start_creating(name, parent);
808 struct eventfs_root_inode *rei;
809 struct eventfs_inode *ei;
810 struct tracefs_inode *ti;
811 struct inode *inode;
812 kuid_t uid;
813 kgid_t gid;
814
815 if (security_locked_down(LOCKDOWN_TRACEFS))
816 return NULL;
817
818 if (IS_ERR(dentry))
819 return ERR_CAST(dentry);
820
821 ei = alloc_root_ei(name);
822 if (!ei)
823 goto fail;
824
825 inode = tracefs_get_inode(dentry->d_sb);
826 if (unlikely(!inode))
827 goto fail;
828
829 // Note: we have a ref to the dentry from tracefs_start_creating()
830 rei = get_root_inode(ei);
831 rei->events_dir = dentry;
832 rei->parent_inode = d_inode(dentry->d_sb->s_root);
833
834 ei->entries = entries;
835 ei->nr_entries = size;
836 ei->data = data;
837
838 /* Save the ownership of this directory */
839 uid = d_inode(dentry->d_parent)->i_uid;
840 gid = d_inode(dentry->d_parent)->i_gid;
841
842 ei->attr.uid = uid;
843 ei->attr.gid = gid;
844
845 /*
846 * When the "events" directory is created, it takes on the
847 * permissions of its parent. But can be reset on remount.
848 */
849 ei->attr.mode |= EVENTFS_SAVE_UID | EVENTFS_SAVE_GID;
850
851 INIT_LIST_HEAD(&ei->children);
852 INIT_LIST_HEAD(&ei->list);
853
854 ti = get_tracefs(inode);
855 ti->flags |= TRACEFS_EVENT_INODE;
856 ti->private = ei;
857
858 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
859 inode->i_uid = uid;
860 inode->i_gid = gid;
861 inode->i_op = &eventfs_dir_inode_operations;
862 inode->i_fop = &eventfs_file_operations;
863
864 dentry->d_fsdata = get_ei(ei);
865
866 /*
867 * Keep all eventfs directories with i_nlink == 1.
868 * Due to the dynamic nature of the dentry creations and not
869 * wanting to add a pointer to the parent eventfs_inode in the
870 * eventfs_inode structure, keeping the i_nlink in sync with the
871 * number of directories would cause too much complexity for
872 * something not worth much. Keeping directory links at 1
873 * tells userspace not to trust the link number.
874 */
875 d_instantiate(dentry, inode);
876 /* The dentry of the "events" parent does keep track though */
877 inc_nlink(dentry->d_parent->d_inode);
878 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
879 tracefs_end_creating(dentry);
880
881 return ei;
882
883 fail:
884 cleanup_ei(ei);
885 tracefs_failed_creating(dentry);
886 return ERR_PTR(-ENOMEM);
887}
888
889/**
890 * eventfs_remove_rec - remove eventfs dir or file from list
891 * @ei: eventfs_inode to be removed.
892 * @level: prevent recursion from going more than 3 levels deep.
893 *
894 * This function recursively removes eventfs_inodes which
895 * contains info of files and/or directories.
896 */
897static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
898{
899 struct eventfs_inode *ei_child;
900
901 /*
902 * Check recursion depth. It should never be greater than 3:
903 * 0 - events/
904 * 1 - events/group/
905 * 2 - events/group/event/
906 * 3 - events/group/event/file
907 */
908 if (WARN_ON_ONCE(level > 3))
909 return;
910
911 /* search for nested folders or files */
912 list_for_each_entry(ei_child, &ei->children, list)
913 eventfs_remove_rec(ei_child, level + 1);
914
915 list_del(&ei->list);
916 free_ei(ei);
917}
918
919/**
920 * eventfs_remove_dir - remove eventfs dir or file from list
921 * @ei: eventfs_inode to be removed.
922 *
923 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
924 */
925void eventfs_remove_dir(struct eventfs_inode *ei)
926{
927 if (!ei)
928 return;
929
930 mutex_lock(&eventfs_mutex);
931 eventfs_remove_rec(ei, 0);
932 mutex_unlock(&eventfs_mutex);
933}
934
935/**
936 * eventfs_remove_events_dir - remove the top level eventfs directory
937 * @ei: the event_inode returned by eventfs_create_events_dir().
938 *
939 * This function removes the events main directory
940 */
941void eventfs_remove_events_dir(struct eventfs_inode *ei)
942{
943 struct eventfs_root_inode *rei;
944 struct dentry *dentry;
945
946 rei = get_root_inode(ei);
947 dentry = rei->events_dir;
948 if (!dentry)
949 return;
950
951 rei->events_dir = NULL;
952 eventfs_remove_dir(ei);
953
954 /*
955 * Matches the dget() done by tracefs_start_creating()
956 * in eventfs_create_events_dir() when it the dentry was
957 * created. In other words, it's a normal dentry that
958 * sticks around while the other ei->dentry are created
959 * and destroyed dynamically.
960 */
961 d_invalidate(dentry);
962 dput(dentry);
963}