X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Ffs%2Fbattfs.c;h=119085907637112386c7ef5094722166e981e127;hb=133c08ba914cdfe533693a1633fe4377b6e255f9;hp=e7e343ced9397b749c773248a4f3ebd2ba3b90c6;hpb=7a4746bfb1441cb97881c0d1f521192ab7d23f7a;p=bertos.git diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index e7e343ce..11908590 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -50,6 +50,19 @@ #include /* memset, memmove */ +#if LOG_LEVEL >= LOG_LVL_INFO +static void dumpPageArray(struct BattFsSuper *disk) +{ + 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)) + kputchar('\n'); + kprintf("%04d ", disk->page_array[i]); + } + kputchar('\n'); +} +#endif /** * Convert from memory representation to disk structure. @@ -145,7 +158,7 @@ static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHea if (diskRead(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) != BATTFS_HEADER_LEN) { - LOG_ERR("Error: page[%d]\n", page); + LOG_ERR("page[%d]\n", page); return false; } @@ -163,6 +176,8 @@ static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr) { uint8_t buf[BATTFS_HEADER_LEN]; + #warning FIXME:refactor computeFcs to save time and stack + hdr->fcs = computeFcs(hdr); /* Fill buffer */ battfs_to_disk(hdr, buf); @@ -174,12 +189,28 @@ static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr) if (disk->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) != BATTFS_HEADER_LEN) { - LOG_ERR("Error writing to buffer\n"); + LOG_ERR("writing to buffer\n"); return false; } 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. @@ -201,6 +232,7 @@ static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode) static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset) { pgcnt_t dst = src + offset; + LOG_INFO("src %d, offset %d, size %ld\n", src, offset, (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) @@ -368,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; @@ -381,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; @@ -392,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; } @@ -446,12 +474,11 @@ 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)) { - LOG_ERR("error counting file pages\n"); + LOG_ERR("counting file pages\n"); return false; } @@ -464,11 +491,16 @@ bool battfs_init(struct BattFsSuper *disk) /* Fill page allocation array using filelen_table */ if (!fillPageArray(disk, filelen_table)) { - LOG_ERR("error filling page array\n"); + 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; @@ -502,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) { @@ -511,17 +543,30 @@ static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, return false; } flushBuffer(disk); - LOG_INFO("Getting new page: %d\n", disk->page_array[disk->free_page_start]); + 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++]; - movePages(disk, new_pos, +1); - #warning TODO: move other files! + memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t)); + + 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); } /** @@ -537,10 +582,12 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) pgoff_t pg_offset; pgaddr_t addr_offset; pgaddr_t wr_len; - pgoff_t max_off = fd->size / (fdb->disk->page_size - BATTFS_HEADER_LEN); + + BattFsPageHeader curr_hdr; + if (!getBufferHdr(fdb->disk, &curr_hdr)) + return total_write; #warning TODO seek_pos > size? - size = MIN((kfile_off_t)size, fd->size - fd->seek_pos); while (size) { @@ -549,18 +596,18 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) wr_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset)); /* Handle write outside EOF */ - if (pg_offset > max_off) + if (pg_offset > fdb->max_off) { - if (!getNewPage(fdb->disk, (fdb->disk->page_array - fdb->start) + max_off, 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; - max_off = pg_offset; + fdb->max_off = pg_offset; } - /* Handle cache load of a new page*/ - if (fdb->start[pg_offset] != fdb->disk->curr_page) + 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; @@ -575,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? @@ -590,10 +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; - fdb->fd.size += fill_delta; + fd->size += fill_delta; + 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; } @@ -613,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) { @@ -627,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; @@ -639,7 +697,7 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size) /** * Search file \a inode in \a disk using a binary search. * \a last is filled with array offset of file start - * in disk->page_array if filse is found, otherwise + * in disk->page_array if file is found, otherwise * \a last is filled with the correct insert position * for creating a file with the given \a inode. * \return true if file is found, false otherwisr. @@ -654,14 +712,15 @@ static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last) while (first < *last) { page = (first + *last) / 2; - + 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) @@ -669,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; } @@ -692,15 +751,16 @@ static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inod file_size_t size = 0; BattFsPageHeader hdr; - for (;;) + while (start < &disk->page_array[disk->free_page_start]) { if (!readHdr(disk, *start++, &hdr)) return EOF; if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode) size += hdr.fill; else - return size; + break; } + return size; } /** @@ -718,17 +778,21 @@ bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mo pgcnt_t start_pos; if (!findFile(disk, inode, &start_pos)) { + LOG_INFO("file %d not found\n", inode); if (!(mode & BATTFS_CREATE)) return false; - - if (!(getNewPage(disk, start_pos, inode, 0))) + /* Create the file */ + BattFsPageHeader hdr; + if (!(getNewPage(disk, start_pos, inode, 0, &hdr))) return false; } fd->start = &disk->page_array[start_pos]; + LOG_INFO("Start pos %d\n", start_pos); /* Fill file size */ if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF) return false; + fd->max_off = fd->fd.size / (disk->page_size - BATTFS_HEADER_LEN); /* Reset seek position */ fd->fd.seek_pos = 0; @@ -785,6 +849,7 @@ bool battfs_close(struct BattFsSuper *disk) } #if UNIT_TEST + bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff) { BattFsPageHeader hdr;