Handle reading outside EOF.
[bertos.git] / bertos / fs / battfs.c
index b15460a0c722c6f8fab0ed4419e62da823349570..119085907637112386c7ef5094722166e981e127 100644 (file)
 #include <cfg/macros.h> /* MIN, MAX */
 #include <cpu/byteorder.h> /* cpu_to_xx */
 
-#define LOG_LEVEL       LOG_LVL_WARN
+#define LOG_LEVEL       LOG_LVL_INFO
 #define LOG_FORMAT      LOG_FMT_VERBOSE
 #include <cfg/log.h>
 
 #include <string.h> /* memset, memmove */
 
-#ifdef _DEBUG
+#if LOG_LEVEL >= LOG_LVL_INFO
 static void dumpPageArray(struct BattFsSuper *disk)
 {
-       kprintf("Page array dump:");
+       kprintf("Page array dump, free_page_start %d:", disk->free_page_start);
        for (pgcnt_t i = 0; i < disk->page_count; i++)
        {
                if (!(i % 16))
@@ -195,6 +195,22 @@ static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
        return true;
 }
 
+static bool getBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
+{
+       uint8_t buf[BATTFS_HEADER_LEN];
+
+       if (disk->bufferRead(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+           != BATTFS_HEADER_LEN)
+       {
+               LOG_ERR("reading from buffer\n");
+               return false;
+       }
+
+       disk_to_battfs(buf, hdr);
+
+       return true;
+}
+
 /**
  * Count the number of pages from
  * inode 0 to \a inode in \a filelen_table.
@@ -384,8 +400,7 @@ static bool flushBuffer(struct BattFsSuper *disk)
        {
                LOG_INFO("Flushing to disk page %d\n", disk->curr_page);
 
-               if (!(setBufferHdr(disk, &disk->curr_hdr)
-                       && disk->erase(disk, disk->curr_page)
+               if (!(disk->erase(disk, disk->curr_page)
                        && disk->save(disk, disk->curr_page)))
                        return false;
 
@@ -397,10 +412,10 @@ static bool flushBuffer(struct BattFsSuper *disk)
 /**
  * Load \a new_page from \a disk in disk page buffer.
  * If a previuos page is still dirty in the buffer, will be
- * flushed first.
+ * flushed first. The new page header loaded will be put in \a new_hdr.
  * \return true if ok, false on errors.
  */
-static bool loadPage(struct BattFsSuper *disk, pgcnt_t new_page)
+static bool loadPage(struct BattFsSuper *disk, pgcnt_t new_page, BattFsPageHeader *new_hdr)
 {
        if (disk->curr_page == new_page)
                return true;
@@ -408,15 +423,12 @@ static bool loadPage(struct BattFsSuper *disk, pgcnt_t new_page)
        LOG_INFO("Loading page %d\n", new_page);
 
        if (!(flushBuffer(disk)
-               && disk->load(disk, new_page)))
+               && disk->load(disk, new_page)
+               && getBufferHdr(disk, new_hdr)))
                return false;
 
        disk->curr_page = new_page;
 
-       /* Load current header */
-       if (!readHdr(disk, disk->curr_page, &disk->curr_hdr))
-               return false;
-
        return true;
 }
 
@@ -462,7 +474,6 @@ bool battfs_init(struct BattFsSuper *disk)
        disk->cache_dirty = false;
        disk->curr_page = 0;
        disk->load(disk, disk->curr_page);
-       readHdr(disk, disk->curr_page, &disk->curr_hdr);
 
        /* Count pages per file */
        if (!countDiskFilePages(disk, filelen_table))
@@ -483,8 +494,13 @@ bool battfs_init(struct BattFsSuper *disk)
                LOG_ERR("filling page array\n");
                return false;
        }
+       #if LOG_LEVEL > LOG_LVL_INFO
+               dumpPageArray(disk)
+       #endif
        #warning TODO: shuffle free blocks
-
+       //#if LOG_LEVEL > LOG_LVL_INFO
+       //      dumpPageArray(disk)
+       //#endif
        /* Init list for opened files. */
        LIST_INIT(&disk->file_opened_list);
        return true;
@@ -518,7 +534,7 @@ static int battfs_fileclose(struct KFile *fd)
 }
 
 
-static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff)
+static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
 {
        if (disk->free_page_start >= disk->page_count)
        {
@@ -530,15 +546,27 @@ static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode,
        LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
        disk->curr_page = disk->page_array[disk->free_page_start++];
        memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
-       #warning TODO: move other files!
+
+       Node *n;
+       /* Move following file start point one position ahead. */
+       FOREACH_NODE(n, &disk->file_opened_list)
+       {
+               BattFs *file = containerof(n, BattFs, link);
+               if (file->inode > inode)
+               {
+                       LOG_INFO("Move file %d start pos\n", file->inode);
+                       file->start++;
+               }
+       }
+
        disk->page_array[new_pos] = disk->curr_page;
        disk->cache_dirty = true;
 
-       disk->curr_hdr.inode = inode;
-       disk->curr_hdr.pgoff =  pgoff;
-       disk->curr_hdr.fill = 0;
-       disk->curr_hdr.seq = 0;
-       return true;
+       new_hdr->inode = inode;
+       new_hdr->pgoff =  pgoff;
+       new_hdr->fill = 0;
+       new_hdr->seq = 0;
+       return setBufferHdr(disk, new_hdr);
 }
 
 /**
@@ -555,6 +583,10 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
        pgaddr_t addr_offset;
        pgaddr_t wr_len;
 
+       BattFsPageHeader curr_hdr;
+       if (!getBufferHdr(fdb->disk, &curr_hdr))
+               return total_write;
+
        #warning TODO seek_pos > size?
 
        while (size)
@@ -566,7 +598,8 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
                /* Handle write outside EOF */
                if (pg_offset > fdb->max_off)
                {
-                       if (!getNewPage(fdb->disk, (fdb->disk->page_array - fdb->start) + pg_offset, fdb->inode, pg_offset))
+                       LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (fdb->start - fdb->disk->page_array) + pg_offset);
+                       if (!getNewPage(fdb->disk, (fdb->start - fdb->disk->page_array) + pg_offset, fdb->inode, pg_offset, &curr_hdr))
                                return total_write;
                        fdb->max_off = pg_offset;
                }
@@ -574,7 +607,7 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
                else if (fdb->start[pg_offset] != fdb->disk->curr_page)
                {
                        LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], fdb->disk->page_array[fdb->disk->free_page_start]);
-                       if (!loadPage(fdb->disk, fdb->start[pg_offset]))
+                       if (!loadPage(fdb->disk, fdb->start[pg_offset], &curr_hdr))
                        {
                                #warning TODO set error?
                                return total_write;
@@ -589,11 +622,11 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
                        fdb->disk->page_array[fdb->disk->page_count - 1] = fdb->start[pg_offset];
                        /* Assign new page */
                        fdb->start[pg_offset] = fdb->disk->curr_page;
-                       fdb->disk->curr_hdr.seq++;
+                       curr_hdr.seq++;
                }
 
 
-               LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", fdb->disk->curr_page, addr_offset, wr_len);
+               //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", fdb->disk->curr_page, addr_offset, wr_len);
                if (fdb->disk->bufferWrite(fdb->disk, addr_offset, buf, wr_len) != wr_len)
                {
                        #warning TODO set error?
@@ -604,11 +637,15 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
                fd->seek_pos += wr_len;
                total_write += wr_len;
                buf += wr_len;
-               fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - fdb->disk->curr_hdr.fill, (int32_t)0);
+               fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
                fdb->disk->free_bytes -= fill_delta;
-               fdb->disk->curr_hdr.fill += fill_delta;
                fd->size += fill_delta;
-               LOG_INFO("free_bytes %d, seek_pos %d, size %d, curr_hdr.fill %d\n", fdb->disk->free_bytes, fd->seek_pos, fd->size, fdb->disk->curr_hdr.fill);
+               curr_hdr.fill += fill_delta;
+
+               if (!setBufferHdr(fdb->disk, &curr_hdr))
+                       return total_write;
+
+               //LOG_INFO("free_bytes %d, seek_pos %d, size %d, curr_hdr.fill %d\n", fdb->disk->free_bytes, fd->seek_pos, fd->size, curr_hdr.fill);
        }
        return total_write;
 }
@@ -628,7 +665,7 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
        pgaddr_t addr_offset;
        pgaddr_t read_len;
 
-       size = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
+       size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, 0));
 
        while (size)
        {
@@ -642,6 +679,12 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
                        #warning TODO set error?
                }
 
+               #if _DEBUG
+                       BattFsPageHeader hdr;
+                       readHdr(fdb->disk, fdb->start[pg_offset], &hdr);
+                       ASSERT(hdr.inode == fdb->inode);
+               #endif
+
                size -= read_len;
                fd->seek_pos += read_len;
                total_read += read_len;
@@ -672,11 +715,12 @@ static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
                LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
                if (!readHdr(disk, disk->page_array[page], &hdr))
                        return false;
-
+               LOG_INFO("inode read: %d\n", hdr.inode);
                fcs = computeFcs(&hdr);
                if (hdr.fcs == fcs && hdr.inode == inode)
                {
                        *last = page - hdr.pgoff;
+                       LOG_INFO("Found: %d\n", *last);
                        return true;
                }
                else if (hdr.fcs == fcs && hdr.inode < inode)
@@ -684,7 +728,7 @@ static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
                else
                        *last = page - 1;
        }
-
+       LOG_INFO("Not found: last %d\n", *last);
        return false;
 }
 
@@ -738,7 +782,8 @@ bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mo
                if (!(mode & BATTFS_CREATE))
                        return false;
                /* Create the file */
-               if (!(getNewPage(disk, start_pos, inode, 0)))
+               BattFsPageHeader hdr;
+               if (!(getNewPage(disk, start_pos, inode, 0, &hdr)))
                        return false;
        }
        fd->start = &disk->page_array[start_pos];