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 fscrypt_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 #include <linux/pagemap.h>
24 #include <linux/module.h>
25 #include <linux/bio.h>
26 #include <linux/namei.h>
27 #include "fscrypt_private.h"
29 static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
32 struct bvec_iter_all iter_all;
34 bio_for_each_segment_all(bv, bio, iter_all) {
35 struct page *page = bv->bv_page;
36 int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
41 SetPageUptodate(page);
47 void fscrypt_decrypt_bio(struct bio *bio)
49 __fscrypt_decrypt_bio(bio, false);
51 EXPORT_SYMBOL(fscrypt_decrypt_bio);
53 static void completion_pages(struct work_struct *work)
55 struct fscrypt_ctx *ctx = container_of(work, struct fscrypt_ctx, work);
56 struct bio *bio = ctx->bio;
58 __fscrypt_decrypt_bio(bio, true);
59 fscrypt_release_ctx(ctx);
63 void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
65 INIT_WORK(&ctx->work, completion_pages);
67 fscrypt_enqueue_decrypt_work(&ctx->work);
69 EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
71 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
72 sector_t pblk, unsigned int len)
74 const unsigned int blockbits = inode->i_blkbits;
75 const unsigned int blocksize = 1 << blockbits;
76 struct page *ciphertext_page;
80 ciphertext_page = fscrypt_alloc_bounce_page(GFP_NOWAIT);
85 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
86 ZERO_PAGE(0), ciphertext_page,
87 blocksize, 0, GFP_NOFS);
91 bio = bio_alloc(GFP_NOWAIT, 1);
96 bio_set_dev(bio, inode->i_sb->s_bdev);
97 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
98 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
99 ret = bio_add_page(bio, ciphertext_page, blocksize, 0);
100 if (WARN_ON(ret != blocksize)) {
101 /* should never happen! */
106 err = submit_bio_wait(bio);
107 if (err == 0 && bio->bi_status)
117 fscrypt_free_bounce_page(ciphertext_page);
120 EXPORT_SYMBOL(fscrypt_zeroout_range);