blob: 2da9b9536afb6e47391d127c4fe40d2d12658afe [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 Broz4e4eef62008-02-08 02:10:49 +00004 * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Herbert Xud1806f62006-08-22 20:29:17 +10009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
15#include <linux/mempool.h>
16#include <linux/slab.h>
17#include <linux/crypto.h>
18#include <linux/workqueue.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070019#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100021#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100023#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "dm.h"
26
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070028#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * context holding the current state of a multi-part conversion
32 */
33struct convert_context {
34 struct bio *bio_in;
35 struct bio *bio_out;
36 unsigned int offset_in;
37 unsigned int offset_out;
38 unsigned int idx_in;
39 unsigned int idx_out;
40 sector_t sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Milan Broz53017032008-02-08 02:10:38 +000043/*
44 * per bio private data
45 */
46struct dm_crypt_io {
47 struct dm_target *target;
48 struct bio *base_bio;
49 struct work_struct work;
50
51 struct convert_context ctx;
52
53 atomic_t pending;
54 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000055 sector_t sector;
Milan Broz53017032008-02-08 02:10:38 +000056};
57
Milan Broz01482b72008-02-08 02:11:04 +000058struct dm_crypt_request {
59 struct scatterlist sg_in;
60 struct scatterlist sg_out;
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063struct crypt_config;
64
65struct crypt_iv_operations {
66 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010067 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 void (*dtr)(struct crypt_config *cc);
69 const char *(*status)(struct crypt_config *cc);
70 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
71};
72
73/*
74 * Crypt: maps a linear range of a block device
75 * and encrypts / decrypts at the same time.
76 */
Milan Broze48d4bb2006-10-03 01:15:37 -070077enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Linus Torvalds1da177e2005-04-16 15:20:36 -070078struct crypt_config {
79 struct dm_dev *dev;
80 sector_t start;
81
82 /*
83 * pool for per bio private data and
84 * for encryption buffer pages
85 */
86 mempool_t *io_pool;
87 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -070088 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Milan Brozcabf08e2007-10-19 22:38:58 +010090 struct workqueue_struct *io_queue;
91 struct workqueue_struct *crypt_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * crypto related data
94 */
95 struct crypt_iv_operations *iv_gen_ops;
96 char *iv_mode;
Herbert Xu79066ad2006-12-05 13:41:52 -080097 union {
98 struct crypto_cipher *essiv_tfm;
99 int benbi_shift;
100 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 sector_t iv_offset;
102 unsigned int iv_size;
103
Herbert Xud1806f62006-08-22 20:29:17 +1000104 char cipher[CRYPTO_MAX_ALG_NAME];
105 char chainmode[CRYPTO_MAX_ALG_NAME];
106 struct crypto_blkcipher *tfm;
Milan Broze48d4bb2006-10-03 01:15:37 -0700107 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int key_size;
109 u8 key[0];
110};
111
Milan Broz6a24c712006-10-03 01:15:40 -0700112#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define MIN_POOL_PAGES 32
114#define MIN_BIO_PAGES 8
115
Christoph Lametere18b8902006-12-06 20:33:20 -0800116static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100118static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000119static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Olaf Kirch027581f2007-05-09 02:32:52 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Different IV generation algorithms:
123 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000124 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200125 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000127 * essiv: "encrypted sector|salt initial vector", the sector number is
128 * encrypted with the bulk cipher using a salt as key. The salt
129 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
Rik Snel48527fa2006-09-03 08:56:39 +1000131 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
132 * (needed for LRW-32-AES and possible other narrow block modes)
133 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700134 * null: the initial vector is always zero. Provides compatibility with
135 * obsolete loop_fish2 devices. Do not use for new devices.
136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * plumb: unimplemented, see:
138 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
139 */
140
141static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
142{
143 memset(iv, 0, cc->iv_size);
144 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
145
146 return 0;
147}
148
149static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100150 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Herbert Xud1806f62006-08-22 20:29:17 +1000152 struct crypto_cipher *essiv_tfm;
Herbert Xu35058682006-08-24 19:10:20 +1000153 struct crypto_hash *hash_tfm;
154 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct scatterlist sg;
156 unsigned int saltsize;
157 u8 *salt;
Herbert Xud1806f62006-08-22 20:29:17 +1000158 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (opts == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700161 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EINVAL;
163 }
164
165 /* Hash the cipher key with the given hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000166 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
167 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700168 ti->error = "Error initializing ESSIV hash";
Herbert Xu35058682006-08-24 19:10:20 +1000169 return PTR_ERR(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Herbert Xu35058682006-08-24 19:10:20 +1000172 saltsize = crypto_hash_digestsize(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 salt = kmalloc(saltsize, GFP_KERNEL);
174 if (salt == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700175 ti->error = "Error kmallocing salt storage in ESSIV";
Herbert Xu35058682006-08-24 19:10:20 +1000176 crypto_free_hash(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -ENOMEM;
178 }
179
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700180 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xu35058682006-08-24 19:10:20 +1000181 desc.tfm = hash_tfm;
182 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
183 err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
184 crypto_free_hash(hash_tfm);
185
186 if (err) {
187 ti->error = "Error calculating hash in ESSIV";
Dmitry Monakhov815f9e32007-10-19 22:38:38 +0100188 kfree(salt);
Herbert Xu35058682006-08-24 19:10:20 +1000189 return err;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* Setup the essiv_tfm with the given salt */
Herbert Xud1806f62006-08-22 20:29:17 +1000193 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
194 if (IS_ERR(essiv_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700195 ti->error = "Error allocating crypto tfm for ESSIV";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000197 return PTR_ERR(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Herbert Xud1806f62006-08-22 20:29:17 +1000199 if (crypto_cipher_blocksize(essiv_tfm) !=
200 crypto_blkcipher_ivsize(cc->tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700201 ti->error = "Block size of ESSIV cipher does "
Milan Brozd469f842007-10-19 22:42:37 +0100202 "not match IV size of block cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000203 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kfree(salt);
205 return -EINVAL;
206 }
Herbert Xud1806f62006-08-22 20:29:17 +1000207 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
208 if (err) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700209 ti->error = "Failed to set key for ESSIV cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000210 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 kfree(salt);
215
Herbert Xu79066ad2006-12-05 13:41:52 -0800216 cc->iv_gen_private.essiv_tfm = essiv_tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
220static void crypt_iv_essiv_dtr(struct crypt_config *cc)
221{
Herbert Xu79066ad2006-12-05 13:41:52 -0800222 crypto_free_cipher(cc->iv_gen_private.essiv_tfm);
223 cc->iv_gen_private.essiv_tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 memset(iv, 0, cc->iv_size);
229 *(u64 *)iv = cpu_to_le64(sector);
Herbert Xu79066ad2006-12-05 13:41:52 -0800230 crypto_cipher_encrypt_one(cc->iv_gen_private.essiv_tfm, iv, iv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Rik Snel48527fa2006-09-03 08:56:39 +1000234static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
235 const char *opts)
236{
237 unsigned int bs = crypto_blkcipher_blocksize(cc->tfm);
David Howellsf0d1b0b2006-12-08 02:37:49 -0800238 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000239
240 /* we need to calculate how far we must shift the sector count
241 * to get the cipher block count, we use this shift in _gen */
242
243 if (1 << log != bs) {
244 ti->error = "cypher blocksize is not a power of 2";
245 return -EINVAL;
246 }
247
248 if (log > 9) {
249 ti->error = "cypher blocksize is > 512";
250 return -EINVAL;
251 }
252
Herbert Xu79066ad2006-12-05 13:41:52 -0800253 cc->iv_gen_private.benbi_shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000254
255 return 0;
256}
257
258static void crypt_iv_benbi_dtr(struct crypt_config *cc)
259{
Rik Snel48527fa2006-09-03 08:56:39 +1000260}
261
262static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
263{
Herbert Xu79066ad2006-12-05 13:41:52 -0800264 __be64 val;
265
Rik Snel48527fa2006-09-03 08:56:39 +1000266 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800267
268 val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi_shift) + 1);
269 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
273
Ludwig Nussel46b47732007-05-09 02:32:55 -0700274static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
275{
276 memset(iv, 0, cc->iv_size);
277
278 return 0;
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static struct crypt_iv_operations crypt_iv_plain_ops = {
282 .generator = crypt_iv_plain_gen
283};
284
285static struct crypt_iv_operations crypt_iv_essiv_ops = {
286 .ctr = crypt_iv_essiv_ctr,
287 .dtr = crypt_iv_essiv_dtr,
288 .generator = crypt_iv_essiv_gen
289};
290
Rik Snel48527fa2006-09-03 08:56:39 +1000291static struct crypt_iv_operations crypt_iv_benbi_ops = {
292 .ctr = crypt_iv_benbi_ctr,
293 .dtr = crypt_iv_benbi_dtr,
294 .generator = crypt_iv_benbi_gen
295};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Ludwig Nussel46b47732007-05-09 02:32:55 -0700297static struct crypt_iv_operations crypt_iv_null_ops = {
298 .generator = crypt_iv_null_gen
299};
300
Arjan van de Ven858119e2006-01-14 13:20:43 -0800301static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
303 struct scatterlist *in, unsigned int length,
304 int write, sector_t sector)
305{
Herbert Xu45789322006-09-03 08:58:41 +1000306 u8 iv[cc->iv_size] __attribute__ ((aligned(__alignof__(u64))));
Herbert Xud1806f62006-08-22 20:29:17 +1000307 struct blkcipher_desc desc = {
308 .tfm = cc->tfm,
309 .info = iv,
310 .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int r;
313
314 if (cc->iv_gen_ops) {
315 r = cc->iv_gen_ops->generator(cc, iv, sector);
316 if (r < 0)
317 return r;
318
319 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000320 r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 else
Herbert Xud1806f62006-08-22 20:29:17 +1000322 r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 } else {
324 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000325 r = crypto_blkcipher_encrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 else
Herbert Xud1806f62006-08-22 20:29:17 +1000327 r = crypto_blkcipher_decrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 return r;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Milan Broz01482b72008-02-08 02:11:04 +0000347static int crypt_convert_block(struct crypt_config *cc,
348 struct convert_context *ctx)
349{
350 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
351 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
352 struct dm_crypt_request dmreq;
353
354 sg_init_table(&dmreq.sg_in, 1);
355 sg_set_page(&dmreq.sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
356 bv_in->bv_offset + ctx->offset_in);
357
358 sg_init_table(&dmreq.sg_out, 1);
359 sg_set_page(&dmreq.sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
360 bv_out->bv_offset + ctx->offset_out);
361
362 ctx->offset_in += 1 << SECTOR_SHIFT;
363 if (ctx->offset_in >= bv_in->bv_len) {
364 ctx->offset_in = 0;
365 ctx->idx_in++;
366 }
367
368 ctx->offset_out += 1 << SECTOR_SHIFT;
369 if (ctx->offset_out >= bv_out->bv_len) {
370 ctx->offset_out = 0;
371 ctx->idx_out++;
372 }
373
374 return crypt_convert_scatterlist(cc, &dmreq.sg_out, &dmreq.sg_in,
375 dmreq.sg_in.length,
376 bio_data_dir(ctx->bio_in) == WRITE,
377 ctx->sector);
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * Encrypt / decrypt data from one bio to another one (can be the same one)
382 */
383static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100384 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int r = 0;
387
388 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
389 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Milan Broz01482b72008-02-08 02:11:04 +0000390 r = crypt_convert_block(cc, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (r < 0)
392 break;
393
394 ctx->sector++;
395 }
396
397 return r;
398}
399
Milan Brozd469f842007-10-19 22:42:37 +0100400static void dm_crypt_bio_destructor(struct bio *bio)
401{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100402 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700403 struct crypt_config *cc = io->target->private;
404
405 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100406}
Milan Broz6a24c712006-10-03 01:15:40 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * Generate a new unfragmented bio with the given size
410 * This should never violate the device limitations
411 * May return a smaller bio when running out of pages
412 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100413static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Olaf Kirch027581f2007-05-09 02:32:52 -0700415 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700416 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400418 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000419 unsigned i, len;
420 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700422 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700423 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Olaf Kirch027581f2007-05-09 02:32:52 -0700426 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -0700427
Olaf Kirchf97380b2007-05-09 02:32:54 -0700428 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000429 page = mempool_alloc(cc->page_pool, gfp_mask);
430 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432
433 /*
434 * if additional pages cannot be allocated without waiting,
435 * return a partially allocated bio, the caller will then try
436 * to allocate additional bios while submitting this partial bio
437 */
Olaf Kirchf97380b2007-05-09 02:32:54 -0700438 if (i == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
440
Milan Broz91e10622007-12-13 14:16:10 +0000441 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Milan Broz91e10622007-12-13 14:16:10 +0000443 if (!bio_add_page(clone, page, len, 0)) {
444 mempool_free(page, cc->page_pool);
445 break;
446 }
447
448 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
Milan Broz8b004452006-10-03 01:15:37 -0700451 if (!clone->bi_size) {
452 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return NULL;
454 }
455
Milan Broz8b004452006-10-03 01:15:37 -0700456 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Neil Brown644bd2f2007-10-16 13:48:46 +0200459static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Neil Brown644bd2f2007-10-16 13:48:46 +0200461 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct bio_vec *bv;
463
Neil Brown644bd2f2007-10-16 13:48:46 +0200464 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700465 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 BUG_ON(!bv->bv_page);
467 mempool_free(bv->bv_page, cc->page_pool);
468 bv->bv_page = NULL;
469 }
470}
471
472/*
473 * One of the bios was finished. Check for completion of
474 * the whole request and correctly clean up the buffer.
475 */
Milan Broz5742fd72008-02-08 02:10:43 +0000476static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Milan Broz5742fd72008-02-08 02:10:43 +0000478 struct crypt_config *cc = io->target->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (!atomic_dec_and_test(&io->pending))
481 return;
482
NeilBrown6712ecf2007-09-27 12:47:43 +0200483 bio_endio(io->base_bio, io->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 mempool_free(io, cc->io_pool);
485}
486
487/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100488 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *
490 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700491 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100492 *
493 * kcryptd performs the actual encryption or decryption.
494 *
495 * kcryptd_io performs the IO submission.
496 *
497 * They must be separated as otherwise the final stages could be
498 * starved by new requests which can block in the first stages due
499 * to memory allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200501static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700502{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100503 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700504 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000505 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700506
Milan Brozadfe4772007-12-13 14:15:51 +0000507 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
508 error = -EIO;
509
Milan Broz8b004452006-10-03 01:15:37 -0700510 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200511 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700512 */
Milan Brozee7a4912008-02-08 02:10:46 +0000513 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200514 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000515
516 bio_put(clone);
517
518 if (rw == READ && !error) {
519 kcryptd_queue_crypt(io);
520 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200521 }
Milan Broz8b004452006-10-03 01:15:37 -0700522
Milan Brozadfe4772007-12-13 14:15:51 +0000523 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000524 io->error = error;
525
526 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700527}
528
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100529static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700530{
531 struct crypt_config *cc = io->target->private;
532
533 clone->bi_private = io;
534 clone->bi_end_io = crypt_endio;
535 clone->bi_bdev = cc->dev->bdev;
536 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700537 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700538}
539
Milan Broz4e4eef62008-02-08 02:10:49 +0000540static void kcryptd_io_read(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700541{
542 struct crypt_config *cc = io->target->private;
543 struct bio *base_bio = io->base_bio;
544 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700545
546 atomic_inc(&io->pending);
Milan Broz8b004452006-10-03 01:15:37 -0700547
548 /*
549 * The block layer might modify the bvec array, so always
550 * copy the required bvecs because we need the original
551 * one in order to decrypt the whole bio data *afterwards*.
552 */
Milan Broz6a24c712006-10-03 01:15:40 -0700553 clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
Milan Broz93e605c2006-10-03 01:15:38 -0700554 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000555 io->error = -ENOMEM;
556 crypt_dec_pending(io);
Milan Broz23541d22006-10-03 01:15:39 -0700557 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700558 }
Milan Broz8b004452006-10-03 01:15:37 -0700559
560 clone_init(io, clone);
561 clone->bi_idx = 0;
562 clone->bi_vcnt = bio_segments(base_bio);
563 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000564 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700565 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
566 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700567
Milan Broz93e605c2006-10-03 01:15:38 -0700568 generic_make_request(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700569}
570
Milan Broz4e4eef62008-02-08 02:10:49 +0000571static void kcryptd_io_write(struct dm_crypt_io *io)
572{
573}
574
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000575static void kcryptd_io(struct work_struct *work)
576{
577 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
578
579 if (bio_data_dir(io->base_bio) == READ)
580 kcryptd_io_read(io);
581 else
582 kcryptd_io_write(io);
583}
584
585static void kcryptd_queue_io(struct dm_crypt_io *io)
586{
587 struct crypt_config *cc = io->target->private;
588
589 INIT_WORK(&io->work, kcryptd_io);
590 queue_work(cc->io_queue, &io->work);
591}
592
Milan Broz4e4eef62008-02-08 02:10:49 +0000593static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int error)
594{
Milan Brozdec1ced2008-02-08 02:10:57 +0000595 struct bio *clone = io->ctx.bio_out;
596 struct crypt_config *cc = io->target->private;
597
598 if (unlikely(error < 0)) {
599 crypt_free_buffer_pages(cc, clone);
600 bio_put(clone);
601 io->error = -EIO;
Milan Brozdec1ced2008-02-08 02:10:57 +0000602 return;
603 }
604
605 /* crypt_convert should have filled the clone bio */
606 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
607
608 clone->bi_sector = cc->start + io->sector;
609 io->sector += bio_sectors(clone);
Milan Broz899c95d2008-02-08 02:11:02 +0000610
611 atomic_inc(&io->pending);
612 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000613}
614
Milan Broz84131db2008-02-08 02:10:59 +0000615static void kcryptd_crypt_write_convert_loop(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700616{
617 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700618 struct bio *clone;
Milan Brozdec1ced2008-02-08 02:10:57 +0000619 unsigned remaining = io->base_bio->bi_size;
620 int r;
Milan Broz8b004452006-10-03 01:15:37 -0700621
Milan Broz93e605c2006-10-03 01:15:38 -0700622 /*
623 * The allocated buffers can be smaller than the whole bio,
624 * so repeat the whole process until all the data can be handled.
625 */
626 while (remaining) {
Olaf Kirchf97380b2007-05-09 02:32:54 -0700627 clone = crypt_alloc_buffer(io, remaining);
Milan Broz23541d22006-10-03 01:15:39 -0700628 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000629 io->error = -ENOMEM;
Milan Broz23541d22006-10-03 01:15:39 -0700630 return;
631 }
Milan Broz93e605c2006-10-03 01:15:38 -0700632
Milan Broz53017032008-02-08 02:10:38 +0000633 io->ctx.bio_out = clone;
634 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -0700635
Milan Broz93e605c2006-10-03 01:15:38 -0700636 remaining -= clone->bi_size;
Milan Brozdec1ced2008-02-08 02:10:57 +0000637
638 r = crypt_convert(cc, &io->ctx);
639
640 kcryptd_crypt_write_io_submit(io, r);
641 if (unlikely(r < 0))
642 return;
Milan Broz93e605c2006-10-03 01:15:38 -0700643
Milan Broz93e605c2006-10-03 01:15:38 -0700644 /* out of memory -> run queues */
Milan Brozdec1ced2008-02-08 02:10:57 +0000645 if (unlikely(remaining))
Olaf Kirch98221eb2007-05-09 02:32:52 -0700646 congestion_wait(WRITE, HZ/100);
Milan Broz8b004452006-10-03 01:15:37 -0700647 }
Milan Broz8b004452006-10-03 01:15:37 -0700648}
649
Milan Broz84131db2008-02-08 02:10:59 +0000650static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
651{
652 struct crypt_config *cc = io->target->private;
653
Milan Broz899c95d2008-02-08 02:11:02 +0000654 /*
655 * Prevent io from disappearing until this function completes.
656 */
Milan Broz84131db2008-02-08 02:10:59 +0000657 atomic_inc(&io->pending);
658
659 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, io->sector);
660 kcryptd_crypt_write_convert_loop(io);
Milan Broz899c95d2008-02-08 02:11:02 +0000661
662 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +0000663}
664
Milan Broz4e4eef62008-02-08 02:10:49 +0000665static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
Milan Broz5742fd72008-02-08 02:10:43 +0000666{
667 if (unlikely(error < 0))
668 io->error = -EIO;
669
670 crypt_dec_pending(io);
671}
672
Milan Broz4e4eef62008-02-08 02:10:49 +0000673static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700674{
675 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +0000676 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -0700677
Milan Broz53017032008-02-08 02:10:38 +0000678 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +0000679 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -0700680
Milan Broz5742fd72008-02-08 02:10:43 +0000681 r = crypt_convert(cc, &io->ctx);
682
Milan Broz4e4eef62008-02-08 02:10:49 +0000683 kcryptd_crypt_read_done(io, r);
Milan Broz8b004452006-10-03 01:15:37 -0700684}
685
Milan Broz4e4eef62008-02-08 02:10:49 +0000686static void kcryptd_crypt(struct work_struct *work)
687{
688 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
689
690 if (bio_data_dir(io->base_bio) == READ)
691 kcryptd_crypt_read_convert(io);
692 else
693 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -0700694}
695
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000696static void kcryptd_queue_crypt(struct dm_crypt_io *io)
697{
698 struct crypt_config *cc = io->target->private;
699
700 INIT_WORK(&io->work, kcryptd_crypt);
701 queue_work(cc->crypt_queue, &io->work);
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Decode key from its hex representation
706 */
707static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
708{
709 char buffer[3];
710 char *endp;
711 unsigned int i;
712
713 buffer[2] = '\0';
714
Milan Broz8b004452006-10-03 01:15:37 -0700715 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 buffer[0] = *hex++;
717 buffer[1] = *hex++;
718
719 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
720
721 if (endp != &buffer[2])
722 return -EINVAL;
723 }
724
725 if (*hex != '\0')
726 return -EINVAL;
727
728 return 0;
729}
730
731/*
732 * Encode key into its hex representation
733 */
734static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
735{
736 unsigned int i;
737
Milan Broz8b004452006-10-03 01:15:37 -0700738 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sprintf(hex, "%02x", *key);
740 hex += 2;
741 key++;
742 }
743}
744
Milan Broze48d4bb2006-10-03 01:15:37 -0700745static int crypt_set_key(struct crypt_config *cc, char *key)
746{
747 unsigned key_size = strlen(key) >> 1;
748
749 if (cc->key_size && cc->key_size != key_size)
750 return -EINVAL;
751
752 cc->key_size = key_size; /* initial settings */
753
754 if ((!key_size && strcmp(key, "-")) ||
Milan Brozd469f842007-10-19 22:42:37 +0100755 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
Milan Broze48d4bb2006-10-03 01:15:37 -0700756 return -EINVAL;
757
758 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
759
760 return 0;
761}
762
763static int crypt_wipe_key(struct crypt_config *cc)
764{
765 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
766 memset(&cc->key, 0, cc->key_size * sizeof(u8));
767 return 0;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/*
771 * Construct an encryption mapping:
772 * <cipher> <key> <iv_offset> <dev_path> <start>
773 */
774static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
775{
776 struct crypt_config *cc;
Herbert Xud1806f62006-08-22 20:29:17 +1000777 struct crypto_blkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 char *tmp;
779 char *cipher;
780 char *chainmode;
781 char *ivmode;
782 char *ivopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 unsigned int key_size;
Andrew Morton4ee218c2006-03-27 01:17:48 -0800784 unsigned long long tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (argc != 5) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700787 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EINVAL;
789 }
790
791 tmp = argv[0];
792 cipher = strsep(&tmp, "-");
793 chainmode = strsep(&tmp, "-");
794 ivopts = strsep(&tmp, "-");
795 ivmode = strsep(&ivopts, ":");
796
797 if (tmp)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700798 DMWARN("Unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 key_size = strlen(argv[1]) >> 1;
801
Milan Broze48d4bb2006-10-03 01:15:37 -0700802 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (cc == NULL) {
804 ti->error =
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700805 "Cannot allocate transparent encryption context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOMEM;
807 }
808
Milan Broze48d4bb2006-10-03 01:15:37 -0700809 if (crypt_set_key(cc, argv[1])) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700810 ti->error = "Error decoding key";
Milan Broz636d5782007-10-19 22:47:52 +0100811 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 /* Compatiblity mode for old dm-crypt cipher strings */
815 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
816 chainmode = "cbc";
817 ivmode = "plain";
818 }
819
Herbert Xud1806f62006-08-22 20:29:17 +1000820 if (strcmp(chainmode, "ecb") && !ivmode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700821 ti->error = "This chaining mode requires an IV mechanism";
Milan Broz636d5782007-10-19 22:47:52 +0100822 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Milan Brozd469f842007-10-19 22:42:37 +0100825 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
826 chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
Herbert Xud1806f62006-08-22 20:29:17 +1000827 ti->error = "Chain mode + cipher name is too long";
Milan Broz636d5782007-10-19 22:47:52 +0100828 goto bad_cipher;
Herbert Xud1806f62006-08-22 20:29:17 +1000829 }
830
831 tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
832 if (IS_ERR(tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700833 ti->error = "Error allocating crypto tfm";
Milan Broz636d5782007-10-19 22:47:52 +0100834 goto bad_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Herbert Xud1806f62006-08-22 20:29:17 +1000837 strcpy(cc->cipher, cipher);
838 strcpy(cc->chainmode, chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 cc->tfm = tfm;
840
841 /*
Rik Snel48527fa2006-09-03 08:56:39 +1000842 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * See comments at iv code
844 */
845
846 if (ivmode == NULL)
847 cc->iv_gen_ops = NULL;
848 else if (strcmp(ivmode, "plain") == 0)
849 cc->iv_gen_ops = &crypt_iv_plain_ops;
850 else if (strcmp(ivmode, "essiv") == 0)
851 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +1000852 else if (strcmp(ivmode, "benbi") == 0)
853 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -0700854 else if (strcmp(ivmode, "null") == 0)
855 cc->iv_gen_ops = &crypt_iv_null_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700857 ti->error = "Invalid IV mode";
Milan Broz636d5782007-10-19 22:47:52 +0100858 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
862 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
Milan Broz636d5782007-10-19 22:47:52 +0100863 goto bad_ivmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Herbert Xud1806f62006-08-22 20:29:17 +1000865 cc->iv_size = crypto_blkcipher_ivsize(tfm);
866 if (cc->iv_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* at least a 64 bit sector number should fit in our buffer */
Herbert Xud1806f62006-08-22 20:29:17 +1000868 cc->iv_size = max(cc->iv_size,
Milan Brozd469f842007-10-19 22:42:37 +0100869 (unsigned int)(sizeof(u64) / sizeof(u8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (cc->iv_gen_ops) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700872 DMWARN("Selected cipher does not support IVs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (cc->iv_gen_ops->dtr)
874 cc->iv_gen_ops->dtr(cc);
875 cc->iv_gen_ops = NULL;
876 }
877 }
878
Matthew Dobson93d23412006-03-26 01:37:50 -0800879 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700881 ti->error = "Cannot allocate crypt io mempool";
Milan Broz636d5782007-10-19 22:47:52 +0100882 goto bad_slab_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Matthew Dobsona19b27c2006-03-26 01:37:45 -0800885 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700887 ti->error = "Cannot allocate page mempool";
Milan Broz636d5782007-10-19 22:47:52 +0100888 goto bad_page_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
Jens Axboe59725112007-04-02 10:06:42 +0200891 cc->bs = bioset_create(MIN_IOS, MIN_IOS);
Milan Broz6a24c712006-10-03 01:15:40 -0700892 if (!cc->bs) {
893 ti->error = "Cannot allocate crypt bioset";
894 goto bad_bs;
895 }
896
Herbert Xud1806f62006-08-22 20:29:17 +1000897 if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700898 ti->error = "Error setting key";
Milan Broz636d5782007-10-19 22:47:52 +0100899 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
Andrew Morton4ee218c2006-03-27 01:17:48 -0800902 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700903 ti->error = "Invalid iv_offset sector";
Milan Broz636d5782007-10-19 22:47:52 +0100904 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800906 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Andrew Morton4ee218c2006-03-27 01:17:48 -0800908 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700909 ti->error = "Invalid device sector";
Milan Broz636d5782007-10-19 22:47:52 +0100910 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800912 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (dm_get_device(ti, argv[3], cc->start, ti->len,
Milan Brozd469f842007-10-19 22:42:37 +0100915 dm_table_get_mode(ti->table), &cc->dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700916 ti->error = "Device lookup failed";
Milan Broz636d5782007-10-19 22:47:52 +0100917 goto bad_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
920 if (ivmode && cc->iv_gen_ops) {
921 if (ivopts)
922 *(ivopts - 1) = ':';
923 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
924 if (!cc->iv_mode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700925 ti->error = "Error kmallocing iv_mode string";
Milan Broz636d5782007-10-19 22:47:52 +0100926 goto bad_ivmode_string;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 strcpy(cc->iv_mode, ivmode);
929 } else
930 cc->iv_mode = NULL;
931
Milan Brozcabf08e2007-10-19 22:38:58 +0100932 cc->io_queue = create_singlethread_workqueue("kcryptd_io");
933 if (!cc->io_queue) {
934 ti->error = "Couldn't create kcryptd io queue";
935 goto bad_io_queue;
936 }
937
938 cc->crypt_queue = create_singlethread_workqueue("kcryptd");
939 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +0100940 ti->error = "Couldn't create kcryptd queue";
Milan Brozcabf08e2007-10-19 22:38:58 +0100941 goto bad_crypt_queue;
Milan Broz9934a8b2007-10-19 22:38:57 +0100942 }
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ti->private = cc;
945 return 0;
946
Milan Brozcabf08e2007-10-19 22:38:58 +0100947bad_crypt_queue:
948 destroy_workqueue(cc->io_queue);
949bad_io_queue:
Milan Broz9934a8b2007-10-19 22:38:57 +0100950 kfree(cc->iv_mode);
Milan Broz636d5782007-10-19 22:47:52 +0100951bad_ivmode_string:
Dmitry Monakhov55b42c52007-10-19 22:38:37 +0100952 dm_put_device(ti, cc->dev);
Milan Broz636d5782007-10-19 22:47:52 +0100953bad_device:
Milan Broz6a24c712006-10-03 01:15:40 -0700954 bioset_free(cc->bs);
955bad_bs:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 mempool_destroy(cc->page_pool);
Milan Broz636d5782007-10-19 22:47:52 +0100957bad_page_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 mempool_destroy(cc->io_pool);
Milan Broz636d5782007-10-19 22:47:52 +0100959bad_slab_pool:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
961 cc->iv_gen_ops->dtr(cc);
Milan Broz636d5782007-10-19 22:47:52 +0100962bad_ivmode:
Herbert Xud1806f62006-08-22 20:29:17 +1000963 crypto_free_blkcipher(tfm);
Milan Broz636d5782007-10-19 22:47:52 +0100964bad_cipher:
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800965 /* Must zero key material before freeing */
966 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(cc);
968 return -EINVAL;
969}
970
971static void crypt_dtr(struct dm_target *ti)
972{
973 struct crypt_config *cc = (struct crypt_config *) ti->private;
974
Milan Brozcabf08e2007-10-19 22:38:58 +0100975 destroy_workqueue(cc->io_queue);
976 destroy_workqueue(cc->crypt_queue);
Milan Broz80b16c12007-07-21 04:37:27 -0700977
Milan Broz6a24c712006-10-03 01:15:40 -0700978 bioset_free(cc->bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 mempool_destroy(cc->page_pool);
980 mempool_destroy(cc->io_pool);
981
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700982 kfree(cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
984 cc->iv_gen_ops->dtr(cc);
Herbert Xud1806f62006-08-22 20:29:17 +1000985 crypto_free_blkcipher(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 dm_put_device(ti, cc->dev);
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800987
988 /* Must zero key material before freeing */
989 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 kfree(cc);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static int crypt_map(struct dm_target *ti, struct bio *bio,
994 union map_info *map_context)
995{
Milan Broz8b004452006-10-03 01:15:37 -0700996 struct crypt_config *cc = ti->private;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100997 struct dm_crypt_io *io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Milan Broze48d4bb2006-10-03 01:15:37 -0700999 io = mempool_alloc(cc->io_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 io->target = ti;
Milan Broz8b004452006-10-03 01:15:37 -07001001 io->base_bio = bio;
Milan Broz0c395b02008-02-08 02:10:54 +00001002 io->sector = bio->bi_sector - ti->begin;
Milan Brozcabf08e2007-10-19 22:38:58 +01001003 io->error = 0;
Milan Broz93e605c2006-10-03 01:15:38 -07001004 atomic_set(&io->pending, 0);
Milan Brozcabf08e2007-10-19 22:38:58 +01001005
1006 if (bio_data_dir(io->base_bio) == READ)
1007 kcryptd_queue_io(io);
1008 else
1009 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001011 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static int crypt_status(struct dm_target *ti, status_type_t type,
1015 char *result, unsigned int maxlen)
1016{
1017 struct crypt_config *cc = (struct crypt_config *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned int sz = 0;
1019
1020 switch (type) {
1021 case STATUSTYPE_INFO:
1022 result[0] = '\0';
1023 break;
1024
1025 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (cc->iv_mode)
Christophe Saout37af6562006-10-30 20:39:08 +01001027 DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
1028 cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 else
Christophe Saout37af6562006-10-30 20:39:08 +01001030 DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (cc->key_size > 0) {
1033 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1034 return -ENOMEM;
1035
1036 crypt_encode_key(result + sz, cc->key, cc->key_size);
1037 sz += cc->key_size << 1;
1038 } else {
1039 if (sz >= maxlen)
1040 return -ENOMEM;
1041 result[sz++] = '-';
1042 }
1043
Andrew Morton4ee218c2006-03-27 01:17:48 -08001044 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1045 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047 }
1048 return 0;
1049}
1050
Milan Broze48d4bb2006-10-03 01:15:37 -07001051static void crypt_postsuspend(struct dm_target *ti)
1052{
1053 struct crypt_config *cc = ti->private;
1054
1055 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1056}
1057
1058static int crypt_preresume(struct dm_target *ti)
1059{
1060 struct crypt_config *cc = ti->private;
1061
1062 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1063 DMERR("aborting resume - crypt key is not set.");
1064 return -EAGAIN;
1065 }
1066
1067 return 0;
1068}
1069
1070static void crypt_resume(struct dm_target *ti)
1071{
1072 struct crypt_config *cc = ti->private;
1073
1074 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1075}
1076
1077/* Message interface
1078 * key set <key>
1079 * key wipe
1080 */
1081static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1082{
1083 struct crypt_config *cc = ti->private;
1084
1085 if (argc < 2)
1086 goto error;
1087
1088 if (!strnicmp(argv[0], MESG_STR("key"))) {
1089 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1090 DMWARN("not suspended during key manipulation.");
1091 return -EINVAL;
1092 }
1093 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
1094 return crypt_set_key(cc, argv[2]);
1095 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
1096 return crypt_wipe_key(cc);
1097 }
1098
1099error:
1100 DMWARN("unrecognised message received.");
1101 return -EINVAL;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static struct target_type crypt_target = {
1105 .name = "crypt",
Ludwig Nussel46b47732007-05-09 02:32:55 -07001106 .version= {1, 5, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 .module = THIS_MODULE,
1108 .ctr = crypt_ctr,
1109 .dtr = crypt_dtr,
1110 .map = crypt_map,
1111 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001112 .postsuspend = crypt_postsuspend,
1113 .preresume = crypt_preresume,
1114 .resume = crypt_resume,
1115 .message = crypt_message,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116};
1117
1118static int __init dm_crypt_init(void)
1119{
1120 int r;
1121
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001122 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!_crypt_io_pool)
1124 return -ENOMEM;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 r = dm_register_target(&crypt_target);
1127 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001128 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001129 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return r;
1133}
1134
1135static void __exit dm_crypt_exit(void)
1136{
1137 int r = dm_unregister_target(&crypt_target);
1138
1139 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001140 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 kmem_cache_destroy(_crypt_io_pool);
1143}
1144
1145module_init(dm_crypt_init);
1146module_exit(dm_crypt_exit);
1147
1148MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1149MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1150MODULE_LICENSE("GPL");