dm-crypt.c 37 KB

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