GNU Linux-libre 5.4.241-gnu1
[releases.git] / include / linux / debugfs.h
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 };
39
40 extern struct dentry *arch_debugfs_dir;
41
42 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed)      \
43 static int __fops ## _open(struct inode *inode, struct file *file)      \
44 {                                                                       \
45         __simple_attr_check_format(__fmt, 0ull);                        \
46         return simple_attr_open(inode, file, __get, __set, __fmt);      \
47 }                                                                       \
48 static const struct file_operations __fops = {                          \
49         .owner   = THIS_MODULE,                                         \
50         .open    = __fops ## _open,                                     \
51         .release = simple_attr_release,                                 \
52         .read    = debugfs_attr_read,                                   \
53         .write   = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write,      \
54         .llseek  = no_llseek,                                           \
55 }
56
57 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt)           \
58         DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
59
60 #define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt)    \
61         DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
62
63 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
64
65 #if defined(CONFIG_DEBUG_FS)
66
67 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
68
69 struct dentry *debugfs_create_file(const char *name, umode_t mode,
70                                    struct dentry *parent, void *data,
71                                    const struct file_operations *fops);
72 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
73                                    struct dentry *parent, void *data,
74                                    const struct file_operations *fops);
75
76 struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
77                                         struct dentry *parent, void *data,
78                                         const struct file_operations *fops,
79                                         loff_t file_size);
80
81 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
82
83 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
84                                       const char *dest);
85
86 struct dentry *debugfs_create_automount(const char *name,
87                                         struct dentry *parent,
88                                         debugfs_automount_t f,
89                                         void *data);
90
91 void debugfs_remove(struct dentry *dentry);
92 void debugfs_remove_recursive(struct dentry *dentry);
93
94 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
95
96 const struct file_operations *debugfs_real_fops(const struct file *filp);
97
98 int debugfs_file_get(struct dentry *dentry);
99 void debugfs_file_put(struct dentry *dentry);
100
101 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
102                         size_t len, loff_t *ppos);
103 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
104                         size_t len, loff_t *ppos);
105 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
106                         size_t len, loff_t *ppos);
107
108 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
109                 struct dentry *new_dir, const char *new_name);
110
111 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
112                                  struct dentry *parent, u8 *value);
113 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
114                                   struct dentry *parent, u16 *value);
115 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
116                                   struct dentry *parent, u32 *value);
117 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
118                                   struct dentry *parent, u64 *value);
119 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
120                                     struct dentry *parent, unsigned long *value);
121 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
122                                  struct dentry *parent, u8 *value);
123 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
124                                   struct dentry *parent, u16 *value);
125 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
126                                   struct dentry *parent, u32 *value);
127 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
128                                   struct dentry *parent, u64 *value);
129 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
130                                      struct dentry *parent, size_t *value);
131 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
132                                      struct dentry *parent, atomic_t *value);
133 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
134                                   struct dentry *parent, bool *value);
135
136 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
137                                   struct dentry *parent,
138                                   struct debugfs_blob_wrapper *blob);
139
140 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
141                                      struct dentry *parent,
142                                      struct debugfs_regset32 *regset);
143
144 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
145                           int nregs, void __iomem *base, char *prefix);
146
147 void debugfs_create_u32_array(const char *name, umode_t mode,
148                               struct dentry *parent, u32 *array, u32 elements);
149
150 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
151                                            struct dentry *parent,
152                                            int (*read_fn)(struct seq_file *s,
153                                                           void *data));
154
155 bool debugfs_initialized(void);
156
157 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
158                                size_t count, loff_t *ppos);
159
160 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
161                                 size_t count, loff_t *ppos);
162
163 #else
164
165 #include <linux/err.h>
166
167 /*
168  * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
169  * so users have a chance to detect if there was a real error or not.  We don't
170  * want to duplicate the design decision mistakes of procfs and devfs again.
171  */
172
173 static inline struct dentry *debugfs_lookup(const char *name,
174                                             struct dentry *parent)
175 {
176         return ERR_PTR(-ENODEV);
177 }
178
179 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
180                                         struct dentry *parent, void *data,
181                                         const struct file_operations *fops)
182 {
183         return ERR_PTR(-ENODEV);
184 }
185
186 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
187                                         umode_t mode, struct dentry *parent,
188                                         void *data,
189                                         const struct file_operations *fops)
190 {
191         return ERR_PTR(-ENODEV);
192 }
193
194 static inline struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
195                                         struct dentry *parent, void *data,
196                                         const struct file_operations *fops,
197                                         loff_t file_size)
198 {
199         return ERR_PTR(-ENODEV);
200 }
201
202 static inline struct dentry *debugfs_create_dir(const char *name,
203                                                 struct dentry *parent)
204 {
205         return ERR_PTR(-ENODEV);
206 }
207
208 static inline struct dentry *debugfs_create_symlink(const char *name,
209                                                     struct dentry *parent,
210                                                     const char *dest)
211 {
212         return ERR_PTR(-ENODEV);
213 }
214
215 static inline struct dentry *debugfs_create_automount(const char *name,
216                                         struct dentry *parent,
217                                         debugfs_automount_t f,
218                                         void *data)
219 {
220         return ERR_PTR(-ENODEV);
221 }
222
223 static inline void debugfs_remove(struct dentry *dentry)
224 { }
225
226 static inline void debugfs_remove_recursive(struct dentry *dentry)
227 { }
228
229 static inline void debugfs_lookup_and_remove(const char *name,
230                                              struct dentry *parent)
231 { }
232
233 const struct file_operations *debugfs_real_fops(const struct file *filp);
234
235 static inline int debugfs_file_get(struct dentry *dentry)
236 {
237         return 0;
238 }
239
240 static inline void debugfs_file_put(struct dentry *dentry)
241 { }
242
243 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
244                                         size_t len, loff_t *ppos)
245 {
246         return -ENODEV;
247 }
248
249 static inline ssize_t debugfs_attr_write(struct file *file,
250                                         const char __user *buf,
251                                         size_t len, loff_t *ppos)
252 {
253         return -ENODEV;
254 }
255
256 static inline ssize_t debugfs_attr_write_signed(struct file *file,
257                                         const char __user *buf,
258                                         size_t len, loff_t *ppos)
259 {
260         return -ENODEV;
261 }
262
263 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
264                 struct dentry *new_dir, char *new_name)
265 {
266         return ERR_PTR(-ENODEV);
267 }
268
269 static inline struct dentry *debugfs_create_u8(const char *name, umode_t mode,
270                                                struct dentry *parent,
271                                                u8 *value)
272 {
273         return ERR_PTR(-ENODEV);
274 }
275
276 static inline struct dentry *debugfs_create_u16(const char *name, umode_t mode,
277                                                 struct dentry *parent,
278                                                 u16 *value)
279 {
280         return ERR_PTR(-ENODEV);
281 }
282
283 static inline struct dentry *debugfs_create_u32(const char *name, umode_t mode,
284                                                 struct dentry *parent,
285                                                 u32 *value)
286 {
287         return ERR_PTR(-ENODEV);
288 }
289
290 static inline struct dentry *debugfs_create_u64(const char *name, umode_t mode,
291                                                 struct dentry *parent,
292                                                 u64 *value)
293 {
294         return ERR_PTR(-ENODEV);
295 }
296
297 static inline struct dentry *debugfs_create_ulong(const char *name,
298                                                 umode_t mode,
299                                                 struct dentry *parent,
300                                                 unsigned long *value)
301 {
302         return ERR_PTR(-ENODEV);
303 }
304
305 static inline struct dentry *debugfs_create_x8(const char *name, umode_t mode,
306                                                struct dentry *parent,
307                                                u8 *value)
308 {
309         return ERR_PTR(-ENODEV);
310 }
311
312 static inline struct dentry *debugfs_create_x16(const char *name, umode_t mode,
313                                                 struct dentry *parent,
314                                                 u16 *value)
315 {
316         return ERR_PTR(-ENODEV);
317 }
318
319 static inline struct dentry *debugfs_create_x32(const char *name, umode_t mode,
320                                                 struct dentry *parent,
321                                                 u32 *value)
322 {
323         return ERR_PTR(-ENODEV);
324 }
325
326 static inline struct dentry *debugfs_create_x64(const char *name, umode_t mode,
327                                                 struct dentry *parent,
328                                                 u64 *value)
329 {
330         return ERR_PTR(-ENODEV);
331 }
332
333 static inline struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
334                                      struct dentry *parent,
335                                      size_t *value)
336 {
337         return ERR_PTR(-ENODEV);
338 }
339
340 static inline struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
341                                      struct dentry *parent, atomic_t *value)
342 {
343         return ERR_PTR(-ENODEV);
344 }
345
346 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
347                                                  struct dentry *parent,
348                                                  bool *value)
349 {
350         return ERR_PTR(-ENODEV);
351 }
352
353 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
354                                   struct dentry *parent,
355                                   struct debugfs_blob_wrapper *blob)
356 {
357         return ERR_PTR(-ENODEV);
358 }
359
360 static inline struct dentry *debugfs_create_regset32(const char *name,
361                                    umode_t mode, struct dentry *parent,
362                                    struct debugfs_regset32 *regset)
363 {
364         return ERR_PTR(-ENODEV);
365 }
366
367 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
368                          int nregs, void __iomem *base, char *prefix)
369 {
370 }
371
372 static inline bool debugfs_initialized(void)
373 {
374         return false;
375 }
376
377 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
378                                             struct dentry *parent, u32 *array,
379                                             u32 elements)
380 {
381 }
382
383 static inline struct dentry *debugfs_create_devm_seqfile(struct device *dev,
384                                                          const char *name,
385                                                          struct dentry *parent,
386                                            int (*read_fn)(struct seq_file *s,
387                                                           void *data))
388 {
389         return ERR_PTR(-ENODEV);
390 }
391
392 static inline ssize_t debugfs_read_file_bool(struct file *file,
393                                              char __user *user_buf,
394                                              size_t count, loff_t *ppos)
395 {
396         return -ENODEV;
397 }
398
399 static inline ssize_t debugfs_write_file_bool(struct file *file,
400                                               const char __user *user_buf,
401                                               size_t count, loff_t *ppos)
402 {
403         return -ENODEV;
404 }
405
406 #endif
407
408 #endif