13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 3bd33d12fSHarry Wei * inode.c - part of debugfs, a tiny little debug file system 41da177e4SLinus Torvalds * 543e23b6cSGreg Kroah-Hartman * Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]> 61da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 743e23b6cSGreg Kroah-Hartman * Copyright (C) 2019 Linux Foundation <[email protected]> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 10e1511a84SMauro Carvalho Chehab * See ./Documentation/core-api/kernel-api.rst for more details. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13d03ae477SGreg Kroah-Hartman #define pr_fmt(fmt) "debugfs: " fmt 14d03ae477SGreg Kroah-Hartman 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/fs.h> 171da177e4SLinus Torvalds #include <linux/mount.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include <linux/init.h> 204d8ebddcSRandy Dunlap #include <linux/kobject.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/debugfs.h> 234f36557fSMathieu Desnoyers #include <linux/fsnotify.h> 2466f54963SPeter Oberparleiter #include <linux/string.h> 25d6e48686SLudwig Nussel #include <linux/seq_file.h> 26d6e48686SLudwig Nussel #include <linux/parser.h> 2792562927SMimi Zohar #include <linux/magic.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 295496197fSDavid Howells #include <linux/security.h> 309fd4dcecSNicolai Stange 319fd4dcecSNicolai Stange #include "internal.h" 321da177e4SLinus Torvalds 3382aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE 0700 34d6e48686SLudwig Nussel 351da177e4SLinus Torvalds static struct vfsmount *debugfs_mount; 361da177e4SLinus Torvalds static int debugfs_mount_count; 37c0f92ba9SFrederic Weisbecker static bool debugfs_registered; 38312723a0SKees Cook static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS; 391da177e4SLinus Torvalds 405496197fSDavid Howells /* 415496197fSDavid Howells * Don't allow access attributes to be changed whilst the kernel is locked down 425496197fSDavid Howells * so that we can use the file mode as part of a heuristic to determine whether 435496197fSDavid Howells * to lock down individual files. 445496197fSDavid Howells */ 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(); 750f7bda3fSJeff Layton inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds return inode; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 80d6e48686SLudwig Nussel struct debugfs_mount_opts { 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 Opt_err 93d6e48686SLudwig Nussel }; 94d6e48686SLudwig Nussel 95d6e48686SLudwig Nussel static const match_table_t tokens = { 96d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 97d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 98d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 99d6e48686SLudwig Nussel {Opt_err, NULL} 100d6e48686SLudwig Nussel }; 101d6e48686SLudwig Nussel 102d6e48686SLudwig Nussel struct debugfs_fs_info { 103d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 104d6e48686SLudwig Nussel }; 105d6e48686SLudwig Nussel 106d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 107d6e48686SLudwig Nussel { 108d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 109d6e48686SLudwig Nussel int option; 110d6e48686SLudwig Nussel int token; 1117dc05881SEric W. Biederman kuid_t uid; 1127dc05881SEric W. Biederman kgid_t gid; 113d6e48686SLudwig Nussel char *p; 114d6e48686SLudwig Nussel 115b8de524cSBrian Norris opts->opts = 0; 116d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 117d6e48686SLudwig Nussel 118d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 119d6e48686SLudwig Nussel if (!*p) 120d6e48686SLudwig Nussel continue; 121d6e48686SLudwig Nussel 122d6e48686SLudwig Nussel token = match_token(p, tokens, args); 123d6e48686SLudwig Nussel switch (token) { 124d6e48686SLudwig Nussel case Opt_uid: 125d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 126d6e48686SLudwig Nussel return -EINVAL; 1277dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1287dc05881SEric W. Biederman if (!uid_valid(uid)) 1297dc05881SEric W. Biederman return -EINVAL; 1307dc05881SEric W. Biederman opts->uid = uid; 131d6e48686SLudwig Nussel break; 132d6e48686SLudwig Nussel case Opt_gid: 133f1688e04SDave Reisner if (match_int(&args[0], &option)) 134d6e48686SLudwig Nussel return -EINVAL; 1357dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1367dc05881SEric W. Biederman if (!gid_valid(gid)) 1377dc05881SEric W. Biederman return -EINVAL; 1387dc05881SEric W. Biederman opts->gid = gid; 139d6e48686SLudwig Nussel break; 140d6e48686SLudwig Nussel case Opt_mode: 141d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 142d6e48686SLudwig Nussel return -EINVAL; 143d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 144d6e48686SLudwig Nussel break; 145d6e48686SLudwig Nussel /* 146d6e48686SLudwig Nussel * We might like to report bad mount options here; 147d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 148d6e48686SLudwig Nussel */ 149d6e48686SLudwig Nussel } 150b8de524cSBrian Norris 151b8de524cSBrian Norris opts->opts |= BIT(token); 152d6e48686SLudwig Nussel } 153d6e48686SLudwig Nussel 154d6e48686SLudwig Nussel return 0; 155d6e48686SLudwig Nussel } 156d6e48686SLudwig Nussel 157b8de524cSBrian Norris static void _debugfs_apply_options(struct super_block *sb, bool remount) 158d6e48686SLudwig Nussel { 159d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1602b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 161d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 162d6e48686SLudwig Nussel 163b8de524cSBrian Norris /* 164b8de524cSBrian Norris * On remount, only reset mode/uid/gid if they were provided as mount 165b8de524cSBrian Norris * options. 166b8de524cSBrian Norris */ 167b8de524cSBrian Norris 168b8de524cSBrian Norris if (!remount || opts->opts & BIT(Opt_mode)) { 169d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 170d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 171b8de524cSBrian Norris } 172d6e48686SLudwig Nussel 173b8de524cSBrian Norris if (!remount || opts->opts & BIT(Opt_uid)) 174d6e48686SLudwig Nussel inode->i_uid = opts->uid; 175d6e48686SLudwig Nussel 176b8de524cSBrian Norris if (!remount || opts->opts & BIT(Opt_gid)) 177b8de524cSBrian Norris inode->i_gid = opts->gid; 178b8de524cSBrian Norris } 179b8de524cSBrian Norris 180b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb) 181b8de524cSBrian Norris { 182b8de524cSBrian Norris _debugfs_apply_options(sb, false); 183b8de524cSBrian Norris } 184b8de524cSBrian Norris 185b8de524cSBrian Norris static void debugfs_apply_options_remount(struct super_block *sb) 186b8de524cSBrian Norris { 187b8de524cSBrian Norris _debugfs_apply_options(sb, true); 188d6e48686SLudwig Nussel } 189d6e48686SLudwig Nussel 190d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 191d6e48686SLudwig Nussel { 192d6e48686SLudwig Nussel int err; 193d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 194d6e48686SLudwig Nussel 19502b9984dSTheodore Ts'o sync_filesystem(sb); 196d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 197d6e48686SLudwig Nussel if (err) 198d6e48686SLudwig Nussel goto fail; 199d6e48686SLudwig Nussel 200b8de524cSBrian Norris debugfs_apply_options_remount(sb); 201d6e48686SLudwig Nussel 202d6e48686SLudwig Nussel fail: 203d6e48686SLudwig Nussel return err; 204d6e48686SLudwig Nussel } 205d6e48686SLudwig Nussel 206d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 207d6e48686SLudwig Nussel { 208d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 209d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 210d6e48686SLudwig Nussel 2117dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 2127dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 2137dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 2147dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 2157dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 2167dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 217d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 218d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 219d6e48686SLudwig Nussel 220d6e48686SLudwig Nussel return 0; 221d6e48686SLudwig Nussel } 222d6e48686SLudwig Nussel 2236234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 2240db59e59SAl Viro { 2250db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 2265723cb01SAl Viro kfree(inode->i_link); 22793b919daSAl Viro free_inode_nonrcu(inode); 22893b919daSAl Viro } 22993b919daSAl Viro 230d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 231d6e48686SLudwig Nussel .statfs = simple_statfs, 232d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 233d6e48686SLudwig Nussel .show_options = debugfs_show_options, 2346234ddf4SAl Viro .free_inode = debugfs_free_inode, 235d6e48686SLudwig Nussel }; 236d6e48686SLudwig Nussel 2377c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2387c8d4698SNicolai Stange { 2390ed04a18SJohannes Berg struct debugfs_fsdata *fsd = dentry->d_fsdata; 2407d39bc50SNicolai Stange 2410ed04a18SJohannes Berg if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 2420ed04a18SJohannes Berg return; 2430ed04a18SJohannes Berg 244*f4acfcd4SJohannes Berg /* check it wasn't a dir (no fsdata) or automount (no real_fops) */ 245*f4acfcd4SJohannes Berg if (fsd && fsd->real_fops) { 246*f4acfcd4SJohannes Berg #ifdef CONFIG_LOCKDEP 247*f4acfcd4SJohannes Berg lockdep_unregister_key(&fsd->key); 248*f4acfcd4SJohannes Berg kfree(fsd->lock_name); 249*f4acfcd4SJohannes Berg #endif 250*f4acfcd4SJohannes Berg } 251*f4acfcd4SJohannes Berg 2520ed04a18SJohannes Berg kfree(fsd); 2537c8d4698SNicolai Stange } 2547c8d4698SNicolai Stange 25577b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 25677b3da6eSAl Viro { 2570ed04a18SJohannes Berg struct debugfs_fsdata *fsd = path->dentry->d_fsdata; 2580ed04a18SJohannes Berg 2590ed04a18SJohannes Berg return fsd->automount(path->dentry, d_inode(path->dentry)->i_private); 26077b3da6eSAl Viro } 26177b3da6eSAl Viro 26277b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 26377b3da6eSAl Viro .d_delete = always_delete_dentry, 2647c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 26577b3da6eSAl Viro .d_automount = debugfs_automount, 26677b3da6eSAl Viro }; 26777b3da6eSAl Viro 2681da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2691da177e4SLinus Torvalds { 270cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 271d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 272d6e48686SLudwig Nussel int err; 2731da177e4SLinus Torvalds 274d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 275d6e48686SLudwig Nussel sb->s_fs_info = fsi; 276d6e48686SLudwig Nussel if (!fsi) { 277d6e48686SLudwig Nussel err = -ENOMEM; 278d6e48686SLudwig Nussel goto fail; 279d6e48686SLudwig Nussel } 280d6e48686SLudwig Nussel 281d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 282d6e48686SLudwig Nussel if (err) 283d6e48686SLudwig Nussel goto fail; 284d6e48686SLudwig Nussel 285d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 286d6e48686SLudwig Nussel if (err) 287d6e48686SLudwig Nussel goto fail; 288d6e48686SLudwig Nussel 289d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 29077b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 291d6e48686SLudwig Nussel 292d6e48686SLudwig Nussel debugfs_apply_options(sb); 293d6e48686SLudwig Nussel 294d6e48686SLudwig Nussel return 0; 295d6e48686SLudwig Nussel 296d6e48686SLudwig Nussel fail: 297d6e48686SLudwig Nussel kfree(fsi); 298d6e48686SLudwig Nussel sb->s_fs_info = NULL; 299d6e48686SLudwig Nussel return err; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 302fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 3031da177e4SLinus Torvalds int flags, const char *dev_name, 304fc14f2feSAl Viro void *data) 3051da177e4SLinus Torvalds { 306a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 307a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 308a24c6f7bSPeter Enderborg 309fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 3131da177e4SLinus Torvalds .owner = THIS_MODULE, 3141da177e4SLinus Torvalds .name = "debugfs", 315fc14f2feSAl Viro .mount = debug_mount, 3161da177e4SLinus Torvalds .kill_sb = kill_litter_super, 3171da177e4SLinus Torvalds }; 3187f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 3191da177e4SLinus Torvalds 320a7c5437bSOmar Sandoval /** 321a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 322a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 323a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 324a7c5437bSOmar Sandoval * 325a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 326a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 327a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 328a7c5437bSOmar Sandoval * 329a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 330a7c5437bSOmar Sandoval * returned. 331a7c5437bSOmar Sandoval */ 332a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 333a7c5437bSOmar Sandoval { 334a7c5437bSOmar Sandoval struct dentry *dentry; 335a7c5437bSOmar Sandoval 336bc6de804SGreg Kroah-Hartman if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 337a7c5437bSOmar Sandoval return NULL; 338a7c5437bSOmar Sandoval 339a7c5437bSOmar Sandoval if (!parent) 340a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 341a7c5437bSOmar Sandoval 3426c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 343a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 344a7c5437bSOmar Sandoval return NULL; 345a7c5437bSOmar Sandoval return dentry; 346a7c5437bSOmar Sandoval } 347a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 348a7c5437bSOmar Sandoval 349190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 350c3b1a350SAl Viro { 351190afd81SAl Viro struct dentry *dentry; 352c3b1a350SAl Viro int error; 353c3b1a350SAl Viro 354a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 355a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 356a24c6f7bSPeter Enderborg 35756348560SGreg Kroah-Hartman if (!debugfs_initialized()) 35856348560SGreg Kroah-Hartman return ERR_PTR(-ENOENT); 35956348560SGreg Kroah-Hartman 360d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 361c3b1a350SAl Viro 362c9e15f25SGreg KH if (IS_ERR(parent)) 363c9e15f25SGreg KH return parent; 364c9e15f25SGreg KH 365c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 366c3b1a350SAl Viro &debugfs_mount_count); 36743e23b6cSGreg Kroah-Hartman if (error) { 36843e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 369190afd81SAl Viro return ERR_PTR(error); 37043e23b6cSGreg Kroah-Hartman } 371c3b1a350SAl Viro 372cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 373cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 374cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 375cfa57c11SAl Viro * have around. 376cfa57c11SAl Viro */ 377cfa57c11SAl Viro if (!parent) 378cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 379cfa57c11SAl Viro 3805955102cSAl Viro inode_lock(d_inode(parent)); 381a3d1e7ebSAl Viro if (unlikely(IS_DEADDIR(d_inode(parent)))) 382a3d1e7ebSAl Viro dentry = ERR_PTR(-ENOENT); 383a3d1e7ebSAl Viro else 384cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3852b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 386c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 387c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 388c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 389c33d4423SGreg Kroah-Hartman else 390c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 391c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 392190afd81SAl Viro dput(dentry); 393190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 394190afd81SAl Viro } 3950ee9608cSDaniel Borkmann 3960ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3975955102cSAl Viro inode_unlock(d_inode(parent)); 3980ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3990ee9608cSDaniel Borkmann } 4000ee9608cSDaniel Borkmann 401190afd81SAl Viro return dentry; 402190afd81SAl Viro } 403190afd81SAl Viro 4045233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 405190afd81SAl Viro { 4065955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 407190afd81SAl Viro dput(dentry); 408190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 409ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 410190afd81SAl Viro } 4115233e311SAl Viro 4125233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 4135233e311SAl Viro { 4145955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 415190afd81SAl Viro return dentry; 416190afd81SAl Viro } 417190afd81SAl Viro 41849d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 41949d200deSNicolai Stange struct dentry *parent, void *data, 42049d200deSNicolai Stange const struct file_operations *proxy_fops, 42149d200deSNicolai Stange const struct file_operations *real_fops) 42249d200deSNicolai Stange { 42349d200deSNicolai Stange struct dentry *dentry; 42449d200deSNicolai Stange struct inode *inode; 42549d200deSNicolai Stange 42649d200deSNicolai Stange if (!(mode & S_IFMT)) 42749d200deSNicolai Stange mode |= S_IFREG; 42849d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 42949d200deSNicolai Stange dentry = start_creating(name, parent); 43049d200deSNicolai Stange 4317d39bc50SNicolai Stange if (IS_ERR(dentry)) 432ff9fb72bSGreg Kroah-Hartman return dentry; 43349d200deSNicolai Stange 434a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 435a24c6f7bSPeter Enderborg failed_creating(dentry); 436a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 437a24c6f7bSPeter Enderborg } 438a24c6f7bSPeter Enderborg 43949d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 44043e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 44143e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 44243e23b6cSGreg Kroah-Hartman name); 44349d200deSNicolai Stange return failed_creating(dentry); 44443e23b6cSGreg Kroah-Hartman } 44549d200deSNicolai Stange 44649d200deSNicolai Stange inode->i_mode = mode; 44749d200deSNicolai Stange inode->i_private = data; 44849d200deSNicolai Stange 4495496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 45049d200deSNicolai Stange inode->i_fop = proxy_fops; 4517d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 4527d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 45349d200deSNicolai Stange 45449d200deSNicolai Stange d_instantiate(dentry, inode); 45549d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 45649d200deSNicolai Stange return end_creating(dentry); 45749d200deSNicolai Stange } 45849d200deSNicolai Stange 4591da177e4SLinus Torvalds /** 4601da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 4611da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 462be030e65SAlberto Bertogli * @mode: the permission that the file should have. 4631da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 464e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4651da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4661da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 4678e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 4681da177e4SLinus Torvalds * the open() call. 4691da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 4701da177e4SLinus Torvalds * this file. 4711da177e4SLinus Torvalds * 4721da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 473be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 474be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4751da177e4SLinus Torvalds * recommended to be used instead.) 4761da177e4SLinus Torvalds * 4771da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4781da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4791da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 480adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 481ff9fb72bSGreg Kroah-Hartman * returned. 4821da177e4SLinus Torvalds * 4836468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 484873760fbSCornelia Huck * returned. 4858bcbde2bSDouglas Anderson * 4868bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 4878bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 4888bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 4898bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 4901da177e4SLinus Torvalds */ 491f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4921da177e4SLinus Torvalds struct dentry *parent, void *data, 49399ac48f5SArjan van de Ven const struct file_operations *fops) 4941da177e4SLinus Torvalds { 495c3b1a350SAl Viro 49649d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 49749d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 49849d200deSNicolai Stange &debugfs_noop_file_operations, 49949d200deSNicolai Stange fops); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 5021da177e4SLinus Torvalds 503c6468808SNicolai Stange /** 504c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 505c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 506c6468808SNicolai Stange * @mode: the permission that the file should have. 507c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 508c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 509c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 510c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 511c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 512c6468808SNicolai Stange * the open() call. 513c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 514c6468808SNicolai Stange * this file. 515c6468808SNicolai Stange * 516c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 517c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 518c6468808SNicolai Stange * handed it will not get protected against file removals by the 519c6468808SNicolai Stange * debugfs core. 520c6468808SNicolai Stange * 521c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 5220eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 5230eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 524c6468808SNicolai Stange * debugfs though. 525c6468808SNicolai Stange * 526c6468808SNicolai Stange * Any struct file_operations defined by means of 527c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 528c6468808SNicolai Stange * thus, may be used here. 529c6468808SNicolai Stange */ 53049d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 53149d200deSNicolai Stange struct dentry *parent, void *data, 53249d200deSNicolai Stange const struct file_operations *fops) 53349d200deSNicolai Stange { 53449d200deSNicolai Stange 53549d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 53649d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 53749d200deSNicolai Stange &debugfs_noop_file_operations, 53849d200deSNicolai Stange fops); 53949d200deSNicolai Stange } 540c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 54149d200deSNicolai Stange 5421da177e4SLinus Torvalds /** 543e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 544e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 545e59b4e91SDavid Howells * @mode: the permission that the file should have. 546e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 547e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 548e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 549e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 550e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 551e59b4e91SDavid Howells * the open() call. 552e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 553e59b4e91SDavid Howells * this file. 554e59b4e91SDavid Howells * @file_size: initial file size 555e59b4e91SDavid Howells * 556e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 557e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 558e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 559e59b4e91SDavid Howells * recommended to be used instead.) 560e59b4e91SDavid Howells */ 561526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode, 562e59b4e91SDavid Howells struct dentry *parent, void *data, 563e59b4e91SDavid Howells const struct file_operations *fops, 564e59b4e91SDavid Howells loff_t file_size) 565e59b4e91SDavid Howells { 566e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 567e59b4e91SDavid Howells 568af505cadSNirmoy Das if (!IS_ERR(de)) 5692b0143b5SDavid Howells d_inode(de)->i_size = file_size; 570e59b4e91SDavid Howells } 571e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 572e59b4e91SDavid Howells 573e59b4e91SDavid Howells /** 5741da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5751da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5761da177e4SLinus Torvalds * create. 5771da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 578e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5791da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5801da177e4SLinus Torvalds * 5811da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5821da177e4SLinus Torvalds * 5831da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5841da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5851da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 586adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 587ff9fb72bSGreg Kroah-Hartman * returned. 5881da177e4SLinus Torvalds * 5896468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 590873760fbSCornelia Huck * returned. 5918bcbde2bSDouglas Anderson * 5928bcbde2bSDouglas Anderson * NOTE: it's expected that most callers should _ignore_ the errors returned 5938bcbde2bSDouglas Anderson * by this function. Other debugfs functions handle the fact that the "dentry" 5948bcbde2bSDouglas Anderson * passed to them could be an error and they don't crash in that case. 5958bcbde2bSDouglas Anderson * Drivers should generally work fine even if debugfs fails to init anyway. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5981da177e4SLinus Torvalds { 599ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 600680b3024SAl Viro struct inode *inode; 601ad5abd5bSAl Viro 602ad5abd5bSAl Viro if (IS_ERR(dentry)) 603ff9fb72bSGreg Kroah-Hartman return dentry; 604ad5abd5bSAl Viro 605a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 606a24c6f7bSPeter Enderborg failed_creating(dentry); 607a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 608a24c6f7bSPeter Enderborg } 609a24c6f7bSPeter Enderborg 610edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 61143e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 61243e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 61343e23b6cSGreg Kroah-Hartman name); 6145233e311SAl Viro return failed_creating(dentry); 61543e23b6cSGreg Kroah-Hartman } 616680b3024SAl Viro 617f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 6185496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 619edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 620edac65eaSAl Viro 621edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 622edac65eaSAl Viro inc_nlink(inode); 623680b3024SAl Viro d_instantiate(dentry, inode); 6242b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 6252b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 6265233e311SAl Viro return end_creating(dentry); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /** 63177b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 63277b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 63377b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 63477b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 63577b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 63677b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 63777b3da6eSAl Viro * @data: opaque argument to pass to f(). 63877b3da6eSAl Viro * 63977b3da6eSAl Viro * @f should return what ->d_automount() would. 64077b3da6eSAl Viro */ 64177b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 64277b3da6eSAl Viro struct dentry *parent, 64393faccbbSEric W. Biederman debugfs_automount_t f, 64477b3da6eSAl Viro void *data) 64577b3da6eSAl Viro { 64677b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 6470ed04a18SJohannes Berg struct debugfs_fsdata *fsd; 64877b3da6eSAl Viro struct inode *inode; 64977b3da6eSAl Viro 65077b3da6eSAl Viro if (IS_ERR(dentry)) 651ff9fb72bSGreg Kroah-Hartman return dentry; 65277b3da6eSAl Viro 6530ed04a18SJohannes Berg fsd = kzalloc(sizeof(*fsd), GFP_KERNEL); 6540ed04a18SJohannes Berg if (!fsd) { 6550ed04a18SJohannes Berg failed_creating(dentry); 6560ed04a18SJohannes Berg return ERR_PTR(-ENOMEM); 6570ed04a18SJohannes Berg } 6580ed04a18SJohannes Berg 6590ed04a18SJohannes Berg fsd->automount = f; 6600ed04a18SJohannes Berg 661a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 662a24c6f7bSPeter Enderborg failed_creating(dentry); 6630ed04a18SJohannes Berg kfree(fsd); 664a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 665a24c6f7bSPeter Enderborg } 666a24c6f7bSPeter Enderborg 66777b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 66843e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 66943e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 67043e23b6cSGreg Kroah-Hartman name); 6710ed04a18SJohannes Berg kfree(fsd); 67277b3da6eSAl Viro return failed_creating(dentry); 67343e23b6cSGreg Kroah-Hartman } 67477b3da6eSAl Viro 67587243debSSeth Forshee make_empty_dir_inode(inode); 67677b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 67777b3da6eSAl Viro inode->i_private = data; 6780ed04a18SJohannes Berg dentry->d_fsdata = fsd; 679a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 680a8f324a4SRoman Pen inc_nlink(inode); 68177b3da6eSAl Viro d_instantiate(dentry, inode); 682a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 683a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 68477b3da6eSAl Viro return end_creating(dentry); 68577b3da6eSAl Viro } 68677b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 68777b3da6eSAl Viro 68877b3da6eSAl Viro /** 68966f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 69066f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 69166f54963SPeter Oberparleiter * create. 69266f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 693e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 69466f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 69566f54963SPeter Oberparleiter * filesystem. 69666f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 69766f54963SPeter Oberparleiter * symbolic link. 69866f54963SPeter Oberparleiter * 69966f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 70066f54963SPeter Oberparleiter * links to the given target path. 70166f54963SPeter Oberparleiter * 70266f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 70366f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 70466f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 705adc92dd4SDaniel W. S. Almeida * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 706ff9fb72bSGreg Kroah-Hartman * will be returned. 70766f54963SPeter Oberparleiter * 70866f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 709873760fbSCornelia Huck * returned. 71066f54963SPeter Oberparleiter */ 71166f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 71266f54963SPeter Oberparleiter const char *target) 71366f54963SPeter Oberparleiter { 714ad5abd5bSAl Viro struct dentry *dentry; 715680b3024SAl Viro struct inode *inode; 716680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 71766f54963SPeter Oberparleiter if (!link) 718ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 71966f54963SPeter Oberparleiter 720ad5abd5bSAl Viro dentry = start_creating(name, parent); 721ad5abd5bSAl Viro if (IS_ERR(dentry)) { 72266f54963SPeter Oberparleiter kfree(link); 723ff9fb72bSGreg Kroah-Hartman return dentry; 724ad5abd5bSAl Viro } 725ad5abd5bSAl Viro 726edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 727680b3024SAl Viro if (unlikely(!inode)) { 72843e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 72943e23b6cSGreg Kroah-Hartman name); 730ad5abd5bSAl Viro kfree(link); 7315233e311SAl Viro return failed_creating(dentry); 732680b3024SAl Viro } 733edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 7345496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 7355723cb01SAl Viro inode->i_link = link; 736680b3024SAl Viro d_instantiate(dentry, inode); 7375233e311SAl Viro return end_creating(dentry); 73866f54963SPeter Oberparleiter } 73966f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 74066f54963SPeter Oberparleiter 741823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 742e9117a5aSNicolai Stange { 743e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 744e9117a5aSNicolai Stange 7457d39bc50SNicolai Stange /* 7467d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 7477d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 7487d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 7497d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 7507d39bc50SNicolai Stange */ 7517d39bc50SNicolai Stange smp_mb(); 7527d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 7537d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 7547d39bc50SNicolai Stange return; 755*f4acfcd4SJohannes Berg 756*f4acfcd4SJohannes Berg lock_map_acquire(&fsd->lockdep_map); 757*f4acfcd4SJohannes Berg lock_map_release(&fsd->lockdep_map); 758*f4acfcd4SJohannes Berg 759e9117a5aSNicolai Stange if (!refcount_dec_and_test(&fsd->active_users)) 760e9117a5aSNicolai Stange wait_for_completion(&fsd->active_users_drained); 761e9117a5aSNicolai Stange } 762e9117a5aSNicolai Stange 763a3d1e7ebSAl Viro static void remove_one(struct dentry *victim) 7641da177e4SLinus Torvalds { 765a3d1e7ebSAl Viro if (d_is_reg(victim)) 766a3d1e7ebSAl Viro __debugfs_file_removed(victim); 7671da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 770cfc94cdfSJan Kara /** 771a3d1e7ebSAl Viro * debugfs_remove - recursively removes a directory 772398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 773398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 7749505e637SHaavard Skinnemoen * 7759505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 7769505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 7779505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 7789505e637SHaavard Skinnemoen * 7799505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7809505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7819505e637SHaavard Skinnemoen * removed, you are responsible here. 7829505e637SHaavard Skinnemoen */ 783a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry) 7849505e637SHaavard Skinnemoen { 785a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7869505e637SHaavard Skinnemoen return; 7879505e637SHaavard Skinnemoen 788a3d1e7ebSAl Viro simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 789a3d1e7ebSAl Viro simple_recursive_removal(dentry, remove_one); 790776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 791776164c1SOleg Nesterov } 792a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove); 7939505e637SHaavard Skinnemoen 7949505e637SHaavard Skinnemoen /** 795dec9b2f1SGreg Kroah-Hartman * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it 796dec9b2f1SGreg Kroah-Hartman * @name: a pointer to a string containing the name of the item to look up. 797dec9b2f1SGreg Kroah-Hartman * @parent: a pointer to the parent dentry of the item. 798dec9b2f1SGreg Kroah-Hartman * 799dec9b2f1SGreg Kroah-Hartman * This is the equlivant of doing something like 800dec9b2f1SGreg Kroah-Hartman * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting 801dec9b2f1SGreg Kroah-Hartman * handled for the directory being looked up. 802dec9b2f1SGreg Kroah-Hartman */ 803dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent) 804dec9b2f1SGreg Kroah-Hartman { 805dec9b2f1SGreg Kroah-Hartman struct dentry *dentry; 806dec9b2f1SGreg Kroah-Hartman 807dec9b2f1SGreg Kroah-Hartman dentry = debugfs_lookup(name, parent); 808dec9b2f1SGreg Kroah-Hartman if (!dentry) 809dec9b2f1SGreg Kroah-Hartman return; 810dec9b2f1SGreg Kroah-Hartman 811dec9b2f1SGreg Kroah-Hartman debugfs_remove(dentry); 812dec9b2f1SGreg Kroah-Hartman dput(dentry); 813dec9b2f1SGreg Kroah-Hartman } 814dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove); 815dec9b2f1SGreg Kroah-Hartman 816dec9b2f1SGreg Kroah-Hartman /** 817cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 818cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 819cfc94cdfSJan Kara * should be a directory dentry. 820cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 821cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 822cfc94cdfSJan Kara * moved. This should be a directory dentry. 823cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 824cfc94cdfSJan Kara * 825cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 826cfc94cdfSJan Kara * exist for rename to succeed. 827cfc94cdfSJan Kara * 828cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 82988cd618dSBagas Sanjaya * reflect renaming) if it succeeds. If an error occurs, ERR_PTR(-ERROR) 830d3002468SQi Zheng * will be returned. 831cfc94cdfSJan Kara * 832cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 833cfc94cdfSJan Kara * returned. 834cfc94cdfSJan Kara */ 835cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 836cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 837cfc94cdfSJan Kara { 838cfc94cdfSJan Kara int error; 839cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 84049d31c2fSAl Viro struct name_snapshot old_name; 841cfc94cdfSJan Kara 842d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 843d88c93f0SGreg Kroah-Hartman return old_dir; 844d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 845d88c93f0SGreg Kroah-Hartman return new_dir; 846d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 847d88c93f0SGreg Kroah-Hartman return old_dentry; 848d88c93f0SGreg Kroah-Hartman 849cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 850cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 8512b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 852cfc94cdfSJan Kara goto exit; 853cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 8542b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 855cfc94cdfSJan Kara d_mountpoint(old_dentry)) 856cfc94cdfSJan Kara goto exit; 857cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 858cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 8592b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 860cfc94cdfSJan Kara goto exit; 861cfc94cdfSJan Kara 86249d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 863cfc94cdfSJan Kara 864e18275aeSChristian Brauner error = simple_rename(&nop_mnt_idmap, d_inode(old_dir), old_dentry, 865549c7297SChristian Brauner d_inode(new_dir), dentry, 0); 866cfc94cdfSJan Kara if (error) { 86749d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 868cfc94cdfSJan Kara goto exit; 869cfc94cdfSJan Kara } 870cfc94cdfSJan Kara d_move(old_dentry, dentry); 871f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 872e36cb0b8SDavid Howells d_is_dir(old_dentry), 8735a190ae6SAl Viro NULL, old_dentry); 87449d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 875cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 876cfc94cdfSJan Kara dput(dentry); 877cfc94cdfSJan Kara return old_dentry; 878cfc94cdfSJan Kara exit: 879cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 880cfc94cdfSJan Kara dput(dentry); 881cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 882ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 883ff9fb72bSGreg Kroah-Hartman return dentry; 884ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 885cfc94cdfSJan Kara } 886cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 887cfc94cdfSJan Kara 888c0f92ba9SFrederic Weisbecker /** 889c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 890c0f92ba9SFrederic Weisbecker */ 891c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 892c0f92ba9SFrederic Weisbecker { 893c0f92ba9SFrederic Weisbecker return debugfs_registered; 894c0f92ba9SFrederic Weisbecker } 895c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 896c0f92ba9SFrederic Weisbecker 897a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str) 898a24c6f7bSPeter Enderborg { 899a24c6f7bSPeter Enderborg if (str) { 900a24c6f7bSPeter Enderborg if (!strcmp(str, "on")) 901a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 902a24c6f7bSPeter Enderborg else if (!strcmp(str, "no-mount")) 903a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API; 904a24c6f7bSPeter Enderborg else if (!strcmp(str, "off")) 905a24c6f7bSPeter Enderborg debugfs_allow = 0; 906a24c6f7bSPeter Enderborg } 907a24c6f7bSPeter Enderborg 908a24c6f7bSPeter Enderborg return 0; 909a24c6f7bSPeter Enderborg } 910a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel); 9111da177e4SLinus Torvalds static int __init debugfs_init(void) 9121da177e4SLinus Torvalds { 9131da177e4SLinus Torvalds int retval; 9141da177e4SLinus Torvalds 915a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 916a24c6f7bSPeter Enderborg return -EPERM; 917a24c6f7bSPeter Enderborg 918f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 919f9bb4882SEric W. Biederman if (retval) 920f9bb4882SEric W. Biederman return retval; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 9231da177e4SLinus Torvalds if (retval) 924f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 925c0f92ba9SFrederic Weisbecker else 926c0f92ba9SFrederic Weisbecker debugfs_registered = true; 927c0f92ba9SFrederic Weisbecker 9281da177e4SLinus Torvalds return retval; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds core_initcall(debugfs_init); 931