4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief BattFS: a filesystem for embedded platforms (implementation).
37 * \author Francesco Sacchi <batt@develer.com>
42 #include "cfg/cfg_battfs.h"
43 #include <cfg/debug.h>
44 #include <cfg/macros.h> /* MIN, MAX */
46 #include <cpu/byteorder.h> /* cpu_to_xx */
48 #define LOG_LEVEL BATTFS_LOG_LEVEL
49 #define LOG_FORMAT BATTFS_LOG_FORMAT
52 #include <string.h> /* memset, memmove */
54 #if LOG_LEVEL >= LOG_LVL_INFO
55 static void dumpPageArray(struct BattFsSuper *disk)
57 kprintf("Page array dump, free_page_start %d:", disk->free_page_start);
58 for (pgcnt_t i = 0; i < disk->page_count; i++)
62 kprintf("%04d ", disk->page_array[i]);
69 * Convert from memory representation to disk structure.
70 * \note filesystem is in little-endian format.
72 INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf)
74 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
78 buf[2] = hdr->fill >> 8;
81 buf[4] = hdr->pgoff >> 8;
84 * Sequence number is 40 bits long.
85 * No need to take care of wraparonds: the memory will die first!
88 buf[6] = hdr->seq >> 8;
89 buf[7] = hdr->seq >> 16;
90 buf[8] = hdr->seq >> 24;
91 buf[9] = hdr->seq >> 32;
94 * This field must be the last one!
95 * This is needed because if the page is only partially
96 * written, we can use this to detect it.
99 buf[11] = hdr->fcs >> 8;
103 * Convert from disk structure to memory representation.
104 * \note filesystem is in little-endian format.
106 INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr)
108 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
110 hdr->fill = buf[2] << 8 | buf[1];
111 hdr->pgoff = buf[4] << 8 | buf[3];
112 hdr->seq = (seq_t)buf[9] << 32 | (seq_t)buf[8] << 24 | (seq_t)buf[7] << 16 | buf[6] << 8 | buf[5];
113 hdr->fcs = buf[11] << 8 | buf[10];
117 * Compute the fcs of the header.
119 static fcs_t computeFcs(struct BattFsPageHeader *hdr)
121 uint8_t buf[BATTFS_HEADER_LEN];
124 battfs_to_disk(hdr, buf);
126 /* fcs is at the end of whole header */
127 rotating_update(buf, BATTFS_HEADER_LEN - sizeof(fcs_t), &cks);
133 * If available, the cache will be used.
134 * \return the number of bytes read.
136 static size_t diskRead(struct BattFsSuper *disk, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
138 /* Try to read from cache */
139 if (page == disk->curr_page)
140 return disk->bufferRead(disk, addr, buf, size);
143 return disk->read(disk, page, addr, buf, size);
148 * Read header of \a page in \a hdr.
149 * \return true on success, false otherwise.
151 static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
153 uint8_t buf[BATTFS_HEADER_LEN];
155 * Read header from disk.
156 * Header is actually a footer, and so
157 * resides at page end.
159 if (diskRead(disk, page, disk->data_size, buf, BATTFS_HEADER_LEN)
160 != BATTFS_HEADER_LEN)
162 LOG_ERR("page[%d]\n", page);
167 disk_to_battfs(buf, hdr);
173 * Set header on current \a disk page buffer.
174 * \return true on success, false otherwise.
176 static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
178 uint8_t buf[BATTFS_HEADER_LEN];
180 #warning FIXME:refactor computeFcs to save time and stack
181 hdr->fcs = computeFcs(hdr);
183 battfs_to_disk(hdr, buf);
186 * write header to buffer.
187 * Header is actually a footer, and so
188 * resides at page end.
190 if (disk->bufferWrite(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
191 != BATTFS_HEADER_LEN)
193 LOG_ERR("writing to buffer\n");
199 static bool getBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
201 uint8_t buf[BATTFS_HEADER_LEN];
203 if (disk->bufferRead(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
204 != BATTFS_HEADER_LEN)
206 LOG_ERR("reading from buffer\n");
210 disk_to_battfs(buf, hdr);
216 * Count the number of pages from
217 * inode 0 to \a inode in \a filelen_table.
219 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
223 for (inode_t i = 0; i < inode; i++)
224 cnt += filelen_table[i];
230 * Move all pages in page allocation array from \a src to \a src + \a offset.
231 * The number of pages moved is page_count - MAX(dst, src).
233 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
235 pgcnt_t dst = src + offset;
236 LOG_INFO("src %d, offset %d, size %d\n", src, offset, (unsigned int)((disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t)));
237 memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
241 /* Fill empty space in array with sentinel */
242 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
243 disk->page_array[page] = PAGE_UNSET_SENTINEL;
248 * Count number of pages per file on \a disk.
249 * This information is registered in \a filelen_table.
250 * Array index represent file inode, while value contained
251 * is the number of pages used by that file.
253 * \return true if ok, false on disk read errors.
254 * \note The whole disk is scanned once.
256 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
258 BattFsPageHeader hdr;
259 disk->free_page_start = 0;
261 /* Count the number of disk page per file */
262 for (pgcnt_t page = 0; page < disk->page_count; page++)
264 if (!readHdr(disk, page, &hdr))
267 /* Increase free space */
268 disk->free_bytes += disk->data_size;
270 /* Check header FCS */
271 if (hdr.fcs == computeFcs(&hdr))
273 ASSERT(hdr.fill <= disk->data_size);
275 /* Page is valid and is owned by a file */
276 filelen_table[hdr.inode]++;
278 /* Keep trace of free space */
279 disk->free_bytes -= hdr.fill;
280 disk->free_page_start++;
283 LOG_INFO("free_bytes:%ld, free_page_start:%d\n", (long)disk->free_bytes, disk->free_page_start);
289 * Fill page allocation array of \a disk
290 * using file lenghts in \a filelen_table.
292 * The page allocation array is an array containings all file infos.
293 * Is ordered by file, and within each file is ordered by page offset
295 * e.g. : at page array[0] you will find page address of the first page
296 * of the first file (if present).
297 * Free blocks are allocated after the last file.
299 * \return true if ok, false on disk read errors.
300 * \note The whole disk is scanned at max twice.
302 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
304 BattFsPageHeader hdr;
305 pgcnt_t curr_free_page = disk->free_page_start;
306 /* Fill page allocation array */
307 for (pgcnt_t page = 0; page < disk->page_count; page++)
309 if (!readHdr(disk, page, &hdr))
312 /* Check header FCS */
313 if (hdr.fcs == computeFcs(&hdr))
315 /* Compute array position */
316 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
317 array_pos += hdr.pgoff;
320 /* Check if position is already used by another page of the same file */
321 if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
322 disk->page_array[array_pos] = page;
325 BattFsPageHeader hdr_prv;
327 if (!readHdr(disk, disk->page_array[array_pos], &hdr_prv))
330 /* Check header FCS */
331 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
333 /* Only the very same page with a different seq number can be here */
334 ASSERT(hdr.inode == hdr_prv.inode);
335 ASSERT(hdr.pgoff == hdr_prv.pgoff);
336 ASSERT(hdr.seq != hdr_prv.seq);
338 pgcnt_t new_page, old_page;
342 * Sequence number comparison: since
343 * seq is 40 bits wide, it wraps once
344 * every 1.1E12 times.
345 * The memory will not live enough to
346 * see a wraparound, so we can use a simple
349 if (hdr.seq > hdr_prv.seq)
351 /* Current header is newer than the previuos one */
352 old_page = disk->page_array[array_pos];
354 old_fill = hdr_prv.fill;
358 /* Previous header is newer than the current one */
360 new_page = disk->page_array[array_pos];
365 disk->page_array[array_pos] = new_page;
367 disk->free_bytes += old_fill;
368 /* Shift all array one position to the left, overwriting duplicate page */
369 array_pos -= hdr.pgoff;
370 array_pos += filelen_table[hdr.inode];
371 movePages(disk, array_pos, -1);
372 /* Move back all indexes */
373 filelen_table[hdr.inode]--;
374 disk->free_page_start--;
376 /* Set old page as free */
377 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
378 disk->page_array[curr_free_page++] = old_page;
384 /* Invalid page, keep as free */
385 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
386 //LOG_INFO("Page %d invalid, keeping as free\n", page);
387 disk->page_array[curr_free_page++] = page;
395 * Flush the current \a disk buffer.
396 * \return true if ok, false on errors.
398 static bool flushBuffer(struct BattFsSuper *disk)
400 if (disk->cache_dirty)
402 LOG_INFO("Flushing to disk page %d\n", disk->curr_page);
404 if (!(disk->erase(disk, disk->curr_page)
405 && disk->save(disk, disk->curr_page)))
408 disk->cache_dirty = false;
414 * Load \a new_page from \a disk in disk page buffer.
415 * If a previuos page is still dirty in the buffer, will be
416 * flushed first. The new page header loaded will be put in \a new_hdr.
417 * \return true if ok, false on errors.
419 static bool loadPage(struct BattFsSuper *disk, pgcnt_t new_page, BattFsPageHeader *new_hdr)
421 if (disk->curr_page == new_page)
422 return getBufferHdr(disk, new_hdr);
424 LOG_INFO("Loading page %d\n", new_page);
426 if (!(flushBuffer(disk)
427 && disk->load(disk, new_page)
428 && getBufferHdr(disk, new_hdr)))
431 disk->curr_page = new_page;
438 * Initialize and mount disk described by
440 * \return false on errors, true otherwise.
442 bool battfs_mount(struct BattFsSuper *disk)
444 pgoff_t filelen_table[BATTFS_MAX_FILES];
446 /* Disk open must set all of these */
449 ASSERT(disk->bufferWrite);
450 ASSERT(disk->bufferRead);
454 ASSERT(disk->page_size > BATTFS_HEADER_LEN);
455 /* Fill page_size with the usable space */
456 disk->data_size = disk->page_size - BATTFS_HEADER_LEN;
457 ASSERT(disk->page_count);
458 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
459 ASSERT(disk->page_array);
461 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
463 disk->free_bytes = 0;
464 disk->disk_size = (disk_size_t)disk->data_size * disk->page_count;
466 /* Initialize page buffer cache */
467 disk->cache_dirty = false;
469 disk->load(disk, disk->curr_page);
471 /* Count pages per file */
472 if (!countDiskFilePages(disk, filelen_table))
474 LOG_ERR("counting file pages\n");
478 /* Once here, we have filelen_table filled with file lengths */
480 /* Fill page array with sentinel */
481 for (pgcnt_t page = 0; page < disk->page_count; page++)
482 disk->page_array[page] = PAGE_UNSET_SENTINEL;
484 /* Fill page allocation array using filelen_table */
485 if (!fillPageArray(disk, filelen_table))
487 LOG_ERR("filling page array\n");
490 #if LOG_LEVEL >= LOG_LVL_INFO
493 #if CONFIG_BATTFS_SHUFFLE_FREE_PAGES
494 SHUFFLE(&disk->page_array[disk->free_page_start], disk->page_count - disk->free_page_start);
496 LOG_INFO("Page array after shuffle:\n");
497 #if LOG_LEVEL >= LOG_LVL_INFO
501 /* Init list for opened files. */
502 LIST_INIT(&disk->file_opened_list);
507 * Check the filesystem.
508 * \return true if ok, false on errors.
510 bool battfs_fsck(struct BattFsSuper *disk)
512 #define FSCHECK(cond) do { if(!(cond)) { LOG_ERR("\"" #cond "\"\n"); return false; } } while (0)
514 FSCHECK(disk->free_page_start <= disk->page_count);
515 FSCHECK(disk->data_size < disk->page_size);
516 FSCHECK(disk->free_bytes <= disk->disk_size);
518 disk_size_t free_bytes = 0;
519 BattFsPageHeader hdr, prev_hdr;
521 pgcnt_t page_used = 0;
525 /* Uneeded, the first time will be overwritten but useful to silence
526 * the warning for uninitialized value */
527 FSCHECK(readHdr(disk, 0, &prev_hdr));
528 for (pgcnt_t page = 0; page < disk->page_count; page++)
530 FSCHECK(readHdr(disk, disk->page_array[page], &hdr));
531 free_bytes += disk->data_size;
533 if (page < disk->free_page_start)
535 FSCHECK(computeFcs(&hdr) == hdr.fcs);
537 free_bytes -= hdr.fill;
538 if (hdr.inode != prev_hdr.inode || start)
541 FSCHECK(hdr.inode > prev_hdr.inode);
545 FSCHECK(hdr.pgoff == 0);
550 FSCHECK(hdr.fill != 0);
551 FSCHECK(prev_hdr.fill == disk->data_size);
552 FSCHECK(hdr.pgoff == prev_hdr.pgoff + 1);
558 FSCHECK(page_used == disk->free_page_start);
559 FSCHECK(free_bytes == disk->free_bytes);
566 * \return 0 if ok, EOF on errors.
568 static int battfs_flush(struct KFile *fd)
570 BattFs *fdb = BATTFS_CAST(fd);
572 if (flushBuffer(fdb->disk))
576 fdb->errors |= BATTFS_DISK_FLUSHBUF_ERR;
583 * \return 0 if ok, EOF on errors.
585 static int battfs_fileclose(struct KFile *fd)
587 BattFs *fdb = BATTFS_CAST(fd);
589 if (battfs_flush(fd) == 0)
599 static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
601 if (SPACE_OVER(disk))
603 LOG_ERR("No disk space available!\n");
607 LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
608 disk->curr_page = disk->page_array[disk->free_page_start++];
609 memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
612 /* Move following file start point one position ahead. */
613 FOREACH_NODE(n, &disk->file_opened_list)
615 BattFs *file = containerof(n, BattFs, link);
616 if (file->inode > inode)
618 LOG_INFO("Move file %d start pos\n", file->inode);
623 disk->page_array[new_pos] = disk->curr_page;
624 disk->cache_dirty = true;
626 new_hdr->inode = inode;
627 new_hdr->pgoff = pgoff;
630 return setBufferHdr(disk, new_hdr);
634 * Write to file \a fd \a size bytes from \a buf.
635 * \return The number of bytes written.
637 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
639 BattFs *fdb = BATTFS_CAST(fd);
640 BattFsSuper *disk = fdb->disk;
641 const uint8_t *buf = (const uint8_t *)_buf;
643 size_t total_write = 0;
645 pgaddr_t addr_offset;
647 BattFsPageHeader curr_hdr;
649 if (fd->seek_pos < 0)
651 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
655 if (fd->seek_pos > fd->size)
657 /* Handle writing when seek pos if far over EOF */
658 if (!loadPage(disk, fdb->start[fdb->max_off], &curr_hdr))
660 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
664 /* Fill unused space of first page with 0s */
666 pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, (kfile_off_t)(disk->data_size - curr_hdr.fill));
669 if (disk->bufferWrite(disk, curr_hdr.fill, &dummy, 1) != 1)
671 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
677 disk->cache_dirty = true;
679 setBufferHdr(disk, &curr_hdr);
681 /* Allocate the missing pages first. */
682 pgoff_t missing_pages = fd->seek_pos / disk->data_size - fdb->max_off;
686 LOG_INFO("missing pages: %d\n", missing_pages);
689 /* Fill page buffer with 0 to avoid filling unused pages with garbage */
690 for (pgaddr_t off = 0; off < disk->data_size; off++)
692 if (disk->bufferWrite(disk, off, &dummy, 1) != 1)
694 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
699 while (missing_pages--)
701 zero_bytes = MIN((kfile_off_t)disk->data_size, fd->seek_pos - fd->size);
702 /* Get the new page needed */
703 if (!getNewPage(disk, (fdb->start - disk->page_array) + fdb->max_off + 1, fdb->inode, fdb->max_off + 1, &curr_hdr))
705 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
709 /* Update size and free space left */
710 fd->size += zero_bytes;
711 disk->free_bytes -= zero_bytes;
713 curr_hdr.fill = zero_bytes;
714 setBufferHdr(disk, &curr_hdr);
720 else if (!getBufferHdr(disk, &curr_hdr))
722 fdb->errors |= BATTFS_DISK_BUFFERRD_ERR;
728 pg_offset = fd->seek_pos / disk->data_size;
729 addr_offset = fd->seek_pos % disk->data_size;
730 wr_len = MIN(size, (size_t)(disk->data_size - addr_offset));
732 /* Handle write outside EOF */
733 if (pg_offset > fdb->max_off)
735 LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (int)((fdb->start - disk->page_array) + pg_offset));
736 if (!getNewPage(disk, (fdb->start - disk->page_array) + pg_offset, fdb->inode, pg_offset, &curr_hdr))
738 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
742 fdb->max_off = pg_offset;
744 /* Handle cache load of a new page*/
745 else if (fdb->start[pg_offset] != disk->curr_page)
747 if (SPACE_OVER(disk))
749 LOG_ERR("No disk space available!\n");
750 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
753 LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], disk->page_array[disk->free_page_start]);
754 if (!loadPage(disk, fdb->start[pg_offset], &curr_hdr))
756 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
760 /* Get a free page */
761 disk->curr_page = disk->page_array[disk->free_page_start];
762 movePages(disk, disk->free_page_start + 1, -1);
764 /* Insert previous page in free blocks list */
765 LOG_INFO("Setting page %d as free\n", fdb->start[pg_offset]);
766 disk->page_array[disk->page_count - 1] = fdb->start[pg_offset];
767 /* Assign new page */
768 fdb->start[pg_offset] = disk->curr_page;
772 //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", disk->curr_page, addr_offset, wr_len);
773 if (disk->bufferWrite(disk, addr_offset, buf, wr_len) != wr_len)
775 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
778 disk->cache_dirty = true;
781 fd->seek_pos += wr_len;
782 total_write += wr_len;
784 fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
785 disk->free_bytes -= fill_delta;
786 fd->size += fill_delta;
787 curr_hdr.fill += fill_delta;
789 if (!setBufferHdr(disk, &curr_hdr))
791 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
795 //LOG_INFO("free_bytes %d, seek_pos %d, size %d, curr_hdr.fill %d\n", disk->free_bytes, fd->seek_pos, fd->size, curr_hdr.fill);
802 * Read from file \a fd \a size bytes in \a buf.
803 * \return The number of bytes read.
805 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
807 BattFs *fdb = BATTFS_CAST(fd);
808 BattFsSuper *disk = fdb->disk;
809 uint8_t *buf = (uint8_t *)_buf;
811 size_t total_read = 0;
813 pgaddr_t addr_offset;
816 if (fd->seek_pos < 0)
818 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
822 size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, (kfile_off_t)0));
826 pg_offset = fd->seek_pos / disk->data_size;
827 addr_offset = fd->seek_pos % disk->data_size;
828 read_len = MIN(size, (size_t)(disk->data_size - addr_offset));
830 //LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
832 if (diskRead(disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
834 fdb->errors |= BATTFS_DISK_READ_ERR;
839 BattFsPageHeader hdr;
840 readHdr(disk, fdb->start[pg_offset], &hdr);
841 ASSERT(hdr.inode == fdb->inode);
845 fd->seek_pos += read_len;
846 total_read += read_len;
854 * Search file \a inode in \a disk using a binary search.
855 * \a last is filled with array offset of file start
856 * in disk->page_array if file is found, otherwise
857 * \a last is filled with the correct insert position
858 * for creating a file with the given \a inode.
859 * \return true if file is found, false otherwisr.
861 static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
863 BattFsPageHeader hdr;
864 pgcnt_t first = 0, page;
865 *last = disk->free_page_start;
868 while (first < *last)
870 page = (first + *last) / 2;
871 LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
872 if (!readHdr(disk, disk->page_array[page], &hdr))
874 LOG_INFO("inode read: %d\n", hdr.inode);
875 fcs = computeFcs(&hdr);
876 if (hdr.fcs == fcs && hdr.inode == inode)
878 *last = page - hdr.pgoff;
879 LOG_INFO("Found: %d\n", *last);
882 else if (hdr.fcs == fcs && hdr.inode < inode)
887 LOG_INFO("Not found: last %d\n", *last);
892 * \return true if file \a inode exists on \a disk, false otherwise.
894 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
897 return findFile(disk, inode, &dummy);
901 * Count size of file \a inode on \a disk, starting at pointer \a start
902 * in disk->page_array. Size is written in \a size.
903 * \return true if all s ok, false on disk read errors.
905 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
907 file_size_t size = 0;
908 BattFsPageHeader hdr;
910 while (start < &disk->page_array[disk->free_page_start])
912 if (!readHdr(disk, *start++, &hdr))
914 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
922 static int battfs_error(struct KFile *fd)
924 BattFs *fdb = BATTFS_CAST(fd);
929 static void battfs_clearerr(struct KFile *fd)
931 BattFs *fdb = BATTFS_CAST(fd);
936 * Open file \a inode from \a disk in \a mode.
937 * File context is stored in \a fd.
938 * \return true if ok, false otherwise.
940 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
944 memset(fd, 0, sizeof(*fd));
946 /* Search file start point in disk page array */
948 if (!findFile(disk, inode, &start_pos))
950 LOG_INFO("file %d not found\n", inode);
951 if (!(mode & BATTFS_CREATE))
953 fd->errors |= BATTFS_FILE_NOT_FOUND_ERR;
956 /* Create the file */
957 BattFsPageHeader hdr;
958 if (!(getNewPage(disk, start_pos, inode, 0, &hdr)))
960 fd->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
964 fd->start = &disk->page_array[start_pos];
965 LOG_INFO("Start pos %d\n", start_pos);
968 if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
970 fd->errors |= BATTFS_DISK_READ_ERR;
973 fd->max_off = fd->fd.size / disk->data_size;
975 /* Reset seek position */
978 /* Insert file handle in list, ordered by inode, ascending. */
979 FOREACH_NODE(n, &disk->file_opened_list)
981 BattFs *file = containerof(n, BattFs, link);
982 if (file->inode >= inode)
985 INSERT_BEFORE(&fd->link, n);
992 fd->fd.close = battfs_fileclose;
993 fd->fd.flush = battfs_flush;
994 fd->fd.read = battfs_read;
995 fd->fd.reopen = kfile_genericReopen;
996 fd->fd.seek = kfile_genericSeek;
997 fd->fd.write = battfs_write;
999 fd->fd.error = battfs_error;
1000 fd->fd.clearerr = battfs_clearerr;
1002 DB(fd->fd._type = KFT_BATTFS);
1011 bool battfs_umount(struct BattFsSuper *disk)
1016 /* Close all open files */
1017 FOREACH_NODE(n, &disk->file_opened_list)
1019 BattFs *file = containerof(n, BattFs, link);
1020 res += battfs_fileclose(&file->fd);
1024 return disk->close(disk) && (res == 0);
1029 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
1031 BattFsPageHeader hdr;
1033 /* Reset page to all 0xff */
1034 uint8_t buf[disk->page_size];
1035 memset(buf, 0xFF, disk->page_size);
1036 disk->bufferWrite(disk, 0, buf, disk->page_size);
1042 hdr.fcs = computeFcs(&hdr);
1044 if (!(setBufferHdr(disk, &hdr) && disk->save(disk, page)))
1046 LOG_ERR("error writing hdr\n");