Skip to contentSkip to conversation

ceph: cleanup hardcoded constants of file handle size

PATCH In review Viacheslav Dubeyko 2 participants 2 messages Feb 10, 2025 – Mar 4, 2025

Subsystems (2)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

PATCH 1/1 ceph: cleanup hardcoded constants of file handle size

From: Viacheslav Dubeyko <[email protected]>

The ceph/export.c contains very confusing logic of file handle size calculation based on hardcoded values. This patch makes the cleanup of this logic by means of introduction the named constants.

---
 fs/ceph/export.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
fs/ceph/export.c +13 -8
index 150076ced937..b2f2af104679 100644
@@ -33,12 +33,19 @@ struct ceph_nfs_snapfh {
 	u32 hash;
 } __attribute__ ((packed));
 
+#define BYTES_PER_U32		(sizeof(u32))
+#define CEPH_FH_BASIC_SIZE \
+	(sizeof(struct ceph_nfs_fh) / BYTES_PER_U32)
+#define CEPH_FH_WITH_PARENT_SIZE \
+	(sizeof(struct ceph_nfs_confh) / BYTES_PER_U32)
+#define CEPH_FH_SNAPPED_INODE_SIZE \
+	(sizeof(struct ceph_nfs_snapfh) / BYTES_PER_U32)
+
 static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
 			      struct inode *parent_inode)
 {
 	struct ceph_client *cl = ceph_inode_to_client(inode);
-	static const int snap_handle_length =
-		sizeof(struct ceph_nfs_snapfh) >> 2;
+	static const int snap_handle_length = CEPH_FH_SNAPPED_INODE_SIZE;
 	struct ceph_nfs_snapfh *sfh = (void *)rawfh;
 	u64 snapid = ceph_snap(inode);
 	int ret;
@@ -88,10 +95,8 @@ static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
 			  struct inode *parent_inode)
 {
 	struct ceph_client *cl = ceph_inode_to_client(inode);
-	static const int handle_length =
-		sizeof(struct ceph_nfs_fh) >> 2;
-	static const int connected_handle_length =
-		sizeof(struct ceph_nfs_confh) >> 2;
+	static const int handle_length = CEPH_FH_BASIC_SIZE;
+	static const int connected_handle_length = CEPH_FH_WITH_PARENT_SIZE;
 	int type;
 
 	if (ceph_snap(inode) != CEPH_NOSNAP)
@@ -308,7 +313,7 @@ static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
 	if (fh_type != FILEID_INO32_GEN  &&
 	    fh_type != FILEID_INO32_GEN_PARENT)
 		return NULL;
-	if (fh_len < sizeof(*fh) / 4)
+	if (fh_len < sizeof(*fh) / BYTES_PER_U32)
 		return NULL;
 
 	doutc(fsc->client, "%llx\n", fh->ino);
@@ -427,7 +432,7 @@ static struct dentry *ceph_fh_to_parent(struct super_block *sb,
 
 	if (fh_type != FILEID_INO32_GEN_PARENT)
 		return NULL;
-	if (fh_len < sizeof(*cfh) / 4)
+	if (fh_len < sizeof(*cfh) / BYTES_PER_U32)
 		return NULL;
 
 	doutc(fsc->client, "%llx\n", cfh->parent_ino);
Signature · 1 line
2.48.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 73 lines · Viacheslav Dubeyko

From: Viacheslav Dubeyko <[email protected]>

The ceph/export.c contains very confusing logic of file handle size calculation based on hardcoded values. This patch makes the cleanup of this logic by means of introduction the named constants.

---
 fs/ceph/export.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
fs/ceph/export.c +13 -9
index 150076ced937..b2f2af104679 100644
@@ -33,12 +33,19 @@ struct ceph_nfs_snapfh {
        u32 hash;
 } __attribute__ ((packed));
 
+#define BYTES_PER_U32          (sizeof(u32))
+#define CEPH_FH_BASIC_SIZE \
+       (sizeof(struct ceph_nfs_fh) / BYTES_PER_U32)
+#define CEPH_FH_WITH_PARENT_SIZE \
+       (sizeof(struct ceph_nfs_confh) / BYTES_PER_U32)
+#define CEPH_FH_SNAPPED_INODE_SIZE \
+       (sizeof(struct ceph_nfs_snapfh) / BYTES_PER_U32)
+
 static int ceph_encode_snapfh(struct inode *inode, u32 *rawfh, int *max_len,
                              struct inode *parent_inode)
 {
        struct ceph_client *cl = ceph_inode_to_client(inode);
-       static const int snap_handle_length =
-               sizeof(struct ceph_nfs_snapfh) >> 2;
+       static const int snap_handle_length = CEPH_FH_SNAPPED_INODE_SIZE;
        struct ceph_nfs_snapfh *sfh = (void *)rawfh;
        u64 snapid = ceph_snap(inode);
        int ret;
@@ -88,10 +95,8 @@ static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
                          struct inode *parent_inode)
 {
        struct ceph_client *cl = ceph_inode_to_client(inode);
-       static const int handle_length =
-               sizeof(struct ceph_nfs_fh) >> 2;
-       static const int connected_handle_length =
-               sizeof(struct ceph_nfs_confh) >> 2;
+       static const int handle_length = CEPH_FH_BASIC_SIZE;
+       static const int connected_handle_length = CEPH_FH_WITH_PARENT_SIZE;
        int type;
 
        if (ceph_snap(inode) != CEPH_NOSNAP)
@@ -308,7 +313,7 @@ static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
        if (fh_type != FILEID_INO32_GEN  &&
            fh_type != FILEID_INO32_GEN_PARENT)
                return NULL;
-       if (fh_len < sizeof(*fh) / 4)
+       if (fh_len < sizeof(*fh) / BYTES_PER_U32)
                return NULL;
 
        doutc(fsc->client, "%llx\n", fh->ino);
@@ -427,7 +432,7 @@ static struct dentry *ceph_fh_to_parent(struct super_block *sb,
 
        if (fh_type != FILEID_INO32_GEN_PARENT)
                return NULL;
-       if (fh_len < sizeof(*cfh) / 4)
+       if (fh_len < sizeof(*cfh) / BYTES_PER_U32)
                return NULL;
 
        doutc(fsc->client, "%llx\n", cfh->parent_ino);
--

2.48.0