xref: /linux-6.15/fs/debugfs/file.c (revision 57b31475)
13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
9e1b4fc7aSMauro Carvalho Chehab  *  See Documentation/filesystems/ for more details.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141a087c6aSAlessandro Rubini #include <linux/seq_file.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/debugfs.h>
1703e099fbSAlessandro Rubini #include <linux/io.h>
189fe2a701SSrivatsa Vaddagiri #include <linux/slab.h>
193a76e5e0SSeth Jennings #include <linux/atomic.h>
2098210b7fSArend van Spriel #include <linux/device.h>
2130332eeeSGeert Uytterhoeven #include <linux/pm_runtime.h>
22cfe39442SAl Viro #include <linux/poll.h>
235496197fSDavid Howells #include <linux/security.h>
249fd4dcecSNicolai Stange 
259fd4dcecSNicolai Stange #include "internal.h"
261da177e4SLinus Torvalds 
2749d200deSNicolai Stange struct poll_table_struct;
2849d200deSNicolai Stange 
default_read_file(struct file * file,char __user * buf,size_t count,loff_t * ppos)291da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
301da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
default_write_file(struct file * file,const char __user * buf,size_t count,loff_t * ppos)351da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
361da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	return count;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
419fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
421da177e4SLinus Torvalds 	.read =		default_read_file,
431da177e4SLinus Torvalds 	.write =	default_write_file,
44234e3405SStephen Boyd 	.open =		simple_open,
456038f373SArnd Bergmann 	.llseek =	noop_llseek,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
489fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
499fd4dcecSNicolai Stange 
debugfs_get_aux(const struct file * file)5012c92098SAl Viro const void *debugfs_get_aux(const struct file *file)
5112c92098SAl Viro {
5212c92098SAl Viro 	return DEBUGFS_I(file_inode(file))->aux;
5312c92098SAl Viro }
5412c92098SAl Viro EXPORT_SYMBOL_GPL(debugfs_get_aux);
5512c92098SAl Viro 
debugfs_real_fops(const struct file * filp)567c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
577c8d4698SNicolai Stange {
587c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
59055ab8e3SNicolai Stange 
6095688800SAl Viro 	if (!fsd) {
617d39bc50SNicolai Stange 		/*
627d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
637d39bc50SNicolai Stange 		 * debugfs_file_get().
647d39bc50SNicolai Stange 		 */
657d39bc50SNicolai Stange 		WARN_ON(1);
667d39bc50SNicolai Stange 		return NULL;
677d39bc50SNicolai Stange 	}
687d39bc50SNicolai Stange 
697c8d4698SNicolai Stange 	return fsd->real_fops;
707c8d4698SNicolai Stange }
717c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
727c8d4698SNicolai Stange 
73f8f25893SJohannes Berg enum dbgfs_get_mode {
74f8f25893SJohannes Berg 	DBGFS_GET_ALREADY,
75f8f25893SJohannes Berg 	DBGFS_GET_REGULAR,
76f8f25893SJohannes Berg 	DBGFS_GET_SHORT,
77f8f25893SJohannes Berg };
78f8f25893SJohannes Berg 
__debugfs_file_get(struct dentry * dentry,enum dbgfs_get_mode mode)79f8f25893SJohannes Berg static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode)
80e9117a5aSNicolai Stange {
817d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
827d39bc50SNicolai Stange 	void *d_fsd;
83e9117a5aSNicolai Stange 
840ed04a18SJohannes Berg 	/*
850ed04a18SJohannes Berg 	 * This could only happen if some debugfs user erroneously calls
860ed04a18SJohannes Berg 	 * debugfs_file_get() on a dentry that isn't even a file, let
870ed04a18SJohannes Berg 	 * them know about it.
880ed04a18SJohannes Berg 	 */
890ed04a18SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
900ed04a18SJohannes Berg 		return -EINVAL;
910ed04a18SJohannes Berg 
927d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
9395688800SAl Viro 	if (d_fsd) {
947d39bc50SNicolai Stange 		fsd = d_fsd;
957d39bc50SNicolai Stange 	} else {
9695688800SAl Viro 		struct inode *inode = dentry->d_inode;
97*57b31475SAl Viro 		unsigned int methods = 0;
9895688800SAl Viro 
99f8f25893SJohannes Berg 		if (WARN_ON(mode == DBGFS_GET_ALREADY))
100f8f25893SJohannes Berg 			return -EINVAL;
101f8f25893SJohannes Berg 
1027d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
1037d39bc50SNicolai Stange 		if (!fsd)
1047d39bc50SNicolai Stange 			return -ENOMEM;
1057d39bc50SNicolai Stange 
106f8f25893SJohannes Berg 		if (mode == DBGFS_GET_SHORT) {
10741a0ecc0SAl Viro 			const struct debugfs_short_fops *ops;
10895688800SAl Viro 			ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops;
10941a0ecc0SAl Viro 			if (ops->llseek)
110*57b31475SAl Viro 				methods |= HAS_LSEEK;
11141a0ecc0SAl Viro 			if (ops->read)
112*57b31475SAl Viro 				methods |= HAS_READ;
11341a0ecc0SAl Viro 			if (ops->write)
114*57b31475SAl Viro 				methods |= HAS_WRITE;
115*57b31475SAl Viro 			fsd->real_fops = NULL;
1168dc6d81cSJohannes Berg 		} else {
11741a0ecc0SAl Viro 			const struct file_operations *ops;
11895688800SAl Viro 			ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops;
11941a0ecc0SAl Viro 			if (ops->llseek)
120*57b31475SAl Viro 				methods |= HAS_LSEEK;
12141a0ecc0SAl Viro 			if (ops->read)
122*57b31475SAl Viro 				methods |= HAS_READ;
12341a0ecc0SAl Viro 			if (ops->write)
124*57b31475SAl Viro 				methods |= HAS_WRITE;
12541a0ecc0SAl Viro 			if (ops->unlocked_ioctl)
126*57b31475SAl Viro 				methods |= HAS_IOCTL;
12741a0ecc0SAl Viro 			if (ops->poll)
128*57b31475SAl Viro 				methods |= HAS_POLL;
129*57b31475SAl Viro 			fsd->short_fops = NULL;
1308dc6d81cSJohannes Berg 		}
131*57b31475SAl Viro 		fsd->methods = methods;
1327d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
1337d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
134159f5bdaSJohannes Berg 		INIT_LIST_HEAD(&fsd->cancellations);
135159f5bdaSJohannes Berg 		mutex_init(&fsd->cancellations_mtx);
136159f5bdaSJohannes Berg 
13795688800SAl Viro 		d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd);
13895688800SAl Viro 		if (d_fsd) {
139159f5bdaSJohannes Berg 			mutex_destroy(&fsd->cancellations_mtx);
1407d39bc50SNicolai Stange 			kfree(fsd);
14195688800SAl Viro 			fsd = d_fsd;
1427d39bc50SNicolai Stange 		}
1437d39bc50SNicolai Stange 	}
1447d39bc50SNicolai Stange 
1457d39bc50SNicolai Stange 	/*
1467d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1477d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1487d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1497d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1507d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1517d39bc50SNicolai Stange 	 * removers.
1527d39bc50SNicolai Stange 	 */
153e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
154e9117a5aSNicolai Stange 		return -EIO;
155e9117a5aSNicolai Stange 
156e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
157e9117a5aSNicolai Stange 		return -EIO;
158e9117a5aSNicolai Stange 
159e9117a5aSNicolai Stange 	return 0;
160e9117a5aSNicolai Stange }
161f8f25893SJohannes Berg 
162f8f25893SJohannes Berg /**
163f8f25893SJohannes Berg  * debugfs_file_get - mark the beginning of file data access
164f8f25893SJohannes Berg  * @dentry: the dentry object whose data is being accessed.
165f8f25893SJohannes Berg  *
166f8f25893SJohannes Berg  * Up to a matching call to debugfs_file_put(), any successive call
167f8f25893SJohannes Berg  * into the file removing functions debugfs_remove() and
168f8f25893SJohannes Berg  * debugfs_remove_recursive() will block. Since associated private
169f8f25893SJohannes Berg  * file data may only get freed after a successful return of any of
170f8f25893SJohannes Berg  * the removal functions, you may safely access it after a successful
171f8f25893SJohannes Berg  * call to debugfs_file_get() without worrying about lifetime issues.
172f8f25893SJohannes Berg  *
173f8f25893SJohannes Berg  * If -%EIO is returned, the file has already been removed and thus,
174f8f25893SJohannes Berg  * it is not safe to access any of its data. If, on the other hand,
175f8f25893SJohannes Berg  * it is allowed to access the file data, zero is returned.
176f8f25893SJohannes Berg  */
debugfs_file_get(struct dentry * dentry)177f8f25893SJohannes Berg int debugfs_file_get(struct dentry *dentry)
178f8f25893SJohannes Berg {
179f8f25893SJohannes Berg 	return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);
180f8f25893SJohannes Berg }
181e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
182e9117a5aSNicolai Stange 
183e9117a5aSNicolai Stange /**
184e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
185e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
186e9117a5aSNicolai Stange  *          debugfs_file_get().
187e9117a5aSNicolai Stange  *
188e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
189e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
190e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
191e9117a5aSNicolai Stange  */
debugfs_file_put(struct dentry * dentry)192e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
193e9117a5aSNicolai Stange {
1947d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
195e9117a5aSNicolai Stange 
196e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
197e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
198e9117a5aSNicolai Stange }
199e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
200e9117a5aSNicolai Stange 
2018c88a474SJohannes Berg /**
2028c88a474SJohannes Berg  * debugfs_enter_cancellation - enter a debugfs cancellation
2038c88a474SJohannes Berg  * @file: the file being accessed
2048c88a474SJohannes Berg  * @cancellation: the cancellation object, the cancel callback
2058c88a474SJohannes Berg  *	inside of it must be initialized
2068c88a474SJohannes Berg  *
2078c88a474SJohannes Berg  * When a debugfs file is removed it needs to wait for all active
2088c88a474SJohannes Berg  * operations to complete. However, the operation itself may need
2098c88a474SJohannes Berg  * to wait for hardware or completion of some asynchronous process
2108c88a474SJohannes Berg  * or similar. As such, it may need to be cancelled to avoid long
2118c88a474SJohannes Berg  * waits or even deadlocks.
2128c88a474SJohannes Berg  *
2138c88a474SJohannes Berg  * This function can be used inside a debugfs handler that may
2148c88a474SJohannes Berg  * need to be cancelled. As soon as this function is called, the
2158c88a474SJohannes Berg  * cancellation's 'cancel' callback may be called, at which point
2168c88a474SJohannes Berg  * the caller should proceed to call debugfs_leave_cancellation()
2178c88a474SJohannes Berg  * and leave the debugfs handler function as soon as possible.
2188c88a474SJohannes Berg  * Note that the 'cancel' callback is only ever called in the
2198c88a474SJohannes Berg  * context of some kind of debugfs_remove().
2208c88a474SJohannes Berg  *
2218c88a474SJohannes Berg  * This function must be paired with debugfs_leave_cancellation().
2228c88a474SJohannes Berg  */
debugfs_enter_cancellation(struct file * file,struct debugfs_cancellation * cancellation)2238c88a474SJohannes Berg void debugfs_enter_cancellation(struct file *file,
2248c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
2258c88a474SJohannes Berg {
2268c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
2278c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
2288c88a474SJohannes Berg 
2298c88a474SJohannes Berg 	INIT_LIST_HEAD(&cancellation->list);
2308c88a474SJohannes Berg 
2318c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
2328c88a474SJohannes Berg 		return;
2338c88a474SJohannes Berg 
2348c88a474SJohannes Berg 	if (WARN_ON(!cancellation->cancel))
2358c88a474SJohannes Berg 		return;
2368c88a474SJohannes Berg 
2378c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
23895688800SAl Viro 	if (WARN_ON(!fsd))
2398c88a474SJohannes Berg 		return;
2408c88a474SJohannes Berg 
2418c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2428c88a474SJohannes Berg 	list_add(&cancellation->list, &fsd->cancellations);
2438c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2448c88a474SJohannes Berg 
2458c88a474SJohannes Berg 	/* if we're already removing wake it up to cancel */
2468c88a474SJohannes Berg 	if (d_unlinked(dentry))
2478c88a474SJohannes Berg 		complete(&fsd->active_users_drained);
2488c88a474SJohannes Berg }
2498c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
2508c88a474SJohannes Berg 
2518c88a474SJohannes Berg /**
2528c88a474SJohannes Berg  * debugfs_leave_cancellation - leave cancellation section
2538c88a474SJohannes Berg  * @file: the file being accessed
2548c88a474SJohannes Berg  * @cancellation: the cancellation previously registered with
2558c88a474SJohannes Berg  *	debugfs_enter_cancellation()
2568c88a474SJohannes Berg  *
2578c88a474SJohannes Berg  * See the documentation of debugfs_enter_cancellation().
2588c88a474SJohannes Berg  */
debugfs_leave_cancellation(struct file * file,struct debugfs_cancellation * cancellation)2598c88a474SJohannes Berg void debugfs_leave_cancellation(struct file *file,
2608c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
2618c88a474SJohannes Berg {
2628c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
2638c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
2648c88a474SJohannes Berg 
2658c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
2668c88a474SJohannes Berg 		return;
2678c88a474SJohannes Berg 
2688c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
26995688800SAl Viro 	if (WARN_ON(!fsd))
2708c88a474SJohannes Berg 		return;
2718c88a474SJohannes Berg 
2728c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2738c88a474SJohannes Berg 	if (!list_empty(&cancellation->list))
2748c88a474SJohannes Berg 		list_del(&cancellation->list);
2758c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2768c88a474SJohannes Berg }
2778c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
2788c88a474SJohannes Berg 
2795496197fSDavid Howells /*
2805496197fSDavid Howells  * Only permit access to world-readable files when the kernel is locked down.
2815496197fSDavid Howells  * We also need to exclude any file that has ways to write or alter it as root
2825496197fSDavid Howells  * can bypass the permissions check.
2835496197fSDavid Howells  */
debugfs_locked_down(struct inode * inode,struct file * filp,const struct file_operations * real_fops)284a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode,
2855496197fSDavid Howells 			       struct file *filp,
2865496197fSDavid Howells 			       const struct file_operations *real_fops)
2875496197fSDavid Howells {
288358fcf5dSMichal Suchanek 	if ((inode->i_mode & 07777 & ~0444) == 0 &&
2895496197fSDavid Howells 	    !(filp->f_mode & FMODE_WRITE) &&
2908dc6d81cSJohannes Berg 	    (!real_fops ||
2918dc6d81cSJohannes Berg 	     (!real_fops->unlocked_ioctl &&
2925496197fSDavid Howells 	      !real_fops->compat_ioctl &&
2938dc6d81cSJohannes Berg 	      !real_fops->mmap)))
294a37f4958SEric Snowberg 		return 0;
2955496197fSDavid Howells 
296a37f4958SEric Snowberg 	if (security_locked_down(LOCKDOWN_DEBUGFS))
297a37f4958SEric Snowberg 		return -EPERM;
298a37f4958SEric Snowberg 
299a37f4958SEric Snowberg 	return 0;
3005496197fSDavid Howells }
3015496197fSDavid Howells 
open_proxy_open(struct inode * inode,struct file * filp)3029fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
3039fd4dcecSNicolai Stange {
30469d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
3059fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
3067d39bc50SNicolai Stange 	int r;
3079fd4dcecSNicolai Stange 
30867510d7eSJohannes Berg 	r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
3097d39bc50SNicolai Stange 	if (r)
3107d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
3119fd4dcecSNicolai Stange 
31286f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
3135496197fSDavid Howells 
314a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
3155496197fSDavid Howells 	if (r)
3165496197fSDavid Howells 		goto out;
3175496197fSDavid Howells 
318275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
319e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
320275678e7STaehee Yoo 		if (real_fops->owner &&
321112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
322112cedc8SSven Eckelmann 			r = -ENXIO;
323275678e7STaehee Yoo 			goto out;
324112cedc8SSven Eckelmann 		}
325275678e7STaehee Yoo #endif
326275678e7STaehee Yoo 
3279fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
3289fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
3299fd4dcecSNicolai Stange 			dentry);
3309fd4dcecSNicolai Stange 		r = -ENXIO;
3319fd4dcecSNicolai Stange 		goto out;
3329fd4dcecSNicolai Stange 	}
3339fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
3349fd4dcecSNicolai Stange 
3359fd4dcecSNicolai Stange 	if (real_fops->open)
3369fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
3379fd4dcecSNicolai Stange 
3389fd4dcecSNicolai Stange out:
33969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
3409fd4dcecSNicolai Stange 	return r;
3419fd4dcecSNicolai Stange }
3429fd4dcecSNicolai Stange 
3439fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
3449fd4dcecSNicolai Stange 	.open = open_proxy_open,
3459fd4dcecSNicolai Stange };
3469fd4dcecSNicolai Stange 
34749d200deSNicolai Stange #define PROTO(args...) args
34849d200deSNicolai Stange #define ARGS(args...) args
34949d200deSNicolai Stange 
35041a0ecc0SAl Viro #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret)	\
35149d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
35249d200deSNicolai Stange {									\
35369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);				\
35441a0ecc0SAl Viro 	struct debugfs_fsdata *fsd = dentry->d_fsdata;			\
355154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
35649d200deSNicolai Stange 	ret_type r;							\
35749d200deSNicolai Stange 									\
35841a0ecc0SAl Viro 	if (!(fsd->methods & bit))					\
35941a0ecc0SAl Viro 		return ret;						\
36069d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
36169d29f9eSNicolai Stange 	if (unlikely(r))						\
36269d29f9eSNicolai Stange 		return r;						\
363154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
36449d200deSNicolai Stange 	r = real_fops->name(args);					\
36569d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
36649d200deSNicolai Stange 	return r;							\
36749d200deSNicolai Stange }
36849d200deSNicolai Stange 
36941a0ecc0SAl Viro #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args, bit, ret)	\
3708dc6d81cSJohannes Berg static ret_type full_proxy_ ## name(proto)				\
3718dc6d81cSJohannes Berg {									\
3728dc6d81cSJohannes Berg 	struct dentry *dentry = F_DENTRY(filp);				\
37341a0ecc0SAl Viro 	struct debugfs_fsdata *fsd = dentry->d_fsdata;			\
3748dc6d81cSJohannes Berg 	ret_type r;							\
3758dc6d81cSJohannes Berg 									\
37641a0ecc0SAl Viro 	if (!(fsd->methods & bit))					\
37741a0ecc0SAl Viro 		return ret;						\
3788dc6d81cSJohannes Berg 	r = debugfs_file_get(dentry);					\
3798dc6d81cSJohannes Berg 	if (unlikely(r))						\
3808dc6d81cSJohannes Berg 		return r;						\
3818dc6d81cSJohannes Berg 	if (fsd->real_fops)						\
3828dc6d81cSJohannes Berg 		r = fsd->real_fops->name(args);				\
3838dc6d81cSJohannes Berg 	else								\
3848dc6d81cSJohannes Berg 		r = fsd->short_fops->name(args);			\
3858dc6d81cSJohannes Berg 	debugfs_file_put(dentry);					\
3868dc6d81cSJohannes Berg 	return r;							\
3878dc6d81cSJohannes Berg }
3888dc6d81cSJohannes Berg 
3898dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
39049d200deSNicolai Stange 		     PROTO(struct file *filp, loff_t offset, int whence),
39141a0ecc0SAl Viro 		     ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE);
39249d200deSNicolai Stange 
3938dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
39449d200deSNicolai Stange 		     PROTO(struct file *filp, char __user *buf, size_t size,
39549d200deSNicolai Stange 			   loff_t *ppos),
39641a0ecc0SAl Viro 		     ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL);
39749d200deSNicolai Stange 
3988dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(write, ssize_t, filp,
3998dc6d81cSJohannes Berg 		     PROTO(struct file *filp, const char __user *buf,
4008dc6d81cSJohannes Berg 			   size_t size, loff_t *ppos),
40141a0ecc0SAl Viro 		     ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL);
40249d200deSNicolai Stange 
40349d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
40449d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
40541a0ecc0SAl Viro 		ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY);
40649d200deSNicolai Stange 
full_proxy_poll(struct file * filp,struct poll_table_struct * wait)407076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
40849d200deSNicolai Stange 				struct poll_table_struct *wait)
40949d200deSNicolai Stange {
41069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
41141a0ecc0SAl Viro 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
412e6c8adcaSAl Viro 	__poll_t r = 0;
413154b9d75SNicolai Stange 	const struct file_operations *real_fops;
41449d200deSNicolai Stange 
41541a0ecc0SAl Viro 	if (!(fsd->methods & HAS_POLL))
41641a0ecc0SAl Viro 		return DEFAULT_POLLMASK;
41769d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
418a9a08845SLinus Torvalds 		return EPOLLHUP;
41949d200deSNicolai Stange 
420154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
42149d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
42269d29f9eSNicolai Stange 	debugfs_file_put(dentry);
42349d200deSNicolai Stange 	return r;
42449d200deSNicolai Stange }
42549d200deSNicolai Stange 
full_proxy_release(struct inode * inode,struct file * filp)42649d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
42749d200deSNicolai Stange {
42886f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
42949d200deSNicolai Stange 	int r = 0;
43049d200deSNicolai Stange 
43149d200deSNicolai Stange 	/*
43249d200deSNicolai Stange 	 * We must not protect this against removal races here: the
43349d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
43449d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
43549d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
43649d200deSNicolai Stange 	 */
43795688800SAl Viro 	if (real_fops->release)
43849d200deSNicolai Stange 		r = real_fops->release(inode, filp);
43949d200deSNicolai Stange 
44049d200deSNicolai Stange 	fops_put(real_fops);
441a1a9e5d2SEric Engestrom 	return r;
44249d200deSNicolai Stange }
44349d200deSNicolai Stange 
full_proxy_open_regular(struct inode * inode,struct file * filp)44495688800SAl Viro static int full_proxy_open_regular(struct inode *inode, struct file *filp)
44549d200deSNicolai Stange {
44669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
4478dc6d81cSJohannes Berg 	const struct file_operations *real_fops;
4488dc6d81cSJohannes Berg 	struct debugfs_fsdata *fsd;
4497d39bc50SNicolai Stange 	int r;
45049d200deSNicolai Stange 
45195688800SAl Viro 	r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
4527d39bc50SNicolai Stange 	if (r)
4537d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
45449d200deSNicolai Stange 
4558dc6d81cSJohannes Berg 	fsd = dentry->d_fsdata;
4568dc6d81cSJohannes Berg 	real_fops = fsd->real_fops;
457a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
4585496197fSDavid Howells 	if (r)
4595496197fSDavid Howells 		goto out;
4605496197fSDavid Howells 
46195688800SAl Viro 	if (!fops_get(real_fops)) {
462e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
463275678e7STaehee Yoo 		if (real_fops->owner &&
464112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
465112cedc8SSven Eckelmann 			r = -ENXIO;
466275678e7STaehee Yoo 			goto out;
467112cedc8SSven Eckelmann 		}
468275678e7STaehee Yoo #endif
469275678e7STaehee Yoo 
47049d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
47149d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
47249d200deSNicolai Stange 			dentry);
47349d200deSNicolai Stange 		r = -ENXIO;
47449d200deSNicolai Stange 		goto out;
47549d200deSNicolai Stange 	}
47649d200deSNicolai Stange 
47795688800SAl Viro 	if (real_fops->open) {
47849d200deSNicolai Stange 		r = real_fops->open(inode, filp);
479b10e3e90SNicolai Stange 		if (r) {
48041a0ecc0SAl Viro 			fops_put(real_fops);
48141a0ecc0SAl Viro 		} else if (filp->f_op != &debugfs_full_proxy_file_operations) {
48249d200deSNicolai Stange 			/* No protection against file removal anymore. */
48349d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
48449d200deSNicolai Stange 				dentry);
48549d200deSNicolai Stange 			fops_put(real_fops);
48641a0ecc0SAl Viro 		}
48741a0ecc0SAl Viro 	}
48849d200deSNicolai Stange out:
48969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
49049d200deSNicolai Stange 	return r;
49149d200deSNicolai Stange }
49249d200deSNicolai Stange 
49349d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
494f8f25893SJohannes Berg 	.open = full_proxy_open_regular,
49541a0ecc0SAl Viro 	.release = full_proxy_release,
49641a0ecc0SAl Viro 	.llseek = full_proxy_llseek,
49741a0ecc0SAl Viro 	.read = full_proxy_read,
49841a0ecc0SAl Viro 	.write = full_proxy_write,
49941a0ecc0SAl Viro 	.poll = full_proxy_poll,
50041a0ecc0SAl Viro 	.unlocked_ioctl = full_proxy_unlocked_ioctl
501f8f25893SJohannes Berg };
502f8f25893SJohannes Berg 
full_proxy_open_short(struct inode * inode,struct file * filp)503f8f25893SJohannes Berg static int full_proxy_open_short(struct inode *inode, struct file *filp)
504f8f25893SJohannes Berg {
50595688800SAl Viro 	struct dentry *dentry = F_DENTRY(filp);
50695688800SAl Viro 	int r;
50795688800SAl Viro 
50895688800SAl Viro 	r = __debugfs_file_get(dentry, DBGFS_GET_SHORT);
50995688800SAl Viro 	if (r)
51095688800SAl Viro 		return r == -EIO ? -ENOENT : r;
51195688800SAl Viro 	r = debugfs_locked_down(inode, filp, NULL);
51295688800SAl Viro 	if (!r)
51395688800SAl Viro 		r = simple_open(inode, filp);
51495688800SAl Viro 	debugfs_file_put(dentry);
51595688800SAl Viro 	return r;
516f8f25893SJohannes Berg }
517f8f25893SJohannes Berg 
518f8f25893SJohannes Berg const struct file_operations debugfs_full_short_proxy_file_operations = {
519f8f25893SJohannes Berg 	.open = full_proxy_open_short,
52041a0ecc0SAl Viro 	.llseek = full_proxy_llseek,
52141a0ecc0SAl Viro 	.read = full_proxy_read,
52241a0ecc0SAl Viro 	.write = full_proxy_write,
52349d200deSNicolai Stange };
52449d200deSNicolai Stange 
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)525c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
526c6468808SNicolai Stange 			size_t len, loff_t *ppos)
527c6468808SNicolai Stange {
52869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
529c6468808SNicolai Stange 	ssize_t ret;
530c6468808SNicolai Stange 
53169d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
53269d29f9eSNicolai Stange 	if (unlikely(ret))
53369d29f9eSNicolai Stange 		return ret;
534c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
53569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
536c6468808SNicolai Stange 	return ret;
537c6468808SNicolai Stange }
538c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
539c6468808SNicolai Stange 
debugfs_attr_write_xsigned(struct file * file,const char __user * buf,size_t len,loff_t * ppos,bool is_signed)540d472cf79SAkinobu Mita static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
541d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos, bool is_signed)
542c6468808SNicolai Stange {
54369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
544c6468808SNicolai Stange 	ssize_t ret;
545c6468808SNicolai Stange 
54669d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
54769d29f9eSNicolai Stange 	if (unlikely(ret))
54869d29f9eSNicolai Stange 		return ret;
549d472cf79SAkinobu Mita 	if (is_signed)
550d472cf79SAkinobu Mita 		ret = simple_attr_write_signed(file, buf, len, ppos);
551d472cf79SAkinobu Mita 	else
552c6468808SNicolai Stange 		ret = simple_attr_write(file, buf, len, ppos);
55369d29f9eSNicolai Stange 	debugfs_file_put(dentry);
554c6468808SNicolai Stange 	return ret;
555c6468808SNicolai Stange }
556d472cf79SAkinobu Mita 
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)557d472cf79SAkinobu Mita ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
558d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
559d472cf79SAkinobu Mita {
560d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
561d472cf79SAkinobu Mita }
562c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
563c6468808SNicolai Stange 
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)564d472cf79SAkinobu Mita ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
565d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
566d472cf79SAkinobu Mita {
567d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
568d472cf79SAkinobu Mita }
569d472cf79SAkinobu Mita EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
570d472cf79SAkinobu Mita 
debugfs_create_mode_unsafe(const char * name,umode_t mode,struct dentry * parent,void * value,const struct file_operations * fops,const struct file_operations * fops_ro,const struct file_operations * fops_wo)5714909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
5724909f168SNicolai Stange 					struct dentry *parent, void *value,
5734909f168SNicolai Stange 					const struct file_operations *fops,
5744909f168SNicolai Stange 					const struct file_operations *fops_ro,
5754909f168SNicolai Stange 					const struct file_operations *fops_wo)
5764909f168SNicolai Stange {
5774909f168SNicolai Stange 	/* if there are no write bits set, make read only */
5784909f168SNicolai Stange 	if (!(mode & S_IWUGO))
5794909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5804909f168SNicolai Stange 						fops_ro);
5814909f168SNicolai Stange 	/* if there are no read bits set, make write only */
5824909f168SNicolai Stange 	if (!(mode & S_IRUGO))
5834909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5844909f168SNicolai Stange 						fops_wo);
5854909f168SNicolai Stange 
5864909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
5874909f168SNicolai Stange }
5884909f168SNicolai Stange 
debugfs_u8_set(void * data,u64 val)5898b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
590acaefc25SArnd Bergmann {
591acaefc25SArnd Bergmann 	*(u8 *)data = val;
5928b88b099SChristoph Hellwig 	return 0;
593acaefc25SArnd Bergmann }
debugfs_u8_get(void * data,u64 * val)5948b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
595acaefc25SArnd Bergmann {
5968b88b099SChristoph Hellwig 	*val = *(u8 *)data;
5978b88b099SChristoph Hellwig 	return 0;
598acaefc25SArnd Bergmann }
5994909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
6014909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /**
6046468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
6051da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6061da177e4SLinus Torvalds  * @mode: the permission that the file should have
6071da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6086468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6091da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6101da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6111da177e4SLinus Torvalds  *         from.
6121da177e4SLinus Torvalds  *
6131da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6141da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6151da177e4SLinus Torvalds  * set, it can be read from, and written to.
6161da177e4SLinus Torvalds  */
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)6179655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
6189655ac4aSGreg Kroah-Hartman 		       u8 *value)
6191da177e4SLinus Torvalds {
6209655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
621b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
6241da177e4SLinus Torvalds 
debugfs_u16_set(void * data,u64 val)6258b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
626acaefc25SArnd Bergmann {
627acaefc25SArnd Bergmann 	*(u16 *)data = val;
6288b88b099SChristoph Hellwig 	return 0;
629acaefc25SArnd Bergmann }
debugfs_u16_get(void * data,u64 * val)6308b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
631acaefc25SArnd Bergmann {
6328b88b099SChristoph Hellwig 	*val = *(u16 *)data;
6338b88b099SChristoph Hellwig 	return 0;
634acaefc25SArnd Bergmann }
6354909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
6364909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
6374909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
638acaefc25SArnd Bergmann 
6391da177e4SLinus Torvalds /**
6406468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
6411da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6421da177e4SLinus Torvalds  * @mode: the permission that the file should have
6431da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6446468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6451da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6461da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6471da177e4SLinus Torvalds  *         from.
6481da177e4SLinus Torvalds  *
6491da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6501da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6511da177e4SLinus Torvalds  * set, it can be read from, and written to.
6521da177e4SLinus Torvalds  */
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)653313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
654313f5dbbSGreg Kroah-Hartman 			u16 *value)
6551da177e4SLinus Torvalds {
656313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
657b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
6601da177e4SLinus Torvalds 
debugfs_u32_set(void * data,u64 val)6618b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
662acaefc25SArnd Bergmann {
663acaefc25SArnd Bergmann 	*(u32 *)data = val;
6648b88b099SChristoph Hellwig 	return 0;
665acaefc25SArnd Bergmann }
debugfs_u32_get(void * data,u64 * val)6668b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
667acaefc25SArnd Bergmann {
6688b88b099SChristoph Hellwig 	*val = *(u32 *)data;
6698b88b099SChristoph Hellwig 	return 0;
670acaefc25SArnd Bergmann }
6714909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
6724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
6734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
674acaefc25SArnd Bergmann 
6751da177e4SLinus Torvalds /**
6766468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
6771da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6781da177e4SLinus Torvalds  * @mode: the permission that the file should have
6791da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6806468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6811da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6821da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6831da177e4SLinus Torvalds  *         from.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6861da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6871da177e4SLinus Torvalds  * set, it can be read from, and written to.
6881da177e4SLinus Torvalds  */
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)6892b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
6902b07021aSGreg Kroah-Hartman 			u32 *value)
6911da177e4SLinus Torvalds {
6922b07021aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
693b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
6961da177e4SLinus Torvalds 
debugfs_u64_set(void * data,u64 val)6978b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
6988447891fSMichael Ellerman {
6998447891fSMichael Ellerman 	*(u64 *)data = val;
7008b88b099SChristoph Hellwig 	return 0;
7018447891fSMichael Ellerman }
7028447891fSMichael Ellerman 
debugfs_u64_get(void * data,u64 * val)7038b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
7048447891fSMichael Ellerman {
7058b88b099SChristoph Hellwig 	*val = *(u64 *)data;
7068b88b099SChristoph Hellwig 	return 0;
7078447891fSMichael Ellerman }
7084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
7094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
7104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
7118447891fSMichael Ellerman 
7128447891fSMichael Ellerman /**
7138447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
7148447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
7158447891fSMichael Ellerman  * @mode: the permission that the file should have
7168447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
7178447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
7188447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
7198447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
7208447891fSMichael Ellerman  *         from.
7218447891fSMichael Ellerman  *
7228447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
7238447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
7248447891fSMichael Ellerman  * set, it can be read from, and written to.
7258447891fSMichael Ellerman  */
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)726ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
727ad26221fSGreg Kroah-Hartman 			u64 *value)
7288447891fSMichael Ellerman {
729ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
730b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
7318447891fSMichael Ellerman }
7328447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
7338447891fSMichael Ellerman 
debugfs_ulong_set(void * data,u64 val)734c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
735c23fe831SViresh Kumar {
736c23fe831SViresh Kumar 	*(unsigned long *)data = val;
737c23fe831SViresh Kumar 	return 0;
738c23fe831SViresh Kumar }
739c23fe831SViresh Kumar 
debugfs_ulong_get(void * data,u64 * val)740c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
741c23fe831SViresh Kumar {
742c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
743c23fe831SViresh Kumar 	return 0;
744c23fe831SViresh Kumar }
7454909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
7464909f168SNicolai Stange 			"%llu\n");
7474909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
7484909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
749c23fe831SViresh Kumar 
750c23fe831SViresh Kumar /**
751c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
752c23fe831SViresh Kumar  * an unsigned long value.
753c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
754c23fe831SViresh Kumar  * @mode: the permission that the file should have
755c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
756c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
757c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
758c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
759c23fe831SViresh Kumar  *         from.
760c23fe831SViresh Kumar  *
761c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
762c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
763c23fe831SViresh Kumar  * set, it can be read from, and written to.
764c23fe831SViresh Kumar  */
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)765fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
766fb05b14cSGreg Kroah-Hartman 			  unsigned long *value)
767c23fe831SViresh Kumar {
768fb05b14cSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
769fb05b14cSGreg Kroah-Hartman 				   &fops_ulong_ro, &fops_ulong_wo);
770c23fe831SViresh Kumar }
771c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
772c23fe831SViresh Kumar 
7734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
7744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
7754909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
7762ebefc50SRobin Getz 
7774909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
7784909f168SNicolai Stange 			"0x%04llx\n");
7794909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
7804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
7812ebefc50SRobin Getz 
7824909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
7834909f168SNicolai Stange 			"0x%08llx\n");
7844909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
7854909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
7862ebefc50SRobin Getz 
7874909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
7884909f168SNicolai Stange 			"0x%016llx\n");
7894909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
7904909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
79115b0beaaSHuang Ying 
792e6716b87SRandy Dunlap /*
79315b0beaaSHuang Ying  * 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
794e6716b87SRandy Dunlap  *
795e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
796e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
797e6716b87SRandy Dunlap  * decimal functions.
798e6716b87SRandy Dunlap  */
799e6716b87SRandy Dunlap 
8002ebefc50SRobin Getz /**
8012ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
802e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
803e6716b87SRandy Dunlap  * @mode: the permission that the file should have
804e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
805e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
806e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
807e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
808e6716b87SRandy Dunlap  *         from.
8092ebefc50SRobin Getz  */
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)810c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
811c7c11689SGreg Kroah-Hartman 		       u8 *value)
8122ebefc50SRobin Getz {
813c7c11689SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
814b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
8152ebefc50SRobin Getz }
8162ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
8172ebefc50SRobin Getz 
818e6716b87SRandy Dunlap /**
819e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
820e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
821e6716b87SRandy Dunlap  * @mode: the permission that the file should have
822e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
823e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
824e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
825e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
826e6716b87SRandy Dunlap  *         from.
827e6716b87SRandy Dunlap  */
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)828e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
829e40d38f2SGreg Kroah-Hartman 			u16 *value)
8302ebefc50SRobin Getz {
831e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
832b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
8332ebefc50SRobin Getz }
8342ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
8352ebefc50SRobin Getz 
836e6716b87SRandy Dunlap /**
837e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
838e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
839e6716b87SRandy Dunlap  * @mode: the permission that the file should have
840e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
841e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
842e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
843e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
844e6716b87SRandy Dunlap  *         from.
845e6716b87SRandy Dunlap  */
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)846f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
847f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
8482ebefc50SRobin Getz {
849f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
850b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
8512ebefc50SRobin Getz }
8522ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
8532ebefc50SRobin Getz 
85415b0beaaSHuang Ying /**
85515b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
85615b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
85715b0beaaSHuang Ying  * @mode: the permission that the file should have
85815b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
85915b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
86015b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
86115b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
86215b0beaaSHuang Ying  *         from.
86315b0beaaSHuang Ying  */
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)8640864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
8650864c408SGreg Kroah-Hartman 			u64 *value)
86615b0beaaSHuang Ying {
8670864c408SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
86882b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
86915b0beaaSHuang Ying }
87015b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
87115b0beaaSHuang Ying 
8725e078787SInaky Perez-Gonzalez 
debugfs_size_t_set(void * data,u64 val)8735e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
8745e078787SInaky Perez-Gonzalez {
8755e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
8765e078787SInaky Perez-Gonzalez 	return 0;
8775e078787SInaky Perez-Gonzalez }
debugfs_size_t_get(void * data,u64 * val)8785e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
8795e078787SInaky Perez-Gonzalez {
8805e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
8815e078787SInaky Perez-Gonzalez 	return 0;
8825e078787SInaky Perez-Gonzalez }
8834909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
8845e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
8854909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
8864909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
8875e078787SInaky Perez-Gonzalez 
8885e078787SInaky Perez-Gonzalez /**
8895e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
8905e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
8915e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
8925e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
8935e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
8945e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
8955e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
8965e078787SInaky Perez-Gonzalez  *         from.
8975e078787SInaky Perez-Gonzalez  */
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)8988e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
8995e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
9005e078787SInaky Perez-Gonzalez {
9018e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
9028e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
9035e078787SInaky Perez-Gonzalez }
9045e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
9055e078787SInaky Perez-Gonzalez 
debugfs_atomic_t_set(void * data,u64 val)9063a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
9073a76e5e0SSeth Jennings {
9083a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
9093a76e5e0SSeth Jennings 	return 0;
9103a76e5e0SSeth Jennings }
debugfs_atomic_t_get(void * data,u64 * val)9113a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
9123a76e5e0SSeth Jennings {
9133a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
9143a76e5e0SSeth Jennings 	return 0;
9153a76e5e0SSeth Jennings }
916d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
9173a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
918d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
9194909f168SNicolai Stange 			"%lld\n");
920d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
9214909f168SNicolai Stange 			"%lld\n");
9223a76e5e0SSeth Jennings 
9233a76e5e0SSeth Jennings /**
9243a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
9253a76e5e0SSeth Jennings  * write an atomic_t value
9263a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
9273a76e5e0SSeth Jennings  * @mode: the permission that the file should have
9283a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
9293a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
9303a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
9313a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
9323a76e5e0SSeth Jennings  *         from.
9333a76e5e0SSeth Jennings  */
debugfs_create_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)9349927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode,
9353a76e5e0SSeth Jennings 			     struct dentry *parent, atomic_t *value)
9363a76e5e0SSeth Jennings {
9379927c6faSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
9389927c6faSGreg Kroah-Hartman 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
9393a76e5e0SSeth Jennings }
9403a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
9415e078787SInaky Perez-Gonzalez 
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)9420642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
9431da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
9441da177e4SLinus Torvalds {
945c8a9c285SRasmus Villemoes 	char buf[2];
9464d45f797SNicolai Stange 	bool val;
94769d29f9eSNicolai Stange 	int r;
94869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9491da177e4SLinus Torvalds 
95069d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
95169d29f9eSNicolai Stange 	if (unlikely(r))
9524d45f797SNicolai Stange 		return r;
95369d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
95469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
9554d45f797SNicolai Stange 
9564d45f797SNicolai Stange 	if (val)
9571da177e4SLinus Torvalds 		buf[0] = 'Y';
9581da177e4SLinus Torvalds 	else
9591da177e4SLinus Torvalds 		buf[0] = 'N';
9601da177e4SLinus Torvalds 	buf[1] = '\n';
9611da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
9621da177e4SLinus Torvalds }
9630642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
9641da177e4SLinus Torvalds 
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)9650642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
9661da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9671da177e4SLinus Torvalds {
9688705b48eSJonathan Cameron 	bool bv;
96969d29f9eSNicolai Stange 	int r;
970621a5f7aSViresh Kumar 	bool *val = file->private_data;
97169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9721da177e4SLinus Torvalds 
973964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
974964f8363SAndy Shevchenko 	if (!r) {
97569d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
97669d29f9eSNicolai Stange 		if (unlikely(r))
9774d45f797SNicolai Stange 			return r;
97869d29f9eSNicolai Stange 		*val = bv;
97969d29f9eSNicolai Stange 		debugfs_file_put(dentry);
9804d45f797SNicolai Stange 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	return count;
9831da177e4SLinus Torvalds }
9840642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
9851da177e4SLinus Torvalds 
9864b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
9870642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
9880642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
989234e3405SStephen Boyd 	.open =		simple_open,
9906038f373SArnd Bergmann 	.llseek =	default_llseek,
9911da177e4SLinus Torvalds };
9921da177e4SLinus Torvalds 
9936713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
9946713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
9956713e8fbSStephen Boyd 	.open =		simple_open,
9966713e8fbSStephen Boyd 	.llseek =	default_llseek,
9976713e8fbSStephen Boyd };
9986713e8fbSStephen Boyd 
9996713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
10006713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
10016713e8fbSStephen Boyd 	.open =		simple_open,
10026713e8fbSStephen Boyd 	.llseek =	default_llseek,
10036713e8fbSStephen Boyd };
10046713e8fbSStephen Boyd 
10051da177e4SLinus Torvalds /**
10066468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
10071da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
10081da177e4SLinus Torvalds  * @mode: the permission that the file should have
10091da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
10106468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
10111da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
10121da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
10131da177e4SLinus Torvalds  *         from.
10141da177e4SLinus Torvalds  *
10151da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
10161da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
10171da177e4SLinus Torvalds  * set, it can be read from, and written to.
10181da177e4SLinus Torvalds  */
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)1019393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
1020393b0638SGreg Kroah-Hartman 			 bool *value)
10211da177e4SLinus Torvalds {
1022393b0638SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
10236713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
10261da177e4SLinus Torvalds 
debugfs_read_file_str(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)10279af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
10289af0440eSPeter Zijlstra 			      size_t count, loff_t *ppos)
10299af0440eSPeter Zijlstra {
10309af0440eSPeter Zijlstra 	struct dentry *dentry = F_DENTRY(file);
10319af0440eSPeter Zijlstra 	char *str, *copy = NULL;
10329af0440eSPeter Zijlstra 	int copy_len, len;
10339af0440eSPeter Zijlstra 	ssize_t ret;
10349af0440eSPeter Zijlstra 
10359af0440eSPeter Zijlstra 	ret = debugfs_file_get(dentry);
10369af0440eSPeter Zijlstra 	if (unlikely(ret))
10379af0440eSPeter Zijlstra 		return ret;
10389af0440eSPeter Zijlstra 
10399af0440eSPeter Zijlstra 	str = *(char **)file->private_data;
10409af0440eSPeter Zijlstra 	len = strlen(str) + 1;
10419af0440eSPeter Zijlstra 	copy = kmalloc(len, GFP_KERNEL);
10429af0440eSPeter Zijlstra 	if (!copy) {
10439af0440eSPeter Zijlstra 		debugfs_file_put(dentry);
10449af0440eSPeter Zijlstra 		return -ENOMEM;
10459af0440eSPeter Zijlstra 	}
10469af0440eSPeter Zijlstra 
10479af0440eSPeter Zijlstra 	copy_len = strscpy(copy, str, len);
10489af0440eSPeter Zijlstra 	debugfs_file_put(dentry);
10499af0440eSPeter Zijlstra 	if (copy_len < 0) {
10509af0440eSPeter Zijlstra 		kfree(copy);
10519af0440eSPeter Zijlstra 		return copy_len;
10529af0440eSPeter Zijlstra 	}
10539af0440eSPeter Zijlstra 
10549af0440eSPeter Zijlstra 	copy[copy_len] = '\n';
10559af0440eSPeter Zijlstra 
1056f501b6a2SDietmar Eggemann 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
10579af0440eSPeter Zijlstra 	kfree(copy);
10589af0440eSPeter Zijlstra 
10599af0440eSPeter Zijlstra 	return ret;
10609af0440eSPeter Zijlstra }
1061d60b59b9SCristian Marussi EXPORT_SYMBOL_GPL(debugfs_create_str);
10629af0440eSPeter Zijlstra 
debugfs_write_file_str(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)10639af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
10649af0440eSPeter Zijlstra 				      size_t count, loff_t *ppos)
10659af0440eSPeter Zijlstra {
106686b54881SMike Tipton 	struct dentry *dentry = F_DENTRY(file);
106786b54881SMike Tipton 	char *old, *new = NULL;
106886b54881SMike Tipton 	int pos = *ppos;
106986b54881SMike Tipton 	int r;
107086b54881SMike Tipton 
107186b54881SMike Tipton 	r = debugfs_file_get(dentry);
107286b54881SMike Tipton 	if (unlikely(r))
107386b54881SMike Tipton 		return r;
107486b54881SMike Tipton 
107586b54881SMike Tipton 	old = *(char **)file->private_data;
107686b54881SMike Tipton 
107786b54881SMike Tipton 	/* only allow strict concatenation */
107886b54881SMike Tipton 	r = -EINVAL;
107986b54881SMike Tipton 	if (pos && pos != strlen(old))
108086b54881SMike Tipton 		goto error;
108186b54881SMike Tipton 
108286b54881SMike Tipton 	r = -E2BIG;
108386b54881SMike Tipton 	if (pos + count + 1 > PAGE_SIZE)
108486b54881SMike Tipton 		goto error;
108586b54881SMike Tipton 
108686b54881SMike Tipton 	r = -ENOMEM;
108786b54881SMike Tipton 	new = kmalloc(pos + count + 1, GFP_KERNEL);
108886b54881SMike Tipton 	if (!new)
108986b54881SMike Tipton 		goto error;
109086b54881SMike Tipton 
109186b54881SMike Tipton 	if (pos)
109286b54881SMike Tipton 		memcpy(new, old, pos);
109386b54881SMike Tipton 
109486b54881SMike Tipton 	r = -EFAULT;
109586b54881SMike Tipton 	if (copy_from_user(new + pos, user_buf, count))
109686b54881SMike Tipton 		goto error;
109786b54881SMike Tipton 
109886b54881SMike Tipton 	new[pos + count] = '\0';
109986b54881SMike Tipton 	strim(new);
110086b54881SMike Tipton 
11017360a48bSMike Tipton 	rcu_assign_pointer(*(char __rcu **)file->private_data, new);
110286b54881SMike Tipton 	synchronize_rcu();
110386b54881SMike Tipton 	kfree(old);
110486b54881SMike Tipton 
110586b54881SMike Tipton 	debugfs_file_put(dentry);
110686b54881SMike Tipton 	return count;
110786b54881SMike Tipton 
110886b54881SMike Tipton error:
110986b54881SMike Tipton 	kfree(new);
111086b54881SMike Tipton 	debugfs_file_put(dentry);
111186b54881SMike Tipton 	return r;
11129af0440eSPeter Zijlstra }
11139af0440eSPeter Zijlstra 
11149af0440eSPeter Zijlstra static const struct file_operations fops_str = {
11159af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
11169af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
11179af0440eSPeter Zijlstra 	.open =		simple_open,
11189af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11199af0440eSPeter Zijlstra };
11209af0440eSPeter Zijlstra 
11219af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = {
11229af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
11239af0440eSPeter Zijlstra 	.open =		simple_open,
11249af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11259af0440eSPeter Zijlstra };
11269af0440eSPeter Zijlstra 
11279af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = {
11289af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
11299af0440eSPeter Zijlstra 	.open =		simple_open,
11309af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11319af0440eSPeter Zijlstra };
11329af0440eSPeter Zijlstra 
11339af0440eSPeter Zijlstra /**
11349af0440eSPeter Zijlstra  * debugfs_create_str - create a debugfs file that is used to read and write a string value
11359af0440eSPeter Zijlstra  * @name: a pointer to a string containing the name of the file to create.
11369af0440eSPeter Zijlstra  * @mode: the permission that the file should have
11379af0440eSPeter Zijlstra  * @parent: a pointer to the parent dentry for this file.  This should be a
11389af0440eSPeter Zijlstra  *          directory dentry if set.  If this parameter is %NULL, then the
11399af0440eSPeter Zijlstra  *          file will be created in the root of the debugfs filesystem.
11409af0440eSPeter Zijlstra  * @value: a pointer to the variable that the file should read to and write
11419af0440eSPeter Zijlstra  *         from.
11429af0440eSPeter Zijlstra  *
11439af0440eSPeter Zijlstra  * This function creates a file in debugfs with the given name that
11449af0440eSPeter Zijlstra  * contains the value of the variable @value.  If the @mode variable is so
11459af0440eSPeter Zijlstra  * set, it can be read from, and written to.
11469af0440eSPeter Zijlstra  */
debugfs_create_str(const char * name,umode_t mode,struct dentry * parent,char ** value)11479af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode,
11489af0440eSPeter Zijlstra 			struct dentry *parent, char **value)
11499af0440eSPeter Zijlstra {
11509af0440eSPeter Zijlstra 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
11519af0440eSPeter Zijlstra 				   &fops_str_ro, &fops_str_wo);
11529af0440eSPeter Zijlstra }
11539af0440eSPeter Zijlstra 
read_file_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1154dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1155dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
1156dd308bc3SMichael Ellerman {
1157dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
115869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
115983b711cbSNicolai Stange 	ssize_t r;
116083b711cbSNicolai Stange 
116169d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
116269d29f9eSNicolai Stange 	if (unlikely(r))
116369d29f9eSNicolai Stange 		return r;
116483b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1165dd308bc3SMichael Ellerman 				blob->size);
116669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
116783b711cbSNicolai Stange 	return r;
1168dd308bc3SMichael Ellerman }
1169dd308bc3SMichael Ellerman 
write_file_blob(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)117071cd3c63SAvadhut Naik static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
117171cd3c63SAvadhut Naik 			       size_t count, loff_t *ppos)
117271cd3c63SAvadhut Naik {
117371cd3c63SAvadhut Naik 	struct debugfs_blob_wrapper *blob = file->private_data;
117471cd3c63SAvadhut Naik 	struct dentry *dentry = F_DENTRY(file);
117571cd3c63SAvadhut Naik 	ssize_t r;
117671cd3c63SAvadhut Naik 
117771cd3c63SAvadhut Naik 	r = debugfs_file_get(dentry);
117871cd3c63SAvadhut Naik 	if (unlikely(r))
117971cd3c63SAvadhut Naik 		return r;
118071cd3c63SAvadhut Naik 	r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
118171cd3c63SAvadhut Naik 				   count);
118271cd3c63SAvadhut Naik 
118371cd3c63SAvadhut Naik 	debugfs_file_put(dentry);
118471cd3c63SAvadhut Naik 	return r;
118571cd3c63SAvadhut Naik }
118671cd3c63SAvadhut Naik 
118700977a59SArjan van de Ven static const struct file_operations fops_blob = {
1188dd308bc3SMichael Ellerman 	.read =		read_file_blob,
118971cd3c63SAvadhut Naik 	.write =	write_file_blob,
1190234e3405SStephen Boyd 	.open =		simple_open,
11916038f373SArnd Bergmann 	.llseek =	default_llseek,
1192dd308bc3SMichael Ellerman };
1193dd308bc3SMichael Ellerman 
1194dd308bc3SMichael Ellerman /**
119571cd3c63SAvadhut Naik  * debugfs_create_blob - create a debugfs file that is used to read and write
119671cd3c63SAvadhut Naik  * a binary blob
1197dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
119871cd3c63SAvadhut Naik  * @mode: the permission that the file should have
1199dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
12006468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1201dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
1202dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1203dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
1204dd308bc3SMichael Ellerman  *
1205dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
1206dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
120771cd3c63SAvadhut Naik  * read from and written to.
1208dd308bc3SMichael Ellerman  *
1209dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
1210dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
1211dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
1212adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
12139abb2499SRonald Tschalär  * returned.
1214dd308bc3SMichael Ellerman  *
1215adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
12169abb2499SRonald Tschalär  * be returned.
1217dd308bc3SMichael Ellerman  */
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)1218f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1219dd308bc3SMichael Ellerman 				   struct dentry *parent,
1220dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
1221dd308bc3SMichael Ellerman {
122271cd3c63SAvadhut Naik 	return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
1223dd308bc3SMichael Ellerman }
1224dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
12251a087c6aSAlessandro Rubini 
u32_format_array(char * buf,size_t bufsize,u32 * array,int array_size)1226e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
1227e05e279eSLinus Torvalds 			       u32 *array, int array_size)
12289fe2a701SSrivatsa Vaddagiri {
12299fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
12309fe2a701SSrivatsa Vaddagiri 
1231e05e279eSLinus Torvalds 	while (--array_size >= 0) {
12329fe2a701SSrivatsa Vaddagiri 		size_t len;
1233e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
12349fe2a701SSrivatsa Vaddagiri 
1235e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
12369fe2a701SSrivatsa Vaddagiri 		ret += len;
12379fe2a701SSrivatsa Vaddagiri 
12389fe2a701SSrivatsa Vaddagiri 		buf += len;
12399fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
12409fe2a701SSrivatsa Vaddagiri 	}
12419fe2a701SSrivatsa Vaddagiri 	return ret;
12429fe2a701SSrivatsa Vaddagiri }
12439fe2a701SSrivatsa Vaddagiri 
u32_array_open(struct inode * inode,struct file * file)124436048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
12459fe2a701SSrivatsa Vaddagiri {
1246a2b992c8SJakub Kicinski 	struct debugfs_u32_array *data = inode->i_private;
1247a2b992c8SJakub Kicinski 	int size, elements = data->n_elements;
1248e05e279eSLinus Torvalds 	char *buf;
12499fe2a701SSrivatsa Vaddagiri 
1250e05e279eSLinus Torvalds 	/*
1251e05e279eSLinus Torvalds 	 * Max size:
1252e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1253e05e279eSLinus Torvalds 	 *  - terminating NUL character
1254e05e279eSLinus Torvalds 	 */
1255e05e279eSLinus Torvalds 	size = elements*11;
1256e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
1257e05e279eSLinus Torvalds 	if (!buf)
125836048853SDavid Rientjes 		return -ENOMEM;
1259e05e279eSLinus Torvalds 	buf[size] = 0;
1260e05e279eSLinus Torvalds 
1261e05e279eSLinus Torvalds 	file->private_data = buf;
1262a2b992c8SJakub Kicinski 	u32_format_array(buf, size, data->array, data->n_elements);
1263e05e279eSLinus Torvalds 
126436048853SDavid Rientjes 	return nonseekable_open(inode, file);
12659fe2a701SSrivatsa Vaddagiri }
12669fe2a701SSrivatsa Vaddagiri 
u32_array_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)126736048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
126836048853SDavid Rientjes 			      loff_t *ppos)
126936048853SDavid Rientjes {
127036048853SDavid Rientjes 	size_t size = strlen(file->private_data);
12719fe2a701SSrivatsa Vaddagiri 
12729fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
12739fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
12749fe2a701SSrivatsa Vaddagiri }
12759fe2a701SSrivatsa Vaddagiri 
u32_array_release(struct inode * inode,struct file * file)12769fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
12779fe2a701SSrivatsa Vaddagiri {
12789fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
12799fe2a701SSrivatsa Vaddagiri 
12809fe2a701SSrivatsa Vaddagiri 	return 0;
12819fe2a701SSrivatsa Vaddagiri }
12829fe2a701SSrivatsa Vaddagiri 
12839fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
12849fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
12859fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
12869fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
12879fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
12889fe2a701SSrivatsa Vaddagiri };
12899fe2a701SSrivatsa Vaddagiri 
12909fe2a701SSrivatsa Vaddagiri /**
12919fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
12929fe2a701SSrivatsa Vaddagiri  * array.
12939fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
12949fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
12959fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
12969fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
12979fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
1298a2b992c8SJakub Kicinski  * @array: wrapper struct containing data pointer and size of the array.
12999fe2a701SSrivatsa Vaddagiri  *
13009fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
13019fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
13029fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
13039fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
13049fe2a701SSrivatsa Vaddagiri  */
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,struct debugfs_u32_array * array)1305c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1306a2b992c8SJakub Kicinski 			      struct dentry *parent,
1307a2b992c8SJakub Kicinski 			      struct debugfs_u32_array *array)
13089fe2a701SSrivatsa Vaddagiri {
1309a2b992c8SJakub Kicinski 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
13109fe2a701SSrivatsa Vaddagiri }
13119fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
13129fe2a701SSrivatsa Vaddagiri 
13133b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
13143b85e4abSHeiko Carstens 
13151a087c6aSAlessandro Rubini /*
13161a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
13171a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
13181a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
13191a087c6aSAlessandro Rubini  */
13201a087c6aSAlessandro Rubini 
13211a087c6aSAlessandro Rubini /**
13221a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
13231a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
13241a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1325b5763accSRandy Dunlap  * @nregs: the length of the above array
13261a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
13271a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
13281a087c6aSAlessandro Rubini  *
13291a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
13301a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
13311a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
13321a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
13331a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
13341a087c6aSAlessandro Rubini  * for example configuration of dma channels
13351a087c6aSAlessandro Rubini  */
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)13369761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
13371a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
13381a087c6aSAlessandro Rubini {
13399761536eSJoe Perches 	int i;
13401a087c6aSAlessandro Rubini 
13411a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
13421a087c6aSAlessandro Rubini 		if (prefix)
13439761536eSJoe Perches 			seq_printf(s, "%s", prefix);
13449761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
13455858441cSDan Carpenter 			   readl(base + regs->offset));
13469761536eSJoe Perches 		if (seq_has_overflowed(s))
13479761536eSJoe Perches 			break;
13481a087c6aSAlessandro Rubini 	}
13491a087c6aSAlessandro Rubini }
13501a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
13511a087c6aSAlessandro Rubini 
debugfs_regset32_show(struct seq_file * s,void * data)135219029f3fSChenXiaoSong static int debugfs_regset32_show(struct seq_file *s, void *data)
13531a087c6aSAlessandro Rubini {
13541a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
13551a087c6aSAlessandro Rubini 
135630332eeeSGeert Uytterhoeven 	if (regset->dev)
135730332eeeSGeert Uytterhoeven 		pm_runtime_get_sync(regset->dev);
135830332eeeSGeert Uytterhoeven 
13591a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
136030332eeeSGeert Uytterhoeven 
136130332eeeSGeert Uytterhoeven 	if (regset->dev)
136230332eeeSGeert Uytterhoeven 		pm_runtime_put(regset->dev);
136330332eeeSGeert Uytterhoeven 
13641a087c6aSAlessandro Rubini 	return 0;
13651a087c6aSAlessandro Rubini }
13661a087c6aSAlessandro Rubini 
136719029f3fSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
13681a087c6aSAlessandro Rubini 
13691a087c6aSAlessandro Rubini /**
13701a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
13711a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
13721a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
13731a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
13741a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
13751a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
13761a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
13771a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
13781a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
13791a087c6aSAlessandro Rubini  *
13801a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
13811a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
13821a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
13831a087c6aSAlessandro Rubini  */
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)1384ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode,
13851a087c6aSAlessandro Rubini 			     struct dentry *parent,
13861a087c6aSAlessandro Rubini 			     struct debugfs_regset32 *regset)
13871a087c6aSAlessandro Rubini {
138819029f3fSChenXiaoSong 	debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
13891a087c6aSAlessandro Rubini }
13901a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
13913b85e4abSHeiko Carstens 
13923b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
139398210b7fSArend van Spriel 
139498210b7fSArend van Spriel struct debugfs_devm_entry {
139598210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
139698210b7fSArend van Spriel 	struct device *dev;
139798210b7fSArend van Spriel };
139898210b7fSArend van Spriel 
debugfs_devm_entry_open(struct inode * inode,struct file * f)139998210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
140098210b7fSArend van Spriel {
140198210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
140298210b7fSArend van Spriel 
140398210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
140498210b7fSArend van Spriel }
140598210b7fSArend van Spriel 
140698210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
140798210b7fSArend van Spriel 	.owner = THIS_MODULE,
140898210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
140998210b7fSArend van Spriel 	.release = single_release,
141098210b7fSArend van Spriel 	.read = seq_read,
141198210b7fSArend van Spriel 	.llseek = seq_lseek
141298210b7fSArend van Spriel };
141398210b7fSArend van Spriel 
141498210b7fSArend van Spriel /**
141598210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
141698210b7fSArend van Spriel  *
141798210b7fSArend van Spriel  * @dev: device related to this debugfs file.
141898210b7fSArend van Spriel  * @name: name of the debugfs file.
141998210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
142098210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
142198210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
142298210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
142398210b7fSArend van Spriel  */
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))14240d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name,
142598210b7fSArend van Spriel 				 struct dentry *parent,
14260d519cbfSGreg Kroah-Hartman 				 int (*read_fn)(struct seq_file *s, void *data))
142798210b7fSArend van Spriel {
142898210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
142998210b7fSArend van Spriel 
143098210b7fSArend van Spriel 	if (IS_ERR(parent))
14310d519cbfSGreg Kroah-Hartman 		return;
143298210b7fSArend van Spriel 
143398210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
143498210b7fSArend van Spriel 	if (!entry)
14350d519cbfSGreg Kroah-Hartman 		return;
143698210b7fSArend van Spriel 
143798210b7fSArend van Spriel 	entry->read = read_fn;
143898210b7fSArend van Spriel 	entry->dev = dev;
143998210b7fSArend van Spriel 
14400d519cbfSGreg Kroah-Hartman 	debugfs_create_file(name, S_IRUGO, parent, entry,
144198210b7fSArend van Spriel 			    &debugfs_devm_entry_ops);
144298210b7fSArend van Spriel }
144398210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1444