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