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/mount.h> 18 #include <linux/pagemap.h> 19 #include <linux/init.h> 20 #include <linux/kobject.h> 21 #include <linux/namei.h> 22 #include <linux/debugfs.h> 23 #include <linux/fsnotify.h> 24 #include <linux/string.h> 25 #include <linux/seq_file.h> 26 #include <linux/parser.h> 27 #include <linux/magic.h> 28 #include <linux/slab.h> 29 30 #include "internal.h" 31 32 #define DEBUGFS_DEFAULT_MODE 0700 33 34 static struct vfsmount *debugfs_mount; 35 static int debugfs_mount_count; 36 static bool debugfs_registered; 37 38 static struct inode *debugfs_get_inode(struct super_block *sb) 39 { 40 struct inode *inode = new_inode(sb); 41 if (inode) { 42 inode->i_ino = get_next_ino(); 43 inode->i_atime = inode->i_mtime = 44 inode->i_ctime = current_time(inode); 45 } 46 return inode; 47 } 48 49 struct debugfs_mount_opts { 50 kuid_t uid; 51 kgid_t gid; 52 umode_t mode; 53 }; 54 55 enum { 56 Opt_uid, 57 Opt_gid, 58 Opt_mode, 59 Opt_err 60 }; 61 62 static const match_table_t tokens = { 63 {Opt_uid, "uid=%u"}, 64 {Opt_gid, "gid=%u"}, 65 {Opt_mode, "mode=%o"}, 66 {Opt_err, NULL} 67 }; 68 69 struct debugfs_fs_info { 70 struct debugfs_mount_opts mount_opts; 71 }; 72 73 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 74 { 75 substring_t args[MAX_OPT_ARGS]; 76 int option; 77 int token; 78 kuid_t uid; 79 kgid_t gid; 80 char *p; 81 82 opts->mode = DEBUGFS_DEFAULT_MODE; 83 84 while ((p = strsep(&data, ",")) != NULL) { 85 if (!*p) 86 continue; 87 88 token = match_token(p, tokens, args); 89 switch (token) { 90 case Opt_uid: 91 if (match_int(&args[0], &option)) 92 return -EINVAL; 93 uid = make_kuid(current_user_ns(), option); 94 if (!uid_valid(uid)) 95 return -EINVAL; 96 opts->uid = uid; 97 break; 98 case Opt_gid: 99 if (match_int(&args[0], &option)) 100 return -EINVAL; 101 gid = make_kgid(current_user_ns(), option); 102 if (!gid_valid(gid)) 103 return -EINVAL; 104 opts->gid = gid; 105 break; 106 case Opt_mode: 107 if (match_octal(&args[0], &option)) 108 return -EINVAL; 109 opts->mode = option & S_IALLUGO; 110 break; 111 /* 112 * We might like to report bad mount options here; 113 * but traditionally debugfs has ignored all mount options 114 */ 115 } 116 } 117 118 return 0; 119 } 120 121 static int debugfs_apply_options(struct super_block *sb) 122 { 123 struct debugfs_fs_info *fsi = sb->s_fs_info; 124 struct inode *inode = d_inode(sb->s_root); 125 struct debugfs_mount_opts *opts = &fsi->mount_opts; 126 127 inode->i_mode &= ~S_IALLUGO; 128 inode->i_mode |= opts->mode; 129 130 inode->i_uid = opts->uid; 131 inode->i_gid = opts->gid; 132 133 return 0; 134 } 135 136 static int debugfs_remount(struct super_block *sb, int *flags, char *data) 137 { 138 int err; 139 struct debugfs_fs_info *fsi = sb->s_fs_info; 140 141 sync_filesystem(sb); 142 err = debugfs_parse_options(data, &fsi->mount_opts); 143 if (err) 144 goto fail; 145 146 debugfs_apply_options(sb); 147 148 fail: 149 return err; 150 } 151 152 static int debugfs_show_options(struct seq_file *m, struct dentry *root) 153 { 154 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 155 struct debugfs_mount_opts *opts = &fsi->mount_opts; 156 157 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 158 seq_printf(m, ",uid=%u", 159 from_kuid_munged(&init_user_ns, opts->uid)); 160 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 161 seq_printf(m, ",gid=%u", 162 from_kgid_munged(&init_user_ns, opts->gid)); 163 if (opts->mode != DEBUGFS_DEFAULT_MODE) 164 seq_printf(m, ",mode=%o", opts->mode); 165 166 return 0; 167 } 168 169 static void debugfs_free_inode(struct inode *inode) 170 { 171 if (S_ISLNK(inode->i_mode)) 172 kfree(inode->i_link); 173 free_inode_nonrcu(inode); 174 } 175 176 static const struct super_operations debugfs_super_operations = { 177 .statfs = simple_statfs, 178 .remount_fs = debugfs_remount, 179 .show_options = debugfs_show_options, 180 .free_inode = debugfs_free_inode, 181 }; 182 183 static void debugfs_release_dentry(struct dentry *dentry) 184 { 185 void *fsd = dentry->d_fsdata; 186 187 if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 188 kfree(dentry->d_fsdata); 189 } 190 191 static struct vfsmount *debugfs_automount(struct path *path) 192 { 193 debugfs_automount_t f; 194 f = (debugfs_automount_t)path->dentry->d_fsdata; 195 return f(path->dentry, d_inode(path->dentry)->i_private); 196 } 197 198 static const struct dentry_operations debugfs_dops = { 199 .d_delete = always_delete_dentry, 200 .d_release = debugfs_release_dentry, 201 .d_automount = debugfs_automount, 202 }; 203 204 static int debug_fill_super(struct super_block *sb, void *data, int silent) 205 { 206 static const struct tree_descr debug_files[] = {{""}}; 207 struct debugfs_fs_info *fsi; 208 int err; 209 210 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 211 sb->s_fs_info = fsi; 212 if (!fsi) { 213 err = -ENOMEM; 214 goto fail; 215 } 216 217 err = debugfs_parse_options(data, &fsi->mount_opts); 218 if (err) 219 goto fail; 220 221 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 222 if (err) 223 goto fail; 224 225 sb->s_op = &debugfs_super_operations; 226 sb->s_d_op = &debugfs_dops; 227 228 debugfs_apply_options(sb); 229 230 return 0; 231 232 fail: 233 kfree(fsi); 234 sb->s_fs_info = NULL; 235 return err; 236 } 237 238 static struct dentry *debug_mount(struct file_system_type *fs_type, 239 int flags, const char *dev_name, 240 void *data) 241 { 242 return mount_single(fs_type, flags, data, debug_fill_super); 243 } 244 245 static struct file_system_type debug_fs_type = { 246 .owner = THIS_MODULE, 247 .name = "debugfs", 248 .mount = debug_mount, 249 .kill_sb = kill_litter_super, 250 }; 251 MODULE_ALIAS_FS("debugfs"); 252 253 /** 254 * debugfs_lookup() - look up an existing debugfs file 255 * @name: a pointer to a string containing the name of the file to look up. 256 * @parent: a pointer to the parent dentry of the file. 257 * 258 * This function will return a pointer to a dentry if it succeeds. If the file 259 * doesn't exist or an error occurs, %NULL will be returned. The returned 260 * dentry must be passed to dput() when it is no longer needed. 261 * 262 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 263 * returned. 264 */ 265 struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 266 { 267 struct dentry *dentry; 268 269 if (IS_ERR(parent)) 270 return NULL; 271 272 if (!parent) 273 parent = debugfs_mount->mnt_root; 274 275 dentry = lookup_one_len_unlocked(name, parent, strlen(name)); 276 if (IS_ERR(dentry)) 277 return NULL; 278 if (!d_really_is_positive(dentry)) { 279 dput(dentry); 280 return NULL; 281 } 282 return dentry; 283 } 284 EXPORT_SYMBOL_GPL(debugfs_lookup); 285 286 static struct dentry *start_creating(const char *name, struct dentry *parent) 287 { 288 struct dentry *dentry; 289 int error; 290 291 pr_debug("creating file '%s'\n", name); 292 293 if (IS_ERR(parent)) 294 return parent; 295 296 error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 297 &debugfs_mount_count); 298 if (error) { 299 pr_err("Unable to pin filesystem for file '%s'\n", name); 300 return ERR_PTR(error); 301 } 302 303 /* If the parent is not specified, we create it in the root. 304 * We need the root dentry to do this, which is in the super 305 * block. A pointer to that is in the struct vfsmount that we 306 * have around. 307 */ 308 if (!parent) 309 parent = debugfs_mount->mnt_root; 310 311 inode_lock(d_inode(parent)); 312 dentry = lookup_one_len(name, parent, strlen(name)); 313 if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 314 dput(dentry); 315 pr_err("File '%s' already present!\n", name); 316 dentry = ERR_PTR(-EEXIST); 317 } 318 319 if (IS_ERR(dentry)) { 320 inode_unlock(d_inode(parent)); 321 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 322 } 323 324 return dentry; 325 } 326 327 static struct dentry *failed_creating(struct dentry *dentry) 328 { 329 inode_unlock(d_inode(dentry->d_parent)); 330 dput(dentry); 331 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 332 return ERR_PTR(-ENOMEM); 333 } 334 335 static struct dentry *end_creating(struct dentry *dentry) 336 { 337 inode_unlock(d_inode(dentry->d_parent)); 338 return dentry; 339 } 340 341 static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 342 struct dentry *parent, void *data, 343 const struct file_operations *proxy_fops, 344 const struct file_operations *real_fops) 345 { 346 struct dentry *dentry; 347 struct inode *inode; 348 349 if (!(mode & S_IFMT)) 350 mode |= S_IFREG; 351 BUG_ON(!S_ISREG(mode)); 352 dentry = start_creating(name, parent); 353 354 if (IS_ERR(dentry)) 355 return dentry; 356 357 inode = debugfs_get_inode(dentry->d_sb); 358 if (unlikely(!inode)) { 359 pr_err("out of free dentries, can not create file '%s'\n", 360 name); 361 return failed_creating(dentry); 362 } 363 364 inode->i_mode = mode; 365 inode->i_private = data; 366 367 inode->i_fop = proxy_fops; 368 dentry->d_fsdata = (void *)((unsigned long)real_fops | 369 DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 370 371 d_instantiate(dentry, inode); 372 fsnotify_create(d_inode(dentry->d_parent), dentry); 373 return end_creating(dentry); 374 } 375 376 /** 377 * debugfs_create_file - create a file in the debugfs filesystem 378 * @name: a pointer to a string containing the name of the file to create. 379 * @mode: the permission that the file should have. 380 * @parent: a pointer to the parent dentry for this file. This should be a 381 * directory dentry if set. If this parameter is NULL, then the 382 * file will be created in the root of the debugfs filesystem. 383 * @data: a pointer to something that the caller will want to get to later 384 * on. The inode.i_private pointer will point to this value on 385 * the open() call. 386 * @fops: a pointer to a struct file_operations that should be used for 387 * this file. 388 * 389 * This is the basic "create a file" function for debugfs. It allows for a 390 * wide range of flexibility in creating a file, or a directory (if you want 391 * to create a directory, the debugfs_create_dir() function is 392 * recommended to be used instead.) 393 * 394 * This function will return a pointer to a dentry if it succeeds. This 395 * pointer must be passed to the debugfs_remove() function when the file is 396 * to be removed (no automatic cleanup happens if your module is unloaded, 397 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 398 * returned. 399 * 400 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 401 * returned. 402 */ 403 struct dentry *debugfs_create_file(const char *name, umode_t mode, 404 struct dentry *parent, void *data, 405 const struct file_operations *fops) 406 { 407 408 return __debugfs_create_file(name, mode, parent, data, 409 fops ? &debugfs_full_proxy_file_operations : 410 &debugfs_noop_file_operations, 411 fops); 412 } 413 EXPORT_SYMBOL_GPL(debugfs_create_file); 414 415 /** 416 * debugfs_create_file_unsafe - create a file in the debugfs filesystem 417 * @name: a pointer to a string containing the name of the file to create. 418 * @mode: the permission that the file should have. 419 * @parent: a pointer to the parent dentry for this file. This should be a 420 * directory dentry if set. If this parameter is NULL, then the 421 * file will be created in the root of the debugfs filesystem. 422 * @data: a pointer to something that the caller will want to get to later 423 * on. The inode.i_private pointer will point to this value on 424 * the open() call. 425 * @fops: a pointer to a struct file_operations that should be used for 426 * this file. 427 * 428 * debugfs_create_file_unsafe() is completely analogous to 429 * debugfs_create_file(), the only difference being that the fops 430 * handed it will not get protected against file removals by the 431 * debugfs core. 432 * 433 * It is your responsibility to protect your struct file_operation 434 * methods against file removals by means of debugfs_file_get() 435 * and debugfs_file_put(). ->open() is still protected by 436 * debugfs though. 437 * 438 * Any struct file_operations defined by means of 439 * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 440 * thus, may be used here. 441 */ 442 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 443 struct dentry *parent, void *data, 444 const struct file_operations *fops) 445 { 446 447 return __debugfs_create_file(name, mode, parent, data, 448 fops ? &debugfs_open_proxy_file_operations : 449 &debugfs_noop_file_operations, 450 fops); 451 } 452 EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 453 454 /** 455 * debugfs_create_file_size - create a file in the debugfs filesystem 456 * @name: a pointer to a string containing the name of the file to create. 457 * @mode: the permission that the file should have. 458 * @parent: a pointer to the parent dentry for this file. This should be a 459 * directory dentry if set. If this parameter is NULL, then the 460 * file will be created in the root of the debugfs filesystem. 461 * @data: a pointer to something that the caller will want to get to later 462 * on. The inode.i_private pointer will point to this value on 463 * the open() call. 464 * @fops: a pointer to a struct file_operations that should be used for 465 * this file. 466 * @file_size: initial file size 467 * 468 * This is the basic "create a file" function for debugfs. It allows for a 469 * wide range of flexibility in creating a file, or a directory (if you want 470 * to create a directory, the debugfs_create_dir() function is 471 * recommended to be used instead.) 472 * 473 * This function will return a pointer to a dentry if it succeeds. This 474 * pointer must be passed to the debugfs_remove() function when the file is 475 * to be removed (no automatic cleanup happens if your module is unloaded, 476 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 477 * returned. 478 * 479 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 480 * returned. 481 */ 482 struct dentry *debugfs_create_file_size(const char *name, umode_t mode, 483 struct dentry *parent, void *data, 484 const struct file_operations *fops, 485 loff_t file_size) 486 { 487 struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 488 489 if (de) 490 d_inode(de)->i_size = file_size; 491 return de; 492 } 493 EXPORT_SYMBOL_GPL(debugfs_create_file_size); 494 495 /** 496 * debugfs_create_dir - create a directory in the debugfs filesystem 497 * @name: a pointer to a string containing the name of the directory to 498 * create. 499 * @parent: a pointer to the parent dentry for this file. This should be a 500 * directory dentry if set. If this parameter is NULL, then the 501 * directory will be created in the root of the debugfs filesystem. 502 * 503 * This function creates a directory in debugfs with the given name. 504 * 505 * This function will return a pointer to a dentry if it succeeds. This 506 * pointer must be passed to the debugfs_remove() function when the file is 507 * to be removed (no automatic cleanup happens if your module is unloaded, 508 * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 509 * returned. 510 * 511 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 512 * returned. 513 */ 514 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 515 { 516 struct dentry *dentry = start_creating(name, parent); 517 struct inode *inode; 518 519 if (IS_ERR(dentry)) 520 return dentry; 521 522 inode = debugfs_get_inode(dentry->d_sb); 523 if (unlikely(!inode)) { 524 pr_err("out of free dentries, can not create directory '%s'\n", 525 name); 526 return failed_creating(dentry); 527 } 528 529 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 530 inode->i_op = &simple_dir_inode_operations; 531 inode->i_fop = &simple_dir_operations; 532 533 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 534 inc_nlink(inode); 535 d_instantiate(dentry, inode); 536 inc_nlink(d_inode(dentry->d_parent)); 537 fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 538 return end_creating(dentry); 539 } 540 EXPORT_SYMBOL_GPL(debugfs_create_dir); 541 542 /** 543 * debugfs_create_automount - create automount point in the debugfs filesystem 544 * @name: a pointer to a string containing the name of the file to create. 545 * @parent: a pointer to the parent dentry for this file. This should be a 546 * directory dentry if set. If this parameter is NULL, then the 547 * file will be created in the root of the debugfs filesystem. 548 * @f: function to be called when pathname resolution steps on that one. 549 * @data: opaque argument to pass to f(). 550 * 551 * @f should return what ->d_automount() would. 552 */ 553 struct dentry *debugfs_create_automount(const char *name, 554 struct dentry *parent, 555 debugfs_automount_t f, 556 void *data) 557 { 558 struct dentry *dentry = start_creating(name, parent); 559 struct inode *inode; 560 561 if (IS_ERR(dentry)) 562 return dentry; 563 564 inode = debugfs_get_inode(dentry->d_sb); 565 if (unlikely(!inode)) { 566 pr_err("out of free dentries, can not create automount '%s'\n", 567 name); 568 return failed_creating(dentry); 569 } 570 571 make_empty_dir_inode(inode); 572 inode->i_flags |= S_AUTOMOUNT; 573 inode->i_private = data; 574 dentry->d_fsdata = (void *)f; 575 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 576 inc_nlink(inode); 577 d_instantiate(dentry, inode); 578 inc_nlink(d_inode(dentry->d_parent)); 579 fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 580 return end_creating(dentry); 581 } 582 EXPORT_SYMBOL(debugfs_create_automount); 583 584 /** 585 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 586 * @name: a pointer to a string containing the name of the symbolic link to 587 * create. 588 * @parent: a pointer to the parent dentry for this symbolic link. This 589 * should be a directory dentry if set. If this parameter is NULL, 590 * then the symbolic link will be created in the root of the debugfs 591 * filesystem. 592 * @target: a pointer to a string containing the path to the target of the 593 * symbolic link. 594 * 595 * This function creates a symbolic link with the given name in debugfs that 596 * links to the given target path. 597 * 598 * This function will return a pointer to a dentry if it succeeds. This 599 * pointer must be passed to the debugfs_remove() function when the symbolic 600 * link is to be removed (no automatic cleanup happens if your module is 601 * unloaded, you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) 602 * will be returned. 603 * 604 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 605 * returned. 606 */ 607 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 608 const char *target) 609 { 610 struct dentry *dentry; 611 struct inode *inode; 612 char *link = kstrdup(target, GFP_KERNEL); 613 if (!link) 614 return ERR_PTR(-ENOMEM); 615 616 dentry = start_creating(name, parent); 617 if (IS_ERR(dentry)) { 618 kfree(link); 619 return dentry; 620 } 621 622 inode = debugfs_get_inode(dentry->d_sb); 623 if (unlikely(!inode)) { 624 pr_err("out of free dentries, can not create symlink '%s'\n", 625 name); 626 kfree(link); 627 return failed_creating(dentry); 628 } 629 inode->i_mode = S_IFLNK | S_IRWXUGO; 630 inode->i_op = &simple_symlink_inode_operations; 631 inode->i_link = link; 632 d_instantiate(dentry, inode); 633 return end_creating(dentry); 634 } 635 EXPORT_SYMBOL_GPL(debugfs_create_symlink); 636 637 static void __debugfs_remove_file(struct dentry *dentry, struct dentry *parent) 638 { 639 struct debugfs_fsdata *fsd; 640 641 simple_unlink(d_inode(parent), dentry); 642 d_delete(dentry); 643 644 /* 645 * Paired with the closing smp_mb() implied by a successful 646 * cmpxchg() in debugfs_file_get(): either 647 * debugfs_file_get() must see a dead dentry or we must see a 648 * debugfs_fsdata instance at ->d_fsdata here (or both). 649 */ 650 smp_mb(); 651 fsd = READ_ONCE(dentry->d_fsdata); 652 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 653 return; 654 if (!refcount_dec_and_test(&fsd->active_users)) 655 wait_for_completion(&fsd->active_users_drained); 656 } 657 658 static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 659 { 660 int ret = 0; 661 662 if (simple_positive(dentry)) { 663 dget(dentry); 664 if (!d_is_reg(dentry)) { 665 if (d_is_dir(dentry)) 666 ret = simple_rmdir(d_inode(parent), dentry); 667 else 668 simple_unlink(d_inode(parent), dentry); 669 if (!ret) 670 d_delete(dentry); 671 } else { 672 __debugfs_remove_file(dentry, parent); 673 } 674 dput(dentry); 675 } 676 return ret; 677 } 678 679 /** 680 * debugfs_remove - removes a file or directory from the debugfs filesystem 681 * @dentry: a pointer to a the dentry of the file or directory to be 682 * removed. If this parameter is NULL or an error value, nothing 683 * will be done. 684 * 685 * This function removes a file or directory in debugfs that was previously 686 * created with a call to another debugfs function (like 687 * debugfs_create_file() or variants thereof.) 688 * 689 * This function is required to be called in order for the file to be 690 * removed, no automatic cleanup of files will happen when a module is 691 * removed, you are responsible here. 692 */ 693 void debugfs_remove(struct dentry *dentry) 694 { 695 struct dentry *parent; 696 int ret; 697 698 if (IS_ERR_OR_NULL(dentry)) 699 return; 700 701 parent = dentry->d_parent; 702 inode_lock(d_inode(parent)); 703 ret = __debugfs_remove(dentry, parent); 704 inode_unlock(d_inode(parent)); 705 if (!ret) 706 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 707 } 708 EXPORT_SYMBOL_GPL(debugfs_remove); 709 710 /** 711 * debugfs_remove_recursive - recursively removes a directory 712 * @dentry: a pointer to a the dentry of the directory to be removed. If this 713 * parameter is NULL or an error value, nothing will be done. 714 * 715 * This function recursively removes a directory tree in debugfs that 716 * was previously created with a call to another debugfs function 717 * (like debugfs_create_file() or variants thereof.) 718 * 719 * This function is required to be called in order for the file to be 720 * removed, no automatic cleanup of files will happen when a module is 721 * removed, you are responsible here. 722 */ 723 void debugfs_remove_recursive(struct dentry *dentry) 724 { 725 struct dentry *child, *parent; 726 727 if (IS_ERR_OR_NULL(dentry)) 728 return; 729 730 parent = dentry; 731 down: 732 inode_lock(d_inode(parent)); 733 loop: 734 /* 735 * The parent->d_subdirs is protected by the d_lock. Outside that 736 * lock, the child can be unlinked and set to be freed which can 737 * use the d_u.d_child as the rcu head and corrupt this list. 738 */ 739 spin_lock(&parent->d_lock); 740 list_for_each_entry(child, &parent->d_subdirs, d_child) { 741 if (!simple_positive(child)) 742 continue; 743 744 /* perhaps simple_empty(child) makes more sense */ 745 if (!list_empty(&child->d_subdirs)) { 746 spin_unlock(&parent->d_lock); 747 inode_unlock(d_inode(parent)); 748 parent = child; 749 goto down; 750 } 751 752 spin_unlock(&parent->d_lock); 753 754 if (!__debugfs_remove(child, parent)) 755 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 756 757 /* 758 * The parent->d_lock protects agaist child from unlinking 759 * from d_subdirs. When releasing the parent->d_lock we can 760 * no longer trust that the next pointer is valid. 761 * Restart the loop. We'll skip this one with the 762 * simple_positive() check. 763 */ 764 goto loop; 765 } 766 spin_unlock(&parent->d_lock); 767 768 inode_unlock(d_inode(parent)); 769 child = parent; 770 parent = parent->d_parent; 771 inode_lock(d_inode(parent)); 772 773 if (child != dentry) 774 /* go up */ 775 goto loop; 776 777 if (!__debugfs_remove(child, parent)) 778 simple_release_fs(&debugfs_mount, &debugfs_mount_count); 779 inode_unlock(d_inode(parent)); 780 } 781 EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 782 783 /** 784 * debugfs_rename - rename a file/directory in the debugfs filesystem 785 * @old_dir: a pointer to the parent dentry for the renamed object. This 786 * should be a directory dentry. 787 * @old_dentry: dentry of an object to be renamed. 788 * @new_dir: a pointer to the parent dentry where the object should be 789 * moved. This should be a directory dentry. 790 * @new_name: a pointer to a string containing the target name. 791 * 792 * This function renames a file/directory in debugfs. The target must not 793 * exist for rename to succeed. 794 * 795 * This function will return a pointer to old_dentry (which is updated to 796 * reflect renaming) if it succeeds. If an error occurs, %NULL will be 797 * returned. 798 * 799 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 800 * returned. 801 */ 802 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 803 struct dentry *new_dir, const char *new_name) 804 { 805 int error; 806 struct dentry *dentry = NULL, *trap; 807 struct name_snapshot old_name; 808 809 if (IS_ERR(old_dir)) 810 return old_dir; 811 if (IS_ERR(new_dir)) 812 return new_dir; 813 if (IS_ERR_OR_NULL(old_dentry)) 814 return old_dentry; 815 816 trap = lock_rename(new_dir, old_dir); 817 /* Source or destination directories don't exist? */ 818 if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 819 goto exit; 820 /* Source does not exist, cyclic rename, or mountpoint? */ 821 if (d_really_is_negative(old_dentry) || old_dentry == trap || 822 d_mountpoint(old_dentry)) 823 goto exit; 824 dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 825 /* Lookup failed, cyclic rename or target exists? */ 826 if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 827 goto exit; 828 829 take_dentry_name_snapshot(&old_name, old_dentry); 830 831 error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 832 dentry, 0); 833 if (error) { 834 release_dentry_name_snapshot(&old_name); 835 goto exit; 836 } 837 d_move(old_dentry, dentry); 838 fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 839 d_is_dir(old_dentry), 840 NULL, old_dentry); 841 release_dentry_name_snapshot(&old_name); 842 unlock_rename(new_dir, old_dir); 843 dput(dentry); 844 return old_dentry; 845 exit: 846 if (dentry && !IS_ERR(dentry)) 847 dput(dentry); 848 unlock_rename(new_dir, old_dir); 849 if (IS_ERR(dentry)) 850 return dentry; 851 return ERR_PTR(-EINVAL); 852 } 853 EXPORT_SYMBOL_GPL(debugfs_rename); 854 855 /** 856 * debugfs_initialized - Tells whether debugfs has been registered 857 */ 858 bool debugfs_initialized(void) 859 { 860 return debugfs_registered; 861 } 862 EXPORT_SYMBOL_GPL(debugfs_initialized); 863 864 static int __init debugfs_init(void) 865 { 866 int retval; 867 868 retval = sysfs_create_mount_point(kernel_kobj, "debug"); 869 if (retval) 870 return retval; 871 872 retval = register_filesystem(&debug_fs_type); 873 if (retval) 874 sysfs_remove_mount_point(kernel_kobj, "debug"); 875 else 876 debugfs_registered = true; 877 878 return retval; 879 } 880 core_initcall(debugfs_init); 881 882