diff mbox series

[RFC,2/2] ext4: ioctl add EXT4_IOC_SUPERBLOCK_KEY_S_ERRORS

Message ID 20230802083402.515570-3-zhanchengbin1@huawei.com
State New
Headers show
Series ext4: Fix the conflict between modifying the superblock in user mode and kernel mode | expand

Commit Message

zhanchengbin Aug. 2, 2023, 8:34 a.m. UTC
Added modifications to s_errors in the modification framework of
superblock's attrs. There is no lock protection when the user mode
operates on the same block, so I modify the s_errors by the modification
framework of superblock's attrs.

The parameters passed in from the user mode will be checked by
sb_attr_errors_check first, if the check is passed, It will call
buffer_lock in ext4_update_superblocks_fn and use sb_attr_errors_set, so
it can ensure the atomicity of a modification.

Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
---
 fs/ext4/ioctl.c           | 22 ++++++++++++++++++++++
 include/uapi/linux/ext4.h |  4 +++-
 2 files changed, 25 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 76653d855073..81cb403bacf7 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -30,10 +30,32 @@ 
 typedef void ext4_update_sb_callback(struct ext4_super_block *es,
 				       const void *arg);
 
+int sb_attr_errors_check(struct ext4_sbattr *p_sbattr)
+{
+	__u16 error_behavior = *(__u16 *)(p_sbattr->sba_value);
+
+	if (p_sbattr->sba_len != EXT4_IOC_SUPERBLOCK_LEN_S_ERRORS)
+		return -EINVAL;
+
+	if (error_behavior < EXT4_ERRORS_CONTINUE ||
+	    error_behavior > EXT4_ERRORS_PANIC)
+		return -EINVAL;
+
+	return 0;
+}
+
+void sb_attr_errors_set(struct ext4_super_block *es, const void *arg)
+{
+	struct ext4_sbattr *p_sbattr = (struct ext4_sbattr *)arg;
+
+	es->s_errors = cpu_to_le16(*(__u16 *)p_sbattr->sba_value);
+}
+
 /*
  * Check and modify functions for each superblock variable
  */
 struct ext4_sbattr_operation ext4_sbattr_ops[] = {
+	{EXT4_IOC_SUPERBLOCK_KEY_S_ERRORS, sb_attr_errors_check, sb_attr_errors_set},
 	{EXT4_IOC_SUPERBLOCK_KEY_MAX, NULL, NULL},
 };
 
diff --git a/include/uapi/linux/ext4.h b/include/uapi/linux/ext4.h
index a9f33a0399e8..aad2de6528dd 100644
--- a/include/uapi/linux/ext4.h
+++ b/include/uapi/linux/ext4.h
@@ -90,8 +90,10 @@  struct ext4_sbattr {
 };
 
 enum ext4_ioc_superblock_key {
-	EXT4_IOC_SUPERBLOCK_KEY_MAX = 0,
+	EXT4_IOC_SUPERBLOCK_KEY_S_ERRORS = 0,
+	EXT4_IOC_SUPERBLOCK_KEY_MAX,
 };
+#define EXT4_IOC_SUPERBLOCK_LEN_S_ERRORS	2
 
 #define EXT4_SBATTR_MAX_COUNT	20