* First bit used by mark, last 2 bits used by seq.
* Since only 2 pages with the same inode and pgoff
* can exist at the same time, 2 bit for seq are enough.
+ * Unused bits are set to 1.
*/
- buf[7] = ((hdr->mark >> 16) & 0x01) | (hdr->seq << 6);
+ buf[7] = ((hdr->mark >> 16) & 0x01) | (hdr->seq << 6) | ~(BV(7) | BV(6) | BV(0));
/*
* This field must be the before the last one!
* Header is actually a footer, and so
* resides at page end.
*/
- if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN - 1, buf, BATTFS_HEADER_LEN)
+ if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
!= BATTFS_HEADER_LEN)
{
TRACEMSG("Error: page[%d]\n", page);
return true;
}
+/**
+ * Write header of page \a page.
+ * \return true on success, false otherwise.
+ * \note \a hdr is dirtyed even on errors.
+ */
+static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
+{
+ uint8_t buf[BATTFS_HEADER_LEN];
+
+ /* Fill buffer */
+ battfs_to_disk(hdr, buf);
+
+ /*
+ * write header to disk.
+ * Header is actually a footer, and so
+ * resides at page end.
+ */
+ if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+ != BATTFS_HEADER_LEN)
+ {
+ TRACEMSG("Error: page[%d]\n", page);
+ return false;
+ }
+ return true;
+}
+
/**
* Count the number of pages from
* inode 0 to \a inode in \a filelen_table.
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));
+ memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
if (offset < 0)
{
hdr->fcs_free = computeFcsFree(hdr);
battfs_to_disk(hdr, buf);
- if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN - 1, buf, BATTFS_HEADER_LEN))
+ if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN))
{
TRACEMSG("error marking page [%d]\n", page);
return false;
* 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 unsing
- * at most half numbers of an mark_t type.
- * The free blocks algorith increments by 1 the disk->free_next
+ * 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,
/* Valid interval also found in upper half */
if (maxl == minh - 1)
{
- /* Interval starts in lower half and end in upper */
+ /* Interval starts in lower half and ends in upper */
disk->free_start = minl;
disk->free_next = maxh;
}
else
{
- /* Interval starts in upper half and end in lower */
+ /* Interval starts in upper half and ends in lower */
ASSERT(minl == 0);
ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
{
/*
* No valid interval found.
- * Hopefully the disk is brand new.
+ * Hopefully the disk is brand new (or full).
*/
- TRACEMSG("No valid marked free block found, new disk?\n");
+ TRACEMSG("No valid marked free block found, new disk or disk full\n");
disk->free_start = 0;
- disk->free_next = -1; //to be incremented ahead
+ disk->free_next = -1; //to be increased later
}
/* free_next should contain the first usable address */
* Fill page allocation array of \a disk
* using file lenghts in \a filelen_table.
*
- * The page allocation array is an array containings all files info.
+ * 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
+ * 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.
return false;
}
+ /* Init list for opened files. */
+ LIST_INIT(&disk->file_opened_list);
return true;
}
+/**
+ * Flush file \a fd.
+ * \return 0 if ok, EOF on errors.
+ */
+static int battfs_flush(struct KFile *fd)
+{
+ (void)fd;
+ #warning TODO
+ return 0;
+}
+
+/**
+ * Close file \a fd.
+ * \return 0 if ok, EOF on errors.
+ */
+static int battfs_fileclose(struct KFile *fd)
+{
+ KFileBattFs *fdb = KFILEBATTFS(fd);
+
+ battfs_flush(fd);
+ REMOVE(&fdb->link);
+ return 0;
+}
+
+/**
+ * Read from file \a fd \a size bytes in \a buf.
+ * \return The number of bytes read.
+ */
+static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
+{
+ KFileBattFs *fdb = KFILEBATTFS(fd);
+ uint8_t *buf = (uint8_t *)_buf;
+
+ size_t total_read = 0;
+ pgoff_t pg_offset;
+ pgaddr_t addr_offset;
+ pgaddr_t read_len;
+
+ size = MIN(size, fd->size - fd->seek_pos);
+ while (size)
+ {
+ pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
+ addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
+ read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
+
+ /* Read from disk */
+ if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
+ {
+ #warning TODO set error?
+ }
+
+ size -= read_len;
+ fd->seek_pos += read_len;
+ total_read += read_len;
+ buf += read_len;
+ }
+ return total_read;
+}
+
+
+/**
+ * Search file \a inode in \a disk using a binary search.
+ * \return pointer to file start in disk->page_array
+ * if file exists, NULL otherwise.
+ */
+static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode)
+{
+ BattFsPageHeader hdr;
+ pgcnt_t first = 0, page, last = disk->page_count -1;
+ fcs_t fcs;
+
+ while (first <= last)
+ {
+ page = (first + last) / 2;
+
+ if (!battfs_readHeader(disk, disk->page_array[page], &hdr))
+ return NULL;
+
+ fcs = computeFcs(&hdr);
+ if (hdr.fcs == fcs && hdr.inode == inode)
+ return (&disk->page_array[page]) - hdr.pgoff;
+ else if (hdr.fcs == fcs && hdr.inode < inode)
+ first = page + 1;
+ else
+ last = page - 1;
+ }
+
+ return NULL;
+}
+
+/**
+ * \return true if file \a inode exists on \a disk, false otherwise.
+ */
+bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
+{
+ return findFile(disk, inode) != NULL;
+}
+
+/**
+ * Count size of file \a inode on \a disk, starting at pointer \a start
+ * in disk->page_array. Size is written in \a size.
+ * \return true if all s ok, false on disk read errors.
+ */
+static bool countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode, file_size_t *size)
+{
+ *size = 0;
+ BattFsPageHeader hdr;
+
+ for (;;)
+ {
+ if (!battfs_readHeader(disk, *start++, &hdr))
+ return false;
+ if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
+ *size += hdr.fill;
+ else
+ return true;
+ }
+}
+
+/**
+ * Open file \a inode from \a disk in \a mode.
+ * File context is stored in \a fd.
+ * \return true if ok, false otherwise.
+ */
+bool battfs_fileopen(BattFsSuper *disk, KFileBattFs *fd, inode_t inode, filemode_t mode)
+{
+ Node *n;
+
+ memset(fd, 0, sizeof(*fd));
+
+ /* Search file start point in disk page array */
+ fd->start = findFile(disk, inode);
+ if (fd->start == NULL)
+ {
+ if (!(mode & BATTFS_CREATE))
+ return false;
+
+ /* File does not exist, create it */
+ BattFsPageHeader hdr;
+ hdr.inode = inode;
+ hdr.seq = 0;
+ hdr.fill = 0;
+ hdr.pgoff = 0;
+ hdr.mark = MARK_PAGE_VALID;
+ hdr.fcs_free = FCS_FREE_VALID;
+ hdr.fcs = computeFcs(&hdr);
+ #warning TODO: get a free block and write on disk!
+ }
+
+ /* Fill file size */
+ if (!countFileSize(disk, fd->start, inode, &fd->fd.size))
+ return false;
+
+ /* Reset seek position */
+ fd->fd.seek_pos = 0;
+
+ /* Insert file handle in list, ordered by inode, ascending. */
+ FOREACH_NODE(n, &disk->file_opened_list)
+ {
+ KFileBattFs *file = containerof(n, KFileBattFs, link);
+ if (file->inode >= inode)
+ break;
+ }
+ INSERT_BEFORE(&fd->link, n);
+
+ /* Fill in data */
+ fd->inode = inode;
+ fd->mode = mode;
+ fd->disk = disk;
+
+ fd->fd.close = battfs_fileclose;
+ fd->fd.flush = battfs_flush;
+ fd->fd.read = battfs_read;
+ fd->fd.reopen = kfile_genericReopen;
+ fd->fd.seek = kfile_genericSeek;
+
+#warning TODO battfs_write, battfs_error, battfs_clearerr
+#if 0
+ fd->fd.write = battfs_write;
+ fd->fd.error = battfs_error;
+ fd->fd.clearerr = battfs_clearerr;
+#endif
+
+ DB(fd->fd._type = KFT_BATTFS);
+
+ return true;
+}
+
+/**
+ * Close \a disk.
+ */
+bool battfs_close(struct BattFsSuper *disk)
+{
+ Node *n;
+ int res = 0;
+
+ /* Close all open files */
+ FOREACH_NODE(n, &disk->file_opened_list)
+ {
+ KFileBattFs *file = containerof(n, KFileBattFs, link);
+ res += battfs_fileclose(&file->fd);
+ }
+
+ /* Close disk */
+ return disk->close(disk) && (res == 0);
+}
+
+
+bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff, mark_t mark)
+{
+ BattFsPageHeader hdr;
+
+ hdr.inode = inode;
+ hdr.seq = seq;
+ hdr.fill = fill;
+ hdr.pgoff = pgoff;
+ hdr.mark = MARK_PAGE_VALID;
+ hdr.fcs_free = FCS_FREE_VALID;
+ hdr.fcs = computeFcs(&hdr);
+ if (mark != MARK_PAGE_VALID)
+ {
+ hdr.mark = mark;
+ hdr.fcs_free = computeFcsFree(&hdr);
+ }
+
+ if (!battfs_writeHeader(disk, page, &hdr))
+ {
+ TRACEMSG("error writing hdr\n");
+ return false;
+ }
+
+ return true;
+}