blob: e77676df6713de8ab4016d89688b2405ba89e63b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - part of debugfs, a tiny little debug file system
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
16/* uncomment to get debug messages from the debug filesystem, ah the irony. */
17/* #define DEBUG */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/fs.h>
21#include <linux/mount.h>
22#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/namei.h>
25#include <linux/debugfs.h>
26
27#define DEBUGFS_MAGIC 0x64626720
28
29/* declared over in file.c */
30extern struct file_operations debugfs_file_operations;
31
32static struct vfsmount *debugfs_mount;
33static int debugfs_mount_count;
34
35static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
36{
37 struct inode *inode = new_inode(sb);
38
39 if (inode) {
40 inode->i_mode = mode;
41 inode->i_uid = 0;
42 inode->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 inode->i_blocks = 0;
44 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
45 switch (mode & S_IFMT) {
46 default:
47 init_special_inode(inode, mode, dev);
48 break;
49 case S_IFREG:
50 inode->i_fop = &debugfs_file_operations;
51 break;
52 case S_IFDIR:
53 inode->i_op = &simple_dir_inode_operations;
54 inode->i_fop = &simple_dir_operations;
55
56 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -070057 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 break;
59 }
60 }
61 return inode;
62}
63
64/* SMP-safe */
65static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
66 int mode, dev_t dev)
67{
Jens Axboe71601e2b2006-06-08 10:26:39 +020068 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int error = -EPERM;
70
71 if (dentry->d_inode)
72 return -EEXIST;
73
Jens Axboe71601e2b2006-06-08 10:26:39 +020074 inode = debugfs_get_inode(dir->i_sb, mode, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (inode) {
76 d_instantiate(dentry, inode);
77 dget(dentry);
78 error = 0;
79 }
80 return error;
81}
82
83static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
84{
85 int res;
86
87 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
88 res = debugfs_mknod(dir, dentry, mode, 0);
89 if (!res)
Dave Hansend8c76e62006-09-30 23:29:04 -070090 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return res;
92}
93
94static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
95{
96 mode = (mode & S_IALLUGO) | S_IFREG;
97 return debugfs_mknod(dir, dentry, mode, 0);
98}
99
100static inline int debugfs_positive(struct dentry *dentry)
101{
102 return dentry->d_inode && !d_unhashed(dentry);
103}
104
105static int debug_fill_super(struct super_block *sb, void *data, int silent)
106{
107 static struct tree_descr debug_files[] = {{""}};
108
109 return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
110}
111
David Howells454e2392006-06-23 02:02:57 -0700112static int debug_get_sb(struct file_system_type *fs_type,
113 int flags, const char *dev_name,
114 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells454e2392006-06-23 02:02:57 -0700116 return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct file_system_type debug_fs_type = {
120 .owner = THIS_MODULE,
121 .name = "debugfs",
122 .get_sb = debug_get_sb,
123 .kill_sb = kill_litter_super,
124};
125
126static int debugfs_create_by_name(const char *name, mode_t mode,
127 struct dentry *parent,
128 struct dentry **dentry)
129{
130 int error = 0;
131
132 /* If the parent is not specified, we create it in the root.
133 * We need the root dentry to do this, which is in the super
134 * block. A pointer to that is in the struct vfsmount that we
135 * have around.
136 */
137 if (!parent ) {
138 if (debugfs_mount && debugfs_mount->mnt_sb) {
139 parent = debugfs_mount->mnt_sb->s_root;
140 }
141 }
142 if (!parent) {
143 pr_debug("debugfs: Ah! can not find a parent!\n");
144 return -EFAULT;
145 }
146
147 *dentry = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800148 mutex_lock(&parent->d_inode->i_mutex);
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700149 *dentry = lookup_one_len(name, parent, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!IS_ERR(dentry)) {
151 if ((mode & S_IFMT) == S_IFDIR)
152 error = debugfs_mkdir(parent->d_inode, *dentry, mode);
153 else
154 error = debugfs_create(parent->d_inode, *dentry, mode);
155 } else
156 error = PTR_ERR(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800157 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return error;
160}
161
162/**
163 * debugfs_create_file - create a file in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * @name: a pointer to a string containing the name of the file to create.
165 * @mode: the permission that the file should have
166 * @parent: a pointer to the parent dentry for this file. This should be a
167 * directory dentry if set. If this paramater is NULL, then the
168 * file will be created in the root of the debugfs filesystem.
169 * @data: a pointer to something that the caller will want to get to later
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700170 * on. The inode.i_private pointer will point to this value on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * the open() call.
172 * @fops: a pointer to a struct file_operations that should be used for
173 * this file.
174 *
175 * This is the basic "create a file" function for debugfs. It allows for a
176 * wide range of flexibility in createing a file, or a directory (if you
177 * want to create a directory, the debugfs_create_dir() function is
178 * recommended to be used instead.)
179 *
180 * This function will return a pointer to a dentry if it succeeds. This
181 * pointer must be passed to the debugfs_remove() function when the file is
182 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700183 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700185 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700187 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * code.
189 */
190struct dentry *debugfs_create_file(const char *name, mode_t mode,
191 struct dentry *parent, void *data,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800192 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct dentry *dentry = NULL;
195 int error;
196
197 pr_debug("debugfs: creating file '%s'\n",name);
198
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400199 error = simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (error)
201 goto exit;
202
203 error = debugfs_create_by_name(name, mode, parent, &dentry);
204 if (error) {
205 dentry = NULL;
206 goto exit;
207 }
208
209 if (dentry->d_inode) {
210 if (data)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700211 dentry->d_inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (fops)
213 dentry->d_inode->i_fop = fops;
214 }
215exit:
216 return dentry;
217}
218EXPORT_SYMBOL_GPL(debugfs_create_file);
219
220/**
221 * debugfs_create_dir - create a directory in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * @name: a pointer to a string containing the name of the directory to
223 * create.
224 * @parent: a pointer to the parent dentry for this file. This should be a
225 * directory dentry if set. If this paramater is NULL, then the
226 * directory will be created in the root of the debugfs filesystem.
227 *
228 * This function creates a directory in debugfs with the given name.
229 *
230 * This function will return a pointer to a dentry if it succeeds. This
231 * pointer must be passed to the debugfs_remove() function when the file is
232 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700233 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700235 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700237 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * code.
239 */
240struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
241{
242 return debugfs_create_file(name,
243 S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
244 parent, NULL, NULL);
245}
246EXPORT_SYMBOL_GPL(debugfs_create_dir);
247
248/**
249 * debugfs_remove - removes a file or directory from the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * @dentry: a pointer to a the dentry of the file or directory to be
251 * removed.
252 *
253 * This function removes a file or directory in debugfs that was previously
254 * created with a call to another debugfs function (like
Komal Shah5a659802006-10-03 23:28:36 +0200255 * debugfs_create_file() or variants thereof.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
257 * This function is required to be called in order for the file to be
258 * removed, no automatic cleanup of files will happen when a module is
259 * removed, you are responsible here.
260 */
261void debugfs_remove(struct dentry *dentry)
262{
263 struct dentry *parent;
264
265 if (!dentry)
266 return;
267
268 parent = dentry->d_parent;
269 if (!parent || !parent->d_inode)
270 return;
271
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800272 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (debugfs_positive(dentry)) {
274 if (dentry->d_inode) {
275 if (S_ISDIR(dentry->d_inode->i_mode))
276 simple_rmdir(parent->d_inode, dentry);
277 else
278 simple_unlink(parent->d_inode, dentry);
279 dput(dentry);
280 }
281 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800282 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
284}
285EXPORT_SYMBOL_GPL(debugfs_remove);
286
287static decl_subsys(debug, NULL, NULL);
288
289static int __init debugfs_init(void)
290{
291 int retval;
292
293 kset_set_kset_s(&debug_subsys, kernel_subsys);
294 retval = subsystem_register(&debug_subsys);
295 if (retval)
296 return retval;
297
298 retval = register_filesystem(&debug_fs_type);
299 if (retval)
300 subsystem_unregister(&debug_subsys);
301 return retval;
302}
303
304static void __exit debugfs_exit(void)
305{
306 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
307 unregister_filesystem(&debug_fs_type);
308 subsystem_unregister(&debug_subsys);
309}
310
311core_initcall(debugfs_init);
312module_exit(debugfs_exit);
313MODULE_LICENSE("GPL");
314