lfs_emubd.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Emulating block device, wraps filebd and rambd while providing a bunch
  3. * of hooks for testing littlefs in various conditions.
  4. *
  5. * Copyright (c) 2022, The littlefs authors.
  6. * Copyright (c) 2017, Arm Limited. All rights reserved.
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #ifndef LFS_EMUBD_H
  10. #define LFS_EMUBD_H
  11. #include "lfs.h"
  12. #include "lfs_util.h"
  13. #include "bd/lfs_rambd.h"
  14. #include "bd/lfs_filebd.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. // Block device specific tracing
  20. #ifndef LFS_EMUBD_TRACE
  21. #ifdef LFS_EMUBD_YES_TRACE
  22. #define LFS_EMUBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  23. #else
  24. #define LFS_EMUBD_TRACE(...)
  25. #endif
  26. #endif
  27. // Mode determining how "bad-blocks" behave during testing. This simulates
  28. // some real-world circumstances such as progs not sticking (prog-noop),
  29. // a readonly disk (erase-noop), and ECC failures (read-error).
  30. //
  31. // Not that read-noop is not allowed. Read _must_ return a consistent (but
  32. // may be arbitrary) value on every read.
  33. typedef enum lfs_emubd_badblock_behavior {
  34. LFS_EMUBD_BADBLOCK_PROGERROR,
  35. LFS_EMUBD_BADBLOCK_ERASEERROR,
  36. LFS_EMUBD_BADBLOCK_READERROR,
  37. LFS_EMUBD_BADBLOCK_PROGNOOP,
  38. LFS_EMUBD_BADBLOCK_ERASENOOP,
  39. } lfs_emubd_badblock_behavior_t;
  40. // Mode determining how power-loss behaves during testing. For now this
  41. // only supports a noop behavior, leaving the data on-disk untouched.
  42. typedef enum lfs_emubd_powerloss_behavior {
  43. LFS_EMUBD_POWERLOSS_NOOP,
  44. } lfs_emubd_powerloss_behavior_t;
  45. // Type for measuring read/program/erase operations
  46. typedef uint64_t lfs_emubd_io_t;
  47. typedef int64_t lfs_emubd_sio_t;
  48. // Type for measuring wear
  49. typedef uint32_t lfs_emubd_wear_t;
  50. typedef int32_t lfs_emubd_swear_t;
  51. // Type for tracking power-cycles
  52. typedef uint32_t lfs_emubd_powercycles_t;
  53. typedef int32_t lfs_emubd_spowercycles_t;
  54. // Type for delays in nanoseconds
  55. typedef uint64_t lfs_emubd_sleep_t;
  56. typedef int64_t lfs_emubd_ssleep_t;
  57. // emubd config, this is required for testing
  58. struct lfs_emubd_config {
  59. // Minimum size of a read operation in bytes.
  60. lfs_size_t read_size;
  61. // Minimum size of a program operation in bytes.
  62. lfs_size_t prog_size;
  63. // Size of an erase operation in bytes.
  64. lfs_size_t erase_size;
  65. // Number of erase blocks on the device.
  66. lfs_size_t erase_count;
  67. // 8-bit erase value to use for simulating erases. -1 does not simulate
  68. // erases, which can speed up testing by avoiding the extra block-device
  69. // operations to store the erase value.
  70. int32_t erase_value;
  71. // Number of erase cycles before a block becomes "bad". The exact behavior
  72. // of bad blocks is controlled by badblock_behavior.
  73. uint32_t erase_cycles;
  74. // The mode determining how bad-blocks fail
  75. lfs_emubd_badblock_behavior_t badblock_behavior;
  76. // Number of write operations (erase/prog) before triggering a power-loss.
  77. // power_cycles=0 disables this. The exact behavior of power-loss is
  78. // controlled by a combination of powerloss_behavior and powerloss_cb.
  79. lfs_emubd_powercycles_t power_cycles;
  80. // The mode determining how power-loss affects disk
  81. lfs_emubd_powerloss_behavior_t powerloss_behavior;
  82. // Function to call to emulate power-loss. The exact behavior of power-loss
  83. // is up to the runner to provide.
  84. void (*powerloss_cb)(void*);
  85. // Data for power-loss callback
  86. void *powerloss_data;
  87. // True to track when power-loss could have occured. Note this involves
  88. // heavy memory usage!
  89. bool track_branches;
  90. // Path to file to use as a mirror of the disk. This provides a way to view
  91. // the current state of the block device.
  92. const char *disk_path;
  93. // Artificial delay in nanoseconds, there is no purpose for this other
  94. // than slowing down the simulation.
  95. lfs_emubd_sleep_t read_sleep;
  96. // Artificial delay in nanoseconds, there is no purpose for this other
  97. // than slowing down the simulation.
  98. lfs_emubd_sleep_t prog_sleep;
  99. // Artificial delay in nanoseconds, there is no purpose for this other
  100. // than slowing down the simulation.
  101. lfs_emubd_sleep_t erase_sleep;
  102. };
  103. // A reference counted block
  104. typedef struct lfs_emubd_block {
  105. uint32_t rc;
  106. lfs_emubd_wear_t wear;
  107. uint8_t data[];
  108. } lfs_emubd_block_t;
  109. // Disk mirror
  110. typedef struct lfs_emubd_disk {
  111. uint32_t rc;
  112. int fd;
  113. uint8_t *scratch;
  114. } lfs_emubd_disk_t;
  115. // emubd state
  116. typedef struct lfs_emubd {
  117. // array of copy-on-write blocks
  118. lfs_emubd_block_t **blocks;
  119. // some other test state
  120. lfs_emubd_io_t readed;
  121. lfs_emubd_io_t proged;
  122. lfs_emubd_io_t erased;
  123. lfs_emubd_powercycles_t power_cycles;
  124. lfs_emubd_disk_t *disk;
  125. const struct lfs_emubd_config *cfg;
  126. } lfs_emubd_t;
  127. /// Block device API ///
  128. // Create an emulating block device using the geometry in lfs_config
  129. int lfs_emubd_create(const struct lfs_config *cfg,
  130. const struct lfs_emubd_config *bdcfg);
  131. // Clean up memory associated with block device
  132. int lfs_emubd_destroy(const struct lfs_config *cfg);
  133. // Read a block
  134. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  135. lfs_off_t off, void *buffer, lfs_size_t size);
  136. // Program a block
  137. //
  138. // The block must have previously been erased.
  139. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  140. lfs_off_t off, const void *buffer, lfs_size_t size);
  141. // Erase a block
  142. //
  143. // A block must be erased before being programmed. The
  144. // state of an erased block is undefined.
  145. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block);
  146. // Sync the block device
  147. int lfs_emubd_sync(const struct lfs_config *cfg);
  148. /// Additional extended API for driving test features ///
  149. // A CRC of a block for debugging purposes
  150. int lfs_emubd_crc(const struct lfs_config *cfg,
  151. lfs_block_t block, uint32_t *crc);
  152. // A CRC of the entire block device for debugging purposes
  153. int lfs_emubd_bdcrc(const struct lfs_config *cfg, uint32_t *crc);
  154. // Get total amount of bytes read
  155. lfs_emubd_sio_t lfs_emubd_readed(const struct lfs_config *cfg);
  156. // Get total amount of bytes programmed
  157. lfs_emubd_sio_t lfs_emubd_proged(const struct lfs_config *cfg);
  158. // Get total amount of bytes erased
  159. lfs_emubd_sio_t lfs_emubd_erased(const struct lfs_config *cfg);
  160. // Manually set amount of bytes read
  161. int lfs_emubd_setreaded(const struct lfs_config *cfg, lfs_emubd_io_t readed);
  162. // Manually set amount of bytes programmed
  163. int lfs_emubd_setproged(const struct lfs_config *cfg, lfs_emubd_io_t proged);
  164. // Manually set amount of bytes erased
  165. int lfs_emubd_seterased(const struct lfs_config *cfg, lfs_emubd_io_t erased);
  166. // Get simulated wear on a given block
  167. lfs_emubd_swear_t lfs_emubd_wear(const struct lfs_config *cfg,
  168. lfs_block_t block);
  169. // Manually set simulated wear on a given block
  170. int lfs_emubd_setwear(const struct lfs_config *cfg,
  171. lfs_block_t block, lfs_emubd_wear_t wear);
  172. // Get the remaining power-cycles
  173. lfs_emubd_spowercycles_t lfs_emubd_powercycles(
  174. const struct lfs_config *cfg);
  175. // Manually set the remaining power-cycles
  176. int lfs_emubd_setpowercycles(const struct lfs_config *cfg,
  177. lfs_emubd_powercycles_t power_cycles);
  178. // Create a copy-on-write copy of the state of this block device
  179. int lfs_emubd_copy(const struct lfs_config *cfg, lfs_emubd_t *copy);
  180. #ifdef __cplusplus
  181. } /* extern "C" */
  182. #endif
  183. #endif