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 (interface).
38 * TODO: Add detailed filesystem description.
40 * $WIZ$ module_name = "battfs"
41 * $WIZ$ module_depends = "rotating_hash", "kfile"
42 * $WIZ$ module_configuration = "bertos/cfg/cfg_battfs.h"
48 #include <cfg/compiler.h> // uintXX_t; STATIC_ASSERT
49 #include <cpu/types.h> // CPU_BITS_PER_CHAR
50 #include <algo/rotating_hash.h>
51 #include <struct/list.h>
52 #include <kern/kfile.h>
54 typedef uint16_t fill_t; ///< Type for keeping trace of space filled inside a page
55 typedef fill_t pgaddr_t; ///< Type for addressing space inside a page
56 typedef uint16_t pgcnt_t; ///< Type for counting pages on disk
57 typedef pgcnt_t pgoff_t; ///< Type for counting pages inside a file
58 typedef uint8_t inode_t; ///< Type for file inodes
59 typedef uint64_t seq_t; ///< Type for page seq number, at least 40bits wide.
60 typedef rotating_t fcs_t; ///< Type for header FCS.
64 * BattFS page header, used to represent a page
66 * To see how this is stored on disk:
70 typedef struct BattFsPageHeader
72 inode_t inode; ///< File inode (file identifier).
73 fill_t fill; ///< Filled bytes in page.
74 pgoff_t pgoff; ///< Page offset inside file.
77 * Page sequence number.
78 * Every time a page is rewritten the seq number is
79 * increased by 1. seq_t is wide enough to not to perform
80 * a wrap around before the memory death.
81 * So it can be kept as it would be
82 * monotonically increasing for our needs.
87 * FCS (Frame Check Sequence) of the page header.
93 * Size of the header once saved on disk.
97 #define BATTFS_HEADER_LEN 12
100 * Maximum page address.
102 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
105 * Max number of files.
107 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
113 * Sentinel used to keep trace of unset pages in disk->page_array.
115 #define PAGE_UNSET_SENTINEL ((pgcnt_t)((1L << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1))
118 * Type interface for disk page read function.
119 * \a page is the page address, \a addr the address inside the page,
120 * \a size the lenght to be read.
121 * \return the number of bytes read.
123 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
127 * Type interface for disk page load function.
128 * The disk should supply a buffer used for loading/saving pages.
129 * This has to be done by the disk driver because it knows memory details
130 * (e.g. some memories can have the buffer inside the memory itself).
131 * \a page is the page to be loaded from the disk in the buffer.
132 * \return true if ok, false on errors.
134 typedef bool (*disk_page_load_t) (struct BattFsSuper *d, pgcnt_t page);
137 * Type interface for disk pagebuffer write function.
138 * \a addr is the address inside the current loaded page,
139 * \a size the lenght to be written.
140 * \return the number of bytes written.
142 typedef size_t (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t);
145 * Type interface for disk pagebuffer read function.
146 * \a addr is the address inside the current loaded page,
147 * \a size the lenght to be read.
148 * \return the number of bytes read.
150 typedef size_t (*disk_buffer_read_t) (struct BattFsSuper *d, pgaddr_t addr, void *buf, size_t);
153 * Type interface for disk page save function.
154 * The disk should supply a buffer used for loading/saving pages.
155 * For details \see disk_page_load_t.
156 * \a page is the page where the buffer will be written.
157 * \return true if ok, false on errors.
159 typedef bool (*disk_page_save_t) (struct BattFsSuper *d, pgcnt_t page);
162 * Type interface for disk page erase function.
163 * \a page is the page address.
164 * \return true if all is ok, false otherwise.
166 typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
169 * Type interface for disk deinit function.
170 * \return true if all is ok, false otherwise.
172 typedef bool (*disk_close_t) (struct BattFsSuper *d);
175 typedef uint32_t disk_size_t; ///< Type for disk sizes.
178 * Context used to describe a disk.
179 * This context structure will be used to access disk.
180 * Must be initialized by hw memory driver.
182 typedef struct BattFsSuper
184 void *disk_ctx; ///< Disk context used by disk access functions.
185 disk_page_read_t read; ///< Page read.
186 disk_page_load_t load; ///< Page load.
187 disk_buffer_write_t bufferWrite; ///< Buffer write.
188 disk_buffer_read_t bufferRead; ///< Buffer read.
189 disk_page_save_t save; ///< Page save.
190 disk_page_erase_t erase; ///< Page erase.
191 disk_close_t close; ///< Disk deinit.
193 pgaddr_t page_size; ///< Size of a memory page, in bytes. Used by disk low level driver.
194 pgaddr_t data_size; ///< Size of space usable for data in a disk page, in bytes. The rest is used by the page header.
195 pgcnt_t page_count; ///< Number of pages on disk.
198 * Page allocation array.
199 * This array must be allocated somewhere and
200 * must have enough space for page_count elements.
201 * Is used by the filesystem to represent
202 * the entire disk in memory.
205 pgcnt_t curr_page; ///< Current page loaded in disk buffer.
206 bool cache_dirty; ///< True if current cache is dirty (nneds to be flushed).
209 * Lowest address, in page array, for free pages.
210 * Pages above this element are free for use.
212 pgcnt_t free_page_start;
214 disk_size_t disk_size; ///< Size of the disk, in bytes (page_count * page_size).
215 disk_size_t free_bytes; ///< Free space on the disk.
217 List file_opened_list; ///< List used to keep trace of open files.
218 /* TODO add other fields. */
222 * True if space on \a disk is over.
224 #define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->page_count)
226 typedef uint8_t filemode_t; ///< Type for file open modes.
227 typedef int32_t file_size_t; ///< Type for file sizes.
230 * Modes for battfs_fileopen.
233 #define BATTFS_CREATE BV(0) ///< Create file if does not exist
234 #define BATTFS_RD BV(1) ///< Open file for reading
235 #define BATTFS_WR BV(2) ///< Open file fir writing
243 #define BATTFS_NEGATIVE_SEEK_ERR BV(0) ///< Trying to read/write before file start.
244 #define BATTFS_DISK_READ_ERR BV(1) ///< Error reading from disk driver.
245 #define BATTFS_DISK_LOADPAGE_ERR BV(2) ///< Error loading a disk page in the buffer.
246 #define BATTFS_DISK_BUFFERWR_ERR BV(3) ///< Error writing in the disk page buffer.
247 #define BATTFS_DISK_GETNEWPAGE_ERR BV(4) ///< Error getting a free page.
248 #define BATTFS_DISK_BUFFERRD_ERR BV(6) ///< Error reading from the disk page buffer.
249 #define BATTFS_DISK_SPACEOVER_ERR BV(7) ///< No more disk space available.
250 #define BATTFS_DISK_FLUSHBUF_ERR BV(8) ///< Error flushing (writing) the current page to disk.
251 #define BATTFS_FILE_NOT_FOUND_ERR BV(9) ///< File not found on disk.
255 * Describe a BattFs file usign a KFile.
257 typedef struct BattFs
259 KFile fd; ///< KFile context
260 Node link; ///< Link for inserting in opened file list
261 inode_t inode; ///< inode of the opened file
262 BattFsSuper *disk; ///< Disk context
263 filemode_t mode; ///< File open mode
264 pgcnt_t *start; ///< Pointer to page_array file start position.
265 pgcnt_t max_off; ///< Max page offset allocated for the file.
266 int errors; ///< File status/errors
270 * Id for battfs file descriptors.
272 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
275 * Macro used to cast a KFile to a BattFS.
276 * Also perform dynamic type check.
278 INLINE BattFs * BATTFS_CAST(KFile *fd)
280 ASSERT(fd->_type == KFT_BATTFS);
284 bool battfs_mount(struct BattFsSuper *d);
285 bool battfs_fsck(struct BattFsSuper *disk);
286 bool battfs_umount(struct BattFsSuper *disk);
288 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
289 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
290 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
291 #endif /* FS_BATTFS_H */