]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/debugfs/file.c
Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[mirror_ubuntu-bionic-kernel.git] / fs / debugfs / file.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/pagemap.h>
66f54963 19#include <linux/namei.h>
1da177e4
LT
20#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{
8e18e294
TT
36 if (inode->i_private)
37 file->private_data = inode->i_private;
1da177e4
LT
38
39 return 0;
40}
41
4b6f5d20 42const struct file_operations debugfs_file_operations = {
1da177e4
LT
43 .read = default_read_file,
44 .write = default_write_file,
45 .open = default_open,
46};
47
66f54963
PO
48static 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
acaefc25
AB
59static 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");
1da177e4
LT
68
69/**
6468b3af 70 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
1da177e4
LT
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
6468b3af 74 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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
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,
6468b3af 86 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 87 *
6468b3af 88 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 89 * returned. It is not wise to check for this value, but rather, check for
6468b3af 90 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
91 * 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
acaefc25
AB
100static 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
1da177e4 110/**
6468b3af 111 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1da177e4
LT
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
6468b3af 115 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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
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,
6468b3af 127 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 128 *
6468b3af 129 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 130 * returned. It is not wise to check for this value, but rather, check for
6468b3af 131 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
132 * 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
acaefc25
AB
141static 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
1da177e4 151/**
6468b3af 152 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1da177e4
LT
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
6468b3af 156 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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
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,
6468b3af 168 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 169 *
6468b3af 170 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 171 * returned. It is not wise to check for this value, but rather, check for
6468b3af 172 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
173 * 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
182static ssize_t read_file_bool(struct file *file, char __user *user_buf,
183 size_t count, loff_t *ppos)
184{
185 char buf[3];
186 u32 *val = file->private_data;
187
188 if (*val)
189 buf[0] = 'Y';
190 else
191 buf[0] = 'N';
192 buf[1] = '\n';
193 buf[2] = 0x00;
194 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
195}
196
197static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
198 size_t count, loff_t *ppos)
199{
200 char buf[32];
201 int buf_size;
202 u32 *val = file->private_data;
203
204 buf_size = min(count, (sizeof(buf)-1));
205 if (copy_from_user(buf, user_buf, buf_size))
206 return -EFAULT;
207
208 switch (buf[0]) {
209 case 'y':
210 case 'Y':
211 case '1':
212 *val = 1;
213 break;
214 case 'n':
215 case 'N':
216 case '0':
217 *val = 0;
218 break;
219 }
220
221 return count;
222}
223
4b6f5d20 224static const struct file_operations fops_bool = {
1da177e4
LT
225 .read = read_file_bool,
226 .write = write_file_bool,
227 .open = default_open,
228};
229
230/**
6468b3af 231 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
1da177e4
LT
232 * @name: a pointer to a string containing the name of the file to create.
233 * @mode: the permission that the file should have
234 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 235 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
236 * file will be created in the root of the debugfs filesystem.
237 * @value: a pointer to the variable that the file should read to and write
238 * from.
239 *
240 * This function creates a file in debugfs with the given name that
241 * contains the value of the variable @value. If the @mode variable is so
242 * set, it can be read from, and written to.
243 *
244 * This function will return a pointer to a dentry if it succeeds. This
245 * pointer must be passed to the debugfs_remove() function when the file is
246 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 247 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 248 *
6468b3af 249 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 250 * returned. It is not wise to check for this value, but rather, check for
6468b3af 251 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
252 * code.
253 */
254struct dentry *debugfs_create_bool(const char *name, mode_t mode,
255 struct dentry *parent, u32 *value)
256{
257 return debugfs_create_file(name, mode, parent, value, &fops_bool);
258}
259EXPORT_SYMBOL_GPL(debugfs_create_bool);
260
dd308bc3
ME
261static ssize_t read_file_blob(struct file *file, char __user *user_buf,
262 size_t count, loff_t *ppos)
263{
264 struct debugfs_blob_wrapper *blob = file->private_data;
265 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
266 blob->size);
267}
268
00977a59 269static const struct file_operations fops_blob = {
dd308bc3
ME
270 .read = read_file_blob,
271 .open = default_open,
272};
273
274/**
6468b3af 275 * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
dd308bc3
ME
276 * @name: a pointer to a string containing the name of the file to create.
277 * @mode: the permission that the file should have
278 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 279 * directory dentry if set. If this parameter is %NULL, then the
dd308bc3
ME
280 * file will be created in the root of the debugfs filesystem.
281 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
282 * to the blob data and the size of the data.
283 *
284 * This function creates a file in debugfs with the given name that exports
285 * @blob->data as a binary blob. If the @mode variable is so set it can be
286 * read from. Writing is not supported.
287 *
288 * This function will return a pointer to a dentry if it succeeds. This
289 * pointer must be passed to the debugfs_remove() function when the file is
290 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 291 * you are responsible here.) If an error occurs, %NULL will be returned.
dd308bc3 292 *
6468b3af 293 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
dd308bc3 294 * returned. It is not wise to check for this value, but rather, check for
6468b3af 295 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
dd308bc3
ME
296 * code.
297 */
298struct dentry *debugfs_create_blob(const char *name, mode_t mode,
299 struct dentry *parent,
300 struct debugfs_blob_wrapper *blob)
301{
302 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
303}
304EXPORT_SYMBOL_GPL(debugfs_create_blob);