lfs_emubd.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 = 0, // Error on prog
  35. LFS_EMUBD_BADBLOCK_ERASEERROR = 1, // Error on erase
  36. LFS_EMUBD_BADBLOCK_READERROR = 2, // Error on read
  37. LFS_EMUBD_BADBLOCK_PROGNOOP = 3, // Prog does nothing silently
  38. LFS_EMUBD_BADBLOCK_ERASENOOP = 4, // Erase does nothing silently
  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 = 0, // Progs are atomic
  44. LFS_EMUBD_POWERLOSS_OOO = 1, // Blocks are written out-of-order
  45. } lfs_emubd_powerloss_behavior_t;
  46. // Type for measuring read/program/erase operations
  47. typedef uint64_t lfs_emubd_io_t;
  48. typedef int64_t lfs_emubd_sio_t;
  49. // Type for measuring wear
  50. typedef uint32_t lfs_emubd_wear_t;
  51. typedef int32_t lfs_emubd_swear_t;
  52. // Type for tracking power-cycles
  53. typedef uint32_t lfs_emubd_powercycles_t;
  54. typedef int32_t lfs_emubd_spowercycles_t;
  55. // Type for delays in nanoseconds
  56. typedef uint64_t lfs_emubd_sleep_t;
  57. typedef int64_t lfs_emubd_ssleep_t;
  58. // emubd config, this is required for testing
  59. struct lfs_emubd_config {
  60. // Minimum size of a read operation in bytes.
  61. lfs_size_t read_size;
  62. // Minimum size of a program operation in bytes.
  63. lfs_size_t prog_size;
  64. // Size of an erase operation in bytes.
  65. lfs_size_t erase_size;
  66. // Number of erase blocks on the device.
  67. lfs_size_t erase_count;
  68. // 8-bit erase value to use for simulating erases. -1 does not simulate
  69. // erases, which can speed up testing by avoiding the extra block-device
  70. // operations to store the erase value.
  71. int32_t erase_value;
  72. // Number of erase cycles before a block becomes "bad". The exact behavior
  73. // of bad blocks is controlled by badblock_behavior.
  74. uint32_t erase_cycles;
  75. // The mode determining how bad-blocks fail
  76. lfs_emubd_badblock_behavior_t badblock_behavior;
  77. // Number of write operations (erase/prog) before triggering a power-loss.
  78. // power_cycles=0 disables this. The exact behavior of power-loss is
  79. // controlled by a combination of powerloss_behavior and powerloss_cb.
  80. lfs_emubd_powercycles_t power_cycles;
  81. // The mode determining how power-loss affects disk
  82. lfs_emubd_powerloss_behavior_t powerloss_behavior;
  83. // Function to call to emulate power-loss. The exact behavior of power-loss
  84. // is up to the runner to provide.
  85. void (*powerloss_cb)(void*);
  86. // Data for power-loss callback
  87. void *powerloss_data;
  88. // True to track when power-loss could have occured. Note this involves
  89. // heavy memory usage!
  90. bool track_branches;
  91. // Path to file to use as a mirror of the disk. This provides a way to view
  92. // the current state of the block device.
  93. const char *disk_path;
  94. // Artificial delay in nanoseconds, there is no purpose for this other
  95. // than slowing down the simulation.
  96. lfs_emubd_sleep_t read_sleep;
  97. // Artificial delay in nanoseconds, there is no purpose for this other
  98. // than slowing down the simulation.
  99. lfs_emubd_sleep_t prog_sleep;
  100. // Artificial delay in nanoseconds, there is no purpose for this other
  101. // than slowing down the simulation.
  102. lfs_emubd_sleep_t erase_sleep;
  103. };
  104. // A reference counted block
  105. typedef struct lfs_emubd_block {
  106. uint32_t rc;
  107. lfs_emubd_wear_t wear;
  108. uint8_t data[];
  109. } lfs_emubd_block_t;
  110. // Disk mirror
  111. typedef struct lfs_emubd_disk {
  112. uint32_t rc;
  113. int fd;
  114. uint8_t *scratch;
  115. } lfs_emubd_disk_t;
  116. // emubd state
  117. typedef struct lfs_emubd {
  118. // array of copy-on-write blocks
  119. lfs_emubd_block_t **blocks;
  120. // some other test state
  121. lfs_emubd_io_t readed;
  122. lfs_emubd_io_t proged;
  123. lfs_emubd_io_t erased;
  124. lfs_emubd_powercycles_t power_cycles;
  125. lfs_ssize_t ooo_block;
  126. lfs_emubd_block_t *ooo_data;
  127. lfs_emubd_disk_t *disk;
  128. const struct lfs_emubd_config *cfg;
  129. } lfs_emubd_t;
  130. /// Block device API ///
  131. // Create an emulating block device using the geometry in lfs_config
  132. int lfs_emubd_create(const struct lfs_config *cfg,
  133. const struct lfs_emubd_config *bdcfg);
  134. // Clean up memory associated with block device
  135. int lfs_emubd_destroy(const struct lfs_config *cfg);
  136. // Read a block
  137. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  138. lfs_off_t off, void *buffer, lfs_size_t size);
  139. // Program a block
  140. //
  141. // The block must have previously been erased.
  142. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  143. lfs_off_t off, const void *buffer, lfs_size_t size);
  144. // Erase a block
  145. //
  146. // A block must be erased before being programmed. The
  147. // state of an erased block is undefined.
  148. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block);
  149. // Sync the block device
  150. int lfs_emubd_sync(const struct lfs_config *cfg);
  151. /// Additional extended API for driving test features ///
  152. // A CRC of a block for debugging purposes
  153. int lfs_emubd_crc(const struct lfs_config *cfg,
  154. lfs_block_t block, uint32_t *crc);
  155. // A CRC of the entire block device for debugging purposes
  156. int lfs_emubd_bdcrc(const struct lfs_config *cfg, uint32_t *crc);
  157. // Get total amount of bytes read
  158. lfs_emubd_sio_t lfs_emubd_readed(const struct lfs_config *cfg);
  159. // Get total amount of bytes programmed
  160. lfs_emubd_sio_t lfs_emubd_proged(const struct lfs_config *cfg);
  161. // Get total amount of bytes erased
  162. lfs_emubd_sio_t lfs_emubd_erased(const struct lfs_config *cfg);
  163. // Manually set amount of bytes read
  164. int lfs_emubd_setreaded(const struct lfs_config *cfg, lfs_emubd_io_t readed);
  165. // Manually set amount of bytes programmed
  166. int lfs_emubd_setproged(const struct lfs_config *cfg, lfs_emubd_io_t proged);
  167. // Manually set amount of bytes erased
  168. int lfs_emubd_seterased(const struct lfs_config *cfg, lfs_emubd_io_t erased);
  169. // Get simulated wear on a given block
  170. lfs_emubd_swear_t lfs_emubd_wear(const struct lfs_config *cfg,
  171. lfs_block_t block);
  172. // Manually set simulated wear on a given block
  173. int lfs_emubd_setwear(const struct lfs_config *cfg,
  174. lfs_block_t block, lfs_emubd_wear_t wear);
  175. // Get the remaining power-cycles
  176. lfs_emubd_spowercycles_t lfs_emubd_powercycles(
  177. const struct lfs_config *cfg);
  178. // Manually set the remaining power-cycles
  179. int lfs_emubd_setpowercycles(const struct lfs_config *cfg,
  180. lfs_emubd_powercycles_t power_cycles);
  181. // Create a copy-on-write copy of the state of this block device
  182. int lfs_emubd_copy(const struct lfs_config *cfg, lfs_emubd_t *copy);
  183. #ifdef __cplusplus
  184. } /* extern "C" */
  185. #endif
  186. #endif