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