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