dm-verity-fec.h 4.2 KB

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