The SD card¶
The drive in the field logs to a Secure Digital (SD) card of 32 GB. A technician takes the card out after a fault, puts it into a laptop and copies the log off it. The laptop must see an ordinary card with files on it, while the drive writes the log at the speed of raw sectors. This chapter shows how the drive sets the card up and why the ring's segments match the card's allocation unit. It also shows how the drive notices what a laptop did.
The ring as a file¶
The card carries an exFAT volume, the file system of SD cards over 32 GB, which the drive uses on smaller cards too.
The ring lives in one file of it, logring.wlr, the ring file. The drive creates the file once, at its full size
and in one piece, and then writes the ring into its sectors directly. It never touches the file system while it logs, so
a power cut cannot damage the file system.
The rest of the card stays an ordinary volume. It holds a README.txt that says what the ring file is, and 128 MiB of
free room for files the drive or a person puts there. The field rw_bytes of FormatOptions sets that room, and
the ring file gets the rest in whole segments. A laptop reads the ring file like any other file: the logring tool
and the Python package read it from the mounted card.
logring_figures from a format
of a simulated card that keeps only the sectors written to it.On the drive's card the ring file starts at byte 146 800 640, 140 MiB into the card, and is 30 376 MiB long. Its first segment holds the superblock twice, in slot A at its start and in slot B half a segment in. The writer writes the two slots in turn, so a power cut during one leaves the other. The meta area follows with 4 segments, then the data ring with 7589 segments.
Segments match the allocation unit¶
An SD card does not write single sectors in place: its controller erases and programs flash in large units. The SD specification calls such a unit the allocation unit and lets the card report its size. The speed a card's class promises holds for a host that fills one allocation unit from start to end before it moves to the next.
logring therefore makes a segment the allocation unit, or a multiple of it when the unit is under 4 MiB. It also starts the ring file on an allocation unit boundary. The writer fills each segment from its first block to its last, in flushes, and only then opens the next one. The drive's card reports 4 MiB, so its segments are 4 MiB. A card that reports nothing gets 4 MiB segments.
The segment size is fixed at the format and kept in the superblock. Each later mount reads it from there, so the firmware never needs to know it.
Mounting the card¶
At every boot the drive checks that the ring file is still where it put it, before it writes a single sector. A
laptop may have done anything to the card since: deleted the file, copied it back, defragmented the volume. Writing
by sector address into a file that moved would destroy other files. The drive's mount_sd handles every case:
/// Mount the drive's ring on the SD card `dev`. A card whose ring file checks out goes on logging. A ring file
/// that a PC moved, deleted or resized is made anew, and the other files stay. A card without a volume the drive
/// accepts is formatted, after the card check that `opts` asks for.
pub async fn mount_sd<'a, D: BlockDevice, C: Clock>(
dev: D,
card: Option<&CardInfo>,
opts: &FormatOptions<'_>,
clock: &mut C,
bufs: &'a mut Buffers<BUF, TRACKS>,
) -> Result<(Drive<'a, ExfatRing<D>>, Mounted), SdError<D::Error>> {
let (formatted, how) = match open(dev).await.map_err(SdError::Card)? {
Opened::Bound(bound) => {
let (ring, cfg) = bound.into_parts(drive::config());
let drive = Drive::mount(ring, bufs, cfg).await.map_err(SdError::Ring)?;
return Ok((drive, Mounted::Bound));
}
Opened::Unbound(Unbound::Volume(_), dev) => {
let f = format_card(dev, card, opts, clock, bufs.stage()).await;
(f.map_err(SdError::Format)?, Mounted::Formatted)
}
Opened::Unbound(why, dev) => {
let f = recreate_ring(dev, card, opts, 0).await;
(f.map_err(SdError::Format)?, Mounted::Recreated(why))
}
};
let (ring, cfg) = formatted.into_parts(drive::config());
let drive = Drive::format(ring, bufs, cfg)
.await
.map_err(SdError::Ring)?;
Ok((drive, how))
}
The function exfat::open mounts the volume, finds the ring file and compares it with the superblock's
binding. The binding holds the volume's serial number, the file's first cluster, its length and its first
sector. A ring file that matches is bound, and the drive mounts the ring in it as on a raw card. Anything else is
unbound, and exfat::Unbound says why:
| Reason | What the drive finds |
|---|---|
Volume |
no exFAT volume it accepts: a camera's card, a damaged volume |
Missing |
no logring.wlr in the root directory: deleted, renamed or moved into a folder |
NotAFile |
logring.wlr is a directory |
NotContiguous |
the file has a cluster chain: a laptop rewrote or defragmented it |
Size |
the file is empty, not whole sectors, or its valid length differs from its length |
NotAllocated |
some of the file's clusters are free in the allocation bitmap |
NoHeader |
neither superblock slot decodes: another file took the place |
OtherCard |
the binding names another volume: the file came from another card |
Moved |
the file is not where the binding says: copied elsewhere, or replaced by a copy |
Resized |
the file's length differs from the binding's |
Geometry |
the superblock's layout does not fit the file |
A file that a laptop cut short or extended usually shows as Size. Resized catches one whose lengths still agree
with each other but not with the binding.
The drive answers an unbound ring file by making it anew with recreate_ring. That loses the log in it but keeps
every other file. A card without a usable volume gets a full format with format_card. A new card in the drive is
such a card: its first boot formats it, and its second boot finds the ring file bound.
The card check¶
A format starts with a check of the card, because some cards lie. A fake card reports 32 GB but holds far less, and its writes past the real size wrap round onto earlier sectors. The aliasing test writes marks across the whole card and reads them back. The format thus refuses such a card before a single record goes to it.
The second part of the check measures how fast the card writes flushes of every power of two from 16 KiB to 1 MiB. The
smallest size within 90 % of the fastest becomes the card's flush_bytes, kept in the superblock for every later mount.
The format reference gives the exact steps. The drive example's simulated card takes no time to
write, so the example turns this measurement off and keeps the drive's own 16 KiB.
How long the card holds¶
A minute of the drive's bench takes 1033 KiB of the card's data ring. The 7589 segments of 4 MiB therefore hold the last 20.9 days of the bench. After that the ring goes round, and When the ring is full says what happens then.
logring.wlr, written in place without the file system.pub struct FormatOptions<'a>
How to format a card.
pub async fn mount_sd<'a, D: BlockDevice, C: Clock>(
dev: D,
card: Option<&CardInfo>,
opts: &FormatOptions<'_>,
clock: &mut C,
bufs: &'a mut Buffers<BUF, TRACKS>,
) -> Result<(Drive<'a, ExfatRing<D>>, Mounted), SdError<D::Error>>
Mount the drive's ring on the SD card dev. A card whose ring file checks out goes on logging. A ring file
that a PC moved, deleted or resized is made anew, and the other files stay. A card without a volume the drive
accepts is formatted, after the card check that opts asks for.
pub async fn open<D: BlockDevice>(mut dev: D) -> Result<Opened<D>, D::Error>
Check the card and bind the ring file: the exFAT volume mounts (boot checksum and fields), logring.wlr is a
contiguous (NoFatChain) file of whole sectors whose valid length is its length, all its clusters are marked
in the bitmap, and its header (superblock A/B, with CRC) carries the same binding: this volume's serial, the
file's first cluster, cluster count and start LBA.
pub enum Unbound
Why the ring file can't be written: logging stays off until the device re-creates the ring file (recreate_ring, keeps the rw files) or formats the card (format_card).
pub async fn recreate_ring<D: BlockDevice>(
mut dev: D,
card: Option<&CardInfo>,
opts: &FormatOptions<'_>,
card_flush_bytes: u32,
) -> Result<Formatted<D>, FormatError<D::Error>>
Re-create the ring file on a card whose volume is fine (after Unbound): delete
logring.wlr if there is one (whatever it is now; the log in it is lost), then create it again in the longest
free run as format_card would, rw_bytes of the free space left free.
pub async fn format_card<D: BlockDevice, C: Clock>(
mut dev: D,
card: Option<&CardInfo>,
opts: &FormatOptions<'_>,
clock: &mut C,
scratch: &mut [u8],
) -> Result<Formatted<D>, FormatError<D::Error>>
Format a card for the logring: the card check (feature card-check: aliasing test, write-size sweep, as
CheckOptions says; without the feature nothing is checked and clock is unused), then MBR +
exFAT with the card's allocation unit as boundary unit where it is a power of two, README.txt, and the ring
file logring.wlr (read-only + system + hidden, contiguous, starting on an AU boundary, whole segments) at the
end of the free space, rw_bytes left free before it.