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:
- A page that a power cut leaves erased in every bit looks unwritten, so the boot programs it again.
- A torn page that passes every check is taken for a committed one.
- An epoch whose metadata can no longer be verified is sealed. It stays readable but takes no new records until you begin another.
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:
nandlog_store_record(), when the new record does not fit or its timestamp steps back by more thanNANDLOG_TIMESTAMP_TOLERANCE_MS. It commits the previous page.nandlog_flush(true), which commits a partial page.nandlog_begin_epoch(), which commits to the old epoch before it begins the new one.nandlog_begin_reading(), so that a read never serves a page from RAM.
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:
- Every durable record is delivered intact from the epoch it was logged in, until a later epoch's write head sweeps over its block.
- A torn page is never served damaged. A read reports it as a gap, a zero-length page that carries its sequence number, however the page later reads or drifts.
- No committed page that still reads is erased.
- Nothing is programmed a second time, except as described under Pages that read blank.
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.
- A boot that finds such a page at the write head programs it again. That happens when a power cut stopped a program before it changed a bit, when the part refused a program without changing a bit and the slot recording the refusal was lost, or when a torn page drifted back to erased before the next boot listed it. These are the residual second programs. On the Winbond part, a program stopped within its first 20 µs leaves the page reading erased in every bit, and in its soak one such page, programmed again, read back whole at the commit and was unreadable a boot later.
- In the log region, each one lands on the page at the write head, which no read has served.
NANDLOG_PASS_OVER_HEAD_AT_BOOTrules them out there, at a cost paid at every boot (choosing it). - A metadata or table copy that a power cut stopped before it changed a bit is written again by the next write to its pair.
- The model assumes that a committed page never comes to read erased in every bit. A boot looks past one that a page of the epoch closely follows, but would take one directly below the write head for the head.
Torn pages that read whole.
- A torn page whose header CRC, payload CRC, and ECC all check is taken for a committed page, so a read may serve it although its commit never returned. The model assumes that such a page does not drift afterward.
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:
- Its passed-over list is nearly full. With
NANDLOG_MAX_EPOCH_DETAILS_BYTESat its default of 512, a slot holds 880 runs of positions on a 4 KB page and 368 on a 2 KB page, and the epoch is sealed while 4 are still free. WithNANDLOG_PASS_OVER_HEAD_AT_BOOT, most boots during logging add a run. - No copy of the newest slot the epoch acted on holds its list whole. The list and the details are gone.
- The bad-block table no longer lays the epoch out as it was written (below).
- Its slot carries longer details than
NANDLOG_MAX_EPOCH_DETAILS_BYTESallows, after a build lowered that setting.
Lost metadata.
- One unreadable page of the metadata ring or the bad-block table changes nothing, because each record is on two pages. An epoch whose details alone are lost goes on without them.
- If both copies of a slot are lost outright, the boot goes by the slot before it. An epoch begun since is recovered from its own pages, without its details, whatever the older slot says. Positions that only the lost slot listed are guarded only by the head search's check just past the head, and a failed program that only it recorded may be programmed again if it reads blank.
- Pages of a newer epoch that a boot cannot place stop logging, and the next epoch begun is numbered past them.
- If the whole ring is lost, the next epoch begun is numbered past every page left on the part, so the epochs before it are never adopted or counted as its own. Pages of an earlier format belong to no epoch, whatever their numbers.
- If both copies of the table's newest version are lost, the driver loads the version before it. If every version is lost, it rebuilds the table from the blocks' marks.
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.
- Reads go ahead as best they can, and may miss pages or serve a relocated page twice under its one
sequence number. Every boot logs an error, and
nandlog_epoch_info()setslayout_unverified, until a new epoch begins. That epoch starts past the last block holding the sealed epoch's pages, so none of them is erased. - A head search that reads a page of the epoch below its own position also seals the epoch, since a block under committed pages was retired without the log.
- The one retirement accepted without a seal is the block of a failed write head that the slot recorded, and only with the head at or past the failed page, because a half-programmed mark can read differently from one boot to the next.
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.
- Outside the current epoch, that costs one block of capacity.
- Inside it, the block's pages go out of reach and the epoch is sealed, with nothing erased. The newest slot catches this if it counts the block in the epoch's span, and the head search catches it otherwise.
- In the write head's block, when the newest slot does not yet count it, nothing shows. Its pages go out of reach and their sequence numbers are used again, the one case in which a sequence number can come to hold other records.
The Winbond remap table.
- A retirement is an entry in the part's own 20-entry remap table, which its factory-bad blocks share. Once the table is full, a failing block cannot be retired, and the log stops taking pages. The library assumes, and does not verify, that the part writes an entry whole or not at all across a power cut.
Earlier epochs.
- An earlier epoch lasts only until the write head sweeps over it. A block of it that the erase-ahead window cannot erase is retired, and so no longer read, a little before that.
Outside the model.
- Corruption that both the part's ECC and CRC-32 miss.
- A
nandlog_port_fatal()that returns, or a SPI transfer that neither completes nor ends innandlog_port_fatal(). - A part that neither completes a page program nor reports its failure, on which the bad-block table's atomicity rests.
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.
| Failure | Response |
|---|---|
| A program fails, or the page reads back wrong or only at the ECC limit | The 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 copy | It is retired too, and the next good block is tried, up to NANDLOG_PAGE_PLACEMENT_ATTEMPTS blocks. |
| A page to relocate cannot be read | It moves as a tombstone. |
| An erase fails | It is tried up to NANDLOG_BLOCK_ERRORS_BEFORE_REMOVAL times, then the block is retired. |
| Power lost mid-program | The next boot lists the torn page and serves it as a gap. |
| Power lost mid-erase | The 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 version | The old state stands until the new one is complete, and the boot rewrites what it went by. |
| Metadata unreadable, worn, or unverifiable | One 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 read | The page is served as a gap. |
| The log region fills | Records 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 recorded | Logging stops with NANDLOG_ERR_UNWRITABLE and a call to nandlog_port_unwritable(), until a reboot or a new epoch. |
| The part stops responding | A 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).