1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Inline encryption support for fscrypt
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2019 Google LLC
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * With "inline encryption", the block layer handles the decryption/encryption
10*4882a593Smuzhiyun * as part of the bio, instead of the filesystem doing the crypto itself via
11*4882a593Smuzhiyun * crypto API. See Documentation/block/inline-encryption.rst. fscrypt still
12*4882a593Smuzhiyun * provides the key and IV to use.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/blk-crypto.h>
16*4882a593Smuzhiyun #include <linux/blkdev.h>
17*4882a593Smuzhiyun #include <linux/buffer_head.h>
18*4882a593Smuzhiyun #include <linux/keyslot-manager.h>
19*4882a593Smuzhiyun #include <linux/sched/mm.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/uio.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "fscrypt_private.h"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct fscrypt_blk_crypto_key {
26*4882a593Smuzhiyun struct blk_crypto_key base;
27*4882a593Smuzhiyun int num_devs;
28*4882a593Smuzhiyun struct request_queue *devs[];
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
fscrypt_get_num_devices(struct super_block * sb)31*4882a593Smuzhiyun static int fscrypt_get_num_devices(struct super_block *sb)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun if (sb->s_cop->get_num_devices)
34*4882a593Smuzhiyun return sb->s_cop->get_num_devices(sb);
35*4882a593Smuzhiyun return 1;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
fscrypt_get_devices(struct super_block * sb,int num_devs,struct request_queue ** devs)38*4882a593Smuzhiyun static void fscrypt_get_devices(struct super_block *sb, int num_devs,
39*4882a593Smuzhiyun struct request_queue **devs)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun if (num_devs == 1)
42*4882a593Smuzhiyun devs[0] = bdev_get_queue(sb->s_bdev);
43*4882a593Smuzhiyun else
44*4882a593Smuzhiyun sb->s_cop->get_devices(sb, devs);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
fscrypt_get_dun_bytes(const struct fscrypt_info * ci)47*4882a593Smuzhiyun static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_info *ci)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun struct super_block *sb = ci->ci_inode->i_sb;
50*4882a593Smuzhiyun unsigned int flags = fscrypt_policy_flags(&ci->ci_policy);
51*4882a593Smuzhiyun int ino_bits = 64, lblk_bits = 64;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
54*4882a593Smuzhiyun return offsetofend(union fscrypt_iv, nonce);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64)
57*4882a593Smuzhiyun return sizeof(__le64);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
60*4882a593Smuzhiyun return sizeof(__le32);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Default case: IVs are just the file logical block number */
63*4882a593Smuzhiyun if (sb->s_cop->get_ino_and_lblk_bits)
64*4882a593Smuzhiyun sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
65*4882a593Smuzhiyun return DIV_ROUND_UP(lblk_bits, 8);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* Enable inline encryption for this file if supported. */
fscrypt_select_encryption_impl(struct fscrypt_info * ci,bool is_hw_wrapped_key)69*4882a593Smuzhiyun int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
70*4882a593Smuzhiyun bool is_hw_wrapped_key)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun const struct inode *inode = ci->ci_inode;
73*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
74*4882a593Smuzhiyun struct blk_crypto_config crypto_cfg;
75*4882a593Smuzhiyun int num_devs;
76*4882a593Smuzhiyun struct request_queue **devs;
77*4882a593Smuzhiyun int i;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* The file must need contents encryption, not filenames encryption */
80*4882a593Smuzhiyun if (!S_ISREG(inode->i_mode))
81*4882a593Smuzhiyun return 0;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* The crypto mode must have a blk-crypto counterpart */
84*4882a593Smuzhiyun if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
85*4882a593Smuzhiyun return 0;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* The filesystem must be mounted with -o inlinecrypt */
88*4882a593Smuzhiyun if (!(sb->s_flags & SB_INLINECRYPT))
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * When a page contains multiple logically contiguous filesystem blocks,
93*4882a593Smuzhiyun * some filesystem code only calls fscrypt_mergeable_bio() for the first
94*4882a593Smuzhiyun * block in the page. This is fine for most of fscrypt's IV generation
95*4882a593Smuzhiyun * strategies, where contiguous blocks imply contiguous IVs. But it
96*4882a593Smuzhiyun * doesn't work with IV_INO_LBLK_32. For now, simply exclude
97*4882a593Smuzhiyun * IV_INO_LBLK_32 with blocksize != PAGE_SIZE from inline encryption.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun if ((fscrypt_policy_flags(&ci->ci_policy) &
100*4882a593Smuzhiyun FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
101*4882a593Smuzhiyun sb->s_blocksize != PAGE_SIZE)
102*4882a593Smuzhiyun return 0;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * On all the filesystem's devices, blk-crypto must support the crypto
106*4882a593Smuzhiyun * configuration that the file would use.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode;
109*4882a593Smuzhiyun crypto_cfg.data_unit_size = sb->s_blocksize;
110*4882a593Smuzhiyun crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
111*4882a593Smuzhiyun crypto_cfg.is_hw_wrapped = is_hw_wrapped_key;
112*4882a593Smuzhiyun num_devs = fscrypt_get_num_devices(sb);
113*4882a593Smuzhiyun devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL);
114*4882a593Smuzhiyun if (!devs)
115*4882a593Smuzhiyun return -ENOMEM;
116*4882a593Smuzhiyun fscrypt_get_devices(sb, num_devs, devs);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun for (i = 0; i < num_devs; i++) {
119*4882a593Smuzhiyun if (!blk_crypto_config_supported(devs[i], &crypto_cfg))
120*4882a593Smuzhiyun goto out_free_devs;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun ci->ci_inlinecrypt = true;
124*4882a593Smuzhiyun out_free_devs:
125*4882a593Smuzhiyun kfree(devs);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,unsigned int raw_key_size,bool is_hw_wrapped,const struct fscrypt_info * ci)130*4882a593Smuzhiyun int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
131*4882a593Smuzhiyun const u8 *raw_key,
132*4882a593Smuzhiyun unsigned int raw_key_size,
133*4882a593Smuzhiyun bool is_hw_wrapped,
134*4882a593Smuzhiyun const struct fscrypt_info *ci)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun const struct inode *inode = ci->ci_inode;
137*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
138*4882a593Smuzhiyun enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode;
139*4882a593Smuzhiyun int num_devs = fscrypt_get_num_devices(sb);
140*4882a593Smuzhiyun int queue_refs = 0;
141*4882a593Smuzhiyun struct fscrypt_blk_crypto_key *blk_key;
142*4882a593Smuzhiyun int err;
143*4882a593Smuzhiyun int i;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL);
146*4882a593Smuzhiyun if (!blk_key)
147*4882a593Smuzhiyun return -ENOMEM;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun blk_key->num_devs = num_devs;
150*4882a593Smuzhiyun fscrypt_get_devices(sb, num_devs, blk_key->devs);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun BUILD_BUG_ON(FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE >
153*4882a593Smuzhiyun BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun err = blk_crypto_init_key(&blk_key->base, raw_key, raw_key_size,
156*4882a593Smuzhiyun is_hw_wrapped, crypto_mode,
157*4882a593Smuzhiyun fscrypt_get_dun_bytes(ci), sb->s_blocksize);
158*4882a593Smuzhiyun if (err) {
159*4882a593Smuzhiyun fscrypt_err(inode, "error %d initializing blk-crypto key", err);
160*4882a593Smuzhiyun goto fail;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * We have to start using blk-crypto on all the filesystem's devices.
165*4882a593Smuzhiyun * We also have to save all the request_queue's for later so that the
166*4882a593Smuzhiyun * key can be evicted from them. This is needed because some keys
167*4882a593Smuzhiyun * aren't destroyed until after the filesystem was already unmounted
168*4882a593Smuzhiyun * (namely, the per-mode keys in struct fscrypt_master_key).
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun for (i = 0; i < num_devs; i++) {
171*4882a593Smuzhiyun if (!blk_get_queue(blk_key->devs[i])) {
172*4882a593Smuzhiyun fscrypt_err(inode, "couldn't get request_queue");
173*4882a593Smuzhiyun err = -EAGAIN;
174*4882a593Smuzhiyun goto fail;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun queue_refs++;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun err = blk_crypto_start_using_key(&blk_key->base,
179*4882a593Smuzhiyun blk_key->devs[i]);
180*4882a593Smuzhiyun if (err) {
181*4882a593Smuzhiyun fscrypt_err(inode,
182*4882a593Smuzhiyun "error %d starting to use blk-crypto", err);
183*4882a593Smuzhiyun goto fail;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
188*4882a593Smuzhiyun * I.e., here we publish ->blk_key with a RELEASE barrier so that
189*4882a593Smuzhiyun * concurrent tasks can ACQUIRE it. Note that this concurrency is only
190*4882a593Smuzhiyun * possible for per-mode keys, not for per-file keys.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun smp_store_release(&prep_key->blk_key, blk_key);
193*4882a593Smuzhiyun return 0;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun fail:
196*4882a593Smuzhiyun for (i = 0; i < queue_refs; i++)
197*4882a593Smuzhiyun blk_put_queue(blk_key->devs[i]);
198*4882a593Smuzhiyun kfree_sensitive(blk_key);
199*4882a593Smuzhiyun return err;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key * prep_key)202*4882a593Smuzhiyun void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun struct fscrypt_blk_crypto_key *blk_key = prep_key->blk_key;
205*4882a593Smuzhiyun int i;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (blk_key) {
208*4882a593Smuzhiyun for (i = 0; i < blk_key->num_devs; i++) {
209*4882a593Smuzhiyun blk_crypto_evict_key(blk_key->devs[i], &blk_key->base);
210*4882a593Smuzhiyun blk_put_queue(blk_key->devs[i]);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun kfree_sensitive(blk_key);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
fscrypt_derive_raw_secret(struct super_block * sb,const u8 * wrapped_key,unsigned int wrapped_key_size,u8 * raw_secret,unsigned int raw_secret_size)216*4882a593Smuzhiyun int fscrypt_derive_raw_secret(struct super_block *sb,
217*4882a593Smuzhiyun const u8 *wrapped_key,
218*4882a593Smuzhiyun unsigned int wrapped_key_size,
219*4882a593Smuzhiyun u8 *raw_secret, unsigned int raw_secret_size)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun struct request_queue *q;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun q = bdev_get_queue(sb->s_bdev);
224*4882a593Smuzhiyun if (!q->ksm)
225*4882a593Smuzhiyun return -EOPNOTSUPP;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun return blk_ksm_derive_raw_secret(q->ksm, wrapped_key, wrapped_key_size,
228*4882a593Smuzhiyun raw_secret, raw_secret_size);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
__fscrypt_inode_uses_inline_crypto(const struct inode * inode)231*4882a593Smuzhiyun bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun return inode->i_crypt_info->ci_inlinecrypt;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
236*4882a593Smuzhiyun
fscrypt_generate_dun(const struct fscrypt_info * ci,u64 lblk_num,u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])237*4882a593Smuzhiyun static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
238*4882a593Smuzhiyun u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun union fscrypt_iv iv;
241*4882a593Smuzhiyun int i;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun fscrypt_generate_iv(&iv, lblk_num, ci);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
246*4882a593Smuzhiyun memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
247*4882a593Smuzhiyun for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++)
248*4882a593Smuzhiyun dun[i] = le64_to_cpu(iv.dun[i]);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
253*4882a593Smuzhiyun * @bio: a bio which will eventually be submitted to the file
254*4882a593Smuzhiyun * @inode: the file's inode
255*4882a593Smuzhiyun * @first_lblk: the first file logical block number in the I/O
256*4882a593Smuzhiyun * @gfp_mask: memory allocation flags - these must be a waiting mask so that
257*4882a593Smuzhiyun * bio_crypt_set_ctx can't fail.
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * If the contents of the file should be encrypted (or decrypted) with inline
260*4882a593Smuzhiyun * encryption, then assign the appropriate encryption context to the bio.
261*4882a593Smuzhiyun *
262*4882a593Smuzhiyun * Normally the bio should be newly allocated (i.e. no pages added yet), as
263*4882a593Smuzhiyun * otherwise fscrypt_mergeable_bio() won't work as intended.
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * The encryption context will be freed automatically when the bio is freed.
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * This function also handles setting bi_skip_dm_default_key when needed.
268*4882a593Smuzhiyun */
fscrypt_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,u64 first_lblk,gfp_t gfp_mask)269*4882a593Smuzhiyun void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
270*4882a593Smuzhiyun u64 first_lblk, gfp_t gfp_mask)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun const struct fscrypt_info *ci;
273*4882a593Smuzhiyun u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun if (fscrypt_inode_should_skip_dm_default_key(inode))
276*4882a593Smuzhiyun bio_set_skip_dm_default_key(bio);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (!fscrypt_inode_uses_inline_crypto(inode))
279*4882a593Smuzhiyun return;
280*4882a593Smuzhiyun ci = inode->i_crypt_info;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun fscrypt_generate_dun(ci, first_lblk, dun);
283*4882a593Smuzhiyun bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* 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)288*4882a593Smuzhiyun static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
289*4882a593Smuzhiyun const struct inode **inode_ret,
290*4882a593Smuzhiyun u64 *lblk_num_ret)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun struct page *page = bh->b_page;
293*4882a593Smuzhiyun const struct address_space *mapping;
294*4882a593Smuzhiyun const struct inode *inode;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /*
297*4882a593Smuzhiyun * The ext4 journal (jbd2) can submit a buffer_head it directly created
298*4882a593Smuzhiyun * for a non-pagecache page. fscrypt doesn't care about these.
299*4882a593Smuzhiyun */
300*4882a593Smuzhiyun mapping = page_mapping(page);
301*4882a593Smuzhiyun if (!mapping)
302*4882a593Smuzhiyun return false;
303*4882a593Smuzhiyun inode = mapping->host;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun *inode_ret = inode;
306*4882a593Smuzhiyun *lblk_num_ret = ((u64)page->index << (PAGE_SHIFT - inode->i_blkbits)) +
307*4882a593Smuzhiyun (bh_offset(bh) >> inode->i_blkbits);
308*4882a593Smuzhiyun return true;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
313*4882a593Smuzhiyun * crypto
314*4882a593Smuzhiyun * @bio: a bio which will eventually be submitted to the file
315*4882a593Smuzhiyun * @first_bh: the first buffer_head for which I/O will be submitted
316*4882a593Smuzhiyun * @gfp_mask: memory allocation flags
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
319*4882a593Smuzhiyun * of an inode and block number directly.
320*4882a593Smuzhiyun */
fscrypt_set_bio_crypt_ctx_bh(struct bio * bio,const struct buffer_head * first_bh,gfp_t gfp_mask)321*4882a593Smuzhiyun void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
322*4882a593Smuzhiyun const struct buffer_head *first_bh,
323*4882a593Smuzhiyun gfp_t gfp_mask)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun const struct inode *inode;
326*4882a593Smuzhiyun u64 first_lblk;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
329*4882a593Smuzhiyun fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /**
334*4882a593Smuzhiyun * fscrypt_mergeable_bio() - test whether data can be added to a bio
335*4882a593Smuzhiyun * @bio: the bio being built up
336*4882a593Smuzhiyun * @inode: the inode for the next part of the I/O
337*4882a593Smuzhiyun * @next_lblk: the next file logical block number in the I/O
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * When building a bio which may contain data which should undergo inline
340*4882a593Smuzhiyun * encryption (or decryption) via fscrypt, filesystems should call this function
341*4882a593Smuzhiyun * to ensure that the resulting bio contains only contiguous data unit numbers.
342*4882a593Smuzhiyun * This will return false if the next part of the I/O cannot be merged with the
343*4882a593Smuzhiyun * bio because either the encryption key would be different or the encryption
344*4882a593Smuzhiyun * data unit numbers would be discontiguous.
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * fscrypt_set_bio_crypt_ctx() must have already been called on the bio.
347*4882a593Smuzhiyun *
348*4882a593Smuzhiyun * This function also returns false if the next part of the I/O would need to
349*4882a593Smuzhiyun * have a different value for the bi_skip_dm_default_key flag.
350*4882a593Smuzhiyun *
351*4882a593Smuzhiyun * Return: true iff the I/O is mergeable
352*4882a593Smuzhiyun */
fscrypt_mergeable_bio(struct bio * bio,const struct inode * inode,u64 next_lblk)353*4882a593Smuzhiyun bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
354*4882a593Smuzhiyun u64 next_lblk)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
357*4882a593Smuzhiyun u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun if (!!bc != fscrypt_inode_uses_inline_crypto(inode))
360*4882a593Smuzhiyun return false;
361*4882a593Smuzhiyun if (bio_should_skip_dm_default_key(bio) !=
362*4882a593Smuzhiyun fscrypt_inode_should_skip_dm_default_key(inode))
363*4882a593Smuzhiyun return false;
364*4882a593Smuzhiyun if (!bc)
365*4882a593Smuzhiyun return true;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * Comparing the key pointers is good enough, as all I/O for each key
369*4882a593Smuzhiyun * uses the same pointer. I.e., there's currently no need to support
370*4882a593Smuzhiyun * merging requests where the keys are the same but the pointers differ.
371*4882a593Smuzhiyun */
372*4882a593Smuzhiyun if (bc->bc_key != &inode->i_crypt_info->ci_enc_key.blk_key->base)
373*4882a593Smuzhiyun return false;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
376*4882a593Smuzhiyun return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /**
381*4882a593Smuzhiyun * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
382*4882a593Smuzhiyun * @bio: the bio being built up
383*4882a593Smuzhiyun * @next_bh: the next buffer_head for which I/O will be submitted
384*4882a593Smuzhiyun *
385*4882a593Smuzhiyun * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
386*4882a593Smuzhiyun * an inode and block number directly.
387*4882a593Smuzhiyun *
388*4882a593Smuzhiyun * Return: true iff the I/O is mergeable
389*4882a593Smuzhiyun */
fscrypt_mergeable_bio_bh(struct bio * bio,const struct buffer_head * next_bh)390*4882a593Smuzhiyun bool fscrypt_mergeable_bio_bh(struct bio *bio,
391*4882a593Smuzhiyun const struct buffer_head *next_bh)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun const struct inode *inode;
394*4882a593Smuzhiyun u64 next_lblk;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
397*4882a593Smuzhiyun return !bio->bi_crypt_context &&
398*4882a593Smuzhiyun !bio_should_skip_dm_default_key(bio);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun return fscrypt_mergeable_bio(bio, inode, next_lblk);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /**
405*4882a593Smuzhiyun * fscrypt_dio_supported() - check whether a direct I/O request is unsupported
406*4882a593Smuzhiyun * due to encryption constraints
407*4882a593Smuzhiyun * @iocb: the file and position the I/O is targeting
408*4882a593Smuzhiyun * @iter: the I/O data segment(s)
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * Return: true if direct I/O is supported
411*4882a593Smuzhiyun */
fscrypt_dio_supported(struct kiocb * iocb,struct iov_iter * iter)412*4882a593Smuzhiyun bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun const struct inode *inode = file_inode(iocb->ki_filp);
415*4882a593Smuzhiyun const unsigned int blocksize = i_blocksize(inode);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /* If the file is unencrypted, no veto from us. */
418*4882a593Smuzhiyun if (!fscrypt_needs_contents_encryption(inode))
419*4882a593Smuzhiyun return true;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* We only support direct I/O with inline crypto, not fs-layer crypto */
422*4882a593Smuzhiyun if (!fscrypt_inode_uses_inline_crypto(inode))
423*4882a593Smuzhiyun return false;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun /*
426*4882a593Smuzhiyun * Since the granularity of encryption is filesystem blocks, the I/O
427*4882a593Smuzhiyun * must be block aligned -- not just disk sector aligned.
428*4882a593Smuzhiyun */
429*4882a593Smuzhiyun if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), blocksize))
430*4882a593Smuzhiyun return false;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun return true;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /**
437*4882a593Smuzhiyun * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs
438*4882a593Smuzhiyun * @inode: the file on which I/O is being done
439*4882a593Smuzhiyun * @lblk: the block at which the I/O is being started from
440*4882a593Smuzhiyun * @nr_blocks: the number of blocks we want to submit starting at @pos
441*4882a593Smuzhiyun *
442*4882a593Smuzhiyun * Determine the limit to the number of blocks that can be submitted in the bio
443*4882a593Smuzhiyun * targeting @pos without causing a data unit number (DUN) discontinuity.
444*4882a593Smuzhiyun *
445*4882a593Smuzhiyun * This is normally just @nr_blocks, as normally the DUNs just increment along
446*4882a593Smuzhiyun * with the logical blocks. (Or the file is not encrypted.)
447*4882a593Smuzhiyun *
448*4882a593Smuzhiyun * In rare cases, fscrypt can be using an IV generation method that allows the
449*4882a593Smuzhiyun * DUN to wrap around within logically continuous blocks, and that wraparound
450*4882a593Smuzhiyun * will occur. If this happens, a value less than @nr_blocks will be returned
451*4882a593Smuzhiyun * so that the wraparound doesn't occur in the middle of the bio.
452*4882a593Smuzhiyun *
453*4882a593Smuzhiyun * Return: the actual number of blocks that can be submitted
454*4882a593Smuzhiyun */
fscrypt_limit_io_blocks(const struct inode * inode,u64 lblk,u64 nr_blocks)455*4882a593Smuzhiyun u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun const struct fscrypt_info *ci = inode->i_crypt_info;
458*4882a593Smuzhiyun u32 dun;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (!fscrypt_inode_uses_inline_crypto(inode))
461*4882a593Smuzhiyun return nr_blocks;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (nr_blocks <= 1)
464*4882a593Smuzhiyun return nr_blocks;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun if (!(fscrypt_policy_flags(&ci->ci_policy) &
467*4882a593Smuzhiyun FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
468*4882a593Smuzhiyun return nr_blocks;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun dun = ci->ci_hashed_ino + lblk;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);
477