xref: /openbmc/linux/fs/debugfs/file.c (revision 7d39bc50)
1 /*
2  *  file.c - part of debugfs, a tiny little debug file system
3  *
4  *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  *  Copyright (C) 2004 IBM Inc.
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License version
9  *	2 as published by the Free Software Foundation.
10  *
11  *  debugfs is for people to use instead of /proc or /sys.
12  *  See Documentation/filesystems/ for more details.
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/debugfs.h>
21 #include <linux/io.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/device.h>
25 #include <asm/poll.h>
26 
27 #include "internal.h"
28 
29 struct poll_table_struct;
30 
31 static ssize_t default_read_file(struct file *file, char __user *buf,
32 				 size_t count, loff_t *ppos)
33 {
34 	return 0;
35 }
36 
37 static ssize_t default_write_file(struct file *file, const char __user *buf,
38 				   size_t count, loff_t *ppos)
39 {
40 	return count;
41 }
42 
43 const struct file_operations debugfs_noop_file_operations = {
44 	.read =		default_read_file,
45 	.write =	default_write_file,
46 	.open =		simple_open,
47 	.llseek =	noop_llseek,
48 };
49 
50 #define F_DENTRY(filp) ((filp)->f_path.dentry)
51 
52 const struct file_operations *debugfs_real_fops(const struct file *filp)
53 {
54 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
55 
56 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
57 		/*
58 		 * Urgh, we've been called w/o a protecting
59 		 * debugfs_file_get().
60 		 */
61 		WARN_ON(1);
62 		return NULL;
63 	}
64 
65 	return fsd->real_fops;
66 }
67 EXPORT_SYMBOL_GPL(debugfs_real_fops);
68 
69 /**
70  * debugfs_file_get - mark the beginning of file data access
71  * @dentry: the dentry object whose data is being accessed.
72  *
73  * Up to a matching call to debugfs_file_put(), any successive call
74  * into the file removing functions debugfs_remove() and
75  * debugfs_remove_recursive() will block. Since associated private
76  * file data may only get freed after a successful return of any of
77  * the removal functions, you may safely access it after a successful
78  * call to debugfs_file_get() without worrying about lifetime issues.
79  *
80  * If -%EIO is returned, the file has already been removed and thus,
81  * it is not safe to access any of its data. If, on the other hand,
82  * it is allowed to access the file data, zero is returned.
83  */
84 int debugfs_file_get(struct dentry *dentry)
85 {
86 	struct debugfs_fsdata *fsd;
87 	void *d_fsd;
88 
89 	d_fsd = READ_ONCE(dentry->d_fsdata);
90 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
91 		fsd = d_fsd;
92 	} else {
93 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
94 		if (!fsd)
95 			return -ENOMEM;
96 
97 		fsd->real_fops = (void *)((unsigned long)d_fsd &
98 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
99 		refcount_set(&fsd->active_users, 1);
100 		init_completion(&fsd->active_users_drained);
101 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
102 			kfree(fsd);
103 			fsd = READ_ONCE(dentry->d_fsdata);
104 		}
105 	}
106 
107 	/*
108 	 * In case of a successful cmpxchg() above, this check is
109 	 * strictly necessary and must follow it, see the comment in
110 	 * __debugfs_remove_file().
111 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
112 	 * successful, this serves the purpose of not starving
113 	 * removers.
114 	 */
115 	if (d_unlinked(dentry))
116 		return -EIO;
117 
118 	if (!refcount_inc_not_zero(&fsd->active_users))
119 		return -EIO;
120 
121 	return 0;
122 }
123 EXPORT_SYMBOL_GPL(debugfs_file_get);
124 
125 /**
126  * debugfs_file_put - mark the end of file data access
127  * @dentry: the dentry object formerly passed to
128  *          debugfs_file_get().
129  *
130  * Allow any ongoing concurrent call into debugfs_remove() or
131  * debugfs_remove_recursive() blocked by a former call to
132  * debugfs_file_get() to proceed and return to its caller.
133  */
134 void debugfs_file_put(struct dentry *dentry)
135 {
136 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
137 
138 	if (refcount_dec_and_test(&fsd->active_users))
139 		complete(&fsd->active_users_drained);
140 }
141 EXPORT_SYMBOL_GPL(debugfs_file_put);
142 
143 static int open_proxy_open(struct inode *inode, struct file *filp)
144 {
145 	struct dentry *dentry = F_DENTRY(filp);
146 	const struct file_operations *real_fops = NULL;
147 	int r;
148 
149 	r = debugfs_file_get(dentry);
150 	if (r)
151 		return r == -EIO ? -ENOENT : r;
152 
153 	real_fops = debugfs_real_fops(filp);
154 	real_fops = fops_get(real_fops);
155 	if (!real_fops) {
156 		/* Huh? Module did not clean up after itself at exit? */
157 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
158 			dentry);
159 		r = -ENXIO;
160 		goto out;
161 	}
162 	replace_fops(filp, real_fops);
163 
164 	if (real_fops->open)
165 		r = real_fops->open(inode, filp);
166 
167 out:
168 	debugfs_file_put(dentry);
169 	return r;
170 }
171 
172 const struct file_operations debugfs_open_proxy_file_operations = {
173 	.open = open_proxy_open,
174 };
175 
176 #define PROTO(args...) args
177 #define ARGS(args...) args
178 
179 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
180 static ret_type full_proxy_ ## name(proto)				\
181 {									\
182 	struct dentry *dentry = F_DENTRY(filp);			\
183 	const struct file_operations *real_fops;			\
184 	ret_type r;							\
185 									\
186 	r = debugfs_file_get(dentry);					\
187 	if (unlikely(r))						\
188 		return r;						\
189 	real_fops = debugfs_real_fops(filp);				\
190 	r = real_fops->name(args);					\
191 	debugfs_file_put(dentry);					\
192 	return r;							\
193 }
194 
195 FULL_PROXY_FUNC(llseek, loff_t, filp,
196 		PROTO(struct file *filp, loff_t offset, int whence),
197 		ARGS(filp, offset, whence));
198 
199 FULL_PROXY_FUNC(read, ssize_t, filp,
200 		PROTO(struct file *filp, char __user *buf, size_t size,
201 			loff_t *ppos),
202 		ARGS(filp, buf, size, ppos));
203 
204 FULL_PROXY_FUNC(write, ssize_t, filp,
205 		PROTO(struct file *filp, const char __user *buf, size_t size,
206 			loff_t *ppos),
207 		ARGS(filp, buf, size, ppos));
208 
209 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
210 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
211 		ARGS(filp, cmd, arg));
212 
213 static unsigned int full_proxy_poll(struct file *filp,
214 				struct poll_table_struct *wait)
215 {
216 	struct dentry *dentry = F_DENTRY(filp);
217 	unsigned int r = 0;
218 	const struct file_operations *real_fops;
219 
220 	if (debugfs_file_get(dentry))
221 		return POLLHUP;
222 
223 	real_fops = debugfs_real_fops(filp);
224 	r = real_fops->poll(filp, wait);
225 	debugfs_file_put(dentry);
226 	return r;
227 }
228 
229 static int full_proxy_release(struct inode *inode, struct file *filp)
230 {
231 	const struct dentry *dentry = F_DENTRY(filp);
232 	const struct file_operations *real_fops = debugfs_real_fops(filp);
233 	const struct file_operations *proxy_fops = filp->f_op;
234 	int r = 0;
235 
236 	/*
237 	 * We must not protect this against removal races here: the
238 	 * original releaser should be called unconditionally in order
239 	 * not to leak any resources. Releasers must not assume that
240 	 * ->i_private is still being meaningful here.
241 	 */
242 	if (real_fops->release)
243 		r = real_fops->release(inode, filp);
244 
245 	replace_fops(filp, d_inode(dentry)->i_fop);
246 	kfree((void *)proxy_fops);
247 	fops_put(real_fops);
248 	return r;
249 }
250 
251 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
252 				const struct file_operations *real_fops)
253 {
254 	proxy_fops->release = full_proxy_release;
255 	if (real_fops->llseek)
256 		proxy_fops->llseek = full_proxy_llseek;
257 	if (real_fops->read)
258 		proxy_fops->read = full_proxy_read;
259 	if (real_fops->write)
260 		proxy_fops->write = full_proxy_write;
261 	if (real_fops->poll)
262 		proxy_fops->poll = full_proxy_poll;
263 	if (real_fops->unlocked_ioctl)
264 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
265 }
266 
267 static int full_proxy_open(struct inode *inode, struct file *filp)
268 {
269 	struct dentry *dentry = F_DENTRY(filp);
270 	const struct file_operations *real_fops = NULL;
271 	struct file_operations *proxy_fops = NULL;
272 	int r;
273 
274 	r = debugfs_file_get(dentry);
275 	if (r)
276 		return r == -EIO ? -ENOENT : r;
277 
278 	real_fops = debugfs_real_fops(filp);
279 	real_fops = fops_get(real_fops);
280 	if (!real_fops) {
281 		/* Huh? Module did not cleanup after itself at exit? */
282 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
283 			dentry);
284 		r = -ENXIO;
285 		goto out;
286 	}
287 
288 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
289 	if (!proxy_fops) {
290 		r = -ENOMEM;
291 		goto free_proxy;
292 	}
293 	__full_proxy_fops_init(proxy_fops, real_fops);
294 	replace_fops(filp, proxy_fops);
295 
296 	if (real_fops->open) {
297 		r = real_fops->open(inode, filp);
298 		if (r) {
299 			replace_fops(filp, d_inode(dentry)->i_fop);
300 			goto free_proxy;
301 		} else if (filp->f_op != proxy_fops) {
302 			/* No protection against file removal anymore. */
303 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
304 				dentry);
305 			goto free_proxy;
306 		}
307 	}
308 
309 	goto out;
310 free_proxy:
311 	kfree(proxy_fops);
312 	fops_put(real_fops);
313 out:
314 	debugfs_file_put(dentry);
315 	return r;
316 }
317 
318 const struct file_operations debugfs_full_proxy_file_operations = {
319 	.open = full_proxy_open,
320 };
321 
322 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
323 			size_t len, loff_t *ppos)
324 {
325 	struct dentry *dentry = F_DENTRY(file);
326 	ssize_t ret;
327 
328 	ret = debugfs_file_get(dentry);
329 	if (unlikely(ret))
330 		return ret;
331 	ret = simple_attr_read(file, buf, len, ppos);
332 	debugfs_file_put(dentry);
333 	return ret;
334 }
335 EXPORT_SYMBOL_GPL(debugfs_attr_read);
336 
337 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
338 			 size_t len, loff_t *ppos)
339 {
340 	struct dentry *dentry = F_DENTRY(file);
341 	ssize_t ret;
342 
343 	ret = debugfs_file_get(dentry);
344 	if (unlikely(ret))
345 		return ret;
346 	ret = simple_attr_write(file, buf, len, ppos);
347 	debugfs_file_put(dentry);
348 	return ret;
349 }
350 EXPORT_SYMBOL_GPL(debugfs_attr_write);
351 
352 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
353 					struct dentry *parent, void *value,
354 					const struct file_operations *fops,
355 					const struct file_operations *fops_ro,
356 					const struct file_operations *fops_wo)
357 {
358 	/* if there are no write bits set, make read only */
359 	if (!(mode & S_IWUGO))
360 		return debugfs_create_file_unsafe(name, mode, parent, value,
361 						fops_ro);
362 	/* if there are no read bits set, make write only */
363 	if (!(mode & S_IRUGO))
364 		return debugfs_create_file_unsafe(name, mode, parent, value,
365 						fops_wo);
366 
367 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
368 }
369 
370 static int debugfs_u8_set(void *data, u64 val)
371 {
372 	*(u8 *)data = val;
373 	return 0;
374 }
375 static int debugfs_u8_get(void *data, u64 *val)
376 {
377 	*val = *(u8 *)data;
378 	return 0;
379 }
380 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
381 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
382 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
383 
384 /**
385  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
386  * @name: a pointer to a string containing the name of the file to create.
387  * @mode: the permission that the file should have
388  * @parent: a pointer to the parent dentry for this file.  This should be a
389  *          directory dentry if set.  If this parameter is %NULL, then the
390  *          file will be created in the root of the debugfs filesystem.
391  * @value: a pointer to the variable that the file should read to and write
392  *         from.
393  *
394  * This function creates a file in debugfs with the given name that
395  * contains the value of the variable @value.  If the @mode variable is so
396  * set, it can be read from, and written to.
397  *
398  * This function will return a pointer to a dentry if it succeeds.  This
399  * pointer must be passed to the debugfs_remove() function when the file is
400  * to be removed (no automatic cleanup happens if your module is unloaded,
401  * you are responsible here.)  If an error occurs, %NULL will be returned.
402  *
403  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
404  * returned.  It is not wise to check for this value, but rather, check for
405  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
406  * code.
407  */
408 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
409 				 struct dentry *parent, u8 *value)
410 {
411 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
412 				   &fops_u8_ro, &fops_u8_wo);
413 }
414 EXPORT_SYMBOL_GPL(debugfs_create_u8);
415 
416 static int debugfs_u16_set(void *data, u64 val)
417 {
418 	*(u16 *)data = val;
419 	return 0;
420 }
421 static int debugfs_u16_get(void *data, u64 *val)
422 {
423 	*val = *(u16 *)data;
424 	return 0;
425 }
426 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
427 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
428 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
429 
430 /**
431  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
432  * @name: a pointer to a string containing the name of the file to create.
433  * @mode: the permission that the file should have
434  * @parent: a pointer to the parent dentry for this file.  This should be a
435  *          directory dentry if set.  If this parameter is %NULL, then the
436  *          file will be created in the root of the debugfs filesystem.
437  * @value: a pointer to the variable that the file should read to and write
438  *         from.
439  *
440  * This function creates a file in debugfs with the given name that
441  * contains the value of the variable @value.  If the @mode variable is so
442  * set, it can be read from, and written to.
443  *
444  * This function will return a pointer to a dentry if it succeeds.  This
445  * pointer must be passed to the debugfs_remove() function when the file is
446  * to be removed (no automatic cleanup happens if your module is unloaded,
447  * you are responsible here.)  If an error occurs, %NULL will be returned.
448  *
449  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
450  * returned.  It is not wise to check for this value, but rather, check for
451  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
452  * code.
453  */
454 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
455 				  struct dentry *parent, u16 *value)
456 {
457 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
458 				   &fops_u16_ro, &fops_u16_wo);
459 }
460 EXPORT_SYMBOL_GPL(debugfs_create_u16);
461 
462 static int debugfs_u32_set(void *data, u64 val)
463 {
464 	*(u32 *)data = val;
465 	return 0;
466 }
467 static int debugfs_u32_get(void *data, u64 *val)
468 {
469 	*val = *(u32 *)data;
470 	return 0;
471 }
472 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
473 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
474 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
475 
476 /**
477  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
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  * This function creates a file in debugfs with the given name that
487  * contains the value of the variable @value.  If the @mode variable is so
488  * set, it can be read from, and written to.
489  *
490  * This function will return a pointer to a dentry if it succeeds.  This
491  * pointer must be passed to the debugfs_remove() function when the file is
492  * to be removed (no automatic cleanup happens if your module is unloaded,
493  * you are responsible here.)  If an error occurs, %NULL will be returned.
494  *
495  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
496  * returned.  It is not wise to check for this value, but rather, check for
497  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
498  * code.
499  */
500 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
501 				 struct dentry *parent, u32 *value)
502 {
503 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
504 				   &fops_u32_ro, &fops_u32_wo);
505 }
506 EXPORT_SYMBOL_GPL(debugfs_create_u32);
507 
508 static int debugfs_u64_set(void *data, u64 val)
509 {
510 	*(u64 *)data = val;
511 	return 0;
512 }
513 
514 static int debugfs_u64_get(void *data, u64 *val)
515 {
516 	*val = *(u64 *)data;
517 	return 0;
518 }
519 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
520 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
521 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
522 
523 /**
524  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
525  * @name: a pointer to a string containing the name of the file to create.
526  * @mode: the permission that the file should have
527  * @parent: a pointer to the parent dentry for this file.  This should be a
528  *          directory dentry if set.  If this parameter is %NULL, then the
529  *          file will be created in the root of the debugfs filesystem.
530  * @value: a pointer to the variable that the file should read to and write
531  *         from.
532  *
533  * This function creates a file in debugfs with the given name that
534  * contains the value of the variable @value.  If the @mode variable is so
535  * set, it can be read from, and written to.
536  *
537  * This function will return a pointer to a dentry if it succeeds.  This
538  * pointer must be passed to the debugfs_remove() function when the file is
539  * to be removed (no automatic cleanup happens if your module is unloaded,
540  * you are responsible here.)  If an error occurs, %NULL will be returned.
541  *
542  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
543  * returned.  It is not wise to check for this value, but rather, check for
544  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
545  * code.
546  */
547 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
548 				 struct dentry *parent, u64 *value)
549 {
550 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
551 				   &fops_u64_ro, &fops_u64_wo);
552 }
553 EXPORT_SYMBOL_GPL(debugfs_create_u64);
554 
555 static int debugfs_ulong_set(void *data, u64 val)
556 {
557 	*(unsigned long *)data = val;
558 	return 0;
559 }
560 
561 static int debugfs_ulong_get(void *data, u64 *val)
562 {
563 	*val = *(unsigned long *)data;
564 	return 0;
565 }
566 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
567 			"%llu\n");
568 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
569 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
570 
571 /**
572  * debugfs_create_ulong - create a debugfs file that is used to read and write
573  * an unsigned long value.
574  * @name: a pointer to a string containing the name of the file to create.
575  * @mode: the permission that the file should have
576  * @parent: a pointer to the parent dentry for this file.  This should be a
577  *          directory dentry if set.  If this parameter is %NULL, then the
578  *          file will be created in the root of the debugfs filesystem.
579  * @value: a pointer to the variable that the file should read to and write
580  *         from.
581  *
582  * This function creates a file in debugfs with the given name that
583  * contains the value of the variable @value.  If the @mode variable is so
584  * set, it can be read from, and written to.
585  *
586  * This function will return a pointer to a dentry if it succeeds.  This
587  * pointer must be passed to the debugfs_remove() function when the file is
588  * to be removed (no automatic cleanup happens if your module is unloaded,
589  * you are responsible here.)  If an error occurs, %NULL will be returned.
590  *
591  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
592  * returned.  It is not wise to check for this value, but rather, check for
593  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
594  * code.
595  */
596 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
597 				    struct dentry *parent, unsigned long *value)
598 {
599 	return debugfs_create_mode_unsafe(name, mode, parent, value,
600 					&fops_ulong, &fops_ulong_ro,
601 					&fops_ulong_wo);
602 }
603 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
604 
605 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
606 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
607 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
608 
609 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
610 			"0x%04llx\n");
611 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
612 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
613 
614 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
615 			"0x%08llx\n");
616 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
617 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
618 
619 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
620 			"0x%016llx\n");
621 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
622 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
623 
624 /*
625  * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
626  *
627  * These functions are exactly the same as the above functions (but use a hex
628  * output for the decimal challenged). For details look at the above unsigned
629  * decimal functions.
630  */
631 
632 /**
633  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
634  * @name: a pointer to a string containing the name of the file to create.
635  * @mode: the permission that the file should have
636  * @parent: a pointer to the parent dentry for this file.  This should be a
637  *          directory dentry if set.  If this parameter is %NULL, then the
638  *          file will be created in the root of the debugfs filesystem.
639  * @value: a pointer to the variable that the file should read to and write
640  *         from.
641  */
642 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
643 				 struct dentry *parent, u8 *value)
644 {
645 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
646 				   &fops_x8_ro, &fops_x8_wo);
647 }
648 EXPORT_SYMBOL_GPL(debugfs_create_x8);
649 
650 /**
651  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
652  * @name: a pointer to a string containing the name of the file to create.
653  * @mode: the permission that the file should have
654  * @parent: a pointer to the parent dentry for this file.  This should be a
655  *          directory dentry if set.  If this parameter is %NULL, then the
656  *          file will be created in the root of the debugfs filesystem.
657  * @value: a pointer to the variable that the file should read to and write
658  *         from.
659  */
660 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
661 				 struct dentry *parent, u16 *value)
662 {
663 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
664 				   &fops_x16_ro, &fops_x16_wo);
665 }
666 EXPORT_SYMBOL_GPL(debugfs_create_x16);
667 
668 /**
669  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
670  * @name: a pointer to a string containing the name of the file to create.
671  * @mode: the permission that the file should have
672  * @parent: a pointer to the parent dentry for this file.  This should be a
673  *          directory dentry if set.  If this parameter is %NULL, then the
674  *          file will be created in the root of the debugfs filesystem.
675  * @value: a pointer to the variable that the file should read to and write
676  *         from.
677  */
678 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
679 				 struct dentry *parent, u32 *value)
680 {
681 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
682 				   &fops_x32_ro, &fops_x32_wo);
683 }
684 EXPORT_SYMBOL_GPL(debugfs_create_x32);
685 
686 /**
687  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
688  * @name: a pointer to a string containing the name of the file to create.
689  * @mode: the permission that the file should have
690  * @parent: a pointer to the parent dentry for this file.  This should be a
691  *          directory dentry if set.  If this parameter is %NULL, then the
692  *          file will be created in the root of the debugfs filesystem.
693  * @value: a pointer to the variable that the file should read to and write
694  *         from.
695  */
696 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
697 				 struct dentry *parent, u64 *value)
698 {
699 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
700 				   &fops_x64_ro, &fops_x64_wo);
701 }
702 EXPORT_SYMBOL_GPL(debugfs_create_x64);
703 
704 
705 static int debugfs_size_t_set(void *data, u64 val)
706 {
707 	*(size_t *)data = val;
708 	return 0;
709 }
710 static int debugfs_size_t_get(void *data, u64 *val)
711 {
712 	*val = *(size_t *)data;
713 	return 0;
714 }
715 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
716 			"%llu\n"); /* %llu and %zu are more or less the same */
717 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
718 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
719 
720 /**
721  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
722  * @name: a pointer to a string containing the name of the file to create.
723  * @mode: the permission that the file should have
724  * @parent: a pointer to the parent dentry for this file.  This should be a
725  *          directory dentry if set.  If this parameter is %NULL, then the
726  *          file will be created in the root of the debugfs filesystem.
727  * @value: a pointer to the variable that the file should read to and write
728  *         from.
729  */
730 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
731 				     struct dentry *parent, size_t *value)
732 {
733 	return debugfs_create_mode_unsafe(name, mode, parent, value,
734 					&fops_size_t, &fops_size_t_ro,
735 					&fops_size_t_wo);
736 }
737 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
738 
739 static int debugfs_atomic_t_set(void *data, u64 val)
740 {
741 	atomic_set((atomic_t *)data, val);
742 	return 0;
743 }
744 static int debugfs_atomic_t_get(void *data, u64 *val)
745 {
746 	*val = atomic_read((atomic_t *)data);
747 	return 0;
748 }
749 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
750 			debugfs_atomic_t_set, "%lld\n");
751 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
752 			"%lld\n");
753 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
754 			"%lld\n");
755 
756 /**
757  * debugfs_create_atomic_t - create a debugfs file that is used to read and
758  * write an atomic_t value
759  * @name: a pointer to a string containing the name of the file to create.
760  * @mode: the permission that the file should have
761  * @parent: a pointer to the parent dentry for this file.  This should be a
762  *          directory dentry if set.  If this parameter is %NULL, then the
763  *          file will be created in the root of the debugfs filesystem.
764  * @value: a pointer to the variable that the file should read to and write
765  *         from.
766  */
767 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
768 				 struct dentry *parent, atomic_t *value)
769 {
770 	return debugfs_create_mode_unsafe(name, mode, parent, value,
771 					&fops_atomic_t, &fops_atomic_t_ro,
772 					&fops_atomic_t_wo);
773 }
774 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
775 
776 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
777 			       size_t count, loff_t *ppos)
778 {
779 	char buf[3];
780 	bool val;
781 	int r;
782 	struct dentry *dentry = F_DENTRY(file);
783 
784 	r = debugfs_file_get(dentry);
785 	if (unlikely(r))
786 		return r;
787 	val = *(bool *)file->private_data;
788 	debugfs_file_put(dentry);
789 
790 	if (val)
791 		buf[0] = 'Y';
792 	else
793 		buf[0] = 'N';
794 	buf[1] = '\n';
795 	buf[2] = 0x00;
796 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
797 }
798 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
799 
800 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
801 				size_t count, loff_t *ppos)
802 {
803 	char buf[32];
804 	size_t buf_size;
805 	bool bv;
806 	int r;
807 	bool *val = file->private_data;
808 	struct dentry *dentry = F_DENTRY(file);
809 
810 	buf_size = min(count, (sizeof(buf)-1));
811 	if (copy_from_user(buf, user_buf, buf_size))
812 		return -EFAULT;
813 
814 	buf[buf_size] = '\0';
815 	if (strtobool(buf, &bv) == 0) {
816 		r = debugfs_file_get(dentry);
817 		if (unlikely(r))
818 			return r;
819 		*val = bv;
820 		debugfs_file_put(dentry);
821 	}
822 
823 	return count;
824 }
825 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
826 
827 static const struct file_operations fops_bool = {
828 	.read =		debugfs_read_file_bool,
829 	.write =	debugfs_write_file_bool,
830 	.open =		simple_open,
831 	.llseek =	default_llseek,
832 };
833 
834 static const struct file_operations fops_bool_ro = {
835 	.read =		debugfs_read_file_bool,
836 	.open =		simple_open,
837 	.llseek =	default_llseek,
838 };
839 
840 static const struct file_operations fops_bool_wo = {
841 	.write =	debugfs_write_file_bool,
842 	.open =		simple_open,
843 	.llseek =	default_llseek,
844 };
845 
846 /**
847  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
848  * @name: a pointer to a string containing the name of the file to create.
849  * @mode: the permission that the file should have
850  * @parent: a pointer to the parent dentry for this file.  This should be a
851  *          directory dentry if set.  If this parameter is %NULL, then the
852  *          file will be created in the root of the debugfs filesystem.
853  * @value: a pointer to the variable that the file should read to and write
854  *         from.
855  *
856  * This function creates a file in debugfs with the given name that
857  * contains the value of the variable @value.  If the @mode variable is so
858  * set, it can be read from, and written to.
859  *
860  * This function will return a pointer to a dentry if it succeeds.  This
861  * pointer must be passed to the debugfs_remove() function when the file is
862  * to be removed (no automatic cleanup happens if your module is unloaded,
863  * you are responsible here.)  If an error occurs, %NULL will be returned.
864  *
865  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
866  * returned.  It is not wise to check for this value, but rather, check for
867  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
868  * code.
869  */
870 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
871 				   struct dentry *parent, bool *value)
872 {
873 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
874 				   &fops_bool_ro, &fops_bool_wo);
875 }
876 EXPORT_SYMBOL_GPL(debugfs_create_bool);
877 
878 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
879 			      size_t count, loff_t *ppos)
880 {
881 	struct debugfs_blob_wrapper *blob = file->private_data;
882 	struct dentry *dentry = F_DENTRY(file);
883 	ssize_t r;
884 
885 	r = debugfs_file_get(dentry);
886 	if (unlikely(r))
887 		return r;
888 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
889 				blob->size);
890 	debugfs_file_put(dentry);
891 	return r;
892 }
893 
894 static const struct file_operations fops_blob = {
895 	.read =		read_file_blob,
896 	.open =		simple_open,
897 	.llseek =	default_llseek,
898 };
899 
900 /**
901  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
902  * @name: a pointer to a string containing the name of the file to create.
903  * @mode: the permission that the file should have
904  * @parent: a pointer to the parent dentry for this file.  This should be a
905  *          directory dentry if set.  If this parameter is %NULL, then the
906  *          file will be created in the root of the debugfs filesystem.
907  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
908  *        to the blob data and the size of the data.
909  *
910  * This function creates a file in debugfs with the given name that exports
911  * @blob->data as a binary blob. If the @mode variable is so set it can be
912  * read from. Writing is not supported.
913  *
914  * This function will return a pointer to a dentry if it succeeds.  This
915  * pointer must be passed to the debugfs_remove() function when the file is
916  * to be removed (no automatic cleanup happens if your module is unloaded,
917  * you are responsible here.)  If an error occurs, %NULL will be returned.
918  *
919  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
920  * returned.  It is not wise to check for this value, but rather, check for
921  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
922  * code.
923  */
924 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
925 				   struct dentry *parent,
926 				   struct debugfs_blob_wrapper *blob)
927 {
928 	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
929 }
930 EXPORT_SYMBOL_GPL(debugfs_create_blob);
931 
932 struct array_data {
933 	void *array;
934 	u32 elements;
935 };
936 
937 static size_t u32_format_array(char *buf, size_t bufsize,
938 			       u32 *array, int array_size)
939 {
940 	size_t ret = 0;
941 
942 	while (--array_size >= 0) {
943 		size_t len;
944 		char term = array_size ? ' ' : '\n';
945 
946 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
947 		ret += len;
948 
949 		buf += len;
950 		bufsize -= len;
951 	}
952 	return ret;
953 }
954 
955 static int u32_array_open(struct inode *inode, struct file *file)
956 {
957 	struct array_data *data = inode->i_private;
958 	int size, elements = data->elements;
959 	char *buf;
960 
961 	/*
962 	 * Max size:
963 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
964 	 *  - terminating NUL character
965 	 */
966 	size = elements*11;
967 	buf = kmalloc(size+1, GFP_KERNEL);
968 	if (!buf)
969 		return -ENOMEM;
970 	buf[size] = 0;
971 
972 	file->private_data = buf;
973 	u32_format_array(buf, size, data->array, data->elements);
974 
975 	return nonseekable_open(inode, file);
976 }
977 
978 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
979 			      loff_t *ppos)
980 {
981 	size_t size = strlen(file->private_data);
982 
983 	return simple_read_from_buffer(buf, len, ppos,
984 					file->private_data, size);
985 }
986 
987 static int u32_array_release(struct inode *inode, struct file *file)
988 {
989 	kfree(file->private_data);
990 
991 	return 0;
992 }
993 
994 static const struct file_operations u32_array_fops = {
995 	.owner	 = THIS_MODULE,
996 	.open	 = u32_array_open,
997 	.release = u32_array_release,
998 	.read	 = u32_array_read,
999 	.llseek  = no_llseek,
1000 };
1001 
1002 /**
1003  * debugfs_create_u32_array - create a debugfs file that is used to read u32
1004  * array.
1005  * @name: a pointer to a string containing the name of the file to create.
1006  * @mode: the permission that the file should have.
1007  * @parent: a pointer to the parent dentry for this file.  This should be a
1008  *          directory dentry if set.  If this parameter is %NULL, then the
1009  *          file will be created in the root of the debugfs filesystem.
1010  * @array: u32 array that provides data.
1011  * @elements: total number of elements in the array.
1012  *
1013  * This function creates a file in debugfs with the given name that exports
1014  * @array as data. If the @mode variable is so set it can be read from.
1015  * Writing is not supported. Seek within the file is also not supported.
1016  * Once array is created its size can not be changed.
1017  *
1018  * The function returns a pointer to dentry on success. If debugfs is not
1019  * enabled in the kernel, the value -%ENODEV will be returned.
1020  */
1021 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
1022 					    struct dentry *parent,
1023 					    u32 *array, u32 elements)
1024 {
1025 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
1026 
1027 	if (data == NULL)
1028 		return NULL;
1029 
1030 	data->array = array;
1031 	data->elements = elements;
1032 
1033 	return debugfs_create_file_unsafe(name, mode, parent, data,
1034 					&u32_array_fops);
1035 }
1036 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1037 
1038 #ifdef CONFIG_HAS_IOMEM
1039 
1040 /*
1041  * The regset32 stuff is used to print 32-bit registers using the
1042  * seq_file utilities. We offer printing a register set in an already-opened
1043  * sequential file or create a debugfs file that only prints a regset32.
1044  */
1045 
1046 /**
1047  * debugfs_print_regs32 - use seq_print to describe a set of registers
1048  * @s: the seq_file structure being used to generate output
1049  * @regs: an array if struct debugfs_reg32 structures
1050  * @nregs: the length of the above array
1051  * @base: the base address to be used in reading the registers
1052  * @prefix: a string to be prefixed to every output line
1053  *
1054  * This function outputs a text block describing the current values of
1055  * some 32-bit hardware registers. It is meant to be used within debugfs
1056  * files based on seq_file that need to show registers, intermixed with other
1057  * information. The prefix argument may be used to specify a leading string,
1058  * because some peripherals have several blocks of identical registers,
1059  * for example configuration of dma channels
1060  */
1061 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1062 			  int nregs, void __iomem *base, char *prefix)
1063 {
1064 	int i;
1065 
1066 	for (i = 0; i < nregs; i++, regs++) {
1067 		if (prefix)
1068 			seq_printf(s, "%s", prefix);
1069 		seq_printf(s, "%s = 0x%08x\n", regs->name,
1070 			   readl(base + regs->offset));
1071 		if (seq_has_overflowed(s))
1072 			break;
1073 	}
1074 }
1075 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1076 
1077 static int debugfs_show_regset32(struct seq_file *s, void *data)
1078 {
1079 	struct debugfs_regset32 *regset = s->private;
1080 
1081 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1082 	return 0;
1083 }
1084 
1085 static int debugfs_open_regset32(struct inode *inode, struct file *file)
1086 {
1087 	return single_open(file, debugfs_show_regset32, inode->i_private);
1088 }
1089 
1090 static const struct file_operations fops_regset32 = {
1091 	.open =		debugfs_open_regset32,
1092 	.read =		seq_read,
1093 	.llseek =	seq_lseek,
1094 	.release =	single_release,
1095 };
1096 
1097 /**
1098  * debugfs_create_regset32 - create a debugfs file that returns register values
1099  * @name: a pointer to a string containing the name of the file to create.
1100  * @mode: the permission that the file should have
1101  * @parent: a pointer to the parent dentry for this file.  This should be a
1102  *          directory dentry if set.  If this parameter is %NULL, then the
1103  *          file will be created in the root of the debugfs filesystem.
1104  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1105  *          to an array of register definitions, the array size and the base
1106  *          address where the register bank is to be found.
1107  *
1108  * This function creates a file in debugfs with the given name that reports
1109  * the names and values of a set of 32-bit registers. If the @mode variable
1110  * is so set it can be read from. Writing is not supported.
1111  *
1112  * This function will return a pointer to a dentry if it succeeds.  This
1113  * pointer must be passed to the debugfs_remove() function when the file is
1114  * to be removed (no automatic cleanup happens if your module is unloaded,
1115  * you are responsible here.)  If an error occurs, %NULL will be returned.
1116  *
1117  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1118  * returned.  It is not wise to check for this value, but rather, check for
1119  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1120  * code.
1121  */
1122 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
1123 				       struct dentry *parent,
1124 				       struct debugfs_regset32 *regset)
1125 {
1126 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1127 }
1128 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1129 
1130 #endif /* CONFIG_HAS_IOMEM */
1131 
1132 struct debugfs_devm_entry {
1133 	int (*read)(struct seq_file *seq, void *data);
1134 	struct device *dev;
1135 };
1136 
1137 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1138 {
1139 	struct debugfs_devm_entry *entry = inode->i_private;
1140 
1141 	return single_open(f, entry->read, entry->dev);
1142 }
1143 
1144 static const struct file_operations debugfs_devm_entry_ops = {
1145 	.owner = THIS_MODULE,
1146 	.open = debugfs_devm_entry_open,
1147 	.release = single_release,
1148 	.read = seq_read,
1149 	.llseek = seq_lseek
1150 };
1151 
1152 /**
1153  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1154  *
1155  * @dev: device related to this debugfs file.
1156  * @name: name of the debugfs file.
1157  * @parent: a pointer to the parent dentry for this file.  This should be a
1158  *	directory dentry if set.  If this parameter is %NULL, then the
1159  *	file will be created in the root of the debugfs filesystem.
1160  * @read_fn: function pointer called to print the seq_file content.
1161  */
1162 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1163 					   struct dentry *parent,
1164 					   int (*read_fn)(struct seq_file *s,
1165 							  void *data))
1166 {
1167 	struct debugfs_devm_entry *entry;
1168 
1169 	if (IS_ERR(parent))
1170 		return ERR_PTR(-ENOENT);
1171 
1172 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1173 	if (!entry)
1174 		return ERR_PTR(-ENOMEM);
1175 
1176 	entry->read = read_fn;
1177 	entry->dev = dev;
1178 
1179 	return debugfs_create_file(name, S_IRUGO, parent, entry,
1180 				   &debugfs_devm_entry_ops);
1181 }
1182 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1183 
1184