debugfs: add support for self-protecting attribute file fops
[deliverable/linux.git] / fs / debugfs / file.c
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/filesystems for more details.
13 *
14 */
15
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/debugfs.h>
21 #include <linux/io.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/device.h>
25 #include <linux/srcu.h>
26 #include <asm/poll.h>
27
28 #include "internal.h"
29
30 struct poll_table_struct;
31
32 static ssize_t default_read_file(struct file *file, char __user *buf,
33 size_t count, loff_t *ppos)
34 {
35 return 0;
36 }
37
38 static ssize_t default_write_file(struct file *file, const char __user *buf,
39 size_t count, loff_t *ppos)
40 {
41 return count;
42 }
43
44 const struct file_operations debugfs_noop_file_operations = {
45 .read = default_read_file,
46 .write = default_write_file,
47 .open = simple_open,
48 .llseek = noop_llseek,
49 };
50
51 /**
52 * debugfs_use_file_start - mark the beginning of file data access
53 * @dentry: the dentry object whose data is being accessed.
54 * @srcu_idx: a pointer to some memory to store a SRCU index in.
55 *
56 * Up to a matching call to debugfs_use_file_finish(), any
57 * successive call into the file removing functions debugfs_remove()
58 * and debugfs_remove_recursive() will block. Since associated private
59 * file data may only get freed after a successful return of any of
60 * the removal functions, you may safely access it after a successful
61 * call to debugfs_use_file_start() without worrying about
62 * lifetime issues.
63 *
64 * If -%EIO is returned, the file has already been removed and thus,
65 * it is not safe to access any of its data. If, on the other hand,
66 * it is allowed to access the file data, zero is returned.
67 *
68 * Regardless of the return code, any call to
69 * debugfs_use_file_start() must be followed by a matching call
70 * to debugfs_use_file_finish().
71 */
72 int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
73 __acquires(&debugfs_srcu)
74 {
75 *srcu_idx = srcu_read_lock(&debugfs_srcu);
76 barrier();
77 if (d_unlinked(dentry))
78 return -EIO;
79 return 0;
80 }
81 EXPORT_SYMBOL_GPL(debugfs_use_file_start);
82
83 /**
84 * debugfs_use_file_finish - mark the end of file data access
85 * @srcu_idx: the SRCU index "created" by a former call to
86 * debugfs_use_file_start().
87 *
88 * Allow any ongoing concurrent call into debugfs_remove() or
89 * debugfs_remove_recursive() blocked by a former call to
90 * debugfs_use_file_start() to proceed and return to its caller.
91 */
92 void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
93 {
94 srcu_read_unlock(&debugfs_srcu, srcu_idx);
95 }
96 EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
97
98 #define F_DENTRY(filp) ((filp)->f_path.dentry)
99
100 #define REAL_FOPS_DEREF(dentry) \
101 ((const struct file_operations *)(dentry)->d_fsdata)
102
103 static int open_proxy_open(struct inode *inode, struct file *filp)
104 {
105 const struct dentry *dentry = F_DENTRY(filp);
106 const struct file_operations *real_fops = NULL;
107 int srcu_idx, r;
108
109 r = debugfs_use_file_start(dentry, &srcu_idx);
110 if (r) {
111 r = -ENOENT;
112 goto out;
113 }
114
115 real_fops = REAL_FOPS_DEREF(dentry);
116 real_fops = fops_get(real_fops);
117 if (!real_fops) {
118 /* Huh? Module did not clean up after itself at exit? */
119 WARN(1, "debugfs file owner did not clean up at exit: %pd",
120 dentry);
121 r = -ENXIO;
122 goto out;
123 }
124 replace_fops(filp, real_fops);
125
126 if (real_fops->open)
127 r = real_fops->open(inode, filp);
128
129 out:
130 fops_put(real_fops);
131 debugfs_use_file_finish(srcu_idx);
132 return r;
133 }
134
135 const struct file_operations debugfs_open_proxy_file_operations = {
136 .open = open_proxy_open,
137 };
138
139 #define PROTO(args...) args
140 #define ARGS(args...) args
141
142 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
143 static ret_type full_proxy_ ## name(proto) \
144 { \
145 const struct dentry *dentry = F_DENTRY(filp); \
146 const struct file_operations *real_fops = \
147 REAL_FOPS_DEREF(dentry); \
148 int srcu_idx; \
149 ret_type r; \
150 \
151 r = debugfs_use_file_start(dentry, &srcu_idx); \
152 if (likely(!r)) \
153 r = real_fops->name(args); \
154 debugfs_use_file_finish(srcu_idx); \
155 return r; \
156 }
157
158 FULL_PROXY_FUNC(llseek, loff_t, filp,
159 PROTO(struct file *filp, loff_t offset, int whence),
160 ARGS(filp, offset, whence));
161
162 FULL_PROXY_FUNC(read, ssize_t, filp,
163 PROTO(struct file *filp, char __user *buf, size_t size,
164 loff_t *ppos),
165 ARGS(filp, buf, size, ppos));
166
167 FULL_PROXY_FUNC(write, ssize_t, filp,
168 PROTO(struct file *filp, const char __user *buf, size_t size,
169 loff_t *ppos),
170 ARGS(filp, buf, size, ppos));
171
172 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
173 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
174 ARGS(filp, cmd, arg));
175
176 static unsigned int full_proxy_poll(struct file *filp,
177 struct poll_table_struct *wait)
178 {
179 const struct dentry *dentry = F_DENTRY(filp);
180 const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
181 int srcu_idx;
182 unsigned int r = 0;
183
184 if (debugfs_use_file_start(dentry, &srcu_idx)) {
185 debugfs_use_file_finish(srcu_idx);
186 return POLLHUP;
187 }
188
189 r = real_fops->poll(filp, wait);
190 debugfs_use_file_finish(srcu_idx);
191 return r;
192 }
193
194 static int full_proxy_release(struct inode *inode, struct file *filp)
195 {
196 const struct dentry *dentry = F_DENTRY(filp);
197 const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
198 const struct file_operations *proxy_fops = filp->f_op;
199 int r = 0;
200
201 /*
202 * We must not protect this against removal races here: the
203 * original releaser should be called unconditionally in order
204 * not to leak any resources. Releasers must not assume that
205 * ->i_private is still being meaningful here.
206 */
207 if (real_fops->release)
208 r = real_fops->release(inode, filp);
209
210 replace_fops(filp, d_inode(dentry)->i_fop);
211 kfree((void *)proxy_fops);
212 fops_put(real_fops);
213 return 0;
214 }
215
216 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
217 const struct file_operations *real_fops)
218 {
219 proxy_fops->release = full_proxy_release;
220 if (real_fops->llseek)
221 proxy_fops->llseek = full_proxy_llseek;
222 if (real_fops->read)
223 proxy_fops->read = full_proxy_read;
224 if (real_fops->write)
225 proxy_fops->write = full_proxy_write;
226 if (real_fops->poll)
227 proxy_fops->poll = full_proxy_poll;
228 if (real_fops->unlocked_ioctl)
229 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
230 }
231
232 static int full_proxy_open(struct inode *inode, struct file *filp)
233 {
234 const struct dentry *dentry = F_DENTRY(filp);
235 const struct file_operations *real_fops = NULL;
236 struct file_operations *proxy_fops = NULL;
237 int srcu_idx, r;
238
239 r = debugfs_use_file_start(dentry, &srcu_idx);
240 if (r) {
241 r = -ENOENT;
242 goto out;
243 }
244
245 real_fops = REAL_FOPS_DEREF(dentry);
246 real_fops = fops_get(real_fops);
247 if (!real_fops) {
248 /* Huh? Module did not cleanup after itself at exit? */
249 WARN(1, "debugfs file owner did not clean up at exit: %pd",
250 dentry);
251 r = -ENXIO;
252 goto out;
253 }
254
255 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
256 if (!proxy_fops) {
257 r = -ENOMEM;
258 goto free_proxy;
259 }
260 __full_proxy_fops_init(proxy_fops, real_fops);
261 replace_fops(filp, proxy_fops);
262
263 if (real_fops->open) {
264 r = real_fops->open(inode, filp);
265
266 if (filp->f_op != proxy_fops) {
267 /* No protection against file removal anymore. */
268 WARN(1, "debugfs file owner replaced proxy fops: %pd",
269 dentry);
270 goto free_proxy;
271 }
272 }
273
274 goto out;
275 free_proxy:
276 kfree(proxy_fops);
277 fops_put(real_fops);
278 out:
279 debugfs_use_file_finish(srcu_idx);
280 return r;
281 }
282
283 const struct file_operations debugfs_full_proxy_file_operations = {
284 .open = full_proxy_open,
285 };
286
287 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
288 size_t len, loff_t *ppos)
289 {
290 ssize_t ret;
291 int srcu_idx;
292
293 ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
294 if (likely(!ret))
295 ret = simple_attr_read(file, buf, len, ppos);
296 debugfs_use_file_finish(srcu_idx);
297 return ret;
298 }
299 EXPORT_SYMBOL_GPL(debugfs_attr_read);
300
301 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
302 size_t len, loff_t *ppos)
303 {
304 ssize_t ret;
305 int srcu_idx;
306
307 ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
308 if (likely(!ret))
309 ret = simple_attr_write(file, buf, len, ppos);
310 debugfs_use_file_finish(srcu_idx);
311 return ret;
312 }
313 EXPORT_SYMBOL_GPL(debugfs_attr_write);
314
315 static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
316 struct dentry *parent, void *value,
317 const struct file_operations *fops,
318 const struct file_operations *fops_ro,
319 const struct file_operations *fops_wo)
320 {
321 /* if there are no write bits set, make read only */
322 if (!(mode & S_IWUGO))
323 return debugfs_create_file(name, mode, parent, value, fops_ro);
324 /* if there are no read bits set, make write only */
325 if (!(mode & S_IRUGO))
326 return debugfs_create_file(name, mode, parent, value, fops_wo);
327
328 return debugfs_create_file(name, mode, parent, value, fops);
329 }
330
331 static int debugfs_u8_set(void *data, u64 val)
332 {
333 *(u8 *)data = val;
334 return 0;
335 }
336 static int debugfs_u8_get(void *data, u64 *val)
337 {
338 *val = *(u8 *)data;
339 return 0;
340 }
341 DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
342 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
343 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
344
345 /**
346 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
347 * @name: a pointer to a string containing the name of the file to create.
348 * @mode: the permission that the file should have
349 * @parent: a pointer to the parent dentry for this file. This should be a
350 * directory dentry if set. If this parameter is %NULL, then the
351 * file will be created in the root of the debugfs filesystem.
352 * @value: a pointer to the variable that the file should read to and write
353 * from.
354 *
355 * This function creates a file in debugfs with the given name that
356 * contains the value of the variable @value. If the @mode variable is so
357 * set, it can be read from, and written to.
358 *
359 * This function will return a pointer to a dentry if it succeeds. This
360 * pointer must be passed to the debugfs_remove() function when the file is
361 * to be removed (no automatic cleanup happens if your module is unloaded,
362 * you are responsible here.) If an error occurs, %NULL will be returned.
363 *
364 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
365 * returned. It is not wise to check for this value, but rather, check for
366 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
367 * code.
368 */
369 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
370 struct dentry *parent, u8 *value)
371 {
372 return debugfs_create_mode(name, mode, parent, value, &fops_u8,
373 &fops_u8_ro, &fops_u8_wo);
374 }
375 EXPORT_SYMBOL_GPL(debugfs_create_u8);
376
377 static int debugfs_u16_set(void *data, u64 val)
378 {
379 *(u16 *)data = val;
380 return 0;
381 }
382 static int debugfs_u16_get(void *data, u64 *val)
383 {
384 *val = *(u16 *)data;
385 return 0;
386 }
387 DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
388 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
389 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
390
391 /**
392 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
393 * @name: a pointer to a string containing the name of the file to create.
394 * @mode: the permission that the file should have
395 * @parent: a pointer to the parent dentry for this file. This should be a
396 * directory dentry if set. If this parameter is %NULL, then the
397 * file will be created in the root of the debugfs filesystem.
398 * @value: a pointer to the variable that the file should read to and write
399 * from.
400 *
401 * This function creates a file in debugfs with the given name that
402 * contains the value of the variable @value. If the @mode variable is so
403 * set, it can be read from, and written to.
404 *
405 * This function will return a pointer to a dentry if it succeeds. This
406 * pointer must be passed to the debugfs_remove() function when the file is
407 * to be removed (no automatic cleanup happens if your module is unloaded,
408 * you are responsible here.) If an error occurs, %NULL will be returned.
409 *
410 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
411 * returned. It is not wise to check for this value, but rather, check for
412 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
413 * code.
414 */
415 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
416 struct dentry *parent, u16 *value)
417 {
418 return debugfs_create_mode(name, mode, parent, value, &fops_u16,
419 &fops_u16_ro, &fops_u16_wo);
420 }
421 EXPORT_SYMBOL_GPL(debugfs_create_u16);
422
423 static int debugfs_u32_set(void *data, u64 val)
424 {
425 *(u32 *)data = val;
426 return 0;
427 }
428 static int debugfs_u32_get(void *data, u64 *val)
429 {
430 *val = *(u32 *)data;
431 return 0;
432 }
433 DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
434 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
435 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
436
437 /**
438 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
439 * @name: a pointer to a string containing the name of the file to create.
440 * @mode: the permission that the file should have
441 * @parent: a pointer to the parent dentry for this file. This should be a
442 * directory dentry if set. If this parameter is %NULL, then the
443 * file will be created in the root of the debugfs filesystem.
444 * @value: a pointer to the variable that the file should read to and write
445 * from.
446 *
447 * This function creates a file in debugfs with the given name that
448 * contains the value of the variable @value. If the @mode variable is so
449 * set, it can be read from, and written to.
450 *
451 * This function will return a pointer to a dentry if it succeeds. This
452 * pointer must be passed to the debugfs_remove() function when the file is
453 * to be removed (no automatic cleanup happens if your module is unloaded,
454 * you are responsible here.) If an error occurs, %NULL will be returned.
455 *
456 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
457 * returned. It is not wise to check for this value, but rather, check for
458 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
459 * code.
460 */
461 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
462 struct dentry *parent, u32 *value)
463 {
464 return debugfs_create_mode(name, mode, parent, value, &fops_u32,
465 &fops_u32_ro, &fops_u32_wo);
466 }
467 EXPORT_SYMBOL_GPL(debugfs_create_u32);
468
469 static int debugfs_u64_set(void *data, u64 val)
470 {
471 *(u64 *)data = val;
472 return 0;
473 }
474
475 static int debugfs_u64_get(void *data, u64 *val)
476 {
477 *val = *(u64 *)data;
478 return 0;
479 }
480 DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
481 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
482 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
483
484 /**
485 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
486 * @name: a pointer to a string containing the name of the file to create.
487 * @mode: the permission that the file should have
488 * @parent: a pointer to the parent dentry for this file. This should be a
489 * directory dentry if set. If this parameter is %NULL, then the
490 * file will be created in the root of the debugfs filesystem.
491 * @value: a pointer to the variable that the file should read to and write
492 * from.
493 *
494 * This function creates a file in debugfs with the given name that
495 * contains the value of the variable @value. If the @mode variable is so
496 * set, it can be read from, and written to.
497 *
498 * This function will return a pointer to a dentry if it succeeds. This
499 * pointer must be passed to the debugfs_remove() function when the file is
500 * to be removed (no automatic cleanup happens if your module is unloaded,
501 * you are responsible here.) If an error occurs, %NULL will be returned.
502 *
503 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
504 * returned. It is not wise to check for this value, but rather, check for
505 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
506 * code.
507 */
508 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
509 struct dentry *parent, u64 *value)
510 {
511 return debugfs_create_mode(name, mode, parent, value, &fops_u64,
512 &fops_u64_ro, &fops_u64_wo);
513 }
514 EXPORT_SYMBOL_GPL(debugfs_create_u64);
515
516 static int debugfs_ulong_set(void *data, u64 val)
517 {
518 *(unsigned long *)data = val;
519 return 0;
520 }
521
522 static int debugfs_ulong_get(void *data, u64 *val)
523 {
524 *val = *(unsigned long *)data;
525 return 0;
526 }
527 DEFINE_SIMPLE_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
528 DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
529 DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
530
531 /**
532 * debugfs_create_ulong - create a debugfs file that is used to read and write
533 * an unsigned long value.
534 * @name: a pointer to a string containing the name of the file to create.
535 * @mode: the permission that the file should have
536 * @parent: a pointer to the parent dentry for this file. This should be a
537 * directory dentry if set. If this parameter is %NULL, then the
538 * file will be created in the root of the debugfs filesystem.
539 * @value: a pointer to the variable that the file should read to and write
540 * from.
541 *
542 * This function creates a file in debugfs with the given name that
543 * contains the value of the variable @value. If the @mode variable is so
544 * set, it can be read from, and written to.
545 *
546 * This function will return a pointer to a dentry if it succeeds. This
547 * pointer must be passed to the debugfs_remove() function when the file is
548 * to be removed (no automatic cleanup happens if your module is unloaded,
549 * you are responsible here.) If an error occurs, %NULL will be returned.
550 *
551 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
552 * returned. It is not wise to check for this value, but rather, check for
553 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
554 * code.
555 */
556 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
557 struct dentry *parent, unsigned long *value)
558 {
559 return debugfs_create_mode(name, mode, parent, value, &fops_ulong,
560 &fops_ulong_ro, &fops_ulong_wo);
561 }
562 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
563
564 DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
565 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
566 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
567
568 DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
569 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
570 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
571
572 DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
573 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
574 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
575
576 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
577 DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
578 DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
579
580 /*
581 * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
582 *
583 * These functions are exactly the same as the above functions (but use a hex
584 * output for the decimal challenged). For details look at the above unsigned
585 * decimal functions.
586 */
587
588 /**
589 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
590 * @name: a pointer to a string containing the name of the file to create.
591 * @mode: the permission that the file should have
592 * @parent: a pointer to the parent dentry for this file. This should be a
593 * directory dentry if set. If this parameter is %NULL, then the
594 * file will be created in the root of the debugfs filesystem.
595 * @value: a pointer to the variable that the file should read to and write
596 * from.
597 */
598 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
599 struct dentry *parent, u8 *value)
600 {
601 return debugfs_create_mode(name, mode, parent, value, &fops_x8,
602 &fops_x8_ro, &fops_x8_wo);
603 }
604 EXPORT_SYMBOL_GPL(debugfs_create_x8);
605
606 /**
607 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
608 * @name: a pointer to a string containing the name of the file to create.
609 * @mode: the permission that the file should have
610 * @parent: a pointer to the parent dentry for this file. This should be a
611 * directory dentry if set. If this parameter is %NULL, then the
612 * file will be created in the root of the debugfs filesystem.
613 * @value: a pointer to the variable that the file should read to and write
614 * from.
615 */
616 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
617 struct dentry *parent, u16 *value)
618 {
619 return debugfs_create_mode(name, mode, parent, value, &fops_x16,
620 &fops_x16_ro, &fops_x16_wo);
621 }
622 EXPORT_SYMBOL_GPL(debugfs_create_x16);
623
624 /**
625 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
626 * @name: a pointer to a string containing the name of the file to create.
627 * @mode: the permission that the file should have
628 * @parent: a pointer to the parent dentry for this file. This should be a
629 * directory dentry if set. If this parameter is %NULL, then the
630 * file will be created in the root of the debugfs filesystem.
631 * @value: a pointer to the variable that the file should read to and write
632 * from.
633 */
634 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
635 struct dentry *parent, u32 *value)
636 {
637 return debugfs_create_mode(name, mode, parent, value, &fops_x32,
638 &fops_x32_ro, &fops_x32_wo);
639 }
640 EXPORT_SYMBOL_GPL(debugfs_create_x32);
641
642 /**
643 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
644 * @name: a pointer to a string containing the name of the file to create.
645 * @mode: the permission that the file should have
646 * @parent: a pointer to the parent dentry for this file. This should be a
647 * directory dentry if set. If this parameter is %NULL, then the
648 * file will be created in the root of the debugfs filesystem.
649 * @value: a pointer to the variable that the file should read to and write
650 * from.
651 */
652 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
653 struct dentry *parent, u64 *value)
654 {
655 return debugfs_create_mode(name, mode, parent, value, &fops_x64,
656 &fops_x64_ro, &fops_x64_wo);
657 }
658 EXPORT_SYMBOL_GPL(debugfs_create_x64);
659
660
661 static int debugfs_size_t_set(void *data, u64 val)
662 {
663 *(size_t *)data = val;
664 return 0;
665 }
666 static int debugfs_size_t_get(void *data, u64 *val)
667 {
668 *val = *(size_t *)data;
669 return 0;
670 }
671 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
672 "%llu\n"); /* %llu and %zu are more or less the same */
673 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
674 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
675
676 /**
677 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
678 * @name: a pointer to a string containing the name of the file to create.
679 * @mode: the permission that the file should have
680 * @parent: a pointer to the parent dentry for this file. This should be a
681 * directory dentry if set. If this parameter is %NULL, then the
682 * file will be created in the root of the debugfs filesystem.
683 * @value: a pointer to the variable that the file should read to and write
684 * from.
685 */
686 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
687 struct dentry *parent, size_t *value)
688 {
689 return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
690 &fops_size_t_ro, &fops_size_t_wo);
691 }
692 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
693
694 static int debugfs_atomic_t_set(void *data, u64 val)
695 {
696 atomic_set((atomic_t *)data, val);
697 return 0;
698 }
699 static int debugfs_atomic_t_get(void *data, u64 *val)
700 {
701 *val = atomic_read((atomic_t *)data);
702 return 0;
703 }
704 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
705 debugfs_atomic_t_set, "%lld\n");
706 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
707 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
708
709 /**
710 * debugfs_create_atomic_t - create a debugfs file that is used to read and
711 * write an atomic_t value
712 * @name: a pointer to a string containing the name of the file to create.
713 * @mode: the permission that the file should have
714 * @parent: a pointer to the parent dentry for this file. This should be a
715 * directory dentry if set. If this parameter is %NULL, then the
716 * file will be created in the root of the debugfs filesystem.
717 * @value: a pointer to the variable that the file should read to and write
718 * from.
719 */
720 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
721 struct dentry *parent, atomic_t *value)
722 {
723 return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
724 &fops_atomic_t_ro, &fops_atomic_t_wo);
725 }
726 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
727
728 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
729 size_t count, loff_t *ppos)
730 {
731 char buf[3];
732 bool *val = file->private_data;
733
734 if (*val)
735 buf[0] = 'Y';
736 else
737 buf[0] = 'N';
738 buf[1] = '\n';
739 buf[2] = 0x00;
740 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
741 }
742 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
743
744 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
745 size_t count, loff_t *ppos)
746 {
747 char buf[32];
748 size_t buf_size;
749 bool bv;
750 bool *val = file->private_data;
751
752 buf_size = min(count, (sizeof(buf)-1));
753 if (copy_from_user(buf, user_buf, buf_size))
754 return -EFAULT;
755
756 buf[buf_size] = '\0';
757 if (strtobool(buf, &bv) == 0)
758 *val = bv;
759
760 return count;
761 }
762 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
763
764 static const struct file_operations fops_bool = {
765 .read = debugfs_read_file_bool,
766 .write = debugfs_write_file_bool,
767 .open = simple_open,
768 .llseek = default_llseek,
769 };
770
771 static const struct file_operations fops_bool_ro = {
772 .read = debugfs_read_file_bool,
773 .open = simple_open,
774 .llseek = default_llseek,
775 };
776
777 static const struct file_operations fops_bool_wo = {
778 .write = debugfs_write_file_bool,
779 .open = simple_open,
780 .llseek = default_llseek,
781 };
782
783 /**
784 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
785 * @name: a pointer to a string containing the name of the file to create.
786 * @mode: the permission that the file should have
787 * @parent: a pointer to the parent dentry for this file. This should be a
788 * directory dentry if set. If this parameter is %NULL, then the
789 * file will be created in the root of the debugfs filesystem.
790 * @value: a pointer to the variable that the file should read to and write
791 * from.
792 *
793 * This function creates a file in debugfs with the given name that
794 * contains the value of the variable @value. If the @mode variable is so
795 * set, it can be read from, and written to.
796 *
797 * This function will return a pointer to a dentry if it succeeds. This
798 * pointer must be passed to the debugfs_remove() function when the file is
799 * to be removed (no automatic cleanup happens if your module is unloaded,
800 * you are responsible here.) If an error occurs, %NULL will be returned.
801 *
802 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
803 * returned. It is not wise to check for this value, but rather, check for
804 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
805 * code.
806 */
807 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
808 struct dentry *parent, bool *value)
809 {
810 return debugfs_create_mode(name, mode, parent, value, &fops_bool,
811 &fops_bool_ro, &fops_bool_wo);
812 }
813 EXPORT_SYMBOL_GPL(debugfs_create_bool);
814
815 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
816 size_t count, loff_t *ppos)
817 {
818 struct debugfs_blob_wrapper *blob = file->private_data;
819 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
820 blob->size);
821 }
822
823 static const struct file_operations fops_blob = {
824 .read = read_file_blob,
825 .open = simple_open,
826 .llseek = default_llseek,
827 };
828
829 /**
830 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
831 * @name: a pointer to a string containing the name of the file to create.
832 * @mode: the permission that the file should have
833 * @parent: a pointer to the parent dentry for this file. This should be a
834 * directory dentry if set. If this parameter is %NULL, then the
835 * file will be created in the root of the debugfs filesystem.
836 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
837 * to the blob data and the size of the data.
838 *
839 * This function creates a file in debugfs with the given name that exports
840 * @blob->data as a binary blob. If the @mode variable is so set it can be
841 * read from. Writing is not supported.
842 *
843 * This function will return a pointer to a dentry if it succeeds. This
844 * pointer must be passed to the debugfs_remove() function when the file is
845 * to be removed (no automatic cleanup happens if your module is unloaded,
846 * you are responsible here.) If an error occurs, %NULL will be returned.
847 *
848 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
849 * returned. It is not wise to check for this value, but rather, check for
850 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
851 * code.
852 */
853 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
854 struct dentry *parent,
855 struct debugfs_blob_wrapper *blob)
856 {
857 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
858 }
859 EXPORT_SYMBOL_GPL(debugfs_create_blob);
860
861 struct array_data {
862 void *array;
863 u32 elements;
864 };
865
866 static size_t u32_format_array(char *buf, size_t bufsize,
867 u32 *array, int array_size)
868 {
869 size_t ret = 0;
870
871 while (--array_size >= 0) {
872 size_t len;
873 char term = array_size ? ' ' : '\n';
874
875 len = snprintf(buf, bufsize, "%u%c", *array++, term);
876 ret += len;
877
878 buf += len;
879 bufsize -= len;
880 }
881 return ret;
882 }
883
884 static int u32_array_open(struct inode *inode, struct file *file)
885 {
886 struct array_data *data = inode->i_private;
887 int size, elements = data->elements;
888 char *buf;
889
890 /*
891 * Max size:
892 * - 10 digits + ' '/'\n' = 11 bytes per number
893 * - terminating NUL character
894 */
895 size = elements*11;
896 buf = kmalloc(size+1, GFP_KERNEL);
897 if (!buf)
898 return -ENOMEM;
899 buf[size] = 0;
900
901 file->private_data = buf;
902 u32_format_array(buf, size, data->array, data->elements);
903
904 return nonseekable_open(inode, file);
905 }
906
907 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
908 loff_t *ppos)
909 {
910 size_t size = strlen(file->private_data);
911
912 return simple_read_from_buffer(buf, len, ppos,
913 file->private_data, size);
914 }
915
916 static int u32_array_release(struct inode *inode, struct file *file)
917 {
918 kfree(file->private_data);
919
920 return 0;
921 }
922
923 static const struct file_operations u32_array_fops = {
924 .owner = THIS_MODULE,
925 .open = u32_array_open,
926 .release = u32_array_release,
927 .read = u32_array_read,
928 .llseek = no_llseek,
929 };
930
931 /**
932 * debugfs_create_u32_array - create a debugfs file that is used to read u32
933 * array.
934 * @name: a pointer to a string containing the name of the file to create.
935 * @mode: the permission that the file should have.
936 * @parent: a pointer to the parent dentry for this file. This should be a
937 * directory dentry if set. If this parameter is %NULL, then the
938 * file will be created in the root of the debugfs filesystem.
939 * @array: u32 array that provides data.
940 * @elements: total number of elements in the array.
941 *
942 * This function creates a file in debugfs with the given name that exports
943 * @array as data. If the @mode variable is so set it can be read from.
944 * Writing is not supported. Seek within the file is also not supported.
945 * Once array is created its size can not be changed.
946 *
947 * The function returns a pointer to dentry on success. If debugfs is not
948 * enabled in the kernel, the value -%ENODEV will be returned.
949 */
950 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
951 struct dentry *parent,
952 u32 *array, u32 elements)
953 {
954 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
955
956 if (data == NULL)
957 return NULL;
958
959 data->array = array;
960 data->elements = elements;
961
962 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
963 }
964 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
965
966 #ifdef CONFIG_HAS_IOMEM
967
968 /*
969 * The regset32 stuff is used to print 32-bit registers using the
970 * seq_file utilities. We offer printing a register set in an already-opened
971 * sequential file or create a debugfs file that only prints a regset32.
972 */
973
974 /**
975 * debugfs_print_regs32 - use seq_print to describe a set of registers
976 * @s: the seq_file structure being used to generate output
977 * @regs: an array if struct debugfs_reg32 structures
978 * @nregs: the length of the above array
979 * @base: the base address to be used in reading the registers
980 * @prefix: a string to be prefixed to every output line
981 *
982 * This function outputs a text block describing the current values of
983 * some 32-bit hardware registers. It is meant to be used within debugfs
984 * files based on seq_file that need to show registers, intermixed with other
985 * information. The prefix argument may be used to specify a leading string,
986 * because some peripherals have several blocks of identical registers,
987 * for example configuration of dma channels
988 */
989 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
990 int nregs, void __iomem *base, char *prefix)
991 {
992 int i;
993
994 for (i = 0; i < nregs; i++, regs++) {
995 if (prefix)
996 seq_printf(s, "%s", prefix);
997 seq_printf(s, "%s = 0x%08x\n", regs->name,
998 readl(base + regs->offset));
999 if (seq_has_overflowed(s))
1000 break;
1001 }
1002 }
1003 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1004
1005 static int debugfs_show_regset32(struct seq_file *s, void *data)
1006 {
1007 struct debugfs_regset32 *regset = s->private;
1008
1009 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1010 return 0;
1011 }
1012
1013 static int debugfs_open_regset32(struct inode *inode, struct file *file)
1014 {
1015 return single_open(file, debugfs_show_regset32, inode->i_private);
1016 }
1017
1018 static const struct file_operations fops_regset32 = {
1019 .open = debugfs_open_regset32,
1020 .read = seq_read,
1021 .llseek = seq_lseek,
1022 .release = single_release,
1023 };
1024
1025 /**
1026 * debugfs_create_regset32 - create a debugfs file that returns register values
1027 * @name: a pointer to a string containing the name of the file to create.
1028 * @mode: the permission that the file should have
1029 * @parent: a pointer to the parent dentry for this file. This should be a
1030 * directory dentry if set. If this parameter is %NULL, then the
1031 * file will be created in the root of the debugfs filesystem.
1032 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1033 * to an array of register definitions, the array size and the base
1034 * address where the register bank is to be found.
1035 *
1036 * This function creates a file in debugfs with the given name that reports
1037 * the names and values of a set of 32-bit registers. If the @mode variable
1038 * is so set it can be read from. Writing is not supported.
1039 *
1040 * This function will return a pointer to a dentry if it succeeds. This
1041 * pointer must be passed to the debugfs_remove() function when the file is
1042 * to be removed (no automatic cleanup happens if your module is unloaded,
1043 * you are responsible here.) If an error occurs, %NULL will be returned.
1044 *
1045 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1046 * returned. It is not wise to check for this value, but rather, check for
1047 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1048 * code.
1049 */
1050 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
1051 struct dentry *parent,
1052 struct debugfs_regset32 *regset)
1053 {
1054 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1055 }
1056 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1057
1058 #endif /* CONFIG_HAS_IOMEM */
1059
1060 struct debugfs_devm_entry {
1061 int (*read)(struct seq_file *seq, void *data);
1062 struct device *dev;
1063 };
1064
1065 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1066 {
1067 struct debugfs_devm_entry *entry = inode->i_private;
1068
1069 return single_open(f, entry->read, entry->dev);
1070 }
1071
1072 static const struct file_operations debugfs_devm_entry_ops = {
1073 .owner = THIS_MODULE,
1074 .open = debugfs_devm_entry_open,
1075 .release = single_release,
1076 .read = seq_read,
1077 .llseek = seq_lseek
1078 };
1079
1080 /**
1081 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1082 *
1083 * @dev: device related to this debugfs file.
1084 * @name: name of the debugfs file.
1085 * @parent: a pointer to the parent dentry for this file. This should be a
1086 * directory dentry if set. If this parameter is %NULL, then the
1087 * file will be created in the root of the debugfs filesystem.
1088 * @read_fn: function pointer called to print the seq_file content.
1089 */
1090 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1091 struct dentry *parent,
1092 int (*read_fn)(struct seq_file *s,
1093 void *data))
1094 {
1095 struct debugfs_devm_entry *entry;
1096
1097 if (IS_ERR(parent))
1098 return ERR_PTR(-ENOENT);
1099
1100 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1101 if (!entry)
1102 return ERR_PTR(-ENOMEM);
1103
1104 entry->read = read_fn;
1105 entry->dev = dev;
1106
1107 return debugfs_create_file(name, S_IRUGO, parent, entry,
1108 &debugfs_devm_entry_ops);
1109 }
1110 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1111
This page took 0.059349 seconds and 5 git commands to generate.