lfs_testbd.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // testbd config, this is required for testing
  52. struct lfs_testbd_config {
  53. // 8-bit erase value to use for simulating erases. -1 does not simulate
  54. // erases, which can speed up testing by avoiding the extra block-device
  55. // operations to store the erase value.
  56. int32_t erase_value;
  57. // Number of erase cycles before a block becomes "bad". The exact behavior
  58. // of bad blocks is controlled by badblock_behavior.
  59. uint32_t erase_cycles;
  60. // The mode determining how bad-blocks fail
  61. lfs_testbd_badblock_behavior_t badblock_behavior;
  62. // Number of write operations (erase/prog) before triggering a power-loss.
  63. // power_cycles=0 disables this. The exact behavior of power-loss is
  64. // controlled by a combination of powerloss_behavior and powerloss_cb.
  65. lfs_testbd_powercycles_t power_cycles;
  66. // The mode determining how power-loss affects disk
  67. lfs_testbd_powerloss_behavior_t powerloss_behavior;
  68. // Function to call to emulate power-loss. The exact behavior of power-loss
  69. // is up to the runner to provide.
  70. void (*powerloss_cb)(void*);
  71. // Data for power-loss callback
  72. void *powerloss_data;
  73. // True to track when power-loss could have occured. Note this involves
  74. // heavy memory usage!
  75. bool track_branches;
  76. // // Optional buffer for RAM block device.
  77. // void *buffer;
  78. //
  79. // // Optional buffer for wear.
  80. // void *wear_buffer;
  81. //
  82. // // Optional buffer for scratch memory, needed when erase_value != -1.
  83. // void *scratch_buffer;
  84. };
  85. // A reference counted block
  86. typedef struct lfs_testbd_block {
  87. uint32_t rc;
  88. lfs_testbd_wear_t wear;
  89. uint8_t data[];
  90. } lfs_testbd_block_t;
  91. // testbd state
  92. typedef struct lfs_testbd {
  93. // array of copy-on-write blocks
  94. lfs_testbd_block_t **blocks;
  95. uint32_t power_cycles;
  96. // array of tracked branches
  97. struct lfs_testbd *branches;
  98. lfs_testbd_powercycles_t branch_count;
  99. lfs_testbd_powercycles_t branch_capacity;
  100. // TODO file?
  101. // union {
  102. // struct {
  103. // lfs_filebd_t bd;
  104. // } file;
  105. // struct {
  106. // lfs_rambd_t bd;
  107. // struct lfs_rambd_config cfg;
  108. // } ram;
  109. // } u;
  110. //
  111. // bool persist;
  112. // uint32_t power_cycles;
  113. // lfs_testbd_wear_t *wear;
  114. // uint8_t *scratch;
  115. const struct lfs_testbd_config *cfg;
  116. } lfs_testbd_t;
  117. /// Block device API ///
  118. // Create a test block device using the geometry in lfs_config
  119. //
  120. // Note that filebd is used if a path is provided, if path is NULL
  121. // testbd will use rambd which can be much faster.
  122. int lfs_testbd_create(const struct lfs_config *cfg, const char *path);
  123. int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
  124. const struct lfs_testbd_config *bdcfg);
  125. // Clean up memory associated with block device
  126. int lfs_testbd_destroy(const struct lfs_config *cfg);
  127. // Read a block
  128. int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
  129. lfs_off_t off, void *buffer, lfs_size_t size);
  130. // Program a block
  131. //
  132. // The block must have previously been erased.
  133. int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
  134. lfs_off_t off, const void *buffer, lfs_size_t size);
  135. // Erase a block
  136. //
  137. // A block must be erased before being programmed. The
  138. // state of an erased block is undefined.
  139. int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block);
  140. // Sync the block device
  141. int lfs_testbd_sync(const struct lfs_config *cfg);
  142. /// Additional extended API for driving test features ///
  143. // Get simulated wear on a given block
  144. lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
  145. lfs_block_t block);
  146. // Manually set simulated wear on a given block
  147. int lfs_testbd_setwear(const struct lfs_config *cfg,
  148. lfs_block_t block, lfs_testbd_wear_t wear);
  149. // Get the remaining power-cycles
  150. lfs_testbd_spowercycles_t lfs_testbd_getpowercycles(
  151. const struct lfs_config *cfg);
  152. // Manually set the remaining power-cycles
  153. int lfs_testbd_setpowercycles(const struct lfs_config *cfg,
  154. lfs_testbd_powercycles_t power_cycles);
  155. // Get a power-loss branch, requires track_branches=true
  156. int lfs_testbd_getbranch(const struct lfs_config *cfg,
  157. lfs_testbd_powercycles_t branch, lfs_testbd_t *bd);
  158. // Get the current number of power-loss branches
  159. lfs_testbd_spowercycles_t lfs_testbd_getbranchcount(
  160. const struct lfs_config *cfg);
  161. #ifdef __cplusplus
  162. } /* extern "C" */
  163. #endif
  164. #endif