Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions issues/a-fat-files-mtime-through-its-handle-is-its-last-level.md

This file was deleted.

17 changes: 17 additions & 0 deletions issues/storage-is-layers-and-a-role-is-a-filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
56 changes: 37 additions & 19 deletions kernel/pure/paged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]).
Expand All @@ -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<u64> {
(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<Version> {
(self.writing == 0).then_some(Version(self.version))
Expand Down Expand Up @@ -358,9 +374,9 @@ impl<F: Copy> Pages<F> {
/// 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 {
Expand Down Expand Up @@ -736,7 +752,7 @@ mod tests {
#[test]
fn a_read_counts_only_where_no_write_began_or_was_under_way() {
let mut p = Pages::<u64>::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");
Expand All @@ -755,7 +771,7 @@ mod tests {
#[test]
fn a_short_call_holds_the_write_until_its_end() {
let mut p = Pages::<u64>::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);
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -866,7 +884,7 @@ mod tests {
#[test]
fn a_range_must_be_ended_exactly_as_begun_and_once() {
let mut p: Pages<u64> = 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");
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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::<u64>::new(Pages::<u64>::MAX_CAPACITY + 1, 0).is_none());
Expand All @@ -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();
}
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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<Object> = (0..4).map(|i| p.object(i, 1 << 30)).collect();
let mut objects: Vec<Object> = (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 {
Expand Down
63 changes: 48 additions & 15 deletions kernel/src/object/paged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<u32, SyscallError> {
/// 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<u64>) -> Result<u32, SyscallError> {
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<PagedStat, SyscallError> {
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) {
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<PagedStat, SyscallError> {
self.named.pager.stat(self.named.slot)
}
}

impl ZeroHandles for PagedHolderObject {
Expand Down
Loading