ringfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. /**
  9. * @defgroup ringfs_impl RingFS implementation
  10. * @details
  11. *
  12. * @{
  13. */
  14. #include <ringfs.h>
  15. #include <inttypes.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. //#include "main.h"
  19. #include "common_config.h"
  20. /**
  21. * @defgroup sector
  22. * @{
  23. */
  24. enum sector_status {
  25. SECTOR_ERASED = 0xFFFFFFFF, /**< Default state after NOR flash erase. */
  26. SECTOR_FREE = 0xFFFFFF00, /**< Sector erased. */
  27. SECTOR_IN_USE = 0xFFFF0000, /**< Sector contains valid data. */
  28. SECTOR_ERASING = 0xFF000000, /**< Sector should be erased. */
  29. SECTOR_FORMATTING = 0x00000000, /**< The entire partition is being formatted. */
  30. };
  31. struct sector_header {
  32. uint32_t status;
  33. uint32_t version;
  34. };
  35. static int _sector_address(struct ringfs *fs, int sector_offset)
  36. {
  37. return (fs->flash->sector_offset + sector_offset) * fs->flash->sector_size;
  38. }
  39. static int _sector_get_status(struct ringfs *fs, int sector, uint32_t *status)
  40. {
  41. return fs->flash->read(fs->flash,
  42. _sector_address(fs, sector) + offsetof(struct sector_header, status),
  43. status, sizeof(*status));
  44. }
  45. static int _sector_set_status(struct ringfs *fs, int sector, uint32_t status)
  46. {
  47. return fs->flash->program(fs->flash,
  48. _sector_address(fs, sector) + offsetof(struct sector_header, status),
  49. &status, sizeof(status));
  50. }
  51. static int _sector_free(struct ringfs *fs, int sector)
  52. {
  53. int sector_addr = _sector_address(fs, sector);
  54. _sector_set_status(fs, sector, SECTOR_ERASING);
  55. fs->flash->sector_erase(fs->flash, sector_addr);
  56. fs->flash->program(fs->flash,
  57. sector_addr + offsetof(struct sector_header, version),
  58. &fs->version, sizeof(fs->version));
  59. _sector_set_status(fs, sector, SECTOR_FREE);
  60. return 0;
  61. }
  62. /**
  63. * @}
  64. * @defgroup slot
  65. * @{
  66. */
  67. enum slot_status {
  68. SLOT_ERASED = 0xFFFFFFFF, /**< Default state after NOR flash erase. */
  69. SLOT_RESERVED = 0xFFFFFF00, /**< Write started but not yet committed. */
  70. SLOT_VALID = 0xFFFF0000, /**< Write committed, slot contains valid data. */
  71. SLOT_GARBAGE = 0xFF000000, /**< Slot contents discarded and no longer valid. */
  72. };
  73. struct slot_header {
  74. uint32_t status;
  75. };
  76. static int _slot_address(struct ringfs *fs, struct ringfs_loc *loc)
  77. {
  78. return _sector_address(fs, loc->sector) +
  79. sizeof(struct sector_header) +
  80. (sizeof(struct slot_header) + fs->object_size) * loc->slot;
  81. }
  82. static int _slot_get_status(struct ringfs *fs, struct ringfs_loc *loc, uint32_t *status)
  83. {
  84. return fs->flash->read(fs->flash,
  85. _slot_address(fs, loc) + offsetof(struct slot_header, status),
  86. status, sizeof(*status));
  87. }
  88. static int _slot_set_status(struct ringfs *fs, struct ringfs_loc *loc, uint32_t status)
  89. {
  90. return fs->flash->program(fs->flash,
  91. _slot_address(fs, loc) + offsetof(struct slot_header, status),
  92. &status, sizeof(status));
  93. }
  94. /**
  95. * @}
  96. * @defgroup loc
  97. * @{
  98. */
  99. static bool _loc_equal(struct ringfs_loc *a, struct ringfs_loc *b)
  100. {
  101. return (a->sector == b->sector) && (a->slot == b->slot);
  102. }
  103. /** Advance a location to the beginning of the next sector. */
  104. static void _loc_advance_sector(struct ringfs *fs, struct ringfs_loc *loc)
  105. {
  106. loc->slot = 0;
  107. loc->sector++;
  108. if (loc->sector >= fs->flash->sector_count)
  109. loc->sector = 0;
  110. }
  111. /** Advance a location to the next slot, advancing the sector too if needed. */
  112. static void _loc_advance_slot(struct ringfs *fs, struct ringfs_loc *loc)
  113. {
  114. loc->slot++;
  115. if (loc->slot >= fs->slots_per_sector)
  116. _loc_advance_sector(fs, loc);
  117. }
  118. /**
  119. * @}
  120. */
  121. /* And here we go. */
  122. int ringfs_init(struct ringfs *fs, struct ringfs_flash_partition *flash, uint32_t version, int object_size)
  123. {
  124. /* Copy arguments to instance. */
  125. fs->flash = flash;
  126. fs->version = version;
  127. fs->object_size = object_size;
  128. /* Precalculate commonly used values. */
  129. fs->slots_per_sector = (fs->flash->sector_size - sizeof(struct sector_header)) /
  130. (sizeof(struct slot_header) + fs->object_size);
  131. return 0;
  132. }
  133. int ringfs_format(struct ringfs *fs)
  134. {
  135. /* Mark all sectors to prevent half-erased filesystems. */
  136. for (int sector=0; sector<fs->flash->sector_count; sector++)
  137. _sector_set_status(fs, sector, SECTOR_FORMATTING);
  138. /* Erase, update version, mark as free. */
  139. for (int sector=0; sector<fs->flash->sector_count; sector++)
  140. _sector_free(fs, sector);
  141. /* Start reading & writing at the first sector. */
  142. fs->read.sector = 0;
  143. fs->read.slot = 0;
  144. fs->write.sector = 0;
  145. fs->write.slot = 0;
  146. fs->cursor.sector = 0;
  147. fs->cursor.slot = 0;
  148. return 0;
  149. }
  150. int ringfs_scan(struct ringfs *fs)
  151. {
  152. uint32_t previous_sector_status = SECTOR_FREE;
  153. /* The read sector is the first IN_USE sector *after* a FREE sector
  154. * (or the first one). */
  155. int read_sector = 0;
  156. /* The write sector is the last IN_USE sector *before* a FREE sector
  157. * (or the last one). */
  158. int write_sector = fs->flash->sector_count - 1;
  159. /* There must be at least one FREE sector available at all times. */
  160. bool free_seen = false;
  161. /* If there's no IN_USE sector, we start at the first one. */
  162. bool used_seen = false;
  163. bool err_sector = true;
  164. /* Iterate over sectors. */
  165. for (int sector=0; sector<fs->flash->sector_count; sector++) {
  166. int addr = _sector_address(fs, sector);
  167. /* Read sector header. */
  168. struct sector_header header;
  169. fs->flash->read(fs->flash, addr, &header, sizeof(header));
  170. /* Detect partially-formatted partitions. */
  171. if (header.status == SECTOR_FORMATTING) {
  172. DBG printf("ringfs_scan: partially formatted partition\r\n");
  173. if(err_sector){
  174. err_sector = false;
  175. _sector_free(fs, addr);
  176. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  177. header.status = SECTOR_FREE;
  178. header.version = fs->version;
  179. }else{
  180. return -1;
  181. }
  182. }
  183. /* Detect and fix partially erased sectors. */
  184. if (header.status == SECTOR_ERASING || header.status == SECTOR_ERASED) {
  185. _sector_free(fs, addr);
  186. header.status = SECTOR_FREE;
  187. }
  188. /* Detect corrupted sectors. */
  189. if (header.status != SECTOR_FREE && header.status != SECTOR_IN_USE) {
  190. DBG printf("ringfs_scan: corrupted sector %d\r\n", sector);
  191. if(err_sector){
  192. err_sector = false;
  193. _sector_free(fs, addr);
  194. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  195. header.status = SECTOR_FREE;
  196. header.version = fs->version;
  197. }else{
  198. return -1;
  199. }
  200. }
  201. /* Detect obsolete versions. We can't do this earlier because the version
  202. * could have been invalid due to a partial erase. */
  203. if (header.version != fs->version) {
  204. DBG printf("ringfs_scan: corrupted sector %d\r\n", sector);
  205. DBG printf("ringfs_scan: incompatible version 0x%08"PRIx32"\r\n", header.version);
  206. if(err_sector){
  207. err_sector = false;
  208. _sector_free(fs, addr);
  209. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  210. DBG printf("ringfs_scan: incompatible version 0x%08"PRIx32"\r\n", header.version);
  211. header.status = SECTOR_FREE;
  212. header.version = fs->version;
  213. }else{
  214. return -1;
  215. }
  216. }
  217. /* Record the presence of a FREE sector. */
  218. if (header.status == SECTOR_FREE)
  219. free_seen = true;
  220. /* Record the presence of a IN_USE sector. */
  221. if (header.status == SECTOR_IN_USE)
  222. used_seen = true;
  223. /* Update read & write sectors according to the above rules. */
  224. if (header.status == SECTOR_IN_USE && previous_sector_status == SECTOR_FREE)
  225. read_sector = sector;
  226. if (header.status == SECTOR_FREE && previous_sector_status == SECTOR_IN_USE)
  227. write_sector = sector-1;
  228. previous_sector_status = header.status;
  229. }
  230. /* Detect the lack of a FREE sector. */
  231. if (!free_seen) {
  232. DBG printf("ringfs_scan: invariant violated: no FREE sector found\r\n");
  233. return -1;
  234. }
  235. /* Start writing at the first sector if the filesystem is empty. */
  236. if (!used_seen) {
  237. write_sector = 0;
  238. }
  239. /* Scan the write sector and skip all occupied slots at the beginning. */
  240. fs->write.sector = write_sector;
  241. fs->write.slot = 0;
  242. while (fs->write.sector == write_sector) {
  243. uint32_t status;
  244. _slot_get_status(fs, &fs->write, &status);
  245. if (status == SLOT_ERASED)
  246. break;
  247. _loc_advance_slot(fs, &fs->write);
  248. }
  249. /* If the sector was full, we're at the beginning of a FREE sector now. */
  250. /* Position the read head at the start of the first IN_USE sector, then skip
  251. * over garbage/invalid slots until something of value is found or we reach
  252. * the write head which means there's no data. */
  253. fs->read.sector = read_sector;
  254. fs->read.slot = 0;
  255. while (!_loc_equal(&fs->read, &fs->write)) {
  256. uint32_t status;
  257. _slot_get_status(fs, &fs->read, &status);
  258. if (status == SLOT_VALID)
  259. break;
  260. _loc_advance_slot(fs, &fs->read);
  261. }
  262. /* Move the read cursor to the read head position. */
  263. ringfs_rewind(fs);
  264. return 0;
  265. }
  266. int ringfs_capacity(struct ringfs *fs)
  267. {
  268. return fs->slots_per_sector * (fs->flash->sector_count - 1);
  269. }
  270. int ringfs_count_estimate(struct ringfs *fs)
  271. {
  272. int sector_diff = (fs->write.sector - fs->read.sector + fs->flash->sector_count) %
  273. fs->flash->sector_count;
  274. return sector_diff * fs->slots_per_sector + fs->write.slot - fs->read.slot;
  275. }
  276. int ringfs_count_exact(struct ringfs *fs)
  277. {
  278. int count = 0;
  279. /* Use a temporary loc for iteration. */
  280. struct ringfs_loc loc = fs->read;
  281. while (!_loc_equal(&loc, &fs->write)) {
  282. uint32_t status;
  283. _slot_get_status(fs, &loc, &status);
  284. if (status == SLOT_VALID)
  285. count++;
  286. _loc_advance_slot(fs, &loc);
  287. }
  288. return count;
  289. }
  290. int ringfs_cursor_position(struct ringfs *fs) {
  291. return fs->cursor_position;
  292. }
  293. int ringfs_append(struct ringfs *fs, const void *object)
  294. {
  295. uint32_t status;
  296. /*
  297. * There are three sectors involved in appending a value:
  298. * - the sector where the append happens: it has to be writable
  299. * - the next sector: it must be free (invariant)
  300. * - the next-next sector: read & cursor heads are moved there if needed
  301. */
  302. /* Make sure the next sector is free. */
  303. int next_sector = (fs->write.sector+1) % fs->flash->sector_count;
  304. _sector_get_status(fs, next_sector, &status);
  305. if (status != SECTOR_FREE) {
  306. /* Next sector must be freed. But first... */
  307. /* Move the read & cursor heads out of the way. */
  308. if (fs->read.sector == next_sector)
  309. _loc_advance_sector(fs, &fs->read);
  310. if (fs->cursor.sector == next_sector)
  311. _loc_advance_sector(fs, &fs->cursor);
  312. /* Free the next sector. */
  313. _sector_free(fs, next_sector);
  314. }
  315. /* Now we can make sure the current write sector is writable. */
  316. _sector_get_status(fs, fs->write.sector, &status);
  317. if (status == SECTOR_FREE) {
  318. /* Free sector. Mark as used. */
  319. _sector_set_status(fs, fs->write.sector, SECTOR_IN_USE);
  320. } else if (status != SECTOR_IN_USE) {
  321. printf("ringfs_append: corrupted filesystem\r\n");
  322. return -1;
  323. }
  324. /* Preallocate slot. */
  325. _slot_set_status(fs, &fs->write, SLOT_RESERVED);
  326. /* Write object. */
  327. fs->flash->program(fs->flash,
  328. _slot_address(fs, &fs->write) + sizeof(struct slot_header),
  329. object, fs->object_size);
  330. /* Commit write. */
  331. _slot_set_status(fs, &fs->write, SLOT_VALID);
  332. /* Advance the write head. */
  333. _loc_advance_slot(fs, &fs->write);
  334. return 0;
  335. }
  336. int ringfs_fetch(struct ringfs *fs, void *object)
  337. {
  338. /* Advance forward in search of a valid slot. */
  339. while (!_loc_equal(&fs->cursor, &fs->write)) {
  340. uint32_t status;
  341. _slot_get_status(fs, &fs->cursor, &status);
  342. if (status == SLOT_VALID) {
  343. fs->flash->read(fs->flash,
  344. _slot_address(fs, &fs->cursor) + sizeof(struct slot_header),
  345. object, fs->object_size);
  346. _loc_advance_slot(fs, &fs->cursor);
  347. fs->cursor_position++;
  348. return 0;
  349. }
  350. _loc_advance_slot(fs, &fs->cursor);
  351. }
  352. return -1;
  353. }
  354. int ringfs_discard(struct ringfs *fs)
  355. {
  356. while (!_loc_equal(&fs->read, &fs->cursor)) {
  357. _slot_set_status(fs, &fs->read, SLOT_GARBAGE);
  358. _loc_advance_slot(fs, &fs->read);
  359. }
  360. fs->cursor_position = 0;
  361. return 0;
  362. }
  363. int ringfs_rewind(struct ringfs *fs)
  364. {
  365. fs->cursor = fs->read;
  366. fs->cursor_position = 0;
  367. return 0;
  368. }
  369. /**
  370. * @}
  371. */
  372. /* vim: set ts=4 sw=4 et: */