Skip to contentSkip to conversation

ceph: cleanup of hardcoded invalid ID

RFC PATCH New Viacheslav Dubeyko 1 participant 3 messages May 1, 2025

Subsystems (2)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

cover ceph: cleanup of hardcoded invalid ID

From: Viacheslav Dubeyko <[email protected]>

This patchset introduces CEPH_INVALID_CACHE_IDX instead of hardcoded value. Also, it reworks ceph_readdir() logic by introducing BUG_ON() for debug case and introduces WARN_ON() for release case.

Viacheslav Dubeyko (2):
ceph: introduce CEPH_INVALID_CACHE_IDX
ceph: exchange BUG_ON on WARN_ON in ceph_readdir()

 fs/ceph/Kconfig | 13 +++++++++++++
 fs/ceph/dir.c   | 33 ++++++++++++++++++++++++---------
 fs/ceph/file.c  |  2 +-
 fs/ceph/super.h |  8 ++++++++
 4 files changed, 46 insertions(+), 10 deletions(-)
Signature · 1 line
2.48.0
author maintainer
#1 lore raw

RFC PATCH 1/2 ceph: introduce CEPH_INVALID_CACHE_IDX

From: Viacheslav Dubeyko <[email protected]>

This patch introduces CEPH_INVALID_CACHE_IDX instead of hardcoded value. The CEPH_INVALID_CACHE_IDX constant is used for initialization and checking that cache index is invalid.

---
 fs/ceph/dir.c   | 12 ++++++------
 fs/ceph/file.c  |  2 +-
 fs/ceph/super.h |  8 ++++++++
 3 files changed, 15 insertions(+), 7 deletions(-)
fs/ceph/dir.c +6 -6
index a321aa6d0ed2..acecc16f2b99 100644
@@ -296,8 +296,8 @@ static int __dcache_readdir(struct file *file,  struct dir_context *ctx,
 			err = ret;
 		dput(last);
 		/* last_name no longer match cache index */
-		if (dfi->readdir_cache_idx >= 0) {
-			dfi->readdir_cache_idx = -1;
+		if (!is_cache_idx_invalid(dfi->readdir_cache_idx)) {
+			dfi->readdir_cache_idx = CEPH_INVALID_CACHE_IDX;
 			dfi->dir_release_count = 0;
 		}
 	}
@@ -483,7 +483,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 
 		if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
 			dfi->readdir_cache_idx = req->r_readdir_cache_idx;
-			if (dfi->readdir_cache_idx < 0) {
+			if (is_cache_idx_invalid(dfi->readdir_cache_idx)) {
 				/* preclude from marking dir ordered */
 				dfi->dir_ordered_count = 0;
 			} else if (ceph_frag_is_leftmost(frag) &&
@@ -497,7 +497,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 			doutc(cl, "%p %llx.%llx !did_prepopulate\n", inode,
 			      ceph_vinop(inode));
 			/* disable readdir cache */
-			dfi->readdir_cache_idx = -1;
+			dfi->readdir_cache_idx = CEPH_INVALID_CACHE_IDX;
 			/* preclude from marking dir complete */
 			dfi->dir_release_count = 0;
 		}
@@ -643,7 +643,7 @@ static void reset_readdir(struct ceph_dir_file_info *dfi)
 	kfree(dfi->last_name);
 	dfi->last_name = NULL;
 	dfi->dir_release_count = 0;
-	dfi->readdir_cache_idx = -1;
+	dfi->readdir_cache_idx = CEPH_INVALID_CACHE_IDX;
 	dfi->next_offset = 2;  /* compensate for . and .. */
 	dfi->file_info.flags &= ~CEPH_F_ATEND;
 }
@@ -703,7 +703,7 @@ static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
 			/* for hash offset, we don't know if a forward seek
 			 * is within same frag */
 			dfi->dir_release_count = 0;
-			dfi->readdir_cache_idx = -1;
+			dfi->readdir_cache_idx = CEPH_INVALID_CACHE_IDX;
 		}
 
 		if (offset != file->f_pos) {
fs/ceph/file.c +1 -1
index 851d70200c6b..2097a23c0c31 100644
@@ -220,7 +220,7 @@ static int ceph_init_file_info(struct inode *inode, struct file *file,
 		file->private_data = dfi;
 		fi = &dfi->file_info;
 		dfi->next_offset = 2;
-		dfi->readdir_cache_idx = -1;
+		dfi->readdir_cache_idx = CEPH_INVALID_CACHE_IDX;
 	} else {
 		fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
 		if (!fi)
fs/ceph/super.h +8 -0
index bb0db0cc8003..bcd1dda1ab81 100644
@@ -840,6 +840,14 @@ struct ceph_file_info {
 	u32 filp_gen;
 };
 
+#define CEPH_INVALID_CACHE_IDX		(-1)
+
+static inline
+bool is_cache_idx_invalid(int cache_idx)
+{
+	return cache_idx <= CEPH_INVALID_CACHE_IDX;
+}
+
 struct ceph_dir_file_info {
 	struct ceph_file_info file_info;
 
Signature · 1 line
2.48.0
author maintainer
#2 lore raw

RFC PATCH 2/2 ceph: exchange BUG_ON on WARN_ON in ceph_readdir()

In reply to Viacheslav Dubeyko

From: Viacheslav Dubeyko <[email protected]>

This patch leaves BUG_ON() for debug case and introduces WARN_ON() for release case in ceph_readdir() logic. If dfi->readdir_cache_idx somehow is invalid, then we will have BUG_ON() in debug build but release build will issue WARN_ON() instead.

---
 fs/ceph/Kconfig | 13 +++++++++++++
 fs/ceph/dir.c   | 21 ++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
fs/ceph/Kconfig +13 -0
index 3e7def3d31c1..dba85d202a14 100644
@@ -50,3 +50,16 @@ config CEPH_FS_SECURITY_LABEL
 
 	  If you are not using a security module that requires using
 	  extended attributes for file security labels, say N.
+
+config CEPH_FS_DEBUG
+	bool "Ceph client debugging"
+	depends on CEPH_FS
+	default n
+	help
+	  If you say Y here, this option enables additional pre-condition
+	  and post-condition checks in functions. Also it could enable
+	  BUG_ON() instead of returning the error code. This option could
+	  save more messages in system log and execute additional computation.
+
+	  If you are going to debug the code, then chose Y here.
+	  If unsure, say N.
fs/ceph/dir.c +18 -3
index acecc16f2b99..c88326e2ddbf 100644
@@ -614,13 +614,28 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		spin_lock(&ci->i_ceph_lock);
 		if (dfi->dir_ordered_count ==
 				atomic64_read(&ci->i_ordered_count)) {
+			bool is_invalid;
+			size_t size;
+
 			doutc(cl, " marking %p %llx.%llx complete and ordered\n",
 			      inode, ceph_vinop(inode));
 			/* use i_size to track number of entries in
 			 * readdir cache */
-			BUG_ON(dfi->readdir_cache_idx < 0);
-			i_size_write(inode, dfi->readdir_cache_idx *
-				     sizeof(struct dentry*));
+
+			is_invalid =
+				is_cache_idx_invalid(dfi->readdir_cache_idx);
+
+#ifdef CONFIG_CEPH_FS_DEBUG
+			BUG_ON(is_invalid);
+#else
+			WARN_ON(is_invalid);
+#endif /* CONFIG_CEPH_FS_DEBUG */
+
+			if (!is_invalid) {
+				size = dfi->readdir_cache_idx;
+				size *= sizeof(struct dentry*);
+				i_size_write(inode, size);
+			}
 		} else {
 			doutc(cl, " marking %llx.%llx complete\n",
 			      ceph_vinop(inode));
Signature · 1 line
2.48.0