11da177e4SLinus Torvalds /* 2bd33d12fSHarry Wei * inode.c - part of debugfs, a tiny little debug file system 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 51da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License version 91da177e4SLinus Torvalds * 2 as published by the Free Software Foundation. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 12e1511a84SMauro Carvalho Chehab * See ./Documentation/core-api/kernel-api.rst for more details. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/fs.h> 181da177e4SLinus Torvalds #include <linux/mount.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/init.h> 214d8ebddcSRandy Dunlap #include <linux/kobject.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/debugfs.h> 244f36557fSMathieu Desnoyers #include <linux/fsnotify.h> 2566f54963SPeter Oberparleiter #include <linux/string.h> 26d6e48686SLudwig Nussel #include <linux/seq_file.h> 27d6e48686SLudwig Nussel #include <linux/parser.h> 2892562927SMimi Zohar #include <linux/magic.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 309fd4dcecSNicolai Stange #include <linux/srcu.h> 319fd4dcecSNicolai Stange 329fd4dcecSNicolai Stange #include "internal.h" 331da177e4SLinus Torvalds 3482aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE 0700 35d6e48686SLudwig Nussel 369fd4dcecSNicolai Stange DEFINE_SRCU(debugfs_srcu); 379fd4dcecSNicolai Stange 381da177e4SLinus Torvalds static struct vfsmount *debugfs_mount; 391da177e4SLinus Torvalds static int debugfs_mount_count; 40c0f92ba9SFrederic Weisbecker static bool debugfs_registered; 411da177e4SLinus Torvalds 42edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 451da177e4SLinus Torvalds if (inode) { 4685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 471b48b530SDeepa Dinamani inode->i_atime = inode->i_mtime = 48c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 491da177e4SLinus Torvalds } 501da177e4SLinus Torvalds return inode; 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds 53d6e48686SLudwig Nussel struct debugfs_mount_opts { 547dc05881SEric W. Biederman kuid_t uid; 557dc05881SEric W. Biederman kgid_t gid; 56d6e48686SLudwig Nussel umode_t mode; 57d6e48686SLudwig Nussel }; 58d6e48686SLudwig Nussel 59d6e48686SLudwig Nussel enum { 60d6e48686SLudwig Nussel Opt_uid, 61d6e48686SLudwig Nussel Opt_gid, 62d6e48686SLudwig Nussel Opt_mode, 63d6e48686SLudwig Nussel Opt_err 64d6e48686SLudwig Nussel }; 65d6e48686SLudwig Nussel 66d6e48686SLudwig Nussel static const match_table_t tokens = { 67d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 68d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 69d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 70d6e48686SLudwig Nussel {Opt_err, NULL} 71d6e48686SLudwig Nussel }; 72d6e48686SLudwig Nussel 73d6e48686SLudwig Nussel struct debugfs_fs_info { 74d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 75d6e48686SLudwig Nussel }; 76d6e48686SLudwig Nussel 77d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 78d6e48686SLudwig Nussel { 79d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 80d6e48686SLudwig Nussel int option; 81d6e48686SLudwig Nussel int token; 827dc05881SEric W. Biederman kuid_t uid; 837dc05881SEric W. Biederman kgid_t gid; 84d6e48686SLudwig Nussel char *p; 85d6e48686SLudwig Nussel 86d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 87d6e48686SLudwig Nussel 88d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 89d6e48686SLudwig Nussel if (!*p) 90d6e48686SLudwig Nussel continue; 91d6e48686SLudwig Nussel 92d6e48686SLudwig Nussel token = match_token(p, tokens, args); 93d6e48686SLudwig Nussel switch (token) { 94d6e48686SLudwig Nussel case Opt_uid: 95d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 96d6e48686SLudwig Nussel return -EINVAL; 977dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 987dc05881SEric W. Biederman if (!uid_valid(uid)) 997dc05881SEric W. Biederman return -EINVAL; 1007dc05881SEric W. Biederman opts->uid = uid; 101d6e48686SLudwig Nussel break; 102d6e48686SLudwig Nussel case Opt_gid: 103f1688e04SDave Reisner if (match_int(&args[0], &option)) 104d6e48686SLudwig Nussel return -EINVAL; 1057dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1067dc05881SEric W. Biederman if (!gid_valid(gid)) 1077dc05881SEric W. Biederman return -EINVAL; 1087dc05881SEric W. Biederman opts->gid = gid; 109d6e48686SLudwig Nussel break; 110d6e48686SLudwig Nussel case Opt_mode: 111d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 112d6e48686SLudwig Nussel return -EINVAL; 113d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 114d6e48686SLudwig Nussel break; 115d6e48686SLudwig Nussel /* 116d6e48686SLudwig Nussel * We might like to report bad mount options here; 117d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 118d6e48686SLudwig Nussel */ 119d6e48686SLudwig Nussel } 120d6e48686SLudwig Nussel } 121d6e48686SLudwig Nussel 122d6e48686SLudwig Nussel return 0; 123d6e48686SLudwig Nussel } 124d6e48686SLudwig Nussel 125d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 126d6e48686SLudwig Nussel { 127d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1282b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 129d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 130d6e48686SLudwig Nussel 131d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 132d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 133d6e48686SLudwig Nussel 134d6e48686SLudwig Nussel inode->i_uid = opts->uid; 135d6e48686SLudwig Nussel inode->i_gid = opts->gid; 136d6e48686SLudwig Nussel 137d6e48686SLudwig Nussel return 0; 138d6e48686SLudwig Nussel } 139d6e48686SLudwig Nussel 140d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 141d6e48686SLudwig Nussel { 142d6e48686SLudwig Nussel int err; 143d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 144d6e48686SLudwig Nussel 14502b9984dSTheodore Ts'o sync_filesystem(sb); 146d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 147d6e48686SLudwig Nussel if (err) 148d6e48686SLudwig Nussel goto fail; 149d6e48686SLudwig Nussel 150d6e48686SLudwig Nussel debugfs_apply_options(sb); 151d6e48686SLudwig Nussel 152d6e48686SLudwig Nussel fail: 153d6e48686SLudwig Nussel return err; 154d6e48686SLudwig Nussel } 155d6e48686SLudwig Nussel 156d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 157d6e48686SLudwig Nussel { 158d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 159d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 160d6e48686SLudwig Nussel 1617dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1627dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1637dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1647dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1657dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1667dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 167d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 168d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 169d6e48686SLudwig Nussel 170d6e48686SLudwig Nussel return 0; 171d6e48686SLudwig Nussel } 172d6e48686SLudwig Nussel 1730db59e59SAl Viro static void debugfs_evict_inode(struct inode *inode) 1740db59e59SAl Viro { 1750db59e59SAl Viro truncate_inode_pages_final(&inode->i_data); 1760db59e59SAl Viro clear_inode(inode); 1770db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 1785723cb01SAl Viro kfree(inode->i_link); 1790db59e59SAl Viro } 1800db59e59SAl Viro 181d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 182d6e48686SLudwig Nussel .statfs = simple_statfs, 183d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 184d6e48686SLudwig Nussel .show_options = debugfs_show_options, 1850db59e59SAl Viro .evict_inode = debugfs_evict_inode, 186d6e48686SLudwig Nussel }; 187d6e48686SLudwig Nussel 188*7c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry) 189*7c8d4698SNicolai Stange { 190*7c8d4698SNicolai Stange kfree(dentry->d_fsdata); 191*7c8d4698SNicolai Stange } 192*7c8d4698SNicolai Stange 19377b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 19477b3da6eSAl Viro { 19593faccbbSEric W. Biederman debugfs_automount_t f; 19693faccbbSEric W. Biederman f = (debugfs_automount_t)path->dentry->d_fsdata; 19793faccbbSEric W. Biederman return f(path->dentry, d_inode(path->dentry)->i_private); 19877b3da6eSAl Viro } 19977b3da6eSAl Viro 20077b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 20177b3da6eSAl Viro .d_delete = always_delete_dentry, 202*7c8d4698SNicolai Stange .d_release = debugfs_release_dentry, 20377b3da6eSAl Viro .d_automount = debugfs_automount, 20477b3da6eSAl Viro }; 20577b3da6eSAl Viro 2061da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2071da177e4SLinus Torvalds { 208cda37124SEric Biggers static const struct tree_descr debug_files[] = {{""}}; 209d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 210d6e48686SLudwig Nussel int err; 2111da177e4SLinus Torvalds 212d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 213d6e48686SLudwig Nussel sb->s_fs_info = fsi; 214d6e48686SLudwig Nussel if (!fsi) { 215d6e48686SLudwig Nussel err = -ENOMEM; 216d6e48686SLudwig Nussel goto fail; 217d6e48686SLudwig Nussel } 218d6e48686SLudwig Nussel 219d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 220d6e48686SLudwig Nussel if (err) 221d6e48686SLudwig Nussel goto fail; 222d6e48686SLudwig Nussel 223d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 224d6e48686SLudwig Nussel if (err) 225d6e48686SLudwig Nussel goto fail; 226d6e48686SLudwig Nussel 227d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 22877b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 229d6e48686SLudwig Nussel 230d6e48686SLudwig Nussel debugfs_apply_options(sb); 231d6e48686SLudwig Nussel 232d6e48686SLudwig Nussel return 0; 233d6e48686SLudwig Nussel 234d6e48686SLudwig Nussel fail: 235d6e48686SLudwig Nussel kfree(fsi); 236d6e48686SLudwig Nussel sb->s_fs_info = NULL; 237d6e48686SLudwig Nussel return err; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2411da177e4SLinus Torvalds int flags, const char *dev_name, 242fc14f2feSAl Viro void *data) 2431da177e4SLinus Torvalds { 244fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2481da177e4SLinus Torvalds .owner = THIS_MODULE, 2491da177e4SLinus Torvalds .name = "debugfs", 250fc14f2feSAl Viro .mount = debug_mount, 2511da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2521da177e4SLinus Torvalds }; 2537f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2541da177e4SLinus Torvalds 255a7c5437bSOmar Sandoval /** 256a7c5437bSOmar Sandoval * debugfs_lookup() - look up an existing debugfs file 257a7c5437bSOmar Sandoval * @name: a pointer to a string containing the name of the file to look up. 258a7c5437bSOmar Sandoval * @parent: a pointer to the parent dentry of the file. 259a7c5437bSOmar Sandoval * 260a7c5437bSOmar Sandoval * This function will return a pointer to a dentry if it succeeds. If the file 261a7c5437bSOmar Sandoval * doesn't exist or an error occurs, %NULL will be returned. The returned 262a7c5437bSOmar Sandoval * dentry must be passed to dput() when it is no longer needed. 263a7c5437bSOmar Sandoval * 264a7c5437bSOmar Sandoval * If debugfs is not enabled in the kernel, the value -%ENODEV will be 265a7c5437bSOmar Sandoval * returned. 266a7c5437bSOmar Sandoval */ 267a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent) 268a7c5437bSOmar Sandoval { 269a7c5437bSOmar Sandoval struct dentry *dentry; 270a7c5437bSOmar Sandoval 271a7c5437bSOmar Sandoval if (IS_ERR(parent)) 272a7c5437bSOmar Sandoval return NULL; 273a7c5437bSOmar Sandoval 274a7c5437bSOmar Sandoval if (!parent) 275a7c5437bSOmar Sandoval parent = debugfs_mount->mnt_root; 276a7c5437bSOmar Sandoval 277a7c5437bSOmar Sandoval inode_lock(d_inode(parent)); 278a7c5437bSOmar Sandoval dentry = lookup_one_len(name, parent, strlen(name)); 279a7c5437bSOmar Sandoval inode_unlock(d_inode(parent)); 280a7c5437bSOmar Sandoval 281a7c5437bSOmar Sandoval if (IS_ERR(dentry)) 282a7c5437bSOmar Sandoval return NULL; 283a7c5437bSOmar Sandoval if (!d_really_is_positive(dentry)) { 284a7c5437bSOmar Sandoval dput(dentry); 285a7c5437bSOmar Sandoval return NULL; 286a7c5437bSOmar Sandoval } 287a7c5437bSOmar Sandoval return dentry; 288a7c5437bSOmar Sandoval } 289a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup); 290a7c5437bSOmar Sandoval 291190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 292c3b1a350SAl Viro { 293190afd81SAl Viro struct dentry *dentry; 294c3b1a350SAl Viro int error; 295c3b1a350SAl Viro 296c3b1a350SAl Viro pr_debug("debugfs: creating file '%s'\n",name); 297c3b1a350SAl Viro 298c9e15f25SGreg KH if (IS_ERR(parent)) 299c9e15f25SGreg KH return parent; 300c9e15f25SGreg KH 301c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 302c3b1a350SAl Viro &debugfs_mount_count); 303c3b1a350SAl Viro if (error) 304190afd81SAl Viro return ERR_PTR(error); 305c3b1a350SAl Viro 306cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 307cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 308cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 309cfa57c11SAl Viro * have around. 310cfa57c11SAl Viro */ 311cfa57c11SAl Viro if (!parent) 312cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 313cfa57c11SAl Viro 3145955102cSAl Viro inode_lock(d_inode(parent)); 315cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 3162b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 317190afd81SAl Viro dput(dentry); 318190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 319190afd81SAl Viro } 3200ee9608cSDaniel Borkmann 3210ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 3225955102cSAl Viro inode_unlock(d_inode(parent)); 3230ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3240ee9608cSDaniel Borkmann } 3250ee9608cSDaniel Borkmann 326190afd81SAl Viro return dentry; 327190afd81SAl Viro } 328190afd81SAl Viro 3295233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 330190afd81SAl Viro { 3315955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 332190afd81SAl Viro dput(dentry); 333190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 3345233e311SAl Viro return NULL; 335190afd81SAl Viro } 3365233e311SAl Viro 3375233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 3385233e311SAl Viro { 3395955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 340190afd81SAl Viro return dentry; 341190afd81SAl Viro } 342190afd81SAl Viro 34349d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode, 34449d200deSNicolai Stange struct dentry *parent, void *data, 34549d200deSNicolai Stange const struct file_operations *proxy_fops, 34649d200deSNicolai Stange const struct file_operations *real_fops) 34749d200deSNicolai Stange { 34849d200deSNicolai Stange struct dentry *dentry; 34949d200deSNicolai Stange struct inode *inode; 350*7c8d4698SNicolai Stange struct debugfs_fsdata *fsd; 351*7c8d4698SNicolai Stange 352*7c8d4698SNicolai Stange fsd = kmalloc(sizeof(*fsd), GFP_KERNEL); 353*7c8d4698SNicolai Stange if (!fsd) 354*7c8d4698SNicolai Stange return NULL; 35549d200deSNicolai Stange 35649d200deSNicolai Stange if (!(mode & S_IFMT)) 35749d200deSNicolai Stange mode |= S_IFREG; 35849d200deSNicolai Stange BUG_ON(!S_ISREG(mode)); 35949d200deSNicolai Stange dentry = start_creating(name, parent); 36049d200deSNicolai Stange 361*7c8d4698SNicolai Stange if (IS_ERR(dentry)) { 362*7c8d4698SNicolai Stange kfree(fsd); 36349d200deSNicolai Stange return NULL; 364*7c8d4698SNicolai Stange } 36549d200deSNicolai Stange 36649d200deSNicolai Stange inode = debugfs_get_inode(dentry->d_sb); 367*7c8d4698SNicolai Stange if (unlikely(!inode)) { 368*7c8d4698SNicolai Stange kfree(fsd); 36949d200deSNicolai Stange return failed_creating(dentry); 370*7c8d4698SNicolai Stange } 37149d200deSNicolai Stange 37249d200deSNicolai Stange inode->i_mode = mode; 37349d200deSNicolai Stange inode->i_private = data; 37449d200deSNicolai Stange 37549d200deSNicolai Stange inode->i_fop = proxy_fops; 376*7c8d4698SNicolai Stange fsd->real_fops = real_fops; 377*7c8d4698SNicolai Stange dentry->d_fsdata = fsd; 37849d200deSNicolai Stange 37949d200deSNicolai Stange d_instantiate(dentry, inode); 38049d200deSNicolai Stange fsnotify_create(d_inode(dentry->d_parent), dentry); 38149d200deSNicolai Stange return end_creating(dentry); 38249d200deSNicolai Stange } 38349d200deSNicolai Stange 3841da177e4SLinus Torvalds /** 3851da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 3861da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 387be030e65SAlberto Bertogli * @mode: the permission that the file should have. 3881da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 389e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3901da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 3911da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 3928e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 3931da177e4SLinus Torvalds * the open() call. 3941da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 3951da177e4SLinus Torvalds * this file. 3961da177e4SLinus Torvalds * 3971da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 398be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 399be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 4001da177e4SLinus Torvalds * recommended to be used instead.) 4011da177e4SLinus Torvalds * 4021da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4031da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4041da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4056468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4061da177e4SLinus Torvalds * 4076468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 408873760fbSCornelia Huck * returned. 4091da177e4SLinus Torvalds */ 410f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4111da177e4SLinus Torvalds struct dentry *parent, void *data, 41299ac48f5SArjan van de Ven const struct file_operations *fops) 4131da177e4SLinus Torvalds { 414c3b1a350SAl Viro 41549d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 41649d200deSNicolai Stange fops ? &debugfs_full_proxy_file_operations : 41749d200deSNicolai Stange &debugfs_noop_file_operations, 41849d200deSNicolai Stange fops); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4211da177e4SLinus Torvalds 422c6468808SNicolai Stange /** 423c6468808SNicolai Stange * debugfs_create_file_unsafe - create a file in the debugfs filesystem 424c6468808SNicolai Stange * @name: a pointer to a string containing the name of the file to create. 425c6468808SNicolai Stange * @mode: the permission that the file should have. 426c6468808SNicolai Stange * @parent: a pointer to the parent dentry for this file. This should be a 427c6468808SNicolai Stange * directory dentry if set. If this parameter is NULL, then the 428c6468808SNicolai Stange * file will be created in the root of the debugfs filesystem. 429c6468808SNicolai Stange * @data: a pointer to something that the caller will want to get to later 430c6468808SNicolai Stange * on. The inode.i_private pointer will point to this value on 431c6468808SNicolai Stange * the open() call. 432c6468808SNicolai Stange * @fops: a pointer to a struct file_operations that should be used for 433c6468808SNicolai Stange * this file. 434c6468808SNicolai Stange * 435c6468808SNicolai Stange * debugfs_create_file_unsafe() is completely analogous to 436c6468808SNicolai Stange * debugfs_create_file(), the only difference being that the fops 437c6468808SNicolai Stange * handed it will not get protected against file removals by the 438c6468808SNicolai Stange * debugfs core. 439c6468808SNicolai Stange * 440c6468808SNicolai Stange * It is your responsibility to protect your struct file_operation 441c6468808SNicolai Stange * methods against file removals by means of debugfs_use_file_start() 442c6468808SNicolai Stange * and debugfs_use_file_finish(). ->open() is still protected by 443c6468808SNicolai Stange * debugfs though. 444c6468808SNicolai Stange * 445c6468808SNicolai Stange * Any struct file_operations defined by means of 446c6468808SNicolai Stange * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and 447c6468808SNicolai Stange * thus, may be used here. 448c6468808SNicolai Stange */ 44949d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 45049d200deSNicolai Stange struct dentry *parent, void *data, 45149d200deSNicolai Stange const struct file_operations *fops) 45249d200deSNicolai Stange { 45349d200deSNicolai Stange 45449d200deSNicolai Stange return __debugfs_create_file(name, mode, parent, data, 45549d200deSNicolai Stange fops ? &debugfs_open_proxy_file_operations : 45649d200deSNicolai Stange &debugfs_noop_file_operations, 45749d200deSNicolai Stange fops); 45849d200deSNicolai Stange } 459c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe); 46049d200deSNicolai Stange 4611da177e4SLinus Torvalds /** 462e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 463e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 464e59b4e91SDavid Howells * @mode: the permission that the file should have. 465e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 466e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 467e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 468e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 469e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 470e59b4e91SDavid Howells * the open() call. 471e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 472e59b4e91SDavid Howells * this file. 473e59b4e91SDavid Howells * @file_size: initial file size 474e59b4e91SDavid Howells * 475e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 476e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 477e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 478e59b4e91SDavid Howells * recommended to be used instead.) 479e59b4e91SDavid Howells * 480e59b4e91SDavid Howells * This function will return a pointer to a dentry if it succeeds. This 481e59b4e91SDavid Howells * pointer must be passed to the debugfs_remove() function when the file is 482e59b4e91SDavid Howells * to be removed (no automatic cleanup happens if your module is unloaded, 483e59b4e91SDavid Howells * you are responsible here.) If an error occurs, %NULL will be returned. 484e59b4e91SDavid Howells * 485e59b4e91SDavid Howells * If debugfs is not enabled in the kernel, the value -%ENODEV will be 486e59b4e91SDavid Howells * returned. 487e59b4e91SDavid Howells */ 488e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode, 489e59b4e91SDavid Howells struct dentry *parent, void *data, 490e59b4e91SDavid Howells const struct file_operations *fops, 491e59b4e91SDavid Howells loff_t file_size) 492e59b4e91SDavid Howells { 493e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 494e59b4e91SDavid Howells 495e59b4e91SDavid Howells if (de) 4962b0143b5SDavid Howells d_inode(de)->i_size = file_size; 497e59b4e91SDavid Howells return de; 498e59b4e91SDavid Howells } 499e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 500e59b4e91SDavid Howells 501e59b4e91SDavid Howells /** 5021da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 5031da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 5041da177e4SLinus Torvalds * create. 5051da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 506e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 5071da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 5081da177e4SLinus Torvalds * 5091da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 5101da177e4SLinus Torvalds * 5111da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5121da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5131da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 5146468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 5151da177e4SLinus Torvalds * 5166468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 517873760fbSCornelia Huck * returned. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 5201da177e4SLinus Torvalds { 521ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 522680b3024SAl Viro struct inode *inode; 523ad5abd5bSAl Viro 524ad5abd5bSAl Viro if (IS_ERR(dentry)) 525ad5abd5bSAl Viro return NULL; 526ad5abd5bSAl Viro 527edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 528680b3024SAl Viro if (unlikely(!inode)) 5295233e311SAl Viro return failed_creating(dentry); 530680b3024SAl Viro 531edac65eaSAl Viro inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 532edac65eaSAl Viro inode->i_op = &simple_dir_inode_operations; 533edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 534edac65eaSAl Viro 535edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 536edac65eaSAl Viro inc_nlink(inode); 537680b3024SAl Viro d_instantiate(dentry, inode); 5382b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 5392b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 5405233e311SAl Viro return end_creating(dentry); 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /** 54577b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 54677b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 54777b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 54877b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 54977b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 55077b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 55177b3da6eSAl Viro * @data: opaque argument to pass to f(). 55277b3da6eSAl Viro * 55377b3da6eSAl Viro * @f should return what ->d_automount() would. 55477b3da6eSAl Viro */ 55577b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 55677b3da6eSAl Viro struct dentry *parent, 55793faccbbSEric W. Biederman debugfs_automount_t f, 55877b3da6eSAl Viro void *data) 55977b3da6eSAl Viro { 56077b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 56177b3da6eSAl Viro struct inode *inode; 56277b3da6eSAl Viro 56377b3da6eSAl Viro if (IS_ERR(dentry)) 56477b3da6eSAl Viro return NULL; 56577b3da6eSAl Viro 56677b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 56777b3da6eSAl Viro if (unlikely(!inode)) 56877b3da6eSAl Viro return failed_creating(dentry); 56977b3da6eSAl Viro 57087243debSSeth Forshee make_empty_dir_inode(inode); 57177b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 57277b3da6eSAl Viro inode->i_private = data; 57377b3da6eSAl Viro dentry->d_fsdata = (void *)f; 574a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 575a8f324a4SRoman Pen inc_nlink(inode); 57677b3da6eSAl Viro d_instantiate(dentry, inode); 577a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 578a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 57977b3da6eSAl Viro return end_creating(dentry); 58077b3da6eSAl Viro } 58177b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 58277b3da6eSAl Viro 58377b3da6eSAl Viro /** 58466f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 58566f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 58666f54963SPeter Oberparleiter * create. 58766f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 588e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 58966f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 59066f54963SPeter Oberparleiter * filesystem. 59166f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 59266f54963SPeter Oberparleiter * symbolic link. 59366f54963SPeter Oberparleiter * 59466f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 59566f54963SPeter Oberparleiter * links to the given target path. 59666f54963SPeter Oberparleiter * 59766f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 59866f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 59966f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 60066f54963SPeter Oberparleiter * unloaded, you are responsible here.) If an error occurs, %NULL will be 60166f54963SPeter Oberparleiter * returned. 60266f54963SPeter Oberparleiter * 60366f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 604873760fbSCornelia Huck * returned. 60566f54963SPeter Oberparleiter */ 60666f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 60766f54963SPeter Oberparleiter const char *target) 60866f54963SPeter Oberparleiter { 609ad5abd5bSAl Viro struct dentry *dentry; 610680b3024SAl Viro struct inode *inode; 611680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 61266f54963SPeter Oberparleiter if (!link) 61366f54963SPeter Oberparleiter return NULL; 61466f54963SPeter Oberparleiter 615ad5abd5bSAl Viro dentry = start_creating(name, parent); 616ad5abd5bSAl Viro if (IS_ERR(dentry)) { 61766f54963SPeter Oberparleiter kfree(link); 618ad5abd5bSAl Viro return NULL; 619ad5abd5bSAl Viro } 620ad5abd5bSAl Viro 621edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 622680b3024SAl Viro if (unlikely(!inode)) { 623ad5abd5bSAl Viro kfree(link); 6245233e311SAl Viro return failed_creating(dentry); 625680b3024SAl Viro } 626edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 6275723cb01SAl Viro inode->i_op = &simple_symlink_inode_operations; 6285723cb01SAl Viro inode->i_link = link; 629680b3024SAl Viro d_instantiate(dentry, inode); 6305233e311SAl Viro return end_creating(dentry); 63166f54963SPeter Oberparleiter } 63266f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 63366f54963SPeter Oberparleiter 63425d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 6351da177e4SLinus Torvalds { 63665c33336SMathieu Desnoyers int ret = 0; 6371da177e4SLinus Torvalds 638dc3f4198SAl Viro if (simple_positive(dentry)) { 63929a7f3adSMathieu Desnoyers dget(dentry); 6407ceab50cSDavid Howells if (d_is_dir(dentry)) 6412b0143b5SDavid Howells ret = simple_rmdir(d_inode(parent), dentry); 6420db59e59SAl Viro else 6432b0143b5SDavid Howells simple_unlink(d_inode(parent), dentry); 64429a7f3adSMathieu Desnoyers if (!ret) 64529a7f3adSMathieu Desnoyers d_delete(dentry); 64629a7f3adSMathieu Desnoyers dput(dentry); 6471da177e4SLinus Torvalds } 64825d41d84SJan Kara return ret; 6499505e637SHaavard Skinnemoen } 6509505e637SHaavard Skinnemoen 6519505e637SHaavard Skinnemoen /** 6529505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 6539505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 654398dc4adSUlf Magnusson * removed. If this parameter is NULL or an error value, nothing 655398dc4adSUlf Magnusson * will be done. 6569505e637SHaavard Skinnemoen * 6579505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 6589505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 6599505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 6609505e637SHaavard Skinnemoen * 6619505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 6629505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 6639505e637SHaavard Skinnemoen * removed, you are responsible here. 6649505e637SHaavard Skinnemoen */ 6659505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 6669505e637SHaavard Skinnemoen { 6679505e637SHaavard Skinnemoen struct dentry *parent; 66825d41d84SJan Kara int ret; 6699505e637SHaavard Skinnemoen 670a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 6719505e637SHaavard Skinnemoen return; 6729505e637SHaavard Skinnemoen 6739505e637SHaavard Skinnemoen parent = dentry->d_parent; 6745955102cSAl Viro inode_lock(d_inode(parent)); 67525d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 6765955102cSAl Viro inode_unlock(d_inode(parent)); 67725d41d84SJan Kara if (!ret) 6781da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 67949d200deSNicolai Stange 6809fd4dcecSNicolai Stange synchronize_srcu(&debugfs_srcu); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 6831da177e4SLinus Torvalds 684cfc94cdfSJan Kara /** 6859505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 686398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 687398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 6889505e637SHaavard Skinnemoen * 6899505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 6909505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 6919505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 6929505e637SHaavard Skinnemoen * 6939505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 6949505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 6959505e637SHaavard Skinnemoen * removed, you are responsible here. 6969505e637SHaavard Skinnemoen */ 6979505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 6989505e637SHaavard Skinnemoen { 699485d4402SSteven Rostedt struct dentry *child, *parent; 7009505e637SHaavard Skinnemoen 701a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 7029505e637SHaavard Skinnemoen return; 7039505e637SHaavard Skinnemoen 7049505e637SHaavard Skinnemoen parent = dentry; 705776164c1SOleg Nesterov down: 7065955102cSAl Viro inode_lock(d_inode(parent)); 707485d4402SSteven Rostedt loop: 708485d4402SSteven Rostedt /* 709485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 710485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 711485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 712485d4402SSteven Rostedt */ 713485d4402SSteven Rostedt spin_lock(&parent->d_lock); 714946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 715dc3f4198SAl Viro if (!simple_positive(child)) 716776164c1SOleg Nesterov continue; 7179505e637SHaavard Skinnemoen 718776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 7199505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 720485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 7215955102cSAl Viro inode_unlock(d_inode(parent)); 7229505e637SHaavard Skinnemoen parent = child; 723776164c1SOleg Nesterov goto down; 7249505e637SHaavard Skinnemoen } 725485d4402SSteven Rostedt 726485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 727485d4402SSteven Rostedt 728776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 729776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 730485d4402SSteven Rostedt 731485d4402SSteven Rostedt /* 732485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 733485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 734485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 735485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 736dc3f4198SAl Viro * simple_positive() check. 737485d4402SSteven Rostedt */ 738485d4402SSteven Rostedt goto loop; 739776164c1SOleg Nesterov } 740485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 741776164c1SOleg Nesterov 7425955102cSAl Viro inode_unlock(d_inode(parent)); 743776164c1SOleg Nesterov child = parent; 744776164c1SOleg Nesterov parent = parent->d_parent; 7455955102cSAl Viro inode_lock(d_inode(parent)); 746776164c1SOleg Nesterov 747485d4402SSteven Rostedt if (child != dentry) 748485d4402SSteven Rostedt /* go up */ 749485d4402SSteven Rostedt goto loop; 75056a83cc9SSteven Rostedt 751776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 7529505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 7535955102cSAl Viro inode_unlock(d_inode(parent)); 75449d200deSNicolai Stange 7559fd4dcecSNicolai Stange synchronize_srcu(&debugfs_srcu); 7569505e637SHaavard Skinnemoen } 7579505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 7589505e637SHaavard Skinnemoen 7599505e637SHaavard Skinnemoen /** 760cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 761cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 762cfc94cdfSJan Kara * should be a directory dentry. 763cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 764cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 765cfc94cdfSJan Kara * moved. This should be a directory dentry. 766cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 767cfc94cdfSJan Kara * 768cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 769cfc94cdfSJan Kara * exist for rename to succeed. 770cfc94cdfSJan Kara * 771cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 772cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 773cfc94cdfSJan Kara * returned. 774cfc94cdfSJan Kara * 775cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 776cfc94cdfSJan Kara * returned. 777cfc94cdfSJan Kara */ 778cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 779cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 780cfc94cdfSJan Kara { 781cfc94cdfSJan Kara int error; 782cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 78349d31c2fSAl Viro struct name_snapshot old_name; 784cfc94cdfSJan Kara 785cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 786cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 7872b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 788cfc94cdfSJan Kara goto exit; 789cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 7902b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 791cfc94cdfSJan Kara d_mountpoint(old_dentry)) 792cfc94cdfSJan Kara goto exit; 793cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 794cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 7952b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 796cfc94cdfSJan Kara goto exit; 797cfc94cdfSJan Kara 79849d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 799cfc94cdfSJan Kara 8002b0143b5SDavid Howells error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 801e0e0be8aSMiklos Szeredi dentry, 0); 802cfc94cdfSJan Kara if (error) { 80349d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 804cfc94cdfSJan Kara goto exit; 805cfc94cdfSJan Kara } 806cfc94cdfSJan Kara d_move(old_dentry, dentry); 80749d31c2fSAl Viro fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name.name, 808e36cb0b8SDavid Howells d_is_dir(old_dentry), 8095a190ae6SAl Viro NULL, old_dentry); 81049d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 811cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 812cfc94cdfSJan Kara dput(dentry); 813cfc94cdfSJan Kara return old_dentry; 814cfc94cdfSJan Kara exit: 815cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 816cfc94cdfSJan Kara dput(dentry); 817cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 818cfc94cdfSJan Kara return NULL; 819cfc94cdfSJan Kara } 820cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 821cfc94cdfSJan Kara 822c0f92ba9SFrederic Weisbecker /** 823c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 824c0f92ba9SFrederic Weisbecker */ 825c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 826c0f92ba9SFrederic Weisbecker { 827c0f92ba9SFrederic Weisbecker return debugfs_registered; 828c0f92ba9SFrederic Weisbecker } 829c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 830c0f92ba9SFrederic Weisbecker 8311da177e4SLinus Torvalds static int __init debugfs_init(void) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds int retval; 8341da177e4SLinus Torvalds 835f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 836f9bb4882SEric W. Biederman if (retval) 837f9bb4882SEric W. Biederman return retval; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 8401da177e4SLinus Torvalds if (retval) 841f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 842c0f92ba9SFrederic Weisbecker else 843c0f92ba9SFrederic Weisbecker debugfs_registered = true; 844c0f92ba9SFrederic Weisbecker 8451da177e4SLinus Torvalds return retval; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds core_initcall(debugfs_init); 8481da177e4SLinus Torvalds 849