ringfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. fs->cursor_position = 0;
  149. return 0;
  150. }
  151. int ringfs_scan(struct ringfs *fs)
  152. {
  153. uint32_t previous_sector_status = SECTOR_FREE;
  154. /* The read sector is the first IN_USE sector *after* a FREE sector
  155. * (or the first one). */
  156. int read_sector = 0;
  157. /* The write sector is the last IN_USE sector *before* a FREE sector
  158. * (or the last one). */
  159. int write_sector = fs->flash->sector_count - 1;
  160. /* There must be at least one FREE sector available at all times. */
  161. bool free_seen = false;
  162. /* If there's no IN_USE sector, we start at the first one. */
  163. bool used_seen = false;
  164. bool err_sector = true;
  165. /* Iterate over sectors. */
  166. for (int sector=0; sector<fs->flash->sector_count; sector++) {
  167. int addr = _sector_address(fs, sector);
  168. /* Read sector header. */
  169. struct sector_header header;
  170. fs->flash->read(fs->flash, addr, &header, sizeof(header));
  171. /* Detect partially-formatted partitions. */
  172. if (header.status == SECTOR_FORMATTING) {
  173. DBG printf("ringfs_scan: partially formatted partition\r\n");
  174. if(err_sector){
  175. err_sector = false;
  176. _sector_free(fs, addr);
  177. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  178. header.status = SECTOR_FREE;
  179. header.version = fs->version;
  180. }else{
  181. return -1;
  182. }
  183. }
  184. /* Detect and fix partially erased sectors. */
  185. if (header.status == SECTOR_ERASING || header.status == SECTOR_ERASED) {
  186. _sector_free(fs, addr);
  187. header.status = SECTOR_FREE;
  188. }
  189. /* Detect corrupted sectors. */
  190. if (header.status != SECTOR_FREE && header.status != SECTOR_IN_USE) {
  191. DBG printf("ringfs_scan: corrupted sector %d\r\n", sector);
  192. if(err_sector){
  193. err_sector = false;
  194. _sector_free(fs, addr);
  195. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  196. header.status = SECTOR_FREE;
  197. header.version = fs->version;
  198. }else{
  199. return -1;
  200. }
  201. }
  202. /* Detect obsolete versions. We can't do this earlier because the version
  203. * could have been invalid due to a partial erase. */
  204. if (header.version != fs->version) {
  205. DBG printf("ringfs_scan: corrupted sector %d\r\n", sector);
  206. DBG printf("ringfs_scan: incompatible version 0x%08"PRIx32"\r\n", header.version);
  207. if(err_sector){
  208. err_sector = false;
  209. _sector_free(fs, addr);
  210. //fs->flash->read(fs->flash, addr, &header, sizeof(header));
  211. DBG printf("ringfs_scan: incompatible version 0x%08"PRIx32"\r\n", header.version);
  212. header.status = SECTOR_FREE;
  213. header.version = fs->version;
  214. }else{
  215. return -1;
  216. }
  217. }
  218. /* Record the presence of a FREE sector. */
  219. if (header.status == SECTOR_FREE)
  220. free_seen = true;
  221. /* Record the presence of a IN_USE sector. */
  222. if (header.status == SECTOR_IN_USE)
  223. used_seen = true;
  224. /* Update read & write sectors according to the above rules. */
  225. if (header.status == SECTOR_IN_USE && previous_sector_status == SECTOR_FREE)
  226. read_sector = sector;
  227. if (header.status == SECTOR_FREE && previous_sector_status == SECTOR_IN_USE)
  228. write_sector = sector-1;
  229. previous_sector_status = header.status;
  230. }
  231. /* Detect the lack of a FREE sector. */
  232. if (!free_seen) {
  233. DBG printf("ringfs_scan: invariant violated: no FREE sector found\r\n");
  234. return -1;
  235. }
  236. /* Start writing at the first sector if the filesystem is empty. */
  237. if (!used_seen) {
  238. write_sector = 0;
  239. }
  240. /* Scan the write sector and skip all occupied slots at the beginning. */
  241. fs->write.sector = write_sector;
  242. fs->write.slot = 0;
  243. while (fs->write.sector == write_sector) {
  244. uint32_t status;
  245. _slot_get_status(fs, &fs->write, &status);
  246. if (status == SLOT_ERASED)
  247. break;
  248. _loc_advance_slot(fs, &fs->write);
  249. }
  250. /* If the sector was full, we're at the beginning of a FREE sector now. */
  251. /* Position the read head at the start of the first IN_USE sector, then skip
  252. * over garbage/invalid slots until something of value is found or we reach
  253. * the write head which means there's no data. */
  254. fs->read.sector = read_sector;
  255. fs->read.slot = 0;
  256. while (!_loc_equal(&fs->read, &fs->write)) {
  257. uint32_t status;
  258. _slot_get_status(fs, &fs->read, &status);
  259. if (status == SLOT_VALID)
  260. break;
  261. _loc_advance_slot(fs, &fs->read);
  262. }
  263. /* Move the read cursor to the read head position. */
  264. ringfs_rewind(fs);
  265. return 0;
  266. }
  267. int ringfs_capacity(struct ringfs *fs)
  268. {
  269. return fs->slots_per_sector * (fs->flash->sector_count - 1);
  270. }
  271. int ringfs_count_estimate(struct ringfs *fs)
  272. {
  273. int sector_diff = (fs->write.sector - fs->read.sector + fs->flash->sector_count) %
  274. fs->flash->sector_count;
  275. return sector_diff * fs->slots_per_sector + fs->write.slot - fs->read.slot;
  276. }
  277. int ringfs_count_exact(struct ringfs *fs)
  278. {
  279. int count = 0;
  280. /* Use a temporary loc for iteration. */
  281. struct ringfs_loc loc = fs->read;
  282. while (!_loc_equal(&loc, &fs->write)) {
  283. uint32_t status;
  284. _slot_get_status(fs, &loc, &status);
  285. if (status == SLOT_VALID)
  286. count++;
  287. _loc_advance_slot(fs, &loc);
  288. }
  289. return count;
  290. }
  291. int ringfs_cursor_position(struct ringfs *fs) {
  292. return fs->cursor_position;
  293. }
  294. int ringfs_append(struct ringfs *fs, const void *object)
  295. {
  296. uint32_t status;
  297. /*
  298. * There are three sectors involved in appending a value:
  299. * - the sector where the append happens: it has to be writable
  300. * - the next sector: it must be free (invariant)
  301. * - the next-next sector: read & cursor heads are moved there if needed
  302. */
  303. /* Make sure the next sector is free. */
  304. int next_sector = (fs->write.sector+1) % fs->flash->sector_count;
  305. _sector_get_status(fs, next_sector, &status);
  306. if (status != SECTOR_FREE) {
  307. /* Next sector must be freed. But first... */
  308. /* Move the read & cursor heads out of the way. */
  309. if (fs->read.sector == next_sector)
  310. _loc_advance_sector(fs, &fs->read);
  311. if (fs->cursor.sector == next_sector)
  312. _loc_advance_sector(fs, &fs->cursor);
  313. /* Free the next sector. */
  314. _sector_free(fs, next_sector);
  315. }
  316. /* Now we can make sure the current write sector is writable. */
  317. _sector_get_status(fs, fs->write.sector, &status);
  318. if (status == SECTOR_FREE) {
  319. /* Free sector. Mark as used. */
  320. _sector_set_status(fs, fs->write.sector, SECTOR_IN_USE);
  321. } else if (status != SECTOR_IN_USE) {
  322. printf("ringfs_append: corrupted filesystem\r\n");
  323. return -1;
  324. }
  325. /* Preallocate slot. */
  326. _slot_set_status(fs, &fs->write, SLOT_RESERVED);
  327. /* Write object. */
  328. fs->flash->program(fs->flash,
  329. _slot_address(fs, &fs->write) + sizeof(struct slot_header),
  330. object, fs->object_size);
  331. /* Commit write. */
  332. _slot_set_status(fs, &fs->write, SLOT_VALID);
  333. /* Advance the write head. */
  334. _loc_advance_slot(fs, &fs->write);
  335. return 0;
  336. }
  337. int ringfs_fetch(struct ringfs *fs, void *object)
  338. {
  339. /* Advance forward in search of a valid slot. */
  340. while (!_loc_equal(&fs->cursor, &fs->write)) {
  341. uint32_t status;
  342. _slot_get_status(fs, &fs->cursor, &status);
  343. if (status == SLOT_VALID) {
  344. fs->flash->read(fs->flash,
  345. _slot_address(fs, &fs->cursor) + sizeof(struct slot_header),
  346. object, fs->object_size);
  347. _loc_advance_slot(fs, &fs->cursor);
  348. fs->cursor_position++;
  349. return 0;
  350. }
  351. _loc_advance_slot(fs, &fs->cursor);
  352. }
  353. return -1;
  354. }
  355. int ringfs_discard(struct ringfs *fs)
  356. {
  357. while (!_loc_equal(&fs->read, &fs->cursor)) {
  358. _slot_set_status(fs, &fs->read, SLOT_GARBAGE);
  359. _loc_advance_slot(fs, &fs->read);
  360. }
  361. fs->cursor_position = 0;
  362. return 0;
  363. }
  364. int ringfs_rewind(struct ringfs *fs)
  365. {
  366. fs->cursor = fs->read;
  367. fs->cursor_position = 0;
  368. return 0;
  369. }
  370. /**
  371. * @}
  372. */
  373. /* vim: set ts=4 sw=4 et: */