test_alloc.toml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. # allocator tests
  2. # note for these to work there are a number constraints on the device geometry
  3. if = 'BLOCK_CYCLES == -1'
  4. # parallel allocation test
  5. [cases.parallel_allocation]
  6. defines.FILES = 3
  7. defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
  8. code = '''
  9. const char *names[] = {"bacon", "eggs", "pancakes"};
  10. lfs_file_t files[FILES];
  11. lfs_t lfs;
  12. lfs_format(&lfs, cfg) => 0;
  13. lfs_mount(&lfs, cfg) => 0;
  14. lfs_mkdir(&lfs, "breakfast") => 0;
  15. lfs_unmount(&lfs) => 0;
  16. lfs_mount(&lfs, cfg) => 0;
  17. for (int n = 0; n < FILES; n++) {
  18. char path[1024];
  19. sprintf(path, "breakfast/%s", names[n]);
  20. lfs_file_open(&lfs, &files[n], path,
  21. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
  22. }
  23. for (int n = 0; n < FILES; n++) {
  24. size_t size = strlen(names[n]);
  25. for (lfs_size_t i = 0; i < SIZE; i += size) {
  26. lfs_file_write(&lfs, &files[n], names[n], size) => size;
  27. }
  28. }
  29. for (int n = 0; n < FILES; n++) {
  30. lfs_file_close(&lfs, &files[n]) => 0;
  31. }
  32. lfs_unmount(&lfs) => 0;
  33. lfs_mount(&lfs, cfg) => 0;
  34. for (int n = 0; n < FILES; n++) {
  35. char path[1024];
  36. sprintf(path, "breakfast/%s", names[n]);
  37. lfs_file_t file;
  38. lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
  39. size_t size = strlen(names[n]);
  40. for (lfs_size_t i = 0; i < SIZE; i += size) {
  41. uint8_t buffer[1024];
  42. lfs_file_read(&lfs, &file, buffer, size) => size;
  43. assert(memcmp(buffer, names[n], size) == 0);
  44. }
  45. lfs_file_close(&lfs, &file) => 0;
  46. }
  47. lfs_unmount(&lfs) => 0;
  48. '''
  49. # serial allocation test
  50. [cases.serial_allocation]
  51. defines.FILES = 3
  52. defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
  53. code = '''
  54. const char *names[] = {"bacon", "eggs", "pancakes"};
  55. lfs_t lfs;
  56. lfs_format(&lfs, cfg) => 0;
  57. lfs_mount(&lfs, cfg) => 0;
  58. lfs_mkdir(&lfs, "breakfast") => 0;
  59. lfs_unmount(&lfs) => 0;
  60. for (int n = 0; n < FILES; n++) {
  61. lfs_mount(&lfs, cfg) => 0;
  62. char path[1024];
  63. sprintf(path, "breakfast/%s", names[n]);
  64. lfs_file_t file;
  65. lfs_file_open(&lfs, &file, path,
  66. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
  67. size_t size = strlen(names[n]);
  68. uint8_t buffer[1024];
  69. memcpy(buffer, names[n], size);
  70. for (int i = 0; i < SIZE; i += size) {
  71. lfs_file_write(&lfs, &file, buffer, size) => size;
  72. }
  73. lfs_file_close(&lfs, &file) => 0;
  74. lfs_unmount(&lfs) => 0;
  75. }
  76. lfs_mount(&lfs, cfg) => 0;
  77. for (int n = 0; n < FILES; n++) {
  78. char path[1024];
  79. sprintf(path, "breakfast/%s", names[n]);
  80. lfs_file_t file;
  81. lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
  82. size_t size = strlen(names[n]);
  83. for (int i = 0; i < SIZE; i += size) {
  84. uint8_t buffer[1024];
  85. lfs_file_read(&lfs, &file, buffer, size) => size;
  86. assert(memcmp(buffer, names[n], size) == 0);
  87. }
  88. lfs_file_close(&lfs, &file) => 0;
  89. }
  90. lfs_unmount(&lfs) => 0;
  91. '''
  92. # parallel allocation reuse test
  93. [cases.parallel_allocation_reuse]
  94. defines.FILES = 3
  95. defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
  96. defines.CYCLES = [1, 10]
  97. code = '''
  98. const char *names[] = {"bacon", "eggs", "pancakes"};
  99. lfs_file_t files[FILES];
  100. lfs_t lfs;
  101. lfs_format(&lfs, cfg) => 0;
  102. for (int c = 0; c < CYCLES; c++) {
  103. lfs_mount(&lfs, cfg) => 0;
  104. lfs_mkdir(&lfs, "breakfast") => 0;
  105. lfs_unmount(&lfs) => 0;
  106. lfs_mount(&lfs, cfg) => 0;
  107. for (int n = 0; n < FILES; n++) {
  108. char path[1024];
  109. sprintf(path, "breakfast/%s", names[n]);
  110. lfs_file_open(&lfs, &files[n], path,
  111. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
  112. }
  113. for (int n = 0; n < FILES; n++) {
  114. size_t size = strlen(names[n]);
  115. for (int i = 0; i < SIZE; i += size) {
  116. lfs_file_write(&lfs, &files[n], names[n], size) => size;
  117. }
  118. }
  119. for (int n = 0; n < FILES; n++) {
  120. lfs_file_close(&lfs, &files[n]) => 0;
  121. }
  122. lfs_unmount(&lfs) => 0;
  123. lfs_mount(&lfs, cfg) => 0;
  124. for (int n = 0; n < FILES; n++) {
  125. char path[1024];
  126. sprintf(path, "breakfast/%s", names[n]);
  127. lfs_file_t file;
  128. lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
  129. size_t size = strlen(names[n]);
  130. for (int i = 0; i < SIZE; i += size) {
  131. uint8_t buffer[1024];
  132. lfs_file_read(&lfs, &file, buffer, size) => size;
  133. assert(memcmp(buffer, names[n], size) == 0);
  134. }
  135. lfs_file_close(&lfs, &file) => 0;
  136. }
  137. lfs_unmount(&lfs) => 0;
  138. lfs_mount(&lfs, cfg) => 0;
  139. for (int n = 0; n < FILES; n++) {
  140. char path[1024];
  141. sprintf(path, "breakfast/%s", names[n]);
  142. lfs_remove(&lfs, path) => 0;
  143. }
  144. lfs_remove(&lfs, "breakfast") => 0;
  145. lfs_unmount(&lfs) => 0;
  146. }
  147. '''
  148. # serial allocation reuse test
  149. [cases.serial_allocation_reuse]
  150. defines.FILES = 3
  151. defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
  152. defines.CYCLES = [1, 10]
  153. code = '''
  154. const char *names[] = {"bacon", "eggs", "pancakes"};
  155. lfs_t lfs;
  156. lfs_format(&lfs, cfg) => 0;
  157. for (int c = 0; c < CYCLES; c++) {
  158. lfs_mount(&lfs, cfg) => 0;
  159. lfs_mkdir(&lfs, "breakfast") => 0;
  160. lfs_unmount(&lfs) => 0;
  161. for (int n = 0; n < FILES; n++) {
  162. lfs_mount(&lfs, cfg) => 0;
  163. char path[1024];
  164. sprintf(path, "breakfast/%s", names[n]);
  165. lfs_file_t file;
  166. lfs_file_open(&lfs, &file, path,
  167. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
  168. size_t size = strlen(names[n]);
  169. uint8_t buffer[1024];
  170. memcpy(buffer, names[n], size);
  171. for (int i = 0; i < SIZE; i += size) {
  172. lfs_file_write(&lfs, &file, buffer, size) => size;
  173. }
  174. lfs_file_close(&lfs, &file) => 0;
  175. lfs_unmount(&lfs) => 0;
  176. }
  177. lfs_mount(&lfs, cfg) => 0;
  178. for (int n = 0; n < FILES; n++) {
  179. char path[1024];
  180. sprintf(path, "breakfast/%s", names[n]);
  181. lfs_file_t file;
  182. lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
  183. size_t size = strlen(names[n]);
  184. for (int i = 0; i < SIZE; i += size) {
  185. uint8_t buffer[1024];
  186. lfs_file_read(&lfs, &file, buffer, size) => size;
  187. assert(memcmp(buffer, names[n], size) == 0);
  188. }
  189. lfs_file_close(&lfs, &file) => 0;
  190. }
  191. lfs_unmount(&lfs) => 0;
  192. lfs_mount(&lfs, cfg) => 0;
  193. for (int n = 0; n < FILES; n++) {
  194. char path[1024];
  195. sprintf(path, "breakfast/%s", names[n]);
  196. lfs_remove(&lfs, path) => 0;
  197. }
  198. lfs_remove(&lfs, "breakfast") => 0;
  199. lfs_unmount(&lfs) => 0;
  200. }
  201. '''
  202. # exhaustion test
  203. [cases.exhaustion]
  204. code = '''
  205. lfs_t lfs;
  206. lfs_format(&lfs, cfg) => 0;
  207. lfs_mount(&lfs, cfg) => 0;
  208. lfs_file_t file;
  209. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  210. size_t size = strlen("exhaustion");
  211. uint8_t buffer[1024];
  212. memcpy(buffer, "exhaustion", size);
  213. lfs_file_write(&lfs, &file, buffer, size) => size;
  214. lfs_file_sync(&lfs, &file) => 0;
  215. size = strlen("blahblahblahblah");
  216. memcpy(buffer, "blahblahblahblah", size);
  217. lfs_ssize_t res;
  218. while (true) {
  219. res = lfs_file_write(&lfs, &file, buffer, size);
  220. if (res < 0) {
  221. break;
  222. }
  223. res => size;
  224. }
  225. res => LFS_ERR_NOSPC;
  226. lfs_file_close(&lfs, &file) => 0;
  227. lfs_unmount(&lfs) => 0;
  228. lfs_mount(&lfs, cfg) => 0;
  229. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_RDONLY);
  230. size = strlen("exhaustion");
  231. lfs_file_size(&lfs, &file) => size;
  232. lfs_file_read(&lfs, &file, buffer, size) => size;
  233. memcmp(buffer, "exhaustion", size) => 0;
  234. lfs_file_close(&lfs, &file) => 0;
  235. lfs_unmount(&lfs) => 0;
  236. '''
  237. # exhaustion wraparound test
  238. [cases.exhaustion_wraparound]
  239. defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-4)) / 3)'
  240. code = '''
  241. lfs_t lfs;
  242. lfs_format(&lfs, cfg) => 0;
  243. lfs_mount(&lfs, cfg) => 0;
  244. lfs_file_t file;
  245. lfs_file_open(&lfs, &file, "padding", LFS_O_WRONLY | LFS_O_CREAT);
  246. size_t size = strlen("buffering");
  247. uint8_t buffer[1024];
  248. memcpy(buffer, "buffering", size);
  249. for (int i = 0; i < SIZE; i += size) {
  250. lfs_file_write(&lfs, &file, buffer, size) => size;
  251. }
  252. lfs_file_close(&lfs, &file) => 0;
  253. lfs_remove(&lfs, "padding") => 0;
  254. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  255. size = strlen("exhaustion");
  256. memcpy(buffer, "exhaustion", size);
  257. lfs_file_write(&lfs, &file, buffer, size) => size;
  258. lfs_file_sync(&lfs, &file) => 0;
  259. size = strlen("blahblahblahblah");
  260. memcpy(buffer, "blahblahblahblah", size);
  261. lfs_ssize_t res;
  262. while (true) {
  263. res = lfs_file_write(&lfs, &file, buffer, size);
  264. if (res < 0) {
  265. break;
  266. }
  267. res => size;
  268. }
  269. res => LFS_ERR_NOSPC;
  270. lfs_file_close(&lfs, &file) => 0;
  271. lfs_unmount(&lfs) => 0;
  272. lfs_mount(&lfs, cfg) => 0;
  273. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_RDONLY);
  274. size = strlen("exhaustion");
  275. lfs_file_size(&lfs, &file) => size;
  276. lfs_file_read(&lfs, &file, buffer, size) => size;
  277. memcmp(buffer, "exhaustion", size) => 0;
  278. lfs_file_close(&lfs, &file) => 0;
  279. lfs_remove(&lfs, "exhaustion") => 0;
  280. lfs_unmount(&lfs) => 0;
  281. '''
  282. # dir exhaustion test
  283. [cases.dir_exhaustion]
  284. code = '''
  285. lfs_t lfs;
  286. lfs_format(&lfs, cfg) => 0;
  287. lfs_mount(&lfs, cfg) => 0;
  288. // find out max file size
  289. lfs_mkdir(&lfs, "exhaustiondir") => 0;
  290. size_t size = strlen("blahblahblahblah");
  291. uint8_t buffer[1024];
  292. memcpy(buffer, "blahblahblahblah", size);
  293. lfs_file_t file;
  294. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  295. int count = 0;
  296. int err;
  297. while (true) {
  298. err = lfs_file_write(&lfs, &file, buffer, size);
  299. if (err < 0) {
  300. break;
  301. }
  302. count += 1;
  303. }
  304. err => LFS_ERR_NOSPC;
  305. lfs_file_close(&lfs, &file) => 0;
  306. lfs_remove(&lfs, "exhaustion") => 0;
  307. lfs_remove(&lfs, "exhaustiondir") => 0;
  308. // see if dir fits with max file size
  309. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  310. for (int i = 0; i < count; i++) {
  311. lfs_file_write(&lfs, &file, buffer, size) => size;
  312. }
  313. lfs_file_close(&lfs, &file) => 0;
  314. lfs_mkdir(&lfs, "exhaustiondir") => 0;
  315. lfs_remove(&lfs, "exhaustiondir") => 0;
  316. lfs_remove(&lfs, "exhaustion") => 0;
  317. // see if dir fits with > max file size
  318. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  319. for (int i = 0; i < count+1; i++) {
  320. lfs_file_write(&lfs, &file, buffer, size) => size;
  321. }
  322. lfs_file_close(&lfs, &file) => 0;
  323. lfs_mkdir(&lfs, "exhaustiondir") => LFS_ERR_NOSPC;
  324. lfs_remove(&lfs, "exhaustion") => 0;
  325. lfs_unmount(&lfs) => 0;
  326. '''
  327. # what if we have a bad block during an allocation scan?
  328. [cases.bad_block_allocation]
  329. in = "lfs.c"
  330. defines.ERASE_CYCLES = 0xffffffff
  331. defines.BADBLOCK_BEHAVIOR = 'LFS_TESTBD_BADBLOCK_READERROR'
  332. code = '''
  333. lfs_t lfs;
  334. lfs_format(&lfs, cfg) => 0;
  335. lfs_mount(&lfs, cfg) => 0;
  336. // first fill to exhaustion to find available space
  337. lfs_file_t file;
  338. lfs_file_open(&lfs, &file, "pacman", LFS_O_WRONLY | LFS_O_CREAT) => 0;
  339. uint8_t buffer[1024];
  340. strcpy((char*)buffer, "waka");
  341. size_t size = strlen("waka");
  342. lfs_size_t filesize = 0;
  343. while (true) {
  344. lfs_ssize_t res = lfs_file_write(&lfs, &file, buffer, size);
  345. assert(res == (lfs_ssize_t)size || res == LFS_ERR_NOSPC);
  346. if (res == LFS_ERR_NOSPC) {
  347. break;
  348. }
  349. filesize += size;
  350. }
  351. lfs_file_close(&lfs, &file) => 0;
  352. // now fill all but a couple of blocks of the filesystem with data
  353. filesize -= 3*BLOCK_SIZE;
  354. lfs_file_open(&lfs, &file, "pacman", LFS_O_WRONLY | LFS_O_CREAT) => 0;
  355. strcpy((char*)buffer, "waka");
  356. size = strlen("waka");
  357. for (lfs_size_t i = 0; i < filesize/size; i++) {
  358. lfs_file_write(&lfs, &file, buffer, size) => size;
  359. }
  360. lfs_file_close(&lfs, &file) => 0;
  361. // also save head of file so we can error during lookahead scan
  362. lfs_block_t fileblock = file.ctz.head;
  363. lfs_unmount(&lfs) => 0;
  364. // remount to force an alloc scan
  365. lfs_mount(&lfs, cfg) => 0;
  366. // but mark the head of our file as a "bad block", this is force our
  367. // scan to bail early
  368. lfs_testbd_setwear(cfg, fileblock, 0xffffffff) => 0;
  369. lfs_file_open(&lfs, &file, "ghost", LFS_O_WRONLY | LFS_O_CREAT) => 0;
  370. strcpy((char*)buffer, "chomp");
  371. size = strlen("chomp");
  372. while (true) {
  373. lfs_ssize_t res = lfs_file_write(&lfs, &file, buffer, size);
  374. assert(res == (lfs_ssize_t)size || res == LFS_ERR_CORRUPT);
  375. if (res == LFS_ERR_CORRUPT) {
  376. break;
  377. }
  378. }
  379. lfs_file_close(&lfs, &file) => 0;
  380. // now reverse the "bad block" and try to write the file again until we
  381. // run out of space
  382. lfs_testbd_setwear(cfg, fileblock, 0) => 0;
  383. lfs_file_open(&lfs, &file, "ghost", LFS_O_WRONLY | LFS_O_CREAT) => 0;
  384. strcpy((char*)buffer, "chomp");
  385. size = strlen("chomp");
  386. while (true) {
  387. lfs_ssize_t res = lfs_file_write(&lfs, &file, buffer, size);
  388. assert(res == (lfs_ssize_t)size || res == LFS_ERR_NOSPC);
  389. if (res == LFS_ERR_NOSPC) {
  390. break;
  391. }
  392. }
  393. lfs_file_close(&lfs, &file) => 0;
  394. lfs_unmount(&lfs) => 0;
  395. // check that the disk isn't hurt
  396. lfs_mount(&lfs, cfg) => 0;
  397. lfs_file_open(&lfs, &file, "pacman", LFS_O_RDONLY) => 0;
  398. strcpy((char*)buffer, "waka");
  399. size = strlen("waka");
  400. for (lfs_size_t i = 0; i < filesize/size; i++) {
  401. uint8_t rbuffer[4];
  402. lfs_file_read(&lfs, &file, rbuffer, size) => size;
  403. assert(memcmp(rbuffer, buffer, size) == 0);
  404. }
  405. lfs_file_close(&lfs, &file) => 0;
  406. lfs_unmount(&lfs) => 0;
  407. '''
  408. # Below, I don't like these tests. They're fragile and depend _heavily_
  409. # on the geometry of the block device. But they are valuable. Eventually they
  410. # should be removed and replaced with generalized tests.
  411. # chained dir exhaustion test
  412. [cases.chained_dir_exhaustion]
  413. if = 'BLOCK_SIZE == 512'
  414. defines.BLOCK_COUNT = 1024
  415. code = '''
  416. lfs_t lfs;
  417. lfs_format(&lfs, cfg) => 0;
  418. lfs_mount(&lfs, cfg) => 0;
  419. // find out max file size
  420. lfs_mkdir(&lfs, "exhaustiondir") => 0;
  421. for (int i = 0; i < 10; i++) {
  422. char path[1024];
  423. sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
  424. lfs_mkdir(&lfs, path) => 0;
  425. }
  426. size_t size = strlen("blahblahblahblah");
  427. uint8_t buffer[1024];
  428. memcpy(buffer, "blahblahblahblah", size);
  429. lfs_file_t file;
  430. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  431. int count = 0;
  432. int err;
  433. while (true) {
  434. err = lfs_file_write(&lfs, &file, buffer, size);
  435. if (err < 0) {
  436. break;
  437. }
  438. count += 1;
  439. }
  440. err => LFS_ERR_NOSPC;
  441. lfs_file_close(&lfs, &file) => 0;
  442. lfs_remove(&lfs, "exhaustion") => 0;
  443. lfs_remove(&lfs, "exhaustiondir") => 0;
  444. for (int i = 0; i < 10; i++) {
  445. char path[1024];
  446. sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
  447. lfs_remove(&lfs, path) => 0;
  448. }
  449. // see that chained dir fails
  450. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  451. for (int i = 0; i < count+1; i++) {
  452. lfs_file_write(&lfs, &file, buffer, size) => size;
  453. }
  454. lfs_file_sync(&lfs, &file) => 0;
  455. for (int i = 0; i < 10; i++) {
  456. char path[1024];
  457. sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
  458. lfs_mkdir(&lfs, path) => 0;
  459. }
  460. lfs_mkdir(&lfs, "exhaustiondir") => LFS_ERR_NOSPC;
  461. // shorten file to try a second chained dir
  462. while (true) {
  463. err = lfs_mkdir(&lfs, "exhaustiondir");
  464. if (err != LFS_ERR_NOSPC) {
  465. break;
  466. }
  467. lfs_ssize_t filesize = lfs_file_size(&lfs, &file);
  468. filesize > 0 => true;
  469. lfs_file_truncate(&lfs, &file, filesize - size) => 0;
  470. lfs_file_sync(&lfs, &file) => 0;
  471. }
  472. err => 0;
  473. lfs_mkdir(&lfs, "exhaustiondir2") => LFS_ERR_NOSPC;
  474. lfs_file_close(&lfs, &file) => 0;
  475. lfs_unmount(&lfs) => 0;
  476. '''
  477. # split dir test
  478. [cases.split_dir]
  479. if = 'BLOCK_SIZE == 512'
  480. defines.BLOCK_COUNT = 1024
  481. code = '''
  482. lfs_t lfs;
  483. lfs_format(&lfs, cfg) => 0;
  484. lfs_mount(&lfs, cfg) => 0;
  485. // create one block hole for half a directory
  486. lfs_file_t file;
  487. lfs_file_open(&lfs, &file, "bump", LFS_O_WRONLY | LFS_O_CREAT) => 0;
  488. for (lfs_size_t i = 0; i < cfg->block_size; i += 2) {
  489. uint8_t buffer[1024];
  490. memcpy(&buffer[i], "hi", 2);
  491. }
  492. uint8_t buffer[1024];
  493. lfs_file_write(&lfs, &file, buffer, cfg->block_size) => cfg->block_size;
  494. lfs_file_close(&lfs, &file) => 0;
  495. lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
  496. size_t size = strlen("blahblahblahblah");
  497. memcpy(buffer, "blahblahblahblah", size);
  498. for (lfs_size_t i = 0;
  499. i < (cfg->block_count-4)*(cfg->block_size-8);
  500. i += size) {
  501. lfs_file_write(&lfs, &file, buffer, size) => size;
  502. }
  503. lfs_file_close(&lfs, &file) => 0;
  504. // remount to force reset of lookahead
  505. lfs_unmount(&lfs) => 0;
  506. lfs_mount(&lfs, cfg) => 0;
  507. // open hole
  508. lfs_remove(&lfs, "bump") => 0;
  509. lfs_mkdir(&lfs, "splitdir") => 0;
  510. lfs_file_open(&lfs, &file, "splitdir/bump",
  511. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  512. for (lfs_size_t i = 0; i < cfg->block_size; i += 2) {
  513. memcpy(&buffer[i], "hi", 2);
  514. }
  515. lfs_file_write(&lfs, &file, buffer, 2*cfg->block_size) => LFS_ERR_NOSPC;
  516. lfs_file_close(&lfs, &file) => 0;
  517. lfs_unmount(&lfs) => 0;
  518. '''
  519. # outdated lookahead test
  520. [cases.outdated_lookahead]
  521. if = 'BLOCK_SIZE == 512'
  522. defines.BLOCK_COUNT = 1024
  523. code = '''
  524. lfs_t lfs;
  525. lfs_format(&lfs, cfg) => 0;
  526. lfs_mount(&lfs, cfg) => 0;
  527. // fill completely with two files
  528. lfs_file_t file;
  529. lfs_file_open(&lfs, &file, "exhaustion1",
  530. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  531. size_t size = strlen("blahblahblahblah");
  532. uint8_t buffer[1024];
  533. memcpy(buffer, "blahblahblahblah", size);
  534. for (lfs_size_t i = 0;
  535. i < ((cfg->block_count-2)/2)*(cfg->block_size-8);
  536. i += size) {
  537. lfs_file_write(&lfs, &file, buffer, size) => size;
  538. }
  539. lfs_file_close(&lfs, &file) => 0;
  540. lfs_file_open(&lfs, &file, "exhaustion2",
  541. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  542. size = strlen("blahblahblahblah");
  543. memcpy(buffer, "blahblahblahblah", size);
  544. for (lfs_size_t i = 0;
  545. i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8);
  546. i += size) {
  547. lfs_file_write(&lfs, &file, buffer, size) => size;
  548. }
  549. lfs_file_close(&lfs, &file) => 0;
  550. // remount to force reset of lookahead
  551. lfs_unmount(&lfs) => 0;
  552. lfs_mount(&lfs, cfg) => 0;
  553. // rewrite one file
  554. lfs_file_open(&lfs, &file, "exhaustion1",
  555. LFS_O_WRONLY | LFS_O_TRUNC) => 0;
  556. lfs_file_sync(&lfs, &file) => 0;
  557. size = strlen("blahblahblahblah");
  558. memcpy(buffer, "blahblahblahblah", size);
  559. for (lfs_size_t i = 0;
  560. i < ((cfg->block_count-2)/2)*(cfg->block_size-8);
  561. i += size) {
  562. lfs_file_write(&lfs, &file, buffer, size) => size;
  563. }
  564. lfs_file_close(&lfs, &file) => 0;
  565. // rewrite second file, this requires lookahead does not
  566. // use old population
  567. lfs_file_open(&lfs, &file, "exhaustion2",
  568. LFS_O_WRONLY | LFS_O_TRUNC) => 0;
  569. lfs_file_sync(&lfs, &file) => 0;
  570. size = strlen("blahblahblahblah");
  571. memcpy(buffer, "blahblahblahblah", size);
  572. for (lfs_size_t i = 0;
  573. i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8);
  574. i += size) {
  575. lfs_file_write(&lfs, &file, buffer, size) => size;
  576. }
  577. lfs_file_close(&lfs, &file) => 0;
  578. lfs_unmount(&lfs) => 0;
  579. '''
  580. # outdated lookahead and split dir test
  581. [cases.outdated_lookahead_split_dir]
  582. if = 'BLOCK_SIZE == 512'
  583. defines.BLOCK_COUNT = 1024
  584. code = '''
  585. lfs_t lfs;
  586. lfs_format(&lfs, cfg) => 0;
  587. lfs_mount(&lfs, cfg) => 0;
  588. // fill completely with two files
  589. lfs_file_t file;
  590. lfs_file_open(&lfs, &file, "exhaustion1",
  591. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  592. size_t size = strlen("blahblahblahblah");
  593. uint8_t buffer[1024];
  594. memcpy(buffer, "blahblahblahblah", size);
  595. for (lfs_size_t i = 0;
  596. i < ((cfg->block_count-2)/2)*(cfg->block_size-8);
  597. i += size) {
  598. lfs_file_write(&lfs, &file, buffer, size) => size;
  599. }
  600. lfs_file_close(&lfs, &file) => 0;
  601. lfs_file_open(&lfs, &file, "exhaustion2",
  602. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  603. size = strlen("blahblahblahblah");
  604. memcpy(buffer, "blahblahblahblah", size);
  605. for (lfs_size_t i = 0;
  606. i < ((cfg->block_count-2+1)/2)*(cfg->block_size-8);
  607. i += size) {
  608. lfs_file_write(&lfs, &file, buffer, size) => size;
  609. }
  610. lfs_file_close(&lfs, &file) => 0;
  611. // remount to force reset of lookahead
  612. lfs_unmount(&lfs) => 0;
  613. lfs_mount(&lfs, cfg) => 0;
  614. // rewrite one file with a hole of one block
  615. lfs_file_open(&lfs, &file, "exhaustion1",
  616. LFS_O_WRONLY | LFS_O_TRUNC) => 0;
  617. lfs_file_sync(&lfs, &file) => 0;
  618. size = strlen("blahblahblahblah");
  619. memcpy(buffer, "blahblahblahblah", size);
  620. for (lfs_size_t i = 0;
  621. i < ((cfg->block_count-2)/2 - 1)*(cfg->block_size-8);
  622. i += size) {
  623. lfs_file_write(&lfs, &file, buffer, size) => size;
  624. }
  625. lfs_file_close(&lfs, &file) => 0;
  626. // try to allocate a directory, should fail!
  627. lfs_mkdir(&lfs, "split") => LFS_ERR_NOSPC;
  628. // file should not fail
  629. lfs_file_open(&lfs, &file, "notasplit",
  630. LFS_O_WRONLY | LFS_O_CREAT) => 0;
  631. lfs_file_write(&lfs, &file, "hi", 2) => 2;
  632. lfs_file_close(&lfs, &file) => 0;
  633. lfs_unmount(&lfs) => 0;
  634. '''