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 * 51da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 61da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 9e1511a84SMauro Carvalho Chehab * See ./Documentation/core-api/kernel-api.rst for more details. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141da177e4SLinus Torvalds #include <linux/mount.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/init.h> 174d8ebddcSRandy Dunlap #include <linux/kobject.h> 181da177e4SLinus Torvalds #include <linux/namei.h> 191da177e4SLinus Torvalds #include <linux/debugfs.h> 204f36557fSMathieu Desnoyers #include <linux/fsnotify.h> 2166f54963SPeter Oberparleiter #include <linux/string.h> 22d6e48686SLudwig Nussel #include <linux/seq_file.h> 23d6e48686SLudwig Nussel #include <linux/parser.h> 2492562927SMimi Zohar #include <linux/magic.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 269fd4dcecSNicolai Stange 279fd4dcecSNicolai Stange #include "internal.h" 281da177e4SLinus Torvalds 2982aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE 0700 30d6e48686SLudwig Nussel 311da177e4SLinus Torvalds static struct vfsmount *debugfs_mount; 321da177e4SLinus Torvalds static int debugfs_mount_count; 33c0f92ba9SFrederic Weisbecker static bool debugfs_registered; 341da177e4SLinus Torvalds 35edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 361da177e4SLinus Torvalds { 371da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 381da177e4SLinus Torvalds if (inode) { 3985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 401b48b530SDeepa Dinamani inode->i_atime = inode->i_mtime = 41c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds return inode; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds 46d6e48686SLudwig Nussel struct debugfs_mount_opts { 477dc05881SEric W. Biederman kuid_t uid; 487dc05881SEric W. Biederman kgid_t gid; 49d6e48686SLudwig Nussel umode_t mode; 50d6e48686SLudwig Nussel }; 51d6e48686SLudwig Nussel 52d6e48686SLudwig Nussel enum { 53d6e48686SLudwig Nussel Opt_uid, 54d6e48686SLudwig Nussel Opt_gid, 55d6e48686SLudwig Nussel Opt_mode, 56d6e48686SLudwig Nussel Opt_err 57d6e48686SLudwig Nussel }; 58d6e48686SLudwig Nussel 59d6e48686SLudwig Nussel static const match_table_t tokens = { 60d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 61d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 62d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 63d6e48686SLudwig Nussel {Opt_err, NULL} 64d6e48686SLudwig Nussel }; 65d6e48686SLudwig Nussel 66d6e48686SLudwig Nussel struct debugfs_fs_info { 67d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 68d6e48686SLudwig Nussel }; 69d6e48686SLudwig Nussel 70d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 71d6e48686SLudwig Nussel { 72d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 73d6e48686SLudwig Nussel int option; 74d6e48686SLudwig Nussel int token; 757dc05881SEric W. Biederman kuid_t uid; 767dc05881SEric W. Biederman kgid_t gid; 77d6e48686SLudwig Nussel char *p; 78d6e48686SLudwig Nussel 79d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 80d6e48686SLudwig Nussel 81d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 82d6e48686SLudwig Nussel if (!*p) 83d6e48686SLudwig Nussel continue; 84d6e48686SLudwig Nussel 85d6e48686SLudwig Nussel token = match_token(p, tokens, args); 86d6e48686SLudwig Nussel switch (token) { 87d6e48686SLudwig Nussel case Opt_uid: 88d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 89d6e48686SLudwig Nussel return -EINVAL; 907dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 917dc05881SEric W. Biederman if (!uid_valid(uid)) 927dc05881SEric W. Biederman return -EINVAL; 937dc05881SEric W. Biederman opts->uid = uid; 94d6e48686SLudwig Nussel break; 95d6e48686SLudwig Nussel case Opt_gid: 96f1688e04SDave Reisner if (match_int(&args[0], &option)) 97d6e48686SLudwig Nussel return -EINVAL; 987dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 997dc05881SEric W. Biederman if (!gid_valid(gid)) 1007dc05881SEric W. Biederman return -EINVAL; 1017dc05881SEric W. Biederman opts->gid = gid; 102d6e48686SLudwig Nussel break; 103d6e48686SLudwig Nussel case Opt_mode: 104d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 105d6e48686SLudwig Nussel return -EINVAL; 106d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 107d6e48686SLudwig Nussel break; 108d6e48686SLudwig Nussel /* 109d6e48686SLudwig Nussel * We might like to report bad mount options here; 110d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 111d6e48686SLudwig Nussel */ 112d6e48686SLudwig Nussel } 113d6e48686SLudwig Nussel } 114d6e48686SLudwig Nussel 115d6e48686SLudwig Nussel return 0; 116d6e48686SLudwig Nussel } 117d6e48686SLudwig Nussel 118d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 119d6e48686SLudwig Nussel { 120d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1212b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 122d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 123d6e48686SLudwig Nussel 124d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 125d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 126d6e48686SLudwig Nussel 127d6e48686SLudwig Nussel inode->i_uid = opts->uid; 128d6e48686SLudwig Nussel inode->i_gid = opts->gid; 129d6e48686SLudwig Nussel 130d6e48686SLudwig Nussel return 0; 131d6e48686SLudwig Nussel } 132d6e48686SLudwig Nussel 133d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 134d6e48686SLudwig Nussel { 135d6e48686SLudwig Nussel int err; 136d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 137d6e48686SLudwig Nussel 13802b9984dSTheodore Ts'o sync_filesystem(sb); 139d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 140d6e48686SLudwig Nussel if (err) 141d6e48686SLudwig Nussel goto fail; 142d6e48686SLudwig Nussel 143d6e48686SLudwig Nussel debugfs_apply_options(sb); 144d6e48686SLudwig Nussel 145d6e48686SLudwig Nussel fail: 146d6e48686SLudwig Nussel return err; 147d6e48686SLudwig Nussel } 148d6e48686SLudwig Nussel 149d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 150d6e48686SLudwig Nussel { 151d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 152d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 153d6e48686SLudwig Nussel 1547dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1557dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1567dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1577dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1587dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1597dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 160d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 161d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 162d6e48686SLudwig Nussel 163d6e48686SLudwig Nussel return 0; 164d6e48686SLudwig Nussel } 165d6e48686SLudwig Nussel 166*6234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode) 1670db59e59SAl Viro { 1680db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 1695723cb01SAl Viro kfree(inode->i_link); 17093b919daSAl Viro free_inode_nonrcu(inode); 17193b919daSAl Viro } 17293b919daSAl Viro 173d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 174d6e48686SLudwig Nussel .statfs = simple_statfs, 175d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 176d6e48686SLudwig Nussel .show_options = debugfs_show_options, 177*6234ddf4SAl Viro .free_inode = debugfs_free_inode, 178d6e48686SLudwig Nussel }; 179d6e48686SLudwig Nussel 1807c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 1817c8d4698SNicolai Stange { 1827d39bc50SNicolai Stange void *fsd = dentry->d_fsdata; 1837d39bc50SNicolai Stange 1847d39bc50SNicolai Stange if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) 1857c8d4698SNicolai Stange kfree(dentry->d_fsdata); 1867c8d4698SNicolai Stange } 1877c8d4698SNicolai Stange 18877b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 18977b3da6eSAl Viro { 19093faccbbSEric W. Biederman debugfs_automount_t f; 19193faccbbSEric W. Biederman f = (debugfs_automount_t)path->dentry->d_fsdata; 19293faccbbSEric W. Biederman return f(path->dentry, d_inode(path->dentry)->i_private); 19377b3da6eSAl Viro } 19477b3da6eSAl Viro 19577b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 19677b3da6eSAl Viro .d_delete = always_delete_dentry, 1977c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 19877b3da6eSAl Viro .d_automount = debugfs_automount, 19977b3da6eSAl Viro }; 20077b3da6eSAl Viro 2011da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2021da177e4SLinus Torvalds { 203cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 204d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 205d6e48686SLudwig Nussel int err; 2061da177e4SLinus Torvalds 207d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 208d6e48686SLudwig Nussel sb->s_fs_info = fsi; 209d6e48686SLudwig Nussel if (!fsi) { 210d6e48686SLudwig Nussel err = -ENOMEM; 211d6e48686SLudwig Nussel goto fail; 212d6e48686SLudwig Nussel } 213d6e48686SLudwig Nussel 214d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 215d6e48686SLudwig Nussel if (err) 216d6e48686SLudwig Nussel goto fail; 217d6e48686SLudwig Nussel 218d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 219d6e48686SLudwig Nussel if (err) 220d6e48686SLudwig Nussel goto fail; 221d6e48686SLudwig Nussel 222d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 22377b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 224d6e48686SLudwig Nussel 225d6e48686SLudwig Nussel debugfs_apply_options(sb); 226d6e48686SLudwig Nussel 227d6e48686SLudwig Nussel return 0; 228d6e48686SLudwig Nussel 229d6e48686SLudwig Nussel fail: 230d6e48686SLudwig Nussel kfree(fsi); 231d6e48686SLudwig Nussel sb->s_fs_info = NULL; 232d6e48686SLudwig Nussel return err; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 235fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2361da177e4SLinus Torvalds int flags, const char *dev_name, 237fc14f2feSAl Viro void *data) 2381da177e4SLinus Torvalds { 239fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2431da177e4SLinus Torvalds .owner = THIS_MODULE, 2441da177e4SLinus Torvalds .name = "debugfs", 245fc14f2feSAl Viro .mount = debug_mount, 2461da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2471da177e4SLinus Torvalds }; 2487f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2491da177e4SLinus Torvalds 250a7c5437bSOmar Sandoval /** 251a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 252a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 253a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 254a7c5437bSOmar Sandoval * 255a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 256a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 257a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 258a7c5437bSOmar Sandoval * 259a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 260a7c5437bSOmar Sandoval * returned. 261a7c5437bSOmar Sandoval */ 262a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 263a7c5437bSOmar Sandoval { 264a7c5437bSOmar Sandoval struct dentry *dentry; 265a7c5437bSOmar Sandoval 266a7c5437bSOmar Sandoval if (IS_ERR(parent)) 267a7c5437bSOmar Sandoval return NULL; 268a7c5437bSOmar Sandoval 269a7c5437bSOmar Sandoval if (!parent) 270a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 271a7c5437bSOmar Sandoval 272cd1c0c93SAl Viro dentry = lookup_one_len_unlocked(name, parent, strlen(name)); 273a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 274a7c5437bSOmar Sandoval return NULL; 275a7c5437bSOmar Sandoval if (!d_really_is_positive(dentry)) { 276a7c5437bSOmar Sandoval dput(dentry); 277a7c5437bSOmar Sandoval return NULL; 278a7c5437bSOmar Sandoval } 279a7c5437bSOmar Sandoval return dentry; 280a7c5437bSOmar Sandoval } 281a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 282a7c5437bSOmar Sandoval 283190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 284c3b1a350SAl Viro { 285190afd81SAl Viro struct dentry *dentry; 286c3b1a350SAl Viro int error; 287c3b1a350SAl Viro 288c3b1a350SAl Viro pr_debug("debugfs: creating file '%s'\n",name); 289c3b1a350SAl Viro 290c9e15f25SGreg KH if (IS_ERR(parent)) 291c9e15f25SGreg KH return parent; 292c9e15f25SGreg KH 293c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 294c3b1a350SAl Viro &debugfs_mount_count); 295c3b1a350SAl Viro if (error) 296190afd81SAl Viro return ERR_PTR(error); 297c3b1a350SAl Viro 298cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 299cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 300cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 301cfa57c11SAl Viro * have around. 302cfa57c11SAl Viro */ 303cfa57c11SAl Viro if (!parent) 304cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 305cfa57c11SAl Viro 3065955102cSAl Viro inode_lock(d_inode(parent)); 307cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3082b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 309190afd81SAl Viro dput(dentry); 310190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 311190afd81SAl Viro } 3120ee9608cSDaniel Borkmann 3130ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3145955102cSAl Viro inode_unlock(d_inode(parent)); 3150ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3160ee9608cSDaniel Borkmann } 3170ee9608cSDaniel Borkmann 318190afd81SAl Viro return dentry; 319190afd81SAl Viro } 320190afd81SAl Viro 3215233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 322190afd81SAl Viro { 3235955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 324190afd81SAl Viro dput(dentry); 325190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 326ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 327190afd81SAl Viro } 3285233e311SAl Viro 3295233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3305233e311SAl Viro { 3315955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 332190afd81SAl Viro return dentry; 333190afd81SAl Viro } 334190afd81SAl Viro 33549d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 33649d200deSNicolai Stange struct dentry *parent, void *data, 33749d200deSNicolai Stange const struct file_operations *proxy_fops, 33849d200deSNicolai Stange const struct file_operations *real_fops) 33949d200deSNicolai Stange { 34049d200deSNicolai Stange struct dentry *dentry; 34149d200deSNicolai Stange struct inode *inode; 34249d200deSNicolai Stange 34349d200deSNicolai Stange if (!(mode & S_IFMT)) 34449d200deSNicolai Stange mode |= S_IFREG; 34549d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 34649d200deSNicolai Stange dentry = start_creating(name, parent); 34749d200deSNicolai Stange 3487d39bc50SNicolai Stange if (IS_ERR(dentry)) 349ff9fb72bSGreg Kroah-Hartman return dentry; 35049d200deSNicolai Stange 35149d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 3527d39bc50SNicolai Stange if (unlikely(!inode)) 35349d200deSNicolai Stange return failed_creating(dentry); 35449d200deSNicolai Stange 35549d200deSNicolai Stange inode->i_mode = mode; 35649d200deSNicolai Stange inode->i_private = data; 35749d200deSNicolai Stange 35849d200deSNicolai Stange inode->i_fop = proxy_fops; 3597d39bc50SNicolai Stange dentry->d_fsdata = (void *)((unsigned long)real_fops | 3607d39bc50SNicolai Stange DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 36149d200deSNicolai Stange 36249d200deSNicolai Stange d_instantiate(dentry, inode); 36349d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 36449d200deSNicolai Stange return end_creating(dentry); 36549d200deSNicolai Stange } 36649d200deSNicolai Stange 3671da177e4SLinus Torvalds /** 3681da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 3691da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 370be030e65SAlberto Bertogli * @mode: the permission that the file should have. 3711da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 372e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3731da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 3741da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 3758e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 3761da177e4SLinus Torvalds * the open() call. 3771da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 3781da177e4SLinus Torvalds * this file. 3791da177e4SLinus Torvalds * 3801da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 381be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 382be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 3831da177e4SLinus Torvalds * recommended to be used instead.) 3841da177e4SLinus Torvalds * 3851da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 3861da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 3871da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 388ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 389ff9fb72bSGreg Kroah-Hartman * returned. 3901da177e4SLinus Torvalds * 3916468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 392873760fbSCornelia Huck * returned. 3931da177e4SLinus Torvalds */ 394f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 3951da177e4SLinus Torvalds struct dentry *parent, void *data, 39699ac48f5SArjan van de Ven const struct file_operations *fops) 3971da177e4SLinus Torvalds { 398c3b1a350SAl Viro 39949d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 40049d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 40149d200deSNicolai Stange &debugfs_noop_file_operations, 40249d200deSNicolai Stange fops); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4051da177e4SLinus Torvalds 406c6468808SNicolai Stange /** 407c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 408c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 409c6468808SNicolai Stange * @mode: the permission that the file should have. 410c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 411c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 412c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 413c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 414c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 415c6468808SNicolai Stange * the open() call. 416c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 417c6468808SNicolai Stange * this file. 418c6468808SNicolai Stange * 419c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 420c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 421c6468808SNicolai Stange * handed it will not get protected against file removals by the 422c6468808SNicolai Stange * debugfs core. 423c6468808SNicolai Stange * 424c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 4250eeb2731SSergey Senozhatsky * methods against file removals by means of debugfs_file_get() 4260eeb2731SSergey Senozhatsky * and debugfs_file_put(). ->open() is still protected by 427c6468808SNicolai Stange * debugfs though. 428c6468808SNicolai Stange * 429c6468808SNicolai Stange * Any struct file_operations defined by means of 430c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 431c6468808SNicolai Stange * thus, may be used here. 432c6468808SNicolai Stange */ 43349d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 43449d200deSNicolai Stange struct dentry *parent, void *data, 43549d200deSNicolai Stange const struct file_operations *fops) 43649d200deSNicolai Stange { 43749d200deSNicolai Stange 43849d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 43949d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 44049d200deSNicolai Stange &debugfs_noop_file_operations, 44149d200deSNicolai Stange fops); 44249d200deSNicolai Stange } 443c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 44449d200deSNicolai Stange 4451da177e4SLinus Torvalds /** 446e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 447e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 448e59b4e91SDavid Howells * @mode: the permission that the file should have. 449e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 450e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 451e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 452e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 453e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 454e59b4e91SDavid Howells * the open() call. 455e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 456e59b4e91SDavid Howells * this file. 457e59b4e91SDavid Howells * @file_size: initial file size 458e59b4e91SDavid Howells * 459e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 460e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 461e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 462e59b4e91SDavid Howells * recommended to be used instead.) 463e59b4e91SDavid Howells * 464e59b4e91SDavid Howells * This function will return a pointer to a dentry if it succeeds. This 465e59b4e91SDavid Howells * pointer must be passed to the debugfs_remove() function when the file is 466e59b4e91SDavid Howells * to be removed (no automatic cleanup happens if your module is unloaded, 467ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 468ff9fb72bSGreg Kroah-Hartman * returned. 469e59b4e91SDavid Howells * 470e59b4e91SDavid Howells * If debugfs is not enabled in the kernel, the value -%ENODEV will be 471e59b4e91SDavid Howells * returned. 472e59b4e91SDavid Howells */ 473e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode, 474e59b4e91SDavid Howells struct dentry *parent, void *data, 475e59b4e91SDavid Howells const struct file_operations *fops, 476e59b4e91SDavid Howells loff_t file_size) 477e59b4e91SDavid Howells { 478e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 479e59b4e91SDavid Howells 480e59b4e91SDavid Howells if (de) 4812b0143b5SDavid Howells d_inode(de)->i_size = file_size; 482e59b4e91SDavid Howells return de; 483e59b4e91SDavid Howells } 484e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 485e59b4e91SDavid Howells 486e59b4e91SDavid Howells /** 4871da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 4881da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 4891da177e4SLinus Torvalds * create. 4901da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 491e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4921da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 4931da177e4SLinus Torvalds * 4941da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 4951da177e4SLinus Torvalds * 4961da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4971da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4981da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 499ff9fb72bSGreg Kroah-Hartman * you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) will be 500ff9fb72bSGreg Kroah-Hartman * returned. 5011da177e4SLinus Torvalds * 5026468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 503873760fbSCornelia Huck * returned. 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5061da177e4SLinus Torvalds { 507ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 508680b3024SAl Viro struct inode *inode; 509ad5abd5bSAl Viro 510ad5abd5bSAl Viro if (IS_ERR(dentry)) 511ff9fb72bSGreg Kroah-Hartman return dentry; 512ad5abd5bSAl Viro 513edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 514680b3024SAl Viro if (unlikely(!inode)) 5155233e311SAl Viro return failed_creating(dentry); 516680b3024SAl Viro 517f5b7769eSLinus Torvalds inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 518edac65eaSAl Viro inode->i_op = &simple_dir_inode_operations; 519edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 520edac65eaSAl Viro 521edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 522edac65eaSAl Viro inc_nlink(inode); 523680b3024SAl Viro d_instantiate(dentry, inode); 5242b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 5252b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 5265233e311SAl Viro return end_creating(dentry); 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /** 53177b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 53277b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 53377b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 53477b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 53577b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 53677b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 53777b3da6eSAl Viro * @data: opaque argument to pass to f(). 53877b3da6eSAl Viro * 53977b3da6eSAl Viro * @f should return what ->d_automount() would. 54077b3da6eSAl Viro */ 54177b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 54277b3da6eSAl Viro struct dentry *parent, 54393faccbbSEric W. Biederman debugfs_automount_t f, 54477b3da6eSAl Viro void *data) 54577b3da6eSAl Viro { 54677b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 54777b3da6eSAl Viro struct inode *inode; 54877b3da6eSAl Viro 54977b3da6eSAl Viro if (IS_ERR(dentry)) 550ff9fb72bSGreg Kroah-Hartman return dentry; 55177b3da6eSAl Viro 55277b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 55377b3da6eSAl Viro if (unlikely(!inode)) 55477b3da6eSAl Viro return failed_creating(dentry); 55577b3da6eSAl Viro 55687243debSSeth Forshee make_empty_dir_inode(inode); 55777b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 55877b3da6eSAl Viro inode->i_private = data; 55977b3da6eSAl Viro dentry->d_fsdata = (void *)f; 560a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 561a8f324a4SRoman Pen inc_nlink(inode); 56277b3da6eSAl Viro d_instantiate(dentry, inode); 563a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 564a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 56577b3da6eSAl Viro return end_creating(dentry); 56677b3da6eSAl Viro } 56777b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 56877b3da6eSAl Viro 56977b3da6eSAl Viro /** 57066f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 57166f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 57266f54963SPeter Oberparleiter * create. 57366f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 574e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 57566f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 57666f54963SPeter Oberparleiter * filesystem. 57766f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 57866f54963SPeter Oberparleiter * symbolic link. 57966f54963SPeter Oberparleiter * 58066f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 58166f54963SPeter Oberparleiter * links to the given target path. 58266f54963SPeter Oberparleiter * 58366f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 58466f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 58566f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 586ff9fb72bSGreg Kroah-Hartman * unloaded, you are responsible here.) If an error occurs, %ERR_PTR(-ERROR) 587ff9fb72bSGreg Kroah-Hartman * will be returned. 58866f54963SPeter Oberparleiter * 58966f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 590873760fbSCornelia Huck * returned. 59166f54963SPeter Oberparleiter */ 59266f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 59366f54963SPeter Oberparleiter const char *target) 59466f54963SPeter Oberparleiter { 595ad5abd5bSAl Viro struct dentry *dentry; 596680b3024SAl Viro struct inode *inode; 597680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 59866f54963SPeter Oberparleiter if (!link) 599ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 60066f54963SPeter Oberparleiter 601ad5abd5bSAl Viro dentry = start_creating(name, parent); 602ad5abd5bSAl Viro if (IS_ERR(dentry)) { 60366f54963SPeter Oberparleiter kfree(link); 604ff9fb72bSGreg Kroah-Hartman return dentry; 605ad5abd5bSAl Viro } 606ad5abd5bSAl Viro 607edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 608680b3024SAl Viro if (unlikely(!inode)) { 609ad5abd5bSAl Viro kfree(link); 6105233e311SAl Viro return failed_creating(dentry); 611680b3024SAl Viro } 612edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 6135723cb01SAl Viro inode->i_op = &simple_symlink_inode_operations; 6145723cb01SAl Viro inode->i_link = link; 615680b3024SAl Viro d_instantiate(dentry, inode); 6165233e311SAl Viro return end_creating(dentry); 61766f54963SPeter Oberparleiter } 61866f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 61966f54963SPeter Oberparleiter 620e9117a5aSNicolai Stange static void __debugfs_remove_file(struct dentry *dentry, struct dentry *parent) 621e9117a5aSNicolai Stange { 622e9117a5aSNicolai Stange struct debugfs_fsdata *fsd; 623e9117a5aSNicolai Stange 624e9117a5aSNicolai Stange simple_unlink(d_inode(parent), dentry); 625e9117a5aSNicolai Stange d_delete(dentry); 6267d39bc50SNicolai Stange 6277d39bc50SNicolai Stange /* 6287d39bc50SNicolai Stange * Paired with the closing smp_mb() implied by a successful 6297d39bc50SNicolai Stange * cmpxchg() in debugfs_file_get(): either 6307d39bc50SNicolai Stange * debugfs_file_get() must see a dead dentry or we must see a 6317d39bc50SNicolai Stange * debugfs_fsdata instance at ->d_fsdata here (or both). 6327d39bc50SNicolai Stange */ 6337d39bc50SNicolai Stange smp_mb(); 6347d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 6357d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) 6367d39bc50SNicolai Stange return; 637e9117a5aSNicolai Stange if (!refcount_dec_and_test(&fsd->active_users)) 638e9117a5aSNicolai Stange wait_for_completion(&fsd->active_users_drained); 639e9117a5aSNicolai Stange } 640e9117a5aSNicolai Stange 64125d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 6421da177e4SLinus Torvalds { 64365c33336SMathieu Desnoyers int ret = 0; 6441da177e4SLinus Torvalds 645dc3f4198SAl Viro if (simple_positive(dentry)) { 64629a7f3adSMathieu Desnoyers dget(dentry); 647e9117a5aSNicolai Stange if (!d_is_reg(dentry)) { 6487ceab50cSDavid Howells if (d_is_dir(dentry)) 6492b0143b5SDavid Howells ret = simple_rmdir(d_inode(parent), dentry); 6500db59e59SAl Viro else 6512b0143b5SDavid Howells simple_unlink(d_inode(parent), dentry); 65229a7f3adSMathieu Desnoyers if (!ret) 65329a7f3adSMathieu Desnoyers d_delete(dentry); 654e9117a5aSNicolai Stange } else { 655e9117a5aSNicolai Stange __debugfs_remove_file(dentry, parent); 656e9117a5aSNicolai Stange } 65729a7f3adSMathieu Desnoyers dput(dentry); 6581da177e4SLinus Torvalds } 65925d41d84SJan Kara return ret; 6609505e637SHaavard Skinnemoen } 6619505e637SHaavard Skinnemoen 6629505e637SHaavard Skinnemoen /** 6639505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 6649505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 665398dc4adSUlf Magnusson * removed. If this parameter is NULL or an error value, nothing 666398dc4adSUlf Magnusson * will be done. 6679505e637SHaavard Skinnemoen * 6689505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 6699505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 6709505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 6719505e637SHaavard Skinnemoen * 6729505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 6739505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 6749505e637SHaavard Skinnemoen * removed, you are responsible here. 6759505e637SHaavard Skinnemoen */ 6769505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 6779505e637SHaavard Skinnemoen { 6789505e637SHaavard Skinnemoen struct dentry *parent; 67925d41d84SJan Kara int ret; 6809505e637SHaavard Skinnemoen 681a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 6829505e637SHaavard Skinnemoen return; 6839505e637SHaavard Skinnemoen 6849505e637SHaavard Skinnemoen parent = dentry->d_parent; 6855955102cSAl Viro inode_lock(d_inode(parent)); 68625d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 6875955102cSAl Viro inode_unlock(d_inode(parent)); 68825d41d84SJan Kara if (!ret) 6891da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 6921da177e4SLinus Torvalds 693cfc94cdfSJan Kara /** 6949505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 695398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 696398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 6979505e637SHaavard Skinnemoen * 6989505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 6999505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 7009505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 7019505e637SHaavard Skinnemoen * 7029505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 7039505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 7049505e637SHaavard Skinnemoen * removed, you are responsible here. 7059505e637SHaavard Skinnemoen */ 7069505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 7079505e637SHaavard Skinnemoen { 708485d4402SSteven Rostedt struct dentry *child, *parent; 7099505e637SHaavard Skinnemoen 710a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7119505e637SHaavard Skinnemoen return; 7129505e637SHaavard Skinnemoen 7139505e637SHaavard Skinnemoen parent = dentry; 714776164c1SOleg Nesterov down: 7155955102cSAl Viro inode_lock(d_inode(parent)); 716485d4402SSteven Rostedt loop: 717485d4402SSteven Rostedt /* 718485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 719485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 720485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 721485d4402SSteven Rostedt */ 722485d4402SSteven Rostedt spin_lock(&parent->d_lock); 723946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 724dc3f4198SAl Viro if (!simple_positive(child)) 725776164c1SOleg Nesterov continue; 7269505e637SHaavard Skinnemoen 727776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 7289505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 729485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 7305955102cSAl Viro inode_unlock(d_inode(parent)); 7319505e637SHaavard Skinnemoen parent = child; 732776164c1SOleg Nesterov goto down; 7339505e637SHaavard Skinnemoen } 734485d4402SSteven Rostedt 735485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 736485d4402SSteven Rostedt 737776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 738776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 739485d4402SSteven Rostedt 740485d4402SSteven Rostedt /* 741485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 742485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 743485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 744485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 745dc3f4198SAl Viro * simple_positive() check. 746485d4402SSteven Rostedt */ 747485d4402SSteven Rostedt goto loop; 748776164c1SOleg Nesterov } 749485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 750776164c1SOleg Nesterov 7515955102cSAl Viro inode_unlock(d_inode(parent)); 752776164c1SOleg Nesterov child = parent; 753776164c1SOleg Nesterov parent = parent->d_parent; 7545955102cSAl Viro inode_lock(d_inode(parent)); 755776164c1SOleg Nesterov 756485d4402SSteven Rostedt if (child != dentry) 757485d4402SSteven Rostedt /* go up */ 758485d4402SSteven Rostedt goto loop; 75956a83cc9SSteven Rostedt 760776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 7619505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7625955102cSAl Viro inode_unlock(d_inode(parent)); 7639505e637SHaavard Skinnemoen } 7649505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 7659505e637SHaavard Skinnemoen 7669505e637SHaavard Skinnemoen /** 767cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 768cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 769cfc94cdfSJan Kara * should be a directory dentry. 770cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 771cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 772cfc94cdfSJan Kara * moved. This should be a directory dentry. 773cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 774cfc94cdfSJan Kara * 775cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 776cfc94cdfSJan Kara * exist for rename to succeed. 777cfc94cdfSJan Kara * 778cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 779cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 780cfc94cdfSJan Kara * returned. 781cfc94cdfSJan Kara * 782cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 783cfc94cdfSJan Kara * returned. 784cfc94cdfSJan Kara */ 785cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 786cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 787cfc94cdfSJan Kara { 788cfc94cdfSJan Kara int error; 789cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 79049d31c2fSAl Viro struct name_snapshot old_name; 791cfc94cdfSJan Kara 792d88c93f0SGreg Kroah-Hartman if (IS_ERR(old_dir)) 793d88c93f0SGreg Kroah-Hartman return old_dir; 794d88c93f0SGreg Kroah-Hartman if (IS_ERR(new_dir)) 795d88c93f0SGreg Kroah-Hartman return new_dir; 796d88c93f0SGreg Kroah-Hartman if (IS_ERR_OR_NULL(old_dentry)) 797d88c93f0SGreg Kroah-Hartman return old_dentry; 798d88c93f0SGreg Kroah-Hartman 799cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 800cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 8012b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 802cfc94cdfSJan Kara goto exit; 803cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 8042b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 805cfc94cdfSJan Kara d_mountpoint(old_dentry)) 806cfc94cdfSJan Kara goto exit; 807cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 808cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 8092b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 810cfc94cdfSJan Kara goto exit; 811cfc94cdfSJan Kara 81249d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 813cfc94cdfSJan Kara 8142b0143b5SDavid Howells error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 815e0e0be8aSMiklos Szeredi dentry, 0); 816cfc94cdfSJan Kara if (error) { 81749d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 818cfc94cdfSJan Kara goto exit; 819cfc94cdfSJan Kara } 820cfc94cdfSJan Kara d_move(old_dentry, dentry); 82149d31c2fSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name.name, 822e36cb0b8SDavid Howells d_is_dir(old_dentry), 8235a190ae6SAl Viro NULL, old_dentry); 82449d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 825cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 826cfc94cdfSJan Kara dput(dentry); 827cfc94cdfSJan Kara return old_dentry; 828cfc94cdfSJan Kara exit: 829cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 830cfc94cdfSJan Kara dput(dentry); 831cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 832ff9fb72bSGreg Kroah-Hartman if (IS_ERR(dentry)) 833ff9fb72bSGreg Kroah-Hartman return dentry; 834ff9fb72bSGreg Kroah-Hartman return ERR_PTR(-EINVAL); 835cfc94cdfSJan Kara } 836cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 837cfc94cdfSJan Kara 838c0f92ba9SFrederic Weisbecker /** 839c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 840c0f92ba9SFrederic Weisbecker */ 841c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 842c0f92ba9SFrederic Weisbecker { 843c0f92ba9SFrederic Weisbecker return debugfs_registered; 844c0f92ba9SFrederic Weisbecker } 845c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 846c0f92ba9SFrederic Weisbecker 8471da177e4SLinus Torvalds static int __init debugfs_init(void) 8481da177e4SLinus Torvalds { 8491da177e4SLinus Torvalds int retval; 8501da177e4SLinus Torvalds 851f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 852f9bb4882SEric W. Biederman if (retval) 853f9bb4882SEric W. Biederman return retval; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 8561da177e4SLinus Torvalds if (retval) 857f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 858c0f92ba9SFrederic Weisbecker else 859c0f92ba9SFrederic Weisbecker debugfs_registered = true; 860c0f92ba9SFrederic Weisbecker 8611da177e4SLinus Torvalds return retval; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds core_initcall(debugfs_init); 8641da177e4SLinus Torvalds 865