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/kernel-api 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 void debugfs_u8_set(void *data
, u64 val
)
63 static u64
debugfs_u8_get(void *data
)
67 DEFINE_SIMPLE_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
70 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
71 * @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
74 * directory dentry if set. If this parameter is %NULL, then the
75 * 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
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.
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,
86 * you are responsible here.) If an error occurs, %NULL will be returned.
88 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
89 * returned. It is not wise to check for this value, but rather, check for
90 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
93 struct dentry
*debugfs_create_u8(const char *name
, mode_t mode
,
94 struct dentry
*parent
, u8
*value
)
96 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u8
);
98 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
100 static void debugfs_u16_set(void *data
, u64 val
)
104 static u64
debugfs_u16_get(void *data
)
108 DEFINE_SIMPLE_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
111 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
112 * @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
115 * directory dentry if set. If this parameter is %NULL, then the
116 * 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
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.
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,
127 * you are responsible here.) If an error occurs, %NULL will be returned.
129 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
130 * returned. It is not wise to check for this value, but rather, check for
131 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
134 struct dentry
*debugfs_create_u16(const char *name
, mode_t mode
,
135 struct dentry
*parent
, u16
*value
)
137 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u16
);
139 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
141 static void debugfs_u32_set(void *data
, u64 val
)
145 static u64
debugfs_u32_get(void *data
)
149 DEFINE_SIMPLE_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
152 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
153 * @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
156 * directory dentry if set. If this parameter is %NULL, then the
157 * 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
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.
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,
168 * you are responsible here.) If an error occurs, %NULL will be returned.
170 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
171 * returned. It is not wise to check for this value, but rather, check for
172 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
175 struct dentry
*debugfs_create_u32(const char *name
, mode_t mode
,
176 struct dentry
*parent
, u32
*value
)
178 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u32
);
180 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
182 static void debugfs_u64_set(void *data
, u64 val
)
187 static u64
debugfs_u64_get(void *data
)
191 DEFINE_SIMPLE_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
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
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.
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.
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
217 struct dentry
*debugfs_create_u64(const char *name
, mode_t mode
,
218 struct dentry
*parent
, u64
*value
)
220 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u64
);
222 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
224 static ssize_t
read_file_bool(struct file
*file
, char __user
*user_buf
,
225 size_t count
, loff_t
*ppos
)
228 u32
*val
= file
->private_data
;
236 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
239 static ssize_t
write_file_bool(struct file
*file
, const char __user
*user_buf
,
240 size_t count
, loff_t
*ppos
)
244 u32
*val
= file
->private_data
;
246 buf_size
= min(count
, (sizeof(buf
)-1));
247 if (copy_from_user(buf
, user_buf
, buf_size
))
266 static const struct file_operations fops_bool
= {
267 .read
= read_file_bool
,
268 .write
= write_file_bool
,
269 .open
= default_open
,
273 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
274 * @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
277 * directory dentry if set. If this parameter is %NULL, then the
278 * 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
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.
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,
289 * you are responsible here.) If an error occurs, %NULL will be returned.
291 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
292 * returned. It is not wise to check for this value, but rather, check for
293 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
296 struct dentry
*debugfs_create_bool(const char *name
, mode_t mode
,
297 struct dentry
*parent
, u32
*value
)
299 return debugfs_create_file(name
, mode
, parent
, value
, &fops_bool
);
301 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
303 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
304 size_t count
, loff_t
*ppos
)
306 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
307 return simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
311 static const struct file_operations fops_blob
= {
312 .read
= read_file_blob
,
313 .open
= default_open
,
317 * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
318 * @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
321 * directory dentry if set. If this parameter is %NULL, then the
322 * 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.
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.
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,
333 * you are responsible here.) If an error occurs, %NULL will be returned.
335 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
336 * returned. It is not wise to check for this value, but rather, check for
337 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
340 struct dentry
*debugfs_create_blob(const char *name
, mode_t mode
,
341 struct dentry
*parent
,
342 struct debugfs_blob_wrapper
*blob
)
344 return debugfs_create_file(name
, mode
, parent
, blob
, &fops_blob
);
346 EXPORT_SYMBOL_GPL(debugfs_create_blob
);