From 7e94a090a80de4f5ac08a1ab74d1726da4dcf08b Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:37:24 -0800 Subject: [PATCH 1/7] virtiofs: say that inodes deduplicate only within a volume The comment of VirtioFs::insert_inode said that, if the file system supports stable inode numbers and an inode already exists with the device and inode number of the inserted one, the existing inode is returned. That reads as a key over the whole file system, and it leaves out the file type that a match needs and the volumes without stable file IDs. InodeMap's summary still said that the map tracks inodes by their actual inode number. InodeMap indexes the inodes that insertions may reuse by DedupKey: (volume ID, device number, inode number) on a volume with stable file IDs, and (volume ID, path) on any other. Both keys hold the volume ID, so the device and inode numbers identify a candidate only among one volume's inodes, and a host object that two children of an aggregate reach gets a node in each. A candidate takes the inserted inode's name and lookup only if it has the same file type; one of another type leaves the key to the inserted inode, which gets a node of its own. Say so in the comment of insert_inode, without claiming that every file system has stable file IDs: a Linux volume always reports them, and a Windows volume only if its file system can open files by ID. Say in InodeMap's summary that it indexes the inodes that insertions may reuse by DedupKey. No code changes. The comment now states the invariant of section 3.6.4 of NVX's host filesystem design, under which the server matches a volume's objects by host device and inode number, or by name on volumes whose inode numbers are not stable. On Windows, a scratch test, not kept, checked the comment: two aggregate children of one directory looked up its file as two nodes with equal device and inode numbers, while a second lookup through one child kept that child's node. For virtiofs, cargo check, cargo clippy --all-targets, cargo doc --no-deps with warnings denied, with and without private items, and the package's tests pass, and cargo xtask fmt --fix changes nothing. Refs microsoft/nvx#1905 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/virtio/virtiofs/src/lib.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/vm/devices/virtio/virtiofs/src/lib.rs b/vm/devices/virtio/virtiofs/src/lib.rs index 97a6a7fe4ac..7b31a1aaee4 100644 --- a/vm/devices/virtio/virtiofs/src/lib.rs +++ b/vm/devices/virtio/virtiofs/src/lib.rs @@ -882,10 +882,19 @@ impl VirtioFs { Ok(inode) } - /// Insert a new inode, and returns the assigned node ID as well as a reference to the inode. + /// Inserts `inode`, and returns the inode that the map holds for its host + /// object, with that inode's node ID. /// - /// If the file system supports stable inode numbers and an inode already existed with this - /// device and inode number, the existing inode is returned, not the passed in one. + /// An inode already in the map is a candidate for the object only if it + /// belongs to the volume of `inode` (see [`DedupKey`]). On a volume with + /// stable file IDs, the host device and inode numbers of `inode` identify + /// a candidate within that volume; the pair is not a key across volumes, + /// so a host object that two children of an aggregate reach gets a node + /// in each. On a volume without stable file IDs, the path of `inode` + /// identifies a candidate instead. A candidate of the same file type takes + /// the new name and one more lookup, and is returned, not the passed in + /// one; one of another file type held an object that the host has since + /// replaced, so `inode` gets a node of its own. fn insert_inode(&self, inode: VirtioFsInode) -> lx::Result<(Arc, u64)> { let mut inodes = self.inner.inodes.write(); microvm::fs::insert_inode(self, &mut inodes, inode) @@ -1164,7 +1173,7 @@ impl HandleMap { } } -/// Assigns node IDs to inodes, and keeps track of in-use inodes by their actual inode number. +/// Assigns node IDs to inodes, and indexes the inodes that insertions may reuse by [`DedupKey`]. /// /// We cannot use the real inode number as the FUSE node ID: /// - FUSE node ID 1 is reserved for the root, so this would break if a file system used that inode From 06e69049e1109704d09760590f0d92bcc956138e Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:38:13 -0800 Subject: [PATCH 2/7] chipset_arc_mutex_device: find offsets in a region that ends at the top TestChipset, the MMIO-only chipset on which device unit tests, such as vpci's, build their devices, found the offset of an address in one of its regions by comparing the address with the region's exclusive end, base + len (microsoft/nvx#1902). For a region whose last byte is u64::MAX, that end does not fit the address type, although map accepts the region, since it checks only that the last byte fits. The first lookup in such a region then panicked with "attempt to add with overflow" where overflow checks are on, as in debug builds, and otherwise wrapped to an empty range, so that the region gave no offset for any address. vmotherboard, the production chipset, had the same defect until "vmotherboard: find offsets in an I/O region that ends at the top" (microsoft/openvmm#5168), so a test on TestChipset could disagree with it about such a region. No device that a test builds on TestChipset looks up an offset today, so the defect is latent. Subtract the base instead, with checked_sub, and accept the address when the offset is below the region's length, as vmotherboard does, so that no end address is formed. The result is the same for every other region and address. Test: mmio_region_ending_at_the_top_finds_its_offsets, in test_chipset's tests, applies the boundary cases of microsoft/openvmm#5168 to TestChipset: it maps a 4 KiB region at u64::MAX - 0xfff, and checks the offsets of the byte before it and of its first and last bytes, then again after a remap one byte lower, which ends the region just below the top, and after an unmap. At feature/microvm/dev 899dcd63f, it fails with "attempt to add with overflow" in a debug build, and, in a release build, without overflow checks, with no offset for the region's first byte. It passes with the fix in both. The tests now initialize tracing with test_with_tracing, as OpenVMM's unit tests do. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 1 + vm/chipset_arc_mutex_device/Cargo.toml | 3 ++ .../src/test_chipset.rs | 32 ++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1d41733ce9..01a357aca69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -624,6 +624,7 @@ dependencies = [ "closeable_mutex", "parking_lot", "range_map_vec", + "test_with_tracing", "thiserror 2.0.16", "tracing", ] diff --git a/vm/chipset_arc_mutex_device/Cargo.toml b/vm/chipset_arc_mutex_device/Cargo.toml index 031d212dcaa..11565e360d3 100644 --- a/vm/chipset_arc_mutex_device/Cargo.toml +++ b/vm/chipset_arc_mutex_device/Cargo.toml @@ -17,5 +17,8 @@ parking_lot.workspace = true thiserror.workspace = true tracing.workspace = true +[dev-dependencies] +test_with_tracing.workspace = true + [lints] workspace = true diff --git a/vm/chipset_arc_mutex_device/src/test_chipset.rs b/vm/chipset_arc_mutex_device/src/test_chipset.rs index e94b4de28aa..e95e2d9ad2e 100644 --- a/vm/chipset_arc_mutex_device/src/test_chipset.rs +++ b/vm/chipset_arc_mutex_device/src/test_chipset.rs @@ -84,10 +84,11 @@ impl ControlMmioIntercept for TestDeviceRange { fn offset_of(&self, addr: u64) -> Option { let base = self.addr?; - - (base..(base + self.len)) - .contains(&addr) - .then(|| addr - base) + // Compare the offset with the length: the exclusive end of a region + // that ends at the top of the address space does not fit in the + // address type. + let offset = addr.checked_sub(base)?; + (offset < self.len).then_some(offset) } } @@ -190,6 +191,7 @@ impl MmioInterceptServices for TestChipsetServicesImpl<'_> { #[cfg(test)] mod tests { use super::*; + use test_with_tracing::test; mod sample_dev { use super::*; @@ -262,4 +264,26 @@ mod tests { Ok(()) } + + #[test] + fn mmio_region_ending_at_the_top_finds_its_offsets() { + let mut mapper = TestMmioRangeMapper { + dev: Weak::>::new(), + map: Default::default(), + }; + let mut region = mapper.new_io_region("top", 0x1000); + let base = u64::MAX - 0xfff; + assert_eq!(region.offset_of(u64::MAX), None); + region.map(base); + assert_eq!(region.addr(), Some(base)); + assert_eq!(region.offset_of(base - 1), None); + assert_eq!(region.offset_of(base), Some(0)); + assert_eq!(region.offset_of(u64::MAX), Some(0xfff)); + region.map(base - 1); + assert_eq!(region.offset_of(base - 1), Some(0)); + assert_eq!(region.offset_of(u64::MAX - 1), Some(0xfff)); + assert_eq!(region.offset_of(u64::MAX), None); + region.unmap(); + assert_eq!(region.offset_of(base), None); + } } From 3f7112dea23464c07647eccb9290f335d38d3926 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:38:23 -0800 Subject: [PATCH 3/7] chipset_device_fuzz: find offsets in an I/O region that ends at the top FuzzChipset, on which the chipset and IDE fuzzers run their devices, found the offset of an address in one of its port I/O or MMIO regions by comparing the address with the region's exclusive end, base + len (microsoft/nvx#1902). For a region whose last byte is the top of its address space, such as 2 ports at 0xfffe or an MMIO region that ends at u64::MAX, that end does not fit the address type, although map accepts the region, since it checks only that the last byte fits. The first lookup in such a region then panicked with "attempt to add with overflow" where overflow checks are on, as in debug builds, and otherwise wrapped to an empty range, so that the region gave no offset for any address. A fuzzed device that looked up an offset there would thus crash, or ignore an access to its own region, where vmotherboard, the production chipset, finds the offset since "vmotherboard: find offsets in an I/O region that ends at the top" (microsoft/openvmm#5168). None of the devices that the fuzzers run today looks up an offset, so the defect is latent. Subtract the base instead, with checked_sub, and accept the address when the offset is below the region's length, as vmotherboard does, so that no end address is formed. The result is the same for every other region and address, for both widths that impl_intercept! generates. Tests, in the crate's new tests module: pio_region_ending_at_the_top_finds_its_offsets and mmio_region_ending_at_the_top_finds_its_offsets apply the boundary cases of microsoft/openvmm#5168 to FuzzChipset: they map a 2-port region at 0xfffe and a 4 KiB MMIO region at u64::MAX - 0xfff, and check the offsets of the byte before each region and of its first and last bytes, then again after a remap one byte lower, which ends the region just below the top, and after an unmap. At feature/microvm/dev 899dcd63f, both fail with "attempt to add with overflow" in a debug build, and, in a release build, without overflow checks, with no offset for the region's first byte. Both pass with the fix in both builds. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 1 + vm/chipset_device_fuzz/Cargo.toml | 3 ++ vm/chipset_device_fuzz/src/lib.rs | 61 +++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01a357aca69..b87a2ac1a62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -650,6 +650,7 @@ dependencies = [ "closeable_mutex", "parking_lot", "range_map_vec", + "test_with_tracing", "tracing", ] diff --git a/vm/chipset_device_fuzz/Cargo.toml b/vm/chipset_device_fuzz/Cargo.toml index 351768119d3..2c44f19f03a 100644 --- a/vm/chipset_device_fuzz/Cargo.toml +++ b/vm/chipset_device_fuzz/Cargo.toml @@ -18,5 +18,8 @@ arbitrary = { workspace = true, features = ["derive"] } parking_lot.workspace = true tracing.workspace = true +[dev-dependencies] +test_with_tracing.workspace = true + [lints] workspace = true diff --git a/vm/chipset_device_fuzz/src/lib.rs b/vm/chipset_device_fuzz/src/lib.rs index 0a7aa540554..25085f40b4f 100644 --- a/vm/chipset_device_fuzz/src/lib.rs +++ b/vm/chipset_device_fuzz/src/lib.rs @@ -489,10 +489,11 @@ macro_rules! impl_intercept { fn offset_of(&self, addr: $usize) -> Option<$usize> { let base = self.addr?; - - (base..(base + self.len)) - .contains(&addr) - .then(|| addr - base) + // Compare the offset with the length: the exclusive end of a + // region that ends at the top of the address space does not + // fit in the address type. + let offset = addr.checked_sub(base)?; + (offset < self.len).then_some(offset) } } }; @@ -628,3 +629,55 @@ impl PollDeviceServices for FuzzChipsetServicesImpl<'_> { self.took_poll.get() } } + +#[cfg(test)] +mod tests { + use super::*; + use test_with_tracing::test; + + enum NoDevice {} + + impl ChipsetDevice for NoDevice {} + + fn mapper() -> FuzzRegisterIntercept { + FuzzRegisterIntercept { + dev: Weak::>::new(), + map: Default::default(), + } + } + + #[test] + fn pio_region_ending_at_the_top_finds_its_offsets() { + let mut region = mapper::().new_io_region("top", 2); + assert_eq!(region.offset_of(0xffff), None); + region.map(0xfffe); + assert_eq!(region.addr(), Some(0xfffe)); + assert_eq!(region.offset_of(0xfffd), None); + assert_eq!(region.offset_of(0xfffe), Some(0)); + assert_eq!(region.offset_of(0xffff), Some(1)); + region.map(0xfffd); + assert_eq!(region.offset_of(0xfffd), Some(0)); + assert_eq!(region.offset_of(0xfffe), Some(1)); + assert_eq!(region.offset_of(0xffff), None); + region.unmap(); + assert_eq!(region.offset_of(0xfffe), None); + } + + #[test] + fn mmio_region_ending_at_the_top_finds_its_offsets() { + let mut region = mapper::().new_io_region("top", 0x1000); + let base = u64::MAX - 0xfff; + assert_eq!(region.offset_of(u64::MAX), None); + region.map(base); + assert_eq!(region.addr(), Some(base)); + assert_eq!(region.offset_of(base - 1), None); + assert_eq!(region.offset_of(base), Some(0)); + assert_eq!(region.offset_of(u64::MAX), Some(0xfff)); + region.map(base - 1); + assert_eq!(region.offset_of(base - 1), Some(0)); + assert_eq!(region.offset_of(u64::MAX - 1), Some(0xfff)); + assert_eq!(region.offset_of(u64::MAX), None); + region.unmap(); + assert_eq!(region.offset_of(base), None); + } +} From 09966aa637e0d488f71c7d7536264357d39db32f Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:36:50 -0800 Subject: [PATCH 4/7] virtiofs: keep the node that a LINK returns until the guest forgets it A successful FUSE_LINK reply hands the guest a lookup reference to the target node, but VirtioFs::link took that reference, with inc_lookup, only after its host link, and without the inode map's lock (microsoft/nvx#1897). FORGET removes a node under that lock once its lookup count reaches zero, and the guest sends it on the high-priority queue, whose worker runs independently of the request queue's. A FORGET that dropped the guest's other references to the target while the LINK ran therefore removed the node, and the LINK then counted its reference on the removed inode and replied with the node's ID: every later request about the node failed with EINVAL (request for unknown inode), though the reply had handed the guest a reference to it, and the new name went to an inode that nothing tracked. LOOKUP, CREATE, MKNOD, READDIRPLUS, and the other requests that return a node take their reference in InodeMap::insert or insert_reserved, under the lock, so they either find the node still mapped or map the object anew. LINK now takes its reply's reference when it resolves the target, with VirtioFs::reference_inode, which increments the node's lookup count under the inode map's lock. A FORGET thus either removed the node first, and the LINK fails with EINVAL, as a request about any unknown node does, or leaves the node mapped until the guest forgets the reply's reference too. A LINK that fails afterwards, before or at its host link, releases the reference when its LookupReference drops, as a FORGET of it would: through InodeMap::forget, which FORGET now uses too, and which removes the node if the guest has forgotten it meanwhile. inc_lookup now reports whether it changed the count, so that a failed LINK leaves a count that stopped at u64::MAX there. The microVM profile shares this path and only adds its alias bounds, so it needs no change of its own. Tests, in virtiofs' tests, on a direct and on a microVM share: link_keeps_its_node_through_a_concurrent_forget holds the target's path lock, which a LINK needs to record its new name after its host link, so that the LINK stops there; it then forgets the guest's lookup of the target, lets the LINK finish, and checks that the reply's node answers GETATTR until the guest forgets the reply's reference too. failed_link_retains_no_reference checks that a LINK that fails with ENOTDIR before its host link, or with EEXIST at it, leaves no reference, and released_link_reference_removes_a_forgotten_node, on a direct share, that the release of a LINK's reference that a FORGET overtook removes the node. Without the fix, at 899dcd63f, the first test fails on Windows: GETATTR of the reply's node fails with EINVAL. With it, the three tests and the package's other tests pass on Windows. Without the release of a failed LINK's reference, the other two fail. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/virtio/virtiofs/src/inode.rs | 8 +- vm/devices/virtio/virtiofs/src/lib.rs | 230 ++++++++++++++++++++++-- 2 files changed, 221 insertions(+), 17 deletions(-) diff --git a/vm/devices/virtio/virtiofs/src/inode.rs b/vm/devices/virtio/virtiofs/src/inode.rs index 48d58c74bca..c5e3780c2e8 100644 --- a/vm/devices/virtio/virtiofs/src/inode.rs +++ b/vm/devices/virtio/virtiofs/src/inode.rs @@ -365,18 +365,20 @@ impl VirtioFsInode { *path = new_path; } - /// Increments the lookup count without updating the path, saturating at `u64::MAX`. + /// Increments the lookup count without updating the path, saturating at `u64::MAX`, and + /// returns whether the count changed. /// /// This is used when returning an existing inode in a FUSE reply (e.g., for hard links) /// where the kernel will track the reference and later send a forget. /// /// A count that wrapped to zero would let the next forget evict an inode that the guest /// still references, so the count stops at `u64::MAX` instead. - pub fn inc_lookup(&self) { + pub fn inc_lookup(&self) -> bool { self.lookup_count .update(Ordering::AcqRel, Ordering::Acquire, |count| { count.saturating_add(1) - }); + }) + != u64::MAX } /// Decrements the lookup count, and returns the new count. diff --git a/vm/devices/virtio/virtiofs/src/lib.rs b/vm/devices/virtio/virtiofs/src/lib.rs index 7b31a1aaee4..9677b92da21 100644 --- a/vm/devices/virtio/virtiofs/src/lib.rs +++ b/vm/devices/virtio/virtiofs/src/lib.rs @@ -268,13 +268,7 @@ impl Fuse for VirtioFs { } // This must be done under lock so an inode can't be resurrected between the lookup count // reaching zero and removing it from the list. - let mut inodes = self.inner.inodes.write(); - if let Some(inode) = inodes.get(node_id) { - if inode.forget(node_id, lookup_count) == 0 { - tracing::trace!(node_id, "Removing inode"); - inodes.remove(node_id); - } - } + self.inner.inodes.write().forget(node_id, lookup_count); } fn open(&self, request: &Request, flags: u32) -> lx::Result { @@ -413,7 +407,12 @@ impl Fuse for VirtioFs { return Err(lx::Error::EROFS); } let inode = self.get_inode(request.node_id())?; - let target_inode = self.get_inode(target)?; + // The reply hands the guest a lookup reference to the target node. + // Take it now, while the inode map holds the node, so that a FORGET + // on another queue cannot remove the node before the reply returns + // it. A link that fails releases the reference. + let target_reference = self.reference_inode(target)?; + let target_inode = target_reference.inode(); // Like Linux's linkat, which reports a read-only destination before a // link across filesystems, check where the link would go first. self.check_writable(&inode)?; @@ -429,13 +428,11 @@ impl Fuse for VirtioFs { if target_inode.readonly() { return Err(lx::Error::EXDEV); } - self.preflight_alias_add(&target_inode, &alias)?; - let attr = inode.link(name, &target_inode)?; + self.preflight_alias_add(target_inode, &alias)?; + let attr = inode.link(name, target_inode)?; target_inode.add_alias(alias); - - // Increment the lookup count since we're returning an entry for this inode. - // The kernel will send a forget for this entry later. - target_inode.inc_lookup(); + // The kernel will send a forget for the reply's reference later. + target_reference.keep(); // Use the target inode as the reply, with refreshed attributes. Ok(fuse_entry_out::new( @@ -916,6 +913,40 @@ impl VirtioFs { }) } + /// Takes a lookup reference to the node `node_id` for a reply that + /// returns the node to the guest, as [`Self::insert_inode`] takes one when + /// a lookup finds a node that the inode map holds. + /// + /// A FORGET removes a node under the inode map's lock once the guest holds + /// no reference to it, so this takes the reference under that lock too: + /// either the FORGET removed the node first, and this fails with `EINVAL`, + /// as a request about any unknown node does, or the node stays in the map + /// until the guest forgets this reference as well. Dropping the reference + /// releases it, as a FORGET of it would, unless the request keeps it for + /// its reply (see [`LookupReference::keep`]). Like a lookup's, the + /// reference adds nothing to a count that stopped at `u64::MAX` (see + /// [`VirtioFsInode::inc_lookup`]), so dropping it then releases nothing. + fn reference_inode(&self, node_id: u64) -> lx::Result> { + let reference = { + let inodes = self.inner.inodes.read(); + let inode = inodes.get(node_id).ok_or_else(|| { + tracelimit::warn_ratelimited!(node_id, "request for unknown inode"); + lx::Error::EINVAL + })?; + // A FORGET takes the write lock, so the read lock keeps it out + // while the count, which changes atomically, goes up. + let release = inode.inc_lookup(); + LookupReference { + inodes: &self.inner.inodes, + node_id, + inode, + release, + } + }; + reference.inode.validate_confined_object()?; + Ok(reference) + } + /// Retrieves the open file `fh` of a request about the node `node_id`. /// /// The handle must have been opened on that node, and the guest must still @@ -1002,6 +1033,48 @@ impl Drop for InodeReservation<'_> { } } +/// A lookup reference to a node that a request takes for a reply that +/// returns the node to the guest (see [`VirtioFs::reference_inode`]). +struct LookupReference<'a> { + inodes: &'a RwLock, + node_id: u64, + inode: Arc, + /// Whether dropping the reference releases it: taking it added to the + /// node's lookup count, which stops at `u64::MAX`, and the request has + /// not kept it. + release: bool, +} + +impl LookupReference<'_> { + /// Returns the node's inode. + fn inode(&self) -> &Arc { + &self.inode + } + + /// Keeps the reference for the reply, which hands it to the guest. + fn keep(mut self) { + self.release = false; + } +} + +impl Drop for LookupReference<'_> { + fn drop(&mut self) { + if !self.release { + return; + } + let mut inodes = self.inodes.write(); + // Since the reference was taken, a FUSE_DESTROY may have emptied the + // map, or a FORGET of more references than the guest held may have + // removed the node. + if inodes + .get(self.node_id) + .is_some_and(|inode| Arc::ptr_eq(&inode, &self.inode)) + { + inodes.forget(self.node_id, 1); + } + } +} + struct FileReservation<'a> { files: &'a RwLock>>, reservation: HandleReservation, @@ -1313,6 +1386,21 @@ impl InodeMap { .filter(|existing| Self::reuses(existing, inode)) } + /// Drops `lookup_count` of the guest's lookup references to the node + /// `node_id`, and removes the node once it has none left. + /// + /// The caller holds the map's lock, so no request can take another + /// reference to the node between its count reaching zero and its removal. + pub fn forget(&mut self, node_id: u64, lookup_count: u64) { + let Some(inode) = self.get(node_id) else { + return; + }; + if inode.forget(node_id, lookup_count) == 0 { + tracing::trace!(node_id, "Removing inode"); + self.remove(node_id); + } + } + /// Remove an inode with the specified FUSE node ID from the map. pub fn remove(&mut self, node_id: u64) { let Some(inode) = self.inodes_by_node_id.remove(node_id) else { @@ -1420,6 +1508,9 @@ impl InodeMap { mod tests { use super::HandleMap; use super::VirtioFs; + use super::profile::MICROVM_ATTACHMENT_ID; + use super::profile::MicroVmVirtioFsProfile; + use super::profile::microvm_root_identity; use fuse::Fuse; use fuse::Request; use fuse::protocol::*; @@ -2067,6 +2158,117 @@ mod tests { assert!(fs.get_inode(FUSE_ROOT_ID).is_ok()); } + /// Returns a direct file system of `root`, or a writable microVM one if + /// `microvm`, whose root holds the regular file `file`, which the guest + /// has looked up once, with the file's node ID. + fn fs_with_a_looked_up_file(root: &Path, microvm: bool) -> (VirtioFs, u64) { + std::fs::write(root.join("file"), b"data").unwrap(); + let fs = if microvm { + let profile = MicroVmVirtioFsProfile::from_attachment( + MICROVM_ATTACHMENT_ID.to_owned(), + microvm_root_identity(root).unwrap(), + false, + Vec::new(), + ) + .unwrap(); + VirtioFs::new_microvm(root, profile).unwrap() + } else { + VirtioFs::new(root, None).unwrap() + }; + let node_id = fs + .lookup(&request(FUSE_ROOT_ID), "file".as_ref()) + .unwrap() + .nodeid; + (fs, node_id) + } + + /// A successful LINK hands the guest a lookup reference to its target + /// node, so the node must stay in the inode table until the guest forgets + /// that reference too, even when a FORGET, which the guest sends on + /// another queue, drops every other reference while the LINK runs. + #[test] + fn link_keeps_its_node_through_a_concurrent_forget() { + for microvm in [false, true] { + let root = tempfile::tempdir().unwrap(); + let (fs, node_id) = fs_with_a_looked_up_file(root.path(), microvm); + let target = fs.get_inode(node_id).unwrap(); + // The FORGET below finds the node by its inode number, without + // the path that the test holds. + assert!(target.volume.supports_stable_file_id()); + + // A LINK records the new name after its host link, under the + // target's alias lock and then its path lock, so while the test + // holds the path, the LINK stops there, before it replies. + let path = target.path.read(); + std::thread::scope(|scope| { + let link = + scope.spawn(|| fs.link(&request(FUSE_ROOT_ID), "link".as_ref(), node_id)); + while !target.aliases.is_locked_exclusive() && !link.is_finished() { + std::thread::yield_now(); + } + fs.forget(node_id, 1); + drop(path); + assert_eq!(link.join().unwrap().unwrap().nodeid, node_id); + }); + + fs.get_attr(&request(node_id), 0, 0).unwrap(); + fs.forget(node_id, 1); + assert_eq!( + fs.get_attr(&request(node_id), 0, 0).unwrap_err(), + lx::Error::EINVAL + ); + } + } + + /// A LINK that fails, whether before or at its host link, hands the guest + /// no reference to its target node, so the node leaves the inode table + /// once the guest forgets the references that it holds. + #[test] + fn failed_link_retains_no_reference() { + for microvm in [false, true] { + let root = tempfile::tempdir().unwrap(); + let (fs, node_id) = fs_with_a_looked_up_file(root.path(), microvm); + std::fs::write(root.path().join("existing"), b"other").unwrap(); + + assert_eq!( + fs.link(&request(node_id), "link".as_ref(), node_id) + .unwrap_err(), + lx::Error::ENOTDIR + ); + assert_eq!( + fs.link(&request(FUSE_ROOT_ID), "existing".as_ref(), node_id) + .unwrap_err(), + lx::Error::EEXIST + ); + + fs.forget(node_id, 1); + assert_eq!( + fs.get_attr(&request(node_id), 0, 0).unwrap_err(), + lx::Error::EINVAL + ); + } + } + + /// A LINK takes the reference that its reply hands the guest before it + /// links on the host, so a FORGET of the guest's other references to the + /// target node meanwhile leaves the node in the inode table. A LINK that + /// then fails releases the reference as a FORGET of it would, which + /// removes the node, since the guest holds no reference to it. + #[test] + fn released_link_reference_removes_a_forgotten_node() { + let root = tempfile::tempdir().unwrap(); + let (fs, node_id) = fs_with_a_looked_up_file(root.path(), false); + + let reference = fs.reference_inode(node_id).unwrap(); + fs.forget(node_id, 1); + fs.get_attr(&request(node_id), 0, 0).unwrap(); + drop(reference); + assert_eq!( + fs.get_attr(&request(node_id), 0, 0).unwrap_err(), + lx::Error::EINVAL + ); + } + #[test] #[should_panic(expected = "root inode ID changed during clear")] fn clear_rejects_corrupt_root_allocator() { From b5914cf2da2a9a0f17a6b4be3a5323e4f14a9b02 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:41:41 -0800 Subject: [PATCH 5/7] virtiofs: fail a microVM save while the guest holds a replaced object A microVM save succeeded while the guest held an object whose name the host had given to another object, as host software does when it renames a new file over a file that the guest holds open, and the snapshot then failed to restore (microsoft/nvx#1901). Once the guest had looked the name up again, which gives the new object a node of its own, the held node and the new one both recorded the name, and the restore failed with "saved aliases are duplicated or ambiguous"; without that lookup, it failed with "saved guest inode ID does not match the attachment". The save took the identity that it records for a node from whatever object the node's first name reached at the save, and checked only that the node's other names reached the same object, never that the object was the node's own. So it recorded the held node with the new object's identity, under the guest inode number of the held object, which no restore accepts, and a restore that did would have bound the held node to the new object. The save now records each node's own identity, the device, inode number, and file type of the object that the node got, from which its guest inode number derives, and checks every name of the node against it, as a restore does. A name that reaches another object fails the save, with an error that names the node and its guest inode number and says that the guest must release the object, as a name that is gone already fails it. The held object has no name left by which a restore could reopen it, and a snapshot records nothing else, so no save can keep it; the snapshot format is unchanged. Once the guest releases the object, the save succeeds, and its restore binds the name to the new object. a_held_file_that_the_host_replaces_fails_the_save_until_released holds a file open, has the host rename a new file over it, with and without a lookup of the new file, and checks that the save fails and says why, that the guest's handle still reads the old file, that a snapshot saved before the replacement does not restore, and that once the guest releases the file, the save succeeds and its restore gives the name the new file's node and identity. Without the fix, at 899dcd63f, on Windows the save succeeds, and the restore of such a save fails with the errors above. With the fix, the test and the package's other tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/reference/devices/virtio/virtio-fs.md | 6 ++ vm/devices/virtio/virtiofs/src/microvm/fs.rs | 24 ++--- .../virtio/virtiofs/src/microvm/fs_tests.rs | 87 +++++++++++++++++++ .../virtio/virtiofs/src/microvm/inode.rs | 13 +++ .../virtio/virtiofs/src/microvm/state.rs | 13 --- 5 files changed, 120 insertions(+), 23 deletions(-) diff --git a/Guide/src/reference/devices/virtio/virtio-fs.md b/Guide/src/reference/devices/virtio/virtio-fs.md index 05f5605d58c..536f73a37af 100644 --- a/Guide/src/reference/devices/virtio/virtio-fs.md +++ b/Guide/src/reference/devices/virtio/virtio-fs.md @@ -377,6 +377,12 @@ without being followed, and an alias whose ancestor has become a link fails restore. A handle that the guest held open for writing must still be on a writable path. +Capture checks those identities first, rather than save a snapshot that +cannot restore: it fails while the guest holds an object whose name the host +has given to another object, as host software does when it renames a new file +over a file that the guest holds open, until the guest kernel releases that +object, as it does once the guest closes the file. + A snapshot of an aggregate records each child's name, canonical host path, root identity, access mode, and denied, allowed, and writable paths, and the attachment's identity is the SHA-256 of the children's names and root diff --git a/vm/devices/virtio/virtiofs/src/microvm/fs.rs b/vm/devices/virtio/virtiofs/src/microvm/fs.rs index 08d644422d2..6ecf8784511 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/fs.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/fs.rs @@ -32,7 +32,6 @@ use super::state::validate_identity; use super::state::validate_microvm_state; use super::state::validate_relative_path; use super::state::validate_reopenable_alias; -use super::state::validate_saved_identity; use crate::ATTRIBUTE_TIMEOUT; use crate::ENTRY_TIMEOUT; use crate::FuseNegotiation; @@ -532,21 +531,26 @@ impl VirtioFs { .context("saved alias count overflow")?; anyhow::ensure!(alias_count <= MAX_ALIASES, "saved alias table is too large"); let volume = inode.volume(); + // A restore binds the node only to its own object, which the + // node's guest inode number numbers, so every name must still + // reach that object rather than one that the host has put in + // its place. + let object_identity = inode.object_identity(); let mut relative_aliases = Vec::with_capacity(aliases.len()); - let mut object_identity = None; for alias in aliases { validate_relative_path(&alias, true) .map_err(anyhow::Error::from) .context("inode has an unsafe relative alias")?; let stat = validate_reopenable_alias(&volume, &alias) .context("inode alias cannot be revalidated for save")?; - let identity = saved_identity(&stat); - if let Some(expected) = &object_identity { - validate_saved_identity(&identity, expected) - .context("inode aliases identify different objects")?; - } else { - object_identity = Some(identity); - } + validate_identity(&stat, &object_identity).with_context(|| { + format!( + "node {node_id} (guest inode {}) has a name that reaches another \ + host object, as after the host replaced an object that the \ + guest holds; the guest must release the object before a save", + inode.guest_inode_nr() + ) + })?; let alias = encode_relative_path(&alias)?; alias_bytes = alias_bytes .checked_add(alias.len()) @@ -563,7 +567,7 @@ impl VirtioFs { relative_aliases, lookup_count: inode.lookup_count(), guest_inode_id: inode.guest_inode_nr(), - object_identity: object_identity.context("inode has no aliases")?, + object_identity, }); anyhow::ensure!( node_ids diff --git a/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs b/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs index 2090e421afa..23fb68f1225 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs @@ -3,7 +3,11 @@ //! Tests for microVM filesystem persistence and limits. +use super::policy_tests::lookup; use super::policy_tests::name; +use super::policy_tests::open; +use super::policy_tests::read_node; +use super::policy_tests::release; use super::policy_tests::request; use super::profile::MICROVM_ATTACHMENT_ID; use super::profile::MICROVM_FUSE_MAJOR; @@ -1313,6 +1317,89 @@ fn hard_link_aliases_restore_and_reject_missing_or_replaced_names() { ); } +/// Host software often replaces a file by renaming a new file over its name. +/// When the guest holds the old file open, the file's node keeps the name, +/// which now reaches the new file, and a lookup of the name gives the new file +/// a node of its own. A restore could reopen the held file by none of its +/// names, and must not bind its node to the new file, so a save must fail, +/// and say why, rather than save a snapshot that cannot restore, whether or +/// not the guest has looked the new file up. The guest's handle still reads +/// the old file. Once the guest releases the old file, a save succeeds, and +/// its restore binds the name to the new file, at the new file's node if the +/// guest has one. A snapshot saved before the replacement no longer restores, +/// since the held node's name reaches another object. +#[test] +fn a_held_file_that_the_host_replaces_fails_the_save_until_released() { + for look_up_replacement in [false, true] { + let case = format!("look_up_replacement={look_up_replacement}"); + let temporary_directory = tempdir().unwrap(); + let root_path = temporary_directory.path(); + std::fs::write(root_path.join("file"), b"old").unwrap(); + let profile = profile(root_path); + let fs = VirtioFs::new_microvm(root_path, profile.clone()).unwrap(); + let held = lookup(&fs, "file").unwrap(); + let fh = open(&fs, held, lx::O_RDONLY).unwrap(); + let saved_before = fs + .save_microvm_state(&profile, SessionState::default()) + .unwrap(); + let restore = |state| { + let destination = VirtioFs::new_microvm(root_path, profile.clone()).unwrap(); + let session = Session::new(destination.clone()); + destination + .restore_microvm_state(&profile, state, &session) + .map(|()| destination) + }; + + std::fs::write(root_path.join("new"), b"new").unwrap(); + std::fs::rename(root_path.join("new"), root_path.join("file")).unwrap(); + let replacement = look_up_replacement.then(|| lookup(&fs, "file").unwrap()); + assert_ne!(replacement, Some(held), "{case}"); + + let Err(error) = fs.save_microvm_state(&profile, SessionState::default()) else { + panic!("{case}: a save succeeded while the guest held a replaced file"); + }; + let message = format!("{error:#}"); + assert!( + message.contains(&format!("node {held} ")) + && message.contains("has a name that reaches another host object"), + "{case}: {message}" + ); + let mut read = fuse_read_in::new_zeroed(); + read.fh = fh; + read.size = 64; + assert_eq!(fs.read(&request(held), &read).unwrap(), b"old", "{case}"); + let Err(error) = restore(saved_before) else { + panic!("{case}: a restore bound the held node to the new file"); + }; + assert!( + format!("{error:#}").contains("saved inode identity does not match the attachment"), + "{case}: {error:#}" + ); + + release(&fs, held, fh); + fs.forget(held, 1); + let state = fs + .save_microvm_state(&profile, SessionState::default()) + .unwrap(); + let destination = restore(state).unwrap(); + assert_eq!( + destination.get_attr(&request(held), 0, 0).err(), + Some(lx::Error::EINVAL), + "{case}" + ); + let node = lookup(&destination, "file").unwrap(); + if let Some(replacement) = replacement { + assert_eq!(node, replacement, "{case}"); + assert_eq!( + destination.get_attr(&request(node), 0, 0).unwrap().attr.ino, + fs.get_attr(&request(replacement), 0, 0).unwrap().attr.ino, + "{case}" + ); + } + assert_eq!(read_node(&destination, node), b"new", "{case}"); + } +} + /// Objects with one inode number on two devices, as `/proc` and `/sys` are, /// save as two inodes and restore as two. #[cfg(target_os = "linux")] diff --git a/vm/devices/virtio/virtiofs/src/microvm/inode.rs b/vm/devices/virtio/virtiofs/src/microvm/inode.rs index 32c1ea8844d..7259307c05b 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/inode.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/inode.rs @@ -6,6 +6,7 @@ use super::policy::PathVisibility; use super::policy::SubtreePolicy; use super::saved_state::MAX_PATH_BYTES; +use super::saved_state::SavedObjectIdentity; use super::state::relative_path_encoded_len; use super::state::validate_relative_path; use crate::inode::VirtioFsInode; @@ -147,6 +148,18 @@ impl VirtioFsInode { Ok(inode) } + /// Returns the identity of this inode's own object, which a snapshot + /// records for the inode: the object that the guest reached when it got + /// the node, or that a restore bound the node to, which the node's guest + /// inode number numbers. + pub(crate) fn object_identity(&self) -> SavedObjectIdentity { + SavedObjectIdentity { + device_id: self.device_nr(), + inode_id: self.inode_nr(), + kind: self.file_type(), + } + } + /// Checks that a microVM inode used as a directory is reached without /// crossing a symbolic link, including at its final component. /// diff --git a/vm/devices/virtio/virtiofs/src/microvm/state.rs b/vm/devices/virtio/virtiofs/src/microvm/state.rs index 99699680df7..1a1d7ddbab7 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/state.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/state.rs @@ -163,19 +163,6 @@ pub(crate) fn validate_identity( Ok(()) } -pub(crate) fn validate_saved_identity( - actual: &SavedObjectIdentity, - expected: &SavedObjectIdentity, -) -> anyhow::Result<()> { - anyhow::ensure!( - actual.kind == expected.kind - && actual.device_id == expected.device_id - && actual.inode_id == expected.inode_id, - "object identity changed between aliases" - ); - Ok(()) -} - pub(crate) fn validate_reopenable_alias( volume: &VirtioFsVolume, path: &Path, From b2ac1073de026d2de85bb5bbcf1aa061bffd87f0 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 11:49:55 -0800 Subject: [PATCH 6/7] virtiofs: fail a microVM save of nodes or handles that a restore rejects A microVM save now fails when a name of a node reaches another object than the node's own (microsoft/nvx#1901), but a restore also rejects two nodes that share a name or the key of one object, and a handle that its node's names do not reopen, which a save still let through. A host makes either when it reuses inode numbers or puts an object back. When it gives the inode number of an object that the guest holds to an object of another type, and then to one of the held object's type again, while the guest looks up each, two nodes name one object; they save, and the restore fails with "saved aliases are duplicated or ambiguous", or, when their names differ, with "saved inode aliases are ambiguous". When the guest opens a node while the host has replaced its object, as Linux does when a program reopens a file through /proc/self/fd, its handle reaches the replacement; once the host gives the node's name its object back, the handle saves, and the restore fails with "reopened handle identity does not match". The save now keys the nodes that it records by their names and by their dedup keys, as a restore does, and fails when two nodes share either, with an error that names both and says that the guest must release one of them. It also checks each handle against its node's identity, and fails with an error that names the handle and its node and says that the guest must close the handle. The snapshot format is unchanged. nodes_that_name_one_object_fail_the_save gives a file's inode number to a directory and then to a file again, at the file's name or at another name of it, as the tests of retyped objects do, and a_handle_of_another_object_than_its_node_fails_the_save opens a node while the host has replaced its object and then puts the object back. Each checks that the save fails with its error, and that it succeeds once the guest releases the extra node or handle. Without this change, on top of the previous commit, on Windows, both saves succeed, and their restores fail with the errors above. With it, both tests and the package's other tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/virtio/virtiofs/src/microvm/fs.rs | 35 +++++++++ .../virtio/virtiofs/src/microvm/fs_tests.rs | 77 +++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/vm/devices/virtio/virtiofs/src/microvm/fs.rs b/vm/devices/virtio/virtiofs/src/microvm/fs.rs index 6ecf8784511..d9940904f23 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/fs.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/fs.rs @@ -292,6 +292,20 @@ fn open_microvm_volume( Ok((volume, root_inode, root_stat)) } +/// Returns the error of a save whose nodes `first` and `second` name one host +/// object, by one name or by two, which a restore cannot tell apart, as after +/// the host gave the inode number of an object that the guest holds to another +/// object of its type. +fn nodes_of_one_object(first: u64, second: u64) -> anyhow::Error { + anyhow::anyhow!( + "nodes {} and {} name one host object, as after the host replaced an object that \ + the guest holds by one with its inode number; the guest must release one of them \ + before a save", + first.min(second), + first.max(second) + ) +} + impl VirtioFs { /// Creates a filesystem attachment for the fixed microVM profile. /// @@ -520,6 +534,10 @@ impl VirtioFs { let mut node_ids = HashMap::with_capacity(inodes.inodes_by_node_id.values.len()); let mut alias_count = 0usize; let mut alias_bytes = 0usize; + // A restore keys the nodes by their names and by their objects, so + // no two nodes may share either. + let mut alias_nodes = HashMap::new(); + let mut object_nodes = HashMap::new(); for (&node_id, inode) in &inodes.inodes_by_node_id.values { let aliases = inode.aliases(); anyhow::ensure!( @@ -551,6 +569,11 @@ impl VirtioFs { inode.guest_inode_nr() ) })?; + if let Some(other) = + alias_nodes.insert((inode.volume_id(), alias.clone()), node_id) + { + return Err(nodes_of_one_object(other, node_id)); + } let alias = encode_relative_path(&alias)?; alias_bytes = alias_bytes .checked_add(alias.len()) @@ -561,6 +584,9 @@ impl VirtioFs { ); relative_aliases.push(alias); } + if let Some(other) = object_nodes.insert(inode.dedup_key(), node_id) { + return Err(nodes_of_one_object(other, node_id)); + } saved.push(SavedInode { node_id, volume_id: inode.volume_id(), @@ -593,6 +619,15 @@ impl VirtioFs { .object_stat() .map_err(anyhow::Error::from) .context("open handle cannot be revalidated for save")?; + // A restore reopens the handle by its node's names, which reach + // the node's own object. + validate_identity(&stat, &file.inode().object_identity()).with_context(|| { + format!( + "handle {handle_id} of node {node_id} reaches another host object than \ + the node, as after the guest opened the node while the host had \ + replaced its object; the guest must close the handle before a save" + ) + })?; let snapshot_entries = file.directory_entries(); let directory_snapshot_built = file.directory_snapshot_built(); anyhow::ensure!( diff --git a/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs b/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs index 23fb68f1225..0e4fb1e2f3e 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs @@ -1400,6 +1400,83 @@ fn a_held_file_that_the_host_replaces_fails_the_save_until_released() { } } +/// A host can give the inode number of a removed object to a new object of +/// another file type, and then to an object of the removed object's type +/// again, while the guest holds a node of each: each gets a node of its own +/// (see `InodeMap::dedup_entry`). Once the guest forgets the second, the nodes +/// of the first and the last object both name an object of that inode number +/// and type, by one name or by two names of it, which a restore cannot tell +/// apart, so a save must fail rather than save a snapshot that cannot restore. +#[test] +fn nodes_that_name_one_object_fail_the_save() { + for path in ["file", "link"] { + let temporary_directory = tempdir().unwrap(); + let root_path = temporary_directory.path(); + let (fs, file, node_id) = microvm_with_a_tracked_file(root_path); + std::fs::hard_link(root_path.join("file"), root_path.join("link")).unwrap(); + let (_, directory) = fs + .insert_inode(directory_with_the_inode_number_of(&file, "file")) + .unwrap(); + let stat = file.volume().lstat(file.clone_path()).unwrap(); + let (_, other) = fs + .insert_inode(VirtioFsInode::with_attr( + file.volume(), + PathBuf::from(path), + &stat, + )) + .unwrap(); + assert_ne!(other, node_id, "{path}"); + fs.forget(directory, 1); + + let Err(error) = fs.save_microvm_state(&profile(root_path), SessionState::default()) else { + panic!("{path}: a save succeeded with two nodes of one object"); + }; + let message = format!("{error:#}"); + assert!( + message.contains(&format!("nodes {node_id} and {other} name one host object")), + "{path}: {message}" + ); + fs.forget(other, 1); + fs.save_microvm_state(&profile(root_path), SessionState::default()) + .unwrap(); + } +} + +/// The guest can open a node after the host has replaced its object, as Linux +/// does when a program reopens a file that it holds through `/proc/self/fd`, +/// and the host can then give the node's name its object back. The node's +/// names then reach its object, but its handle reaches the replacement, which +/// a restore, which reopens each handle by its node's names, cannot reach, so +/// a save must fail rather than save a snapshot that cannot restore. +#[test] +fn a_handle_of_another_object_than_its_node_fails_the_save() { + let temporary_directory = tempdir().unwrap(); + let root_path = temporary_directory.path(); + std::fs::write(root_path.join("file"), b"old").unwrap(); + let profile = profile(root_path); + let fs = VirtioFs::new_microvm(root_path, profile.clone()).unwrap(); + let node = lookup(&fs, "file").unwrap(); + std::fs::rename(root_path.join("file"), root_path.join("kept")).unwrap(); + std::fs::write(root_path.join("file"), b"new").unwrap(); + let fh = open(&fs, node, lx::O_RDONLY).unwrap(); + std::fs::rename(root_path.join("file"), root_path.join("replacement")).unwrap(); + std::fs::rename(root_path.join("kept"), root_path.join("file")).unwrap(); + + let Err(error) = fs.save_microvm_state(&profile, SessionState::default()) else { + panic!("a save succeeded with a handle of another object than its node"); + }; + let message = format!("{error:#}"); + assert!( + message.contains(&format!( + "handle {fh} of node {node} reaches another host object" + )), + "{message}" + ); + release(&fs, node, fh); + fs.save_microvm_state(&profile, SessionState::default()) + .unwrap(); +} + /// Objects with one inode number on two devices, as `/proc` and `/sys` are, /// save as two inodes and restore as two. #[cfg(target_os = "linux")] From 5e0e72d72d60bce5e2c5735a08ffebd57ae24216 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 23 Nov 2026 15:18:13 -0800 Subject: [PATCH 7/7] virtiofs: say which nodes a microVM save keeps apart The comments of the microVM save's checks of its nodes said that a restore keys the nodes by their names and objects, so that no two nodes may share either, and that two nodes name one object after the host gives the inode number of an object that the guest holds to another object of its type. A restore, and so the save, keeps apart only the nodes of one volume, since an aggregate gives each child a node of its own for a host object that both children reach, as through a hard link. And a host that gives a held object's inode number to another object of its type gives the guest the held object's node for it, since a lookup reuses that node; two nodes of one object arise only when an object of another type took the number in between, as the tests of the check show. The comments now say so. The code, its errors, and its tests are unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- vm/devices/virtio/virtiofs/src/microvm/fs.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/vm/devices/virtio/virtiofs/src/microvm/fs.rs b/vm/devices/virtio/virtiofs/src/microvm/fs.rs index d9940904f23..700a319f5b5 100644 --- a/vm/devices/virtio/virtiofs/src/microvm/fs.rs +++ b/vm/devices/virtio/virtiofs/src/microvm/fs.rs @@ -292,10 +292,11 @@ fn open_microvm_volume( Ok((volume, root_inode, root_stat)) } -/// Returns the error of a save whose nodes `first` and `second` name one host -/// object, by one name or by two, which a restore cannot tell apart, as after -/// the host gave the inode number of an object that the guest holds to another -/// object of its type. +/// Returns the error of a save whose nodes `first` and `second` share a name or +/// the dedup key of one host object in one volume, which a restore cannot tell +/// apart, as after the host gave the inode number of an object that the guest +/// holds to an object of another type, and then to one of the held object's +/// type again, while the guest looked up each. fn nodes_of_one_object(first: u64, second: u64) -> anyhow::Error { anyhow::anyhow!( "nodes {} and {} name one host object, as after the host replaced an object that \ @@ -534,8 +535,8 @@ impl VirtioFs { let mut node_ids = HashMap::with_capacity(inodes.inodes_by_node_id.values.len()); let mut alias_count = 0usize; let mut alias_bytes = 0usize; - // A restore keys the nodes by their names and by their objects, so - // no two nodes may share either. + // A restore keys the nodes of each volume by their names and by + // their dedup keys, so no two nodes of one volume may share either. let mut alias_nodes = HashMap::new(); let mut object_nodes = HashMap::new(); for (&node_id, inode) in &inodes.inodes_by_node_id.values {