xref: /openbmc/linux/include/linux/fscrypt.h (revision 15e3ae36)
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 #define FS_CRYPTO_BLOCK_SIZE		16
22 
23 struct fscrypt_info;
24 
25 struct fscrypt_str {
26 	unsigned char *name;
27 	u32 len;
28 };
29 
30 struct fscrypt_name {
31 	const struct qstr *usr_fname;
32 	struct fscrypt_str disk_name;
33 	u32 hash;
34 	u32 minor_hash;
35 	struct fscrypt_str crypto_buf;
36 	bool is_ciphertext_name;
37 };
38 
39 #define FSTR_INIT(n, l)		{ .name = n, .len = l }
40 #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
41 #define fname_name(p)		((p)->disk_name.name)
42 #define fname_len(p)		((p)->disk_name.len)
43 
44 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
45 #define FSCRYPT_SET_CONTEXT_MAX_SIZE	40
46 
47 #ifdef CONFIG_FS_ENCRYPTION
48 /*
49  * fscrypt superblock flags
50  */
51 #define FS_CFLG_OWN_PAGES (1U << 1)
52 
53 /*
54  * crypto operations for filesystems
55  */
56 struct fscrypt_operations {
57 	unsigned int flags;
58 	const char *key_prefix;
59 	int (*get_context)(struct inode *, void *, size_t);
60 	int (*set_context)(struct inode *, const void *, size_t, void *);
61 	bool (*dummy_context)(struct inode *);
62 	bool (*empty_dir)(struct inode *);
63 	unsigned int max_namelen;
64 	bool (*has_stable_inodes)(struct super_block *sb);
65 	void (*get_ino_and_lblk_bits)(struct super_block *sb,
66 				      int *ino_bits_ret, int *lblk_bits_ret);
67 };
68 
69 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
70 {
71 	/* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
72 	return READ_ONCE(inode->i_crypt_info) != NULL;
73 }
74 
75 /**
76  * fscrypt_needs_contents_encryption() - check whether an inode needs
77  *					 contents encryption
78  *
79  * Return: %true iff the inode is an encrypted regular file and the kernel was
80  * built with fscrypt support.
81  *
82  * If you need to know whether the encrypt bit is set even when the kernel was
83  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
84  */
85 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
86 {
87 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
88 }
89 
90 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
91 {
92 	return inode->i_sb->s_cop->dummy_context &&
93 		inode->i_sb->s_cop->dummy_context(inode);
94 }
95 
96 /*
97  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
98  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
99  * must be cleared.  Note that we don't have to support arbitrary moves of this
100  * flag because fscrypt doesn't allow encrypted aliases to be the source or
101  * target of a rename().
102  */
103 static inline void fscrypt_handle_d_move(struct dentry *dentry)
104 {
105 	dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
106 }
107 
108 /* crypto.c */
109 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
110 
111 extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
112 						     unsigned int len,
113 						     unsigned int offs,
114 						     gfp_t gfp_flags);
115 extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
116 					 struct page *page, unsigned int len,
117 					 unsigned int offs, u64 lblk_num,
118 					 gfp_t gfp_flags);
119 
120 extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
121 					    unsigned int offs);
122 extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
123 					 struct page *page, unsigned int len,
124 					 unsigned int offs, u64 lblk_num);
125 
126 static inline bool fscrypt_is_bounce_page(struct page *page)
127 {
128 	return page->mapping == NULL;
129 }
130 
131 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
132 {
133 	return (struct page *)page_private(bounce_page);
134 }
135 
136 extern void fscrypt_free_bounce_page(struct page *bounce_page);
137 
138 /* policy.c */
139 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
140 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
141 extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
142 extern int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
143 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
144 extern int fscrypt_inherit_context(struct inode *, struct inode *,
145 					void *, bool);
146 /* keyring.c */
147 extern void fscrypt_sb_free(struct super_block *sb);
148 extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
149 extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
150 extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
151 					      void __user *arg);
152 extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
153 
154 /* keysetup.c */
155 extern int fscrypt_get_encryption_info(struct inode *);
156 extern void fscrypt_put_encryption_info(struct inode *);
157 extern void fscrypt_free_inode(struct inode *);
158 extern int fscrypt_drop_inode(struct inode *inode);
159 
160 /* fname.c */
161 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
162 				int lookup, struct fscrypt_name *);
163 
164 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
165 {
166 	kfree(fname->crypto_buf.name);
167 }
168 
169 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
170 				struct fscrypt_str *);
171 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
172 extern int fscrypt_fname_disk_to_usr(const struct inode *inode,
173 				     u32 hash, u32 minor_hash,
174 				     const struct fscrypt_str *iname,
175 				     struct fscrypt_str *oname);
176 extern bool fscrypt_match_name(const struct fscrypt_name *fname,
177 			       const u8 *de_name, u32 de_name_len);
178 extern u64 fscrypt_fname_siphash(const struct inode *dir,
179 				 const struct qstr *name);
180 
181 /* bio.c */
182 extern void fscrypt_decrypt_bio(struct bio *);
183 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
184 				 unsigned int);
185 
186 /* hooks.c */
187 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
188 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
189 				  struct dentry *dentry);
190 extern int __fscrypt_prepare_rename(struct inode *old_dir,
191 				    struct dentry *old_dentry,
192 				    struct inode *new_dir,
193 				    struct dentry *new_dentry,
194 				    unsigned int flags);
195 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
196 				    struct fscrypt_name *fname);
197 extern int fscrypt_prepare_setflags(struct inode *inode,
198 				    unsigned int oldflags, unsigned int flags);
199 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
200 				     unsigned int max_len,
201 				     struct fscrypt_str *disk_link);
202 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
203 				     unsigned int len,
204 				     struct fscrypt_str *disk_link);
205 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
206 				       unsigned int max_size,
207 				       struct delayed_call *done);
208 static inline void fscrypt_set_ops(struct super_block *sb,
209 				   const struct fscrypt_operations *s_cop)
210 {
211 	sb->s_cop = s_cop;
212 }
213 #else  /* !CONFIG_FS_ENCRYPTION */
214 
215 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
216 {
217 	return false;
218 }
219 
220 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
221 {
222 	return false;
223 }
224 
225 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
226 {
227 	return false;
228 }
229 
230 static inline void fscrypt_handle_d_move(struct dentry *dentry)
231 {
232 }
233 
234 /* crypto.c */
235 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
236 {
237 }
238 
239 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
240 							    unsigned int len,
241 							    unsigned int offs,
242 							    gfp_t gfp_flags)
243 {
244 	return ERR_PTR(-EOPNOTSUPP);
245 }
246 
247 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
248 						struct page *page,
249 						unsigned int len,
250 						unsigned int offs, u64 lblk_num,
251 						gfp_t gfp_flags)
252 {
253 	return -EOPNOTSUPP;
254 }
255 
256 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
257 						   unsigned int len,
258 						   unsigned int offs)
259 {
260 	return -EOPNOTSUPP;
261 }
262 
263 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
264 						struct page *page,
265 						unsigned int len,
266 						unsigned int offs, u64 lblk_num)
267 {
268 	return -EOPNOTSUPP;
269 }
270 
271 static inline bool fscrypt_is_bounce_page(struct page *page)
272 {
273 	return false;
274 }
275 
276 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
277 {
278 	WARN_ON_ONCE(1);
279 	return ERR_PTR(-EINVAL);
280 }
281 
282 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
283 {
284 }
285 
286 /* policy.c */
287 static inline int fscrypt_ioctl_set_policy(struct file *filp,
288 					   const void __user *arg)
289 {
290 	return -EOPNOTSUPP;
291 }
292 
293 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
294 {
295 	return -EOPNOTSUPP;
296 }
297 
298 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
299 					      void __user *arg)
300 {
301 	return -EOPNOTSUPP;
302 }
303 
304 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
305 {
306 	return -EOPNOTSUPP;
307 }
308 
309 static inline int fscrypt_has_permitted_context(struct inode *parent,
310 						struct inode *child)
311 {
312 	return 0;
313 }
314 
315 static inline int fscrypt_inherit_context(struct inode *parent,
316 					  struct inode *child,
317 					  void *fs_data, bool preload)
318 {
319 	return -EOPNOTSUPP;
320 }
321 
322 /* keyring.c */
323 static inline void fscrypt_sb_free(struct super_block *sb)
324 {
325 }
326 
327 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
328 {
329 	return -EOPNOTSUPP;
330 }
331 
332 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
333 {
334 	return -EOPNOTSUPP;
335 }
336 
337 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
338 						     void __user *arg)
339 {
340 	return -EOPNOTSUPP;
341 }
342 
343 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
344 					       void __user *arg)
345 {
346 	return -EOPNOTSUPP;
347 }
348 
349 /* keysetup.c */
350 static inline int fscrypt_get_encryption_info(struct inode *inode)
351 {
352 	return -EOPNOTSUPP;
353 }
354 
355 static inline void fscrypt_put_encryption_info(struct inode *inode)
356 {
357 	return;
358 }
359 
360 static inline void fscrypt_free_inode(struct inode *inode)
361 {
362 }
363 
364 static inline int fscrypt_drop_inode(struct inode *inode)
365 {
366 	return 0;
367 }
368 
369  /* fname.c */
370 static inline int fscrypt_setup_filename(struct inode *dir,
371 					 const struct qstr *iname,
372 					 int lookup, struct fscrypt_name *fname)
373 {
374 	if (IS_ENCRYPTED(dir))
375 		return -EOPNOTSUPP;
376 
377 	memset(fname, 0, sizeof(*fname));
378 	fname->usr_fname = iname;
379 	fname->disk_name.name = (unsigned char *)iname->name;
380 	fname->disk_name.len = iname->len;
381 	return 0;
382 }
383 
384 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
385 {
386 	return;
387 }
388 
389 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
390 					     u32 max_encrypted_len,
391 					     struct fscrypt_str *crypto_str)
392 {
393 	return -EOPNOTSUPP;
394 }
395 
396 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
397 {
398 	return;
399 }
400 
401 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
402 					    u32 hash, u32 minor_hash,
403 					    const struct fscrypt_str *iname,
404 					    struct fscrypt_str *oname)
405 {
406 	return -EOPNOTSUPP;
407 }
408 
409 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
410 				      const u8 *de_name, u32 de_name_len)
411 {
412 	/* Encryption support disabled; use standard comparison */
413 	if (de_name_len != fname->disk_name.len)
414 		return false;
415 	return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
416 }
417 
418 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
419 					const struct qstr *name)
420 {
421 	WARN_ON_ONCE(1);
422 	return 0;
423 }
424 
425 /* bio.c */
426 static inline void fscrypt_decrypt_bio(struct bio *bio)
427 {
428 }
429 
430 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
431 					sector_t pblk, unsigned int len)
432 {
433 	return -EOPNOTSUPP;
434 }
435 
436 /* hooks.c */
437 
438 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
439 {
440 	if (IS_ENCRYPTED(inode))
441 		return -EOPNOTSUPP;
442 	return 0;
443 }
444 
445 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
446 					 struct dentry *dentry)
447 {
448 	return -EOPNOTSUPP;
449 }
450 
451 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
452 					   struct dentry *old_dentry,
453 					   struct inode *new_dir,
454 					   struct dentry *new_dentry,
455 					   unsigned int flags)
456 {
457 	return -EOPNOTSUPP;
458 }
459 
460 static inline int __fscrypt_prepare_lookup(struct inode *dir,
461 					   struct dentry *dentry,
462 					   struct fscrypt_name *fname)
463 {
464 	return -EOPNOTSUPP;
465 }
466 
467 static inline int fscrypt_prepare_setflags(struct inode *inode,
468 					   unsigned int oldflags,
469 					   unsigned int flags)
470 {
471 	return 0;
472 }
473 
474 static inline int __fscrypt_prepare_symlink(struct inode *dir,
475 					    unsigned int len,
476 					    unsigned int max_len,
477 					    struct fscrypt_str *disk_link)
478 {
479 	return -EOPNOTSUPP;
480 }
481 
482 
483 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
484 					    const char *target,
485 					    unsigned int len,
486 					    struct fscrypt_str *disk_link)
487 {
488 	return -EOPNOTSUPP;
489 }
490 
491 static inline const char *fscrypt_get_symlink(struct inode *inode,
492 					      const void *caddr,
493 					      unsigned int max_size,
494 					      struct delayed_call *done)
495 {
496 	return ERR_PTR(-EOPNOTSUPP);
497 }
498 
499 static inline void fscrypt_set_ops(struct super_block *sb,
500 				   const struct fscrypt_operations *s_cop)
501 {
502 }
503 
504 #endif	/* !CONFIG_FS_ENCRYPTION */
505 
506 /**
507  * fscrypt_require_key - require an inode's encryption key
508  * @inode: the inode we need the key for
509  *
510  * If the inode is encrypted, set up its encryption key if not already done.
511  * Then require that the key be present and return -ENOKEY otherwise.
512  *
513  * No locks are needed, and the key will live as long as the struct inode --- so
514  * it won't go away from under you.
515  *
516  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
517  * if a problem occurred while setting up the encryption key.
518  */
519 static inline int fscrypt_require_key(struct inode *inode)
520 {
521 	if (IS_ENCRYPTED(inode)) {
522 		int err = fscrypt_get_encryption_info(inode);
523 
524 		if (err)
525 			return err;
526 		if (!fscrypt_has_encryption_key(inode))
527 			return -ENOKEY;
528 	}
529 	return 0;
530 }
531 
532 /**
533  * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
534  * @old_dentry: an existing dentry for the inode being linked
535  * @dir: the target directory
536  * @dentry: negative dentry for the target filename
537  *
538  * A new link can only be added to an encrypted directory if the directory's
539  * encryption key is available --- since otherwise we'd have no way to encrypt
540  * the filename.  Therefore, we first set up the directory's encryption key (if
541  * not already done) and return an error if it's unavailable.
542  *
543  * We also verify that the link will not violate the constraint that all files
544  * in an encrypted directory tree use the same encryption policy.
545  *
546  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
547  * -EXDEV if the link would result in an inconsistent encryption policy, or
548  * another -errno code.
549  */
550 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
551 				       struct inode *dir,
552 				       struct dentry *dentry)
553 {
554 	if (IS_ENCRYPTED(dir))
555 		return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
556 	return 0;
557 }
558 
559 /**
560  * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
561  * @old_dir: source directory
562  * @old_dentry: dentry for source file
563  * @new_dir: target directory
564  * @new_dentry: dentry for target location (may be negative unless exchanging)
565  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
566  *
567  * Prepare for ->rename() where the source and/or target directories may be
568  * encrypted.  A new link can only be added to an encrypted directory if the
569  * directory's encryption key is available --- since otherwise we'd have no way
570  * to encrypt the filename.  A rename to an existing name, on the other hand,
571  * *is* cryptographically possible without the key.  However, we take the more
572  * conservative approach and just forbid all no-key renames.
573  *
574  * We also verify that the rename will not violate the constraint that all files
575  * in an encrypted directory tree use the same encryption policy.
576  *
577  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
578  * rename would cause inconsistent encryption policies, or another -errno code.
579  */
580 static inline int fscrypt_prepare_rename(struct inode *old_dir,
581 					 struct dentry *old_dentry,
582 					 struct inode *new_dir,
583 					 struct dentry *new_dentry,
584 					 unsigned int flags)
585 {
586 	if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
587 		return __fscrypt_prepare_rename(old_dir, old_dentry,
588 						new_dir, new_dentry, flags);
589 	return 0;
590 }
591 
592 /**
593  * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
594  * @dir: directory being searched
595  * @dentry: filename being looked up
596  * @fname: (output) the name to use to search the on-disk directory
597  *
598  * Prepare for ->lookup() in a directory which may be encrypted by determining
599  * the name that will actually be used to search the directory on-disk.  Lookups
600  * can be done with or without the directory's encryption key; without the key,
601  * filenames are presented in encrypted form.  Therefore, we'll try to set up
602  * the directory's encryption key, but even without it the lookup can continue.
603  *
604  * This also installs a custom ->d_revalidate() method which will invalidate the
605  * dentry if it was created without the key and the key is later added.
606  *
607  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
608  * correctly formed encoded ciphertext name, so a negative dentry should be
609  * created; or another -errno code.
610  */
611 static inline int fscrypt_prepare_lookup(struct inode *dir,
612 					 struct dentry *dentry,
613 					 struct fscrypt_name *fname)
614 {
615 	if (IS_ENCRYPTED(dir))
616 		return __fscrypt_prepare_lookup(dir, dentry, fname);
617 
618 	memset(fname, 0, sizeof(*fname));
619 	fname->usr_fname = &dentry->d_name;
620 	fname->disk_name.name = (unsigned char *)dentry->d_name.name;
621 	fname->disk_name.len = dentry->d_name.len;
622 	return 0;
623 }
624 
625 /**
626  * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
627  * @dentry: dentry through which the inode is being changed
628  * @attr: attributes to change
629  *
630  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
631  * most attribute changes are allowed even without the encryption key.  However,
632  * without the encryption key we do have to forbid truncates.  This is needed
633  * because the size being truncated to may not be a multiple of the filesystem
634  * block size, and in that case we'd have to decrypt the final block, zero the
635  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
636  * filesystem block boundary, but it's simpler to just forbid all truncates ---
637  * and we already forbid all other contents modifications without the key.)
638  *
639  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
640  * if a problem occurred while setting up the encryption key.
641  */
642 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
643 					  struct iattr *attr)
644 {
645 	if (attr->ia_valid & ATTR_SIZE)
646 		return fscrypt_require_key(d_inode(dentry));
647 	return 0;
648 }
649 
650 /**
651  * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
652  * @dir: directory in which the symlink is being created
653  * @target: plaintext symlink target
654  * @len: length of @target excluding null terminator
655  * @max_len: space the filesystem has available to store the symlink target
656  * @disk_link: (out) the on-disk symlink target being prepared
657  *
658  * This function computes the size the symlink target will require on-disk,
659  * stores it in @disk_link->len, and validates it against @max_len.  An
660  * encrypted symlink may be longer than the original.
661  *
662  * Additionally, @disk_link->name is set to @target if the symlink will be
663  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
664  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
665  * on-disk target later.  (The reason for the two-step process is that some
666  * filesystems need to know the size of the symlink target before creating the
667  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
668  *
669  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
670  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
671  * occurred while setting up the encryption key.
672  */
673 static inline int fscrypt_prepare_symlink(struct inode *dir,
674 					  const char *target,
675 					  unsigned int len,
676 					  unsigned int max_len,
677 					  struct fscrypt_str *disk_link)
678 {
679 	if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
680 		return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
681 
682 	disk_link->name = (unsigned char *)target;
683 	disk_link->len = len + 1;
684 	if (disk_link->len > max_len)
685 		return -ENAMETOOLONG;
686 	return 0;
687 }
688 
689 /**
690  * fscrypt_encrypt_symlink - encrypt the symlink target if needed
691  * @inode: symlink inode
692  * @target: plaintext symlink target
693  * @len: length of @target excluding null terminator
694  * @disk_link: (in/out) the on-disk symlink target being prepared
695  *
696  * If the symlink target needs to be encrypted, then this function encrypts it
697  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
698  * previously to compute @disk_link->len.  If the filesystem did not allocate a
699  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
700  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
701  *
702  * Return: 0 on success, -errno on failure
703  */
704 static inline int fscrypt_encrypt_symlink(struct inode *inode,
705 					  const char *target,
706 					  unsigned int len,
707 					  struct fscrypt_str *disk_link)
708 {
709 	if (IS_ENCRYPTED(inode))
710 		return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
711 	return 0;
712 }
713 
714 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
715 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
716 {
717 	struct page *page = *pagep;
718 
719 	if (fscrypt_is_bounce_page(page)) {
720 		*pagep = fscrypt_pagecache_page(page);
721 		fscrypt_free_bounce_page(page);
722 	}
723 }
724 
725 #endif	/* _LINUX_FSCRYPT_H */
726