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