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. 121da177e4SLinus Torvalds * See Documentation/DocBook/kernel-api 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> 301da177e4SLinus Torvalds 3182aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE 0700 32d6e48686SLudwig Nussel 331da177e4SLinus Torvalds static struct vfsmount *debugfs_mount; 341da177e4SLinus Torvalds static int debugfs_mount_count; 35c0f92ba9SFrederic Weisbecker static bool debugfs_registered; 361da177e4SLinus Torvalds 37edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb) 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 401da177e4SLinus Torvalds if (inode) { 4185fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 421da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds return inode; 451da177e4SLinus Torvalds } 461da177e4SLinus Torvalds 47d6e48686SLudwig Nussel struct debugfs_mount_opts { 487dc05881SEric W. Biederman kuid_t uid; 497dc05881SEric W. Biederman kgid_t gid; 50d6e48686SLudwig Nussel umode_t mode; 51d6e48686SLudwig Nussel }; 52d6e48686SLudwig Nussel 53d6e48686SLudwig Nussel enum { 54d6e48686SLudwig Nussel Opt_uid, 55d6e48686SLudwig Nussel Opt_gid, 56d6e48686SLudwig Nussel Opt_mode, 57d6e48686SLudwig Nussel Opt_err 58d6e48686SLudwig Nussel }; 59d6e48686SLudwig Nussel 60d6e48686SLudwig Nussel static const match_table_t tokens = { 61d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 62d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 63d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 64d6e48686SLudwig Nussel {Opt_err, NULL} 65d6e48686SLudwig Nussel }; 66d6e48686SLudwig Nussel 67d6e48686SLudwig Nussel struct debugfs_fs_info { 68d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 69d6e48686SLudwig Nussel }; 70d6e48686SLudwig Nussel 71d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 72d6e48686SLudwig Nussel { 73d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 74d6e48686SLudwig Nussel int option; 75d6e48686SLudwig Nussel int token; 767dc05881SEric W. Biederman kuid_t uid; 777dc05881SEric W. Biederman kgid_t gid; 78d6e48686SLudwig Nussel char *p; 79d6e48686SLudwig Nussel 80d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 81d6e48686SLudwig Nussel 82d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 83d6e48686SLudwig Nussel if (!*p) 84d6e48686SLudwig Nussel continue; 85d6e48686SLudwig Nussel 86d6e48686SLudwig Nussel token = match_token(p, tokens, args); 87d6e48686SLudwig Nussel switch (token) { 88d6e48686SLudwig Nussel case Opt_uid: 89d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 90d6e48686SLudwig Nussel return -EINVAL; 917dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 927dc05881SEric W. Biederman if (!uid_valid(uid)) 937dc05881SEric W. Biederman return -EINVAL; 947dc05881SEric W. Biederman opts->uid = uid; 95d6e48686SLudwig Nussel break; 96d6e48686SLudwig Nussel case Opt_gid: 97f1688e04SDave Reisner if (match_int(&args[0], &option)) 98d6e48686SLudwig Nussel return -EINVAL; 997dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1007dc05881SEric W. Biederman if (!gid_valid(gid)) 1017dc05881SEric W. Biederman return -EINVAL; 1027dc05881SEric W. Biederman opts->gid = gid; 103d6e48686SLudwig Nussel break; 104d6e48686SLudwig Nussel case Opt_mode: 105d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 106d6e48686SLudwig Nussel return -EINVAL; 107d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 108d6e48686SLudwig Nussel break; 109d6e48686SLudwig Nussel /* 110d6e48686SLudwig Nussel * We might like to report bad mount options here; 111d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 112d6e48686SLudwig Nussel */ 113d6e48686SLudwig Nussel } 114d6e48686SLudwig Nussel } 115d6e48686SLudwig Nussel 116d6e48686SLudwig Nussel return 0; 117d6e48686SLudwig Nussel } 118d6e48686SLudwig Nussel 119d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 120d6e48686SLudwig Nussel { 121d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 1222b0143b5SDavid Howells struct inode *inode = d_inode(sb->s_root); 123d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 124d6e48686SLudwig Nussel 125d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 126d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 127d6e48686SLudwig Nussel 128d6e48686SLudwig Nussel inode->i_uid = opts->uid; 129d6e48686SLudwig Nussel inode->i_gid = opts->gid; 130d6e48686SLudwig Nussel 131d6e48686SLudwig Nussel return 0; 132d6e48686SLudwig Nussel } 133d6e48686SLudwig Nussel 134d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 135d6e48686SLudwig Nussel { 136d6e48686SLudwig Nussel int err; 137d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 138d6e48686SLudwig Nussel 13902b9984dSTheodore Ts'o sync_filesystem(sb); 140d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 141d6e48686SLudwig Nussel if (err) 142d6e48686SLudwig Nussel goto fail; 143d6e48686SLudwig Nussel 144d6e48686SLudwig Nussel debugfs_apply_options(sb); 145d6e48686SLudwig Nussel 146d6e48686SLudwig Nussel fail: 147d6e48686SLudwig Nussel return err; 148d6e48686SLudwig Nussel } 149d6e48686SLudwig Nussel 150d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 151d6e48686SLudwig Nussel { 152d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 153d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 154d6e48686SLudwig Nussel 1557dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 1567dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 1577dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 1587dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 1597dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 1607dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 161d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 162d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 163d6e48686SLudwig Nussel 164d6e48686SLudwig Nussel return 0; 165d6e48686SLudwig Nussel } 166d6e48686SLudwig Nussel 1670db59e59SAl Viro static void debugfs_evict_inode(struct inode *inode) 1680db59e59SAl Viro { 1690db59e59SAl Viro truncate_inode_pages_final(&inode->i_data); 1700db59e59SAl Viro clear_inode(inode); 1710db59e59SAl Viro if (S_ISLNK(inode->i_mode)) 1725723cb01SAl Viro kfree(inode->i_link); 1730db59e59SAl Viro } 1740db59e59SAl Viro 175d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 176d6e48686SLudwig Nussel .statfs = simple_statfs, 177d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 178d6e48686SLudwig Nussel .show_options = debugfs_show_options, 1790db59e59SAl Viro .evict_inode = debugfs_evict_inode, 180d6e48686SLudwig Nussel }; 181d6e48686SLudwig Nussel 18277b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path) 18377b3da6eSAl Viro { 18477b3da6eSAl Viro struct vfsmount *(*f)(void *); 18577b3da6eSAl Viro f = (struct vfsmount *(*)(void *))path->dentry->d_fsdata; 1862b0143b5SDavid Howells return f(d_inode(path->dentry)->i_private); 18777b3da6eSAl Viro } 18877b3da6eSAl Viro 18977b3da6eSAl Viro static const struct dentry_operations debugfs_dops = { 19077b3da6eSAl Viro .d_delete = always_delete_dentry, 19177b3da6eSAl Viro .d_automount = debugfs_automount, 19277b3da6eSAl Viro }; 19377b3da6eSAl Viro 1941da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds static struct tree_descr debug_files[] = {{""}}; 197d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 198d6e48686SLudwig Nussel int err; 1991da177e4SLinus Torvalds 200d6e48686SLudwig Nussel save_mount_options(sb, data); 201d6e48686SLudwig Nussel 202d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 203d6e48686SLudwig Nussel sb->s_fs_info = fsi; 204d6e48686SLudwig Nussel if (!fsi) { 205d6e48686SLudwig Nussel err = -ENOMEM; 206d6e48686SLudwig Nussel goto fail; 207d6e48686SLudwig Nussel } 208d6e48686SLudwig Nussel 209d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 210d6e48686SLudwig Nussel if (err) 211d6e48686SLudwig Nussel goto fail; 212d6e48686SLudwig Nussel 213d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 214d6e48686SLudwig Nussel if (err) 215d6e48686SLudwig Nussel goto fail; 216d6e48686SLudwig Nussel 217d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 21877b3da6eSAl Viro sb->s_d_op = &debugfs_dops; 219d6e48686SLudwig Nussel 220d6e48686SLudwig Nussel debugfs_apply_options(sb); 221d6e48686SLudwig Nussel 222d6e48686SLudwig Nussel return 0; 223d6e48686SLudwig Nussel 224d6e48686SLudwig Nussel fail: 225d6e48686SLudwig Nussel kfree(fsi); 226d6e48686SLudwig Nussel sb->s_fs_info = NULL; 227d6e48686SLudwig Nussel return err; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 230fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2311da177e4SLinus Torvalds int flags, const char *dev_name, 232fc14f2feSAl Viro void *data) 2331da177e4SLinus Torvalds { 234fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2381da177e4SLinus Torvalds .owner = THIS_MODULE, 2391da177e4SLinus Torvalds .name = "debugfs", 240fc14f2feSAl Viro .mount = debug_mount, 2411da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2421da177e4SLinus Torvalds }; 2437f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2441da177e4SLinus Torvalds 245190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 246c3b1a350SAl Viro { 247190afd81SAl Viro struct dentry *dentry; 248c3b1a350SAl Viro int error; 249c3b1a350SAl Viro 250c3b1a350SAl Viro pr_debug("debugfs: creating file '%s'\n",name); 251c3b1a350SAl Viro 252c9e15f25SGreg KH if (IS_ERR(parent)) 253c9e15f25SGreg KH return parent; 254c9e15f25SGreg KH 255c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 256c3b1a350SAl Viro &debugfs_mount_count); 257c3b1a350SAl Viro if (error) 258190afd81SAl Viro return ERR_PTR(error); 259c3b1a350SAl Viro 260cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 261cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 262cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 263cfa57c11SAl Viro * have around. 264cfa57c11SAl Viro */ 265cfa57c11SAl Viro if (!parent) 266cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 267cfa57c11SAl Viro 2685955102cSAl Viro inode_lock(d_inode(parent)); 269cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 2702b0143b5SDavid Howells if (!IS_ERR(dentry) && d_really_is_positive(dentry)) { 271190afd81SAl Viro dput(dentry); 272190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 273190afd81SAl Viro } 2740ee9608cSDaniel Borkmann 2750ee9608cSDaniel Borkmann if (IS_ERR(dentry)) { 2765955102cSAl Viro inode_unlock(d_inode(parent)); 2770ee9608cSDaniel Borkmann simple_release_fs(&debugfs_mount, &debugfs_mount_count); 2780ee9608cSDaniel Borkmann } 2790ee9608cSDaniel Borkmann 280190afd81SAl Viro return dentry; 281190afd81SAl Viro } 282190afd81SAl Viro 2835233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry) 284190afd81SAl Viro { 2855955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 286190afd81SAl Viro dput(dentry); 287190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 2885233e311SAl Viro return NULL; 289190afd81SAl Viro } 2905233e311SAl Viro 2915233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry) 2925233e311SAl Viro { 2935955102cSAl Viro inode_unlock(d_inode(dentry->d_parent)); 294190afd81SAl Viro return dentry; 295190afd81SAl Viro } 296190afd81SAl Viro 2971da177e4SLinus Torvalds /** 2981da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 2991da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 300be030e65SAlberto Bertogli * @mode: the permission that the file should have. 3011da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 302e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3031da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 3041da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 3058e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 3061da177e4SLinus Torvalds * the open() call. 3071da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 3081da177e4SLinus Torvalds * this file. 3091da177e4SLinus Torvalds * 3101da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 311be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 312be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 3131da177e4SLinus Torvalds * recommended to be used instead.) 3141da177e4SLinus Torvalds * 3151da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 3161da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 3171da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 3186468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 3191da177e4SLinus Torvalds * 3206468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 321873760fbSCornelia Huck * returned. 3221da177e4SLinus Torvalds */ 323f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 3241da177e4SLinus Torvalds struct dentry *parent, void *data, 32599ac48f5SArjan van de Ven const struct file_operations *fops) 3261da177e4SLinus Torvalds { 327ad5abd5bSAl Viro struct dentry *dentry; 328680b3024SAl Viro struct inode *inode; 329c3b1a350SAl Viro 330ad5abd5bSAl Viro if (!(mode & S_IFMT)) 331ad5abd5bSAl Viro mode |= S_IFREG; 332ad5abd5bSAl Viro BUG_ON(!S_ISREG(mode)); 333ad5abd5bSAl Viro dentry = start_creating(name, parent); 334ad5abd5bSAl Viro 335ad5abd5bSAl Viro if (IS_ERR(dentry)) 336ad5abd5bSAl Viro return NULL; 337ad5abd5bSAl Viro 338edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 339680b3024SAl Viro if (unlikely(!inode)) 3405233e311SAl Viro return failed_creating(dentry); 341680b3024SAl Viro 342edac65eaSAl Viro inode->i_mode = mode; 343edac65eaSAl Viro inode->i_fop = fops ? fops : &debugfs_file_operations; 344edac65eaSAl Viro inode->i_private = data; 345680b3024SAl Viro d_instantiate(dentry, inode); 3462b0143b5SDavid Howells fsnotify_create(d_inode(dentry->d_parent), dentry); 3475233e311SAl Viro return end_creating(dentry); 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /** 352e59b4e91SDavid Howells * debugfs_create_file_size - create a file in the debugfs filesystem 353e59b4e91SDavid Howells * @name: a pointer to a string containing the name of the file to create. 354e59b4e91SDavid Howells * @mode: the permission that the file should have. 355e59b4e91SDavid Howells * @parent: a pointer to the parent dentry for this file. This should be a 356e59b4e91SDavid Howells * directory dentry if set. If this parameter is NULL, then the 357e59b4e91SDavid Howells * file will be created in the root of the debugfs filesystem. 358e59b4e91SDavid Howells * @data: a pointer to something that the caller will want to get to later 359e59b4e91SDavid Howells * on. The inode.i_private pointer will point to this value on 360e59b4e91SDavid Howells * the open() call. 361e59b4e91SDavid Howells * @fops: a pointer to a struct file_operations that should be used for 362e59b4e91SDavid Howells * this file. 363e59b4e91SDavid Howells * @file_size: initial file size 364e59b4e91SDavid Howells * 365e59b4e91SDavid Howells * This is the basic "create a file" function for debugfs. It allows for a 366e59b4e91SDavid Howells * wide range of flexibility in creating a file, or a directory (if you want 367e59b4e91SDavid Howells * to create a directory, the debugfs_create_dir() function is 368e59b4e91SDavid Howells * recommended to be used instead.) 369e59b4e91SDavid Howells * 370e59b4e91SDavid Howells * This function will return a pointer to a dentry if it succeeds. This 371e59b4e91SDavid Howells * pointer must be passed to the debugfs_remove() function when the file is 372e59b4e91SDavid Howells * to be removed (no automatic cleanup happens if your module is unloaded, 373e59b4e91SDavid Howells * you are responsible here.) If an error occurs, %NULL will be returned. 374e59b4e91SDavid Howells * 375e59b4e91SDavid Howells * If debugfs is not enabled in the kernel, the value -%ENODEV will be 376e59b4e91SDavid Howells * returned. 377e59b4e91SDavid Howells */ 378e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode, 379e59b4e91SDavid Howells struct dentry *parent, void *data, 380e59b4e91SDavid Howells const struct file_operations *fops, 381e59b4e91SDavid Howells loff_t file_size) 382e59b4e91SDavid Howells { 383e59b4e91SDavid Howells struct dentry *de = debugfs_create_file(name, mode, parent, data, fops); 384e59b4e91SDavid Howells 385e59b4e91SDavid Howells if (de) 3862b0143b5SDavid Howells d_inode(de)->i_size = file_size; 387e59b4e91SDavid Howells return de; 388e59b4e91SDavid Howells } 389e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size); 390e59b4e91SDavid Howells 391e59b4e91SDavid Howells /** 3921da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 3931da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 3941da177e4SLinus Torvalds * create. 3951da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 396e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3971da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 3981da177e4SLinus Torvalds * 3991da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 4001da177e4SLinus Torvalds * 4011da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4021da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4031da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4046468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4051da177e4SLinus Torvalds * 4066468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 407873760fbSCornelia Huck * returned. 4081da177e4SLinus Torvalds */ 4091da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 4101da177e4SLinus Torvalds { 411ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 412680b3024SAl Viro struct inode *inode; 413ad5abd5bSAl Viro 414ad5abd5bSAl Viro if (IS_ERR(dentry)) 415ad5abd5bSAl Viro return NULL; 416ad5abd5bSAl Viro 417edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 418680b3024SAl Viro if (unlikely(!inode)) 4195233e311SAl Viro return failed_creating(dentry); 420680b3024SAl Viro 421edac65eaSAl Viro inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 422edac65eaSAl Viro inode->i_op = &simple_dir_inode_operations; 423edac65eaSAl Viro inode->i_fop = &simple_dir_operations; 424edac65eaSAl Viro 425edac65eaSAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 426edac65eaSAl Viro inc_nlink(inode); 427680b3024SAl Viro d_instantiate(dentry, inode); 4282b0143b5SDavid Howells inc_nlink(d_inode(dentry->d_parent)); 4292b0143b5SDavid Howells fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 4305233e311SAl Viro return end_creating(dentry); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /** 43577b3da6eSAl Viro * debugfs_create_automount - create automount point in the debugfs filesystem 43677b3da6eSAl Viro * @name: a pointer to a string containing the name of the file to create. 43777b3da6eSAl Viro * @parent: a pointer to the parent dentry for this file. This should be a 43877b3da6eSAl Viro * directory dentry if set. If this parameter is NULL, then the 43977b3da6eSAl Viro * file will be created in the root of the debugfs filesystem. 44077b3da6eSAl Viro * @f: function to be called when pathname resolution steps on that one. 44177b3da6eSAl Viro * @data: opaque argument to pass to f(). 44277b3da6eSAl Viro * 44377b3da6eSAl Viro * @f should return what ->d_automount() would. 44477b3da6eSAl Viro */ 44577b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name, 44677b3da6eSAl Viro struct dentry *parent, 44777b3da6eSAl Viro struct vfsmount *(*f)(void *), 44877b3da6eSAl Viro void *data) 44977b3da6eSAl Viro { 45077b3da6eSAl Viro struct dentry *dentry = start_creating(name, parent); 45177b3da6eSAl Viro struct inode *inode; 45277b3da6eSAl Viro 45377b3da6eSAl Viro if (IS_ERR(dentry)) 45477b3da6eSAl Viro return NULL; 45577b3da6eSAl Viro 45677b3da6eSAl Viro inode = debugfs_get_inode(dentry->d_sb); 45777b3da6eSAl Viro if (unlikely(!inode)) 45877b3da6eSAl Viro return failed_creating(dentry); 45977b3da6eSAl Viro 46077b3da6eSAl Viro inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; 46177b3da6eSAl Viro inode->i_flags |= S_AUTOMOUNT; 46277b3da6eSAl Viro inode->i_private = data; 46377b3da6eSAl Viro dentry->d_fsdata = (void *)f; 464*a8f324a4SRoman Pen /* directory inodes start off with i_nlink == 2 (for "." entry) */ 465*a8f324a4SRoman Pen inc_nlink(inode); 46677b3da6eSAl Viro d_instantiate(dentry, inode); 467*a8f324a4SRoman Pen inc_nlink(d_inode(dentry->d_parent)); 468*a8f324a4SRoman Pen fsnotify_mkdir(d_inode(dentry->d_parent), dentry); 46977b3da6eSAl Viro return end_creating(dentry); 47077b3da6eSAl Viro } 47177b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount); 47277b3da6eSAl Viro 47377b3da6eSAl Viro /** 47466f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 47566f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 47666f54963SPeter Oberparleiter * create. 47766f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 478e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 47966f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 48066f54963SPeter Oberparleiter * filesystem. 48166f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 48266f54963SPeter Oberparleiter * symbolic link. 48366f54963SPeter Oberparleiter * 48466f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 48566f54963SPeter Oberparleiter * links to the given target path. 48666f54963SPeter Oberparleiter * 48766f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 48866f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 48966f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 49066f54963SPeter Oberparleiter * unloaded, you are responsible here.) If an error occurs, %NULL will be 49166f54963SPeter Oberparleiter * returned. 49266f54963SPeter Oberparleiter * 49366f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 494873760fbSCornelia Huck * returned. 49566f54963SPeter Oberparleiter */ 49666f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 49766f54963SPeter Oberparleiter const char *target) 49866f54963SPeter Oberparleiter { 499ad5abd5bSAl Viro struct dentry *dentry; 500680b3024SAl Viro struct inode *inode; 501680b3024SAl Viro char *link = kstrdup(target, GFP_KERNEL); 50266f54963SPeter Oberparleiter if (!link) 50366f54963SPeter Oberparleiter return NULL; 50466f54963SPeter Oberparleiter 505ad5abd5bSAl Viro dentry = start_creating(name, parent); 506ad5abd5bSAl Viro if (IS_ERR(dentry)) { 50766f54963SPeter Oberparleiter kfree(link); 508ad5abd5bSAl Viro return NULL; 509ad5abd5bSAl Viro } 510ad5abd5bSAl Viro 511edac65eaSAl Viro inode = debugfs_get_inode(dentry->d_sb); 512680b3024SAl Viro if (unlikely(!inode)) { 513ad5abd5bSAl Viro kfree(link); 5145233e311SAl Viro return failed_creating(dentry); 515680b3024SAl Viro } 516edac65eaSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 5175723cb01SAl Viro inode->i_op = &simple_symlink_inode_operations; 5185723cb01SAl Viro inode->i_link = link; 519680b3024SAl Viro d_instantiate(dentry, inode); 5205233e311SAl Viro return end_creating(dentry); 52166f54963SPeter Oberparleiter } 52266f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 52366f54963SPeter Oberparleiter 52425d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 5251da177e4SLinus Torvalds { 52665c33336SMathieu Desnoyers int ret = 0; 5271da177e4SLinus Torvalds 528dc3f4198SAl Viro if (simple_positive(dentry)) { 52929a7f3adSMathieu Desnoyers dget(dentry); 5307ceab50cSDavid Howells if (d_is_dir(dentry)) 5312b0143b5SDavid Howells ret = simple_rmdir(d_inode(parent), dentry); 5320db59e59SAl Viro else 5332b0143b5SDavid Howells simple_unlink(d_inode(parent), dentry); 53429a7f3adSMathieu Desnoyers if (!ret) 53529a7f3adSMathieu Desnoyers d_delete(dentry); 53629a7f3adSMathieu Desnoyers dput(dentry); 5371da177e4SLinus Torvalds } 53825d41d84SJan Kara return ret; 5399505e637SHaavard Skinnemoen } 5409505e637SHaavard Skinnemoen 5419505e637SHaavard Skinnemoen /** 5429505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 5439505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 544398dc4adSUlf Magnusson * removed. If this parameter is NULL or an error value, nothing 545398dc4adSUlf Magnusson * will be done. 5469505e637SHaavard Skinnemoen * 5479505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 5489505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 5499505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 5509505e637SHaavard Skinnemoen * 5519505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 5529505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 5539505e637SHaavard Skinnemoen * removed, you are responsible here. 5549505e637SHaavard Skinnemoen */ 5559505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 5569505e637SHaavard Skinnemoen { 5579505e637SHaavard Skinnemoen struct dentry *parent; 55825d41d84SJan Kara int ret; 5599505e637SHaavard Skinnemoen 560a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 5619505e637SHaavard Skinnemoen return; 5629505e637SHaavard Skinnemoen 5639505e637SHaavard Skinnemoen parent = dentry->d_parent; 5642b0143b5SDavid Howells if (!parent || d_really_is_negative(parent)) 5659505e637SHaavard Skinnemoen return; 5669505e637SHaavard Skinnemoen 5675955102cSAl Viro inode_lock(d_inode(parent)); 56825d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 5695955102cSAl Viro inode_unlock(d_inode(parent)); 57025d41d84SJan Kara if (!ret) 5711da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 5741da177e4SLinus Torvalds 575cfc94cdfSJan Kara /** 5769505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 577398dc4adSUlf Magnusson * @dentry: a pointer to a the dentry of the directory to be removed. If this 578398dc4adSUlf Magnusson * parameter is NULL or an error value, nothing will be done. 5799505e637SHaavard Skinnemoen * 5809505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 5819505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 5829505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 5839505e637SHaavard Skinnemoen * 5849505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 5859505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 5869505e637SHaavard Skinnemoen * removed, you are responsible here. 5879505e637SHaavard Skinnemoen */ 5889505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 5899505e637SHaavard Skinnemoen { 590485d4402SSteven Rostedt struct dentry *child, *parent; 5919505e637SHaavard Skinnemoen 592a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 5939505e637SHaavard Skinnemoen return; 5949505e637SHaavard Skinnemoen 5959505e637SHaavard Skinnemoen parent = dentry->d_parent; 5962b0143b5SDavid Howells if (!parent || d_really_is_negative(parent)) 5979505e637SHaavard Skinnemoen return; 5989505e637SHaavard Skinnemoen 5999505e637SHaavard Skinnemoen parent = dentry; 600776164c1SOleg Nesterov down: 6015955102cSAl Viro inode_lock(d_inode(parent)); 602485d4402SSteven Rostedt loop: 603485d4402SSteven Rostedt /* 604485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 605485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 606485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 607485d4402SSteven Rostedt */ 608485d4402SSteven Rostedt spin_lock(&parent->d_lock); 609946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 610dc3f4198SAl Viro if (!simple_positive(child)) 611776164c1SOleg Nesterov continue; 6129505e637SHaavard Skinnemoen 613776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 6149505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 615485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 6165955102cSAl Viro inode_unlock(d_inode(parent)); 6179505e637SHaavard Skinnemoen parent = child; 618776164c1SOleg Nesterov goto down; 6199505e637SHaavard Skinnemoen } 620485d4402SSteven Rostedt 621485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 622485d4402SSteven Rostedt 623776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 624776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 625485d4402SSteven Rostedt 626485d4402SSteven Rostedt /* 627485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 628485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 629485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 630485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 631dc3f4198SAl Viro * simple_positive() check. 632485d4402SSteven Rostedt */ 633485d4402SSteven Rostedt goto loop; 634776164c1SOleg Nesterov } 635485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 636776164c1SOleg Nesterov 6375955102cSAl Viro inode_unlock(d_inode(parent)); 638776164c1SOleg Nesterov child = parent; 639776164c1SOleg Nesterov parent = parent->d_parent; 6405955102cSAl Viro inode_lock(d_inode(parent)); 641776164c1SOleg Nesterov 642485d4402SSteven Rostedt if (child != dentry) 643485d4402SSteven Rostedt /* go up */ 644485d4402SSteven Rostedt goto loop; 64556a83cc9SSteven Rostedt 646776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 6479505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 6485955102cSAl Viro inode_unlock(d_inode(parent)); 6499505e637SHaavard Skinnemoen } 6509505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 6519505e637SHaavard Skinnemoen 6529505e637SHaavard Skinnemoen /** 653cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 654cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 655cfc94cdfSJan Kara * should be a directory dentry. 656cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 657cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 658cfc94cdfSJan Kara * moved. This should be a directory dentry. 659cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 660cfc94cdfSJan Kara * 661cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 662cfc94cdfSJan Kara * exist for rename to succeed. 663cfc94cdfSJan Kara * 664cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 665cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 666cfc94cdfSJan Kara * returned. 667cfc94cdfSJan Kara * 668cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 669cfc94cdfSJan Kara * returned. 670cfc94cdfSJan Kara */ 671cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 672cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 673cfc94cdfSJan Kara { 674cfc94cdfSJan Kara int error; 675cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 676cfc94cdfSJan Kara const char *old_name; 677cfc94cdfSJan Kara 678cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 679cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 6802b0143b5SDavid Howells if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir)) 681cfc94cdfSJan Kara goto exit; 682cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 6832b0143b5SDavid Howells if (d_really_is_negative(old_dentry) || old_dentry == trap || 684cfc94cdfSJan Kara d_mountpoint(old_dentry)) 685cfc94cdfSJan Kara goto exit; 686cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 687cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 6882b0143b5SDavid Howells if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry)) 689cfc94cdfSJan Kara goto exit; 690cfc94cdfSJan Kara 691cfc94cdfSJan Kara old_name = fsnotify_oldname_init(old_dentry->d_name.name); 692cfc94cdfSJan Kara 6932b0143b5SDavid Howells error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir), 694cfc94cdfSJan Kara dentry); 695cfc94cdfSJan Kara if (error) { 696cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 697cfc94cdfSJan Kara goto exit; 698cfc94cdfSJan Kara } 699cfc94cdfSJan Kara d_move(old_dentry, dentry); 7002b0143b5SDavid Howells fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name, 701e36cb0b8SDavid Howells d_is_dir(old_dentry), 7025a190ae6SAl Viro NULL, old_dentry); 703cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 704cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 705cfc94cdfSJan Kara dput(dentry); 706cfc94cdfSJan Kara return old_dentry; 707cfc94cdfSJan Kara exit: 708cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 709cfc94cdfSJan Kara dput(dentry); 710cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 711cfc94cdfSJan Kara return NULL; 712cfc94cdfSJan Kara } 713cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 714cfc94cdfSJan Kara 715c0f92ba9SFrederic Weisbecker /** 716c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 717c0f92ba9SFrederic Weisbecker */ 718c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 719c0f92ba9SFrederic Weisbecker { 720c0f92ba9SFrederic Weisbecker return debugfs_registered; 721c0f92ba9SFrederic Weisbecker } 722c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 723c0f92ba9SFrederic Weisbecker 7241da177e4SLinus Torvalds static int __init debugfs_init(void) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds int retval; 7271da177e4SLinus Torvalds 728f9bb4882SEric W. Biederman retval = sysfs_create_mount_point(kernel_kobj, "debug"); 729f9bb4882SEric W. Biederman if (retval) 730f9bb4882SEric W. Biederman return retval; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 7331da177e4SLinus Torvalds if (retval) 734f9bb4882SEric W. Biederman sysfs_remove_mount_point(kernel_kobj, "debug"); 735c0f92ba9SFrederic Weisbecker else 736c0f92ba9SFrederic Weisbecker debugfs_registered = true; 737c0f92ba9SFrederic Weisbecker 7381da177e4SLinus Torvalds return retval; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds core_initcall(debugfs_init); 7411da177e4SLinus Torvalds 742