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