Merge tag '9p-6.4-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[linux-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 *
14db0b3c
EB
28 * Return: %true on success; %false on failure. On failure, bio->bi_status is
29 * also set to an error status.
f262ca7d 30 */
14db0b3c 31bool fscrypt_decrypt_bio(struct bio *bio)
58ae7468 32{
51e4e315 33 struct folio_iter fi;
58ae7468 34
51e4e315
EB
35 bio_for_each_folio_all(fi, bio) {
36 int err = fscrypt_decrypt_pagecache_blocks(fi.folio, fi.length,
37 fi.offset);
14db0b3c
EB
38
39 if (err) {
40 bio->bi_status = errno_to_blk_status(err);
41 return false;
42 }
58ae7468 43 }
14db0b3c 44 return true;
0cb8dae4 45}
0cb8dae4
EB
46EXPORT_SYMBOL(fscrypt_decrypt_bio);
47
5fee3609
ST
48static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
49 pgoff_t lblk, sector_t pblk,
50 unsigned int len)
51{
52 const unsigned int blockbits = inode->i_blkbits;
53 const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
54 struct bio *bio;
55 int ret, err = 0;
56 int num_pages = 0;
57
58 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
07888c66
CH
59 bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
60 GFP_NOFS);
5fee3609
ST
61
62 while (len) {
63 unsigned int blocks_this_page = min(len, blocks_per_page);
64 unsigned int bytes_this_page = blocks_this_page << blockbits;
65
66 if (num_pages == 0) {
67 fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
5fee3609
ST
68 bio->bi_iter.bi_sector =
69 pblk << (blockbits - SECTOR_SHIFT);
5fee3609
ST
70 }
71 ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
41b2ad80 72 if (WARN_ON_ONCE(ret != bytes_this_page)) {
5fee3609
ST
73 err = -EIO;
74 goto out;
75 }
76 num_pages++;
77 len -= blocks_this_page;
78 lblk += blocks_this_page;
79 pblk += blocks_this_page;
a8affc03 80 if (num_pages == BIO_MAX_VECS || !len ||
5fee3609
ST
81 !fscrypt_mergeable_bio(bio, inode, lblk)) {
82 err = submit_bio_wait(bio);
83 if (err)
84 goto out;
a7c50c94 85 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
5fee3609
ST
86 num_pages = 0;
87 }
88 }
89out:
90 bio_put(bio);
91 return err;
92}
93
796f12d7
EB
94/**
95 * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
96 * @inode: the file's inode
97 * @lblk: the first file logical block to zero out
98 * @pblk: the first filesystem physical block to zero out
99 * @len: number of blocks to zero out
100 *
101 * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
102 * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
103 * both logically and physically contiguous. It's also assumed that the
104 * filesystem only uses a single block device, ->s_bdev.
105 *
106 * Note that since each block uses a different IV, this involves writing a
107 * different ciphertext to each block; we can't simply reuse the same one.
108 *
109 * Return: 0 on success; -errno on failure.
110 */
58ae7468 111int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
796f12d7 112 sector_t pblk, unsigned int len)
58ae7468 113{
930d4539
EB
114 const unsigned int blockbits = inode->i_blkbits;
115 const unsigned int blocksize = 1 << blockbits;
796f12d7
EB
116 const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
117 const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
118 struct page *pages[16]; /* write up to 16 pages at a time */
119 unsigned int nr_pages;
120 unsigned int i;
121 unsigned int offset;
58ae7468 122 struct bio *bio;
796f12d7 123 int ret, err;
58ae7468 124
796f12d7
EB
125 if (len == 0)
126 return 0;
58ae7468 127
5fee3609
ST
128 if (fscrypt_inode_uses_inline_crypto(inode))
129 return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
130 len);
131
a8affc03 132 BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
796f12d7
EB
133 nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
134 (len + blocks_per_page - 1) >> blocks_per_page_bits);
58ae7468 135
796f12d7
EB
136 /*
137 * We need at least one page for ciphertext. Allocate the first one
138 * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
139 *
140 * Any additional page allocations are allowed to fail, as they only
141 * help performance, and waiting on the mempool for them could deadlock.
142 */
143 for (i = 0; i < nr_pages; i++) {
144 pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
145 GFP_NOWAIT | __GFP_NOWARN);
146 if (!pages[i])
147 break;
148 }
149 nr_pages = i;
41b2ad80 150 if (WARN_ON_ONCE(nr_pages <= 0))
796f12d7
EB
151 return -EINVAL;
152
153 /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
07888c66 154 bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
796f12d7
EB
155
156 do {
930d4539 157 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
796f12d7
EB
158
159 i = 0;
160 offset = 0;
161 do {
162 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
163 ZERO_PAGE(0), pages[i],
164 blocksize, offset, GFP_NOFS);
165 if (err)
166 goto out;
167 lblk++;
168 pblk++;
169 len--;
170 offset += blocksize;
171 if (offset == PAGE_SIZE || len == 0) {
172 ret = bio_add_page(bio, pages[i++], offset, 0);
41b2ad80 173 if (WARN_ON_ONCE(ret != offset)) {
796f12d7
EB
174 err = -EIO;
175 goto out;
176 }
177 offset = 0;
178 }
179 } while (i != nr_pages && len != 0);
180
58ae7468 181 err = submit_bio_wait(bio);
58ae7468 182 if (err)
796f12d7 183 goto out;
a7c50c94 184 bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
796f12d7 185 } while (len != 0);
58ae7468 186 err = 0;
796f12d7
EB
187out:
188 bio_put(bio);
189 for (i = 0; i < nr_pages; i++)
190 fscrypt_free_bounce_page(pages[i]);
58ae7468
RW
191 return err;
192}
193EXPORT_SYMBOL(fscrypt_zeroout_range);