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/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/debugfs.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/device.h>
25 #include <linux/srcu.h>
30 struct poll_table_struct
;
32 static ssize_t
default_read_file(struct file
*file
, char __user
*buf
,
33 size_t count
, loff_t
*ppos
)
38 static ssize_t
default_write_file(struct file
*file
, const char __user
*buf
,
39 size_t count
, loff_t
*ppos
)
44 const struct file_operations debugfs_noop_file_operations
= {
45 .read
= default_read_file
,
46 .write
= default_write_file
,
48 .llseek
= noop_llseek
,
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.
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
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.
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().
72 int debugfs_use_file_start(const struct dentry
*dentry
, int *srcu_idx
)
73 __acquires(&debugfs_srcu
)
75 *srcu_idx
= srcu_read_lock(&debugfs_srcu
);
77 if (d_unlinked(dentry
))
81 EXPORT_SYMBOL_GPL(debugfs_use_file_start
);
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().
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.
92 void debugfs_use_file_finish(int srcu_idx
) __releases(&debugfs_srcu
)
94 srcu_read_unlock(&debugfs_srcu
, srcu_idx
);
96 EXPORT_SYMBOL_GPL(debugfs_use_file_finish
);
98 #define F_DENTRY(filp) ((filp)->f_path.dentry)
100 #define REAL_FOPS_DEREF(dentry) \
101 ((const struct file_operations *)(dentry)->d_fsdata)
103 static int open_proxy_open(struct inode
*inode
, struct file
*filp
)
105 const struct dentry
*dentry
= F_DENTRY(filp
);
106 const struct file_operations
*real_fops
= NULL
;
109 r
= debugfs_use_file_start(dentry
, &srcu_idx
);
115 real_fops
= REAL_FOPS_DEREF(dentry
);
116 real_fops
= fops_get(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",
124 replace_fops(filp
, real_fops
);
127 r
= real_fops
->open(inode
, filp
);
131 debugfs_use_file_finish(srcu_idx
);
135 const struct file_operations debugfs_open_proxy_file_operations
= {
136 .open
= open_proxy_open
,
139 #define PROTO(args...) args
140 #define ARGS(args...) args
142 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
143 static ret_type full_proxy_ ## name(proto) \
145 const struct dentry *dentry = F_DENTRY(filp); \
146 const struct file_operations *real_fops = \
147 REAL_FOPS_DEREF(dentry); \
151 r = debugfs_use_file_start(dentry, &srcu_idx); \
153 r = real_fops->name(args); \
154 debugfs_use_file_finish(srcu_idx); \
158 FULL_PROXY_FUNC(llseek
, loff_t
, filp
,
159 PROTO(struct file
*filp
, loff_t offset
, int whence
),
160 ARGS(filp
, offset
, whence
));
162 FULL_PROXY_FUNC(read
, ssize_t
, filp
,
163 PROTO(struct file
*filp
, char __user
*buf
, size_t size
,
165 ARGS(filp
, buf
, size
, ppos
));
167 FULL_PROXY_FUNC(write
, ssize_t
, filp
,
168 PROTO(struct file
*filp
, const char __user
*buf
, size_t size
,
170 ARGS(filp
, buf
, size
, ppos
));
172 FULL_PROXY_FUNC(unlocked_ioctl
, long, filp
,
173 PROTO(struct file
*filp
, unsigned int cmd
, unsigned long arg
),
174 ARGS(filp
, cmd
, arg
));
176 static unsigned int full_proxy_poll(struct file
*filp
,
177 struct poll_table_struct
*wait
)
179 const struct dentry
*dentry
= F_DENTRY(filp
);
180 const struct file_operations
*real_fops
= REAL_FOPS_DEREF(dentry
);
184 if (debugfs_use_file_start(dentry
, &srcu_idx
)) {
185 debugfs_use_file_finish(srcu_idx
);
189 r
= real_fops
->poll(filp
, wait
);
190 debugfs_use_file_finish(srcu_idx
);
194 static int full_proxy_release(struct inode
*inode
, struct file
*filp
)
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
;
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.
207 if (real_fops
->release
)
208 r
= real_fops
->release(inode
, filp
);
210 replace_fops(filp
, d_inode(dentry
)->i_fop
);
211 kfree((void *)proxy_fops
);
216 static void __full_proxy_fops_init(struct file_operations
*proxy_fops
,
217 const struct file_operations
*real_fops
)
219 proxy_fops
->release
= full_proxy_release
;
220 if (real_fops
->llseek
)
221 proxy_fops
->llseek
= full_proxy_llseek
;
223 proxy_fops
->read
= full_proxy_read
;
224 if (real_fops
->write
)
225 proxy_fops
->write
= full_proxy_write
;
227 proxy_fops
->poll
= full_proxy_poll
;
228 if (real_fops
->unlocked_ioctl
)
229 proxy_fops
->unlocked_ioctl
= full_proxy_unlocked_ioctl
;
232 static int full_proxy_open(struct inode
*inode
, struct file
*filp
)
234 const struct dentry
*dentry
= F_DENTRY(filp
);
235 const struct file_operations
*real_fops
= NULL
;
236 struct file_operations
*proxy_fops
= NULL
;
239 r
= debugfs_use_file_start(dentry
, &srcu_idx
);
245 real_fops
= REAL_FOPS_DEREF(dentry
);
246 real_fops
= fops_get(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",
255 proxy_fops
= kzalloc(sizeof(*proxy_fops
), GFP_KERNEL
);
260 __full_proxy_fops_init(proxy_fops
, real_fops
);
261 replace_fops(filp
, proxy_fops
);
263 if (real_fops
->open
) {
264 r
= real_fops
->open(inode
, filp
);
266 if (filp
->f_op
!= proxy_fops
) {
267 /* No protection against file removal anymore. */
268 WARN(1, "debugfs file owner replaced proxy fops: %pd",
279 debugfs_use_file_finish(srcu_idx
);
283 const struct file_operations debugfs_full_proxy_file_operations
= {
284 .open
= full_proxy_open
,
287 ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
288 size_t len
, loff_t
*ppos
)
293 ret
= debugfs_use_file_start(F_DENTRY(file
), &srcu_idx
);
295 ret
= simple_attr_read(file
, buf
, len
, ppos
);
296 debugfs_use_file_finish(srcu_idx
);
299 EXPORT_SYMBOL_GPL(debugfs_attr_read
);
301 ssize_t
debugfs_attr_write(struct file
*file
, const char __user
*buf
,
302 size_t len
, loff_t
*ppos
)
307 ret
= debugfs_use_file_start(F_DENTRY(file
), &srcu_idx
);
309 ret
= simple_attr_write(file
, buf
, len
, ppos
);
310 debugfs_use_file_finish(srcu_idx
);
313 EXPORT_SYMBOL_GPL(debugfs_attr_write
);
315 static struct dentry
*debugfs_create_mode_unsafe(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
)
321 /* if there are no write bits set, make read only */
322 if (!(mode
& S_IWUGO
))
323 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
325 /* if there are no read bits set, make write only */
326 if (!(mode
& S_IRUGO
))
327 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
330 return debugfs_create_file_unsafe(name
, mode
, parent
, value
, fops
);
333 static int debugfs_u8_set(void *data
, u64 val
)
338 static int debugfs_u8_get(void *data
, u64
*val
)
343 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
344 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro
, debugfs_u8_get
, NULL
, "%llu\n");
345 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo
, NULL
, debugfs_u8_set
, "%llu\n");
348 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
349 * @name: a pointer to a string containing the name of the file to create.
350 * @mode: the permission that the file should have
351 * @parent: a pointer to the parent dentry for this file. This should be a
352 * directory dentry if set. If this parameter is %NULL, then the
353 * file will be created in the root of the debugfs filesystem.
354 * @value: a pointer to the variable that the file should read to and write
357 * This function creates a file in debugfs with the given name that
358 * contains the value of the variable @value. If the @mode variable is so
359 * set, it can be read from, and written to.
361 * This function will return a pointer to a dentry if it succeeds. This
362 * pointer must be passed to the debugfs_remove() function when the file is
363 * to be removed (no automatic cleanup happens if your module is unloaded,
364 * you are responsible here.) If an error occurs, %NULL will be returned.
366 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
367 * returned. It is not wise to check for this value, but rather, check for
368 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
371 struct dentry
*debugfs_create_u8(const char *name
, umode_t mode
,
372 struct dentry
*parent
, u8
*value
)
374 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u8
,
375 &fops_u8_ro
, &fops_u8_wo
);
377 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
379 static int debugfs_u16_set(void *data
, u64 val
)
384 static int debugfs_u16_get(void *data
, u64
*val
)
389 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
390 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro
, debugfs_u16_get
, NULL
, "%llu\n");
391 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo
, NULL
, debugfs_u16_set
, "%llu\n");
394 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
395 * @name: a pointer to a string containing the name of the file to create.
396 * @mode: the permission that the file should have
397 * @parent: a pointer to the parent dentry for this file. This should be a
398 * directory dentry if set. If this parameter is %NULL, then the
399 * file will be created in the root of the debugfs filesystem.
400 * @value: a pointer to the variable that the file should read to and write
403 * This function creates a file in debugfs with the given name that
404 * contains the value of the variable @value. If the @mode variable is so
405 * set, it can be read from, and written to.
407 * This function will return a pointer to a dentry if it succeeds. This
408 * pointer must be passed to the debugfs_remove() function when the file is
409 * to be removed (no automatic cleanup happens if your module is unloaded,
410 * you are responsible here.) If an error occurs, %NULL will be returned.
412 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
413 * returned. It is not wise to check for this value, but rather, check for
414 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
417 struct dentry
*debugfs_create_u16(const char *name
, umode_t mode
,
418 struct dentry
*parent
, u16
*value
)
420 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u16
,
421 &fops_u16_ro
, &fops_u16_wo
);
423 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
425 static int debugfs_u32_set(void *data
, u64 val
)
430 static int debugfs_u32_get(void *data
, u64
*val
)
435 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
436 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro
, debugfs_u32_get
, NULL
, "%llu\n");
437 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo
, NULL
, debugfs_u32_set
, "%llu\n");
440 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
441 * @name: a pointer to a string containing the name of the file to create.
442 * @mode: the permission that the file should have
443 * @parent: a pointer to the parent dentry for this file. This should be a
444 * directory dentry if set. If this parameter is %NULL, then the
445 * file will be created in the root of the debugfs filesystem.
446 * @value: a pointer to the variable that the file should read to and write
449 * This function creates a file in debugfs with the given name that
450 * contains the value of the variable @value. If the @mode variable is so
451 * set, it can be read from, and written to.
453 * This function will return a pointer to a dentry if it succeeds. This
454 * pointer must be passed to the debugfs_remove() function when the file is
455 * to be removed (no automatic cleanup happens if your module is unloaded,
456 * you are responsible here.) If an error occurs, %NULL will be returned.
458 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
459 * returned. It is not wise to check for this value, but rather, check for
460 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
463 struct dentry
*debugfs_create_u32(const char *name
, umode_t mode
,
464 struct dentry
*parent
, u32
*value
)
466 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u32
,
467 &fops_u32_ro
, &fops_u32_wo
);
469 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
471 static int debugfs_u64_set(void *data
, u64 val
)
477 static int debugfs_u64_get(void *data
, u64
*val
)
482 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
483 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro
, debugfs_u64_get
, NULL
, "%llu\n");
484 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo
, NULL
, debugfs_u64_set
, "%llu\n");
487 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
488 * @name: a pointer to a string containing the name of the file to create.
489 * @mode: the permission that the file should have
490 * @parent: a pointer to the parent dentry for this file. This should be a
491 * directory dentry if set. If this parameter is %NULL, then the
492 * file will be created in the root of the debugfs filesystem.
493 * @value: a pointer to the variable that the file should read to and write
496 * This function creates a file in debugfs with the given name that
497 * contains the value of the variable @value. If the @mode variable is so
498 * set, it can be read from, and written to.
500 * This function will return a pointer to a dentry if it succeeds. This
501 * pointer must be passed to the debugfs_remove() function when the file is
502 * to be removed (no automatic cleanup happens if your module is unloaded,
503 * you are responsible here.) If an error occurs, %NULL will be returned.
505 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
506 * returned. It is not wise to check for this value, but rather, check for
507 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
510 struct dentry
*debugfs_create_u64(const char *name
, umode_t mode
,
511 struct dentry
*parent
, u64
*value
)
513 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u64
,
514 &fops_u64_ro
, &fops_u64_wo
);
516 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
518 static int debugfs_ulong_set(void *data
, u64 val
)
520 *(unsigned long *)data
= val
;
524 static int debugfs_ulong_get(void *data
, u64
*val
)
526 *val
= *(unsigned long *)data
;
529 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong
, debugfs_ulong_get
, debugfs_ulong_set
,
531 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro
, debugfs_ulong_get
, NULL
, "%llu\n");
532 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo
, NULL
, debugfs_ulong_set
, "%llu\n");
535 * debugfs_create_ulong - create a debugfs file that is used to read and write
536 * an unsigned long value.
537 * @name: a pointer to a string containing the name of the file to create.
538 * @mode: the permission that the file should have
539 * @parent: a pointer to the parent dentry for this file. This should be a
540 * directory dentry if set. If this parameter is %NULL, then the
541 * file will be created in the root of the debugfs filesystem.
542 * @value: a pointer to the variable that the file should read to and write
545 * This function creates a file in debugfs with the given name that
546 * contains the value of the variable @value. If the @mode variable is so
547 * set, it can be read from, and written to.
549 * This function will return a pointer to a dentry if it succeeds. This
550 * pointer must be passed to the debugfs_remove() function when the file is
551 * to be removed (no automatic cleanup happens if your module is unloaded,
552 * you are responsible here.) If an error occurs, %NULL will be returned.
554 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
555 * returned. It is not wise to check for this value, but rather, check for
556 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
559 struct dentry
*debugfs_create_ulong(const char *name
, umode_t mode
,
560 struct dentry
*parent
, unsigned long *value
)
562 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
,
563 &fops_ulong
, &fops_ulong_ro
,
566 EXPORT_SYMBOL_GPL(debugfs_create_ulong
);
568 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
569 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro
, debugfs_u8_get
, NULL
, "0x%02llx\n");
570 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo
, NULL
, debugfs_u8_set
, "0x%02llx\n");
572 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
,
574 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro
, debugfs_u16_get
, NULL
, "0x%04llx\n");
575 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo
, NULL
, debugfs_u16_set
, "0x%04llx\n");
577 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
,
579 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro
, debugfs_u32_get
, NULL
, "0x%08llx\n");
580 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo
, NULL
, debugfs_u32_set
, "0x%08llx\n");
582 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64
, debugfs_u64_get
, debugfs_u64_set
,
584 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro
, debugfs_u64_get
, NULL
, "0x%016llx\n");
585 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo
, NULL
, debugfs_u64_set
, "0x%016llx\n");
588 * 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
590 * These functions are exactly the same as the above functions (but use a hex
591 * output for the decimal challenged). For details look at the above unsigned
596 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
597 * @name: a pointer to a string containing the name of the file to create.
598 * @mode: the permission that the file should have
599 * @parent: a pointer to the parent dentry for this file. This should be a
600 * directory dentry if set. If this parameter is %NULL, then the
601 * file will be created in the root of the debugfs filesystem.
602 * @value: a pointer to the variable that the file should read to and write
605 struct dentry
*debugfs_create_x8(const char *name
, umode_t mode
,
606 struct dentry
*parent
, u8
*value
)
608 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x8
,
609 &fops_x8_ro
, &fops_x8_wo
);
611 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
614 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
615 * @name: a pointer to a string containing the name of the file to create.
616 * @mode: the permission that the file should have
617 * @parent: a pointer to the parent dentry for this file. This should be a
618 * directory dentry if set. If this parameter is %NULL, then the
619 * file will be created in the root of the debugfs filesystem.
620 * @value: a pointer to the variable that the file should read to and write
623 struct dentry
*debugfs_create_x16(const char *name
, umode_t mode
,
624 struct dentry
*parent
, u16
*value
)
626 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x16
,
627 &fops_x16_ro
, &fops_x16_wo
);
629 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
632 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
633 * @name: a pointer to a string containing the name of the file to create.
634 * @mode: the permission that the file should have
635 * @parent: a pointer to the parent dentry for this file. This should be a
636 * directory dentry if set. If this parameter is %NULL, then the
637 * file will be created in the root of the debugfs filesystem.
638 * @value: a pointer to the variable that the file should read to and write
641 struct dentry
*debugfs_create_x32(const char *name
, umode_t mode
,
642 struct dentry
*parent
, u32
*value
)
644 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x32
,
645 &fops_x32_ro
, &fops_x32_wo
);
647 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
650 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
651 * @name: a pointer to a string containing the name of the file to create.
652 * @mode: the permission that the file should have
653 * @parent: a pointer to the parent dentry for this file. This should be a
654 * directory dentry if set. If this parameter is %NULL, then the
655 * file will be created in the root of the debugfs filesystem.
656 * @value: a pointer to the variable that the file should read to and write
659 struct dentry
*debugfs_create_x64(const char *name
, umode_t mode
,
660 struct dentry
*parent
, u64
*value
)
662 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x64
,
663 &fops_x64_ro
, &fops_x64_wo
);
665 EXPORT_SYMBOL_GPL(debugfs_create_x64
);
668 static int debugfs_size_t_set(void *data
, u64 val
)
670 *(size_t *)data
= val
;
673 static int debugfs_size_t_get(void *data
, u64
*val
)
675 *val
= *(size_t *)data
;
678 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
679 "%llu\n"); /* %llu and %zu are more or less the same */
680 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro
, debugfs_size_t_get
, NULL
, "%llu\n");
681 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo
, NULL
, debugfs_size_t_set
, "%llu\n");
684 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
685 * @name: a pointer to a string containing the name of the file to create.
686 * @mode: the permission that the file should have
687 * @parent: a pointer to the parent dentry for this file. This should be a
688 * directory dentry if set. If this parameter is %NULL, then the
689 * file will be created in the root of the debugfs filesystem.
690 * @value: a pointer to the variable that the file should read to and write
693 struct dentry
*debugfs_create_size_t(const char *name
, umode_t mode
,
694 struct dentry
*parent
, size_t *value
)
696 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
,
697 &fops_size_t
, &fops_size_t_ro
,
700 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
702 static int debugfs_atomic_t_set(void *data
, u64 val
)
704 atomic_set((atomic_t
*)data
, val
);
707 static int debugfs_atomic_t_get(void *data
, u64
*val
)
709 *val
= atomic_read((atomic_t
*)data
);
712 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t
, debugfs_atomic_t_get
,
713 debugfs_atomic_t_set
, "%lld\n");
714 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro
, debugfs_atomic_t_get
, NULL
,
716 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo
, NULL
, debugfs_atomic_t_set
,
720 * debugfs_create_atomic_t - create a debugfs file that is used to read and
721 * write an atomic_t value
722 * @name: a pointer to a string containing the name of the file to create.
723 * @mode: the permission that the file should have
724 * @parent: a pointer to the parent dentry for this file. This should be a
725 * directory dentry if set. If this parameter is %NULL, then the
726 * file will be created in the root of the debugfs filesystem.
727 * @value: a pointer to the variable that the file should read to and write
730 struct dentry
*debugfs_create_atomic_t(const char *name
, umode_t mode
,
731 struct dentry
*parent
, atomic_t
*value
)
733 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
,
734 &fops_atomic_t
, &fops_atomic_t_ro
,
737 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t
);
739 ssize_t
debugfs_read_file_bool(struct file
*file
, char __user
*user_buf
,
740 size_t count
, loff_t
*ppos
)
746 r
= debugfs_use_file_start(F_DENTRY(file
), &srcu_idx
);
748 val
= *(bool *)file
->private_data
;
749 debugfs_use_file_finish(srcu_idx
);
759 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
761 EXPORT_SYMBOL_GPL(debugfs_read_file_bool
);
763 ssize_t
debugfs_write_file_bool(struct file
*file
, const char __user
*user_buf
,
764 size_t count
, loff_t
*ppos
)
770 bool *val
= file
->private_data
;
772 buf_size
= min(count
, (sizeof(buf
)-1));
773 if (copy_from_user(buf
, user_buf
, buf_size
))
776 buf
[buf_size
] = '\0';
777 if (strtobool(buf
, &bv
) == 0) {
778 r
= debugfs_use_file_start(F_DENTRY(file
), &srcu_idx
);
781 debugfs_use_file_finish(srcu_idx
);
788 EXPORT_SYMBOL_GPL(debugfs_write_file_bool
);
790 static const struct file_operations fops_bool
= {
791 .read
= debugfs_read_file_bool
,
792 .write
= debugfs_write_file_bool
,
794 .llseek
= default_llseek
,
797 static const struct file_operations fops_bool_ro
= {
798 .read
= debugfs_read_file_bool
,
800 .llseek
= default_llseek
,
803 static const struct file_operations fops_bool_wo
= {
804 .write
= debugfs_write_file_bool
,
806 .llseek
= default_llseek
,
810 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
811 * @name: a pointer to a string containing the name of the file to create.
812 * @mode: the permission that the file should have
813 * @parent: a pointer to the parent dentry for this file. This should be a
814 * directory dentry if set. If this parameter is %NULL, then the
815 * file will be created in the root of the debugfs filesystem.
816 * @value: a pointer to the variable that the file should read to and write
819 * This function creates a file in debugfs with the given name that
820 * contains the value of the variable @value. If the @mode variable is so
821 * set, it can be read from, and written to.
823 * This function will return a pointer to a dentry if it succeeds. This
824 * pointer must be passed to the debugfs_remove() function when the file is
825 * to be removed (no automatic cleanup happens if your module is unloaded,
826 * you are responsible here.) If an error occurs, %NULL will be returned.
828 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
829 * returned. It is not wise to check for this value, but rather, check for
830 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
833 struct dentry
*debugfs_create_bool(const char *name
, umode_t mode
,
834 struct dentry
*parent
, bool *value
)
836 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_bool
,
837 &fops_bool_ro
, &fops_bool_wo
);
839 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
841 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
842 size_t count
, loff_t
*ppos
)
844 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
848 r
= debugfs_use_file_start(F_DENTRY(file
), &srcu_idx
);
850 r
= simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
852 debugfs_use_file_finish(srcu_idx
);
856 static const struct file_operations fops_blob
= {
857 .read
= read_file_blob
,
859 .llseek
= default_llseek
,
863 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
864 * @name: a pointer to a string containing the name of the file to create.
865 * @mode: the permission that the file should have
866 * @parent: a pointer to the parent dentry for this file. This should be a
867 * directory dentry if set. If this parameter is %NULL, then the
868 * file will be created in the root of the debugfs filesystem.
869 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
870 * to the blob data and the size of the data.
872 * This function creates a file in debugfs with the given name that exports
873 * @blob->data as a binary blob. If the @mode variable is so set it can be
874 * read from. Writing is not supported.
876 * This function will return a pointer to a dentry if it succeeds. This
877 * pointer must be passed to the debugfs_remove() function when the file is
878 * to be removed (no automatic cleanup happens if your module is unloaded,
879 * you are responsible here.) If an error occurs, %NULL will be returned.
881 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
882 * returned. It is not wise to check for this value, but rather, check for
883 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
886 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
887 struct dentry
*parent
,
888 struct debugfs_blob_wrapper
*blob
)
890 return debugfs_create_file_unsafe(name
, mode
, parent
, blob
, &fops_blob
);
892 EXPORT_SYMBOL_GPL(debugfs_create_blob
);
899 static size_t u32_format_array(char *buf
, size_t bufsize
,
900 u32
*array
, int array_size
)
904 while (--array_size
>= 0) {
906 char term
= array_size
? ' ' : '\n';
908 len
= snprintf(buf
, bufsize
, "%u%c", *array
++, term
);
917 static int u32_array_open(struct inode
*inode
, struct file
*file
)
919 struct array_data
*data
= inode
->i_private
;
920 int size
, elements
= data
->elements
;
925 * - 10 digits + ' '/'\n' = 11 bytes per number
926 * - terminating NUL character
929 buf
= kmalloc(size
+1, GFP_KERNEL
);
934 file
->private_data
= buf
;
935 u32_format_array(buf
, size
, data
->array
, data
->elements
);
937 return nonseekable_open(inode
, file
);
940 static ssize_t
u32_array_read(struct file
*file
, char __user
*buf
, size_t len
,
943 size_t size
= strlen(file
->private_data
);
945 return simple_read_from_buffer(buf
, len
, ppos
,
946 file
->private_data
, size
);
949 static int u32_array_release(struct inode
*inode
, struct file
*file
)
951 kfree(file
->private_data
);
956 static const struct file_operations u32_array_fops
= {
957 .owner
= THIS_MODULE
,
958 .open
= u32_array_open
,
959 .release
= u32_array_release
,
960 .read
= u32_array_read
,
965 * debugfs_create_u32_array - create a debugfs file that is used to read u32
967 * @name: a pointer to a string containing the name of the file to create.
968 * @mode: the permission that the file should have.
969 * @parent: a pointer to the parent dentry for this file. This should be a
970 * directory dentry if set. If this parameter is %NULL, then the
971 * file will be created in the root of the debugfs filesystem.
972 * @array: u32 array that provides data.
973 * @elements: total number of elements in the array.
975 * This function creates a file in debugfs with the given name that exports
976 * @array as data. If the @mode variable is so set it can be read from.
977 * Writing is not supported. Seek within the file is also not supported.
978 * Once array is created its size can not be changed.
980 * The function returns a pointer to dentry on success. If debugfs is not
981 * enabled in the kernel, the value -%ENODEV will be returned.
983 struct dentry
*debugfs_create_u32_array(const char *name
, umode_t mode
,
984 struct dentry
*parent
,
985 u32
*array
, u32 elements
)
987 struct array_data
*data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
993 data
->elements
= elements
;
995 return debugfs_create_file_unsafe(name
, mode
, parent
, data
,
998 EXPORT_SYMBOL_GPL(debugfs_create_u32_array
);
1000 #ifdef CONFIG_HAS_IOMEM
1003 * The regset32 stuff is used to print 32-bit registers using the
1004 * seq_file utilities. We offer printing a register set in an already-opened
1005 * sequential file or create a debugfs file that only prints a regset32.
1009 * debugfs_print_regs32 - use seq_print to describe a set of registers
1010 * @s: the seq_file structure being used to generate output
1011 * @regs: an array if struct debugfs_reg32 structures
1012 * @nregs: the length of the above array
1013 * @base: the base address to be used in reading the registers
1014 * @prefix: a string to be prefixed to every output line
1016 * This function outputs a text block describing the current values of
1017 * some 32-bit hardware registers. It is meant to be used within debugfs
1018 * files based on seq_file that need to show registers, intermixed with other
1019 * information. The prefix argument may be used to specify a leading string,
1020 * because some peripherals have several blocks of identical registers,
1021 * for example configuration of dma channels
1023 void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
1024 int nregs
, void __iomem
*base
, char *prefix
)
1028 for (i
= 0; i
< nregs
; i
++, regs
++) {
1030 seq_printf(s
, "%s", prefix
);
1031 seq_printf(s
, "%s = 0x%08x\n", regs
->name
,
1032 readl(base
+ regs
->offset
));
1033 if (seq_has_overflowed(s
))
1037 EXPORT_SYMBOL_GPL(debugfs_print_regs32
);
1039 static int debugfs_show_regset32(struct seq_file
*s
, void *data
)
1041 struct debugfs_regset32
*regset
= s
->private;
1043 debugfs_print_regs32(s
, regset
->regs
, regset
->nregs
, regset
->base
, "");
1047 static int debugfs_open_regset32(struct inode
*inode
, struct file
*file
)
1049 return single_open(file
, debugfs_show_regset32
, inode
->i_private
);
1052 static const struct file_operations fops_regset32
= {
1053 .open
= debugfs_open_regset32
,
1055 .llseek
= seq_lseek
,
1056 .release
= single_release
,
1060 * debugfs_create_regset32 - create a debugfs file that returns register values
1061 * @name: a pointer to a string containing the name of the file to create.
1062 * @mode: the permission that the file should have
1063 * @parent: a pointer to the parent dentry for this file. This should be a
1064 * directory dentry if set. If this parameter is %NULL, then the
1065 * file will be created in the root of the debugfs filesystem.
1066 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1067 * to an array of register definitions, the array size and the base
1068 * address where the register bank is to be found.
1070 * This function creates a file in debugfs with the given name that reports
1071 * the names and values of a set of 32-bit registers. If the @mode variable
1072 * is so set it can be read from. Writing is not supported.
1074 * This function will return a pointer to a dentry if it succeeds. This
1075 * pointer must be passed to the debugfs_remove() function when the file is
1076 * to be removed (no automatic cleanup happens if your module is unloaded,
1077 * you are responsible here.) If an error occurs, %NULL will be returned.
1079 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1080 * returned. It is not wise to check for this value, but rather, check for
1081 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1084 struct dentry
*debugfs_create_regset32(const char *name
, umode_t mode
,
1085 struct dentry
*parent
,
1086 struct debugfs_regset32
*regset
)
1088 return debugfs_create_file(name
, mode
, parent
, regset
, &fops_regset32
);
1090 EXPORT_SYMBOL_GPL(debugfs_create_regset32
);
1092 #endif /* CONFIG_HAS_IOMEM */
1094 struct debugfs_devm_entry
{
1095 int (*read
)(struct seq_file
*seq
, void *data
);
1099 static int debugfs_devm_entry_open(struct inode
*inode
, struct file
*f
)
1101 struct debugfs_devm_entry
*entry
= inode
->i_private
;
1103 return single_open(f
, entry
->read
, entry
->dev
);
1106 static const struct file_operations debugfs_devm_entry_ops
= {
1107 .owner
= THIS_MODULE
,
1108 .open
= debugfs_devm_entry_open
,
1109 .release
= single_release
,
1115 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1117 * @dev: device related to this debugfs file.
1118 * @name: name of the debugfs file.
1119 * @parent: a pointer to the parent dentry for this file. This should be a
1120 * directory dentry if set. If this parameter is %NULL, then the
1121 * file will be created in the root of the debugfs filesystem.
1122 * @read_fn: function pointer called to print the seq_file content.
1124 struct dentry
*debugfs_create_devm_seqfile(struct device
*dev
, const char *name
,
1125 struct dentry
*parent
,
1126 int (*read_fn
)(struct seq_file
*s
,
1129 struct debugfs_devm_entry
*entry
;
1132 return ERR_PTR(-ENOENT
);
1134 entry
= devm_kzalloc(dev
, sizeof(*entry
), GFP_KERNEL
);
1136 return ERR_PTR(-ENOMEM
);
1138 entry
->read
= read_fn
;
1141 return debugfs_create_file(name
, S_IRUGO
, parent
, entry
,
1142 &debugfs_devm_entry_ops
);
1144 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile
);