xref: /linux-6.15/fs/debugfs/file.c (revision 66f54963)
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>
19*66f54963SPeter 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 
48*66f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
49*66f54963SPeter Oberparleiter {
50*66f54963SPeter Oberparleiter 	nd_set_link(nd, dentry->d_inode->i_private);
51*66f54963SPeter Oberparleiter 	return NULL;
52*66f54963SPeter Oberparleiter }
53*66f54963SPeter Oberparleiter 
54*66f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = {
55*66f54963SPeter Oberparleiter 	.readlink       = generic_readlink,
56*66f54963SPeter Oberparleiter 	.follow_link    = debugfs_follow_link,
57*66f54963SPeter Oberparleiter };
58*66f54963SPeter 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 
1821da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf,
1831da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	char buf[3];
1861da177e4SLinus Torvalds 	u32 *val = file->private_data;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (*val)
1891da177e4SLinus Torvalds 		buf[0] = 'Y';
1901da177e4SLinus Torvalds 	else
1911da177e4SLinus Torvalds 		buf[0] = 'N';
1921da177e4SLinus Torvalds 	buf[1] = '\n';
1931da177e4SLinus Torvalds 	buf[2] = 0x00;
1941da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
1981da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	char buf[32];
2011da177e4SLinus Torvalds 	int buf_size;
2021da177e4SLinus Torvalds 	u32 *val = file->private_data;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
2051da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
2061da177e4SLinus Torvalds 		return -EFAULT;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	switch (buf[0]) {
2091da177e4SLinus Torvalds 	case 'y':
2101da177e4SLinus Torvalds 	case 'Y':
2111da177e4SLinus Torvalds 	case '1':
2121da177e4SLinus Torvalds 		*val = 1;
2131da177e4SLinus Torvalds 		break;
2141da177e4SLinus Torvalds 	case 'n':
2151da177e4SLinus Torvalds 	case 'N':
2161da177e4SLinus Torvalds 	case '0':
2171da177e4SLinus Torvalds 		*val = 0;
2181da177e4SLinus Torvalds 		break;
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	return count;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2244b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
2251da177e4SLinus Torvalds 	.read =		read_file_bool,
2261da177e4SLinus Torvalds 	.write =	write_file_bool,
2271da177e4SLinus Torvalds 	.open =		default_open,
2281da177e4SLinus Torvalds };
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /**
2316468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
2321da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
2331da177e4SLinus Torvalds  * @mode: the permission that the file should have
2341da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
2356468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
2361da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
2371da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
2381da177e4SLinus Torvalds  *         from.
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
2411da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
2421da177e4SLinus Torvalds  * set, it can be read from, and written to.
2431da177e4SLinus Torvalds  *
2441da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
2451da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
2461da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
2476468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
2481da177e4SLinus Torvalds  *
2496468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
2501da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
2516468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2521da177e4SLinus Torvalds  * code.
2531da177e4SLinus Torvalds  */
2541da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode,
2551da177e4SLinus Torvalds 				   struct dentry *parent, u32 *value)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
2601da177e4SLinus Torvalds 
261dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
262dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
263dd308bc3SMichael Ellerman {
264dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
265dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
266dd308bc3SMichael Ellerman 			blob->size);
267dd308bc3SMichael Ellerman }
268dd308bc3SMichael Ellerman 
26900977a59SArjan van de Ven static const struct file_operations fops_blob = {
270dd308bc3SMichael Ellerman 	.read =		read_file_blob,
271dd308bc3SMichael Ellerman 	.open =		default_open,
272dd308bc3SMichael Ellerman };
273dd308bc3SMichael Ellerman 
274dd308bc3SMichael Ellerman /**
2756468b3afSRandy Dunlap  * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
276dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
277dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
278dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
2796468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
280dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
281dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
282dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
283dd308bc3SMichael Ellerman  *
284dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
285dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
286dd308bc3SMichael Ellerman  * read from. Writing is not supported.
287dd308bc3SMichael Ellerman  *
288dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
289dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
290dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
2916468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
292dd308bc3SMichael Ellerman  *
2936468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
294dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
2956468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
296dd308bc3SMichael Ellerman  * code.
297dd308bc3SMichael Ellerman  */
298dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode,
299dd308bc3SMichael Ellerman 				   struct dentry *parent,
300dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
301dd308bc3SMichael Ellerman {
302dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
303dd308bc3SMichael Ellerman }
304dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
305