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