blob: 4519a7c0098cf6c2cffc4a6a6d36f4e353628c7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf14299f2014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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.
Milan Brozed04d982013-10-28 23:21:04 +01005 * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/bio.h>
16#include <linux/blkdev.h>
17#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/crypto.h>
20#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050021#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100024#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100026#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000027#include <crypto/hash.h>
28#include <crypto/md5.h>
29#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mikulas Patocka586e80e2008-10-21 17:44:59 +010031#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Alasdair G Kergon72d94862006-06-26 00:27:35 -070033#define DM_MSG_PREFIX "crypt"
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;
Kent Overstreet003b5c52013-10-11 15:45:43 -070042 struct bvec_iter iter_in;
43 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010044 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010045 atomic_t cc_pending;
Mikulas Patocka610f2de2014-02-20 18:01:01 -050046 struct ablkcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Milan Broz53017032008-02-08 02:10:38 +000049/*
50 * per bio private data
51 */
52struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010053 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000054 struct bio *base_bio;
55 struct work_struct work;
56
57 struct convert_context ctx;
58
Mikulas Patocka40b62292012-07-27 15:08:04 +010059 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000060 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000061 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050062
63 struct list_head list;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040064} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000065
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
Milan Brozed04d982013-10-28 23:21:04 +0100102#define TCW_WHITENING_SIZE 16
103struct iv_tcw_private {
104 struct crypto_shash *crc32_tfm;
105 u8 *iv_seed;
106 u8 *whitening;
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Crypt: maps a linear range of a block device
111 * and encrypts / decrypts at the same time.
112 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500113enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
114 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000115
116/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500117 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000118 */
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 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000127 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700129 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500130 struct mutex bio_alloc_lock;
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
Mikulas Patockadc267622015-02-13 08:25:59 -0500135 struct task_struct *write_thread;
136 wait_queue_head_t write_thread_wait;
137 struct list_head write_thread_list;
138
Milan Broz5ebaee62010-08-12 04:14:07 +0100139 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000140 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800143 union {
Milan Broz60473592009-12-10 23:51:55 +0000144 struct iv_essiv_private essiv;
145 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000146 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100147 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800148 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sector_t iv_offset;
150 unsigned int iv_size;
151
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100152 /* ESSIV: struct crypto_cipher *essiv_tfm */
153 void *iv_private;
154 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000155 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000156
157 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000158 * Layout of each crypto request:
159 *
160 * struct ablkcipher_request
161 * context
162 * padding
163 * struct dm_crypt_request
164 * padding
165 * IV
166 *
167 * The padding is added so that dm_crypt_request and the IV are
168 * correctly aligned.
169 */
170 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000171
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400172 unsigned int per_bio_data_size;
173
Milan Broze48d4bb2006-10-03 01:15:37 -0700174 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100176 unsigned int key_parts; /* independent parts in key buffer */
177 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 u8 key[0];
179};
180
Milan Broz6a24c712006-10-03 01:15:40 -0700181#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100183static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000184static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000185static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700186
Andi Kleenc0297722011-01-13 19:59:53 +0000187/*
188 * Use this to access cipher attributes that are the same for each CPU.
189 */
190static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
191{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100192 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Different IV generation algorithms:
197 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000198 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200199 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Milan Broz61afef62009-12-10 23:52:25 +0000201 * plain64: the initial vector is the 64-bit little-endian version of the sector
202 * number, padded with zeros if necessary.
203 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000204 * essiv: "encrypted sector|salt initial vector", the sector number is
205 * encrypted with the bulk cipher using a salt as key. The salt
206 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Rik Snel48527fa2006-09-03 08:56:39 +1000208 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
209 * (needed for LRW-32-AES and possible other narrow block modes)
210 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700211 * null: the initial vector is always zero. Provides compatibility with
212 * obsolete loop_fish2 devices. Do not use for new devices.
213 *
Milan Broz34745782011-01-13 19:59:55 +0000214 * lmk: Compatible implementation of the block chaining mode used
215 * by the Loop-AES block device encryption system
216 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
217 * It operates on full 512 byte sectors and uses CBC
218 * with an IV derived from the sector number, the data and
219 * optionally extra IV seed.
220 * This means that after decryption the first block
221 * of sector must be tweaked according to decrypted data.
222 * Loop-AES can use three encryption schemes:
223 * version 1: is plain aes-cbc mode
224 * version 2: uses 64 multikey scheme with lmk IV generator
225 * version 3: the same as version 2 with additional IV seed
226 * (it uses 65 keys, last key is used as IV seed)
227 *
Milan Brozed04d982013-10-28 23:21:04 +0100228 * tcw: Compatible implementation of the block chaining mode used
229 * by the TrueCrypt device encryption system (prior to version 4.1).
230 * For more info see: http://www.truecrypt.org
231 * It operates on full 512 byte sectors and uses CBC
232 * with an IV derived from initial key and the sector number.
233 * In addition, whitening value is applied on every sector, whitening
234 * is calculated from initial key, sector number and mixed using CRC32.
235 * Note that this encryption scheme is vulnerable to watermarking attacks
236 * and should be used for old compatible containers access only.
237 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * plumb: unimplemented, see:
239 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
240 */
241
Milan Broz2dc53272011-01-13 19:59:54 +0000242static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
243 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100246 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return 0;
249}
250
Milan Broz61afef62009-12-10 23:52:25 +0000251static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000252 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000253{
254 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100255 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000256
257 return 0;
258}
259
Milan Brozb95bf2d2009-12-10 23:51:56 +0000260/* Initialise ESSIV - compute salt but no local memory allocations */
261static int crypt_iv_essiv_init(struct crypt_config *cc)
262{
263 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
264 struct hash_desc desc;
265 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000266 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100267 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000268
269 sg_init_one(&sg, cc->key, cc->key_size);
270 desc.tfm = essiv->hash_tfm;
271 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
272
273 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
274 if (err)
275 return err;
276
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100277 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000278
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100279 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
280 crypto_hash_digestsize(essiv->hash_tfm));
281 if (err)
282 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000283
284 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000285}
286
Milan Broz542da312009-12-10 23:51:57 +0000287/* Wipe salt and reset key derived from volume key */
288static int crypt_iv_essiv_wipe(struct crypt_config *cc)
289{
290 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
291 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000292 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100293 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000294
295 memset(essiv->salt, 0, salt_size);
296
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100297 essiv_tfm = cc->iv_private;
298 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
299 if (r)
300 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000301
302 return err;
303}
304
305/* Set up per cpu cipher state */
306static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
307 struct dm_target *ti,
308 u8 *salt, unsigned saltsize)
309{
310 struct crypto_cipher *essiv_tfm;
311 int err;
312
313 /* Setup the essiv_tfm with the given salt */
314 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
315 if (IS_ERR(essiv_tfm)) {
316 ti->error = "Error allocating crypto tfm for ESSIV";
317 return essiv_tfm;
318 }
319
320 if (crypto_cipher_blocksize(essiv_tfm) !=
321 crypto_ablkcipher_ivsize(any_tfm(cc))) {
322 ti->error = "Block size of ESSIV cipher does "
323 "not match IV size of block cipher";
324 crypto_free_cipher(essiv_tfm);
325 return ERR_PTR(-EINVAL);
326 }
327
328 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
329 if (err) {
330 ti->error = "Failed to set key for ESSIV cipher";
331 crypto_free_cipher(essiv_tfm);
332 return ERR_PTR(err);
333 }
334
335 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000336}
337
Milan Broz60473592009-12-10 23:51:55 +0000338static void crypt_iv_essiv_dtr(struct crypt_config *cc)
339{
Andi Kleenc0297722011-01-13 19:59:53 +0000340 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000341 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
342
Milan Brozb95bf2d2009-12-10 23:51:56 +0000343 crypto_free_hash(essiv->hash_tfm);
344 essiv->hash_tfm = NULL;
345
346 kzfree(essiv->salt);
347 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000348
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100349 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000350
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100351 if (essiv_tfm)
352 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000353
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100354 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100358 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Milan Broz5861f1b2009-12-10 23:51:56 +0000360 struct crypto_cipher *essiv_tfm = NULL;
361 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000362 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100363 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Milan Broz5861f1b2009-12-10 23:51:56 +0000365 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700366 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -EINVAL;
368 }
369
Milan Brozb95bf2d2009-12-10 23:51:56 +0000370 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000371 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
372 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700373 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000374 err = PTR_ERR(hash_tfm);
375 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Milan Brozb95bf2d2009-12-10 23:51:56 +0000378 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000379 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700380 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000381 err = -ENOMEM;
382 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Milan Brozb95bf2d2009-12-10 23:51:56 +0000385 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000386 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
387
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100388 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
389 crypto_hash_digestsize(hash_tfm));
390 if (IS_ERR(essiv_tfm)) {
391 crypt_iv_essiv_dtr(cc);
392 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000393 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100394 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000397
398bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000399 if (hash_tfm && !IS_ERR(hash_tfm))
400 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000401 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Milan Broz2dc53272011-01-13 19:59:54 +0000405static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
406 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100408 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100411 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000412 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415}
416
Rik Snel48527fa2006-09-03 08:56:39 +1000417static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
418 const char *opts)
419{
Andi Kleenc0297722011-01-13 19:59:53 +0000420 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800421 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000422
423 /* we need to calculate how far we must shift the sector count
424 * to get the cipher block count, we use this shift in _gen */
425
426 if (1 << log != bs) {
427 ti->error = "cypher blocksize is not a power of 2";
428 return -EINVAL;
429 }
430
431 if (log > 9) {
432 ti->error = "cypher blocksize is > 512";
433 return -EINVAL;
434 }
435
Milan Broz60473592009-12-10 23:51:55 +0000436 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000437
438 return 0;
439}
440
441static void crypt_iv_benbi_dtr(struct crypt_config *cc)
442{
Rik Snel48527fa2006-09-03 08:56:39 +1000443}
444
Milan Broz2dc53272011-01-13 19:59:54 +0000445static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
446 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000447{
Herbert Xu79066ad2006-12-05 13:41:52 -0800448 __be64 val;
449
Rik Snel48527fa2006-09-03 08:56:39 +1000450 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800451
Milan Broz2dc53272011-01-13 19:59:54 +0000452 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800453 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
Milan Broz2dc53272011-01-13 19:59:54 +0000458static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
459 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700460{
461 memset(iv, 0, cc->iv_size);
462
463 return 0;
464}
465
Milan Broz34745782011-01-13 19:59:55 +0000466static void crypt_iv_lmk_dtr(struct crypt_config *cc)
467{
468 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
469
470 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
471 crypto_free_shash(lmk->hash_tfm);
472 lmk->hash_tfm = NULL;
473
474 kzfree(lmk->seed);
475 lmk->seed = NULL;
476}
477
478static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
479 const char *opts)
480{
481 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
482
483 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
484 if (IS_ERR(lmk->hash_tfm)) {
485 ti->error = "Error initializing LMK hash";
486 return PTR_ERR(lmk->hash_tfm);
487 }
488
489 /* No seed in LMK version 2 */
490 if (cc->key_parts == cc->tfms_count) {
491 lmk->seed = NULL;
492 return 0;
493 }
494
495 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
496 if (!lmk->seed) {
497 crypt_iv_lmk_dtr(cc);
498 ti->error = "Error kmallocing seed storage in LMK";
499 return -ENOMEM;
500 }
501
502 return 0;
503}
504
505static int crypt_iv_lmk_init(struct crypt_config *cc)
506{
507 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
508 int subkey_size = cc->key_size / cc->key_parts;
509
510 /* LMK seed is on the position of LMK_KEYS + 1 key */
511 if (lmk->seed)
512 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
513 crypto_shash_digestsize(lmk->hash_tfm));
514
515 return 0;
516}
517
518static int crypt_iv_lmk_wipe(struct crypt_config *cc)
519{
520 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
521
522 if (lmk->seed)
523 memset(lmk->seed, 0, LMK_SEED_SIZE);
524
525 return 0;
526}
527
528static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
529 struct dm_crypt_request *dmreq,
530 u8 *data)
531{
532 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200533 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000534 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100535 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000536 int i, r;
537
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200538 desc->tfm = lmk->hash_tfm;
539 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000540
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200541 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000542 if (r)
543 return r;
544
545 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200546 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000547 if (r)
548 return r;
549 }
550
551 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200552 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000553 if (r)
554 return r;
555
556 /* Sector is cropped to 56 bits here */
557 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
558 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
559 buf[2] = cpu_to_le32(4024);
560 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200561 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000562 if (r)
563 return r;
564
565 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200566 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000567 if (r)
568 return r;
569
570 for (i = 0; i < MD5_HASH_WORDS; i++)
571 __cpu_to_le32s(&md5state.hash[i]);
572 memcpy(iv, &md5state.hash, cc->iv_size);
573
574 return 0;
575}
576
577static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
578 struct dm_crypt_request *dmreq)
579{
580 u8 *src;
581 int r = 0;
582
583 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800584 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000585 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800586 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000587 } else
588 memset(iv, 0, cc->iv_size);
589
590 return r;
591}
592
593static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
594 struct dm_crypt_request *dmreq)
595{
596 u8 *dst;
597 int r;
598
599 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
600 return 0;
601
Cong Wangc2e022c2011-11-28 13:26:02 +0800602 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000603 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
604
605 /* Tweak the first block of plaintext sector */
606 if (!r)
607 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
608
Cong Wangc2e022c2011-11-28 13:26:02 +0800609 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000610 return r;
611}
612
Milan Brozed04d982013-10-28 23:21:04 +0100613static void crypt_iv_tcw_dtr(struct crypt_config *cc)
614{
615 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
616
617 kzfree(tcw->iv_seed);
618 tcw->iv_seed = NULL;
619 kzfree(tcw->whitening);
620 tcw->whitening = NULL;
621
622 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
623 crypto_free_shash(tcw->crc32_tfm);
624 tcw->crc32_tfm = NULL;
625}
626
627static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
628 const char *opts)
629{
630 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
631
632 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
633 ti->error = "Wrong key size for TCW";
634 return -EINVAL;
635 }
636
637 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
638 if (IS_ERR(tcw->crc32_tfm)) {
639 ti->error = "Error initializing CRC32 in TCW";
640 return PTR_ERR(tcw->crc32_tfm);
641 }
642
643 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
644 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
645 if (!tcw->iv_seed || !tcw->whitening) {
646 crypt_iv_tcw_dtr(cc);
647 ti->error = "Error allocating seed storage in TCW";
648 return -ENOMEM;
649 }
650
651 return 0;
652}
653
654static int crypt_iv_tcw_init(struct crypt_config *cc)
655{
656 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
657 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
658
659 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
660 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
661 TCW_WHITENING_SIZE);
662
663 return 0;
664}
665
666static int crypt_iv_tcw_wipe(struct crypt_config *cc)
667{
668 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
669
670 memset(tcw->iv_seed, 0, cc->iv_size);
671 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
672
673 return 0;
674}
675
676static int crypt_iv_tcw_whitening(struct crypt_config *cc,
677 struct dm_crypt_request *dmreq,
678 u8 *data)
679{
680 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
681 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
682 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200683 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100684 int i, r;
685
686 /* xor whitening with sector number */
687 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
688 crypto_xor(buf, (u8 *)&sector, 8);
689 crypto_xor(&buf[8], (u8 *)&sector, 8);
690
691 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200692 desc->tfm = tcw->crc32_tfm;
693 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100694 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200695 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100696 if (r)
697 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200698 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100699 if (r)
700 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200701 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100702 if (r)
703 goto out;
704 }
705 crypto_xor(&buf[0], &buf[12], 4);
706 crypto_xor(&buf[4], &buf[8], 4);
707
708 /* apply whitening (8 bytes) to whole sector */
709 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
710 crypto_xor(data + i * 8, buf, 8);
711out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100712 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100713 return r;
714}
715
716static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
717 struct dm_crypt_request *dmreq)
718{
719 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
720 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
721 u8 *src;
722 int r = 0;
723
724 /* Remove whitening from ciphertext */
725 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
726 src = kmap_atomic(sg_page(&dmreq->sg_in));
727 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
728 kunmap_atomic(src);
729 }
730
731 /* Calculate IV */
732 memcpy(iv, tcw->iv_seed, cc->iv_size);
733 crypto_xor(iv, (u8 *)&sector, 8);
734 if (cc->iv_size > 8)
735 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
736
737 return r;
738}
739
740static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
741 struct dm_crypt_request *dmreq)
742{
743 u8 *dst;
744 int r;
745
746 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
747 return 0;
748
749 /* Apply whitening on ciphertext */
750 dst = kmap_atomic(sg_page(&dmreq->sg_out));
751 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
752 kunmap_atomic(dst);
753
754 return r;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static struct crypt_iv_operations crypt_iv_plain_ops = {
758 .generator = crypt_iv_plain_gen
759};
760
Milan Broz61afef62009-12-10 23:52:25 +0000761static struct crypt_iv_operations crypt_iv_plain64_ops = {
762 .generator = crypt_iv_plain64_gen
763};
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static struct crypt_iv_operations crypt_iv_essiv_ops = {
766 .ctr = crypt_iv_essiv_ctr,
767 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000768 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000769 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .generator = crypt_iv_essiv_gen
771};
772
Rik Snel48527fa2006-09-03 08:56:39 +1000773static struct crypt_iv_operations crypt_iv_benbi_ops = {
774 .ctr = crypt_iv_benbi_ctr,
775 .dtr = crypt_iv_benbi_dtr,
776 .generator = crypt_iv_benbi_gen
777};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Ludwig Nussel46b47732007-05-09 02:32:55 -0700779static struct crypt_iv_operations crypt_iv_null_ops = {
780 .generator = crypt_iv_null_gen
781};
782
Milan Broz34745782011-01-13 19:59:55 +0000783static struct crypt_iv_operations crypt_iv_lmk_ops = {
784 .ctr = crypt_iv_lmk_ctr,
785 .dtr = crypt_iv_lmk_dtr,
786 .init = crypt_iv_lmk_init,
787 .wipe = crypt_iv_lmk_wipe,
788 .generator = crypt_iv_lmk_gen,
789 .post = crypt_iv_lmk_post
790};
791
Milan Brozed04d982013-10-28 23:21:04 +0100792static struct crypt_iv_operations crypt_iv_tcw_ops = {
793 .ctr = crypt_iv_tcw_ctr,
794 .dtr = crypt_iv_tcw_dtr,
795 .init = crypt_iv_tcw_init,
796 .wipe = crypt_iv_tcw_wipe,
797 .generator = crypt_iv_tcw_gen,
798 .post = crypt_iv_tcw_post
799};
800
Milan Brozd469f842007-10-19 22:42:37 +0100801static void crypt_convert_init(struct crypt_config *cc,
802 struct convert_context *ctx,
803 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000804 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 ctx->bio_in = bio_in;
807 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700808 if (bio_in)
809 ctx->iter_in = bio_in->bi_iter;
810 if (bio_out)
811 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100812 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000813 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Huang Yingb2174ee2009-03-16 17:44:33 +0000816static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
817 struct ablkcipher_request *req)
818{
819 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
820}
821
822static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
823 struct dm_crypt_request *dmreq)
824{
825 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
826}
827
Milan Broz2dc53272011-01-13 19:59:54 +0000828static u8 *iv_of_dmreq(struct crypt_config *cc,
829 struct dm_crypt_request *dmreq)
830{
831 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
832 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
833}
834
Milan Broz01482b72008-02-08 02:11:04 +0000835static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000836 struct convert_context *ctx,
837 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000838{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700839 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
840 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000841 struct dm_crypt_request *dmreq;
842 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100843 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000844
Huang Yingb2174ee2009-03-16 17:44:33 +0000845 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000846 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000847
Mikulas Patockac66029f2012-07-27 15:08:05 +0100848 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000849 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000850 sg_init_table(&dmreq->sg_in, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700851 sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
852 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000853
Milan Broz3a7f6c92008-02-08 02:11:14 +0000854 sg_init_table(&dmreq->sg_out, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700855 sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
856 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000857
Kent Overstreet003b5c52013-10-11 15:45:43 -0700858 bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
859 bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
Milan Broz01482b72008-02-08 02:11:04 +0000860
Milan Broz3a7f6c92008-02-08 02:11:14 +0000861 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000862 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000863 if (r < 0)
864 return r;
865 }
866
867 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
868 1 << SECTOR_SHIFT, iv);
869
870 if (bio_data_dir(ctx->bio_in) == WRITE)
871 r = crypto_ablkcipher_encrypt(req);
872 else
873 r = crypto_ablkcipher_decrypt(req);
874
Milan Broz2dc53272011-01-13 19:59:54 +0000875 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
876 r = cc->iv_gen_ops->post(cc, iv, dmreq);
877
Milan Broz3a7f6c92008-02-08 02:11:14 +0000878 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000879}
880
Milan Broz95497a92008-02-08 02:11:12 +0000881static void kcryptd_async_done(struct crypto_async_request *async_req,
882 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000883
Milan Brozddd42ed2008-02-08 02:11:07 +0000884static void crypt_alloc_req(struct crypt_config *cc,
885 struct convert_context *ctx)
886{
Mikulas Patockac66029f2012-07-27 15:08:05 +0100887 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000888
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500889 if (!ctx->req)
890 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000891
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500892 ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
893 ablkcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000894 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500895 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000896}
897
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400898static void crypt_free_req(struct crypt_config *cc,
899 struct ablkcipher_request *req, struct bio *base_bio)
900{
901 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
902
903 if ((struct ablkcipher_request *)(io + 1) != req)
904 mempool_free(req, cc->req_pool);
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/*
908 * Encrypt / decrypt data from one bio to another one (can be the same one)
909 */
910static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100911 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Milan Broz3f1e9072008-03-28 14:16:07 -0700913 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Mikulas Patocka40b62292012-07-27 15:08:04 +0100915 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100916
Kent Overstreet003b5c52013-10-11 15:45:43 -0700917 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Milan Broz3a7f6c92008-02-08 02:11:14 +0000919 crypt_alloc_req(cc, ctx);
920
Mikulas Patocka40b62292012-07-27 15:08:04 +0100921 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700922
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500923 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000924
925 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700926 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000927 case -EBUSY:
928 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800929 reinit_completion(&ctx->restart);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000930 /* fall through*/
931 case -EINPROGRESS:
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500932 ctx->req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100933 ctx->cc_sector++;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000934 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000935
Milan Broz3f1e9072008-03-28 14:16:07 -0700936 /* sync */
937 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100938 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100939 ctx->cc_sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100940 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700941 continue;
942
943 /* error */
944 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100945 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700946 return r;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Milan Broz3f1e9072008-03-28 14:16:07 -0700950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500953static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/*
956 * Generate a new unfragmented bio with the given size
957 * This should never violate the device limitations
Mikulas Patocka7145c242015-02-13 08:24:41 -0500958 *
959 * This function may be called concurrently. If we allocate from the mempool
960 * concurrently, there is a possibility of deadlock. For example, if we have
961 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
962 * the mempool concurrently, it may deadlock in a situation where both processes
963 * have allocated 128 pages and the mempool is exhausted.
964 *
965 * In order to avoid this scenario we allocate the pages under a mutex.
966 *
967 * In order to not degrade performance with excessive locking, we try
968 * non-blocking allocations without a mutex first but on failure we fallback
969 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500971static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +0100973 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -0700974 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500976 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
977 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +0000978 struct page *page;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500979 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Mikulas Patocka7145c242015-02-13 08:24:41 -0500981retry:
982 if (unlikely(gfp_mask & __GFP_WAIT))
983 mutex_lock(&cc->bio_alloc_lock);
984
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700985 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700986 if (!clone)
Mikulas Patocka7145c242015-02-13 08:24:41 -0500987 goto return_clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Olaf Kirch027581f2007-05-09 02:32:52 -0700989 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -0700990
Mikulas Patocka7145c242015-02-13 08:24:41 -0500991 remaining_size = size;
992
Olaf Kirchf97380b2007-05-09 02:32:54 -0700993 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000994 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -0500995 if (!page) {
996 crypt_free_buffer_pages(cc, clone);
997 bio_put(clone);
998 gfp_mask |= __GFP_WAIT;
999 goto retry;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mikulas Patocka7145c242015-02-13 08:24:41 -05001002 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001004 bvec = &clone->bi_io_vec[clone->bi_vcnt++];
1005 bvec->bv_page = page;
1006 bvec->bv_len = len;
1007 bvec->bv_offset = 0;
1008
1009 clone->bi_iter.bi_size += len;
Milan Broz91e10622007-12-13 14:16:10 +00001010
Mikulas Patocka7145c242015-02-13 08:24:41 -05001011 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013
Mikulas Patocka7145c242015-02-13 08:24:41 -05001014return_clone:
1015 if (unlikely(gfp_mask & __GFP_WAIT))
1016 mutex_unlock(&cc->bio_alloc_lock);
1017
Milan Broz8b004452006-10-03 01:15:37 -07001018 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Neil Brown644bd2f2007-10-16 13:48:46 +02001021static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Neil Brown644bd2f2007-10-16 13:48:46 +02001023 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct bio_vec *bv;
1025
Kent Overstreetcb34e052012-09-05 15:22:02 -07001026 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 BUG_ON(!bv->bv_page);
1028 mempool_free(bv->bv_page, cc->page_pool);
1029 bv->bv_page = NULL;
1030 }
1031}
1032
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001033static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1034 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001035{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001036 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001037 io->base_bio = bio;
1038 io->sector = sector;
1039 io->error = 0;
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001040 io->ctx.req = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001041 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001042}
1043
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001044static void crypt_inc_pending(struct dm_crypt_io *io)
1045{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001046 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
1050 * One of the bios was finished. Check for completion of
1051 * the whole request and correctly clean up the buffer.
1052 */
Milan Broz5742fd72008-02-08 02:10:43 +00001053static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001055 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001056 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001057 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Mikulas Patocka40b62292012-07-27 15:08:04 +01001059 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return;
1061
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001062 if (io->ctx.req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001063 crypt_free_req(cc, io->ctx.req, base_bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001064
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001065 bio_endio(base_bio, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001069 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *
1071 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001072 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001073 *
1074 * kcryptd performs the actual encryption or decryption.
1075 *
1076 * kcryptd_io performs the IO submission.
1077 *
1078 * They must be separated as otherwise the final stages could be
1079 * starved by new requests which can block in the first stages due
1080 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001081 *
1082 * The work is done per CPU global for all dm-crypt instances.
1083 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
NeilBrown6712ecf2007-09-27 12:47:43 +02001085static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -07001086{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001087 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001088 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001089 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -07001090
Milan Brozadfe4772007-12-13 14:15:51 +00001091 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1092 error = -EIO;
1093
Milan Broz8b004452006-10-03 01:15:37 -07001094 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001095 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001096 */
Milan Brozee7a4912008-02-08 02:10:46 +00001097 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001098 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001099
1100 bio_put(clone);
1101
1102 if (rw == READ && !error) {
1103 kcryptd_queue_crypt(io);
1104 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001105 }
Milan Broz8b004452006-10-03 01:15:37 -07001106
Milan Brozadfe4772007-12-13 14:15:51 +00001107 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +00001108 io->error = error;
1109
1110 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001111}
1112
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001113static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001114{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001115 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001116
1117 clone->bi_private = io;
1118 clone->bi_end_io = crypt_endio;
1119 clone->bi_bdev = cc->dev->bdev;
1120 clone->bi_rw = io->base_bio->bi_rw;
1121}
1122
Milan Broz20c82532011-01-13 19:59:53 +00001123static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001124{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001125 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001126 struct bio *base_bio = io->base_bio;
1127 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001128
Milan Broz8b004452006-10-03 01:15:37 -07001129 /*
1130 * The block layer might modify the bvec array, so always
1131 * copy the required bvecs because we need the original
1132 * one in order to decrypt the whole bio data *afterwards*.
1133 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -07001134 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001135 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001136 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001137
Milan Broz20c82532011-01-13 19:59:53 +00001138 crypt_inc_pending(io);
1139
Milan Broz8b004452006-10-03 01:15:37 -07001140 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001141 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001142
Milan Broz93e605c2006-10-03 01:15:38 -07001143 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001144 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001145}
1146
Mikulas Patockadc267622015-02-13 08:25:59 -05001147static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001148{
1149 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1150
Mikulas Patockadc267622015-02-13 08:25:59 -05001151 crypt_inc_pending(io);
1152 if (kcryptd_io_read(io, GFP_NOIO))
1153 io->error = -ENOMEM;
1154 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001155}
1156
Mikulas Patockadc267622015-02-13 08:25:59 -05001157static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001158{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001159 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001160
Mikulas Patockadc267622015-02-13 08:25:59 -05001161 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001162 queue_work(cc->io_queue, &io->work);
1163}
1164
Mikulas Patockadc267622015-02-13 08:25:59 -05001165static void kcryptd_io_write(struct dm_crypt_io *io)
1166{
1167 struct bio *clone = io->ctx.bio_out;
1168
1169 generic_make_request(clone);
1170}
1171
1172static int dmcrypt_write(void *data)
1173{
1174 struct crypt_config *cc = data;
1175 while (1) {
1176 struct list_head local_list;
1177 struct blk_plug plug;
1178
1179 DECLARE_WAITQUEUE(wait, current);
1180
1181 spin_lock_irq(&cc->write_thread_wait.lock);
1182continue_locked:
1183
1184 if (!list_empty(&cc->write_thread_list))
1185 goto pop_from_list;
1186
1187 __set_current_state(TASK_INTERRUPTIBLE);
1188 __add_wait_queue(&cc->write_thread_wait, &wait);
1189
1190 spin_unlock_irq(&cc->write_thread_wait.lock);
1191
1192 if (unlikely(kthread_should_stop())) {
1193 set_task_state(current, TASK_RUNNING);
1194 remove_wait_queue(&cc->write_thread_wait, &wait);
1195 break;
1196 }
1197
1198 schedule();
1199
1200 set_task_state(current, TASK_RUNNING);
1201 spin_lock_irq(&cc->write_thread_wait.lock);
1202 __remove_wait_queue(&cc->write_thread_wait, &wait);
1203 goto continue_locked;
1204
1205pop_from_list:
1206 local_list = cc->write_thread_list;
1207 local_list.next->prev = &local_list;
1208 local_list.prev->next = &local_list;
1209 INIT_LIST_HEAD(&cc->write_thread_list);
1210
1211 spin_unlock_irq(&cc->write_thread_wait.lock);
1212
1213 blk_start_plug(&plug);
1214 do {
1215 struct dm_crypt_io *io = container_of(local_list.next,
1216 struct dm_crypt_io, list);
1217 list_del(&io->list);
1218 kcryptd_io_write(io);
1219 } while (!list_empty(&local_list));
1220 blk_finish_plug(&plug);
1221 }
1222 return 0;
1223}
1224
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001225static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001226{
Milan Brozdec1ced2008-02-08 02:10:57 +00001227 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001228 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001229 unsigned long flags;
Milan Brozdec1ced2008-02-08 02:10:57 +00001230
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001231 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001232 crypt_free_buffer_pages(cc, clone);
1233 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001234 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001235 return;
1236 }
1237
1238 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001239 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001240
Kent Overstreet4f024f32013-10-11 15:44:27 -07001241 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001242
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001243 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1244 generic_make_request(clone);
1245 return;
1246 }
1247
Mikulas Patockadc267622015-02-13 08:25:59 -05001248 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
1249 list_add_tail(&io->list, &cc->write_thread_list);
1250 wake_up_locked(&cc->write_thread_wait);
1251 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001252}
1253
Milan Brozfc5a5e92008-10-10 13:37:04 +01001254static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001255{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001256 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001257 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001258 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001259 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001260 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001261
Milan Broz93e605c2006-10-03 01:15:38 -07001262 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001263 * Prevent io from disappearing until this function completes.
1264 */
1265 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001266 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001267
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001268 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1269 if (unlikely(!clone)) {
1270 io->error = -EIO;
1271 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001272 }
Milan Broz899c95d2008-02-08 02:11:02 +00001273
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001274 io->ctx.bio_out = clone;
1275 io->ctx.iter_out = clone->bi_iter;
1276
1277 sector += bio_sectors(clone);
1278
1279 crypt_inc_pending(io);
1280 r = crypt_convert(cc, &io->ctx);
1281 if (r)
1282 io->error = -EIO;
1283 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1284
1285 /* Encryption was already finished, submit io now */
1286 if (crypt_finished) {
1287 kcryptd_crypt_write_io_submit(io, 0);
1288 io->sector = sector;
1289 }
1290
1291dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001292 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001293}
1294
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001295static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001296{
Milan Broz5742fd72008-02-08 02:10:43 +00001297 crypt_dec_pending(io);
1298}
1299
Milan Broz4e4eef62008-02-08 02:10:49 +00001300static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001301{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001302 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001303 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001304
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001305 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001306
Milan Broz53017032008-02-08 02:10:38 +00001307 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001308 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001309
Milan Broz5742fd72008-02-08 02:10:43 +00001310 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001311 if (r < 0)
1312 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001313
Mikulas Patocka40b62292012-07-27 15:08:04 +01001314 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001315 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001316
1317 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001318}
1319
Milan Broz95497a92008-02-08 02:11:12 +00001320static void kcryptd_async_done(struct crypto_async_request *async_req,
1321 int error)
1322{
Huang Yingb2174ee2009-03-16 17:44:33 +00001323 struct dm_crypt_request *dmreq = async_req->data;
1324 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001325 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001326 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001327
1328 if (error == -EINPROGRESS) {
1329 complete(&ctx->restart);
1330 return;
1331 }
1332
Milan Broz2dc53272011-01-13 19:59:54 +00001333 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1334 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1335
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001336 if (error < 0)
1337 io->error = -EIO;
1338
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001339 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001340
Mikulas Patocka40b62292012-07-27 15:08:04 +01001341 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001342 return;
1343
1344 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001345 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001346 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001347 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001348}
1349
Milan Broz4e4eef62008-02-08 02:10:49 +00001350static void kcryptd_crypt(struct work_struct *work)
1351{
1352 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1353
1354 if (bio_data_dir(io->base_bio) == READ)
1355 kcryptd_crypt_read_convert(io);
1356 else
1357 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001358}
1359
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001360static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1361{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001362 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001363
1364 INIT_WORK(&io->work, kcryptd_crypt);
1365 queue_work(cc->crypt_queue, &io->work);
1366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/*
1369 * Decode key from its hex representation
1370 */
1371static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1372{
1373 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned int i;
1375
1376 buffer[2] = '\0';
1377
Milan Broz8b004452006-10-03 01:15:37 -07001378 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 buffer[0] = *hex++;
1380 buffer[1] = *hex++;
1381
majianpeng1a66a082012-07-27 15:07:59 +01001382 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EINVAL;
1384 }
1385
1386 if (*hex != '\0')
1387 return -EINVAL;
1388
1389 return 0;
1390}
1391
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001392static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001393{
Milan Brozd1f96422011-01-13 19:59:54 +00001394 unsigned i;
1395
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001396 if (!cc->tfms)
1397 return;
1398
Milan Brozd1f96422011-01-13 19:59:54 +00001399 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001400 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1401 crypto_free_ablkcipher(cc->tfms[i]);
1402 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001403 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001404
1405 kfree(cc->tfms);
1406 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001407}
1408
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001409static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001410{
Milan Brozd1f96422011-01-13 19:59:54 +00001411 unsigned i;
1412 int err;
1413
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001414 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1415 GFP_KERNEL);
1416 if (!cc->tfms)
1417 return -ENOMEM;
1418
Milan Brozd1f96422011-01-13 19:59:54 +00001419 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001420 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1421 if (IS_ERR(cc->tfms[i])) {
1422 err = PTR_ERR(cc->tfms[i]);
1423 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001424 return err;
1425 }
1426 }
1427
1428 return 0;
1429}
1430
Andi Kleenc0297722011-01-13 19:59:53 +00001431static int crypt_setkey_allcpus(struct crypt_config *cc)
1432{
Milan Brozda31a072013-10-28 23:21:03 +01001433 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001434 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001435
Milan Brozda31a072013-10-28 23:21:03 +01001436 /* Ignore extra keys (which are used for IV etc) */
1437 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1438
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001439 for (i = 0; i < cc->tfms_count; i++) {
1440 r = crypto_ablkcipher_setkey(cc->tfms[i],
1441 cc->key + (i * subkey_size),
1442 subkey_size);
1443 if (r)
1444 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001445 }
1446
1447 return err;
1448}
1449
Milan Broze48d4bb2006-10-03 01:15:37 -07001450static int crypt_set_key(struct crypt_config *cc, char *key)
1451{
Milan Brozde8be5a2011-03-24 13:54:27 +00001452 int r = -EINVAL;
1453 int key_string_len = strlen(key);
1454
Milan Broz69a8cfc2011-01-13 19:59:49 +00001455 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001456 if (cc->key_size != (key_string_len >> 1))
1457 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001458
Milan Broz69a8cfc2011-01-13 19:59:49 +00001459 /* Hyphen (which gives a key_size of zero) means there is no key. */
1460 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001461 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001462
Milan Broz69a8cfc2011-01-13 19:59:49 +00001463 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001464 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001465
1466 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1467
Milan Brozde8be5a2011-03-24 13:54:27 +00001468 r = crypt_setkey_allcpus(cc);
1469
1470out:
1471 /* Hex key string not needed after here, so wipe it. */
1472 memset(key, '0', key_string_len);
1473
1474 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001475}
1476
1477static int crypt_wipe_key(struct crypt_config *cc)
1478{
1479 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1480 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001481
1482 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001483}
1484
Milan Broz28513fc2010-08-12 04:14:06 +01001485static void crypt_dtr(struct dm_target *ti)
1486{
1487 struct crypt_config *cc = ti->private;
1488
1489 ti->private = NULL;
1490
1491 if (!cc)
1492 return;
1493
Mikulas Patockadc267622015-02-13 08:25:59 -05001494 if (cc->write_thread)
1495 kthread_stop(cc->write_thread);
1496
Milan Broz28513fc2010-08-12 04:14:06 +01001497 if (cc->io_queue)
1498 destroy_workqueue(cc->io_queue);
1499 if (cc->crypt_queue)
1500 destroy_workqueue(cc->crypt_queue);
1501
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001502 crypt_free_tfms(cc);
1503
Milan Broz28513fc2010-08-12 04:14:06 +01001504 if (cc->bs)
1505 bioset_free(cc->bs);
1506
1507 if (cc->page_pool)
1508 mempool_destroy(cc->page_pool);
1509 if (cc->req_pool)
1510 mempool_destroy(cc->req_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01001511
1512 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1513 cc->iv_gen_ops->dtr(cc);
1514
Milan Broz28513fc2010-08-12 04:14:06 +01001515 if (cc->dev)
1516 dm_put_device(ti, cc->dev);
1517
Milan Broz5ebaee62010-08-12 04:14:07 +01001518 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001519 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001520
1521 /* Must zero key material before freeing */
1522 kzfree(cc);
1523}
1524
Milan Broz5ebaee62010-08-12 04:14:07 +01001525static int crypt_ctr_cipher(struct dm_target *ti,
1526 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Milan Broz5ebaee62010-08-12 04:14:07 +01001528 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001529 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001530 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001531 int ret = -EINVAL;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001532 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Milan Broz5ebaee62010-08-12 04:14:07 +01001534 /* Convert to crypto api definition? */
1535 if (strchr(cipher_in, '(')) {
1536 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EINVAL;
1538 }
1539
Milan Broz7dbcd132011-01-13 19:59:52 +00001540 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1541 if (!cc->cipher_string)
1542 goto bad_mem;
1543
Milan Broz5ebaee62010-08-12 04:14:07 +01001544 /*
1545 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001546 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001547 */
1548 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001549 keycount = strsep(&tmp, "-");
1550 cipher = strsep(&keycount, ":");
1551
1552 if (!keycount)
1553 cc->tfms_count = 1;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001554 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001555 !is_power_of_2(cc->tfms_count)) {
1556 ti->error = "Bad cipher key count specification";
1557 return -EINVAL;
1558 }
1559 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001560 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001561
1562 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1563 if (!cc->cipher)
1564 goto bad_mem;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 chainmode = strsep(&tmp, "-");
1567 ivopts = strsep(&tmp, "-");
1568 ivmode = strsep(&ivopts, ":");
1569
1570 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001571 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Milan Broz7dbcd132011-01-13 19:59:52 +00001573 /*
1574 * For compatibility with the original dm-crypt mapping format, if
1575 * only the cipher name is supplied, use cbc-plain.
1576 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001577 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 chainmode = "cbc";
1579 ivmode = "plain";
1580 }
1581
Herbert Xud1806f62006-08-22 20:29:17 +10001582 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001583 ti->error = "IV mechanism required";
1584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Milan Broz5ebaee62010-08-12 04:14:07 +01001587 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1588 if (!cipher_api)
1589 goto bad_mem;
1590
1591 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1592 "%s(%s)", chainmode, cipher);
1593 if (ret < 0) {
1594 kfree(cipher_api);
1595 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001596 }
1597
Milan Broz5ebaee62010-08-12 04:14:07 +01001598 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001599 ret = crypt_alloc_tfms(cc, cipher_api);
1600 if (ret < 0) {
1601 ti->error = "Error allocating crypto tfm";
1602 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Milan Broz5ebaee62010-08-12 04:14:07 +01001605 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001606 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001607 if (cc->iv_size)
1608 /* at least a 64 bit sector number should fit in our buffer */
1609 cc->iv_size = max(cc->iv_size,
1610 (unsigned int)(sizeof(u64) / sizeof(u8)));
1611 else if (ivmode) {
1612 DMWARN("Selected cipher does not support IVs");
1613 ivmode = NULL;
1614 }
1615
1616 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (ivmode == NULL)
1618 cc->iv_gen_ops = NULL;
1619 else if (strcmp(ivmode, "plain") == 0)
1620 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001621 else if (strcmp(ivmode, "plain64") == 0)
1622 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 else if (strcmp(ivmode, "essiv") == 0)
1624 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001625 else if (strcmp(ivmode, "benbi") == 0)
1626 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001627 else if (strcmp(ivmode, "null") == 0)
1628 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001629 else if (strcmp(ivmode, "lmk") == 0) {
1630 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001631 /*
1632 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001633 * to length of provided multi-key string.
1634 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001635 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001636 */
Milan Brozda31a072013-10-28 23:21:03 +01001637 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001638 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001639 cc->key_extra_size = cc->key_size / cc->key_parts;
1640 }
Milan Brozed04d982013-10-28 23:21:04 +01001641 } else if (strcmp(ivmode, "tcw") == 0) {
1642 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1643 cc->key_parts += 2; /* IV + whitening */
1644 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001645 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001646 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001647 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001648 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
Milan Brozda31a072013-10-28 23:21:03 +01001651 /* Initialize and set key */
1652 ret = crypt_set_key(cc, key);
1653 if (ret < 0) {
1654 ti->error = "Error decoding and setting key";
1655 goto bad;
1656 }
1657
Milan Broz28513fc2010-08-12 04:14:06 +01001658 /* Allocate IV */
1659 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1660 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1661 if (ret < 0) {
1662 ti->error = "Error creating IV";
1663 goto bad;
1664 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001665 }
1666
Milan Broz28513fc2010-08-12 04:14:06 +01001667 /* Initialize IV (set keys for ESSIV etc) */
1668 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1669 ret = cc->iv_gen_ops->init(cc);
1670 if (ret < 0) {
1671 ti->error = "Error initialising IV";
1672 goto bad;
1673 }
1674 }
1675
Milan Broz5ebaee62010-08-12 04:14:07 +01001676 ret = 0;
1677bad:
1678 kfree(cipher_api);
1679 return ret;
1680
1681bad_mem:
1682 ti->error = "Cannot allocate cipher strings";
1683 return -ENOMEM;
1684}
1685
1686/*
1687 * Construct an encryption mapping:
1688 * <cipher> <key> <iv_offset> <dev_path> <start>
1689 */
1690static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1691{
1692 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001693 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001694 unsigned long long tmpll;
1695 int ret;
Mikulas Patockad49ec522014-08-28 11:09:31 -04001696 size_t iv_size_padding;
Milan Broz772ae5f2011-08-02 12:32:08 +01001697 struct dm_arg_set as;
1698 const char *opt_string;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001699 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001700
Milan Broz772ae5f2011-08-02 12:32:08 +01001701 static struct dm_arg _args[] = {
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001702 {0, 3, "Invalid number of feature args"},
Milan Broz772ae5f2011-08-02 12:32:08 +01001703 };
1704
1705 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001706 ti->error = "Not enough arguments";
1707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
Milan Broz5ebaee62010-08-12 04:14:07 +01001710 key_size = strlen(argv[1]) >> 1;
1711
1712 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1713 if (!cc) {
1714 ti->error = "Cannot allocate encryption context";
1715 return -ENOMEM;
1716 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001717 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001718
1719 ti->private = cc;
1720 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1721 if (ret < 0)
1722 goto bad;
1723
Milan Brozddd42ed2008-02-08 02:11:07 +00001724 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001725 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001726 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
1727
1728 if (crypto_ablkcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
1729 /* Allocate the padding exactly */
1730 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
1731 & crypto_ablkcipher_alignmask(any_tfm(cc));
1732 } else {
1733 /*
1734 * If the cipher requires greater alignment than kmalloc
1735 * alignment, we don't know the exact position of the
1736 * initialization vector. We must assume worst case.
1737 */
1738 iv_size_padding = crypto_ablkcipher_alignmask(any_tfm(cc));
1739 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001740
Mikulas Patocka94f5e022015-02-13 08:25:26 -05001741 ret = -ENOMEM;
Milan Brozddd42ed2008-02-08 02:11:07 +00001742 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
Mikulas Patockad49ec522014-08-28 11:09:31 -04001743 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00001744 if (!cc->req_pool) {
1745 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001746 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001747 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001748
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001749 cc->per_bio_data_size = ti->per_bio_data_size =
Mikulas Patockad49ec522014-08-28 11:09:31 -04001750 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
1751 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
1752 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001753
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001754 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001756 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001757 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759
Jens Axboebb799ca2008-12-10 15:35:05 +01001760 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001761 if (!cc->bs) {
1762 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001763 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001764 }
1765
Mikulas Patocka7145c242015-02-13 08:24:41 -05001766 mutex_init(&cc->bio_alloc_lock);
1767
Milan Broz28513fc2010-08-12 04:14:06 +01001768 ret = -EINVAL;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001769 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001770 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001771 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001773 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Milan Broz28513fc2010-08-12 04:14:06 +01001775 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1776 ti->error = "Device lookup failed";
1777 goto bad;
1778 }
1779
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001780 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001781 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001782 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001784 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Milan Broz772ae5f2011-08-02 12:32:08 +01001786 argv += 5;
1787 argc -= 5;
1788
1789 /* Optional parameters */
1790 if (argc) {
1791 as.argc = argc;
1792 as.argv = argv;
1793
1794 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1795 if (ret)
1796 goto bad;
1797
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001798 while (opt_params--) {
1799 opt_string = dm_shift_arg(&as);
1800 if (!opt_string) {
1801 ti->error = "Not enough feature arguments";
1802 goto bad;
1803 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001804
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001805 if (!strcasecmp(opt_string, "allow_discards"))
1806 ti->num_discard_bios = 1;
1807
1808 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
1809 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
1810
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001811 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
1812 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
1813
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001814 else {
1815 ti->error = "Invalid feature arguments";
1816 goto bad;
1817 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001818 }
1819 }
1820
Milan Broz28513fc2010-08-12 04:14:06 +01001821 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04001822 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001823 if (!cc->io_queue) {
1824 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001825 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001826 }
1827
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001828 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
1829 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
1830 else
1831 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
1832 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01001833 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001834 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001835 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001836 }
1837
Mikulas Patockadc267622015-02-13 08:25:59 -05001838 init_waitqueue_head(&cc->write_thread_wait);
1839 INIT_LIST_HEAD(&cc->write_thread_list);
1840
1841 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
1842 if (IS_ERR(cc->write_thread)) {
1843 ret = PTR_ERR(cc->write_thread);
1844 cc->write_thread = NULL;
1845 ti->error = "Couldn't spawn write thread";
1846 goto bad;
1847 }
1848 wake_up_process(cc->write_thread);
1849
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001850 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001851 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854
Milan Broz28513fc2010-08-12 04:14:06 +01001855bad:
1856 crypt_dtr(ti);
1857 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001860static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001862 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001863 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001864
Milan Broz772ae5f2011-08-02 12:32:08 +01001865 /*
1866 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1867 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1868 * - for REQ_DISCARD caller must use flush if IO ordering matters
1869 */
1870 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001871 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001872 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001873 bio->bi_iter.bi_sector = cc->start +
1874 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001875 return DM_MAPIO_REMAPPED;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001878 io = dm_per_bio_data(bio, cc->per_bio_data_size);
1879 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
1880 io->ctx.req = (struct ablkcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001881
Milan Broz20c82532011-01-13 19:59:53 +00001882 if (bio_data_dir(io->base_bio) == READ) {
1883 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05001884 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00001885 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001886 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001888 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001891static void crypt_status(struct dm_target *ti, status_type_t type,
1892 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Milan Broz5ebaee62010-08-12 04:14:07 +01001894 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001895 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001896 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 switch (type) {
1899 case STATUSTYPE_INFO:
1900 result[0] = '\0';
1901 break;
1902
1903 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001904 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001906 if (cc->key_size > 0)
1907 for (i = 0; i < cc->key_size; i++)
1908 DMEMIT("%02x", cc->key[i]);
1909 else
1910 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Andrew Morton4ee218c2006-03-27 01:17:48 -08001912 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1913 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001914
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001915 num_feature_args += !!ti->num_discard_bios;
1916 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001917 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001918 if (num_feature_args) {
1919 DMEMIT(" %d", num_feature_args);
1920 if (ti->num_discard_bios)
1921 DMEMIT(" allow_discards");
1922 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
1923 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001924 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
1925 DMEMIT(" submit_from_crypt_cpus");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001926 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Milan Broze48d4bb2006-10-03 01:15:37 -07001932static void crypt_postsuspend(struct dm_target *ti)
1933{
1934 struct crypt_config *cc = ti->private;
1935
1936 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1937}
1938
1939static int crypt_preresume(struct dm_target *ti)
1940{
1941 struct crypt_config *cc = ti->private;
1942
1943 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1944 DMERR("aborting resume - crypt key is not set.");
1945 return -EAGAIN;
1946 }
1947
1948 return 0;
1949}
1950
1951static void crypt_resume(struct dm_target *ti)
1952{
1953 struct crypt_config *cc = ti->private;
1954
1955 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1956}
1957
1958/* Message interface
1959 * key set <key>
1960 * key wipe
1961 */
1962static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1963{
1964 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001965 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001966
1967 if (argc < 2)
1968 goto error;
1969
Mike Snitzer498f0102011-08-02 12:32:04 +01001970 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001971 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1972 DMWARN("not suspended during key manipulation.");
1973 return -EINVAL;
1974 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001975 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001976 ret = crypt_set_key(cc, argv[2]);
1977 if (ret)
1978 return ret;
1979 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1980 ret = cc->iv_gen_ops->init(cc);
1981 return ret;
1982 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001983 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001984 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1985 ret = cc->iv_gen_ops->wipe(cc);
1986 if (ret)
1987 return ret;
1988 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001989 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001990 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001991 }
1992
1993error:
1994 DMWARN("unrecognised message received.");
1995 return -EINVAL;
1996}
1997
Milan Brozd41e26b2008-07-21 12:00:40 +01001998static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1999 struct bio_vec *biovec, int max_size)
2000{
2001 struct crypt_config *cc = ti->private;
2002 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
2003
2004 if (!q->merge_bvec_fn)
2005 return max_size;
2006
2007 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01002008 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01002009
2010 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2011}
2012
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002013static int crypt_iterate_devices(struct dm_target *ti,
2014 iterate_devices_callout_fn fn, void *data)
2015{
2016 struct crypt_config *cc = ti->private;
2017
Mike Snitzer5dea2712009-07-23 20:30:42 +01002018 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002019}
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021static struct target_type crypt_target = {
2022 .name = "crypt",
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002023 .version = {1, 14, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 .module = THIS_MODULE,
2025 .ctr = crypt_ctr,
2026 .dtr = crypt_dtr,
2027 .map = crypt_map,
2028 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002029 .postsuspend = crypt_postsuspend,
2030 .preresume = crypt_preresume,
2031 .resume = crypt_resume,
2032 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01002033 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002034 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035};
2036
2037static int __init dm_crypt_init(void)
2038{
2039 int r;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002042 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002043 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return r;
2046}
2047
2048static void __exit dm_crypt_exit(void)
2049{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002050 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
2053module_init(dm_crypt_init);
2054module_exit(dm_crypt_exit);
2055
Jana Saoutbf14299f2014-06-24 14:27:04 -04002056MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2058MODULE_LICENSE("GPL");