+ }
+ else
+ {
+ disk->free_next++;
+ return true;
+ }
+}
+
+/**
+ * Determine free_start and free_next blocks for \a disk
+ * using \a minl, \a maxl, \a minh, \a maxh.
+ *
+ * Mark_t is a type that has at least 1 bit more than
+ * pgaddr_t. So all free blocks can be numbered using
+ * at most half numbers of a mark_t type.
+ * The free blocks algorithm increments by 1 the disk->free_next
+ * every time a page becomes free. So the free block sequence is
+ * guaranteed to be countiguous.
+ * Only wrap arounds may happen, but due to half size sequence limitation,
+ * there are only 4 possible situations:
+ *
+ * \verbatim
+ * |------lower half------|-------upper half-------|
+ *
+ * 1) |------minl*****maxl---|------------------------|
+ * 2) |------minl********maxl|minh******maxh----------|
+ * 3) |----------------------|----minh*******maxh-----|
+ * 4) |minl******maxl--------|------------minh****maxh|
+ * \endverbatim
+ *
+ * Situations 1 and 3 are easy to detect, while 2 and 4 require more care.
+ */
+static void findFreeStartNext(struct BattFsSuper *disk, mark_t minl, mark_t maxl, mark_t minh, mark_t maxh)
+{
+ /* Determine free_start & free_next */
+ if (maxl >= minl)
+ {
+ /* Valid interval found in lower half */
+ if (maxh >= minh)
+ {
+ /* Valid interval also found in upper half */
+ if (maxl == minh - 1)
+ {
+ /* Interval starts in lower half and ends in upper */
+ disk->free_start = minl;
+ disk->free_next = maxh;
+ }
+ else
+ {
+ /* Interval starts in upper half and ends in lower */
+ ASSERT(minl == 0);
+ ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
+
+ disk->free_start = minh;
+ disk->free_next = maxl;
+ }
+ }
+ else
+ {
+ /*
+ * Upper interval is invalid.
+ * Use lower values.
+ */
+
+ disk->free_start = minl;
+ disk->free_next = maxl;
+ }
+ }
+ else if (maxh >= minh)
+ {
+ /*
+ * Lower interval is invalid.
+ * Use upper values.
+ */
+ disk->free_start = minh;
+ disk->free_next = maxh;
+ }
+ else
+ {
+ /*
+ * No valid interval found.
+ * Hopefully the disk is brand new (or full).
+ */
+ TRACEMSG("No valid marked free block found, new disk or disk full\n");
+ disk->free_start = 0;
+ disk->free_next = -1; //to be increased later
+ }
+
+ /* 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);
+}
+
+/**
+ * Count number of pages per file on \a disk.
+ * This information is registered in \a filelen_table.
+ * Array index represent file inode, while value contained
+ * is the number of pages used by that file.
+ *
+ * \return true if ok, false on disk read errors.
+ * \note The whole disk is scanned once.
+ */
+static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
+{
+ BattFsPageHeader hdr;
+ mark_t minl, maxl, minh, maxh;
+
+ /* Initialize min and max counters to keep trace od free blocks */
+ minl = MAX_PAGE_ADDR;
+ maxl = 0;
+ minh = MAX_PAGE_ADDR | MARK_HALF_SIZE;
+ maxh = 0 | MARK_HALF_SIZE;
+
+
+ /* Count the number of disk page per file */
+ 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))
+ {
+ ASSERT(hdr.mark == MARK_PAGE_VALID);
+ ASSERT(hdr.fcs_free == FCS_FREE_VALID);
+ ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
+
+ /* Page is valid and is owned by a file */
+ filelen_table[hdr.inode]++;
+
+ /* Keep trace of free space */
+ disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN - hdr.fill;
+ }
+ else
+ {
+ /* Increase free space */
+ disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
+
+ /* Check if page is marked free */
+ if (hdr.fcs_free == computeFcsFree(&hdr))
+ {
+ /*
+ * This page is a valid and marked free page.
+ * Update min and max free page markers.
+ */
+ if (hdr.mark < MARK_HALF_SIZE)
+ {
+ minl = MIN(minl, hdr.mark);
+ maxl = MAX(maxl, hdr.mark);
+ }
+ else
+ {
+ minh = MIN(minh, hdr.mark);
+ maxh = MAX(maxh, hdr.mark);
+ }
+ }
+ else
+ TRACEMSG("page [%d] invalid, keeping as free\n", page);
+ }
+ }
+ findFreeStartNext(disk, minl, maxl, minh, maxh);
+ return true;
+}
+
+/**
+ * Fill page allocation array of \a disk
+ * using file lenghts in \a filelen_table.
+ *
+ * The page allocation array is an array containings all file infos.
+ * Is ordered by file, and within each file is ordered by page offset
+ * inside file.
+ * e.g. : at page array[0] you will find page address of the first page
+ * of the first file (if present).
+ * Free blocks are allocated after the last file, starting from invalid ones
+ * and continuing with the marked free ones.
+ *
+ * \return true if ok, false on disk read errors.
+ * \note The whole disk is scanned once.
+ */
+static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
+{
+ BattFsPageHeader hdr;
+ /* 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]--;