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