[PATCH v5 2/2] ceph: defer the blocklist and fence off the MDS worker
From: Kaho Jiang
Date: Tue Dec 01 2026 - 23:51:16 EST
On a malformed SPLIT the failure path calls ceph_monc_blocklist_add()
with snap_rwsem held, and then ceph_mdsc_close_sessions() from the MDS
connection worker that must deliver the SESSION_CLOSE reply it waits
for. With mount_timeout=0 the worker never processes the reply, so the
wait is indefinite.
Defer the blocklist (and its OSD map wait) and the fence to a work item
so the worker is never blocked. Keep the client fenced by setting
CEPH_MOUNT_FENCE_IO synchronously, then fail pending MDS requests and
drop each session's caps from the work item, as the force-umount path
does, without waiting for a close ack a blocklisted client can never
get. Also discard the target inode in handle_reply() when it is still
I_NEW, so a later cap lookup cannot block forever on it.
Fixes: a68e564adcaa ("ceph: blocklist the kclient when receiving corrupted snap trace")
Reported-by: co+408fcc361565713f@xxxxxxx
Assisted-by: LLM
Signed-off-by: Kaho Jiang <kj2864@xxxxxxx>
---
fs/ceph/caps.c | 2 +-
fs/ceph/mds_client.c | 41 ++++++++++++++++++++++++++++++++++++++++-
fs/ceph/mds_client.h | 3 +++
fs/ceph/snap.c | 33 ++++++++++-----------------------
4 files changed, 54 insertions(+), 25 deletions(-)
diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c
index cc8748f7e0bd..4e86c277ac00 100644
--- a/fs/ceph/caps.c
+++ b/fs/ceph/caps.c
@@ -4983,7 +4983,7 @@ void ceph_handle_caps(struct ceph_mds_session *session,
/* Defer closing the sessions after s_mutex lock being released */
if (close_sessions)
- ceph_mdsc_close_sessions(mdsc);
+ ceph_mdsc_schedule_snap_fence(mdsc);
kfree(extra_info.fscrypt_auth);
return;
diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index 3d3bd5e06a92..d6a2bb660a49 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -69,6 +69,7 @@ static void __wake_requests(struct ceph_mds_client *mdsc,
static void ceph_cap_release_work(struct work_struct *work);
static void ceph_cap_reclaim_work(struct work_struct *work);
static void ceph_mdsc_reset_workfn(struct work_struct *work);
+static void ceph_mdsc_snap_fence_workfn(struct work_struct *work);
static const struct ceph_connection_operations mds_con_ops;
@@ -4373,6 +4374,9 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
}
out_err:
+ if (req->r_target_inode && (req->r_target_inode->i_state & I_NEW))
+ discard_new_inode(req->r_target_inode);
+
mutex_lock(&mdsc->mutex);
if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
if (err) {
@@ -4398,7 +4402,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
/* Defer closing the sessions after s_mutex lock being released */
if (close_sessions)
- ceph_mdsc_close_sessions(mdsc);
+ ceph_mdsc_schedule_snap_fence(mdsc);
return;
}
@@ -6515,6 +6519,8 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
init_waitqueue_head(&mdsc->reset_state.blocked_wq);
atomic_set(&mdsc->reset_state.blocked_requests, 0);
INIT_WORK(&mdsc->reset_work, ceph_mdsc_reset_workfn);
+ INIT_WORK(&mdsc->snap_fence_work, ceph_mdsc_snap_fence_workfn);
+ atomic_set(&mdsc->snap_fence_pending, 0);
ceph_caps_init(mdsc);
ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
@@ -6909,6 +6915,38 @@ static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
return atomic_read(&mdsc->num_sessions) <= skipped;
}
+static void ceph_mdsc_snap_fence_workfn(struct work_struct *work)
+{
+ struct ceph_mds_client *mdsc =
+ container_of(work, struct ceph_mds_client, snap_fence_work);
+ struct ceph_client *cl = mdsc->fsc->client;
+ int ret;
+
+ ret = ceph_monc_blocklist_add(&cl->monc, &cl->msgr.inst.addr);
+ if (ret)
+ pr_err_client(cl, "failed to blocklist %s: %d\n",
+ ceph_pr_addr(&cl->msgr.inst.addr), ret);
+ else
+ pr_warn_ratelimited_client(cl,
+ "%s %s was blocklisted, do remount to continue after corrupted snaptrace is fixed\n",
+ __func__, ceph_pr_addr(&cl->msgr.inst.addr));
+
+ ceph_mdsc_force_umount(mdsc);
+ ceph_dec_osd_stopping_blocker(mdsc);
+}
+
+void ceph_mdsc_schedule_snap_fence(struct ceph_mds_client *mdsc)
+{
+ if (atomic_cmpxchg(&mdsc->snap_fence_pending, 0, 1))
+ return;
+
+ if (!ceph_inc_osd_stopping_blocker(mdsc))
+ return;
+
+ if (!queue_work(system_dfl_wq, &mdsc->snap_fence_work))
+ ceph_dec_osd_stopping_blocker(mdsc);
+}
+
/*
* called after sb is ro or when metadata corrupted.
*/
@@ -7067,6 +7105,7 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
wake_up_all(&mdsc->reset_state.blocked_wq);
cancel_work_sync(&mdsc->reset_work);
+ cancel_work_sync(&mdsc->snap_fence_work);
ceph_mdsc_stop(mdsc);
ceph_metric_destroy(&mdsc->metric);
diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h
index ff9ba09f3b59..65d0fd878741 100644
--- a/fs/ceph/mds_client.h
+++ b/fs/ceph/mds_client.h
@@ -590,6 +590,8 @@ struct ceph_mds_client {
struct ceph_client_metric metric;
struct work_struct reset_work;
struct ceph_client_reset_state reset_state;
+ struct work_struct snap_fence_work;
+ atomic_t snap_fence_pending;
struct ceph_subvolume_metrics_tracker subvol_metrics;
/* Subvolume metrics send tracking */
@@ -633,6 +635,7 @@ int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc);
extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
extern void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc);
+extern void ceph_mdsc_schedule_snap_fence(struct ceph_mds_client *mdsc);
extern void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc);
extern void ceph_mdsc_destroy(struct ceph_fs_client *fsc);
diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c
index 3a1b3beaacf5..6bf6a74f76fa 100644
--- a/fs/ceph/snap.c
+++ b/fs/ceph/snap.c
@@ -796,11 +796,9 @@ int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
struct ceph_snap_realm *realm;
struct ceph_snap_realm *first_realm = NULL;
struct ceph_snap_realm *realm_to_rebuild = NULL;
- struct ceph_client *client = mdsc->fsc->client;
size_t snaps_len;
int rebuild_snapcs;
int err = -ENOMEM;
- int ret;
LIST_HEAD(dirty_realms);
lockdep_assert_held_write(&mdsc->snap_rwsem);
@@ -924,22 +922,8 @@ int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
* When receiving a corrupted snap trace we don't know what
* exactly has happened in MDS side. And we shouldn't continue
* writing to OSD, which may corrupt the snapshot contents.
- *
- * Just try to blocklist this kclient and then this kclient
- * must be remounted to continue after the corrupted metadata
- * fixed in the MDS side.
*/
WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
- ret = ceph_monc_blocklist_add(&client->monc, &client->msgr.inst.addr);
- if (ret)
- pr_err_client(cl, "failed to blocklist %s: %d\n",
- ceph_pr_addr(&client->msgr.inst.addr), ret);
-
- pr_warn_ratelimited_client(cl, "%s %s%sdo remount to continue%s",
- __func__,
- ret ? "" : ceph_pr_addr(&client->msgr.inst.addr),
- ret ? "" : " was blocklisted, ",
- err == -EIO ? " after corrupted snaptrace is fixed" : "");
return err;
}
@@ -1050,7 +1034,7 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
/* decode */
if (msg->front.iov_len < sizeof(*h))
- goto bad;
+ goto fail;
h = p;
op = le32_to_cpu(h->op);
split = le64_to_cpu(h->split); /* non-zero if we are splitting an
@@ -1076,12 +1060,12 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
* child.
*/
split_inos = p;
- ceph_decode_need(&p, e, size_mul(num_split_inos, sizeof(u64)), bad);
+ ceph_decode_need(&p, e, size_mul(num_split_inos, sizeof(u64)), fail);
p += sizeof(u64) * num_split_inos;
split_realms = p;
- ceph_decode_need(&p, e, size_mul(num_split_realms, sizeof(u64)), bad);
+ ceph_decode_need(&p, e, size_mul(num_split_realms, sizeof(u64)), fail);
p += sizeof(u64) * num_split_realms;
- ceph_decode_need(&p, e, sizeof(*ri), bad);
+ ceph_decode_need(&p, e, sizeof(*ri), fail);
/* we will peek at realm info here, but will _not_
* advance p, as the realm update will occur below in
* ceph_update_snap_trace. */
@@ -1158,8 +1142,8 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
* positioned at the start of realm info, as expected by
* ceph_update_snap_trace().
*/
- ceph_decode_skip_n(&p, e, size_mul(num_split_inos, sizeof(u64)), bad);
- ceph_decode_skip_n(&p, e, size_mul(num_split_realms, sizeof(u64)), bad);
+ ceph_decode_skip_n(&p, e, size_mul(num_split_inos, sizeof(u64)), fail);
+ ceph_decode_skip_n(&p, e, size_mul(num_split_realms, sizeof(u64)), fail);
}
/*
@@ -1185,6 +1169,9 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
ceph_dec_mds_stopping_blocker(mdsc);
return;
+fail:
+ WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
+ close_sessions = true;
bad:
pr_err_client(cl, "corrupt snap message from mds%d\n", mds);
ceph_msg_dump(msg);
@@ -1195,7 +1182,7 @@ void ceph_handle_snap(struct ceph_mds_client *mdsc,
ceph_dec_mds_stopping_blocker(mdsc);
if (close_sessions)
- ceph_mdsc_close_sessions(mdsc);
+ ceph_mdsc_schedule_snap_fence(mdsc);
return;
}
--
2.43.0