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>
43 #include <cfg/debug.h>
44 #include <cfg/macros.h> /* MIN, MAX */
45 #include <cpu/byteorder.h> /* cpu_to_xx */
47 #define LOG_LEVEL LOG_LVL_INFO
48 #define LOG_FORMAT LOG_FMT_VERBOSE
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->page_count; 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);
132 * If available, the cache will be used.
133 * \return the number of bytes read.
135 static size_t diskRead(struct BattFsSuper *disk, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
137 /* Try to read from cache */
138 if (page == disk->curr_page)
139 return disk->bufferRead(disk, addr, buf, size);
142 return disk->read(disk, page, addr, buf, size);
147 * Read header of \a page in \a hdr.
148 * \return true on success, false otherwise.
150 static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
152 uint8_t buf[BATTFS_HEADER_LEN];
154 * Read header from disk.
155 * Header is actually a footer, and so
156 * resides at page end.
158 if (diskRead(disk, page, disk->data_size, buf, BATTFS_HEADER_LEN)
159 != BATTFS_HEADER_LEN)
161 LOG_ERR("page[%d]\n", page);
166 disk_to_battfs(buf, hdr);
172 * Set header on current \a disk page buffer.
173 * \return true on success, false otherwise.
175 static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
177 uint8_t buf[BATTFS_HEADER_LEN];
179 #warning FIXME:refactor computeFcs to save time and stack
180 hdr->fcs = computeFcs(hdr);
182 battfs_to_disk(hdr, buf);
185 * write header to buffer.
186 * Header is actually a footer, and so
187 * resides at page end.
189 if (disk->bufferWrite(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
190 != BATTFS_HEADER_LEN)
192 LOG_ERR("writing to buffer\n");
198 static bool getBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr)
200 uint8_t buf[BATTFS_HEADER_LEN];
202 if (disk->bufferRead(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
203 != BATTFS_HEADER_LEN)
205 LOG_ERR("reading from buffer\n");
209 disk_to_battfs(buf, hdr);
215 * Count the number of pages from
216 * inode 0 to \a inode in \a filelen_table.
218 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
222 for (inode_t i = 0; i < inode; i++)
223 cnt += filelen_table[i];
229 * Move all pages in page allocation array from \a src to \a src + \a offset.
230 * The number of pages moved is page_count - MAX(dst, src).
232 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
234 pgcnt_t dst = src + offset;
235 LOG_INFO("src %d, offset %d, size %d\n", src, offset, (unsigned int)((disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t)));
236 memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
240 /* Fill empty space in array with sentinel */
241 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
242 disk->page_array[page] = PAGE_UNSET_SENTINEL;
247 * Count number of pages per file on \a disk.
248 * This information is registered in \a filelen_table.
249 * Array index represent file inode, while value contained
250 * is the number of pages used by that file.
252 * \return true if ok, false on disk read errors.
253 * \note The whole disk is scanned once.
255 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
257 BattFsPageHeader hdr;
258 disk->free_page_start = 0;
260 /* Count the number of disk page per file */
261 for (pgcnt_t page = 0; page < disk->page_count; page++)
263 if (!readHdr(disk, page, &hdr))
266 /* Increase free space */
267 disk->free_bytes += disk->data_size;
269 /* Check header FCS */
270 if (hdr.fcs == computeFcs(&hdr))
272 ASSERT(hdr.fill <= disk->data_size);
274 /* Page is valid and is owned by a file */
275 filelen_table[hdr.inode]++;
277 /* Keep trace of free space */
278 disk->free_bytes -= hdr.fill;
279 disk->free_page_start++;
282 LOG_INFO("free_bytes:%d, free_page_start:%d\n", disk->free_bytes, disk->free_page_start);
288 * Fill page allocation array of \a disk
289 * using file lenghts in \a filelen_table.
291 * The page allocation array is an array containings all file infos.
292 * Is ordered by file, and within each file is ordered by page offset
294 * e.g. : at page array[0] you will find page address of the first page
295 * of the first file (if present).
296 * Free blocks are allocated after the last file.
298 * \return true if ok, false on disk read errors.
299 * \note The whole disk is scanned at max twice.
301 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
303 BattFsPageHeader hdr;
304 pgcnt_t curr_free_page = disk->free_page_start;
305 /* Fill page allocation array */
306 for (pgcnt_t page = 0; page < disk->page_count; page++)
308 if (!readHdr(disk, page, &hdr))
311 /* Check header FCS */
312 if (hdr.fcs == computeFcs(&hdr))
314 /* Compute array position */
315 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
316 array_pos += hdr.pgoff;
319 /* Check if position is already used by another page of the same file */
320 if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
321 disk->page_array[array_pos] = page;
324 BattFsPageHeader hdr_prv;
326 if (!readHdr(disk, disk->page_array[array_pos], &hdr_prv))
329 /* Check header FCS */
330 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
332 /* Only the very same page with a different seq number can be here */
333 ASSERT(hdr.inode == hdr_prv.inode);
334 ASSERT(hdr.pgoff == hdr_prv.pgoff);
335 ASSERT(hdr.seq != hdr_prv.seq);
337 pgcnt_t new_page, old_page;
341 * Sequence number comparison: since
342 * seq is 40 bits wide, it wraps once
343 * every 1.1E12 times.
344 * The memory will not live enough to
345 * see a wraparound, so we can use a simple
348 if (hdr.seq > hdr_prv.seq)
350 /* Current header is newer than the previuos one */
351 old_page = disk->page_array[array_pos];
353 old_fill = hdr_prv.fill;
357 /* Previous header is newer than the current one */
359 new_page = disk->page_array[array_pos];
364 disk->page_array[array_pos] = new_page;
366 disk->free_bytes += old_fill;
367 /* Shift all array one position to the left, overwriting duplicate page */
368 array_pos -= hdr.pgoff;
369 array_pos += filelen_table[hdr.inode];
370 movePages(disk, array_pos, -1);
371 /* Move back all indexes */
372 filelen_table[hdr.inode]--;
373 disk->free_page_start--;
375 /* Set old page as free */
376 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
377 disk->page_array[curr_free_page++] = old_page;
383 /* Invalid page, keep as free */
384 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
385 LOG_INFO("Page %d invalid, keeping as free\n", page);
386 disk->page_array[curr_free_page++] = page;
394 * Flush the current \a disk buffer.
395 * \return true if ok, false on errors.
397 static bool flushBuffer(struct BattFsSuper *disk)
399 if (disk->cache_dirty)
401 LOG_INFO("Flushing to disk page %d\n", disk->curr_page);
403 if (!(disk->erase(disk, disk->curr_page)
404 && disk->save(disk, disk->curr_page)))
407 disk->cache_dirty = false;
413 * Load \a new_page from \a disk in disk page buffer.
414 * If a previuos page is still dirty in the buffer, will be
415 * flushed first. The new page header loaded will be put in \a new_hdr.
416 * \return true if ok, false on errors.
418 static bool loadPage(struct BattFsSuper *disk, pgcnt_t new_page, BattFsPageHeader *new_hdr)
420 if (disk->curr_page == new_page)
421 return getBufferHdr(disk, new_hdr);
423 LOG_INFO("Loading page %d\n", new_page);
425 if (!(flushBuffer(disk)
426 && disk->load(disk, new_page)
427 && getBufferHdr(disk, new_hdr)))
430 disk->curr_page = new_page;
437 * Initialize and mount disk described by
439 * \return false on errors, true otherwise.
441 bool battfs_mount(struct BattFsSuper *disk)
443 pgoff_t filelen_table[BATTFS_MAX_FILES];
448 /* Init disk device */
449 if (!disk->open(disk))
451 LOG_ERR("open error\n");
455 /* Disk open must set all of these */
458 ASSERT(disk->bufferWrite);
459 ASSERT(disk->bufferRead);
463 ASSERT(disk->page_size > BATTFS_HEADER_LEN);
464 /* Fill page_size with the usable space */
465 disk->data_size = disk->page_size - BATTFS_HEADER_LEN;
466 ASSERT(disk->page_count);
467 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
468 ASSERT(disk->page_array);
470 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
472 disk->free_bytes = 0;
473 disk->disk_size = (disk_size_t)disk->data_size * disk->page_count;
475 /* Initialize page buffer cache */
476 disk->cache_dirty = false;
478 disk->load(disk, disk->curr_page);
480 /* Count pages per file */
481 if (!countDiskFilePages(disk, filelen_table))
483 LOG_ERR("counting file pages\n");
487 /* Once here, we have filelen_table filled with file lengths */
489 /* Fill page array with sentinel */
490 for (pgcnt_t page = 0; page < disk->page_count; page++)
491 disk->page_array[page] = PAGE_UNSET_SENTINEL;
493 /* Fill page allocation array using filelen_table */
494 if (!fillPageArray(disk, filelen_table))
496 LOG_ERR("filling page array\n");
499 #if LOG_LEVEL >= LOG_LVL_INFO
502 #warning TODO: shuffle free blocks
503 //#if LOG_LEVEL > LOG_LVL_INFO
504 // dumpPageArray(disk);
506 /* Init list for opened files. */
507 LIST_INIT(&disk->file_opened_list);
512 * Check the filesystem.
513 * \return true if ok, false on errors.
515 bool battfs_fsck(struct BattFsSuper *disk)
517 #define FSCHECK(cond) do { if(!(cond)) { LOG_ERR("\"" #cond "\"\n"); return false; } } while (0)
519 FSCHECK(disk->free_page_start <= disk->page_count);
520 FSCHECK(disk->data_size < disk->page_size);
521 FSCHECK(disk->free_bytes <= disk->disk_size);
523 disk_size_t free_bytes = 0;
524 BattFsPageHeader hdr, prev_hdr;
526 pgcnt_t page_used = 0;
530 for (pgcnt_t page = 0; page < disk->page_count; page++)
532 FSCHECK(readHdr(disk, disk->page_array[page], &hdr));
533 free_bytes += disk->data_size;
535 if (page < disk->free_page_start)
537 FSCHECK(computeFcs(&hdr) == hdr.fcs);
539 free_bytes -= hdr.fill;
540 if (hdr.inode != prev_hdr.inode || start)
543 FSCHECK(hdr.inode > prev_hdr.inode);
547 FSCHECK(hdr.pgoff == 0);
552 FSCHECK(hdr.fill != 0);
553 FSCHECK(prev_hdr.fill == disk->data_size);
554 FSCHECK(hdr.pgoff == prev_hdr.pgoff + 1);
560 FSCHECK(page_used == disk->free_page_start);
561 FSCHECK(free_bytes == disk->free_bytes);
568 * \return 0 if ok, EOF on errors.
570 static int battfs_flush(struct KFile *fd)
572 BattFs *fdb = BATTFS_CAST(fd);
574 if (flushBuffer(fdb->disk))
578 fdb->errors |= BATTFS_DISK_FLUSHBUF_ERR;
585 * \return 0 if ok, EOF on errors.
587 static int battfs_fileclose(struct KFile *fd)
589 BattFs *fdb = BATTFS_CAST(fd);
591 if (battfs_flush(fd) == 0)
601 static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
603 if (SPACE_OVER(disk))
605 LOG_ERR("No disk space available!\n");
609 LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
610 disk->curr_page = disk->page_array[disk->free_page_start++];
611 memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
614 /* Move following file start point one position ahead. */
615 FOREACH_NODE(n, &disk->file_opened_list)
617 BattFs *file = containerof(n, BattFs, link);
618 if (file->inode > inode)
620 LOG_INFO("Move file %d start pos\n", file->inode);
625 disk->page_array[new_pos] = disk->curr_page;
626 disk->cache_dirty = true;
628 new_hdr->inode = inode;
629 new_hdr->pgoff = pgoff;
632 return setBufferHdr(disk, new_hdr);
636 * Write to file \a fd \a size bytes from \a buf.
637 * \return The number of bytes written.
639 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
641 BattFs *fdb = BATTFS_CAST(fd);
642 BattFsSuper *disk = fdb->disk;
643 const uint8_t *buf = (const uint8_t *)_buf;
645 size_t total_write = 0;
647 pgaddr_t addr_offset;
649 BattFsPageHeader curr_hdr;
651 if (fd->seek_pos < 0)
653 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
657 if (fd->seek_pos > fd->size)
659 /* Handle writing when seek pos if far over EOF */
660 if (!loadPage(disk, fdb->start[fdb->max_off], &curr_hdr))
662 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
666 /* Fill unused space of first page with 0s */
668 pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, disk->data_size - curr_hdr.fill);
671 if (disk->bufferWrite(disk, curr_hdr.fill, &dummy, 1) != 1)
673 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
679 disk->cache_dirty = true;
681 setBufferHdr(disk, &curr_hdr);
683 /* Allocate the missing pages first. */
684 pgoff_t missing_pages = fd->seek_pos / disk->data_size - fdb->max_off;
688 LOG_INFO("missing pages: %d\n", missing_pages);
691 /* Fill page buffer with 0 to avoid filling unused pages with garbage */
692 for (pgaddr_t off = 0; off < disk->data_size; off++)
694 if (disk->bufferWrite(disk, off, &dummy, 1) != 1)
696 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
701 while (missing_pages--)
703 zero_bytes = MIN((kfile_off_t)disk->data_size, fd->seek_pos - fd->size);
704 /* Get the new page needed */
705 if (!getNewPage(disk, (fdb->start - disk->page_array) + fdb->max_off + 1, fdb->inode, fdb->max_off + 1, &curr_hdr))
707 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
711 /* Update size and free space left */
712 fd->size += zero_bytes;
713 disk->free_bytes -= zero_bytes;
715 curr_hdr.fill = zero_bytes;
716 setBufferHdr(disk, &curr_hdr);
722 else if (!getBufferHdr(disk, &curr_hdr))
724 fdb->errors |= BATTFS_DISK_BUFFERRD_ERR;
730 pg_offset = fd->seek_pos / disk->data_size;
731 addr_offset = fd->seek_pos % disk->data_size;
732 wr_len = MIN(size, (size_t)(disk->data_size - addr_offset));
734 /* Handle write outside EOF */
735 if (pg_offset > fdb->max_off)
737 LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (int)((fdb->start - disk->page_array) + pg_offset));
738 if (!getNewPage(disk, (fdb->start - disk->page_array) + pg_offset, fdb->inode, pg_offset, &curr_hdr))
740 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
744 fdb->max_off = pg_offset;
746 /* Handle cache load of a new page*/
747 else if (fdb->start[pg_offset] != disk->curr_page)
749 if (SPACE_OVER(disk))
751 LOG_ERR("No disk space available!\n");
752 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
755 LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], disk->page_array[disk->free_page_start]);
756 if (!loadPage(disk, fdb->start[pg_offset], &curr_hdr))
758 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
762 /* Get a free page */
763 disk->curr_page = disk->page_array[disk->free_page_start];
764 movePages(disk, disk->free_page_start + 1, -1);
766 /* Insert previous page in free blocks list */
767 LOG_INFO("Setting page %d as free\n", fdb->start[pg_offset]);
768 disk->page_array[disk->page_count - 1] = fdb->start[pg_offset];
769 /* Assign new page */
770 fdb->start[pg_offset] = disk->curr_page;
774 //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", disk->curr_page, addr_offset, wr_len);
775 if (disk->bufferWrite(disk, addr_offset, buf, wr_len) != wr_len)
777 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
780 disk->cache_dirty = true;
783 fd->seek_pos += wr_len;
784 total_write += wr_len;
786 fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
787 disk->free_bytes -= fill_delta;
788 fd->size += fill_delta;
789 curr_hdr.fill += fill_delta;
791 if (!setBufferHdr(disk, &curr_hdr))
793 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
797 //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);
804 * Read from file \a fd \a size bytes in \a buf.
805 * \return The number of bytes read.
807 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
809 BattFs *fdb = BATTFS_CAST(fd);
810 BattFsSuper *disk = fdb->disk;
811 uint8_t *buf = (uint8_t *)_buf;
813 size_t total_read = 0;
815 pgaddr_t addr_offset;
818 if (fd->seek_pos < 0)
820 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
824 size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, 0));
828 pg_offset = fd->seek_pos / disk->data_size;
829 addr_offset = fd->seek_pos % disk->data_size;
830 read_len = MIN(size, (size_t)(disk->data_size - addr_offset));
832 //LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
834 if (diskRead(disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
836 fdb->errors |= BATTFS_DISK_READ_ERR;
841 BattFsPageHeader hdr;
842 readHdr(disk, fdb->start[pg_offset], &hdr);
843 ASSERT(hdr.inode == fdb->inode);
847 fd->seek_pos += read_len;
848 total_read += read_len;
856 * Search file \a inode in \a disk using a binary search.
857 * \a last is filled with array offset of file start
858 * in disk->page_array if file is found, otherwise
859 * \a last is filled with the correct insert position
860 * for creating a file with the given \a inode.
861 * \return true if file is found, false otherwisr.
863 static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
865 BattFsPageHeader hdr;
866 pgcnt_t first = 0, page;
867 *last = disk->free_page_start;
870 while (first < *last)
872 page = (first + *last) / 2;
873 LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
874 if (!readHdr(disk, disk->page_array[page], &hdr))
876 LOG_INFO("inode read: %d\n", hdr.inode);
877 fcs = computeFcs(&hdr);
878 if (hdr.fcs == fcs && hdr.inode == inode)
880 *last = page - hdr.pgoff;
881 LOG_INFO("Found: %d\n", *last);
884 else if (hdr.fcs == fcs && hdr.inode < inode)
889 LOG_INFO("Not found: last %d\n", *last);
894 * \return true if file \a inode exists on \a disk, false otherwise.
896 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
899 return findFile(disk, inode, &dummy);
903 * Count size of file \a inode on \a disk, starting at pointer \a start
904 * in disk->page_array. Size is written in \a size.
905 * \return true if all s ok, false on disk read errors.
907 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
909 file_size_t size = 0;
910 BattFsPageHeader hdr;
912 while (start < &disk->page_array[disk->free_page_start])
914 if (!readHdr(disk, *start++, &hdr))
916 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
924 static int battfs_error(struct KFile *fd)
926 BattFs *fdb = BATTFS_CAST(fd);
931 static void battfs_clearerr(struct KFile *fd)
933 BattFs *fdb = BATTFS_CAST(fd);
938 * Open file \a inode from \a disk in \a mode.
939 * File context is stored in \a fd.
940 * \return true if ok, false otherwise.
942 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
946 memset(fd, 0, sizeof(*fd));
948 /* Search file start point in disk page array */
950 if (!findFile(disk, inode, &start_pos))
952 LOG_INFO("file %d not found\n", inode);
953 if (!(mode & BATTFS_CREATE))
955 fd->errors |= BATTFS_FILE_NOT_FOUND_ERR;
958 /* Create the file */
959 BattFsPageHeader hdr;
960 if (!(getNewPage(disk, start_pos, inode, 0, &hdr)))
962 fd->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
966 fd->start = &disk->page_array[start_pos];
967 LOG_INFO("Start pos %d\n", start_pos);
970 if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
972 fd->errors |= BATTFS_DISK_READ_ERR;
975 fd->max_off = fd->fd.size / disk->data_size;
977 /* Reset seek position */
980 /* Insert file handle in list, ordered by inode, ascending. */
981 FOREACH_NODE(n, &disk->file_opened_list)
983 BattFs *file = containerof(n, BattFs, link);
984 if (file->inode >= inode)
987 INSERT_BEFORE(&fd->link, n);
994 fd->fd.close = battfs_fileclose;
995 fd->fd.flush = battfs_flush;
996 fd->fd.read = battfs_read;
997 fd->fd.reopen = kfile_genericReopen;
998 fd->fd.seek = kfile_genericSeek;
999 fd->fd.write = battfs_write;
1001 fd->fd.error = battfs_error;
1002 fd->fd.clearerr = battfs_clearerr;
1004 DB(fd->fd._type = KFT_BATTFS);
1013 bool battfs_umount(struct BattFsSuper *disk)
1018 /* Close all open files */
1019 FOREACH_NODE(n, &disk->file_opened_list)
1021 BattFs *file = containerof(n, BattFs, link);
1022 res += battfs_fileclose(&file->fd);
1026 return disk->close(disk) && (res == 0);
1031 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
1033 BattFsPageHeader hdr;
1035 /* Reset page to all 0xff */
1036 uint8_t buf[disk->page_size];
1037 memset(buf, 0xFF, disk->page_size);
1038 disk->bufferWrite(disk, 0, buf, disk->page_size);
1044 hdr.fcs = computeFcs(&hdr);
1046 if (!(setBufferHdr(disk, &hdr) && disk->save(disk, page)))
1048 LOG_ERR("error writing hdr\n");