]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * file.c - part of debugfs, a tiny little debug file system | |
3 | * | |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> | |
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 | ||
1da177e4 LT |
19 | #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 */ | |
30 | extern struct file_operations debugfs_file_operations; | |
31 | ||
32 | static struct vfsmount *debugfs_mount; | |
33 | static int debugfs_mount_count; | |
34 | ||
35 | static 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; | |
1da177e4 LT |
43 | 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) */ | |
d8c76e6f | 57 | inc_nlink(inode); |
1da177e4 LT |
58 | break; |
59 | } | |
60 | } | |
61 | return inode; | |
62 | } | |
63 | ||
64 | /* SMP-safe */ | |
65 | static int debugfs_mknod(struct inode *dir, struct dentry *dentry, | |
66 | int mode, dev_t dev) | |
67 | { | |
71601e2b | 68 | struct inode *inode; |
1da177e4 LT |
69 | int error = -EPERM; |
70 | ||
71 | if (dentry->d_inode) | |
72 | return -EEXIST; | |
73 | ||
71601e2b | 74 | inode = debugfs_get_inode(dir->i_sb, mode, dev); |
1da177e4 LT |
75 | if (inode) { |
76 | d_instantiate(dentry, inode); | |
77 | dget(dentry); | |
78 | error = 0; | |
79 | } | |
80 | return error; | |
81 | } | |
82 | ||
83 | static 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) | |
d8c76e6f | 90 | inc_nlink(dir); |
1da177e4 LT |
91 | return res; |
92 | } | |
93 | ||
94 | static 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 | ||
100 | static inline int debugfs_positive(struct dentry *dentry) | |
101 | { | |
102 | return dentry->d_inode && !d_unhashed(dentry); | |
103 | } | |
104 | ||
105 | static 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 | ||
454e2398 DH |
112 | static int debug_get_sb(struct file_system_type *fs_type, |
113 | int flags, const char *dev_name, | |
114 | void *data, struct vfsmount *mnt) | |
1da177e4 | 115 | { |
454e2398 | 116 | return get_sb_single(fs_type, flags, data, debug_fill_super, mnt); |
1da177e4 LT |
117 | } |
118 | ||
119 | static 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 | ||
126 | static 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; | |
1b1dcc1b | 148 | mutex_lock(&parent->d_inode->i_mutex); |
5f45f1a7 | 149 | *dentry = lookup_one_len(name, parent, strlen(name)); |
7bb0386f | 150 | if (!IS_ERR(*dentry)) { |
1da177e4 LT |
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 | |
7bb0386f | 156 | error = PTR_ERR(*dentry); |
1b1dcc1b | 157 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
158 | |
159 | return error; | |
160 | } | |
161 | ||
162 | /** | |
163 | * debugfs_create_file - create a file in the debugfs filesystem | |
1da177e4 LT |
164 | * @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 | |
8e18e294 | 170 | * on. The inode.i_private pointer will point to this value on |
1da177e4 LT |
171 | * 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, | |
6468b3af | 183 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 184 | * |
6468b3af | 185 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 186 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 187 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
188 | * code. |
189 | */ | |
190 | struct dentry *debugfs_create_file(const char *name, mode_t mode, | |
191 | struct dentry *parent, void *data, | |
99ac48f5 | 192 | const struct file_operations *fops) |
1da177e4 LT |
193 | { |
194 | struct dentry *dentry = NULL; | |
195 | int error; | |
196 | ||
197 | pr_debug("debugfs: creating file '%s'\n",name); | |
198 | ||
1f5ce9e9 | 199 | error = simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count); |
1da177e4 LT |
200 | 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) | |
8e18e294 | 211 | dentry->d_inode->i_private = data; |
1da177e4 LT |
212 | if (fops) |
213 | dentry->d_inode->i_fop = fops; | |
214 | } | |
215 | exit: | |
216 | return dentry; | |
217 | } | |
218 | EXPORT_SYMBOL_GPL(debugfs_create_file); | |
219 | ||
220 | /** | |
221 | * debugfs_create_dir - create a directory in the debugfs filesystem | |
1da177e4 LT |
222 | * @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, | |
6468b3af | 233 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 234 | * |
6468b3af | 235 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 236 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 237 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
238 | * code. |
239 | */ | |
240 | struct 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 | } | |
246 | EXPORT_SYMBOL_GPL(debugfs_create_dir); | |
247 | ||
248 | /** | |
249 | * debugfs_remove - removes a file or directory from the debugfs filesystem | |
1da177e4 LT |
250 | * @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 | |
5a65980e | 255 | * debugfs_create_file() or variants thereof.) |
1da177e4 LT |
256 | * |
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 | */ | |
261 | void 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 | ||
1b1dcc1b | 272 | mutex_lock(&parent->d_inode->i_mutex); |
1da177e4 LT |
273 | 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 | } | |
1b1dcc1b | 282 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
283 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); |
284 | } | |
285 | EXPORT_SYMBOL_GPL(debugfs_remove); | |
286 | ||
287 | static decl_subsys(debug, NULL, NULL); | |
288 | ||
289 | static 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 | ||
304 | static 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 | ||
311 | core_initcall(debugfs_init); | |
312 | module_exit(debugfs_exit); | |
313 | MODULE_LICENSE("GPL"); | |
314 |