dm-crypt.c 32 KB

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