add patch migrate-into-vfs-crypto-engine
[ext4-patch-queue.git] / migrate-into-vfs-crypto-engine
blobe9ffad4022ce5e9ad8705b02130db7f432327026
1 ext4 crypto: migrate into vfs's crypto engine
3 From: Jaegeuk Kim <jaegeuk@kernel.org>
5 This patch removes the most parts of internal crypto codes.
6 And then, it modifies and adds some ext4-specific crypt codes to use the generic
7 facility.
9 Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
10 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
11 diff --git a/fs/ext4/Kconfig b/fs/ext4/Kconfig
12 index b46e9fc..e38039f 100644
13 --- a/fs/ext4/Kconfig
14 +++ b/fs/ext4/Kconfig
15 @@ -99,17 +99,9 @@ config EXT4_FS_SECURITY
16           extended attributes for file security labels, say N.
18  config EXT4_ENCRYPTION
19 -       tristate "Ext4 Encryption"
20 +       bool "Ext4 Encryption"
21         depends on EXT4_FS
22 -       select CRYPTO_AES
23 -       select CRYPTO_CBC
24 -       select CRYPTO_ECB
25 -       select CRYPTO_XTS
26 -       select CRYPTO_CTS
27 -       select CRYPTO_CTR
28 -       select CRYPTO_SHA256
29 -       select KEYS
30 -       select ENCRYPTED_KEYS
31 +       select FS_ENCRYPTION
32         help
33           Enable encryption of ext4 files and directories.  This
34           feature is similar to ecryptfs, but it is more memory
35 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
36 index f52cf54..354103f 100644
37 --- a/fs/ext4/Makefile
38 +++ b/fs/ext4/Makefile
39 @@ -12,5 +12,3 @@ ext4-y        := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
41  ext4-$(CONFIG_EXT4_FS_POSIX_ACL)       += acl.o
42  ext4-$(CONFIG_EXT4_FS_SECURITY)                += xattr_security.o
43 -ext4-$(CONFIG_EXT4_FS_ENCRYPTION)      += crypto_policy.o crypto.o \
44 -               crypto_key.o crypto_fname.o
45 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
46 deleted file mode 100644
47 index 6a6c273..0000000
48 --- a/fs/ext4/crypto.c
49 +++ /dev/null
50 @@ -1,536 +0,0 @@
51 -/*
52 - * linux/fs/ext4/crypto.c
53 - *
54 - * Copyright (C) 2015, Google, Inc.
55 - *
56 - * This contains encryption functions for ext4
57 - *
58 - * Written by Michael Halcrow, 2014.
59 - *
60 - * Filename encryption additions
61 - *     Uday Savagaonkar, 2014
62 - * Encryption policy handling additions
63 - *     Ildar Muslukhov, 2014
64 - *
65 - * This has not yet undergone a rigorous security audit.
66 - *
67 - * The usage of AES-XTS should conform to recommendations in NIST
68 - * Special Publication 800-38E and IEEE P1619/D16.
69 - */
71 -#include <crypto/skcipher.h>
72 -#include <keys/user-type.h>
73 -#include <keys/encrypted-type.h>
74 -#include <linux/ecryptfs.h>
75 -#include <linux/gfp.h>
76 -#include <linux/kernel.h>
77 -#include <linux/key.h>
78 -#include <linux/list.h>
79 -#include <linux/mempool.h>
80 -#include <linux/module.h>
81 -#include <linux/mutex.h>
82 -#include <linux/random.h>
83 -#include <linux/scatterlist.h>
84 -#include <linux/spinlock_types.h>
85 -#include <linux/namei.h>
87 -#include "ext4_extents.h"
88 -#include "xattr.h"
90 -/* Encryption added and removed here! (L: */
92 -static unsigned int num_prealloc_crypto_pages = 32;
93 -static unsigned int num_prealloc_crypto_ctxs = 128;
95 -module_param(num_prealloc_crypto_pages, uint, 0444);
96 -MODULE_PARM_DESC(num_prealloc_crypto_pages,
97 -                "Number of crypto pages to preallocate");
98 -module_param(num_prealloc_crypto_ctxs, uint, 0444);
99 -MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
100 -                "Number of crypto contexts to preallocate");
102 -static mempool_t *ext4_bounce_page_pool;
104 -static LIST_HEAD(ext4_free_crypto_ctxs);
105 -static DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
107 -static struct kmem_cache *ext4_crypto_ctx_cachep;
108 -struct kmem_cache *ext4_crypt_info_cachep;
110 -/**
111 - * ext4_release_crypto_ctx() - Releases an encryption context
112 - * @ctx: The encryption context to release.
113 - *
114 - * If the encryption context was allocated from the pre-allocated pool, returns
115 - * it to that pool. Else, frees it.
116 - *
117 - * If there's a bounce page in the context, this frees that.
118 - */
119 -void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
121 -       unsigned long flags;
123 -       if (ctx->flags & EXT4_WRITE_PATH_FL && ctx->w.bounce_page)
124 -               mempool_free(ctx->w.bounce_page, ext4_bounce_page_pool);
125 -       ctx->w.bounce_page = NULL;
126 -       ctx->w.control_page = NULL;
127 -       if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
128 -               kmem_cache_free(ext4_crypto_ctx_cachep, ctx);
129 -       } else {
130 -               spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
131 -               list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
132 -               spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
133 -       }
136 -/**
137 - * ext4_get_crypto_ctx() - Gets an encryption context
138 - * @inode:       The inode for which we are doing the crypto
139 - *
140 - * Allocates and initializes an encryption context.
141 - *
142 - * Return: An allocated and initialized encryption context on success; error
143 - * value or NULL otherwise.
144 - */
145 -struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
146 -                                           gfp_t gfp_flags)
148 -       struct ext4_crypto_ctx *ctx = NULL;
149 -       int res = 0;
150 -       unsigned long flags;
151 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
153 -       if (ci == NULL)
154 -               return ERR_PTR(-ENOKEY);
156 -       /*
157 -        * We first try getting the ctx from a free list because in
158 -        * the common case the ctx will have an allocated and
159 -        * initialized crypto tfm, so it's probably a worthwhile
160 -        * optimization. For the bounce page, we first try getting it
161 -        * from the kernel allocator because that's just about as fast
162 -        * as getting it from a list and because a cache of free pages
163 -        * should generally be a "last resort" option for a filesystem
164 -        * to be able to do its job.
165 -        */
166 -       spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
167 -       ctx = list_first_entry_or_null(&ext4_free_crypto_ctxs,
168 -                                      struct ext4_crypto_ctx, free_list);
169 -       if (ctx)
170 -               list_del(&ctx->free_list);
171 -       spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
172 -       if (!ctx) {
173 -               ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, gfp_flags);
174 -               if (!ctx) {
175 -                       res = -ENOMEM;
176 -                       goto out;
177 -               }
178 -               ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
179 -       } else {
180 -               ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
181 -       }
182 -       ctx->flags &= ~EXT4_WRITE_PATH_FL;
184 -out:
185 -       if (res) {
186 -               if (!IS_ERR_OR_NULL(ctx))
187 -                       ext4_release_crypto_ctx(ctx);
188 -               ctx = ERR_PTR(res);
189 -       }
190 -       return ctx;
193 -struct workqueue_struct *ext4_read_workqueue;
194 -static DEFINE_MUTEX(crypto_init);
196 -/**
197 - * ext4_exit_crypto() - Shutdown the ext4 encryption system
198 - */
199 -void ext4_exit_crypto(void)
201 -       struct ext4_crypto_ctx *pos, *n;
203 -       list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list)
204 -               kmem_cache_free(ext4_crypto_ctx_cachep, pos);
205 -       INIT_LIST_HEAD(&ext4_free_crypto_ctxs);
206 -       if (ext4_bounce_page_pool)
207 -               mempool_destroy(ext4_bounce_page_pool);
208 -       ext4_bounce_page_pool = NULL;
209 -       if (ext4_read_workqueue)
210 -               destroy_workqueue(ext4_read_workqueue);
211 -       ext4_read_workqueue = NULL;
212 -       if (ext4_crypto_ctx_cachep)
213 -               kmem_cache_destroy(ext4_crypto_ctx_cachep);
214 -       ext4_crypto_ctx_cachep = NULL;
215 -       if (ext4_crypt_info_cachep)
216 -               kmem_cache_destroy(ext4_crypt_info_cachep);
217 -       ext4_crypt_info_cachep = NULL;
220 -/**
221 - * ext4_init_crypto() - Set up for ext4 encryption.
222 - *
223 - * We only call this when we start accessing encrypted files, since it
224 - * results in memory getting allocated that wouldn't otherwise be used.
225 - *
226 - * Return: Zero on success, non-zero otherwise.
227 - */
228 -int ext4_init_crypto(void)
230 -       int i, res = -ENOMEM;
232 -       mutex_lock(&crypto_init);
233 -       if (ext4_read_workqueue)
234 -               goto already_initialized;
235 -       ext4_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
236 -       if (!ext4_read_workqueue)
237 -               goto fail;
239 -       ext4_crypto_ctx_cachep = KMEM_CACHE(ext4_crypto_ctx,
240 -                                           SLAB_RECLAIM_ACCOUNT);
241 -       if (!ext4_crypto_ctx_cachep)
242 -               goto fail;
244 -       ext4_crypt_info_cachep = KMEM_CACHE(ext4_crypt_info,
245 -                                           SLAB_RECLAIM_ACCOUNT);
246 -       if (!ext4_crypt_info_cachep)
247 -               goto fail;
249 -       for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
250 -               struct ext4_crypto_ctx *ctx;
252 -               ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
253 -               if (!ctx) {
254 -                       res = -ENOMEM;
255 -                       goto fail;
256 -               }
257 -               list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
258 -       }
260 -       ext4_bounce_page_pool =
261 -               mempool_create_page_pool(num_prealloc_crypto_pages, 0);
262 -       if (!ext4_bounce_page_pool) {
263 -               res = -ENOMEM;
264 -               goto fail;
265 -       }
266 -already_initialized:
267 -       mutex_unlock(&crypto_init);
268 -       return 0;
269 -fail:
270 -       ext4_exit_crypto();
271 -       mutex_unlock(&crypto_init);
272 -       return res;
275 -void ext4_restore_control_page(struct page *data_page)
277 -       struct ext4_crypto_ctx *ctx =
278 -               (struct ext4_crypto_ctx *)page_private(data_page);
280 -       set_page_private(data_page, (unsigned long)NULL);
281 -       ClearPagePrivate(data_page);
282 -       unlock_page(data_page);
283 -       ext4_release_crypto_ctx(ctx);
286 -/**
287 - * ext4_crypt_complete() - The completion callback for page encryption
288 - * @req: The asynchronous encryption request context
289 - * @res: The result of the encryption operation
290 - */
291 -static void ext4_crypt_complete(struct crypto_async_request *req, int res)
293 -       struct ext4_completion_result *ecr = req->data;
295 -       if (res == -EINPROGRESS)
296 -               return;
297 -       ecr->res = res;
298 -       complete(&ecr->completion);
301 -typedef enum {
302 -       EXT4_DECRYPT = 0,
303 -       EXT4_ENCRYPT,
304 -} ext4_direction_t;
306 -static int ext4_page_crypto(struct inode *inode,
307 -                           ext4_direction_t rw,
308 -                           pgoff_t index,
309 -                           struct page *src_page,
310 -                           struct page *dest_page,
311 -                           gfp_t gfp_flags)
314 -       u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
315 -       struct skcipher_request *req = NULL;
316 -       DECLARE_EXT4_COMPLETION_RESULT(ecr);
317 -       struct scatterlist dst, src;
318 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
319 -       struct crypto_skcipher *tfm = ci->ci_ctfm;
320 -       int res = 0;
322 -       req = skcipher_request_alloc(tfm, gfp_flags);
323 -       if (!req) {
324 -               printk_ratelimited(KERN_ERR
325 -                                  "%s: crypto_request_alloc() failed\n",
326 -                                  __func__);
327 -               return -ENOMEM;
328 -       }
329 -       skcipher_request_set_callback(
330 -               req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
331 -               ext4_crypt_complete, &ecr);
333 -       BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE < sizeof(index));
334 -       memcpy(xts_tweak, &index, sizeof(index));
335 -       memset(&xts_tweak[sizeof(index)], 0,
336 -              EXT4_XTS_TWEAK_SIZE - sizeof(index));
338 -       sg_init_table(&dst, 1);
339 -       sg_set_page(&dst, dest_page, PAGE_SIZE, 0);
340 -       sg_init_table(&src, 1);
341 -       sg_set_page(&src, src_page, PAGE_SIZE, 0);
342 -       skcipher_request_set_crypt(req, &src, &dst, PAGE_SIZE,
343 -                                  xts_tweak);
344 -       if (rw == EXT4_DECRYPT)
345 -               res = crypto_skcipher_decrypt(req);
346 -       else
347 -               res = crypto_skcipher_encrypt(req);
348 -       if (res == -EINPROGRESS || res == -EBUSY) {
349 -               wait_for_completion(&ecr.completion);
350 -               res = ecr.res;
351 -       }
352 -       skcipher_request_free(req);
353 -       if (res) {
354 -               printk_ratelimited(
355 -                       KERN_ERR
356 -                       "%s: crypto_skcipher_encrypt() returned %d\n",
357 -                       __func__, res);
358 -               return res;
359 -       }
360 -       return 0;
363 -static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx,
364 -                                     gfp_t gfp_flags)
366 -       ctx->w.bounce_page = mempool_alloc(ext4_bounce_page_pool, gfp_flags);
367 -       if (ctx->w.bounce_page == NULL)
368 -               return ERR_PTR(-ENOMEM);
369 -       ctx->flags |= EXT4_WRITE_PATH_FL;
370 -       return ctx->w.bounce_page;
373 -/**
374 - * ext4_encrypt() - Encrypts a page
375 - * @inode:          The inode for which the encryption should take place
376 - * @plaintext_page: The page to encrypt. Must be locked.
377 - *
378 - * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
379 - * encryption context.
380 - *
381 - * Called on the page write path.  The caller must call
382 - * ext4_restore_control_page() on the returned ciphertext page to
383 - * release the bounce buffer and the encryption context.
384 - *
385 - * Return: An allocated page with the encrypted content on success. Else, an
386 - * error value or NULL.
387 - */
388 -struct page *ext4_encrypt(struct inode *inode,
389 -                         struct page *plaintext_page,
390 -                         gfp_t gfp_flags)
392 -       struct ext4_crypto_ctx *ctx;
393 -       struct page *ciphertext_page = NULL;
394 -       int err;
396 -       BUG_ON(!PageLocked(plaintext_page));
398 -       ctx = ext4_get_crypto_ctx(inode, gfp_flags);
399 -       if (IS_ERR(ctx))
400 -               return (struct page *) ctx;
402 -       /* The encryption operation will require a bounce page. */
403 -       ciphertext_page = alloc_bounce_page(ctx, gfp_flags);
404 -       if (IS_ERR(ciphertext_page))
405 -               goto errout;
406 -       ctx->w.control_page = plaintext_page;
407 -       err = ext4_page_crypto(inode, EXT4_ENCRYPT, plaintext_page->index,
408 -                              plaintext_page, ciphertext_page, gfp_flags);
409 -       if (err) {
410 -               ciphertext_page = ERR_PTR(err);
411 -       errout:
412 -               ext4_release_crypto_ctx(ctx);
413 -               return ciphertext_page;
414 -       }
415 -       SetPagePrivate(ciphertext_page);
416 -       set_page_private(ciphertext_page, (unsigned long)ctx);
417 -       lock_page(ciphertext_page);
418 -       return ciphertext_page;
421 -/**
422 - * ext4_decrypt() - Decrypts a page in-place
423 - * @ctx:  The encryption context.
424 - * @page: The page to decrypt. Must be locked.
425 - *
426 - * Decrypts page in-place using the ctx encryption context.
427 - *
428 - * Called from the read completion callback.
429 - *
430 - * Return: Zero on success, non-zero otherwise.
431 - */
432 -int ext4_decrypt(struct page *page)
434 -       BUG_ON(!PageLocked(page));
436 -       return ext4_page_crypto(page->mapping->host, EXT4_DECRYPT,
437 -                               page->index, page, page, GFP_NOFS);
440 -int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
441 -                          ext4_fsblk_t pblk, ext4_lblk_t len)
443 -       struct ext4_crypto_ctx  *ctx;
444 -       struct page             *ciphertext_page = NULL;
445 -       struct bio              *bio;
446 -       int                     ret, err = 0;
448 -#if 0
449 -       ext4_msg(inode->i_sb, KERN_CRIT,
450 -                "ext4_encrypted_zeroout ino %lu lblk %u len %u",
451 -                (unsigned long) inode->i_ino, lblk, len);
452 -#endif
454 -       BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
456 -       ctx = ext4_get_crypto_ctx(inode, GFP_NOFS);
457 -       if (IS_ERR(ctx))
458 -               return PTR_ERR(ctx);
460 -       ciphertext_page = alloc_bounce_page(ctx, GFP_NOWAIT);
461 -       if (IS_ERR(ciphertext_page)) {
462 -               err = PTR_ERR(ciphertext_page);
463 -               goto errout;
464 -       }
466 -       while (len--) {
467 -               err = ext4_page_crypto(inode, EXT4_ENCRYPT, lblk,
468 -                                      ZERO_PAGE(0), ciphertext_page,
469 -                                      GFP_NOFS);
470 -               if (err)
471 -                       goto errout;
473 -               bio = bio_alloc(GFP_NOWAIT, 1);
474 -               if (!bio) {
475 -                       err = -ENOMEM;
476 -                       goto errout;
477 -               }
478 -               bio->bi_bdev = inode->i_sb->s_bdev;
479 -               bio->bi_iter.bi_sector =
480 -                       pblk << (inode->i_sb->s_blocksize_bits - 9);
481 -               ret = bio_add_page(bio, ciphertext_page,
482 -                                  inode->i_sb->s_blocksize, 0);
483 -               if (ret != inode->i_sb->s_blocksize) {
484 -                       /* should never happen! */
485 -                       ext4_msg(inode->i_sb, KERN_ERR,
486 -                                "bio_add_page failed: %d", ret);
487 -                       WARN_ON(1);
488 -                       bio_put(bio);
489 -                       err = -EIO;
490 -                       goto errout;
491 -               }
492 -               err = submit_bio_wait(WRITE, bio);
493 -               if ((err == 0) && bio->bi_error)
494 -                       err = -EIO;
495 -               bio_put(bio);
496 -               if (err)
497 -                       goto errout;
498 -               lblk++; pblk++;
499 -       }
500 -       err = 0;
501 -errout:
502 -       ext4_release_crypto_ctx(ctx);
503 -       return err;
506 -bool ext4_valid_contents_enc_mode(uint32_t mode)
508 -       return (mode == EXT4_ENCRYPTION_MODE_AES_256_XTS);
511 -/**
512 - * ext4_validate_encryption_key_size() - Validate the encryption key size
513 - * @mode: The key mode.
514 - * @size: The key size to validate.
515 - *
516 - * Return: The validated key size for @mode. Zero if invalid.
517 - */
518 -uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
520 -       if (size == ext4_encryption_key_size(mode))
521 -               return size;
522 -       return 0;
526 - * Validate dentries for encrypted directories to make sure we aren't
527 - * potentially caching stale data after a key has been added or
528 - * removed.
529 - */
530 -static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
532 -       struct dentry *dir;
533 -       struct ext4_crypt_info *ci;
534 -       int dir_has_key, cached_with_key;
536 -       if (flags & LOOKUP_RCU)
537 -               return -ECHILD;
539 -       dir = dget_parent(dentry);
540 -       if (!ext4_encrypted_inode(d_inode(dir))) {
541 -               dput(dir);
542 -               return 0;
543 -       }
544 -       ci = EXT4_I(d_inode(dir))->i_crypt_info;
545 -       if (ci && ci->ci_keyring_key &&
546 -           (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
547 -                                         (1 << KEY_FLAG_REVOKED) |
548 -                                         (1 << KEY_FLAG_DEAD))))
549 -               ci = NULL;
551 -       /* this should eventually be an flag in d_flags */
552 -       cached_with_key = dentry->d_fsdata != NULL;
553 -       dir_has_key = (ci != NULL);
554 -       dput(dir);
556 -       /*
557 -        * If the dentry was cached without the key, and it is a
558 -        * negative dentry, it might be a valid name.  We can't check
559 -        * if the key has since been made available due to locking
560 -        * reasons, so we fail the validation so ext4_lookup() can do
561 -        * this check.
562 -        *
563 -        * We also fail the validation if the dentry was created with
564 -        * the key present, but we no longer have the key, or vice versa.
565 -        */
566 -       if ((!cached_with_key && d_is_negative(dentry)) ||
567 -           (!cached_with_key && dir_has_key) ||
568 -           (cached_with_key && !dir_has_key)) {
569 -#if 0                          /* Revalidation debug */
570 -               char buf[80];
571 -               char *cp = simple_dname(dentry, buf, sizeof(buf));
573 -               if (IS_ERR(cp))
574 -                       cp = (char *) "???";
575 -               pr_err("revalidate: %s %p %d %d %d\n", cp, dentry->d_fsdata,
576 -                      cached_with_key, d_is_negative(dentry),
577 -                      dir_has_key);
578 -#endif
579 -               return 0;
580 -       }
581 -       return 1;
584 -const struct dentry_operations ext4_encrypted_d_ops = {
585 -       .d_revalidate = ext4_d_revalidate,
587 diff --git a/fs/ext4/crypto_fname.c b/fs/ext4/crypto_fname.c
588 deleted file mode 100644
589 index 1a2f360..0000000
590 --- a/fs/ext4/crypto_fname.c
591 +++ /dev/null
592 @@ -1,468 +0,0 @@
594 - * linux/fs/ext4/crypto_fname.c
595 - *
596 - * Copyright (C) 2015, Google, Inc.
597 - *
598 - * This contains functions for filename crypto management in ext4
599 - *
600 - * Written by Uday Savagaonkar, 2014.
601 - *
602 - * This has not yet undergone a rigorous security audit.
603 - *
604 - */
606 -#include <crypto/skcipher.h>
607 -#include <keys/encrypted-type.h>
608 -#include <keys/user-type.h>
609 -#include <linux/gfp.h>
610 -#include <linux/kernel.h>
611 -#include <linux/key.h>
612 -#include <linux/list.h>
613 -#include <linux/mempool.h>
614 -#include <linux/random.h>
615 -#include <linux/scatterlist.h>
616 -#include <linux/spinlock_types.h>
618 -#include "ext4.h"
619 -#include "ext4_crypto.h"
620 -#include "xattr.h"
622 -/**
623 - * ext4_dir_crypt_complete() -
624 - */
625 -static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
627 -       struct ext4_completion_result *ecr = req->data;
629 -       if (res == -EINPROGRESS)
630 -               return;
631 -       ecr->res = res;
632 -       complete(&ecr->completion);
635 -bool ext4_valid_filenames_enc_mode(uint32_t mode)
637 -       return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
640 -static unsigned max_name_len(struct inode *inode)
642 -       return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
643 -               EXT4_NAME_LEN;
646 -/**
647 - * ext4_fname_encrypt() -
648 - *
649 - * This function encrypts the input filename, and returns the length of the
650 - * ciphertext. Errors are returned as negative numbers.  We trust the caller to
651 - * allocate sufficient memory to oname string.
652 - */
653 -static int ext4_fname_encrypt(struct inode *inode,
654 -                             const struct qstr *iname,
655 -                             struct ext4_str *oname)
657 -       u32 ciphertext_len;
658 -       struct skcipher_request *req = NULL;
659 -       DECLARE_EXT4_COMPLETION_RESULT(ecr);
660 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
661 -       struct crypto_skcipher *tfm = ci->ci_ctfm;
662 -       int res = 0;
663 -       char iv[EXT4_CRYPTO_BLOCK_SIZE];
664 -       struct scatterlist src_sg, dst_sg;
665 -       int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
666 -       char *workbuf, buf[32], *alloc_buf = NULL;
667 -       unsigned lim = max_name_len(inode);
669 -       if (iname->len <= 0 || iname->len > lim)
670 -               return -EIO;
672 -       ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
673 -               EXT4_CRYPTO_BLOCK_SIZE : iname->len;
674 -       ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
675 -       ciphertext_len = (ciphertext_len > lim)
676 -                       ? lim : ciphertext_len;
678 -       if (ciphertext_len <= sizeof(buf)) {
679 -               workbuf = buf;
680 -       } else {
681 -               alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
682 -               if (!alloc_buf)
683 -                       return -ENOMEM;
684 -               workbuf = alloc_buf;
685 -       }
687 -       /* Allocate request */
688 -       req = skcipher_request_alloc(tfm, GFP_NOFS);
689 -       if (!req) {
690 -               printk_ratelimited(
691 -                   KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
692 -               kfree(alloc_buf);
693 -               return -ENOMEM;
694 -       }
695 -       skcipher_request_set_callback(req,
696 -               CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
697 -               ext4_dir_crypt_complete, &ecr);
699 -       /* Copy the input */
700 -       memcpy(workbuf, iname->name, iname->len);
701 -       if (iname->len < ciphertext_len)
702 -               memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
704 -       /* Initialize IV */
705 -       memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
707 -       /* Create encryption request */
708 -       sg_init_one(&src_sg, workbuf, ciphertext_len);
709 -       sg_init_one(&dst_sg, oname->name, ciphertext_len);
710 -       skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
711 -       res = crypto_skcipher_encrypt(req);
712 -       if (res == -EINPROGRESS || res == -EBUSY) {
713 -               wait_for_completion(&ecr.completion);
714 -               res = ecr.res;
715 -       }
716 -       kfree(alloc_buf);
717 -       skcipher_request_free(req);
718 -       if (res < 0) {
719 -               printk_ratelimited(
720 -                   KERN_ERR "%s: Error (error code %d)\n", __func__, res);
721 -       }
722 -       oname->len = ciphertext_len;
723 -       return res;
727 - * ext4_fname_decrypt()
728 - *     This function decrypts the input filename, and returns
729 - *     the length of the plaintext.
730 - *     Errors are returned as negative numbers.
731 - *     We trust the caller to allocate sufficient memory to oname string.
732 - */
733 -static int ext4_fname_decrypt(struct inode *inode,
734 -                             const struct ext4_str *iname,
735 -                             struct ext4_str *oname)
737 -       struct ext4_str tmp_in[2], tmp_out[1];
738 -       struct skcipher_request *req = NULL;
739 -       DECLARE_EXT4_COMPLETION_RESULT(ecr);
740 -       struct scatterlist src_sg, dst_sg;
741 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
742 -       struct crypto_skcipher *tfm = ci->ci_ctfm;
743 -       int res = 0;
744 -       char iv[EXT4_CRYPTO_BLOCK_SIZE];
745 -       unsigned lim = max_name_len(inode);
747 -       if (iname->len <= 0 || iname->len > lim)
748 -               return -EIO;
750 -       tmp_in[0].name = iname->name;
751 -       tmp_in[0].len = iname->len;
752 -       tmp_out[0].name = oname->name;
754 -       /* Allocate request */
755 -       req = skcipher_request_alloc(tfm, GFP_NOFS);
756 -       if (!req) {
757 -               printk_ratelimited(
758 -                   KERN_ERR "%s: crypto_request_alloc() failed\n",  __func__);
759 -               return -ENOMEM;
760 -       }
761 -       skcipher_request_set_callback(req,
762 -               CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
763 -               ext4_dir_crypt_complete, &ecr);
765 -       /* Initialize IV */
766 -       memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
768 -       /* Create encryption request */
769 -       sg_init_one(&src_sg, iname->name, iname->len);
770 -       sg_init_one(&dst_sg, oname->name, oname->len);
771 -       skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
772 -       res = crypto_skcipher_decrypt(req);
773 -       if (res == -EINPROGRESS || res == -EBUSY) {
774 -               wait_for_completion(&ecr.completion);
775 -               res = ecr.res;
776 -       }
777 -       skcipher_request_free(req);
778 -       if (res < 0) {
779 -               printk_ratelimited(
780 -                   KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
781 -                   __func__, res);
782 -               return res;
783 -       }
785 -       oname->len = strnlen(oname->name, iname->len);
786 -       return oname->len;
789 -static const char *lookup_table =
790 -       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
792 -/**
793 - * ext4_fname_encode_digest() -
794 - *
795 - * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
796 - * The encoded string is roughly 4/3 times the size of the input string.
797 - */
798 -static int digest_encode(const char *src, int len, char *dst)
800 -       int i = 0, bits = 0, ac = 0;
801 -       char *cp = dst;
803 -       while (i < len) {
804 -               ac += (((unsigned char) src[i]) << bits);
805 -               bits += 8;
806 -               do {
807 -                       *cp++ = lookup_table[ac & 0x3f];
808 -                       ac >>= 6;
809 -                       bits -= 6;
810 -               } while (bits >= 6);
811 -               i++;
812 -       }
813 -       if (bits)
814 -               *cp++ = lookup_table[ac & 0x3f];
815 -       return cp - dst;
818 -static int digest_decode(const char *src, int len, char *dst)
820 -       int i = 0, bits = 0, ac = 0;
821 -       const char *p;
822 -       char *cp = dst;
824 -       while (i < len) {
825 -               p = strchr(lookup_table, src[i]);
826 -               if (p == NULL || src[i] == 0)
827 -                       return -2;
828 -               ac += (p - lookup_table) << bits;
829 -               bits += 6;
830 -               if (bits >= 8) {
831 -                       *cp++ = ac & 0xff;
832 -                       ac >>= 8;
833 -                       bits -= 8;
834 -               }
835 -               i++;
836 -       }
837 -       if (ac)
838 -               return -1;
839 -       return cp - dst;
842 -/**
843 - * ext4_fname_crypto_round_up() -
844 - *
845 - * Return: The next multiple of block size
846 - */
847 -u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
849 -       return ((size+blksize-1)/blksize)*blksize;
852 -unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen)
854 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
855 -       int padding = 32;
857 -       if (ci)
858 -               padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
859 -       if (ilen < EXT4_CRYPTO_BLOCK_SIZE)
860 -               ilen = EXT4_CRYPTO_BLOCK_SIZE;
861 -       return ext4_fname_crypto_round_up(ilen, padding);
865 - * ext4_fname_crypto_alloc_buffer() -
866 - *
867 - * Allocates an output buffer that is sufficient for the crypto operation
868 - * specified by the context and the direction.
869 - */
870 -int ext4_fname_crypto_alloc_buffer(struct inode *inode,
871 -                                  u32 ilen, struct ext4_str *crypto_str)
873 -       unsigned int olen = ext4_fname_encrypted_size(inode, ilen);
875 -       crypto_str->len = olen;
876 -       if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
877 -               olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
878 -       /* Allocated buffer can hold one more character to null-terminate the
879 -        * string */
880 -       crypto_str->name = kmalloc(olen+1, GFP_NOFS);
881 -       if (!(crypto_str->name))
882 -               return -ENOMEM;
883 -       return 0;
886 -/**
887 - * ext4_fname_crypto_free_buffer() -
888 - *
889 - * Frees the buffer allocated for crypto operation.
890 - */
891 -void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
893 -       if (!crypto_str)
894 -               return;
895 -       kfree(crypto_str->name);
896 -       crypto_str->name = NULL;
899 -/**
900 - * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
901 - */
902 -int _ext4_fname_disk_to_usr(struct inode *inode,
903 -                           struct dx_hash_info *hinfo,
904 -                           const struct ext4_str *iname,
905 -                           struct ext4_str *oname)
907 -       char buf[24];
908 -       int ret;
910 -       if (iname->len < 3) {
911 -               /*Check for . and .. */
912 -               if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
913 -                       oname->name[0] = '.';
914 -                       oname->name[iname->len-1] = '.';
915 -                       oname->len = iname->len;
916 -                       return oname->len;
917 -               }
918 -       }
919 -       if (iname->len < EXT4_CRYPTO_BLOCK_SIZE) {
920 -               EXT4_ERROR_INODE(inode, "encrypted inode too small");
921 -               return -EUCLEAN;
922 -       }
923 -       if (EXT4_I(inode)->i_crypt_info)
924 -               return ext4_fname_decrypt(inode, iname, oname);
926 -       if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
927 -               ret = digest_encode(iname->name, iname->len, oname->name);
928 -               oname->len = ret;
929 -               return ret;
930 -       }
931 -       if (hinfo) {
932 -               memcpy(buf, &hinfo->hash, 4);
933 -               memcpy(buf+4, &hinfo->minor_hash, 4);
934 -       } else
935 -               memset(buf, 0, 8);
936 -       memcpy(buf + 8, iname->name + iname->len - 16, 16);
937 -       oname->name[0] = '_';
938 -       ret = digest_encode(buf, 24, oname->name+1);
939 -       oname->len = ret + 1;
940 -       return ret + 1;
943 -int ext4_fname_disk_to_usr(struct inode *inode,
944 -                          struct dx_hash_info *hinfo,
945 -                          const struct ext4_dir_entry_2 *de,
946 -                          struct ext4_str *oname)
948 -       struct ext4_str iname = {.name = (unsigned char *) de->name,
949 -                                .len = de->name_len };
951 -       return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
955 -/**
956 - * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
957 - */
958 -int ext4_fname_usr_to_disk(struct inode *inode,
959 -                          const struct qstr *iname,
960 -                          struct ext4_str *oname)
962 -       int res;
963 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
965 -       if (iname->len < 3) {
966 -               /*Check for . and .. */
967 -               if (iname->name[0] == '.' &&
968 -                               iname->name[iname->len-1] == '.') {
969 -                       oname->name[0] = '.';
970 -                       oname->name[iname->len-1] = '.';
971 -                       oname->len = iname->len;
972 -                       return oname->len;
973 -               }
974 -       }
975 -       if (ci) {
976 -               res = ext4_fname_encrypt(inode, iname, oname);
977 -               return res;
978 -       }
979 -       /* Without a proper key, a user is not allowed to modify the filenames
980 -        * in a directory. Consequently, a user space name cannot be mapped to
981 -        * a disk-space name */
982 -       return -EACCES;
985 -int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
986 -                             int lookup, struct ext4_filename *fname)
988 -       struct ext4_crypt_info *ci;
989 -       int ret = 0, bigname = 0;
991 -       memset(fname, 0, sizeof(struct ext4_filename));
992 -       fname->usr_fname = iname;
994 -       if (!ext4_encrypted_inode(dir) ||
995 -           ((iname->name[0] == '.') &&
996 -            ((iname->len == 1) ||
997 -             ((iname->name[1] == '.') && (iname->len == 2))))) {
998 -               fname->disk_name.name = (unsigned char *) iname->name;
999 -               fname->disk_name.len = iname->len;
1000 -               return 0;
1001 -       }
1002 -       ret = ext4_get_encryption_info(dir);
1003 -       if (ret)
1004 -               return ret;
1005 -       ci = EXT4_I(dir)->i_crypt_info;
1006 -       if (ci) {
1007 -               ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
1008 -                                                    &fname->crypto_buf);
1009 -               if (ret < 0)
1010 -                       return ret;
1011 -               ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
1012 -               if (ret < 0)
1013 -                       goto errout;
1014 -               fname->disk_name.name = fname->crypto_buf.name;
1015 -               fname->disk_name.len = fname->crypto_buf.len;
1016 -               return 0;
1017 -       }
1018 -       if (!lookup)
1019 -               return -EACCES;
1021 -       /* We don't have the key and we are doing a lookup; decode the
1022 -        * user-supplied name
1023 -        */
1024 -       if (iname->name[0] == '_')
1025 -               bigname = 1;
1026 -       if ((bigname && (iname->len != 33)) ||
1027 -           (!bigname && (iname->len > 43)))
1028 -               return -ENOENT;
1030 -       fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
1031 -       if (fname->crypto_buf.name == NULL)
1032 -               return -ENOMEM;
1033 -       ret = digest_decode(iname->name + bigname, iname->len - bigname,
1034 -                           fname->crypto_buf.name);
1035 -       if (ret < 0) {
1036 -               ret = -ENOENT;
1037 -               goto errout;
1038 -       }
1039 -       fname->crypto_buf.len = ret;
1040 -       if (bigname) {
1041 -               memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
1042 -               memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
1043 -       } else {
1044 -               fname->disk_name.name = fname->crypto_buf.name;
1045 -               fname->disk_name.len = fname->crypto_buf.len;
1046 -       }
1047 -       return 0;
1048 -errout:
1049 -       kfree(fname->crypto_buf.name);
1050 -       fname->crypto_buf.name = NULL;
1051 -       return ret;
1054 -void ext4_fname_free_filename(struct ext4_filename *fname)
1056 -       kfree(fname->crypto_buf.name);
1057 -       fname->crypto_buf.name = NULL;
1058 -       fname->usr_fname = NULL;
1059 -       fname->disk_name.name = NULL;
1061 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
1062 deleted file mode 100644
1063 index 0129d68..0000000
1064 --- a/fs/ext4/crypto_key.c
1065 +++ /dev/null
1066 @@ -1,274 +0,0 @@
1068 - * linux/fs/ext4/crypto_key.c
1069 - *
1070 - * Copyright (C) 2015, Google, Inc.
1071 - *
1072 - * This contains encryption key functions for ext4
1073 - *
1074 - * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
1075 - */
1077 -#include <crypto/skcipher.h>
1078 -#include <keys/encrypted-type.h>
1079 -#include <keys/user-type.h>
1080 -#include <linux/random.h>
1081 -#include <linux/scatterlist.h>
1082 -#include <uapi/linux/keyctl.h>
1084 -#include "ext4.h"
1085 -#include "xattr.h"
1087 -static void derive_crypt_complete(struct crypto_async_request *req, int rc)
1089 -       struct ext4_completion_result *ecr = req->data;
1091 -       if (rc == -EINPROGRESS)
1092 -               return;
1094 -       ecr->res = rc;
1095 -       complete(&ecr->completion);
1098 -/**
1099 - * ext4_derive_key_aes() - Derive a key using AES-128-ECB
1100 - * @deriving_key: Encryption key used for derivation.
1101 - * @source_key:   Source key to which to apply derivation.
1102 - * @derived_key:  Derived key.
1103 - *
1104 - * Return: Zero on success; non-zero otherwise.
1105 - */
1106 -static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
1107 -                              char source_key[EXT4_AES_256_XTS_KEY_SIZE],
1108 -                              char derived_key[EXT4_AES_256_XTS_KEY_SIZE])
1110 -       int res = 0;
1111 -       struct skcipher_request *req = NULL;
1112 -       DECLARE_EXT4_COMPLETION_RESULT(ecr);
1113 -       struct scatterlist src_sg, dst_sg;
1114 -       struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
1116 -       if (IS_ERR(tfm)) {
1117 -               res = PTR_ERR(tfm);
1118 -               tfm = NULL;
1119 -               goto out;
1120 -       }
1121 -       crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1122 -       req = skcipher_request_alloc(tfm, GFP_NOFS);
1123 -       if (!req) {
1124 -               res = -ENOMEM;
1125 -               goto out;
1126 -       }
1127 -       skcipher_request_set_callback(req,
1128 -                       CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1129 -                       derive_crypt_complete, &ecr);
1130 -       res = crypto_skcipher_setkey(tfm, deriving_key,
1131 -                                    EXT4_AES_128_ECB_KEY_SIZE);
1132 -       if (res < 0)
1133 -               goto out;
1134 -       sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE);
1135 -       sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE);
1136 -       skcipher_request_set_crypt(req, &src_sg, &dst_sg,
1137 -                                  EXT4_AES_256_XTS_KEY_SIZE, NULL);
1138 -       res = crypto_skcipher_encrypt(req);
1139 -       if (res == -EINPROGRESS || res == -EBUSY) {
1140 -               wait_for_completion(&ecr.completion);
1141 -               res = ecr.res;
1142 -       }
1144 -out:
1145 -       skcipher_request_free(req);
1146 -       crypto_free_skcipher(tfm);
1147 -       return res;
1150 -void ext4_free_crypt_info(struct ext4_crypt_info *ci)
1152 -       if (!ci)
1153 -               return;
1155 -       if (ci->ci_keyring_key)
1156 -               key_put(ci->ci_keyring_key);
1157 -       crypto_free_skcipher(ci->ci_ctfm);
1158 -       kmem_cache_free(ext4_crypt_info_cachep, ci);
1161 -void ext4_free_encryption_info(struct inode *inode,
1162 -                              struct ext4_crypt_info *ci)
1164 -       struct ext4_inode_info *ei = EXT4_I(inode);
1165 -       struct ext4_crypt_info *prev;
1167 -       if (ci == NULL)
1168 -               ci = ACCESS_ONCE(ei->i_crypt_info);
1169 -       if (ci == NULL)
1170 -               return;
1171 -       prev = cmpxchg(&ei->i_crypt_info, ci, NULL);
1172 -       if (prev != ci)
1173 -               return;
1175 -       ext4_free_crypt_info(ci);
1178 -int _ext4_get_encryption_info(struct inode *inode)
1180 -       struct ext4_inode_info *ei = EXT4_I(inode);
1181 -       struct ext4_crypt_info *crypt_info;
1182 -       char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
1183 -                                (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
1184 -       struct key *keyring_key = NULL;
1185 -       struct ext4_encryption_key *master_key;
1186 -       struct ext4_encryption_context ctx;
1187 -       const struct user_key_payload *ukp;
1188 -       struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1189 -       struct crypto_skcipher *ctfm;
1190 -       const char *cipher_str;
1191 -       char raw_key[EXT4_MAX_KEY_SIZE];
1192 -       char mode;
1193 -       int res;
1195 -       if (!ext4_read_workqueue) {
1196 -               res = ext4_init_crypto();
1197 -               if (res)
1198 -                       return res;
1199 -       }
1201 -retry:
1202 -       crypt_info = ACCESS_ONCE(ei->i_crypt_info);
1203 -       if (crypt_info) {
1204 -               if (!crypt_info->ci_keyring_key ||
1205 -                   key_validate(crypt_info->ci_keyring_key) == 0)
1206 -                       return 0;
1207 -               ext4_free_encryption_info(inode, crypt_info);
1208 -               goto retry;
1209 -       }
1211 -       res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1212 -                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1213 -                                &ctx, sizeof(ctx));
1214 -       if (res < 0) {
1215 -               if (!DUMMY_ENCRYPTION_ENABLED(sbi))
1216 -                       return res;
1217 -               ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
1218 -               ctx.filenames_encryption_mode =
1219 -                       EXT4_ENCRYPTION_MODE_AES_256_CTS;
1220 -               ctx.flags = 0;
1221 -       } else if (res != sizeof(ctx))
1222 -               return -EINVAL;
1223 -       res = 0;
1225 -       crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
1226 -       if (!crypt_info)
1227 -               return -ENOMEM;
1229 -       crypt_info->ci_flags = ctx.flags;
1230 -       crypt_info->ci_data_mode = ctx.contents_encryption_mode;
1231 -       crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
1232 -       crypt_info->ci_ctfm = NULL;
1233 -       crypt_info->ci_keyring_key = NULL;
1234 -       memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
1235 -              sizeof(crypt_info->ci_master_key));
1236 -       if (S_ISREG(inode->i_mode))
1237 -               mode = crypt_info->ci_data_mode;
1238 -       else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1239 -               mode = crypt_info->ci_filename_mode;
1240 -       else
1241 -               BUG();
1242 -       switch (mode) {
1243 -       case EXT4_ENCRYPTION_MODE_AES_256_XTS:
1244 -               cipher_str = "xts(aes)";
1245 -               break;
1246 -       case EXT4_ENCRYPTION_MODE_AES_256_CTS:
1247 -               cipher_str = "cts(cbc(aes))";
1248 -               break;
1249 -       default:
1250 -               printk_once(KERN_WARNING
1251 -                           "ext4: unsupported key mode %d (ino %u)\n",
1252 -                           mode, (unsigned) inode->i_ino);
1253 -               res = -ENOKEY;
1254 -               goto out;
1255 -       }
1256 -       if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
1257 -               memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
1258 -               goto got_key;
1259 -       }
1260 -       memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
1261 -              EXT4_KEY_DESC_PREFIX_SIZE);
1262 -       sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
1263 -               "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
1264 -               ctx.master_key_descriptor);
1265 -       full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
1266 -                           (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
1267 -       keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
1268 -       if (IS_ERR(keyring_key)) {
1269 -               res = PTR_ERR(keyring_key);
1270 -               keyring_key = NULL;
1271 -               goto out;
1272 -       }
1273 -       crypt_info->ci_keyring_key = keyring_key;
1274 -       if (keyring_key->type != &key_type_logon) {
1275 -               printk_once(KERN_WARNING
1276 -                           "ext4: key type must be logon\n");
1277 -               res = -ENOKEY;
1278 -               goto out;
1279 -       }
1280 -       down_read(&keyring_key->sem);
1281 -       ukp = user_key_payload(keyring_key);
1282 -       if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
1283 -               res = -EINVAL;
1284 -               up_read(&keyring_key->sem);
1285 -               goto out;
1286 -       }
1287 -       master_key = (struct ext4_encryption_key *)ukp->data;
1288 -       BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
1289 -                    EXT4_KEY_DERIVATION_NONCE_SIZE);
1290 -       if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) {
1291 -               printk_once(KERN_WARNING
1292 -                           "ext4: key size incorrect: %d\n",
1293 -                           master_key->size);
1294 -               res = -ENOKEY;
1295 -               up_read(&keyring_key->sem);
1296 -               goto out;
1297 -       }
1298 -       res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
1299 -                                 raw_key);
1300 -       up_read(&keyring_key->sem);
1301 -       if (res)
1302 -               goto out;
1303 -got_key:
1304 -       ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
1305 -       if (!ctfm || IS_ERR(ctfm)) {
1306 -               res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
1307 -               printk(KERN_DEBUG
1308 -                      "%s: error %d (inode %u) allocating crypto tfm\n",
1309 -                      __func__, res, (unsigned) inode->i_ino);
1310 -               goto out;
1311 -       }
1312 -       crypt_info->ci_ctfm = ctfm;
1313 -       crypto_skcipher_clear_flags(ctfm, ~0);
1314 -       crypto_tfm_set_flags(crypto_skcipher_tfm(ctfm),
1315 -                            CRYPTO_TFM_REQ_WEAK_KEY);
1316 -       res = crypto_skcipher_setkey(ctfm, raw_key,
1317 -                                    ext4_encryption_key_size(mode));
1318 -       if (res)
1319 -               goto out;
1320 -       memzero_explicit(raw_key, sizeof(raw_key));
1321 -       if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) {
1322 -               ext4_free_crypt_info(crypt_info);
1323 -               goto retry;
1324 -       }
1325 -       return 0;
1327 -out:
1328 -       if (res == -ENOKEY)
1329 -               res = 0;
1330 -       ext4_free_crypt_info(crypt_info);
1331 -       memzero_explicit(raw_key, sizeof(raw_key));
1332 -       return res;
1335 -int ext4_has_encryption_key(struct inode *inode)
1337 -       struct ext4_inode_info *ei = EXT4_I(inode);
1339 -       return (ei->i_crypt_info != NULL);
1341 diff --git a/fs/ext4/crypto_policy.c b/fs/ext4/crypto_policy.c
1342 deleted file mode 100644
1343 index ad05069..0000000
1344 --- a/fs/ext4/crypto_policy.c
1345 +++ /dev/null
1346 @@ -1,229 +0,0 @@
1348 - * linux/fs/ext4/crypto_policy.c
1349 - *
1350 - * Copyright (C) 2015, Google, Inc.
1351 - *
1352 - * This contains encryption policy functions for ext4
1353 - *
1354 - * Written by Michael Halcrow, 2015.
1355 - */
1357 -#include <linux/random.h>
1358 -#include <linux/string.h>
1359 -#include <linux/types.h>
1361 -#include "ext4_jbd2.h"
1362 -#include "ext4.h"
1363 -#include "xattr.h"
1365 -static int ext4_inode_has_encryption_context(struct inode *inode)
1367 -       int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1368 -                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
1369 -       return (res > 0);
1373 - * check whether the policy is consistent with the encryption context
1374 - * for the inode
1375 - */
1376 -static int ext4_is_encryption_context_consistent_with_policy(
1377 -       struct inode *inode, const struct ext4_encryption_policy *policy)
1379 -       struct ext4_encryption_context ctx;
1380 -       int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1381 -                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1382 -                                sizeof(ctx));
1383 -       if (res != sizeof(ctx))
1384 -               return 0;
1385 -       return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
1386 -                       EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
1387 -               (ctx.flags ==
1388 -                policy->flags) &&
1389 -               (ctx.contents_encryption_mode ==
1390 -                policy->contents_encryption_mode) &&
1391 -               (ctx.filenames_encryption_mode ==
1392 -                policy->filenames_encryption_mode));
1395 -static int ext4_create_encryption_context_from_policy(
1396 -       struct inode *inode, const struct ext4_encryption_policy *policy)
1398 -       struct ext4_encryption_context ctx;
1399 -       handle_t *handle;
1400 -       int res, res2;
1402 -       res = ext4_convert_inline_data(inode);
1403 -       if (res)
1404 -               return res;
1406 -       ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
1407 -       memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
1408 -              EXT4_KEY_DESCRIPTOR_SIZE);
1409 -       if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
1410 -               printk(KERN_WARNING
1411 -                      "%s: Invalid contents encryption mode %d\n", __func__,
1412 -                       policy->contents_encryption_mode);
1413 -               return -EINVAL;
1414 -       }
1415 -       if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
1416 -               printk(KERN_WARNING
1417 -                      "%s: Invalid filenames encryption mode %d\n", __func__,
1418 -                       policy->filenames_encryption_mode);
1419 -               return -EINVAL;
1420 -       }
1421 -       if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
1422 -               return -EINVAL;
1423 -       ctx.contents_encryption_mode = policy->contents_encryption_mode;
1424 -       ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
1425 -       ctx.flags = policy->flags;
1426 -       BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
1427 -       get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
1429 -       handle = ext4_journal_start(inode, EXT4_HT_MISC,
1430 -                                   ext4_jbd2_credits_xattr(inode));
1431 -       if (IS_ERR(handle))
1432 -               return PTR_ERR(handle);
1433 -       res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1434 -                            EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1435 -                            sizeof(ctx), 0);
1436 -       if (!res) {
1437 -               ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1438 -               res = ext4_mark_inode_dirty(handle, inode);
1439 -               if (res)
1440 -                       EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1441 -       }
1442 -       res2 = ext4_journal_stop(handle);
1443 -       if (!res)
1444 -               res = res2;
1445 -       return res;
1448 -int ext4_process_policy(const struct ext4_encryption_policy *policy,
1449 -                       struct inode *inode)
1451 -       if (policy->version != 0)
1452 -               return -EINVAL;
1454 -       if (!ext4_inode_has_encryption_context(inode)) {
1455 -               if (!S_ISDIR(inode->i_mode))
1456 -                       return -EINVAL;
1457 -               if (!ext4_empty_dir(inode))
1458 -                       return -ENOTEMPTY;
1459 -               return ext4_create_encryption_context_from_policy(inode,
1460 -                                                                 policy);
1461 -       }
1463 -       if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
1464 -               return 0;
1466 -       printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
1467 -              __func__);
1468 -       return -EINVAL;
1471 -int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
1473 -       struct ext4_encryption_context ctx;
1475 -       int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1476 -                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1477 -                                &ctx, sizeof(ctx));
1478 -       if (res != sizeof(ctx))
1479 -               return -ENOENT;
1480 -       if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
1481 -               return -EINVAL;
1482 -       policy->version = 0;
1483 -       policy->contents_encryption_mode = ctx.contents_encryption_mode;
1484 -       policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
1485 -       policy->flags = ctx.flags;
1486 -       memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
1487 -              EXT4_KEY_DESCRIPTOR_SIZE);
1488 -       return 0;
1491 -int ext4_is_child_context_consistent_with_parent(struct inode *parent,
1492 -                                                struct inode *child)
1494 -       struct ext4_crypt_info *parent_ci, *child_ci;
1495 -       int res;
1497 -       if ((parent == NULL) || (child == NULL)) {
1498 -               pr_err("parent %p child %p\n", parent, child);
1499 -               WARN_ON(1);     /* Should never happen */
1500 -               return 0;
1501 -       }
1502 -       /* no restrictions if the parent directory is not encrypted */
1503 -       if (!ext4_encrypted_inode(parent))
1504 -               return 1;
1505 -       /* if the child directory is not encrypted, this is always a problem */
1506 -       if (!ext4_encrypted_inode(child))
1507 -               return 0;
1508 -       res = ext4_get_encryption_info(parent);
1509 -       if (res)
1510 -               return 0;
1511 -       res = ext4_get_encryption_info(child);
1512 -       if (res)
1513 -               return 0;
1514 -       parent_ci = EXT4_I(parent)->i_crypt_info;
1515 -       child_ci = EXT4_I(child)->i_crypt_info;
1516 -       if (!parent_ci && !child_ci)
1517 -               return 1;
1518 -       if (!parent_ci || !child_ci)
1519 -               return 0;
1521 -       return (memcmp(parent_ci->ci_master_key,
1522 -                      child_ci->ci_master_key,
1523 -                      EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
1524 -               (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
1525 -               (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
1526 -               (parent_ci->ci_flags == child_ci->ci_flags));
1529 -/**
1530 - * ext4_inherit_context() - Sets a child context from its parent
1531 - * @parent: Parent inode from which the context is inherited.
1532 - * @child:  Child inode that inherits the context from @parent.
1533 - *
1534 - * Return: Zero on success, non-zero otherwise
1535 - */
1536 -int ext4_inherit_context(struct inode *parent, struct inode *child)
1538 -       struct ext4_encryption_context ctx;
1539 -       struct ext4_crypt_info *ci;
1540 -       int res;
1542 -       res = ext4_get_encryption_info(parent);
1543 -       if (res < 0)
1544 -               return res;
1545 -       ci = EXT4_I(parent)->i_crypt_info;
1546 -       if (ci == NULL)
1547 -               return -ENOKEY;
1549 -       ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
1550 -       if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
1551 -               ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
1552 -               ctx.filenames_encryption_mode =
1553 -                       EXT4_ENCRYPTION_MODE_AES_256_CTS;
1554 -               ctx.flags = 0;
1555 -               memset(ctx.master_key_descriptor, 0x42,
1556 -                      EXT4_KEY_DESCRIPTOR_SIZE);
1557 -               res = 0;
1558 -       } else {
1559 -               ctx.contents_encryption_mode = ci->ci_data_mode;
1560 -               ctx.filenames_encryption_mode = ci->ci_filename_mode;
1561 -               ctx.flags = ci->ci_flags;
1562 -               memcpy(ctx.master_key_descriptor, ci->ci_master_key,
1563 -                      EXT4_KEY_DESCRIPTOR_SIZE);
1564 -       }
1565 -       get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
1566 -       res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
1567 -                            EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1568 -                            sizeof(ctx), 0);
1569 -       if (!res) {
1570 -               ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
1571 -               ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
1572 -               res = ext4_get_encryption_info(child);
1573 -       }
1574 -       return res;
1576 diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
1577 index 68323e3..67415e0 100644
1578 --- a/fs/ext4/dir.c
1579 +++ b/fs/ext4/dir.c
1580 @@ -109,10 +109,10 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1581         struct super_block *sb = inode->i_sb;
1582         struct buffer_head *bh = NULL;
1583         int dir_has_error = 0;
1584 -       struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1585 +       struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1587         if (ext4_encrypted_inode(inode)) {
1588 -               err = ext4_get_encryption_info(inode);
1589 +               err = fscrypt_get_encryption_info(inode);
1590                 if (err && err != -ENOKEY)
1591                         return err;
1592         }
1593 @@ -139,8 +139,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1594         }
1596         if (ext4_encrypted_inode(inode)) {
1597 -               err = ext4_fname_crypto_alloc_buffer(inode, EXT4_NAME_LEN,
1598 -                                                    &fname_crypto_str);
1599 +               err = fscrypt_fname_alloc_buffer(inode, EXT4_NAME_LEN, &fstr);
1600                 if (err < 0)
1601                         return err;
1602         }
1603 @@ -253,16 +252,19 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1604                                             get_dtype(sb, de->file_type)))
1605                                                 goto done;
1606                                 } else {
1607 -                                       int save_len = fname_crypto_str.len;
1608 +                                       int save_len = fstr.len;
1609 +                                       struct fscrypt_str de_name =
1610 +                                                       FSTR_INIT(de->name,
1611 +                                                               de->name_len);
1613                                         /* Directory is encrypted */
1614 -                                       err = ext4_fname_disk_to_usr(inode,
1615 -                                               NULL, de, &fname_crypto_str);
1616 -                                       fname_crypto_str.len = save_len;
1617 +                                       err = fscrypt_fname_disk_to_usr(inode,
1618 +                                               0, 0, &de_name, &fstr);
1619 +                                       fstr.len = save_len;
1620                                         if (err < 0)
1621                                                 goto errout;
1622                                         if (!dir_emit(ctx,
1623 -                                           fname_crypto_str.name, err,
1624 +                                           fstr.name, err,
1625                                             le32_to_cpu(de->inode),
1626                                             get_dtype(sb, de->file_type)))
1627                                                 goto done;
1628 @@ -281,7 +283,7 @@ done:
1629         err = 0;
1630  errout:
1631  #ifdef CONFIG_EXT4_FS_ENCRYPTION
1632 -       ext4_fname_crypto_free_buffer(&fname_crypto_str);
1633 +       fscrypt_fname_free_buffer(&fstr);
1634  #endif
1635         brelse(bh);
1636         return err;
1637 @@ -432,7 +434,7 @@ void ext4_htree_free_dir_info(struct dir_private_info *p)
1638  int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
1639                              __u32 minor_hash,
1640                             struct ext4_dir_entry_2 *dirent,
1641 -                           struct ext4_str *ent_name)
1642 +                           struct fscrypt_str *ent_name)
1644         struct rb_node **p, *parent = NULL;
1645         struct fname *fname, *new_fn;
1646 @@ -609,7 +611,7 @@ finished:
1647  static int ext4_dir_open(struct inode * inode, struct file * filp)
1649         if (ext4_encrypted_inode(inode))
1650 -               return ext4_get_encryption_info(inode) ? -EACCES : 0;
1651 +               return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
1652         return 0;
1655 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
1656 index 96c73e6..ea31931 100644
1657 --- a/fs/ext4/ext4.h
1658 +++ b/fs/ext4/ext4.h
1659 @@ -32,6 +32,7 @@
1660  #include <linux/percpu_counter.h>
1661  #include <linux/ratelimit.h>
1662  #include <crypto/hash.h>
1663 +#include <linux/fscrypto.h>
1664  #include <linux/falloc.h>
1665  #include <linux/percpu-rwsem.h>
1666  #ifdef __KERNEL__
1667 @@ -608,15 +609,6 @@ enum {
1668  #define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER  0x0010
1669  #define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER   0x0020
1671 -/* Encryption algorithms */
1672 -#define EXT4_ENCRYPTION_MODE_INVALID           0
1673 -#define EXT4_ENCRYPTION_MODE_AES_256_XTS       1
1674 -#define EXT4_ENCRYPTION_MODE_AES_256_GCM       2
1675 -#define EXT4_ENCRYPTION_MODE_AES_256_CBC       3
1676 -#define EXT4_ENCRYPTION_MODE_AES_256_CTS       4
1678 -#include "ext4_crypto.h"
1680  /*
1681   * ioctl commands
1682   */
1683 @@ -638,9 +630,9 @@ enum {
1684  #define EXT4_IOC_RESIZE_FS             _IOW('f', 16, __u64)
1685  #define EXT4_IOC_SWAP_BOOT             _IO('f', 17)
1686  #define EXT4_IOC_PRECACHE_EXTENTS      _IO('f', 18)
1687 -#define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
1688 -#define EXT4_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
1689 -#define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
1690 +#define EXT4_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
1691 +#define EXT4_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
1692 +#define EXT4_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
1694  #ifndef FS_IOC_FSGETXATTR
1695  /* Until the uapi changes get merged for project quota... */
1696 @@ -1082,10 +1074,6 @@ struct ext4_inode_info {
1697         /* Precomputed uuid+inum+igen checksum for seeding inode checksums */
1698         __u32 i_csum_seed;
1700 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1701 -       /* Encryption params */
1702 -       struct ext4_crypt_info *i_crypt_info;
1703 -#endif
1704         kprojid_t i_projid;
1705  };
1707 @@ -1344,6 +1332,11 @@ struct ext4_super_block {
1708  /* Number of quota types we support */
1709  #define EXT4_MAXQUOTAS 3
1711 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
1712 +#define EXT4_KEY_DESC_PREFIX "ext4:"
1713 +#define EXT4_KEY_DESC_PREFIX_SIZE 5
1714 +#endif
1716  /*
1717   * fourth extended-fs super-block data in memory
1718   */
1719 @@ -1513,6 +1506,12 @@ struct ext4_sb_info {
1721         /* Barrier between changing inodes' journal flags and writepages ops. */
1722         struct percpu_rw_semaphore s_journal_flag_rwsem;
1724 +       /* Encryption support */
1725 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
1726 +       u8 key_prefix[EXT4_KEY_DESC_PREFIX_SIZE];
1727 +       u8 key_prefix_size;
1728 +#endif
1729  };
1731  static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
1732 @@ -1611,15 +1610,6 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1733  /*
1734   * Returns true if the inode is inode is encrypted
1735   */
1736 -static inline int ext4_encrypted_inode(struct inode *inode)
1738 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1739 -       return ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT);
1740 -#else
1741 -       return 0;
1742 -#endif
1745  #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
1747  /*
1748 @@ -2083,10 +2073,10 @@ struct dx_hash_info
1750  struct ext4_filename {
1751         const struct qstr *usr_fname;
1752 -       struct ext4_str disk_name;
1753 +       struct fscrypt_str disk_name;
1754         struct dx_hash_info hinfo;
1755  #ifdef CONFIG_EXT4_FS_ENCRYPTION
1756 -       struct ext4_str crypto_buf;
1757 +       struct fscrypt_str crypto_buf;
1758  #endif
1759  };
1761 @@ -2297,132 +2287,82 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
1762                                               struct ext4_group_desc *gdp);
1763  ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
1765 -/* crypto_policy.c */
1766 -int ext4_is_child_context_consistent_with_parent(struct inode *parent,
1767 -                                                struct inode *child);
1768 -int ext4_inherit_context(struct inode *parent, struct inode *child);
1769 -void ext4_to_hex(char *dst, char *src, size_t src_size);
1770 -int ext4_process_policy(const struct ext4_encryption_policy *policy,
1771 -                       struct inode *inode);
1772 -int ext4_get_policy(struct inode *inode,
1773 -                   struct ext4_encryption_policy *policy);
1775 -/* crypto.c */
1776 -extern struct kmem_cache *ext4_crypt_info_cachep;
1777 -bool ext4_valid_contents_enc_mode(uint32_t mode);
1778 -uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size);
1779 -extern struct workqueue_struct *ext4_read_workqueue;
1780 -struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
1781 -                                           gfp_t gfp_flags);
1782 -void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx);
1783 -void ext4_restore_control_page(struct page *data_page);
1784 -struct page *ext4_encrypt(struct inode *inode,
1785 -                         struct page *plaintext_page,
1786 -                         gfp_t gfp_flags);
1787 -int ext4_decrypt(struct page *page);
1788 -int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
1789 -                          ext4_fsblk_t pblk, ext4_lblk_t len);
1790 -extern const struct dentry_operations ext4_encrypted_d_ops;
1792 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1793 -int ext4_init_crypto(void);
1794 -void ext4_exit_crypto(void);
1795  static inline int ext4_sb_has_crypto(struct super_block *sb)
1797         return ext4_has_feature_encrypt(sb);
1799 -#else
1800 -static inline int ext4_init_crypto(void) { return 0; }
1801 -static inline void ext4_exit_crypto(void) { }
1802 -static inline int ext4_sb_has_crypto(struct super_block *sb)
1804 +static inline bool ext4_encrypted_inode(struct inode *inode)
1806 -       return 0;
1807 +       return ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT);
1809 -#endif
1811 -/* crypto_fname.c */
1812 -bool ext4_valid_filenames_enc_mode(uint32_t mode);
1813 -u32 ext4_fname_crypto_round_up(u32 size, u32 blksize);
1814 -unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen);
1815 -int ext4_fname_crypto_alloc_buffer(struct inode *inode,
1816 -                                  u32 ilen, struct ext4_str *crypto_str);
1817 -int _ext4_fname_disk_to_usr(struct inode *inode,
1818 -                           struct dx_hash_info *hinfo,
1819 -                           const struct ext4_str *iname,
1820 -                           struct ext4_str *oname);
1821 -int ext4_fname_disk_to_usr(struct inode *inode,
1822 -                          struct dx_hash_info *hinfo,
1823 -                          const struct ext4_dir_entry_2 *de,
1824 -                          struct ext4_str *oname);
1825 -int ext4_fname_usr_to_disk(struct inode *inode,
1826 -                          const struct qstr *iname,
1827 -                          struct ext4_str *oname);
1828  #ifdef CONFIG_EXT4_FS_ENCRYPTION
1829 -void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str);
1830 -int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
1831 -                             int lookup, struct ext4_filename *fname);
1832 -void ext4_fname_free_filename(struct ext4_filename *fname);
1833 -#else
1834 -static inline
1835 -int ext4_setup_fname_crypto(struct inode *inode)
1837 -       return 0;
1839 -static inline void ext4_fname_crypto_free_buffer(struct ext4_str *p) { }
1840  static inline int ext4_fname_setup_filename(struct inode *dir,
1841 -                                    const struct qstr *iname,
1842 -                                    int lookup, struct ext4_filename *fname)
1843 +                       const struct qstr *iname,
1844 +                       int lookup, struct ext4_filename *fname)
1846 -       fname->usr_fname = iname;
1847 -       fname->disk_name.name = (unsigned char *) iname->name;
1848 -       fname->disk_name.len = iname->len;
1849 -       return 0;
1851 -static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
1852 -#endif
1854 +       struct fscrypt_name name;
1855 +       int err;
1857 -/* crypto_key.c */
1858 -void ext4_free_crypt_info(struct ext4_crypt_info *ci);
1859 -void ext4_free_encryption_info(struct inode *inode, struct ext4_crypt_info *ci);
1860 -int _ext4_get_encryption_info(struct inode *inode);
1861 +       memset(fname, 0, sizeof(struct ext4_filename));
1863 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1864 -int ext4_has_encryption_key(struct inode *inode);
1865 +       err = fscrypt_setup_filename(dir, iname, lookup, &name);
1867 -static inline int ext4_get_encryption_info(struct inode *inode)
1869 -       struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
1871 -       if (!ci ||
1872 -           (ci->ci_keyring_key &&
1873 -            (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1874 -                                          (1 << KEY_FLAG_REVOKED) |
1875 -                                          (1 << KEY_FLAG_DEAD)))))
1876 -               return _ext4_get_encryption_info(inode);
1877 -       return 0;
1878 +       fname->usr_fname = name.usr_fname;
1879 +       fname->disk_name = name.disk_name;
1880 +       fname->hinfo.hash = name.hash;
1881 +       fname->hinfo.minor_hash = name.minor_hash;
1882 +       fname->crypto_buf = name.crypto_buf;
1883 +       return err;
1886 -static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
1887 +static inline void ext4_fname_free_filename(struct ext4_filename *fname)
1889 -       return EXT4_I(inode)->i_crypt_info;
1891 +       struct fscrypt_name name;
1893 -#else
1894 -static inline int ext4_has_encryption_key(struct inode *inode)
1896 -       return 0;
1897 +       name.crypto_buf = fname->crypto_buf;
1898 +       fscrypt_free_filename(&name);
1900 +       fname->crypto_buf.name = NULL;
1901 +       fname->usr_fname = NULL;
1902 +       fname->disk_name.name = NULL;
1904 -static inline int ext4_get_encryption_info(struct inode *inode)
1905 +#else
1906 +static inline int ext4_fname_setup_filename(struct inode *dir,
1907 +               const struct qstr *iname,
1908 +               int lookup, struct ext4_filename *fname)
1910 +       fname->usr_fname = iname;
1911 +       fname->disk_name.name = (unsigned char *) iname->name;
1912 +       fname->disk_name.len = iname->len;
1913         return 0;
1915 -static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
1917 -       return NULL;
1919 -#endif
1920 +static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
1922 +#define fscrypt_set_d_op(i)
1923 +#define fscrypt_get_ctx                        fscrypt_notsupp_get_ctx
1924 +#define fscrypt_release_ctx            fscrypt_notsupp_release_ctx
1925 +#define fscrypt_encrypt_page           fscrypt_notsupp_encrypt_page
1926 +#define fscrypt_decrypt_page           fscrypt_notsupp_decrypt_page
1927 +#define fscrypt_decrypt_bio_pages      fscrypt_notsupp_decrypt_bio_pages
1928 +#define fscrypt_pullback_bio_page      fscrypt_notsupp_pullback_bio_page
1929 +#define fscrypt_restore_control_page   fscrypt_notsupp_restore_control_page
1930 +#define fscrypt_zeroout_range          fscrypt_notsupp_zeroout_range
1931 +#define fscrypt_process_policy         fscrypt_notsupp_process_policy
1932 +#define fscrypt_get_policy             fscrypt_notsupp_get_policy
1933 +#define fscrypt_has_permitted_context  fscrypt_notsupp_has_permitted_context
1934 +#define fscrypt_inherit_context                fscrypt_notsupp_inherit_context
1935 +#define fscrypt_get_encryption_info    fscrypt_notsupp_get_encryption_info
1936 +#define fscrypt_put_encryption_info    fscrypt_notsupp_put_encryption_info
1937 +#define fscrypt_setup_filename         fscrypt_notsupp_setup_filename
1938 +#define fscrypt_free_filename          fscrypt_notsupp_free_filename
1939 +#define fscrypt_fname_encrypted_size   fscrypt_notsupp_fname_encrypted_size
1940 +#define fscrypt_fname_alloc_buffer     fscrypt_notsupp_fname_alloc_buffer
1941 +#define fscrypt_fname_free_buffer      fscrypt_notsupp_fname_free_buffer
1942 +#define fscrypt_fname_disk_to_usr      fscrypt_notsupp_fname_disk_to_usr
1943 +#define fscrypt_fname_usr_to_disk      fscrypt_notsupp_fname_usr_to_disk
1944 +#endif
1946  /* dir.c */
1947  extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
1948 @@ -2436,7 +2376,7 @@ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
1949  extern int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
1950                                 __u32 minor_hash,
1951                                 struct ext4_dir_entry_2 *dirent,
1952 -                               struct ext4_str *ent_name);
1953 +                               struct fscrypt_str *ent_name);
1954  extern void ext4_htree_free_dir_info(struct dir_private_info *p);
1955  extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1956                              struct buffer_head *bh,
1957 @@ -2624,7 +2564,7 @@ extern int ext4_generic_delete_entry(handle_t *handle,
1958                                      void *entry_buf,
1959                                      int buf_size,
1960                                      int csum_size);
1961 -extern int ext4_empty_dir(struct inode *inode);
1962 +extern bool ext4_empty_dir(struct inode *inode);
1964  /* resize.c */
1965  extern int ext4_group_add(struct super_block *sb,
1966 @@ -3106,7 +3046,7 @@ extern int ext4_delete_inline_entry(handle_t *handle,
1967                                     struct ext4_dir_entry_2 *de_del,
1968                                     struct buffer_head *bh,
1969                                     int *has_inline_data);
1970 -extern int empty_inline_dir(struct inode *dir, int *has_inline_data);
1971 +extern bool empty_inline_dir(struct inode *dir, int *has_inline_data);
1972  extern struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1973                                         struct ext4_dir_entry_2 **parent_de,
1974                                         int *retval);
1975 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
1976 deleted file mode 100644
1977 index 1f73c29..0000000
1978 --- a/fs/ext4/ext4_crypto.h
1979 +++ /dev/null
1980 @@ -1,159 +0,0 @@
1982 - * linux/fs/ext4/ext4_crypto.h
1983 - *
1984 - * Copyright (C) 2015, Google, Inc.
1985 - *
1986 - * This contains encryption header content for ext4
1987 - *
1988 - * Written by Michael Halcrow, 2015.
1989 - */
1991 -#ifndef _EXT4_CRYPTO_H
1992 -#define _EXT4_CRYPTO_H
1994 -#include <linux/fs.h>
1996 -#define EXT4_KEY_DESCRIPTOR_SIZE 8
1998 -/* Policy provided via an ioctl on the topmost directory */
1999 -struct ext4_encryption_policy {
2000 -       char version;
2001 -       char contents_encryption_mode;
2002 -       char filenames_encryption_mode;
2003 -       char flags;
2004 -       char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
2005 -} __attribute__((__packed__));
2007 -#define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
2008 -#define EXT4_KEY_DERIVATION_NONCE_SIZE 16
2010 -#define EXT4_POLICY_FLAGS_PAD_4                0x00
2011 -#define EXT4_POLICY_FLAGS_PAD_8                0x01
2012 -#define EXT4_POLICY_FLAGS_PAD_16       0x02
2013 -#define EXT4_POLICY_FLAGS_PAD_32       0x03
2014 -#define EXT4_POLICY_FLAGS_PAD_MASK     0x03
2015 -#define EXT4_POLICY_FLAGS_VALID                0x03
2017 -/**
2018 - * Encryption context for inode
2019 - *
2020 - * Protector format:
2021 - *  1 byte: Protector format (1 = this version)
2022 - *  1 byte: File contents encryption mode
2023 - *  1 byte: File names encryption mode
2024 - *  1 byte: Reserved
2025 - *  8 bytes: Master Key descriptor
2026 - *  16 bytes: Encryption Key derivation nonce
2027 - */
2028 -struct ext4_encryption_context {
2029 -       char format;
2030 -       char contents_encryption_mode;
2031 -       char filenames_encryption_mode;
2032 -       char flags;
2033 -       char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
2034 -       char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
2035 -} __attribute__((__packed__));
2037 -/* Encryption parameters */
2038 -#define EXT4_XTS_TWEAK_SIZE 16
2039 -#define EXT4_AES_128_ECB_KEY_SIZE 16
2040 -#define EXT4_AES_256_GCM_KEY_SIZE 32
2041 -#define EXT4_AES_256_CBC_KEY_SIZE 32
2042 -#define EXT4_AES_256_CTS_KEY_SIZE 32
2043 -#define EXT4_AES_256_XTS_KEY_SIZE 64
2044 -#define EXT4_MAX_KEY_SIZE 64
2046 -#define EXT4_KEY_DESC_PREFIX "ext4:"
2047 -#define EXT4_KEY_DESC_PREFIX_SIZE 5
2049 -/* This is passed in from userspace into the kernel keyring */
2050 -struct ext4_encryption_key {
2051 -        __u32 mode;
2052 -        char raw[EXT4_MAX_KEY_SIZE];
2053 -        __u32 size;
2054 -} __attribute__((__packed__));
2056 -struct ext4_crypt_info {
2057 -       char            ci_data_mode;
2058 -       char            ci_filename_mode;
2059 -       char            ci_flags;
2060 -       struct crypto_skcipher *ci_ctfm;
2061 -       struct key      *ci_keyring_key;
2062 -       char            ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
2065 -#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL             0x00000001
2066 -#define EXT4_WRITE_PATH_FL                           0x00000002
2068 -struct ext4_crypto_ctx {
2069 -       union {
2070 -               struct {
2071 -                       struct page *bounce_page;       /* Ciphertext page */
2072 -                       struct page *control_page;      /* Original page  */
2073 -               } w;
2074 -               struct {
2075 -                       struct bio *bio;
2076 -                       struct work_struct work;
2077 -               } r;
2078 -               struct list_head free_list;     /* Free list */
2079 -       };
2080 -       char flags;                      /* Flags */
2081 -       char mode;                       /* Encryption mode for tfm */
2084 -struct ext4_completion_result {
2085 -       struct completion completion;
2086 -       int res;
2089 -#define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
2090 -       struct ext4_completion_result ecr = { \
2091 -               COMPLETION_INITIALIZER((ecr).completion), 0 }
2093 -static inline int ext4_encryption_key_size(int mode)
2095 -       switch (mode) {
2096 -       case EXT4_ENCRYPTION_MODE_AES_256_XTS:
2097 -               return EXT4_AES_256_XTS_KEY_SIZE;
2098 -       case EXT4_ENCRYPTION_MODE_AES_256_GCM:
2099 -               return EXT4_AES_256_GCM_KEY_SIZE;
2100 -       case EXT4_ENCRYPTION_MODE_AES_256_CBC:
2101 -               return EXT4_AES_256_CBC_KEY_SIZE;
2102 -       case EXT4_ENCRYPTION_MODE_AES_256_CTS:
2103 -               return EXT4_AES_256_CTS_KEY_SIZE;
2104 -       default:
2105 -               BUG();
2106 -       }
2107 -       return 0;
2110 -#define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
2111 -#define EXT4_CRYPTO_BLOCK_SIZE         16
2112 -#define EXT4_FNAME_CRYPTO_DIGEST_SIZE  32
2114 -struct ext4_str {
2115 -       unsigned char *name;
2116 -       u32 len;
2119 -/**
2120 - * For encrypted symlinks, the ciphertext length is stored at the beginning
2121 - * of the string in little-endian format.
2122 - */
2123 -struct ext4_encrypted_symlink_data {
2124 -       __le16 len;
2125 -       char encrypted_path[1];
2126 -} __attribute__((__packed__));
2128 -/**
2129 - * This function is used to calculate the disk space required to
2130 - * store a filename of length l in encrypted symlink format.
2131 - */
2132 -static inline u32 encrypted_symlink_data_len(u32 l)
2134 -       if (l < EXT4_CRYPTO_BLOCK_SIZE)
2135 -               l = EXT4_CRYPTO_BLOCK_SIZE;
2136 -       return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
2139 -#endif /* _EXT4_CRYPTO_H */
2140 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
2141 index df44c87..4f615cd 100644
2142 --- a/fs/ext4/file.c
2143 +++ b/fs/ext4/file.c
2144 @@ -303,10 +303,10 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
2145         struct inode *inode = file->f_mapping->host;
2147         if (ext4_encrypted_inode(inode)) {
2148 -               int err = ext4_get_encryption_info(inode);
2149 +               int err = fscrypt_get_encryption_info(inode);
2150                 if (err)
2151                         return 0;
2152 -               if (ext4_encryption_info(inode) == NULL)
2153 +               if (!fscrypt_has_encryption_key(inode))
2154                         return -ENOKEY;
2155         }
2156         file_accessed(file);
2157 @@ -362,16 +362,16 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
2158                 }
2159         }
2160         if (ext4_encrypted_inode(inode)) {
2161 -               ret = ext4_get_encryption_info(inode);
2162 +               ret = fscrypt_get_encryption_info(inode);
2163                 if (ret)
2164                         return -EACCES;
2165 -               if (ext4_encryption_info(inode) == NULL)
2166 +               if (!fscrypt_has_encryption_key(inode))
2167                         return -ENOKEY;
2168         }
2170         dir = dget_parent(file_dentry(filp));
2171         if (ext4_encrypted_inode(d_inode(dir)) &&
2172 -           !ext4_is_child_context_consistent_with_parent(d_inode(dir), inode)) {
2173 +                       !fscrypt_has_permitted_context(d_inode(dir), inode)) {
2174                 ext4_warning(inode->i_sb,
2175                              "Inconsistent encryption contexts: %lu/%lu",
2176                              (unsigned long) d_inode(dir)->i_ino,
2177 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
2178 index 3da4cf8..35f3518 100644
2179 --- a/fs/ext4/ialloc.c
2180 +++ b/fs/ext4/ialloc.c
2181 @@ -767,10 +767,10 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
2182         if ((ext4_encrypted_inode(dir) ||
2183              DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
2184             (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
2185 -               err = ext4_get_encryption_info(dir);
2186 +               err = fscrypt_get_encryption_info(dir);
2187                 if (err)
2188                         return ERR_PTR(err);
2189 -               if (ext4_encryption_info(dir) == NULL)
2190 +               if (!fscrypt_has_encryption_key(dir))
2191                         return ERR_PTR(-EPERM);
2192                 if (!handle)
2193                         nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
2194 @@ -1115,7 +1115,8 @@ got:
2195         }
2197         if (encrypt) {
2198 -               err = ext4_inherit_context(dir, inode);
2199 +               /* give pointer to avoid set_context with journal ops. */
2200 +               err = fscrypt_inherit_context(dir, inode, &encrypt, true);
2201                 if (err)
2202                         goto fail_free_drop;
2203         }
2204 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
2205 index ff7538c..f74d5ee 100644
2206 --- a/fs/ext4/inline.c
2207 +++ b/fs/ext4/inline.c
2208 @@ -1326,7 +1326,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,
2209         struct ext4_iloc iloc;
2210         void *dir_buf = NULL;
2211         struct ext4_dir_entry_2 fake;
2212 -       struct ext4_str tmp_str;
2213 +       struct fscrypt_str tmp_str;
2215         ret = ext4_get_inode_loc(inode, &iloc);
2216         if (ret)
2217 @@ -1739,20 +1739,20 @@ ext4_get_inline_entry(struct inode *inode,
2218         return (struct ext4_dir_entry_2 *)(inline_pos + offset);
2221 -int empty_inline_dir(struct inode *dir, int *has_inline_data)
2222 +bool empty_inline_dir(struct inode *dir, int *has_inline_data)
2224         int err, inline_size;
2225         struct ext4_iloc iloc;
2226         void *inline_pos;
2227         unsigned int offset;
2228         struct ext4_dir_entry_2 *de;
2229 -       int ret = 1;
2230 +       bool ret = true;
2232         err = ext4_get_inode_loc(dir, &iloc);
2233         if (err) {
2234                 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
2235                                  err, dir->i_ino);
2236 -               return 1;
2237 +               return true;
2238         }
2240         down_read(&EXT4_I(dir)->xattr_sem);
2241 @@ -1766,7 +1766,7 @@ int empty_inline_dir(struct inode *dir, int *has_inline_data)
2242                 ext4_warning(dir->i_sb,
2243                              "bad inline directory (dir #%lu) - no `..'",
2244                              dir->i_ino);
2245 -               ret = 1;
2246 +               ret = true;
2247                 goto out;
2248         }
2250 @@ -1784,11 +1784,11 @@ int empty_inline_dir(struct inode *dir, int *has_inline_data)
2251                                      dir->i_ino, le32_to_cpu(de->inode),
2252                                      le16_to_cpu(de->rec_len), de->name_len,
2253                                      inline_size);
2254 -                       ret = 1;
2255 +                       ret = true;
2256                         goto out;
2257                 }
2258                 if (le32_to_cpu(de->inode)) {
2259 -                       ret = 0;
2260 +                       ret = false;
2261                         goto out;
2262                 }
2263                 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
2264 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
2265 index ea39d19..5a6277d 100644
2266 --- a/fs/ext4/inode.c
2267 +++ b/fs/ext4/inode.c
2268 @@ -392,7 +392,7 @@ int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
2269         int ret;
2271         if (ext4_encrypted_inode(inode))
2272 -               return ext4_encrypted_zeroout(inode, lblk, pblk, len);
2273 +               return fscrypt_zeroout_range(inode, lblk, pblk, len);
2275         ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
2276         if (ret > 0)
2277 @@ -1158,7 +1158,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
2278         if (unlikely(err))
2279                 page_zero_new_buffers(page, from, to);
2280         else if (decrypt)
2281 -               err = ext4_decrypt(page);
2282 +               err = fscrypt_decrypt_page(page);
2283         return err;
2285  #endif
2286 @@ -3735,9 +3735,9 @@ static int __ext4_block_zero_page_range(handle_t *handle,
2287                 if (S_ISREG(inode->i_mode) &&
2288                     ext4_encrypted_inode(inode)) {
2289                         /* We expect the key to be set. */
2290 -                       BUG_ON(!ext4_has_encryption_key(inode));
2291 +                       BUG_ON(!fscrypt_has_encryption_key(inode));
2292                         BUG_ON(blocksize != PAGE_SIZE);
2293 -                       WARN_ON_ONCE(ext4_decrypt(page));
2294 +                       WARN_ON_ONCE(fscrypt_decrypt_page(page));
2295                 }
2296         }
2297         if (ext4_should_journal_data(inode)) {
2298 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
2299 index b5a39b0..10686fd 100644
2300 --- a/fs/ext4/ioctl.c
2301 +++ b/fs/ext4/ioctl.c
2302 @@ -770,19 +770,13 @@ resizefs_out:
2303                 return ext4_ext_precache(inode);
2304         case EXT4_IOC_SET_ENCRYPTION_POLICY: {
2305  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2306 -               struct ext4_encryption_policy policy;
2307 -               int err = 0;
2308 +               struct fscrypt_policy policy;
2310                 if (copy_from_user(&policy,
2311 -                                  (struct ext4_encryption_policy __user *)arg,
2312 -                                  sizeof(policy))) {
2313 -                       err = -EFAULT;
2314 -                       goto encryption_policy_out;
2315 -               }
2317 -               err = ext4_process_policy(&policy, inode);
2318 -encryption_policy_out:
2319 -               return err;
2320 +                                  (struct fscrypt_policy __user *)arg,
2321 +                                  sizeof(policy)))
2322 +                       return -EFAULT;
2323 +               return fscrypt_process_policy(inode, &policy);
2324  #else
2325                 return -EOPNOTSUPP;
2326  #endif
2327 @@ -825,12 +819,12 @@ encryption_policy_out:
2328         }
2329         case EXT4_IOC_GET_ENCRYPTION_POLICY: {
2330  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2331 -               struct ext4_encryption_policy policy;
2332 +               struct fscrypt_policy policy;
2333                 int err = 0;
2335                 if (!ext4_encrypted_inode(inode))
2336                         return -ENOENT;
2337 -               err = ext4_get_policy(inode, &policy);
2338 +               err = fscrypt_get_policy(inode, &policy);
2339                 if (err)
2340                         return err;
2341                 if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
2342 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
2343 index 94d22e7..4637c43 100644
2344 --- a/fs/ext4/namei.c
2345 +++ b/fs/ext4/namei.c
2346 @@ -611,19 +611,19 @@ static struct stats dx_show_leaf(struct inode *dir,
2347  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2348                                 int len;
2349                                 char *name;
2350 -                               struct ext4_str fname_crypto_str
2351 -                                       = {.name = NULL, .len = 0};
2352 +                               struct fscrypt_str fname_crypto_str =
2353 +                                       FSTR_INIT(NULL, 0);
2354                                 int res = 0;
2356                                 name  = de->name;
2357                                 len = de->name_len;
2358 -                               if (ext4_encrypted_inode(inode))
2359 -                                       res = ext4_get_encryption_info(dir);
2360 +                               if (ext4_encrypted_inode(dir))
2361 +                                       res = fscrypt_get_encryption_info(dir);
2362                                 if (res) {
2363                                         printk(KERN_WARNING "Error setting up"
2364                                                " fname crypto: %d\n", res);
2365                                 }
2366 -                               if (ctx == NULL) {
2367 +                               if (!fscrypt_has_encryption_key(dir)) {
2368                                         /* Directory is not encrypted */
2369                                         ext4fs_dirhash(de->name,
2370                                                 de->name_len, &h);
2371 @@ -632,19 +632,21 @@ static struct stats dx_show_leaf(struct inode *dir,
2372                                                (unsigned) ((char *) de
2373                                                            - base));
2374                                 } else {
2375 +                                       struct fscrypt_str de_name =
2376 +                                               FSTR_INIT(name, len);
2378                                         /* Directory is encrypted */
2379 -                                       res = ext4_fname_crypto_alloc_buffer(
2380 -                                               ctx, de->name_len,
2381 +                                       res = fscrypt_fname_alloc_buffer(
2382 +                                               dir, len,
2383                                                 &fname_crypto_str);
2384 -                                       if (res < 0) {
2385 +                                       if (res < 0)
2386                                                 printk(KERN_WARNING "Error "
2387                                                         "allocating crypto "
2388                                                         "buffer--skipping "
2389                                                         "crypto\n");
2390 -                                               ctx = NULL;
2391 -                                       }
2392 -                                       res = ext4_fname_disk_to_usr(ctx, NULL, de,
2393 -                                                       &fname_crypto_str);
2394 +                                       res = fscrypt_fname_disk_to_usr(dir,
2395 +                                               0, 0, &de_name,
2396 +                                               &fname_crypto_str);
2397                                         if (res < 0) {
2398                                                 printk(KERN_WARNING "Error "
2399                                                         "converting filename "
2400 @@ -661,8 +663,8 @@ static struct stats dx_show_leaf(struct inode *dir,
2401                                         printk("%*.s:(E)%x.%u ", len, name,
2402                                                h.hash, (unsigned) ((char *) de
2403                                                                    - base));
2404 -                                       ext4_fname_crypto_free_buffer(
2405 -                                               &fname_crypto_str);
2406 +                                       fscrypt_fname_free_buffer(
2407 +                                                       &fname_crypto_str);
2408                                 }
2409  #else
2410                                 int len = de->name_len;
2411 @@ -951,7 +953,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2412         struct buffer_head *bh;
2413         struct ext4_dir_entry_2 *de, *top;
2414         int err = 0, count = 0;
2415 -       struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
2416 +       struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
2418         dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
2419                                                         (unsigned long)block));
2420 @@ -966,12 +968,12 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2421  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2422         /* Check if the directory is encrypted */
2423         if (ext4_encrypted_inode(dir)) {
2424 -               err = ext4_get_encryption_info(dir);
2425 +               err = fscrypt_get_encryption_info(dir);
2426                 if (err < 0) {
2427                         brelse(bh);
2428                         return err;
2429                 }
2430 -               err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
2431 +               err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
2432                                                      &fname_crypto_str);
2433                 if (err < 0) {
2434                         brelse(bh);
2435 @@ -1002,10 +1004,13 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2436                                    &tmp_str);
2437                 } else {
2438                         int save_len = fname_crypto_str.len;
2439 +                       struct fscrypt_str de_name = FSTR_INIT(de->name,
2440 +                                                               de->name_len);
2442                         /* Directory is encrypted */
2443 -                       err = ext4_fname_disk_to_usr(dir, hinfo, de,
2444 -                                                    &fname_crypto_str);
2445 +                       err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
2446 +                                       hinfo->minor_hash, &de_name,
2447 +                                       &fname_crypto_str);
2448                         if (err < 0) {
2449                                 count = err;
2450                                 goto errout;
2451 @@ -1024,7 +1029,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2452  errout:
2453         brelse(bh);
2454  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2455 -       ext4_fname_crypto_free_buffer(&fname_crypto_str);
2456 +       fscrypt_fname_free_buffer(&fname_crypto_str);
2457  #endif
2458         return count;
2460 @@ -1049,7 +1054,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2461         int count = 0;
2462         int ret, err;
2463         __u32 hashval;
2464 -       struct ext4_str tmp_str;
2465 +       struct fscrypt_str tmp_str;
2467         dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
2468                        start_hash, start_minor_hash));
2469 @@ -1562,26 +1567,23 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
2470         struct ext4_dir_entry_2 *de;
2471         struct buffer_head *bh;
2473 -       if (ext4_encrypted_inode(dir)) {
2474 -               int res = ext4_get_encryption_info(dir);
2475 +       if (ext4_encrypted_inode(dir)) {
2476 +               int res = fscrypt_get_encryption_info(dir);
2478                 /*
2479 -                * This should be a properly defined flag for
2480 -                * dentry->d_flags when we uplift this to the VFS.
2481 -                * d_fsdata is set to (void *) 1 if if the dentry is
2482 +                * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
2483                  * created while the directory was encrypted and we
2484 -                * don't have access to the key.
2485 +                * have access to the key.
2486                  */
2487 -              dentry->d_fsdata = NULL;
2488 -              if (ext4_encryption_info(dir))
2489 -                      dentry->d_fsdata = (void *) 1;
2490 -              d_set_d_op(dentry, &ext4_encrypted_d_ops);
2491 -              if (res && res != -ENOKEY)
2492 -                      return ERR_PTR(res);
2493 -       }
2494 +               if (fscrypt_has_encryption_key(dir))
2495 +                       fscrypt_set_encrypted_dentry(dentry);
2496 +               fscrypt_set_d_op(dentry);
2497 +               if (res && res != -ENOKEY)
2498 +                       return ERR_PTR(res);
2499 +       }
2501 -       if (dentry->d_name.len > EXT4_NAME_LEN)
2502 -               return ERR_PTR(-ENAMETOOLONG);
2503 +       if (dentry->d_name.len > EXT4_NAME_LEN)
2504 +              return ERR_PTR(-ENAMETOOLONG);
2506         bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2507         if (IS_ERR(bh))
2508 @@ -1608,11 +1610,9 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
2509                 }
2510                 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
2511                     (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
2512 -                   !ext4_is_child_context_consistent_with_parent(dir,
2513 -                                                                 inode)) {
2514 +                   !fscrypt_has_permitted_context(dir, inode)) {
2515                         int nokey = ext4_encrypted_inode(inode) &&
2516 -                               !ext4_encryption_info(inode);
2518 +                               !fscrypt_has_encryption_key(inode);
2519                         iput(inode);
2520                         if (nokey)
2521                                 return ERR_PTR(-ENOKEY);
2522 @@ -2689,30 +2689,30 @@ out_stop:
2523  /*
2524   * routine to check that the specified directory is empty (for rmdir)
2525   */
2526 -int ext4_empty_dir(struct inode *inode)
2527 +bool ext4_empty_dir(struct inode *inode)
2529         unsigned int offset;
2530         struct buffer_head *bh;
2531         struct ext4_dir_entry_2 *de, *de1;
2532         struct super_block *sb;
2533 -       int err = 0;
2535         if (ext4_has_inline_data(inode)) {
2536                 int has_inline_data = 1;
2537 +               int ret;
2539 -               err = empty_inline_dir(inode, &has_inline_data);
2540 +               ret = empty_inline_dir(inode, &has_inline_data);
2541                 if (has_inline_data)
2542 -                       return err;
2543 +                       return ret;
2544         }
2546         sb = inode->i_sb;
2547         if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2548                 EXT4_ERROR_INODE(inode, "invalid size");
2549 -               return 1;
2550 +               return true;
2551         }
2552         bh = ext4_read_dirblock(inode, 0, EITHER);
2553         if (IS_ERR(bh))
2554 -               return 1;
2555 +               return true;
2557         de = (struct ext4_dir_entry_2 *) bh->b_data;
2558         de1 = ext4_next_entry(de, sb->s_blocksize);
2559 @@ -2721,7 +2721,7 @@ int ext4_empty_dir(struct inode *inode)
2560                         strcmp(".", de->name) || strcmp("..", de1->name)) {
2561                 ext4_warning_inode(inode, "directory missing '.' and/or '..'");
2562                 brelse(bh);
2563 -               return 1;
2564 +               return true;
2565         }
2566         offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2567                  ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2568 @@ -2729,12 +2729,11 @@ int ext4_empty_dir(struct inode *inode)
2569         while (offset < inode->i_size) {
2570                 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2571                         unsigned int lblock;
2572 -                       err = 0;
2573                         brelse(bh);
2574                         lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2575                         bh = ext4_read_dirblock(inode, lblock, EITHER);
2576                         if (IS_ERR(bh))
2577 -                               return 1;
2578 +                               return true;
2579                         de = (struct ext4_dir_entry_2 *) bh->b_data;
2580                 }
2581                 if (ext4_check_dir_entry(inode, NULL, de, bh,
2582 @@ -2746,13 +2745,13 @@ int ext4_empty_dir(struct inode *inode)
2583                 }
2584                 if (le32_to_cpu(de->inode)) {
2585                         brelse(bh);
2586 -                       return 0;
2587 +                       return false;
2588                 }
2589                 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2590                 de = ext4_next_entry(de, sb->s_blocksize);
2591         }
2592         brelse(bh);
2593 -       return 1;
2594 +       return true;
2597  /*
2598 @@ -3075,8 +3074,8 @@ static int ext4_symlink(struct inode *dir,
2599         int err, len = strlen(symname);
2600         int credits;
2601         bool encryption_required;
2602 -       struct ext4_str disk_link;
2603 -       struct ext4_encrypted_symlink_data *sd = NULL;
2604 +       struct fscrypt_str disk_link;
2605 +       struct fscrypt_symlink_data *sd = NULL;
2607         disk_link.len = len + 1;
2608         disk_link.name = (char *) symname;
2609 @@ -3084,13 +3083,13 @@ static int ext4_symlink(struct inode *dir,
2610         encryption_required = (ext4_encrypted_inode(dir) ||
2611                                DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
2612         if (encryption_required) {
2613 -               err = ext4_get_encryption_info(dir);
2614 +               err = fscrypt_get_encryption_info(dir);
2615                 if (err)
2616                         return err;
2617 -               if (ext4_encryption_info(dir) == NULL)
2618 +               if (!fscrypt_has_encryption_key(dir))
2619                         return -EPERM;
2620 -               disk_link.len = (ext4_fname_encrypted_size(dir, len) +
2621 -                                sizeof(struct ext4_encrypted_symlink_data));
2622 +               disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
2623 +                                sizeof(struct fscrypt_symlink_data));
2624                 sd = kzalloc(disk_link.len, GFP_KERNEL);
2625                 if (!sd)
2626                         return -ENOMEM;
2627 @@ -3138,13 +3137,12 @@ static int ext4_symlink(struct inode *dir,
2629         if (encryption_required) {
2630                 struct qstr istr;
2631 -               struct ext4_str ostr;
2632 +               struct fscrypt_str ostr =
2633 +                       FSTR_INIT(sd->encrypted_path, disk_link.len);
2635                 istr.name = (const unsigned char *) symname;
2636                 istr.len = len;
2637 -               ostr.name = sd->encrypted_path;
2638 -               ostr.len = disk_link.len;
2639 -               err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
2640 +               err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
2641                 if (err < 0)
2642                         goto err_drop_inode;
2643                 sd->len = cpu_to_le16(ostr.len);
2644 @@ -3233,7 +3231,7 @@ static int ext4_link(struct dentry *old_dentry,
2645         if (inode->i_nlink >= EXT4_LINK_MAX)
2646                 return -EMLINK;
2647         if (ext4_encrypted_inode(dir) &&
2648 -           !ext4_is_child_context_consistent_with_parent(dir, inode))
2649 +                       !fscrypt_has_permitted_context(dir, inode))
2650                 return -EPERM;
2652         if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
2653 @@ -3556,8 +3554,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2655         if ((old.dir != new.dir) &&
2656             ext4_encrypted_inode(new.dir) &&
2657 -           !ext4_is_child_context_consistent_with_parent(new.dir,
2658 -                                                         old.inode)) {
2659 +           !fscrypt_has_permitted_context(new.dir, old.inode)) {
2660                 retval = -EPERM;
2661                 goto end_rename;
2662         }
2663 @@ -3729,10 +3726,8 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
2664         if ((ext4_encrypted_inode(old_dir) ||
2665              ext4_encrypted_inode(new_dir)) &&
2666             (old_dir != new_dir) &&
2667 -           (!ext4_is_child_context_consistent_with_parent(new_dir,
2668 -                                                          old.inode) ||
2669 -            !ext4_is_child_context_consistent_with_parent(old_dir,
2670 -                                                          new.inode)))
2671 +           (!fscrypt_has_permitted_context(new_dir, old.inode) ||
2672 +            !fscrypt_has_permitted_context(old_dir, new.inode)))
2673                 return -EPERM;
2675         if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
2676 diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
2677 index 2a01df9..5ad05af 100644
2678 --- a/fs/ext4/page-io.c
2679 +++ b/fs/ext4/page-io.c
2680 @@ -24,6 +24,7 @@
2681  #include <linux/slab.h>
2682  #include <linux/mm.h>
2683  #include <linux/backing-dev.h>
2684 +#include <linux/fscrypto.h>
2686  #include "ext4_jbd2.h"
2687  #include "xattr.h"
2688 @@ -67,7 +68,6 @@ static void ext4_finish_bio(struct bio *bio)
2689                 struct page *page = bvec->bv_page;
2690  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2691                 struct page *data_page = NULL;
2692 -               struct ext4_crypto_ctx *ctx = NULL;
2693  #endif
2694                 struct buffer_head *bh, *head;
2695                 unsigned bio_start = bvec->bv_offset;
2696 @@ -82,8 +82,7 @@ static void ext4_finish_bio(struct bio *bio)
2697                 if (!page->mapping) {
2698                         /* The bounce data pages are unmapped. */
2699                         data_page = page;
2700 -                       ctx = (struct ext4_crypto_ctx *)page_private(data_page);
2701 -                       page = ctx->w.control_page;
2702 +                       fscrypt_pullback_bio_page(&page, false);
2703                 }
2704  #endif
2706 @@ -113,8 +112,8 @@ static void ext4_finish_bio(struct bio *bio)
2707                 local_irq_restore(flags);
2708                 if (!under_io) {
2709  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2710 -                       if (ctx)
2711 -                               ext4_restore_control_page(data_page);
2712 +                       if (data_page)
2713 +                               fscrypt_restore_control_page(data_page);
2714  #endif
2715                         end_page_writeback(page);
2716                 }
2717 @@ -472,7 +471,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
2718                 gfp_t gfp_flags = GFP_NOFS;
2720         retry_encrypt:
2721 -               data_page = ext4_encrypt(inode, page, gfp_flags);
2722 +               data_page = fscrypt_encrypt_page(inode, page, gfp_flags);
2723                 if (IS_ERR(data_page)) {
2724                         ret = PTR_ERR(data_page);
2725                         if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
2726 @@ -510,7 +509,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
2727         if (ret) {
2728         out:
2729                 if (data_page)
2730 -                       ext4_restore_control_page(data_page);
2731 +                       fscrypt_restore_control_page(data_page);
2732                 printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
2733                 redirty_page_for_writepage(wbc, page);
2734                 do {
2735 diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
2736 index e24ec3b..18b2cf2 100644
2737 --- a/fs/ext4/readpage.c
2738 +++ b/fs/ext4/readpage.c
2739 @@ -46,37 +46,6 @@
2741  #include "ext4.h"
2744 - * Call ext4_decrypt on every single page, reusing the encryption
2745 - * context.
2746 - */
2747 -static void completion_pages(struct work_struct *work)
2749 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2750 -       struct ext4_crypto_ctx *ctx =
2751 -               container_of(work, struct ext4_crypto_ctx, r.work);
2752 -       struct bio      *bio    = ctx->r.bio;
2753 -       struct bio_vec  *bv;
2754 -       int             i;
2756 -       bio_for_each_segment_all(bv, bio, i) {
2757 -               struct page *page = bv->bv_page;
2759 -               int ret = ext4_decrypt(page);
2760 -               if (ret) {
2761 -                       WARN_ON_ONCE(1);
2762 -                       SetPageError(page);
2763 -               } else
2764 -                       SetPageUptodate(page);
2765 -               unlock_page(page);
2766 -       }
2767 -       ext4_release_crypto_ctx(ctx);
2768 -       bio_put(bio);
2769 -#else
2770 -       BUG();
2771 -#endif
2774  static inline bool ext4_bio_encrypted(struct bio *bio)
2776  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2777 @@ -104,14 +73,10 @@ static void mpage_end_io(struct bio *bio)
2778         int i;
2780         if (ext4_bio_encrypted(bio)) {
2781 -               struct ext4_crypto_ctx *ctx = bio->bi_private;
2783                 if (bio->bi_error) {
2784 -                       ext4_release_crypto_ctx(ctx);
2785 +                       fscrypt_release_ctx(bio->bi_private);
2786                 } else {
2787 -                       INIT_WORK(&ctx->r.work, completion_pages);
2788 -                       ctx->r.bio = bio;
2789 -                       queue_work(ext4_read_workqueue, &ctx->r.work);
2790 +                       fscrypt_decrypt_bio_pages(bio->bi_private, bio);
2791                         return;
2792                 }
2793         }
2794 @@ -274,11 +239,11 @@ int ext4_mpage_readpages(struct address_space *mapping,
2795                         bio = NULL;
2796                 }
2797                 if (bio == NULL) {
2798 -                       struct ext4_crypto_ctx *ctx = NULL;
2799 +                       struct fscrypt_ctx *ctx = NULL;
2801                         if (ext4_encrypted_inode(inode) &&
2802                             S_ISREG(inode->i_mode)) {
2803 -                               ctx = ext4_get_crypto_ctx(inode, GFP_NOFS);
2804 +                               ctx = fscrypt_get_ctx(inode, GFP_NOFS);
2805                                 if (IS_ERR(ctx))
2806                                         goto set_error_page;
2807                         }
2808 @@ -286,7 +251,7 @@ int ext4_mpage_readpages(struct address_space *mapping,
2809                                 min_t(int, nr_pages, BIO_MAX_PAGES));
2810                         if (!bio) {
2811                                 if (ctx)
2812 -                                       ext4_release_crypto_ctx(ctx);
2813 +                                       fscrypt_release_ctx(ctx);
2814                                 goto set_error_page;
2815                         }
2816                         bio->bi_bdev = bdev;
2817 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
2818 index 13c49af7..1e3fd5c 100644
2819 --- a/fs/ext4/super.c
2820 +++ b/fs/ext4/super.c
2821 @@ -945,9 +945,6 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
2822         ei->i_datasync_tid = 0;
2823         atomic_set(&ei->i_unwritten, 0);
2824         INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
2825 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2826 -       ei->i_crypt_info = NULL;
2827 -#endif
2828         return &ei->vfs_inode;
2831 @@ -1026,8 +1023,7 @@ void ext4_clear_inode(struct inode *inode)
2832                 EXT4_I(inode)->jinode = NULL;
2833         }
2834  #ifdef CONFIG_EXT4_FS_ENCRYPTION
2835 -       if (EXT4_I(inode)->i_crypt_info)
2836 -               ext4_free_encryption_info(inode, EXT4_I(inode)->i_crypt_info);
2837 +       fscrypt_put_encryption_info(inode, NULL);
2838  #endif
2841 @@ -1094,6 +1090,90 @@ static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
2842         return try_to_free_buffers(page);
2845 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
2846 +static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
2848 +       return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2849 +                                EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
2852 +static int ext4_key_prefix(struct inode *inode, u8 **key)
2854 +       *key = EXT4_SB(inode->i_sb)->key_prefix;
2855 +       return EXT4_SB(inode->i_sb)->key_prefix_size;
2858 +static int ext4_prepare_context(struct inode *inode)
2860 +       return ext4_convert_inline_data(inode);
2863 +static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
2864 +                                                       void *fs_data)
2866 +       handle_t *handle;
2867 +       int res, res2;
2869 +       /* fs_data is null when internally used. */
2870 +       if (fs_data) {
2871 +               res  = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2872 +                               EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
2873 +                               len, 0);
2874 +               if (!res) {
2875 +                       ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
2876 +                       ext4_clear_inode_state(inode,
2877 +                                       EXT4_STATE_MAY_INLINE_DATA);
2878 +               }
2879 +               return res;
2880 +       }
2882 +       handle = ext4_journal_start(inode, EXT4_HT_MISC,
2883 +                       ext4_jbd2_credits_xattr(inode));
2884 +       if (IS_ERR(handle))
2885 +               return PTR_ERR(handle);
2887 +       res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2888 +                       EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
2889 +                       len, 0);
2890 +       if (!res) {
2891 +               ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
2892 +               res = ext4_mark_inode_dirty(handle, inode);
2893 +               if (res)
2894 +                       EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
2895 +       }
2896 +       res2 = ext4_journal_stop(handle);
2897 +       if (!res)
2898 +               res = res2;
2899 +       return res;
2902 +static int ext4_dummy_context(struct inode *inode)
2904 +       return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
2907 +static unsigned ext4_max_namelen(struct inode *inode)
2909 +       return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
2910 +               EXT4_NAME_LEN;
2913 +static struct fscrypt_operations ext4_cryptops = {
2914 +       .get_context            = ext4_get_context,
2915 +       .key_prefix             = ext4_key_prefix,
2916 +       .prepare_context        = ext4_prepare_context,
2917 +       .set_context            = ext4_set_context,
2918 +       .dummy_context          = ext4_dummy_context,
2919 +       .is_encrypted           = ext4_encrypted_inode,
2920 +       .empty_dir              = ext4_empty_dir,
2921 +       .max_namelen            = ext4_max_namelen,
2923 +#else
2924 +static struct fscrypt_operations ext4_cryptops = {
2925 +       .is_encrypted           = ext4_encrypted_inode,
2927 +#endif
2929  #ifdef CONFIG_QUOTA
2930  static char *quotatypes[] = INITQFNAMES;
2931  #define QTYPE2NAME(t) (quotatypes[t])
2932 @@ -3693,6 +3773,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
2933         sb->s_op = &ext4_sops;
2934         sb->s_export_op = &ext4_export_ops;
2935         sb->s_xattr = ext4_xattr_handlers;
2936 +       sb->s_cop = &ext4_cryptops;
2937  #ifdef CONFIG_QUOTA
2938         sb->dq_op = &ext4_quota_operations;
2939         if (ext4_has_feature_quota(sb))
2940 @@ -4003,6 +4084,11 @@ no_journal:
2941         ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
2943         kfree(orig_data);
2944 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
2945 +       memcpy(sbi->key_prefix, EXT4_KEY_DESC_PREFIX,
2946 +                               EXT4_KEY_DESC_PREFIX_SIZE);
2947 +       sbi->key_prefix_size = EXT4_KEY_DESC_PREFIX_SIZE;
2948 +#endif
2949         return 0;
2951  cantfind_ext4:
2952 @@ -5431,7 +5517,6 @@ out5:
2954  static void __exit ext4_exit_fs(void)
2956 -       ext4_exit_crypto();
2957         ext4_destroy_lazyinit_thread();
2958         unregister_as_ext2();
2959         unregister_as_ext3();
2960 diff --git a/fs/ext4/symlink.c b/fs/ext4/symlink.c
2961 index 75ed5c2..4d83d9e 100644
2962 --- a/fs/ext4/symlink.c
2963 +++ b/fs/ext4/symlink.c
2964 @@ -22,23 +22,22 @@
2965  #include "ext4.h"
2966  #include "xattr.h"
2968 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2969  static const char *ext4_encrypted_get_link(struct dentry *dentry,
2970                                            struct inode *inode,
2971                                            struct delayed_call *done)
2973         struct page *cpage = NULL;
2974         char *caddr, *paddr = NULL;
2975 -       struct ext4_str cstr, pstr;
2976 -       struct ext4_encrypted_symlink_data *sd;
2977 +       struct fscrypt_str cstr, pstr;
2978 +       struct fscrypt_symlink_data *sd;
2979         loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
2980         int res;
2981 -       u32 plen, max_size = inode->i_sb->s_blocksize;
2982 +       u32 max_size = inode->i_sb->s_blocksize;
2984         if (!dentry)
2985                 return ERR_PTR(-ECHILD);
2987 -       res = ext4_get_encryption_info(inode);
2988 +       res = fscrypt_get_encryption_info(inode);
2989         if (res)
2990                 return ERR_PTR(res);
2992 @@ -54,30 +53,27 @@ static const char *ext4_encrypted_get_link(struct dentry *dentry,
2993         }
2995         /* Symlink is encrypted */
2996 -       sd = (struct ext4_encrypted_symlink_data *)caddr;
2997 +       sd = (struct fscrypt_symlink_data *)caddr;
2998         cstr.name = sd->encrypted_path;
2999         cstr.len  = le16_to_cpu(sd->len);
3000 -       if ((cstr.len +
3001 -            sizeof(struct ext4_encrypted_symlink_data) - 1) >
3002 -           max_size) {
3003 +       if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
3004                 /* Symlink data on the disk is corrupted */
3005                 res = -EFSCORRUPTED;
3006                 goto errout;
3007         }
3008 -       plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
3009 -               EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
3010 -       paddr = kmalloc(plen + 1, GFP_NOFS);
3011 -       if (!paddr) {
3012 -               res = -ENOMEM;
3014 +       res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
3015 +       if (res)
3016                 goto errout;
3017 -       }
3018 -       pstr.name = paddr;
3019 -       pstr.len = plen;
3020 -       res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
3022 +       res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
3023         if (res < 0)
3024                 goto errout;
3026 +       paddr = pstr.name;
3028         /* Null-terminate the name */
3029 -       if (res <= plen)
3030 +       if (res <= pstr.len)
3031                 paddr[res] = '\0';
3032         if (cpage)
3033                 put_page(cpage);
3034 @@ -99,7 +95,6 @@ const struct inode_operations ext4_encrypted_symlink_inode_operations = {
3035         .listxattr      = ext4_listxattr,
3036         .removexattr    = generic_removexattr,
3037  };
3038 -#endif
3040  const struct inode_operations ext4_symlink_inode_operations = {
3041         .readlink       = generic_readlink,