13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 3bd33d12fSHarry Wei * inode.c - part of debugfs, a tiny little debug file system 41da177e4SLinus Torvalds * 543e23b6cSGreg Kroah-Hartman * Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]> 61da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 743e23b6cSGreg Kroah-Hartman * Copyright (C) 2019 Linux Foundation <[email protected]> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 10e1511a84SMauro Carvalho Chehab * See ./Documentation/core-api/kernel-api.rst for more details. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13d03ae477SGreg Kroah-Hartman #define pr_fmt(fmt) "debugfs: " fmt 14d03ae477SGreg Kroah-Hartman 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/fs.h> 171da177e4SLinus Torvalds #include <linux/mount.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include <linux/init.h> 204d8ebddcSRandy Dunlap #include <linux/kobject.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/debugfs.h> 234f36557fSMathieu Desnoyers #include <linux/fsnotify.h> 2466f54963SPeter Oberparleiter #include <linux/string.h> 25d6e48686SLudwig Nussel #include <linux/seq_file.h> 26d6e48686SLudwig Nussel #include <linux/parser.h> 2792562927SMimi Zohar #include <linux/magic.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 295496197fSDavid Howells #include <linux/security.h> 309fd4dcecSNicolai Stange 319fd4dcecSNicolai Stange #include "internal.h" 321da177e4SLinus Torvalds 3382aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE 0700 34d6e48686SLudwig Nussel 351da177e4SLinus Torvalds static struct vfsmount *debugfs_mount; 361da177e4SLinus Torvalds static int debugfs_mount_count; 37c0f92ba9SFrederic Weisbecker static bool debugfs_registered; 38312723a0SKees Cook static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS; 391da177e4SLinus Torvalds 405496197fSDavid Howells /* 415496197fSDavid Howells * Don't allow access attributes to be changed whilst the kernel is locked down 425496197fSDavid Howells * so that we can use the file mode as part of a heuristic to determine whether 435496197fSDavid Howells * to lock down individual files. 445496197fSDavid Howells */ 45549c7297SChristian Brauner static int debugfs_setattr(struct user_namespace *mnt_userns, 46549c7297SChristian Brauner struct dentry *dentry, struct iattr *ia) 475496197fSDavid Howells { 485881fa8dSOndrej Mosnacek int ret; 495496197fSDavid Howells 505881fa8dSOndrej Mosnacek if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) { 515881fa8dSOndrej Mosnacek ret = security_locked_down(LOCKDOWN_DEBUGFS); 525881fa8dSOndrej Mosnacek if (ret) 535496197fSDavid Howells return ret; 545881fa8dSOndrej Mosnacek } 55549c7297SChristian Brauner return simple_setattr(&init_user_ns, dentry, ia); 565496197fSDavid Howells } 575496197fSDavid Howells 585496197fSDavid Howells static const struct inode_operations debugfs_file_inode_operations = { 595496197fSDavid Howells .setattr = debugfs_setattr, 605496197fSDavid Howells }; 615496197fSDavid Howells static const struct inode_operations debugfs_dir_inode_operations = { 625496197fSDavid Howells .lookup = simple_lookup, 635496197fSDavid Howells .setattr = debugfs_setattr, 645496197fSDavid Howells }; 655496197fSDavid Howells static const struct inode_operations debugfs_symlink_inode_operations = { 665496197fSDavid Howells .get_link = simple_get_link, 675496197fSDavid Howells .setattr = debugfs_setattr, 685496197fSDavid Howells }; 695496197fSDavid Howells 70edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 711da177e4SLinus Torvalds { 721da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 731da177e4SLinus Torvalds if (inode) { 7485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 751b48b530SDeepa Dinamani inode->i_atime = inode->i_mtime = 76c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds return inode; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 81d6e48686SLudwig Nussel struct debugfs_mount_opts { 827dc05881SEric W. Biederman kuid_t uid; 837dc05881SEric W. Biederman kgid_t gid; 84d6e48686SLudwig Nussel umode_t mode; 85d6e48686SLudwig Nussel }; 86d6e48686SLudwig Nussel 87d6e48686SLudwig Nussel enum { 88d6e48686SLudwig Nussel Opt_uid, 89d6e48686SLudwig Nussel Opt_gid, 90d6e48686SLudwig Nussel Opt_mode, 91d6e48686SLudwig Nussel Opt_err 92d6e48686SLudwig Nussel }; 93d6e48686SLudwig Nussel 94d6e48686SLudwig Nussel static const match_table_t tokens = { 95d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 96d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 97d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 98d6e48686SLudwig Nussel {Opt_err, NULL} 99d6e48686SLudwig Nussel }; 100d6e48686SLudwig Nussel 101d6e48686SLudwig Nussel struct debugfs_fs_info { 102d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 103d6e48686SLudwig Nussel }; 104d6e48686SLudwig Nussel 105d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 106d6e48686SLudwig Nussel { 107d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 108d6e48686SLudwig Nussel int option; 109d6e48686SLudwig Nussel int token; 1107dc05881SEric W. Biederman kuid_t uid; 1117dc05881SEric W. Biederman kgid_t gid; 112d6e48686SLudwig Nussel char *p; 113d6e48686SLudwig Nussel 114d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 115d6e48686SLudwig Nussel 116d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 117d6e48686SLudwig Nussel if (!*p) 118d6e48686SLudwig Nussel continue; 119d6e48686SLudwig Nussel 120d6e48686SLudwig Nussel token = match_token(p, tokens, args); 121d6e48686SLudwig Nussel switch (token) { 122d6e48686SLudwig Nussel case Opt_uid: 123d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 124d6e48686SLudwig Nussel return -EINVAL; 1257dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1267dc05881SEric W. Biederman if (!uid_valid(uid)) 1277dc05881SEric W. Biederman return -EINVAL; 1287dc05881SEric W. Biederman opts->uid = uid; 129d6e48686SLudwig Nussel break; 130d6e48686SLudwig Nussel case Opt_gid: 131f1688e04SDave Reisner if (match_int(&args[0], &option)) 132d6e48686SLudwig Nussel return -EINVAL; 1337dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1347dc05881SEric W. Biederman if (!gid_valid(gid)) 1357dc05881SEric W. Biederman return -EINVAL; 1367dc05881SEric W. Biederman opts->gid = gid; 137d6e48686SLudwig Nussel break; 138d6e48686SLudwig Nussel case Opt_mode: 139d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 140d6e48686SLudwig Nussel return -EINVAL; 141d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 142d6e48686SLudwig Nussel break; 143d6e48686SLudwig Nussel /* 144d6e48686SLudwig Nussel * We might like to report bad mount options here; 145d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 146d6e48686SLudwig Nussel */ 147d6e48686SLudwig Nussel } 148d6e48686SLudwig Nussel } 149d6e48686SLudwig Nussel 150d6e48686SLudwig Nussel return 0; 151d6e48686SLudwig Nussel } 152d6e48686SLudwig Nussel 153d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 154d6e48686SLudwig Nussel { 155d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1562b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 157d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 158d6e48686SLudwig Nussel 159d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 160d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 161d6e48686SLudwig Nussel 162d6e48686SLudwig Nussel inode->i_uid = opts->uid; 163d6e48686SLudwig Nussel inode->i_gid = opts->gid; 164d6e48686SLudwig Nussel 165d6e48686SLudwig Nussel return 0; 166d6e48686SLudwig Nussel } 167d6e48686SLudwig Nussel 168d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 169d6e48686SLudwig Nussel { 170d6e48686SLudwig Nussel int err; 171d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 172d6e48686SLudwig Nussel 17302b9984dSTheodore Ts'o sync_filesystem(sb); 174d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 175d6e48686SLudwig Nussel if (err) 176d6e48686SLudwig Nussel goto fail; 177d6e48686SLudwig Nussel 178d6e48686SLudwig Nussel debugfs_apply_options(sb); 179d6e48686SLudwig Nussel 180d6e48686SLudwig Nussel fail: 181d6e48686SLudwig Nussel return err; 182d6e48686SLudwig Nussel } 183d6e48686SLudwig Nussel 184d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 185d6e48686SLudwig Nussel { 186d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 187d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 188d6e48686SLudwig Nussel 1897dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1907dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1917dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1927dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1937dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1947dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 195d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 196d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 197d6e48686SLudwig Nussel 198d6e48686SLudwig Nussel return 0; 199d6e48686SLudwig Nussel } 200d6e48686SLudwig Nussel 2016234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 2020db59e59SAl Viro { 2030db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 2045723cb01SAl Viro kfree(inode->i_link); 20593b919daSAl Viro free_inode_nonrcu(inode); 20693b919daSAl Viro } 20793b919daSAl Viro 208d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 209d6e48686SLudwig Nussel .statfs = simple_statfs, 210d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 211d6e48686SLudwig Nussel .show_options = debugfs_show_options, 2126234ddf4SAl Viro .free_inode = debugfs_free_inode, 213d6e48686SLudwig Nussel }; 214d6e48686SLudwig Nussel 2157c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2167c8d4698SNicolai Stange { 2177d39bc50SNicolai Stange void *fsd = dentry->d_fsdata; 2187d39bc50SNicolai Stange 2197d39bc50SNicolai Stange if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 2207c8d4698SNicolai Stange kfree(dentry->d_fsdata); 2217c8d4698SNicolai Stange } 2227c8d4698SNicolai Stange 22377b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 22477b3da6eSAl Viro { 22593faccbbSEric W. Biederman debugfs_automount_t f; 22693faccbbSEric W. Biederman f = (debugfs_automount_t)path->dentry->d_fsdata; 22793faccbbSEric W. Biederman return f(path->dentry, d_inode(path->dentry)->i_private); 22877b3da6eSAl Viro } 22977b3da6eSAl Viro 23077b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 23177b3da6eSAl Viro .d_delete = always_delete_dentry, 2327c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 23377b3da6eSAl Viro .d_automount = debugfs_automount, 23477b3da6eSAl Viro }; 23577b3da6eSAl Viro 2361da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2371da177e4SLinus Torvalds { 238cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 239d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 240d6e48686SLudwig Nussel int err; 2411da177e4SLinus Torvalds 242d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 243d6e48686SLudwig Nussel sb->s_fs_info = fsi; 244d6e48686SLudwig Nussel if (!fsi) { 245d6e48686SLudwig Nussel err = -ENOMEM; 246d6e48686SLudwig Nussel goto fail; 247d6e48686SLudwig Nussel } 248d6e48686SLudwig Nussel 249d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 250d6e48686SLudwig Nussel if (err) 251d6e48686SLudwig Nussel goto fail; 252d6e48686SLudwig Nussel 253d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 254d6e48686SLudwig Nussel if (err) 255d6e48686SLudwig Nussel goto fail; 256d6e48686SLudwig Nussel 257d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 25877b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 259d6e48686SLudwig Nussel 260d6e48686SLudwig Nussel debugfs_apply_options(sb); 261d6e48686SLudwig Nussel 262d6e48686SLudwig Nussel return 0; 263d6e48686SLudwig Nussel 264d6e48686SLudwig Nussel fail: 265d6e48686SLudwig Nussel kfree(fsi); 266d6e48686SLudwig Nussel sb->s_fs_info = NULL; 267d6e48686SLudwig Nussel return err; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 270fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2711da177e4SLinus Torvalds int flags, const char *dev_name, 272fc14f2feSAl Viro void *data) 2731da177e4SLinus Torvalds { 274a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 275a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 276a24c6f7bSPeter Enderborg 277fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2811da177e4SLinus Torvalds .owner = THIS_MODULE, 2821da177e4SLinus Torvalds .name = "debugfs", 283fc14f2feSAl Viro .mount = debug_mount, 2841da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2851da177e4SLinus Torvalds }; 2867f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2871da177e4SLinus Torvalds 288a7c5437bSOmar Sandoval /** 289a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 290a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 291a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 292a7c5437bSOmar Sandoval * 293a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 294a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 295a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 296a7c5437bSOmar Sandoval * 297a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 298a7c5437bSOmar Sandoval * returned. 299a7c5437bSOmar Sandoval */ 300a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 301a7c5437bSOmar Sandoval { 302a7c5437bSOmar Sandoval struct dentry *dentry; 303a7c5437bSOmar Sandoval 304bc6de804SGreg Kroah-Hartman if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 305a7c5437bSOmar Sandoval return NULL; 306a7c5437bSOmar Sandoval 307a7c5437bSOmar Sandoval if (!parent) 308a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 309a7c5437bSOmar Sandoval 3106c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 311a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 312a7c5437bSOmar Sandoval return NULL; 313a7c5437bSOmar Sandoval return dentry; 314a7c5437bSOmar Sandoval } 315a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 316a7c5437bSOmar Sandoval 317190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 318c3b1a350SAl Viro { 319190afd81SAl Viro struct dentry *dentry; 320c3b1a350SAl Viro int error; 321c3b1a350SAl Viro 322a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 323a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 324a24c6f7bSPeter Enderborg 32556348560SGreg Kroah-Hartman if (!debugfs_initialized()) 32656348560SGreg Kroah-Hartman return ERR_PTR(-ENOENT); 32756348560SGreg Kroah-Hartman 328d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 329c3b1a350SAl Viro 330c9e15f25SGreg KH if (IS_ERR(parent)) 331c9e15f25SGreg KH return parent; 332c9e15f25SGreg KH 333c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 334c3b1a350SAl Viro &debugfs_mount_count); 33543e23b6cSGreg Kroah-Hartman if (error) { 33643e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 337190afd81SAl Viro return ERR_PTR(error); 33843e23b6cSGreg Kroah-Hartman } 339c3b1a350SAl Viro 340cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 341cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 342cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 343cfa57c11SAl Viro * have around. 344cfa57c11SAl Viro */ 345cfa57c11SAl Viro if (!parent) 346cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 347cfa57c11SAl Viro 3485955102cSAl Viro inode_lock(d_inode(parent)); 349a3d1e7ebSAl Viro if (unlikely(IS_DEADDIR(d_inode(parent)))) 350a3d1e7ebSAl Viro dentry = ERR_PTR(-ENOENT); 351a3d1e7ebSAl Viro else 352cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3532b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 354c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 355c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 356c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 357c33d4423SGreg Kroah-Hartman else 358c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 359c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 360190afd81SAl Viro dput(dentry); 361190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 362190afd81SAl Viro } 3630ee9608cSDaniel Borkmann 3640ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3655955102cSAl Viro inode_unlock(d_inode(parent)); 3660ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3670ee9608cSDaniel Borkmann } 3680ee9608cSDaniel Borkmann 369190afd81SAl Viro return dentry; 370190afd81SAl Viro } 371190afd81SAl Viro 3725233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 373190afd81SAl Viro { 3745955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 375190afd81SAl Viro dput(dentry); 376190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 377ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 378190afd81SAl Viro } 3795233e311SAl Viro 3805233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3815233e311SAl Viro { 3825955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 383190afd81SAl Viro return dentry; 384190afd81SAl Viro } 385190afd81SAl Viro 38649d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 38749d200deSNicolai Stange struct dentry *parent, void *data, 38849d200deSNicolai Stange const struct file_operations *proxy_fops, 38949d200deSNicolai Stange const struct file_operations *real_fops) 39049d200deSNicolai Stange { 39149d200deSNicolai Stange struct dentry *dentry; 39249d200deSNicolai Stange struct inode *inode; 39349d200deSNicolai Stange 39449d200deSNicolai Stange if (!(mode & S_IFMT)) 39549d200deSNicolai Stange mode |= S_IFREG; 39649d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 39749d200deSNicolai Stange dentry = start_creating(name, parent); 39849d200deSNicolai Stange 3997d39bc50SNicolai Stange if (IS_ERR(dentry)) 400ff9fb72bSGreg Kroah-Hartman return dentry; 40149d200deSNicolai Stange 402a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 403a24c6f7bSPeter Enderborg failed_creating(dentry); 404a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 405a24c6f7bSPeter Enderborg } 406a24c6f7bSPeter Enderborg 40749d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 40843e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 40943e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 41043e23b6cSGreg Kroah-Hartman name); 41149d200deSNicolai Stange return failed_creating(dentry); 41243e23b6cSGreg Kroah-Hartman } 41349d200deSNicolai Stange 41449d200deSNicolai Stange inode->i_mode = mode; 41549d200deSNicolai Stange inode->i_private = data; 41649d200deSNicolai Stange 4175496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 41849d200deSNicolai Stange inode->i_fop = proxy_fops; 4197d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 4207d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 42149d200deSNicolai Stange 42249d200deSNicolai Stange d_instantiate(dentry, inode); 42349d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 42449d200deSNicolai Stange return end_creating(dentry); 42549d200deSNicolai Stange } 42649d200deSNicolai Stange 4271da177e4SLinus Torvalds /** 4281da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 4291da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 430be030e65SAlberto Bertogli * @mode: the permission that the file should have. 4311da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 432e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4331da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4341da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 4358e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 4361da177e4SLinus Torvalds * the open() call. 4371da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 4381da177e4SLinus Torvalds * this file. 4391da177e4SLinus Torvalds * 4401da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 441be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 442be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4431da177e4SLinus Torvalds * recommended to be used instead.) 4441da177e4SLinus Torvalds * 4451da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4461da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4471da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 448adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 449ff9fb72bSGreg Kroah-Hartman * returned. 4501da177e4SLinus Torvalds * 4516468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 452873760fbSCornelia Huck * returned. 453*8bcbde2bSDouglas Anderson * 454*8bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 455*8bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 456*8bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 457*8bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 4581da177e4SLinus Torvalds */ 459f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4601da177e4SLinus Torvalds struct dentry *parent, void *data, 46199ac48f5SArjan van de Ven const struct file_operations *fops) 4621da177e4SLinus Torvalds { 463c3b1a350SAl Viro 46449d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 46549d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 46649d200deSNicolai Stange &debugfs_noop_file_operations, 46749d200deSNicolai Stange fops); 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4701da177e4SLinus Torvalds 471c6468808SNicolai Stange /** 472c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 473c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 474c6468808SNicolai Stange * @mode: the permission that the file should have. 475c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 476c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 477c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 478c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 479c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 480c6468808SNicolai Stange * the open() call. 481c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 482c6468808SNicolai Stange * this file. 483c6468808SNicolai Stange * 484c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 485c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 486c6468808SNicolai Stange * handed it will not get protected against file removals by the 487c6468808SNicolai Stange * debugfs core. 488c6468808SNicolai Stange * 489c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 4900eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 4910eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 492c6468808SNicolai Stange * debugfs though. 493c6468808SNicolai Stange * 494c6468808SNicolai Stange * Any struct file_operations defined by means of 495c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 496c6468808SNicolai Stange * thus, may be used here. 497c6468808SNicolai Stange */ 49849d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 49949d200deSNicolai Stange struct dentry *parent, void *data, 50049d200deSNicolai Stange const struct file_operations *fops) 50149d200deSNicolai Stange { 50249d200deSNicolai Stange 50349d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 50449d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 50549d200deSNicolai Stange &debugfs_noop_file_operations, 50649d200deSNicolai Stange fops); 50749d200deSNicolai Stange } 508c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 50949d200deSNicolai Stange 5101da177e4SLinus Torvalds /** 511e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 512e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 513e59b4e91SDavid Howells * @mode: the permission that the file should have. 514e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 515e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 516e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 517e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 518e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 519e59b4e91SDavid Howells * the open() call. 520e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 521e59b4e91SDavid Howells * this file. 522e59b4e91SDavid Howells * @file_size: initial file size 523e59b4e91SDavid Howells * 524e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 525e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 526e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 527e59b4e91SDavid Howells * recommended to be used instead.) 528e59b4e91SDavid Howells */ 529526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode, 530e59b4e91SDavid Howells struct dentry *parent, void *data, 531e59b4e91SDavid Howells const struct file_operations *fops, 532e59b4e91SDavid Howells loff_t file_size) 533e59b4e91SDavid Howells { 534e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 535e59b4e91SDavid Howells 536af505cadSNirmoy Das if (!IS_ERR(de)) 5372b0143b5SDavid Howells d_inode(de)->i_size = file_size; 538e59b4e91SDavid Howells } 539e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 540e59b4e91SDavid Howells 541e59b4e91SDavid Howells /** 5421da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5431da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5441da177e4SLinus Torvalds * create. 5451da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 546e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5471da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5481da177e4SLinus Torvalds * 5491da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5501da177e4SLinus Torvalds * 5511da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5521da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5531da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 554adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 555ff9fb72bSGreg Kroah-Hartman * returned. 5561da177e4SLinus Torvalds * 5576468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 558873760fbSCornelia Huck * returned. 559*8bcbde2bSDouglas Anderson * 560*8bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 561*8bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 562*8bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 563*8bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5661da177e4SLinus Torvalds { 567ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 568680b3024SAl Viro struct inode *inode; 569ad5abd5bSAl Viro 570ad5abd5bSAl Viro if (IS_ERR(dentry)) 571ff9fb72bSGreg Kroah-Hartman return dentry; 572ad5abd5bSAl Viro 573a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 574a24c6f7bSPeter Enderborg failed_creating(dentry); 575a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 576a24c6f7bSPeter Enderborg } 577a24c6f7bSPeter Enderborg 578edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 57943e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 58043e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 58143e23b6cSGreg Kroah-Hartman name); 5825233e311SAl Viro return failed_creating(dentry); 58343e23b6cSGreg Kroah-Hartman } 584680b3024SAl Viro 585f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 5865496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 587edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 588edac65eaSAl Viro 589edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 590edac65eaSAl Viro inc_nlink(inode); 591680b3024SAl Viro d_instantiate(dentry, inode); 5922b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 5932b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 5945233e311SAl Viro return end_creating(dentry); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /** 59977b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 60077b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 60177b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 60277b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 60377b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 60477b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 60577b3da6eSAl Viro * @data: opaque argument to pass to f(). 60677b3da6eSAl Viro * 60777b3da6eSAl Viro * @f should return what ->d_automount() would. 60877b3da6eSAl Viro */ 60977b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 61077b3da6eSAl Viro struct dentry *parent, 61193faccbbSEric W. Biederman debugfs_automount_t f, 61277b3da6eSAl Viro void *data) 61377b3da6eSAl Viro { 61477b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 61577b3da6eSAl Viro struct inode *inode; 61677b3da6eSAl Viro 61777b3da6eSAl Viro if (IS_ERR(dentry)) 618ff9fb72bSGreg Kroah-Hartman return dentry; 61977b3da6eSAl Viro 620a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 621a24c6f7bSPeter Enderborg failed_creating(dentry); 622a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 623a24c6f7bSPeter Enderborg } 624a24c6f7bSPeter Enderborg 62577b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 62643e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 62743e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 62843e23b6cSGreg Kroah-Hartman name); 62977b3da6eSAl Viro return failed_creating(dentry); 63043e23b6cSGreg Kroah-Hartman } 63177b3da6eSAl Viro 63287243debSSeth Forshee make_empty_dir_inode(inode); 63377b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 63477b3da6eSAl Viro inode->i_private = data; 63577b3da6eSAl Viro dentry->d_fsdata = (void *)f; 636a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 637a8f324a4SRoman Pen inc_nlink(inode); 63877b3da6eSAl Viro d_instantiate(dentry, inode); 639a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 640a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 64177b3da6eSAl Viro return end_creating(dentry); 64277b3da6eSAl Viro } 64377b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 64477b3da6eSAl Viro 64577b3da6eSAl Viro /** 64666f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 64766f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 64866f54963SPeter Oberparleiter * create. 64966f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 650e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 65166f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 65266f54963SPeter Oberparleiter * filesystem. 65366f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 65466f54963SPeter Oberparleiter * symbolic link. 65566f54963SPeter Oberparleiter * 65666f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 65766f54963SPeter Oberparleiter * links to the given target path. 65866f54963SPeter Oberparleiter * 65966f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 66066f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 66166f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 662adc92dd4SDaniel W. S. Almeida * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 663ff9fb72bSGreg Kroah-Hartman * will be returned. 66466f54963SPeter Oberparleiter * 66566f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 666873760fbSCornelia Huck * returned. 66766f54963SPeter Oberparleiter */ 66866f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 66966f54963SPeter Oberparleiter const char *target) 67066f54963SPeter Oberparleiter { 671ad5abd5bSAl Viro struct dentry *dentry; 672680b3024SAl Viro struct inode *inode; 673680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 67466f54963SPeter Oberparleiter if (!link) 675ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 67666f54963SPeter Oberparleiter 677ad5abd5bSAl Viro dentry = start_creating(name, parent); 678ad5abd5bSAl Viro if (IS_ERR(dentry)) { 67966f54963SPeter Oberparleiter kfree(link); 680ff9fb72bSGreg Kroah-Hartman return dentry; 681ad5abd5bSAl Viro } 682ad5abd5bSAl Viro 683edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 684680b3024SAl Viro if (unlikely(!inode)) { 68543e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 68643e23b6cSGreg Kroah-Hartman name); 687ad5abd5bSAl Viro kfree(link); 6885233e311SAl Viro return failed_creating(dentry); 689680b3024SAl Viro } 690edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 6915496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 6925723cb01SAl Viro inode->i_link = link; 693680b3024SAl Viro d_instantiate(dentry, inode); 6945233e311SAl Viro return end_creating(dentry); 69566f54963SPeter Oberparleiter } 69666f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 69766f54963SPeter Oberparleiter 698823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 699e9117a5aSNicolai Stange { 700e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 701e9117a5aSNicolai Stange 7027d39bc50SNicolai Stange /* 7037d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 7047d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 7057d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 7067d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 7077d39bc50SNicolai Stange */ 7087d39bc50SNicolai Stange smp_mb(); 7097d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 7107d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 7117d39bc50SNicolai Stange return; 712e9117a5aSNicolai Stange if (!refcount_dec_and_test(&fsd->active_users)) 713e9117a5aSNicolai Stange wait_for_completion(&fsd->active_users_drained); 714e9117a5aSNicolai Stange } 715e9117a5aSNicolai Stange 716a3d1e7ebSAl Viro static void remove_one(struct dentry *victim) 7171da177e4SLinus Torvalds { 718a3d1e7ebSAl Viro if (d_is_reg(victim)) 719a3d1e7ebSAl Viro __debugfs_file_removed(victim); 7201da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 723cfc94cdfSJan Kara /** 724a3d1e7ebSAl Viro * debugfs_remove - recursively removes a directory 725398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 726398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 7279505e637SHaavard Skinnemoen * 7289505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 7299505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 7309505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 7319505e637SHaavard Skinnemoen * 7329505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7339505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7349505e637SHaavard Skinnemoen * removed, you are responsible here. 7359505e637SHaavard Skinnemoen */ 736a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry) 7379505e637SHaavard Skinnemoen { 738a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7399505e637SHaavard Skinnemoen return; 7409505e637SHaavard Skinnemoen 741a3d1e7ebSAl Viro simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 742a3d1e7ebSAl Viro simple_recursive_removal(dentry, remove_one); 743776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 744776164c1SOleg Nesterov } 745a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove); 7469505e637SHaavard Skinnemoen 7479505e637SHaavard Skinnemoen /** 748cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 749cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 750cfc94cdfSJan Kara * should be a directory dentry. 751cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 752cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 753cfc94cdfSJan Kara * moved. This should be a directory dentry. 754cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 755cfc94cdfSJan Kara * 756cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 757cfc94cdfSJan Kara * exist for rename to succeed. 758cfc94cdfSJan Kara * 759cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 760cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 761cfc94cdfSJan Kara * returned. 762cfc94cdfSJan Kara * 763cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 764cfc94cdfSJan Kara * returned. 765cfc94cdfSJan Kara */ 766cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 767cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 768cfc94cdfSJan Kara { 769cfc94cdfSJan Kara int error; 770cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 77149d31c2fSAl Viro struct name_snapshot old_name; 772cfc94cdfSJan Kara 773d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 774d88c93f0SGreg Kroah-Hartman return old_dir; 775d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 776d88c93f0SGreg Kroah-Hartman return new_dir; 777d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 778d88c93f0SGreg Kroah-Hartman return old_dentry; 779d88c93f0SGreg Kroah-Hartman 780cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 781cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 7822b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 783cfc94cdfSJan Kara goto exit; 784cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 7852b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 786cfc94cdfSJan Kara d_mountpoint(old_dentry)) 787cfc94cdfSJan Kara goto exit; 788cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 789cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 7902b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 791cfc94cdfSJan Kara goto exit; 792cfc94cdfSJan Kara 79349d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 794cfc94cdfSJan Kara 795549c7297SChristian Brauner error = simple_rename(&init_user_ns, d_inode(old_dir), old_dentry, 796549c7297SChristian Brauner d_inode(new_dir), dentry, 0); 797cfc94cdfSJan Kara if (error) { 79849d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 799cfc94cdfSJan Kara goto exit; 800cfc94cdfSJan Kara } 801cfc94cdfSJan Kara d_move(old_dentry, dentry); 802f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 803e36cb0b8SDavid Howells d_is_dir(old_dentry), 8045a190ae6SAl Viro NULL, old_dentry); 80549d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 806cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 807cfc94cdfSJan Kara dput(dentry); 808cfc94cdfSJan Kara return old_dentry; 809cfc94cdfSJan Kara exit: 810cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 811cfc94cdfSJan Kara dput(dentry); 812cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 813ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 814ff9fb72bSGreg Kroah-Hartman return dentry; 815ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 816cfc94cdfSJan Kara } 817cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 818cfc94cdfSJan Kara 819c0f92ba9SFrederic Weisbecker /** 820c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 821c0f92ba9SFrederic Weisbecker */ 822c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 823c0f92ba9SFrederic Weisbecker { 824c0f92ba9SFrederic Weisbecker return debugfs_registered; 825c0f92ba9SFrederic Weisbecker } 826c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 827c0f92ba9SFrederic Weisbecker 828a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str) 829a24c6f7bSPeter Enderborg { 830a24c6f7bSPeter Enderborg if (str) { 831a24c6f7bSPeter Enderborg if (!strcmp(str, "on")) 832a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 833a24c6f7bSPeter Enderborg else if (!strcmp(str, "no-mount")) 834a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API; 835a24c6f7bSPeter Enderborg else if (!strcmp(str, "off")) 836a24c6f7bSPeter Enderborg debugfs_allow = 0; 837a24c6f7bSPeter Enderborg } 838a24c6f7bSPeter Enderborg 839a24c6f7bSPeter Enderborg return 0; 840a24c6f7bSPeter Enderborg } 841a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel); 8421da177e4SLinus Torvalds static int __init debugfs_init(void) 8431da177e4SLinus Torvalds { 8441da177e4SLinus Torvalds int retval; 8451da177e4SLinus Torvalds 846a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 847a24c6f7bSPeter Enderborg return -EPERM; 848a24c6f7bSPeter Enderborg 849f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 850f9bb4882SEric W. Biederman if (retval) 851f9bb4882SEric W. Biederman return retval; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 8541da177e4SLinus Torvalds if (retval) 855f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 856c0f92ba9SFrederic Weisbecker else 857c0f92ba9SFrederic Weisbecker debugfs_registered = true; 858c0f92ba9SFrederic Weisbecker 8591da177e4SLinus Torvalds return retval; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds core_initcall(debugfs_init); 862