Skip to content

Commit 14e034a

Browse files
Luís Henriquesidryomov
authored andcommitted
ceph: mark directory as non-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. [ xiubli: revise commit title per Milind ] Signed-off-by: Luís Henriques <lhenriques@suse.de> Reviewed-by: Xiubo Li <xiubli@redhat.com> Reviewed-by: Milind Changire <mchangir@redhat.com> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent e127e03 commit 14e034a

4 files changed

Lines changed: 46 additions & 9 deletions

File tree

fs/ceph/crypto.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
287287
if (fname->name_len > NAME_MAX || fname->ctext_len > NAME_MAX)
288288
return -EIO;
289289

290-
ret = __fscrypt_prepare_readdir(fname->dir);
291-
if (ret)
290+
ret = ceph_fscrypt_prepare_readdir(fname->dir);
291+
if (ret < 0)
292292
return ret;
293293

294294
/*
@@ -334,3 +334,34 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
334334
fscrypt_fname_free_buffer(&_tname);
335335
return ret;
336336
}
337+
338+
/**
339+
* ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
340+
* @dir: directory inode for readdir prep
341+
*
342+
* Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
343+
* non-complete if this call results in having the directory unlocked.
344+
*
345+
* Returns:
346+
* 1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
347+
* 0 - if directory is still locked
348+
* < 0 - if __fscrypt_prepare_readdir() fails
349+
*/
350+
int ceph_fscrypt_prepare_readdir(struct inode *dir)
351+
{
352+
bool had_key = fscrypt_has_encryption_key(dir);
353+
int err;
354+
355+
if (!IS_ENCRYPTED(dir))
356+
return 0;
357+
358+
err = __fscrypt_prepare_readdir(dir);
359+
if (err)
360+
return err;
361+
if (!had_key && fscrypt_has_encryption_key(dir)) {
362+
/* directory just got unlocked, mark it as not complete */
363+
ceph_dir_clear_complete(dir);
364+
return 1;
365+
}
366+
return 0;
367+
}

fs/ceph/crypto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static inline void ceph_fname_free_buffer(struct inode *parent,
103103

104104
int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
105105
struct fscrypt_str *oname, bool *is_nokey);
106+
int ceph_fscrypt_prepare_readdir(struct inode *dir);
106107

107108
#else /* CONFIG_FS_ENCRYPTION */
108109

@@ -160,6 +161,11 @@ static inline int ceph_fname_to_usr(const struct ceph_fname *fname,
160161
oname->len = fname->name_len;
161162
return 0;
162163
}
164+
165+
static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
166+
{
167+
return 0;
168+
}
163169
#endif /* CONFIG_FS_ENCRYPTION */
164170

165171
#endif

fs/ceph/dir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
343343
ctx->pos = 2;
344344
}
345345

346-
err = fscrypt_prepare_readdir(inode);
347-
if (err)
346+
err = ceph_fscrypt_prepare_readdir(inode);
347+
if (err < 0)
348348
return err;
349349

350350
spin_lock(&ci->i_ceph_lock);
@@ -785,8 +785,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
785785
return ERR_PTR(-ENAMETOOLONG);
786786

787787
if (IS_ENCRYPTED(dir)) {
788-
err = __fscrypt_prepare_readdir(dir);
789-
if (err)
788+
err = ceph_fscrypt_prepare_readdir(dir);
789+
if (err < 0)
790790
return ERR_PTR(err);
791791
if (!fscrypt_has_encryption_key(dir)) {
792792
spin_lock(&dentry->d_lock);

fs/ceph/mds_client.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,8 +2545,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
25452545
if (!IS_ENCRYPTED(dir))
25462546
goto success;
25472547

2548-
ret = __fscrypt_prepare_readdir(dir);
2549-
if (ret)
2548+
ret = ceph_fscrypt_prepare_readdir(dir);
2549+
if (ret < 0)
25502550
return ERR_PTR(ret);
25512551

25522552
/* No key? Just ignore it. */
@@ -2666,7 +2666,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
26662666
spin_unlock(&cur->d_lock);
26672667
parent = dget_parent(cur);
26682668

2669-
ret = __fscrypt_prepare_readdir(d_inode(parent));
2669+
ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
26702670
if (ret < 0) {
26712671
dput(parent);
26722672
dput(cur);

0 commit comments

Comments
 (0)