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> 17a20971c1SDavid Howells #include <linux/fs_context.h> 18a20971c1SDavid Howells #include <linux/fs_parser.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/init.h> 214d8ebddcSRandy Dunlap #include <linux/kobject.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/debugfs.h> 244f36557fSMathieu Desnoyers #include <linux/fsnotify.h> 2566f54963SPeter Oberparleiter #include <linux/string.h> 26d6e48686SLudwig Nussel #include <linux/seq_file.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 */ 45c1632a0fSChristian Brauner static int debugfs_setattr(struct mnt_idmap *idmap, 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 } 55c1632a0fSChristian Brauner return simple_setattr(&nop_mnt_idmap, 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(); 75d21b3c32SJeff Layton simple_inode_init_ts(inode); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds return inode; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 80a20971c1SDavid Howells struct debugfs_fs_info { 817dc05881SEric W. Biederman kuid_t uid; 827dc05881SEric W. Biederman kgid_t gid; 83d6e48686SLudwig Nussel umode_t mode; 84b8de524cSBrian Norris /* Opt_* bitfield. */ 85b8de524cSBrian Norris unsigned int opts; 86d6e48686SLudwig Nussel }; 87d6e48686SLudwig Nussel 88d6e48686SLudwig Nussel enum { 89d6e48686SLudwig Nussel Opt_uid, 90d6e48686SLudwig Nussel Opt_gid, 91d6e48686SLudwig Nussel Opt_mode, 923a987b88SMarc Aurèle La France Opt_source, 93d6e48686SLudwig Nussel }; 94d6e48686SLudwig Nussel 95a20971c1SDavid Howells static const struct fs_parameter_spec debugfs_param_specs[] = { 9649abee59SEric Sandeen fsparam_gid ("gid", Opt_gid), 97a20971c1SDavid Howells fsparam_u32oct ("mode", Opt_mode), 9849abee59SEric Sandeen fsparam_uid ("uid", Opt_uid), 993a987b88SMarc Aurèle La France fsparam_string ("source", Opt_source), 100a20971c1SDavid Howells {} 101d6e48686SLudwig Nussel }; 102d6e48686SLudwig Nussel 103a20971c1SDavid Howells static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 104d6e48686SLudwig Nussel { 105a20971c1SDavid Howells struct debugfs_fs_info *opts = fc->s_fs_info; 106a20971c1SDavid Howells struct fs_parse_result result; 107a20971c1SDavid Howells int opt; 108d6e48686SLudwig Nussel 109a20971c1SDavid Howells opt = fs_parse(fc, debugfs_param_specs, param, &result); 1100c07c273SChristian Brauner if (opt < 0) { 1110c07c273SChristian Brauner /* 1120c07c273SChristian Brauner * We might like to report bad mount options here; but 1130c07c273SChristian Brauner * traditionally debugfs has ignored all mount options 1140c07c273SChristian Brauner */ 1150c07c273SChristian Brauner if (opt == -ENOPARAM) 1160c07c273SChristian Brauner return 0; 1170c07c273SChristian Brauner 118a20971c1SDavid Howells return opt; 1190c07c273SChristian Brauner } 120d6e48686SLudwig Nussel 121a20971c1SDavid Howells switch (opt) { 122d6e48686SLudwig Nussel case Opt_uid: 12349abee59SEric Sandeen opts->uid = result.uid; 124d6e48686SLudwig Nussel break; 125d6e48686SLudwig Nussel case Opt_gid: 12649abee59SEric Sandeen opts->gid = result.gid; 127d6e48686SLudwig Nussel break; 128d6e48686SLudwig Nussel case Opt_mode: 129a20971c1SDavid Howells opts->mode = result.uint_32 & S_IALLUGO; 130d6e48686SLudwig Nussel break; 1313a987b88SMarc Aurèle La France case Opt_source: 1323a987b88SMarc Aurèle La France if (fc->source) 1333a987b88SMarc Aurèle La France return invalfc(fc, "Multiple sources specified"); 1343a987b88SMarc Aurèle La France fc->source = param->string; 1353a987b88SMarc Aurèle La France param->string = NULL; 1363a987b88SMarc Aurèle La France break; 137d6e48686SLudwig Nussel /* 138d6e48686SLudwig Nussel * We might like to report bad mount options here; 139d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 140d6e48686SLudwig Nussel */ 141d6e48686SLudwig Nussel } 142b8de524cSBrian Norris 143a20971c1SDavid Howells opts->opts |= BIT(opt); 144d6e48686SLudwig Nussel 145d6e48686SLudwig Nussel return 0; 146d6e48686SLudwig Nussel } 147d6e48686SLudwig Nussel 148b8de524cSBrian Norris static void _debugfs_apply_options(struct super_block *sb, bool remount) 149d6e48686SLudwig Nussel { 150d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1512b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 152d6e48686SLudwig Nussel 153b8de524cSBrian Norris /* 154b8de524cSBrian Norris * On remount, only reset mode/uid/gid if they were provided as mount 155b8de524cSBrian Norris * options. 156b8de524cSBrian Norris */ 157b8de524cSBrian Norris 158a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_mode)) { 159d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 160a20971c1SDavid Howells inode->i_mode |= fsi->mode; 161b8de524cSBrian Norris } 162d6e48686SLudwig Nussel 163a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_uid)) 164a20971c1SDavid Howells inode->i_uid = fsi->uid; 165d6e48686SLudwig Nussel 166a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_gid)) 167a20971c1SDavid Howells inode->i_gid = fsi->gid; 168b8de524cSBrian Norris } 169b8de524cSBrian Norris 170b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb) 171b8de524cSBrian Norris { 172b8de524cSBrian Norris _debugfs_apply_options(sb, false); 173b8de524cSBrian Norris } 174b8de524cSBrian Norris 175b8de524cSBrian Norris static void debugfs_apply_options_remount(struct super_block *sb) 176b8de524cSBrian Norris { 177b8de524cSBrian Norris _debugfs_apply_options(sb, true); 178d6e48686SLudwig Nussel } 179d6e48686SLudwig Nussel 180a20971c1SDavid Howells static int debugfs_reconfigure(struct fs_context *fc) 181d6e48686SLudwig Nussel { 182a20971c1SDavid Howells struct super_block *sb = fc->root->d_sb; 183a20971c1SDavid Howells struct debugfs_fs_info *sb_opts = sb->s_fs_info; 184a20971c1SDavid Howells struct debugfs_fs_info *new_opts = fc->s_fs_info; 185d6e48686SLudwig Nussel 18602b9984dSTheodore Ts'o sync_filesystem(sb); 187d6e48686SLudwig Nussel 188a20971c1SDavid Howells /* structure copy of new mount options to sb */ 189a20971c1SDavid Howells *sb_opts = *new_opts; 190b8de524cSBrian Norris debugfs_apply_options_remount(sb); 191d6e48686SLudwig Nussel 192a20971c1SDavid Howells return 0; 193d6e48686SLudwig Nussel } 194d6e48686SLudwig Nussel 195d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 196d6e48686SLudwig Nussel { 197d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 198d6e48686SLudwig Nussel 199a20971c1SDavid Howells if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID)) 2007dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 201a20971c1SDavid Howells from_kuid_munged(&init_user_ns, fsi->uid)); 202a20971c1SDavid Howells if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID)) 2037dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 204a20971c1SDavid Howells from_kgid_munged(&init_user_ns, fsi->gid)); 205a20971c1SDavid Howells if (fsi->mode != DEBUGFS_DEFAULT_MODE) 206a20971c1SDavid Howells seq_printf(m, ",mode=%o", fsi->mode); 207d6e48686SLudwig Nussel 208d6e48686SLudwig Nussel return 0; 209d6e48686SLudwig Nussel } 210d6e48686SLudwig Nussel 211268b3611SAl Viro static struct kmem_cache *debugfs_inode_cachep __ro_after_init; 212268b3611SAl Viro 213268b3611SAl Viro static void init_once(void *foo) 214268b3611SAl Viro { 215268b3611SAl Viro struct debugfs_inode_info *info = foo; 216268b3611SAl Viro inode_init_once(&info->vfs_inode); 217268b3611SAl Viro } 218268b3611SAl Viro 219268b3611SAl Viro static struct inode *debugfs_alloc_inode(struct super_block *sb) 220268b3611SAl Viro { 221268b3611SAl Viro struct debugfs_inode_info *info; 222268b3611SAl Viro info = alloc_inode_sb(sb, debugfs_inode_cachep, GFP_KERNEL); 223268b3611SAl Viro if (!info) 224268b3611SAl Viro return NULL; 225268b3611SAl Viro return &info->vfs_inode; 226268b3611SAl Viro } 227268b3611SAl Viro 2286234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 2290db59e59SAl Viro { 2300db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 2315723cb01SAl Viro kfree(inode->i_link); 232268b3611SAl Viro kmem_cache_free(debugfs_inode_cachep, DEBUGFS_I(inode)); 23393b919daSAl Viro } 23493b919daSAl Viro 235d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 236d6e48686SLudwig Nussel .statfs = simple_statfs, 237d6e48686SLudwig Nussel .show_options = debugfs_show_options, 238268b3611SAl Viro .alloc_inode = debugfs_alloc_inode, 2396234ddf4SAl Viro .free_inode = debugfs_free_inode, 240d6e48686SLudwig Nussel }; 241d6e48686SLudwig Nussel 2427c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2437c8d4698SNicolai Stange { 2440ed04a18SJohannes Berg struct debugfs_fsdata *fsd = dentry->d_fsdata; 2457d39bc50SNicolai Stange 2460ed04a18SJohannes Berg if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 2470ed04a18SJohannes Berg return; 2480ed04a18SJohannes Berg 249*bacaaf83SAl Viro /* check it wasn't a dir or automount (no fsdata) */ 250*bacaaf83SAl Viro if (fsd) { 2518c88a474SJohannes Berg WARN_ON(!list_empty(&fsd->cancellations)); 2528c88a474SJohannes Berg mutex_destroy(&fsd->cancellations_mtx); 253f4acfcd4SJohannes Berg } 254f4acfcd4SJohannes Berg 2550ed04a18SJohannes Berg kfree(fsd); 2567c8d4698SNicolai Stange } 2577c8d4698SNicolai Stange 25877b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 25977b3da6eSAl Viro { 260*bacaaf83SAl Viro struct inode *inode = path->dentry->d_inode; 2610ed04a18SJohannes Berg 262*bacaaf83SAl Viro return DEBUGFS_I(inode)->automount(path->dentry, inode->i_private); 26377b3da6eSAl Viro } 26477b3da6eSAl Viro 26577b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 26677b3da6eSAl Viro .d_delete = always_delete_dentry, 2677c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 26877b3da6eSAl Viro .d_automount = debugfs_automount, 26977b3da6eSAl Viro }; 27077b3da6eSAl Viro 271a20971c1SDavid Howells static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc) 2721da177e4SLinus Torvalds { 273cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 274d6e48686SLudwig Nussel int err; 2751da177e4SLinus Torvalds 276d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 277d6e48686SLudwig Nussel if (err) 278a20971c1SDavid Howells return err; 279d6e48686SLudwig Nussel 280d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 28177b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 282d6e48686SLudwig Nussel 283d6e48686SLudwig Nussel debugfs_apply_options(sb); 284d6e48686SLudwig Nussel 285d6e48686SLudwig Nussel return 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288a20971c1SDavid Howells static int debugfs_get_tree(struct fs_context *fc) 2891da177e4SLinus Torvalds { 290a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 291a20971c1SDavid Howells return -EPERM; 292a24c6f7bSPeter Enderborg 293a20971c1SDavid Howells return get_tree_single(fc, debugfs_fill_super); 294a20971c1SDavid Howells } 295a20971c1SDavid Howells 296a20971c1SDavid Howells static void debugfs_free_fc(struct fs_context *fc) 297a20971c1SDavid Howells { 298a20971c1SDavid Howells kfree(fc->s_fs_info); 299a20971c1SDavid Howells } 300a20971c1SDavid Howells 301a20971c1SDavid Howells static const struct fs_context_operations debugfs_context_ops = { 302a20971c1SDavid Howells .free = debugfs_free_fc, 303a20971c1SDavid Howells .parse_param = debugfs_parse_param, 304a20971c1SDavid Howells .get_tree = debugfs_get_tree, 305a20971c1SDavid Howells .reconfigure = debugfs_reconfigure, 306a20971c1SDavid Howells }; 307a20971c1SDavid Howells 308a20971c1SDavid Howells static int debugfs_init_fs_context(struct fs_context *fc) 309a20971c1SDavid Howells { 310a20971c1SDavid Howells struct debugfs_fs_info *fsi; 311a20971c1SDavid Howells 312a20971c1SDavid Howells fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 313a20971c1SDavid Howells if (!fsi) 314a20971c1SDavid Howells return -ENOMEM; 315a20971c1SDavid Howells 316a20971c1SDavid Howells fsi->mode = DEBUGFS_DEFAULT_MODE; 317a20971c1SDavid Howells 318a20971c1SDavid Howells fc->s_fs_info = fsi; 319a20971c1SDavid Howells fc->ops = &debugfs_context_ops; 320a20971c1SDavid Howells return 0; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 3241da177e4SLinus Torvalds .owner = THIS_MODULE, 3251da177e4SLinus Torvalds .name = "debugfs", 326a20971c1SDavid Howells .init_fs_context = debugfs_init_fs_context, 327a20971c1SDavid Howells .parameters = debugfs_param_specs, 3281da177e4SLinus Torvalds .kill_sb = kill_litter_super, 3291da177e4SLinus Torvalds }; 3307f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 3311da177e4SLinus Torvalds 332a7c5437bSOmar Sandoval /** 333a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 334a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 335a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 336a7c5437bSOmar Sandoval * 337a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 338a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 339a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 340a7c5437bSOmar Sandoval * 341a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 342a7c5437bSOmar Sandoval * returned. 343a7c5437bSOmar Sandoval */ 344a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 345a7c5437bSOmar Sandoval { 346a7c5437bSOmar Sandoval struct dentry *dentry; 347a7c5437bSOmar Sandoval 348bc6de804SGreg Kroah-Hartman if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 349a7c5437bSOmar Sandoval return NULL; 350a7c5437bSOmar Sandoval 351a7c5437bSOmar Sandoval if (!parent) 352a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 353a7c5437bSOmar Sandoval 3546c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 355a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 356a7c5437bSOmar Sandoval return NULL; 357a7c5437bSOmar Sandoval return dentry; 358a7c5437bSOmar Sandoval } 359a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 360a7c5437bSOmar Sandoval 361190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 362c3b1a350SAl Viro { 363190afd81SAl Viro struct dentry *dentry; 364c3b1a350SAl Viro int error; 365c3b1a350SAl Viro 366a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 367a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 368a24c6f7bSPeter Enderborg 36956348560SGreg Kroah-Hartman if (!debugfs_initialized()) 37056348560SGreg Kroah-Hartman return ERR_PTR(-ENOENT); 37156348560SGreg Kroah-Hartman 372d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 373c3b1a350SAl Viro 374c9e15f25SGreg KH if (IS_ERR(parent)) 375c9e15f25SGreg KH return parent; 376c9e15f25SGreg KH 377c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 378c3b1a350SAl Viro &debugfs_mount_count); 37943e23b6cSGreg Kroah-Hartman if (error) { 38043e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 381190afd81SAl Viro return ERR_PTR(error); 38243e23b6cSGreg Kroah-Hartman } 383c3b1a350SAl Viro 384cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 385cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 386cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 387cfa57c11SAl Viro * have around. 388cfa57c11SAl Viro */ 389cfa57c11SAl Viro if (!parent) 390cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 391cfa57c11SAl Viro 3925955102cSAl Viro inode_lock(d_inode(parent)); 393a3d1e7ebSAl Viro if (unlikely(IS_DEADDIR(d_inode(parent)))) 394a3d1e7ebSAl Viro dentry = ERR_PTR(-ENOENT); 395a3d1e7ebSAl Viro else 396cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3972b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 398c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 399c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 400c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 401c33d4423SGreg Kroah-Hartman else 402c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 403c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 404190afd81SAl Viro dput(dentry); 405190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 406190afd81SAl Viro } 4070ee9608cSDaniel Borkmann 4080ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 4095955102cSAl Viro inode_unlock(d_inode(parent)); 4100ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 4110ee9608cSDaniel Borkmann } 4120ee9608cSDaniel Borkmann 413190afd81SAl Viro return dentry; 414190afd81SAl Viro } 415190afd81SAl Viro 4165233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 417190afd81SAl Viro { 4185955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 419190afd81SAl Viro dput(dentry); 420190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 421ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 422190afd81SAl Viro } 4235233e311SAl Viro 4245233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 4255233e311SAl Viro { 4265955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 427190afd81SAl Viro return dentry; 428190afd81SAl Viro } 429190afd81SAl Viro 43049d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 43149d200deSNicolai Stange struct dentry *parent, void *data, 43249d200deSNicolai Stange const struct file_operations *proxy_fops, 4338dc6d81cSJohannes Berg const void *real_fops) 43449d200deSNicolai Stange { 43549d200deSNicolai Stange struct dentry *dentry; 43649d200deSNicolai Stange struct inode *inode; 43749d200deSNicolai Stange 43849d200deSNicolai Stange if (!(mode & S_IFMT)) 43949d200deSNicolai Stange mode |= S_IFREG; 44049d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 44149d200deSNicolai Stange dentry = start_creating(name, parent); 44249d200deSNicolai Stange 4437d39bc50SNicolai Stange if (IS_ERR(dentry)) 444ff9fb72bSGreg Kroah-Hartman return dentry; 44549d200deSNicolai Stange 446a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 447a24c6f7bSPeter Enderborg failed_creating(dentry); 448a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 449a24c6f7bSPeter Enderborg } 450a24c6f7bSPeter Enderborg 45149d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 45243e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 45343e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 45443e23b6cSGreg Kroah-Hartman name); 45549d200deSNicolai Stange return failed_creating(dentry); 45643e23b6cSGreg Kroah-Hartman } 45749d200deSNicolai Stange 45849d200deSNicolai Stange inode->i_mode = mode; 45949d200deSNicolai Stange inode->i_private = data; 46049d200deSNicolai Stange 4615496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 46249d200deSNicolai Stange inode->i_fop = proxy_fops; 4637d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 4647d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 46549d200deSNicolai Stange 46649d200deSNicolai Stange d_instantiate(dentry, inode); 46749d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 46849d200deSNicolai Stange return end_creating(dentry); 46949d200deSNicolai Stange } 47049d200deSNicolai Stange 4718dc6d81cSJohannes Berg struct dentry *debugfs_create_file_full(const char *name, umode_t mode, 4721da177e4SLinus Torvalds struct dentry *parent, void *data, 47399ac48f5SArjan van de Ven const struct file_operations *fops) 4741da177e4SLinus Torvalds { 4758dc6d81cSJohannes Berg if (WARN_ON((unsigned long)fops & 476f8f25893SJohannes Berg DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 4778dc6d81cSJohannes Berg return ERR_PTR(-EINVAL); 478c3b1a350SAl Viro 47949d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 48049d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 48149d200deSNicolai Stange &debugfs_noop_file_operations, 48249d200deSNicolai Stange fops); 4831da177e4SLinus Torvalds } 4848dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_full); 4858dc6d81cSJohannes Berg 4868dc6d81cSJohannes Berg struct dentry *debugfs_create_file_short(const char *name, umode_t mode, 4878dc6d81cSJohannes Berg struct dentry *parent, void *data, 4888dc6d81cSJohannes Berg const struct debugfs_short_fops *fops) 4898dc6d81cSJohannes Berg { 4908dc6d81cSJohannes Berg if (WARN_ON((unsigned long)fops & 491f8f25893SJohannes Berg DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 4928dc6d81cSJohannes Berg return ERR_PTR(-EINVAL); 4938dc6d81cSJohannes Berg 4948dc6d81cSJohannes Berg return __debugfs_create_file(name, mode, parent, data, 495f8f25893SJohannes Berg fops ? &debugfs_full_short_proxy_file_operations : 4968dc6d81cSJohannes Berg &debugfs_noop_file_operations, 497f8f25893SJohannes Berg fops); 4988dc6d81cSJohannes Berg } 4998dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_short); 5001da177e4SLinus Torvalds 501c6468808SNicolai Stange /** 502c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 503c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 504c6468808SNicolai Stange * @mode: the permission that the file should have. 505c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 506c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 507c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 508c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 509c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 510c6468808SNicolai Stange * the open() call. 511c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 512c6468808SNicolai Stange * this file. 513c6468808SNicolai Stange * 514c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 515c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 516c6468808SNicolai Stange * handed it will not get protected against file removals by the 517c6468808SNicolai Stange * debugfs core. 518c6468808SNicolai Stange * 519c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 5200eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 5210eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 522c6468808SNicolai Stange * debugfs though. 523c6468808SNicolai Stange * 524c6468808SNicolai Stange * Any struct file_operations defined by means of 525c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 526c6468808SNicolai Stange * thus, may be used here. 527c6468808SNicolai Stange */ 52849d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 52949d200deSNicolai Stange struct dentry *parent, void *data, 53049d200deSNicolai Stange const struct file_operations *fops) 53149d200deSNicolai Stange { 53249d200deSNicolai Stange 53349d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 53449d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 53549d200deSNicolai Stange &debugfs_noop_file_operations, 53649d200deSNicolai Stange fops); 53749d200deSNicolai Stange } 538c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 53949d200deSNicolai Stange 5401da177e4SLinus Torvalds /** 541e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 542e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 543e59b4e91SDavid Howells * @mode: the permission that the file should have. 544e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 545e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 546e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 547e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 548e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 549e59b4e91SDavid Howells * the open() call. 550e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 551e59b4e91SDavid Howells * this file. 552e59b4e91SDavid Howells * @file_size: initial file size 553e59b4e91SDavid Howells * 554e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 555e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 556e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 557e59b4e91SDavid Howells * recommended to be used instead.) 558e59b4e91SDavid Howells */ 559526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode, 560e59b4e91SDavid Howells struct dentry *parent, void *data, 561e59b4e91SDavid Howells const struct file_operations *fops, 562e59b4e91SDavid Howells loff_t file_size) 563e59b4e91SDavid Howells { 564e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 565e59b4e91SDavid Howells 566af505cadSNirmoy Das if (!IS_ERR(de)) 5672b0143b5SDavid Howells d_inode(de)->i_size = file_size; 568e59b4e91SDavid Howells } 569e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 570e59b4e91SDavid Howells 571e59b4e91SDavid Howells /** 5721da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5731da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5741da177e4SLinus Torvalds * create. 5751da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 576e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5771da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5781da177e4SLinus Torvalds * 5791da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5801da177e4SLinus Torvalds * 5811da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5821da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5831da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 584adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 585ff9fb72bSGreg Kroah-Hartman * returned. 5861da177e4SLinus Torvalds * 5876468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 588873760fbSCornelia Huck * returned. 5898bcbde2bSDouglas Anderson * 5908bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 5918bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 5928bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 5938bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5961da177e4SLinus Torvalds { 597ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 598680b3024SAl Viro struct inode *inode; 599ad5abd5bSAl Viro 600ad5abd5bSAl Viro if (IS_ERR(dentry)) 601ff9fb72bSGreg Kroah-Hartman return dentry; 602ad5abd5bSAl Viro 603a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 604a24c6f7bSPeter Enderborg failed_creating(dentry); 605a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 606a24c6f7bSPeter Enderborg } 607a24c6f7bSPeter Enderborg 608edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 60943e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 61043e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 61143e23b6cSGreg Kroah-Hartman name); 6125233e311SAl Viro return failed_creating(dentry); 61343e23b6cSGreg Kroah-Hartman } 614680b3024SAl Viro 615f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 6165496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 617edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 618edac65eaSAl Viro 619edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 620edac65eaSAl Viro inc_nlink(inode); 621680b3024SAl Viro d_instantiate(dentry, inode); 6222b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 6232b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 6245233e311SAl Viro return end_creating(dentry); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /** 62977b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 63077b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 63177b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 63277b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 63377b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 63477b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 63577b3da6eSAl Viro * @data: opaque argument to pass to f(). 63677b3da6eSAl Viro * 63777b3da6eSAl Viro * @f should return what ->d_automount() would. 63877b3da6eSAl Viro */ 63977b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 64077b3da6eSAl Viro struct dentry *parent, 64193faccbbSEric W. Biederman debugfs_automount_t f, 64277b3da6eSAl Viro void *data) 64377b3da6eSAl Viro { 64477b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 64577b3da6eSAl Viro struct inode *inode; 64677b3da6eSAl Viro 64777b3da6eSAl Viro if (IS_ERR(dentry)) 648ff9fb72bSGreg Kroah-Hartman return dentry; 64977b3da6eSAl Viro 650a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 651a24c6f7bSPeter Enderborg failed_creating(dentry); 652a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 653a24c6f7bSPeter Enderborg } 654a24c6f7bSPeter Enderborg 65577b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 65643e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 65743e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 65843e23b6cSGreg Kroah-Hartman name); 65977b3da6eSAl Viro return failed_creating(dentry); 66043e23b6cSGreg Kroah-Hartman } 66177b3da6eSAl Viro 66287243debSSeth Forshee make_empty_dir_inode(inode); 66377b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 66477b3da6eSAl Viro inode->i_private = data; 665*bacaaf83SAl Viro DEBUGFS_I(inode)->automount = f; 666a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 667a8f324a4SRoman Pen inc_nlink(inode); 66877b3da6eSAl Viro d_instantiate(dentry, inode); 669a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 670a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 67177b3da6eSAl Viro return end_creating(dentry); 67277b3da6eSAl Viro } 67377b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 67477b3da6eSAl Viro 67577b3da6eSAl Viro /** 67666f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 67766f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 67866f54963SPeter Oberparleiter * create. 67966f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 680e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 68166f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 68266f54963SPeter Oberparleiter * filesystem. 68366f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 68466f54963SPeter Oberparleiter * symbolic link. 68566f54963SPeter Oberparleiter * 68666f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 68766f54963SPeter Oberparleiter * links to the given target path. 68866f54963SPeter Oberparleiter * 68966f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 69066f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 69166f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 692adc92dd4SDaniel W. S. Almeida * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 693ff9fb72bSGreg Kroah-Hartman * will be returned. 69466f54963SPeter Oberparleiter * 69566f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 696873760fbSCornelia Huck * returned. 69766f54963SPeter Oberparleiter */ 69866f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 69966f54963SPeter Oberparleiter const char *target) 70066f54963SPeter Oberparleiter { 701ad5abd5bSAl Viro struct dentry *dentry; 702680b3024SAl Viro struct inode *inode; 703680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 70466f54963SPeter Oberparleiter if (!link) 705ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 70666f54963SPeter Oberparleiter 707ad5abd5bSAl Viro dentry = start_creating(name, parent); 708ad5abd5bSAl Viro if (IS_ERR(dentry)) { 70966f54963SPeter Oberparleiter kfree(link); 710ff9fb72bSGreg Kroah-Hartman return dentry; 711ad5abd5bSAl Viro } 712ad5abd5bSAl Viro 713edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 714680b3024SAl Viro if (unlikely(!inode)) { 71543e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 71643e23b6cSGreg Kroah-Hartman name); 717ad5abd5bSAl Viro kfree(link); 7185233e311SAl Viro return failed_creating(dentry); 719680b3024SAl Viro } 720edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 7215496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 7225723cb01SAl Viro inode->i_link = link; 723680b3024SAl Viro d_instantiate(dentry, inode); 7245233e311SAl Viro return end_creating(dentry); 72566f54963SPeter Oberparleiter } 72666f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 72766f54963SPeter Oberparleiter 728823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 729e9117a5aSNicolai Stange { 730e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 731e9117a5aSNicolai Stange 7327d39bc50SNicolai Stange /* 7337d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 7347d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 7357d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 7367d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 7377d39bc50SNicolai Stange */ 7387d39bc50SNicolai Stange smp_mb(); 7397d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 7407d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 7417d39bc50SNicolai Stange return; 742f4acfcd4SJohannes Berg 743952c3fceSJohannes Berg /* if this was the last reference, we're done */ 744952c3fceSJohannes Berg if (refcount_dec_and_test(&fsd->active_users)) 7458c88a474SJohannes Berg return; 7468c88a474SJohannes Berg 747952c3fceSJohannes Berg /* 748952c3fceSJohannes Berg * If there's still a reference, the code that obtained it can 749952c3fceSJohannes Berg * be in different states: 750952c3fceSJohannes Berg * - The common case of not using cancellations, or already 751952c3fceSJohannes Berg * after debugfs_leave_cancellation(), where we just need 752952c3fceSJohannes Berg * to wait for debugfs_file_put() which signals the completion; 753952c3fceSJohannes Berg * - inside a cancellation section, i.e. between 754952c3fceSJohannes Berg * debugfs_enter_cancellation() and debugfs_leave_cancellation(), 755952c3fceSJohannes Berg * in which case we need to trigger the ->cancel() function, 756952c3fceSJohannes Berg * and then wait for debugfs_file_put() just like in the 757952c3fceSJohannes Berg * previous case; 758952c3fceSJohannes Berg * - before debugfs_enter_cancellation() (but obviously after 759952c3fceSJohannes Berg * debugfs_file_get()), in which case we may not see the 760952c3fceSJohannes Berg * cancellation in the list on the first round of the loop, 761952c3fceSJohannes Berg * but debugfs_enter_cancellation() signals the completion 762952c3fceSJohannes Berg * after adding it, so this code gets woken up to call the 763952c3fceSJohannes Berg * ->cancel() function. 764952c3fceSJohannes Berg */ 7658c88a474SJohannes Berg while (refcount_read(&fsd->active_users)) { 7668c88a474SJohannes Berg struct debugfs_cancellation *c; 7678c88a474SJohannes Berg 7688c88a474SJohannes Berg /* 7698c88a474SJohannes Berg * Lock the cancellations. Note that the cancellations 7708c88a474SJohannes Berg * structs are meant to be on the stack, so we need to 7718c88a474SJohannes Berg * ensure we either use them here or don't touch them, 7728c88a474SJohannes Berg * and debugfs_leave_cancellation() will wait for this 7738c88a474SJohannes Berg * to be finished processing before exiting one. It may 7748c88a474SJohannes Berg * of course win and remove the cancellation, but then 7758c88a474SJohannes Berg * chances are we never even got into this bit, we only 7768c88a474SJohannes Berg * do if the refcount isn't zero already. 7778c88a474SJohannes Berg */ 7788c88a474SJohannes Berg mutex_lock(&fsd->cancellations_mtx); 7798c88a474SJohannes Berg while ((c = list_first_entry_or_null(&fsd->cancellations, 7808c88a474SJohannes Berg typeof(*c), list))) { 7818c88a474SJohannes Berg list_del_init(&c->list); 7828c88a474SJohannes Berg c->cancel(dentry, c->cancel_data); 7838c88a474SJohannes Berg } 7848c88a474SJohannes Berg mutex_unlock(&fsd->cancellations_mtx); 7858c88a474SJohannes Berg 7868c88a474SJohannes Berg wait_for_completion(&fsd->active_users_drained); 7878c88a474SJohannes Berg } 788e9117a5aSNicolai Stange } 789e9117a5aSNicolai Stange 790a3d1e7ebSAl Viro static void remove_one(struct dentry *victim) 7911da177e4SLinus Torvalds { 792a3d1e7ebSAl Viro if (d_is_reg(victim)) 793a3d1e7ebSAl Viro __debugfs_file_removed(victim); 7941da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 797cfc94cdfSJan Kara /** 798a3d1e7ebSAl Viro * debugfs_remove - recursively removes a directory 799398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 800398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 8019505e637SHaavard Skinnemoen * 8029505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 8039505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 8049505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 8059505e637SHaavard Skinnemoen * 8069505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 8079505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 8089505e637SHaavard Skinnemoen * removed, you are responsible here. 8099505e637SHaavard Skinnemoen */ 810a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry) 8119505e637SHaavard Skinnemoen { 812a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 8139505e637SHaavard Skinnemoen return; 8149505e637SHaavard Skinnemoen 815a3d1e7ebSAl Viro simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 816a3d1e7ebSAl Viro simple_recursive_removal(dentry, remove_one); 817776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 818776164c1SOleg Nesterov } 819a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove); 8209505e637SHaavard Skinnemoen 8219505e637SHaavard Skinnemoen /** 822dec9b2f1SGreg Kroah-Hartman * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it 823dec9b2f1SGreg Kroah-Hartman * @name: a pointer to a string containing the name of the item to look up. 824dec9b2f1SGreg Kroah-Hartman * @parent: a pointer to the parent dentry of the item. 825dec9b2f1SGreg Kroah-Hartman * 826dec9b2f1SGreg Kroah-Hartman * This is the equlivant of doing something like 827dec9b2f1SGreg Kroah-Hartman * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting 828dec9b2f1SGreg Kroah-Hartman * handled for the directory being looked up. 829dec9b2f1SGreg Kroah-Hartman */ 830dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent) 831dec9b2f1SGreg Kroah-Hartman { 832dec9b2f1SGreg Kroah-Hartman struct dentry *dentry; 833dec9b2f1SGreg Kroah-Hartman 834dec9b2f1SGreg Kroah-Hartman dentry = debugfs_lookup(name, parent); 835dec9b2f1SGreg Kroah-Hartman if (!dentry) 836dec9b2f1SGreg Kroah-Hartman return; 837dec9b2f1SGreg Kroah-Hartman 838dec9b2f1SGreg Kroah-Hartman debugfs_remove(dentry); 839dec9b2f1SGreg Kroah-Hartman dput(dentry); 840dec9b2f1SGreg Kroah-Hartman } 841dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove); 842dec9b2f1SGreg Kroah-Hartman 843dec9b2f1SGreg Kroah-Hartman /** 844cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 845cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 846cfc94cdfSJan Kara * should be a directory dentry. 847cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 848cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 849cfc94cdfSJan Kara * moved. This should be a directory dentry. 850cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 851cfc94cdfSJan Kara * 852cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 853cfc94cdfSJan Kara * exist for rename to succeed. 854cfc94cdfSJan Kara * 855cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 85688cd618dSBagas Sanjaya * reflect renaming) if it succeeds. If an error occurs, ERR_PTR(-ERROR) 857d3002468SQi Zheng * will be returned. 858cfc94cdfSJan Kara * 859cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 860cfc94cdfSJan Kara * returned. 861cfc94cdfSJan Kara */ 862cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 863cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 864cfc94cdfSJan Kara { 865cfc94cdfSJan Kara int error; 866cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 86749d31c2fSAl Viro struct name_snapshot old_name; 868cfc94cdfSJan Kara 869d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 870d88c93f0SGreg Kroah-Hartman return old_dir; 871d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 872d88c93f0SGreg Kroah-Hartman return new_dir; 873d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 874d88c93f0SGreg Kroah-Hartman return old_dentry; 875d88c93f0SGreg Kroah-Hartman 876cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 877cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 8782b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 879cfc94cdfSJan Kara goto exit; 880cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 8812b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 882cfc94cdfSJan Kara d_mountpoint(old_dentry)) 883cfc94cdfSJan Kara goto exit; 884cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 885cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 8862b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 887cfc94cdfSJan Kara goto exit; 888cfc94cdfSJan Kara 88949d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 890cfc94cdfSJan Kara 891e18275aeSChristian Brauner error = simple_rename(&nop_mnt_idmap, d_inode(old_dir), old_dentry, 892549c7297SChristian Brauner d_inode(new_dir), dentry, 0); 893cfc94cdfSJan Kara if (error) { 89449d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 895cfc94cdfSJan Kara goto exit; 896cfc94cdfSJan Kara } 897cfc94cdfSJan Kara d_move(old_dentry, dentry); 898f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 899e36cb0b8SDavid Howells d_is_dir(old_dentry), 9005a190ae6SAl Viro NULL, old_dentry); 90149d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 902cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 903cfc94cdfSJan Kara dput(dentry); 904cfc94cdfSJan Kara return old_dentry; 905cfc94cdfSJan Kara exit: 906cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 907cfc94cdfSJan Kara dput(dentry); 908cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 909ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 910ff9fb72bSGreg Kroah-Hartman return dentry; 911ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 912cfc94cdfSJan Kara } 913cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 914cfc94cdfSJan Kara 915c0f92ba9SFrederic Weisbecker /** 916c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 917c0f92ba9SFrederic Weisbecker */ 918c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 919c0f92ba9SFrederic Weisbecker { 920c0f92ba9SFrederic Weisbecker return debugfs_registered; 921c0f92ba9SFrederic Weisbecker } 922c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 923c0f92ba9SFrederic Weisbecker 924a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str) 925a24c6f7bSPeter Enderborg { 926a24c6f7bSPeter Enderborg if (str) { 927a24c6f7bSPeter Enderborg if (!strcmp(str, "on")) 928a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 929a24c6f7bSPeter Enderborg else if (!strcmp(str, "no-mount")) 930a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API; 931a24c6f7bSPeter Enderborg else if (!strcmp(str, "off")) 932a24c6f7bSPeter Enderborg debugfs_allow = 0; 933a24c6f7bSPeter Enderborg } 934a24c6f7bSPeter Enderborg 935a24c6f7bSPeter Enderborg return 0; 936a24c6f7bSPeter Enderborg } 937a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel); 9381da177e4SLinus Torvalds static int __init debugfs_init(void) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds int retval; 9411da177e4SLinus Torvalds 942a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 943a24c6f7bSPeter Enderborg return -EPERM; 944a24c6f7bSPeter Enderborg 945f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 946f9bb4882SEric W. Biederman if (retval) 947f9bb4882SEric W. Biederman return retval; 9481da177e4SLinus Torvalds 949268b3611SAl Viro debugfs_inode_cachep = kmem_cache_create("debugfs_inode_cache", 950268b3611SAl Viro sizeof(struct debugfs_inode_info), 0, 951268b3611SAl Viro SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 952268b3611SAl Viro init_once); 953268b3611SAl Viro if (debugfs_inode_cachep == NULL) { 954f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 955268b3611SAl Viro return -ENOMEM; 956268b3611SAl Viro } 957c0f92ba9SFrederic Weisbecker 958268b3611SAl Viro retval = register_filesystem(&debug_fs_type); 959268b3611SAl Viro if (retval) { // Really not going to happen 960268b3611SAl Viro sysfs_remove_mount_point(kernel_kobj, "debug"); 961268b3611SAl Viro kmem_cache_destroy(debugfs_inode_cachep); 9621da177e4SLinus Torvalds return retval; 9631da177e4SLinus Torvalds } 964268b3611SAl Viro debugfs_registered = true; 965268b3611SAl Viro return 0; 966268b3611SAl Viro } 9671da177e4SLinus Torvalds core_initcall(debugfs_init); 968