Skip to contentSkip to conversation

ceph: encrypt the snapshot directories

PATCH Superseded [email protected] 2 participants 17 messages Mar 1, 2022 Ceph client

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover ceph: encrypt the snapshot directories

From: Xiubo Li <[email protected]>

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

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

======

NOTE: This patch series won't fix the long snap shot issue as Luis is working on that.

Xiubo Li (7):
ceph: fail the request when failing to decode dentry names
ceph: skip the memories when received a higher version of message
ceph: do not dencrypt the dentry name twice for readdir
ceph: add ceph_get_snap_parent_inode() support
ceph: use the parent inode of '.snap' to dencrypt the names for
readdir
ceph: use the parent inode of '.snap' to encrypt name to build path
ceph: try to encrypt/decrypt long snap name

 fs/ceph/crypto.c     |  75 ++++++++++++++++++++++++++---
 fs/ceph/crypto.h     |   2 +-
 fs/ceph/dir.c        |  87 +++++++++++++++++++---------------
 fs/ceph/inode.c      | 110 ++++++++++++++++++++++++++++++++++++++-----
 fs/ceph/mds_client.c |  59 ++++++++++++++---------
 fs/ceph/mds_client.h |   3 ++
 fs/ceph/snap.c       |  24 ++++++++++
 fs/ceph/super.h      |   2 +
 8 files changed, 286 insertions(+), 76 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/7 ceph: fail the request when failing to decode dentry names

From: Xiubo Li <[email protected]>

------------[ 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);
---
 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 a449f4a07c07..6be0c1f793c2 100644
@@ -534,6 +534,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);
 
@@ -542,12 +549,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 8b0832271fdf..2bc2f02b84e8 100644
@@ -1898,8 +1898,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 914a6e68bb56..94b4c6508044 100644
@@ -3474,7 +3474,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
#2 lore raw

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

Quoted 55 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

------------[ 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);
---
 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 a449f4a07c07..6be0c1f793c2 100644
@@ -534,6 +534,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);
 
@@ -542,12 +549,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 +0 -0
index 8b0832271fdf..2bc2f02b84e8 100644

fs/ceph/inode.c int ceph_readdir_prepopulate(struct ceph_mds_request *req, · line 1905

Quoted 10 lines
fs/ceph/inode.c +3 -2
@@ -1898,8 +1898,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;
 		}
 

Is this really an improvement? Suppose I have one dentry with a corrupt name. Do I want to fail a readdir request which might allow me to get at other dentries in that directory that isn't corrupt?

Maybe we should try to emit some placeholder there?

Quoted 14 lines
fs/ceph/inode.c +0 -0
 		dname.name = oname.name;
fs/ceph/mds_client.c +1 -1
index 914a6e68bb56..94b4c6508044 100644
@@ -3474,7 +3474,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
Jeff Layton <[email protected]>
author
#3 lore raw

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

Quoted 57 lines · Jeff Layton
Quoted 56 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

------------[ 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);
---
  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 a449f4a07c07..6be0c1f793c2 100644
@@ -534,6 +534,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);
 
@@ -542,12 +549,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 +0 -0
index 8b0832271fdf..2bc2f02b84e8 100644
@@ -1898,8 +1898,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 
Quoted 11 lines

fs/ceph/inode.c int ceph_readdir_prepopulate(struct ceph_mds_request *req, · line 1905

Quoted 9 lines
fs/ceph/inode.c +3 -2
  		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;
  		}
 

Is this really an improvement?

Yeah, if we just continue without setting the rde->offset it will crash in "BUG_ON(rde->offset < ctx->pos);" in ceph_readdir().

Quoted 3 lines

Suppose I have one dentry with a corrupt
name. Do I want to fail a readdir request which might allow me to get at other dentries in that directory that isn't corrupt?

It's a little hard to handle the code in ceph_readdir():

 503         /* search start position */
 504         if (rinfo->dir_nr > 0) {
 505                 int step, nr = rinfo->dir_nr;
 506                 while (nr > 0) {
 507                         step = nr >> 1;
 508                         if (rinfo->dir_entries[i + step].offset < ctx->pos) { 509                                 i +=  step + 1;
 510                                 nr -= step + 1;
 511                         } else {
 512                                 nr = step;
 513                         }
 514                 }
 515         }

In this case how to set the rde->offset ?

Quoted 18 lines

Maybe we should try to emit some placeholder there?

Quoted 14 lines
fs/ceph/inode.c +0 -0
  		dname.name = oname.name;
fs/ceph/mds_client.c +1 -1
index 914a6e68bb56..94b4c6508044 100644
@@ -3474,7 +3474,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);
#4 lore raw

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

Quoted 70 lines · Xiubo Li
Quoted 57 lines · Jeff Layton
Quoted 56 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

------------[ 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);
---
  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 a449f4a07c07..6be0c1f793c2 100644
@@ -534,6 +534,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);
 
@@ -542,12 +549,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 +0 -0
index 8b0832271fdf..2bc2f02b84e8 100644
@@ -1898,8 +1898,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 
Quoted 11 lines

fs/ceph/inode.c int ceph_readdir_prepopulate(struct ceph_mds_request *req, · line 1905

Quoted 9 lines
fs/ceph/inode.c +3 -2
  		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;
  		}
 

Is this really an improvement?

Quoted 4 lines

Yeah, if we just continue without setting the rde->offset it will crash in "BUG_ON(rde->offset < ctx->pos);" in ceph_readdir().

Ok.

Quoted 21 lines
Quoted 3 lines

Suppose I have one dentry with a corrupt
name. Do I want to fail a readdir request which might allow me to get at other dentries in that directory that isn't corrupt?

It's a little hard to handle the code in ceph_readdir():

 503         /* search start position */
 504         if (rinfo->dir_nr > 0) {
 505                 int step, nr = rinfo->dir_nr;
 506                 while (nr > 0) {
 507                         step = nr >> 1;
 508                         if (rinfo->dir_entries[i + step].offset <
ctx->pos) {
 509                                 i +=  step + 1;
 510                                 nr -= step + 1;
 511                         } else {
 512                                 nr = step;
 513                         }
 514                 }
 515         }

Quoted 3 lines

In this case how to set the rde->offset ?

Yeah, that is the nasty part. I would probably just pretend that the corrupt dentry doesn't exist. Offsets are set by ceph_make_fpos, AFAICT, and I don't think skipping one should affect the position of the other.

OTOH, this is just a nice-to-have thing. If it's too nasty to deal with, we can just return an error for now and aim to do better error handling here later.

Quoted 19 lines
Quoted 18 lines

Maybe we should try to emit some placeholder there?

Quoted 14 lines
fs/ceph/inode.c +0 -0
  		dname.name = oname.name;
fs/ceph/mds_client.c +1 -1
index 914a6e68bb56..94b4c6508044 100644
@@ -3474,7 +3474,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
Jeff Layton <[email protected]>
#5 lore raw

PATCH v2 2/7 ceph: skip the memories when received a higher version of message

In reply to [email protected]

From: Xiubo Li <[email protected]>

We should skip the extra memories which are from the higher version just likes the libcephfs client does.

URL: https://tracker.ceph.com/issues/54430

---
 fs/ceph/mds_client.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ceph/mds_client.c +2 -0
index 94b4c6508044..3dea96df4769 100644
@@ -326,6 +326,7 @@ static int parse_reply_info_lease(void **p, void *end,
 			goto bad;
 
 		ceph_decode_32_safe(p, end, struct_len, bad);
+		end = *p + struct_len;
 	} else {
 		struct_len = sizeof(**lease);
 		*altname_len = 0;
@@ -346,6 +347,7 @@ static int parse_reply_info_lease(void **p, void *end,
 			*altname = NULL;
 			*altname_len = 0;
 		}
+		*p = end;
 	}
 	return 0;
 bad:
Signature · 1 line
2.27.0
#6 lore raw

PATCH v2 3/7 ceph: do not dencrypt the dentry name twice for readdir

In reply to [email protected]

From: Xiubo Li <[email protected]>

---
 fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
 fs/ceph/inode.c      | 15 ++++++++++
 fs/ceph/mds_client.h |  1 +
 3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +31 -35
index 6be0c1f793c2..e3917b4426e8 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? */
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 			}
 		}
 	}
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		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");
 			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
 			goto out;
 		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
 		ctx->pos++;
 	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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
#7 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 24 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
 fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
 fs/ceph/inode.c      | 15 ++++++++++
 fs/ceph/mds_client.h |  1 +
 3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Quoted 19 lines
fs/ceph/dir.c +0 -8
 	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? */

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

Quoted 10 lines
fs/ceph/dir.c +7 -0
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 			}
 		}
 	}
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

Quoted 5 lines
fs/ceph/dir.c +0 -3
 	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,

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

Quoted 10 lines
fs/ceph/dir.c +0 -10
-					    .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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

Quoted 99 lines
fs/ceph/dir.c +23 -8
+		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");
 			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
 			goto out;
 		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
 		ctx->pos++;
 	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

Signature · 1 line
Jeff Layton <[email protected]>
author
#8 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 28 lines · Jeff Layton
Quoted 27 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
  fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
  fs/ceph/inode.c      | 15 ++++++++++
  fs/ceph/mds_client.h |  1 +
  3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
  	}
 
Quoted 7 lines

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

Quoted 4 lines
fs/ceph/dir.c +0 -4
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Since the dentry names are already decrypted in ceph_readdir_prepopulate(), and this patch is trying to avoid do the same thing again here after that.

Because in ceph_readdir() this isn't needed any more. And in other places such as build path it will do it when needed.

Quoted 23 lines
Quoted 23 lines
fs/ceph/dir.c +0 -8
  	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? */
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  			}
  		}
  	}
Quoted 8 lines

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

Quoted 7 lines
fs/ceph/dir.c +7 -0
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

The long snap name in format of "_${ENCRYPTED-SNAP-NAME}_${INO}", the max length will be 1 +256 + 16.

Quoted 11 lines
Quoted 11 lines
fs/ceph/dir.c +0 -7
  	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;
-
Quoted 8 lines

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

Quoted 6 lines
fs/ceph/dir.c +0 -6
-		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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

In ceph_readdir_prepopulate(), and it will set the rde->dentry, and here we can get the name from the dentry directly instead of decrypt it again.

Quoted 110 lines
Quoted 100 lines
fs/ceph/dir.c +23 -8
+		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");
  			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
  			goto out;
  		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
  		ctx->pos++;
  	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

#9 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 76 lines · Xiubo Li
Quoted 28 lines · Jeff Layton
Quoted 27 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
  fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
  fs/ceph/inode.c      | 15 ++++++++++
  fs/ceph/mds_client.h |  1 +
  3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
  	}
 
Quoted 7 lines

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

Quoted 4 lines
fs/ceph/dir.c +0 -4
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Since the dentry names are already decrypted in ceph_readdir_prepopulate(), and this patch is trying to avoid do the same thing again here after that.

Because in ceph_readdir() this isn't needed any more. And in other places such as build path it will do it when needed.

Quoted 23 lines
Quoted 23 lines
fs/ceph/dir.c +0 -8
  	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? */
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  			}
  		}
  	}
Quoted 8 lines

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

Quoted 7 lines
fs/ceph/dir.c +7 -0
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

Quoted 3 lines

The long snap name in format of "_${ENCRYPTED-SNAP-NAME}_${INO}", the max length will be 1 +256 + 16.

Ok. Best to make a named constant definition for this then. Bare numbers in an allocation like this make it hard to understand what's going on.

Quoted 135 lines
Quoted 12 lines
Quoted 12 lines
fs/ceph/dir.c +0 -7
  	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;
-
Quoted 8 lines

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

Quoted 6 lines
fs/ceph/dir.c +0 -6
-		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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

In ceph_readdir_prepopulate(), and it will set the rde->dentry, and here we can get the name from the dentry directly instead of decrypt it again.

Quoted 110 lines
Quoted 100 lines
fs/ceph/dir.c +23 -8
+		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");
  			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
  			goto out;
  		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
  		ctx->pos++;
  	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

Signature · 1 line
Jeff Layton <[email protected]>
author
#10 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 73 lines · Jeff Layton
Quoted 72 lines · Xiubo Li
Quoted 28 lines · Jeff Layton
Quoted 27 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
   fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
   fs/ceph/inode.c      | 15 ++++++++++
   fs/ceph/mds_client.h |  1 +
   3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
   	}
 
Quoted 6 lines

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

Quoted 4 lines
fs/ceph/dir.c +0 -4
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Since the dentry names are already decrypted in ceph_readdir_prepopulate(), and this patch is trying to avoid do the same thing again here after that.

Because in ceph_readdir() this isn't needed any more. And in other places such as build path it will do it when needed.

Quoted 30 lines
Quoted 19 lines
fs/ceph/dir.c +0 -8
   	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? */

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

Quoted 10 lines
fs/ceph/dir.c +7 -0
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
   			}
   		}
   	}
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

Quoted 6 lines
Quoted 3 lines

The long snap name in format of "_${ENCRYPTED-SNAP-NAME}_${INO}", the max length will be 1 +256 + 16.

Ok. Best to make a named constant definition for this then. Bare numbers in an allocation like this make it hard to understand what's going on.

Sure.

So will this patch still makes sense ?

From my test with thousands of dentries this could save a lot of time.

Quoted 129 lines
Quoted 129 lines
Quoted 10 lines
Quoted 10 lines
fs/ceph/dir.c +0 -7
   	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;
-
Quoted 8 lines

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

Quoted 6 lines
fs/ceph/dir.c +0 -6
-		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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

In ceph_readdir_prepopulate(), and it will set the rde->dentry, and here we can get the name from the dentry directly instead of decrypt it again.

Quoted 108 lines
Quoted 99 lines
fs/ceph/dir.c +23 -8
+		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");
   			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
   			goto out;
   		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
   		ctx->pos++;
   	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

#11 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 77 lines · Xiubo Li
Quoted 76 lines · Jeff Layton
Quoted 75 lines · Xiubo Li
Quoted 28 lines · Jeff Layton
Quoted 27 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
   fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
   fs/ceph/inode.c      | 15 ++++++++++
   fs/ceph/mds_client.h |  1 +
   3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
   	}
 
Quoted 6 lines

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

Quoted 4 lines
fs/ceph/dir.c +0 -4
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Since the dentry names are already decrypted in ceph_readdir_prepopulate(), and this patch is trying to avoid do the same thing again here after that.

Because in ceph_readdir() this isn't needed any more. And in other places such as build path it will do it when needed.

Quoted 22 lines
Quoted 22 lines
fs/ceph/dir.c +0 -8
   	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? */
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
   			}
   		}
   	}
Quoted 8 lines

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

Quoted 7 lines
fs/ceph/dir.c +7 -0
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

The long snap name in format of "_${ENCRYPTED-SNAP-NAME}_${INO}", the max length will be 1 +256 + 16.

Quoted 10 lines
Quoted 3 lines

Ok. Best to make a named constant definition for this then. Bare numbers in an allocation like this make it hard to understand what's going on.

Sure.

So will this patch still makes sense ?

From my test with thousands of dentries this could save a lot of time.

Yeah, I think so.

It probably would have made more sense if you had described in the commit message how we end up decrypting the names twice, and how this patch addresses that.

Quoted 130 lines
Quoted 129 lines
Quoted 129 lines
Quoted 10 lines
Quoted 10 lines
fs/ceph/dir.c +0 -7
   	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;
-
Quoted 8 lines

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

Quoted 6 lines
fs/ceph/dir.c +0 -6
-		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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

In ceph_readdir_prepopulate(), and it will set the rde->dentry, and here we can get the name from the dentry directly instead of decrypt it again.

Quoted 108 lines
Quoted 99 lines
fs/ceph/dir.c +23 -8
+		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");
   			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
   			goto out;
   		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
   		ctx->pos++;
   	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

Signature · 1 line
Jeff Layton <[email protected]>
author
#12 lore raw

Re: [PATCH v2 3/7] ceph: do not dencrypt the dentry name twice for readdir

Quoted 85 lines · Jeff Layton
Quoted 84 lines · Xiubo Li
Quoted 73 lines · Jeff Layton
Quoted 72 lines · Xiubo Li
Quoted 28 lines · Jeff Layton
Quoted 27 lines · On Tue, 2022-03-01 at 19:30 +0800, [email protected] wrote:

From: Xiubo Li <[email protected]>

---
    fs/ceph/dir.c        | 66 +++++++++++++++++++++-----------------------
    fs/ceph/inode.c      | 15 ++++++++++
    fs/ceph/mds_client.h |  1 +
    3 files changed, 47 insertions(+), 35 deletions(-)
fs/ceph/dir.c +1 -2
index 6be0c1f793c2..e3917b4426e8 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;
    	}
 
Quoted 6 lines

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

Quoted 4 lines
fs/ceph/dir.c +0 -4
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
-		goto out;
-

Why are you removing this? This is what ensures that the key is loaded if we're going to need it.

Since the dentry names are already decrypted in ceph_readdir_prepopulate(), and this patch is trying to avoid do the same thing again here after that.

Because in ceph_readdir() this isn't needed any more. And in other places such as build path it will do it when needed.

Quoted 30 lines
Quoted 19 lines
fs/ceph/dir.c +0 -8
    	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? */

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

Quoted 10 lines
fs/ceph/dir.c +7 -0
@@ -525,40 +512,49 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
    			}
    		}
    	}
+
+	dentry_name = kmalloc(280, GFP_KERNEL);
+	if (!dentry_name) {
+		err = -ENOMEM;
+		goto out;
+	}
+

Woah, what's up with the bare "280" here?

Quoted 6 lines
Quoted 3 lines

The long snap name in format of "_${ENCRYPTED-SNAP-NAME}_${INO}", the max length will be 1 +256 + 16.

Ok. Best to make a named constant definition for this then. Bare numbers in an allocation like this make it hard to understand what's going on.

Sure.

So will this patch still makes sense ?

Quoted 8 lines
Quoted 3 lines

From my test with thousands of dentries this could save a lot of time.

Yeah, I think so.

It probably would have made more sense if you had described in the commit message how we end up decrypting the names twice, and how this patch addresses that.

Sure, will do that.

Quoted 129 lines
Quoted 129 lines
Quoted 129 lines
Quoted 129 lines
Quoted 10 lines
Quoted 10 lines
fs/ceph/dir.c +0 -7
    	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;
-
Quoted 8 lines

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

Quoted 6 lines
fs/ceph/dir.c +0 -6
-		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;
-		}

I may be missing something, but if you rip this out, where does the decryption happen?

In ceph_readdir_prepopulate(), and it will set the rde->dentry, and here we can get the name from the dentry directly instead of decrypt it again.

Quoted 108 lines
Quoted 99 lines
fs/ceph/dir.c +23 -8
+		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");
    			err = 0;
+			for (; i < rinfo->dir_nr; i++) {
+				rde = rinfo->dir_entries + i;
+				dput(rde->dentry);
+				rde->dentry = NULL;
+			}
    			goto out;
    		}
 
-		/* Reset the lengths to their original allocated vals */
-		oname.len = olen;
    		ctx->pos++;
    	}
 
@@ -616,8 +612,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 2bc2f02b84e8..877e699fe43b 100644
@@ -1903,6 +1903,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);
@@ -1963,6 +1964,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);
@@ -2025,6 +2032,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 +0 -0
index 0dfe24f94567..663d7754d57d 100644

fs/ceph/mds_client.h struct ceph_mds_reply_info_in { · line 102

Quoted 7 lines
fs/ceph/mds_client.h +1 -0
@@ -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;

NAK on this patch as it is...

#13 lore raw

PATCH v2 4/7 ceph: add ceph_get_snap_parent_inode() support

In reply to [email protected]

From: Xiubo Li <[email protected]>

Get the parent inode for the snap directory ".snap", if the inode is not a snap directory just return it with the reference increased.

---
 fs/ceph/snap.c  | 24 ++++++++++++++++++++++++
 fs/ceph/super.h |  1 +
 2 files changed, 25 insertions(+)
fs/ceph/snap.c +24 -0
index 66a1a92cf579..5e9656926dc7 100644
@@ -1247,3 +1247,27 @@ void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
 		kfree(sm);
 	}
 }
+
+/*
+ * Get the parent inode for the snap directory ".snap",
+ * if the inode is not a snap directory just return it
+ * with the reference increased.
+ */
+struct inode *ceph_get_snap_parent_inode(struct inode *inode)
+{
+	struct inode *pinode;
+
+	if (ceph_snap(inode) == CEPH_SNAPDIR) {
+		struct ceph_vino vino = {
+			.ino = ceph_ino(inode),
+			.snap = CEPH_NOSNAP,
+		};
+		pinode = ceph_find_inode(inode->i_sb, vino);
+		BUG_ON(!pinode);
+	} else {
+		ihold(inode);
+		pinode = inode;
+	}
+
+	return pinode;
+}
fs/ceph/super.h +1 -0
index d5a4d311f4c2..e7c69ca5e289 100644
@@ -969,6 +969,7 @@ extern void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
 				struct ceph_snapid_map *sm);
 extern void ceph_trim_snapid_map(struct ceph_mds_client *mdsc);
 extern void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc);
+extern struct inode *ceph_get_snap_parent_inode(struct inode *inode);
 void ceph_umount_begin(struct super_block *sb);
 
 
Signature · 1 line
2.27.0
#14 lore raw

PATCH v2 5/7 ceph: use the parent inode of '.snap' to dencrypt the names for readdir

In reply to [email protected]

From: Xiubo Li <[email protected]>

The inode for '.snap' directory will always with no key setup, so we can use the parent inode to do this.

---
 fs/ceph/inode.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/ceph/inode.c +9 -6
index 877e699fe43b..a6c4965e5363 100644
@@ -1817,7 +1817,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
 	struct qstr dname;
 	struct dentry *dn;
-	struct inode *in;
+	struct inode *in, *pinode;
 	int err = 0, skipped = 0, ret, i;
 	u32 frag = le32_to_cpu(req->r_args.readdir.frag);
 	u32 last_hash = 0;
@@ -1876,11 +1876,13 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 	cache_ctl.index = req->r_readdir_cache_idx;
 	fpos_offset = req->r_readdir_offset;
 
-	err = ceph_fname_alloc_buffer(inode, &tname);
+	pinode = ceph_get_snap_parent_inode(inode);
+
+	err = ceph_fname_alloc_buffer(pinode, &tname);
 	if (err < 0)
 		goto out;
 
-	err = ceph_fname_alloc_buffer(inode, &oname);
+	err = ceph_fname_alloc_buffer(pinode, &oname);
 	if (err < 0)
 		goto out;
 
@@ -1890,7 +1892,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
 		struct ceph_vino tvino;
 		u32 olen = oname.len;
-		struct ceph_fname fname = { .dir	= inode,
+		struct ceph_fname fname = { .dir	= pinode,
 					    .name	= rde->name,
 					    .name_len	= rde->name_len,
 					    .ctext	= rde->altname,
@@ -2045,8 +2047,9 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		req->r_readdir_cache_idx = cache_ctl.index;
 	}
 	ceph_readdir_cache_release(&cache_ctl);
-	ceph_fname_free_buffer(inode, &tname);
-	ceph_fname_free_buffer(inode, &oname);
+	ceph_fname_free_buffer(pinode, &tname);
+	ceph_fname_free_buffer(pinode, &oname);
+	iput(pinode);
 	dout("readdir_prepopulate done\n");
 	return err;
 }
Signature · 1 line
2.27.0
#15 lore raw

PATCH v2 6/7 ceph: use the parent inode of '.snap' to encrypt name to build path

In reply to [email protected]

From: Xiubo Li <[email protected]>

The inode for '.snap' directory will always with no key setup, so we can use the parent inode to do this.

---
 fs/ceph/mds_client.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
fs/ceph/mds_client.c +20 -13
index 3dea96df4769..5433e6050563 100644
@@ -2465,8 +2465,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  */
 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for_wire)
 {
-	struct dentry *cur;
-	struct inode *inode;
+	struct dentry *cur, *parent;
+	struct inode *inode, *pinode;
 	char *path;
 	int pos;
 	unsigned seq;
@@ -2479,13 +2479,16 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 	if (!path)
 		return ERR_PTR(-ENOMEM);
 retry:
+	pinode = NULL;
+	parent = NULL;
 	pos = PATH_MAX - 1;
 	path[pos] = '\0';
 
 	seq = read_seqbegin(&rename_lock);
 	cur = dget(dentry);
 	for (;;) {
-		struct dentry *parent;
+		parent = dget_parent(cur);
+		pinode = ceph_get_snap_parent_inode(d_inode(parent));
 
 		spin_lock(&cur->d_lock);
 		inode = d_inode(cur);
@@ -2493,12 +2496,11 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			dout("build_path path+%d: %p SNAPDIR\n",
 			     pos, cur);
 			spin_unlock(&cur->d_lock);
-			parent = dget_parent(cur);
 		} else if (for_wire && inode && dentry != cur && ceph_snap(inode) == CEPH_NOSNAP) {
 			spin_unlock(&cur->d_lock);
 			pos++; /* get rid of any prepended '/' */
 			break;
-		} else if (!for_wire || !IS_ENCRYPTED(d_inode(cur->d_parent))) {
+		} else if (!for_wire || !IS_ENCRYPTED(pinode)) {
 			pos -= cur->d_name.len;
 			if (pos < 0) {
 				spin_unlock(&cur->d_lock);
@@ -2506,7 +2508,6 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			}
 			memcpy(path + pos, cur->d_name.name, cur->d_name.len);
 			spin_unlock(&cur->d_lock);
-			parent = dget_parent(cur);
 		} else {
 			int len, ret;
 			char buf[FSCRYPT_BASE64URL_CHARS(NAME_MAX)];
@@ -2518,32 +2519,32 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			memcpy(buf, cur->d_name.name, cur->d_name.len);
 			len = cur->d_name.len;
 			spin_unlock(&cur->d_lock);
-			parent = dget_parent(cur);
 
-			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = __fscrypt_prepare_readdir(pinode);
 			if (ret < 0) {
 				dput(parent);
 				dput(cur);
+				iput(pinode);
 				return ERR_PTR(ret);
 			}
 
-			if (fscrypt_has_encryption_key(d_inode(parent))) {
-				len = ceph_encode_encrypted_fname(d_inode(parent), cur, buf);
+			if (fscrypt_has_encryption_key(pinode)) {
+				len = ceph_encode_encrypted_fname(pinode, cur, buf);
 				if (len < 0) {
 					dput(parent);
 					dput(cur);
+					iput(pinode);
 					return ERR_PTR(len);
 				}
 			}
 			pos -= len;
-			if (pos < 0) {
-				dput(parent);
+			if (pos < 0)
 				break;
-			}
 			memcpy(path + pos, buf, len);
 		}
 		dput(cur);
 		cur = parent;
+		parent = NULL;
 
 		/* Are we at the root? */
 		if (IS_ROOT(cur))
@@ -2554,7 +2555,13 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			break;
 
 		path[pos] = '/';
+		iput(pinode);
+		pinode = NULL;
 	}
+	if (pinode)
+		iput(pinode);
+	if (parent)
+		dput(parent);
 	inode = d_inode(cur);
 	base = inode ? ceph_ino(inode) : 0;
 	dput(cur);
Signature · 1 line
2.27.0
#16 lore raw

PATCH v2 7/7 ceph: try to encrypt/decrypt long snap name

In reply to [email protected]

From: Xiubo Li <[email protected]>

The child realm will inherit parents' snapshots, and the snapshot names will be in long name format:

  "_${ENCRYPTED-NAME}_${PARENT-INO}"

We need to parse the ENCRYPTED-NAME and to the decryption for readdir and when lookup a snapshot we also need to encrypt the real snapshot name and then switch to the long snap name to do the lookup in MDS.

---
 fs/ceph/crypto.c     | 75 ++++++++++++++++++++++++++++++++++++----
 fs/ceph/crypto.h     |  2 +-
 fs/ceph/dir.c        | 26 +++++++++++---
 fs/ceph/inode.c      | 81 ++++++++++++++++++++++++++++++++++++++++----
 fs/ceph/mds_client.c | 22 ++++++++----
 fs/ceph/mds_client.h |  2 ++
 fs/ceph/super.h      |  1 +
 7 files changed, 184 insertions(+), 25 deletions(-)
fs/ceph/crypto.c +69 -6
index 5a87e7385d3f..3af0d7a0ed10 100644
@@ -128,14 +128,61 @@ void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req, struct ceph_acl_se
 	swap(req->r_fscrypt_auth, as->fscrypt_auth);
 }
 
-int ceph_encode_encrypted_fname(const struct inode *parent, struct dentry *dentry, char *buf)
+int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry, char *buf)
 {
+	struct ceph_dentry_info *di = ceph_dentry(dentry);
+	struct qstr d_name = {.len = 0, .name = NULL};
+	struct inode *pinode = parent;
 	u32 len;
 	int elen;
 	int ret;
 	u8 *cryptbuf;
+	char *p;
+	unsigned char *last = NULL;
 
-	WARN_ON_ONCE(!fscrypt_has_encryption_key(parent));
+	// The long snap name format is "_${SNAP-NAME}_{INO}"
+	if (di->long_snap_name) {
+		struct ceph_vino vino = { .snap = CEPH_NOSNAP };
+
+		last = strrchr(dentry->d_name.name, '_');
+		if (!last)
+			return -EINVAL;
+
+		ret = kstrtou64(last + 1, 0, &vino.ino);
+		if (ret) {
+			kfree(d_name.name);
+			return ret;
+		}
+		pinode = ceph_get_inode(parent->i_sb, vino, NULL);
+		ret = __fscrypt_prepare_readdir(pinode);
+		if (ret < 0) {
+			iput(pinode);
+			return ret;
+		}
+		if (!fscrypt_has_encryption_key(pinode)) {
+			memcpy(buf, dentry->d_name.name, dentry->d_name.len);
+			buf[dentry->d_name.len] = '\0';
+			iput(pinode);
+			return dentry->d_name.len;
+		}
+
+		d_name.len = last - dentry->d_name.name - 1;
+		d_name.name = kstrndup(dentry->d_name.name + 1,
+				       d_name.len, GFP_KERNEL);
+		if (!d_name.name)
+			return -ENOMEM;
+		p = buf + 1;
+		buf[0] = '_';
+		dout(" long_snap_name real snap name: %s, ino: %s\n",
+		     d_name.name, last + 1);
+	} else {
+		p = buf;
+		d_name.name = dentry->d_name.name;
+		d_name.len = dentry->d_name.len;
+		ihold(parent);
+	}
+
+	WARN_ON_ONCE(!fscrypt_has_encryption_key(pinode));
 
 	/*
 	 * convert cleartext dentry name to ciphertext
@@ -144,20 +191,31 @@ int ceph_encode_encrypted_fname(const struct inode *parent, struct dentry *dentr
 	 *
 	 * See: fscrypt_setup_filename
 	 */
-	if (!fscrypt_fname_encrypted_size(parent, dentry->d_name.len, NAME_MAX, &len))
+	if (!fscrypt_fname_encrypted_size(pinode, d_name.len, NAME_MAX, &len)) {
+		iput(pinode);
 		return -ENAMETOOLONG;
+	}
 
 	/* Allocate a buffer appropriate to hold the result */
 	cryptbuf = kmalloc(len > CEPH_NOHASH_NAME_MAX ? NAME_MAX : len, GFP_KERNEL);
-	if (!cryptbuf)
+	if (!cryptbuf) {
+		iput(pinode);
+		if (di->long_snap_name)
+			kfree(d_name.name);
 		return -ENOMEM;
+	}
 
-	ret = fscrypt_fname_encrypt(parent, &dentry->d_name, cryptbuf, len);
+	ret = fscrypt_fname_encrypt(pinode, &d_name, cryptbuf, len);
 	if (ret) {
+		iput(pinode);
 		kfree(cryptbuf);
+		if (di->long_snap_name)
+			kfree(d_name.name);
 		return ret;
 	}
 
+	iput(pinode);
+
 	/* hash the end if the name is long enough */
 	if (len > CEPH_NOHASH_NAME_MAX) {
 		u8 hash[SHA256_DIGEST_SIZE];
@@ -170,8 +228,13 @@ int ceph_encode_encrypted_fname(const struct inode *parent, struct dentry *dentr
 	}
 
 	/* base64 encode the encrypted name */
-	elen = fscrypt_base64url_encode(cryptbuf, len, buf);
+	elen = fscrypt_base64url_encode(cryptbuf, len, p);
 	kfree(cryptbuf);
+	if (di->long_snap_name) {
+		kfree(d_name.name);
+		strcpy(p + elen, last);
+		elen += 1 + strlen(last);
+	}
 	dout("base64-encoded ciphertext name = %.*s\n", elen, buf);
 	return elen;
 }
fs/ceph/crypto.h +1 -1
index 1e08f8a64ad6..e99b457a776a 100644
@@ -90,7 +90,7 @@ void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
 int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
 				 struct ceph_acl_sec_ctx *as);
 void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req, struct ceph_acl_sec_ctx *as);
-int ceph_encode_encrypted_fname(const struct inode *parent, struct dentry *dentry, char *buf);
+int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry, char *buf);
 
 static inline int ceph_fname_alloc_buffer(struct inode *parent, struct fscrypt_str *fname)
 {
fs/ceph/dir.c +21 -5
index e3917b4426e8..86c04e248f07 100644
@@ -50,6 +50,7 @@ static int ceph_d_init(struct dentry *dentry)
 	di->time = jiffies;
 	dentry->d_fsdata = di;
 	INIT_LIST_HEAD(&di->lease_list);
+	di->long_snap_name = false;
 
 	atomic64_inc(&mdsc->metric.total_dentries);
 
@@ -547,7 +548,7 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 			      le32_to_cpu(rde->inode.in->mode) >> 12)) {
 			dout("filldir stopping us...\n");
 			err = 0;
-			for (; i < rinfo->dir_nr; i++) {
+			for (++i; i < rinfo->dir_nr; i++) {
 				rde = rinfo->dir_entries + i;
 				dput(rde->dentry);
 				rde->dentry = NULL;
@@ -775,7 +776,9 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 {
 	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
+	struct ceph_dentry_info *di = ceph_dentry(dentry);
 	struct ceph_mds_request *req;
+	struct inode *pinode = dir;
 	int op;
 	int mask;
 	int err;
@@ -786,21 +789,22 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 	if (dentry->d_name.len > NAME_MAX)
 		return ERR_PTR(-ENAMETOOLONG);
 
-	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
+	pinode = ceph_get_snap_parent_inode(dir);
+	if (IS_ENCRYPTED(pinode)) {
+		err = __fscrypt_prepare_readdir(pinode);
 		if (err)
 			return ERR_PTR(err);
-		if (!fscrypt_has_encryption_key(dir)) {
+		if (!fscrypt_has_encryption_key(pinode)) {
 			spin_lock(&dentry->d_lock);
 			dentry->d_flags |= DCACHE_NOKEY_NAME;
 			spin_unlock(&dentry->d_lock);
 		}
 	}
+	iput(pinode);
 
 	/* can we conclude ENOENT locally? */
 	if (d_really_is_negative(dentry)) {
 		struct ceph_inode_info *ci = ceph_inode(dir);
-		struct ceph_dentry_info *di = ceph_dentry(dentry);
 
 		spin_lock(&ci->i_ceph_lock);
 		dout(" dir %p flags are 0x%lx\n", dir, ci->i_ceph_flags);
@@ -823,6 +827,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 
 	op = ceph_snap(dir) == CEPH_SNAPDIR ?
 		CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
+retry:
 	req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
 	if (IS_ERR(req))
 		return ERR_CAST(req);
@@ -841,6 +846,16 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 	if (err == -ENOENT) {
 		struct dentry *res;
 
+		// long snap name with the format "_${SNAP-NAME}_${INO}" ?
+		if (IS_ENCRYPTED(pinode) && !di->long_snap_name &&
+		    op == CEPH_MDS_OP_LOOKUPSNAP &&
+		    dentry->d_name.name[0] == '_') {
+			di->long_snap_name = true;
+			ceph_mdsc_put_request(req);
+			dout("lookup retry with long snap name set.\n");
+			goto retry;
+		}
+
 		res = ceph_handle_snapdir(req, dentry);
 		if (IS_ERR(res)) {
 			err = PTR_ERR(res);
@@ -848,6 +863,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 			dentry = res;
 			err = 0;
 		}
+		di->long_snap_name = false;
 	}
 	dentry = ceph_finish_lookup(req, dentry, err);
 	ceph_mdsc_put_request(req);  /* will dput(dentry) */
fs/ceph/inode.c +75 -6

123 lines not on this page · raw message

fs/ceph/mds_client.c +15 -7
index 5433e6050563..f001d57b0bd0 100644
@@ -309,7 +309,8 @@ static int parse_reply_info_dir(void **p, void *end,
 
 static int parse_reply_info_lease(void **p, void *end,
 				  struct ceph_mds_reply_lease **lease,
-				  u64 features, u32 *altname_len, u8 **altname)
+				  u64 features, u32 *altname_len, u8 **altname,
+				  u8 *long_snap_name)
 {
 	u8 struct_v;
 	u32 struct_len;
@@ -338,14 +339,16 @@ static int parse_reply_info_lease(void **p, void *end,
 	*p += sizeof(**lease);
 
 	if (features == (u64)-1) {
+		*altname = NULL;
+		*altname_len = 0;
 		if (struct_v >= 2) {
 			ceph_decode_32_safe(p, end, *altname_len, bad);
 			ceph_decode_need(p, end, *altname_len, bad);
 			*altname = *p;
 			*p += *altname_len;
-		} else {
-			*altname = NULL;
-			*altname_len = 0;
+		}
+		if (struct_v >= 3) {
+			ceph_decode_8_safe(p, end, *long_snap_name, bad);
 		}
 		*p = end;
 	}
@@ -379,7 +382,8 @@ static int parse_reply_info_trace(void **p, void *end,
 		*p += info->dname_len;
 
 		err = parse_reply_info_lease(p, end, &info->dlease, features,
-					     &info->altname_len, &info->altname);
+					     &info->altname_len, &info->altname,
+					     &info->long_snap_name);
 		if (err < 0)
 			goto out_bad;
 	}
@@ -447,7 +451,8 @@ static int parse_reply_info_readdir(void **p, void *end,
 
 		/* dentry lease */
 		err = parse_reply_info_lease(p, end, &rde->lease, features,
-					     &rde->altname_len, &rde->altname);
+					     &rde->altname_len, &rde->altname,
+					     &rde->long_snap_name);
 		if (err)
 			goto out_bad;
 
@@ -2510,7 +2515,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			spin_unlock(&cur->d_lock);
 		} else {
 			int len, ret;
-			char buf[FSCRYPT_BASE64URL_CHARS(NAME_MAX)];
+			char buf[FSCRYPT_BASE64URL_CHARS(NAME_MAX) + 20];
 
 			/*
 			 * Proactively copy name into buf, in case we need to present
@@ -2783,6 +2788,9 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
 	if (test_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags))
 		len += sizeof(__le64);
 
+	/* extra chars '_' and '_${INO}' for long snap names */
+	len += 60;
+
 	msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
 	if (!msg) {
 		msg = ERR_PTR(-ENOMEM);
fs/ceph/mds_client.h +2 -0
index 663d7754d57d..5068f85c7505 100644
@@ -104,6 +104,7 @@ struct ceph_mds_reply_dir_entry {
 	struct ceph_mds_reply_lease   *lease;
 	struct ceph_mds_reply_info_in inode;
 	loff_t			      offset;
+	u8			      long_snap_name;
 };
 
 struct ceph_mds_reply_xattr {
@@ -129,6 +130,7 @@ struct ceph_mds_reply_info_parsed {
 	u32                           altname_len;
 	struct ceph_mds_reply_lease   *dlease;
 	struct ceph_mds_reply_xattr   xattr_info;
+	u8			      long_snap_name;
 
 	/* extra */
 	union {
fs/ceph/super.h +1 -0
index e7c69ca5e289..f9ae5a1fe6e2 100644
@@ -297,6 +297,7 @@ struct ceph_dentry_info {
 	unsigned long lease_renew_after, lease_renew_from;
 	unsigned long time;
 	u64 offset;
+	bool long_snap_name;
 };
 
 #define CEPH_DENTRY_REFERENCED		1
Signature · 1 line
2.27.0