#940

devices/virtio-fs: keep the guest page cache of unchanged files on open

Openjgoyvaertswants to merge intofrom

Fixes #939.

With the default CachePolicy::Auto the server never sets FOPEN_KEEP_CACHE, so every open() of a file in the guest drops that file's page cache, including pages other processes have mapped. This change keeps the cache when the host file is unchanged:

  • Each InodeData remembers the host file's mtime, ctime and size from its previous open.
  • do_open sets FOPEN_KEEP_CACHE under Auto when they match. Directories are excluded.
  • A file changed on the host is invalidated on its next open exactly as before, so Auto keeps its consistency. That's the difference from CachePolicy::Always, which needs exclusive access to the directory.
  • Behaviour is unchanged for the first open of an inode and for Never / Always.

Testing

End-to-end, with the change applied to v1.19.6 (the same hunk applies cleanly to main). Built with GPU SND BLK NET INPUT and run under muvm 0.7.0 on an M4 Pro (Arch Linux ARM), using the reproducer from #939 in a fresh VM per run:

another process's open()touching the 64 MiB mapping againhost rewrites the file, guest reopens
stock 1.19.6100% -> 0% resident (3 of 3 runs)33-44 msnew contents seen
patched100% -> 100% resident (3 of 3 runs)0.2-0.5 msnew contents seen

On main:

  • cargo fmt -- --check is clean.
  • krun-devices builds with net,blk,gpu,input without warnings.
  • Its fs unit tests pass.
  • I couldn't run main end-to-end: its libkrun.so.2 needs the musl init target, and muvm links against .so.1.
  • clippy wasn't available on this machine.

Only the Linux backend is changed. macos/passthrough.rs has the same Auto behaviour, but I can't test it.

🤖 Generated with Claude Code