test_orphans.toml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_ack(&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. # reentrant testing for orphans, basically just spam mkdir/remove
  114. [cases.test_orphans_reentrant]
  115. reentrant = true
  116. # TODO fix this case, caused by non-DAG trees
  117. if = '!(DEPTH == 3 && CACHE_SIZE != 64)'
  118. defines = [
  119. {FILES=6, DEPTH=1, CYCLES=20},
  120. {FILES=26, DEPTH=1, CYCLES=20},
  121. {FILES=3, DEPTH=3, CYCLES=20},
  122. ]
  123. code = '''
  124. lfs_t lfs;
  125. int err = lfs_mount(&lfs, cfg);
  126. if (err) {
  127. lfs_format(&lfs, cfg) => 0;
  128. lfs_mount(&lfs, cfg) => 0;
  129. }
  130. uint32_t prng = 1;
  131. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  132. for (unsigned i = 0; i < CYCLES; i++) {
  133. // create random path
  134. char full_path[256];
  135. for (unsigned d = 0; d < DEPTH; d++) {
  136. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  137. }
  138. // if it does not exist, we create it, else we destroy
  139. struct lfs_info info;
  140. int res = lfs_stat(&lfs, full_path, &info);
  141. if (res == LFS_ERR_NOENT) {
  142. // create each directory in turn, ignore if dir already exists
  143. for (unsigned d = 0; d < DEPTH; d++) {
  144. char path[1024];
  145. strcpy(path, full_path);
  146. path[2*d+2] = '\0';
  147. err = lfs_mkdir(&lfs, path);
  148. assert(!err || err == LFS_ERR_EXIST);
  149. }
  150. for (unsigned d = 0; d < DEPTH; d++) {
  151. char path[1024];
  152. strcpy(path, full_path);
  153. path[2*d+2] = '\0';
  154. lfs_stat(&lfs, path, &info) => 0;
  155. assert(strcmp(info.name, &path[2*d+1]) == 0);
  156. assert(info.type == LFS_TYPE_DIR);
  157. }
  158. } else {
  159. // is valid dir?
  160. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  161. assert(info.type == LFS_TYPE_DIR);
  162. // try to delete path in reverse order, ignore if dir is not empty
  163. for (int d = DEPTH-1; d >= 0; d--) {
  164. char path[1024];
  165. strcpy(path, full_path);
  166. path[2*d+2] = '\0';
  167. err = lfs_remove(&lfs, path);
  168. assert(!err || err == LFS_ERR_NOTEMPTY);
  169. }
  170. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  171. }
  172. }
  173. lfs_unmount(&lfs) => 0;
  174. '''