#include <cfg/macros.h> /* MIN, MAX */
#include <cpu/byteorder.h> /* cpu_to_xx */
+#include <cfg/log.h>
#include <string.h> /* memset, memmove */
* \return the number of old versions of the page or PAGE_ERROR
* on disk read errors.
*/
-static pgcnt_t findLastVersion(pgcnt_t *page_array)
+static pgcnt_t findLastVersion(struct BattFsSuper *disk, pgcnt_t *page_array)
{
pgcnt_t *array_start = page_array;
BattFsPageHeader hdr;
/* Temps used to find the sequence number range */
seq_t minl = HALF_SEQ - 1;
seq_t maxl = 0;
- seq_t minh = FULL_SEQ;
- seq_t maxh = HALF_SEQ;
+ seq_t minh = MAX_SEQ;
+ seq_t maxh = MAX_SEQ;
pgcnt_t lpos = 0, hpos = 0, dup_cnt = 0;
/*
*/
do
{
- if (hdr.seq < SEQ_HALF_SIZE)
+ if (hdr.seq < HALF_SEQ)
{
minl = MIN(minl, hdr.seq);
if (hdr.seq > maxl)
return PAGE_ERROR;
dup_cnt++;
}
- while (curr_inode == hdr.inode && curr_pgoff == hdr.pgoff && hdr.fcs == computeFcs(&hdr))
+ while (curr_inode == hdr.inode && curr_pgoff == hdr.pgoff && hdr.fcs == computeFcs(&hdr));
/* Return early if there is only one version of the current page */
{
/* Interval starts in upper half and ends in lower */
ASSERT(minl == 0);
- ASSERT(maxh == FULL_SEQ);
+ ASSERT(maxh == MAX_SEQ);
last_ver = lpos;
}
do
{
- dup_pages = findLastVersion(curr_page);
+ dup_pages = findLastVersion(disk, curr_page);
if (dup_pages == PAGE_ERROR)
return false;
/* The first page is the last version */
*/
static int battfs_fileclose(struct KFile *fd)
{
- BattFS *fdb = BATTFSKFILE(fd);
+ BattFs *fdb = BATTFS_CAST(fd);
battfs_flush(fd);
REMOVE(&fdb->link);
*/
static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
{
- BattFS *fdb = BATTFSKFILE(fd);
+ BattFs *fdb = BATTFS_CAST(fd);
uint8_t *buf = (uint8_t *)_buf;
size_t total_read = 0;
* File context is stored in \a fd.
* \return true if ok, false otherwise.
*/
-bool battfs_fileopen(BattFsSuper *disk, BattFS *fd, inode_t inode, filemode_t mode)
+bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
{
Node *n;
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!
}
/* Insert file handle in list, ordered by inode, ascending. */
FOREACH_NODE(n, &disk->file_opened_list)
{
- BattFS *file = containerof(n, BattFS, link);
+ BattFs *file = containerof(n, BattFs, link);
if (file->inode >= inode)
break;
}
/* Close all open files */
FOREACH_NODE(n, &disk->file_opened_list)
{
- BattFS *file = containerof(n, BattFS, link);
+ BattFs *file = containerof(n, BattFs, link);
res += battfs_fileclose(&file->fd);
}
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)
+bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
{
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.seq = seq;
hdr.fcs = computeFcs(&hdr);
- if (mark != MARK_PAGE_VALID)
- {
- hdr.mark = mark;
- hdr.fcs_free = computeFcsFree(&hdr);
- }
if (!battfs_writeHeader(disk, page, &hdr))
{
for (int i = 0; i < PAGE_COUNT; i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i] = i;
}
fclose(fp);
for (int i = 0; i < PAGE_COUNT / 2; i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i] = i;
}
fseek(fp, FILE_SIZE / 2, SEEK_SET);
for (int i = 0; i < PAGE_COUNT / 2; i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i] = i;
}
for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i] = i;
}
fclose(fp);
for (int i = 0; i < PAGE_COUNT / 3; i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i] = i;
}
for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
{
- battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
+ battfs_writeTestBlock(disk, i, 0, 0, 0, i);
ref[i + PAGE_COUNT / 3 + 1] = i;
}
fclose(fp);
fp = fopen(test_filename, "w+");
- battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 1, 0, 0, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
+ battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 1, 0, 0, 0, 1);
+ battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
+ battfs_writeTestBlock(disk, 3, 0, 0, 0, 0);
fclose(fp);
ref[0] = 0;
fp = fopen(test_filename, "w+");
- battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 1, 0, 1, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, 0, 0, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
+ battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 1, 0, 1, 0, 1);
+ battfs_writeTestBlock(disk, 2, 0, 0, 0, 1);
+ battfs_writeTestBlock(disk, 3, 0, 0, 0, 0);
fclose(fp);
ref[0] = 0;
fp = fopen(test_filename, "w+");
- battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
- battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
+ battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
fclose(fp);
fp = fopen(test_filename, "w+");
- battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
- battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, 0, 3, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 4, 0, 0, 0, 0, 1236);
- battfs_writeTestBlock(disk, 5, 4, 0, 0, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 6, 4, 1, 0, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 7, 4, 0, 0, 1, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 2, 0, 3, 0, 1);
+ battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
+ battfs_writeTestBlock(disk, 4, 0, 0, 0, 0);
+ battfs_writeTestBlock(disk, 5, 4, 0, 0, 0);
+ battfs_writeTestBlock(disk, 6, 4, 1, 0, 1);
+ battfs_writeTestBlock(disk, 7, 4, 0, 0, 1);
fclose(fp);
static void test10(BattFsSuper *disk)
{
- BattFS fd1;
- BattFS fd2;
+ BattFs fd1;
+ BattFs fd2;
kprintf("Test10: open file test, inode 0 and inode 4\n");
fp = fopen(test_filename, "w+");
- unsigned int PAGE_FILL = 116;
+ int PAGE_FILL = 116;
unsigned int INODE = 0;
unsigned int INODE2 = 4;
unsigned int INEXISTENT_INODE = 123;
unsigned int MODE = 0;
- battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
- battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
- battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
fclose(fp);
static void test11(BattFsSuper *disk)
{
- BattFS fd1;
+ BattFs fd1;
uint8_t buf[16];
kprintf("Test11: read file test\n");
unsigned int INODE2 = 4;
unsigned int MODE = 0;
- battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
- battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
- battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
fclose(fp);
static void test12(BattFsSuper *disk)
{
- BattFS fd1;
+ BattFs fd1;
kprintf("Test12: read file test across page boundary and seek test\n");
unsigned int MODE = 0;
uint8_t buf[PAGE_FILL + 10];
- battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
- battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 4, INODE, 0, PAGE_FILL, 0, 1236);
- battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3, MARK_PAGE_VALID);
- battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3, MARK_PAGE_VALID);
+ battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
+ battfs_writeTestBlock(disk, 4, INODE, 0, PAGE_FILL, 0);
+ battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2);
+ battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3);
+ battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3);
fclose(fp);