xref: /openbmc/linux/fs/crypto/bio.c (revision a7c50c940477bae89fb2b4f51bd969a2d95d7512)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
258ae7468SRichard Weinberger /*
3f262ca7dSEric Biggers  * Utility functions for file contents encryption/decryption on
4f262ca7dSEric Biggers  * block device-based filesystems.
558ae7468SRichard Weinberger  *
658ae7468SRichard Weinberger  * Copyright (C) 2015, Google, Inc.
758ae7468SRichard Weinberger  * Copyright (C) 2015, Motorola Mobility
858ae7468SRichard Weinberger  */
958ae7468SRichard Weinberger 
1058ae7468SRichard Weinberger #include <linux/pagemap.h>
1158ae7468SRichard Weinberger #include <linux/module.h>
1258ae7468SRichard Weinberger #include <linux/bio.h>
1358ae7468SRichard Weinberger #include <linux/namei.h>
1458ae7468SRichard Weinberger #include "fscrypt_private.h"
1558ae7468SRichard Weinberger 
16f262ca7dSEric Biggers /**
17f262ca7dSEric Biggers  * fscrypt_decrypt_bio() - decrypt the contents of a bio
18f262ca7dSEric Biggers  * @bio: the bio to decrypt
19f262ca7dSEric Biggers  *
20f262ca7dSEric Biggers  * Decrypt the contents of a "read" bio following successful completion of the
21f262ca7dSEric Biggers  * underlying disk read.  The bio must be reading a whole number of blocks of an
22f262ca7dSEric Biggers  * encrypted file directly into the page cache.  If the bio is reading the
23f262ca7dSEric Biggers  * ciphertext into bounce pages instead of the page cache (for example, because
24f262ca7dSEric Biggers  * the file is also compressed, so decompression is required after decryption),
25f262ca7dSEric Biggers  * then this function isn't applicable.  This function may sleep, so it must be
26f262ca7dSEric Biggers  * called from a workqueue rather than from the bio's bi_end_io callback.
27f262ca7dSEric Biggers  *
28f262ca7dSEric Biggers  * This function sets PG_error on any pages that contain any blocks that failed
29f262ca7dSEric Biggers  * to be decrypted.  The filesystem must not mark such pages uptodate.
30f262ca7dSEric Biggers  */
311565bdadSEric Biggers void fscrypt_decrypt_bio(struct bio *bio)
3258ae7468SRichard Weinberger {
3358ae7468SRichard Weinberger 	struct bio_vec *bv;
346dc4f100SMing Lei 	struct bvec_iter_all iter_all;
3558ae7468SRichard Weinberger 
362b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bv, bio, iter_all) {
3758ae7468SRichard Weinberger 		struct page *page = bv->bv_page;
38ffceeefbSEric Biggers 		int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
39ffceeefbSEric Biggers 							   bv->bv_offset);
40ff5d3a97SEric Biggers 		if (ret)
4158ae7468SRichard Weinberger 			SetPageError(page);
4258ae7468SRichard Weinberger 	}
430cb8dae4SEric Biggers }
440cb8dae4SEric Biggers EXPORT_SYMBOL(fscrypt_decrypt_bio);
450cb8dae4SEric Biggers 
465fee3609SSatya Tangirala static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
475fee3609SSatya Tangirala 					      pgoff_t lblk, sector_t pblk,
485fee3609SSatya Tangirala 					      unsigned int len)
495fee3609SSatya Tangirala {
505fee3609SSatya Tangirala 	const unsigned int blockbits = inode->i_blkbits;
515fee3609SSatya Tangirala 	const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
525fee3609SSatya Tangirala 	struct bio *bio;
535fee3609SSatya Tangirala 	int ret, err = 0;
545fee3609SSatya Tangirala 	int num_pages = 0;
555fee3609SSatya Tangirala 
565fee3609SSatya Tangirala 	/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
5707888c66SChristoph Hellwig 	bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
5807888c66SChristoph Hellwig 			GFP_NOFS);
595fee3609SSatya Tangirala 
605fee3609SSatya Tangirala 	while (len) {
615fee3609SSatya Tangirala 		unsigned int blocks_this_page = min(len, blocks_per_page);
625fee3609SSatya Tangirala 		unsigned int bytes_this_page = blocks_this_page << blockbits;
635fee3609SSatya Tangirala 
645fee3609SSatya Tangirala 		if (num_pages == 0) {
655fee3609SSatya Tangirala 			fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
665fee3609SSatya Tangirala 			bio->bi_iter.bi_sector =
675fee3609SSatya Tangirala 					pblk << (blockbits - SECTOR_SHIFT);
685fee3609SSatya Tangirala 		}
695fee3609SSatya Tangirala 		ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
705fee3609SSatya Tangirala 		if (WARN_ON(ret != bytes_this_page)) {
715fee3609SSatya Tangirala 			err = -EIO;
725fee3609SSatya Tangirala 			goto out;
735fee3609SSatya Tangirala 		}
745fee3609SSatya Tangirala 		num_pages++;
755fee3609SSatya Tangirala 		len -= blocks_this_page;
765fee3609SSatya Tangirala 		lblk += blocks_this_page;
775fee3609SSatya Tangirala 		pblk += blocks_this_page;
78a8affc03SChristoph Hellwig 		if (num_pages == BIO_MAX_VECS || !len ||
795fee3609SSatya Tangirala 		    !fscrypt_mergeable_bio(bio, inode, lblk)) {
805fee3609SSatya Tangirala 			err = submit_bio_wait(bio);
815fee3609SSatya Tangirala 			if (err)
825fee3609SSatya Tangirala 				goto out;
83*a7c50c94SChristoph Hellwig 			bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
845fee3609SSatya Tangirala 			num_pages = 0;
855fee3609SSatya Tangirala 		}
865fee3609SSatya Tangirala 	}
875fee3609SSatya Tangirala out:
885fee3609SSatya Tangirala 	bio_put(bio);
895fee3609SSatya Tangirala 	return err;
905fee3609SSatya Tangirala }
915fee3609SSatya Tangirala 
92796f12d7SEric Biggers /**
93796f12d7SEric Biggers  * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
94796f12d7SEric Biggers  * @inode: the file's inode
95796f12d7SEric Biggers  * @lblk: the first file logical block to zero out
96796f12d7SEric Biggers  * @pblk: the first filesystem physical block to zero out
97796f12d7SEric Biggers  * @len: number of blocks to zero out
98796f12d7SEric Biggers  *
99796f12d7SEric Biggers  * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
100796f12d7SEric Biggers  * ciphertext blocks which decrypt to the all-zeroes block.  The blocks must be
101796f12d7SEric Biggers  * both logically and physically contiguous.  It's also assumed that the
102796f12d7SEric Biggers  * filesystem only uses a single block device, ->s_bdev.
103796f12d7SEric Biggers  *
104796f12d7SEric Biggers  * Note that since each block uses a different IV, this involves writing a
105796f12d7SEric Biggers  * different ciphertext to each block; we can't simply reuse the same one.
106796f12d7SEric Biggers  *
107796f12d7SEric Biggers  * Return: 0 on success; -errno on failure.
108796f12d7SEric Biggers  */
10958ae7468SRichard Weinberger int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
11058ae7468SRichard Weinberger 			  sector_t pblk, unsigned int len)
11158ae7468SRichard Weinberger {
112930d4539SEric Biggers 	const unsigned int blockbits = inode->i_blkbits;
113930d4539SEric Biggers 	const unsigned int blocksize = 1 << blockbits;
114796f12d7SEric Biggers 	const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
115796f12d7SEric Biggers 	const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
116796f12d7SEric Biggers 	struct page *pages[16]; /* write up to 16 pages at a time */
117796f12d7SEric Biggers 	unsigned int nr_pages;
118796f12d7SEric Biggers 	unsigned int i;
119796f12d7SEric Biggers 	unsigned int offset;
12058ae7468SRichard Weinberger 	struct bio *bio;
121796f12d7SEric Biggers 	int ret, err;
12258ae7468SRichard Weinberger 
123796f12d7SEric Biggers 	if (len == 0)
124796f12d7SEric Biggers 		return 0;
12558ae7468SRichard Weinberger 
1265fee3609SSatya Tangirala 	if (fscrypt_inode_uses_inline_crypto(inode))
1275fee3609SSatya Tangirala 		return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
1285fee3609SSatya Tangirala 							  len);
1295fee3609SSatya Tangirala 
130a8affc03SChristoph Hellwig 	BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
131796f12d7SEric Biggers 	nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
132796f12d7SEric Biggers 			 (len + blocks_per_page - 1) >> blocks_per_page_bits);
13358ae7468SRichard Weinberger 
134796f12d7SEric Biggers 	/*
135796f12d7SEric Biggers 	 * We need at least one page for ciphertext.  Allocate the first one
136796f12d7SEric Biggers 	 * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
137796f12d7SEric Biggers 	 *
138796f12d7SEric Biggers 	 * Any additional page allocations are allowed to fail, as they only
139796f12d7SEric Biggers 	 * help performance, and waiting on the mempool for them could deadlock.
140796f12d7SEric Biggers 	 */
141796f12d7SEric Biggers 	for (i = 0; i < nr_pages; i++) {
142796f12d7SEric Biggers 		pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
143796f12d7SEric Biggers 						     GFP_NOWAIT | __GFP_NOWARN);
144796f12d7SEric Biggers 		if (!pages[i])
145796f12d7SEric Biggers 			break;
14658ae7468SRichard Weinberger 	}
147796f12d7SEric Biggers 	nr_pages = i;
148796f12d7SEric Biggers 	if (WARN_ON(nr_pages <= 0))
149796f12d7SEric Biggers 		return -EINVAL;
150796f12d7SEric Biggers 
151796f12d7SEric Biggers 	/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
15207888c66SChristoph Hellwig 	bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
153796f12d7SEric Biggers 
154796f12d7SEric Biggers 	do {
155930d4539SEric Biggers 		bio->bi_iter.bi_sector = pblk << (blockbits - 9);
156796f12d7SEric Biggers 
157796f12d7SEric Biggers 		i = 0;
158796f12d7SEric Biggers 		offset = 0;
159796f12d7SEric Biggers 		do {
160796f12d7SEric Biggers 			err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
161796f12d7SEric Biggers 						  ZERO_PAGE(0), pages[i],
162796f12d7SEric Biggers 						  blocksize, offset, GFP_NOFS);
16358ae7468SRichard Weinberger 			if (err)
164796f12d7SEric Biggers 				goto out;
16558ae7468SRichard Weinberger 			lblk++;
16658ae7468SRichard Weinberger 			pblk++;
167796f12d7SEric Biggers 			len--;
168796f12d7SEric Biggers 			offset += blocksize;
169796f12d7SEric Biggers 			if (offset == PAGE_SIZE || len == 0) {
170796f12d7SEric Biggers 				ret = bio_add_page(bio, pages[i++], offset, 0);
171796f12d7SEric Biggers 				if (WARN_ON(ret != offset)) {
172796f12d7SEric Biggers 					err = -EIO;
173796f12d7SEric Biggers 					goto out;
17458ae7468SRichard Weinberger 				}
175796f12d7SEric Biggers 				offset = 0;
176796f12d7SEric Biggers 			}
177796f12d7SEric Biggers 		} while (i != nr_pages && len != 0);
178796f12d7SEric Biggers 
179796f12d7SEric Biggers 		err = submit_bio_wait(bio);
180796f12d7SEric Biggers 		if (err)
181796f12d7SEric Biggers 			goto out;
182*a7c50c94SChristoph Hellwig 		bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
183796f12d7SEric Biggers 	} while (len != 0);
18458ae7468SRichard Weinberger 	err = 0;
185796f12d7SEric Biggers out:
186796f12d7SEric Biggers 	bio_put(bio);
187796f12d7SEric Biggers 	for (i = 0; i < nr_pages; i++)
188796f12d7SEric Biggers 		fscrypt_free_bounce_page(pages[i]);
18958ae7468SRichard Weinberger 	return err;
19058ae7468SRichard Weinberger }
19158ae7468SRichard Weinberger EXPORT_SYMBOL(fscrypt_zeroout_range);
192