xref: /linux-6.15/include/linux/fscrypt.h (revision 59b59a94)
132190f0aSLinus Torvalds /* SPDX-License-Identifier: GPL-2.0 */
2734f0d24SDave Chinner /*
3734f0d24SDave Chinner  * fscrypt.h: declarations for per-file encryption
4734f0d24SDave Chinner  *
5643fa961SChandan Rajendra  * Filesystems that implement per-file encryption must include this header
6643fa961SChandan Rajendra  * file.
7734f0d24SDave Chinner  *
8734f0d24SDave Chinner  * Copyright (C) 2015, Google, Inc.
9734f0d24SDave Chinner  *
10734f0d24SDave Chinner  * Written by Michael Halcrow, 2015.
11734f0d24SDave Chinner  * Modified by Jaegeuk Kim, 2015.
12734f0d24SDave Chinner  */
13734f0d24SDave Chinner #ifndef _LINUX_FSCRYPT_H
14734f0d24SDave Chinner #define _LINUX_FSCRYPT_H
15734f0d24SDave Chinner 
16734f0d24SDave Chinner #include <linux/fs.h>
17643fa961SChandan Rajendra #include <linux/mm.h>
18643fa961SChandan Rajendra #include <linux/slab.h>
197af0ab0dSEric Biggers #include <uapi/linux/fscrypt.h>
20734f0d24SDave Chinner 
2163cec138SEric Biggers /*
2263cec138SEric Biggers  * The lengths of all file contents blocks must be divisible by this value.
2363cec138SEric Biggers  * This is needed to ensure that all contents encryption modes will work, as
2463cec138SEric Biggers  * some of the supported modes don't support arbitrarily byte-aligned messages.
2563cec138SEric Biggers  *
2663cec138SEric Biggers  * Since the needed alignment is 16 bytes, most filesystems will meet this
2763cec138SEric Biggers  * requirement naturally, as typical block sizes are powers of 2.  However, if a
2863cec138SEric Biggers  * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
2963cec138SEric Biggers  * compression), then it will need to pad to this alignment before encryption.
3063cec138SEric Biggers  */
3163cec138SEric Biggers #define FSCRYPT_CONTENTS_ALIGNMENT 16
32734f0d24SDave Chinner 
33ac4acb1fSEric Biggers union fscrypt_policy;
343e7807d5SJosef Bacik struct fscrypt_inode_info;
35218d921bSEric Biggers struct fs_parameter;
36ed318a6cSEric Biggers struct seq_file;
37734f0d24SDave Chinner 
38734f0d24SDave Chinner struct fscrypt_str {
39734f0d24SDave Chinner 	unsigned char *name;
40734f0d24SDave Chinner 	u32 len;
41734f0d24SDave Chinner };
42734f0d24SDave Chinner 
43734f0d24SDave Chinner struct fscrypt_name {
44734f0d24SDave Chinner 	const struct qstr *usr_fname;
45734f0d24SDave Chinner 	struct fscrypt_str disk_name;
46734f0d24SDave Chinner 	u32 hash;
47734f0d24SDave Chinner 	u32 minor_hash;
48734f0d24SDave Chinner 	struct fscrypt_str crypto_buf;
4970fb2612SEric Biggers 	bool is_nokey_name;
50734f0d24SDave Chinner };
51734f0d24SDave Chinner 
52734f0d24SDave Chinner #define FSTR_INIT(n, l)		{ .name = n, .len = l }
53734f0d24SDave Chinner #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
54734f0d24SDave Chinner #define fname_name(p)		((p)->disk_name.name)
55734f0d24SDave Chinner #define fname_len(p)		((p)->disk_name.len)
56734f0d24SDave Chinner 
57734f0d24SDave Chinner /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
585dae460cSEric Biggers #define FSCRYPT_SET_CONTEXT_MAX_SIZE	40
59734f0d24SDave Chinner 
60643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
6138ef66b0SEric Biggers 
6238ef66b0SEric Biggers /* Crypto operations for filesystems */
63643fa961SChandan Rajendra struct fscrypt_operations {
6438ef66b0SEric Biggers 
6540e13e18SEric Biggers 	/*
6640e13e18SEric Biggers 	 * If set, then fs/crypto/ will allocate a global bounce page pool the
6740e13e18SEric Biggers 	 * first time an encryption key is set up for a file.  The bounce page
6840e13e18SEric Biggers 	 * pool is required by the following functions:
6940e13e18SEric Biggers 	 *
7040e13e18SEric Biggers 	 * - fscrypt_encrypt_pagecache_blocks()
7140e13e18SEric Biggers 	 * - fscrypt_zeroout_range() for files not using inline crypto
7240e13e18SEric Biggers 	 *
7340e13e18SEric Biggers 	 * If the filesystem doesn't use those, it doesn't need to set this.
7440e13e18SEric Biggers 	 */
7540e13e18SEric Biggers 	unsigned int needs_bounce_pages : 1;
7638ef66b0SEric Biggers 
7738ef66b0SEric Biggers 	/*
787a0263dcSEric Biggers 	 * If set, then fs/crypto/ will allow the use of encryption settings
797a0263dcSEric Biggers 	 * that assume inode numbers fit in 32 bits (i.e.
807a0263dcSEric Biggers 	 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64}), provided that the other
817a0263dcSEric Biggers 	 * prerequisites for these settings are also met.  This is only useful
827a0263dcSEric Biggers 	 * if the filesystem wants to support inline encryption hardware that is
837a0263dcSEric Biggers 	 * limited to 32-bit or 64-bit data unit numbers and where programming
847a0263dcSEric Biggers 	 * keyslots is very slow.
857a0263dcSEric Biggers 	 */
867a0263dcSEric Biggers 	unsigned int has_32bit_inodes : 1;
877a0263dcSEric Biggers 
887a0263dcSEric Biggers 	/*
895b118884SEric Biggers 	 * If set, then fs/crypto/ will allow users to select a crypto data unit
905b118884SEric Biggers 	 * size that is less than the filesystem block size.  This is done via
915b118884SEric Biggers 	 * the log2_data_unit_size field of the fscrypt policy.  This flag is
925b118884SEric Biggers 	 * not compatible with filesystems that encrypt variable-length blocks
935b118884SEric Biggers 	 * (i.e. blocks that aren't all equal to filesystem's block size), for
945b118884SEric Biggers 	 * example as a result of compression.  It's also not compatible with
955b118884SEric Biggers 	 * the fscrypt_encrypt_block_inplace() and
965b118884SEric Biggers 	 * fscrypt_decrypt_block_inplace() functions.
975b118884SEric Biggers 	 */
985b118884SEric Biggers 	unsigned int supports_subblock_data_units : 1;
995b118884SEric Biggers 
1005b118884SEric Biggers 	/*
1015970fbadSEric Biggers 	 * This field exists only for backwards compatibility reasons and should
1025970fbadSEric Biggers 	 * only be set by the filesystems that are setting it already.  It
1035970fbadSEric Biggers 	 * contains the filesystem-specific key description prefix that is
1045970fbadSEric Biggers 	 * accepted for "logon" keys for v1 fscrypt policies.  This
1055970fbadSEric Biggers 	 * functionality is deprecated in favor of the generic prefix
1065970fbadSEric Biggers 	 * "fscrypt:", which itself is deprecated in favor of the filesystem
1075970fbadSEric Biggers 	 * keyring ioctls such as FS_IOC_ADD_ENCRYPTION_KEY.  Filesystems that
1085970fbadSEric Biggers 	 * are newly adding fscrypt support should not set this field.
10938ef66b0SEric Biggers 	 */
1105970fbadSEric Biggers 	const char *legacy_key_prefix;
11138ef66b0SEric Biggers 
11238ef66b0SEric Biggers 	/*
11338ef66b0SEric Biggers 	 * Get the fscrypt context of the given inode.
11438ef66b0SEric Biggers 	 *
11538ef66b0SEric Biggers 	 * @inode: the inode whose context to get
11638ef66b0SEric Biggers 	 * @ctx: the buffer into which to get the context
11738ef66b0SEric Biggers 	 * @len: length of the @ctx buffer in bytes
11838ef66b0SEric Biggers 	 *
11938ef66b0SEric Biggers 	 * Return: On success, returns the length of the context in bytes; this
12038ef66b0SEric Biggers 	 *	   may be less than @len.  On failure, returns -ENODATA if the
12138ef66b0SEric Biggers 	 *	   inode doesn't have a context, -ERANGE if the context is
12238ef66b0SEric Biggers 	 *	   longer than @len, or another -errno code.
12338ef66b0SEric Biggers 	 */
124fe015a78SEric Biggers 	int (*get_context)(struct inode *inode, void *ctx, size_t len);
12538ef66b0SEric Biggers 
12638ef66b0SEric Biggers 	/*
12738ef66b0SEric Biggers 	 * Set an fscrypt context on the given inode.
12838ef66b0SEric Biggers 	 *
12938ef66b0SEric Biggers 	 * @inode: the inode whose context to set.  The inode won't already have
13038ef66b0SEric Biggers 	 *	   an fscrypt context.
13138ef66b0SEric Biggers 	 * @ctx: the context to set
13238ef66b0SEric Biggers 	 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
13338ef66b0SEric Biggers 	 * @fs_data: If called from fscrypt_set_context(), this will be the
13438ef66b0SEric Biggers 	 *	     value the filesystem passed to fscrypt_set_context().
13538ef66b0SEric Biggers 	 *	     Otherwise (i.e. when called from
13638ef66b0SEric Biggers 	 *	     FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
13738ef66b0SEric Biggers 	 *
13838ef66b0SEric Biggers 	 * i_rwsem will be held for write.
13938ef66b0SEric Biggers 	 *
14038ef66b0SEric Biggers 	 * Return: 0 on success, -errno on failure.
14138ef66b0SEric Biggers 	 */
142fe015a78SEric Biggers 	int (*set_context)(struct inode *inode, const void *ctx, size_t len,
143fe015a78SEric Biggers 			   void *fs_data);
14438ef66b0SEric Biggers 
14538ef66b0SEric Biggers 	/*
14638ef66b0SEric Biggers 	 * Get the dummy fscrypt policy in use on the filesystem (if any).
14738ef66b0SEric Biggers 	 *
14838ef66b0SEric Biggers 	 * Filesystems only need to implement this function if they support the
14938ef66b0SEric Biggers 	 * test_dummy_encryption mount option.
15038ef66b0SEric Biggers 	 *
15138ef66b0SEric Biggers 	 * Return: A pointer to the dummy fscrypt policy, if the filesystem is
15238ef66b0SEric Biggers 	 *	   mounted with test_dummy_encryption; otherwise NULL.
15338ef66b0SEric Biggers 	 */
154ac4acb1fSEric Biggers 	const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
15538ef66b0SEric Biggers 
15638ef66b0SEric Biggers 	/*
15738ef66b0SEric Biggers 	 * Check whether a directory is empty.  i_rwsem will be held for write.
15838ef66b0SEric Biggers 	 */
159fe015a78SEric Biggers 	bool (*empty_dir)(struct inode *inode);
16038ef66b0SEric Biggers 
16138ef66b0SEric Biggers 	/*
16238ef66b0SEric Biggers 	 * Check whether the filesystem's inode numbers and UUID are stable,
16338ef66b0SEric Biggers 	 * meaning that they will never be changed even by offline operations
16438ef66b0SEric Biggers 	 * such as filesystem shrinking and therefore can be used in the
16538ef66b0SEric Biggers 	 * encryption without the possibility of files becoming unreadable.
16638ef66b0SEric Biggers 	 *
16738ef66b0SEric Biggers 	 * Filesystems only need to implement this function if they want to
16838ef66b0SEric Biggers 	 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags.  These
16938ef66b0SEric Biggers 	 * flags are designed to work around the limitations of UFS and eMMC
17038ef66b0SEric Biggers 	 * inline crypto hardware, and they shouldn't be used in scenarios where
17138ef66b0SEric Biggers 	 * such hardware isn't being used.
17238ef66b0SEric Biggers 	 *
17338ef66b0SEric Biggers 	 * Leaving this NULL is equivalent to always returning false.
17438ef66b0SEric Biggers 	 */
175b103fb76SEric Biggers 	bool (*has_stable_inodes)(struct super_block *sb);
17638ef66b0SEric Biggers 
17738ef66b0SEric Biggers 	/*
1780e91fc1eSChristoph Hellwig 	 * Return an array of pointers to the block devices to which the
1790e91fc1eSChristoph Hellwig 	 * filesystem may write encrypted file contents, NULL if the filesystem
1800e91fc1eSChristoph Hellwig 	 * only has a single such block device, or an ERR_PTR() on error.
1810e91fc1eSChristoph Hellwig 	 *
1820e91fc1eSChristoph Hellwig 	 * On successful non-NULL return, *num_devs is set to the number of
1830e91fc1eSChristoph Hellwig 	 * devices in the returned array.  The caller must free the returned
1840e91fc1eSChristoph Hellwig 	 * array using kfree().
18538ef66b0SEric Biggers 	 *
18638ef66b0SEric Biggers 	 * If the filesystem can use multiple block devices (other than block
18738ef66b0SEric Biggers 	 * devices that aren't used for encrypted file contents, such as
18838ef66b0SEric Biggers 	 * external journal devices), and wants to support inline encryption,
18938ef66b0SEric Biggers 	 * then it must implement this function.  Otherwise it's not needed.
19038ef66b0SEric Biggers 	 */
1910e91fc1eSChristoph Hellwig 	struct block_device **(*get_devices)(struct super_block *sb,
1920e91fc1eSChristoph Hellwig 					     unsigned int *num_devs);
193643fa961SChandan Rajendra };
194643fa961SChandan Rajendra 
1955be1fa8aSAl Viro int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
1965be1fa8aSAl Viro 			 struct dentry *dentry, unsigned int flags);
197e9b10713SGabriel Krisman Bertazi 
1983e7807d5SJosef Bacik static inline struct fscrypt_inode_info *
fscrypt_get_inode_info(const struct inode * inode)1993e7807d5SJosef Bacik fscrypt_get_inode_info(const struct inode *inode)
200643fa961SChandan Rajendra {
201ab673b98SEric Biggers 	/*
2025b421f08SEric Biggers 	 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
203ab673b98SEric Biggers 	 * I.e., another task may publish ->i_crypt_info concurrently, executing
204ab673b98SEric Biggers 	 * a RELEASE barrier.  We need to use smp_load_acquire() here to safely
205ab673b98SEric Biggers 	 * ACQUIRE the memory the other task published.
206ab673b98SEric Biggers 	 */
207ab673b98SEric Biggers 	return smp_load_acquire(&inode->i_crypt_info);
208643fa961SChandan Rajendra }
209643fa961SChandan Rajendra 
21056dce717SEric Biggers /**
21156dce717SEric Biggers  * fscrypt_needs_contents_encryption() - check whether an inode needs
21256dce717SEric Biggers  *					 contents encryption
213d2fe9754SEric Biggers  * @inode: the inode to check
21456dce717SEric Biggers  *
21556dce717SEric Biggers  * Return: %true iff the inode is an encrypted regular file and the kernel was
21656dce717SEric Biggers  * built with fscrypt support.
21756dce717SEric Biggers  *
21856dce717SEric Biggers  * If you need to know whether the encrypt bit is set even when the kernel was
21956dce717SEric Biggers  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
22056dce717SEric Biggers  */
fscrypt_needs_contents_encryption(const struct inode * inode)22156dce717SEric Biggers static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
22256dce717SEric Biggers {
22356dce717SEric Biggers 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
22456dce717SEric Biggers }
22556dce717SEric Biggers 
2260bf3d5c1SEric Biggers /*
227e9b10713SGabriel Krisman Bertazi  * When d_splice_alias() moves a directory's no-key alias to its
228e9b10713SGabriel Krisman Bertazi  * plaintext alias as a result of the encryption key being added,
229e9b10713SGabriel Krisman Bertazi  * DCACHE_NOKEY_NAME must be cleared and there might be an opportunity
230e9b10713SGabriel Krisman Bertazi  * to disable d_revalidate.  Note that we don't have to support the
231e9b10713SGabriel Krisman Bertazi  * inverse operation because fscrypt doesn't allow no-key names to be
232e9b10713SGabriel Krisman Bertazi  * the source or target of a rename().
2330bf3d5c1SEric Biggers  */
fscrypt_handle_d_move(struct dentry * dentry)2340bf3d5c1SEric Biggers static inline void fscrypt_handle_d_move(struct dentry *dentry)
2350bf3d5c1SEric Biggers {
236e9b10713SGabriel Krisman Bertazi 	/*
237e9b10713SGabriel Krisman Bertazi 	 * VFS calls fscrypt_handle_d_move even for non-fscrypt
238e9b10713SGabriel Krisman Bertazi 	 * filesystems.
239e9b10713SGabriel Krisman Bertazi 	 */
240e9b10713SGabriel Krisman Bertazi 	if (dentry->d_flags & DCACHE_NOKEY_NAME) {
241501e43fbSEric Biggers 		dentry->d_flags &= ~DCACHE_NOKEY_NAME;
242e9b10713SGabriel Krisman Bertazi 
243e9b10713SGabriel Krisman Bertazi 		/*
244e9b10713SGabriel Krisman Bertazi 		 * Other filesystem features might be handling dentry
245e9b10713SGabriel Krisman Bertazi 		 * revalidation, in which case it cannot be disabled.
246e9b10713SGabriel Krisman Bertazi 		 */
247e9b10713SGabriel Krisman Bertazi 		if (dentry->d_op->d_revalidate == fscrypt_d_revalidate)
248e9b10713SGabriel Krisman Bertazi 			dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
249e9b10713SGabriel Krisman Bertazi 	}
2500bf3d5c1SEric Biggers }
2510bf3d5c1SEric Biggers 
252159e1de2SEric Biggers /**
253159e1de2SEric Biggers  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
254159e1de2SEric Biggers  * @dentry: the dentry to check
255159e1de2SEric Biggers  *
256159e1de2SEric Biggers  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
257159e1de2SEric Biggers  * dentry that was created in an encrypted directory that hasn't had its
258159e1de2SEric Biggers  * encryption key added yet.  Such dentries may be either positive or negative.
259159e1de2SEric Biggers  *
260159e1de2SEric Biggers  * When a filesystem is asked to create a new filename in an encrypted directory
261159e1de2SEric Biggers  * and the new filename's dentry is a no-key dentry, it must fail the operation
262159e1de2SEric Biggers  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
263159e1de2SEric Biggers  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
264159e1de2SEric Biggers  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
265159e1de2SEric Biggers  *
266159e1de2SEric Biggers  * This is necessary because creating a filename requires the directory's
267159e1de2SEric Biggers  * encryption key, but just checking for the key on the directory inode during
268159e1de2SEric Biggers  * the final filesystem operation doesn't guarantee that the key was available
269159e1de2SEric Biggers  * during the preceding dentry lookup.  And the key must have already been
270159e1de2SEric Biggers  * available during the dentry lookup in order for it to have been checked
271159e1de2SEric Biggers  * whether the filename already exists in the directory and for the new file's
272159e1de2SEric Biggers  * dentry not to be invalidated due to it incorrectly having the no-key flag.
273159e1de2SEric Biggers  *
274159e1de2SEric Biggers  * Return: %true if the dentry is a no-key name
275159e1de2SEric Biggers  */
fscrypt_is_nokey_name(const struct dentry * dentry)276159e1de2SEric Biggers static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
277159e1de2SEric Biggers {
278159e1de2SEric Biggers 	return dentry->d_flags & DCACHE_NOKEY_NAME;
279159e1de2SEric Biggers }
280159e1de2SEric Biggers 
fscrypt_prepare_dentry(struct dentry * dentry,bool is_nokey_name)2818b6bb995SGabriel Krisman Bertazi static inline void fscrypt_prepare_dentry(struct dentry *dentry,
2828b6bb995SGabriel Krisman Bertazi 					  bool is_nokey_name)
2838b6bb995SGabriel Krisman Bertazi {
284e86e6638SGabriel Krisman Bertazi 	/*
285e86e6638SGabriel Krisman Bertazi 	 * This code tries to only take ->d_lock when necessary to write
286e86e6638SGabriel Krisman Bertazi 	 * to ->d_flags.  We shouldn't be peeking on d_flags for
287e86e6638SGabriel Krisman Bertazi 	 * DCACHE_OP_REVALIDATE unlocked, but in the unlikely case
288e86e6638SGabriel Krisman Bertazi 	 * there is a race, the worst it can happen is that we fail to
289e86e6638SGabriel Krisman Bertazi 	 * unset DCACHE_OP_REVALIDATE and pay the cost of an extra
290e86e6638SGabriel Krisman Bertazi 	 * d_revalidate.
291e86e6638SGabriel Krisman Bertazi 	 */
2928b6bb995SGabriel Krisman Bertazi 	if (is_nokey_name) {
2938b6bb995SGabriel Krisman Bertazi 		spin_lock(&dentry->d_lock);
2948b6bb995SGabriel Krisman Bertazi 		dentry->d_flags |= DCACHE_NOKEY_NAME;
2958b6bb995SGabriel Krisman Bertazi 		spin_unlock(&dentry->d_lock);
296e86e6638SGabriel Krisman Bertazi 	} else if (dentry->d_flags & DCACHE_OP_REVALIDATE &&
297e86e6638SGabriel Krisman Bertazi 		   dentry->d_op->d_revalidate == fscrypt_d_revalidate) {
298e86e6638SGabriel Krisman Bertazi 		/*
299e86e6638SGabriel Krisman Bertazi 		 * Unencrypted dentries and encrypted dentries where the
300e86e6638SGabriel Krisman Bertazi 		 * key is available are always valid from fscrypt
301e86e6638SGabriel Krisman Bertazi 		 * perspective. Avoid the cost of calling
302e86e6638SGabriel Krisman Bertazi 		 * fscrypt_d_revalidate unnecessarily.
303e86e6638SGabriel Krisman Bertazi 		 */
304e86e6638SGabriel Krisman Bertazi 		spin_lock(&dentry->d_lock);
305e86e6638SGabriel Krisman Bertazi 		dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
306e86e6638SGabriel Krisman Bertazi 		spin_unlock(&dentry->d_lock);
3078b6bb995SGabriel Krisman Bertazi 	}
3088b6bb995SGabriel Krisman Bertazi }
3098b6bb995SGabriel Krisman Bertazi 
310643fa961SChandan Rajendra /* crypto.c */
31160700902SEric Biggers void fscrypt_enqueue_decrypt_work(struct work_struct *);
31253bc1d85SEric Biggers 
313*59b59a94SMatthew Wilcox (Oracle) struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
314*59b59a94SMatthew Wilcox (Oracle) 		size_t len, size_t offs, gfp_t gfp_flags);
31560700902SEric Biggers int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
31660700902SEric Biggers 				  unsigned int len, unsigned int offs,
31760700902SEric Biggers 				  u64 lblk_num, gfp_t gfp_flags);
318aa8bc1acSEric Biggers 
31951e4e315SEric Biggers int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
32051e4e315SEric Biggers 				     size_t offs);
32160700902SEric Biggers int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
32260700902SEric Biggers 				  unsigned int len, unsigned int offs,
32360700902SEric Biggers 				  u64 lblk_num);
324643fa961SChandan Rajendra 
fscrypt_is_bounce_page(struct page * page)325d2d0727bSEric Biggers static inline bool fscrypt_is_bounce_page(struct page *page)
326643fa961SChandan Rajendra {
327d2d0727bSEric Biggers 	return page->mapping == NULL;
328643fa961SChandan Rajendra }
329643fa961SChandan Rajendra 
fscrypt_pagecache_page(struct page * bounce_page)330d2d0727bSEric Biggers static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
331d2d0727bSEric Biggers {
332d2d0727bSEric Biggers 	return (struct page *)page_private(bounce_page);
333d2d0727bSEric Biggers }
334d2d0727bSEric Biggers 
fscrypt_is_bounce_folio(struct folio * folio)335c76e14dcSMatthew Wilcox static inline bool fscrypt_is_bounce_folio(struct folio *folio)
336c76e14dcSMatthew Wilcox {
337c76e14dcSMatthew Wilcox 	return folio->mapping == NULL;
338c76e14dcSMatthew Wilcox }
339c76e14dcSMatthew Wilcox 
fscrypt_pagecache_folio(struct folio * bounce_folio)340c76e14dcSMatthew Wilcox static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio)
341c76e14dcSMatthew Wilcox {
342c76e14dcSMatthew Wilcox 	return bounce_folio->private;
343c76e14dcSMatthew Wilcox }
344c76e14dcSMatthew Wilcox 
34560700902SEric Biggers void fscrypt_free_bounce_page(struct page *bounce_page);
346643fa961SChandan Rajendra 
347643fa961SChandan Rajendra /* policy.c */
34860700902SEric Biggers int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
34960700902SEric Biggers int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
35060700902SEric Biggers int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
35160700902SEric Biggers int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
35260700902SEric Biggers int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
353637fa738SJeff Layton int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
354a992b20cSEric Biggers int fscrypt_set_context(struct inode *inode, void *fs_data);
355fe015a78SEric Biggers 
356ac4acb1fSEric Biggers struct fscrypt_dummy_policy {
357ac4acb1fSEric Biggers 	const union fscrypt_policy *policy;
358ed318a6cSEric Biggers };
359ed318a6cSEric Biggers 
360218d921bSEric Biggers int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
361218d921bSEric Biggers 				    struct fscrypt_dummy_policy *dummy_policy);
362218d921bSEric Biggers bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
363218d921bSEric Biggers 				  const struct fscrypt_dummy_policy *p2);
364ed318a6cSEric Biggers void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
365ed318a6cSEric Biggers 					struct super_block *sb);
366218d921bSEric Biggers static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)367218d921bSEric Biggers fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
368218d921bSEric Biggers {
369218d921bSEric Biggers 	return dummy_policy->policy != NULL;
370218d921bSEric Biggers }
371ed318a6cSEric Biggers static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)372ac4acb1fSEric Biggers fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
373ed318a6cSEric Biggers {
374ac4acb1fSEric Biggers 	kfree(dummy_policy->policy);
375ac4acb1fSEric Biggers 	dummy_policy->policy = NULL;
376ed318a6cSEric Biggers }
377ed318a6cSEric Biggers 
37822d94f49SEric Biggers /* keyring.c */
379ccd30a47SEric Biggers void fscrypt_destroy_keyring(struct super_block *sb);
38060700902SEric Biggers int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
38160700902SEric Biggers int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
38260700902SEric Biggers int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
38360700902SEric Biggers int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
38422d94f49SEric Biggers 
385feed8258SEric Biggers /* keysetup.c */
386a992b20cSEric Biggers int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
387a992b20cSEric Biggers 			      bool *encrypt_ret);
38860700902SEric Biggers void fscrypt_put_encryption_info(struct inode *inode);
38960700902SEric Biggers void fscrypt_free_inode(struct inode *inode);
39060700902SEric Biggers int fscrypt_drop_inode(struct inode *inode);
391643fa961SChandan Rajendra 
392643fa961SChandan Rajendra /* fname.c */
393d3e94fdcSJeff Layton int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
394d3e94fdcSJeff Layton 			  u8 *out, unsigned int olen);
395d3e94fdcSJeff Layton bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
396d3e94fdcSJeff Layton 				  u32 max_len, u32 *encrypted_len_ret);
39760700902SEric Biggers int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
398fe015a78SEric Biggers 			   int lookup, struct fscrypt_name *fname);
399643fa961SChandan Rajendra 
fscrypt_free_filename(struct fscrypt_name * fname)400643fa961SChandan Rajendra static inline void fscrypt_free_filename(struct fscrypt_name *fname)
401643fa961SChandan Rajendra {
402643fa961SChandan Rajendra 	kfree(fname->crypto_buf.name);
403643fa961SChandan Rajendra }
404643fa961SChandan Rajendra 
4058b10fe68SJeff Layton int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
406fe015a78SEric Biggers 			       struct fscrypt_str *crypto_str);
40760700902SEric Biggers void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
40860700902SEric Biggers int fscrypt_fname_disk_to_usr(const struct inode *inode,
4098a4ab0b8SEric Biggers 			      u32 hash, u32 minor_hash,
4108a4ab0b8SEric Biggers 			      const struct fscrypt_str *iname,
4118a4ab0b8SEric Biggers 			      struct fscrypt_str *oname);
41260700902SEric Biggers bool fscrypt_match_name(const struct fscrypt_name *fname,
413edc440e3SDaniel Rosenberg 			const u8 *de_name, u32 de_name_len);
41460700902SEric Biggers u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
415aa408f83SDaniel Rosenberg 
416643fa961SChandan Rajendra /* bio.c */
41714db0b3cSEric Biggers bool fscrypt_decrypt_bio(struct bio *bio);
41860700902SEric Biggers int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
419fe015a78SEric Biggers 			  sector_t pblk, unsigned int len);
420643fa961SChandan Rajendra 
421643fa961SChandan Rajendra /* hooks.c */
42260700902SEric Biggers int fscrypt_file_open(struct inode *inode, struct file *filp);
42360700902SEric Biggers int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
424968dd6d0SEric Biggers 			   struct dentry *dentry);
42560700902SEric Biggers int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
42660700902SEric Biggers 			     struct inode *new_dir, struct dentry *new_dentry,
427643fa961SChandan Rajendra 			     unsigned int flags);
42860700902SEric Biggers int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
429b01531dbSEric Biggers 			     struct fscrypt_name *fname);
4306f2656eaSLuís Henriques int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
431ec0caa97SEric Biggers int __fscrypt_prepare_readdir(struct inode *dir);
4327622350eSEric Biggers int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
43360700902SEric Biggers int fscrypt_prepare_setflags(struct inode *inode,
4346e1918cfSDaniel Rosenberg 			     unsigned int oldflags, unsigned int flags);
43531114726SEric Biggers int fscrypt_prepare_symlink(struct inode *dir, const char *target,
43631114726SEric Biggers 			    unsigned int len, unsigned int max_len,
437643fa961SChandan Rajendra 			    struct fscrypt_str *disk_link);
43860700902SEric Biggers int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
43960700902SEric Biggers 			      unsigned int len, struct fscrypt_str *disk_link);
44060700902SEric Biggers const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
441643fa961SChandan Rajendra 				unsigned int max_size,
442643fa961SChandan Rajendra 				struct delayed_call *done);
443d1876056SEric Biggers int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)444eea2c05dSSascha Hauer static inline void fscrypt_set_ops(struct super_block *sb,
445eea2c05dSSascha Hauer 				   const struct fscrypt_operations *s_cop)
446eea2c05dSSascha Hauer {
447eea2c05dSSascha Hauer 	sb->s_cop = s_cop;
448eea2c05dSSascha Hauer }
449643fa961SChandan Rajendra #else  /* !CONFIG_FS_ENCRYPTION */
450643fa961SChandan Rajendra 
4513e7807d5SJosef Bacik static inline struct fscrypt_inode_info *
fscrypt_get_inode_info(const struct inode * inode)4523e7807d5SJosef Bacik fscrypt_get_inode_info(const struct inode *inode)
453643fa961SChandan Rajendra {
454ab673b98SEric Biggers 	return NULL;
455643fa961SChandan Rajendra }
456643fa961SChandan Rajendra 
fscrypt_needs_contents_encryption(const struct inode * inode)45756dce717SEric Biggers static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
45856dce717SEric Biggers {
45956dce717SEric Biggers 	return false;
46056dce717SEric Biggers }
46156dce717SEric Biggers 
fscrypt_handle_d_move(struct dentry * dentry)4620bf3d5c1SEric Biggers static inline void fscrypt_handle_d_move(struct dentry *dentry)
4630bf3d5c1SEric Biggers {
4640bf3d5c1SEric Biggers }
4650bf3d5c1SEric Biggers 
fscrypt_is_nokey_name(const struct dentry * dentry)466159e1de2SEric Biggers static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
467159e1de2SEric Biggers {
468159e1de2SEric Biggers 	return false;
469159e1de2SEric Biggers }
470159e1de2SEric Biggers 
fscrypt_prepare_dentry(struct dentry * dentry,bool is_nokey_name)4718b6bb995SGabriel Krisman Bertazi static inline void fscrypt_prepare_dentry(struct dentry *dentry,
4728b6bb995SGabriel Krisman Bertazi 					  bool is_nokey_name)
4738b6bb995SGabriel Krisman Bertazi {
4748b6bb995SGabriel Krisman Bertazi }
4758b6bb995SGabriel Krisman Bertazi 
476643fa961SChandan Rajendra /* crypto.c */
fscrypt_enqueue_decrypt_work(struct work_struct * work)477643fa961SChandan Rajendra static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
478643fa961SChandan Rajendra {
479643fa961SChandan Rajendra }
480643fa961SChandan Rajendra 
fscrypt_encrypt_pagecache_blocks(struct folio * folio,size_t len,size_t offs,gfp_t gfp_flags)481*59b59a94SMatthew Wilcox (Oracle) static inline struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
482*59b59a94SMatthew Wilcox (Oracle) 		size_t len, size_t offs, gfp_t gfp_flags)
483643fa961SChandan Rajendra {
484643fa961SChandan Rajendra 	return ERR_PTR(-EOPNOTSUPP);
485643fa961SChandan Rajendra }
486643fa961SChandan Rajendra 
fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num,gfp_t gfp_flags)48703569f2fSEric Biggers static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
48803569f2fSEric Biggers 						struct page *page,
48903569f2fSEric Biggers 						unsigned int len,
49003569f2fSEric Biggers 						unsigned int offs, u64 lblk_num,
49103569f2fSEric Biggers 						gfp_t gfp_flags)
49203569f2fSEric Biggers {
49303569f2fSEric Biggers 	return -EOPNOTSUPP;
49403569f2fSEric Biggers }
49503569f2fSEric Biggers 
fscrypt_decrypt_pagecache_blocks(struct folio * folio,size_t len,size_t offs)49651e4e315SEric Biggers static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio,
49751e4e315SEric Biggers 						   size_t len, size_t offs)
498643fa961SChandan Rajendra {
499643fa961SChandan Rajendra 	return -EOPNOTSUPP;
500643fa961SChandan Rajendra }
501643fa961SChandan Rajendra 
fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)50241adbcb7SEric Biggers static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
50341adbcb7SEric Biggers 						struct page *page,
50441adbcb7SEric Biggers 						unsigned int len,
50541adbcb7SEric Biggers 						unsigned int offs, u64 lblk_num)
50641adbcb7SEric Biggers {
50741adbcb7SEric Biggers 	return -EOPNOTSUPP;
50841adbcb7SEric Biggers }
50941adbcb7SEric Biggers 
fscrypt_is_bounce_page(struct page * page)510d2d0727bSEric Biggers static inline bool fscrypt_is_bounce_page(struct page *page)
511d2d0727bSEric Biggers {
512d2d0727bSEric Biggers 	return false;
513d2d0727bSEric Biggers }
514d2d0727bSEric Biggers 
fscrypt_pagecache_page(struct page * bounce_page)515d2d0727bSEric Biggers static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
516643fa961SChandan Rajendra {
517643fa961SChandan Rajendra 	WARN_ON_ONCE(1);
518643fa961SChandan Rajendra 	return ERR_PTR(-EINVAL);
519643fa961SChandan Rajendra }
520643fa961SChandan Rajendra 
fscrypt_is_bounce_folio(struct folio * folio)521c76e14dcSMatthew Wilcox static inline bool fscrypt_is_bounce_folio(struct folio *folio)
522c76e14dcSMatthew Wilcox {
523c76e14dcSMatthew Wilcox 	return false;
524c76e14dcSMatthew Wilcox }
525c76e14dcSMatthew Wilcox 
fscrypt_pagecache_folio(struct folio * bounce_folio)526c76e14dcSMatthew Wilcox static inline struct folio *fscrypt_pagecache_folio(struct folio *bounce_folio)
527c76e14dcSMatthew Wilcox {
528c76e14dcSMatthew Wilcox 	WARN_ON_ONCE(1);
529c76e14dcSMatthew Wilcox 	return ERR_PTR(-EINVAL);
530c76e14dcSMatthew Wilcox }
531c76e14dcSMatthew Wilcox 
fscrypt_free_bounce_page(struct page * bounce_page)532d2d0727bSEric Biggers static inline void fscrypt_free_bounce_page(struct page *bounce_page)
533643fa961SChandan Rajendra {
534643fa961SChandan Rajendra }
535643fa961SChandan Rajendra 
536643fa961SChandan Rajendra /* policy.c */
fscrypt_ioctl_set_policy(struct file * filp,const void __user * arg)537643fa961SChandan Rajendra static inline int fscrypt_ioctl_set_policy(struct file *filp,
538643fa961SChandan Rajendra 					   const void __user *arg)
539643fa961SChandan Rajendra {
540643fa961SChandan Rajendra 	return -EOPNOTSUPP;
541643fa961SChandan Rajendra }
542643fa961SChandan Rajendra 
fscrypt_ioctl_get_policy(struct file * filp,void __user * arg)543643fa961SChandan Rajendra static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
544643fa961SChandan Rajendra {
545643fa961SChandan Rajendra 	return -EOPNOTSUPP;
546643fa961SChandan Rajendra }
547643fa961SChandan Rajendra 
fscrypt_ioctl_get_policy_ex(struct file * filp,void __user * arg)5485dae460cSEric Biggers static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
5495dae460cSEric Biggers 					      void __user *arg)
5505dae460cSEric Biggers {
5515dae460cSEric Biggers 	return -EOPNOTSUPP;
5525dae460cSEric Biggers }
5535dae460cSEric Biggers 
fscrypt_ioctl_get_nonce(struct file * filp,void __user * arg)554e98ad464SEric Biggers static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
555e98ad464SEric Biggers {
556e98ad464SEric Biggers 	return -EOPNOTSUPP;
557e98ad464SEric Biggers }
558e98ad464SEric Biggers 
fscrypt_has_permitted_context(struct inode * parent,struct inode * child)559643fa961SChandan Rajendra static inline int fscrypt_has_permitted_context(struct inode *parent,
560643fa961SChandan Rajendra 						struct inode *child)
561643fa961SChandan Rajendra {
562643fa961SChandan Rajendra 	return 0;
563643fa961SChandan Rajendra }
564643fa961SChandan Rajendra 
fscrypt_set_context(struct inode * inode,void * fs_data)565a992b20cSEric Biggers static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
566a992b20cSEric Biggers {
567a992b20cSEric Biggers 	return -EOPNOTSUPP;
568a992b20cSEric Biggers }
569a992b20cSEric Biggers 
570ac4acb1fSEric Biggers struct fscrypt_dummy_policy {
571ed318a6cSEric Biggers };
572ed318a6cSEric Biggers 
573218d921bSEric Biggers static inline int
fscrypt_parse_test_dummy_encryption(const struct fs_parameter * param,struct fscrypt_dummy_policy * dummy_policy)574218d921bSEric Biggers fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
575218d921bSEric Biggers 				    struct fscrypt_dummy_policy *dummy_policy)
576218d921bSEric Biggers {
577218d921bSEric Biggers 	return -EINVAL;
578218d921bSEric Biggers }
579218d921bSEric Biggers 
580218d921bSEric Biggers static inline bool
fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy * p1,const struct fscrypt_dummy_policy * p2)581218d921bSEric Biggers fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
582218d921bSEric Biggers 			     const struct fscrypt_dummy_policy *p2)
583218d921bSEric Biggers {
584218d921bSEric Biggers 	return true;
585218d921bSEric Biggers }
586218d921bSEric Biggers 
fscrypt_show_test_dummy_encryption(struct seq_file * seq,char sep,struct super_block * sb)587ed318a6cSEric Biggers static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
588ed318a6cSEric Biggers 						      char sep,
589ed318a6cSEric Biggers 						      struct super_block *sb)
590ed318a6cSEric Biggers {
591ed318a6cSEric Biggers }
592ed318a6cSEric Biggers 
593218d921bSEric Biggers static inline bool
fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy * dummy_policy)594218d921bSEric Biggers fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
595218d921bSEric Biggers {
596218d921bSEric Biggers 	return false;
597218d921bSEric Biggers }
598218d921bSEric Biggers 
599ed318a6cSEric Biggers static inline void
fscrypt_free_dummy_policy(struct fscrypt_dummy_policy * dummy_policy)600ac4acb1fSEric Biggers fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
601ed318a6cSEric Biggers {
602ed318a6cSEric Biggers }
603ed318a6cSEric Biggers 
60422d94f49SEric Biggers /* keyring.c */
fscrypt_destroy_keyring(struct super_block * sb)605ccd30a47SEric Biggers static inline void fscrypt_destroy_keyring(struct super_block *sb)
60622d94f49SEric Biggers {
60722d94f49SEric Biggers }
60822d94f49SEric Biggers 
fscrypt_ioctl_add_key(struct file * filp,void __user * arg)60922d94f49SEric Biggers static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
61022d94f49SEric Biggers {
61122d94f49SEric Biggers 	return -EOPNOTSUPP;
61222d94f49SEric Biggers }
61322d94f49SEric Biggers 
fscrypt_ioctl_remove_key(struct file * filp,void __user * arg)614b1c0ec35SEric Biggers static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
615b1c0ec35SEric Biggers {
616b1c0ec35SEric Biggers 	return -EOPNOTSUPP;
617b1c0ec35SEric Biggers }
618b1c0ec35SEric Biggers 
fscrypt_ioctl_remove_key_all_users(struct file * filp,void __user * arg)61978a1b96bSEric Biggers static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
62078a1b96bSEric Biggers 						     void __user *arg)
62178a1b96bSEric Biggers {
62278a1b96bSEric Biggers 	return -EOPNOTSUPP;
62378a1b96bSEric Biggers }
62478a1b96bSEric Biggers 
fscrypt_ioctl_get_key_status(struct file * filp,void __user * arg)6255a7e2992SEric Biggers static inline int fscrypt_ioctl_get_key_status(struct file *filp,
6265a7e2992SEric Biggers 					       void __user *arg)
6275a7e2992SEric Biggers {
6285a7e2992SEric Biggers 	return -EOPNOTSUPP;
6295a7e2992SEric Biggers }
6305a7e2992SEric Biggers 
631feed8258SEric Biggers /* keysetup.c */
632643fa961SChandan Rajendra 
fscrypt_prepare_new_inode(struct inode * dir,struct inode * inode,bool * encrypt_ret)633a992b20cSEric Biggers static inline int fscrypt_prepare_new_inode(struct inode *dir,
634a992b20cSEric Biggers 					    struct inode *inode,
635a992b20cSEric Biggers 					    bool *encrypt_ret)
636a992b20cSEric Biggers {
637a992b20cSEric Biggers 	if (IS_ENCRYPTED(dir))
638a992b20cSEric Biggers 		return -EOPNOTSUPP;
639a992b20cSEric Biggers 	return 0;
640a992b20cSEric Biggers }
641a992b20cSEric Biggers 
fscrypt_put_encryption_info(struct inode * inode)642643fa961SChandan Rajendra static inline void fscrypt_put_encryption_info(struct inode *inode)
643643fa961SChandan Rajendra {
644643fa961SChandan Rajendra 	return;
645643fa961SChandan Rajendra }
646643fa961SChandan Rajendra 
fscrypt_free_inode(struct inode * inode)6472c58d548SEric Biggers static inline void fscrypt_free_inode(struct inode *inode)
6482c58d548SEric Biggers {
6492c58d548SEric Biggers }
6502c58d548SEric Biggers 
fscrypt_drop_inode(struct inode * inode)651b1c0ec35SEric Biggers static inline int fscrypt_drop_inode(struct inode *inode)
652b1c0ec35SEric Biggers {
653b1c0ec35SEric Biggers 	return 0;
654b1c0ec35SEric Biggers }
655b1c0ec35SEric Biggers 
656643fa961SChandan Rajendra  /* fname.c */
fscrypt_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct fscrypt_name * fname)657643fa961SChandan Rajendra static inline int fscrypt_setup_filename(struct inode *dir,
658643fa961SChandan Rajendra 					 const struct qstr *iname,
659643fa961SChandan Rajendra 					 int lookup, struct fscrypt_name *fname)
660643fa961SChandan Rajendra {
661643fa961SChandan Rajendra 	if (IS_ENCRYPTED(dir))
662643fa961SChandan Rajendra 		return -EOPNOTSUPP;
663643fa961SChandan Rajendra 
664b01531dbSEric Biggers 	memset(fname, 0, sizeof(*fname));
665643fa961SChandan Rajendra 	fname->usr_fname = iname;
666643fa961SChandan Rajendra 	fname->disk_name.name = (unsigned char *)iname->name;
667643fa961SChandan Rajendra 	fname->disk_name.len = iname->len;
668643fa961SChandan Rajendra 	return 0;
669643fa961SChandan Rajendra }
670643fa961SChandan Rajendra 
fscrypt_free_filename(struct fscrypt_name * fname)671643fa961SChandan Rajendra static inline void fscrypt_free_filename(struct fscrypt_name *fname)
672643fa961SChandan Rajendra {
673643fa961SChandan Rajendra 	return;
674643fa961SChandan Rajendra }
675643fa961SChandan Rajendra 
fscrypt_fname_alloc_buffer(u32 max_encrypted_len,struct fscrypt_str * crypto_str)6768b10fe68SJeff Layton static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
677643fa961SChandan Rajendra 					     struct fscrypt_str *crypto_str)
678643fa961SChandan Rajendra {
679643fa961SChandan Rajendra 	return -EOPNOTSUPP;
680643fa961SChandan Rajendra }
681643fa961SChandan Rajendra 
fscrypt_fname_free_buffer(struct fscrypt_str * crypto_str)682643fa961SChandan Rajendra static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
683643fa961SChandan Rajendra {
684643fa961SChandan Rajendra 	return;
685643fa961SChandan Rajendra }
686643fa961SChandan Rajendra 
fscrypt_fname_disk_to_usr(const struct inode * inode,u32 hash,u32 minor_hash,const struct fscrypt_str * iname,struct fscrypt_str * oname)6878a4ab0b8SEric Biggers static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
688643fa961SChandan Rajendra 					    u32 hash, u32 minor_hash,
689643fa961SChandan Rajendra 					    const struct fscrypt_str *iname,
690643fa961SChandan Rajendra 					    struct fscrypt_str *oname)
691643fa961SChandan Rajendra {
692643fa961SChandan Rajendra 	return -EOPNOTSUPP;
693643fa961SChandan Rajendra }
694643fa961SChandan Rajendra 
fscrypt_match_name(const struct fscrypt_name * fname,const u8 * de_name,u32 de_name_len)695643fa961SChandan Rajendra static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
696643fa961SChandan Rajendra 				      const u8 *de_name, u32 de_name_len)
697643fa961SChandan Rajendra {
698643fa961SChandan Rajendra 	/* Encryption support disabled; use standard comparison */
699643fa961SChandan Rajendra 	if (de_name_len != fname->disk_name.len)
700643fa961SChandan Rajendra 		return false;
701643fa961SChandan Rajendra 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
702643fa961SChandan Rajendra }
703643fa961SChandan Rajendra 
fscrypt_fname_siphash(const struct inode * dir,const struct qstr * name)704aa408f83SDaniel Rosenberg static inline u64 fscrypt_fname_siphash(const struct inode *dir,
705aa408f83SDaniel Rosenberg 					const struct qstr *name)
706aa408f83SDaniel Rosenberg {
707aa408f83SDaniel Rosenberg 	WARN_ON_ONCE(1);
708aa408f83SDaniel Rosenberg 	return 0;
709aa408f83SDaniel Rosenberg }
710aa408f83SDaniel Rosenberg 
fscrypt_d_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)7115be1fa8aSAl Viro static inline int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
7125be1fa8aSAl Viro 				       struct dentry *dentry, unsigned int flags)
7135b2a828bSEric Biggers {
7145b2a828bSEric Biggers 	return 1;
7155b2a828bSEric Biggers }
7165b2a828bSEric Biggers 
717643fa961SChandan Rajendra /* bio.c */
fscrypt_decrypt_bio(struct bio * bio)71814db0b3cSEric Biggers static inline bool fscrypt_decrypt_bio(struct bio *bio)
719643fa961SChandan Rajendra {
72014db0b3cSEric Biggers 	return true;
721643fa961SChandan Rajendra }
722643fa961SChandan Rajendra 
fscrypt_zeroout_range(const struct inode * inode,pgoff_t lblk,sector_t pblk,unsigned int len)723643fa961SChandan Rajendra static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
724643fa961SChandan Rajendra 					sector_t pblk, unsigned int len)
725643fa961SChandan Rajendra {
726643fa961SChandan Rajendra 	return -EOPNOTSUPP;
727643fa961SChandan Rajendra }
728643fa961SChandan Rajendra 
729643fa961SChandan Rajendra /* hooks.c */
730643fa961SChandan Rajendra 
fscrypt_file_open(struct inode * inode,struct file * filp)731643fa961SChandan Rajendra static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
732643fa961SChandan Rajendra {
733643fa961SChandan Rajendra 	if (IS_ENCRYPTED(inode))
734643fa961SChandan Rajendra 		return -EOPNOTSUPP;
735643fa961SChandan Rajendra 	return 0;
736643fa961SChandan Rajendra }
737643fa961SChandan Rajendra 
__fscrypt_prepare_link(struct inode * inode,struct inode * dir,struct dentry * dentry)738968dd6d0SEric Biggers static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
739968dd6d0SEric Biggers 					 struct dentry *dentry)
740643fa961SChandan Rajendra {
741643fa961SChandan Rajendra 	return -EOPNOTSUPP;
742643fa961SChandan Rajendra }
743643fa961SChandan Rajendra 
__fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)744643fa961SChandan Rajendra static inline int __fscrypt_prepare_rename(struct inode *old_dir,
745643fa961SChandan Rajendra 					   struct dentry *old_dentry,
746643fa961SChandan Rajendra 					   struct inode *new_dir,
747643fa961SChandan Rajendra 					   struct dentry *new_dentry,
748643fa961SChandan Rajendra 					   unsigned int flags)
749643fa961SChandan Rajendra {
750643fa961SChandan Rajendra 	return -EOPNOTSUPP;
751643fa961SChandan Rajendra }
752643fa961SChandan Rajendra 
__fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)753643fa961SChandan Rajendra static inline int __fscrypt_prepare_lookup(struct inode *dir,
754b01531dbSEric Biggers 					   struct dentry *dentry,
755b01531dbSEric Biggers 					   struct fscrypt_name *fname)
756643fa961SChandan Rajendra {
757643fa961SChandan Rajendra 	return -EOPNOTSUPP;
758643fa961SChandan Rajendra }
759643fa961SChandan Rajendra 
fscrypt_prepare_lookup_partial(struct inode * dir,struct dentry * dentry)7606f2656eaSLuís Henriques static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
7616f2656eaSLuís Henriques 						 struct dentry *dentry)
7626f2656eaSLuís Henriques {
7636f2656eaSLuís Henriques 	return -EOPNOTSUPP;
7646f2656eaSLuís Henriques }
7656f2656eaSLuís Henriques 
__fscrypt_prepare_readdir(struct inode * dir)766ec0caa97SEric Biggers static inline int __fscrypt_prepare_readdir(struct inode *dir)
767ec0caa97SEric Biggers {
768ec0caa97SEric Biggers 	return -EOPNOTSUPP;
769ec0caa97SEric Biggers }
770ec0caa97SEric Biggers 
__fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)7717622350eSEric Biggers static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
7727622350eSEric Biggers 					    struct iattr *attr)
7737622350eSEric Biggers {
7747622350eSEric Biggers 	return -EOPNOTSUPP;
7757622350eSEric Biggers }
7767622350eSEric Biggers 
fscrypt_prepare_setflags(struct inode * inode,unsigned int oldflags,unsigned int flags)7776e1918cfSDaniel Rosenberg static inline int fscrypt_prepare_setflags(struct inode *inode,
7786e1918cfSDaniel Rosenberg 					   unsigned int oldflags,
7796e1918cfSDaniel Rosenberg 					   unsigned int flags)
7806e1918cfSDaniel Rosenberg {
7816e1918cfSDaniel Rosenberg 	return 0;
7826e1918cfSDaniel Rosenberg }
7836e1918cfSDaniel Rosenberg 
fscrypt_prepare_symlink(struct inode * dir,const char * target,unsigned int len,unsigned int max_len,struct fscrypt_str * disk_link)78431114726SEric Biggers static inline int fscrypt_prepare_symlink(struct inode *dir,
78531114726SEric Biggers 					  const char *target,
786643fa961SChandan Rajendra 					  unsigned int len,
787643fa961SChandan Rajendra 					  unsigned int max_len,
788643fa961SChandan Rajendra 					  struct fscrypt_str *disk_link)
789643fa961SChandan Rajendra {
79031114726SEric Biggers 	if (IS_ENCRYPTED(dir))
791643fa961SChandan Rajendra 		return -EOPNOTSUPP;
79231114726SEric Biggers 	disk_link->name = (unsigned char *)target;
79331114726SEric Biggers 	disk_link->len = len + 1;
79431114726SEric Biggers 	if (disk_link->len > max_len)
79531114726SEric Biggers 		return -ENAMETOOLONG;
79631114726SEric Biggers 	return 0;
797643fa961SChandan Rajendra }
798643fa961SChandan Rajendra 
__fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)799643fa961SChandan Rajendra static inline int __fscrypt_encrypt_symlink(struct inode *inode,
800643fa961SChandan Rajendra 					    const char *target,
801643fa961SChandan Rajendra 					    unsigned int len,
802643fa961SChandan Rajendra 					    struct fscrypt_str *disk_link)
803643fa961SChandan Rajendra {
804643fa961SChandan Rajendra 	return -EOPNOTSUPP;
805643fa961SChandan Rajendra }
806643fa961SChandan Rajendra 
fscrypt_get_symlink(struct inode * inode,const void * caddr,unsigned int max_size,struct delayed_call * done)807643fa961SChandan Rajendra static inline const char *fscrypt_get_symlink(struct inode *inode,
808643fa961SChandan Rajendra 					      const void *caddr,
809643fa961SChandan Rajendra 					      unsigned int max_size,
810643fa961SChandan Rajendra 					      struct delayed_call *done)
811643fa961SChandan Rajendra {
812643fa961SChandan Rajendra 	return ERR_PTR(-EOPNOTSUPP);
813643fa961SChandan Rajendra }
814eea2c05dSSascha Hauer 
fscrypt_symlink_getattr(const struct path * path,struct kstat * stat)815d1876056SEric Biggers static inline int fscrypt_symlink_getattr(const struct path *path,
816d1876056SEric Biggers 					  struct kstat *stat)
817d1876056SEric Biggers {
818d1876056SEric Biggers 	return -EOPNOTSUPP;
819d1876056SEric Biggers }
820d1876056SEric Biggers 
fscrypt_set_ops(struct super_block * sb,const struct fscrypt_operations * s_cop)821eea2c05dSSascha Hauer static inline void fscrypt_set_ops(struct super_block *sb,
822eea2c05dSSascha Hauer 				   const struct fscrypt_operations *s_cop)
823eea2c05dSSascha Hauer {
824eea2c05dSSascha Hauer }
825eea2c05dSSascha Hauer 
826643fa961SChandan Rajendra #endif	/* !CONFIG_FS_ENCRYPTION */
827734f0d24SDave Chinner 
8285fee3609SSatya Tangirala /* inline_crypt.c */
8295fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
8305fee3609SSatya Tangirala 
8315fee3609SSatya Tangirala bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
8325fee3609SSatya Tangirala 
8335fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx(struct bio *bio,
8345fee3609SSatya Tangirala 			       const struct inode *inode, u64 first_lblk,
8355fee3609SSatya Tangirala 			       gfp_t gfp_mask);
8365fee3609SSatya Tangirala 
8375fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
8385fee3609SSatya Tangirala 				  const struct buffer_head *first_bh,
8395fee3609SSatya Tangirala 				  gfp_t gfp_mask);
8405fee3609SSatya Tangirala 
8415fee3609SSatya Tangirala bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
8425fee3609SSatya Tangirala 			   u64 next_lblk);
8435fee3609SSatya Tangirala 
8445fee3609SSatya Tangirala bool fscrypt_mergeable_bio_bh(struct bio *bio,
8455fee3609SSatya Tangirala 			      const struct buffer_head *next_bh);
8465fee3609SSatya Tangirala 
84753dd3f80SEric Biggers bool fscrypt_dio_supported(struct inode *inode);
848c6c89783SEric Biggers 
849c6c89783SEric Biggers u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
850c6c89783SEric Biggers 
8515fee3609SSatya Tangirala #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
8525fee3609SSatya Tangirala 
__fscrypt_inode_uses_inline_crypto(const struct inode * inode)8535fee3609SSatya Tangirala static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
8545fee3609SSatya Tangirala {
8555fee3609SSatya Tangirala 	return false;
8565fee3609SSatya Tangirala }
8575fee3609SSatya Tangirala 
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)8585fee3609SSatya Tangirala static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
8595fee3609SSatya Tangirala 					     const struct inode *inode,
8605fee3609SSatya Tangirala 					     u64 first_lblk, gfp_t gfp_mask) { }
8615fee3609SSatya Tangirala 
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)8625fee3609SSatya Tangirala static inline void fscrypt_set_bio_crypt_ctx_bh(
8635fee3609SSatya Tangirala 					 struct bio *bio,
8645fee3609SSatya Tangirala 					 const struct buffer_head *first_bh,
8655fee3609SSatya Tangirala 					 gfp_t gfp_mask) { }
8665fee3609SSatya Tangirala 
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)8675fee3609SSatya Tangirala static inline bool fscrypt_mergeable_bio(struct bio *bio,
8685fee3609SSatya Tangirala 					 const struct inode *inode,
8695fee3609SSatya Tangirala 					 u64 next_lblk)
8705fee3609SSatya Tangirala {
8715fee3609SSatya Tangirala 	return true;
8725fee3609SSatya Tangirala }
8735fee3609SSatya Tangirala 
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)8745fee3609SSatya Tangirala static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
8755fee3609SSatya Tangirala 					    const struct buffer_head *next_bh)
8765fee3609SSatya Tangirala {
8775fee3609SSatya Tangirala 	return true;
8785fee3609SSatya Tangirala }
879c6c89783SEric Biggers 
fscrypt_dio_supported(struct inode * inode)88053dd3f80SEric Biggers static inline bool fscrypt_dio_supported(struct inode *inode)
881c6c89783SEric Biggers {
882c6c89783SEric Biggers 	return !fscrypt_needs_contents_encryption(inode);
883c6c89783SEric Biggers }
884c6c89783SEric Biggers 
fscrypt_limit_io_blocks(const struct inode * inode,u64 lblk,u64 nr_blocks)885c6c89783SEric Biggers static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
886c6c89783SEric Biggers 					  u64 nr_blocks)
887c6c89783SEric Biggers {
888c6c89783SEric Biggers 	return nr_blocks;
889c6c89783SEric Biggers }
8905fee3609SSatya Tangirala #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
8915fee3609SSatya Tangirala 
8925fee3609SSatya Tangirala /**
8935fee3609SSatya Tangirala  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
8945fee3609SSatya Tangirala  *					encryption
8955fee3609SSatya Tangirala  * @inode: an inode. If encrypted, its key must be set up.
8965fee3609SSatya Tangirala  *
8975fee3609SSatya Tangirala  * Return: true if the inode requires file contents encryption and if the
8985fee3609SSatya Tangirala  *	   encryption should be done in the block layer via blk-crypto rather
8995fee3609SSatya Tangirala  *	   than in the filesystem layer.
9005fee3609SSatya Tangirala  */
fscrypt_inode_uses_inline_crypto(const struct inode * inode)9015fee3609SSatya Tangirala static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
9025fee3609SSatya Tangirala {
9035fee3609SSatya Tangirala 	return fscrypt_needs_contents_encryption(inode) &&
9045fee3609SSatya Tangirala 	       __fscrypt_inode_uses_inline_crypto(inode);
9055fee3609SSatya Tangirala }
9065fee3609SSatya Tangirala 
9075fee3609SSatya Tangirala /**
9085fee3609SSatya Tangirala  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
9095fee3609SSatya Tangirala  *					  encryption
9105fee3609SSatya Tangirala  * @inode: an inode. If encrypted, its key must be set up.
9115fee3609SSatya Tangirala  *
9125fee3609SSatya Tangirala  * Return: true if the inode requires file contents encryption and if the
9135fee3609SSatya Tangirala  *	   encryption should be done in the filesystem layer rather than in the
9145fee3609SSatya Tangirala  *	   block layer via blk-crypto.
9155fee3609SSatya Tangirala  */
fscrypt_inode_uses_fs_layer_crypto(const struct inode * inode)9165fee3609SSatya Tangirala static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
9175fee3609SSatya Tangirala {
9185fee3609SSatya Tangirala 	return fscrypt_needs_contents_encryption(inode) &&
9195fee3609SSatya Tangirala 	       !__fscrypt_inode_uses_inline_crypto(inode);
9205fee3609SSatya Tangirala }
9215fee3609SSatya Tangirala 
922d293c3e4SEric Biggers /**
923ab673b98SEric Biggers  * fscrypt_has_encryption_key() - check whether an inode has had its key set up
924ab673b98SEric Biggers  * @inode: the inode to check
925ab673b98SEric Biggers  *
926ab673b98SEric Biggers  * Return: %true if the inode has had its encryption key set up, else %false.
927ab673b98SEric Biggers  *
928ab673b98SEric Biggers  * Usually this should be preceded by fscrypt_get_encryption_info() to try to
929ab673b98SEric Biggers  * set up the key first.
930ab673b98SEric Biggers  */
fscrypt_has_encryption_key(const struct inode * inode)931ab673b98SEric Biggers static inline bool fscrypt_has_encryption_key(const struct inode *inode)
932ab673b98SEric Biggers {
9333e7807d5SJosef Bacik 	return fscrypt_get_inode_info(inode) != NULL;
934ab673b98SEric Biggers }
935ab673b98SEric Biggers 
936ab673b98SEric Biggers /**
937d2fe9754SEric Biggers  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
938d2fe9754SEric Biggers  *			    directory
9390ea87a96SEric Biggers  * @old_dentry: an existing dentry for the inode being linked
9400ea87a96SEric Biggers  * @dir: the target directory
9410ea87a96SEric Biggers  * @dentry: negative dentry for the target filename
9420ea87a96SEric Biggers  *
9430ea87a96SEric Biggers  * A new link can only be added to an encrypted directory if the directory's
9440ea87a96SEric Biggers  * encryption key is available --- since otherwise we'd have no way to encrypt
945234f1b7fSEric Biggers  * the filename.
9460ea87a96SEric Biggers  *
9470ea87a96SEric Biggers  * We also verify that the link will not violate the constraint that all files
9480ea87a96SEric Biggers  * in an encrypted directory tree use the same encryption policy.
9490ea87a96SEric Biggers  *
9500ea87a96SEric Biggers  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
951f5e55e77SEric Biggers  * -EXDEV if the link would result in an inconsistent encryption policy, or
9520ea87a96SEric Biggers  * another -errno code.
9530ea87a96SEric Biggers  */
fscrypt_prepare_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)9540ea87a96SEric Biggers static inline int fscrypt_prepare_link(struct dentry *old_dentry,
9550ea87a96SEric Biggers 				       struct inode *dir,
9560ea87a96SEric Biggers 				       struct dentry *dentry)
9570ea87a96SEric Biggers {
9580ea87a96SEric Biggers 	if (IS_ENCRYPTED(dir))
959968dd6d0SEric Biggers 		return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
9600ea87a96SEric Biggers 	return 0;
9610ea87a96SEric Biggers }
9620ea87a96SEric Biggers 
96394b26f36SEric Biggers /**
964d2fe9754SEric Biggers  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
965d2fe9754SEric Biggers  *			      directories
96694b26f36SEric Biggers  * @old_dir: source directory
96794b26f36SEric Biggers  * @old_dentry: dentry for source file
96894b26f36SEric Biggers  * @new_dir: target directory
96994b26f36SEric Biggers  * @new_dentry: dentry for target location (may be negative unless exchanging)
97094b26f36SEric Biggers  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
97194b26f36SEric Biggers  *
97294b26f36SEric Biggers  * Prepare for ->rename() where the source and/or target directories may be
97394b26f36SEric Biggers  * encrypted.  A new link can only be added to an encrypted directory if the
97494b26f36SEric Biggers  * directory's encryption key is available --- since otherwise we'd have no way
97594b26f36SEric Biggers  * to encrypt the filename.  A rename to an existing name, on the other hand,
97694b26f36SEric Biggers  * *is* cryptographically possible without the key.  However, we take the more
97794b26f36SEric Biggers  * conservative approach and just forbid all no-key renames.
97894b26f36SEric Biggers  *
97994b26f36SEric Biggers  * We also verify that the rename will not violate the constraint that all files
98094b26f36SEric Biggers  * in an encrypted directory tree use the same encryption policy.
98194b26f36SEric Biggers  *
982f5e55e77SEric Biggers  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
98394b26f36SEric Biggers  * rename would cause inconsistent encryption policies, or another -errno code.
98494b26f36SEric Biggers  */
fscrypt_prepare_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)98594b26f36SEric Biggers static inline int fscrypt_prepare_rename(struct inode *old_dir,
98694b26f36SEric Biggers 					 struct dentry *old_dentry,
98794b26f36SEric Biggers 					 struct inode *new_dir,
98894b26f36SEric Biggers 					 struct dentry *new_dentry,
98994b26f36SEric Biggers 					 unsigned int flags)
99094b26f36SEric Biggers {
99194b26f36SEric Biggers 	if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
99294b26f36SEric Biggers 		return __fscrypt_prepare_rename(old_dir, old_dentry,
99394b26f36SEric Biggers 						new_dir, new_dentry, flags);
99494b26f36SEric Biggers 	return 0;
99594b26f36SEric Biggers }
99694b26f36SEric Biggers 
99732c3cf02SEric Biggers /**
998d2fe9754SEric Biggers  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
999d2fe9754SEric Biggers  *			      directory
100032c3cf02SEric Biggers  * @dir: directory being searched
100132c3cf02SEric Biggers  * @dentry: filename being looked up
1002b01531dbSEric Biggers  * @fname: (output) the name to use to search the on-disk directory
100332c3cf02SEric Biggers  *
1004b01531dbSEric Biggers  * Prepare for ->lookup() in a directory which may be encrypted by determining
100570fb2612SEric Biggers  * the name that will actually be used to search the directory on-disk.  If the
1006a14d0b67SEric Biggers  * directory's encryption policy is supported by this kernel and its encryption
1007a14d0b67SEric Biggers  * key is available, then the lookup is assumed to be by plaintext name;
1008a14d0b67SEric Biggers  * otherwise, it is assumed to be by no-key name.
100932c3cf02SEric Biggers  *
1010bb9cd910SDaniel Rosenberg  * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
1011bb9cd910SDaniel Rosenberg  * name.  In this case the filesystem must assign the dentry a dentry_operations
1012bb9cd910SDaniel Rosenberg  * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
1013bb9cd910SDaniel Rosenberg  * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
1014bb9cd910SDaniel Rosenberg  * directory's encryption key is later added.
101532c3cf02SEric Biggers  *
101670fb2612SEric Biggers  * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
101770fb2612SEric Biggers  * filename isn't a valid no-key name, so a negative dentry should be created;
101870fb2612SEric Biggers  * or another -errno code.
101932c3cf02SEric Biggers  */
fscrypt_prepare_lookup(struct inode * dir,struct dentry * dentry,struct fscrypt_name * fname)102032c3cf02SEric Biggers static inline int fscrypt_prepare_lookup(struct inode *dir,
102132c3cf02SEric Biggers 					 struct dentry *dentry,
1022b01531dbSEric Biggers 					 struct fscrypt_name *fname)
102332c3cf02SEric Biggers {
102432c3cf02SEric Biggers 	if (IS_ENCRYPTED(dir))
1025b01531dbSEric Biggers 		return __fscrypt_prepare_lookup(dir, dentry, fname);
1026b01531dbSEric Biggers 
1027b01531dbSEric Biggers 	memset(fname, 0, sizeof(*fname));
1028b01531dbSEric Biggers 	fname->usr_fname = &dentry->d_name;
1029b01531dbSEric Biggers 	fname->disk_name.name = (unsigned char *)dentry->d_name.name;
1030b01531dbSEric Biggers 	fname->disk_name.len = dentry->d_name.len;
1031e86e6638SGabriel Krisman Bertazi 
1032e86e6638SGabriel Krisman Bertazi 	fscrypt_prepare_dentry(dentry, false);
1033e86e6638SGabriel Krisman Bertazi 
103432c3cf02SEric Biggers 	return 0;
103532c3cf02SEric Biggers }
103632c3cf02SEric Biggers 
1037815dac33SEric Biggers /**
1038ec0caa97SEric Biggers  * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
1039ec0caa97SEric Biggers  * @dir: the directory inode
1040ec0caa97SEric Biggers  *
1041ec0caa97SEric Biggers  * If the directory is encrypted and it doesn't already have its encryption key
1042ec0caa97SEric Biggers  * set up, try to set it up so that the filenames will be listed in plaintext
1043ec0caa97SEric Biggers  * form rather than in no-key form.
1044ec0caa97SEric Biggers  *
1045ec0caa97SEric Biggers  * Return: 0 on success; -errno on error.  Note that the encryption key being
1046a14d0b67SEric Biggers  *	   unavailable is not considered an error.  It is also not an error if
1047a14d0b67SEric Biggers  *	   the encryption policy is unsupported by this kernel; that is treated
1048a14d0b67SEric Biggers  *	   like the key being unavailable, so that files can still be deleted.
1049ec0caa97SEric Biggers  */
fscrypt_prepare_readdir(struct inode * dir)1050ec0caa97SEric Biggers static inline int fscrypt_prepare_readdir(struct inode *dir)
1051ec0caa97SEric Biggers {
1052ec0caa97SEric Biggers 	if (IS_ENCRYPTED(dir))
1053ec0caa97SEric Biggers 		return __fscrypt_prepare_readdir(dir);
1054ec0caa97SEric Biggers 	return 0;
1055ec0caa97SEric Biggers }
1056ec0caa97SEric Biggers 
1057ec0caa97SEric Biggers /**
1058d2fe9754SEric Biggers  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
1059d2fe9754SEric Biggers  *			       attributes
1060815dac33SEric Biggers  * @dentry: dentry through which the inode is being changed
1061815dac33SEric Biggers  * @attr: attributes to change
1062815dac33SEric Biggers  *
1063815dac33SEric Biggers  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
1064815dac33SEric Biggers  * most attribute changes are allowed even without the encryption key.  However,
1065815dac33SEric Biggers  * without the encryption key we do have to forbid truncates.  This is needed
1066815dac33SEric Biggers  * because the size being truncated to may not be a multiple of the filesystem
1067815dac33SEric Biggers  * block size, and in that case we'd have to decrypt the final block, zero the
1068815dac33SEric Biggers  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
1069815dac33SEric Biggers  * filesystem block boundary, but it's simpler to just forbid all truncates ---
1070815dac33SEric Biggers  * and we already forbid all other contents modifications without the key.)
1071815dac33SEric Biggers  *
1072815dac33SEric Biggers  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
1073815dac33SEric Biggers  * if a problem occurred while setting up the encryption key.
1074815dac33SEric Biggers  */
fscrypt_prepare_setattr(struct dentry * dentry,struct iattr * attr)1075815dac33SEric Biggers static inline int fscrypt_prepare_setattr(struct dentry *dentry,
1076815dac33SEric Biggers 					  struct iattr *attr)
1077815dac33SEric Biggers {
10787622350eSEric Biggers 	if (IS_ENCRYPTED(d_inode(dentry)))
10797622350eSEric Biggers 		return __fscrypt_prepare_setattr(dentry, attr);
1080815dac33SEric Biggers 	return 0;
1081815dac33SEric Biggers }
1082815dac33SEric Biggers 
108376e81d6dSEric Biggers /**
1084d2fe9754SEric Biggers  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
108576e81d6dSEric Biggers  * @inode: symlink inode
108676e81d6dSEric Biggers  * @target: plaintext symlink target
108776e81d6dSEric Biggers  * @len: length of @target excluding null terminator
108876e81d6dSEric Biggers  * @disk_link: (in/out) the on-disk symlink target being prepared
108976e81d6dSEric Biggers  *
109076e81d6dSEric Biggers  * If the symlink target needs to be encrypted, then this function encrypts it
109176e81d6dSEric Biggers  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
109276e81d6dSEric Biggers  * previously to compute @disk_link->len.  If the filesystem did not allocate a
109376e81d6dSEric Biggers  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
109476e81d6dSEric Biggers  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
109576e81d6dSEric Biggers  *
109676e81d6dSEric Biggers  * Return: 0 on success, -errno on failure
109776e81d6dSEric Biggers  */
fscrypt_encrypt_symlink(struct inode * inode,const char * target,unsigned int len,struct fscrypt_str * disk_link)109876e81d6dSEric Biggers static inline int fscrypt_encrypt_symlink(struct inode *inode,
109976e81d6dSEric Biggers 					  const char *target,
110076e81d6dSEric Biggers 					  unsigned int len,
110176e81d6dSEric Biggers 					  struct fscrypt_str *disk_link)
110276e81d6dSEric Biggers {
110376e81d6dSEric Biggers 	if (IS_ENCRYPTED(inode))
110476e81d6dSEric Biggers 		return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
110576e81d6dSEric Biggers 	return 0;
110676e81d6dSEric Biggers }
110776e81d6dSEric Biggers 
1108d2d0727bSEric Biggers /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
fscrypt_finalize_bounce_page(struct page ** pagep)1109d2d0727bSEric Biggers static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1110d2d0727bSEric Biggers {
1111d2d0727bSEric Biggers 	struct page *page = *pagep;
1112d2d0727bSEric Biggers 
1113d2d0727bSEric Biggers 	if (fscrypt_is_bounce_page(page)) {
1114d2d0727bSEric Biggers 		*pagep = fscrypt_pagecache_page(page);
1115d2d0727bSEric Biggers 		fscrypt_free_bounce_page(page);
1116d2d0727bSEric Biggers 	}
1117d2d0727bSEric Biggers }
1118d2d0727bSEric Biggers 
1119734f0d24SDave Chinner #endif	/* _LINUX_FSCRYPT_H */
1120