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