lfs_testbd.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // Disk mirror
  99. typedef struct lfs_testbd_disk {
  100. uint32_t rc;
  101. int fd;
  102. uint8_t *scratch;
  103. } lfs_testbd_disk_t;
  104. // testbd state
  105. typedef struct lfs_testbd {
  106. // array of copy-on-write blocks
  107. lfs_testbd_block_t **blocks;
  108. // some other test state
  109. uint32_t power_cycles;
  110. lfs_testbd_disk_t *disk;
  111. const struct lfs_testbd_config *cfg;
  112. } lfs_testbd_t;
  113. /// Block device API ///
  114. // Create a test block device using the geometry in lfs_config
  115. //
  116. // Note that filebd is used if a path is provided, if path is NULL
  117. // testbd will use rambd which can be much faster.
  118. int lfs_testbd_create(const struct lfs_config *cfg, const char *path);
  119. int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
  120. const struct lfs_testbd_config *bdcfg);
  121. // Clean up memory associated with block device
  122. int lfs_testbd_destroy(const struct lfs_config *cfg);
  123. // Read a block
  124. int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
  125. lfs_off_t off, void *buffer, lfs_size_t size);
  126. // Program a block
  127. //
  128. // The block must have previously been erased.
  129. int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
  130. lfs_off_t off, const void *buffer, lfs_size_t size);
  131. // Erase a block
  132. //
  133. // A block must be erased before being programmed. The
  134. // state of an erased block is undefined.
  135. int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block);
  136. // Sync the block device
  137. int lfs_testbd_sync(const struct lfs_config *cfg);
  138. /// Additional extended API for driving test features ///
  139. // Get simulated wear on a given block
  140. lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
  141. lfs_block_t block);
  142. // Manually set simulated wear on a given block
  143. int lfs_testbd_setwear(const struct lfs_config *cfg,
  144. lfs_block_t block, lfs_testbd_wear_t wear);
  145. // Get the remaining power-cycles
  146. lfs_testbd_spowercycles_t lfs_testbd_getpowercycles(
  147. const struct lfs_config *cfg);
  148. // Manually set the remaining power-cycles
  149. int lfs_testbd_setpowercycles(const struct lfs_config *cfg,
  150. lfs_testbd_powercycles_t power_cycles);
  151. // Create a copy-on-write copy of the state of this block device
  152. int lfs_testbd_copy(const struct lfs_config *cfg, lfs_testbd_t *copy);
  153. #ifdef __cplusplus
  154. } /* extern "C" */
  155. #endif
  156. #endif