Skip to content

Commit 6f2656e

Browse files
Luís Henriquesebiggers
authored andcommitted
fscrypt: new helper function - fscrypt_prepare_lookup_partial()
This patch introduces a new helper function which can be used both in lookups and in atomic_open operations by filesystems that want to handle filename encryption and no-key dentries themselves. The reason for this function to be used in atomic open is that this operation can act as a lookup if handed a dentry that is negative. And in this case we may need to set DCACHE_NOKEY_NAME. Signed-off-by: Luís Henriques <[email protected]> Tested-by: Xiubo Li <[email protected]> Reviewed-by: Xiubo Li <[email protected]> [ebiggers: improved the function comment, and moved the function to just below __fscrypt_prepare_lookup()] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 9c7fb7f commit 6f2656e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

fs/crypto/hooks.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
111111
}
112112
EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
113113

114+
/**
115+
* fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
116+
* @dir: the encrypted directory being searched
117+
* @dentry: the dentry being looked up in @dir
118+
*
119+
* This function should be used by the ->lookup and ->atomic_open methods of
120+
* filesystems that handle filename encryption and no-key name encoding
121+
* themselves and thus can't use fscrypt_prepare_lookup(). Like
122+
* fscrypt_prepare_lookup(), this will try to set up the directory's encryption
123+
* key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
124+
* However, this function doesn't set up a struct fscrypt_name for the filename.
125+
*
126+
* Return: 0 on success; -errno on error. Note that the encryption key being
127+
* unavailable is not considered an error. It is also not an error if
128+
* the encryption policy is unsupported by this kernel; that is treated
129+
* like the key being unavailable, so that files can still be deleted.
130+
*/
131+
int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
132+
{
133+
int err = fscrypt_get_encryption_info(dir, true);
134+
135+
if (!err && !fscrypt_has_encryption_key(dir)) {
136+
spin_lock(&dentry->d_lock);
137+
dentry->d_flags |= DCACHE_NOKEY_NAME;
138+
spin_unlock(&dentry->d_lock);
139+
}
140+
return err;
141+
}
142+
EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
143+
114144
int __fscrypt_prepare_readdir(struct inode *dir)
115145
{
116146
return fscrypt_get_encryption_info(dir, true);

include/linux/fscrypt.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
359359
unsigned int flags);
360360
int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
361361
struct fscrypt_name *fname);
362+
int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
362363
int __fscrypt_prepare_readdir(struct inode *dir);
363364
int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
364365
int fscrypt_prepare_setflags(struct inode *inode,
@@ -673,6 +674,12 @@ static inline int __fscrypt_prepare_lookup(struct inode *dir,
673674
return -EOPNOTSUPP;
674675
}
675676

677+
static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
678+
struct dentry *dentry)
679+
{
680+
return -EOPNOTSUPP;
681+
}
682+
676683
static inline int __fscrypt_prepare_readdir(struct inode *dir)
677684
{
678685
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)