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/)
35 * \author Francesco Sacchi <batt@develer.com>
37 * \brief BattFS: a filesystem for embedded platforms (implementation).
42 #include <cfg/debug.h>
43 #include <cfg/macros.h> /* MIN, MAX */
44 #include <mware/byteorder.h> /* cpu_to_xx */
47 #include <string.h> /* memset, memmove */
51 * Convert from memory representation to disk structure.
52 * \note filesystem is in little-endian format.
54 INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf)
56 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
60 buf[2] = hdr->fill >> 8;
63 buf[4] = hdr->pgoff >> 8;
66 * Mark is at least 1 bit longer than page address.
67 * Needed to take care of wraparonds.
70 buf[6] = hdr->mark >> 8;
73 * First bit used by mark, last 2 bits used by seq.
74 * Since only 2 pages with the same inode and pgoff
75 * can exist at the same time, 2 bit for seq are enough.
77 buf[7] = ((hdr->mark >> 16) & 0x01) | (hdr->seq << 6);
80 * This field must be the before the last one!
82 buf[8] = hdr->fcs_free;
83 buf[9] = hdr->fcs_free >> 8;
86 * This field must be the last one!
87 * This is needed because if the page is only partially
88 * written, we can use this to detect it.
91 buf[11] = hdr->fcs >> 8;
95 * Convert from disk structure to memory representation.
96 * \note filesystem is in little-endian format.
98 INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr)
100 STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
102 hdr->fill = buf[2] << 8 | buf[1];
103 hdr->pgoff = buf[4] << 8 | buf[3];
104 hdr->mark = (mark_t)(buf[7] & 0x01) << 16 | buf[6] << 8 | buf[5];
105 hdr->seq = buf[7] >> 6;
106 hdr->fcs_free = buf[9] << 8 | buf[8];
107 hdr->fcs = buf[11] << 8 | buf[10];
111 * Compute the fcs of the header.
113 static fcs_t computeFcs(struct BattFsPageHeader *hdr)
115 uint8_t buf[BATTFS_HEADER_LEN];
118 battfs_to_disk(hdr, buf);
120 /* fcs is at the end of whole header */
121 rotating_update(buf, BATTFS_HEADER_LEN - sizeof(fcs_t), &cks);
126 * Compute the fcs of the header marked as free.
128 static fcs_t computeFcsFree(struct BattFsPageHeader *hdr)
130 uint8_t buf[BATTFS_HEADER_LEN];
133 battfs_to_disk(hdr, buf);
135 /* fcs_free is just before fcs of whole header */
136 rotating_update(buf, BATTFS_HEADER_LEN - 2 * sizeof(fcs_t), &cks);
142 * Read header of page \a page.
143 * \return true on success, false otherwise.
144 * \note \a hdr is dirtyed even on errors.
146 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
148 uint8_t buf[BATTFS_HEADER_LEN];
150 * Read header from disk.
151 * Header is actually a footer, and so
152 * resides at page end.
154 if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
155 != BATTFS_HEADER_LEN)
157 TRACEMSG("Error: page[%d]\n", page);
162 disk_to_battfs(buf, hdr);
168 * Write header of page \a page.
169 * \return true on success, false otherwise.
170 * \note \a hdr is dirtyed even on errors.
172 static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
174 uint8_t buf[BATTFS_HEADER_LEN];
177 battfs_to_disk(hdr, buf);
180 * write header to disk.
181 * Header is actually a footer, and so
182 * resides at page end.
184 if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
185 != BATTFS_HEADER_LEN)
187 TRACEMSG("Error: page[%d]\n", page);
194 * Count the number of pages from
195 * inode 0 to \a inode in \a filelen_table.
197 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
201 for (inode_t i = 0; i < inode; i++)
202 cnt += filelen_table[i];
208 * Move all pages in page allocation array from \a src to \a src + \a offset.
209 * The number of pages moved is page_count - MAX(dst, src).
211 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
213 pgcnt_t dst = src + offset;
214 memmove(&disk->page_array[dst], &disk->page_array[src], disk->page_count - MAX(dst, src) * sizeof(pgcnt_t));
218 /* Fill empty space in array with sentinel */
219 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
220 disk->page_array[page] = PAGE_UNSET_SENTINEL;
225 * Insert \a page into page allocation array of \a disk,
226 * using \a mark to compute position.
228 static void insertFreePage(struct BattFsSuper *disk, mark_t mark, pgcnt_t page)
230 ASSERT(mark - disk->free_start < disk->free_next - disk->free_start);
232 pgcnt_t free_pos = disk->page_count - disk->free_next + mark;
233 ASSERT(free_pos < disk->page_count);
235 TRACEMSG("mark:%u, page:%u, free_start:%u, free_next:%u, free_pos:%u\n",
236 mark, page, disk->free_start, disk->free_next, free_pos);
238 ASSERT(disk->page_array[free_pos] == PAGE_UNSET_SENTINEL);
239 disk->page_array[free_pos] = page;
243 * Mark \a page of \a disk as free.
244 * \note free_next of \a disk is used as \a page free marker
245 * and is increased by 1.
247 static bool battfs_markFree(struct BattFsSuper *disk, struct BattFsPageHeader *hdr, pgcnt_t page)
249 uint8_t buf[BATTFS_HEADER_LEN];
251 hdr->mark = disk->free_next;
252 hdr->fcs_free = computeFcsFree(hdr);
253 battfs_to_disk(hdr, buf);
255 if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN - 1, buf, BATTFS_HEADER_LEN))
257 TRACEMSG("error marking page [%d]\n", page);
268 * Determine free_start and free_next blocks for \a disk
269 * using \a minl, \a maxl, \a minh, \a maxh.
271 * Mark_t is a type that has at least 1 bit more than
272 * pgaddr_t. So all free blocks can be numbered unsing
273 * at most half numbers of an mark_t type.
274 * The free blocks algorith increments by 1 the disk->free_next
275 * every time a page becomes free. So the free block sequence is
276 * guaranteed to be countiguous.
277 * Only wrap arounds may happen, but due to half size sequence limitation,
278 * there are only 4 possible situations:
281 * |------lower half------|-------upper half-------|
283 * 1) |------minl*****maxl---|------------------------|
284 * 2) |------minl********maxl|minh******maxh----------|
285 * 3) |----------------------|----minh*******maxh-----|
286 * 4) |minl******maxl--------|------------minh****maxh|
289 * Situations 1 and 3 are easy to detect, while 2 and 4 require more care.
291 static void findFreeStartNext(struct BattFsSuper *disk, mark_t minl, mark_t maxl, mark_t minh, mark_t maxh)
293 /* Determine free_start & free_next */
296 /* Valid interval found in lower half */
299 /* Valid interval also found in upper half */
300 if (maxl == minh - 1)
302 /* Interval starts in lower half and end in upper */
303 disk->free_start = minl;
304 disk->free_next = maxh;
308 /* Interval starts in upper half and end in lower */
310 ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
312 disk->free_start = minh;
313 disk->free_next = maxl;
319 * Upper interval is invalid.
323 disk->free_start = minl;
324 disk->free_next = maxl;
327 else if (maxh >= minh)
330 * Lower interval is invalid.
333 disk->free_start = minh;
334 disk->free_next = maxh;
339 * No valid interval found.
340 * Hopefully the disk is brand new.
342 TRACEMSG("No valid marked free block found, new disk or disk full\n");
343 disk->free_start = 0;
344 disk->free_next = -1; //to be incremented ahead
347 /* free_next should contain the first usable address */
350 TRACEMSG("Free markers:\n minl %u\n maxl %u\n minh %u\n maxh %u\n free_start %u\n free_next %u\n",
351 minl, maxl, minh, maxh, disk->free_start, disk->free_next);
355 * Count number of pages per file on \a disk.
356 * This information is registered in \a filelen_table.
357 * Array index represent file inode, while value contained
358 * is the number of pages used by that file.
360 * \return true if ok, false on disk read errors.
361 * \note The whole disk is scanned once.
363 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
365 BattFsPageHeader hdr;
366 mark_t minl, maxl, minh, maxh;
368 /* Initialize min and max counters to keep trace od free blocks */
369 minl = MAX_PAGE_ADDR;
371 minh = MAX_PAGE_ADDR | MARK_HALF_SIZE;
372 maxh = 0 | MARK_HALF_SIZE;
375 /* Count the number of disk page per file */
376 for (pgcnt_t page = 0; page < disk->page_count; page++)
378 if (!battfs_readHeader(disk, page, &hdr))
381 /* Check header FCS */
382 if (hdr.fcs == computeFcs(&hdr))
384 ASSERT(hdr.mark == MARK_PAGE_VALID);
385 ASSERT(hdr.fcs_free == FCS_FREE_VALID);
386 ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
388 /* Page is valid and is owned by a file */
389 filelen_table[hdr.inode]++;
391 /* Keep trace of free space */
392 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN - hdr.fill;
396 /* Increase free space */
397 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
399 /* Check if page is marked free */
400 if (hdr.fcs_free == computeFcsFree(&hdr))
403 * This page is a valid and marked free page.
404 * Update min and max free page markers.
406 if (hdr.mark < MARK_HALF_SIZE)
408 minl = MIN(minl, hdr.mark);
409 maxl = MAX(maxl, hdr.mark);
413 minh = MIN(minh, hdr.mark);
414 maxh = MAX(maxh, hdr.mark);
418 TRACEMSG("page [%d] invalid, keeping as free\n", page);
421 findFreeStartNext(disk, minl, maxl, minh, maxh);
426 * Fill page allocation array of \a disk
427 * using file lenghts in \a filelen_table.
429 * The page allocation array is an array containings all files info.
430 * Is ordered by file, and within each file is ordered by page offset
432 * e.g. : at page array[0] you will find page address of the first page
433 * of the first file (if present).
434 * Free blocks are allocated after the last file starting from invalid ones
435 * and continuing with the marked free ones.
437 * \return true if ok, false on disk read errors.
438 * \note The whole disk is scanned once.
440 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
442 BattFsPageHeader hdr;
443 /* Fill page allocation array */
444 for (pgcnt_t page = 0; page < disk->page_count; page++)
446 if (!battfs_readHeader(disk, page, &hdr))
449 /* Check header FCS */
450 if (hdr.fcs == computeFcs(&hdr))
452 /* Page is valid and is owned by a file */
453 ASSERT(hdr.mark == MARK_PAGE_VALID);
454 ASSERT(hdr.fcs_free == FCS_FREE_VALID);
456 /* Compute array position */
457 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
458 array_pos += hdr.pgoff;
460 /* Check if position is already used by another page of the same file */
461 if (LIKELY(disk->page_array[array_pos] == PAGE_UNSET_SENTINEL))
462 disk->page_array[array_pos] = page;
465 BattFsPageHeader hdr_old;
467 if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_old))
470 /* Check header FCS */
471 ASSERT(hdr_old.fcs == computeFcs(&hdr_old));
473 /* Only the very same page with a different seq number can be here */
474 ASSERT(hdr.inode == hdr_old.inode);
475 ASSERT(hdr.pgoff == hdr_old.pgoff);
476 ASSERT(hdr.mark == hdr_old.mark);
477 ASSERT(hdr.fcs_free == hdr_old.fcs_free);
478 ASSERT(hdr.seq != hdr_old.seq);
480 pgcnt_t new_page, old_page;
483 /* Fancy check to handle seq wraparound (2 bits only) */
484 if (((hdr.seq - hdr_old.seq) & 0x03) < 2)
486 /* Current header is newer than the previuos one */
487 old_page = disk->page_array[array_pos];
489 old_fill = hdr_old.fill;
493 /* Previous header is newer than the current one */
495 new_page = disk->page_array[array_pos];
500 disk->page_array[array_pos] = new_page;
503 disk->free_bytes += old_fill;
505 /* Shift all array one position to the left, overwriting duplicate page */
506 array_pos -= hdr.pgoff;
507 array_pos += filelen_table[hdr.inode];
508 movePages(disk, array_pos, -1);
510 /* Decrease file page count */
511 filelen_table[hdr.inode]--;
513 /* Add old page to free pages pool */
514 if (!battfs_markFree(disk, &hdr, old_page))
517 insertFreePage(disk, hdr.mark, old_page);
522 /* Check if page is free */
523 if (hdr.fcs_free != computeFcsFree(&hdr))
524 /* Page is not a valid marked page, insert at list beginning */
525 hdr.mark = --disk->free_start;
527 insertFreePage(disk, hdr.mark, page);
534 * Initialize and mount disk described by
536 * \return false on errors, true otherwise.
538 bool battfs_init(struct BattFsSuper *disk)
540 pgoff_t filelen_table[BATTFS_MAX_FILES];
545 /* Init disk device */
546 if (!disk->open(disk))
548 TRACEMSG("open error\n");
552 /* Disk open must set all of these */
557 ASSERT(disk->page_size);
558 ASSERT(disk->page_count);
559 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
560 ASSERT(disk->page_array);
562 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
564 disk->free_bytes = 0;
565 disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
567 /* Count pages per file */
568 if (!countDiskFilePages(disk, filelen_table))
570 TRACEMSG("error counting file pages\n");
574 /* Once here, we have filelen_table filled with file lengths */
576 /* Fill page array with sentinel */
577 for (pgcnt_t page = 0; page < disk->page_count; page++)
578 disk->page_array[page] = PAGE_UNSET_SENTINEL;
580 /* Fill page allocation array using filelen_table */
581 if (!fillPageArray(disk, filelen_table))
583 TRACEMSG("error filling page array\n");
591 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff, mark_t mark)
593 BattFsPageHeader hdr;
594 TRACEMSG("page %d, inode %d, pgoff %d\n", page, inode, pgoff);
600 hdr.mark = MARK_PAGE_VALID;
601 hdr.fcs_free = FCS_FREE_VALID;
602 hdr.fcs = computeFcs(&hdr);
603 if (mark != MARK_PAGE_VALID)
606 hdr.fcs_free = computeFcsFree(&hdr);
609 if (!battfs_writeHeader(disk, page, &hdr))
611 TRACEMSG("error writing hdr\n");