Skip to content

Commit 71cd3c6

Browse files
Avadhut Naikrafaeljw
authored andcommitted
fs: debugfs: Add write functionality to debugfs blobs
Currently, debugfs_create_blob() creates read-only debugfs binary blob files. In some cases, however, userspace tools need to write variable length data structures into predetermined memory addresses. An example is when injecting Vendor-defined error types through the einj module. In such cases, the functionality to write to these blob files in debugfs would be desired since the mapping aspect can be handled within the modules with userspace tools only needing to write into the blob files. Implement a write callback to enable writing to these blob files, created in debugfs, by owners only. Signed-off-by: Avadhut Naik <Avadhut.Naik@amd.com> Reviewed-by: Alexey Kardashevskiy <aik@amd.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Tony Luck <tony.luck@intel.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 709f3cb commit 71cd3c6

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

fs/debugfs/file.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,17 +1008,35 @@ static ssize_t read_file_blob(struct file *file, char __user *user_buf,
10081008
return r;
10091009
}
10101010

1011+
static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
1012+
size_t count, loff_t *ppos)
1013+
{
1014+
struct debugfs_blob_wrapper *blob = file->private_data;
1015+
struct dentry *dentry = F_DENTRY(file);
1016+
ssize_t r;
1017+
1018+
r = debugfs_file_get(dentry);
1019+
if (unlikely(r))
1020+
return r;
1021+
r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
1022+
count);
1023+
1024+
debugfs_file_put(dentry);
1025+
return r;
1026+
}
1027+
10111028
static const struct file_operations fops_blob = {
10121029
.read = read_file_blob,
1030+
.write = write_file_blob,
10131031
.open = simple_open,
10141032
.llseek = default_llseek,
10151033
};
10161034

10171035
/**
1018-
* debugfs_create_blob - create a debugfs file that is used to read a binary blob
1036+
* debugfs_create_blob - create a debugfs file that is used to read and write
1037+
* a binary blob
10191038
* @name: a pointer to a string containing the name of the file to create.
1020-
* @mode: the read permission that the file should have (other permissions are
1021-
* masked out)
1039+
* @mode: the permission that the file should have
10221040
* @parent: a pointer to the parent dentry for this file. This should be a
10231041
* directory dentry if set. If this parameter is %NULL, then the
10241042
* file will be created in the root of the debugfs filesystem.
@@ -1027,7 +1045,7 @@ static const struct file_operations fops_blob = {
10271045
*
10281046
* This function creates a file in debugfs with the given name that exports
10291047
* @blob->data as a binary blob. If the @mode variable is so set it can be
1030-
* read from. Writing is not supported.
1048+
* read from and written to.
10311049
*
10321050
* This function will return a pointer to a dentry if it succeeds. This
10331051
* pointer must be passed to the debugfs_remove() function when the file is
@@ -1042,7 +1060,7 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
10421060
struct dentry *parent,
10431061
struct debugfs_blob_wrapper *blob)
10441062
{
1045-
return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
1063+
return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
10461064
}
10471065
EXPORT_SYMBOL_GPL(debugfs_create_blob);
10481066

0 commit comments

Comments
 (0)