Skip to content

fix(kvdb): skip a status-only first KV header when caching free space - #445

Draft
brightness-810 wants to merge 1 commit into
armink:masterfrom
brightness-810:fix/skip-first-KV-header-when-caching-free-space
Draft

brightness-810 wants to merge 1 commit into
armink:masterfrom
brightness-810:fix/skip-first-KV-header-when-caching-free-space

Conversation

@brightness-810

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

Copy link
Copy Markdown

What this changes

An interrupted first KV write can leave the trailing-FF boundary inside its status table. Caching this boundary as free space can let recovery allocate a replacement inside the incomplete header.

A subsequent search may mark the residual header ERR_HDR at a byte that also belongs to the replacement KV's DELETED status slot. Committing the replacement as WRITE does not clear that marker; deleting the source can then leave no readable copy.

When the initial cached FF boundary falls within the first KV header's status-table interval, advance the allocation address past the complete header. This keeps the cache-hit condition, remaining-capacity calculation, and on-flash format unchanged, and adds no flash reads or writes.

What this changes

During initial free-space caching, skip the first KV header only when the trailing-FF boundary falls inside its status-table interval. A fully blank data area is unchanged.

This changes only the in-memory allocation address. It does not modify persistent status encoding, the on-flash format, public structures, or APIs.

Related issue

Closes #441

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 behavior change requires regression coverage

Local host RAM-NOR tests covered the reconstructed case with caches disabled and enabled, including a deinit/init reopen. A controlled STM32 debug replay also verified the adjusted allocation address and a subsequent reboot. These tests have not been ported to the upstream UTEST suite.

The PR's QEMU KVDB UTEST failed for all six FDB_WRITE_GRAN values. The available log does not include the failing assertion, so this change is not yet validated by CI.

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 ran all six values, but the KVDB UTEST failed for each; the failing assertion is not available.

Data layout and compatibility

  • KV/TSL node format unchanged
  • Node format changed: N/A
  • Public struct or API in inc/flashdb.h changed, compatibility impact: N/A
  • API or config macro changed, and docs/ plus docs/zh-cn/ are updated: N/A

Power-loss semantics

  • Not on a write path
  • On a write path, covered by the fdb_kvdb_deinit -> fdb_kvdb_init reboot simulation; also exercised in a controlled STM32 debug replay
  • On a write path, not coverable by utest, verification method: N/A

An interrupted first KV write can leave the trailing-FF boundary
inside its status table. Caching this boundary as free space allows
recovery to allocate a replacement inside the incomplete header.

A subsequent search can mark the residual header ERR_HDR at a byte
that also belongs to the replacement's DELETED slot. Committing WRITE
does not clear that marker, and deleting the source leaves no readable
copy.

Advance the initial cached free address past the complete first KV
header when the FF boundary falls within its status-table interval.
Keep the cache-hit condition, capacity calculation, and on-flash
format unchanged, without adding flash reads or writes.
@brightness-810
brightness-810 marked this pull request as draft November 19, 2026 03:06

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 掉电恢复] 首槽状态残留的 FF 边界被缓存为可分配地址,可能导致状态表重叠

1 participant