[PATCH 2/2] Make format-device complain if device is too small

Valerie Aurora val at versity.com
Tue Jun 24 12:17:42 PDT 2025


It was possible to format a device to small to use. Pull out the size
checking code and share it between device formatting and device
initialization code, and print out some useful debug info.

Signed-off-by: Valerie Aurora <val at versity.com>
---
 cli/format-device.c |  5 +++++
 devd/bstore.c       | 24 ++++++++++++++----------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/cli/format-device.c b/cli/format-device.c
index 3d6550d..34daeeb 100644
--- a/cli/format-device.c
+++ b/cli/format-device.c
@@ -20,6 +20,7 @@
 #include "shared/lk/crc64.h"
 #include "shared/lk/math.h"
 
+#include "shared/bstore.h"
 #include "shared/devfd.h"
 #include "shared/format-block.h"
 #include "shared/format-dev.h"
@@ -97,6 +98,10 @@ static int format_device_func(int argc, char **argv)
 	cmt->layout.details_blocks = cpu_to_le64(details);
 	cmt->layout.storage_blocks = cpu_to_le64(store);
 
+	ret = bstore_check(cmt);
+	if (ret < 0)
+		goto out;
+
 	uuid_generate_random(uuid);
 	uuid_unparse(uuid, uuid_str);
 	BUILD_BUG_ON(NGNFS_UUID_SIZE != sizeof(uuid_t));
diff --git a/devd/bstore.c b/devd/bstore.c
index ce870d1..dc25427 100644
--- a/devd/bstore.c
+++ b/devd/bstore.c
@@ -14,6 +14,7 @@
 #include "shared/lk/string.h"
 #include "shared/lk/overflow.h"
 
+#include "shared/bstore.h"
 #include "shared/dtracef.h"
 #include "shared/format-block.h"
 #include "shared/format-dev.h"
@@ -1235,22 +1236,25 @@ static int init_journal(struct bstore_instance *inst)
 	if (check_add_overflow(inst->commit_blocks, journal_blocks, &total) ||
 	    check_add_overflow(summary_blocks, total, &total) ||
 	    check_add_overflow(details_blocks, total, &total) ||
-	    check_add_overflow(inst->storage_blocks, total, &total) ||
-	    total > block_total_blocks()) {
-		ret = -EINVAL;
+	    check_add_overflow(inst->storage_blocks, total, &total)) {
+		printf("%s: overflow in calculating total blocks\n", __func__);
+		bstore_print_commit_block(cmt);
+		ret = -EUCLEAN;
 		goto out;
 	}
 
-	/* arbitrary tiny mins for commits/journal, ulong max for the size of the stable_ht */
-	if (inst->commit_blocks < 256								||
-	    journal_blocks < 256								||
-	    journal_blocks >= ULONG_MAX								||
-	    summary_blocks < DIV_ROUND_UP(details_blocks, NGNFS_DEV_SUMMARIES_PER_BLOCK)	||
-	    details_blocks < DIV_ROUND_UP(inst->storage_blocks, NGNFS_DEV_DETAILS_PER_BLOCK)) {
-		ret = -EINVAL;
+	if (total > block_total_blocks()) {
+		printf("%s: error in block accounting: total blocks %llu > device total %llu\n",
+		       __func__, total, block_total_blocks());
+		bstore_print_commit_block(cmt);
+		ret = -EUCLEAN;
 		goto out;
 	}
 
+	ret = bstore_check(cmt);
+	if (ret < 0)
+		goto out;
+
 	inst->stable_ht = htable_alloc(journal_blocks);
 	if (!inst->stable_ht) {
 		ret = -ENOMEM;
-- 
2.49.0




More information about the ngnfs-devel mailing list