Update BattFS in order to use the new kblock interface.
[bertos.git] / bertos / fs / battfs.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief BattFS: a filesystem for embedded platforms (interface).
38  * TODO: Add detailed filesystem description.
39  *
40  * $WIZ$ module_name = "battfs"
41  * $WIZ$ module_depends = "rotating_hash", "kfile"
42  * $WIZ$ module_configuration = "bertos/cfg/cfg_battfs.h"
43  */
44
45 #ifndef FS_BATTFS_H
46 #define FS_BATTFS_H
47
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>
53 #include <io/kblock.h>
54
55 typedef uint16_t fill_t;    ///< Type for keeping trace of space filled inside a page
56 typedef fill_t   pgaddr_t;  ///< Type for addressing space inside a page
57 typedef uint16_t pgcnt_t;   ///< Type for counting pages on disk
58 typedef pgcnt_t  pgoff_t;   ///< Type for counting pages inside a file
59 typedef uint8_t  inode_t;   ///< Type for file inodes
60 typedef uint64_t  seq_t;    ///< Type for page seq number, at least 40bits wide.
61 typedef rotating_t fcs_t;   ///< Type for header FCS.
62
63
64 /**
65  * BattFS page header, used to represent a page
66  * header in memory.
67  * To see how this is stored on disk:
68  * \see battfs_to_disk
69  * \see disk_to_battfs
70  */
71 typedef struct BattFsPageHeader
72 {
73         inode_t  inode; ///< File inode (file identifier).
74         fill_t   fill;  ///< Filled bytes in page.
75         pgoff_t  pgoff; ///< Page offset inside file.
76
77         /**
78          * Page sequence number.
79          * Every time a page is rewritten the seq number is
80          * increased by 1. seq_t is wide enough to not to perform
81          * a wrap around before the memory death.
82          * So it can be kept as it would be
83          * monotonically increasing for our needs.
84          */
85         seq_t    seq;
86
87         /**
88          * FCS (Frame Check Sequence) of the page header.
89          */
90         fcs_t fcs;
91 } BattFsPageHeader;
92
93 /**
94  * Size of the header once saved on disk.
95  * \see battfs_to_disk
96  * \see disk_to_battfs
97  */
98 #define BATTFS_HEADER_LEN 12
99
100 /**
101  * Maximum page address.
102  */
103 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
104
105 /**
106  * Max number of files.
107  */
108 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
109
110 /* Fwd decl */
111 struct BattFsSuper;
112
113 /**
114  * Sentinel used to keep trace of unset pages in disk->page_array.
115  */
116 #define PAGE_UNSET_SENTINEL ((pgcnt_t)((1L << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1))
117
118 typedef uint32_t disk_size_t; ///< Type for disk sizes.
119
120 /**
121  * Context used to describe a disk.
122  * This context structure will be used to access disk.
123  * Must be initialized by hw memory driver.
124  */
125 typedef struct BattFsSuper
126 {
127         KBlock *dev;             ///< Block device context (physical disk).
128
129         pgaddr_t page_size;      ///< Size of a memory page, in bytes. Used by disk low level driver.
130         pgcnt_t page_count;      ///< Number of pages on disk.
131
132         pgaddr_t data_size;      ///< Size of space usable for data in a disk page, in bytes. The rest is used by the page header.
133         /**
134          * Page allocation array.
135          * This array must be allocated somewhere and
136          * must have enough space for page_count elements.
137          * Is used by the filesystem to represent
138          * the entire disk in memory.
139          */
140         pgcnt_t *page_array;
141
142         /**
143          * Lowest address, in page array, for free pages.
144          * Pages above this element are free for use.
145          */
146         pgcnt_t free_page_start;
147
148         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
149         disk_size_t free_bytes;  ///< Free space on the disk.
150
151         List file_opened_list;       ///< List used to keep trace of open files.
152         /* TODO add other fields. */
153 } BattFsSuper;
154
155 /**
156  * True if space on \a disk is over.
157  */
158 #define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->page_count)
159
160 typedef uint8_t filemode_t;  ///< Type for file open modes.
161 typedef int32_t file_size_t; ///< Type for file sizes.
162
163 /**
164  * Modes for battfs_fileopen.
165  * \{
166  */
167 #define BATTFS_CREATE BV(0)  ///< Create file if does not exist
168 #define BATTFS_RD     BV(1)  ///< Open file for reading
169 #define BATTFS_WR     BV(2)  ///< Open file fir writing
170 /*/}*/
171
172
173 /**
174  * File errors.
175  * \{
176  */
177 #define BATTFS_NEGATIVE_SEEK_ERR   BV(0) ///< Trying to read/write before file start.
178 #define BATTFS_DISK_READ_ERR       BV(1) ///< Error reading from disk device.
179 #define BATTFS_DISK_WRITE_ERR      BV(2) ///< Error writing in the disk device.
180 #define BATTFS_DISK_SPACEOVER_ERR  BV(3) ///< No more disk space available.
181 #define BATTFS_DISK_FLUSHBUF_ERR   BV(4) ///< Error flushing (writing) the current page to disk.
182 #define BATTFS_FILE_NOT_FOUND_ERR  BV(5) ///< File not found on disk.
183 /*/}*/
184
185 /**
186  * Describe a BattFs file usign a KFile.
187  */
188 typedef struct BattFs
189 {
190         KFile fd;           ///< KFile context
191         Node link;          ///< Link for inserting in opened file list
192         inode_t inode;      ///< inode of the opened file
193         BattFsSuper *disk;  ///< Disk context
194         filemode_t mode;    ///< File open mode
195         pgcnt_t *start;     ///< Pointer to page_array file start position.
196         pgcnt_t max_off;    ///< Max page offset allocated for the file.
197         int errors;         ///< File status/errors
198 } BattFs;
199
200 /**
201  * Id for battfs file descriptors.
202  */
203 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
204
205 /**
206  * Macro used to cast a KFile to a BattFS.
207  * Also perform dynamic type check.
208  */
209 INLINE BattFs * BATTFS_CAST(KFile *fd)
210 {
211         ASSERT(fd->_type == KFT_BATTFS);
212         return (BattFs *)fd;
213 }
214
215 bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size);
216 bool battfs_fsck(struct BattFsSuper *disk);
217 bool battfs_umount(struct BattFsSuper *disk);
218
219 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
220 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
221
222 void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
223 void battfs_eraseBlock(KBlock *dev, pgcnt_t page);
224 #endif /* FS_BATTFS_H */