Whamcloud - gitweb
LU-16374 enc: align Base64 encoding with RFC 4648 base64url
[fs/lustre-release.git] / libcfs / include / libcfs / crypto / llcrypt.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * llcrypt.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 /*
14  * Linux commit 219d54332a09
15  * tags/v5.4
16  */
17 #ifndef _LINUX_LLCRYPT_H
18 #define _LINUX_LLCRYPT_H
19
20 #ifndef DCACHE_ENCRYPTED_NAME
21 #define DCACHE_ENCRYPTED_NAME 0x02000000
22 #endif
23
24 #include <linux/fs.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <lustre_disk.h>
28 #include <uapi/linux/llcrypt.h>
29
30 #define LL_CRYPTO_BLOCK_SIZE            16
31
32 struct llcrypt_ctx;
33 struct llcrypt_info;
34
35 struct llcrypt_str {
36         unsigned char *name;
37         u32 len;
38 };
39
40 struct llcrypt_name {
41         const struct qstr *usr_fname;
42         struct llcrypt_str disk_name;
43         u32 hash;
44         u32 minor_hash;
45         struct llcrypt_str crypto_buf;
46         bool is_ciphertext_name;
47 };
48
49 #define LLTR_INIT(n, l)         { .name = n, .len = l }
50 #define LLTR_TO_QSTR(f)         QSTR_INIT((f)->name, (f)->len)
51 #define lname_name(p)           ((p)->disk_name.name)
52 #define lname_len(p)            ((p)->disk_name.len)
53
54 /* Maximum value for the third parameter of llcrypt_operations.set_context(). */
55 #define LLCRYPT_SET_CONTEXT_MAX_SIZE    40
56 #define LLCRYPT_DIGESTED_CHAR_OLD       '_'
57 #define LLCRYPT_DIGESTED_CHAR           '+'
58
59 #ifdef CONFIG_LL_ENCRYPTION
60 /*
61  * llcrypt superblock flags
62  */
63 #define LL_CFLG_OWN_PAGES (1U << 1)
64
65 /*
66  * crypto operations for filesystems
67  */
68 struct llcrypt_operations {
69         unsigned int flags;
70         const char *key_prefix;
71         int (*get_context)(struct inode *, void *, size_t);
72         int (*set_context)(struct inode *, const void *, size_t, void *);
73         bool (*dummy_context)(struct inode *);
74         bool (*empty_dir)(struct inode *);
75         unsigned int max_namelen;
76 };
77
78 /* Decryption work */
79 struct llcrypt_ctx {
80         union {
81                 struct {
82                         struct bio *bio;
83                         struct work_struct work;
84                 };
85                 struct list_head free_list;     /* Free list */
86         };
87         u8 flags;                               /* Flags */
88 };
89
90 extern bool llcrypt_has_encryption_key(const struct inode *inode);
91
92 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
93 {
94         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
95
96         if (unlikely(!lsi))
97                 return false;
98
99         return lsi->lsi_cop->dummy_context &&
100                 lsi->lsi_cop->dummy_context(inode);
101 }
102
103 /*
104  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
105  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
106  * must be cleared.  Note that we don't have to support arbitrary moves of this
107  * flag because llcrypt doesn't allow encrypted aliases to be the source or
108  * target of a rename().
109  */
110 static inline void llcrypt_handle_d_move(struct dentry *dentry)
111 {
112         dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
113 }
114
115 /* crypto.c */
116 extern int __init llcrypt_init(void);
117 extern void __exit llcrypt_exit(void);
118 extern void llcrypt_enqueue_decrypt_work(struct work_struct *);
119 extern struct llcrypt_ctx *llcrypt_get_ctx(gfp_t);
120 extern void llcrypt_release_ctx(struct llcrypt_ctx *);
121
122 extern struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
123                                                      unsigned int len,
124                                                      unsigned int offs,
125                                                      gfp_t gfp_flags);
126 extern int llcrypt_encrypt_block(const struct inode *inode, struct page *src,
127                          struct page *dst, unsigned int len,
128                          unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
129
130 extern int llcrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
131                                             unsigned int offs);
132
133 extern int llcrypt_decrypt_block(const struct inode *inode, struct page *src,
134                          struct page *dst, unsigned int len,
135                          unsigned int offs, u64 lblk_num, gfp_t gfp_flags);
136
137 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
138                                                 struct page *page,
139                                                 unsigned int len,
140                                                 unsigned int offs,
141                                                 u64 lblk_num)
142 {
143         return llcrypt_decrypt_block(inode, page, page, len, offs, lblk_num,
144                                      GFP_NOFS);
145 }
146
147 static inline bool llcrypt_is_bounce_page(struct page *page)
148 {
149         return page->mapping == NULL;
150 }
151
152 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
153 {
154         return (struct page *)page_private(bounce_page);
155 }
156
157 extern void llcrypt_free_bounce_page(struct page *bounce_page);
158
159 /* policy.c */
160 extern int llcrypt_ioctl_set_policy(struct file *, const void __user *);
161 extern int llcrypt_ioctl_get_policy(struct file *, void __user *);
162 extern int llcrypt_ioctl_get_policy_ex(struct file *, void __user *);
163 extern int llcrypt_has_permitted_context(struct inode *, struct inode *);
164 extern int llcrypt_inherit_context(struct inode *, struct inode *,
165                                         void *, bool);
166 extern bool llcrypt_policy_has_filename_enc(struct inode *inode);
167 /* keyring.c */
168 extern void llcrypt_sb_free(struct super_block *sb);
169 extern int llcrypt_ioctl_add_key(struct file *filp, void __user *arg);
170 extern int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg);
171 extern int llcrypt_ioctl_remove_key_all_users(struct file *filp,
172                                               void __user *arg);
173 extern int llcrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
174
175 /* keysetup.c */
176 extern int llcrypt_get_encryption_info(struct inode *);
177 extern void llcrypt_put_encryption_info(struct inode *);
178 extern void llcrypt_free_inode(struct inode *);
179 extern int llcrypt_drop_inode(struct inode *inode);
180
181 /* fname.c */
182 extern int llcrypt_setup_filename(struct inode *, const struct qstr *,
183                                 int lookup, struct llcrypt_name *);
184
185 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
186 {
187         kfree(fname->crypto_buf.name);
188 }
189
190 extern int llcrypt_fname_alloc_buffer(const struct inode *, u32,
191                                 struct llcrypt_str *);
192 extern void llcrypt_fname_free_buffer(struct llcrypt_str *);
193 extern int llcrypt_fname_disk_to_usr(struct inode *, u32, u32,
194                         const struct llcrypt_str *, struct llcrypt_str *);
195
196 #define LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE       32
197
198 /* Extracts the second-to-last ciphertext block; see explanation below */
199 #define LLCRYPT_FNAME_DIGEST(name, len) \
200         ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1, \
201                              LL_CRYPTO_BLOCK_SIZE))
202
203 #define LLCRYPT_FNAME_DIGEST_SIZE       LL_CRYPTO_BLOCK_SIZE
204
205 /**
206  * llcrypt_digested_name - alternate identifier for an on-disk filename
207  *
208  * When userspace lists an encrypted directory without access to the key,
209  * filenames whose ciphertext is longer than LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE
210  * bytes are shown in this abbreviated form (base64-encoded) rather than as the
211  * full ciphertext (base64-encoded).  This is necessary to allow supporting
212  * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
213  *
214  * To make it possible for filesystems to still find the correct directory entry
215  * despite not knowing the full on-disk name, we encode any filesystem-specific
216  * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
217  * followed by the second-to-last ciphertext block of the filename.  Due to the
218  * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
219  * depends on the full plaintext.  (Note that ciphertext stealing causes the
220  * last two blocks to appear "flipped".)  This makes accidental collisions very
221  * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
222  * share the same filesystem-specific hashes.
223  *
224  * However, this scheme isn't immune to intentional collisions, which can be
225  * created by anyone able to create arbitrary plaintext filenames and view them
226  * without the key.  Making the "digest" be a real cryptographic hash like
227  * SHA-256 over the full ciphertext would prevent this, although it would be
228  * less efficient and harder to implement, especially since the filesystem would
229  * need to calculate it for each directory entry examined during a search.
230  */
231 struct llcrypt_digested_name {
232         u32 hash;
233         u32 minor_hash;
234         u8 digest[LLCRYPT_FNAME_DIGEST_SIZE];
235 };
236
237 /**
238  * llcrypt_match_name() - test whether the given name matches a directory entry
239  * @fname: the name being searched for
240  * @de_name: the name from the directory entry
241  * @de_name_len: the length of @de_name in bytes
242  *
243  * Normally @fname->disk_name will be set, and in that case we simply compare
244  * that to the name stored in the directory entry.  The only exception is that
245  * if we don't have the key for an encrypted directory and a filename in it is
246  * very long, then we won't have the full disk_name and we'll instead need to
247  * match against the llcrypt_digested_name.
248  *
249  * Return: %true if the name matches, otherwise %false.
250  */
251 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
252                                       const u8 *de_name, u32 de_name_len)
253 {
254         if (unlikely(!fname->disk_name.name)) {
255                 const struct llcrypt_digested_name *n =
256                         (const void *)fname->crypto_buf.name;
257                 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
258                         return false;
259                 if (de_name_len <= LLCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
260                         return false;
261                 return !memcmp(LLCRYPT_FNAME_DIGEST(de_name, de_name_len),
262                                n->digest, LLCRYPT_FNAME_DIGEST_SIZE);
263         }
264
265         if (de_name_len != fname->disk_name.len)
266                 return false;
267         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
268 }
269
270 /* bio.c */
271 extern void llcrypt_decrypt_bio(struct bio *);
272 extern void llcrypt_enqueue_decrypt_bio(struct llcrypt_ctx *ctx,
273                                         struct bio *bio);
274 extern int llcrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
275                                  unsigned int);
276
277 /* hooks.c */
278 extern int llcrypt_file_open(struct inode *inode, struct file *filp);
279 extern int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
280                                   struct dentry *dentry);
281 extern int __llcrypt_prepare_rename(struct inode *old_dir,
282                                     struct dentry *old_dentry,
283                                     struct inode *new_dir,
284                                     struct dentry *new_dentry,
285                                     unsigned int flags);
286 extern int __llcrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
287                                     struct llcrypt_name *fname);
288 extern int __llcrypt_prepare_symlink(struct inode *dir, unsigned int len,
289                                      unsigned int max_len,
290                                      struct llcrypt_str *disk_link);
291 extern int __llcrypt_encrypt_symlink(struct inode *inode, const char *target,
292                                      unsigned int len,
293                                      struct llcrypt_str *disk_link);
294 extern const char *llcrypt_get_symlink(struct inode *inode, const void *caddr,
295                                        unsigned int max_size,
296                                        struct delayed_call *done);
297 static inline void llcrypt_set_ops(struct super_block *sb,
298                                    const struct llcrypt_operations *lsi_cop)
299 {
300         struct lustre_sb_info *lsi = s2lsi(sb);
301
302         if (lsi)
303                 lsi->lsi_cop = lsi_cop;
304 }
305 #else  /* !CONFIG_LL_ENCRYPTION */
306
307 struct llcrypt_operations;
308 #define llcrypt_init()         0
309 #define llcrypt_exit()         {}
310
311 #undef IS_ENCRYPTED
312 #define IS_ENCRYPTED(x)        0
313
314 static inline bool llcrypt_has_encryption_key(const struct inode *inode)
315 {
316         return false;
317 }
318
319 static inline bool llcrypt_dummy_context_enabled(struct inode *inode)
320 {
321         return false;
322 }
323
324 static inline void llcrypt_handle_d_move(struct dentry *dentry)
325 {
326 }
327
328 /* crypto.c */
329 static inline void llcrypt_enqueue_decrypt_work(struct work_struct *work)
330 {
331 }
332
333 static inline struct llcrypt_ctx *llcrypt_get_ctx(gfp_t gfp_flags)
334 {
335         return ERR_PTR(-EOPNOTSUPP);
336 }
337
338 static inline void llcrypt_release_ctx(struct llcrypt_ctx *ctx)
339 {
340         return;
341 }
342
343 static inline struct page *llcrypt_encrypt_pagecache_blocks(struct page *page,
344                                                             unsigned int len,
345                                                             unsigned int offs,
346                                                             gfp_t gfp_flags)
347 {
348         return ERR_PTR(-EOPNOTSUPP);
349 }
350
351 static inline int llcrypt_encrypt_block(const struct inode *inode,
352                                         struct page *src, struct page *dst,
353                                         unsigned int len, unsigned int offs,
354                                         u64 lblk_num, gfp_t gfp_flags)
355 {
356         return -EOPNOTSUPP;
357 }
358
359 static inline int llcrypt_decrypt_pagecache_blocks(struct page *page,
360                                                    unsigned int len,
361                                                    unsigned int offs)
362 {
363         return -EOPNOTSUPP;
364 }
365
366 static inline int llcrypt_decrypt_block(const struct inode *inode,
367                                         struct page *src, struct page *dst,
368                                         unsigned int len, unsigned int offs,
369                                         u64 lblk_num, gfp_t gfp_flags)
370 {
371         return -EOPNOTSUPP;
372 }
373
374 static inline int llcrypt_decrypt_block_inplace(const struct inode *inode,
375                                                 struct page *page,
376                                                 unsigned int len,
377                                                 unsigned int offs, u64 lblk_num)
378 {
379         return -EOPNOTSUPP;
380 }
381
382 static inline bool llcrypt_is_bounce_page(struct page *page)
383 {
384         return false;
385 }
386
387 static inline struct page *llcrypt_pagecache_page(struct page *bounce_page)
388 {
389         WARN_ON_ONCE(1);
390         return ERR_PTR(-EINVAL);
391 }
392
393 static inline void llcrypt_free_bounce_page(struct page *bounce_page)
394 {
395 }
396
397 /* policy.c */
398 static inline int llcrypt_ioctl_set_policy(struct file *filp,
399                                            const void __user *arg)
400 {
401         return -EOPNOTSUPP;
402 }
403
404 static inline int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
405 {
406         return -EOPNOTSUPP;
407 }
408
409 static inline int llcrypt_ioctl_get_policy_ex(struct file *filp,
410                                               void __user *arg)
411 {
412         return -EOPNOTSUPP;
413 }
414
415 static inline int llcrypt_has_permitted_context(struct inode *parent,
416                                                 struct inode *child)
417 {
418         return 0;
419 }
420
421 static inline int llcrypt_inherit_context(struct inode *parent,
422                                           struct inode *child,
423                                           void *fs_data, bool preload)
424 {
425         return -EOPNOTSUPP;
426 }
427 static inline bool llcrypt_policy_has_filename_enc(struct inode *inode)
428 {
429         return false;
430 }
431
432 /* keyring.c */
433 static inline void llcrypt_sb_free(struct super_block *sb)
434 {
435 }
436
437 static inline int llcrypt_ioctl_add_key(struct file *filp, void __user *arg)
438 {
439         return -EOPNOTSUPP;
440 }
441
442 static inline int llcrypt_ioctl_remove_key(struct file *filp, void __user *arg)
443 {
444         return -EOPNOTSUPP;
445 }
446
447 static inline int llcrypt_ioctl_remove_key_all_users(struct file *filp,
448                                                      void __user *arg)
449 {
450         return -EOPNOTSUPP;
451 }
452
453 static inline int llcrypt_ioctl_get_key_status(struct file *filp,
454                                                void __user *arg)
455 {
456         return -EOPNOTSUPP;
457 }
458
459 /* keysetup.c */
460 static inline int llcrypt_get_encryption_info(struct inode *inode)
461 {
462         return -EOPNOTSUPP;
463 }
464
465 static inline void llcrypt_put_encryption_info(struct inode *inode)
466 {
467         return;
468 }
469
470 static inline void llcrypt_free_inode(struct inode *inode)
471 {
472 }
473
474 static inline int llcrypt_drop_inode(struct inode *inode)
475 {
476         return 0;
477 }
478
479  /* fname.c */
480 static inline int llcrypt_setup_filename(struct inode *dir,
481                                          const struct qstr *iname,
482                                          int lookup, struct llcrypt_name *fname)
483 {
484         if (IS_ENCRYPTED(dir))
485                 return -EOPNOTSUPP;
486
487         memset(fname, 0, sizeof(*fname));
488         fname->usr_fname = iname;
489         fname->disk_name.name = (unsigned char *)iname->name;
490         fname->disk_name.len = iname->len;
491         return 0;
492 }
493
494 static inline void llcrypt_free_filename(struct llcrypt_name *fname)
495 {
496         return;
497 }
498
499 static inline int llcrypt_fname_alloc_buffer(const struct inode *inode,
500                                              u32 max_encrypted_len,
501                                              struct llcrypt_str *crypto_str)
502 {
503         return -EOPNOTSUPP;
504 }
505
506 static inline void llcrypt_fname_free_buffer(struct llcrypt_str *crypto_str)
507 {
508         return;
509 }
510
511 static inline int llcrypt_fname_disk_to_usr(struct inode *inode,
512                                             u32 hash, u32 minor_hash,
513                                             const struct llcrypt_str *iname,
514                                             struct llcrypt_str *oname)
515 {
516         return -EOPNOTSUPP;
517 }
518
519 static inline bool llcrypt_match_name(const struct llcrypt_name *fname,
520                                       const u8 *de_name, u32 de_name_len)
521 {
522         /* Encryption support disabled; use standard comparison */
523         if (de_name_len != fname->disk_name.len)
524                 return false;
525         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
526 }
527
528 /* bio.c */
529 static inline void llcrypt_decrypt_bio(struct bio *bio)
530 {
531 }
532
533 static inline void llcrypt_enqueue_decrypt_bio(struct llcrypt_ctx *ctx,
534                                                struct bio *bio)
535 {
536 }
537
538 static inline int llcrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
539                                         sector_t pblk, unsigned int len)
540 {
541         return -EOPNOTSUPP;
542 }
543
544 /* hooks.c */
545
546 static inline int llcrypt_file_open(struct inode *inode, struct file *filp)
547 {
548         if (IS_ENCRYPTED(inode))
549                 return -EOPNOTSUPP;
550         return 0;
551 }
552
553 static inline int __llcrypt_prepare_link(struct inode *inode, struct inode *dir,
554                                          struct dentry *dentry)
555 {
556         return -EOPNOTSUPP;
557 }
558
559 static inline int __llcrypt_prepare_rename(struct inode *old_dir,
560                                            struct dentry *old_dentry,
561                                            struct inode *new_dir,
562                                            struct dentry *new_dentry,
563                                            unsigned int flags)
564 {
565         return -EOPNOTSUPP;
566 }
567
568 static inline int __llcrypt_prepare_lookup(struct inode *dir,
569                                            struct dentry *dentry,
570                                            struct llcrypt_name *fname)
571 {
572         return -EOPNOTSUPP;
573 }
574
575 static inline int __llcrypt_prepare_symlink(struct inode *dir,
576                                             unsigned int len,
577                                             unsigned int max_len,
578                                             struct llcrypt_str *disk_link)
579 {
580         return -EOPNOTSUPP;
581 }
582
583
584 static inline int __llcrypt_encrypt_symlink(struct inode *inode,
585                                             const char *target,
586                                             unsigned int len,
587                                             struct llcrypt_str *disk_link)
588 {
589         return -EOPNOTSUPP;
590 }
591
592 #define llcrypt_get_symlink(inode, caddr, max_size, done)   ERR_PTR(-EOPNOTSUPP)
593
594 static inline void llcrypt_set_ops(struct super_block *sb,
595                                    const struct llcrypt_operations *lsi_cop)
596 {
597 }
598
599 #endif  /* !CONFIG_LL_ENCRYPTION */
600
601 /**
602  * llcrypt_require_key - require an inode's encryption key
603  * @inode: the inode we need the key for
604  *
605  * If the inode is encrypted, set up its encryption key if not already done.
606  * Then require that the key be present and return -ENOKEY otherwise.
607  *
608  * No locks are needed, and the key will live as long as the struct inode --- so
609  * it won't go away from under you.
610  *
611  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
612  * if a problem occurred while setting up the encryption key.
613  */
614 static inline int llcrypt_require_key(struct inode *inode)
615 {
616         if (IS_ENCRYPTED(inode)) {
617                 int err = llcrypt_get_encryption_info(inode);
618
619                 if (err)
620                         return err;
621                 if (!llcrypt_has_encryption_key(inode))
622                         return -ENOKEY;
623         }
624         return 0;
625 }
626
627 /**
628  * llcrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
629  * @old_dentry: an existing dentry for the inode being linked
630  * @dir: the target directory
631  * @dentry: negative dentry for the target filename
632  *
633  * A new link can only be added to an encrypted directory if the directory's
634  * encryption key is available --- since otherwise we'd have no way to encrypt
635  * the filename.  Therefore, we first set up the directory's encryption key (if
636  * not already done) and return an error if it's unavailable.
637  *
638  * We also verify that the link will not violate the constraint that all files
639  * in an encrypted directory tree use the same encryption policy.
640  *
641  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
642  * -EXDEV if the link would result in an inconsistent encryption policy, or
643  * another -errno code.
644  */
645 static inline int llcrypt_prepare_link(struct dentry *old_dentry,
646                                        struct inode *dir,
647                                        struct dentry *dentry)
648 {
649         if (IS_ENCRYPTED(dir))
650                 return __llcrypt_prepare_link(d_inode(old_dentry), dir, dentry);
651         return 0;
652 }
653
654 /**
655  * llcrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
656  * @old_dir: source directory
657  * @old_dentry: dentry for source file
658  * @new_dir: target directory
659  * @new_dentry: dentry for target location (may be negative unless exchanging)
660  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
661  *
662  * Prepare for ->rename() where the source and/or target directories may be
663  * encrypted.  A new link can only be added to an encrypted directory if the
664  * directory's encryption key is available --- since otherwise we'd have no way
665  * to encrypt the filename.  A rename to an existing name, on the other hand,
666  * *is* cryptographically possible without the key.  However, we take the more
667  * conservative approach and just forbid all no-key renames.
668  *
669  * We also verify that the rename will not violate the constraint that all files
670  * in an encrypted directory tree use the same encryption policy.
671  *
672  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
673  * rename would cause inconsistent encryption policies, or another -errno code.
674  */
675 static inline int llcrypt_prepare_rename(struct inode *old_dir,
676                                          struct dentry *old_dentry,
677                                          struct inode *new_dir,
678                                          struct dentry *new_dentry,
679                                          unsigned int flags)
680 {
681         if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
682                 return __llcrypt_prepare_rename(old_dir, old_dentry,
683                                                 new_dir, new_dentry, flags);
684         return 0;
685 }
686
687 /**
688  * llcrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
689  * @dir: directory being searched
690  * @dentry: filename being looked up
691  * @fname: (output) the name to use to search the on-disk directory
692  *
693  * Prepare for ->lookup() in a directory which may be encrypted by determining
694  * the name that will actually be used to search the directory on-disk.  Lookups
695  * can be done with or without the directory's encryption key; without the key,
696  * filenames are presented in encrypted form.  Therefore, we'll try to set up
697  * the directory's encryption key, but even without it the lookup can continue.
698  *
699  * This also installs a custom ->d_revalidate() method which will invalidate the
700  * dentry if it was created without the key and the key is later added.
701  *
702  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
703  * correctly formed encoded ciphertext name, so a negative dentry should be
704  * created; or another -errno code.
705  */
706 static inline int llcrypt_prepare_lookup(struct inode *dir,
707                                          struct dentry *dentry,
708                                          struct llcrypt_name *fname)
709 {
710         if (IS_ENCRYPTED(dir))
711                 return __llcrypt_prepare_lookup(dir, dentry, fname);
712
713         memset(fname, 0, sizeof(*fname));
714         fname->usr_fname = &dentry->d_name;
715         fname->disk_name.name = (unsigned char *)dentry->d_name.name;
716         fname->disk_name.len = dentry->d_name.len;
717         return 0;
718 }
719
720 /**
721  * llcrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
722  * @dentry: dentry through which the inode is being changed
723  * @attr: attributes to change
724  *
725  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
726  * most attribute changes are allowed even without the encryption key.  However,
727  * without the encryption key we do have to forbid truncates.  This is needed
728  * because the size being truncated to may not be a multiple of the filesystem
729  * block size, and in that case we'd have to decrypt the final block, zero the
730  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
731  * filesystem block boundary, but it's simpler to just forbid all truncates ---
732  * and we already forbid all other contents modifications without the key.)
733  *
734  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
735  * if a problem occurred while setting up the encryption key.
736  */
737 static inline int llcrypt_prepare_setattr(struct dentry *dentry,
738                                           struct iattr *attr)
739 {
740         if (attr->ia_valid & ATTR_SIZE)
741                 return llcrypt_require_key(d_inode(dentry));
742         return 0;
743 }
744
745 /**
746  * llcrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
747  * @dir: directory in which the symlink is being created
748  * @target: plaintext symlink target
749  * @len: length of @target excluding null terminator
750  * @max_len: space the filesystem has available to store the symlink target
751  * @disk_link: (out) the on-disk symlink target being prepared
752  *
753  * This function computes the size the symlink target will require on-disk,
754  * stores it in @disk_link->len, and validates it against @max_len.  An
755  * encrypted symlink may be longer than the original.
756  *
757  * Additionally, @disk_link->name is set to @target if the symlink will be
758  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
759  * symlinks, the filesystem must call llcrypt_encrypt_symlink() to create the
760  * on-disk target later.  (The reason for the two-step process is that some
761  * filesystems need to know the size of the symlink target before creating the
762  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
763  *
764  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
765  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
766  * occurred while setting up the encryption key.
767  */
768 static inline int llcrypt_prepare_symlink(struct inode *dir,
769                                           const char *target,
770                                           unsigned int len,
771                                           unsigned int max_len,
772                                           struct llcrypt_str *disk_link)
773 {
774         if ((IS_ENCRYPTED(dir) || llcrypt_dummy_context_enabled(dir)) &&
775             llcrypt_policy_has_filename_enc(dir))
776                 return __llcrypt_prepare_symlink(dir, len, max_len, disk_link);
777
778         disk_link->name = (unsigned char *)target;
779         disk_link->len = len + 1;
780         if (disk_link->len > max_len)
781                 return -ENAMETOOLONG;
782         return 0;
783 }
784
785 /**
786  * llcrypt_encrypt_symlink - encrypt the symlink target if needed
787  * @inode: symlink inode
788  * @target: plaintext symlink target
789  * @len: length of @target excluding null terminator
790  * @disk_link: (in/out) the on-disk symlink target being prepared
791  *
792  * If the symlink target needs to be encrypted, then this function encrypts it
793  * into @disk_link->name.  llcrypt_prepare_symlink() must have been called
794  * previously to compute @disk_link->len.  If the filesystem did not allocate a
795  * buffer for @disk_link->name after calling llcrypt_prepare_link(), then one
796  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
797  *
798  * Return: 0 on success, -errno on failure
799  */
800 static inline int llcrypt_encrypt_symlink(struct inode *inode,
801                                           const char *target,
802                                           unsigned int len,
803                                           struct llcrypt_str *disk_link)
804 {
805         if (IS_ENCRYPTED(inode))
806                 return __llcrypt_encrypt_symlink(inode, target, len, disk_link);
807         return 0;
808 }
809
810 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
811 static inline void llcrypt_finalize_bounce_page(struct page **pagep)
812 {
813         struct page *page = *pagep;
814
815         if (llcrypt_is_bounce_page(page)) {
816                 *pagep = llcrypt_pagecache_page(page);
817                 llcrypt_free_bounce_page(page);
818         }
819 }
820
821 #endif  /* _LINUX_LLCRYPT_H */