diff --git a/bcache.c b/bcache.c index fbaaa0ef..7e061649 100644 --- a/bcache.c +++ b/bcache.c @@ -530,12 +530,34 @@ int dev_open(const char *dev, bool wipe_bcache) return fd; } +static unsigned min_bucket_size(int num_bucket_sizes, unsigned *bucket_sizes) +{ + int i; + unsigned min = bucket_sizes[0]; + + for (i = 0; i < num_bucket_sizes; i++) + min = bucket_sizes[i] < min ? bucket_sizes[i] : min; + + return min; +} + +static unsigned node_size(unsigned bucket_size) { + + if (bucket_size <= 256) + return bucket_size; + else if (bucket_size <= 512) + return bucket_size / 2; + else + return bucket_size / 4; +} + void write_cache_sbs(int *fds, struct cache_sb *sb, unsigned block_size, unsigned *bucket_sizes, int num_bucket_sizes) { char uuid_str[40], set_uuid_str[40]; size_t i; + unsigned min_size = min_bucket_size(num_bucket_sizes, bucket_sizes); sb->offset = SB_SECTOR; sb->version = BCACHE_SB_VERSION_CDEV_V3; @@ -552,11 +574,13 @@ void write_cache_sbs(int *fds, struct cache_sb *sb, for (i = 0; i < sb->nr_in_set; i++) { struct cache_member *m = sb->members + i; - sb->uuid = m->uuid; - if(num_bucket_sizes <= 1) + if (num_bucket_sizes <= 1) sb->bucket_size = bucket_sizes[0]; else sb->bucket_size = bucket_sizes[i]; + SET_CACHE_BTREE_NODE_SIZE(sb, node_size(min_size)); + + sb->uuid = m->uuid; sb->nbuckets = getblocks(fds[i]) / sb->bucket_size; sb->nr_this_dev = i; sb->first_bucket = (23 / sb->bucket_size) + 1;