dm-crypt.c 33 KB

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