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 <[email protected]> 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 21 struct device; 22 struct file_operations; 23 24 struct debugfs_blob_wrapper { 25 void *data; 26 unsigned long size; 27 }; 28 29 struct debugfs_reg32 { 30 char *name; 31 unsigned long offset; 32 }; 33 34 struct 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 41 struct debugfs_u32_array { 42 u32 *array; 43 u32 n_elements; 44 }; 45 46 extern struct dentry *arch_debugfs_dir; 47 48 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \ 49 static 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 } \ 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, \ 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 68 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *); 69 70 struct 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 78 struct dentry *debugfs_lookup(const char *name, struct dentry *parent); 79 80 struct 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); 84 struct 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 141 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode, 142 struct dentry *parent, void *data, 143 const struct file_operations *fops); 144 145 void 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 150 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent); 151 152 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, 153 const char *dest); 154 155 struct dentry *debugfs_create_automount(const char *name, 156 struct dentry *parent, 157 debugfs_automount_t f, 158 void *data); 159 160 void debugfs_remove(struct dentry *dentry); 161 #define debugfs_remove_recursive debugfs_remove 162 163 void debugfs_lookup_and_remove(const char *name, struct dentry *parent); 164 165 const struct file_operations *debugfs_real_fops(const struct file *filp); 166 const void *debugfs_get_aux(const struct file *file); 167 168 int debugfs_file_get(struct dentry *dentry); 169 void debugfs_file_put(struct dentry *dentry); 170 171 ssize_t debugfs_attr_read(struct file *file, char __user *buf, 172 size_t len, loff_t *ppos); 173 ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 174 size_t len, loff_t *ppos); 175 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 176 size_t len, loff_t *ppos); 177 178 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 179 struct dentry *new_dir, const char *new_name); 180 181 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 182 u8 *value); 183 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 184 u16 *value); 185 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 186 u32 *value); 187 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 188 u64 *value); 189 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 190 unsigned long *value); 191 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 192 u8 *value); 193 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 194 u16 *value); 195 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 196 u32 *value); 197 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 198 u64 *value); 199 void debugfs_create_size_t(const char *name, umode_t mode, 200 struct dentry *parent, size_t *value); 201 void debugfs_create_atomic_t(const char *name, umode_t mode, 202 struct dentry *parent, atomic_t *value); 203 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 204 bool *value); 205 void debugfs_create_str(const char *name, umode_t mode, 206 struct dentry *parent, char **value); 207 208 struct dentry *debugfs_create_blob(const char *name, umode_t mode, 209 struct dentry *parent, 210 struct debugfs_blob_wrapper *blob); 211 212 void debugfs_create_regset32(const char *name, umode_t mode, 213 struct dentry *parent, 214 struct debugfs_regset32 *regset); 215 216 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 217 int nregs, void __iomem *base, char *prefix); 218 219 void debugfs_create_u32_array(const char *name, umode_t mode, 220 struct dentry *parent, 221 struct debugfs_u32_array *array); 222 223 void debugfs_create_devm_seqfile(struct device *dev, const char *name, 224 struct dentry *parent, 225 int (*read_fn)(struct seq_file *s, void *data)); 226 227 bool debugfs_initialized(void); 228 229 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 230 size_t count, loff_t *ppos); 231 232 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 233 size_t count, loff_t *ppos); 234 235 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 236 size_t count, loff_t *ppos); 237 238 /** 239 * struct debugfs_cancellation - cancellation data 240 * @list: internal, for keeping track 241 * @cancel: callback to call 242 * @cancel_data: extra data for the callback to call 243 */ 244 struct debugfs_cancellation { 245 struct list_head list; 246 void (*cancel)(struct dentry *, void *); 247 void *cancel_data; 248 }; 249 250 void __acquires(cancellation) 251 debugfs_enter_cancellation(struct file *file, 252 struct debugfs_cancellation *cancellation); 253 void __releases(cancellation) 254 debugfs_leave_cancellation(struct file *file, 255 struct debugfs_cancellation *cancellation); 256 257 #else 258 259 #include <linux/err.h> 260 261 /* 262 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled 263 * so users have a chance to detect if there was a real error or not. We don't 264 * want to duplicate the design decision mistakes of procfs and devfs again. 265 */ 266 267 static inline struct dentry *debugfs_lookup(const char *name, 268 struct dentry *parent) 269 { 270 return ERR_PTR(-ENODEV); 271 } 272 273 static inline struct dentry *debugfs_create_file_aux(const char *name, 274 umode_t mode, struct dentry *parent, 275 void *data, void *aux, 276 const void *fops) 277 { 278 return ERR_PTR(-ENODEV); 279 } 280 281 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode, 282 struct dentry *parent, void *data, 283 const void *fops) 284 { 285 return ERR_PTR(-ENODEV); 286 } 287 288 static inline struct dentry *debugfs_create_file_unsafe(const char *name, 289 umode_t mode, struct dentry *parent, 290 void *data, 291 const struct file_operations *fops) 292 { 293 return ERR_PTR(-ENODEV); 294 } 295 296 static inline void debugfs_create_file_size(const char *name, umode_t mode, 297 struct dentry *parent, void *data, 298 const struct file_operations *fops, 299 loff_t file_size) 300 { } 301 302 static inline struct dentry *debugfs_create_dir(const char *name, 303 struct dentry *parent) 304 { 305 return ERR_PTR(-ENODEV); 306 } 307 308 static inline struct dentry *debugfs_create_symlink(const char *name, 309 struct dentry *parent, 310 const char *dest) 311 { 312 return ERR_PTR(-ENODEV); 313 } 314 315 static inline struct dentry *debugfs_create_automount(const char *name, 316 struct dentry *parent, 317 debugfs_automount_t f, 318 void *data) 319 { 320 return ERR_PTR(-ENODEV); 321 } 322 323 static inline void debugfs_remove(struct dentry *dentry) 324 { } 325 326 static inline void debugfs_remove_recursive(struct dentry *dentry) 327 { } 328 329 static inline void debugfs_lookup_and_remove(const char *name, 330 struct dentry *parent) 331 { } 332 333 const struct file_operations *debugfs_real_fops(const struct file *filp); 334 void *debugfs_get_aux(const struct file *file); 335 336 static inline int debugfs_file_get(struct dentry *dentry) 337 { 338 return 0; 339 } 340 341 static inline void debugfs_file_put(struct dentry *dentry) 342 { } 343 344 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf, 345 size_t len, loff_t *ppos) 346 { 347 return -ENODEV; 348 } 349 350 static inline ssize_t debugfs_attr_write(struct file *file, 351 const char __user *buf, 352 size_t len, loff_t *ppos) 353 { 354 return -ENODEV; 355 } 356 357 static inline ssize_t debugfs_attr_write_signed(struct file *file, 358 const char __user *buf, 359 size_t len, loff_t *ppos) 360 { 361 return -ENODEV; 362 } 363 364 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, 365 struct dentry *new_dir, char *new_name) 366 { 367 return ERR_PTR(-ENODEV); 368 } 369 370 static inline void debugfs_create_u8(const char *name, umode_t mode, 371 struct dentry *parent, u8 *value) { } 372 373 static inline void debugfs_create_u16(const char *name, umode_t mode, 374 struct dentry *parent, u16 *value) { } 375 376 static inline void debugfs_create_u32(const char *name, umode_t mode, 377 struct dentry *parent, u32 *value) { } 378 379 static inline void debugfs_create_u64(const char *name, umode_t mode, 380 struct dentry *parent, u64 *value) { } 381 382 static inline void debugfs_create_ulong(const char *name, umode_t mode, 383 struct dentry *parent, 384 unsigned long *value) { } 385 386 static inline void debugfs_create_x8(const char *name, umode_t mode, 387 struct dentry *parent, u8 *value) { } 388 389 static inline void debugfs_create_x16(const char *name, umode_t mode, 390 struct dentry *parent, u16 *value) { } 391 392 static inline void debugfs_create_x32(const char *name, umode_t mode, 393 struct dentry *parent, u32 *value) { } 394 395 static inline void debugfs_create_x64(const char *name, umode_t mode, 396 struct dentry *parent, u64 *value) { } 397 398 static inline void debugfs_create_size_t(const char *name, umode_t mode, 399 struct dentry *parent, size_t *value) 400 { } 401 402 static inline void debugfs_create_atomic_t(const char *name, umode_t mode, 403 struct dentry *parent, 404 atomic_t *value) 405 { } 406 407 static inline void debugfs_create_bool(const char *name, umode_t mode, 408 struct dentry *parent, bool *value) { } 409 410 static inline void debugfs_create_str(const char *name, umode_t mode, 411 struct dentry *parent, 412 char **value) 413 { } 414 415 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode, 416 struct dentry *parent, 417 struct debugfs_blob_wrapper *blob) 418 { 419 return ERR_PTR(-ENODEV); 420 } 421 422 static inline void debugfs_create_regset32(const char *name, umode_t mode, 423 struct dentry *parent, 424 struct debugfs_regset32 *regset) 425 { 426 } 427 428 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 429 int nregs, void __iomem *base, char *prefix) 430 { 431 } 432 433 static inline bool debugfs_initialized(void) 434 { 435 return false; 436 } 437 438 static inline void debugfs_create_u32_array(const char *name, umode_t mode, 439 struct dentry *parent, 440 struct debugfs_u32_array *array) 441 { 442 } 443 444 static inline void debugfs_create_devm_seqfile(struct device *dev, 445 const char *name, 446 struct dentry *parent, 447 int (*read_fn)(struct seq_file *s, 448 void *data)) 449 { 450 } 451 452 static inline ssize_t debugfs_read_file_bool(struct file *file, 453 char __user *user_buf, 454 size_t count, loff_t *ppos) 455 { 456 return -ENODEV; 457 } 458 459 static inline ssize_t debugfs_write_file_bool(struct file *file, 460 const char __user *user_buf, 461 size_t count, loff_t *ppos) 462 { 463 return -ENODEV; 464 } 465 466 static inline ssize_t debugfs_read_file_str(struct file *file, 467 char __user *user_buf, 468 size_t count, loff_t *ppos) 469 { 470 return -ENODEV; 471 } 472 473 #endif 474 475 #define debugfs_create_file_aux_num(name, mode, parent, data, n, fops) \ 476 debugfs_create_file_aux(name, mode, parent, data, \ 477 (void *)(unsigned long)n, fops) 478 #define debugfs_get_aux_num(f) (unsigned long)debugfs_get_aux(f) 479 480 /** 481 * debugfs_create_xul - create a debugfs file that is used to read and write an 482 * unsigned long value, formatted in hexadecimal 483 * @name: a pointer to a string containing the name of the file to create. 484 * @mode: the permission that the file should have 485 * @parent: a pointer to the parent dentry for this file. This should be a 486 * directory dentry if set. If this parameter is %NULL, then the 487 * file will be created in the root of the debugfs filesystem. 488 * @value: a pointer to the variable that the file should read to and write 489 * from. 490 */ 491 static inline void debugfs_create_xul(const char *name, umode_t mode, 492 struct dentry *parent, 493 unsigned long *value) 494 { 495 if (sizeof(*value) == sizeof(u32)) 496 debugfs_create_x32(name, mode, parent, (u32 *)value); 497 else 498 debugfs_create_x64(name, mode, parent, (u64 *)value); 499 } 500 501 #endif 502