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