dm-crypt.c 33 KB

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