Skip to content

vfs: pin owner-managed claims across unlocked break and revoke dispatch - #1834

Open
akamit91 wants to merge 3 commits into
chimera-nas:mainfrom
akamit91:vfs-claim-callback-lifetime
Open

akamit91 wants to merge 3 commits into
chimera-nas:mainfrom
akamit91:vfs-claim-callback-lifetime

Conversation

@akamit91

@akamit91 akamit91 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Problem

The VFS claim core selects conflicting claims under file->lock, but break, revoke, and callback processing may continue after the lock is released.

For claims embedded in protocol-owned objects, such as NFSv4 delegations, OPEN/LOCK state, and FUSE coherence grants, the owning protocol can concurrently unlink and free the object. This creates a use-after-free window between claim selection and callback dispatch.

SMB cache grants already have reference-counted lifetime protection, but that protection does not extend to all claim types.

Solution

Introduce a lightweight owner-pinning mechanism that keeps selected claims and their required owner context alive across unlocked operations.

The changes:

  • Add chimera_vfs_claim_owner_register() to register protocol-specific try_get / put operations by claim construct and owner protocol.
  • Reuse existing SMB grant references and NFS/FUSE owner reference counts instead of introducing a separate refcount on every VFS claim.
  • Acquire lifetime protection while holding file->lock, before allowing a selected claim to be used outside the critical section.
  • Cover claim acquisition, I/O break dispatch, revocation, expiry processing, and deferred callbacks.
  • Revalidate claim membership before initiating a break or revoke, since keeping an object alive does not necessarily mean its claim is still active.
  • Handle refused pins when an owner has already committed to teardown, allowing its release path to wake waiting operations rather than repeatedly retrying.
  • Ensure reference releases occur outside file->lock to avoid lock recursion and teardown deadlocks.
  • Retain the NFS client's lifetime while an associated delegation remains alive.

Design considerations

The implementation intentionally reuses protocol-owned lifetime mechanisms and keeps the VFS claim core independent of protocol-specific object layouts.

It does not introduce a new claim ownership model, change backend claim arbitration, or redesign callback routing.

The additional reference operations are primarily limited to conflicting claim and callback paths rather than ordinary uncontended I/O.

Testing

  • Extend vfs_claim_test with owner-pin acquisition/release coverage.
  • Verify refused-pin handling for a conflicting claim.
  • Retain existing claim admission, break, and revocation tests.

Scope

This change addresses memory safety during claim selection and unlocked dispatch.

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Quick-tier test coverage

Component Functions Lines Branches
src/server/nfs █████████░ 91%
1,148/1,262
████████░░ 77%
24,312/31,491
██████░░░░ 61%
11,903/19,439
src/server/smb █████████░ 89%
698/786
███████░░░ 70%
16,805/23,886
██████░░░░ 59%
6,950/11,844
src/vfs █████████░ 91%
891/984
████████░░ 77%
17,846/23,114
██████░░░░ 63%
6,289/9,967
src/vfs/diskfs ████████░░ 83%
665/800
███████░░░ 75%
12,270/16,425
██████░░░░ 60%
3,136/5,202
src/vfs/nfs ████████░░ 75%
220/293
██████░░░░ 61%
5,458/8,878
█████░░░░░ 48%
1,496/3,124
src/server/s3 █████████░ 92%
313/342
████████░░ 77%
6,495/8,449
██████░░░░ 60%
1,989/3,292
src/posix ███████░░░ 70%
218/312
██████░░░░ 60%
3,270/5,427
█████░░░░░ 51%
843/1,665
src/vfs/memfs ██████████ 96%
93/97
████████░░ 79%
3,878/4,899
███████░░░ 72%
1,509/2,096
src/vfs/cairn ████████░░ 84%
106/126
███████░░░ 73%
3,466/4,772
██████░░░░ 63%
956/1,506
src/vfs/smb ██████████ 99%
173/175
████████░░ 82%
3,521/4,271
██████░░░░ 61%
840/1,366
src/server/fuse ████████░░ 77%
155/201
██████░░░░ 63%
2,351/3,753
█████░░░░░ 54%
1,419/2,644
src/common ████████░░ 79%
148/187
█████░░░░░ 51%
1,897/3,721
███░░░░░░░ 28%
803/2,825
src/server/rest ██████░░░░ 62%
58/94
█████░░░░░ 47%
1,114/2,377
████░░░░░░ 36%
333/938
src/vfs/io_uring █████████░ 93%
51/55
████████░░ 77%
1,783/2,320
██████░░░░ 62%
604/971
src/vfs/linux █████████░ 91%
58/64
███████░░░ 72%
1,675/2,320
██████░░░░ 60%
663/1,111
src/server ███████░░░ 66%
188/284
██████░░░░ 56%
1,175/2,104
████░░░░░░ 36%
190/532
src/client ██████░░░░ 61%
80/132
█████░░░░░ 50%
1,008/2,003
██░░░░░░░░ 24%
83/347
src/smb_common ██████████ 100%
51/51
████████░░ 78%
1,239/1,582
███████░░░ 68%
510/746
src/vfs/sdk █████████░ 94%
32/34
███████░░░ 71%
350/495
████████░░ 79%
129/164
src/nfs_common ██████████ 100%
15/15
███████░░░ 65%
231/355
███████░░░ 75%
139/186
src/vfs/memkv ██████████ 100%
14/14
█████████░ 90%
257/286
███████░░░ 72%
114/158
src/metrics ██████████ 100%
10/10
████████░░ 81%
198/243
██████░░░░ 61%
56/92
Total █████████░ 85%
5,385/6,318
███████░░░ 72%
110,599/153,171
██████░░░░ 58%
40,954/70,215
make coverage CTEST_ARGS="--output-on-failure"

Coverage of this pull request's changes

████████░░ 83% (316/381 changed lines executed)

File Changed lines executed
src/server/fuse/fuse.c ██████████ 100% (6/6)
src/server/fuse/fuse_coherence.c █████████░ 92% (79/86)
src/server/fuse/fuse_internal.h no executable lines changed
src/server/nfs/nfs4_state.c ███████░░░ 69% (48/70)
src/vfs/vfs_claim.c ██████████ 98% (113/115)
src/vfs/vfs_claim_backend.c ░░░░░░░░░░ 0% (0/1)
src/vfs/vfs_claim_break.c ███████░░░ 67% (49/73)
src/vfs/vfs_claim_internal.h no executable lines changed
src/vfs/vfs_claim_io.c ███████░░░ 71% (17/24)
src/vfs/vfs_claim_owner.h no executable lines changed
src/vfs/vfs_claim_types.h no executable lines changed
src/vfs/vfs_proc_getattr.c ███████░░░ 67% (4/6)
src/vfs/vfs_procs.h no executable lines changed

Changed lines the quick-tier tests never executed: src/server/fuse/fuse_coherence.c 278, 649–651, 697, 826–827; src/server/nfs/nfs4_state.c 2181–2182, 2248–2251, 2253, 2255–2265, 2271–2272, 2274–2275; src/vfs/vfs_claim.c 1738–1739; src/vfs/vfs_claim_backend.c 511 …and 16 more.

Executed by the quick-tier tests (ctest) — code reached solely by the other suites counts as unexecuted here. Lines with no coverage mapping (declarations, braces, comments) are excluded rather than counted as misses.

@akamit91
akamit91 force-pushed the vfs-claim-callback-lifetime branch from 8cd8590 to 3ff11c2 Compare November 13, 2026 13:16
…d park

A claim selected under file->lock is only safe to dereference again once
the lock is dropped if something keeps its owner alive.  Grant-bearing
claims already have that in their refcounted grant; owner-managed claims
(NFSv4 delegations and OPEN/LOCK state, FUSE coherence grants) did not,
leaving a window between selection and dispatch where the owning object
could be torn down and freed out from under the claim core.

Introduce an owner-ops registration -- chimera_vfs_claim_owner_register()
mapping (construct, proto) to try_get/put callbacks -- and route every
break, revoke, and park path through chimera_vfs_claim_ref_take()/
chimera_vfs_claim_ref_drop() before touching a selected claim across an
unlock:

- chimera_vfs_claim_try_acquire()'s BREAKING loop re-admits and pins the
  conflict at the top of every iteration instead of carrying a stale
  pointer across the unlocked dispatch window (selection-to-pin race).
- The courtesy-reclaim DENIED path pins the reclaimed holder before
  invoking its revoked_cb off-lock, since callbacks such as
  nfs_client_lease_revoked_cb dereference the owning nfs_client.
- chimera_vfs_claim_drive_breaks() and chimera_vfs_claim_revoke_breaks()
  pin selected/collected victims before unlocking.
- chimera_vfs_claim_revoke_ex() re-validates that a pinned claim is still
  a member of the expected file before mutating it, matching the
  membership check already done in begin_break_ex(); a mismatch means a
  concurrent teardown already finished with the claim.

A refused pin means a concurrent teardown already won the race for that
holder (e.g. DELEGRETURN/CLOSE, or the NFSv4 lease sweeper freeing a
lock-owner's leases outright in lock_state_cleanup); callers back off
onto the holder's own teardown path pumping the file, rather than
dereferencing a claim that may already be gone.

Owner-side reference counting is switched to nonzero-only CAS loops
(nfs_delegation_try_get, chimera_fuse_grant_claim_try_get,
nfs_client_claim_try_get) so a racing pin either wins against a still-
live refcount or observes zero and backs off, never resurrecting an
object mid-free.  NFSv4 OPEN and LOCK claim callbacks now pin the owning
nfs_client the same way delegations do.

Unregistered (construct, proto) pairs default to caller-managed and are
always pinnable, preserving the pre-existing lifetime assumption for
every claim kind that predates this mechanism.
Carry SMB waiter wakeups through the final dispatch pin while preserving explicit shutdown suppression. Park expired-break waiters on refused owner pins and continue namespace recalls past tearing-down holders.

Drain FUSE grants, dispatch pins, and notifier work before freeing mount state. Validate explicit CLONE ranges in the backend and use authoritative source size for clone-to-EOF, with a corrected specs revision and focused regression coverage.
Exclude the POSIX pwrite growth block from Windows builds while retaining memfs CLONE boundary and delegation checks. Register the Linux backend probe only when building on Linux.
@akamit91
akamit91 force-pushed the vfs-claim-callback-lifetime branch from bbda053 to 889a5a4 Compare December 5, 2026 20:05

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.

1 participant