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; 381da177e4SLinus Torvalds 395496197fSDavid Howells /* 405496197fSDavid Howells * Don't allow access attributes to be changed whilst the kernel is locked down 415496197fSDavid Howells * so that we can use the file mode as part of a heuristic to determine whether 425496197fSDavid Howells * to lock down individual files. 435496197fSDavid Howells */ 445496197fSDavid Howells static int debugfs_setattr(struct dentry *dentry, struct iattr *ia) 455496197fSDavid Howells { 465496197fSDavid Howells int ret = security_locked_down(LOCKDOWN_DEBUGFS); 475496197fSDavid Howells 485496197fSDavid Howells if (ret && (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))) 495496197fSDavid Howells return ret; 505496197fSDavid Howells return simple_setattr(dentry, ia); 515496197fSDavid Howells } 525496197fSDavid Howells 535496197fSDavid Howells static const struct inode_operations debugfs_file_inode_operations = { 545496197fSDavid Howells .setattr = debugfs_setattr, 555496197fSDavid Howells }; 565496197fSDavid Howells static const struct inode_operations debugfs_dir_inode_operations = { 575496197fSDavid Howells .lookup = simple_lookup, 585496197fSDavid Howells .setattr = debugfs_setattr, 595496197fSDavid Howells }; 605496197fSDavid Howells static const struct inode_operations debugfs_symlink_inode_operations = { 615496197fSDavid Howells .get_link = simple_get_link, 625496197fSDavid Howells .setattr = debugfs_setattr, 635496197fSDavid Howells }; 645496197fSDavid Howells 65edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 681da177e4SLinus Torvalds if (inode) { 6985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 701b48b530SDeepa Dinamani inode->i_atime = inode->i_mtime = 71c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds return inode; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 76d6e48686SLudwig Nussel struct debugfs_mount_opts { 777dc05881SEric W. Biederman kuid_t uid; 787dc05881SEric W. Biederman kgid_t gid; 79d6e48686SLudwig Nussel umode_t mode; 80d6e48686SLudwig Nussel }; 81d6e48686SLudwig Nussel 82d6e48686SLudwig Nussel enum { 83d6e48686SLudwig Nussel Opt_uid, 84d6e48686SLudwig Nussel Opt_gid, 85d6e48686SLudwig Nussel Opt_mode, 86d6e48686SLudwig Nussel Opt_err 87d6e48686SLudwig Nussel }; 88d6e48686SLudwig Nussel 89d6e48686SLudwig Nussel static const match_table_t tokens = { 90d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 91d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 92d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 93d6e48686SLudwig Nussel {Opt_err, NULL} 94d6e48686SLudwig Nussel }; 95d6e48686SLudwig Nussel 96d6e48686SLudwig Nussel struct debugfs_fs_info { 97d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 98d6e48686SLudwig Nussel }; 99d6e48686SLudwig Nussel 100d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 101d6e48686SLudwig Nussel { 102d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 103d6e48686SLudwig Nussel int option; 104d6e48686SLudwig Nussel int token; 1057dc05881SEric W. Biederman kuid_t uid; 1067dc05881SEric W. Biederman kgid_t gid; 107d6e48686SLudwig Nussel char *p; 108d6e48686SLudwig Nussel 109d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 110d6e48686SLudwig Nussel 111d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 112d6e48686SLudwig Nussel if (!*p) 113d6e48686SLudwig Nussel continue; 114d6e48686SLudwig Nussel 115d6e48686SLudwig Nussel token = match_token(p, tokens, args); 116d6e48686SLudwig Nussel switch (token) { 117d6e48686SLudwig Nussel case Opt_uid: 118d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 119d6e48686SLudwig Nussel return -EINVAL; 1207dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1217dc05881SEric W. Biederman if (!uid_valid(uid)) 1227dc05881SEric W. Biederman return -EINVAL; 1237dc05881SEric W. Biederman opts->uid = uid; 124d6e48686SLudwig Nussel break; 125d6e48686SLudwig Nussel case Opt_gid: 126f1688e04SDave Reisner if (match_int(&args[0], &option)) 127d6e48686SLudwig Nussel return -EINVAL; 1287dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1297dc05881SEric W. Biederman if (!gid_valid(gid)) 1307dc05881SEric W. Biederman return -EINVAL; 1317dc05881SEric W. Biederman opts->gid = gid; 132d6e48686SLudwig Nussel break; 133d6e48686SLudwig Nussel case Opt_mode: 134d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 135d6e48686SLudwig Nussel return -EINVAL; 136d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 137d6e48686SLudwig Nussel break; 138d6e48686SLudwig Nussel /* 139d6e48686SLudwig Nussel * We might like to report bad mount options here; 140d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 141d6e48686SLudwig Nussel */ 142d6e48686SLudwig Nussel } 143d6e48686SLudwig Nussel } 144d6e48686SLudwig Nussel 145d6e48686SLudwig Nussel return 0; 146d6e48686SLudwig Nussel } 147d6e48686SLudwig Nussel 148d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 149d6e48686SLudwig Nussel { 150d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1512b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 152d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 153d6e48686SLudwig Nussel 154d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 155d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 156d6e48686SLudwig Nussel 157d6e48686SLudwig Nussel inode->i_uid = opts->uid; 158d6e48686SLudwig Nussel inode->i_gid = opts->gid; 159d6e48686SLudwig Nussel 160d6e48686SLudwig Nussel return 0; 161d6e48686SLudwig Nussel } 162d6e48686SLudwig Nussel 163d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 164d6e48686SLudwig Nussel { 165d6e48686SLudwig Nussel int err; 166d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 167d6e48686SLudwig Nussel 16802b9984dSTheodore Ts'o sync_filesystem(sb); 169d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 170d6e48686SLudwig Nussel if (err) 171d6e48686SLudwig Nussel goto fail; 172d6e48686SLudwig Nussel 173d6e48686SLudwig Nussel debugfs_apply_options(sb); 174d6e48686SLudwig Nussel 175d6e48686SLudwig Nussel fail: 176d6e48686SLudwig Nussel return err; 177d6e48686SLudwig Nussel } 178d6e48686SLudwig Nussel 179d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 180d6e48686SLudwig Nussel { 181d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 182d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 183d6e48686SLudwig Nussel 1847dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1857dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1867dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1877dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1887dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1897dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 190d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 191d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 192d6e48686SLudwig Nussel 193d6e48686SLudwig Nussel return 0; 194d6e48686SLudwig Nussel } 195d6e48686SLudwig Nussel 1966234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 1970db59e59SAl Viro { 1980db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 1995723cb01SAl Viro kfree(inode->i_link); 20093b919daSAl Viro free_inode_nonrcu(inode); 20193b919daSAl Viro } 20293b919daSAl Viro 203d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 204d6e48686SLudwig Nussel .statfs = simple_statfs, 205d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 206d6e48686SLudwig Nussel .show_options = debugfs_show_options, 2076234ddf4SAl Viro .free_inode = debugfs_free_inode, 208d6e48686SLudwig Nussel }; 209d6e48686SLudwig Nussel 2107c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2117c8d4698SNicolai Stange { 2127d39bc50SNicolai Stange void *fsd = dentry->d_fsdata; 2137d39bc50SNicolai Stange 2147d39bc50SNicolai Stange if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 2157c8d4698SNicolai Stange kfree(dentry->d_fsdata); 2167c8d4698SNicolai Stange } 2177c8d4698SNicolai Stange 21877b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 21977b3da6eSAl Viro { 22093faccbbSEric W. Biederman debugfs_automount_t f; 22193faccbbSEric W. Biederman f = (debugfs_automount_t)path->dentry->d_fsdata; 22293faccbbSEric W. Biederman return f(path->dentry, d_inode(path->dentry)->i_private); 22377b3da6eSAl Viro } 22477b3da6eSAl Viro 22577b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 22677b3da6eSAl Viro .d_delete = always_delete_dentry, 2277c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 22877b3da6eSAl Viro .d_automount = debugfs_automount, 22977b3da6eSAl Viro }; 23077b3da6eSAl Viro 2311da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2321da177e4SLinus Torvalds { 233cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 234d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 235d6e48686SLudwig Nussel int err; 2361da177e4SLinus Torvalds 237d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 238d6e48686SLudwig Nussel sb->s_fs_info = fsi; 239d6e48686SLudwig Nussel if (!fsi) { 240d6e48686SLudwig Nussel err = -ENOMEM; 241d6e48686SLudwig Nussel goto fail; 242d6e48686SLudwig Nussel } 243d6e48686SLudwig Nussel 244d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 245d6e48686SLudwig Nussel if (err) 246d6e48686SLudwig Nussel goto fail; 247d6e48686SLudwig Nussel 248d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 249d6e48686SLudwig Nussel if (err) 250d6e48686SLudwig Nussel goto fail; 251d6e48686SLudwig Nussel 252d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 25377b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 254d6e48686SLudwig Nussel 255d6e48686SLudwig Nussel debugfs_apply_options(sb); 256d6e48686SLudwig Nussel 257d6e48686SLudwig Nussel return 0; 258d6e48686SLudwig Nussel 259d6e48686SLudwig Nussel fail: 260d6e48686SLudwig Nussel kfree(fsi); 261d6e48686SLudwig Nussel sb->s_fs_info = NULL; 262d6e48686SLudwig Nussel return err; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2661da177e4SLinus Torvalds int flags, const char *dev_name, 267fc14f2feSAl Viro void *data) 2681da177e4SLinus Torvalds { 269fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2731da177e4SLinus Torvalds .owner = THIS_MODULE, 2741da177e4SLinus Torvalds .name = "debugfs", 275fc14f2feSAl Viro .mount = debug_mount, 2761da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2771da177e4SLinus Torvalds }; 2787f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2791da177e4SLinus Torvalds 280a7c5437bSOmar Sandoval /** 281a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 282a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 283a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 284a7c5437bSOmar Sandoval * 285a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 286a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 287a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 288a7c5437bSOmar Sandoval * 289a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 290a7c5437bSOmar Sandoval * returned. 291a7c5437bSOmar Sandoval */ 292a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 293a7c5437bSOmar Sandoval { 294a7c5437bSOmar Sandoval struct dentry *dentry; 295a7c5437bSOmar Sandoval 296a7c5437bSOmar Sandoval if (IS_ERR(parent)) 297a7c5437bSOmar Sandoval return NULL; 298a7c5437bSOmar Sandoval 299a7c5437bSOmar Sandoval if (!parent) 300a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 301a7c5437bSOmar Sandoval 302*6c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 303a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 304a7c5437bSOmar Sandoval return NULL; 305a7c5437bSOmar Sandoval return dentry; 306a7c5437bSOmar Sandoval } 307a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 308a7c5437bSOmar Sandoval 309190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 310c3b1a350SAl Viro { 311190afd81SAl Viro struct dentry *dentry; 312c3b1a350SAl Viro int error; 313c3b1a350SAl Viro 314d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 315c3b1a350SAl Viro 316c9e15f25SGreg KH if (IS_ERR(parent)) 317c9e15f25SGreg KH return parent; 318c9e15f25SGreg KH 319c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 320c3b1a350SAl Viro &debugfs_mount_count); 32143e23b6cSGreg Kroah-Hartman if (error) { 32243e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 323190afd81SAl Viro return ERR_PTR(error); 32443e23b6cSGreg Kroah-Hartman } 325c3b1a350SAl Viro 326cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 327cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 328cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 329cfa57c11SAl Viro * have around. 330cfa57c11SAl Viro */ 331cfa57c11SAl Viro if (!parent) 332cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 333cfa57c11SAl Viro 3345955102cSAl Viro inode_lock(d_inode(parent)); 335cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3362b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 337c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 338c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 339c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 340c33d4423SGreg Kroah-Hartman else 341c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 342c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 343190afd81SAl Viro dput(dentry); 344190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 345190afd81SAl Viro } 3460ee9608cSDaniel Borkmann 3470ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3485955102cSAl Viro inode_unlock(d_inode(parent)); 3490ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3500ee9608cSDaniel Borkmann } 3510ee9608cSDaniel Borkmann 352190afd81SAl Viro return dentry; 353190afd81SAl Viro } 354190afd81SAl Viro 3555233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 356190afd81SAl Viro { 3575955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 358190afd81SAl Viro dput(dentry); 359190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 360ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 361190afd81SAl Viro } 3625233e311SAl Viro 3635233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3645233e311SAl Viro { 3655955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 366190afd81SAl Viro return dentry; 367190afd81SAl Viro } 368190afd81SAl Viro 36949d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 37049d200deSNicolai Stange struct dentry *parent, void *data, 37149d200deSNicolai Stange const struct file_operations *proxy_fops, 37249d200deSNicolai Stange const struct file_operations *real_fops) 37349d200deSNicolai Stange { 37449d200deSNicolai Stange struct dentry *dentry; 37549d200deSNicolai Stange struct inode *inode; 37649d200deSNicolai Stange 37749d200deSNicolai Stange if (!(mode & S_IFMT)) 37849d200deSNicolai Stange mode |= S_IFREG; 37949d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 38049d200deSNicolai Stange dentry = start_creating(name, parent); 38149d200deSNicolai Stange 3827d39bc50SNicolai Stange if (IS_ERR(dentry)) 383ff9fb72bSGreg Kroah-Hartman return dentry; 38449d200deSNicolai Stange 38549d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 38643e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 38743e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 38843e23b6cSGreg Kroah-Hartman name); 38949d200deSNicolai Stange return failed_creating(dentry); 39043e23b6cSGreg Kroah-Hartman } 39149d200deSNicolai Stange 39249d200deSNicolai Stange inode->i_mode = mode; 39349d200deSNicolai Stange inode->i_private = data; 39449d200deSNicolai Stange 3955496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 39649d200deSNicolai Stange inode->i_fop = proxy_fops; 3977d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 3987d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 39949d200deSNicolai Stange 40049d200deSNicolai Stange d_instantiate(dentry, inode); 40149d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 40249d200deSNicolai Stange return end_creating(dentry); 40349d200deSNicolai Stange } 40449d200deSNicolai Stange 4051da177e4SLinus Torvalds /** 4061da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 4071da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 408be030e65SAlberto Bertogli * @mode: the permission that the file should have. 4091da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 410e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4111da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4121da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 4138e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 4141da177e4SLinus Torvalds * the open() call. 4151da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 4161da177e4SLinus Torvalds * this file. 4171da177e4SLinus Torvalds * 4181da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 419be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 420be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4211da177e4SLinus Torvalds * recommended to be used instead.) 4221da177e4SLinus Torvalds * 4231da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4241da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4251da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 426ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 427ff9fb72bSGreg Kroah-Hartman * returned. 4281da177e4SLinus Torvalds * 4296468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 430873760fbSCornelia Huck * returned. 4311da177e4SLinus Torvalds */ 432f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4331da177e4SLinus Torvalds struct dentry *parent, void *data, 43499ac48f5SArjan van de Ven const struct file_operations *fops) 4351da177e4SLinus Torvalds { 436c3b1a350SAl Viro 43749d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 43849d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 43949d200deSNicolai Stange &debugfs_noop_file_operations, 44049d200deSNicolai Stange fops); 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4431da177e4SLinus Torvalds 444c6468808SNicolai Stange /** 445c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 446c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 447c6468808SNicolai Stange * @mode: the permission that the file should have. 448c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 449c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 450c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 451c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 452c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 453c6468808SNicolai Stange * the open() call. 454c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 455c6468808SNicolai Stange * this file. 456c6468808SNicolai Stange * 457c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 458c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 459c6468808SNicolai Stange * handed it will not get protected against file removals by the 460c6468808SNicolai Stange * debugfs core. 461c6468808SNicolai Stange * 462c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 4630eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 4640eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 465c6468808SNicolai Stange * debugfs though. 466c6468808SNicolai Stange * 467c6468808SNicolai Stange * Any struct file_operations defined by means of 468c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 469c6468808SNicolai Stange * thus, may be used here. 470c6468808SNicolai Stange */ 47149d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 47249d200deSNicolai Stange struct dentry *parent, void *data, 47349d200deSNicolai Stange const struct file_operations *fops) 47449d200deSNicolai Stange { 47549d200deSNicolai Stange 47649d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 47749d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 47849d200deSNicolai Stange &debugfs_noop_file_operations, 47949d200deSNicolai Stange fops); 48049d200deSNicolai Stange } 481c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 48249d200deSNicolai Stange 4831da177e4SLinus Torvalds /** 484e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 485e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 486e59b4e91SDavid Howells * @mode: the permission that the file should have. 487e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 488e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 489e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 490e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 491e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 492e59b4e91SDavid Howells * the open() call. 493e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 494e59b4e91SDavid Howells * this file. 495e59b4e91SDavid Howells * @file_size: initial file size 496e59b4e91SDavid Howells * 497e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 498e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 499e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 500e59b4e91SDavid Howells * recommended to be used instead.) 501e59b4e91SDavid Howells * 502e59b4e91SDavid Howells * This function will return a pointer to a dentry if it succeeds. This 503e59b4e91SDavid Howells * pointer must be passed to the debugfs_remove() function when the file is 504e59b4e91SDavid Howells * to be removed (no automatic cleanup happens if your module is unloaded, 505ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 506ff9fb72bSGreg Kroah-Hartman * returned. 507e59b4e91SDavid Howells * 508e59b4e91SDavid Howells * If debugfs is not enabled in the kernel, the value -%ENODEV will be 509e59b4e91SDavid Howells * returned. 510e59b4e91SDavid Howells */ 511e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode, 512e59b4e91SDavid Howells struct dentry *parent, void *data, 513e59b4e91SDavid Howells const struct file_operations *fops, 514e59b4e91SDavid Howells loff_t file_size) 515e59b4e91SDavid Howells { 516e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 517e59b4e91SDavid Howells 518e59b4e91SDavid Howells if (de) 5192b0143b5SDavid Howells d_inode(de)->i_size = file_size; 520e59b4e91SDavid Howells return de; 521e59b4e91SDavid Howells } 522e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 523e59b4e91SDavid Howells 524e59b4e91SDavid Howells /** 5251da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5261da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5271da177e4SLinus Torvalds * create. 5281da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 529e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5301da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5311da177e4SLinus Torvalds * 5321da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5331da177e4SLinus Torvalds * 5341da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5351da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5361da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 537ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 538ff9fb72bSGreg Kroah-Hartman * returned. 5391da177e4SLinus Torvalds * 5406468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 541873760fbSCornelia Huck * returned. 5421da177e4SLinus Torvalds */ 5431da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5441da177e4SLinus Torvalds { 545ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 546680b3024SAl Viro struct inode *inode; 547ad5abd5bSAl Viro 548ad5abd5bSAl Viro if (IS_ERR(dentry)) 549ff9fb72bSGreg Kroah-Hartman return dentry; 550ad5abd5bSAl Viro 551edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 55243e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 55343e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 55443e23b6cSGreg Kroah-Hartman name); 5555233e311SAl Viro return failed_creating(dentry); 55643e23b6cSGreg Kroah-Hartman } 557680b3024SAl Viro 558f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 5595496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 560edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 561edac65eaSAl Viro 562edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 563edac65eaSAl Viro inc_nlink(inode); 564680b3024SAl Viro d_instantiate(dentry, inode); 5652b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 5662b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 5675233e311SAl Viro return end_creating(dentry); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /** 57277b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 57377b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 57477b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 57577b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 57677b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 57777b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 57877b3da6eSAl Viro * @data: opaque argument to pass to f(). 57977b3da6eSAl Viro * 58077b3da6eSAl Viro * @f should return what ->d_automount() would. 58177b3da6eSAl Viro */ 58277b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 58377b3da6eSAl Viro struct dentry *parent, 58493faccbbSEric W. Biederman debugfs_automount_t f, 58577b3da6eSAl Viro void *data) 58677b3da6eSAl Viro { 58777b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 58877b3da6eSAl Viro struct inode *inode; 58977b3da6eSAl Viro 59077b3da6eSAl Viro if (IS_ERR(dentry)) 591ff9fb72bSGreg Kroah-Hartman return dentry; 59277b3da6eSAl Viro 59377b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 59443e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 59543e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 59643e23b6cSGreg Kroah-Hartman name); 59777b3da6eSAl Viro return failed_creating(dentry); 59843e23b6cSGreg Kroah-Hartman } 59977b3da6eSAl Viro 60087243debSSeth Forshee make_empty_dir_inode(inode); 60177b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 60277b3da6eSAl Viro inode->i_private = data; 60377b3da6eSAl Viro dentry->d_fsdata = (void *)f; 604a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 605a8f324a4SRoman Pen inc_nlink(inode); 60677b3da6eSAl Viro d_instantiate(dentry, inode); 607a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 608a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 60977b3da6eSAl Viro return end_creating(dentry); 61077b3da6eSAl Viro } 61177b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 61277b3da6eSAl Viro 61377b3da6eSAl Viro /** 61466f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 61566f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 61666f54963SPeter Oberparleiter * create. 61766f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 618e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 61966f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 62066f54963SPeter Oberparleiter * filesystem. 62166f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 62266f54963SPeter Oberparleiter * symbolic link. 62366f54963SPeter Oberparleiter * 62466f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 62566f54963SPeter Oberparleiter * links to the given target path. 62666f54963SPeter Oberparleiter * 62766f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 62866f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 62966f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 630ff9fb72bSGreg Kroah-Hartman * unloaded, you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) 631ff9fb72bSGreg Kroah-Hartman * will be returned. 63266f54963SPeter Oberparleiter * 63366f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 634873760fbSCornelia Huck * returned. 63566f54963SPeter Oberparleiter */ 63666f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 63766f54963SPeter Oberparleiter const char *target) 63866f54963SPeter Oberparleiter { 639ad5abd5bSAl Viro struct dentry *dentry; 640680b3024SAl Viro struct inode *inode; 641680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 64266f54963SPeter Oberparleiter if (!link) 643ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 64466f54963SPeter Oberparleiter 645ad5abd5bSAl Viro dentry = start_creating(name, parent); 646ad5abd5bSAl Viro if (IS_ERR(dentry)) { 64766f54963SPeter Oberparleiter kfree(link); 648ff9fb72bSGreg Kroah-Hartman return dentry; 649ad5abd5bSAl Viro } 650ad5abd5bSAl Viro 651edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 652680b3024SAl Viro if (unlikely(!inode)) { 65343e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 65443e23b6cSGreg Kroah-Hartman name); 655ad5abd5bSAl Viro kfree(link); 6565233e311SAl Viro return failed_creating(dentry); 657680b3024SAl Viro } 658edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 6595496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 6605723cb01SAl Viro inode->i_link = link; 661680b3024SAl Viro d_instantiate(dentry, inode); 6625233e311SAl Viro return end_creating(dentry); 66366f54963SPeter Oberparleiter } 66466f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 66566f54963SPeter Oberparleiter 666823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 667e9117a5aSNicolai Stange { 668e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 669e9117a5aSNicolai Stange 6707d39bc50SNicolai Stange /* 6717d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 6727d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 6737d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 6747d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 6757d39bc50SNicolai Stange */ 6767d39bc50SNicolai Stange smp_mb(); 6777d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 6787d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 6797d39bc50SNicolai Stange return; 680e9117a5aSNicolai Stange if (!refcount_dec_and_test(&fsd->active_users)) 681e9117a5aSNicolai Stange wait_for_completion(&fsd->active_users_drained); 682e9117a5aSNicolai Stange } 683e9117a5aSNicolai Stange 68425d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 6851da177e4SLinus Torvalds { 68665c33336SMathieu Desnoyers int ret = 0; 6871da177e4SLinus Torvalds 688dc3f4198SAl Viro if (simple_positive(dentry)) { 68929a7f3adSMathieu Desnoyers dget(dentry); 690823e545cSAmir Goldstein if (d_is_dir(dentry)) { 6912b0143b5SDavid Howells ret = simple_rmdir(d_inode(parent), dentry); 6926679ea6dSAmir Goldstein if (!ret) 6936679ea6dSAmir Goldstein fsnotify_rmdir(d_inode(parent), dentry); 694823e545cSAmir Goldstein } else { 6952b0143b5SDavid Howells simple_unlink(d_inode(parent), dentry); 6966679ea6dSAmir Goldstein fsnotify_unlink(d_inode(parent), dentry); 697823e545cSAmir Goldstein } 69829a7f3adSMathieu Desnoyers if (!ret) 69929a7f3adSMathieu Desnoyers d_delete(dentry); 700823e545cSAmir Goldstein if (d_is_reg(dentry)) 701823e545cSAmir Goldstein __debugfs_file_removed(dentry); 70229a7f3adSMathieu Desnoyers dput(dentry); 7031da177e4SLinus Torvalds } 70425d41d84SJan Kara return ret; 7059505e637SHaavard Skinnemoen } 7069505e637SHaavard Skinnemoen 7079505e637SHaavard Skinnemoen /** 7089505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 7099505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 710398dc4adSUlf Magnusson * removed. If this parameter is NULL or an error value, nothing 711398dc4adSUlf Magnusson * will be done. 7129505e637SHaavard Skinnemoen * 7139505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 7149505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 7159505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 7169505e637SHaavard Skinnemoen * 7179505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7189505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7199505e637SHaavard Skinnemoen * removed, you are responsible here. 7209505e637SHaavard Skinnemoen */ 7219505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 7229505e637SHaavard Skinnemoen { 7239505e637SHaavard Skinnemoen struct dentry *parent; 72425d41d84SJan Kara int ret; 7259505e637SHaavard Skinnemoen 726a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7279505e637SHaavard Skinnemoen return; 7289505e637SHaavard Skinnemoen 7299505e637SHaavard Skinnemoen parent = dentry->d_parent; 7305955102cSAl Viro inode_lock(d_inode(parent)); 73125d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 7325955102cSAl Viro inode_unlock(d_inode(parent)); 73325d41d84SJan Kara if (!ret) 7341da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 7371da177e4SLinus Torvalds 738cfc94cdfSJan Kara /** 7399505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 740398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 741398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 7429505e637SHaavard Skinnemoen * 7439505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 7449505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 7459505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 7469505e637SHaavard Skinnemoen * 7479505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7489505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7499505e637SHaavard Skinnemoen * removed, you are responsible here. 7509505e637SHaavard Skinnemoen */ 7519505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 7529505e637SHaavard Skinnemoen { 753485d4402SSteven Rostedt struct dentry *child, *parent; 7549505e637SHaavard Skinnemoen 755a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7569505e637SHaavard Skinnemoen return; 7579505e637SHaavard Skinnemoen 7589505e637SHaavard Skinnemoen parent = dentry; 759776164c1SOleg Nesterov down: 7605955102cSAl Viro inode_lock(d_inode(parent)); 761485d4402SSteven Rostedt loop: 762485d4402SSteven Rostedt /* 763485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 764485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 765485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 766485d4402SSteven Rostedt */ 767485d4402SSteven Rostedt spin_lock(&parent->d_lock); 768946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 769dc3f4198SAl Viro if (!simple_positive(child)) 770776164c1SOleg Nesterov continue; 7719505e637SHaavard Skinnemoen 772776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 7739505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 774485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 7755955102cSAl Viro inode_unlock(d_inode(parent)); 7769505e637SHaavard Skinnemoen parent = child; 777776164c1SOleg Nesterov goto down; 7789505e637SHaavard Skinnemoen } 779485d4402SSteven Rostedt 780485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 781485d4402SSteven Rostedt 782776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 783776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 784485d4402SSteven Rostedt 785485d4402SSteven Rostedt /* 786485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 787485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 788485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 789485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 790dc3f4198SAl Viro * simple_positive() check. 791485d4402SSteven Rostedt */ 792485d4402SSteven Rostedt goto loop; 793776164c1SOleg Nesterov } 794485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 795776164c1SOleg Nesterov 7965955102cSAl Viro inode_unlock(d_inode(parent)); 797776164c1SOleg Nesterov child = parent; 798776164c1SOleg Nesterov parent = parent->d_parent; 7995955102cSAl Viro inode_lock(d_inode(parent)); 800776164c1SOleg Nesterov 801485d4402SSteven Rostedt if (child != dentry) 802485d4402SSteven Rostedt /* go up */ 803485d4402SSteven Rostedt goto loop; 80456a83cc9SSteven Rostedt 805776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 8069505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 8075955102cSAl Viro inode_unlock(d_inode(parent)); 8089505e637SHaavard Skinnemoen } 8099505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 8109505e637SHaavard Skinnemoen 8119505e637SHaavard Skinnemoen /** 812cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 813cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 814cfc94cdfSJan Kara * should be a directory dentry. 815cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 816cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 817cfc94cdfSJan Kara * moved. This should be a directory dentry. 818cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 819cfc94cdfSJan Kara * 820cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 821cfc94cdfSJan Kara * exist for rename to succeed. 822cfc94cdfSJan Kara * 823cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 824cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 825cfc94cdfSJan Kara * returned. 826cfc94cdfSJan Kara * 827cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 828cfc94cdfSJan Kara * returned. 829cfc94cdfSJan Kara */ 830cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 831cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 832cfc94cdfSJan Kara { 833cfc94cdfSJan Kara int error; 834cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 83549d31c2fSAl Viro struct name_snapshot old_name; 836cfc94cdfSJan Kara 837d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 838d88c93f0SGreg Kroah-Hartman return old_dir; 839d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 840d88c93f0SGreg Kroah-Hartman return new_dir; 841d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 842d88c93f0SGreg Kroah-Hartman return old_dentry; 843d88c93f0SGreg Kroah-Hartman 844cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 845cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 8462b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 847cfc94cdfSJan Kara goto exit; 848cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 8492b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 850cfc94cdfSJan Kara d_mountpoint(old_dentry)) 851cfc94cdfSJan Kara goto exit; 852cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 853cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 8542b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 855cfc94cdfSJan Kara goto exit; 856cfc94cdfSJan Kara 85749d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 858cfc94cdfSJan Kara 8592b0143b5SDavid Howells error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 860e0e0be8aSMiklos Szeredi dentry, 0); 861cfc94cdfSJan Kara if (error) { 86249d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 863cfc94cdfSJan Kara goto exit; 864cfc94cdfSJan Kara } 865cfc94cdfSJan Kara d_move(old_dentry, dentry); 866f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 867e36cb0b8SDavid Howells d_is_dir(old_dentry), 8685a190ae6SAl Viro NULL, old_dentry); 86949d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 870cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 871cfc94cdfSJan Kara dput(dentry); 872cfc94cdfSJan Kara return old_dentry; 873cfc94cdfSJan Kara exit: 874cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 875cfc94cdfSJan Kara dput(dentry); 876cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 877ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 878ff9fb72bSGreg Kroah-Hartman return dentry; 879ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 880cfc94cdfSJan Kara } 881cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 882cfc94cdfSJan Kara 883c0f92ba9SFrederic Weisbecker /** 884c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 885c0f92ba9SFrederic Weisbecker */ 886c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 887c0f92ba9SFrederic Weisbecker { 888c0f92ba9SFrederic Weisbecker return debugfs_registered; 889c0f92ba9SFrederic Weisbecker } 890c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 891c0f92ba9SFrederic Weisbecker 8921da177e4SLinus Torvalds static int __init debugfs_init(void) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds int retval; 8951da177e4SLinus Torvalds 896f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 897f9bb4882SEric W. Biederman if (retval) 898f9bb4882SEric W. Biederman return retval; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 9011da177e4SLinus Torvalds if (retval) 902f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 903c0f92ba9SFrederic Weisbecker else 904c0f92ba9SFrederic Weisbecker debugfs_registered = true; 905c0f92ba9SFrederic Weisbecker 9061da177e4SLinus Torvalds return retval; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds core_initcall(debugfs_init); 9091da177e4SLinus Torvalds 910