+ for (inode_t i = 0; i < inode; i++)
+ cnt += filelen_table[i];
+
+ return cnt;
+}
+
+/**
+ * Move all pages in page allocation array from \a src to \a src + \a offset.
+ * The number of pages moved is page_count - MAX(dst, src).
+ */
+static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
+{
+ pgcnt_t dst = src + offset;
+ memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
+
+ if (offset < 0)
+ {
+ /* Fill empty space in array with sentinel */
+ for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
+ disk->page_array[page] = PAGE_UNSET_SENTINEL;
+ }
+}
+
+/**
+ * Insert \a page into page allocation array of \a disk,
+ * using \a mark to compute position.
+ */
+static void insertFreePage(struct BattFsSuper *disk, mark_t mark, pgcnt_t page)
+{
+ ASSERT(mark - disk->free_start < disk->free_next - disk->free_start);
+
+ pgcnt_t free_pos = disk->page_count - disk->free_next + mark;
+ ASSERT(free_pos < disk->page_count);
+
+ TRACEMSG("mark:%u, page:%u, free_start:%u, free_next:%u, free_pos:%u\n",
+ mark, page, disk->free_start, disk->free_next, free_pos);
+
+ ASSERT(disk->page_array[free_pos] == PAGE_UNSET_SENTINEL);
+ disk->page_array[free_pos] = page;
+}
+
+/**
+ * Mark \a page of \a disk as free.
+ * \note free_next of \a disk is used as \a page free marker
+ * and is increased by 1.
+ */
+static bool battfs_markFree(struct BattFsSuper *disk, struct BattFsPageHeader *hdr, pgcnt_t page)
+{
+ uint8_t buf[BATTFS_HEADER_LEN];
+
+ hdr->mark = disk->free_next;
+ hdr->fcs_free = computeFcsFree(hdr);
+ battfs_to_disk(hdr, buf);
+
+ if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN))
+ {
+ TRACEMSG("error marking page [%d]\n", page);
+ return false;
+ }
+ 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;