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 37f4ae40a6SAl Viro static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev, 38d3a3b0adSMathieu Desnoyers void *data, const struct file_operations *fops) 39d3a3b0adSMathieu Desnoyers 401da177e4SLinus Torvalds { 411da177e4SLinus Torvalds struct inode *inode = new_inode(sb); 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds if (inode) { 4485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 451da177e4SLinus Torvalds inode->i_mode = mode; 461da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 471da177e4SLinus Torvalds switch (mode & S_IFMT) { 481da177e4SLinus Torvalds default: 491da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 501da177e4SLinus Torvalds break; 511da177e4SLinus Torvalds case S_IFREG: 52d3a3b0adSMathieu Desnoyers inode->i_fop = fops ? fops : &debugfs_file_operations; 53d3a3b0adSMathieu Desnoyers inode->i_private = data; 541da177e4SLinus Torvalds break; 5566f54963SPeter Oberparleiter case S_IFLNK: 5666f54963SPeter Oberparleiter inode->i_op = &debugfs_link_operations; 57d3a3b0adSMathieu Desnoyers inode->i_private = data; 5866f54963SPeter Oberparleiter break; 591da177e4SLinus Torvalds case S_IFDIR: 601da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 61ac481d6cSAl Viro inode->i_fop = &simple_dir_operations; 621da177e4SLinus Torvalds 63bafb232eSMathieu Desnoyers /* directory inodes start off with i_nlink == 2 64bafb232eSMathieu Desnoyers * (for "." entry) */ 65d8c76e6fSDave Hansen inc_nlink(inode); 661da177e4SLinus Torvalds break; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds return inode; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* SMP-safe */ 73160f7592SAl Viro static int debugfs_mknod(struct dentry *dentry, 74160f7592SAl Viro umode_t mode, void *data, 75d3a3b0adSMathieu Desnoyers const struct file_operations *fops) 761da177e4SLinus Torvalds { 7771601e2bSJens Axboe struct inode *inode; 781da177e4SLinus Torvalds 79160f7592SAl Viro inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops); 80160f7592SAl Viro if (unlikely(!inode)) 81160f7592SAl Viro return -EPERM; 821da177e4SLinus Torvalds d_instantiate(dentry, inode); 831da177e4SLinus Torvalds dget(dentry); 84160f7592SAl Viro return 0; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds static inline int debugfs_positive(struct dentry *dentry) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds return dentry->d_inode && !d_unhashed(dentry); 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 92d6e48686SLudwig Nussel struct debugfs_mount_opts { 937dc05881SEric W. Biederman kuid_t uid; 947dc05881SEric W. Biederman kgid_t gid; 95d6e48686SLudwig Nussel umode_t mode; 96d6e48686SLudwig Nussel }; 97d6e48686SLudwig Nussel 98d6e48686SLudwig Nussel enum { 99d6e48686SLudwig Nussel Opt_uid, 100d6e48686SLudwig Nussel Opt_gid, 101d6e48686SLudwig Nussel Opt_mode, 102d6e48686SLudwig Nussel Opt_err 103d6e48686SLudwig Nussel }; 104d6e48686SLudwig Nussel 105d6e48686SLudwig Nussel static const match_table_t tokens = { 106d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 107d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 108d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 109d6e48686SLudwig Nussel {Opt_err, NULL} 110d6e48686SLudwig Nussel }; 111d6e48686SLudwig Nussel 112d6e48686SLudwig Nussel struct debugfs_fs_info { 113d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 114d6e48686SLudwig Nussel }; 115d6e48686SLudwig Nussel 116d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 117d6e48686SLudwig Nussel { 118d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 119d6e48686SLudwig Nussel int option; 120d6e48686SLudwig Nussel int token; 1217dc05881SEric W. Biederman kuid_t uid; 1227dc05881SEric W. Biederman kgid_t gid; 123d6e48686SLudwig Nussel char *p; 124d6e48686SLudwig Nussel 125d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 126d6e48686SLudwig Nussel 127d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 128d6e48686SLudwig Nussel if (!*p) 129d6e48686SLudwig Nussel continue; 130d6e48686SLudwig Nussel 131d6e48686SLudwig Nussel token = match_token(p, tokens, args); 132d6e48686SLudwig Nussel switch (token) { 133d6e48686SLudwig Nussel case Opt_uid: 134d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 135d6e48686SLudwig Nussel return -EINVAL; 1367dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1377dc05881SEric W. Biederman if (!uid_valid(uid)) 1387dc05881SEric W. Biederman return -EINVAL; 1397dc05881SEric W. Biederman opts->uid = uid; 140d6e48686SLudwig Nussel break; 141d6e48686SLudwig Nussel case Opt_gid: 142f1688e04SDave Reisner if (match_int(&args[0], &option)) 143d6e48686SLudwig Nussel return -EINVAL; 1447dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1457dc05881SEric W. Biederman if (!gid_valid(gid)) 1467dc05881SEric W. Biederman return -EINVAL; 1477dc05881SEric W. Biederman opts->gid = gid; 148d6e48686SLudwig Nussel break; 149d6e48686SLudwig Nussel case Opt_mode: 150d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 151d6e48686SLudwig Nussel return -EINVAL; 152d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 153d6e48686SLudwig Nussel break; 154d6e48686SLudwig Nussel /* 155d6e48686SLudwig Nussel * We might like to report bad mount options here; 156d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 157d6e48686SLudwig Nussel */ 158d6e48686SLudwig Nussel } 159d6e48686SLudwig Nussel } 160d6e48686SLudwig Nussel 161d6e48686SLudwig Nussel return 0; 162d6e48686SLudwig Nussel } 163d6e48686SLudwig Nussel 164d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 165d6e48686SLudwig Nussel { 166d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 167d6e48686SLudwig Nussel struct inode *inode = sb->s_root->d_inode; 168d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 169d6e48686SLudwig Nussel 170d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 171d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 172d6e48686SLudwig Nussel 173d6e48686SLudwig Nussel inode->i_uid = opts->uid; 174d6e48686SLudwig Nussel inode->i_gid = opts->gid; 175d6e48686SLudwig Nussel 176d6e48686SLudwig Nussel return 0; 177d6e48686SLudwig Nussel } 178d6e48686SLudwig Nussel 179d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 180d6e48686SLudwig Nussel { 181d6e48686SLudwig Nussel int err; 182d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 183d6e48686SLudwig Nussel 18402b9984dSTheodore Ts'o sync_filesystem(sb); 185d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 186d6e48686SLudwig Nussel if (err) 187d6e48686SLudwig Nussel goto fail; 188d6e48686SLudwig Nussel 189d6e48686SLudwig Nussel debugfs_apply_options(sb); 190d6e48686SLudwig Nussel 191d6e48686SLudwig Nussel fail: 192d6e48686SLudwig Nussel return err; 193d6e48686SLudwig Nussel } 194d6e48686SLudwig Nussel 195d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 196d6e48686SLudwig Nussel { 197d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 198d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 199d6e48686SLudwig Nussel 2007dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 2017dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 2027dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 2037dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 2047dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 2057dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 206d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 207d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 208d6e48686SLudwig Nussel 209d6e48686SLudwig Nussel return 0; 210d6e48686SLudwig Nussel } 211d6e48686SLudwig Nussel 212d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 213d6e48686SLudwig Nussel .statfs = simple_statfs, 214d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 215d6e48686SLudwig Nussel .show_options = debugfs_show_options, 216d6e48686SLudwig Nussel }; 217d6e48686SLudwig Nussel 2181da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds static struct tree_descr debug_files[] = {{""}}; 221d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 222d6e48686SLudwig Nussel int err; 2231da177e4SLinus Torvalds 224d6e48686SLudwig Nussel save_mount_options(sb, data); 225d6e48686SLudwig Nussel 226d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 227d6e48686SLudwig Nussel sb->s_fs_info = fsi; 228d6e48686SLudwig Nussel if (!fsi) { 229d6e48686SLudwig Nussel err = -ENOMEM; 230d6e48686SLudwig Nussel goto fail; 231d6e48686SLudwig Nussel } 232d6e48686SLudwig Nussel 233d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 234d6e48686SLudwig Nussel if (err) 235d6e48686SLudwig Nussel goto fail; 236d6e48686SLudwig Nussel 237d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 238d6e48686SLudwig Nussel if (err) 239d6e48686SLudwig Nussel goto fail; 240d6e48686SLudwig Nussel 241d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 242d6e48686SLudwig Nussel 243d6e48686SLudwig Nussel debugfs_apply_options(sb); 244d6e48686SLudwig Nussel 245d6e48686SLudwig Nussel return 0; 246d6e48686SLudwig Nussel 247d6e48686SLudwig Nussel fail: 248d6e48686SLudwig Nussel kfree(fsi); 249d6e48686SLudwig Nussel sb->s_fs_info = NULL; 250d6e48686SLudwig Nussel return err; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 253fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2541da177e4SLinus Torvalds int flags, const char *dev_name, 255fc14f2feSAl Viro void *data) 2561da177e4SLinus Torvalds { 257fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 2611da177e4SLinus Torvalds .owner = THIS_MODULE, 2621da177e4SLinus Torvalds .name = "debugfs", 263fc14f2feSAl Viro .mount = debug_mount, 2641da177e4SLinus Torvalds .kill_sb = kill_litter_super, 2651da177e4SLinus Torvalds }; 2667f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 2671da177e4SLinus Torvalds 268190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 269c3b1a350SAl Viro { 270190afd81SAl Viro struct dentry *dentry; 271c3b1a350SAl Viro int error; 272c3b1a350SAl Viro 273c3b1a350SAl Viro pr_debug("debugfs: creating file '%s'\n",name); 274c3b1a350SAl Viro 275c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 276c3b1a350SAl Viro &debugfs_mount_count); 277c3b1a350SAl Viro if (error) 278190afd81SAl Viro return ERR_PTR(error); 279c3b1a350SAl Viro 280cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 281cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 282cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 283cfa57c11SAl Viro * have around. 284cfa57c11SAl Viro */ 285cfa57c11SAl Viro if (!parent) 286cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 287cfa57c11SAl Viro 288cfa57c11SAl Viro mutex_lock(&parent->d_inode->i_mutex); 289cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 290190afd81SAl Viro if (!IS_ERR(dentry) && dentry->d_inode) { 291190afd81SAl Viro dput(dentry); 292190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 293190afd81SAl Viro } 294190afd81SAl Viro if (IS_ERR(dentry)) 295190afd81SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 296190afd81SAl Viro return dentry; 297190afd81SAl Viro } 298190afd81SAl Viro 299190afd81SAl Viro static struct dentry *end_creating(struct dentry *dentry, int error) 300190afd81SAl Viro { 301190afd81SAl Viro mutex_unlock(&dentry->d_parent->d_inode->i_mutex); 302190afd81SAl Viro dput(dentry); 303190afd81SAl Viro 304190afd81SAl Viro if (error) { 305190afd81SAl Viro dentry = NULL; 306190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 307190afd81SAl Viro } 308190afd81SAl Viro return dentry; 309190afd81SAl Viro } 310190afd81SAl Viro 3111da177e4SLinus Torvalds /** 3121da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 3131da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 314be030e65SAlberto Bertogli * @mode: the permission that the file should have. 3151da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 316e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3171da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 3181da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 3198e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 3201da177e4SLinus Torvalds * the open() call. 3211da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 3221da177e4SLinus Torvalds * this file. 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 325be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 326be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 3271da177e4SLinus Torvalds * recommended to be used instead.) 3281da177e4SLinus Torvalds * 3291da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 3301da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 3311da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 3326468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 3331da177e4SLinus Torvalds * 3346468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 335873760fbSCornelia Huck * returned. 3361da177e4SLinus Torvalds */ 337f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 3381da177e4SLinus Torvalds struct dentry *parent, void *data, 33999ac48f5SArjan van de Ven const struct file_operations *fops) 3401da177e4SLinus Torvalds { 341ad5abd5bSAl Viro struct dentry *dentry; 342ad5abd5bSAl Viro int error; 343c3b1a350SAl Viro 344ad5abd5bSAl Viro if (!(mode & S_IFMT)) 345ad5abd5bSAl Viro mode |= S_IFREG; 346ad5abd5bSAl Viro BUG_ON(!S_ISREG(mode)); 347ad5abd5bSAl Viro dentry = start_creating(name, parent); 348ad5abd5bSAl Viro 349ad5abd5bSAl Viro if (IS_ERR(dentry)) 350ad5abd5bSAl Viro return NULL; 351ad5abd5bSAl Viro 352*3473cde5SAl Viro error = debugfs_mknod(dentry, mode, data, fops); 353*3473cde5SAl Viro if (!error) 354*3473cde5SAl Viro fsnotify_create(dentry->d_parent->d_inode, dentry); 355ad5abd5bSAl Viro return end_creating(dentry, error); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /** 3601da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 3611da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 3621da177e4SLinus Torvalds * create. 3631da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 364e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3651da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 3661da177e4SLinus Torvalds * 3671da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 3681da177e4SLinus Torvalds * 3691da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 3701da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 3711da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 3726468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 3731da177e4SLinus Torvalds * 3746468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 375873760fbSCornelia Huck * returned. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 3781da177e4SLinus Torvalds { 379ad5abd5bSAl Viro struct dentry *dentry = start_creating(name, parent); 380ad5abd5bSAl Viro int error; 381ad5abd5bSAl Viro 382ad5abd5bSAl Viro if (IS_ERR(dentry)) 383ad5abd5bSAl Viro return NULL; 384ad5abd5bSAl Viro 38502538a75SAl Viro error = debugfs_mknod(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, 38602538a75SAl Viro NULL, NULL); 38702538a75SAl Viro if (!error) { 38802538a75SAl Viro inc_nlink(dentry->d_parent->d_inode); 38902538a75SAl Viro fsnotify_mkdir(dentry->d_parent->d_inode, dentry); 39002538a75SAl Viro } 391ad5abd5bSAl Viro return end_creating(dentry, error); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /** 39666f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 39766f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 39866f54963SPeter Oberparleiter * create. 39966f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 400e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 40166f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 40266f54963SPeter Oberparleiter * filesystem. 40366f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 40466f54963SPeter Oberparleiter * symbolic link. 40566f54963SPeter Oberparleiter * 40666f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 40766f54963SPeter Oberparleiter * links to the given target path. 40866f54963SPeter Oberparleiter * 40966f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 41066f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 41166f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 41266f54963SPeter Oberparleiter * unloaded, you are responsible here.) If an error occurs, %NULL will be 41366f54963SPeter Oberparleiter * returned. 41466f54963SPeter Oberparleiter * 41566f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 416873760fbSCornelia Huck * returned. 41766f54963SPeter Oberparleiter */ 41866f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 41966f54963SPeter Oberparleiter const char *target) 42066f54963SPeter Oberparleiter { 421ad5abd5bSAl Viro struct dentry *dentry; 42266f54963SPeter Oberparleiter char *link; 423ad5abd5bSAl Viro int error; 42466f54963SPeter Oberparleiter 42566f54963SPeter Oberparleiter link = kstrdup(target, GFP_KERNEL); 42666f54963SPeter Oberparleiter if (!link) 42766f54963SPeter Oberparleiter return NULL; 42866f54963SPeter Oberparleiter 429ad5abd5bSAl Viro dentry = start_creating(name, parent); 430ad5abd5bSAl Viro 431ad5abd5bSAl Viro if (IS_ERR(dentry)) { 43266f54963SPeter Oberparleiter kfree(link); 433ad5abd5bSAl Viro return NULL; 434ad5abd5bSAl Viro } 435ad5abd5bSAl Viro 436160f7592SAl Viro error = debugfs_mknod(dentry, S_IFLNK | S_IRWXUGO, link, NULL); 437ad5abd5bSAl Viro if (error) 438ad5abd5bSAl Viro kfree(link); 439ad5abd5bSAl Viro 440ad5abd5bSAl Viro return end_creating(dentry, error); 44166f54963SPeter Oberparleiter } 44266f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 44366f54963SPeter Oberparleiter 44425d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 4451da177e4SLinus Torvalds { 44665c33336SMathieu Desnoyers int ret = 0; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (debugfs_positive(dentry)) { 4491da177e4SLinus Torvalds if (dentry->d_inode) { 45029a7f3adSMathieu Desnoyers dget(dentry); 45166f54963SPeter Oberparleiter switch (dentry->d_inode->i_mode & S_IFMT) { 45266f54963SPeter Oberparleiter case S_IFDIR: 45365c33336SMathieu Desnoyers ret = simple_rmdir(parent->d_inode, dentry); 45466f54963SPeter Oberparleiter break; 45566f54963SPeter Oberparleiter case S_IFLNK: 45666f54963SPeter Oberparleiter kfree(dentry->d_inode->i_private); 45766f54963SPeter Oberparleiter /* fall through */ 45866f54963SPeter Oberparleiter default: 4591da177e4SLinus Torvalds simple_unlink(parent->d_inode, dentry); 46066f54963SPeter Oberparleiter break; 46166f54963SPeter Oberparleiter } 46229a7f3adSMathieu Desnoyers if (!ret) 46329a7f3adSMathieu Desnoyers d_delete(dentry); 46429a7f3adSMathieu Desnoyers dput(dentry); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds } 46725d41d84SJan Kara return ret; 4689505e637SHaavard Skinnemoen } 4699505e637SHaavard Skinnemoen 4709505e637SHaavard Skinnemoen /** 4719505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 4729505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 4739505e637SHaavard Skinnemoen * removed. 4749505e637SHaavard Skinnemoen * 4759505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 4769505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 4779505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 4789505e637SHaavard Skinnemoen * 4799505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 4809505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 4819505e637SHaavard Skinnemoen * removed, you are responsible here. 4829505e637SHaavard Skinnemoen */ 4839505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 4849505e637SHaavard Skinnemoen { 4859505e637SHaavard Skinnemoen struct dentry *parent; 48625d41d84SJan Kara int ret; 4879505e637SHaavard Skinnemoen 488a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 4899505e637SHaavard Skinnemoen return; 4909505e637SHaavard Skinnemoen 4919505e637SHaavard Skinnemoen parent = dentry->d_parent; 4929505e637SHaavard Skinnemoen if (!parent || !parent->d_inode) 4939505e637SHaavard Skinnemoen return; 4949505e637SHaavard Skinnemoen 4959505e637SHaavard Skinnemoen mutex_lock(&parent->d_inode->i_mutex); 49625d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 4971b1dcc1bSJes Sorensen mutex_unlock(&parent->d_inode->i_mutex); 49825d41d84SJan Kara if (!ret) 4991da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 5021da177e4SLinus Torvalds 503cfc94cdfSJan Kara /** 5049505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 5059505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the directory to be removed. 5069505e637SHaavard Skinnemoen * 5079505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 5089505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 5099505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 5109505e637SHaavard Skinnemoen * 5119505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 5129505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 5139505e637SHaavard Skinnemoen * removed, you are responsible here. 5149505e637SHaavard Skinnemoen */ 5159505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 5169505e637SHaavard Skinnemoen { 517485d4402SSteven Rostedt struct dentry *child, *parent; 5189505e637SHaavard Skinnemoen 519a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 5209505e637SHaavard Skinnemoen return; 5219505e637SHaavard Skinnemoen 5229505e637SHaavard Skinnemoen parent = dentry->d_parent; 5239505e637SHaavard Skinnemoen if (!parent || !parent->d_inode) 5249505e637SHaavard Skinnemoen return; 5259505e637SHaavard Skinnemoen 5269505e637SHaavard Skinnemoen parent = dentry; 527776164c1SOleg Nesterov down: 5289505e637SHaavard Skinnemoen mutex_lock(&parent->d_inode->i_mutex); 529485d4402SSteven Rostedt loop: 530485d4402SSteven Rostedt /* 531485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 532485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 533485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 534485d4402SSteven Rostedt */ 535485d4402SSteven Rostedt spin_lock(&parent->d_lock); 536946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 537776164c1SOleg Nesterov if (!debugfs_positive(child)) 538776164c1SOleg Nesterov continue; 5399505e637SHaavard Skinnemoen 540776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 5419505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 542485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 5439505e637SHaavard Skinnemoen mutex_unlock(&parent->d_inode->i_mutex); 5449505e637SHaavard Skinnemoen parent = child; 545776164c1SOleg Nesterov goto down; 5469505e637SHaavard Skinnemoen } 547485d4402SSteven Rostedt 548485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 549485d4402SSteven Rostedt 550776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 551776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 552485d4402SSteven Rostedt 553485d4402SSteven Rostedt /* 554485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 555485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 556485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 557485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 558485d4402SSteven Rostedt * debugfs_positive() check. 559485d4402SSteven Rostedt */ 560485d4402SSteven Rostedt goto loop; 561776164c1SOleg Nesterov } 562485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 563776164c1SOleg Nesterov 564776164c1SOleg Nesterov mutex_unlock(&parent->d_inode->i_mutex); 565776164c1SOleg Nesterov child = parent; 566776164c1SOleg Nesterov parent = parent->d_parent; 567776164c1SOleg Nesterov mutex_lock(&parent->d_inode->i_mutex); 568776164c1SOleg Nesterov 569485d4402SSteven Rostedt if (child != dentry) 570485d4402SSteven Rostedt /* go up */ 571485d4402SSteven Rostedt goto loop; 57256a83cc9SSteven Rostedt 573776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 5749505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 5759505e637SHaavard Skinnemoen mutex_unlock(&parent->d_inode->i_mutex); 5769505e637SHaavard Skinnemoen } 5779505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 5789505e637SHaavard Skinnemoen 5799505e637SHaavard Skinnemoen /** 580cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 581cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 582cfc94cdfSJan Kara * should be a directory dentry. 583cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 584cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 585cfc94cdfSJan Kara * moved. This should be a directory dentry. 586cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 587cfc94cdfSJan Kara * 588cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 589cfc94cdfSJan Kara * exist for rename to succeed. 590cfc94cdfSJan Kara * 591cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 592cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 593cfc94cdfSJan Kara * returned. 594cfc94cdfSJan Kara * 595cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 596cfc94cdfSJan Kara * returned. 597cfc94cdfSJan Kara */ 598cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 599cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 600cfc94cdfSJan Kara { 601cfc94cdfSJan Kara int error; 602cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 603cfc94cdfSJan Kara const char *old_name; 604cfc94cdfSJan Kara 605cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 606cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 607cfc94cdfSJan Kara if (!old_dir->d_inode || !new_dir->d_inode) 608cfc94cdfSJan Kara goto exit; 609cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 610cfc94cdfSJan Kara if (!old_dentry->d_inode || old_dentry == trap || 611cfc94cdfSJan Kara d_mountpoint(old_dentry)) 612cfc94cdfSJan Kara goto exit; 613cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 614cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 615cfc94cdfSJan Kara if (IS_ERR(dentry) || dentry == trap || dentry->d_inode) 616cfc94cdfSJan Kara goto exit; 617cfc94cdfSJan Kara 618cfc94cdfSJan Kara old_name = fsnotify_oldname_init(old_dentry->d_name.name); 619cfc94cdfSJan Kara 620cfc94cdfSJan Kara error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode, 621cfc94cdfSJan Kara dentry); 622cfc94cdfSJan Kara if (error) { 623cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 624cfc94cdfSJan Kara goto exit; 625cfc94cdfSJan Kara } 626cfc94cdfSJan Kara d_move(old_dentry, dentry); 627cfc94cdfSJan Kara fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name, 628123df294SAl Viro S_ISDIR(old_dentry->d_inode->i_mode), 6295a190ae6SAl Viro NULL, old_dentry); 630cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 631cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 632cfc94cdfSJan Kara dput(dentry); 633cfc94cdfSJan Kara return old_dentry; 634cfc94cdfSJan Kara exit: 635cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 636cfc94cdfSJan Kara dput(dentry); 637cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 638cfc94cdfSJan Kara return NULL; 639cfc94cdfSJan Kara } 640cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 641cfc94cdfSJan Kara 642c0f92ba9SFrederic Weisbecker /** 643c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 644c0f92ba9SFrederic Weisbecker */ 645c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 646c0f92ba9SFrederic Weisbecker { 647c0f92ba9SFrederic Weisbecker return debugfs_registered; 648c0f92ba9SFrederic Weisbecker } 649c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 650c0f92ba9SFrederic Weisbecker 651c0f92ba9SFrederic Weisbecker 652191e186bSGreg Kroah-Hartman static struct kobject *debug_kobj; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds static int __init debugfs_init(void) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds int retval; 6571da177e4SLinus Torvalds 6580ff21e46SGreg Kroah-Hartman debug_kobj = kobject_create_and_add("debug", kernel_kobj); 659191e186bSGreg Kroah-Hartman if (!debug_kobj) 660191e186bSGreg Kroah-Hartman return -EINVAL; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 6631da177e4SLinus Torvalds if (retval) 664197b12d6SGreg Kroah-Hartman kobject_put(debug_kobj); 665c0f92ba9SFrederic Weisbecker else 666c0f92ba9SFrederic Weisbecker debugfs_registered = true; 667c0f92ba9SFrederic Weisbecker 6681da177e4SLinus Torvalds return retval; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds core_initcall(debugfs_init); 6711da177e4SLinus Torvalds 672