dm-verity-fec.c 21 KB

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