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