Skip to content

Commit 86b5488

Browse files
Mike TiptonGeorgi Djakov
authored andcommitted
debugfs: Add write support to debugfs_create_str()
Currently, debugfs_create_str() only supports reading strings from debugfs. Add support for writing them as well. Based on original implementation by Peter Zijlstra [0]. Write support was present in the initial patch version, but dropped in v2 due to lack of users. We have a user now, so reintroduce it. [0] https://lore.kernel.org/all/YF3Hv5zXb%[email protected]/ Signed-off-by: Mike Tipton <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Georgi Djakov <[email protected]>
1 parent 2ccdd1b commit 86b5488

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

fs/debugfs/file.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,52 @@ EXPORT_SYMBOL_GPL(debugfs_create_str);
904904
static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
905905
size_t count, loff_t *ppos)
906906
{
907-
/* This is really only for read-only strings */
908-
return -EINVAL;
907+
struct dentry *dentry = F_DENTRY(file);
908+
char *old, *new = NULL;
909+
int pos = *ppos;
910+
int r;
911+
912+
r = debugfs_file_get(dentry);
913+
if (unlikely(r))
914+
return r;
915+
916+
old = *(char **)file->private_data;
917+
918+
/* only allow strict concatenation */
919+
r = -EINVAL;
920+
if (pos && pos != strlen(old))
921+
goto error;
922+
923+
r = -E2BIG;
924+
if (pos + count + 1 > PAGE_SIZE)
925+
goto error;
926+
927+
r = -ENOMEM;
928+
new = kmalloc(pos + count + 1, GFP_KERNEL);
929+
if (!new)
930+
goto error;
931+
932+
if (pos)
933+
memcpy(new, old, pos);
934+
935+
r = -EFAULT;
936+
if (copy_from_user(new + pos, user_buf, count))
937+
goto error;
938+
939+
new[pos + count] = '\0';
940+
strim(new);
941+
942+
rcu_assign_pointer(*(char **)file->private_data, new);
943+
synchronize_rcu();
944+
kfree(old);
945+
946+
debugfs_file_put(dentry);
947+
return count;
948+
949+
error:
950+
kfree(new);
951+
debugfs_file_put(dentry);
952+
return r;
909953
}
910954

911955
static const struct file_operations fops_str = {

0 commit comments

Comments
 (0)