ringfs.c 13 KB

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