blob: 1dc2c62200ee97b9b06cf406c7cbbd9f1eaa757d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Broze48d4bb2006-10-03 01:15:37 -07004 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Herbert Xud1806f62006-08-22 20:29:17 +10009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Andrew Morton3fcfab12006-10-19 23:28:16 -070019#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100021#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100023#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "dm.h"
26
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070028#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * per bio private data
32 */
33struct crypt_io {
34 struct dm_target *target;
Milan Broz8b004452006-10-03 01:15:37 -070035 struct bio *base_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct bio *first_clone;
37 struct work_struct work;
38 atomic_t pending;
39 int error;
Milan Broz23541d22006-10-03 01:15:39 -070040 int post_process;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
43/*
44 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
47 struct bio *bio_in;
48 struct bio *bio_out;
49 unsigned int offset_in;
50 unsigned int offset_out;
51 unsigned int idx_in;
52 unsigned int idx_out;
53 sector_t sector;
54 int write;
55};
56
57struct crypt_config;
58
59struct crypt_iv_operations {
60 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
61 const char *opts);
62 void (*dtr)(struct crypt_config *cc);
63 const char *(*status)(struct crypt_config *cc);
64 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
65};
66
67/*
68 * Crypt: maps a linear range of a block device
69 * and encrypts / decrypts at the same time.
70 */
Milan Broze48d4bb2006-10-03 01:15:37 -070071enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct crypt_config {
73 struct dm_dev *dev;
74 sector_t start;
75
76 /*
77 * pool for per bio private data and
78 * for encryption buffer pages
79 */
80 mempool_t *io_pool;
81 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -070082 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
85 * crypto related data
86 */
87 struct crypt_iv_operations *iv_gen_ops;
88 char *iv_mode;
Herbert Xu79066ad2006-12-05 13:41:52 -080089 union {
90 struct crypto_cipher *essiv_tfm;
91 int benbi_shift;
92 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 sector_t iv_offset;
94 unsigned int iv_size;
95
Herbert Xud1806f62006-08-22 20:29:17 +100096 char cipher[CRYPTO_MAX_ALG_NAME];
97 char chainmode[CRYPTO_MAX_ALG_NAME];
98 struct crypto_blkcipher *tfm;
Milan Broze48d4bb2006-10-03 01:15:37 -070099 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int key_size;
101 u8 key[0];
102};
103
Milan Broz6a24c712006-10-03 01:15:40 -0700104#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define MIN_POOL_PAGES 32
106#define MIN_BIO_PAGES 8
107
Christoph Lametere18b8902006-12-06 20:33:20 -0800108static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Olaf Kirch027581f2007-05-09 02:32:52 -0700110static void clone_init(struct crypt_io *, struct bio *);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Different IV generation algorithms:
114 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000115 * plain: the initial vector is the 32-bit little-endian version of the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * number, padded with zeros if neccessary.
117 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000118 * essiv: "encrypted sector|salt initial vector", the sector number is
119 * encrypted with the bulk cipher using a salt as key. The salt
120 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
Rik Snel48527fa2006-09-03 08:56:39 +1000122 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
123 * (needed for LRW-32-AES and possible other narrow block modes)
124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * plumb: unimplemented, see:
126 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
127 */
128
129static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
130{
131 memset(iv, 0, cc->iv_size);
132 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
133
134 return 0;
135}
136
137static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
138 const char *opts)
139{
Herbert Xud1806f62006-08-22 20:29:17 +1000140 struct crypto_cipher *essiv_tfm;
Herbert Xu35058682006-08-24 19:10:20 +1000141 struct crypto_hash *hash_tfm;
142 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct scatterlist sg;
144 unsigned int saltsize;
145 u8 *salt;
Herbert Xud1806f62006-08-22 20:29:17 +1000146 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (opts == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700149 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -EINVAL;
151 }
152
153 /* Hash the cipher key with the given hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000154 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
155 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700156 ti->error = "Error initializing ESSIV hash";
Herbert Xu35058682006-08-24 19:10:20 +1000157 return PTR_ERR(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
Herbert Xu35058682006-08-24 19:10:20 +1000160 saltsize = crypto_hash_digestsize(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 salt = kmalloc(saltsize, GFP_KERNEL);
162 if (salt == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700163 ti->error = "Error kmallocing salt storage in ESSIV";
Herbert Xu35058682006-08-24 19:10:20 +1000164 crypto_free_hash(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ENOMEM;
166 }
167
David Hardeman378f0582005-09-17 17:55:31 +1000168 sg_set_buf(&sg, cc->key, cc->key_size);
Herbert Xu35058682006-08-24 19:10:20 +1000169 desc.tfm = hash_tfm;
170 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
171 err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
172 crypto_free_hash(hash_tfm);
173
174 if (err) {
175 ti->error = "Error calculating hash in ESSIV";
176 return err;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* Setup the essiv_tfm with the given salt */
Herbert Xud1806f62006-08-22 20:29:17 +1000180 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
181 if (IS_ERR(essiv_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700182 ti->error = "Error allocating crypto tfm for ESSIV";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000184 return PTR_ERR(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Herbert Xud1806f62006-08-22 20:29:17 +1000186 if (crypto_cipher_blocksize(essiv_tfm) !=
187 crypto_blkcipher_ivsize(cc->tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700188 ti->error = "Block size of ESSIV cipher does "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 "not match IV size of block cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000190 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(salt);
192 return -EINVAL;
193 }
Herbert Xud1806f62006-08-22 20:29:17 +1000194 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
195 if (err) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700196 ti->error = "Failed to set key for ESSIV cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000197 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 kfree(salt);
202
Herbert Xu79066ad2006-12-05 13:41:52 -0800203 cc->iv_gen_private.essiv_tfm = essiv_tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
205}
206
207static void crypt_iv_essiv_dtr(struct crypt_config *cc)
208{
Herbert Xu79066ad2006-12-05 13:41:52 -0800209 crypto_free_cipher(cc->iv_gen_private.essiv_tfm);
210 cc->iv_gen_private.essiv_tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 memset(iv, 0, cc->iv_size);
216 *(u64 *)iv = cpu_to_le64(sector);
Herbert Xu79066ad2006-12-05 13:41:52 -0800217 crypto_cipher_encrypt_one(cc->iv_gen_private.essiv_tfm, iv, iv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
Rik Snel48527fa2006-09-03 08:56:39 +1000221static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
222 const char *opts)
223{
224 unsigned int bs = crypto_blkcipher_blocksize(cc->tfm);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800225 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000226
227 /* we need to calculate how far we must shift the sector count
228 * to get the cipher block count, we use this shift in _gen */
229
230 if (1 << log != bs) {
231 ti->error = "cypher blocksize is not a power of 2";
232 return -EINVAL;
233 }
234
235 if (log > 9) {
236 ti->error = "cypher blocksize is > 512";
237 return -EINVAL;
238 }
239
Herbert Xu79066ad2006-12-05 13:41:52 -0800240 cc->iv_gen_private.benbi_shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000241
242 return 0;
243}
244
245static void crypt_iv_benbi_dtr(struct crypt_config *cc)
246{
Rik Snel48527fa2006-09-03 08:56:39 +1000247}
248
249static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
250{
Herbert Xu79066ad2006-12-05 13:41:52 -0800251 __be64 val;
252
Rik Snel48527fa2006-09-03 08:56:39 +1000253 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800254
255 val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi_shift) + 1);
256 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
261static struct crypt_iv_operations crypt_iv_plain_ops = {
262 .generator = crypt_iv_plain_gen
263};
264
265static struct crypt_iv_operations crypt_iv_essiv_ops = {
266 .ctr = crypt_iv_essiv_ctr,
267 .dtr = crypt_iv_essiv_dtr,
268 .generator = crypt_iv_essiv_gen
269};
270
Rik Snel48527fa2006-09-03 08:56:39 +1000271static struct crypt_iv_operations crypt_iv_benbi_ops = {
272 .ctr = crypt_iv_benbi_ctr,
273 .dtr = crypt_iv_benbi_dtr,
274 .generator = crypt_iv_benbi_gen
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Arjan van de Ven858119e2006-01-14 13:20:43 -0800277static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
279 struct scatterlist *in, unsigned int length,
280 int write, sector_t sector)
281{
Herbert Xu45789322006-09-03 08:58:41 +1000282 u8 iv[cc->iv_size] __attribute__ ((aligned(__alignof__(u64))));
Herbert Xud1806f62006-08-22 20:29:17 +1000283 struct blkcipher_desc desc = {
284 .tfm = cc->tfm,
285 .info = iv,
286 .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
287 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int r;
289
290 if (cc->iv_gen_ops) {
291 r = cc->iv_gen_ops->generator(cc, iv, sector);
292 if (r < 0)
293 return r;
294
295 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000296 r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 else
Herbert Xud1806f62006-08-22 20:29:17 +1000298 r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000301 r = crypto_blkcipher_encrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Herbert Xud1806f62006-08-22 20:29:17 +1000303 r = crypto_blkcipher_decrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 return r;
307}
308
309static void
310crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
311 struct bio *bio_out, struct bio *bio_in,
312 sector_t sector, int write)
313{
314 ctx->bio_in = bio_in;
315 ctx->bio_out = bio_out;
316 ctx->offset_in = 0;
317 ctx->offset_out = 0;
318 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
319 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
320 ctx->sector = sector + cc->iv_offset;
321 ctx->write = write;
322}
323
324/*
325 * Encrypt / decrypt data from one bio to another one (can be the same one)
326 */
327static int crypt_convert(struct crypt_config *cc,
328 struct convert_context *ctx)
329{
330 int r = 0;
331
332 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
333 ctx->idx_out < ctx->bio_out->bi_vcnt) {
334 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
335 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
336 struct scatterlist sg_in = {
337 .page = bv_in->bv_page,
338 .offset = bv_in->bv_offset + ctx->offset_in,
339 .length = 1 << SECTOR_SHIFT
340 };
341 struct scatterlist sg_out = {
342 .page = bv_out->bv_page,
343 .offset = bv_out->bv_offset + ctx->offset_out,
344 .length = 1 << SECTOR_SHIFT
345 };
346
347 ctx->offset_in += sg_in.length;
348 if (ctx->offset_in >= bv_in->bv_len) {
349 ctx->offset_in = 0;
350 ctx->idx_in++;
351 }
352
353 ctx->offset_out += sg_out.length;
354 if (ctx->offset_out >= bv_out->bv_len) {
355 ctx->offset_out = 0;
356 ctx->idx_out++;
357 }
358
359 r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
360 ctx->write, ctx->sector);
361 if (r < 0)
362 break;
363
364 ctx->sector++;
365 }
366
367 return r;
368}
369
Milan Broz6a24c712006-10-03 01:15:40 -0700370 static void dm_crypt_bio_destructor(struct bio *bio)
371 {
372 struct crypt_io *io = bio->bi_private;
373 struct crypt_config *cc = io->target->private;
374
375 bio_free(bio, cc->bs);
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/*
379 * Generate a new unfragmented bio with the given size
380 * This should never violate the device limitations
381 * May return a smaller bio when running out of pages
382 */
383static struct bio *
Olaf Kirch027581f2007-05-09 02:32:52 -0700384crypt_alloc_buffer(struct crypt_io *io, unsigned int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct bio *base_bio, unsigned int *bio_vec_idx)
386{
Olaf Kirch027581f2007-05-09 02:32:52 -0700387 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700388 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400390 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned int i;
392
Milan Broz6a24c712006-10-03 01:15:40 -0700393 if (base_bio) {
394 clone = bio_alloc_bioset(GFP_NOIO, base_bio->bi_max_vecs, cc->bs);
395 __bio_clone(clone, base_bio);
396 } else
397 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
398
Milan Broz8b004452006-10-03 01:15:37 -0700399 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Olaf Kirch027581f2007-05-09 02:32:52 -0700402 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* if the last bio was not complete, continue where that one ended */
Milan Broz8b004452006-10-03 01:15:37 -0700405 clone->bi_idx = *bio_vec_idx;
406 clone->bi_vcnt = *bio_vec_idx;
407 clone->bi_size = 0;
408 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Milan Broz8b004452006-10-03 01:15:37 -0700410 /* clone->bi_idx pages have already been allocated */
411 size -= clone->bi_idx * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Milan Broz8b004452006-10-03 01:15:37 -0700413 for (i = clone->bi_idx; i < nr_iovecs; i++) {
414 struct bio_vec *bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
417 if (!bv->bv_page)
418 break;
419
420 /*
421 * if additional pages cannot be allocated without waiting,
422 * return a partially allocated bio, the caller will then try
423 * to allocate additional bios while submitting this partial bio
424 */
Milan Broz8b004452006-10-03 01:15:37 -0700425 if ((i - clone->bi_idx) == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
427
428 bv->bv_offset = 0;
429 if (size > PAGE_SIZE)
430 bv->bv_len = PAGE_SIZE;
431 else
432 bv->bv_len = size;
433
Milan Broz8b004452006-10-03 01:15:37 -0700434 clone->bi_size += bv->bv_len;
435 clone->bi_vcnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 size -= bv->bv_len;
437 }
438
Milan Broz8b004452006-10-03 01:15:37 -0700439 if (!clone->bi_size) {
440 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return NULL;
442 }
443
444 /*
445 * Remember the last bio_vec allocated to be able
446 * to correctly continue after the splitting.
447 */
Milan Broz8b004452006-10-03 01:15:37 -0700448 *bio_vec_idx = clone->bi_vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Milan Broz8b004452006-10-03 01:15:37 -0700450 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static void crypt_free_buffer_pages(struct crypt_config *cc,
Milan Broz8b004452006-10-03 01:15:37 -0700454 struct bio *clone, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned int i, start, end;
457 struct bio_vec *bv;
458
459 /*
460 * This is ugly, but Jens Axboe thinks that using bi_idx in the
461 * endio function is too dangerous at the moment, so I calculate the
462 * correct position using bi_vcnt and bi_size.
463 * The bv_offset and bv_len fields might already be modified but we
464 * know that we always allocated whole pages.
465 * A fix to the bi_idx issue in the kernel is in the works, so
466 * we will hopefully be able to revert to the cleaner solution soon.
467 */
Milan Broz8b004452006-10-03 01:15:37 -0700468 i = clone->bi_vcnt - 1;
469 bv = bio_iovec_idx(clone, i);
470 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - clone->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 start = end - bytes;
472
473 start >>= PAGE_SHIFT;
Milan Broz8b004452006-10-03 01:15:37 -0700474 if (!clone->bi_size)
475 end = clone->bi_vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
477 end >>= PAGE_SHIFT;
478
Milan Broz8b004452006-10-03 01:15:37 -0700479 for (i = start; i < end; i++) {
480 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 BUG_ON(!bv->bv_page);
482 mempool_free(bv->bv_page, cc->page_pool);
483 bv->bv_page = NULL;
484 }
485}
486
487/*
488 * One of the bios was finished. Check for completion of
489 * the whole request and correctly clean up the buffer.
490 */
491static void dec_pending(struct crypt_io *io, int error)
492{
493 struct crypt_config *cc = (struct crypt_config *) io->target->private;
494
495 if (error < 0)
496 io->error = error;
497
498 if (!atomic_dec_and_test(&io->pending))
499 return;
500
501 if (io->first_clone)
502 bio_put(io->first_clone);
503
Milan Broz8b004452006-10-03 01:15:37 -0700504 bio_endio(io->base_bio, io->base_bio->bi_size, io->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 mempool_free(io, cc->io_pool);
507}
508
509/*
510 * kcryptd:
511 *
512 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700513 * interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515static struct workqueue_struct *_kcryptd_workqueue;
David Howellsc4028952006-11-22 14:57:56 +0000516static void kcryptd_do_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518static void kcryptd_queue_io(struct crypt_io *io)
519{
David Howellsc4028952006-11-22 14:57:56 +0000520 INIT_WORK(&io->work, kcryptd_do_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 queue_work(_kcryptd_workqueue, &io->work);
522}
523
Milan Broz8b004452006-10-03 01:15:37 -0700524static int crypt_endio(struct bio *clone, unsigned int done, int error)
525{
526 struct crypt_io *io = clone->bi_private;
527 struct crypt_config *cc = io->target->private;
528 unsigned read_io = bio_data_dir(clone) == READ;
529
530 /*
531 * free the processed pages, even if
532 * it's only a partially completed write
533 */
534 if (!read_io)
535 crypt_free_buffer_pages(cc, clone, done);
536
Milan Broz23541d22006-10-03 01:15:39 -0700537 /* keep going - not finished yet */
Milan Broz8b004452006-10-03 01:15:37 -0700538 if (unlikely(clone->bi_size))
539 return 1;
540
Milan Broz8b004452006-10-03 01:15:37 -0700541 if (!read_io)
542 goto out;
543
544 if (unlikely(!bio_flagged(clone, BIO_UPTODATE))) {
545 error = -EIO;
546 goto out;
547 }
548
549 bio_put(clone);
Milan Broz23541d22006-10-03 01:15:39 -0700550 io->post_process = 1;
Milan Broz8b004452006-10-03 01:15:37 -0700551 kcryptd_queue_io(io);
552 return 0;
553
554out:
555 bio_put(clone);
556 dec_pending(io, error);
557 return error;
558}
559
560static void clone_init(struct crypt_io *io, struct bio *clone)
561{
562 struct crypt_config *cc = io->target->private;
563
564 clone->bi_private = io;
565 clone->bi_end_io = crypt_endio;
566 clone->bi_bdev = cc->dev->bdev;
567 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700568 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700569}
570
Milan Broz23541d22006-10-03 01:15:39 -0700571static void process_read(struct crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700572{
573 struct crypt_config *cc = io->target->private;
574 struct bio *base_bio = io->base_bio;
575 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700576 sector_t sector = base_bio->bi_sector - io->target->begin;
577
578 atomic_inc(&io->pending);
Milan Broz8b004452006-10-03 01:15:37 -0700579
580 /*
581 * The block layer might modify the bvec array, so always
582 * copy the required bvecs because we need the original
583 * one in order to decrypt the whole bio data *afterwards*.
584 */
Milan Broz6a24c712006-10-03 01:15:40 -0700585 clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
Milan Broz93e605c2006-10-03 01:15:38 -0700586 if (unlikely(!clone)) {
587 dec_pending(io, -ENOMEM);
Milan Broz23541d22006-10-03 01:15:39 -0700588 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700589 }
Milan Broz8b004452006-10-03 01:15:37 -0700590
591 clone_init(io, clone);
592 clone->bi_idx = 0;
593 clone->bi_vcnt = bio_segments(base_bio);
594 clone->bi_size = base_bio->bi_size;
Milan Broz93e605c2006-10-03 01:15:38 -0700595 clone->bi_sector = cc->start + sector;
Milan Broz8b004452006-10-03 01:15:37 -0700596 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
597 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700598
Milan Broz93e605c2006-10-03 01:15:38 -0700599 generic_make_request(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700600}
601
Milan Broz23541d22006-10-03 01:15:39 -0700602static void process_write(struct crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700603{
604 struct crypt_config *cc = io->target->private;
605 struct bio *base_bio = io->base_bio;
606 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700607 struct convert_context ctx;
608 unsigned remaining = base_bio->bi_size;
609 sector_t sector = base_bio->bi_sector - io->target->begin;
610 unsigned bvec_idx = 0;
Milan Broz8b004452006-10-03 01:15:37 -0700611
Milan Broz93e605c2006-10-03 01:15:38 -0700612 atomic_inc(&io->pending);
Milan Broz8b004452006-10-03 01:15:37 -0700613
Milan Broz93e605c2006-10-03 01:15:38 -0700614 crypt_convert_init(cc, &ctx, NULL, base_bio, sector, 1);
Milan Broz8b004452006-10-03 01:15:37 -0700615
Milan Broz93e605c2006-10-03 01:15:38 -0700616 /*
617 * The allocated buffers can be smaller than the whole bio,
618 * so repeat the whole process until all the data can be handled.
619 */
620 while (remaining) {
Olaf Kirch027581f2007-05-09 02:32:52 -0700621 clone = crypt_alloc_buffer(io, base_bio->bi_size,
Milan Broz93e605c2006-10-03 01:15:38 -0700622 io->first_clone, &bvec_idx);
Milan Broz23541d22006-10-03 01:15:39 -0700623 if (unlikely(!clone)) {
624 dec_pending(io, -ENOMEM);
625 return;
626 }
Milan Broz93e605c2006-10-03 01:15:38 -0700627
628 ctx.bio_out = clone;
629
630 if (unlikely(crypt_convert(cc, &ctx) < 0)) {
631 crypt_free_buffer_pages(cc, clone, clone->bi_size);
632 bio_put(clone);
Milan Broz23541d22006-10-03 01:15:39 -0700633 dec_pending(io, -EIO);
634 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700635 }
636
Milan Broz93e605c2006-10-03 01:15:38 -0700637 clone->bi_sector = cc->start + sector;
638
639 if (!io->first_clone) {
640 /*
641 * hold a reference to the first clone, because it
642 * holds the bio_vec array and that can't be freed
643 * before all other clones are released
644 */
645 bio_get(clone);
646 io->first_clone = clone;
647 }
648
Milan Broz93e605c2006-10-03 01:15:38 -0700649 remaining -= clone->bi_size;
650 sector += bio_sectors(clone);
651
Milan Broz23541d22006-10-03 01:15:39 -0700652 /* prevent bio_put of first_clone */
653 if (remaining)
654 atomic_inc(&io->pending);
655
Milan Broz93e605c2006-10-03 01:15:38 -0700656 generic_make_request(clone);
657
Olaf Kirch98221eb2007-05-09 02:32:52 -0700658 /* Do not reference clone after this - it
659 * may be gone already. */
660
Milan Broz93e605c2006-10-03 01:15:38 -0700661 /* out of memory -> run queues */
662 if (remaining)
Olaf Kirch98221eb2007-05-09 02:32:52 -0700663 congestion_wait(WRITE, HZ/100);
Milan Broz8b004452006-10-03 01:15:37 -0700664 }
Milan Broz8b004452006-10-03 01:15:37 -0700665}
666
667static void process_read_endio(struct crypt_io *io)
668{
669 struct crypt_config *cc = io->target->private;
670 struct convert_context ctx;
671
672 crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
673 io->base_bio->bi_sector - io->target->begin, 0);
674
675 dec_pending(io, crypt_convert(cc, &ctx));
676}
677
David Howellsc4028952006-11-22 14:57:56 +0000678static void kcryptd_do_work(struct work_struct *work)
Milan Broz8b004452006-10-03 01:15:37 -0700679{
David Howellsc4028952006-11-22 14:57:56 +0000680 struct crypt_io *io = container_of(work, struct crypt_io, work);
Milan Broz8b004452006-10-03 01:15:37 -0700681
Milan Broz23541d22006-10-03 01:15:39 -0700682 if (io->post_process)
683 process_read_endio(io);
684 else if (bio_data_dir(io->base_bio) == READ)
685 process_read(io);
686 else
687 process_write(io);
Milan Broz8b004452006-10-03 01:15:37 -0700688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
691 * Decode key from its hex representation
692 */
693static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
694{
695 char buffer[3];
696 char *endp;
697 unsigned int i;
698
699 buffer[2] = '\0';
700
Milan Broz8b004452006-10-03 01:15:37 -0700701 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 buffer[0] = *hex++;
703 buffer[1] = *hex++;
704
705 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
706
707 if (endp != &buffer[2])
708 return -EINVAL;
709 }
710
711 if (*hex != '\0')
712 return -EINVAL;
713
714 return 0;
715}
716
717/*
718 * Encode key into its hex representation
719 */
720static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
721{
722 unsigned int i;
723
Milan Broz8b004452006-10-03 01:15:37 -0700724 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 sprintf(hex, "%02x", *key);
726 hex += 2;
727 key++;
728 }
729}
730
Milan Broze48d4bb2006-10-03 01:15:37 -0700731static int crypt_set_key(struct crypt_config *cc, char *key)
732{
733 unsigned key_size = strlen(key) >> 1;
734
735 if (cc->key_size && cc->key_size != key_size)
736 return -EINVAL;
737
738 cc->key_size = key_size; /* initial settings */
739
740 if ((!key_size && strcmp(key, "-")) ||
741 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
742 return -EINVAL;
743
744 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
745
746 return 0;
747}
748
749static int crypt_wipe_key(struct crypt_config *cc)
750{
751 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
752 memset(&cc->key, 0, cc->key_size * sizeof(u8));
753 return 0;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Construct an encryption mapping:
758 * <cipher> <key> <iv_offset> <dev_path> <start>
759 */
760static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
761{
762 struct crypt_config *cc;
Herbert Xud1806f62006-08-22 20:29:17 +1000763 struct crypto_blkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 char *tmp;
765 char *cipher;
766 char *chainmode;
767 char *ivmode;
768 char *ivopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned int key_size;
Andrew Morton4ee218c2006-03-27 01:17:48 -0800770 unsigned long long tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (argc != 5) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700773 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EINVAL;
775 }
776
777 tmp = argv[0];
778 cipher = strsep(&tmp, "-");
779 chainmode = strsep(&tmp, "-");
780 ivopts = strsep(&tmp, "-");
781 ivmode = strsep(&ivopts, ":");
782
783 if (tmp)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700784 DMWARN("Unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 key_size = strlen(argv[1]) >> 1;
787
Milan Broze48d4bb2006-10-03 01:15:37 -0700788 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (cc == NULL) {
790 ti->error =
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700791 "Cannot allocate transparent encryption context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENOMEM;
793 }
794
Milan Broze48d4bb2006-10-03 01:15:37 -0700795 if (crypt_set_key(cc, argv[1])) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700796 ti->error = "Error decoding key";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto bad1;
798 }
799
800 /* Compatiblity mode for old dm-crypt cipher strings */
801 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
802 chainmode = "cbc";
803 ivmode = "plain";
804 }
805
Herbert Xud1806f62006-08-22 20:29:17 +1000806 if (strcmp(chainmode, "ecb") && !ivmode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700807 ti->error = "This chaining mode requires an IV mechanism";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto bad1;
809 }
810
Herbert Xud1806f62006-08-22 20:29:17 +1000811 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode,
812 cipher) >= CRYPTO_MAX_ALG_NAME) {
813 ti->error = "Chain mode + cipher name is too long";
814 goto bad1;
815 }
816
817 tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
818 if (IS_ERR(tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700819 ti->error = "Error allocating crypto tfm";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto bad1;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Herbert Xud1806f62006-08-22 20:29:17 +1000823 strcpy(cc->cipher, cipher);
824 strcpy(cc->chainmode, chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 cc->tfm = tfm;
826
827 /*
Rik Snel48527fa2006-09-03 08:56:39 +1000828 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * See comments at iv code
830 */
831
832 if (ivmode == NULL)
833 cc->iv_gen_ops = NULL;
834 else if (strcmp(ivmode, "plain") == 0)
835 cc->iv_gen_ops = &crypt_iv_plain_ops;
836 else if (strcmp(ivmode, "essiv") == 0)
837 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +1000838 else if (strcmp(ivmode, "benbi") == 0)
839 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700841 ti->error = "Invalid IV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto bad2;
843 }
844
845 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
846 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
847 goto bad2;
848
Herbert Xud1806f62006-08-22 20:29:17 +1000849 cc->iv_size = crypto_blkcipher_ivsize(tfm);
850 if (cc->iv_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* at least a 64 bit sector number should fit in our buffer */
Herbert Xud1806f62006-08-22 20:29:17 +1000852 cc->iv_size = max(cc->iv_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 (unsigned int)(sizeof(u64) / sizeof(u8)));
854 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (cc->iv_gen_ops) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700856 DMWARN("Selected cipher does not support IVs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (cc->iv_gen_ops->dtr)
858 cc->iv_gen_ops->dtr(cc);
859 cc->iv_gen_ops = NULL;
860 }
861 }
862
Matthew Dobson93d23412006-03-26 01:37:50 -0800863 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700865 ti->error = "Cannot allocate crypt io mempool";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto bad3;
867 }
868
Matthew Dobsona19b27c2006-03-26 01:37:45 -0800869 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700871 ti->error = "Cannot allocate page mempool";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto bad4;
873 }
874
Jens Axboe59725112007-04-02 10:06:42 +0200875 cc->bs = bioset_create(MIN_IOS, MIN_IOS);
Milan Broz6a24c712006-10-03 01:15:40 -0700876 if (!cc->bs) {
877 ti->error = "Cannot allocate crypt bioset";
878 goto bad_bs;
879 }
880
Herbert Xud1806f62006-08-22 20:29:17 +1000881 if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700882 ti->error = "Error setting key";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto bad5;
884 }
885
Andrew Morton4ee218c2006-03-27 01:17:48 -0800886 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700887 ti->error = "Invalid iv_offset sector";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto bad5;
889 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800890 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Andrew Morton4ee218c2006-03-27 01:17:48 -0800892 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700893 ti->error = "Invalid device sector";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto bad5;
895 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800896 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (dm_get_device(ti, argv[3], cc->start, ti->len,
899 dm_table_get_mode(ti->table), &cc->dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700900 ti->error = "Device lookup failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 goto bad5;
902 }
903
904 if (ivmode && cc->iv_gen_ops) {
905 if (ivopts)
906 *(ivopts - 1) = ':';
907 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
908 if (!cc->iv_mode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700909 ti->error = "Error kmallocing iv_mode string";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto bad5;
911 }
912 strcpy(cc->iv_mode, ivmode);
913 } else
914 cc->iv_mode = NULL;
915
916 ti->private = cc;
917 return 0;
918
919bad5:
Milan Broz6a24c712006-10-03 01:15:40 -0700920 bioset_free(cc->bs);
921bad_bs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 mempool_destroy(cc->page_pool);
923bad4:
924 mempool_destroy(cc->io_pool);
925bad3:
926 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
927 cc->iv_gen_ops->dtr(cc);
928bad2:
Herbert Xud1806f62006-08-22 20:29:17 +1000929 crypto_free_blkcipher(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930bad1:
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800931 /* Must zero key material before freeing */
932 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 kfree(cc);
934 return -EINVAL;
935}
936
937static void crypt_dtr(struct dm_target *ti)
938{
939 struct crypt_config *cc = (struct crypt_config *) ti->private;
940
Milan Broz6a24c712006-10-03 01:15:40 -0700941 bioset_free(cc->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 mempool_destroy(cc->page_pool);
943 mempool_destroy(cc->io_pool);
944
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700945 kfree(cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
947 cc->iv_gen_ops->dtr(cc);
Herbert Xud1806f62006-08-22 20:29:17 +1000948 crypto_free_blkcipher(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 dm_put_device(ti, cc->dev);
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800950
951 /* Must zero key material before freeing */
952 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree(cc);
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static int crypt_map(struct dm_target *ti, struct bio *bio,
957 union map_info *map_context)
958{
Milan Broz8b004452006-10-03 01:15:37 -0700959 struct crypt_config *cc = ti->private;
Milan Broze48d4bb2006-10-03 01:15:37 -0700960 struct crypt_io *io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Milan Broz9c89f8b2007-05-09 02:32:51 -0700962 if (bio_barrier(bio))
963 return -EOPNOTSUPP;
964
Milan Broze48d4bb2006-10-03 01:15:37 -0700965 io = mempool_alloc(cc->io_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 io->target = ti;
Milan Broz8b004452006-10-03 01:15:37 -0700967 io->base_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 io->first_clone = NULL;
Milan Broz23541d22006-10-03 01:15:39 -0700969 io->error = io->post_process = 0;
Milan Broz93e605c2006-10-03 01:15:38 -0700970 atomic_set(&io->pending, 0);
Milan Broz23541d22006-10-03 01:15:39 -0700971 kcryptd_queue_io(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800973 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static int crypt_status(struct dm_target *ti, status_type_t type,
977 char *result, unsigned int maxlen)
978{
979 struct crypt_config *cc = (struct crypt_config *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 unsigned int sz = 0;
981
982 switch (type) {
983 case STATUSTYPE_INFO:
984 result[0] = '\0';
985 break;
986
987 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (cc->iv_mode)
Christophe Saout37af6562006-10-30 20:39:08 +0100989 DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
990 cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 else
Christophe Saout37af6562006-10-30 20:39:08 +0100992 DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (cc->key_size > 0) {
995 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
996 return -ENOMEM;
997
998 crypt_encode_key(result + sz, cc->key, cc->key_size);
999 sz += cc->key_size << 1;
1000 } else {
1001 if (sz >= maxlen)
1002 return -ENOMEM;
1003 result[sz++] = '-';
1004 }
1005
Andrew Morton4ee218c2006-03-27 01:17:48 -08001006 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1007 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 }
1010 return 0;
1011}
1012
Milan Broze48d4bb2006-10-03 01:15:37 -07001013static void crypt_postsuspend(struct dm_target *ti)
1014{
1015 struct crypt_config *cc = ti->private;
1016
1017 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1018}
1019
1020static int crypt_preresume(struct dm_target *ti)
1021{
1022 struct crypt_config *cc = ti->private;
1023
1024 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1025 DMERR("aborting resume - crypt key is not set.");
1026 return -EAGAIN;
1027 }
1028
1029 return 0;
1030}
1031
1032static void crypt_resume(struct dm_target *ti)
1033{
1034 struct crypt_config *cc = ti->private;
1035
1036 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1037}
1038
1039/* Message interface
1040 * key set <key>
1041 * key wipe
1042 */
1043static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1044{
1045 struct crypt_config *cc = ti->private;
1046
1047 if (argc < 2)
1048 goto error;
1049
1050 if (!strnicmp(argv[0], MESG_STR("key"))) {
1051 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1052 DMWARN("not suspended during key manipulation.");
1053 return -EINVAL;
1054 }
1055 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
1056 return crypt_set_key(cc, argv[2]);
1057 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
1058 return crypt_wipe_key(cc);
1059 }
1060
1061error:
1062 DMWARN("unrecognised message received.");
1063 return -EINVAL;
1064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static struct target_type crypt_target = {
1067 .name = "crypt",
Milan Broz23541d22006-10-03 01:15:39 -07001068 .version= {1, 3, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .module = THIS_MODULE,
1070 .ctr = crypt_ctr,
1071 .dtr = crypt_dtr,
1072 .map = crypt_map,
1073 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001074 .postsuspend = crypt_postsuspend,
1075 .preresume = crypt_preresume,
1076 .resume = crypt_resume,
1077 .message = crypt_message,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078};
1079
1080static int __init dm_crypt_init(void)
1081{
1082 int r;
1083
1084 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
1085 sizeof(struct crypt_io),
1086 0, 0, NULL, NULL);
1087 if (!_crypt_io_pool)
1088 return -ENOMEM;
1089
1090 _kcryptd_workqueue = create_workqueue("kcryptd");
1091 if (!_kcryptd_workqueue) {
1092 r = -ENOMEM;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001093 DMERR("couldn't create kcryptd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto bad1;
1095 }
1096
1097 r = dm_register_target(&crypt_target);
1098 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001099 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 goto bad2;
1101 }
1102
1103 return 0;
1104
1105bad2:
1106 destroy_workqueue(_kcryptd_workqueue);
1107bad1:
1108 kmem_cache_destroy(_crypt_io_pool);
1109 return r;
1110}
1111
1112static void __exit dm_crypt_exit(void)
1113{
1114 int r = dm_unregister_target(&crypt_target);
1115
1116 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001117 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 destroy_workqueue(_kcryptd_workqueue);
1120 kmem_cache_destroy(_crypt_io_pool);
1121}
1122
1123module_init(dm_crypt_init);
1124module_exit(dm_crypt_exit);
1125
1126MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1127MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1128MODULE_LICENSE("GPL");