Skip to content

Commit 32c3cf0

Browse files
ebiggerstytso
authored andcommitted
fscrypt: new helper function - fscrypt_prepare_lookup()
Introduce a helper function which prepares to look up the given dentry in the given directory. If the directory is encrypted, it handles loading the directory's encryption key, setting the dentry's ->d_op to fscrypt_d_ops, and setting DCACHE_ENCRYPTED_WITH_KEY if the directory's encryption key is available. Note: once all filesystems switch over to this, we'll be able to move fscrypt_d_ops and fscrypt_set_encrypted_dentry() to fscrypt_private.h. Acked-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 94b26f3 commit 32c3cf0

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎fs/crypto/hooks.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
9292
return 0;
9393
}
9494
EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
95+
96+
int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
97+
{
98+
int err = fscrypt_get_encryption_info(dir);
99+
100+
if (err)
101+
return err;
102+
103+
if (fscrypt_has_encryption_key(dir)) {
104+
spin_lock(&dentry->d_lock);
105+
dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
106+
spin_unlock(&dentry->d_lock);
107+
}
108+
109+
d_set_d_op(dentry, &fscrypt_d_ops);
110+
return 0;
111+
}
112+
EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);

‎include/linux/fscrypt.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,32 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir,
237237
return 0;
238238
}
239239

240+
/**
241+
* fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
242+
* @dir: directory being searched
243+
* @dentry: filename being looked up
244+
* @flags: lookup flags
245+
*
246+
* Prepare for ->lookup() in a directory which may be encrypted. Lookups can be
247+
* done with or without the directory's encryption key; without the key,
248+
* filenames are presented in encrypted form. Therefore, we'll try to set up
249+
* the directory's encryption key, but even without it the lookup can continue.
250+
*
251+
* To allow invalidating stale dentries if the directory's encryption key is
252+
* added later, we also install a custom ->d_revalidate() method and use the
253+
* DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
254+
* plaintext name (flag set) or a ciphertext name (flag cleared).
255+
*
256+
* Return: 0 on success, -errno if a problem occurred while setting up the
257+
* encryption key
258+
*/
259+
static inline int fscrypt_prepare_lookup(struct inode *dir,
260+
struct dentry *dentry,
261+
unsigned int flags)
262+
{
263+
if (IS_ENCRYPTED(dir))
264+
return __fscrypt_prepare_lookup(dir, dentry);
265+
return 0;
266+
}
267+
240268
#endif /* _LINUX_FSCRYPT_H */

‎include/linux/fscrypt_notsupp.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,10 @@ static inline int __fscrypt_prepare_rename(struct inode *old_dir,
201201
return -EOPNOTSUPP;
202202
}
203203

204+
static inline int __fscrypt_prepare_lookup(struct inode *dir,
205+
struct dentry *dentry)
206+
{
207+
return -EOPNOTSUPP;
208+
}
209+
204210
#endif /* _LINUX_FSCRYPT_NOTSUPP_H */

‎include/linux/fscrypt_supp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,6 @@ extern int __fscrypt_prepare_rename(struct inode *old_dir,
151151
struct inode *new_dir,
152152
struct dentry *new_dentry,
153153
unsigned int flags);
154+
extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
154155

155156
#endif /* _LINUX_FSCRYPT_SUPP_H */

0 commit comments

Comments
 (0)