diff --git a/issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md b/issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md deleted file mode 100644 index 927bb2b804c..00000000000 --- a/issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md +++ /dev/null @@ -1,59 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-09-05 ---- - -# A block freed by a delete and handed to the next file keeps the deleted file's bytes on the device - -Delete a file on DATA, write a new one large enough to be given the block that -just came free, shut down, and read the new file off the image on the host: its -first extent still holds **the deleted file's content**. The guest reads the new -file correctly in the same boot — it executed it — so the two views disagree and -the device's is the wrong one. - -## Reproduction - -Measured in `pkg_install_gbae`'s guest on 2026-09-05, with the probes in an -order that has since been changed (see the exit condition). - -1. `Profile::Metal`, DATA on NVMe, formatted for this run; confirm `/apps and - /home are a tmpfs` is **not** in the boot log. -2. Create `/apps/toy` and a symlink `/apps/toy/echo -> /system/bin/toybox`. -3. Delete both (`/system/bin/pkg remove toy`, which unlinks then `SYS_RMDIR`s). -4. Install a 1,608,600-byte binary at `/apps/gbae/gbae`. -5. Launch it — it runs, so the guest's own read of the path is right. -6. `run shutdown`, then read `apps/gbae/gbae` off the image with - `tests/common/storage.rs`'s `FileBlocks`. - -The length is right and byte 0 is not: - - apps/gbae/gbae is 1608600 bytes on the device against the archive's 1608600, - first differing at 0: device 2f73797374656d2f62696e2f746f7962 - against archive 7f454c46020101000000000000000000 - -`2f73797374656d2f62696e2f746f7962` is `/system/bin/toyb` — the first sixteen -bytes of the symlink target written at step 2 and deleted at step 3. Reproduced -on both arms of one run (the parallel one and the `ALONE` re-run), byte for -byte. - -## What it is not - -Not a mapping taken across the write: none is taken here. - -Which half is wrong is not established. Either the allocator handed out a block -whose old contents were never overwritten and the new file's first extent was -never written back, or the extent the host reader resolves for the new file is -the freed one. The kernel's own `Syncing filesystems` line is in the shutdown -capture either way. - -## Exit condition - -A test deletes a file, writes another that reuses its blocks, and finds the new -file's bytes on the device. - -`pkg_install_gbae`, whose boot measured this, was cut in `520c0d129`. DATA was -then the kernel's; it is the file server's now (`system/fileserver/src/data.rs`), -and its commits have been shadow-paged since `3adcf424e`. Nothing has looked -again, so whether either change removed the cause is not known. diff --git a/issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md b/issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md deleted file mode 100644 index c10c3cb352e..00000000000 --- a/issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-09-26 ---- - -# A rename that stops between its writes leaves a chain two entries reach - -`lib/fat32/src/fs.rs`'s rename writes the new entry run, then erases the old -one, then repoints a moved directory's `..`. A refused write is undone before the -call returns, but a machine that stops between those writes — a reset, a power -loss, or a device that stops answering so the undo never lands — leaves the -state the order had reached. Measured by -`a_stop_at_any_write_leaves_only_the_named_windows` -(`lib/fat32/tests/refused_writes.rs`), which freezes the device at every write -of a directory move and lists what `toyos-fat32-check` says: `cluster 5 is -already held by /into/A moved directory` (both entries live), `".." names -cluster 0; the format requires the parent's cluster 4` (moved, `..` not yet -repointed), and a `DotEntry` complaint on the doubly reached directory. Every -other call's stop leaves a leak, one split FAT entry or a partial long-name run; -rename's leaves a cross-link, which is the state FAT is worst at recovering -from. - -The order is deliberate: erasing first opens a window in which neither name -resolves, which the rename's own doc calls worse. The trade-off is between a -leak-shaped stop (erase first: the chain is orphaned for a moment) and a -cross-link-shaped one (insert first), and it is not this file's to settle. - -## Exit condition - -A rename whose device stops at any of its writes leaves only states the test -above admits for every other call, and the rename arm of its `own` filter is -deleted. diff --git a/issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md b/issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md deleted file mode 100644 index a0314f73e82..00000000000 --- a/issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-09-26 ---- - -# A shrink frees clusters before the entry stops naming them - -`lib/fat32/src/fs.rs`'s `set_len` to a shorter length frees the chain's tail -at once and records the new size in the directory entry only at the caller's -next `flush_meta`. Between the two the entry names more bytes than its chain -holds, and for a shrink to zero it names a first cluster that is free — which -the next allocation hands to another file. `a_stop_at_any_write_leaves_only_the_named_windows` -(`lib/fat32/tests/refused_writes.rs`) measures it by freezing the device -during a `set_len` then `flush_meta`: `DIR_FileSize is 2560 bytes, which needs 5 -clusters, and the chain holds 2`. It needs no refusal: a machine that stops -between a successful shrink and its flush leaves the same volume. - -fileserver's `FatVolume::truncate` and `FatVolume::level` (`system/fileserver/src/fat.rs`) -are the two callers: the first shrinks, the second writes the entry. - -## Exit condition - -A shrink stopped at any write leaves at most a leak, and both truncate arms of -that test's `own` filter are deleted. diff --git a/issues/a-stop-mid-append-leaves-a-fat-volume-s-leak-for-ever.md b/issues/a-stop-mid-append-leaves-a-fat-volume-s-leak-for-ever.md deleted file mode 100644 index a896697e455..00000000000 --- a/issues/a-stop-mid-append-leaves-a-fat-volume-s-leak-for-ever.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-10-21 ---- - -# A stop mid-append leaves a FAT volume's leak and stale free count for ever - -A reset, panic or power loss inside a `toyos-fat32` call leaves what its -repair module's open windows name. On the T14's first unclean reset mid-append -(#971, `stick_survives_a_reset_under_load`) that was a log part's chain two -clusters past its size and `FSI_Free_Count` two clusters off. No byte the -size names was lost. `toyos-fat32-check` names both states, and the metal -readback accepts exactly those for a boot its deadline ends -(`toyos_fat32_check::unexplained_by_a_stop`). - -Nothing on the mount path takes them back (`lib/fat32/src/repair.rs`, "The -repair lives in memory"). The clusters stay allocated and unreachable, and -the free count stays the hint it was, which `free_bytes` reports as the -volume's. Every unclean stop adds at most what one call claims. - -**Exit**: a writable mount of a volume that was not cleanly stopped takes -back what a stop can leave: lost chains freed, a chain too long cut to its -size, the FAT copies reconciled, and the free count recounted. It knows the -volume was not cleanly stopped from fatgen103's ClnShutBitMask (FAT[1]), -cleared while writes are open and set again at sync. Shown by the fileserver -crash test mounting each stopped state writable and the checker then finding -nothing. Owner: `lib/fat32`, held by the orchestrator. diff --git a/issues/a-sync-commits-the-entry-of-a-file-whose-pages-were-refused.md b/issues/a-sync-commits-the-entry-of-a-file-whose-pages-were-refused.md index bcca2298d20..199d9ea34d6 100644 --- a/issues/a-sync-commits-the-entry-of-a-file-whose-pages-were-refused.md +++ b/issues/a-sync-commits-the-entry-of-a-file-whose-pages-were-refused.md @@ -13,8 +13,7 @@ among them, so the commit makes durable an entry whose new extents name blocks that never received its pages. In the same boot the file still reads right: its pages stay dirty in its object and the next sync tries them again. After a stop before that, it reads what those blocks last held, -which may be a deleted file's bytes -(`issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md`). +which may be a deleted file's bytes. An fsync does not: a carried file whose pages are refused refuses the commit (`Files::fsync`). diff --git a/issues/a-write-back-the-device-refused-waits-for-the-next-write.md b/issues/a-write-back-the-device-refused-waits-for-the-next-write.md deleted file mode 100644 index 8a52c470210..00000000000 --- a/issues/a-write-back-the-device-refused-waits-for-the-next-write.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-09-27 ---- - -# A write-back the device refused waits for the next write - -fileserver's write-back (`system/fileserver/src/writeback.rs`) is spent when it comes due, -and only a sync that answers is recorded: one that left a file unwritten stays -due and is tried again. A sync the volume refuses whole — the cache's flush or -the device — returns before that, so the write-back is spent and nothing -syncs the dirty blocks the cache still holds until a client writes or syncs -again. - -Owner: the fileserver loop (`system/fileserver/src/main.rs`). - -**Exit**: a refused sync keeps the write-back due at a bound that does not -say the refusal once every two seconds for the rest of the boot, with a host -test of `WriteBack` over a refused sync. diff --git a/issues/nothing-reclaims-what-a-stop-leaves-on-a-fat-volume.md b/issues/nothing-reclaims-what-a-stop-leaves-on-a-fat-volume.md deleted file mode 100644 index b9b1641ff53..00000000000 --- a/issues/nothing-reclaims-what-a-stop-leaves-on-a-fat-volume.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-09-26 ---- - -# Nothing reclaims what a stop leaves on a FAT volume - -`toyos-fat32` undoes or carries through every refused write before the next call -proceeds, but that repair lives in memory (`lib/fat32/src/repair.rs`). A -machine that stops while one is queued, or between two writes of any call, -leaves what `a_stop_at_any_write_leaves_only_the_named_windows` -(`lib/fat32/tests/refused_writes.rs`) admits: orphaned clusters no more than -one call claims and frees, at most one FAT entry whose two copies differ, and at -most one partial long-name run. `toyos-fat32-check` names each. Nothing in -ToyOS repairs them: mounting writes nothing by design, the log partition has no -`fsck`, and a host `fsck` is a macOS binary. - -A split entry asks which copy is true, and the answer is known: each holds a -state the call or its repair passed through, so copying the active entry over -the mirror loses at most the stopped call's last write. - -## Exit condition - -Two steps, the first standing on its own: - -1. Mount detects each of those states without writing anything — a FAT entry - whose copies differ, a run of allocated clusters no entry reaches, a - long-name run no short entry completes — and logs each by name, so a volume - a stop left is known as one before anything is written to it. -2. A volume a stop left in any of those states is consistent again after the - next mount's first write, or the owner rules the leak acceptable and this - is a `rejected` file saying so. diff --git a/issues/the-nvme-driver-reads-a-namespaces-format-and-page-size-partly.md b/issues/the-nvme-driver-reads-a-namespaces-format-and-page-size-partly.md deleted file mode 100644 index eedd391cb2b..00000000000 --- a/issues/the-nvme-driver-reads-a-namespaces-format-and-page-size-partly.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-10-23 ---- - -# The NVMe driver reads a namespace's format and the page size partly - -Every image drives NVMe by class code, so `Controller::open` -(`system/nvme/src/nvme.rs`) meets whatever controller a machine carries. Three -of its geometry reads assume what QEMU's and the benches' controllers report. -The field layouts come from NVM Express Base Specification 2.0: the Controller -Capabilities register, the Identify Controller data structure, and the Identify -Namespace data structure with its LBA Format data structure. - -- **The LBA format index.** The driver takes `FLBAS` bits 3:0 as the index. - When `NLBAF` is greater than 16, bits 6:5 hold the index's upper two bits, so - on such a namespace the driver may read another format's `LBADS`. -- **Metadata.** The driver ignores the format's metadata size (`MS`, bits 15:0 - of the LBA format). With `FLBAS` bit 4 set (extended LBA), metadata is - interleaved with the data, so one sector moves more than `2^LBADS` bytes - through the PRPs. A separate-buffer format needs an `MPTR` the driver never - sets. -- **Memory page size.** `CC.MPS` is written as 0 (4 KiB) without checking - `CAP.MPSMIN`. `MDTS` is converted to blocks as though its unit were 4 KiB, - but its unit is `2^(12 + MPSMIN)` bytes. - -By reading, none of these crosses a partition. GPT's CRCs reject a misread -table, sessions are bounded to their partition's span, and DMA stays inside -the claim's domain. They are correctness gaps on unusual drives. They have not -been seen on any machine. - -**Exit**: `Controller::open` refuses each unsupported case by name with -`Refusal::Geometry`, or supports it. Specifically: - -- it takes the full format index; -- it refuses any format with `MS` non-zero; -- it refuses a controller whose `MPSMIN` is not 0, or sizes `MDTS` in that unit; -- a host model of the Identify and CAP fields holds each case. - -**Owner**: the orchestrator, which dispatches it. diff --git a/issues/the-shell-runs-a-command-whose-redirect-it-could-not-open.md b/issues/the-shell-runs-a-command-whose-redirect-it-could-not-open.md deleted file mode 100644 index 6f3bd962da7..00000000000 --- a/issues/the-shell-runs-a-command-whose-redirect-it-could-not-open.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -status: open -kind: defect -severity: [data-loss] -opened: 2026-10-07 ---- - -# The shell runs a command whose redirect it could not open - -`apply_redirect` (`system/shell/src/main.rs`) answers a redirect target it -could not open by printing `cannot open: ` and returning, and its -callers spawn the command all the same, with the shell's own output in place -of the file. `echo secret > /somewhere/refused` therefore writes `secret` to -the terminal, or into whatever the shell's output is, and the line's status is -the command's. The `2>` arms do the same without the line. - -Read from the source, not measured, and no test holds it. - -Owner: the shell. - -## Exit condition - -A command whose redirect could not be opened is not run, the shell says the -error the open answered, and the line's status is a failure; a test -redirects into a path that does not open and reads back neither the command's -output nor a zero status. diff --git a/lib/fat32/src/dir.rs b/lib/fat32/src/dir.rs index dc1914327f4..f94cd1ac87a 100644 --- a/lib/fat32/src/dir.rs +++ b/lib/fat32/src/dir.rs @@ -241,6 +241,12 @@ impl DirScan { } } + /// Entries read so far: the end marker or the chain's end, once + /// [`Self::next`] has answered `None`. + pub(crate) fn scanned(&self) -> u32 { + self.index + } + fn drop_run(&mut self) { self.run_valid = false; self.run_units = 0; @@ -589,7 +595,7 @@ impl Fat32 { /// What a `..` entry must name for a given parent. Zero when the parent is /// the root, which is what the format requires and what `fsck` checks: the /// root has no cluster number a `..` entry may name. - fn dotdot_target(&self, parent: Cluster) -> u32 { + pub(crate) fn dotdot_target(&self, parent: Cluster) -> u32 { if parent == self.geometry().root() { 0 } else { parent.raw() } } diff --git a/lib/fat32/src/fat.rs b/lib/fat32/src/fat.rs index 69c3574931b..e51a1bdfd04 100644 --- a/lib/fat32/src/fat.rs +++ b/lib/fat32/src/fat.rs @@ -13,7 +13,7 @@ pub(crate) const END_OF_CHAIN: u32 = 0x0FFF_FFFF; /// A FAT32 entry is 28 bits. The top four belong to whoever formatted the /// volume and are preserved across every write, which is why `set_fat_entry` /// reads before it writes. -const ENTRY_MASK: u32 = 0x0FFF_FFFF; +pub(crate) const ENTRY_MASK: u32 = 0x0FFF_FFFF; impl Fat32 { /// One FAT entry, masked to its 28 significant bits. @@ -49,7 +49,7 @@ impl Fat32 { /// One FAT copy's entry, preserving the four reserved high bits the format /// leaves to whoever made the volume. - async fn write_fat_entry(&mut self, fat: u32, cluster: Cluster, value: u32) -> Result<(), Error> { + pub(crate) async fn write_fat_entry(&mut self, fat: u32, cluster: Cluster, value: u32) -> Result<(), Error> { let offset = self.geom.fat_entry_offset(fat, cluster); let mut raw = [0u8; 4]; self.dev.read_at(offset, &mut raw).await?; diff --git a/lib/fat32/src/fs.rs b/lib/fat32/src/fs.rs index ed418945b77..f84130a95cd 100644 --- a/lib/fat32/src/fs.rs +++ b/lib/fat32/src/fs.rs @@ -819,6 +819,10 @@ impl Fat32 { /// the handle once the repair carries the shrink: a refusal before that /// has written nothing, and the handle's old size is what makes the retry /// shrink again. + /// + /// **The entry stops naming the clusters before they are freed**, so a + /// stop between the two leaks them and never leaves an entry naming free + /// space. async fn shrink_chain(&mut self, f: &mut File, len: u64) -> Result<(), Error> { let bpc = self.geom.bytes_per_cluster() as u64; let keep = len.div_ceil(bpc); @@ -831,6 +835,23 @@ impl Fat32 { // part of a chain and frees the rest, and a loop between the two // halves is how live clusters reach the allocator. self.verify_acyclic(first).await?; + let unnamed = keep == 0 && f.entry_cluster.is_some(); + if f.entry_size as u64 > len || unnamed { + let mut raw = self.live_entry(f).await?; + if keep == 0 { + raw.set_first_cluster(0); + } + raw.set_size(len as u32); + let mark = self.repair.len(); + self.write_entry_at(f.loc.entry_offset, &raw).await?; + self.dev.flush().await?; + // Landed: the free that follows only goes forward. + self.repair.truncate(mark); + f.entry_size = len as u32; + if keep == 0 { + f.entry_cluster = None; + } + } if keep == 0 { f.first_cluster = None; f.size = len as u32; @@ -1068,6 +1089,7 @@ impl Fat32 { /// up to the last erase, carried forward from it by [`Self::atomic`]. async fn erase_committed(&mut self, loc: Loc, chain: Option) -> Result<(), Error> { self.erase_entries(loc).await?; + self.dev.flush().await?; self.commit(); if let Some(c) = chain { self.queue(Repair::Free { start: c, anchor: None }); @@ -1102,6 +1124,7 @@ impl Fat32 { } self.insert_entry(new_dir, &new_name, &node.raw).await?; + self.dev.flush().await?; self.erase_entries(loc).await?; if let Some(c) = moved_dir { if new_dir != loc.dir_start { diff --git a/lib/fat32/src/lib.rs b/lib/fat32/src/lib.rs index f18b2e7df22..ceb7333f79e 100644 --- a/lib/fat32/src/lib.rs +++ b/lib/fat32/src/lib.rs @@ -82,6 +82,7 @@ mod error; mod fat; mod fs; mod name; +mod recover; mod repair; mod time; @@ -90,5 +91,6 @@ pub use dir::MAX_DIR_ENTRIES; pub use error::Error; pub use fs::{DirEntry, Extent, Fat32, File, Metadata, ReplaceFailed, Replaced}; pub use name::MAX_LFN_CHARS; +pub use recover::{Recovered, Recovery}; pub use repair::MAX_REPAIR_STEPS; pub use time::FatTime; diff --git a/lib/fat32/src/recover.rs b/lib/fat32/src/recover.rs new file mode 100644 index 00000000000..84c1a4feecd --- /dev/null +++ b/lib/fat32/src/recover.rs @@ -0,0 +1,412 @@ +//! What a writable mount takes back of a volume a stop left: the states +//! [`crate::repair`]'s open windows name, and the one a rename stopped +//! between its writes leaves. +//! +//! **Nothing is written unless everything found is one of those states.** +//! The volume is read whole first: every directory walked, every chain +//! followed to its file's size, the FAT copies compared. Anything a stop of +//! this crate cannot leave — a link that is not one, a directory that does +//! not read whole, a cluster two different entries reach, a chain short of +//! its size, FAT copies that differ where one does not read whole — refuses +//! the recovery by name and leaves every byte as it was, since a walk that +//! did not reach a cluster cannot say it is lost. Then, in this order, each +//! write moving towards consistency so a stop inside it leaves no worse a +//! volume: +//! +//! - **A split FAT entry** takes the active copy's value, which the walk +//! read whole. +//! - **A rename's second name** — an entry equal in every field but the name +//! to one the walk already met, reaching the same chain — is erased, so the +//! file keeps its old name or its new one, and each directory's `..` names +//! the parent its kept entry is in. +//! - **A long-name entry no short entry completes** is freed. +//! - **A file's chain longer than its size** is ended at the size, so the +//! bytes a growth wrote past its entry are lost, as the entry says. +//! - **A cluster no entry reaches** is freed, and the free count is counted +//! again. + +use alloc::collections::BTreeMap; +use alloc::format; +use alloc::string::String; +use alloc::vec; +use alloc::vec::Vec; + +use crate::boot::Cluster; +use crate::device::Volume as _; +use crate::dir::{DirScan, EntryCursor, RawEntry}; +use crate::error::Error; +use crate::fat::{END_OF_CHAIN, ENTRY_MASK}; +use crate::fs::{Fat32, Loc}; +use toyos_blockdev::BlockDev; + +/// What [`Fat32::recover`] did. +#[derive(Debug, PartialEq, Eq)] +pub enum Recovery { + /// Every finding was a stop's, and this is what was written. + Recovered(Recovered), + /// The volume holds what no stop of this crate leaves, named here, and + /// nothing was written. + Refused(String), +} + +/// What a recovery changed. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct Recovered { + /// Split FAT entries whose mirrors took the active copy's value. + pub mirrors: u32, + /// Second names a stopped rename left, erased. + pub renames: u32, + /// Directories whose `..` was repointed at the parent their entry is in. + pub parents: u32, + /// Long-name entries no short entry completed, freed. + pub long_names: u32, + /// Files whose chain was ended at their size; the clusters past it are + /// counted in [`Self::lost`]. + pub ended: u32, + /// Allocated clusters no entry reached, freed. + pub lost: u32, +} + +impl Recovered { + /// Whether the volume was already consistent. + pub fn nothing(&self) -> bool { + *self == Self::default() + } +} + +/// Free in a deleted directory entry's first byte. +const DELETED: u8 = 0xE5; +/// The bad-cluster marker, which is allocated and reached by nothing. +const BAD: u32 = 0x0FFF_FFF7; + +/// One bit per cluster number: reached by an entry. +struct Reached(Vec); + +impl Reached { + fn new(clusters: u32) -> Reached { + Reached(vec![0; (clusters as usize + 2).div_ceil(64)]) + } + + /// Mark `c`; false if it was already marked. + fn mark(&mut self, c: Cluster) -> bool { + let (word, bit) = (c.raw() as usize / 64, c.raw() % 64); + let was = self.0[word] & (1 << bit) != 0; + self.0[word] |= 1 << bit; + !was + } + + fn has(&self, raw: u32) -> bool { + self.0[raw as usize / 64] & (1 << (raw % 64)) != 0 + } +} + +/// What the read-only pass found, every item a stop's. +struct Findings { + /// Each FAT entry whose copies differ, and the active copy's value. + splits: Vec<(Cluster, u32)>, + seconds: Vec, + /// Directories whose `..` does not name their parent, and that parent. + parents: Vec<(Cluster, Cluster)>, + /// Offsets of long-name entries no short entry completes. + names: Vec, + /// Each long chain's cluster at its file's size. + ends: Vec, + reached: Reached, +} + +/// The walk met what no stop leaves. +type Unexplained = String; + +impl Fat32 { + /// Take back what a stop left, then sync; or refuse, writing nothing. + /// For a writable mount, before its first write: the volume must not be + /// written by anything else meanwhile. + pub async fn recover(&mut self) -> Result { + assert!(!self.in_call, "toyos-fat32: recover inside a mutating call"); + self.settle().await?; + let found = match self.examine().await? { + Ok(found) => found, + Err(why) => return Ok(Recovery::Refused(why)), + }; + let mut done = Recovered::default(); + for &(cluster, value) in &found.splits { + for fat in self.geom.fat_mirrors().filter(|&f| f != 0) { + self.write_fat_entry(fat, cluster, value).await?; + } + done.mirrors += 1; + } + for loc in found.seconds { + let mark = self.repair.len(); + self.erase_entries(loc).await?; + self.repair.truncate(mark); + done.renames += 1; + } + for (dir, parent) in found.parents { + let mark = self.repair.len(); + self.set_dot_dot(dir, parent).await?; + self.repair.truncate(mark); + done.parents += 1; + } + for offset in found.names { + let mut raw = self.read_entry_at(offset).await?; + raw.0[0] = DELETED; + self.put_entry_at(offset, &raw).await?; + done.long_names += 1; + } + for last in found.ends { + self.set_fat_entry(last, END_OF_CHAIN).await?; + done.ended += 1; + } + let free; + (done.lost, free) = self.free_unreached(&found.reached).await?; + self.fsinfo.free_count = Some(free); + self.fsinfo.next_free = None; + self.fsinfo.dirty = true; + self.sync().await?; + Ok(Recovery::Recovered(done)) + } + + /// Read the volume whole and say what a recovery would write, or what it + /// found that no stop leaves. + /// + /// **Copies that differ are a stop's only where each reads whole.** A + /// stopped flush of a write-back device may have written any of its FAT + /// blocks of either copy; each copy still reads whole because an entry + /// reaches the disk a flush after its chain, and every write that must + /// precede another a flush before it ([`crate::repair`]), so the blocks + /// differ only past a file's size or in clusters nothing reaches. A copy torn or written by something else + /// does not read whole, and then neither is written over. + async fn examine(&mut self) -> Result, Error> { + let splits = self.splits().await?; + let mut found = Findings { + splits, + seconds: Vec::new(), + parents: Vec::new(), + names: Vec::new(), + ends: Vec::new(), + reached: Reached::new(self.geom.cluster_count), + }; + if let Err(why) = self.walk_tree(&mut found).await? { + return Ok(Err(why)); + } + if !found.splits.is_empty() { + for copy in self.geom.fat_mirrors().filter(|&f| f != 0) { + if let Err(why) = self.walk_copy(copy).await? { + let n = found.splits.len(); + return Ok(Err(format!("the FAT copies differ in {n} entries, and copy {copy} does not read whole: {why}"))); + } + } + } + Ok(Ok(found)) + } + + /// The walk, reading FAT `copy` in place of the active one. + async fn walk_copy(&mut self, copy: u32) -> Result, Error> { + let mut found = Findings { + splits: Vec::new(), + seconds: Vec::new(), + parents: Vec::new(), + names: Vec::new(), + ends: Vec::new(), + reached: Reached::new(self.geom.cluster_count), + }; + let active = self.geom.active_fat.replace(copy); + let walked = self.walk_tree(&mut found).await; + self.geom.active_fat = active; + walked + } + + /// Every FAT entry whose mirrors differ from FAT 0, with FAT 0's value; + /// none where mirroring is off, since the other copies are not the + /// volume's then. + async fn splits(&mut self) -> Result, Error> { + let mut splits = Vec::new(); + if self.geom.active_fat.is_some() { + return Ok(splits); + } + let bps = self.geom.bytes_per_sector as usize; + let sectors = self.geom.max_cluster() as u64 * 4 / bps as u64 + 1; + let mut ours = vec![0u8; bps]; + let mirrors: Vec = self.geom.fat_mirrors().filter(|&f| f != 0).collect(); + let mut theirs = vec![vec![0u8; bps]; mirrors.len()]; + for sector in 0..sectors { + let at = sector * bps as u64; + self.dev.read_at(self.geom.fat_base_offset(0) + at, &mut ours).await?; + for (fat, buf) in mirrors.iter().zip(theirs.iter_mut()) { + self.dev.read_at(self.geom.fat_base_offset(*fat) + at, buf).await?; + } + for e in 0..bps / 4 { + let entry = |b: &[u8]| u32::from_le_bytes(b[e * 4..e * 4 + 4].try_into().expect("4 bytes")) & ENTRY_MASK; + let number = (sector as usize * bps / 4 + e) as u32; + let Some(cluster) = self.geom.cluster(number) else { continue }; + if theirs.iter().any(|buf| entry(buf) != entry(&ours)) { + splits.push((cluster, entry(&ours))); + } + } + } + Ok(splits) + } + + /// Walk the tree from the root, marking every cluster it reaches and + /// noting what a stop left; the first thing no stop leaves ends it. + async fn walk_tree(&mut self, found: &mut Findings) -> Result, Error> { + let whole = self.geom.cluster_count as u64; + let bpc = self.geom.bytes_per_cluster() as u64; + let root = self.geom.root(); + if self.reach_chain(root, whole, &mut found.reached).await?.is_err() { + return Ok(Err("the root directory's chain does not read whole".into())); + } + // Each entry's first cluster, and every field of the entry but its + // name: byte 12, the case of the short name, is the name's. + let mut named: BTreeMap = BTreeMap::new(); + let mut dirs = vec![root]; + while let Some(dir) = dirs.pop() { + let mut scan = DirScan::new(dir); + let mut after = 0u32; + loop { + let located = match scan.next(self).await { + Ok(located) => located, + Err(Error::CorruptDirectory | Error::CorruptChain) => { + return Ok(Err(format!("the directory at cluster {} does not read whole", dir.raw()))); + } + Err(e) => return Err(e), + }; + let end = located.as_ref().map_or(scan.scanned(), |l| l.first_index); + self.orphan_names(dir, after, end, &mut found.names).await?; + let Some(located) = located else { break }; + after = located.index + 1; + let (raw, size) = (located.raw, located.raw.size() as u64); + let Some(first) = self.geom.cluster(raw.first_cluster()) else { + if raw.first_cluster() != 0 || (!raw.is_dir() && size != 0) { + return Ok(Err(format!( + "an entry in the directory at cluster {} names cluster {} for {size} bytes", + dir.raw(), + raw.first_cluster() + ))); + } + continue; + }; + let mut fields = [0u8; 20]; + fields[0] = raw.0[11]; + fields[1..].copy_from_slice(&raw.0[13..]); + if found.reached.has(first.raw()) { + if named.get(&first.raw()) != Some(&fields) { + return Ok(Err(format!("cluster {} is reached by two different entries", first.raw()))); + } + found.seconds.push(Loc { + dir_start: dir, + first_index: located.first_index, + index: located.index, + entry_offset: located.offset, + }); + continue; + } + named.insert(first.raw(), fields); + let unexplained = |what: &str| format!("the chain from cluster {} {what}", first.raw()); + if raw.is_dir() { + if self.reach_chain(first, whole, &mut found.reached).await?.is_err() { + return Ok(Err(unexplained("of a directory does not read whole"))); + } + match self.names_parent(first, dir).await? { + Some(true) => {} + Some(false) => found.parents.push((first, dir)), + None => return Ok(Err(format!("the directory at cluster {} has no `..`", first.raw()))), + } + dirs.push(first); + } else if size == 0 { + return Ok(Err(unexplained("is named by an empty file"))); + } else { + let keep = size.div_ceil(bpc); + match self.reach_chain(first, keep, &mut found.reached).await? { + Ok((n, last)) if n == keep => match self.next_cluster(last).await { + Ok(Some(_)) => found.ends.push(last), + Ok(None) => {} + Err(Error::CorruptChain) => return Ok(Err(unexplained("ends in a link that is not one"))), + Err(e) => return Err(e), + }, + Ok(_) => return Ok(Err(unexplained(&format!("is short of its file's {size} bytes")))), + Err(()) => return Ok(Err(unexplained("of a file does not read whole"))), + } + } + } + } + Ok(Ok(())) + } + + /// Whether `dir`'s `..` names `parent`; `None` for a directory with no + /// `..` entry. + async fn names_parent(&mut self, dir: Cluster, parent: Cluster) -> Result, Error> { + let Some(offset) = EntryCursor::new(dir).offset_of(self, 1).await? else { return Ok(None) }; + let raw = self.read_entry_at(offset).await?; + if raw.0[..2] != *b".." { + return Ok(None); + } + Ok(Some(raw.first_cluster() == self.dotdot_target(parent))) + } + + /// Mark up to `limit` clusters of the chain from `start`: their count and + /// the last, or `Err(())` for a chain that reaches a marked cluster or a + /// link that is not one. + async fn reach_chain( + &mut self, + start: Cluster, + limit: u64, + reached: &mut Reached, + ) -> Result, Error> { + let mut c = start; + let mut n = 0; + loop { + if !reached.mark(c) { + return Ok(Err(())); + } + n += 1; + if n == limit { + return Ok(Ok((n, c))); + } + match self.next_cluster(c).await { + Ok(Some(next)) => c = next, + Ok(None) => return Ok(Ok((n, c))), + Err(Error::CorruptChain) => return Ok(Err(())), + Err(e) => return Err(e), + } + } + } + + /// Note each live long-name entry of `dir` from index `from` before + /// `to`: none of them is part of a name a short entry completes. + async fn orphan_names(&mut self, dir: Cluster, from: u32, to: u32, names: &mut Vec) -> Result<(), Error> { + let mut cursor = EntryCursor::new(dir); + for index in from..to { + let Some(offset) = cursor.offset_of(self, index).await? else { break }; + let raw: RawEntry = self.read_entry_at(offset).await?; + if raw.is_lfn() && raw.0[0] != DELETED { + names.push(offset); + } + } + Ok(()) + } + + /// Free every allocated cluster `reached` does not hold: how many, and + /// the free clusters then. + async fn free_unreached(&mut self, reached: &Reached) -> Result<(u32, u32), Error> { + let bps = self.geom.bytes_per_sector as usize; + let sectors = self.geom.max_cluster() as u64 * 4 / bps as u64 + 1; + let (mut lost, mut free) = (0, 0); + let mut buf = vec![0u8; bps]; + for sector in 0..sectors { + self.dev.read_at(self.geom.fat_base_offset(self.geom.active_fat.unwrap_or(0)) + sector * bps as u64, &mut buf).await?; + for e in 0..bps / 4 { + let number = (sector as usize * bps / 4 + e) as u32; + let Some(cluster) = self.geom.cluster(number) else { continue }; + let value = u32::from_le_bytes(buf[e * 4..e * 4 + 4].try_into().expect("4 bytes")) & ENTRY_MASK; + if value == 0 { + free += 1; + } else if value != BAD && !reached.has(number) { + self.set_fat_entry(cluster, 0).await?; + lost += 1; + } + } + } + Ok((lost, free + lost)) + } +} diff --git a/lib/fat32/src/repair.rs b/lib/fat32/src/repair.rs index 85e74476ded..27632eb5876 100644 --- a/lib/fat32/src/repair.rs +++ b/lib/fat32/src/repair.rs @@ -30,6 +30,11 @@ //! — counts when its write lands. Every queued step is driven until it lands, //! so no refusal leaves the count a guess. //! +//! **A write-back device orders nothing between two flushes**, so a call +//! whose writes must land in order flushes between them: a removed name's +//! erase before its chain's free, a shorter entry before the clusters past +//! it, and a rename's new name before the old one's erase. +//! //! What stays open, because FAT has no journal: //! //! - **Between the two copies of one entry.** [`Fat32::set_fat_entry`] is one @@ -40,16 +45,18 @@ //! - **Between a claim and its link.** A claimed cluster is end-of-chain before //! anything reaches it, so a stop there orphans it: a leak, never a chain //! into free space. -//! - **Inside a free.** The name is erased or the kept cluster terminated -//! first, so a stop part way through the walk orphans the rest of the chain. +//! - **Inside a free.** The name is erased, or a truncation's entry +//! shortened, before the first cluster is freed, so a stop part way through +//! the walk orphans the rest of the chain. //! - **Between the entries of one name.** A long name is a run of up to //! twenty-one directory entries written one at a time, so a stop inside a //! create, rename or remove leaves a partial run. //! - **The repair lives in memory.** A reset, panic or power loss while it is //! non-empty leaves the state the refused write reached: orphaned clusters //! bounded by what one call claims and frees, at most one split entry, and -//! at most one partial run, each of which `toyos-fat32-check` names. Nothing -//! on the mount path repairs them. +//! at most one partial run, each of which `toyos-fat32-check` names. +//! +//! [`Fat32::recover`] takes back every state these windows leave. use crate::boot::Cluster; use toyos_blockdev::BlockDev; diff --git a/lib/fat32/tests/refused_writes.rs b/lib/fat32/tests/refused_writes.rs index 65c167c822c..85fe27a6136 100644 --- a/lib/fat32/tests/refused_writes.rs +++ b/lib/fat32/tests/refused_writes.rs @@ -27,7 +27,7 @@ use std::cell::{Cell, Ref, RefCell}; use common::now; use spec_volume::{fat_offset, Volume, BYTES_PER_SECTOR, CLUSTERS, FAT_SECTORS, NUM_FATS}; use toyos_blockdev::{within, Block, BlockDev, Error as DevError, BLOCK}; -use toyos_fat32::{Error, Fat32, FatTime, File, MAX_LFN_CHARS, MAX_REPAIR_STEPS}; +use toyos_fat32::{Error, Fat32, FatTime, File, Recovered, Recovery, MAX_LFN_CHARS, MAX_REPAIR_STEPS}; use toyos_fat32_check::Complaint; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -395,8 +395,8 @@ fn append_and_sync() -> Scenario { } } -/// A shrink frees a tail from its first write on; its refusal is carried -/// forward too, and the entry is recorded once the free has gone through. +/// A shrink records the shorter entry, then frees the tail: a refused entry +/// is undone, and a refusal from the free on is carried forward. fn truncate() -> Scenario { Scenario { name: "truncate", @@ -665,10 +665,9 @@ fn all() -> [Scenario; 11] { #[test] fn a_stop_at_any_write_leaves_only_the_named_windows() { for s in all() { - // What these two calls' own write order leaves at a stop, owned by - // their issue files and not by this repair. + // What rename's own write order leaves at a stop: one chain under + // both names, which `Fat32::recover` resolves by keeping one. let own: fn(&Complaint) -> bool = match s.name { - // issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md "rename" => |c| { matches!( c, @@ -677,9 +676,6 @@ fn a_stop_at_any_write_leaves_only_the_named_windows() { | Complaint::DotEntry { .. } ) }, - // issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md - "truncate" => |c| matches!(c, Complaint::ChainTooShort { .. }), - "truncate to zero" => |c| matches!(c, Complaint::ChainTooShort { .. } | Complaint::ChainOutOfRange { .. }), _ => |_| false, }; let (base, handle) = fixture(&s); @@ -729,6 +725,158 @@ fn a_stop_at_any_write_leaves_only_the_named_windows() { } } +/// **What a stop leaves, a writable mount takes back.** Every state the test +/// above admits, rename's own included, mounted again and recovered, is one +/// the checker finds nothing in, and a second recovery finds nothing to do. +#[test] +fn a_recovery_after_a_stop_at_any_write_leaves_a_consistent_volume() { + let mut seen = Recovered::default(); + for s in all() { + let (base, handle) = fixture(&s); + for outcome in [Outcome::Refused, Outcome::Landed] { + for from in 0u64.. { + let mut fs = now(Fat32::mount(Faulty::new(base.clone()))).expect("mount"); + let mut h = handle; + fs.device().arm(burst(from, u64::MAX, outcome)); + let _ = (s.call)(&mut fs, &mut h, 0); + if fs.device().refused() == 0 { + break; + } + let context = format!("{}: stopped at write {from}, {outcome:?}", s.name); + let stopped = fs.into_device().into_medium(); + let mut fs = now(Fat32::mount(Faulty::new(stopped))).expect("mount the stopped volume"); + let recovered = match now(fs.recover()) { + Ok(Recovery::Recovered(done)) => done, + other => panic!("{context}: {other:?}"), + }; + seen.mirrors += recovered.mirrors; + seen.renames += recovered.renames; + seen.parents += recovered.parents; + seen.long_names += recovered.long_names; + seen.ended += recovered.ended; + seen.lost += recovered.lost; + let left = complaints(&fs.device().medium(), true); + assert!(left.is_empty(), "{context}: {recovered:?}, and still\n{}", toyos_fat32_check::describe(&left)); + common::assert_fats_agree(&fs.geometry().clone(), &fs.device().medium()); + let again = now(fs.recover()).expect("recover again"); + assert_eq!(again, Recovery::Recovered(Recovered::default()), "{context}: a second recovery"); + if s.name == "rename" { + let moved = now(fs.exists("moving/kept.bin")).expect("exists") + || now(fs.exists("into/A moved directory/kept.bin")).expect("exists"); + assert!(moved, "{context}: the moved directory's file is under neither name"); + } + } + } + } + // Each repair met at least one stop that needed it, or this shows nothing of it. + let Recovered { mirrors, renames, parents, long_names, ended, lost } = seen; + assert!([mirrors, renames, parents, long_names, ended, lost].iter().all(|&n| n > 0), "{seen:?}"); +} + +/// A clean volume with a file and a directory holding one, as a writer that +/// is not this crate might leave it, and where each lies. +fn foreign() -> (Vec, usize, usize, usize) { + let mut v = Volume::new(); + v.finish(); + let mut fs = now(Fat32::mount(Faulty::new(v.bytes))).expect("mount"); + common::write_new(&mut fs, "A.BIN", &common::pattern(700, 1), stamp()); + now(fs.create_dir("DIR", stamp())).expect("mkdir"); + common::write_new(&mut fs, "DIR/B.BIN", &common::pattern(900, 2), stamp()); + now(fs.sync()).expect("sync"); + let a = now(fs.entry_offset("A.BIN")).unwrap() as usize; + let dir = now(fs.entry_offset("DIR")).unwrap() as usize; + let b = now(fs.entry_offset("DIR/B.BIN")).unwrap() as usize; + (fs.into_device().into_medium(), a, dir, b) +} + +fn first_cluster(medium: &[u8], entry: usize) -> u32 { + let lo = u16::from_le_bytes([medium[entry + 26], medium[entry + 27]]) as u32; + let hi = u16::from_le_bytes([medium[entry + 20], medium[entry + 21]]) as u32; + hi << 16 | lo +} + +/// Recovery of `medium` refuses, naming `why`, and leaves every byte. +fn refused_untouched(medium: Vec, why: &str) { + let mut fs = now(Fat32::mount(Faulty::new(medium.clone()))).expect("mount"); + match now(fs.recover()).expect("recover") { + Recovery::Refused(said) => assert!(said.contains(why), "refused for {said:?}, not {why:?}"), + done => panic!("recovered {done:?} a volume no stop leaves"), + } + assert!(*fs.device().medium() == medium[..], "a refused recovery wrote to the volume"); +} + +/// A directory whose chain has a link that is not one: the walk cannot see +/// what is under it, so it cannot say anything there is lost. +#[test] +fn a_directory_with_a_broken_link_is_refused_and_left() { + let (mut medium, _, dir, _) = foreign(); + let cluster = first_cluster(&medium, dir); + for copy in 0..NUM_FATS { + medium[fat_offset(copy, cluster)..][..4].copy_from_slice(&0u32.to_le_bytes()); + } + refused_untouched(medium, "of a directory does not read whole"); +} + +/// A file's chain run into a directory's: one cluster two different entries +/// reach, whichever the walk meets first. +#[test] +fn a_file_cross_linked_into_a_directory_is_refused_and_left() { + let (mut medium, a, dir, _) = foreign(); + let into = first_cluster(&medium, dir); + let from = first_cluster(&medium, a); + for copy in 0..NUM_FATS { + medium[fat_offset(copy, from)..][..4].copy_from_slice(&into.to_le_bytes()); + } + // A.BIN's 700 bytes need two clusters, the second now the directory's. + refused_untouched(medium, "is reached by two different entries"); +} + +/// Two files reaching one chain that differ in more than their name are no +/// rename a stop left. +#[test] +fn a_cross_link_between_two_files_is_refused_and_left() { + let (mut medium, a, _, b) = foreign(); + let first = medium[a + 26..a + 28].to_vec(); + medium[b + 26..b + 28].copy_from_slice(&first); + refused_untouched(medium, "is reached by two different entries"); +} + +/// The copies differing where one of them does not read whole: a torn or +/// foreign copy, which a stop of ours never leaves, and the other copy is +/// not written over it. FAT 0 torn, then the mirror. +#[test] +fn copies_that_differ_where_one_does_not_read_whole_are_refused_and_left() { + for (copy, why) in [(0, "of a file does not read whole"), (1, "copy 1 does not read whole")] { + let (mut medium, a, ..) = foreign(); + // A.BIN's first link, and a sector's worth of entries no file uses, + // in this copy alone. + let first = first_cluster(&medium, a); + medium[fat_offset(copy, first)..][..4].copy_from_slice(&0u32.to_le_bytes()); + for cluster in 100..228 { + medium[fat_offset(copy, cluster)..][..4].copy_from_slice(&0x0FFF_FFFFu32.to_le_bytes()); + } + refused_untouched(medium, why); + } +} + +/// The copies differing in many entries where each reads whole: what a +/// stopped flush of the file server's cache leaves, which takes FAT 0. +#[test] +fn copies_that_differ_where_each_reads_whole_are_recovered() { + let (mut medium, ..) = foreign(); + for cluster in 100..228 { + medium[fat_offset(0, cluster)..][..4].copy_from_slice(&0x0FFF_FFFFu32.to_le_bytes()); + } + let mut fs = now(Fat32::mount(Faulty::new(medium))).expect("mount"); + match now(fs.recover()).expect("recover") { + Recovery::Recovered(done) => assert_eq!((done.mirrors, done.lost), (128, 128), "{done:?}"), + refused => panic!("{refused:?}"), + } + let left = complaints(&fs.device().medium(), true); + assert!(left.is_empty(), "{}", toyos_fat32_check::describe(&left)); + assert_eq!(common::read_all(&mut fs, "DIR/B.BIN"), common::pattern(900, 2)); +} + // ----------------------------------------------- the shape a stop produces /// The blocks FAT copy 0 alone lives in on the fixture: its last block is the diff --git a/system/fileserver/src/data.rs b/system/fileserver/src/data.rs index 9f45317e595..ef20eb3545e 100644 --- a/system/fileserver/src/data.rs +++ b/system/fileserver/src/data.rs @@ -762,6 +762,33 @@ mod tests { assert_eq!(tail.pop(), Some(b'x')); assert!(tail.iter().all(|&b| b == 0)); } + + /// A file a delete made room for holds its own bytes on the device. The + /// first file takes most of the volume, so the second cannot be written + /// without the blocks the delete freed. + #[test] + fn a_file_in_a_deleted_files_blocks_holds_its_own_bytes_on_the_device() { + const CHUNK: usize = 64 * BLOCK; + let mut v = volume(); + let free = v.room().unwrap().free as usize; + let len = (free * 6 / 10) & !(CHUNK - 1); + let fill = |v: &mut DataVolume, path: &str, bytes: &[u8]| { + let node = v.open(path, CREATE).unwrap(); + for (i, chunk) in bytes.chunks(CHUNK).enumerate() { + v.write(node, (i * CHUNK) as u64, chunk).unwrap_or_else(|e| panic!("{path} at {}: {e:?}", i * CHUNK)); + } + v.close(node).unwrap(); + v.sync().unwrap(); + }; + fill(&mut v, "home/old", &vec![0x5c; len]); + v.unlink("home/old").unwrap(); + v.sync().unwrap(); + let bytes: Vec = (0..len).map(|i| (i % 251) as u8).collect(); + fill(&mut v, "home/new", &bytes); + let mut v = reopen(v); + let new = v.open("home/new", EXISTING).unwrap(); + assert!(read(&mut v, new) == bytes, "the device holds other bytes than the file written"); + } #[test] fn unlink_and_replacement_keep_live_inode_identity() { let mut v = volume(); diff --git a/system/fileserver/src/fat.rs b/system/fileserver/src/fat.rs index d0ee179c2f6..e02e98f93fc 100644 --- a/system/fileserver/src/fat.rs +++ b/system/fileserver/src/fat.rs @@ -17,6 +17,11 @@ //! FAT32 has no symlink representation, so a symlink is refused rather than //! written as a file its reader would take for one. //! +//! **A writable mount first takes back what a stop left** (`Fat32::recover`): +//! leaks, a split FAT entry, a half-done rename, a partial long name and a +//! chain past its size. A volume holding anything else is served read-only, +//! untouched. +//! //! A volume that is not FAT32 is left untouched: `toyos-fat32` has no format //! path and its probe writes nothing. @@ -25,7 +30,7 @@ use std::rc::Rc; use toyos::fs; use toyos_blockdev::BlockDev; -use toyos_fat32::{Error, Fat32, FatTime}; +use toyos_fat32::{Error, Fat32, FatTime, Recovered, Recovery}; use crate::cache::{Cache, Disk}; use crate::volume::{parent, Identities, Kind, Listed, Meta, Node, OpenHow, Out, Reserved, Volume, WriteBack, NANOS_PER_SEC}; @@ -100,7 +105,7 @@ impl FatVolume { /// before the volume is known to be FAT32. pub fn mount(disk: D, clean_limit: usize, writable: bool, clock: fn() -> u64) -> Result { let cache = Rc::new(Cache::new(disk, clean_limit)); - let fs = cache.block_on(Fat32::mount(Rc::clone(&cache))).map_err(|e| format!("no FAT32 here: {e}"))?; + let mut fs = cache.block_on(Fat32::mount(Rc::clone(&cache))).map_err(|e| format!("no FAT32 here: {e}"))?; let geom = fs.geometry(); println!( "fileserver: FAT32 mounted, {} bytes, {}-byte sectors, {}-byte clusters", @@ -108,6 +113,30 @@ impl FatVolume { geom.bytes_per_sector, geom.bytes_per_cluster() ); + // Before the first write: a stop leaves leaks and splits nothing else + // takes back. A volume holding what no stop of ours leaves, or one the + // recovery could not finish reading or writing, is served read-only. + let writable = writable + && match cache.block_on(fs.recover()) { + Ok(Recovery::Recovered(done)) if done.nothing() => true, + Ok(Recovery::Recovered(Recovered { mirrors, renames, parents, long_names, ended, lost })) => { + println!( + "fileserver: the FAT volume a stop left was recovered: {mirrors} split FAT entries \ + mended, {renames} second names of a stopped rename erased, {parents} `..` \ + repointed, {long_names} partial long names erased, {ended} files ended at their \ + size, {lost} clusters no entry reached freed" + ); + true + } + Ok(Recovery::Refused(why)) => { + println!("fileserver: the FAT volume holds what no stop of ours leaves ({why}); it is served read-only"); + false + } + Err(e) => { + println!("fileserver: recovering the FAT volume failed ({e}); it is served read-only"); + false + } + }; Ok(Self { fs, cache, @@ -912,7 +941,7 @@ mod tests { }) .collect(); assert!(refused.is_empty(), "{stop}: {}", describe(&refused)); - let mut after = FatVolume::mount(Bytes::new(bytes), CLEAN_LIMIT, false, clock).unwrap_or_else(|e| panic!("{stop}: {e}")); + let mut after = FatVolume::mount(Bytes::new(bytes.clone()), CLEAN_LIMIT, false, clock).unwrap_or_else(|e| panic!("{stop}: {e}")); let n = after.open("a.txt", OPEN).unwrap(); let size = after.lstat("a.txt").unwrap().size as usize; let mut out = vec![0u8; size]; @@ -924,12 +953,179 @@ mod tests { held.len(), whole.len() ); + + // A writable mount takes back what the stop left, and keeps + // the file as the read-only mount found it. + let recovery: Epochs = Rc::new(std::cell::RefCell::new(vec![Vec::new()])); + let traced = Traced { image: Bytes::new(bytes.clone()), epochs: Rc::clone(&recovery) }; + drop(FatVolume::mount(traced, CLEAN_LIMIT, true, clock).unwrap_or_else(|e| panic!("{stop}: {e}"))); + let mut recovered = bytes.clone(); + written(&mut recovered, recovery.borrow().iter().flatten()); + let left = check(&recovered); + assert!(left.is_empty(), "{stop}, then recovered: {}", describe(&left)); + let mut again = FatVolume::mount(Bytes::new(recovered), CLEAN_LIMIT, false, clock).unwrap(); + let n = again.open("a.txt", OPEN).unwrap(); + let mut kept = vec![0u8; again.lstat("a.txt").unwrap().size as usize]; + assert_eq!(again.read(n, 0, &mut crate::volume::Buf(&mut kept)), Ok(size)); + assert!(kept == out, "{stop}: recovery changed the file a read-only mount read"); } written(&mut image, epoch); } assert!(grown_before_its_size, "no stop fell between the chain's growth and the size's write"); } + /// `op` on a volume `setup` made and synced, stopped with every flush + /// epoch before one on the disk and any of that one's writes: each state + /// is one a writable mount recovers rather than refuses, which the + /// checker then finds nothing in, and `holds` accepts what a read-only + /// mount of it reads. Whether any stop needed a repair. + fn stopped_anywhere( + setup: impl Fn(&mut FatVolume), + op: impl Fn(&mut FatVolume), + holds: impl Fn(&mut FatVolume>>, &str), + ) -> bool { + use toyos_fat32_check::{check, describe}; + let clock = || 1_717_245_296 * NANOS_PER_SEC; + let spec = spec_volume::fixture(); + let mut image = spec.bytes.clone(); + image.resize(spec.bytes.len().div_ceil(BLOCK) * BLOCK, 0); + let epochs: Epochs = Rc::new(std::cell::RefCell::new(vec![Vec::new()])); + let mut v = FatVolume::mount(Traced { image: Bytes::new(image.clone()), epochs: Rc::clone(&epochs) }, CLEAN_LIMIT, true, clock) + .unwrap(); + setup(&mut v); + assert_eq!(v.sync(), Ok(Vec::new())); + let synced = epochs.borrow().len() - 1; + op(&mut v); + assert_eq!(v.sync(), Ok(Vec::new())); + let epochs = epochs.borrow().clone(); + written(&mut image, epochs[..synced].iter().flatten()); + let mut repaired = false; + for (e, epoch) in epochs.iter().enumerate().skip(synced) { + assert!(epoch.len() <= 12, "epoch {e} has {} writes, too many to stop at every subset", epoch.len()); + for kept in 0..1u32 << epoch.len() { + let stop = format!("stopped in flush epoch {e} of {} with writes {kept:#b} of its {}", epochs.len(), epoch.len()); + let mut bytes = image.clone(); + written(&mut bytes, epoch.iter().enumerate().filter(|(i, _)| kept >> i & 1 == 1).map(|(_, w)| w)); + let recovery: Epochs = Rc::new(std::cell::RefCell::new(vec![Vec::new()])); + let traced = Traced { image: Bytes::new(bytes.clone()), epochs: Rc::clone(&recovery) }; + let mounted = FatVolume::mount(traced, CLEAN_LIMIT, true, clock).unwrap_or_else(|e| panic!("{stop}: {e}")); + assert!(mounted.writable(), "{stop}: recovery refused a state a stop of ours leaves"); + drop(mounted); + repaired |= recovery.borrow().iter().any(|w| !w.is_empty()); + written(&mut bytes, recovery.borrow().iter().flatten()); + let left = check(&bytes); + assert!(left.is_empty(), "{stop}, then recovered: {}", describe(&left)); + let mut after = FatVolume::mount(Bytes::new(bytes), CLEAN_LIMIT, false, clock).unwrap(); + holds(&mut after, &stop); + } + written(&mut image, epoch); + } + repaired + } + + const CREATE: OpenHow = OpenHow { create: true, create_new: false, truncate: false }; + const OPEN: OpenHow = OpenHow { create: false, create_new: false, truncate: false }; + + fn pattern(len: usize, seed: u32) -> Vec { + (0..len as u32).map(|i| (i % 251 + seed) as u8).collect() + } + + fn put(v: &mut FatVolume, path: &str, bytes: &[u8]) { + let n = v.open(path, CREATE).unwrap(); + v.write(n, 0, bytes).unwrap(); + v.close(n).unwrap(); + } + + /// `path`'s bytes, or `None` where it is not there. + fn read_all(v: &mut FatVolume>>, path: &str) -> Option> { + let size = v.lstat(path).ok()?.size as usize; + let n = v.open(path, OPEN).unwrap(); + let mut out = vec![0u8; size]; + assert_eq!(v.read(n, 0, &mut crate::volume::Buf(&mut out)), Ok(size)); + Some(out) + } + + /// An unlink — logkeeper's sweep of an old part — stopped anywhere: the + /// name is there with its bytes or gone, and its neighbour is untouched. + #[test] + fn an_unlink_stopped_anywhere_is_recovered() { + let repaired = stopped_anywhere( + |v| { + put(v, "old.log", &pattern(3 * BLOCK, 1)); + put(v, "kept.log", &pattern(2000, 2)); + }, + |v| v.unlink("old.log").unwrap(), + |v, stop| { + let old = read_all(v, "old.log"); + assert!(old.is_none() || old == Some(pattern(3 * BLOCK, 1)), "{stop}: old.log is neither gone nor whole"); + assert_eq!(read_all(v, "kept.log"), Some(pattern(2000, 2)), "{stop}: its neighbour"); + }, + ); + assert!(repaired, "no stop left an unlink anything to recover"); + } + + /// A shrink stopped anywhere: the file is its old length or its new one, + /// its kept bytes as they were. + #[test] + fn a_shrink_stopped_anywhere_is_recovered() { + let repaired = stopped_anywhere( + |v| put(v, "a.log", &pattern(4 * BLOCK, 3)), + |v| { + let n = v.open("a.log", OPEN).unwrap(); + v.truncate(n, 100).unwrap(); + v.close(n).unwrap(); + }, + |v, stop| { + let a = read_all(v, "a.log").unwrap_or_else(|| panic!("{stop}: a.log is gone")); + let whole = pattern(4 * BLOCK, 3); + assert!(a == whole || a == whole[..100], "{stop}: a.log is {} bytes, neither length", a.len()); + }, + ); + assert!(repaired, "no stop left a shrink anything to recover"); + } + + /// A rename into another directory stopped anywhere: the file is under + /// exactly one of its names, whole. + #[test] + fn a_rename_stopped_anywhere_is_recovered() { + let repaired = stopped_anywhere( + |v| { + v.mkdir("into").unwrap(); + put(v, "a.log", &pattern(2 * BLOCK, 4)); + }, + |v| v.rename("a.log", "into/a moved file.log").unwrap(), + |v, stop| { + let names = [read_all(v, "a.log"), read_all(v, "into/a moved file.log")]; + let whole = Some(pattern(2 * BLOCK, 4)); + assert!( + names == [whole.clone(), None] || names == [None, whole], + "{stop}: the file is not under exactly one name, whole" + ); + }, + ); + assert!(repaired, "no stop left a rename anything to recover"); + } + + /// A volume holding what no stop of ours leaves — here a directory whose + /// chain has a link that is not one — is served read-only and not a byte + /// of it is written. + #[test] + fn a_volume_recovery_refuses_is_served_read_only_and_untouched() { + let clock = || 1_717_245_296 * NANOS_PER_SEC; + let spec = spec_volume::fixture(); + let mut image = spec.bytes.clone(); + image.resize(spec.bytes.len().div_ceil(BLOCK) * BLOCK, 0); + let sub = spec.at("sub").first; + for copy in 0..spec_volume::NUM_FATS { + image[spec_volume::fat_offset(copy, sub)..][..4].copy_from_slice(&0u32.to_le_bytes()); + } + let epochs: Epochs = Rc::new(std::cell::RefCell::new(vec![Vec::new()])); + let v = FatVolume::mount(Traced { image: Bytes::new(image), epochs: Rc::clone(&epochs) }, CLEAN_LIMIT, true, clock) + .unwrap(); + assert!(!v.writable(), "a volume recovery refused is served writable"); + assert!(epochs.borrow().iter().all(Vec::is_empty), "a refused recovery wrote {:?}", epochs.borrow()); + } + /// What the driver says of a volume that stopped answering is `Io`, which /// no caller takes for a name that is not there. #[test] diff --git a/system/fileserver/src/main.rs b/system/fileserver/src/main.rs index a88a5a6f5be..54ee49863c4 100644 --- a/system/fileserver/src/main.rs +++ b/system/fileserver/src/main.rs @@ -928,7 +928,7 @@ impl Server { self.files.fsync(&mut *self.volume, named)?; // One that left nothing to make durable did the write-back's work. if !self.files.owes(&*self.volume) { - self.writeback.synced(false, Instant::now()); + self.writeback.synced(); } Ok(Answer::Reply(Reply::ok())) } @@ -1272,14 +1272,23 @@ impl Server { Ok(Answer::Reply(Reply::ok())) } - /// The volume synced, answered as every file fared. A file whose entry - /// was refused keeps the write-back due, so the next one tries it again. + /// The volume synced, answered as every file fared. A sync refused, + /// whole or for one file, keeps the write-back due, so a later one tries + /// it again. fn sync(&mut self) -> Result { - let unwritten = self.files.sync(&mut *self.volume)?; - self.writeback.synced(!unwritten.is_empty(), Instant::now()); - match unwritten.into_iter().next() { - Some((_, e)) => Err(e), - None => Ok(Answer::Reply(Reply::ok())), + let refused = match self.files.sync(&mut *self.volume) { + Ok(unwritten) => unwritten.into_iter().next().map(|(_, e)| e), + Err(e) => Some(e), + }; + match refused { + Some(e) => { + self.writeback.refused(Instant::now()); + Err(e) + } + None => { + self.writeback.synced(); + Ok(Answer::Reply(Reply::ok())) + } } } diff --git a/system/fileserver/src/writeback.rs b/system/fileserver/src/writeback.rs index b15d691973a..92d4850a464 100644 --- a/system/fileserver/src/writeback.rs +++ b/system/fileserver/src/writeback.rs @@ -1,5 +1,7 @@ //! When the sync nobody asked for is due: [`WRITEBACK`] after the first write -//! no sync has answered for, and again after a sync that left a file unwritten. +//! no sync has answered for, and again after a sync that was refused, whole or +//! for one file, at an interval that doubles with each refusal up to +//! [`REFUSED_MAX`]. use std::time::{Duration, Instant}; @@ -7,9 +9,15 @@ use std::time::{Duration, Instant}; /// write-back drained a closed file's pages on its next pass. pub const WRITEBACK: Duration = Duration::from_secs(2); +/// The longest a refused sync waits to be tried again. Policy: a device that +/// keeps refusing is said about once a minute, not once per [`WRITEBACK`]. +pub const REFUSED_MAX: Duration = Duration::from_secs(64); + #[derive(Default)] pub struct WriteBack { since: Option, + /// The interval since the last refusal; `None` is [`WRITEBACK`]. + refused: Option, } impl WriteBack { @@ -18,24 +26,34 @@ impl WriteBack { self.since.get_or_insert(now); } + fn interval(&self) -> Duration { + self.refused.unwrap_or(WRITEBACK) + } + /// How long until it is due, while one is owed. pub fn left(&self, now: Instant) -> Option { - self.since.map(|at| WRITEBACK.saturating_sub(now.duration_since(at))) + self.since.map(|at| self.interval().saturating_sub(now.duration_since(at))) } /// Whether it is due at `now`; one that is, is spent. pub fn take_due(&mut self, now: Instant) -> bool { - let due = self.since.is_some_and(|at| now.duration_since(at) >= WRITEBACK); + let due = self.since.is_some_and(|at| now.duration_since(at) >= self.interval()); if due { self.since = None; } due } - /// A sync answered at `now`: one that left a file unwritten keeps the - /// write-back due, so the next one tries it again. - pub fn synced(&mut self, unwritten: bool, now: Instant) { - self.since = unwritten.then_some(now); + /// A sync made everything written durable: nothing is owed. + pub fn synced(&mut self) { + *self = Self::default(); + } + + /// A sync at `now` was refused, whole or for a file: the write-back stays + /// owed, after twice the last refusal's interval. + pub fn refused(&mut self, now: Instant) { + self.refused = Some(self.refused.map_or(WRITEBACK, |last| (last * 2).min(REFUSED_MAX))); + self.since = Some(now); } } @@ -44,7 +62,7 @@ mod tests { use super::*; #[test] - fn a_sync_that_left_a_file_unwritten_is_due_again() { + fn a_write_back_is_due_from_the_first_write_and_spent_when_taken() { let t0 = Instant::now(); let mut wb = WriteBack::default(); assert_eq!(wb.left(t0), None, "nothing written, nothing owed"); @@ -53,12 +71,32 @@ mod tests { assert!(!wb.take_due(t0 + WRITEBACK / 2), "due from the first write, not the last"); assert!(wb.take_due(t0 + WRITEBACK)); assert_eq!(wb.left(t0 + WRITEBACK), None, "spent"); + } + + /// **A refused write-back stays owed**, though nothing writes again, and + /// a device that keeps refusing is tried at a doubling interval that + /// stops at [`REFUSED_MAX`]; a sync that lands resets it. + #[test] + fn a_refused_sync_stays_due_at_a_backed_off_interval() { + let mut now = Instant::now(); + let mut wb = WriteBack::default(); + wb.dirtied(now); + now += WRITEBACK; + assert!(wb.take_due(now)); + let mut intervals = Vec::new(); + for _ in 0..8 { + wb.refused(now); + let left = wb.left(now).expect("a refused sync is still owed"); + intervals.push(left.as_secs()); + assert!(!wb.take_due(now + left - Duration::from_millis(1))); + now += left; + assert!(wb.take_due(now)); + } + assert_eq!(intervals, [2, 4, 8, 16, 32, 64, 64, 64]); - let t1 = t0 + 2 * WRITEBACK; - wb.synced(true, t1); - assert_eq!(wb.left(t1), Some(WRITEBACK)); - assert!(wb.take_due(t1 + WRITEBACK), "tried again"); - wb.synced(false, t1 + WRITEBACK); - assert_eq!(wb.left(t1 + WRITEBACK), None, "every file written"); + wb.synced(); + assert_eq!(wb.left(now), None, "every write durable"); + wb.dirtied(now); + assert_eq!(wb.left(now), Some(WRITEBACK), "the next write waits the ordinary interval"); } } diff --git a/system/nvme/src/nvme.rs b/system/nvme/src/nvme.rs index 954e8d50c71..2b94ab553f6 100644 --- a/system/nvme/src/nvme.rs +++ b/system/nvme/src/nvme.rs @@ -136,6 +136,44 @@ impl std::fmt::Display for Refusal { } } +/// Refuse a controller whose smallest memory page is not 4 KiB: `CC.MPS` is +/// written 0 and `MDTS` is read in 4 KiB units, both of which `CAP.MPSMIN` +/// (bits 51:48, a page of `2^(12 + MPSMIN)` bytes; Base 2.0 §3.1.4.1) must +/// allow. +fn page_size(cap: u64) -> Result<(), String> { + match (cap >> 48) & 0xF { + 0 => Ok(()), + min => Err(format!("the controller's smallest memory page is {} KiB, and this driver's is 4 KiB", 4 << min)), + } +} + +/// Namespace 1's sector size as a shift, from Identify Namespace's `NLBAF` +/// (byte 25, a zero-based count), `FLBAS` (byte 26) and the LBA format at +/// each byte offset `format_at` reads (NVM Command Set 1.0 §4.1.5.1). The +/// format index is `FLBAS` bits 3:0 with bits 6:5 above them, which a +/// namespace of 16 formats or fewer clears. A format carrying metadata is +/// refused: its sectors move more than `2^LBADS` bytes, or need a metadata +/// pointer this driver never sets. +fn sector_shift(nlbaf: u8, flbas: u8, format_at: impl Fn(usize) -> u32) -> Result { + let formats = nlbaf as usize + 1; + let index = (flbas & 0x0F) as usize | (((flbas >> 5) & 0b11) as usize) << 4; + if index >= formats { + return Err(format!("namespace 1 is in LBA format {index}, and it reports {formats}")); + } + let format = format_at(128 + 4 * index); + let metadata = format & 0xFFFF; + if metadata != 0 { + return Err(format!("namespace 1's format carries {metadata} bytes of metadata per sector")); + } + let lbads = (format >> 16) & 0xFF; + if !(9..=12).contains(&lbads) { + return Err(format!( + "namespace 1 has 2^{lbads}-byte sectors, and a 4 KiB block is whole sectors only for 512..=4096" + )); + } + Ok(lbads) +} + /// One submission entry's fields (§4.2); the identifier is the slot's. #[derive(Clone, Copy, Default)] struct Command { @@ -237,6 +275,7 @@ impl Controller { // which this struct holds. let regs = unsafe { Window::new(bar.as_ptr(), bar_bytes as usize) }; let cap: u64 = regs.read(REG_CAP); + page_size(cap).map_err(Refusal::Geometry)?; let stride = 4usize << ((cap >> 32) & 0xF); if DOORBELLS + (2 * MAX_IO_QUEUES as usize + 2) * stride > bar_bytes as usize { return Err(Refusal::NoRegisters); @@ -351,8 +390,8 @@ impl Controller { let mdts: u8 = id.read(77); let vwc: u8 = id.read(525); self.vwc = vwc & 1 != 0; - // A power of two in units of the minimum page, 4 KiB here; zero is no - // limit. + // A power of two in units of the minimum page, which `page_size` + // holds at 4 KiB; zero is no limit. self.max_blocks = match mdts { 0 => u32::MAX, n if n >= 20 => u32::MAX, @@ -372,15 +411,7 @@ impl Controller { Command { opcode: ADMIN_IDENTIFY, nsid: 1, prp1: buf, cdw10: 0, ..Command::default() }, )?; let nsze: u64 = id.read(0); - let flbas: u8 = id.read(26); - let format: u32 = id.read(128 + 4 * (flbas & 0x0F) as usize); - let lbads = (format >> 16) & 0xFF; - if !(9..=12).contains(&lbads) { - return Err(Refusal::Geometry(format!( - "namespace 1 has 2^{lbads}-byte sectors, and a 4 KiB block is whole sectors only \ - for 512..=4096" - ))); - } + let lbads = sector_shift(id.read(25), id.read(26), |at| id.read(at)).map_err(Refusal::Geometry)?; self.lba_bytes = 1 << lbads; self.sectors = nsze; Ok(()) @@ -670,3 +701,56 @@ impl Controller { self.issued.iter().map(u64::to_string).collect::>().join("/") } } + +#[cfg(test)] +mod tests { + use super::{page_size, sector_shift}; + + /// An Identify Namespace page: `formats` LBA formats, each `(MS, LBADS)`. + fn namespace(formats: &[(u16, u8)]) -> impl Fn(usize) -> u32 { + let formats = formats.to_vec(); + move |at| { + let (ms, lbads) = formats[(at - 128) / 4]; + ms as u32 | (lbads as u32) << 16 + } + } + + #[test] + fn a_namespace_past_sixteen_formats_takes_the_index_high_bits() { + let mut formats = vec![(0, 9); 64]; + formats[0x23] = (0, 12); + assert_eq!(sector_shift(63, 0b0100_0011, namespace(&formats)), Ok(12)); + assert_eq!(sector_shift(63, 0b0000_0011, namespace(&formats)), Ok(9)); + // Bit 4 between the two halves is not the index's. + assert_eq!(sector_shift(63, 0b0101_0011, namespace(&formats)), Ok(12)); + } + + #[test] + fn a_format_index_past_the_count_is_refused() { + let formats = vec![(0, 9); 16]; + assert!(sector_shift(1, 2, namespace(&formats)).unwrap_err().contains("LBA format 2")); + assert!(sector_shift(15, 0b0010_0011, namespace(&formats)).unwrap_err().contains("LBA format 19")); + } + + #[test] + fn a_format_with_metadata_is_refused_extended_or_separate() { + let formats = [(8, 9), (64, 12)]; + // FLBAS bit 4 set: metadata interleaved with the data. + assert!(sector_shift(1, 0b1_0000, namespace(&formats)).unwrap_err().contains("8 bytes of metadata")); + // Clear: a separate buffer. + assert!(sector_shift(1, 1, namespace(&formats)).unwrap_err().contains("64 bytes of metadata")); + } + + #[test] + fn a_sector_outside_512_to_4096_bytes_is_refused() { + assert!(sector_shift(0, 0, namespace(&[(0, 13)])).is_err()); + assert_eq!(sector_shift(0, 0, namespace(&[(0, 9)])), Ok(9)); + } + + #[test] + fn a_controller_whose_smallest_page_is_not_4_kib_is_refused() { + assert_eq!(page_size(0x0000_0020_FF01_003F), Ok(())); + let cap = 0x0001_0020_FF01_003F | (0xF << 52); + assert!(page_size(cap).unwrap_err().contains("8 KiB")); + } +} diff --git a/system/shell/src/main.rs b/system/shell/src/main.rs index f6ef42eaf50..1373dae68fe 100644 --- a/system/shell/src/main.rs +++ b/system/shell/src/main.rs @@ -636,7 +636,7 @@ fn execute_pipeline(pipeline: &[SimpleCommand]) -> bool { return execute_simple(&pipeline[0], None); } - let mut children = Vec::new(); + let mut children: Vec = Vec::new(); let mut prev_stdout: Option = None; for (i, cmd) in pipeline.iter().enumerate() { @@ -650,8 +650,15 @@ fn execute_pipeline(pipeline: &[SimpleCommand]) -> bool { command.stdin(stdout); } else if is_first { if let Some(ref path) = cmd.input_file { - if let Ok(file) = File::open(path) { - command.stdin(file); + match File::open(path) { + Ok(file) => { + command.stdin(file); + } + Err(e) => { + eprintln!("shell: {path}: {e}"); + set_status_code(1); + return false; + } } } else if cmd.heredoc.is_some() { command.stdin(Stdio::piped()); @@ -660,8 +667,16 @@ fn execute_pipeline(pipeline: &[SimpleCommand]) -> bool { if !is_last { command.stdout(Stdio::piped()); - } else { - apply_redirect(&mut command, cmd); + } else if !apply_redirect(&mut command, cmd) { + // The commands before it end on the pipe this one would have read, + // and are the line an interrupt reaches meanwhile. + drop(command); + waiting_for(&children.iter().collect::>()); + for mut child in children { + let _ = child.wait(); + } + waited(1); + return false; } match command.spawn() { @@ -754,7 +769,9 @@ fn execute_simple(cmd: &SimpleCommand, piped_stdin: Option bool { } let mut command = Command::new(program); command.args(&cmd.args[2..]).independent().detached(); - apply_redirect(&mut command, cmd); + if !apply_redirect(&mut command, cmd) { + return false; + } match command.spawn() { Ok(_) => { set_status_code(0); @@ -894,55 +913,53 @@ fn unstarted(cmd: &SimpleCommand, e: &std::io::Error) { } } -fn apply_redirect(command: &mut Command, cmd: &SimpleCommand) { - match &cmd.redirect { - Some(Redirect::Truncate(path)) => { - if let Ok(file) = File::create(path) { - command.stdout(file); - } else { - eprintln!("cannot open: {}", path); - } - } - Some(Redirect::Append(path)) => { - if let Ok(file) = OpenOptions::new().create(true).append(true).open(path) { - command.stdout(file); - } else { - eprintln!("cannot open: {}", path); - } - } - None => {} - } - match &cmd.stderr { - Some(StderrRedirect::ToStdout) => { - // 2>&1: stderr goes where stdout goes - match &cmd.redirect { - Some(Redirect::Truncate(path)) => { - if let Ok(file) = OpenOptions::new().write(true).open(path) { - command.stderr(file); - } - } - Some(Redirect::Append(path)) => { - if let Ok(file) = OpenOptions::new().append(true).open(path) { - command.stderr(file); - } +/// Open `cmd`'s redirections onto `command`. One that does not open is said +/// with what its open answered, the status is 1 and false is answered: the +/// command is not run, since its output would go where the line did not send +/// it. `2>&1` onto a file shares stdout's open file and its offset. +fn apply_redirect(command: &mut Command, cmd: &SimpleCommand) -> bool { + let opened = (|| -> Result<(), (&str, io::Error)> { + let open = |path: &str, append: bool| { + let mut how = OpenOptions::new(); + how.create(true); + if append { how.append(true) } else { how.write(true).truncate(true) }; + how.open(path) + }; + let stdout = match &cmd.redirect { + Some(Redirect::Truncate(path)) => Some((path, open(path, false).map_err(|e| (path.as_str(), e))?)), + Some(Redirect::Append(path)) => Some((path, open(path, true).map_err(|e| (path.as_str(), e))?)), + None => None, + }; + match &cmd.stderr { + Some(StderrRedirect::ToStdout) => match &stdout { + Some((path, file)) => { + command.stderr(file.try_clone().map_err(|e| (path.as_str(), e))?); } + // stdout is inherited or piped — stderr follows None => { - // stdout is inherited or piped — stderr follows command.stderr(Stdio::inherit()); } + }, + Some(StderrRedirect::ToFile(path)) => { + command.stderr(open(path, false).map_err(|e| (path.as_str(), e))?); } - } - Some(StderrRedirect::ToFile(path)) => { - if let Ok(file) = File::create(path) { - command.stderr(file); + Some(StderrRedirect::AppendFile(path)) => { + command.stderr(open(path, true).map_err(|e| (path.as_str(), e))?); } + None => {} } - Some(StderrRedirect::AppendFile(path)) => { - if let Ok(file) = OpenOptions::new().create(true).append(true).open(path) { - command.stderr(file); - } + if let Some((_, file)) = stdout { + command.stdout(file); + } + Ok(()) + })(); + match opened { + Ok(()) => true, + Err((path, e)) => { + eprintln!("shell: {path}: {e}"); + set_status_code(1); + false } - None => {} } } diff --git a/tests/guest/libc/spawn_system.c b/tests/guest/libc/spawn_system.c index b816ef5fcff..97a27394872 100644 --- a/tests/guest/libc/spawn_system.c +++ b/tests/guest/libc/spawn_system.c @@ -1,5 +1,5 @@ /* system and pclose answer a command's exit, run by /system/bin/shell -c, - and popen its output. */ + and popen its output; a command whose redirect does not open is not run. */ #include "check.h" int main(void) { @@ -19,6 +19,15 @@ int main(void) { CHECK(ends != NULL, "popen of exit 3"); status = pclose(ends); CHECK(WIFEXITED(status) && WEXITSTATUS(status) == 3, "pclose of exit 3 answered %#x", status); - printf("spawn_system: system and pclose answered the command's exit 3, and popen its output\n"); + + /* A redirect that does not open runs nothing: no output, and a failure. */ + FILE *refused = popen("echo unsent > /tmp/no-such-directory/file", "r"); + CHECK(refused != NULL, "popen of a refused redirect"); + while (fgets(line, sizeof line, refused) != NULL) + CHECK(strstr(line, "unsent") == NULL, "a refused redirect's command ran and said %s", line); + status = pclose(refused); + CHECK(!(WIFEXITED(status) && WEXITSTATUS(status) == 0), "a refused redirect answered %#x", status); + printf("spawn_system: system and pclose answered the command's exit 3, popen its output, " + "and a refused redirect ran nothing\n"); return 0; } diff --git a/tests/toyos.rs b/tests/toyos.rs index 41e6e9c5553..d9896346a24 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -5257,15 +5257,17 @@ fn nvme_disk_boots(test_config: &Path) -> Result<(), String> { if let Some(usb) = qemu::profile_argv(&options).iter().find(|arg| arg.contains("usb") || arg.contains("xhci")) { return Err(format!("this machine was to have no USB controller, and QEMU is given {usb}")); } - let qemu = QemuInstance::boot_with_options(test_config, &[], &[], options); - let log = serial::Serial::boot(&qemu); - log.must_be_clean()?; + let mut qemu = QemuInstance::boot_with_options(test_config, &[], &[], options); + serial::Serial::boot(&qemu).must_be_clean()?; // Every role is on this disk, so each is served off the nvme driver's - // sessions through the block service, or the boot left it absent. + // sessions through the block service, or the boot left it absent. LOG's + // server says so once its recovery has read the volume whole, which may + // be after the runner is ready. + let mut console = qemu.boot_log().to_string(); for role in NVME_ROLES { - log.must_say(&format!("fileserver: {role}"))?; + qemu::await_marker(&mut qemu, &mut console, &format!("fileserver: {role}"), "the role to be served")?; } - Ok(()) + serial::Serial::named("the nvme boot", console).must_be_clean() } /// The NVMe driver's own account of the designation, which must be that it