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> |
| 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 8 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/bio.h> |
| 13 | #include <linux/blkdev.h> |
| 14 | #include <linux/mempool.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/crypto.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <asm/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 19 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/page.h> |
| 21 | |
| 22 | #include "dm.h" |
| 23 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 24 | #define DM_MSG_PREFIX "crypt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * per bio private data |
| 28 | */ |
| 29 | struct crypt_io { |
| 30 | struct dm_target *target; |
| 31 | struct bio *bio; |
| 32 | struct bio *first_clone; |
| 33 | struct work_struct work; |
| 34 | atomic_t pending; |
| 35 | int error; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * context holding the current state of a multi-part conversion |
| 40 | */ |
| 41 | struct convert_context { |
| 42 | struct bio *bio_in; |
| 43 | struct bio *bio_out; |
| 44 | unsigned int offset_in; |
| 45 | unsigned int offset_out; |
| 46 | unsigned int idx_in; |
| 47 | unsigned int idx_out; |
| 48 | sector_t sector; |
| 49 | int write; |
| 50 | }; |
| 51 | |
| 52 | struct crypt_config; |
| 53 | |
| 54 | struct crypt_iv_operations { |
| 55 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
| 56 | const char *opts); |
| 57 | void (*dtr)(struct crypt_config *cc); |
| 58 | const char *(*status)(struct crypt_config *cc); |
| 59 | int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector); |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * Crypt: maps a linear range of a block device |
| 64 | * and encrypts / decrypts at the same time. |
| 65 | */ |
| 66 | struct crypt_config { |
| 67 | struct dm_dev *dev; |
| 68 | sector_t start; |
| 69 | |
| 70 | /* |
| 71 | * pool for per bio private data and |
| 72 | * for encryption buffer pages |
| 73 | */ |
| 74 | mempool_t *io_pool; |
| 75 | mempool_t *page_pool; |
| 76 | |
| 77 | /* |
| 78 | * crypto related data |
| 79 | */ |
| 80 | struct crypt_iv_operations *iv_gen_ops; |
| 81 | char *iv_mode; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 82 | struct crypto_cipher *iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | sector_t iv_offset; |
| 84 | unsigned int iv_size; |
| 85 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 86 | char cipher[CRYPTO_MAX_ALG_NAME]; |
| 87 | char chainmode[CRYPTO_MAX_ALG_NAME]; |
| 88 | struct crypto_blkcipher *tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | unsigned int key_size; |
| 90 | u8 key[0]; |
| 91 | }; |
| 92 | |
| 93 | #define MIN_IOS 256 |
| 94 | #define MIN_POOL_PAGES 32 |
| 95 | #define MIN_BIO_PAGES 8 |
| 96 | |
| 97 | static kmem_cache_t *_crypt_io_pool; |
| 98 | |
| 99 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * Different IV generation algorithms: |
| 101 | * |
| 102 | * plain: the initial vector is the 32-bit low-endian version of the sector |
| 103 | * number, padded with zeros if neccessary. |
| 104 | * |
| 105 | * ess_iv: "encrypted sector|salt initial vector", the sector number is |
| 106 | * encrypted with the bulk cipher using a salt as key. The salt |
| 107 | * should be derived from the bulk cipher's key via hashing. |
| 108 | * |
| 109 | * plumb: unimplemented, see: |
| 110 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 111 | */ |
| 112 | |
| 113 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 114 | { |
| 115 | memset(iv, 0, cc->iv_size); |
| 116 | *(u32 *)iv = cpu_to_le32(sector & 0xffffffff); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 122 | const char *opts) |
| 123 | { |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 124 | struct crypto_cipher *essiv_tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct crypto_tfm *hash_tfm; |
| 126 | struct scatterlist sg; |
| 127 | unsigned int saltsize; |
| 128 | u8 *salt; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 129 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (opts == NULL) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 132 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | /* Hash the cipher key with the given hash algorithm */ |
Herbert Xu | eb6f116 | 2005-09-01 17:43:25 -0700 | [diff] [blame] | 137 | hash_tfm = crypto_alloc_tfm(opts, CRYPTO_TFM_REQ_MAY_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (hash_tfm == NULL) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 139 | ti->error = "Error initializing ESSIV hash"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | if (crypto_tfm_alg_type(hash_tfm) != CRYPTO_ALG_TYPE_DIGEST) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 144 | ti->error = "Expected digest algorithm for ESSIV hash"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | crypto_free_tfm(hash_tfm); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | saltsize = crypto_tfm_alg_digestsize(hash_tfm); |
| 150 | salt = kmalloc(saltsize, GFP_KERNEL); |
| 151 | if (salt == NULL) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 152 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | crypto_free_tfm(hash_tfm); |
| 154 | return -ENOMEM; |
| 155 | } |
| 156 | |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 157 | sg_set_buf(&sg, cc->key, cc->key_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | crypto_digest_digest(hash_tfm, &sg, 1, salt); |
| 159 | crypto_free_tfm(hash_tfm); |
| 160 | |
| 161 | /* Setup the essiv_tfm with the given salt */ |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 162 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 163 | if (IS_ERR(essiv_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 164 | ti->error = "Error allocating crypto tfm for ESSIV"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | kfree(salt); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 166 | return PTR_ERR(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 168 | if (crypto_cipher_blocksize(essiv_tfm) != |
| 169 | crypto_blkcipher_ivsize(cc->tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 170 | ti->error = "Block size of ESSIV cipher does " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | "not match IV size of block cipher"; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 172 | crypto_free_cipher(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | kfree(salt); |
| 174 | return -EINVAL; |
| 175 | } |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 176 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 177 | if (err) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 178 | ti->error = "Failed to set key for ESSIV cipher"; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 179 | crypto_free_cipher(essiv_tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | kfree(salt); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 181 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | kfree(salt); |
| 184 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 185 | cc->iv_gen_private = essiv_tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 190 | { |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 191 | crypto_free_cipher(cc->iv_gen_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | cc->iv_gen_private = NULL; |
| 193 | } |
| 194 | |
| 195 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector) |
| 196 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | memset(iv, 0, cc->iv_size); |
| 198 | *(u64 *)iv = cpu_to_le64(sector); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 199 | crypto_cipher_encrypt_one(cc->iv_gen_private, iv, iv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
| 204 | .generator = crypt_iv_plain_gen |
| 205 | }; |
| 206 | |
| 207 | static struct crypt_iv_operations crypt_iv_essiv_ops = { |
| 208 | .ctr = crypt_iv_essiv_ctr, |
| 209 | .dtr = crypt_iv_essiv_dtr, |
| 210 | .generator = crypt_iv_essiv_gen |
| 211 | }; |
| 212 | |
| 213 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 214 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out, |
| 216 | struct scatterlist *in, unsigned int length, |
| 217 | int write, sector_t sector) |
| 218 | { |
| 219 | u8 iv[cc->iv_size]; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 220 | struct blkcipher_desc desc = { |
| 221 | .tfm = cc->tfm, |
| 222 | .info = iv, |
| 223 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP, |
| 224 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int r; |
| 226 | |
| 227 | if (cc->iv_gen_ops) { |
| 228 | r = cc->iv_gen_ops->generator(cc, iv, sector); |
| 229 | if (r < 0) |
| 230 | return r; |
| 231 | |
| 232 | if (write) |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 233 | r = crypto_blkcipher_encrypt_iv(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | else |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 235 | r = crypto_blkcipher_decrypt_iv(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } else { |
| 237 | if (write) |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 238 | r = crypto_blkcipher_encrypt(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | else |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 240 | r = crypto_blkcipher_decrypt(&desc, out, in, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return r; |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx, |
| 248 | struct bio *bio_out, struct bio *bio_in, |
| 249 | sector_t sector, int write) |
| 250 | { |
| 251 | ctx->bio_in = bio_in; |
| 252 | ctx->bio_out = bio_out; |
| 253 | ctx->offset_in = 0; |
| 254 | ctx->offset_out = 0; |
| 255 | ctx->idx_in = bio_in ? bio_in->bi_idx : 0; |
| 256 | ctx->idx_out = bio_out ? bio_out->bi_idx : 0; |
| 257 | ctx->sector = sector + cc->iv_offset; |
| 258 | ctx->write = write; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 263 | */ |
| 264 | static int crypt_convert(struct crypt_config *cc, |
| 265 | struct convert_context *ctx) |
| 266 | { |
| 267 | int r = 0; |
| 268 | |
| 269 | while(ctx->idx_in < ctx->bio_in->bi_vcnt && |
| 270 | ctx->idx_out < ctx->bio_out->bi_vcnt) { |
| 271 | struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in); |
| 272 | struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out); |
| 273 | struct scatterlist sg_in = { |
| 274 | .page = bv_in->bv_page, |
| 275 | .offset = bv_in->bv_offset + ctx->offset_in, |
| 276 | .length = 1 << SECTOR_SHIFT |
| 277 | }; |
| 278 | struct scatterlist sg_out = { |
| 279 | .page = bv_out->bv_page, |
| 280 | .offset = bv_out->bv_offset + ctx->offset_out, |
| 281 | .length = 1 << SECTOR_SHIFT |
| 282 | }; |
| 283 | |
| 284 | ctx->offset_in += sg_in.length; |
| 285 | if (ctx->offset_in >= bv_in->bv_len) { |
| 286 | ctx->offset_in = 0; |
| 287 | ctx->idx_in++; |
| 288 | } |
| 289 | |
| 290 | ctx->offset_out += sg_out.length; |
| 291 | if (ctx->offset_out >= bv_out->bv_len) { |
| 292 | ctx->offset_out = 0; |
| 293 | ctx->idx_out++; |
| 294 | } |
| 295 | |
| 296 | r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length, |
| 297 | ctx->write, ctx->sector); |
| 298 | if (r < 0) |
| 299 | break; |
| 300 | |
| 301 | ctx->sector++; |
| 302 | } |
| 303 | |
| 304 | return r; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Generate a new unfragmented bio with the given size |
| 309 | * This should never violate the device limitations |
| 310 | * May return a smaller bio when running out of pages |
| 311 | */ |
| 312 | static struct bio * |
| 313 | crypt_alloc_buffer(struct crypt_config *cc, unsigned int size, |
| 314 | struct bio *base_bio, unsigned int *bio_vec_idx) |
| 315 | { |
| 316 | struct bio *bio; |
| 317 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 318 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | unsigned int i; |
| 320 | |
| 321 | /* |
Nick Piggin | bd53b71 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 322 | * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and |
| 323 | * to fail earlier. This is not necessary but increases throughput. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | * FIXME: Is this really intelligent? |
| 325 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (base_bio) |
Nick Piggin | bd53b71 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 327 | bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | else |
Nick Piggin | bd53b71 | 2005-05-01 08:58:37 -0700 | [diff] [blame] | 329 | bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs); |
| 330 | if (!bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | /* if the last bio was not complete, continue where that one ended */ |
| 334 | bio->bi_idx = *bio_vec_idx; |
| 335 | bio->bi_vcnt = *bio_vec_idx; |
| 336 | bio->bi_size = 0; |
| 337 | bio->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 338 | |
| 339 | /* bio->bi_idx pages have already been allocated */ |
| 340 | size -= bio->bi_idx * PAGE_SIZE; |
| 341 | |
| 342 | for(i = bio->bi_idx; i < nr_iovecs; i++) { |
| 343 | struct bio_vec *bv = bio_iovec_idx(bio, i); |
| 344 | |
| 345 | bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask); |
| 346 | if (!bv->bv_page) |
| 347 | break; |
| 348 | |
| 349 | /* |
| 350 | * if additional pages cannot be allocated without waiting, |
| 351 | * return a partially allocated bio, the caller will then try |
| 352 | * to allocate additional bios while submitting this partial bio |
| 353 | */ |
| 354 | if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1)) |
| 355 | gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT; |
| 356 | |
| 357 | bv->bv_offset = 0; |
| 358 | if (size > PAGE_SIZE) |
| 359 | bv->bv_len = PAGE_SIZE; |
| 360 | else |
| 361 | bv->bv_len = size; |
| 362 | |
| 363 | bio->bi_size += bv->bv_len; |
| 364 | bio->bi_vcnt++; |
| 365 | size -= bv->bv_len; |
| 366 | } |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (!bio->bi_size) { |
| 369 | bio_put(bio); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Remember the last bio_vec allocated to be able |
| 375 | * to correctly continue after the splitting. |
| 376 | */ |
| 377 | *bio_vec_idx = bio->bi_vcnt; |
| 378 | |
| 379 | return bio; |
| 380 | } |
| 381 | |
| 382 | static void crypt_free_buffer_pages(struct crypt_config *cc, |
| 383 | struct bio *bio, unsigned int bytes) |
| 384 | { |
| 385 | unsigned int i, start, end; |
| 386 | struct bio_vec *bv; |
| 387 | |
| 388 | /* |
| 389 | * This is ugly, but Jens Axboe thinks that using bi_idx in the |
| 390 | * endio function is too dangerous at the moment, so I calculate the |
| 391 | * correct position using bi_vcnt and bi_size. |
| 392 | * The bv_offset and bv_len fields might already be modified but we |
| 393 | * know that we always allocated whole pages. |
| 394 | * A fix to the bi_idx issue in the kernel is in the works, so |
| 395 | * we will hopefully be able to revert to the cleaner solution soon. |
| 396 | */ |
| 397 | i = bio->bi_vcnt - 1; |
| 398 | bv = bio_iovec_idx(bio, i); |
| 399 | end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size; |
| 400 | start = end - bytes; |
| 401 | |
| 402 | start >>= PAGE_SHIFT; |
| 403 | if (!bio->bi_size) |
| 404 | end = bio->bi_vcnt; |
| 405 | else |
| 406 | end >>= PAGE_SHIFT; |
| 407 | |
| 408 | for(i = start; i < end; i++) { |
| 409 | bv = bio_iovec_idx(bio, i); |
| 410 | BUG_ON(!bv->bv_page); |
| 411 | mempool_free(bv->bv_page, cc->page_pool); |
| 412 | bv->bv_page = NULL; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * One of the bios was finished. Check for completion of |
| 418 | * the whole request and correctly clean up the buffer. |
| 419 | */ |
| 420 | static void dec_pending(struct crypt_io *io, int error) |
| 421 | { |
| 422 | struct crypt_config *cc = (struct crypt_config *) io->target->private; |
| 423 | |
| 424 | if (error < 0) |
| 425 | io->error = error; |
| 426 | |
| 427 | if (!atomic_dec_and_test(&io->pending)) |
| 428 | return; |
| 429 | |
| 430 | if (io->first_clone) |
| 431 | bio_put(io->first_clone); |
| 432 | |
| 433 | bio_endio(io->bio, io->bio->bi_size, io->error); |
| 434 | |
| 435 | mempool_free(io, cc->io_pool); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * kcryptd: |
| 440 | * |
| 441 | * Needed because it would be very unwise to do decryption in an |
| 442 | * interrupt context, so bios returning from read requests get |
| 443 | * queued here. |
| 444 | */ |
| 445 | static struct workqueue_struct *_kcryptd_workqueue; |
| 446 | |
| 447 | static void kcryptd_do_work(void *data) |
| 448 | { |
| 449 | struct crypt_io *io = (struct crypt_io *) data; |
| 450 | struct crypt_config *cc = (struct crypt_config *) io->target->private; |
| 451 | struct convert_context ctx; |
| 452 | int r; |
| 453 | |
| 454 | crypt_convert_init(cc, &ctx, io->bio, io->bio, |
| 455 | io->bio->bi_sector - io->target->begin, 0); |
| 456 | r = crypt_convert(cc, &ctx); |
| 457 | |
| 458 | dec_pending(io, r); |
| 459 | } |
| 460 | |
| 461 | static void kcryptd_queue_io(struct crypt_io *io) |
| 462 | { |
| 463 | INIT_WORK(&io->work, kcryptd_do_work, io); |
| 464 | queue_work(_kcryptd_workqueue, &io->work); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Decode key from its hex representation |
| 469 | */ |
| 470 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) |
| 471 | { |
| 472 | char buffer[3]; |
| 473 | char *endp; |
| 474 | unsigned int i; |
| 475 | |
| 476 | buffer[2] = '\0'; |
| 477 | |
| 478 | for(i = 0; i < size; i++) { |
| 479 | buffer[0] = *hex++; |
| 480 | buffer[1] = *hex++; |
| 481 | |
| 482 | key[i] = (u8)simple_strtoul(buffer, &endp, 16); |
| 483 | |
| 484 | if (endp != &buffer[2]) |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | if (*hex != '\0') |
| 489 | return -EINVAL; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Encode key into its hex representation |
| 496 | */ |
| 497 | static void crypt_encode_key(char *hex, u8 *key, unsigned int size) |
| 498 | { |
| 499 | unsigned int i; |
| 500 | |
| 501 | for(i = 0; i < size; i++) { |
| 502 | sprintf(hex, "%02x", *key); |
| 503 | hex += 2; |
| 504 | key++; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Construct an encryption mapping: |
| 510 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 511 | */ |
| 512 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 513 | { |
| 514 | struct crypt_config *cc; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 515 | struct crypto_blkcipher *tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | char *tmp; |
| 517 | char *cipher; |
| 518 | char *chainmode; |
| 519 | char *ivmode; |
| 520 | char *ivopts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | unsigned int key_size; |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 522 | unsigned long long tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | if (argc != 5) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 525 | ti->error = "Not enough arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | tmp = argv[0]; |
| 530 | cipher = strsep(&tmp, "-"); |
| 531 | chainmode = strsep(&tmp, "-"); |
| 532 | ivopts = strsep(&tmp, "-"); |
| 533 | ivmode = strsep(&ivopts, ":"); |
| 534 | |
| 535 | if (tmp) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 536 | DMWARN("Unexpected additional cipher options"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | key_size = strlen(argv[1]) >> 1; |
| 539 | |
| 540 | cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
| 541 | if (cc == NULL) { |
| 542 | ti->error = |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 543 | "Cannot allocate transparent encryption context"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return -ENOMEM; |
| 545 | } |
| 546 | |
| 547 | cc->key_size = key_size; |
| 548 | if ((!key_size && strcmp(argv[1], "-") != 0) || |
| 549 | (key_size && crypt_decode_key(cc->key, argv[1], key_size) < 0)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 550 | ti->error = "Error decoding key"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | goto bad1; |
| 552 | } |
| 553 | |
| 554 | /* Compatiblity mode for old dm-crypt cipher strings */ |
| 555 | if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) { |
| 556 | chainmode = "cbc"; |
| 557 | ivmode = "plain"; |
| 558 | } |
| 559 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 560 | if (strcmp(chainmode, "ecb") && !ivmode) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 561 | ti->error = "This chaining mode requires an IV mechanism"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto bad1; |
| 563 | } |
| 564 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 565 | if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode, |
| 566 | cipher) >= CRYPTO_MAX_ALG_NAME) { |
| 567 | ti->error = "Chain mode + cipher name is too long"; |
| 568 | goto bad1; |
| 569 | } |
| 570 | |
| 571 | tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 572 | if (IS_ERR(tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 573 | ti->error = "Error allocating crypto tfm"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto bad1; |
| 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 577 | strcpy(cc->cipher, cipher); |
| 578 | strcpy(cc->chainmode, chainmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | cc->tfm = tfm; |
| 580 | |
| 581 | /* |
| 582 | * Choose ivmode. Valid modes: "plain", "essiv:<esshash>". |
| 583 | * See comments at iv code |
| 584 | */ |
| 585 | |
| 586 | if (ivmode == NULL) |
| 587 | cc->iv_gen_ops = NULL; |
| 588 | else if (strcmp(ivmode, "plain") == 0) |
| 589 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
| 590 | else if (strcmp(ivmode, "essiv") == 0) |
| 591 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
| 592 | else { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 593 | ti->error = "Invalid IV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | goto bad2; |
| 595 | } |
| 596 | |
| 597 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr && |
| 598 | cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0) |
| 599 | goto bad2; |
| 600 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 601 | cc->iv_size = crypto_blkcipher_ivsize(tfm); |
| 602 | if (cc->iv_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | /* at least a 64 bit sector number should fit in our buffer */ |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 604 | cc->iv_size = max(cc->iv_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
| 606 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (cc->iv_gen_ops) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 608 | DMWARN("Selected cipher does not support IVs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (cc->iv_gen_ops->dtr) |
| 610 | cc->iv_gen_ops->dtr(cc); |
| 611 | cc->iv_gen_ops = NULL; |
| 612 | } |
| 613 | } |
| 614 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 615 | cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (!cc->io_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 617 | ti->error = "Cannot allocate crypt io mempool"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | goto bad3; |
| 619 | } |
| 620 | |
Matthew Dobson | a19b27c | 2006-03-26 01:37:45 -0800 | [diff] [blame] | 621 | cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (!cc->page_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 623 | ti->error = "Cannot allocate page mempool"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | goto bad4; |
| 625 | } |
| 626 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 627 | if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 628 | ti->error = "Error setting key"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | goto bad5; |
| 630 | } |
| 631 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 632 | if (sscanf(argv[2], "%llu", &tmpll) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 633 | ti->error = "Invalid iv_offset sector"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | goto bad5; |
| 635 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 636 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 638 | if (sscanf(argv[4], "%llu", &tmpll) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 639 | ti->error = "Invalid device sector"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | goto bad5; |
| 641 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 642 | cc->start = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | if (dm_get_device(ti, argv[3], cc->start, ti->len, |
| 645 | dm_table_get_mode(ti->table), &cc->dev)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 646 | ti->error = "Device lookup failed"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | goto bad5; |
| 648 | } |
| 649 | |
| 650 | if (ivmode && cc->iv_gen_ops) { |
| 651 | if (ivopts) |
| 652 | *(ivopts - 1) = ':'; |
| 653 | cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL); |
| 654 | if (!cc->iv_mode) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 655 | ti->error = "Error kmallocing iv_mode string"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | goto bad5; |
| 657 | } |
| 658 | strcpy(cc->iv_mode, ivmode); |
| 659 | } else |
| 660 | cc->iv_mode = NULL; |
| 661 | |
| 662 | ti->private = cc; |
| 663 | return 0; |
| 664 | |
| 665 | bad5: |
| 666 | mempool_destroy(cc->page_pool); |
| 667 | bad4: |
| 668 | mempool_destroy(cc->io_pool); |
| 669 | bad3: |
| 670 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 671 | cc->iv_gen_ops->dtr(cc); |
| 672 | bad2: |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 673 | crypto_free_blkcipher(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | bad1: |
Stefan Rompf | 9d3520a | 2006-01-06 00:20:08 -0800 | [diff] [blame] | 675 | /* Must zero key material before freeing */ |
| 676 | memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | kfree(cc); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | static void crypt_dtr(struct dm_target *ti) |
| 682 | { |
| 683 | struct crypt_config *cc = (struct crypt_config *) ti->private; |
| 684 | |
| 685 | mempool_destroy(cc->page_pool); |
| 686 | mempool_destroy(cc->io_pool); |
| 687 | |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 688 | kfree(cc->iv_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 690 | cc->iv_gen_ops->dtr(cc); |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 691 | crypto_free_blkcipher(cc->tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | dm_put_device(ti, cc->dev); |
Stefan Rompf | 9d3520a | 2006-01-06 00:20:08 -0800 | [diff] [blame] | 693 | |
| 694 | /* Must zero key material before freeing */ |
| 695 | memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | kfree(cc); |
| 697 | } |
| 698 | |
| 699 | static int crypt_endio(struct bio *bio, unsigned int done, int error) |
| 700 | { |
| 701 | struct crypt_io *io = (struct crypt_io *) bio->bi_private; |
| 702 | struct crypt_config *cc = (struct crypt_config *) io->target->private; |
| 703 | |
| 704 | if (bio_data_dir(bio) == WRITE) { |
| 705 | /* |
| 706 | * free the processed pages, even if |
| 707 | * it's only a partially completed write |
| 708 | */ |
| 709 | crypt_free_buffer_pages(cc, bio, done); |
| 710 | } |
| 711 | |
| 712 | if (bio->bi_size) |
| 713 | return 1; |
| 714 | |
| 715 | bio_put(bio); |
| 716 | |
| 717 | /* |
| 718 | * successful reads are decrypted by the worker thread |
| 719 | */ |
| 720 | if ((bio_data_dir(bio) == READ) |
| 721 | && bio_flagged(bio, BIO_UPTODATE)) { |
| 722 | kcryptd_queue_io(io); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | dec_pending(io, error); |
| 727 | return error; |
| 728 | } |
| 729 | |
| 730 | static inline struct bio * |
| 731 | crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio, |
| 732 | sector_t sector, unsigned int *bvec_idx, |
| 733 | struct convert_context *ctx) |
| 734 | { |
| 735 | struct bio *clone; |
| 736 | |
| 737 | if (bio_data_dir(bio) == WRITE) { |
| 738 | clone = crypt_alloc_buffer(cc, bio->bi_size, |
| 739 | io->first_clone, bvec_idx); |
| 740 | if (clone) { |
| 741 | ctx->bio_out = clone; |
| 742 | if (crypt_convert(cc, ctx) < 0) { |
| 743 | crypt_free_buffer_pages(cc, clone, |
| 744 | clone->bi_size); |
| 745 | bio_put(clone); |
| 746 | return NULL; |
| 747 | } |
| 748 | } |
| 749 | } else { |
| 750 | /* |
| 751 | * The block layer might modify the bvec array, so always |
| 752 | * copy the required bvecs because we need the original |
| 753 | * one in order to decrypt the whole bio data *afterwards*. |
| 754 | */ |
| 755 | clone = bio_alloc(GFP_NOIO, bio_segments(bio)); |
| 756 | if (clone) { |
| 757 | clone->bi_idx = 0; |
| 758 | clone->bi_vcnt = bio_segments(bio); |
| 759 | clone->bi_size = bio->bi_size; |
| 760 | memcpy(clone->bi_io_vec, bio_iovec(bio), |
| 761 | sizeof(struct bio_vec) * clone->bi_vcnt); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (!clone) |
| 766 | return NULL; |
| 767 | |
| 768 | clone->bi_private = io; |
| 769 | clone->bi_end_io = crypt_endio; |
| 770 | clone->bi_bdev = cc->dev->bdev; |
| 771 | clone->bi_sector = cc->start + sector; |
| 772 | clone->bi_rw = bio->bi_rw; |
| 773 | |
| 774 | return clone; |
| 775 | } |
| 776 | |
| 777 | static int crypt_map(struct dm_target *ti, struct bio *bio, |
| 778 | union map_info *map_context) |
| 779 | { |
| 780 | struct crypt_config *cc = (struct crypt_config *) ti->private; |
| 781 | struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO); |
| 782 | struct convert_context ctx; |
| 783 | struct bio *clone; |
| 784 | unsigned int remaining = bio->bi_size; |
| 785 | sector_t sector = bio->bi_sector - ti->begin; |
| 786 | unsigned int bvec_idx = 0; |
| 787 | |
| 788 | io->target = ti; |
| 789 | io->bio = bio; |
| 790 | io->first_clone = NULL; |
| 791 | io->error = 0; |
| 792 | atomic_set(&io->pending, 1); /* hold a reference */ |
| 793 | |
| 794 | if (bio_data_dir(bio) == WRITE) |
| 795 | crypt_convert_init(cc, &ctx, NULL, bio, sector, 1); |
| 796 | |
| 797 | /* |
| 798 | * The allocated buffers can be smaller than the whole bio, |
| 799 | * so repeat the whole process until all the data can be handled. |
| 800 | */ |
| 801 | while (remaining) { |
| 802 | clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx); |
| 803 | if (!clone) |
| 804 | goto cleanup; |
| 805 | |
| 806 | if (!io->first_clone) { |
| 807 | /* |
| 808 | * hold a reference to the first clone, because it |
| 809 | * holds the bio_vec array and that can't be freed |
| 810 | * before all other clones are released |
| 811 | */ |
| 812 | bio_get(clone); |
| 813 | io->first_clone = clone; |
| 814 | } |
| 815 | atomic_inc(&io->pending); |
| 816 | |
| 817 | remaining -= clone->bi_size; |
| 818 | sector += bio_sectors(clone); |
| 819 | |
| 820 | generic_make_request(clone); |
| 821 | |
| 822 | /* out of memory -> run queues */ |
| 823 | if (remaining) |
| 824 | blk_congestion_wait(bio_data_dir(clone), HZ/100); |
| 825 | } |
| 826 | |
| 827 | /* drop reference, clones could have returned before we reach this */ |
| 828 | dec_pending(io, 0); |
| 829 | return 0; |
| 830 | |
| 831 | cleanup: |
| 832 | if (io->first_clone) { |
| 833 | dec_pending(io, -ENOMEM); |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | /* if no bio has been dispatched yet, we can directly return the error */ |
| 838 | mempool_free(io, cc->io_pool); |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
| 842 | static int crypt_status(struct dm_target *ti, status_type_t type, |
| 843 | char *result, unsigned int maxlen) |
| 844 | { |
| 845 | struct crypt_config *cc = (struct crypt_config *) ti->private; |
| 846 | const char *cipher; |
| 847 | const char *chainmode = NULL; |
| 848 | unsigned int sz = 0; |
| 849 | |
| 850 | switch (type) { |
| 851 | case STATUSTYPE_INFO: |
| 852 | result[0] = '\0'; |
| 853 | break; |
| 854 | |
| 855 | case STATUSTYPE_TABLE: |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 856 | cipher = crypto_blkcipher_name(cc->tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 858 | chainmode = cc->chainmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | if (cc->iv_mode) |
| 861 | DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode); |
| 862 | else |
| 863 | DMEMIT("%s-%s ", cipher, chainmode); |
| 864 | |
| 865 | if (cc->key_size > 0) { |
| 866 | if ((maxlen - sz) < ((cc->key_size << 1) + 1)) |
| 867 | return -ENOMEM; |
| 868 | |
| 869 | crypt_encode_key(result + sz, cc->key, cc->key_size); |
| 870 | sz += cc->key_size << 1; |
| 871 | } else { |
| 872 | if (sz >= maxlen) |
| 873 | return -ENOMEM; |
| 874 | result[sz++] = '-'; |
| 875 | } |
| 876 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 877 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 878 | cc->dev->name, (unsigned long long)cc->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static struct target_type crypt_target = { |
| 885 | .name = "crypt", |
| 886 | .version= {1, 1, 0}, |
| 887 | .module = THIS_MODULE, |
| 888 | .ctr = crypt_ctr, |
| 889 | .dtr = crypt_dtr, |
| 890 | .map = crypt_map, |
| 891 | .status = crypt_status, |
| 892 | }; |
| 893 | |
| 894 | static int __init dm_crypt_init(void) |
| 895 | { |
| 896 | int r; |
| 897 | |
| 898 | _crypt_io_pool = kmem_cache_create("dm-crypt_io", |
| 899 | sizeof(struct crypt_io), |
| 900 | 0, 0, NULL, NULL); |
| 901 | if (!_crypt_io_pool) |
| 902 | return -ENOMEM; |
| 903 | |
| 904 | _kcryptd_workqueue = create_workqueue("kcryptd"); |
| 905 | if (!_kcryptd_workqueue) { |
| 906 | r = -ENOMEM; |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 907 | DMERR("couldn't create kcryptd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | goto bad1; |
| 909 | } |
| 910 | |
| 911 | r = dm_register_target(&crypt_target); |
| 912 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 913 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | goto bad2; |
| 915 | } |
| 916 | |
| 917 | return 0; |
| 918 | |
| 919 | bad2: |
| 920 | destroy_workqueue(_kcryptd_workqueue); |
| 921 | bad1: |
| 922 | kmem_cache_destroy(_crypt_io_pool); |
| 923 | return r; |
| 924 | } |
| 925 | |
| 926 | static void __exit dm_crypt_exit(void) |
| 927 | { |
| 928 | int r = dm_unregister_target(&crypt_target); |
| 929 | |
| 930 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 931 | DMERR("unregister failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | destroy_workqueue(_kcryptd_workqueue); |
| 934 | kmem_cache_destroy(_crypt_io_pool); |
| 935 | } |
| 936 | |
| 937 | module_init(dm_crypt_init); |
| 938 | module_exit(dm_crypt_exit); |
| 939 | |
| 940 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); |
| 941 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 942 | MODULE_LICENSE("GPL"); |