X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Ffs%2Fbattfs.c;h=9bc8bdfd0b649b556743dcb1fae4b68eef509f6f;hb=c78bc5055c5fe1b424974f870abda1424166e5db;hp=aeacb9cd40b2cf294c1d9d90efc4142342eff32e;hpb=981ca46472cea3525828a899d83c9f3a1ed9c497;p=bertos.git diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index aeacb9cd..9bc8bdfd 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -44,9 +44,25 @@ #include /* MIN, MAX */ #include /* cpu_to_xx */ +#define LOG_LEVEL LOG_LVL_INFO +#define LOG_FORMAT LOG_FMT_VERBOSE +#include #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. @@ -64,25 +80,14 @@ INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf) buf[4] = hdr->pgoff >> 8; /* - * Mark is at least 1 bit longer than page address. - * Needed to take care of wraparonds. - */ - buf[5] = hdr->mark; - buf[6] = hdr->mark >> 8; - - /* - * 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) | ~(BV(7) | BV(6) | BV(0)); - - /* - * This field must be the before the last one! + * Sequence number is 40 bits long. + * No need to take care of wraparonds: the memory will die first! */ - buf[8] = hdr->fcs_free; - buf[9] = hdr->fcs_free >> 8; + buf[5] = hdr->seq; + buf[6] = hdr->seq >> 8; + buf[7] = hdr->seq >> 16; + buf[8] = hdr->seq >> 24; + buf[9] = hdr->seq >> 32; /* * This field must be the last one! @@ -103,9 +108,7 @@ INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr) hdr->inode = buf[0]; hdr->fill = buf[2] << 8 | buf[1]; hdr->pgoff = buf[4] << 8 | buf[3]; - hdr->mark = (mark_t)(buf[7] & 0x01) << 16 | buf[6] << 8 | buf[5]; - hdr->seq = buf[7] >> 6; - hdr->fcs_free = buf[9] << 8 | buf[8]; + hdr->seq = (seq_t)buf[9] << 32 | (seq_t)buf[8] << 24 | (seq_t)buf[7] << 16 | buf[6] << 8 | buf[5]; hdr->fcs = buf[11] << 8 | buf[10]; } @@ -125,26 +128,26 @@ static fcs_t computeFcs(struct BattFsPageHeader *hdr) } /** - * Compute the fcs of the header marked as free. + * Read from disk. + * If available, the cache will be used. + * \return the number of bytes read. */ -static fcs_t computeFcsFree(struct BattFsPageHeader *hdr) +static size_t diskRead(struct BattFsSuper *disk, pgcnt_t page, pgaddr_t addr, void *buf, size_t size) { - uint8_t buf[BATTFS_HEADER_LEN]; - fcs_t cks; - - battfs_to_disk(hdr, buf); - rotating_init(&cks); - /* fcs_free is just before fcs of whole header */ - rotating_update(buf, BATTFS_HEADER_LEN - 2 * sizeof(fcs_t), &cks); - return cks; + /* Try to read from cache */ + if (page == disk->curr_page) + return disk->bufferRead(disk, addr, buf, size); + /* Read from disk */ + else + return disk->read(disk, page, addr, buf, size); } /** - * Read header of page \a page. + * Read header of \a page in \a hdr. * \return true on success, false otherwise. */ -static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr) +static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr) { uint8_t buf[BATTFS_HEADER_LEN]; /* @@ -152,10 +155,10 @@ static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct Bat * Header is actually a footer, and so * resides at page end. */ - if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) + if (diskRead(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) != BATTFS_HEADER_LEN) { - TRACEMSG("Error: page[%d]\n", page); + LOG_ERR("page[%d]\n", page); return false; } @@ -166,27 +169,45 @@ static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct Bat } /** - * Write header of page \a page. + * Set header on current \a disk page buffer. * \return true on success, false otherwise. */ -static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr) +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); /* - * write header to disk. + * write header to buffer. * 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) + if (disk->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN) + != BATTFS_HEADER_LEN) + { + 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) { - TRACEMSG("Error: page[%d]\n", page); + LOG_ERR("reading from buffer\n"); return false; } + + disk_to_battfs(buf, hdr); + return true; } @@ -211,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) @@ -221,136 +243,6 @@ static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset) } } -/** - * 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. @@ -363,19 +255,12 @@ static void findFreeStartNext(struct BattFsSuper *disk, mark_t minl, mark_t maxl 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; - + disk->free_page_start = 0; /* Count the number of disk page per file */ for (pgcnt_t page = 0; page < disk->page_count; page++) { - if (!battfs_readHeader(disk, page, &hdr)) + if (!readHdr(disk, page, &hdr)) return false; /* Increase free space */ @@ -384,8 +269,6 @@ static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table) /* Check header FCS */ if (hdr.fcs == computeFcs(&hdr)) { - ASSERT(hdr.mark == MARK_PAGE_VALID); - ASSERT(hdr.fcs_free == FCS_FREE_VALID); ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN); /* Page is valid and is owned by a file */ @@ -393,32 +276,11 @@ static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table) /* Keep trace of free space */ disk->free_bytes -= hdr.fill; - } - else - { - /* Check if page is marked free */ - if (hdr.fcs_free == computeFcsFree(&hdr)) - { - /* - * This page is a valid and marked free page. - * Update min and max free page markers. - */ - if (hdr.mark < MARK_HALF_SIZE) - { - minl = MIN(minl, hdr.mark); - maxl = MAX(maxl, hdr.mark); - } - else - { - minh = MIN(minh, hdr.mark); - maxh = MAX(maxh, hdr.mark); - } - } - else - TRACEMSG("page [%d] invalid, keeping as free\n", page); + disk->free_page_start++; } } - findFreeStartNext(disk, minl, maxl, minh, maxh); + LOG_INFO("free_bytes:%d, free_page_start:%d\n", disk->free_bytes, disk->free_page_start); + return true; } @@ -431,62 +293,64 @@ static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table) * 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 - * and continuing with the marked free ones. + * Free blocks are allocated after the last file. * * \return true if ok, false on disk read errors. - * \note The whole disk is scanned once. + * \note The whole disk is scanned at max twice. */ static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table) { BattFsPageHeader hdr; + pgcnt_t curr_free_page = disk->free_page_start; /* Fill page allocation array */ for (pgcnt_t page = 0; page < disk->page_count; page++) { - if (!battfs_readHeader(disk, page, &hdr)) + if (!readHdr(disk, page, &hdr)) return false; /* Check header FCS */ if (hdr.fcs == computeFcs(&hdr)) { - /* Page is valid and is owned by a file */ - ASSERT(hdr.mark == MARK_PAGE_VALID); - ASSERT(hdr.fcs_free == FCS_FREE_VALID); - /* Compute array position */ pgcnt_t array_pos = countPages(filelen_table, hdr.inode); array_pos += hdr.pgoff; + /* Check if position is already used by another page of the same file */ - if (LIKELY(disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)) + if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL) disk->page_array[array_pos] = page; else { - BattFsPageHeader hdr_old; + BattFsPageHeader hdr_prv; - if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_old)) + if (!readHdr(disk, disk->page_array[array_pos], &hdr_prv)) return false; /* Check header FCS */ - ASSERT(hdr_old.fcs == computeFcs(&hdr_old)); + ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv)); /* Only the very same page with a different seq number can be here */ - ASSERT(hdr.inode == hdr_old.inode); - ASSERT(hdr.pgoff == hdr_old.pgoff); - ASSERT(hdr.mark == hdr_old.mark); - ASSERT(hdr.fcs_free == hdr_old.fcs_free); - ASSERT(hdr.seq != hdr_old.seq); + ASSERT(hdr.inode == hdr_prv.inode); + ASSERT(hdr.pgoff == hdr_prv.pgoff); + ASSERT(hdr.seq != hdr_prv.seq); pgcnt_t new_page, old_page; fill_t old_fill; - /* Fancy check to handle seq wraparound (2 bits only) */ - if (((hdr.seq - hdr_old.seq) & 0x03) < 2) + /* + * Sequence number comparison: since + * seq is 40 bits wide, it wraps once + * every 1.1E12 times. + * The memory will not live enough to + * see a wraparound, so we can use a simple + * compare here. + */ + if (hdr.seq > hdr_prv.seq) { /* Current header is newer than the previuos one */ old_page = disk->page_array[array_pos]; new_page = page; - old_fill = hdr_old.fill; + old_fill = hdr_prv.fill; } else { @@ -498,38 +362,77 @@ static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table) /* Set new page */ disk->page_array[array_pos] = new_page; - /* Add free space */ disk->free_bytes += old_fill; - /* Shift all array one position to the left, overwriting duplicate page */ array_pos -= hdr.pgoff; array_pos += filelen_table[hdr.inode]; movePages(disk, array_pos, -1); - - /* Decrease file page count */ + /* Move back all indexes */ filelen_table[hdr.inode]--; + disk->free_page_start--; + curr_free_page--; + /* Set old page as free */ + ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL); + disk->page_array[curr_free_page++] = old_page; - /* Add old page to free pages pool */ - if (!battfs_markFree(disk, &hdr, old_page)) - return false; - - insertFreePage(disk, hdr.mark, old_page); } } else { - /* Check if page is free */ - if (hdr.fcs_free != computeFcsFree(&hdr)) - /* Page is not a valid marked page, insert at list beginning */ - hdr.mark = --disk->free_start; - - insertFreePage(disk, hdr.mark, page); + /* Invalid page, keep as free */ + ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL); + LOG_INFO("Page %d invalid, keeping as free\n", page); + disk->page_array[curr_free_page++] = page; } } return true; } + +/** + * Flush the current \a disk buffer. + * \return true if ok, false on errors. + */ +static bool flushBuffer(struct BattFsSuper *disk) +{ + if (disk->cache_dirty) + { + LOG_INFO("Flushing to disk page %d\n", disk->curr_page); + + if (!(disk->erase(disk, disk->curr_page) + && disk->save(disk, disk->curr_page))) + return false; + + disk->cache_dirty = false; + } + return true; +} + +/** + * 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. 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, BattFsPageHeader *new_hdr) +{ + if (disk->curr_page == new_page) + return true; + + LOG_INFO("Loading page %d\n", new_page); + + if (!(flushBuffer(disk) + && disk->load(disk, new_page) + && getBufferHdr(disk, new_hdr))) + return false; + + disk->curr_page = new_page; + + return true; +} + + /** * Initialize and mount disk described by * \a disk. @@ -545,13 +448,16 @@ bool battfs_init(struct BattFsSuper *disk) /* Init disk device */ if (!disk->open(disk)) { - TRACEMSG("open error\n"); + LOG_ERR("open error\n"); return false; } /* Disk open must set all of these */ ASSERT(disk->read); - ASSERT(disk->write); + ASSERT(disk->load); + ASSERT(disk->bufferWrite); + ASSERT(disk->bufferRead); + ASSERT(disk->save); ASSERT(disk->erase); ASSERT(disk->close); ASSERT(disk->page_size); @@ -564,10 +470,15 @@ bool battfs_init(struct BattFsSuper *disk) disk->free_bytes = 0; disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count; + /* Initialize page buffer cache */ + disk->cache_dirty = false; + disk->curr_page = 0; + disk->load(disk, disk->curr_page); + /* Count pages per file */ if (!countDiskFilePages(disk, filelen_table)) { - TRACEMSG("error counting file pages\n"); + LOG_ERR("counting file pages\n"); return false; } @@ -580,10 +491,16 @@ bool battfs_init(struct BattFsSuper *disk) /* Fill page allocation array using filelen_table */ if (!fillPageArray(disk, filelen_table)) { - TRACEMSG("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; @@ -595,9 +512,12 @@ bool battfs_init(struct BattFsSuper *disk) */ static int battfs_flush(struct KFile *fd) { - (void)fd; - #warning TODO - return 0; + BattFs *fdb = BATTFS_CAST(fd); + + if (flushBuffer(fdb->disk)) + return 0; + else + return EOF; } /** @@ -606,20 +526,138 @@ static int battfs_flush(struct KFile *fd) */ static int battfs_fileclose(struct KFile *fd) { - BattFS *fdb = BATTFSKFILE(fd); + BattFs *fdb = BATTFS_CAST(fd); battfs_flush(fd); REMOVE(&fdb->link); return 0; } + +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) + { + #warning TODO space over! + LOG_INFO("No disk space available\n"); + return false; + } + flushBuffer(disk); + 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)); + + 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; + + new_hdr->inode = inode; + new_hdr->pgoff = pgoff; + new_hdr->fill = 0; + new_hdr->seq = 0; + return setBufferHdr(disk, new_hdr); +} + +/** + * Write to file \a fd \a size bytes from \a buf. + * \return The number of bytes written. + */ +static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) +{ + BattFs *fdb = BATTFS_CAST(fd); + const uint8_t *buf = (const uint8_t *)_buf; + + size_t total_write = 0; + pgoff_t pg_offset; + 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) + { + pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN); + addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN); + wr_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset)); + + /* Handle write outside EOF */ + if (pg_offset > fdb->max_off) + { + 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; + } + /* Handle cache load of a new 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], &curr_hdr)) + { + #warning TODO set error? + return total_write; + } + + /* Get a free page */ + fdb->disk->curr_page = fdb->disk->page_array[fdb->disk->free_page_start]; + movePages(fdb->disk, fdb->disk->free_page_start + 1, -1); + + /* Insert previous page in free blocks list */ + LOG_INFO("Setting page %d as free\n", fdb->start[pg_offset]); + fdb->disk->page_array[fdb->disk->page_count - 1] = fdb->start[pg_offset]; + /* Assign new page */ + fdb->start[pg_offset] = fdb->disk->curr_page; + curr_hdr.seq++; + } + + + //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? + } + fdb->disk->cache_dirty = true; + + size -= wr_len; + fd->seek_pos += wr_len; + total_write += wr_len; + buf += wr_len; + fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0); + fdb->disk->free_bytes -= 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; +} + + /** * 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) { - BattFS *fdb = BATTFSKFILE(fd); + BattFs *fdb = BATTFS_CAST(fd); uint8_t *buf = (uint8_t *)_buf; size_t total_read = 0; @@ -636,11 +674,17 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size) 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) + if (diskRead(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len) { #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; @@ -652,32 +696,40 @@ static size_t battfs_read(struct KFile *fd, void *_buf, size_t size) /** * 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. + * \a last is filled with array offset of file start + * 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. */ -static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode) +static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last) { BattFsPageHeader hdr; - pgcnt_t first = 0, page, last = disk->page_count -1; + pgcnt_t first = 0, page; + *last = disk->free_page_start; fcs_t fcs; - while (first <= last) + while (first < *last) { - page = (first + last) / 2; - - if (!battfs_readHeader(disk, disk->page_array[page], &hdr)) - return NULL; - + 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) - return (&disk->page_array[page]) - hdr.pgoff; + { + *last = page - hdr.pgoff; + LOG_INFO("Found: %d\n", *last); + return true; + } else if (hdr.fcs == fcs && hdr.inode < inode) first = page + 1; else - last = page - 1; + *last = page - 1; } - - return NULL; + LOG_INFO("Not found: last %d\n", *last); + return false; } /** @@ -685,7 +737,8 @@ static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode) */ bool battfs_fileExists(BattFsSuper *disk, inode_t inode) { - return findFile(disk, inode) != NULL; + pgcnt_t dummy; + return findFile(disk, inode, &dummy); } /** @@ -693,20 +746,21 @@ bool battfs_fileExists(BattFsSuper *disk, inode_t inode) * 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) +static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode) { - *size = 0; + file_size_t size = 0; BattFsPageHeader hdr; - for (;;) + while (start < &disk->page_array[disk->free_page_start]) { - if (!battfs_readHeader(disk, *start++, &hdr)) - return false; + if (!readHdr(disk, *start++, &hdr)) + return EOF; if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode) - *size += hdr.fill; + size += hdr.fill; else - return true; + break; } + return size; } /** @@ -714,34 +768,31 @@ static bool countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode, file * 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; memset(fd, 0, sizeof(*fd)); /* Search file start point in disk page array */ - fd->start = findFile(disk, inode); - if (fd->start == NULL) + pgcnt_t start_pos; + if (!findFile(disk, inode, &start_pos)) { + LOG_INFO("file %d not found\n", inode); if (!(mode & BATTFS_CREATE)) return false; - - /* File does not exist, create it */ + /* Create the file */ 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! + 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 (!countFileSize(disk, fd->start, inode, &fd->fd.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; @@ -749,7 +800,7 @@ bool battfs_fileopen(BattFsSuper *disk, BattFS *fd, inode_t inode, filemode_t mo /* 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; } @@ -765,10 +816,10 @@ bool battfs_fileopen(BattFsSuper *disk, BattFS *fd, inode_t inode, filemode_t mo fd->fd.read = battfs_read; fd->fd.reopen = kfile_genericReopen; fd->fd.seek = kfile_genericSeek; + fd->fd.write = battfs_write; -#warning TODO battfs_write, battfs_error, battfs_clearerr +#warning TODO battfs_error, battfs_clearerr #if 0 - fd->fd.write = battfs_write; fd->fd.error = battfs_error; fd->fd.clearerr = battfs_clearerr; #endif @@ -789,7 +840,7 @@ bool battfs_close(struct BattFsSuper *disk) /* 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); } @@ -797,29 +848,29 @@ bool battfs_close(struct BattFsSuper *disk) return disk->close(disk) && (res == 0); } +#if UNIT_TEST -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; + /* Reset page to all 0xff */ + uint8_t buf[disk->page_size]; + memset(buf, 0xFF, disk->page_size); + disk->bufferWrite(disk, 0, buf, disk->page_size); + 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)) + if (!(setBufferHdr(disk, &hdr) && disk->save(disk, page))) { - TRACEMSG("error writing hdr\n"); + LOG_ERR("error writing hdr\n"); return false; } return true; } +#endif