dm crypt: extract scatterlist processing
[deliverable/linux.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>
4e4eef64 4 * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
1da177e4
LT
5 *
6 * This file is released under the GPL.
7 */
8
d1806f6a 9#include <linux/err.h>
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
15#include <linux/mempool.h>
16#include <linux/slab.h>
17#include <linux/crypto.h>
18#include <linux/workqueue.h>
3fcfab16 19#include <linux/backing-dev.h>
1da177e4 20#include <asm/atomic.h>
378f058c 21#include <linux/scatterlist.h>
1da177e4 22#include <asm/page.h>
48527fa7 23#include <asm/unaligned.h>
1da177e4
LT
24
25#include "dm.h"
26
72d94861 27#define DM_MSG_PREFIX "crypt"
e48d4bbf 28#define MESG_STR(x) x, sizeof(x)
1da177e4 29
1da177e4
LT
30/*
31 * context holding the current state of a multi-part conversion
32 */
33struct convert_context {
34 struct bio *bio_in;
35 struct bio *bio_out;
36 unsigned int offset_in;
37 unsigned int offset_out;
38 unsigned int idx_in;
39 unsigned int idx_out;
40 sector_t sector;
1da177e4
LT
41};
42
53017030
MB
43/*
44 * per bio private data
45 */
46struct dm_crypt_io {
47 struct dm_target *target;
48 struct bio *base_bio;
49 struct work_struct work;
50
51 struct convert_context ctx;
52
53 atomic_t pending;
54 int error;
0c395b0f 55 sector_t sector;
53017030
MB
56};
57
01482b76
MB
58struct dm_crypt_request {
59 struct scatterlist sg_in;
60 struct scatterlist sg_out;
61};
62
1da177e4
LT
63struct crypt_config;
64
65struct crypt_iv_operations {
66 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
d469f841 67 const char *opts);
1da177e4
LT
68 void (*dtr)(struct crypt_config *cc);
69 const char *(*status)(struct crypt_config *cc);
70 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
71};
72
73/*
74 * Crypt: maps a linear range of a block device
75 * and encrypts / decrypts at the same time.
76 */
e48d4bbf 77enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
1da177e4
LT
78struct crypt_config {
79 struct dm_dev *dev;
80 sector_t start;
81
82 /*
83 * pool for per bio private data and
84 * for encryption buffer pages
85 */
86 mempool_t *io_pool;
87 mempool_t *page_pool;
6a24c718 88 struct bio_set *bs;
1da177e4 89
cabf08e4
MB
90 struct workqueue_struct *io_queue;
91 struct workqueue_struct *crypt_queue;
1da177e4
LT
92 /*
93 * crypto related data
94 */
95 struct crypt_iv_operations *iv_gen_ops;
96 char *iv_mode;
79066ad3
HX
97 union {
98 struct crypto_cipher *essiv_tfm;
99 int benbi_shift;
100 } iv_gen_private;
1da177e4
LT
101 sector_t iv_offset;
102 unsigned int iv_size;
103
d1806f6a
HX
104 char cipher[CRYPTO_MAX_ALG_NAME];
105 char chainmode[CRYPTO_MAX_ALG_NAME];
106 struct crypto_blkcipher *tfm;
e48d4bbf 107 unsigned long flags;
1da177e4
LT
108 unsigned int key_size;
109 u8 key[0];
110};
111
6a24c718 112#define MIN_IOS 16
1da177e4
LT
113#define MIN_POOL_PAGES 32
114#define MIN_BIO_PAGES 8
115
e18b890b 116static struct kmem_cache *_crypt_io_pool;
1da177e4 117
028867ac 118static void clone_init(struct dm_crypt_io *, struct bio *);
395b167c 119static void kcryptd_queue_crypt(struct dm_crypt_io *io);
027581f3 120
1da177e4
LT
121/*
122 * Different IV generation algorithms:
123 *
3c164bd8 124 * plain: the initial vector is the 32-bit little-endian version of the sector
3a4fa0a2 125 * number, padded with zeros if necessary.
1da177e4 126 *
3c164bd8
RS
127 * essiv: "encrypted sector|salt initial vector", the sector number is
128 * encrypted with the bulk cipher using a salt as key. The salt
129 * should be derived from the bulk cipher's key via hashing.
1da177e4 130 *
48527fa7
RS
131 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
132 * (needed for LRW-32-AES and possible other narrow block modes)
133 *
46b47730
LN
134 * null: the initial vector is always zero. Provides compatibility with
135 * obsolete loop_fish2 devices. Do not use for new devices.
136 *
1da177e4
LT
137 * plumb: unimplemented, see:
138 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
139 */
140
141static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
142{
143 memset(iv, 0, cc->iv_size);
144 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
145
146 return 0;
147}
148
149static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
d469f841 150 const char *opts)
1da177e4 151{
d1806f6a 152 struct crypto_cipher *essiv_tfm;
35058687
HX
153 struct crypto_hash *hash_tfm;
154 struct hash_desc desc;
1da177e4
LT
155 struct scatterlist sg;
156 unsigned int saltsize;
157 u8 *salt;
d1806f6a 158 int err;
1da177e4
LT
159
160 if (opts == NULL) {
72d94861 161 ti->error = "Digest algorithm missing for ESSIV mode";
1da177e4
LT
162 return -EINVAL;
163 }
164
165 /* Hash the cipher key with the given hash algorithm */
35058687
HX
166 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
167 if (IS_ERR(hash_tfm)) {
72d94861 168 ti->error = "Error initializing ESSIV hash";
35058687 169 return PTR_ERR(hash_tfm);
1da177e4
LT
170 }
171
35058687 172 saltsize = crypto_hash_digestsize(hash_tfm);
1da177e4
LT
173 salt = kmalloc(saltsize, GFP_KERNEL);
174 if (salt == NULL) {
72d94861 175 ti->error = "Error kmallocing salt storage in ESSIV";
35058687 176 crypto_free_hash(hash_tfm);
1da177e4
LT
177 return -ENOMEM;
178 }
179
68e3f5dd 180 sg_init_one(&sg, cc->key, cc->key_size);
35058687
HX
181 desc.tfm = hash_tfm;
182 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
183 err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
184 crypto_free_hash(hash_tfm);
185
186 if (err) {
187 ti->error = "Error calculating hash in ESSIV";
815f9e32 188 kfree(salt);
35058687
HX
189 return err;
190 }
1da177e4
LT
191
192 /* Setup the essiv_tfm with the given salt */
d1806f6a
HX
193 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
194 if (IS_ERR(essiv_tfm)) {
72d94861 195 ti->error = "Error allocating crypto tfm for ESSIV";
1da177e4 196 kfree(salt);
d1806f6a 197 return PTR_ERR(essiv_tfm);
1da177e4 198 }
d1806f6a
HX
199 if (crypto_cipher_blocksize(essiv_tfm) !=
200 crypto_blkcipher_ivsize(cc->tfm)) {
72d94861 201 ti->error = "Block size of ESSIV cipher does "
d469f841 202 "not match IV size of block cipher";
d1806f6a 203 crypto_free_cipher(essiv_tfm);
1da177e4
LT
204 kfree(salt);
205 return -EINVAL;
206 }
d1806f6a
HX
207 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
208 if (err) {
72d94861 209 ti->error = "Failed to set key for ESSIV cipher";
d1806f6a 210 crypto_free_cipher(essiv_tfm);
1da177e4 211 kfree(salt);
d1806f6a 212 return err;
1da177e4
LT
213 }
214 kfree(salt);
215
79066ad3 216 cc->iv_gen_private.essiv_tfm = essiv_tfm;
1da177e4
LT
217 return 0;
218}
219
220static void crypt_iv_essiv_dtr(struct crypt_config *cc)
221{
79066ad3
HX
222 crypto_free_cipher(cc->iv_gen_private.essiv_tfm);
223 cc->iv_gen_private.essiv_tfm = NULL;
1da177e4
LT
224}
225
226static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
227{
1da177e4
LT
228 memset(iv, 0, cc->iv_size);
229 *(u64 *)iv = cpu_to_le64(sector);
79066ad3 230 crypto_cipher_encrypt_one(cc->iv_gen_private.essiv_tfm, iv, iv);
1da177e4
LT
231 return 0;
232}
233
48527fa7
RS
234static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
235 const char *opts)
236{
237 unsigned int bs = crypto_blkcipher_blocksize(cc->tfm);
f0d1b0b3 238 int log = ilog2(bs);
48527fa7
RS
239
240 /* we need to calculate how far we must shift the sector count
241 * to get the cipher block count, we use this shift in _gen */
242
243 if (1 << log != bs) {
244 ti->error = "cypher blocksize is not a power of 2";
245 return -EINVAL;
246 }
247
248 if (log > 9) {
249 ti->error = "cypher blocksize is > 512";
250 return -EINVAL;
251 }
252
79066ad3 253 cc->iv_gen_private.benbi_shift = 9 - log;
48527fa7
RS
254
255 return 0;
256}
257
258static void crypt_iv_benbi_dtr(struct crypt_config *cc)
259{
48527fa7
RS
260}
261
262static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
263{
79066ad3
HX
264 __be64 val;
265
48527fa7 266 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
79066ad3
HX
267
268 val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi_shift) + 1);
269 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
48527fa7 270
1da177e4
LT
271 return 0;
272}
273
46b47730
LN
274static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
275{
276 memset(iv, 0, cc->iv_size);
277
278 return 0;
279}
280
1da177e4
LT
281static struct crypt_iv_operations crypt_iv_plain_ops = {
282 .generator = crypt_iv_plain_gen
283};
284
285static struct crypt_iv_operations crypt_iv_essiv_ops = {
286 .ctr = crypt_iv_essiv_ctr,
287 .dtr = crypt_iv_essiv_dtr,
288 .generator = crypt_iv_essiv_gen
289};
290
48527fa7
RS
291static struct crypt_iv_operations crypt_iv_benbi_ops = {
292 .ctr = crypt_iv_benbi_ctr,
293 .dtr = crypt_iv_benbi_dtr,
294 .generator = crypt_iv_benbi_gen
295};
1da177e4 296
46b47730
LN
297static struct crypt_iv_operations crypt_iv_null_ops = {
298 .generator = crypt_iv_null_gen
299};
300
858119e1 301static int
1da177e4
LT
302crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
303 struct scatterlist *in, unsigned int length,
304 int write, sector_t sector)
305{
45789328 306 u8 iv[cc->iv_size] __attribute__ ((aligned(__alignof__(u64))));
d1806f6a
HX
307 struct blkcipher_desc desc = {
308 .tfm = cc->tfm,
309 .info = iv,
310 .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
311 };
1da177e4
LT
312 int r;
313
314 if (cc->iv_gen_ops) {
315 r = cc->iv_gen_ops->generator(cc, iv, sector);
316 if (r < 0)
317 return r;
318
319 if (write)
d1806f6a 320 r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
1da177e4 321 else
d1806f6a 322 r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
1da177e4
LT
323 } else {
324 if (write)
d1806f6a 325 r = crypto_blkcipher_encrypt(&desc, out, in, length);
1da177e4 326 else
d1806f6a 327 r = crypto_blkcipher_decrypt(&desc, out, in, length);
1da177e4
LT
328 }
329
330 return r;
331}
332
d469f841
MB
333static void crypt_convert_init(struct crypt_config *cc,
334 struct convert_context *ctx,
335 struct bio *bio_out, struct bio *bio_in,
fcd369da 336 sector_t sector)
1da177e4
LT
337{
338 ctx->bio_in = bio_in;
339 ctx->bio_out = bio_out;
340 ctx->offset_in = 0;
341 ctx->offset_out = 0;
342 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
343 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
344 ctx->sector = sector + cc->iv_offset;
1da177e4
LT
345}
346
01482b76
MB
347static int crypt_convert_block(struct crypt_config *cc,
348 struct convert_context *ctx)
349{
350 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
351 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
352 struct dm_crypt_request dmreq;
353
354 sg_init_table(&dmreq.sg_in, 1);
355 sg_set_page(&dmreq.sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
356 bv_in->bv_offset + ctx->offset_in);
357
358 sg_init_table(&dmreq.sg_out, 1);
359 sg_set_page(&dmreq.sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
360 bv_out->bv_offset + ctx->offset_out);
361
362 ctx->offset_in += 1 << SECTOR_SHIFT;
363 if (ctx->offset_in >= bv_in->bv_len) {
364 ctx->offset_in = 0;
365 ctx->idx_in++;
366 }
367
368 ctx->offset_out += 1 << SECTOR_SHIFT;
369 if (ctx->offset_out >= bv_out->bv_len) {
370 ctx->offset_out = 0;
371 ctx->idx_out++;
372 }
373
374 return crypt_convert_scatterlist(cc, &dmreq.sg_out, &dmreq.sg_in,
375 dmreq.sg_in.length,
376 bio_data_dir(ctx->bio_in) == WRITE,
377 ctx->sector);
378}
379
1da177e4
LT
380/*
381 * Encrypt / decrypt data from one bio to another one (can be the same one)
382 */
383static int crypt_convert(struct crypt_config *cc,
d469f841 384 struct convert_context *ctx)
1da177e4
LT
385{
386 int r = 0;
387
388 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
389 ctx->idx_out < ctx->bio_out->bi_vcnt) {
01482b76 390 r = crypt_convert_block(cc, ctx);
1da177e4
LT
391 if (r < 0)
392 break;
393
394 ctx->sector++;
395 }
396
397 return r;
398}
399
d469f841
MB
400static void dm_crypt_bio_destructor(struct bio *bio)
401{
028867ac 402 struct dm_crypt_io *io = bio->bi_private;
6a24c718
MB
403 struct crypt_config *cc = io->target->private;
404
405 bio_free(bio, cc->bs);
d469f841 406}
6a24c718 407
1da177e4
LT
408/*
409 * Generate a new unfragmented bio with the given size
410 * This should never violate the device limitations
411 * May return a smaller bio when running out of pages
412 */
028867ac 413static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
1da177e4 414{
027581f3 415 struct crypt_config *cc = io->target->private;
8b004457 416 struct bio *clone;
1da177e4 417 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
b4e3ca1a 418 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
91e10625
MB
419 unsigned i, len;
420 struct page *page;
1da177e4 421
2f9941b6 422 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
8b004457 423 if (!clone)
1da177e4 424 return NULL;
1da177e4 425
027581f3 426 clone_init(io, clone);
6a24c718 427
f97380bc 428 for (i = 0; i < nr_iovecs; i++) {
91e10625
MB
429 page = mempool_alloc(cc->page_pool, gfp_mask);
430 if (!page)
1da177e4
LT
431 break;
432
433 /*
434 * if additional pages cannot be allocated without waiting,
435 * return a partially allocated bio, the caller will then try
436 * to allocate additional bios while submitting this partial bio
437 */
f97380bc 438 if (i == (MIN_BIO_PAGES - 1))
1da177e4
LT
439 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
440
91e10625
MB
441 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
442
443 if (!bio_add_page(clone, page, len, 0)) {
444 mempool_free(page, cc->page_pool);
445 break;
446 }
1da177e4 447
91e10625 448 size -= len;
1da177e4
LT
449 }
450
8b004457
MB
451 if (!clone->bi_size) {
452 bio_put(clone);
1da177e4
LT
453 return NULL;
454 }
455
8b004457 456 return clone;
1da177e4
LT
457}
458
644bd2f0 459static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
1da177e4 460{
644bd2f0 461 unsigned int i;
1da177e4
LT
462 struct bio_vec *bv;
463
644bd2f0 464 for (i = 0; i < clone->bi_vcnt; i++) {
8b004457 465 bv = bio_iovec_idx(clone, i);
1da177e4
LT
466 BUG_ON(!bv->bv_page);
467 mempool_free(bv->bv_page, cc->page_pool);
468 bv->bv_page = NULL;
469 }
470}
471
472/*
473 * One of the bios was finished. Check for completion of
474 * the whole request and correctly clean up the buffer.
475 */
5742fd77 476static void crypt_dec_pending(struct dm_crypt_io *io)
1da177e4 477{
5742fd77 478 struct crypt_config *cc = io->target->private;
1da177e4
LT
479
480 if (!atomic_dec_and_test(&io->pending))
481 return;
482
6712ecf8 483 bio_endio(io->base_bio, io->error);
1da177e4
LT
484 mempool_free(io, cc->io_pool);
485}
486
487/*
cabf08e4 488 * kcryptd/kcryptd_io:
1da177e4
LT
489 *
490 * Needed because it would be very unwise to do decryption in an
23541d2d 491 * interrupt context.
cabf08e4
MB
492 *
493 * kcryptd performs the actual encryption or decryption.
494 *
495 * kcryptd_io performs the IO submission.
496 *
497 * They must be separated as otherwise the final stages could be
498 * starved by new requests which can block in the first stages due
499 * to memory allocation.
1da177e4 500 */
6712ecf8 501static void crypt_endio(struct bio *clone, int error)
8b004457 502{
028867ac 503 struct dm_crypt_io *io = clone->bi_private;
8b004457 504 struct crypt_config *cc = io->target->private;
ee7a491e 505 unsigned rw = bio_data_dir(clone);
8b004457 506
adfe4770
MB
507 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
508 error = -EIO;
509
8b004457 510 /*
6712ecf8 511 * free the processed pages
8b004457 512 */
ee7a491e 513 if (rw == WRITE)
644bd2f0 514 crypt_free_buffer_pages(cc, clone);
8b004457
MB
515
516 bio_put(clone);
8b004457 517
ee7a491e
MB
518 if (rw == READ && !error) {
519 kcryptd_queue_crypt(io);
520 return;
521 }
5742fd77
MB
522
523 if (unlikely(error))
524 io->error = error;
525
526 crypt_dec_pending(io);
8b004457
MB
527}
528
028867ac 529static void clone_init(struct dm_crypt_io *io, struct bio *clone)
8b004457
MB
530{
531 struct crypt_config *cc = io->target->private;
532
533 clone->bi_private = io;
534 clone->bi_end_io = crypt_endio;
535 clone->bi_bdev = cc->dev->bdev;
536 clone->bi_rw = io->base_bio->bi_rw;
027581f3 537 clone->bi_destructor = dm_crypt_bio_destructor;
8b004457
MB
538}
539
4e4eef64 540static void kcryptd_io_read(struct dm_crypt_io *io)
8b004457
MB
541{
542 struct crypt_config *cc = io->target->private;
543 struct bio *base_bio = io->base_bio;
544 struct bio *clone;
93e605c2
MB
545
546 atomic_inc(&io->pending);
8b004457
MB
547
548 /*
549 * The block layer might modify the bvec array, so always
550 * copy the required bvecs because we need the original
551 * one in order to decrypt the whole bio data *afterwards*.
552 */
6a24c718 553 clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
93e605c2 554 if (unlikely(!clone)) {
5742fd77
MB
555 io->error = -ENOMEM;
556 crypt_dec_pending(io);
23541d2d 557 return;
93e605c2 558 }
8b004457
MB
559
560 clone_init(io, clone);
561 clone->bi_idx = 0;
562 clone->bi_vcnt = bio_segments(base_bio);
563 clone->bi_size = base_bio->bi_size;
0c395b0f 564 clone->bi_sector = cc->start + io->sector;
8b004457
MB
565 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
566 sizeof(struct bio_vec) * clone->bi_vcnt);
8b004457 567
93e605c2 568 generic_make_request(clone);
8b004457
MB
569}
570
4e4eef64
MB
571static void kcryptd_io_write(struct dm_crypt_io *io)
572{
573}
574
395b167c
AK
575static void kcryptd_io(struct work_struct *work)
576{
577 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
578
579 if (bio_data_dir(io->base_bio) == READ)
580 kcryptd_io_read(io);
581 else
582 kcryptd_io_write(io);
583}
584
585static void kcryptd_queue_io(struct dm_crypt_io *io)
586{
587 struct crypt_config *cc = io->target->private;
588
589 INIT_WORK(&io->work, kcryptd_io);
590 queue_work(cc->io_queue, &io->work);
591}
592
4e4eef64
MB
593static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int error)
594{
dec1cedf
MB
595 struct bio *clone = io->ctx.bio_out;
596 struct crypt_config *cc = io->target->private;
597
598 if (unlikely(error < 0)) {
599 crypt_free_buffer_pages(cc, clone);
600 bio_put(clone);
601 io->error = -EIO;
dec1cedf
MB
602 return;
603 }
604
605 /* crypt_convert should have filled the clone bio */
606 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
607
608 clone->bi_sector = cc->start + io->sector;
609 io->sector += bio_sectors(clone);
899c95d3
MB
610
611 atomic_inc(&io->pending);
612 generic_make_request(clone);
4e4eef64
MB
613}
614
84131db6 615static void kcryptd_crypt_write_convert_loop(struct dm_crypt_io *io)
8b004457
MB
616{
617 struct crypt_config *cc = io->target->private;
8b004457 618 struct bio *clone;
dec1cedf
MB
619 unsigned remaining = io->base_bio->bi_size;
620 int r;
8b004457 621
93e605c2
MB
622 /*
623 * The allocated buffers can be smaller than the whole bio,
624 * so repeat the whole process until all the data can be handled.
625 */
626 while (remaining) {
f97380bc 627 clone = crypt_alloc_buffer(io, remaining);
23541d2d 628 if (unlikely(!clone)) {
5742fd77 629 io->error = -ENOMEM;
23541d2d
MB
630 return;
631 }
93e605c2 632
53017030
MB
633 io->ctx.bio_out = clone;
634 io->ctx.idx_out = 0;
93e605c2 635
dec1cedf 636 remaining -= clone->bi_size;
93e605c2 637
dec1cedf 638 r = crypt_convert(cc, &io->ctx);
f97380bc 639
dec1cedf
MB
640 kcryptd_crypt_write_io_submit(io, r);
641 if (unlikely(r < 0))
642 return;
93e605c2 643
93e605c2 644 /* out of memory -> run queues */
dec1cedf 645 if (unlikely(remaining))
98221eb7 646 congestion_wait(WRITE, HZ/100);
93e605c2 647 }
8b004457
MB
648}
649
84131db6
MB
650static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
651{
652 struct crypt_config *cc = io->target->private;
653
899c95d3
MB
654 /*
655 * Prevent io from disappearing until this function completes.
656 */
84131db6
MB
657 atomic_inc(&io->pending);
658
659 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, io->sector);
660 kcryptd_crypt_write_convert_loop(io);
899c95d3
MB
661
662 crypt_dec_pending(io);
84131db6
MB
663}
664
4e4eef64 665static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
5742fd77
MB
666{
667 if (unlikely(error < 0))
668 io->error = -EIO;
669
670 crypt_dec_pending(io);
671}
672
4e4eef64 673static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
8b004457
MB
674{
675 struct crypt_config *cc = io->target->private;
5742fd77 676 int r = 0;
1da177e4 677
53017030 678 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
0c395b0f 679 io->sector);
1da177e4 680
5742fd77
MB
681 r = crypt_convert(cc, &io->ctx);
682
4e4eef64 683 kcryptd_crypt_read_done(io, r);
1da177e4
LT
684}
685
395b167c 686static void kcryptd_crypt(struct work_struct *work)
1da177e4 687{
028867ac 688 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
8b004457 689
cabf08e4 690 if (bio_data_dir(io->base_bio) == READ)
395b167c 691 kcryptd_crypt_read_convert(io);
4e4eef64 692 else
395b167c 693 kcryptd_crypt_write_convert(io);
cabf08e4
MB
694}
695
395b167c 696static void kcryptd_queue_crypt(struct dm_crypt_io *io)
cabf08e4 697{
395b167c 698 struct crypt_config *cc = io->target->private;
cabf08e4 699
395b167c
AK
700 INIT_WORK(&io->work, kcryptd_crypt);
701 queue_work(cc->crypt_queue, &io->work);
1da177e4
LT
702}
703
704/*
705 * Decode key from its hex representation
706 */
707static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
708{
709 char buffer[3];
710 char *endp;
711 unsigned int i;
712
713 buffer[2] = '\0';
714
8b004457 715 for (i = 0; i < size; i++) {
1da177e4
LT
716 buffer[0] = *hex++;
717 buffer[1] = *hex++;
718
719 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
720
721 if (endp != &buffer[2])
722 return -EINVAL;
723 }
724
725 if (*hex != '\0')
726 return -EINVAL;
727
728 return 0;
729}
730
731/*
732 * Encode key into its hex representation
733 */
734static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
735{
736 unsigned int i;
737
8b004457 738 for (i = 0; i < size; i++) {
1da177e4
LT
739 sprintf(hex, "%02x", *key);
740 hex += 2;
741 key++;
742 }
743}
744
e48d4bbf
MB
745static int crypt_set_key(struct crypt_config *cc, char *key)
746{
747 unsigned key_size = strlen(key) >> 1;
748
749 if (cc->key_size && cc->key_size != key_size)
750 return -EINVAL;
751
752 cc->key_size = key_size; /* initial settings */
753
754 if ((!key_size && strcmp(key, "-")) ||
d469f841 755 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
e48d4bbf
MB
756 return -EINVAL;
757
758 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
759
760 return 0;
761}
762
763static int crypt_wipe_key(struct crypt_config *cc)
764{
765 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
766 memset(&cc->key, 0, cc->key_size * sizeof(u8));
767 return 0;
768}
769
1da177e4
LT
770/*
771 * Construct an encryption mapping:
772 * <cipher> <key> <iv_offset> <dev_path> <start>
773 */
774static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
775{
776 struct crypt_config *cc;
d1806f6a 777 struct crypto_blkcipher *tfm;
1da177e4
LT
778 char *tmp;
779 char *cipher;
780 char *chainmode;
781 char *ivmode;
782 char *ivopts;
1da177e4 783 unsigned int key_size;
4ee218cd 784 unsigned long long tmpll;
1da177e4
LT
785
786 if (argc != 5) {
72d94861 787 ti->error = "Not enough arguments";
1da177e4
LT
788 return -EINVAL;
789 }
790
791 tmp = argv[0];
792 cipher = strsep(&tmp, "-");
793 chainmode = strsep(&tmp, "-");
794 ivopts = strsep(&tmp, "-");
795 ivmode = strsep(&ivopts, ":");
796
797 if (tmp)
72d94861 798 DMWARN("Unexpected additional cipher options");
1da177e4
LT
799
800 key_size = strlen(argv[1]) >> 1;
801
e48d4bbf 802 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1da177e4
LT
803 if (cc == NULL) {
804 ti->error =
72d94861 805 "Cannot allocate transparent encryption context";
1da177e4
LT
806 return -ENOMEM;
807 }
808
e48d4bbf 809 if (crypt_set_key(cc, argv[1])) {
72d94861 810 ti->error = "Error decoding key";
636d5786 811 goto bad_cipher;
1da177e4
LT
812 }
813
814 /* Compatiblity mode for old dm-crypt cipher strings */
815 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
816 chainmode = "cbc";
817 ivmode = "plain";
818 }
819
d1806f6a
HX
820 if (strcmp(chainmode, "ecb") && !ivmode) {
821 ti->error = "This chaining mode requires an IV mechanism";
636d5786 822 goto bad_cipher;
1da177e4
LT
823 }
824
d469f841
MB
825 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
826 chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
d1806f6a 827 ti->error = "Chain mode + cipher name is too long";
636d5786 828 goto bad_cipher;
1da177e4
LT
829 }
830
d1806f6a
HX
831 tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
832 if (IS_ERR(tfm)) {
72d94861 833 ti->error = "Error allocating crypto tfm";
636d5786 834 goto bad_cipher;
1da177e4 835 }
1da177e4 836
d1806f6a
HX
837 strcpy(cc->cipher, cipher);
838 strcpy(cc->chainmode, chainmode);
1da177e4
LT
839 cc->tfm = tfm;
840
841 /*
48527fa7 842 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
1da177e4
LT
843 * See comments at iv code
844 */
845
846 if (ivmode == NULL)
847 cc->iv_gen_ops = NULL;
848 else if (strcmp(ivmode, "plain") == 0)
849 cc->iv_gen_ops = &crypt_iv_plain_ops;
850 else if (strcmp(ivmode, "essiv") == 0)
851 cc->iv_gen_ops = &crypt_iv_essiv_ops;
48527fa7
RS
852 else if (strcmp(ivmode, "benbi") == 0)
853 cc->iv_gen_ops = &crypt_iv_benbi_ops;
46b47730
LN
854 else if (strcmp(ivmode, "null") == 0)
855 cc->iv_gen_ops = &crypt_iv_null_ops;
1da177e4 856 else {
72d94861 857 ti->error = "Invalid IV mode";
636d5786 858 goto bad_ivmode;
1da177e4
LT
859 }
860
861 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
862 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
636d5786 863 goto bad_ivmode;
1da177e4 864
d1806f6a
HX
865 cc->iv_size = crypto_blkcipher_ivsize(tfm);
866 if (cc->iv_size)
1da177e4 867 /* at least a 64 bit sector number should fit in our buffer */
d1806f6a 868 cc->iv_size = max(cc->iv_size,
d469f841 869 (unsigned int)(sizeof(u64) / sizeof(u8)));
1da177e4 870 else {
1da177e4 871 if (cc->iv_gen_ops) {
72d94861 872 DMWARN("Selected cipher does not support IVs");
1da177e4
LT
873 if (cc->iv_gen_ops->dtr)
874 cc->iv_gen_ops->dtr(cc);
875 cc->iv_gen_ops = NULL;
876 }
877 }
878
93d2341c 879 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
1da177e4 880 if (!cc->io_pool) {
72d94861 881 ti->error = "Cannot allocate crypt io mempool";
636d5786 882 goto bad_slab_pool;
1da177e4
LT
883 }
884
a19b27ce 885 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
1da177e4 886 if (!cc->page_pool) {
72d94861 887 ti->error = "Cannot allocate page mempool";
636d5786 888 goto bad_page_pool;
1da177e4
LT
889 }
890
5972511b 891 cc->bs = bioset_create(MIN_IOS, MIN_IOS);
6a24c718
MB
892 if (!cc->bs) {
893 ti->error = "Cannot allocate crypt bioset";
894 goto bad_bs;
895 }
896
d1806f6a 897 if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
72d94861 898 ti->error = "Error setting key";
636d5786 899 goto bad_device;
1da177e4
LT
900 }
901
4ee218cd 902 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
72d94861 903 ti->error = "Invalid iv_offset sector";
636d5786 904 goto bad_device;
1da177e4 905 }
4ee218cd 906 cc->iv_offset = tmpll;
1da177e4 907
4ee218cd 908 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
72d94861 909 ti->error = "Invalid device sector";
636d5786 910 goto bad_device;
1da177e4 911 }
4ee218cd 912 cc->start = tmpll;
1da177e4
LT
913
914 if (dm_get_device(ti, argv[3], cc->start, ti->len,
d469f841 915 dm_table_get_mode(ti->table), &cc->dev)) {
72d94861 916 ti->error = "Device lookup failed";
636d5786 917 goto bad_device;
1da177e4
LT
918 }
919
920 if (ivmode && cc->iv_gen_ops) {
921 if (ivopts)
922 *(ivopts - 1) = ':';
923 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
924 if (!cc->iv_mode) {
72d94861 925 ti->error = "Error kmallocing iv_mode string";
636d5786 926 goto bad_ivmode_string;
1da177e4
LT
927 }
928 strcpy(cc->iv_mode, ivmode);
929 } else
930 cc->iv_mode = NULL;
931
cabf08e4
MB
932 cc->io_queue = create_singlethread_workqueue("kcryptd_io");
933 if (!cc->io_queue) {
934 ti->error = "Couldn't create kcryptd io queue";
935 goto bad_io_queue;
936 }
937
938 cc->crypt_queue = create_singlethread_workqueue("kcryptd");
939 if (!cc->crypt_queue) {
9934a8be 940 ti->error = "Couldn't create kcryptd queue";
cabf08e4 941 goto bad_crypt_queue;
9934a8be
MB
942 }
943
1da177e4
LT
944 ti->private = cc;
945 return 0;
946
cabf08e4
MB
947bad_crypt_queue:
948 destroy_workqueue(cc->io_queue);
949bad_io_queue:
9934a8be 950 kfree(cc->iv_mode);
636d5786 951bad_ivmode_string:
55b42c5a 952 dm_put_device(ti, cc->dev);
636d5786 953bad_device:
6a24c718
MB
954 bioset_free(cc->bs);
955bad_bs:
1da177e4 956 mempool_destroy(cc->page_pool);
636d5786 957bad_page_pool:
1da177e4 958 mempool_destroy(cc->io_pool);
636d5786 959bad_slab_pool:
1da177e4
LT
960 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
961 cc->iv_gen_ops->dtr(cc);
636d5786 962bad_ivmode:
d1806f6a 963 crypto_free_blkcipher(tfm);
636d5786 964bad_cipher:
9d3520a3
SR
965 /* Must zero key material before freeing */
966 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
1da177e4
LT
967 kfree(cc);
968 return -EINVAL;
969}
970
971static void crypt_dtr(struct dm_target *ti)
972{
973 struct crypt_config *cc = (struct crypt_config *) ti->private;
974
cabf08e4
MB
975 destroy_workqueue(cc->io_queue);
976 destroy_workqueue(cc->crypt_queue);
80b16c19 977
6a24c718 978 bioset_free(cc->bs);
1da177e4
LT
979 mempool_destroy(cc->page_pool);
980 mempool_destroy(cc->io_pool);
981
990a8baf 982 kfree(cc->iv_mode);
1da177e4
LT
983 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
984 cc->iv_gen_ops->dtr(cc);
d1806f6a 985 crypto_free_blkcipher(cc->tfm);
1da177e4 986 dm_put_device(ti, cc->dev);
9d3520a3
SR
987
988 /* Must zero key material before freeing */
989 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
1da177e4
LT
990 kfree(cc);
991}
992
1da177e4
LT
993static int crypt_map(struct dm_target *ti, struct bio *bio,
994 union map_info *map_context)
995{
8b004457 996 struct crypt_config *cc = ti->private;
028867ac 997 struct dm_crypt_io *io;
1da177e4 998
e48d4bbf 999 io = mempool_alloc(cc->io_pool, GFP_NOIO);
1da177e4 1000 io->target = ti;
8b004457 1001 io->base_bio = bio;
0c395b0f 1002 io->sector = bio->bi_sector - ti->begin;
cabf08e4 1003 io->error = 0;
93e605c2 1004 atomic_set(&io->pending, 0);
cabf08e4
MB
1005
1006 if (bio_data_dir(io->base_bio) == READ)
1007 kcryptd_queue_io(io);
1008 else
1009 kcryptd_queue_crypt(io);
1da177e4 1010
d2a7ad29 1011 return DM_MAPIO_SUBMITTED;
1da177e4
LT
1012}
1013
1014static int crypt_status(struct dm_target *ti, status_type_t type,
1015 char *result, unsigned int maxlen)
1016{
1017 struct crypt_config *cc = (struct crypt_config *) ti->private;
1da177e4
LT
1018 unsigned int sz = 0;
1019
1020 switch (type) {
1021 case STATUSTYPE_INFO:
1022 result[0] = '\0';
1023 break;
1024
1025 case STATUSTYPE_TABLE:
1da177e4 1026 if (cc->iv_mode)
37af6560
CS
1027 DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
1028 cc->iv_mode);
1da177e4 1029 else
37af6560 1030 DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
1da177e4
LT
1031
1032 if (cc->key_size > 0) {
1033 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1034 return -ENOMEM;
1035
1036 crypt_encode_key(result + sz, cc->key, cc->key_size);
1037 sz += cc->key_size << 1;
1038 } else {
1039 if (sz >= maxlen)
1040 return -ENOMEM;
1041 result[sz++] = '-';
1042 }
1043
4ee218cd
AM
1044 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1045 cc->dev->name, (unsigned long long)cc->start);
1da177e4
LT
1046 break;
1047 }
1048 return 0;
1049}
1050
e48d4bbf
MB
1051static void crypt_postsuspend(struct dm_target *ti)
1052{
1053 struct crypt_config *cc = ti->private;
1054
1055 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1056}
1057
1058static int crypt_preresume(struct dm_target *ti)
1059{
1060 struct crypt_config *cc = ti->private;
1061
1062 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1063 DMERR("aborting resume - crypt key is not set.");
1064 return -EAGAIN;
1065 }
1066
1067 return 0;
1068}
1069
1070static void crypt_resume(struct dm_target *ti)
1071{
1072 struct crypt_config *cc = ti->private;
1073
1074 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1075}
1076
1077/* Message interface
1078 * key set <key>
1079 * key wipe
1080 */
1081static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1082{
1083 struct crypt_config *cc = ti->private;
1084
1085 if (argc < 2)
1086 goto error;
1087
1088 if (!strnicmp(argv[0], MESG_STR("key"))) {
1089 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1090 DMWARN("not suspended during key manipulation.");
1091 return -EINVAL;
1092 }
1093 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
1094 return crypt_set_key(cc, argv[2]);
1095 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
1096 return crypt_wipe_key(cc);
1097 }
1098
1099error:
1100 DMWARN("unrecognised message received.");
1101 return -EINVAL;
1102}
1103
1da177e4
LT
1104static struct target_type crypt_target = {
1105 .name = "crypt",
46b47730 1106 .version= {1, 5, 0},
1da177e4
LT
1107 .module = THIS_MODULE,
1108 .ctr = crypt_ctr,
1109 .dtr = crypt_dtr,
1110 .map = crypt_map,
1111 .status = crypt_status,
e48d4bbf
MB
1112 .postsuspend = crypt_postsuspend,
1113 .preresume = crypt_preresume,
1114 .resume = crypt_resume,
1115 .message = crypt_message,
1da177e4
LT
1116};
1117
1118static int __init dm_crypt_init(void)
1119{
1120 int r;
1121
028867ac 1122 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
1da177e4
LT
1123 if (!_crypt_io_pool)
1124 return -ENOMEM;
1125
1da177e4
LT
1126 r = dm_register_target(&crypt_target);
1127 if (r < 0) {
72d94861 1128 DMERR("register failed %d", r);
9934a8be 1129 kmem_cache_destroy(_crypt_io_pool);
1da177e4
LT
1130 }
1131
1da177e4
LT
1132 return r;
1133}
1134
1135static void __exit dm_crypt_exit(void)
1136{
1137 int r = dm_unregister_target(&crypt_target);
1138
1139 if (r < 0)
72d94861 1140 DMERR("unregister failed %d", r);
1da177e4 1141
1da177e4
LT
1142 kmem_cache_destroy(_crypt_io_pool);
1143}
1144
1145module_init(dm_crypt_init);
1146module_exit(dm_crypt_exit);
1147
1148MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1149MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1150MODULE_LICENSE("GPL");
This page took 0.390338 seconds and 5 git commands to generate.