block layer: a read or a writeback that fails is refused, not taken as blank or dropped (#187)

A disk header that cannot be read refuses the attach (BLK_EIO, with a
message naming the disk by size); it was taken as blank and offered for
a format. A data read that fails is refused, not cached as zeros. An
eviction whose writeback fails keeps the slot and refuses the new one;
the changes were lost. test_host_blocks, a device whose reads can fail:
7 of 65 failed against the old code, 65 pass (63 at 32 bits). The
test's own errors (a detached device not reopened; two devices declared
out of the later checks' scope) corrected. ident_attach_fail on amd64:
the attach refused; its two failures left are #179's. The five write
results still thrown away are reported, not changed. Gates pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-21 00:37:33 +00:00
parent 20918f9f09
commit 63e9966abc
9 changed files with 1949 additions and 10 deletions
+18 -8
View File
@@ -248,13 +248,14 @@ static inline uint32_t lbn_to_slot_pbn(blk_dev_slot_t *slot, uint32_t lbn) {
static inline uint32_t slot_pbn_to_devblock(uint32_t rel_pbn) { return rel_pbn / BLK_PACK_RATIO; }
static inline uint32_t slot_pbn_pack_offset(uint32_t rel_pbn) { return rel_pbn % BLK_PACK_RATIO; }
/* A read that fails is BLK_EIO, not zeros (#187, 2026-11-20): a block that cannot be read is not
* given as empty. Past the device's end is zeros, as before: there is nothing there to read. */
static int read_devblock_4k(blk_dev_slot_t *slot, uint32_t dev4k, uint8_t *buf4k) {
uint32_t base = devblock4k_to_lba1k(slot, dev4k);
for (uint32_t i = 0; i < 4; i++) {
uint32_t lba = base + i;
if (lba >= slot->total_blkio_blocks_1k) { memset(buf4k + i*1024u, 0, 1024u); continue; }
int rc = blkio_read(slot->dev, lba, buf4k + i*1024u);
if (rc != BLKIO_OK) memset(buf4k + i*1024u, 0, 1024u);
if (blkio_read(slot->dev, lba, buf4k + i*1024u) != BLKIO_OK) return BLK_EIO;
}
return BLK_OK;
}
@@ -306,8 +307,9 @@ static cache_slot_t *cache_get_slot(blk_dev_slot_t *slot, uint32_t dev4k) {
return s;
}
}
/* evict slot[0] FIFO */
(void) cache_writeback(slot, &slot->cache[0]);
/* evict slot[0] FIFO -- only once what it holds is written: a slot whose writeback fails is kept,
* and no slot is given (#187, 2026-11-20; it was evicted whatever the write did, its data lost) */
if (cache_writeback(slot, &slot->cache[0]) != BLK_OK) return NULL;
for (int i = 0; i < DISK_CACHE_SLOTS - 1; i++) slot->cache[i] = slot->cache[i+1];
memset(&slot->cache[DISK_CACHE_SLOTS-1], 0, sizeof(cache_slot_t));
slot->cache[DISK_CACHE_SLOTS-1].valid = 1;
@@ -319,7 +321,10 @@ static cache_slot_t *cache_load_devblock(blk_dev_slot_t *slot, uint32_t dev4k) {
cache_slot_t *s = cache_get_slot(slot, dev4k);
if (!s) return NULL;
if (!s->loaded) {
(void) read_devblock_4k(slot, dev4k, s->data);
if (read_devblock_4k(slot, dev4k, s->data) != BLK_OK) { /* #187: not cached as zeros */
memset(s, 0, sizeof(*s));
return NULL;
}
uint8_t *mr = s->data + META_REGION_OFFSET;
for (uint32_t i = 0; i < BLK_PACK_RATIO; i++) {
meta_from_slice(&s->meta[i], mr + i * META_PER_BLOCK);
@@ -347,8 +352,7 @@ static inline void volmeta_to_buf(const blk_volume_meta_t *in, uint8_t *buf) {
static int read_header_4k(blk_dev_slot_t *slot, uint8_t *buf4k) {
for (uint32_t i = 0; i < 4; i++) {
if (blkio_read(slot->dev, i, buf4k + i*1024u) != BLKIO_OK)
memset(buf4k + i*1024u, 0, 1024u);
if (blkio_read(slot->dev, i, buf4k + i*1024u) != BLKIO_OK) return BLK_EIO; /* #187 */
}
return BLK_OK;
}
@@ -631,7 +635,13 @@ static int blk_commit_format(blk_dev_slot_t *slot) {
* was left untouched. */
static int blk_format_or_load_disk(blk_dev_slot_t *slot) {
uint8_t hdr[BLK_DEVICE_SECTOR] = {0};
(void) read_header_4k(slot, hdr);
/* #187 (2026-11-20): a header that cannot be read refuses the attach. It was taken as blank -- a
* formatted disk offered to its owner for a format -- with no message. */
if (read_header_4k(slot, hdr) != BLK_OK) {
log_message(LOG_WARN, "blk: a disk of %u KiB: its header could not be read -- not attached",
(unsigned)slot->total_blkio_blocks_1k);
return BLK_EIO;
}
volmeta_from_buf(&slot->vol_meta, hdr);
if (slot->vol_meta.magic == 0x53544652u && slot->vol_meta.version == 2 &&