Skip to content

Commit 9af0440

Browse files
author
Peter Zijlstra
committed
debugfs: Implement debugfs_create_str()
Implement debugfs_create_str() to easily display names and such in debugfs. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Tested-by: Valentin Schneider <valentin.schneider@arm.com> Link: https://lkml.kernel.org/r/20210412102001.415407080@infradead.org
1 parent 1011dcc commit 9af0440

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎fs/debugfs/file.c‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,97 @@ struct dentry *debugfs_create_bool(const char *name, umode_t mode,
865865
}
866866
EXPORT_SYMBOL_GPL(debugfs_create_bool);
867867

868+
ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
869+
size_t count, loff_t *ppos)
870+
{
871+
struct dentry *dentry = F_DENTRY(file);
872+
char *str, *copy = NULL;
873+
int copy_len, len;
874+
ssize_t ret;
875+
876+
ret = debugfs_file_get(dentry);
877+
if (unlikely(ret))
878+
return ret;
879+
880+
str = *(char **)file->private_data;
881+
len = strlen(str) + 1;
882+
copy = kmalloc(len, GFP_KERNEL);
883+
if (!copy) {
884+
debugfs_file_put(dentry);
885+
return -ENOMEM;
886+
}
887+
888+
copy_len = strscpy(copy, str, len);
889+
debugfs_file_put(dentry);
890+
if (copy_len < 0) {
891+
kfree(copy);
892+
return copy_len;
893+
}
894+
895+
copy[copy_len] = '\n';
896+
897+
ret = simple_read_from_buffer(user_buf, count, ppos, copy, copy_len);
898+
kfree(copy);
899+
900+
return ret;
901+
}
902+
903+
static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
904+
size_t count, loff_t *ppos)
905+
{
906+
/* This is really only for read-only strings */
907+
return -EINVAL;
908+
}
909+
910+
static const struct file_operations fops_str = {
911+
.read = debugfs_read_file_str,
912+
.write = debugfs_write_file_str,
913+
.open = simple_open,
914+
.llseek = default_llseek,
915+
};
916+
917+
static const struct file_operations fops_str_ro = {
918+
.read = debugfs_read_file_str,
919+
.open = simple_open,
920+
.llseek = default_llseek,
921+
};
922+
923+
static const struct file_operations fops_str_wo = {
924+
.write = debugfs_write_file_str,
925+
.open = simple_open,
926+
.llseek = default_llseek,
927+
};
928+
929+
/**
930+
* debugfs_create_str - create a debugfs file that is used to read and write a string value
931+
* @name: a pointer to a string containing the name of the file to create.
932+
* @mode: the permission that the file should have
933+
* @parent: a pointer to the parent dentry for this file. This should be a
934+
* directory dentry if set. If this parameter is %NULL, then the
935+
* file will be created in the root of the debugfs filesystem.
936+
* @value: a pointer to the variable that the file should read to and write
937+
* from.
938+
*
939+
* This function creates a file in debugfs with the given name that
940+
* contains the value of the variable @value. If the @mode variable is so
941+
* set, it can be read from, and written to.
942+
*
943+
* This function will return a pointer to a dentry if it succeeds. This
944+
* pointer must be passed to the debugfs_remove() function when the file is
945+
* to be removed (no automatic cleanup happens if your module is unloaded,
946+
* you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
947+
* returned.
948+
*
949+
* If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
950+
* be returned.
951+
*/
952+
void debugfs_create_str(const char *name, umode_t mode,
953+
struct dentry *parent, char **value)
954+
{
955+
debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
956+
&fops_str_ro, &fops_str_wo);
957+
}
958+
868959
static ssize_t read_file_blob(struct file *file, char __user *user_buf,
869960
size_t count, loff_t *ppos)
870961
{

‎include/linux/debugfs.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ void debugfs_create_atomic_t(const char *name, umode_t mode,
128128
struct dentry *parent, atomic_t *value);
129129
struct dentry *debugfs_create_bool(const char *name, umode_t mode,
130130
struct dentry *parent, bool *value);
131+
void debugfs_create_str(const char *name, umode_t mode,
132+
struct dentry *parent, char **value);
131133

132134
struct dentry *debugfs_create_blob(const char *name, umode_t mode,
133135
struct dentry *parent,
@@ -156,6 +158,9 @@ ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
156158
ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
157159
size_t count, loff_t *ppos);
158160

161+
ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
162+
size_t count, loff_t *ppos);
163+
159164
#else
160165

161166
#include <linux/err.h>
@@ -297,6 +302,11 @@ static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
297302
return ERR_PTR(-ENODEV);
298303
}
299304

305+
static inline void debugfs_create_str(const char *name, umode_t mode,
306+
struct dentry *parent,
307+
char **value)
308+
{ }
309+
300310
static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
301311
struct dentry *parent,
302312
struct debugfs_blob_wrapper *blob)
@@ -348,6 +358,13 @@ static inline ssize_t debugfs_write_file_bool(struct file *file,
348358
return -ENODEV;
349359
}
350360

361+
static inline ssize_t debugfs_read_file_str(struct file *file,
362+
char __user *user_buf,
363+
size_t count, loff_t *ppos)
364+
{
365+
return -ENODEV;
366+
}
367+
351368
#endif
352369

353370
/**

0 commit comments

Comments
 (0)