xref: /linux-6.15/fs/debugfs/file.c (revision e6716b87)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
51da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
91da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
121da177e4SLinus Torvalds  *  See Documentation/DocBook/kernel-api for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
1966f54963SPeter Oberparleiter #include <linux/namei.h>
201da177e4SLinus Torvalds #include <linux/debugfs.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
231da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	return 0;
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
291da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	return count;
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file)
351da177e4SLinus Torvalds {
368e18e294STheodore Ts'o 	if (inode->i_private)
378e18e294STheodore Ts'o 		file->private_data = inode->i_private;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	return 0;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
424b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = {
431da177e4SLinus Torvalds 	.read =		default_read_file,
441da177e4SLinus Torvalds 	.write =	default_write_file,
451da177e4SLinus Torvalds 	.open =		default_open,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
4866f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
4966f54963SPeter Oberparleiter {
5066f54963SPeter Oberparleiter 	nd_set_link(nd, dentry->d_inode->i_private);
5166f54963SPeter Oberparleiter 	return NULL;
5266f54963SPeter Oberparleiter }
5366f54963SPeter Oberparleiter 
5466f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = {
5566f54963SPeter Oberparleiter 	.readlink       = generic_readlink,
5666f54963SPeter Oberparleiter 	.follow_link    = debugfs_follow_link,
5766f54963SPeter Oberparleiter };
5866f54963SPeter Oberparleiter 
59acaefc25SArnd Bergmann static void debugfs_u8_set(void *data, u64 val)
60acaefc25SArnd Bergmann {
61acaefc25SArnd Bergmann 	*(u8 *)data = val;
62acaefc25SArnd Bergmann }
63acaefc25SArnd Bergmann static u64 debugfs_u8_get(void *data)
64acaefc25SArnd Bergmann {
65acaefc25SArnd Bergmann 	return *(u8 *)data;
66acaefc25SArnd Bergmann }
67acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /**
706468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
711da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
721da177e4SLinus Torvalds  * @mode: the permission that the file should have
731da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
746468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
751da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
761da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
771da177e4SLinus Torvalds  *         from.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
801da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
811da177e4SLinus Torvalds  * set, it can be read from, and written to.
821da177e4SLinus Torvalds  *
831da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
841da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
851da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
866468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
871da177e4SLinus Torvalds  *
886468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
891da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
906468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
911da177e4SLinus Torvalds  * code.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode,
941da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u8);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
991da177e4SLinus Torvalds 
100acaefc25SArnd Bergmann static void debugfs_u16_set(void *data, u64 val)
101acaefc25SArnd Bergmann {
102acaefc25SArnd Bergmann 	*(u16 *)data = val;
103acaefc25SArnd Bergmann }
104acaefc25SArnd Bergmann static u64 debugfs_u16_get(void *data)
105acaefc25SArnd Bergmann {
106acaefc25SArnd Bergmann 	return *(u16 *)data;
107acaefc25SArnd Bergmann }
108acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
109acaefc25SArnd Bergmann 
1101da177e4SLinus Torvalds /**
1116468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1121da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1131da177e4SLinus Torvalds  * @mode: the permission that the file should have
1141da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1156468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1161da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1171da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1181da177e4SLinus Torvalds  *         from.
1191da177e4SLinus Torvalds  *
1201da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1211da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1221da177e4SLinus Torvalds  * set, it can be read from, and written to.
1231da177e4SLinus Torvalds  *
1241da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1251da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1261da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1276468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1281da177e4SLinus Torvalds  *
1296468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1301da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1316468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1321da177e4SLinus Torvalds  * code.
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode,
1351da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u16);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
1401da177e4SLinus Torvalds 
141acaefc25SArnd Bergmann static void debugfs_u32_set(void *data, u64 val)
142acaefc25SArnd Bergmann {
143acaefc25SArnd Bergmann 	*(u32 *)data = val;
144acaefc25SArnd Bergmann }
145acaefc25SArnd Bergmann static u64 debugfs_u32_get(void *data)
146acaefc25SArnd Bergmann {
147acaefc25SArnd Bergmann 	return *(u32 *)data;
148acaefc25SArnd Bergmann }
149acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
150acaefc25SArnd Bergmann 
1511da177e4SLinus Torvalds /**
1526468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1531da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1541da177e4SLinus Torvalds  * @mode: the permission that the file should have
1551da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1566468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1571da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1581da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1591da177e4SLinus Torvalds  *         from.
1601da177e4SLinus Torvalds  *
1611da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1621da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1631da177e4SLinus Torvalds  * set, it can be read from, and written to.
1641da177e4SLinus Torvalds  *
1651da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1661da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1671da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1686468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1691da177e4SLinus Torvalds  *
1706468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1711da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1726468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1731da177e4SLinus Torvalds  * code.
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode,
1761da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u32);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
1811da177e4SLinus Torvalds 
1828447891fSMichael Ellerman static void debugfs_u64_set(void *data, u64 val)
1838447891fSMichael Ellerman {
1848447891fSMichael Ellerman 	*(u64 *)data = val;
1858447891fSMichael Ellerman }
1868447891fSMichael Ellerman 
1878447891fSMichael Ellerman static u64 debugfs_u64_get(void *data)
1888447891fSMichael Ellerman {
1898447891fSMichael Ellerman 	return *(u64 *)data;
1908447891fSMichael Ellerman }
1918447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
1928447891fSMichael Ellerman 
1938447891fSMichael Ellerman /**
1948447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
1958447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
1968447891fSMichael Ellerman  * @mode: the permission that the file should have
1978447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
1988447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
1998447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
2008447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
2018447891fSMichael Ellerman  *         from.
2028447891fSMichael Ellerman  *
2038447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
2048447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
2058447891fSMichael Ellerman  * set, it can be read from, and written to.
2068447891fSMichael Ellerman  *
2078447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
2088447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
2098447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
2108447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
2118447891fSMichael Ellerman  *
2128447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
2138447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
2148447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2158447891fSMichael Ellerman  * code.
2168447891fSMichael Ellerman  */
2178447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode,
2188447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
2198447891fSMichael Ellerman {
2208447891fSMichael Ellerman 	return debugfs_create_file(name, mode, parent, value, &fops_u64);
2218447891fSMichael Ellerman }
2228447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
2238447891fSMichael Ellerman 
2242ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
2252ebefc50SRobin Getz 
2262ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
2272ebefc50SRobin Getz 
2282ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
2292ebefc50SRobin Getz 
230*e6716b87SRandy Dunlap /*
231*e6716b87SRandy Dunlap  * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
232*e6716b87SRandy Dunlap  *
233*e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
234*e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
235*e6716b87SRandy Dunlap  * decimal functions.
236*e6716b87SRandy Dunlap  */
237*e6716b87SRandy Dunlap 
2382ebefc50SRobin Getz /**
2392ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
240*e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
241*e6716b87SRandy Dunlap  * @mode: the permission that the file should have
242*e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
243*e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
244*e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
245*e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
246*e6716b87SRandy Dunlap  *         from.
2472ebefc50SRobin Getz  */
2482ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode,
2492ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
2502ebefc50SRobin Getz {
2512ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x8);
2522ebefc50SRobin Getz }
2532ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
2542ebefc50SRobin Getz 
255*e6716b87SRandy Dunlap /**
256*e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
257*e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
258*e6716b87SRandy Dunlap  * @mode: the permission that the file should have
259*e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
260*e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
261*e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
262*e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
263*e6716b87SRandy Dunlap  *         from.
264*e6716b87SRandy Dunlap  */
2652ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode,
2662ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
2672ebefc50SRobin Getz {
2682ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x16);
2692ebefc50SRobin Getz }
2702ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
2712ebefc50SRobin Getz 
272*e6716b87SRandy Dunlap /**
273*e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
274*e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
275*e6716b87SRandy Dunlap  * @mode: the permission that the file should have
276*e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
277*e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
278*e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
279*e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
280*e6716b87SRandy Dunlap  *         from.
281*e6716b87SRandy Dunlap  */
2822ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode,
2832ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
2842ebefc50SRobin Getz {
2852ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x32);
2862ebefc50SRobin Getz }
2872ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
2882ebefc50SRobin Getz 
2891da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf,
2901da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	char buf[3];
2931da177e4SLinus Torvalds 	u32 *val = file->private_data;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (*val)
2961da177e4SLinus Torvalds 		buf[0] = 'Y';
2971da177e4SLinus Torvalds 	else
2981da177e4SLinus Torvalds 		buf[0] = 'N';
2991da177e4SLinus Torvalds 	buf[1] = '\n';
3001da177e4SLinus Torvalds 	buf[2] = 0x00;
3011da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
3051da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	char buf[32];
3081da177e4SLinus Torvalds 	int buf_size;
3091da177e4SLinus Torvalds 	u32 *val = file->private_data;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
3121da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
3131da177e4SLinus Torvalds 		return -EFAULT;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	switch (buf[0]) {
3161da177e4SLinus Torvalds 	case 'y':
3171da177e4SLinus Torvalds 	case 'Y':
3181da177e4SLinus Torvalds 	case '1':
3191da177e4SLinus Torvalds 		*val = 1;
3201da177e4SLinus Torvalds 		break;
3211da177e4SLinus Torvalds 	case 'n':
3221da177e4SLinus Torvalds 	case 'N':
3231da177e4SLinus Torvalds 	case '0':
3241da177e4SLinus Torvalds 		*val = 0;
3251da177e4SLinus Torvalds 		break;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	return count;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3314b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
3321da177e4SLinus Torvalds 	.read =		read_file_bool,
3331da177e4SLinus Torvalds 	.write =	write_file_bool,
3341da177e4SLinus Torvalds 	.open =		default_open,
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /**
3386468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
3391da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3401da177e4SLinus Torvalds  * @mode: the permission that the file should have
3411da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3426468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3431da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3441da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3451da177e4SLinus Torvalds  *         from.
3461da177e4SLinus Torvalds  *
3471da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3481da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3491da177e4SLinus Torvalds  * set, it can be read from, and written to.
3501da177e4SLinus Torvalds  *
3511da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3521da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3531da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3546468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3551da177e4SLinus Torvalds  *
3566468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
3571da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
3586468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
3591da177e4SLinus Torvalds  * code.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode,
3621da177e4SLinus Torvalds 				   struct dentry *parent, u32 *value)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
3671da177e4SLinus Torvalds 
368dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
369dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
370dd308bc3SMichael Ellerman {
371dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
372dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
373dd308bc3SMichael Ellerman 			blob->size);
374dd308bc3SMichael Ellerman }
375dd308bc3SMichael Ellerman 
37600977a59SArjan van de Ven static const struct file_operations fops_blob = {
377dd308bc3SMichael Ellerman 	.read =		read_file_blob,
378dd308bc3SMichael Ellerman 	.open =		default_open,
379dd308bc3SMichael Ellerman };
380dd308bc3SMichael Ellerman 
381dd308bc3SMichael Ellerman /**
3826468b3afSRandy Dunlap  * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
383dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
384dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
385dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
3866468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
387dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
388dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
389dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
390dd308bc3SMichael Ellerman  *
391dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
392dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
393dd308bc3SMichael Ellerman  * read from. Writing is not supported.
394dd308bc3SMichael Ellerman  *
395dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
396dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
397dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
3986468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
399dd308bc3SMichael Ellerman  *
4006468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
401dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
4026468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
403dd308bc3SMichael Ellerman  * code.
404dd308bc3SMichael Ellerman  */
405dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode,
406dd308bc3SMichael Ellerman 				   struct dentry *parent,
407dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
408dd308bc3SMichael Ellerman {
409dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
410dd308bc3SMichael Ellerman }
411dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
412