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