Skip to contentSkip to conversation

bcache-tools: enable big endian support for s390x

PATCH In review Coly Li 3 participants 9 messages Mar 15, 2018 – Aug 6, 2018 Block layer

lore
Share & tools
lore

Coly Li

author
#0 lore raw

cover bcache-tools: enable big endian support for s390x

Recently I recieve a bug report that bcache does not work on IBM s390x, I find the problem is from endianness support and checksum calculation.

Both user space tools and Linux kernel code should be fixed, this patch set is the modification to user space tools.

Ideas of this patch set are,
1, super block members should be swapped to little endian before writting
down to media.
2, super block members should be explicitly swapped to little endian
before calculate checksum.
3, checksum also should be swapped to little endian before writiting down
to media.
4, checksum is checked before super block members being swapped into CPU
byteorder.

The first 3 patches are preparation and the last patch does big endian support.

NOTE: Linux kernel code also needs to be patched, this patch set just makes new created bcache devices in a correct byte order.

Coly Li
---

Coly Li (4):
bcache-tools: change bcache.c:crc64() to non-inline function
bcache-tools: only set/display sb.bucket_size for cache devices
bcache-tools: print more super block information in bcache-super-show
bcache-tools: add CPU endianness support

 bcache-super-show.c | 52 +++++++++++++++++++++-----------
 bcache.c            | 75 ++++++++++++++++++++++++++++++++++++++++++++-
 bcache.h            | 19 +++++++++---
 byteorder.h         | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 make-bcache.c       |  5 +--
 5 files changed, 212 insertions(+), 26 deletions(-)
 create mode 100644 byteorder.h
Signature · 1 line
2.16.2
author
#1 lore raw

PATCH 1/4 bcache-tools: change bcache.c:crc64() to non-inline function

bcache.o is used as a static lib but not explicitly compile and linked as position independent code. The only routine in bcache.c is inline function crc64(). For gcc 7.X this inline function will be optimized out in such situation, and when linking bcache.o to make-bcache or bcache-show-super there will be an error "no reference to crc64()".

The simplest way to fix the compiling failure is to change crc64() to be a non-inline function, then there will be an entry of crc64() in symbols table of bcache.o.

---
 bcache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bcache.c +1 -1
index 8f37445..8b4b986 100644
@@ -115,7 +115,7 @@ static const uint64_t crc_table[256] = {
 	0x9AFCE626CE85B507ULL
 };
 
-inline uint64_t crc64(const void *_data, size_t len)
+uint64_t crc64(const void *_data, size_t len)
 {
 	uint64_t crc = 0xFFFFFFFFFFFFFFFFULL;
 	const unsigned char *data = _data;
Signature · 1 line
2.16.2
author
#2 lore raw

PATCH 2/4 bcache-tools: only set/display sb.bucket_size for cache devices

In reply to Coly Li

bucket_size of struct cached_dev is not shared between cache devices and backing devices. Currently make-bcache.c assigns bucket_size to backing devices, and bcache-super-show.c displays bucket_size for backing devices, all these are incorrect.

This patch fixes it by only set and display sb.bucket_size for cache device super block.

---
 bcache-super-show.c | 9 ++++-----
 make-bcache.c       | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)
bcache-super-show.c +4 -5
index 26cc40e..515c596 100644
@@ -160,20 +160,19 @@ int main(int argc, char **argv)
 	uuid_unparse(sb.uuid, uuid);
 	printf("dev.uuid\t\t%s\n", uuid);
 
-	printf("dev.sectors_per_block\t%u\n"
-	       "dev.sectors_per_bucket\t%u\n",
-	       sb.block_size,
-	       sb.bucket_size);
+	printf("dev.sectors_per_block\t%u\n", sb.block_size);
 
 	if (!SB_IS_BDEV(&sb)) {
 		// total_sectors includes the superblock;
-		printf("dev.cache.first_sector\t%u\n"
+		printf("dev.sectors_per_block\t%u\n"
+		       "dev.cache.first_sector\t%u\n"
 		       "dev.cache.cache_sectors\t%ju\n"
 		       "dev.cache.total_sectors\t%ju\n"
 		       "dev.cache.ordered\t%s\n"
 		       "dev.cache.discard\t%s\n"
 		       "dev.cache.pos\t\t%u\n"
 		       "dev.cache.replacement\t%ju",
+		       sb.bucket_size,
 		       sb.bucket_size * sb.first_bucket,
 		       sb.bucket_size * (sb.nbuckets - sb.first_bucket),
 		       sb.bucket_size * sb.nbuckets,
make-bcache.c +1 -1
index c626eae..14c605d 100644
@@ -217,7 +217,6 @@ static void write_sb(char *dev, unsigned block_size, unsigned bucket_size,
 	uuid_generate(sb.uuid);
 	memcpy(sb.set_uuid, set_uuid, sizeof(sb.set_uuid));
 
-	sb.bucket_size	= bucket_size;
 	sb.block_size	= block_size;
 
 	uuid_unparse(sb.uuid, uuid_str);
@@ -242,6 +241,7 @@ static void write_sb(char *dev, unsigned block_size, unsigned bucket_size,
 		       sb.block_size,
 		       data_offset);
 	} else {
+		sb.bucket_size		= bucket_size;
 		sb.nbuckets		= getblocks(fd) / sb.bucket_size;
 		sb.nr_in_set		= 1;
 		sb.first_bucket		= (23 / sb.bucket_size) + 1;
Signature · 1 line
2.16.2
author
#3 lore raw

PATCH 3/4 bcache-tools: print more super block information in bcache-super-show

In reply to Coly Li

This patch prints out more super block information. The more detailed and exact information is helpful to analyse and debug cache device and backing device states.

---
 bcache-super-show.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
bcache-super-show.c +14 -0
index 515c596..2d46cac 100644
@@ -131,12 +131,18 @@ int main(int argc, char **argv)
 		case BCACHE_SB_VERSION_CDEV:
 		case BCACHE_SB_VERSION_CDEV_WITH_UUID:
 			printf(" [cache device]\n");
+			printf("sb.nbuckets\t\t%" PRIu64 "\n", sb.nbuckets);
+			printf("sb.bucket_size\t\t%d\n", sb.bucket_size);
+			printf("sb.nr_in_set\t\t%d\n", sb.nr_in_set);
+			printf("sb.nr_this_dev\t\t%d\n", sb.nr_this_dev);
 			break;
 
 		// The second adds data offset support
 		case BCACHE_SB_VERSION_BDEV:
 		case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
 			printf(" [backing device]\n");
+			printf("sb.data_offset\t\t%" PRIu64 "\n",
+			       sb.data_offset);
 			break;
 
 		default:
@@ -145,6 +151,14 @@ int main(int argc, char **argv)
 			return 0;
 	}
 
+	printf("sb.block_size\t\t%d\n", sb.block_size);
+	printf("sb.offset\t\t%" PRIu64 "\n", sb.offset);
+	printf("sb.flags\t\t%" PRIx64 "\n", sb.flags);
+	printf("sb.seq\t\t\t%" PRIu64 "\n", sb.seq);
+	printf("sb.last_mount\t\t%" PRIu32 "\n", sb.last_mount);
+	printf("sb.first_bucket\t\t%d\n", sb.first_bucket);
+	printf("sb.keys\t\t\t%d\n", sb.keys);
+
 	putchar('\n');
 
 	char label[SB_LABEL_SIZE + 1];
Signature · 1 line
2.16.2
author
#4 lore raw

PATCH 4/4 bcache-tools: add CPU endianness support

In reply to Coly Li

Current code assumes CPU arch is little endian, so the created bcache devices cannot be recognized by big endian machine (e.g. S390).

This patch adds CPU endianness support, does same as Linux kernel code does, to make kernel code swap byte order of super block members correctly.

---
 bcache-super-show.c | 29 ++++++++++--------
 bcache.c            | 73 ++++++++++++++++++++++++++++++++++++++++++++
 bcache.h            | 19 +++++++++---
 byteorder.h         | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 make-bcache.c       |  3 +-
 5 files changed, 192 insertions(+), 19 deletions(-)
 create mode 100644 byteorder.h
bcache-super-show.c +16 -13
index 2d46cac..4f8ccd8 100644
@@ -63,7 +63,7 @@ int main(int argc, char **argv)
 	extern char *optarg;
 	struct cache_sb sb;
 	char uuid[40];
-	uint64_t expected_csum;
+	uint64_t expected_csum_le64;
 
 	while ((o = getopt(argc, argv, "f")) != EOF)
 		switch (o) {
@@ -104,27 +104,30 @@ int main(int argc, char **argv)
 		exit(2);
 	}
 
-	printf("sb.first_sector\t\t%" PRIu64, sb.offset);
-	if (sb.offset == SB_SECTOR) {
-		printf(" [match]\n");
-	} else {
-		printf(" [expected %ds]\n", SB_SECTOR);
-		fprintf(stderr, "Invalid superblock (bad sector)\n");
-		exit(2);
-	}
-
 	printf("sb.csum\t\t\t%" PRIX64, sb.csum);
-	expected_csum = csum_set(&sb);
-	if (sb.csum == expected_csum) {
+	expected_csum_le64 = csum_set_le64(&sb);
+	/* compare two little endian numbers is safe */
+	if (sb.csum == expected_csum_le64) {
 		printf(" [match]\n");
 	} else {
-		printf(" [expected %" PRIX64 "]\n", expected_csum);
+		printf(" [expected %" PRIX64 "]\n", le64_to_cpu(expected_csum_le64));
 		if (!force_csum) {
 			fprintf(stderr, "Corrupt superblock (bad csum)\n");
 			exit(2);
 		}
 	}
 
+	swap_sb_to_cpu(&sb);
+
+	printf("sb.first_sector\t\t%" PRIu64, sb.offset);
+	if (sb.offset == SB_SECTOR) {
+		printf(" [match]\n");
+	} else {
+		printf(" [expected %ds]\n", SB_SECTOR);
+		fprintf(stderr, "Invalid superblock (bad sector)\n");
+		exit(2);
+	}
+
 	printf("sb.version\t\t%" PRIu64, sb.version);
 	switch (sb.version) {
 		// These are handled the same by the kernel
bcache.c +73 -0

83 lines not on this page · raw message

bcache.h +14 -5
index 61e4252..eb72c6f 100644
@@ -7,6 +7,8 @@
 #ifndef _BCACHE_H
 #define _BCACHE_H
 
+#include "byteorder.h"
+
 #define BITMASK(name, type, field, offset, size)		\
 static inline uint64_t name(const type *k)			\
 { return (k->field >> offset) & ~(((uint64_t) ~0) << size); }	\
@@ -115,12 +117,19 @@ BITMASK(BDEV_STATE,		struct cache_sb, flags, 61, 2);
 #define BDEV_STATE_DIRTY	2U
 #define BDEV_STATE_STALE	3U
 
+void swap_sb_from_cpu(struct cache_sb *sb);
+void swap_sb_to_cpu(struct cache_sb *sb);
 uint64_t crc64(const void *_data, size_t len);
 
-#define node(i, j)		((void *) ((i)->d + (j)))
-#define end(i)			node(i, (i)->keys)
-
-#define csum_set(i)							\
-	crc64(((void *) (i)) + 8, ((void *) end(i)) - (((void *) (i)) + 8))
+/* members in calculation are all in little endian */
+static inline uint64_t csum_set_le64(struct cache_sb *sb)
+{
+	uint64_t csum;
+	uint16_t keys = le16_to_cpu(sb->keys);
+	void *start = (void *)(sb) + sizeof(sb->csum);
+	void *end = (void *)(sb->d + keys);
 
+	csum = crc64(start, end - start);
+	return cpu_to_le64(csum);
+}
 #endif
byteorder.h new file +87 -0

87 lines not on this page · raw message

make-bcache.c +2 -1
index 14c605d..144433f 100644
@@ -274,7 +274,8 @@ static void write_sb(char *dev, unsigned block_size, unsigned bucket_size,
 		       sb.first_bucket);
 	}
 
-	sb.csum = csum_set(&sb);
+	swap_sb_from_cpu(&sb);
+	sb.csum = csum_set_le64(&sb);
 
 	/* Zero start of disk */
 	if (pwrite(fd, zeroes, SB_START, 0) != SB_START) {
Signature · 1 line
2.16.2
#5 lore raw
In reply to Coly Li

Hmm. I can review these, but I don't currently have rights to apply to bcache-tools. Who's been maintaining these? I'm glad to take it on...

Mike

Quoted 39 lines · Coly Li

Recently I recieve a bug report that bcache does not work on IBM s390x, I find the problem is from endianness support and checksum calculation.

Both user space tools and Linux kernel code should be fixed, this patch set is the modification to user space tools.

Ideas of this patch set are,
1, super block members should be swapped to little endian before writting
down to media.
2, super block members should be explicitly swapped to little endian
before calculate checksum.
3, checksum also should be swapped to little endian before writiting down
to media.
4, checksum is checked before super block members being swapped into CPU
byteorder.

The first 3 patches are preparation and the last patch does big endian support.

NOTE: Linux kernel code also needs to be patched, this patch set just makes new created bcache devices in a correct byte order.

Coly Li
---

Coly Li (4):
bcache-tools: change bcache.c:crc64() to non-inline function
bcache-tools: only set/display sb.bucket_size for cache devices
bcache-tools: print more super block information in bcache-super-show
bcache-tools: add CPU endianness support

 bcache-super-show.c | 52 +++++++++++++++++++++-----------
 bcache.c            | 75 ++++++++++++++++++++++++++++++++++++++++++++-
 bcache.h            | 19 +++++++++---
 byteorder.h         | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 make-bcache.c       |  5 +--
 5 files changed, 212 insertions(+), 26 deletions(-)
 create mode 100644 byteorder.h
author
#6 lore raw
Quoted 3 lines · Michael Lyle

Hmm. I can review these, but I don't currently have rights to apply to bcache-tools. Who's been maintaining these? I'm glad to take it on...

Hi Mike,

I guess that Kent maintains user space tools currently. Since bcachefs should have a different tools set, use bcache-tools specifically for Linux bcache might be a good idea.

If you plan to maintain the user space tools, and have an agreement with Kent, maybe you can clone a tree on your own and modify the wiki page for your new tree address. At least I will poll new code for our products from your tree once you are ready.

Back to this patch set, I will post a new one, which distinguish on-disk format from in-memory structure.

Thanks.

Coly

[snip]

#7 lore raw

Re:bcache-tools repo?

In reply to Coly Li

Hi,

I'm the maintainer of the bcache-tools package for Fedora, and bcache tools has been quiet for a while so I wasn't paying attention. But I accidentally noticed there's stuff going on again with bcache tools. I did a quick search in the mail archives to see where the new bcache-tools git repository is (I think https://bcache.evilpiepirate.org/ doesn't point in the right direction), and I'm not able to find it.

Could anybody point me to the git repo that contains the latest bcache-tools?

Thanks!

Rolf

Quoted 39 lines · Coly Li

Recently I recieve a bug report that bcache does not work on IBM s390x, I find the problem is from endianness support and checksum calculation.

Both user space tools and Linux kernel code should be fixed, this patch set is the modification to user space tools.

Ideas of this patch set are,
1, super block members should be swapped to little endian before writting
down to media.
2, super block members should be explicitly swapped to little endian
before calculate checksum.
3, checksum also should be swapped to little endian before writiting down
to media.
4, checksum is checked before super block members being swapped into CPU
byteorder.

The first 3 patches are preparation and the last patch does big endian support.

NOTE: Linux kernel code also needs to be patched, this patch set just makes new created bcache devices in a correct byte order.

Coly Li
---

Coly Li (4):
bcache-tools: change bcache.c:crc64() to non-inline function
bcache-tools: only set/display sb.bucket_size for cache devices
bcache-tools: print more super block information in bcache-super-show
bcache-tools: add CPU endianness support

  bcache-super-show.c | 52 +++++++++++++++++++++-----------
  bcache.c            | 75 ++++++++++++++++++++++++++++++++++++++++++++-
  bcache.h            | 19 +++++++++---
  byteorder.h         | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  make-bcache.c       |  5 +--
  5 files changed, 212 insertions(+), 26 deletions(-)

create mode 100644 byteorder.h

author
#8 lore raw

Re: bcache-tools repo?

Quoted 10 lines · Rolf Fokkens

Hi,

I'm the maintainer of the bcache-tools package for Fedora, and bcache tools has been quiet for a while so I wasn't paying attention. But I accidentally noticed there's stuff going on again with bcache tools. I did a quick search in the mail archives to see where the new bcache-tools git repository is (I think
https://bcache.evilpiepirate.org/ doesn't point in the right direction), and I'm not able to find it.

Quoted 2 lines

Could anybody point me to the git repo that contains the latest bcache-tools?

Hi Rolf,

So far we don't have any change to bcache-tools, there are some changes in progress, notable once are big endian support and bcache-ctl.

Big endian support is still under developing. bcache-ctl seems to work well so far, but the author does not post patch yet.

I tend to keep all the changes in https://bcache.evilpiepirate.org until it is necessary to change the git repo.

Thanks.

Coly Li