[1/6] ext4: sysfs: print ext4_super_block fields as little-endian

Message ID 20180620153322.54221-1-arnd@arndb.de
State Superseded, archived
Headers show
Series
  • [1/6] ext4: sysfs: print ext4_super_block fields as little-endian
Related show

Commit Message

Arnd Bergmann June 20, 2018, 3:32 p.m.
While working on extended rand for last_error/first_error timestamps,
I noticed that the endianess is wrong, we access the little-endian
fields in struct ext4_super_block as native-endian when we print them.

This adds a special case in ext4_attr_show() and ext4_attr_store()
to byteswap the superblock fields if needed.

In older kernels, this code was part of super.c, it got moved to sysfs.c
in linux-4.4.

Cc: stable@vger.kernel.org
Fixes: 52c198c6820f ("ext4: add sysfs entry showing whether the fs contains errors")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 fs/ext4/sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

Comments

Andreas Dilger June 21, 2018, 5:49 p.m. | #1
On Jun 20, 2018, at 9:32 AM, Arnd Bergmann <arnd@arndb.de> wrote:
> 
> While working on extended rand for last_error/first_error timestamps,
> I noticed that the endianess is wrong, we access the little-endian
> fields in struct ext4_super_block as native-endian when we print them.
> 
> This adds a special case in ext4_attr_show() and ext4_attr_store()
> to byteswap the superblock fields if needed.
> 
> In older kernels, this code was part of super.c, it got moved to sysfs.c
> in linux-4.4.
> 
> Cc: stable@vger.kernel.org
> Fixes: 52c198c6820f ("ext4: add sysfs entry showing whether the fs contains errors")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

I was wondering why this didn't just use le32_to_cpu() all the time,
but I see that these functions are being used for both ext4_super_block
(on-disk) fields, as well as ext4_sb_info (in-memory) fields.  A bit
ugly, but I don't think there is a better solution.

Reviewed-by: Andreas Dilger <adilger@dilger.ca>

> ---
> fs/ext4/sysfs.c | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
> index f34da0bb8f17..b970a200f20c 100644
> --- a/fs/ext4/sysfs.c
> +++ b/fs/ext4/sysfs.c
> @@ -274,8 +274,12 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
> 	case attr_pointer_ui:
> 		if (!ptr)
> 			return 0;
> -		return snprintf(buf, PAGE_SIZE, "%u\n",
> -				*((unsigned int *) ptr));
> +		if (a->attr_ptr == ptr_ext4_super_block_offset)
> +			return snprintf(buf, PAGE_SIZE, "%u\n",
> +					le32_to_cpup(ptr));
> +		else
> +			return snprintf(buf, PAGE_SIZE, "%u\n",
> +					*((unsigned int *) ptr));
> 	case attr_pointer_atomic:
> 		if (!ptr)
> 			return 0;
> @@ -308,7 +312,10 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
> 		ret = kstrtoul(skip_spaces(buf), 0, &t);
> 		if (ret)
> 			return ret;
> -		*((unsigned int *) ptr) = t;
> +		if (a->attr_ptr == ptr_ext4_super_block_offset)
> +			*((__le32 *) ptr) = cpu_to_le32(t);
> +		else
> +			*((unsigned int *) ptr) = t;
> 		return len;
> 	case attr_inode_readahead:
> 		return inode_readahead_blks_store(sbi, buf, len);
> --
> 2.9.0
> 


Cheers, Andreas
Arnd Bergmann June 21, 2018, 8:07 p.m. | #2
On Thu, Jun 21, 2018 at 7:49 PM, Andreas Dilger <adilger@dilger.ca> wrote:
> On Jun 20, 2018, at 9:32 AM, Arnd Bergmann <arnd@arndb.de> wrote:
>>
>> While working on extended rand for last_error/first_error timestamps,
>> I noticed that the endianess is wrong, we access the little-endian
>> fields in struct ext4_super_block as native-endian when we print them.
>>
>> This adds a special case in ext4_attr_show() and ext4_attr_store()
>> to byteswap the superblock fields if needed.
>>
>> In older kernels, this code was part of super.c, it got moved to sysfs.c
>> in linux-4.4.
>>
>> Cc: stable@vger.kernel.org
>> Fixes: 52c198c6820f ("ext4: add sysfs entry showing whether the fs contains errors")
>> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>
> I was wondering why this didn't just use le32_to_cpu() all the time,
> but I see that these functions are being used for both ext4_super_block
> (on-disk) fields, as well as ext4_sb_info (in-memory) fields.  A bit
> ugly, but I don't think there is a better solution.
>
> Reviewed-by: Andreas Dilger <adilger@dilger.ca>

One alternative that I considered was to just do away with helpers
for the ext4_super_block structure and only use them for ext4_sb_info,
especially after the last patch that changes this again. However,
as a bugfix for stable backports it seemed best to keep the change
as simple as possible.

Thanks for the review,

      Arnd

Patch

diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
index f34da0bb8f17..b970a200f20c 100644
--- a/fs/ext4/sysfs.c
+++ b/fs/ext4/sysfs.c
@@ -274,8 +274,12 @@  static ssize_t ext4_attr_show(struct kobject *kobj,
 	case attr_pointer_ui:
 		if (!ptr)
 			return 0;
-		return snprintf(buf, PAGE_SIZE, "%u\n",
-				*((unsigned int *) ptr));
+		if (a->attr_ptr == ptr_ext4_super_block_offset)
+			return snprintf(buf, PAGE_SIZE, "%u\n",
+					le32_to_cpup(ptr));
+		else
+			return snprintf(buf, PAGE_SIZE, "%u\n",
+					*((unsigned int *) ptr));
 	case attr_pointer_atomic:
 		if (!ptr)
 			return 0;
@@ -308,7 +312,10 @@  static ssize_t ext4_attr_store(struct kobject *kobj,
 		ret = kstrtoul(skip_spaces(buf), 0, &t);
 		if (ret)
 			return ret;
-		*((unsigned int *) ptr) = t;
+		if (a->attr_ptr == ptr_ext4_super_block_offset)
+			*((__le32 *) ptr) = cpu_to_le32(t);
+		else
+			*((unsigned int *) ptr) = t;
 		return len;
 	case attr_inode_readahead:
 		return inode_readahead_blks_store(sbi, buf, len);