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