blob: 48a8445ae53325217cdd87ea8285122fa88d07d1 [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>
Andi Kleenc0297722011-01-13 19:59:53 +000021#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100023#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100025#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000026#include <crypto/hash.h>
27#include <crypto/md5.h>
28#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Mikulas Patocka586e80e2008-10-21 17:44:59 +010030#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alasdair G Kergon72d94862006-06-26 00:27:35 -070032#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070033#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * context holding the current state of a multi-part conversion
37 */
38struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000039 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct bio *bio_in;
41 struct bio *bio_out;
42 unsigned int offset_in;
43 unsigned int offset_out;
44 unsigned int idx_in;
45 unsigned int idx_out;
46 sector_t sector;
Mikulas Patocka815bc972012-03-27 13:17:56 -070047 atomic_t cc_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Milan Broz53017032008-02-08 02:10:38 +000050/*
51 * per bio private data
52 */
53struct dm_crypt_io {
54 struct dm_target *target;
55 struct bio *base_bio;
56 struct work_struct work;
57
58 struct convert_context ctx;
59
Mikulas Patocka815bc972012-03-27 13:17:56 -070060 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000061 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000062 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010063 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000064};
65
Milan Broz01482b72008-02-08 02:11:04 +000066struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000067 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000068 struct scatterlist sg_in;
69 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000070 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct crypt_config;
74
75struct crypt_iv_operations {
76 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010077 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000079 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000080 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000081 int (*generator)(struct crypt_config *cc, u8 *iv,
82 struct dm_crypt_request *dmreq);
83 int (*post)(struct crypt_config *cc, u8 *iv,
84 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Milan Broz60473592009-12-10 23:51:55 +000087struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000088 struct crypto_hash *hash_tfm;
89 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000090};
91
92struct iv_benbi_private {
93 int shift;
94};
95
Milan Broz34745782011-01-13 19:59:55 +000096#define LMK_SEED_SIZE 64 /* hash + 0 */
97struct iv_lmk_private {
98 struct crypto_shash *hash_tfm;
99 u8 *seed;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Crypt: maps a linear range of a block device
104 * and encrypts / decrypts at the same time.
105 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700106enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000107
108/*
109 * Duplicated per-CPU state for cipher.
110 */
111struct crypt_cpu {
112 struct ablkcipher_request *req;
Andi Kleenc0297722011-01-13 19:59:53 +0000113};
114
115/*
116 * The fields in here must be read only after initialization,
117 * changing state should be in crypt_cpu.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119struct crypt_config {
120 struct dm_dev *dev;
121 sector_t start;
122
123 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000124 * pool for per bio private data, crypto requests and
125 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000128 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700130 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Milan Brozcabf08e2007-10-19 22:38:58 +0100132 struct workqueue_struct *io_queue;
133 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700134
Milan Broz5ebaee62010-08-12 04:14:07 +0100135 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000136 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800139 union {
Milan Broz60473592009-12-10 23:51:55 +0000140 struct iv_essiv_private essiv;
141 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000142 struct iv_lmk_private lmk;
Herbert Xu79066ad2006-12-05 13:41:52 -0800143 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 sector_t iv_offset;
145 unsigned int iv_size;
146
Milan Brozddd42ed2008-02-08 02:11:07 +0000147 /*
Andi Kleenc0297722011-01-13 19:59:53 +0000148 * Duplicated per cpu state. Access through
149 * per_cpu_ptr() only.
150 */
151 struct crypt_cpu __percpu *cpu;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700152
153 /* ESSIV: struct crypto_cipher *essiv_tfm */
154 void *iv_private;
155 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000156 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000157
158 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000159 * Layout of each crypto request:
160 *
161 * struct ablkcipher_request
162 * context
163 * padding
164 * struct dm_crypt_request
165 * padding
166 * IV
167 *
168 * The padding is added so that dm_crypt_request and the IV are
169 * correctly aligned.
170 */
171 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000172
Milan Broze48d4bb2006-10-03 01:15:37 -0700173 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int key_size;
Milan Brozd1f96422011-01-13 19:59:54 +0000175 unsigned int key_parts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u8 key[0];
177};
178
Milan Broz6a24c712006-10-03 01:15:40 -0700179#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define MIN_POOL_PAGES 32
181#define MIN_BIO_PAGES 8
182
Christoph Lametere18b8902006-12-06 20:33:20 -0800183static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100185static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000186static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000187static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700188
Andi Kleenc0297722011-01-13 19:59:53 +0000189static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
190{
191 return this_cpu_ptr(cc->cpu);
192}
193
194/*
195 * Use this to access cipher attributes that are the same for each CPU.
196 */
197static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
198{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700199 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * Different IV generation algorithms:
204 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000205 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200206 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Milan Broz61afef62009-12-10 23:52:25 +0000208 * plain64: the initial vector is the 64-bit little-endian version of the sector
209 * number, padded with zeros if necessary.
210 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000211 * essiv: "encrypted sector|salt initial vector", the sector number is
212 * encrypted with the bulk cipher using a salt as key. The salt
213 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
Rik Snel48527fa2006-09-03 08:56:39 +1000215 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
216 * (needed for LRW-32-AES and possible other narrow block modes)
217 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700218 * null: the initial vector is always zero. Provides compatibility with
219 * obsolete loop_fish2 devices. Do not use for new devices.
220 *
Milan Broz34745782011-01-13 19:59:55 +0000221 * lmk: Compatible implementation of the block chaining mode used
222 * by the Loop-AES block device encryption system
223 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
224 * It operates on full 512 byte sectors and uses CBC
225 * with an IV derived from the sector number, the data and
226 * optionally extra IV seed.
227 * This means that after decryption the first block
228 * of sector must be tweaked according to decrypted data.
229 * Loop-AES can use three encryption schemes:
230 * version 1: is plain aes-cbc mode
231 * version 2: uses 64 multikey scheme with lmk IV generator
232 * version 3: the same as version 2 with additional IV seed
233 * (it uses 65 keys, last key is used as IV seed)
234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * plumb: unimplemented, see:
236 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
237 */
238
Milan Broz2dc53272011-01-13 19:59:54 +0000239static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
240 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000243 *(u32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 return 0;
246}
247
Milan Broz61afef62009-12-10 23:52:25 +0000248static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000249 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000250{
251 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000252 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000253
254 return 0;
255}
256
Milan Brozb95bf2d2009-12-10 23:51:56 +0000257/* Initialise ESSIV - compute salt but no local memory allocations */
258static int crypt_iv_essiv_init(struct crypt_config *cc)
259{
260 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
261 struct hash_desc desc;
262 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000263 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700264 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000265
266 sg_init_one(&sg, cc->key, cc->key_size);
267 desc.tfm = essiv->hash_tfm;
268 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
269
270 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
271 if (err)
272 return err;
273
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700274 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000275
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700276 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
277 crypto_hash_digestsize(essiv->hash_tfm));
278 if (err)
279 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000280
281 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000282}
283
Milan Broz542da312009-12-10 23:51:57 +0000284/* Wipe salt and reset key derived from volume key */
285static int crypt_iv_essiv_wipe(struct crypt_config *cc)
286{
287 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
288 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000289 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700290 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000291
292 memset(essiv->salt, 0, salt_size);
293
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700294 essiv_tfm = cc->iv_private;
295 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
296 if (r)
297 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000298
299 return err;
300}
301
302/* Set up per cpu cipher state */
303static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
304 struct dm_target *ti,
305 u8 *salt, unsigned saltsize)
306{
307 struct crypto_cipher *essiv_tfm;
308 int err;
309
310 /* Setup the essiv_tfm with the given salt */
311 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
312 if (IS_ERR(essiv_tfm)) {
313 ti->error = "Error allocating crypto tfm for ESSIV";
314 return essiv_tfm;
315 }
316
317 if (crypto_cipher_blocksize(essiv_tfm) !=
318 crypto_ablkcipher_ivsize(any_tfm(cc))) {
319 ti->error = "Block size of ESSIV cipher does "
320 "not match IV size of block cipher";
321 crypto_free_cipher(essiv_tfm);
322 return ERR_PTR(-EINVAL);
323 }
324
325 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
326 if (err) {
327 ti->error = "Failed to set key for ESSIV cipher";
328 crypto_free_cipher(essiv_tfm);
329 return ERR_PTR(err);
330 }
331
332 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000333}
334
Milan Broz60473592009-12-10 23:51:55 +0000335static void crypt_iv_essiv_dtr(struct crypt_config *cc)
336{
Andi Kleenc0297722011-01-13 19:59:53 +0000337 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000338 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
339
Milan Brozb95bf2d2009-12-10 23:51:56 +0000340 crypto_free_hash(essiv->hash_tfm);
341 essiv->hash_tfm = NULL;
342
343 kzfree(essiv->salt);
344 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000345
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700346 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000347
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700348 if (essiv_tfm)
349 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000350
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700351 cc->iv_private = NULL;
352
Milan Broz60473592009-12-10 23:51:55 +0000353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100356 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Milan Broz5861f1b2009-12-10 23:51:56 +0000358 struct crypto_cipher *essiv_tfm = NULL;
359 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000360 u8 *salt = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700361 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Milan Broz5861f1b2009-12-10 23:51:56 +0000363 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700364 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -EINVAL;
366 }
367
Milan Brozb95bf2d2009-12-10 23:51:56 +0000368 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000369 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
370 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700371 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000372 err = PTR_ERR(hash_tfm);
373 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Milan Brozb95bf2d2009-12-10 23:51:56 +0000376 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000377 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700378 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000379 err = -ENOMEM;
380 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Milan Brozb95bf2d2009-12-10 23:51:56 +0000383 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000384 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
385
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700386 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
387 crypto_hash_digestsize(hash_tfm));
388 if (IS_ERR(essiv_tfm)) {
389 crypt_iv_essiv_dtr(cc);
390 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000391 }
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700392 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000395
396bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000397 if (hash_tfm && !IS_ERR(hash_tfm))
398 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000399 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Milan Broz2dc53272011-01-13 19:59:54 +0000403static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
404 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700406
407 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000410 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000411 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414}
415
Rik Snel48527fa2006-09-03 08:56:39 +1000416static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
417 const char *opts)
418{
Andi Kleenc0297722011-01-13 19:59:53 +0000419 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800420 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000421
422 /* we need to calculate how far we must shift the sector count
423 * to get the cipher block count, we use this shift in _gen */
424
425 if (1 << log != bs) {
426 ti->error = "cypher blocksize is not a power of 2";
427 return -EINVAL;
428 }
429
430 if (log > 9) {
431 ti->error = "cypher blocksize is > 512";
432 return -EINVAL;
433 }
434
Milan Broz60473592009-12-10 23:51:55 +0000435 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000436
437 return 0;
438}
439
440static void crypt_iv_benbi_dtr(struct crypt_config *cc)
441{
Rik Snel48527fa2006-09-03 08:56:39 +1000442}
443
Milan Broz2dc53272011-01-13 19:59:54 +0000444static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
445 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000446{
Herbert Xu79066ad2006-12-05 13:41:52 -0800447 __be64 val;
448
Rik Snel48527fa2006-09-03 08:56:39 +1000449 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800450
Milan Broz2dc53272011-01-13 19:59:54 +0000451 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800452 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
Milan Broz2dc53272011-01-13 19:59:54 +0000457static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
458 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700459{
460 memset(iv, 0, cc->iv_size);
461
462 return 0;
463}
464
Milan Broz34745782011-01-13 19:59:55 +0000465static void crypt_iv_lmk_dtr(struct crypt_config *cc)
466{
467 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
468
469 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
470 crypto_free_shash(lmk->hash_tfm);
471 lmk->hash_tfm = NULL;
472
473 kzfree(lmk->seed);
474 lmk->seed = NULL;
475}
476
477static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
478 const char *opts)
479{
480 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
481
482 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
483 if (IS_ERR(lmk->hash_tfm)) {
484 ti->error = "Error initializing LMK hash";
485 return PTR_ERR(lmk->hash_tfm);
486 }
487
488 /* No seed in LMK version 2 */
489 if (cc->key_parts == cc->tfms_count) {
490 lmk->seed = NULL;
491 return 0;
492 }
493
494 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
495 if (!lmk->seed) {
496 crypt_iv_lmk_dtr(cc);
497 ti->error = "Error kmallocing seed storage in LMK";
498 return -ENOMEM;
499 }
500
501 return 0;
502}
503
504static int crypt_iv_lmk_init(struct crypt_config *cc)
505{
506 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
507 int subkey_size = cc->key_size / cc->key_parts;
508
509 /* LMK seed is on the position of LMK_KEYS + 1 key */
510 if (lmk->seed)
511 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
512 crypto_shash_digestsize(lmk->hash_tfm));
513
514 return 0;
515}
516
517static int crypt_iv_lmk_wipe(struct crypt_config *cc)
518{
519 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
520
521 if (lmk->seed)
522 memset(lmk->seed, 0, LMK_SEED_SIZE);
523
524 return 0;
525}
526
527static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
528 struct dm_crypt_request *dmreq,
529 u8 *data)
530{
531 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
532 struct {
533 struct shash_desc desc;
534 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
535 } sdesc;
536 struct md5_state md5state;
537 u32 buf[4];
538 int i, r;
539
540 sdesc.desc.tfm = lmk->hash_tfm;
541 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
542
543 r = crypto_shash_init(&sdesc.desc);
544 if (r)
545 return r;
546
547 if (lmk->seed) {
548 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
549 if (r)
550 return r;
551 }
552
553 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
554 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
555 if (r)
556 return r;
557
558 /* Sector is cropped to 56 bits here */
559 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
560 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
561 buf[2] = cpu_to_le32(4024);
562 buf[3] = 0;
563 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
564 if (r)
565 return r;
566
567 /* No MD5 padding here */
568 r = crypto_shash_export(&sdesc.desc, &md5state);
569 if (r)
570 return r;
571
572 for (i = 0; i < MD5_HASH_WORDS; i++)
573 __cpu_to_le32s(&md5state.hash[i]);
574 memcpy(iv, &md5state.hash, cc->iv_size);
575
576 return 0;
577}
578
579static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
580 struct dm_crypt_request *dmreq)
581{
582 u8 *src;
583 int r = 0;
584
585 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
586 src = kmap_atomic(sg_page(&dmreq->sg_in), KM_USER0);
587 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
588 kunmap_atomic(src, KM_USER0);
589 } else
590 memset(iv, 0, cc->iv_size);
591
592 return r;
593}
594
595static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
596 struct dm_crypt_request *dmreq)
597{
598 u8 *dst;
599 int r;
600
601 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
602 return 0;
603
604 dst = kmap_atomic(sg_page(&dmreq->sg_out), KM_USER0);
605 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
606
607 /* Tweak the first block of plaintext sector */
608 if (!r)
609 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
610
611 kunmap_atomic(dst, KM_USER0);
612 return r;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static struct crypt_iv_operations crypt_iv_plain_ops = {
616 .generator = crypt_iv_plain_gen
617};
618
Milan Broz61afef62009-12-10 23:52:25 +0000619static struct crypt_iv_operations crypt_iv_plain64_ops = {
620 .generator = crypt_iv_plain64_gen
621};
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static struct crypt_iv_operations crypt_iv_essiv_ops = {
624 .ctr = crypt_iv_essiv_ctr,
625 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000626 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000627 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 .generator = crypt_iv_essiv_gen
629};
630
Rik Snel48527fa2006-09-03 08:56:39 +1000631static struct crypt_iv_operations crypt_iv_benbi_ops = {
632 .ctr = crypt_iv_benbi_ctr,
633 .dtr = crypt_iv_benbi_dtr,
634 .generator = crypt_iv_benbi_gen
635};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Ludwig Nussel46b47732007-05-09 02:32:55 -0700637static struct crypt_iv_operations crypt_iv_null_ops = {
638 .generator = crypt_iv_null_gen
639};
640
Milan Broz34745782011-01-13 19:59:55 +0000641static struct crypt_iv_operations crypt_iv_lmk_ops = {
642 .ctr = crypt_iv_lmk_ctr,
643 .dtr = crypt_iv_lmk_dtr,
644 .init = crypt_iv_lmk_init,
645 .wipe = crypt_iv_lmk_wipe,
646 .generator = crypt_iv_lmk_gen,
647 .post = crypt_iv_lmk_post
648};
649
Milan Brozd469f842007-10-19 22:42:37 +0100650static void crypt_convert_init(struct crypt_config *cc,
651 struct convert_context *ctx,
652 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000653 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 ctx->bio_in = bio_in;
656 ctx->bio_out = bio_out;
657 ctx->offset_in = 0;
658 ctx->offset_out = 0;
659 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
660 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
661 ctx->sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000662 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Huang Yingb2174ee2009-03-16 17:44:33 +0000665static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
666 struct ablkcipher_request *req)
667{
668 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
669}
670
671static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
672 struct dm_crypt_request *dmreq)
673{
674 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
675}
676
Milan Broz2dc53272011-01-13 19:59:54 +0000677static u8 *iv_of_dmreq(struct crypt_config *cc,
678 struct dm_crypt_request *dmreq)
679{
680 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
681 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
682}
683
Milan Broz01482b72008-02-08 02:11:04 +0000684static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000685 struct convert_context *ctx,
686 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000687{
688 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
689 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000690 struct dm_crypt_request *dmreq;
691 u8 *iv;
692 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000693
Huang Yingb2174ee2009-03-16 17:44:33 +0000694 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000695 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000696
Milan Broz2dc53272011-01-13 19:59:54 +0000697 dmreq->iv_sector = ctx->sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000698 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000699 sg_init_table(&dmreq->sg_in, 1);
700 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000701 bv_in->bv_offset + ctx->offset_in);
702
Milan Broz3a7f6c92008-02-08 02:11:14 +0000703 sg_init_table(&dmreq->sg_out, 1);
704 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000705 bv_out->bv_offset + ctx->offset_out);
706
707 ctx->offset_in += 1 << SECTOR_SHIFT;
708 if (ctx->offset_in >= bv_in->bv_len) {
709 ctx->offset_in = 0;
710 ctx->idx_in++;
711 }
712
713 ctx->offset_out += 1 << SECTOR_SHIFT;
714 if (ctx->offset_out >= bv_out->bv_len) {
715 ctx->offset_out = 0;
716 ctx->idx_out++;
717 }
718
Milan Broz3a7f6c92008-02-08 02:11:14 +0000719 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000720 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000721 if (r < 0)
722 return r;
723 }
724
725 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
726 1 << SECTOR_SHIFT, iv);
727
728 if (bio_data_dir(ctx->bio_in) == WRITE)
729 r = crypto_ablkcipher_encrypt(req);
730 else
731 r = crypto_ablkcipher_decrypt(req);
732
Milan Broz2dc53272011-01-13 19:59:54 +0000733 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
734 r = cc->iv_gen_ops->post(cc, iv, dmreq);
735
Milan Broz3a7f6c92008-02-08 02:11:14 +0000736 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000737}
738
Milan Broz95497a92008-02-08 02:11:12 +0000739static void kcryptd_async_done(struct crypto_async_request *async_req,
740 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000741
Milan Brozddd42ed2008-02-08 02:11:07 +0000742static void crypt_alloc_req(struct crypt_config *cc,
743 struct convert_context *ctx)
744{
Andi Kleenc0297722011-01-13 19:59:53 +0000745 struct crypt_cpu *this_cc = this_crypt_config(cc);
Milan Brozd1f96422011-01-13 19:59:54 +0000746 unsigned key_index = ctx->sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000747
748 if (!this_cc->req)
749 this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
750
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700751 ablkcipher_request_set_tfm(this_cc->req, cc->tfms[key_index]);
Andi Kleenc0297722011-01-13 19:59:53 +0000752 ablkcipher_request_set_callback(this_cc->req,
753 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
754 kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * Encrypt / decrypt data from one bio to another one (can be the same one)
759 */
760static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100761 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Andi Kleenc0297722011-01-13 19:59:53 +0000763 struct crypt_cpu *this_cc = this_crypt_config(cc);
Milan Broz3f1e9072008-03-28 14:16:07 -0700764 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Mikulas Patocka815bc972012-03-27 13:17:56 -0700766 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
769 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Milan Broz3a7f6c92008-02-08 02:11:14 +0000771 crypt_alloc_req(cc, ctx);
772
Mikulas Patocka815bc972012-03-27 13:17:56 -0700773 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700774
Andi Kleenc0297722011-01-13 19:59:53 +0000775 r = crypt_convert_block(cc, ctx, this_cc->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000776
777 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700778 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000779 case -EBUSY:
780 wait_for_completion(&ctx->restart);
781 INIT_COMPLETION(ctx->restart);
782 /* fall through*/
783 case -EINPROGRESS:
Andi Kleenc0297722011-01-13 19:59:53 +0000784 this_cc->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000785 ctx->sector++;
786 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000787
Milan Broz3f1e9072008-03-28 14:16:07 -0700788 /* sync */
789 case 0:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700790 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700791 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100792 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700793 continue;
794
795 /* error */
796 default:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700797 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700798 return r;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Milan Broz3f1e9072008-03-28 14:16:07 -0700802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Milan Brozd469f842007-10-19 22:42:37 +0100805static void dm_crypt_bio_destructor(struct bio *bio)
806{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100807 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700808 struct crypt_config *cc = io->target->private;
809
810 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100811}
Milan Broz6a24c712006-10-03 01:15:40 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
814 * Generate a new unfragmented bio with the given size
815 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100816 * May return a smaller bio when running out of pages, indicated by
817 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Milan Broz933f01d2008-10-10 13:37:08 +0100819static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
820 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Olaf Kirch027581f2007-05-09 02:32:52 -0700822 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700823 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400825 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000826 unsigned i, len;
827 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700829 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700830 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Olaf Kirch027581f2007-05-09 02:32:52 -0700833 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100834 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700835
Olaf Kirchf97380b2007-05-09 02:32:54 -0700836 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000837 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100838 if (!page) {
839 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /*
844 * if additional pages cannot be allocated without waiting,
845 * return a partially allocated bio, the caller will then try
846 * to allocate additional bios while submitting this partial bio
847 */
Mikulas Patocka71b41c32012-03-27 13:15:32 -0700848 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Milan Broz91e10622007-12-13 14:16:10 +0000850 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Milan Broz91e10622007-12-13 14:16:10 +0000852 if (!bio_add_page(clone, page, len, 0)) {
853 mempool_free(page, cc->page_pool);
854 break;
855 }
856
857 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Milan Broz8b004452006-10-03 01:15:37 -0700860 if (!clone->bi_size) {
861 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return NULL;
863 }
864
Milan Broz8b004452006-10-03 01:15:37 -0700865 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Neil Brown644bd2f2007-10-16 13:48:46 +0200868static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Neil Brown644bd2f2007-10-16 13:48:46 +0200870 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct bio_vec *bv;
872
Neil Brown644bd2f2007-10-16 13:48:46 +0200873 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700874 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 BUG_ON(!bv->bv_page);
876 mempool_free(bv->bv_page, cc->page_pool);
877 bv->bv_page = NULL;
878 }
879}
880
Milan Brozdc440d1e2008-10-10 13:37:03 +0100881static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
882 struct bio *bio, sector_t sector)
883{
884 struct crypt_config *cc = ti->private;
885 struct dm_crypt_io *io;
886
887 io = mempool_alloc(cc->io_pool, GFP_NOIO);
888 io->target = ti;
889 io->base_bio = bio;
890 io->sector = sector;
891 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100892 io->base_io = NULL;
Mikulas Patocka815bc972012-03-27 13:17:56 -0700893 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +0100894
895 return io;
896}
897
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100898static void crypt_inc_pending(struct dm_crypt_io *io)
899{
Mikulas Patocka815bc972012-03-27 13:17:56 -0700900 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/*
904 * One of the bios was finished. Check for completion of
905 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100906 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Milan Broz5742fd72008-02-08 02:10:43 +0000908static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Milan Broz5742fd72008-02-08 02:10:43 +0000910 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000911 struct bio *base_bio = io->base_bio;
912 struct dm_crypt_io *base_io = io->base_io;
913 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Mikulas Patocka815bc972012-03-27 13:17:56 -0700915 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000919
920 if (likely(!base_io))
921 bio_endio(base_bio, error);
922 else {
923 if (error && !base_io->error)
924 base_io->error = error;
925 crypt_dec_pending(base_io);
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100930 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 *
932 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700933 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100934 *
935 * kcryptd performs the actual encryption or decryption.
936 *
937 * kcryptd_io performs the IO submission.
938 *
939 * They must be separated as otherwise the final stages could be
940 * starved by new requests which can block in the first stages due
941 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +0000942 *
943 * The work is done per CPU global for all dm-crypt instances.
944 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200946static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700947{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100948 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700949 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000950 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700951
Milan Brozadfe4772007-12-13 14:15:51 +0000952 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
953 error = -EIO;
954
Milan Broz8b004452006-10-03 01:15:37 -0700955 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200956 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700957 */
Milan Brozee7a4912008-02-08 02:10:46 +0000958 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200959 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000960
961 bio_put(clone);
962
963 if (rw == READ && !error) {
964 kcryptd_queue_crypt(io);
965 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200966 }
Milan Broz8b004452006-10-03 01:15:37 -0700967
Milan Brozadfe4772007-12-13 14:15:51 +0000968 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000969 io->error = error;
970
971 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700972}
973
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100974static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700975{
976 struct crypt_config *cc = io->target->private;
977
978 clone->bi_private = io;
979 clone->bi_end_io = crypt_endio;
980 clone->bi_bdev = cc->dev->bdev;
981 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700982 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700983}
984
Milan Broz20c82532011-01-13 19:59:53 +0000985static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -0700986{
987 struct crypt_config *cc = io->target->private;
988 struct bio *base_bio = io->base_bio;
989 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700990
Milan Broz8b004452006-10-03 01:15:37 -0700991 /*
992 * The block layer might modify the bvec array, so always
993 * copy the required bvecs because we need the original
994 * one in order to decrypt the whole bio data *afterwards*.
995 */
Milan Broz20c82532011-01-13 19:59:53 +0000996 clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100997 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +0000998 return 1;
Milan Broz8b004452006-10-03 01:15:37 -0700999
Milan Broz20c82532011-01-13 19:59:53 +00001000 crypt_inc_pending(io);
1001
Milan Broz8b004452006-10-03 01:15:37 -07001002 clone_init(io, clone);
1003 clone->bi_idx = 0;
1004 clone->bi_vcnt = bio_segments(base_bio);
1005 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +00001006 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001007 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
1008 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -07001009
Milan Broz93e605c2006-10-03 01:15:38 -07001010 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001011 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001012}
1013
Milan Broz4e4eef62008-02-08 02:10:49 +00001014static void kcryptd_io_write(struct dm_crypt_io *io)
1015{
Milan Broz95497a92008-02-08 02:11:12 +00001016 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +00001017 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001018}
1019
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001020static void kcryptd_io(struct work_struct *work)
1021{
1022 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1023
Milan Broz20c82532011-01-13 19:59:53 +00001024 if (bio_data_dir(io->base_bio) == READ) {
1025 crypt_inc_pending(io);
1026 if (kcryptd_io_read(io, GFP_NOIO))
1027 io->error = -ENOMEM;
1028 crypt_dec_pending(io);
1029 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001030 kcryptd_io_write(io);
1031}
1032
1033static void kcryptd_queue_io(struct dm_crypt_io *io)
1034{
1035 struct crypt_config *cc = io->target->private;
1036
1037 INIT_WORK(&io->work, kcryptd_io);
1038 queue_work(cc->io_queue, &io->work);
1039}
1040
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001041static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001042{
Milan Brozdec1ced2008-02-08 02:10:57 +00001043 struct bio *clone = io->ctx.bio_out;
1044 struct crypt_config *cc = io->target->private;
1045
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001046 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001047 crypt_free_buffer_pages(cc, clone);
1048 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001049 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001050 return;
1051 }
1052
1053 /* crypt_convert should have filled the clone bio */
1054 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
1055
1056 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001057
Milan Broz95497a92008-02-08 02:11:12 +00001058 if (async)
1059 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001060 else
Milan Broz95497a92008-02-08 02:11:12 +00001061 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001062}
1063
Milan Brozfc5a5e92008-10-10 13:37:04 +01001064static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001065{
1066 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -07001067 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001068 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001069 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001070 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +00001071 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001072 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001073 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001074
Milan Broz93e605c2006-10-03 01:15:38 -07001075 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001076 * Prevent io from disappearing until this function completes.
1077 */
1078 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001079 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001080
1081 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001082 * The allocated buffers can be smaller than the whole bio,
1083 * so repeat the whole process until all the data can be handled.
1084 */
1085 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001086 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001087 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001088 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001089 break;
Milan Broz23541d22006-10-03 01:15:39 -07001090 }
Milan Broz93e605c2006-10-03 01:15:38 -07001091
Milan Broz53017032008-02-08 02:10:38 +00001092 io->ctx.bio_out = clone;
1093 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -07001094
Milan Broz93e605c2006-10-03 01:15:38 -07001095 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001096 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001097
Milan Broz4e594092008-10-10 13:37:07 +01001098 crypt_inc_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001099 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001100 if (r < 0)
1101 io->error = -EIO;
Mikulas Patocka815bc972012-03-27 13:17:56 -07001102 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001103
Milan Brozc8081612008-10-10 13:37:08 +01001104 /* Encryption was already finished, submit io now */
1105 if (crypt_finished) {
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001106 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001107
1108 /*
1109 * If there was an error, do not try next fragments.
1110 * For async, error is processed in async handler.
1111 */
Milan Broz6c031f42008-10-10 13:37:06 +01001112 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001113 break;
Milan Brozb635b002008-10-21 17:45:00 +01001114
1115 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001116 }
Milan Broz93e605c2006-10-03 01:15:38 -07001117
Milan Broz933f01d2008-10-10 13:37:08 +01001118 /*
1119 * Out of memory -> run queues
1120 * But don't wait if split was due to the io size restriction
1121 */
1122 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001123 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001124
Milan Broz393b47e2008-10-21 17:45:02 +01001125 /*
1126 * With async crypto it is unsafe to share the crypto context
1127 * between fragments, so switch to a new dm_crypt_io structure.
1128 */
1129 if (unlikely(!crypt_finished && remaining)) {
1130 new_io = crypt_io_alloc(io->target, io->base_bio,
1131 sector);
1132 crypt_inc_pending(new_io);
1133 crypt_convert_init(cc, &new_io->ctx, NULL,
1134 io->base_bio, sector);
1135 new_io->ctx.idx_in = io->ctx.idx_in;
1136 new_io->ctx.offset_in = io->ctx.offset_in;
1137
1138 /*
1139 * Fragments after the first use the base_io
1140 * pending count.
1141 */
1142 if (!io->base_io)
1143 new_io->base_io = io;
1144 else {
1145 new_io->base_io = io->base_io;
1146 crypt_inc_pending(io->base_io);
1147 crypt_dec_pending(io);
1148 }
1149
1150 io = new_io;
1151 }
Milan Broz8b004452006-10-03 01:15:37 -07001152 }
Milan Broz899c95d2008-02-08 02:11:02 +00001153
1154 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001155}
1156
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001157static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001158{
Milan Broz5742fd72008-02-08 02:10:43 +00001159 crypt_dec_pending(io);
1160}
1161
Milan Broz4e4eef62008-02-08 02:10:49 +00001162static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001163{
1164 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +00001165 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001166
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001167 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001168
Milan Broz53017032008-02-08 02:10:38 +00001169 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001170 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001171
Milan Broz5742fd72008-02-08 02:10:43 +00001172 r = crypt_convert(cc, &io->ctx);
1173
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001174 if (r < 0)
1175 io->error = -EIO;
1176
Mikulas Patocka815bc972012-03-27 13:17:56 -07001177 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001178 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001179
1180 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001181}
1182
Milan Broz95497a92008-02-08 02:11:12 +00001183static void kcryptd_async_done(struct crypto_async_request *async_req,
1184 int error)
1185{
Huang Yingb2174ee2009-03-16 17:44:33 +00001186 struct dm_crypt_request *dmreq = async_req->data;
1187 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001188 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1189 struct crypt_config *cc = io->target->private;
1190
1191 if (error == -EINPROGRESS) {
1192 complete(&ctx->restart);
1193 return;
1194 }
1195
Milan Broz2dc53272011-01-13 19:59:54 +00001196 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1197 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1198
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001199 if (error < 0)
1200 io->error = -EIO;
1201
Huang Yingb2174ee2009-03-16 17:44:33 +00001202 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001203
Mikulas Patocka815bc972012-03-27 13:17:56 -07001204 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001205 return;
1206
1207 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001208 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001209 else
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001210 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001211}
1212
Milan Broz4e4eef62008-02-08 02:10:49 +00001213static void kcryptd_crypt(struct work_struct *work)
1214{
1215 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1216
1217 if (bio_data_dir(io->base_bio) == READ)
1218 kcryptd_crypt_read_convert(io);
1219 else
1220 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001221}
1222
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001223static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1224{
1225 struct crypt_config *cc = io->target->private;
1226
1227 INIT_WORK(&io->work, kcryptd_crypt);
1228 queue_work(cc->crypt_queue, &io->work);
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Decode key from its hex representation
1233 */
1234static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1235{
1236 char buffer[3];
1237 char *endp;
1238 unsigned int i;
1239
1240 buffer[2] = '\0';
1241
Milan Broz8b004452006-10-03 01:15:37 -07001242 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 buffer[0] = *hex++;
1244 buffer[1] = *hex++;
1245
1246 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
1247
1248 if (endp != &buffer[2])
1249 return -EINVAL;
1250 }
1251
1252 if (*hex != '\0')
1253 return -EINVAL;
1254
1255 return 0;
1256}
1257
1258/*
1259 * Encode key into its hex representation
1260 */
1261static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
1262{
1263 unsigned int i;
1264
Milan Broz8b004452006-10-03 01:15:37 -07001265 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sprintf(hex, "%02x", *key);
1267 hex += 2;
1268 key++;
1269 }
1270}
1271
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001272static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001273{
Milan Brozd1f96422011-01-13 19:59:54 +00001274 unsigned i;
1275
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001276 if (!cc->tfms)
1277 return;
1278
Milan Brozd1f96422011-01-13 19:59:54 +00001279 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001280 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1281 crypto_free_ablkcipher(cc->tfms[i]);
1282 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001283 }
1284}
1285
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001286static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001287{
Milan Brozd1f96422011-01-13 19:59:54 +00001288 unsigned i;
1289 int err;
1290
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001291 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1292 GFP_KERNEL);
1293 if (!cc->tfms)
1294 return -ENOMEM;
1295
Milan Brozd1f96422011-01-13 19:59:54 +00001296 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001297 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1298 if (IS_ERR(cc->tfms[i])) {
1299 err = PTR_ERR(cc->tfms[i]);
1300 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001301 return err;
1302 }
1303 }
1304
1305 return 0;
1306}
1307
Andi Kleenc0297722011-01-13 19:59:53 +00001308static int crypt_setkey_allcpus(struct crypt_config *cc)
1309{
Milan Brozd1f96422011-01-13 19:59:54 +00001310 unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count);
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001311 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001312
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001313 for (i = 0; i < cc->tfms_count; i++) {
1314 r = crypto_ablkcipher_setkey(cc->tfms[i],
1315 cc->key + (i * subkey_size),
1316 subkey_size);
1317 if (r)
1318 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001319 }
1320
1321 return err;
1322}
1323
Milan Broze48d4bb2006-10-03 01:15:37 -07001324static int crypt_set_key(struct crypt_config *cc, char *key)
1325{
Milan Brozde8be5a2011-03-24 13:54:27 +00001326 int r = -EINVAL;
1327 int key_string_len = strlen(key);
1328
Milan Broz69a8cfc2011-01-13 19:59:49 +00001329 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001330 if (cc->key_size != (key_string_len >> 1))
1331 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001332
Milan Broz69a8cfc2011-01-13 19:59:49 +00001333 /* Hyphen (which gives a key_size of zero) means there is no key. */
1334 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001335 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001336
Milan Broz69a8cfc2011-01-13 19:59:49 +00001337 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001338 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001339
1340 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1341
Milan Brozde8be5a2011-03-24 13:54:27 +00001342 r = crypt_setkey_allcpus(cc);
1343
1344out:
1345 /* Hex key string not needed after here, so wipe it. */
1346 memset(key, '0', key_string_len);
1347
1348 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001349}
1350
1351static int crypt_wipe_key(struct crypt_config *cc)
1352{
1353 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1354 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001355
1356 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001357}
1358
Milan Broz28513fc2010-08-12 04:14:06 +01001359static void crypt_dtr(struct dm_target *ti)
1360{
1361 struct crypt_config *cc = ti->private;
Andi Kleenc0297722011-01-13 19:59:53 +00001362 struct crypt_cpu *cpu_cc;
1363 int cpu;
Milan Broz28513fc2010-08-12 04:14:06 +01001364
1365 ti->private = NULL;
1366
1367 if (!cc)
1368 return;
1369
1370 if (cc->io_queue)
1371 destroy_workqueue(cc->io_queue);
1372 if (cc->crypt_queue)
1373 destroy_workqueue(cc->crypt_queue);
1374
Andi Kleenc0297722011-01-13 19:59:53 +00001375 if (cc->cpu)
1376 for_each_possible_cpu(cpu) {
1377 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
1378 if (cpu_cc->req)
1379 mempool_free(cpu_cc->req, cc->req_pool);
Andi Kleenc0297722011-01-13 19:59:53 +00001380 }
1381
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001382 crypt_free_tfms(cc);
1383
Milan Broz28513fc2010-08-12 04:14:06 +01001384 if (cc->bs)
1385 bioset_free(cc->bs);
1386
1387 if (cc->page_pool)
1388 mempool_destroy(cc->page_pool);
1389 if (cc->req_pool)
1390 mempool_destroy(cc->req_pool);
1391 if (cc->io_pool)
1392 mempool_destroy(cc->io_pool);
1393
1394 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1395 cc->iv_gen_ops->dtr(cc);
1396
Milan Broz28513fc2010-08-12 04:14:06 +01001397 if (cc->dev)
1398 dm_put_device(ti, cc->dev);
1399
Andi Kleenc0297722011-01-13 19:59:53 +00001400 if (cc->cpu)
1401 free_percpu(cc->cpu);
1402
Milan Broz5ebaee62010-08-12 04:14:07 +01001403 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001404 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001405
1406 /* Must zero key material before freeing */
1407 kzfree(cc);
1408}
1409
Milan Broz5ebaee62010-08-12 04:14:07 +01001410static int crypt_ctr_cipher(struct dm_target *ti,
1411 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Milan Broz5ebaee62010-08-12 04:14:07 +01001413 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001414 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001415 char *cipher_api = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001416 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Milan Broz5ebaee62010-08-12 04:14:07 +01001418 /* Convert to crypto api definition? */
1419 if (strchr(cipher_in, '(')) {
1420 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EINVAL;
1422 }
1423
Milan Broz7dbcd132011-01-13 19:59:52 +00001424 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1425 if (!cc->cipher_string)
1426 goto bad_mem;
1427
Milan Broz5ebaee62010-08-12 04:14:07 +01001428 /*
1429 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001430 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001431 */
1432 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001433 keycount = strsep(&tmp, "-");
1434 cipher = strsep(&keycount, ":");
1435
1436 if (!keycount)
1437 cc->tfms_count = 1;
1438 else if (sscanf(keycount, "%u", &cc->tfms_count) != 1 ||
1439 !is_power_of_2(cc->tfms_count)) {
1440 ti->error = "Bad cipher key count specification";
1441 return -EINVAL;
1442 }
1443 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01001444
1445 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1446 if (!cc->cipher)
1447 goto bad_mem;
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 chainmode = strsep(&tmp, "-");
1450 ivopts = strsep(&tmp, "-");
1451 ivmode = strsep(&ivopts, ":");
1452
1453 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001454 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001456 cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)),
Milan Brozd1f96422011-01-13 19:59:54 +00001457 __alignof__(struct crypt_cpu));
Andi Kleenc0297722011-01-13 19:59:53 +00001458 if (!cc->cpu) {
1459 ti->error = "Cannot allocate per cpu state";
1460 goto bad_mem;
1461 }
1462
Milan Broz7dbcd132011-01-13 19:59:52 +00001463 /*
1464 * For compatibility with the original dm-crypt mapping format, if
1465 * only the cipher name is supplied, use cbc-plain.
1466 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001467 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 chainmode = "cbc";
1469 ivmode = "plain";
1470 }
1471
Herbert Xud1806f62006-08-22 20:29:17 +10001472 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001473 ti->error = "IV mechanism required";
1474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476
Milan Broz5ebaee62010-08-12 04:14:07 +01001477 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1478 if (!cipher_api)
1479 goto bad_mem;
1480
1481 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1482 "%s(%s)", chainmode, cipher);
1483 if (ret < 0) {
1484 kfree(cipher_api);
1485 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001486 }
1487
Milan Broz5ebaee62010-08-12 04:14:07 +01001488 /* Allocate cipher */
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001489 ret = crypt_alloc_tfms(cc, cipher_api);
1490 if (ret < 0) {
1491 ti->error = "Error allocating crypto tfm";
1492 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Milan Broz5ebaee62010-08-12 04:14:07 +01001495 /* Initialize and set key */
1496 ret = crypt_set_key(cc, key);
Milan Broz28513fc2010-08-12 04:14:06 +01001497 if (ret < 0) {
Milan Broz0b430952009-12-10 23:51:55 +00001498 ti->error = "Error decoding and setting key";
Milan Broz28513fc2010-08-12 04:14:06 +01001499 goto bad;
Milan Broz0b430952009-12-10 23:51:55 +00001500 }
1501
Milan Broz5ebaee62010-08-12 04:14:07 +01001502 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001503 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001504 if (cc->iv_size)
1505 /* at least a 64 bit sector number should fit in our buffer */
1506 cc->iv_size = max(cc->iv_size,
1507 (unsigned int)(sizeof(u64) / sizeof(u8)));
1508 else if (ivmode) {
1509 DMWARN("Selected cipher does not support IVs");
1510 ivmode = NULL;
1511 }
1512
1513 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (ivmode == NULL)
1515 cc->iv_gen_ops = NULL;
1516 else if (strcmp(ivmode, "plain") == 0)
1517 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001518 else if (strcmp(ivmode, "plain64") == 0)
1519 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 else if (strcmp(ivmode, "essiv") == 0)
1521 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001522 else if (strcmp(ivmode, "benbi") == 0)
1523 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001524 else if (strcmp(ivmode, "null") == 0)
1525 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001526 else if (strcmp(ivmode, "lmk") == 0) {
1527 cc->iv_gen_ops = &crypt_iv_lmk_ops;
1528 /* Version 2 and 3 is recognised according
1529 * to length of provided multi-key string.
1530 * If present (version 3), last key is used as IV seed.
1531 */
1532 if (cc->key_size % cc->key_parts)
1533 cc->key_parts++;
1534 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001535 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001536 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001537 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Milan Broz28513fc2010-08-12 04:14:06 +01001540 /* Allocate IV */
1541 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1542 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1543 if (ret < 0) {
1544 ti->error = "Error creating IV";
1545 goto bad;
1546 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001547 }
1548
Milan Broz28513fc2010-08-12 04:14:06 +01001549 /* Initialize IV (set keys for ESSIV etc) */
1550 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1551 ret = cc->iv_gen_ops->init(cc);
1552 if (ret < 0) {
1553 ti->error = "Error initialising IV";
1554 goto bad;
1555 }
1556 }
1557
Milan Broz5ebaee62010-08-12 04:14:07 +01001558 ret = 0;
1559bad:
1560 kfree(cipher_api);
1561 return ret;
1562
1563bad_mem:
1564 ti->error = "Cannot allocate cipher strings";
1565 return -ENOMEM;
1566}
1567
1568/*
1569 * Construct an encryption mapping:
1570 * <cipher> <key> <iv_offset> <dev_path> <start>
1571 */
1572static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1573{
1574 struct crypt_config *cc;
1575 unsigned int key_size;
1576 unsigned long long tmpll;
1577 int ret;
1578
1579 if (argc != 5) {
1580 ti->error = "Not enough arguments";
1581 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
Milan Broz5ebaee62010-08-12 04:14:07 +01001584 key_size = strlen(argv[1]) >> 1;
1585
1586 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1587 if (!cc) {
1588 ti->error = "Cannot allocate encryption context";
1589 return -ENOMEM;
1590 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001591 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001592
1593 ti->private = cc;
1594 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1595 if (ret < 0)
1596 goto bad;
1597
Milan Broz28513fc2010-08-12 04:14:06 +01001598 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001599 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001601 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001602 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Milan Brozddd42ed2008-02-08 02:11:07 +00001605 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001606 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001607 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001608 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001609 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001610
1611 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1612 sizeof(struct dm_crypt_request) + cc->iv_size);
1613 if (!cc->req_pool) {
1614 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001615 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001616 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001617
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001618 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001620 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001621 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
Jens Axboebb799ca2008-12-10 15:35:05 +01001624 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001625 if (!cc->bs) {
1626 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001627 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001628 }
1629
Milan Broz28513fc2010-08-12 04:14:06 +01001630 ret = -EINVAL;
Andrew Morton4ee218c2006-03-27 01:17:48 -08001631 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001632 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001633 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001635 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Milan Broz28513fc2010-08-12 04:14:06 +01001637 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1638 ti->error = "Device lookup failed";
1639 goto bad;
1640 }
1641
Andrew Morton4ee218c2006-03-27 01:17:48 -08001642 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001643 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001644 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001646 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Milan Broz28513fc2010-08-12 04:14:06 +01001648 ret = -ENOMEM;
Andi Kleenc0297722011-01-13 19:59:53 +00001649 cc->io_queue = alloc_workqueue("kcryptd_io",
1650 WQ_NON_REENTRANT|
1651 WQ_MEM_RECLAIM,
1652 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001653 if (!cc->io_queue) {
1654 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001655 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001656 }
1657
Andi Kleenc0297722011-01-13 19:59:53 +00001658 cc->crypt_queue = alloc_workqueue("kcryptd",
1659 WQ_NON_REENTRANT|
1660 WQ_CPU_INTENSIVE|
1661 WQ_MEM_RECLAIM,
1662 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001663 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001664 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001665 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001666 }
1667
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001668 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670
Milan Broz28513fc2010-08-12 04:14:06 +01001671bad:
1672 crypt_dtr(ti);
1673 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int crypt_map(struct dm_target *ti, struct bio *bio,
1677 union map_info *map_context)
1678{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001679 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001680 struct crypt_config *cc;
1681
Tejun Heod87f4c12010-09-03 11:56:19 +02001682 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001683 cc = ti->private;
1684 bio->bi_bdev = cc->dev->bdev;
1685 return DM_MAPIO_REMAPPED;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001688 io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001689
Milan Broz20c82532011-01-13 19:59:53 +00001690 if (bio_data_dir(io->base_bio) == READ) {
1691 if (kcryptd_io_read(io, GFP_NOWAIT))
1692 kcryptd_queue_io(io);
1693 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001694 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001696 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static int crypt_status(struct dm_target *ti, status_type_t type,
1700 char *result, unsigned int maxlen)
1701{
Milan Broz5ebaee62010-08-12 04:14:07 +01001702 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 unsigned int sz = 0;
1704
1705 switch (type) {
1706 case STATUSTYPE_INFO:
1707 result[0] = '\0';
1708 break;
1709
1710 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001711 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 if (cc->key_size > 0) {
1714 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1715 return -ENOMEM;
1716
1717 crypt_encode_key(result + sz, cc->key, cc->key_size);
1718 sz += cc->key_size << 1;
1719 } else {
1720 if (sz >= maxlen)
1721 return -ENOMEM;
1722 result[sz++] = '-';
1723 }
1724
Andrew Morton4ee218c2006-03-27 01:17:48 -08001725 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1726 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
1728 }
1729 return 0;
1730}
1731
Milan Broze48d4bb2006-10-03 01:15:37 -07001732static void crypt_postsuspend(struct dm_target *ti)
1733{
1734 struct crypt_config *cc = ti->private;
1735
1736 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1737}
1738
1739static int crypt_preresume(struct dm_target *ti)
1740{
1741 struct crypt_config *cc = ti->private;
1742
1743 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1744 DMERR("aborting resume - crypt key is not set.");
1745 return -EAGAIN;
1746 }
1747
1748 return 0;
1749}
1750
1751static void crypt_resume(struct dm_target *ti)
1752{
1753 struct crypt_config *cc = ti->private;
1754
1755 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1756}
1757
1758/* Message interface
1759 * key set <key>
1760 * key wipe
1761 */
1762static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1763{
1764 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001765 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001766
1767 if (argc < 2)
1768 goto error;
1769
1770 if (!strnicmp(argv[0], MESG_STR("key"))) {
1771 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1772 DMWARN("not suspended during key manipulation.");
1773 return -EINVAL;
1774 }
Milan Broz542da312009-12-10 23:51:57 +00001775 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) {
1776 ret = crypt_set_key(cc, argv[2]);
1777 if (ret)
1778 return ret;
1779 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1780 ret = cc->iv_gen_ops->init(cc);
1781 return ret;
1782 }
1783 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) {
1784 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1785 ret = cc->iv_gen_ops->wipe(cc);
1786 if (ret)
1787 return ret;
1788 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001789 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001790 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001791 }
1792
1793error:
1794 DMWARN("unrecognised message received.");
1795 return -EINVAL;
1796}
1797
Milan Brozd41e26b2008-07-21 12:00:40 +01001798static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1799 struct bio_vec *biovec, int max_size)
1800{
1801 struct crypt_config *cc = ti->private;
1802 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1803
1804 if (!q->merge_bvec_fn)
1805 return max_size;
1806
1807 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001808 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001809
1810 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1811}
1812
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001813static int crypt_iterate_devices(struct dm_target *ti,
1814 iterate_devices_callout_fn fn, void *data)
1815{
1816 struct crypt_config *cc = ti->private;
1817
Mike Snitzer5dea2712009-07-23 20:30:42 +01001818 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821static struct target_type crypt_target = {
1822 .name = "crypt",
Milan Brozd1f96422011-01-13 19:59:54 +00001823 .version = {1, 10, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 .module = THIS_MODULE,
1825 .ctr = crypt_ctr,
1826 .dtr = crypt_dtr,
1827 .map = crypt_map,
1828 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001829 .postsuspend = crypt_postsuspend,
1830 .preresume = crypt_preresume,
1831 .resume = crypt_resume,
1832 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001833 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001834 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835};
1836
1837static int __init dm_crypt_init(void)
1838{
1839 int r;
1840
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001841 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!_crypt_io_pool)
1843 return -ENOMEM;
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 r = dm_register_target(&crypt_target);
1846 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001847 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001848 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return r;
1852}
1853
1854static void __exit dm_crypt_exit(void)
1855{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001856 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 kmem_cache_destroy(_crypt_io_pool);
1858}
1859
1860module_init(dm_crypt_init);
1861module_exit(dm_crypt_exit);
1862
1863MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1864MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1865MODULE_LICENSE("GPL");