From db2346c39abc35ac4a35812a63ac4acdf7d3480f Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:13:02 +0100 Subject: [PATCH 01/12] fileserver: a refused sync keeps the write-back due, backing off A write-back that came due was spent before its sync ran, and a sync the volume refused whole (the cache's flush or the device) returned before anything recorded it, so the dirty blocks the cache still held waited for the next client write or sync. A sync refused for one file stayed due, but was retried and said every two seconds for the rest of the boot. Now every refused sync, whole or for one file, client's or write-back's, keeps the write-back owed: tried again after WRITEBACK, then at an interval that doubles to REFUSED_MAX (64 s), and reset by the first sync that lands. Host test: `writeback::tests::a_refused_sync_stays_due_at_a_backed_off_interval`. Closes issues/a-write-back-the-device-refused-waits-for-the-next-write.md. Co-Authored-By: Claude Opus 5.5 --- ...device-refused-waits-for-the-next-write.md | 21 ------ system/fileserver/src/main.rs | 25 ++++--- system/fileserver/src/writeback.rs | 66 +++++++++++++++---- 3 files changed, 69 insertions(+), 43 deletions(-) delete mode 100644 issues/a-write-back-the-device-refused-waits-for-the-next-write.md 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/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"); } } From 64864dd0001825729677c4922682218f4c5c73a7 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:14:23 +0100 Subject: [PATCH 02/12] fileserver: a file in a deleted file's blocks holds its own bytes on DATA `issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md` measured, in 2026-09, a new file whose first extent on the device still held a deleted symlink's bytes while the guest read it right. DATA was then the kernel's BCFS volume; #1152 (e11b6a784) replaced it with native bcachefs, whose file data passes the block cache straight to the device. Nothing had looked again. The host test its exit asks for: a file takes 60% of a 64 MiB volume, is deleted and synced, and a second as large is written, which cannot fit without the freed blocks; the volume is stopped and mounted again, so the read comes from the device, and the second file reads back whole. Negative control: `Cache::write_data` answering Ok without writing fails it: the read is refused (`Io`), the extent's checksum not matching what the device holds. The case of a refused write-back before a stop is issues/a-sync-commits-the-entry-of-a-file-whose-pages-were-refused.md's, which no longer points at the closed file. Co-Authored-By: Claude Opus 5.5 --- ...t-on-data-keeps-the-deleted-files-bytes.md | 59 ------------------- ...ntry-of-a-file-whose-pages-were-refused.md | 3 +- system/fileserver/src/data.rs | 26 ++++++++ 3 files changed, 27 insertions(+), 61 deletions(-) delete mode 100644 issues/a-reallocated-extent-on-data-keeps-the-deleted-files-bytes.md 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-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/system/fileserver/src/data.rs b/system/fileserver/src/data.rs index 9f45317e595..32733ebd213 100644 --- a/system/fileserver/src/data.rs +++ b/system/fileserver/src/data.rs @@ -762,6 +762,32 @@ 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(); From 9b546176394310a3599cc36f3f3705ad628a6f53 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:14:30 +0100 Subject: [PATCH 03/12] toyos-fat32: a shrink stops naming clusters before it frees them `set_len` to a shorter length freed the chain's tail at once and recorded the new size only at the caller's next `flush_meta`. A stop between the two left an entry naming more bytes than its chain held, and a shrink to zero an entry naming a free first cluster, which the next allocation handed to another file. `shrink_chain` now writes the entry's shorter size, and for a shrink to zero its cleared first cluster, before the first free. A refused entry write is undone like any call's; once it lands, the free only goes forward. A stop anywhere leaves at most the freed clusters orphaned, which `a_stop_at_any_write_leaves_only_the_named_windows` now holds for both truncation scenarios with their `own` arms deleted. Negative control: the old `shrink_chain` against the stricter test fails at `truncate: stopped at write 2, Refused: /dated.log: DIR_FileSize is 2560 bytes, which needs 5 clusters, and the chain holds 2`. Closes issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md. Co-Authored-By: Claude Opus 5.5 --- ...ters-before-the-entry-stops-naming-them.md | 26 ------------------- lib/fat32/src/fs.rs | 20 ++++++++++++++ lib/fat32/src/repair.rs | 5 ++-- lib/fat32/tests/refused_writes.rs | 11 +++----- 4 files changed, 27 insertions(+), 35 deletions(-) delete mode 100644 issues/a-shrink-frees-clusters-before-the-entry-stops-naming-them.md 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/lib/fat32/src/fs.rs b/lib/fat32/src/fs.rs index ed418945b77..4fcb9906fba 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,22 @@ 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?; + // 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; diff --git a/lib/fat32/src/repair.rs b/lib/fat32/src/repair.rs index 85e74476ded..42d726f8de1 100644 --- a/lib/fat32/src/repair.rs +++ b/lib/fat32/src/repair.rs @@ -40,8 +40,9 @@ //! - **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. diff --git a/lib/fat32/tests/refused_writes.rs b/lib/fat32/tests/refused_writes.rs index 65c167c822c..ace9ad689ae 100644 --- a/lib/fat32/tests/refused_writes.rs +++ b/lib/fat32/tests/refused_writes.rs @@ -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,8 +665,8 @@ 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, owned by its issue + // file and not by this repair. 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| { @@ -677,9 +677,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); From f34bd07302b2dbd44afb2e1dfd266ec7cea0f267 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:14:41 +0100 Subject: [PATCH 04/12] shell: a command whose redirect does not open is not run `apply_redirect` printed `cannot open: ` for a `>` or `>>` target it could not open and its callers spawned the command anyway, its output going to the shell's own; a `2>` target or a pipeline's `<` file that did not open was dropped silently. `echo secret > /refused/path` wrote `secret` to the terminal and answered the command's status. Now every redirection is opened before the spawn. One that does not open is said with the error its open answered, the status is 1, and nothing is run; in a pipeline the stages already started are waited for, ending on the pipe the refused stage would have read. `2>&1` onto a file shares stdout's open file rather than opening the path a second time, whose own offset wrote over stdout's output. `spawn_system` (libc_children) popens `echo unsent > /tmp/no-such-directory/file` and finds no `unsent` on the stream and a non-zero status. Closes issues/the-shell-runs-a-command-whose-redirect-it-could-not-open.md. Co-Authored-By: Claude Opus 5.5 --- ...ommand-whose-redirect-it-could-not-open.md | 26 ----- system/shell/src/main.rs | 108 ++++++++++-------- tests/guest/libc/spawn_system.c | 13 ++- 3 files changed, 72 insertions(+), 75 deletions(-) delete mode 100644 issues/the-shell-runs-a-command-whose-redirect-it-could-not-open.md 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/system/shell/src/main.rs b/system/shell/src/main.rs index f6ef42eaf50..92434c9f69c 100644 --- a/system/shell/src/main.rs +++ b/system/shell/src/main.rs @@ -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,13 @@ 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. + drop(command); + for mut child in children { + let _ = child.wait(); + } + return false; } match command.spawn() { @@ -754,7 +766,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 +910,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; } From ea571baadfcf10641d8e09568dda7646ed12916c Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:14:41 +0100 Subject: [PATCH 05/12] nvme: the whole LBA format index, and no format or page size it cannot drive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Controller::open` read three geometry fields partly: - the LBA format index from `FLBAS` bits 3:0 only; bits 6:5 are its upper bits on a namespace of more than 16 formats (NVM Command Set 1.0 §4.1.5.1); - no format's metadata size (`MS`): an extended-LBA format moves more than `2^LBADS` bytes per sector, and a separate-buffer one needs an `MPTR` this driver never sets; - `CC.MPS` written 0 and `MDTS` read in 4 KiB units without `CAP.MPSMIN` (Base 2.0 §3.1.4.1). Now the index is all six bits and checked against `NLBAF`, a format with metadata is refused, and so is a controller whose smallest page is not 4 KiB, each by name as `Refusal::Geometry`. Host tests over modelled Identify Namespace pages and CAP values hold each case. Closes issues/the-nvme-driver-reads-a-namespaces-format-and-page-size-partly.md. Co-Authored-By: Claude Opus 5.5 --- ...-namespaces-format-and-page-size-partly.md | 42 ------- system/nvme/src/nvme.rs | 106 ++++++++++++++++-- 2 files changed, 95 insertions(+), 53 deletions(-) delete mode 100644 issues/the-nvme-driver-reads-a-namespaces-format-and-page-size-partly.md 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/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")); + } +} From 668160b1ee3c9770f94bc93b418887214e24e3b5 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 11:27:44 +0100 Subject: [PATCH 06/12] toyos-fat32: a writable mount takes back what a stop left A reset, panic or power loss inside a `toyos-fat32` call leaves what its repair module's open windows name: orphaned clusters, one FAT entry whose copies differ, one partial long-name run, and the growth window's chain past its size. The repair lived in memory and nothing on the mount path took any of it back, so every unclean stop leaked for good and left the free count a stale hint (T14, #971). `Fat32::recover`, which the file server runs on a writable FAT mount before its first write: - every mirror's entry that differs takes the active FAT's value, a state the stopped call or its repair passed through; - the tree is walked from the root, marking each directory's chain and each file's up to its size; a long-name entry no short entry completes is freed, an empty file's cluster is dropped from its entry, and a chain past its size is ended there; - every allocated cluster nothing reached is freed, and the free count is the one that scan finds; - then a sync. Each step only moves towards consistency, so a stop inside it leaves no worse a volume. A cluster two entries reach, a link that is not one, or a chain short of its size is counted and left as it is. It runs on every writable mount rather than on fatgen103's ClnShutBitMask, as the record proposed: the bit would cost a dirty and a clean FAT write around every sync (logkeeper syncs every round), while a scan of the LOG volume's FAT and its small tree costs a few hundred kilobytes read once a boot, and a clean volume is left untouched (`Recovered::nothing`). Tests: - `a_recovery_after_a_stop_at_any_write_leaves_a_consistent_volume` (`lib/fat32/tests/refused_writes.rs`): every scenario stopped at every write, each way, is recovered and then clean under `toyos-fat32-check` with the FATs agreeing, but rename's own cross-link (issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md), which it leaves; a second recovery does nothing; and each of the four repairs met a stop that needed it. - The file server's stopped-append test now also mounts each stopped state writable: the checker finds nothing and the file reads as the read-only mount read it. Negative controls, each applied and restored: skipping the lost-cluster free, the mirror copy, the long-name free or the chain end each fails the first test, at `append` write 2, `append` write 1, `extend-dir` write 6 and `append` write 4 respectively. Closes issues/a-stop-mid-append-leaves-a-fat-volume-s-leak-for-ever.md and issues/nothing-reclaims-what-a-stop-leaves-on-a-fat-volume.md. Co-Authored-By: Claude Opus 5.5 --- ...end-leaves-a-fat-volume-s-leak-for-ever.md | 29 -- ...aims-what-a-stop-leaves-on-a-fat-volume.md | 34 --- lib/fat32/src/dir.rs | 6 + lib/fat32/src/fat.rs | 4 +- lib/fat32/src/lib.rs | 2 + lib/fat32/src/recover.rs | 263 ++++++++++++++++++ lib/fat32/src/repair.rs | 5 +- lib/fat32/tests/refused_writes.rs | 47 +++- system/fileserver/src/fat.rs | 37 ++- 9 files changed, 356 insertions(+), 71 deletions(-) delete mode 100644 issues/a-stop-mid-append-leaves-a-fat-volume-s-leak-for-ever.md delete mode 100644 issues/nothing-reclaims-what-a-stop-leaves-on-a-fat-volume.md create mode 100644 lib/fat32/src/recover.rs 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/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/lib/fat32/src/dir.rs b/lib/fat32/src/dir.rs index dc1914327f4..465e2f92ea4 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; 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/lib.rs b/lib/fat32/src/lib.rs index f18b2e7df22..8fb7e25d845 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; 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..035b7ebfeb1 --- /dev/null +++ b/lib/fat32/src/recover.rs @@ -0,0 +1,263 @@ +//! What a writable mount takes back of a volume a stop left: the states +//! [`crate::repair`]'s open windows name, made consistent before the first +//! write. Each step moves towards consistency on its own, so a stop inside +//! this leaves no worse a volume than it found. +//! +//! - **A split FAT entry** takes the active copy's value, which holds a state +//! the stopped call or its repair passed through. +//! - **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 long-name entry no short entry completes** is freed. +//! - **A cluster no entry reaches** is freed. +//! - **The free count** is counted again. +//! +//! What a stop cannot leave and this cannot settle — a cluster two entries +//! reach, a link that is not one, a chain shorter than its size — is counted +//! as [`Recovered::unrepaired`] and left as it is. + +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; +use toyos_blockdev::BlockDev; + +/// What [`Fat32::recover`] changed, and what it found and left. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct Recovered { + /// FAT entries whose mirrors took the active copy's value. + pub mirrors: u32, + /// Files whose chain was ended at their size; what it held past that is + /// counted in [`Self::lost`]. + pub cut: u32, + /// Long-name entries no short entry completed, freed. + pub long_names: u32, + /// Allocated clusters no entry reached, freed. + pub lost: u32, + /// Inconsistencies left as they were. + pub unrepaired: 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 + } +} + +impl Fat32 { + /// Take back what a stop left, then sync. 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 mut done = Recovered { mirrors: self.reconcile_mirrors().await?, ..Recovered::default() }; + let mut reached = Reached::new(self.geom.cluster_count); + self.reach_tree(&mut reached, &mut done).await?; + let free; + (done.lost, free) = self.free_unreached(&reached).await?; + self.fsinfo.free_count = Some(free); + self.fsinfo.next_free = None; + self.fsinfo.dirty = true; + self.sync().await?; + Ok(done) + } + + /// Every mirror's entry that differs from the active FAT's takes its + /// value. + async fn reconcile_mirrors(&mut self) -> Result { + // Mirroring off: the other copies are not the volume's. + if self.geom.active_fat.is_some() { + return Ok(0); + } + let active = 0; + let bps = self.geom.bytes_per_sector as usize; + let sectors = self.geom.max_cluster() as u64 * 4 / bps as u64 + 1; + let mut fixed = 0; + let mut ours = vec![0u8; bps]; + let mut theirs = vec![0u8; bps]; + self.invalidate_sector(); + for sector in 0..sectors { + let at = sector * bps as u64; + self.dev.read_at(self.geom.fat_base_offset(active) + at, &mut ours).await?; + for fat in self.geom.fat_mirrors().filter(|&f| f != active) { + self.dev.read_at(self.geom.fat_base_offset(fat) + at, &mut theirs).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 }; + let value = entry(&ours); + if entry(&theirs) != value { + self.write_fat_entry(fat, cluster, value).await?; + fixed += 1; + } + } + } + } + Ok(fixed) + } + + /// Mark every cluster the tree reaches from the root, freeing each + /// long-name entry no short entry completes, then end each file's chain + /// at its size: the rest is reached by nothing, and freed with the lost. + async fn reach_tree(&mut self, reached: &mut Reached, done: &mut Recovered) -> Result<(), Error> { + let whole = self.geom.cluster_count as u64; + let bpc = self.geom.bytes_per_cluster() as u64; + let mut dirs = Vec::new(); + let mut long = Vec::new(); + let root = self.geom.root(); + match self.reach_chain(root, whole, reached).await? { + Ok(_) => dirs.push(root), + Err(()) => done.unrepaired += 1, + } + while let Some(dir) = dirs.pop() { + let mut scan = DirScan::new(dir); + let mut after = 0u32; + loop { + let found = match scan.next(self).await { + Ok(found) => found, + Err(Error::CorruptDirectory | Error::CorruptChain) => { + done.unrepaired += 1; + break; + } + Err(e) => return Err(e), + }; + let end = found.as_ref().map_or(scan.scanned(), |l| l.first_index); + done.long_names += self.free_orphan_names(dir, after, end).await?; + let Some(located) = found 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) { + done.unrepaired += 1; + } + continue; + }; + if raw.is_dir() { + match self.reach_chain(first, whole, reached).await? { + Ok(_) => dirs.push(first), + Err(()) => done.unrepaired += 1, + } + } else if size == 0 { + // An empty file names no cluster. + let mut emptied = raw; + emptied.set_first_cluster(0); + self.put_entry_at(located.offset, &emptied).await?; + done.cut += 1; + } else { + match self.reach_chain(first, size.div_ceil(bpc), reached).await? { + Ok((n, last)) if n == size.div_ceil(bpc) => long.push(last), + _ => done.unrepaired += 1, + } + } + } + } + for last in long { + if self.next_cluster(last).await?.is_some() { + self.set_fat_entry(last, END_OF_CHAIN).await?; + done.cut += 1; + } + } + Ok(()) + } + + /// 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), + } + } + } + + /// Free each long-name entry of `dir` from index `from` before `to`: + /// none of them is part of a name a short entry completes. + async fn free_orphan_names(&mut self, dir: Cluster, from: u32, to: u32) -> Result { + let mut freed = 0; + let mut cursor = EntryCursor::new(dir); + for index in from..to { + let Some(offset) = cursor.offset_of(self, index).await? else { break }; + let mut raw: RawEntry = self.read_entry_at(offset).await?; + if raw.is_lfn() && raw.0[0] != DELETED { + raw.0[0] = DELETED; + self.put_entry_at(offset, &raw).await?; + freed += 1; + } + } + Ok(freed) + } + + /// 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 42d726f8de1..4c5a54c7d1f 100644 --- a/lib/fat32/src/repair.rs +++ b/lib/fat32/src/repair.rs @@ -49,8 +49,9 @@ //! - **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 ace9ad689ae..48c847efda1 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, MAX_LFN_CHARS, MAX_REPAIR_STEPS}; use toyos_fat32_check::Complaint; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -726,6 +726,51 @@ 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, mounted again and recovered, is one the checker finds +/// nothing in — but rename's own, which recovery leaves and counts — 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 = now(fs.recover()).unwrap_or_else(|e| panic!("{context}: recover: {e}")); + seen.mirrors += recovered.mirrors; + seen.cut += recovered.cut; + seen.long_names += recovered.long_names; + seen.lost += recovered.lost; + let said = complaints(&fs.device().medium(), true); + let rename_own = |c: &Complaint| { + matches!(c, Complaint::CrossLinked { .. } | Complaint::DotDotCluster { .. } | Complaint::DotEntry { .. }) + }; + let left: Vec = said.into_iter().filter(|c| !(s.name == "rename" && rename_own(c))).collect(); + assert!(left.is_empty(), "{context}: {recovered:?}, and still\n{}", toyos_fat32_check::describe(&left)); + common::assert_fats_agree(&fs.geometry().clone(), &fs.device().medium()); + if s.name != "rename" { + assert_eq!(recovered.unrepaired, 0, "{context}"); + } + let again = now(fs.recover()).expect("recover again"); + assert!(again.nothing() || s.name == "rename", "{context}: a second recovery did {again:?}"); + } + } + } + // Each repair met at least one stop that needed it, or this shows nothing of it. + assert!(seen.mirrors > 0 && seen.cut > 0 && seen.long_names > 0 && seen.lost > 0, "{seen:?}"); +} + // ----------------------------------------------- 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/fat.rs b/system/fileserver/src/fat.rs index d0ee179c2f6..ccdcb27a0c6 100644 --- a/system/fileserver/src/fat.rs +++ b/system/fileserver/src/fat.rs @@ -17,6 +17,9 @@ //! 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 partial long name and a chain past its size. +//! //! A volume that is not FAT32 is left untouched: `toyos-fat32` has no format //! path and its probe writes nothing. @@ -25,7 +28,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}; use crate::cache::{Cache, Disk}; use crate::volume::{parent, Identities, Kind, Listed, Meta, Node, OpenHow, Out, Reserved, Volume, WriteBack, NANOS_PER_SEC}; @@ -100,7 +103,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 +111,19 @@ impl FatVolume { geom.bytes_per_sector, geom.bytes_per_cluster() ); + // Before the first write: a stop leaves leaks and splits nothing else + // takes back. One that fails leaves the volume no worse than it was. + if writable { + match cache.block_on(fs.recover()) { + Ok(done) if done.nothing() => {} + Ok(Recovered { mirrors, cut, long_names, lost, unrepaired }) => println!( + "fileserver: the FAT volume was not left whole: {mirrors} split FAT entries mended, {cut} \ + clusters past a file's size and {lost} no entry reached freed, {long_names} partial long \ + names erased; {unrepaired} inconsistencies left as they are" + ), + Err(e) => println!("fileserver: recovering the FAT volume failed ({e}); it is mounted as it is"), + } + } Ok(Self { fs, cache, @@ -912,7 +928,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,6 +940,21 @@ 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); } From f8db19fc2e4cb78ddb010d5b6463c9fa6a3dad88 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 12:06:29 +0100 Subject: [PATCH 07/12] shell: the pipeline's children are named a Vec f34bd0730's wait on the pipeline's started stages is the first use of `children` that does not fix its element type before `child.wait()`, and the ToyOS build of `shell` refused it (E0282). `cargo run -- --ci host` does not build `shell`, whose manifest names every host as failing; the image build does: `cargo run -- --build-only --arch aarch64` exit 0 with this. Co-Authored-By: Claude Opus 5.5 --- system/shell/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/shell/src/main.rs b/system/shell/src/main.rs index 92434c9f69c..3527ab3a4e2 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() { From 958cc39cb8896f058e5d9ca42882a8faa5977a88 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 12:50:30 +0100 Subject: [PATCH 08/12] toyos-fat32: recovery reads the volume whole first, and writes nothing it cannot explain Review r1 of #1185 found that recovery decided what was lost from a walk that could be incomplete and wrote before it knew it understood the volume: a directory it could not walk had every cluster under it freed while its entries still named them, and FAT 0 was copied over a differing mirror with no bound, before anything was checked. LOG is the stick a Mac also writes, so a FAT this crate did not write is the expected case. `Fat32::recover` now examines first and writes nothing on its way: - every directory is walked whole, and every file's chain followed to its size; - each FAT entry whose copies differ is noted. Anything no stop of this crate leaves refuses the recovery by name (`Recovery::Refused`), with every byte left as it was: - 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, or one ending in a link that is not one; - an empty file naming a cluster; - a directory with no `..`; - FAT copies that differ where a copy does not read whole. Only then does it write, each step moving towards consistency. Split copies are a stop's only where each copy reads whole, not where they differ in at most one entry. The file server's cache writes a flush's FAT blocks of both copies in any order, so a stop inside that flush splits every entry of the blocks it had not written. The file server's stopped-flush test met exactly that once recovery was bounded at one. Each copy is then the volume before or after the flush, which the walk over it reads whole (`walk_copy` reads the mirror as the active copy). A copy torn or written by something else does not read whole, and neither copy is written over. The file server serves a FAT volume recovery refused, or could not finish, read-only, and says why. Its recovery line names what each count counts: `ended` is files ended at their size, and the clusters past them are `lost`. The rename a stop leaves half done is resolved here too, by the coordinator's ruling: the order stays insert, erase, repoint. Erasing first would open a window in which neither name resolves, and recovery would free that chain as lost. An entry equal in every field but its name to one the walk already met, reaching the same chain, is the rename's second name. It is erased, so the file keeps its old name or its new one. Each directory's `..` is set to the parent its kept entry is in. Two entries that differ in any other field refuse the recovery. Tests (`lib/fat32/tests/refused_writes.rs`): - `a_recovery_after_a_stop_at_any_write_leaves_a_consistent_volume`, rename no longer exempt: - every scenario is stopped at every write, each way; - each is recovered, then clean under `toyos-fat32-check`; - a second recovery is `Recovered::default()`; - each of the six repairs met a stop that needed it. - Refused, each asserting the medium is byte-for-byte unchanged: - `a_directory_with_a_broken_link_is_refused_and_left`; - `a_file_cross_linked_into_a_directory_is_refused_and_left`; - `a_cross_link_between_two_files_is_refused_and_left`; - `copies_that_differ_where_one_does_not_read_whole_are_refused_and_left` (FAT 0 torn, then the mirror). - `copies_that_differ_where_each_reads_whole_are_recovered`: 128 differing entries, recovered to a clean volume with the files intact. - The file server's stopped-flush test now passes with recovery bounded by readability rather than a count. - `a_volume_recovery_refuses_is_served_read_only_and_untouched`: no write reaches the disk. Negative controls, each applied, run red and restored: - refusing nothing for a broken directory, for a cross-link, or for a copy that does not read whole fails its refusal test; - each of the six repairs skipped fails the stop test or the split test; - serving a refused volume writable fails the file server's test. Closes issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md. Co-Authored-By: Claude Opus 5.5 --- ...writes-leaves-a-chain-two-entries-reach.md | 34 -- lib/fat32/src/dir.rs | 2 +- lib/fat32/src/lib.rs | 2 +- lib/fat32/src/recover.rs | 317 +++++++++++++----- lib/fat32/tests/refused_writes.rs | 144 ++++++-- system/fileserver/src/fat.rs | 61 +++- 6 files changed, 404 insertions(+), 156 deletions(-) delete mode 100644 issues/a-rename-that-stops-between-its-writes-leaves-a-chain-two-entries-reach.md 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/lib/fat32/src/dir.rs b/lib/fat32/src/dir.rs index 465e2f92ea4..f94cd1ac87a 100644 --- a/lib/fat32/src/dir.rs +++ b/lib/fat32/src/dir.rs @@ -595,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/lib.rs b/lib/fat32/src/lib.rs index 8fb7e25d845..ceb7333f79e 100644 --- a/lib/fat32/src/lib.rs +++ b/lib/fat32/src/lib.rs @@ -91,6 +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; +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 index 035b7ebfeb1..95e326ad33f 100644 --- a/lib/fat32/src/recover.rs +++ b/lib/fat32/src/recover.rs @@ -1,20 +1,32 @@ //! What a writable mount takes back of a volume a stop left: the states -//! [`crate::repair`]'s open windows name, made consistent before the first -//! write. Each step moves towards consistency on its own, so a stop inside -//! this leaves no worse a volume than it found. +//! [`crate::repair`]'s open windows name, and the one a rename stopped +//! between its writes leaves. //! -//! - **A split FAT entry** takes the active copy's value, which holds a state -//! the stopped call or its repair passed through. +//! **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, a state the stopped +//! call, its repair or the stopped flush passed through. +//! - **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 long-name entry no short entry completes** is freed. -//! - **A cluster no entry reaches** is freed. -//! - **The free count** is counted again. -//! -//! What a stop cannot leave and this cannot settle — a cluster two entries -//! reach, a link that is not one, a chain shorter than its size — is counted -//! as [`Recovered::unrepaired`] and left as it is. +//! - **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; @@ -23,23 +35,35 @@ 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; +use crate::fs::{Fat32, Loc}; use toyos_blockdev::BlockDev; -/// What [`Fat32::recover`] changed, and what it found and left. +/// 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 { - /// FAT entries whose mirrors took the active copy's value. + /// Split FAT entries whose mirrors took the active copy's value. pub mirrors: u32, - /// Files whose chain was ended at their size; what it held past that is - /// counted in [`Self::lost`]. - pub cut: 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, - /// Inconsistencies left as they were. - pub unrepaired: u32, } impl Recovered { @@ -75,121 +99,243 @@ impl Reached { } } +/// 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. 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 { + /// 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 mut done = Recovered { mirrors: self.reconcile_mirrors().await?, ..Recovered::default() }; - let mut reached = Reached::new(self.geom.cluster_count); - self.reach_tree(&mut reached, &mut done).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(&reached).await?; + (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(done) + Ok(Recovery::Recovered(done)) } - /// Every mirror's entry that differs from the active FAT's takes its - /// value. - async fn reconcile_mirrors(&mut self) -> Result { - // Mirroring off: the other copies are not the volume's. + /// 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.** The + /// file server's cache writes a flush's FAT blocks of both copies in any + /// order, so a stop inside one splits every entry of the blocks it had + /// not written. Each copy then holds the volume before or after that + /// flush, which the walk over it reads whole; a copy torn or written by + /// something else does not, 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(0); + return Ok(splits); } - let active = 0; let bps = self.geom.bytes_per_sector as usize; let sectors = self.geom.max_cluster() as u64 * 4 / bps as u64 + 1; - let mut fixed = 0; let mut ours = vec![0u8; bps]; let mut theirs = vec![0u8; bps]; - self.invalidate_sector(); for sector in 0..sectors { let at = sector * bps as u64; - self.dev.read_at(self.geom.fat_base_offset(active) + at, &mut ours).await?; - for fat in self.geom.fat_mirrors().filter(|&f| f != active) { + self.dev.read_at(self.geom.fat_base_offset(0) + at, &mut ours).await?; + for fat in self.geom.fat_mirrors().filter(|&f| f != 0) { self.dev.read_at(self.geom.fat_base_offset(fat) + at, &mut theirs).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 }; - let value = entry(&ours); - if entry(&theirs) != value { - self.write_fat_entry(fat, cluster, value).await?; - fixed += 1; + if entry(&theirs) != entry(&ours) && splits.last().is_none_or(|&(c, _)| c != cluster) { + splits.push((cluster, entry(&ours))); } } } } - Ok(fixed) + Ok(splits) } - /// Mark every cluster the tree reaches from the root, freeing each - /// long-name entry no short entry completes, then end each file's chain - /// at its size: the rest is reached by nothing, and freed with the lost. - async fn reach_tree(&mut self, reached: &mut Reached, done: &mut Recovered) -> Result<(), Error> { + /// 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 mut dirs = Vec::new(); - let mut long = Vec::new(); let root = self.geom.root(); - match self.reach_chain(root, whole, reached).await? { - Ok(_) => dirs.push(root), - Err(()) => done.unrepaired += 1, + 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. + 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 found = match scan.next(self).await { - Ok(found) => found, + let located = match scan.next(self).await { + Ok(located) => located, Err(Error::CorruptDirectory | Error::CorruptChain) => { - done.unrepaired += 1; - break; + return Ok(Err(format!("the directory at cluster {} does not read whole", dir.raw()))); } Err(e) => return Err(e), }; - let end = found.as_ref().map_or(scan.scanned(), |l| l.first_index); - done.long_names += self.free_orphan_names(dir, after, end).await?; - let Some(located) = found else { break }; + 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) { - done.unrepaired += 1; + return Ok(Err(format!( + "an entry in the directory at cluster {} names cluster {} for {size} bytes", + dir.raw(), + raw.first_cluster() + ))); } continue; }; + let fields: [u8; 21] = raw.0[11..].try_into().expect("21 bytes"); + 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() { - match self.reach_chain(first, whole, reached).await? { - Ok(_) => dirs.push(first), - Err(()) => done.unrepaired += 1, + 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 { - // An empty file names no cluster. - let mut emptied = raw; - emptied.set_first_cluster(0); - self.put_entry_at(located.offset, &emptied).await?; - done.cut += 1; + return Ok(Err(unexplained("is named by an empty file"))); } else { - match self.reach_chain(first, size.div_ceil(bpc), reached).await? { - Ok((n, last)) if n == size.div_ceil(bpc) => long.push(last), - _ => done.unrepaired += 1, + 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"))), } } } } - for last in long { - if self.next_cluster(last).await?.is_some() { - self.set_fat_entry(last, END_OF_CHAIN).await?; - done.cut += 1; - } + 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(()) + Ok(Some(raw.first_cluster() == self.dotdot_target(parent))) } /// Mark up to `limit` clusters of the chain from `start`: their count and @@ -220,21 +366,18 @@ impl Fat32 { } } - /// Free each long-name entry of `dir` from index `from` before `to`: - /// none of them is part of a name a short entry completes. - async fn free_orphan_names(&mut self, dir: Cluster, from: u32, to: u32) -> Result { - let mut freed = 0; + /// 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 mut raw: RawEntry = self.read_entry_at(offset).await?; + let raw: RawEntry = self.read_entry_at(offset).await?; if raw.is_lfn() && raw.0[0] != DELETED { - raw.0[0] = DELETED; - self.put_entry_at(offset, &raw).await?; - freed += 1; + names.push(offset); } } - Ok(freed) + Ok(()) } /// Free every allocated cluster `reached` does not hold: how many, and diff --git a/lib/fat32/tests/refused_writes.rs b/lib/fat32/tests/refused_writes.rs index 48c847efda1..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, Recovered, 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)] @@ -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 rename's own write order leaves at a stop, owned by its issue - // file 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, @@ -727,9 +726,8 @@ 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, mounted again and recovered, is one the checker finds -/// nothing in — but rename's own, which recovery leaves and counts — and a -/// second recovery finds nothing to do. +/// 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(); @@ -747,28 +745,136 @@ fn a_recovery_after_a_stop_at_any_write_leaves_a_consistent_volume() { 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 = now(fs.recover()).unwrap_or_else(|e| panic!("{context}: recover: {e}")); + let recovered = match now(fs.recover()) { + Ok(Recovery::Recovered(done)) => done, + other => panic!("{context}: {other:?}"), + }; seen.mirrors += recovered.mirrors; - seen.cut += recovered.cut; + seen.renames += recovered.renames; + seen.parents += recovered.parents; seen.long_names += recovered.long_names; + seen.ended += recovered.ended; seen.lost += recovered.lost; - let said = complaints(&fs.device().medium(), true); - let rename_own = |c: &Complaint| { - matches!(c, Complaint::CrossLinked { .. } | Complaint::DotDotCluster { .. } | Complaint::DotEntry { .. }) - }; - let left: Vec = said.into_iter().filter(|c| !(s.name == "rename" && rename_own(c))).collect(); + 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()); - if s.name != "rename" { - assert_eq!(recovered.unrepaired, 0, "{context}"); - } let again = now(fs.recover()).expect("recover again"); - assert!(again.nothing() || s.name == "rename", "{context}: a second recovery did {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. - assert!(seen.mirrors > 0 && seen.cut > 0 && seen.long_names > 0 && seen.lost > 0, "{seen:?}"); + 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 diff --git a/system/fileserver/src/fat.rs b/system/fileserver/src/fat.rs index ccdcb27a0c6..6966ee2598e 100644 --- a/system/fileserver/src/fat.rs +++ b/system/fileserver/src/fat.rs @@ -18,7 +18,9 @@ //! 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 partial long name and a chain past its size. +//! 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. @@ -28,7 +30,7 @@ use std::rc::Rc; use toyos::fs; use toyos_blockdev::BlockDev; -use toyos_fat32::{Error, Fat32, FatTime, Recovered}; +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}; @@ -112,18 +114,29 @@ impl FatVolume { geom.bytes_per_cluster() ); // Before the first write: a stop leaves leaks and splits nothing else - // takes back. One that fails leaves the volume no worse than it was. - if writable { - match cache.block_on(fs.recover()) { - Ok(done) if done.nothing() => {} - Ok(Recovered { mirrors, cut, long_names, lost, unrepaired }) => println!( - "fileserver: the FAT volume was not left whole: {mirrors} split FAT entries mended, {cut} \ - clusters past a file's size and {lost} no entry reached freed, {long_names} partial long \ - names erased; {unrepaired} inconsistencies left as they are" - ), - Err(e) => println!("fileserver: recovering the FAT volume failed ({e}); it is mounted as it is"), - } - } + // 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, @@ -961,6 +974,26 @@ mod tests { assert!(grown_before_its_size, "no stop fell between the chain's growth and the size's write"); } + /// 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] From 33291dbf0d4b09b1372e7cf9e532fced63cf65a7 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 12:50:30 +0100 Subject: [PATCH 09/12] shell: an interrupt reaches the stages a refused redirect waits for Review r1 of #1185: when a pipeline's last redirect does not open, the shell waits for the stages it already started, and `sleep 1000 | cat > /bad` waited on children the interrupt path did not know. They are now the foreground line (`waiting_for`, `waited`), as the ordinary wait's are. Also the blank line between the file server's DATA tests. Co-Authored-By: Claude Opus 5.5 --- system/fileserver/src/data.rs | 1 + system/shell/src/main.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/system/fileserver/src/data.rs b/system/fileserver/src/data.rs index 32733ebd213..ef20eb3545e 100644 --- a/system/fileserver/src/data.rs +++ b/system/fileserver/src/data.rs @@ -762,6 +762,7 @@ 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. diff --git a/system/shell/src/main.rs b/system/shell/src/main.rs index 3527ab3a4e2..1373dae68fe 100644 --- a/system/shell/src/main.rs +++ b/system/shell/src/main.rs @@ -668,11 +668,14 @@ fn execute_pipeline(pipeline: &[SimpleCommand]) -> bool { if !is_last { command.stdout(Stdio::piped()); } else if !apply_redirect(&mut command, cmd) { - // The commands before it end on the pipe this one would have read. + // 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; } From e4111609266785456518d60c1ef51faba5614047 Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 13:38:27 +0100 Subject: [PATCH 10/12] toyos-fat32: an erase and its free, a shrink and its free, a rename's two names, a flush apart Review r2 of #1185: a write-back device such as the file server's cache orders nothing between two flushes. Three calls put two writes that must land in order into one flush: - `remove` (the erase and the chain's free; `release_replaced` is a remove); - a shrink (the shorter entry and the free past it); - `rename` (the new name and the old one's erase). A stop that landed the second write without the first left: - a live entry over a freed chain, which recovery now refuses, so one reset during logkeeper's sweep left LOG read-only for good, and on main it mounted writable and later cross-linked; - for a rename, a file under neither name, which recovery would then free. Each now flushes between the two, as growth already keeps its chain a flush before its entry. `crate::repair`'s header states the rule. Evidence, the file server's stopped-flush test generalised (`stopped_anywhere`): an unlink, a shrink and a rename into another directory, each stopped with every earlier flush epoch on the disk and any subset of one epoch's writes. In every state: - a writable mount recovers rather than refuses; - the checker then finds nothing; - the file is whole under one name, or its old or new length, and its neighbour is untouched. Negative controls, the current ordering without each flush, applied and restored: - unlink: recovery refused at epoch 4 writes 0b10; - rename: the file under neither name at epoch 3 writes 0b1; - shrink: recovery refused at epoch 3 writes 0b10. Also from the review: - `recover.rs`'s claim that a stopped flush leaves each copy before or after it was false; the comment now states what keeps a copy reading whole (an entry a flush after its chain, an erase a flush before its free). - `splits` compares every mirror in one pass over an entry, so a volume of more than two FATs counts and rewrites a cluster once. - A rename's second name is matched on every field but the name, now without byte 12, the short name's case, which `insert_entry` does not copy. - The header is rewrapped. Co-Authored-By: Claude Opus 5.5 --- lib/fat32/src/fs.rs | 3 + lib/fat32/src/recover.rs | 52 ++++++++------ lib/fat32/src/repair.rs | 5 ++ system/fileserver/src/fat.rs | 132 +++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 23 deletions(-) diff --git a/lib/fat32/src/fs.rs b/lib/fat32/src/fs.rs index 4fcb9906fba..f84130a95cd 100644 --- a/lib/fat32/src/fs.rs +++ b/lib/fat32/src/fs.rs @@ -844,6 +844,7 @@ impl Fat32 { 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; @@ -1088,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 }); @@ -1122,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/recover.rs b/lib/fat32/src/recover.rs index 95e326ad33f..d084d1321a6 100644 --- a/lib/fat32/src/recover.rs +++ b/lib/fat32/src/recover.rs @@ -9,11 +9,12 @@ //! 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: +//! 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, a state the stopped -//! call, its repair or the stopped flush passed through. +//! - **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 @@ -168,12 +169,13 @@ impl Fat32 { /// 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.** The - /// file server's cache writes a flush's FAT blocks of both copies in any - /// order, so a stop inside one splits every entry of the blocks it had - /// not written. Each copy then holds the volume before or after that - /// flush, which the walk over it reads whole; a copy torn or written by - /// something else does not, and then neither is written over. + /// **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 an erase a flush before + /// its chain's free, 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 { @@ -225,19 +227,20 @@ impl Fat32 { 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 mut theirs = 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 in self.geom.fat_mirrors().filter(|&f| f != 0) { - self.dev.read_at(self.geom.fat_base_offset(fat) + at, &mut theirs).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 entry(&theirs) != entry(&ours) && splits.last().is_none_or(|&(c, _)| c != cluster) { - splits.push((cluster, entry(&ours))); - } + 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))); } } } @@ -253,8 +256,9 @@ impl Fat32 { 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. - let mut named: BTreeMap = BTreeMap::new(); + // 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); @@ -282,7 +286,9 @@ impl Fat32 { } continue; }; - let fields: [u8; 21] = raw.0[11..].try_into().expect("21 bytes"); + 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()))); diff --git a/lib/fat32/src/repair.rs b/lib/fat32/src/repair.rs index 4c5a54c7d1f..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 diff --git a/system/fileserver/src/fat.rs b/system/fileserver/src/fat.rs index 6966ee2598e..e02e98f93fc 100644 --- a/system/fileserver/src/fat.rs +++ b/system/fileserver/src/fat.rs @@ -974,6 +974,138 @@ mod tests { 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. From 258e9197d36327e925974892eee40be4486d168e Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 13:47:04 +0100 Subject: [PATCH 11/12] tests: nvme_disk_boots waits for each role, LOG's after its recovery Guest run 42023343796 at `33291dbf0` reds `nvme_disk_boots`. LOG's server mounted at 1.983 s and the runner was ready at 2.004 s. The row read the boot log only up to the ready marker, and LOG's `serving` line comes after `Fat32::recover` has read the 34 MiB volume whole (19 ms on the T14's stick, more under QEMU). Being served before the runner is ready is no part of the row's claim, that every role is served off the NVMe disk, so it now waits on the guest for each role's line. Co-Authored-By: Claude Opus 5.5 --- tests/toyos.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/toyos.rs b/tests/toyos.rs index 41e6e9c5553..2d331dca24f 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -5257,13 +5257,15 @@ 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(()) } From 4937c2bdcdbe486cd414e89a2f693fba392d35ce Mon Sep 17 00:00:00 2001 From: japabu Date: Thu, 5 Nov 2026 14:29:35 +0100 Subject: [PATCH 12/12] tests: nvme_disk_boots checks the boot it waited for, LOG's mount included MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review r3 of #1185: once the row waits past the runner's READY for each role, its cleanliness check, which read the boot log only up to READY, no longer covered LOG's mount and recovery. It now checks the whole console it accumulated. (A LOG volume recovery refused would also miss the role's marker, `Log serving /log — FAT32, `, which a read-only mount says as `FAT32 read-only, `.) `recover.rs`'s comment points at `crate::repair`'s ordering rule instead of repeating two of its three cases. Co-Authored-By: Claude Opus 5.5 --- lib/fat32/src/recover.rs | 6 +++--- tests/toyos.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fat32/src/recover.rs b/lib/fat32/src/recover.rs index d084d1321a6..84c1a4feecd 100644 --- a/lib/fat32/src/recover.rs +++ b/lib/fat32/src/recover.rs @@ -172,9 +172,9 @@ impl Fat32 { /// **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 an erase a flush before - /// its chain's free, so the blocks differ only past a file's size or in - /// clusters nothing reaches. A copy torn or written by something else + /// 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?; diff --git a/tests/toyos.rs b/tests/toyos.rs index 2d331dca24f..d9896346a24 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -5267,7 +5267,7 @@ fn nvme_disk_boots(test_config: &Path) -> Result<(), String> { for role in NVME_ROLES { 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