Skip to contentSkip to conversation

ceph: misc fix for fscrypt

PATCH In review [email protected] 2 participants 7 messages Mar 3, 2022 – Mar 5, 2022 Ceph client

lore
Share & tools
lore
#0 lore raw

v4 cover ceph: misc fix for fscrypt

From: Xiubo Li <[email protected]>

This patch series is base on the 'wip-fscrypt' branch in ceph-client.

V4:
- Remove the snapshot name support patches and will leave this to Luis.
- For the first commit to fail the request one, it's hasty to skip the corrupt dentries and risky, will improve this later in future. For now will fail it directly and propogate the errno to user space.

V3:
- Add more detail comments in the commit comments and code comments.
- Fix some bugs.
- Improved the patches.
- Remove the already merged patch.

V2:
- Fix several bugs, such as for the long snap name encrypt/dencrypt
- Skip double dencypting dentry names for readdir

Xiubo Li (2):
ceph: fail the request when failing to decode dentry names
ceph: do not dencrypt the dentry name twice for readdir

 fs/ceph/crypto.h     |  8 +++++
 fs/ceph/dir.c        | 71 ++++++++++++++++++++++++--------------------
 fs/ceph/inode.c      | 20 +++++++++++--
 fs/ceph/mds_client.c |  2 +-
 fs/ceph/mds_client.h |  1 +
 5 files changed, 66 insertions(+), 36 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v4 1/2 ceph: fail the request when failing to decode dentry names

From: Xiubo Li <[email protected]>

If we just skip the corrupt dentry names without setting the rde's offset it will crash in ceph_readdir():

------------[ cut here ]------------
kernel BUG at fs/ceph/dir.c:537!
invalid opcode: 0000 [#1] PREEMPT SMP KASAN NOPTI
CPU: 16 PID: 21641 Comm: ls Tainted: G            E     5.17.0-rc2+ #92
Hardware name: Red Hat RHEV Hypervisor, BIOS 1.11.0-2.el7 04/01/2014

The corresponding code in ceph_readdir() is:

	BUG_ON(rde->offset < ctx->pos);

For now let's just fail the readdir request since it's nasty to handle it and will do better error handling later in future.

---
 fs/ceph/dir.c        | 13 +++++++------
 fs/ceph/inode.c      |  5 +++--
 fs/ceph/mds_client.c |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
fs/ceph/dir.c +7 -6
index bdd757690a11..4da59810b036 100644
@@ -537,6 +537,13 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 					    .ctext_len	= rde->altname_len };
 		u32 olen = oname.len;
 
+		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
+		if (err) {
+			pr_err("%s unable to decode %.*s, got %d\n", __func__,
+			       rde->name_len, rde->name, err);
+			goto out;
+		}
+
 		BUG_ON(rde->offset < ctx->pos);
 		BUG_ON(!rde->inode.in);
 
@@ -545,12 +552,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		     i, rinfo->dir_nr, ctx->pos,
 		     rde->name_len, rde->name, &rde->inode.in);
 
-		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
-		if (err) {
-			dout("Unable to decode %.*s. Skipping it.\n", rde->name_len, rde->name);
-			continue;
-		}
-
 		if (!dir_emit(ctx, oname.name, oname.len,
 			      ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
 			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
fs/ceph/inode.c +3 -2
index d842ccb15667..e5a9838981ba 100644
@@ -1897,8 +1897,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 
 		err = ceph_fname_to_usr(&fname, &tname, &oname, &is_nokey);
 		if (err) {
-			dout("Unable to decode %.*s. Skipping it.", rde->name_len, rde->name);
-			continue;
+			pr_err("%s unable to decode %.*s, got %d\n", __func__,
+			       rde->name_len, rde->name, err);
+			goto out;
 		}
 
 		dname.name = oname.name;
fs/ceph/mds_client.c +1 -1
index 34fc7c226b0d..f0d2442187a3 100644
@@ -3449,7 +3449,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	if (err == 0) {
 		if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
 				    req->r_op == CEPH_MDS_OP_LSSNAP))
-			ceph_readdir_prepopulate(req, req->r_session);
+			err = ceph_readdir_prepopulate(req, req->r_session);
 	}
 	current->journal_info = NULL;
 	mutex_unlock(&req->r_fill_mutex);
Signature · 1 line
2.27.0
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] ceph: fail the request when failing to decode dentry names

Quoted 77 lines · On Thu, 2022-03-03 at 11:26 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

If we just skip the corrupt dentry names without setting the rde's offset it will crash in ceph_readdir():

------------[ cut here ]------------
kernel BUG at fs/ceph/dir.c:537!
invalid opcode: 0000 [#1] PREEMPT SMP KASAN NOPTI
CPU: 16 PID: 21641 Comm: ls Tainted: G            E     5.17.0-rc2+ #92
Hardware name: Red Hat RHEV Hypervisor, BIOS 1.11.0-2.el7 04/01/2014

The corresponding code in ceph_readdir() is:

	BUG_ON(rde->offset < ctx->pos);

For now let's just fail the readdir request since it's nasty to handle it and will do better error handling later in future.

---
 fs/ceph/dir.c        | 13 +++++++------
 fs/ceph/inode.c      |  5 +++--
 fs/ceph/mds_client.c |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
fs/ceph/dir.c +7 -6
index bdd757690a11..4da59810b036 100644
@@ -537,6 +537,13 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 					    .ctext_len	= rde->altname_len };
 		u32 olen = oname.len;
 
+		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
+		if (err) {
+			pr_err("%s unable to decode %.*s, got %d\n", __func__,
+			       rde->name_len, rde->name, err);
+			goto out;
+		}
+
 		BUG_ON(rde->offset < ctx->pos);
 		BUG_ON(!rde->inode.in);
 
@@ -545,12 +552,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		     i, rinfo->dir_nr, ctx->pos,
 		     rde->name_len, rde->name, &rde->inode.in);
 
-		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
-		if (err) {
-			dout("Unable to decode %.*s. Skipping it.\n", rde->name_len, rde->name);
-			continue;
-		}
-
 		if (!dir_emit(ctx, oname.name, oname.len,
 			      ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
 			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
fs/ceph/inode.c +3 -2
index d842ccb15667..e5a9838981ba 100644
@@ -1897,8 +1897,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 
 		err = ceph_fname_to_usr(&fname, &tname, &oname, &is_nokey);
 		if (err) {
-			dout("Unable to decode %.*s. Skipping it.", rde->name_len, rde->name);
-			continue;
+			pr_err("%s unable to decode %.*s, got %d\n", __func__,
+			       rde->name_len, rde->name, err);
+			goto out;
 		}
 
 		dname.name = oname.name;
fs/ceph/mds_client.c +0 -0
index 34fc7c226b0d..f0d2442187a3 100644

fs/ceph/mds_client.c static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) · line 3455

Quoted 8 lines
fs/ceph/mds_client.c +1 -1
@@ -3449,7 +3449,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
 	if (err == 0) {
 		if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
 				    req->r_op == CEPH_MDS_OP_LSSNAP))
-			ceph_readdir_prepopulate(req, req->r_session);
+			err = ceph_readdir_prepopulate(req, req->r_session);
 	}
 	current->journal_info = NULL;
 	mutex_unlock(&req->r_fill_mutex);
#3 lore raw

PATCH v4 2/2 ceph: do not dencrypt the dentry name twice for readdir

In reply to [email protected]

From: Xiubo Li <[email protected]>

For the readdir request the dentries will be pasred and dencrypted in ceph_readdir_prepopulate(). And in ceph_readdir() we could just get the dentry name from the dentry cache instead of parsing and dencrypting them again. This could improve performance.

---
 fs/ceph/crypto.h     |  8 +++++
 fs/ceph/dir.c        | 74 +++++++++++++++++++++++---------------------
 fs/ceph/inode.c      | 15 +++++++++
 fs/ceph/mds_client.h |  1 +
 4 files changed, 63 insertions(+), 35 deletions(-)
fs/ceph/crypto.h +8 -0
index 1e08f8a64ad6..9a00c60b8535 100644
@@ -83,6 +83,14 @@ static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
  */
 #define CEPH_NOHASH_NAME_MAX (189 - SHA256_DIGEST_SIZE)
 
+/*
+ * The encrypted long snap name will be in format of
+ * "_${ENCRYPTED-LONG-SNAP-NAME}_${INODE-NUM}". And will set the max longth
+ * to sizeof('_') + NAME_MAX + sizeof('_') + max of sizeof(${INO}) + extra 7
+ * bytes to align the total size to 8 bytes.
+ */
+#define CEPH_ENCRPTED_LONG_SNAP_NAME_MAX (1 + 255 + 1 + 16 + 7)
+
 void ceph_fscrypt_set_ops(struct super_block *sb);
 
 void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
fs/ceph/dir.c +39 -35

121 lines not on this page · raw message

fs/ceph/inode.c +15 -0
index e5a9838981ba..d0719feed792 100644
@@ -1902,6 +1902,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 			goto out;
 		}
 
+		rde->dentry = NULL;
 		dname.name = oname.name;
 		dname.len = oname.len;
 		dname.hash = full_name_hash(parent, dname.name, dname.len);
@@ -1962,6 +1963,12 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 			goto retry_lookup;
 		}
 
+		/*
+		 * ceph_readdir will use the dentry to get the name
+		 * to avoid doing the dencrypt again there.
+		 */
+		rde->dentry = dget(dn);
+
 		/* inode */
 		if (d_really_is_positive(dn)) {
 			in = d_inode(dn);
@@ -2024,6 +2031,14 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		dput(dn);
 	}
 out:
+	if (err) {
+		for (; i >= 0; i--) {
+			struct ceph_mds_reply_dir_entry *rde;
+
+			rde = rinfo->dir_entries + i;
+			dput(rde->dentry);
+		}
+	}
 	if (err == 0 && skipped == 0) {
 		set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
 		req->r_readdir_cache_idx = cache_ctl.index;
fs/ceph/mds_client.h +1 -0
index 0dfe24f94567..663d7754d57d 100644
@@ -96,6 +96,7 @@ struct ceph_mds_reply_info_in {
 };
 
 struct ceph_mds_reply_dir_entry {
+	struct dentry		      *dentry;
 	char                          *name;
 	u8			      *altname;
 	u32                           name_len;
Signature · 1 line
2.27.0
author
#4 lore raw

Re: [PATCH v4 2/2] ceph: do not dencrypt the dentry name twice for readdir

Quoted 49 lines · On 3/3/22 11:26 AM, [email protected] wrote:

From: Xiubo Li <[email protected]>

For the readdir request the dentries will be pasred and dencrypted in ceph_readdir_prepopulate(). And in ceph_readdir() we could just get the dentry name from the dentry cache instead of parsing and dencrypting them again. This could improve performance.

---
  fs/ceph/crypto.h     |  8 +++++
  fs/ceph/dir.c        | 74 +++++++++++++++++++++++---------------------
  fs/ceph/inode.c      | 15 +++++++++
  fs/ceph/mds_client.h |  1 +
  4 files changed, 63 insertions(+), 35 deletions(-)
fs/ceph/crypto.h +8 -0
index 1e08f8a64ad6..9a00c60b8535 100644
@@ -83,6 +83,14 @@ static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
   */
  #define CEPH_NOHASH_NAME_MAX (189 - SHA256_DIGEST_SIZE)
 
+/*
+ * The encrypted long snap name will be in format of
+ * "_${ENCRYPTED-LONG-SNAP-NAME}_${INODE-NUM}". And will set the max longth
+ * to sizeof('_') + NAME_MAX + sizeof('_') + max of sizeof(${INO}) + extra 7
+ * bytes to align the total size to 8 bytes.
+ */
+#define CEPH_ENCRPTED_LONG_SNAP_NAME_MAX (1 + 255 + 1 + 16 + 7)
+
  void ceph_fscrypt_set_ops(struct super_block *sb);
 
  void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
fs/ceph/dir.c +1 -2
index 4da59810b036..fa3da3b29130 100644
@@ -316,8 +316,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  	int err;
  	unsigned frag = -1;
  	struct ceph_mds_reply_info_parsed *rinfo;
-	struct fscrypt_str tname = FSTR_INIT(NULL, 0);
-	struct fscrypt_str oname = FSTR_INIT(NULL, 0);
+	char *dentry_name = NULL;
 
  	dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
  	if (dfi->file_info.flags & CEPH_F_ATEND)

fs/ceph/dir.c static int ceph_readdir(struct file *file, struct dir_context *ctx) · line 351

Quoted 7 lines
fs/ceph/dir.c +0 -4
@@ -345,10 +344,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Yeah, we need this here, because it will be used in the "__dcache_readdir()".

Will add it back in next version.

Quoted 160 lines
fs/ceph/dir.c +38 -29
  	spin_lock(&ci->i_ceph_lock);
  	/* request Fx cap. if have Fx, we don't need to release Fs cap
  	 * for later create/unlink. */
@@ -369,14 +364,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		spin_unlock(&ci->i_ceph_lock);
  	}
 
-	err = ceph_fname_alloc_buffer(inode, &tname);
-	if (err < 0)
-		goto out;
-
-	err = ceph_fname_alloc_buffer(inode, &oname);
-	if (err < 0)
-		goto out;
-
  	/* proceed with a normal readdir */
  more:
  	/* do we have the correct frag content buffered? */
@@ -528,42 +515,59 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  			}
  		}
  	}
+
+	dentry_name = kmalloc(CEPH_ENCRPTED_LONG_SNAP_NAME_MAX, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		ceph_mdsc_put_request(dfi->last_readdir);
+		dfi->last_readdir = NULL;
+		goto out;
+	}
+
  	for (; i < rinfo->dir_nr; i++) {
  		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
-		struct ceph_fname fname = { .dir	= inode,
-					    .name	= rde->name,
-					    .name_len	= rde->name_len,
-					    .ctext	= rde->altname,
-					    .ctext_len	= rde->altname_len };
-		u32 olen = oname.len;
-
-		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
-		if (err) {
-			pr_err("%s unable to decode %.*s, got %d\n", __func__,
-			       rde->name_len, rde->name, err);
-			goto out;
-		}
+		struct dentry *dn = rde->dentry;
+		int name_len;
 
  		BUG_ON(rde->offset < ctx->pos);
  		BUG_ON(!rde->inode.in);
+		BUG_ON(!rde->dentry);
 
  		ctx->pos = rde->offset;
-		dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
-		     i, rinfo->dir_nr, ctx->pos,
-		     rde->name_len, rde->name, &rde->inode.in);
 
-		if (!dir_emit(ctx, oname.name, oname.len,
+		spin_lock(&dn->d_lock);
+		memcpy(dentry_name, dn->d_name.name, dn->d_name.len);
+		name_len = dn->d_name.len;
+		spin_unlock(&dn->d_lock);
+
+		dentry_name[name_len] = '\0';
+		dout("readdir (%d/%d) -> %llx '%s' %p\n",
+		     i, rinfo->dir_nr, ctx->pos, dentry_name, &rde->inode.in);
+
+		dput(dn);
+		rde->dentry = NULL;
+
+		if (!dir_emit(ctx, dentry_name, name_len,
  			      ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
  			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
  			dout("filldir stopping us...\n");
+
+			/*
+			 * dput the rest dentries. Must do this before
+			 * releasing the request.
+			 */
+			for (++i; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
+
  			err = 0;
  			ceph_mdsc_put_request(dfi->last_readdir);
  			dfi->last_readdir = NULL;
  			goto out;
  		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
  		ctx->pos++;
  	}
 
@@ -621,8 +625,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  	err = 0;
  	dout("readdir %p file %p done.\n", inode, file);
  out:
-	ceph_fname_free_buffer(inode, &tname);
-	ceph_fname_free_buffer(inode, &oname);
+	if (dentry_name)
+		kfree(dentry_name);
  	return err;
  }
 
fs/ceph/inode.c +15 -0
index e5a9838981ba..d0719feed792 100644
@@ -1902,6 +1902,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  			goto out;
  		}
 
+		rde->dentry = NULL;
  		dname.name = oname.name;
  		dname.len = oname.len;
  		dname.hash = full_name_hash(parent, dname.name, dname.len);
@@ -1962,6 +1963,12 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  			goto retry_lookup;
  		}
 
+		/*
+		 * ceph_readdir will use the dentry to get the name
+		 * to avoid doing the dencrypt again there.
+		 */
+		rde->dentry = dget(dn);
+
  		/* inode */
  		if (d_really_is_positive(dn)) {
  			in = d_inode(dn);
@@ -2024,6 +2031,14 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  		dput(dn);
  	}
  out:
+	if (err) {
+		for (; i >= 0; i--) {
+			struct ceph_mds_reply_dir_entry *rde;
+
+			rde = rinfo->dir_entries + i;
+			dput(rde->dentry);
+		}
+	}
  	if (err == 0 && skipped == 0) {
  		set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
  		req->r_readdir_cache_idx = cache_ctl.index;
fs/ceph/mds_client.h +1 -0
index 0dfe24f94567..663d7754d57d 100644
@@ -96,6 +96,7 @@ struct ceph_mds_reply_info_in {
  };
 
  struct ceph_mds_reply_dir_entry {
+	struct dentry		      *dentry;
  	char                          *name;
  	u8			      *altname;
  	u32                           name_len;
#5 lore raw

Re: [PATCH v4 2/2] ceph: do not dencrypt the dentry name twice for readdir

In reply to [email protected]
Quoted 21 lines · On Thu, 2022-03-03 at 11:26 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

For the readdir request the dentries will be pasred and dencrypted in ceph_readdir_prepopulate(). And in ceph_readdir() we could just get the dentry name from the dentry cache instead of parsing and dencrypting them again. This could improve performance.

---
 fs/ceph/crypto.h     |  8 +++++
 fs/ceph/dir.c        | 74 +++++++++++++++++++++++---------------------
 fs/ceph/inode.c      | 15 +++++++++
 fs/ceph/mds_client.h |  1 +
 4 files changed, 63 insertions(+), 35 deletions(-)
fs/ceph/crypto.h +0 -0
index 1e08f8a64ad6..9a00c60b8535 100644
@@ -83,6 +83,14 @@ static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
  */

fs/ceph/crypto.h static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa) · line 93

Quoted 10 lines
fs/ceph/crypto.h +8 -0
 #define CEPH_NOHASH_NAME_MAX (189 - SHA256_DIGEST_SIZE)
 
+/*
+ * The encrypted long snap name will be in format of
+ * "_${ENCRYPTED-LONG-SNAP-NAME}_${INODE-NUM}". And will set the max longth
+ * to sizeof('_') + NAME_MAX + sizeof('_') + max of sizeof(${INO}) + extra 7
+ * bytes to align the total size to 8 bytes.
+ */
+#define CEPH_ENCRPTED_LONG_SNAP_NAME_MAX (1 + 255 + 1 + 16 + 7)
+

Maybe CEPH_ENCRYPTED_LONG_SNAP_NAME_MAX instead?

Quoted 46 lines
fs/ceph/crypto.h +0 -0
 void ceph_fscrypt_set_ops(struct super_block *sb);
 
 void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
fs/ceph/dir.c +1 -14
index 4da59810b036..fa3da3b29130 100644
@@ -316,8 +316,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 	int err;
 	unsigned frag = -1;
 	struct ceph_mds_reply_info_parsed *rinfo;
-	struct fscrypt_str tname = FSTR_INIT(NULL, 0);
-	struct fscrypt_str oname = FSTR_INIT(NULL, 0);
+	char *dentry_name = NULL;
 
 	dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
 	if (dfi->file_info.flags & CEPH_F_ATEND)
@@ -345,10 +344,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		ctx->pos = 2;
 	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-
 	spin_lock(&ci->i_ceph_lock);
 	/* request Fx cap. if have Fx, we don't need to release Fs cap
 	 * for later create/unlink. */
@@ -369,14 +364,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		spin_unlock(&ci->i_ceph_lock);
 	}
 
-	err = ceph_fname_alloc_buffer(inode, &tname);
-	if (err < 0)
-		goto out;
-
-	err = ceph_fname_alloc_buffer(inode, &oname);
-	if (err < 0)
-		goto out;
-
 	/* proceed with a normal readdir */
 more:
 	/* do we have the correct frag content buffered? */
@@ -528,42 +515,59 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 			}
 		}

fs/ceph/dir.c static int ceph_readdir(struct file *file, struct dir_context *ctx) · line 526

Quoted 10 lines
fs/ceph/dir.c +9 -0
 	}
+
+	dentry_name = kmalloc(CEPH_ENCRPTED_LONG_SNAP_NAME_MAX, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		ceph_mdsc_put_request(dfi->last_readdir);
+		dfi->last_readdir = NULL;
+		goto out;
+	}
+

If this happens (or an earlier error), all of the dentry references will leak. I think you probably need to move the cleanup into destroy_reply_info().

Quoted 129 lines
fs/ceph/dir.c +29 -21
 	for (; i < rinfo->dir_nr; i++) {
 		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
-		struct ceph_fname fname = { .dir	= inode,
-					    .name	= rde->name,
-					    .name_len	= rde->name_len,
-					    .ctext	= rde->altname,
-					    .ctext_len	= rde->altname_len };
-		u32 olen = oname.len;
-
-		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
-		if (err) {
-			pr_err("%s unable to decode %.*s, got %d\n", __func__,
-			       rde->name_len, rde->name, err);
-			goto out;
-		}
+		struct dentry *dn = rde->dentry;
+		int name_len;
 
 		BUG_ON(rde->offset < ctx->pos);
 		BUG_ON(!rde->inode.in);
+		BUG_ON(!rde->dentry);
 
 		ctx->pos = rde->offset;
-		dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
-		     i, rinfo->dir_nr, ctx->pos,
-		     rde->name_len, rde->name, &rde->inode.in);
 
-		if (!dir_emit(ctx, oname.name, oname.len,
+		spin_lock(&dn->d_lock);
+		memcpy(dentry_name, dn->d_name.name, dn->d_name.len);
+		name_len = dn->d_name.len;
+		spin_unlock(&dn->d_lock);
+
+		dentry_name[name_len] = '\0';
+		dout("readdir (%d/%d) -> %llx '%s' %p\n",
+		     i, rinfo->dir_nr, ctx->pos, dentry_name, &rde->inode.in);
+
+		dput(dn);
+		rde->dentry = NULL;
+
+		if (!dir_emit(ctx, dentry_name, name_len,
 			      ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
 			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
 			dout("filldir stopping us...\n");
+
+			/*
+			 * dput the rest dentries. Must do this before
+			 * releasing the request.
+			 */
+			for (++i; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
+
 			err = 0;
 			ceph_mdsc_put_request(dfi->last_readdir);
 			dfi->last_readdir = NULL;
 			goto out;
 		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
 		ctx->pos++;
 	}
 
@@ -621,8 +625,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 	err = 0;
 	dout("readdir %p file %p done.\n", inode, file);
 out:
-	ceph_fname_free_buffer(inode, &tname);
-	ceph_fname_free_buffer(inode, &oname);
+	if (dentry_name)
+		kfree(dentry_name);
 	return err;
 }
 
fs/ceph/inode.c +15 -0
index e5a9838981ba..d0719feed792 100644
@@ -1902,6 +1902,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 			goto out;
 		}
 
+		rde->dentry = NULL;
 		dname.name = oname.name;
 		dname.len = oname.len;
 		dname.hash = full_name_hash(parent, dname.name, dname.len);
@@ -1962,6 +1963,12 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 			goto retry_lookup;
 		}
 
+		/*
+		 * ceph_readdir will use the dentry to get the name
+		 * to avoid doing the dencrypt again there.
+		 */
+		rde->dentry = dget(dn);
+
 		/* inode */
 		if (d_really_is_positive(dn)) {
 			in = d_inode(dn);
@@ -2024,6 +2031,14 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		dput(dn);
 	}
 out:
+	if (err) {
+		for (; i >= 0; i--) {
+			struct ceph_mds_reply_dir_entry *rde;
+
+			rde = rinfo->dir_entries + i;
+			dput(rde->dentry);
+		}
+	}
 	if (err == 0 && skipped == 0) {
 		set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
 		req->r_readdir_cache_idx = cache_ctl.index;
fs/ceph/mds_client.h +1 -0
index 0dfe24f94567..663d7754d57d 100644
@@ -96,6 +96,7 @@ struct ceph_mds_reply_info_in {
 };
 
 struct ceph_mds_reply_dir_entry {
+	struct dentry		      *dentry;
 	char                          *name;
 	u8			      *altname;
 	u32                           name_len;
Signature · 1 line
Jeff Layton <[email protected]>
author
#6 lore raw

Re: [PATCH v4 2/2] ceph: do not dencrypt the dentry name twice for readdir

Quoted 24 lines · Jeff Layton
Quoted 23 lines · On Thu, 2022-03-03 at 11:26 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

For the readdir request the dentries will be pasred and dencrypted in ceph_readdir_prepopulate(). And in ceph_readdir() we could just get the dentry name from the dentry cache instead of parsing and dencrypting them again. This could improve performance.

---
  fs/ceph/crypto.h     |  8 +++++
  fs/ceph/dir.c        | 74 +++++++++++++++++++++++---------------------
  fs/ceph/inode.c      | 15 +++++++++
  fs/ceph/mds_client.h |  1 +
  4 files changed, 63 insertions(+), 35 deletions(-)
fs/ceph/crypto.h +0 -0
index 1e08f8a64ad6..9a00c60b8535 100644
@@ -83,6 +83,14 @@ static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
   */
  #define CEPH_NOHASH_NAME_MAX (189 - SHA256_DIGEST_SIZE)
 
Quoted 9 lines

fs/ceph/crypto.h static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa) · line 93

Quoted 8 lines
fs/ceph/crypto.h +8 -0
+/*
+ * The encrypted long snap name will be in format of
+ * "_${ENCRYPTED-LONG-SNAP-NAME}_${INODE-NUM}". And will set the max longth
+ * to sizeof('_') + NAME_MAX + sizeof('_') + max of sizeof(${INO}) + extra 7
+ * bytes to align the total size to 8 bytes.
+ */
+#define CEPH_ENCRPTED_LONG_SNAP_NAME_MAX (1 + 255 + 1 + 16 + 7)
+

Maybe CEPH_ENCRYPTED_LONG_SNAP_NAME_MAX instead?

Sure, will fix it.

Quoted 52 lines
Quoted 52 lines
fs/ceph/crypto.h +0 -0
  void ceph_fscrypt_set_ops(struct super_block *sb);
 
  void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
fs/ceph/dir.c +5 -14
index 4da59810b036..fa3da3b29130 100644
@@ -316,8 +316,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  	int err;
  	unsigned frag = -1;
  	struct ceph_mds_reply_info_parsed *rinfo;
-	struct fscrypt_str tname = FSTR_INIT(NULL, 0);
-	struct fscrypt_str oname = FSTR_INIT(NULL, 0);
+	char *dentry_name = NULL;
 
  	dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
  	if (dfi->file_info.flags & CEPH_F_ATEND)
@@ -345,10 +344,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-
  	spin_lock(&ci->i_ceph_lock);
  	/* request Fx cap. if have Fx, we don't need to release Fs cap
  	 * for later create/unlink. */
@@ -369,14 +364,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		spin_unlock(&ci->i_ceph_lock);
  	}
 
-	err = ceph_fname_alloc_buffer(inode, &tname);
-	if (err < 0)
-		goto out;
-
-	err = ceph_fname_alloc_buffer(inode, &oname);
-	if (err < 0)
-		goto out;
-
  	/* proceed with a normal readdir */
  more:
  	/* do we have the correct frag content buffered? */
@@ -528,42 +515,59 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  			}
  		}
  	}
+
+	dentry_name = kmalloc(CEPH_ENCRPTED_LONG_SNAP_NAME_MAX, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
Quoted 8 lines

fs/ceph/dir.c static int ceph_readdir(struct file *file, struct dir_context *ctx) · line 526

Quoted 5 lines
fs/ceph/dir.c +5 -0
+		ceph_mdsc_put_request(dfi->last_readdir);
+		dfi->last_readdir = NULL;
+		goto out;
+	}
+

If this happens (or an earlier error), all of the dentry references will leak. I think you probably need to move the cleanup into destroy_reply_info().

Yeah, I will fix it too.

Thanks.

Quoted 130 lines
Quoted 130 lines
fs/ceph/dir.c +29 -21
  	for (; i < rinfo->dir_nr; i++) {
  		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
-		struct ceph_fname fname = { .dir	= inode,
-					    .name	= rde->name,
-					    .name_len	= rde->name_len,
-					    .ctext	= rde->altname,
-					    .ctext_len	= rde->altname_len };
-		u32 olen = oname.len;
-
-		err = ceph_fname_to_usr(&fname, &tname, &oname, NULL);
-		if (err) {
-			pr_err("%s unable to decode %.*s, got %d\n", __func__,
-			       rde->name_len, rde->name, err);
-			goto out;
-		}
+		struct dentry *dn = rde->dentry;
+		int name_len;
 
  		BUG_ON(rde->offset < ctx->pos);
  		BUG_ON(!rde->inode.in);
+		BUG_ON(!rde->dentry);
 
  		ctx->pos = rde->offset;
-		dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
-		     i, rinfo->dir_nr, ctx->pos,
-		     rde->name_len, rde->name, &rde->inode.in);
 
-		if (!dir_emit(ctx, oname.name, oname.len,
+		spin_lock(&dn->d_lock);
+		memcpy(dentry_name, dn->d_name.name, dn->d_name.len);
+		name_len = dn->d_name.len;
+		spin_unlock(&dn->d_lock);
+
+		dentry_name[name_len] = '\0';
+		dout("readdir (%d/%d) -> %llx '%s' %p\n",
+		     i, rinfo->dir_nr, ctx->pos, dentry_name, &rde->inode.in);
+
+		dput(dn);
+		rde->dentry = NULL;
+
+		if (!dir_emit(ctx, dentry_name, name_len,
  			      ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
  			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
  			dout("filldir stopping us...\n");
+
+			/*
+			 * dput the rest dentries. Must do this before
+			 * releasing the request.
+			 */
+			for (++i; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
+
  			err = 0;
  			ceph_mdsc_put_request(dfi->last_readdir);
  			dfi->last_readdir = NULL;
  			goto out;
  		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
  		ctx->pos++;
  	}
 
@@ -621,8 +625,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  	err = 0;
  	dout("readdir %p file %p done.\n", inode, file);
  out:
-	ceph_fname_free_buffer(inode, &tname);
-	ceph_fname_free_buffer(inode, &oname);
+	if (dentry_name)
+		kfree(dentry_name);
  	return err;
  }
 
fs/ceph/inode.c +15 -0
index e5a9838981ba..d0719feed792 100644
@@ -1902,6 +1902,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  			goto out;
  		}
 
+		rde->dentry = NULL;
  		dname.name = oname.name;
  		dname.len = oname.len;
  		dname.hash = full_name_hash(parent, dname.name, dname.len);
@@ -1962,6 +1963,12 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  			goto retry_lookup;
  		}
 
+		/*
+		 * ceph_readdir will use the dentry to get the name
+		 * to avoid doing the dencrypt again there.
+		 */
+		rde->dentry = dget(dn);
+
  		/* inode */
  		if (d_really_is_positive(dn)) {
  			in = d_inode(dn);
@@ -2024,6 +2031,14 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  		dput(dn);
  	}
  out:
+	if (err) {
+		for (; i >= 0; i--) {
+			struct ceph_mds_reply_dir_entry *rde;
+
+			rde = rinfo->dir_entries + i;
+			dput(rde->dentry);
+		}
+	}
  	if (err == 0 && skipped == 0) {
  		set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
  		req->r_readdir_cache_idx = cache_ctl.index;
fs/ceph/mds_client.h +1 -0
index 0dfe24f94567..663d7754d57d 100644
@@ -96,6 +96,7 @@ struct ceph_mds_reply_info_in {
  };
 
  struct ceph_mds_reply_dir_entry {
+	struct dentry		      *dentry;
  	char                          *name;
  	u8			      *altname;
  	u32                           name_len;