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 | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 4 | * Copyright (C) 2006 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 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 9 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/bio.h> |
| 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/mempool.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/workqueue.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 19 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/page.h> |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 23 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include "dm.h" |
| 26 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 27 | #define DM_MSG_PREFIX "crypt" |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 28 | #define MESG_STR(x) x, sizeof(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * per bio private data |
| 32 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 33 | struct dm_crypt_io { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct dm_target *target; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 35 | struct bio *base_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct work_struct work; |
| 37 | atomic_t pending; |
| 38 | int error; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * context holding the current state of a multi-part conversion |
| 43 | */ |
| 44 | struct convert_context { |
| 45 | struct bio *bio_in; |
| 46 | struct bio *bio_out; |
| 47 | unsigned int offset_in; |
| 48 | unsigned int offset_out; |
| 49 | unsigned int idx_in; |
| 50 | unsigned int idx_out; |
| 51 | sector_t sector; |
| 52 | int write; |
| 53 | }; |
| 54 | |
| 55 | struct crypt_config; |
| 56 | |
| 57 | struct crypt_iv_operations { |
| 58 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 59 | const char *opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | void (*dtr)(struct crypt_config *cc); |
| 61 | const char *(*status)(struct crypt_config *cc); |
| 62 | int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector); |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * Crypt: maps a linear range of a block device |
| 67 | * and encrypts / decrypts at the same time. |
| 68 | */ |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 69 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct crypt_config { |
| 71 | struct dm_dev *dev; |
| 72 | sector_t start; |
| 73 | |
| 74 | /* |
| 75 | * pool for per bio private data and |
| 76 | * for encryption buffer pages |
| 77 | */ |
| 78 | mempool_t *io_pool; |
| 79 | mempool_t *page_pool; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 80 | struct bio_set *bs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 82 | struct workqueue_struct *io_queue; |
| 83 | struct workqueue_struct *crypt_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* |
| 85 | * crypto related data |
| 86 | */ |
| 87 | struct crypt_iv_operations *iv_gen_ops; |
| 88 | char *iv_mode; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 89 | union { |
| 90 | struct crypto_cipher *essiv_tfm; |
| 91 | int benbi_shift; |
| 92 | } iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | sector_t iv_offset; |
| 94 | unsigned int iv_size; |
| 95 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 96 | char cipher[CRYPTO_MAX_ALG_NAME]; |
| 97 | char chainmode[CRYPTO_MAX_ALG_NAME]; |
| 98 | struct crypto_blkcipher *tfm; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 99 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | unsigned int key_size; |
| 101 | u8 key[0]; |
| 102 | }; |
| 103 | |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 104 | #define MIN_IOS 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #define MIN_POOL_PAGES 32 |
| 106 | #define MIN_BIO_PAGES 8 |
| 107 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 108 | static struct kmem_cache *_crypt_io_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 110 | static void clone_init(struct dm_crypt_io *, struct bio *); |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * Different IV generation algorithms: |
| 114 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 115 | * 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] | 116 | * number, padded with zeros if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 118 | * essiv: "encrypted sector|salt initial vector", the sector number is |
| 119 | * encrypted with the bulk cipher using a salt as key. The salt |
| 120 | * should be derived from the bulk cipher's key via hashing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | * |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 122 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
| 123 | * (needed for LRW-32-AES and possible other narrow block modes) |
| 124 | * |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 125 | * null: the initial vector is always zero. Provides compatibility with |
| 126 | * obsolete loop_fish2 devices. Do not use for new devices. |
| 127 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * plumb: unimplemented, see: |
| 129 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 130 | */ |
| 131 | |
| 132 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 133 | { |
| 134 | memset(iv, 0, cc->iv_size); |
| 135 | *(u32 *)iv = cpu_to_le32(sector & 0xffffffff); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | 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] | 141 | const char *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 143 | struct crypto_cipher *essiv_tfm; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 144 | struct crypto_hash *hash_tfm; |
| 145 | struct hash_desc desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | struct scatterlist sg; |
| 147 | unsigned int saltsize; |
| 148 | u8 *salt; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 149 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | if (opts == NULL) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 152 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | /* Hash the cipher key with the given hash algorithm */ |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 157 | hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC); |
| 158 | if (IS_ERR(hash_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 159 | ti->error = "Error initializing ESSIV hash"; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 160 | return PTR_ERR(hash_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 163 | saltsize = crypto_hash_digestsize(hash_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | salt = kmalloc(saltsize, GFP_KERNEL); |
| 165 | if (salt == NULL) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 166 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 167 | crypto_free_hash(hash_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return -ENOMEM; |
| 169 | } |
| 170 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 171 | sg_init_one(&sg, cc->key, cc->key_size); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 172 | desc.tfm = hash_tfm; |
| 173 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 174 | err = crypto_hash_digest(&desc, &sg, cc->key_size, salt); |
| 175 | crypto_free_hash(hash_tfm); |
| 176 | |
| 177 | if (err) { |
| 178 | ti->error = "Error calculating hash in ESSIV"; |
Dmitry Monakhov | 815f9e3 | 2007-10-19 22:38:38 +0100 | [diff] [blame] | 179 | kfree(salt); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 180 | return err; |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | /* Setup the essiv_tfm with the given salt */ |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 184 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 185 | if (IS_ERR(essiv_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 186 | ti->error = "Error allocating crypto tfm for ESSIV"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | kfree(salt); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 188 | return PTR_ERR(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 190 | if (crypto_cipher_blocksize(essiv_tfm) != |
| 191 | crypto_blkcipher_ivsize(cc->tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 192 | ti->error = "Block size of ESSIV cipher does " |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 193 | "not match IV size of block cipher"; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 194 | crypto_free_cipher(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | kfree(salt); |
| 196 | return -EINVAL; |
| 197 | } |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 198 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 199 | if (err) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 200 | ti->error = "Failed to set key for ESSIV cipher"; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 201 | crypto_free_cipher(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | kfree(salt); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 203 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | kfree(salt); |
| 206 | |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 207 | cc->iv_gen_private.essiv_tfm = essiv_tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 212 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 213 | crypto_free_cipher(cc->iv_gen_private.essiv_tfm); |
| 214 | cc->iv_gen_private.essiv_tfm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 218 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | memset(iv, 0, cc->iv_size); |
| 220 | *(u64 *)iv = cpu_to_le64(sector); |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 221 | crypto_cipher_encrypt_one(cc->iv_gen_private.essiv_tfm, iv, iv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 225 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 226 | const char *opts) |
| 227 | { |
| 228 | unsigned int bs = crypto_blkcipher_blocksize(cc->tfm); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 229 | int log = ilog2(bs); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 230 | |
| 231 | /* we need to calculate how far we must shift the sector count |
| 232 | * to get the cipher block count, we use this shift in _gen */ |
| 233 | |
| 234 | if (1 << log != bs) { |
| 235 | ti->error = "cypher blocksize is not a power of 2"; |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | if (log > 9) { |
| 240 | ti->error = "cypher blocksize is > 512"; |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 244 | cc->iv_gen_private.benbi_shift = 9 - log; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) |
| 250 | { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 254 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 255 | __be64 val; |
| 256 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 257 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 258 | |
| 259 | val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi_shift) + 1); |
| 260 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 265 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 266 | { |
| 267 | memset(iv, 0, cc->iv_size); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
| 273 | .generator = crypt_iv_plain_gen |
| 274 | }; |
| 275 | |
| 276 | static struct crypt_iv_operations crypt_iv_essiv_ops = { |
| 277 | .ctr = crypt_iv_essiv_ctr, |
| 278 | .dtr = crypt_iv_essiv_dtr, |
| 279 | .generator = crypt_iv_essiv_gen |
| 280 | }; |
| 281 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 282 | static struct crypt_iv_operations crypt_iv_benbi_ops = { |
| 283 | .ctr = crypt_iv_benbi_ctr, |
| 284 | .dtr = crypt_iv_benbi_dtr, |
| 285 | .generator = crypt_iv_benbi_gen |
| 286 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 288 | static struct crypt_iv_operations crypt_iv_null_ops = { |
| 289 | .generator = crypt_iv_null_gen |
| 290 | }; |
| 291 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 292 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out, |
| 294 | struct scatterlist *in, unsigned int length, |
| 295 | int write, sector_t sector) |
| 296 | { |
Herbert Xu | 4578932 | 2006-09-03 08:58:41 +1000 | [diff] [blame] | 297 | u8 iv[cc->iv_size] __attribute__ ((aligned(__alignof__(u64)))); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 298 | struct blkcipher_desc desc = { |
| 299 | .tfm = cc->tfm, |
| 300 | .info = iv, |
| 301 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP, |
| 302 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | int r; |
| 304 | |
| 305 | if (cc->iv_gen_ops) { |
| 306 | r = cc->iv_gen_ops->generator(cc, iv, sector); |
| 307 | if (r < 0) |
| 308 | return r; |
| 309 | |
| 310 | if (write) |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 311 | r = crypto_blkcipher_encrypt_iv(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | else |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 313 | r = crypto_blkcipher_decrypt_iv(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } else { |
| 315 | if (write) |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 316 | r = crypto_blkcipher_encrypt(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | else |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 318 | r = crypto_blkcipher_decrypt(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return r; |
| 322 | } |
| 323 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 324 | static void crypt_convert_init(struct crypt_config *cc, |
| 325 | struct convert_context *ctx, |
| 326 | struct bio *bio_out, struct bio *bio_in, |
| 327 | sector_t sector, int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | ctx->bio_in = bio_in; |
| 330 | ctx->bio_out = bio_out; |
| 331 | ctx->offset_in = 0; |
| 332 | ctx->offset_out = 0; |
| 333 | ctx->idx_in = bio_in ? bio_in->bi_idx : 0; |
| 334 | ctx->idx_out = bio_out ? bio_out->bi_idx : 0; |
| 335 | ctx->sector = sector + cc->iv_offset; |
| 336 | ctx->write = write; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 341 | */ |
| 342 | static int crypt_convert(struct crypt_config *cc, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 343 | struct convert_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | int r = 0; |
| 346 | |
| 347 | while(ctx->idx_in < ctx->bio_in->bi_vcnt && |
| 348 | ctx->idx_out < ctx->bio_out->bi_vcnt) { |
| 349 | struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in); |
| 350 | struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 351 | struct scatterlist sg_in, sg_out; |
| 352 | |
| 353 | sg_init_table(&sg_in, 1); |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 354 | sg_set_page(&sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT, bv_in->bv_offset + ctx->offset_in); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 355 | |
| 356 | sg_init_table(&sg_out, 1); |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 357 | sg_set_page(&sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT, bv_out->bv_offset + ctx->offset_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | ctx->offset_in += sg_in.length; |
| 360 | if (ctx->offset_in >= bv_in->bv_len) { |
| 361 | ctx->offset_in = 0; |
| 362 | ctx->idx_in++; |
| 363 | } |
| 364 | |
| 365 | ctx->offset_out += sg_out.length; |
| 366 | if (ctx->offset_out >= bv_out->bv_len) { |
| 367 | ctx->offset_out = 0; |
| 368 | ctx->idx_out++; |
| 369 | } |
| 370 | |
| 371 | r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 372 | ctx->write, ctx->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (r < 0) |
| 374 | break; |
| 375 | |
| 376 | ctx->sector++; |
| 377 | } |
| 378 | |
| 379 | return r; |
| 380 | } |
| 381 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 382 | static void dm_crypt_bio_destructor(struct bio *bio) |
| 383 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 384 | struct dm_crypt_io *io = bio->bi_private; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 385 | struct crypt_config *cc = io->target->private; |
| 386 | |
| 387 | bio_free(bio, cc->bs); |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 388 | } |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* |
| 391 | * Generate a new unfragmented bio with the given size |
| 392 | * This should never violate the device limitations |
| 393 | * May return a smaller bio when running out of pages |
| 394 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 395 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 397 | struct crypt_config *cc = io->target->private; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 398 | struct bio *clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 400 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 401 | unsigned i, len; |
| 402 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Olaf Kirch | 2f9941b | 2007-05-09 02:32:53 -0700 | [diff] [blame] | 404 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 405 | if (!clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 408 | clone_init(io, clone); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 409 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 410 | for (i = 0; i < nr_iovecs; i++) { |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 411 | page = mempool_alloc(cc->page_pool, gfp_mask); |
| 412 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | break; |
| 414 | |
| 415 | /* |
| 416 | * if additional pages cannot be allocated without waiting, |
| 417 | * return a partially allocated bio, the caller will then try |
| 418 | * to allocate additional bios while submitting this partial bio |
| 419 | */ |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 420 | if (i == (MIN_BIO_PAGES - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT; |
| 422 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 423 | len = (size > PAGE_SIZE) ? PAGE_SIZE : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 425 | if (!bio_add_page(clone, page, len, 0)) { |
| 426 | mempool_free(page, cc->page_pool); |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 433 | if (!clone->bi_size) { |
| 434 | bio_put(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return NULL; |
| 436 | } |
| 437 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 438 | return clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 441 | 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] | 442 | { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 443 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | struct bio_vec *bv; |
| 445 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 446 | for (i = 0; i < clone->bi_vcnt; i++) { |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 447 | bv = bio_iovec_idx(clone, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | BUG_ON(!bv->bv_page); |
| 449 | mempool_free(bv->bv_page, cc->page_pool); |
| 450 | bv->bv_page = NULL; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * One of the bios was finished. Check for completion of |
| 456 | * the whole request and correctly clean up the buffer. |
| 457 | */ |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 458 | static void crypt_dec_pending(struct dm_crypt_io *io, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | struct crypt_config *cc = (struct crypt_config *) io->target->private; |
| 461 | |
| 462 | if (error < 0) |
| 463 | io->error = error; |
| 464 | |
| 465 | if (!atomic_dec_and_test(&io->pending)) |
| 466 | return; |
| 467 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 468 | bio_endio(io->base_bio, io->error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | mempool_free(io, cc->io_pool); |
| 471 | } |
| 472 | |
| 473 | /* |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 474 | * kcryptd/kcryptd_io: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | * |
| 476 | * Needed because it would be very unwise to do decryption in an |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 477 | * interrupt context. |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 478 | * |
| 479 | * kcryptd performs the actual encryption or decryption. |
| 480 | * |
| 481 | * kcryptd_io performs the IO submission. |
| 482 | * |
| 483 | * They must be separated as otherwise the final stages could be |
| 484 | * starved by new requests which can block in the first stages due |
| 485 | * to memory allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 487 | static void kcryptd_do_work(struct work_struct *work); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 488 | static void kcryptd_do_crypt(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 490 | static void kcryptd_queue_io(struct dm_crypt_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 492 | struct crypt_config *cc = io->target->private; |
| 493 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 494 | INIT_WORK(&io->work, kcryptd_do_work); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 495 | queue_work(cc->io_queue, &io->work); |
| 496 | } |
| 497 | |
| 498 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
| 499 | { |
| 500 | struct crypt_config *cc = io->target->private; |
| 501 | |
| 502 | INIT_WORK(&io->work, kcryptd_do_crypt); |
| 503 | queue_work(cc->crypt_queue, &io->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 506 | static void crypt_endio(struct bio *clone, int error) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 507 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 508 | struct dm_crypt_io *io = clone->bi_private; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 509 | struct crypt_config *cc = io->target->private; |
| 510 | unsigned read_io = bio_data_dir(clone) == READ; |
| 511 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 512 | if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error)) |
| 513 | error = -EIO; |
| 514 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 515 | /* |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 516 | * free the processed pages |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 517 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 518 | if (!read_io) { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 519 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 520 | goto out; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 521 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 522 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 523 | if (unlikely(error)) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 524 | goto out; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 525 | |
| 526 | bio_put(clone); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 527 | kcryptd_queue_crypt(io); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 528 | return; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 529 | |
| 530 | out: |
| 531 | bio_put(clone); |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 532 | crypt_dec_pending(io, error); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 535 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 536 | { |
| 537 | struct crypt_config *cc = io->target->private; |
| 538 | |
| 539 | clone->bi_private = io; |
| 540 | clone->bi_end_io = crypt_endio; |
| 541 | clone->bi_bdev = cc->dev->bdev; |
| 542 | clone->bi_rw = io->base_bio->bi_rw; |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 543 | clone->bi_destructor = dm_crypt_bio_destructor; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 546 | static void process_read(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 547 | { |
| 548 | struct crypt_config *cc = io->target->private; |
| 549 | struct bio *base_bio = io->base_bio; |
| 550 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 551 | sector_t sector = base_bio->bi_sector - io->target->begin; |
| 552 | |
| 553 | atomic_inc(&io->pending); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * The block layer might modify the bvec array, so always |
| 557 | * copy the required bvecs because we need the original |
| 558 | * one in order to decrypt the whole bio data *afterwards*. |
| 559 | */ |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 560 | clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs); |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 561 | if (unlikely(!clone)) { |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 562 | crypt_dec_pending(io, -ENOMEM); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 563 | return; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 564 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 565 | |
| 566 | clone_init(io, clone); |
| 567 | clone->bi_idx = 0; |
| 568 | clone->bi_vcnt = bio_segments(base_bio); |
| 569 | clone->bi_size = base_bio->bi_size; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 570 | clone->bi_sector = cc->start + sector; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 571 | memcpy(clone->bi_io_vec, bio_iovec(base_bio), |
| 572 | sizeof(struct bio_vec) * clone->bi_vcnt); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 573 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 574 | generic_make_request(clone); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 577 | static void process_write(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 578 | { |
| 579 | struct crypt_config *cc = io->target->private; |
| 580 | struct bio *base_bio = io->base_bio; |
| 581 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 582 | struct convert_context ctx; |
| 583 | unsigned remaining = base_bio->bi_size; |
| 584 | sector_t sector = base_bio->bi_sector - io->target->begin; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 585 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 586 | atomic_inc(&io->pending); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 587 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 588 | crypt_convert_init(cc, &ctx, NULL, base_bio, sector, 1); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 589 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 590 | /* |
| 591 | * The allocated buffers can be smaller than the whole bio, |
| 592 | * so repeat the whole process until all the data can be handled. |
| 593 | */ |
| 594 | while (remaining) { |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 595 | clone = crypt_alloc_buffer(io, remaining); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 596 | if (unlikely(!clone)) { |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 597 | crypt_dec_pending(io, -ENOMEM); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 598 | return; |
| 599 | } |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 600 | |
| 601 | ctx.bio_out = clone; |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 602 | ctx.idx_out = 0; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 603 | |
| 604 | if (unlikely(crypt_convert(cc, &ctx) < 0)) { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 605 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 606 | bio_put(clone); |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 607 | crypt_dec_pending(io, -EIO); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 608 | return; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 611 | /* crypt_convert should have filled the clone bio */ |
| 612 | BUG_ON(ctx.idx_out < clone->bi_vcnt); |
| 613 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 614 | clone->bi_sector = cc->start + sector; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 615 | remaining -= clone->bi_size; |
| 616 | sector += bio_sectors(clone); |
| 617 | |
Olaf Kirch | 2f9941b | 2007-05-09 02:32:53 -0700 | [diff] [blame] | 618 | /* Grab another reference to the io struct |
| 619 | * before we kick off the request */ |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 620 | if (remaining) |
| 621 | atomic_inc(&io->pending); |
| 622 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 623 | generic_make_request(clone); |
| 624 | |
Olaf Kirch | 98221eb | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 625 | /* Do not reference clone after this - it |
| 626 | * may be gone already. */ |
| 627 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 628 | /* out of memory -> run queues */ |
| 629 | if (remaining) |
Olaf Kirch | 98221eb | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 630 | congestion_wait(WRITE, HZ/100); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 631 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 634 | static void process_read_endio(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 635 | { |
| 636 | struct crypt_config *cc = io->target->private; |
| 637 | struct convert_context ctx; |
| 638 | |
| 639 | crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio, |
| 640 | io->base_bio->bi_sector - io->target->begin, 0); |
| 641 | |
Milan Broz | 80fd662 | 2007-10-20 01:14:21 +0100 | [diff] [blame] | 642 | crypt_dec_pending(io, crypt_convert(cc, &ctx)); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 643 | } |
| 644 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 645 | static void kcryptd_do_work(struct work_struct *work) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 646 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 647 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 648 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 649 | if (bio_data_dir(io->base_bio) == READ) |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 650 | process_read(io); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void kcryptd_do_crypt(struct work_struct *work) |
| 654 | { |
| 655 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 656 | |
| 657 | if (bio_data_dir(io->base_bio) == READ) |
| 658 | process_read_endio(io); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 659 | else |
| 660 | process_write(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | /* |
| 664 | * Decode key from its hex representation |
| 665 | */ |
| 666 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) |
| 667 | { |
| 668 | char buffer[3]; |
| 669 | char *endp; |
| 670 | unsigned int i; |
| 671 | |
| 672 | buffer[2] = '\0'; |
| 673 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 674 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | buffer[0] = *hex++; |
| 676 | buffer[1] = *hex++; |
| 677 | |
| 678 | key[i] = (u8)simple_strtoul(buffer, &endp, 16); |
| 679 | |
| 680 | if (endp != &buffer[2]) |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | |
| 684 | if (*hex != '\0') |
| 685 | return -EINVAL; |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Encode key into its hex representation |
| 692 | */ |
| 693 | static void crypt_encode_key(char *hex, u8 *key, unsigned int size) |
| 694 | { |
| 695 | unsigned int i; |
| 696 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 697 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | sprintf(hex, "%02x", *key); |
| 699 | hex += 2; |
| 700 | key++; |
| 701 | } |
| 702 | } |
| 703 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 704 | static int crypt_set_key(struct crypt_config *cc, char *key) |
| 705 | { |
| 706 | unsigned key_size = strlen(key) >> 1; |
| 707 | |
| 708 | if (cc->key_size && cc->key_size != key_size) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | cc->key_size = key_size; /* initial settings */ |
| 712 | |
| 713 | if ((!key_size && strcmp(key, "-")) || |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 714 | (key_size && crypt_decode_key(cc->key, key, key_size) < 0)) |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 715 | return -EINVAL; |
| 716 | |
| 717 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int crypt_wipe_key(struct crypt_config *cc) |
| 723 | { |
| 724 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 725 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); |
| 726 | return 0; |
| 727 | } |
| 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
| 730 | * Construct an encryption mapping: |
| 731 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 732 | */ |
| 733 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 734 | { |
| 735 | struct crypt_config *cc; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 736 | struct crypto_blkcipher *tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | char *tmp; |
| 738 | char *cipher; |
| 739 | char *chainmode; |
| 740 | char *ivmode; |
| 741 | char *ivopts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | unsigned int key_size; |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 743 | unsigned long long tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | if (argc != 5) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 746 | ti->error = "Not enough arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | } |
| 749 | |
| 750 | tmp = argv[0]; |
| 751 | cipher = strsep(&tmp, "-"); |
| 752 | chainmode = strsep(&tmp, "-"); |
| 753 | ivopts = strsep(&tmp, "-"); |
| 754 | ivmode = strsep(&ivopts, ":"); |
| 755 | |
| 756 | if (tmp) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 757 | DMWARN("Unexpected additional cipher options"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | key_size = strlen(argv[1]) >> 1; |
| 760 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 761 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if (cc == NULL) { |
| 763 | ti->error = |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 764 | "Cannot allocate transparent encryption context"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return -ENOMEM; |
| 766 | } |
| 767 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 768 | if (crypt_set_key(cc, argv[1])) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 769 | ti->error = "Error decoding key"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 770 | goto bad_cipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* Compatiblity mode for old dm-crypt cipher strings */ |
| 774 | if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) { |
| 775 | chainmode = "cbc"; |
| 776 | ivmode = "plain"; |
| 777 | } |
| 778 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 779 | if (strcmp(chainmode, "ecb") && !ivmode) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 780 | ti->error = "This chaining mode requires an IV mechanism"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 781 | goto bad_cipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 784 | if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 785 | chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) { |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 786 | ti->error = "Chain mode + cipher name is too long"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 787 | goto bad_cipher; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 791 | if (IS_ERR(tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 792 | ti->error = "Error allocating crypto tfm"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 793 | goto bad_cipher; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 796 | strcpy(cc->cipher, cipher); |
| 797 | strcpy(cc->chainmode, chainmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | cc->tfm = tfm; |
| 799 | |
| 800 | /* |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 801 | * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | * See comments at iv code |
| 803 | */ |
| 804 | |
| 805 | if (ivmode == NULL) |
| 806 | cc->iv_gen_ops = NULL; |
| 807 | else if (strcmp(ivmode, "plain") == 0) |
| 808 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
| 809 | else if (strcmp(ivmode, "essiv") == 0) |
| 810 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 811 | else if (strcmp(ivmode, "benbi") == 0) |
| 812 | cc->iv_gen_ops = &crypt_iv_benbi_ops; |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 813 | else if (strcmp(ivmode, "null") == 0) |
| 814 | cc->iv_gen_ops = &crypt_iv_null_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | else { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 816 | ti->error = "Invalid IV mode"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 817 | goto bad_ivmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr && |
| 821 | cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0) |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 822 | goto bad_ivmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 824 | cc->iv_size = crypto_blkcipher_ivsize(tfm); |
| 825 | if (cc->iv_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | /* at least a 64 bit sector number should fit in our buffer */ |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 827 | cc->iv_size = max(cc->iv_size, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 828 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | if (cc->iv_gen_ops) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 831 | DMWARN("Selected cipher does not support IVs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (cc->iv_gen_ops->dtr) |
| 833 | cc->iv_gen_ops->dtr(cc); |
| 834 | cc->iv_gen_ops = NULL; |
| 835 | } |
| 836 | } |
| 837 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 838 | cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | if (!cc->io_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 840 | ti->error = "Cannot allocate crypt io mempool"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 841 | goto bad_slab_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Matthew Dobson | a19b27c | 2006-03-26 01:37:45 -0800 | [diff] [blame] | 844 | cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | if (!cc->page_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 846 | ti->error = "Cannot allocate page mempool"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 847 | goto bad_page_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Jens Axboe | 5972511 | 2007-04-02 10:06:42 +0200 | [diff] [blame] | 850 | cc->bs = bioset_create(MIN_IOS, MIN_IOS); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 851 | if (!cc->bs) { |
| 852 | ti->error = "Cannot allocate crypt bioset"; |
| 853 | goto bad_bs; |
| 854 | } |
| 855 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 856 | if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 857 | ti->error = "Error setting key"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 858 | goto bad_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 861 | if (sscanf(argv[2], "%llu", &tmpll) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 862 | ti->error = "Invalid iv_offset sector"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 863 | goto bad_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 865 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 867 | if (sscanf(argv[4], "%llu", &tmpll) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 868 | ti->error = "Invalid device sector"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 869 | goto bad_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 871 | cc->start = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
| 873 | if (dm_get_device(ti, argv[3], cc->start, ti->len, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 874 | dm_table_get_mode(ti->table), &cc->dev)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 875 | ti->error = "Device lookup failed"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 876 | goto bad_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | if (ivmode && cc->iv_gen_ops) { |
| 880 | if (ivopts) |
| 881 | *(ivopts - 1) = ':'; |
| 882 | cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL); |
| 883 | if (!cc->iv_mode) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 884 | ti->error = "Error kmallocing iv_mode string"; |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 885 | goto bad_ivmode_string; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
| 887 | strcpy(cc->iv_mode, ivmode); |
| 888 | } else |
| 889 | cc->iv_mode = NULL; |
| 890 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 891 | cc->io_queue = create_singlethread_workqueue("kcryptd_io"); |
| 892 | if (!cc->io_queue) { |
| 893 | ti->error = "Couldn't create kcryptd io queue"; |
| 894 | goto bad_io_queue; |
| 895 | } |
| 896 | |
| 897 | cc->crypt_queue = create_singlethread_workqueue("kcryptd"); |
| 898 | if (!cc->crypt_queue) { |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 899 | ti->error = "Couldn't create kcryptd queue"; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 900 | goto bad_crypt_queue; |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | ti->private = cc; |
| 904 | return 0; |
| 905 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 906 | bad_crypt_queue: |
| 907 | destroy_workqueue(cc->io_queue); |
| 908 | bad_io_queue: |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 909 | kfree(cc->iv_mode); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 910 | bad_ivmode_string: |
Dmitry Monakhov | 55b42c5 | 2007-10-19 22:38:37 +0100 | [diff] [blame] | 911 | dm_put_device(ti, cc->dev); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 912 | bad_device: |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 913 | bioset_free(cc->bs); |
| 914 | bad_bs: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | mempool_destroy(cc->page_pool); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 916 | bad_page_pool: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | mempool_destroy(cc->io_pool); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 918 | bad_slab_pool: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 920 | cc->iv_gen_ops->dtr(cc); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 921 | bad_ivmode: |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 922 | crypto_free_blkcipher(tfm); |
Milan Broz | 636d578 | 2007-10-19 22:47:52 +0100 | [diff] [blame] | 923 | bad_cipher: |
Stefan Rompf | 9d3520a | 2006-01-06 00:20:08 -0800 | [diff] [blame] | 924 | /* Must zero key material before freeing */ |
| 925 | memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | kfree(cc); |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | static void crypt_dtr(struct dm_target *ti) |
| 931 | { |
| 932 | struct crypt_config *cc = (struct crypt_config *) ti->private; |
| 933 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 934 | destroy_workqueue(cc->io_queue); |
| 935 | destroy_workqueue(cc->crypt_queue); |
Milan Broz | 80b16c1 | 2007-07-21 04:37:27 -0700 | [diff] [blame] | 936 | |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 937 | bioset_free(cc->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | mempool_destroy(cc->page_pool); |
| 939 | mempool_destroy(cc->io_pool); |
| 940 | |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 941 | kfree(cc->iv_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 943 | cc->iv_gen_ops->dtr(cc); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 944 | crypto_free_blkcipher(cc->tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | dm_put_device(ti, cc->dev); |
Stefan Rompf | 9d3520a | 2006-01-06 00:20:08 -0800 | [diff] [blame] | 946 | |
| 947 | /* Must zero key material before freeing */ |
| 948 | memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | kfree(cc); |
| 950 | } |
| 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | static int crypt_map(struct dm_target *ti, struct bio *bio, |
| 953 | union map_info *map_context) |
| 954 | { |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 955 | struct crypt_config *cc = ti->private; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 956 | struct dm_crypt_io *io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 958 | io = mempool_alloc(cc->io_pool, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | io->target = ti; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 960 | io->base_bio = bio; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 961 | io->error = 0; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 962 | atomic_set(&io->pending, 0); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 963 | |
| 964 | if (bio_data_dir(io->base_bio) == READ) |
| 965 | kcryptd_queue_io(io); |
| 966 | else |
| 967 | kcryptd_queue_crypt(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 969 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static int crypt_status(struct dm_target *ti, status_type_t type, |
| 973 | char *result, unsigned int maxlen) |
| 974 | { |
| 975 | struct crypt_config *cc = (struct crypt_config *) ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | unsigned int sz = 0; |
| 977 | |
| 978 | switch (type) { |
| 979 | case STATUSTYPE_INFO: |
| 980 | result[0] = '\0'; |
| 981 | break; |
| 982 | |
| 983 | case STATUSTYPE_TABLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (cc->iv_mode) |
Christophe Saout | 37af656 | 2006-10-30 20:39:08 +0100 | [diff] [blame] | 985 | DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode, |
| 986 | cc->iv_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | else |
Christophe Saout | 37af656 | 2006-10-30 20:39:08 +0100 | [diff] [blame] | 988 | DMEMIT("%s-%s ", cc->cipher, cc->chainmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
| 990 | if (cc->key_size > 0) { |
| 991 | if ((maxlen - sz) < ((cc->key_size << 1) + 1)) |
| 992 | return -ENOMEM; |
| 993 | |
| 994 | crypt_encode_key(result + sz, cc->key, cc->key_size); |
| 995 | sz += cc->key_size << 1; |
| 996 | } else { |
| 997 | if (sz >= maxlen) |
| 998 | return -ENOMEM; |
| 999 | result[sz++] = '-'; |
| 1000 | } |
| 1001 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1002 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 1003 | cc->dev->name, (unsigned long long)cc->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1009 | static void crypt_postsuspend(struct dm_target *ti) |
| 1010 | { |
| 1011 | struct crypt_config *cc = ti->private; |
| 1012 | |
| 1013 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1014 | } |
| 1015 | |
| 1016 | static int crypt_preresume(struct dm_target *ti) |
| 1017 | { |
| 1018 | struct crypt_config *cc = ti->private; |
| 1019 | |
| 1020 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { |
| 1021 | DMERR("aborting resume - crypt key is not set."); |
| 1022 | return -EAGAIN; |
| 1023 | } |
| 1024 | |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | static void crypt_resume(struct dm_target *ti) |
| 1029 | { |
| 1030 | struct crypt_config *cc = ti->private; |
| 1031 | |
| 1032 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1033 | } |
| 1034 | |
| 1035 | /* Message interface |
| 1036 | * key set <key> |
| 1037 | * key wipe |
| 1038 | */ |
| 1039 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv) |
| 1040 | { |
| 1041 | struct crypt_config *cc = ti->private; |
| 1042 | |
| 1043 | if (argc < 2) |
| 1044 | goto error; |
| 1045 | |
| 1046 | if (!strnicmp(argv[0], MESG_STR("key"))) { |
| 1047 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
| 1048 | DMWARN("not suspended during key manipulation."); |
| 1049 | return -EINVAL; |
| 1050 | } |
| 1051 | if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) |
| 1052 | return crypt_set_key(cc, argv[2]); |
| 1053 | if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) |
| 1054 | return crypt_wipe_key(cc); |
| 1055 | } |
| 1056 | |
| 1057 | error: |
| 1058 | DMWARN("unrecognised message received."); |
| 1059 | return -EINVAL; |
| 1060 | } |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | static struct target_type crypt_target = { |
| 1063 | .name = "crypt", |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 1064 | .version= {1, 5, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | .module = THIS_MODULE, |
| 1066 | .ctr = crypt_ctr, |
| 1067 | .dtr = crypt_dtr, |
| 1068 | .map = crypt_map, |
| 1069 | .status = crypt_status, |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1070 | .postsuspend = crypt_postsuspend, |
| 1071 | .preresume = crypt_preresume, |
| 1072 | .resume = crypt_resume, |
| 1073 | .message = crypt_message, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | }; |
| 1075 | |
| 1076 | static int __init dm_crypt_init(void) |
| 1077 | { |
| 1078 | int r; |
| 1079 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1080 | _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (!_crypt_io_pool) |
| 1082 | return -ENOMEM; |
| 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | r = dm_register_target(&crypt_target); |
| 1085 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1086 | DMERR("register failed %d", r); |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1087 | kmem_cache_destroy(_crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | return r; |
| 1091 | } |
| 1092 | |
| 1093 | static void __exit dm_crypt_exit(void) |
| 1094 | { |
| 1095 | int r = dm_unregister_target(&crypt_target); |
| 1096 | |
| 1097 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1098 | DMERR("unregister failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | kmem_cache_destroy(_crypt_io_pool); |
| 1101 | } |
| 1102 | |
| 1103 | module_init(dm_crypt_init); |
| 1104 | module_exit(dm_crypt_exit); |
| 1105 | |
| 1106 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); |
| 1107 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 1108 | MODULE_LICENSE("GPL"); |