blob: 91e1bf91769f910d6343eb408e5a133dfce2a745 [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 Broz542da312009-12-10 23:51:57 +00004 * Copyright (C) 2006-2009 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);
Milan Brozb95bf2d2009-12-10 23:51:56 +000074 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000075 int (*wipe)(struct crypt_config *cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
77};
78
Milan Broz60473592009-12-10 23:51:55 +000079struct iv_essiv_private {
80 struct crypto_cipher *tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +000081 struct crypto_hash *hash_tfm;
82 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000083};
84
85struct iv_benbi_private {
86 int shift;
87};
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Crypt: maps a linear range of a block device
91 * and encrypts / decrypts at the same time.
92 */
Milan Broze48d4bb2006-10-03 01:15:37 -070093enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct crypt_config {
95 struct dm_dev *dev;
96 sector_t start;
97
98 /*
Milan Brozddd42ed2008-02-08 02:11:07 +000099 * pool for per bio private data, crypto requests and
100 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000103 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700105 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Milan Brozcabf08e2007-10-19 22:38:58 +0100107 struct workqueue_struct *io_queue;
108 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * crypto related data
112 */
113 struct crypt_iv_operations *iv_gen_ops;
114 char *iv_mode;
Herbert Xu79066ad2006-12-05 13:41:52 -0800115 union {
Milan Broz60473592009-12-10 23:51:55 +0000116 struct iv_essiv_private essiv;
117 struct iv_benbi_private benbi;
Herbert Xu79066ad2006-12-05 13:41:52 -0800118 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 sector_t iv_offset;
120 unsigned int iv_size;
121
Milan Brozddd42ed2008-02-08 02:11:07 +0000122 /*
123 * Layout of each crypto request:
124 *
125 * struct ablkcipher_request
126 * context
127 * padding
128 * struct dm_crypt_request
129 * padding
130 * IV
131 *
132 * The padding is added so that dm_crypt_request and the IV are
133 * correctly aligned.
134 */
135 unsigned int dmreq_start;
136 struct ablkcipher_request *req;
137
Herbert Xud1806f62006-08-22 20:29:17 +1000138 char cipher[CRYPTO_MAX_ALG_NAME];
139 char chainmode[CRYPTO_MAX_ALG_NAME];
Milan Broz3a7f6c92008-02-08 02:11:14 +0000140 struct crypto_ablkcipher *tfm;
Milan Broze48d4bb2006-10-03 01:15:37 -0700141 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned int key_size;
143 u8 key[0];
144};
145
Milan Broz6a24c712006-10-03 01:15:40 -0700146#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define MIN_POOL_PAGES 32
148#define MIN_BIO_PAGES 8
149
Christoph Lametere18b8902006-12-06 20:33:20 -0800150static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100152static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000153static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Olaf Kirch027581f2007-05-09 02:32:52 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Different IV generation algorithms:
157 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000158 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200159 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000161 * essiv: "encrypted sector|salt initial vector", the sector number is
162 * encrypted with the bulk cipher using a salt as key. The salt
163 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
Rik Snel48527fa2006-09-03 08:56:39 +1000165 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
166 * (needed for LRW-32-AES and possible other narrow block modes)
167 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700168 * null: the initial vector is always zero. Provides compatibility with
169 * obsolete loop_fish2 devices. Do not use for new devices.
170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * plumb: unimplemented, see:
172 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
173 */
174
175static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
176{
177 memset(iv, 0, cc->iv_size);
178 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
179
180 return 0;
181}
182
Milan Brozb95bf2d2009-12-10 23:51:56 +0000183/* Initialise ESSIV - compute salt but no local memory allocations */
184static int crypt_iv_essiv_init(struct crypt_config *cc)
185{
186 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
187 struct hash_desc desc;
188 struct scatterlist sg;
189 int err;
190
191 sg_init_one(&sg, cc->key, cc->key_size);
192 desc.tfm = essiv->hash_tfm;
193 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
194
195 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
196 if (err)
197 return err;
198
199 return crypto_cipher_setkey(essiv->tfm, essiv->salt,
200 crypto_hash_digestsize(essiv->hash_tfm));
201}
202
Milan Broz542da312009-12-10 23:51:57 +0000203/* Wipe salt and reset key derived from volume key */
204static int crypt_iv_essiv_wipe(struct crypt_config *cc)
205{
206 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
207 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
208
209 memset(essiv->salt, 0, salt_size);
210
211 return crypto_cipher_setkey(essiv->tfm, essiv->salt, salt_size);
212}
213
Milan Broz60473592009-12-10 23:51:55 +0000214static void crypt_iv_essiv_dtr(struct crypt_config *cc)
215{
216 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
217
218 crypto_free_cipher(essiv->tfm);
219 essiv->tfm = NULL;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000220
221 crypto_free_hash(essiv->hash_tfm);
222 essiv->hash_tfm = NULL;
223
224 kzfree(essiv->salt);
225 essiv->salt = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100229 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Milan Broz5861f1b2009-12-10 23:51:56 +0000231 struct crypto_cipher *essiv_tfm = NULL;
232 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000233 u8 *salt = NULL;
Herbert Xud1806f62006-08-22 20:29:17 +1000234 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Milan Broz5861f1b2009-12-10 23:51:56 +0000236 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700237 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EINVAL;
239 }
240
Milan Brozb95bf2d2009-12-10 23:51:56 +0000241 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000242 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
243 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700244 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000245 err = PTR_ERR(hash_tfm);
246 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Milan Brozb95bf2d2009-12-10 23:51:56 +0000249 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000250 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700251 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000252 err = -ENOMEM;
253 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Milan Brozb95bf2d2009-12-10 23:51:56 +0000256 /* Allocate essiv_tfm */
Herbert Xud1806f62006-08-22 20:29:17 +1000257 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
258 if (IS_ERR(essiv_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700259 ti->error = "Error allocating crypto tfm for ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000260 err = PTR_ERR(essiv_tfm);
261 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Herbert Xud1806f62006-08-22 20:29:17 +1000263 if (crypto_cipher_blocksize(essiv_tfm) !=
Milan Broz3a7f6c92008-02-08 02:11:14 +0000264 crypto_ablkcipher_ivsize(cc->tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700265 ti->error = "Block size of ESSIV cipher does "
Milan Brozd469f842007-10-19 22:42:37 +0100266 "not match IV size of block cipher";
Milan Broz5861f1b2009-12-10 23:51:56 +0000267 err = -EINVAL;
268 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Milan Brozb95bf2d2009-12-10 23:51:56 +0000271 cc->iv_gen_private.essiv.salt = salt;
Milan Broz60473592009-12-10 23:51:55 +0000272 cc->iv_gen_private.essiv.tfm = essiv_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000273 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000276
277bad:
278 if (essiv_tfm && !IS_ERR(essiv_tfm))
279 crypto_free_cipher(essiv_tfm);
280 if (hash_tfm && !IS_ERR(hash_tfm))
281 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000282 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 memset(iv, 0, cc->iv_size);
289 *(u64 *)iv = cpu_to_le64(sector);
Milan Broz60473592009-12-10 23:51:55 +0000290 crypto_cipher_encrypt_one(cc->iv_gen_private.essiv.tfm, iv, iv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
Rik Snel48527fa2006-09-03 08:56:39 +1000294static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
295 const char *opts)
296{
Milan Broz3a7f6c92008-02-08 02:11:14 +0000297 unsigned bs = crypto_ablkcipher_blocksize(cc->tfm);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800298 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000299
300 /* we need to calculate how far we must shift the sector count
301 * to get the cipher block count, we use this shift in _gen */
302
303 if (1 << log != bs) {
304 ti->error = "cypher blocksize is not a power of 2";
305 return -EINVAL;
306 }
307
308 if (log > 9) {
309 ti->error = "cypher blocksize is > 512";
310 return -EINVAL;
311 }
312
Milan Broz60473592009-12-10 23:51:55 +0000313 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000314
315 return 0;
316}
317
318static void crypt_iv_benbi_dtr(struct crypt_config *cc)
319{
Rik Snel48527fa2006-09-03 08:56:39 +1000320}
321
322static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
323{
Herbert Xu79066ad2006-12-05 13:41:52 -0800324 __be64 val;
325
Rik Snel48527fa2006-09-03 08:56:39 +1000326 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800327
Milan Broz60473592009-12-10 23:51:55 +0000328 val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800329 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332}
333
Ludwig Nussel46b47732007-05-09 02:32:55 -0700334static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
335{
336 memset(iv, 0, cc->iv_size);
337
338 return 0;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static struct crypt_iv_operations crypt_iv_plain_ops = {
342 .generator = crypt_iv_plain_gen
343};
344
345static struct crypt_iv_operations crypt_iv_essiv_ops = {
346 .ctr = crypt_iv_essiv_ctr,
347 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000348 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000349 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .generator = crypt_iv_essiv_gen
351};
352
Rik Snel48527fa2006-09-03 08:56:39 +1000353static struct crypt_iv_operations crypt_iv_benbi_ops = {
354 .ctr = crypt_iv_benbi_ctr,
355 .dtr = crypt_iv_benbi_dtr,
356 .generator = crypt_iv_benbi_gen
357};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ludwig Nussel46b47732007-05-09 02:32:55 -0700359static struct crypt_iv_operations crypt_iv_null_ops = {
360 .generator = crypt_iv_null_gen
361};
362
Milan Brozd469f842007-10-19 22:42:37 +0100363static void crypt_convert_init(struct crypt_config *cc,
364 struct convert_context *ctx,
365 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000366 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 ctx->bio_in = bio_in;
369 ctx->bio_out = bio_out;
370 ctx->offset_in = 0;
371 ctx->offset_out = 0;
372 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
373 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
374 ctx->sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000375 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Huang Yingb2174ee2009-03-16 17:44:33 +0000378static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
379 struct ablkcipher_request *req)
380{
381 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
382}
383
384static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
385 struct dm_crypt_request *dmreq)
386{
387 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
388}
389
Milan Broz01482b72008-02-08 02:11:04 +0000390static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000391 struct convert_context *ctx,
392 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000393{
394 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
395 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000396 struct dm_crypt_request *dmreq;
397 u8 *iv;
398 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000399
Huang Yingb2174ee2009-03-16 17:44:33 +0000400 dmreq = dmreq_of_req(cc, req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000401 iv = (u8 *)ALIGN((unsigned long)(dmreq + 1),
402 crypto_ablkcipher_alignmask(cc->tfm) + 1);
403
Huang Yingb2174ee2009-03-16 17:44:33 +0000404 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000405 sg_init_table(&dmreq->sg_in, 1);
406 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000407 bv_in->bv_offset + ctx->offset_in);
408
Milan Broz3a7f6c92008-02-08 02:11:14 +0000409 sg_init_table(&dmreq->sg_out, 1);
410 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000411 bv_out->bv_offset + ctx->offset_out);
412
413 ctx->offset_in += 1 << SECTOR_SHIFT;
414 if (ctx->offset_in >= bv_in->bv_len) {
415 ctx->offset_in = 0;
416 ctx->idx_in++;
417 }
418
419 ctx->offset_out += 1 << SECTOR_SHIFT;
420 if (ctx->offset_out >= bv_out->bv_len) {
421 ctx->offset_out = 0;
422 ctx->idx_out++;
423 }
424
Milan Broz3a7f6c92008-02-08 02:11:14 +0000425 if (cc->iv_gen_ops) {
426 r = cc->iv_gen_ops->generator(cc, iv, ctx->sector);
427 if (r < 0)
428 return r;
429 }
430
431 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
432 1 << SECTOR_SHIFT, iv);
433
434 if (bio_data_dir(ctx->bio_in) == WRITE)
435 r = crypto_ablkcipher_encrypt(req);
436 else
437 r = crypto_ablkcipher_decrypt(req);
438
439 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000440}
441
Milan Broz95497a92008-02-08 02:11:12 +0000442static void kcryptd_async_done(struct crypto_async_request *async_req,
443 int error);
Milan Brozddd42ed2008-02-08 02:11:07 +0000444static void crypt_alloc_req(struct crypt_config *cc,
445 struct convert_context *ctx)
446{
447 if (!cc->req)
448 cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Milan Broz95497a92008-02-08 02:11:12 +0000449 ablkcipher_request_set_tfm(cc->req, cc->tfm);
450 ablkcipher_request_set_callback(cc->req, CRYPTO_TFM_REQ_MAY_BACKLOG |
Huang Yingb2174ee2009-03-16 17:44:33 +0000451 CRYPTO_TFM_REQ_MAY_SLEEP,
452 kcryptd_async_done,
453 dmreq_of_req(cc, cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
457 * Encrypt / decrypt data from one bio to another one (can be the same one)
458 */
459static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100460 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Milan Broz3f1e9072008-03-28 14:16:07 -0700462 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Milan Brozc8081612008-10-10 13:37:08 +0100464 atomic_set(&ctx->pending, 1);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
467 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Milan Broz3a7f6c92008-02-08 02:11:14 +0000469 crypt_alloc_req(cc, ctx);
470
Milan Broz3f1e9072008-03-28 14:16:07 -0700471 atomic_inc(&ctx->pending);
472
Milan Broz3a7f6c92008-02-08 02:11:14 +0000473 r = crypt_convert_block(cc, ctx, cc->req);
474
475 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700476 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000477 case -EBUSY:
478 wait_for_completion(&ctx->restart);
479 INIT_COMPLETION(ctx->restart);
480 /* fall through*/
481 case -EINPROGRESS:
Milan Broz3a7f6c92008-02-08 02:11:14 +0000482 cc->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000483 ctx->sector++;
484 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000485
Milan Broz3f1e9072008-03-28 14:16:07 -0700486 /* sync */
487 case 0:
488 atomic_dec(&ctx->pending);
489 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100490 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700491 continue;
492
493 /* error */
494 default:
495 atomic_dec(&ctx->pending);
496 return r;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Milan Broz3f1e9072008-03-28 14:16:07 -0700500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Milan Brozd469f842007-10-19 22:42:37 +0100503static void dm_crypt_bio_destructor(struct bio *bio)
504{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100505 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700506 struct crypt_config *cc = io->target->private;
507
508 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100509}
Milan Broz6a24c712006-10-03 01:15:40 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * Generate a new unfragmented bio with the given size
513 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100514 * May return a smaller bio when running out of pages, indicated by
515 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
Milan Broz933f01d2008-10-10 13:37:08 +0100517static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
518 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Olaf Kirch027581f2007-05-09 02:32:52 -0700520 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700521 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400523 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000524 unsigned i, len;
525 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700527 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700528 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Olaf Kirch027581f2007-05-09 02:32:52 -0700531 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100532 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700533
Olaf Kirchf97380b2007-05-09 02:32:54 -0700534 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000535 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100536 if (!page) {
537 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * if additional pages cannot be allocated without waiting,
543 * return a partially allocated bio, the caller will then try
544 * to allocate additional bios while submitting this partial bio
545 */
Olaf Kirchf97380b2007-05-09 02:32:54 -0700546 if (i == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
548
Milan Broz91e10622007-12-13 14:16:10 +0000549 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Milan Broz91e10622007-12-13 14:16:10 +0000551 if (!bio_add_page(clone, page, len, 0)) {
552 mempool_free(page, cc->page_pool);
553 break;
554 }
555
556 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Milan Broz8b004452006-10-03 01:15:37 -0700559 if (!clone->bi_size) {
560 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return NULL;
562 }
563
Milan Broz8b004452006-10-03 01:15:37 -0700564 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Neil Brown644bd2f2007-10-16 13:48:46 +0200567static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Neil Brown644bd2f2007-10-16 13:48:46 +0200569 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct bio_vec *bv;
571
Neil Brown644bd2f2007-10-16 13:48:46 +0200572 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700573 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 BUG_ON(!bv->bv_page);
575 mempool_free(bv->bv_page, cc->page_pool);
576 bv->bv_page = NULL;
577 }
578}
579
Milan Brozdc440d1e2008-10-10 13:37:03 +0100580static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
581 struct bio *bio, sector_t sector)
582{
583 struct crypt_config *cc = ti->private;
584 struct dm_crypt_io *io;
585
586 io = mempool_alloc(cc->io_pool, GFP_NOIO);
587 io->target = ti;
588 io->base_bio = bio;
589 io->sector = sector;
590 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100591 io->base_io = NULL;
Milan Brozdc440d1e2008-10-10 13:37:03 +0100592 atomic_set(&io->pending, 0);
593
594 return io;
595}
596
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100597static void crypt_inc_pending(struct dm_crypt_io *io)
598{
599 atomic_inc(&io->pending);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * One of the bios was finished. Check for completion of
604 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100605 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Milan Broz5742fd72008-02-08 02:10:43 +0000607static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Milan Broz5742fd72008-02-08 02:10:43 +0000609 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000610 struct bio *base_bio = io->base_bio;
611 struct dm_crypt_io *base_io = io->base_io;
612 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (!atomic_dec_and_test(&io->pending))
615 return;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000618
619 if (likely(!base_io))
620 bio_endio(base_bio, error);
621 else {
622 if (error && !base_io->error)
623 base_io->error = error;
624 crypt_dec_pending(base_io);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100629 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700632 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100633 *
634 * kcryptd performs the actual encryption or decryption.
635 *
636 * kcryptd_io performs the IO submission.
637 *
638 * They must be separated as otherwise the final stages could be
639 * starved by new requests which can block in the first stages due
640 * to memory allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200642static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700643{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100644 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700645 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000646 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700647
Milan Brozadfe4772007-12-13 14:15:51 +0000648 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
649 error = -EIO;
650
Milan Broz8b004452006-10-03 01:15:37 -0700651 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200652 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700653 */
Milan Brozee7a4912008-02-08 02:10:46 +0000654 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200655 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000656
657 bio_put(clone);
658
659 if (rw == READ && !error) {
660 kcryptd_queue_crypt(io);
661 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200662 }
Milan Broz8b004452006-10-03 01:15:37 -0700663
Milan Brozadfe4772007-12-13 14:15:51 +0000664 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000665 io->error = error;
666
667 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700668}
669
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100670static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700671{
672 struct crypt_config *cc = io->target->private;
673
674 clone->bi_private = io;
675 clone->bi_end_io = crypt_endio;
676 clone->bi_bdev = cc->dev->bdev;
677 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700678 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700679}
680
Milan Broz4e4eef62008-02-08 02:10:49 +0000681static void kcryptd_io_read(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700682{
683 struct crypt_config *cc = io->target->private;
684 struct bio *base_bio = io->base_bio;
685 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700686
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100687 crypt_inc_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700688
689 /*
690 * The block layer might modify the bvec array, so always
691 * copy the required bvecs because we need the original
692 * one in order to decrypt the whole bio data *afterwards*.
693 */
Milan Broz6a24c712006-10-03 01:15:40 -0700694 clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
Milan Broz93e605c2006-10-03 01:15:38 -0700695 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000696 io->error = -ENOMEM;
697 crypt_dec_pending(io);
Milan Broz23541d22006-10-03 01:15:39 -0700698 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700699 }
Milan Broz8b004452006-10-03 01:15:37 -0700700
701 clone_init(io, clone);
702 clone->bi_idx = 0;
703 clone->bi_vcnt = bio_segments(base_bio);
704 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000705 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700706 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
707 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700708
Milan Broz93e605c2006-10-03 01:15:38 -0700709 generic_make_request(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700710}
711
Milan Broz4e4eef62008-02-08 02:10:49 +0000712static void kcryptd_io_write(struct dm_crypt_io *io)
713{
Milan Broz95497a92008-02-08 02:11:12 +0000714 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +0000715 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000716}
717
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000718static void kcryptd_io(struct work_struct *work)
719{
720 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
721
722 if (bio_data_dir(io->base_bio) == READ)
723 kcryptd_io_read(io);
724 else
725 kcryptd_io_write(io);
726}
727
728static void kcryptd_queue_io(struct dm_crypt_io *io)
729{
730 struct crypt_config *cc = io->target->private;
731
732 INIT_WORK(&io->work, kcryptd_io);
733 queue_work(cc->io_queue, &io->work);
734}
735
Milan Broz95497a92008-02-08 02:11:12 +0000736static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io,
737 int error, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +0000738{
Milan Brozdec1ced2008-02-08 02:10:57 +0000739 struct bio *clone = io->ctx.bio_out;
740 struct crypt_config *cc = io->target->private;
741
742 if (unlikely(error < 0)) {
743 crypt_free_buffer_pages(cc, clone);
744 bio_put(clone);
745 io->error = -EIO;
Milan Broz6c031f42008-10-10 13:37:06 +0100746 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000747 return;
748 }
749
750 /* crypt_convert should have filled the clone bio */
751 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
752
753 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +0000754
Milan Broz95497a92008-02-08 02:11:12 +0000755 if (async)
756 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +0100757 else
Milan Broz95497a92008-02-08 02:11:12 +0000758 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000759}
760
Milan Brozfc5a5e92008-10-10 13:37:04 +0100761static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700762{
763 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700764 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +0100765 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +0100766 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +0100767 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +0000768 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100769 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +0000770 int r;
Milan Broz8b004452006-10-03 01:15:37 -0700771
Milan Broz93e605c2006-10-03 01:15:38 -0700772 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +0100773 * Prevent io from disappearing until this function completes.
774 */
775 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +0100776 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +0100777
778 /*
Milan Broz93e605c2006-10-03 01:15:38 -0700779 * The allocated buffers can be smaller than the whole bio,
780 * so repeat the whole process until all the data can be handled.
781 */
782 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +0100783 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -0700784 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000785 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +0100786 break;
Milan Broz23541d22006-10-03 01:15:39 -0700787 }
Milan Broz93e605c2006-10-03 01:15:38 -0700788
Milan Broz53017032008-02-08 02:10:38 +0000789 io->ctx.bio_out = clone;
790 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -0700791
Milan Broz93e605c2006-10-03 01:15:38 -0700792 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100793 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +0000794
Milan Broz4e594092008-10-10 13:37:07 +0100795 crypt_inc_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000796 r = crypt_convert(cc, &io->ctx);
Milan Brozc8081612008-10-10 13:37:08 +0100797 crypt_finished = atomic_dec_and_test(&io->ctx.pending);
Milan Brozdec1ced2008-02-08 02:10:57 +0000798
Milan Brozc8081612008-10-10 13:37:08 +0100799 /* Encryption was already finished, submit io now */
800 if (crypt_finished) {
Milan Broz3a7f6c92008-02-08 02:11:14 +0000801 kcryptd_crypt_write_io_submit(io, r, 0);
Milan Brozc8081612008-10-10 13:37:08 +0100802
803 /*
804 * If there was an error, do not try next fragments.
805 * For async, error is processed in async handler.
806 */
Milan Broz6c031f42008-10-10 13:37:06 +0100807 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +0100808 break;
Milan Brozb635b002008-10-21 17:45:00 +0100809
810 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +0100811 }
Milan Broz93e605c2006-10-03 01:15:38 -0700812
Milan Broz933f01d2008-10-10 13:37:08 +0100813 /*
814 * Out of memory -> run queues
815 * But don't wait if split was due to the io size restriction
816 */
817 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +0200818 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +0100819
Milan Broz393b47e2008-10-21 17:45:02 +0100820 /*
821 * With async crypto it is unsafe to share the crypto context
822 * between fragments, so switch to a new dm_crypt_io structure.
823 */
824 if (unlikely(!crypt_finished && remaining)) {
825 new_io = crypt_io_alloc(io->target, io->base_bio,
826 sector);
827 crypt_inc_pending(new_io);
828 crypt_convert_init(cc, &new_io->ctx, NULL,
829 io->base_bio, sector);
830 new_io->ctx.idx_in = io->ctx.idx_in;
831 new_io->ctx.offset_in = io->ctx.offset_in;
832
833 /*
834 * Fragments after the first use the base_io
835 * pending count.
836 */
837 if (!io->base_io)
838 new_io->base_io = io;
839 else {
840 new_io->base_io = io->base_io;
841 crypt_inc_pending(io->base_io);
842 crypt_dec_pending(io);
843 }
844
845 io = new_io;
846 }
Milan Broz8b004452006-10-03 01:15:37 -0700847 }
Milan Broz899c95d2008-02-08 02:11:02 +0000848
849 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +0000850}
851
Milan Broz4e4eef62008-02-08 02:10:49 +0000852static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
Milan Broz5742fd72008-02-08 02:10:43 +0000853{
854 if (unlikely(error < 0))
855 io->error = -EIO;
856
857 crypt_dec_pending(io);
858}
859
Milan Broz4e4eef62008-02-08 02:10:49 +0000860static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700861{
862 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +0000863 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -0700864
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100865 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000866
Milan Broz53017032008-02-08 02:10:38 +0000867 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +0000868 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -0700869
Milan Broz5742fd72008-02-08 02:10:43 +0000870 r = crypt_convert(cc, &io->ctx);
871
Milan Broz3f1e9072008-03-28 14:16:07 -0700872 if (atomic_dec_and_test(&io->ctx.pending))
Milan Broz3a7f6c92008-02-08 02:11:14 +0000873 kcryptd_crypt_read_done(io, r);
874
875 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700876}
877
Milan Broz95497a92008-02-08 02:11:12 +0000878static void kcryptd_async_done(struct crypto_async_request *async_req,
879 int error)
880{
Huang Yingb2174ee2009-03-16 17:44:33 +0000881 struct dm_crypt_request *dmreq = async_req->data;
882 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +0000883 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
884 struct crypt_config *cc = io->target->private;
885
886 if (error == -EINPROGRESS) {
887 complete(&ctx->restart);
888 return;
889 }
890
Huang Yingb2174ee2009-03-16 17:44:33 +0000891 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +0000892
893 if (!atomic_dec_and_test(&ctx->pending))
894 return;
895
896 if (bio_data_dir(io->base_bio) == READ)
897 kcryptd_crypt_read_done(io, error);
898 else
899 kcryptd_crypt_write_io_submit(io, error, 1);
900}
901
Milan Broz4e4eef62008-02-08 02:10:49 +0000902static void kcryptd_crypt(struct work_struct *work)
903{
904 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
905
906 if (bio_data_dir(io->base_bio) == READ)
907 kcryptd_crypt_read_convert(io);
908 else
909 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -0700910}
911
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000912static void kcryptd_queue_crypt(struct dm_crypt_io *io)
913{
914 struct crypt_config *cc = io->target->private;
915
916 INIT_WORK(&io->work, kcryptd_crypt);
917 queue_work(cc->crypt_queue, &io->work);
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * Decode key from its hex representation
922 */
923static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
924{
925 char buffer[3];
926 char *endp;
927 unsigned int i;
928
929 buffer[2] = '\0';
930
Milan Broz8b004452006-10-03 01:15:37 -0700931 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 buffer[0] = *hex++;
933 buffer[1] = *hex++;
934
935 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
936
937 if (endp != &buffer[2])
938 return -EINVAL;
939 }
940
941 if (*hex != '\0')
942 return -EINVAL;
943
944 return 0;
945}
946
947/*
948 * Encode key into its hex representation
949 */
950static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
951{
952 unsigned int i;
953
Milan Broz8b004452006-10-03 01:15:37 -0700954 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sprintf(hex, "%02x", *key);
956 hex += 2;
957 key++;
958 }
959}
960
Milan Broze48d4bb2006-10-03 01:15:37 -0700961static int crypt_set_key(struct crypt_config *cc, char *key)
962{
963 unsigned key_size = strlen(key) >> 1;
964
965 if (cc->key_size && cc->key_size != key_size)
966 return -EINVAL;
967
968 cc->key_size = key_size; /* initial settings */
969
970 if ((!key_size && strcmp(key, "-")) ||
Milan Brozd469f842007-10-19 22:42:37 +0100971 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
Milan Broze48d4bb2006-10-03 01:15:37 -0700972 return -EINVAL;
973
974 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
975
Milan Broz0b430952009-12-10 23:51:55 +0000976 return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size);
Milan Broze48d4bb2006-10-03 01:15:37 -0700977}
978
979static int crypt_wipe_key(struct crypt_config *cc)
980{
981 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
982 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz0b430952009-12-10 23:51:55 +0000983 return crypto_ablkcipher_setkey(cc->tfm, cc->key, cc->key_size);
Milan Broze48d4bb2006-10-03 01:15:37 -0700984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * Construct an encryption mapping:
988 * <cipher> <key> <iv_offset> <dev_path> <start>
989 */
990static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
991{
992 struct crypt_config *cc;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000993 struct crypto_ablkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 char *tmp;
995 char *cipher;
996 char *chainmode;
997 char *ivmode;
998 char *ivopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned int key_size;
Andrew Morton4ee218c2006-03-27 01:17:48 -08001000 unsigned long long tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (argc != 5) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001003 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EINVAL;
1005 }
1006
1007 tmp = argv[0];
1008 cipher = strsep(&tmp, "-");
1009 chainmode = strsep(&tmp, "-");
1010 ivopts = strsep(&tmp, "-");
1011 ivmode = strsep(&ivopts, ":");
1012
1013 if (tmp)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001014 DMWARN("Unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 key_size = strlen(argv[1]) >> 1;
1017
Milan Broze48d4bb2006-10-03 01:15:37 -07001018 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (cc == NULL) {
1020 ti->error =
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001021 "Cannot allocate transparent encryption context";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -ENOMEM;
1023 }
1024
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001025 /* Compatibility mode for old dm-crypt cipher strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
1027 chainmode = "cbc";
1028 ivmode = "plain";
1029 }
1030
Herbert Xud1806f62006-08-22 20:29:17 +10001031 if (strcmp(chainmode, "ecb") && !ivmode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001032 ti->error = "This chaining mode requires an IV mechanism";
Milan Broz636d5782007-10-19 22:47:52 +01001033 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
Milan Brozd469f842007-10-19 22:42:37 +01001036 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
1037 chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
Herbert Xud1806f62006-08-22 20:29:17 +10001038 ti->error = "Chain mode + cipher name is too long";
Milan Broz636d5782007-10-19 22:47:52 +01001039 goto bad_cipher;
Herbert Xud1806f62006-08-22 20:29:17 +10001040 }
1041
Milan Broz3a7f6c92008-02-08 02:11:14 +00001042 tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0);
Herbert Xud1806f62006-08-22 20:29:17 +10001043 if (IS_ERR(tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001044 ti->error = "Error allocating crypto tfm";
Milan Broz636d5782007-10-19 22:47:52 +01001045 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Herbert Xud1806f62006-08-22 20:29:17 +10001048 strcpy(cc->cipher, cipher);
1049 strcpy(cc->chainmode, chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 cc->tfm = tfm;
1051
Milan Broz0b430952009-12-10 23:51:55 +00001052 if (crypt_set_key(cc, argv[1]) < 0) {
1053 ti->error = "Error decoding and setting key";
1054 goto bad_ivmode;
1055 }
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
Rik Snel48527fa2006-09-03 08:56:39 +10001058 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * See comments at iv code
1060 */
1061
1062 if (ivmode == NULL)
1063 cc->iv_gen_ops = NULL;
1064 else if (strcmp(ivmode, "plain") == 0)
1065 cc->iv_gen_ops = &crypt_iv_plain_ops;
1066 else if (strcmp(ivmode, "essiv") == 0)
1067 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001068 else if (strcmp(ivmode, "benbi") == 0)
1069 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001070 else if (strcmp(ivmode, "null") == 0)
1071 cc->iv_gen_ops = &crypt_iv_null_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001073 ti->error = "Invalid IV mode";
Milan Broz636d5782007-10-19 22:47:52 +01001074 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
1078 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
Milan Broz636d5782007-10-19 22:47:52 +01001079 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Milan Brozb95bf2d2009-12-10 23:51:56 +00001081 if (cc->iv_gen_ops && cc->iv_gen_ops->init &&
1082 cc->iv_gen_ops->init(cc) < 0) {
1083 ti->error = "Error initialising IV";
1084 goto bad_slab_pool;
1085 }
1086
Milan Broz3a7f6c92008-02-08 02:11:14 +00001087 cc->iv_size = crypto_ablkcipher_ivsize(tfm);
Herbert Xud1806f62006-08-22 20:29:17 +10001088 if (cc->iv_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* at least a 64 bit sector number should fit in our buffer */
Herbert Xud1806f62006-08-22 20:29:17 +10001090 cc->iv_size = max(cc->iv_size,
Milan Brozd469f842007-10-19 22:42:37 +01001091 (unsigned int)(sizeof(u64) / sizeof(u8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (cc->iv_gen_ops) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001094 DMWARN("Selected cipher does not support IVs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (cc->iv_gen_ops->dtr)
1096 cc->iv_gen_ops->dtr(cc);
1097 cc->iv_gen_ops = NULL;
1098 }
1099 }
1100
Matthew Dobson93d23412006-03-26 01:37:50 -08001101 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001103 ti->error = "Cannot allocate crypt io mempool";
Milan Broz636d5782007-10-19 22:47:52 +01001104 goto bad_slab_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Milan Brozddd42ed2008-02-08 02:11:07 +00001107 cc->dmreq_start = sizeof(struct ablkcipher_request);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001108 cc->dmreq_start += crypto_ablkcipher_reqsize(tfm);
Milan Brozddd42ed2008-02-08 02:11:07 +00001109 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Milan Broz3a7f6c92008-02-08 02:11:14 +00001110 cc->dmreq_start += crypto_ablkcipher_alignmask(tfm) &
1111 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001112
1113 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1114 sizeof(struct dm_crypt_request) + cc->iv_size);
1115 if (!cc->req_pool) {
1116 ti->error = "Cannot allocate crypt request mempool";
1117 goto bad_req_pool;
1118 }
1119 cc->req = NULL;
1120
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001121 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001123 ti->error = "Cannot allocate page mempool";
Milan Broz636d5782007-10-19 22:47:52 +01001124 goto bad_page_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Jens Axboebb799ca2008-12-10 15:35:05 +01001127 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001128 if (!cc->bs) {
1129 ti->error = "Cannot allocate crypt bioset";
1130 goto bad_bs;
1131 }
1132
Andrew Morton4ee218c2006-03-27 01:17:48 -08001133 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001134 ti->error = "Invalid iv_offset sector";
Milan Broz636d5782007-10-19 22:47:52 +01001135 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001137 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Andrew Morton4ee218c2006-03-27 01:17:48 -08001139 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001140 ti->error = "Invalid device sector";
Milan Broz636d5782007-10-19 22:47:52 +01001141 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001143 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (dm_get_device(ti, argv[3], cc->start, ti->len,
Milan Brozd469f842007-10-19 22:42:37 +01001146 dm_table_get_mode(ti->table), &cc->dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001147 ti->error = "Device lookup failed";
Milan Broz636d5782007-10-19 22:47:52 +01001148 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 if (ivmode && cc->iv_gen_ops) {
1152 if (ivopts)
1153 *(ivopts - 1) = ':';
1154 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
1155 if (!cc->iv_mode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001156 ti->error = "Error kmallocing iv_mode string";
Milan Broz636d5782007-10-19 22:47:52 +01001157 goto bad_ivmode_string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 strcpy(cc->iv_mode, ivmode);
1160 } else
1161 cc->iv_mode = NULL;
1162
Milan Brozcabf08e2007-10-19 22:38:58 +01001163 cc->io_queue = create_singlethread_workqueue("kcryptd_io");
1164 if (!cc->io_queue) {
1165 ti->error = "Couldn't create kcryptd io queue";
1166 goto bad_io_queue;
1167 }
1168
1169 cc->crypt_queue = create_singlethread_workqueue("kcryptd");
1170 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001171 ti->error = "Couldn't create kcryptd queue";
Milan Brozcabf08e2007-10-19 22:38:58 +01001172 goto bad_crypt_queue;
Milan Broz9934a8b2007-10-19 22:38:57 +01001173 }
1174
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001175 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ti->private = cc;
1177 return 0;
1178
Milan Brozcabf08e2007-10-19 22:38:58 +01001179bad_crypt_queue:
1180 destroy_workqueue(cc->io_queue);
1181bad_io_queue:
Milan Broz9934a8b2007-10-19 22:38:57 +01001182 kfree(cc->iv_mode);
Milan Broz636d5782007-10-19 22:47:52 +01001183bad_ivmode_string:
Dmitry Monakhov55b42c52007-10-19 22:38:37 +01001184 dm_put_device(ti, cc->dev);
Milan Broz636d5782007-10-19 22:47:52 +01001185bad_device:
Milan Broz6a24c712006-10-03 01:15:40 -07001186 bioset_free(cc->bs);
1187bad_bs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 mempool_destroy(cc->page_pool);
Milan Broz636d5782007-10-19 22:47:52 +01001189bad_page_pool:
Milan Brozddd42ed2008-02-08 02:11:07 +00001190 mempool_destroy(cc->req_pool);
1191bad_req_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 mempool_destroy(cc->io_pool);
Milan Broz636d5782007-10-19 22:47:52 +01001193bad_slab_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1195 cc->iv_gen_ops->dtr(cc);
Milan Broz636d5782007-10-19 22:47:52 +01001196bad_ivmode:
Milan Broz3a7f6c92008-02-08 02:11:14 +00001197 crypto_free_ablkcipher(tfm);
Milan Broz636d5782007-10-19 22:47:52 +01001198bad_cipher:
Stefan Rompf9d3520a2006-01-06 00:20:08 -08001199 /* Must zero key material before freeing */
Johannes Weinerb81d6cf2009-04-02 19:55:28 +01001200 kzfree(cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return -EINVAL;
1202}
1203
1204static void crypt_dtr(struct dm_target *ti)
1205{
1206 struct crypt_config *cc = (struct crypt_config *) ti->private;
1207
Milan Brozcabf08e2007-10-19 22:38:58 +01001208 destroy_workqueue(cc->io_queue);
1209 destroy_workqueue(cc->crypt_queue);
Milan Broz80b16c12007-07-21 04:37:27 -07001210
Milan Brozddd42ed2008-02-08 02:11:07 +00001211 if (cc->req)
1212 mempool_free(cc->req, cc->req_pool);
1213
Milan Broz6a24c712006-10-03 01:15:40 -07001214 bioset_free(cc->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 mempool_destroy(cc->page_pool);
Milan Brozddd42ed2008-02-08 02:11:07 +00001216 mempool_destroy(cc->req_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 mempool_destroy(cc->io_pool);
1218
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001219 kfree(cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1221 cc->iv_gen_ops->dtr(cc);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001222 crypto_free_ablkcipher(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 dm_put_device(ti, cc->dev);
Stefan Rompf9d3520a2006-01-06 00:20:08 -08001224
1225 /* Must zero key material before freeing */
Johannes Weinerb81d6cf2009-04-02 19:55:28 +01001226 kzfree(cc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static int crypt_map(struct dm_target *ti, struct bio *bio,
1230 union map_info *map_context)
1231{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001232 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001233 struct crypt_config *cc;
1234
1235 if (unlikely(bio_empty_barrier(bio))) {
1236 cc = ti->private;
1237 bio->bi_bdev = cc->dev->bdev;
1238 return DM_MAPIO_REMAPPED;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Milan Brozdc440d1e2008-10-10 13:37:03 +01001241 io = crypt_io_alloc(ti, bio, bio->bi_sector - ti->begin);
Milan Brozcabf08e2007-10-19 22:38:58 +01001242
1243 if (bio_data_dir(io->base_bio) == READ)
1244 kcryptd_queue_io(io);
1245 else
1246 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001248 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251static int crypt_status(struct dm_target *ti, status_type_t type,
1252 char *result, unsigned int maxlen)
1253{
1254 struct crypt_config *cc = (struct crypt_config *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned int sz = 0;
1256
1257 switch (type) {
1258 case STATUSTYPE_INFO:
1259 result[0] = '\0';
1260 break;
1261
1262 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (cc->iv_mode)
Christophe Saout37af6562006-10-30 20:39:08 +01001264 DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
1265 cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 else
Christophe Saout37af6562006-10-30 20:39:08 +01001267 DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (cc->key_size > 0) {
1270 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1271 return -ENOMEM;
1272
1273 crypt_encode_key(result + sz, cc->key, cc->key_size);
1274 sz += cc->key_size << 1;
1275 } else {
1276 if (sz >= maxlen)
1277 return -ENOMEM;
1278 result[sz++] = '-';
1279 }
1280
Andrew Morton4ee218c2006-03-27 01:17:48 -08001281 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1282 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 }
1285 return 0;
1286}
1287
Milan Broze48d4bb2006-10-03 01:15:37 -07001288static void crypt_postsuspend(struct dm_target *ti)
1289{
1290 struct crypt_config *cc = ti->private;
1291
1292 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1293}
1294
1295static int crypt_preresume(struct dm_target *ti)
1296{
1297 struct crypt_config *cc = ti->private;
1298
1299 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1300 DMERR("aborting resume - crypt key is not set.");
1301 return -EAGAIN;
1302 }
1303
1304 return 0;
1305}
1306
1307static void crypt_resume(struct dm_target *ti)
1308{
1309 struct crypt_config *cc = ti->private;
1310
1311 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1312}
1313
1314/* Message interface
1315 * key set <key>
1316 * key wipe
1317 */
1318static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1319{
1320 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001321 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001322
1323 if (argc < 2)
1324 goto error;
1325
1326 if (!strnicmp(argv[0], MESG_STR("key"))) {
1327 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1328 DMWARN("not suspended during key manipulation.");
1329 return -EINVAL;
1330 }
Milan Broz542da312009-12-10 23:51:57 +00001331 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) {
1332 ret = crypt_set_key(cc, argv[2]);
1333 if (ret)
1334 return ret;
1335 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1336 ret = cc->iv_gen_ops->init(cc);
1337 return ret;
1338 }
1339 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) {
1340 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1341 ret = cc->iv_gen_ops->wipe(cc);
1342 if (ret)
1343 return ret;
1344 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001345 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001346 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001347 }
1348
1349error:
1350 DMWARN("unrecognised message received.");
1351 return -EINVAL;
1352}
1353
Milan Brozd41e26b2008-07-21 12:00:40 +01001354static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1355 struct bio_vec *biovec, int max_size)
1356{
1357 struct crypt_config *cc = ti->private;
1358 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1359
1360 if (!q->merge_bvec_fn)
1361 return max_size;
1362
1363 bvm->bi_bdev = cc->dev->bdev;
1364 bvm->bi_sector = cc->start + bvm->bi_sector - ti->begin;
1365
1366 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1367}
1368
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001369static int crypt_iterate_devices(struct dm_target *ti,
1370 iterate_devices_callout_fn fn, void *data)
1371{
1372 struct crypt_config *cc = ti->private;
1373
Mike Snitzer5dea2712009-07-23 20:30:42 +01001374 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377static struct target_type crypt_target = {
1378 .name = "crypt",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001379 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .module = THIS_MODULE,
1381 .ctr = crypt_ctr,
1382 .dtr = crypt_dtr,
1383 .map = crypt_map,
1384 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001385 .postsuspend = crypt_postsuspend,
1386 .preresume = crypt_preresume,
1387 .resume = crypt_resume,
1388 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001389 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001390 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391};
1392
1393static int __init dm_crypt_init(void)
1394{
1395 int r;
1396
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001397 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!_crypt_io_pool)
1399 return -ENOMEM;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 r = dm_register_target(&crypt_target);
1402 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001403 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001404 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return r;
1408}
1409
1410static void __exit dm_crypt_exit(void)
1411{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001412 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 kmem_cache_destroy(_crypt_io_pool);
1414}
1415
1416module_init(dm_crypt_init);
1417module_exit(dm_crypt_exit);
1418
1419MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1420MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1421MODULE_LICENSE("GPL");