lfs_testbd.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Testing 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_TESTBD_H
  10. #define LFS_TESTBD_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_TESTBD_TRACE
  21. #ifdef LFS_TESTBD_YES_TRACE
  22. #define LFS_TESTBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  23. #else
  24. #define LFS_TESTBD_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_testbd_badblock_behavior {
  34. LFS_TESTBD_BADBLOCK_PROGERROR,
  35. LFS_TESTBD_BADBLOCK_ERASEERROR,
  36. LFS_TESTBD_BADBLOCK_READERROR,
  37. LFS_TESTBD_BADBLOCK_PROGNOOP,
  38. LFS_TESTBD_BADBLOCK_ERASENOOP,
  39. } lfs_testbd_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_testbd_powerloss_behavior {
  43. LFS_TESTBD_POWERLOSS_NOOP,
  44. } lfs_testbd_powerloss_behavior_t;
  45. // Type for measuring wear
  46. typedef uint32_t lfs_testbd_wear_t;
  47. typedef int32_t lfs_testbd_swear_t;
  48. // Type for tracking power-cycles
  49. typedef uint32_t lfs_testbd_powercycles_t;
  50. typedef int32_t lfs_testbd_spowercycles_t;
  51. // Type for delays in nanoseconds
  52. typedef uint64_t lfs_testbd_delay_t;
  53. typedef int64_t lfs_testbd_sdelay_t;
  54. // testbd config, this is required for testing
  55. struct lfs_testbd_config {
  56. // 8-bit erase value to use for simulating erases. -1 does not simulate
  57. // erases, which can speed up testing by avoiding the extra block-device
  58. // operations to store the erase value.
  59. int32_t erase_value;
  60. // Number of erase cycles before a block becomes "bad". The exact behavior
  61. // of bad blocks is controlled by badblock_behavior.
  62. uint32_t erase_cycles;
  63. // The mode determining how bad-blocks fail
  64. lfs_testbd_badblock_behavior_t badblock_behavior;
  65. // Number of write operations (erase/prog) before triggering a power-loss.
  66. // power_cycles=0 disables this. The exact behavior of power-loss is
  67. // controlled by a combination of powerloss_behavior and powerloss_cb.
  68. lfs_testbd_powercycles_t power_cycles;
  69. // The mode determining how power-loss affects disk
  70. lfs_testbd_powerloss_behavior_t powerloss_behavior;
  71. // Function to call to emulate power-loss. The exact behavior of power-loss
  72. // is up to the runner to provide.
  73. void (*powerloss_cb)(void*);
  74. // Data for power-loss callback
  75. void *powerloss_data;
  76. // True to track when power-loss could have occured. Note this involves
  77. // heavy memory usage!
  78. bool track_branches;
  79. // Path to file to use as a mirror of the disk. This provides a way to view
  80. // the current state of the block device.
  81. const char *disk_path;
  82. // Artificial delay in nanoseconds, there is no purpose for this other
  83. // than slowing down the simulation.
  84. lfs_testbd_delay_t read_delay;
  85. // Artificial delay in nanoseconds, there is no purpose for this other
  86. // than slowing down the simulation.
  87. lfs_testbd_delay_t prog_delay;
  88. // Artificial delay in nanoseconds, there is no purpose for this other
  89. // than slowing down the simulation.
  90. lfs_testbd_delay_t erase_delay;
  91. };
  92. // A reference counted block
  93. typedef struct lfs_testbd_block {
  94. uint32_t rc;
  95. lfs_testbd_wear_t wear;
  96. uint8_t data[];
  97. } lfs_testbd_block_t;
  98. // testbd state
  99. typedef struct lfs_testbd {
  100. // array of copy-on-write blocks
  101. lfs_testbd_block_t **blocks;
  102. // some other test state
  103. uint32_t power_cycles;
  104. int disk_fd;
  105. uint8_t *disk_scratch_block;
  106. // array of tracked branches
  107. struct lfs_testbd *branches;
  108. lfs_testbd_powercycles_t branch_count;
  109. lfs_testbd_powercycles_t branch_capacity;
  110. // TODO file?
  111. // union {
  112. // struct {
  113. // lfs_filebd_t bd;
  114. // } file;
  115. // struct {
  116. // lfs_rambd_t bd;
  117. // struct lfs_rambd_config cfg;
  118. // } ram;
  119. // } u;
  120. //
  121. // bool persist;
  122. // uint32_t power_cycles;
  123. // lfs_testbd_wear_t *wear;
  124. // uint8_t *scratch;
  125. const struct lfs_testbd_config *cfg;
  126. } lfs_testbd_t;
  127. /// Block device API ///
  128. // Create a test block device using the geometry in lfs_config
  129. //
  130. // Note that filebd is used if a path is provided, if path is NULL
  131. // testbd will use rambd which can be much faster.
  132. int lfs_testbd_create(const struct lfs_config *cfg, const char *path);
  133. int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
  134. const struct lfs_testbd_config *bdcfg);
  135. // Clean up memory associated with block device
  136. int lfs_testbd_destroy(const struct lfs_config *cfg);
  137. // Read a block
  138. int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
  139. lfs_off_t off, void *buffer, lfs_size_t size);
  140. // Program a block
  141. //
  142. // The block must have previously been erased.
  143. int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
  144. lfs_off_t off, const void *buffer, lfs_size_t size);
  145. // Erase a block
  146. //
  147. // A block must be erased before being programmed. The
  148. // state of an erased block is undefined.
  149. int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block);
  150. // Sync the block device
  151. int lfs_testbd_sync(const struct lfs_config *cfg);
  152. /// Additional extended API for driving test features ///
  153. // Get simulated wear on a given block
  154. lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
  155. lfs_block_t block);
  156. // Manually set simulated wear on a given block
  157. int lfs_testbd_setwear(const struct lfs_config *cfg,
  158. lfs_block_t block, lfs_testbd_wear_t wear);
  159. // Get the remaining power-cycles
  160. lfs_testbd_spowercycles_t lfs_testbd_getpowercycles(
  161. const struct lfs_config *cfg);
  162. // Manually set the remaining power-cycles
  163. int lfs_testbd_setpowercycles(const struct lfs_config *cfg,
  164. lfs_testbd_powercycles_t power_cycles);
  165. // Get a power-loss branch, requires track_branches=true
  166. int lfs_testbd_getbranch(const struct lfs_config *cfg,
  167. lfs_testbd_powercycles_t branch, lfs_testbd_t *bd);
  168. // Get the current number of power-loss branches
  169. lfs_testbd_spowercycles_t lfs_testbd_getbranchcount(
  170. const struct lfs_config *cfg);
  171. #ifdef __cplusplus
  172. } /* extern "C" */
  173. #endif
  174. #endif