Skip to content
Merged
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
2 changes: 1 addition & 1 deletion vm/devices/virtio/virtiofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ impl InodeReservation<'_> {
fn insert(mut self, inode: VirtioFsInode) -> lx::Result<(Arc<VirtioFsInode>, u64)> {
let mut inodes = self.inodes.write();
if self.microvm {
inodes.preflight_microvm_insert(&inode)?;
inodes.preflight_microvm_reserved_insert(&inode)?;
}
let result = inodes.insert_reserved(&self.reservation, inode)?;
self.committed = true;
Expand Down
81 changes: 81 additions & 0 deletions vm/devices/virtio/virtiofs/src/microvm/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

//! Tests for microVM filesystem persistence and limits.

use super::policy_tests::create;
use super::policy_tests::lookup;
use super::policy_tests::mkdir;
use super::policy_tests::name;
use super::policy_tests::open;
use super::policy_tests::read_node;
Expand Down Expand Up @@ -46,6 +48,7 @@ use fuse::protocol::fuse_dirent_align;
use fuse::protocol::fuse_direntplus;
use fuse::protocol::fuse_in_header;
use fuse::protocol::fuse_init_in;
use fuse::protocol::fuse_mknod_in;
use fuse::protocol::fuse_read_in;
use fuse::protocol::fuse_release_in;
use std::path::Path;
Expand Down Expand Up @@ -175,6 +178,84 @@ fn largest_restored_next_node_id_is_never_handed_out() {
.unwrap();
}

/// A saved state may also hold the last node ID that the node table hands
/// out, `u64::MAX - 1`. `MKDIR`, `MKNOD`, `SYMLINK`, and `CREATE` reserve the
/// node ID of the object that they make before they make it on the host, so
/// the object takes that ID even though the table can hand out no other: the
/// reply returns the object's node rather than fail and leave the object on
/// the host without one. The next request that makes an object then fails
/// with `ENOSPC` before it changes the host, and the state saves the node and
/// the next node ID `u64::MAX`, which a restore accepts.
#[test]
fn a_created_object_takes_the_last_node_id() {
for operation in ["MKDIR", "MKNOD", "SYMLINK", "CREATE"] {
let temporary_directory = tempdir().unwrap();
let root_path = temporary_directory.path();
let profile = MicroVmVirtioFsProfile::from_attachment(
MICROVM_ATTACHMENT_ID.to_owned(),
microvm_root_identity(root_path).unwrap(),
false,
Vec::new(),
)
.unwrap();
let source = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
let mut state = source
.save_microvm_state(&profile, SessionState::default())
.unwrap();
state.next_node_id = u64::MAX - 1;
let fs = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
let session = Session::new(fs.clone());
fs.restore_microvm_state(&profile, state, &session).unwrap();

let make = |object: &str| -> lx::Result<u64> {
let parent = request(FUSE_ROOT_ID);
match operation {
"MKDIR" => mkdir(&fs, FUSE_ROOT_ID, object),
"MKNOD" => {
let arg = fuse_mknod_in {
mode: lx::S_IFREG | 0o644,
rdev: 0,
umask: 0,
padding: 0,
};
Ok(fs.mknod(&parent, name(object), &arg)?.nodeid)
}
"SYMLINK" => Ok(fs.symlink(&parent, name(object), name("target"))?.nodeid),
"CREATE" => {
let (node_id, fh) = create(&fs, FUSE_ROOT_ID, object)?;
release(&fs, node_id, fh);
Ok(node_id)
}
_ => unreachable!(),
}
};
let on_host = |object: &str| root_path.join(object).symlink_metadata().is_ok();

assert_eq!(make("first"), Ok(u64::MAX - 1), "{operation}");
assert!(on_host("first"), "{operation}");
assert_eq!(lookup(&fs, "first"), Ok(u64::MAX - 1), "{operation}");
assert_eq!(make("second"), Err(lx::Error::ENOSPC), "{operation}");
assert!(!on_host("second"), "{operation}");

let state = fs
.save_microvm_state(&profile, SessionState::default())
.unwrap();
assert_eq!(state.next_node_id, u64::MAX, "{operation}");
assert!(
state
.inodes
.iter()
.any(|inode| inode.node_id == u64::MAX - 1),
"{operation}"
);
let restored = VirtioFs::new_microvm(root_path, profile.clone()).unwrap();
let session = Session::new(restored.clone());
restored
.restore_microvm_state(&profile, state, &session)
.unwrap();
}
}

/// A reset or `FUSE_DESTROY` ends a restored session as it ends a new one, so
/// a remounting guest gets nodes from 2 and handles from 1 again, even after a
/// restore whose tables were full.
Expand Down
39 changes: 37 additions & 2 deletions vm/devices/virtio/virtiofs/src/microvm/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,54 @@ impl InodeMap {
}

pub(crate) fn preflight_microvm_insert(&self, inode: &VirtioFsInode) -> lx::Result<()> {
self.preflight_microvm_insert_with(inode, false)
}

/// Checks an insert of `inode` with the node ID that a reservation holds
/// (see [`InodeMap::insert_reserved`]).
///
/// Unlike [`Self::preflight_microvm_insert`], this does not check that the
/// table can hand out another node ID, since the insert takes none: a
/// request that reserved the last node ID, `u64::MAX - 1`, must still be
/// able to insert the node of the object that it has made on the host,
/// rather than fail and leave that object there without a node. The
/// inode and alias limits apply as to any insert.
pub(crate) fn preflight_microvm_reserved_insert(
&self,
inode: &VirtioFsInode,
) -> lx::Result<()> {
self.preflight_microvm_insert_with(inode, true)
}

/// Checks an insert of `inode`, whose node, if it adds one, takes the node
/// ID that a reservation holds if `reserved`, and otherwise the table's
/// next node ID.
fn preflight_microvm_insert_with(
&self,
inode: &VirtioFsInode,
reserved: bool,
) -> lx::Result<()> {
let path = inode.clone_path();
Self::validate_microvm_alias_path(&path)?;
// The insert aliases the inode under the dedup key only if it reuses
// that inode, and otherwise adds a node (see `Self::dedup_entry`).
if let Some(existing) = self.reused(inode) {
return self.preflight_microvm_alias_add(existing, &path);
}
self.preflight_microvm_new_inode(&path)
self.preflight_microvm_node(&path, reserved)
}

pub(crate) fn preflight_microvm_new_inode(&self, path: &Path) -> lx::Result<()> {
self.preflight_microvm_node(path, false)
}

/// Checks a new node at `path` against the inode and alias limits and,
/// unless a reservation holds its node ID (`reserved`), checks that the
/// table can hand out its next node ID.
fn preflight_microvm_node(&self, path: &Path, reserved: bool) -> lx::Result<()> {
Self::validate_microvm_alias_path(path)?;
if self.inodes_by_node_id.values.len() >= MAX_INODES || !self.inodes_by_node_id.can_insert()
if self.inodes_by_node_id.values.len() >= MAX_INODES
|| (!reserved && !self.inodes_by_node_id.can_insert())
{
return Err(lx::Error::ENOSPC);
}
Expand Down
Loading