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).
36 * \author Francesco Sacchi <batt@develer.com>
41 #include "cfg/cfg_battfs.h"
42 #include <cfg/debug.h>
43 #include <cfg/macros.h> /* MIN, MAX */
45 #include <cpu/byteorder.h> /* cpu_to_xx */
47 #define LOG_LEVEL BATTFS_LOG_LEVEL
48 #define LOG_FORMAT BATTFS_LOG_FORMAT
51 #include <string.h> /* memset, memmove */
53 #if LOG_LEVEL >= LOG_LVL_INFO
54 static void dumpPageArray(struct BattFsSuper *disk)
56 kprintf("Page array dump, free_page_start %d:", disk->free_page_start);
57 for (pgcnt_t i = 0; i < disk->dev->blk_cnt; i++)
61 kprintf("%04d ", disk->page_array[i]);
68 * Convert from memory representation to disk structure.
69 * \note filesystem is in little-endian format.
71 INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf)
73 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
77 buf[2] = hdr->fill >> 8;
80 buf[4] = hdr->pgoff >> 8;
83 * Sequence number is 40 bits long.
84 * No need to take care of wraparonds: the memory will die first!
87 buf[6] = hdr->seq >> 8;
88 buf[7] = hdr->seq >> 16;
89 buf[8] = hdr->seq >> 24;
90 buf[9] = hdr->seq >> 32;
93 * This field must be the last one!
94 * This is needed because if the page is only partially
95 * written, we can use this to detect it.
98 buf[11] = hdr->fcs >> 8;
102 * Convert from disk structure to memory representation.
103 * \note filesystem is in little-endian format.
105 INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr)
107 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
109 hdr->fill = buf[2] << 8 | buf[1];
110 hdr->pgoff = buf[4] << 8 | buf[3];
111 hdr->seq = (seq_t)buf[9] << 32 | (seq_t)buf[8] << 24 | (seq_t)buf[7] << 16 | buf[6] << 8 | buf[5];
112 hdr->fcs = buf[11] << 8 | buf[10];
116 * Compute the fcs of the header.
118 static fcs_t computeFcs(struct BattFsPageHeader *hdr)
120 uint8_t buf[BATTFS_HEADER_LEN];
123 battfs_to_disk(hdr, buf);
125 /* fcs is at the end of whole header */
126 rotating_update(buf, BATTFS_HEADER_LEN - sizeof(fcs_t), &cks);
131 * Read header of \a page in \a hdr.
132 * \return true on success, false otherwise.
134 static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
136 uint8_t buf[BATTFS_HEADER_LEN];
139 * Read header from disk.
140 * Header is actually a footer, and so
141 * resides at page end.
143 if (kblock_read(disk->dev, page, buf, disk->data_size, BATTFS_HEADER_LEN)
144 != BATTFS_HEADER_LEN)
146 LOG_ERR("page[%d]\n", page);
151 disk_to_battfs(buf, hdr);
156 static bool writeHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
158 uint8_t buf[BATTFS_HEADER_LEN];
160 #warning FIXME:refactor computeFcs to save time and stack
161 hdr->fcs = computeFcs(hdr);
163 battfs_to_disk(hdr, buf);
166 * write header to disk.
167 * Header is actually a footer, and so
168 * resides at page end.
170 if (kblock_write(disk->dev, page, buf, disk->data_size, BATTFS_HEADER_LEN)
171 != BATTFS_HEADER_LEN)
173 LOG_ERR("writing to buffer\n");
181 * Count the number of pages from
182 * inode 0 to \a inode in \a filelen_table.
184 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
188 for (inode_t i = 0; i < inode; i++)
189 cnt += filelen_table[i];
195 * Move all pages in page allocation array from \a src to \a src + \a offset.
196 * The number of pages moved is page_count - MAX(dst, src).
198 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
200 pgcnt_t dst = src + offset;
201 LOG_INFO("src %d, offset %d, size %d\n", src, offset, (unsigned int)((disk->dev->blk_cnt - MAX(dst, src)) * sizeof(pgcnt_t)));
202 memmove(&disk->page_array[dst], &disk->page_array[src], (disk->dev->blk_cnt - MAX(dst, src)) * sizeof(pgcnt_t));
206 /* Fill empty space in array with sentinel */
207 for (pgcnt_t page = disk->dev->blk_cnt + offset; page < disk->dev->blk_cnt; page++)
208 disk->page_array[page] = PAGE_UNSET_SENTINEL;
213 * Count number of pages per file on \a disk.
214 * This information is registered in \a filelen_table.
215 * Array index represent file inode, while value contained
216 * is the number of pages used by that file.
218 * \return true if ok, false on disk read errors.
219 * \note The whole disk is scanned once.
221 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
223 BattFsPageHeader hdr;
224 disk->free_page_start = 0;
226 /* Count the number of disk page per file */
227 for (pgcnt_t page = 0; page < disk->dev->blk_cnt; page++)
229 if (!readHdr(disk, page, &hdr))
232 /* Increase free space */
233 disk->free_bytes += disk->data_size;
235 /* Check header FCS */
236 if (hdr.fcs == computeFcs(&hdr))
238 ASSERT(hdr.fill <= disk->data_size);
240 /* Page is valid and is owned by a file */
241 filelen_table[hdr.inode]++;
243 /* Keep trace of free space */
244 disk->free_bytes -= hdr.fill;
245 disk->free_page_start++;
248 LOG_INFO("free_bytes:%ld, free_page_start:%d\n", (long)disk->free_bytes, disk->free_page_start);
254 * Fill page allocation array of \a disk
255 * using file lenghts in \a filelen_table.
257 * The page allocation array is an array containings all file infos.
258 * Is ordered by file, and within each file is ordered by page offset
260 * e.g. : at page array[0] you will find page address of the first page
261 * of the first file (if present).
262 * Free blocks are allocated after the last file.
264 * \return true if ok, false on disk read errors.
265 * \note The whole disk is scanned at max twice.
267 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
269 BattFsPageHeader hdr;
270 pgcnt_t curr_free_page = disk->free_page_start;
271 /* Fill page allocation array */
272 for (pgcnt_t page = 0; page < disk->dev->blk_cnt; page++)
274 if (!readHdr(disk, page, &hdr))
277 /* Check header FCS */
278 if (hdr.fcs == computeFcs(&hdr))
280 /* Compute array position */
281 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
282 array_pos += hdr.pgoff;
285 /* Check if position is already used by another page of the same file */
286 if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
287 disk->page_array[array_pos] = page;
290 BattFsPageHeader hdr_prv;
292 if (!readHdr(disk, disk->page_array[array_pos], &hdr_prv))
295 /* Check header FCS */
296 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
298 /* Only the very same page with a different seq number can be here */
299 ASSERT(hdr.inode == hdr_prv.inode);
300 ASSERT(hdr.pgoff == hdr_prv.pgoff);
301 ASSERT(hdr.seq != hdr_prv.seq);
303 pgcnt_t new_page, old_page;
307 * Sequence number comparison: since
308 * seq is 40 bits wide, it wraps once
309 * every 1.1E12 times.
310 * The memory will not live enough to
311 * see a wraparound, so we can use a simple
314 if (hdr.seq > hdr_prv.seq)
316 /* Current header is newer than the previuos one */
317 old_page = disk->page_array[array_pos];
319 old_fill = hdr_prv.fill;
323 /* Previous header is newer than the current one */
325 new_page = disk->page_array[array_pos];
330 disk->page_array[array_pos] = new_page;
332 disk->free_bytes += old_fill;
333 /* Shift all array one position to the left, overwriting duplicate page */
334 array_pos -= hdr.pgoff;
335 array_pos += filelen_table[hdr.inode];
336 movePages(disk, array_pos, -1);
337 /* Move back all indexes */
338 filelen_table[hdr.inode]--;
339 disk->free_page_start--;
341 /* Set old page as free */
342 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
343 disk->page_array[curr_free_page++] = old_page;
349 /* Invalid page, keep as free */
350 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
351 //LOG_INFO("Page %d invalid, keeping as free\n", page);
352 disk->page_array[curr_free_page++] = page;
360 * Initialize and mount disk described by
362 * \return false on errors, true otherwise.
364 bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size)
366 pgoff_t filelen_table[BATTFS_MAX_FILES];
369 ASSERT(kblock_buffered(dev));
372 ASSERT(disk->dev->blk_size > BATTFS_HEADER_LEN);
373 /* Fill page_size with the usable space */
374 disk->data_size = disk->dev->blk_size - BATTFS_HEADER_LEN;
375 ASSERT(disk->dev->blk_cnt);
376 ASSERT(disk->dev->blk_cnt < PAGE_UNSET_SENTINEL - 1);
378 disk->page_array = page_array;
379 ASSERT(array_size >= disk->dev->blk_cnt * sizeof(pgcnt_t));
381 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
383 disk->free_bytes = 0;
384 disk->disk_size = (disk_size_t)disk->data_size * disk->dev->blk_cnt;
386 /* Count pages per file */
387 if (!countDiskFilePages(disk, filelen_table))
389 LOG_ERR("counting file pages\n");
393 /* Once here, we have filelen_table filled with file lengths */
395 /* Fill page array with sentinel */
396 for (pgcnt_t page = 0; page < disk->dev->blk_cnt; page++)
397 disk->page_array[page] = PAGE_UNSET_SENTINEL;
399 /* Fill page allocation array using filelen_table */
400 if (!fillPageArray(disk, filelen_table))
402 LOG_ERR("filling page array\n");
405 #if LOG_LEVEL >= LOG_LVL_INFO
408 #if CONFIG_BATTFS_SHUFFLE_FREE_PAGES
409 SHUFFLE(&disk->page_array[disk->free_page_start], disk->dev->blk_cnt - disk->free_page_start);
411 LOG_INFO("Page array after shuffle:\n");
412 #if LOG_LEVEL >= LOG_LVL_INFO
416 /* Init list for opened files. */
417 LIST_INIT(&disk->file_opened_list);
422 * Check the filesystem.
423 * \return true if ok, false on errors.
425 bool battfs_fsck(struct BattFsSuper *disk)
427 #define FSCHECK(cond) do { if(!(cond)) { LOG_ERR("\"" #cond "\"\n"); return false; } } while (0)
429 FSCHECK(disk->free_page_start <= disk->dev->blk_cnt);
430 FSCHECK(disk->data_size < disk->dev->blk_size);
431 FSCHECK(disk->free_bytes <= disk->disk_size);
433 disk_size_t free_bytes = 0;
434 BattFsPageHeader hdr, prev_hdr;
436 pgcnt_t page_used = 0;
440 /* Uneeded, the first time will be overwritten but useful to silence
441 * the warning for uninitialized value */
442 FSCHECK(readHdr(disk, 0, &prev_hdr));
443 for (pgcnt_t page = 0; page < disk->dev->blk_cnt; page++)
445 FSCHECK(readHdr(disk, disk->page_array[page], &hdr));
446 free_bytes += disk->data_size;
448 if (page < disk->free_page_start)
450 FSCHECK(computeFcs(&hdr) == hdr.fcs);
452 free_bytes -= hdr.fill;
453 if (hdr.inode != prev_hdr.inode || start)
456 FSCHECK(hdr.inode > prev_hdr.inode);
460 FSCHECK(hdr.pgoff == 0);
465 FSCHECK(hdr.fill != 0);
466 FSCHECK(prev_hdr.fill == disk->data_size);
467 FSCHECK(hdr.pgoff == prev_hdr.pgoff + 1);
473 FSCHECK(page_used == disk->free_page_start);
474 FSCHECK(free_bytes == disk->free_bytes);
481 * \return 0 if ok, EOF on errors.
483 static int battfs_flush(struct KFile *fd)
485 BattFs *fdb = BATTFS_CAST(fd);
487 if (kblock_flush(fdb->disk->dev) == 0)
491 fdb->errors |= BATTFS_DISK_FLUSHBUF_ERR;
498 * \return 0 if ok, EOF on errors.
500 static int battfs_fileclose(struct KFile *fd)
502 BattFs *fdb = BATTFS_CAST(fd);
504 if (battfs_flush(fd) == 0)
513 #define NO_SPACE PAGE_UNSET_SENTINEL
515 static pgcnt_t allocateNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode)
517 if (SPACE_OVER(disk))
519 LOG_ERR("No disk space available!\n");
523 LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
524 pgcnt_t new_page = disk->page_array[disk->free_page_start++];
525 memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
528 /* Move following file start point one position ahead. */
529 FOREACH_NODE(n, &disk->file_opened_list)
531 BattFs *file = containerof(n, BattFs, link);
532 if (file->inode > inode)
534 LOG_INFO("Move file %d start pos\n", file->inode);
539 disk->page_array[new_pos] = new_page;
543 static pgcnt_t renewPage(struct BattFsSuper *disk, pgcnt_t old_pos)
545 if (SPACE_OVER(disk))
547 LOG_ERR("No disk space available!\n");
551 /* Get a free page */
552 pgcnt_t new_page = disk->page_array[disk->free_page_start];
553 movePages(disk, disk->free_page_start + 1, -1);
555 /* Insert previous page in free blocks list */
556 LOG_INFO("Setting page %d as free\n", old_pos);
557 disk->page_array[disk->dev->blk_cnt - 1] = old_pos;
562 * Write to file \a fd \a size bytes from \a buf.
563 * \return The number of bytes written.
565 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
567 BattFs *fdb = BATTFS_CAST(fd);
568 BattFsSuper *disk = fdb->disk;
569 const uint8_t *buf = (const uint8_t *)_buf;
571 size_t total_write = 0;
573 pgaddr_t addr_offset;
575 BattFsPageHeader curr_hdr;
578 if (fd->seek_pos < 0)
580 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
584 if (fd->seek_pos > fd->size)
586 if (!readHdr(disk, fdb->start[fdb->max_off], &curr_hdr))
588 fdb->errors |= BATTFS_DISK_READ_ERR;
593 * Renew page only if is not in cache.
594 * This avoids rewriting the same page continuously
595 * if the user code keeps writing in the same portion
598 if ((fdb->start[fdb->max_off] != kblock_cachedBlock(disk->dev)) || !kblock_cacheDirty(disk->dev))
600 new_page = renewPage(disk, fdb->start[fdb->max_off]);
601 if (new_page == NO_SPACE)
603 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
607 kblock_copy(disk->dev, fdb->start[fdb->max_off], new_page);
608 fdb->start[fdb->max_off] = new_page;
611 new_page = fdb->start[fdb->max_off];
613 /* Fill unused space of first page with 0s */
615 // TODO: write in blocks to speed up things
616 pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, (kfile_off_t)(disk->data_size - curr_hdr.fill));
619 if (kblock_write(disk->dev, new_page, &dummy, curr_hdr.fill, 1) != 1)
621 fdb->errors |= BATTFS_DISK_WRITE_ERR;
629 if (!writeHdr(disk, new_page, &curr_hdr))
631 fdb->errors |= BATTFS_DISK_WRITE_ERR;
635 /* Allocate the missing pages first. */
636 pgoff_t missing_pages = fd->seek_pos / disk->data_size - fdb->max_off;
638 LOG_INFO("missing pages: %d\n", missing_pages);
640 while (missing_pages--)
642 zero_bytes = MIN((kfile_off_t)disk->data_size, fd->seek_pos - fd->size);
644 new_page = allocateNewPage(disk, (fdb->start - disk->page_array) + fdb->max_off + 1, fdb->inode);
645 if (new_page == NO_SPACE)
647 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
652 // TODO: write in blocks to speed up things
653 /* Fill page buffer with 0 to avoid filling unused pages with garbage */
654 for (pgaddr_t off = 0; off < disk->data_size; off++)
656 if (kblock_write(disk->dev, new_page, &dummy, off, 1) != 1)
658 fdb->errors |= BATTFS_DISK_WRITE_ERR;
662 curr_hdr.inode = fdb->inode;
663 curr_hdr.pgoff = ++fdb->max_off;
664 curr_hdr.fill = zero_bytes;
667 if (!writeHdr(disk, new_page, &curr_hdr))
669 fdb->errors |= BATTFS_DISK_WRITE_ERR;
673 /* Update size and free space left */
674 fd->size += zero_bytes;
675 disk->free_bytes -= zero_bytes;
681 pg_offset = fd->seek_pos / disk->data_size;
682 addr_offset = fd->seek_pos % disk->data_size;
683 wr_len = MIN(size, (size_t)(disk->data_size - addr_offset));
685 /* Handle write outside EOF */
686 if (pg_offset > fdb->max_off)
688 LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (int)((fdb->start - disk->page_array) + pg_offset));
690 new_page = allocateNewPage(disk, (fdb->start - disk->page_array) + pg_offset, fdb->inode);
691 if (new_page == NO_SPACE)
693 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
697 curr_hdr.inode = fdb->inode;
698 curr_hdr.pgoff = pg_offset;
701 fdb->max_off = pg_offset;
705 if (!readHdr(disk, fdb->start[pg_offset], &curr_hdr))
707 fdb->errors |= BATTFS_DISK_READ_ERR;
711 /* Renew page only if is not in cache. */
712 if ((fdb->start[fdb->max_off] != kblock_cachedBlock(disk->dev)) || !kblock_cacheDirty(disk->dev))
714 new_page = renewPage(disk, fdb->start[pg_offset]);
715 if (new_page == NO_SPACE)
717 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
721 LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], new_page);
722 if (kblock_copy(disk->dev, fdb->start[pg_offset], new_page) != 0)
724 fdb->errors |= BATTFS_DISK_WRITE_ERR;
727 fdb->start[pg_offset] = new_page;
731 LOG_INFO("Using cached block %d\n", fdb->start[pg_offset]);
732 new_page = fdb->start[pg_offset];
737 //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", disk->curr_page, addr_offset, wr_len);
738 if (kblock_write(disk->dev, new_page, buf, addr_offset, wr_len) != wr_len)
740 fdb->errors |= BATTFS_DISK_WRITE_ERR;
745 fd->seek_pos += wr_len;
746 total_write += wr_len;
748 fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
749 disk->free_bytes -= fill_delta;
750 fd->size += fill_delta;
751 curr_hdr.fill += fill_delta;
753 if (!writeHdr(disk, new_page, &curr_hdr))
755 fdb->errors |= BATTFS_DISK_WRITE_ERR;
759 //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);
766 * Read from file \a fd \a size bytes in \a buf.
767 * \return The number of bytes read.
769 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
771 BattFs *fdb = BATTFS_CAST(fd);
772 BattFsSuper *disk = fdb->disk;
773 uint8_t *buf = (uint8_t *)_buf;
775 size_t total_read = 0;
777 pgaddr_t addr_offset;
780 if (fd->seek_pos < 0)
782 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
786 size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, (kfile_off_t)0));
790 pg_offset = fd->seek_pos / disk->data_size;
791 addr_offset = fd->seek_pos % disk->data_size;
792 read_len = MIN(size, (size_t)(disk->data_size - addr_offset));
794 //LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
796 if (kblock_read(disk->dev, fdb->start[pg_offset], buf, addr_offset, read_len) != read_len)
798 fdb->errors |= BATTFS_DISK_READ_ERR;
803 BattFsPageHeader hdr;
804 readHdr(disk, fdb->start[pg_offset], &hdr);
805 ASSERT(hdr.inode == fdb->inode);
809 fd->seek_pos += read_len;
810 total_read += read_len;
818 * Search file \a inode in \a disk using a binary search.
819 * \a last is filled with array offset of file start
820 * in disk->page_array if file is found, otherwise
821 * \a last is filled with the correct insert position
822 * for creating a file with the given \a inode.
823 * \return true if file is found, false otherwisr.
825 static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
827 BattFsPageHeader hdr;
828 pgcnt_t first = 0, page;
829 *last = disk->free_page_start;
832 while (first < *last)
834 page = (first + *last) / 2;
835 LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
836 if (!readHdr(disk, disk->page_array[page], &hdr))
838 LOG_INFO("inode read: %d\n", hdr.inode);
839 fcs = computeFcs(&hdr);
840 if (hdr.fcs == fcs && hdr.inode == inode)
842 *last = page - hdr.pgoff;
843 LOG_INFO("Found: %d\n", *last);
846 else if (hdr.fcs == fcs && hdr.inode < inode)
851 LOG_INFO("Not found: last %d\n", *last);
856 * \return true if file \a inode exists on \a disk, false otherwise.
858 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
861 return findFile(disk, inode, &dummy);
865 * Count size of file \a inode on \a disk, starting at pointer \a start
866 * in disk->page_array. Size is written in \a size.
867 * \return true if all s ok, false on disk read errors.
869 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
871 file_size_t size = 0;
872 BattFsPageHeader hdr;
874 while (start < &disk->page_array[disk->free_page_start])
876 if (!readHdr(disk, *start++, &hdr))
878 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
886 static int battfs_error(struct KFile *fd)
888 BattFs *fdb = BATTFS_CAST(fd);
893 static void battfs_clearerr(struct KFile *fd)
895 BattFs *fdb = BATTFS_CAST(fd);
900 * Open file \a inode from \a disk in \a mode.
901 * File context is stored in \a fd.
902 * \return true if ok, false otherwise.
904 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
908 memset(fd, 0, sizeof(*fd));
910 /* Search file start point in disk page array */
912 if (!findFile(disk, inode, &start_pos))
914 LOG_INFO("file %d not found\n", inode);
915 if (!(mode & BATTFS_CREATE))
917 fd->errors |= BATTFS_FILE_NOT_FOUND_ERR;
920 /* Create the file */
921 BattFsPageHeader hdr;
923 if (allocateNewPage(disk, start_pos, inode) == NO_SPACE)
925 fd->errors |= BATTFS_DISK_SPACEOVER_ERR;
933 if (!writeHdr(disk, disk->page_array[start_pos], &hdr))
935 fd->errors |= BATTFS_DISK_WRITE_ERR;
939 fd->start = &disk->page_array[start_pos];
940 LOG_INFO("Start pos %d\n", start_pos);
943 if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
945 fd->errors |= BATTFS_DISK_READ_ERR;
948 fd->max_off = fd->fd.size / disk->data_size;
950 /* Reset seek position */
953 /* Insert file handle in list, ordered by inode, ascending. */
954 FOREACH_NODE(n, &disk->file_opened_list)
956 BattFs *file = containerof(n, BattFs, link);
957 if (file->inode >= inode)
960 INSERT_BEFORE(&fd->link, n);
967 fd->fd.close = battfs_fileclose;
968 fd->fd.flush = battfs_flush;
969 fd->fd.read = battfs_read;
970 fd->fd.reopen = kfile_genericReopen;
971 fd->fd.seek = kfile_genericSeek;
972 fd->fd.write = battfs_write;
974 fd->fd.error = battfs_error;
975 fd->fd.clearerr = battfs_clearerr;
977 DB(fd->fd._type = KFT_BATTFS);
986 bool battfs_umount(struct BattFsSuper *disk)
991 /* Close all open files */
992 FOREACH_NODE(n, &disk->file_opened_list)
994 BattFs *file = containerof(n, BattFs, link);
995 res += battfs_fileclose(&file->fd);
999 return (kblock_flush(disk->dev) == 0) && (kblock_close(disk->dev) == 0) && (res == 0);
1004 void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
1006 uint8_t buf[BATTFS_HEADER_LEN];
1007 battfs_eraseBlock(dev, page);
1009 BattFsPageHeader hdr;
1014 hdr.fcs = computeFcs(&hdr);
1016 battfs_to_disk(&hdr, buf);
1018 ASSERT(kblock_write(dev, page, buf, dev->blk_size - BATTFS_HEADER_LEN, BATTFS_HEADER_LEN) == BATTFS_HEADER_LEN);
1019 ASSERT(kblock_flush(dev) == 0);
1022 void battfs_eraseBlock(KBlock *dev, pgcnt_t page)
1024 /* Reset page to all 0xff */
1025 uint8_t buf[dev->blk_size];
1026 memset(buf, 0xFF, dev->blk_size);
1027 ASSERT(kblock_write(dev, page, buf, 0, dev->blk_size) == dev->blk_size);
1028 ASSERT(kblock_flush(dev) == 0);