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 - 1, buf, BATTFS_HEADER_LEN)
155 != BATTFS_HEADER_LEN)
157 TRACEMSG("Error: page[%d]\n", page);
162 disk_to_battfs(buf, hdr);
168 * Count the number of pages from
169 * inode 0 to \a inode in \a filelen_table.
171 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
175 for (inode_t i = 0; i < inode; i++)
176 cnt += filelen_table[i];
182 * Move all pages in page allocation array from \a src to \a src + \a offset.
183 * The number of pages moved is page_count - MAX(dst, src).
185 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
187 pgcnt_t dst = src + offset;
188 memmove(&disk->page_array[dst], &disk->page_array[src], disk->page_count - MAX(dst, src) * sizeof(pgcnt_t));
192 /* Fill empty space in array with sentinel */
193 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
194 disk->page_array[page] = PAGE_UNSET_SENTINEL;
199 * Insert \a page into page allocation array of \a disk, using \a filelen_table and
200 * \a free_number to compute position.
202 static void insertFreePage(struct BattFsSuper *disk, pgoff_t *filelen_table, mark_t mark, pgcnt_t page)
204 ASSERT(mark >= disk->free_start);
205 ASSERT(mark < disk->free_next);
207 pgcnt_t free_pos = countPages(filelen_table, BATTFS_MAX_FILES - 1);
208 free_pos += mark - disk->free_start;
209 TRACEMSG("mark:%d, page:%d, free_start:%d, free_next:%d, free_pos:%d\n",
210 mark, page, disk->free_start, disk->free_next, free_pos);
212 ASSERT(disk->page_array[free_pos] == PAGE_UNSET_SENTINEL);
213 disk->page_array[free_pos] = page;
217 * Mark \a page of \a disk as free.
218 * \note free_next of \a disk is used as \a page free marker
219 * and is increased by 1.
221 static bool battfs_markFree(struct BattFsSuper *disk, struct BattFsPageHeader *hdr, pgcnt_t page)
223 uint8_t buf[BATTFS_HEADER_LEN];
225 hdr->mark = disk->free_next;
226 hdr->fcs_free = computeFcsFree(hdr);
227 battfs_to_disk(hdr, buf);
229 if (!disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN - 1, buf, BATTFS_HEADER_LEN))
231 TRACEMSG("error marking page [%d]\n", page);
243 * Initialize and mount disk described by
245 * \return false on errors, true otherwise.
247 bool battfs_init(struct BattFsSuper *disk)
249 BattFsPageHeader hdr;
250 pgoff_t filelen_table[BATTFS_MAX_FILES];
251 mark_t minl, maxl, minh, maxh;
256 /* Init disk device */
257 if (!disk->open(disk))
259 TRACEMSG("open error\n");
263 /* Disk open must set all of these */
268 ASSERT(disk->page_size);
269 ASSERT(disk->page_count);
270 ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
271 ASSERT(disk->page_array);
273 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
275 /* Initialize min and max counters to keep trace od free blocks */
276 minl = MAX_PAGE_ADDR;
278 minh = MAX_PAGE_ADDR | MARK_HALF_SIZE;
279 maxh = 0 | MARK_HALF_SIZE;
281 disk->free_bytes = 0;
282 disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
284 /* Count the number of disk page per file */
285 for (pgcnt_t page = 0; page < disk->page_count; page++)
287 if (!battfs_readHeader(disk, page, &hdr))
290 /* Check header FCS */
291 if (hdr.fcs == computeFcs(&hdr))
293 ASSERT(hdr.mark == MARK_PAGE_VALID);
294 ASSERT(hdr.fcs_free == FCS_FREE_VALID);
295 ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
297 /* Page is valid and is owned by a file */
298 filelen_table[hdr.inode]++;
300 /* Keep trace of free space */
301 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN - hdr.fill;
305 /* Increase free space */
306 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
308 /* Check if page is marked free */
309 if (hdr.fcs_free == computeFcsFree(&hdr))
312 * This page is a valid and marked free page.
313 * Update min and max free page markers.
315 if (hdr.mark < MARK_HALF_SIZE)
317 minl = MIN(minl, hdr.mark);
318 maxl = MAX(maxl, hdr.mark);
322 minh = MIN(minh, hdr.mark);
323 maxh = MAX(maxh, hdr.mark);
327 TRACEMSG("page [%d] invalid, keeping as free\n", page);
331 /* Once here, we have filelen_table filled with file lengths */
333 /* Fill page array with sentinel */
334 for (pgcnt_t page = 0; page < disk->page_count; page++)
335 disk->page_array[page] = PAGE_UNSET_SENTINEL;
337 /* Determine free_start & free_next */
342 if (maxl == minh - 1)
344 disk->free_start = minl;
345 disk->free_next = maxh;
350 ASSERT(maxh == (MAX_PAGE_ADDR | MARK_HALF_SIZE));
352 disk->free_start = minh;
353 disk->free_next = maxl;
358 disk->free_start = minl;
359 disk->free_next = maxl;
362 else if (maxh >= minh)
364 disk->free_start = minh;
365 disk->free_next = maxh;
369 TRACEMSG("No valid marked free block found\n");
370 disk->free_start = 0;
371 disk->free_next = -1; //to be incremented ahead
374 /* free_next should contain the first usable address */
377 TRACEMSG("Free markers:\n minl %u\n maxl %u\n minh %u\n maxh %u\n free_start %u\n free_next %u\n",
378 minl, maxl, minh, maxh, disk->free_start, disk->free_next);
381 /* Fill page allocation array */
382 for (pgcnt_t page = 0; page < disk->page_count; page++)
384 if (!battfs_readHeader(disk, page, &hdr))
387 /* Check header FCS */
388 if (hdr.fcs == computeFcs(&hdr))
390 /* Page is valid and is owned by a file */
391 ASSERT(hdr.mark == MARK_PAGE_VALID);
392 ASSERT(hdr.fcs_free == FCS_FREE_VALID);
394 /* Compute array position */
395 pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
396 array_pos += hdr.pgoff;
398 /* Check if position is already used by another page of the same file */
399 if (LIKELY(disk->page_array[array_pos] == PAGE_UNSET_SENTINEL))
400 disk->page_array[array_pos] = page;
403 BattFsPageHeader hdr_old;
405 if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_old))
408 /* Check header FCS */
409 ASSERT(hdr_old.fcs == computeFcs(&hdr_old));
411 /* Only the very same page with a different seq number can be here */
412 ASSERT(hdr.inode == hdr_old.inode);
413 ASSERT(hdr.pgoff == hdr_old.pgoff);
414 ASSERT(hdr.mark == hdr_old.mark);
415 ASSERT(hdr.fcs_free == hdr_old.fcs_free);
416 ASSERT(hdr.seq != hdr_old.seq);
418 pgcnt_t new_page, old_page;
421 /* Fancy check to handle seq wraparound */
422 #define HALF_SEQ (1 << ((sizeof(seq_t) * CPU_BITS_PER_CHAR) - 1))
423 if ((hdr.seq - hdr_old.seq) < HALF_SEQ)
425 /* Current header is newer than the previuos one */
426 old_page = disk->page_array[array_pos];
428 old_fill = hdr_old.fill;
432 /* Previous header is newer than the current one */
434 new_page = disk->page_array[array_pos];
439 disk->page_array[array_pos] = new_page;
442 disk->free_bytes += old_fill;
444 /* Shift all array one position to the left, overwriting duplicate page */
445 array_pos -= hdr.pgoff;
446 array_pos += filelen_table[hdr.inode];
447 movePages(disk, array_pos, -1);
449 /* Decrease file page count */
450 filelen_table[hdr.inode]--;
452 /* Add old page to free pages pool */
453 if (!battfs_markFree(disk, &hdr, old_page))
456 insertFreePage(disk, filelen_table, hdr.mark, old_page);
461 /* Check if page is free */
462 if (hdr.fcs_free != computeFcsFree(&hdr))
463 /* Page is not a valid marked page, insert at the end of list */
464 hdr.mark = disk->free_next++;
466 insertFreePage(disk, filelen_table, hdr.mark, page);