Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Christophe Saout <christophe@saout.de> |
| 3 | * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 9 | #include <linux/completion.h> |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 10 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #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 Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 20 | #include <linux/backing-dev.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 21 | #include <linux/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 22 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/page.h> |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 25 | #include <crypto/hash.h> |
| 26 | #include <crypto/md5.h> |
| 27 | #include <crypto/algapi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 29 | #include <linux/device-mapper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 31 | #define DM_MSG_PREFIX "crypt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * context holding the current state of a multi-part conversion |
| 35 | */ |
| 36 | struct convert_context { |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 37 | struct completion restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct bio *bio_in; |
| 39 | struct bio *bio_out; |
| 40 | unsigned int offset_in; |
| 41 | unsigned int offset_out; |
| 42 | unsigned int idx_in; |
| 43 | unsigned int idx_out; |
| 44 | sector_t sector; |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 45 | atomic_t cc_pending; |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 46 | struct ablkcipher_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 49 | /* |
| 50 | * per bio private data |
| 51 | */ |
| 52 | struct dm_crypt_io { |
| 53 | struct dm_target *target; |
| 54 | struct bio *base_bio; |
| 55 | struct work_struct work; |
| 56 | |
| 57 | struct convert_context ctx; |
| 58 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 59 | atomic_t io_pending; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 60 | int error; |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 61 | sector_t sector; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 62 | struct dm_crypt_io *base_io; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 65 | struct dm_crypt_request { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 66 | struct convert_context *ctx; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 67 | struct scatterlist sg_in; |
| 68 | struct scatterlist sg_out; |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 69 | sector_t iv_sector; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct crypt_config; |
| 73 | |
| 74 | struct crypt_iv_operations { |
| 75 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 76 | const char *opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | void (*dtr)(struct crypt_config *cc); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 78 | int (*init)(struct crypt_config *cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 79 | int (*wipe)(struct crypt_config *cc); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 80 | int (*generator)(struct crypt_config *cc, u8 *iv, |
| 81 | struct dm_crypt_request *dmreq); |
| 82 | int (*post)(struct crypt_config *cc, u8 *iv, |
| 83 | struct dm_crypt_request *dmreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 86 | struct iv_essiv_private { |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 87 | struct crypto_hash *hash_tfm; |
| 88 | u8 *salt; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct iv_benbi_private { |
| 92 | int shift; |
| 93 | }; |
| 94 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 95 | #define LMK_SEED_SIZE 64 /* hash + 0 */ |
| 96 | struct iv_lmk_private { |
| 97 | struct crypto_shash *hash_tfm; |
| 98 | u8 *seed; |
| 99 | }; |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Crypt: maps a linear range of a block device |
| 103 | * and encrypts / decrypts at the same time. |
| 104 | */ |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 105 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID }; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 108 | * The fields in here must be read only after initialization, |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 109 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct crypt_config { |
| 111 | struct dm_dev *dev; |
| 112 | sector_t start; |
| 113 | |
| 114 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 115 | * pool for per bio private data, crypto requests and |
| 116 | * encryption requeusts/buffer pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | mempool_t *io_pool; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 119 | mempool_t *req_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mempool_t *page_pool; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 121 | struct bio_set *bs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 123 | struct workqueue_struct *io_queue; |
| 124 | struct workqueue_struct *crypt_queue; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 125 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 126 | char *cipher; |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 127 | char *cipher_string; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | struct crypt_iv_operations *iv_gen_ops; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 130 | union { |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 131 | struct iv_essiv_private essiv; |
| 132 | struct iv_benbi_private benbi; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 133 | struct iv_lmk_private lmk; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 134 | } iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | sector_t iv_offset; |
| 136 | unsigned int iv_size; |
| 137 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 138 | /* ESSIV: struct crypto_cipher *essiv_tfm */ |
| 139 | void *iv_private; |
| 140 | struct crypto_ablkcipher **tfms; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 141 | unsigned tfms_count; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 144 | * Layout of each crypto request: |
| 145 | * |
| 146 | * struct ablkcipher_request |
| 147 | * context |
| 148 | * padding |
| 149 | * struct dm_crypt_request |
| 150 | * padding |
| 151 | * IV |
| 152 | * |
| 153 | * The padding is added so that dm_crypt_request and the IV are |
| 154 | * correctly aligned. |
| 155 | */ |
| 156 | unsigned int dmreq_start; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 157 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 158 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | unsigned int key_size; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 160 | unsigned int key_parts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | u8 key[0]; |
| 162 | }; |
| 163 | |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 164 | #define MIN_IOS 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | #define MIN_POOL_PAGES 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 167 | static struct kmem_cache *_crypt_io_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 169 | static void clone_init(struct dm_crypt_io *, struct bio *); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 170 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 171 | static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq); |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 172 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 173 | /* |
| 174 | * Use this to access cipher attributes that are the same for each CPU. |
| 175 | */ |
| 176 | static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc) |
| 177 | { |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 178 | return cc->tfms[0]; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * Different IV generation algorithms: |
| 183 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 184 | * plain: the initial vector is the 32-bit little-endian version of the sector |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 185 | * number, padded with zeros if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 187 | * plain64: the initial vector is the 64-bit little-endian version of the sector |
| 188 | * number, padded with zeros if necessary. |
| 189 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 190 | * essiv: "encrypted sector|salt initial vector", the sector number is |
| 191 | * encrypted with the bulk cipher using a salt as key. The salt |
| 192 | * should be derived from the bulk cipher's key via hashing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 194 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
| 195 | * (needed for LRW-32-AES and possible other narrow block modes) |
| 196 | * |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 197 | * null: the initial vector is always zero. Provides compatibility with |
| 198 | * obsolete loop_fish2 devices. Do not use for new devices. |
| 199 | * |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 200 | * lmk: Compatible implementation of the block chaining mode used |
| 201 | * by the Loop-AES block device encryption system |
| 202 | * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ |
| 203 | * It operates on full 512 byte sectors and uses CBC |
| 204 | * with an IV derived from the sector number, the data and |
| 205 | * optionally extra IV seed. |
| 206 | * This means that after decryption the first block |
| 207 | * of sector must be tweaked according to decrypted data. |
| 208 | * Loop-AES can use three encryption schemes: |
| 209 | * version 1: is plain aes-cbc mode |
| 210 | * version 2: uses 64 multikey scheme with lmk IV generator |
| 211 | * version 3: the same as version 2 with additional IV seed |
| 212 | * (it uses 65 keys, last key is used as IV seed) |
| 213 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * plumb: unimplemented, see: |
| 215 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 216 | */ |
| 217 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 218 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, |
| 219 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 222 | *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 227 | static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 228 | struct dm_crypt_request *dmreq) |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 229 | { |
| 230 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 231 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 236 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
| 237 | static int crypt_iv_essiv_init(struct crypt_config *cc) |
| 238 | { |
| 239 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 240 | struct hash_desc desc; |
| 241 | struct scatterlist sg; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 242 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 243 | int err; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 244 | |
| 245 | sg_init_one(&sg, cc->key, cc->key_size); |
| 246 | desc.tfm = essiv->hash_tfm; |
| 247 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 248 | |
| 249 | err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt); |
| 250 | if (err) |
| 251 | return err; |
| 252 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 253 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 254 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 255 | err = crypto_cipher_setkey(essiv_tfm, essiv->salt, |
| 256 | crypto_hash_digestsize(essiv->hash_tfm)); |
| 257 | if (err) |
| 258 | return err; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 259 | |
| 260 | return 0; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 263 | /* Wipe salt and reset key derived from volume key */ |
| 264 | static int crypt_iv_essiv_wipe(struct crypt_config *cc) |
| 265 | { |
| 266 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 267 | unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 268 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 269 | int r, err = 0; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 270 | |
| 271 | memset(essiv->salt, 0, salt_size); |
| 272 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 273 | essiv_tfm = cc->iv_private; |
| 274 | r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size); |
| 275 | if (r) |
| 276 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 277 | |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | /* Set up per cpu cipher state */ |
| 282 | static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc, |
| 283 | struct dm_target *ti, |
| 284 | u8 *salt, unsigned saltsize) |
| 285 | { |
| 286 | struct crypto_cipher *essiv_tfm; |
| 287 | int err; |
| 288 | |
| 289 | /* Setup the essiv_tfm with the given salt */ |
| 290 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 291 | if (IS_ERR(essiv_tfm)) { |
| 292 | ti->error = "Error allocating crypto tfm for ESSIV"; |
| 293 | return essiv_tfm; |
| 294 | } |
| 295 | |
| 296 | if (crypto_cipher_blocksize(essiv_tfm) != |
| 297 | crypto_ablkcipher_ivsize(any_tfm(cc))) { |
| 298 | ti->error = "Block size of ESSIV cipher does " |
| 299 | "not match IV size of block cipher"; |
| 300 | crypto_free_cipher(essiv_tfm); |
| 301 | return ERR_PTR(-EINVAL); |
| 302 | } |
| 303 | |
| 304 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 305 | if (err) { |
| 306 | ti->error = "Failed to set key for ESSIV cipher"; |
| 307 | crypto_free_cipher(essiv_tfm); |
| 308 | return ERR_PTR(err); |
| 309 | } |
| 310 | |
| 311 | return essiv_tfm; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 314 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 315 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 316 | struct crypto_cipher *essiv_tfm; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 317 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 318 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 319 | crypto_free_hash(essiv->hash_tfm); |
| 320 | essiv->hash_tfm = NULL; |
| 321 | |
| 322 | kzfree(essiv->salt); |
| 323 | essiv->salt = NULL; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 324 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 325 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 326 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 327 | if (essiv_tfm) |
| 328 | crypto_free_cipher(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 329 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 330 | cc->iv_private = NULL; |
| 331 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 335 | const char *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 337 | struct crypto_cipher *essiv_tfm = NULL; |
| 338 | struct crypto_hash *hash_tfm = NULL; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 339 | u8 *salt = NULL; |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 340 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 342 | if (!opts) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 343 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | } |
| 346 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 347 | /* Allocate hash algorithm */ |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 348 | hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC); |
| 349 | if (IS_ERR(hash_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 350 | ti->error = "Error initializing ESSIV hash"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 351 | err = PTR_ERR(hash_tfm); |
| 352 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 355 | salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 356 | if (!salt) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 357 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 358 | err = -ENOMEM; |
| 359 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 362 | cc->iv_gen_private.essiv.salt = salt; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 363 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
| 364 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 365 | essiv_tfm = setup_essiv_cpu(cc, ti, salt, |
| 366 | crypto_hash_digestsize(hash_tfm)); |
| 367 | if (IS_ERR(essiv_tfm)) { |
| 368 | crypt_iv_essiv_dtr(cc); |
| 369 | return PTR_ERR(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 370 | } |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 371 | cc->iv_private = essiv_tfm; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return 0; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 374 | |
| 375 | bad: |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 376 | if (hash_tfm && !IS_ERR(hash_tfm)) |
| 377 | crypto_free_hash(hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 378 | kfree(salt); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 379 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 382 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, |
| 383 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 385 | |
| 386 | struct crypto_cipher *essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 389 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 390 | crypto_cipher_encrypt_one(essiv_tfm, iv, iv); |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 395 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 396 | const char *opts) |
| 397 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 398 | unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc)); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 399 | int log = ilog2(bs); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 400 | |
| 401 | /* we need to calculate how far we must shift the sector count |
| 402 | * to get the cipher block count, we use this shift in _gen */ |
| 403 | |
| 404 | if (1 << log != bs) { |
| 405 | ti->error = "cypher blocksize is not a power of 2"; |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | if (log > 9) { |
| 410 | ti->error = "cypher blocksize is > 512"; |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 414 | cc->iv_gen_private.benbi.shift = 9 - log; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) |
| 420 | { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 421 | } |
| 422 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 423 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, |
| 424 | struct dm_crypt_request *dmreq) |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 425 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 426 | __be64 val; |
| 427 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 428 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 429 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 430 | val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 431 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 436 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, |
| 437 | struct dm_crypt_request *dmreq) |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 438 | { |
| 439 | memset(iv, 0, cc->iv_size); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 444 | static void crypt_iv_lmk_dtr(struct crypt_config *cc) |
| 445 | { |
| 446 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 447 | |
| 448 | if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) |
| 449 | crypto_free_shash(lmk->hash_tfm); |
| 450 | lmk->hash_tfm = NULL; |
| 451 | |
| 452 | kzfree(lmk->seed); |
| 453 | lmk->seed = NULL; |
| 454 | } |
| 455 | |
| 456 | static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 457 | const char *opts) |
| 458 | { |
| 459 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 460 | |
| 461 | lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0); |
| 462 | if (IS_ERR(lmk->hash_tfm)) { |
| 463 | ti->error = "Error initializing LMK hash"; |
| 464 | return PTR_ERR(lmk->hash_tfm); |
| 465 | } |
| 466 | |
| 467 | /* No seed in LMK version 2 */ |
| 468 | if (cc->key_parts == cc->tfms_count) { |
| 469 | lmk->seed = NULL; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); |
| 474 | if (!lmk->seed) { |
| 475 | crypt_iv_lmk_dtr(cc); |
| 476 | ti->error = "Error kmallocing seed storage in LMK"; |
| 477 | return -ENOMEM; |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int crypt_iv_lmk_init(struct crypt_config *cc) |
| 484 | { |
| 485 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 486 | int subkey_size = cc->key_size / cc->key_parts; |
| 487 | |
| 488 | /* LMK seed is on the position of LMK_KEYS + 1 key */ |
| 489 | if (lmk->seed) |
| 490 | memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), |
| 491 | crypto_shash_digestsize(lmk->hash_tfm)); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int crypt_iv_lmk_wipe(struct crypt_config *cc) |
| 497 | { |
| 498 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 499 | |
| 500 | if (lmk->seed) |
| 501 | memset(lmk->seed, 0, LMK_SEED_SIZE); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, |
| 507 | struct dm_crypt_request *dmreq, |
| 508 | u8 *data) |
| 509 | { |
| 510 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 511 | struct { |
| 512 | struct shash_desc desc; |
| 513 | char ctx[crypto_shash_descsize(lmk->hash_tfm)]; |
| 514 | } sdesc; |
| 515 | struct md5_state md5state; |
| 516 | u32 buf[4]; |
| 517 | int i, r; |
| 518 | |
| 519 | sdesc.desc.tfm = lmk->hash_tfm; |
| 520 | sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 521 | |
| 522 | r = crypto_shash_init(&sdesc.desc); |
| 523 | if (r) |
| 524 | return r; |
| 525 | |
| 526 | if (lmk->seed) { |
| 527 | r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE); |
| 528 | if (r) |
| 529 | return r; |
| 530 | } |
| 531 | |
| 532 | /* Sector is always 512B, block size 16, add data of blocks 1-31 */ |
| 533 | r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31); |
| 534 | if (r) |
| 535 | return r; |
| 536 | |
| 537 | /* Sector is cropped to 56 bits here */ |
| 538 | buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); |
| 539 | buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); |
| 540 | buf[2] = cpu_to_le32(4024); |
| 541 | buf[3] = 0; |
| 542 | r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf)); |
| 543 | if (r) |
| 544 | return r; |
| 545 | |
| 546 | /* No MD5 padding here */ |
| 547 | r = crypto_shash_export(&sdesc.desc, &md5state); |
| 548 | if (r) |
| 549 | return r; |
| 550 | |
| 551 | for (i = 0; i < MD5_HASH_WORDS; i++) |
| 552 | __cpu_to_le32s(&md5state.hash[i]); |
| 553 | memcpy(iv, &md5state.hash, cc->iv_size); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, |
| 559 | struct dm_crypt_request *dmreq) |
| 560 | { |
| 561 | u8 *src; |
| 562 | int r = 0; |
| 563 | |
| 564 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 565 | src = kmap_atomic(sg_page(&dmreq->sg_in)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 566 | r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset); |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 567 | kunmap_atomic(src); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 568 | } else |
| 569 | memset(iv, 0, cc->iv_size); |
| 570 | |
| 571 | return r; |
| 572 | } |
| 573 | |
| 574 | static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, |
| 575 | struct dm_crypt_request *dmreq) |
| 576 | { |
| 577 | u8 *dst; |
| 578 | int r; |
| 579 | |
| 580 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) |
| 581 | return 0; |
| 582 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 583 | dst = kmap_atomic(sg_page(&dmreq->sg_out)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 584 | r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset); |
| 585 | |
| 586 | /* Tweak the first block of plaintext sector */ |
| 587 | if (!r) |
| 588 | crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size); |
| 589 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 590 | kunmap_atomic(dst); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 591 | return r; |
| 592 | } |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
| 595 | .generator = crypt_iv_plain_gen |
| 596 | }; |
| 597 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 598 | static struct crypt_iv_operations crypt_iv_plain64_ops = { |
| 599 | .generator = crypt_iv_plain64_gen |
| 600 | }; |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | static struct crypt_iv_operations crypt_iv_essiv_ops = { |
| 603 | .ctr = crypt_iv_essiv_ctr, |
| 604 | .dtr = crypt_iv_essiv_dtr, |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 605 | .init = crypt_iv_essiv_init, |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 606 | .wipe = crypt_iv_essiv_wipe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | .generator = crypt_iv_essiv_gen |
| 608 | }; |
| 609 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 610 | static struct crypt_iv_operations crypt_iv_benbi_ops = { |
| 611 | .ctr = crypt_iv_benbi_ctr, |
| 612 | .dtr = crypt_iv_benbi_dtr, |
| 613 | .generator = crypt_iv_benbi_gen |
| 614 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 616 | static struct crypt_iv_operations crypt_iv_null_ops = { |
| 617 | .generator = crypt_iv_null_gen |
| 618 | }; |
| 619 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 620 | static struct crypt_iv_operations crypt_iv_lmk_ops = { |
| 621 | .ctr = crypt_iv_lmk_ctr, |
| 622 | .dtr = crypt_iv_lmk_dtr, |
| 623 | .init = crypt_iv_lmk_init, |
| 624 | .wipe = crypt_iv_lmk_wipe, |
| 625 | .generator = crypt_iv_lmk_gen, |
| 626 | .post = crypt_iv_lmk_post |
| 627 | }; |
| 628 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 629 | static void crypt_convert_init(struct crypt_config *cc, |
| 630 | struct convert_context *ctx, |
| 631 | struct bio *bio_out, struct bio *bio_in, |
Milan Broz | fcd369d | 2008-02-08 02:10:41 +0000 | [diff] [blame] | 632 | sector_t sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
| 634 | ctx->bio_in = bio_in; |
| 635 | ctx->bio_out = bio_out; |
| 636 | ctx->offset_in = 0; |
| 637 | ctx->offset_out = 0; |
| 638 | ctx->idx_in = bio_in ? bio_in->bi_idx : 0; |
| 639 | ctx->idx_out = bio_out ? bio_out->bi_idx : 0; |
| 640 | ctx->sector = sector + cc->iv_offset; |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 641 | init_completion(&ctx->restart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 644 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
| 645 | struct ablkcipher_request *req) |
| 646 | { |
| 647 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); |
| 648 | } |
| 649 | |
| 650 | static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc, |
| 651 | struct dm_crypt_request *dmreq) |
| 652 | { |
| 653 | return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start); |
| 654 | } |
| 655 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 656 | static u8 *iv_of_dmreq(struct crypt_config *cc, |
| 657 | struct dm_crypt_request *dmreq) |
| 658 | { |
| 659 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
| 660 | crypto_ablkcipher_alignmask(any_tfm(cc)) + 1); |
| 661 | } |
| 662 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 663 | static int crypt_convert_block(struct crypt_config *cc, |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 664 | struct convert_context *ctx, |
| 665 | struct ablkcipher_request *req) |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 666 | { |
| 667 | struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in); |
| 668 | struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 669 | struct dm_crypt_request *dmreq; |
| 670 | u8 *iv; |
| 671 | int r = 0; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 672 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 673 | dmreq = dmreq_of_req(cc, req); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 674 | iv = iv_of_dmreq(cc, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 675 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 676 | dmreq->iv_sector = ctx->sector; |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 677 | dmreq->ctx = ctx; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 678 | sg_init_table(&dmreq->sg_in, 1); |
| 679 | sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT, |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 680 | bv_in->bv_offset + ctx->offset_in); |
| 681 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 682 | sg_init_table(&dmreq->sg_out, 1); |
| 683 | sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT, |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 684 | bv_out->bv_offset + ctx->offset_out); |
| 685 | |
| 686 | ctx->offset_in += 1 << SECTOR_SHIFT; |
| 687 | if (ctx->offset_in >= bv_in->bv_len) { |
| 688 | ctx->offset_in = 0; |
| 689 | ctx->idx_in++; |
| 690 | } |
| 691 | |
| 692 | ctx->offset_out += 1 << SECTOR_SHIFT; |
| 693 | if (ctx->offset_out >= bv_out->bv_len) { |
| 694 | ctx->offset_out = 0; |
| 695 | ctx->idx_out++; |
| 696 | } |
| 697 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 698 | if (cc->iv_gen_ops) { |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 699 | r = cc->iv_gen_ops->generator(cc, iv, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 700 | if (r < 0) |
| 701 | return r; |
| 702 | } |
| 703 | |
| 704 | ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out, |
| 705 | 1 << SECTOR_SHIFT, iv); |
| 706 | |
| 707 | if (bio_data_dir(ctx->bio_in) == WRITE) |
| 708 | r = crypto_ablkcipher_encrypt(req); |
| 709 | else |
| 710 | r = crypto_ablkcipher_decrypt(req); |
| 711 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 712 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 713 | r = cc->iv_gen_ops->post(cc, iv, dmreq); |
| 714 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 715 | return r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 718 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 719 | int error); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 720 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 721 | static void crypt_alloc_req(struct crypt_config *cc, |
| 722 | struct convert_context *ctx) |
| 723 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 724 | unsigned key_index = ctx->sector & (cc->tfms_count - 1); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 725 | |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 726 | if (!ctx->req) |
| 727 | ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 728 | |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 729 | ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]); |
| 730 | ablkcipher_request_set_callback(ctx->req, |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 731 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 732 | kcryptd_async_done, dmreq_of_req(cc, ctx->req)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | /* |
| 736 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 737 | */ |
| 738 | static int crypt_convert(struct crypt_config *cc, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 739 | struct convert_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | { |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 741 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 743 | atomic_set(&ctx->cc_pending, 1); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | while(ctx->idx_in < ctx->bio_in->bi_vcnt && |
| 746 | ctx->idx_out < ctx->bio_out->bi_vcnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 748 | crypt_alloc_req(cc, ctx); |
| 749 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 750 | atomic_inc(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 751 | |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 752 | r = crypt_convert_block(cc, ctx, ctx->req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 753 | |
| 754 | switch (r) { |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 755 | /* async */ |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 756 | case -EBUSY: |
| 757 | wait_for_completion(&ctx->restart); |
| 758 | INIT_COMPLETION(ctx->restart); |
| 759 | /* fall through*/ |
| 760 | case -EINPROGRESS: |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 761 | ctx->req = NULL; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 762 | ctx->sector++; |
| 763 | continue; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 764 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 765 | /* sync */ |
| 766 | case 0: |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 767 | atomic_dec(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 768 | ctx->sector++; |
Milan Broz | c7f1b20 | 2008-07-02 09:34:28 +0100 | [diff] [blame] | 769 | cond_resched(); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 770 | continue; |
| 771 | |
| 772 | /* error */ |
| 773 | default: |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 774 | atomic_dec(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 775 | return r; |
| 776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 779 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 782 | static void dm_crypt_bio_destructor(struct bio *bio) |
| 783 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 784 | struct dm_crypt_io *io = bio->bi_private; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 785 | struct crypt_config *cc = io->target->private; |
| 786 | |
| 787 | bio_free(bio, cc->bs); |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 788 | } |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | /* |
| 791 | * Generate a new unfragmented bio with the given size |
| 792 | * This should never violate the device limitations |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 793 | * May return a smaller bio when running out of pages, indicated by |
| 794 | * *out_of_pages set to 1. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | */ |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 796 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size, |
| 797 | unsigned *out_of_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | { |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 799 | struct crypt_config *cc = io->target->private; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 800 | struct bio *clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 802 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 803 | unsigned i, len; |
| 804 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Olaf Kirch | 2f9941b | 2007-05-09 02:32:53 -0700 | [diff] [blame] | 806 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 807 | if (!clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 810 | clone_init(io, clone); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 811 | *out_of_pages = 0; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 812 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 813 | for (i = 0; i < nr_iovecs; i++) { |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 814 | page = mempool_alloc(cc->page_pool, gfp_mask); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 815 | if (!page) { |
| 816 | *out_of_pages = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | break; |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
| 820 | /* |
Mikulas Patocka | aeb2dea | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 821 | * If additional pages cannot be allocated without waiting, |
| 822 | * return a partially-allocated bio. The caller will then try |
| 823 | * to allocate more bios while submitting this partial bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | */ |
Mikulas Patocka | aeb2dea | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 825 | gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 827 | len = (size > PAGE_SIZE) ? PAGE_SIZE : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 829 | if (!bio_add_page(clone, page, len, 0)) { |
| 830 | mempool_free(page, cc->page_pool); |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 837 | if (!clone->bi_size) { |
| 838 | bio_put(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return NULL; |
| 840 | } |
| 841 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 842 | return clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 845 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 847 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | struct bio_vec *bv; |
| 849 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 850 | for (i = 0; i < clone->bi_vcnt; i++) { |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 851 | bv = bio_iovec_idx(clone, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | BUG_ON(!bv->bv_page); |
| 853 | mempool_free(bv->bv_page, cc->page_pool); |
| 854 | bv->bv_page = NULL; |
| 855 | } |
| 856 | } |
| 857 | |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 858 | static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti, |
| 859 | struct bio *bio, sector_t sector) |
| 860 | { |
| 861 | struct crypt_config *cc = ti->private; |
| 862 | struct dm_crypt_io *io; |
| 863 | |
| 864 | io = mempool_alloc(cc->io_pool, GFP_NOIO); |
| 865 | io->target = ti; |
| 866 | io->base_bio = bio; |
| 867 | io->sector = sector; |
| 868 | io->error = 0; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 869 | io->base_io = NULL; |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 870 | io->ctx.req = NULL; |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 871 | atomic_set(&io->io_pending, 0); |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 872 | |
| 873 | return io; |
| 874 | } |
| 875 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 876 | static void crypt_inc_pending(struct dm_crypt_io *io) |
| 877 | { |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 878 | atomic_inc(&io->io_pending); |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* |
| 882 | * One of the bios was finished. Check for completion of |
| 883 | * the whole request and correctly clean up the buffer. |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 884 | * If base_io is set, wait for the last fragment to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | */ |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 886 | static void crypt_dec_pending(struct dm_crypt_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 888 | struct crypt_config *cc = io->target->private; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 889 | struct bio *base_bio = io->base_bio; |
| 890 | struct dm_crypt_io *base_io = io->base_io; |
| 891 | int error = io->error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 893 | if (!atomic_dec_and_test(&io->io_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return; |
| 895 | |
Mikulas Patocka | 30c273a | 2012-03-27 14:16:33 -0700 | [diff] [blame] | 896 | if (io->ctx.req) |
| 897 | mempool_free(io->ctx.req, cc->req_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | mempool_free(io, cc->io_pool); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 899 | |
| 900 | if (likely(!base_io)) |
| 901 | bio_endio(base_bio, error); |
| 902 | else { |
| 903 | if (error && !base_io->error) |
| 904 | base_io->error = error; |
| 905 | crypt_dec_pending(base_io); |
| 906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 910 | * kcryptd/kcryptd_io: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | * |
| 912 | * Needed because it would be very unwise to do decryption in an |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 913 | * interrupt context. |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 914 | * |
| 915 | * kcryptd performs the actual encryption or decryption. |
| 916 | * |
| 917 | * kcryptd_io performs the IO submission. |
| 918 | * |
| 919 | * They must be separated as otherwise the final stages could be |
| 920 | * starved by new requests which can block in the first stages due |
| 921 | * to memory allocation. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 922 | * |
| 923 | * The work is done per CPU global for all dm-crypt instances. |
| 924 | * They should not depend on each other and do not block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 926 | static void crypt_endio(struct bio *clone, int error) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 927 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 928 | struct dm_crypt_io *io = clone->bi_private; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 929 | struct crypt_config *cc = io->target->private; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 930 | unsigned rw = bio_data_dir(clone); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 931 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 932 | if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error)) |
| 933 | error = -EIO; |
| 934 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 935 | /* |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 936 | * free the processed pages |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 937 | */ |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 938 | if (rw == WRITE) |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 939 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 940 | |
| 941 | bio_put(clone); |
| 942 | |
| 943 | if (rw == READ && !error) { |
| 944 | kcryptd_queue_crypt(io); |
| 945 | return; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 946 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 947 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 948 | if (unlikely(error)) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 949 | io->error = error; |
| 950 | |
| 951 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 954 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 955 | { |
| 956 | struct crypt_config *cc = io->target->private; |
| 957 | |
| 958 | clone->bi_private = io; |
| 959 | clone->bi_end_io = crypt_endio; |
| 960 | clone->bi_bdev = cc->dev->bdev; |
| 961 | clone->bi_rw = io->base_bio->bi_rw; |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 962 | clone->bi_destructor = dm_crypt_bio_destructor; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 965 | static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 966 | { |
| 967 | struct crypt_config *cc = io->target->private; |
| 968 | struct bio *base_bio = io->base_bio; |
| 969 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 970 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 971 | /* |
| 972 | * The block layer might modify the bvec array, so always |
| 973 | * copy the required bvecs because we need the original |
| 974 | * one in order to decrypt the whole bio data *afterwards*. |
| 975 | */ |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 976 | clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 977 | if (!clone) |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 978 | return 1; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 979 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 980 | crypt_inc_pending(io); |
| 981 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 982 | clone_init(io, clone); |
| 983 | clone->bi_idx = 0; |
| 984 | clone->bi_vcnt = bio_segments(base_bio); |
| 985 | clone->bi_size = base_bio->bi_size; |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 986 | clone->bi_sector = cc->start + io->sector; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 987 | memcpy(clone->bi_io_vec, bio_iovec(base_bio), |
| 988 | sizeof(struct bio_vec) * clone->bi_vcnt); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 989 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 990 | generic_make_request(clone); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 991 | return 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 994 | static void kcryptd_io_write(struct dm_crypt_io *io) |
| 995 | { |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 996 | struct bio *clone = io->ctx.bio_out; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 997 | generic_make_request(clone); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1000 | static void kcryptd_io(struct work_struct *work) |
| 1001 | { |
| 1002 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1003 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1004 | if (bio_data_dir(io->base_bio) == READ) { |
| 1005 | crypt_inc_pending(io); |
| 1006 | if (kcryptd_io_read(io, GFP_NOIO)) |
| 1007 | io->error = -ENOMEM; |
| 1008 | crypt_dec_pending(io); |
| 1009 | } else |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1010 | kcryptd_io_write(io); |
| 1011 | } |
| 1012 | |
| 1013 | static void kcryptd_queue_io(struct dm_crypt_io *io) |
| 1014 | { |
| 1015 | struct crypt_config *cc = io->target->private; |
| 1016 | |
| 1017 | INIT_WORK(&io->work, kcryptd_io); |
| 1018 | queue_work(cc->io_queue, &io->work); |
| 1019 | } |
| 1020 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1021 | static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1022 | { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1023 | struct bio *clone = io->ctx.bio_out; |
| 1024 | struct crypt_config *cc = io->target->private; |
| 1025 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1026 | if (unlikely(io->error < 0)) { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1027 | crypt_free_buffer_pages(cc, clone); |
| 1028 | bio_put(clone); |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1029 | crypt_dec_pending(io); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* crypt_convert should have filled the clone bio */ |
| 1034 | BUG_ON(io->ctx.idx_out < clone->bi_vcnt); |
| 1035 | |
| 1036 | clone->bi_sector = cc->start + io->sector; |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1037 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1038 | if (async) |
| 1039 | kcryptd_queue_io(io); |
Alasdair G Kergon | 1e37bb8 | 2008-10-10 13:37:05 +0100 | [diff] [blame] | 1040 | else |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1041 | generic_make_request(clone); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1044 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1045 | { |
| 1046 | struct crypt_config *cc = io->target->private; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1047 | struct bio *clone; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1048 | struct dm_crypt_io *new_io; |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1049 | int crypt_finished; |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1050 | unsigned out_of_pages = 0; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1051 | unsigned remaining = io->base_bio->bi_size; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1052 | sector_t sector = io->sector; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1053 | int r; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1054 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1055 | /* |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1056 | * Prevent io from disappearing until this function completes. |
| 1057 | */ |
| 1058 | crypt_inc_pending(io); |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1059 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1060 | |
| 1061 | /* |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1062 | * The allocated buffers can be smaller than the whole bio, |
| 1063 | * so repeat the whole process until all the data can be handled. |
| 1064 | */ |
| 1065 | while (remaining) { |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1066 | clone = crypt_alloc_buffer(io, remaining, &out_of_pages); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1067 | if (unlikely(!clone)) { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1068 | io->error = -ENOMEM; |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1069 | break; |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1070 | } |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1071 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1072 | io->ctx.bio_out = clone; |
| 1073 | io->ctx.idx_out = 0; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1074 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1075 | remaining -= clone->bi_size; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1076 | sector += bio_sectors(clone); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1077 | |
Milan Broz | 4e59409 | 2008-10-10 13:37:07 +0100 | [diff] [blame] | 1078 | crypt_inc_pending(io); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1079 | |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1080 | r = crypt_convert(cc, &io->ctx); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1081 | if (r < 0) |
| 1082 | io->error = -EIO; |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 1083 | crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1084 | |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1085 | /* Encryption was already finished, submit io now */ |
| 1086 | if (crypt_finished) { |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1087 | kcryptd_crypt_write_io_submit(io, 0); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1088 | |
| 1089 | /* |
| 1090 | * If there was an error, do not try next fragments. |
| 1091 | * For async, error is processed in async handler. |
| 1092 | */ |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1093 | if (unlikely(r < 0)) |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1094 | break; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1095 | |
| 1096 | io->sector = sector; |
Milan Broz | 4e59409 | 2008-10-10 13:37:07 +0100 | [diff] [blame] | 1097 | } |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1098 | |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1099 | /* |
| 1100 | * Out of memory -> run queues |
| 1101 | * But don't wait if split was due to the io size restriction |
| 1102 | */ |
| 1103 | if (unlikely(out_of_pages)) |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 1104 | congestion_wait(BLK_RW_ASYNC, HZ/100); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1105 | |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1106 | /* |
| 1107 | * With async crypto it is unsafe to share the crypto context |
| 1108 | * between fragments, so switch to a new dm_crypt_io structure. |
| 1109 | */ |
| 1110 | if (unlikely(!crypt_finished && remaining)) { |
| 1111 | new_io = crypt_io_alloc(io->target, io->base_bio, |
| 1112 | sector); |
| 1113 | crypt_inc_pending(new_io); |
| 1114 | crypt_convert_init(cc, &new_io->ctx, NULL, |
| 1115 | io->base_bio, sector); |
| 1116 | new_io->ctx.idx_in = io->ctx.idx_in; |
| 1117 | new_io->ctx.offset_in = io->ctx.offset_in; |
| 1118 | |
| 1119 | /* |
| 1120 | * Fragments after the first use the base_io |
| 1121 | * pending count. |
| 1122 | */ |
| 1123 | if (!io->base_io) |
| 1124 | new_io->base_io = io; |
| 1125 | else { |
| 1126 | new_io->base_io = io->base_io; |
| 1127 | crypt_inc_pending(io->base_io); |
| 1128 | crypt_dec_pending(io); |
| 1129 | } |
| 1130 | |
| 1131 | io = new_io; |
| 1132 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1133 | } |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1134 | |
| 1135 | crypt_dec_pending(io); |
Milan Broz | 84131db | 2008-02-08 02:10:59 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1138 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1139 | { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1140 | crypt_dec_pending(io); |
| 1141 | } |
| 1142 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1143 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1144 | { |
| 1145 | struct crypt_config *cc = io->target->private; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1146 | int r = 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1147 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1148 | crypt_inc_pending(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1149 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1150 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 1151 | io->sector); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1152 | |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1153 | r = crypt_convert(cc, &io->ctx); |
| 1154 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1155 | if (r < 0) |
| 1156 | io->error = -EIO; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1157 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 1158 | if (atomic_dec_and_test(&io->ctx.cc_pending)) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1159 | kcryptd_crypt_read_done(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1160 | |
| 1161 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1164 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 1165 | int error) |
| 1166 | { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1167 | struct dm_crypt_request *dmreq = async_req->data; |
| 1168 | struct convert_context *ctx = dmreq->ctx; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1169 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); |
| 1170 | struct crypt_config *cc = io->target->private; |
| 1171 | |
| 1172 | if (error == -EINPROGRESS) { |
| 1173 | complete(&ctx->restart); |
| 1174 | return; |
| 1175 | } |
| 1176 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1177 | if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 1178 | error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq); |
| 1179 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1180 | if (error < 0) |
| 1181 | io->error = -EIO; |
| 1182 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1183 | mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1184 | |
Mikulas Patocka | 815bc97 | 2012-03-27 13:17:56 -0700 | [diff] [blame] | 1185 | if (!atomic_dec_and_test(&ctx->cc_pending)) |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1186 | return; |
| 1187 | |
| 1188 | if (bio_data_dir(io->base_bio) == READ) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1189 | kcryptd_crypt_read_done(io); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1190 | else |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1191 | kcryptd_crypt_write_io_submit(io, 1); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1194 | static void kcryptd_crypt(struct work_struct *work) |
| 1195 | { |
| 1196 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1197 | |
| 1198 | if (bio_data_dir(io->base_bio) == READ) |
| 1199 | kcryptd_crypt_read_convert(io); |
| 1200 | else |
| 1201 | kcryptd_crypt_write_convert(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1204 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
| 1205 | { |
| 1206 | struct crypt_config *cc = io->target->private; |
| 1207 | |
| 1208 | INIT_WORK(&io->work, kcryptd_crypt); |
| 1209 | queue_work(cc->crypt_queue, &io->work); |
| 1210 | } |
| 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | /* |
| 1213 | * Decode key from its hex representation |
| 1214 | */ |
| 1215 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) |
| 1216 | { |
| 1217 | char buffer[3]; |
| 1218 | char *endp; |
| 1219 | unsigned int i; |
| 1220 | |
| 1221 | buffer[2] = '\0'; |
| 1222 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1223 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | buffer[0] = *hex++; |
| 1225 | buffer[1] = *hex++; |
| 1226 | |
| 1227 | key[i] = (u8)simple_strtoul(buffer, &endp, 16); |
| 1228 | |
| 1229 | if (endp != &buffer[2]) |
| 1230 | return -EINVAL; |
| 1231 | } |
| 1232 | |
| 1233 | if (*hex != '\0') |
| 1234 | return -EINVAL; |
| 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * Encode key into its hex representation |
| 1241 | */ |
| 1242 | static void crypt_encode_key(char *hex, u8 *key, unsigned int size) |
| 1243 | { |
| 1244 | unsigned int i; |
| 1245 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1246 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | sprintf(hex, "%02x", *key); |
| 1248 | hex += 2; |
| 1249 | key++; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1253 | static void crypt_free_tfms(struct crypt_config *cc) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1254 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1255 | unsigned i; |
| 1256 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1257 | if (!cc->tfms) |
| 1258 | return; |
| 1259 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1260 | for (i = 0; i < cc->tfms_count; i++) |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1261 | if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) { |
| 1262 | crypto_free_ablkcipher(cc->tfms[i]); |
| 1263 | cc->tfms[i] = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1267 | static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1268 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1269 | unsigned i; |
| 1270 | int err; |
| 1271 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1272 | cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *), |
| 1273 | GFP_KERNEL); |
| 1274 | if (!cc->tfms) |
| 1275 | return -ENOMEM; |
| 1276 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1277 | for (i = 0; i < cc->tfms_count; i++) { |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1278 | cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0); |
| 1279 | if (IS_ERR(cc->tfms[i])) { |
| 1280 | err = PTR_ERR(cc->tfms[i]); |
| 1281 | crypt_free_tfms(cc); |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1282 | return err; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1289 | static int crypt_setkey_allcpus(struct crypt_config *cc) |
| 1290 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1291 | unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count); |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1292 | int err = 0, i, r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1293 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1294 | for (i = 0; i < cc->tfms_count; i++) { |
| 1295 | r = crypto_ablkcipher_setkey(cc->tfms[i], |
| 1296 | cc->key + (i * subkey_size), |
| 1297 | subkey_size); |
| 1298 | if (r) |
| 1299 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | return err; |
| 1303 | } |
| 1304 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1305 | static int crypt_set_key(struct crypt_config *cc, char *key) |
| 1306 | { |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1307 | int r = -EINVAL; |
| 1308 | int key_string_len = strlen(key); |
| 1309 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1310 | /* The key size may not be changed. */ |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1311 | if (cc->key_size != (key_string_len >> 1)) |
| 1312 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1313 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1314 | /* Hyphen (which gives a key_size of zero) means there is no key. */ |
| 1315 | if (!cc->key_size && strcmp(key, "-")) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1316 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1317 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1318 | if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1319 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1320 | |
| 1321 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1322 | |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1323 | r = crypt_setkey_allcpus(cc); |
| 1324 | |
| 1325 | out: |
| 1326 | /* Hex key string not needed after here, so wipe it. */ |
| 1327 | memset(key, '0', key_string_len); |
| 1328 | |
| 1329 | return r; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static int crypt_wipe_key(struct crypt_config *cc) |
| 1333 | { |
| 1334 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1335 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1336 | |
| 1337 | return crypt_setkey_allcpus(cc); |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1340 | static void crypt_dtr(struct dm_target *ti) |
| 1341 | { |
| 1342 | struct crypt_config *cc = ti->private; |
| 1343 | |
| 1344 | ti->private = NULL; |
| 1345 | |
| 1346 | if (!cc) |
| 1347 | return; |
| 1348 | |
| 1349 | if (cc->io_queue) |
| 1350 | destroy_workqueue(cc->io_queue); |
| 1351 | if (cc->crypt_queue) |
| 1352 | destroy_workqueue(cc->crypt_queue); |
| 1353 | |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1354 | crypt_free_tfms(cc); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1355 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1356 | if (cc->bs) |
| 1357 | bioset_free(cc->bs); |
| 1358 | |
| 1359 | if (cc->page_pool) |
| 1360 | mempool_destroy(cc->page_pool); |
| 1361 | if (cc->req_pool) |
| 1362 | mempool_destroy(cc->req_pool); |
| 1363 | if (cc->io_pool) |
| 1364 | mempool_destroy(cc->io_pool); |
| 1365 | |
| 1366 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 1367 | cc->iv_gen_ops->dtr(cc); |
| 1368 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1369 | if (cc->dev) |
| 1370 | dm_put_device(ti, cc->dev); |
| 1371 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1372 | kzfree(cc->cipher); |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1373 | kzfree(cc->cipher_string); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1374 | |
| 1375 | /* Must zero key material before freeing */ |
| 1376 | kzfree(cc); |
| 1377 | } |
| 1378 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1379 | static int crypt_ctr_cipher(struct dm_target *ti, |
| 1380 | char *cipher_in, char *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1382 | struct crypt_config *cc = ti->private; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1383 | char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1384 | char *cipher_api = NULL; |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1385 | int ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1386 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1388 | /* Convert to crypto api definition? */ |
| 1389 | if (strchr(cipher_in, '(')) { |
| 1390 | ti->error = "Bad cipher specification"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1394 | cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); |
| 1395 | if (!cc->cipher_string) |
| 1396 | goto bad_mem; |
| 1397 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1398 | /* |
| 1399 | * Legacy dm-crypt cipher specification |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1400 | * cipher[:keycount]-mode-iv:ivopts |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1401 | */ |
| 1402 | tmp = cipher_in; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1403 | keycount = strsep(&tmp, "-"); |
| 1404 | cipher = strsep(&keycount, ":"); |
| 1405 | |
| 1406 | if (!keycount) |
| 1407 | cc->tfms_count = 1; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1408 | else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1409 | !is_power_of_2(cc->tfms_count)) { |
| 1410 | ti->error = "Bad cipher key count specification"; |
| 1411 | return -EINVAL; |
| 1412 | } |
| 1413 | cc->key_parts = cc->tfms_count; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1414 | |
| 1415 | cc->cipher = kstrdup(cipher, GFP_KERNEL); |
| 1416 | if (!cc->cipher) |
| 1417 | goto bad_mem; |
| 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | chainmode = strsep(&tmp, "-"); |
| 1420 | ivopts = strsep(&tmp, "-"); |
| 1421 | ivmode = strsep(&ivopts, ":"); |
| 1422 | |
| 1423 | if (tmp) |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1424 | DMWARN("Ignoring unexpected additional cipher options"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1426 | /* |
| 1427 | * For compatibility with the original dm-crypt mapping format, if |
| 1428 | * only the cipher name is supplied, use cbc-plain. |
| 1429 | */ |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1430 | if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | chainmode = "cbc"; |
| 1432 | ivmode = "plain"; |
| 1433 | } |
| 1434 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1435 | if (strcmp(chainmode, "ecb") && !ivmode) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1436 | ti->error = "IV mechanism required"; |
| 1437 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1440 | cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); |
| 1441 | if (!cipher_api) |
| 1442 | goto bad_mem; |
| 1443 | |
| 1444 | ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, |
| 1445 | "%s(%s)", chainmode, cipher); |
| 1446 | if (ret < 0) { |
| 1447 | kfree(cipher_api); |
| 1448 | goto bad_mem; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1449 | } |
| 1450 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1451 | /* Allocate cipher */ |
Mikulas Patocka | fbd06f9 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 1452 | ret = crypt_alloc_tfms(cc, cipher_api); |
| 1453 | if (ret < 0) { |
| 1454 | ti->error = "Error allocating crypto tfm"; |
| 1455 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1458 | /* Initialize and set key */ |
| 1459 | ret = crypt_set_key(cc, key); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1460 | if (ret < 0) { |
Milan Broz | 0b43095 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 1461 | ti->error = "Error decoding and setting key"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1462 | goto bad; |
Milan Broz | 0b43095 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1465 | /* Initialize IV */ |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1466 | cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc)); |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1467 | if (cc->iv_size) |
| 1468 | /* at least a 64 bit sector number should fit in our buffer */ |
| 1469 | cc->iv_size = max(cc->iv_size, |
| 1470 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
| 1471 | else if (ivmode) { |
| 1472 | DMWARN("Selected cipher does not support IVs"); |
| 1473 | ivmode = NULL; |
| 1474 | } |
| 1475 | |
| 1476 | /* Choose ivmode, see comments at iv code. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (ivmode == NULL) |
| 1478 | cc->iv_gen_ops = NULL; |
| 1479 | else if (strcmp(ivmode, "plain") == 0) |
| 1480 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 1481 | else if (strcmp(ivmode, "plain64") == 0) |
| 1482 | cc->iv_gen_ops = &crypt_iv_plain64_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | else if (strcmp(ivmode, "essiv") == 0) |
| 1484 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 1485 | else if (strcmp(ivmode, "benbi") == 0) |
| 1486 | cc->iv_gen_ops = &crypt_iv_benbi_ops; |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 1487 | else if (strcmp(ivmode, "null") == 0) |
| 1488 | cc->iv_gen_ops = &crypt_iv_null_ops; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1489 | else if (strcmp(ivmode, "lmk") == 0) { |
| 1490 | cc->iv_gen_ops = &crypt_iv_lmk_ops; |
| 1491 | /* Version 2 and 3 is recognised according |
| 1492 | * to length of provided multi-key string. |
| 1493 | * If present (version 3), last key is used as IV seed. |
| 1494 | */ |
| 1495 | if (cc->key_size % cc->key_parts) |
| 1496 | cc->key_parts++; |
| 1497 | } else { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1498 | ret = -EINVAL; |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1499 | ti->error = "Invalid IV mode"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1500 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1503 | /* Allocate IV */ |
| 1504 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { |
| 1505 | ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); |
| 1506 | if (ret < 0) { |
| 1507 | ti->error = "Error creating IV"; |
| 1508 | goto bad; |
| 1509 | } |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1512 | /* Initialize IV (set keys for ESSIV etc) */ |
| 1513 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) { |
| 1514 | ret = cc->iv_gen_ops->init(cc); |
| 1515 | if (ret < 0) { |
| 1516 | ti->error = "Error initialising IV"; |
| 1517 | goto bad; |
| 1518 | } |
| 1519 | } |
| 1520 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1521 | ret = 0; |
| 1522 | bad: |
| 1523 | kfree(cipher_api); |
| 1524 | return ret; |
| 1525 | |
| 1526 | bad_mem: |
| 1527 | ti->error = "Cannot allocate cipher strings"; |
| 1528 | return -ENOMEM; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Construct an encryption mapping: |
| 1533 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 1534 | */ |
| 1535 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1536 | { |
| 1537 | struct crypt_config *cc; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1538 | unsigned int key_size, opt_params; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1539 | unsigned long long tmpll; |
| 1540 | int ret; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1541 | struct dm_arg_set as; |
| 1542 | const char *opt_string; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1543 | char dummy; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1544 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1545 | static struct dm_arg _args[] = { |
| 1546 | {0, 1, "Invalid number of feature args"}, |
| 1547 | }; |
| 1548 | |
| 1549 | if (argc < 5) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1550 | ti->error = "Not enough arguments"; |
| 1551 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1554 | key_size = strlen(argv[1]) >> 1; |
| 1555 | |
| 1556 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
| 1557 | if (!cc) { |
| 1558 | ti->error = "Cannot allocate encryption context"; |
| 1559 | return -ENOMEM; |
| 1560 | } |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1561 | cc->key_size = key_size; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1562 | |
| 1563 | ti->private = cc; |
| 1564 | ret = crypt_ctr_cipher(ti, argv[0], argv[1]); |
| 1565 | if (ret < 0) |
| 1566 | goto bad; |
| 1567 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1568 | ret = -ENOMEM; |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1569 | cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | if (!cc->io_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1571 | ti->error = "Cannot allocate crypt io mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1572 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1575 | cc->dmreq_start = sizeof(struct ablkcipher_request); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1576 | cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1577 | cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment()); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1578 | cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) & |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1579 | ~(crypto_tfm_ctx_alignment() - 1); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1580 | |
| 1581 | cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + |
| 1582 | sizeof(struct dm_crypt_request) + cc->iv_size); |
| 1583 | if (!cc->req_pool) { |
| 1584 | ti->error = "Cannot allocate crypt request mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1585 | goto bad; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1586 | } |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1587 | |
Matthew Dobson | a19b27c | 2006-03-26 01:37:45 -0800 | [diff] [blame] | 1588 | cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | if (!cc->page_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1590 | ti->error = "Cannot allocate page mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1591 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Jens Axboe | bb799ca | 2008-12-10 15:35:05 +0100 | [diff] [blame] | 1594 | cc->bs = bioset_create(MIN_IOS, 0); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1595 | if (!cc->bs) { |
| 1596 | ti->error = "Cannot allocate crypt bioset"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1597 | goto bad; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1600 | ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1601 | if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1602 | ti->error = "Invalid iv_offset sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1603 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1605 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1607 | if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) { |
| 1608 | ti->error = "Device lookup failed"; |
| 1609 | goto bad; |
| 1610 | } |
| 1611 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1612 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1613 | ti->error = "Invalid device sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1614 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1616 | cc->start = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1618 | argv += 5; |
| 1619 | argc -= 5; |
| 1620 | |
| 1621 | /* Optional parameters */ |
| 1622 | if (argc) { |
| 1623 | as.argc = argc; |
| 1624 | as.argv = argv; |
| 1625 | |
| 1626 | ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); |
| 1627 | if (ret) |
| 1628 | goto bad; |
| 1629 | |
| 1630 | opt_string = dm_shift_arg(&as); |
| 1631 | |
| 1632 | if (opt_params == 1 && opt_string && |
| 1633 | !strcasecmp(opt_string, "allow_discards")) |
| 1634 | ti->num_discard_requests = 1; |
| 1635 | else if (opt_params) { |
| 1636 | ret = -EINVAL; |
| 1637 | ti->error = "Invalid feature arguments"; |
| 1638 | goto bad; |
| 1639 | } |
| 1640 | } |
| 1641 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1642 | ret = -ENOMEM; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1643 | cc->io_queue = alloc_workqueue("kcryptd_io", |
| 1644 | WQ_NON_REENTRANT| |
| 1645 | WQ_MEM_RECLAIM, |
| 1646 | 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1647 | if (!cc->io_queue) { |
| 1648 | ti->error = "Couldn't create kcryptd io queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1649 | goto bad; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1652 | cc->crypt_queue = alloc_workqueue("kcryptd", |
| 1653 | WQ_NON_REENTRANT| |
| 1654 | WQ_CPU_INTENSIVE| |
| 1655 | WQ_MEM_RECLAIM, |
| 1656 | 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1657 | if (!cc->crypt_queue) { |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1658 | ti->error = "Couldn't create kcryptd queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1659 | goto bad; |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1662 | ti->num_flush_requests = 1; |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 1663 | ti->discard_zeroes_data_unsupported = 1; |
| 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | return 0; |
| 1666 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1667 | bad: |
| 1668 | crypt_dtr(ti); |
| 1669 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | static int crypt_map(struct dm_target *ti, struct bio *bio, |
| 1673 | union map_info *map_context) |
| 1674 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1675 | struct dm_crypt_io *io; |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1676 | struct crypt_config *cc; |
| 1677 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1678 | /* |
| 1679 | * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues. |
| 1680 | * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight |
| 1681 | * - for REQ_DISCARD caller must use flush if IO ordering matters |
| 1682 | */ |
| 1683 | if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) { |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1684 | cc = ti->private; |
| 1685 | bio->bi_bdev = cc->dev->bdev; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1686 | if (bio_sectors(bio)) |
| 1687 | bio->bi_sector = cc->start + dm_target_offset(ti, bio->bi_sector); |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1688 | return DM_MAPIO_REMAPPED; |
| 1689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Alasdair G Kergon | b441a262 | 2010-08-12 04:14:11 +0100 | [diff] [blame] | 1691 | io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector)); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1692 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1693 | if (bio_data_dir(io->base_bio) == READ) { |
| 1694 | if (kcryptd_io_read(io, GFP_NOWAIT)) |
| 1695 | kcryptd_queue_io(io); |
| 1696 | } else |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1697 | kcryptd_queue_crypt(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1699 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static int crypt_status(struct dm_target *ti, status_type_t type, |
| 1703 | char *result, unsigned int maxlen) |
| 1704 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1705 | struct crypt_config *cc = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | unsigned int sz = 0; |
| 1707 | |
| 1708 | switch (type) { |
| 1709 | case STATUSTYPE_INFO: |
| 1710 | result[0] = '\0'; |
| 1711 | break; |
| 1712 | |
| 1713 | case STATUSTYPE_TABLE: |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1714 | DMEMIT("%s ", cc->cipher_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
| 1716 | if (cc->key_size > 0) { |
| 1717 | if ((maxlen - sz) < ((cc->key_size << 1) + 1)) |
| 1718 | return -ENOMEM; |
| 1719 | |
| 1720 | crypt_encode_key(result + sz, cc->key, cc->key_size); |
| 1721 | sz += cc->key_size << 1; |
| 1722 | } else { |
| 1723 | if (sz >= maxlen) |
| 1724 | return -ENOMEM; |
| 1725 | result[sz++] = '-'; |
| 1726 | } |
| 1727 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1728 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 1729 | cc->dev->name, (unsigned long long)cc->start); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1730 | |
| 1731 | if (ti->num_discard_requests) |
| 1732 | DMEMIT(" 1 allow_discards"); |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | break; |
| 1735 | } |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1739 | static void crypt_postsuspend(struct dm_target *ti) |
| 1740 | { |
| 1741 | struct crypt_config *cc = ti->private; |
| 1742 | |
| 1743 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1744 | } |
| 1745 | |
| 1746 | static int crypt_preresume(struct dm_target *ti) |
| 1747 | { |
| 1748 | struct crypt_config *cc = ti->private; |
| 1749 | |
| 1750 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { |
| 1751 | DMERR("aborting resume - crypt key is not set."); |
| 1752 | return -EAGAIN; |
| 1753 | } |
| 1754 | |
| 1755 | return 0; |
| 1756 | } |
| 1757 | |
| 1758 | static void crypt_resume(struct dm_target *ti) |
| 1759 | { |
| 1760 | struct crypt_config *cc = ti->private; |
| 1761 | |
| 1762 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1763 | } |
| 1764 | |
| 1765 | /* Message interface |
| 1766 | * key set <key> |
| 1767 | * key wipe |
| 1768 | */ |
| 1769 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv) |
| 1770 | { |
| 1771 | struct crypt_config *cc = ti->private; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1772 | int ret = -EINVAL; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1773 | |
| 1774 | if (argc < 2) |
| 1775 | goto error; |
| 1776 | |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1777 | if (!strcasecmp(argv[0], "key")) { |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1778 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
| 1779 | DMWARN("not suspended during key manipulation."); |
| 1780 | return -EINVAL; |
| 1781 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1782 | if (argc == 3 && !strcasecmp(argv[1], "set")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1783 | ret = crypt_set_key(cc, argv[2]); |
| 1784 | if (ret) |
| 1785 | return ret; |
| 1786 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) |
| 1787 | ret = cc->iv_gen_ops->init(cc); |
| 1788 | return ret; |
| 1789 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1790 | if (argc == 2 && !strcasecmp(argv[1], "wipe")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1791 | if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { |
| 1792 | ret = cc->iv_gen_ops->wipe(cc); |
| 1793 | if (ret) |
| 1794 | return ret; |
| 1795 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1796 | return crypt_wipe_key(cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1797 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | error: |
| 1801 | DMWARN("unrecognised message received."); |
| 1802 | return -EINVAL; |
| 1803 | } |
| 1804 | |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 1805 | static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
| 1806 | struct bio_vec *biovec, int max_size) |
| 1807 | { |
| 1808 | struct crypt_config *cc = ti->private; |
| 1809 | struct request_queue *q = bdev_get_queue(cc->dev->bdev); |
| 1810 | |
| 1811 | if (!q->merge_bvec_fn) |
| 1812 | return max_size; |
| 1813 | |
| 1814 | bvm->bi_bdev = cc->dev->bdev; |
Alasdair G Kergon | b441a262 | 2010-08-12 04:14:11 +0100 | [diff] [blame] | 1815 | bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector); |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 1816 | |
| 1817 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 1818 | } |
| 1819 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1820 | static int crypt_iterate_devices(struct dm_target *ti, |
| 1821 | iterate_devices_callout_fn fn, void *data) |
| 1822 | { |
| 1823 | struct crypt_config *cc = ti->private; |
| 1824 | |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 1825 | return fn(ti, cc->dev, cc->start, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1826 | } |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | static struct target_type crypt_target = { |
| 1829 | .name = "crypt", |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1830 | .version = {1, 11, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | .module = THIS_MODULE, |
| 1832 | .ctr = crypt_ctr, |
| 1833 | .dtr = crypt_dtr, |
| 1834 | .map = crypt_map, |
| 1835 | .status = crypt_status, |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1836 | .postsuspend = crypt_postsuspend, |
| 1837 | .preresume = crypt_preresume, |
| 1838 | .resume = crypt_resume, |
| 1839 | .message = crypt_message, |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 1840 | .merge = crypt_merge, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1841 | .iterate_devices = crypt_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | }; |
| 1843 | |
| 1844 | static int __init dm_crypt_init(void) |
| 1845 | { |
| 1846 | int r; |
| 1847 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1848 | _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | if (!_crypt_io_pool) |
| 1850 | return -ENOMEM; |
| 1851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | r = dm_register_target(&crypt_target); |
| 1853 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1854 | DMERR("register failed %d", r); |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1855 | kmem_cache_destroy(_crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | return r; |
| 1859 | } |
| 1860 | |
| 1861 | static void __exit dm_crypt_exit(void) |
| 1862 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 1863 | dm_unregister_target(&crypt_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | kmem_cache_destroy(_crypt_io_pool); |
| 1865 | } |
| 1866 | |
| 1867 | module_init(dm_crypt_init); |
| 1868 | module_exit(dm_crypt_exit); |
| 1869 | |
| 1870 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); |
| 1871 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 1872 | MODULE_LICENSE("GPL"); |