blob: 2e124e0075c5a3c2512af9626b03250c43e45cf3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/pagemap.h>
Peter Oberparleiter66f54962007-02-13 12:13:54 +010019#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/debugfs.h>
21
22static ssize_t default_read_file(struct file *file, char __user *buf,
23 size_t count, loff_t *ppos)
24{
25 return 0;
26}
27
28static ssize_t default_write_file(struct file *file, const char __user *buf,
29 size_t count, loff_t *ppos)
30{
31 return count;
32}
33
34static int default_open(struct inode *inode, struct file *file)
35{
Theodore Ts'o8e18e292006-09-27 01:50:46 -070036 if (inode->i_private)
37 file->private_data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 return 0;
40}
41
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations debugfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .read = default_read_file,
44 .write = default_write_file,
45 .open = default_open,
46};
47
Peter Oberparleiter66f54962007-02-13 12:13:54 +010048static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
49{
50 nd_set_link(nd, dentry->d_inode->i_private);
51 return NULL;
52}
53
54const struct inode_operations debugfs_link_operations = {
55 .readlink = generic_readlink,
56 .follow_link = debugfs_follow_link,
57};
58
Arnd Bergmannacaefc22005-05-18 14:40:59 +020059static void debugfs_u8_set(void *data, u64 val)
60{
61 *(u8 *)data = val;
62}
63static u64 debugfs_u8_get(void *data)
64{
65 return *(u8 *)data;
66}
67DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -070070 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * @name: a pointer to a string containing the name of the file to create.
72 * @mode: the permission that the file should have
73 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -070074 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * file will be created in the root of the debugfs filesystem.
76 * @value: a pointer to the variable that the file should read to and write
77 * from.
78 *
79 * This function creates a file in debugfs with the given name that
80 * contains the value of the variable @value. If the @mode variable is so
81 * set, it can be read from, and written to.
82 *
83 * This function will return a pointer to a dentry if it succeeds. This
84 * pointer must be passed to the debugfs_remove() function when the file is
85 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -070086 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -070088 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -070090 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * code.
92 */
93struct dentry *debugfs_create_u8(const char *name, mode_t mode,
94 struct dentry *parent, u8 *value)
95{
96 return debugfs_create_file(name, mode, parent, value, &fops_u8);
97}
98EXPORT_SYMBOL_GPL(debugfs_create_u8);
99
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200100static void debugfs_u16_set(void *data, u64 val)
101{
102 *(u16 *)data = val;
103}
104static u64 debugfs_u16_get(void *data)
105{
106 return *(u16 *)data;
107}
108DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700111 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * @name: a pointer to a string containing the name of the file to create.
113 * @mode: the permission that the file should have
114 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700115 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * file will be created in the root of the debugfs filesystem.
117 * @value: a pointer to the variable that the file should read to and write
118 * from.
119 *
120 * This function creates a file in debugfs with the given name that
121 * contains the value of the variable @value. If the @mode variable is so
122 * set, it can be read from, and written to.
123 *
124 * This function will return a pointer to a dentry if it succeeds. This
125 * pointer must be passed to the debugfs_remove() function when the file is
126 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700127 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700129 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700131 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * code.
133 */
134struct dentry *debugfs_create_u16(const char *name, mode_t mode,
135 struct dentry *parent, u16 *value)
136{
137 return debugfs_create_file(name, mode, parent, value, &fops_u16);
138}
139EXPORT_SYMBOL_GPL(debugfs_create_u16);
140
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200141static void debugfs_u32_set(void *data, u64 val)
142{
143 *(u32 *)data = val;
144}
145static u64 debugfs_u32_get(void *data)
146{
147 return *(u32 *)data;
148}
149DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700152 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * @name: a pointer to a string containing the name of the file to create.
154 * @mode: the permission that the file should have
155 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700156 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * file will be created in the root of the debugfs filesystem.
158 * @value: a pointer to the variable that the file should read to and write
159 * from.
160 *
161 * This function creates a file in debugfs with the given name that
162 * contains the value of the variable @value. If the @mode variable is so
163 * set, it can be read from, and written to.
164 *
165 * This function will return a pointer to a dentry if it succeeds. This
166 * pointer must be passed to the debugfs_remove() function when the file is
167 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700168 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700170 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700172 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * code.
174 */
175struct dentry *debugfs_create_u32(const char *name, mode_t mode,
176 struct dentry *parent, u32 *value)
177{
178 return debugfs_create_file(name, mode, parent, value, &fops_u32);
179}
180EXPORT_SYMBOL_GPL(debugfs_create_u32);
181
Michael Ellerman84478912007-04-17 15:59:36 +1000182static void debugfs_u64_set(void *data, u64 val)
183{
184 *(u64 *)data = val;
185}
186
187static u64 debugfs_u64_get(void *data)
188{
189 return *(u64 *)data;
190}
191DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
192
193/**
194 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
195 * @name: a pointer to a string containing the name of the file to create.
196 * @mode: the permission that the file should have
197 * @parent: a pointer to the parent dentry for this file. This should be a
198 * directory dentry if set. If this parameter is %NULL, then the
199 * file will be created in the root of the debugfs filesystem.
200 * @value: a pointer to the variable that the file should read to and write
201 * from.
202 *
203 * This function creates a file in debugfs with the given name that
204 * contains the value of the variable @value. If the @mode variable is so
205 * set, it can be read from, and written to.
206 *
207 * This function will return a pointer to a dentry if it succeeds. This
208 * pointer must be passed to the debugfs_remove() function when the file is
209 * to be removed (no automatic cleanup happens if your module is unloaded,
210 * you are responsible here.) If an error occurs, %NULL will be returned.
211 *
212 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
213 * returned. It is not wise to check for this value, but rather, check for
214 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
215 * code.
216 */
217struct dentry *debugfs_create_u64(const char *name, mode_t mode,
218 struct dentry *parent, u64 *value)
219{
220 return debugfs_create_file(name, mode, parent, value, &fops_u64);
221}
222EXPORT_SYMBOL_GPL(debugfs_create_u64);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static ssize_t read_file_bool(struct file *file, char __user *user_buf,
225 size_t count, loff_t *ppos)
226{
227 char buf[3];
228 u32 *val = file->private_data;
229
230 if (*val)
231 buf[0] = 'Y';
232 else
233 buf[0] = 'N';
234 buf[1] = '\n';
235 buf[2] = 0x00;
236 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
237}
238
239static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
240 size_t count, loff_t *ppos)
241{
242 char buf[32];
243 int buf_size;
244 u32 *val = file->private_data;
245
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249
250 switch (buf[0]) {
251 case 'y':
252 case 'Y':
253 case '1':
254 *val = 1;
255 break;
256 case 'n':
257 case 'N':
258 case '0':
259 *val = 0;
260 break;
261 }
262
263 return count;
264}
265
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800266static const struct file_operations fops_bool = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .read = read_file_bool,
268 .write = write_file_bool,
269 .open = default_open,
270};
271
272/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700273 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * @name: a pointer to a string containing the name of the file to create.
275 * @mode: the permission that the file should have
276 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700277 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * file will be created in the root of the debugfs filesystem.
279 * @value: a pointer to the variable that the file should read to and write
280 * from.
281 *
282 * This function creates a file in debugfs with the given name that
283 * contains the value of the variable @value. If the @mode variable is so
284 * set, it can be read from, and written to.
285 *
286 * This function will return a pointer to a dentry if it succeeds. This
287 * pointer must be passed to the debugfs_remove() function when the file is
288 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700289 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700291 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700293 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * code.
295 */
296struct dentry *debugfs_create_bool(const char *name, mode_t mode,
297 struct dentry *parent, u32 *value)
298{
299 return debugfs_create_file(name, mode, parent, value, &fops_bool);
300}
301EXPORT_SYMBOL_GPL(debugfs_create_bool);
302
Michael Ellermandd308bc2006-03-07 21:41:59 +1100303static ssize_t read_file_blob(struct file *file, char __user *user_buf,
304 size_t count, loff_t *ppos)
305{
306 struct debugfs_blob_wrapper *blob = file->private_data;
307 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
308 blob->size);
309}
310
Arjan van de Ven00977a52007-02-12 00:55:34 -0800311static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100312 .read = read_file_blob,
313 .open = default_open,
314};
315
316/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700317 * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100318 * @name: a pointer to a string containing the name of the file to create.
319 * @mode: the permission that the file should have
320 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700321 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100322 * file will be created in the root of the debugfs filesystem.
323 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
324 * to the blob data and the size of the data.
325 *
326 * This function creates a file in debugfs with the given name that exports
327 * @blob->data as a binary blob. If the @mode variable is so set it can be
328 * read from. Writing is not supported.
329 *
330 * This function will return a pointer to a dentry if it succeeds. This
331 * pointer must be passed to the debugfs_remove() function when the file is
332 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700333 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100334 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700335 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100336 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700337 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100338 * code.
339 */
340struct dentry *debugfs_create_blob(const char *name, mode_t mode,
341 struct dentry *parent,
342 struct debugfs_blob_wrapper *blob)
343{
344 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
345}
346EXPORT_SYMBOL_GPL(debugfs_create_blob);