1 // SPDX-License-Identifier: GPL-2.0
3 * This contains encryption functions for per-file encryption.
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility
8 * Written by Michael Halcrow, 2014.
10 * Filename encryption additions
11 * Uday Savagaonkar, 2014
12 * Encryption policy handling additions
13 * Ildar Muslukhov, 2014
14 * Add llcrypt_pullback_bio_page()
17 * This has not yet undergone a rigorous security audit.
19 * The usage of AES-XTS should conform to recommendations in NIST
20 * Special Publication 800-38E and IEEE P1619/D16.
23 * Linux commit 219d54332a09
27 #include <linux/pagemap.h>
28 #include <linux/module.h>
29 #include <linux/bio.h>
30 #include <linux/namei.h>
31 #include <lustre_compat.h>
32 #include "llcrypt_private.h"
34 static void __llcrypt_decrypt_bio(struct bio *bio, bool done)
37 #ifdef HAVE_BVEC_ITER_ALL
38 struct bvec_iter_all iter_all;
43 bio_for_each_segment_all(bv, bio, iter_all) {
44 struct page *page = bv->bv_page;
45 int ret = llcrypt_decrypt_pagecache_blocks(page, bv->bv_len,
50 SetPageUptodate(page);
56 void llcrypt_decrypt_bio(struct bio *bio)
58 __llcrypt_decrypt_bio(bio, false);
60 EXPORT_SYMBOL(llcrypt_decrypt_bio);
62 static void completion_pages(struct work_struct *work)
64 struct llcrypt_ctx *ctx = container_of(work, struct llcrypt_ctx, work);
65 struct bio *bio = ctx->bio;
67 __llcrypt_decrypt_bio(bio, true);
68 llcrypt_release_ctx(ctx);
72 void llcrypt_enqueue_decrypt_bio(struct llcrypt_ctx *ctx, struct bio *bio)
74 INIT_WORK(&ctx->work, completion_pages);
76 llcrypt_enqueue_decrypt_work(&ctx->work);
78 EXPORT_SYMBOL(llcrypt_enqueue_decrypt_bio);
80 int llcrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
81 sector_t pblk, unsigned int len)
83 const unsigned int blockbits = inode->i_blkbits;
84 const unsigned int blocksize = 1 << blockbits;
85 struct page *ciphertext_page;
89 ciphertext_page = llcrypt_alloc_bounce_page(GFP_NOWAIT);
94 err = llcrypt_crypt_block(inode, FS_ENCRYPT, lblk,
95 ZERO_PAGE(0), ciphertext_page,
96 blocksize, 0, GFP_NOFS);
100 bio = cfs_bio_alloc(inode->i_sb->s_bdev, 1, REQ_OP_WRITE,
106 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
107 ret = bio_add_page(bio, ciphertext_page, blocksize, 0);
108 if (WARN_ON(ret != blocksize)) {
109 /* should never happen! */
114 err = submit_bio_wait(bio);
115 if (err == 0 && bio->bi_status)
125 llcrypt_free_bounce_page(ciphertext_page);
128 EXPORT_SYMBOL(llcrypt_zeroout_range);