linux/fs/debugfs/inode.c
<<
>>
Prefs
   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
  34static struct vfsmount *debugfs_mount;
  35static int debugfs_mount_count;
  36static bool debugfs_registered;
  37
  38static 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
  49struct debugfs_mount_opts {
  50        kuid_t uid;
  51        kgid_t gid;
  52        umode_t mode;
  53};
  54
  55enum {
  56        Opt_uid,
  57        Opt_gid,
  58        Opt_mode,
  59        Opt_err
  60};
  61
  62static 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
  69struct debugfs_fs_info {
  70        struct debugfs_mount_opts mount_opts;
  71};
  72
  73static 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
 121static 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
 136static 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
 148fail:
 149        return err;
 150}
 151
 152static 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
 169static 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
 176static 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
 183static 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
 191static 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
 198static 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
 204static 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
 232fail:
 233        kfree(fsi);
 234        sb->s_fs_info = NULL;
 235        return err;
 236}
 237
 238static 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
 245static 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};
 251MODULE_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 */
 265struct 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}
 284EXPORT_SYMBOL_GPL(debugfs_lookup);
 285
 286static 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                if (d_is_dir(dentry))
 315                        pr_err("Directory '%s' with parent '%s' already present!\n",
 316                               name, parent->d_name.name);
 317                else
 318                        pr_err("File '%s' in directory '%s' already present!\n",
 319                               name, parent->d_name.name);
 320                dput(dentry);
 321                dentry = ERR_PTR(-EEXIST);
 322        }
 323
 324        if (IS_ERR(dentry)) {
 325                inode_unlock(d_inode(parent));
 326                simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 327        }
 328
 329        return dentry;
 330}
 331
 332static struct dentry *failed_creating(struct dentry *dentry)
 333{
 334        inode_unlock(d_inode(dentry->d_parent));
 335        dput(dentry);
 336        simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 337        return ERR_PTR(-ENOMEM);
 338}
 339
 340static struct dentry *end_creating(struct dentry *dentry)
 341{
 342        inode_unlock(d_inode(dentry->d_parent));
 343        return dentry;
 344}
 345
 346static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
 347                                struct dentry *parent, void *data,
 348                                const struct file_operations *proxy_fops,
 349                                const struct file_operations *real_fops)
 350{
 351        struct dentry *dentry;
 352        struct inode *inode;
 353
 354        if (!(mode & S_IFMT))
 355                mode |= S_IFREG;
 356        BUG_ON(!S_ISREG(mode));
 357        dentry = start_creating(name, parent);
 358
 359        if (IS_ERR(dentry))
 360                return dentry;
 361
 362        inode = debugfs_get_inode(dentry->d_sb);
 363        if (unlikely(!inode)) {
 364                pr_err("out of free dentries, can not create file '%s'\n",
 365                       name);
 366                return failed_creating(dentry);
 367        }
 368
 369        inode->i_mode = mode;
 370        inode->i_private = data;
 371
 372        inode->i_fop = proxy_fops;
 373        dentry->d_fsdata = (void *)((unsigned long)real_fops |
 374                                DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
 375
 376        d_instantiate(dentry, inode);
 377        fsnotify_create(d_inode(dentry->d_parent), dentry);
 378        return end_creating(dentry);
 379}
 380
 381/**
 382 * debugfs_create_file - create a file in the debugfs filesystem
 383 * @name: a pointer to a string containing the name of the file to create.
 384 * @mode: the permission that the file should have.
 385 * @parent: a pointer to the parent dentry for this file.  This should be a
 386 *          directory dentry if set.  If this parameter is NULL, then the
 387 *          file will be created in the root of the debugfs filesystem.
 388 * @data: a pointer to something that the caller will want to get to later
 389 *        on.  The inode.i_private pointer will point to this value on
 390 *        the open() call.
 391 * @fops: a pointer to a struct file_operations that should be used for
 392 *        this file.
 393 *
 394 * This is the basic "create a file" function for debugfs.  It allows for a
 395 * wide range of flexibility in creating a file, or a directory (if you want
 396 * to create a directory, the debugfs_create_dir() function is
 397 * recommended to be used instead.)
 398 *
 399 * This function will return a pointer to a dentry if it succeeds.  This
 400 * pointer must be passed to the debugfs_remove() function when the file is
 401 * to be removed (no automatic cleanup happens if your module is unloaded,
 402 * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
 403 * returned.
 404 *
 405 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
 406 * returned.
 407 */
 408struct dentry *debugfs_create_file(const char *name, umode_t mode,
 409                                   struct dentry *parent, void *data,
 410                                   const struct file_operations *fops)
 411{
 412
 413        return __debugfs_create_file(name, mode, parent, data,
 414                                fops ? &debugfs_full_proxy_file_operations :
 415                                        &debugfs_noop_file_operations,
 416                                fops);
 417}
 418EXPORT_SYMBOL_GPL(debugfs_create_file);
 419
 420/**
 421 * debugfs_create_file_unsafe - create a file in the debugfs filesystem
 422 * @name: a pointer to a string containing the name of the file to create.
 423 * @mode: the permission that the file should have.
 424 * @parent: a pointer to the parent dentry for this file.  This should be a
 425 *          directory dentry if set.  If this parameter is NULL, then the
 426 *          file will be created in the root of the debugfs filesystem.
 427 * @data: a pointer to something that the caller will want to get to later
 428 *        on.  The inode.i_private pointer will point to this value on
 429 *        the open() call.
 430 * @fops: a pointer to a struct file_operations that should be used for
 431 *        this file.
 432 *
 433 * debugfs_create_file_unsafe() is completely analogous to
 434 * debugfs_create_file(), the only difference being that the fops
 435 * handed it will not get protected against file removals by the
 436 * debugfs core.
 437 *
 438 * It is your responsibility to protect your struct file_operation
 439 * methods against file removals by means of debugfs_file_get()
 440 * and debugfs_file_put(). ->open() is still protected by
 441 * debugfs though.
 442 *
 443 * Any struct file_operations defined by means of
 444 * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
 445 * thus, may be used here.
 446 */
 447struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
 448                                   struct dentry *parent, void *data,
 449                                   const struct file_operations *fops)
 450{
 451
 452        return __debugfs_create_file(name, mode, parent, data,
 453                                fops ? &debugfs_open_proxy_file_operations :
 454                                        &debugfs_noop_file_operations,
 455                                fops);
 456}
 457EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
 458
 459/**
 460 * debugfs_create_file_size - create a file in the debugfs filesystem
 461 * @name: a pointer to a string containing the name of the file to create.
 462 * @mode: the permission that the file should have.
 463 * @parent: a pointer to the parent dentry for this file.  This should be a
 464 *          directory dentry if set.  If this parameter is NULL, then the
 465 *          file will be created in the root of the debugfs filesystem.
 466 * @data: a pointer to something that the caller will want to get to later
 467 *        on.  The inode.i_private pointer will point to this value on
 468 *        the open() call.
 469 * @fops: a pointer to a struct file_operations that should be used for
 470 *        this file.
 471 * @file_size: initial file size
 472 *
 473 * This is the basic "create a file" function for debugfs.  It allows for a
 474 * wide range of flexibility in creating a file, or a directory (if you want
 475 * to create a directory, the debugfs_create_dir() function is
 476 * recommended to be used instead.)
 477 *
 478 * This function will return a pointer to a dentry if it succeeds.  This
 479 * pointer must be passed to the debugfs_remove() function when the file is
 480 * to be removed (no automatic cleanup happens if your module is unloaded,
 481 * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
 482 * returned.
 483 *
 484 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
 485 * returned.
 486 */
 487struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
 488                                        struct dentry *parent, void *data,
 489                                        const struct file_operations *fops,
 490                                        loff_t file_size)
 491{
 492        struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
 493
 494        if (de)
 495                d_inode(de)->i_size = file_size;
 496        return de;
 497}
 498EXPORT_SYMBOL_GPL(debugfs_create_file_size);
 499
 500/**
 501 * debugfs_create_dir - create a directory in the debugfs filesystem
 502 * @name: a pointer to a string containing the name of the directory to
 503 *        create.
 504 * @parent: a pointer to the parent dentry for this file.  This should be a
 505 *          directory dentry if set.  If this parameter is NULL, then the
 506 *          directory will be created in the root of the debugfs filesystem.
 507 *
 508 * This function creates a directory in debugfs with the given name.
 509 *
 510 * This function will return a pointer to a dentry if it succeeds.  This
 511 * pointer must be passed to the debugfs_remove() function when the file is
 512 * to be removed (no automatic cleanup happens if your module is unloaded,
 513 * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
 514 * returned.
 515 *
 516 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
 517 * returned.
 518 */
 519struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 520{
 521        struct dentry *dentry = start_creating(name, parent);
 522        struct inode *inode;
 523
 524        if (IS_ERR(dentry))
 525                return dentry;
 526
 527        inode = debugfs_get_inode(dentry->d_sb);
 528        if (unlikely(!inode)) {
 529                pr_err("out of free dentries, can not create directory '%s'\n",
 530                       name);
 531                return failed_creating(dentry);
 532        }
 533
 534        inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
 535        inode->i_op = &simple_dir_inode_operations;
 536        inode->i_fop = &simple_dir_operations;
 537
 538        /* directory inodes start off with i_nlink == 2 (for "." entry) */
 539        inc_nlink(inode);
 540        d_instantiate(dentry, inode);
 541        inc_nlink(d_inode(dentry->d_parent));
 542        fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
 543        return end_creating(dentry);
 544}
 545EXPORT_SYMBOL_GPL(debugfs_create_dir);
 546
 547/**
 548 * debugfs_create_automount - create automount point in the debugfs filesystem
 549 * @name: a pointer to a string containing the name of the file to create.
 550 * @parent: a pointer to the parent dentry for this file.  This should be a
 551 *          directory dentry if set.  If this parameter is NULL, then the
 552 *          file will be created in the root of the debugfs filesystem.
 553 * @f: function to be called when pathname resolution steps on that one.
 554 * @data: opaque argument to pass to f().
 555 *
 556 * @f should return what ->d_automount() would.
 557 */
 558struct dentry *debugfs_create_automount(const char *name,
 559                                        struct dentry *parent,
 560                                        debugfs_automount_t f,
 561                                        void *data)
 562{
 563        struct dentry *dentry = start_creating(name, parent);
 564        struct inode *inode;
 565
 566        if (IS_ERR(dentry))
 567                return dentry;
 568
 569        inode = debugfs_get_inode(dentry->d_sb);
 570        if (unlikely(!inode)) {
 571                pr_err("out of free dentries, can not create automount '%s'\n",
 572                       name);
 573                return failed_creating(dentry);
 574        }
 575
 576        make_empty_dir_inode(inode);
 577        inode->i_flags |= S_AUTOMOUNT;
 578        inode->i_private = data;
 579        dentry->d_fsdata = (void *)f;
 580        /* directory inodes start off with i_nlink == 2 (for "." entry) */
 581        inc_nlink(inode);
 582        d_instantiate(dentry, inode);
 583        inc_nlink(d_inode(dentry->d_parent));
 584        fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
 585        return end_creating(dentry);
 586}
 587EXPORT_SYMBOL(debugfs_create_automount);
 588
 589/**
 590 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
 591 * @name: a pointer to a string containing the name of the symbolic link to
 592 *        create.
 593 * @parent: a pointer to the parent dentry for this symbolic link.  This
 594 *          should be a directory dentry if set.  If this parameter is NULL,
 595 *          then the symbolic link will be created in the root of the debugfs
 596 *          filesystem.
 597 * @target: a pointer to a string containing the path to the target of the
 598 *          symbolic link.
 599 *
 600 * This function creates a symbolic link with the given name in debugfs that
 601 * links to the given target path.
 602 *
 603 * This function will return a pointer to a dentry if it succeeds.  This
 604 * pointer must be passed to the debugfs_remove() function when the symbolic
 605 * link is to be removed (no automatic cleanup happens if your module is
 606 * unloaded, you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR)
 607 * will be returned.
 608 *
 609 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
 610 * returned.
 611 */
 612struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 613                                      const char *target)
 614{
 615        struct dentry *dentry;
 616        struct inode *inode;
 617        char *link = kstrdup(target, GFP_KERNEL);
 618        if (!link)
 619                return ERR_PTR(-ENOMEM);
 620
 621        dentry = start_creating(name, parent);
 622        if (IS_ERR(dentry)) {
 623                kfree(link);
 624                return dentry;
 625        }
 626
 627        inode = debugfs_get_inode(dentry->d_sb);
 628        if (unlikely(!inode)) {
 629                pr_err("out of free dentries, can not create symlink '%s'\n",
 630                       name);
 631                kfree(link);
 632                return failed_creating(dentry);
 633        }
 634        inode->i_mode = S_IFLNK | S_IRWXUGO;
 635        inode->i_op = &simple_symlink_inode_operations;
 636        inode->i_link = link;
 637        d_instantiate(dentry, inode);
 638        return end_creating(dentry);
 639}
 640EXPORT_SYMBOL_GPL(debugfs_create_symlink);
 641
 642static void __debugfs_file_removed(struct dentry *dentry)
 643{
 644        struct debugfs_fsdata *fsd;
 645
 646        /*
 647         * Paired with the closing smp_mb() implied by a successful
 648         * cmpxchg() in debugfs_file_get(): either
 649         * debugfs_file_get() must see a dead dentry or we must see a
 650         * debugfs_fsdata instance at ->d_fsdata here (or both).
 651         */
 652        smp_mb();
 653        fsd = READ_ONCE(dentry->d_fsdata);
 654        if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
 655                return;
 656        if (!refcount_dec_and_test(&fsd->active_users))
 657                wait_for_completion(&fsd->active_users_drained);
 658}
 659
 660static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
 661{
 662        int ret = 0;
 663
 664        if (simple_positive(dentry)) {
 665                dget(dentry);
 666                if (d_is_dir(dentry)) {
 667                        ret = simple_rmdir(d_inode(parent), dentry);
 668                        if (!ret)
 669                                fsnotify_rmdir(d_inode(parent), dentry);
 670                } else {
 671                        simple_unlink(d_inode(parent), dentry);
 672                        fsnotify_unlink(d_inode(parent), dentry);
 673                }
 674                if (!ret)
 675                        d_delete(dentry);
 676                if (d_is_reg(dentry))
 677                        __debugfs_file_removed(dentry);
 678                dput(dentry);
 679        }
 680        return ret;
 681}
 682
 683/**
 684 * debugfs_remove - removes a file or directory from the debugfs filesystem
 685 * @dentry: a pointer to a the dentry of the file or directory to be
 686 *          removed.  If this parameter is NULL or an error value, nothing
 687 *          will be done.
 688 *
 689 * This function removes a file or directory in debugfs that was previously
 690 * created with a call to another debugfs function (like
 691 * debugfs_create_file() or variants thereof.)
 692 *
 693 * This function is required to be called in order for the file to be
 694 * removed, no automatic cleanup of files will happen when a module is
 695 * removed, you are responsible here.
 696 */
 697void debugfs_remove(struct dentry *dentry)
 698{
 699        struct dentry *parent;
 700        int ret;
 701
 702        if (IS_ERR_OR_NULL(dentry))
 703                return;
 704
 705        parent = dentry->d_parent;
 706        inode_lock(d_inode(parent));
 707        ret = __debugfs_remove(dentry, parent);
 708        inode_unlock(d_inode(parent));
 709        if (!ret)
 710                simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 711}
 712EXPORT_SYMBOL_GPL(debugfs_remove);
 713
 714/**
 715 * debugfs_remove_recursive - recursively removes a directory
 716 * @dentry: a pointer to a the dentry of the directory to be removed.  If this
 717 *          parameter is NULL or an error value, nothing will be done.
 718 *
 719 * This function recursively removes a directory tree in debugfs that
 720 * was previously created with a call to another debugfs function
 721 * (like debugfs_create_file() or variants thereof.)
 722 *
 723 * This function is required to be called in order for the file to be
 724 * removed, no automatic cleanup of files will happen when a module is
 725 * removed, you are responsible here.
 726 */
 727void debugfs_remove_recursive(struct dentry *dentry)
 728{
 729        struct dentry *child, *parent;
 730
 731        if (IS_ERR_OR_NULL(dentry))
 732                return;
 733
 734        parent = dentry;
 735 down:
 736        inode_lock(d_inode(parent));
 737 loop:
 738        /*
 739         * The parent->d_subdirs is protected by the d_lock. Outside that
 740         * lock, the child can be unlinked and set to be freed which can
 741         * use the d_u.d_child as the rcu head and corrupt this list.
 742         */
 743        spin_lock(&parent->d_lock);
 744        list_for_each_entry(child, &parent->d_subdirs, d_child) {
 745                if (!simple_positive(child))
 746                        continue;
 747
 748                /* perhaps simple_empty(child) makes more sense */
 749                if (!list_empty(&child->d_subdirs)) {
 750                        spin_unlock(&parent->d_lock);
 751                        inode_unlock(d_inode(parent));
 752                        parent = child;
 753                        goto down;
 754                }
 755
 756                spin_unlock(&parent->d_lock);
 757
 758                if (!__debugfs_remove(child, parent))
 759                        simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 760
 761                /*
 762                 * The parent->d_lock protects agaist child from unlinking
 763                 * from d_subdirs. When releasing the parent->d_lock we can
 764                 * no longer trust that the next pointer is valid.
 765                 * Restart the loop. We'll skip this one with the
 766                 * simple_positive() check.
 767                 */
 768                goto loop;
 769        }
 770        spin_unlock(&parent->d_lock);
 771
 772        inode_unlock(d_inode(parent));
 773        child = parent;
 774        parent = parent->d_parent;
 775        inode_lock(d_inode(parent));
 776
 777        if (child != dentry)
 778                /* go up */
 779                goto loop;
 780
 781        if (!__debugfs_remove(child, parent))
 782                simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 783        inode_unlock(d_inode(parent));
 784}
 785EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
 786
 787/**
 788 * debugfs_rename - rename a file/directory in the debugfs filesystem
 789 * @old_dir: a pointer to the parent dentry for the renamed object. This
 790 *          should be a directory dentry.
 791 * @old_dentry: dentry of an object to be renamed.
 792 * @new_dir: a pointer to the parent dentry where the object should be
 793 *          moved. This should be a directory dentry.
 794 * @new_name: a pointer to a string containing the target name.
 795 *
 796 * This function renames a file/directory in debugfs.  The target must not
 797 * exist for rename to succeed.
 798 *
 799 * This function will return a pointer to old_dentry (which is updated to
 800 * reflect renaming) if it succeeds. If an error occurs, %NULL will be
 801 * returned.
 802 *
 803 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
 804 * returned.
 805 */
 806struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
 807                struct dentry *new_dir, const char *new_name)
 808{
 809        int error;
 810        struct dentry *dentry = NULL, *trap;
 811        struct name_snapshot old_name;
 812
 813        if (IS_ERR(old_dir))
 814                return old_dir;
 815        if (IS_ERR(new_dir))
 816                return new_dir;
 817        if (IS_ERR_OR_NULL(old_dentry))
 818                return old_dentry;
 819
 820        trap = lock_rename(new_dir, old_dir);
 821        /* Source or destination directories don't exist? */
 822        if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
 823                goto exit;
 824        /* Source does not exist, cyclic rename, or mountpoint? */
 825        if (d_really_is_negative(old_dentry) || old_dentry == trap ||
 826            d_mountpoint(old_dentry))
 827                goto exit;
 828        dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
 829        /* Lookup failed, cyclic rename or target exists? */
 830        if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
 831                goto exit;
 832
 833        take_dentry_name_snapshot(&old_name, old_dentry);
 834
 835        error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
 836                              dentry, 0);
 837        if (error) {
 838                release_dentry_name_snapshot(&old_name);
 839                goto exit;
 840        }
 841        d_move(old_dentry, dentry);
 842        fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
 843                d_is_dir(old_dentry),
 844                NULL, old_dentry);
 845        release_dentry_name_snapshot(&old_name);
 846        unlock_rename(new_dir, old_dir);
 847        dput(dentry);
 848        return old_dentry;
 849exit:
 850        if (dentry && !IS_ERR(dentry))
 851                dput(dentry);
 852        unlock_rename(new_dir, old_dir);
 853        if (IS_ERR(dentry))
 854                return dentry;
 855        return ERR_PTR(-EINVAL);
 856}
 857EXPORT_SYMBOL_GPL(debugfs_rename);
 858
 859/**
 860 * debugfs_initialized - Tells whether debugfs has been registered
 861 */
 862bool debugfs_initialized(void)
 863{
 864        return debugfs_registered;
 865}
 866EXPORT_SYMBOL_GPL(debugfs_initialized);
 867
 868static int __init debugfs_init(void)
 869{
 870        int retval;
 871
 872        retval = sysfs_create_mount_point(kernel_kobj, "debug");
 873        if (retval)
 874                return retval;
 875
 876        retval = register_filesystem(&debug_fs_type);
 877        if (retval)
 878                sysfs_remove_mount_point(kernel_kobj, "debug");
 879        else
 880                debugfs_registered = true;
 881
 882        return retval;
 883}
 884core_initcall(debugfs_init);
 885
 886