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