[PATCH 2/9] sysfs: add growable flag to struct bin_attribute
From: Zev Weiss
Date: Wed Oct 06 2021 - 20:10:23 EST
Previously, sysfs_kf_bin_write() unconditionally disallowed writing
past the existing size of the file. In order to support mutable
device-tree status properties (which are bin_attributes), we need to
be able to write a longer value over a shorter existing one
(e.g. writing "reserved\n" over "okay\0"). bin_attributes that
require this can now set the growable flag to disable that checking
and allow arbitrary amounts of data to be written at arbitrary
offsets.
Signed-off-by: Zev Weiss <zev@xxxxxxxxxxxxxxxxx>
---
fs/sysfs/file.c | 2 +-
include/linux/sysfs.h | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index b2b85be95adf..156df003ea8f 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -147,7 +147,7 @@ static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
struct kobject *kobj = of->kn->parent->priv;
loff_t size = file_inode(of->file)->i_size;
- if (size) {
+ if (!battr->growable && size) {
if (size <= pos)
return -EFBIG;
count = min_t(ssize_t, count, size - pos);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 49de5189cf88..f8a56094c6c9 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -175,6 +175,7 @@ struct address_space;
struct bin_attribute {
struct attribute attr;
size_t size;
+ bool growable:1;
void *private;
struct address_space *(*f_mapping)(void);
ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
--
2.33.0