dm-verity-fec.h 4.5 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. size_t io_size; /* IO size for roots */
  37. sector_t start; /* parity data start in blocks */
  38. sector_t blocks; /* number of blocks covered */
  39. sector_t rounds; /* number of interleaving rounds */
  40. sector_t hash_blocks; /* blocks covered after v->hash_start */
  41. unsigned char roots; /* number of parity bytes, M-N of RS(M, N) */
  42. unsigned char rsn; /* N of RS(M, N) */
  43. mempool_t rs_pool; /* mempool for fio->rs */
  44. mempool_t prealloc_pool; /* mempool for preallocated buffers */
  45. mempool_t extra_pool; /* mempool for extra buffers */
  46. mempool_t output_pool; /* mempool for output */
  47. struct kmem_cache *cache; /* cache for buffers */
  48. };
  49. /* per-bio data */
  50. struct dm_verity_fec_io {
  51. struct rs_control *rs; /* Reed-Solomon state */
  52. int erasures[DM_VERITY_FEC_MAX_RSN]; /* erasures for decode_rs8 */
  53. u8 *bufs[DM_VERITY_FEC_BUF_MAX]; /* bufs for deinterleaving */
  54. unsigned nbufs; /* number of buffers allocated */
  55. u8 *output; /* buffer for corrected output */
  56. size_t output_pos;
  57. unsigned level; /* recursion level */
  58. };
  59. #ifdef CONFIG_DM_VERITY_FEC
  60. /* each feature parameter requires a value */
  61. #define DM_VERITY_OPTS_FEC 8
  62. extern bool verity_fec_is_enabled(struct dm_verity *v);
  63. extern int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
  64. enum verity_block_type type, sector_t block,
  65. u8 *dest, struct bvec_iter *iter);
  66. extern unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz,
  67. char *result, unsigned maxlen);
  68. extern void verity_fec_finish_io(struct dm_verity_io *io);
  69. extern void verity_fec_init_io(struct dm_verity_io *io);
  70. extern bool verity_is_fec_opt_arg(const char *arg_name);
  71. extern int verity_fec_parse_opt_args(struct dm_arg_set *as,
  72. struct dm_verity *v, unsigned *argc,
  73. const char *arg_name);
  74. extern void verity_fec_dtr(struct dm_verity *v);
  75. extern int verity_fec_ctr_alloc(struct dm_verity *v);
  76. extern int verity_fec_ctr(struct dm_verity *v);
  77. #else /* !CONFIG_DM_VERITY_FEC */
  78. #define DM_VERITY_OPTS_FEC 0
  79. static inline bool verity_fec_is_enabled(struct dm_verity *v)
  80. {
  81. return false;
  82. }
  83. static inline int verity_fec_decode(struct dm_verity *v,
  84. struct dm_verity_io *io,
  85. enum verity_block_type type,
  86. sector_t block, u8 *dest,
  87. struct bvec_iter *iter)
  88. {
  89. return -EOPNOTSUPP;
  90. }
  91. static inline unsigned verity_fec_status_table(struct dm_verity *v,
  92. unsigned sz, char *result,
  93. unsigned maxlen)
  94. {
  95. return sz;
  96. }
  97. static inline void verity_fec_finish_io(struct dm_verity_io *io)
  98. {
  99. }
  100. static inline void verity_fec_init_io(struct dm_verity_io *io)
  101. {
  102. }
  103. static inline bool verity_is_fec_opt_arg(const char *arg_name)
  104. {
  105. return false;
  106. }
  107. static inline int verity_fec_parse_opt_args(struct dm_arg_set *as,
  108. struct dm_verity *v,
  109. unsigned *argc,
  110. const char *arg_name)
  111. {
  112. return -EINVAL;
  113. }
  114. static inline void verity_fec_dtr(struct dm_verity *v)
  115. {
  116. }
  117. static inline int verity_fec_ctr_alloc(struct dm_verity *v)
  118. {
  119. return 0;
  120. }
  121. static inline int verity_fec_ctr(struct dm_verity *v)
  122. {
  123. return 0;
  124. }
  125. #endif /* CONFIG_DM_VERITY_FEC */
  126. #endif /* DM_VERITY_FEC_H */