1 // SPDX-License-Identifier: GPL-2.0
3 * debugfs.h - a tiny little debug file system
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
8 * debugfs is for people to use instead of /proc or /sys.
9 * See Documentation/filesystems/ for more details.
16 #include <linux/seq_file.h>
18 #include <linux/types.h>
19 #include <linux/compiler.h>
22 struct file_operations
;
24 struct debugfs_blob_wrapper
{
29 struct debugfs_reg32
{
34 struct debugfs_regset32
{
35 const struct debugfs_reg32
*regs
;
38 struct device
*dev
; /* Optional device for Runtime PM */
41 struct debugfs_u32_array
{
46 extern struct dentry
*arch_debugfs_dir
;
48 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
49 static int __fops ## _open(struct inode *inode, struct file *file) \
51 __simple_attr_check_format(__fmt, 0ull); \
52 return simple_attr_open(inode, file, __get, __set, __fmt); \
54 static const struct file_operations __fops = { \
55 .owner = THIS_MODULE, \
56 .open = __fops ## _open, \
57 .release = simple_attr_release, \
58 .read = debugfs_attr_read, \
59 .write = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write, \
62 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
63 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
65 #define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
66 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
68 typedef struct vfsmount
*(*debugfs_automount_t
)(struct dentry
*, void *);
70 #if defined(CONFIG_DEBUG_FS)
72 struct dentry
*debugfs_lookup(const char *name
, struct dentry
*parent
);
74 struct debugfs_short_fops
{
75 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
76 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
77 loff_t (*llseek
) (struct file
*, loff_t
, int);
80 struct dentry
*debugfs_create_file_full(const char *name
, umode_t mode
,
81 struct dentry
*parent
, void *data
,
82 const struct file_operations
*fops
);
83 struct dentry
*debugfs_create_file_short(const char *name
, umode_t mode
,
84 struct dentry
*parent
, void *data
,
85 const struct debugfs_short_fops
*fops
);
88 * debugfs_create_file - create a file in the debugfs filesystem
89 * @name: a pointer to a string containing the name of the file to create.
90 * @mode: the permission that the file should have.
91 * @parent: a pointer to the parent dentry for this file. This should be a
92 * directory dentry if set. If this parameter is NULL, then the
93 * file will be created in the root of the debugfs filesystem.
94 * @data: a pointer to something that the caller will want to get to later
95 * on. The inode.i_private pointer will point to this value on
97 * @fops: a pointer to a struct file_operations or struct debugfs_short_fops that
98 * should be used for this file.
100 * This is the basic "create a file" function for debugfs. It allows for a
101 * wide range of flexibility in creating a file, or a directory (if you want
102 * to create a directory, the debugfs_create_dir() function is
103 * recommended to be used instead.)
105 * This function will return a pointer to a dentry if it succeeds. This
106 * pointer must be passed to the debugfs_remove() function when the file is
107 * to be removed (no automatic cleanup happens if your module is unloaded,
108 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
111 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
114 * If fops points to a struct debugfs_short_fops, then simple_open() will be
115 * used for the open, and only read/write/llseek are supported and are proxied,
116 * so no module reference or release are needed.
118 * NOTE: it's expected that most callers should _ignore_ the errors returned
119 * by this function. Other debugfs functions handle the fact that the "dentry"
120 * passed to them could be an error and they don't crash in that case.
121 * Drivers should generally work fine even if debugfs fails to init anyway.
123 #define debugfs_create_file(name, mode, parent, data, fops) \
125 const struct file_operations *: debugfs_create_file_full, \
126 const struct debugfs_short_fops *: debugfs_create_file_short, \
127 struct file_operations *: debugfs_create_file_full, \
128 struct debugfs_short_fops *: debugfs_create_file_short) \
129 (name, mode, parent, data, fops)
131 struct dentry
*debugfs_create_file_unsafe(const char *name
, umode_t mode
,
132 struct dentry
*parent
, void *data
,
133 const struct file_operations
*fops
);
135 void debugfs_create_file_size(const char *name
, umode_t mode
,
136 struct dentry
*parent
, void *data
,
137 const struct file_operations
*fops
,
140 struct dentry
*debugfs_create_dir(const char *name
, struct dentry
*parent
);
142 struct dentry
*debugfs_create_symlink(const char *name
, struct dentry
*parent
,
145 struct dentry
*debugfs_create_automount(const char *name
,
146 struct dentry
*parent
,
147 debugfs_automount_t f
,
150 void debugfs_remove(struct dentry
*dentry
);
151 #define debugfs_remove_recursive debugfs_remove
153 void debugfs_lookup_and_remove(const char *name
, struct dentry
*parent
);
155 const struct file_operations
*debugfs_real_fops(const struct file
*filp
);
157 int debugfs_file_get(struct dentry
*dentry
);
158 void debugfs_file_put(struct dentry
*dentry
);
160 ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
161 size_t len
, loff_t
*ppos
);
162 ssize_t
debugfs_attr_write(struct file
*file
, const char __user
*buf
,
163 size_t len
, loff_t
*ppos
);
164 ssize_t
debugfs_attr_write_signed(struct file
*file
, const char __user
*buf
,
165 size_t len
, loff_t
*ppos
);
167 struct dentry
*debugfs_rename(struct dentry
*old_dir
, struct dentry
*old_dentry
,
168 struct dentry
*new_dir
, const char *new_name
);
170 void debugfs_create_u8(const char *name
, umode_t mode
, struct dentry
*parent
,
172 void debugfs_create_u16(const char *name
, umode_t mode
, struct dentry
*parent
,
174 void debugfs_create_u32(const char *name
, umode_t mode
, struct dentry
*parent
,
176 void debugfs_create_u64(const char *name
, umode_t mode
, struct dentry
*parent
,
178 void debugfs_create_ulong(const char *name
, umode_t mode
, struct dentry
*parent
,
179 unsigned long *value
);
180 void debugfs_create_x8(const char *name
, umode_t mode
, struct dentry
*parent
,
182 void debugfs_create_x16(const char *name
, umode_t mode
, struct dentry
*parent
,
184 void debugfs_create_x32(const char *name
, umode_t mode
, struct dentry
*parent
,
186 void debugfs_create_x64(const char *name
, umode_t mode
, struct dentry
*parent
,
188 void debugfs_create_size_t(const char *name
, umode_t mode
,
189 struct dentry
*parent
, size_t *value
);
190 void debugfs_create_atomic_t(const char *name
, umode_t mode
,
191 struct dentry
*parent
, atomic_t
*value
);
192 void debugfs_create_bool(const char *name
, umode_t mode
, struct dentry
*parent
,
194 void debugfs_create_str(const char *name
, umode_t mode
,
195 struct dentry
*parent
, char **value
);
197 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
198 struct dentry
*parent
,
199 struct debugfs_blob_wrapper
*blob
);
201 void debugfs_create_regset32(const char *name
, umode_t mode
,
202 struct dentry
*parent
,
203 struct debugfs_regset32
*regset
);
205 void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
206 int nregs
, void __iomem
*base
, char *prefix
);
208 void debugfs_create_u32_array(const char *name
, umode_t mode
,
209 struct dentry
*parent
,
210 struct debugfs_u32_array
*array
);
212 void debugfs_create_devm_seqfile(struct device
*dev
, const char *name
,
213 struct dentry
*parent
,
214 int (*read_fn
)(struct seq_file
*s
, void *data
));
216 bool debugfs_initialized(void);
218 ssize_t
debugfs_read_file_bool(struct file
*file
, char __user
*user_buf
,
219 size_t count
, loff_t
*ppos
);
221 ssize_t
debugfs_write_file_bool(struct file
*file
, const char __user
*user_buf
,
222 size_t count
, loff_t
*ppos
);
224 ssize_t
debugfs_read_file_str(struct file
*file
, char __user
*user_buf
,
225 size_t count
, loff_t
*ppos
);
228 * struct debugfs_cancellation - cancellation data
229 * @list: internal, for keeping track
230 * @cancel: callback to call
231 * @cancel_data: extra data for the callback to call
233 struct debugfs_cancellation
{
234 struct list_head list
;
235 void (*cancel
)(struct dentry
*, void *);
239 void __acquires(cancellation
)
240 debugfs_enter_cancellation(struct file
*file
,
241 struct debugfs_cancellation
*cancellation
);
242 void __releases(cancellation
)
243 debugfs_leave_cancellation(struct file
*file
,
244 struct debugfs_cancellation
*cancellation
);
248 #include <linux/err.h>
251 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
252 * so users have a chance to detect if there was a real error or not. We don't
253 * want to duplicate the design decision mistakes of procfs and devfs again.
256 static inline struct dentry
*debugfs_lookup(const char *name
,
257 struct dentry
*parent
)
259 return ERR_PTR(-ENODEV
);
262 static inline struct dentry
*debugfs_create_file(const char *name
, umode_t mode
,
263 struct dentry
*parent
, void *data
,
266 return ERR_PTR(-ENODEV
);
269 static inline struct dentry
*debugfs_create_file_unsafe(const char *name
,
270 umode_t mode
, struct dentry
*parent
,
272 const struct file_operations
*fops
)
274 return ERR_PTR(-ENODEV
);
277 static inline void debugfs_create_file_size(const char *name
, umode_t mode
,
278 struct dentry
*parent
, void *data
,
279 const struct file_operations
*fops
,
283 static inline struct dentry
*debugfs_create_dir(const char *name
,
284 struct dentry
*parent
)
286 return ERR_PTR(-ENODEV
);
289 static inline struct dentry
*debugfs_create_symlink(const char *name
,
290 struct dentry
*parent
,
293 return ERR_PTR(-ENODEV
);
296 static inline struct dentry
*debugfs_create_automount(const char *name
,
297 struct dentry
*parent
,
298 debugfs_automount_t f
,
301 return ERR_PTR(-ENODEV
);
304 static inline void debugfs_remove(struct dentry
*dentry
)
307 static inline void debugfs_remove_recursive(struct dentry
*dentry
)
310 static inline void debugfs_lookup_and_remove(const char *name
,
311 struct dentry
*parent
)
314 const struct file_operations
*debugfs_real_fops(const struct file
*filp
);
316 static inline int debugfs_file_get(struct dentry
*dentry
)
321 static inline void debugfs_file_put(struct dentry
*dentry
)
324 static inline ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
325 size_t len
, loff_t
*ppos
)
330 static inline ssize_t
debugfs_attr_write(struct file
*file
,
331 const char __user
*buf
,
332 size_t len
, loff_t
*ppos
)
337 static inline ssize_t
debugfs_attr_write_signed(struct file
*file
,
338 const char __user
*buf
,
339 size_t len
, loff_t
*ppos
)
344 static inline struct dentry
*debugfs_rename(struct dentry
*old_dir
, struct dentry
*old_dentry
,
345 struct dentry
*new_dir
, char *new_name
)
347 return ERR_PTR(-ENODEV
);
350 static inline void debugfs_create_u8(const char *name
, umode_t mode
,
351 struct dentry
*parent
, u8
*value
) { }
353 static inline void debugfs_create_u16(const char *name
, umode_t mode
,
354 struct dentry
*parent
, u16
*value
) { }
356 static inline void debugfs_create_u32(const char *name
, umode_t mode
,
357 struct dentry
*parent
, u32
*value
) { }
359 static inline void debugfs_create_u64(const char *name
, umode_t mode
,
360 struct dentry
*parent
, u64
*value
) { }
362 static inline void debugfs_create_ulong(const char *name
, umode_t mode
,
363 struct dentry
*parent
,
364 unsigned long *value
) { }
366 static inline void debugfs_create_x8(const char *name
, umode_t mode
,
367 struct dentry
*parent
, u8
*value
) { }
369 static inline void debugfs_create_x16(const char *name
, umode_t mode
,
370 struct dentry
*parent
, u16
*value
) { }
372 static inline void debugfs_create_x32(const char *name
, umode_t mode
,
373 struct dentry
*parent
, u32
*value
) { }
375 static inline void debugfs_create_x64(const char *name
, umode_t mode
,
376 struct dentry
*parent
, u64
*value
) { }
378 static inline void debugfs_create_size_t(const char *name
, umode_t mode
,
379 struct dentry
*parent
, size_t *value
)
382 static inline void debugfs_create_atomic_t(const char *name
, umode_t mode
,
383 struct dentry
*parent
,
387 static inline void debugfs_create_bool(const char *name
, umode_t mode
,
388 struct dentry
*parent
, bool *value
) { }
390 static inline void debugfs_create_str(const char *name
, umode_t mode
,
391 struct dentry
*parent
,
395 static inline struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
396 struct dentry
*parent
,
397 struct debugfs_blob_wrapper
*blob
)
399 return ERR_PTR(-ENODEV
);
402 static inline void debugfs_create_regset32(const char *name
, umode_t mode
,
403 struct dentry
*parent
,
404 struct debugfs_regset32
*regset
)
408 static inline void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
409 int nregs
, void __iomem
*base
, char *prefix
)
413 static inline bool debugfs_initialized(void)
418 static inline void debugfs_create_u32_array(const char *name
, umode_t mode
,
419 struct dentry
*parent
,
420 struct debugfs_u32_array
*array
)
424 static inline void debugfs_create_devm_seqfile(struct device
*dev
,
426 struct dentry
*parent
,
427 int (*read_fn
)(struct seq_file
*s
,
432 static inline ssize_t
debugfs_read_file_bool(struct file
*file
,
433 char __user
*user_buf
,
434 size_t count
, loff_t
*ppos
)
439 static inline ssize_t
debugfs_write_file_bool(struct file
*file
,
440 const char __user
*user_buf
,
441 size_t count
, loff_t
*ppos
)
446 static inline ssize_t
debugfs_read_file_str(struct file
*file
,
447 char __user
*user_buf
,
448 size_t count
, loff_t
*ppos
)
456 * debugfs_create_xul - create a debugfs file that is used to read and write an
457 * unsigned long value, formatted in hexadecimal
458 * @name: a pointer to a string containing the name of the file to create.
459 * @mode: the permission that the file should have
460 * @parent: a pointer to the parent dentry for this file. This should be a
461 * directory dentry if set. If this parameter is %NULL, then the
462 * file will be created in the root of the debugfs filesystem.
463 * @value: a pointer to the variable that the file should read to and write
466 static inline void debugfs_create_xul(const char *name
, umode_t mode
,
467 struct dentry
*parent
,
468 unsigned long *value
)
470 if (sizeof(*value
) == sizeof(u32
))
471 debugfs_create_x32(name
, mode
, parent
, (u32
*)value
);
473 debugfs_create_x64(name
, mode
, parent
, (u64
*)value
);