Skip to contentSkip to conversation

ceph: mark directory as non-complete complete after loading key

PATCH Superseded Luís Henriques 2 participants 6 messages Nov 22, 2022 – Nov 29, 2022 Ceph client

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ceph: mark directory as non-complete complete after loading key

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

 fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
 fs/ceph/crypto.h     |  6 ++++++
 fs/ceph/dir.c        |  8 ++++----
 fs/ceph/mds_client.c |  6 +++---
 4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -2
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
 		goto out_inode;
 	}
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
 		goto out_inode;
 
 	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
 	}
 	return ret;
 }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
 				 struct ceph_sparse_extent *map, u32 ext_cnt);
 int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
 				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
 
 static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
 {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
 {
 	return page;
 }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
 #endif /* CONFIG_FS_ENCRYPTION */
 
 static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -4
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
 		ctx->pos = 2;
 	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
 		return err;
 
 	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 		return ERR_PTR(-ENAMETOOLONG);
 
 	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
 			return ERR_PTR(err);
 		if (!fscrypt_has_encryption_key(dir)) {
 			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +3 -3
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
 	if (!IS_ENCRYPTED(dir))
 		goto success;
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
 		return ERR_PTR(ret);
 
 	/* No key? Just ignore it. */
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
 			spin_unlock(&cur->d_lock);
 			parent = dget_parent(cur);
 
-			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
 			if (ret < 0) {
 				dput(parent);
 				dput(cur);
#1 lore raw
Quoted 145 lines · Luís Henriques

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

  fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
  fs/ceph/crypto.h     |  6 ++++++
  fs/ceph/dir.c        |  8 ++++----
  fs/ceph/mds_client.c |  6 +++---
  4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -2
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  		goto out_inode;
  	}
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		goto out_inode;
 
  	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  	}
  	return ret;
  }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
  				 struct ceph_sparse_extent *map, u32 ext_cnt);
  int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
 
  static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
  	return page;
  }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
  #endif /* CONFIG_FS_ENCRYPTION */
 
  static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -4
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
  		return err;
 
  	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
 
  	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
  			return ERR_PTR(err);
  		if (!fscrypt_has_encryption_key(dir)) {
  			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +2 -2
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  	if (!IS_ENCRYPTED(dir))
  		goto success;
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		return ERR_PTR(ret);
 
  	/* No key? Just ignore it. */

fs/ceph/mds_client.c char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for · line 2674

Quoted 8 lines
fs/ceph/mds_client.c +1 -1
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
  			spin_unlock(&cur->d_lock);
  			parent = dget_parent(cur);
 
-			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
  			if (ret < 0) {
  				dput(parent);
  				dput(cur);

Hi Luis,

This version LGTM.

Just one nit. I think the following:

if (fscrypt_has_encryption_key(d_inode(parent))) {

is no needed any more.

We can just switch to:

if (ret) {

And also other places ?

Thanks!

- Xiubo

Quoted 1 line
#2 lore raw
Quoted 153 lines · Xiubo Li
Quoted 136 lines · Luís Henriques

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

  fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
  fs/ceph/crypto.h     |  6 ++++++
  fs/ceph/dir.c        |  8 ++++----
  fs/ceph/mds_client.c |  6 +++---
  4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -1
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  		goto out_inode;
  	}
  -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		goto out_inode;
    	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  	}
  	return ret;
  }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
  				 struct ceph_sparse_extent *map, u32 ext_cnt);
  int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
    static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
  	return page;
  }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
  #endif /* CONFIG_FS_ENCRYPTION */
    static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -3
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
  -	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
  		return err;
    	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
    	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
  			return ERR_PTR(err);
  		if (!fscrypt_has_encryption_key(dir)) {
  			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +2 -1
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  	if (!IS_ENCRYPTED(dir))
  		goto success;
  -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		return ERR_PTR(ret);
    	/* No key? Just ignore it. */

fs/ceph/mds_client.c char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for · line 2674

Quoted 7 lines
fs/ceph/mds_client.c +1 -0
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
  			spin_unlock(&cur->d_lock);
  			parent = dget_parent(cur);
  -			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
  			if (ret < 0) {
  				dput(parent);
  				dput(cur);

Hi Luis,

This version LGTM.

Just one nit. I think the following:

if (fscrypt_has_encryption_key(d_inode(parent))) {

Quoted 7 lines

is no needed any more.

We can just switch to:

if (ret) {

And also other places ?

No, that won't work, and I found that out the hard way :-)

If the directory is unlocked (i.e. the key is loaded), then the helper function ceph_fscrypt_prepare_readdir() will return '0' because the status hasn't change. Thus, we still need to check that here.

Cheers,

Signature · 1 line
Luís
#3 lore raw
Quoted 158 lines · Luís Henriques
Quoted 156 lines · Xiubo Li
Quoted 136 lines · Luís Henriques

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

   fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
   fs/ceph/crypto.h     |  6 ++++++
   fs/ceph/dir.c        |  8 ++++----
   fs/ceph/mds_client.c |  6 +++---
   4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -1
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
   		goto out_inode;
   	}
   -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
   		goto out_inode;
     	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
   	}
   	return ret;
   }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
   				 struct ceph_sparse_extent *map, u32 ext_cnt);
   int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
   				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
     static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
   {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
   {
   	return page;
   }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
   #endif /* CONFIG_FS_ENCRYPTION */
     static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -3
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
   		ctx->pos = 2;
   	}
   -	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
   		return err;
     	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
   		return ERR_PTR(-ENAMETOOLONG);
     	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
   			return ERR_PTR(err);
   		if (!fscrypt_has_encryption_key(dir)) {
   			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +2 -1
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
   	if (!IS_ENCRYPTED(dir))
   		goto success;
   -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
   		return ERR_PTR(ret);
     	/* No key? Just ignore it. */

fs/ceph/mds_client.c char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for · line 2674

Quoted 7 lines
fs/ceph/mds_client.c +1 -0
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
   			spin_unlock(&cur->d_lock);
   			parent = dget_parent(cur);
   -			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
   			if (ret < 0) {
   				dput(parent);
   				dput(cur);

Hi Luis,

This version LGTM.

Just one nit. I think the following:

if (fscrypt_has_encryption_key(d_inode(parent))) {

is no needed any more.

We can just switch to:

Quoted 8 lines
Quoted 3 lines

if (ret) {

And also other places ?

No, that won't work, and I found that out the hard way :-)

If the directory is unlocked (i.e. the key is loaded), then the helper function ceph_fscrypt_prepare_readdir() will return '0' because the status hasn't change. Thus, we still need to check that here.

Yeah, sounds reasonable!

LGTM and I will run the test later!

Thanks Luis.

- Xiubo

Quoted 1 line

Cheers,

#4 lore raw
In reply to Luís Henriques

Hi Luis,

BTW, which branch are you using to generate this patch ?

Could you rebase to the testing branch ?

Thanks

- Xiubo

Quoted 154 lines · Luís Henriques

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

  fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
  fs/ceph/crypto.h     |  6 ++++++
  fs/ceph/dir.c        |  8 ++++----
  fs/ceph/mds_client.c |  6 +++---
  4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -2
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  		goto out_inode;
  	}
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		goto out_inode;
 
  	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  	}
  	return ret;
  }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
  				 struct ceph_sparse_extent *map, u32 ext_cnt);
  int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
 
  static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
  	return page;
  }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
  #endif /* CONFIG_FS_ENCRYPTION */
 
  static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -4
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
 
-	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
  		return err;
 
  	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
 
  	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
  			return ERR_PTR(err);
  		if (!fscrypt_has_encryption_key(dir)) {
  			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +3 -3
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  	if (!IS_ENCRYPTED(dir))
  		goto success;
 
-	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		return ERR_PTR(ret);
 
  	/* No key? Just ignore it. */
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
  			spin_unlock(&cur->d_lock);
  			parent = dget_parent(cur);
 
-			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
  			if (ret < 0) {
  				dput(parent);
  				dput(cur);
#5 lore raw
Quoted 5 lines · Xiubo Li

Hi Luis,

BTW, which branch are you using to generate this patch ?

Could you rebase to the testing branch ?

Hmmm, I think I've used the wip-fscrypt branch. OK, I'll resend this after running a few tests just to make sure.

Cheers,

Signature · 1 line
Luís
Quoted 152 lines

Thanks

- Xiubo

Quoted 144 lines · Luís Henriques

When setting a directory's crypt context, ceph_dir_clear_complete() needs to be called otherwise if it was complete before, any existing (old) dentry will still be valid.

This patch adds a wrapper around __fscrypt_prepare_readdir() which will ensure a directory is marked as non-complete if key status changes.

---
Hi!

Hopefully I'm not breaking anything else. I've run a bunch of fstests and didn't saw any (new) failures.

Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper

Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes

  fs/ceph/crypto.c     | 35 +++++++++++++++++++++++++++++++++--
  fs/ceph/crypto.h     |  6 ++++++
  fs/ceph/dir.c        |  8 ++++----
  fs/ceph/mds_client.c |  6 +++---
  4 files changed, 46 insertions(+), 9 deletions(-)
fs/ceph/crypto.c +33 -1
index 35a2ccfe6899..5ef65a06af98 100644
@@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  		goto out_inode;
  	}
  -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		goto out_inode;
    	/*
@@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  	}
  	return ret;
  }
+
+/**
+ * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
+ * @dir: directory inode for readdir prep
+ *
+ * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
+ * non-complete if this call results in having the directory unlocked.
+ *
+ * Returns:
+ *     1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
+ *     0 - if directory is still locked
+ *   < 0 - if __fscrypt_prepare_readdir() fails
+ */
+int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	bool had_key = fscrypt_has_encryption_key(dir);
+	int err;
+
+	if (!IS_ENCRYPTED(dir))
+		return 0;
+
+	err = __fscrypt_prepare_readdir(dir);
+	if (err)
+		return err;
+	if (!had_key && fscrypt_has_encryption_key(dir)) {
+		/* directory just got unlocked, mark it as not complete */
+		ceph_dir_clear_complete(dir);
+		return 1;
+	}
+	return 0;
+}
fs/ceph/crypto.h +6 -0
index c6ee993f4ec8..cad53ec916fd 100644
@@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of
  				 struct ceph_sparse_extent *map, u32 ext_cnt);
  int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  				int len, gfp_t gfp);
+int ceph_fscrypt_prepare_readdir(struct inode *dir);
    static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
@@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  {
  	return page;
  }
+
+static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
+{
+	return 0;
+}
  #endif /* CONFIG_FS_ENCRYPTION */
    static inline loff_t ceph_fscrypt_page_offset(struct page *page)
fs/ceph/dir.c +4 -3
index edc2bf0aab83..5829f27d058d 100644
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
  		ctx->pos = 2;
  	}
  -	err = fscrypt_prepare_readdir(inode);
-	if (err)
+	err = ceph_fscrypt_prepare_readdir(inode);
+	if (err < 0)
  		return err;
    	spin_lock(&ci->i_ceph_lock);
@@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
    	if (IS_ENCRYPTED(dir)) {
-		err = __fscrypt_prepare_readdir(dir);
-		if (err)
+		err = ceph_fscrypt_prepare_readdir(dir);
+		if (err < 0)
  			return ERR_PTR(err);
  		if (!fscrypt_has_encryption_key(dir)) {
  			spin_lock(&dentry->d_lock);
fs/ceph/mds_client.c +3 -1
index 9518ac8e407d..4becc9eada4b 100644
@@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  	if (!IS_ENCRYPTED(dir))
  		goto success;
  -	ret = __fscrypt_prepare_readdir(dir);
-	if (ret)
+	ret = ceph_fscrypt_prepare_readdir(dir);
+	if (ret < 0)
  		return ERR_PTR(ret);
    	/* No key? Just ignore it. */
@@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for
  			spin_unlock(&cur->d_lock);
  			parent = dget_parent(cur);
  -			ret = __fscrypt_prepare_readdir(d_inode(parent));
+			ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
  			if (ret < 0) {
  				dput(parent);
  				dput(cur);