Skip to content

Commit 21b1a55

Browse files
ppennaCopilot
andcommitted
virtiofs: accept FSYNCDIR through the synthetic root's handle
fsync and fdatasync of a descriptor of an aggregate's synthetic root, as `sync` calls for a directory that it is given, failed with EBADF, which POSIX reserves for a descriptor that is not open (microsoft/nvx#1624). The Linux FUSE client sends both as FUSE_FSYNCDIR, with the node of the directory and the handle that FUSE_OPENDIR returned. FUSE_OPENDIR of the synthetic root returns SYNTHETIC_ROOT_FH, which names no open file, and READDIR, READDIRPLUS, and RELEASEDIR, and GETATTR and STATX through a handle, recognize it with the root's node before they look a handle up. VirtioFs::fsync_dir looked it up in the handle map, found nothing, and failed with EBADF. fsync_dir now succeeds for the synthetic root's handle with the root's node, as release_dir does, before it looks a handle up: the root keeps its entries only in memory, so it has nothing to write back. ENOSYS would not do, since the Linux client takes it for the whole mount and stops syncing the children's directories too. Any other handle or node reaches get_file as before. aggregate::tests::synthetic_root_handle_accepts_fsyncdir checks that FUSE_FSYNCDIR through an aggregate root's handle succeeds with and without FUSE_FSYNC_FDATASYNC, and that the handle with a child's node, and another handle with the root's node, still fail with EBADF. At its parent, 655bc31, the change for microsoft/nvx#1606 on the stack of changes above feature/microvm/dev 3288834, the test fails with EBADF on Windows and on a Linux KVM host; with the fix, it and the rest of cargo nextest -p virtiofs pass on both. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 655bc31 commit 21b1a55

3 files changed

Lines changed: 88 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use std::sync::Arc;
2525
use zerocopy::FromZeros;
2626

2727
/// Reserved file handle `open_dir` returns for the synthetic aggregate root;
28-
/// `read_dir`/`read_dir_plus`/`release_dir` recognize it and serve the root
29-
/// registry instead of the handle map. `u64::MAX` can't collide with a real
28+
/// `read_dir`/`read_dir_plus`/`fsync_dir`/`release_dir` recognize it and serve
29+
/// the root instead of the handle map. `u64::MAX` can't collide with a real
3030
/// handle, since `HandleMap` allocates from 1 upward.
3131
pub(crate) const SYNTHETIC_ROOT_FH: u64 = u64::MAX;
3232

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,37 @@ use crate::inode;
77
use crate::inode::VirtioFsVolume;
88
use crate::test_helpers::decode_dir_entries;
99
use fuse::Fuse;
10+
use fuse::protocol::FUSE_FSYNC_FDATASYNC;
11+
use fuse::protocol::FUSE_FSYNCDIR;
12+
use fuse::protocol::FUSE_LOOKUP;
13+
use fuse::protocol::FUSE_OPENDIR;
1014
use fuse::protocol::FUSE_ROOT_ID;
1115
use fuse::protocol::fuse_dirent;
1216
use fuse::protocol::fuse_dirent_align;
1317
use fuse::protocol::fuse_direntplus;
18+
use fuse::protocol::fuse_fsync_in;
19+
use fuse::protocol::fuse_in_header;
20+
use fuse::protocol::fuse_open_in;
1421
use lxutil::LxVolumeOptions;
1522
use std::sync::Arc;
1623
use test_with_tracing::test;
1724
use zerocopy::FromBytes;
25+
use zerocopy::FromZeros;
26+
use zerocopy::IntoBytes;
27+
28+
/// A guest request with `opcode` about the node `node_id`, whose argument is
29+
/// `arg`.
30+
fn request(opcode: u32, node_id: u64, arg: &[u8]) -> fuse::Request {
31+
let header = fuse_in_header {
32+
len: (size_of::<fuse_in_header>() + arg.len()) as u32,
33+
opcode,
34+
nodeid: node_id,
35+
..fuse_in_header::new_zeroed()
36+
};
37+
let mut bytes = header.as_bytes().to_vec();
38+
bytes.extend_from_slice(arg);
39+
fuse::Request::new(bytes.as_slice()).unwrap()
40+
}
1841

1942
fn assert_config_errno(error: crate::CreateError, expected: lx::Error) {
2043
match error {
@@ -112,6 +135,63 @@ fn aggregate_operations_are_scoped_to_aggregate_mode() {
112135
assert_eq!(fs.remove_child("x").unwrap_err(), lx::Error::EINVAL);
113136
}
114137

138+
/// `fsync` and `fdatasync` of a descriptor of the synthetic root, which the
139+
/// guest sends as `FUSE_FSYNCDIR` with the root's node and the handle that
140+
/// `FUSE_OPENDIR` returned, succeed: the root keeps its entries only in
141+
/// memory, so it has nothing to write back. They must not fail with `EBADF`,
142+
/// as if the descriptor were not open (microsoft/nvx#1624). The handle is
143+
/// still the root's alone, and the root has no other.
144+
#[test]
145+
fn synthetic_root_handle_accepts_fsyncdir() {
146+
let share = tempfile::tempdir().unwrap();
147+
let fs = VirtioFs::new_aggregate();
148+
fs.add_child("share", share.path(), None).unwrap();
149+
let open = fuse_open_in {
150+
flags: lx::O_RDONLY as u32,
151+
..fuse_open_in::new_zeroed()
152+
};
153+
let fh = fs
154+
.open_dir(
155+
&request(FUSE_OPENDIR, FUSE_ROOT_ID, open.as_bytes()),
156+
open.flags,
157+
)
158+
.unwrap()
159+
.fh;
160+
let fsync_dir = |node_id, handle, fsync_flags| {
161+
let arg = fuse_fsync_in {
162+
fh: handle,
163+
fsync_flags,
164+
..fuse_fsync_in::new_zeroed()
165+
};
166+
fs.fsync_dir(
167+
&request(FUSE_FSYNCDIR, node_id, arg.as_bytes()),
168+
arg.fh,
169+
arg.fsync_flags,
170+
)
171+
};
172+
173+
for flags in [0, FUSE_FSYNC_FDATASYNC] {
174+
assert_eq!(
175+
fsync_dir(FUSE_ROOT_ID, fh, flags),
176+
Ok(()),
177+
"flags {flags:#x}"
178+
);
179+
}
180+
181+
// The guest has opened no other handle, so the root's handle with the
182+
// node of a child, and any other handle with the root's node, name no
183+
// open directory.
184+
let child = fs
185+
.lookup(
186+
&request(FUSE_LOOKUP, FUSE_ROOT_ID, b"share\0"),
187+
"share".as_ref(),
188+
)
189+
.unwrap()
190+
.nodeid;
191+
assert_eq!(fsync_dir(child, fh, 0), Err(lx::Error::EBADF));
192+
assert_eq!(fsync_dir(FUSE_ROOT_ID, fh ^ 1, 0), Err(lx::Error::EBADF));
193+
}
194+
115195
#[test]
116196
fn add_child_validates_name() {
117197
let root = tempfile::tempdir().unwrap();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl Fuse for VirtioFs {
479479
if self.is_synthetic_root(request.node_id()) {
480480
util::open_flags(flags)?;
481481
// The synthetic root has no backing handle; hand out a sentinel that
482-
// read_dir/read_dir_plus/release_dir recognize.
482+
// read_dir/read_dir_plus/fsync_dir/release_dir recognize.
483483
return Ok(fuse_open_out::new(SYNTHETIC_ROOT_FH, 0));
484484
}
485485
// There is no special handling for directories, so just call open.
@@ -585,6 +585,11 @@ impl Fuse for VirtioFs {
585585
}
586586

587587
fn fsync_dir(&self, request: &Request, fh: u64, flags: u32) -> lx::Result<()> {
588+
// The synthetic root keeps its entries only in memory, so it has
589+
// nothing to write back.
590+
if self.is_synthetic_root_handle(request.node_id(), fh) {
591+
return Ok(());
592+
}
588593
let file = self.get_file(request.node_id(), fh)?;
589594
let data_only = flags & FUSE_FSYNC_FDATASYNC != 0;
590595
file.fsync_dir(data_only)

0 commit comments

Comments
 (0)