dm-verity-fec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright (C) 2015 Google, Inc.
  3. *
  4. * Author: Sami Tolvanen <samitolvanen@google.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include "dm-verity-fec.h"
  12. #include <linux/math64.h>
  13. #define DM_MSG_PREFIX "verity-fec"
  14. /*
  15. * If error correction has been configured, returns true.
  16. */
  17. bool verity_fec_is_enabled(struct dm_verity *v)
  18. {
  19. return v->fec && v->fec->dev;
  20. }
  21. /*
  22. * Return a pointer to dm_verity_fec_io after dm_verity_io and its variable
  23. * length fields.
  24. */
  25. static inline struct dm_verity_fec_io *fec_io(struct dm_verity_io *io)
  26. {
  27. return (struct dm_verity_fec_io *) verity_io_digest_end(io->v, io);
  28. }
  29. /*
  30. * Return an interleaved offset for a byte in RS block.
  31. */
  32. static inline u64 fec_interleave(struct dm_verity *v, u64 offset)
  33. {
  34. u32 mod;
  35. mod = do_div(offset, v->fec->rsn);
  36. return offset + mod * (v->fec->rounds << v->data_dev_block_bits);
  37. }
  38. /*
  39. * Decode an RS block using Reed-Solomon.
  40. */
  41. static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio,
  42. u8 *data, u8 *fec, int neras)
  43. {
  44. int i;
  45. uint16_t par[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
  46. for (i = 0; i < v->fec->roots; i++)
  47. par[i] = fec[i];
  48. return decode_rs8(fio->rs, data, par, v->fec->rsn, NULL, neras,
  49. fio->erasures, 0, NULL);
  50. }
  51. /*
  52. * Read error-correcting codes for the requested RS block. Returns a pointer
  53. * to the data block. Caller is responsible for releasing buf.
  54. */
  55. static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index,
  56. unsigned *offset, struct dm_buffer **buf)
  57. {
  58. u64 position, block, rem;
  59. u8 *res;
  60. position = (index + rsb) * v->fec->roots;
  61. block = div64_u64_rem(position, v->fec->io_size, &rem);
  62. *offset = (unsigned)rem;
  63. res = dm_bufio_read(v->fec->bufio, block, buf);
  64. if (unlikely(IS_ERR(res))) {
  65. DMERR("%s: FEC %llu: parity read failed (block %llu): %ld",
  66. v->data_dev->name, (unsigned long long)rsb,
  67. (unsigned long long)block, PTR_ERR(res));
  68. *buf = NULL;
  69. }
  70. return res;
  71. }
  72. /* Loop over each preallocated buffer slot. */
  73. #define fec_for_each_prealloc_buffer(__i) \
  74. for (__i = 0; __i < DM_VERITY_FEC_BUF_PREALLOC; __i++)
  75. /* Loop over each extra buffer slot. */
  76. #define fec_for_each_extra_buffer(io, __i) \
  77. for (__i = DM_VERITY_FEC_BUF_PREALLOC; __i < DM_VERITY_FEC_BUF_MAX; __i++)
  78. /* Loop over each allocated buffer. */
  79. #define fec_for_each_buffer(io, __i) \
  80. for (__i = 0; __i < (io)->nbufs; __i++)
  81. /* Loop over each RS block in each allocated buffer. */
  82. #define fec_for_each_buffer_rs_block(io, __i, __j) \
  83. fec_for_each_buffer(io, __i) \
  84. for (__j = 0; __j < 1 << DM_VERITY_FEC_BUF_RS_BITS; __j++)
  85. /*
  86. * Return a pointer to the current RS block when called inside
  87. * fec_for_each_buffer_rs_block.
  88. */
  89. static inline u8 *fec_buffer_rs_block(struct dm_verity *v,
  90. struct dm_verity_fec_io *fio,
  91. unsigned i, unsigned j)
  92. {
  93. return &fio->bufs[i][j * v->fec->rsn];
  94. }
  95. /*
  96. * Return an index to the current RS block when called inside
  97. * fec_for_each_buffer_rs_block.
  98. */
  99. static inline unsigned fec_buffer_rs_index(unsigned i, unsigned j)
  100. {
  101. return (i << DM_VERITY_FEC_BUF_RS_BITS) + j;
  102. }
  103. /*
  104. * Decode all RS blocks from buffers and copy corrected bytes into fio->output
  105. * starting from block_offset.
  106. */
  107. static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio,
  108. u64 rsb, int byte_index, unsigned block_offset,
  109. int neras)
  110. {
  111. int r, corrected = 0, res;
  112. struct dm_buffer *buf;
  113. unsigned n, i, offset;
  114. u8 *par, *block;
  115. par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
  116. if (IS_ERR(par))
  117. return PTR_ERR(par);
  118. /*
  119. * Decode the RS blocks we have in bufs. Each RS block results in
  120. * one corrected target byte and consumes fec->roots parity bytes.
  121. */
  122. fec_for_each_buffer_rs_block(fio, n, i) {
  123. block = fec_buffer_rs_block(v, fio, n, i);
  124. res = fec_decode_rs8(v, fio, block, &par[offset], neras);
  125. if (res < 0) {
  126. r = res;
  127. goto error;
  128. }
  129. corrected += res;
  130. fio->output[block_offset] = block[byte_index];
  131. block_offset++;
  132. if (block_offset >= 1 << v->data_dev_block_bits)
  133. goto done;
  134. /* read the next block when we run out of parity bytes */
  135. offset += v->fec->roots;
  136. if (offset >= v->fec->io_size) {
  137. dm_bufio_release(buf);
  138. par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
  139. if (unlikely(IS_ERR(par)))
  140. return PTR_ERR(par);
  141. }
  142. }
  143. done:
  144. r = corrected;
  145. error:
  146. dm_bufio_release(buf);
  147. if (r < 0 && neras)
  148. DMERR_LIMIT("%s: FEC %llu: failed to correct: %d",
  149. v->data_dev->name, (unsigned long long)rsb, r);
  150. else if (r > 0)
  151. DMWARN_LIMIT("%s: FEC %llu: corrected %d errors",
  152. v->data_dev->name, (unsigned long long)rsb, r);
  153. return r;
  154. }
  155. /*
  156. * Locate data block erasures using verity hashes.
  157. */
  158. static int fec_is_erasure(struct dm_verity *v, struct dm_verity_io *io,
  159. u8 *want_digest, u8 *data)
  160. {
  161. if (unlikely(verity_hash(v, verity_io_hash_req(v, io),
  162. data, 1 << v->data_dev_block_bits,
  163. verity_io_real_digest(v, io))))
  164. return 0;
  165. return memcmp(verity_io_real_digest(v, io), want_digest,
  166. v->digest_size) != 0;
  167. }
  168. /*
  169. * Read data blocks that are part of the RS block and deinterleave as much as
  170. * fits into buffers. Check for erasure locations if @neras is non-NULL.
  171. */
  172. static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io,
  173. u64 rsb, u64 target, unsigned block_offset,
  174. int *neras)
  175. {
  176. bool is_zero;
  177. int i, j, target_index = -1;
  178. struct dm_buffer *buf;
  179. struct dm_bufio_client *bufio;
  180. struct dm_verity_fec_io *fio = fec_io(io);
  181. u64 block, ileaved;
  182. u8 *bbuf, *rs_block;
  183. u8 want_digest[v->digest_size];
  184. unsigned n, k;
  185. if (neras)
  186. *neras = 0;
  187. /*
  188. * read each of the rsn data blocks that are part of the RS block, and
  189. * interleave contents to available bufs
  190. */
  191. for (i = 0; i < v->fec->rsn; i++) {
  192. ileaved = fec_interleave(v, rsb * v->fec->rsn + i);
  193. /*
  194. * target is the data block we want to correct, target_index is
  195. * the index of this block within the rsn RS blocks
  196. */
  197. if (ileaved == target)
  198. target_index = i;
  199. block = ileaved >> v->data_dev_block_bits;
  200. bufio = v->fec->data_bufio;
  201. if (block >= v->data_blocks) {
  202. block -= v->data_blocks;
  203. /*
  204. * blocks outside the area were assumed to contain
  205. * zeros when encoding data was generated
  206. */
  207. if (unlikely(block >= v->fec->hash_blocks))
  208. continue;
  209. block += v->hash_start;
  210. bufio = v->bufio;
  211. }
  212. bbuf = dm_bufio_read(bufio, block, &buf);
  213. if (unlikely(IS_ERR(bbuf))) {
  214. DMWARN_LIMIT("%s: FEC %llu: read failed (%llu): %ld",
  215. v->data_dev->name,
  216. (unsigned long long)rsb,
  217. (unsigned long long)block, PTR_ERR(bbuf));
  218. /* assume the block is corrupted */
  219. if (neras && *neras <= v->fec->roots)
  220. fio->erasures[(*neras)++] = i;
  221. continue;
  222. }
  223. /* locate erasures if the block is on the data device */
  224. if (bufio == v->fec->data_bufio &&
  225. verity_hash_for_block(v, io, block, want_digest,
  226. &is_zero) == 0) {
  227. /* skip known zero blocks entirely */
  228. if (is_zero)
  229. goto done;
  230. /*
  231. * skip if we have already found the theoretical
  232. * maximum number (i.e. fec->roots) of erasures
  233. */
  234. if (neras && *neras <= v->fec->roots &&
  235. fec_is_erasure(v, io, want_digest, bbuf))
  236. fio->erasures[(*neras)++] = i;
  237. }
  238. /*
  239. * deinterleave and copy the bytes that fit into bufs,
  240. * starting from block_offset
  241. */
  242. fec_for_each_buffer_rs_block(fio, n, j) {
  243. k = fec_buffer_rs_index(n, j) + block_offset;
  244. if (k >= 1 << v->data_dev_block_bits)
  245. goto done;
  246. rs_block = fec_buffer_rs_block(v, fio, n, j);
  247. rs_block[i] = bbuf[k];
  248. }
  249. done:
  250. dm_bufio_release(buf);
  251. }
  252. return target_index;
  253. }
  254. /*
  255. * Allocate RS control structure and FEC buffers from preallocated mempools,
  256. * and attempt to allocate as many extra buffers as available.
  257. */
  258. static int fec_alloc_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
  259. {
  260. unsigned n;
  261. if (!fio->rs)
  262. fio->rs = mempool_alloc(&v->fec->rs_pool, GFP_NOIO);
  263. fec_for_each_prealloc_buffer(n) {
  264. if (fio->bufs[n])
  265. continue;
  266. fio->bufs[n] = mempool_alloc(&v->fec->prealloc_pool, GFP_NOWAIT);
  267. if (unlikely(!fio->bufs[n])) {
  268. DMERR("failed to allocate FEC buffer");
  269. return -ENOMEM;
  270. }
  271. }
  272. /* try to allocate the maximum number of buffers */
  273. fec_for_each_extra_buffer(fio, n) {
  274. if (fio->bufs[n])
  275. continue;
  276. fio->bufs[n] = mempool_alloc(&v->fec->extra_pool, GFP_NOWAIT);
  277. /* we can manage with even one buffer if necessary */
  278. if (unlikely(!fio->bufs[n]))
  279. break;
  280. }
  281. fio->nbufs = n;
  282. if (!fio->output)
  283. fio->output = mempool_alloc(&v->fec->output_pool, GFP_NOIO);
  284. return 0;
  285. }
  286. /*
  287. * Initialize buffers and clear erasures. fec_read_bufs() assumes buffers are
  288. * zeroed before deinterleaving.
  289. */
  290. static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
  291. {
  292. unsigned n;
  293. fec_for_each_buffer(fio, n)
  294. memset(fio->bufs[n], 0, v->fec->rsn << DM_VERITY_FEC_BUF_RS_BITS);
  295. memset(fio->erasures, 0, sizeof(fio->erasures));
  296. }
  297. /*
  298. * Decode all RS blocks in a single data block and return the target block
  299. * (indicated by @offset) in fio->output. If @use_erasures is non-zero, uses
  300. * hashes to locate erasures.
  301. */
  302. static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io,
  303. struct dm_verity_fec_io *fio, u64 rsb, u64 offset,
  304. bool use_erasures)
  305. {
  306. int r, neras = 0;
  307. unsigned pos;
  308. r = fec_alloc_bufs(v, fio);
  309. if (unlikely(r < 0))
  310. return r;
  311. for (pos = 0; pos < 1 << v->data_dev_block_bits; ) {
  312. fec_init_bufs(v, fio);
  313. r = fec_read_bufs(v, io, rsb, offset, pos,
  314. use_erasures ? &neras : NULL);
  315. if (unlikely(r < 0))
  316. return r;
  317. r = fec_decode_bufs(v, fio, rsb, r, pos, neras);
  318. if (r < 0)
  319. return r;
  320. pos += fio->nbufs << DM_VERITY_FEC_BUF_RS_BITS;
  321. }
  322. /* Always re-validate the corrected block against the expected hash */
  323. r = verity_hash(v, verity_io_hash_req(v, io), fio->output,
  324. 1 << v->data_dev_block_bits,
  325. verity_io_real_digest(v, io));
  326. if (unlikely(r < 0))
  327. return r;
  328. if (memcmp(verity_io_real_digest(v, io), verity_io_want_digest(v, io),
  329. v->digest_size)) {
  330. DMERR_LIMIT("%s: FEC %llu: failed to correct (%d erasures)",
  331. v->data_dev->name, (unsigned long long)rsb, neras);
  332. return -EILSEQ;
  333. }
  334. return 0;
  335. }
  336. static int fec_bv_copy(struct dm_verity *v, struct dm_verity_io *io, u8 *data,
  337. size_t len)
  338. {
  339. struct dm_verity_fec_io *fio = fec_io(io);
  340. memcpy(data, &fio->output[fio->output_pos], len);
  341. fio->output_pos += len;
  342. return 0;
  343. }
  344. /*
  345. * Correct errors in a block. Copies corrected block to dest if non-NULL,
  346. * otherwise to a bio_vec starting from iter.
  347. */
  348. int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
  349. enum verity_block_type type, sector_t block, u8 *dest,
  350. struct bvec_iter *iter)
  351. {
  352. int r;
  353. struct dm_verity_fec_io *fio = fec_io(io);
  354. u64 offset, res, rsb;
  355. if (!verity_fec_is_enabled(v))
  356. return -EOPNOTSUPP;
  357. if (fio->level >= DM_VERITY_FEC_MAX_RECURSION) {
  358. DMWARN_LIMIT("%s: FEC: recursion too deep", v->data_dev->name);
  359. return -EIO;
  360. }
  361. fio->level++;
  362. if (type == DM_VERITY_BLOCK_TYPE_METADATA)
  363. block = block - v->hash_start + v->data_blocks;
  364. /*
  365. * For RS(M, N), the continuous FEC data is divided into blocks of N
  366. * bytes. Since block size may not be divisible by N, the last block
  367. * is zero padded when decoding.
  368. *
  369. * Each byte of the block is covered by a different RS(M, N) code,
  370. * and each code is interleaved over N blocks to make it less likely
  371. * that bursty corruption will leave us in unrecoverable state.
  372. */
  373. offset = block << v->data_dev_block_bits;
  374. res = div64_u64(offset, v->fec->rounds << v->data_dev_block_bits);
  375. /*
  376. * The base RS block we can feed to the interleaver to find out all
  377. * blocks required for decoding.
  378. */
  379. rsb = offset - res * (v->fec->rounds << v->data_dev_block_bits);
  380. /*
  381. * Locating erasures is slow, so attempt to recover the block without
  382. * them first. Do a second attempt with erasures if the corruption is
  383. * bad enough.
  384. */
  385. r = fec_decode_rsb(v, io, fio, rsb, offset, false);
  386. if (r < 0) {
  387. r = fec_decode_rsb(v, io, fio, rsb, offset, true);
  388. if (r < 0)
  389. goto done;
  390. }
  391. if (dest)
  392. memcpy(dest, fio->output, 1 << v->data_dev_block_bits);
  393. else if (iter) {
  394. fio->output_pos = 0;
  395. r = verity_for_bv_block(v, io, iter, fec_bv_copy);
  396. }
  397. done:
  398. fio->level--;
  399. return r;
  400. }
  401. /*
  402. * Clean up per-bio data.
  403. */
  404. void verity_fec_finish_io(struct dm_verity_io *io)
  405. {
  406. unsigned n;
  407. struct dm_verity_fec *f = io->v->fec;
  408. struct dm_verity_fec_io *fio = fec_io(io);
  409. if (!verity_fec_is_enabled(io->v))
  410. return;
  411. mempool_free(fio->rs, &f->rs_pool);
  412. fec_for_each_prealloc_buffer(n)
  413. mempool_free(fio->bufs[n], &f->prealloc_pool);
  414. fec_for_each_extra_buffer(fio, n)
  415. mempool_free(fio->bufs[n], &f->extra_pool);
  416. mempool_free(fio->output, &f->output_pool);
  417. }
  418. /*
  419. * Initialize per-bio data.
  420. */
  421. void verity_fec_init_io(struct dm_verity_io *io)
  422. {
  423. struct dm_verity_fec_io *fio = fec_io(io);
  424. if (!verity_fec_is_enabled(io->v))
  425. return;
  426. fio->rs = NULL;
  427. memset(fio->bufs, 0, sizeof(fio->bufs));
  428. fio->nbufs = 0;
  429. fio->output = NULL;
  430. fio->level = 0;
  431. }
  432. /*
  433. * Append feature arguments and values to the status table.
  434. */
  435. unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz,
  436. char *result, unsigned maxlen)
  437. {
  438. if (!verity_fec_is_enabled(v))
  439. return sz;
  440. DMEMIT(" " DM_VERITY_OPT_FEC_DEV " %s "
  441. DM_VERITY_OPT_FEC_BLOCKS " %llu "
  442. DM_VERITY_OPT_FEC_START " %llu "
  443. DM_VERITY_OPT_FEC_ROOTS " %d",
  444. v->fec->dev->name,
  445. (unsigned long long)v->fec->blocks,
  446. (unsigned long long)v->fec->start,
  447. v->fec->roots);
  448. return sz;
  449. }
  450. void verity_fec_dtr(struct dm_verity *v)
  451. {
  452. struct dm_verity_fec *f = v->fec;
  453. if (!verity_fec_is_enabled(v))
  454. goto out;
  455. mempool_exit(&f->rs_pool);
  456. mempool_exit(&f->prealloc_pool);
  457. mempool_exit(&f->extra_pool);
  458. mempool_exit(&f->output_pool);
  459. kmem_cache_destroy(f->cache);
  460. if (f->data_bufio)
  461. dm_bufio_client_destroy(f->data_bufio);
  462. if (f->bufio)
  463. dm_bufio_client_destroy(f->bufio);
  464. if (f->dev)
  465. dm_put_device(v->ti, f->dev);
  466. out:
  467. kfree(f);
  468. v->fec = NULL;
  469. }
  470. static void *fec_rs_alloc(gfp_t gfp_mask, void *pool_data)
  471. {
  472. struct dm_verity *v = (struct dm_verity *)pool_data;
  473. return init_rs_gfp(8, 0x11d, 0, 1, v->fec->roots, gfp_mask);
  474. }
  475. static void fec_rs_free(void *element, void *pool_data)
  476. {
  477. struct rs_control *rs = (struct rs_control *)element;
  478. if (rs)
  479. free_rs(rs);
  480. }
  481. bool verity_is_fec_opt_arg(const char *arg_name)
  482. {
  483. return (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV) ||
  484. !strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS) ||
  485. !strcasecmp(arg_name, DM_VERITY_OPT_FEC_START) ||
  486. !strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS));
  487. }
  488. int verity_fec_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
  489. unsigned *argc, const char *arg_name)
  490. {
  491. int r;
  492. struct dm_target *ti = v->ti;
  493. const char *arg_value;
  494. unsigned long long num_ll;
  495. unsigned char num_c;
  496. char dummy;
  497. if (!*argc) {
  498. ti->error = "FEC feature arguments require a value";
  499. return -EINVAL;
  500. }
  501. arg_value = dm_shift_arg(as);
  502. (*argc)--;
  503. if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV)) {
  504. r = dm_get_device(ti, arg_value, FMODE_READ, &v->fec->dev);
  505. if (r) {
  506. ti->error = "FEC device lookup failed";
  507. return r;
  508. }
  509. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS)) {
  510. if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
  511. ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  512. >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
  513. ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
  514. return -EINVAL;
  515. }
  516. v->fec->blocks = num_ll;
  517. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START)) {
  518. if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
  519. ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) >>
  520. (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
  521. ti->error = "Invalid " DM_VERITY_OPT_FEC_START;
  522. return -EINVAL;
  523. }
  524. v->fec->start = num_ll;
  525. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS)) {
  526. if (sscanf(arg_value, "%hhu%c", &num_c, &dummy) != 1 || !num_c ||
  527. num_c < (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MAX_RSN) ||
  528. num_c > (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN)) {
  529. ti->error = "Invalid " DM_VERITY_OPT_FEC_ROOTS;
  530. return -EINVAL;
  531. }
  532. v->fec->roots = num_c;
  533. } else {
  534. ti->error = "Unrecognized verity FEC feature request";
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. /*
  540. * Allocate dm_verity_fec for v->fec. Must be called before verity_fec_ctr.
  541. */
  542. int verity_fec_ctr_alloc(struct dm_verity *v)
  543. {
  544. struct dm_verity_fec *f;
  545. f = kzalloc(sizeof(struct dm_verity_fec), GFP_KERNEL);
  546. if (!f) {
  547. v->ti->error = "Cannot allocate FEC structure";
  548. return -ENOMEM;
  549. }
  550. v->fec = f;
  551. return 0;
  552. }
  553. /*
  554. * Validate arguments and preallocate memory. Must be called after arguments
  555. * have been parsed using verity_fec_parse_opt_args.
  556. */
  557. int verity_fec_ctr(struct dm_verity *v)
  558. {
  559. struct dm_verity_fec *f = v->fec;
  560. struct dm_target *ti = v->ti;
  561. u64 hash_blocks, fec_blocks;
  562. int ret;
  563. if (!verity_fec_is_enabled(v)) {
  564. verity_fec_dtr(v);
  565. return 0;
  566. }
  567. /*
  568. * FEC is computed over data blocks, possible metadata, and
  569. * hash blocks. In other words, FEC covers total of fec_blocks
  570. * blocks consisting of the following:
  571. *
  572. * data blocks | hash blocks | metadata (optional)
  573. *
  574. * We allow metadata after hash blocks to support a use case
  575. * where all data is stored on the same device and FEC covers
  576. * the entire area.
  577. *
  578. * If metadata is included, we require it to be available on the
  579. * hash device after the hash blocks.
  580. */
  581. hash_blocks = v->hash_blocks - v->hash_start;
  582. /*
  583. * Require matching block sizes for data and hash devices for
  584. * simplicity.
  585. */
  586. if (v->data_dev_block_bits != v->hash_dev_block_bits) {
  587. ti->error = "Block sizes must match to use FEC";
  588. return -EINVAL;
  589. }
  590. if (!f->roots) {
  591. ti->error = "Missing " DM_VERITY_OPT_FEC_ROOTS;
  592. return -EINVAL;
  593. }
  594. f->rsn = DM_VERITY_FEC_RSM - f->roots;
  595. if (!f->blocks) {
  596. ti->error = "Missing " DM_VERITY_OPT_FEC_BLOCKS;
  597. return -EINVAL;
  598. }
  599. f->rounds = f->blocks;
  600. if (sector_div(f->rounds, f->rsn))
  601. f->rounds++;
  602. /*
  603. * Due to optional metadata, f->blocks can be larger than
  604. * data_blocks and hash_blocks combined.
  605. */
  606. if (f->blocks < v->data_blocks + hash_blocks || !f->rounds) {
  607. ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
  608. return -EINVAL;
  609. }
  610. /*
  611. * Metadata is accessed through the hash device, so we require
  612. * it to be large enough.
  613. */
  614. f->hash_blocks = f->blocks - v->data_blocks;
  615. if (dm_bufio_get_device_size(v->bufio) < f->hash_blocks) {
  616. ti->error = "Hash device is too small for "
  617. DM_VERITY_OPT_FEC_BLOCKS;
  618. return -E2BIG;
  619. }
  620. if ((f->roots << SECTOR_SHIFT) & ((1 << v->data_dev_block_bits) - 1))
  621. f->io_size = 1 << v->data_dev_block_bits;
  622. else
  623. f->io_size = v->fec->roots << SECTOR_SHIFT;
  624. f->bufio = dm_bufio_client_create(f->dev->bdev,
  625. f->io_size,
  626. 1, 0, NULL, NULL);
  627. if (IS_ERR(f->bufio)) {
  628. ti->error = "Cannot initialize FEC bufio client";
  629. return PTR_ERR(f->bufio);
  630. }
  631. dm_bufio_set_sector_offset(f->bufio, f->start << (v->data_dev_block_bits - SECTOR_SHIFT));
  632. fec_blocks = div64_u64(f->rounds * f->roots, v->fec->roots << SECTOR_SHIFT);
  633. if (dm_bufio_get_device_size(f->bufio) < fec_blocks) {
  634. ti->error = "FEC device is too small";
  635. return -E2BIG;
  636. }
  637. f->data_bufio = dm_bufio_client_create(v->data_dev->bdev,
  638. 1 << v->data_dev_block_bits,
  639. 1, 0, NULL, NULL);
  640. if (IS_ERR(f->data_bufio)) {
  641. ti->error = "Cannot initialize FEC data bufio client";
  642. return PTR_ERR(f->data_bufio);
  643. }
  644. if (dm_bufio_get_device_size(f->data_bufio) < v->data_blocks) {
  645. ti->error = "Data device is too small";
  646. return -E2BIG;
  647. }
  648. /* Preallocate an rs_control structure for each worker thread */
  649. ret = mempool_init(&f->rs_pool, num_online_cpus(), fec_rs_alloc,
  650. fec_rs_free, (void *) v);
  651. if (ret) {
  652. ti->error = "Cannot allocate RS pool";
  653. return ret;
  654. }
  655. f->cache = kmem_cache_create("dm_verity_fec_buffers",
  656. f->rsn << DM_VERITY_FEC_BUF_RS_BITS,
  657. 0, 0, NULL);
  658. if (!f->cache) {
  659. ti->error = "Cannot create FEC buffer cache";
  660. return -ENOMEM;
  661. }
  662. /* Preallocate DM_VERITY_FEC_BUF_PREALLOC buffers for each thread */
  663. ret = mempool_init_slab_pool(&f->prealloc_pool, num_online_cpus() *
  664. DM_VERITY_FEC_BUF_PREALLOC,
  665. f->cache);
  666. if (ret) {
  667. ti->error = "Cannot allocate FEC buffer prealloc pool";
  668. return ret;
  669. }
  670. ret = mempool_init_slab_pool(&f->extra_pool, 0, f->cache);
  671. if (ret) {
  672. ti->error = "Cannot allocate FEC buffer extra pool";
  673. return ret;
  674. }
  675. /* Preallocate an output buffer for each thread */
  676. ret = mempool_init_kmalloc_pool(&f->output_pool, num_online_cpus(),
  677. 1 << v->data_dev_block_bits);
  678. if (ret) {
  679. ti->error = "Cannot allocate FEC output pool";
  680. return ret;
  681. }
  682. /* Reserve space for our per-bio data */
  683. ti->per_io_data_size += sizeof(struct dm_verity_fec_io);
  684. return 0;
  685. }