nandlog v1.1

Durability

What nandlog guarantees across a power cut, how it keeps that guarantee, and where the guarantee ends. Read it before you trust the log with data you cannot collect again.

Once the call that commits a page returns, every record in that page survives a power cut at any later instant. That includes a power cut in the middle of a program, an erase, a relocation, or a metadata write, and a second one during the reboot that follows. A page that a power cut tore reads back as a gap that carries its sequence number, never as damaged records. Records still in RAM are lost, as at any reset.

The guarantee has limits. The ones that matter most are these:

The simulator checks this contract at every program and erase of its test traces. On the Alliance AS5F18G04SND, an unattended soak stopped 2,059 of the 1.0 library's programs and erases with the RESET command, with no violation over 1,443 verified boots. On the Winbond W25N01GWZEIG, a soak of the 1.1 library stopped 2,017 programs and erases and found one violation, a residual second program (below). RESET is a proxy for a brown-out, not a power cut, and tests that remove power from the board are still to come.

If you are deciding whether to use the library, read The contract and Where the guarantee ends, and leave the rest for when you integrate it.

The contract

A record is durable once the call that commits its page has returned. Four calls commit a page:

Records still in RAM when power fails are lost, so flush on a timer. A commit that fails with NANDLOG_ERR_UNWRITABLE or NANDLOG_ERR_FULL also loses its records, counted in records_lost.

After a power cut at any instant, at every later boot:

Sequence numbers are identities. A page's sequence number is its position in the epoch. What a read has served under an epoch and sequence number is what that number holds from then on, or a gap if the part later loses the page, never other records. A host can therefore ask again for a missing page by number (retransmission).

How the log keeps it

The log trusts only what it can verify, and a boot writes its decisions down before it acts on them, so that a later boot decides the same way. The byte layouts are on the On-Flash Format page.

Write once, verify. Each page is programmed once, at the write head, and read back byte for byte. A page that fails is recorded in the metadata ring and never programmed again, and its block is relocated and retired. Every page carries its epoch, its sequence number, and CRC-32s over its header and payload (page header). An unreadable page in a relocated block becomes a tombstone, a valid header with no payload (tombstones).

Only a blank page marks the head. A boot finds the write head, the position the next page goes to, by a binary search in which only a page erased in every bit counts as past the head, so unreadable pages, retired blocks, and torn programs below the head cannot mislead it (finding the write head).

Torn pages are listed. A torn page is one whose program a power cut interrupted. The first boot that finds torn pages directly below the head lists their positions as passed over in the epoch's metadata slot before it commits anything after them. A listed position counts as written whatever it later reads, so a torn page that drifts back to blank is never taken for the head.

Metadata written twice and read once. The metadata ring, the first eight blocks of the part, holds a slot for each epoch (metadata slot). Each slot is written to both pages of a pair. A boot reads each copy once, since a torn page can read differently each time, and judges it by every checksum it carries. A newest slot that is not whole in both copies is written again into a fresh block before the boot acts on it (finding the newest epoch).

Nothing below the head is erased. No erase may touch a block that holds the current epoch's committed pages. Every boot erases the blocks ahead of the head again, because an interrupted erase can leave a block that reads erased but programs unreliably. A boot that finds the head part-way into a block, with nothing whole of the epoch below it there, treats the block as suspect. It moves the head back to the block's start if the block was never entered, and otherwise erases it again and gives each passed-over position in it a tombstone.

Copy, then retire. A relocation copies the failing block's pages to the same offsets in the next good block, verifies them, and only then retires the failing block, so a power cut part-way through leaves copies the head search still reads correctly. Likewise, nandlog_begin_epoch() erases nothing for a new epoch until its slot is on the part.

Where the guarantee ends

The contract holds with the exceptions below, grouped by kind.

Pages that read blank. No read can tell a page that is erased in every bit from one that was never programmed.

Torn pages that read whole.

Sealed epochs. A sealed epoch stays readable but refuses records with NANDLOG_ERR_SEALED, and the log calls nandlog_port_unwritable() at every boot until you begin a new epoch. The log seals an epoch in four cases:

Lost metadata.

A table that disagrees with the ring. Each slot records how many blocks were retired and a fingerprint of which of the epoch's blocks were. A boot whose bad-block table lays the epoch out differently seals it.

Rebuilds from the marks (Alliance). The Alliance driver rebuilds its table from the spare-area marks on a new part, after every copy of the table is lost or reset, and at every boot in degraded mode. A rebuild takes any byte but FFh for a mark, so that a half-programmed mark reads the same at every boot. The marks lie outside the ECC, so one stray programmed bit in a good block's mark retires that block.

The Winbond remap table.

Earlier epochs.

Outside the model.

What the log does when the part fails

The log keeps what it can verify, retires what it cannot trust, and reports the rest as a gap or a status code.

FailureResponse
A program fails, or the page reads back wrong or only at the ECC limitThe ring records the page, which is never programmed again. The block's pages move to the next good block, then or at the next boot, and the block is retired.
A relocation target refuses its erase or a copyIt is retired too, and the next good block is tried, up to NANDLOG_PAGE_PLACEMENT_ATTEMPTS blocks.
A page to relocate cannot be readIt moves as a tombstone.
An erase failsIt is tried up to NANDLOG_BLOCK_ERRORS_BEFORE_REMOVAL times, then the block is retired.
Power lost mid-programThe next boot lists the torn page and serves it as a gap.
Power lost mid-eraseThe next boot erases the blocks ahead of the head again and handles a suspect head block.
Power lost mid-relocation, or mid-write of a slot or table versionThe old state stands until the new one is complete, and the boot rewrites what it went by.
Metadata unreadable, worn, or unverifiableOne bad copy changes nothing, and a copy at the ECC limit is written again (Wear). Metadata that cannot be verified seals the epoch (Limits).
A payload CRC fails on readThe page is served as a gap.
The log region fillsRecords are refused with NANDLOG_ERR_FULL until a new epoch. Nothing of the epoch is erased.
No block will retain a page, or a retirement cannot be recordedLogging stops with NANDLOG_ERR_UNWRITABLE and a call to nandlog_port_unwritable(), until a reboot or a new epoch.
The part stops respondingA bounded busy-wait, then nandlog_port_fatal().
The table holds more than the datasheet's 80 bad blocks (Alliance)A warning, and the table is kept.
The reserve cannot hold the table (Alliance)The table moves to a pair of log-region blocks, or, with none to spare, to the blocks' marks (Bad blocks).
The remap table is full (Winbond)A failing block cannot be retired, so logging stops.

The consistent rule is that a page that cannot be trusted is a gap, and a gap is not the end of the log. Reads step over it, and offload reports it as a zero-length page with its sequence number, so a host can ask for it again.

Bad blocks

To retire a block, the log moves its pages, asks the chip driver to retire it, and records the retirement in the metadata ring. A driver must make a retirement survive a power cut whole or not at all, and must not report the block as bad until then (the driver's side).

The Alliance table. The AS5F18G04SND driver keeps its table in the first eight blocks of a 16-block reserve at the top of the array. Each version goes to both pages of the next pair and is read back before any older version is erased, so a power cut leaves either the old table or the new one. Every retired block also gets 00h in the first spare byte of its first page, where the factory puts its marks. Marks are read with ECC off, and every boot programs again any mark that does not read exactly 00h.

When the reserve cannot hold the table. If the reserve's blocks fail, a boot gives the driver an aligned pair of blocks from the top of the log region, outside the current epoch and holding nothing a read could serve. The pair is retired and holds the table from then on, and later boots find it by reading one or two pages of each block down to 48 blocks below the reserve (the reserve).

Degraded mode. With no such pair to be had, the marks are the only record. A retirement is committed by its block's mark, programmed and read back as exactly 00h after the block's pages have moved. Every boot rebuilds the table from one raw read of every block's mark and looks for a pair again. Reading all 4,096 marks of the 8 Gbit part this way took about 1.4 s. A block that will not take even its mark gets the table a pair in the same call, and only with no pair anywhere does the log stop.

The recovery utility. nandlog_reset_bad_block_table() discards the table so that the next boot rebuilds it from the marks. The Alliance driver first programs every mark again, and keeps the table if any will not read 00h. It never erases a block that may carry a mark, and erases the newest version last, so neither a power cut nor a block that refuses an erase can roll the table back.

The Winbond remap table. The W25N01GWZEIG keeps retirements in its own one-way, 20-entry remap table. Its first boot remaps every factory-bad block, so those entries are not available to blocks that fail later, and the recovery utility cannot clear the table.

Wear on the metadata

Nothing rewrites a page in place. A log page that reads back corrected only at the ECC limit is counted in reads_at_ecc_limit and left to the part's ECC. The metadata is read far harder. Every boot reads about ten pages of the newest ring block, and about as many of the newest table block, so at one boot a minute each block takes about five million page reads a year. Both copies of each record sit in that one block, so they wear together. A boot that reads either copy of the newest slot or table version only at the ECC limit therefore writes it again, unchanged, into a fresh block. Nothing that reads back at the limit counts as written, so a page stuck there costs one boot one write, not every boot.

This relies on the part reporting the limit before a page goes past it. The Alliance part reports a read corrected at the limit, one bit error short of an uncorrectable page. That covers errors that build up between boots, such as read disturb, but not those of a long period unpowered, which are left to the part's retention rating. The Winbond part has no such verdict. On it, and on any part, check the read-disturb rating against your boot rate.

Read disturb can also program one bit of a table block's mark. The driver takes a reserve block with a one-bit mark as unmarked if it holds a newer version than the one loaded and that version does not list it as retired, and then writes the newer version again.

The head search's first four probes move from boot to boot with the ring's slot numbering and NANDLOG_BOOT_SALT, so that frequent boots do not keep reading the same pages. A clean boot leaves the numbering unchanged, so a device that boots often should set NANDLOG_BOOT_SALT to a value that changes every boot, such as a reset-surviving boot counter or a random number. Every boot also erases the blocks ahead of the write head again, so a device that reboots far more often than it fills a block should set NANDLOG_ERASE_AHEAD_BLOCKS to 0 (Configure).

How this is tested

In the simulator (Testing), the crash-point campaign cuts power at every program and erase of 14 traces, under four models of a torn operation, again in the recovering boot and the logging after it, and then lets weak pages drift. A fuzzer and a bounded-exhaustive check cover random sequences, every combination of page states below the write head of a toy part, and every cut in every metadata write path.

On hardware, the 1.0 library has run on an Ambiq Apollo4 Plus (Cortex-M4F, 48 MHz SPI, on-die ECC on) with the Alliance AS5F18G04SND (hardware tests). nandlog_interrupt stopped 672 programs and 42 erases with the RESET command (FFh), and a RESET at 50 µs or later into a BLOCK ERASE never shortened it. In the unattended soak (nandlog_selfcut and nandlog_selfcut_inject), the firmware stopped its own programs and erases at random with RESET, then reset the MCU, and tests/powercut_check.py checked the contract at every boot. It passed 2,059 interrupted operations (1,579 programs and 480 erases) over 1,443 verified boots with no violation, including 8 injected failures that retired blocks and relocated the write head.

RESET is a proxy for a brown-out, not a power cut. The campaign that removes power from the board (nandlog_powercut, driven by a relay or a switched USB hub) has not yet been run on 1.0, and because the Alliance part always finishes an erase after RESET, its soak tears none. The Winbond W25N01GWZEIG tears an erase stopped about 50 µs in, and its soak cut some there (see Testing).