add patch improve-code-readability-in-ext4_iget
[ext4-patch-queue.git] / print-ext4_super_block-fields-as-little-endian
blobdae65691037342d7a522299a39c0043563da2b64
1 ext4: sysfs: print ext4_super_block fields as little-endian
3 From: Arnd Bergmann <arnd@arndb.de>
5 While working on extended rand for last_error/first_error timestamps,
6 I noticed that the endianess is wrong; we access the little-endian
7 fields in struct ext4_super_block as native-endian when we print them.
9 This adds a special case in ext4_attr_show() and ext4_attr_store()
10 to byteswap the superblock fields if needed.
12 In older kernels, this code was part of super.c, it got moved to
13 sysfs.c in linux-4.4.
15 Cc: stable@vger.kernel.org
16 Fixes: 52c198c6820f ("ext4: add sysfs entry showing whether the fs contains errors")
17 Reviewed-by: Andreas Dilger <adilger@dilger.ca>
18 Signed-off-by: Arnd Bergmann <arnd@arndb.de>
19 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
20 ---
21  fs/ext4/sysfs.c | 13 ++++++++++---
22  1 file changed, 10 insertions(+), 3 deletions(-)
24 diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
25 index f34da0bb8f17..b970a200f20c 100644
26 --- a/fs/ext4/sysfs.c
27 +++ b/fs/ext4/sysfs.c
28 @@ -274,8 +274,12 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
29         case attr_pointer_ui:
30                 if (!ptr)
31                         return 0;
32 -               return snprintf(buf, PAGE_SIZE, "%u\n",
33 -                               *((unsigned int *) ptr));
34 +               if (a->attr_ptr == ptr_ext4_super_block_offset)
35 +                       return snprintf(buf, PAGE_SIZE, "%u\n",
36 +                                       le32_to_cpup(ptr));
37 +               else
38 +                       return snprintf(buf, PAGE_SIZE, "%u\n",
39 +                                       *((unsigned int *) ptr));
40         case attr_pointer_atomic:
41                 if (!ptr)
42                         return 0;
43 @@ -308,7 +312,10 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
44                 ret = kstrtoul(skip_spaces(buf), 0, &t);
45                 if (ret)
46                         return ret;
47 -               *((unsigned int *) ptr) = t;
48 +               if (a->attr_ptr == ptr_ext4_super_block_offset)
49 +                       *((__le32 *) ptr) = cpu_to_le32(t);
50 +               else
51 +                       *((unsigned int *) ptr) = t;
52                 return len;
53         case attr_inode_readahead:
54                 return inode_readahead_blks_store(sbi, buf, len);
55 -- 
56 2.9.0