Skip to content

Commit

Permalink
btrfs-progs: fix accessors for big endian systems
Browse files Browse the repository at this point in the history
[BUG]
There is a bug report that on s390x big endian systems, mkfs.btrfs just
fails:

  $ mkfs.btrfs  -f ~/test.img
  btrfs-progs v6.3.1
  Invalid mapping for 1081344-1097728, got 17592186044416-17592190238720
  Couldn't map the block 1081344
  ERROR: cannot read chunk root
  ERROR: open ctree failed

[CAUSE]
The error is caused by wrong endian conversion. The s390x is a big
endian architecture:

  $ lscpu
  Byte Order:            Big Endian

While checking the offending @disk_key and @key inside
btrfs_read_sys_array(), we got:

  2301		while (cur_offset < array_size) {
  (gdb)
  2304			if (cur_offset + len > array_size)
  (gdb)
  2307			btrfs_disk_key_to_cpu(&key, disk_key);
  (gdb)
  2310			sb_array_offset += len;
  (gdb) print *disk_key
  $2 = {objectid = 281474976710656, type = 228 '\344', offset = 17592186044416}
  (gdb) print key
  $3 = {objectid = 281474976710656, type = 228 '\344', offset = 17592186044416}
  (gdb)

Now we can see, @disk_key is indeed in the little endian, but @key is
not converted to the CPU native endian.

Furthermore, if we step into the help btrfs_disk_key_to_cpu(), it shows
we're using little endian version:

  (gdb) step
  btrfs_disk_key_to_cpu (disk_key=0x109fcdb, cpu_key=0x3ffffff847f)
      at ./kernel-shared/accessors.h:592
  592		memcpy(cpu_key, disk_key, sizeof(struct btrfs_key));

[FIX]
The kernel accessors.h checks if __LITTLE_ENDIAN is defined or not, but
that only works inside kernel.

In user space, __LITTLE_ENDIAN and __BIG_ENDIAN are both defined inside
endian.h header.

Instead we should check __BYTE_ORDER against __LITTLE_ENDIAN to
determine our endianness.

With this change, s390x build works as expected now.

Issue: #639
Signed-off-by: Qu Wenruo <[email protected]>
Signed-off-by: David Sterba <[email protected]>
  • Loading branch information
adam900710 authored and kdave committed Jun 15, 2023
1 parent aa49b7c commit fecbcc9
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion kernel-shared/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef BTRFS_ACCESSORS_H
#define BTRFS_ACCESSORS_H

#include <endian.h>

#ifndef _static_assert
#define _static_assert(expr) _Static_assert(expr, #expr)
#endif
Expand Down Expand Up @@ -579,7 +581,7 @@ BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key, objectid, 64)
BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);

#ifdef __LITTLE_ENDIAN
#if __BYTE_ORDER == __LITTLE_ENDIAN

/*
* Optimized helpers for little-endian architectures where CPU and on-disk
Expand Down

0 comments on commit fecbcc9

Please sign in to comment.