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