xref: /linux/fs/tracefs/event_inode.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  event_inode.c - part of tracefs, a pseudo file system for activating tracing
4  *
5  *  Copyright (C) 2020-23 VMware Inc, author: Steven Rostedt <rostedt@goodmis.org>
6  *  Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com>
7  *  Copyright (C) 2023 Google, author: Steven Rostedt <rostedt@goodmis.org>
8  *
9  *  eventfs is used to dynamically create inodes and dentries based on the
10  *  meta data provided by the tracing system.
11  *
12  *  eventfs stores the meta-data of files/dirs and holds off on creating
13  *  inodes/dentries of the files. When accessed, the eventfs will create the
14  *  inodes/dentries in a just-in-time (JIT) manner. The eventfs will clean up
15  *  and delete the inodes/dentries when they are no longer referenced.
16  */
17 #include <linux/fsnotify.h>
18 #include <linux/fs.h>
19 #include <linux/namei.h>
20 #include <linux/workqueue.h>
21 #include <linux/security.h>
22 #include <linux/tracefs.h>
23 #include <linux/kref.h>
24 #include <linux/delay.h>
25 #include "internal.h"
26 
27 /*
28  * eventfs_mutex protects the eventfs_inode (ei) dentry. Any access
29  * to the ei->dentry must be done under this mutex and after checking
30  * if ei->is_freed is not set. When ei->is_freed is set, the dentry
31  * is on its way to being freed after the last dput() is made on it.
32  */
33 static DEFINE_MUTEX(eventfs_mutex);
34 
35 /* Choose something "unique" ;-) */
36 #define EVENTFS_FILE_INODE_INO		0x12c4e37
37 
38 struct eventfs_root_inode {
39 	struct eventfs_inode		ei;
40 	struct dentry			*events_dir;
41 };
42 
43 static struct eventfs_root_inode *get_root_inode(struct eventfs_inode *ei)
44 {
45 	WARN_ON_ONCE(!ei->is_events);
46 	return container_of(ei, struct eventfs_root_inode, ei);
47 }
48 
49 /* Just try to make something consistent and unique */
50 static int eventfs_dir_ino(struct eventfs_inode *ei)
51 {
52 	if (!ei->ino)
53 		ei->ino = get_next_ino();
54 
55 	return ei->ino;
56 }
57 
58 /*
59  * The eventfs_inode (ei) itself is protected by SRCU. It is released from
60  * its parent's list and will have is_freed set (under eventfs_mutex).
61  * After the SRCU grace period is over and the last dput() is called
62  * the ei is freed.
63  */
64 DEFINE_STATIC_SRCU(eventfs_srcu);
65 
66 /* Mode is unsigned short, use the upper bits for flags */
67 enum {
68 	EVENTFS_SAVE_MODE	= BIT(16),
69 	EVENTFS_SAVE_UID	= BIT(17),
70 	EVENTFS_SAVE_GID	= BIT(18),
71 	EVENTFS_TOPLEVEL	= BIT(19),
72 };
73 
74 #define EVENTFS_MODE_MASK	(EVENTFS_SAVE_MODE - 1)
75 
76 /*
77  * eventfs_inode reference count management.
78  *
79  * NOTE! We count only references from dentries, in the
80  * form 'dentry->d_fsdata'. There are also references from
81  * directory inodes ('ti->private'), but the dentry reference
82  * count is always a superset of the inode reference count.
83  */
84 static void release_ei(struct kref *ref)
85 {
86 	struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
87 	struct eventfs_root_inode *rei;
88 
89 	WARN_ON_ONCE(!ei->is_freed);
90 
91 	kfree(ei->entry_attrs);
92 	kfree_const(ei->name);
93 	if (ei->is_events) {
94 		rei = get_root_inode(ei);
95 		kfree_rcu(rei, ei.rcu);
96 	} else {
97 		kfree_rcu(ei, rcu);
98 	}
99 }
100 
101 static inline void put_ei(struct eventfs_inode *ei)
102 {
103 	if (ei)
104 		kref_put(&ei->kref, release_ei);
105 }
106 
107 static inline void free_ei(struct eventfs_inode *ei)
108 {
109 	if (ei) {
110 		ei->is_freed = 1;
111 		put_ei(ei);
112 	}
113 }
114 
115 static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
116 {
117 	if (ei)
118 		kref_get(&ei->kref);
119 	return ei;
120 }
121 
122 static struct dentry *eventfs_root_lookup(struct inode *dir,
123 					  struct dentry *dentry,
124 					  unsigned int flags);
125 static int eventfs_iterate(struct file *file, struct dir_context *ctx);
126 
127 static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
128 {
129 	unsigned int ia_valid = iattr->ia_valid;
130 
131 	if (ia_valid & ATTR_MODE) {
132 		attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
133 			(iattr->ia_mode & EVENTFS_MODE_MASK) |
134 			EVENTFS_SAVE_MODE;
135 	}
136 	if (ia_valid & ATTR_UID) {
137 		attr->mode |= EVENTFS_SAVE_UID;
138 		attr->uid = iattr->ia_uid;
139 	}
140 	if (ia_valid & ATTR_GID) {
141 		attr->mode |= EVENTFS_SAVE_GID;
142 		attr->gid = iattr->ia_gid;
143 	}
144 }
145 
146 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
147 			    struct iattr *iattr)
148 {
149 	const struct eventfs_entry *entry;
150 	struct eventfs_inode *ei;
151 	const char *name;
152 	int ret;
153 
154 	mutex_lock(&eventfs_mutex);
155 	ei = dentry->d_fsdata;
156 	if (ei->is_freed) {
157 		/* Do not allow changes if the event is about to be removed. */
158 		mutex_unlock(&eventfs_mutex);
159 		return -ENODEV;
160 	}
161 
162 	/* Preallocate the children mode array if necessary */
163 	if (!(dentry->d_inode->i_mode & S_IFDIR)) {
164 		if (!ei->entry_attrs) {
165 			ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
166 						  GFP_NOFS);
167 			if (!ei->entry_attrs) {
168 				ret = -ENOMEM;
169 				goto out;
170 			}
171 		}
172 	}
173 
174 	ret = simple_setattr(idmap, dentry, iattr);
175 	if (ret < 0)
176 		goto out;
177 
178 	/*
179 	 * If this is a dir, then update the ei cache, only the file
180 	 * mode is saved in the ei->m_children, and the ownership is
181 	 * determined by the parent directory.
182 	 */
183 	if (dentry->d_inode->i_mode & S_IFDIR) {
184 		/*
185 		 * The events directory dentry is never freed, unless its
186 		 * part of an instance that is deleted. It's attr is the
187 		 * default for its child files and directories.
188 		 * Do not update it. It's not used for its own mode or ownership.
189 		 */
190 		if (ei->is_events) {
191 			/* But it still needs to know if it was modified */
192 			if (iattr->ia_valid & ATTR_UID)
193 				ei->attr.mode |= EVENTFS_SAVE_UID;
194 			if (iattr->ia_valid & ATTR_GID)
195 				ei->attr.mode |= EVENTFS_SAVE_GID;
196 		} else {
197 			update_attr(&ei->attr, iattr);
198 		}
199 
200 	} else {
201 		name = dentry->d_name.name;
202 
203 		for (int i = 0; i < ei->nr_entries; i++) {
204 			entry = &ei->entries[i];
205 			if (strcmp(name, entry->name) == 0) {
206 				update_attr(&ei->entry_attrs[i], iattr);
207 				break;
208 			}
209 		}
210 	}
211  out:
212 	mutex_unlock(&eventfs_mutex);
213 	return ret;
214 }
215 
216 static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb)
217 {
218 	struct inode *root;
219 
220 	/* Only update if the "events" was on the top level */
221 	if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
222 		return;
223 
224 	/* Get the tracefs root inode. */
225 	root = d_inode(sb->s_root);
226 	ei->attr.uid = root->i_uid;
227 	ei->attr.gid = root->i_gid;
228 }
229 
230 static void set_top_events_ownership(struct inode *inode)
231 {
232 	struct tracefs_inode *ti = get_tracefs(inode);
233 	struct eventfs_inode *ei = ti->private;
234 
235 	/* The top events directory doesn't get automatically updated */
236 	if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
237 		return;
238 
239 	update_top_events_attr(ei, inode->i_sb);
240 
241 	if (!(ei->attr.mode & EVENTFS_SAVE_UID))
242 		inode->i_uid = ei->attr.uid;
243 
244 	if (!(ei->attr.mode & EVENTFS_SAVE_GID))
245 		inode->i_gid = ei->attr.gid;
246 }
247 
248 static int eventfs_get_attr(struct mnt_idmap *idmap,
249 			    const struct path *path, struct kstat *stat,
250 			    u32 request_mask, unsigned int flags)
251 {
252 	struct dentry *dentry = path->dentry;
253 	struct inode *inode = d_backing_inode(dentry);
254 
255 	set_top_events_ownership(inode);
256 
257 	generic_fillattr(idmap, request_mask, inode, stat);
258 	return 0;
259 }
260 
261 static int eventfs_permission(struct mnt_idmap *idmap,
262 			      struct inode *inode, int mask)
263 {
264 	set_top_events_ownership(inode);
265 	return generic_permission(idmap, inode, mask);
266 }
267 
268 static const struct inode_operations eventfs_root_dir_inode_operations = {
269 	.lookup		= eventfs_root_lookup,
270 	.setattr	= eventfs_set_attr,
271 	.getattr	= eventfs_get_attr,
272 	.permission	= eventfs_permission,
273 };
274 
275 static const struct inode_operations eventfs_file_inode_operations = {
276 	.setattr	= eventfs_set_attr,
277 };
278 
279 static const struct file_operations eventfs_file_operations = {
280 	.read		= generic_read_dir,
281 	.iterate_shared	= eventfs_iterate,
282 	.llseek		= generic_file_llseek,
283 };
284 
285 /* Return the evenfs_inode of the "events" directory */
286 static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
287 {
288 	struct eventfs_inode *ei;
289 
290 	do {
291 		// The parent is stable because we do not do renames
292 		dentry = dentry->d_parent;
293 		// ... and directories always have d_fsdata
294 		ei = dentry->d_fsdata;
295 
296 		/*
297 		 * If the ei is being freed, the ownership of the children
298 		 * doesn't matter.
299 		 */
300 		if (ei->is_freed) {
301 			ei = NULL;
302 			break;
303 		}
304 		// Walk upwards until you find the events inode
305 	} while (!ei->is_events);
306 
307 	update_top_events_attr(ei, dentry->d_sb);
308 
309 	return ei;
310 }
311 
312 static void update_inode_attr(struct dentry *dentry, struct inode *inode,
313 			      struct eventfs_attr *attr, umode_t mode)
314 {
315 	struct eventfs_inode *events_ei = eventfs_find_events(dentry);
316 
317 	if (!events_ei)
318 		return;
319 
320 	inode->i_mode = mode;
321 	inode->i_uid = events_ei->attr.uid;
322 	inode->i_gid = events_ei->attr.gid;
323 
324 	if (!attr)
325 		return;
326 
327 	if (attr->mode & EVENTFS_SAVE_MODE)
328 		inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
329 
330 	if (attr->mode & EVENTFS_SAVE_UID)
331 		inode->i_uid = attr->uid;
332 
333 	if (attr->mode & EVENTFS_SAVE_GID)
334 		inode->i_gid = attr->gid;
335 }
336 
337 /**
338  * lookup_file - look up a file in the tracefs filesystem
339  * @parent_ei: Pointer to the eventfs_inode that represents parent of the file
340  * @dentry: the dentry to look up
341  * @mode: the permission that the file should have.
342  * @attr: saved attributes changed by user
343  * @data: something that the caller will want to get to later on.
344  * @fop: struct file_operations that should be used for this file.
345  *
346  * This function creates a dentry that represents a file in the eventsfs_inode
347  * directory. The inode.i_private pointer will point to @data in the open()
348  * call.
349  */
350 static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
351 				  struct dentry *dentry,
352 				  umode_t mode,
353 				  struct eventfs_attr *attr,
354 				  void *data,
355 				  const struct file_operations *fop)
356 {
357 	struct tracefs_inode *ti;
358 	struct inode *inode;
359 
360 	if (!(mode & S_IFMT))
361 		mode |= S_IFREG;
362 
363 	if (WARN_ON_ONCE(!S_ISREG(mode)))
364 		return ERR_PTR(-EIO);
365 
366 	inode = tracefs_get_inode(dentry->d_sb);
367 	if (unlikely(!inode))
368 		return ERR_PTR(-ENOMEM);
369 
370 	/* If the user updated the directory's attributes, use them */
371 	update_inode_attr(dentry, inode, attr, mode);
372 
373 	inode->i_op = &eventfs_file_inode_operations;
374 	inode->i_fop = fop;
375 	inode->i_private = data;
376 
377 	/* All files will have the same inode number */
378 	inode->i_ino = EVENTFS_FILE_INODE_INO;
379 
380 	ti = get_tracefs(inode);
381 	ti->flags |= TRACEFS_EVENT_INODE;
382 
383 	// Files have their parent's ei as their fsdata
384 	dentry->d_fsdata = get_ei(parent_ei);
385 
386 	d_add(dentry, inode);
387 	return NULL;
388 };
389 
390 /**
391  * lookup_dir_entry - look up a dir in the tracefs filesystem
392  * @dentry: the directory to look up
393  * @pei: Pointer to the parent eventfs_inode if available
394  * @ei: the eventfs_inode that represents the directory to create
395  *
396  * This function will look up a dentry for a directory represented by
397  * a eventfs_inode.
398  */
399 static struct dentry *lookup_dir_entry(struct dentry *dentry,
400 	struct eventfs_inode *pei, struct eventfs_inode *ei)
401 {
402 	struct tracefs_inode *ti;
403 	struct inode *inode;
404 
405 	inode = tracefs_get_inode(dentry->d_sb);
406 	if (unlikely(!inode))
407 		return ERR_PTR(-ENOMEM);
408 
409 	/* If the user updated the directory's attributes, use them */
410 	update_inode_attr(dentry, inode, &ei->attr,
411 			  S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
412 
413 	inode->i_op = &eventfs_root_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 	ti = get_tracefs(inode);
420 	ti->flags |= TRACEFS_EVENT_INODE;
421 	/* Only directories have ti->private set to an ei, not files */
422 	ti->private = ei;
423 
424 	dentry->d_fsdata = get_ei(ei);
425 
426 	d_add(dentry, inode);
427 	return NULL;
428 }
429 
430 static inline struct eventfs_inode *init_ei(struct eventfs_inode *ei, const char *name)
431 {
432 	ei->name = kstrdup_const(name, GFP_KERNEL);
433 	if (!ei->name)
434 		return NULL;
435 	kref_init(&ei->kref);
436 	return ei;
437 }
438 
439 static inline struct eventfs_inode *alloc_ei(const char *name)
440 {
441 	struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
442 	struct eventfs_inode *result;
443 
444 	if (!ei)
445 		return NULL;
446 
447 	result = init_ei(ei, name);
448 	if (!result)
449 		kfree(ei);
450 
451 	return result;
452 }
453 
454 static inline struct eventfs_inode *alloc_root_ei(const char *name)
455 {
456 	struct eventfs_root_inode *rei = kzalloc(sizeof(*rei), GFP_KERNEL);
457 	struct eventfs_inode *ei;
458 
459 	if (!rei)
460 		return NULL;
461 
462 	rei->ei.is_events = 1;
463 	ei = init_ei(&rei->ei, name);
464 	if (!ei)
465 		kfree(rei);
466 
467 	return ei;
468 }
469 
470 /**
471  * eventfs_d_release - dentry is going away
472  * @dentry: dentry which has the reference to remove.
473  *
474  * Remove the association between a dentry from an eventfs_inode.
475  */
476 void eventfs_d_release(struct dentry *dentry)
477 {
478 	put_ei(dentry->d_fsdata);
479 }
480 
481 /**
482  * lookup_file_dentry - create a dentry for a file of an eventfs_inode
483  * @dentry: The parent dentry under which the new file's dentry will be created
484  * @ei: the eventfs_inode that the file will be created under
485  * @idx: the index into the entry_attrs[] of the @ei
486  * @mode: The mode of the file.
487  * @data: The data to use to set the inode of the file with on open()
488  * @fops: The fops of the file to be created.
489  *
490  * This function creates a dentry for a file associated with an
491  * eventfs_inode @ei. It uses the entry attributes specified by @idx,
492  * if available. The file will have the specified @mode and its inode will be
493  * set up with @data upon open. The file operations will be set to @fops.
494  *
495  * Return: Returns a pointer to the newly created file's dentry or an error
496  * pointer.
497  */
498 static struct dentry *
499 lookup_file_dentry(struct dentry *dentry,
500 		   struct eventfs_inode *ei, int idx,
501 		   umode_t mode, void *data,
502 		   const struct file_operations *fops)
503 {
504 	struct eventfs_attr *attr = NULL;
505 
506 	if (ei->entry_attrs)
507 		attr = &ei->entry_attrs[idx];
508 
509 	return lookup_file(ei, dentry, mode, attr, data, fops);
510 }
511 
512 /**
513  * eventfs_root_lookup - lookup routine to create file/dir
514  * @dir: in which a lookup is being done
515  * @dentry: file/dir dentry
516  * @flags: Just passed to simple_lookup()
517  *
518  * Used to create dynamic file/dir with-in @dir, search with-in @ei
519  * list, if @dentry found go ahead and create the file/dir
520  */
521 
522 static struct dentry *eventfs_root_lookup(struct inode *dir,
523 					  struct dentry *dentry,
524 					  unsigned int flags)
525 {
526 	struct eventfs_inode *ei_child;
527 	struct tracefs_inode *ti;
528 	struct eventfs_inode *ei;
529 	const char *name = dentry->d_name.name;
530 	struct dentry *result = NULL;
531 
532 	ti = get_tracefs(dir);
533 	if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
534 		return ERR_PTR(-EIO);
535 
536 	mutex_lock(&eventfs_mutex);
537 
538 	ei = ti->private;
539 	if (!ei || ei->is_freed)
540 		goto out;
541 
542 	list_for_each_entry(ei_child, &ei->children, list) {
543 		if (strcmp(ei_child->name, name) != 0)
544 			continue;
545 		/* A child is freed and removed from the list at the same time */
546 		if (WARN_ON_ONCE(ei_child->is_freed))
547 			goto out;
548 		result = lookup_dir_entry(dentry, ei, ei_child);
549 		goto out;
550 	}
551 
552 	for (int i = 0; i < ei->nr_entries; i++) {
553 		void *data;
554 		umode_t mode;
555 		const struct file_operations *fops;
556 		const struct eventfs_entry *entry = &ei->entries[i];
557 
558 		if (strcmp(name, entry->name) != 0)
559 			continue;
560 
561 		data = ei->data;
562 		if (entry->callback(name, &mode, &data, &fops) <= 0)
563 			goto out;
564 
565 		result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
566 		goto out;
567 	}
568  out:
569 	mutex_unlock(&eventfs_mutex);
570 	return result;
571 }
572 
573 /*
574  * Walk the children of a eventfs_inode to fill in getdents().
575  */
576 static int eventfs_iterate(struct file *file, struct dir_context *ctx)
577 {
578 	const struct file_operations *fops;
579 	struct inode *f_inode = file_inode(file);
580 	const struct eventfs_entry *entry;
581 	struct eventfs_inode *ei_child;
582 	struct tracefs_inode *ti;
583 	struct eventfs_inode *ei;
584 	const char *name;
585 	umode_t mode;
586 	int idx;
587 	int ret = -EINVAL;
588 	int ino;
589 	int i, r, c;
590 
591 	if (!dir_emit_dots(file, ctx))
592 		return 0;
593 
594 	ti = get_tracefs(f_inode);
595 	if (!(ti->flags & TRACEFS_EVENT_INODE))
596 		return -EINVAL;
597 
598 	c = ctx->pos - 2;
599 
600 	idx = srcu_read_lock(&eventfs_srcu);
601 
602 	mutex_lock(&eventfs_mutex);
603 	ei = READ_ONCE(ti->private);
604 	if (ei && ei->is_freed)
605 		ei = NULL;
606 	mutex_unlock(&eventfs_mutex);
607 
608 	if (!ei)
609 		goto out;
610 
611 	/*
612 	 * Need to create the dentries and inodes to have a consistent
613 	 * inode number.
614 	 */
615 	ret = 0;
616 
617 	/* Start at 'c' to jump over already read entries */
618 	for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
619 		void *cdata = ei->data;
620 
621 		entry = &ei->entries[i];
622 		name = entry->name;
623 
624 		mutex_lock(&eventfs_mutex);
625 		/* If ei->is_freed then just bail here, nothing more to do */
626 		if (ei->is_freed) {
627 			mutex_unlock(&eventfs_mutex);
628 			goto out;
629 		}
630 		r = entry->callback(name, &mode, &cdata, &fops);
631 		mutex_unlock(&eventfs_mutex);
632 		if (r <= 0)
633 			continue;
634 
635 		ino = EVENTFS_FILE_INODE_INO;
636 
637 		if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
638 			goto out;
639 	}
640 
641 	/* Subtract the skipped entries above */
642 	c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
643 
644 	list_for_each_entry_srcu(ei_child, &ei->children, list,
645 				 srcu_read_lock_held(&eventfs_srcu)) {
646 
647 		if (c > 0) {
648 			c--;
649 			continue;
650 		}
651 
652 		ctx->pos++;
653 
654 		if (ei_child->is_freed)
655 			continue;
656 
657 		name = ei_child->name;
658 
659 		ino = eventfs_dir_ino(ei_child);
660 
661 		if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
662 			goto out_dec;
663 	}
664 	ret = 1;
665  out:
666 	srcu_read_unlock(&eventfs_srcu, idx);
667 
668 	return ret;
669 
670  out_dec:
671 	/* Incremented ctx->pos without adding something, reset it */
672 	ctx->pos--;
673 	goto out;
674 }
675 
676 /**
677  * eventfs_create_dir - Create the eventfs_inode for this directory
678  * @name: The name of the directory to create.
679  * @parent: The eventfs_inode of the parent directory.
680  * @entries: A list of entries that represent the files under this directory
681  * @size: The number of @entries
682  * @data: The default data to pass to the files (an entry may override it).
683  *
684  * This function creates the descriptor to represent a directory in the
685  * eventfs. This descriptor is an eventfs_inode, and it is returned to be
686  * used to create other children underneath.
687  *
688  * The @entries is an array of eventfs_entry structures which has:
689  *	const char		 *name
690  *	eventfs_callback	callback;
691  *
692  * The name is the name of the file, and the callback is a pointer to a function
693  * that will be called when the file is reference (either by lookup or by
694  * reading a directory). The callback is of the prototype:
695  *
696  *    int callback(const char *name, umode_t *mode, void **data,
697  *		   const struct file_operations **fops);
698  *
699  * When a file needs to be created, this callback will be called with
700  *   name = the name of the file being created (so that the same callback
701  *          may be used for multiple files).
702  *   mode = a place to set the file's mode
703  *   data = A pointer to @data, and the callback may replace it, which will
704  *         cause the file created to pass the new data to the open() call.
705  *   fops = the fops to use for the created file.
706  *
707  * NB. @callback is called while holding internal locks of the eventfs
708  *     system. The callback must not call any code that might also call into
709  *     the tracefs or eventfs system or it will risk creating a deadlock.
710  */
711 struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
712 					 const struct eventfs_entry *entries,
713 					 int size, void *data)
714 {
715 	struct eventfs_inode *ei;
716 
717 	if (!parent)
718 		return ERR_PTR(-EINVAL);
719 
720 	ei = alloc_ei(name);
721 	if (!ei)
722 		return ERR_PTR(-ENOMEM);
723 
724 	ei->entries = entries;
725 	ei->nr_entries = size;
726 	ei->data = data;
727 	INIT_LIST_HEAD(&ei->children);
728 	INIT_LIST_HEAD(&ei->list);
729 
730 	mutex_lock(&eventfs_mutex);
731 	if (!parent->is_freed)
732 		list_add_tail(&ei->list, &parent->children);
733 	mutex_unlock(&eventfs_mutex);
734 
735 	/* Was the parent freed? */
736 	if (list_empty(&ei->list)) {
737 		free_ei(ei);
738 		ei = NULL;
739 	}
740 	return ei;
741 }
742 
743 /**
744  * eventfs_create_events_dir - create the top level events directory
745  * @name: The name of the top level directory to create.
746  * @parent: Parent dentry for this file in the tracefs directory.
747  * @entries: A list of entries that represent the files under this directory
748  * @size: The number of @entries
749  * @data: The default data to pass to the files (an entry may override it).
750  *
751  * This function creates the top of the trace event directory.
752  *
753  * See eventfs_create_dir() for use of @entries.
754  */
755 struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
756 						const struct eventfs_entry *entries,
757 						int size, void *data)
758 {
759 	struct dentry *dentry = tracefs_start_creating(name, parent);
760 	struct eventfs_root_inode *rei;
761 	struct eventfs_inode *ei;
762 	struct tracefs_inode *ti;
763 	struct inode *inode;
764 	kuid_t uid;
765 	kgid_t gid;
766 
767 	if (security_locked_down(LOCKDOWN_TRACEFS))
768 		return NULL;
769 
770 	if (IS_ERR(dentry))
771 		return ERR_CAST(dentry);
772 
773 	ei = alloc_root_ei(name);
774 	if (!ei)
775 		goto fail;
776 
777 	inode = tracefs_get_inode(dentry->d_sb);
778 	if (unlikely(!inode))
779 		goto fail;
780 
781 	// Note: we have a ref to the dentry from tracefs_start_creating()
782 	rei = get_root_inode(ei);
783 	rei->events_dir = dentry;
784 
785 	ei->entries = entries;
786 	ei->nr_entries = size;
787 	ei->data = data;
788 
789 	/* Save the ownership of this directory */
790 	uid = d_inode(dentry->d_parent)->i_uid;
791 	gid = d_inode(dentry->d_parent)->i_gid;
792 
793 	/*
794 	 * If the events directory is of the top instance, then parent
795 	 * is NULL. Set the attr.mode to reflect this and its permissions will
796 	 * default to the tracefs root dentry.
797 	 */
798 	if (!parent)
799 		ei->attr.mode = EVENTFS_TOPLEVEL;
800 
801 	/* This is used as the default ownership of the files and directories */
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 | TRACEFS_EVENT_TOP_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_root_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_instantiate(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 	free_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  */
851 static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
852 {
853 	struct eventfs_inode *ei_child;
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(ei_child, &ei->children, list)
867 		eventfs_remove_rec(ei_child, level + 1);
868 
869 	list_del(&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  */
879 void eventfs_remove_dir(struct eventfs_inode *ei)
880 {
881 	if (!ei)
882 		return;
883 
884 	mutex_lock(&eventfs_mutex);
885 	eventfs_remove_rec(ei, 0);
886 	mutex_unlock(&eventfs_mutex);
887 }
888 
889 /**
890  * eventfs_remove_events_dir - remove the top level eventfs directory
891  * @ei: the event_inode returned by eventfs_create_events_dir().
892  *
893  * This function removes the events main directory
894  */
895 void eventfs_remove_events_dir(struct eventfs_inode *ei)
896 {
897 	struct eventfs_root_inode *rei;
898 	struct dentry *dentry;
899 
900 	rei = get_root_inode(ei);
901 	dentry = rei->events_dir;
902 	if (!dentry)
903 		return;
904 
905 	rei->events_dir = NULL;
906 	eventfs_remove_dir(ei);
907 
908 	/*
909 	 * Matches the dget() done by tracefs_start_creating()
910 	 * in eventfs_create_events_dir() when it the dentry was
911 	 * created. In other words, it's a normal dentry that
912 	 * sticks around while the other ei->dentry are created
913 	 * and destroyed dynamically.
914 	 */
915 	d_invalidate(dentry);
916 	dput(dentry);
917 }
918