Skip to content

Prototype: NFSv4.1 for the macOS NFS backend (stacked on #793) - #795

Draft
cberner wants to merge 1 commit into
claude/nfs-prototypefrom
claude/nfs4-prototype
Draft

cberner wants to merge 1 commit into
claude/nfs-prototypefrom
claude/nfs4-prototype

Conversation

@cberner

@cberner cberner commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Draft, not to merge. NFSv4.1 for the macOS NFS backend, stacked on #793. NFSv3 stays the default. MountOption::CUSTOM("vers=4") or "vers=4.1" selects 4.1 (macOS's mount_nfs supports minor versions 0 and 1). This also brings fixes to the shared NFS code that apply to v3 as well.

v3 and v4.1 compared

Features

NFSv3 NFSv4.1
Protocols NFS + MOUNT NFS only (COMPOUND)
Server state none clients, sessions, slot reply cache, opens, lock owners
Retransmitted non-idempotent calls may run twice (e.g. a retried REMOVE gets ENOENT) exactly once: the slot keeps the reply
open/release reach the filesystem no: each READ/WRITE uses a handle the server opens and caches yes: OPEN and CLOSE map to them
Byte-range locks local to the client (locallocks), never reach the filesystem the filesystem's getlk/setlk, or a table in the server
ftruncate on a file without write permission refused (SETATTR has no open) works: the open's handle goes with SETATTR
chown to uid/gid 0 works needs sysctl vfs.generic.nfs.client.idmap_ctrl=0 (macOS client bug, see below)
pjdfstest, usual set 482/482 482/482
pjdfstest, excluded cases as root 2058/2108 2052/2108
Linux client (cthon04) passes passes

Both versions share identity (AUTH_SYS uid/gid passed to the filesystem, ACCESS through FUSE_ACCESS), the secret export path, handle keys, the transport and the FUSE glue.

Performance (mac-ci, simple example; medians of 3 interleaved rounds on one runner)

workload FSKit NFSv3 NFSv4.1
write 256 MiB 444 MiB/s 433 MiB/s at wsize 512 KiB (1 trace) 519 MiB/s
read 256 MiB 608 MiB/s 267 MiB/s 320 MiB/s
create 1000 x 4 KiB 49/s 50/s 40/s
stat 1000 3329/s 148/s 143/s
list 1000, with stat 2170/s 136/s 137/s
read 1000 x 4 KiB 613/s 450-2180/s (varies by run) 400/s
delete 1000 47/s 39/s 34/s
  • Data: v4.1 is somewhat ahead.
  • Metadata: about even. v4.1 pays for OPEN/CLOSE on small files.
  • stat and list: both NFS versions are bound by the simple example's lookup, which re-reads its whole directory: about 7 ms per entry over 1000 entries. FSKit hides this because the kernel caches lookups. A server-side lookup/attribute cache that honours FUSE's entry and attr TTLs would help both versions equally.

Implementation size (lines of code, excluding comments and blank lines)

lines
shared: rpc, xdr, transport, FUSE glue, mount, serve-only, options ~1,820
v3-specific (nfs3.rs, which also holds attribute and name helpers v4 uses) ~740
v4.1-specific (nfs4.rs, about 215 of which are constants) ~2,000
tests, shared and v3 / v4.1 ~1,200 / ~600

What the 4.1 server does

  • Sessions:
    • EXCHANGE_ID, CREATE_SESSION, SEQUENCE, DESTROY_SESSION, BIND_CONN_TO_SESSION, BACKCHANNEL_CTL, DESTROY_CLIENTID and RECLAIM_COMPLETE.
    • Client and session ids are random.
    • Each session has up to 16 slots.
    • At most 64 clients and 64 sessions are kept.
  • Exactly-once:
    • A slot keeps its reply when the client sets sa_cachethis or the reply is under 4 KiB.
    • A request retransmitted while it is still running gets DELAY, and its reply is then kept whatever its size.
    • A request whose reply wasn't kept gets RETRY_UNCACHED_REP.
    • A reply that would exceed the session's maximum gets REP_TOO_BIG.
  • Operations:
    • File handles and names: PUTROOTFH, PUTFH, GETFH, SAVEFH, RESTOREFH, LOOKUP, LOOKUPP, CREATE, REMOVE, RENAME and LINK.
    • Data and attributes: GETATTR (with suppattr_exclcreat), SETATTR, ACCESS, READ, WRITE, COMMIT, READDIR and READLINK.
    • Opens: OPEN with CLAIM_NULL or CLAIM_FH in all four create modes, OPEN_DOWNGRADE and CLOSE.
    • Locks and state: LOCK, LOCKT, LOCKU, TEST_STATEID and FREE_STATEID.
    • Security: SECINFO and SECINFO_NO_NAME.
    • The current stateid is tracked through a COMPOUND.
    • The 4.0-only operations answer NOTSUPP.
  • CLOSE:
    • It drops the open's locks.
    • It waits for the flush and reports the flush's error.
  • Stateids are checked against the current file.

Shared fixes from review (both versions)

  • Export path and sealing:
    • The export path holds a secret. v3 MNT answers only that path, and EXPORT lists nothing.
    • v4 seals the export at the first PUTFH of a real handle, and from then on refuses EXCHANGE_ID except from the mount's own client.
  • Cached FUSE handles are told apart by an id of their own, not by fh.
  • Names are checked as the kernel would before they reach the filesystem: /, NUL, empty, longer than NAME_MAX, and . or .. as targets.
  • Calls without AUTH_SYS run as nobody.
  • Limits: each connection queues at most 32 calls, and at most 16 connections are served.
  • COMMIT and stable WRITE sync through the handles that wrote.
  • v3 WRITE and COMMIT return wcc_data.
  • ACCESS always grants DELETE. macOS asks for it on the node being removed, which only its directory decides.
  • READDIR and READDIRPLUS return what they have after 100 ms. A 1 MiB listing of 1000 entries took 8 s, and macOS gave up with ETIMEDOUT.

Defaults chosen from measurements on mac-ci

  • actimeo=1, matching FUSE's default 1-second timeouts:
    • Uncached, every operation revalidated each directory on its path, at about 10 calls per create, stat or unlink.
    • With caching, stat dropped from 4 calls to 1, read from 9 to 3, and create/unlink from ~14 to 7.
    • CUSTOM("actimeo=0") turns it off.
  • rsize 1 MiB for both versions: v3's FSINFO now offers 1 MiB as rtpref too, where it was 128 KiB.
  • wsize: 1 MiB for v4.1 and 512 KiB for v3.
    • At 1 MiB, macOS's v3 client stalls a writer for 15-30 s after each ~64 MiB, about 2 MiB/s overall.

    • 256 MiB of writes over v3 by wsize:

      wsize 32 KiB 64 KiB 128 KiB 256 KiB 512 KiB 1 MiB
      MiB/s 39 72 173 300 433 1.7-3.4

pjdfstest excluded cases on 4.1: the 56 left

  • 18: supplementary groups. A FUSE request names one group.
  • 15: PATH_MAX, which NFS can't carry.
  • 16 (rename/09, rename/10): macOS's VFS asks for write permission on a directory being renamed (xnu vn_authorize_renamex). APFS behaves the same.
  • 7 (chown/07): macOS's v4 client drops a chown that changes nothing.
  • v3's 50: the first three groups, plus one ftruncate case.

macOS client quirk: chown to root

  • The bug: with its identity service on, which is the default, macOS's v4 client can't encode owner or group 0. chown to root fails with ENOSPC before any request is sent.
  • The workaround: sudo sysctl vfs.generic.nfs.client.idmap_ctrl=0. The client then sends "root" and "wheel", which the server resolves.
  • Where it's applied: it's documented in the README, and mac-ci sets it for the 4.1 excluded-tests step.

Testing

  • Unit tests: 19 NFS tests:
    • Sessions and the reply cache, both cached and uncached retransmissions.
    • Sequence errors, sealing and EXCHANGE_ID refusal.
    • CLAIM_FH, TEST_STATEID and FREE_STATEID, and SETATTR with the open's handle.
    • Name checks, the MOUNT protocol, v3 wcc_data, and mount options.
  • Linux: cthon04 (basic, general, special, lock) passes through Linux's v3 client and through its NFSv4.1 client.
  • mac-ci: mount tests, pjdfs, benchmarks and the excluded cases on FSKit, NFSv3 and NFSv4.1. The 4.1 mount and pjdfs steps gate.

Known gaps

  • Exclusive creates: their verifiers aren't stored, so only the slot cache protects a retried exclusive create.
  • No lease expiry, grace period or delegations: the server lives as long as the mount.
  • No extended attributes: these could come through namedattr.
  • Node ids are never forgotten: a handle is a node id plus key.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ALhxewwQVDkW5VehX2vkXu

@cberner
cberner force-pushed the claude/nfs4-prototype branch 2 times, most recently from 69149e4 to 08a01fc Compare November 27, 2026 23:02
@cberner
cberner force-pushed the claude/nfs4-prototype branch 4 times, most recently from 13235b9 to ef01938 Compare November 28, 2026 01:08
@cberner cberner changed the title Prototype: NFSv4.0 for the macOS NFS backend (stacked on #793) Prototype: NFSv4.1 for the macOS NFS backend (stacked on #793) Nov 28, 2026
@cberner
cberner force-pushed the claude/nfs4-prototype branch 15 times, most recently from f11adb3 to 7312966 Compare November 28, 2026 18:54
Experimental, next to NFSv3, which stays the default: with
MountOption::CUSTOM("vers=4") (or "vers=4.1") the backend mounts with
mount_nfs -o vers=4.1, and the same in-process server answers NFS
version 4, minor version 1 only.

nfs4.rs answers COMPOUND, with the operations a client needs to mount and
use a filesystem:
- sessions: EXCHANGE_ID, CREATE_SESSION, SEQUENCE, DESTROY_SESSION,
  BIND_CONN_TO_SESSION, BACKCHANNEL_CTL, DESTROY_CLIENTID and
  RECLAIM_COMPLETE;
- file handles: PUTROOTFH, PUTFH, GETFH, SAVEFH and RESTOREFH;
- names: LOOKUP, LOOKUPP, CREATE, REMOVE, RENAME and LINK;
- data: GETATTR (a broad fattr4 set, with suppattr_exclcreat), SETATTR,
  ACCESS, READ, WRITE, COMMIT, READDIR and READLINK;
- opens: OPEN (CLAIM_NULL and CLAIM_FH, all four create modes),
  OPEN_DOWNGRADE and CLOSE;
- locks: LOCK, LOCKT and LOCKU;
- security and state: SECINFO, SECINFO_NO_NAME, TEST_STATEID and
  FREE_STATEID.

Every COMPOUND but those setting up a session starts with SEQUENCE. Each
session has up to 16 slots, and a slot keeps its last reply when the
client asks or the reply is small, so a retransmitted request is answered
again rather than run twice, and one whose reply was not kept gets
NFS4ERR_RETRY_UNCACHED_REP. The current stateid is tracked through a
COMPOUND. NFSv4.0's SETCLIENTID, RENEW, OPEN_CONFIRM and
RELEASE_LOCKOWNER are refused as 4.1 requires.

OPEN and CLOSE become FUSE open and release, and READ and WRITE use the
open's handle. Locks go to the filesystem's getlk and setlk, or to a table
kept here when it answers ENOSYS. Other state is kept in its simplest
form: client ids never expire, and there are no delegations, callbacks or
pNFS. A client that restarts, or is destroyed, has its opens released.
Owners are numeric strings.

The pseudo-root's one entry is the export, by a name made up for the
server, which can be looked up only until the mount is made. Handles carry
the server's key as for v3, and a v4 server refuses v3's MNT, so neither
version hands other local users a handle.

Unit tests drive COMPOUNDs through the RPC layer against the in-memory
filesystem, sessions and their reply cache included. mac-ci runs the
macOS mount tests, pjdfstest and the benchmark with vers=4.1 too, as
experimental steps next to the v3 ones, and the Linux job runs cthon04
through Linux's NFSv4.1 client as well as its v3 one.

A SETATTR of the size through an open for writing is an ftruncate: the
open's FUSE handle goes with it, so the filesystem may allow what the
file's mode would not.

For both versions:
- mount_nfs caches attributes for a second (actimeo=1), as FUSE does by
  default, instead of not at all: uncached, each operation cost the
  client several calls to revalidate every directory on its path
  (about 10 per create, stat or unlink). On mac-ci this cut stat from 4
  calls to 1, read from 9 to 3, create and unlink from about 14 to 7.
  MountOption::CUSTOM("actimeo=0") restores no caching;
- NFSv4.1 mounts with rsize and wsize of 1 MiB, rather than the
  client's 32 KiB: on mac-ci, writes of 256 MiB went from 67 to 584
  MiB/s and reads from 239 to 491 MiB/s, with the attribute caching;
- READDIR and READDIRPLUS stop adding entries after 100 ms and return
  what they have: a 1 MiB listing of 1000 entries of the simple example,
  whose every lookup reads its whole directory, took 8 s, and macOS gave
  up on it with ETIMEDOUT;
- ACCESS always grants DELETE. macOS asks it of the node to be removed
  or renamed, which only its directory decides, and REMOVE and RENAME
  are the filesystem's to refuse. Until now deleting a read-only file
  from a writable directory was refused by the client;
- each COMPOUND's operations and status are logged at debug, and each
  call's time in the server at trace; a call taking over a second is a
  warning, as the client may retransmit it or give up on it.

NFSv3 mounts with rsize 1 MiB and wsize 512 KiB. FSINFO offered 1 MiB
as rtmax but 128 KiB as rtpref, to which macOS shrank rsize; it now
offers 1 MiB for both. At wsize 1 MiB macOS's v3 client stalls a large
writer for 15 to 30 s after each 64 MiB or so, for 2 MiB/s in all; on
mac-ci 256 MiB went at 39 MiB/s with 32 KiB writes, 173 with 128 KiB
and 433 with 512 KiB, without stalls. v3 WRITE and COMMIT now return
the file's attributes from before and after (wcc_data), as RFC 1813
has them, rather than none.

From a review of the server:
- the export's path holds a secret, for v3 too: MNT answers only that
  path, and EXPORT lists nothing. v4 seals the export at the first PUTFH
  of a handle of it, rather than once mount_nfs has returned, and from
  then on refuses EXCHANGE_ID but from the mount's own client;
- v4 client and session ids are random. They were a counter, and the
  session id held the server's verifier, which EXCHANGE_ID hands out:
  another local user could have named the mount's session and read the
  replies it kept, file handles among them;
- cached FUSE handles are told apart by an id of their own, not by fh,
  which a filesystem may give every open;
- v4 OPEN compares access modes only, so a file created by an owner is
  no longer reopened read-write when it opens it again; concurrent
  OPENs by one owner no longer make two opens;
- CLOSE drops the open's byte-range locks, waits for the flush and
  reports its error; FREE_STATEID of a lock-owner still holding locks
  kept here gets NFS4ERR_LOCKS_HELD; a filesystem without getlk but with
  setlk gets NFS4ERR_NOTSUPP for LOCKT, rather than its locks forgotten;
- a stateid is checked against the current file; lock offsets beyond
  FUSE's are refused or clipped;
- COMMIT and stable WRITE sync through the handles written through;
- names are checked as the kernel would before reaching the filesystem
  ('/', NUL, empty, longer than NAME_MAX, "." and ".." as targets);
- calls without AUTH_SYS credentials are nobody's, not the mounting
  user's;
- a reply beyond the session's maximum gets NFS4ERR_REP_TOO_BIG;
- each connection queues at most 32 calls, no more than 16 connections
  are served, and no more than 64 clients and 64 sessions are kept;
- a connection accepted while the server stops is closed.

A request retransmitted while it is still being answered gets
NFS4ERR_DELAY, and its reply is then kept whatever its size, so that the
retransmission is answered with it rather than with
NFS4ERR_RETRY_UNCACHED_REP. macos_bench.sh shows the server's log when
it fails.

macOS's v4 client cannot send owner or group 0 while it maps ids
through its identity service, its default: chown to root fails with
ENOSPC before any request is made. With
vfs.generic.nfs.client.idmap_ctrl=0 it sends "root" and "wheel", which
the server resolves. This is documented, and mac-ci sets it for the v4.1
run of the excluded pjdfstest cases.

Requests now carry the caller's uid and gid, from each call's AUTH_SYS
credentials, for both NFS versions, so the filesystem checks permissions
against the user who made the call, and SessionACL applies as it does
with FUSE. ACCESS asks the filesystem, through FUSE_ACCESS as the
caller, rather than granting all, and cached file handles serve only
the user who opened them. NFSv4 owners may be names the host's user
database knows, as macOS sends root. pjdfstest's excluded cases, most of
which switch users, run as root in mac-ci on FSKit, NFSv3 and NFSv4.1,
reported in the job summary (PJDFS_EXCLUDED=1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALhxewwQVDkW5VehX2vkXu
@cberner
cberner force-pushed the claude/nfs4-prototype branch from 7312966 to 9fe778e Compare November 28, 2026 19:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants