Skip to content

Commit 3288834

Browse files
ppennaCopilot
andcommitted
virtiofs: keep node names when renaming a name onto itself
A guest rename of a name onto itself, in the same directory, succeeds on the host without changing anything, as POSIX requires, but it dropped the names at and below that name from every node of the volume (microsoft/nvx#1610). A node that the guest knew only through them was left with no names, so a microVM save then failed with "inode has no bounded reopenable aliases", and a volume that deduplicates nodes by path stopped finding it. A node with another hard link kept only the other name, and once the guest removed that one, the node's requests failed with ENOENT although the renamed name still reached its object. VirtioFs::rename tracks a rename that is not an exchange as a move: it drops the names at and below the destination, which the host replaced, and then rewrites those at and below the source under the destination. When both are one name, the first step drops the names that the second should keep. The microVM alias preflight models the same steps. A rename whose source and destination are one name now changes no names and skips the preflight. It still reaches the host after the same writability checks, so the host fails it as it fails any other rename: with ENOENT for a missing name, and with EEXIST under RENAME_NOREPLACE on Linux. renaming_a_name_onto_itself_keeps_every_name, in virtiofs' microVM exchange tests, which now cover a rename of a name onto itself too, renames a directory with nodes below it, a file in it, and a file with a second hard link each onto itself. It checks that every node keeps exactly its names, that a snapshot taken right away saves and restores them, and that the hard-linked file's node still reads the file once the guest removes the other link; and that the host still fails a missing name with ENOENT, and RENAME_NOREPLACE with EEXIST on Linux and EINVAL on Windows. Without the fix, at feature/microvm/dev b51f915, it fails on Windows and on Linux (Azure Linux 3.0, nvx-mshv-test), where the renamed directory's node has no names left; with the fix, it and the package's other tests pass there. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b51f915 commit 3288834

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎vm/devices/virtio/virtiofs/src/lib.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,14 @@ impl Fuse for VirtioFs {
533533
let new_path = new_inode.child_path(new_name)?;
534534
self.check_writable_entry(&inode, &old_path)?;
535535
self.check_writable_entry(&new_inode, &new_path)?;
536+
// Renaming a name onto itself changes nothing on the host, which still
537+
// fails it as it fails any other rename, such as with ENOENT for a
538+
// missing name. Every node keeps its names: the bookkeeping of a move
539+
// below would drop those at and below the name, since it first drops
540+
// the destination's.
541+
if old_path == new_path {
542+
return inode.rename(name, &new_inode, new_name, flags);
543+
}
536544
let exchange = flags & RENAME_EXCHANGE != 0;
537545
self.preflight_rename_aliases(inode.volume_id(), &old_path, &new_path, exchange)?;
538546
inode.rename(name, &new_inode, new_name, flags)?;

‎vm/devices/virtio/virtiofs/src/microvm/exchange_tests.rs‎

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
//! Tests for `RENAME_EXCHANGE` on microVM shares: the names that HostFs keeps
5-
//! for each node, the alias limits, and snapshots taken right after an
6-
//! exchange.
4+
//! Tests for renames on microVM shares that do not move one name to another:
5+
//! `RENAME_EXCHANGE`, which swaps two names, and a rename of a name onto
6+
//! itself, which changes none. They check the names that HostFs keeps for each
7+
//! node, the alias limits, and snapshots taken right after the rename.
78
89
use super::policy_tests::error;
910
use super::policy_tests::list;
@@ -34,6 +35,10 @@ use tempfile::TempDir;
3435
use tempfile::tempdir;
3536
use test_with_tracing::test;
3637

38+
/// Linux's `RENAME_NOREPLACE` flag for `renameat2`, which fails a rename onto
39+
/// an existing name.
40+
const RENAME_NOREPLACE: u32 = 1;
41+
3742
/// A read-write microVM share without an access policy.
3843
struct Share {
3944
_directory: TempDir,
@@ -318,3 +323,79 @@ fn exchange_checks_the_alias_limits_of_both_names() {
318323
.is_none()
319324
);
320325
}
326+
327+
/// A rename of a name onto itself changes nothing on the host, so every node
328+
/// at or below the name keeps all of its names, and a snapshot taken right
329+
/// after it saves and restores them. A node whose object has another hard link
330+
/// still reaches the object through the renamed name once the guest removes
331+
/// the link. The host still fails such a rename as it fails any other.
332+
#[test]
333+
fn renaming_a_name_onto_itself_keeps_every_name() {
334+
let share = Share::new(&[
335+
("directory/file", "file"),
336+
("directory/nested/leaf", "leaf"),
337+
("first", "first"),
338+
]);
339+
std::fs::hard_link(share.path("first"), share.path("second")).unwrap();
340+
let fs = share.fs();
341+
let directory = lookup(&fs, "directory").unwrap();
342+
let first = lookup(&fs, "first").unwrap();
343+
assert_eq!(lookup(&fs, "second").unwrap(), first);
344+
let nodes: [(u64, &[&str]); 5] = [
345+
(directory, &["directory"]),
346+
(lookup(&fs, "directory/file").unwrap(), &["directory/file"]),
347+
(
348+
lookup(&fs, "directory/nested").unwrap(),
349+
&["directory/nested"],
350+
),
351+
(
352+
lookup(&fs, "directory/nested/leaf").unwrap(),
353+
&["directory/nested/leaf"],
354+
),
355+
(first, &["first", "second"]),
356+
];
357+
358+
for (parent, entry) in [
359+
(FUSE_ROOT_ID, "directory"),
360+
(directory, "file"),
361+
(FUSE_ROOT_ID, "first"),
362+
] {
363+
fs.rename(&request(parent), name(entry), parent, name(entry), 0)
364+
.unwrap();
365+
}
366+
let rename_onto_itself = |entry, flags| {
367+
error(fs.rename(
368+
&request(FUSE_ROOT_ID),
369+
name(entry),
370+
FUSE_ROOT_ID,
371+
name(entry),
372+
flags,
373+
))
374+
};
375+
assert_eq!(rename_onto_itself("missing", 0), lx::Error::ENOENT);
376+
// Windows hosts reject every rename flag.
377+
assert_eq!(
378+
rename_onto_itself("first", RENAME_NOREPLACE),
379+
if cfg!(windows) {
380+
lx::Error::EINVAL
381+
} else {
382+
lx::Error::EEXIST
383+
}
384+
);
385+
let check = |fs: &VirtioFs| {
386+
for (node, names) in nodes {
387+
assert_names(fs, node, names);
388+
}
389+
};
390+
check(&fs);
391+
392+
let state = share.save(&fs);
393+
for (node, names) in nodes {
394+
assert_eq!(saved_names(&state, node), path_set(names));
395+
}
396+
check(&share.restore(state));
397+
398+
fs.unlink(&request(FUSE_ROOT_ID), name("second")).unwrap();
399+
assert_names(&fs, first, &["first"]);
400+
assert_eq!(read_node(&fs, first), b"first");
401+
}

0 commit comments

Comments
 (0)