blob: bec5ac54e23e642aae83ceaf52e5b1cf32ea3ad8 [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 Broz3f1e9072008-03-28 14:16:07 -07004 * Copyright (C) 2006-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Milan Broz43d69032008-02-08 02:11:09 +00009#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100010#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/bio.h>
15#include <linux/blkdev.h>
16#include <linux/mempool.h>
17#include <linux/slab.h>
18#include <linux/crypto.h>
19#include <linux/workqueue.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070020#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100022#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100024#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Mikulas Patocka586e80e2008-10-21 17:44:59 +010026#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070029#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * context holding the current state of a multi-part conversion
33 */
34struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000035 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct bio *bio_in;
37 struct bio *bio_out;
38 unsigned int offset_in;
39 unsigned int offset_out;
40 unsigned int idx_in;
41 unsigned int idx_out;
42 sector_t sector;
Milan Broz43d69032008-02-08 02:11:09 +000043 atomic_t pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Milan Broz53017032008-02-08 02:10:38 +000046/*
47 * per bio private data
48 */
49struct dm_crypt_io {
50 struct dm_target *target;
51 struct bio *base_bio;
52 struct work_struct work;
53
54 struct convert_context ctx;
55
56 atomic_t pending;
57 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000058 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010059 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000060};
61
Milan Broz01482b72008-02-08 02:11:04 +000062struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000063 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000064 struct scatterlist sg_in;
65 struct scatterlist sg_out;
66};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068struct crypt_config;
69
70struct crypt_iv_operations {
71 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010072 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 void (*dtr)(struct crypt_config *cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
75};
76
Milan Broz60473592009-12-10 23:51:55 +000077struct iv_essiv_private {
78 struct crypto_cipher *tfm;
79};
80
81struct iv_benbi_private {
82 int shift;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Crypt: maps a linear range of a block device
87 * and encrypts / decrypts at the same time.
88 */
Milan Broze48d4bb2006-10-03 01:15:37 -070089enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Linus Torvalds1da177e2005-04-16 15:20:36 -070090struct crypt_config {
91 struct dm_dev *dev;
92 sector_t start;
93
94 /*
Milan Brozddd42ed2008-02-08 02:11:07 +000095 * pool for per bio private data, crypto requests and
96 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +000099 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700101 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Milan Brozcabf08e2007-10-19 22:38:58 +0100103 struct workqueue_struct *io_queue;
104 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /*
107 * crypto related data
108 */
109 struct crypt_iv_operations *iv_gen_ops;
110 char *iv_mode;
Herbert Xu79066ad2006-12-05 13:41:52 -0800111 union {
Milan Broz60473592009-12-10 23:51:55 +0000112 struct iv_essiv_private essiv;
113 struct iv_benbi_private benbi;
Herbert Xu79066ad2006-12-05 13:41:52 -0800114 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sector_t iv_offset;
116 unsigned int iv_size;
117
Milan Brozddd42ed2008-02-08 02:11:07 +0000118 /*
119 * Layout of each crypto request:
120 *
121 * struct ablkcipher_request
122 * context
123 * padding
124 * struct dm_crypt_request
125 * padding
126 * IV
127 *
128 * The padding is added so that dm_crypt_request and the IV are
129 * correctly aligned.
130 */
131 unsigned int dmreq_start;
132 struct ablkcipher_request *req;
133
Herbert Xud1806f62006-08-22 20:29:17 +1000134 char cipher[CRYPTO_MAX_ALG_NAME];
135 char chainmode[CRYPTO_MAX_ALG_NAME];
Milan Broz3a7f6c92008-02-08 02:11:14 +0000136 struct crypto_ablkcipher *tfm;
Milan Broze48d4bb2006-10-03 01:15:37 -0700137 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned int key_size;
139 u8 key[0];
140};
141
Milan Broz6a24c712006-10-03 01:15:40 -0700142#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define MIN_POOL_PAGES 32
144#define MIN_BIO_PAGES 8
145
Christoph Lametere18b8902006-12-06 20:33:20 -0800146static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100148static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000149static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Olaf Kirch027581f2007-05-09 02:32:52 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Different IV generation algorithms:
153 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000154 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200155 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000157 * essiv: "encrypted sector|salt initial vector", the sector number is
158 * encrypted with the bulk cipher using a salt as key. The salt
159 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
Rik Snel48527fa2006-09-03 08:56:39 +1000161 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
162 * (needed for LRW-32-AES and possible other narrow block modes)
163 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700164 * null: the initial vector is always zero. Provides compatibility with
165 * obsolete loop_fish2 devices. Do not use for new devices.
166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * plumb: unimplemented, see:
168 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
169 */
170
171static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
172{
173 memset(iv, 0, cc->iv_size);
174 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
175
176 return 0;
177}
178
Milan Broz60473592009-12-10 23:51:55 +0000179static void crypt_iv_essiv_dtr(struct crypt_config *cc)
180{
181 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
182
183 crypto_free_cipher(essiv->tfm);
184 essiv->tfm = NULL;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100188 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Herbert Xud1806f62006-08-22 20:29:17 +1000190 struct crypto_cipher *essiv_tfm;
Herbert Xu35058682006-08-24 19:10:20 +1000191 struct crypto_hash *hash_tfm;
192 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct scatterlist sg;
194 unsigned int saltsize;
195 u8 *salt;
Herbert Xud1806f62006-08-22 20:29:17 +1000196 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (opts == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700199 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
201 }
202
203 /* Hash the cipher key with the given hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000204 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
205 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700206 ti->error = "Error initializing ESSIV hash";
Herbert Xu35058682006-08-24 19:10:20 +1000207 return PTR_ERR(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Herbert Xu35058682006-08-24 19:10:20 +1000210 saltsize = crypto_hash_digestsize(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 salt = kmalloc(saltsize, GFP_KERNEL);
212 if (salt == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700213 ti->error = "Error kmallocing salt storage in ESSIV";
Herbert Xu35058682006-08-24 19:10:20 +1000214 crypto_free_hash(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -ENOMEM;
216 }
217
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700218 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xu35058682006-08-24 19:10:20 +1000219 desc.tfm = hash_tfm;
220 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
221 err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
222 crypto_free_hash(hash_tfm);
223
224 if (err) {
225 ti->error = "Error calculating hash in ESSIV";
Dmitry Monakhov815f9e32007-10-19 22:38:38 +0100226 kfree(salt);
Herbert Xu35058682006-08-24 19:10:20 +1000227 return err;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* Setup the essiv_tfm with the given salt */
Herbert Xud1806f62006-08-22 20:29:17 +1000231 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
232 if (IS_ERR(essiv_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700233 ti->error = "Error allocating crypto tfm for ESSIV";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000235 return PTR_ERR(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Herbert Xud1806f62006-08-22 20:29:17 +1000237 if (crypto_cipher_blocksize(essiv_tfm) !=
Milan Broz3a7f6c92008-02-08 02:11:14 +0000238 crypto_ablkcipher_ivsize(cc->tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700239 ti->error = "Block size of ESSIV cipher does "
Milan Brozd469f842007-10-19 22:42:37 +0100240 "not match IV size of block cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000241 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 kfree(salt);
243 return -EINVAL;
244 }
Herbert Xud1806f62006-08-22 20:29:17 +1000245 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
246 if (err) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700247 ti->error = "Failed to set key for ESSIV cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000248 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 kfree(salt);
253
Milan Broz60473592009-12-10 23:51:55 +0000254 cc->iv_gen_private.essiv.tfm = essiv_tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
259{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 memset(iv, 0, cc->iv_size);
261 *(u64 *)iv = cpu_to_le64(sector);
Milan Broz60473592009-12-10 23:51:55 +0000262 crypto_cipher_encrypt_one(cc->iv_gen_private.essiv.tfm, iv, iv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
Rik Snel48527fa2006-09-03 08:56:39 +1000266static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
267 const char *opts)
268{
Milan Broz3a7f6c92008-02-08 02:11:14 +0000269 unsigned bs = crypto_ablkcipher_blocksize(cc->tfm);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800270 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000271
272 /* we need to calculate how far we must shift the sector count
273 * to get the cipher block count, we use this shift in _gen */
274
275 if (1 << log != bs) {
276 ti->error = "cypher blocksize is not a power of 2";
277 return -EINVAL;
278 }
279
280 if (log > 9) {
281 ti->error = "cypher blocksize is > 512";
282 return -EINVAL;
283 }
284
Milan Broz60473592009-12-10 23:51:55 +0000285 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000286
287 return 0;
288}
289
290static void crypt_iv_benbi_dtr(struct crypt_config *cc)
291{
Rik Snel48527fa2006-09-03 08:56:39 +1000292}
293
294static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
295{
Herbert Xu79066ad2006-12-05 13:41:52 -0800296 __be64 val;
297
Rik Snel48527fa2006-09-03 08:56:39 +1000298 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800299
Milan Broz60473592009-12-10 23:51:55 +0000300 val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800301 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
Ludwig Nussel46b47732007-05-09 02:32:55 -0700306static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
307{
308 memset(iv, 0, cc->iv_size);
309
310 return 0;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static struct crypt_iv_operations crypt_iv_plain_ops = {
314 .generator = crypt_iv_plain_gen
315};
316
317static struct crypt_iv_operations crypt_iv_essiv_ops = {
318 .ctr = crypt_iv_essiv_ctr,
319 .dtr = crypt_iv_essiv_dtr,
320 .generator = crypt_iv_essiv_gen
321};
322
Rik Snel48527fa2006-09-03 08:56:39 +1000323static struct crypt_iv_operations crypt_iv_benbi_ops = {
324 .ctr = crypt_iv_benbi_ctr,
325 .dtr = crypt_iv_benbi_dtr,
326 .generator = crypt_iv_benbi_gen
327};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Ludwig Nussel46b47732007-05-09 02:32:55 -0700329static struct crypt_iv_operations crypt_iv_null_ops = {
330 .generator = crypt_iv_null_gen
331};
332
Milan Brozd469f842007-10-19 22:42:37 +0100333static void crypt_convert_init(struct crypt_config *cc,
334 struct convert_context *ctx,
335 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000336 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
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;
Milan Broz43d69032008-02-08 02:11:09 +0000345 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Huang Yingb2174ee2009-03-16 17:44:33 +0000348static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
349 struct ablkcipher_request *req)
350{
351 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
352}
353
354static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
355 struct dm_crypt_request *dmreq)
356{
357 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
358}
359
Milan Broz01482b72008-02-08 02:11:04 +0000360static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000361 struct convert_context *ctx,
362 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000363{
364 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
365 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000366 struct dm_crypt_request *dmreq;
367 u8 *iv;
368 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000369
Huang Yingb2174ee2009-03-16 17:44:33 +0000370 dmreq = dmreq_of_req(cc, req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000371 iv = (u8 *)ALIGN((unsigned long)(dmreq + 1),
372 crypto_ablkcipher_alignmask(cc->tfm) + 1);
373
Huang Yingb2174ee2009-03-16 17:44:33 +0000374 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000375 sg_init_table(&dmreq->sg_in, 1);
376 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000377 bv_in->bv_offset + ctx->offset_in);
378
Milan Broz3a7f6c92008-02-08 02:11:14 +0000379 sg_init_table(&dmreq->sg_out, 1);
380 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000381 bv_out->bv_offset + ctx->offset_out);
382
383 ctx->offset_in += 1 << SECTOR_SHIFT;
384 if (ctx->offset_in >= bv_in->bv_len) {
385 ctx->offset_in = 0;
386 ctx->idx_in++;
387 }
388
389 ctx->offset_out += 1 << SECTOR_SHIFT;
390 if (ctx->offset_out >= bv_out->bv_len) {
391 ctx->offset_out = 0;
392 ctx->idx_out++;
393 }
394
Milan Broz3a7f6c92008-02-08 02:11:14 +0000395 if (cc->iv_gen_ops) {
396 r = cc->iv_gen_ops->generator(cc, iv, ctx->sector);
397 if (r < 0)
398 return r;
399 }
400
401 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
402 1 << SECTOR_SHIFT, iv);
403
404 if (bio_data_dir(ctx->bio_in) == WRITE)
405 r = crypto_ablkcipher_encrypt(req);
406 else
407 r = crypto_ablkcipher_decrypt(req);
408
409 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000410}
411
Milan Broz95497a92008-02-08 02:11:12 +0000412static void kcryptd_async_done(struct crypto_async_request *async_req,
413 int error);
Milan Brozddd42ed2008-02-08 02:11:07 +0000414static void crypt_alloc_req(struct crypt_config *cc,
415 struct convert_context *ctx)
416{
417 if (!cc->req)
418 cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Milan Broz95497a92008-02-08 02:11:12 +0000419 ablkcipher_request_set_tfm(cc->req, cc->tfm);
420 ablkcipher_request_set_callback(cc->req, CRYPTO_TFM_REQ_MAY_BACKLOG |
Huang Yingb2174ee2009-03-16 17:44:33 +0000421 CRYPTO_TFM_REQ_MAY_SLEEP,
422 kcryptd_async_done,
423 dmreq_of_req(cc, cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * Encrypt / decrypt data from one bio to another one (can be the same one)
428 */
429static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100430 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Milan Broz3f1e9072008-03-28 14:16:07 -0700432 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Milan Brozc8081612008-10-10 13:37:08 +0100434 atomic_set(&ctx->pending, 1);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
437 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Milan Broz3a7f6c92008-02-08 02:11:14 +0000439 crypt_alloc_req(cc, ctx);
440
Milan Broz3f1e9072008-03-28 14:16:07 -0700441 atomic_inc(&ctx->pending);
442
Milan Broz3a7f6c92008-02-08 02:11:14 +0000443 r = crypt_convert_block(cc, ctx, cc->req);
444
445 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700446 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000447 case -EBUSY:
448 wait_for_completion(&ctx->restart);
449 INIT_COMPLETION(ctx->restart);
450 /* fall through*/
451 case -EINPROGRESS:
Milan Broz3a7f6c92008-02-08 02:11:14 +0000452 cc->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000453 ctx->sector++;
454 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000455
Milan Broz3f1e9072008-03-28 14:16:07 -0700456 /* sync */
457 case 0:
458 atomic_dec(&ctx->pending);
459 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100460 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700461 continue;
462
463 /* error */
464 default:
465 atomic_dec(&ctx->pending);
466 return r;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Milan Broz3f1e9072008-03-28 14:16:07 -0700470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Milan Brozd469f842007-10-19 22:42:37 +0100473static void dm_crypt_bio_destructor(struct bio *bio)
474{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100475 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700476 struct crypt_config *cc = io->target->private;
477
478 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100479}
Milan Broz6a24c712006-10-03 01:15:40 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Generate a new unfragmented bio with the given size
483 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100484 * May return a smaller bio when running out of pages, indicated by
485 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
Milan Broz933f01d2008-10-10 13:37:08 +0100487static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
488 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Olaf Kirch027581f2007-05-09 02:32:52 -0700490 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700491 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400493 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000494 unsigned i, len;
495 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700497 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700498 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Olaf Kirch027581f2007-05-09 02:32:52 -0700501 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100502 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700503
Olaf Kirchf97380b2007-05-09 02:32:54 -0700504 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000505 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100506 if (!page) {
507 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * if additional pages cannot be allocated without waiting,
513 * return a partially allocated bio, the caller will then try
514 * to allocate additional bios while submitting this partial bio
515 */
Olaf Kirchf97380b2007-05-09 02:32:54 -0700516 if (i == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
518
Milan Broz91e10622007-12-13 14:16:10 +0000519 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Milan Broz91e10622007-12-13 14:16:10 +0000521 if (!bio_add_page(clone, page, len, 0)) {
522 mempool_free(page, cc->page_pool);
523 break;
524 }
525
526 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Milan Broz8b004452006-10-03 01:15:37 -0700529 if (!clone->bi_size) {
530 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return NULL;
532 }
533
Milan Broz8b004452006-10-03 01:15:37 -0700534 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Neil Brown644bd2f2007-10-16 13:48:46 +0200537static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Neil Brown644bd2f2007-10-16 13:48:46 +0200539 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct bio_vec *bv;
541
Neil Brown644bd2f2007-10-16 13:48:46 +0200542 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700543 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 BUG_ON(!bv->bv_page);
545 mempool_free(bv->bv_page, cc->page_pool);
546 bv->bv_page = NULL;
547 }
548}
549
Milan Brozdc440d1e2008-10-10 13:37:03 +0100550static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
551 struct bio *bio, sector_t sector)
552{
553 struct crypt_config *cc = ti->private;
554 struct dm_crypt_io *io;
555
556 io = mempool_alloc(cc->io_pool, GFP_NOIO);
557 io->target = ti;
558 io->base_bio = bio;
559 io->sector = sector;
560 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100561 io->base_io = NULL;
Milan Brozdc440d1e2008-10-10 13:37:03 +0100562 atomic_set(&io->pending, 0);
563
564 return io;
565}
566
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100567static void crypt_inc_pending(struct dm_crypt_io *io)
568{
569 atomic_inc(&io->pending);
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * One of the bios was finished. Check for completion of
574 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100575 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Milan Broz5742fd72008-02-08 02:10:43 +0000577static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Milan Broz5742fd72008-02-08 02:10:43 +0000579 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000580 struct bio *base_bio = io->base_bio;
581 struct dm_crypt_io *base_io = io->base_io;
582 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (!atomic_dec_and_test(&io->pending))
585 return;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000588
589 if (likely(!base_io))
590 bio_endio(base_bio, error);
591 else {
592 if (error && !base_io->error)
593 base_io->error = error;
594 crypt_dec_pending(base_io);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100599 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700602 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100603 *
604 * kcryptd performs the actual encryption or decryption.
605 *
606 * kcryptd_io performs the IO submission.
607 *
608 * They must be separated as otherwise the final stages could be
609 * starved by new requests which can block in the first stages due
610 * to memory allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200612static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700613{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100614 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700615 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000616 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700617
Milan Brozadfe4772007-12-13 14:15:51 +0000618 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
619 error = -EIO;
620
Milan Broz8b004452006-10-03 01:15:37 -0700621 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200622 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700623 */
Milan Brozee7a4912008-02-08 02:10:46 +0000624 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200625 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000626
627 bio_put(clone);
628
629 if (rw == READ && !error) {
630 kcryptd_queue_crypt(io);
631 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200632 }
Milan Broz8b004452006-10-03 01:15:37 -0700633
Milan Brozadfe4772007-12-13 14:15:51 +0000634 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000635 io->error = error;
636
637 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700638}
639
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100640static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700641{
642 struct crypt_config *cc = io->target->private;
643
644 clone->bi_private = io;
645 clone->bi_end_io = crypt_endio;
646 clone->bi_bdev = cc->dev->bdev;
647 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700648 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700649}
650
Milan Broz4e4eef62008-02-08 02:10:49 +0000651static void kcryptd_io_read(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700652{
653 struct crypt_config *cc = io->target->private;
654 struct bio *base_bio = io->base_bio;
655 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700656
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100657 crypt_inc_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700658
659 /*
660 * The block layer might modify the bvec array, so always
661 * copy the required bvecs because we need the original
662 * one in order to decrypt the whole bio data *afterwards*.
663 */
Milan Broz6a24c712006-10-03 01:15:40 -0700664 clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
Milan Broz93e605c2006-10-03 01:15:38 -0700665 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000666 io->error = -ENOMEM;
667 crypt_dec_pending(io);
Milan Broz23541d22006-10-03 01:15:39 -0700668 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700669 }
Milan Broz8b004452006-10-03 01:15:37 -0700670
671 clone_init(io, clone);
672 clone->bi_idx = 0;
673 clone->bi_vcnt = bio_segments(base_bio);
674 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000675 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700676 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
677 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700678
Milan Broz93e605c2006-10-03 01:15:38 -0700679 generic_make_request(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700680}
681
Milan Broz4e4eef62008-02-08 02:10:49 +0000682static void kcryptd_io_write(struct dm_crypt_io *io)
683{
Milan Broz95497a92008-02-08 02:11:12 +0000684 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +0000685 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000686}
687
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000688static void kcryptd_io(struct work_struct *work)
689{
690 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
691
692 if (bio_data_dir(io->base_bio) == READ)
693 kcryptd_io_read(io);
694 else
695 kcryptd_io_write(io);
696}
697
698static void kcryptd_queue_io(struct dm_crypt_io *io)
699{
700 struct crypt_config *cc = io->target->private;
701
702 INIT_WORK(&io->work, kcryptd_io);
703 queue_work(cc->io_queue, &io->work);
704}
705
Milan Broz95497a92008-02-08 02:11:12 +0000706static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io,
707 int error, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +0000708{
Milan Brozdec1ced2008-02-08 02:10:57 +0000709 struct bio *clone = io->ctx.bio_out;
710 struct crypt_config *cc = io->target->private;
711
712 if (unlikely(error < 0)) {
713 crypt_free_buffer_pages(cc, clone);
714 bio_put(clone);
715 io->error = -EIO;
Milan Broz6c031f42008-10-10 13:37:06 +0100716 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000717 return;
718 }
719
720 /* crypt_convert should have filled the clone bio */
721 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
722
723 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +0000724
Milan Broz95497a92008-02-08 02:11:12 +0000725 if (async)
726 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb8e2008-10-10 13:37:05 +0100727 else
Milan Broz95497a92008-02-08 02:11:12 +0000728 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000729}
730
Milan Brozfc5a5e92008-10-10 13:37:04 +0100731static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700732{
733 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700734 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +0100735 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +0100736 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +0100737 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +0000738 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100739 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +0000740 int r;
Milan Broz8b004452006-10-03 01:15:37 -0700741
Milan Broz93e605c2006-10-03 01:15:38 -0700742 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +0100743 * Prevent io from disappearing until this function completes.
744 */
745 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +0100746 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +0100747
748 /*
Milan Broz93e605c2006-10-03 01:15:38 -0700749 * The allocated buffers can be smaller than the whole bio,
750 * so repeat the whole process until all the data can be handled.
751 */
752 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +0100753 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -0700754 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000755 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +0100756 break;
Milan Broz23541d22006-10-03 01:15:39 -0700757 }
Milan Broz93e605c2006-10-03 01:15:38 -0700758
Milan Broz53017032008-02-08 02:10:38 +0000759 io->ctx.bio_out = clone;
760 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -0700761
Milan Broz93e605c2006-10-03 01:15:38 -0700762 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100763 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +0000764
Milan Broz4e594092008-10-10 13:37:07 +0100765 crypt_inc_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000766 r = crypt_convert(cc, &io->ctx);
Milan Brozc8081612008-10-10 13:37:08 +0100767 crypt_finished = atomic_dec_and_test(&io->ctx.pending);
Milan Brozdec1ced2008-02-08 02:10:57 +0000768
Milan Brozc8081612008-10-10 13:37:08 +0100769 /* Encryption was already finished, submit io now */
770 if (crypt_finished) {
Milan Broz3a7f6c92008-02-08 02:11:14 +0000771 kcryptd_crypt_write_io_submit(io, r, 0);
Milan Brozc8081612008-10-10 13:37:08 +0100772
773 /*
774 * If there was an error, do not try next fragments.
775 * For async, error is processed in async handler.
776 */
Milan Broz6c031f42008-10-10 13:37:06 +0100777 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +0100778 break;
Milan Brozb635b002008-10-21 17:45:00 +0100779
780 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +0100781 }
Milan Broz93e605c2006-10-03 01:15:38 -0700782
Milan Broz933f01d2008-10-10 13:37:08 +0100783 /*
784 * Out of memory -> run queues
785 * But don't wait if split was due to the io size restriction
786 */
787 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +0200788 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +0100789
Milan Broz393b47e2008-10-21 17:45:02 +0100790 /*
791 * With async crypto it is unsafe to share the crypto context
792 * between fragments, so switch to a new dm_crypt_io structure.
793 */
794 if (unlikely(!crypt_finished && remaining)) {
795 new_io = crypt_io_alloc(io->target, io->base_bio,
796 sector);
797 crypt_inc_pending(new_io);
798 crypt_convert_init(cc, &new_io->ctx, NULL,
799 io->base_bio, sector);
800 new_io->ctx.idx_in = io->ctx.idx_in;
801 new_io->ctx.offset_in = io->ctx.offset_in;
802
803 /*
804 * Fragments after the first use the base_io
805 * pending count.
806 */
807 if (!io->base_io)
808 new_io->base_io = io;
809 else {
810 new_io->base_io = io->base_io;
811 crypt_inc_pending(io->base_io);
812 crypt_dec_pending(io);
813 }
814
815 io = new_io;
816 }
Milan Broz8b004452006-10-03 01:15:37 -0700817 }
Milan Broz899c95d2008-02-08 02:11:02 +0000818
819 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +0000820}
821
Milan Broz4e4eef62008-02-08 02:10:49 +0000822static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
Milan Broz5742fd72008-02-08 02:10:43 +0000823{
824 if (unlikely(error < 0))
825 io->error = -EIO;
826
827 crypt_dec_pending(io);
828}
829
Milan Broz4e4eef62008-02-08 02:10:49 +0000830static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700831{
832 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +0000833 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -0700834
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100835 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000836
Milan Broz53017032008-02-08 02:10:38 +0000837 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +0000838 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -0700839
Milan Broz5742fd72008-02-08 02:10:43 +0000840 r = crypt_convert(cc, &io->ctx);
841
Milan Broz3f1e9072008-03-28 14:16:07 -0700842 if (atomic_dec_and_test(&io->ctx.pending))
Milan Broz3a7f6c92008-02-08 02:11:14 +0000843 kcryptd_crypt_read_done(io, r);
844
845 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700846}
847
Milan Broz95497a92008-02-08 02:11:12 +0000848static void kcryptd_async_done(struct crypto_async_request *async_req,
849 int error)
850{
Huang Yingb2174ee2009-03-16 17:44:33 +0000851 struct dm_crypt_request *dmreq = async_req->data;
852 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +0000853 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
854 struct crypt_config *cc = io->target->private;
855
856 if (error == -EINPROGRESS) {
857 complete(&ctx->restart);
858 return;
859 }
860
Huang Yingb2174ee2009-03-16 17:44:33 +0000861 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +0000862
863 if (!atomic_dec_and_test(&ctx->pending))
864 return;
865
866 if (bio_data_dir(io->base_bio) == READ)
867 kcryptd_crypt_read_done(io, error);
868 else
869 kcryptd_crypt_write_io_submit(io, error, 1);
870}
871
Milan Broz4e4eef62008-02-08 02:10:49 +0000872static void kcryptd_crypt(struct work_struct *work)
873{
874 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
875
876 if (bio_data_dir(io->base_bio) == READ)
877 kcryptd_crypt_read_convert(io);
878 else
879 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -0700880}
881
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000882static void kcryptd_queue_crypt(struct dm_crypt_io *io)
883{
884 struct crypt_config *cc = io->target->private;
885
886 INIT_WORK(&io->work, kcryptd_crypt);
887 queue_work(cc->crypt_queue, &io->work);
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * Decode key from its hex representation
892 */
893static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
894{
895 char buffer[3];
896 char *endp;
897 unsigned int i;
898
899 buffer[2] = '\0';
900
Milan Broz8b004452006-10-03 01:15:37 -0700901 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 buffer[0] = *hex++;
903 buffer[1] = *hex++;
904
905 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
906
907 if (endp != &buffer[2])
908 return -EINVAL;
909 }
910
911 if (*hex != '\0')
912 return -EINVAL;
913
914 return 0;
915}
916
917/*
918 * Encode key into its hex representation
919 */
920static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
921{
922 unsigned int i;
923
Milan Broz8b004452006-10-03 01:15:37 -0700924 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 sprintf(hex, "%02x", *key);
926 hex += 2;
927 key++;
928 }
929}
930
Milan Broze48d4bb2006-10-03 01:15:37 -0700931static int crypt_set_key(struct crypt_config *cc, char *key)
932{
933 unsigned key_size = strlen(key) >> 1;
934
935 if (cc->key_size && cc->key_size != key_size)
936 return -EINVAL;
937
938 cc->key_size = key_size; /* initial settings */
939
940 if ((!key_size && strcmp(key, "-")) ||
Milan Brozd469f842007-10-19 22:42:37 +0100941 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
Milan Broze48d4bb2006-10-03 01:15:37 -0700942 return -EINVAL;
943
944 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
945
Milan Broz0b430952009-12-10 23:51:55 +0000946 return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size);
Milan Broze48d4bb2006-10-03 01:15:37 -0700947}
948
949static int crypt_wipe_key(struct crypt_config *cc)
950{
951 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
952 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz0b430952009-12-10 23:51:55 +0000953 return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size);
Milan Broze48d4bb2006-10-03 01:15:37 -0700954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/*
957 * Construct an encryption mapping:
958 * <cipher> <key> <iv_offset> <dev_path> <start>
959 */
960static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
961{
962 struct crypt_config *cc;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000963 struct crypto_ablkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 char *tmp;
965 char *cipher;
966 char *chainmode;
967 char *ivmode;
968 char *ivopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned int key_size;
Andrew Morton4ee218c2006-03-27 01:17:48 -0800970 unsigned long long tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (argc != 5) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700973 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -EINVAL;
975 }
976
977 tmp = argv[0];
978 cipher = strsep(&tmp, "-");
979 chainmode = strsep(&tmp, "-");
980 ivopts = strsep(&tmp, "-");
981 ivmode = strsep(&ivopts, ":");
982
983 if (tmp)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700984 DMWARN("Unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 key_size = strlen(argv[1]) >> 1;
987
Milan Broze48d4bb2006-10-03 01:15:37 -0700988 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (cc == NULL) {
990 ti->error =
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700991 "Cannot allocate transparent encryption context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENOMEM;
993 }
994
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400995 /* Compatibility mode for old dm-crypt cipher strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
997 chainmode = "cbc";
998 ivmode = "plain";
999 }
1000
Herbert Xud1806f62006-08-22 20:29:17 +10001001 if (strcmp(chainmode, "ecb") && !ivmode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001002 ti->error = "This chaining mode requires an IV mechanism";
Milan Broz636d5782007-10-19 22:47:52 +01001003 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Milan Brozd469f842007-10-19 22:42:37 +01001006 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
1007 chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
Herbert Xud1806f62006-08-22 20:29:17 +10001008 ti->error = "Chain mode + cipher name is too long";
Milan Broz636d5782007-10-19 22:47:52 +01001009 goto bad_cipher;
Herbert Xud1806f62006-08-22 20:29:17 +10001010 }
1011
Milan Broz3a7f6c92008-02-08 02:11:14 +00001012 tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0);
Herbert Xud1806f62006-08-22 20:29:17 +10001013 if (IS_ERR(tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001014 ti->error = "Error allocating crypto tfm";
Milan Broz636d5782007-10-19 22:47:52 +01001015 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Herbert Xud1806f62006-08-22 20:29:17 +10001018 strcpy(cc->cipher, cipher);
1019 strcpy(cc->chainmode, chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 cc->tfm = tfm;
1021
Milan Broz0b430952009-12-10 23:51:55 +00001022 if (crypt_set_key(cc, argv[1]) < 0) {
1023 ti->error = "Error decoding and setting key";
1024 goto bad_ivmode;
1025 }
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
Rik Snel48527fa2006-09-03 08:56:39 +10001028 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * See comments at iv code
1030 */
1031
1032 if (ivmode == NULL)
1033 cc->iv_gen_ops = NULL;
1034 else if (strcmp(ivmode, "plain") == 0)
1035 cc->iv_gen_ops = &crypt_iv_plain_ops;
1036 else if (strcmp(ivmode, "essiv") == 0)
1037 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001038 else if (strcmp(ivmode, "benbi") == 0)
1039 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001040 else if (strcmp(ivmode, "null") == 0)
1041 cc->iv_gen_ops = &crypt_iv_null_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001043 ti->error = "Invalid IV mode";
Milan Broz636d5782007-10-19 22:47:52 +01001044 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
1047 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
1048 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
Milan Broz636d5782007-10-19 22:47:52 +01001049 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Milan Broz3a7f6c92008-02-08 02:11:14 +00001051 cc->iv_size = crypto_ablkcipher_ivsize(tfm);
Herbert Xud1806f62006-08-22 20:29:17 +10001052 if (cc->iv_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* at least a 64 bit sector number should fit in our buffer */
Herbert Xud1806f62006-08-22 20:29:17 +10001054 cc->iv_size = max(cc->iv_size,
Milan Brozd469f842007-10-19 22:42:37 +01001055 (unsigned int)(sizeof(u64) / sizeof(u8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (cc->iv_gen_ops) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001058 DMWARN("Selected cipher does not support IVs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (cc->iv_gen_ops->dtr)
1060 cc->iv_gen_ops->dtr(cc);
1061 cc->iv_gen_ops = NULL;
1062 }
1063 }
1064
Matthew Dobson93d23412006-03-26 01:37:50 -08001065 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001067 ti->error = "Cannot allocate crypt io mempool";
Milan Broz636d5782007-10-19 22:47:52 +01001068 goto bad_slab_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Milan Brozddd42ed2008-02-08 02:11:07 +00001071 cc->dmreq_start = sizeof(struct ablkcipher_request);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001072 cc->dmreq_start += crypto_ablkcipher_reqsize(tfm);
Milan Brozddd42ed2008-02-08 02:11:07 +00001073 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Milan Broz3a7f6c92008-02-08 02:11:14 +00001074 cc->dmreq_start += crypto_ablkcipher_alignmask(tfm) &
1075 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001076
1077 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1078 sizeof(struct dm_crypt_request) + cc->iv_size);
1079 if (!cc->req_pool) {
1080 ti->error = "Cannot allocate crypt request mempool";
1081 goto bad_req_pool;
1082 }
1083 cc->req = NULL;
1084
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001085 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001087 ti->error = "Cannot allocate page mempool";
Milan Broz636d5782007-10-19 22:47:52 +01001088 goto bad_page_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Jens Axboebb799ca2008-12-10 15:35:05 +01001091 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001092 if (!cc->bs) {
1093 ti->error = "Cannot allocate crypt bioset";
1094 goto bad_bs;
1095 }
1096
Andrew Morton4ee218c2006-03-27 01:17:48 -08001097 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001098 ti->error = "Invalid iv_offset sector";
Milan Broz636d5782007-10-19 22:47:52 +01001099 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001101 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Andrew Morton4ee218c2006-03-27 01:17:48 -08001103 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001104 ti->error = "Invalid device sector";
Milan Broz636d5782007-10-19 22:47:52 +01001105 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001107 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (dm_get_device(ti, argv[3], cc->start, ti->len,
Milan Brozd469f842007-10-19 22:42:37 +01001110 dm_table_get_mode(ti->table), &cc->dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001111 ti->error = "Device lookup failed";
Milan Broz636d5782007-10-19 22:47:52 +01001112 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
1115 if (ivmode && cc->iv_gen_ops) {
1116 if (ivopts)
1117 *(ivopts - 1) = ':';
1118 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
1119 if (!cc->iv_mode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001120 ti->error = "Error kmallocing iv_mode string";
Milan Broz636d5782007-10-19 22:47:52 +01001121 goto bad_ivmode_string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 strcpy(cc->iv_mode, ivmode);
1124 } else
1125 cc->iv_mode = NULL;
1126
Milan Brozcabf08e2007-10-19 22:38:58 +01001127 cc->io_queue = create_singlethread_workqueue("kcryptd_io");
1128 if (!cc->io_queue) {
1129 ti->error = "Couldn't create kcryptd io queue";
1130 goto bad_io_queue;
1131 }
1132
1133 cc->crypt_queue = create_singlethread_workqueue("kcryptd");
1134 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001135 ti->error = "Couldn't create kcryptd queue";
Milan Brozcabf08e2007-10-19 22:38:58 +01001136 goto bad_crypt_queue;
Milan Broz9934a8b2007-10-19 22:38:57 +01001137 }
1138
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001139 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ti->private = cc;
1141 return 0;
1142
Milan Brozcabf08e2007-10-19 22:38:58 +01001143bad_crypt_queue:
1144 destroy_workqueue(cc->io_queue);
1145bad_io_queue:
Milan Broz9934a8b2007-10-19 22:38:57 +01001146 kfree(cc->iv_mode);
Milan Broz636d5782007-10-19 22:47:52 +01001147bad_ivmode_string:
Dmitry Monakhov55b42c52007-10-19 22:38:37 +01001148 dm_put_device(ti, cc->dev);
Milan Broz636d5782007-10-19 22:47:52 +01001149bad_device:
Milan Broz6a24c712006-10-03 01:15:40 -07001150 bioset_free(cc->bs);
1151bad_bs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 mempool_destroy(cc->page_pool);
Milan Broz636d5782007-10-19 22:47:52 +01001153bad_page_pool:
Milan Brozddd42ed2008-02-08 02:11:07 +00001154 mempool_destroy(cc->req_pool);
1155bad_req_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 mempool_destroy(cc->io_pool);
Milan Broz636d5782007-10-19 22:47:52 +01001157bad_slab_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1159 cc->iv_gen_ops->dtr(cc);
Milan Broz636d5782007-10-19 22:47:52 +01001160bad_ivmode:
Milan Broz3a7f6c92008-02-08 02:11:14 +00001161 crypto_free_ablkcipher(tfm);
Milan Broz636d5782007-10-19 22:47:52 +01001162bad_cipher:
Stefan Rompf9d3520a2006-01-06 00:20:08 -08001163 /* Must zero key material before freeing */
Johannes Weinerb81d6cf2009-04-02 19:55:28 +01001164 kzfree(cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -EINVAL;
1166}
1167
1168static void crypt_dtr(struct dm_target *ti)
1169{
1170 struct crypt_config *cc = (struct crypt_config *) ti->private;
1171
Milan Brozcabf08e2007-10-19 22:38:58 +01001172 destroy_workqueue(cc->io_queue);
1173 destroy_workqueue(cc->crypt_queue);
Milan Broz80b16c12007-07-21 04:37:27 -07001174
Milan Brozddd42ed2008-02-08 02:11:07 +00001175 if (cc->req)
1176 mempool_free(cc->req, cc->req_pool);
1177
Milan Broz6a24c712006-10-03 01:15:40 -07001178 bioset_free(cc->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mempool_destroy(cc->page_pool);
Milan Brozddd42ed2008-02-08 02:11:07 +00001180 mempool_destroy(cc->req_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 mempool_destroy(cc->io_pool);
1182
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001183 kfree(cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1185 cc->iv_gen_ops->dtr(cc);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001186 crypto_free_ablkcipher(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dm_put_device(ti, cc->dev);
Stefan Rompf9d3520a2006-01-06 00:20:08 -08001188
1189 /* Must zero key material before freeing */
Johannes Weinerb81d6cf2009-04-02 19:55:28 +01001190 kzfree(cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193static int crypt_map(struct dm_target *ti, struct bio *bio,
1194 union map_info *map_context)
1195{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001196 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001197 struct crypt_config *cc;
1198
1199 if (unlikely(bio_empty_barrier(bio))) {
1200 cc = ti->private;
1201 bio->bi_bdev = cc->dev->bdev;
1202 return DM_MAPIO_REMAPPED;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Milan Brozdc440d1e2008-10-10 13:37:03 +01001205 io = crypt_io_alloc(ti, bio, bio->bi_sector - ti->begin);
Milan Brozcabf08e2007-10-19 22:38:58 +01001206
1207 if (bio_data_dir(io->base_bio) == READ)
1208 kcryptd_queue_io(io);
1209 else
1210 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001212 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static int crypt_status(struct dm_target *ti, status_type_t type,
1216 char *result, unsigned int maxlen)
1217{
1218 struct crypt_config *cc = (struct crypt_config *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned int sz = 0;
1220
1221 switch (type) {
1222 case STATUSTYPE_INFO:
1223 result[0] = '\0';
1224 break;
1225
1226 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (cc->iv_mode)
Christophe Saout37af6562006-10-30 20:39:08 +01001228 DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
1229 cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 else
Christophe Saout37af6562006-10-30 20:39:08 +01001231 DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (cc->key_size > 0) {
1234 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1235 return -ENOMEM;
1236
1237 crypt_encode_key(result + sz, cc->key, cc->key_size);
1238 sz += cc->key_size << 1;
1239 } else {
1240 if (sz >= maxlen)
1241 return -ENOMEM;
1242 result[sz++] = '-';
1243 }
1244
Andrew Morton4ee218c2006-03-27 01:17:48 -08001245 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1246 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 }
1249 return 0;
1250}
1251
Milan Broze48d4bb2006-10-03 01:15:37 -07001252static void crypt_postsuspend(struct dm_target *ti)
1253{
1254 struct crypt_config *cc = ti->private;
1255
1256 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1257}
1258
1259static int crypt_preresume(struct dm_target *ti)
1260{
1261 struct crypt_config *cc = ti->private;
1262
1263 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1264 DMERR("aborting resume - crypt key is not set.");
1265 return -EAGAIN;
1266 }
1267
1268 return 0;
1269}
1270
1271static void crypt_resume(struct dm_target *ti)
1272{
1273 struct crypt_config *cc = ti->private;
1274
1275 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1276}
1277
1278/* Message interface
1279 * key set <key>
1280 * key wipe
1281 */
1282static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1283{
1284 struct crypt_config *cc = ti->private;
1285
1286 if (argc < 2)
1287 goto error;
1288
1289 if (!strnicmp(argv[0], MESG_STR("key"))) {
1290 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1291 DMWARN("not suspended during key manipulation.");
1292 return -EINVAL;
1293 }
1294 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
1295 return crypt_set_key(cc, argv[2]);
1296 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
1297 return crypt_wipe_key(cc);
1298 }
1299
1300error:
1301 DMWARN("unrecognised message received.");
1302 return -EINVAL;
1303}
1304
Milan Brozd41e26b2008-07-21 12:00:40 +01001305static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1306 struct bio_vec *biovec, int max_size)
1307{
1308 struct crypt_config *cc = ti->private;
1309 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1310
1311 if (!q->merge_bvec_fn)
1312 return max_size;
1313
1314 bvm->bi_bdev = cc->dev->bdev;
1315 bvm->bi_sector = cc->start + bvm->bi_sector - ti->begin;
1316
1317 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1318}
1319
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001320static int crypt_iterate_devices(struct dm_target *ti,
1321 iterate_devices_callout_fn fn, void *data)
1322{
1323 struct crypt_config *cc = ti->private;
1324
Mike Snitzer5dea2712009-07-23 20:30:42 +01001325 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static struct target_type crypt_target = {
1329 .name = "crypt",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001330 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 .module = THIS_MODULE,
1332 .ctr = crypt_ctr,
1333 .dtr = crypt_dtr,
1334 .map = crypt_map,
1335 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001336 .postsuspend = crypt_postsuspend,
1337 .preresume = crypt_preresume,
1338 .resume = crypt_resume,
1339 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001340 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001341 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342};
1343
1344static int __init dm_crypt_init(void)
1345{
1346 int r;
1347
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001348 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!_crypt_io_pool)
1350 return -ENOMEM;
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 r = dm_register_target(&crypt_target);
1353 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001354 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001355 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return r;
1359}
1360
1361static void __exit dm_crypt_exit(void)
1362{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001363 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 kmem_cache_destroy(_crypt_io_pool);
1365}
1366
1367module_init(dm_crypt_init);
1368module_exit(dm_crypt_exit);
1369
1370MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1371MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1372MODULE_LICENSE("GPL");