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