dm-crypt.c 31 KB

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