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