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