lfs_testbd.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // Path to file to use as a mirror of the disk. This provides a way to view
  77. // the current state of the block device.
  78. const char *disk_path;
  79. };
  80. // A reference counted block
  81. typedef struct lfs_testbd_block {
  82. uint32_t rc;
  83. lfs_testbd_wear_t wear;
  84. uint8_t data[];
  85. } lfs_testbd_block_t;
  86. // testbd state
  87. typedef struct lfs_testbd {
  88. // array of copy-on-write blocks
  89. lfs_testbd_block_t **blocks;
  90. // some other test state
  91. uint32_t power_cycles;
  92. int disk_fd;
  93. uint8_t *disk_scratch_block;
  94. // array of tracked branches
  95. struct lfs_testbd *branches;
  96. lfs_testbd_powercycles_t branch_count;
  97. lfs_testbd_powercycles_t branch_capacity;
  98. // TODO file?
  99. // union {
  100. // struct {
  101. // lfs_filebd_t bd;
  102. // } file;
  103. // struct {
  104. // lfs_rambd_t bd;
  105. // struct lfs_rambd_config cfg;
  106. // } ram;
  107. // } u;
  108. //
  109. // bool persist;
  110. // uint32_t power_cycles;
  111. // lfs_testbd_wear_t *wear;
  112. // uint8_t *scratch;
  113. const struct lfs_testbd_config *cfg;
  114. } lfs_testbd_t;
  115. /// Block device API ///
  116. // Create a test block device using the geometry in lfs_config
  117. //
  118. // Note that filebd is used if a path is provided, if path is NULL
  119. // testbd will use rambd which can be much faster.
  120. int lfs_testbd_create(const struct lfs_config *cfg, const char *path);
  121. int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
  122. const struct lfs_testbd_config *bdcfg);
  123. // Clean up memory associated with block device
  124. int lfs_testbd_destroy(const struct lfs_config *cfg);
  125. // Read a block
  126. int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
  127. lfs_off_t off, void *buffer, lfs_size_t size);
  128. // Program a block
  129. //
  130. // The block must have previously been erased.
  131. int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
  132. lfs_off_t off, const void *buffer, lfs_size_t size);
  133. // Erase a block
  134. //
  135. // A block must be erased before being programmed. The
  136. // state of an erased block is undefined.
  137. int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block);
  138. // Sync the block device
  139. int lfs_testbd_sync(const struct lfs_config *cfg);
  140. /// Additional extended API for driving test features ///
  141. // Get simulated wear on a given block
  142. lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
  143. lfs_block_t block);
  144. // Manually set simulated wear on a given block
  145. int lfs_testbd_setwear(const struct lfs_config *cfg,
  146. lfs_block_t block, lfs_testbd_wear_t wear);
  147. // Get the remaining power-cycles
  148. lfs_testbd_spowercycles_t lfs_testbd_getpowercycles(
  149. const struct lfs_config *cfg);
  150. // Manually set the remaining power-cycles
  151. int lfs_testbd_setpowercycles(const struct lfs_config *cfg,
  152. lfs_testbd_powercycles_t power_cycles);
  153. // Get a power-loss branch, requires track_branches=true
  154. int lfs_testbd_getbranch(const struct lfs_config *cfg,
  155. lfs_testbd_powercycles_t branch, lfs_testbd_t *bd);
  156. // Get the current number of power-loss branches
  157. lfs_testbd_spowercycles_t lfs_testbd_getbranchcount(
  158. const struct lfs_config *cfg);
  159. #ifdef __cplusplus
  160. } /* extern "C" */
  161. #endif
  162. #endif