at v6.19-rc1 16 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * debugfs.h - a tiny little debug file system 4 * 5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> 6 * Copyright (C) 2004 IBM Inc. 7 * 8 * debugfs is for people to use instead of /proc or /sys. 9 * See Documentation/filesystems/ for more details. 10 */ 11 12#ifndef _DEBUGFS_H_ 13#define _DEBUGFS_H_ 14 15#include <linux/fs.h> 16#include <linux/seq_file.h> 17 18#include <linux/types.h> 19#include <linux/compiler.h> 20 21struct device; 22struct file_operations; 23 24struct debugfs_blob_wrapper { 25 void *data; 26 unsigned long size; 27}; 28 29struct debugfs_reg32 { 30 char *name; 31 unsigned long offset; 32}; 33 34struct debugfs_regset32 { 35 const struct debugfs_reg32 *regs; 36 int nregs; 37 void __iomem *base; 38 struct device *dev; /* Optional device for Runtime PM */ 39}; 40 41struct debugfs_u32_array { 42 u32 *array; 43 u32 n_elements; 44}; 45 46extern struct dentry *arch_debugfs_dir; 47 48#define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \ 49static int __fops ## _open(struct inode *inode, struct file *file) \ 50{ \ 51 __simple_attr_check_format(__fmt, 0ull); \ 52 return simple_attr_open(inode, file, __get, __set, __fmt); \ 53} \ 54static 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, \ 60} 61 62#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \ 63 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false) 64 65#define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \ 66 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true) 67 68typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *); 69 70struct debugfs_short_fops { 71 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); 72 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); 73 loff_t (*llseek) (struct file *, loff_t, int); 74}; 75 76#if defined(CONFIG_DEBUG_FS) 77 78struct dentry *debugfs_lookup(const char *name, struct dentry *parent); 79 80struct dentry *debugfs_create_file_full(const char *name, umode_t mode, 81 struct dentry *parent, void *data, 82 const void *aux, 83 const struct file_operations *fops); 84struct dentry *debugfs_create_file_short(const char *name, umode_t mode, 85 struct dentry *parent, void *data, 86 const void *aux, 87 const struct debugfs_short_fops *fops); 88 89/** 90 * debugfs_create_file - create a file in the debugfs filesystem 91 * @name: a pointer to a string containing the name of the file to create. 92 * @mode: the permission that the file should have. 93 * @parent: a pointer to the parent dentry for this file. This should be a 94 * directory dentry if set. If this parameter is NULL, then the 95 * file will be created in the root of the debugfs filesystem. 96 * @data: a pointer to something that the caller will want to get to later 97 * on. The inode.i_private pointer will point to this value on 98 * the open() call. 99 * @fops: a pointer to a struct file_operations or struct debugfs_short_fops that 100 * should be used for this file. 101 * 102 * This is the basic "create a file" function for debugfs. It allows for a 103 * wide range of flexibility in creating a file, or a directory (if you want 104 * to create a directory, the debugfs_create_dir() function is 105 * recommended to be used instead.) 106 * 107 * This function will return a pointer to a dentry if it succeeds. This 108 * pointer must be passed to the debugfs_remove() function when the file is 109 * to be removed (no automatic cleanup happens if your module is unloaded, 110 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 111 * returned. 112 * 113 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 114 * returned. 115 * 116 * If fops points to a struct debugfs_short_fops, then simple_open() will be 117 * used for the open, and only read/write/llseek are supported and are proxied, 118 * so no module reference or release are needed. 119 * 120 * NOTE: it's expected that most callers should _ignore_ the errors returned 121 * by this function. Other debugfs functions handle the fact that the "dentry" 122 * passed to them could be an error and they don't crash in that case. 123 * Drivers should generally work fine even if debugfs fails to init anyway. 124 */ 125#define debugfs_create_file(name, mode, parent, data, fops) \ 126 _Generic(fops, \ 127 const struct file_operations *: debugfs_create_file_full, \ 128 const struct debugfs_short_fops *: debugfs_create_file_short, \ 129 struct file_operations *: debugfs_create_file_full, \ 130 struct debugfs_short_fops *: debugfs_create_file_short) \ 131 (name, mode, parent, data, NULL, fops) 132 133#define debugfs_create_file_aux(name, mode, parent, data, aux, fops) \ 134 _Generic(fops, \ 135 const struct file_operations *: debugfs_create_file_full, \ 136 const struct debugfs_short_fops *: debugfs_create_file_short, \ 137 struct file_operations *: debugfs_create_file_full, \ 138 struct debugfs_short_fops *: debugfs_create_file_short) \ 139 (name, mode, parent, data, aux, fops) 140 141struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 142 struct dentry *parent, void *data, 143 const struct file_operations *fops); 144 145void debugfs_create_file_size(const char *name, umode_t mode, 146 struct dentry *parent, void *data, 147 const struct file_operations *fops, 148 loff_t file_size); 149 150struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); 151 152struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 153 const char *dest); 154 155struct dentry *debugfs_create_automount(const char *name, 156 struct dentry *parent, 157 debugfs_automount_t f, 158 void *data); 159 160void debugfs_remove(struct dentry *dentry); 161#define debugfs_remove_recursive debugfs_remove 162 163void debugfs_lookup_and_remove(const char *name, struct dentry *parent); 164 165void *debugfs_get_aux(const struct file *file); 166 167int debugfs_file_get(struct dentry *dentry); 168void debugfs_file_put(struct dentry *dentry); 169 170ssize_t debugfs_attr_read(struct file *file, char __user *buf, 171 size_t len, loff_t *ppos); 172ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 173 size_t len, loff_t *ppos); 174ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 175 size_t len, loff_t *ppos); 176 177int debugfs_change_name(struct dentry *dentry, const char *fmt, ...) __printf(2, 3); 178 179void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 180 u8 *value); 181void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 182 u16 *value); 183void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 184 u32 *value); 185void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 186 u64 *value); 187void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 188 unsigned long *value); 189void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 190 u8 *value); 191void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 192 u16 *value); 193void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 194 u32 *value); 195void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 196 u64 *value); 197void debugfs_create_size_t(const char *name, umode_t mode, 198 struct dentry *parent, size_t *value); 199void debugfs_create_atomic_t(const char *name, umode_t mode, 200 struct dentry *parent, atomic_t *value); 201void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 202 bool *value); 203void debugfs_create_str(const char *name, umode_t mode, 204 struct dentry *parent, char **value); 205 206struct dentry *debugfs_create_blob(const char *name, umode_t mode, 207 struct dentry *parent, 208 struct debugfs_blob_wrapper *blob); 209 210void debugfs_create_regset32(const char *name, umode_t mode, 211 struct dentry *parent, 212 struct debugfs_regset32 *regset); 213 214void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 215 int nregs, void __iomem *base, char *prefix); 216 217void debugfs_create_u32_array(const char *name, umode_t mode, 218 struct dentry *parent, 219 struct debugfs_u32_array *array); 220 221void debugfs_create_devm_seqfile(struct device *dev, const char *name, 222 struct dentry *parent, 223 int (*read_fn)(struct seq_file *s, void *data)); 224 225bool debugfs_initialized(void); 226 227ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 228 size_t count, loff_t *ppos); 229 230ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 231 size_t count, loff_t *ppos); 232 233ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 234 size_t count, loff_t *ppos); 235 236/** 237 * struct debugfs_cancellation - cancellation data 238 * @list: internal, for keeping track 239 * @cancel: callback to call 240 * @cancel_data: extra data for the callback to call 241 */ 242struct debugfs_cancellation { 243 struct list_head list; 244 void (*cancel)(struct dentry *, void *); 245 void *cancel_data; 246}; 247 248void __acquires(cancellation) 249debugfs_enter_cancellation(struct file *file, 250 struct debugfs_cancellation *cancellation); 251void __releases(cancellation) 252debugfs_leave_cancellation(struct file *file, 253 struct debugfs_cancellation *cancellation); 254 255#else 256 257#include <linux/err.h> 258 259/* 260 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled 261 * so users have a chance to detect if there was a real error or not. We don't 262 * want to duplicate the design decision mistakes of procfs and devfs again. 263 */ 264 265static inline struct dentry *debugfs_lookup(const char *name, 266 struct dentry *parent) 267{ 268 return ERR_PTR(-ENODEV); 269} 270 271static inline struct dentry *debugfs_create_file_aux(const char *name, 272 umode_t mode, struct dentry *parent, 273 void *data, void *aux, 274 const void *fops) 275{ 276 return ERR_PTR(-ENODEV); 277} 278 279static inline struct dentry *debugfs_create_file(const char *name, umode_t mode, 280 struct dentry *parent, void *data, 281 const void *fops) 282{ 283 return ERR_PTR(-ENODEV); 284} 285 286static inline struct dentry *debugfs_create_file_unsafe(const char *name, 287 umode_t mode, struct dentry *parent, 288 void *data, 289 const struct file_operations *fops) 290{ 291 return ERR_PTR(-ENODEV); 292} 293 294static inline void debugfs_create_file_size(const char *name, umode_t mode, 295 struct dentry *parent, void *data, 296 const struct file_operations *fops, 297 loff_t file_size) 298{ } 299 300static inline struct dentry *debugfs_create_dir(const char *name, 301 struct dentry *parent) 302{ 303 return ERR_PTR(-ENODEV); 304} 305 306static inline struct dentry *debugfs_create_symlink(const char *name, 307 struct dentry *parent, 308 const char *dest) 309{ 310 return ERR_PTR(-ENODEV); 311} 312 313static inline struct dentry *debugfs_create_automount(const char *name, 314 struct dentry *parent, 315 debugfs_automount_t f, 316 void *data) 317{ 318 return ERR_PTR(-ENODEV); 319} 320 321static inline void debugfs_remove(struct dentry *dentry) 322{ } 323 324static inline void debugfs_remove_recursive(struct dentry *dentry) 325{ } 326 327static inline void debugfs_lookup_and_remove(const char *name, 328 struct dentry *parent) 329{ } 330 331void *debugfs_get_aux(const struct file *file); 332 333static inline int debugfs_file_get(struct dentry *dentry) 334{ 335 return 0; 336} 337 338static inline void debugfs_file_put(struct dentry *dentry) 339{ } 340 341static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf, 342 size_t len, loff_t *ppos) 343{ 344 return -ENODEV; 345} 346 347static inline ssize_t debugfs_attr_write(struct file *file, 348 const char __user *buf, 349 size_t len, loff_t *ppos) 350{ 351 return -ENODEV; 352} 353 354static inline ssize_t debugfs_attr_write_signed(struct file *file, 355 const char __user *buf, 356 size_t len, loff_t *ppos) 357{ 358 return -ENODEV; 359} 360 361static inline int __printf(2, 3) debugfs_change_name(struct dentry *dentry, 362 const char *fmt, ...) 363{ 364 return -ENODEV; 365} 366 367static inline void debugfs_create_u8(const char *name, umode_t mode, 368 struct dentry *parent, u8 *value) { } 369 370static inline void debugfs_create_u16(const char *name, umode_t mode, 371 struct dentry *parent, u16 *value) { } 372 373static inline void debugfs_create_u32(const char *name, umode_t mode, 374 struct dentry *parent, u32 *value) { } 375 376static inline void debugfs_create_u64(const char *name, umode_t mode, 377 struct dentry *parent, u64 *value) { } 378 379static inline void debugfs_create_ulong(const char *name, umode_t mode, 380 struct dentry *parent, 381 unsigned long *value) { } 382 383static inline void debugfs_create_x8(const char *name, umode_t mode, 384 struct dentry *parent, u8 *value) { } 385 386static inline void debugfs_create_x16(const char *name, umode_t mode, 387 struct dentry *parent, u16 *value) { } 388 389static inline void debugfs_create_x32(const char *name, umode_t mode, 390 struct dentry *parent, u32 *value) { } 391 392static inline void debugfs_create_x64(const char *name, umode_t mode, 393 struct dentry *parent, u64 *value) { } 394 395static inline void debugfs_create_size_t(const char *name, umode_t mode, 396 struct dentry *parent, size_t *value) 397{ } 398 399static inline void debugfs_create_atomic_t(const char *name, umode_t mode, 400 struct dentry *parent, 401 atomic_t *value) 402{ } 403 404static inline void debugfs_create_bool(const char *name, umode_t mode, 405 struct dentry *parent, bool *value) { } 406 407static inline void debugfs_create_str(const char *name, umode_t mode, 408 struct dentry *parent, 409 char **value) 410{ } 411 412static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode, 413 struct dentry *parent, 414 struct debugfs_blob_wrapper *blob) 415{ 416 return ERR_PTR(-ENODEV); 417} 418 419static inline void debugfs_create_regset32(const char *name, umode_t mode, 420 struct dentry *parent, 421 struct debugfs_regset32 *regset) 422{ 423} 424 425static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 426 int nregs, void __iomem *base, char *prefix) 427{ 428} 429 430static inline bool debugfs_initialized(void) 431{ 432 return false; 433} 434 435static inline void debugfs_create_u32_array(const char *name, umode_t mode, 436 struct dentry *parent, 437 struct debugfs_u32_array *array) 438{ 439} 440 441static inline void debugfs_create_devm_seqfile(struct device *dev, 442 const char *name, 443 struct dentry *parent, 444 int (*read_fn)(struct seq_file *s, 445 void *data)) 446{ 447} 448 449static inline ssize_t debugfs_read_file_bool(struct file *file, 450 char __user *user_buf, 451 size_t count, loff_t *ppos) 452{ 453 return -ENODEV; 454} 455 456static inline ssize_t debugfs_write_file_bool(struct file *file, 457 const char __user *user_buf, 458 size_t count, loff_t *ppos) 459{ 460 return -ENODEV; 461} 462 463static inline ssize_t debugfs_read_file_str(struct file *file, 464 char __user *user_buf, 465 size_t count, loff_t *ppos) 466{ 467 return -ENODEV; 468} 469 470#endif 471 472#define debugfs_create_file_aux_num(name, mode, parent, data, n, fops) \ 473 debugfs_create_file_aux(name, mode, parent, data, \ 474 (void *)(unsigned long)n, fops) 475#define debugfs_get_aux_num(f) (unsigned long)debugfs_get_aux(f) 476 477/** 478 * debugfs_create_xul - create a debugfs file that is used to read and write an 479 * unsigned long value, formatted in hexadecimal 480 * @name: a pointer to a string containing the name of the file to create. 481 * @mode: the permission that the file should have 482 * @parent: a pointer to the parent dentry for this file. This should be a 483 * directory dentry if set. If this parameter is %NULL, then the 484 * file will be created in the root of the debugfs filesystem. 485 * @value: a pointer to the variable that the file should read to and write 486 * from. 487 */ 488static inline void debugfs_create_xul(const char *name, umode_t mode, 489 struct dentry *parent, 490 unsigned long *value) 491{ 492 if (sizeof(*value) == sizeof(u32)) 493 debugfs_create_x32(name, mode, parent, (u32 *)value); 494 else 495 debugfs_create_x64(name, mode, parent, (u64 *)value); 496} 497 498#endif