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
156715c98bSChristoph Hellwig #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>
20c6c89783SEric Biggers #include <linux/uio.h>
215fee3609SSatya Tangirala
225fee3609SSatya Tangirala #include "fscrypt_private.h"
235fee3609SSatya Tangirala
fscrypt_get_devices(struct super_block * sb,unsigned int * num_devs)240e91fc1eSChristoph Hellwig static struct block_device **fscrypt_get_devices(struct super_block *sb,
250e91fc1eSChristoph Hellwig unsigned int *num_devs)
265fee3609SSatya Tangirala {
270e91fc1eSChristoph Hellwig struct block_device **devs;
285fee3609SSatya Tangirala
290e91fc1eSChristoph Hellwig if (sb->s_cop->get_devices) {
300e91fc1eSChristoph Hellwig devs = sb->s_cop->get_devices(sb, num_devs);
310e91fc1eSChristoph Hellwig if (devs)
320e91fc1eSChristoph Hellwig return devs;
330e91fc1eSChristoph Hellwig }
340e91fc1eSChristoph Hellwig devs = kmalloc(sizeof(*devs), GFP_KERNEL);
350e91fc1eSChristoph Hellwig if (!devs)
360e91fc1eSChristoph Hellwig return ERR_PTR(-ENOMEM);
370e91fc1eSChristoph Hellwig devs[0] = sb->s_bdev;
380e91fc1eSChristoph Hellwig *num_devs = 1;
390e91fc1eSChristoph Hellwig return devs;
405fee3609SSatya Tangirala }
415fee3609SSatya Tangirala
fscrypt_get_dun_bytes(const struct fscrypt_inode_info * ci)423e7807d5SJosef Bacik static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_inode_info *ci)
435fee3609SSatya Tangirala {
44f0904e8bSEric Biggers const struct super_block *sb = ci->ci_inode->i_sb;
455fee3609SSatya Tangirala unsigned int flags = fscrypt_policy_flags(&ci->ci_policy);
465b118884SEric Biggers int dun_bits;
475fee3609SSatya Tangirala
485fee3609SSatya Tangirala if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
495fee3609SSatya Tangirala return offsetofend(union fscrypt_iv, nonce);
505fee3609SSatya Tangirala
515fee3609SSatya Tangirala if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64)
525fee3609SSatya Tangirala return sizeof(__le64);
535fee3609SSatya Tangirala
545fee3609SSatya Tangirala if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
555fee3609SSatya Tangirala return sizeof(__le32);
565fee3609SSatya Tangirala
575b118884SEric Biggers /* Default case: IVs are just the file data unit index */
585b118884SEric Biggers dun_bits = fscrypt_max_file_dun_bits(sb, ci->ci_data_unit_bits);
595b118884SEric Biggers return DIV_ROUND_UP(dun_bits, 8);
605fee3609SSatya Tangirala }
615fee3609SSatya Tangirala
62a7a5bc5fSEric Biggers /*
63a7a5bc5fSEric Biggers * Log a message when starting to use blk-crypto (native) or blk-crypto-fallback
64a7a5bc5fSEric Biggers * for an encryption mode for the first time. This is the blk-crypto
65a7a5bc5fSEric Biggers * counterpart to the message logged when starting to use the crypto API for the
66a7a5bc5fSEric Biggers * first time. A limitation is that these messages don't convey which specific
67a7a5bc5fSEric Biggers * filesystems or files are using each implementation. However, *usually*
68a7a5bc5fSEric Biggers * systems use just one implementation per mode, which makes these messages
69a7a5bc5fSEric Biggers * helpful for debugging problems where the "wrong" implementation is used.
70a7a5bc5fSEric Biggers */
fscrypt_log_blk_crypto_impl(struct fscrypt_mode * mode,struct block_device ** devs,unsigned int num_devs,const struct blk_crypto_config * cfg)71a7a5bc5fSEric Biggers static void fscrypt_log_blk_crypto_impl(struct fscrypt_mode *mode,
720e91fc1eSChristoph Hellwig struct block_device **devs,
730e91fc1eSChristoph Hellwig unsigned int num_devs,
74a7a5bc5fSEric Biggers const struct blk_crypto_config *cfg)
75a7a5bc5fSEric Biggers {
760e91fc1eSChristoph Hellwig unsigned int i;
77a7a5bc5fSEric Biggers
78a7a5bc5fSEric Biggers for (i = 0; i < num_devs; i++) {
79a7a5bc5fSEric Biggers if (!IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) ||
806715c98bSChristoph Hellwig blk_crypto_config_supported_natively(devs[i], cfg)) {
81a7a5bc5fSEric Biggers if (!xchg(&mode->logged_blk_crypto_native, 1))
82a7a5bc5fSEric Biggers pr_info("fscrypt: %s using blk-crypto (native)\n",
83a7a5bc5fSEric Biggers mode->friendly_name);
84a7a5bc5fSEric Biggers } else if (!xchg(&mode->logged_blk_crypto_fallback, 1)) {
85a7a5bc5fSEric Biggers pr_info("fscrypt: %s using blk-crypto-fallback\n",
86a7a5bc5fSEric Biggers mode->friendly_name);
87a7a5bc5fSEric Biggers }
88a7a5bc5fSEric Biggers }
89a7a5bc5fSEric Biggers }
90a7a5bc5fSEric Biggers
915fee3609SSatya Tangirala /* Enable inline encryption for this file if supported. */
fscrypt_select_encryption_impl(struct fscrypt_inode_info * ci)923e7807d5SJosef Bacik int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci)
935fee3609SSatya Tangirala {
945fee3609SSatya Tangirala const struct inode *inode = ci->ci_inode;
955fee3609SSatya Tangirala struct super_block *sb = inode->i_sb;
965fee3609SSatya Tangirala struct blk_crypto_config crypto_cfg;
970e91fc1eSChristoph Hellwig struct block_device **devs;
980e91fc1eSChristoph Hellwig unsigned int num_devs;
990e91fc1eSChristoph Hellwig unsigned int i;
1005fee3609SSatya Tangirala
1015fee3609SSatya Tangirala /* The file must need contents encryption, not filenames encryption */
102d19d8d34SEric Biggers if (!S_ISREG(inode->i_mode))
1035fee3609SSatya Tangirala return 0;
1045fee3609SSatya Tangirala
1055fee3609SSatya Tangirala /* The crypto mode must have a blk-crypto counterpart */
1065fee3609SSatya Tangirala if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
1075fee3609SSatya Tangirala return 0;
1085fee3609SSatya Tangirala
1095fee3609SSatya Tangirala /* The filesystem must be mounted with -o inlinecrypt */
1105fee3609SSatya Tangirala if (!(sb->s_flags & SB_INLINECRYPT))
1115fee3609SSatya Tangirala return 0;
1125fee3609SSatya Tangirala
1135fee3609SSatya Tangirala /*
1145fee3609SSatya Tangirala * When a page contains multiple logically contiguous filesystem blocks,
1155fee3609SSatya Tangirala * some filesystem code only calls fscrypt_mergeable_bio() for the first
1165fee3609SSatya Tangirala * block in the page. This is fine for most of fscrypt's IV generation
1175fee3609SSatya Tangirala * strategies, where contiguous blocks imply contiguous IVs. But it
1185fee3609SSatya Tangirala * doesn't work with IV_INO_LBLK_32. For now, simply exclude
1195fee3609SSatya Tangirala * IV_INO_LBLK_32 with blocksize != PAGE_SIZE from inline encryption.
1205fee3609SSatya Tangirala */
1215fee3609SSatya Tangirala if ((fscrypt_policy_flags(&ci->ci_policy) &
1225fee3609SSatya Tangirala FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
1235fee3609SSatya Tangirala sb->s_blocksize != PAGE_SIZE)
1245fee3609SSatya Tangirala return 0;
1255fee3609SSatya Tangirala
1265fee3609SSatya Tangirala /*
1270e91fc1eSChristoph Hellwig * On all the filesystem's block devices, blk-crypto must support the
1280e91fc1eSChristoph Hellwig * crypto configuration that the file would use.
1295fee3609SSatya Tangirala */
1305fee3609SSatya Tangirala crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode;
1315b118884SEric Biggers crypto_cfg.data_unit_size = 1U << ci->ci_data_unit_bits;
1325fee3609SSatya Tangirala crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
133*ebc41765SEric Biggers crypto_cfg.key_type = BLK_CRYPTO_KEY_TYPE_RAW;
1340e91fc1eSChristoph Hellwig
1350e91fc1eSChristoph Hellwig devs = fscrypt_get_devices(sb, &num_devs);
1360e91fc1eSChristoph Hellwig if (IS_ERR(devs))
1370e91fc1eSChristoph Hellwig return PTR_ERR(devs);
1385fee3609SSatya Tangirala
1395fee3609SSatya Tangirala for (i = 0; i < num_devs; i++) {
140fce3caeaSChristoph Hellwig if (!blk_crypto_config_supported(devs[i], &crypto_cfg))
1415fee3609SSatya Tangirala goto out_free_devs;
1425fee3609SSatya Tangirala }
1435fee3609SSatya Tangirala
144a7a5bc5fSEric Biggers fscrypt_log_blk_crypto_impl(ci->ci_mode, devs, num_devs, &crypto_cfg);
145a7a5bc5fSEric Biggers
1465fee3609SSatya Tangirala ci->ci_inlinecrypt = true;
1475fee3609SSatya Tangirala out_free_devs:
1485fee3609SSatya Tangirala kfree(devs);
1495fee3609SSatya Tangirala
1505fee3609SSatya Tangirala return 0;
1515fee3609SSatya Tangirala }
1525fee3609SSatya Tangirala
fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,const struct fscrypt_inode_info * ci)1535fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
1545fee3609SSatya Tangirala const u8 *raw_key,
1553e7807d5SJosef Bacik const struct fscrypt_inode_info *ci)
1565fee3609SSatya Tangirala {
1575fee3609SSatya Tangirala const struct inode *inode = ci->ci_inode;
1585fee3609SSatya Tangirala struct super_block *sb = inode->i_sb;
1595fee3609SSatya Tangirala enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode;
16022e9947aSEric Biggers struct blk_crypto_key *blk_key;
1610e91fc1eSChristoph Hellwig struct block_device **devs;
16222e9947aSEric Biggers unsigned int num_devs;
16322e9947aSEric Biggers unsigned int i;
1645fee3609SSatya Tangirala int err;
1655fee3609SSatya Tangirala
16622e9947aSEric Biggers blk_key = kmalloc(sizeof(*blk_key), GFP_KERNEL);
1675fee3609SSatya Tangirala if (!blk_key)
1685fee3609SSatya Tangirala return -ENOMEM;
1695fee3609SSatya Tangirala
170*ebc41765SEric Biggers err = blk_crypto_init_key(blk_key, raw_key, ci->ci_mode->keysize,
171*ebc41765SEric Biggers BLK_CRYPTO_KEY_TYPE_RAW, crypto_mode,
1725b118884SEric Biggers fscrypt_get_dun_bytes(ci),
1735b118884SEric Biggers 1U << ci->ci_data_unit_bits);
1745fee3609SSatya Tangirala if (err) {
1755fee3609SSatya Tangirala fscrypt_err(inode, "error %d initializing blk-crypto key", err);
1765fee3609SSatya Tangirala goto fail;
1775fee3609SSatya Tangirala }
1785fee3609SSatya Tangirala
17922e9947aSEric Biggers /* Start using blk-crypto on all the filesystem's block devices. */
1800e91fc1eSChristoph Hellwig devs = fscrypt_get_devices(sb, &num_devs);
1810e91fc1eSChristoph Hellwig if (IS_ERR(devs)) {
1820e91fc1eSChristoph Hellwig err = PTR_ERR(devs);
18322e9947aSEric Biggers goto fail;
18422e9947aSEric Biggers }
1855fee3609SSatya Tangirala for (i = 0; i < num_devs; i++) {
186fce3caeaSChristoph Hellwig err = blk_crypto_start_using_key(devs[i], blk_key);
18722e9947aSEric Biggers if (err)
18822e9947aSEric Biggers break;
1895fee3609SSatya Tangirala }
19022e9947aSEric Biggers kfree(devs);
1915fee3609SSatya Tangirala if (err) {
19222e9947aSEric Biggers fscrypt_err(inode, "error %d starting to use blk-crypto", err);
1935fee3609SSatya Tangirala goto fail;
1945fee3609SSatya Tangirala }
19522e9947aSEric Biggers
1965fee3609SSatya Tangirala /*
19797c6327fSEric Biggers * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
19897c6327fSEric Biggers * I.e., here we publish ->blk_key with a RELEASE barrier so that
19997c6327fSEric Biggers * concurrent tasks can ACQUIRE it. Note that this concurrency is only
20097c6327fSEric Biggers * possible for per-mode keys, not for per-file keys.
2015fee3609SSatya Tangirala */
2025fee3609SSatya Tangirala smp_store_release(&prep_key->blk_key, blk_key);
2035fee3609SSatya Tangirala return 0;
2045fee3609SSatya Tangirala
2055fee3609SSatya Tangirala fail:
206453431a5SWaiman Long kfree_sensitive(blk_key);
2075fee3609SSatya Tangirala return err;
2085fee3609SSatya Tangirala }
2095fee3609SSatya Tangirala
fscrypt_destroy_inline_crypt_key(struct super_block * sb,struct fscrypt_prepared_key * prep_key)21022e9947aSEric Biggers void fscrypt_destroy_inline_crypt_key(struct super_block *sb,
21122e9947aSEric Biggers struct fscrypt_prepared_key *prep_key)
2125fee3609SSatya Tangirala {
21322e9947aSEric Biggers struct blk_crypto_key *blk_key = prep_key->blk_key;
2140e91fc1eSChristoph Hellwig struct block_device **devs;
21522e9947aSEric Biggers unsigned int num_devs;
21622e9947aSEric Biggers unsigned int i;
2175fee3609SSatya Tangirala
21822e9947aSEric Biggers if (!blk_key)
21922e9947aSEric Biggers return;
22022e9947aSEric Biggers
22122e9947aSEric Biggers /* Evict the key from all the filesystem's block devices. */
2220e91fc1eSChristoph Hellwig devs = fscrypt_get_devices(sb, &num_devs);
2230e91fc1eSChristoph Hellwig if (!IS_ERR(devs)) {
22422e9947aSEric Biggers for (i = 0; i < num_devs; i++)
225fce3caeaSChristoph Hellwig blk_crypto_evict_key(devs[i], blk_key);
22622e9947aSEric Biggers kfree(devs);
2275fee3609SSatya Tangirala }
228453431a5SWaiman Long kfree_sensitive(blk_key);
2295fee3609SSatya Tangirala }
2305fee3609SSatya Tangirala
__fscrypt_inode_uses_inline_crypto(const struct inode * inode)2315fee3609SSatya Tangirala bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
2325fee3609SSatya Tangirala {
2335fee3609SSatya Tangirala return inode->i_crypt_info->ci_inlinecrypt;
2345fee3609SSatya Tangirala }
2355fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
2365fee3609SSatya Tangirala
fscrypt_generate_dun(const struct fscrypt_inode_info * ci,u64 lblk_num,u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])2373e7807d5SJosef Bacik static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
2383e7807d5SJosef Bacik u64 lblk_num,
2395fee3609SSatya Tangirala u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
2405fee3609SSatya Tangirala {
2415b118884SEric Biggers u64 index = lblk_num << ci->ci_data_units_per_block_bits;
2425fee3609SSatya Tangirala union fscrypt_iv iv;
2435fee3609SSatya Tangirala int i;
2445fee3609SSatya Tangirala
2455b118884SEric Biggers fscrypt_generate_iv(&iv, index, ci);
2465fee3609SSatya Tangirala
2475fee3609SSatya Tangirala BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
2485fee3609SSatya Tangirala memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
2495fee3609SSatya Tangirala for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++)
2505fee3609SSatya Tangirala dun[i] = le64_to_cpu(iv.dun[i]);
2515fee3609SSatya Tangirala }
2525fee3609SSatya Tangirala
2535fee3609SSatya Tangirala /**
2545fee3609SSatya Tangirala * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
2555fee3609SSatya Tangirala * @bio: a bio which will eventually be submitted to the file
2565fee3609SSatya Tangirala * @inode: the file's inode
2575fee3609SSatya Tangirala * @first_lblk: the first file logical block number in the I/O
2585fee3609SSatya Tangirala * @gfp_mask: memory allocation flags - these must be a waiting mask so that
2595fee3609SSatya Tangirala * bio_crypt_set_ctx can't fail.
2605fee3609SSatya Tangirala *
2615fee3609SSatya Tangirala * If the contents of the file should be encrypted (or decrypted) with inline
2625fee3609SSatya Tangirala * encryption, then assign the appropriate encryption context to the bio.
2635fee3609SSatya Tangirala *
2645fee3609SSatya Tangirala * Normally the bio should be newly allocated (i.e. no pages added yet), as
2655fee3609SSatya Tangirala * otherwise fscrypt_mergeable_bio() won't work as intended.
2665fee3609SSatya Tangirala *
2675fee3609SSatya Tangirala * The encryption context will be freed automatically when the bio is freed.
2685fee3609SSatya Tangirala */
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)2695fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
2705fee3609SSatya Tangirala u64 first_lblk, gfp_t gfp_mask)
2715fee3609SSatya Tangirala {
2723e7807d5SJosef Bacik const struct fscrypt_inode_info *ci;
2735fee3609SSatya Tangirala u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
2745fee3609SSatya Tangirala
2755fee3609SSatya Tangirala if (!fscrypt_inode_uses_inline_crypto(inode))
2765fee3609SSatya Tangirala return;
27755e32c54SEric Biggers ci = inode->i_crypt_info;
2785fee3609SSatya Tangirala
2795fee3609SSatya Tangirala fscrypt_generate_dun(ci, first_lblk, dun);
28022e9947aSEric Biggers bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask);
2815fee3609SSatya Tangirala }
2825fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
2835fee3609SSatya Tangirala
2845fee3609SSatya Tangirala /* Extract the inode and logical block number from a buffer_head. */
bh_get_inode_and_lblk_num(const struct buffer_head * bh,const struct inode ** inode_ret,u64 * lblk_num_ret)2855fee3609SSatya Tangirala static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
2865fee3609SSatya Tangirala const struct inode **inode_ret,
2875fee3609SSatya Tangirala u64 *lblk_num_ret)
2885fee3609SSatya Tangirala {
289262f014dSMatthew Wilcox (Oracle) struct folio *folio = bh->b_folio;
2905fee3609SSatya Tangirala const struct address_space *mapping;
2915fee3609SSatya Tangirala const struct inode *inode;
2925fee3609SSatya Tangirala
2935fee3609SSatya Tangirala /*
2945fee3609SSatya Tangirala * The ext4 journal (jbd2) can submit a buffer_head it directly created
2955fee3609SSatya Tangirala * for a non-pagecache page. fscrypt doesn't care about these.
2965fee3609SSatya Tangirala */
297262f014dSMatthew Wilcox (Oracle) mapping = folio_mapping(folio);
2985fee3609SSatya Tangirala if (!mapping)
2995fee3609SSatya Tangirala return false;
3005fee3609SSatya Tangirala inode = mapping->host;
3015fee3609SSatya Tangirala
3025fee3609SSatya Tangirala *inode_ret = inode;
303262f014dSMatthew Wilcox (Oracle) *lblk_num_ret = ((u64)folio->index << (PAGE_SHIFT - inode->i_blkbits)) +
3045fee3609SSatya Tangirala (bh_offset(bh) >> inode->i_blkbits);
3055fee3609SSatya Tangirala return true;
3065fee3609SSatya Tangirala }
3075fee3609SSatya Tangirala
3085fee3609SSatya Tangirala /**
3095fee3609SSatya Tangirala * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
3105fee3609SSatya Tangirala * crypto
3115fee3609SSatya Tangirala * @bio: a bio which will eventually be submitted to the file
3125fee3609SSatya Tangirala * @first_bh: the first buffer_head for which I/O will be submitted
3135fee3609SSatya Tangirala * @gfp_mask: memory allocation flags
3145fee3609SSatya Tangirala *
3155fee3609SSatya Tangirala * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
3165fee3609SSatya Tangirala * of an inode and block number directly.
3175fee3609SSatya Tangirala */
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)3185fee3609SSatya Tangirala void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
3195fee3609SSatya Tangirala const struct buffer_head *first_bh,
3205fee3609SSatya Tangirala gfp_t gfp_mask)
3215fee3609SSatya Tangirala {
3225fee3609SSatya Tangirala const struct inode *inode;
3235fee3609SSatya Tangirala u64 first_lblk;
3245fee3609SSatya Tangirala
3255fee3609SSatya Tangirala if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
3265fee3609SSatya Tangirala fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
3275fee3609SSatya Tangirala }
3285fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
3295fee3609SSatya Tangirala
3305fee3609SSatya Tangirala /**
3315fee3609SSatya Tangirala * fscrypt_mergeable_bio() - test whether data can be added to a bio
3325fee3609SSatya Tangirala * @bio: the bio being built up
3335fee3609SSatya Tangirala * @inode: the inode for the next part of the I/O
3345fee3609SSatya Tangirala * @next_lblk: the next file logical block number in the I/O
3355fee3609SSatya Tangirala *
3365fee3609SSatya Tangirala * When building a bio which may contain data which should undergo inline
3375fee3609SSatya Tangirala * encryption (or decryption) via fscrypt, filesystems should call this function
3385fee3609SSatya Tangirala * to ensure that the resulting bio contains only contiguous data unit numbers.
3395fee3609SSatya Tangirala * This will return false if the next part of the I/O cannot be merged with the
3405fee3609SSatya Tangirala * bio because either the encryption key would be different or the encryption
3415fee3609SSatya Tangirala * data unit numbers would be discontiguous.
3425fee3609SSatya Tangirala *
3435fee3609SSatya Tangirala * fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
3445fee3609SSatya Tangirala *
345c6c89783SEric Biggers * This function isn't required in cases where crypto-mergeability is ensured in
346c6c89783SEric Biggers * another way, such as I/O targeting only a single file (and thus a single key)
347c6c89783SEric Biggers * combined with fscrypt_limit_io_blocks() to ensure DUN contiguity.
348c6c89783SEric Biggers *
3495fee3609SSatya Tangirala * Return: true iff the I/O is mergeable
3505fee3609SSatya Tangirala */
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)3515fee3609SSatya Tangirala bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
3525fee3609SSatya Tangirala u64 next_lblk)
3535fee3609SSatya Tangirala {
3545fee3609SSatya Tangirala const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
3555fee3609SSatya Tangirala u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
3565fee3609SSatya Tangirala
3575fee3609SSatya Tangirala if (!!bc != fscrypt_inode_uses_inline_crypto(inode))
3585fee3609SSatya Tangirala return false;
3595fee3609SSatya Tangirala if (!bc)
3605fee3609SSatya Tangirala return true;
3615fee3609SSatya Tangirala
3625fee3609SSatya Tangirala /*
3635fee3609SSatya Tangirala * Comparing the key pointers is good enough, as all I/O for each key
3645fee3609SSatya Tangirala * uses the same pointer. I.e., there's currently no need to support
3655fee3609SSatya Tangirala * merging requests where the keys are the same but the pointers differ.
3665fee3609SSatya Tangirala */
36722e9947aSEric Biggers if (bc->bc_key != inode->i_crypt_info->ci_enc_key.blk_key)
3685fee3609SSatya Tangirala return false;
3695fee3609SSatya Tangirala
3705fee3609SSatya Tangirala fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
3715fee3609SSatya Tangirala return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
3725fee3609SSatya Tangirala }
3735fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
3745fee3609SSatya Tangirala
3755fee3609SSatya Tangirala /**
3765fee3609SSatya Tangirala * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
3775fee3609SSatya Tangirala * @bio: the bio being built up
3785fee3609SSatya Tangirala * @next_bh: the next buffer_head for which I/O will be submitted
3795fee3609SSatya Tangirala *
3805fee3609SSatya Tangirala * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
3815fee3609SSatya Tangirala * an inode and block number directly.
3825fee3609SSatya Tangirala *
3835fee3609SSatya Tangirala * Return: true iff the I/O is mergeable
3845fee3609SSatya Tangirala */
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)3855fee3609SSatya Tangirala bool fscrypt_mergeable_bio_bh(struct bio *bio,
3865fee3609SSatya Tangirala const struct buffer_head *next_bh)
3875fee3609SSatya Tangirala {
3885fee3609SSatya Tangirala const struct inode *inode;
3895fee3609SSatya Tangirala u64 next_lblk;
3905fee3609SSatya Tangirala
3915fee3609SSatya Tangirala if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
3925fee3609SSatya Tangirala return !bio->bi_crypt_context;
3935fee3609SSatya Tangirala
3945fee3609SSatya Tangirala return fscrypt_mergeable_bio(bio, inode, next_lblk);
3955fee3609SSatya Tangirala }
3965fee3609SSatya Tangirala EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
397c6c89783SEric Biggers
398c6c89783SEric Biggers /**
39953dd3f80SEric Biggers * fscrypt_dio_supported() - check whether DIO (direct I/O) is supported on an
40053dd3f80SEric Biggers * inode, as far as encryption is concerned
40153dd3f80SEric Biggers * @inode: the inode in question
402c6c89783SEric Biggers *
403c6c89783SEric Biggers * Return: %true if there are no encryption constraints that prevent DIO from
404c6c89783SEric Biggers * being supported; %false if DIO is unsupported. (Note that in the
405c6c89783SEric Biggers * %true case, the filesystem might have other, non-encryption-related
40653dd3f80SEric Biggers * constraints that prevent DIO from actually being supported. Also, on
40753dd3f80SEric Biggers * encrypted files the filesystem is still responsible for only allowing
40853dd3f80SEric Biggers * DIO when requests are filesystem-block-aligned.)
409c6c89783SEric Biggers */
fscrypt_dio_supported(struct inode * inode)41053dd3f80SEric Biggers bool fscrypt_dio_supported(struct inode *inode)
411c6c89783SEric Biggers {
41253dd3f80SEric Biggers int err;
413c6c89783SEric Biggers
414c6c89783SEric Biggers /* If the file is unencrypted, no veto from us. */
415c6c89783SEric Biggers if (!fscrypt_needs_contents_encryption(inode))
416c6c89783SEric Biggers return true;
417c6c89783SEric Biggers
418c6c89783SEric Biggers /*
41953dd3f80SEric Biggers * We only support DIO with inline crypto, not fs-layer crypto.
420c6c89783SEric Biggers *
42153dd3f80SEric Biggers * To determine whether the inode is using inline crypto, we have to set
42253dd3f80SEric Biggers * up the key if it wasn't already done. This is because in the current
42353dd3f80SEric Biggers * design of fscrypt, the decision of whether to use inline crypto or
42453dd3f80SEric Biggers * not isn't made until the inode's encryption key is being set up. In
42553dd3f80SEric Biggers * the DIO read/write case, the key will always be set up already, since
42653dd3f80SEric Biggers * the file will be open. But in the case of statx(), the key might not
42753dd3f80SEric Biggers * be set up yet, as the file might not have been opened yet.
428c6c89783SEric Biggers */
42953dd3f80SEric Biggers err = fscrypt_require_key(inode);
43053dd3f80SEric Biggers if (err) {
43153dd3f80SEric Biggers /*
43253dd3f80SEric Biggers * Key unavailable or couldn't be set up. This edge case isn't
43353dd3f80SEric Biggers * worth worrying about; just report that DIO is unsupported.
43453dd3f80SEric Biggers */
435c6c89783SEric Biggers return false;
43653dd3f80SEric Biggers }
43753dd3f80SEric Biggers return fscrypt_inode_uses_inline_crypto(inode);
438c6c89783SEric Biggers }
439c6c89783SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
440c6c89783SEric Biggers
441c6c89783SEric Biggers /**
442c6c89783SEric Biggers * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs
443c6c89783SEric Biggers * @inode: the file on which I/O is being done
444c6c89783SEric Biggers * @lblk: the block at which the I/O is being started from
445c6c89783SEric Biggers * @nr_blocks: the number of blocks we want to submit starting at @lblk
446c6c89783SEric Biggers *
447c6c89783SEric Biggers * Determine the limit to the number of blocks that can be submitted in a bio
448c6c89783SEric Biggers * targeting @lblk without causing a data unit number (DUN) discontiguity.
449c6c89783SEric Biggers *
450c6c89783SEric Biggers * This is normally just @nr_blocks, as normally the DUNs just increment along
451c6c89783SEric Biggers * with the logical blocks. (Or the file is not encrypted.)
452c6c89783SEric Biggers *
453c6c89783SEric Biggers * In rare cases, fscrypt can be using an IV generation method that allows the
454c6c89783SEric Biggers * DUN to wrap around within logically contiguous blocks, and that wraparound
455c6c89783SEric Biggers * will occur. If this happens, a value less than @nr_blocks will be returned
456c6c89783SEric Biggers * so that the wraparound doesn't occur in the middle of a bio, which would
457c6c89783SEric Biggers * cause encryption/decryption to produce wrong results.
458c6c89783SEric Biggers *
459c6c89783SEric Biggers * Return: the actual number of blocks that can be submitted
460c6c89783SEric Biggers */
fscrypt_limit_io_blocks(const struct inode * inode,u64 lblk,u64 nr_blocks)461c6c89783SEric Biggers u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks)
462c6c89783SEric Biggers {
4633e7807d5SJosef Bacik const struct fscrypt_inode_info *ci;
464c6c89783SEric Biggers u32 dun;
465c6c89783SEric Biggers
466c6c89783SEric Biggers if (!fscrypt_inode_uses_inline_crypto(inode))
467c6c89783SEric Biggers return nr_blocks;
468c6c89783SEric Biggers
469c6c89783SEric Biggers if (nr_blocks <= 1)
470c6c89783SEric Biggers return nr_blocks;
471c6c89783SEric Biggers
472c6c89783SEric Biggers ci = inode->i_crypt_info;
473c6c89783SEric Biggers if (!(fscrypt_policy_flags(&ci->ci_policy) &
474c6c89783SEric Biggers FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
475c6c89783SEric Biggers return nr_blocks;
476c6c89783SEric Biggers
477c6c89783SEric Biggers /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
478c6c89783SEric Biggers
479c6c89783SEric Biggers dun = ci->ci_hashed_ino + lblk;
480c6c89783SEric Biggers
481c6c89783SEric Biggers return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
482c6c89783SEric Biggers }
483c6c89783SEric Biggers EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);
484