debugfs.h 10.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11
/*
 *  debugfs.h - a tiny little debug file system
 *
 *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
 *  Copyright (C) 2004 IBM Inc.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License version
 *	2 as published by the Free Software Foundation.
 *
 *  debugfs is for people to use instead of /proc or /sys.
12
 *  See Documentation/DocBook/filesystems for more details.
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16 17 18
 */

#ifndef _DEBUGFS_H_
#define _DEBUGFS_H_

#include <linux/fs.h>
19
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
20

21
#include <linux/types.h>
22
#include <linux/compiler.h>
23

24
struct device;
25
struct file_operations;
26
struct srcu_struct;
27

28 29 30 31 32
struct debugfs_blob_wrapper {
	void *data;
	unsigned long size;
};

33 34 35 36 37 38
struct debugfs_reg32 {
	char *name;
	unsigned long offset;
};

struct debugfs_regset32 {
39
	const struct debugfs_reg32 *regs;
40 41 42 43
	int nregs;
	void __iomem *base;
};

44 45
extern struct dentry *arch_debugfs_dir;

46 47
extern struct srcu_struct debugfs_srcu;

48 49 50 51 52 53 54
/**
 * debugfs_real_fops - getter for the real file operation
 * @filp: a pointer to a struct file
 *
 * Must only be called under the protection established by
 * debugfs_use_file_start().
 */
55
static inline const struct file_operations *debugfs_real_fops(const struct file *filp)
56 57 58 59 60 61 62 63 64
	__must_hold(&debugfs_srcu)
{
	/*
	 * Neither the pointer to the struct file_operations, nor its
	 * contents ever change -- srcu_dereference() is not needed here.
	 */
	return filp->f_path.dentry->d_fsdata;
}

65 66 67 68 69 70 71 72 73 74 75 76
#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt)		\
static int __fops ## _open(struct inode *inode, struct file *file)	\
{									\
	__simple_attr_check_format(__fmt, 0ull);			\
	return simple_attr_open(inode, file, __get, __set, __fmt);	\
}									\
static const struct file_operations __fops = {				\
	.owner	 = THIS_MODULE,						\
	.open	 = __fops ## _open,					\
	.release = simple_attr_release,					\
	.read	 = debugfs_attr_read,					\
	.write	 = debugfs_attr_write,					\
77
	.llseek  = no_llseek,						\
78 79
}

Linus Torvalds's avatar
Linus Torvalds committed
80
#if defined(CONFIG_DEBUG_FS)
81

82 83
struct dentry *debugfs_lookup(const char *name, struct dentry *parent);

Al Viro's avatar
Al Viro committed
84
struct dentry *debugfs_create_file(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
85
				   struct dentry *parent, void *data,
86
				   const struct file_operations *fops);
87 88 89
struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
				   struct dentry *parent, void *data,
				   const struct file_operations *fops);
Linus Torvalds's avatar
Linus Torvalds committed
90

91 92 93 94 95
struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
					struct dentry *parent, void *data,
					const struct file_operations *fops,
					loff_t file_size);

Linus Torvalds's avatar
Linus Torvalds committed
96 97
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);

98 99 100
struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
				      const char *dest);

101
typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
102 103
struct dentry *debugfs_create_automount(const char *name,
					struct dentry *parent,
104
					debugfs_automount_t f,
105 106
					void *data);

Linus Torvalds's avatar
Linus Torvalds committed
107
void debugfs_remove(struct dentry *dentry);
108
void debugfs_remove_recursive(struct dentry *dentry);
Linus Torvalds's avatar
Linus Torvalds committed
109

110 111 112 113 114
int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
	__acquires(&debugfs_srcu);

void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu);

115 116 117 118 119
ssize_t debugfs_attr_read(struct file *file, char __user *buf,
			size_t len, loff_t *ppos);
ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
			size_t len, loff_t *ppos);

120 121 122
struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
                struct dentry *new_dir, const char *new_name);

Al Viro's avatar
Al Viro committed
123
struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
124
				 struct dentry *parent, u8 *value);
Al Viro's avatar
Al Viro committed
125
struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
126
				  struct dentry *parent, u16 *value);
Al Viro's avatar
Al Viro committed
127
struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
128
				  struct dentry *parent, u32 *value);
Al Viro's avatar
Al Viro committed
129
struct dentry *debugfs_create_u64(const char *name, umode_t mode,
130
				  struct dentry *parent, u64 *value);
131 132
struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
				    struct dentry *parent, unsigned long *value);
Al Viro's avatar
Al Viro committed
133
struct dentry *debugfs_create_x8(const char *name, umode_t mode,
134
				 struct dentry *parent, u8 *value);
Al Viro's avatar
Al Viro committed
135
struct dentry *debugfs_create_x16(const char *name, umode_t mode,
136
				  struct dentry *parent, u16 *value);
Al Viro's avatar
Al Viro committed
137
struct dentry *debugfs_create_x32(const char *name, umode_t mode,
138
				  struct dentry *parent, u32 *value);
Al Viro's avatar
Al Viro committed
139
struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying's avatar
Huang Ying committed
140
				  struct dentry *parent, u64 *value);
Al Viro's avatar
Al Viro committed
141
struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
142
				     struct dentry *parent, size_t *value);
143 144
struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
				     struct dentry *parent, atomic_t *value);
Al Viro's avatar
Al Viro committed
145
struct dentry *debugfs_create_bool(const char *name, umode_t mode,
146
				  struct dentry *parent, bool *value);
Linus Torvalds's avatar
Linus Torvalds committed
147

Al Viro's avatar
Al Viro committed
148
struct dentry *debugfs_create_blob(const char *name, umode_t mode,
149 150
				  struct dentry *parent,
				  struct debugfs_blob_wrapper *blob);
151

152
struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
153 154 155
				     struct dentry *parent,
				     struct debugfs_regset32 *regset);

156 157
void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
			  int nregs, void __iomem *base, char *prefix);
158

159 160 161 162
struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
					struct dentry *parent,
					u32 *array, u32 elements);

163 164 165 166 167
struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
					   struct dentry *parent,
					   int (*read_fn)(struct seq_file *s,
							  void *data));

168 169
bool debugfs_initialized(void);

170 171 172 173 174 175
ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
			       size_t count, loff_t *ppos);

ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
				size_t count, loff_t *ppos);

Linus Torvalds's avatar
Linus Torvalds committed
176
#else
177 178 179

#include <linux/err.h>

180
/*
Linus Torvalds's avatar
Linus Torvalds committed
181 182 183 184 185
 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
 * so users have a chance to detect if there was a real error or not.  We don't
 * want to duplicate the design decision mistakes of procfs and devfs again.
 */

186 187 188 189 190 191
static inline struct dentry *debugfs_lookup(const char *name,
					    struct dentry *parent)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
192
static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
193 194
					struct dentry *parent, void *data,
					const struct file_operations *fops)
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198
{
	return ERR_PTR(-ENODEV);
}

199 200 201 202 203 204 205 206
static inline struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
					struct dentry *parent, void *data,
					const struct file_operations *fops,
					loff_t file_size)
{
	return ERR_PTR(-ENODEV);
}

Linus Torvalds's avatar
Linus Torvalds committed
207 208 209 210 211 212
static inline struct dentry *debugfs_create_dir(const char *name,
						struct dentry *parent)
{
	return ERR_PTR(-ENODEV);
}

213 214 215 216 217 218 219
static inline struct dentry *debugfs_create_symlink(const char *name,
						    struct dentry *parent,
						    const char *dest)
{
	return ERR_PTR(-ENODEV);
}

220 221 222 223 224 225 226 227
static inline struct dentry *debugfs_create_automount(const char *name,
					struct dentry *parent,
					struct vfsmount *(*f)(void *),
					void *data)
{
	return ERR_PTR(-ENODEV);
}

Linus Torvalds's avatar
Linus Torvalds committed
228 229 230
static inline void debugfs_remove(struct dentry *dentry)
{ }

231 232 233
static inline void debugfs_remove_recursive(struct dentry *dentry)
{ }

234 235 236 237 238 239 240 241 242 243 244
static inline int debugfs_use_file_start(const struct dentry *dentry,
					int *srcu_idx)
	__acquires(&debugfs_srcu)
{
	return 0;
}

static inline void debugfs_use_file_finish(int srcu_idx)
	__releases(&debugfs_srcu)
{ }

245 246 247 248 249 250 251 252 253 254 255 256
static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
					size_t len, loff_t *ppos)
{
	return -ENODEV;
}

static inline ssize_t debugfs_attr_write(struct file *file,
					const char __user *buf,
					size_t len, loff_t *ppos)
{
	return -ENODEV;
}
257

258 259 260 261 262 263
static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
                struct dentry *new_dir, char *new_name)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
264
static inline struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270
					       struct dentry *parent,
					       u8 *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
271
static inline struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
272
						struct dentry *parent,
273
						u16 *value)
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
278
static inline struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
279
						struct dentry *parent,
280
						u32 *value)
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
285
static inline struct dentry *debugfs_create_u64(const char *name, umode_t mode,
286 287 288 289 290 291
						struct dentry *parent,
						u64 *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
292
static inline struct dentry *debugfs_create_x8(const char *name, umode_t mode,
293 294 295 296 297 298
					       struct dentry *parent,
					       u8 *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
299
static inline struct dentry *debugfs_create_x16(const char *name, umode_t mode,
300 301 302 303 304 305
						struct dentry *parent,
						u16 *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
306
static inline struct dentry *debugfs_create_x32(const char *name, umode_t mode,
307 308 309 310 311 312
						struct dentry *parent,
						u32 *value)
{
	return ERR_PTR(-ENODEV);
}

313 314 315 316 317 318 319
static inline struct dentry *debugfs_create_x64(const char *name, umode_t mode,
						struct dentry *parent,
						u64 *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
320
static inline struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
321 322 323 324 325 326
				     struct dentry *parent,
				     size_t *value)
{
	return ERR_PTR(-ENODEV);
}

327 328 329 330 331 332
static inline struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
				     struct dentry *parent, atomic_t *value)
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
333
static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Linus Torvalds's avatar
Linus Torvalds committed
334
						 struct dentry *parent,
335
						 bool *value)
Linus Torvalds's avatar
Linus Torvalds committed
336 337 338 339
{
	return ERR_PTR(-ENODEV);
}

Al Viro's avatar
Al Viro committed
340
static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
341 342 343 344 345 346
				  struct dentry *parent,
				  struct debugfs_blob_wrapper *blob)
{
	return ERR_PTR(-ENODEV);
}

347
static inline struct dentry *debugfs_create_regset32(const char *name,
348
				   umode_t mode, struct dentry *parent,
349 350 351 352 353
				   struct debugfs_regset32 *regset)
{
	return ERR_PTR(-ENODEV);
}

354
static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
355 356 357 358
			 int nregs, void __iomem *base, char *prefix)
{
}

359 360 361 362 363
static inline bool debugfs_initialized(void)
{
	return false;
}

364 365 366 367 368 369 370
static inline struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
					struct dentry *parent,
					u32 *array, u32 elements)
{
	return ERR_PTR(-ENODEV);
}

371 372 373 374 375 376 377 378 379
static inline struct dentry *debugfs_create_devm_seqfile(struct device *dev,
							 const char *name,
							 struct dentry *parent,
					   int (*read_fn)(struct seq_file *s,
							  void *data))
{
	return ERR_PTR(-ENODEV);
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393
static inline ssize_t debugfs_read_file_bool(struct file *file,
					     char __user *user_buf,
					     size_t count, loff_t *ppos)
{
	return -ENODEV;
}

static inline ssize_t debugfs_write_file_bool(struct file *file,
					      const char __user *user_buf,
					      size_t count, loff_t *ppos)
{
	return -ENODEV;
}

Linus Torvalds's avatar
Linus Torvalds committed
394 395 396
#endif

#endif