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 */ 731da177e4SLinus Torvalds static int debugfs_mknod(struct inode *dir, struct dentry *dentry, 74f4ae40a6SAl Viro umode_t mode, dev_t dev, void *data, 75d3a3b0adSMathieu Desnoyers const struct file_operations *fops) 761da177e4SLinus Torvalds { 7771601e2bSJens Axboe struct inode *inode; 781da177e4SLinus Torvalds int error = -EPERM; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds if (dentry->d_inode) 811da177e4SLinus Torvalds return -EEXIST; 821da177e4SLinus Torvalds 83d3a3b0adSMathieu Desnoyers inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops); 841da177e4SLinus Torvalds if (inode) { 851da177e4SLinus Torvalds d_instantiate(dentry, inode); 861da177e4SLinus Torvalds dget(dentry); 871da177e4SLinus Torvalds error = 0; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds return error; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 92e09ddf36SAl Viro static int debugfs_mkdir(struct dentry *dentry, umode_t mode) 931da177e4SLinus Torvalds { 94e09ddf36SAl Viro struct inode *dir = dentry->d_parent->d_inode; 951da177e4SLinus Torvalds int res; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR; 98ac481d6cSAl Viro res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL); 994f36557fSMathieu Desnoyers if (!res) { 100d8c76e6fSDave Hansen inc_nlink(dir); 1014f36557fSMathieu Desnoyers fsnotify_mkdir(dir, dentry); 1024f36557fSMathieu Desnoyers } 1031da177e4SLinus Torvalds return res; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 106e09ddf36SAl Viro static int debugfs_link(struct dentry *dentry, umode_t mode, 107ac481d6cSAl Viro void *data) 10866f54963SPeter Oberparleiter { 109e09ddf36SAl Viro struct inode *dir = dentry->d_parent->d_inode; 11066f54963SPeter Oberparleiter mode = (mode & S_IALLUGO) | S_IFLNK; 111ac481d6cSAl Viro return debugfs_mknod(dir, dentry, mode, 0, data, NULL); 11266f54963SPeter Oberparleiter } 11366f54963SPeter Oberparleiter 114e09ddf36SAl Viro static int debugfs_create(struct dentry *dentry, umode_t mode, 115d3a3b0adSMathieu Desnoyers void *data, const struct file_operations *fops) 1161da177e4SLinus Torvalds { 117e09ddf36SAl Viro struct inode *dir = dentry->d_parent->d_inode; 1184f36557fSMathieu Desnoyers int res; 1194f36557fSMathieu Desnoyers 1201da177e4SLinus Torvalds mode = (mode & S_IALLUGO) | S_IFREG; 121d3a3b0adSMathieu Desnoyers res = debugfs_mknod(dir, dentry, mode, 0, data, fops); 1224f36557fSMathieu Desnoyers if (!res) 1234f36557fSMathieu Desnoyers fsnotify_create(dir, dentry); 1244f36557fSMathieu Desnoyers return res; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline int debugfs_positive(struct dentry *dentry) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds return dentry->d_inode && !d_unhashed(dentry); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 132d6e48686SLudwig Nussel struct debugfs_mount_opts { 1337dc05881SEric W. Biederman kuid_t uid; 1347dc05881SEric W. Biederman kgid_t gid; 135d6e48686SLudwig Nussel umode_t mode; 136d6e48686SLudwig Nussel }; 137d6e48686SLudwig Nussel 138d6e48686SLudwig Nussel enum { 139d6e48686SLudwig Nussel Opt_uid, 140d6e48686SLudwig Nussel Opt_gid, 141d6e48686SLudwig Nussel Opt_mode, 142d6e48686SLudwig Nussel Opt_err 143d6e48686SLudwig Nussel }; 144d6e48686SLudwig Nussel 145d6e48686SLudwig Nussel static const match_table_t tokens = { 146d6e48686SLudwig Nussel {Opt_uid, "uid=%u"}, 147d6e48686SLudwig Nussel {Opt_gid, "gid=%u"}, 148d6e48686SLudwig Nussel {Opt_mode, "mode=%o"}, 149d6e48686SLudwig Nussel {Opt_err, NULL} 150d6e48686SLudwig Nussel }; 151d6e48686SLudwig Nussel 152d6e48686SLudwig Nussel struct debugfs_fs_info { 153d6e48686SLudwig Nussel struct debugfs_mount_opts mount_opts; 154d6e48686SLudwig Nussel }; 155d6e48686SLudwig Nussel 156d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) 157d6e48686SLudwig Nussel { 158d6e48686SLudwig Nussel substring_t args[MAX_OPT_ARGS]; 159d6e48686SLudwig Nussel int option; 160d6e48686SLudwig Nussel int token; 1617dc05881SEric W. Biederman kuid_t uid; 1627dc05881SEric W. Biederman kgid_t gid; 163d6e48686SLudwig Nussel char *p; 164d6e48686SLudwig Nussel 165d6e48686SLudwig Nussel opts->mode = DEBUGFS_DEFAULT_MODE; 166d6e48686SLudwig Nussel 167d6e48686SLudwig Nussel while ((p = strsep(&data, ",")) != NULL) { 168d6e48686SLudwig Nussel if (!*p) 169d6e48686SLudwig Nussel continue; 170d6e48686SLudwig Nussel 171d6e48686SLudwig Nussel token = match_token(p, tokens, args); 172d6e48686SLudwig Nussel switch (token) { 173d6e48686SLudwig Nussel case Opt_uid: 174d6e48686SLudwig Nussel if (match_int(&args[0], &option)) 175d6e48686SLudwig Nussel return -EINVAL; 1767dc05881SEric W. Biederman uid = make_kuid(current_user_ns(), option); 1777dc05881SEric W. Biederman if (!uid_valid(uid)) 1787dc05881SEric W. Biederman return -EINVAL; 1797dc05881SEric W. Biederman opts->uid = uid; 180d6e48686SLudwig Nussel break; 181d6e48686SLudwig Nussel case Opt_gid: 182f1688e04SDave Reisner if (match_int(&args[0], &option)) 183d6e48686SLudwig Nussel return -EINVAL; 1847dc05881SEric W. Biederman gid = make_kgid(current_user_ns(), option); 1857dc05881SEric W. Biederman if (!gid_valid(gid)) 1867dc05881SEric W. Biederman return -EINVAL; 1877dc05881SEric W. Biederman opts->gid = gid; 188d6e48686SLudwig Nussel break; 189d6e48686SLudwig Nussel case Opt_mode: 190d6e48686SLudwig Nussel if (match_octal(&args[0], &option)) 191d6e48686SLudwig Nussel return -EINVAL; 192d6e48686SLudwig Nussel opts->mode = option & S_IALLUGO; 193d6e48686SLudwig Nussel break; 194d6e48686SLudwig Nussel /* 195d6e48686SLudwig Nussel * We might like to report bad mount options here; 196d6e48686SLudwig Nussel * but traditionally debugfs has ignored all mount options 197d6e48686SLudwig Nussel */ 198d6e48686SLudwig Nussel } 199d6e48686SLudwig Nussel } 200d6e48686SLudwig Nussel 201d6e48686SLudwig Nussel return 0; 202d6e48686SLudwig Nussel } 203d6e48686SLudwig Nussel 204d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb) 205d6e48686SLudwig Nussel { 206d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 207d6e48686SLudwig Nussel struct inode *inode = sb->s_root->d_inode; 208d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 209d6e48686SLudwig Nussel 210d6e48686SLudwig Nussel inode->i_mode &= ~S_IALLUGO; 211d6e48686SLudwig Nussel inode->i_mode |= opts->mode; 212d6e48686SLudwig Nussel 213d6e48686SLudwig Nussel inode->i_uid = opts->uid; 214d6e48686SLudwig Nussel inode->i_gid = opts->gid; 215d6e48686SLudwig Nussel 216d6e48686SLudwig Nussel return 0; 217d6e48686SLudwig Nussel } 218d6e48686SLudwig Nussel 219d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data) 220d6e48686SLudwig Nussel { 221d6e48686SLudwig Nussel int err; 222d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = sb->s_fs_info; 223d6e48686SLudwig Nussel 22402b9984dSTheodore Ts'o sync_filesystem(sb); 225d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 226d6e48686SLudwig Nussel if (err) 227d6e48686SLudwig Nussel goto fail; 228d6e48686SLudwig Nussel 229d6e48686SLudwig Nussel debugfs_apply_options(sb); 230d6e48686SLudwig Nussel 231d6e48686SLudwig Nussel fail: 232d6e48686SLudwig Nussel return err; 233d6e48686SLudwig Nussel } 234d6e48686SLudwig Nussel 235d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root) 236d6e48686SLudwig Nussel { 237d6e48686SLudwig Nussel struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; 238d6e48686SLudwig Nussel struct debugfs_mount_opts *opts = &fsi->mount_opts; 239d6e48686SLudwig Nussel 2407dc05881SEric W. Biederman if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) 2417dc05881SEric W. Biederman seq_printf(m, ",uid=%u", 2427dc05881SEric W. Biederman from_kuid_munged(&init_user_ns, opts->uid)); 2437dc05881SEric W. Biederman if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) 2447dc05881SEric W. Biederman seq_printf(m, ",gid=%u", 2457dc05881SEric W. Biederman from_kgid_munged(&init_user_ns, opts->gid)); 246d6e48686SLudwig Nussel if (opts->mode != DEBUGFS_DEFAULT_MODE) 247d6e48686SLudwig Nussel seq_printf(m, ",mode=%o", opts->mode); 248d6e48686SLudwig Nussel 249d6e48686SLudwig Nussel return 0; 250d6e48686SLudwig Nussel } 251d6e48686SLudwig Nussel 252d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = { 253d6e48686SLudwig Nussel .statfs = simple_statfs, 254d6e48686SLudwig Nussel .remount_fs = debugfs_remount, 255d6e48686SLudwig Nussel .show_options = debugfs_show_options, 256d6e48686SLudwig Nussel }; 257d6e48686SLudwig Nussel 2581da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds static struct tree_descr debug_files[] = {{""}}; 261d6e48686SLudwig Nussel struct debugfs_fs_info *fsi; 262d6e48686SLudwig Nussel int err; 2631da177e4SLinus Torvalds 264d6e48686SLudwig Nussel save_mount_options(sb, data); 265d6e48686SLudwig Nussel 266d6e48686SLudwig Nussel fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); 267d6e48686SLudwig Nussel sb->s_fs_info = fsi; 268d6e48686SLudwig Nussel if (!fsi) { 269d6e48686SLudwig Nussel err = -ENOMEM; 270d6e48686SLudwig Nussel goto fail; 271d6e48686SLudwig Nussel } 272d6e48686SLudwig Nussel 273d6e48686SLudwig Nussel err = debugfs_parse_options(data, &fsi->mount_opts); 274d6e48686SLudwig Nussel if (err) 275d6e48686SLudwig Nussel goto fail; 276d6e48686SLudwig Nussel 277d6e48686SLudwig Nussel err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); 278d6e48686SLudwig Nussel if (err) 279d6e48686SLudwig Nussel goto fail; 280d6e48686SLudwig Nussel 281d6e48686SLudwig Nussel sb->s_op = &debugfs_super_operations; 282d6e48686SLudwig Nussel 283d6e48686SLudwig Nussel debugfs_apply_options(sb); 284d6e48686SLudwig Nussel 285d6e48686SLudwig Nussel return 0; 286d6e48686SLudwig Nussel 287d6e48686SLudwig Nussel fail: 288d6e48686SLudwig Nussel kfree(fsi); 289d6e48686SLudwig Nussel sb->s_fs_info = NULL; 290d6e48686SLudwig Nussel return err; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 293fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type, 2941da177e4SLinus Torvalds int flags, const char *dev_name, 295fc14f2feSAl Viro void *data) 2961da177e4SLinus Torvalds { 297fc14f2feSAl Viro return mount_single(fs_type, flags, data, debug_fill_super); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static struct file_system_type debug_fs_type = { 3011da177e4SLinus Torvalds .owner = THIS_MODULE, 3021da177e4SLinus Torvalds .name = "debugfs", 303fc14f2feSAl Viro .mount = debug_mount, 3041da177e4SLinus Torvalds .kill_sb = kill_litter_super, 3051da177e4SLinus Torvalds }; 3067f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs"); 3071da177e4SLinus Torvalds 308*190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent) 309c3b1a350SAl Viro { 310*190afd81SAl Viro struct dentry *dentry; 311c3b1a350SAl Viro int error; 312c3b1a350SAl Viro 313c3b1a350SAl Viro pr_debug("debugfs: creating file '%s'\n",name); 314c3b1a350SAl Viro 315c3b1a350SAl Viro error = simple_pin_fs(&debug_fs_type, &debugfs_mount, 316c3b1a350SAl Viro &debugfs_mount_count); 317c3b1a350SAl Viro if (error) 318*190afd81SAl Viro return ERR_PTR(error); 319c3b1a350SAl Viro 320cfa57c11SAl Viro /* If the parent is not specified, we create it in the root. 321cfa57c11SAl Viro * We need the root dentry to do this, which is in the super 322cfa57c11SAl Viro * block. A pointer to that is in the struct vfsmount that we 323cfa57c11SAl Viro * have around. 324cfa57c11SAl Viro */ 325cfa57c11SAl Viro if (!parent) 326cfa57c11SAl Viro parent = debugfs_mount->mnt_root; 327cfa57c11SAl Viro 328cfa57c11SAl Viro mutex_lock(&parent->d_inode->i_mutex); 329cfa57c11SAl Viro dentry = lookup_one_len(name, parent, strlen(name)); 330*190afd81SAl Viro if (!IS_ERR(dentry) && dentry->d_inode) { 331*190afd81SAl Viro dput(dentry); 332*190afd81SAl Viro dentry = ERR_PTR(-EEXIST); 333*190afd81SAl Viro } 334*190afd81SAl Viro if (IS_ERR(dentry)) 335*190afd81SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 336*190afd81SAl Viro return dentry; 337*190afd81SAl Viro } 338*190afd81SAl Viro 339*190afd81SAl Viro static struct dentry *end_creating(struct dentry *dentry, int error) 340*190afd81SAl Viro { 341*190afd81SAl Viro mutex_unlock(&dentry->d_parent->d_inode->i_mutex); 342*190afd81SAl Viro dput(dentry); 343*190afd81SAl Viro 344*190afd81SAl Viro if (error) { 345*190afd81SAl Viro dentry = NULL; 346*190afd81SAl Viro simple_release_fs(&debugfs_mount, &debugfs_mount_count); 347*190afd81SAl Viro } 348*190afd81SAl Viro return dentry; 349*190afd81SAl Viro } 350*190afd81SAl Viro 351*190afd81SAl Viro static struct dentry *__create_file(const char *name, umode_t mode, 352*190afd81SAl Viro struct dentry *parent, void *data, 353*190afd81SAl Viro const struct file_operations *fops) 354*190afd81SAl Viro { 355*190afd81SAl Viro struct dentry *dentry = start_creating(name, parent); 356*190afd81SAl Viro int error; 357*190afd81SAl Viro 358*190afd81SAl Viro if (IS_ERR(dentry)) 359*190afd81SAl Viro return NULL; 360*190afd81SAl Viro 361cfa57c11SAl Viro switch (mode & S_IFMT) { 362cfa57c11SAl Viro case S_IFDIR: 363e09ddf36SAl Viro error = debugfs_mkdir(dentry, mode); 364cfa57c11SAl Viro break; 365cfa57c11SAl Viro case S_IFLNK: 366e09ddf36SAl Viro error = debugfs_link(dentry, mode, data); 367cfa57c11SAl Viro break; 368cfa57c11SAl Viro default: 369e09ddf36SAl Viro error = debugfs_create(dentry, mode, data, fops); 370cfa57c11SAl Viro break; 371cfa57c11SAl Viro } 372*190afd81SAl Viro return end_creating(dentry, error); 373c3b1a350SAl Viro } 374c3b1a350SAl Viro 3751da177e4SLinus Torvalds /** 3761da177e4SLinus Torvalds * debugfs_create_file - create a file in the debugfs filesystem 3771da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 378be030e65SAlberto Bertogli * @mode: the permission that the file should have. 3791da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 380e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 3811da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 3821da177e4SLinus Torvalds * @data: a pointer to something that the caller will want to get to later 3838e18e294STheodore Ts'o * on. The inode.i_private pointer will point to this value on 3841da177e4SLinus Torvalds * the open() call. 3851da177e4SLinus Torvalds * @fops: a pointer to a struct file_operations that should be used for 3861da177e4SLinus Torvalds * this file. 3871da177e4SLinus Torvalds * 3881da177e4SLinus Torvalds * This is the basic "create a file" function for debugfs. It allows for a 389be030e65SAlberto Bertogli * wide range of flexibility in creating a file, or a directory (if you want 390be030e65SAlberto Bertogli * to create a directory, the debugfs_create_dir() function is 3911da177e4SLinus Torvalds * recommended to be used instead.) 3921da177e4SLinus Torvalds * 3931da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 3941da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 3951da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 3966468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 3971da177e4SLinus Torvalds * 3986468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 399873760fbSCornelia Huck * returned. 4001da177e4SLinus Torvalds */ 401f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode, 4021da177e4SLinus Torvalds struct dentry *parent, void *data, 40399ac48f5SArjan van de Ven const struct file_operations *fops) 4041da177e4SLinus Torvalds { 405c3b1a350SAl Viro switch (mode & S_IFMT) { 406c3b1a350SAl Viro case S_IFREG: 407c3b1a350SAl Viro case 0: 408c3b1a350SAl Viro break; 409c3b1a350SAl Viro default: 410c3b1a350SAl Viro BUG(); 4111da177e4SLinus Torvalds } 412c3b1a350SAl Viro 413c3b1a350SAl Viro return __create_file(name, mode, parent, data, fops); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file); 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /** 4181da177e4SLinus Torvalds * debugfs_create_dir - create a directory in the debugfs filesystem 4191da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the directory to 4201da177e4SLinus Torvalds * create. 4211da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 422e227867fSMasanari Iida * directory dentry if set. If this parameter is NULL, then the 4231da177e4SLinus Torvalds * directory will be created in the root of the debugfs filesystem. 4241da177e4SLinus Torvalds * 4251da177e4SLinus Torvalds * This function creates a directory in debugfs with the given name. 4261da177e4SLinus Torvalds * 4271da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4281da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4291da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4306468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4311da177e4SLinus Torvalds * 4326468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 433873760fbSCornelia Huck * returned. 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 4361da177e4SLinus Torvalds { 437c3b1a350SAl Viro return __create_file(name, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, 4381da177e4SLinus Torvalds parent, NULL, NULL); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /** 44366f54963SPeter Oberparleiter * debugfs_create_symlink- create a symbolic link in the debugfs filesystem 44466f54963SPeter Oberparleiter * @name: a pointer to a string containing the name of the symbolic link to 44566f54963SPeter Oberparleiter * create. 44666f54963SPeter Oberparleiter * @parent: a pointer to the parent dentry for this symbolic link. This 447e227867fSMasanari Iida * should be a directory dentry if set. If this parameter is NULL, 44866f54963SPeter Oberparleiter * then the symbolic link will be created in the root of the debugfs 44966f54963SPeter Oberparleiter * filesystem. 45066f54963SPeter Oberparleiter * @target: a pointer to a string containing the path to the target of the 45166f54963SPeter Oberparleiter * symbolic link. 45266f54963SPeter Oberparleiter * 45366f54963SPeter Oberparleiter * This function creates a symbolic link with the given name in debugfs that 45466f54963SPeter Oberparleiter * links to the given target path. 45566f54963SPeter Oberparleiter * 45666f54963SPeter Oberparleiter * This function will return a pointer to a dentry if it succeeds. This 45766f54963SPeter Oberparleiter * pointer must be passed to the debugfs_remove() function when the symbolic 45866f54963SPeter Oberparleiter * link is to be removed (no automatic cleanup happens if your module is 45966f54963SPeter Oberparleiter * unloaded, you are responsible here.) If an error occurs, %NULL will be 46066f54963SPeter Oberparleiter * returned. 46166f54963SPeter Oberparleiter * 46266f54963SPeter Oberparleiter * If debugfs is not enabled in the kernel, the value -%ENODEV will be 463873760fbSCornelia Huck * returned. 46466f54963SPeter Oberparleiter */ 46566f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 46666f54963SPeter Oberparleiter const char *target) 46766f54963SPeter Oberparleiter { 46866f54963SPeter Oberparleiter struct dentry *result; 46966f54963SPeter Oberparleiter char *link; 47066f54963SPeter Oberparleiter 47166f54963SPeter Oberparleiter link = kstrdup(target, GFP_KERNEL); 47266f54963SPeter Oberparleiter if (!link) 47366f54963SPeter Oberparleiter return NULL; 47466f54963SPeter Oberparleiter 475c3b1a350SAl Viro result = __create_file(name, S_IFLNK | S_IRWXUGO, parent, link, NULL); 47666f54963SPeter Oberparleiter if (!result) 47766f54963SPeter Oberparleiter kfree(link); 47866f54963SPeter Oberparleiter return result; 47966f54963SPeter Oberparleiter } 48066f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink); 48166f54963SPeter Oberparleiter 48225d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) 4831da177e4SLinus Torvalds { 48465c33336SMathieu Desnoyers int ret = 0; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (debugfs_positive(dentry)) { 4871da177e4SLinus Torvalds if (dentry->d_inode) { 48829a7f3adSMathieu Desnoyers dget(dentry); 48966f54963SPeter Oberparleiter switch (dentry->d_inode->i_mode & S_IFMT) { 49066f54963SPeter Oberparleiter case S_IFDIR: 49165c33336SMathieu Desnoyers ret = simple_rmdir(parent->d_inode, dentry); 49266f54963SPeter Oberparleiter break; 49366f54963SPeter Oberparleiter case S_IFLNK: 49466f54963SPeter Oberparleiter kfree(dentry->d_inode->i_private); 49566f54963SPeter Oberparleiter /* fall through */ 49666f54963SPeter Oberparleiter default: 4971da177e4SLinus Torvalds simple_unlink(parent->d_inode, dentry); 49866f54963SPeter Oberparleiter break; 49966f54963SPeter Oberparleiter } 50029a7f3adSMathieu Desnoyers if (!ret) 50129a7f3adSMathieu Desnoyers d_delete(dentry); 50229a7f3adSMathieu Desnoyers dput(dentry); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds } 50525d41d84SJan Kara return ret; 5069505e637SHaavard Skinnemoen } 5079505e637SHaavard Skinnemoen 5089505e637SHaavard Skinnemoen /** 5099505e637SHaavard Skinnemoen * debugfs_remove - removes a file or directory from the debugfs filesystem 5109505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the file or directory to be 5119505e637SHaavard Skinnemoen * removed. 5129505e637SHaavard Skinnemoen * 5139505e637SHaavard Skinnemoen * This function removes a file or directory in debugfs that was previously 5149505e637SHaavard Skinnemoen * created with a call to another debugfs function (like 5159505e637SHaavard Skinnemoen * debugfs_create_file() or variants thereof.) 5169505e637SHaavard Skinnemoen * 5179505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 5189505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 5199505e637SHaavard Skinnemoen * removed, you are responsible here. 5209505e637SHaavard Skinnemoen */ 5219505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry) 5229505e637SHaavard Skinnemoen { 5239505e637SHaavard Skinnemoen struct dentry *parent; 52425d41d84SJan Kara int ret; 5259505e637SHaavard Skinnemoen 526a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 5279505e637SHaavard Skinnemoen return; 5289505e637SHaavard Skinnemoen 5299505e637SHaavard Skinnemoen parent = dentry->d_parent; 5309505e637SHaavard Skinnemoen if (!parent || !parent->d_inode) 5319505e637SHaavard Skinnemoen return; 5329505e637SHaavard Skinnemoen 5339505e637SHaavard Skinnemoen mutex_lock(&parent->d_inode->i_mutex); 53425d41d84SJan Kara ret = __debugfs_remove(dentry, parent); 5351b1dcc1bSJes Sorensen mutex_unlock(&parent->d_inode->i_mutex); 53625d41d84SJan Kara if (!ret) 5371da177e4SLinus Torvalds simple_release_fs(&debugfs_mount, &debugfs_mount_count); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove); 5401da177e4SLinus Torvalds 541cfc94cdfSJan Kara /** 5429505e637SHaavard Skinnemoen * debugfs_remove_recursive - recursively removes a directory 5439505e637SHaavard Skinnemoen * @dentry: a pointer to a the dentry of the directory to be removed. 5449505e637SHaavard Skinnemoen * 5459505e637SHaavard Skinnemoen * This function recursively removes a directory tree in debugfs that 5469505e637SHaavard Skinnemoen * was previously created with a call to another debugfs function 5479505e637SHaavard Skinnemoen * (like debugfs_create_file() or variants thereof.) 5489505e637SHaavard Skinnemoen * 5499505e637SHaavard Skinnemoen * This function is required to be called in order for the file to be 5509505e637SHaavard Skinnemoen * removed, no automatic cleanup of files will happen when a module is 5519505e637SHaavard Skinnemoen * removed, you are responsible here. 5529505e637SHaavard Skinnemoen */ 5539505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry) 5549505e637SHaavard Skinnemoen { 555485d4402SSteven Rostedt struct dentry *child, *parent; 5569505e637SHaavard Skinnemoen 557a59d6293SArend van Spriel if (IS_ERR_OR_NULL(dentry)) 5589505e637SHaavard Skinnemoen return; 5599505e637SHaavard Skinnemoen 5609505e637SHaavard Skinnemoen parent = dentry->d_parent; 5619505e637SHaavard Skinnemoen if (!parent || !parent->d_inode) 5629505e637SHaavard Skinnemoen return; 5639505e637SHaavard Skinnemoen 5649505e637SHaavard Skinnemoen parent = dentry; 565776164c1SOleg Nesterov down: 5669505e637SHaavard Skinnemoen mutex_lock(&parent->d_inode->i_mutex); 567485d4402SSteven Rostedt loop: 568485d4402SSteven Rostedt /* 569485d4402SSteven Rostedt * The parent->d_subdirs is protected by the d_lock. Outside that 570485d4402SSteven Rostedt * lock, the child can be unlinked and set to be freed which can 571485d4402SSteven Rostedt * use the d_u.d_child as the rcu head and corrupt this list. 572485d4402SSteven Rostedt */ 573485d4402SSteven Rostedt spin_lock(&parent->d_lock); 574946e51f2SAl Viro list_for_each_entry(child, &parent->d_subdirs, d_child) { 575776164c1SOleg Nesterov if (!debugfs_positive(child)) 576776164c1SOleg Nesterov continue; 5779505e637SHaavard Skinnemoen 578776164c1SOleg Nesterov /* perhaps simple_empty(child) makes more sense */ 5799505e637SHaavard Skinnemoen if (!list_empty(&child->d_subdirs)) { 580485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 5819505e637SHaavard Skinnemoen mutex_unlock(&parent->d_inode->i_mutex); 5829505e637SHaavard Skinnemoen parent = child; 583776164c1SOleg Nesterov goto down; 5849505e637SHaavard Skinnemoen } 585485d4402SSteven Rostedt 586485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 587485d4402SSteven Rostedt 588776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 589776164c1SOleg Nesterov simple_release_fs(&debugfs_mount, &debugfs_mount_count); 590485d4402SSteven Rostedt 591485d4402SSteven Rostedt /* 592485d4402SSteven Rostedt * The parent->d_lock protects agaist child from unlinking 593485d4402SSteven Rostedt * from d_subdirs. When releasing the parent->d_lock we can 594485d4402SSteven Rostedt * no longer trust that the next pointer is valid. 595485d4402SSteven Rostedt * Restart the loop. We'll skip this one with the 596485d4402SSteven Rostedt * debugfs_positive() check. 597485d4402SSteven Rostedt */ 598485d4402SSteven Rostedt goto loop; 599776164c1SOleg Nesterov } 600485d4402SSteven Rostedt spin_unlock(&parent->d_lock); 601776164c1SOleg Nesterov 602776164c1SOleg Nesterov mutex_unlock(&parent->d_inode->i_mutex); 603776164c1SOleg Nesterov child = parent; 604776164c1SOleg Nesterov parent = parent->d_parent; 605776164c1SOleg Nesterov mutex_lock(&parent->d_inode->i_mutex); 606776164c1SOleg Nesterov 607485d4402SSteven Rostedt if (child != dentry) 608485d4402SSteven Rostedt /* go up */ 609485d4402SSteven Rostedt goto loop; 61056a83cc9SSteven Rostedt 611776164c1SOleg Nesterov if (!__debugfs_remove(child, parent)) 6129505e637SHaavard Skinnemoen simple_release_fs(&debugfs_mount, &debugfs_mount_count); 6139505e637SHaavard Skinnemoen mutex_unlock(&parent->d_inode->i_mutex); 6149505e637SHaavard Skinnemoen } 6159505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive); 6169505e637SHaavard Skinnemoen 6179505e637SHaavard Skinnemoen /** 618cfc94cdfSJan Kara * debugfs_rename - rename a file/directory in the debugfs filesystem 619cfc94cdfSJan Kara * @old_dir: a pointer to the parent dentry for the renamed object. This 620cfc94cdfSJan Kara * should be a directory dentry. 621cfc94cdfSJan Kara * @old_dentry: dentry of an object to be renamed. 622cfc94cdfSJan Kara * @new_dir: a pointer to the parent dentry where the object should be 623cfc94cdfSJan Kara * moved. This should be a directory dentry. 624cfc94cdfSJan Kara * @new_name: a pointer to a string containing the target name. 625cfc94cdfSJan Kara * 626cfc94cdfSJan Kara * This function renames a file/directory in debugfs. The target must not 627cfc94cdfSJan Kara * exist for rename to succeed. 628cfc94cdfSJan Kara * 629cfc94cdfSJan Kara * This function will return a pointer to old_dentry (which is updated to 630cfc94cdfSJan Kara * reflect renaming) if it succeeds. If an error occurs, %NULL will be 631cfc94cdfSJan Kara * returned. 632cfc94cdfSJan Kara * 633cfc94cdfSJan Kara * If debugfs is not enabled in the kernel, the value -%ENODEV will be 634cfc94cdfSJan Kara * returned. 635cfc94cdfSJan Kara */ 636cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 637cfc94cdfSJan Kara struct dentry *new_dir, const char *new_name) 638cfc94cdfSJan Kara { 639cfc94cdfSJan Kara int error; 640cfc94cdfSJan Kara struct dentry *dentry = NULL, *trap; 641cfc94cdfSJan Kara const char *old_name; 642cfc94cdfSJan Kara 643cfc94cdfSJan Kara trap = lock_rename(new_dir, old_dir); 644cfc94cdfSJan Kara /* Source or destination directories don't exist? */ 645cfc94cdfSJan Kara if (!old_dir->d_inode || !new_dir->d_inode) 646cfc94cdfSJan Kara goto exit; 647cfc94cdfSJan Kara /* Source does not exist, cyclic rename, or mountpoint? */ 648cfc94cdfSJan Kara if (!old_dentry->d_inode || old_dentry == trap || 649cfc94cdfSJan Kara d_mountpoint(old_dentry)) 650cfc94cdfSJan Kara goto exit; 651cfc94cdfSJan Kara dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); 652cfc94cdfSJan Kara /* Lookup failed, cyclic rename or target exists? */ 653cfc94cdfSJan Kara if (IS_ERR(dentry) || dentry == trap || dentry->d_inode) 654cfc94cdfSJan Kara goto exit; 655cfc94cdfSJan Kara 656cfc94cdfSJan Kara old_name = fsnotify_oldname_init(old_dentry->d_name.name); 657cfc94cdfSJan Kara 658cfc94cdfSJan Kara error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode, 659cfc94cdfSJan Kara dentry); 660cfc94cdfSJan Kara if (error) { 661cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 662cfc94cdfSJan Kara goto exit; 663cfc94cdfSJan Kara } 664cfc94cdfSJan Kara d_move(old_dentry, dentry); 665cfc94cdfSJan Kara fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name, 666123df294SAl Viro S_ISDIR(old_dentry->d_inode->i_mode), 6675a190ae6SAl Viro NULL, old_dentry); 668cfc94cdfSJan Kara fsnotify_oldname_free(old_name); 669cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 670cfc94cdfSJan Kara dput(dentry); 671cfc94cdfSJan Kara return old_dentry; 672cfc94cdfSJan Kara exit: 673cfc94cdfSJan Kara if (dentry && !IS_ERR(dentry)) 674cfc94cdfSJan Kara dput(dentry); 675cfc94cdfSJan Kara unlock_rename(new_dir, old_dir); 676cfc94cdfSJan Kara return NULL; 677cfc94cdfSJan Kara } 678cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename); 679cfc94cdfSJan Kara 680c0f92ba9SFrederic Weisbecker /** 681c0f92ba9SFrederic Weisbecker * debugfs_initialized - Tells whether debugfs has been registered 682c0f92ba9SFrederic Weisbecker */ 683c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void) 684c0f92ba9SFrederic Weisbecker { 685c0f92ba9SFrederic Weisbecker return debugfs_registered; 686c0f92ba9SFrederic Weisbecker } 687c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized); 688c0f92ba9SFrederic Weisbecker 689c0f92ba9SFrederic Weisbecker 690191e186bSGreg Kroah-Hartman static struct kobject *debug_kobj; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds static int __init debugfs_init(void) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds int retval; 6951da177e4SLinus Torvalds 6960ff21e46SGreg Kroah-Hartman debug_kobj = kobject_create_and_add("debug", kernel_kobj); 697191e186bSGreg Kroah-Hartman if (!debug_kobj) 698191e186bSGreg Kroah-Hartman return -EINVAL; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds retval = register_filesystem(&debug_fs_type); 7011da177e4SLinus Torvalds if (retval) 702197b12d6SGreg Kroah-Hartman kobject_put(debug_kobj); 703c0f92ba9SFrederic Weisbecker else 704c0f92ba9SFrederic Weisbecker debugfs_registered = true; 705c0f92ba9SFrederic Weisbecker 7061da177e4SLinus Torvalds return retval; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds core_initcall(debugfs_init); 7091da177e4SLinus Torvalds 710