162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Utility functions for file contents encryption/decryption on
462306a36Sopenharmony_ci * block device-based filesystems.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2015, Google, Inc.
762306a36Sopenharmony_ci * Copyright (C) 2015, Motorola Mobility
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/pagemap.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/bio.h>
1362306a36Sopenharmony_ci#include <linux/namei.h>
1462306a36Sopenharmony_ci#include "fscrypt_private.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/**
1762306a36Sopenharmony_ci * fscrypt_decrypt_bio() - decrypt the contents of a bio
1862306a36Sopenharmony_ci * @bio: the bio to decrypt
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Decrypt the contents of a "read" bio following successful completion of the
2162306a36Sopenharmony_ci * underlying disk read.  The bio must be reading a whole number of blocks of an
2262306a36Sopenharmony_ci * encrypted file directly into the page cache.  If the bio is reading the
2362306a36Sopenharmony_ci * ciphertext into bounce pages instead of the page cache (for example, because
2462306a36Sopenharmony_ci * the file is also compressed, so decompression is required after decryption),
2562306a36Sopenharmony_ci * then this function isn't applicable.  This function may sleep, so it must be
2662306a36Sopenharmony_ci * called from a workqueue rather than from the bio's bi_end_io callback.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * Return: %true on success; %false on failure.  On failure, bio->bi_status is
2962306a36Sopenharmony_ci *	   also set to an error status.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_cibool fscrypt_decrypt_bio(struct bio *bio)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	struct folio_iter fi;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	bio_for_each_folio_all(fi, bio) {
3662306a36Sopenharmony_ci		int err = fscrypt_decrypt_pagecache_blocks(fi.folio, fi.length,
3762306a36Sopenharmony_ci							   fi.offset);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci		if (err) {
4062306a36Sopenharmony_ci			bio->bi_status = errno_to_blk_status(err);
4162306a36Sopenharmony_ci			return false;
4262306a36Sopenharmony_ci		}
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci	return true;
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ciEXPORT_SYMBOL(fscrypt_decrypt_bio);
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
4962306a36Sopenharmony_ci					      pgoff_t lblk, sector_t pblk,
5062306a36Sopenharmony_ci					      unsigned int len)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	const unsigned int blockbits = inode->i_blkbits;
5362306a36Sopenharmony_ci	const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
5462306a36Sopenharmony_ci	struct bio *bio;
5562306a36Sopenharmony_ci	int ret, err = 0;
5662306a36Sopenharmony_ci	int num_pages = 0;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
5962306a36Sopenharmony_ci	bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
6062306a36Sopenharmony_ci			GFP_NOFS);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	while (len) {
6362306a36Sopenharmony_ci		unsigned int blocks_this_page = min(len, blocks_per_page);
6462306a36Sopenharmony_ci		unsigned int bytes_this_page = blocks_this_page << blockbits;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci		if (num_pages == 0) {
6762306a36Sopenharmony_ci			fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
6862306a36Sopenharmony_ci			bio->bi_iter.bi_sector =
6962306a36Sopenharmony_ci					pblk << (blockbits - SECTOR_SHIFT);
7062306a36Sopenharmony_ci		}
7162306a36Sopenharmony_ci		ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
7262306a36Sopenharmony_ci		if (WARN_ON_ONCE(ret != bytes_this_page)) {
7362306a36Sopenharmony_ci			err = -EIO;
7462306a36Sopenharmony_ci			goto out;
7562306a36Sopenharmony_ci		}
7662306a36Sopenharmony_ci		num_pages++;
7762306a36Sopenharmony_ci		len -= blocks_this_page;
7862306a36Sopenharmony_ci		lblk += blocks_this_page;
7962306a36Sopenharmony_ci		pblk += blocks_this_page;
8062306a36Sopenharmony_ci		if (num_pages == BIO_MAX_VECS || !len ||
8162306a36Sopenharmony_ci		    !fscrypt_mergeable_bio(bio, inode, lblk)) {
8262306a36Sopenharmony_ci			err = submit_bio_wait(bio);
8362306a36Sopenharmony_ci			if (err)
8462306a36Sopenharmony_ci				goto out;
8562306a36Sopenharmony_ci			bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
8662306a36Sopenharmony_ci			num_pages = 0;
8762306a36Sopenharmony_ci		}
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ciout:
9062306a36Sopenharmony_ci	bio_put(bio);
9162306a36Sopenharmony_ci	return err;
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
9662306a36Sopenharmony_ci * @inode: the file's inode
9762306a36Sopenharmony_ci * @lblk: the first file logical block to zero out
9862306a36Sopenharmony_ci * @pblk: the first filesystem physical block to zero out
9962306a36Sopenharmony_ci * @len: number of blocks to zero out
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
10262306a36Sopenharmony_ci * ciphertext blocks which decrypt to the all-zeroes block.  The blocks must be
10362306a36Sopenharmony_ci * both logically and physically contiguous.  It's also assumed that the
10462306a36Sopenharmony_ci * filesystem only uses a single block device, ->s_bdev.
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * Note that since each block uses a different IV, this involves writing a
10762306a36Sopenharmony_ci * different ciphertext to each block; we can't simply reuse the same one.
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci * Return: 0 on success; -errno on failure.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_ciint fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
11262306a36Sopenharmony_ci			  sector_t pblk, unsigned int len)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	const unsigned int blockbits = inode->i_blkbits;
11562306a36Sopenharmony_ci	const unsigned int blocksize = 1 << blockbits;
11662306a36Sopenharmony_ci	const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
11762306a36Sopenharmony_ci	const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
11862306a36Sopenharmony_ci	struct page *pages[16]; /* write up to 16 pages at a time */
11962306a36Sopenharmony_ci	unsigned int nr_pages;
12062306a36Sopenharmony_ci	unsigned int i;
12162306a36Sopenharmony_ci	unsigned int offset;
12262306a36Sopenharmony_ci	struct bio *bio;
12362306a36Sopenharmony_ci	int ret, err;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	if (len == 0)
12662306a36Sopenharmony_ci		return 0;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	if (fscrypt_inode_uses_inline_crypto(inode))
12962306a36Sopenharmony_ci		return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
13062306a36Sopenharmony_ci							  len);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
13362306a36Sopenharmony_ci	nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
13462306a36Sopenharmony_ci			 (len + blocks_per_page - 1) >> blocks_per_page_bits);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/*
13762306a36Sopenharmony_ci	 * We need at least one page for ciphertext.  Allocate the first one
13862306a36Sopenharmony_ci	 * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
13962306a36Sopenharmony_ci	 *
14062306a36Sopenharmony_ci	 * Any additional page allocations are allowed to fail, as they only
14162306a36Sopenharmony_ci	 * help performance, and waiting on the mempool for them could deadlock.
14262306a36Sopenharmony_ci	 */
14362306a36Sopenharmony_ci	for (i = 0; i < nr_pages; i++) {
14462306a36Sopenharmony_ci		pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
14562306a36Sopenharmony_ci						     GFP_NOWAIT | __GFP_NOWARN);
14662306a36Sopenharmony_ci		if (!pages[i])
14762306a36Sopenharmony_ci			break;
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci	nr_pages = i;
15062306a36Sopenharmony_ci	if (WARN_ON_ONCE(nr_pages <= 0))
15162306a36Sopenharmony_ci		return -EINVAL;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
15462306a36Sopenharmony_ci	bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	do {
15762306a36Sopenharmony_ci		bio->bi_iter.bi_sector = pblk << (blockbits - 9);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci		i = 0;
16062306a36Sopenharmony_ci		offset = 0;
16162306a36Sopenharmony_ci		do {
16262306a36Sopenharmony_ci			err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
16362306a36Sopenharmony_ci						  ZERO_PAGE(0), pages[i],
16462306a36Sopenharmony_ci						  blocksize, offset, GFP_NOFS);
16562306a36Sopenharmony_ci			if (err)
16662306a36Sopenharmony_ci				goto out;
16762306a36Sopenharmony_ci			lblk++;
16862306a36Sopenharmony_ci			pblk++;
16962306a36Sopenharmony_ci			len--;
17062306a36Sopenharmony_ci			offset += blocksize;
17162306a36Sopenharmony_ci			if (offset == PAGE_SIZE || len == 0) {
17262306a36Sopenharmony_ci				ret = bio_add_page(bio, pages[i++], offset, 0);
17362306a36Sopenharmony_ci				if (WARN_ON_ONCE(ret != offset)) {
17462306a36Sopenharmony_ci					err = -EIO;
17562306a36Sopenharmony_ci					goto out;
17662306a36Sopenharmony_ci				}
17762306a36Sopenharmony_ci				offset = 0;
17862306a36Sopenharmony_ci			}
17962306a36Sopenharmony_ci		} while (i != nr_pages && len != 0);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci		err = submit_bio_wait(bio);
18262306a36Sopenharmony_ci		if (err)
18362306a36Sopenharmony_ci			goto out;
18462306a36Sopenharmony_ci		bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
18562306a36Sopenharmony_ci	} while (len != 0);
18662306a36Sopenharmony_ci	err = 0;
18762306a36Sopenharmony_ciout:
18862306a36Sopenharmony_ci	bio_put(bio);
18962306a36Sopenharmony_ci	for (i = 0; i < nr_pages; i++)
19062306a36Sopenharmony_ci		fscrypt_free_bounce_page(pages[i]);
19162306a36Sopenharmony_ci	return err;
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ciEXPORT_SYMBOL(fscrypt_zeroout_range);
194