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> 17*a20971c1SDavid Howells #include <linux/fs_context.h> 18*a20971c1SDavid 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 80*a20971c1SDavid 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, 92d6e48686SLudwig Nussel }; 93d6e48686SLudwig Nussel 94*a20971c1SDavid Howells static const struct fs_parameter_spec debugfs_param_specs[] = { 95*a20971c1SDavid Howells fsparam_u32 ("gid", Opt_gid), 96*a20971c1SDavid Howells fsparam_u32oct ("mode", Opt_mode), 97*a20971c1SDavid Howells fsparam_u32 ("uid", Opt_uid), 98*a20971c1SDavid Howells {} 99d6e48686SLudwig Nussel }; 100d6e48686SLudwig Nussel 101*a20971c1SDavid Howells static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 102d6e48686SLudwig Nussel { 103*a20971c1SDavid Howells struct debugfs_fs_info *opts = fc->s_fs_info; 104*a20971c1SDavid Howells struct fs_parse_result result; 1057dc05881SEric W. Biederman kuid_t uid; 1067dc05881SEric W. Biederman kgid_t gid; 107*a20971c1SDavid Howells int opt; 108d6e48686SLudwig Nussel 109*a20971c1SDavid Howells opt = fs_parse(fc, debugfs_param_specs, param, &result); 110*a20971c1SDavid Howells if (opt < 0) 111*a20971c1SDavid Howells return opt; 112d6e48686SLudwig Nussel 113*a20971c1SDavid Howells switch (opt) { 114d6e48686SLudwig Nussel case Opt_uid: 115*a20971c1SDavid Howells uid = make_kuid(current_user_ns(), result.uint_32); 1167dc05881SEric W. Biederman if (!uid_valid(uid)) 117*a20971c1SDavid Howells return invalf(fc, "Unknown uid"); 1187dc05881SEric W. Biederman opts->uid = uid; 119d6e48686SLudwig Nussel break; 120d6e48686SLudwig Nussel case Opt_gid: 121*a20971c1SDavid Howells gid = make_kgid(current_user_ns(), result.uint_32); 1227dc05881SEric W. Biederman if (!gid_valid(gid)) 123*a20971c1SDavid Howells return invalf(fc, "Unknown gid"); 1247dc05881SEric W. Biederman opts->gid = gid; 125d6e48686SLudwig Nussel break; 126d6e48686SLudwig Nussel case Opt_mode: 127*a20971c1SDavid Howells opts->mode = result.uint_32 & S_IALLUGO; 128d6e48686SLudwig Nussel break; 129d6e48686SLudwig Nussel /* 130d6e48686SLudwig Nussel * We might like to report bad mount options here; 131d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 132d6e48686SLudwig Nussel */ 133d6e48686SLudwig Nussel } 134b8de524cSBrian Norris 135*a20971c1SDavid Howells opts->opts |= BIT(opt); 136d6e48686SLudwig Nussel 137d6e48686SLudwig Nussel return 0; 138d6e48686SLudwig Nussel } 139d6e48686SLudwig Nussel 140b8de524cSBrian Norris static void _debugfs_apply_options(struct super_block *sb, bool remount) 141d6e48686SLudwig Nussel { 142d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1432b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 144d6e48686SLudwig Nussel 145b8de524cSBrian Norris /* 146b8de524cSBrian Norris * On remount, only reset mode/uid/gid if they were provided as mount 147b8de524cSBrian Norris * options. 148b8de524cSBrian Norris */ 149b8de524cSBrian Norris 150*a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_mode)) { 151d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 152*a20971c1SDavid Howells inode->i_mode |= fsi->mode; 153b8de524cSBrian Norris } 154d6e48686SLudwig Nussel 155*a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_uid)) 156*a20971c1SDavid Howells inode->i_uid = fsi->uid; 157d6e48686SLudwig Nussel 158*a20971c1SDavid Howells if (!remount || fsi->opts & BIT(Opt_gid)) 159*a20971c1SDavid Howells inode->i_gid = fsi->gid; 160b8de524cSBrian Norris } 161b8de524cSBrian Norris 162b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb) 163b8de524cSBrian Norris { 164b8de524cSBrian Norris _debugfs_apply_options(sb, false); 165b8de524cSBrian Norris } 166b8de524cSBrian Norris 167b8de524cSBrian Norris static void debugfs_apply_options_remount(struct super_block *sb) 168b8de524cSBrian Norris { 169b8de524cSBrian Norris _debugfs_apply_options(sb, true); 170d6e48686SLudwig Nussel } 171d6e48686SLudwig Nussel 172*a20971c1SDavid Howells static int debugfs_reconfigure(struct fs_context *fc) 173d6e48686SLudwig Nussel { 174*a20971c1SDavid Howells struct super_block *sb = fc->root->d_sb; 175*a20971c1SDavid Howells struct debugfs_fs_info *sb_opts = sb->s_fs_info; 176*a20971c1SDavid Howells struct debugfs_fs_info *new_opts = fc->s_fs_info; 177d6e48686SLudwig Nussel 17802b9984dSTheodore Ts'o sync_filesystem(sb); 179d6e48686SLudwig Nussel 180*a20971c1SDavid Howells /* structure copy of new mount options to sb */ 181*a20971c1SDavid Howells *sb_opts = *new_opts; 182b8de524cSBrian Norris debugfs_apply_options_remount(sb); 183d6e48686SLudwig Nussel 184*a20971c1SDavid Howells return 0; 185d6e48686SLudwig Nussel } 186d6e48686SLudwig Nussel 187d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 188d6e48686SLudwig Nussel { 189d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 190d6e48686SLudwig Nussel 191*a20971c1SDavid Howells if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID)) 1927dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 193*a20971c1SDavid Howells from_kuid_munged(&init_user_ns, fsi->uid)); 194*a20971c1SDavid Howells if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID)) 1957dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 196*a20971c1SDavid Howells from_kgid_munged(&init_user_ns, fsi->gid)); 197*a20971c1SDavid Howells if (fsi->mode != DEBUGFS_DEFAULT_MODE) 198*a20971c1SDavid Howells seq_printf(m, ",mode=%o", fsi->mode); 199d6e48686SLudwig Nussel 200d6e48686SLudwig Nussel return 0; 201d6e48686SLudwig Nussel } 202d6e48686SLudwig Nussel 2036234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 2040db59e59SAl Viro { 2050db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 2065723cb01SAl Viro kfree(inode->i_link); 20793b919daSAl Viro free_inode_nonrcu(inode); 20893b919daSAl Viro } 20993b919daSAl Viro 210d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 211d6e48686SLudwig Nussel .statfs = simple_statfs, 212d6e48686SLudwig Nussel .show_options = debugfs_show_options, 2136234ddf4SAl Viro .free_inode = debugfs_free_inode, 214d6e48686SLudwig Nussel }; 215d6e48686SLudwig Nussel 2167c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2177c8d4698SNicolai Stange { 2180ed04a18SJohannes Berg struct debugfs_fsdata *fsd = dentry->d_fsdata; 2197d39bc50SNicolai Stange 2200ed04a18SJohannes Berg if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 2210ed04a18SJohannes Berg return; 2220ed04a18SJohannes Berg 223f4acfcd4SJohannes Berg /* check it wasn't a dir (no fsdata) or automount (no real_fops) */ 224f4acfcd4SJohannes Berg if (fsd && fsd->real_fops) { 2258c88a474SJohannes Berg WARN_ON(!list_empty(&fsd->cancellations)); 2268c88a474SJohannes Berg mutex_destroy(&fsd->cancellations_mtx); 227f4acfcd4SJohannes Berg } 228f4acfcd4SJohannes Berg 2290ed04a18SJohannes Berg kfree(fsd); 2307c8d4698SNicolai Stange } 2317c8d4698SNicolai Stange 23277b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 23377b3da6eSAl Viro { 2340ed04a18SJohannes Berg struct debugfs_fsdata *fsd = path->dentry->d_fsdata; 2350ed04a18SJohannes Berg 2360ed04a18SJohannes Berg return fsd->automount(path->dentry, d_inode(path->dentry)->i_private); 23777b3da6eSAl Viro } 23877b3da6eSAl Viro 23977b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 24077b3da6eSAl Viro .d_delete = always_delete_dentry, 2417c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 24277b3da6eSAl Viro .d_automount = debugfs_automount, 24377b3da6eSAl Viro }; 24477b3da6eSAl Viro 245*a20971c1SDavid Howells static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc) 2461da177e4SLinus Torvalds { 247cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 248d6e48686SLudwig Nussel int err; 2491da177e4SLinus Torvalds 250d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 251d6e48686SLudwig Nussel if (err) 252*a20971c1SDavid Howells return err; 253d6e48686SLudwig Nussel 254d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 25577b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 256d6e48686SLudwig Nussel 257d6e48686SLudwig Nussel debugfs_apply_options(sb); 258d6e48686SLudwig Nussel 259d6e48686SLudwig Nussel return 0; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 262*a20971c1SDavid Howells static int debugfs_get_tree(struct fs_context *fc) 2631da177e4SLinus Torvalds { 264a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 265*a20971c1SDavid Howells return -EPERM; 266a24c6f7bSPeter Enderborg 267*a20971c1SDavid Howells return get_tree_single(fc, debugfs_fill_super); 268*a20971c1SDavid Howells } 269*a20971c1SDavid Howells 270*a20971c1SDavid Howells static void debugfs_free_fc(struct fs_context *fc) 271*a20971c1SDavid Howells { 272*a20971c1SDavid Howells kfree(fc->s_fs_info); 273*a20971c1SDavid Howells } 274*a20971c1SDavid Howells 275*a20971c1SDavid Howells static const struct fs_context_operations debugfs_context_ops = { 276*a20971c1SDavid Howells .free = debugfs_free_fc, 277*a20971c1SDavid Howells .parse_param = debugfs_parse_param, 278*a20971c1SDavid Howells .get_tree = debugfs_get_tree, 279*a20971c1SDavid Howells .reconfigure = debugfs_reconfigure, 280*a20971c1SDavid Howells }; 281*a20971c1SDavid Howells 282*a20971c1SDavid Howells static int debugfs_init_fs_context(struct fs_context *fc) 283*a20971c1SDavid Howells { 284*a20971c1SDavid Howells struct debugfs_fs_info *fsi; 285*a20971c1SDavid Howells 286*a20971c1SDavid Howells fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 287*a20971c1SDavid Howells if (!fsi) 288*a20971c1SDavid Howells return -ENOMEM; 289*a20971c1SDavid Howells 290*a20971c1SDavid Howells fsi->mode = DEBUGFS_DEFAULT_MODE; 291*a20971c1SDavid Howells 292*a20971c1SDavid Howells fc->s_fs_info = fsi; 293*a20971c1SDavid Howells fc->ops = &debugfs_context_ops; 294*a20971c1SDavid Howells return 0; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2981da177e4SLinus Torvalds .owner = THIS_MODULE, 2991da177e4SLinus Torvalds .name = "debugfs", 300*a20971c1SDavid Howells .init_fs_context = debugfs_init_fs_context, 301*a20971c1SDavid Howells .parameters = debugfs_param_specs, 3021da177e4SLinus Torvalds .kill_sb = kill_litter_super, 3031da177e4SLinus Torvalds }; 3047f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 3051da177e4SLinus Torvalds 306a7c5437bSOmar Sandoval /** 307a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 308a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 309a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 310a7c5437bSOmar Sandoval * 311a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 312a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 313a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 314a7c5437bSOmar Sandoval * 315a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 316a7c5437bSOmar Sandoval * returned. 317a7c5437bSOmar Sandoval */ 318a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 319a7c5437bSOmar Sandoval { 320a7c5437bSOmar Sandoval struct dentry *dentry; 321a7c5437bSOmar Sandoval 322bc6de804SGreg Kroah-Hartman if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 323a7c5437bSOmar Sandoval return NULL; 324a7c5437bSOmar Sandoval 325a7c5437bSOmar Sandoval if (!parent) 326a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 327a7c5437bSOmar Sandoval 3286c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 329a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 330a7c5437bSOmar Sandoval return NULL; 331a7c5437bSOmar Sandoval return dentry; 332a7c5437bSOmar Sandoval } 333a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 334a7c5437bSOmar Sandoval 335190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 336c3b1a350SAl Viro { 337190afd81SAl Viro struct dentry *dentry; 338c3b1a350SAl Viro int error; 339c3b1a350SAl Viro 340a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 341a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 342a24c6f7bSPeter Enderborg 34356348560SGreg Kroah-Hartman if (!debugfs_initialized()) 34456348560SGreg Kroah-Hartman return ERR_PTR(-ENOENT); 34556348560SGreg Kroah-Hartman 346d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 347c3b1a350SAl Viro 348c9e15f25SGreg KH if (IS_ERR(parent)) 349c9e15f25SGreg KH return parent; 350c9e15f25SGreg KH 351c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 352c3b1a350SAl Viro &debugfs_mount_count); 35343e23b6cSGreg Kroah-Hartman if (error) { 35443e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 355190afd81SAl Viro return ERR_PTR(error); 35643e23b6cSGreg Kroah-Hartman } 357c3b1a350SAl Viro 358cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 359cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 360cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 361cfa57c11SAl Viro * have around. 362cfa57c11SAl Viro */ 363cfa57c11SAl Viro if (!parent) 364cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 365cfa57c11SAl Viro 3665955102cSAl Viro inode_lock(d_inode(parent)); 367a3d1e7ebSAl Viro if (unlikely(IS_DEADDIR(d_inode(parent)))) 368a3d1e7ebSAl Viro dentry = ERR_PTR(-ENOENT); 369a3d1e7ebSAl Viro else 370cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3712b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 372c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 373c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 374c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 375c33d4423SGreg Kroah-Hartman else 376c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 377c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 378190afd81SAl Viro dput(dentry); 379190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 380190afd81SAl Viro } 3810ee9608cSDaniel Borkmann 3820ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3835955102cSAl Viro inode_unlock(d_inode(parent)); 3840ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3850ee9608cSDaniel Borkmann } 3860ee9608cSDaniel Borkmann 387190afd81SAl Viro return dentry; 388190afd81SAl Viro } 389190afd81SAl Viro 3905233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 391190afd81SAl Viro { 3925955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 393190afd81SAl Viro dput(dentry); 394190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 395ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 396190afd81SAl Viro } 3975233e311SAl Viro 3985233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3995233e311SAl Viro { 4005955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 401190afd81SAl Viro return dentry; 402190afd81SAl Viro } 403190afd81SAl Viro 40449d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 40549d200deSNicolai Stange struct dentry *parent, void *data, 40649d200deSNicolai Stange const struct file_operations *proxy_fops, 40749d200deSNicolai Stange const struct file_operations *real_fops) 40849d200deSNicolai Stange { 40949d200deSNicolai Stange struct dentry *dentry; 41049d200deSNicolai Stange struct inode *inode; 41149d200deSNicolai Stange 41249d200deSNicolai Stange if (!(mode & S_IFMT)) 41349d200deSNicolai Stange mode |= S_IFREG; 41449d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 41549d200deSNicolai Stange dentry = start_creating(name, parent); 41649d200deSNicolai Stange 4177d39bc50SNicolai Stange if (IS_ERR(dentry)) 418ff9fb72bSGreg Kroah-Hartman return dentry; 41949d200deSNicolai Stange 420a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 421a24c6f7bSPeter Enderborg failed_creating(dentry); 422a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 423a24c6f7bSPeter Enderborg } 424a24c6f7bSPeter Enderborg 42549d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 42643e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 42743e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 42843e23b6cSGreg Kroah-Hartman name); 42949d200deSNicolai Stange return failed_creating(dentry); 43043e23b6cSGreg Kroah-Hartman } 43149d200deSNicolai Stange 43249d200deSNicolai Stange inode->i_mode = mode; 43349d200deSNicolai Stange inode->i_private = data; 43449d200deSNicolai Stange 4355496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 43649d200deSNicolai Stange inode->i_fop = proxy_fops; 4377d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 4387d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 43949d200deSNicolai Stange 44049d200deSNicolai Stange d_instantiate(dentry, inode); 44149d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 44249d200deSNicolai Stange return end_creating(dentry); 44349d200deSNicolai Stange } 44449d200deSNicolai Stange 4451da177e4SLinus Torvalds /** 4461da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 4471da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 448be030e65SAlberto Bertogli * @mode: the permission that the file should have. 4491da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 450e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4511da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4521da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 4538e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 4541da177e4SLinus Torvalds * the open() call. 4551da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 4561da177e4SLinus Torvalds * this file. 4571da177e4SLinus Torvalds * 4581da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 459be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 460be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4611da177e4SLinus Torvalds * recommended to be used instead.) 4621da177e4SLinus Torvalds * 4631da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4641da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4651da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 466adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 467ff9fb72bSGreg Kroah-Hartman * returned. 4681da177e4SLinus Torvalds * 4696468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 470873760fbSCornelia Huck * returned. 4718bcbde2bSDouglas Anderson * 4728bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 4738bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 4748bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 4758bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 4761da177e4SLinus Torvalds */ 477f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4781da177e4SLinus Torvalds struct dentry *parent, void *data, 47999ac48f5SArjan van de Ven const struct file_operations *fops) 4801da177e4SLinus Torvalds { 481c3b1a350SAl Viro 48249d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 48349d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 48449d200deSNicolai Stange &debugfs_noop_file_operations, 48549d200deSNicolai Stange fops); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4881da177e4SLinus Torvalds 489c6468808SNicolai Stange /** 490c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 491c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 492c6468808SNicolai Stange * @mode: the permission that the file should have. 493c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 494c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 495c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 496c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 497c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 498c6468808SNicolai Stange * the open() call. 499c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 500c6468808SNicolai Stange * this file. 501c6468808SNicolai Stange * 502c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 503c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 504c6468808SNicolai Stange * handed it will not get protected against file removals by the 505c6468808SNicolai Stange * debugfs core. 506c6468808SNicolai Stange * 507c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 5080eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 5090eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 510c6468808SNicolai Stange * debugfs though. 511c6468808SNicolai Stange * 512c6468808SNicolai Stange * Any struct file_operations defined by means of 513c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 514c6468808SNicolai Stange * thus, may be used here. 515c6468808SNicolai Stange */ 51649d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 51749d200deSNicolai Stange struct dentry *parent, void *data, 51849d200deSNicolai Stange const struct file_operations *fops) 51949d200deSNicolai Stange { 52049d200deSNicolai Stange 52149d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 52249d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 52349d200deSNicolai Stange &debugfs_noop_file_operations, 52449d200deSNicolai Stange fops); 52549d200deSNicolai Stange } 526c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 52749d200deSNicolai Stange 5281da177e4SLinus Torvalds /** 529e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 530e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 531e59b4e91SDavid Howells * @mode: the permission that the file should have. 532e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 533e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 534e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 535e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 536e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 537e59b4e91SDavid Howells * the open() call. 538e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 539e59b4e91SDavid Howells * this file. 540e59b4e91SDavid Howells * @file_size: initial file size 541e59b4e91SDavid Howells * 542e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 543e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 544e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 545e59b4e91SDavid Howells * recommended to be used instead.) 546e59b4e91SDavid Howells */ 547526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode, 548e59b4e91SDavid Howells struct dentry *parent, void *data, 549e59b4e91SDavid Howells const struct file_operations *fops, 550e59b4e91SDavid Howells loff_t file_size) 551e59b4e91SDavid Howells { 552e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 553e59b4e91SDavid Howells 554af505cadSNirmoy Das if (!IS_ERR(de)) 5552b0143b5SDavid Howells d_inode(de)->i_size = file_size; 556e59b4e91SDavid Howells } 557e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 558e59b4e91SDavid Howells 559e59b4e91SDavid Howells /** 5601da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5611da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5621da177e4SLinus Torvalds * create. 5631da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 564e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5651da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5661da177e4SLinus Torvalds * 5671da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5681da177e4SLinus Torvalds * 5691da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5701da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5711da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 572adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 573ff9fb72bSGreg Kroah-Hartman * returned. 5741da177e4SLinus Torvalds * 5756468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 576873760fbSCornelia Huck * returned. 5778bcbde2bSDouglas Anderson * 5788bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 5798bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 5808bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 5818bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 5821da177e4SLinus Torvalds */ 5831da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5841da177e4SLinus Torvalds { 585ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 586680b3024SAl Viro struct inode *inode; 587ad5abd5bSAl Viro 588ad5abd5bSAl Viro if (IS_ERR(dentry)) 589ff9fb72bSGreg Kroah-Hartman return dentry; 590ad5abd5bSAl Viro 591a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 592a24c6f7bSPeter Enderborg failed_creating(dentry); 593a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 594a24c6f7bSPeter Enderborg } 595a24c6f7bSPeter Enderborg 596edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 59743e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 59843e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 59943e23b6cSGreg Kroah-Hartman name); 6005233e311SAl Viro return failed_creating(dentry); 60143e23b6cSGreg Kroah-Hartman } 602680b3024SAl Viro 603f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 6045496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 605edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 606edac65eaSAl Viro 607edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 608edac65eaSAl Viro inc_nlink(inode); 609680b3024SAl Viro d_instantiate(dentry, inode); 6102b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 6112b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 6125233e311SAl Viro return end_creating(dentry); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds /** 61777b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 61877b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 61977b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 62077b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 62177b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 62277b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 62377b3da6eSAl Viro * @data: opaque argument to pass to f(). 62477b3da6eSAl Viro * 62577b3da6eSAl Viro * @f should return what ->d_automount() would. 62677b3da6eSAl Viro */ 62777b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 62877b3da6eSAl Viro struct dentry *parent, 62993faccbbSEric W. Biederman debugfs_automount_t f, 63077b3da6eSAl Viro void *data) 63177b3da6eSAl Viro { 63277b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 6330ed04a18SJohannes Berg struct debugfs_fsdata *fsd; 63477b3da6eSAl Viro struct inode *inode; 63577b3da6eSAl Viro 63677b3da6eSAl Viro if (IS_ERR(dentry)) 637ff9fb72bSGreg Kroah-Hartman return dentry; 63877b3da6eSAl Viro 6390ed04a18SJohannes Berg fsd = kzalloc(sizeof(*fsd), GFP_KERNEL); 6400ed04a18SJohannes Berg if (!fsd) { 6410ed04a18SJohannes Berg failed_creating(dentry); 6420ed04a18SJohannes Berg return ERR_PTR(-ENOMEM); 6430ed04a18SJohannes Berg } 6440ed04a18SJohannes Berg 6450ed04a18SJohannes Berg fsd->automount = f; 6460ed04a18SJohannes Berg 647a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 648a24c6f7bSPeter Enderborg failed_creating(dentry); 6490ed04a18SJohannes Berg kfree(fsd); 650a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 651a24c6f7bSPeter Enderborg } 652a24c6f7bSPeter Enderborg 65377b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 65443e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 65543e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 65643e23b6cSGreg Kroah-Hartman name); 6570ed04a18SJohannes Berg kfree(fsd); 65877b3da6eSAl Viro return failed_creating(dentry); 65943e23b6cSGreg Kroah-Hartman } 66077b3da6eSAl Viro 66187243debSSeth Forshee make_empty_dir_inode(inode); 66277b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 66377b3da6eSAl Viro inode->i_private = data; 6640ed04a18SJohannes Berg dentry->d_fsdata = fsd; 665a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 666a8f324a4SRoman Pen inc_nlink(inode); 66777b3da6eSAl Viro d_instantiate(dentry, inode); 668a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 669a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 67077b3da6eSAl Viro return end_creating(dentry); 67177b3da6eSAl Viro } 67277b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 67377b3da6eSAl Viro 67477b3da6eSAl Viro /** 67566f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 67666f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 67766f54963SPeter Oberparleiter * create. 67866f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 679e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 68066f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 68166f54963SPeter Oberparleiter * filesystem. 68266f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 68366f54963SPeter Oberparleiter * symbolic link. 68466f54963SPeter Oberparleiter * 68566f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 68666f54963SPeter Oberparleiter * links to the given target path. 68766f54963SPeter Oberparleiter * 68866f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 68966f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 69066f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 691adc92dd4SDaniel W. S. Almeida * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 692ff9fb72bSGreg Kroah-Hartman * will be returned. 69366f54963SPeter Oberparleiter * 69466f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 695873760fbSCornelia Huck * returned. 69666f54963SPeter Oberparleiter */ 69766f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 69866f54963SPeter Oberparleiter const char *target) 69966f54963SPeter Oberparleiter { 700ad5abd5bSAl Viro struct dentry *dentry; 701680b3024SAl Viro struct inode *inode; 702680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 70366f54963SPeter Oberparleiter if (!link) 704ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 70566f54963SPeter Oberparleiter 706ad5abd5bSAl Viro dentry = start_creating(name, parent); 707ad5abd5bSAl Viro if (IS_ERR(dentry)) { 70866f54963SPeter Oberparleiter kfree(link); 709ff9fb72bSGreg Kroah-Hartman return dentry; 710ad5abd5bSAl Viro } 711ad5abd5bSAl Viro 712edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 713680b3024SAl Viro if (unlikely(!inode)) { 71443e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 71543e23b6cSGreg Kroah-Hartman name); 716ad5abd5bSAl Viro kfree(link); 7175233e311SAl Viro return failed_creating(dentry); 718680b3024SAl Viro } 719edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 7205496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 7215723cb01SAl Viro inode->i_link = link; 722680b3024SAl Viro d_instantiate(dentry, inode); 7235233e311SAl Viro return end_creating(dentry); 72466f54963SPeter Oberparleiter } 72566f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 72666f54963SPeter Oberparleiter 727823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 728e9117a5aSNicolai Stange { 729e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 730e9117a5aSNicolai Stange 7317d39bc50SNicolai Stange /* 7327d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 7337d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 7347d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 7357d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 7367d39bc50SNicolai Stange */ 7377d39bc50SNicolai Stange smp_mb(); 7387d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 7397d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 7407d39bc50SNicolai Stange return; 741f4acfcd4SJohannes Berg 742952c3fceSJohannes Berg /* if this was the last reference, we're done */ 743952c3fceSJohannes Berg if (refcount_dec_and_test(&fsd->active_users)) 7448c88a474SJohannes Berg return; 7458c88a474SJohannes Berg 746952c3fceSJohannes Berg /* 747952c3fceSJohannes Berg * If there's still a reference, the code that obtained it can 748952c3fceSJohannes Berg * be in different states: 749952c3fceSJohannes Berg * - The common case of not using cancellations, or already 750952c3fceSJohannes Berg * after debugfs_leave_cancellation(), where we just need 751952c3fceSJohannes Berg * to wait for debugfs_file_put() which signals the completion; 752952c3fceSJohannes Berg * - inside a cancellation section, i.e. between 753952c3fceSJohannes Berg * debugfs_enter_cancellation() and debugfs_leave_cancellation(), 754952c3fceSJohannes Berg * in which case we need to trigger the ->cancel() function, 755952c3fceSJohannes Berg * and then wait for debugfs_file_put() just like in the 756952c3fceSJohannes Berg * previous case; 757952c3fceSJohannes Berg * - before debugfs_enter_cancellation() (but obviously after 758952c3fceSJohannes Berg * debugfs_file_get()), in which case we may not see the 759952c3fceSJohannes Berg * cancellation in the list on the first round of the loop, 760952c3fceSJohannes Berg * but debugfs_enter_cancellation() signals the completion 761952c3fceSJohannes Berg * after adding it, so this code gets woken up to call the 762952c3fceSJohannes Berg * ->cancel() function. 763952c3fceSJohannes Berg */ 7648c88a474SJohannes Berg while (refcount_read(&fsd->active_users)) { 7658c88a474SJohannes Berg struct debugfs_cancellation *c; 7668c88a474SJohannes Berg 7678c88a474SJohannes Berg /* 7688c88a474SJohannes Berg * Lock the cancellations. Note that the cancellations 7698c88a474SJohannes Berg * structs are meant to be on the stack, so we need to 7708c88a474SJohannes Berg * ensure we either use them here or don't touch them, 7718c88a474SJohannes Berg * and debugfs_leave_cancellation() will wait for this 7728c88a474SJohannes Berg * to be finished processing before exiting one. It may 7738c88a474SJohannes Berg * of course win and remove the cancellation, but then 7748c88a474SJohannes Berg * chances are we never even got into this bit, we only 7758c88a474SJohannes Berg * do if the refcount isn't zero already. 7768c88a474SJohannes Berg */ 7778c88a474SJohannes Berg mutex_lock(&fsd->cancellations_mtx); 7788c88a474SJohannes Berg while ((c = list_first_entry_or_null(&fsd->cancellations, 7798c88a474SJohannes Berg typeof(*c), list))) { 7808c88a474SJohannes Berg list_del_init(&c->list); 7818c88a474SJohannes Berg c->cancel(dentry, c->cancel_data); 7828c88a474SJohannes Berg } 7838c88a474SJohannes Berg mutex_unlock(&fsd->cancellations_mtx); 7848c88a474SJohannes Berg 7858c88a474SJohannes Berg wait_for_completion(&fsd->active_users_drained); 7868c88a474SJohannes Berg } 787e9117a5aSNicolai Stange } 788e9117a5aSNicolai Stange 789a3d1e7ebSAl Viro static void remove_one(struct dentry *victim) 7901da177e4SLinus Torvalds { 791a3d1e7ebSAl Viro if (d_is_reg(victim)) 792a3d1e7ebSAl Viro __debugfs_file_removed(victim); 7931da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 796cfc94cdfSJan Kara /** 797a3d1e7ebSAl Viro * debugfs_remove - recursively removes a directory 798398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 799398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 8009505e637SHaavard Skinnemoen * 8019505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 8029505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 8039505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 8049505e637SHaavard Skinnemoen * 8059505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 8069505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 8079505e637SHaavard Skinnemoen * removed, you are responsible here. 8089505e637SHaavard Skinnemoen */ 809a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry) 8109505e637SHaavard Skinnemoen { 811a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 8129505e637SHaavard Skinnemoen return; 8139505e637SHaavard Skinnemoen 814a3d1e7ebSAl Viro simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 815a3d1e7ebSAl Viro simple_recursive_removal(dentry, remove_one); 816776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 817776164c1SOleg Nesterov } 818a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove); 8199505e637SHaavard Skinnemoen 8209505e637SHaavard Skinnemoen /** 821dec9b2f1SGreg Kroah-Hartman * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it 822dec9b2f1SGreg Kroah-Hartman * @name: a pointer to a string containing the name of the item to look up. 823dec9b2f1SGreg Kroah-Hartman * @parent: a pointer to the parent dentry of the item. 824dec9b2f1SGreg Kroah-Hartman * 825dec9b2f1SGreg Kroah-Hartman * This is the equlivant of doing something like 826dec9b2f1SGreg Kroah-Hartman * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting 827dec9b2f1SGreg Kroah-Hartman * handled for the directory being looked up. 828dec9b2f1SGreg Kroah-Hartman */ 829dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent) 830dec9b2f1SGreg Kroah-Hartman { 831dec9b2f1SGreg Kroah-Hartman struct dentry *dentry; 832dec9b2f1SGreg Kroah-Hartman 833dec9b2f1SGreg Kroah-Hartman dentry = debugfs_lookup(name, parent); 834dec9b2f1SGreg Kroah-Hartman if (!dentry) 835dec9b2f1SGreg Kroah-Hartman return; 836dec9b2f1SGreg Kroah-Hartman 837dec9b2f1SGreg Kroah-Hartman debugfs_remove(dentry); 838dec9b2f1SGreg Kroah-Hartman dput(dentry); 839dec9b2f1SGreg Kroah-Hartman } 840dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove); 841dec9b2f1SGreg Kroah-Hartman 842dec9b2f1SGreg Kroah-Hartman /** 843cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 844cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 845cfc94cdfSJan Kara * should be a directory dentry. 846cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 847cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 848cfc94cdfSJan Kara * moved. This should be a directory dentry. 849cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 850cfc94cdfSJan Kara * 851cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 852cfc94cdfSJan Kara * exist for rename to succeed. 853cfc94cdfSJan Kara * 854cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 85588cd618dSBagas Sanjaya * reflect renaming) if it succeeds. If an error occurs, ERR_PTR(-ERROR) 856d3002468SQi Zheng * will be returned. 857cfc94cdfSJan Kara * 858cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 859cfc94cdfSJan Kara * returned. 860cfc94cdfSJan Kara */ 861cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 862cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 863cfc94cdfSJan Kara { 864cfc94cdfSJan Kara int error; 865cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 86649d31c2fSAl Viro struct name_snapshot old_name; 867cfc94cdfSJan Kara 868d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 869d88c93f0SGreg Kroah-Hartman return old_dir; 870d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 871d88c93f0SGreg Kroah-Hartman return new_dir; 872d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 873d88c93f0SGreg Kroah-Hartman return old_dentry; 874d88c93f0SGreg Kroah-Hartman 875cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 876cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 8772b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 878cfc94cdfSJan Kara goto exit; 879cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 8802b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 881cfc94cdfSJan Kara d_mountpoint(old_dentry)) 882cfc94cdfSJan Kara goto exit; 883cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 884cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 8852b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 886cfc94cdfSJan Kara goto exit; 887cfc94cdfSJan Kara 88849d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 889cfc94cdfSJan Kara 890e18275aeSChristian Brauner error = simple_rename(&nop_mnt_idmap, d_inode(old_dir), old_dentry, 891549c7297SChristian Brauner d_inode(new_dir), dentry, 0); 892cfc94cdfSJan Kara if (error) { 89349d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 894cfc94cdfSJan Kara goto exit; 895cfc94cdfSJan Kara } 896cfc94cdfSJan Kara d_move(old_dentry, dentry); 897f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 898e36cb0b8SDavid Howells d_is_dir(old_dentry), 8995a190ae6SAl Viro NULL, old_dentry); 90049d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 901cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 902cfc94cdfSJan Kara dput(dentry); 903cfc94cdfSJan Kara return old_dentry; 904cfc94cdfSJan Kara exit: 905cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 906cfc94cdfSJan Kara dput(dentry); 907cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 908ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 909ff9fb72bSGreg Kroah-Hartman return dentry; 910ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 911cfc94cdfSJan Kara } 912cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 913cfc94cdfSJan Kara 914c0f92ba9SFrederic Weisbecker /** 915c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 916c0f92ba9SFrederic Weisbecker */ 917c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 918c0f92ba9SFrederic Weisbecker { 919c0f92ba9SFrederic Weisbecker return debugfs_registered; 920c0f92ba9SFrederic Weisbecker } 921c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 922c0f92ba9SFrederic Weisbecker 923a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str) 924a24c6f7bSPeter Enderborg { 925a24c6f7bSPeter Enderborg if (str) { 926a24c6f7bSPeter Enderborg if (!strcmp(str, "on")) 927a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 928a24c6f7bSPeter Enderborg else if (!strcmp(str, "no-mount")) 929a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API; 930a24c6f7bSPeter Enderborg else if (!strcmp(str, "off")) 931a24c6f7bSPeter Enderborg debugfs_allow = 0; 932a24c6f7bSPeter Enderborg } 933a24c6f7bSPeter Enderborg 934a24c6f7bSPeter Enderborg return 0; 935a24c6f7bSPeter Enderborg } 936a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel); 9371da177e4SLinus Torvalds static int __init debugfs_init(void) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds int retval; 9401da177e4SLinus Torvalds 941a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 942a24c6f7bSPeter Enderborg return -EPERM; 943a24c6f7bSPeter Enderborg 944f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 945f9bb4882SEric W. Biederman if (retval) 946f9bb4882SEric W. Biederman return retval; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 9491da177e4SLinus Torvalds if (retval) 950f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 951c0f92ba9SFrederic Weisbecker else 952c0f92ba9SFrederic Weisbecker debugfs_registered = true; 953c0f92ba9SFrederic Weisbecker 9541da177e4SLinus Torvalds return retval; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds core_initcall(debugfs_init); 957