Skip to content

lxutil: allow fdatasync on directories on Windows - #4861

Merged
ggarzia-MSFT merged 4 commits into
microsoft:mainfrom
ggarzia-MSFT:user/ggarzia/virtiofs-fsync-dir-full-sync
Nov 4, 2026
Merged

ggarzia-MSFT merged 4 commits into
microsoft:mainfrom
ggarzia-MSFT:user/ggarzia/virtiofs-fsync-dir-full-sync

Conversation

@ggarzia-MSFT

@ggarzia-MSFT ggarzia-MSFT commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes: microsoft/WSL#42129

Fix fdatasync() on a directory failing with EINVAL when the share is backed by a Windows host (virtiofs and plan9).

Root cause

When a Linux guest calls fdatasync() on a directory, the request reaches LxFile::fsync(data_only = true). On Windows, that translates to NtFlushBuffersFileEx with FLUSH_FLAGS_FILE_DATA_ONLY. Windows rejects data-only flushes on directory handles with STATUS_INVALID_PARAMETER, which is surfaced to the guest as EINVAL. On a native Linux filesystem, the same call succeeds.

Fix

Add LxFile::fsync_dir for callers that already know the handle is a directory.

  • On Windows, fsync_dir always does a full flush, which Windows accepts on directories.
  • On Linux, fsync_dir is the same as fsync, so behavior on Linux hosts is unchanged.
  • virtiofs calls fsync_dir from its FUSE_FSYNCDIR handler, which the guest only sends for directories.
  • plan9 calls fsync_dir when the fid's cached qid marks it as a directory.
  • LxFile::fsync is unchanged, and no extra system call is made to determine the file type.

Testing

  • Extended the cross-platform lxutil fsync test to call fsync_dir on a directory, both full and data-only.
  • Added directory sync tests for virtiofs (fsync_dir_with_fdatasync) and plan9 (fsync_directory).
  • Without the fix, all three tests fail on Windows with EINVAL; with it, they pass.
  • cargo test on Windows: lxutil (68 passed), virtiofs (59 passed), plan9 (11 passed).
  • Clippy, doc and cargo xtask fmt are clean.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the directory-sync fix.

No open issues 🎉

What changed in this PR

This PR fixes directory fdatasync() failures on Windows-hosted virtiofs shares by making directory sync requests perform a full flush.

Changes:

  • Clear FUSE_FSYNC_FDATASYNC before forwarding directory sync requests to fsync.
  • Add a regression test covering directory syncs with and without the data-only flag.
File Description
vm/​devices/​virtio/​virtiofs/​src/​lib.rs Force full directory flushes and add regression coverage for both sync modes.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 3, 2026 00:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the change addressing directory-sync failures on Windows-hosted virtiofs shares.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 3, 2026 00:29
@ggarzia-MSFT ggarzia-MSFT changed the title virtiofs: always do a full sync for fsync_dir lxutil: allow fdatasync on directories on Windows Nov 3, 2026
@github-actions github-actions Bot added the unsafe Related to unsafe code label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the Windows directory data-sync fix.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 3, 2026 01:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The cached qid can misclassify the opened handle and select fsync(true) for a directory, leaving the Windows EINVAL failure unresolved.

1 open issue

Review effort: Balanced

Comment thread vm/devices/support/fs/plan9/src/fid.rs Resolved
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@ggarzia-MSFT
ggarzia-MSFT merged commit 88405ba into microsoft:main Nov 4, 2026
78 checks passed
Sriram Nambakam (snambakam) pushed a commit to snambakam/openvmm that referenced this pull request Nov 6, 2026
## Summary
Closes: microsoft/WSL#42129

Fix `fdatasync()` on a directory failing with `EINVAL` when the share is
backed by a Windows host (virtiofs and plan9).

## Root cause

When a Linux guest calls `fdatasync()` on a directory, the request
reaches `LxFile::fsync(data_only = true)`. On Windows, that translates
to `NtFlushBuffersFileEx` with `FLUSH_FLAGS_FILE_DATA_ONLY`. Windows
rejects data-only flushes on directory handles with
`STATUS_INVALID_PARAMETER`, which is surfaced to the guest as `EINVAL`.
On a native Linux filesystem, the same call succeeds.

## Fix

Add `LxFile::fsync_dir` for callers that already know the handle is a
directory.

- On Windows, `fsync_dir` always does a full flush, which Windows
accepts on directories.
- On Linux, `fsync_dir` is the same as `fsync`, so behavior on Linux
hosts is unchanged.
- virtiofs calls `fsync_dir` from its `FUSE_FSYNCDIR` handler, which the
guest only sends for directories.
- plan9 calls `fsync_dir` when the fid's cached qid marks it as a
directory.
- `LxFile::fsync` is unchanged, and no extra system call is made to
determine the file type.

## Testing

- Extended the cross-platform `lxutil` `fsync` test to call `fsync_dir`
on a directory, both full and data-only.
- Added directory sync tests for virtiofs (`fsync_dir_with_fdatasync`)
and plan9 (`fsync_directory`).
- Without the fix, all three tests fail on Windows with `EINVAL`; with
it, they pass.
- `cargo test` on Windows: `lxutil` (68 passed), `virtiofs` (59 passed),
`plan9` (11 passed).
- Clippy, doc and `cargo xtask fmt` are clean.

---------

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB 9.0 fails with Windows bind mounts in WSLC but works in Docker Desktop

5 participants