xref: /linux-6.15/fs/debugfs/file.c (revision 4b6f5d20)
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/config.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/fs.h>
191da177e4SLinus Torvalds #include <linux/pagemap.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 {
361da177e4SLinus Torvalds 	if (inode->u.generic_ip)
371da177e4SLinus Torvalds 		file->private_data = inode->u.generic_ip;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	return 0;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
42*4b6f5d20SArjan 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 
48acaefc25SArnd Bergmann static void debugfs_u8_set(void *data, u64 val)
49acaefc25SArnd Bergmann {
50acaefc25SArnd Bergmann 	*(u8 *)data = val;
51acaefc25SArnd Bergmann }
52acaefc25SArnd Bergmann static u64 debugfs_u8_get(void *data)
53acaefc25SArnd Bergmann {
54acaefc25SArnd Bergmann 	return *(u8 *)data;
55acaefc25SArnd Bergmann }
56acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /**
59276e0c75SVincent Hanquez  * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write an unsigned 8 bit value.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
621da177e4SLinus Torvalds  * @mode: the permission that the file should have
631da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
641da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
651da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
661da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
671da177e4SLinus Torvalds  *         from.
681da177e4SLinus Torvalds  *
691da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
701da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
711da177e4SLinus Torvalds  * set, it can be read from, and written to.
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
741da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
751da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
761da177e4SLinus Torvalds  * you are responsible here.)  If an error occurs, NULL will be returned.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * If debugfs is not enabled in the kernel, the value -ENODEV will be
791da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
801da177e4SLinus Torvalds  * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
811da177e4SLinus Torvalds  * code.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode,
841da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u8);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
891da177e4SLinus Torvalds 
90acaefc25SArnd Bergmann static void debugfs_u16_set(void *data, u64 val)
91acaefc25SArnd Bergmann {
92acaefc25SArnd Bergmann 	*(u16 *)data = val;
93acaefc25SArnd Bergmann }
94acaefc25SArnd Bergmann static u64 debugfs_u16_get(void *data)
95acaefc25SArnd Bergmann {
96acaefc25SArnd Bergmann 	return *(u16 *)data;
97acaefc25SArnd Bergmann }
98acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
99acaefc25SArnd Bergmann 
1001da177e4SLinus Torvalds /**
101276e0c75SVincent Hanquez  * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write an unsigned 16 bit value.
1021da177e4SLinus Torvalds  *
1031da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1041da177e4SLinus Torvalds  * @mode: the permission that the file should have
1051da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1061da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
1071da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1081da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1091da177e4SLinus Torvalds  *         from.
1101da177e4SLinus Torvalds  *
1111da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1121da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1131da177e4SLinus Torvalds  * set, it can be read from, and written to.
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1161da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1171da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1181da177e4SLinus Torvalds  * you are responsible here.)  If an error occurs, NULL will be returned.
1191da177e4SLinus Torvalds  *
1201da177e4SLinus Torvalds  * If debugfs is not enabled in the kernel, the value -ENODEV will be
1211da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1221da177e4SLinus Torvalds  * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
1231da177e4SLinus Torvalds  * code.
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode,
1261da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u16);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
1311da177e4SLinus Torvalds 
132acaefc25SArnd Bergmann static void debugfs_u32_set(void *data, u64 val)
133acaefc25SArnd Bergmann {
134acaefc25SArnd Bergmann 	*(u32 *)data = val;
135acaefc25SArnd Bergmann }
136acaefc25SArnd Bergmann static u64 debugfs_u32_get(void *data)
137acaefc25SArnd Bergmann {
138acaefc25SArnd Bergmann 	return *(u32 *)data;
139acaefc25SArnd Bergmann }
140acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
141acaefc25SArnd Bergmann 
1421da177e4SLinus Torvalds /**
143276e0c75SVincent Hanquez  * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write an unsigned 32 bit value.
1441da177e4SLinus Torvalds  *
1451da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1461da177e4SLinus Torvalds  * @mode: the permission that the file should have
1471da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1481da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
1491da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1501da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1511da177e4SLinus Torvalds  *         from.
1521da177e4SLinus Torvalds  *
1531da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1541da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1551da177e4SLinus Torvalds  * set, it can be read from, and written to.
1561da177e4SLinus Torvalds  *
1571da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1581da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1591da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1601da177e4SLinus Torvalds  * you are responsible here.)  If an error occurs, NULL will be returned.
1611da177e4SLinus Torvalds  *
1621da177e4SLinus Torvalds  * If debugfs is not enabled in the kernel, the value -ENODEV will be
1631da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1641da177e4SLinus Torvalds  * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
1651da177e4SLinus Torvalds  * code.
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode,
1681da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u32);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf,
1751da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	char buf[3];
1781da177e4SLinus Torvalds 	u32 *val = file->private_data;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (*val)
1811da177e4SLinus Torvalds 		buf[0] = 'Y';
1821da177e4SLinus Torvalds 	else
1831da177e4SLinus Torvalds 		buf[0] = 'N';
1841da177e4SLinus Torvalds 	buf[1] = '\n';
1851da177e4SLinus Torvalds 	buf[2] = 0x00;
1861da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
1901da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	char buf[32];
1931da177e4SLinus Torvalds 	int buf_size;
1941da177e4SLinus Torvalds 	u32 *val = file->private_data;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
1971da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
1981da177e4SLinus Torvalds 		return -EFAULT;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	switch (buf[0]) {
2011da177e4SLinus Torvalds 	case 'y':
2021da177e4SLinus Torvalds 	case 'Y':
2031da177e4SLinus Torvalds 	case '1':
2041da177e4SLinus Torvalds 		*val = 1;
2051da177e4SLinus Torvalds 		break;
2061da177e4SLinus Torvalds 	case 'n':
2071da177e4SLinus Torvalds 	case 'N':
2081da177e4SLinus Torvalds 	case '0':
2091da177e4SLinus Torvalds 		*val = 0;
2101da177e4SLinus Torvalds 		break;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	return count;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
216*4b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
2171da177e4SLinus Torvalds 	.read =		read_file_bool,
2181da177e4SLinus Torvalds 	.write =	write_file_bool,
2191da177e4SLinus Torvalds 	.open =		default_open,
2201da177e4SLinus Torvalds };
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /**
2231da177e4SLinus Torvalds  * debugfs_create_bool - create a file in the debugfs filesystem that is used to read and write a boolean value.
2241da177e4SLinus Torvalds  *
2251da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
2261da177e4SLinus Torvalds  * @mode: the permission that the file should have
2271da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
2281da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
2291da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
2301da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
2311da177e4SLinus Torvalds  *         from.
2321da177e4SLinus Torvalds  *
2331da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
2341da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
2351da177e4SLinus Torvalds  * set, it can be read from, and written to.
2361da177e4SLinus Torvalds  *
2371da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
2381da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
2391da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
2401da177e4SLinus Torvalds  * you are responsible here.)  If an error occurs, NULL will be returned.
2411da177e4SLinus Torvalds  *
2421da177e4SLinus Torvalds  * If debugfs is not enabled in the kernel, the value -ENODEV will be
2431da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
2441da177e4SLinus Torvalds  * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
2451da177e4SLinus Torvalds  * code.
2461da177e4SLinus Torvalds  */
2471da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode,
2481da177e4SLinus Torvalds 				   struct dentry *parent, u32 *value)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
2531da177e4SLinus Torvalds 
254dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
255dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
256dd308bc3SMichael Ellerman {
257dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
258dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
259dd308bc3SMichael Ellerman 			blob->size);
260dd308bc3SMichael Ellerman }
261dd308bc3SMichael Ellerman 
262dd308bc3SMichael Ellerman static struct file_operations fops_blob = {
263dd308bc3SMichael Ellerman 	.read =		read_file_blob,
264dd308bc3SMichael Ellerman 	.open =		default_open,
265dd308bc3SMichael Ellerman };
266dd308bc3SMichael Ellerman 
267dd308bc3SMichael Ellerman /**
268dd308bc3SMichael Ellerman  * debugfs_create_blob - create a file in the debugfs filesystem that is
269dd308bc3SMichael Ellerman  * used to read and write a binary blob.
270dd308bc3SMichael Ellerman  *
271dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
272dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
273dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
274dd308bc3SMichael Ellerman  *          directory dentry if set.  If this paramater is NULL, then the
275dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
276dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
277dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
278dd308bc3SMichael Ellerman  *
279dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
280dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
281dd308bc3SMichael Ellerman  * read from. Writing is not supported.
282dd308bc3SMichael Ellerman  *
283dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
284dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
285dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
286dd308bc3SMichael Ellerman  * you are responsible here.)  If an error occurs, NULL will be returned.
287dd308bc3SMichael Ellerman  *
288dd308bc3SMichael Ellerman  * If debugfs is not enabled in the kernel, the value -ENODEV will be
289dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
290dd308bc3SMichael Ellerman  * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
291dd308bc3SMichael Ellerman  * code.
292dd308bc3SMichael Ellerman  */
293dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode,
294dd308bc3SMichael Ellerman 				   struct dentry *parent,
295dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
296dd308bc3SMichael Ellerman {
297dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
298dd308bc3SMichael Ellerman }
299dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
300