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; 38a24c6f7bSPeter Enderborg static unsigned int debugfs_allow = 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 */ 455496197fSDavid Howells static int debugfs_setattr(struct dentry *dentry, struct iattr *ia) 465496197fSDavid Howells { 475496197fSDavid Howells int ret = security_locked_down(LOCKDOWN_DEBUGFS); 485496197fSDavid Howells 495496197fSDavid Howells if (ret && (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))) 505496197fSDavid Howells return ret; 515496197fSDavid Howells return simple_setattr(dentry, ia); 525496197fSDavid Howells } 535496197fSDavid Howells 545496197fSDavid Howells static const struct inode_operations debugfs_file_inode_operations = { 555496197fSDavid Howells .setattr = debugfs_setattr, 565496197fSDavid Howells }; 575496197fSDavid Howells static const struct inode_operations debugfs_dir_inode_operations = { 585496197fSDavid Howells .lookup = simple_lookup, 595496197fSDavid Howells .setattr = debugfs_setattr, 605496197fSDavid Howells }; 615496197fSDavid Howells static const struct inode_operations debugfs_symlink_inode_operations = { 625496197fSDavid Howells .get_link = simple_get_link, 635496197fSDavid Howells .setattr = debugfs_setattr, 645496197fSDavid Howells }; 655496197fSDavid Howells 66edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 691da177e4SLinus Torvalds if (inode) { 7085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 711b48b530SDeepa Dinamani inode->i_atime = inode->i_mtime = 72c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds return inode; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 77d6e48686SLudwig Nussel struct debugfs_mount_opts { 787dc05881SEric W. Biederman kuid_t uid; 797dc05881SEric W. Biederman kgid_t gid; 80d6e48686SLudwig Nussel umode_t mode; 81d6e48686SLudwig Nussel }; 82d6e48686SLudwig Nussel 83d6e48686SLudwig Nussel enum { 84d6e48686SLudwig Nussel Opt_uid, 85d6e48686SLudwig Nussel Opt_gid, 86d6e48686SLudwig Nussel Opt_mode, 87d6e48686SLudwig Nussel Opt_err 88d6e48686SLudwig Nussel }; 89d6e48686SLudwig Nussel 90d6e48686SLudwig Nussel static const match_table_t tokens = { 91d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 92d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 93d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 94d6e48686SLudwig Nussel {Opt_err, NULL} 95d6e48686SLudwig Nussel }; 96d6e48686SLudwig Nussel 97d6e48686SLudwig Nussel struct debugfs_fs_info { 98d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 99d6e48686SLudwig Nussel }; 100d6e48686SLudwig Nussel 101d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 102d6e48686SLudwig Nussel { 103d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 104d6e48686SLudwig Nussel int option; 105d6e48686SLudwig Nussel int token; 1067dc05881SEric W. Biederman kuid_t uid; 1077dc05881SEric W. Biederman kgid_t gid; 108d6e48686SLudwig Nussel char *p; 109d6e48686SLudwig Nussel 110d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 111d6e48686SLudwig Nussel 112d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 113d6e48686SLudwig Nussel if (!*p) 114d6e48686SLudwig Nussel continue; 115d6e48686SLudwig Nussel 116d6e48686SLudwig Nussel token = match_token(p, tokens, args); 117d6e48686SLudwig Nussel switch (token) { 118d6e48686SLudwig Nussel case Opt_uid: 119d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 120d6e48686SLudwig Nussel return -EINVAL; 1217dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1227dc05881SEric W. Biederman if (!uid_valid(uid)) 1237dc05881SEric W. Biederman return -EINVAL; 1247dc05881SEric W. Biederman opts->uid = uid; 125d6e48686SLudwig Nussel break; 126d6e48686SLudwig Nussel case Opt_gid: 127f1688e04SDave Reisner if (match_int(&args[0], &option)) 128d6e48686SLudwig Nussel return -EINVAL; 1297dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1307dc05881SEric W. Biederman if (!gid_valid(gid)) 1317dc05881SEric W. Biederman return -EINVAL; 1327dc05881SEric W. Biederman opts->gid = gid; 133d6e48686SLudwig Nussel break; 134d6e48686SLudwig Nussel case Opt_mode: 135d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 136d6e48686SLudwig Nussel return -EINVAL; 137d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 138d6e48686SLudwig Nussel break; 139d6e48686SLudwig Nussel /* 140d6e48686SLudwig Nussel * We might like to report bad mount options here; 141d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 142d6e48686SLudwig Nussel */ 143d6e48686SLudwig Nussel } 144d6e48686SLudwig Nussel } 145d6e48686SLudwig Nussel 146d6e48686SLudwig Nussel return 0; 147d6e48686SLudwig Nussel } 148d6e48686SLudwig Nussel 149d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 150d6e48686SLudwig Nussel { 151d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1522b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 153d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 154d6e48686SLudwig Nussel 155d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 156d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 157d6e48686SLudwig Nussel 158d6e48686SLudwig Nussel inode->i_uid = opts->uid; 159d6e48686SLudwig Nussel inode->i_gid = opts->gid; 160d6e48686SLudwig Nussel 161d6e48686SLudwig Nussel return 0; 162d6e48686SLudwig Nussel } 163d6e48686SLudwig Nussel 164d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 165d6e48686SLudwig Nussel { 166d6e48686SLudwig Nussel int err; 167d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 168d6e48686SLudwig Nussel 16902b9984dSTheodore Ts'o sync_filesystem(sb); 170d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 171d6e48686SLudwig Nussel if (err) 172d6e48686SLudwig Nussel goto fail; 173d6e48686SLudwig Nussel 174d6e48686SLudwig Nussel debugfs_apply_options(sb); 175d6e48686SLudwig Nussel 176d6e48686SLudwig Nussel fail: 177d6e48686SLudwig Nussel return err; 178d6e48686SLudwig Nussel } 179d6e48686SLudwig Nussel 180d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 181d6e48686SLudwig Nussel { 182d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 183d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 184d6e48686SLudwig Nussel 1857dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1867dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1877dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1887dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1897dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1907dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 191d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 192d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 193d6e48686SLudwig Nussel 194d6e48686SLudwig Nussel return 0; 195d6e48686SLudwig Nussel } 196d6e48686SLudwig Nussel 1976234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 1980db59e59SAl Viro { 1990db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 2005723cb01SAl Viro kfree(inode->i_link); 20193b919daSAl Viro free_inode_nonrcu(inode); 20293b919daSAl Viro } 20393b919daSAl Viro 204d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 205d6e48686SLudwig Nussel .statfs = simple_statfs, 206d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 207d6e48686SLudwig Nussel .show_options = debugfs_show_options, 2086234ddf4SAl Viro .free_inode = debugfs_free_inode, 209d6e48686SLudwig Nussel }; 210d6e48686SLudwig Nussel 2117c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 2127c8d4698SNicolai Stange { 2137d39bc50SNicolai Stange void *fsd = dentry->d_fsdata; 2147d39bc50SNicolai Stange 2157d39bc50SNicolai Stange if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 2167c8d4698SNicolai Stange kfree(dentry->d_fsdata); 2177c8d4698SNicolai Stange } 2187c8d4698SNicolai Stange 21977b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 22077b3da6eSAl Viro { 22193faccbbSEric W. Biederman debugfs_automount_t f; 22293faccbbSEric W. Biederman f = (debugfs_automount_t)path->dentry->d_fsdata; 22393faccbbSEric W. Biederman return f(path->dentry, d_inode(path->dentry)->i_private); 22477b3da6eSAl Viro } 22577b3da6eSAl Viro 22677b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 22777b3da6eSAl Viro .d_delete = always_delete_dentry, 2287c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 22977b3da6eSAl Viro .d_automount = debugfs_automount, 23077b3da6eSAl Viro }; 23177b3da6eSAl Viro 2321da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2331da177e4SLinus Torvalds { 234cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 235d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 236d6e48686SLudwig Nussel int err; 2371da177e4SLinus Torvalds 238d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 239d6e48686SLudwig Nussel sb->s_fs_info = fsi; 240d6e48686SLudwig Nussel if (!fsi) { 241d6e48686SLudwig Nussel err = -ENOMEM; 242d6e48686SLudwig Nussel goto fail; 243d6e48686SLudwig Nussel } 244d6e48686SLudwig Nussel 245d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 246d6e48686SLudwig Nussel if (err) 247d6e48686SLudwig Nussel goto fail; 248d6e48686SLudwig Nussel 249d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 250d6e48686SLudwig Nussel if (err) 251d6e48686SLudwig Nussel goto fail; 252d6e48686SLudwig Nussel 253d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 25477b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 255d6e48686SLudwig Nussel 256d6e48686SLudwig Nussel debugfs_apply_options(sb); 257d6e48686SLudwig Nussel 258d6e48686SLudwig Nussel return 0; 259d6e48686SLudwig Nussel 260d6e48686SLudwig Nussel fail: 261d6e48686SLudwig Nussel kfree(fsi); 262d6e48686SLudwig Nussel sb->s_fs_info = NULL; 263d6e48686SLudwig Nussel return err; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2671da177e4SLinus Torvalds int flags, const char *dev_name, 268fc14f2feSAl Viro void *data) 2691da177e4SLinus Torvalds { 270a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 271a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 272a24c6f7bSPeter Enderborg 273fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2771da177e4SLinus Torvalds .owner = THIS_MODULE, 2781da177e4SLinus Torvalds .name = "debugfs", 279fc14f2feSAl Viro .mount = debug_mount, 2801da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2811da177e4SLinus Torvalds }; 2827f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2831da177e4SLinus Torvalds 284a7c5437bSOmar Sandoval /** 285a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 286a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 287a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 288a7c5437bSOmar Sandoval * 289a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 290a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 291a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 292a7c5437bSOmar Sandoval * 293a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 294a7c5437bSOmar Sandoval * returned. 295a7c5437bSOmar Sandoval */ 296a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 297a7c5437bSOmar Sandoval { 298a7c5437bSOmar Sandoval struct dentry *dentry; 299a7c5437bSOmar Sandoval 300*bc6de804SGreg Kroah-Hartman if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent)) 301a7c5437bSOmar Sandoval return NULL; 302a7c5437bSOmar Sandoval 303a7c5437bSOmar Sandoval if (!parent) 304a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 305a7c5437bSOmar Sandoval 3066c2d4798SAl Viro dentry = lookup_positive_unlocked(name, parent, strlen(name)); 307a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 308a7c5437bSOmar Sandoval return NULL; 309a7c5437bSOmar Sandoval return dentry; 310a7c5437bSOmar Sandoval } 311a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 312a7c5437bSOmar Sandoval 313190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 314c3b1a350SAl Viro { 315190afd81SAl Viro struct dentry *dentry; 316c3b1a350SAl Viro int error; 317c3b1a350SAl Viro 318a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) 319a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 320a24c6f7bSPeter Enderborg 321d03ae477SGreg Kroah-Hartman pr_debug("creating file '%s'\n", name); 322c3b1a350SAl Viro 323c9e15f25SGreg KH if (IS_ERR(parent)) 324c9e15f25SGreg KH return parent; 325c9e15f25SGreg KH 326c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 327c3b1a350SAl Viro &debugfs_mount_count); 32843e23b6cSGreg Kroah-Hartman if (error) { 32943e23b6cSGreg Kroah-Hartman pr_err("Unable to pin filesystem for file '%s'\n", name); 330190afd81SAl Viro return ERR_PTR(error); 33143e23b6cSGreg Kroah-Hartman } 332c3b1a350SAl Viro 333cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 334cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 335cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 336cfa57c11SAl Viro * have around. 337cfa57c11SAl Viro */ 338cfa57c11SAl Viro if (!parent) 339cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 340cfa57c11SAl Viro 3415955102cSAl Viro inode_lock(d_inode(parent)); 342a3d1e7ebSAl Viro if (unlikely(IS_DEADDIR(d_inode(parent)))) 343a3d1e7ebSAl Viro dentry = ERR_PTR(-ENOENT); 344a3d1e7ebSAl Viro else 345cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3462b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 347c33d4423SGreg Kroah-Hartman if (d_is_dir(dentry)) 348c33d4423SGreg Kroah-Hartman pr_err("Directory '%s' with parent '%s' already present!\n", 349c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 350c33d4423SGreg Kroah-Hartman else 351c33d4423SGreg Kroah-Hartman pr_err("File '%s' in directory '%s' already present!\n", 352c33d4423SGreg Kroah-Hartman name, parent->d_name.name); 353190afd81SAl Viro dput(dentry); 354190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 355190afd81SAl Viro } 3560ee9608cSDaniel Borkmann 3570ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3585955102cSAl Viro inode_unlock(d_inode(parent)); 3590ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3600ee9608cSDaniel Borkmann } 3610ee9608cSDaniel Borkmann 362190afd81SAl Viro return dentry; 363190afd81SAl Viro } 364190afd81SAl Viro 3655233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 366190afd81SAl Viro { 3675955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 368190afd81SAl Viro dput(dentry); 369190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 370ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 371190afd81SAl Viro } 3725233e311SAl Viro 3735233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3745233e311SAl Viro { 3755955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 376190afd81SAl Viro return dentry; 377190afd81SAl Viro } 378190afd81SAl Viro 37949d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 38049d200deSNicolai Stange struct dentry *parent, void *data, 38149d200deSNicolai Stange const struct file_operations *proxy_fops, 38249d200deSNicolai Stange const struct file_operations *real_fops) 38349d200deSNicolai Stange { 38449d200deSNicolai Stange struct dentry *dentry; 38549d200deSNicolai Stange struct inode *inode; 38649d200deSNicolai Stange 38749d200deSNicolai Stange if (!(mode & S_IFMT)) 38849d200deSNicolai Stange mode |= S_IFREG; 38949d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 39049d200deSNicolai Stange dentry = start_creating(name, parent); 39149d200deSNicolai Stange 3927d39bc50SNicolai Stange if (IS_ERR(dentry)) 393ff9fb72bSGreg Kroah-Hartman return dentry; 39449d200deSNicolai Stange 395a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 396a24c6f7bSPeter Enderborg failed_creating(dentry); 397a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 398a24c6f7bSPeter Enderborg } 399a24c6f7bSPeter Enderborg 40049d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 40143e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 40243e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create file '%s'\n", 40343e23b6cSGreg Kroah-Hartman name); 40449d200deSNicolai Stange return failed_creating(dentry); 40543e23b6cSGreg Kroah-Hartman } 40649d200deSNicolai Stange 40749d200deSNicolai Stange inode->i_mode = mode; 40849d200deSNicolai Stange inode->i_private = data; 40949d200deSNicolai Stange 4105496197fSDavid Howells inode->i_op = &debugfs_file_inode_operations; 41149d200deSNicolai Stange inode->i_fop = proxy_fops; 4127d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 4137d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 41449d200deSNicolai Stange 41549d200deSNicolai Stange d_instantiate(dentry, inode); 41649d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 41749d200deSNicolai Stange return end_creating(dentry); 41849d200deSNicolai Stange } 41949d200deSNicolai Stange 4201da177e4SLinus Torvalds /** 4211da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 4221da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 423be030e65SAlberto Bertogli * @mode: the permission that the file should have. 4241da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 425e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4261da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4271da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 4288e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 4291da177e4SLinus Torvalds * the open() call. 4301da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 4311da177e4SLinus Torvalds * this file. 4321da177e4SLinus Torvalds * 4331da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 434be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 435be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4361da177e4SLinus Torvalds * recommended to be used instead.) 4371da177e4SLinus Torvalds * 4381da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4391da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4401da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 441adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 442ff9fb72bSGreg Kroah-Hartman * returned. 4431da177e4SLinus Torvalds * 4446468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 445873760fbSCornelia Huck * returned. 4461da177e4SLinus Torvalds */ 447f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4481da177e4SLinus Torvalds struct dentry *parent, void *data, 44999ac48f5SArjan van de Ven const struct file_operations *fops) 4501da177e4SLinus Torvalds { 451c3b1a350SAl Viro 45249d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 45349d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 45449d200deSNicolai Stange &debugfs_noop_file_operations, 45549d200deSNicolai Stange fops); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4581da177e4SLinus Torvalds 459c6468808SNicolai Stange /** 460c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 461c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 462c6468808SNicolai Stange * @mode: the permission that the file should have. 463c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 464c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 465c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 466c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 467c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 468c6468808SNicolai Stange * the open() call. 469c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 470c6468808SNicolai Stange * this file. 471c6468808SNicolai Stange * 472c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 473c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 474c6468808SNicolai Stange * handed it will not get protected against file removals by the 475c6468808SNicolai Stange * debugfs core. 476c6468808SNicolai Stange * 477c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 4780eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 4790eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 480c6468808SNicolai Stange * debugfs though. 481c6468808SNicolai Stange * 482c6468808SNicolai Stange * Any struct file_operations defined by means of 483c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 484c6468808SNicolai Stange * thus, may be used here. 485c6468808SNicolai Stange */ 48649d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 48749d200deSNicolai Stange struct dentry *parent, void *data, 48849d200deSNicolai Stange const struct file_operations *fops) 48949d200deSNicolai Stange { 49049d200deSNicolai Stange 49149d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 49249d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 49349d200deSNicolai Stange &debugfs_noop_file_operations, 49449d200deSNicolai Stange fops); 49549d200deSNicolai Stange } 496c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 49749d200deSNicolai Stange 4981da177e4SLinus Torvalds /** 499e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 500e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 501e59b4e91SDavid Howells * @mode: the permission that the file should have. 502e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 503e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 504e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 505e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 506e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 507e59b4e91SDavid Howells * the open() call. 508e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 509e59b4e91SDavid Howells * this file. 510e59b4e91SDavid Howells * @file_size: initial file size 511e59b4e91SDavid Howells * 512e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 513e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 514e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 515e59b4e91SDavid Howells * recommended to be used instead.) 516e59b4e91SDavid Howells */ 517526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode, 518e59b4e91SDavid Howells struct dentry *parent, void *data, 519e59b4e91SDavid Howells const struct file_operations *fops, 520e59b4e91SDavid Howells loff_t file_size) 521e59b4e91SDavid Howells { 522e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 523e59b4e91SDavid Howells 524e59b4e91SDavid Howells if (de) 5252b0143b5SDavid Howells d_inode(de)->i_size = file_size; 526e59b4e91SDavid Howells } 527e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 528e59b4e91SDavid Howells 529e59b4e91SDavid Howells /** 5301da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5311da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5321da177e4SLinus Torvalds * create. 5331da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 534e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5351da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5361da177e4SLinus Torvalds * 5371da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5381da177e4SLinus Torvalds * 5391da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5401da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5411da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 542adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 543ff9fb72bSGreg Kroah-Hartman * returned. 5441da177e4SLinus Torvalds * 5456468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 546873760fbSCornelia Huck * returned. 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5491da177e4SLinus Torvalds { 550ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 551680b3024SAl Viro struct inode *inode; 552ad5abd5bSAl Viro 553ad5abd5bSAl Viro if (IS_ERR(dentry)) 554ff9fb72bSGreg Kroah-Hartman return dentry; 555ad5abd5bSAl Viro 556a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 557a24c6f7bSPeter Enderborg failed_creating(dentry); 558a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 559a24c6f7bSPeter Enderborg } 560a24c6f7bSPeter Enderborg 561edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 56243e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 56343e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create directory '%s'\n", 56443e23b6cSGreg Kroah-Hartman name); 5655233e311SAl Viro return failed_creating(dentry); 56643e23b6cSGreg Kroah-Hartman } 567680b3024SAl Viro 568f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 5695496197fSDavid Howells inode->i_op = &debugfs_dir_inode_operations; 570edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 571edac65eaSAl Viro 572edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 573edac65eaSAl Viro inc_nlink(inode); 574680b3024SAl Viro d_instantiate(dentry, inode); 5752b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 5762b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 5775233e311SAl Viro return end_creating(dentry); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /** 58277b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 58377b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 58477b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 58577b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 58677b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 58777b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 58877b3da6eSAl Viro * @data: opaque argument to pass to f(). 58977b3da6eSAl Viro * 59077b3da6eSAl Viro * @f should return what ->d_automount() would. 59177b3da6eSAl Viro */ 59277b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 59377b3da6eSAl Viro struct dentry *parent, 59493faccbbSEric W. Biederman debugfs_automount_t f, 59577b3da6eSAl Viro void *data) 59677b3da6eSAl Viro { 59777b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 59877b3da6eSAl Viro struct inode *inode; 59977b3da6eSAl Viro 60077b3da6eSAl Viro if (IS_ERR(dentry)) 601ff9fb72bSGreg Kroah-Hartman return dentry; 60277b3da6eSAl Viro 603a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_API)) { 604a24c6f7bSPeter Enderborg failed_creating(dentry); 605a24c6f7bSPeter Enderborg return ERR_PTR(-EPERM); 606a24c6f7bSPeter Enderborg } 607a24c6f7bSPeter Enderborg 60877b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 60943e23b6cSGreg Kroah-Hartman if (unlikely(!inode)) { 61043e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create automount '%s'\n", 61143e23b6cSGreg Kroah-Hartman name); 61277b3da6eSAl Viro return failed_creating(dentry); 61343e23b6cSGreg Kroah-Hartman } 61477b3da6eSAl Viro 61587243debSSeth Forshee make_empty_dir_inode(inode); 61677b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 61777b3da6eSAl Viro inode->i_private = data; 61877b3da6eSAl Viro dentry->d_fsdata = (void *)f; 619a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 620a8f324a4SRoman Pen inc_nlink(inode); 62177b3da6eSAl Viro d_instantiate(dentry, inode); 622a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 623a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 62477b3da6eSAl Viro return end_creating(dentry); 62577b3da6eSAl Viro } 62677b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 62777b3da6eSAl Viro 62877b3da6eSAl Viro /** 62966f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 63066f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 63166f54963SPeter Oberparleiter * create. 63266f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 633e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 63466f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 63566f54963SPeter Oberparleiter * filesystem. 63666f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 63766f54963SPeter Oberparleiter * symbolic link. 63866f54963SPeter Oberparleiter * 63966f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 64066f54963SPeter Oberparleiter * links to the given target path. 64166f54963SPeter Oberparleiter * 64266f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 64366f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 64466f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 645adc92dd4SDaniel W. S. Almeida * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR) 646ff9fb72bSGreg Kroah-Hartman * will be returned. 64766f54963SPeter Oberparleiter * 64866f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 649873760fbSCornelia Huck * returned. 65066f54963SPeter Oberparleiter */ 65166f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 65266f54963SPeter Oberparleiter const char *target) 65366f54963SPeter Oberparleiter { 654ad5abd5bSAl Viro struct dentry *dentry; 655680b3024SAl Viro struct inode *inode; 656680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 65766f54963SPeter Oberparleiter if (!link) 658ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 65966f54963SPeter Oberparleiter 660ad5abd5bSAl Viro dentry = start_creating(name, parent); 661ad5abd5bSAl Viro if (IS_ERR(dentry)) { 66266f54963SPeter Oberparleiter kfree(link); 663ff9fb72bSGreg Kroah-Hartman return dentry; 664ad5abd5bSAl Viro } 665ad5abd5bSAl Viro 666edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 667680b3024SAl Viro if (unlikely(!inode)) { 66843e23b6cSGreg Kroah-Hartman pr_err("out of free dentries, can not create symlink '%s'\n", 66943e23b6cSGreg Kroah-Hartman name); 670ad5abd5bSAl Viro kfree(link); 6715233e311SAl Viro return failed_creating(dentry); 672680b3024SAl Viro } 673edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 6745496197fSDavid Howells inode->i_op = &debugfs_symlink_inode_operations; 6755723cb01SAl Viro inode->i_link = link; 676680b3024SAl Viro d_instantiate(dentry, inode); 6775233e311SAl Viro return end_creating(dentry); 67866f54963SPeter Oberparleiter } 67966f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 68066f54963SPeter Oberparleiter 681823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry) 682e9117a5aSNicolai Stange { 683e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 684e9117a5aSNicolai Stange 6857d39bc50SNicolai Stange /* 6867d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 6877d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 6887d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 6897d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 6907d39bc50SNicolai Stange */ 6917d39bc50SNicolai Stange smp_mb(); 6927d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 6937d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 6947d39bc50SNicolai Stange return; 695e9117a5aSNicolai Stange if (!refcount_dec_and_test(&fsd->active_users)) 696e9117a5aSNicolai Stange wait_for_completion(&fsd->active_users_drained); 697e9117a5aSNicolai Stange } 698e9117a5aSNicolai Stange 699a3d1e7ebSAl Viro static void remove_one(struct dentry *victim) 7001da177e4SLinus Torvalds { 701a3d1e7ebSAl Viro if (d_is_reg(victim)) 702a3d1e7ebSAl Viro __debugfs_file_removed(victim); 7031da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 706cfc94cdfSJan Kara /** 707a3d1e7ebSAl Viro * debugfs_remove - recursively removes a directory 708398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 709398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 7109505e637SHaavard Skinnemoen * 7119505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 7129505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 7139505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 7149505e637SHaavard Skinnemoen * 7159505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7169505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7179505e637SHaavard Skinnemoen * removed, you are responsible here. 7189505e637SHaavard Skinnemoen */ 719a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry) 7209505e637SHaavard Skinnemoen { 721a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7229505e637SHaavard Skinnemoen return; 7239505e637SHaavard Skinnemoen 724a3d1e7ebSAl Viro simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); 725a3d1e7ebSAl Viro simple_recursive_removal(dentry, remove_one); 726776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 727776164c1SOleg Nesterov } 728a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove); 7299505e637SHaavard Skinnemoen 7309505e637SHaavard Skinnemoen /** 731cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 732cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 733cfc94cdfSJan Kara * should be a directory dentry. 734cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 735cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 736cfc94cdfSJan Kara * moved. This should be a directory dentry. 737cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 738cfc94cdfSJan Kara * 739cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 740cfc94cdfSJan Kara * exist for rename to succeed. 741cfc94cdfSJan Kara * 742cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 743cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 744cfc94cdfSJan Kara * returned. 745cfc94cdfSJan Kara * 746cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 747cfc94cdfSJan Kara * returned. 748cfc94cdfSJan Kara */ 749cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 750cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 751cfc94cdfSJan Kara { 752cfc94cdfSJan Kara int error; 753cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 75449d31c2fSAl Viro struct name_snapshot old_name; 755cfc94cdfSJan Kara 756d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 757d88c93f0SGreg Kroah-Hartman return old_dir; 758d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 759d88c93f0SGreg Kroah-Hartman return new_dir; 760d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 761d88c93f0SGreg Kroah-Hartman return old_dentry; 762d88c93f0SGreg Kroah-Hartman 763cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 764cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 7652b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 766cfc94cdfSJan Kara goto exit; 767cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 7682b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 769cfc94cdfSJan Kara d_mountpoint(old_dentry)) 770cfc94cdfSJan Kara goto exit; 771cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 772cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 7732b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 774cfc94cdfSJan Kara goto exit; 775cfc94cdfSJan Kara 77649d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 777cfc94cdfSJan Kara 7782b0143b5SDavid Howells error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 779e0e0be8aSMiklos Szeredi dentry, 0); 780cfc94cdfSJan Kara if (error) { 78149d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 782cfc94cdfSJan Kara goto exit; 783cfc94cdfSJan Kara } 784cfc94cdfSJan Kara d_move(old_dentry, dentry); 785f4ec3a3dSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name, 786e36cb0b8SDavid Howells d_is_dir(old_dentry), 7875a190ae6SAl Viro NULL, old_dentry); 78849d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 789cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 790cfc94cdfSJan Kara dput(dentry); 791cfc94cdfSJan Kara return old_dentry; 792cfc94cdfSJan Kara exit: 793cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 794cfc94cdfSJan Kara dput(dentry); 795cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 796ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 797ff9fb72bSGreg Kroah-Hartman return dentry; 798ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 799cfc94cdfSJan Kara } 800cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 801cfc94cdfSJan Kara 802c0f92ba9SFrederic Weisbecker /** 803c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 804c0f92ba9SFrederic Weisbecker */ 805c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 806c0f92ba9SFrederic Weisbecker { 807c0f92ba9SFrederic Weisbecker return debugfs_registered; 808c0f92ba9SFrederic Weisbecker } 809c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 810c0f92ba9SFrederic Weisbecker 811a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str) 812a24c6f7bSPeter Enderborg { 813a24c6f7bSPeter Enderborg if (str) { 814a24c6f7bSPeter Enderborg if (!strcmp(str, "on")) 815a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT; 816a24c6f7bSPeter Enderborg else if (!strcmp(str, "no-mount")) 817a24c6f7bSPeter Enderborg debugfs_allow = DEBUGFS_ALLOW_API; 818a24c6f7bSPeter Enderborg else if (!strcmp(str, "off")) 819a24c6f7bSPeter Enderborg debugfs_allow = 0; 820a24c6f7bSPeter Enderborg } 821a24c6f7bSPeter Enderborg 822a24c6f7bSPeter Enderborg return 0; 823a24c6f7bSPeter Enderborg } 824a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel); 8251da177e4SLinus Torvalds static int __init debugfs_init(void) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds int retval; 8281da177e4SLinus Torvalds 829a24c6f7bSPeter Enderborg if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT)) 830a24c6f7bSPeter Enderborg return -EPERM; 831a24c6f7bSPeter Enderborg 832f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 833f9bb4882SEric W. Biederman if (retval) 834f9bb4882SEric W. Biederman return retval; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 8371da177e4SLinus Torvalds if (retval) 838f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 839c0f92ba9SFrederic Weisbecker else 840c0f92ba9SFrederic Weisbecker debugfs_registered = true; 841c0f92ba9SFrederic Weisbecker 8421da177e4SLinus Torvalds return retval; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds core_initcall(debugfs_init); 845