test_alloc.toml 23 KB

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