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 */
49 #include <string.h> /* memset, memmove */
53 * Convert from memory representation to disk structure.
54 * \note filesystem is in little-endian format.
56 INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf)
58 STATIC_ASSERT(BATTFS_HEADER_LEN == 10);
62 buf[2] = hdr->fill >> 8;
65 buf[4] = hdr->pgoff >> 8;
68 * Sequence number is at least 1 bit longer than page address.
69 * Needed to take care of wraparonds.
72 buf[6] = hdr->seq >> 8;
75 * First bit used by seq.
76 * Unused bits are set to 1.
78 buf[7] = (hdr->seq >> 16) ? 0xFF : 0xFE;
81 * This field must be the last one!
82 * This is needed because if the page is only partially
83 * written, we can use this to detect it.
86 buf[9] = hdr->fcs >> 8;
90 * Convert from disk structure to memory representation.
91 * \note filesystem is in little-endian format.
93 INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr)
95 STATIC_ASSERT(BATTFS_HEADER_LEN == 10);
97 hdr->fill = buf[2] << 8 | buf[1];
98 hdr->pgoff = buf[4] << 8 | buf[3];
99 hdr->seq = (seq_t)(buf[7] & 0x01) << 16 | buf[6] << 8 | buf[5];
100 hdr->fcs = buf[9] << 8 | buf[8];
104 * Compute the fcs of the header.
106 static fcs_t computeFcs(struct BattFsPageHeader *hdr)
108 uint8_t buf[BATTFS_HEADER_LEN];
111 battfs_to_disk(hdr, buf);
113 /* fcs is at the end of whole header */
114 rotating_update(buf, BATTFS_HEADER_LEN - sizeof(fcs_t), &cks);
120 * Read header of page \a page.
121 * \return true on success, false otherwise.
123 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
125 uint8_t buf[BATTFS_HEADER_LEN];
127 * Read header from disk.
128 * Header is actually a footer, and so
129 * resides at page end.
131 if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
132 != BATTFS_HEADER_LEN)
134 TRACEMSG("Error: page[%d]\n", page);
139 disk_to_battfs(buf, hdr);
145 * Write header of page \a page.
146 * \return true on success, false otherwise.
148 static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
150 uint8_t buf[BATTFS_HEADER_LEN];
153 battfs_to_disk(hdr, buf);
156 * write header to disk.
157 * Header is actually a footer, and so
158 * resides at page end.
160 if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
161 != BATTFS_HEADER_LEN)
163 TRACEMSG("Error: page[%d]\n", page);
170 * Count the number of pages from
171 * inode 0 to \a inode in \a filelen_table.
173 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
177 for (inode_t i = 0; i < inode; i++)
178 cnt += filelen_table[i];
184 * Move all pages in page allocation array from \a src to \a src + \a offset.
185 * The number of pages moved is page_count - MAX(dst, src).
187 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
189 pgcnt_t dst = src + offset;
190 memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
194 /* Fill empty space in array with sentinel */
195 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
196 disk->page_array[page] = PAGE_UNSET_SENTINEL;
203 * Insert \a page at the bottom of page allocation array of \a disk.
205 static void insertFreePage(struct BattFsSuper *disk, pgcnt_t page)
207 pgcnt_t free_pos = disk->page_count - 1;
208 ASSERT(disk->page_array[free_pos] == PAGE_UNSET_SENTINEL);
209 ASSERT(page <= free_pos);
211 disk->page_array[free_pos] = page;
215 * Mark \a page of \a disk as free.
216 * \note free_next of \a disk is used as \a page free marker
217 * and is increased by 1.
219 static bool battfs_markFree(struct BattFsSuper *disk, struct BattFsPageHeader *hdr, pgcnt_t page)
221 uint8_t buf[BATTFS_HEADER_LEN];
223 hdr->mark = disk->free_next;
224 hdr->fcs_free = computeFcsFree(hdr);
225 battfs_to_disk(hdr, buf);
227 if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN))
229 TRACEMSG("error marking page [%d]\n", page);
240 * Determine free_start and free_next blocks for \a disk
241 * using \a minl, \a maxl, \a minh, \a maxh.
243 * Mark_t is a type that has at least 1 bit more than
244 * pgaddr_t. So all free blocks can be numbered using
245 * at most half numbers of a mark_t type.
246 * The free blocks algorithm increments by 1 the disk->free_next
247 * every time a page becomes free. So the free block sequence is
248 * guaranteed to be countiguous.
249 * Only wrap arounds may happen, but due to half size sequence limitation,
250 * there are only 4 possible situations:
253 * |------lower half------|-------upper half-------|
255 * 1) |------minl*****maxl---|------------------------|
256 * 2) |------minl********maxl|minh******maxh----------|
257 * 3) |----------------------|----minh*******maxh-----|
258 * 4) |minl******maxl--------|------------minh****maxh|
261 * Situations 1 and 3 are easy to detect, while 2 and 4 require more care.
263 static void findFreeStartNext(struct BattFsSuper *disk, mark_t minl, mark_t maxl, mark_t minh, mark_t maxh)
265 /* Determine free_start & free_next */
268 /* Valid interval found in lower half */
271 /* Valid interval also found in upper half */
272 if (maxl == minh - 1)
274 /* Interval starts in lower half and ends in upper */
275 disk->free_start = minl;
276 disk->free_next = maxh;
280 /* Interval starts in upper half and ends in lower */
282 ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
284 disk->free_start = minh;
285 disk->free_next = maxl;
291 * Upper interval is invalid.
295 disk->free_start = minl;
296 disk->free_next = maxl;
299 else if (maxh >= minh)
302 * Lower interval is invalid.
305 disk->free_start = minh;
306 disk->free_next = maxh;
311 * No valid interval found.
312 * Hopefully the disk is brand new (or full).
314 TRACEMSG("No valid marked free block found, new disk or disk full\n");
315 disk->free_start = 0;
316 disk->free_next = -1; //to be increased later
319 /* free_next should contain the first usable address */
322 TRACEMSG("Free markers:\n minl %u\n maxl %u\n minh %u\n maxh %u\n free_start %u\n free_next %u\n",
323 minl, maxl, minh, maxh, disk->free_start, disk->free_next);
328 * Count number of pages per file on \a disk.
329 * This information is registered in \a filelen_table.
330 * Array index represent file inode, while value contained
331 * is the number of pages used by that file.
333 * \return true if ok, false on disk read errors.
334 * \note The whole disk is scanned once.
336 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
338 BattFsPageHeader hdr;
339 disk->free_page_start = 0;
341 /* Count the number of disk page per file */
342 for (pgcnt_t page = 0; page < disk->page_count; page++)
344 if (!battfs_readHeader(disk, page, &hdr))
347 /* Increase free space */
348 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
350 /* Check header FCS */
351 if (hdr.fcs == computeFcs(&hdr))
353 ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
355 /* Page is valid and is owned by a file */
356 filelen_table[hdr.inode]++;
358 /* Keep trace of free space */
359 disk->free_bytes -= hdr.fill;
360 disk->free_page_start++;
368 * Fill page allocation array of \a disk
369 * using file lenghts in \a filelen_table.
371 * The page allocation array is an array containings all file infos.
372 * Is ordered by file, and within each file is ordered by page offset
374 * e.g. : at page array[0] you will find page address of the first page
375 * of the first file (if present).
376 * Free blocks are allocated after the last file, starting from invalid ones
377 * and continuing with the marked free ones.
379 * \return true if ok, false on disk read errors.
380 * \note The whole disk is scanned once.
382 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
384 BattFsPageHeader hdr;
385 pgcnt_t curr_free_page = disk->free_page_start;
386 /* Fill page allocation array */
387 for (pgcnt_t page = 0; page < disk->page_count; page++)
389 if (!battfs_readHeader(disk, page, &hdr))
392 /* Check header FCS */
393 if (hdr.fcs == computeFcs(&hdr))
395 /* Compute array position */
396 pgcnt_t array_pos_start = countPages(filelen_table, hdr.inode);
397 pgcnt_t array_pos = array_pos_start + hdr.pgoff;
399 /* Find the first free position */
400 while (disk->page_array[array_pos] != PAGE_UNSET_SENTINEL)
402 ASSERT(array_pos < array_pos_start + filelen_table[hdr.inode + 1]);
406 disk->page_array[array_pos] = page;
410 /* Invalid page, keep as free */
411 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
412 LOG_INFO("Page %d invalid, keeping as free\n", page);
413 disk->page_array[curr_free_page++] = page;
420 * Find the latest version of a page, starting from the
421 * page supplied by \a page_array.
422 * The pages are read from the disk until a different
423 * inode or page offset is found.
424 * The lastest version of the page is moved in the first
425 * position of \a page_array.
426 * \return the number of old versions of the page or PAGE_ERROR
427 * on disk read errors.
429 static pgcnt_t findLastVersion(struct BattFsSuper *disk, pgcnt_t *page_array)
431 pgcnt_t *array_start = page_array;
432 BattFsPageHeader hdr;
433 if (!battfs_readHeader(disk, *page_array++, &hdr))
436 /* Free space: early bailout */
437 if (hdr.fcs != computeFcs(&hdr))
441 * If the first page is valid,
442 * inode and pg_off in the array are taken
443 * as the current page markers.
445 inode_t curr_inode = hdr.inode;
446 pgoff_t curr_pgoff = hdr.pgoff;
448 /* Temps used to find the sequence number range */
449 seq_t minl = HALF_SEQ - 1;
451 seq_t minh = MAX_SEQ;
452 seq_t maxh = MAX_SEQ;
453 pgcnt_t lpos = 0, hpos = 0, dup_cnt = 0;
456 * Find min and max values for the two
457 * half of seq_num range.
458 * With this we can find seqnum wraparounds.
459 * seq_t is a type that has at least 1 bit more than
460 * pgaddr_t. So all version of a page blocks can be numbered using
461 * at most half numbers of a seq_t type.
462 * The sequence number algorithm increments by 1 the previous seq_num
463 * every time a page is rewritten. So the sequence is
464 * guaranteed to be countiguous.
465 * Only wrap arounds may happen, but due to half size sequence limitation,
466 * there are only 4 possible situations:
469 * |------lower half------|-------upper half-------|
471 * 1) |------minl*****maxl---|------------------------|
472 * 2) |------minl********maxl|minh******maxh----------|
473 * 3) |----------------------|----minh*******maxh-----|
474 * 4) |minl******maxl--------|------------minh****maxh|
477 * Situations 1 and 3 are easy to detect, while 2 and 4 require more care.
481 if (hdr.seq < HALF_SEQ)
483 minl = MIN(minl, hdr.seq);
492 minh = MIN(minh, hdr.seq);
500 if (!battfs_readHeader(disk, *page_array++, &hdr))
504 while (curr_inode == hdr.inode && curr_pgoff == hdr.pgoff && hdr.fcs == computeFcs(&hdr));
507 /* Return early if there is only one version of the current page */
511 /* Find the position in the array of the last version of the page */
512 pgcnt_t last_ver = hpos;
515 /* Valid interval found in lower half */
518 /* Valid interval also found in upper half */
519 if (maxl != minh - 1)
521 /* Interval starts in upper half and ends in lower */
523 ASSERT(maxh == MAX_SEQ);
530 * Upper interval is invalid.
536 /* Put last page version at array start position */
537 SWAP(array_start[0], array_start[last_ver]);
543 * Collect old pages, removing empty spaces from \a pg_array, for a maximum len of \a pg_len.
544 * Once the collect task is completed, copy \a old_cnt pages from \a old_pages at the
545 * end of free space in pg_array.
547 void collectOldPages(pgcnt_t *pg_array, pgcnt_t pg_len, pgcnt_t *old_pages, pgcnt_t old_cnt)
552 for (pgcnt_t curr_page = 0; curr_page < pg_len; pg_len++)
556 if (pg_array[curr_page] == PAGE_UNSET_SENTINEL)
560 pg_array[curr_page - gap] = pg_array[curr_page];
566 if (pg_array[curr_page] != PAGE_UNSET_SENTINEL)
567 pg_array[curr_page - gap] = pg_array[curr_page];
575 ASSERT(gap == old_cnt);
576 pg_array += pg_len - old_cnt;
578 memcpy(pg_array, old_pages, old_cnt * sizeof(pgcnt_t));
582 * This function scan the page array of \a disk looking for
583 * old versions of the same page.
585 * Only the last version is kept as valid, the old ones are inserted
586 * in the free blocks heap.
587 * \return true if ok, false on disk read errors.
588 * \note The whole disk is scanned once.
590 static bool dropOldPages(struct BattFsSuper *disk)
592 #define OLD_PAGE_BUFLEN 64
593 pgcnt_t old_pages[OLD_PAGE_BUFLEN];
596 pgcnt_t *curr_page = disk->page_array;
597 pgcnt_t *collect_start = disk->page_array;
598 pgcnt_t collect_len = disk->page_count;
603 dup_pages = findLastVersion(disk, curr_page);
604 if (dup_pages == PAGE_ERROR)
606 /* The first page is the last version */
610 if (old_cnt >= OLD_PAGE_BUFLEN)
612 collectOldPages(collect_start, collect_len, old_pages, old_cnt);
613 collect_len -= old_cnt;
614 disk->free_bytes += old_cnt * (disk->page_size - BATTFS_HEADER_LEN);
615 disk->free_page_start -= old_cnt;
616 curr_page -= old_cnt;
617 collect_start = curr_page;
621 old_pages[old_cnt++] = *curr_page;
622 *curr_page++ = PAGE_UNSET_SENTINEL;
625 while (curr_page < disk->page_array + disk->free_page_start);
627 collectOldPages(collect_start, collect_len, old_pages, old_cnt);
628 disk->free_bytes += old_cnt * (disk->page_size - BATTFS_HEADER_LEN);
629 disk->free_page_start -= old_cnt;
636 * Initialize and mount disk described by
638 * \return false on errors, true otherwise.
640 bool battfs_init(struct BattFsSuper *disk)
642 pgoff_t filelen_table[BATTFS_MAX_FILES];
647 /* Init disk device */
648 if (!disk->open(disk))
650 TRACEMSG("open error\n");
654 /* Disk open must set all of these */
659 ASSERT(disk->page_size);
660 ASSERT(disk->page_count);
661 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
662 ASSERT(disk->page_array);
664 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
666 disk->free_bytes = 0;
667 disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
669 /* Count pages per file */
670 if (!countDiskFilePages(disk, filelen_table))
672 TRACEMSG("error counting file pages\n");
676 /* Once here, we have filelen_table filled with file lengths */
678 /* Fill page array with sentinel */
679 for (pgcnt_t page = 0; page < disk->page_count; page++)
680 disk->page_array[page] = PAGE_UNSET_SENTINEL;
682 /* Fill page allocation array using filelen_table */
683 if (!fillPageArray(disk, filelen_table))
685 TRACEMSG("error filling page array\n");
689 if (!dropOldPages(disk))
691 LOG_ERR("error dropping old pages\n");
695 /* Init list for opened files. */
696 LIST_INIT(&disk->file_opened_list);
702 * \return 0 if ok, EOF on errors.
704 static int battfs_flush(struct KFile *fd)
713 * \return 0 if ok, EOF on errors.
715 static int battfs_fileclose(struct KFile *fd)
717 BattFs *fdb = BATTFS_CAST(fd);
725 * Read from file \a fd \a size bytes in \a buf.
726 * \return The number of bytes read.
728 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
730 BattFs *fdb = BATTFS_CAST(fd);
731 uint8_t *buf = (uint8_t *)_buf;
733 size_t total_read = 0;
735 pgaddr_t addr_offset;
738 size = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
742 pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
743 addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
744 read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
747 if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
749 #warning TODO set error?
753 fd->seek_pos += read_len;
754 total_read += read_len;
762 * Search file \a inode in \a disk using a binary search.
763 * \return pointer to file start in disk->page_array
764 * if file exists, NULL otherwise.
766 static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode)
768 BattFsPageHeader hdr;
769 pgcnt_t first = 0, page, last = disk->page_count -1;
772 while (first <= last)
774 page = (first + last) / 2;
776 if (!battfs_readHeader(disk, disk->page_array[page], &hdr))
779 fcs = computeFcs(&hdr);
780 if (hdr.fcs == fcs && hdr.inode == inode)
781 return (&disk->page_array[page]) - hdr.pgoff;
782 else if (hdr.fcs == fcs && hdr.inode < inode)
792 * \return true if file \a inode exists on \a disk, false otherwise.
794 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
796 return findFile(disk, inode) != NULL;
800 * Count size of file \a inode on \a disk, starting at pointer \a start
801 * in disk->page_array. Size is written in \a size.
802 * \return true if all s ok, false on disk read errors.
804 static bool countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode, file_size_t *size)
807 BattFsPageHeader hdr;
811 if (!battfs_readHeader(disk, *start++, &hdr))
813 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
821 * Open file \a inode from \a disk in \a mode.
822 * File context is stored in \a fd.
823 * \return true if ok, false otherwise.
825 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
829 memset(fd, 0, sizeof(*fd));
831 /* Search file start point in disk page array */
832 fd->start = findFile(disk, inode);
833 if (fd->start == NULL)
835 if (!(mode & BATTFS_CREATE))
838 /* File does not exist, create it */
839 BattFsPageHeader hdr;
844 hdr.fcs = computeFcs(&hdr);
845 #warning TODO: get a free block and write on disk!
849 if (!countFileSize(disk, fd->start, inode, &fd->fd.size))
852 /* Reset seek position */
855 /* Insert file handle in list, ordered by inode, ascending. */
856 FOREACH_NODE(n, &disk->file_opened_list)
858 BattFs *file = containerof(n, BattFs, link);
859 if (file->inode >= inode)
862 INSERT_BEFORE(&fd->link, n);
869 fd->fd.close = battfs_fileclose;
870 fd->fd.flush = battfs_flush;
871 fd->fd.read = battfs_read;
872 fd->fd.reopen = kfile_genericReopen;
873 fd->fd.seek = kfile_genericSeek;
875 #warning TODO battfs_write, battfs_error, battfs_clearerr
877 fd->fd.write = battfs_write;
878 fd->fd.error = battfs_error;
879 fd->fd.clearerr = battfs_clearerr;
882 DB(fd->fd._type = KFT_BATTFS);
890 bool battfs_close(struct BattFsSuper *disk)
895 /* Close all open files */
896 FOREACH_NODE(n, &disk->file_opened_list)
898 BattFs *file = containerof(n, BattFs, link);
899 res += battfs_fileclose(&file->fd);
903 return disk->close(disk) && (res == 0);
906 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
908 BattFsPageHeader hdr;
914 hdr.fcs = computeFcs(&hdr);
916 if (!battfs_writeHeader(disk, page, &hdr))
918 TRACEMSG("error writing hdr\n");