+ /* Fill page array with sentinel */
+ for (pgcnt_t page = 0; page < disk->page_count; page++)
+ disk->page_array[page] = PAGE_UNSET_SENTINEL;
+
+ /* Determine free_start & free_next */
+ if (maxl >= minl)
+ {
+ if (maxh >= minh)
+ {
+ if (maxl == minh - 1)
+ {
+ disk->free_start = minl;
+ disk->free_next = maxh;
+ }
+ else
+ {
+ ASSERT(minl == 0);
+ ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
+
+ disk->free_start = minh;
+ disk->free_next = maxl;
+ }
+ }
+ else
+ {
+ disk->free_start = minl;
+ disk->free_next = maxl;
+ }
+ }
+ else if (maxh >= minh)
+ {
+ disk->free_start = minh;
+ disk->free_next = maxh;
+ }
+ else
+ {
+ TRACEMSG("No valid marked free block found\n");
+ disk->free_start = 0;
+ disk->free_next = -1; //to be incremented ahead
+ }
+
+ /* free_next should contain the first usable address */
+ disk->free_next++;
+
+ TRACEMSG("Free markers:\n minl %u\n maxl %u\n minh %u\n maxh %u\n free_start %u\n free_next %u\n",
+ minl, maxl, minh, maxh, disk->free_start, disk->free_next);
+
+
+ /* Fill page allocation array */
+ for (pgcnt_t page = 0; page < disk->page_count; page++)
+ {
+ if (!battfs_readHeader(disk, page, &hdr))
+ return false;
+
+ /* Check header FCS */
+ if (hdr.fcs == computeFcs(&hdr))
+ {
+ /* Page is valid and is owned by a file */
+ ASSERT(hdr.mark == MARK_PAGE_VALID);
+ ASSERT(hdr.fcs_free == FCS_FREE_VALID);
+
+ /* Compute array position */
+ pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
+ array_pos += hdr.pgoff;
+
+ /* Check if position is already used by another page of the same file */
+ if (LIKELY(disk->page_array[array_pos] == PAGE_UNSET_SENTINEL))
+ disk->page_array[array_pos] = page;
+ else
+ {
+ BattFsPageHeader hdr_old;
+
+ if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_old))
+ return false;
+
+ /* Check header FCS */
+ ASSERT(hdr_old.fcs == computeFcs(&hdr_old));
+
+ /* Only the very same page with a different seq number can be here */
+ ASSERT(hdr.inode == hdr_old.inode);
+ ASSERT(hdr.pgoff == hdr_old.pgoff);
+ ASSERT(hdr.mark == hdr_old.mark);
+ ASSERT(hdr.fcs_free == hdr_old.fcs_free);
+ ASSERT(hdr.seq != hdr_old.seq);
+
+ pgcnt_t new_page, old_page;
+ fill_t old_fill;
+
+ /* Fancy check to handle seq wraparound (2 bits only) */
+ if (((hdr.seq - hdr_old.seq) & 0x03) < 2)
+ {
+ /* Current header is newer than the previuos one */
+ old_page = disk->page_array[array_pos];
+ new_page = page;
+ old_fill = hdr_old.fill;
+ }
+ else
+ {
+ /* Previous header is newer than the current one */
+ old_page = page;
+ new_page = disk->page_array[array_pos];
+ old_fill = hdr.fill;
+ }
+
+ /* Set new page */
+ disk->page_array[array_pos] = new_page;
+
+ /* Add free space */
+ disk->free_bytes += old_fill;
+
+ /* Shift all array one position to the left, overwriting duplicate page */
+ array_pos -= hdr.pgoff;
+ array_pos += filelen_table[hdr.inode];
+ movePages(disk, array_pos, -1);
+
+ /* Decrease file page count */
+ filelen_table[hdr.inode]--;
+
+ /* Add old page to free pages pool */
+ if (!battfs_markFree(disk, &hdr, old_page))
+ return false;
+
+ insertFreePage(disk, filelen_table, hdr.mark, old_page);
+ }
+ }
+ else
+ {
+ /* Check if page is free */
+ if (hdr.fcs_free != computeFcsFree(&hdr))
+ /* Page is not a valid marked page, insert at the end of list */
+ hdr.mark = disk->free_next++;
+
+ insertFreePage(disk, filelen_table, hdr.mark, page);
+ }
+ }
+
+ #warning Test me!