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