Skip to contentSkip to conversation

ceph: add manual client reset and selftests

PATCH Superseded Alex Markuze 2 participants 18 messages Mar 27 – Mar 31

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alex Markuze

author maintainer
#0 lore raw

cover ceph: add manual client reset and selftests

Manual client reset gives operators a way to break client/MDS capability ownership stalemates without unmounting the client. The reset blocks new metadata work, gives dirty state a bounded chance to drain, asks each MDS to close the session, and then tears the local sessions down so the client can rebuild clean state.

The debugfs interface in this series is only the control surface used to develop and validate the reset flow. It is not the core feature, and the long-term intent is to have the client trigger the reset automatically when it detects the stuck condition.

A reconnect-only reset would require MDS support for accepting reconnects outside the normal MDS RECONNECT phase. Because that support does not exist today, the series first wires up the reconnect-based worker for validation and then switches the recovery path over to direct session teardown.

I split the work into smaller reviewable steps so the preparatory cleanup lands first, then the reset framework and control path, then the initial worker, then the direct-teardown refinements, and finally the single-client selftests and validation harness.

Patch breakdown:
- 1/10 converts the Ceph inode flags to named bit positions and keeps the file-lock error bit ready for atomic bitops.
- 2/10 adds a bounded wait and diagnostics to wait_caps_flush().
- 3/10 adds reset state tracking and blocks new metadata requests and lock acquisition while reset is in progress.
- 4/10 adds the manual reset debugfs control surface, status reporting plumbing, scheduling hooks, and tracepoints.
- 5/10 adds the initial reconnect-based reset worker and the reconnect completion tracking needed by that worker.
- 6/10 switches reset recovery from reconnect to direct session teardown.
- 7/10 adds the explicit reset state machine, bounded drain phase, and richer status reporting.
- 8/10 removes reconnect bookkeeping left behind by the direct-teardown design.
- 9/10 queues CEPH_SESSION_REQUEST_CLOSE before the forced teardown so the MDS can release state immediately.
- 10/10 adds the single-client stress, corner-case, and validation tests used to exercise the reset flow, and passes the configured debugfs root through the validation wrapper so child scripts observe the same client namespace.

Alex Markuze (10):
ceph: convert inode flags to named bit positions
ceph: add bounded timeout and diagnostics to wait_caps_flush()
ceph: add client reset state and request gating
ceph: add manual reset debugfs control and trace plumbing
ceph: implement reconnect-based manual reset worker
ceph: replace reconnect with direct session teardown in manual reset
ceph: add reset state machine, drain phase, and status reporting
ceph: drop reset reconnect bookkeeping after direct teardown switch
ceph: send SESSION_REQUEST_CLOSE before forced teardown in manual
reset
ceph: add single-client reset selftests and validation harness

 fs/ceph/caps.c                                |   7 +
 fs/ceph/debugfs.c                             | 172 ++++-
 fs/ceph/file.c                                |   2 +-
 fs/ceph/locks.c                               |  24 +-
 fs/ceph/mds_client.c                          | 630 +++++++++++++++-
 fs/ceph/mds_client.h                          |  38 +-
 fs/ceph/super.h                               |  60 +-
 include/trace/events/ceph.h                   |  60 ++
 tools/testing/selftests/filesystems/Makefile  |   1 +
 .../selftests/filesystems/ceph/HOWTO.md       | 121 +++
 .../selftests/filesystems/ceph/Makefile       |   6 +
 .../selftests/filesystems/ceph/README.md      |  40 +
 .../filesystems/ceph/reset_corner_cases.sh    | 707 ++++++++++++++++++
 .../filesystems/ceph/reset_stress.sh          | 682 +++++++++++++++++
 .../filesystems/ceph/run_validation.sh        | 317 ++++++++
 .../filesystems/ceph/validate_consistency.py  | 291 +++++++
 16 files changed, 3105 insertions(+), 53 deletions(-)
 create mode 100644 tools/testing/selftests/filesystems/ceph/HOWTO.md
 create mode 100644 tools/testing/selftests/filesystems/ceph/Makefile
 create mode 100644 tools/testing/selftests/filesystems/ceph/README.md
 create mode 100755 tools/testing/selftests/filesystems/ceph/reset_corner_cases.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/reset_stress.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/run_validation.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/validate_consistency.py
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/10 ceph: convert inode flags to named bit positions

Define all CEPH_I_* flags as named bit positions with derived bitmask values so the Ceph client can use test_bit(), set_bit(), and clear_bit() consistently on inode error and state flags. This prepares the later reset path to use atomic bit operations on the file-lock error state.

---
 fs/ceph/file.c       |  2 +-
 fs/ceph/locks.c      |  8 +++----
 fs/ceph/mds_client.c |  3 ++-
 fs/ceph/mds_client.h |  2 +-
 fs/ceph/super.h      | 54 +++++++++++++++++++++++++++-----------------
 5 files changed, 40 insertions(+), 29 deletions(-)
fs/ceph/file.c +1 -1
index 66bbf6d517a9..aa9ee283dfda 100644
@@ -579,7 +579,7 @@ static void wake_async_create_waiters(struct inode *inode,
 
 	spin_lock(&ci->i_ceph_lock);
 	if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
-		clear_and_wake_up_bit(CEPH_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
+		clear_and_wake_up_bit(CEPH_I_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
 
 		if (ci->i_ceph_flags & CEPH_I_ASYNC_CHECK_CAPS) {
 			ci->i_ceph_flags &= ~CEPH_I_ASYNC_CHECK_CAPS;
fs/ceph/locks.c +3 -5
index dd764f9c64b9..2f21574dfb99 100644
@@ -58,7 +58,7 @@ static void ceph_fl_release_lock(struct file_lock *fl)
 	if (atomic_dec_and_test(&ci->i_filelock_ref)) {
 		/* clear error when all locks are released */
 		spin_lock(&ci->i_ceph_lock);
-		ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
+		clear_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags);
 		spin_unlock(&ci->i_ceph_lock);
 	}
 	fl->fl_u.ceph.inode = NULL;
@@ -272,9 +272,8 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 		wait = 1;
 
 	spin_lock(&ci->i_ceph_lock);
-	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
+	if (test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags))
 		err = -EIO;
-	}
 	spin_unlock(&ci->i_ceph_lock);
 	if (err < 0) {
 		if (op == CEPH_MDS_OP_SETFILELOCK && lock_is_unlock(fl))
@@ -332,9 +331,8 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 	doutc(cl, "fl_file: %p\n", fl->c.flc_file);
 
 	spin_lock(&ci->i_ceph_lock);
-	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
+	if (test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags))
 		err = -EIO;
-	}
 	spin_unlock(&ci->i_ceph_lock);
 	if (err < 0) {
 		if (lock_is_unlock(fl))
fs/ceph/mds_client.c +2 -1
index 23b6d00643c9..12f1b57d4b1c 100644
@@ -3610,7 +3610,8 @@ static void __do_request(struct ceph_mds_client *mdsc,
 
 		spin_lock(&ci->i_ceph_lock);
 		cap = ci->i_auth_cap;
-		if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE && mds != cap->mds) {
+		if (test_bit(CEPH_I_ASYNC_CREATE_BIT, &ci->i_ceph_flags) &&
+		    mds != cap->mds) {
 			doutc(cl, "session changed for auth cap %d -> %d\n",
 			      cap->session->s_mds, session->s_mds);
 
fs/ceph/mds_client.h +1 -1
index 0428a5eaf28c..e91a199d56fd 100644
@@ -658,7 +658,7 @@ static inline int ceph_wait_on_async_create(struct inode *inode)
 {
 	struct ceph_inode_info *ci = ceph_inode(inode);
 
-	return wait_on_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT,
+	return wait_on_bit(&ci->i_ceph_flags, CEPH_I_ASYNC_CREATE_BIT,
 			   TASK_KILLABLE);
 }
 
fs/ceph/super.h +33 -21

74 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 01/10] ceph: convert inode flags to named bit positions

Quoted 53 lines · Alex Markuze

Define all CEPH_I_* flags as named bit positions with derived bitmask values so the Ceph client can use test_bit(), set_bit(), and clear_bit() consistently on inode error and state flags. This prepares the later reset path to use atomic bit operations on the file-lock error state.

---
 fs/ceph/file.c       |  2 +-
 fs/ceph/locks.c      |  8 +++----
 fs/ceph/mds_client.c |  3 ++-
 fs/ceph/mds_client.h |  2 +-
 fs/ceph/super.h      | 54 +++++++++++++++++++++++++++-----------------
 5 files changed, 40 insertions(+), 29 deletions(-)
fs/ceph/file.c +1 -1
index 66bbf6d517a9..aa9ee283dfda 100644
@@ -579,7 +579,7 @@ static void wake_async_create_waiters(struct inode *inode,
 
 	spin_lock(&ci->i_ceph_lock);
 	if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
-		clear_and_wake_up_bit(CEPH_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
+		clear_and_wake_up_bit(CEPH_I_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
 
 		if (ci->i_ceph_flags & CEPH_I_ASYNC_CHECK_CAPS) {
 			ci->i_ceph_flags &= ~CEPH_I_ASYNC_CHECK_CAPS;
fs/ceph/locks.c +2 -3
index dd764f9c64b9..2f21574dfb99 100644
@@ -58,7 +58,7 @@ static void ceph_fl_release_lock(struct file_lock *fl)
 	if (atomic_dec_and_test(&ci->i_filelock_ref)) {
 		/* clear error when all locks are released */
 		spin_lock(&ci->i_ceph_lock);
-		ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
+		clear_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags);
 		spin_unlock(&ci->i_ceph_lock);
 	}
 	fl->fl_u.ceph.inode = NULL;
@@ -272,9 +272,8 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 		wait = 1;
 
 	spin_lock(&ci->i_ceph_lock);
-	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
+	if (test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags))
 		err = -EIO;
-	}
 	spin_unlock(&ci->i_ceph_lock);
 	if (err < 0) {
 		if (op == CEPH_MDS_OP_SETFILELOCK && lock_is_unlock(fl))

fs/ceph/locks.c int ceph_flock(struct file *file, int cmd, struct file_lock *fl) · line 334

Quoted 5 lines
fs/ceph/locks.c +1 -1
@@ -332,9 +331,8 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 	doutc(cl, "fl_file: %p\n", fl->c.flc_file);
 
 	spin_lock(&ci->i_ceph_lock);
-	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
+	if (test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags))

There are several other places of using CEPH_I_ERROR_FILELOCK. I believe we need to convert that cases too:

[1] https://elixir.bootlin.com/linux/v7.0-rc5/source/fs/ceph/caps.c#L2832
[2] https://elixir.bootlin.com/linux/v7.0-rc5/source/fs/ceph/caps.c#L5083
[3] https://elixir.bootlin.com/linux/v7.0-rc5/source/fs/ceph/mds_client.c#L4696

Quoted 48 lines
fs/ceph/locks.c +0 -1
 		err = -EIO;
-	}
 	spin_unlock(&ci->i_ceph_lock);
 	if (err < 0) {
 		if (lock_is_unlock(fl))
fs/ceph/mds_client.c +2 -1
index 23b6d00643c9..12f1b57d4b1c 100644
@@ -3610,7 +3610,8 @@ static void __do_request(struct ceph_mds_client *mdsc,
 
 		spin_lock(&ci->i_ceph_lock);
 		cap = ci->i_auth_cap;
-		if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE && mds != cap->mds) {
+		if (test_bit(CEPH_I_ASYNC_CREATE_BIT, &ci->i_ceph_flags) &&
+		    mds != cap->mds) {
 			doutc(cl, "session changed for auth cap %d -> %d\n",
 			      cap->session->s_mds, session->s_mds);
 
fs/ceph/mds_client.h +1 -1
index 0428a5eaf28c..e91a199d56fd 100644
@@ -658,7 +658,7 @@ static inline int ceph_wait_on_async_create(struct inode *inode)
 {
 	struct ceph_inode_info *ci = ceph_inode(inode);
 
-	return wait_on_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT,
+	return wait_on_bit(&ci->i_ceph_flags, CEPH_I_ASYNC_CREATE_BIT,
 			   TASK_KILLABLE);
 }
 
fs/ceph/super.h +0 -8
index 29a980e22dc2..272c287836c1 100644
@@ -655,23 +655,35 @@ static inline struct inode *ceph_find_inode(struct super_block *sb,
 /*
  * Ceph inode.
  */
-#define CEPH_I_DIR_ORDERED	(1 << 0)  /* dentries in dir are ordered */
-#define CEPH_I_FLUSH		(1 << 2)  /* do not delay flush of dirty metadata */
-#define CEPH_I_POOL_PERM	(1 << 3)  /* pool rd/wr bits are valid */
-#define CEPH_I_POOL_RD		(1 << 4)  /* can read from pool */
-#define CEPH_I_POOL_WR		(1 << 5)  /* can write to pool */
-#define CEPH_I_SEC_INITED	(1 << 6)  /* security initialized */
-#define CEPH_I_KICK_FLUSH	(1 << 7)  /* kick flushing caps */
-#define CEPH_I_FLUSH_SNAPS	(1 << 8)  /* need flush snapss */

fs/ceph/super.h static inline struct inode *ceph_find_inode(struct super_block *sb, · line 658

Quoted 10 lines
fs/ceph/super.h +1 -9
-#define CEPH_I_ERROR_WRITE	(1 << 9) /* have seen write errors */
-#define CEPH_I_ERROR_FILELOCK	(1 << 10) /* have seen file lock errors */
-#define CEPH_I_ODIRECT_BIT	(11) /* inode in direct I/O mode */
-#define CEPH_I_ODIRECT		(1 << CEPH_I_ODIRECT_BIT)
-#define CEPH_ASYNC_CREATE_BIT	(12)	  /* async create in flight for this */
-#define CEPH_I_ASYNC_CREATE	(1 << CEPH_ASYNC_CREATE_BIT)
-#define CEPH_I_SHUTDOWN		(1 << 13) /* inode is no longer usable */
-#define CEPH_I_ASYNC_CHECK_CAPS	(1 << 14) /* check caps immediately after async
-					     creating finishes */
+#define CEPH_I_DIR_ORDERED_BIT		(0)  /* dentries in dir are ordered */

Somehow, we skip the bit 1. I assume we need to have some comment here why it was skipped.

As far as I can see, bit 1 was CEPH_I_NODELAY, which was removed by commit:

a0d93e327fc4 ("ceph: remove delay check logic from ceph_check_caps()")

-#define CEPH_I_NODELAY (1 << 1) /* do not delay cap release */

  The layout just before removal was:                                          
  #define CEPH_I_DIR_ORDERED  (1 << 0)
  #define CEPH_I_NODELAY      (1 << 1)  /* removed */                          
  #define CEPH_I_FLUSH        (1 << 2)                                         
  #define CEPH_I_POOL_PERM    (1 << 3)
  ...

When CEPH_I_NODELAY was deleted, the remaining flags were not renumbered, leaving bit 1 permanently vacant.

So, what could we do here? Simply add comment?

Thanks,
Slava.

Quoted 54 lines
fs/ceph/super.h +32 -4
+#define CEPH_I_FLUSH_BIT		(2)  /* do not delay flush of dirty metadata */
+#define CEPH_I_POOL_PERM_BIT		(3)  /* pool rd/wr bits are valid */
+#define CEPH_I_POOL_RD_BIT		(4)  /* can read from pool */
+#define CEPH_I_POOL_WR_BIT		(5)  /* can write to pool */
+#define CEPH_I_SEC_INITED_BIT		(6)  /* security initialized */
+#define CEPH_I_KICK_FLUSH_BIT		(7)  /* kick flushing caps */
+#define CEPH_I_FLUSH_SNAPS_BIT		(8)  /* need flush snapss */
+#define CEPH_I_ERROR_WRITE_BIT		(9)  /* have seen write errors */
+#define CEPH_I_ERROR_FILELOCK_BIT	(10) /* have seen file lock errors */
+#define CEPH_I_ODIRECT_BIT		(11) /* inode in direct I/O mode */
+#define CEPH_I_ASYNC_CREATE_BIT		(12) /* async create in flight for this */
+#define CEPH_I_SHUTDOWN_BIT		(13) /* inode is no longer usable */
+#define CEPH_I_ASYNC_CHECK_CAPS_BIT	(14) /* check caps after async creating finishes */
+
+#define CEPH_I_DIR_ORDERED		(1 << CEPH_I_DIR_ORDERED_BIT)
+#define CEPH_I_FLUSH			(1 << CEPH_I_FLUSH_BIT)
+#define CEPH_I_POOL_PERM		(1 << CEPH_I_POOL_PERM_BIT)
+#define CEPH_I_POOL_RD			(1 << CEPH_I_POOL_RD_BIT)
+#define CEPH_I_POOL_WR			(1 << CEPH_I_POOL_WR_BIT)
+#define CEPH_I_SEC_INITED		(1 << CEPH_I_SEC_INITED_BIT)
+#define CEPH_I_KICK_FLUSH		(1 << CEPH_I_KICK_FLUSH_BIT)
+#define CEPH_I_FLUSH_SNAPS		(1 << CEPH_I_FLUSH_SNAPS_BIT)
+#define CEPH_I_ERROR_WRITE		(1 << CEPH_I_ERROR_WRITE_BIT)
+#define CEPH_I_ERROR_FILELOCK		(1 << CEPH_I_ERROR_FILELOCK_BIT)
+#define CEPH_I_ODIRECT			(1 << CEPH_I_ODIRECT_BIT)
+#define CEPH_I_ASYNC_CREATE		(1 << CEPH_I_ASYNC_CREATE_BIT)
+#define CEPH_I_SHUTDOWN			(1 << CEPH_I_SHUTDOWN_BIT)
+#define CEPH_I_ASYNC_CHECK_CAPS		(1 << CEPH_I_ASYNC_CHECK_CAPS_BIT)
 
 /*
  * Masks of ceph inode work.
@@ -691,18 +703,18 @@ static inline struct inode *ceph_find_inode(struct super_block *sb,
  */
 static inline void ceph_set_error_write(struct ceph_inode_info *ci)
 {
-	if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ERROR_WRITE)) {
+	if (!test_bit(CEPH_I_ERROR_WRITE_BIT, &ci->i_ceph_flags)) {
 		spin_lock(&ci->i_ceph_lock);
-		ci->i_ceph_flags |= CEPH_I_ERROR_WRITE;
+		set_bit(CEPH_I_ERROR_WRITE_BIT, &ci->i_ceph_flags);
 		spin_unlock(&ci->i_ceph_lock);
 	}
 }
 
 static inline void ceph_clear_error_write(struct ceph_inode_info *ci)
 {
-	if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ERROR_WRITE) {
+	if (test_bit(CEPH_I_ERROR_WRITE_BIT, &ci->i_ceph_flags)) {
 		spin_lock(&ci->i_ceph_lock);
-		ci->i_ceph_flags &= ~CEPH_I_ERROR_WRITE;
+		clear_bit(CEPH_I_ERROR_WRITE_BIT, &ci->i_ceph_flags);
 		spin_unlock(&ci->i_ceph_lock);
 	}
 }
author maintainer
#3 lore raw

PATCH 2/10 ceph: add bounded timeout and diagnostics to wait_caps_flush()

In reply to Alex Markuze

Convert wait_caps_flush() from an unbounded wait into a bounded loop that periodically dumps outstanding flush state when progress stalls. This preserves the original data-integrity semantics while making hung cap flushes diagnosable during reset development and validation.

---
 fs/ceph/caps.c       |  7 +++++++
 fs/ceph/mds_client.c | 47 ++++++++++++++++++++++++++++++++++++++++++--
 fs/ceph/super.h      |  2 ++
 3 files changed, 54 insertions(+), 2 deletions(-)
fs/ceph/caps.c +7 -0
index d51454e995a8..be030fb8e864 100644
@@ -1648,6 +1648,7 @@ static void __ceph_flush_snaps(struct ceph_inode_info *ci,
 
 		spin_lock(&mdsc->cap_dirty_lock);
 		capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
+		capsnap->cap_flush.ci = ci;
 		list_add_tail(&capsnap->cap_flush.g_list,
 			      &mdsc->cap_flush_list);
 		if (oldest_flush_tid == 0)
@@ -1846,6 +1847,9 @@ struct ceph_cap_flush *ceph_alloc_cap_flush(void)
 		return NULL;
 
 	cf->is_capsnap = false;
+	cf->ci = NULL;
+	cf->tid = 0;
+	cf->wake = false;
 	return cf;
 }
 
@@ -1931,6 +1935,7 @@ static u64 __mark_caps_flushing(struct inode *inode,
 	doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode));
 
 	swap(cf, ci->i_prealloc_cap_flush);
+	cf->ci = ci;
 	cf->caps = flushing;
 	cf->wake = wake;
 
@@ -3826,6 +3831,8 @@ static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
 	bool wake_ci = false;
 	bool wake_mdsc = false;
 
+	ci->i_last_cap_flush_ack = flush_tid;
+
 	list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
 		/* Is this the one that was flushed? */
 		if (cf->tid == flush_tid)
fs/ceph/mds_client.c +45 -2

68 lines not on this page · raw message

fs/ceph/super.h +2 -0
index 272c287836c1..5de1e5923b63 100644
@@ -238,6 +238,7 @@ struct ceph_cap_flush {
 	bool is_capsnap; /* true means capsnap */
 	struct list_head g_list; // global
 	struct list_head i_list; // per inode
+	struct ceph_inode_info *ci;
 };
 
 /*
@@ -443,6 +444,7 @@ struct ceph_inode_info {
 	struct ceph_snap_context *i_head_snapc;  /* set if wr_buffer_head > 0 or
 						    dirty|flushing caps */
 	unsigned i_snap_caps;           /* cap bits for snapped files */
+	u64 i_last_cap_flush_ack;		/* latest cap flush_ack tid for this inode */
 
 	unsigned long i_last_rd;
 	unsigned long i_last_wr;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 02/10] ceph: add bounded timeout and diagnostics to wait_caps_flush()

Quoted 25 lines · Alex Markuze

Convert wait_caps_flush() from an unbounded wait into a bounded loop that periodically dumps outstanding flush state when progress stalls. This preserves the original data-integrity semantics while making hung cap flushes diagnosable during reset development and validation.

---
 fs/ceph/caps.c       |  7 +++++++
 fs/ceph/mds_client.c | 47 ++++++++++++++++++++++++++++++++++++++++++--
 fs/ceph/super.h      |  2 ++
 3 files changed, 54 insertions(+), 2 deletions(-)
fs/ceph/caps.c +1 -0
index d51454e995a8..be030fb8e864 100644
@@ -1648,6 +1648,7 @@ static void __ceph_flush_snaps(struct ceph_inode_info *ci,
 
 		spin_lock(&mdsc->cap_dirty_lock);
 		capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
+		capsnap->cap_flush.ci = ci;
 		list_add_tail(&capsnap->cap_flush.g_list,
 			      &mdsc->cap_flush_list);
 		if (oldest_flush_tid == 0)

fs/ceph/caps.c struct ceph_cap_flush *ceph_alloc_cap_flush(void) · line 1852

Quoted 6 lines
fs/ceph/caps.c +3 -0
@@ -1846,6 +1847,9 @@ struct ceph_cap_flush *ceph_alloc_cap_flush(void)
 		return NULL;
 
 	cf->is_capsnap = false;
+	cf->ci = NULL;
+	cf->tid = 0;
+	cf->wake = false;

It looks like cf->caps needs initialization too. Am I correct? The dump_cap_flushes() unconditionally calls ceph_cap_string(cf->caps).

Quoted 12 lines
fs/ceph/caps.c +1 -0
 	return cf;
 }
 
@@ -1931,6 +1935,7 @@ static u64 __mark_caps_flushing(struct inode *inode,
 	doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode));
 
 	swap(cf, ci->i_prealloc_cap_flush);
+	cf->ci = ci;
 	cf->caps = flushing;
 	cf->wake = wake;
 

fs/ceph/caps.c static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, · line 3834

Quoted 4 lines
fs/ceph/caps.c +1 -0
@@ -3826,6 +3831,8 @@ static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
 	bool wake_ci = false;
 	bool wake_mdsc = false;
 
+	ci->i_last_cap_flush_ack = flush_tid;

Do we have race condition here with dump_cap_flushes()? Maybe, READ_ONCE/WRITE_ONCE pair make sense?

Quoted 26 lines
fs/ceph/caps.c +1 -0
+
 	list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
 		/* Is this the one that was flushed? */
 		if (cf->tid == flush_tid)
fs/ceph/mds_client.c +7 -0
index 12f1b57d4b1c..d70c0b669b75 100644
@@ -27,6 +27,8 @@
 #include <trace/events/ceph.h>
 
 #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
+#define CEPH_CAP_FLUSH_WAIT_TIMEOUT_SEC 60
+#define CEPH_CAP_FLUSH_MAX_DUMP_COUNT 5
 
 /*
  * A cluster of MDS (metadata server) daemons is responsible for
@@ -2285,6 +2287,40 @@ static int check_caps_flush(struct ceph_mds_client *mdsc,
 	return ret;
 }
 
+/*
+ * Dump pending cap flushes for diagnostic purposes.
+ *
+ * cf->ci is safe to dereference here: the inode remains alive while its
+ * ceph_cap_flush is on i_cap_flush_list, which holds a reference.

fs/ceph/mds_client.c static int check_caps_flush(struct ceph_mds_client *mdsc, · line 2304

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+ */
+static void dump_cap_flushes(struct ceph_mds_client *mdsc, u64 want_tid)
+{
+	struct ceph_client *cl = mdsc->fsc->client;
+	struct ceph_cap_flush *cf;
+
+	pr_info_client(cl, "still waiting for cap flushes through %llu:\n",
+		       want_tid);
+	spin_lock(&mdsc->cap_dirty_lock);
+	list_for_each_entry(cf, &mdsc->cap_flush_list, g_list) {

So, what's about inode lifetime here? The iteration is over the global cap_flush_list (via g_list), not i_cap_flush_list. Are we safe here?

Quoted 56 lines
fs/ceph/mds_client.c +28 -2
+		if (cf->tid > want_tid)
+			break;
+		if (!cf->ci) {
+			pr_info_client(cl,
+				       "(null ci) %s tid=%llu wake=%d%s\n",
+				       ceph_cap_string(cf->caps), cf->tid,
+				       cf->wake,
+				       cf->is_capsnap ? " is_capsnap" : "");
+			continue;
+		}
+		pr_info_client(cl, "%llx:%llx %s %llu %llu %d%s\n",
+			       ceph_vinop(&cf->ci->netfs.inode),
+			       ceph_cap_string(cf->caps), cf->tid,
+			       cf->ci->i_last_cap_flush_ack, cf->wake,
+			       cf->is_capsnap ? " is_capsnap" : "");
+	}
+	spin_unlock(&mdsc->cap_dirty_lock);
+}
+
 /*
  * flush all dirty inode data to disk.
  *
@@ -2294,11 +2330,18 @@ static void wait_caps_flush(struct ceph_mds_client *mdsc,
 			    u64 want_flush_tid)
 {
 	struct ceph_client *cl = mdsc->fsc->client;
+	int i = 0;
+	long ret;
 
 	doutc(cl, "want %llu\n", want_flush_tid);
 
-	wait_event(mdsc->cap_flushing_wq,
-		   check_caps_flush(mdsc, want_flush_tid));
+	do {
+		ret = wait_event_timeout(mdsc->cap_flushing_wq,
+			   check_caps_flush(mdsc, want_flush_tid),
+			   CEPH_CAP_FLUSH_WAIT_TIMEOUT_SEC * HZ);
+		if (ret == 0 && i++ < CEPH_CAP_FLUSH_MAX_DUMP_COUNT)
+			dump_cap_flushes(mdsc, want_flush_tid);
+	} while (ret == 0);
 
 	doutc(cl, "ok, flushed thru %llu\n", want_flush_tid);
 }
fs/ceph/super.h +1 -0
index 272c287836c1..5de1e5923b63 100644
@@ -238,6 +238,7 @@ struct ceph_cap_flush {
 	bool is_capsnap; /* true means capsnap */
 	struct list_head g_list; // global
 	struct list_head i_list; // per inode
+	struct ceph_inode_info *ci;
 };
 
 /*

fs/ceph/super.h struct ceph_inode_info { · line 447

Quoted 4 lines
fs/ceph/super.h +1 -0
@@ -443,6 +444,7 @@ struct ceph_inode_info {
 	struct ceph_snap_context *i_head_snapc;  /* set if wr_buffer_head > 0 or
 						    dirty|flushing caps */
 	unsigned i_snap_caps;           /* cap bits for snapped files */
+	u64 i_last_cap_flush_ack;		/* latest cap flush_ack tid for this inode */

It looks like i_last_cap_flush_ack is not initialized properly anywhere.

Thanks,
Slava.

Quoted 3 lines
fs/ceph/super.h +0 -0
 	unsigned long i_last_rd;
 	unsigned long i_last_wr;
author maintainer
#5 lore raw

PATCH 3/10 ceph: add client reset state and request gating

In reply to Alex Markuze

Add the client-side reset state tracking and the wait path used to block new metadata requests and new lock acquisitions while a reset is in progress. This wires the reset state into client init and shutdown so blocked callers are released cleanly when the mount is torn down.

---
 fs/ceph/locks.c      | 14 ++++++++
 fs/ceph/mds_client.c | 79 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/mds_client.h | 28 ++++++++++++++++
 3 files changed, 121 insertions(+)
fs/ceph/locks.c +14 -0
index 2f21574dfb99..e6f0c565e9d6 100644
@@ -281,6 +281,13 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 		return err;
 	}
 
+	/* Wait for reset to complete before acquiring new locks */
+	if (op == CEPH_MDS_OP_SETFILELOCK && !lock_is_unlock(fl)) {
+		err = ceph_mdsc_wait_for_reset(mdsc);
+		if (err)
+			return err;
+	}
+
 	if (lock_is_read(fl))
 		lock_cmd = CEPH_LOCK_SHARED;
 	else if (lock_is_write(fl))
@@ -340,6 +347,13 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 		return err;
 	}
 
+	/* Wait for reset to complete before acquiring new locks */
+	if (!lock_is_unlock(fl)) {
+		err = ceph_mdsc_wait_for_reset(mdsc);
+		if (err)
+			return err;
+	}
+
 	if (IS_SETLKW(cmd))
 		wait = 1;
 
fs/ceph/mds_client.c +79 -0

103 lines not on this page · raw message

fs/ceph/mds_client.h +28 -0
index e91a199d56fd..5cc22c109cfe 100644
@@ -74,6 +74,32 @@ struct ceph_fs_client;
 struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
+#define CEPH_CLIENT_RESET_REASON_LEN	64
+#define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
+
+enum ceph_client_reset_phase {
+	CEPH_CLIENT_RESET_IDLE = 0,
+	CEPH_CLIENT_RESET_QUIESCING,
+	CEPH_CLIENT_RESET_DRAINING,
+	CEPH_CLIENT_RESET_TEARDOWN,
+};
+
+struct ceph_client_reset_state {
+	spinlock_t lock;
+	u64 trigger_count;
+	u64 success_count;
+	u64 failure_count;
+	unsigned long last_start;
+	unsigned long last_finish;
+	int last_errno;
+	enum ceph_client_reset_phase phase;
+	bool inject_error;
+	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
+
+	/* Request blocking during reset */
+	wait_queue_head_t blocked_wq;
+	atomic_t blocked_requests;
+};
 
 struct ceph_mds_cap_match {
 	s64 uid;  /* default to MDS_AUTH_UID_ANY */
@@ -536,6 +562,7 @@ struct ceph_mds_client {
 	struct list_head  dentry_dir_leases; /* lru list */
 
 	struct ceph_client_metric metric;
+	struct ceph_client_reset_state reset_state;
 
 	spinlock_t		snapid_map_lock;
 	struct rb_root		snapid_map_tree;
@@ -563,6 +590,7 @@ extern const char *ceph_session_state_name(int s);
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
 extern void ceph_put_mds_session(struct ceph_mds_session *s);
+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);
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 03/10] ceph: add client reset state and request gating

Quoted 17 lines · Alex Markuze

Add the client-side reset state tracking and the wait path used to block new metadata requests and new lock acquisitions while a reset is in progress. This wires the reset state into client init and shutdown so blocked callers are released cleanly when the mount is torn down.

---
 fs/ceph/locks.c      | 14 ++++++++
 fs/ceph/mds_client.c | 79 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/mds_client.h | 28 ++++++++++++++++
 3 files changed, 121 insertions(+)
fs/ceph/locks.c +0 -0
index 2f21574dfb99..e6f0c565e9d6 100644

fs/ceph/locks.c int ceph_lock(struct file *file, int cmd, struct file_lock *fl) · line 286

Quoted 6 lines
fs/ceph/locks.c +3 -0
@@ -281,6 +281,13 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 		return err;
 	}
 
+	/* Wait for reset to complete before acquiring new locks */
+	if (op == CEPH_MDS_OP_SETFILELOCK && !lock_is_unlock(fl)) {
+		err = ceph_mdsc_wait_for_reset(mdsc);

Maybe, I am missing something. But I don't see where mdsc variable has been declared and initialized in ceph_lock(). Is it my misunderstanding?

Quoted 8 lines
fs/ceph/locks.c +4 -0
+		if (err)
+			return err;
+	}
+
 	if (lock_is_read(fl))
 		lock_cmd = CEPH_LOCK_SHARED;
 	else if (lock_is_write(fl))

fs/ceph/locks.c int ceph_flock(struct file *file, int cmd, struct file_lock *fl) · line 352

Quoted 6 lines
fs/ceph/locks.c +3 -0
@@ -340,6 +347,13 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 		return err;
 	}
 
+	/* Wait for reset to complete before acquiring new locks */
+	if (!lock_is_unlock(fl)) {
+		err = ceph_mdsc_wait_for_reset(mdsc);

Ditto. The same misunderstanding here.

Thanks,
Slava.

Quoted 170 lines
fs/ceph/locks.c +4 -0
+		if (err)
+			return err;
+	}
+
 	if (IS_SETLKW(cmd))
 		wait = 1;
 
fs/ceph/mds_client.c +79 -0
index d70c0b669b75..0dd9a529850a 100644
@@ -3742,12 +3742,69 @@ static void kick_requests(struct ceph_mds_client *mdsc, int mds)
 	}
 }
 
+/*
+ * Wait for an active reset to complete.
+ * Returns 0 if reset completed successfully or no reset was active.
+ * Returns -ETIMEDOUT if we timed out waiting.
+ * Returns -ERESTARTSYS if interrupted by signal.
+ */
+int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_client *cl = mdsc->fsc->client;
+	long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
+	int blocked_count;
+	int ret;
+
+	if (READ_ONCE(st->phase) == CEPH_CLIENT_RESET_IDLE)
+		return 0;
+
+	blocked_count = atomic_inc_return(&st->blocked_requests);
+	doutc(cl, "request blocked during reset, %d total blocked\n",
+	      blocked_count);
+
+	ret = wait_event_interruptible_timeout(st->blocked_wq,
+					       READ_ONCE(st->phase) ==
+					       CEPH_CLIENT_RESET_IDLE,
+					       timeout);
+
+	atomic_dec(&st->blocked_requests);
+
+	if (ret == 0) {
+		pr_warn_client(cl, "timed out waiting for reset to complete\n");
+		return -ETIMEDOUT;
+	}
+	if (ret < 0)
+		return ret;
+
+	spin_lock(&st->lock);
+	ret = st->last_errno;
+	spin_unlock(&st->lock);
+	return ret;
+}
+
 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 			      struct ceph_mds_request *req)
 {
 	struct ceph_client *cl = mdsc->fsc->client;
 	int err = 0;
 
+	/*
+	 * If a reset is in progress, wait for it to complete.
+	 *
+	 * This is best-effort: a request can pass this check just
+	 * before the phase leaves IDLE and proceed concurrently with
+	 * reset. That is acceptable because such requests will either
+	 * complete normally or fail and be retried by the caller, and
+	 * adding lock serialization here would penalize every request
+	 * for a rare manual operation.
+	 */
+	err = ceph_mdsc_wait_for_reset(mdsc);
+	if (err) {
+		doutc(cl, "wait_for_reset failed: %d\n", err);
+		return err;
+	}
+
 	/* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
 	if (req->r_inode)
 		ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
@@ -5628,6 +5685,19 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	INIT_LIST_HEAD(&mdsc->dentry_leases);
 	INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
 
+	spin_lock_init(&mdsc->reset_state.lock);
+	mdsc->reset_state.trigger_count = 0;
+	mdsc->reset_state.success_count = 0;
+	mdsc->reset_state.failure_count = 0;
+	mdsc->reset_state.last_start = 0;
+	mdsc->reset_state.last_finish = 0;
+	mdsc->reset_state.last_errno = 0;
+	mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+	mdsc->reset_state.inject_error = false;
+	mdsc->reset_state.last_reason[0] = '\0';
+	init_waitqueue_head(&mdsc->reset_state.blocked_wq);
+	atomic_set(&mdsc->reset_state.blocked_requests, 0);
+
 	ceph_caps_init(mdsc);
 	ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
 
@@ -6153,6 +6223,15 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	/* flush out any connection work with references to us */
 	ceph_msgr_flush();
 
+	spin_lock(&mdsc->reset_state.lock);
+	if (mdsc->reset_state.phase != CEPH_CLIENT_RESET_IDLE) {
+		mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+		mdsc->reset_state.last_errno = -ESHUTDOWN;
+		mdsc->reset_state.failure_count++;
+	}
+	spin_unlock(&mdsc->reset_state.lock);
+	wake_up_all(&mdsc->reset_state.blocked_wq);
+
 	ceph_mdsc_stop(mdsc);
 
 	ceph_metric_destroy(&mdsc->metric);
fs/ceph/mds_client.h +28 -0
index e91a199d56fd..5cc22c109cfe 100644
@@ -74,6 +74,32 @@ struct ceph_fs_client;
 struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
+#define CEPH_CLIENT_RESET_REASON_LEN	64
+#define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
+
+enum ceph_client_reset_phase {
+	CEPH_CLIENT_RESET_IDLE = 0,
+	CEPH_CLIENT_RESET_QUIESCING,
+	CEPH_CLIENT_RESET_DRAINING,
+	CEPH_CLIENT_RESET_TEARDOWN,
+};
+
+struct ceph_client_reset_state {
+	spinlock_t lock;
+	u64 trigger_count;
+	u64 success_count;
+	u64 failure_count;
+	unsigned long last_start;
+	unsigned long last_finish;
+	int last_errno;
+	enum ceph_client_reset_phase phase;
+	bool inject_error;
+	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
+
+	/* Request blocking during reset */
+	wait_queue_head_t blocked_wq;
+	atomic_t blocked_requests;
+};
 
 struct ceph_mds_cap_match {
 	s64 uid;  /* default to MDS_AUTH_UID_ANY */
@@ -536,6 +562,7 @@ struct ceph_mds_client {
 	struct list_head  dentry_dir_leases; /* lru list */
 
 	struct ceph_client_metric metric;
+	struct ceph_client_reset_state reset_state;
 
 	spinlock_t		snapid_map_lock;
 	struct rb_root		snapid_map_tree;
@@ -563,6 +590,7 @@ extern const char *ceph_session_state_name(int s);
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
 extern void ceph_put_mds_session(struct ceph_mds_session *s);
+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);
author maintainer
#7 lore raw

Re: [PATCH 03/10] ceph: add client reset state and request gating

I'll cleanup some of the dead code the patchset and send v2.

Quoted 220 lines · Viacheslav Dubeyko
Quoted 17 lines · Alex Markuze

Add the client-side reset state tracking and the wait path used to block new metadata requests and new lock acquisitions while a reset is in progress. This wires the reset state into client init and shutdown so blocked callers are released cleanly when the mount is torn down.

---
 fs/ceph/locks.c      | 14 ++++++++
 fs/ceph/mds_client.c | 79 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ceph/mds_client.h | 28 ++++++++++++++++
 3 files changed, 121 insertions(+)
fs/ceph/locks.c +0 -0
index 2f21574dfb99..e6f0c565e9d6 100644

fs/ceph/locks.c int ceph_lock(struct file *file, int cmd, struct file_lock *fl) · line 286

Quoted 6 lines
fs/ceph/locks.c +3 -0
@@ -281,6 +281,13 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
              return err;
      }
 
+     /* Wait for reset to complete before acquiring new locks */
+     if (op == CEPH_MDS_OP_SETFILELOCK && !lock_is_unlock(fl)) {
+             err = ceph_mdsc_wait_for_reset(mdsc);

Maybe, I am missing something. But I don't see where mdsc variable has been declared and initialized in ceph_lock(). Is it my misunderstanding?

Quoted 8 lines
fs/ceph/locks.c +4 -0
+             if (err)
+                     return err;
+     }
+
      if (lock_is_read(fl))
              lock_cmd = CEPH_LOCK_SHARED;
      else if (lock_is_write(fl))

fs/ceph/locks.c int ceph_flock(struct file *file, int cmd, struct file_lock *fl) · line 352

Quoted 6 lines
fs/ceph/locks.c +3 -0
@@ -340,6 +347,13 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
              return err;
      }
 
+     /* Wait for reset to complete before acquiring new locks */
+     if (!lock_is_unlock(fl)) {
+             err = ceph_mdsc_wait_for_reset(mdsc);

Ditto. The same misunderstanding here.

Thanks,
Slava.

Quoted 170 lines
fs/ceph/locks.c +4 -0
+             if (err)
+                     return err;
+     }
+
      if (IS_SETLKW(cmd))
              wait = 1;
 
fs/ceph/mds_client.c +79 -0
index d70c0b669b75..0dd9a529850a 100644
@@ -3742,12 +3742,69 @@ static void kick_requests(struct ceph_mds_client *mdsc, int mds)
      }
 }
 
+/*
+ * Wait for an active reset to complete.
+ * Returns 0 if reset completed successfully or no reset was active.
+ * Returns -ETIMEDOUT if we timed out waiting.
+ * Returns -ERESTARTSYS if interrupted by signal.
+ */
+int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
+{
+     struct ceph_client_reset_state *st = &mdsc->reset_state;
+     struct ceph_client *cl = mdsc->fsc->client;
+     long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
+     int blocked_count;
+     int ret;
+
+     if (READ_ONCE(st->phase) == CEPH_CLIENT_RESET_IDLE)
+             return 0;
+
+     blocked_count = atomic_inc_return(&st->blocked_requests);
+     doutc(cl, "request blocked during reset, %d total blocked\n",
+           blocked_count);
+
+     ret = wait_event_interruptible_timeout(st->blocked_wq,
+                                            READ_ONCE(st->phase) ==
+                                            CEPH_CLIENT_RESET_IDLE,
+                                            timeout);
+
+     atomic_dec(&st->blocked_requests);
+
+     if (ret == 0) {
+             pr_warn_client(cl, "timed out waiting for reset to complete\n");
+             return -ETIMEDOUT;
+     }
+     if (ret < 0)
+             return ret;
+
+     spin_lock(&st->lock);
+     ret = st->last_errno;
+     spin_unlock(&st->lock);
+     return ret;
+}
+
 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
                            struct ceph_mds_request *req)
 {
      struct ceph_client *cl = mdsc->fsc->client;
      int err = 0;
 
+     /*
+      * If a reset is in progress, wait for it to complete.
+      *
+      * This is best-effort: a request can pass this check just
+      * before the phase leaves IDLE and proceed concurrently with
+      * reset. That is acceptable because such requests will either
+      * complete normally or fail and be retried by the caller, and
+      * adding lock serialization here would penalize every request
+      * for a rare manual operation.
+      */
+     err = ceph_mdsc_wait_for_reset(mdsc);
+     if (err) {
+             doutc(cl, "wait_for_reset failed: %d\n", err);
+             return err;
+     }
+
      /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
      if (req->r_inode)
              ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
@@ -5628,6 +5685,19 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
      INIT_LIST_HEAD(&mdsc->dentry_leases);
      INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
 
+     spin_lock_init(&mdsc->reset_state.lock);
+     mdsc->reset_state.trigger_count = 0;
+     mdsc->reset_state.success_count = 0;
+     mdsc->reset_state.failure_count = 0;
+     mdsc->reset_state.last_start = 0;
+     mdsc->reset_state.last_finish = 0;
+     mdsc->reset_state.last_errno = 0;
+     mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+     mdsc->reset_state.inject_error = false;
+     mdsc->reset_state.last_reason[0] = '\0';
+     init_waitqueue_head(&mdsc->reset_state.blocked_wq);
+     atomic_set(&mdsc->reset_state.blocked_requests, 0);
+
      ceph_caps_init(mdsc);
      ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
 
@@ -6153,6 +6223,15 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
      /* flush out any connection work with references to us */
      ceph_msgr_flush();
 
+     spin_lock(&mdsc->reset_state.lock);
+     if (mdsc->reset_state.phase != CEPH_CLIENT_RESET_IDLE) {
+             mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+             mdsc->reset_state.last_errno = -ESHUTDOWN;
+             mdsc->reset_state.failure_count++;
+     }
+     spin_unlock(&mdsc->reset_state.lock);
+     wake_up_all(&mdsc->reset_state.blocked_wq);
+
      ceph_mdsc_stop(mdsc);
 
      ceph_metric_destroy(&mdsc->metric);
fs/ceph/mds_client.h +28 -0
index e91a199d56fd..5cc22c109cfe 100644
@@ -74,6 +74,32 @@ struct ceph_fs_client;
 struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
+#define CEPH_CLIENT_RESET_REASON_LEN 64
+#define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
+
+enum ceph_client_reset_phase {
+     CEPH_CLIENT_RESET_IDLE = 0,
+     CEPH_CLIENT_RESET_QUIESCING,
+     CEPH_CLIENT_RESET_DRAINING,
+     CEPH_CLIENT_RESET_TEARDOWN,
+};
+
+struct ceph_client_reset_state {
+     spinlock_t lock;
+     u64 trigger_count;
+     u64 success_count;
+     u64 failure_count;
+     unsigned long last_start;
+     unsigned long last_finish;
+     int last_errno;
+     enum ceph_client_reset_phase phase;
+     bool inject_error;
+     char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
+
+     /* Request blocking during reset */
+     wait_queue_head_t blocked_wq;
+     atomic_t blocked_requests;
+};
 
 struct ceph_mds_cap_match {
      s64 uid;  /* default to MDS_AUTH_UID_ANY */
@@ -536,6 +562,7 @@ struct ceph_mds_client {
      struct list_head  dentry_dir_leases; /* lru list */
 
      struct ceph_client_metric metric;
+     struct ceph_client_reset_state reset_state;
 
      spinlock_t              snapid_map_lock;
      struct rb_root          snapid_map_tree;
@@ -563,6 +590,7 @@ extern const char *ceph_session_state_name(int s);
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
 extern void ceph_put_mds_session(struct ceph_mds_session *s);
+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);
author maintainer
#8 lore raw

PATCH 4/10 ceph: add manual reset debugfs control and trace plumbing

In reply to Alex Markuze

Add the debugfs trigger, status, and fault-injection files together with the tracepoints and reset scheduling hooks used to observe and drive the manual reset flow. This wires the reset bookkeeping into client init and shutdown while keeping the actual reconnect-based worker logic in a follow-up patch.

---
 fs/ceph/debugfs.c           | 168 +++++++++++++++++++++++++++++++++---
 fs/ceph/locks.c             |   2 +
 fs/ceph/mds_client.c        | 116 ++++++++++++++++++++++---
 fs/ceph/mds_client.h        |  20 +++--
 fs/ceph/super.h             |   4 +
 include/trace/events/ceph.h |  60 +++++++++++++
 6 files changed, 340 insertions(+), 30 deletions(-)
fs/ceph/debugfs.c +158 -10
index f3fe786b4143..9e2c32fb1f38 100644
@@ -9,6 +9,8 @@
 #include <linux/seq_file.h>
 #include <linux/math64.h>
 #include <linux/ktime.h>
+#include <linux/uaccess.h>
+#include <linux/kstrtox.h>
 
 #include <linux/ceph/libceph.h>
 #include <linux/ceph/mon_client.h>
@@ -22,6 +24,10 @@
 #include "mds_client.h"
 #include "metric.h"
 
+#define CEPH_DEBUGFS_MODE_READONLY 0400
+#define CEPH_DEBUGFS_MODE_WRITEONLY 0200
+#define CEPH_DEBUGFS_MODE_READWRITE 0600
+
 static int mdsmap_show(struct seq_file *s, void *p)
 {
 	int i;
@@ -360,16 +366,140 @@ static int status_show(struct seq_file *s, void *p)
 	return 0;
 }
 
+static int reset_status_show(struct seq_file *s, void *p)
+{
+	struct ceph_fs_client *fsc = s->private;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	struct ceph_client_reset_state *st;
+	u64 trigger = 0, success = 0, failure = 0;
+	unsigned long last_start = 0, last_finish = 0;
+	int last_errno = 0;
+	bool in_progress = false;
+	bool inject_error = false;
+	int pending_reconnects = 0;
+	int blocked_requests = 0;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+
+	if (!mdsc)
+		return 0;
+
+	st = &mdsc->reset_state;
+
+	spin_lock(&st->lock);
+	trigger = st->trigger_count;
+	success = st->success_count;
+	failure = st->failure_count;
+	last_start = st->last_start;
+	last_finish = st->last_finish;
+	last_errno = st->last_errno;
+	in_progress = st->in_progress;
+	inject_error = st->inject_error;
+	strscpy(reason, st->last_reason, sizeof(reason));
+	spin_unlock(&st->lock);
+
+	pending_reconnects = atomic_read(&st->pending_reconnects);
+	blocked_requests = atomic_read(&st->blocked_requests);
+
+	seq_printf(s, "in_progress: %s\n", in_progress ? "yes" : "no");
+	seq_printf(s, "trigger_count: %llu\n", trigger);
+	seq_printf(s, "success_count: %llu\n", success);
+	seq_printf(s, "failure_count: %llu\n", failure);
+	if (last_start)
+		seq_printf(s, "last_start_ms_ago: %u\n",
+			   jiffies_to_msecs(jiffies - last_start));
+	else
+		seq_puts(s, "last_start_ms_ago: (never)\n");
+	if (last_finish)
+		seq_printf(s, "last_finish_ms_ago: %u\n",
+			   jiffies_to_msecs(jiffies - last_finish));
+	else
+		seq_puts(s, "last_finish_ms_ago: (never)\n");
+	seq_printf(s, "last_errno: %d\n", last_errno);
+	seq_printf(s, "last_reason: %s\n",
+		   reason[0] ? reason : "(none)");
+	seq_printf(s, "inject_error_pending: %s\n",
+		   inject_error ? "yes" : "no");
+	seq_printf(s, "pending_reconnects: %d\n", pending_reconnects);
+	seq_printf(s, "blocked_requests: %d\n", blocked_requests);
+
+	return 0;
+}
+
+static ssize_t reset_trigger_write(struct file *file, const char __user *buf,
+				   size_t len, loff_t *ppos)
+{
+	struct ceph_fs_client *fsc = file->private_data;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+	size_t copy;
+	int ret;
+
+	if (!mdsc)
+		return -ENODEV;
+
+	copy = min_t(size_t, len, sizeof(reason) - 1);
+	if (copy && copy_from_user(reason, buf, copy))
+		return -EFAULT;
+	reason[copy] = '\0';
+	strim(reason);
+
+	ret = ceph_mdsc_schedule_reset(mdsc, reason);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t reset_inject_error_write(struct file *file,
+					const char __user *buf,
+					size_t len, loff_t *ppos)
+{
+	struct ceph_fs_client *fsc = file->private_data;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	struct ceph_client_reset_state *st;
+	bool enable;
+	int ret;
+
+	if (!mdsc)
+		return -ENODEV;
+
+	ret = kstrtobool_from_user(buf, len, &enable);
+	if (ret)
+		return ret;
+
+	st = &mdsc->reset_state;
+	spin_lock(&st->lock);
+	st->inject_error = enable;
+	spin_unlock(&st->lock);
+
+	return len;
+}
+
 DEFINE_SHOW_ATTRIBUTE(mdsmap);
 DEFINE_SHOW_ATTRIBUTE(mdsc);
 DEFINE_SHOW_ATTRIBUTE(caps);
 DEFINE_SHOW_ATTRIBUTE(mds_sessions);
 DEFINE_SHOW_ATTRIBUTE(status);
+DEFINE_SHOW_ATTRIBUTE(reset_status);
 DEFINE_SHOW_ATTRIBUTE(metrics_file);
 DEFINE_SHOW_ATTRIBUTE(metrics_latency);
 DEFINE_SHOW_ATTRIBUTE(metrics_size);
 DEFINE_SHOW_ATTRIBUTE(metrics_caps);
 
+static const struct file_operations ceph_reset_trigger_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = reset_trigger_write,
+	.llseek = noop_llseek,
+};
+
+static const struct file_operations ceph_reset_inject_error_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = reset_inject_error_write,
+	.llseek = noop_llseek,
+};
+
 
 /*
  * debugfs
@@ -404,6 +534,7 @@ void ceph_fs_debugfs_cleanup(struct ceph_fs_client *fsc)
 	debugfs_remove(fsc->debugfs_caps);
 	debugfs_remove(fsc->debugfs_status);
 	debugfs_remove(fsc->debugfs_mdsc);
+	debugfs_remove_recursive(fsc->debugfs_reset_dir);
 	debugfs_remove_recursive(fsc->debugfs_metrics_dir);
 	doutc(fsc->client, "done\n");
 }
@@ -415,7 +546,7 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 	doutc(fsc->client, "begin\n");
 	fsc->debugfs_congestion_kb =
 		debugfs_create_file("writeback_congestion_kb",
-				    0600,
+				    CEPH_DEBUGFS_MODE_READWRITE,
 				    fsc->client->debugfs_dir,
 				    fsc,
 				    &congestion_kb_fops);
@@ -428,31 +559,48 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 				       name);
 
 	fsc->debugfs_mdsmap = debugfs_create_file("mdsmap",
-					0400,
+					CEPH_DEBUGFS_MODE_READONLY,
 					fsc->client->debugfs_dir,
 					fsc,
 					&mdsmap_fops);
 
 	fsc->debugfs_mds_sessions = debugfs_create_file("mds_sessions",
-					0400,
+					CEPH_DEBUGFS_MODE_READONLY,
 					fsc->client->debugfs_dir,
 					fsc,
 					&mds_sessions_fops);
 
 	fsc->debugfs_mdsc = debugfs_create_file("mdsc",
-						0400,
+						CEPH_DEBUGFS_MODE_READONLY,
 						fsc->client->debugfs_dir,
 						fsc,
 						&mdsc_fops);
 
 	fsc->debugfs_caps = debugfs_create_file("caps",
-						0400,
+						CEPH_DEBUGFS_MODE_READONLY,
 						fsc->client->debugfs_dir,
 						fsc,
 						&caps_fops);
 
+	fsc->debugfs_reset_dir = debugfs_create_dir("reset",
+						    fsc->client->debugfs_dir);
+	if (fsc->debugfs_reset_dir) {
+		fsc->debugfs_reset_trigger =
+			debugfs_create_file("trigger", CEPH_DEBUGFS_MODE_WRITEONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &ceph_reset_trigger_fops);
+		fsc->debugfs_reset_status =
+			debugfs_create_file("status", CEPH_DEBUGFS_MODE_READONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &reset_status_fops);
+		fsc->debugfs_reset_inject =
+			debugfs_create_file("inject_error", CEPH_DEBUGFS_MODE_WRITEONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &ceph_reset_inject_error_fops);
+	}
+
 	fsc->debugfs_status = debugfs_create_file("status",
-						  0400,
+						  CEPH_DEBUGFS_MODE_READONLY,
 						  fsc->client->debugfs_dir,
 						  fsc,
 						  &status_fops);
@@ -460,13 +608,13 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 	fsc->debugfs_metrics_dir = debugfs_create_dir("metrics",
 						      fsc->client->debugfs_dir);
 
-	debugfs_create_file("file", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("file", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_file_fops);
-	debugfs_create_file("latency", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("latency", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_latency_fops);
-	debugfs_create_file("size", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("size", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_size_fops);
-	debugfs_create_file("caps", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("caps", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_caps_fops);
 	doutc(fsc->client, "done\n");
 }
fs/ceph/locks.c +2 -0
index e6f0c565e9d6..079947a5e42a 100644
@@ -251,6 +251,7 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 {
 	struct inode *inode = file_inode(file);
 	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	int err = 0;
 	u16 op = CEPH_MDS_OP_SETFILELOCK;
@@ -324,6 +325,7 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 {
 	struct inode *inode = file_inode(file);
 	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	int err = 0;
 	u8 wait = 0;
fs/ceph/mds_client.c +104 -12

179 lines not on this page · raw message

fs/ceph/mds_client.h +12 -8
index 5cc22c109cfe..c8269df1a9e5 100644
@@ -75,15 +75,9 @@ struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
 #define CEPH_CLIENT_RESET_REASON_LEN	64
+#define CEPH_CLIENT_RESET_TIMEOUT_SEC	60
 #define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
 
-enum ceph_client_reset_phase {
-	CEPH_CLIENT_RESET_IDLE = 0,
-	CEPH_CLIENT_RESET_QUIESCING,
-	CEPH_CLIENT_RESET_DRAINING,
-	CEPH_CLIENT_RESET_TEARDOWN,
-};
-
 struct ceph_client_reset_state {
 	spinlock_t lock;
 	u64 trigger_count;
@@ -92,10 +86,15 @@ struct ceph_client_reset_state {
 	unsigned long last_start;
 	unsigned long last_finish;
 	int last_errno;
-	enum ceph_client_reset_phase phase;
+	bool in_progress;
 	bool inject_error;
 	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
 
+	/* Completion tracking for session reconnects */
+	u64 active_reset_gen;
+	atomic_t pending_reconnects;
+	struct completion reconnect_done;
+
 	/* Request blocking during reset */
 	wait_queue_head_t blocked_wq;
 	atomic_t blocked_requests;
@@ -277,6 +276,8 @@ struct ceph_mds_session {
 	struct list_head  s_waiting;  /* waiting requests */
 	struct list_head  s_unsafe;   /* unsafe requests */
 	struct xarray	  s_delegated_inos;
+
+	u64		  s_reset_gen; /* generation of reset that initiated reconnect */
 };
 
 /*
@@ -562,6 +563,7 @@ struct ceph_mds_client {
 	struct list_head  dentry_dir_leases; /* lru list */
 
 	struct ceph_client_metric metric;
+	struct work_struct	reset_work;
 	struct ceph_client_reset_state reset_state;
 
 	spinlock_t		snapid_map_lock;
@@ -590,6 +592,8 @@ extern const char *ceph_session_state_name(int s);
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
 extern void ceph_put_mds_session(struct ceph_mds_session *s);
+int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
+			     const char *reason);
 int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc);
 
 extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
fs/ceph/super.h +4 -0
index 5de1e5923b63..6c403eef6492 100644
@@ -179,6 +179,10 @@ struct ceph_fs_client {
 	struct dentry *debugfs_status;
 	struct dentry *debugfs_mds_sessions;
 	struct dentry *debugfs_metrics_dir;
+	struct dentry *debugfs_reset_dir;
+	struct dentry *debugfs_reset_trigger;
+	struct dentry *debugfs_reset_status;
+	struct dentry *debugfs_reset_inject;
 #endif
 
 #ifdef CONFIG_CEPH_FSCACHE
include/trace/events/ceph.h +60 -0

66 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH 04/10] ceph: add manual reset debugfs control and trace plumbing

Quoted 63 lines · Alex Markuze

Add the debugfs trigger, status, and fault-injection files together with the tracepoints and reset scheduling hooks used to observe and drive the manual reset flow. This wires the reset bookkeeping into client init and shutdown while keeping the actual reconnect-based worker logic in a follow-up patch.

---
 fs/ceph/debugfs.c           | 168 +++++++++++++++++++++++++++++++++---
 fs/ceph/locks.c             |   2 +
 fs/ceph/mds_client.c        | 116 ++++++++++++++++++++++---
 fs/ceph/mds_client.h        |  20 +++--
 fs/ceph/super.h             |   4 +
 include/trace/events/ceph.h |  60 +++++++++++++
 6 files changed, 340 insertions(+), 30 deletions(-)
fs/ceph/debugfs.c +25 -0
index f3fe786b4143..9e2c32fb1f38 100644
@@ -9,6 +9,8 @@
 #include <linux/seq_file.h>
 #include <linux/math64.h>
 #include <linux/ktime.h>
+#include <linux/uaccess.h>
+#include <linux/kstrtox.h>
 
 #include <linux/ceph/libceph.h>
 #include <linux/ceph/mon_client.h>
@@ -22,6 +24,10 @@
 #include "mds_client.h"
 #include "metric.h"
 
+#define CEPH_DEBUGFS_MODE_READONLY 0400
+#define CEPH_DEBUGFS_MODE_WRITEONLY 0200
+#define CEPH_DEBUGFS_MODE_READWRITE 0600
+
 static int mdsmap_show(struct seq_file *s, void *p)
 {
 	int i;
@@ -360,16 +366,140 @@ static int status_show(struct seq_file *s, void *p)
 	return 0;
 }
 
+static int reset_status_show(struct seq_file *s, void *p)
+{
+	struct ceph_fs_client *fsc = s->private;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	struct ceph_client_reset_state *st;
+	u64 trigger = 0, success = 0, failure = 0;
+	unsigned long last_start = 0, last_finish = 0;
+	int last_errno = 0;
+	bool in_progress = false;
+	bool inject_error = false;
+	int pending_reconnects = 0;
+	int blocked_requests = 0;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+
+	if (!mdsc)
+		return 0;
+
+	st = &mdsc->reset_state;
+

fs/ceph/debugfs.c static int status_show(struct seq_file *s, void *p) · line 397

Quoted 10 lines
fs/ceph/debugfs.c +10 -0
+	spin_lock(&st->lock);
+	trigger = st->trigger_count;
+	success = st->success_count;
+	failure = st->failure_count;
+	last_start = st->last_start;
+	last_finish = st->last_finish;
+	last_errno = st->last_errno;
+	in_progress = st->in_progress;
+	inject_error = st->inject_error;
+	strscpy(reason, st->last_reason, sizeof(reason));

It is used sizeof(reason) here. It looks like the copying a memory blob but not a string. Why not used the memcpy() then. :)

Quoted 149 lines
fs/ceph/debugfs.c +100 -4
+	spin_unlock(&st->lock);
+
+	pending_reconnects = atomic_read(&st->pending_reconnects);
+	blocked_requests = atomic_read(&st->blocked_requests);
+
+	seq_printf(s, "in_progress: %s\n", in_progress ? "yes" : "no");
+	seq_printf(s, "trigger_count: %llu\n", trigger);
+	seq_printf(s, "success_count: %llu\n", success);
+	seq_printf(s, "failure_count: %llu\n", failure);
+	if (last_start)
+		seq_printf(s, "last_start_ms_ago: %u\n",
+			   jiffies_to_msecs(jiffies - last_start));
+	else
+		seq_puts(s, "last_start_ms_ago: (never)\n");
+	if (last_finish)
+		seq_printf(s, "last_finish_ms_ago: %u\n",
+			   jiffies_to_msecs(jiffies - last_finish));
+	else
+		seq_puts(s, "last_finish_ms_ago: (never)\n");
+	seq_printf(s, "last_errno: %d\n", last_errno);
+	seq_printf(s, "last_reason: %s\n",
+		   reason[0] ? reason : "(none)");
+	seq_printf(s, "inject_error_pending: %s\n",
+		   inject_error ? "yes" : "no");
+	seq_printf(s, "pending_reconnects: %d\n", pending_reconnects);
+	seq_printf(s, "blocked_requests: %d\n", blocked_requests);
+
+	return 0;
+}
+
+static ssize_t reset_trigger_write(struct file *file, const char __user *buf,
+				   size_t len, loff_t *ppos)
+{
+	struct ceph_fs_client *fsc = file->private_data;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+	size_t copy;
+	int ret;
+
+	if (!mdsc)
+		return -ENODEV;
+
+	copy = min_t(size_t, len, sizeof(reason) - 1);
+	if (copy && copy_from_user(reason, buf, copy))
+		return -EFAULT;
+	reason[copy] = '\0';
+	strim(reason);
+
+	ret = ceph_mdsc_schedule_reset(mdsc, reason);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t reset_inject_error_write(struct file *file,
+					const char __user *buf,
+					size_t len, loff_t *ppos)
+{
+	struct ceph_fs_client *fsc = file->private_data;
+	struct ceph_mds_client *mdsc = fsc->mdsc;
+	struct ceph_client_reset_state *st;
+	bool enable;
+	int ret;
+
+	if (!mdsc)
+		return -ENODEV;
+
+	ret = kstrtobool_from_user(buf, len, &enable);
+	if (ret)
+		return ret;
+
+	st = &mdsc->reset_state;
+	spin_lock(&st->lock);
+	st->inject_error = enable;
+	spin_unlock(&st->lock);
+
+	return len;
+}
+
 DEFINE_SHOW_ATTRIBUTE(mdsmap);
 DEFINE_SHOW_ATTRIBUTE(mdsc);
 DEFINE_SHOW_ATTRIBUTE(caps);
 DEFINE_SHOW_ATTRIBUTE(mds_sessions);
 DEFINE_SHOW_ATTRIBUTE(status);
+DEFINE_SHOW_ATTRIBUTE(reset_status);
 DEFINE_SHOW_ATTRIBUTE(metrics_file);
 DEFINE_SHOW_ATTRIBUTE(metrics_latency);
 DEFINE_SHOW_ATTRIBUTE(metrics_size);
 DEFINE_SHOW_ATTRIBUTE(metrics_caps);
 
+static const struct file_operations ceph_reset_trigger_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = reset_trigger_write,
+	.llseek = noop_llseek,
+};
+
+static const struct file_operations ceph_reset_inject_error_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = reset_inject_error_write,
+	.llseek = noop_llseek,
+};
+
 
 /*
  * debugfs
@@ -404,6 +534,7 @@ void ceph_fs_debugfs_cleanup(struct ceph_fs_client *fsc)
 	debugfs_remove(fsc->debugfs_caps);
 	debugfs_remove(fsc->debugfs_status);
 	debugfs_remove(fsc->debugfs_mdsc);
+	debugfs_remove_recursive(fsc->debugfs_reset_dir);
 	debugfs_remove_recursive(fsc->debugfs_metrics_dir);
 	doutc(fsc->client, "done\n");
 }
@@ -415,7 +546,7 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 	doutc(fsc->client, "begin\n");
 	fsc->debugfs_congestion_kb =
 		debugfs_create_file("writeback_congestion_kb",
-				    0600,
+				    CEPH_DEBUGFS_MODE_READWRITE,
 				    fsc->client->debugfs_dir,
 				    fsc,
 				    &congestion_kb_fops);
@@ -428,31 +559,48 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 				       name);
 
 	fsc->debugfs_mdsmap = debugfs_create_file("mdsmap",
-					0400,
+					CEPH_DEBUGFS_MODE_READONLY,
 					fsc->client->debugfs_dir,
 					fsc,
 					&mdsmap_fops);
 
 	fsc->debugfs_mds_sessions = debugfs_create_file("mds_sessions",
-					0400,
+					CEPH_DEBUGFS_MODE_READONLY,
 					fsc->client->debugfs_dir,
 					fsc,
 					&mds_sessions_fops);
 
 	fsc->debugfs_mdsc = debugfs_create_file("mdsc",
-						0400,
+						CEPH_DEBUGFS_MODE_READONLY,
 						fsc->client->debugfs_dir,
 						fsc,
 						&mdsc_fops);
 

fs/ceph/debugfs.c void ceph_fs_debugfs_init(struct ceph_fs_client *fsc) · line 587

Quoted 10 lines
fs/ceph/debugfs.c +4 -1
 	fsc->debugfs_caps = debugfs_create_file("caps",
-						0400,
+						CEPH_DEBUGFS_MODE_READONLY,
 						fsc->client->debugfs_dir,
 						fsc,
 						&caps_fops);
 
+	fsc->debugfs_reset_dir = debugfs_create_dir("reset",
+						    fsc->client->debugfs_dir);
+	if (fsc->debugfs_reset_dir) {

Is it correct check? The debugfs_create_dir returns ERR_PTR(-ENODEV) (never NULL) when debugfs is disabled.

Maybe, !IS_ERR(fsc->debugfs_reset_dir)?

Quoted 43 lines
fs/ceph/debugfs.c +19 -5
+		fsc->debugfs_reset_trigger =
+			debugfs_create_file("trigger", CEPH_DEBUGFS_MODE_WRITEONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &ceph_reset_trigger_fops);
+		fsc->debugfs_reset_status =
+			debugfs_create_file("status", CEPH_DEBUGFS_MODE_READONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &reset_status_fops);
+		fsc->debugfs_reset_inject =
+			debugfs_create_file("inject_error", CEPH_DEBUGFS_MODE_WRITEONLY,
+					    fsc->debugfs_reset_dir, fsc,
+					    &ceph_reset_inject_error_fops);
+	}
+
 	fsc->debugfs_status = debugfs_create_file("status",
-						  0400,
+						  CEPH_DEBUGFS_MODE_READONLY,
 						  fsc->client->debugfs_dir,
 						  fsc,
 						  &status_fops);
@@ -460,13 +608,13 @@ void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
 	fsc->debugfs_metrics_dir = debugfs_create_dir("metrics",
 						      fsc->client->debugfs_dir);
 
-	debugfs_create_file("file", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("file", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_file_fops);
-	debugfs_create_file("latency", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("latency", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_latency_fops);
-	debugfs_create_file("size", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("size", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_size_fops);
-	debugfs_create_file("caps", 0400, fsc->debugfs_metrics_dir, fsc,
+	debugfs_create_file("caps", CEPH_DEBUGFS_MODE_READONLY, fsc->debugfs_metrics_dir, fsc,
 			    &metrics_caps_fops);
 	doutc(fsc->client, "done\n");
 }
fs/ceph/locks.c +0 -0
index e6f0c565e9d6..079947a5e42a 100644

fs/ceph/locks.c int ceph_lock(struct file *file, int cmd, struct file_lock *fl) · line 254

Quoted 4 lines
fs/ceph/locks.c +1 -0
@@ -251,6 +251,7 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 {
 	struct inode *inode = file_inode(file);
 	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);

This declaration is needed for previous patch. So, previous patch could be not possible to compile.

fs/ceph/locks.c int ceph_flock(struct file *file, int cmd, struct file_lock *fl) · line 328

Quoted 8 lines
fs/ceph/locks.c +1 -0
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	int err = 0;
 	u16 op = CEPH_MDS_OP_SETFILELOCK;
@@ -324,6 +325,7 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 {
 	struct inode *inode = file_inode(file);
 	struct ceph_inode_info *ci = ceph_inode(inode);
+	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);

Ditto.

Quoted 110 lines
fs/ceph/locks.c +0 -0
 	struct ceph_client *cl = ceph_inode_to_client(inode);
 	int err = 0;
 	u8 wait = 0;
fs/ceph/mds_client.c +65 -4
index 0dd9a529850a..4b5adc38f7b7 100644
@@ -67,6 +67,7 @@ static void __wake_requests(struct ceph_mds_client *mdsc,
 			    struct list_head *head);
 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 const struct ceph_connection_operations mds_con_ops;
 
@@ -3756,33 +3757,119 @@ int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
 	int blocked_count;
 	int ret;
 
-	if (READ_ONCE(st->phase) == CEPH_CLIENT_RESET_IDLE)
+	if (!READ_ONCE(st->in_progress))
 		return 0;
 
 	blocked_count = atomic_inc_return(&st->blocked_requests);
 	doutc(cl, "request blocked during reset, %d total blocked\n",
 	      blocked_count);
+	trace_ceph_client_reset_blocked(mdsc, blocked_count);
 
 	ret = wait_event_interruptible_timeout(st->blocked_wq,
-					       READ_ONCE(st->phase) ==
-					       CEPH_CLIENT_RESET_IDLE,
+					       !READ_ONCE(st->in_progress),
 					       timeout);
 
 	atomic_dec(&st->blocked_requests);
 
 	if (ret == 0) {
 		pr_warn_client(cl, "timed out waiting for reset to complete\n");
+		trace_ceph_client_reset_unblocked(mdsc, -ETIMEDOUT);
 		return -ETIMEDOUT;
 	}
-	if (ret < 0)
+	if (ret < 0) {
+		trace_ceph_client_reset_unblocked(mdsc, ret);
 		return ret;
+	}
 
 	spin_lock(&st->lock);
 	ret = st->last_errno;
 	spin_unlock(&st->lock);
+	trace_ceph_client_reset_unblocked(mdsc, ret);
 	return ret;
 }
 
+static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+
+	spin_lock(&st->lock);
+	st->last_finish = jiffies;
+	st->last_errno = ret;
+	st->in_progress = false;
+	if (ret)
+		st->failure_count++;
+	else
+		st->success_count++;
+	spin_unlock(&st->lock);
+
+	wake_up_all(&st->blocked_wq);
+	trace_ceph_client_reset_complete(mdsc, ret);
+}
+
+static void ceph_mdsc_reset_workfn(struct work_struct *work)
+{
+	struct ceph_mds_client *mdsc =
+		container_of(work, struct ceph_mds_client, reset_work);
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	bool inject_error;
+	int ret = 0;
+
+	spin_lock(&st->lock);
+	inject_error = st->inject_error;
+	if (inject_error)
+		st->inject_error = false;
+	spin_unlock(&st->lock);
+
+	if (inject_error)
+		ret = -EIO;
+
+	ceph_mdsc_reset_complete(mdsc, ret);
+}
+
+int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
+			     const char *reason)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_fs_client *fsc = mdsc->fsc;
+	const char *msg = (reason && reason[0]) ? reason : "manual";
+	int mount_state;
+
+	mount_state = READ_ONCE(fsc->mount_state);
+	if (mount_state != CEPH_MOUNT_MOUNTED) {
+		pr_warn_client(fsc->client,
+			       "reset rejected: mount_state=%d (not mounted)\n",
+			       mount_state);
+		return -EINVAL;
+	}
+
+	spin_lock(&st->lock);
+	if (st->in_progress) {

fs/ceph/mds_client.c int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc) · line 3856

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+		spin_unlock(&st->lock);
+		return -EBUSY;
+	}
+
+	st->in_progress = true;
+	st->active_reset_gen++;
+	st->last_start = jiffies;
+	st->last_errno = 0;
+	st->trigger_count++;
+	strscpy(st->last_reason, msg, sizeof(st->last_reason));

Ditto.

fs/ceph/mds_client.c int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc) · line 3863

Quoted 7 lines
fs/ceph/mds_client.c +7 -0
+	spin_unlock(&st->lock);
+
+	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
+		spin_lock(&st->lock);
+		st->in_progress = false;
+		st->last_errno = -EALREADY;
+		st->failure_count++;

Should we set st->last_finish here?

st->last_finish = jiffies;

Quoted 51 lines
fs/ceph/mds_client.c +19 -7
+		spin_unlock(&st->lock);
+		wake_up_all(&st->blocked_wq);
+		return -EALREADY;
+	}
+
+	trace_ceph_client_reset_schedule(mdsc, msg);
+	return 0;
+}
+
 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 			      struct ceph_mds_request *req)
 {
@@ -3793,11 +3880,11 @@ int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 	 * If a reset is in progress, wait for it to complete.
 	 *
 	 * This is best-effort: a request can pass this check just
-	 * before the phase leaves IDLE and proceed concurrently with
-	 * reset. That is acceptable because such requests will either
-	 * complete normally or fail and be retried by the caller, and
-	 * adding lock serialization here would penalize every request
-	 * for a rare manual operation.
+	 * before in_progress is set and proceed concurrently with
+	 * reset.  That is acceptable because (a) such requests will
+	 * either complete normally or fail and be retried by the
+	 * caller, and (b) adding lock serialization here would
+	 * penalize every request for a rare manual operation.
 	 */
 	err = ceph_mdsc_wait_for_reset(mdsc);
 	if (err) {
@@ -5692,11 +5779,15 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	mdsc->reset_state.last_start = 0;
 	mdsc->reset_state.last_finish = 0;
 	mdsc->reset_state.last_errno = 0;
-	mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+	mdsc->reset_state.in_progress = false;
 	mdsc->reset_state.inject_error = false;
+	mdsc->reset_state.active_reset_gen = 0;
 	mdsc->reset_state.last_reason[0] = '\0';
+	atomic_set(&mdsc->reset_state.pending_reconnects, 0);
+	init_completion(&mdsc->reset_state.reconnect_done);
 	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);
 
 	ceph_caps_init(mdsc);
 	ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
@@ -6224,14 +6315,15 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	ceph_msgr_flush();
 
 	spin_lock(&mdsc->reset_state.lock);
-	if (mdsc->reset_state.phase != CEPH_CLIENT_RESET_IDLE) {

fs/ceph/mds_client.c void ceph_mdsc_destroy(struct ceph_fs_client *fsc) · line 6326

Quoted 10 lines
fs/ceph/mds_client.c +3 -1
-		mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
+	if (mdsc->reset_state.in_progress) {
+		mdsc->reset_state.in_progress = false;
 		mdsc->reset_state.last_errno = -ESHUTDOWN;
 		mdsc->reset_state.failure_count++;
 	}
 	spin_unlock(&mdsc->reset_state.lock);
 	wake_up_all(&mdsc->reset_state.blocked_wq);
 
+	cancel_work_sync(&mdsc->reset_work);

Could we have a race condition between ceph_mdsc_destroy() and the running work function?

The destroy path:
1a. Acquires spin_lock
1b. Sets in_progress = false
1c. Increments failure_count
1d. Releases lock
2. Calls wake_up_all()
3. Calls cancel_work_sync()

If ceph_mdsc_reset_workfn is already running at step 1, it will call ceph_mdsc_reset_complete(mdsc, 0) which overwrites last_errno (0 instead of - ESHUTDOWN), increments success_count, and sets last_finish.

Should cancel_work_sync() be called before?

cancel_work_sync(&mdsc->reset_work);

spin_lock(&mdsc->reset_state.lock);
<logic>
spin_unlock(&mdsc->reset_state.lock);

Quoted 42 lines
fs/ceph/mds_client.c +0 -0
 	ceph_mdsc_stop(mdsc);
 
 	ceph_metric_destroy(&mdsc->metric);
fs/ceph/mds_client.h +7 -8
index 5cc22c109cfe..c8269df1a9e5 100644
@@ -75,15 +75,9 @@ struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
 #define CEPH_CLIENT_RESET_REASON_LEN	64
+#define CEPH_CLIENT_RESET_TIMEOUT_SEC	60
 #define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
 
-enum ceph_client_reset_phase {
-	CEPH_CLIENT_RESET_IDLE = 0,
-	CEPH_CLIENT_RESET_QUIESCING,
-	CEPH_CLIENT_RESET_DRAINING,
-	CEPH_CLIENT_RESET_TEARDOWN,
-};
-
 struct ceph_client_reset_state {
 	spinlock_t lock;
 	u64 trigger_count;
@@ -92,10 +86,15 @@ struct ceph_client_reset_state {
 	unsigned long last_start;
 	unsigned long last_finish;
 	int last_errno;
-	enum ceph_client_reset_phase phase;
+	bool in_progress;
 	bool inject_error;
 	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
 
+	/* Completion tracking for session reconnects */
+	u64 active_reset_gen;
+	atomic_t pending_reconnects;
+	struct completion reconnect_done;
+
 	/* Request blocking during reset */
 	wait_queue_head_t blocked_wq;
 	atomic_t blocked_requests;

fs/ceph/mds_client.h struct ceph_mds_session { · line 280

Quoted 5 lines
fs/ceph/mds_client.h +2 -0
@@ -277,6 +276,8 @@ struct ceph_mds_session {
 	struct list_head  s_waiting;  /* waiting requests */
 	struct list_head  s_unsafe;   /* unsafe requests */
 	struct xarray	  s_delegated_inos;
+
+	u64		  s_reset_gen; /* generation of reset that initiated reconnect */

The s_reset_gen declared but never used in this patch. Will it be used in other patches?

Thanks,
Slava.

Quoted 106 lines
fs/ceph/mds_client.h +3 -0
 };
 
 /*
@@ -562,6 +563,7 @@ struct ceph_mds_client {
 	struct list_head  dentry_dir_leases; /* lru list */
 
 	struct ceph_client_metric metric;
+	struct work_struct	reset_work;
 	struct ceph_client_reset_state reset_state;
 
 	spinlock_t		snapid_map_lock;
@@ -590,6 +592,8 @@ extern const char *ceph_session_state_name(int s);
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
 extern void ceph_put_mds_session(struct ceph_mds_session *s);
+int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
+			     const char *reason);
 int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc);
 
 extern int ceph_mdsc_init(struct ceph_fs_client *fsc);
fs/ceph/super.h +4 -0
index 5de1e5923b63..6c403eef6492 100644
@@ -179,6 +179,10 @@ struct ceph_fs_client {
 	struct dentry *debugfs_status;
 	struct dentry *debugfs_mds_sessions;
 	struct dentry *debugfs_metrics_dir;
+	struct dentry *debugfs_reset_dir;
+	struct dentry *debugfs_reset_trigger;
+	struct dentry *debugfs_reset_status;
+	struct dentry *debugfs_reset_inject;
 #endif
 
 #ifdef CONFIG_CEPH_FSCACHE
include/trace/events/ceph.h +60 -0
index 08cb0659fbfc..22b755f4c7c7 100644
@@ -226,6 +226,66 @@ TRACE_EVENT(ceph_handle_caps,
 		  __entry->mseq)
 );
 
+/*
+ * Client reset tracepoints
+ */
+TRACE_EVENT(ceph_client_reset_schedule,
+	TP_PROTO(const struct ceph_mds_client *mdsc, const char *reason),
+	TP_ARGS(mdsc, reason),
+	TP_STRUCT__entry(
+		__field(const void *, mdsc)
+		__string(reason, reason ? reason : "")
+	),
+	TP_fast_assign(
+		__entry->mdsc = mdsc;
+		__assign_str(reason);
+	),
+	TP_printk("mdsc=%p reason=%s", __entry->mdsc, __get_str(reason))
+);
+
+TRACE_EVENT(ceph_client_reset_complete,
+	TP_PROTO(const struct ceph_mds_client *mdsc, int ret),
+	TP_ARGS(mdsc, ret),
+	TP_STRUCT__entry(
+		__field(const void *, mdsc)
+		__field(int, ret)
+	),
+	TP_fast_assign(
+		__entry->mdsc = mdsc;
+		__entry->ret = ret;
+	),
+	TP_printk("mdsc=%p ret=%d", __entry->mdsc, __entry->ret)
+);
+
+TRACE_EVENT(ceph_client_reset_blocked,
+	TP_PROTO(const struct ceph_mds_client *mdsc, int blocked_count),
+	TP_ARGS(mdsc, blocked_count),
+	TP_STRUCT__entry(
+		__field(const void *, mdsc)
+		__field(int, blocked_count)
+	),
+	TP_fast_assign(
+		__entry->mdsc = mdsc;
+		__entry->blocked_count = blocked_count;
+	),
+	TP_printk("mdsc=%p blocked_count=%d", __entry->mdsc,
+		  __entry->blocked_count)
+);
+
+TRACE_EVENT(ceph_client_reset_unblocked,
+	TP_PROTO(const struct ceph_mds_client *mdsc, int ret),
+	TP_ARGS(mdsc, ret),
+	TP_STRUCT__entry(
+		__field(const void *, mdsc)
+		__field(int, ret)
+	),
+	TP_fast_assign(
+		__entry->mdsc = mdsc;
+		__entry->ret = ret;
+	),
+	TP_printk("mdsc=%p ret=%d", __entry->mdsc, __entry->ret)
+);
+
 #undef EM
 #undef E_
 #endif /* _TRACE_CEPH_H */
author maintainer
#10 lore raw

PATCH 5/10 ceph: implement reconnect-based manual reset worker

In reply to Alex Markuze

Add the reconnect-based reset worker that snapshots eligible sessions, tracks reconnect completion, and wires reset-driven reconnect handling through the session state machine. This keeps the initial manual reset path functional before the later patches switch recovery over to direct session teardown.

---
 fs/ceph/mds_client.c | 534 +++++++++++++++++++++++++++++++------------
 1 file changed, 394 insertions(+), 140 deletions(-)
fs/ceph/mds_client.c +394 -140
index 4b5adc38f7b7..5a2a9e473058 100644
@@ -12,6 +12,7 @@
 #include <linux/bits.h>
 #include <linux/ktime.h>
 #include <linux/bitmap.h>
+#include <linux/workqueue.h>
 #include <linux/mnt_idmapping.h>
 
 #include "super.h"
@@ -61,6 +62,7 @@ struct ceph_reconnect_state {
 	struct ceph_pagelist *pagelist;
 	unsigned msg_version;
 	bool allow_multi;
+	bool from_reset;
 };
 
 static void __wake_requests(struct ceph_mds_client *mdsc,
@@ -68,6 +70,8 @@ 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_reconnect_session_done(struct ceph_mds_client *mdsc,
+					     struct ceph_mds_session *session);
 
 static const struct ceph_connection_operations mds_con_ops;
 
@@ -3743,133 +3747,6 @@ static void kick_requests(struct ceph_mds_client *mdsc, int mds)
 	}
 }
 
-/*
- * Wait for an active reset to complete.
- * Returns 0 if reset completed successfully or no reset was active.
- * Returns -ETIMEDOUT if we timed out waiting.
- * Returns -ERESTARTSYS if interrupted by signal.
- */
-int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	struct ceph_client *cl = mdsc->fsc->client;
-	long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
-	int blocked_count;
-	int ret;
-
-	if (!READ_ONCE(st->in_progress))
-		return 0;
-
-	blocked_count = atomic_inc_return(&st->blocked_requests);
-	doutc(cl, "request blocked during reset, %d total blocked\n",
-	      blocked_count);
-	trace_ceph_client_reset_blocked(mdsc, blocked_count);
-
-	ret = wait_event_interruptible_timeout(st->blocked_wq,
-					       !READ_ONCE(st->in_progress),
-					       timeout);
-
-	atomic_dec(&st->blocked_requests);
-
-	if (ret == 0) {
-		pr_warn_client(cl, "timed out waiting for reset to complete\n");
-		trace_ceph_client_reset_unblocked(mdsc, -ETIMEDOUT);
-		return -ETIMEDOUT;
-	}
-	if (ret < 0) {
-		trace_ceph_client_reset_unblocked(mdsc, ret);
-		return ret;
-	}
-
-	spin_lock(&st->lock);
-	ret = st->last_errno;
-	spin_unlock(&st->lock);
-	trace_ceph_client_reset_unblocked(mdsc, ret);
-	return ret;
-}
-
-static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-
-	spin_lock(&st->lock);
-	st->last_finish = jiffies;
-	st->last_errno = ret;
-	st->in_progress = false;
-	if (ret)
-		st->failure_count++;
-	else
-		st->success_count++;
-	spin_unlock(&st->lock);
-
-	wake_up_all(&st->blocked_wq);
-	trace_ceph_client_reset_complete(mdsc, ret);
-}
-
-static void ceph_mdsc_reset_workfn(struct work_struct *work)
-{
-	struct ceph_mds_client *mdsc =
-		container_of(work, struct ceph_mds_client, reset_work);
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	bool inject_error;
-	int ret = 0;
-
-	spin_lock(&st->lock);
-	inject_error = st->inject_error;
-	if (inject_error)
-		st->inject_error = false;
-	spin_unlock(&st->lock);
-
-	if (inject_error)
-		ret = -EIO;
-
-	ceph_mdsc_reset_complete(mdsc, ret);
-}
-
-int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
-			     const char *reason)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	struct ceph_fs_client *fsc = mdsc->fsc;
-	const char *msg = (reason && reason[0]) ? reason : "manual";
-	int mount_state;
-
-	mount_state = READ_ONCE(fsc->mount_state);
-	if (mount_state != CEPH_MOUNT_MOUNTED) {
-		pr_warn_client(fsc->client,
-			       "reset rejected: mount_state=%d (not mounted)\n",
-			       mount_state);
-		return -EINVAL;
-	}
-
-	spin_lock(&st->lock);
-	if (st->in_progress) {
-		spin_unlock(&st->lock);
-		return -EBUSY;
-	}
-
-	st->in_progress = true;
-	st->active_reset_gen++;
-	st->last_start = jiffies;
-	st->last_errno = 0;
-	st->trigger_count++;
-	strscpy(st->last_reason, msg, sizeof(st->last_reason));
-	spin_unlock(&st->lock);
-
-	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
-		spin_lock(&st->lock);
-		st->in_progress = false;
-		st->last_errno = -EALREADY;
-		st->failure_count++;
-		spin_unlock(&st->lock);
-		wake_up_all(&st->blocked_wq);
-		return -EALREADY;
-	}
-
-	trace_ceph_client_reset_schedule(mdsc, msg);
-	return 0;
-}
-
 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 			      struct ceph_mds_request *req)
 {
@@ -4524,9 +4401,11 @@ static void handle_session(struct ceph_mds_session *session,
 
 	switch (op) {
 	case CEPH_SESSION_OPEN:
-		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
 			pr_info_client(cl, "mds%d reconnect success\n",
 				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 
 		session->s_features = features;
 		if (session->s_state == CEPH_MDS_SESSION_OPEN) {
@@ -4559,9 +4438,11 @@ static void handle_session(struct ceph_mds_session *session,
 		break;
 
 	case CEPH_SESSION_CLOSE:
-		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
 			pr_info_client(cl, "mds%d reconnect denied\n",
 				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 		session->s_state = CEPH_MDS_SESSION_CLOSED;
 		cleanup_session_requests(mdsc, session);
 		remove_session_caps(session);
@@ -4600,7 +4481,13 @@ static void handle_session(struct ceph_mds_session *session,
 		break;
 
 	case CEPH_SESSION_REJECT:
-		WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
+		WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING &&
+			session->s_state != CEPH_MDS_SESSION_RECONNECTING);
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
+			pr_info_client(cl, "mds%d reconnect rejected\n",
+				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 		pr_info_client(cl, "mds%d rejected session\n",
 			       session->s_mds);
 		session->s_state = CEPH_MDS_SESSION_REJECTED;
@@ -4862,6 +4749,17 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	cap->mseq = 0;       /* and migrate_seq */
 	cap->cap_gen = atomic_read(&cap->session->s_cap_gen);
 
+	/*
+	 * Don't set CEPH_I_ERROR_FILELOCK here - instead, we attempt to
+	 * reclaim locks by sending them in the reconnect message.
+	 * The MDS will do best-effort reclaim. If locks can't be reclaimed
+	 * (e.g., another client grabbed a conflicting lock), future lock
+	 * operations will fail and the error flag will be set then.
+	 *
+	 * Note: i_filelock_ref > 0 means we have locks to reclaim.
+	 * The lock info will be encoded below if flock_len is non-zero.
+	 */
+
 	/* These are lost when the session goes away */
 	if (S_ISDIR(inode->i_mode)) {
 		if (cap->issued & CEPH_CAP_DIR_CREATE) {
@@ -4877,8 +4775,22 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 		rec.v2.issued = cpu_to_le32(cap->issued);
 		rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
 		rec.v2.pathbase = cpu_to_le64(path_info.vino.ino);
-		rec.v2.flock_len = (__force __le32)
-			((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
+		if (recon_state->from_reset) {
+			/*
+			 * Reset-initiated reconnect: always try to reclaim
+			 * locks. The MDS does best-effort; failures are
+			 * handled on future lock operations.
+			 */
+			rec.v2.flock_len = cpu_to_le32(1);
+		} else {
+			/*
+			 * Normal reconnect: skip lock reclaim if locks
+			 * were already known to be in error state.
+			 */
+			rec.v2.flock_len = cpu_to_le32(
+				test_bit(CEPH_I_ERROR_FILELOCK_BIT,
+					 &ci->i_ceph_flags) ? 0 : 1);
+		}
 	} else {
 		struct timespec64 ts;
 
@@ -5075,8 +4987,9 @@ static int encode_snap_realms(struct ceph_mds_client *mdsc,
  *
  * This is a relatively heavyweight operation, but it's rare.
  */
-static void send_mds_reconnect(struct ceph_mds_client *mdsc,
-			       struct ceph_mds_session *session)
+static int send_mds_reconnect(struct ceph_mds_client *mdsc,
+			      struct ceph_mds_session *session,
+			      bool from_reset)
 {
 	struct ceph_client *cl = mdsc->fsc->client;
 	struct ceph_msg *reply;
@@ -5084,6 +4997,7 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 	int err = -ENOMEM;
 	struct ceph_reconnect_state recon_state = {
 		.session = session,
+		.from_reset = from_reset,
 	};
 	LIST_HEAD(dispose);
 
@@ -5100,6 +5014,30 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 	xa_destroy(&session->s_delegated_inos);
 
 	mutex_lock(&session->s_mutex);
+	if (session->s_state == CEPH_MDS_SESSION_CLOSED ||
+	    session->s_state == CEPH_MDS_SESSION_REJECTED) {
+		pr_info_client(cl, "mds%d skipping reconnect, session %s\n",
+			       mds,
+			       ceph_session_state_name(session->s_state));
+		mutex_unlock(&session->s_mutex);
+		ceph_msg_put(reply);
+		err = -ESTALE;
+		goto fail_nomsg;
+	}
+
+	mutex_lock(&mdsc->mutex);
+	if (mds >= mdsc->max_sessions || mdsc->sessions[mds] != session) {
+		mutex_unlock(&mdsc->mutex);
+		pr_info_client(cl,
+			       "mds%d skipping reconnect, session unregistered\n",
+			       mds);
+		mutex_unlock(&session->s_mutex);
+		ceph_msg_put(reply);
+		err = -ENOENT;
+		goto fail_nomsg;
+	}
+	mutex_unlock(&mdsc->mutex);
+
 	session->s_state = CEPH_MDS_SESSION_RECONNECTING;
 	session->s_seq = 0;
 
@@ -5229,7 +5167,7 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 
 	up_read(&mdsc->snap_rwsem);
 	ceph_pagelist_release(recon_state.pagelist);
-	return;
+	return 0;
 
 fail:
 	ceph_msg_put(reply);
@@ -5240,7 +5178,301 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 fail_nopagelist:
 	pr_err_client(cl, "error %d preparing reconnect for mds%d\n",
 		      err, mds);
-	return;
+	return err;
+}
+
+/*
+ * Called when a session completes reconnection (success or failure).
+ * Only counts toward reset completion if this session's reconnect was
+ * initiated by the currently active reset generation.  Late completions
+ * from a prior (timed-out) reset are silently ignored.
+ *
+ * Caller must hold session->s_mutex to serialize s_reset_gen updates.
+ */
+static void ceph_mdsc_reconnect_session_done(struct ceph_mds_client *mdsc,
+					     struct ceph_mds_session *session)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	u64 active_gen;
+
+	lockdep_assert_held(&session->s_mutex);
+
+	if (!session->s_reset_gen)
+		return;
+
+	spin_lock(&st->lock);
+	active_gen = st->active_reset_gen;
+	spin_unlock(&st->lock);
+
+	if (session->s_reset_gen != active_gen) {
+		session->s_reset_gen = 0;
+		return;
+	}
+
+	session->s_reset_gen = 0;
+
+	if (atomic_dec_and_test(&st->pending_reconnects))
+		complete(&st->reconnect_done);
+}
+
+/*
+ * Wait for a reset to complete if one is in progress.
+ * Returns 0 if reset completed successfully or wasn't in progress.
+ * Returns -ETIMEDOUT if we timed out waiting.
+ * Returns -ERESTARTSYS if interrupted by signal.
+ */
+int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_client *cl = mdsc->fsc->client;
+	long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
+	int blocked_count;
+	int ret;
+
+	if (!READ_ONCE(st->in_progress))
+		return 0;
+
+	blocked_count = atomic_inc_return(&st->blocked_requests);
+	doutc(cl, "request blocked during reset, %d total blocked\n",
+	      blocked_count);
+	trace_ceph_client_reset_blocked(mdsc, blocked_count);
+
+	ret = wait_event_interruptible_timeout(st->blocked_wq,
+					       !READ_ONCE(st->in_progress),
+					       timeout);
+
+	atomic_dec(&st->blocked_requests);
+
+	if (ret == 0) {
+		pr_warn_client(cl, "timed out waiting for reset to complete\n");
+		trace_ceph_client_reset_unblocked(mdsc, -ETIMEDOUT);
+		return -ETIMEDOUT;
+	}
+	if (ret < 0) {
+		trace_ceph_client_reset_unblocked(mdsc, ret);
+		return ret;  /* -ERESTARTSYS */
+	}
+
+	/*
+	 * Propagate the reset result to all blocked callers.  If the
+	 * reset failed, sessions may be in an inconsistent state, so
+	 * failing requests forces the operator to investigate rather
+	 * than silently proceeding with potentially stale state.
+	 */
+	spin_lock(&st->lock);
+	ret = st->last_errno;
+	spin_unlock(&st->lock);
+
+	trace_ceph_client_reset_unblocked(mdsc, ret);
+	return ret;
+}
+
+static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+
+	spin_lock(&st->lock);
+	st->last_finish = jiffies;
+	st->last_errno = ret;
+	st->in_progress = false;
+	if (ret)
+		st->failure_count++;
+	else
+		st->success_count++;
+	spin_unlock(&st->lock);
+
+	/* Wake up all requests that were blocked waiting for reset */
+	wake_up_all(&st->blocked_wq);
+
+	trace_ceph_client_reset_complete(mdsc, ret);
+}
+
+static void ceph_mdsc_reset_workfn(struct work_struct *work)
+{
+	struct ceph_mds_client *mdsc =
+		container_of(work, struct ceph_mds_client, reset_work);
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_client *cl = mdsc->fsc->client;
+	struct ceph_mds_session **sessions = NULL;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+	int max_sessions, i, n = 0;
+	bool inject_error;
+	u64 reset_gen;
+	int ret = 0;
+
+	spin_lock(&st->lock);
+	strscpy(reason, st->last_reason, sizeof(reason));
+	inject_error = st->inject_error;
+	if (inject_error)
+		st->inject_error = false;
+	reset_gen = st->active_reset_gen;
+	spin_unlock(&st->lock);
+
+	if (inject_error) {
+		ret = -EIO;
+		goto out_complete;
+	}
+
+	mutex_lock(&mdsc->mutex);
+	max_sessions = mdsc->max_sessions;
+	if (max_sessions <= 0) {
+		mutex_unlock(&mdsc->mutex);
+		goto out_complete;
+	}
+
+	sessions = kcalloc(max_sessions, sizeof(*sessions), GFP_NOFS);
+	if (!sessions) {
+		mutex_unlock(&mdsc->mutex);
+		ret = -ENOMEM;
+		pr_err_client(cl,
+			      "manual session reset failed to allocate session array\n");
+		ceph_mdsc_reset_complete(mdsc, ret);
+		return;
+	}
+
+	for (i = 0; i < max_sessions; i++) {
+		struct ceph_mds_session *session = mdsc->sessions[i];
+
+		if (!session)
+			continue;
+
+		/*
+		 * Read session state without s_mutex to avoid nesting
+		 * mdsc->mutex -> s_mutex, which would invert the
+		 * s_mutex -> mdsc->mutex order used by
+		 * cleanup_session_requests().  s_state is an int
+		 * so loads are atomic; send_mds_reconnect() will
+		 * reject sessions that transitioned to CLOSED or
+		 * REJECTED under s_mutex before proceeding.
+		 */
+		switch (READ_ONCE(session->s_state)) {
+		case CEPH_MDS_SESSION_OPEN:
+		case CEPH_MDS_SESSION_HUNG:
+		case CEPH_MDS_SESSION_OPENING:
+		case CEPH_MDS_SESSION_CLOSING:
+			sessions[n++] = ceph_get_mds_session(session);
+			break;
+		case CEPH_MDS_SESSION_RECONNECTING:
+			pr_info_client(cl,
+				       "mds%d already reconnecting, skipping\n",
+				       session->s_mds);
+			break;
+		default:
+			pr_info_client(cl,
+				       "mds%d in state %s, skipping reconnect\n",
+				       session->s_mds,
+				       ceph_session_state_name(session->s_state));
+			break;
+		}
+	}
+	mutex_unlock(&mdsc->mutex);
+
+	pr_info_client(cl,
+		       "manual session reset executing (sessions=%d, reason=\"%s\")\n",
+		       n, reason);
+
+	if (n == 0) {
+		kfree(sessions);
+		goto out_complete;
+	}
+
+	/* Initialize completion tracking */
+	reinit_completion(&st->reconnect_done);
+	atomic_set(&st->pending_reconnects, n);
+
+	for (i = 0; i < n; i++) {
+		int err;
+
+		mutex_lock(&sessions[i]->s_mutex);
+		sessions[i]->s_reset_gen = reset_gen;
+		mutex_unlock(&sessions[i]->s_mutex);
+
+		err = send_mds_reconnect(mdsc, sessions[i], true);
+		if (err) {
+			bool skipped = (err == -ESTALE || err == -ENOENT);
+
+			mutex_lock(&sessions[i]->s_mutex);
+			sessions[i]->s_reset_gen = 0;
+			mutex_unlock(&sessions[i]->s_mutex);
+
+			if (skipped) {
+				pr_info_client(cl,
+					       "mds%d reconnect skipped during reset: %d\n",
+					       sessions[i]->s_mds, err);
+			} else {
+				pr_err_client(cl,
+					      "mds%d reconnect failed: %d\n",
+					      sessions[i]->s_mds, err);
+				if (!ret)
+					ret = err;
+			}
+			if (atomic_dec_and_test(&st->pending_reconnects))
+				complete(&st->reconnect_done);
+		}
+		ceph_put_mds_session(sessions[i]);
+	}
+
+	kfree(sessions);
+
+	/* Wait for all sessions to complete reconnection */
+	if (!wait_for_completion_timeout(&st->reconnect_done,
+					 CEPH_CLIENT_RESET_TIMEOUT_SEC * HZ)) {
+		pr_warn_client(cl,
+			       "reset timed out waiting for %d sessions\n",
+			       atomic_read(&st->pending_reconnects));
+		if (!ret)
+			ret = -ETIMEDOUT;
+	}
+
+out_complete:
+	ceph_mdsc_reset_complete(mdsc, ret);
+}
+
+int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
+			     const char *reason)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_fs_client *fsc = mdsc->fsc;
+	const char *msg = (reason && reason[0]) ? reason : "manual";
+	int mount_state;
+
+	mount_state = READ_ONCE(fsc->mount_state);
+	if (mount_state != CEPH_MOUNT_MOUNTED) {
+		pr_warn_client(fsc->client,
+			       "reset rejected: mount_state=%d (not mounted)\n",
+			       mount_state);
+		return -EINVAL;
+	}
+
+	spin_lock(&st->lock);
+	if (st->in_progress) {
+		spin_unlock(&st->lock);
+		return -EBUSY;
+	}
+
+	st->in_progress = true;
+	st->active_reset_gen++;
+	st->last_start = jiffies;
+	st->last_errno = 0;
+	st->trigger_count++;
+	strscpy(st->last_reason, msg, sizeof(st->last_reason));
+	spin_unlock(&st->lock);
+
+	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
+		spin_lock(&st->lock);
+		st->in_progress = false;
+		st->last_errno = -EALREADY;
+		st->failure_count++;
+		spin_unlock(&st->lock);
+		wake_up_all(&st->blocked_wq);
+		return -EALREADY;
+	}
+
+	pr_info_client(mdsc->fsc->client,
+		       "manual session reset scheduled (reason=\"%s\")\n",
+		       msg);
+	trace_ceph_client_reset_schedule(mdsc, msg);
+	return 0;
 }
 
 
@@ -5321,9 +5553,15 @@ static void check_new_map(struct ceph_mds_client *mdsc,
 		 */
 		if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
 		    newstate >= CEPH_MDS_STATE_RECONNECT) {
+			int rc;
+
 			mutex_unlock(&mdsc->mutex);
 			clear_bit(i, targets);
-			send_mds_reconnect(mdsc, s);
+			rc = send_mds_reconnect(mdsc, s, false);
+			if (rc)
+				pr_err_client(cl,
+					      "mds%d reconnect failed: %d\n",
+					      i, rc);
 			mutex_lock(&mdsc->mutex);
 		}
 
@@ -5387,7 +5625,11 @@ static void check_new_map(struct ceph_mds_client *mdsc,
 		}
 		doutc(cl, "send reconnect to export target mds.%d\n", i);
 		mutex_unlock(&mdsc->mutex);
-		send_mds_reconnect(mdsc, s);
+		err = send_mds_reconnect(mdsc, s, false);
+		if (err)
+			pr_err_client(cl,
+				      "mds%d export target reconnect failed: %d\n",
+				      i, err);
 		ceph_put_mds_session(s);
 		mutex_lock(&mdsc->mutex);
 	}
@@ -6314,6 +6556,11 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	/* flush out any connection work with references to us */
 	ceph_msgr_flush();
 
+	/*
+	 * Mark reset as failed and wake any blocked waiters before
+	 * cancelling, so unmount doesn't stall on blocked_wq timeout
+	 * if cancel_work_sync() prevents the work from running.
+	 */
 	spin_lock(&mdsc->reset_state.lock);
 	if (mdsc->reset_state.in_progress) {
 		mdsc->reset_state.in_progress = false;
@@ -6498,8 +6745,13 @@ static void mds_peer_reset(struct ceph_connection *con)
 	pr_warn_client(mdsc->fsc->client, "mds%d closed our session\n",
 		       s->s_mds);
 	if (READ_ONCE(mdsc->fsc->mount_state) != CEPH_MOUNT_FENCE_IO &&
-	    ceph_mdsmap_get_state(mdsc->mdsmap, s->s_mds) >= CEPH_MDS_STATE_RECONNECT)
-		send_mds_reconnect(mdsc, s);
+	    ceph_mdsmap_get_state(mdsc->mdsmap, s->s_mds) >= CEPH_MDS_STATE_RECONNECT) {
+		int rc = send_mds_reconnect(mdsc, s, false);
+		if (rc)
+			pr_err_client(mdsc->fsc->client,
+				      "mds%d reconnect failed: %d\n",
+				      s->s_mds, rc);
+	}
 }
 
 static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
@@ -6511,6 +6763,8 @@ static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
 
 	mutex_lock(&mdsc->mutex);
 	if (__verify_registered_session(mdsc, s) < 0) {
+		pr_info_client(cl, "dropping tid %llu from unregistered session %d\n",
+			       msg->hdr.tid, s->s_mds);
 		mutex_unlock(&mdsc->mutex);
 		goto out;
 	}
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH 05/10] ceph: implement reconnect-based manual reset worker

Quoted 348 lines · Alex Markuze

Add the reconnect-based reset worker that snapshots eligible sessions, tracks reconnect completion, and wires reset-driven reconnect handling through the session state machine. This keeps the initial manual reset path functional before the later patches switch recovery over to direct session teardown.

---
 fs/ceph/mds_client.c | 534 +++++++++++++++++++++++++++++++------------
 1 file changed, 394 insertions(+), 140 deletions(-)
fs/ceph/mds_client.c +98 -136
index 4b5adc38f7b7..5a2a9e473058 100644
@@ -12,6 +12,7 @@
 #include <linux/bits.h>
 #include <linux/ktime.h>
 #include <linux/bitmap.h>
+#include <linux/workqueue.h>
 #include <linux/mnt_idmapping.h>
 
 #include "super.h"
@@ -61,6 +62,7 @@ struct ceph_reconnect_state {
 	struct ceph_pagelist *pagelist;
 	unsigned msg_version;
 	bool allow_multi;
+	bool from_reset;
 };
 
 static void __wake_requests(struct ceph_mds_client *mdsc,
@@ -68,6 +70,8 @@ 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_reconnect_session_done(struct ceph_mds_client *mdsc,
+					     struct ceph_mds_session *session);
 
 static const struct ceph_connection_operations mds_con_ops;
 
@@ -3743,133 +3747,6 @@ static void kick_requests(struct ceph_mds_client *mdsc, int mds)
 	}
 }
 
-/*
- * Wait for an active reset to complete.
- * Returns 0 if reset completed successfully or no reset was active.
- * Returns -ETIMEDOUT if we timed out waiting.
- * Returns -ERESTARTSYS if interrupted by signal.
- */
-int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	struct ceph_client *cl = mdsc->fsc->client;
-	long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
-	int blocked_count;
-	int ret;
-
-	if (!READ_ONCE(st->in_progress))
-		return 0;
-
-	blocked_count = atomic_inc_return(&st->blocked_requests);
-	doutc(cl, "request blocked during reset, %d total blocked\n",
-	      blocked_count);
-	trace_ceph_client_reset_blocked(mdsc, blocked_count);
-
-	ret = wait_event_interruptible_timeout(st->blocked_wq,
-					       !READ_ONCE(st->in_progress),
-					       timeout);
-
-	atomic_dec(&st->blocked_requests);
-
-	if (ret == 0) {
-		pr_warn_client(cl, "timed out waiting for reset to complete\n");
-		trace_ceph_client_reset_unblocked(mdsc, -ETIMEDOUT);
-		return -ETIMEDOUT;
-	}
-	if (ret < 0) {
-		trace_ceph_client_reset_unblocked(mdsc, ret);
-		return ret;
-	}
-
-	spin_lock(&st->lock);
-	ret = st->last_errno;
-	spin_unlock(&st->lock);
-	trace_ceph_client_reset_unblocked(mdsc, ret);
-	return ret;
-}
-
-static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-
-	spin_lock(&st->lock);
-	st->last_finish = jiffies;
-	st->last_errno = ret;
-	st->in_progress = false;
-	if (ret)
-		st->failure_count++;
-	else
-		st->success_count++;
-	spin_unlock(&st->lock);
-
-	wake_up_all(&st->blocked_wq);
-	trace_ceph_client_reset_complete(mdsc, ret);
-}
-
-static void ceph_mdsc_reset_workfn(struct work_struct *work)
-{
-	struct ceph_mds_client *mdsc =
-		container_of(work, struct ceph_mds_client, reset_work);
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	bool inject_error;
-	int ret = 0;
-
-	spin_lock(&st->lock);
-	inject_error = st->inject_error;
-	if (inject_error)
-		st->inject_error = false;
-	spin_unlock(&st->lock);
-
-	if (inject_error)
-		ret = -EIO;
-
-	ceph_mdsc_reset_complete(mdsc, ret);
-}
-
-int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
-			     const char *reason)
-{
-	struct ceph_client_reset_state *st = &mdsc->reset_state;
-	struct ceph_fs_client *fsc = mdsc->fsc;
-	const char *msg = (reason && reason[0]) ? reason : "manual";
-	int mount_state;
-
-	mount_state = READ_ONCE(fsc->mount_state);
-	if (mount_state != CEPH_MOUNT_MOUNTED) {
-		pr_warn_client(fsc->client,
-			       "reset rejected: mount_state=%d (not mounted)\n",
-			       mount_state);
-		return -EINVAL;
-	}
-
-	spin_lock(&st->lock);
-	if (st->in_progress) {
-		spin_unlock(&st->lock);
-		return -EBUSY;
-	}
-
-	st->in_progress = true;
-	st->active_reset_gen++;
-	st->last_start = jiffies;
-	st->last_errno = 0;
-	st->trigger_count++;
-	strscpy(st->last_reason, msg, sizeof(st->last_reason));
-	spin_unlock(&st->lock);
-
-	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
-		spin_lock(&st->lock);
-		st->in_progress = false;
-		st->last_errno = -EALREADY;
-		st->failure_count++;
-		spin_unlock(&st->lock);
-		wake_up_all(&st->blocked_wq);
-		return -EALREADY;
-	}
-
-	trace_ceph_client_reset_schedule(mdsc, msg);
-	return 0;
-}
-
 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 			      struct ceph_mds_request *req)
 {
@@ -4524,9 +4401,11 @@ static void handle_session(struct ceph_mds_session *session,
 
 	switch (op) {
 	case CEPH_SESSION_OPEN:
-		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
 			pr_info_client(cl, "mds%d reconnect success\n",
 				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 
 		session->s_features = features;
 		if (session->s_state == CEPH_MDS_SESSION_OPEN) {
@@ -4559,9 +4438,11 @@ static void handle_session(struct ceph_mds_session *session,
 		break;
 
 	case CEPH_SESSION_CLOSE:
-		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
 			pr_info_client(cl, "mds%d reconnect denied\n",
 				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 		session->s_state = CEPH_MDS_SESSION_CLOSED;
 		cleanup_session_requests(mdsc, session);
 		remove_session_caps(session);
@@ -4600,7 +4481,13 @@ static void handle_session(struct ceph_mds_session *session,
 		break;
 
 	case CEPH_SESSION_REJECT:
-		WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
+		WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING &&
+			session->s_state != CEPH_MDS_SESSION_RECONNECTING);
+		if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) {
+			pr_info_client(cl, "mds%d reconnect rejected\n",
+				       session->s_mds);
+			ceph_mdsc_reconnect_session_done(mdsc, session);
+		}
 		pr_info_client(cl, "mds%d rejected session\n",
 			       session->s_mds);
 		session->s_state = CEPH_MDS_SESSION_REJECTED;
@@ -4862,6 +4749,17 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 	cap->mseq = 0;       /* and migrate_seq */
 	cap->cap_gen = atomic_read(&cap->session->s_cap_gen);
 
+	/*
+	 * Don't set CEPH_I_ERROR_FILELOCK here - instead, we attempt to
+	 * reclaim locks by sending them in the reconnect message.
+	 * The MDS will do best-effort reclaim. If locks can't be reclaimed
+	 * (e.g., another client grabbed a conflicting lock), future lock
+	 * operations will fail and the error flag will be set then.
+	 *
+	 * Note: i_filelock_ref > 0 means we have locks to reclaim.
+	 * The lock info will be encoded below if flock_len is non-zero.
+	 */
+
 	/* These are lost when the session goes away */
 	if (S_ISDIR(inode->i_mode)) {
 		if (cap->issued & CEPH_CAP_DIR_CREATE) {
@@ -4877,8 +4775,22 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
 		rec.v2.issued = cpu_to_le32(cap->issued);
 		rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
 		rec.v2.pathbase = cpu_to_le64(path_info.vino.ino);
-		rec.v2.flock_len = (__force __le32)
-			((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
+		if (recon_state->from_reset) {
+			/*
+			 * Reset-initiated reconnect: always try to reclaim
+			 * locks. The MDS does best-effort; failures are
+			 * handled on future lock operations.
+			 */
+			rec.v2.flock_len = cpu_to_le32(1);
+		} else {
+			/*
+			 * Normal reconnect: skip lock reclaim if locks
+			 * were already known to be in error state.
+			 */
+			rec.v2.flock_len = cpu_to_le32(
+				test_bit(CEPH_I_ERROR_FILELOCK_BIT,
+					 &ci->i_ceph_flags) ? 0 : 1);
+		}
 	} else {
 		struct timespec64 ts;
 
@@ -5075,8 +4987,9 @@ static int encode_snap_realms(struct ceph_mds_client *mdsc,
  *
  * This is a relatively heavyweight operation, but it's rare.
  */
-static void send_mds_reconnect(struct ceph_mds_client *mdsc,
-			       struct ceph_mds_session *session)
+static int send_mds_reconnect(struct ceph_mds_client *mdsc,
+			      struct ceph_mds_session *session,
+			      bool from_reset)
 {
 	struct ceph_client *cl = mdsc->fsc->client;
 	struct ceph_msg *reply;
@@ -5084,6 +4997,7 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 	int err = -ENOMEM;
 	struct ceph_reconnect_state recon_state = {
 		.session = session,
+		.from_reset = from_reset,
 	};
 	LIST_HEAD(dispose);
 
@@ -5100,6 +5014,30 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 	xa_destroy(&session->s_delegated_inos);
 
 	mutex_lock(&session->s_mutex);
+	if (session->s_state == CEPH_MDS_SESSION_CLOSED ||
+	    session->s_state == CEPH_MDS_SESSION_REJECTED) {
+		pr_info_client(cl, "mds%d skipping reconnect, session %s\n",
+			       mds,
+			       ceph_session_state_name(session->s_state));
+		mutex_unlock(&session->s_mutex);
+		ceph_msg_put(reply);
+		err = -ESTALE;
+		goto fail_nomsg;
+	}
+
+	mutex_lock(&mdsc->mutex);
+	if (mds >= mdsc->max_sessions || mdsc->sessions[mds] != session) {
+		mutex_unlock(&mdsc->mutex);
+		pr_info_client(cl,
+			       "mds%d skipping reconnect, session unregistered\n",
+			       mds);
+		mutex_unlock(&session->s_mutex);
+		ceph_msg_put(reply);
+		err = -ENOENT;
+		goto fail_nomsg;
+	}
+	mutex_unlock(&mdsc->mutex);
+
 	session->s_state = CEPH_MDS_SESSION_RECONNECTING;
 	session->s_seq = 0;
 
@@ -5229,7 +5167,7 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 
 	up_read(&mdsc->snap_rwsem);
 	ceph_pagelist_release(recon_state.pagelist);
-	return;
+	return 0;
 
 fail:
 	ceph_msg_put(reply);
@@ -5240,7 +5178,301 @@ static void send_mds_reconnect(struct ceph_mds_client *mdsc,
 fail_nopagelist:
 	pr_err_client(cl, "error %d preparing reconnect for mds%d\n",
 		      err, mds);
-	return;
+	return err;
+}
+
+/*
+ * Called when a session completes reconnection (success or failure).
+ * Only counts toward reset completion if this session's reconnect was
+ * initiated by the currently active reset generation.  Late completions
+ * from a prior (timed-out) reset are silently ignored.
+ *
+ * Caller must hold session->s_mutex to serialize s_reset_gen updates.
+ */
+static void ceph_mdsc_reconnect_session_done(struct ceph_mds_client *mdsc,
+					     struct ceph_mds_session *session)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	u64 active_gen;
+
+	lockdep_assert_held(&session->s_mutex);
+
+	if (!session->s_reset_gen)
+		return;
+
+	spin_lock(&st->lock);
+	active_gen = st->active_reset_gen;
+	spin_unlock(&st->lock);

fs/ceph/mds_client.c static void send_mds_reconnect(struct ceph_mds_client *mdsc, · line 5215

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+
+	if (session->s_reset_gen != active_gen) {
+		session->s_reset_gen = 0;
+		return;
+	}
+
+	session->s_reset_gen = 0;
+
+	if (atomic_dec_and_test(&st->pending_reconnects))
+		complete(&st->reconnect_done);

What if somehow st->pending_reconnects never reach zero by incorrect increment/decrement logic?

Quoted 79 lines
fs/ceph/mds_client.c +79 -0
+}
+
+/*
+ * Wait for a reset to complete if one is in progress.
+ * Returns 0 if reset completed successfully or wasn't in progress.
+ * Returns -ETIMEDOUT if we timed out waiting.
+ * Returns -ERESTARTSYS if interrupted by signal.
+ */
+int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_client *cl = mdsc->fsc->client;
+	long timeout = CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC * HZ;
+	int blocked_count;
+	int ret;
+
+	if (!READ_ONCE(st->in_progress))
+		return 0;
+
+	blocked_count = atomic_inc_return(&st->blocked_requests);
+	doutc(cl, "request blocked during reset, %d total blocked\n",
+	      blocked_count);
+	trace_ceph_client_reset_blocked(mdsc, blocked_count);
+
+	ret = wait_event_interruptible_timeout(st->blocked_wq,
+					       !READ_ONCE(st->in_progress),
+					       timeout);
+
+	atomic_dec(&st->blocked_requests);
+
+	if (ret == 0) {
+		pr_warn_client(cl, "timed out waiting for reset to complete\n");
+		trace_ceph_client_reset_unblocked(mdsc, -ETIMEDOUT);
+		return -ETIMEDOUT;
+	}
+	if (ret < 0) {
+		trace_ceph_client_reset_unblocked(mdsc, ret);
+		return ret;  /* -ERESTARTSYS */
+	}
+
+	/*
+	 * Propagate the reset result to all blocked callers.  If the
+	 * reset failed, sessions may be in an inconsistent state, so
+	 * failing requests forces the operator to investigate rather
+	 * than silently proceeding with potentially stale state.
+	 */
+	spin_lock(&st->lock);
+	ret = st->last_errno;
+	spin_unlock(&st->lock);
+
+	trace_ceph_client_reset_unblocked(mdsc, ret);
+	return ret;
+}
+
+static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+
+	spin_lock(&st->lock);
+	st->last_finish = jiffies;
+	st->last_errno = ret;
+	st->in_progress = false;
+	if (ret)
+		st->failure_count++;
+	else
+		st->success_count++;
+	spin_unlock(&st->lock);
+
+	/* Wake up all requests that were blocked waiting for reset */
+	wake_up_all(&st->blocked_wq);
+
+	trace_ceph_client_reset_complete(mdsc, ret);
+}
+
+static void ceph_mdsc_reset_workfn(struct work_struct *work)
+{
+	struct ceph_mds_client *mdsc =
+		container_of(work, struct ceph_mds_client, reset_work);
+	struct ceph_client_reset_state *st = &mdsc->reset_state;

fs/ceph/mds_client.c static void send_mds_reconnect(struct ceph_mds_client *mdsc, · line 5304

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+	struct ceph_client *cl = mdsc->fsc->client;
+	struct ceph_mds_session **sessions = NULL;
+	char reason[CEPH_CLIENT_RESET_REASON_LEN];
+	int max_sessions, i, n = 0;
+	bool inject_error;
+	u64 reset_gen;
+	int ret = 0;
+
+	spin_lock(&st->lock);
+	strscpy(reason, st->last_reason, sizeof(reason));

It still looks like memory copy operation to me.

Quoted 39 lines
fs/ceph/mds_client.c +39 -0
+	inject_error = st->inject_error;
+	if (inject_error)
+		st->inject_error = false;
+	reset_gen = st->active_reset_gen;
+	spin_unlock(&st->lock);
+
+	if (inject_error) {
+		ret = -EIO;
+		goto out_complete;
+	}
+
+	mutex_lock(&mdsc->mutex);
+	max_sessions = mdsc->max_sessions;
+	if (max_sessions <= 0) {
+		mutex_unlock(&mdsc->mutex);
+		goto out_complete;
+	}
+
+	sessions = kcalloc(max_sessions, sizeof(*sessions), GFP_NOFS);
+	if (!sessions) {
+		mutex_unlock(&mdsc->mutex);
+		ret = -ENOMEM;
+		pr_err_client(cl,
+			      "manual session reset failed to allocate session array\n");
+		ceph_mdsc_reset_complete(mdsc, ret);
+		return;
+	}
+
+	for (i = 0; i < max_sessions; i++) {
+		struct ceph_mds_session *session = mdsc->sessions[i];
+
+		if (!session)
+			continue;
+
+		/*
+		 * Read session state without s_mutex to avoid nesting
+		 * mdsc->mutex -> s_mutex, which would invert the
+		 * s_mutex -> mdsc->mutex order used by
+		 * cleanup_session_requests().  s_state is an int

fs/ceph/mds_client.c static void send_mds_reconnect(struct ceph_mds_client *mdsc, · line 5353

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+		 * so loads are atomic; send_mds_reconnect() will
+		 * reject sessions that transitioned to CLOSED or
+		 * REJECTED under s_mutex before proceeding.
+		 */
+		switch (READ_ONCE(session->s_state)) {
+		case CEPH_MDS_SESSION_OPEN:
+		case CEPH_MDS_SESSION_HUNG:
+		case CEPH_MDS_SESSION_OPENING:
+		case CEPH_MDS_SESSION_CLOSING:
+			sessions[n++] = ceph_get_mds_session(session);

Why n but not i here? It looks like potential really not trivial bug.

Quoted 95 lines
fs/ceph/mds_client.c +95 -0
+			break;
+		case CEPH_MDS_SESSION_RECONNECTING:
+			pr_info_client(cl,
+				       "mds%d already reconnecting, skipping\n",
+				       session->s_mds);
+			break;
+		default:
+			pr_info_client(cl,
+				       "mds%d in state %s, skipping reconnect\n",
+				       session->s_mds,
+				       ceph_session_state_name(session->s_state));
+			break;
+		}
+	}
+	mutex_unlock(&mdsc->mutex);
+
+	pr_info_client(cl,
+		       "manual session reset executing (sessions=%d, reason=\"%s\")\n",
+		       n, reason);
+
+	if (n == 0) {
+		kfree(sessions);
+		goto out_complete;
+	}
+
+	/* Initialize completion tracking */
+	reinit_completion(&st->reconnect_done);
+	atomic_set(&st->pending_reconnects, n);
+
+	for (i = 0; i < n; i++) {
+		int err;
+
+		mutex_lock(&sessions[i]->s_mutex);
+		sessions[i]->s_reset_gen = reset_gen;
+		mutex_unlock(&sessions[i]->s_mutex);
+
+		err = send_mds_reconnect(mdsc, sessions[i], true);
+		if (err) {
+			bool skipped = (err == -ESTALE || err == -ENOENT);
+
+			mutex_lock(&sessions[i]->s_mutex);
+			sessions[i]->s_reset_gen = 0;
+			mutex_unlock(&sessions[i]->s_mutex);
+
+			if (skipped) {
+				pr_info_client(cl,
+					       "mds%d reconnect skipped during reset: %d\n",
+					       sessions[i]->s_mds, err);
+			} else {
+				pr_err_client(cl,
+					      "mds%d reconnect failed: %d\n",
+					      sessions[i]->s_mds, err);
+				if (!ret)
+					ret = err;
+			}
+			if (atomic_dec_and_test(&st->pending_reconnects))
+				complete(&st->reconnect_done);
+		}
+		ceph_put_mds_session(sessions[i]);
+	}
+
+	kfree(sessions);
+
+	/* Wait for all sessions to complete reconnection */
+	if (!wait_for_completion_timeout(&st->reconnect_done,
+					 CEPH_CLIENT_RESET_TIMEOUT_SEC * HZ)) {
+		pr_warn_client(cl,
+			       "reset timed out waiting for %d sessions\n",
+			       atomic_read(&st->pending_reconnects));
+		if (!ret)
+			ret = -ETIMEDOUT;
+	}
+
+out_complete:
+	ceph_mdsc_reset_complete(mdsc, ret);
+}
+
+int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
+			     const char *reason)
+{
+	struct ceph_client_reset_state *st = &mdsc->reset_state;
+	struct ceph_fs_client *fsc = mdsc->fsc;
+	const char *msg = (reason && reason[0]) ? reason : "manual";
+	int mount_state;
+
+	mount_state = READ_ONCE(fsc->mount_state);
+	if (mount_state != CEPH_MOUNT_MOUNTED) {
+		pr_warn_client(fsc->client,
+			       "reset rejected: mount_state=%d (not mounted)\n",
+			       mount_state);
+		return -EINVAL;
+	}
+
+	spin_lock(&st->lock);
+	if (st->in_progress) {

fs/ceph/mds_client.c static void send_mds_reconnect(struct ceph_mds_client *mdsc, · line 5458

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+		spin_unlock(&st->lock);
+		return -EBUSY;
+	}
+
+	st->in_progress = true;
+	st->active_reset_gen++;
+	st->last_start = jiffies;
+	st->last_errno = 0;
+	st->trigger_count++;
+	strscpy(st->last_reason, msg, sizeof(st->last_reason));

Ditto.

Thanks,
Slava.

Quoted 87 lines
fs/ceph/mds_client.c +43 -4
+	spin_unlock(&st->lock);
+
+	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
+		spin_lock(&st->lock);
+		st->in_progress = false;
+		st->last_errno = -EALREADY;
+		st->failure_count++;
+		spin_unlock(&st->lock);
+		wake_up_all(&st->blocked_wq);
+		return -EALREADY;
+	}
+
+	pr_info_client(mdsc->fsc->client,
+		       "manual session reset scheduled (reason=\"%s\")\n",
+		       msg);
+	trace_ceph_client_reset_schedule(mdsc, msg);
+	return 0;
 }
 
 
@@ -5321,9 +5553,15 @@ static void check_new_map(struct ceph_mds_client *mdsc,
 		 */
 		if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
 		    newstate >= CEPH_MDS_STATE_RECONNECT) {
+			int rc;
+
 			mutex_unlock(&mdsc->mutex);
 			clear_bit(i, targets);
-			send_mds_reconnect(mdsc, s);
+			rc = send_mds_reconnect(mdsc, s, false);
+			if (rc)
+				pr_err_client(cl,
+					      "mds%d reconnect failed: %d\n",
+					      i, rc);
 			mutex_lock(&mdsc->mutex);
 		}
 
@@ -5387,7 +5625,11 @@ static void check_new_map(struct ceph_mds_client *mdsc,
 		}
 		doutc(cl, "send reconnect to export target mds.%d\n", i);
 		mutex_unlock(&mdsc->mutex);
-		send_mds_reconnect(mdsc, s);
+		err = send_mds_reconnect(mdsc, s, false);
+		if (err)
+			pr_err_client(cl,
+				      "mds%d export target reconnect failed: %d\n",
+				      i, err);
 		ceph_put_mds_session(s);
 		mutex_lock(&mdsc->mutex);
 	}
@@ -6314,6 +6556,11 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	/* flush out any connection work with references to us */
 	ceph_msgr_flush();
 
+	/*
+	 * Mark reset as failed and wake any blocked waiters before
+	 * cancelling, so unmount doesn't stall on blocked_wq timeout
+	 * if cancel_work_sync() prevents the work from running.
+	 */
 	spin_lock(&mdsc->reset_state.lock);
 	if (mdsc->reset_state.in_progress) {
 		mdsc->reset_state.in_progress = false;
@@ -6498,8 +6745,13 @@ static void mds_peer_reset(struct ceph_connection *con)
 	pr_warn_client(mdsc->fsc->client, "mds%d closed our session\n",
 		       s->s_mds);
 	if (READ_ONCE(mdsc->fsc->mount_state) != CEPH_MOUNT_FENCE_IO &&
-	    ceph_mdsmap_get_state(mdsc->mdsmap, s->s_mds) >= CEPH_MDS_STATE_RECONNECT)
-		send_mds_reconnect(mdsc, s);
+	    ceph_mdsmap_get_state(mdsc->mdsmap, s->s_mds) >= CEPH_MDS_STATE_RECONNECT) {
+		int rc = send_mds_reconnect(mdsc, s, false);
+		if (rc)
+			pr_err_client(mdsc->fsc->client,
+				      "mds%d reconnect failed: %d\n",
+				      s->s_mds, rc);
+	}
 }
 
 static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
@@ -6511,6 +6763,8 @@ static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
 
 	mutex_lock(&mdsc->mutex);
 	if (__verify_registered_session(mdsc, s) < 0) {
+		pr_info_client(cl, "dropping tid %llu from unregistered session %d\n",
+			       msg->hdr.tid, s->s_mds);
 		mutex_unlock(&mdsc->mutex);
 		goto out;
 	}
author maintainer
#12 lore raw

PATCH 6/10 ceph: replace reconnect with direct session teardown in manual reset

In reply to Alex Markuze

Reconnect only works during the MDS reconnect window, so it cannot be the recovery primitive for an operator-triggered reset against a live MDS. Switch the reset path to discard local session state directly so the client can break capability stalemates and rebuild clean state without waiting for MDS-side reconnect support.

---
 fs/ceph/mds_client.c | 84 +++++++++++++++++++++-----------------------
 1 file changed, 40 insertions(+), 44 deletions(-)
fs/ceph/mds_client.c +40 -44
index 5a2a9e473058..766744468dd6 100644
@@ -5297,7 +5297,6 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 	char reason[CEPH_CLIENT_RESET_REASON_LEN];
 	int max_sessions, i, n = 0;
 	bool inject_error;
-	u64 reset_gen;
 	int ret = 0;
 
 	spin_lock(&st->lock);
@@ -5305,7 +5304,6 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 	inject_error = st->inject_error;
 	if (inject_error)
 		st->inject_error = false;
-	reset_gen = st->active_reset_gen;
 	spin_unlock(&st->lock);
 
 	if (inject_error) {
@@ -5341,9 +5339,8 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 		 * mdsc->mutex -> s_mutex, which would invert the
 		 * s_mutex -> mdsc->mutex order used by
 		 * cleanup_session_requests().  s_state is an int
-		 * so loads are atomic; send_mds_reconnect() will
-		 * reject sessions that transitioned to CLOSED or
-		 * REJECTED under s_mutex before proceeding.
+		 * so loads are atomic; the teardown loop below
+		 * handles races with concurrent state transitions.
 		 */
 		switch (READ_ONCE(session->s_state)) {
 		case CEPH_MDS_SESSION_OPEN:
@@ -5354,12 +5351,12 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 			break;
 		case CEPH_MDS_SESSION_RECONNECTING:
 			pr_info_client(cl,
-				       "mds%d already reconnecting, skipping\n",
+				       "mds%d already reconnecting, skipping reset\n",
 				       session->s_mds);
 			break;
 		default:
 			pr_info_client(cl,
-				       "mds%d in state %s, skipping reconnect\n",
+				       "mds%d in state %s, skipping reset\n",
 				       session->s_mds,
 				       ceph_session_state_name(session->s_state));
 			break;
@@ -5376,54 +5373,53 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 		goto out_complete;
 	}
 
-	/* Initialize completion tracking */
-	reinit_completion(&st->reconnect_done);
-	atomic_set(&st->pending_reconnects, n);
-
+	/*
+	 * Tear down each session: close the connection, remove all
+	 * caps, clean up requests, then kick pending requests so they
+	 * re-open a fresh session on the next attempt.
+	 *
+	 * This is modeled on the check_new_map() forced-close path
+	 * for stopped MDS ranks - a proven pattern for hard session
+	 * teardown.  We do NOT attempt send_mds_reconnect() because
+	 * the MDS only accepts reconnects during its own RECONNECT
+	 * phase (after MDS restart), not from an active client.
+	 */
 	for (i = 0; i < n; i++) {
-		int err;
+		int mds = sessions[i]->s_mds;
+
+		pr_info_client(cl, "mds%d resetting session\n", mds);
+
+		mutex_lock(&mdsc->mutex);
+		if (mds >= mdsc->max_sessions ||
+		    mdsc->sessions[mds] != sessions[i]) {
+			pr_info_client(cl,
+				       "mds%d session already torn down, skipping\n",
+				       mds);
+			mutex_unlock(&mdsc->mutex);
+			ceph_put_mds_session(sessions[i]);
+			continue;
+		}
+		sessions[i]->s_state = CEPH_MDS_SESSION_CLOSED;
+		__unregister_session(mdsc, sessions[i]);
+		__wake_requests(mdsc, &sessions[i]->s_waiting);
+		mutex_unlock(&mdsc->mutex);
 
 		mutex_lock(&sessions[i]->s_mutex);
-		sessions[i]->s_reset_gen = reset_gen;
+		cleanup_session_requests(mdsc, sessions[i]);
+		remove_session_caps(sessions[i]);
 		mutex_unlock(&sessions[i]->s_mutex);
 
-		err = send_mds_reconnect(mdsc, sessions[i], true);
-		if (err) {
-			bool skipped = (err == -ESTALE || err == -ENOENT);
+		ceph_put_mds_session(sessions[i]);
 
-			mutex_lock(&sessions[i]->s_mutex);
-			sessions[i]->s_reset_gen = 0;
-			mutex_unlock(&sessions[i]->s_mutex);
+		mutex_lock(&mdsc->mutex);
+		kick_requests(mdsc, mds);
+		mutex_unlock(&mdsc->mutex);
 
-			if (skipped) {
-				pr_info_client(cl,
-					       "mds%d reconnect skipped during reset: %d\n",
-					       sessions[i]->s_mds, err);
-			} else {
-				pr_err_client(cl,
-					      "mds%d reconnect failed: %d\n",
-					      sessions[i]->s_mds, err);
-				if (!ret)
-					ret = err;
-			}
-			if (atomic_dec_and_test(&st->pending_reconnects))
-				complete(&st->reconnect_done);
-		}
-		ceph_put_mds_session(sessions[i]);
+		pr_info_client(cl, "mds%d session reset complete\n", mds);
 	}
 
 	kfree(sessions);
 
-	/* Wait for all sessions to complete reconnection */
-	if (!wait_for_completion_timeout(&st->reconnect_done,
-					 CEPH_CLIENT_RESET_TIMEOUT_SEC * HZ)) {
-		pr_warn_client(cl,
-			       "reset timed out waiting for %d sessions\n",
-			       atomic_read(&st->pending_reconnects));
-		if (!ret)
-			ret = -ETIMEDOUT;
-	}
-
 out_complete:
 	ceph_mdsc_reset_complete(mdsc, ret);
 }
Signature · 1 line
2.34.1
author maintainer
#13 lore raw

PATCH 7/10 ceph: add reset state machine, drain phase, and status reporting

In reply to Alex Markuze

Track manual reset progress with explicit phases so blocked callers and debugfs status can distinguish quiescing, draining, and teardown. Add a bounded best-effort drain window before session teardown and report the outcome so operators can see whether dirty state drained or had to be dropped.

---
 fs/ceph/debugfs.c    |  14 ++++--
 fs/ceph/mds_client.c | 116 ++++++++++++++++++++++++++++++++++++++-----
 fs/ceph/mds_client.h |  16 ++++--
 3 files changed, 127 insertions(+), 19 deletions(-)
fs/ceph/debugfs.c +11 -3
index 9e2c32fb1f38..88969dc5dde5 100644
@@ -374,8 +374,10 @@ static int reset_status_show(struct seq_file *s, void *p)
 	u64 trigger = 0, success = 0, failure = 0;
 	unsigned long last_start = 0, last_finish = 0;
 	int last_errno = 0;
-	bool in_progress = false;
+	enum ceph_client_reset_phase phase = CEPH_CLIENT_RESET_IDLE;
 	bool inject_error = false;
+	bool drain_timed_out = false;
+	int sessions_reset = 0;
 	int pending_reconnects = 0;
 	int blocked_requests = 0;
 	char reason[CEPH_CLIENT_RESET_REASON_LEN];
@@ -392,15 +394,17 @@ static int reset_status_show(struct seq_file *s, void *p)
 	last_start = st->last_start;
 	last_finish = st->last_finish;
 	last_errno = st->last_errno;
-	in_progress = st->in_progress;
+	phase = st->phase;
 	inject_error = st->inject_error;
+	drain_timed_out = st->drain_timed_out;
+	sessions_reset = st->sessions_reset;
 	strscpy(reason, st->last_reason, sizeof(reason));
 	spin_unlock(&st->lock);
 
 	pending_reconnects = atomic_read(&st->pending_reconnects);
 	blocked_requests = atomic_read(&st->blocked_requests);
 
-	seq_printf(s, "in_progress: %s\n", in_progress ? "yes" : "no");
+	seq_printf(s, "phase: %s\n", ceph_reset_phase_name(phase));
 	seq_printf(s, "trigger_count: %llu\n", trigger);
 	seq_printf(s, "success_count: %llu\n", success);
 	seq_printf(s, "failure_count: %llu\n", failure);
@@ -419,6 +423,10 @@ static int reset_status_show(struct seq_file *s, void *p)
 		   reason[0] ? reason : "(none)");
 	seq_printf(s, "inject_error_pending: %s\n",
 		   inject_error ? "yes" : "no");
+	seq_printf(s, "drain_timed_out: %s\n",
+		   drain_timed_out ? "yes" : "no");
+	seq_printf(s, "sessions_reset: %d\n", sessions_reset);
+	/* pending_reconnects: always 0 for manual reset (MDS-reconnect only) */
 	seq_printf(s, "pending_reconnects: %d\n", pending_reconnects);
 	seq_printf(s, "blocked_requests: %d\n", blocked_requests);
 
fs/ceph/mds_client.c +103 -13

208 lines not on this page · raw message

fs/ceph/mds_client.h +13 -3
index c8269df1a9e5..87302755461c 100644
@@ -75,9 +75,16 @@ struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
 #define CEPH_CLIENT_RESET_REASON_LEN	64
-#define CEPH_CLIENT_RESET_TIMEOUT_SEC	60
+#define CEPH_CLIENT_RESET_DRAIN_SEC	5
 #define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
 
+enum ceph_client_reset_phase {
+	CEPH_CLIENT_RESET_IDLE = 0,
+	CEPH_CLIENT_RESET_QUIESCING,
+	CEPH_CLIENT_RESET_DRAINING,
+	CEPH_CLIENT_RESET_TEARDOWN,
+};
+
 struct ceph_client_reset_state {
 	spinlock_t lock;
 	u64 trigger_count;
@@ -86,11 +93,13 @@ struct ceph_client_reset_state {
 	unsigned long last_start;
 	unsigned long last_finish;
 	int last_errno;
-	bool in_progress;
+	enum ceph_client_reset_phase phase;
 	bool inject_error;
+	bool drain_timed_out;
+	int sessions_reset;
 	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
 
-	/* Completion tracking for session reconnects */
+	/* Async reconnect completion (used by mds_peer_reset path) */
 	u64 active_reset_gen;
 	atomic_t pending_reconnects;
 	struct completion reconnect_done;
@@ -588,6 +597,7 @@ extern struct ceph_mds_session *
 __ceph_lookup_mds_session(struct ceph_mds_client *, int mds);
 
 extern const char *ceph_session_state_name(int s);
+extern const char *ceph_reset_phase_name(enum ceph_client_reset_phase phase);
 
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH 07/10] ceph: add reset state machine, drain phase, and status reporting

Quoted 50 lines · Alex Markuze

Track manual reset progress with explicit phases so blocked callers and debugfs status can distinguish quiescing, draining, and teardown. Add a bounded best-effort drain window before session teardown and report the outcome so operators can see whether dirty state drained or had to be dropped.

---
 fs/ceph/debugfs.c    |  14 ++++--
 fs/ceph/mds_client.c | 116 ++++++++++++++++++++++++++++++++++++++-----
 fs/ceph/mds_client.h |  16 ++++--
 3 files changed, 127 insertions(+), 19 deletions(-)
fs/ceph/debugfs.c +7 -3
index 9e2c32fb1f38..88969dc5dde5 100644
@@ -374,8 +374,10 @@ static int reset_status_show(struct seq_file *s, void *p)
 	u64 trigger = 0, success = 0, failure = 0;
 	unsigned long last_start = 0, last_finish = 0;
 	int last_errno = 0;
-	bool in_progress = false;
+	enum ceph_client_reset_phase phase = CEPH_CLIENT_RESET_IDLE;
 	bool inject_error = false;
+	bool drain_timed_out = false;
+	int sessions_reset = 0;
 	int pending_reconnects = 0;
 	int blocked_requests = 0;
 	char reason[CEPH_CLIENT_RESET_REASON_LEN];
@@ -392,15 +394,17 @@ static int reset_status_show(struct seq_file *s, void *p)
 	last_start = st->last_start;
 	last_finish = st->last_finish;
 	last_errno = st->last_errno;
-	in_progress = st->in_progress;
+	phase = st->phase;
 	inject_error = st->inject_error;
+	drain_timed_out = st->drain_timed_out;
+	sessions_reset = st->sessions_reset;
 	strscpy(reason, st->last_reason, sizeof(reason));
 	spin_unlock(&st->lock);
 
 	pending_reconnects = atomic_read(&st->pending_reconnects);
 	blocked_requests = atomic_read(&st->blocked_requests);
 
-	seq_printf(s, "in_progress: %s\n", in_progress ? "yes" : "no");
+	seq_printf(s, "phase: %s\n", ceph_reset_phase_name(phase));
 	seq_printf(s, "trigger_count: %llu\n", trigger);
 	seq_printf(s, "success_count: %llu\n", success);
 	seq_printf(s, "failure_count: %llu\n", failure);

fs/ceph/debugfs.c static int reset_status_show(struct seq_file *s, void *p) · line 427

Quoted 5 lines
fs/ceph/debugfs.c +2 -0
@@ -419,6 +423,10 @@ static int reset_status_show(struct seq_file *s, void *p)
 		   reason[0] ? reason : "(none)");
 	seq_printf(s, "inject_error_pending: %s\n",
 		   inject_error ? "yes" : "no");
+	seq_printf(s, "drain_timed_out: %s\n",
+		   drain_timed_out ? "yes" : "no");

While phase == IDLE, drain_timed_out reflects the previous run's outcome. Should we comment it somehow?

fs/ceph/debugfs.c static int reset_status_show(struct seq_file *s, void *p) · line 429

Quoted 2 lines
fs/ceph/debugfs.c +2 -0
+	seq_printf(s, "sessions_reset: %d\n", sessions_reset);
+	/* pending_reconnects: always 0 for manual reset (MDS-reconnect only) */

Is it actual comment? The pending_reconnects can be non-zero during the QUIESCING phase. Am I right?

Quoted 101 lines
fs/ceph/debugfs.c +0 -0
 	seq_printf(s, "pending_reconnects: %d\n", pending_reconnects);
 	seq_printf(s, "blocked_requests: %d\n", blocked_requests);
 
fs/ceph/mds_client.c +40 -7
index 766744468dd6..287cee71227b 100644
@@ -3757,7 +3757,7 @@ int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
 	 * If a reset is in progress, wait for it to complete.
 	 *
 	 * This is best-effort: a request can pass this check just
-	 * before in_progress is set and proceed concurrently with
+	 * before the phase leaves IDLE and proceed concurrently with
 	 * reset.  That is acceptable because (a) such requests will
 	 * either complete normally or fail and be retried by the
 	 * caller, and (b) adding lock serialization here would
@@ -5215,9 +5215,20 @@ static void ceph_mdsc_reconnect_session_done(struct ceph_mds_client *mdsc,
 		complete(&st->reconnect_done);
 }
 
+const char *ceph_reset_phase_name(enum ceph_client_reset_phase phase)
+{
+	switch (phase) {
+	case CEPH_CLIENT_RESET_IDLE:	  return "idle";
+	case CEPH_CLIENT_RESET_QUIESCING: return "quiescing";
+	case CEPH_CLIENT_RESET_DRAINING:  return "draining";
+	case CEPH_CLIENT_RESET_TEARDOWN:  return "teardown";
+	default:			  return "unknown";
+	}
+}
+
 /*
- * Wait for a reset to complete if one is in progress.
- * Returns 0 if reset completed successfully or wasn't in progress.
+ * Wait for an active reset to complete.
+ * Returns 0 if reset completed successfully or no reset was active.
  * Returns -ETIMEDOUT if we timed out waiting.
  * Returns -ERESTARTSYS if interrupted by signal.
  */
@@ -5229,7 +5240,7 @@ int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
 	int blocked_count;
 	int ret;
 
-	if (!READ_ONCE(st->in_progress))
+	if (READ_ONCE(st->phase) == CEPH_CLIENT_RESET_IDLE)
 		return 0;
 
 	blocked_count = atomic_inc_return(&st->blocked_requests);
@@ -5238,7 +5249,8 @@ int ceph_mdsc_wait_for_reset(struct ceph_mds_client *mdsc)
 	trace_ceph_client_reset_blocked(mdsc, blocked_count);
 
 	ret = wait_event_interruptible_timeout(st->blocked_wq,
-					       !READ_ONCE(st->in_progress),
+					       READ_ONCE(st->phase) ==
+						CEPH_CLIENT_RESET_IDLE,
 					       timeout);
 
 	atomic_dec(&st->blocked_requests);
@@ -5274,7 +5286,7 @@ static void ceph_mdsc_reset_complete(struct ceph_mds_client *mdsc, int ret)
 	spin_lock(&st->lock);
 	st->last_finish = jiffies;
 	st->last_errno = ret;
-	st->in_progress = false;
+	st->phase = CEPH_CLIENT_RESET_IDLE;
 	if (ret)
 		st->failure_count++;
 	else
@@ -5295,7 +5307,7 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 	struct ceph_client *cl = mdsc->fsc->client;
 	struct ceph_mds_session **sessions = NULL;
 	char reason[CEPH_CLIENT_RESET_REASON_LEN];
-	int max_sessions, i, n = 0;
+	int max_sessions, i, n = 0, torn_down = 0;
 	bool inject_error;
 	int ret = 0;
 
@@ -5373,6 +5385,70 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 		goto out_complete;
 	}
 
+	spin_lock(&st->lock);
+	st->phase = CEPH_CLIENT_RESET_DRAINING;
+	spin_unlock(&st->lock);
+
+	/*
+	 * Best-effort drain: flush dirty state while sessions are still
+	 * alive.  New requests are blocked while phase != IDLE.
+	 * The sessions are functional, so non-stuck state drains normally.
+	 * Stuck state (the cause of the stalemate the operator is trying
+	 * to break) will not drain - that is expected, and we proceed to
+	 * forced teardown after the timeout.
+	 *
+	 * Three things are drained:
+	 *  1. MDS journal - send_flush_mdlog asks each MDS to journal
+	 *     pending unsafe operations (creates, renames, setattrs).
+	 *     Once journaled, they survive the session teardown.
+	 *  2. Dirty caps - ceph_flush_dirty_caps triggers cap flush on
+	 *     all sessions.  Non-stuck caps flush in milliseconds.
+	 *  3. Cap releases - push pending cap release messages.
+	 *
+	 * All three happen concurrently during the bounded wait window.

fs/ceph/mds_client.c static void ceph_mdsc_reset_workfn(struct work_struct *work) · line 5418

Quoted 10 lines
fs/ceph/mds_client.c +10 -0
+	 */
+	for (i = 0; i < n; i++)
+		send_flush_mdlog(sessions[i]);
+
+	ceph_flush_dirty_caps(mdsc);
+	ceph_flush_cap_releases(mdsc);
+
+	spin_lock(&mdsc->cap_dirty_lock);
+	if (!list_empty(&mdsc->cap_flush_list)) {
+		u64 want_flush = mdsc->last_cap_flush_tid;
                struct ceph_cap_flush *cf =
                     list_last_entry(&mdsc->cap_flush_list,                    
                                   struct ceph_cap_flush, g_list);

fs/ceph/mds_client.c static void ceph_mdsc_reset_workfn(struct work_struct *work) · line 5419

Quoted 1 line
fs/ceph/mds_client.c +1 -0
+		long drain_ret;

Should we add this code here?

                 cf->wake = true;

The existing wait_caps_flush() first sets cf->wake = true on the tail entry of cap_flush_list before waiting.

Quoted 120 lines
fs/ceph/mds_client.c +52 -6
+
+		spin_unlock(&mdsc->cap_dirty_lock);
+		pr_info_client(cl,
+			       "draining (want_flush=%llu, %d sessions)\n",
+			       want_flush, n);
+		drain_ret = wait_event_timeout(mdsc->cap_flushing_wq,
+					       check_caps_flush(mdsc,
+								want_flush),
+					       CEPH_CLIENT_RESET_DRAIN_SEC * HZ);
+		if (drain_ret == 0) {
+			pr_info_client(cl,
+				       "drain timed out, proceeding with forced teardown\n");
+			spin_lock(&st->lock);
+			st->drain_timed_out = true;
+			spin_unlock(&st->lock);
+		} else {
+			pr_info_client(cl, "drain completed successfully\n");
+			spin_lock(&st->lock);
+			st->drain_timed_out = false;
+			spin_unlock(&st->lock);
+		}
+	} else {
+		spin_unlock(&mdsc->cap_dirty_lock);
+		spin_lock(&st->lock);
+		st->drain_timed_out = false;
+		spin_unlock(&st->lock);
+	}
+
+	spin_lock(&st->lock);
+	st->phase = CEPH_CLIENT_RESET_TEARDOWN;
+	spin_unlock(&st->lock);
+
 	/*
 	 * Tear down each session: close the connection, remove all
 	 * caps, clean up requests, then kick pending requests so they
@@ -5383,6 +5459,11 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 	 * teardown.  We do NOT attempt send_mds_reconnect() because
 	 * the MDS only accepts reconnects during its own RECONNECT
 	 * phase (after MDS restart), not from an active client.
+	 *
+	 * Any state that did not drain (caps that didn't flush, unsafe
+	 * requests that the MDS didn't journal) is force-dropped here.
+	 * This is intentional: that state is stuck and is the reason
+	 * the operator triggered the reset.
 	 */
 	for (i = 0; i < n; i++) {
 		int mds = sessions[i]->s_mds;
@@ -5415,11 +5496,16 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 		kick_requests(mdsc, mds);
 		mutex_unlock(&mdsc->mutex);
 
+		torn_down++;
 		pr_info_client(cl, "mds%d session reset complete\n", mds);
 	}
 
 	kfree(sessions);
 
+	spin_lock(&st->lock);
+	st->sessions_reset = torn_down;
+	spin_unlock(&st->lock);
+
 out_complete:
 	ceph_mdsc_reset_complete(mdsc, ret);
 }
@@ -5441,22 +5527,24 @@ int ceph_mdsc_schedule_reset(struct ceph_mds_client *mdsc,
 	}
 
 	spin_lock(&st->lock);
-	if (st->in_progress) {
+	if (st->phase != CEPH_CLIENT_RESET_IDLE) {
 		spin_unlock(&st->lock);
 		return -EBUSY;
 	}
 
-	st->in_progress = true;
+	st->phase = CEPH_CLIENT_RESET_QUIESCING;
 	st->active_reset_gen++;
 	st->last_start = jiffies;
 	st->last_errno = 0;
+	st->drain_timed_out = false;
+	st->sessions_reset = 0;
 	st->trigger_count++;
 	strscpy(st->last_reason, msg, sizeof(st->last_reason));
 	spin_unlock(&st->lock);
 
 	if (WARN_ON_ONCE(!queue_work(system_unbound_wq, &mdsc->reset_work))) {
 		spin_lock(&st->lock);
-		st->in_progress = false;
+		st->phase = CEPH_CLIENT_RESET_IDLE;
 		st->last_errno = -EALREADY;
 		st->failure_count++;
 		spin_unlock(&st->lock);
@@ -6017,8 +6105,10 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
 	mdsc->reset_state.last_start = 0;
 	mdsc->reset_state.last_finish = 0;
 	mdsc->reset_state.last_errno = 0;
-	mdsc->reset_state.in_progress = false;
+	mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
 	mdsc->reset_state.inject_error = false;
+	mdsc->reset_state.drain_timed_out = false;
+	mdsc->reset_state.sessions_reset = 0;
 	mdsc->reset_state.active_reset_gen = 0;
 	mdsc->reset_state.last_reason[0] = '\0';
 	atomic_set(&mdsc->reset_state.pending_reconnects, 0);
@@ -6558,8 +6648,8 @@ void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
 	 * if cancel_work_sync() prevents the work from running.
 	 */
 	spin_lock(&mdsc->reset_state.lock);
-	if (mdsc->reset_state.in_progress) {
-		mdsc->reset_state.in_progress = false;
+	if (mdsc->reset_state.phase != CEPH_CLIENT_RESET_IDLE) {
+		mdsc->reset_state.phase = CEPH_CLIENT_RESET_IDLE;
 		mdsc->reset_state.last_errno = -ESHUTDOWN;
 		mdsc->reset_state.failure_count++;
 	}
fs/ceph/mds_client.h +0 -0
index c8269df1a9e5..87302755461c 100644

fs/ceph/mds_client.h struct ceph_cap; · line 78

Quoted 4 lines
fs/ceph/mds_client.h +0 -1
@@ -75,9 +75,16 @@ struct ceph_cap;
 
 #define MDS_AUTH_UID_ANY -1
 #define CEPH_CLIENT_RESET_REASON_LEN	64
-#define CEPH_CLIENT_RESET_TIMEOUT_SEC	60

Is it safe to remove this declaration in the patch? Any compilation issues?

Thanks,
Slava.

Quoted 37 lines
fs/ceph/mds_client.h +13 -2
+#define CEPH_CLIENT_RESET_DRAIN_SEC	5
 #define CEPH_CLIENT_RESET_WAIT_TIMEOUT_SEC 120
 
+enum ceph_client_reset_phase {
+	CEPH_CLIENT_RESET_IDLE = 0,
+	CEPH_CLIENT_RESET_QUIESCING,
+	CEPH_CLIENT_RESET_DRAINING,
+	CEPH_CLIENT_RESET_TEARDOWN,
+};
+
 struct ceph_client_reset_state {
 	spinlock_t lock;
 	u64 trigger_count;
@@ -86,11 +93,13 @@ struct ceph_client_reset_state {
 	unsigned long last_start;
 	unsigned long last_finish;
 	int last_errno;
-	bool in_progress;
+	enum ceph_client_reset_phase phase;
 	bool inject_error;
+	bool drain_timed_out;
+	int sessions_reset;
 	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
 
-	/* Completion tracking for session reconnects */
+	/* Async reconnect completion (used by mds_peer_reset path) */
 	u64 active_reset_gen;
 	atomic_t pending_reconnects;
 	struct completion reconnect_done;
@@ -588,6 +597,7 @@ extern struct ceph_mds_session *
 __ceph_lookup_mds_session(struct ceph_mds_client *, int mds);
 
 extern const char *ceph_session_state_name(int s);
+extern const char *ceph_reset_phase_name(enum ceph_client_reset_phase phase);
 
 extern struct ceph_mds_session *
 ceph_get_mds_session(struct ceph_mds_session *s);
author maintainer
#15 lore raw

PATCH 8/10 ceph: drop reset reconnect bookkeeping after direct teardown switch

In reply to Alex Markuze

Once manual reset tears sessions down locally, it no longer tracks per-reset reconnect completions. Remove the leftover generation and pending-reconnect state and simplify peer-reset handling to match the new teardown-based reset path.

---
 fs/ceph/debugfs.c    |   4 --
 fs/ceph/mds_client.c | 165 ++++++++++++++++++++++++-------------------
 fs/ceph/mds_client.h |   6 --
 3 files changed, 91 insertions(+), 84 deletions(-)
fs/ceph/debugfs.c +0 -4
index 88969dc5dde5..0c26491dbbf8 100644
@@ -378,7 +378,6 @@ static int reset_status_show(struct seq_file *s, void *p)
 	bool inject_error = false;
 	bool drain_timed_out = false;
 	int sessions_reset = 0;
-	int pending_reconnects = 0;
 	int blocked_requests = 0;
 	char reason[CEPH_CLIENT_RESET_REASON_LEN];
 
@@ -401,7 +400,6 @@ static int reset_status_show(struct seq_file *s, void *p)
 	strscpy(reason, st->last_reason, sizeof(reason));
 	spin_unlock(&st->lock);
 
-	pending_reconnects = atomic_read(&st->pending_reconnects);
 	blocked_requests = atomic_read(&st->blocked_requests);
 
 	seq_printf(s, "phase: %s\n", ceph_reset_phase_name(phase));
@@ -426,8 +424,6 @@ static int reset_status_show(struct seq_file *s, void *p)
 	seq_printf(s, "drain_timed_out: %s\n",
 		   drain_timed_out ? "yes" : "no");
 	seq_printf(s, "sessions_reset: %d\n", sessions_reset);
-	/* pending_reconnects: always 0 for manual reset (MDS-reconnect only) */
-	seq_printf(s, "pending_reconnects: %d\n", pending_reconnects);
 	seq_printf(s, "blocked_requests: %d\n", blocked_requests);
 
 	return 0;
fs/ceph/mds_client.c +91 -74

263 lines not on this page · raw message

fs/ceph/mds_client.h +0 -6
index 87302755461c..07d79653c0a6 100644
@@ -99,11 +99,6 @@ struct ceph_client_reset_state {
 	int sessions_reset;
 	char last_reason[CEPH_CLIENT_RESET_REASON_LEN];
 
-	/* Async reconnect completion (used by mds_peer_reset path) */
-	u64 active_reset_gen;
-	atomic_t pending_reconnects;
-	struct completion reconnect_done;
-
 	/* Request blocking during reset */
 	wait_queue_head_t blocked_wq;
 	atomic_t blocked_requests;
@@ -286,7 +281,6 @@ struct ceph_mds_session {
 	struct list_head  s_unsafe;   /* unsafe requests */
 	struct xarray	  s_delegated_inos;
 
-	u64		  s_reset_gen; /* generation of reset that initiated reconnect */
 };
 
 /*
Signature · 1 line
2.34.1
author maintainer
#16 lore raw

PATCH 9/10 ceph: send SESSION_REQUEST_CLOSE before forced teardown in manual reset

In reply to Alex Markuze

Queue SESSION_REQUEST_CLOSE through the normal close-session path before forced teardown so the MDS can release state immediately and racing sequence updates still retransmit the close request correctly.

---
 fs/ceph/mds_client.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
fs/ceph/mds_client.c +28 -0
index 8b86c577c127..864c35c6f55f 100644
@@ -6,6 +6,7 @@
 #include <linux/slab.h>
 #include <linux/gfp.h>
 #include <linux/sched.h>
+#include <linux/delay.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
 #include <linux/ratelimit.h>
@@ -5391,6 +5392,33 @@ static void ceph_mdsc_reset_workfn(struct work_struct *work)
 	st->phase = CEPH_CLIENT_RESET_TEARDOWN;
 	spin_unlock(&st->lock);
 
+	/*
+	 * Ask each MDS to close the session before we tear it down
+	 * locally.  Without this the MDS sees only a connection drop and
+	 * waits for the client to reconnect (up to session_autoclose
+	 * seconds) before evicting the session and releasing locks.
+	 *
+	 * Reuse the normal close machinery so the session state/sequence
+	 * snapshot is serialized under s_mutex and a racing s_seq bump
+	 * retransmits REQUEST_CLOSE while the session remains CLOSING.
+	 * We send all close requests first, then yield briefly to let the
+	 * network stack transmit them before __unregister_session()
+	 * closes the connections.
+	 */
+	for (i = 0; i < n; i++) {
+		int err;
+
+		mutex_lock(&sessions[i]->s_mutex);
+		err = __close_session(mdsc, sessions[i]);
+		mutex_unlock(&sessions[i]->s_mutex);
+		if (err < 0)
+			pr_warn_client(cl,
+				       "mds%d failed to queue close request before reset: %d\n",
+				       sessions[i]->s_mds, err);
+	}
+	if (n > 0)
+		msleep(100);
+
 	/*
 	 * Tear down each session: close the connection, remove all
 	 * caps, clean up requests, then kick pending requests so they
Signature · 1 line
2.34.1
#17 lore raw
In reply to Alex Markuze
Quoted 57 lines · Alex Markuze

Manual client reset gives operators a way to break client/MDS capability ownership stalemates without unmounting the client. The reset blocks new metadata work, gives dirty state a bounded chance to drain, asks each MDS to close the session, and then tears the local sessions down so the client can rebuild clean state.

The debugfs interface in this series is only the control surface used to develop and validate the reset flow. It is not the core feature, and the long-term intent is to have the client trigger the reset automatically when it detects the stuck condition.

A reconnect-only reset would require MDS support for accepting reconnects outside the normal MDS RECONNECT phase. Because that support does not exist today, the series first wires up the reconnect-based worker for validation and then switches the recovery path over to direct session teardown.

I split the work into smaller reviewable steps so the preparatory cleanup lands first, then the reset framework and control path, then the initial worker, then the direct-teardown refinements, and finally the single-client selftests and validation harness.

Patch breakdown:
- 1/10 converts the Ceph inode flags to named bit positions and keeps the file-lock error bit ready for atomic bitops.
- 2/10 adds a bounded wait and diagnostics to wait_caps_flush().
- 3/10 adds reset state tracking and blocks new metadata requests and lock acquisition while reset is in progress.
- 4/10 adds the manual reset debugfs control surface, status reporting plumbing, scheduling hooks, and tracepoints.
- 5/10 adds the initial reconnect-based reset worker and the reconnect completion tracking needed by that worker.
- 6/10 switches reset recovery from reconnect to direct session teardown.
- 7/10 adds the explicit reset state machine, bounded drain phase, and richer status reporting.
- 8/10 removes reconnect bookkeeping left behind by the direct-teardown design.
- 9/10 queues CEPH_SESSION_REQUEST_CLOSE before the forced teardown so the MDS can release state immediately.
- 10/10 adds the single-client stress, corner-case, and validation tests used to exercise the reset flow, and passes the configured debugfs root through the validation wrapper so child scripts observe the same client namespace.

Alex Markuze (10):
ceph: convert inode flags to named bit positions
ceph: add bounded timeout and diagnostics to wait_caps_flush()
ceph: add client reset state and request gating
ceph: add manual reset debugfs control and trace plumbing
ceph: implement reconnect-based manual reset worker
ceph: replace reconnect with direct session teardown in manual reset
ceph: add reset state machine, drain phase, and status reporting
ceph: drop reset reconnect bookkeeping after direct teardown switch
ceph: send SESSION_REQUEST_CLOSE before forced teardown in manual
reset
ceph: add single-client reset selftests and validation harness

Quoted 16 lines
 fs/ceph/caps.c                                |   7 +
 fs/ceph/debugfs.c                             | 172 ++++-
 fs/ceph/file.c                                |   2 +-
 fs/ceph/locks.c                               |  24 +-
 fs/ceph/mds_client.c                          | 630 +++++++++++++++-
 fs/ceph/mds_client.h                          |  38 +-
 fs/ceph/super.h                               |  60 +-
 include/trace/events/ceph.h                   |  60 ++
 tools/testing/selftests/filesystems/Makefile  |   1 +
 .../selftests/filesystems/ceph/HOWTO.md       | 121 +++
 .../selftests/filesystems/ceph/Makefile       |   6 +
 .../selftests/filesystems/ceph/README.md      |  40 +
 .../filesystems/ceph/reset_corner_cases.sh    | 707 ++++++++++++++++++
 .../filesystems/ceph/reset_stress.sh          | 682 +++++++++++++++++
 .../filesystems/ceph/run_validation.sh        | 317 ++++++++
 .../filesystems/ceph/validate_consistency.py  | 291 +++++++

I don't see any selftests as part of the patchset. Is it some mistake?

Thanks,
Slava.

Quoted 8 lines
 16 files changed, 3105 insertions(+), 53 deletions(-)
 create mode 100644 tools/testing/selftests/filesystems/ceph/HOWTO.md
 create mode 100644 tools/testing/selftests/filesystems/ceph/Makefile
 create mode 100644 tools/testing/selftests/filesystems/ceph/README.md
 create mode 100755 tools/testing/selftests/filesystems/ceph/reset_corner_cases.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/reset_stress.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/run_validation.sh
 create mode 100755 tools/testing/selftests/filesystems/ceph/validate_consistency.py