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