1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * inode.c - part of debugfs, a tiny little debug file system 4 * 5 * Copyright (C) 2004,2019 Greg Kroah-Hartman <greg@kroah.com> 6 * Copyright (C) 2004 IBM Inc. 7 * Copyright (C) 2019 Linux Foundation <gregkh@linuxfoundation.org> 8 * 9 * debugfs is for people to use instead of /proc or /sys. 10 * See ./Documentation/core-api/kernel-api.rst for more details. 11 */ 12 13 #define pr_fmt(fmt) "debugfs: " fmt 14 15 #include <linux/module.h> 16 #include <linux/fs.h> 17 #include <linux/fs_context.h> 18 #include <linux/fs_parser.h> 19 #include <linux/pagemap.h> 20 #include <linux/init.h> 21 #include <linux/kobject.h> 22 #include <linux/namei.h> 23 #include <linux/debugfs.h> 24 #include <linux/fsnotify.h> 25 #include <linux/string.h> 26 #include <linux/seq_file.h> 27 #include <linux/magic.h> 28 #include <linux/slab.h> 29 #include <linux/security.h> 30 31 #include "internal.h" 32 33 #define DEBUGFS_DEFAULT_MODE 0700 34 35 static struct vfsmount *debugfs_mount; 36 static int debugfs_mount_count; 37 static bool debugfs_registered; 38 static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS; 39 40 /* 41 * Don't allow access attributes to be changed whilst the kernel is locked down 42 * so that we can use the file mode as part of a heuristic to determine whether 43 * to lock down individual files. 44 */ 45 static int debugfs_setattr(struct mnt_idmap *idmap, 46 struct dentry *dentry, struct iattr *ia) 47 { 48 int ret; 49 50 if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) { 51 ret = security_locked_down(LOCKDOWN_DEBUGFS); 52 if (ret) 53 return ret; 54 } 55 return simple_setattr(&nop_mnt_idmap, dentry, ia); 56 } 57 58 static const struct inode_operations debugfs_file_inode_operations = { 59 .setattr = debugfs_setattr, 60 }; 61 static const struct inode_operations debugfs_dir_inode_operations = { 62 .lookup = simple_lookup, 63 .setattr = debugfs_setattr, 64 }; 65 static const struct inode_operations debugfs_symlink_inode_operations = { 66 .get_link = simple_get_link, 67 .setattr = debugfs_setattr, 68 }; 69 70 static struct inode *debugfs_get_inode(struct super_block *sb) 71 { 72 struct inode *inode = new_inode(sb); 73 if (inode) { 74 inode->i_ino = get_next_ino(); 75 simple_inode_init_ts(inode); 76 } 77 return inode; 78 } 79 80 struct debugfs_fs_info { 81 kuid_t uid; 82 kgid_t gid; 83 umode_t mode; 84 /* Opt_* bitfield. */ 85 unsigned int opts; 86 }; 87 88 enum { 89 Opt_uid, 90 Opt_gid, 91 Opt_mode, 92 Opt_source, 93 }; 94 95 static const struct fs_parameter_spec debugfs_param_specs[] = { 96 fsparam_gid ("gid", Opt_gid), 97 fsparam_u32oct ("mode", Opt_mode), 98 fsparam_uid ("uid", Opt_uid), 99 fsparam_string ("source", Opt_source), 100 {} 101 }; 102 103 static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 104 { 105 struct debugfs_fs_info *opts = fc->s_fs_info; 106 struct fs_parse_result result; 107 int opt; 108 109 opt = fs_parse(fc, debugfs_param_specs, param, &result); 110 if (opt < 0) { 111 /* 112 * We might like to report bad mount options here; but 113 * traditionally debugfs has ignored all mount options 114 */ 115 if (opt == -ENOPARAM) 116 return 0; 117 118 return opt; 119 } 120 121 switch (opt) { 122 case Opt_uid: 123 opts->uid = result.uid; 124 break; 125 case Opt_gid: 126 opts->gid = result.gid; 127 break; 128 case Opt_mode: 129 opts->mode = result.uint_32 & S_IALLUGO; 130 break; 131 case Opt_source: 132 if (fc->source) 133 return invalfc(fc, "Multiple sources specified"); 134 fc->source = param->string; 135 param->string = NULL; 136 break; 137 /* 138 * We might like to report bad mount options here; 139 * but traditionally debugfs has ignored all mount options 140 */ 141 } 142 143 opts->opts |= BIT(opt); 144 145 return 0; 146 } 147 148 static void _debugfs_apply_options(struct super_block *sb, bool remount) 149 { 150 struct debugfs_fs_info *fsi = sb->s_fs_info; 151 struct inode *inode = d_inode(sb->s_root); 152 153 /* 154 * On remount, only reset mode/uid/gid if they were provided as mount 155 * options. 156 */ 157 158 if (!remount || fsi->opts & BIT(Opt_mode)) { 159 inode->i_mode &= ~S_IALLUGO; 160 inode->i_mode |= fsi->mode; 161 } 162 163 if (!remount || fsi->opts & BIT(Opt_uid)) 164 inode->i_uid = fsi->uid; 165 166 if (!remount || fsi->opts & BIT(Opt_gid)) 167 inode->i_gid = fsi->gid; 168 } 169 170 static void debugfs_apply_options(struct super_block *sb) 171 { 172 _debugfs_apply_options(sb, false); 173 } 174 175 static void debugfs_apply_options_remount(struct super_block *sb) 176 { 177 _debugfs_apply_options(sb, true); 178 } 179 180 static int debugfs_reconfigure(struct fs_context *fc) 181 { 182 struct super_block *sb = fc->root->d_sb; 183 struct debugfs_fs_info *sb_opts = sb->s_fs_info; 184 struct debugfs_fs_info *new_opts = fc->s_fs_info; 185 186 sync_filesystem(sb); 187 188 /* structure copy of new mount options to sb */ 189 *sb_opts = *new_opts; 190 debugfs_apply_options_remount(sb); 191 192 return 0; 193 } 194 195 static int debugfs_show_options(struct seq_file *m, struct dentry *root) 196 { 197 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 198 199 if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID)) 200 seq_printf(m, ",uid=%u", 201 from_kuid_munged(&init_user_ns, fsi->uid)); 202 if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID)) 203 seq_printf(m, ",gid=%u", 204 from_kgid_munged(&init_user_ns, fsi->gid)); 205 if (fsi->mode != DEBUGFS_DEFAULT_MODE) 206 seq_printf(m, ",mode=%o", fsi->mode); 207 208 return 0; 209 } 210 211 static struct kmem_cache *debugfs_inode_cachep __ro_after_init; 212 213 static void init_once(void *foo) 214 { 215 struct debugfs_inode_info *info = foo; 216 inode_init_once(&info->vfs_inode); 217 } 218 219 static struct inode *debugfs_alloc_inode(struct super_block *sb) 220 { 221 struct debugfs_inode_info *info; 222 info = alloc_inode_sb(sb, debugfs_inode_cachep, GFP_KERNEL); 223 if (!info) 224 return NULL; 225 return &info->vfs_inode; 226 } 227 228 static void debugfs_free_inode(struct inode *inode) 229 { 230 if (S_ISLNK(inode->i_mode)) 231 kfree(inode->i_link); 232 kmem_cache_free(debugfs_inode_cachep, DEBUGFS_I(inode)); 233 } 234 235 static const struct super_operations debugfs_super_operations = { 236 .statfs = simple_statfs, 237 .show_options = debugfs_show_options, 238 .alloc_inode = debugfs_alloc_inode, 239 .free_inode = debugfs_free_inode, 240 }; 241 242 static void debugfs_release_dentry(struct dentry *dentry) 243 { 244 struct debugfs_fsdata *fsd = dentry->d_fsdata; 245 246 if (fsd) { 247 WARN_ON(!list_empty(&fsd->cancellations)); 248 mutex_destroy(&fsd->cancellations_mtx); 249 } 250 kfree(fsd); 251 } 252 253 static struct vfsmount *debugfs_automount(struct path *path) 254 { 255 struct inode *inode = path->dentry->d_inode; 256 257 return DEBUGFS_I(inode)->automount(path->dentry, inode->i_private); 258 } 259 260 static const struct dentry_operations debugfs_dops = { 261 .d_delete = always_delete_dentry, 262 .d_release = debugfs_release_dentry, 263 .d_automount = debugfs_automount, 264 }; 265 266 static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc) 267 { 268 static const struct tree_descr debug_files[] = {{""}}; 269 int err; 270 271 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 272 if (err) 273 return err; 274 275 sb->s_op = &debugfs_super_operations; 276 sb->s_d_op = &debugfs_dops; 277 278 debugfs_apply_options(sb); 279 280 return 0; 281 } 282 283 static int debugfs_get_tree(struct fs_context *fc) 284 { 285 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 286 return -EPERM; 287 288 return get_tree_single(fc, debugfs_fill_super); 289 } 290 291 static void debugfs_free_fc(struct fs_context *fc) 292 { 293 kfree(fc->s_fs_info); 294 } 295 296 static const struct fs_context_operations debugfs_context_ops = { 297 .free = debugfs_free_fc, 298 .parse_param = debugfs_parse_param, 299 .get_tree = debugfs_get_tree, 300 .reconfigure = debugfs_reconfigure, 301 }; 302 303 static int debugfs_init_fs_context(struct fs_context *fc) 304 { 305 struct debugfs_fs_info *fsi; 306 307 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 308 if (!fsi) 309 return -ENOMEM; 310 311 fsi->mode = DEBUGFS_DEFAULT_MODE; 312 313 fc->s_fs_info = fsi; 314 fc->ops = &debugfs_context_ops; 315 return 0; 316 } 317 318 static struct file_system_type debug_fs_type = { 319 .owner = THIS_MODULE, 320 .name = "debugfs", 321 .init_fs_context = debugfs_init_fs_context, 322 .parameters = debugfs_param_specs, 323 .kill_sb = kill_litter_super, 324 }; 325 MODULE_ALIAS_FS("debugfs"); 326 327 /** 328 * debugfs_lookup() - look up an existing debugfs file 329 * @name: a pointer to a string containing the name of the file to look up. 330 * @parent: a pointer to the parent dentry of the file. 331 * 332 * This function will return a pointer to a dentry if it succeeds. If the file 333 * doesn't exist or an error occurs, %NULL will be returned. The returned 334 * dentry must be passed to dput() when it is no longer needed. 335 * 336 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 337 * returned. 338 */ 339 struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 340 { 341 struct dentry *dentry; 342 343 if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 344 return NULL; 345 346 if (!parent) 347 parent = debugfs_mount->mnt_root; 348 349 dentry = lookup_noperm_positive_unlocked(&QSTR(name), parent); 350 if (IS_ERR(dentry)) 351 return NULL; 352 return dentry; 353 } 354 EXPORT_SYMBOL_GPL(debugfs_lookup); 355 356 static struct dentry *start_creating(const char *name, struct dentry *parent) 357 { 358 struct dentry *dentry; 359 int error; 360 361 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 362 return ERR_PTR(-EPERM); 363 364 if (!debugfs_initialized()) 365 return ERR_PTR(-ENOENT); 366 367 pr_debug("creating file '%s'\n", name); 368 369 if (IS_ERR(parent)) 370 return parent; 371 372 error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 373 &debugfs_mount_count); 374 if (error) { 375 pr_err("Unable to pin filesystem for file '%s'\n", name); 376 return ERR_PTR(error); 377 } 378 379 /* If the parent is not specified, we create it in the root. 380 * We need the root dentry to do this, which is in the super 381 * block. A pointer to that is in the struct vfsmount that we 382 * have around. 383 */ 384 if (!parent) 385 parent = debugfs_mount->mnt_root; 386 387 inode_lock(d_inode(parent)); 388 if (unlikely(IS_DEADDIR(d_inode(parent)))) 389 dentry = ERR_PTR(-ENOENT); 390 else 391 dentry = lookup_noperm(&QSTR(name), parent); 392 if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 393 if (d_is_dir(dentry)) 394 pr_err("Directory '%s' with parent '%s' already present!\n", 395 name, parent->d_name.name); 396 else 397 pr_err("File '%s' in directory '%s' already present!\n", 398 name, parent->d_name.name); 399 dput(dentry); 400 dentry = ERR_PTR(-EEXIST); 401 } 402 403 if (IS_ERR(dentry)) { 404 inode_unlock(d_inode(parent)); 405 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 406 } 407 408 return dentry; 409 } 410 411 static struct dentry *failed_creating(struct dentry *dentry) 412 { 413 inode_unlock(d_inode(dentry->d_parent)); 414 dput(dentry); 415 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 416 return ERR_PTR(-ENOMEM); 417 } 418 419 static struct dentry *end_creating(struct dentry *dentry) 420 { 421 inode_unlock(d_inode(dentry->d_parent)); 422 return dentry; 423 } 424 425 static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 426 struct dentry *parent, void *data, 427 const void *aux, 428 const struct file_operations *proxy_fops, 429 const void *real_fops) 430 { 431 struct dentry *dentry; 432 struct inode *inode; 433 434 if (!(mode & S_IFMT)) 435 mode |= S_IFREG; 436 BUG_ON(!S_ISREG(mode)); 437 dentry = start_creating(name, parent); 438 439 if (IS_ERR(dentry)) 440 return dentry; 441 442 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 443 failed_creating(dentry); 444 return ERR_PTR(-EPERM); 445 } 446 447 inode = debugfs_get_inode(dentry->d_sb); 448 if (unlikely(!inode)) { 449 pr_err("out of free dentries, can not create file '%s'\n", 450 name); 451 return failed_creating(dentry); 452 } 453 454 inode->i_mode = mode; 455 inode->i_private = data; 456 457 inode->i_op = &debugfs_file_inode_operations; 458 if (!real_fops) 459 proxy_fops = &debugfs_noop_file_operations; 460 inode->i_fop = proxy_fops; 461 DEBUGFS_I(inode)->raw = real_fops; 462 DEBUGFS_I(inode)->aux = aux; 463 464 d_instantiate(dentry, inode); 465 fsnotify_create(d_inode(dentry->d_parent), dentry); 466 return end_creating(dentry); 467 } 468 469 struct dentry *debugfs_create_file_full(const char *name, umode_t mode, 470 struct dentry *parent, void *data, 471 const void *aux, 472 const struct file_operations *fops) 473 { 474 return __debugfs_create_file(name, mode, parent, data, aux, 475 &debugfs_full_proxy_file_operations, 476 fops); 477 } 478 EXPORT_SYMBOL_GPL(debugfs_create_file_full); 479 480 struct dentry *debugfs_create_file_short(const char *name, umode_t mode, 481 struct dentry *parent, void *data, 482 const void *aux, 483 const struct debugfs_short_fops *fops) 484 { 485 return __debugfs_create_file(name, mode, parent, data, aux, 486 &debugfs_full_short_proxy_file_operations, 487 fops); 488 } 489 EXPORT_SYMBOL_GPL(debugfs_create_file_short); 490 491 /** 492 * debugfs_create_file_unsafe - create a file in the debugfs filesystem 493 * @name: a pointer to a string containing the name of the file to create. 494 * @mode: the permission that the file should have. 495 * @parent: a pointer to the parent dentry for this file. This should be a 496 * directory dentry if set. If this parameter is NULL, then the 497 * file will be created in the root of the debugfs filesystem. 498 * @data: a pointer to something that the caller will want to get to later 499 * on. The inode.i_private pointer will point to this value on 500 * the open() call. 501 * @fops: a pointer to a struct file_operations that should be used for 502 * this file. 503 * 504 * debugfs_create_file_unsafe() is completely analogous to 505 * debugfs_create_file(), the only difference being that the fops 506 * handed it will not get protected against file removals by the 507 * debugfs core. 508 * 509 * It is your responsibility to protect your struct file_operation 510 * methods against file removals by means of debugfs_file_get() 511 * and debugfs_file_put(). ->open() is still protected by 512 * debugfs though. 513 * 514 * Any struct file_operations defined by means of 515 * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 516 * thus, may be used here. 517 */ 518 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 519 struct dentry *parent, void *data, 520 const struct file_operations *fops) 521 { 522 523 return __debugfs_create_file(name, mode, parent, data, NULL, 524 &debugfs_open_proxy_file_operations, 525 fops); 526 } 527 EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 528 529 /** 530 * debugfs_create_file_size - create a file in the debugfs filesystem 531 * @name: a pointer to a string containing the name of the file to create. 532 * @mode: the permission that the file should have. 533 * @parent: a pointer to the parent dentry for this file. This should be a 534 * directory dentry if set. If this parameter is NULL, then the 535 * file will be created in the root of the debugfs filesystem. 536 * @data: a pointer to something that the caller will want to get to later 537 * on. The inode.i_private pointer will point to this value on 538 * the open() call. 539 * @fops: a pointer to a struct file_operations that should be used for 540 * this file. 541 * @file_size: initial file size 542 * 543 * This is the basic "create a file" function for debugfs. It allows for a 544 * wide range of flexibility in creating a file, or a directory (if you want 545 * to create a directory, the debugfs_create_dir() function is 546 * recommended to be used instead.) 547 */ 548 void debugfs_create_file_size(const char *name, umode_t mode, 549 struct dentry *parent, void *data, 550 const struct file_operations *fops, 551 loff_t file_size) 552 { 553 struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 554 555 if (!IS_ERR(de)) 556 d_inode(de)->i_size = file_size; 557 } 558 EXPORT_SYMBOL_GPL(debugfs_create_file_size); 559 560 /** 561 * debugfs_create_dir - create a directory in the debugfs filesystem 562 * @name: a pointer to a string containing the name of the directory to 563 * create. 564 * @parent: a pointer to the parent dentry for this file. This should be a 565 * directory dentry if set. If this parameter is NULL, then the 566 * directory will be created in the root of the debugfs filesystem. 567 * 568 * This function creates a directory in debugfs with the given name. 569 * 570 * This function will return a pointer to a dentry if it succeeds. This 571 * pointer must be passed to the debugfs_remove() function when the file is 572 * to be removed (no automatic cleanup happens if your module is unloaded, 573 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 574 * returned. 575 * 576 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 577 * returned. 578 * 579 * NOTE: it's expected that most callers should _ignore_ the errors returned 580 * by this function. Other debugfs functions handle the fact that the "dentry" 581 * passed to them could be an error and they don't crash in that case. 582 * Drivers should generally work fine even if debugfs fails to init anyway. 583 */ 584 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 585 { 586 struct dentry *dentry = start_creating(name, parent); 587 struct inode *inode; 588 589 if (IS_ERR(dentry)) 590 return dentry; 591 592 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 593 failed_creating(dentry); 594 return ERR_PTR(-EPERM); 595 } 596 597 inode = debugfs_get_inode(dentry->d_sb); 598 if (unlikely(!inode)) { 599 pr_err("out of free dentries, can not create directory '%s'\n", 600 name); 601 return failed_creating(dentry); 602 } 603 604 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 605 inode->i_op = &debugfs_dir_inode_operations; 606 inode->i_fop = &simple_dir_operations; 607 608 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 609 inc_nlink(inode); 610 d_instantiate(dentry, inode); 611 inc_nlink(d_inode(dentry->d_parent)); 612 fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 613 return end_creating(dentry); 614 } 615 EXPORT_SYMBOL_GPL(debugfs_create_dir); 616 617 /** 618 * debugfs_create_automount - create automount point in the debugfs filesystem 619 * @name: a pointer to a string containing the name of the file to create. 620 * @parent: a pointer to the parent dentry for this file. This should be a 621 * directory dentry if set. If this parameter is NULL, then the 622 * file will be created in the root of the debugfs filesystem. 623 * @f: function to be called when pathname resolution steps on that one. 624 * @data: opaque argument to pass to f(). 625 * 626 * @f should return what ->d_automount() would. 627 */ 628 struct dentry *debugfs_create_automount(const char *name, 629 struct dentry *parent, 630 debugfs_automount_t f, 631 void *data) 632 { 633 struct dentry *dentry = start_creating(name, parent); 634 struct inode *inode; 635 636 if (IS_ERR(dentry)) 637 return dentry; 638 639 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 640 failed_creating(dentry); 641 return ERR_PTR(-EPERM); 642 } 643 644 inode = debugfs_get_inode(dentry->d_sb); 645 if (unlikely(!inode)) { 646 pr_err("out of free dentries, can not create automount '%s'\n", 647 name); 648 return failed_creating(dentry); 649 } 650 651 make_empty_dir_inode(inode); 652 inode->i_flags |= S_AUTOMOUNT; 653 inode->i_private = data; 654 DEBUGFS_I(inode)->automount = f; 655 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 656 inc_nlink(inode); 657 d_instantiate(dentry, inode); 658 inc_nlink(d_inode(dentry->d_parent)); 659 fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 660 return end_creating(dentry); 661 } 662 EXPORT_SYMBOL(debugfs_create_automount); 663 664 /** 665 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 666 * @name: a pointer to a string containing the name of the symbolic link to 667 * create. 668 * @parent: a pointer to the parent dentry for this symbolic link. This 669 * should be a directory dentry if set. If this parameter is NULL, 670 * then the symbolic link will be created in the root of the debugfs 671 * filesystem. 672 * @target: a pointer to a string containing the path to the target of the 673 * symbolic link. 674 * 675 * This function creates a symbolic link with the given name in debugfs that 676 * links to the given target path. 677 * 678 * This function will return a pointer to a dentry if it succeeds. This 679 * pointer must be passed to the debugfs_remove() function when the symbolic 680 * link is to be removed (no automatic cleanup happens if your module is 681 * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 682 * will be returned. 683 * 684 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 685 * returned. 686 */ 687 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 688 const char *target) 689 { 690 struct dentry *dentry; 691 struct inode *inode; 692 char *link = kstrdup(target, GFP_KERNEL); 693 if (!link) 694 return ERR_PTR(-ENOMEM); 695 696 dentry = start_creating(name, parent); 697 if (IS_ERR(dentry)) { 698 kfree(link); 699 return dentry; 700 } 701 702 inode = debugfs_get_inode(dentry->d_sb); 703 if (unlikely(!inode)) { 704 pr_err("out of free dentries, can not create symlink '%s'\n", 705 name); 706 kfree(link); 707 return failed_creating(dentry); 708 } 709 inode->i_mode = S_IFLNK | S_IRWXUGO; 710 inode->i_op = &debugfs_symlink_inode_operations; 711 inode->i_link = link; 712 d_instantiate(dentry, inode); 713 return end_creating(dentry); 714 } 715 EXPORT_SYMBOL_GPL(debugfs_create_symlink); 716 717 static void __debugfs_file_removed(struct dentry *dentry) 718 { 719 struct debugfs_fsdata *fsd; 720 721 /* 722 * Paired with the closing smp_mb() implied by a successful 723 * cmpxchg() in debugfs_file_get(): either 724 * debugfs_file_get() must see a dead dentry or we must see a 725 * debugfs_fsdata instance at ->d_fsdata here (or both). 726 */ 727 smp_mb(); 728 fsd = READ_ONCE(dentry->d_fsdata); 729 if (!fsd) 730 return; 731 732 /* if this was the last reference, we're done */ 733 if (refcount_dec_and_test(&fsd->active_users)) 734 return; 735 736 /* 737 * If there's still a reference, the code that obtained it can 738 * be in different states: 739 * - The common case of not using cancellations, or already 740 * after debugfs_leave_cancellation(), where we just need 741 * to wait for debugfs_file_put() which signals the completion; 742 * - inside a cancellation section, i.e. between 743 * debugfs_enter_cancellation() and debugfs_leave_cancellation(), 744 * in which case we need to trigger the ->cancel() function, 745 * and then wait for debugfs_file_put() just like in the 746 * previous case; 747 * - before debugfs_enter_cancellation() (but obviously after 748 * debugfs_file_get()), in which case we may not see the 749 * cancellation in the list on the first round of the loop, 750 * but debugfs_enter_cancellation() signals the completion 751 * after adding it, so this code gets woken up to call the 752 * ->cancel() function. 753 */ 754 while (refcount_read(&fsd->active_users)) { 755 struct debugfs_cancellation *c; 756 757 /* 758 * Lock the cancellations. Note that the cancellations 759 * structs are meant to be on the stack, so we need to 760 * ensure we either use them here or don't touch them, 761 * and debugfs_leave_cancellation() will wait for this 762 * to be finished processing before exiting one. It may 763 * of course win and remove the cancellation, but then 764 * chances are we never even got into this bit, we only 765 * do if the refcount isn't zero already. 766 */ 767 mutex_lock(&fsd->cancellations_mtx); 768 while ((c = list_first_entry_or_null(&fsd->cancellations, 769 typeof(*c), list))) { 770 list_del_init(&c->list); 771 c->cancel(dentry, c->cancel_data); 772 } 773 mutex_unlock(&fsd->cancellations_mtx); 774 775 wait_for_completion(&fsd->active_users_drained); 776 } 777 } 778 779 static void remove_one(struct dentry *victim) 780 { 781 if (d_is_reg(victim)) 782 __debugfs_file_removed(victim); 783 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 784 } 785 786 /** 787 * debugfs_remove - recursively removes a directory 788 * @dentry: a pointer to a the dentry of the directory to be removed. If this 789 * parameter is NULL or an error value, nothing will be done. 790 * 791 * This function recursively removes a directory tree in debugfs that 792 * was previously created with a call to another debugfs function 793 * (like debugfs_create_file() or variants thereof.) 794 * 795 * This function is required to be called in order for the file to be 796 * removed, no automatic cleanup of files will happen when a module is 797 * removed, you are responsible here. 798 */ 799 void debugfs_remove(struct dentry *dentry) 800 { 801 if (IS_ERR_OR_NULL(dentry)) 802 return; 803 804 simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 805 simple_recursive_removal(dentry, remove_one); 806 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 807 } 808 EXPORT_SYMBOL_GPL(debugfs_remove); 809 810 /** 811 * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it 812 * @name: a pointer to a string containing the name of the item to look up. 813 * @parent: a pointer to the parent dentry of the item. 814 * 815 * This is the equlivant of doing something like 816 * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting 817 * handled for the directory being looked up. 818 */ 819 void debugfs_lookup_and_remove(const char *name, struct dentry *parent) 820 { 821 struct dentry *dentry; 822 823 dentry = debugfs_lookup(name, parent); 824 if (!dentry) 825 return; 826 827 debugfs_remove(dentry); 828 dput(dentry); 829 } 830 EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove); 831 832 /** 833 * debugfs_change_name - rename a file/directory in the debugfs filesystem 834 * @dentry: dentry of an object to be renamed. 835 * @fmt: format for new name 836 * 837 * This function renames a file/directory in debugfs. The target must not 838 * exist for rename to succeed. 839 * 840 * This function will return 0 on success and -E... on failure. 841 * 842 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 843 * returned. 844 */ 845 int __printf(2, 3) debugfs_change_name(struct dentry *dentry, const char *fmt, ...) 846 { 847 int error = 0; 848 const char *new_name; 849 struct name_snapshot old_name; 850 struct dentry *parent, *target; 851 struct inode *dir; 852 va_list ap; 853 854 if (IS_ERR_OR_NULL(dentry)) 855 return 0; 856 857 va_start(ap, fmt); 858 new_name = kvasprintf_const(GFP_KERNEL, fmt, ap); 859 va_end(ap); 860 if (!new_name) 861 return -ENOMEM; 862 863 parent = dget_parent(dentry); 864 dir = d_inode(parent); 865 inode_lock(dir); 866 867 take_dentry_name_snapshot(&old_name, dentry); 868 869 if (WARN_ON_ONCE(dentry->d_parent != parent)) { 870 error = -EINVAL; 871 goto out; 872 } 873 if (strcmp(old_name.name.name, new_name) == 0) 874 goto out; 875 target = lookup_noperm(&QSTR(new_name), parent); 876 if (IS_ERR(target)) { 877 error = PTR_ERR(target); 878 goto out; 879 } 880 if (d_really_is_positive(target)) { 881 dput(target); 882 error = -EINVAL; 883 goto out; 884 } 885 simple_rename_timestamp(dir, dentry, dir, target); 886 d_move(dentry, target); 887 dput(target); 888 fsnotify_move(dir, dir, &old_name.name, d_is_dir(dentry), NULL, dentry); 889 out: 890 release_dentry_name_snapshot(&old_name); 891 inode_unlock(dir); 892 dput(parent); 893 kfree_const(new_name); 894 return error; 895 } 896 EXPORT_SYMBOL_GPL(debugfs_change_name); 897 898 /** 899 * debugfs_initialized - Tells whether debugfs has been registered 900 */ 901 bool debugfs_initialized(void) 902 { 903 return debugfs_registered; 904 } 905 EXPORT_SYMBOL_GPL(debugfs_initialized); 906 907 static int __init debugfs_kernel(char *str) 908 { 909 if (str) { 910 if (!strcmp(str, "on")) 911 debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 912 else if (!strcmp(str, "no-mount")) 913 debugfs_allow = DEBUGFS_ALLOW_API; 914 else if (!strcmp(str, "off")) 915 debugfs_allow = 0; 916 } 917 918 return 0; 919 } 920 early_param("debugfs", debugfs_kernel); 921 static int __init debugfs_init(void) 922 { 923 int retval; 924 925 if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 926 return -EPERM; 927 928 retval = sysfs_create_mount_point(kernel_kobj, "debug"); 929 if (retval) 930 return retval; 931 932 debugfs_inode_cachep = kmem_cache_create("debugfs_inode_cache", 933 sizeof(struct debugfs_inode_info), 0, 934 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 935 init_once); 936 if (debugfs_inode_cachep == NULL) { 937 sysfs_remove_mount_point(kernel_kobj, "debug"); 938 return -ENOMEM; 939 } 940 941 retval = register_filesystem(&debug_fs_type); 942 if (retval) { // Really not going to happen 943 sysfs_remove_mount_point(kernel_kobj, "debug"); 944 kmem_cache_destroy(debugfs_inode_cachep); 945 return retval; 946 } 947 debugfs_registered = true; 948 return 0; 949 } 950 core_initcall(debugfs_init); 951