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