dm-verity-fec.c 22 KB

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