diff mbox series

[v2,2/2] btrfs: sysfs: use kstrtoull_suffix() to replace memparse()

Message ID 7e485fb6cafa66c5b9bb01970c152a499b3a7650.1703030510.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series lib/kstrtox: introduce kstrtoull_suffix() helper | expand

Commit Message

Qu Wenruo Dec. 20, 2023, 12:10 a.m. UTC
Since memparse() itself can not handle overflow at all, use
kstrtoull_suffix() to be extra safe.

This would introduce behavior changes to the following sysfs interfaces:

- /sys/fs/btrfs/<uuid>/allocation/(data|metadata|system)/chunk_size

- /sys/fs/btrfs/<uuid>/devinfo/<devid>/scrub_speed_max

The behavior changes would include:

- More overflow detection

- No support for "E" suffix anymore

- More strict tailing character requirement
  Now the only allowed tailing character is newline.
  Other characters including space are no longer supported.

  Although for regular "echo NUMBER >" usage it should be totally fine.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
Changelog:
v2:
- None
---
 fs/btrfs/sysfs.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 84c05246ffd8..089c3fc123fe 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -760,7 +760,7 @@  static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
 {
 	struct btrfs_space_info *space_info = to_space_info(kobj);
 	struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
-	char *retptr;
+	int ret;
 	u64 val;
 
 	if (!capable(CAP_SYS_ADMIN))
@@ -776,11 +776,9 @@  static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
 	if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
 		return -EPERM;
 
-	val = memparse(buf, &retptr);
-	/* There could be trailing '\n', also catch any typos after the value */
-	retptr = skip_spaces(retptr);
-	if (*retptr != 0 || val == 0)
-		return -EINVAL;
+	ret = kstrtoull_suffix(buf, 0, &val, KSTRTOULL_SUFFIX_DEFAULT);
+	if (ret < 0)
+		return ret;
 
 	val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
 
@@ -1779,14 +1777,12 @@  static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
 {
 	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
 						   devid_kobj);
-	char *endptr;
 	unsigned long long limit;
+	int ret;
 
-	limit = memparse(buf, &endptr);
-	/* There could be trailing '\n', also catch any typos after the value. */
-	endptr = skip_spaces(endptr);
-	if (*endptr != 0)
-		return -EINVAL;
+	ret = kstrtoull_suffix(buf, 0, &limit, KSTRTOULL_SUFFIX_DEFAULT);
+	if (ret < 0)
+		return ret;
 	WRITE_ONCE(device->scrub_speed_max, limit);
 	return len;
 }