2 * file.c - part of debugfs, a tiny little debug file system
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
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.
11 * debugfs is for people to use instead of /proc or /sys.
12 * See Documentation/DocBook/filesystems for more details.
16 #include <linux/module.h>
18 #include <linux/pagemap.h>
19 #include <linux/namei.h>
20 #include <linux/debugfs.h>
22 static ssize_t
default_read_file(struct file
*file
, char __user
*buf
,
23 size_t count
, loff_t
*ppos
)
28 static ssize_t
default_write_file(struct file
*file
, const char __user
*buf
,
29 size_t count
, loff_t
*ppos
)
34 static int default_open(struct inode
*inode
, struct file
*file
)
37 file
->private_data
= inode
->i_private
;
42 const struct file_operations debugfs_file_operations
= {
43 .read
= default_read_file
,
44 .write
= default_write_file
,
48 static void *debugfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
50 nd_set_link(nd
, dentry
->d_inode
->i_private
);
54 const struct inode_operations debugfs_link_operations
= {
55 .readlink
= generic_readlink
,
56 .follow_link
= debugfs_follow_link
,
59 static int debugfs_u8_set(void *data
, u64 val
)
64 static int debugfs_u8_get(void *data
, u64
*val
)
69 DEFINE_SIMPLE_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
72 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
73 * @name: a pointer to a string containing the name of the file to create.
74 * @mode: the permission that the file should have
75 * @parent: a pointer to the parent dentry for this file. This should be a
76 * directory dentry if set. If this parameter is %NULL, then the
77 * file will be created in the root of the debugfs filesystem.
78 * @value: a pointer to the variable that the file should read to and write
81 * This function creates a file in debugfs with the given name that
82 * contains the value of the variable @value. If the @mode variable is so
83 * set, it can be read from, and written to.
85 * This function will return a pointer to a dentry if it succeeds. This
86 * pointer must be passed to the debugfs_remove() function when the file is
87 * to be removed (no automatic cleanup happens if your module is unloaded,
88 * you are responsible here.) If an error occurs, %NULL will be returned.
90 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
91 * returned. It is not wise to check for this value, but rather, check for
92 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
95 struct dentry
*debugfs_create_u8(const char *name
, mode_t mode
,
96 struct dentry
*parent
, u8
*value
)
98 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u8
);
100 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
102 static int debugfs_u16_set(void *data
, u64 val
)
107 static int debugfs_u16_get(void *data
, u64
*val
)
112 DEFINE_SIMPLE_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
115 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
116 * @name: a pointer to a string containing the name of the file to create.
117 * @mode: the permission that the file should have
118 * @parent: a pointer to the parent dentry for this file. This should be a
119 * directory dentry if set. If this parameter is %NULL, then the
120 * file will be created in the root of the debugfs filesystem.
121 * @value: a pointer to the variable that the file should read to and write
124 * This function creates a file in debugfs with the given name that
125 * contains the value of the variable @value. If the @mode variable is so
126 * set, it can be read from, and written to.
128 * This function will return a pointer to a dentry if it succeeds. This
129 * pointer must be passed to the debugfs_remove() function when the file is
130 * to be removed (no automatic cleanup happens if your module is unloaded,
131 * you are responsible here.) If an error occurs, %NULL will be returned.
133 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
134 * returned. It is not wise to check for this value, but rather, check for
135 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
138 struct dentry
*debugfs_create_u16(const char *name
, mode_t mode
,
139 struct dentry
*parent
, u16
*value
)
141 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u16
);
143 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
145 static int debugfs_u32_set(void *data
, u64 val
)
150 static int debugfs_u32_get(void *data
, u64
*val
)
155 DEFINE_SIMPLE_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
158 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
159 * @name: a pointer to a string containing the name of the file to create.
160 * @mode: the permission that the file should have
161 * @parent: a pointer to the parent dentry for this file. This should be a
162 * directory dentry if set. If this parameter is %NULL, then the
163 * file will be created in the root of the debugfs filesystem.
164 * @value: a pointer to the variable that the file should read to and write
167 * This function creates a file in debugfs with the given name that
168 * contains the value of the variable @value. If the @mode variable is so
169 * set, it can be read from, and written to.
171 * This function will return a pointer to a dentry if it succeeds. This
172 * pointer must be passed to the debugfs_remove() function when the file is
173 * to be removed (no automatic cleanup happens if your module is unloaded,
174 * you are responsible here.) If an error occurs, %NULL will be returned.
176 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
177 * returned. It is not wise to check for this value, but rather, check for
178 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
181 struct dentry
*debugfs_create_u32(const char *name
, mode_t mode
,
182 struct dentry
*parent
, u32
*value
)
184 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u32
);
186 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
188 static int debugfs_u64_set(void *data
, u64 val
)
194 static int debugfs_u64_get(void *data
, u64
*val
)
199 DEFINE_SIMPLE_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
202 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
203 * @name: a pointer to a string containing the name of the file to create.
204 * @mode: the permission that the file should have
205 * @parent: a pointer to the parent dentry for this file. This should be a
206 * directory dentry if set. If this parameter is %NULL, then the
207 * file will be created in the root of the debugfs filesystem.
208 * @value: a pointer to the variable that the file should read to and write
211 * This function creates a file in debugfs with the given name that
212 * contains the value of the variable @value. If the @mode variable is so
213 * set, it can be read from, and written to.
215 * This function will return a pointer to a dentry if it succeeds. This
216 * pointer must be passed to the debugfs_remove() function when the file is
217 * to be removed (no automatic cleanup happens if your module is unloaded,
218 * you are responsible here.) If an error occurs, %NULL will be returned.
220 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
221 * returned. It is not wise to check for this value, but rather, check for
222 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
225 struct dentry
*debugfs_create_u64(const char *name
, mode_t mode
,
226 struct dentry
*parent
, u64
*value
)
228 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u64
);
230 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
232 DEFINE_SIMPLE_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
234 DEFINE_SIMPLE_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
, "0x%04llx\n");
236 DEFINE_SIMPLE_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
, "0x%08llx\n");
239 * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
241 * These functions are exactly the same as the above functions (but use a hex
242 * output for the decimal challenged). For details look at the above unsigned
247 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
248 * @name: a pointer to a string containing the name of the file to create.
249 * @mode: the permission that the file should have
250 * @parent: a pointer to the parent dentry for this file. This should be a
251 * directory dentry if set. If this parameter is %NULL, then the
252 * file will be created in the root of the debugfs filesystem.
253 * @value: a pointer to the variable that the file should read to and write
256 struct dentry
*debugfs_create_x8(const char *name
, mode_t mode
,
257 struct dentry
*parent
, u8
*value
)
259 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x8
);
261 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
264 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
265 * @name: a pointer to a string containing the name of the file to create.
266 * @mode: the permission that the file should have
267 * @parent: a pointer to the parent dentry for this file. This should be a
268 * directory dentry if set. If this parameter is %NULL, then the
269 * file will be created in the root of the debugfs filesystem.
270 * @value: a pointer to the variable that the file should read to and write
273 struct dentry
*debugfs_create_x16(const char *name
, mode_t mode
,
274 struct dentry
*parent
, u16
*value
)
276 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x16
);
278 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
281 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
282 * @name: a pointer to a string containing the name of the file to create.
283 * @mode: the permission that the file should have
284 * @parent: a pointer to the parent dentry for this file. This should be a
285 * directory dentry if set. If this parameter is %NULL, then the
286 * file will be created in the root of the debugfs filesystem.
287 * @value: a pointer to the variable that the file should read to and write
290 struct dentry
*debugfs_create_x32(const char *name
, mode_t mode
,
291 struct dentry
*parent
, u32
*value
)
293 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x32
);
295 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
298 static int debugfs_size_t_set(void *data
, u64 val
)
300 *(size_t *)data
= val
;
303 static int debugfs_size_t_get(void *data
, u64
*val
)
305 *val
= *(size_t *)data
;
308 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
309 "%llu\n"); /* %llu and %zu are more or less the same */
312 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
313 * @name: a pointer to a string containing the name of the file to create.
314 * @mode: the permission that the file should have
315 * @parent: a pointer to the parent dentry for this file. This should be a
316 * directory dentry if set. If this parameter is %NULL, then the
317 * file will be created in the root of the debugfs filesystem.
318 * @value: a pointer to the variable that the file should read to and write
321 struct dentry
*debugfs_create_size_t(const char *name
, mode_t mode
,
322 struct dentry
*parent
, size_t *value
)
324 return debugfs_create_file(name
, mode
, parent
, value
, &fops_size_t
);
326 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
329 static ssize_t
read_file_bool(struct file
*file
, char __user
*user_buf
,
330 size_t count
, loff_t
*ppos
)
333 u32
*val
= file
->private_data
;
341 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
344 static ssize_t
write_file_bool(struct file
*file
, const char __user
*user_buf
,
345 size_t count
, loff_t
*ppos
)
349 u32
*val
= file
->private_data
;
351 buf_size
= min(count
, (sizeof(buf
)-1));
352 if (copy_from_user(buf
, user_buf
, buf_size
))
371 static const struct file_operations fops_bool
= {
372 .read
= read_file_bool
,
373 .write
= write_file_bool
,
374 .open
= default_open
,
378 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
379 * @name: a pointer to a string containing the name of the file to create.
380 * @mode: the permission that the file should have
381 * @parent: a pointer to the parent dentry for this file. This should be a
382 * directory dentry if set. If this parameter is %NULL, then the
383 * file will be created in the root of the debugfs filesystem.
384 * @value: a pointer to the variable that the file should read to and write
387 * This function creates a file in debugfs with the given name that
388 * contains the value of the variable @value. If the @mode variable is so
389 * set, it can be read from, and written to.
391 * This function will return a pointer to a dentry if it succeeds. This
392 * pointer must be passed to the debugfs_remove() function when the file is
393 * to be removed (no automatic cleanup happens if your module is unloaded,
394 * you are responsible here.) If an error occurs, %NULL will be returned.
396 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
397 * returned. It is not wise to check for this value, but rather, check for
398 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
401 struct dentry
*debugfs_create_bool(const char *name
, mode_t mode
,
402 struct dentry
*parent
, u32
*value
)
404 return debugfs_create_file(name
, mode
, parent
, value
, &fops_bool
);
406 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
408 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
409 size_t count
, loff_t
*ppos
)
411 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
412 return simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
416 static const struct file_operations fops_blob
= {
417 .read
= read_file_blob
,
418 .open
= default_open
,
422 * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
423 * @name: a pointer to a string containing the name of the file to create.
424 * @mode: the permission that the file should have
425 * @parent: a pointer to the parent dentry for this file. This should be a
426 * directory dentry if set. If this parameter is %NULL, then the
427 * file will be created in the root of the debugfs filesystem.
428 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
429 * to the blob data and the size of the data.
431 * This function creates a file in debugfs with the given name that exports
432 * @blob->data as a binary blob. If the @mode variable is so set it can be
433 * read from. Writing is not supported.
435 * This function will return a pointer to a dentry if it succeeds. This
436 * pointer must be passed to the debugfs_remove() function when the file is
437 * to be removed (no automatic cleanup happens if your module is unloaded,
438 * you are responsible here.) If an error occurs, %NULL will be returned.
440 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
441 * returned. It is not wise to check for this value, but rather, check for
442 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
445 struct dentry
*debugfs_create_blob(const char *name
, mode_t mode
,
446 struct dentry
*parent
,
447 struct debugfs_blob_wrapper
*blob
)
449 return debugfs_create_file(name
, mode
, parent
, blob
, &fops_blob
);
451 EXPORT_SYMBOL_GPL(debugfs_create_blob
);