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 (VMware) <rostedt@goodmis.org> 6 * Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com> 7 * 8 * eventfs is used to dynamically create inodes and dentries based on the 9 * meta data provided by the tracing system. 10 * 11 * eventfs stores the meta-data of files/dirs and holds off on creating 12 * inodes/dentries of the files. When accessed, the eventfs will create the 13 * inodes/dentries in a just-in-time (JIT) manner. The eventfs will clean up 14 * and delete the inodes/dentries when they are no longer referenced. 15 */ 16 #include <linux/fsnotify.h> 17 #include <linux/fs.h> 18 #include <linux/namei.h> 19 #include <linux/workqueue.h> 20 #include <linux/security.h> 21 #include <linux/tracefs.h> 22 #include <linux/kref.h> 23 #include <linux/delay.h> 24 #include "internal.h" 25 26 struct eventfs_inode { 27 struct list_head e_top_files; 28 }; 29 30 /* 31 * struct eventfs_file - hold the properties of the eventfs files and 32 * directories. 33 * @name: the name of the file or directory to create 34 * @d_parent: holds parent's dentry 35 * @dentry: once accessed holds dentry 36 * @list: file or directory to be added to parent directory 37 * @ei: list of files and directories within directory 38 * @fop: file_operations for file or directory 39 * @iop: inode_operations for file or directory 40 * @data: something that the caller will want to get to later on 41 * @is_freed: Flag set if the eventfs is on its way to be freed 42 * @mode: the permission that the file or directory should have 43 * @uid: saved uid if changed 44 * @gid: saved gid if changed 45 */ 46 struct eventfs_file { 47 const char *name; 48 struct dentry *d_parent; 49 struct dentry *dentry; 50 struct list_head list; 51 struct eventfs_inode *ei; 52 const struct file_operations *fop; 53 const struct inode_operations *iop; 54 /* 55 * Union - used for deletion 56 * @llist: for calling dput() if needed after RCU 57 * @rcu: eventfs_file to delete in RCU 58 */ 59 union { 60 struct llist_node llist; 61 struct rcu_head rcu; 62 }; 63 void *data; 64 unsigned int is_freed:1; 65 unsigned int mode:31; 66 kuid_t uid; 67 kgid_t gid; 68 }; 69 70 static DEFINE_MUTEX(eventfs_mutex); 71 DEFINE_STATIC_SRCU(eventfs_srcu); 72 73 /* Mode is unsigned short, use the upper bits for flags */ 74 enum { 75 EVENTFS_SAVE_MODE = BIT(16), 76 EVENTFS_SAVE_UID = BIT(17), 77 EVENTFS_SAVE_GID = BIT(18), 78 }; 79 80 #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1) 81 82 static struct dentry *eventfs_root_lookup(struct inode *dir, 83 struct dentry *dentry, 84 unsigned int flags); 85 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file); 86 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx); 87 static int eventfs_release(struct inode *inode, struct file *file); 88 89 static void update_attr(struct eventfs_file *ef, struct iattr *iattr) 90 { 91 unsigned int ia_valid = iattr->ia_valid; 92 93 if (ia_valid & ATTR_MODE) { 94 ef->mode = (ef->mode & ~EVENTFS_MODE_MASK) | 95 (iattr->ia_mode & EVENTFS_MODE_MASK) | 96 EVENTFS_SAVE_MODE; 97 } 98 if (ia_valid & ATTR_UID) { 99 ef->mode |= EVENTFS_SAVE_UID; 100 ef->uid = iattr->ia_uid; 101 } 102 if (ia_valid & ATTR_GID) { 103 ef->mode |= EVENTFS_SAVE_GID; 104 ef->gid = iattr->ia_gid; 105 } 106 } 107 108 static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry, 109 struct iattr *iattr) 110 { 111 struct eventfs_file *ef; 112 int ret; 113 114 mutex_lock(&eventfs_mutex); 115 ef = dentry->d_fsdata; 116 if (ef->is_freed) { 117 /* Do not allow changes if the event is about to be removed. */ 118 mutex_unlock(&eventfs_mutex); 119 return -ENODEV; 120 } 121 122 ret = simple_setattr(idmap, dentry, iattr); 123 if (!ret) 124 update_attr(ef, iattr); 125 mutex_unlock(&eventfs_mutex); 126 return ret; 127 } 128 129 static const struct inode_operations eventfs_root_dir_inode_operations = { 130 .lookup = eventfs_root_lookup, 131 .setattr = eventfs_set_attr, 132 }; 133 134 static const struct inode_operations eventfs_file_inode_operations = { 135 .setattr = eventfs_set_attr, 136 }; 137 138 static const struct file_operations eventfs_file_operations = { 139 .open = dcache_dir_open_wrapper, 140 .read = generic_read_dir, 141 .iterate_shared = dcache_readdir_wrapper, 142 .llseek = generic_file_llseek, 143 .release = eventfs_release, 144 }; 145 146 static void update_inode_attr(struct inode *inode, struct eventfs_file *ef) 147 { 148 inode->i_mode = ef->mode & EVENTFS_MODE_MASK; 149 150 if (ef->mode & EVENTFS_SAVE_UID) 151 inode->i_uid = ef->uid; 152 153 if (ef->mode & EVENTFS_SAVE_GID) 154 inode->i_gid = ef->gid; 155 } 156 157 /** 158 * create_file - create a file in the tracefs filesystem 159 * @ef: the eventfs_file 160 * @parent: parent dentry for this file. 161 * @data: something that the caller will want to get to later on. 162 * @fop: struct file_operations that should be used for this file. 163 * 164 * This is the basic "create a file" function for tracefs. It allows for a 165 * wide range of flexibility in creating a file. 166 * 167 * This function will return a pointer to a dentry if it succeeds. This 168 * pointer must be passed to the tracefs_remove() function when the file is 169 * to be removed (no automatic cleanup happens if your module is unloaded, 170 * you are responsible here.) If an error occurs, %NULL will be returned. 171 * 172 * If tracefs is not enabled in the kernel, the value -%ENODEV will be 173 * returned. 174 */ 175 static struct dentry *create_file(struct eventfs_file *ef, 176 struct dentry *parent, void *data, 177 const struct file_operations *fop) 178 { 179 struct tracefs_inode *ti; 180 struct dentry *dentry; 181 struct inode *inode; 182 183 if (!(ef->mode & S_IFMT)) 184 ef->mode |= S_IFREG; 185 186 if (WARN_ON_ONCE(!S_ISREG(ef->mode))) 187 return NULL; 188 189 dentry = eventfs_start_creating(ef->name, parent); 190 191 if (IS_ERR(dentry)) 192 return dentry; 193 194 inode = tracefs_get_inode(dentry->d_sb); 195 if (unlikely(!inode)) 196 return eventfs_failed_creating(dentry); 197 198 /* If the user updated the directory's attributes, use them */ 199 update_inode_attr(inode, ef); 200 201 inode->i_op = &eventfs_file_inode_operations; 202 inode->i_fop = fop; 203 inode->i_private = data; 204 205 ti = get_tracefs(inode); 206 ti->flags |= TRACEFS_EVENT_INODE; 207 d_instantiate(dentry, inode); 208 fsnotify_create(dentry->d_parent->d_inode, dentry); 209 return eventfs_end_creating(dentry); 210 }; 211 212 /** 213 * create_dir - create a dir in the tracefs filesystem 214 * @ei: the eventfs_inode that represents the directory to create 215 * @parent: parent dentry for this file. 216 * @data: something that the caller will want to get to later on. 217 * 218 * This is the basic "create a dir" function for eventfs. It allows for a 219 * wide range of flexibility in creating a dir. 220 * 221 * This function will return a pointer to a dentry if it succeeds. This 222 * pointer must be passed to the tracefs_remove() function when the file is 223 * to be removed (no automatic cleanup happens if your module is unloaded, 224 * you are responsible here.) If an error occurs, %NULL will be returned. 225 * 226 * If tracefs is not enabled in the kernel, the value -%ENODEV will be 227 * returned. 228 */ 229 static struct dentry *create_dir(struct eventfs_file *ef, 230 struct dentry *parent, void *data) 231 { 232 struct tracefs_inode *ti; 233 struct dentry *dentry; 234 struct inode *inode; 235 236 dentry = eventfs_start_creating(ef->name, parent); 237 if (IS_ERR(dentry)) 238 return dentry; 239 240 inode = tracefs_get_inode(dentry->d_sb); 241 if (unlikely(!inode)) 242 return eventfs_failed_creating(dentry); 243 244 update_inode_attr(inode, ef); 245 246 inode->i_op = &eventfs_root_dir_inode_operations; 247 inode->i_fop = &eventfs_file_operations; 248 inode->i_private = data; 249 250 ti = get_tracefs(inode); 251 ti->flags |= TRACEFS_EVENT_INODE; 252 253 inc_nlink(inode); 254 d_instantiate(dentry, inode); 255 inc_nlink(dentry->d_parent->d_inode); 256 fsnotify_mkdir(dentry->d_parent->d_inode, dentry); 257 return eventfs_end_creating(dentry); 258 } 259 260 static void free_ef(struct eventfs_file *ef) 261 { 262 kfree(ef->name); 263 kfree(ef->ei); 264 kfree(ef); 265 } 266 267 /** 268 * eventfs_set_ef_status_free - set the ef->status to free 269 * @ti: the tracefs_inode of the dentry 270 * @dentry: dentry who's status to be freed 271 * 272 * eventfs_set_ef_status_free will be called if no more 273 * references remain 274 */ 275 void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry) 276 { 277 struct eventfs_inode *ei; 278 struct eventfs_file *ef; 279 280 /* The top level events directory may be freed by this */ 281 if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) { 282 mutex_lock(&eventfs_mutex); 283 ei = ti->private; 284 285 /* Nothing should access this, but just in case! */ 286 ti->private = NULL; 287 mutex_unlock(&eventfs_mutex); 288 289 ef = dentry->d_fsdata; 290 if (ef) 291 free_ef(ef); 292 return; 293 } 294 295 mutex_lock(&eventfs_mutex); 296 297 ef = dentry->d_fsdata; 298 if (!ef) 299 goto out; 300 301 if (ef->is_freed) { 302 free_ef(ef); 303 } else { 304 ef->dentry = NULL; 305 } 306 307 dentry->d_fsdata = NULL; 308 out: 309 mutex_unlock(&eventfs_mutex); 310 } 311 312 /** 313 * eventfs_post_create_dir - post create dir routine 314 * @ef: eventfs_file of recently created dir 315 * 316 * Map the meta-data of files within an eventfs dir to their parent dentry 317 */ 318 static void eventfs_post_create_dir(struct eventfs_file *ef) 319 { 320 struct eventfs_file *ef_child; 321 struct tracefs_inode *ti; 322 323 /* srcu lock already held */ 324 /* fill parent-child relation */ 325 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list, 326 srcu_read_lock_held(&eventfs_srcu)) { 327 ef_child->d_parent = ef->dentry; 328 } 329 330 ti = get_tracefs(ef->dentry->d_inode); 331 ti->private = ef->ei; 332 } 333 334 /** 335 * create_dentry - helper function to create dentry 336 * @ef: eventfs_file of file or directory to create 337 * @parent: parent dentry 338 * @lookup: true if called from lookup routine 339 * 340 * Used to create a dentry for file/dir, executes post dentry creation routine 341 */ 342 static struct dentry * 343 create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup) 344 { 345 bool invalidate = false; 346 struct dentry *dentry; 347 348 mutex_lock(&eventfs_mutex); 349 if (ef->is_freed) { 350 mutex_unlock(&eventfs_mutex); 351 return NULL; 352 } 353 if (ef->dentry) { 354 dentry = ef->dentry; 355 /* On dir open, up the ref count */ 356 if (!lookup) 357 dget(dentry); 358 mutex_unlock(&eventfs_mutex); 359 return dentry; 360 } 361 mutex_unlock(&eventfs_mutex); 362 363 if (!lookup) 364 inode_lock(parent->d_inode); 365 366 if (ef->ei) 367 dentry = create_dir(ef, parent, ef->data); 368 else 369 dentry = create_file(ef, parent, ef->data, ef->fop); 370 371 if (!lookup) 372 inode_unlock(parent->d_inode); 373 374 mutex_lock(&eventfs_mutex); 375 if (IS_ERR_OR_NULL(dentry)) { 376 /* If the ef was already updated get it */ 377 dentry = ef->dentry; 378 if (dentry && !lookup) 379 dget(dentry); 380 mutex_unlock(&eventfs_mutex); 381 return dentry; 382 } 383 384 if (!ef->dentry && !ef->is_freed) { 385 ef->dentry = dentry; 386 if (ef->ei) 387 eventfs_post_create_dir(ef); 388 dentry->d_fsdata = ef; 389 } else { 390 /* A race here, should try again (unless freed) */ 391 invalidate = true; 392 393 /* 394 * Should never happen unless we get here due to being freed. 395 * Otherwise it means two dentries exist with the same name. 396 */ 397 WARN_ON_ONCE(!ef->is_freed); 398 } 399 mutex_unlock(&eventfs_mutex); 400 if (invalidate) 401 d_invalidate(dentry); 402 403 if (lookup || invalidate) 404 dput(dentry); 405 406 return invalidate ? NULL : dentry; 407 } 408 409 static bool match_event_file(struct eventfs_file *ef, const char *name) 410 { 411 bool ret; 412 413 mutex_lock(&eventfs_mutex); 414 ret = !ef->is_freed && strcmp(ef->name, name) == 0; 415 mutex_unlock(&eventfs_mutex); 416 417 return ret; 418 } 419 420 /** 421 * eventfs_root_lookup - lookup routine to create file/dir 422 * @dir: in which a lookup is being done 423 * @dentry: file/dir dentry 424 * @flags: to pass as flags parameter to simple lookup 425 * 426 * Used to create a dynamic file/dir within @dir. Use the eventfs_inode 427 * list of meta data to find the information needed to create the file/dir. 428 */ 429 static struct dentry *eventfs_root_lookup(struct inode *dir, 430 struct dentry *dentry, 431 unsigned int flags) 432 { 433 struct tracefs_inode *ti; 434 struct eventfs_inode *ei; 435 struct eventfs_file *ef; 436 struct dentry *ret = NULL; 437 int idx; 438 439 ti = get_tracefs(dir); 440 if (!(ti->flags & TRACEFS_EVENT_INODE)) 441 return NULL; 442 443 ei = ti->private; 444 idx = srcu_read_lock(&eventfs_srcu); 445 list_for_each_entry_srcu(ef, &ei->e_top_files, list, 446 srcu_read_lock_held(&eventfs_srcu)) { 447 if (!match_event_file(ef, dentry->d_name.name)) 448 continue; 449 ret = simple_lookup(dir, dentry, flags); 450 create_dentry(ef, ef->d_parent, true); 451 break; 452 } 453 srcu_read_unlock(&eventfs_srcu, idx); 454 return ret; 455 } 456 457 struct dentry_list { 458 void *cursor; 459 struct dentry **dentries; 460 }; 461 462 /** 463 * eventfs_release - called to release eventfs file/dir 464 * @inode: inode to be released 465 * @file: file to be released (not used) 466 */ 467 static int eventfs_release(struct inode *inode, struct file *file) 468 { 469 struct tracefs_inode *ti; 470 struct dentry_list *dlist = file->private_data; 471 void *cursor; 472 int i; 473 474 ti = get_tracefs(inode); 475 if (!(ti->flags & TRACEFS_EVENT_INODE)) 476 return -EINVAL; 477 478 if (WARN_ON_ONCE(!dlist)) 479 return -EINVAL; 480 481 for (i = 0; dlist->dentries && dlist->dentries[i]; i++) { 482 dput(dlist->dentries[i]); 483 } 484 485 cursor = dlist->cursor; 486 kfree(dlist->dentries); 487 kfree(dlist); 488 file->private_data = cursor; 489 return dcache_dir_close(inode, file); 490 } 491 492 /** 493 * dcache_dir_open_wrapper - eventfs open wrapper 494 * @inode: not used 495 * @file: dir to be opened (to create its child) 496 * 497 * Used to dynamically create the file/dir within @file. @file is really a 498 * directory and all the files/dirs of the children within @file will be 499 * created. If any of the files/dirs have already been created, their 500 * reference count will be incremented. 501 */ 502 static int dcache_dir_open_wrapper(struct inode *inode, struct file *file) 503 { 504 struct tracefs_inode *ti; 505 struct eventfs_inode *ei; 506 struct eventfs_file *ef; 507 struct dentry_list *dlist; 508 struct dentry **dentries = NULL; 509 struct dentry *dentry = file_dentry(file); 510 struct dentry *d; 511 struct inode *f_inode = file_inode(file); 512 int cnt = 0; 513 int idx; 514 int ret; 515 516 ti = get_tracefs(f_inode); 517 if (!(ti->flags & TRACEFS_EVENT_INODE)) 518 return -EINVAL; 519 520 if (WARN_ON_ONCE(file->private_data)) 521 return -EINVAL; 522 523 dlist = kmalloc(sizeof(*dlist), GFP_KERNEL); 524 if (!dlist) 525 return -ENOMEM; 526 527 ei = ti->private; 528 idx = srcu_read_lock(&eventfs_srcu); 529 list_for_each_entry_srcu(ef, &ei->e_top_files, list, 530 srcu_read_lock_held(&eventfs_srcu)) { 531 d = create_dentry(ef, dentry, false); 532 if (d) { 533 struct dentry **tmp; 534 535 536 tmp = krealloc(dentries, sizeof(d) * (cnt + 2), GFP_KERNEL); 537 if (!tmp) 538 break; 539 tmp[cnt] = d; 540 tmp[cnt + 1] = NULL; 541 cnt++; 542 dentries = tmp; 543 } 544 } 545 srcu_read_unlock(&eventfs_srcu, idx); 546 ret = dcache_dir_open(inode, file); 547 548 /* 549 * dcache_dir_open() sets file->private_data to a dentry cursor. 550 * Need to save that but also save all the dentries that were 551 * opened by this function. 552 */ 553 dlist->cursor = file->private_data; 554 dlist->dentries = dentries; 555 file->private_data = dlist; 556 return ret; 557 } 558 559 /* 560 * This just sets the file->private_data back to the cursor and back. 561 */ 562 static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx) 563 { 564 struct dentry_list *dlist = file->private_data; 565 int ret; 566 567 file->private_data = dlist->cursor; 568 ret = dcache_readdir(file, ctx); 569 dlist->cursor = file->private_data; 570 file->private_data = dlist; 571 return ret; 572 } 573 574 /** 575 * eventfs_prepare_ef - helper function to prepare eventfs_file 576 * @name: the name of the file/directory to create. 577 * @mode: the permission that the file should have. 578 * @fop: struct file_operations that should be used for this file/directory. 579 * @iop: struct inode_operations that should be used for this file/directory. 580 * @data: something that the caller will want to get to later on. The 581 * inode.i_private pointer will point to this value on the open() call. 582 * 583 * This function allocates and fills the eventfs_file structure. 584 */ 585 static struct eventfs_file *eventfs_prepare_ef(const char *name, umode_t mode, 586 const struct file_operations *fop, 587 const struct inode_operations *iop, 588 void *data) 589 { 590 struct eventfs_file *ef; 591 592 ef = kzalloc(sizeof(*ef), GFP_KERNEL); 593 if (!ef) 594 return ERR_PTR(-ENOMEM); 595 596 ef->name = kstrdup(name, GFP_KERNEL); 597 if (!ef->name) { 598 kfree(ef); 599 return ERR_PTR(-ENOMEM); 600 } 601 602 if (S_ISDIR(mode)) { 603 ef->ei = kzalloc(sizeof(*ef->ei), GFP_KERNEL); 604 if (!ef->ei) { 605 kfree(ef->name); 606 kfree(ef); 607 return ERR_PTR(-ENOMEM); 608 } 609 INIT_LIST_HEAD(&ef->ei->e_top_files); 610 ef->mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 611 } else { 612 ef->ei = NULL; 613 ef->mode = mode; 614 } 615 616 ef->iop = iop; 617 ef->fop = fop; 618 ef->data = data; 619 return ef; 620 } 621 622 /** 623 * eventfs_create_events_dir - create the trace event structure 624 * @name: the name of the directory to create. 625 * @parent: parent dentry for this file. This should be a directory dentry 626 * if set. If this parameter is NULL, then the directory will be 627 * created in the root of the tracefs filesystem. 628 * 629 * This function creates the top of the trace event directory. 630 */ 631 struct dentry *eventfs_create_events_dir(const char *name, 632 struct dentry *parent) 633 { 634 struct dentry *dentry = tracefs_start_creating(name, parent); 635 struct eventfs_inode *ei; 636 struct tracefs_inode *ti; 637 struct inode *inode; 638 639 if (security_locked_down(LOCKDOWN_TRACEFS)) 640 return NULL; 641 642 if (IS_ERR(dentry)) 643 return dentry; 644 645 ei = kzalloc(sizeof(*ei), GFP_KERNEL); 646 if (!ei) 647 return ERR_PTR(-ENOMEM); 648 inode = tracefs_get_inode(dentry->d_sb); 649 if (unlikely(!inode)) { 650 kfree(ei); 651 tracefs_failed_creating(dentry); 652 return ERR_PTR(-ENOMEM); 653 } 654 655 INIT_LIST_HEAD(&ei->e_top_files); 656 657 ti = get_tracefs(inode); 658 ti->flags |= TRACEFS_EVENT_INODE | TRACEFS_EVENT_TOP_INODE; 659 ti->private = ei; 660 661 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 662 inode->i_op = &eventfs_root_dir_inode_operations; 663 inode->i_fop = &eventfs_file_operations; 664 665 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 666 inc_nlink(inode); 667 d_instantiate(dentry, inode); 668 inc_nlink(dentry->d_parent->d_inode); 669 fsnotify_mkdir(dentry->d_parent->d_inode, dentry); 670 return tracefs_end_creating(dentry); 671 } 672 673 /** 674 * eventfs_add_subsystem_dir - add eventfs subsystem_dir to list to create later 675 * @name: the name of the file to create. 676 * @parent: parent dentry for this dir. 677 * 678 * This function adds eventfs subsystem dir to list. 679 * And all these dirs are created on the fly when they are looked up, 680 * and the dentry and inodes will be removed when they are done. 681 */ 682 struct eventfs_file *eventfs_add_subsystem_dir(const char *name, 683 struct dentry *parent) 684 { 685 struct tracefs_inode *ti_parent; 686 struct eventfs_inode *ei_parent; 687 struct eventfs_file *ef; 688 689 if (security_locked_down(LOCKDOWN_TRACEFS)) 690 return NULL; 691 692 if (!parent) 693 return ERR_PTR(-EINVAL); 694 695 ti_parent = get_tracefs(parent->d_inode); 696 ei_parent = ti_parent->private; 697 698 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL); 699 if (IS_ERR(ef)) 700 return ef; 701 702 mutex_lock(&eventfs_mutex); 703 list_add_tail(&ef->list, &ei_parent->e_top_files); 704 ef->d_parent = parent; 705 mutex_unlock(&eventfs_mutex); 706 return ef; 707 } 708 709 /** 710 * eventfs_add_dir - add eventfs dir to list to create later 711 * @name: the name of the file to create. 712 * @ef_parent: parent eventfs_file for this dir. 713 * 714 * This function adds eventfs dir to list. 715 * And all these dirs are created on the fly when they are looked up, 716 * and the dentry and inodes will be removed when they are done. 717 */ 718 struct eventfs_file *eventfs_add_dir(const char *name, 719 struct eventfs_file *ef_parent) 720 { 721 struct eventfs_file *ef; 722 723 if (security_locked_down(LOCKDOWN_TRACEFS)) 724 return NULL; 725 726 if (!ef_parent) 727 return ERR_PTR(-EINVAL); 728 729 ef = eventfs_prepare_ef(name, S_IFDIR, NULL, NULL, NULL); 730 if (IS_ERR(ef)) 731 return ef; 732 733 mutex_lock(&eventfs_mutex); 734 list_add_tail(&ef->list, &ef_parent->ei->e_top_files); 735 ef->d_parent = ef_parent->dentry; 736 mutex_unlock(&eventfs_mutex); 737 return ef; 738 } 739 740 /** 741 * eventfs_add_events_file - add the data needed to create a file for later reference 742 * @name: the name of the file to create. 743 * @mode: the permission that the file should have. 744 * @parent: parent dentry for this file. 745 * @data: something that the caller will want to get to later on. 746 * @fop: struct file_operations that should be used for this file. 747 * 748 * This function is used to add the information needed to create a 749 * dentry/inode within the top level events directory. The file created 750 * will have the @mode permissions. The @data will be used to fill the 751 * inode.i_private when the open() call is done. The dentry and inodes are 752 * all created when they are referenced, and removed when they are no 753 * longer referenced. 754 */ 755 int eventfs_add_events_file(const char *name, umode_t mode, 756 struct dentry *parent, void *data, 757 const struct file_operations *fop) 758 { 759 struct tracefs_inode *ti; 760 struct eventfs_inode *ei; 761 struct eventfs_file *ef; 762 763 if (security_locked_down(LOCKDOWN_TRACEFS)) 764 return -ENODEV; 765 766 if (!parent) 767 return -EINVAL; 768 769 if (!(mode & S_IFMT)) 770 mode |= S_IFREG; 771 772 if (!parent->d_inode) 773 return -EINVAL; 774 775 ti = get_tracefs(parent->d_inode); 776 if (!(ti->flags & TRACEFS_EVENT_INODE)) 777 return -EINVAL; 778 779 ei = ti->private; 780 ef = eventfs_prepare_ef(name, mode, fop, NULL, data); 781 782 if (IS_ERR(ef)) 783 return -ENOMEM; 784 785 mutex_lock(&eventfs_mutex); 786 list_add_tail(&ef->list, &ei->e_top_files); 787 ef->d_parent = parent; 788 mutex_unlock(&eventfs_mutex); 789 return 0; 790 } 791 792 /** 793 * eventfs_add_file - add eventfs file to list to create later 794 * @name: the name of the file to create. 795 * @mode: the permission that the file should have. 796 * @ef_parent: parent eventfs_file for this file. 797 * @data: something that the caller will want to get to later on. 798 * @fop: struct file_operations that should be used for this file. 799 * 800 * This function is used to add the information needed to create a 801 * file within a subdirectory of the events directory. The file created 802 * will have the @mode permissions. The @data will be used to fill the 803 * inode.i_private when the open() call is done. The dentry and inodes are 804 * all created when they are referenced, and removed when they are no 805 * longer referenced. 806 */ 807 int eventfs_add_file(const char *name, umode_t mode, 808 struct eventfs_file *ef_parent, 809 void *data, 810 const struct file_operations *fop) 811 { 812 struct eventfs_file *ef; 813 814 if (security_locked_down(LOCKDOWN_TRACEFS)) 815 return -ENODEV; 816 817 if (!ef_parent) 818 return -EINVAL; 819 820 if (!(mode & S_IFMT)) 821 mode |= S_IFREG; 822 823 ef = eventfs_prepare_ef(name, mode, fop, NULL, data); 824 if (IS_ERR(ef)) 825 return -ENOMEM; 826 827 mutex_lock(&eventfs_mutex); 828 list_add_tail(&ef->list, &ef_parent->ei->e_top_files); 829 ef->d_parent = ef_parent->dentry; 830 mutex_unlock(&eventfs_mutex); 831 return 0; 832 } 833 834 static LLIST_HEAD(free_list); 835 836 static void eventfs_workfn(struct work_struct *work) 837 { 838 struct eventfs_file *ef, *tmp; 839 struct llist_node *llnode; 840 841 llnode = llist_del_all(&free_list); 842 llist_for_each_entry_safe(ef, tmp, llnode, llist) { 843 /* This should only get here if it had a dentry */ 844 if (!WARN_ON_ONCE(!ef->dentry)) 845 dput(ef->dentry); 846 } 847 } 848 849 static DECLARE_WORK(eventfs_work, eventfs_workfn); 850 851 static void free_rcu_ef(struct rcu_head *head) 852 { 853 struct eventfs_file *ef = container_of(head, struct eventfs_file, rcu); 854 855 if (ef->dentry) { 856 /* Do not free the ef until all references of dentry are gone */ 857 if (llist_add(&ef->llist, &free_list)) 858 queue_work(system_unbound_wq, &eventfs_work); 859 return; 860 } 861 862 free_ef(ef); 863 } 864 865 static void unhook_dentry(struct dentry *dentry) 866 { 867 if (!dentry) 868 return; 869 /* 870 * Need to add a reference to the dentry that is expected by 871 * simple_recursive_removal(), which will include a dput(). 872 */ 873 dget(dentry); 874 875 /* 876 * Also add a reference for the dput() in eventfs_workfn(). 877 * That is required as that dput() will free the ei after 878 * the SRCU grace period is over. 879 */ 880 dget(dentry); 881 } 882 883 /** 884 * eventfs_remove_rec - remove eventfs dir or file from list 885 * @ef: eventfs_file to be removed. 886 * @level: to check recursion depth 887 * 888 * The helper function eventfs_remove_rec() is used to clean up and free the 889 * associated data from eventfs for both of the added functions. 890 */ 891 static void eventfs_remove_rec(struct eventfs_file *ef, int level) 892 { 893 struct eventfs_file *ef_child; 894 895 if (!ef) 896 return; 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 if (ef->ei) { 908 /* search for nested folders or files */ 909 list_for_each_entry_srcu(ef_child, &ef->ei->e_top_files, list, 910 lockdep_is_held(&eventfs_mutex)) { 911 eventfs_remove_rec(ef_child, level + 1); 912 } 913 } 914 915 ef->is_freed = 1; 916 917 unhook_dentry(ef->dentry); 918 919 list_del_rcu(&ef->list); 920 call_srcu(&eventfs_srcu, &ef->rcu, free_rcu_ef); 921 } 922 923 /** 924 * eventfs_remove - remove eventfs dir or file from list 925 * @ef: eventfs_file to be removed. 926 * 927 * This function acquire the eventfs_mutex lock and call eventfs_remove_rec() 928 */ 929 void eventfs_remove(struct eventfs_file *ef) 930 { 931 struct dentry *dentry; 932 933 if (!ef) 934 return; 935 936 mutex_lock(&eventfs_mutex); 937 dentry = ef->dentry; 938 eventfs_remove_rec(ef, 0); 939 mutex_unlock(&eventfs_mutex); 940 941 /* 942 * If any of the ei children has a dentry, then the ei itself 943 * must have a dentry. 944 */ 945 if (dentry) 946 simple_recursive_removal(dentry, NULL); 947 } 948 949 /** 950 * eventfs_remove_events_dir - remove eventfs dir or file from list 951 * @dentry: events's dentry to be removed. 952 * 953 * This function remove events main directory 954 */ 955 void eventfs_remove_events_dir(struct dentry *dentry) 956 { 957 struct eventfs_file *ef_child; 958 struct eventfs_inode *ei; 959 struct tracefs_inode *ti; 960 961 if (!dentry || !dentry->d_inode) 962 return; 963 964 ti = get_tracefs(dentry->d_inode); 965 if (!ti || !(ti->flags & TRACEFS_EVENT_INODE)) 966 return; 967 968 mutex_lock(&eventfs_mutex); 969 ei = ti->private; 970 list_for_each_entry_srcu(ef_child, &ei->e_top_files, list, 971 lockdep_is_held(&eventfs_mutex)) { 972 eventfs_remove_rec(ef_child, 0); 973 } 974 mutex_unlock(&eventfs_mutex); 975 } 976