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