Skip to content

Commit b73db66

Browse files
ppennaCopilot
andcommitted
fuse: adopt a FUSE_INIT negotiation only once its reply is sent
Session::dispatch_init negotiated in the session's own SessionInfo, under its write lock, and marked the session initialized only after it sent the reply (microsoft/nvx#1898). When the reply could not be sent, as when a virtio-fs client's buffers cannot hold it, the session stayed uninitialized but kept the values of the attempt that the client never received. save_state then saved them with initialized false, which SessionState::validate refuses as UninitializedWithInfo, so such a save could not be restored, and a dormant microVM virtio-fs slot refused to save at all. A later FUSE_INIT reset only the fields that dispatch_init sets before Fuse::init, so the failed attempt's max_background and congestion_threshold reached the file system's init, the reply, and the saved state of the next negotiation. dispatch_init now negotiates in a SessionInfo of the request's own, built from the defaults and the client's offer alone, and stores it in the session, and marks the session initialized, only once the reply is sent. A reply that cannot be sent, like a negotiation that the session refuses, leaves the session as it was: uninitialized, without a negotiation. A FUSE_INIT whose reply is sent negotiates, replies, and saves as before. Fuse::init documents it. Tests, in fuse's session tests of init, with a sender that fails each reply with WriteZero, and a file system that records what each init receives and allows more background requests when the client offers FUSE_ASYNC_DIO: - init_whose_reply_fails_leaves_no_negotiation fails at feature/microvm/dev 899dcd6: the uninitialized session saves the failed attempt's version 7.39, capabilities, and background limits. - init_after_a_failed_reply_negotiates_afresh, whose second FUSE_INIT offers only FUSE_ASYNC_READ at 7.31, fails there: that reply carries the failed attempt's background limits, (64, 48) instead of (0, 0). With the fix, its reply, what init receives, and the saved state match those of a new session. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 899dcd6 commit b73db66

3 files changed

Lines changed: 115 additions & 14 deletions

File tree

‎vm/devices/support/fs/fuse/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ pub trait Fuse {
263263
/// drops `FUSE_INIT_EXT`, which discards `info.want2`. Otherwise, the session fails
264264
/// `FUSE_INIT` with `EIO` and stays uninitialized. The session keeps the client's version and
265265
/// capabilities, even if `init` replaces `info`.
266+
///
267+
/// Each `FUSE_INIT` negotiates afresh, from the session's defaults and the client's offer, and
268+
/// the session adopts the negotiation only once it has sent its reply: when the reply cannot
269+
/// be sent, the session stays uninitialized, without a negotiation.
266270
fn init(&self, _info: &mut SessionInfo) {}
267271

268272
/// Opens a directory.

‎vm/devices/support/fs/fuse/src/session.rs‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ impl Session {
442442
return Err(lx::Error::EIO.into());
443443
};
444444

445-
let mut info = self.info.write();
445+
// The lock serializes racing FUSE_INIT requests.
446+
let mut session_info = self.info.write();
446447
if self.is_initialized() {
447448
tracelimit::error_ratelimited!("Racy FUSE_INIT requests.");
448449
return Err(lx::Error::EIO.into());
@@ -470,28 +471,31 @@ impl Session {
470471
return Err(lx::Error::EIO.into());
471472
}
472473

473-
// Prepare the session info and call the file system to negotiate.
474-
info.major = init.major;
475-
info.minor = init.minor.min(FUSE_KERNEL_MINOR_VERSION);
476-
info.max_readahead = init.max_readahead;
477-
info.capable = init.flags;
478-
info.want = DEFAULT_FLAGS & init.flags;
479-
info.want2 = 0;
480-
info.capable2 = 0;
474+
// Prepare this request's session info, from the defaults and the client's offer alone, and
475+
// call the file system to negotiate. The session publishes the result only once it has
476+
// sent the reply.
477+
let mut info = SessionInfo {
478+
major: init.major,
479+
minor: init.minor.min(FUSE_KERNEL_MINOR_VERSION),
480+
max_readahead: init.max_readahead,
481+
capable: init.flags,
482+
want: DEFAULT_FLAGS & init.flags,
483+
time_gran: 1,
484+
max_write: DEFAULT_MAX_PAGES * PAGE_SIZE,
485+
..SessionInfo::default()
486+
};
481487
// Negotiate flags2 when the kernel supports extended init.
482488
if init.flags & FUSE_INIT_EXT != 0 {
483489
info.capable2 = init.flags2;
484490
info.want2 = DEFAULT_FLAGS2 & init.flags2;
485491
}
486-
info.time_gran = 1;
487-
info.max_write = DEFAULT_MAX_PAGES * PAGE_SIZE;
488492
let SessionInfo {
489493
major,
490494
minor,
491495
capable,
492496
capable2,
493497
..
494-
} = *info;
498+
} = info;
495499
self.fs.init(&mut info);
496500
// The version and the client's capabilities belong to the session:
497501
// keep them, even if `init` replaced the whole `info`.
@@ -514,7 +518,6 @@ impl Session {
514518
"File system wants FUSE flags that the client did not offer",
515519
);
516520
// Leave the session uninitialized, without a negotiation.
517-
*info = SessionInfo::default();
518521
return Err(lx::Error::EIO.into());
519522
}
520523

@@ -532,9 +535,12 @@ impl Session {
532535
out.flags2 = info.want2;
533536
}
534537

538+
// A reply that cannot be sent leaves the session uninitialized, without a negotiation,
539+
// since the client never received it.
535540
sender.send_arg(request.unique(), out)?;
536541

537-
// Indicate other requests can be received now.
542+
// Publish the negotiation, and indicate other requests can be received now.
543+
*session_info = info;
538544
self.initialized.store(true, atomic::Ordering::Release);
539545
Ok(())
540546
}

‎vm/devices/support/fs/fuse/src/session/tests/init.rs‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,94 @@ fn init_keeps_the_client_version_and_capabilities() {
103103
);
104104
saved.validate().unwrap();
105105
}
106+
107+
/// A file system that records the session information that each `init`
108+
/// receives, and that allows more background requests only when the client
109+
/// offers asynchronous direct I/O.
110+
#[derive(Default)]
111+
struct OfferDependentFs {
112+
received: Arc<Mutex<Vec<SessionInfoState>>>,
113+
}
114+
115+
impl Fuse for OfferDependentFs {
116+
fn init(&self, info: &mut SessionInfo) {
117+
self.received.lock().push((*info).into());
118+
if info.capable() & FUSE_ASYNC_DIO != 0 {
119+
info.max_background = 64;
120+
info.congestion_threshold = 48;
121+
}
122+
}
123+
}
124+
125+
/// A sender that cannot send a reply, as a virtio-fs reply fails when the
126+
/// client's buffers cannot hold it, and that counts the replies that the
127+
/// session tries to send.
128+
#[derive(Default)]
129+
struct FailingSender {
130+
attempts: usize,
131+
}
132+
133+
impl ReplySender for FailingSender {
134+
fn send(&mut self, _bufs: &[io::IoSlice<'_>]) -> io::Result<()> {
135+
self.attempts += 1;
136+
Err(io::ErrorKind::WriteZero.into())
137+
}
138+
}
139+
140+
/// Sends `request` to `session` through a sender that cannot send the reply,
141+
/// and checks that the session tries to send exactly one reply.
142+
fn dispatch_request_failing_reply(session: &Session, request: &[u8]) {
143+
let mut sender = FailingSender::default();
144+
session.dispatch(Request::new(request).unwrap(), &mut sender, None);
145+
assert_eq!(sender.attempts, 1);
146+
}
147+
148+
/// The `FUSE_INIT` that the tests send first, whose reply cannot be sent,
149+
/// which offers asynchronous direct I/O and extended flags.
150+
fn first_init_request() -> Vec<u8> {
151+
make_init_request(
152+
7,
153+
39,
154+
131072,
155+
FUSE_ASYNC_READ | FUSE_ASYNC_DIO | FUSE_INIT_EXT,
156+
FUSE_DIRECT_IO_ALLOW_MMAP_FLAG2,
157+
)
158+
}
159+
160+
/// A client that never received the reply to its `FUSE_INIT` agreed to no
161+
/// negotiation, so the session stays uninitialized, without a negotiation to
162+
/// save.
163+
#[test]
164+
fn init_whose_reply_fails_leaves_no_negotiation() {
165+
let session = Session::new(OfferDependentFs::default());
166+
dispatch_request_failing_reply(&session, &first_init_request());
167+
assert!(!session.is_initialized());
168+
assert_eq!(session.save_state(), SessionState::default());
169+
}
170+
171+
/// A `FUSE_INIT` that follows one whose reply could not be sent negotiates as
172+
/// on a new session: `init` receives, and the session replies and saves, only
173+
/// what the new request offers.
174+
#[test]
175+
fn init_after_a_failed_reply_negotiates_afresh() {
176+
let retry = make_init_request(7, 31, 65536, FUSE_ASYNC_READ, 0);
177+
178+
let fs = OfferDependentFs::default();
179+
let received = Arc::clone(&fs.received);
180+
let session = Session::new(fs);
181+
dispatch_request_failing_reply(&session, &first_init_request());
182+
let (header, reply) = dispatch_request(&session, &retry);
183+
184+
let fresh_fs = OfferDependentFs::default();
185+
let fresh_received = Arc::clone(&fresh_fs.received);
186+
let fresh = Session::new(fresh_fs);
187+
let (_, fresh_reply) = dispatch_request(&fresh, &retry);
188+
189+
assert_eq!(header.error, 0);
190+
let (out, _) = fuse_init_out::read_from_prefix(&reply).unwrap();
191+
assert_eq!((out.max_background, out.congestion_threshold), (0, 0));
192+
assert_eq!(reply, fresh_reply);
193+
assert_eq!(received.lock()[1..], fresh_received.lock()[..]);
194+
assert!(session.is_initialized());
195+
assert_eq!(session.save_state(), fresh.save_state());
196+
}

0 commit comments

Comments
 (0)