fscrypt: new helper function - fscrypt_prepare_lookup()
authorEric Biggers <ebiggers@google.com>
Mon, 9 Oct 2017 19:15:43 +0000 (12:15 -0700)
committerJaegeuk Kim <jaegeuk@kernel.org>
Fri, 5 Jan 2018 00:17:34 +0000 (16:17 -0800)
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>
fs/crypto/hooks.c
include/linux/fscrypt.h
include/linux/fscrypt_notsupp.h
include/linux/fscrypt_supp.h

index 822cb78f9b45eb4bc0bfe1d5d42f84ffd9316f69..9f5fb2eb9cf7dcd19f47c96295f9a33ac3021192 100644 (file)
@@ -92,3 +92,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
        return 0;
 }
 EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
+
+int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
+{
+       int err = fscrypt_get_encryption_info(dir);
+
+       if (err)
+               return err;
+
+       if (fscrypt_has_encryption_key(dir)) {
+               spin_lock(&dentry->d_lock);
+               dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
+               spin_unlock(&dentry->d_lock);
+       }
+
+       d_set_d_op(dentry, &fscrypt_d_ops);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
index f3c7684d3fdf4812e2b692a04083e3804b4add4c..ffe928551e8bde80749f62db9a39dfda99bd4844 100644 (file)
@@ -238,4 +238,32 @@ static inline int fscrypt_prepare_rename(struct inode *old_dir,
        return 0;
 }
 
+/**
+ * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
+ * @dir: directory being searched
+ * @dentry: filename being looked up
+ * @flags: lookup flags
+ *
+ * Prepare for ->lookup() in a directory which may be encrypted.  Lookups can be
+ * done with or without the directory's encryption key; without the key,
+ * filenames are presented in encrypted form.  Therefore, we'll try to set up
+ * the directory's encryption key, but even without it the lookup can continue.
+ *
+ * To allow invalidating stale dentries if the directory's encryption key is
+ * added later, we also install a custom ->d_revalidate() method and use the
+ * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a
+ * plaintext name (flag set) or a ciphertext name (flag cleared).
+ *
+ * Return: 0 on success, -errno if a problem occurred while setting up the
+ * encryption key
+ */
+static inline int fscrypt_prepare_lookup(struct inode *dir,
+                                        struct dentry *dentry,
+                                        unsigned int flags)
+{
+       if (IS_ENCRYPTED(dir))
+               return __fscrypt_prepare_lookup(dir, dentry);
+       return 0;
+}
+
 #endif /* _LINUX_FSCRYPT_H */
index 480640710493e3e0eb6bc082f6f2b8a4095e6d6b..63e58808519aaeb60aea66287b40963668a8c6bc 100644 (file)
@@ -202,4 +202,10 @@ static inline int __fscrypt_prepare_rename(struct inode *old_dir,
        return -EOPNOTSUPP;
 }
 
+static inline int __fscrypt_prepare_lookup(struct inode *dir,
+                                          struct dentry *dentry)
+{
+       return -EOPNOTSUPP;
+}
+
 #endif /* _LINUX_FSCRYPT_NOTSUPP_H */
index 0254575542c5ab32d6c1540f20582596e5de3ac5..cf9e9fc02f0afe7348404ca769927d587a63ec64 100644 (file)
@@ -152,5 +152,6 @@ extern int __fscrypt_prepare_rename(struct inode *old_dir,
                                    struct inode *new_dir,
                                    struct dentry *new_dentry,
                                    unsigned int flags);
+extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
 
 #endif /* _LINUX_FSCRYPT_SUPP_H */