Skip to content

fix(kvdb): rebuild invalid sector headers during initialization - #446

Draft
brightness-810 wants to merge 1 commit into
armink:masterfrom
brightness-810:fix/rebuild-invalid-sector-headers-during-initialization
Draft

brightness-810 wants to merge 1 commit into
armink:masterfrom
brightness-810:fix/rebuild-invalid-sector-headers-during-initialization

Conversation

@brightness-810

@brightness-810 brightness-810 commented Nov 17, 2026 •

Copy link
Copy Markdown

What this changes

An interrupted sector erase or header write can leave one or more sector headers invalid while other sector headers remain valid. During initialization, the KVDB may then fail to traverse the sector layout reliably.

When not_formatable is enabled, this change prechecks all sector headers before modifying flash. It aborts on a read error and preserves the flash if no valid sector header remains. If at least one valid header remains, it erases and reformats sectors whose headers are invalid, then recalculates the oldest sector address.

This repairs headers only; it does not inspect the data area of a sector before erasing it. A sector with an invalid header may contain unique KV data, so recovery can lose that data. The warning is intentional: this is a destructive recovery policy, not proof that the sector contents are disposable.

Related issue

Closes #442

Test closure

  • Added or updated cases in tests/fdb_kvdb_tc.c or tests/fdb_tsdb_tc.c
  • New cases are registered in the UTEST_UNIT_RUN(...) list of testcase()
  • Cases cover boundaries (sector full / GC triggered / blob crossing write granularity / init check failure)
  • Bug fix: the case failed before the fix and passes after, and its name carries the issue number
  • No new case needed, reason: N/A; this changes initialization-time flash recovery and needs regression coverage

At the current PR head, the QEMU BSP build succeeds, but the KVDB UTEST fails for all six FDB_WRITE_GRAN values. TSDB passes. The available CI output does not include the failing assertion, so the failure has not yet been attributed to this commit specifically.

Write granularity covered

  • CI passed for FDB_WRITE_GRAN = 1 / 8 / 32 / 64 / 128 / 256
  • Local manual verification only, granularities: N/A
  • Not verified, reason and risk: CI attempted all six values; KVDB UTEST failed for each, and the failing assertion is unavailable.

Data layout and compatibility

  • KV/TSL node format unchanged
  • Node format changed: N/A
  • Public struct or API in inc/flashdb.h unchanged
  • API or config macro unchanged; documentation update: N/A

Power-loss semantics

  • Not on a write path
  • On a write path, covered by the fdb_xxx_deinit -> fdb_xxx_init reboot simulation
  • On a write path, not fully covered by UTEST, verification method: inject power loss during sector erase and each sector-header write, reboot, then verify that valid KV data is preserved or that recovery fails without further erasing.

@brightness-810
brightness-810 marked this pull request as draft November 19, 2026 03:26

This branch has not been deployed

No deployments
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.

[KVDB 掉电恢复] 禁止自动格式化时,全擦除扇区缺少有效头导致整库初始化失败

1 participant