Skip to content

Fix superblock IO error handling and add footer validation#889

Open
xiaoxichen wants to merge 2 commits into
eBay:stable/v7.xfrom
xiaoxichen:fix-superblock-io-error
Open

Fix superblock IO error handling and add footer validation#889
xiaoxichen wants to merge 2 commits into
eBay:stable/v7.xfrom
xiaoxichen:fix-superblock-io-error

Conversation

@xiaoxichen
Copy link
Copy Markdown
Collaborator

This change improves superblock error handling and integrity checking:

  • Fail fast on IO errors during superblock read instead of treating them as fresh/unformatted disks
  • Add footer superblock validation for HDD devices to detect corruption
  • Check header and footer write errors independently to prevent silent failures
  • Add comprehensive unit tests covering all error scenarios

Changes:

  • read_first_block(): Now throws std::system_error on IO errors instead of returning garbage data
  • write_super_block(): Separately validates header and footer writes with independent error checking
  • sanity_check(): New method that validates footer consistency on HDD devices by comparing header and footer superblocks using full memcmp
  • Added 6 unit tests covering IO errors, corruption detection, and footer validation scenarios

Use release assert instead of exceptions for superblock IO errors

Per review feedback, replace exception throws with HS_REL_ASSERT for all superblock IO errors to ensure immediate crash on failure:

  • read_first_block(): Use HS_REL_ASSERT instead of throwing std::system_error
  • sanity_check(): Use HS_REL_ASSERT for header/footer read errors and mismatch
  • Update tests from ASSERT_THROW to ASSERT_DEATH to verify crash behavior

@xiaoxichen
Copy link
Copy Markdown
Collaborator Author

@JacksonYao287 this is a replacement PR for #866

could you please take a review

@xiaoxichen xiaoxichen requested a review from JacksonYao287 June 4, 2026 13:41
m_super_blk_in_footer = m_pdev_info.mirror_super_block;

// Validate footer superblock consistency if mirroring is enabled
sanity_check();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we first boot and format a single device in homestore

  uint32_t DeviceManager::format_single_device(dev_info& dinfo) {

      auto pdev_id = populate_pdev_info(dinfo, attr, m_first_blk_hdr.system_uuid,
                                        fblk->this_pdev_hdr);

      auto pdev = std::make_unique< PhysicalDev >(dinfo, oflags, fblk->this_pdev_hdr);

     // here, after pdev is created , we then write the superblk.
      pdev->write_super_block(buf, sb_size, hs_super_blk::first_block_offset());
  }

and we will do sanity_check in the constructor of pdev

 PhysicalDev::PhysicalDev(const dev_info& dinfo, int oflags,
                            const pdev_info_header& pinfo) {
      // ...
      m_super_blk_in_footer = m_pdev_info.mirror_super_block; // HDD → true

      // sanity_check()  here is called before super_blk is written.
      sanity_check();
  }

if we get brand new HDD, that`s fine since all the data(footer and header) we read is 0.

however, if we reuse some HDD from the decommissioned clusters, there probably some legacy data on them. so when we read header and footer and then do memcmp, it is probably crash at line 174 since they are probably different legacy data.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only this concerning case

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch

@xiaoxichen xiaoxichen force-pushed the fix-superblock-io-error branch from 8ef07a3 to 7bf160a Compare June 5, 2026 10:27
This change improves superblock error handling and integrity checking:

- Fail fast on IO errors during superblock read instead of treating them as
  fresh/unformatted disks
- Add footer superblock validation for HDD devices to detect corruption
- Check header and footer write errors independently to prevent silent failures
- Add comprehensive unit tests covering all error scenarios

Changes:
- read_first_block(): Now throws std::system_error on IO errors instead of
  returning garbage data
- write_super_block(): Separately validates header and footer writes with
  independent error checking
- sanity_check(): New method that validates footer consistency on HDD devices
  by comparing header and footer superblocks using full memcmp
- Added 6 unit tests covering IO errors, corruption detection, and footer
  validation scenarios

Use release assert instead of exceptions for superblock IO errors

Per review feedback, replace exception throws with HS_REL_ASSERT for all
superblock IO errors to ensure immediate crash on failure:

- read_first_block(): Use HS_REL_ASSERT instead of throwing std::system_error
- sanity_check(): Use HS_REL_ASSERT for header/footer read errors and mismatch
- Update tests from ASSERT_THROW to ASSERT_DEATH to verify crash behavior
On first boot (format_devices path), all devices have invalid headers on
disk when PhysicalDev is constructed — before write_super_block is called.
The original code blindly memcmp'd header vs footer on garbage data,
causing a crash on HDD devices where mirror_super_block=1.

In load_devices, a device with an invalid header goes to pdevs_to_format
and then format_single_device. At that point m_first_blk_hdr (and thus
m_pdev_info.system_uuid) is already populated from other valid devices in
the cluster. So if the footer on disk is valid and its system_uuid matches,
it means the footer survived but the header was corrupted — not a fresh
device. If the footer uuid does not match (or footer is invalid), it is
leftover/garbage data and safe to treat as first boot.

New logic:
- Header valid: compare header and footer as before
- Header invalid, footer valid + uuid matches cluster: header corruption, assert
- Otherwise: first boot / leftover data, skip validation
@xiaoxichen xiaoxichen force-pushed the fix-superblock-io-error branch from 7bf160a to 6dd7079 Compare June 5, 2026 10:29
@xiaoxichen
Copy link
Copy Markdown
Collaborator Author

@JacksonYao287 comment addressed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants