Skip to main content

Lesson 25 of 26 · 13 min read

Distributed file systems (NFS, AFS) and hypervisors

Compare stateless idempotent NFSv2 against stateful callback-based AFSv2, then look underneath the OS to see how hypervisors virtualize guest OSes via traps and shadow page tables.

Four advanced architecture themes: Stateless NFSv2, AFS callbacks, Trap-emulate VMM, and Shadow tables.
Compare stateless NFS against callback-driven AFS, then virtualize whole guest operating systems under a hypervisor.
Course syllabus · lesson 25 of 26
In this lesson

Our final content lesson pairs two classic architectural pivots: moving file systems across networks (NFS vs. AFS), and sliding one extra layer of virtualization underneath the operating system itself (Virtual Machine Monitors / Hypervisors).

The crux of the problem spans two design frontiers: how a distributed file protocol trades server statelessness (fast crash recovery) against explicit client-cache callbacks (scalable performance), and how a hypervisor runs unmodified operating systems safely on shared physical hardware.

Stateless NFSv2 (volume/inode file handles, idempotent reads/writes, periodic attribute polling, commit-before-ACK server writes) compared against Stateful AFSv2 (whole-file local caching, server callbacks, last-closer-wins).
Open full-size diagram
NFSv2 resumes without rebuilding per-client open state. AFSv2 tracks callbacks to reduce cache-validation traffic.

Compare NFS revalidation and AFS callbacks

NFS cache blocks + attrs, NFS server file v1, AFS cache whole file, AFS server file v1NFS cacheblocks + attrsNFS serverfile v1AFS cachewhole fileAFS serverfile v1

Step 1 of 3

Cache a file at the client

An NFS client can cache file blocks and attributes. Classic AFS caches whole files locally and receives a callback promise from the server.

Part 1: Sun NFSv2 vs. The Andrew File System (AFS)

Sun NFSv2: Statelessness and idempotency

Early distributed systems kept per-client open-file tables on the server — causing huge headaches whenever the server crashed and forgot who held which file open! Sun's NFSv2 chased one brilliant goal: make requests independent of server-side open-file state.

  • Opaque File Handles (vol_id, inum, generation): Because the protocol does not rely on an open-file descriptor remembered by the server, every NFSPROC_READ or NFSPROC_WRITE request carries the self-contained file handle, exact byte offset, and length!
  • Idempotency (TIP: IDEMPOTENCY IS POWERFUL): Writing bytes X at offset = 512 twice produces the exact same file contents as writing it once if no conflicting write intervenes. A repeated read or lookup has no update effect, though its result can change as other clients modify the filesystem. If the server reboots mid-edit, the client simply retries until the server responds — the file server can resume without reconstructing NFS client sessions once the underlying filesystem is available.
  • Wait: server write buffering rule: An NFS server must force modified data and metadata to stable disk before sending the WRITE ACK! Otherwise, if the server acknowledged from RAM and crashed 10 ms later, the client would assume the write succeeded forever while the disk held old garbage.
  • Caching weakness: Because NFS servers forget who caches blocks, clients cache data and use GETATTR to validate it when cached attributes expire or client policy calls for revalidation. Flushing dirty data at close and validating at open improves visibility across clients. Read behavior during an existing open depends on cache policy.

A client can retry a read after the server reboots from its handle, offset, and count. For example, reading 4 KiB at offset 8,192 uses the same values whether the request is the first attempt or a retransmission. A generation field matters if inode 72 is deleted and reused. An old handle for generation 8 must fail against generation 9, even though the inode number matches. Otherwise a delayed request may operate on a different file.

Consider a write to bytes 8,192 through 12,287 that reaches stable storage but loses its reply. A retry sends the same bytes to that range. With no conflicting writer, the repeated write preserves the contents. An append operation that asks the server to choose the current end would append twice if implemented the same way. Even writes at fixed offsets need care with concurrent updates. If another client's write to that range occurs between the original request and its retry, the retry can replace that newer data.

Andrew File System (AFSv2): Stateful whole-file caching & callbacks

AFSv1 checked cached copies with the server on open. AFSv2 used callbacks to reduce that validation traffic:

  1. Whole-file client caching: On an open without a valid cached copy, the client fetches the entire file into its local cache (Fetch). Reads and writes on that open can then run locally. A modified file is sent back at close (Store).
  2. Callbacks replace polling: When handing a client a file, the AFS server records a stateful callback promise: "I will notify you if anyone else modifies this file." Clients can reuse a copy while its callback remains valid, without checking every open with the server.
  3. Cache consistency (last closer wins): If two clients edit private copies, the last successful store installed by the server wins. A client that already has a file open can continue reading its local version. Clients must revalidate cached copies when callback state is lost on server restart or the validity of a promise can no longer be established.

Compare bandwidth and validation traffic for a read-mostly workload. Suppose 100 clients each open the same 64 KiB file 20 times over a day. After the initial fetches, if the file never changes and all callbacks stay valid, AFS can satisfy the remaining 1,900 opens locally. The initial fetches transfer 6.25 MiB total. A client that revalidates on every open would send up to 2,000 validation requests under the same schedule, although a timed attribute cache can avoid some of those trips. The server pays for callbacks with a subscription list and invalidation traffic proportional to the number of clients caching a changed file.

Now change the access pattern: each client opens an uncached 64 MiB file to inspect only a 4 KiB header. A whole-file fetch transfers 16,384 times as much payload as a single block read. Once cached, the file can support later reads locally, so the result depends on how much of it each client eventually uses and how often the cache copy is invalidated or evicted.

Code exampleTrace
Nested translation: guest VA -> guest table -> guest PA -> EPT -> machine MA
Shadow translation: guest VA -> VMM-maintained shadow table -> machine MA

Virtual Machine Monitor architecture: Guest OS executes in deprivileged Ring 1 / non-root mode, traps privileged operations to the VMM for trap-and-emulate, maps guest VA -> PA -> MA via Shadow Page Tables (or hardware EPT), and bridges the memory information gap via ballooning.
Open full-size diagram
A hypervisor applies Limited Direct Execution and paging indirection one layer lower — virtualizing hardware for whole guest operating systems.

Part 2: Virtual Machine Monitors (Hypervisors)

Why stop virtualizing at processes when an entire data center wants to consolidate Linux, BSD, and Windows servers onto shared physical hosts? A Virtual Machine Monitor (VMM / Hypervisor) inserts itself beneath operating systems, turning one physical server into isolated virtual machines — an OS for operating systems (Appendix B)!

Every mechanism we studied for processes reappears one level lower:

  • Deprivileged CPU & Trap-and-Emulate: Normally a kernel expects full Ring 0 control. The VMM moves the guest OS down (say Ring 1 or hardware non-root mode) while keeping guest user apps in Ring 3. Whenever a guest kernel tries a privileged operation (like updating PTBR or halting the CPU), hardware traps to the real VMM, which validates and emulates the virtual register update safely!
  • Three layers of memory (VA -> PA -> MA) via Shadow Page Tables: Guest applications think they map virtual (VA) to physical (PA), while the VMM actually maps guest "physical" (PA) to real hardware machine addresses (MA)! In software virtualization, the VMM synthesizes hidden Shadow Page Tables (VA -> MA) loaded into real CR3, intercepting guest page-table edits and page faults to keep shadow entries synchronized. Modern CPUs (Intel VT-x / AMD-V + Extended Page Tables EPT / NPT) move two-dimensional VA -> PA -> MA walks straight into silicon!
  • The Information Gap & Balloon Drivers: The VMM knows its frame mappings but generally lacks the guest's knowledge of page use. A guest balloon driver allocates and pins guest physical pages, tells the VMM their frame numbers, and lets the VMM reclaim the machine backing. The guest can use its own free lists and replacement policy to satisfy the allocation.

Follow a privileged instruction and a memory reference

A guest changes its page-table root as part of switching processes. In a trap-and-emulate design, the VMM intercepts the privileged register write, records the guest's intended virtual register value, validates what translation context the guest may use, and resumes the guest. A software VMM can choose a corresponding shadow page table for the hardware. With hardware-assisted virtualization, a guest kernel can run at its normal kernel privilege inside non-root mode, and virtualization controls determine which operations cause a VM exit. Ordinary instructions can run directly. The guest still has separate user and kernel privilege levels inside the VM.

Use 4 KiB pages and guest virtual address 0x35AB. The guest page table maps virtual page 3 to guest frame 7, giving guest physical address 0x75AB. The VMM assigns guest frame 7 to machine frame 0x2C, so the final address is 0x2C5AB. With shadow tables, hardware walks a VMM-managed entry that maps 3 directly to 0x2C. When the guest changes its own page tables, the VMM must invalidate or rebuild stale shadow entries and cached translations. Write-protecting guest page-table pages is one way for the VMM to catch edits.

With EPT, the CPU consults the guest page table and the second-stage mapping. The guest table itself lives at guest physical addresses. Fetching each guest page-table entry also requires translating its address through EPT. A four-level guest walk paired with four-level EPT could therefore require up to 4 * (4 + 1) + 4 = 24 translation-related entry reads before the data access in an uncached model. Translation caches, TLBs, and large pages can bypass much of this work. A guest PTE that fails the guest's own checks can produce a guest page fault. A failure of the second-stage mapping produces an exit for the VMM to handle.

Now ask a VM with 2 GiB configured to inflate its balloon by 256 MiB. At 4 KiB per frame, the balloon driver needs 256 * 1024 / 4 = 65,536 guest frames. If the guest has that much readily available, it can satisfy the request with limited disruption. Otherwise it may reclaim cache or page out anonymous memory. The driver keeps the ballooned frames reserved while the VMM releases their host backing. Deflating returns pages to normal guest use once they can be backed again. A VMM that guesses poorly when reclaiming pages itself may swap a page that the guest was about to free.

Worked trace: AFS invalidation, NFS cache expiry, and a guest fault

Trace Client 1 and Client 2 sharing file /shared/cfg (v1):

  1. AFS flow: Client 1 opens /shared/cfg -> Server sends full file v1 and registers Callback(C1, cfg). Client 2 also opens /shared/cfg -> gets v1 + Callback(C2, cfg). Client 1 edits cfg locally (zero server traffic during writes!) and calls close() -> streams v2 to Server -> Server breaks callbacks to C2 (BreakCallback(cfg)), invalidating C2's cache! Next time C2 calls open(), C2 fetches v2.
  2. VMM shadow page fault trace: Inside a software VMM, a guest process hits an unmapped guest virtual address. Hardware traps to the VMM first -> VMM notices the address is also missing from the guest page table -> VMM forwards a virtual page fault into the guest OS trap handler -> guest OS allocates a guest PA and updates its guest PTE -> guest touches it -> VMM updates VA -> MA in the shadow page table!
  3. Change one input: What happens in NFSv2 if Client 1 updates /shared/cfg, flushes on close(), and Client 2 already read block 0 of /shared/cfg 500 ms earlier with a 3-second attribute-cache timer? If Client 2 keeps using the cached attributes until expiration, it may serve v1 for up to another 2.5 seconds in this model. Once revalidation detects the update, it invalidates the old block and fetches the new version. A client policy that revalidates on the next open may shorten the window.

Finally, put every mechanism across CPU, memory, concurrency, persistence, and tooling together in systems debugging (gdb, valgrind) and cold traces.

Before you move on

Try it in your workspace

Compare stateless NFSv2 idempotent requests + attribute-cache polling against stateful AFSv2 whole-file caching + server callbacks, then trace how a VMM handles guest traps and shadow page tables.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.