fscrypt: remove fscrypt_set_test_dummy_encryption()
[linux-2.6-block.git] / fs / crypto / bio.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
58ae7468 2/*
f262ca7d
EB
3 * Utility functions for file contents encryption/decryption on
4 * block device-based filesystems.
58ae7468
RW
5 *
6 * Copyright (C) 2015, Google, Inc.
7 * Copyright (C) 2015, Motorola Mobility
58ae7468
RW
8 */
9
10#include <linux/pagemap.h>
11#include <linux/module.h>
12#include <linux/bio.h>
13#include <linux/namei.h>
14#include "fscrypt_private.h"
15
f262ca7d
EB
16/**
17 * fscrypt_decrypt_bio() - decrypt the contents of a bio
18 * @bio: the bio to decrypt
19 *
20 * Decrypt the contents of a "read" bio following successful completion of the
21 * underlying disk read. The bio must be reading a whole number of blocks of an
22 * encrypted file directly into the page cache. If the bio is reading the
23 * ciphertext into bounce pages instead of the page cache (for example, because
24 * the file is also compressed, so decompression is required after decryption),
25 * then this function isn't applicable. This function may sleep, so it must be
26 * called from a workqueue rather than from the bio's bi_end_io callback.
27 *
28 * This function sets PG_error on any pages that contain any blocks that failed
29 * to be decrypted. The filesystem must not mark such pages uptodate.
30 */
1565bdad 31void fscrypt_decrypt_bio(struct bio *bio)
58ae7468 32{
58ae7468 33 struct bio_vec *bv;
6dc4f100 34 struct bvec_iter_all iter_all;
58ae7468 35
2b070cfe 36 bio_for_each_segment_all(bv, bio, iter_all) {
58ae7468 37 struct page *page = bv->bv_page;
ffceeefb
EB
38 int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
39 bv->bv_offset);
ff5d3a97 40 if (ret)
58ae7468 41 SetPageError(page);
58ae7468 42 }
0cb8dae4 43}
0cb8dae4
EB
44EXPORT_SYMBOL(fscrypt_decrypt_bio);
45
5fee3609
ST
46static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
47 pgoff_t lblk, sector_t pblk,
48 unsigned int len)
49{
50 const unsigned int blockbits = inode->i_blkbits;
51 const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
52 struct bio *bio;
53 int ret, err = 0;
54 int num_pages = 0;
55
56 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
07888c66
CH
57 bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
58 GFP_NOFS);
5fee3609
ST
59
60 while (len) {
61 unsigned int blocks_this_page = min(len, blocks_per_page);
62 unsigned int bytes_this_page = blocks_this_page << blockbits;
63
64 if (num_pages == 0) {
65 fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
5fee3609
ST
66 bio->bi_iter.bi_sector =
67 pblk << (blockbits - SECTOR_SHIFT);
5fee3609
ST
68 }
69 ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
70 if (WARN_ON(ret != bytes_this_page)) {
71 err = -EIO;
72 goto out;
73 }
74 num_pages++;
75 len -= blocks_this_page;
76 lblk += blocks_this_page;
77 pblk += blocks_this_page;
a8affc03 78 if (num_pages == BIO_MAX_VECS || !len ||
5fee3609
ST
79 !fscrypt_mergeable_bio(bio, inode, lblk)) {
80 err = submit_bio_wait(bio);
81 if (err)
82 goto out;
a7c50c94 83 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
5fee3609
ST
84 num_pages = 0;
85 }
86 }
87out:
88 bio_put(bio);
89 return err;
90}
91
796f12d7
EB
92/**
93 * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
94 * @inode: the file's inode
95 * @lblk: the first file logical block to zero out
96 * @pblk: the first filesystem physical block to zero out
97 * @len: number of blocks to zero out
98 *
99 * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
100 * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
101 * both logically and physically contiguous. It's also assumed that the
102 * filesystem only uses a single block device, ->s_bdev.
103 *
104 * Note that since each block uses a different IV, this involves writing a
105 * different ciphertext to each block; we can't simply reuse the same one.
106 *
107 * Return: 0 on success; -errno on failure.
108 */
58ae7468 109int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
796f12d7 110 sector_t pblk, unsigned int len)
58ae7468 111{
930d4539
EB
112 const unsigned int blockbits = inode->i_blkbits;
113 const unsigned int blocksize = 1 << blockbits;
796f12d7
EB
114 const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
115 const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
116 struct page *pages[16]; /* write up to 16 pages at a time */
117 unsigned int nr_pages;
118 unsigned int i;
119 unsigned int offset;
58ae7468 120 struct bio *bio;
796f12d7 121 int ret, err;
58ae7468 122
796f12d7
EB
123 if (len == 0)
124 return 0;
58ae7468 125
5fee3609
ST
126 if (fscrypt_inode_uses_inline_crypto(inode))
127 return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
128 len);
129
a8affc03 130 BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
796f12d7
EB
131 nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
132 (len + blocks_per_page - 1) >> blocks_per_page_bits);
58ae7468 133
796f12d7
EB
134 /*
135 * We need at least one page for ciphertext. Allocate the first one
136 * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
137 *
138 * Any additional page allocations are allowed to fail, as they only
139 * help performance, and waiting on the mempool for them could deadlock.
140 */
141 for (i = 0; i < nr_pages; i++) {
142 pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
143 GFP_NOWAIT | __GFP_NOWARN);
144 if (!pages[i])
145 break;
146 }
147 nr_pages = i;
148 if (WARN_ON(nr_pages <= 0))
149 return -EINVAL;
150
151 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
07888c66 152 bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
796f12d7
EB
153
154 do {
930d4539 155 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
796f12d7
EB
156
157 i = 0;
158 offset = 0;
159 do {
160 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
161 ZERO_PAGE(0), pages[i],
162 blocksize, offset, GFP_NOFS);
163 if (err)
164 goto out;
165 lblk++;
166 pblk++;
167 len--;
168 offset += blocksize;
169 if (offset == PAGE_SIZE || len == 0) {
170 ret = bio_add_page(bio, pages[i++], offset, 0);
171 if (WARN_ON(ret != offset)) {
172 err = -EIO;
173 goto out;
174 }
175 offset = 0;
176 }
177 } while (i != nr_pages && len != 0);
178
58ae7468 179 err = submit_bio_wait(bio);
58ae7468 180 if (err)
796f12d7 181 goto out;
a7c50c94 182 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
796f12d7 183 } while (len != 0);
58ae7468 184 err = 0;
796f12d7
EB
185out:
186 bio_put(bio);
187 for (i = 0; i < nr_pages; i++)
188 fscrypt_free_bounce_page(pages[i]);
58ae7468
RW
189 return err;
190}
191EXPORT_SYMBOL(fscrypt_zeroout_range);