test_orphans.toml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. [cases.test_orphans_normal]
  2. in = "lfs.c"
  3. if = 'PROG_SIZE <= 0x3fe' # only works with one crc per commit
  4. code = '''
  5. lfs_t lfs;
  6. lfs_format(&lfs, cfg) => 0;
  7. lfs_mount(&lfs, cfg) => 0;
  8. lfs_mkdir(&lfs, "parent") => 0;
  9. lfs_mkdir(&lfs, "parent/orphan") => 0;
  10. lfs_mkdir(&lfs, "parent/child") => 0;
  11. lfs_remove(&lfs, "parent/orphan") => 0;
  12. lfs_unmount(&lfs) => 0;
  13. // corrupt the child's most recent commit, this should be the update
  14. // to the linked-list entry, which should orphan the orphan. Note this
  15. // makes a lot of assumptions about the remove operation.
  16. lfs_mount(&lfs, cfg) => 0;
  17. lfs_dir_t dir;
  18. lfs_dir_open(&lfs, &dir, "parent/child") => 0;
  19. lfs_block_t block = dir.m.pair[0];
  20. lfs_dir_close(&lfs, &dir) => 0;
  21. lfs_unmount(&lfs) => 0;
  22. uint8_t buffer[BLOCK_SIZE];
  23. cfg->read(cfg, block, 0, buffer, BLOCK_SIZE) => 0;
  24. int off = BLOCK_SIZE-1;
  25. while (off >= 0 && buffer[off] == ERASE_VALUE) {
  26. off -= 1;
  27. }
  28. memset(&buffer[off-3], BLOCK_SIZE, 3);
  29. cfg->erase(cfg, block) => 0;
  30. cfg->prog(cfg, block, 0, buffer, BLOCK_SIZE) => 0;
  31. cfg->sync(cfg) => 0;
  32. lfs_mount(&lfs, cfg) => 0;
  33. struct lfs_info info;
  34. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  35. lfs_stat(&lfs, "parent/child", &info) => 0;
  36. lfs_fs_size(&lfs) => 8;
  37. lfs_unmount(&lfs) => 0;
  38. lfs_mount(&lfs, cfg) => 0;
  39. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  40. lfs_stat(&lfs, "parent/child", &info) => 0;
  41. lfs_fs_size(&lfs) => 8;
  42. // this mkdir should both create a dir and deorphan, so size
  43. // should be unchanged
  44. lfs_mkdir(&lfs, "parent/otherchild") => 0;
  45. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  46. lfs_stat(&lfs, "parent/child", &info) => 0;
  47. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  48. lfs_fs_size(&lfs) => 8;
  49. lfs_unmount(&lfs) => 0;
  50. lfs_mount(&lfs, cfg) => 0;
  51. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  52. lfs_stat(&lfs, "parent/child", &info) => 0;
  53. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  54. lfs_fs_size(&lfs) => 8;
  55. lfs_unmount(&lfs) => 0;
  56. '''
  57. # test that we only run deorphan once per power-cycle
  58. [cases.test_orphans_no_orphans]
  59. in = 'lfs.c'
  60. code = '''
  61. lfs_t lfs;
  62. lfs_format(&lfs, cfg) => 0;
  63. lfs_mount(&lfs, cfg) => 0;
  64. // mark the filesystem as having orphans
  65. lfs_fs_preporphans(&lfs, +1) => 0;
  66. lfs_mdir_t mdir;
  67. lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
  68. lfs_dir_commit(&lfs, &mdir, NULL, 0) => 0;
  69. // we should have orphans at this state
  70. assert(lfs_gstate_hasorphans(&lfs.gstate));
  71. lfs_unmount(&lfs) => 0;
  72. // mount
  73. lfs_mount(&lfs, cfg) => 0;
  74. // we should detect orphans
  75. assert(lfs_gstate_hasorphans(&lfs.gstate));
  76. // force consistency
  77. lfs_fs_forceconsistency(&lfs) => 0;
  78. // we should no longer have orphans
  79. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  80. lfs_unmount(&lfs) => 0;
  81. '''
  82. [cases.test_orphans_one_orphan]
  83. in = 'lfs.c'
  84. code = '''
  85. lfs_t lfs;
  86. lfs_format(&lfs, cfg) => 0;
  87. lfs_mount(&lfs, cfg) => 0;
  88. // create an orphan
  89. lfs_mdir_t orphan;
  90. lfs_alloc_ckpoint(&lfs);
  91. lfs_dir_alloc(&lfs, &orphan) => 0;
  92. lfs_dir_commit(&lfs, &orphan, NULL, 0) => 0;
  93. // append our orphan and mark the filesystem as having orphans
  94. lfs_fs_preporphans(&lfs, +1) => 0;
  95. lfs_mdir_t mdir;
  96. lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
  97. lfs_pair_tole32(orphan.pair);
  98. lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
  99. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), orphan.pair})) => 0;
  100. // we should have orphans at this state
  101. assert(lfs_gstate_hasorphans(&lfs.gstate));
  102. lfs_unmount(&lfs) => 0;
  103. // mount
  104. lfs_mount(&lfs, cfg) => 0;
  105. // we should detect orphans
  106. assert(lfs_gstate_hasorphans(&lfs.gstate));
  107. // force consistency
  108. lfs_fs_forceconsistency(&lfs) => 0;
  109. // we should no longer have orphans
  110. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  111. lfs_unmount(&lfs) => 0;
  112. '''
  113. # test that we can persist gstate with lfs_fs_mkconsistent
  114. [cases.test_orphans_mkconsistent_no_orphans]
  115. in = 'lfs.c'
  116. code = '''
  117. lfs_t lfs;
  118. lfs_format(&lfs, cfg) => 0;
  119. lfs_mount(&lfs, cfg) => 0;
  120. // mark the filesystem as having orphans
  121. lfs_fs_preporphans(&lfs, +1) => 0;
  122. lfs_mdir_t mdir;
  123. lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
  124. lfs_dir_commit(&lfs, &mdir, NULL, 0) => 0;
  125. // we should have orphans at this state
  126. assert(lfs_gstate_hasorphans(&lfs.gstate));
  127. lfs_unmount(&lfs) => 0;
  128. // mount
  129. lfs_mount(&lfs, cfg) => 0;
  130. // we should detect orphans
  131. assert(lfs_gstate_hasorphans(&lfs.gstate));
  132. // force consistency
  133. lfs_fs_mkconsistent(&lfs) => 0;
  134. // we should no longer have orphans
  135. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  136. // remount
  137. lfs_unmount(&lfs) => 0;
  138. lfs_mount(&lfs, cfg) => 0;
  139. // we should still have no orphans
  140. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  141. lfs_unmount(&lfs) => 0;
  142. '''
  143. [cases.test_orphans_mkconsistent_one_orphan]
  144. in = 'lfs.c'
  145. code = '''
  146. lfs_t lfs;
  147. lfs_format(&lfs, cfg) => 0;
  148. lfs_mount(&lfs, cfg) => 0;
  149. // create an orphan
  150. lfs_mdir_t orphan;
  151. lfs_alloc_ckpoint(&lfs);
  152. lfs_dir_alloc(&lfs, &orphan) => 0;
  153. lfs_dir_commit(&lfs, &orphan, NULL, 0) => 0;
  154. // append our orphan and mark the filesystem as having orphans
  155. lfs_fs_preporphans(&lfs, +1) => 0;
  156. lfs_mdir_t mdir;
  157. lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
  158. lfs_pair_tole32(orphan.pair);
  159. lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
  160. {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), orphan.pair})) => 0;
  161. // we should have orphans at this state
  162. assert(lfs_gstate_hasorphans(&lfs.gstate));
  163. lfs_unmount(&lfs) => 0;
  164. // mount
  165. lfs_mount(&lfs, cfg) => 0;
  166. // we should detect orphans
  167. assert(lfs_gstate_hasorphans(&lfs.gstate));
  168. // force consistency
  169. lfs_fs_mkconsistent(&lfs) => 0;
  170. // we should no longer have orphans
  171. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  172. // remount
  173. lfs_unmount(&lfs) => 0;
  174. lfs_mount(&lfs, cfg) => 0;
  175. // we should still have no orphans
  176. assert(!lfs_gstate_hasorphans(&lfs.gstate));
  177. lfs_unmount(&lfs) => 0;
  178. '''
  179. # reentrant testing for orphans, basically just spam mkdir/remove
  180. [cases.test_orphans_reentrant]
  181. reentrant = true
  182. # TODO fix this case, caused by non-DAG trees
  183. # NOTE the second condition is required
  184. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  185. defines = [
  186. {FILES=6, DEPTH=1, CYCLES=20},
  187. {FILES=26, DEPTH=1, CYCLES=20},
  188. {FILES=3, DEPTH=3, CYCLES=20},
  189. ]
  190. code = '''
  191. lfs_t lfs;
  192. int err = lfs_mount(&lfs, cfg);
  193. if (err) {
  194. lfs_format(&lfs, cfg) => 0;
  195. lfs_mount(&lfs, cfg) => 0;
  196. }
  197. uint32_t prng = 1;
  198. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  199. for (unsigned i = 0; i < CYCLES; i++) {
  200. // create random path
  201. char full_path[256];
  202. for (unsigned d = 0; d < DEPTH; d++) {
  203. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  204. }
  205. // if it does not exist, we create it, else we destroy
  206. struct lfs_info info;
  207. int res = lfs_stat(&lfs, full_path, &info);
  208. if (res == LFS_ERR_NOENT) {
  209. // create each directory in turn, ignore if dir already exists
  210. for (unsigned d = 0; d < DEPTH; d++) {
  211. char path[1024];
  212. strcpy(path, full_path);
  213. path[2*d+2] = '\0';
  214. err = lfs_mkdir(&lfs, path);
  215. assert(!err || err == LFS_ERR_EXIST);
  216. }
  217. for (unsigned d = 0; d < DEPTH; d++) {
  218. char path[1024];
  219. strcpy(path, full_path);
  220. path[2*d+2] = '\0';
  221. lfs_stat(&lfs, path, &info) => 0;
  222. assert(strcmp(info.name, &path[2*d+1]) == 0);
  223. assert(info.type == LFS_TYPE_DIR);
  224. }
  225. } else {
  226. // is valid dir?
  227. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  228. assert(info.type == LFS_TYPE_DIR);
  229. // try to delete path in reverse order, ignore if dir is not empty
  230. for (int d = DEPTH-1; d >= 0; d--) {
  231. char path[1024];
  232. strcpy(path, full_path);
  233. path[2*d+2] = '\0';
  234. err = lfs_remove(&lfs, path);
  235. assert(!err || err == LFS_ERR_NOTEMPTY);
  236. }
  237. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  238. }
  239. }
  240. lfs_unmount(&lfs) => 0;
  241. '''
  242. # non-reentrant testing for orphans, this is the same as reentrant
  243. # testing, but we test way more states than we could under powerloss
  244. [cases.test_orphans_nonreentrant]
  245. # TODO fix this case, caused by non-DAG trees
  246. # NOTE the second condition is required
  247. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  248. defines = [
  249. {FILES=6, DEPTH=1, CYCLES=2000},
  250. {FILES=26, DEPTH=1, CYCLES=2000},
  251. {FILES=3, DEPTH=3, CYCLES=2000},
  252. ]
  253. code = '''
  254. lfs_t lfs;
  255. lfs_format(&lfs, cfg) => 0;
  256. lfs_mount(&lfs, cfg) => 0;
  257. uint32_t prng = 1;
  258. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  259. for (unsigned i = 0; i < CYCLES; i++) {
  260. // create random path
  261. char full_path[256];
  262. for (unsigned d = 0; d < DEPTH; d++) {
  263. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  264. }
  265. // if it does not exist, we create it, else we destroy
  266. struct lfs_info info;
  267. int res = lfs_stat(&lfs, full_path, &info);
  268. if (res == LFS_ERR_NOENT) {
  269. // create each directory in turn, ignore if dir already exists
  270. for (unsigned d = 0; d < DEPTH; d++) {
  271. char path[1024];
  272. strcpy(path, full_path);
  273. path[2*d+2] = '\0';
  274. int err = lfs_mkdir(&lfs, path);
  275. assert(!err || err == LFS_ERR_EXIST);
  276. }
  277. for (unsigned d = 0; d < DEPTH; d++) {
  278. char path[1024];
  279. strcpy(path, full_path);
  280. path[2*d+2] = '\0';
  281. lfs_stat(&lfs, path, &info) => 0;
  282. assert(strcmp(info.name, &path[2*d+1]) == 0);
  283. assert(info.type == LFS_TYPE_DIR);
  284. }
  285. } else {
  286. // is valid dir?
  287. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  288. assert(info.type == LFS_TYPE_DIR);
  289. // try to delete path in reverse order, ignore if dir is not empty
  290. for (int d = DEPTH-1; d >= 0; d--) {
  291. char path[1024];
  292. strcpy(path, full_path);
  293. path[2*d+2] = '\0';
  294. int err = lfs_remove(&lfs, path);
  295. assert(!err || err == LFS_ERR_NOTEMPTY);
  296. }
  297. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  298. }
  299. }
  300. lfs_unmount(&lfs) => 0;
  301. '''