openvmm: virtiofs: aggregate synthetic root st_nlink counts single-file shares as subdirectories

Open Beginner friendly
#1,632 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Read vm/devices/virtio/virtiofs/src/aggregate.rs: synthetic_root_nlink (L256) feeds both synthetic_root_attr and synthetic_root_statx, and write_child_entry (L416) already shows how to distinguish a file share (volume.exposed_file().is_some()) from a directory share. Count only directory children in the link total, then extend the existing microvm-aggregate tests in that file to assert nlink 3 for one directory plus one file share, and run cargo test -p virtiofs.

Written by the indexing model from the issue text.

Description

area: openvmm

Summary

On a microVM aggregate mount (VirtioFs::new_microvm_aggregate) configured with one or more single-file child shares (MicroVmAggregateChild::new_file), FUSE_GETATTR and FUSE_STATX on the synthetic root (FUSE_ROOT_ID) report nlink = 2 + entries.len(), counting every attached child regardless of whether it exposes a directory or a regular file. Under POSIX stat(2) directory link-count semantics, a directory's st_nlink is 2 plus one per subdirectory (..), whereas regular-file entries do not increment their parent directory's link count. Found by testing.

Root Cause

In vm/devices/virtio/virtiofs/src/aggregate.rs#L256-L264, VirtioFs::synthetic_root_nlink—used by both synthetic_root_attr and synthetic_root_statx—sums all registered aggregate entries indiscriminately:

    fn synthetic_root_nlink(&self) -> u32 {
        let child_count = self
            .inner
            .aggregate()
            .map_or(0, |aggregate| aggregate.registry.read().entries.len());
        u32::try_from(child_count)
            .unwrap_or(u32::MAX)
            .saturating_add(2)
    }

Meanwhile, plain READDIR in VirtioFs::write_child_entry correctly distinguishes single-file shares (volume.exposed_file().is_some(), emitted as lx::DT_REG) from directory shares (lx::DT_DIR), creating an inconsistency between st_nlink and the directory entries emitted by READDIR / READDIRPLUS.

Consequence

Guest utilities that rely on POSIX directory link-count invariants (such as find, fts, and backup/traversal tools applying the st_nlink - 2 leaf-directory optimization) observe a larger subdirectory count on the mount root than the number of DT_DIR entries actually present when single-file shares are attached.

Reproduction

  1. Construct a microVM aggregate via VirtioFs::new_microvm_aggregate with one directory share (MicroVmAggregateChild::new("work", ...)) and one single-file share (MicroVmAggregateChild::new_file("seed", ...)).
  2. Query Fuse::get_attr(&fs, &request(FUSE_ROOT_ID), 0, 0) and Fuse::get_statx(&fs, &request(FUSE_ROOT_ID), ...).
  3. Observe nlink == 4 instead of 3.

Related: #1617, #1598, #1624.

Dominant language
Rust
Stars
831
Forks
56
Avg merge
13h 37m
Merged PRs (30d)
571

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/nvx

All issues in microsoft/nvx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.