xref: /linux/include/linux/fscrypt.h (revision 6f4acc3a3c300e174e3f586b97b04ed8f5948c36)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * fscrypt.h: declarations for per-file encryption
4  *
5  * Filesystems that implement per-file encryption must include this header
6  * file.
7  *
8  * Copyright (C) 2015, Google, Inc.
9  *
10  * Written by Michael Halcrow, 2015.
11  * Modified by Jaegeuk Kim, 2015.
12  */
13 #ifndef _LINUX_FSCRYPT_H
14 #define _LINUX_FSCRYPT_H
15 
16 #include <linux/fs.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/fscrypt.h>
20 
21 /*
22  * The lengths of all file contents blocks must be divisible by this value.
23  * This is needed to ensure that all contents encryption modes will work, as
24  * some of the supported modes don't support arbitrarily byte-aligned messages.
25  *
26  * Since the needed alignment is 16 bytes, most filesystems will meet this
27  * requirement naturally, as typical block sizes are powers of 2.  However, if a
28  * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
29  * compression), then it will need to pad to this alignment before encryption.
30  */
31 #define FSCRYPT_CONTENTS_ALIGNMENT 16
32 
33 union fscrypt_policy;
34 struct fscrypt_inode_info;
35 struct fs_parameter;
36 struct seq_file;
37 
38 struct fscrypt_str {
39 	unsigned char *name;
40 	u32 len;
41 };
42 
43 struct fscrypt_name {
44 	const struct qstr *usr_fname;
45 	struct fscrypt_str disk_name;
46 	u32 hash;
47 	u32 minor_hash;
48 	struct fscrypt_str crypto_buf;
49 	bool is_nokey_name;
50 };
51 
52 #define FSTR_INIT(n, l)		{ .name = n, .len = l }
53 #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
54 #define fname_name(p)		((p)->disk_name.name)
55 #define fname_len(p)		((p)->disk_name.len)
56 
57 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
58 #define FSCRYPT_SET_CONTEXT_MAX_SIZE	40
59 
60 /* Maximum supported number of block devices per filesystem */
61 #define FSCRYPT_MAX_DEVICES	8
62 
63 #ifdef CONFIG_FS_ENCRYPTION
64 
65 /* Crypto operations for filesystems */
66 struct fscrypt_operations {
67 	/*
68 	 * The offset of the pointer to struct fscrypt_inode_info in the
69 	 * filesystem-specific part of the inode, relative to the beginning of
70 	 * the common part of the inode (the 'struct inode').
71 	 */
72 	ptrdiff_t inode_info_offs;
73 
74 	/*
75 	 * If set, then fs/crypto/ will allocate a global bounce page pool the
76 	 * first time an encryption key is set up for a file.  The bounce page
77 	 * pool is required by the following functions:
78 	 *
79 	 * - fscrypt_encrypt_pagecache_blocks()
80 	 * - fscrypt_zeroout_range() for files not using inline crypto
81 	 *
82 	 * If the filesystem doesn't use those, it doesn't need to set this.
83 	 */
84 	unsigned int needs_bounce_pages : 1;
85 
86 	/*
87 	 * If set, then fs/crypto/ will allow the use of encryption settings
88 	 * that assume inode numbers fit in 32 bits (i.e.
89 	 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64}), provided that the other
90 	 * prerequisites for these settings are also met.  This is only useful
91 	 * if the filesystem wants to support inline encryption hardware that is
92 	 * limited to 32-bit or 64-bit data unit numbers and where programming
93 	 * keyslots is very slow.
94 	 */
95 	unsigned int has_32bit_inodes : 1;
96 
97 	/*
98 	 * If set, then fs/crypto/ will allow users to select a crypto data unit
99 	 * size that is less than the filesystem block size.  This is done via
100 	 * the log2_data_unit_size field of the fscrypt policy.  This flag is
101 	 * not compatible with filesystems that encrypt variable-length blocks
102 	 * (i.e. blocks that aren't all equal to filesystem's block size), for
103 	 * example as a result of compression.  It's also not compatible with
104 	 * the fscrypt_encrypt_block_inplace() and
105 	 * fscrypt_decrypt_block_inplace() functions.
106 	 */
107 	unsigned int supports_subblock_data_units : 1;
108 
109 	/*
110 	 * This field exists only for backwards compatibility reasons and should
111 	 * only be set by the filesystems that are setting it already.  It
112 	 * contains the filesystem-specific key description prefix that is
113 	 * accepted for "logon" keys for v1 fscrypt policies.  This
114 	 * functionality is deprecated in favor of the generic prefix
115 	 * "fscrypt:", which itself is deprecated in favor of the filesystem
116 	 * keyring ioctls such as FS_IOC_ADD_ENCRYPTION_KEY.  Filesystems that
117 	 * are newly adding fscrypt support should not set this field.
118 	 */
119 	const char *legacy_key_prefix;
120 
121 	/*
122 	 * Get the fscrypt context of the given inode.
123 	 *
124 	 * @inode: the inode whose context to get
125 	 * @ctx: the buffer into which to get the context
126 	 * @len: length of the @ctx buffer in bytes
127 	 *
128 	 * Return: On success, returns the length of the context in bytes; this
129 	 *	   may be less than @len.  On failure, returns -ENODATA if the
130 	 *	   inode doesn't have a context, -ERANGE if the context is
131 	 *	   longer than @len, or another -errno code.
132 	 */
133 	int (*get_context)(struct inode *inode, void *ctx, size_t len);
134 
135 	/*
136 	 * Set an fscrypt context on the given inode.
137 	 *
138 	 * @inode: the inode whose context to set.  The inode won't already have
139 	 *	   an fscrypt context.
140 	 * @ctx: the context to set
141 	 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
142 	 * @fs_data: If called from fscrypt_set_context(), this will be the
143 	 *	     value the filesystem passed to fscrypt_set_context().
144 	 *	     Otherwise (i.e. when called from
145 	 *	     FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
146 	 *
147 	 * i_rwsem will be held for write.
148 	 *
149 	 * Return: 0 on success, -errno on failure.
150 	 */
151 	int (*set_context)(struct inode *inode, const void *ctx, size_t len,
152 			   void *fs_data);
153 
154 	/*
155 	 * Get the dummy fscrypt policy in use on the filesystem (if any).
156 	 *
157 	 * Filesystems only need to implement this function if they support the
158 	 * test_dummy_encryption mount option.
159 	 *
160 	 * Return: A pointer to the dummy fscrypt policy, if the filesystem is
161 	 *	   mounted with test_dummy_encryption; otherwise NULL.
162 	 */
163 	const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
164 
165 	/*
166 	 * Check whether a directory is empty.  i_rwsem will be held for write.
167 	 */
168 	bool (*empty_dir)(struct inode *inode);
169 
170 	/*
171 	 * Check whether the filesystem's inode numbers and UUID are stable,
172 	 * meaning that they will never be changed even by offline operations
173 	 * such as filesystem shrinking and therefore can be used in the
174 	 * encryption without the possibility of files becoming unreadable.
175 	 *
176 	 * Filesystems only need to implement this function if they want to
177 	 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags.  These
178 	 * flags are designed to work around the limitations of UFS and eMMC
179 	 * inline crypto hardware, and they shouldn't be used in scenarios where
180 	 * such hardware isn't being used.
181 	 *
182 	 * Leaving this NULL is equivalent to always returning false.
183 	 */
184 	bool (*has_stable_inodes)(struct super_block *sb);
185 
186 	/*
187 	 * Retrieve the list of block devices to which the filesystem may write
188 	 * encrypted file contents.
189 	 *
190 	 * This writes the block_device pointers to @devs and returns the count
191 	 * (between 1 and FSCRYPT_MAX_DEVICES inclusively).
192 	 *
193 	 * If the filesystem can use multiple block devices (other than block
194 	 * devices that aren't used for encrypted file contents, such as
195 	 * external journal devices), and wants to support inline encryption,
196 	 * then it must implement this function.  Otherwise it's not needed.
197 	 */
198 	unsigned int (*get_devices)(
199 		struct super_block *sb,
200 		struct block_device *devs[FSCRYPT_MAX_DEVICES]);
201 };
202 
203 int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
204 			 struct dentry *dentry, unsigned int flags);
205 
206 /*
207  * Returns the address of the fscrypt info pointer within the
208  * filesystem-specific part of the inode.  (To save memory on filesystems that
209  * don't support fscrypt, a field in 'struct inode' itself is no longer used.)
210  */
211 static inline struct fscrypt_inode_info **
212 fscrypt_inode_info_addr(const struct inode *inode)
213 {
214 	VFS_WARN_ON_ONCE(inode->i_sb->s_cop->inode_info_offs == 0);
215 	return (void *)inode + inode->i_sb->s_cop->inode_info_offs;
216 }
217 
218 /*
219  * Load the inode's fscrypt info pointer, using a raw dereference.  Since this
220  * uses a raw dereference with no memory barrier, it is appropriate to use only
221  * when the caller knows the inode's key setup already happened, resulting in
222  * non-NULL fscrypt info.  E.g., the file contents en/decryption functions use
223  * this, since fscrypt_file_open() set up the key.
224  */
225 static inline struct fscrypt_inode_info *
226 fscrypt_get_inode_info_raw(const struct inode *inode)
227 {
228 	struct fscrypt_inode_info *ci = *fscrypt_inode_info_addr(inode);
229 
230 	VFS_WARN_ON_ONCE(ci == NULL);
231 	return ci;
232 }
233 
234 static inline struct fscrypt_inode_info *
235 fscrypt_get_inode_info(const struct inode *inode)
236 {
237 	/*
238 	 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
239 	 * I.e., another task may publish the fscrypt info concurrently,
240 	 * executing a RELEASE barrier.  Use smp_load_acquire() here to safely
241 	 * ACQUIRE the memory the other task published.
242 	 */
243 	return smp_load_acquire(fscrypt_inode_info_addr(inode));
244 }
245 
246 /**
247  * fscrypt_needs_contents_encryption() - check whether an inode needs
248  *					 contents encryption
249  * @inode: the inode to check
250  *
251  * Return: %true iff the inode is an encrypted regular file and the kernel was
252  * built with fscrypt support.
253  *
254  * If you need to know whether the encrypt bit is set even when the kernel was
255  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
256  */
257 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
258 {
259 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
260 }
261 
262 /*
263  * When d_splice_alias() moves a directory's no-key alias to its
264  * plaintext alias as a result of the encryption key being added,
265  * DCACHE_NOKEY_NAME must be cleared and there might be an opportunity
266  * to disable d_revalidate.  Note that we don't have to support the
267  * inverse operation because fscrypt doesn't allow no-key names to be
268  * the source or target of a rename().
269  */
270 static inline void fscrypt_handle_d_move(struct dentry *dentry)
271 {
272 	/*
273 	 * VFS calls fscrypt_handle_d_move even for non-fscrypt
274 	 * filesystems.
275 	 */
276 	if (dentry->d_flags & DCACHE_NOKEY_NAME) {
277 		dentry->d_flags &= ~DCACHE_NOKEY_NAME;
278 
279 		/*
280 		 * Other filesystem features might be handling dentry
281 		 * revalidation, in which case it cannot be disabled.
282 		 */
283 		if (dentry->d_op->d_revalidate == fscrypt_d_revalidate)
284 			dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
285 	}
286 }
287 
288 /**
289  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
290  * @dentry: the dentry to check
291  *
292  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
293  * dentry that was created in an encrypted directory that hasn't had its
294  * encryption key added yet.  Such dentries may be either positive or negative.
295  *
296  * When a filesystem is asked to create a new filename in an encrypted directory
297  * and the new filename's dentry is a no-key dentry, it must fail the operation
298  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
299  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
300  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
301  *
302  * This is necessary because creating a filename requires the directory's
303  * encryption key, but just checking for the key on the directory inode during
304  * the final filesystem operation doesn't guarantee that the key was available
305  * during the preceding dentry lookup.  And the key must have already been
306  * available during the dentry lookup in order for it to have been checked
307  * whether the filename already exists in the directory and for the new file's
308  * dentry not to be invalidated due to it incorrectly having the no-key flag.
309  *
310  * Return: %true if the dentry is a no-key name
311  */
312 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
313 {
314 	return dentry->d_flags & DCACHE_NOKEY_NAME;
315 }
316 
317 static inline void fscrypt_prepare_dentry(struct dentry *dentry,
318 					  bool is_nokey_name)
319 {
320 	/*
321 	 * This code tries to only take ->d_lock when necessary to write
322 	 * to ->d_flags.  We shouldn't be peeking on d_flags for
323 	 * DCACHE_OP_REVALIDATE unlocked, but in the unlikely case
324 	 * there is a race, the worst it can happen is that we fail to
325 	 * unset DCACHE_OP_REVALIDATE and pay the cost of an extra
326 	 * d_revalidate.
327 	 */
328 	if (is_nokey_name) {
329 		spin_lock(&dentry->d_lock);
330 		dentry->d_flags |= DCACHE_NOKEY_NAME;
331 		spin_unlock(&dentry->d_lock);
332 	} else if (dentry->d_flags & DCACHE_OP_REVALIDATE &&
333 		   dentry->d_op->d_revalidate == fscrypt_d_revalidate) {
334 		/*
335 		 * Unencrypted dentries and encrypted dentries where the
336 		 * key is available are always valid from fscrypt
337 		 * perspective. Avoid the cost of calling
338 		 * fscrypt_d_revalidate unnecessarily.
339 		 */
340 		spin_lock(&dentry->d_lock);
341 		dentry->d_flags &= ~DCACHE_OP_REVALIDATE;
342 		spin_unlock(&dentry->d_lock);
343 	}
344 }
345 
346 /* crypto.c */
347 void fscrypt_enqueue_decrypt_work(struct work_struct *);
348 
349 struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
350 		size_t len, size_t offs, gfp_t gfp_flags);
351 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
352 				  unsigned int len, unsigned int offs,
353 				  u64 lblk_num);
354 
355 int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
356 				     size_t offs);
357 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
358 				  unsigned int len, unsigned int offs,
359 				  u64 lblk_num);
360 
361 static inline bool fscrypt_is_bounce_page(struct page *page)
362 {
363 	return page->mapping == NULL;
364 }
365 
366 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
367 {
368 	return (struct page *)page_private(bounce_page);
369 }
370 
371 static inline bool fscrypt_is_bounce_folio(const struct folio *folio)
372 {
373 	return folio->mapping == NULL;
374 }
375 
376 static inline
377 struct folio *fscrypt_pagecache_folio(const struct folio *bounce_folio)
378 {
379 	return bounce_folio->private;
380 }
381 
382 void fscrypt_free_bounce_page(struct page *bounce_page);
383 
384 /* policy.c */
385 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
386 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
387 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
388 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
389 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
390 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
391 int fscrypt_set_context(struct inode *inode, void *fs_data);
392 
393 struct fscrypt_dummy_policy {
394 	const union fscrypt_policy *policy;
395 };
396 
397 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
398 				    struct fscrypt_dummy_policy *dummy_policy);
399 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
400 				  const struct fscrypt_dummy_policy *p2);
401 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
402 					struct super_block *sb);
403 static inline bool
404 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
405 {
406 	return dummy_policy->policy != NULL;
407 }
408 static inline void
409 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
410 {
411 	kfree(dummy_policy->policy);
412 	dummy_policy->policy = NULL;
413 }
414 
415 /* keyring.c */
416 void fscrypt_destroy_keyring(struct super_block *sb);
417 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
418 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
419 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
420 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
421 
422 /* keysetup.c */
423 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
424 			      bool *encrypt_ret);
425 void fscrypt_put_encryption_info(struct inode *inode);
426 void fscrypt_free_inode(struct inode *inode);
427 int fscrypt_drop_inode(struct inode *inode);
428 
429 /* fname.c */
430 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
431 			  u8 *out, unsigned int olen);
432 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
433 				  u32 max_len, u32 *encrypted_len_ret);
434 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
435 			   int lookup, struct fscrypt_name *fname);
436 
437 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
438 {
439 	kfree(fname->crypto_buf.name);
440 }
441 
442 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
443 			       struct fscrypt_str *crypto_str);
444 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
445 int fscrypt_fname_disk_to_usr(const struct inode *inode,
446 			      u32 hash, u32 minor_hash,
447 			      const struct fscrypt_str *iname,
448 			      struct fscrypt_str *oname);
449 bool fscrypt_match_name(const struct fscrypt_name *fname,
450 			const u8 *de_name, u32 de_name_len);
451 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
452 
453 /* bio.c */
454 bool fscrypt_decrypt_bio(struct bio *bio);
455 int fscrypt_zeroout_range(const struct inode *inode, loff_t pos,
456 			  sector_t sector, u64 len);
457 
458 /* hooks.c */
459 int fscrypt_file_open(struct inode *inode, struct file *filp);
460 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
461 			   struct dentry *dentry);
462 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
463 			     struct inode *new_dir, struct dentry *new_dentry,
464 			     unsigned int flags);
465 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
466 			     struct fscrypt_name *fname);
467 int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
468 int __fscrypt_prepare_readdir(struct inode *dir);
469 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
470 int fscrypt_prepare_setflags(struct inode *inode,
471 			     unsigned int oldflags, unsigned int flags);
472 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
473 			    unsigned int len, unsigned int max_len,
474 			    struct fscrypt_str *disk_link);
475 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
476 			      unsigned int len, struct fscrypt_str *disk_link);
477 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
478 				unsigned int max_size,
479 				struct delayed_call *done);
480 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
481 static inline void fscrypt_set_ops(struct super_block *sb,
482 				   const struct fscrypt_operations *s_cop)
483 {
484 	sb->s_cop = s_cop;
485 }
486 #else  /* !CONFIG_FS_ENCRYPTION */
487 
488 static inline struct fscrypt_inode_info *
489 fscrypt_get_inode_info(const struct inode *inode)
490 {
491 	return NULL;
492 }
493 
494 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
495 {
496 	return false;
497 }
498 
499 static inline void fscrypt_handle_d_move(struct dentry *dentry)
500 {
501 }
502 
503 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
504 {
505 	return false;
506 }
507 
508 static inline void fscrypt_prepare_dentry(struct dentry *dentry,
509 					  bool is_nokey_name)
510 {
511 }
512 
513 /* crypto.c */
514 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
515 {
516 }
517 
518 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct folio *folio,
519 		size_t len, size_t offs, gfp_t gfp_flags)
520 {
521 	return ERR_PTR(-EOPNOTSUPP);
522 }
523 
524 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
525 						struct page *page,
526 						unsigned int len,
527 						unsigned int offs, u64 lblk_num)
528 {
529 	return -EOPNOTSUPP;
530 }
531 
532 static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio,
533 						   size_t len, size_t offs)
534 {
535 	return -EOPNOTSUPP;
536 }
537 
538 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
539 						struct page *page,
540 						unsigned int len,
541 						unsigned int offs, u64 lblk_num)
542 {
543 	return -EOPNOTSUPP;
544 }
545 
546 static inline bool fscrypt_is_bounce_page(struct page *page)
547 {
548 	return false;
549 }
550 
551 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
552 {
553 	WARN_ON_ONCE(1);
554 	return ERR_PTR(-EINVAL);
555 }
556 
557 static inline bool fscrypt_is_bounce_folio(const struct folio *folio)
558 {
559 	return false;
560 }
561 
562 static inline
563 struct folio *fscrypt_pagecache_folio(const struct folio *bounce_folio)
564 {
565 	WARN_ON_ONCE(1);
566 	return ERR_PTR(-EINVAL);
567 }
568 
569 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
570 {
571 }
572 
573 /* policy.c */
574 static inline int fscrypt_ioctl_set_policy(struct file *filp,
575 					   const void __user *arg)
576 {
577 	return -EOPNOTSUPP;
578 }
579 
580 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
581 {
582 	return -EOPNOTSUPP;
583 }
584 
585 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
586 					      void __user *arg)
587 {
588 	return -EOPNOTSUPP;
589 }
590 
591 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
592 {
593 	return -EOPNOTSUPP;
594 }
595 
596 static inline int fscrypt_has_permitted_context(struct inode *parent,
597 						struct inode *child)
598 {
599 	return 0;
600 }
601 
602 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
603 {
604 	return -EOPNOTSUPP;
605 }
606 
607 struct fscrypt_dummy_policy {
608 };
609 
610 static inline int
611 fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
612 				    struct fscrypt_dummy_policy *dummy_policy)
613 {
614 	return -EINVAL;
615 }
616 
617 static inline bool
618 fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
619 			     const struct fscrypt_dummy_policy *p2)
620 {
621 	return true;
622 }
623 
624 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
625 						      char sep,
626 						      struct super_block *sb)
627 {
628 }
629 
630 static inline bool
631 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
632 {
633 	return false;
634 }
635 
636 static inline void
637 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
638 {
639 }
640 
641 /* keyring.c */
642 static inline void fscrypt_destroy_keyring(struct super_block *sb)
643 {
644 }
645 
646 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
647 {
648 	return -EOPNOTSUPP;
649 }
650 
651 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
652 {
653 	return -EOPNOTSUPP;
654 }
655 
656 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
657 						     void __user *arg)
658 {
659 	return -EOPNOTSUPP;
660 }
661 
662 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
663 					       void __user *arg)
664 {
665 	return -EOPNOTSUPP;
666 }
667 
668 /* keysetup.c */
669 
670 static inline int fscrypt_prepare_new_inode(struct inode *dir,
671 					    struct inode *inode,
672 					    bool *encrypt_ret)
673 {
674 	if (IS_ENCRYPTED(dir))
675 		return -EOPNOTSUPP;
676 	return 0;
677 }
678 
679 static inline void fscrypt_put_encryption_info(struct inode *inode)
680 {
681 	return;
682 }
683 
684 static inline void fscrypt_free_inode(struct inode *inode)
685 {
686 }
687 
688 static inline int fscrypt_drop_inode(struct inode *inode)
689 {
690 	return 0;
691 }
692 
693  /* fname.c */
694 static inline int fscrypt_setup_filename(struct inode *dir,
695 					 const struct qstr *iname,
696 					 int lookup, struct fscrypt_name *fname)
697 {
698 	if (IS_ENCRYPTED(dir))
699 		return -EOPNOTSUPP;
700 
701 	memset(fname, 0, sizeof(*fname));
702 	fname->usr_fname = iname;
703 	fname->disk_name.name = (unsigned char *)iname->name;
704 	fname->disk_name.len = iname->len;
705 	return 0;
706 }
707 
708 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
709 {
710 	return;
711 }
712 
713 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
714 					     struct fscrypt_str *crypto_str)
715 {
716 	return -EOPNOTSUPP;
717 }
718 
719 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
720 {
721 	return;
722 }
723 
724 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
725 					    u32 hash, u32 minor_hash,
726 					    const struct fscrypt_str *iname,
727 					    struct fscrypt_str *oname)
728 {
729 	return -EOPNOTSUPP;
730 }
731 
732 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
733 				      const u8 *de_name, u32 de_name_len)
734 {
735 	/* Encryption support disabled; use standard comparison */
736 	if (de_name_len != fname->disk_name.len)
737 		return false;
738 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
739 }
740 
741 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
742 					const struct qstr *name)
743 {
744 	WARN_ON_ONCE(1);
745 	return 0;
746 }
747 
748 static inline int fscrypt_d_revalidate(struct inode *dir, const struct qstr *name,
749 				       struct dentry *dentry, unsigned int flags)
750 {
751 	return 1;
752 }
753 
754 /* bio.c */
755 static inline bool fscrypt_decrypt_bio(struct bio *bio)
756 {
757 	return true;
758 }
759 
760 static inline int fscrypt_zeroout_range(const struct inode *inode, loff_t pos,
761 					sector_t sector, u64 len)
762 {
763 	return -EOPNOTSUPP;
764 }
765 
766 /* hooks.c */
767 
768 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
769 {
770 	if (IS_ENCRYPTED(inode))
771 		return -EOPNOTSUPP;
772 	return 0;
773 }
774 
775 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
776 					 struct dentry *dentry)
777 {
778 	return -EOPNOTSUPP;
779 }
780 
781 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
782 					   struct dentry *old_dentry,
783 					   struct inode *new_dir,
784 					   struct dentry *new_dentry,
785 					   unsigned int flags)
786 {
787 	return -EOPNOTSUPP;
788 }
789 
790 static inline int __fscrypt_prepare_lookup(struct inode *dir,
791 					   struct dentry *dentry,
792 					   struct fscrypt_name *fname)
793 {
794 	return -EOPNOTSUPP;
795 }
796 
797 static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
798 						 struct dentry *dentry)
799 {
800 	return -EOPNOTSUPP;
801 }
802 
803 static inline int __fscrypt_prepare_readdir(struct inode *dir)
804 {
805 	return -EOPNOTSUPP;
806 }
807 
808 static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
809 					    struct iattr *attr)
810 {
811 	return -EOPNOTSUPP;
812 }
813 
814 static inline int fscrypt_prepare_setflags(struct inode *inode,
815 					   unsigned int oldflags,
816 					   unsigned int flags)
817 {
818 	return 0;
819 }
820 
821 static inline int fscrypt_prepare_symlink(struct inode *dir,
822 					  const char *target,
823 					  unsigned int len,
824 					  unsigned int max_len,
825 					  struct fscrypt_str *disk_link)
826 {
827 	if (IS_ENCRYPTED(dir))
828 		return -EOPNOTSUPP;
829 	disk_link->name = (unsigned char *)target;
830 	disk_link->len = len + 1;
831 	if (disk_link->len > max_len)
832 		return -ENAMETOOLONG;
833 	return 0;
834 }
835 
836 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
837 					    const char *target,
838 					    unsigned int len,
839 					    struct fscrypt_str *disk_link)
840 {
841 	return -EOPNOTSUPP;
842 }
843 
844 static inline const char *fscrypt_get_symlink(struct inode *inode,
845 					      const void *caddr,
846 					      unsigned int max_size,
847 					      struct delayed_call *done)
848 {
849 	return ERR_PTR(-EOPNOTSUPP);
850 }
851 
852 static inline int fscrypt_symlink_getattr(const struct path *path,
853 					  struct kstat *stat)
854 {
855 	return -EOPNOTSUPP;
856 }
857 
858 static inline void fscrypt_set_ops(struct super_block *sb,
859 				   const struct fscrypt_operations *s_cop)
860 {
861 }
862 
863 #endif	/* !CONFIG_FS_ENCRYPTION */
864 
865 /* inline_crypt.c */
866 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
867 
868 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
869 
870 void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
871 			       loff_t pos, gfp_t gfp_mask);
872 
873 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
874 			   loff_t pos);
875 
876 bool fscrypt_dio_supported(struct inode *inode);
877 
878 u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
879 
880 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
881 
882 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
883 {
884 	return false;
885 }
886 
887 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
888 					     const struct inode *inode,
889 					     loff_t pos, gfp_t gfp_mask) { }
890 
891 static inline bool fscrypt_mergeable_bio(struct bio *bio,
892 					 const struct inode *inode,
893 					 loff_t pos)
894 {
895 	return true;
896 }
897 
898 static inline bool fscrypt_dio_supported(struct inode *inode)
899 {
900 	return !fscrypt_needs_contents_encryption(inode);
901 }
902 
903 static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
904 					  u64 nr_blocks)
905 {
906 	return nr_blocks;
907 }
908 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
909 
910 /**
911  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
912  *					encryption
913  * @inode: an inode. If encrypted, its key must be set up.
914  *
915  * Return: true if the inode requires file contents encryption and if the
916  *	   encryption should be done in the block layer via blk-crypto rather
917  *	   than in the filesystem layer.
918  */
919 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
920 {
921 	return fscrypt_needs_contents_encryption(inode) &&
922 	       __fscrypt_inode_uses_inline_crypto(inode);
923 }
924 
925 /**
926  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
927  *					  encryption
928  * @inode: an inode. If encrypted, its key must be set up.
929  *
930  * Return: true if the inode requires file contents encryption and if the
931  *	   encryption should be done in the filesystem layer rather than in the
932  *	   block layer via blk-crypto.
933  */
934 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
935 {
936 	return fscrypt_needs_contents_encryption(inode) &&
937 	       !__fscrypt_inode_uses_inline_crypto(inode);
938 }
939 
940 /**
941  * fscrypt_has_encryption_key() - check whether an inode has had its key set up
942  * @inode: the inode to check
943  *
944  * Return: %true if the inode has had its encryption key set up, else %false.
945  *
946  * Usually this should be preceded by fscrypt_get_encryption_info() to try to
947  * set up the key first.
948  */
949 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
950 {
951 	return fscrypt_get_inode_info(inode) != NULL;
952 }
953 
954 /**
955  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
956  *			    directory
957  * @old_dentry: an existing dentry for the inode being linked
958  * @dir: the target directory
959  * @dentry: negative dentry for the target filename
960  *
961  * A new link can only be added to an encrypted directory if the directory's
962  * encryption key is available --- since otherwise we'd have no way to encrypt
963  * the filename.
964  *
965  * We also verify that the link will not violate the constraint that all files
966  * in an encrypted directory tree use the same encryption policy.
967  *
968  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
969  * -EXDEV if the link would result in an inconsistent encryption policy, or
970  * another -errno code.
971  */
972 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
973 				       struct inode *dir,
974 				       struct dentry *dentry)
975 {
976 	if (IS_ENCRYPTED(dir))
977 		return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
978 	return 0;
979 }
980 
981 /**
982  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
983  *			      directories
984  * @old_dir: source directory
985  * @old_dentry: dentry for source file
986  * @new_dir: target directory
987  * @new_dentry: dentry for target location (may be negative unless exchanging)
988  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
989  *
990  * Prepare for ->rename() where the source and/or target directories may be
991  * encrypted.  A new link can only be added to an encrypted directory if the
992  * directory's encryption key is available --- since otherwise we'd have no way
993  * to encrypt the filename.  A rename to an existing name, on the other hand,
994  * *is* cryptographically possible without the key.  However, we take the more
995  * conservative approach and just forbid all no-key renames.
996  *
997  * We also verify that the rename will not violate the constraint that all files
998  * in an encrypted directory tree use the same encryption policy.
999  *
1000  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
1001  * rename would cause inconsistent encryption policies, or another -errno code.
1002  */
1003 static inline int fscrypt_prepare_rename(struct inode *old_dir,
1004 					 struct dentry *old_dentry,
1005 					 struct inode *new_dir,
1006 					 struct dentry *new_dentry,
1007 					 unsigned int flags)
1008 {
1009 	if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
1010 		return __fscrypt_prepare_rename(old_dir, old_dentry,
1011 						new_dir, new_dentry, flags);
1012 	return 0;
1013 }
1014 
1015 /**
1016  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
1017  *			      directory
1018  * @dir: directory being searched
1019  * @dentry: filename being looked up
1020  * @fname: (output) the name to use to search the on-disk directory
1021  *
1022  * Prepare for ->lookup() in a directory which may be encrypted by determining
1023  * the name that will actually be used to search the directory on-disk.  If the
1024  * directory's encryption policy is supported by this kernel and its encryption
1025  * key is available, then the lookup is assumed to be by plaintext name;
1026  * otherwise, it is assumed to be by no-key name.
1027  *
1028  * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
1029  * name.  In this case the filesystem must assign the dentry a dentry_operations
1030  * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
1031  * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
1032  * directory's encryption key is later added.
1033  *
1034  * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
1035  * filename isn't a valid no-key name, so a negative dentry should be created;
1036  * or another -errno code.
1037  */
1038 static inline int fscrypt_prepare_lookup(struct inode *dir,
1039 					 struct dentry *dentry,
1040 					 struct fscrypt_name *fname)
1041 {
1042 	if (IS_ENCRYPTED(dir))
1043 		return __fscrypt_prepare_lookup(dir, dentry, fname);
1044 
1045 	memset(fname, 0, sizeof(*fname));
1046 	fname->usr_fname = &dentry->d_name;
1047 	fname->disk_name.name = (unsigned char *)dentry->d_name.name;
1048 	fname->disk_name.len = dentry->d_name.len;
1049 
1050 	fscrypt_prepare_dentry(dentry, false);
1051 
1052 	return 0;
1053 }
1054 
1055 /**
1056  * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
1057  * @dir: the directory inode
1058  *
1059  * If the directory is encrypted and it doesn't already have its encryption key
1060  * set up, try to set it up so that the filenames will be listed in plaintext
1061  * form rather than in no-key form.
1062  *
1063  * Return: 0 on success; -errno on error.  Note that the encryption key being
1064  *	   unavailable is not considered an error.  It is also not an error if
1065  *	   the encryption policy is unsupported by this kernel; that is treated
1066  *	   like the key being unavailable, so that files can still be deleted.
1067  */
1068 static inline int fscrypt_prepare_readdir(struct inode *dir)
1069 {
1070 	if (IS_ENCRYPTED(dir))
1071 		return __fscrypt_prepare_readdir(dir);
1072 	return 0;
1073 }
1074 
1075 /**
1076  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
1077  *			       attributes
1078  * @dentry: dentry through which the inode is being changed
1079  * @attr: attributes to change
1080  *
1081  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
1082  * most attribute changes are allowed even without the encryption key.  However,
1083  * without the encryption key we do have to forbid truncates.  This is needed
1084  * because the size being truncated to may not be a multiple of the filesystem
1085  * block size, and in that case we'd have to decrypt the final block, zero the
1086  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
1087  * filesystem block boundary, but it's simpler to just forbid all truncates ---
1088  * and we already forbid all other contents modifications without the key.)
1089  *
1090  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
1091  * if a problem occurred while setting up the encryption key.
1092  */
1093 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
1094 					  struct iattr *attr)
1095 {
1096 	if (IS_ENCRYPTED(d_inode(dentry)))
1097 		return __fscrypt_prepare_setattr(dentry, attr);
1098 	return 0;
1099 }
1100 
1101 /**
1102  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
1103  * @inode: symlink inode
1104  * @target: plaintext symlink target
1105  * @len: length of @target excluding null terminator
1106  * @disk_link: (in/out) the on-disk symlink target being prepared
1107  *
1108  * If the symlink target needs to be encrypted, then this function encrypts it
1109  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
1110  * previously to compute @disk_link->len.  If the filesystem did not allocate a
1111  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1112  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1113  *
1114  * Return: 0 on success, -errno on failure
1115  */
1116 static inline int fscrypt_encrypt_symlink(struct inode *inode,
1117 					  const char *target,
1118 					  unsigned int len,
1119 					  struct fscrypt_str *disk_link)
1120 {
1121 	if (IS_ENCRYPTED(inode))
1122 		return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1123 	return 0;
1124 }
1125 
1126 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
1127 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1128 {
1129 	struct page *page = *pagep;
1130 
1131 	if (fscrypt_is_bounce_page(page)) {
1132 		*pagep = fscrypt_pagecache_page(page);
1133 		fscrypt_free_bounce_page(page);
1134 	}
1135 }
1136 
1137 #endif	/* _LINUX_FSCRYPT_H */
1138