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);
132 * Read header of \a page in \a hdr.
133 * \return true on success, false otherwise.
135 static bool readHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
137 uint8_t buf[BATTFS_HEADER_LEN];
140 * Read header from disk.
141 * Header is actually a footer, and so
142 * resides at page end.
144 if (kblock_read(disk->dev, page, buf, disk->data_size, BATTFS_HEADER_LEN)
145 != BATTFS_HEADER_LEN)
147 LOG_ERR("page[%d]\n", page);
152 disk_to_battfs(buf, hdr);
157 static bool writeHdr(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
159 uint8_t buf[BATTFS_HEADER_LEN];
161 #warning FIXME:refactor computeFcs to save time and stack
162 hdr->fcs = computeFcs(hdr);
164 battfs_to_disk(hdr, buf);
167 * write header to disk.
168 * Header is actually a footer, and so
169 * resides at page end.
171 if (kblock_write(disk->dev, page, buf, disk->data_size, BATTFS_HEADER_LEN)
172 != BATTFS_HEADER_LEN)
174 LOG_ERR("writing to buffer\n");
182 * Count the number of pages from
183 * inode 0 to \a inode in \a filelen_table.
185 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
189 for (inode_t i = 0; i < inode; i++)
190 cnt += filelen_table[i];
196 * Move all pages in page allocation array from \a src to \a src + \a offset.
197 * The number of pages moved is page_count - MAX(dst, src).
199 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
201 pgcnt_t dst = src + offset;
202 LOG_INFO("src %d, offset %d, size %d\n", src, offset, (unsigned int)((disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t)));
203 memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
207 /* Fill empty space in array with sentinel */
208 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
209 disk->page_array[page] = PAGE_UNSET_SENTINEL;
214 * Count number of pages per file on \a disk.
215 * This information is registered in \a filelen_table.
216 * Array index represent file inode, while value contained
217 * is the number of pages used by that file.
219 * \return true if ok, false on disk read errors.
220 * \note The whole disk is scanned once.
222 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
224 BattFsPageHeader hdr;
225 disk->free_page_start = 0;
227 /* Count the number of disk page per file */
228 for (pgcnt_t page = 0; page < disk->page_count; page++)
230 if (!readHdr(disk, page, &hdr))
233 /* Increase free space */
234 disk->free_bytes += disk->data_size;
236 /* Check header FCS */
237 if (hdr.fcs == computeFcs(&hdr))
239 ASSERT(hdr.fill <= disk->data_size);
241 /* Page is valid and is owned by a file */
242 filelen_table[hdr.inode]++;
244 /* Keep trace of free space */
245 disk->free_bytes -= hdr.fill;
246 disk->free_page_start++;
249 LOG_INFO("free_bytes:%ld, free_page_start:%d\n", (long)disk->free_bytes, disk->free_page_start);
255 * Fill page allocation array of \a disk
256 * using file lenghts in \a filelen_table.
258 * The page allocation array is an array containings all file infos.
259 * Is ordered by file, and within each file is ordered by page offset
261 * e.g. : at page array[0] you will find page address of the first page
262 * of the first file (if present).
263 * Free blocks are allocated after the last file.
265 * \return true if ok, false on disk read errors.
266 * \note The whole disk is scanned at max twice.
268 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
270 BattFsPageHeader hdr;
271 pgcnt_t curr_free_page = disk->free_page_start;
272 /* Fill page allocation array */
273 for (pgcnt_t page = 0; page < disk->page_count; page++)
275 if (!readHdr(disk, page, &hdr))
278 /* Check header FCS */
279 if (hdr.fcs == computeFcs(&hdr))
281 /* Compute array position */
282 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
283 array_pos += hdr.pgoff;
286 /* Check if position is already used by another page of the same file */
287 if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
288 disk->page_array[array_pos] = page;
291 BattFsPageHeader hdr_prv;
293 if (!readHdr(disk, disk->page_array[array_pos], &hdr_prv))
296 /* Check header FCS */
297 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
299 /* Only the very same page with a different seq number can be here */
300 ASSERT(hdr.inode == hdr_prv.inode);
301 ASSERT(hdr.pgoff == hdr_prv.pgoff);
302 ASSERT(hdr.seq != hdr_prv.seq);
304 pgcnt_t new_page, old_page;
308 * Sequence number comparison: since
309 * seq is 40 bits wide, it wraps once
310 * every 1.1E12 times.
311 * The memory will not live enough to
312 * see a wraparound, so we can use a simple
315 if (hdr.seq > hdr_prv.seq)
317 /* Current header is newer than the previuos one */
318 old_page = disk->page_array[array_pos];
320 old_fill = hdr_prv.fill;
324 /* Previous header is newer than the current one */
326 new_page = disk->page_array[array_pos];
331 disk->page_array[array_pos] = new_page;
333 disk->free_bytes += old_fill;
334 /* Shift all array one position to the left, overwriting duplicate page */
335 array_pos -= hdr.pgoff;
336 array_pos += filelen_table[hdr.inode];
337 movePages(disk, array_pos, -1);
338 /* Move back all indexes */
339 filelen_table[hdr.inode]--;
340 disk->free_page_start--;
342 /* Set old page as free */
343 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
344 disk->page_array[curr_free_page++] = old_page;
350 /* Invalid page, keep as free */
351 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
352 //LOG_INFO("Page %d invalid, keeping as free\n", page);
353 disk->page_array[curr_free_page++] = page;
361 * Initialize and mount disk described by
363 * \return false on errors, true otherwise.
365 bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size)
367 pgoff_t filelen_table[BATTFS_MAX_FILES];
371 disk->page_size = dev->blk_size;
372 disk->page_count = dev->blk_cnt;
374 ASSERT(disk->page_size > BATTFS_HEADER_LEN);
375 /* Fill page_size with the usable space */
376 disk->data_size = disk->page_size - BATTFS_HEADER_LEN;
377 ASSERT(disk->page_count);
378 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
380 disk->page_array = page_array;
381 ASSERT(array_size >= disk->page_count * sizeof(pgcnt_t));
383 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
385 disk->free_bytes = 0;
386 disk->disk_size = (disk_size_t)disk->data_size * disk->page_count;
388 /* Count pages per file */
389 if (!countDiskFilePages(disk, filelen_table))
391 LOG_ERR("counting file pages\n");
395 /* Once here, we have filelen_table filled with file lengths */
397 /* Fill page array with sentinel */
398 for (pgcnt_t page = 0; page < disk->page_count; page++)
399 disk->page_array[page] = PAGE_UNSET_SENTINEL;
401 /* Fill page allocation array using filelen_table */
402 if (!fillPageArray(disk, filelen_table))
404 LOG_ERR("filling page array\n");
407 #if LOG_LEVEL >= LOG_LVL_INFO
410 #if CONFIG_BATTFS_SHUFFLE_FREE_PAGES
411 SHUFFLE(&disk->page_array[disk->free_page_start], disk->page_count - disk->free_page_start);
413 LOG_INFO("Page array after shuffle:\n");
414 #if LOG_LEVEL >= LOG_LVL_INFO
418 /* Init list for opened files. */
419 LIST_INIT(&disk->file_opened_list);
424 * Check the filesystem.
425 * \return true if ok, false on errors.
427 bool battfs_fsck(struct BattFsSuper *disk)
429 #define FSCHECK(cond) do { if(!(cond)) { LOG_ERR("\"" #cond "\"\n"); return false; } } while (0)
431 FSCHECK(disk->free_page_start <= disk->page_count);
432 FSCHECK(disk->data_size < disk->page_size);
433 FSCHECK(disk->free_bytes <= disk->disk_size);
435 disk_size_t free_bytes = 0;
436 BattFsPageHeader hdr, prev_hdr;
438 pgcnt_t page_used = 0;
442 /* Uneeded, the first time will be overwritten but useful to silence
443 * the warning for uninitialized value */
444 FSCHECK(readHdr(disk, 0, &prev_hdr));
445 for (pgcnt_t page = 0; page < disk->page_count; page++)
447 FSCHECK(readHdr(disk, disk->page_array[page], &hdr));
448 free_bytes += disk->data_size;
450 if (page < disk->free_page_start)
452 FSCHECK(computeFcs(&hdr) == hdr.fcs);
454 free_bytes -= hdr.fill;
455 if (hdr.inode != prev_hdr.inode || start)
458 FSCHECK(hdr.inode > prev_hdr.inode);
462 FSCHECK(hdr.pgoff == 0);
467 FSCHECK(hdr.fill != 0);
468 FSCHECK(prev_hdr.fill == disk->data_size);
469 FSCHECK(hdr.pgoff == prev_hdr.pgoff + 1);
475 FSCHECK(page_used == disk->free_page_start);
476 FSCHECK(free_bytes == disk->free_bytes);
483 * \return 0 if ok, EOF on errors.
485 static int battfs_flush(struct KFile *fd)
487 BattFs *fdb = BATTFS_CAST(fd);
489 if (kblock_flush(fdb->disk->dev) == 0)
493 fdb->errors |= BATTFS_DISK_FLUSHBUF_ERR;
500 * \return 0 if ok, EOF on errors.
502 static int battfs_fileclose(struct KFile *fd)
504 BattFs *fdb = BATTFS_CAST(fd);
506 if (battfs_flush(fd) == 0)
515 #define NO_SPACE PAGE_UNSET_SENTINEL
517 static pgcnt_t allocateNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode)
519 if (SPACE_OVER(disk))
521 LOG_ERR("No disk space available!\n");
525 LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
526 pgcnt_t new_page = disk->page_array[disk->free_page_start++];
527 memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
530 /* Move following file start point one position ahead. */
531 FOREACH_NODE(n, &disk->file_opened_list)
533 BattFs *file = containerof(n, BattFs, link);
534 if (file->inode > inode)
536 LOG_INFO("Move file %d start pos\n", file->inode);
541 disk->page_array[new_pos] = new_page;
545 static pgcnt_t renewPage(struct BattFsSuper *disk, pgcnt_t old_pos)
547 if (SPACE_OVER(disk))
549 LOG_ERR("No disk space available!\n");
550 return PAGE_UNSET_SENTINEL;
553 /* Get a free page */
554 pgcnt_t new_page = disk->page_array[disk->free_page_start];
555 movePages(disk, disk->free_page_start + 1, -1);
557 /* Insert previous page in free blocks list */
558 LOG_INFO("Setting page %d as free\n", old_pos);
559 disk->page_array[disk->page_count - 1] = old_pos;
564 * Write to file \a fd \a size bytes from \a buf.
565 * \return The number of bytes written.
567 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
569 BattFs *fdb = BATTFS_CAST(fd);
570 BattFsSuper *disk = fdb->disk;
571 const uint8_t *buf = (const uint8_t *)_buf;
573 size_t total_write = 0;
575 pgaddr_t addr_offset;
577 BattFsPageHeader curr_hdr;
580 if (fd->seek_pos < 0)
582 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
586 if (fd->seek_pos > fd->size)
588 if (!readHdr(disk, fdb->start[fdb->max_off], &curr_hdr))
590 fdb->errors |= BATTFS_DISK_READ_ERR;
594 new_page = renewPage(disk, fdb->start[fdb->max_off]);
595 if (new_page == NO_SPACE)
597 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
601 kblock_copy(disk->dev, fdb->start[fdb->max_off], new_page);
602 fdb->start[fdb->max_off] = new_page;
604 /* Fill unused space of first page with 0s */
606 pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, (kfile_off_t)(disk->data_size - curr_hdr.fill));
609 if (kblock_write(disk->dev, new_page, &dummy, curr_hdr.fill, 1) != 1)
611 fdb->errors |= BATTFS_DISK_WRITE_ERR;
619 if (!writeHdr(disk, new_page, &curr_hdr))
621 fdb->errors |= BATTFS_DISK_WRITE_ERR;
625 /* Allocate the missing pages first. */
626 pgoff_t missing_pages = fd->seek_pos / disk->data_size - fdb->max_off;
630 LOG_INFO("missing pages: %d\n", missing_pages);
632 while (missing_pages--)
634 zero_bytes = MIN((kfile_off_t)disk->data_size, fd->seek_pos - fd->size);
636 new_page = allocateNewPage(disk, (fdb->start - disk->page_array) + fdb->max_off + 1, fdb->inode);
637 if (new_page == NO_SPACE)
639 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
644 /* Fill page buffer with 0 to avoid filling unused pages with garbage */
645 for (pgaddr_t off = 0; off < disk->data_size; off++)
647 if (kblock_write(disk->dev, new_page, &dummy, off, 1) != 1)
649 fdb->errors |= BATTFS_DISK_WRITE_ERR;
653 curr_hdr.inode = fdb->inode;
654 curr_hdr.pgoff = ++fdb->max_off;
655 curr_hdr.fill = zero_bytes;
658 if (!writeHdr(disk, new_page, &curr_hdr))
660 fdb->errors |= BATTFS_DISK_WRITE_ERR;
664 /* Update size and free space left */
665 fd->size += zero_bytes;
666 disk->free_bytes -= zero_bytes;
673 pg_offset = fd->seek_pos / disk->data_size;
674 addr_offset = fd->seek_pos % disk->data_size;
675 wr_len = MIN(size, (size_t)(disk->data_size - addr_offset));
677 /* Handle write outside EOF */
678 if (pg_offset > fdb->max_off)
680 LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (int)((fdb->start - disk->page_array) + pg_offset));
682 new_page = allocateNewPage(disk, (fdb->start - disk->page_array) + pg_offset, fdb->inode);
683 if (new_page == NO_SPACE)
685 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
689 curr_hdr.inode = fdb->inode;
690 curr_hdr.pgoff = pg_offset;
693 fdb->max_off = pg_offset;
697 if (!readHdr(disk, fdb->start[pg_offset], &curr_hdr))
699 fdb->errors |= BATTFS_DISK_READ_ERR;
703 new_page = renewPage(disk, fdb->start[pg_offset]);
704 if (new_page == NO_SPACE)
706 fdb->errors |= BATTFS_DISK_SPACEOVER_ERR;
710 LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], new_page);
711 if (kblock_copy(disk->dev, fdb->start[pg_offset], new_page) != 0)
713 fdb->errors |= BATTFS_DISK_WRITE_ERR;
716 fdb->start[pg_offset] = new_page;
719 //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", disk->curr_page, addr_offset, wr_len);
720 if (kblock_write(disk->dev, new_page, buf, addr_offset, wr_len) != wr_len)
722 fdb->errors |= BATTFS_DISK_WRITE_ERR;
727 fd->seek_pos += wr_len;
728 total_write += wr_len;
730 fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
731 disk->free_bytes -= fill_delta;
732 fd->size += fill_delta;
733 curr_hdr.fill += fill_delta;
735 if (!writeHdr(disk, new_page, &curr_hdr))
737 fdb->errors |= BATTFS_DISK_WRITE_ERR;
741 //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);
748 * Read from file \a fd \a size bytes in \a buf.
749 * \return The number of bytes read.
751 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
753 BattFs *fdb = BATTFS_CAST(fd);
754 BattFsSuper *disk = fdb->disk;
755 uint8_t *buf = (uint8_t *)_buf;
757 size_t total_read = 0;
759 pgaddr_t addr_offset;
762 if (fd->seek_pos < 0)
764 fdb->errors |= BATTFS_NEGATIVE_SEEK_ERR;
768 size = MIN((kfile_off_t)size, MAX(fd->size - fd->seek_pos, (kfile_off_t)0));
772 pg_offset = fd->seek_pos / disk->data_size;
773 addr_offset = fd->seek_pos % disk->data_size;
774 read_len = MIN(size, (size_t)(disk->data_size - addr_offset));
776 //LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
778 if (kblock_read(disk->dev, fdb->start[pg_offset], buf, addr_offset, read_len) != read_len)
780 fdb->errors |= BATTFS_DISK_READ_ERR;
785 BattFsPageHeader hdr;
786 readHdr(disk, fdb->start[pg_offset], &hdr);
787 ASSERT(hdr.inode == fdb->inode);
791 fd->seek_pos += read_len;
792 total_read += read_len;
800 * Search file \a inode in \a disk using a binary search.
801 * \a last is filled with array offset of file start
802 * in disk->page_array if file is found, otherwise
803 * \a last is filled with the correct insert position
804 * for creating a file with the given \a inode.
805 * \return true if file is found, false otherwisr.
807 static bool findFile(BattFsSuper *disk, inode_t inode, pgcnt_t *last)
809 BattFsPageHeader hdr;
810 pgcnt_t first = 0, page;
811 *last = disk->free_page_start;
814 while (first < *last)
816 page = (first + *last) / 2;
817 LOG_INFO("first %d, last %d, page %d\n", first, *last, page);
818 if (!readHdr(disk, disk->page_array[page], &hdr))
820 LOG_INFO("inode read: %d\n", hdr.inode);
821 fcs = computeFcs(&hdr);
822 if (hdr.fcs == fcs && hdr.inode == inode)
824 *last = page - hdr.pgoff;
825 LOG_INFO("Found: %d\n", *last);
828 else if (hdr.fcs == fcs && hdr.inode < inode)
833 LOG_INFO("Not found: last %d\n", *last);
838 * \return true if file \a inode exists on \a disk, false otherwise.
840 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
843 return findFile(disk, inode, &dummy);
847 * Count size of file \a inode on \a disk, starting at pointer \a start
848 * in disk->page_array. Size is written in \a size.
849 * \return true if all s ok, false on disk read errors.
851 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
853 file_size_t size = 0;
854 BattFsPageHeader hdr;
856 while (start < &disk->page_array[disk->free_page_start])
858 if (!readHdr(disk, *start++, &hdr))
860 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
868 static int battfs_error(struct KFile *fd)
870 BattFs *fdb = BATTFS_CAST(fd);
875 static void battfs_clearerr(struct KFile *fd)
877 BattFs *fdb = BATTFS_CAST(fd);
882 * Open file \a inode from \a disk in \a mode.
883 * File context is stored in \a fd.
884 * \return true if ok, false otherwise.
886 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
890 memset(fd, 0, sizeof(*fd));
892 /* Search file start point in disk page array */
894 if (!findFile(disk, inode, &start_pos))
896 LOG_INFO("file %d not found\n", inode);
897 if (!(mode & BATTFS_CREATE))
899 fd->errors |= BATTFS_FILE_NOT_FOUND_ERR;
902 /* Create the file */
903 BattFsPageHeader hdr;
905 if (allocateNewPage(disk, start_pos, inode) == NO_SPACE)
907 fd->errors |= BATTFS_DISK_SPACEOVER_ERR;
915 if (!writeHdr(disk, disk->page_array[start_pos], &hdr))
917 fd->errors |= BATTFS_DISK_WRITE_ERR;
921 fd->start = &disk->page_array[start_pos];
922 LOG_INFO("Start pos %d\n", start_pos);
925 if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
927 fd->errors |= BATTFS_DISK_READ_ERR;
930 fd->max_off = fd->fd.size / disk->data_size;
932 /* Reset seek position */
935 /* Insert file handle in list, ordered by inode, ascending. */
936 FOREACH_NODE(n, &disk->file_opened_list)
938 BattFs *file = containerof(n, BattFs, link);
939 if (file->inode >= inode)
942 INSERT_BEFORE(&fd->link, n);
949 fd->fd.close = battfs_fileclose;
950 fd->fd.flush = battfs_flush;
951 fd->fd.read = battfs_read;
952 fd->fd.reopen = kfile_genericReopen;
953 fd->fd.seek = kfile_genericSeek;
954 fd->fd.write = battfs_write;
956 fd->fd.error = battfs_error;
957 fd->fd.clearerr = battfs_clearerr;
959 DB(fd->fd._type = KFT_BATTFS);
968 bool battfs_umount(struct BattFsSuper *disk)
973 /* Close all open files */
974 FOREACH_NODE(n, &disk->file_opened_list)
976 BattFs *file = containerof(n, BattFs, link);
977 res += battfs_fileclose(&file->fd);
981 return (kblock_flush(disk->dev) == 0) && (kblock_close(disk->dev) == 0) && (res == 0);
986 void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
988 uint8_t buf[BATTFS_HEADER_LEN];
989 battfs_eraseBlock(dev, page);
991 BattFsPageHeader hdr;
996 hdr.fcs = computeFcs(&hdr);
998 battfs_to_disk(&hdr, buf);
1000 ASSERT(kblock_write(dev, page, buf, dev->blk_size - BATTFS_HEADER_LEN, BATTFS_HEADER_LEN) == BATTFS_HEADER_LEN);
1001 ASSERT(kblock_flush(dev) == 0);
1004 void battfs_eraseBlock(KBlock *dev, pgcnt_t page)
1006 /* Reset page to all 0xff */
1007 uint8_t buf[dev->blk_size];
1008 memset(buf, 0xFF, dev->blk_size);
1009 ASSERT(kblock_write(dev, page, buf, 0, dev->blk_size) == dev->blk_size);
1010 ASSERT(kblock_flush(dev) == 0);