Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Harry Wei | bd33d12 | 2011-07-16 16:45:13 +0800 | [diff] [blame] | 2 | * inode.c - part of debugfs, a tiny little debug file system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> |
| 5 | * Copyright (C) 2004 IBM Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * debugfs is for people to use instead of /proc or /sys. |
| 12 | * See Documentation/DocBook/kernel-api for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mount.h> |
| 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/init.h> |
Randy Dunlap | 4d8ebdd | 2006-11-25 11:09:26 -0800 | [diff] [blame] | 21 | #include <linux/kobject.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/namei.h> |
| 23 | #include <linux/debugfs.h> |
Mathieu Desnoyers | 4f36557 | 2006-11-24 13:45:37 -0500 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 25 | #include <linux/string.h> |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/parser.h> |
Mimi Zohar | 9256292 | 2008-10-07 14:00:12 -0400 | [diff] [blame] | 28 | #include <linux/magic.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Kees Cook | 82aceae4 | 2012-08-27 13:32:15 -0700 | [diff] [blame] | 31 | #define DEBUGFS_DEFAULT_MODE 0700 |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | static struct vfsmount *debugfs_mount; |
| 34 | static int debugfs_mount_count; |
Frederic Weisbecker | c0f92ba | 2009-03-22 23:10:44 +0100 | [diff] [blame] | 35 | static bool debugfs_registered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 37 | static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev, |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 38 | void *data, const struct file_operations *fops) |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | struct inode *inode = new_inode(sb); |
| 42 | |
| 43 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 44 | inode->i_ino = get_next_ino(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | inode->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 47 | switch (mode & S_IFMT) { |
| 48 | default: |
| 49 | init_special_inode(inode, mode, dev); |
| 50 | break; |
| 51 | case S_IFREG: |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 52 | inode->i_fop = fops ? fops : &debugfs_file_operations; |
| 53 | inode->i_private = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | break; |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 55 | case S_IFLNK: |
| 56 | inode->i_op = &debugfs_link_operations; |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 57 | inode->i_private = data; |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 58 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | case S_IFDIR: |
| 60 | inode->i_op = &simple_dir_inode_operations; |
Al Viro | ac481d6 | 2012-06-09 20:40:20 -0400 | [diff] [blame] | 61 | inode->i_fop = &simple_dir_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Mathieu Desnoyers | bafb232e | 2006-11-24 13:46:30 -0500 | [diff] [blame] | 63 | /* directory inodes start off with i_nlink == 2 |
| 64 | * (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 65 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | break; |
| 67 | } |
| 68 | } |
Rahul Bedarkar | 88e412e | 2014-06-06 23:12:04 +0530 | [diff] [blame] | 69 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* SMP-safe */ |
| 73 | static int debugfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 74 | umode_t mode, dev_t dev, void *data, |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 75 | const struct file_operations *fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Jens Axboe | 71601e2b | 2006-06-08 10:26:39 +0200 | [diff] [blame] | 77 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | int error = -EPERM; |
| 79 | |
| 80 | if (dentry->d_inode) |
| 81 | return -EEXIST; |
| 82 | |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 83 | inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (inode) { |
| 85 | d_instantiate(dentry, inode); |
| 86 | dget(dentry); |
| 87 | error = 0; |
| 88 | } |
| 89 | return error; |
| 90 | } |
| 91 | |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 92 | static int debugfs_mkdir(struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 94 | struct inode *dir = dentry->d_parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | int res; |
| 96 | |
| 97 | mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR; |
Al Viro | ac481d6 | 2012-06-09 20:40:20 -0400 | [diff] [blame] | 98 | res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL); |
Mathieu Desnoyers | 4f36557 | 2006-11-24 13:45:37 -0500 | [diff] [blame] | 99 | if (!res) { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 100 | inc_nlink(dir); |
Mathieu Desnoyers | 4f36557 | 2006-11-24 13:45:37 -0500 | [diff] [blame] | 101 | fsnotify_mkdir(dir, dentry); |
| 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return res; |
| 104 | } |
| 105 | |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 106 | static int debugfs_link(struct dentry *dentry, umode_t mode, |
Al Viro | ac481d6 | 2012-06-09 20:40:20 -0400 | [diff] [blame] | 107 | void *data) |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 108 | { |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 109 | struct inode *dir = dentry->d_parent->d_inode; |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 110 | mode = (mode & S_IALLUGO) | S_IFLNK; |
Al Viro | ac481d6 | 2012-06-09 20:40:20 -0400 | [diff] [blame] | 111 | return debugfs_mknod(dir, dentry, mode, 0, data, NULL); |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 114 | static int debugfs_create(struct dentry *dentry, umode_t mode, |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 115 | void *data, const struct file_operations *fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Al Viro | e09ddf3 | 2015-01-25 13:50:23 -0500 | [diff] [blame] | 117 | struct inode *dir = dentry->d_parent->d_inode; |
Mathieu Desnoyers | 4f36557 | 2006-11-24 13:45:37 -0500 | [diff] [blame] | 118 | int res; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mode = (mode & S_IALLUGO) | S_IFREG; |
Mathieu Desnoyers | d3a3b0a | 2009-11-17 14:40:26 -0800 | [diff] [blame] | 121 | res = debugfs_mknod(dir, dentry, mode, 0, data, fops); |
Mathieu Desnoyers | 4f36557 | 2006-11-24 13:45:37 -0500 | [diff] [blame] | 122 | if (!res) |
| 123 | fsnotify_create(dir, dentry); |
| 124 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline int debugfs_positive(struct dentry *dentry) |
| 128 | { |
| 129 | return dentry->d_inode && !d_unhashed(dentry); |
| 130 | } |
| 131 | |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 132 | struct debugfs_mount_opts { |
Eric W. Biederman | 7dc0588 | 2012-04-03 14:01:31 -0700 | [diff] [blame] | 133 | kuid_t uid; |
| 134 | kgid_t gid; |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 135 | umode_t mode; |
| 136 | }; |
| 137 | |
| 138 | enum { |
| 139 | Opt_uid, |
| 140 | Opt_gid, |
| 141 | Opt_mode, |
| 142 | Opt_err |
| 143 | }; |
| 144 | |
| 145 | static const match_table_t tokens = { |
| 146 | {Opt_uid, "uid=%u"}, |
| 147 | {Opt_gid, "gid=%u"}, |
| 148 | {Opt_mode, "mode=%o"}, |
| 149 | {Opt_err, NULL} |
| 150 | }; |
| 151 | |
| 152 | struct debugfs_fs_info { |
| 153 | struct debugfs_mount_opts mount_opts; |
| 154 | }; |
| 155 | |
| 156 | static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) |
| 157 | { |
| 158 | substring_t args[MAX_OPT_ARGS]; |
| 159 | int option; |
| 160 | int token; |
Eric W. Biederman | 7dc0588 | 2012-04-03 14:01:31 -0700 | [diff] [blame] | 161 | kuid_t uid; |
| 162 | kgid_t gid; |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 163 | char *p; |
| 164 | |
| 165 | opts->mode = DEBUGFS_DEFAULT_MODE; |
| 166 | |
| 167 | while ((p = strsep(&data, ",")) != NULL) { |
| 168 | if (!*p) |
| 169 | continue; |
| 170 | |
| 171 | token = match_token(p, tokens, args); |
| 172 | switch (token) { |
| 173 | case Opt_uid: |
| 174 | if (match_int(&args[0], &option)) |
| 175 | return -EINVAL; |
Eric W. Biederman | 7dc0588 | 2012-04-03 14:01:31 -0700 | [diff] [blame] | 176 | uid = make_kuid(current_user_ns(), option); |
| 177 | if (!uid_valid(uid)) |
| 178 | return -EINVAL; |
| 179 | opts->uid = uid; |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 180 | break; |
| 181 | case Opt_gid: |
Dave Reisner | f1688e0 | 2013-01-02 08:54:37 -0500 | [diff] [blame] | 182 | if (match_int(&args[0], &option)) |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 183 | return -EINVAL; |
Eric W. Biederman | 7dc0588 | 2012-04-03 14:01:31 -0700 | [diff] [blame] | 184 | gid = make_kgid(current_user_ns(), option); |
| 185 | if (!gid_valid(gid)) |
| 186 | return -EINVAL; |
| 187 | opts->gid = gid; |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 188 | break; |
| 189 | case Opt_mode: |
| 190 | if (match_octal(&args[0], &option)) |
| 191 | return -EINVAL; |
| 192 | opts->mode = option & S_IALLUGO; |
| 193 | break; |
| 194 | /* |
| 195 | * We might like to report bad mount options here; |
| 196 | * but traditionally debugfs has ignored all mount options |
| 197 | */ |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int debugfs_apply_options(struct super_block *sb) |
| 205 | { |
| 206 | struct debugfs_fs_info *fsi = sb->s_fs_info; |
| 207 | struct inode *inode = sb->s_root->d_inode; |
| 208 | struct debugfs_mount_opts *opts = &fsi->mount_opts; |
| 209 | |
| 210 | inode->i_mode &= ~S_IALLUGO; |
| 211 | inode->i_mode |= opts->mode; |
| 212 | |
| 213 | inode->i_uid = opts->uid; |
| 214 | inode->i_gid = opts->gid; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int debugfs_remount(struct super_block *sb, int *flags, char *data) |
| 220 | { |
| 221 | int err; |
| 222 | struct debugfs_fs_info *fsi = sb->s_fs_info; |
| 223 | |
Theodore Ts'o | 02b9984 | 2014-03-13 10:14:33 -0400 | [diff] [blame] | 224 | sync_filesystem(sb); |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 225 | err = debugfs_parse_options(data, &fsi->mount_opts); |
| 226 | if (err) |
| 227 | goto fail; |
| 228 | |
| 229 | debugfs_apply_options(sb); |
| 230 | |
| 231 | fail: |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static int debugfs_show_options(struct seq_file *m, struct dentry *root) |
| 236 | { |
| 237 | struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; |
| 238 | struct debugfs_mount_opts *opts = &fsi->mount_opts; |
| 239 | |
Eric W. Biederman | 7dc0588 | 2012-04-03 14:01:31 -0700 | [diff] [blame] | 240 | if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) |
| 241 | seq_printf(m, ",uid=%u", |
| 242 | from_kuid_munged(&init_user_ns, opts->uid)); |
| 243 | if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) |
| 244 | seq_printf(m, ",gid=%u", |
| 245 | from_kgid_munged(&init_user_ns, opts->gid)); |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 246 | if (opts->mode != DEBUGFS_DEFAULT_MODE) |
| 247 | seq_printf(m, ",mode=%o", opts->mode); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static const struct super_operations debugfs_super_operations = { |
| 253 | .statfs = simple_statfs, |
| 254 | .remount_fs = debugfs_remount, |
| 255 | .show_options = debugfs_show_options, |
| 256 | }; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | static int debug_fill_super(struct super_block *sb, void *data, int silent) |
| 259 | { |
| 260 | static struct tree_descr debug_files[] = {{""}}; |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 261 | struct debugfs_fs_info *fsi; |
| 262 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Ludwig Nussel | d6e4868 | 2012-01-25 11:52:28 +0100 | [diff] [blame] | 264 | save_mount_options(sb, data); |
| 265 | |
| 266 | fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); |
| 267 | sb->s_fs_info = fsi; |
| 268 | if (!fsi) { |
| 269 | err = -ENOMEM; |
| 270 | goto fail; |
| 271 | } |
| 272 | |
| 273 | err = debugfs_parse_options(data, &fsi->mount_opts); |
| 274 | if (err) |
| 275 | goto fail; |
| 276 | |
| 277 | err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); |
| 278 | if (err) |
| 279 | goto fail; |
| 280 | |
| 281 | sb->s_op = &debugfs_super_operations; |
| 282 | |
| 283 | debugfs_apply_options(sb); |
| 284 | |
| 285 | return 0; |
| 286 | |
| 287 | fail: |
| 288 | kfree(fsi); |
| 289 | sb->s_fs_info = NULL; |
| 290 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 293 | static struct dentry *debug_mount(struct file_system_type *fs_type, |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 294 | int flags, const char *dev_name, |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 295 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 297 | return mount_single(fs_type, flags, data, debug_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static struct file_system_type debug_fs_type = { |
| 301 | .owner = THIS_MODULE, |
| 302 | .name = "debugfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 303 | .mount = debug_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | .kill_sb = kill_litter_super, |
| 305 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 306 | MODULE_ALIAS_FS("debugfs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Al Viro | 190afd8 | 2015-01-25 13:55:55 -0500 | [diff] [blame] | 308 | static struct dentry *start_creating(const char *name, struct dentry *parent) |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 309 | { |
Al Viro | 190afd8 | 2015-01-25 13:55:55 -0500 | [diff] [blame] | 310 | struct dentry *dentry; |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 311 | int error; |
| 312 | |
| 313 | pr_debug("debugfs: creating file '%s'\n",name); |
| 314 | |
| 315 | error = simple_pin_fs(&debug_fs_type, &debugfs_mount, |
| 316 | &debugfs_mount_count); |
| 317 | if (error) |
Al Viro | 190afd8 | 2015-01-25 13:55:55 -0500 | [diff] [blame] | 318 | return ERR_PTR(error); |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 319 | |
Al Viro | cfa57c1 | 2012-06-09 20:33:28 -0400 | [diff] [blame] | 320 | /* If the parent is not specified, we create it in the root. |
Rahul Bedarkar | 88e412e | 2014-06-06 23:12:04 +0530 | [diff] [blame] | 321 | * We need the root dentry to do this, which is in the super |
Al Viro | cfa57c1 | 2012-06-09 20:33:28 -0400 | [diff] [blame] | 322 | * block. A pointer to that is in the struct vfsmount that we |
| 323 | * have around. |
| 324 | */ |
| 325 | if (!parent) |
| 326 | parent = debugfs_mount->mnt_root; |
| 327 | |
Al Viro | cfa57c1 | 2012-06-09 20:33:28 -0400 | [diff] [blame] | 328 | mutex_lock(&parent->d_inode->i_mutex); |
| 329 | dentry = lookup_one_len(name, parent, strlen(name)); |
Al Viro | 190afd8 | 2015-01-25 13:55:55 -0500 | [diff] [blame] | 330 | if (!IS_ERR(dentry) && dentry->d_inode) { |
Al Viro | cfa57c1 | 2012-06-09 20:33:28 -0400 | [diff] [blame] | 331 | dput(dentry); |
Al Viro | 190afd8 | 2015-01-25 13:55:55 -0500 | [diff] [blame] | 332 | dentry = ERR_PTR(-EEXIST); |
| 333 | } |
| 334 | if (IS_ERR(dentry)) |
| 335 | mutex_unlock(&parent->d_inode->i_mutex); |
| 336 | return dentry; |
| 337 | } |
| 338 | |
| 339 | static struct dentry *end_creating(struct dentry *dentry, int error) |
| 340 | { |
| 341 | mutex_unlock(&dentry->d_parent->d_inode->i_mutex); |
| 342 | dput(dentry); |
Al Viro | cfa57c1 | 2012-06-09 20:33:28 -0400 | [diff] [blame] | 343 | |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 344 | if (error) { |
| 345 | dentry = NULL; |
| 346 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); |
| 347 | } |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 348 | return dentry; |
| 349 | } |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /** |
| 352 | * debugfs_create_file - create a file in the debugfs filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * @name: a pointer to a string containing the name of the file to create. |
Alberto Bertogli | be030e6 | 2009-10-31 18:26:52 -0300 | [diff] [blame] | 354 | * @mode: the permission that the file should have. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | * @parent: a pointer to the parent dentry for this file. This should be a |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 356 | * directory dentry if set. If this parameter is NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * file will be created in the root of the debugfs filesystem. |
| 358 | * @data: a pointer to something that the caller will want to get to later |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 359 | * on. The inode.i_private pointer will point to this value on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * the open() call. |
| 361 | * @fops: a pointer to a struct file_operations that should be used for |
| 362 | * this file. |
| 363 | * |
| 364 | * This is the basic "create a file" function for debugfs. It allows for a |
Alberto Bertogli | be030e6 | 2009-10-31 18:26:52 -0300 | [diff] [blame] | 365 | * wide range of flexibility in creating a file, or a directory (if you want |
| 366 | * to create a directory, the debugfs_create_dir() function is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | * recommended to be used instead.) |
| 368 | * |
| 369 | * This function will return a pointer to a dentry if it succeeds. This |
| 370 | * pointer must be passed to the debugfs_remove() function when the file is |
| 371 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 372 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 374 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Cornelia Huck | 873760f | 2007-02-14 07:57:47 +0100 | [diff] [blame] | 375 | * returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 377 | struct dentry *debugfs_create_file(const char *name, umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | struct dentry *parent, void *data, |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 379 | const struct file_operations *fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 381 | struct dentry *dentry; |
| 382 | int error; |
Al Viro | c3b1a35 | 2012-06-09 20:28:22 -0400 | [diff] [blame] | 383 | |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 384 | if (!(mode & S_IFMT)) |
| 385 | mode |= S_IFREG; |
| 386 | BUG_ON(!S_ISREG(mode)); |
| 387 | dentry = start_creating(name, parent); |
| 388 | |
| 389 | if (IS_ERR(dentry)) |
| 390 | return NULL; |
| 391 | |
| 392 | error = debugfs_create(dentry, mode, data, fops); |
| 393 | return end_creating(dentry, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | EXPORT_SYMBOL_GPL(debugfs_create_file); |
| 396 | |
| 397 | /** |
| 398 | * debugfs_create_dir - create a directory in the debugfs filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | * @name: a pointer to a string containing the name of the directory to |
| 400 | * create. |
| 401 | * @parent: a pointer to the parent dentry for this file. This should be a |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 402 | * directory dentry if set. If this parameter is NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * directory will be created in the root of the debugfs filesystem. |
| 404 | * |
| 405 | * This function creates a directory in debugfs with the given name. |
| 406 | * |
| 407 | * This function will return a pointer to a dentry if it succeeds. This |
| 408 | * pointer must be passed to the debugfs_remove() function when the file is |
| 409 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 410 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 412 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Cornelia Huck | 873760f | 2007-02-14 07:57:47 +0100 | [diff] [blame] | 413 | * returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | */ |
| 415 | struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) |
| 416 | { |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 417 | struct dentry *dentry = start_creating(name, parent); |
| 418 | int error; |
| 419 | |
| 420 | if (IS_ERR(dentry)) |
| 421 | return NULL; |
| 422 | |
| 423 | error = debugfs_mkdir(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO); |
| 424 | return end_creating(dentry, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(debugfs_create_dir); |
| 427 | |
| 428 | /** |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 429 | * debugfs_create_symlink- create a symbolic link in the debugfs filesystem |
| 430 | * @name: a pointer to a string containing the name of the symbolic link to |
| 431 | * create. |
| 432 | * @parent: a pointer to the parent dentry for this symbolic link. This |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 433 | * should be a directory dentry if set. If this parameter is NULL, |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 434 | * then the symbolic link will be created in the root of the debugfs |
| 435 | * filesystem. |
| 436 | * @target: a pointer to a string containing the path to the target of the |
| 437 | * symbolic link. |
| 438 | * |
| 439 | * This function creates a symbolic link with the given name in debugfs that |
| 440 | * links to the given target path. |
| 441 | * |
| 442 | * This function will return a pointer to a dentry if it succeeds. This |
| 443 | * pointer must be passed to the debugfs_remove() function when the symbolic |
| 444 | * link is to be removed (no automatic cleanup happens if your module is |
| 445 | * unloaded, you are responsible here.) If an error occurs, %NULL will be |
| 446 | * returned. |
| 447 | * |
| 448 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Cornelia Huck | 873760f | 2007-02-14 07:57:47 +0100 | [diff] [blame] | 449 | * returned. |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 450 | */ |
| 451 | struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, |
| 452 | const char *target) |
| 453 | { |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 454 | struct dentry *dentry; |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 455 | char *link; |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 456 | int error; |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 457 | |
| 458 | link = kstrdup(target, GFP_KERNEL); |
| 459 | if (!link) |
| 460 | return NULL; |
| 461 | |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 462 | dentry = start_creating(name, parent); |
| 463 | |
| 464 | if (IS_ERR(dentry)) { |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 465 | kfree(link); |
Al Viro | ad5abd5 | 2015-01-25 14:02:31 -0500 | [diff] [blame^] | 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | error = debugfs_link(dentry, S_IFLNK | S_IRWXUGO, link); |
| 470 | if (error) |
| 471 | kfree(link); |
| 472 | |
| 473 | return end_creating(dentry, error); |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 474 | } |
| 475 | EXPORT_SYMBOL_GPL(debugfs_create_symlink); |
| 476 | |
Jan Kara | 25d41d8 | 2011-02-07 15:00:27 +0100 | [diff] [blame] | 477 | static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Mathieu Desnoyers | 65c3333 | 2006-11-24 13:50:09 -0500 | [diff] [blame] | 479 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (debugfs_positive(dentry)) { |
| 482 | if (dentry->d_inode) { |
Mathieu Desnoyers | 29a7f3a | 2006-11-24 13:51:14 -0500 | [diff] [blame] | 483 | dget(dentry); |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 484 | switch (dentry->d_inode->i_mode & S_IFMT) { |
| 485 | case S_IFDIR: |
Mathieu Desnoyers | 65c3333 | 2006-11-24 13:50:09 -0500 | [diff] [blame] | 486 | ret = simple_rmdir(parent->d_inode, dentry); |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 487 | break; |
| 488 | case S_IFLNK: |
| 489 | kfree(dentry->d_inode->i_private); |
| 490 | /* fall through */ |
| 491 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | simple_unlink(parent->d_inode, dentry); |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 493 | break; |
| 494 | } |
Mathieu Desnoyers | 29a7f3a | 2006-11-24 13:51:14 -0500 | [diff] [blame] | 495 | if (!ret) |
| 496 | d_delete(dentry); |
| 497 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
Jan Kara | 25d41d8 | 2011-02-07 15:00:27 +0100 | [diff] [blame] | 500 | return ret; |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
| 504 | * debugfs_remove - removes a file or directory from the debugfs filesystem |
| 505 | * @dentry: a pointer to a the dentry of the file or directory to be |
| 506 | * removed. |
| 507 | * |
| 508 | * This function removes a file or directory in debugfs that was previously |
| 509 | * created with a call to another debugfs function (like |
| 510 | * debugfs_create_file() or variants thereof.) |
| 511 | * |
| 512 | * This function is required to be called in order for the file to be |
| 513 | * removed, no automatic cleanup of files will happen when a module is |
| 514 | * removed, you are responsible here. |
| 515 | */ |
| 516 | void debugfs_remove(struct dentry *dentry) |
| 517 | { |
| 518 | struct dentry *parent; |
Jan Kara | 25d41d8 | 2011-02-07 15:00:27 +0100 | [diff] [blame] | 519 | int ret; |
| 520 | |
Arend van Spriel | a59d629 | 2012-05-23 15:13:07 +0200 | [diff] [blame] | 521 | if (IS_ERR_OR_NULL(dentry)) |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 522 | return; |
| 523 | |
| 524 | parent = dentry->d_parent; |
| 525 | if (!parent || !parent->d_inode) |
| 526 | return; |
| 527 | |
| 528 | mutex_lock(&parent->d_inode->i_mutex); |
Jan Kara | 25d41d8 | 2011-02-07 15:00:27 +0100 | [diff] [blame] | 529 | ret = __debugfs_remove(dentry, parent); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 530 | mutex_unlock(&parent->d_inode->i_mutex); |
Jan Kara | 25d41d8 | 2011-02-07 15:00:27 +0100 | [diff] [blame] | 531 | if (!ret) |
| 532 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | EXPORT_SYMBOL_GPL(debugfs_remove); |
| 535 | |
Jan Kara | cfc94cd | 2007-05-09 13:19:52 +0200 | [diff] [blame] | 536 | /** |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 537 | * debugfs_remove_recursive - recursively removes a directory |
| 538 | * @dentry: a pointer to a the dentry of the directory to be removed. |
| 539 | * |
| 540 | * This function recursively removes a directory tree in debugfs that |
| 541 | * was previously created with a call to another debugfs function |
| 542 | * (like debugfs_create_file() or variants thereof.) |
| 543 | * |
| 544 | * This function is required to be called in order for the file to be |
| 545 | * removed, no automatic cleanup of files will happen when a module is |
| 546 | * removed, you are responsible here. |
| 547 | */ |
| 548 | void debugfs_remove_recursive(struct dentry *dentry) |
| 549 | { |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 550 | struct dentry *child, *parent; |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 551 | |
Arend van Spriel | a59d629 | 2012-05-23 15:13:07 +0200 | [diff] [blame] | 552 | if (IS_ERR_OR_NULL(dentry)) |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 553 | return; |
| 554 | |
| 555 | parent = dentry->d_parent; |
| 556 | if (!parent || !parent->d_inode) |
| 557 | return; |
| 558 | |
| 559 | parent = dentry; |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 560 | down: |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 561 | mutex_lock(&parent->d_inode->i_mutex); |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 562 | loop: |
| 563 | /* |
| 564 | * The parent->d_subdirs is protected by the d_lock. Outside that |
| 565 | * lock, the child can be unlinked and set to be freed which can |
| 566 | * use the d_u.d_child as the rcu head and corrupt this list. |
| 567 | */ |
| 568 | spin_lock(&parent->d_lock); |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 569 | list_for_each_entry(child, &parent->d_subdirs, d_child) { |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 570 | if (!debugfs_positive(child)) |
| 571 | continue; |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 572 | |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 573 | /* perhaps simple_empty(child) makes more sense */ |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 574 | if (!list_empty(&child->d_subdirs)) { |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 575 | spin_unlock(&parent->d_lock); |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 576 | mutex_unlock(&parent->d_inode->i_mutex); |
| 577 | parent = child; |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 578 | goto down; |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 579 | } |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 580 | |
| 581 | spin_unlock(&parent->d_lock); |
| 582 | |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 583 | if (!__debugfs_remove(child, parent)) |
| 584 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * The parent->d_lock protects agaist child from unlinking |
| 588 | * from d_subdirs. When releasing the parent->d_lock we can |
| 589 | * no longer trust that the next pointer is valid. |
| 590 | * Restart the loop. We'll skip this one with the |
| 591 | * debugfs_positive() check. |
| 592 | */ |
| 593 | goto loop; |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 594 | } |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 595 | spin_unlock(&parent->d_lock); |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 596 | |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 597 | mutex_unlock(&parent->d_inode->i_mutex); |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 598 | child = parent; |
| 599 | parent = parent->d_parent; |
| 600 | mutex_lock(&parent->d_inode->i_mutex); |
| 601 | |
Steven Rostedt | 485d440 | 2014-06-09 14:06:07 -0400 | [diff] [blame] | 602 | if (child != dentry) |
| 603 | /* go up */ |
| 604 | goto loop; |
Oleg Nesterov | 776164c | 2013-07-26 17:12:56 +0200 | [diff] [blame] | 605 | |
| 606 | if (!__debugfs_remove(child, parent)) |
| 607 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); |
| 608 | mutex_unlock(&parent->d_inode->i_mutex); |
Haavard Skinnemoen | 9505e63 | 2008-07-01 15:14:51 +0200 | [diff] [blame] | 609 | } |
| 610 | EXPORT_SYMBOL_GPL(debugfs_remove_recursive); |
| 611 | |
| 612 | /** |
Jan Kara | cfc94cd | 2007-05-09 13:19:52 +0200 | [diff] [blame] | 613 | * debugfs_rename - rename a file/directory in the debugfs filesystem |
| 614 | * @old_dir: a pointer to the parent dentry for the renamed object. This |
| 615 | * should be a directory dentry. |
| 616 | * @old_dentry: dentry of an object to be renamed. |
| 617 | * @new_dir: a pointer to the parent dentry where the object should be |
| 618 | * moved. This should be a directory dentry. |
| 619 | * @new_name: a pointer to a string containing the target name. |
| 620 | * |
| 621 | * This function renames a file/directory in debugfs. The target must not |
| 622 | * exist for rename to succeed. |
| 623 | * |
| 624 | * This function will return a pointer to old_dentry (which is updated to |
| 625 | * reflect renaming) if it succeeds. If an error occurs, %NULL will be |
| 626 | * returned. |
| 627 | * |
| 628 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
| 629 | * returned. |
| 630 | */ |
| 631 | struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, |
| 632 | struct dentry *new_dir, const char *new_name) |
| 633 | { |
| 634 | int error; |
| 635 | struct dentry *dentry = NULL, *trap; |
| 636 | const char *old_name; |
| 637 | |
| 638 | trap = lock_rename(new_dir, old_dir); |
| 639 | /* Source or destination directories don't exist? */ |
| 640 | if (!old_dir->d_inode || !new_dir->d_inode) |
| 641 | goto exit; |
| 642 | /* Source does not exist, cyclic rename, or mountpoint? */ |
| 643 | if (!old_dentry->d_inode || old_dentry == trap || |
| 644 | d_mountpoint(old_dentry)) |
| 645 | goto exit; |
| 646 | dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); |
| 647 | /* Lookup failed, cyclic rename or target exists? */ |
| 648 | if (IS_ERR(dentry) || dentry == trap || dentry->d_inode) |
| 649 | goto exit; |
| 650 | |
| 651 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); |
| 652 | |
| 653 | error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode, |
| 654 | dentry); |
| 655 | if (error) { |
| 656 | fsnotify_oldname_free(old_name); |
| 657 | goto exit; |
| 658 | } |
| 659 | d_move(old_dentry, dentry); |
| 660 | fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name, |
Al Viro | 123df29 | 2009-12-25 04:57:57 -0500 | [diff] [blame] | 661 | S_ISDIR(old_dentry->d_inode->i_mode), |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 662 | NULL, old_dentry); |
Jan Kara | cfc94cd | 2007-05-09 13:19:52 +0200 | [diff] [blame] | 663 | fsnotify_oldname_free(old_name); |
| 664 | unlock_rename(new_dir, old_dir); |
| 665 | dput(dentry); |
| 666 | return old_dentry; |
| 667 | exit: |
| 668 | if (dentry && !IS_ERR(dentry)) |
| 669 | dput(dentry); |
| 670 | unlock_rename(new_dir, old_dir); |
| 671 | return NULL; |
| 672 | } |
| 673 | EXPORT_SYMBOL_GPL(debugfs_rename); |
| 674 | |
Frederic Weisbecker | c0f92ba | 2009-03-22 23:10:44 +0100 | [diff] [blame] | 675 | /** |
| 676 | * debugfs_initialized - Tells whether debugfs has been registered |
| 677 | */ |
| 678 | bool debugfs_initialized(void) |
| 679 | { |
| 680 | return debugfs_registered; |
| 681 | } |
| 682 | EXPORT_SYMBOL_GPL(debugfs_initialized); |
| 683 | |
| 684 | |
Greg Kroah-Hartman | 191e186 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 685 | static struct kobject *debug_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | static int __init debugfs_init(void) |
| 688 | { |
| 689 | int retval; |
| 690 | |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 691 | debug_kobj = kobject_create_and_add("debug", kernel_kobj); |
Greg Kroah-Hartman | 191e186 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 692 | if (!debug_kobj) |
| 693 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | retval = register_filesystem(&debug_fs_type); |
| 696 | if (retval) |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 697 | kobject_put(debug_kobj); |
Frederic Weisbecker | c0f92ba | 2009-03-22 23:10:44 +0100 | [diff] [blame] | 698 | else |
| 699 | debugfs_registered = true; |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return retval; |
| 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | core_initcall(debugfs_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |