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 */
46 #include <cpu/byteorder.h> /* cpu_to_xx */
48 #define LOG_LEVEL LOG_LVL_INFO
49 #define LOG_FORMAT LOG_FMT_VERBOSE
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 #warning TODO: shuffle free blocks
494 //#if LOG_LEVEL > LOG_LVL_INFO
495 // dumpPageArray(disk);
497 /* Init list for opened files. */
498 LIST_INIT(&disk->file_opened_list);
503 * Check the filesystem.
504 * \return true if ok, false on errors.
506 bool battfs_fsck(struct BattFsSuper *disk)
508 #define FSCHECK(cond) do { if(!(cond)) { LOG_ERR("\"" #cond "\"\n"); return false; } } while (0)
510 FSCHECK(disk->free_page_start <= disk->page_count);
511 FSCHECK(disk->data_size < disk->page_size);
512 FSCHECK(disk->free_bytes <= disk->disk_size);
514 disk_size_t free_bytes = 0;
515 BattFsPageHeader hdr, prev_hdr;
517 pgcnt_t page_used = 0;
521 for (pgcnt_t page = 0; page < disk->page_count; page++)
523 FSCHECK(readHdr(disk, disk->page_array[page], &hdr));
524 free_bytes += disk->data_size;
526 if (page < disk->free_page_start)
528 FSCHECK(computeFcs(&hdr) == hdr.fcs);
530 free_bytes -= hdr.fill;
531 if (hdr.inode != prev_hdr.inode || start)
534 FSCHECK(hdr.inode > prev_hdr.inode);
538 FSCHECK(hdr.pgoff == 0);
543 FSCHECK(hdr.fill != 0);
544 FSCHECK(prev_hdr.fill == disk->data_size);
545 FSCHECK(hdr.pgoff == prev_hdr.pgoff + 1);
551 FSCHECK(page_used == disk->free_page_start);
552 FSCHECK(free_bytes == disk->free_bytes);
559 * \return 0 if ok, EOF on errors.
561 static int battfs_flush(struct KFile *fd)
563 BattFs *fdb = BATTFS_CAST(fd);
565 if (flushBuffer(fdb->disk))
569 fdb->errors |= BATTFS_DISK_FLUSHBUF_ERR;
576 * \return 0 if ok, EOF on errors.
578 static int battfs_fileclose(struct KFile *fd)
580 BattFs *fdb = BATTFS_CAST(fd);
582 if (battfs_flush(fd) == 0)
592 static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
594 if (SPACE_OVER(disk))
596 LOG_ERR("No disk space available!\n");
600 LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
601 disk->curr_page = disk->page_array[disk->free_page_start++];
602 memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
605 /* Move following file start point one position ahead. */
606 FOREACH_NODE(n, &disk->file_opened_list)
608 BattFs *file = containerof(n, BattFs, link);
609 if (file->inode > inode)
611 LOG_INFO("Move file %d start pos\n", file->inode);
616 disk->page_array[new_pos] = disk->curr_page;
617 disk->cache_dirty = true;
619 new_hdr->inode = inode;
620 new_hdr->pgoff = pgoff;
623 return setBufferHdr(disk, new_hdr);
627 * Write to file \a fd \a size bytes from \a buf.
628 * \return The number of bytes written.
630 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
632 BattFs *fdb = BATTFS_CAST(fd);
633 BattFsSuper *disk = fdb->disk;
634 const uint8_t *buf = (const uint8_t *)_buf;
636 size_t total_write = 0;
638 pgaddr_t addr_offset;
640 BattFsPageHeader curr_hdr;
642 if (fd->seek_pos < 0)
644 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
648 if (fd->seek_pos > fd->size)
650 /* Handle writing when seek pos if far over EOF */
651 if (!loadPage(disk, fdb->start[fdb->max_off], &curr_hdr))
653 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
657 /* Fill unused space of first page with 0s */
659 pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, (kfile_off_t)(disk->data_size - curr_hdr.fill));
662 if (disk->bufferWrite(disk, curr_hdr.fill, &dummy, 1) != 1)
664 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
670 disk->cache_dirty = true;
672 setBufferHdr(disk, &curr_hdr);
674 /* Allocate the missing pages first. */
675 pgoff_t missing_pages = fd->seek_pos / disk->data_size - fdb->max_off;
679 LOG_INFO("missing pages: %d\n", missing_pages);
682 /* Fill page buffer with 0 to avoid filling unused pages with garbage */
683 for (pgaddr_t off = 0; off < disk->data_size; off++)
685 if (disk->bufferWrite(disk, off, &dummy, 1) != 1)
687 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
692 while (missing_pages--)
694 zero_bytes = MIN((kfile_off_t)disk->data_size, fd->seek_pos - fd->size);
695 /* Get the new page needed */
696 if (!getNewPage(disk, (fdb->start - disk->page_array) + fdb->max_off + 1, fdb->inode, fdb->max_off + 1, &curr_hdr))
698 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
702 /* Update size and free space left */
703 fd->size += zero_bytes;
704 disk->free_bytes -= zero_bytes;
706 curr_hdr.fill = zero_bytes;
707 setBufferHdr(disk, &curr_hdr);
713 else if (!getBufferHdr(disk, &curr_hdr))
715 fdb->errors |= BATTFS_DISK_BUFFERRD_ERR;
721 pg_offset = fd->seek_pos / disk->data_size;
722 addr_offset = fd->seek_pos % disk->data_size;
723 wr_len = MIN(size, (size_t)(disk->data_size - addr_offset));
725 /* Handle write outside EOF */
726 if (pg_offset > fdb->max_off)
728 LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (int)((fdb->start - disk->page_array) + pg_offset));
729 if (!getNewPage(disk, (fdb->start - disk->page_array) + pg_offset, fdb->inode, pg_offset, &curr_hdr))
731 fdb->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
735 fdb->max_off = pg_offset;
737 /* Handle cache load of a new page*/
738 else if (fdb->start[pg_offset] != disk->curr_page)
740 if (SPACE_OVER(disk))
742 LOG_ERR("No disk space available!\n");
743 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
746 LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], disk->page_array[disk->free_page_start]);
747 if (!loadPage(disk, fdb->start[pg_offset], &curr_hdr))
749 fdb->errors |= BATTFS_DISK_LOADPAGE_ERR;
753 /* Get a free page */
754 disk->curr_page = disk->page_array[disk->free_page_start];
755 movePages(disk, disk->free_page_start + 1, -1);
757 /* Insert previous page in free blocks list */
758 LOG_INFO("Setting page %d as free\n", fdb->start[pg_offset]);
759 disk->page_array[disk->page_count - 1] = fdb->start[pg_offset];
760 /* Assign new page */
761 fdb->start[pg_offset] = disk->curr_page;
765 //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", disk->curr_page, addr_offset, wr_len);
766 if (disk->bufferWrite(disk, addr_offset, buf, wr_len) != wr_len)
768 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
771 disk->cache_dirty = true;
774 fd->seek_pos += wr_len;
775 total_write += wr_len;
777 fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
778 disk->free_bytes -= fill_delta;
779 fd->size += fill_delta;
780 curr_hdr.fill += fill_delta;
782 if (!setBufferHdr(disk, &curr_hdr))
784 fdb->errors |= BATTFS_DISK_BUFFERWR_ERR;
788 //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);
795 * Read from file \a fd \a size bytes in \a buf.
796 * \return The number of bytes read.
798 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
800 BattFs *fdb = BATTFS_CAST(fd);
801 BattFsSuper *disk = fdb->disk;
802 uint8_t *buf = (uint8_t *)_buf;
804 size_t total_read = 0;
806 pgaddr_t addr_offset;
809 if (fd->seek_pos < 0)
811 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
815 size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, (kfile_off_t)0));
819 pg_offset = fd->seek_pos / disk->data_size;
820 addr_offset = fd->seek_pos % disk->data_size;
821 read_len = MIN(size, (size_t)(disk->data_size - addr_offset));
823 //LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
825 if (diskRead(disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
827 fdb->errors |= BATTFS_DISK_READ_ERR;
832 BattFsPageHeader hdr;
833 readHdr(disk, fdb->start[pg_offset], &hdr);
834 ASSERT(hdr.inode == fdb->inode);
838 fd->seek_pos += read_len;
839 total_read += read_len;
847 * Search file \a inode in \a disk using a binary search.
848 * \a last is filled with array offset of file start
849 * in disk->page_array if file is found, otherwise
850 * \a last is filled with the correct insert position
851 * for creating a file with the given \a inode.
852 * \return true if file is found, false otherwisr.
854 static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
856 BattFsPageHeader hdr;
857 pgcnt_t first = 0, page;
858 *last = disk->free_page_start;
861 while (first < *last)
863 page = (first + *last) / 2;
864 LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
865 if (!readHdr(disk, disk->page_array[page], &hdr))
867 LOG_INFO("inode read: %d\n", hdr.inode);
868 fcs = computeFcs(&hdr);
869 if (hdr.fcs == fcs && hdr.inode == inode)
871 *last = page - hdr.pgoff;
872 LOG_INFO("Found: %d\n", *last);
875 else if (hdr.fcs == fcs && hdr.inode < inode)
880 LOG_INFO("Not found: last %d\n", *last);
885 * \return true if file \a inode exists on \a disk, false otherwise.
887 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
890 return findFile(disk, inode, &dummy);
894 * Count size of file \a inode on \a disk, starting at pointer \a start
895 * in disk->page_array. Size is written in \a size.
896 * \return true if all s ok, false on disk read errors.
898 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
900 file_size_t size = 0;
901 BattFsPageHeader hdr;
903 while (start < &disk->page_array[disk->free_page_start])
905 if (!readHdr(disk, *start++, &hdr))
907 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
915 static int battfs_error(struct KFile *fd)
917 BattFs *fdb = BATTFS_CAST(fd);
922 static void battfs_clearerr(struct KFile *fd)
924 BattFs *fdb = BATTFS_CAST(fd);
929 * Open file \a inode from \a disk in \a mode.
930 * File context is stored in \a fd.
931 * \return true if ok, false otherwise.
933 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
937 memset(fd, 0, sizeof(*fd));
939 /* Search file start point in disk page array */
941 if (!findFile(disk, inode, &start_pos))
943 LOG_INFO("file %d not found\n", inode);
944 if (!(mode & BATTFS_CREATE))
946 fd->errors |= BATTFS_FILE_NOT_FOUND_ERR;
949 /* Create the file */
950 BattFsPageHeader hdr;
951 if (!(getNewPage(disk, start_pos, inode, 0, &hdr)))
953 fd->errors |= BATTFS_DISK_GETNEWPAGE_ERR;
957 fd->start = &disk->page_array[start_pos];
958 LOG_INFO("Start pos %d\n", start_pos);
961 if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
963 fd->errors |= BATTFS_DISK_READ_ERR;
966 fd->max_off = fd->fd.size / disk->data_size;
968 /* Reset seek position */
971 /* Insert file handle in list, ordered by inode, ascending. */
972 FOREACH_NODE(n, &disk->file_opened_list)
974 BattFs *file = containerof(n, BattFs, link);
975 if (file->inode >= inode)
978 INSERT_BEFORE(&fd->link, n);
985 fd->fd.close = battfs_fileclose;
986 fd->fd.flush = battfs_flush;
987 fd->fd.read = battfs_read;
988 fd->fd.reopen = kfile_genericReopen;
989 fd->fd.seek = kfile_genericSeek;
990 fd->fd.write = battfs_write;
992 fd->fd.error = battfs_error;
993 fd->fd.clearerr = battfs_clearerr;
995 DB(fd->fd._type = KFT_BATTFS);
1004 bool battfs_umount(struct BattFsSuper *disk)
1009 /* Close all open files */
1010 FOREACH_NODE(n, &disk->file_opened_list)
1012 BattFs *file = containerof(n, BattFs, link);
1013 res += battfs_fileclose(&file->fd);
1017 return disk->close(disk) && (res == 0);
1022 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
1024 BattFsPageHeader hdr;
1026 /* Reset page to all 0xff */
1027 uint8_t buf[disk->page_size];
1028 memset(buf, 0xFF, disk->page_size);
1029 disk->bufferWrite(disk, 0, buf, disk->page_size);
1035 hdr.fcs = computeFcs(&hdr);
1037 if (!(setBufferHdr(disk, &hdr) && disk->save(disk, page)))
1039 LOG_ERR("error writing hdr\n");