Skip to content

mds: fix crash in _eval_stray_remote when stray inode nlink is 0 - #69572

Merged
vshankar merged 12 commits into
ceph:mainfrom
xiubli:stray
Oct 19, 2026
Merged

vshankar merged 12 commits into
ceph:mainfrom
xiubli:stray

Conversation

@xiubli

@xiubli xiubli commented Jun 18, 2026 •

Copy link
Copy Markdown
Member

During MDS failover, stale remote dentries on disk may be loaded via
_omap_fetched->_load_dentry->link_remote, which triggers eval_remote.
When the linked inode's primary dentry is in the stray directory,
_eval_stray_remote is called, but it incorrectly asserts nlink >= 1.

A stray inode can legitimately have nlink == 0 (fully deleted, pending
purge) while stale remote dentries still point to it. Instead of
asserting, skip reintegration when nlink == 0 and let _eval_stray
handle the cleanup via the normal purge path.

Fixes: https://tracker.ceph.com/issues/62663

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@github-actions github-actions Bot added the cephfs Ceph File System label Jun 18, 2026
@xiubli
xiubli requested a review from a team June 18, 2026 08:08
@vshankar

Copy link
Copy Markdown
Contributor

@xiubli - can you explain a bit more in detail as to how can an inode end up in stray (and get persisted in the stray directory object) with nlink=0? I do see in the tracker that the stray inode has nl=-1 which definitely means nlink was 0 and the MDS then decremented it again.

@xiubli

xiubli commented Jun 22, 2026 •

Copy link
Copy Markdown
Member Author

@xiubli - can you explain a bit more in detail as to how can an inode end up in stray (and get persisted in the stray directory object) with nlink=0? I do see in the tracker that the stray inode has nl=-1 which definitely means nlink was 0 and the MDS then decremented it again.

@vshankar Sorry for confusion, possibly I need to create a new tracker for it because the tracker's old and new logs are a bit different.

In our case it should be the nl == 0 instead. I have checked the logs when reproducing this all the caps has been released.

@xiubli
xiubli force-pushed the stray branch 3 times, most recently from 06d0d6b to b14ed7b Compare June 22, 2026 03:53
@xiubli xiubli changed the title mds/StrayManager: skip assert for stray inode with nlink=0 in _eval_stray_remote mds: fix crash in _eval_stray_remote when stray inode nlink is 0 Jun 22, 2026
@xiubli

xiubli commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

jenkins test api

1 similar comment
@xiubli

xiubli commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

jenkins test api

Comment thread src/mds/StrayManager.cc Resolved
@github-actions

Copy link
Copy Markdown

Config Diff Tool Output

+ added: mds_kill_after_unlink_finish (mds.yaml.in)

The above configuration changes are found in the PR. Please update the relevant release documentation if necessary.
Ignore this comment if docs are already updated. To make the "Check ceph config changes" CI check pass, please comment /config check ok and re-run the test.

@xiubli

xiubli commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

/config check ok

@vshankar

Copy link
Copy Markdown
Contributor

jenkins test api

Comment thread src/mds/Server.cc Resolved
@vshankar

Copy link
Copy Markdown
Contributor

@xiubli - I did a a test run over the weekend (fs:functional). Here is the run link: https://pulpito.ceph.com/?branch=wip-62663

I think the fix is going in the right direction. I will analyze the MDS logs and review this change again, but yes, the nlink issue you found is very much real 👍

@xiubli

xiubli commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

@xiubli - I did a a test run over the weekend (fs:functional). Here is the run link: https://pulpito.ceph.com/?branch=wip-62663

I think the fix is going in the right direction. I will analyze the MDS logs and review this change again, but yes, the nlink issue you found is very much real 👍

Cool, I will check the test too. Thanks @vshankar

@xiubli

xiubli commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@xiubli - I did a a test run over the weekend (fs:functional). Here is the run link: https://pulpito.ceph.com/?branch=wip-62663
I think the fix is going in the right direction. I will analyze the MDS logs and review this change again, but yes, the nlink issue you found is very much real 👍

Cool, I will check the test too. Thanks @vshankar

I didn't find any error relate to this PR. @vshankar

@vshankar

vshankar commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

@xiubli - I did a a test run over the weekend (fs:functional). Here is the run link: https://pulpito.ceph.com/?branch=wip-62663
I think the fix is going in the right direction. I will analyze the MDS logs and review this change again, but yes, the nlink issue you found is very much real 👍

Cool, I will check the test too. Thanks @vshankar

I didn't find any error relate to this PR. @vshankar

Yeh. This change looks good and the debug MDS confirms the same. I have run it through fs suite.

@xiubli

xiubli commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@vshankar I appended 2 new commits to fix the nlink < 0 case.

2026-07-01T12:16:44.996+0000 7f5ae0d1c500 -1 datascan.scan_links: Bad nlink on 0x2000c0c478c expected 0 has -1

Thanks

@xiubli

xiubli commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

@vshankar Appended another commit to fix the race windows between unlink/rename, etc and notify_stray. More detail please see https://tracker.ceph.com/issues/62663#note-13.

xiubli added 10 commits July 30, 2026 15:36
In _eval_stray, when nlink == 0 the stale remote_parent cleanup loop
asserted that all remote dentries in the list must be snap dentries
(last != CEPH_NOSNAP).  The assumption was that non-snap remote
parents (regular hardlinks) would already have been cleaned up by
their respective unlink operations before _eval_stray runs.

However, this assumption does not hold in multi-MDS scenarios due to
MDLog callback ordering.  Consider an inode with two hardlinks
(primary A + remote B, nlink=2) whose auth MDS processes both
unlinks:

  1. unlink A (primary): nlink--, A moved to stray dir, MDLog queued
  2. unlink B (remote):  nlink-- (nlink now 0), MDLog queued
  3. MDLog flush: unlink A callback fires first
     → _unlink_local_finish → notify_stray → _eval_stray
     → nlink == 0, but B is still in remote_parents because
       unlink B's callback hasn't run yet
     → B is a non-snap remote dentry (last == CEPH_NOSNAP)
     → original code hits ceph_assert(last != CEPH_NOSNAP) and crashes

The same race can occur after MDS failover when stale remote dentries
on disk are reloaded via _omap_fetched → _load_dentry → link_remote
and linked to an inode whose nlink is already 0.

Fix by removing the assertion and handling both snap and non-snap
remote dentries uniformly.  Calling unlink_remote() on a non-snap
remote parent is safe: it only removes the dentry from the inode's
remote_parents set and clears the inode pointer.  When the remote
dentry's own unlink callback eventually runs, the dentry is properly
removed from its parent directory without conflict.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
…0 stray inodes

scan_links already removes remote dentries whose target inode does not
exist (used_inos check).  However, after an MDS crash, stale remote
dentries may persist on disk pointing to inodes that DO exist but are
in a stray directory with nlink == 0 (fully deleted, pending purge).
These stale entries cause the MDS to crash on startup with:

  ceph_assert(stray_in->get_inode()->nlink >= 1)
  in StrayManager::_eval_stray_remote()

Fix by tracking inodes found in stray dirs with nlink == 0 during the
SCAN_INOS phase, and removing any remote dentries that point to them
during the CHECK_LINK phase.  This allows the offline tool to clean up
the RADOS-level inconsistency before the MDS starts, without requiring
an MDS binary patch.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
Add a dev config 'mds_kill_after_unlink_finish' to inject an MDS crash
after the unlink journal callback (_unlink_local_finish) completes but
before CDir::commit writes the directory omap to RADOS.  This reproduces
the crash window described in tracker 62663:

  Stage 4: _unlink_local_finish() commits nlink=0 and moves the inode
            to the stray directory.  In-memory state is consistent.
  Stage 5: CDir::commit() (async) writes the directory omap, removing
            the deleted dentry entry from RADOS.

If the MDS crashes between Stage 4 and Stage 5, stale remote dentry
entries persist on RADOS.  On recovery, journal replay restores nlink=0
and the inode's stray location, but loading the stale remote dentry's
parent dirfrag from RADOS triggers eval_remote() -> _eval_stray_remote()
against an inode with nlink==0.

Test procedure:
  1. set mds_kill_after_unlink_finish = true
  2. create a file with primary + remote hardlinks
  3. unlink all hardlinks (nlink drops to 0)
  4. the last unlink triggers this injection point; MDS crashes
  5. restart MDS, verify _eval_stray_remote handles nlink==0 cleanly

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
Add test_kill_after_unlink_finish to verify that the MDS recovers
cleanly after crashing in _unlink_local_finish with nlink == 0.

On recovery, stale dentry entries on disk trigger the eval_remote /
_eval_stray_remote paths.  Without the fix (commits "mds: clean up
non-snap remote parents when stray inode nlink is 0" and "mds: fix
crash in _eval_stray_remote when stray inode nlink is 0"), this
would ceph_assert(nlink >= 1) and crash.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
LocalKernelMount.is_blocked() accesses self.addr which may not
be set if get_global_addr() was never called.  Return False
when addr is None instead of crashing with TypeError.

Signed-off-by: Xiubo Li <[email protected]>
…down

When the Python interpreter shuts down, greenlet/gevent may already
be finalized, causing RuntimeError in logging via threading.get_ident().
Wrap _write() in __del__ with try/except to suppress this harmless
shutdown noise.

Signed-off-by: Xiubo Li <[email protected]>
nlink can underflow to -1 when a stray inode with nlink == 0
receives an additional nlink-- operation before the MDS purges
it.  This can happen during journal replay after an MDS crash:

  1. An inode with nlink == 2 (primary + one remote hardlink)
     is fully unlinked.  Both the primary and remote unlink
     journal events are flushed, and their callbacks commit
     nlink to 0, placing the inode in the stray directory.

  2. The MDS crashes after the journal callbacks complete but
     before CDir::commit() writes the updated directory omaps
     to RADOS.  Stale remote dentry entries persist on disk.

  3. During recovery, journal replay restores nlink == 0 and
     the inode's stray location.  When the surviving MDS loads
     the stale remote dentry's parent dirfrag from RADOS, the
     stale entry is decoded and link_remote() triggers
     eval_remote() -> _eval_stray_remote() against an inode
     with nlink == 0.

  4. If this triggers another remote unlink peer request (or
     if client replay reissues an already-applied unlink),
     pi.inode->nlink-- decrements from 0 to -1.

nlink is int32_t and all nlink-- code paths lack a lower-bound
check, so once it reaches -1 it can continue to underflow to
-2, -3, etc. on repeated operations.

Fix all nlink == 0 guards in StrayManager (_eval_stray purge
trigger, _eval_stray_remote early return) to use <= 0 so that
negative nlink values are handled identically to zero.  Also
drop the now-redundant ceph_assert(nlink >= 0) since the
<= 0 early return makes it unreachable for non-positive values.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
…cleanup

The stale_stray_inodes set only tracked inodes in stray directories
with nlink == 0.  However, nlink can underflow to -1 (or lower)
when an additional nlink-- operation races with the MDS purge path.
Inodes with negative nlink should be treated identically to those
with nlink == 0: their remote dentries are stale and must be removed
before MDS startup.

Change the condition from nlink == 0 to nlink <= 0 so that negative
nlink values are also covered.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
In _unlink_local_finish and _rename_finish, notify_stray() was called
after respond_to_request(), which drops the xlocks on the inode.
This creates a race window where a concurrent link or unlink request
can acquire the linklock, modify nlink, and then _eval_stray sees
an inconsistent state.
Move notify_stray() before respond_to_request() so the stray
evaluation happens while the inode's nlink is still consistent
with the just-completed unlink/rename operation.

Fixes: https://tracker.ceph.com/issues/62663
Signed-off-by: Xiubo Li <[email protected]>
The existing test_kill_after_unlink_finish test creates a simple file
and unlinks it (nlink 1->0).  However, the stale OMAP entry produced
by this scenario is type 'I' (primary inode), which triggers a
"duplicate inode" error path during recovery dirfrag fetch rather
than the link_remote -> eval_remote -> _eval_stray_remote path.

A hardlinked file produces stale type 'L' (remote link) OMAP entries
which directly trigger link_remote() -> eval_remote() ->
_eval_stray_remote() during dirfrag fetch on recovery.  This is the
exact path fixed by the _eval_stray_remote nlink assertion change.

Since the kill injection point fires after each individual unlink
journal callback, a single crash cannot leave both nlink==0 and a
stale type-'L' entry on disk.  This test uses two crash cycles:

  Cycle 1: unlink primary (nlink 2->1), crash -> recovery reloads
  the stale type-'L' remote dentry triggering _eval_stray_remote(nlink=1)
  which reintegrates the stray to the surviving remote dentry.

  Cycle 2: unlink the reintegrated primary (nlink 1->0), crash ->
  recovery purges the nlink=0 stray and verifies overall FS health.

Also update the comment in Server.cc to document the two-crash procedure.

Fixes: b1e00aaabeb ("mds: fix crash in _eval_stray_remote when stray inode nlink is 0")
Signed-off-by: Xiubo Li <[email protected]>
@xiubli

xiubli commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

@vshankar Just appended a new commit and there nothing changed with the old commits.

@xiubli

xiubli commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

jenkins test make check

@vshankar

vshankar commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by vshankar started 2026-08-05-16:27.

self.mount_a.run_shell(["rm", "-rf", "kill_unlink_test"])
self.mount_a.run_shell(["rm", "-rf", "kill_unlink_recovery"])

def test_kill_after_unlink_finish_hardlink(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails in fs suite: https://pulpito.ceph.com/vshankar-2026-08-25_04:33:03-fs-wip-vshankar-testing-20260824.070857-distro-default-trial/511579

Overall integration run looks good, so this failure is possibly a test only thing. @xiubli mind having a look.

@xiubli xiubli Sep 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vshankar The teuthology failure in test_kill_after_unlink_finish_hardlink is unrelated to the MDS-side fixes.

The failure happened in the test's own cleanup helper delete_mds_coredump():

Command failed with status 123: 'cd .../archive/coredump && sudo ls | sudo xargs file'

Exit 123 from xargs means a "file" invocation exited 1-125. The coredump dir was empty at that point (cycle-1's core had already been removed), so GNU xargs ran "file" with no operands, "file" exited 1, and xargs returned 123. The root cause is the fixed time.sleep(10) racing with the async rm → journal → abort → coredump chain on a loaded machine; the injection point itself fires for both primary and remote unlinks, so the test logic is fine.

I'll amend a new commit to fix it by using self.fs.wait_for_death() instead of the fixed sleep before cleaning up the coredump, which makes it deterministic.

The fixed time.sleep(10) after the injected crash raced with the
async rm -> journal -> abort -> coredump chain on loaded machines.
When the coredump dir was still empty, "sudo ls | sudo xargs file"
ran file with no operands (GNU xargs without -r), file exited 1 and
xargs returned 123, failing delete_mds_coredump():

  Command failed on trial020 with status 123:
  'cd .../archive/coredump && sudo ls | sudo xargs file'

Use fs.wait_for_death() instead: the mon only drops the rank from
the mdsmap after the process is fully dead, so the coredump is
guaranteed to be written before we clean it up.  If the MDS never
crashes, this now fails with a clear RuntimeError.

Signed-off-by: Xiubo Li <[email protected]>
@vshankar

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by vshankar started 2026-09-02-01:48.

@vshankar

Copy link
Copy Markdown
Contributor

This PR has been added to main integration testing by vshankar started 2026-10-13-15:58.

@anthonyeleven

Copy link
Copy Markdown
Contributor

Tested on reef. b677910 cherry-picks cleanly onto v18.2.7. A 5-rank lab filesystem had been crash-looping on StrayManager.cc: 628: FAILED ceph_assert(stray_in->get_inode()->nlink >= 1) in _eval_stray_remote, reached from handle_peer_rename_prep (18 aborts across three daemons). Each time a daemon took rank 3 and went active, the client's rename was retried and hit the assert again. With this patch, the next daemon to take rank 3 went active in about 4 s, the rename committed, the rank stayed up, and the blocked requests on the other ranks cleared.

The workload (hard-link set conversion with concurrent unlink/relink and subtree re-pinning), logs and details are on https://tracker.ceph.com/issues/81730.

@vshankar

Copy link
Copy Markdown
Contributor

Tested on reef. b677910 cherry-picks cleanly onto v18.2.7. A 5-rank lab filesystem had been crash-looping on StrayManager.cc: 628: FAILED ceph_assert(stray_in->get_inode()->nlink >= 1) in _eval_stray_remote, reached from handle_peer_rename_prep (18 aborts across three daemons). Each time a daemon took rank 3 and went active, the client's rename was retried and hit the assert again. With this patch, the next daemon to take rank 3 went active in about 4 s, the rename committed, the rank stayed up, and the blocked requests on the other ranks cleared.

The workload (hard-link set conversion with concurrent unlink/relink and subtree re-pinning), logs and details are on https://tracker.ceph.com/issues/81730.

Tested on reef. b677910 cherry-picks cleanly onto v18.2.7. A 5-rank lab filesystem had been crash-looping on StrayManager.cc: 628: FAILED ceph_assert(stray_in->get_inode()->nlink >= 1) in _eval_stray_remote, reached from handle_peer_rename_prep (18 aborts across three daemons). Each time a daemon took rank 3 and went active, the client's rename was retried and hit the assert again. With this patch, the next daemon to take rank 3 went active in about 4 s, the rename committed, the rank stayed up, and the blocked requests on the other ranks cleared.

The workload (hard-link set conversion with concurrent unlink/relink and subtree re-pinning), logs and details are on https://tracker.ceph.com/issues/81730.

Thanks for testing this patch out. This changes has been run through fs suite and is ready to merge shortly.

@vshankar vshankar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vshankar
vshankar merged commit 859472b into ceph:main Oct 19, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants