xref: /openbmc/linux/fs/crypto/inline_crypt.c (revision 453431a5)
15fee3609SSatya Tangirala // SPDX-License-Identifier: GPL-2.0
25fee3609SSatya Tangirala /*
35fee3609SSatya Tangirala  * Inline encryption support for fscrypt
45fee3609SSatya Tangirala  *
55fee3609SSatya Tangirala  * Copyright 2019 Google LLC
65fee3609SSatya Tangirala  */
75fee3609SSatya Tangirala 
85fee3609SSatya Tangirala /*
95fee3609SSatya Tangirala  * With "inline encryption", the block layer handles the decryption/encryption
105fee3609SSatya Tangirala  * as part of the bio, instead of the filesystem doing the crypto itself via
115fee3609SSatya Tangirala  * crypto API.  See Documentation/block/inline-encryption.rst.  fscrypt still
125fee3609SSatya Tangirala  * provides the key and IV to use.
135fee3609SSatya Tangirala  */
145fee3609SSatya Tangirala 
155fee3609SSatya Tangirala #include <linux/blk-crypto.h>
165fee3609SSatya Tangirala #include <linux/blkdev.h>
175fee3609SSatya Tangirala #include <linux/buffer_head.h>
185fee3609SSatya Tangirala #include <linux/sched/mm.h>
19453431a5SWaiman Long #include <linux/slab.h>
205fee3609SSatya Tangirala 
215fee3609SSatya Tangirala #include "fscrypt_private.h"
225fee3609SSatya Tangirala 
235fee3609SSatya Tangirala struct fscrypt_blk_crypto_key {
245fee3609SSatya Tangirala 	struct blk_crypto_key base;
255fee3609SSatya Tangirala 	int num_devs;
265fee3609SSatya Tangirala 	struct request_queue *devs[];
275fee3609SSatya Tangirala };
285fee3609SSatya Tangirala 
295fee3609SSatya Tangirala static int fscrypt_get_num_devices(struct super_block *sb)
305fee3609SSatya Tangirala {
315fee3609SSatya Tangirala 	if (sb->s_cop->get_num_devices)
325fee3609SSatya Tangirala 		return sb->s_cop->get_num_devices(sb);
335fee3609SSatya Tangirala 	return 1;
345fee3609SSatya Tangirala }
355fee3609SSatya Tangirala 
365fee3609SSatya Tangirala static void fscrypt_get_devices(struct super_block *sb, int num_devs,
375fee3609SSatya Tangirala 				struct request_queue **devs)
385fee3609SSatya Tangirala {
395fee3609SSatya Tangirala 	if (num_devs == 1)
405fee3609SSatya Tangirala 		devs[0] = bdev_get_queue(sb->s_bdev);
415fee3609SSatya Tangirala 	else
425fee3609SSatya Tangirala 		sb->s_cop->get_devices(sb, devs);
435fee3609SSatya Tangirala }
445fee3609SSatya Tangirala 
455fee3609SSatya Tangirala static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_info *ci)
465fee3609SSatya Tangirala {
475fee3609SSatya Tangirala 	struct super_block *sb = ci->ci_inode->i_sb;
485fee3609SSatya Tangirala 	unsigned int flags = fscrypt_policy_flags(&ci->ci_policy);
495fee3609SSatya Tangirala 	int ino_bits = 64, lblk_bits = 64;
505fee3609SSatya Tangirala 
515fee3609SSatya Tangirala 	if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
525fee3609SSatya Tangirala 		return offsetofend(union fscrypt_iv, nonce);
535fee3609SSatya Tangirala 
545fee3609SSatya Tangirala 	if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64)
555fee3609SSatya Tangirala 		return sizeof(__le64);
565fee3609SSatya Tangirala 
575fee3609SSatya Tangirala 	if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
585fee3609SSatya Tangirala 		return sizeof(__le32);
595fee3609SSatya Tangirala 
605fee3609SSatya Tangirala 	/* Default case: IVs are just the file logical block number */
615fee3609SSatya Tangirala 	if (sb->s_cop->get_ino_and_lblk_bits)
625fee3609SSatya Tangirala 		sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
635fee3609SSatya Tangirala 	return DIV_ROUND_UP(lblk_bits, 8);
645fee3609SSatya Tangirala }
655fee3609SSatya Tangirala 
665fee3609SSatya Tangirala /* Enable inline encryption for this file if supported. */
675fee3609SSatya Tangirala int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
685fee3609SSatya Tangirala {
695fee3609SSatya Tangirala 	const struct inode *inode = ci->ci_inode;
705fee3609SSatya Tangirala 	struct super_block *sb = inode->i_sb;
715fee3609SSatya Tangirala 	struct blk_crypto_config crypto_cfg;
725fee3609SSatya Tangirala 	int num_devs;
735fee3609SSatya Tangirala 	struct request_queue **devs;
745fee3609SSatya Tangirala 	int i;
755fee3609SSatya Tangirala 
765fee3609SSatya Tangirala 	/* The file must need contents encryption, not filenames encryption */
775fee3609SSatya Tangirala 	if (!fscrypt_needs_contents_encryption(inode))
785fee3609SSatya Tangirala 		return 0;
795fee3609SSatya Tangirala 
805fee3609SSatya Tangirala 	/* The crypto mode must have a blk-crypto counterpart */
815fee3609SSatya Tangirala 	if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
825fee3609SSatya Tangirala 		return 0;
835fee3609SSatya Tangirala 
845fee3609SSatya Tangirala 	/* The filesystem must be mounted with -o inlinecrypt */
855fee3609SSatya Tangirala 	if (!(sb->s_flags & SB_INLINECRYPT))
865fee3609SSatya Tangirala 		return 0;
875fee3609SSatya Tangirala 
885fee3609SSatya Tangirala 	/*
895fee3609SSatya Tangirala 	 * When a page contains multiple logically contiguous filesystem blocks,
905fee3609SSatya Tangirala 	 * some filesystem code only calls fscrypt_mergeable_bio() for the first
915fee3609SSatya Tangirala 	 * block in the page. This is fine for most of fscrypt's IV generation
925fee3609SSatya Tangirala 	 * strategies, where contiguous blocks imply contiguous IVs. But it
935fee3609SSatya Tangirala 	 * doesn't work with IV_INO_LBLK_32. For now, simply exclude
945fee3609SSatya Tangirala 	 * IV_INO_LBLK_32 with blocksize != PAGE_SIZE from inline encryption.
955fee3609SSatya Tangirala 	 */
965fee3609SSatya Tangirala 	if ((fscrypt_policy_flags(&ci->ci_policy) &
975fee3609SSatya Tangirala 	     FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
985fee3609SSatya Tangirala 	    sb->s_blocksize != PAGE_SIZE)
995fee3609SSatya Tangirala 		return 0;
1005fee3609SSatya Tangirala 
1015fee3609SSatya Tangirala 	/*
1025fee3609SSatya Tangirala 	 * On all the filesystem's devices, blk-crypto must support the crypto
1035fee3609SSatya Tangirala 	 * configuration that the file would use.
1045fee3609SSatya Tangirala 	 */
1055fee3609SSatya Tangirala 	crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode;
1065fee3609SSatya Tangirala 	crypto_cfg.data_unit_size = sb->s_blocksize;
1075fee3609SSatya Tangirala 	crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
1085fee3609SSatya Tangirala 	num_devs = fscrypt_get_num_devices(sb);
1095fee3609SSatya Tangirala 	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_NOFS);
1105fee3609SSatya Tangirala 	if (!devs)
1115fee3609SSatya Tangirala 		return -ENOMEM;
1125fee3609SSatya Tangirala 	fscrypt_get_devices(sb, num_devs, devs);
1135fee3609SSatya Tangirala 
1145fee3609SSatya Tangirala 	for (i = 0; i < num_devs; i++) {
1155fee3609SSatya Tangirala 		if (!blk_crypto_config_supported(devs[i], &crypto_cfg))
1165fee3609SSatya Tangirala 			goto out_free_devs;
1175fee3609SSatya Tangirala 	}
1185fee3609SSatya Tangirala 
1195fee3609SSatya Tangirala 	ci->ci_inlinecrypt = true;
1205fee3609SSatya Tangirala out_free_devs:
1215fee3609SSatya Tangirala 	kfree(devs);
1225fee3609SSatya Tangirala 
1235fee3609SSatya Tangirala 	return 0;
1245fee3609SSatya Tangirala }
1255fee3609SSatya Tangirala 
1265fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
1275fee3609SSatya Tangirala 				     const u8 *raw_key,
1285fee3609SSatya Tangirala 				     const struct fscrypt_info *ci)
1295fee3609SSatya Tangirala {
1305fee3609SSatya Tangirala 	const struct inode *inode = ci->ci_inode;
1315fee3609SSatya Tangirala 	struct super_block *sb = inode->i_sb;
1325fee3609SSatya Tangirala 	enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode;
1335fee3609SSatya Tangirala 	int num_devs = fscrypt_get_num_devices(sb);
1345fee3609SSatya Tangirala 	int queue_refs = 0;
1355fee3609SSatya Tangirala 	struct fscrypt_blk_crypto_key *blk_key;
1365fee3609SSatya Tangirala 	int err;
1375fee3609SSatya Tangirala 	int i;
1385fee3609SSatya Tangirala 	unsigned int flags;
1395fee3609SSatya Tangirala 
1405fee3609SSatya Tangirala 	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_NOFS);
1415fee3609SSatya Tangirala 	if (!blk_key)
1425fee3609SSatya Tangirala 		return -ENOMEM;
1435fee3609SSatya Tangirala 
1445fee3609SSatya Tangirala 	blk_key->num_devs = num_devs;
1455fee3609SSatya Tangirala 	fscrypt_get_devices(sb, num_devs, blk_key->devs);
1465fee3609SSatya Tangirala 
1475fee3609SSatya Tangirala 	err = blk_crypto_init_key(&blk_key->base, raw_key, crypto_mode,
1485fee3609SSatya Tangirala 				  fscrypt_get_dun_bytes(ci), sb->s_blocksize);
1495fee3609SSatya Tangirala 	if (err) {
1505fee3609SSatya Tangirala 		fscrypt_err(inode, "error %d initializing blk-crypto key", err);
1515fee3609SSatya Tangirala 		goto fail;
1525fee3609SSatya Tangirala 	}
1535fee3609SSatya Tangirala 
1545fee3609SSatya Tangirala 	/*
1555fee3609SSatya Tangirala 	 * We have to start using blk-crypto on all the filesystem's devices.
1565fee3609SSatya Tangirala 	 * We also have to save all the request_queue's for later so that the
1575fee3609SSatya Tangirala 	 * key can be evicted from them.  This is needed because some keys
1585fee3609SSatya Tangirala 	 * aren't destroyed until after the filesystem was already unmounted
1595fee3609SSatya Tangirala 	 * (namely, the per-mode keys in struct fscrypt_master_key).
1605fee3609SSatya Tangirala 	 */
1615fee3609SSatya Tangirala 	for (i = 0; i < num_devs; i++) {
1625fee3609SSatya Tangirala 		if (!blk_get_queue(blk_key->devs[i])) {
1635fee3609SSatya Tangirala 			fscrypt_err(inode, "couldn't get request_queue");
1645fee3609SSatya Tangirala 			err = -EAGAIN;
1655fee3609SSatya Tangirala 			goto fail;
1665fee3609SSatya Tangirala 		}
1675fee3609SSatya Tangirala 		queue_refs++;
1685fee3609SSatya Tangirala 
1695fee3609SSatya Tangirala 		flags = memalloc_nofs_save();
1705fee3609SSatya Tangirala 		err = blk_crypto_start_using_key(&blk_key->base,
1715fee3609SSatya Tangirala 						 blk_key->devs[i]);
1725fee3609SSatya Tangirala 		memalloc_nofs_restore(flags);
1735fee3609SSatya Tangirala 		if (err) {
1745fee3609SSatya Tangirala 			fscrypt_err(inode,
1755fee3609SSatya Tangirala 				    "error %d starting to use blk-crypto", err);
1765fee3609SSatya Tangirala 			goto fail;
1775fee3609SSatya Tangirala 		}
1785fee3609SSatya Tangirala 	}
1795fee3609SSatya Tangirala 	/*
18097c6327fSEric Biggers 	 * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
18197c6327fSEric Biggers 	 * I.e., here we publish ->blk_key with a RELEASE barrier so that
18297c6327fSEric Biggers 	 * concurrent tasks can ACQUIRE it.  Note that this concurrency is only
18397c6327fSEric Biggers 	 * possible for per-mode keys, not for per-file keys.
1845fee3609SSatya Tangirala 	 */
1855fee3609SSatya Tangirala 	smp_store_release(&prep_key->blk_key, blk_key);
1865fee3609SSatya Tangirala 	return 0;
1875fee3609SSatya Tangirala 
1885fee3609SSatya Tangirala fail:
1895fee3609SSatya Tangirala 	for (i = 0; i < queue_refs; i++)
1905fee3609SSatya Tangirala 		blk_put_queue(blk_key->devs[i]);
191453431a5SWaiman Long 	kfree_sensitive(blk_key);
1925fee3609SSatya Tangirala 	return err;
1935fee3609SSatya Tangirala }
1945fee3609SSatya Tangirala 
1955fee3609SSatya Tangirala void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
1965fee3609SSatya Tangirala {
1975fee3609SSatya Tangirala 	struct fscrypt_blk_crypto_key *blk_key = prep_key->blk_key;
1985fee3609SSatya Tangirala 	int i;
1995fee3609SSatya Tangirala 
2005fee3609SSatya Tangirala 	if (blk_key) {
2015fee3609SSatya Tangirala 		for (i = 0; i < blk_key->num_devs; i++) {
2025fee3609SSatya Tangirala 			blk_crypto_evict_key(blk_key->devs[i], &blk_key->base);
2035fee3609SSatya Tangirala 			blk_put_queue(blk_key->devs[i]);
2045fee3609SSatya Tangirala 		}
205453431a5SWaiman Long 		kfree_sensitive(blk_key);
2065fee3609SSatya Tangirala 	}
2075fee3609SSatya Tangirala }
2085fee3609SSatya Tangirala 
2095fee3609SSatya Tangirala bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
2105fee3609SSatya Tangirala {
2115fee3609SSatya Tangirala 	return inode->i_crypt_info->ci_inlinecrypt;
2125fee3609SSatya Tangirala }
2135fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
2145fee3609SSatya Tangirala 
2155fee3609SSatya Tangirala static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
2165fee3609SSatya Tangirala 				 u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
2175fee3609SSatya Tangirala {
2185fee3609SSatya Tangirala 	union fscrypt_iv iv;
2195fee3609SSatya Tangirala 	int i;
2205fee3609SSatya Tangirala 
2215fee3609SSatya Tangirala 	fscrypt_generate_iv(&iv, lblk_num, ci);
2225fee3609SSatya Tangirala 
2235fee3609SSatya Tangirala 	BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
2245fee3609SSatya Tangirala 	memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
2255fee3609SSatya Tangirala 	for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++)
2265fee3609SSatya Tangirala 		dun[i] = le64_to_cpu(iv.dun[i]);
2275fee3609SSatya Tangirala }
2285fee3609SSatya Tangirala 
2295fee3609SSatya Tangirala /**
2305fee3609SSatya Tangirala  * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
2315fee3609SSatya Tangirala  * @bio: a bio which will eventually be submitted to the file
2325fee3609SSatya Tangirala  * @inode: the file's inode
2335fee3609SSatya Tangirala  * @first_lblk: the first file logical block number in the I/O
2345fee3609SSatya Tangirala  * @gfp_mask: memory allocation flags - these must be a waiting mask so that
2355fee3609SSatya Tangirala  *					bio_crypt_set_ctx can't fail.
2365fee3609SSatya Tangirala  *
2375fee3609SSatya Tangirala  * If the contents of the file should be encrypted (or decrypted) with inline
2385fee3609SSatya Tangirala  * encryption, then assign the appropriate encryption context to the bio.
2395fee3609SSatya Tangirala  *
2405fee3609SSatya Tangirala  * Normally the bio should be newly allocated (i.e. no pages added yet), as
2415fee3609SSatya Tangirala  * otherwise fscrypt_mergeable_bio() won't work as intended.
2425fee3609SSatya Tangirala  *
2435fee3609SSatya Tangirala  * The encryption context will be freed automatically when the bio is freed.
2445fee3609SSatya Tangirala  */
2455fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
2465fee3609SSatya Tangirala 			       u64 first_lblk, gfp_t gfp_mask)
2475fee3609SSatya Tangirala {
24855e32c54SEric Biggers 	const struct fscrypt_info *ci;
2495fee3609SSatya Tangirala 	u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
2505fee3609SSatya Tangirala 
2515fee3609SSatya Tangirala 	if (!fscrypt_inode_uses_inline_crypto(inode))
2525fee3609SSatya Tangirala 		return;
25355e32c54SEric Biggers 	ci = inode->i_crypt_info;
2545fee3609SSatya Tangirala 
2555fee3609SSatya Tangirala 	fscrypt_generate_dun(ci, first_lblk, dun);
2565fee3609SSatya Tangirala 	bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask);
2575fee3609SSatya Tangirala }
2585fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
2595fee3609SSatya Tangirala 
2605fee3609SSatya Tangirala /* Extract the inode and logical block number from a buffer_head. */
2615fee3609SSatya Tangirala static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
2625fee3609SSatya Tangirala 				      const struct inode **inode_ret,
2635fee3609SSatya Tangirala 				      u64 *lblk_num_ret)
2645fee3609SSatya Tangirala {
2655fee3609SSatya Tangirala 	struct page *page = bh->b_page;
2665fee3609SSatya Tangirala 	const struct address_space *mapping;
2675fee3609SSatya Tangirala 	const struct inode *inode;
2685fee3609SSatya Tangirala 
2695fee3609SSatya Tangirala 	/*
2705fee3609SSatya Tangirala 	 * The ext4 journal (jbd2) can submit a buffer_head it directly created
2715fee3609SSatya Tangirala 	 * for a non-pagecache page.  fscrypt doesn't care about these.
2725fee3609SSatya Tangirala 	 */
2735fee3609SSatya Tangirala 	mapping = page_mapping(page);
2745fee3609SSatya Tangirala 	if (!mapping)
2755fee3609SSatya Tangirala 		return false;
2765fee3609SSatya Tangirala 	inode = mapping->host;
2775fee3609SSatya Tangirala 
2785fee3609SSatya Tangirala 	*inode_ret = inode;
2795fee3609SSatya Tangirala 	*lblk_num_ret = ((u64)page->index << (PAGE_SHIFT - inode->i_blkbits)) +
2805fee3609SSatya Tangirala 			(bh_offset(bh) >> inode->i_blkbits);
2815fee3609SSatya Tangirala 	return true;
2825fee3609SSatya Tangirala }
2835fee3609SSatya Tangirala 
2845fee3609SSatya Tangirala /**
2855fee3609SSatya Tangirala  * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
2865fee3609SSatya Tangirala  *				    crypto
2875fee3609SSatya Tangirala  * @bio: a bio which will eventually be submitted to the file
2885fee3609SSatya Tangirala  * @first_bh: the first buffer_head for which I/O will be submitted
2895fee3609SSatya Tangirala  * @gfp_mask: memory allocation flags
2905fee3609SSatya Tangirala  *
2915fee3609SSatya Tangirala  * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
2925fee3609SSatya Tangirala  * of an inode and block number directly.
2935fee3609SSatya Tangirala  */
2945fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
2955fee3609SSatya Tangirala 				  const struct buffer_head *first_bh,
2965fee3609SSatya Tangirala 				  gfp_t gfp_mask)
2975fee3609SSatya Tangirala {
2985fee3609SSatya Tangirala 	const struct inode *inode;
2995fee3609SSatya Tangirala 	u64 first_lblk;
3005fee3609SSatya Tangirala 
3015fee3609SSatya Tangirala 	if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
3025fee3609SSatya Tangirala 		fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
3035fee3609SSatya Tangirala }
3045fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
3055fee3609SSatya Tangirala 
3065fee3609SSatya Tangirala /**
3075fee3609SSatya Tangirala  * fscrypt_mergeable_bio() - test whether data can be added to a bio
3085fee3609SSatya Tangirala  * @bio: the bio being built up
3095fee3609SSatya Tangirala  * @inode: the inode for the next part of the I/O
3105fee3609SSatya Tangirala  * @next_lblk: the next file logical block number in the I/O
3115fee3609SSatya Tangirala  *
3125fee3609SSatya Tangirala  * When building a bio which may contain data which should undergo inline
3135fee3609SSatya Tangirala  * encryption (or decryption) via fscrypt, filesystems should call this function
3145fee3609SSatya Tangirala  * to ensure that the resulting bio contains only contiguous data unit numbers.
3155fee3609SSatya Tangirala  * This will return false if the next part of the I/O cannot be merged with the
3165fee3609SSatya Tangirala  * bio because either the encryption key would be different or the encryption
3175fee3609SSatya Tangirala  * data unit numbers would be discontiguous.
3185fee3609SSatya Tangirala  *
3195fee3609SSatya Tangirala  * fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
3205fee3609SSatya Tangirala  *
3215fee3609SSatya Tangirala  * Return: true iff the I/O is mergeable
3225fee3609SSatya Tangirala  */
3235fee3609SSatya Tangirala bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
3245fee3609SSatya Tangirala 			   u64 next_lblk)
3255fee3609SSatya Tangirala {
3265fee3609SSatya Tangirala 	const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
3275fee3609SSatya Tangirala 	u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
3285fee3609SSatya Tangirala 
3295fee3609SSatya Tangirala 	if (!!bc != fscrypt_inode_uses_inline_crypto(inode))
3305fee3609SSatya Tangirala 		return false;
3315fee3609SSatya Tangirala 	if (!bc)
3325fee3609SSatya Tangirala 		return true;
3335fee3609SSatya Tangirala 
3345fee3609SSatya Tangirala 	/*
3355fee3609SSatya Tangirala 	 * Comparing the key pointers is good enough, as all I/O for each key
3365fee3609SSatya Tangirala 	 * uses the same pointer.  I.e., there's currently no need to support
3375fee3609SSatya Tangirala 	 * merging requests where the keys are the same but the pointers differ.
3385fee3609SSatya Tangirala 	 */
3395fee3609SSatya Tangirala 	if (bc->bc_key != &inode->i_crypt_info->ci_enc_key.blk_key->base)
3405fee3609SSatya Tangirala 		return false;
3415fee3609SSatya Tangirala 
3425fee3609SSatya Tangirala 	fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
3435fee3609SSatya Tangirala 	return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
3445fee3609SSatya Tangirala }
3455fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
3465fee3609SSatya Tangirala 
3475fee3609SSatya Tangirala /**
3485fee3609SSatya Tangirala  * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
3495fee3609SSatya Tangirala  * @bio: the bio being built up
3505fee3609SSatya Tangirala  * @next_bh: the next buffer_head for which I/O will be submitted
3515fee3609SSatya Tangirala  *
3525fee3609SSatya Tangirala  * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
3535fee3609SSatya Tangirala  * an inode and block number directly.
3545fee3609SSatya Tangirala  *
3555fee3609SSatya Tangirala  * Return: true iff the I/O is mergeable
3565fee3609SSatya Tangirala  */
3575fee3609SSatya Tangirala bool fscrypt_mergeable_bio_bh(struct bio *bio,
3585fee3609SSatya Tangirala 			      const struct buffer_head *next_bh)
3595fee3609SSatya Tangirala {
3605fee3609SSatya Tangirala 	const struct inode *inode;
3615fee3609SSatya Tangirala 	u64 next_lblk;
3625fee3609SSatya Tangirala 
3635fee3609SSatya Tangirala 	if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
3645fee3609SSatya Tangirala 		return !bio->bi_crypt_context;
3655fee3609SSatya Tangirala 
3665fee3609SSatya Tangirala 	return fscrypt_mergeable_bio(bio, inode, next_lblk);
3675fee3609SSatya Tangirala }
3685fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
369