Skip to contentSkip to conversation

bcache-tools patches for large bucket size incompat feature

PATCH New Coly Li 1 participant 9 messages Aug 17, 2020

Subsystems (2)
lore
Share & tools
lore

Coly Li

author
#0 lore raw

cover bcache-tools patches for large bucket size incompat feature

Hi folks,

This is the user space bcache-tools patches to support large bucket size incompat features. With these patches, large_bucket feature will be set automatically if bucket size > 16MB by "bcache make -C " command. Also all feature sets strings can be displayed by "bcache show " command.

Now the version numbers are upgraded, "bcache make" command will create cache device with version number 5, and backing device with version number 6. All kernel support get merged in Linux v5.9 already, now the user space support follows up.

Coly Li
---

Coly Li (8):
bcache-tools: add struct cache_sb_disk into bcache.h
bcache-tools: bitwise.h: more swap bitwise for different CPU endians
bcache-tools: list.h: only define offsetof() when it is undefined
bcache-tools: add to_cache_sb() and to_cache_sb_disk()
bcache-tools: define separated super block for in-memory and on-disk
format
bcache-tools: upgrade super block versions for feature sets
bcache-tools: add large_bucket incompat feature
bcache-tools: add print_cache_set_supported_feature_sets() in lib.c

 Makefile            |   4 +-
 bcache-super-show.c |  24 ++++--
 bcache.c            |  15 +++-
 bcache.h            | 194 ++++++++++++++++++++++++++++++++++++++------
 bitwise.h           |  10 +++
 features.c          |  74 +++++++++++++++++
 features.h          |   8 ++
 lib.c               | 160 ++++++++++++++++++++++++++++++++----
 lib.h               |   8 +-
 list.h              |   2 +
 make.c              |  70 ++++++----------
 probe-bcache.c      |   8 +-
 12 files changed, 472 insertions(+), 105 deletions(-)
 create mode 100644 features.c
 create mode 100644 features.h
Signature · 1 line
2.26.2
author
#1 lore raw

PATCH 1/8 bcache-tools: add struct cache_sb_disk into bcache.h

This is to sync the data structure from bcache kernel code uapi header. The new added struct cache_sb_disk is used to define the on-disk format bcache super block. Later struct cache_sb will be used for the in-memory format bcache super block.

---
 bcache.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
bcache.h +54 -0
index c83f838..82fe580 100644
@@ -7,6 +7,8 @@
 #ifndef _BCACHE_H
 #define _BCACHE_H
 
+#include <linux/types.h>
+
 #define BITMASK(name, type, field, offset, size)		\
 static inline uint64_t name(const type *k)			\
 { return (k->field >> offset) & ~(((uint64_t) ~0) << size); }	\
@@ -40,6 +42,58 @@ static const char bcache_magic[] = {
 #define BDEV_DATA_START_DEFAULT	16	/* sectors */
 #define SB_START		(SB_SECTOR * 512)
 
+struct cache_sb_disk {
+	__le64			csum;
+	__le64			offset; /* sector where this sb was written */
+	__le64			version;
+
+	__u8			magic[16];
+
+	__u8			uuid[16];
+	union {
+		__u8		set_uuid[16];
+		__le64		set_magic;
+	};
+	__u8			label[SB_LABEL_SIZE];
+
+	__le64			flags;
+	__le64			seq;
+
+	__le64			pad[8];
+
+	union {
+	struct {
+		/* Cache devices */
+		__le64		nbuckets;	/* device size */
+
+		__le16		block_size;	/* sectors */
+		__le16		bucket_size;	/* sectors */
+
+		__le16		nr_in_set;
+		__le16		nr_this_dev;
+	};
+	struct {
+		/* Backing devices */
+		__le64		data_offset;
+
+		/*
+		 * block_size from the cache device section is still used by
+		 * backing devices, so don't add anything here until we fix
+		 * things to not need it for backing devices anymore
+		 */
+	};
+	};
+
+	__le32			last_mount;		/* time overflow in y2106 */
+
+	__le16			first_bucket;
+	union {
+		__le16		njournal_buckets;
+		__le16		keys;
+	};
+	__le64			d[SB_JOURNAL_BUCKETS];	/* journal buckets */
+};
+
 struct cache_sb {
 	uint64_t		csum;
 	uint64_t		offset;	/* sector where this sb was written */
Signature · 1 line
2.26.2
author
#2 lore raw

PATCH 2/8 bcache-tools: bitwise.h: more swap bitwise for different CPU endians

In reply to Coly Li

This patch adds more swap routines to bitwise.h,

	le16_to_cpu()
	le32_to_cpu()
	le64_to_cpu()
---
 bitwise.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
bitwise.h +10 -0
index 968002f..1194b27 100644
@@ -45,11 +45,21 @@
 #define cpu_to_le16(val)	((__le16)(val))
 #define cpu_to_le32(val)	((__le32)(val))
 #define cpu_to_le64(val)	((__le64)(val))
+
+#define le16_to_cpu(val)	((__le16)(val))
+#define le32_to_cpu(val)	((__le32)(val))
+#define le64_to_cpu(val)	((__le64)(val))
+
 #else
 /* For big endian */
 #define cpu_to_le16(val)	((__be16)__swab16((__u16)(val)))
 #define cpu_to_le32(val)	((__be32)__swab32((__u32)(val)))
 #define cpu_to_le64(val)	((__be64)__swab64((__u64)(val)))
+
+#define le16_to_cpu(val)	((__be16)__swab16((__u16)(val)))
+#define le32_to_cpu(val)	((__be32)__swab32((__u32)(val)))
+#define le64_to_cpu(val)	((__be64)__swab64((__u64)(val)))
+
 #endif
 
 #endif
Signature · 1 line
2.26.2
author
#3 lore raw

PATCH 3/8 bcache-tools: list.h: only define offsetof() when it is undefined

In reply to Coly Li

For new gcc headers, offsetof() is defined, the definition in list.h will be warned as redefined.

This patch checks whether offsetof() is defined, and only defines local version of offsetof() when it is not defined by gcc headers.

---
 list.h | 2 ++
 1 file changed, 2 insertions(+)
list.h +2 -0
index 55d2bb4..458281d 100644
@@ -25,10 +25,12 @@
  */
 /*@{*/
 
+#ifndef offsetof
 /**
  * Get offset of a member
  */
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif /* offsetof*/
 
 /**
  * Casts a member of a structure out to the containing structure
Signature · 1 line
2.26.2
author
#4 lore raw

PATCH 4/8 bcache-tools: add to_cache_sb() and to_cache_sb_disk()

In reply to Coly Li

This patch adds routines to_cache_sb() to_cache_sb_disk() to lib.c, which convert bcache super block between in-memory and on-disk formats.

This is a preparation to separate current struct cache_sb into two structures,
- struct cache_sb is for in-memory super block
- struct cache_sb_disk is for on-disk super block

---
 lib.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 lib.h |  3 +-
 2 files changed, 90 insertions(+), 4 deletions(-)
lib.c +88 -3
index 9e69419..542f115 100644
@@ -8,14 +8,14 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
-#include "bcache.h"
-#include "lib.h"
 #include <uuid.h>
 #include <string.h>
 #include <malloc.h>
 #include <regex.h>
 
-
+#include "bcache.h"
+#include "lib.h"
+#include "bitwise.h"
 /*
  * utils function
  */
@@ -681,3 +681,88 @@ int set_label(char *devname, char *label)
 	close(fd);
 	return 0;
 }
+
+
+struct cache_sb *to_cache_sb(struct cache_sb *sb,
+			     struct cache_sb_disk *sb_disk)
+{
+	/* Convert common part */
+	sb->offset = le64_to_cpu(sb_disk->offset);
+	sb->version = le64_to_cpu(sb_disk->version);
+	memcpy(sb->magic, sb_disk->magic, 16);
+	memcpy(sb->uuid, sb_disk->uuid, 16);
+	memcpy(sb->set_uuid, sb_disk->set_uuid, 16);
+	memcpy(sb->label, sb_disk->label, SB_LABEL_SIZE);
+	sb->flags = le64_to_cpu(sb_disk->flags);
+	sb->seq = le64_to_cpu(sb_disk->seq);
+	sb->block_size = le16_to_cpu(sb_disk->block_size);
+	sb->last_mount = le32_to_cpu(sb_disk->last_mount);
+	sb->first_bucket = le16_to_cpu(sb_disk->first_bucket);
+	sb->keys = le16_to_cpu(sb_disk->keys);
+
+	/* For cache or backing devices*/
+
+	if (sb->version > BCACHE_SB_MAX_VERSION) {
+		/* Unsupported version */
+		fprintf(stderr, "Unsupported super block version: %lld\n",
+			sb->version);
+	} else if (SB_IS_BDEV(sb)) {
+		/* Backing device */
+		sb->data_offset = le64_to_cpu(sb_disk->data_offset);
+	} else {
+		int i;
+
+		/* Cache device */
+		sb->nbuckets = le64_to_cpu(sb_disk->nbuckets);
+		sb->nr_in_set = le16_to_cpu(sb_disk->nr_in_set);
+		sb->nr_this_dev = le16_to_cpu(sb_disk->nr_this_dev);
+		sb->bucket_size = le32_to_cpu(sb_disk->bucket_size);
+
+		for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
+			sb->d[i]= le64_to_cpu(sb_disk->d[i]);
+	}
+
+	return sb;
+}
+
+struct cache_sb_disk *to_cache_sb_disk(struct cache_sb_disk *sb_disk,
+				       struct cache_sb *sb)
+{
+	/* Convert common part */
+	sb_disk->offset = cpu_to_le64(sb->offset);
+	sb_disk->version = cpu_to_le64(sb->version);
+	memcpy(sb_disk->magic, sb->magic, 16);
+	memcpy(sb_disk->uuid, sb->uuid, 16);
+	memcpy(sb_disk->set_uuid, sb->set_uuid, 16);
+	memcpy(sb_disk->label, sb->label, SB_LABEL_SIZE);
+	sb_disk->flags = cpu_to_le64(sb->flags);
+	sb_disk->seq = cpu_to_le64(sb->seq);
+	sb_disk->block_size = cpu_to_le16(sb->block_size);
+	sb_disk->last_mount = cpu_to_le32(sb->last_mount);
+	sb_disk->first_bucket = cpu_to_le16(sb->first_bucket);
+	sb_disk->keys = cpu_to_le16(sb->keys);
+
+	/* For cache and backing devices */
+
+	if (sb->version > BCACHE_SB_MAX_VERSION) {
+		/* Unsupported version */
+		fprintf(stderr, "Unsupported super block version: %lld\n",
+			sb->version);
+	} else if (SB_IS_BDEV(sb)) {
+		/* Backing device */
+		sb_disk->data_offset = cpu_to_le64(sb->data_offset);
+	} else {
+		int i;
+
+		/* Cache device */
+		sb_disk->nbuckets = cpu_to_le64(sb->nbuckets);
+		sb_disk->nr_in_set = cpu_to_le16(sb->nr_in_set);
+		sb_disk->nr_this_dev = cpu_to_le16(sb->nr_this_dev);
+		sb_disk->bucket_size = cpu_to_le32(sb->bucket_size);
+
+		for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
+			sb_disk->d[i] = cpu_to_le64(sb->d[i]);
+	}
+
+	return sb_disk;
+}
lib.h +2 -1
index d4537b0..b37608e 100644
@@ -50,7 +50,8 @@ int detach_backdev(char *devname);
 int set_backdev_cachemode(char *devname, char *cachemode);
 int set_label(char *devname, char *label);
 int cset_to_devname(struct list_head *head, char *cset, char *devname);
-
+struct cache_sb *to_cache_sb(struct cache_sb *sb, struct cache_sb_disk *sb_disk);
+struct cache_sb_disk *to_cache_sb_disk(struct cache_sb_disk *sb_disk,struct cache_sb *sb);
 
 #define DEVLEN sizeof(struct dev)
 
Signature · 1 line
2.26.2
author
#5 lore raw

PATCH 5/8 bcache-tools: define separated super block for in-memory and on-disk format

In reply to Coly Li

This patch syncrhonizes the super block definition from bcache kernel code, now the original super block structure is changed into two,
- struct cache_sb_disk
This is for on-disk bcache super block format, which is exactly same as original struct cache_sb.
- struct cache_sb
This is only for in-memory super block, it is no longer exactly mapping to the members and offsets to the cache_sb_disk.

Most part of the patch is to make source code to be consistent to the data structures change.

---
 Makefile            |  2 +-
 bcache-super-show.c | 22 +++++++++++--------
 bcache.h            | 49 ++++++++++++++++++++++--------------------
 lib.c               | 18 +++++++++++-----
 make.c              | 52 ++++++++++-----------------------------------
 probe-bcache.c      |  8 +++----
 6 files changed, 68 insertions(+), 83 deletions(-)
Makefile +1 -1
index 2c326cf..b4546a1 100644
@@ -31,7 +31,7 @@ probe-bcache: CFLAGS += `pkg-config --cflags uuid blkid`
 
 bcache-super-show: LDLIBS += `pkg-config --libs uuid`
 bcache-super-show: CFLAGS += -std=gnu99
-bcache-super-show: crc64.o
+bcache-super-show: crc64.o lib.o
 
 bcache-register: bcache-register.o
 
bcache-super-show.c +13 -9
index 26cc40e..883410f 100644
@@ -25,7 +25,8 @@
 #include <uuid/uuid.h>
 
 #include "bcache.h"
-
+#include "lib.h"
+#include "bitwise.h"
 
 static void usage()
 {
@@ -61,6 +62,7 @@ int main(int argc, char **argv)
 	bool force_csum = false;
 	int o;
 	extern char *optarg;
+	struct cache_sb_disk sb_disk;
 	struct cache_sb sb;
 	char uuid[40];
 	uint64_t expected_csum;
@@ -90,11 +92,13 @@ int main(int argc, char **argv)
 		exit(2);
 	}
 
-	if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) {
+	if (pread(fd, &sb_disk, sizeof(sb_disk), SB_START) != sizeof(sb_disk)) {
 		fprintf(stderr, "Couldn't read\n");
 		exit(2);
 	}
 
+	to_cache_sb(&sb, &sb_disk);
+
 	printf("sb.magic\t\t");
 	if (!memcmp(sb.magic, bcache_magic, 16)) {
 		printf("ok\n");
@@ -104,7 +108,7 @@ int main(int argc, char **argv)
 		exit(2);
 	}
 
-	printf("sb.first_sector\t\t%" PRIu64, sb.offset);
+	printf("sb.first_sector\t\t%llu", sb.offset);
 	if (sb.offset == SB_SECTOR) {
 		printf(" [match]\n");
 	} else {
@@ -113,9 +117,9 @@ int main(int argc, char **argv)
 		exit(2);
 	}
 
-	printf("sb.csum\t\t\t%" PRIX64, sb.csum);
-	expected_csum = csum_set(&sb);
-	if (sb.csum == expected_csum) {
+	printf("sb.csum\t\t\t%llx", le64_to_cpu(sb_disk.csum));
+	expected_csum = csum_set(&sb_disk);
+	if (le64_to_cpu(sb_disk.csum) == expected_csum) {
 		printf(" [match]\n");
 	} else {
 		printf(" [expected %" PRIX64 "]\n", expected_csum);
@@ -125,7 +129,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	printf("sb.version\t\t%" PRIu64, sb.version);
+	printf("sb.version\t\t%llu", sb.version);
 	switch (sb.version) {
 		// These are handled the same by the kernel
 		case BCACHE_SB_VERSION_CDEV:
@@ -168,8 +172,8 @@ int main(int argc, char **argv)
 	if (!SB_IS_BDEV(&sb)) {
 		// total_sectors includes the superblock;
 		printf("dev.cache.first_sector\t%u\n"
-		       "dev.cache.cache_sectors\t%ju\n"
-		       "dev.cache.total_sectors\t%ju\n"
+		       "dev.cache.cache_sectors\t%llu\n"
+		       "dev.cache.total_sectors\t%llu\n"
 		       "dev.cache.ordered\t%s\n"
 		       "dev.cache.discard\t%s\n"
 		       "dev.cache.pos\t\t%u\n"
bcache.h +26 -23

78 lines not on this page · raw message

lib.c +13 -5
index 542f115..9a2fa26 100644
@@ -293,7 +293,6 @@ int detail_base(char *devname, struct cache_sb sb, struct dev *base)
 	strcpy(base->name, devname);
 	base->magic = "ok";
 	base->first_sector = SB_SECTOR;
-	base->csum = sb.csum;
 	base->version = sb.version;
 
 	strncpy(base->label, (char *) sb.label, SB_LABEL_SIZE);
@@ -325,6 +324,7 @@ int detail_base(char *devname, struct cache_sb sb, struct dev *base)
 
 int may_add_item(char *devname, struct list_head *head)
 {
+	struct cache_sb_disk sb_disk;
 	struct cache_sb sb;
 
 	if (strcmp(devname, ".") == 0 || strcmp(devname, "..") == 0)
@@ -336,10 +336,13 @@ int may_add_item(char *devname, struct list_head *head)
 
 	if (fd == -1)
 		return 0;
-	if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) {
+	if (pread(fd, &sb_disk, sizeof(sb_disk), SB_START) != sizeof(sb_disk)) {
 		close(fd);
 		return 0;
 	}
+
+	to_cache_sb(&sb, &sb_disk);
+
 	if (memcmp(sb.magic, bcache_magic, 16)) {
 		close(fd);
 		return 0;
@@ -348,6 +351,8 @@ int may_add_item(char *devname, struct list_head *head)
 	int ret;
 
 	tmp = (struct dev *) malloc(DEVLEN);
+
+	tmp->csum = le64_to_cpu(sb_disk.csum);
 	ret = detail_base(dev, sb, tmp);
 	if (ret != 0) {
 		fprintf(stderr, "Failed to get information for %s\n", dev);
@@ -399,6 +404,7 @@ int list_bdevs(struct list_head *head)
 
 int detail_dev(char *devname, struct bdev *bd, struct cdev *cd, int *type)
 {
+	struct cache_sb_disk sb_disk;
 	struct cache_sb sb;
 	uint64_t expected_csum;
 	int fd = open(devname, O_RDONLY);
@@ -408,11 +414,13 @@ int detail_dev(char *devname, struct bdev *bd, struct cdev *cd, int *type)
 		return 1;
 	}
 
-	if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) {
+	if (pread(fd, &sb_disk, sizeof(sb_disk), SB_START) != sizeof(sb_disk)) {
 		fprintf(stderr, "Couldn't read\n");
 		goto Fail;
 	}
 
+	to_cache_sb(&sb, &sb_disk);
+
 	if (memcmp(sb.magic, bcache_magic, 16)) {
 		fprintf(stderr,
 			"Bad magic,make sure this is an bcache device\n");
@@ -424,8 +432,8 @@ int detail_dev(char *devname, struct bdev *bd, struct cdev *cd, int *type)
 		goto Fail;
 	}
 
-	expected_csum = csum_set(&sb);
-	if (!(sb.csum == expected_csum)) {
+	expected_csum = csum_set(&sb_disk);
+	if (le64_to_cpu(sb_disk.csum) != expected_csum) {
 		fprintf(stderr, "Csum is not match with expected one\n");
 		goto Fail;
 	}
make.c +11 -41

113 lines not on this page · raw message

probe-bcache.c +4 -4
index c94c972..a640046 100644
@@ -29,7 +29,7 @@ int main(int argc, char **argv)
 	bool udev = false;
 	int i, o;
 	extern char *optarg;
-	struct cache_sb sb;
+	struct cache_sb_disk sb_disk;
 	char uuid[40];
 	blkid_probe pr;
 
@@ -66,13 +66,13 @@ int main(int argc, char **argv)
 			continue;
 		}
 
-		if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb))
+		if (pread(fd, &sb_disk, sizeof(sb_disk), SB_START) != sizeof(sb_disk))
 			continue;
 
-		if (memcmp(sb.magic, bcache_magic, 16))
+		if (memcmp(sb_disk.magic, bcache_magic, 16))
 			continue;
 
-		uuid_unparse(sb.uuid, uuid);
+		uuid_unparse(sb_disk.uuid, uuid);
 
 		if (udev)
 			printf("ID_FS_UUID=%s\n"
Signature · 1 line
2.26.2
author
#6 lore raw

PATCH 6/8 bcache-tools: upgrade super block versions for feature sets

In reply to Coly Li

This patch upgrades super block versions for adding feature sets into struct cache_sb_disk,

128         __u64                   feature_compat;
129         __u64                   feature_incompat;
130         __u64                   feature_ro_compat;

With the feature set flags, it will be convenient to add new features without upgrade super block version again (at least for a long time).

The new added versions are,
- BCACHE_SB_VERSION_CDEV_WITH_FEATURES for cache device super block
- BCACHE_SB_VERSION_BDEV_WITH_FEATURES for backing device super block

The feature set flags are also added into the in-memory super block struct cache_sb. The feature sets conversion between cache_sb_disk and cache_sb also added into to_cache_sb() and to_cache_sb_disk().

Some feature sets related macros are also added into bcache.h, and a new file feature.c is added for future new features record and display.

---
 Makefile   |  2 +-
 bcache.h   | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 features.c | 22 ++++++++++++++
 lib.c      | 12 ++++++++
 4 files changed, 118 insertions(+), 3 deletions(-)
 create mode 100644 features.c
Makefile +1 -1
index b4546a1..8b87a67 100644
@@ -38,4 +38,4 @@ bcache-register: bcache-register.o
 bcache: CFLAGS += `pkg-config --cflags blkid uuid smartcols`
 bcache: LDLIBS += `pkg-config --libs blkid uuid smartcols`
 bcache: CFLAGS += -std=gnu99
-bcache: crc64.o lib.o make.o zoned.o
+bcache: crc64.o lib.o make.o zoned.o features.o
bcache.h +83 -2

112 lines not on this page · raw message

features.c new file +22 -0
new file mode 100644
index 0000000..a1c9884
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Author: Coly Li <[email protected]>
+ *
+ * Inspired by e2fsprogs features compat/incompat/ro_compat
+ * related code.
+ */
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "bcache.h"
+
+struct feature {
+	int		compat;
+	unsigned int	mask;
+	const char	*string;
+};
+
+static struct feature feature_list[] = {
+	{0, 0, 0 },
+};
lib.c +12 -0
index 9a2fa26..dcf752c 100644
@@ -730,6 +730,12 @@ struct cache_sb *to_cache_sb(struct cache_sb *sb,
 			sb->d[i]= le64_to_cpu(sb_disk->d[i]);
 	}
 
+	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
+		sb->feature_compat = le64_to_cpu(sb_disk->feature_compat);
+		sb->feature_incompat = le64_to_cpu(sb_disk->feature_incompat);
+		sb->feature_ro_compat = le64_to_cpu(sb_disk->feature_ro_compat);
+	}
+
 	return sb;
 }
 
@@ -772,5 +778,11 @@ struct cache_sb_disk *to_cache_sb_disk(struct cache_sb_disk *sb_disk,
 			sb_disk->d[i] = cpu_to_le64(sb->d[i]);
 	}
 
+	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
+		sb_disk->feature_compat = cpu_to_le64(sb->feature_compat);
+		sb_disk->feature_incompat = cpu_to_le64(sb->feature_incompat);
+		sb_disk->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat);
+	}
+
 	return sb_disk;
 }
Signature · 1 line
2.26.2
author
#7 lore raw

PATCH 7/8 bcache-tools: add large_bucket incompat feature

In reply to Coly Li

This feature adds __le16 bucket_size_hi into struct cache_sb_disk, to permit bucket size to be 32bit width. Current maximum bucket size is 16MB, extend it to 32bits will permit much large bucket size which is desired by zoned SSD devices (a typical zone size is 256MB).

When setting a bucket size > 16MB, large_bucket feature will be set automatically and the super block version will also be set to BCACHE_SB_VERSION_CDEV_WITH_FEATURES.

---
 bcache.h   |  8 ++++++++
 features.c |  2 ++
 lib.c      | 19 +++++++++++++++++++
 lib.h      |  1 +
 make.c     | 18 ++++++++++++------
 5 files changed, 42 insertions(+), 6 deletions(-)
bcache.h +8 -0
index 9d969e1..6aef9c4 100644
@@ -100,6 +100,7 @@ struct cache_sb_disk {
 		__le16		keys;
 	};
 	__le64			d[SB_JOURNAL_BUCKETS];	/* journal buckets */
+	__le16			bucket_size_hi;
 };
 
 /*
@@ -210,6 +211,11 @@ uint64_t crc64(const void *data, size_t len);
 #define BCH_HAS_INCOMPAT_FEATURE(sb, mask) \
 		((sb)->feature_incompat & (mask))
 
+/* Feature set definition */
+
+/* Incompat feature set */
+#define BCH_FEATURE_INCOMPAT_LARGE_BUCKET	0x0001 /* 32bit bucket size */
+
 #define BCH_FEATURE_COMPAT_FUNCS(name, flagname) \
 static inline int bch_has_feature_##name(struct cache_sb *sb) \
 { \
@@ -261,4 +267,6 @@ static inline void bch_clear_feature_##name(struct cache_sb *sb) \
 		~BCH##_FEATURE_INCOMPAT_##flagname; \
 }
 
+BCH_FEATURE_INCOMPAT_FUNCS(large_bucket, LARGE_BUCKET);
+
 #endif
features.c +2 -0
index a1c9884..df15862 100644
@@ -18,5 +18,7 @@ struct feature {
 };
 
 static struct feature feature_list[] = {
+	{BCH_FEATURE_INCOMPAT, BCH_FEATURE_INCOMPAT_LARGE_BUCKET,
+		"large_bucket"},
 	{0, 0, 0 },
 };
lib.c +19 -0
index dcf752c..efabeb1 100644
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 #include <blkid.h>
 #include <dirent.h>
+#include <limits.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -736,6 +737,10 @@ struct cache_sb *to_cache_sb(struct cache_sb *sb,
 		sb->feature_ro_compat = le64_to_cpu(sb_disk->feature_ro_compat);
 	}
 
+	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES &&
+	    bch_has_feature_large_bucket(sb))
+		sb->bucket_size += le16_to_cpu(sb_disk->bucket_size_hi) << 16;
+
 	return sb;
 }
 
@@ -784,5 +789,19 @@ struct cache_sb_disk *to_cache_sb_disk(struct cache_sb_disk *sb_disk,
 		sb_disk->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat);
 	}
 
+	if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES &&
+	    bch_has_feature_large_bucket(sb))
+		sb_disk->bucket_size_hi = cpu_to_le16(sb->bucket_size >> 16);
+
 	return sb_disk;
 }
+
+void set_bucket_size(struct cache_sb *sb, unsigned int bucket_size)
+{
+	if (bucket_size > USHRT_MAX) {
+		sb->version = BCACHE_SB_VERSION_CDEV_WITH_FEATURES;
+		bch_set_feature_large_bucket(sb);
+	}
+
+	sb->bucket_size = bucket_size;
+}
lib.h +1 -0
index b37608e..1dd2bfe 100644
@@ -52,6 +52,7 @@ int set_label(char *devname, char *label);
 int cset_to_devname(struct list_head *head, char *cset, char *devname);
 struct cache_sb *to_cache_sb(struct cache_sb *sb, struct cache_sb_disk *sb_disk);
 struct cache_sb_disk *to_cache_sb_disk(struct cache_sb_disk *sb_disk,struct cache_sb *sb);
+void set_bucket_size(struct cache_sb *sb, unsigned int bucket_size);
 
 #define DEVLEN sizeof(struct dev)
 
make.c +12 -6
index a239023..9631857 100644
@@ -83,7 +83,9 @@ uint64_t hatoi(const char *s)
 	return i;
 }
 
-unsigned int hatoi_validate(const char *s, const char *msg)
+unsigned int hatoi_validate(const char *s,
+			    const char *msg,
+			    unsigned long max)
 {
 	uint64_t v = hatoi(s);
 
@@ -94,7 +96,7 @@ unsigned int hatoi_validate(const char *s, const char *msg)
 
 	v /= 512;
 
-	if (v > USHRT_MAX) {
+	if (v > max) {
 		fprintf(stderr, "%s too large\n", msg);
 		exit(EXIT_FAILURE);
 	}
@@ -338,7 +340,7 @@ static void write_sb(char *dev, unsigned int block_size,
 	uuid_generate(sb.uuid);
 	memcpy(sb.set_uuid, set_uuid, sizeof(sb.set_uuid));
 
-	sb.bucket_size	= bucket_size;
+	set_bucket_size(&sb, bucket_size);
 	sb.block_size	= block_size;
 
 	uuid_unparse(sb.uuid, uuid_str);
@@ -362,7 +364,8 @@ static void write_sb(char *dev, unsigned int block_size,
 		}
 
 		if (data_offset != BDEV_DATA_START_DEFAULT) {
-			sb.version = BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
+			if (sb.version < BCACHE_SB_VERSION_BDEV_WITH_OFFSET)
+				sb.version = BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
 			sb.data_offset = data_offset;
 		}
 
@@ -382,6 +385,7 @@ static void write_sb(char *dev, unsigned int block_size,
 	} else {
 		sb.nbuckets		= getblocks(fd) / sb.bucket_size;
 		sb.nr_in_set		= 1;
+		/* 23 is (SB_SECTOR + SB_SIZE) - 1 sectors */
 		sb.first_bucket		= (23 / sb.bucket_size) + 1;
 
 		if (sb.nbuckets < 1 << 7) {
@@ -538,10 +542,12 @@ int make_bcache(int argc, char **argv)
 			bdev = 1;
 			break;
 		case 'b':
-			bucket_size = hatoi_validate(optarg, "bucket size");
+			bucket_size =
+				hatoi_validate(optarg, "bucket size", UINT_MAX);
 			break;
 		case 'w':
-			block_size = hatoi_validate(optarg, "block size");
+			block_size =
+				hatoi_validate(optarg, "block size", USHRT_MAX);
 			break;
 #if 0
 		case 'U':
Signature · 1 line
2.26.2
author
#8 lore raw

PATCH 8/8 bcache-tools: add print_cache_set_supported_feature_sets() in lib.c

In reply to Coly Li

print_cache_set_supported_feature_sets() is used to print out feature set strings for a specified cache device super block. It can be used when make a bcache cache device, or show a super block information of a bcache cache device.

---
 bcache-super-show.c |  2 ++
 bcache.c            | 15 +++++++++++---
 features.c          | 50 +++++++++++++++++++++++++++++++++++++++++++++
 features.h          |  8 ++++++++
 lib.c               | 20 +++++++++++-------
 lib.h               |  4 ++++
 6 files changed, 89 insertions(+), 10 deletions(-)
 create mode 100644 features.h
bcache-super-show.c +2 -0
index 883410f..cc36029 100644
@@ -134,12 +134,14 @@ int main(int argc, char **argv)
 		// These are handled the same by the kernel
 		case BCACHE_SB_VERSION_CDEV:
 		case BCACHE_SB_VERSION_CDEV_WITH_UUID:
+		case BCACHE_SB_VERSION_CDEV_WITH_FEATURES:
 			printf(" [cache device]\n");
 			break;
 
 		// The second adds data offset support
 		case BCACHE_SB_VERSION_BDEV:
 		case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
+		case BCACHE_SB_VERSION_BDEV_WITH_FEATURES:
 			printf(" [backing device]\n");
 			break;
 
bcache.c +12 -3
index b866271..50e3a88 100644
@@ -18,6 +18,7 @@
 #include "list.h"
 #include <limits.h>
 
+#include "features.h"
 
 #define BCACHE_TOOLS_VERSION	"1.1"
 
@@ -203,11 +204,13 @@ int show_bdevs_detail(void)
 			// These are handled the same by the kernel
 		case BCACHE_SB_VERSION_CDEV:
 		case BCACHE_SB_VERSION_CDEV_WITH_UUID:
+		case BCACHE_SB_VERSION_CDEV_WITH_FEATURES:
 			printf(" (cache)");
 			break;
 			// The second adds data offset supporet
 		case BCACHE_SB_VERSION_BDEV:
 		case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
+		case BCACHE_SB_VERSION_BDEV_WITH_FEATURES:
 			printf(" (data)");
 			break;
 		default:
@@ -257,12 +260,14 @@ int show_bdevs(void)
 			// These are handled the same by the kernel
 		case BCACHE_SB_VERSION_CDEV:
 		case BCACHE_SB_VERSION_CDEV_WITH_UUID:
+		case BCACHE_SB_VERSION_CDEV_WITH_FEATURES:
 			printf(" (cache)");
 			break;
 
 			// The second adds data offset supporet
 		case BCACHE_SB_VERSION_BDEV:
 		case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
+		case BCACHE_SB_VERSION_BDEV_WITH_FEATURES:
 			printf(" (data)");
 			break;
 
@@ -304,7 +309,9 @@ int detail_single(char *devname)
 		fprintf(stderr, "Failed to detail device\n");
 		return ret;
 	}
-	if (type == BCACHE_SB_VERSION_BDEV) {
+	if (type == BCACHE_SB_VERSION_BDEV ||
+	    type == BCACHE_SB_VERSION_BDEV_WITH_OFFSET ||
+	    type == BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
 		printf("sb.magic\t\t%s\n", bd.base.magic);
 		printf("sb.first_sector\t\t%" PRIu64 "\n",
 		       bd.base.first_sector);
@@ -362,14 +369,16 @@ int detail_single(char *devname)
 
 		putchar('\n');
 		printf("cset.uuid\t\t%s\n", bd.base.cset);
-	} else if (type == BCACHE_SB_VERSION_CDEV
-		   || type == BCACHE_SB_VERSION_CDEV_WITH_UUID) {
+	} else if (type == BCACHE_SB_VERSION_CDEV ||
+		   type == BCACHE_SB_VERSION_CDEV_WITH_UUID ||
+		   type == BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
 		printf("sb.magic\t\t%s\n", cd.base.magic);
 		printf("sb.first_sector\t\t%" PRIu64 "\n",
 		       cd.base.first_sector);
 		printf("sb.csum\t\t\t%" PRIX64 "\n", cd.base.csum);
 		printf("sb.version\t\t%" PRIu64, cd.base.version);
 		printf(" [cache device]\n");
+		print_cache_set_supported_feature_sets(&cd.base.sb);
 		putchar('\n');
 		printf("dev.label\t\t");
 		if (*cd.base.label)
features.c +50 -0

60 lines not on this page · raw message

features.h new file +8 -0
new file mode 100644
index 0000000..028b774
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _BCACHE_FEATURES_H
+#define _BCACHE_FEATURES_H
+
+void print_cache_set_supported_feature_sets(struct cache_sb *sb);
+
+#endif
lib.c +13 -7
index efabeb1..29172f5 100644
@@ -192,11 +192,13 @@ int get_cachedev_state(char *cset_id, char *state)
 
 int get_state(struct dev *dev, char *state)
 {
-	if (dev->version == BCACHE_SB_VERSION_CDEV
-	    || dev->version == BCACHE_SB_VERSION_CDEV_WITH_UUID)
+	if (dev->version == BCACHE_SB_VERSION_CDEV ||
+	    dev->version == BCACHE_SB_VERSION_CDEV_WITH_UUID ||
+	    dev->version == BCACHE_SB_VERSION_CDEV_WITH_FEATURES)
 		return get_cachedev_state(dev->cset, state);
-	else if (dev->version == BCACHE_SB_VERSION_BDEV
-		   || dev->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET)
+	else if (dev->version == BCACHE_SB_VERSION_BDEV ||
+		 dev->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET ||
+		 dev->version == BCACHE_SB_VERSION_BDEV_WITH_FEATURES)
 		return get_backdev_state(dev->name, state);
 	else
 		return 1;
@@ -291,6 +293,7 @@ int detail_base(char *devname, struct cache_sb sb, struct dev *base)
 {
 	int ret;
 
+	base->sb = sb;
 	strcpy(base->name, devname);
 	base->magic = "ok";
 	base->first_sector = SB_SECTOR;
@@ -440,13 +443,16 @@ int detail_dev(char *devname, struct bdev *bd, struct cdev *cd, int *type)
 	}
 
 	*type = sb.version;
-	if (sb.version == BCACHE_SB_VERSION_BDEV) {
+	if (sb.version == BCACHE_SB_VERSION_BDEV ||
+	    sb.version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET ||
+	    sb.version == BCACHE_SB_VERSION_BDEV_WITH_FEATURES) {
 		detail_base(devname, sb, &bd->base);
 		bd->first_sector = BDEV_DATA_START_DEFAULT;
 		bd->cache_mode = BDEV_CACHE_MODE(&sb);
 		bd->cache_state = BDEV_STATE(&sb);
-	} else if (sb.version == BCACHE_SB_VERSION_CDEV
-		   || sb.version == BCACHE_SB_VERSION_CDEV_WITH_UUID) {
+	} else if (sb.version == BCACHE_SB_VERSION_CDEV ||
+		   sb.version == BCACHE_SB_VERSION_CDEV_WITH_UUID ||
+		   sb.version == BCACHE_SB_VERSION_CDEV_WITH_FEATURES) {
 		detail_base(devname, sb, &cd->base);
 		cd->first_sector = sb.bucket_size * sb.first_bucket;
 		cd->cache_sectors =
lib.h +4 -0
index 1dd2bfe..9b5ed02 100644
@@ -4,6 +4,7 @@
 #include "list.h"
 
 struct dev {
+	struct cache_sb	sb;
 	char		name[40];
 	char		*magic;
 	uint64_t	first_sector;
@@ -17,6 +18,9 @@ struct dev {
 	char		state[40];
 	char		bname[40];
 	char		attachuuid[40];
+	uint64_t	feature_compat;
+	uint64_t	feature_ro_compat;
+	uint64_t	feature_incompat;
 	struct	list_head	dev_list;
 };
 
Signature · 1 line
2.26.2