blob: e0ebe685be6a6e3266faf688757dfdaad7abeb30 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Mikulas Patocka586e80e2008-10-21 17:44:59 +010027#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Alasdair G Kergon72d94862006-06-26 00:27:35 -070029#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070030#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * context holding the current state of a multi-part conversion
34 */
35struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000036 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct bio *bio_in;
38 struct bio *bio_out;
39 unsigned int offset_in;
40 unsigned int offset_out;
41 unsigned int idx_in;
42 unsigned int idx_out;
43 sector_t sector;
Milan Broz43d69032008-02-08 02:11:09 +000044 atomic_t pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Milan Broz53017032008-02-08 02:10:38 +000047/*
48 * per bio private data
49 */
50struct dm_crypt_io {
51 struct dm_target *target;
52 struct bio *base_bio;
53 struct work_struct work;
54
55 struct convert_context ctx;
56
57 atomic_t pending;
58 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000059 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010060 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000061};
62
Milan Broz01482b72008-02-08 02:11:04 +000063struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000064 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000065 struct scatterlist sg_in;
66 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000067 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000068};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct crypt_config;
71
72struct crypt_iv_operations {
73 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010074 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000076 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000077 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000078 int (*generator)(struct crypt_config *cc, u8 *iv,
79 struct dm_crypt_request *dmreq);
80 int (*post)(struct crypt_config *cc, u8 *iv,
81 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Milan Broz60473592009-12-10 23:51:55 +000084struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000085 struct crypto_hash *hash_tfm;
86 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000087};
88
89struct iv_benbi_private {
90 int shift;
91};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * Crypt: maps a linear range of a block device
95 * and encrypts / decrypts at the same time.
96 */
Milan Broze48d4bb2006-10-03 01:15:37 -070097enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +000098
99/*
100 * Duplicated per-CPU state for cipher.
101 */
102struct crypt_cpu {
103 struct ablkcipher_request *req;
104 struct crypto_ablkcipher *tfm;
105
106 /* ESSIV: struct crypto_cipher *essiv_tfm */
107 void *iv_private;
108};
109
110/*
111 * The fields in here must be read only after initialization,
112 * changing state should be in crypt_cpu.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114struct crypt_config {
115 struct dm_dev *dev;
116 sector_t start;
117
118 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000119 * pool for per bio private data, crypto requests and
120 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000123 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700125 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Milan Brozcabf08e2007-10-19 22:38:58 +0100127 struct workqueue_struct *io_queue;
128 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700129
Milan Broz5ebaee62010-08-12 04:14:07 +0100130 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000131 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800134 union {
Milan Broz60473592009-12-10 23:51:55 +0000135 struct iv_essiv_private essiv;
136 struct iv_benbi_private benbi;
Herbert Xu79066ad2006-12-05 13:41:52 -0800137 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sector_t iv_offset;
139 unsigned int iv_size;
140
Milan Brozddd42ed2008-02-08 02:11:07 +0000141 /*
Andi Kleenc0297722011-01-13 19:59:53 +0000142 * Duplicated per cpu state. Access through
143 * per_cpu_ptr() only.
144 */
145 struct crypt_cpu __percpu *cpu;
146
147 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000148 * Layout of each crypto request:
149 *
150 * struct ablkcipher_request
151 * context
152 * padding
153 * struct dm_crypt_request
154 * padding
155 * IV
156 *
157 * The padding is added so that dm_crypt_request and the IV are
158 * correctly aligned.
159 */
160 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000161
Milan Broze48d4bb2006-10-03 01:15:37 -0700162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned int key_size;
164 u8 key[0];
165};
166
Milan Broz6a24c712006-10-03 01:15:40 -0700167#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define MIN_POOL_PAGES 32
169#define MIN_BIO_PAGES 8
170
Christoph Lametere18b8902006-12-06 20:33:20 -0800171static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100173static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000174static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000175static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700176
Andi Kleenc0297722011-01-13 19:59:53 +0000177static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
178{
179 return this_cpu_ptr(cc->cpu);
180}
181
182/*
183 * Use this to access cipher attributes that are the same for each CPU.
184 */
185static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
186{
187 return __this_cpu_ptr(cc->cpu)->tfm;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Different IV generation algorithms:
192 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000193 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200194 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Milan Broz61afef62009-12-10 23:52:25 +0000196 * plain64: the initial vector is the 64-bit little-endian version of the sector
197 * number, padded with zeros if necessary.
198 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000199 * essiv: "encrypted sector|salt initial vector", the sector number is
200 * encrypted with the bulk cipher using a salt as key. The salt
201 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Rik Snel48527fa2006-09-03 08:56:39 +1000203 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
204 * (needed for LRW-32-AES and possible other narrow block modes)
205 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700206 * null: the initial vector is always zero. Provides compatibility with
207 * obsolete loop_fish2 devices. Do not use for new devices.
208 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * plumb: unimplemented, see:
210 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
211 */
212
Milan Broz2dc53272011-01-13 19:59:54 +0000213static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
214 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000217 *(u32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 return 0;
220}
221
Milan Broz61afef62009-12-10 23:52:25 +0000222static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000223 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000224{
225 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000226 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000227
228 return 0;
229}
230
Milan Brozb95bf2d2009-12-10 23:51:56 +0000231/* Initialise ESSIV - compute salt but no local memory allocations */
232static int crypt_iv_essiv_init(struct crypt_config *cc)
233{
234 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
235 struct hash_desc desc;
236 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000237 struct crypto_cipher *essiv_tfm;
238 int err, cpu;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000239
240 sg_init_one(&sg, cc->key, cc->key_size);
241 desc.tfm = essiv->hash_tfm;
242 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
243
244 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
245 if (err)
246 return err;
247
Andi Kleenc0297722011-01-13 19:59:53 +0000248 for_each_possible_cpu(cpu) {
249 essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private,
250
251 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000252 crypto_hash_digestsize(essiv->hash_tfm));
Andi Kleenc0297722011-01-13 19:59:53 +0000253 if (err)
254 return err;
255 }
256
257 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000258}
259
Milan Broz542da312009-12-10 23:51:57 +0000260/* Wipe salt and reset key derived from volume key */
261static int crypt_iv_essiv_wipe(struct crypt_config *cc)
262{
263 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
264 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000265 struct crypto_cipher *essiv_tfm;
266 int cpu, r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000267
268 memset(essiv->salt, 0, salt_size);
269
Andi Kleenc0297722011-01-13 19:59:53 +0000270 for_each_possible_cpu(cpu) {
271 essiv_tfm = per_cpu_ptr(cc->cpu, cpu)->iv_private;
272 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
273 if (r)
274 err = r;
275 }
276
277 return err;
278}
279
280/* Set up per cpu cipher state */
281static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
282 struct dm_target *ti,
283 u8 *salt, unsigned saltsize)
284{
285 struct crypto_cipher *essiv_tfm;
286 int err;
287
288 /* Setup the essiv_tfm with the given salt */
289 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
290 if (IS_ERR(essiv_tfm)) {
291 ti->error = "Error allocating crypto tfm for ESSIV";
292 return essiv_tfm;
293 }
294
295 if (crypto_cipher_blocksize(essiv_tfm) !=
296 crypto_ablkcipher_ivsize(any_tfm(cc))) {
297 ti->error = "Block size of ESSIV cipher does "
298 "not match IV size of block cipher";
299 crypto_free_cipher(essiv_tfm);
300 return ERR_PTR(-EINVAL);
301 }
302
303 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
304 if (err) {
305 ti->error = "Failed to set key for ESSIV cipher";
306 crypto_free_cipher(essiv_tfm);
307 return ERR_PTR(err);
308 }
309
310 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000311}
312
Milan Broz60473592009-12-10 23:51:55 +0000313static void crypt_iv_essiv_dtr(struct crypt_config *cc)
314{
Andi Kleenc0297722011-01-13 19:59:53 +0000315 int cpu;
316 struct crypt_cpu *cpu_cc;
317 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000318 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
319
Milan Brozb95bf2d2009-12-10 23:51:56 +0000320 crypto_free_hash(essiv->hash_tfm);
321 essiv->hash_tfm = NULL;
322
323 kzfree(essiv->salt);
324 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000325
326 for_each_possible_cpu(cpu) {
327 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
328 essiv_tfm = cpu_cc->iv_private;
329
330 if (essiv_tfm)
331 crypto_free_cipher(essiv_tfm);
332
333 cpu_cc->iv_private = NULL;
334 }
Milan Broz60473592009-12-10 23:51:55 +0000335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100338 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Milan Broz5861f1b2009-12-10 23:51:56 +0000340 struct crypto_cipher *essiv_tfm = NULL;
341 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000342 u8 *salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000343 int err, cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Milan Broz5861f1b2009-12-10 23:51:56 +0000345 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700346 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -EINVAL;
348 }
349
Milan Brozb95bf2d2009-12-10 23:51:56 +0000350 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000351 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
352 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700353 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000354 err = PTR_ERR(hash_tfm);
355 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Milan Brozb95bf2d2009-12-10 23:51:56 +0000358 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000359 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700360 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000361 err = -ENOMEM;
362 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Milan Brozb95bf2d2009-12-10 23:51:56 +0000365 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000366 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
367
Andi Kleenc0297722011-01-13 19:59:53 +0000368 for_each_possible_cpu(cpu) {
369 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
370 crypto_hash_digestsize(hash_tfm));
371 if (IS_ERR(essiv_tfm)) {
372 crypt_iv_essiv_dtr(cc);
373 return PTR_ERR(essiv_tfm);
374 }
375 per_cpu_ptr(cc->cpu, cpu)->iv_private = essiv_tfm;
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000379
380bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000381 if (hash_tfm && !IS_ERR(hash_tfm))
382 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000383 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Milan Broz2dc53272011-01-13 19:59:54 +0000387static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
388 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Andi Kleenc0297722011-01-13 19:59:53 +0000390 struct crypto_cipher *essiv_tfm = this_crypt_config(cc)->iv_private;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000393 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000394 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
Rik Snel48527fa2006-09-03 08:56:39 +1000399static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
400 const char *opts)
401{
Andi Kleenc0297722011-01-13 19:59:53 +0000402 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800403 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000404
405 /* we need to calculate how far we must shift the sector count
406 * to get the cipher block count, we use this shift in _gen */
407
408 if (1 << log != bs) {
409 ti->error = "cypher blocksize is not a power of 2";
410 return -EINVAL;
411 }
412
413 if (log > 9) {
414 ti->error = "cypher blocksize is > 512";
415 return -EINVAL;
416 }
417
Milan Broz60473592009-12-10 23:51:55 +0000418 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000419
420 return 0;
421}
422
423static void crypt_iv_benbi_dtr(struct crypt_config *cc)
424{
Rik Snel48527fa2006-09-03 08:56:39 +1000425}
426
Milan Broz2dc53272011-01-13 19:59:54 +0000427static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
428 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000429{
Herbert Xu79066ad2006-12-05 13:41:52 -0800430 __be64 val;
431
Rik Snel48527fa2006-09-03 08:56:39 +1000432 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800433
Milan Broz2dc53272011-01-13 19:59:54 +0000434 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800435 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
Milan Broz2dc53272011-01-13 19:59:54 +0000440static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
441 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700442{
443 memset(iv, 0, cc->iv_size);
444
445 return 0;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static struct crypt_iv_operations crypt_iv_plain_ops = {
449 .generator = crypt_iv_plain_gen
450};
451
Milan Broz61afef62009-12-10 23:52:25 +0000452static struct crypt_iv_operations crypt_iv_plain64_ops = {
453 .generator = crypt_iv_plain64_gen
454};
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static struct crypt_iv_operations crypt_iv_essiv_ops = {
457 .ctr = crypt_iv_essiv_ctr,
458 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000459 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000460 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .generator = crypt_iv_essiv_gen
462};
463
Rik Snel48527fa2006-09-03 08:56:39 +1000464static struct crypt_iv_operations crypt_iv_benbi_ops = {
465 .ctr = crypt_iv_benbi_ctr,
466 .dtr = crypt_iv_benbi_dtr,
467 .generator = crypt_iv_benbi_gen
468};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Ludwig Nussel46b47732007-05-09 02:32:55 -0700470static struct crypt_iv_operations crypt_iv_null_ops = {
471 .generator = crypt_iv_null_gen
472};
473
Milan Brozd469f842007-10-19 22:42:37 +0100474static void crypt_convert_init(struct crypt_config *cc,
475 struct convert_context *ctx,
476 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000477 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 ctx->bio_in = bio_in;
480 ctx->bio_out = bio_out;
481 ctx->offset_in = 0;
482 ctx->offset_out = 0;
483 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
484 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
485 ctx->sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000486 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Huang Yingb2174ee2009-03-16 17:44:33 +0000489static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
490 struct ablkcipher_request *req)
491{
492 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
493}
494
495static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
496 struct dm_crypt_request *dmreq)
497{
498 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
499}
500
Milan Broz2dc53272011-01-13 19:59:54 +0000501static u8 *iv_of_dmreq(struct crypt_config *cc,
502 struct dm_crypt_request *dmreq)
503{
504 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
505 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
506}
507
Milan Broz01482b72008-02-08 02:11:04 +0000508static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000509 struct convert_context *ctx,
510 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000511{
512 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
513 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000514 struct dm_crypt_request *dmreq;
515 u8 *iv;
516 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000517
Huang Yingb2174ee2009-03-16 17:44:33 +0000518 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000519 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000520
Milan Broz2dc53272011-01-13 19:59:54 +0000521 dmreq->iv_sector = ctx->sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000522 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000523 sg_init_table(&dmreq->sg_in, 1);
524 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000525 bv_in->bv_offset + ctx->offset_in);
526
Milan Broz3a7f6c92008-02-08 02:11:14 +0000527 sg_init_table(&dmreq->sg_out, 1);
528 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000529 bv_out->bv_offset + ctx->offset_out);
530
531 ctx->offset_in += 1 << SECTOR_SHIFT;
532 if (ctx->offset_in >= bv_in->bv_len) {
533 ctx->offset_in = 0;
534 ctx->idx_in++;
535 }
536
537 ctx->offset_out += 1 << SECTOR_SHIFT;
538 if (ctx->offset_out >= bv_out->bv_len) {
539 ctx->offset_out = 0;
540 ctx->idx_out++;
541 }
542
Milan Broz3a7f6c92008-02-08 02:11:14 +0000543 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000544 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000545 if (r < 0)
546 return r;
547 }
548
549 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
550 1 << SECTOR_SHIFT, iv);
551
552 if (bio_data_dir(ctx->bio_in) == WRITE)
553 r = crypto_ablkcipher_encrypt(req);
554 else
555 r = crypto_ablkcipher_decrypt(req);
556
Milan Broz2dc53272011-01-13 19:59:54 +0000557 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
558 r = cc->iv_gen_ops->post(cc, iv, dmreq);
559
Milan Broz3a7f6c92008-02-08 02:11:14 +0000560 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000561}
562
Milan Broz95497a92008-02-08 02:11:12 +0000563static void kcryptd_async_done(struct crypto_async_request *async_req,
564 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000565
Milan Brozddd42ed2008-02-08 02:11:07 +0000566static void crypt_alloc_req(struct crypt_config *cc,
567 struct convert_context *ctx)
568{
Andi Kleenc0297722011-01-13 19:59:53 +0000569 struct crypt_cpu *this_cc = this_crypt_config(cc);
570
571 if (!this_cc->req)
572 this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
573
574 ablkcipher_request_set_tfm(this_cc->req, this_cc->tfm);
575 ablkcipher_request_set_callback(this_cc->req,
576 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
577 kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
581 * Encrypt / decrypt data from one bio to another one (can be the same one)
582 */
583static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100584 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Andi Kleenc0297722011-01-13 19:59:53 +0000586 struct crypt_cpu *this_cc = this_crypt_config(cc);
Milan Broz3f1e9072008-03-28 14:16:07 -0700587 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Milan Brozc8081612008-10-10 13:37:08 +0100589 atomic_set(&ctx->pending, 1);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
592 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Milan Broz3a7f6c92008-02-08 02:11:14 +0000594 crypt_alloc_req(cc, ctx);
595
Milan Broz3f1e9072008-03-28 14:16:07 -0700596 atomic_inc(&ctx->pending);
597
Andi Kleenc0297722011-01-13 19:59:53 +0000598 r = crypt_convert_block(cc, ctx, this_cc->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000599
600 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700601 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000602 case -EBUSY:
603 wait_for_completion(&ctx->restart);
604 INIT_COMPLETION(ctx->restart);
605 /* fall through*/
606 case -EINPROGRESS:
Andi Kleenc0297722011-01-13 19:59:53 +0000607 this_cc->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000608 ctx->sector++;
609 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000610
Milan Broz3f1e9072008-03-28 14:16:07 -0700611 /* sync */
612 case 0:
613 atomic_dec(&ctx->pending);
614 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100615 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700616 continue;
617
618 /* error */
619 default:
620 atomic_dec(&ctx->pending);
621 return r;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Milan Broz3f1e9072008-03-28 14:16:07 -0700625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Milan Brozd469f842007-10-19 22:42:37 +0100628static void dm_crypt_bio_destructor(struct bio *bio)
629{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100630 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700631 struct crypt_config *cc = io->target->private;
632
633 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100634}
Milan Broz6a24c712006-10-03 01:15:40 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/*
637 * Generate a new unfragmented bio with the given size
638 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100639 * May return a smaller bio when running out of pages, indicated by
640 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Milan Broz933f01d2008-10-10 13:37:08 +0100642static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
643 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Olaf Kirch027581f2007-05-09 02:32:52 -0700645 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700646 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400648 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000649 unsigned i, len;
650 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700652 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700653 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Olaf Kirch027581f2007-05-09 02:32:52 -0700656 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100657 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700658
Olaf Kirchf97380b2007-05-09 02:32:54 -0700659 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000660 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100661 if (!page) {
662 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /*
667 * if additional pages cannot be allocated without waiting,
668 * return a partially allocated bio, the caller will then try
669 * to allocate additional bios while submitting this partial bio
670 */
Olaf Kirchf97380b2007-05-09 02:32:54 -0700671 if (i == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
673
Milan Broz91e10622007-12-13 14:16:10 +0000674 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Milan Broz91e10622007-12-13 14:16:10 +0000676 if (!bio_add_page(clone, page, len, 0)) {
677 mempool_free(page, cc->page_pool);
678 break;
679 }
680
681 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
Milan Broz8b004452006-10-03 01:15:37 -0700684 if (!clone->bi_size) {
685 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return NULL;
687 }
688
Milan Broz8b004452006-10-03 01:15:37 -0700689 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Neil Brown644bd2f2007-10-16 13:48:46 +0200692static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Neil Brown644bd2f2007-10-16 13:48:46 +0200694 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct bio_vec *bv;
696
Neil Brown644bd2f2007-10-16 13:48:46 +0200697 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700698 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 BUG_ON(!bv->bv_page);
700 mempool_free(bv->bv_page, cc->page_pool);
701 bv->bv_page = NULL;
702 }
703}
704
Milan Brozdc440d1e2008-10-10 13:37:03 +0100705static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
706 struct bio *bio, sector_t sector)
707{
708 struct crypt_config *cc = ti->private;
709 struct dm_crypt_io *io;
710
711 io = mempool_alloc(cc->io_pool, GFP_NOIO);
712 io->target = ti;
713 io->base_bio = bio;
714 io->sector = sector;
715 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100716 io->base_io = NULL;
Milan Brozdc440d1e2008-10-10 13:37:03 +0100717 atomic_set(&io->pending, 0);
718
719 return io;
720}
721
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100722static void crypt_inc_pending(struct dm_crypt_io *io)
723{
724 atomic_inc(&io->pending);
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * One of the bios was finished. Check for completion of
729 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100730 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Milan Broz5742fd72008-02-08 02:10:43 +0000732static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Milan Broz5742fd72008-02-08 02:10:43 +0000734 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000735 struct bio *base_bio = io->base_bio;
736 struct dm_crypt_io *base_io = io->base_io;
737 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (!atomic_dec_and_test(&io->pending))
740 return;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000743
744 if (likely(!base_io))
745 bio_endio(base_bio, error);
746 else {
747 if (error && !base_io->error)
748 base_io->error = error;
749 crypt_dec_pending(base_io);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100754 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
756 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700757 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100758 *
759 * kcryptd performs the actual encryption or decryption.
760 *
761 * kcryptd_io performs the IO submission.
762 *
763 * They must be separated as otherwise the final stages could be
764 * starved by new requests which can block in the first stages due
765 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +0000766 *
767 * The work is done per CPU global for all dm-crypt instances.
768 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200770static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700771{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100772 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700773 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000774 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700775
Milan Brozadfe4772007-12-13 14:15:51 +0000776 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
777 error = -EIO;
778
Milan Broz8b004452006-10-03 01:15:37 -0700779 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200780 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700781 */
Milan Brozee7a4912008-02-08 02:10:46 +0000782 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200783 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000784
785 bio_put(clone);
786
787 if (rw == READ && !error) {
788 kcryptd_queue_crypt(io);
789 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200790 }
Milan Broz8b004452006-10-03 01:15:37 -0700791
Milan Brozadfe4772007-12-13 14:15:51 +0000792 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000793 io->error = error;
794
795 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700796}
797
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100798static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700799{
800 struct crypt_config *cc = io->target->private;
801
802 clone->bi_private = io;
803 clone->bi_end_io = crypt_endio;
804 clone->bi_bdev = cc->dev->bdev;
805 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700806 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700807}
808
Milan Broz20c82532011-01-13 19:59:53 +0000809static void kcryptd_unplug(struct crypt_config *cc)
810{
811 blk_unplug(bdev_get_queue(cc->dev->bdev));
812}
813
814static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -0700815{
816 struct crypt_config *cc = io->target->private;
817 struct bio *base_bio = io->base_bio;
818 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700819
Milan Broz8b004452006-10-03 01:15:37 -0700820 /*
821 * The block layer might modify the bvec array, so always
822 * copy the required bvecs because we need the original
823 * one in order to decrypt the whole bio data *afterwards*.
824 */
Milan Broz20c82532011-01-13 19:59:53 +0000825 clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs);
826 if (!clone) {
827 kcryptd_unplug(cc);
828 return 1;
Milan Broz93e605c2006-10-03 01:15:38 -0700829 }
Milan Broz8b004452006-10-03 01:15:37 -0700830
Milan Broz20c82532011-01-13 19:59:53 +0000831 crypt_inc_pending(io);
832
Milan Broz8b004452006-10-03 01:15:37 -0700833 clone_init(io, clone);
834 clone->bi_idx = 0;
835 clone->bi_vcnt = bio_segments(base_bio);
836 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000837 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700838 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
839 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700840
Milan Broz93e605c2006-10-03 01:15:38 -0700841 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +0000842 return 0;
Milan Broz8b004452006-10-03 01:15:37 -0700843}
844
Milan Broz4e4eef62008-02-08 02:10:49 +0000845static void kcryptd_io_write(struct dm_crypt_io *io)
846{
Milan Broz95497a92008-02-08 02:11:12 +0000847 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +0000848 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000849}
850
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000851static void kcryptd_io(struct work_struct *work)
852{
853 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
854
Milan Broz20c82532011-01-13 19:59:53 +0000855 if (bio_data_dir(io->base_bio) == READ) {
856 crypt_inc_pending(io);
857 if (kcryptd_io_read(io, GFP_NOIO))
858 io->error = -ENOMEM;
859 crypt_dec_pending(io);
860 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000861 kcryptd_io_write(io);
862}
863
864static void kcryptd_queue_io(struct dm_crypt_io *io)
865{
866 struct crypt_config *cc = io->target->private;
867
868 INIT_WORK(&io->work, kcryptd_io);
869 queue_work(cc->io_queue, &io->work);
870}
871
Milan Broz95497a92008-02-08 02:11:12 +0000872static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io,
873 int error, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +0000874{
Milan Brozdec1ced2008-02-08 02:10:57 +0000875 struct bio *clone = io->ctx.bio_out;
876 struct crypt_config *cc = io->target->private;
877
878 if (unlikely(error < 0)) {
879 crypt_free_buffer_pages(cc, clone);
880 bio_put(clone);
881 io->error = -EIO;
Milan Broz6c031f42008-10-10 13:37:06 +0100882 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000883 return;
884 }
885
886 /* crypt_convert should have filled the clone bio */
887 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
888
889 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +0000890
Milan Broz95497a92008-02-08 02:11:12 +0000891 if (async)
892 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +0100893 else
Milan Broz95497a92008-02-08 02:11:12 +0000894 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +0000895}
896
Milan Brozfc5a5e92008-10-10 13:37:04 +0100897static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700898{
899 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700900 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +0100901 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +0100902 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +0100903 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +0000904 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100905 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +0000906 int r;
Milan Broz8b004452006-10-03 01:15:37 -0700907
Milan Broz93e605c2006-10-03 01:15:38 -0700908 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +0100909 * Prevent io from disappearing until this function completes.
910 */
911 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +0100912 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +0100913
914 /*
Milan Broz93e605c2006-10-03 01:15:38 -0700915 * The allocated buffers can be smaller than the whole bio,
916 * so repeat the whole process until all the data can be handled.
917 */
918 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +0100919 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -0700920 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +0000921 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +0100922 break;
Milan Broz23541d22006-10-03 01:15:39 -0700923 }
Milan Broz93e605c2006-10-03 01:15:38 -0700924
Milan Broz53017032008-02-08 02:10:38 +0000925 io->ctx.bio_out = clone;
926 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -0700927
Milan Broz93e605c2006-10-03 01:15:38 -0700928 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +0100929 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +0000930
Milan Broz4e594092008-10-10 13:37:07 +0100931 crypt_inc_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +0000932 r = crypt_convert(cc, &io->ctx);
Milan Brozc8081612008-10-10 13:37:08 +0100933 crypt_finished = atomic_dec_and_test(&io->ctx.pending);
Milan Brozdec1ced2008-02-08 02:10:57 +0000934
Milan Brozc8081612008-10-10 13:37:08 +0100935 /* Encryption was already finished, submit io now */
936 if (crypt_finished) {
Milan Broz3a7f6c92008-02-08 02:11:14 +0000937 kcryptd_crypt_write_io_submit(io, r, 0);
Milan Brozc8081612008-10-10 13:37:08 +0100938
939 /*
940 * If there was an error, do not try next fragments.
941 * For async, error is processed in async handler.
942 */
Milan Broz6c031f42008-10-10 13:37:06 +0100943 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +0100944 break;
Milan Brozb635b002008-10-21 17:45:00 +0100945
946 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +0100947 }
Milan Broz93e605c2006-10-03 01:15:38 -0700948
Milan Broz933f01d2008-10-10 13:37:08 +0100949 /*
950 * Out of memory -> run queues
951 * But don't wait if split was due to the io size restriction
952 */
953 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +0200954 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +0100955
Milan Broz393b47e2008-10-21 17:45:02 +0100956 /*
957 * With async crypto it is unsafe to share the crypto context
958 * between fragments, so switch to a new dm_crypt_io structure.
959 */
960 if (unlikely(!crypt_finished && remaining)) {
961 new_io = crypt_io_alloc(io->target, io->base_bio,
962 sector);
963 crypt_inc_pending(new_io);
964 crypt_convert_init(cc, &new_io->ctx, NULL,
965 io->base_bio, sector);
966 new_io->ctx.idx_in = io->ctx.idx_in;
967 new_io->ctx.offset_in = io->ctx.offset_in;
968
969 /*
970 * Fragments after the first use the base_io
971 * pending count.
972 */
973 if (!io->base_io)
974 new_io->base_io = io;
975 else {
976 new_io->base_io = io->base_io;
977 crypt_inc_pending(io->base_io);
978 crypt_dec_pending(io);
979 }
980
981 io = new_io;
982 }
Milan Broz8b004452006-10-03 01:15:37 -0700983 }
Milan Broz899c95d2008-02-08 02:11:02 +0000984
985 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +0000986}
987
Milan Broz4e4eef62008-02-08 02:10:49 +0000988static void kcryptd_crypt_read_done(struct dm_crypt_io *io, int error)
Milan Broz5742fd72008-02-08 02:10:43 +0000989{
990 if (unlikely(error < 0))
991 io->error = -EIO;
992
993 crypt_dec_pending(io);
994}
995
Milan Broz4e4eef62008-02-08 02:10:49 +0000996static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -0700997{
998 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +0000999 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001000
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001001 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001002
Milan Broz53017032008-02-08 02:10:38 +00001003 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001004 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001005
Milan Broz5742fd72008-02-08 02:10:43 +00001006 r = crypt_convert(cc, &io->ctx);
1007
Milan Broz3f1e9072008-03-28 14:16:07 -07001008 if (atomic_dec_and_test(&io->ctx.pending))
Milan Broz3a7f6c92008-02-08 02:11:14 +00001009 kcryptd_crypt_read_done(io, r);
1010
1011 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001012}
1013
Milan Broz95497a92008-02-08 02:11:12 +00001014static void kcryptd_async_done(struct crypto_async_request *async_req,
1015 int error)
1016{
Huang Yingb2174ee2009-03-16 17:44:33 +00001017 struct dm_crypt_request *dmreq = async_req->data;
1018 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001019 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1020 struct crypt_config *cc = io->target->private;
1021
1022 if (error == -EINPROGRESS) {
1023 complete(&ctx->restart);
1024 return;
1025 }
1026
Milan Broz2dc53272011-01-13 19:59:54 +00001027 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1028 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1029
Huang Yingb2174ee2009-03-16 17:44:33 +00001030 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001031
1032 if (!atomic_dec_and_test(&ctx->pending))
1033 return;
1034
1035 if (bio_data_dir(io->base_bio) == READ)
1036 kcryptd_crypt_read_done(io, error);
1037 else
1038 kcryptd_crypt_write_io_submit(io, error, 1);
1039}
1040
Milan Broz4e4eef62008-02-08 02:10:49 +00001041static void kcryptd_crypt(struct work_struct *work)
1042{
1043 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1044
1045 if (bio_data_dir(io->base_bio) == READ)
1046 kcryptd_crypt_read_convert(io);
1047 else
1048 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001049}
1050
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001051static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1052{
1053 struct crypt_config *cc = io->target->private;
1054
1055 INIT_WORK(&io->work, kcryptd_crypt);
1056 queue_work(cc->crypt_queue, &io->work);
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059/*
1060 * Decode key from its hex representation
1061 */
1062static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1063{
1064 char buffer[3];
1065 char *endp;
1066 unsigned int i;
1067
1068 buffer[2] = '\0';
1069
Milan Broz8b004452006-10-03 01:15:37 -07001070 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 buffer[0] = *hex++;
1072 buffer[1] = *hex++;
1073
1074 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
1075
1076 if (endp != &buffer[2])
1077 return -EINVAL;
1078 }
1079
1080 if (*hex != '\0')
1081 return -EINVAL;
1082
1083 return 0;
1084}
1085
1086/*
1087 * Encode key into its hex representation
1088 */
1089static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
1090{
1091 unsigned int i;
1092
Milan Broz8b004452006-10-03 01:15:37 -07001093 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sprintf(hex, "%02x", *key);
1095 hex += 2;
1096 key++;
1097 }
1098}
1099
Andi Kleenc0297722011-01-13 19:59:53 +00001100static int crypt_setkey_allcpus(struct crypt_config *cc)
1101{
1102 int cpu, err = 0, r;
1103
1104 for_each_possible_cpu(cpu) {
1105 r = crypto_ablkcipher_setkey(per_cpu_ptr(cc->cpu, cpu)->tfm,
1106 cc->key, cc->key_size);
1107 if (r)
1108 err = r;
1109 }
1110
1111 return err;
1112}
1113
Milan Broze48d4bb2006-10-03 01:15:37 -07001114static int crypt_set_key(struct crypt_config *cc, char *key)
1115{
Milan Broz69a8cfc2011-01-13 19:59:49 +00001116 /* The key size may not be changed. */
1117 if (cc->key_size != (strlen(key) >> 1))
Milan Broze48d4bb2006-10-03 01:15:37 -07001118 return -EINVAL;
1119
Milan Broz69a8cfc2011-01-13 19:59:49 +00001120 /* Hyphen (which gives a key_size of zero) means there is no key. */
1121 if (!cc->key_size && strcmp(key, "-"))
1122 return -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001123
Milan Broz69a8cfc2011-01-13 19:59:49 +00001124 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Broze48d4bb2006-10-03 01:15:37 -07001125 return -EINVAL;
1126
1127 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1128
Andi Kleenc0297722011-01-13 19:59:53 +00001129 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001130}
1131
1132static int crypt_wipe_key(struct crypt_config *cc)
1133{
1134 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1135 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001136
1137 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001138}
1139
Milan Broz28513fc2010-08-12 04:14:06 +01001140static void crypt_dtr(struct dm_target *ti)
1141{
1142 struct crypt_config *cc = ti->private;
Andi Kleenc0297722011-01-13 19:59:53 +00001143 struct crypt_cpu *cpu_cc;
1144 int cpu;
Milan Broz28513fc2010-08-12 04:14:06 +01001145
1146 ti->private = NULL;
1147
1148 if (!cc)
1149 return;
1150
1151 if (cc->io_queue)
1152 destroy_workqueue(cc->io_queue);
1153 if (cc->crypt_queue)
1154 destroy_workqueue(cc->crypt_queue);
1155
Andi Kleenc0297722011-01-13 19:59:53 +00001156 if (cc->cpu)
1157 for_each_possible_cpu(cpu) {
1158 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
1159 if (cpu_cc->req)
1160 mempool_free(cpu_cc->req, cc->req_pool);
1161 if (cpu_cc->tfm)
1162 crypto_free_ablkcipher(cpu_cc->tfm);
1163 }
1164
Milan Broz28513fc2010-08-12 04:14:06 +01001165 if (cc->bs)
1166 bioset_free(cc->bs);
1167
1168 if (cc->page_pool)
1169 mempool_destroy(cc->page_pool);
1170 if (cc->req_pool)
1171 mempool_destroy(cc->req_pool);
1172 if (cc->io_pool)
1173 mempool_destroy(cc->io_pool);
1174
1175 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1176 cc->iv_gen_ops->dtr(cc);
1177
Milan Broz28513fc2010-08-12 04:14:06 +01001178 if (cc->dev)
1179 dm_put_device(ti, cc->dev);
1180
Andi Kleenc0297722011-01-13 19:59:53 +00001181 if (cc->cpu)
1182 free_percpu(cc->cpu);
1183
Milan Broz5ebaee62010-08-12 04:14:07 +01001184 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001185 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001186
1187 /* Must zero key material before freeing */
1188 kzfree(cc);
1189}
1190
Milan Broz5ebaee62010-08-12 04:14:07 +01001191static int crypt_ctr_cipher(struct dm_target *ti,
1192 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Milan Broz5ebaee62010-08-12 04:14:07 +01001194 struct crypt_config *cc = ti->private;
Andi Kleenc0297722011-01-13 19:59:53 +00001195 struct crypto_ablkcipher *tfm;
Milan Broz5ebaee62010-08-12 04:14:07 +01001196 char *tmp, *cipher, *chainmode, *ivmode, *ivopts;
1197 char *cipher_api = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +00001198 int cpu, ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Milan Broz5ebaee62010-08-12 04:14:07 +01001200 /* Convert to crypto api definition? */
1201 if (strchr(cipher_in, '(')) {
1202 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -EINVAL;
1204 }
1205
Milan Broz7dbcd132011-01-13 19:59:52 +00001206 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1207 if (!cc->cipher_string)
1208 goto bad_mem;
1209
Milan Broz5ebaee62010-08-12 04:14:07 +01001210 /*
1211 * Legacy dm-crypt cipher specification
1212 * cipher-mode-iv:ivopts
1213 */
1214 tmp = cipher_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 cipher = strsep(&tmp, "-");
Milan Broz5ebaee62010-08-12 04:14:07 +01001216
1217 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1218 if (!cc->cipher)
1219 goto bad_mem;
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 chainmode = strsep(&tmp, "-");
1222 ivopts = strsep(&tmp, "-");
1223 ivmode = strsep(&ivopts, ":");
1224
1225 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001226 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Andi Kleenc0297722011-01-13 19:59:53 +00001228 cc->cpu = alloc_percpu(struct crypt_cpu);
1229 if (!cc->cpu) {
1230 ti->error = "Cannot allocate per cpu state";
1231 goto bad_mem;
1232 }
1233
Milan Broz7dbcd132011-01-13 19:59:52 +00001234 /*
1235 * For compatibility with the original dm-crypt mapping format, if
1236 * only the cipher name is supplied, use cbc-plain.
1237 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001238 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 chainmode = "cbc";
1240 ivmode = "plain";
1241 }
1242
Herbert Xud1806f62006-08-22 20:29:17 +10001243 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001244 ti->error = "IV mechanism required";
1245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Milan Broz5ebaee62010-08-12 04:14:07 +01001248 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1249 if (!cipher_api)
1250 goto bad_mem;
1251
1252 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1253 "%s(%s)", chainmode, cipher);
1254 if (ret < 0) {
1255 kfree(cipher_api);
1256 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001257 }
1258
Milan Broz5ebaee62010-08-12 04:14:07 +01001259 /* Allocate cipher */
Andi Kleenc0297722011-01-13 19:59:53 +00001260 for_each_possible_cpu(cpu) {
1261 tfm = crypto_alloc_ablkcipher(cipher_api, 0, 0);
1262 if (IS_ERR(tfm)) {
1263 ret = PTR_ERR(tfm);
1264 ti->error = "Error allocating crypto tfm";
1265 goto bad;
1266 }
1267 per_cpu_ptr(cc->cpu, cpu)->tfm = tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Milan Broz5ebaee62010-08-12 04:14:07 +01001270 /* Initialize and set key */
1271 ret = crypt_set_key(cc, key);
Milan Broz28513fc2010-08-12 04:14:06 +01001272 if (ret < 0) {
Milan Broz0b430952009-12-10 23:51:55 +00001273 ti->error = "Error decoding and setting key";
Milan Broz28513fc2010-08-12 04:14:06 +01001274 goto bad;
Milan Broz0b430952009-12-10 23:51:55 +00001275 }
1276
Milan Broz5ebaee62010-08-12 04:14:07 +01001277 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001278 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001279 if (cc->iv_size)
1280 /* at least a 64 bit sector number should fit in our buffer */
1281 cc->iv_size = max(cc->iv_size,
1282 (unsigned int)(sizeof(u64) / sizeof(u8)));
1283 else if (ivmode) {
1284 DMWARN("Selected cipher does not support IVs");
1285 ivmode = NULL;
1286 }
1287
1288 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ivmode == NULL)
1290 cc->iv_gen_ops = NULL;
1291 else if (strcmp(ivmode, "plain") == 0)
1292 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001293 else if (strcmp(ivmode, "plain64") == 0)
1294 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 else if (strcmp(ivmode, "essiv") == 0)
1296 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001297 else if (strcmp(ivmode, "benbi") == 0)
1298 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001299 else if (strcmp(ivmode, "null") == 0)
1300 cc->iv_gen_ops = &crypt_iv_null_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001302 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001303 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001304 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Milan Broz28513fc2010-08-12 04:14:06 +01001307 /* Allocate IV */
1308 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1309 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1310 if (ret < 0) {
1311 ti->error = "Error creating IV";
1312 goto bad;
1313 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001314 }
1315
Milan Broz28513fc2010-08-12 04:14:06 +01001316 /* Initialize IV (set keys for ESSIV etc) */
1317 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1318 ret = cc->iv_gen_ops->init(cc);
1319 if (ret < 0) {
1320 ti->error = "Error initialising IV";
1321 goto bad;
1322 }
1323 }
1324
Milan Broz5ebaee62010-08-12 04:14:07 +01001325 ret = 0;
1326bad:
1327 kfree(cipher_api);
1328 return ret;
1329
1330bad_mem:
1331 ti->error = "Cannot allocate cipher strings";
1332 return -ENOMEM;
1333}
1334
1335/*
1336 * Construct an encryption mapping:
1337 * <cipher> <key> <iv_offset> <dev_path> <start>
1338 */
1339static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1340{
1341 struct crypt_config *cc;
1342 unsigned int key_size;
1343 unsigned long long tmpll;
1344 int ret;
1345
1346 if (argc != 5) {
1347 ti->error = "Not enough arguments";
1348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Milan Broz5ebaee62010-08-12 04:14:07 +01001351 key_size = strlen(argv[1]) >> 1;
1352
1353 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1354 if (!cc) {
1355 ti->error = "Cannot allocate encryption context";
1356 return -ENOMEM;
1357 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001358 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001359
1360 ti->private = cc;
1361 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1362 if (ret < 0)
1363 goto bad;
1364
Milan Broz28513fc2010-08-12 04:14:06 +01001365 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001366 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001368 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001369 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371
Milan Brozddd42ed2008-02-08 02:11:07 +00001372 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001373 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001374 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001375 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001376 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001377
1378 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1379 sizeof(struct dm_crypt_request) + cc->iv_size);
1380 if (!cc->req_pool) {
1381 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001382 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001383 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001384
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001385 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001387 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001388 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Jens Axboebb799ca2008-12-10 15:35:05 +01001391 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001392 if (!cc->bs) {
1393 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001394 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001395 }
1396
Milan Broz28513fc2010-08-12 04:14:06 +01001397 ret = -EINVAL;
Andrew Morton4ee218c2006-03-27 01:17:48 -08001398 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001399 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001400 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001402 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Milan Broz28513fc2010-08-12 04:14:06 +01001404 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1405 ti->error = "Device lookup failed";
1406 goto bad;
1407 }
1408
Andrew Morton4ee218c2006-03-27 01:17:48 -08001409 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001410 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001411 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001413 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Milan Broz28513fc2010-08-12 04:14:06 +01001415 ret = -ENOMEM;
Andi Kleenc0297722011-01-13 19:59:53 +00001416 cc->io_queue = alloc_workqueue("kcryptd_io",
1417 WQ_NON_REENTRANT|
1418 WQ_MEM_RECLAIM,
1419 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001420 if (!cc->io_queue) {
1421 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001422 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001423 }
1424
Andi Kleenc0297722011-01-13 19:59:53 +00001425 cc->crypt_queue = alloc_workqueue("kcryptd",
1426 WQ_NON_REENTRANT|
1427 WQ_CPU_INTENSIVE|
1428 WQ_MEM_RECLAIM,
1429 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001430 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001431 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001432 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001433 }
1434
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001435 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return 0;
1437
Milan Broz28513fc2010-08-12 04:14:06 +01001438bad:
1439 crypt_dtr(ti);
1440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443static int crypt_map(struct dm_target *ti, struct bio *bio,
1444 union map_info *map_context)
1445{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001446 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001447 struct crypt_config *cc;
1448
Tejun Heod87f4c12010-09-03 11:56:19 +02001449 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001450 cc = ti->private;
1451 bio->bi_bdev = cc->dev->bdev;
1452 return DM_MAPIO_REMAPPED;
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001455 io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001456
Milan Broz20c82532011-01-13 19:59:53 +00001457 if (bio_data_dir(io->base_bio) == READ) {
1458 if (kcryptd_io_read(io, GFP_NOWAIT))
1459 kcryptd_queue_io(io);
1460 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001461 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001463 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static int crypt_status(struct dm_target *ti, status_type_t type,
1467 char *result, unsigned int maxlen)
1468{
Milan Broz5ebaee62010-08-12 04:14:07 +01001469 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned int sz = 0;
1471
1472 switch (type) {
1473 case STATUSTYPE_INFO:
1474 result[0] = '\0';
1475 break;
1476
1477 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001478 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (cc->key_size > 0) {
1481 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1482 return -ENOMEM;
1483
1484 crypt_encode_key(result + sz, cc->key, cc->key_size);
1485 sz += cc->key_size << 1;
1486 } else {
1487 if (sz >= maxlen)
1488 return -ENOMEM;
1489 result[sz++] = '-';
1490 }
1491
Andrew Morton4ee218c2006-03-27 01:17:48 -08001492 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1493 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 }
1496 return 0;
1497}
1498
Milan Broze48d4bb2006-10-03 01:15:37 -07001499static void crypt_postsuspend(struct dm_target *ti)
1500{
1501 struct crypt_config *cc = ti->private;
1502
1503 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1504}
1505
1506static int crypt_preresume(struct dm_target *ti)
1507{
1508 struct crypt_config *cc = ti->private;
1509
1510 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1511 DMERR("aborting resume - crypt key is not set.");
1512 return -EAGAIN;
1513 }
1514
1515 return 0;
1516}
1517
1518static void crypt_resume(struct dm_target *ti)
1519{
1520 struct crypt_config *cc = ti->private;
1521
1522 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1523}
1524
1525/* Message interface
1526 * key set <key>
1527 * key wipe
1528 */
1529static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1530{
1531 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001532 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001533
1534 if (argc < 2)
1535 goto error;
1536
1537 if (!strnicmp(argv[0], MESG_STR("key"))) {
1538 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1539 DMWARN("not suspended during key manipulation.");
1540 return -EINVAL;
1541 }
Milan Broz542da312009-12-10 23:51:57 +00001542 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) {
1543 ret = crypt_set_key(cc, argv[2]);
1544 if (ret)
1545 return ret;
1546 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1547 ret = cc->iv_gen_ops->init(cc);
1548 return ret;
1549 }
1550 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) {
1551 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1552 ret = cc->iv_gen_ops->wipe(cc);
1553 if (ret)
1554 return ret;
1555 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001556 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001557 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001558 }
1559
1560error:
1561 DMWARN("unrecognised message received.");
1562 return -EINVAL;
1563}
1564
Milan Brozd41e26b2008-07-21 12:00:40 +01001565static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1566 struct bio_vec *biovec, int max_size)
1567{
1568 struct crypt_config *cc = ti->private;
1569 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1570
1571 if (!q->merge_bvec_fn)
1572 return max_size;
1573
1574 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001575 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001576
1577 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1578}
1579
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001580static int crypt_iterate_devices(struct dm_target *ti,
1581 iterate_devices_callout_fn fn, void *data)
1582{
1583 struct crypt_config *cc = ti->private;
1584
Mike Snitzer5dea2712009-07-23 20:30:42 +01001585 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001586}
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588static struct target_type crypt_target = {
1589 .name = "crypt",
Andi Kleenc0297722011-01-13 19:59:53 +00001590 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 .module = THIS_MODULE,
1592 .ctr = crypt_ctr,
1593 .dtr = crypt_dtr,
1594 .map = crypt_map,
1595 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001596 .postsuspend = crypt_postsuspend,
1597 .preresume = crypt_preresume,
1598 .resume = crypt_resume,
1599 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001600 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001601 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602};
1603
1604static int __init dm_crypt_init(void)
1605{
1606 int r;
1607
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001608 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (!_crypt_io_pool)
1610 return -ENOMEM;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 r = dm_register_target(&crypt_target);
1613 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001614 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001615 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return r;
1619}
1620
1621static void __exit dm_crypt_exit(void)
1622{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001623 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 kmem_cache_destroy(_crypt_io_pool);
1625}
1626
1627module_init(dm_crypt_init);
1628module_exit(dm_crypt_exit);
1629
1630MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1631MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1632MODULE_LICENSE("GPL");