e9819d719d2ac4b8b6057a44a038355287e675e4
[linux-block.git] / fs / tracefs / event_inode.c
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  */
33 static 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 */
39 static 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  */
53 DEFINE_STATIC_SRCU(eventfs_srcu);
54
55 /* Mode is unsigned short, use the upper bits for flags */
56 enum {
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 static struct dentry *eventfs_root_lookup(struct inode *dir,
66                                           struct dentry *dentry,
67                                           unsigned int flags);
68 static int eventfs_iterate(struct file *file, struct dir_context *ctx);
69
70 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
71 {
72         unsigned int ia_valid = iattr->ia_valid;
73
74         if (ia_valid & ATTR_MODE) {
75                 attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
76                         (iattr->ia_mode & EVENTFS_MODE_MASK) |
77                         EVENTFS_SAVE_MODE;
78         }
79         if (ia_valid & ATTR_UID) {
80                 attr->mode |= EVENTFS_SAVE_UID;
81                 attr->uid = iattr->ia_uid;
82         }
83         if (ia_valid & ATTR_GID) {
84                 attr->mode |= EVENTFS_SAVE_GID;
85                 attr->gid = iattr->ia_gid;
86         }
87 }
88
89 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
90                             struct iattr *iattr)
91 {
92         const struct eventfs_entry *entry;
93         struct eventfs_inode *ei;
94         const char *name;
95         int ret;
96
97         mutex_lock(&eventfs_mutex);
98         ei = dentry->d_fsdata;
99         if (ei->is_freed) {
100                 /* Do not allow changes if the event is about to be removed. */
101                 mutex_unlock(&eventfs_mutex);
102                 return -ENODEV;
103         }
104
105         /* Preallocate the children mode array if necessary */
106         if (!(dentry->d_inode->i_mode & S_IFDIR)) {
107                 if (!ei->entry_attrs) {
108                         ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
109                                                   GFP_NOFS);
110                         if (!ei->entry_attrs) {
111                                 ret = -ENOMEM;
112                                 goto out;
113                         }
114                 }
115         }
116
117         ret = simple_setattr(idmap, dentry, iattr);
118         if (ret < 0)
119                 goto out;
120
121         /*
122          * If this is a dir, then update the ei cache, only the file
123          * mode is saved in the ei->m_children, and the ownership is
124          * determined by the parent directory.
125          */
126         if (dentry->d_inode->i_mode & S_IFDIR) {
127                 /*
128                  * The events directory dentry is never freed, unless its
129                  * part of an instance that is deleted. It's attr is the
130                  * default for its child files and directories.
131                  * Do not update it. It's not used for its own mode or ownership.
132                  */
133                 if (ei->is_events) {
134                         /* But it still needs to know if it was modified */
135                         if (iattr->ia_valid & ATTR_UID)
136                                 ei->attr.mode |= EVENTFS_SAVE_UID;
137                         if (iattr->ia_valid & ATTR_GID)
138                                 ei->attr.mode |= EVENTFS_SAVE_GID;
139                 } else {
140                         update_attr(&ei->attr, iattr);
141                 }
142
143         } else {
144                 name = dentry->d_name.name;
145
146                 for (int i = 0; i < ei->nr_entries; i++) {
147                         entry = &ei->entries[i];
148                         if (strcmp(name, entry->name) == 0) {
149                                 update_attr(&ei->entry_attrs[i], iattr);
150                                 break;
151                         }
152                 }
153         }
154  out:
155         mutex_unlock(&eventfs_mutex);
156         return ret;
157 }
158
159 static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
160 {
161         struct inode *root;
162
163         /* Only update if the "events" was on the top level */
164         if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
165                 return;
166
167         /* Get the tracefs root inode. */
168         root = d_inode(sb->s_root);
169         ei->attr.uid = root->i_uid;
170         ei->attr.gid = root->i_gid;
171 }
172
173 static void set_top_events_ownership(struct inode *inode)
174 {
175         struct tracefs_inode *ti = get_tracefs(inode);
176         struct eventfs_inode *ei = ti->private;
177
178         /* The top events directory doesn't get automatically updated */
179         if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
180                 return;
181
182         update_top_events_attr(ei, inode->i_sb);
183
184         if (!(ei->attr.mode & EVENTFS_SAVE_UID))
185                 inode->i_uid = ei->attr.uid;
186
187         if (!(ei->attr.mode & EVENTFS_SAVE_GID))
188                 inode->i_gid = ei->attr.gid;
189 }
190
191 static int eventfs_get_attr(struct mnt_idmap *idmap,
192                             const struct path *path, struct kstat *stat,
193                             u32 request_mask, unsigned int flags)
194 {
195         struct dentry *dentry = path->dentry;
196         struct inode *inode = d_backing_inode(dentry);
197
198         set_top_events_ownership(inode);
199
200         generic_fillattr(idmap, request_mask, inode, stat);
201         return 0;
202 }
203
204 static int eventfs_permission(struct mnt_idmap *idmap,
205                               struct inode *inode, int mask)
206 {
207         set_top_events_ownership(inode);
208         return generic_permission(idmap, inode, mask);
209 }
210
211 static const struct inode_operations eventfs_root_dir_inode_operations = {
212         .lookup         = eventfs_root_lookup,
213         .setattr        = eventfs_set_attr,
214         .getattr        = eventfs_get_attr,
215         .permission     = eventfs_permission,
216 };
217
218 static const struct inode_operations eventfs_file_inode_operations = {
219         .setattr        = eventfs_set_attr,
220 };
221
222 static const struct file_operations eventfs_file_operations = {
223         .read           = generic_read_dir,
224         .iterate_shared = eventfs_iterate,
225         .llseek         = generic_file_llseek,
226 };
227
228 /* Return the evenfs_inode of the "events" directory */
229 static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
230 {
231         struct eventfs_inode *ei;
232
233         mutex_lock(&eventfs_mutex);
234         do {
235                 // The parent is stable because we do not do renames
236                 dentry = dentry->d_parent;
237                 // ... and directories always have d_fsdata
238                 ei = dentry->d_fsdata;
239
240                 /*
241                  * If the ei is being freed, the ownership of the children
242                  * doesn't matter.
243                  */
244                 if (ei->is_freed) {
245                         ei = NULL;
246                         break;
247                 }
248                 // Walk upwards until you find the events inode
249         } while (!ei->is_events);
250         mutex_unlock(&eventfs_mutex);
251
252         update_top_events_attr(ei, dentry->d_sb);
253
254         return ei;
255 }
256
257 static void update_inode_attr(struct dentry *dentry, struct inode *inode,
258                               struct eventfs_attr *attr, umode_t mode)
259 {
260         struct eventfs_inode *events_ei = eventfs_find_events(dentry);
261
262         if (!events_ei)
263                 return;
264
265         inode->i_mode = mode;
266         inode->i_uid = events_ei->attr.uid;
267         inode->i_gid = events_ei->attr.gid;
268
269         if (!attr)
270                 return;
271
272         if (attr->mode & EVENTFS_SAVE_MODE)
273                 inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
274
275         if (attr->mode & EVENTFS_SAVE_UID)
276                 inode->i_uid = attr->uid;
277
278         if (attr->mode & EVENTFS_SAVE_GID)
279                 inode->i_gid = attr->gid;
280 }
281
282 /**
283  * create_file - create a file in the tracefs filesystem
284  * @name: the name of the file to create.
285  * @mode: the permission that the file should have.
286  * @attr: saved attributes changed by user
287  * @parent: parent dentry for this file.
288  * @data: something that the caller will want to get to later on.
289  * @fop: struct file_operations that should be used for this file.
290  *
291  * This function creates a dentry that represents a file in the eventsfs_inode
292  * directory. The inode.i_private pointer will point to @data in the open()
293  * call.
294  */
295 static struct dentry *create_file(const char *name, umode_t mode,
296                                   struct eventfs_attr *attr,
297                                   struct dentry *parent, void *data,
298                                   const struct file_operations *fop)
299 {
300         struct tracefs_inode *ti;
301         struct dentry *dentry;
302         struct inode *inode;
303
304         if (!(mode & S_IFMT))
305                 mode |= S_IFREG;
306
307         if (WARN_ON_ONCE(!S_ISREG(mode)))
308                 return NULL;
309
310         WARN_ON_ONCE(!parent);
311         dentry = eventfs_start_creating(name, parent);
312
313         if (IS_ERR(dentry))
314                 return dentry;
315
316         inode = tracefs_get_inode(dentry->d_sb);
317         if (unlikely(!inode))
318                 return eventfs_failed_creating(dentry);
319
320         /* If the user updated the directory's attributes, use them */
321         update_inode_attr(dentry, inode, attr, mode);
322
323         inode->i_op = &eventfs_file_inode_operations;
324         inode->i_fop = fop;
325         inode->i_private = data;
326
327         /* All files will have the same inode number */
328         inode->i_ino = EVENTFS_FILE_INODE_INO;
329
330         ti = get_tracefs(inode);
331         ti->flags |= TRACEFS_EVENT_INODE;
332         d_instantiate(dentry, inode);
333         fsnotify_create(dentry->d_parent->d_inode, dentry);
334         return eventfs_end_creating(dentry);
335 };
336
337 /**
338  * create_dir - create a dir in the tracefs filesystem
339  * @ei: the eventfs_inode that represents the directory to create
340  * @parent: parent dentry for this file.
341  *
342  * This function will create a dentry for a directory represented by
343  * a eventfs_inode.
344  */
345 static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent)
346 {
347         struct tracefs_inode *ti;
348         struct dentry *dentry;
349         struct inode *inode;
350
351         dentry = eventfs_start_creating(ei->name, parent);
352         if (IS_ERR(dentry))
353                 return dentry;
354
355         inode = tracefs_get_inode(dentry->d_sb);
356         if (unlikely(!inode))
357                 return eventfs_failed_creating(dentry);
358
359         /* If the user updated the directory's attributes, use them */
360         update_inode_attr(dentry, inode, &ei->attr,
361                           S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
362
363         inode->i_op = &eventfs_root_dir_inode_operations;
364         inode->i_fop = &eventfs_file_operations;
365
366         /* All directories will have the same inode number */
367         inode->i_ino = eventfs_dir_ino(ei);
368
369         ti = get_tracefs(inode);
370         ti->flags |= TRACEFS_EVENT_INODE;
371         /* Only directories have ti->private set to an ei, not files */
372         ti->private = ei;
373
374         inc_nlink(inode);
375         d_instantiate(dentry, inode);
376         inc_nlink(dentry->d_parent->d_inode);
377         fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
378         return eventfs_end_creating(dentry);
379 }
380
381 static void free_ei(struct eventfs_inode *ei)
382 {
383         kfree_const(ei->name);
384         kfree(ei->d_children);
385         kfree(ei->entry_attrs);
386         kfree(ei);
387 }
388
389 /**
390  * eventfs_set_ei_status_free - remove the dentry reference from an eventfs_inode
391  * @ti: the tracefs_inode of the dentry
392  * @dentry: dentry which has the reference to remove.
393  *
394  * Remove the association between a dentry from an eventfs_inode.
395  */
396 void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry)
397 {
398         struct eventfs_inode *ei;
399         int i;
400
401         mutex_lock(&eventfs_mutex);
402
403         ei = dentry->d_fsdata;
404         if (!ei)
405                 goto out;
406
407         /* This could belong to one of the files of the ei */
408         if (ei->dentry != dentry) {
409                 for (i = 0; i < ei->nr_entries; i++) {
410                         if (ei->d_children[i] == dentry)
411                                 break;
412                 }
413                 if (WARN_ON_ONCE(i == ei->nr_entries))
414                         goto out;
415                 ei->d_children[i] = NULL;
416         } else if (ei->is_freed) {
417                 free_ei(ei);
418         } else {
419                 ei->dentry = NULL;
420         }
421
422         dentry->d_fsdata = NULL;
423  out:
424         mutex_unlock(&eventfs_mutex);
425 }
426
427 /**
428  * create_file_dentry - create a dentry for a file of an eventfs_inode
429  * @ei: the eventfs_inode that the file will be created under
430  * @idx: the index into the d_children[] of the @ei
431  * @parent: The parent dentry of the created file.
432  * @name: The name of the file to create
433  * @mode: The mode of the file.
434  * @data: The data to use to set the inode of the file with on open()
435  * @fops: The fops of the file to be created.
436  *
437  * Create a dentry for a file of an eventfs_inode @ei and place it into the
438  * address located at @e_dentry.
439  */
440 static struct dentry *
441 create_file_dentry(struct eventfs_inode *ei, int idx,
442                    struct dentry *parent, const char *name, umode_t mode, void *data,
443                    const struct file_operations *fops)
444 {
445         struct eventfs_attr *attr = NULL;
446         struct dentry **e_dentry = &ei->d_children[idx];
447         struct dentry *dentry;
448
449         WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
450
451         mutex_lock(&eventfs_mutex);
452         if (ei->is_freed) {
453                 mutex_unlock(&eventfs_mutex);
454                 return NULL;
455         }
456         /* If the e_dentry already has a dentry, use it */
457         if (*e_dentry) {
458                 dget(*e_dentry);
459                 mutex_unlock(&eventfs_mutex);
460                 return *e_dentry;
461         }
462
463         /* ei->entry_attrs are protected by SRCU */
464         if (ei->entry_attrs)
465                 attr = &ei->entry_attrs[idx];
466
467         mutex_unlock(&eventfs_mutex);
468
469         dentry = create_file(name, mode, attr, parent, data, fops);
470
471         mutex_lock(&eventfs_mutex);
472
473         if (IS_ERR_OR_NULL(dentry)) {
474                 /*
475                  * When the mutex was released, something else could have
476                  * created the dentry for this e_dentry. In which case
477                  * use that one.
478                  *
479                  * If ei->is_freed is set, the e_dentry is currently on its
480                  * way to being freed, don't return it. If e_dentry is NULL
481                  * it means it was already freed.
482                  */
483                 if (ei->is_freed) {
484                         dentry = NULL;
485                 } else {
486                         dentry = *e_dentry;
487                         dget(dentry);
488                 }
489                 mutex_unlock(&eventfs_mutex);
490                 return dentry;
491         }
492
493         if (!*e_dentry && !ei->is_freed) {
494                 *e_dentry = dentry;
495                 dentry->d_fsdata = ei;
496         } else {
497                 /*
498                  * Should never happen unless we get here due to being freed.
499                  * Otherwise it means two dentries exist with the same name.
500                  */
501                 WARN_ON_ONCE(!ei->is_freed);
502                 dentry = NULL;
503         }
504         mutex_unlock(&eventfs_mutex);
505
506         return dentry;
507 }
508
509 /**
510  * eventfs_post_create_dir - post create dir routine
511  * @ei: eventfs_inode of recently created dir
512  *
513  * Map the meta-data of files within an eventfs dir to their parent dentry
514  */
515 static void eventfs_post_create_dir(struct eventfs_inode *ei)
516 {
517         struct eventfs_inode *ei_child;
518
519         lockdep_assert_held(&eventfs_mutex);
520
521         /* srcu lock already held */
522         /* fill parent-child relation */
523         list_for_each_entry_srcu(ei_child, &ei->children, list,
524                                  srcu_read_lock_held(&eventfs_srcu)) {
525                 ei_child->d_parent = ei->dentry;
526         }
527 }
528
529 /**
530  * create_dir_dentry - Create a directory dentry for the eventfs_inode
531  * @pei: The eventfs_inode parent of ei.
532  * @ei: The eventfs_inode to create the directory for
533  * @parent: The dentry of the parent of this directory
534  *
535  * This creates and attaches a directory dentry to the eventfs_inode @ei.
536  */
537 static struct dentry *
538 create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei,
539                   struct dentry *parent)
540 {
541         struct dentry *dentry = NULL;
542
543         WARN_ON_ONCE(!inode_is_locked(parent->d_inode));
544
545         mutex_lock(&eventfs_mutex);
546         if (pei->is_freed || ei->is_freed) {
547                 mutex_unlock(&eventfs_mutex);
548                 return NULL;
549         }
550         if (ei->dentry) {
551                 /* If the eventfs_inode already has a dentry, use it */
552                 dentry = ei->dentry;
553                 dget(dentry);
554                 mutex_unlock(&eventfs_mutex);
555                 return dentry;
556         }
557         mutex_unlock(&eventfs_mutex);
558
559         dentry = create_dir(ei, parent);
560
561         mutex_lock(&eventfs_mutex);
562
563         if (IS_ERR_OR_NULL(dentry) && !ei->is_freed) {
564                 /*
565                  * When the mutex was released, something else could have
566                  * created the dentry for this e_dentry. In which case
567                  * use that one.
568                  *
569                  * If ei->is_freed is set, the e_dentry is currently on its
570                  * way to being freed.
571                  */
572                 dentry = ei->dentry;
573                 if (dentry)
574                         dget(dentry);
575                 mutex_unlock(&eventfs_mutex);
576                 return dentry;
577         }
578
579         if (!ei->dentry && !ei->is_freed) {
580                 ei->dentry = dentry;
581                 eventfs_post_create_dir(ei);
582                 dentry->d_fsdata = ei;
583         } else {
584                 /*
585                  * Should never happen unless we get here due to being freed.
586                  * Otherwise it means two dentries exist with the same name.
587                  */
588                 WARN_ON_ONCE(!ei->is_freed);
589                 dentry = NULL;
590         }
591         mutex_unlock(&eventfs_mutex);
592
593         return dentry;
594 }
595
596 /**
597  * eventfs_root_lookup - lookup routine to create file/dir
598  * @dir: in which a lookup is being done
599  * @dentry: file/dir dentry
600  * @flags: Just passed to simple_lookup()
601  *
602  * Used to create dynamic file/dir with-in @dir, search with-in @ei
603  * list, if @dentry found go ahead and create the file/dir
604  */
605
606 static struct dentry *eventfs_root_lookup(struct inode *dir,
607                                           struct dentry *dentry,
608                                           unsigned int flags)
609 {
610         const struct file_operations *fops;
611         const struct eventfs_entry *entry;
612         struct eventfs_inode *ei_child;
613         struct tracefs_inode *ti;
614         struct eventfs_inode *ei;
615         struct dentry *ei_dentry = NULL;
616         struct dentry *ret = NULL;
617         struct dentry *d;
618         const char *name = dentry->d_name.name;
619         umode_t mode;
620         void *data;
621         int idx;
622         int i;
623         int r;
624
625         ti = get_tracefs(dir);
626         if (!(ti->flags & TRACEFS_EVENT_INODE))
627                 return NULL;
628
629         /* Grab srcu to prevent the ei from going away */
630         idx = srcu_read_lock(&eventfs_srcu);
631
632         /*
633          * Grab the eventfs_mutex to consistent value from ti->private.
634          * This s
635          */
636         mutex_lock(&eventfs_mutex);
637         ei = READ_ONCE(ti->private);
638         if (ei && !ei->is_freed)
639                 ei_dentry = READ_ONCE(ei->dentry);
640         mutex_unlock(&eventfs_mutex);
641
642         if (!ei || !ei_dentry)
643                 goto out;
644
645         data = ei->data;
646
647         list_for_each_entry_srcu(ei_child, &ei->children, list,
648                                  srcu_read_lock_held(&eventfs_srcu)) {
649                 if (strcmp(ei_child->name, name) != 0)
650                         continue;
651                 ret = simple_lookup(dir, dentry, flags);
652                 if (IS_ERR(ret))
653                         goto out;
654                 d = create_dir_dentry(ei, ei_child, ei_dentry);
655                 dput(d);
656                 goto out;
657         }
658
659         for (i = 0; i < ei->nr_entries; i++) {
660                 entry = &ei->entries[i];
661                 if (strcmp(name, entry->name) == 0) {
662                         void *cdata = data;
663                         mutex_lock(&eventfs_mutex);
664                         /* If ei->is_freed, then the event itself may be too */
665                         if (!ei->is_freed)
666                                 r = entry->callback(name, &mode, &cdata, &fops);
667                         else
668                                 r = -1;
669                         mutex_unlock(&eventfs_mutex);
670                         if (r <= 0)
671                                 continue;
672                         ret = simple_lookup(dir, dentry, flags);
673                         if (IS_ERR(ret))
674                                 goto out;
675                         d = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops);
676                         dput(d);
677                         break;
678                 }
679         }
680  out:
681         srcu_read_unlock(&eventfs_srcu, idx);
682         return ret;
683 }
684
685 /*
686  * Walk the children of a eventfs_inode to fill in getdents().
687  */
688 static int eventfs_iterate(struct file *file, struct dir_context *ctx)
689 {
690         const struct file_operations *fops;
691         struct inode *f_inode = file_inode(file);
692         const struct eventfs_entry *entry;
693         struct eventfs_inode *ei_child;
694         struct tracefs_inode *ti;
695         struct eventfs_inode *ei;
696         const char *name;
697         umode_t mode;
698         int idx;
699         int ret = -EINVAL;
700         int ino;
701         int i, r, c;
702
703         if (!dir_emit_dots(file, ctx))
704                 return 0;
705
706         ti = get_tracefs(f_inode);
707         if (!(ti->flags & TRACEFS_EVENT_INODE))
708                 return -EINVAL;
709
710         c = ctx->pos - 2;
711
712         idx = srcu_read_lock(&eventfs_srcu);
713
714         mutex_lock(&eventfs_mutex);
715         ei = READ_ONCE(ti->private);
716         if (ei && ei->is_freed)
717                 ei = NULL;
718         mutex_unlock(&eventfs_mutex);
719
720         if (!ei)
721                 goto out;
722
723         /*
724          * Need to create the dentries and inodes to have a consistent
725          * inode number.
726          */
727         ret = 0;
728
729         /* Start at 'c' to jump over already read entries */
730         for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
731                 void *cdata = ei->data;
732
733                 entry = &ei->entries[i];
734                 name = entry->name;
735
736                 mutex_lock(&eventfs_mutex);
737                 /* If ei->is_freed then just bail here, nothing more to do */
738                 if (ei->is_freed) {
739                         mutex_unlock(&eventfs_mutex);
740                         goto out;
741                 }
742                 r = entry->callback(name, &mode, &cdata, &fops);
743                 mutex_unlock(&eventfs_mutex);
744                 if (r <= 0)
745                         continue;
746
747                 ino = EVENTFS_FILE_INODE_INO;
748
749                 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
750                         goto out;
751         }
752
753         /* Subtract the skipped entries above */
754         c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
755
756         list_for_each_entry_srcu(ei_child, &ei->children, list,
757                                  srcu_read_lock_held(&eventfs_srcu)) {
758
759                 if (c > 0) {
760                         c--;
761                         continue;
762                 }
763
764                 ctx->pos++;
765
766                 if (ei_child->is_freed)
767                         continue;
768
769                 name = ei_child->name;
770
771                 ino = eventfs_dir_ino(ei_child);
772
773                 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
774                         goto out_dec;
775         }
776         ret = 1;
777  out:
778         srcu_read_unlock(&eventfs_srcu, idx);
779
780         return ret;
781
782  out_dec:
783         /* Incremented ctx->pos without adding something, reset it */
784         ctx->pos--;
785         goto out;
786 }
787
788 /**
789  * eventfs_create_dir - Create the eventfs_inode for this directory
790  * @name: The name of the directory to create.
791  * @parent: The eventfs_inode of the parent directory.
792  * @entries: A list of entries that represent the files under this directory
793  * @size: The number of @entries
794  * @data: The default data to pass to the files (an entry may override it).
795  *
796  * This function creates the descriptor to represent a directory in the
797  * eventfs. This descriptor is an eventfs_inode, and it is returned to be
798  * used to create other children underneath.
799  *
800  * The @entries is an array of eventfs_entry structures which has:
801  *      const char               *name
802  *      eventfs_callback        callback;
803  *
804  * The name is the name of the file, and the callback is a pointer to a function
805  * that will be called when the file is reference (either by lookup or by
806  * reading a directory). The callback is of the prototype:
807  *
808  *    int callback(const char *name, umode_t *mode, void **data,
809  *                 const struct file_operations **fops);
810  *
811  * When a file needs to be created, this callback will be called with
812  *   name = the name of the file being created (so that the same callback
813  *          may be used for multiple files).
814  *   mode = a place to set the file's mode
815  *   data = A pointer to @data, and the callback may replace it, which will
816  *         cause the file created to pass the new data to the open() call.
817  *   fops = the fops to use for the created file.
818  *
819  * NB. @callback is called while holding internal locks of the eventfs
820  *     system. The callback must not call any code that might also call into
821  *     the tracefs or eventfs system or it will risk creating a deadlock.
822  */
823 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
824                                          const struct eventfs_entry *entries,
825                                          int size, void *data)
826 {
827         struct eventfs_inode *ei;
828
829         if (!parent)
830                 return ERR_PTR(-EINVAL);
831
832         ei = kzalloc(sizeof(*ei), GFP_KERNEL);
833         if (!ei)
834                 return ERR_PTR(-ENOMEM);
835
836         ei->name = kstrdup_const(name, GFP_KERNEL);
837         if (!ei->name) {
838                 kfree(ei);
839                 return ERR_PTR(-ENOMEM);
840         }
841
842         if (size) {
843                 ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL);
844                 if (!ei->d_children) {
845                         kfree_const(ei->name);
846                         kfree(ei);
847                         return ERR_PTR(-ENOMEM);
848                 }
849         }
850
851         ei->entries = entries;
852         ei->nr_entries = size;
853         ei->data = data;
854         INIT_LIST_HEAD(&ei->children);
855         INIT_LIST_HEAD(&ei->list);
856
857         mutex_lock(&eventfs_mutex);
858         if (!parent->is_freed) {
859                 list_add_tail(&ei->list, &parent->children);
860                 ei->d_parent = parent->dentry;
861         }
862         mutex_unlock(&eventfs_mutex);
863
864         /* Was the parent freed? */
865         if (list_empty(&ei->list)) {
866                 free_ei(ei);
867                 ei = NULL;
868         }
869         return ei;
870 }
871
872 /**
873  * eventfs_create_events_dir - create the top level events directory
874  * @name: The name of the top level directory to create.
875  * @parent: Parent dentry for this file in the tracefs directory.
876  * @entries: A list of entries that represent the files under this directory
877  * @size: The number of @entries
878  * @data: The default data to pass to the files (an entry may override it).
879  *
880  * This function creates the top of the trace event directory.
881  *
882  * See eventfs_create_dir() for use of @entries.
883  */
884 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
885                                                 const struct eventfs_entry *entries,
886                                                 int size, void *data)
887 {
888         struct dentry *dentry = tracefs_start_creating(name, parent);
889         struct eventfs_inode *ei;
890         struct tracefs_inode *ti;
891         struct inode *inode;
892         kuid_t uid;
893         kgid_t gid;
894
895         if (security_locked_down(LOCKDOWN_TRACEFS))
896                 return NULL;
897
898         if (IS_ERR(dentry))
899                 return ERR_CAST(dentry);
900
901         ei = kzalloc(sizeof(*ei), GFP_KERNEL);
902         if (!ei)
903                 goto fail_ei;
904
905         inode = tracefs_get_inode(dentry->d_sb);
906         if (unlikely(!inode))
907                 goto fail;
908
909         if (size) {
910                 ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL);
911                 if (!ei->d_children)
912                         goto fail;
913         }
914
915         ei->dentry = dentry;
916         ei->entries = entries;
917         ei->nr_entries = size;
918         ei->is_events = 1;
919         ei->data = data;
920         ei->name = kstrdup_const(name, GFP_KERNEL);
921         if (!ei->name)
922                 goto fail;
923
924         /* Save the ownership of this directory */
925         uid = d_inode(dentry->d_parent)->i_uid;
926         gid = d_inode(dentry->d_parent)->i_gid;
927
928         /*
929          * If the events directory is of the top instance, then parent
930          * is NULL. Set the attr.mode to reflect this and its permissions will
931          * default to the tracefs root dentry.
932          */
933         if (!parent)
934                 ei->attr.mode = EVENTFS_TOPLEVEL;
935
936         /* This is used as the default ownership of the files and directories */
937         ei->attr.uid = uid;
938         ei->attr.gid = gid;
939
940         INIT_LIST_HEAD(&ei->children);
941         INIT_LIST_HEAD(&ei->list);
942
943         ti = get_tracefs(inode);
944         ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE;
945         ti->private = ei;
946
947         inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
948         inode->i_uid = uid;
949         inode->i_gid = gid;
950         inode->i_op = &eventfs_root_dir_inode_operations;
951         inode->i_fop = &eventfs_file_operations;
952
953         dentry->d_fsdata = ei;
954
955         /* directory inodes start off with i_nlink == 2 (for "." entry) */
956         inc_nlink(inode);
957         d_instantiate(dentry, inode);
958         inc_nlink(dentry->d_parent->d_inode);
959         fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
960         tracefs_end_creating(dentry);
961
962         return ei;
963
964  fail:
965         kfree(ei->d_children);
966         kfree(ei);
967  fail_ei:
968         tracefs_failed_creating(dentry);
969         return ERR_PTR(-ENOMEM);
970 }
971
972 static LLIST_HEAD(free_list);
973
974 static void eventfs_workfn(struct work_struct *work)
975 {
976         struct eventfs_inode *ei, *tmp;
977         struct llist_node *llnode;
978
979         llnode = llist_del_all(&free_list);
980         llist_for_each_entry_safe(ei, tmp, llnode, llist) {
981                 /* This dput() matches the dget() from unhook_dentry() */
982                 for (int i = 0; i < ei->nr_entries; i++) {
983                         if (ei->d_children[i])
984                                 dput(ei->d_children[i]);
985                 }
986                 /* This should only get here if it had a dentry */
987                 if (!WARN_ON_ONCE(!ei->dentry))
988                         dput(ei->dentry);
989         }
990 }
991
992 static DECLARE_WORK(eventfs_work, eventfs_workfn);
993
994 static void free_rcu_ei(struct rcu_head *head)
995 {
996         struct eventfs_inode *ei = container_of(head, struct eventfs_inode, rcu);
997
998         if (ei->dentry) {
999                 /* Do not free the ei until all references of dentry are gone */
1000                 if (llist_add(&ei->llist, &free_list))
1001                         queue_work(system_unbound_wq, &eventfs_work);
1002                 return;
1003         }
1004
1005         /* If the ei doesn't have a dentry, neither should its children */
1006         for (int i = 0; i < ei->nr_entries; i++) {
1007                 WARN_ON_ONCE(ei->d_children[i]);
1008         }
1009
1010         free_ei(ei);
1011 }
1012
1013 static void unhook_dentry(struct dentry *dentry)
1014 {
1015         if (!dentry)
1016                 return;
1017         /*
1018          * Need to add a reference to the dentry that is expected by
1019          * simple_recursive_removal(), which will include a dput().
1020          */
1021         dget(dentry);
1022
1023         /*
1024          * Also add a reference for the dput() in eventfs_workfn().
1025          * That is required as that dput() will free the ei after
1026          * the SRCU grace period is over.
1027          */
1028         dget(dentry);
1029 }
1030
1031 /**
1032  * eventfs_remove_rec - remove eventfs dir or file from list
1033  * @ei: eventfs_inode to be removed.
1034  * @level: prevent recursion from going more than 3 levels deep.
1035  *
1036  * This function recursively removes eventfs_inodes which
1037  * contains info of files and/or directories.
1038  */
1039 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
1040 {
1041         struct eventfs_inode *ei_child;
1042
1043         if (!ei)
1044                 return;
1045         /*
1046          * Check recursion depth. It should never be greater than 3:
1047          * 0 - events/
1048          * 1 - events/group/
1049          * 2 - events/group/event/
1050          * 3 - events/group/event/file
1051          */
1052         if (WARN_ON_ONCE(level > 3))
1053                 return;
1054
1055         /* search for nested folders or files */
1056         list_for_each_entry_srcu(ei_child, &ei->children, list,
1057                                  lockdep_is_held(&eventfs_mutex)) {
1058                 /* Children only have dentry if parent does */
1059                 WARN_ON_ONCE(ei_child->dentry && !ei->dentry);
1060                 eventfs_remove_rec(ei_child, level + 1);
1061         }
1062
1063
1064         ei->is_freed = 1;
1065
1066         for (int i = 0; i < ei->nr_entries; i++) {
1067                 if (ei->d_children[i]) {
1068                         /* Children only have dentry if parent does */
1069                         WARN_ON_ONCE(!ei->dentry);
1070                         unhook_dentry(ei->d_children[i]);
1071                 }
1072         }
1073
1074         unhook_dentry(ei->dentry);
1075
1076         list_del_rcu(&ei->list);
1077         call_srcu(&eventfs_srcu, &ei->rcu, free_rcu_ei);
1078 }
1079
1080 /**
1081  * eventfs_remove_dir - remove eventfs dir or file from list
1082  * @ei: eventfs_inode to be removed.
1083  *
1084  * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
1085  */
1086 void eventfs_remove_dir(struct eventfs_inode *ei)
1087 {
1088         struct dentry *dentry;
1089
1090         if (!ei)
1091                 return;
1092
1093         mutex_lock(&eventfs_mutex);
1094         dentry = ei->dentry;
1095         eventfs_remove_rec(ei, 0);
1096         mutex_unlock(&eventfs_mutex);
1097
1098         /*
1099          * If any of the ei children has a dentry, then the ei itself
1100          * must have a dentry.
1101          */
1102         if (dentry)
1103                 simple_recursive_removal(dentry, NULL);
1104 }
1105
1106 /**
1107  * eventfs_remove_events_dir - remove the top level eventfs directory
1108  * @ei: the event_inode returned by eventfs_create_events_dir().
1109  *
1110  * This function removes the events main directory
1111  */
1112 void eventfs_remove_events_dir(struct eventfs_inode *ei)
1113 {
1114         struct dentry *dentry;
1115
1116         dentry = ei->dentry;
1117         eventfs_remove_dir(ei);
1118
1119         /*
1120          * Matches the dget() done by tracefs_start_creating()
1121          * in eventfs_create_events_dir() when it the dentry was
1122          * created. In other words, it's a normal dentry that
1123          * sticks around while the other ei->dentry are created
1124          * and destroyed dynamically.
1125          */
1126         dput(dentry);
1127 }