block: Rename bio_split() -> bio_pair_split()
[linux-2.6-block.git] / drivers / md / dm-crypt.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
542da317 4 * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
ed04d981 5 * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com>
1da177e4
LT
6 *
7 * This file is released under the GPL.
8 */
9
43d69034 10#include <linux/completion.h>
d1806f6a 11#include <linux/err.h>
1da177e4
LT
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/bio.h>
16#include <linux/blkdev.h>
17#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/crypto.h>
20#include <linux/workqueue.h>
3fcfab16 21#include <linux/backing-dev.h>
c0297721 22#include <linux/percpu.h>
60063497 23#include <linux/atomic.h>
378f058c 24#include <linux/scatterlist.h>
1da177e4 25#include <asm/page.h>
48527fa7 26#include <asm/unaligned.h>
34745785
MB
27#include <crypto/hash.h>
28#include <crypto/md5.h>
29#include <crypto/algapi.h>
1da177e4 30
586e80e6 31#include <linux/device-mapper.h>
1da177e4 32
72d94861 33#define DM_MSG_PREFIX "crypt"
1da177e4 34
1da177e4
LT
35/*
36 * context holding the current state of a multi-part conversion
37 */
38struct convert_context {
43d69034 39 struct completion restart;
1da177e4
LT
40 struct bio *bio_in;
41 struct bio *bio_out;
003b5c57
KO
42 struct bvec_iter iter_in;
43 struct bvec_iter iter_out;
c66029f4 44 sector_t cc_sector;
40b6229b 45 atomic_t cc_pending;
1da177e4
LT
46};
47
53017030
MB
48/*
49 * per bio private data
50 */
51struct dm_crypt_io {
49a8a920 52 struct crypt_config *cc;
53017030
MB
53 struct bio *base_bio;
54 struct work_struct work;
55
56 struct convert_context ctx;
57
40b6229b 58 atomic_t io_pending;
53017030 59 int error;
0c395b0f 60 sector_t sector;
393b47ef 61 struct dm_crypt_io *base_io;
53017030
MB
62};
63
01482b76 64struct dm_crypt_request {
b2174eeb 65 struct convert_context *ctx;
01482b76
MB
66 struct scatterlist sg_in;
67 struct scatterlist sg_out;
2dc5327d 68 sector_t iv_sector;
01482b76
MB
69};
70
1da177e4
LT
71struct crypt_config;
72
73struct crypt_iv_operations {
74 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
d469f841 75 const char *opts);
1da177e4 76 void (*dtr)(struct crypt_config *cc);
b95bf2d3 77 int (*init)(struct crypt_config *cc);
542da317 78 int (*wipe)(struct crypt_config *cc);
2dc5327d
MB
79 int (*generator)(struct crypt_config *cc, u8 *iv,
80 struct dm_crypt_request *dmreq);
81 int (*post)(struct crypt_config *cc, u8 *iv,
82 struct dm_crypt_request *dmreq);
1da177e4
LT
83};
84
60473592 85struct iv_essiv_private {
b95bf2d3
MB
86 struct crypto_hash *hash_tfm;
87 u8 *salt;
60473592
MB
88};
89
90struct iv_benbi_private {
91 int shift;
92};
93
34745785
MB
94#define LMK_SEED_SIZE 64 /* hash + 0 */
95struct iv_lmk_private {
96 struct crypto_shash *hash_tfm;
97 u8 *seed;
98};
99
ed04d981
MB
100#define TCW_WHITENING_SIZE 16
101struct iv_tcw_private {
102 struct crypto_shash *crc32_tfm;
103 u8 *iv_seed;
104 u8 *whitening;
105};
106
1da177e4
LT
107/*
108 * Crypt: maps a linear range of a block device
109 * and encrypts / decrypts at the same time.
110 */
e48d4bbf 111enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
c0297721
AK
112
113/*
114 * Duplicated per-CPU state for cipher.
115 */
116struct crypt_cpu {
117 struct ablkcipher_request *req;
c0297721
AK
118};
119
120/*
121 * The fields in here must be read only after initialization,
122 * changing state should be in crypt_cpu.
123 */
1da177e4
LT
124struct crypt_config {
125 struct dm_dev *dev;
126 sector_t start;
127
128 /*
ddd42edf
MB
129 * pool for per bio private data, crypto requests and
130 * encryption requeusts/buffer pages
1da177e4
LT
131 */
132 mempool_t *io_pool;
ddd42edf 133 mempool_t *req_pool;
1da177e4 134 mempool_t *page_pool;
6a24c718 135 struct bio_set *bs;
1da177e4 136
cabf08e4
MB
137 struct workqueue_struct *io_queue;
138 struct workqueue_struct *crypt_queue;
3f1e9070 139
5ebaee6d 140 char *cipher;
7dbcd137 141 char *cipher_string;
5ebaee6d 142
1da177e4 143 struct crypt_iv_operations *iv_gen_ops;
79066ad3 144 union {
60473592
MB
145 struct iv_essiv_private essiv;
146 struct iv_benbi_private benbi;
34745785 147 struct iv_lmk_private lmk;
ed04d981 148 struct iv_tcw_private tcw;
79066ad3 149 } iv_gen_private;
1da177e4
LT
150 sector_t iv_offset;
151 unsigned int iv_size;
152
c0297721
AK
153 /*
154 * Duplicated per cpu state. Access through
155 * per_cpu_ptr() only.
156 */
157 struct crypt_cpu __percpu *cpu;
fd2d231f
MP
158
159 /* ESSIV: struct crypto_cipher *essiv_tfm */
160 void *iv_private;
161 struct crypto_ablkcipher **tfms;
d1f96423 162 unsigned tfms_count;
c0297721 163
ddd42edf
MB
164 /*
165 * Layout of each crypto request:
166 *
167 * struct ablkcipher_request
168 * context
169 * padding
170 * struct dm_crypt_request
171 * padding
172 * IV
173 *
174 * The padding is added so that dm_crypt_request and the IV are
175 * correctly aligned.
176 */
177 unsigned int dmreq_start;
ddd42edf 178
e48d4bbf 179 unsigned long flags;
1da177e4 180 unsigned int key_size;
da31a078
MB
181 unsigned int key_parts; /* independent parts in key buffer */
182 unsigned int key_extra_size; /* additional keys length */
1da177e4
LT
183 u8 key[0];
184};
185
6a24c718 186#define MIN_IOS 16
1da177e4 187#define MIN_POOL_PAGES 32
1da177e4 188
e18b890b 189static struct kmem_cache *_crypt_io_pool;
1da177e4 190
028867ac 191static void clone_init(struct dm_crypt_io *, struct bio *);
395b167c 192static void kcryptd_queue_crypt(struct dm_crypt_io *io);
2dc5327d 193static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
027581f3 194
c0297721
AK
195static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
196{
197 return this_cpu_ptr(cc->cpu);
198}
199
200/*
201 * Use this to access cipher attributes that are the same for each CPU.
202 */
203static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
204{
fd2d231f 205 return cc->tfms[0];
c0297721
AK
206}
207
1da177e4
LT
208/*
209 * Different IV generation algorithms:
210 *
3c164bd8 211 * plain: the initial vector is the 32-bit little-endian version of the sector
3a4fa0a2 212 * number, padded with zeros if necessary.
1da177e4 213 *
61afef61
MB
214 * plain64: the initial vector is the 64-bit little-endian version of the sector
215 * number, padded with zeros if necessary.
216 *
3c164bd8
RS
217 * essiv: "encrypted sector|salt initial vector", the sector number is
218 * encrypted with the bulk cipher using a salt as key. The salt
219 * should be derived from the bulk cipher's key via hashing.
1da177e4 220 *
48527fa7
RS
221 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
222 * (needed for LRW-32-AES and possible other narrow block modes)
223 *
46b47730
LN
224 * null: the initial vector is always zero. Provides compatibility with
225 * obsolete loop_fish2 devices. Do not use for new devices.
226 *
34745785
MB
227 * lmk: Compatible implementation of the block chaining mode used
228 * by the Loop-AES block device encryption system
229 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
230 * It operates on full 512 byte sectors and uses CBC
231 * with an IV derived from the sector number, the data and
232 * optionally extra IV seed.
233 * This means that after decryption the first block
234 * of sector must be tweaked according to decrypted data.
235 * Loop-AES can use three encryption schemes:
236 * version 1: is plain aes-cbc mode
237 * version 2: uses 64 multikey scheme with lmk IV generator
238 * version 3: the same as version 2 with additional IV seed
239 * (it uses 65 keys, last key is used as IV seed)
240 *
ed04d981
MB
241 * tcw: Compatible implementation of the block chaining mode used
242 * by the TrueCrypt device encryption system (prior to version 4.1).
243 * For more info see: http://www.truecrypt.org
244 * It operates on full 512 byte sectors and uses CBC
245 * with an IV derived from initial key and the sector number.
246 * In addition, whitening value is applied on every sector, whitening
247 * is calculated from initial key, sector number and mixed using CRC32.
248 * Note that this encryption scheme is vulnerable to watermarking attacks
249 * and should be used for old compatible containers access only.
250 *
1da177e4
LT
251 * plumb: unimplemented, see:
252 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
253 */
254
2dc5327d
MB
255static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
256 struct dm_crypt_request *dmreq)
1da177e4
LT
257{
258 memset(iv, 0, cc->iv_size);
283a8328 259 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
1da177e4
LT
260
261 return 0;
262}
263
61afef61 264static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
2dc5327d 265 struct dm_crypt_request *dmreq)
61afef61
MB
266{
267 memset(iv, 0, cc->iv_size);
283a8328 268 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
61afef61
MB
269
270 return 0;
271}
272
b95bf2d3
MB
273/* Initialise ESSIV - compute salt but no local memory allocations */
274static int crypt_iv_essiv_init(struct crypt_config *cc)
275{
276 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
277 struct hash_desc desc;
278 struct scatterlist sg;
c0297721 279 struct crypto_cipher *essiv_tfm;
fd2d231f 280 int err;
b95bf2d3
MB
281
282 sg_init_one(&sg, cc->key, cc->key_size);
283 desc.tfm = essiv->hash_tfm;
284 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
285
286 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
287 if (err)
288 return err;
289
fd2d231f 290 essiv_tfm = cc->iv_private;
c0297721 291
fd2d231f
MP
292 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
293 crypto_hash_digestsize(essiv->hash_tfm));
294 if (err)
295 return err;
c0297721
AK
296
297 return 0;
b95bf2d3
MB
298}
299
542da317
MB
300/* Wipe salt and reset key derived from volume key */
301static int crypt_iv_essiv_wipe(struct crypt_config *cc)
302{
303 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
304 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
c0297721 305 struct crypto_cipher *essiv_tfm;
fd2d231f 306 int r, err = 0;
542da317
MB
307
308 memset(essiv->salt, 0, salt_size);
309
fd2d231f
MP
310 essiv_tfm = cc->iv_private;
311 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
312 if (r)
313 err = r;
c0297721
AK
314
315 return err;
316}
317
318/* Set up per cpu cipher state */
319static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
320 struct dm_target *ti,
321 u8 *salt, unsigned saltsize)
322{
323 struct crypto_cipher *essiv_tfm;
324 int err;
325
326 /* Setup the essiv_tfm with the given salt */
327 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
328 if (IS_ERR(essiv_tfm)) {
329 ti->error = "Error allocating crypto tfm for ESSIV";
330 return essiv_tfm;
331 }
332
333 if (crypto_cipher_blocksize(essiv_tfm) !=
334 crypto_ablkcipher_ivsize(any_tfm(cc))) {
335 ti->error = "Block size of ESSIV cipher does "
336 "not match IV size of block cipher";
337 crypto_free_cipher(essiv_tfm);
338 return ERR_PTR(-EINVAL);
339 }
340
341 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
342 if (err) {
343 ti->error = "Failed to set key for ESSIV cipher";
344 crypto_free_cipher(essiv_tfm);
345 return ERR_PTR(err);
346 }
347
348 return essiv_tfm;
542da317
MB
349}
350
60473592
MB
351static void crypt_iv_essiv_dtr(struct crypt_config *cc)
352{
c0297721 353 struct crypto_cipher *essiv_tfm;
60473592
MB
354 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
355
b95bf2d3
MB
356 crypto_free_hash(essiv->hash_tfm);
357 essiv->hash_tfm = NULL;
358
359 kzfree(essiv->salt);
360 essiv->salt = NULL;
c0297721 361
fd2d231f 362 essiv_tfm = cc->iv_private;
c0297721 363
fd2d231f
MP
364 if (essiv_tfm)
365 crypto_free_cipher(essiv_tfm);
c0297721 366
fd2d231f 367 cc->iv_private = NULL;
60473592
MB
368}
369
1da177e4 370static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
d469f841 371 const char *opts)
1da177e4 372{
5861f1be
MB
373 struct crypto_cipher *essiv_tfm = NULL;
374 struct crypto_hash *hash_tfm = NULL;
5861f1be 375 u8 *salt = NULL;
fd2d231f 376 int err;
1da177e4 377
5861f1be 378 if (!opts) {
72d94861 379 ti->error = "Digest algorithm missing for ESSIV mode";
1da177e4
LT
380 return -EINVAL;
381 }
382
b95bf2d3 383 /* Allocate hash algorithm */
35058687
HX
384 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
385 if (IS_ERR(hash_tfm)) {
72d94861 386 ti->error = "Error initializing ESSIV hash";
5861f1be
MB
387 err = PTR_ERR(hash_tfm);
388 goto bad;
1da177e4
LT
389 }
390
b95bf2d3 391 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
5861f1be 392 if (!salt) {
72d94861 393 ti->error = "Error kmallocing salt storage in ESSIV";
5861f1be
MB
394 err = -ENOMEM;
395 goto bad;
1da177e4
LT
396 }
397
b95bf2d3 398 cc->iv_gen_private.essiv.salt = salt;
b95bf2d3
MB
399 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
400
fd2d231f
MP
401 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
402 crypto_hash_digestsize(hash_tfm));
403 if (IS_ERR(essiv_tfm)) {
404 crypt_iv_essiv_dtr(cc);
405 return PTR_ERR(essiv_tfm);
c0297721 406 }
fd2d231f 407 cc->iv_private = essiv_tfm;
c0297721 408
1da177e4 409 return 0;
5861f1be
MB
410
411bad:
5861f1be
MB
412 if (hash_tfm && !IS_ERR(hash_tfm))
413 crypto_free_hash(hash_tfm);
b95bf2d3 414 kfree(salt);
5861f1be 415 return err;
1da177e4
LT
416}
417
2dc5327d
MB
418static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
419 struct dm_crypt_request *dmreq)
1da177e4 420{
fd2d231f 421 struct crypto_cipher *essiv_tfm = cc->iv_private;
c0297721 422
1da177e4 423 memset(iv, 0, cc->iv_size);
283a8328 424 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
c0297721
AK
425 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
426
1da177e4
LT
427 return 0;
428}
429
48527fa7
RS
430static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
431 const char *opts)
432{
c0297721 433 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
f0d1b0b3 434 int log = ilog2(bs);
48527fa7
RS
435
436 /* we need to calculate how far we must shift the sector count
437 * to get the cipher block count, we use this shift in _gen */
438
439 if (1 << log != bs) {
440 ti->error = "cypher blocksize is not a power of 2";
441 return -EINVAL;
442 }
443
444 if (log > 9) {
445 ti->error = "cypher blocksize is > 512";
446 return -EINVAL;
447 }
448
60473592 449 cc->iv_gen_private.benbi.shift = 9 - log;
48527fa7
RS
450
451 return 0;
452}
453
454static void crypt_iv_benbi_dtr(struct crypt_config *cc)
455{
48527fa7
RS
456}
457
2dc5327d
MB
458static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
459 struct dm_crypt_request *dmreq)
48527fa7 460{
79066ad3
HX
461 __be64 val;
462
48527fa7 463 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
79066ad3 464
2dc5327d 465 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
79066ad3 466 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
48527fa7 467
1da177e4
LT
468 return 0;
469}
470
2dc5327d
MB
471static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
472 struct dm_crypt_request *dmreq)
46b47730
LN
473{
474 memset(iv, 0, cc->iv_size);
475
476 return 0;
477}
478
34745785
MB
479static void crypt_iv_lmk_dtr(struct crypt_config *cc)
480{
481 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
482
483 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
484 crypto_free_shash(lmk->hash_tfm);
485 lmk->hash_tfm = NULL;
486
487 kzfree(lmk->seed);
488 lmk->seed = NULL;
489}
490
491static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
492 const char *opts)
493{
494 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
495
496 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
497 if (IS_ERR(lmk->hash_tfm)) {
498 ti->error = "Error initializing LMK hash";
499 return PTR_ERR(lmk->hash_tfm);
500 }
501
502 /* No seed in LMK version 2 */
503 if (cc->key_parts == cc->tfms_count) {
504 lmk->seed = NULL;
505 return 0;
506 }
507
508 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
509 if (!lmk->seed) {
510 crypt_iv_lmk_dtr(cc);
511 ti->error = "Error kmallocing seed storage in LMK";
512 return -ENOMEM;
513 }
514
515 return 0;
516}
517
518static int crypt_iv_lmk_init(struct crypt_config *cc)
519{
520 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
521 int subkey_size = cc->key_size / cc->key_parts;
522
523 /* LMK seed is on the position of LMK_KEYS + 1 key */
524 if (lmk->seed)
525 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
526 crypto_shash_digestsize(lmk->hash_tfm));
527
528 return 0;
529}
530
531static int crypt_iv_lmk_wipe(struct crypt_config *cc)
532{
533 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
534
535 if (lmk->seed)
536 memset(lmk->seed, 0, LMK_SEED_SIZE);
537
538 return 0;
539}
540
541static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
542 struct dm_crypt_request *dmreq,
543 u8 *data)
544{
545 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
546 struct {
547 struct shash_desc desc;
548 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
549 } sdesc;
550 struct md5_state md5state;
da31a078 551 __le32 buf[4];
34745785
MB
552 int i, r;
553
554 sdesc.desc.tfm = lmk->hash_tfm;
555 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
556
557 r = crypto_shash_init(&sdesc.desc);
558 if (r)
559 return r;
560
561 if (lmk->seed) {
562 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
563 if (r)
564 return r;
565 }
566
567 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
568 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
569 if (r)
570 return r;
571
572 /* Sector is cropped to 56 bits here */
573 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
574 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
575 buf[2] = cpu_to_le32(4024);
576 buf[3] = 0;
577 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
578 if (r)
579 return r;
580
581 /* No MD5 padding here */
582 r = crypto_shash_export(&sdesc.desc, &md5state);
583 if (r)
584 return r;
585
586 for (i = 0; i < MD5_HASH_WORDS; i++)
587 __cpu_to_le32s(&md5state.hash[i]);
588 memcpy(iv, &md5state.hash, cc->iv_size);
589
590 return 0;
591}
592
593static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
594 struct dm_crypt_request *dmreq)
595{
596 u8 *src;
597 int r = 0;
598
599 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
c2e022cb 600 src = kmap_atomic(sg_page(&dmreq->sg_in));
34745785 601 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
c2e022cb 602 kunmap_atomic(src);
34745785
MB
603 } else
604 memset(iv, 0, cc->iv_size);
605
606 return r;
607}
608
609static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
610 struct dm_crypt_request *dmreq)
611{
612 u8 *dst;
613 int r;
614
615 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
616 return 0;
617
c2e022cb 618 dst = kmap_atomic(sg_page(&dmreq->sg_out));
34745785
MB
619 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
620
621 /* Tweak the first block of plaintext sector */
622 if (!r)
623 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
624
c2e022cb 625 kunmap_atomic(dst);
34745785
MB
626 return r;
627}
628
ed04d981
MB
629static void crypt_iv_tcw_dtr(struct crypt_config *cc)
630{
631 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
632
633 kzfree(tcw->iv_seed);
634 tcw->iv_seed = NULL;
635 kzfree(tcw->whitening);
636 tcw->whitening = NULL;
637
638 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
639 crypto_free_shash(tcw->crc32_tfm);
640 tcw->crc32_tfm = NULL;
641}
642
643static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
644 const char *opts)
645{
646 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
647
648 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
649 ti->error = "Wrong key size for TCW";
650 return -EINVAL;
651 }
652
653 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
654 if (IS_ERR(tcw->crc32_tfm)) {
655 ti->error = "Error initializing CRC32 in TCW";
656 return PTR_ERR(tcw->crc32_tfm);
657 }
658
659 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
660 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
661 if (!tcw->iv_seed || !tcw->whitening) {
662 crypt_iv_tcw_dtr(cc);
663 ti->error = "Error allocating seed storage in TCW";
664 return -ENOMEM;
665 }
666
667 return 0;
668}
669
670static int crypt_iv_tcw_init(struct crypt_config *cc)
671{
672 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
673 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
674
675 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
676 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
677 TCW_WHITENING_SIZE);
678
679 return 0;
680}
681
682static int crypt_iv_tcw_wipe(struct crypt_config *cc)
683{
684 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
685
686 memset(tcw->iv_seed, 0, cc->iv_size);
687 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
688
689 return 0;
690}
691
692static int crypt_iv_tcw_whitening(struct crypt_config *cc,
693 struct dm_crypt_request *dmreq,
694 u8 *data)
695{
696 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
697 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
698 u8 buf[TCW_WHITENING_SIZE];
699 struct {
700 struct shash_desc desc;
701 char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
702 } sdesc;
703 int i, r;
704
705 /* xor whitening with sector number */
706 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
707 crypto_xor(buf, (u8 *)&sector, 8);
708 crypto_xor(&buf[8], (u8 *)&sector, 8);
709
710 /* calculate crc32 for every 32bit part and xor it */
711 sdesc.desc.tfm = tcw->crc32_tfm;
712 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
713 for (i = 0; i < 4; i++) {
714 r = crypto_shash_init(&sdesc.desc);
715 if (r)
716 goto out;
717 r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
718 if (r)
719 goto out;
720 r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
721 if (r)
722 goto out;
723 }
724 crypto_xor(&buf[0], &buf[12], 4);
725 crypto_xor(&buf[4], &buf[8], 4);
726
727 /* apply whitening (8 bytes) to whole sector */
728 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
729 crypto_xor(data + i * 8, buf, 8);
730out:
731 memset(buf, 0, sizeof(buf));
732 return r;
733}
734
735static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
736 struct dm_crypt_request *dmreq)
737{
738 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
739 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
740 u8 *src;
741 int r = 0;
742
743 /* Remove whitening from ciphertext */
744 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
745 src = kmap_atomic(sg_page(&dmreq->sg_in));
746 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
747 kunmap_atomic(src);
748 }
749
750 /* Calculate IV */
751 memcpy(iv, tcw->iv_seed, cc->iv_size);
752 crypto_xor(iv, (u8 *)&sector, 8);
753 if (cc->iv_size > 8)
754 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
755
756 return r;
757}
758
759static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
760 struct dm_crypt_request *dmreq)
761{
762 u8 *dst;
763 int r;
764
765 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
766 return 0;
767
768 /* Apply whitening on ciphertext */
769 dst = kmap_atomic(sg_page(&dmreq->sg_out));
770 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
771 kunmap_atomic(dst);
772
773 return r;
774}
775
1da177e4
LT
776static struct crypt_iv_operations crypt_iv_plain_ops = {
777 .generator = crypt_iv_plain_gen
778};
779
61afef61
MB
780static struct crypt_iv_operations crypt_iv_plain64_ops = {
781 .generator = crypt_iv_plain64_gen
782};
783
1da177e4
LT
784static struct crypt_iv_operations crypt_iv_essiv_ops = {
785 .ctr = crypt_iv_essiv_ctr,
786 .dtr = crypt_iv_essiv_dtr,
b95bf2d3 787 .init = crypt_iv_essiv_init,
542da317 788 .wipe = crypt_iv_essiv_wipe,
1da177e4
LT
789 .generator = crypt_iv_essiv_gen
790};
791
48527fa7
RS
792static struct crypt_iv_operations crypt_iv_benbi_ops = {
793 .ctr = crypt_iv_benbi_ctr,
794 .dtr = crypt_iv_benbi_dtr,
795 .generator = crypt_iv_benbi_gen
796};
1da177e4 797
46b47730
LN
798static struct crypt_iv_operations crypt_iv_null_ops = {
799 .generator = crypt_iv_null_gen
800};
801
34745785
MB
802static struct crypt_iv_operations crypt_iv_lmk_ops = {
803 .ctr = crypt_iv_lmk_ctr,
804 .dtr = crypt_iv_lmk_dtr,
805 .init = crypt_iv_lmk_init,
806 .wipe = crypt_iv_lmk_wipe,
807 .generator = crypt_iv_lmk_gen,
808 .post = crypt_iv_lmk_post
809};
810
ed04d981
MB
811static struct crypt_iv_operations crypt_iv_tcw_ops = {
812 .ctr = crypt_iv_tcw_ctr,
813 .dtr = crypt_iv_tcw_dtr,
814 .init = crypt_iv_tcw_init,
815 .wipe = crypt_iv_tcw_wipe,
816 .generator = crypt_iv_tcw_gen,
817 .post = crypt_iv_tcw_post
818};
819
d469f841
MB
820static void crypt_convert_init(struct crypt_config *cc,
821 struct convert_context *ctx,
822 struct bio *bio_out, struct bio *bio_in,
fcd369da 823 sector_t sector)
1da177e4
LT
824{
825 ctx->bio_in = bio_in;
826 ctx->bio_out = bio_out;
003b5c57
KO
827 if (bio_in)
828 ctx->iter_in = bio_in->bi_iter;
829 if (bio_out)
830 ctx->iter_out = bio_out->bi_iter;
c66029f4 831 ctx->cc_sector = sector + cc->iv_offset;
43d69034 832 init_completion(&ctx->restart);
1da177e4
LT
833}
834
b2174eeb
HY
835static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
836 struct ablkcipher_request *req)
837{
838 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
839}
840
841static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
842 struct dm_crypt_request *dmreq)
843{
844 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
845}
846
2dc5327d
MB
847static u8 *iv_of_dmreq(struct crypt_config *cc,
848 struct dm_crypt_request *dmreq)
849{
850 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
851 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
852}
853
01482b76 854static int crypt_convert_block(struct crypt_config *cc,
3a7f6c99
MB
855 struct convert_context *ctx,
856 struct ablkcipher_request *req)
01482b76 857{
003b5c57
KO
858 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
859 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
3a7f6c99
MB
860 struct dm_crypt_request *dmreq;
861 u8 *iv;
40b6229b 862 int r;
3a7f6c99 863
b2174eeb 864 dmreq = dmreq_of_req(cc, req);
2dc5327d 865 iv = iv_of_dmreq(cc, dmreq);
01482b76 866
c66029f4 867 dmreq->iv_sector = ctx->cc_sector;
b2174eeb 868 dmreq->ctx = ctx;
3a7f6c99 869 sg_init_table(&dmreq->sg_in, 1);
003b5c57
KO
870 sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
871 bv_in.bv_offset);
01482b76 872
3a7f6c99 873 sg_init_table(&dmreq->sg_out, 1);
003b5c57
KO
874 sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
875 bv_out.bv_offset);
01482b76 876
003b5c57
KO
877 bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
878 bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
01482b76 879
3a7f6c99 880 if (cc->iv_gen_ops) {
2dc5327d 881 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
3a7f6c99
MB
882 if (r < 0)
883 return r;
884 }
885
886 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
887 1 << SECTOR_SHIFT, iv);
888
889 if (bio_data_dir(ctx->bio_in) == WRITE)
890 r = crypto_ablkcipher_encrypt(req);
891 else
892 r = crypto_ablkcipher_decrypt(req);
893
2dc5327d
MB
894 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
895 r = cc->iv_gen_ops->post(cc, iv, dmreq);
896
3a7f6c99 897 return r;
01482b76
MB
898}
899
95497a96
MB
900static void kcryptd_async_done(struct crypto_async_request *async_req,
901 int error);
c0297721 902
ddd42edf
MB
903static void crypt_alloc_req(struct crypt_config *cc,
904 struct convert_context *ctx)
905{
c0297721 906 struct crypt_cpu *this_cc = this_crypt_config(cc);
c66029f4 907 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
c0297721
AK
908
909 if (!this_cc->req)
910 this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
911
fd2d231f 912 ablkcipher_request_set_tfm(this_cc->req, cc->tfms[key_index]);
c0297721
AK
913 ablkcipher_request_set_callback(this_cc->req,
914 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
915 kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
ddd42edf
MB
916}
917
1da177e4
LT
918/*
919 * Encrypt / decrypt data from one bio to another one (can be the same one)
920 */
921static int crypt_convert(struct crypt_config *cc,
d469f841 922 struct convert_context *ctx)
1da177e4 923{
c0297721 924 struct crypt_cpu *this_cc = this_crypt_config(cc);
3f1e9070 925 int r;
1da177e4 926
40b6229b 927 atomic_set(&ctx->cc_pending, 1);
c8081618 928
003b5c57 929 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
1da177e4 930
3a7f6c99
MB
931 crypt_alloc_req(cc, ctx);
932
40b6229b 933 atomic_inc(&ctx->cc_pending);
3f1e9070 934
c0297721 935 r = crypt_convert_block(cc, ctx, this_cc->req);
3a7f6c99
MB
936
937 switch (r) {
3f1e9070 938 /* async */
3a7f6c99
MB
939 case -EBUSY:
940 wait_for_completion(&ctx->restart);
16735d02 941 reinit_completion(&ctx->restart);
3a7f6c99
MB
942 /* fall through*/
943 case -EINPROGRESS:
c0297721 944 this_cc->req = NULL;
c66029f4 945 ctx->cc_sector++;
3f1e9070
MB
946 continue;
947
948 /* sync */
3a7f6c99 949 case 0:
40b6229b 950 atomic_dec(&ctx->cc_pending);
c66029f4 951 ctx->cc_sector++;
c7f1b204 952 cond_resched();
3a7f6c99 953 continue;
3a7f6c99 954
3f1e9070
MB
955 /* error */
956 default:
40b6229b 957 atomic_dec(&ctx->cc_pending);
3f1e9070
MB
958 return r;
959 }
1da177e4
LT
960 }
961
3f1e9070 962 return 0;
1da177e4
LT
963}
964
965/*
966 * Generate a new unfragmented bio with the given size
967 * This should never violate the device limitations
933f01d4
MB
968 * May return a smaller bio when running out of pages, indicated by
969 * *out_of_pages set to 1.
1da177e4 970 */
933f01d4
MB
971static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
972 unsigned *out_of_pages)
1da177e4 973{
49a8a920 974 struct crypt_config *cc = io->cc;
8b004457 975 struct bio *clone;
1da177e4 976 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
b4e3ca1a 977 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
91e10625
MB
978 unsigned i, len;
979 struct page *page;
1da177e4 980
2f9941b6 981 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
8b004457 982 if (!clone)
1da177e4 983 return NULL;
1da177e4 984
027581f3 985 clone_init(io, clone);
933f01d4 986 *out_of_pages = 0;
6a24c718 987
f97380bc 988 for (i = 0; i < nr_iovecs; i++) {
91e10625 989 page = mempool_alloc(cc->page_pool, gfp_mask);
933f01d4
MB
990 if (!page) {
991 *out_of_pages = 1;
1da177e4 992 break;
933f01d4 993 }
1da177e4
LT
994
995 /*
aeb2deae
MP
996 * If additional pages cannot be allocated without waiting,
997 * return a partially-allocated bio. The caller will then try
998 * to allocate more bios while submitting this partial bio.
1da177e4 999 */
aeb2deae 1000 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
1da177e4 1001
91e10625
MB
1002 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
1003
1004 if (!bio_add_page(clone, page, len, 0)) {
1005 mempool_free(page, cc->page_pool);
1006 break;
1007 }
1da177e4 1008
91e10625 1009 size -= len;
1da177e4
LT
1010 }
1011
4f024f37 1012 if (!clone->bi_iter.bi_size) {
8b004457 1013 bio_put(clone);
1da177e4
LT
1014 return NULL;
1015 }
1016
8b004457 1017 return clone;
1da177e4
LT
1018}
1019
644bd2f0 1020static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
1da177e4 1021{
644bd2f0 1022 unsigned int i;
1da177e4
LT
1023 struct bio_vec *bv;
1024
cb34e057 1025 bio_for_each_segment_all(bv, clone, i) {
1da177e4
LT
1026 BUG_ON(!bv->bv_page);
1027 mempool_free(bv->bv_page, cc->page_pool);
1028 bv->bv_page = NULL;
1029 }
1030}
1031
49a8a920 1032static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc,
dc440d1e
MB
1033 struct bio *bio, sector_t sector)
1034{
dc440d1e
MB
1035 struct dm_crypt_io *io;
1036
1037 io = mempool_alloc(cc->io_pool, GFP_NOIO);
49a8a920 1038 io->cc = cc;
dc440d1e
MB
1039 io->base_bio = bio;
1040 io->sector = sector;
1041 io->error = 0;
393b47ef 1042 io->base_io = NULL;
40b6229b 1043 atomic_set(&io->io_pending, 0);
dc440d1e
MB
1044
1045 return io;
1046}
1047
3e1a8bdd
MB
1048static void crypt_inc_pending(struct dm_crypt_io *io)
1049{
40b6229b 1050 atomic_inc(&io->io_pending);
3e1a8bdd
MB
1051}
1052
1da177e4
LT
1053/*
1054 * One of the bios was finished. Check for completion of
1055 * the whole request and correctly clean up the buffer.
393b47ef 1056 * If base_io is set, wait for the last fragment to complete.
1da177e4 1057 */
5742fd77 1058static void crypt_dec_pending(struct dm_crypt_io *io)
1da177e4 1059{
49a8a920 1060 struct crypt_config *cc = io->cc;
b35f8caa
MB
1061 struct bio *base_bio = io->base_bio;
1062 struct dm_crypt_io *base_io = io->base_io;
1063 int error = io->error;
1da177e4 1064
40b6229b 1065 if (!atomic_dec_and_test(&io->io_pending))
1da177e4
LT
1066 return;
1067
b35f8caa
MB
1068 mempool_free(io, cc->io_pool);
1069
1070 if (likely(!base_io))
1071 bio_endio(base_bio, error);
393b47ef 1072 else {
b35f8caa
MB
1073 if (error && !base_io->error)
1074 base_io->error = error;
1075 crypt_dec_pending(base_io);
393b47ef 1076 }
1da177e4
LT
1077}
1078
1079/*
cabf08e4 1080 * kcryptd/kcryptd_io:
1da177e4
LT
1081 *
1082 * Needed because it would be very unwise to do decryption in an
23541d2d 1083 * interrupt context.
cabf08e4
MB
1084 *
1085 * kcryptd performs the actual encryption or decryption.
1086 *
1087 * kcryptd_io performs the IO submission.
1088 *
1089 * They must be separated as otherwise the final stages could be
1090 * starved by new requests which can block in the first stages due
1091 * to memory allocation.
c0297721
AK
1092 *
1093 * The work is done per CPU global for all dm-crypt instances.
1094 * They should not depend on each other and do not block.
1da177e4 1095 */
6712ecf8 1096static void crypt_endio(struct bio *clone, int error)
8b004457 1097{
028867ac 1098 struct dm_crypt_io *io = clone->bi_private;
49a8a920 1099 struct crypt_config *cc = io->cc;
ee7a491e 1100 unsigned rw = bio_data_dir(clone);
8b004457 1101
adfe4770
MB
1102 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1103 error = -EIO;
1104
8b004457 1105 /*
6712ecf8 1106 * free the processed pages
8b004457 1107 */
ee7a491e 1108 if (rw == WRITE)
644bd2f0 1109 crypt_free_buffer_pages(cc, clone);
8b004457
MB
1110
1111 bio_put(clone);
8b004457 1112
ee7a491e
MB
1113 if (rw == READ && !error) {
1114 kcryptd_queue_crypt(io);
1115 return;
1116 }
5742fd77
MB
1117
1118 if (unlikely(error))
1119 io->error = error;
1120
1121 crypt_dec_pending(io);
8b004457
MB
1122}
1123
028867ac 1124static void clone_init(struct dm_crypt_io *io, struct bio *clone)
8b004457 1125{
49a8a920 1126 struct crypt_config *cc = io->cc;
8b004457
MB
1127
1128 clone->bi_private = io;
1129 clone->bi_end_io = crypt_endio;
1130 clone->bi_bdev = cc->dev->bdev;
1131 clone->bi_rw = io->base_bio->bi_rw;
1132}
1133
20c82538 1134static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
8b004457 1135{
49a8a920 1136 struct crypt_config *cc = io->cc;
8b004457
MB
1137 struct bio *base_bio = io->base_bio;
1138 struct bio *clone;
93e605c2 1139
8b004457
MB
1140 /*
1141 * The block layer might modify the bvec array, so always
1142 * copy the required bvecs because we need the original
1143 * one in order to decrypt the whole bio data *afterwards*.
1144 */
bf800ef1 1145 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
7eaceacc 1146 if (!clone)
20c82538 1147 return 1;
8b004457 1148
20c82538
MB
1149 crypt_inc_pending(io);
1150
8b004457 1151 clone_init(io, clone);
4f024f37 1152 clone->bi_iter.bi_sector = cc->start + io->sector;
8b004457 1153
93e605c2 1154 generic_make_request(clone);
20c82538 1155 return 0;
8b004457
MB
1156}
1157
4e4eef64
MB
1158static void kcryptd_io_write(struct dm_crypt_io *io)
1159{
95497a96 1160 struct bio *clone = io->ctx.bio_out;
95497a96 1161 generic_make_request(clone);
4e4eef64
MB
1162}
1163
395b167c
AK
1164static void kcryptd_io(struct work_struct *work)
1165{
1166 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1167
20c82538
MB
1168 if (bio_data_dir(io->base_bio) == READ) {
1169 crypt_inc_pending(io);
1170 if (kcryptd_io_read(io, GFP_NOIO))
1171 io->error = -ENOMEM;
1172 crypt_dec_pending(io);
1173 } else
395b167c
AK
1174 kcryptd_io_write(io);
1175}
1176
1177static void kcryptd_queue_io(struct dm_crypt_io *io)
1178{
49a8a920 1179 struct crypt_config *cc = io->cc;
395b167c
AK
1180
1181 INIT_WORK(&io->work, kcryptd_io);
1182 queue_work(cc->io_queue, &io->work);
1183}
1184
72c6e7af 1185static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
4e4eef64 1186{
dec1cedf 1187 struct bio *clone = io->ctx.bio_out;
49a8a920 1188 struct crypt_config *cc = io->cc;
dec1cedf 1189
72c6e7af 1190 if (unlikely(io->error < 0)) {
dec1cedf
MB
1191 crypt_free_buffer_pages(cc, clone);
1192 bio_put(clone);
6c031f41 1193 crypt_dec_pending(io);
dec1cedf
MB
1194 return;
1195 }
1196
1197 /* crypt_convert should have filled the clone bio */
003b5c57 1198 BUG_ON(io->ctx.iter_out.bi_size);
dec1cedf 1199
4f024f37 1200 clone->bi_iter.bi_sector = cc->start + io->sector;
899c95d3 1201
95497a96
MB
1202 if (async)
1203 kcryptd_queue_io(io);
1e37bb8e 1204 else
95497a96 1205 generic_make_request(clone);
4e4eef64
MB
1206}
1207
fc5a5e9a 1208static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
8b004457 1209{
49a8a920 1210 struct crypt_config *cc = io->cc;
8b004457 1211 struct bio *clone;
393b47ef 1212 struct dm_crypt_io *new_io;
c8081618 1213 int crypt_finished;
933f01d4 1214 unsigned out_of_pages = 0;
4f024f37 1215 unsigned remaining = io->base_bio->bi_iter.bi_size;
b635b00e 1216 sector_t sector = io->sector;
dec1cedf 1217 int r;
8b004457 1218
fc5a5e9a
MB
1219 /*
1220 * Prevent io from disappearing until this function completes.
1221 */
1222 crypt_inc_pending(io);
b635b00e 1223 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
fc5a5e9a 1224
93e605c2
MB
1225 /*
1226 * The allocated buffers can be smaller than the whole bio,
1227 * so repeat the whole process until all the data can be handled.
1228 */
1229 while (remaining) {
933f01d4 1230 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
23541d2d 1231 if (unlikely(!clone)) {
5742fd77 1232 io->error = -ENOMEM;
fc5a5e9a 1233 break;
23541d2d 1234 }
93e605c2 1235
53017030 1236 io->ctx.bio_out = clone;
003b5c57 1237 io->ctx.iter_out = clone->bi_iter;
93e605c2 1238
4f024f37 1239 remaining -= clone->bi_iter.bi_size;
b635b00e 1240 sector += bio_sectors(clone);
93e605c2 1241
4e594098 1242 crypt_inc_pending(io);
72c6e7af 1243
dec1cedf 1244 r = crypt_convert(cc, &io->ctx);
72c6e7af
MP
1245 if (r < 0)
1246 io->error = -EIO;
1247
40b6229b 1248 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
f97380bc 1249
c8081618
MB
1250 /* Encryption was already finished, submit io now */
1251 if (crypt_finished) {
72c6e7af 1252 kcryptd_crypt_write_io_submit(io, 0);
c8081618
MB
1253
1254 /*
1255 * If there was an error, do not try next fragments.
1256 * For async, error is processed in async handler.
1257 */
6c031f41 1258 if (unlikely(r < 0))
fc5a5e9a 1259 break;
b635b00e
MB
1260
1261 io->sector = sector;
4e594098 1262 }
93e605c2 1263
933f01d4
MB
1264 /*
1265 * Out of memory -> run queues
1266 * But don't wait if split was due to the io size restriction
1267 */
1268 if (unlikely(out_of_pages))
8aa7e847 1269 congestion_wait(BLK_RW_ASYNC, HZ/100);
933f01d4 1270
393b47ef
MB
1271 /*
1272 * With async crypto it is unsafe to share the crypto context
1273 * between fragments, so switch to a new dm_crypt_io structure.
1274 */
1275 if (unlikely(!crypt_finished && remaining)) {
49a8a920 1276 new_io = crypt_io_alloc(io->cc, io->base_bio,
393b47ef
MB
1277 sector);
1278 crypt_inc_pending(new_io);
1279 crypt_convert_init(cc, &new_io->ctx, NULL,
1280 io->base_bio, sector);
003b5c57 1281 new_io->ctx.iter_in = io->ctx.iter_in;
393b47ef
MB
1282
1283 /*
1284 * Fragments after the first use the base_io
1285 * pending count.
1286 */
1287 if (!io->base_io)
1288 new_io->base_io = io;
1289 else {
1290 new_io->base_io = io->base_io;
1291 crypt_inc_pending(io->base_io);
1292 crypt_dec_pending(io);
1293 }
1294
1295 io = new_io;
1296 }
93e605c2 1297 }
899c95d3
MB
1298
1299 crypt_dec_pending(io);
84131db6
MB
1300}
1301
72c6e7af 1302static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
5742fd77 1303{
5742fd77
MB
1304 crypt_dec_pending(io);
1305}
1306
4e4eef64 1307static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
8b004457 1308{
49a8a920 1309 struct crypt_config *cc = io->cc;
5742fd77 1310 int r = 0;
1da177e4 1311
3e1a8bdd 1312 crypt_inc_pending(io);
3a7f6c99 1313
53017030 1314 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
0c395b0f 1315 io->sector);
1da177e4 1316
5742fd77 1317 r = crypt_convert(cc, &io->ctx);
72c6e7af
MP
1318 if (r < 0)
1319 io->error = -EIO;
5742fd77 1320
40b6229b 1321 if (atomic_dec_and_test(&io->ctx.cc_pending))
72c6e7af 1322 kcryptd_crypt_read_done(io);
3a7f6c99
MB
1323
1324 crypt_dec_pending(io);
1da177e4
LT
1325}
1326
95497a96
MB
1327static void kcryptd_async_done(struct crypto_async_request *async_req,
1328 int error)
1329{
b2174eeb
HY
1330 struct dm_crypt_request *dmreq = async_req->data;
1331 struct convert_context *ctx = dmreq->ctx;
95497a96 1332 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
49a8a920 1333 struct crypt_config *cc = io->cc;
95497a96
MB
1334
1335 if (error == -EINPROGRESS) {
1336 complete(&ctx->restart);
1337 return;
1338 }
1339
2dc5327d
MB
1340 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1341 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1342
72c6e7af
MP
1343 if (error < 0)
1344 io->error = -EIO;
1345
b2174eeb 1346 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
95497a96 1347
40b6229b 1348 if (!atomic_dec_and_test(&ctx->cc_pending))
95497a96
MB
1349 return;
1350
1351 if (bio_data_dir(io->base_bio) == READ)
72c6e7af 1352 kcryptd_crypt_read_done(io);
95497a96 1353 else
72c6e7af 1354 kcryptd_crypt_write_io_submit(io, 1);
95497a96
MB
1355}
1356
395b167c 1357static void kcryptd_crypt(struct work_struct *work)
1da177e4 1358{
028867ac 1359 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
8b004457 1360
cabf08e4 1361 if (bio_data_dir(io->base_bio) == READ)
395b167c 1362 kcryptd_crypt_read_convert(io);
4e4eef64 1363 else
395b167c 1364 kcryptd_crypt_write_convert(io);
cabf08e4
MB
1365}
1366
395b167c 1367static void kcryptd_queue_crypt(struct dm_crypt_io *io)
cabf08e4 1368{
49a8a920 1369 struct crypt_config *cc = io->cc;
cabf08e4 1370
395b167c
AK
1371 INIT_WORK(&io->work, kcryptd_crypt);
1372 queue_work(cc->crypt_queue, &io->work);
1da177e4
LT
1373}
1374
1375/*
1376 * Decode key from its hex representation
1377 */
1378static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1379{
1380 char buffer[3];
1da177e4
LT
1381 unsigned int i;
1382
1383 buffer[2] = '\0';
1384
8b004457 1385 for (i = 0; i < size; i++) {
1da177e4
LT
1386 buffer[0] = *hex++;
1387 buffer[1] = *hex++;
1388
1a66a08a 1389 if (kstrtou8(buffer, 16, &key[i]))
1da177e4
LT
1390 return -EINVAL;
1391 }
1392
1393 if (*hex != '\0')
1394 return -EINVAL;
1395
1396 return 0;
1397}
1398
fd2d231f 1399static void crypt_free_tfms(struct crypt_config *cc)
d1f96423 1400{
d1f96423
MB
1401 unsigned i;
1402
fd2d231f
MP
1403 if (!cc->tfms)
1404 return;
1405
d1f96423 1406 for (i = 0; i < cc->tfms_count; i++)
fd2d231f
MP
1407 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1408 crypto_free_ablkcipher(cc->tfms[i]);
1409 cc->tfms[i] = NULL;
d1f96423 1410 }
fd2d231f
MP
1411
1412 kfree(cc->tfms);
1413 cc->tfms = NULL;
d1f96423
MB
1414}
1415
fd2d231f 1416static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
d1f96423 1417{
d1f96423
MB
1418 unsigned i;
1419 int err;
1420
fd2d231f
MP
1421 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1422 GFP_KERNEL);
1423 if (!cc->tfms)
1424 return -ENOMEM;
1425
d1f96423 1426 for (i = 0; i < cc->tfms_count; i++) {
fd2d231f
MP
1427 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1428 if (IS_ERR(cc->tfms[i])) {
1429 err = PTR_ERR(cc->tfms[i]);
1430 crypt_free_tfms(cc);
d1f96423
MB
1431 return err;
1432 }
1433 }
1434
1435 return 0;
1436}
1437
c0297721
AK
1438static int crypt_setkey_allcpus(struct crypt_config *cc)
1439{
da31a078 1440 unsigned subkey_size;
fd2d231f
MP
1441 int err = 0, i, r;
1442
da31a078
MB
1443 /* Ignore extra keys (which are used for IV etc) */
1444 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1445
fd2d231f
MP
1446 for (i = 0; i < cc->tfms_count; i++) {
1447 r = crypto_ablkcipher_setkey(cc->tfms[i],
1448 cc->key + (i * subkey_size),
1449 subkey_size);
1450 if (r)
1451 err = r;
c0297721
AK
1452 }
1453
1454 return err;
1455}
1456
e48d4bbf
MB
1457static int crypt_set_key(struct crypt_config *cc, char *key)
1458{
de8be5ac
MB
1459 int r = -EINVAL;
1460 int key_string_len = strlen(key);
1461
69a8cfcd 1462 /* The key size may not be changed. */
de8be5ac
MB
1463 if (cc->key_size != (key_string_len >> 1))
1464 goto out;
e48d4bbf 1465
69a8cfcd
MB
1466 /* Hyphen (which gives a key_size of zero) means there is no key. */
1467 if (!cc->key_size && strcmp(key, "-"))
de8be5ac 1468 goto out;
e48d4bbf 1469
69a8cfcd 1470 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
de8be5ac 1471 goto out;
e48d4bbf
MB
1472
1473 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1474
de8be5ac
MB
1475 r = crypt_setkey_allcpus(cc);
1476
1477out:
1478 /* Hex key string not needed after here, so wipe it. */
1479 memset(key, '0', key_string_len);
1480
1481 return r;
e48d4bbf
MB
1482}
1483
1484static int crypt_wipe_key(struct crypt_config *cc)
1485{
1486 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1487 memset(&cc->key, 0, cc->key_size * sizeof(u8));
c0297721
AK
1488
1489 return crypt_setkey_allcpus(cc);
e48d4bbf
MB
1490}
1491
28513fcc
MB
1492static void crypt_dtr(struct dm_target *ti)
1493{
1494 struct crypt_config *cc = ti->private;
c0297721
AK
1495 struct crypt_cpu *cpu_cc;
1496 int cpu;
28513fcc
MB
1497
1498 ti->private = NULL;
1499
1500 if (!cc)
1501 return;
1502
1503 if (cc->io_queue)
1504 destroy_workqueue(cc->io_queue);
1505 if (cc->crypt_queue)
1506 destroy_workqueue(cc->crypt_queue);
1507
c0297721
AK
1508 if (cc->cpu)
1509 for_each_possible_cpu(cpu) {
1510 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
1511 if (cpu_cc->req)
1512 mempool_free(cpu_cc->req, cc->req_pool);
c0297721
AK
1513 }
1514
fd2d231f
MP
1515 crypt_free_tfms(cc);
1516
28513fcc
MB
1517 if (cc->bs)
1518 bioset_free(cc->bs);
1519
1520 if (cc->page_pool)
1521 mempool_destroy(cc->page_pool);
1522 if (cc->req_pool)
1523 mempool_destroy(cc->req_pool);
1524 if (cc->io_pool)
1525 mempool_destroy(cc->io_pool);
1526
1527 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1528 cc->iv_gen_ops->dtr(cc);
1529
28513fcc
MB
1530 if (cc->dev)
1531 dm_put_device(ti, cc->dev);
1532
c0297721
AK
1533 if (cc->cpu)
1534 free_percpu(cc->cpu);
1535
5ebaee6d 1536 kzfree(cc->cipher);
7dbcd137 1537 kzfree(cc->cipher_string);
28513fcc
MB
1538
1539 /* Must zero key material before freeing */
1540 kzfree(cc);
1541}
1542
5ebaee6d
MB
1543static int crypt_ctr_cipher(struct dm_target *ti,
1544 char *cipher_in, char *key)
1da177e4 1545{
5ebaee6d 1546 struct crypt_config *cc = ti->private;
d1f96423 1547 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
5ebaee6d 1548 char *cipher_api = NULL;
fd2d231f 1549 int ret = -EINVAL;
31998ef1 1550 char dummy;
1da177e4 1551
5ebaee6d
MB
1552 /* Convert to crypto api definition? */
1553 if (strchr(cipher_in, '(')) {
1554 ti->error = "Bad cipher specification";
1da177e4
LT
1555 return -EINVAL;
1556 }
1557
7dbcd137
MB
1558 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1559 if (!cc->cipher_string)
1560 goto bad_mem;
1561
5ebaee6d
MB
1562 /*
1563 * Legacy dm-crypt cipher specification
d1f96423 1564 * cipher[:keycount]-mode-iv:ivopts
5ebaee6d
MB
1565 */
1566 tmp = cipher_in;
d1f96423
MB
1567 keycount = strsep(&tmp, "-");
1568 cipher = strsep(&keycount, ":");
1569
1570 if (!keycount)
1571 cc->tfms_count = 1;
31998ef1 1572 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
d1f96423
MB
1573 !is_power_of_2(cc->tfms_count)) {
1574 ti->error = "Bad cipher key count specification";
1575 return -EINVAL;
1576 }
1577 cc->key_parts = cc->tfms_count;
da31a078 1578 cc->key_extra_size = 0;
5ebaee6d
MB
1579
1580 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1581 if (!cc->cipher)
1582 goto bad_mem;
1583
1da177e4
LT
1584 chainmode = strsep(&tmp, "-");
1585 ivopts = strsep(&tmp, "-");
1586 ivmode = strsep(&ivopts, ":");
1587
1588 if (tmp)
5ebaee6d 1589 DMWARN("Ignoring unexpected additional cipher options");
1da177e4 1590
fd2d231f 1591 cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)),
d1f96423 1592 __alignof__(struct crypt_cpu));
c0297721
AK
1593 if (!cc->cpu) {
1594 ti->error = "Cannot allocate per cpu state";
1595 goto bad_mem;
1596 }
1597
7dbcd137
MB
1598 /*
1599 * For compatibility with the original dm-crypt mapping format, if
1600 * only the cipher name is supplied, use cbc-plain.
1601 */
5ebaee6d 1602 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
1da177e4
LT
1603 chainmode = "cbc";
1604 ivmode = "plain";
1605 }
1606
d1806f6a 1607 if (strcmp(chainmode, "ecb") && !ivmode) {
5ebaee6d
MB
1608 ti->error = "IV mechanism required";
1609 return -EINVAL;
1da177e4
LT
1610 }
1611
5ebaee6d
MB
1612 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1613 if (!cipher_api)
1614 goto bad_mem;
1615
1616 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1617 "%s(%s)", chainmode, cipher);
1618 if (ret < 0) {
1619 kfree(cipher_api);
1620 goto bad_mem;
1da177e4
LT
1621 }
1622
5ebaee6d 1623 /* Allocate cipher */
fd2d231f
MP
1624 ret = crypt_alloc_tfms(cc, cipher_api);
1625 if (ret < 0) {
1626 ti->error = "Error allocating crypto tfm";
1627 goto bad;
1da177e4 1628 }
1da177e4 1629
5ebaee6d 1630 /* Initialize IV */
c0297721 1631 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
5ebaee6d
MB
1632 if (cc->iv_size)
1633 /* at least a 64 bit sector number should fit in our buffer */
1634 cc->iv_size = max(cc->iv_size,
1635 (unsigned int)(sizeof(u64) / sizeof(u8)));
1636 else if (ivmode) {
1637 DMWARN("Selected cipher does not support IVs");
1638 ivmode = NULL;
1639 }
1640
1641 /* Choose ivmode, see comments at iv code. */
1da177e4
LT
1642 if (ivmode == NULL)
1643 cc->iv_gen_ops = NULL;
1644 else if (strcmp(ivmode, "plain") == 0)
1645 cc->iv_gen_ops = &crypt_iv_plain_ops;
61afef61
MB
1646 else if (strcmp(ivmode, "plain64") == 0)
1647 cc->iv_gen_ops = &crypt_iv_plain64_ops;
1da177e4
LT
1648 else if (strcmp(ivmode, "essiv") == 0)
1649 cc->iv_gen_ops = &crypt_iv_essiv_ops;
48527fa7
RS
1650 else if (strcmp(ivmode, "benbi") == 0)
1651 cc->iv_gen_ops = &crypt_iv_benbi_ops;
46b47730
LN
1652 else if (strcmp(ivmode, "null") == 0)
1653 cc->iv_gen_ops = &crypt_iv_null_ops;
34745785
MB
1654 else if (strcmp(ivmode, "lmk") == 0) {
1655 cc->iv_gen_ops = &crypt_iv_lmk_ops;
ed04d981
MB
1656 /*
1657 * Version 2 and 3 is recognised according
34745785
MB
1658 * to length of provided multi-key string.
1659 * If present (version 3), last key is used as IV seed.
ed04d981 1660 * All keys (including IV seed) are always the same size.
34745785 1661 */
da31a078 1662 if (cc->key_size % cc->key_parts) {
34745785 1663 cc->key_parts++;
da31a078
MB
1664 cc->key_extra_size = cc->key_size / cc->key_parts;
1665 }
ed04d981
MB
1666 } else if (strcmp(ivmode, "tcw") == 0) {
1667 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1668 cc->key_parts += 2; /* IV + whitening */
1669 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
34745785 1670 } else {
5ebaee6d 1671 ret = -EINVAL;
72d94861 1672 ti->error = "Invalid IV mode";
28513fcc 1673 goto bad;
1da177e4
LT
1674 }
1675
da31a078
MB
1676 /* Initialize and set key */
1677 ret = crypt_set_key(cc, key);
1678 if (ret < 0) {
1679 ti->error = "Error decoding and setting key";
1680 goto bad;
1681 }
1682
28513fcc
MB
1683 /* Allocate IV */
1684 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1685 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1686 if (ret < 0) {
1687 ti->error = "Error creating IV";
1688 goto bad;
1689 }
1690 }
1da177e4 1691
28513fcc
MB
1692 /* Initialize IV (set keys for ESSIV etc) */
1693 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1694 ret = cc->iv_gen_ops->init(cc);
1695 if (ret < 0) {
1696 ti->error = "Error initialising IV";
1697 goto bad;
1698 }
b95bf2d3
MB
1699 }
1700
5ebaee6d
MB
1701 ret = 0;
1702bad:
1703 kfree(cipher_api);
1704 return ret;
1705
1706bad_mem:
1707 ti->error = "Cannot allocate cipher strings";
1708 return -ENOMEM;
1709}
1710
1711/*
1712 * Construct an encryption mapping:
1713 * <cipher> <key> <iv_offset> <dev_path> <start>
1714 */
1715static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1716{
1717 struct crypt_config *cc;
772ae5f5 1718 unsigned int key_size, opt_params;
5ebaee6d
MB
1719 unsigned long long tmpll;
1720 int ret;
772ae5f5
MB
1721 struct dm_arg_set as;
1722 const char *opt_string;
31998ef1 1723 char dummy;
772ae5f5
MB
1724
1725 static struct dm_arg _args[] = {
1726 {0, 1, "Invalid number of feature args"},
1727 };
5ebaee6d 1728
772ae5f5 1729 if (argc < 5) {
5ebaee6d
MB
1730 ti->error = "Not enough arguments";
1731 return -EINVAL;
1da177e4
LT
1732 }
1733
5ebaee6d
MB
1734 key_size = strlen(argv[1]) >> 1;
1735
1736 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1737 if (!cc) {
1738 ti->error = "Cannot allocate encryption context";
1739 return -ENOMEM;
1740 }
69a8cfcd 1741 cc->key_size = key_size;
5ebaee6d
MB
1742
1743 ti->private = cc;
1744 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1745 if (ret < 0)
1746 goto bad;
1747
28513fcc 1748 ret = -ENOMEM;
93d2341c 1749 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
1da177e4 1750 if (!cc->io_pool) {
72d94861 1751 ti->error = "Cannot allocate crypt io mempool";
28513fcc 1752 goto bad;
1da177e4
LT
1753 }
1754
ddd42edf 1755 cc->dmreq_start = sizeof(struct ablkcipher_request);
c0297721 1756 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
ddd42edf 1757 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
c0297721 1758 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
3a7f6c99 1759 ~(crypto_tfm_ctx_alignment() - 1);
ddd42edf
MB
1760
1761 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1762 sizeof(struct dm_crypt_request) + cc->iv_size);
1763 if (!cc->req_pool) {
1764 ti->error = "Cannot allocate crypt request mempool";
28513fcc 1765 goto bad;
ddd42edf 1766 }
ddd42edf 1767
a19b27ce 1768 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
1da177e4 1769 if (!cc->page_pool) {
72d94861 1770 ti->error = "Cannot allocate page mempool";
28513fcc 1771 goto bad;
1da177e4
LT
1772 }
1773
bb799ca0 1774 cc->bs = bioset_create(MIN_IOS, 0);
6a24c718
MB
1775 if (!cc->bs) {
1776 ti->error = "Cannot allocate crypt bioset";
28513fcc 1777 goto bad;
6a24c718
MB
1778 }
1779
28513fcc 1780 ret = -EINVAL;
31998ef1 1781 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
72d94861 1782 ti->error = "Invalid iv_offset sector";
28513fcc 1783 goto bad;
1da177e4 1784 }
4ee218cd 1785 cc->iv_offset = tmpll;
1da177e4 1786
28513fcc
MB
1787 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1788 ti->error = "Device lookup failed";
1789 goto bad;
1790 }
1791
31998ef1 1792 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
72d94861 1793 ti->error = "Invalid device sector";
28513fcc 1794 goto bad;
1da177e4 1795 }
4ee218cd 1796 cc->start = tmpll;
1da177e4 1797
772ae5f5
MB
1798 argv += 5;
1799 argc -= 5;
1800
1801 /* Optional parameters */
1802 if (argc) {
1803 as.argc = argc;
1804 as.argv = argv;
1805
1806 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1807 if (ret)
1808 goto bad;
1809
1810 opt_string = dm_shift_arg(&as);
1811
1812 if (opt_params == 1 && opt_string &&
1813 !strcasecmp(opt_string, "allow_discards"))
55a62eef 1814 ti->num_discard_bios = 1;
772ae5f5
MB
1815 else if (opt_params) {
1816 ret = -EINVAL;
1817 ti->error = "Invalid feature arguments";
1818 goto bad;
1819 }
1820 }
1821
28513fcc 1822 ret = -ENOMEM;
670368a8 1823 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
cabf08e4
MB
1824 if (!cc->io_queue) {
1825 ti->error = "Couldn't create kcryptd io queue";
28513fcc 1826 goto bad;
cabf08e4
MB
1827 }
1828
c0297721 1829 cc->crypt_queue = alloc_workqueue("kcryptd",
670368a8 1830 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
cabf08e4 1831 if (!cc->crypt_queue) {
9934a8be 1832 ti->error = "Couldn't create kcryptd queue";
28513fcc 1833 goto bad;
9934a8be
MB
1834 }
1835
55a62eef 1836 ti->num_flush_bios = 1;
0ac55489 1837 ti->discard_zeroes_data_unsupported = true;
983c7db3 1838
1da177e4
LT
1839 return 0;
1840
28513fcc
MB
1841bad:
1842 crypt_dtr(ti);
1843 return ret;
1da177e4
LT
1844}
1845
7de3ee57 1846static int crypt_map(struct dm_target *ti, struct bio *bio)
1da177e4 1847{
028867ac 1848 struct dm_crypt_io *io;
49a8a920 1849 struct crypt_config *cc = ti->private;
647c7db1 1850
772ae5f5
MB
1851 /*
1852 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1853 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1854 * - for REQ_DISCARD caller must use flush if IO ordering matters
1855 */
1856 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
647c7db1 1857 bio->bi_bdev = cc->dev->bdev;
772ae5f5 1858 if (bio_sectors(bio))
4f024f37
KO
1859 bio->bi_iter.bi_sector = cc->start +
1860 dm_target_offset(ti, bio->bi_iter.bi_sector);
647c7db1
MP
1861 return DM_MAPIO_REMAPPED;
1862 }
1da177e4 1863
4f024f37 1864 io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
cabf08e4 1865
20c82538
MB
1866 if (bio_data_dir(io->base_bio) == READ) {
1867 if (kcryptd_io_read(io, GFP_NOWAIT))
1868 kcryptd_queue_io(io);
1869 } else
cabf08e4 1870 kcryptd_queue_crypt(io);
1da177e4 1871
d2a7ad29 1872 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1873}
1874
fd7c092e
MP
1875static void crypt_status(struct dm_target *ti, status_type_t type,
1876 unsigned status_flags, char *result, unsigned maxlen)
1da177e4 1877{
5ebaee6d 1878 struct crypt_config *cc = ti->private;
fd7c092e 1879 unsigned i, sz = 0;
1da177e4
LT
1880
1881 switch (type) {
1882 case STATUSTYPE_INFO:
1883 result[0] = '\0';
1884 break;
1885
1886 case STATUSTYPE_TABLE:
7dbcd137 1887 DMEMIT("%s ", cc->cipher_string);
1da177e4 1888
fd7c092e
MP
1889 if (cc->key_size > 0)
1890 for (i = 0; i < cc->key_size; i++)
1891 DMEMIT("%02x", cc->key[i]);
1892 else
1893 DMEMIT("-");
1da177e4 1894
4ee218cd
AM
1895 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1896 cc->dev->name, (unsigned long long)cc->start);
772ae5f5 1897
55a62eef 1898 if (ti->num_discard_bios)
772ae5f5
MB
1899 DMEMIT(" 1 allow_discards");
1900
1da177e4
LT
1901 break;
1902 }
1da177e4
LT
1903}
1904
e48d4bbf
MB
1905static void crypt_postsuspend(struct dm_target *ti)
1906{
1907 struct crypt_config *cc = ti->private;
1908
1909 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1910}
1911
1912static int crypt_preresume(struct dm_target *ti)
1913{
1914 struct crypt_config *cc = ti->private;
1915
1916 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1917 DMERR("aborting resume - crypt key is not set.");
1918 return -EAGAIN;
1919 }
1920
1921 return 0;
1922}
1923
1924static void crypt_resume(struct dm_target *ti)
1925{
1926 struct crypt_config *cc = ti->private;
1927
1928 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1929}
1930
1931/* Message interface
1932 * key set <key>
1933 * key wipe
1934 */
1935static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1936{
1937 struct crypt_config *cc = ti->private;
542da317 1938 int ret = -EINVAL;
e48d4bbf
MB
1939
1940 if (argc < 2)
1941 goto error;
1942
498f0103 1943 if (!strcasecmp(argv[0], "key")) {
e48d4bbf
MB
1944 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1945 DMWARN("not suspended during key manipulation.");
1946 return -EINVAL;
1947 }
498f0103 1948 if (argc == 3 && !strcasecmp(argv[1], "set")) {
542da317
MB
1949 ret = crypt_set_key(cc, argv[2]);
1950 if (ret)
1951 return ret;
1952 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1953 ret = cc->iv_gen_ops->init(cc);
1954 return ret;
1955 }
498f0103 1956 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
542da317
MB
1957 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1958 ret = cc->iv_gen_ops->wipe(cc);
1959 if (ret)
1960 return ret;
1961 }
e48d4bbf 1962 return crypt_wipe_key(cc);
542da317 1963 }
e48d4bbf
MB
1964 }
1965
1966error:
1967 DMWARN("unrecognised message received.");
1968 return -EINVAL;
1969}
1970
d41e26b9
MB
1971static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1972 struct bio_vec *biovec, int max_size)
1973{
1974 struct crypt_config *cc = ti->private;
1975 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1976
1977 if (!q->merge_bvec_fn)
1978 return max_size;
1979
1980 bvm->bi_bdev = cc->dev->bdev;
b441a262 1981 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
d41e26b9
MB
1982
1983 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1984}
1985
af4874e0
MS
1986static int crypt_iterate_devices(struct dm_target *ti,
1987 iterate_devices_callout_fn fn, void *data)
1988{
1989 struct crypt_config *cc = ti->private;
1990
5dea271b 1991 return fn(ti, cc->dev, cc->start, ti->len, data);
af4874e0
MS
1992}
1993
1da177e4
LT
1994static struct target_type crypt_target = {
1995 .name = "crypt",
ed04d981 1996 .version = {1, 13, 0},
1da177e4
LT
1997 .module = THIS_MODULE,
1998 .ctr = crypt_ctr,
1999 .dtr = crypt_dtr,
2000 .map = crypt_map,
2001 .status = crypt_status,
e48d4bbf
MB
2002 .postsuspend = crypt_postsuspend,
2003 .preresume = crypt_preresume,
2004 .resume = crypt_resume,
2005 .message = crypt_message,
d41e26b9 2006 .merge = crypt_merge,
af4874e0 2007 .iterate_devices = crypt_iterate_devices,
1da177e4
LT
2008};
2009
2010static int __init dm_crypt_init(void)
2011{
2012 int r;
2013
028867ac 2014 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
1da177e4
LT
2015 if (!_crypt_io_pool)
2016 return -ENOMEM;
2017
1da177e4
LT
2018 r = dm_register_target(&crypt_target);
2019 if (r < 0) {
72d94861 2020 DMERR("register failed %d", r);
9934a8be 2021 kmem_cache_destroy(_crypt_io_pool);
1da177e4
LT
2022 }
2023
1da177e4
LT
2024 return r;
2025}
2026
2027static void __exit dm_crypt_exit(void)
2028{
10d3bd09 2029 dm_unregister_target(&crypt_target);
1da177e4
LT
2030 kmem_cache_destroy(_crypt_io_pool);
2031}
2032
2033module_init(dm_crypt_init);
2034module_exit(dm_crypt_exit);
2035
2036MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
2037MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2038MODULE_LICENSE("GPL");