diff --git a/issues/a-fat-files-mtime-through-its-handle-is-its-last-level.md b/issues/a-fat-files-mtime-through-its-handle-is-its-last-level.md deleted file mode 100644 index de11f4bb7e1..00000000000 --- a/issues/a-fat-files-mtime-through-its-handle-is-its-last-level.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-09-29 ---- - -# A FAT file's mtime through an open handle is its entry's last level, not its last write - -The file server's FAT volume answers a file's mtime off its directory entry -(`system/fileserver/src/fat.rs`, `node_meta` and `lstat`), and an entry is stamped -only when it is brought level: at the last close, an fsync or a sync. A write -moves nothing an `fstat` reads, so a writer that writes and `fstat`s reads the -stamp of the entry's last level, and the same file closed and opened again reads -the level that close made: one file, two mtimes. - -**Mechanism read off the code; not reproduced.** - -**Exit condition.** For a FAT file, the mtime `fstat` answers through a handle -after a write is the one it answers after the file is closed and opened again, -checked by a test that writes, `fstat`s, reopens and `fstat`s again. diff --git a/issues/storage-is-layers-and-a-role-is-a-filesystem.md b/issues/storage-is-layers-and-a-role-is-a-filesystem.md index 077dce9a3dd..3252db436e7 100644 --- a/issues/storage-is-layers-and-a-role-is-a-filesystem.md +++ b/issues/storage-is-layers-and-a-role-is-a-filesystem.md @@ -140,6 +140,23 @@ keeps the volumes' structures alone. Mapping them is memory stage 7b, the process-memory track's (`issues/process-memory-in-4-kib-pages-still-copies-each-programs-text.md`). +**A served file's metadata without a request**, in two stages: + +- **3a:** a held descriptor's `fstat` is its paged object's size, mtime and + version (`SYS_PAGED_STAT`). The version is the object's write span, and the + one data generation. +- **3b, deferred until memory stage 7b's mappings:** a client cache of names, + validated by a per-connection page the file server writes. The page carries + generations of the directories in the connection's view and of the files + it has answered about, so that `stat` of an existing file's path hits the + cache; that row is what stands against Linux's 1.5 µs. Its slots carry + identity and generation, and a client compares both under acquire. Server + death clears the page through the paged object's `Gone`. + - Why deferred: building it before mappings means reading slots by + syscall, plus a names-only pager for SYSTEM and a dirty allowance for + BOOT. That is an interim mechanism the mappings would replace. + - Exit: `fsrtt.std.stat` and `open_close`, measured on both benches. + LOG stays its own FAT32 partition through the dev phase, because a Mac has to read the stick; folding it into DATA in the installed product is deferred by the owner's ruling of 2026-09-26, and no stage above owes it yet. diff --git a/kernel/pure/paged.rs b/kernel/pure/paged.rs index b6129f9d227..d3a279be09e 100644 --- a/kernel/pure/paged.rs +++ b/kernel/pure/paged.rs @@ -223,6 +223,8 @@ pub struct Object { writing: u32, /// A call that did not take all it was given left its write under way. held: bool, + /// What its pager last set: nanoseconds, as the file's volume keeps it. + mtime: u64, } /// What a holder's read began at ([`Object::read_begin`]). @@ -247,6 +249,20 @@ impl Object { self.size.div_ceil(PAGE) } + pub fn mtime(&self) -> u64 { + self.mtime + } + + pub fn set_mtime(&mut self, mtime: u64) { + self.mtime = mtime; + } + + /// The version a holder's stat publishes: `None` while a write is under + /// way, as for a read. + pub fn published(&self) -> Option { + (self.writing == 0).then_some(self.version) + } + /// Where a holder's read begins: `None` while a write is under way. pub fn read_begin(&self) -> Option { (self.writing == 0).then_some(Version(self.version)) @@ -358,9 +374,9 @@ impl Pages { /// An object of `size` bytes, at most [`LAST_END`], named `id`: an id no /// live object of this table has, which a released one's may be, since /// [`release`](Self::release) takes every page it held. - pub fn object(&mut self, id: u32, size: u64) -> Object { + pub fn object(&mut self, id: u32, size: u64, mtime: u64) -> Object { assert!(size <= LAST_END, "an object of {size} bytes past the last page"); - Object { id, size, gone: false, head: NONE, pages: 0, version: 0, writing: 0, held: false } + Object { id, size, gone: false, head: NONE, pages: 0, version: 0, writing: 0, held: false, mtime } } fn hash(&self, object: u32, page: u64) -> usize { @@ -736,7 +752,7 @@ mod tests { #[test] fn a_read_counts_only_where_no_write_began_or_was_under_way() { let mut p = Pages::::new(8, 4).unwrap(); - let mut o = p.object(1, PAGE); + let mut o = p.object(1, PAGE, 0); let before = o.read_begin().expect("nothing writes yet"); o.write_begin(); assert_eq!(o.read_begin(), None, "a read meets the write in flight"); @@ -755,7 +771,7 @@ mod tests { #[test] fn a_short_call_holds_the_write_until_its_end() { let mut p = Pages::::new(8, 4).unwrap(); - let mut o = p.object(2, PAGE); + let mut o = p.object(2, PAGE, 0); assert_eq!(o.end_write(), Err(Refused::InvalidArgument), "nothing is held"); o.write_begin(); o.write_end(false); @@ -765,17 +781,19 @@ mod tests { o.write_begin(); o.write_end(true); assert_eq!(o.read_begin(), None, "a later call, short or whole, ends no held write"); + assert_eq!(o.published(), None, "nor does a stat publish one half written"); let held = o.version; assert_eq!(o.end_write(), Ok(())); let v = o.read_begin().expect("the end lets a read begin"); assert!(o.unchanged(v) && v != Version(held), "the end moved the version"); + assert_eq!(o.published(), Some(v.0), "a stat publishes the version a read begins at"); assert_eq!(o.end_write(), Err(Refused::InvalidArgument), "a write ends once"); } #[test] fn a_supplied_page_is_found_and_a_second_supply_changes_nothing() { let mut p = Pages::new(8, 4).unwrap(); - let mut o = p.object(1, 3 * PAGE); + let mut o = p.object(1, 3 * PAGE, 0); assert_eq!(p.lookup(&o, 1), Lookup::Absent); assert_eq!(p.supply(&mut o, 1, 100, false, 2 * PAGE), Ok(Supplied::Installed)); assert_eq!(p.supply(&mut o, 1, 200, false, 2 * PAGE), Ok(Supplied::Present)); @@ -786,10 +804,10 @@ mod tests { #[test] fn a_clean_supply_past_the_size_is_refused_and_a_dirty_one_extends_it() { let mut p = Pages::new(8, 4).unwrap(); - let mut o = p.object(2, PAGE); + let mut o = p.object(2, PAGE, 0); assert_eq!(p.supply(&mut o, 1, 1, false, 2 * PAGE), Err(Refused::PastEnd)); assert_eq!(o.size(), PAGE); - let mut short = p.object(3, 100); + let mut short = p.object(3, 100, 0); assert_eq!(p.supply(&mut short, 0, 2, false, PAGE), Ok(Supplied::Installed)); assert_eq!(short.size(), 100, "a clean page ending past the size leaves it"); assert_eq!(p.supply(&mut o, 1, 1, true, PAGE + 10), Ok(Supplied::Installed)); @@ -802,7 +820,7 @@ mod tests { #[test] fn a_reader_racing_an_extending_write_never_sees_its_unwritten_bytes() { let mut p = Pages::new(8, 4).unwrap(); - let mut o = p.object(4, 100); + let mut o = p.object(4, 100, 0); p.supply(&mut o, 0, 7, false, 100).unwrap(); assert_eq!(p.write_pin(&mut o, 0), Ok(7)); assert_eq!(o.size(), 100, "the copy is in flight: a reader still stops at the old end"); @@ -814,7 +832,7 @@ mod tests { #[test] fn an_unpin_after_a_shrink_removed_the_page_grows_nothing() { let mut p = Pages::new(8, 4).unwrap(); - let mut o = p.object(5, PAGE); + let mut o = p.object(5, PAGE, 0); p.supply(&mut o, 0, 7, false, PAGE).unwrap(); p.write_pin(&mut o, 0).unwrap(); p.resize(&mut o, 0, &mut none); @@ -829,7 +847,7 @@ mod tests { #[test] fn the_dirty_ceiling_refuses_and_a_write_back_frees_it() { let mut p = Pages::new(8, 2).unwrap(); - let mut o = p.object(6, 4 * PAGE); + let mut o = p.object(6, 4 * PAGE, 0); for page in 0..2 { p.supply(&mut o, page, page, true, (page + 1) * PAGE).unwrap(); } @@ -844,7 +862,7 @@ mod tests { #[test] fn kept_is_exactly_the_pages_dirtied_or_pinned_since_the_begin_or_unwritten() { let mut p = Pages::new(16, 16).unwrap(); - let mut o = p.object(7, 8 * PAGE); + let mut o = p.object(7, 8 * PAGE, 0); for page in 0..6 { p.supply(&mut o, page, page, true, (page + 1) * PAGE).unwrap(); } @@ -866,7 +884,7 @@ mod tests { #[test] fn a_range_must_be_ended_exactly_as_begun_and_once() { let mut p: Pages = Pages::new(8, 8).unwrap(); - let mut o = p.object(8, PAGE * 1024); + let mut o = p.object(8, PAGE * 1024, 0); p.writeback_begin(&mut o, 0, 4).unwrap(); assert_eq!(p.writeback_begin(&mut o, 3, 2), Err(Refused::InvalidArgument), "overlapping"); assert_eq!(p.writeback_end(&mut o, 0, 3, true), Err(Refused::InvalidArgument), "not the range begun"); @@ -879,7 +897,7 @@ mod tests { #[test] fn only_clean_unheld_pages_are_withdrawn() { let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(9, 4 * PAGE); + let mut o = p.object(9, 4 * PAGE, 0); p.supply(&mut o, 0, 10, false, PAGE).unwrap(); p.supply(&mut o, 1, 11, true, 2 * PAGE).unwrap(); p.supply(&mut o, 2, 12, true, 3 * PAGE).unwrap(); @@ -937,7 +955,7 @@ mod tests { #[test] fn a_withdrawal_of_a_long_range_walks_the_objects_pages() { let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(1, LAST_END); + let mut o = p.object(1, LAST_END, 0); p.supply(&mut o, 7, 1, false, 8 * PAGE).unwrap(); p.supply(&mut o, 1 << 40, 2, false, ((1 << 40) + 1) * PAGE).unwrap(); let mut freed = Vec::new(); @@ -959,7 +977,7 @@ mod tests { assert!(!within_reach(u64::MAX - PAGE + 1, PAGE), "the supply that wrapped `ends`"); assert!(!within_reach(1, u64::MAX)); let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(1, LAST_END); + let mut o = p.object(1, LAST_END, 0); let last = LAST_END / PAGE - 1; assert_eq!(p.supply(&mut o, last, 1, true, LAST_END), Ok(Supplied::Installed), "the last page"); assert!(Pages::::new(Pages::::MAX_CAPACITY + 1, 0).is_none()); @@ -968,7 +986,7 @@ mod tests { #[test] fn a_shrink_removes_dirty_pages_releases_their_slots_and_names_the_tail() { let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(11, 3 * PAGE); + let mut o = p.object(11, 3 * PAGE, 0); for page in 0..3 { p.supply(&mut o, page, page + 100, true, (page + 1) * PAGE).unwrap(); } @@ -984,7 +1002,7 @@ mod tests { #[test] fn a_failed_page_answers_failed_until_supplied_again() { let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(12, 2 * PAGE); + let mut o = p.object(12, 2 * PAGE, 0); p.supply(&mut o, 0, 5, false, PAGE).unwrap(); let mut freed = Vec::new(); p.fail(&mut o, 0, 2, &mut sink(&mut freed)).unwrap(); @@ -997,7 +1015,7 @@ mod tests { #[test] fn an_invalidated_object_answers_gone_and_gives_every_frame_back() { let mut p = Pages::new(8, 8).unwrap(); - let mut o = p.object(13, 4 * PAGE); + let mut o = p.object(13, 4 * PAGE, 0); p.supply(&mut o, 0, 1, true, PAGE).unwrap(); p.supply(&mut o, 2, 3, false, 3 * PAGE).unwrap(); let mut freed = Vec::new(); @@ -1012,7 +1030,7 @@ mod tests { #[test] fn a_full_table_refuses_and_every_slot_is_reached_after_churn() { let mut p = Pages::new(64, 64).unwrap(); - let mut objects: Vec = (0..4).map(|i| p.object(i, 1 << 30)).collect(); + let mut objects: Vec = (0..4).map(|i| p.object(i, 1 << 30, 0)).collect(); let mut model: BTreeSet<(usize, u64)> = BTreeSet::new(); let mut x = 1u64; for round in 0..20_000u64 { diff --git a/kernel/src/object/paged.rs b/kernel/src/object/paged.rs index e0c3cfc4f52..ef87f83e408 100644 --- a/kernel/src/object/paged.rs +++ b/kernel/src/object/paged.rs @@ -48,7 +48,7 @@ use core::sync::atomic::{AtomicU32, Ordering}; use kernel::chunked::{Chunked, Stack}; use kernel::paged::{Core, Draw, Leave, Lookup, Object, Pages, Refused, Supplied, Version, PAGE}; use kernel::sched::task::WaitClass; -use toyos_abi::syscall::{PagedNotice, SyscallError, NOTICE_NO_HOLDERS, NOTICE_REQUESTED}; +use toyos_abi::syscall::{PagedNotice, PagedStat, SyscallError, NOTICE_NO_HOLDERS, NOTICE_REQUESTED}; use crate::mm::pmm; use crate::process::Pid; @@ -271,9 +271,9 @@ impl PagerObject { unreported || state.no_holders > 0 } - /// A new object of `size` bytes: its id. `ResourceExhausted` where every - /// slot holds one. - pub fn add(&self, size: u64) -> u64 { + /// A new object of `size` bytes last written at `mtime`: its id. + /// `ResourceExhausted` where every slot holds one. + pub fn add(&self, size: u64, mtime: u64) -> u64 { if size > kernel::paged::LAST_END { return SyscallError::InvalidArgument.to_u64(); } @@ -282,7 +282,7 @@ impl PagerObject { return SyscallError::Gone.to_u64(); } let Some(slot) = state.free.pop() else { return SyscallError::ResourceExhausted.to_u64() }; - let object = state.pages.object(slot, size); + let object = state.pages.object(slot, size, mtime); state.entries[slot as usize] = Some(Entry { object, refs: 1, holders: 0, owned: true, no_holders: false }); u64::from(slot) } @@ -588,17 +588,45 @@ impl PagerObject { Step::Copy(count) } - /// A write call on object `id` begins: its slot, held by a reference until - /// [`Self::write_end`], so no release can hand it to another object. - fn write_begin(&self, id: u64) -> Result { + /// A write call on object `id` begins, stamping it `mtime` where given: + /// its slot, held by a reference until [`Self::write_end`], so no release + /// can hand it to another object. + fn write_begin(&self, id: u64, mtime: Option) -> Result { let mut state = self.state.lock(); let slot = state.live(id)?; let entry = state.entry_mut(slot); entry.refs += 1; entry.object.write_begin(); + if let Some(mtime) = mtime { + entry.object.set_mtime(mtime); + } Ok(slot) } + /// Stamp object `id` last written at `mtime`. + pub fn stamp(&self, id: u64, mtime: u64) -> u64 { + let mut state = self.state.lock(); + match state.live(id) { + Ok(slot) => { + state.entry_mut(slot).object.set_mtime(mtime); + 0 + } + Err(e) => e.to_u64(), + } + } + + /// What the object at `slot` publishes to a holder: `WouldBlock` while a + /// write is under way. + fn stat(&self, slot: u32) -> Result { + let state = self.state.lock(); + let object = &state.entry(slot).object; + if state.gone || object.gone() { + return Err(SyscallError::Gone); + } + let version = object.published().ok_or(SyscallError::WouldBlock)?; + Ok(PagedStat { size: object.size(), mtime: object.mtime(), version }) + } + /// The write call [`Self::write_begin`] began ends, `whole` where it took /// everything it was given: its reference goes. fn write_end(&self, slot: u32, whole: bool) { @@ -627,7 +655,7 @@ impl PagerObject { if !dirty { return self.supply_pages(id, offset, src, false); } - let slot = match self.write_begin(id) { + let slot = match self.write_begin(id, None) { Ok(slot) => slot, Err(e) => return e.to_u64(), }; @@ -728,11 +756,11 @@ impl PagerObject { installed } - /// Copy `src` into the present pages of object `id` from `offset`, dirty: - /// the bytes written, up to the first absent page. The caller checked the - /// span is in reach. - pub fn write(&self, id: u64, offset: u64, src: &UserBytes) -> u64 { - let slot = match self.write_begin(id) { + /// Copy `src` into the present pages of object `id` from `offset`, dirty, + /// stamping it `mtime`: the bytes written, up to the first absent page. + /// The caller checked the span is in reach. + pub fn write(&self, id: u64, offset: u64, src: &UserBytes, mtime: u64) -> u64 { + let slot = match self.write_begin(id, Some(mtime)) { Ok(slot) => slot, Err(e) => return e.to_u64(), }; @@ -819,11 +847,12 @@ impl PagerObject { } } - pub fn resize(&self, id: u64, size: u64) -> u64 { + pub fn resize(&self, id: u64, size: u64, mtime: u64) -> u64 { if size > kernel::paged::LAST_END { return SyscallError::InvalidArgument.to_u64(); } let resized = self.removing(id, |pages, object, out| { + object.set_mtime(mtime); if let Some(phys) = pages.resize(object, size, &mut |f| out(f)) { let within = (size % PAGE) as usize; // SAFETY: the last page's frame, the object's while the lock is @@ -1088,6 +1117,10 @@ impl PagedHolderObject { pub fn read(&self, out: &mut UserBytesMut, offset: u64) -> u64 { self.named.pager.read(self.named.slot, out, offset, true) } + + pub fn stat(&self) -> Result { + self.named.pager.stat(self.named.slot) + } } impl ZeroHandles for PagedHolderObject { diff --git a/kernel/src/syscall/dispatch.rs b/kernel/src/syscall/dispatch.rs index 2d5767926ff..a9e41623438 100644 --- a/kernel/src/syscall/dispatch.rs +++ b/kernel/src/syscall/dispatch.rs @@ -48,7 +48,7 @@ use super::machine::{ }; #[cfg(feature = "test-actuators")] use super::machine::lower_sysinfo_bound; -use super::paged::{sys_paged_create, sys_paged_holder, sys_paged_notices, sys_paged_op, sys_paged_read, sys_pager_create}; +use super::paged::{sys_paged_create, sys_paged_holder, sys_paged_notices, sys_paged_op, sys_paged_read, sys_paged_stat, sys_pager_create}; use super::proc::{ spawn_pager, spawn_place, sys_endowments, sys_exit, sys_nanosleep, sys_process_stats, sys_account_info, sys_job_create, sys_job_floor, sys_job_own, sys_process_quit, sys_process_wait, sys_rt_enter, sys_spawn, sys_thread_exit, sys_thread_get_affinity, sys_thread_join, sys_thread_set_affinity, sys_thread_spawn, sys_wake_latency_hold, @@ -684,13 +684,14 @@ pub(crate) fn syscall_dispatch(num: u64, a1: u64, a2: u64, a3: u64, a4: u64) -> SYS_ACPI => sys_acpi(&ctx, RawHandle(a1 as u32), a2, a3), SYS_DEVICE_REG_READ => sys_device_reg_read(RawHandle(a1 as u32), a2, a3), SYS_PAGER_CREATE => sys_pager_create(a1, a2, a3, a4), - SYS_PAGED_CREATE => sys_paged_create(RawHandle(a1 as u32), a2), + SYS_PAGED_CREATE => sys_paged_create(RawHandle(a1 as u32), a2, a3), SYS_PAGED_HOLDER => sys_paged_holder(RawHandle(a1 as u32), a2), SYS_PAGED_READ => { let Some(mut buf) = ctx.user_bytes_mut(UserAddr::new(a2), a3) else { return bad_addr }; sys_paged_read(RawHandle(a1 as u32), &mut buf, a4) } SYS_PAGED_OP => sys_paged_op(&ctx, RawHandle(a1 as u32), a2, UserAddr::new(a3)), + SYS_PAGED_STAT => sys_paged_stat(&ctx, RawHandle(a1 as u32), UserAddr::new(a2)), SYS_PAGED_NOTICES => { let size = core::mem::size_of::(); let Some(len) = (a3 as usize).checked_mul(size) else { return SyscallError::InvalidArgument.to_u64() }; diff --git a/kernel/src/syscall/paged.rs b/kernel/src/syscall/paged.rs index 0a8aaa0f164..5f3fc5f71cc 100644 --- a/kernel/src/syscall/paged.rs +++ b/kernel/src/syscall/paged.rs @@ -10,7 +10,7 @@ use kernel::paged::within_reach; use toyos_abi::handle::{RawHandle, Rights}; use toyos_abi::syscall::{PagedArgs, PagedOp, SyscallError, PAGED_DIRTY, PAGED_RANGE_PAGES, PAGED_WRITTEN}; -use crate::object::paged::{PagerObject, Unmade}; +use crate::object::paged::{PagedHolderObject, PagerObject, Unmade}; use crate::object::{ops, KObjectRef}; use crate::process::{self, PagerRole}; use crate::user_ptr::{SyscallContext, UserBytesMut}; @@ -48,9 +48,9 @@ fn pager(h: RawHandle, rights: Rights) -> Result, u64> { process::with_process_data(|data| data.handles.get::(h, rights)).map_err(|e| e.refuse()) } -pub(super) fn sys_paged_create(h: RawHandle, size: u64) -> u64 { +pub(super) fn sys_paged_create(h: RawHandle, size: u64, mtime: u64) -> u64 { match pager(h, Rights::WRITE) { - Ok(pager) => pager.add(size), + Ok(pager) => pager.add(size, mtime), Err(refused) => refused, } } @@ -80,6 +80,28 @@ pub(super) fn sys_paged_holder(h: RawHandle, object: u64) -> u64 { /// on itself or on another pager. The handle is often a peer's, sent as a /// holder, so another kind is refused `InvalidArgument`, never a fault. pub(super) fn sys_paged_read(h: RawHandle, out: &mut UserBytesMut, offset: u64) -> u64 { + match holder(h) { + Ok(holder) => holder.read(out, offset), + Err(refused) => refused, + } +} + +/// A holder's stat, refused as its read is. +pub(super) fn sys_paged_stat(ctx: &SyscallContext<'_>, h: RawHandle, out: UserAddr) -> u64 { + let stat = match holder(h) { + Ok(holder) => holder.stat(), + Err(refused) => return refused, + }; + match stat { + Ok(stat) => match ctx.copy_out(out, &stat) { + Ok(()) => 0, + Err(_) => SyscallError::BadAddress.to_u64(), + }, + Err(e) => e.to_u64(), + } +} + +fn holder(h: RawHandle) -> Result, u64> { let resolved = process::with_process_data(|data| { let KObjectRef::Paged(holder) = data.handles.get_ref(h, Rights::NONE)? else { return Ok(Err(SyscallError::InvalidArgument)); @@ -91,9 +113,9 @@ pub(super) fn sys_paged_read(h: RawHandle, out: &mut UserBytesMut, offset: u64) Ok(if data.pager.is_pager() { Err(SyscallError::PermissionDenied) } else { Ok(holder) }) }); match resolved { - Ok(Ok(holder)) => holder.read(out, offset), - Ok(Err(e)) => e.to_u64(), - Err(e) => crate::object::HandleError::refuse(e), + Ok(Ok(holder)) => Ok(holder), + Ok(Err(e)) => Err(e.to_u64()), + Err(e) => Err(crate::object::HandleError::refuse(e)), } } @@ -125,12 +147,13 @@ pub(super) fn sys_paged_op(ctx: &SyscallContext<'_>, h: RawHandle, op: u64, args let Some(src) = ctx.user_bytes(UserAddr::new(args.buf), args.len) else { return SyscallError::BadAddress.to_u64() }; match x == PagedOp::Supply as u64 { true => pager.supply(id, args.offset, &src, args.flags & PAGED_DIRTY != 0), - false => pager.write(id, args.offset, &src), + false => pager.write(id, args.offset, &src, args.time), } } x if x == PagedOp::Withdraw as u64 => pager.withdraw(id, args.offset, args.len), x if x == PagedOp::Fail as u64 => pager.fail(id, args.offset, args.len), - x if x == PagedOp::Resize as u64 => pager.resize(id, args.offset), + x if x == PagedOp::Resize as u64 => pager.resize(id, args.offset, args.time), + x if x == PagedOp::Stamp as u64 => pager.stamp(id, args.time), x if x == PagedOp::Release as u64 => pager.release(id), x if x == PagedOp::EndWrite as u64 => pager.end_write(id), x if x == PagedOp::WritebackBegin as u64 || x == PagedOp::WritebackEnd as u64 => { diff --git a/sdk/abi/src/syscall.rs b/sdk/abi/src/syscall.rs index dd0911e4bb2..2378ac33d01 100644 --- a/sdk/abi/src/syscall.rs +++ b/sdk/abi/src/syscall.rs @@ -370,6 +370,8 @@ pub const SYS_THREAD_SET_AFFINITY: u64 = 161; pub const SYS_THREAD_GET_AFFINITY: u64 = 162; /// The CPU the caller is running on. See [`current_cpu`]. pub const SYS_CURRENT_CPU: u64 = 163; +/// What a holder's paged object publishes. See [`paged_stat`]. +pub const SYS_PAGED_STAT: u64 = 164; /// Bins in the per-process syscall profile — one for every number this ABI /// issues, and one at the end for every number it does not. @@ -377,13 +379,13 @@ pub const SYS_CURRENT_CPU: u64 = 163; /// **The profile's parts sum to its total, and that is the whole requirement.** /// A bin array narrower than the ABI reaches drops calls out of the line while /// the total goes on counting them. -pub const SYSCALL_PROFILE_BINS: usize = 165; +pub const SYSCALL_PROFILE_BINS: usize = 166; /// Where a number this ABI does not issue is counted. Merging is a degradation /// a reader can see in the line; dropping is one nobody can. pub const SYSCALL_PROFILE_OTHER: usize = SYSCALL_PROFILE_BINS - 1; -const _: () = assert!(SYS_CURRENT_CPU < SYSCALL_PROFILE_OTHER as u64); +const _: () = assert!(SYS_PAGED_STAT < SYSCALL_PROFILE_OTHER as u64); pub const WNOHANG: u64 = 1; /// Every bit `SYS_PROCESS_WAIT`'s flag word defines; a wait carrying another @@ -3320,8 +3322,10 @@ pub enum PagedOp { /// Copy `len` bytes from `buf` into the present pages at `offset`, dirty, /// growing the size to their end as each page's bytes land. Answers the /// bytes written, stopping at the first absent page; `NotFound` when the - /// first is absent, `WouldBlock` at the dirty ceiling. A write that does - /// not take all it was given stays under way until [`PagedOp::EndWrite`]. + /// first is absent, `WouldBlock` at the dirty ceiling. `time` is the + /// file's mtime from this write, as its volume keeps it. A write that + /// does not take all it was given stays under way until + /// [`PagedOp::EndWrite`]. Write = 1, /// Remove the clean pages of `len` pages from page `offset` that no /// write-back or write holds. Answers how many went. @@ -3329,9 +3333,9 @@ pub enum PagedOp { /// Mark `len` pages from page `offset` failed: each read of one answers /// `Io` until it is supplied again. Fail = 3, - /// Set the size to `offset`. A shrink drops every page past it, dirty or - /// not, and zeroes the last page's tail; the new range of a growth reads - /// as the pager supplies it. + /// Set the size to `offset`, and the mtime to `time`. A shrink drops + /// every page past it, dirty or not, and zeroes the last page's tail; the + /// new range of a growth reads as the pager supplies it. Resize = 4, /// Begin writing back `len` pages from page `offset`, at most /// [`PAGED_RANGE_PAGES`]: each dirty page is cleaned and held until the @@ -3350,6 +3354,9 @@ pub enum PagedOp { /// End the write a short [`PagedOp::Write`] or dirty supply left under /// way; `InvalidArgument` where none is. EndWrite = 9, + /// Set the mtime to `time`, as a file's `utimens` does; the bytes and + /// the version stay. + Stamp = 10, } /// [`PagedOp::Supply`]'s pages are dirty. @@ -3370,6 +3377,21 @@ crate::user_safe! { pub len: u64, pub buf: u64, pub flags: u64, + /// [`PagedOp::Write`]'s, [`PagedOp::Resize`]'s and [`PagedOp::Stamp`]'s + /// mtime, nanoseconds since the Unix epoch. + pub time: u64, + } +} + +crate::user_safe! { + /// What a paged object publishes to its holders ([`paged_stat`]): the + /// size and version the kernel keeps, and the mtime its pager last set. + #[derive(Clone, Copy, Default, Debug, PartialEq, Eq)] + pub struct PagedStat { + pub size: u64, + pub mtime: u64, + /// Moved by every write and resize: the object's data generation. + pub version: u64, } } @@ -3404,11 +3426,12 @@ pub fn pager_create(capacity: u64, dirty: u64, objects: u64, report_ns: u64) -> check(syscall(SYS_PAGER_CREATE, capacity, dirty, objects, report_ns)).map(|raw| RawHandle(raw as u32)) } -/// A paged object of `size` bytes, its pages absent: its id, which `pager`'s -/// calls and notices name it by. `ResourceExhausted` where the pager serves -/// as many objects as it was made for. -pub fn paged_create(pager: RawHandle, size: u64) -> Result { - check(syscall(SYS_PAGED_CREATE, pager.0 as u64, size, 0, 0)) +/// A paged object of `size` bytes last written at `mtime`, its pages absent: +/// its id, which `pager`'s calls and notices name it by. +/// `ResourceExhausted` where the pager serves as many objects as it was made +/// for. +pub fn paged_create(pager: RawHandle, size: u64, mtime: u64) -> Result { + check(syscall(SYS_PAGED_CREATE, pager.0 as u64, size, mtime, 0)) } /// A holder handle on `pager`'s object `object`, carrying `READ`, which may @@ -3431,6 +3454,14 @@ pub fn paged_read(holder: RawHandle, out: &mut [u8], offset: u64) -> Result Result { + let mut out = PagedStat::default(); + check(syscall(SYS_PAGED_STAT, holder.0 as u64, &mut out as *mut PagedStat as u64, 0, 0)).map(|_| out) +} + /// One [`PagedOp`] on one of `pager`'s objects. pub fn paged_op(pager: RawHandle, op: PagedOp, args: &PagedArgs) -> Result { check(syscall(SYS_PAGED_OP, pager.0 as u64, op as u64, args as *const PagedArgs as u64, 0)) diff --git a/sdk/toyos/src/files/owner.rs b/sdk/toyos/src/files/owner.rs index 123eb963a5e..980cf660891 100644 --- a/sdk/toyos/src/files/owner.rs +++ b/sdk/toyos/src/files/owner.rs @@ -34,7 +34,7 @@ enum Place { enum Held { /// A file or directory a file server holds open, and the holder of its /// paged object where the server sent one. - Served { cap: Arc, fid: Fid, position: Position, holder: Option }, + Served { cap: Arc, fid: Fid, position: Position, holder: Option }, /// A directory the kernel serves, by its absolute path. KernelDir(String), } @@ -47,6 +47,22 @@ impl Drop for Held { } } +/// A served file's paged object, and what its open answered of it: an +/// `fstat` is the object's size and mtime over the rest of that, which no +/// change to the file moves. +struct Holder { + paged: crate::paged::Paged, + opened: fs::Stat, +} + +impl Holder { + /// `None` while a write is under way, and the server is asked. + fn stat(&self) -> Option { + let published = self.paged.stat().ok()?; + Some(fs::Stat { size: published.size, mtime: published.mtime, ..self.opened }) + } +} + /// A served file's offset: kept here while this process is the /// description's only holder, so a seek costs no request and a read or a /// write goes at it as a positioned one; the server's from the moment the @@ -302,7 +318,7 @@ fn on_fd_held( match named.map_err(Error::Fd)?? { Err(answer) => answer, Ok(held) => match &*held { - Held::Served { cap, fid, position, holder } => match holder.as_ref().and_then(|h| holding(h, position)) { + Held::Served { cap, fid, position, holder } => match holder.as_ref().and_then(|h| holding(&h.paged, position)) { Some(answer) => Ok(answer), None => served(&mut cap.dir.lock(), *fid, position).map_err(Error::Served), }, @@ -445,7 +461,7 @@ unsafe extern "C" fn toyos_fs_open(at: Fd, path: *const u8, len: usize, how: Ope // it; a pager's process reads through the server. let holder = match opened.holder.map(crate::paged::Paged::received) { None | Some(Err(crate::paged::NotAHolder::Refused | crate::paged::NotAHolder::Ended)) => None, - Some(Ok(holder)) => Some(holder), + Some(Ok(paged)) => Some(Holder { paged, opened: opened.stat }), Some(Err(crate::paged::NotAHolder::Other)) => { dir.close(opened.fid); return Err(Refused::Error(fs::Error::Malformed)); @@ -476,7 +492,10 @@ unsafe extern "C" fn toyos_fs_stat(at: Fd, path: *const u8, len: usize, follow: (At::Fd(fd), true) => match named(fd) { Ok(Named::Handle(handle)) => syscall::fstat(handle).map(Stat::of_kernel).map_err(Error::Kernel), Ok(Named::Held(held)) => match &*held { - Held::Served { cap, fid, .. } => cap.dir.lock().fstat(*fid).map(stat_of).map_err(Error::Served), + Held::Served { cap, fid, holder, .. } => match holder.as_ref().and_then(Holder::stat) { + Some(stat) => Ok(stat_of(stat)), + None => cap.dir.lock().fstat(*fid).map(stat_of).map_err(Error::Served), + }, Held::KernelDir(dir) => syscall::stat(dir.as_bytes(), true).map(Stat::of_kernel).map_err(Error::Kernel), }, Err(e) => Err(e), diff --git a/sdk/toyos/src/paged.rs b/sdk/toyos/src/paged.rs index 1f5bef1e30e..83b31156e38 100644 --- a/sdk/toyos/src/paged.rs +++ b/sdk/toyos/src/paged.rs @@ -12,7 +12,7 @@ use core::time::Duration; use toyos_abi::syscall::{self, PagedArgs, PagedOp, SyscallError, NOTICE_NO_HOLDERS, NOTICE_REQUESTED, PAGED_DIRTY, PAGED_WRITTEN}; -pub use toyos_abi::syscall::{PagedNotice, PAGED_RANGE_PAGES}; +pub use toyos_abi::syscall::{PagedNotice, PagedStat, PAGED_RANGE_PAGES}; use crate::shm::Bytes; use crate::{AsHandle, BorrowedHandle, OwnedHandle}; @@ -81,9 +81,10 @@ impl Pager { })) } - /// An object of `size` bytes, every page absent: its id. - pub fn object(&self, size: u64) -> Result { - syscall::paged_create(self.0.raw(), size).map_err(|e| Refused::of(e, None)) + /// An object of `size` bytes last written at `mtime`, every page absent: + /// its id. + pub fn object(&self, size: u64, mtime: u64) -> Result { + syscall::paged_create(self.0.raw(), size, mtime).map_err(|e| Refused::of(e, None)) } /// A holder handle on object `id`, to send. @@ -101,7 +102,7 @@ impl Pager { /// size or the first absent page after the first; never waits. `out` may /// be a window another process shares. pub fn read(&self, id: u64, out: Bytes<'_>, offset: u64) -> Result { - let args = PagedArgs { object: id, offset, len: out.len() as u64, buf: out.as_mut_ptr() as u64, flags: 0 }; + let args = PagedArgs { object: id, offset, len: out.len() as u64, buf: out.as_mut_ptr() as u64, ..PagedArgs::default() }; self.op(PagedOp::Read, args, Some(SyscallError::WouldBlock)).map(|n| n as usize) } @@ -111,15 +112,15 @@ impl Pager { /// kept. The pages installed. pub fn supply(&self, id: u64, offset: u64, bytes: &[u8], dirty: bool) -> Result { let flags = if dirty { PAGED_DIRTY } else { 0 }; - let args = PagedArgs { object: id, offset, len: bytes.len() as u64, buf: bytes.as_ptr() as u64, flags }; + let args = PagedArgs { object: id, offset, len: bytes.len() as u64, buf: bytes.as_ptr() as u64, flags, time: 0 }; self.op(PagedOp::Supply, args, None) } /// `bytes` into the present pages of object `id` from `offset`, dirty, - /// the size growing as they land: how many were written before the first - /// absent page. - pub fn write(&self, id: u64, offset: u64, bytes: &[u8]) -> Result { - let args = PagedArgs { object: id, offset, len: bytes.len() as u64, buf: bytes.as_ptr() as u64, flags: 0 }; + /// the size growing as they land and the object stamped `mtime`: how + /// many were written before the first absent page. + pub fn write(&self, id: u64, offset: u64, bytes: &[u8], mtime: u64) -> Result { + let args = PagedArgs { object: id, offset, len: bytes.len() as u64, buf: bytes.as_ptr() as u64, flags: 0, time: mtime }; self.op(PagedOp::Write, args, Some(SyscallError::NotFound)).map(|n| n as usize) } @@ -141,16 +142,21 @@ impl Pager { self.op(PagedOp::Fail, PagedArgs { object: id, offset: first, len: count, ..PagedArgs::default() }, None).map(drop) } - /// The size, as `ftruncate` sets it: a shrink drops every page past it, - /// dirty or not. - pub fn resize(&self, id: u64, size: u64) -> Result<(), Refused> { - self.op(PagedOp::Resize, PagedArgs { object: id, offset: size, ..PagedArgs::default() }, None).map(drop) + /// The size and mtime, as `ftruncate` sets them: a shrink drops every + /// page past it, dirty or not. + pub fn resize(&self, id: u64, size: u64, mtime: u64) -> Result<(), Refused> { + self.op(PagedOp::Resize, PagedArgs { object: id, offset: size, time: mtime, ..PagedArgs::default() }, None).map(drop) + } + + /// The mtime, as `utimens` sets it. + pub fn stamp(&self, id: u64, mtime: u64) -> Result<(), Refused> { + self.op(PagedOp::Stamp, PagedArgs { object: id, time: mtime, ..PagedArgs::default() }, None).map(drop) } /// Begin writing back `count` pages from `first`: a bit per page begun. pub fn writeback_begin(&self, id: u64, first: u64, count: u64) -> Result<[u64; 8], Refused> { let mut bits = [0u64; 8]; - let args = PagedArgs { object: id, offset: first, len: count, buf: bits.as_mut_ptr() as u64, flags: 0 }; + let args = PagedArgs { object: id, offset: first, len: count, buf: bits.as_mut_ptr() as u64, ..PagedArgs::default() }; self.op(PagedOp::WritebackBegin, args, None)?; Ok(bits) } @@ -160,7 +166,7 @@ impl Pager { pub fn writeback_end(&self, id: u64, first: u64, count: u64, written: bool) -> Result<[u64; 8], Refused> { let mut bits = [0u64; 8]; let flags = if written { PAGED_WRITTEN } else { 0 }; - let args = PagedArgs { object: id, offset: first, len: count, buf: bits.as_mut_ptr() as u64, flags }; + let args = PagedArgs { object: id, offset: first, len: count, buf: bits.as_mut_ptr() as u64, flags, time: 0 }; self.op(PagedOp::WritebackEnd, args, None)?; Ok(bits) } @@ -212,4 +218,10 @@ impl Paged { pub fn read(&self, out: &mut [u8], offset: u64) -> Result { syscall::paged_read(self.0.raw(), out, offset) } + + /// The size, mtime and version its object publishes; `WouldBlock` while + /// a write of the pager's is under way, and the pager is asked. + pub fn stat(&self) -> Result { + syscall::paged_stat(self.0.raw()) + } } diff --git a/system/fileserver/src/fat.rs b/system/fileserver/src/fat.rs index ec75dbf9f12..9bd1a71cea2 100644 --- a/system/fileserver/src/fat.rs +++ b/system/fileserver/src/fat.rs @@ -4,8 +4,8 @@ //! **A growing write is two writes: the chain, then the directory entry.** //! Between them the volume holds clusters its entry does not reach, which //! fatgen103 refuses, so an open file changed since it was last level is -//! brought level — its entry written and stamped, its chain reconciled — at -//! its last close, at an fsync and at a sync. **The two are a cache flush +//! brought level — its entry written and stamped with its last write, its +//! chain reconciled — at its last close, at an fsync and at a sync. **The two are a cache flush //! apart**: the cache writes lowest block first and the disk keeps no order //! until a flush, so an entry put into the cache beside its chain and data //! would reach the disk before the clusters it names. @@ -41,6 +41,9 @@ struct Open { /// Written, resized or opened truncating since its entry was last /// brought level. dirty: bool, + /// When it was last written or resized, which its level stamps: its + /// mtime meanwhile. + written: Option, } /// The disk's `ReadOnly` reaches `toyos-fat32` as a refused write, which it @@ -122,6 +125,12 @@ impl FatVolume { FatTime::from_unix_secs((self.clock)() / NANOS_PER_SEC) } + /// Now as an entry's write time keeps it: the even second at or before. + fn write_time(&self) -> FatTime { + let secs = (self.clock)() / NANOS_PER_SEC; + FatTime::from_unix_secs(secs - secs % 2) + } + fn entry(&mut self, node: Node) -> Result<&mut Open, fs::Error> { let open = self.open.get_mut(&node).ok_or(fs::Error::Stale)?; if open.gone { @@ -130,15 +139,17 @@ impl FatVolume { Ok(open) } - /// Bring one open file's entry level with its chain, stamped now, when - /// it changed since the entry was last written. + /// Bring one open file's entry level with its chain, stamped with its + /// last write (or now, for a chain alone), when it changed since the + /// entry was last written. fn level(&mut self, node: Node) -> Result<(), fs::Error> { match self.open.get(&node) { Some(open) if !open.gone && (open.dirty || open.file.needs_reconcile()) => {} _ => return Ok(()), } - let time = self.time(); + let now = self.time(); let open = self.open.get_mut(&node).expect("just found"); + let time = open.written.unwrap_or(now); // The chain and the data on the disk before the entry that reaches // them is in the cache. if open.file.needs_reconcile() { @@ -149,6 +160,7 @@ impl FatVolume { self.cache.block_on(self.fs.reconcile(&mut open.file, time)).map_err(|e| logged("a reconcile", &open.path, e))?; } open.dirty = false; + open.written = None; Ok(()) } @@ -185,10 +197,10 @@ impl Volume for FatVolume { if path.is_empty() { return Ok(Meta { kind: Kind::Dir, size: 0, mtime: 0 }); } - if let Some(open) = self.by_path.get(path).and_then(|n| self.open.get(n)) { - let len = open.file.len(); - let mtime = self.cache.block_on(self.fs.metadata(path)).map_err(|e| logged("metadata", path, e))?.modified_unix; - return Ok(Meta { kind: Kind::File, size: len, mtime: mtime * NANOS_PER_SEC }); + if let Some(&node) = self.by_path.get(path) { + if self.open.contains_key(&node) { + return self.node_meta(node); + } } let meta = self.cache.block_on(self.fs.metadata(path)).map_err(|e| match e { Error::NotADirectory => fs::Error::NotFound, @@ -245,7 +257,7 @@ impl Volume for FatVolume { }; let node = self.next; self.next += 1; - self.open.insert(node, Open { path: path.to_string(), file, holders: 0, gone: false, dirty: false }); + self.open.insert(node, Open { path: path.to_string(), file, holders: 0, gone: false, dirty: false, written: None }); self.by_path.insert(path.to_string(), node); node } @@ -253,7 +265,10 @@ impl Volume for FatVolume { self.open.get_mut(&node).expect("just found or made").holders += 1; if how.truncate { // POSIX's `open` stamps a truncated file even when it was empty. - self.open.get_mut(&node).expect("just found or made").dirty = true; + let now = self.write_time(); + let open = self.open.get_mut(&node).expect("just found or made"); + open.dirty = true; + open.written = Some(now); if let Err(e) = self.truncate(node, 0) { // A refused close is said, and its node kept for the next sync. let _ = self.close(node); @@ -281,6 +296,9 @@ impl Volume for FatVolume { fn node_meta(&mut self, node: Node) -> Result { let open = self.entry(node)?; + if let Some(written) = open.written { + return Ok(Meta { kind: Kind::File, size: open.file.len(), mtime: written.to_unix_secs() * NANOS_PER_SEC }); + } let (path, size) = (open.path.clone(), open.file.len()); let mtime = self.cache.block_on(self.fs.metadata(&path)).map_err(|e| logged("metadata", &path, e))?.modified_unix; Ok(Meta { kind: Kind::File, size, mtime: mtime * NANOS_PER_SEC }) @@ -306,11 +324,13 @@ impl Volume for FatVolume { } fn reserve(&mut self, node: Node, offset: u64, len: u64) -> Result { + let now = self.write_time(); let open = self.open.get_mut(&node).ok_or(fs::Error::Stale)?; if open.gone { return Err(fs::Error::Stale); } open.dirty = true; + open.written = Some(now); // A chain has no holes: it holds a block for every page of its size. let reserved = Reserved { size: open.file.len(), held: open.file.len().div_ceil(toyos_blockdev::BLOCK as u64) }; self.cache.block_on(self.fs.reserve(&mut open.file, offset, len)).map_err(|e| logged("reserve", &open.path, e))?; @@ -327,6 +347,7 @@ impl Volume for FatVolume { } fn truncate(&mut self, node: Node, size: u64) -> Result<(), fs::Error> { + let now = self.write_time(); let open = self.open.get_mut(&node).ok_or(fs::Error::Stale)?; if open.gone { return Err(fs::Error::Stale); @@ -335,6 +356,7 @@ impl Volume for FatVolume { return Ok(()); } open.dirty = true; + open.written = Some(now); self.cache.block_on(self.fs.set_len(&mut open.file, size)).map_err(|e| logged("set_len", &open.path, e)) } @@ -555,26 +577,32 @@ mod tests { (v, refused, spec) } - /// What [`the_flush_stamps_its_even_second_and_not_the_write_s`]'s volume - /// reads as the time, which only that test sets. + /// What [`a_files_mtime_is_its_last_write_s_through_its_handle_and_after_a_reopen`]'s + /// volume reads as the time, which only that test sets. static NOW: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); - /// FAT stamps a file's entry with the flush that levels it, read back off - /// the disk: the whole second of that flush, kept to the even one at or - /// before it (`FatTime`'s two-second resolution), and never the write's. + /// A FAT file's mtime is its last write's, kept to the even second at or + /// before it (`FatTime`'s two-second resolution): through its handle + /// before its entry is level, and read back off the disk after a sync + /// later and a reopen, alike. #[test] - fn the_flush_stamps_its_even_second_and_not_the_write_s() { + fn a_files_mtime_is_its_last_write_s_through_its_handle_and_after_a_reopen() { use std::sync::atomic::Ordering::Relaxed; const CREATE: OpenHow = OpenHow { create: true, create_new: false, truncate: false }; let (mut v, _, _) = spec_fat_on(|| NOW.load(Relaxed)); - NOW.store(1_717_245_296 * NANOS_PER_SEC, Relaxed); + NOW.store(1_717_245_101 * NANOS_PER_SEC, Relaxed); let a = v.open("a.txt", CREATE).unwrap(); + NOW.store(1_717_245_297 * NANOS_PER_SEC, Relaxed); v.write(a, 0, &[1; 3000]).unwrap(); + let written = 1_717_245_296 * NANOS_PER_SEC; + assert_eq!(v.node_meta(a).unwrap().mtime, written, "through the handle"); NOW.store(1_717_245_301 * NANOS_PER_SEC + 500_000_000, Relaxed); assert_eq!(v.sync(), Ok(Vec::new())); v.close(a).unwrap(); evict(&v); - assert_eq!(v.lstat("a.txt").unwrap().mtime, 1_717_245_300 * NANOS_PER_SEC); + assert_eq!(v.lstat("a.txt").unwrap().mtime, written, "off the disk, a sync later"); + let again = v.open("a.txt", CREATE).unwrap(); + assert_eq!(v.node_meta(again).unwrap().mtime, written, "through a handle after a reopen"); } /// A write is owed until a commit, and a commit naming another file diff --git a/system/fileserver/src/main.rs b/system/fileserver/src/main.rs index e4d46290ecc..fbbf489718d 100644 --- a/system/fileserver/src/main.rs +++ b/system/fileserver/src/main.rs @@ -1134,7 +1134,7 @@ impl Server { // The volume truncated first, so a refused open changed // nothing; the object follows before anything writes back. if how.truncate && found.is_some() { - let truncated = self.volume.identity(&path).and_then(|identity| self.files.truncated(identity)); + let truncated = self.volume.identity(&path).and_then(|identity| self.files.truncated(&mut *self.volume, identity, node)); if let Err(e) = truncated { let _ = self.volume.close(node); return Err(e); @@ -1243,8 +1243,13 @@ impl Server { Time::At(nanos) => nanos, }; if held { - let node = self.fid(id, r.fid)?.borrow().node().map_err(|_| Error::Unsupported)?; + let file = self.fid(id, r.fid)?; + let (node, identity) = { + let f = file.borrow(); + (f.node().map_err(|_| Error::Unsupported)?, f.identity) + }; self.volume.set_mtime(node, mtime)?; + self.files.stamped(&mut *self.volume, identity, node)?; } else { let path = match self.resolve(id, r.fid, &rel, r.flags & FOLLOW_LAST != 0)? { Resolved::Absolute(p) => return self.follow(id, r, &p), @@ -1253,8 +1258,9 @@ impl Server { if self.volume.lstat(&path)?.kind != Kind::File { return Err(Error::Unsupported); } + let identity = self.volume.identity(&path)?; let node = self.volume.open(&path, OpenHow { create: false, create_new: false, truncate: false })?; - let set = self.volume.set_mtime(node, mtime); + let set = self.volume.set_mtime(node, mtime).and_then(|()| self.files.stamped(&mut *self.volume, identity, node)); let closed = self.volume.close(node); set.and(closed)?; } diff --git a/system/fileserver/src/pages.rs b/system/fileserver/src/pages.rs index 490439887f8..70f14537bbf 100644 --- a/system/fileserver/src/pages.rs +++ b/system/fileserver/src/pages.rs @@ -15,7 +15,9 @@ //! a call that stops short leaves it under way until its end, so no holder //! reads the file half written (`toyos::paged::Pager::end_write`). //! -//! **The volume's size is the object's.** A write reserves on the volume +//! **The volume's size and mtime are the object's**, which its holders +//! `fstat` without a request: each call that moves the volume's mtime hands +//! the object the volume's own value, at its resolution. A write reserves on the volume //! first, so the volume's size runs past the object's only for the bytes the //! write then puts in before it returns; nothing reads the volume for them //! meanwhile, and a write refused part way is cut back to what it put. @@ -36,22 +38,23 @@ const RANGE: u64 = PAGED_RANGE_PAGES; /// A pager's calls on its objects, by id, as `toyos::paged::Pager` makes them. pub trait Pager { - fn object(&self, size: u64) -> Result; + fn object(&self, size: u64, mtime: u64) -> Result; fn read(&self, id: u64, out: Bytes<'_>, offset: u64) -> Result; fn supply(&self, id: u64, offset: u64, bytes: &[u8], dirty: bool) -> Result; - fn write(&self, id: u64, offset: u64, bytes: &[u8]) -> Result; + fn write(&self, id: u64, offset: u64, bytes: &[u8], mtime: u64) -> Result; fn end_write(&self, id: u64) -> Result<(), Refused>; fn withdraw(&self, id: u64, first: u64, count: u64) -> Result; fn fail(&self, id: u64, first: u64, count: u64) -> Result<(), Refused>; - fn resize(&self, id: u64, size: u64) -> Result<(), Refused>; + fn resize(&self, id: u64, size: u64, mtime: u64) -> Result<(), Refused>; + fn stamp(&self, id: u64, mtime: u64) -> Result<(), Refused>; fn writeback_begin(&self, id: u64, first: u64, count: u64) -> Result<[u64; 8], Refused>; fn writeback_end(&self, id: u64, first: u64, count: u64, written: bool) -> Result<[u64; 8], Refused>; fn release(&self, id: u64) -> Result<(), Refused>; } impl Pager for toyos::paged::Pager { - fn object(&self, size: u64) -> Result { - toyos::paged::Pager::object(self, size) + fn object(&self, size: u64, mtime: u64) -> Result { + toyos::paged::Pager::object(self, size, mtime) } fn read(&self, id: u64, out: Bytes<'_>, offset: u64) -> Result { toyos::paged::Pager::read(self, id, out, offset) @@ -59,8 +62,8 @@ impl Pager for toyos::paged::Pager { fn supply(&self, id: u64, offset: u64, bytes: &[u8], dirty: bool) -> Result { toyos::paged::Pager::supply(self, id, offset, bytes, dirty) } - fn write(&self, id: u64, offset: u64, bytes: &[u8]) -> Result { - toyos::paged::Pager::write(self, id, offset, bytes) + fn write(&self, id: u64, offset: u64, bytes: &[u8], mtime: u64) -> Result { + toyos::paged::Pager::write(self, id, offset, bytes, mtime) } fn end_write(&self, id: u64) -> Result<(), Refused> { toyos::paged::Pager::end_write(self, id) @@ -71,8 +74,11 @@ impl Pager for toyos::paged::Pager { fn fail(&self, id: u64, first: u64, count: u64) -> Result<(), Refused> { toyos::paged::Pager::fail(self, id, first, count) } - fn resize(&self, id: u64, size: u64) -> Result<(), Refused> { - toyos::paged::Pager::resize(self, id, size) + fn resize(&self, id: u64, size: u64, mtime: u64) -> Result<(), Refused> { + toyos::paged::Pager::resize(self, id, size, mtime) + } + fn stamp(&self, id: u64, mtime: u64) -> Result<(), Refused> { + toyos::paged::Pager::stamp(self, id, mtime) } fn writeback_begin(&self, id: u64, first: u64, count: u64) -> Result<[u64; 8], Refused> { toyos::paged::Pager::writeback_begin(self, id, first, count) @@ -169,13 +175,14 @@ impl Files

{ Some(f) } - /// After an open of `identity` that truncated: its object holds no page - /// of before. The volume went first, so a refused open changed nothing, - /// and nothing writes back between the two. - pub fn truncated(&mut self, identity: u64) -> Result<(), Error> { + /// After an open of `identity` at `node` that truncated: its object holds + /// no page of before. The volume went first, so a refused open changed + /// nothing, and nothing writes back between the two. + pub fn truncated(&mut self, volume: &mut dyn Volume, identity: u64, node: Node) -> Result<(), Error> { let Some(f) = self.files.get(&identity) else { return Ok(()) }; let id = f.object; - self.pg().resize(id, 0).map_err(word)?; + let mtime = volume.node_meta(node)?.mtime; + self.pg().resize(id, 0, mtime).map_err(word)?; self.files.get_mut(&identity).expect("found").dirty = None; Ok(()) } @@ -196,11 +203,11 @@ impl Files

{ Some(_) => self.forget(volume, identity), None => {} } - let size = volume.node_meta(node)?.size; + let meta = volume.node_meta(node)?; // Objects are bounded by count as pages are: a file nobody holds goes // to make room for another's. let object = loop { - match self.pg().object(size) { + match self.pg().object(meta.size, meta.mtime) { Ok(object) => break object, Err(Refused::NoRoom) if self.drop_oldest(volume, identity) => {} Err(why) => return Err(word(why)), @@ -376,15 +383,30 @@ impl Files

{ } let reserved = volume.reserve(node, offset, data.len() as u64)?; let old = reserved.size; + let mtime = match volume.node_meta(node) { + Ok(meta) => meta.mtime, + Err(e) => { + if let Err(again) = volume.unreserve(node, reserved, 0) { + println!("fileserver: a write of file {identity} was not cut back to {old} ({again:?}), after {e:?}"); + } + return Err(e); + } + }; self.touch(identity); let id = self.files[&identity].object; let mut put = Put { done: 0, held: false }; - let written = self.put(volume, &Writing { identity, node, old, contents: reserved.contents(), offset, data }, &mut put); + let writing = Writing { identity, node, old, contents: reserved.contents(), offset, data, mtime }; + let written = self.put(volume, &writing, &mut put); if let Err(e) = &written { // What was reserved and not put would read as the blocks' old bytes. let landed = if put.done > 0 { offset + put.done as u64 } else { 0 }; let cut = old.max(landed); - let resized = self.pg().resize(id, cut).map_err(word).and(volume.unreserve(node, reserved, landed)); + let resized = self + .pg() + .resize(id, cut, mtime) + .map_err(word) + .and(volume.unreserve(node, reserved, landed)) + .and_then(|()| self.stamped(volume, identity, node)); if let Err(again) = resized { println!("fileserver: a refused write of file {identity} was not cut back to {cut} ({again:?}), after {e:?}"); } @@ -402,13 +424,13 @@ impl Files

{ /// counting into `put` what landed and whether a call left the write /// under way. fn put(&mut self, volume: &mut dyn Volume, w: &Writing<'_>, put: &mut Put) -> Result<(), Error> { - let Writing { identity, node, old, contents, offset, data } = *w; + let Writing { identity, node, old, contents, offset, data, mtime } = *w; let end = offset + data.len() as u64; let done = &mut put.done; while *done < data.len() { let at = offset + *done as u64; let id = self.files.get(&identity).ok_or(Error::Stale)?.object; - let wrote = self.pg().write(id, at, &data[*done..]); + let wrote = self.pg().write(id, at, &data[*done..], mtime); // A first call that takes everything is the whole write; any later // call is under the write a short first one left under way. put.held |= wrote != Ok(data.len() - *done); @@ -468,18 +490,31 @@ impl Files

{ } } + /// `identity`'s volume moved its mtime, which `node` reads: its object + /// follows. + pub fn stamped(&mut self, volume: &mut dyn Volume, identity: u64, node: Node) -> Result<(), Error> { + let Some(f) = self.files.get(&identity) else { return Ok(()) }; + let id = f.object; + let mtime = volume.node_meta(node)?.mtime; + self.pg().stamp(id, mtime).map_err(word) + } + /// `node`'s file cut or grown to `size`: the volume first, so a refusal /// changes nothing, then the object, before anything can write a page /// past the size back. pub fn truncate(&mut self, volume: &mut dyn Volume, identity: u64, node: Node, size: u64) -> Result<(), Error> { volume.truncate(node, size)?; - let Some(f) = self.files.get_mut(&identity) else { return Ok(()) }; + if !self.files.contains_key(&identity) { + return Ok(()); + } + let mtime = volume.node_meta(node)?.mtime; + let f = self.files.get_mut(&identity).expect("found"); if let Some((lo, hi)) = f.dirty { let hi = hi.min(size.div_ceil(PAGE)); f.dirty = (lo < hi).then_some((lo, hi)); } let id = f.object; - self.pg().resize(id, size).map_err(word) + self.pg().resize(id, size, mtime).map_err(word) } /// `identity`'s dirty pages written to the volume. A page dirtied again @@ -622,6 +657,8 @@ struct Writing<'a> { contents: u64, offset: u64, data: &'a [u8], + /// The file's mtime from this write, as the volume keeps it. + mtime: u64, } /// The runs of set bits among the first `count` of `bits`: where each starts diff --git a/system/fileserver/src/pages/tests.rs b/system/fileserver/src/pages/tests.rs index a35bdceb392..216f35ec08b 100644 --- a/system/fileserver/src/pages/tests.rs +++ b/system/fileserver/src/pages/tests.rs @@ -20,6 +20,7 @@ struct Model { next: u64, pages: BTreeMap<(u64, u64), Page>, sizes: BTreeMap, + mtimes: BTreeMap, supplies: u32, /// Every supply from here is refused. refusing: bool, @@ -129,7 +130,7 @@ impl Fake { } impl Pager for Fake { - fn object(&self, size: u64) -> Result { + fn object(&self, size: u64, mtime: u64) -> Result { let mut m = self.0.borrow_mut(); if m.sizes.len() >= m.objects { return Err(Refused::NoRoom); @@ -137,6 +138,7 @@ impl Pager for Fake { m.next += 1; let id = m.next; m.sizes.insert(id, size); + m.mtimes.insert(id, mtime); Ok(id) } @@ -172,7 +174,8 @@ impl Pager for Fake { answer } - fn write(&self, id: u64, offset: u64, bytes: &[u8]) -> Result { + fn write(&self, id: u64, offset: u64, bytes: &[u8], mtime: u64) -> Result { + self.0.borrow_mut().mtimes.insert(id, mtime); let answer = self.write_pages(id, offset, bytes); self.0.borrow_mut().changed(id, answer == Ok(bytes.len())); answer @@ -198,8 +201,14 @@ impl Pager for Fake { Ok(()) } - fn resize(&self, id: u64, size: u64) -> Result<(), Refused> { + fn stamp(&self, id: u64, mtime: u64) -> Result<(), Refused> { + self.0.borrow_mut().mtimes.insert(id, mtime); + Ok(()) + } + + fn resize(&self, id: u64, size: u64, mtime: u64) -> Result<(), Refused> { let mut m = self.0.borrow_mut(); + m.mtimes.insert(id, mtime); m.pages.retain(|(c, p), _| *c != id || *p < size.div_ceil(PAGE)); if let Some(page) = m.pages.get_mut(&(id, size / PAGE)) { page.bytes[(size % PAGE) as usize..].fill(0); @@ -464,7 +473,7 @@ fn a_truncating_open_leaves_the_object_no_page_of_before() { let truncate = OpenHow { create: false, create_new: false, truncate: true }; let again = v.open("home/f", truncate).unwrap(); - files.truncated(id).unwrap(); + files.truncated(&mut v, id, again).unwrap(); files.opened(&mut v, id, again).unwrap(); let object = files.object(id).unwrap(); assert_eq!((fake.0.borrow().sizes[&object], fake.pages_of(object)), (0, 0), "a holder reads the object, and finds nothing of before"); @@ -652,3 +661,41 @@ fn a_directorys_fsync_makes_a_new_name_durable() { let mut v = after_a_stop(v); assert_eq!(v.lstat("home/d").map(|m| m.kind), Ok(crate::volume::Kind::Dir)); } + +static NOW: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1_000_000_000); + +/// A clock a step ahead at each reading, so each change stamps a new time. +fn ticking() -> u64 { + NOW.fetch_add(1_000_000_007, std::sync::atomic::Ordering::Relaxed) +} + +/// What a holder's `fstat` reads is the object's size and mtime: after an +/// open, a write, one refused, a truncate and a stamp, each is the volume's. +#[test] +fn an_objects_size_and_mtime_are_the_volumes_after_every_change() { + let mut v = DataVolume::format(Ram::new(4096), 256, &["home"], ticking).unwrap(); + let fake = Fake::new(64, 16); + let mut files = Files::new(Some(fake.clone())); + let n = v.open("home/f", CREATE).unwrap(); + v.write(n, 0, b"before").unwrap(); + v.close(n).unwrap(); + let (node, id) = open(&mut files, &mut v, "home/f"); + let object = files.object(id).unwrap(); + let published = |v: &mut DataVolume, what: &str| { + let m = fake.0.borrow(); + let meta = v.node_meta(node).unwrap(); + assert_eq!((m.sizes[&object], m.mtimes[&object]), (meta.size, meta.mtime), "after {what}"); + }; + published(&mut v, "the open"); + files.write(&mut v, id, node, 3 * PAGE, b"after").unwrap(); + published(&mut v, "a write"); + fake.0.borrow_mut().refusing = true; + assert!(files.write(&mut v, id, node, 9 * PAGE, &pattern(2 * PAGE as usize, 1)).is_err()); + fake.0.borrow_mut().refusing = false; + published(&mut v, "a refused write"); + files.truncate(&mut v, id, node, 100).unwrap(); + published(&mut v, "a truncate"); + v.set_mtime(node, 42_000_000_000).unwrap(); + files.stamped(&mut v, id, node).unwrap(); + published(&mut v, "a stamp"); +} diff --git a/tests/guest/pagerprobe/src/main.rs b/tests/guest/pagerprobe/src/main.rs index 57f696e5320..b8a70a7515a 100644 --- a/tests/guest/pagerprobe/src/main.rs +++ b/tests/guest/pagerprobe/src/main.rs @@ -39,7 +39,7 @@ fn main() { return hold(); } let pager = Pager::create(LARGE + 16, 0, 4, Duration::from_secs(1)).expect("pagerprobe: its row starts it as a pager"); - let large = pager.object(LARGE * PAGE).expect("pagerprobe: the large object"); + let large = pager.object(LARGE * PAGE, 0).expect("pagerprobe: the large object"); fill(&pager, large); assert_eq!(pager.withdraw(large, 0, u64::MAX), Ok(LARGE), "pagerprobe: the whole object withdrawn at once"); fill(&pager, large); @@ -49,10 +49,10 @@ fn main() { Err(Refused::Kernel(SyscallError::InvalidArgument)), "pagerprobe: a second release of one object was taken" ); - let left = pager.object(LARGE * PAGE).expect("pagerprobe: the object the pager's end takes"); + let left = pager.object(LARGE * PAGE, 0).expect("pagerprobe: the object the pager's end takes"); fill(&pager, left); println!("pagerprobe: more than 512 MiB in one object withdrawn, released and left for the end"); - let object = pager.object(4096).expect("pagerprobe: an object"); + let object = pager.object(4096, 0).expect("pagerprobe: an object"); let own = pager.holder(object).expect("pagerprobe: a holder for itself"); // The handle is a holder, and this process a pager: refused by the kernel. assert_eq!( diff --git a/tests/guest/rust-tests/src/bin/a_held_files_fstat_is_its_servers_stat.rs b/tests/guest/rust-tests/src/bin/a_held_files_fstat_is_its_servers_stat.rs new file mode 100644 index 00000000000..5e760d8b63a --- /dev/null +++ b/tests/guest/rust-tests/src/bin/a_held_files_fstat_is_its_servers_stat.rs @@ -0,0 +1,41 @@ +//! A served file's `fstat` through a descriptor holding its paged object is +//! answered by the kernel from what the object publishes, without a request. +//! After a write, a truncate and a set time, it agrees with the server's +//! `stat` of the file's path on size and mtime, and on every field the open +//! answered. +//! +//! A guest test because the two answers come from two components, the +//! kernel's object and the file server's volume, which only a guest runs +//! together; each side's rule is host-tested (`fileserver` pages, `fat`). + +use std::fs::{self, File, OpenOptions}; +use std::io::Write; +use std::os::toyos::fs::MetadataExt; +use std::time::{Duration, SystemTime}; + +const FILE: &str = "/home/a-held-files-fstat"; + +fn agree(f: &File, what: &str) { + let held = f.metadata().expect("fstat"); + let served = fs::metadata(FILE).expect("stat"); + let fields = |m: &fs::Metadata| (m.len(), m.modified().ok(), m.is_file(), m.ino(), m.dev(), m.nlink()); + assert_eq!(fields(&held), fields(&served), "after {what}: the held fstat, then the server's stat"); +} + +fn main() { + let _ = fs::remove_file(FILE); + let mut f = OpenOptions::new().read(true).write(true).create(true).truncate(true).open(FILE).expect("open"); + agree(&f, "the open"); + f.write_all(&[7u8; 5000]).expect("write"); + agree(&f, "a write"); + assert_eq!(f.metadata().unwrap().len(), 5000, "the write's size"); + f.set_len(100).expect("truncate"); + agree(&f, "a truncate"); + let then = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000); + f.set_modified(then).expect("set the mtime"); + agree(&f, "a set time"); + assert_eq!(f.metadata().unwrap().modified().unwrap(), then, "the time set"); + drop(f); + fs::remove_file(FILE).unwrap(); + println!("a_held_files_fstat_is_its_servers_stat: passed"); +}