dm-verity-fec.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef DM_VERITY_FEC_H
  12. #define DM_VERITY_FEC_H
  13. #include "dm-verity.h"
  14. #include <linux/rslib.h>
  15. /* Reed-Solomon(M, N) parameters */
  16. #define DM_VERITY_FEC_RSM 255
  17. #define DM_VERITY_FEC_MAX_RSN 253
  18. #define DM_VERITY_FEC_MIN_RSN 231 /* ~10% space overhead */
  19. /* buffers for deinterleaving and decoding */
  20. #define DM_VERITY_FEC_BUF_PREALLOC 1 /* buffers to preallocate */
  21. #define DM_VERITY_FEC_BUF_RS_BITS 4 /* 1 << RS blocks per buffer */
  22. /* we need buffers for at most 1 << block size RS blocks */
  23. #define DM_VERITY_FEC_BUF_MAX \
  24. (1 << (PAGE_SHIFT - DM_VERITY_FEC_BUF_RS_BITS))
  25. /* maximum recursion level for verity_fec_decode */
  26. #define DM_VERITY_FEC_MAX_RECURSION 4
  27. #define DM_VERITY_OPT_FEC_DEV "use_fec_from_device"
  28. #define DM_VERITY_OPT_FEC_BLOCKS "fec_blocks"
  29. #define DM_VERITY_OPT_FEC_START "fec_start"
  30. #define DM_VERITY_OPT_FEC_ROOTS "fec_roots"
  31. /* configuration */
  32. struct dm_verity_fec {
  33. struct dm_dev *dev; /* parity data device */
  34. struct dm_bufio_client *data_bufio; /* for data dev access */
  35. struct dm_bufio_client *bufio; /* for parity data access */
  36. sector_t start; /* parity data start in blocks */
  37. sector_t blocks; /* number of blocks covered */
  38. sector_t rounds; /* number of interleaving rounds */
  39. sector_t hash_blocks; /* blocks covered after v->hash_start */
  40. unsigned char roots; /* number of parity bytes, M-N of RS(M, N) */
  41. unsigned char rsn; /* N of RS(M, N) */
  42. mempool_t *rs_pool; /* mempool for fio->rs */
  43. mempool_t *prealloc_pool; /* mempool for preallocated buffers */
  44. mempool_t *extra_pool; /* mempool for extra buffers */
  45. mempool_t *output_pool; /* mempool for output */
  46. struct kmem_cache *cache; /* cache for buffers */
  47. };
  48. /* per-bio data */
  49. struct dm_verity_fec_io {
  50. struct rs_control *rs; /* Reed-Solomon state */
  51. int erasures[DM_VERITY_FEC_MAX_RSN]; /* erasures for decode_rs8 */
  52. u8 *bufs[DM_VERITY_FEC_BUF_MAX]; /* bufs for deinterleaving */
  53. unsigned nbufs; /* number of buffers allocated */
  54. u8 *output; /* buffer for corrected output */
  55. size_t output_pos;
  56. unsigned level; /* recursion level */
  57. };
  58. #ifdef CONFIG_DM_VERITY_FEC
  59. /* each feature parameter requires a value */
  60. #define DM_VERITY_OPTS_FEC 8
  61. extern bool verity_fec_is_enabled(struct dm_verity *v);
  62. extern int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
  63. enum verity_block_type type, sector_t block,
  64. u8 *dest, struct bvec_iter *iter);
  65. extern unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz,
  66. char *result, unsigned maxlen);
  67. extern void verity_fec_finish_io(struct dm_verity_io *io);
  68. extern void verity_fec_init_io(struct dm_verity_io *io);
  69. extern bool verity_is_fec_opt_arg(const char *arg_name);
  70. extern int verity_fec_parse_opt_args(struct dm_arg_set *as,
  71. struct dm_verity *v, unsigned *argc,
  72. const char *arg_name);
  73. extern void verity_fec_dtr(struct dm_verity *v);
  74. extern int verity_fec_ctr_alloc(struct dm_verity *v);
  75. extern int verity_fec_ctr(struct dm_verity *v);
  76. #else /* !CONFIG_DM_VERITY_FEC */
  77. #define DM_VERITY_OPTS_FEC 0
  78. static inline bool verity_fec_is_enabled(struct dm_verity *v)
  79. {
  80. return false;
  81. }
  82. static inline int verity_fec_decode(struct dm_verity *v,
  83. struct dm_verity_io *io,
  84. enum verity_block_type type,
  85. sector_t block, u8 *dest,
  86. struct bvec_iter *iter)
  87. {
  88. return -EOPNOTSUPP;
  89. }
  90. static inline unsigned verity_fec_status_table(struct dm_verity *v,
  91. unsigned sz, char *result,
  92. unsigned maxlen)
  93. {
  94. return sz;
  95. }
  96. static inline void verity_fec_finish_io(struct dm_verity_io *io)
  97. {
  98. }
  99. static inline void verity_fec_init_io(struct dm_verity_io *io)
  100. {
  101. }
  102. static inline bool verity_is_fec_opt_arg(const char *arg_name)
  103. {
  104. return false;
  105. }
  106. static inline int verity_fec_parse_opt_args(struct dm_arg_set *as,
  107. struct dm_verity *v,
  108. unsigned *argc,
  109. const char *arg_name)
  110. {
  111. return -EINVAL;
  112. }
  113. static inline void verity_fec_dtr(struct dm_verity *v)
  114. {
  115. }
  116. static inline int verity_fec_ctr_alloc(struct dm_verity *v)
  117. {
  118. return 0;
  119. }
  120. static inline int verity_fec_ctr(struct dm_verity *v)
  121. {
  122. return 0;
  123. }
  124. #endif /* CONFIG_DM_VERITY_FEC */
  125. #endif /* DM_VERITY_FEC_H */