Skip to content

Commit 2a483a0

Browse files
ppennaCopilot
andcommitted
virtiofs: keep node names when renaming one hard link onto another
A guest rename of a hard link onto another link of the same object succeeds on a Linux host without changing anything, as POSIX requires, but HostFs dropped the source name from the object's node (microsoft/nvx#1620). Once the guest removed the other link, the node had no names left although the source still reached its object, so requests on the node failed with ENOENT, and a microVM save failed with "inode has no bounded reopenable aliases". 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 links of one object, renameat2 replaces and moves nothing, but the node keeps only the destination's name. After a successful rename on a POSIX host, HostFs now checks whether the source still reaches the destination's object, and then changes no names. It decides from the host's result, not from the two names before the rename: through overlayfs, two hard links in a lower layer report one inode number, but the rename still moves one onto the other. A Windows host does move the name: NTFS removes the source link, which the move's bookkeeping already records, and a rename that changes only the case of a name leaves the old spelling reaching the object, so the check stays off there. The rename still passes the move's alias-limit preflight, and the names that it keeps already fit the limits. renaming_a_hard_link_onto_another_link_keeps_the_remaining_names, in virtiofs' microVM rename tests, renames a hard link onto another link of its object in one directory and across two, after checking that the host still fails such a rename under RENAME_NOREPLACE, with EEXIST on Linux and EINVAL on Windows. On Linux, the node keeps all three names, a snapshot taken right away saves and restores them, and the node still reads the object through the source once the guest removes the other links. On Windows, the host keeps only the destination, and so does the node. Without the fix, on top of the fix for microsoft/nvx#1610 at feature/microvm/dev b51f915, it fails on Linux (nvx-kvm-test, Azure Linux 3, kernel 6.18), where the node keeps only the destination, and passes on Windows; with it, it and the package's other tests pass on both. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1270a97 commit 2a483a0

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ impl VirtioFsVolume {
147147
self.root_device_nr
148148
}
149149

150+
/// Returns whether `first` and `second` both name one host object, such as
151+
/// two hard links of a file. A volume without stable file IDs cannot tell.
152+
pub(crate) fn names_one_object(&self, first: &Path, second: &Path) -> bool {
153+
let identity = |path: &Path| {
154+
self.lstat(path)
155+
.ok()
156+
.map(|stat| (stat.device_nr, stat.inode_nr))
157+
};
158+
self.supports_stable_file_id()
159+
&& identity(first).is_some_and(|first| identity(second) == Some(first))
160+
}
161+
150162
/// Maps the host device and inode number of one of the volume's objects to
151163
/// the inode number that the guest sees.
152164
///

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,14 @@ impl Fuse for VirtioFs {
552552
let exchange = flags & RENAME_EXCHANGE != 0;
553553
self.preflight_rename_aliases(inode.volume_id(), &old_path, &new_path, exchange)?;
554554
inode.rename(name, &new_inode, new_name, flags)?;
555+
// A POSIX host renames nothing between two hard links of one object,
556+
// so if the source still reaches the destination's object afterwards,
557+
// every node keeps its names. Windows moves the name instead: it
558+
// removes the source link, or changes only the case of a name, whose
559+
// old spelling may still reach the object.
560+
if cfg!(unix) && inode.volume.names_one_object(&old_path, &new_path) {
561+
return Ok(());
562+
}
555563
let mut inodes = self.inner.inodes.write();
556564
if exchange {
557565
inodes.exchange_alias_prefixes(inode.volume_id(), &old_path, &new_path);

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
//! Tests for renames on microVM shares that do not move one name to another:
55
//! `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.
6+
//! itself, or on a POSIX host onto another hard link of its object, which
7+
//! changes none. They check the names that HostFs keeps for each node, the
8+
//! alias limits, and snapshots taken right after the rename.
89
910
use super::policy_tests::error;
1011
use super::policy_tests::list;
@@ -399,3 +400,65 @@ fn renaming_a_name_onto_itself_keeps_every_name() {
399400
assert_names(&fs, first, &["first"]);
400401
assert_eq!(read_node(&fs, first), b"first");
401402
}
403+
404+
/// A rename of a hard link onto another link of its object, in one directory
405+
/// or across two, changes nothing on a POSIX host, so the object's node keeps
406+
/// every name, and a snapshot taken right after it saves and restores them. A
407+
/// Windows host removes the source link instead, and the node keeps the names
408+
/// that remain. Once the guest removes all of them but one, the node still
409+
/// reaches the object through that one. The host still fails such a rename
410+
/// under `RENAME_NOREPLACE`.
411+
#[test]
412+
fn renaming_a_hard_link_onto_another_link_keeps_the_remaining_names() {
413+
let share = Share::new(&[("directory/first", "data")]);
414+
for link in ["second", "third"] {
415+
std::fs::hard_link(share.path("directory/first"), share.path(link)).unwrap();
416+
}
417+
let fs = share.fs();
418+
let directory = lookup(&fs, "directory").unwrap();
419+
let node = lookup(&fs, "directory/first").unwrap();
420+
for link in ["second", "third"] {
421+
assert_eq!(lookup(&fs, link).unwrap(), node);
422+
}
423+
let rename = |parent, entry, flags| {
424+
fs.rename(
425+
&request(parent),
426+
name(entry),
427+
FUSE_ROOT_ID,
428+
name("third"),
429+
flags,
430+
)
431+
};
432+
433+
// Windows hosts reject every rename flag.
434+
assert_eq!(
435+
error(rename(FUSE_ROOT_ID, "second", RENAME_NOREPLACE)),
436+
if cfg!(windows) {
437+
lx::Error::EINVAL
438+
} else {
439+
lx::Error::EEXIST
440+
}
441+
);
442+
rename(FUSE_ROOT_ID, "second", 0).unwrap();
443+
rename(directory, "first", 0).unwrap();
444+
let names: &[&str] = if cfg!(windows) {
445+
&["third"]
446+
} else {
447+
&["directory/first", "second", "third"]
448+
};
449+
for path in ["directory/first", "second", "third"] {
450+
assert_eq!(share.path(path).exists(), names.contains(&path), "{path}");
451+
}
452+
assert_names(&fs, node, names);
453+
454+
let state = share.save(&fs);
455+
assert_eq!(saved_names(&state, node), path_set(names));
456+
assert_names(&share.restore(state), node, names);
457+
458+
let (kept, others) = names.split_first().unwrap();
459+
for link in others {
460+
fs.unlink(&request(FUSE_ROOT_ID), name(link)).unwrap();
461+
}
462+
assert_names(&fs, node, &[kept]);
463+
assert_eq!(read_node(&fs, node), b"data");
464+
}

0 commit comments

Comments
 (0)