lfs_emubd.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // 8-bit erase value to use for simulating erases. -1 does not simulate
  60. // erases, which can speed up testing by avoiding the extra block-device
  61. // operations to store the erase value.
  62. int32_t erase_value;
  63. // Number of erase cycles before a block becomes "bad". The exact behavior
  64. // of bad blocks is controlled by badblock_behavior.
  65. uint32_t erase_cycles;
  66. // The mode determining how bad-blocks fail
  67. lfs_emubd_badblock_behavior_t badblock_behavior;
  68. // Number of write operations (erase/prog) before triggering a power-loss.
  69. // power_cycles=0 disables this. The exact behavior of power-loss is
  70. // controlled by a combination of powerloss_behavior and powerloss_cb.
  71. lfs_emubd_powercycles_t power_cycles;
  72. // The mode determining how power-loss affects disk
  73. lfs_emubd_powerloss_behavior_t powerloss_behavior;
  74. // Function to call to emulate power-loss. The exact behavior of power-loss
  75. // is up to the runner to provide.
  76. void (*powerloss_cb)(void*);
  77. // Data for power-loss callback
  78. void *powerloss_data;
  79. // True to track when power-loss could have occured. Note this involves
  80. // heavy memory usage!
  81. bool track_branches;
  82. // Path to file to use as a mirror of the disk. This provides a way to view
  83. // the current state of the block device.
  84. const char *disk_path;
  85. // Artificial delay in nanoseconds, there is no purpose for this other
  86. // than slowing down the simulation.
  87. lfs_emubd_sleep_t read_sleep;
  88. // Artificial delay in nanoseconds, there is no purpose for this other
  89. // than slowing down the simulation.
  90. lfs_emubd_sleep_t prog_sleep;
  91. // Artificial delay in nanoseconds, there is no purpose for this other
  92. // than slowing down the simulation.
  93. lfs_emubd_sleep_t erase_sleep;
  94. };
  95. // A reference counted block
  96. typedef struct lfs_emubd_block {
  97. uint32_t rc;
  98. lfs_emubd_wear_t wear;
  99. uint8_t data[];
  100. } lfs_emubd_block_t;
  101. // Disk mirror
  102. typedef struct lfs_emubd_disk {
  103. uint32_t rc;
  104. int fd;
  105. uint8_t *scratch;
  106. } lfs_emubd_disk_t;
  107. // emubd state
  108. typedef struct lfs_emubd {
  109. // array of copy-on-write blocks
  110. lfs_emubd_block_t **blocks;
  111. // some other test state
  112. lfs_emubd_io_t readed;
  113. lfs_emubd_io_t proged;
  114. lfs_emubd_io_t erased;
  115. lfs_emubd_powercycles_t power_cycles;
  116. lfs_emubd_disk_t *disk;
  117. const struct lfs_emubd_config *cfg;
  118. } lfs_emubd_t;
  119. /// Block device API ///
  120. // Create an emulating block device using the geometry in lfs_config
  121. //
  122. // Note that filebd is used if a path is provided, if path is NULL
  123. // emubd will use rambd which can be much faster.
  124. int lfs_emubd_create(const struct lfs_config *cfg, const char *path);
  125. int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
  126. const struct lfs_emubd_config *bdcfg);
  127. // Clean up memory associated with block device
  128. int lfs_emubd_destroy(const struct lfs_config *cfg);
  129. // Read a block
  130. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  131. lfs_off_t off, void *buffer, lfs_size_t size);
  132. // Program a block
  133. //
  134. // The block must have previously been erased.
  135. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  136. lfs_off_t off, const void *buffer, lfs_size_t size);
  137. // Erase a block
  138. //
  139. // A block must be erased before being programmed. The
  140. // state of an erased block is undefined.
  141. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block);
  142. // Sync the block device
  143. int lfs_emubd_sync(const struct lfs_config *cfg);
  144. /// Additional extended API for driving test features ///
  145. // Get total amount of bytes read
  146. lfs_emubd_sio_t lfs_emubd_getreaded(const struct lfs_config *cfg);
  147. // Get total amount of bytes programmed
  148. lfs_emubd_sio_t lfs_emubd_getproged(const struct lfs_config *cfg);
  149. // Get total amount of bytes erased
  150. lfs_emubd_sio_t lfs_emubd_geterased(const struct lfs_config *cfg);
  151. // Manually set amount of bytes read
  152. int lfs_emubd_setreaded(const struct lfs_config *cfg, lfs_emubd_io_t readed);
  153. // Manually set amount of bytes programmed
  154. int lfs_emubd_setproged(const struct lfs_config *cfg, lfs_emubd_io_t proged);
  155. // Manually set amount of bytes erased
  156. int lfs_emubd_seterased(const struct lfs_config *cfg, lfs_emubd_io_t erased);
  157. // Get simulated wear on a given block
  158. lfs_emubd_swear_t lfs_emubd_getwear(const struct lfs_config *cfg,
  159. lfs_block_t block);
  160. // Manually set simulated wear on a given block
  161. int lfs_emubd_setwear(const struct lfs_config *cfg,
  162. lfs_block_t block, lfs_emubd_wear_t wear);
  163. // Get the remaining power-cycles
  164. lfs_emubd_spowercycles_t lfs_emubd_getpowercycles(
  165. const struct lfs_config *cfg);
  166. // Manually set the remaining power-cycles
  167. int lfs_emubd_setpowercycles(const struct lfs_config *cfg,
  168. lfs_emubd_powercycles_t power_cycles);
  169. // Create a copy-on-write copy of the state of this block device
  170. int lfs_emubd_copy(const struct lfs_config *cfg, lfs_emubd_t *copy);
  171. #ifdef __cplusplus
  172. } /* extern "C" */
  173. #endif
  174. #endif