Doc fixes.
[bertos.git] / 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
41 #ifndef FS_BATTFS_H
42 #define FS_BATTFS_H
43
44 #include <cfg/compiler.h> // uintXX_t; STATIC_ASSERT
45 #include <cpu/types.h> // CPU_BITS_PER_CHAR
46 #include <algo/rotating_hash.h>
47 #include <mware/list.h>
48 #include <kern/kfile.h>
49
50 typedef uint16_t fill_t;    ///< Type for keeping trace of space filled inside a page
51 typedef fill_t   pgaddr_t;  ///< Type for addressing space inside a page
52 typedef uint16_t pgcnt_t;   ///< Type for counting pages on disk
53 typedef pgcnt_t  pgoff_t;   ///< Type for counting pages inside a file
54 typedef uint32_t mark_t;    ///< Type for marking pages as free
55 typedef uint8_t  inode_t;   ///< Type for file inodes
56 typedef uint8_t  seq_t;     ///< Type for page seq number
57 typedef rotating_t fcs_t;   ///< Type for header FCS.
58
59 /**
60  * Size required for free block allocation is at least 1 bit more
61  * than page addressing.
62  */
63 STATIC_ASSERT(sizeof(mark_t) > sizeof(pgcnt_t));
64
65 /**
66  * BattFS page header, used to represent a page
67  * header in memory.
68  * To see how this is stored on disk:
69  * \see battfs_to_disk
70  * \see disk_to_battfs
71  */
72 typedef struct BattFsPageHeader
73 {
74         inode_t  inode; ///< File inode (file identifier).
75         seq_t    seq;   ///< Page sequence number.
76         fill_t   fill;  ///< Filled bytes in page.
77         pgoff_t  pgoff; ///< Page offset inside file.
78         mark_t   mark;  ///< Marker used to keep trace of free/used pages.
79
80         /**
81          * FCS (Frame Check Sequence) of the page header once the page
82          * as been marked as free.
83          */
84         fcs_t fcs_free;
85
86         /**
87          * FCS (Frame Check Sequence) of the page header.
88          */
89         fcs_t fcs;
90 } BattFsPageHeader;
91
92 /**
93  * Size of the header once saved on disk.
94  * \see battfs_to_disk
95  * \see disk_to_battfs
96  */
97 #define BATTFS_HEADER_LEN 12
98
99 /**
100  * Marks for valid pages.
101  * Simply set to 1 all field bits.
102  * \{
103  */
104 #define MARK_PAGE_VALID ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t) + 1)) - 1)
105 #define FCS_FREE_VALID  ((1 << (CPU_BITS_PER_CHAR * sizeof(fcs_t))) - 1)
106 /* \} */
107
108
109 /**
110  * Half-size of free page marker.
111  * Used to keep trace of free marker wrap-arounds.
112  */
113 #define MARK_HALF_SIZE (1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t) + 1))
114
115 /**
116  * Maximum page address.
117  */
118 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
119
120 /**
121  * Max number of files.
122  */
123 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
124
125 /* Fwd decl */
126 struct BattFsSuper;
127
128 /**
129  * Sentinel used to keep trace of unset pages in disk->page_array.
130  */
131 #define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
132
133 /**
134  * Type interface for disk init function.
135  * \return true if all is ok, false otherwise.
136  */
137 typedef bool (*disk_open_t) (struct BattFsSuper *d);
138
139 /**
140  * Type interface for disk page read function.
141  * \a page is the page address, \a addr the address inside the page,
142  * \a size the lenght to be read.
143  * \return the number of bytes read.
144  */
145 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
146
147 /**
148  * Type interface for disk page write function.
149  * \a page is the page address, \a addr the address inside the page,
150  * \a size the lenght to be written.
151  * \return the number of bytes written.
152  */
153 typedef size_t  (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t);
154
155 /**
156  * Type interface for disk page erase function.
157  * \a page is the page address.
158  * \return true if all is ok, false otherwise.
159  */
160 typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
161
162 /**
163  * Type interface for disk deinit function.
164  * \return true if all is ok, false otherwise.
165  */
166 typedef bool (*disk_close_t) (struct BattFsSuper *d);
167
168
169 typedef uint32_t disk_size_t; ///< Type for disk sizes.
170
171 /**
172  * Context used to describe a disk.
173  * This context structure will be used to access disk.
174  * Must be initialized by hw memory driver.
175  */
176 typedef struct BattFsSuper
177 {
178         disk_open_t open;        ///< Disk init.
179         disk_page_read_t  read;  ///< Page read.
180         disk_page_write_t write; ///< Page write.
181         disk_page_erase_t erase; ///< Page erase.
182         disk_close_t close;      ///< Disk deinit.
183
184         pgaddr_t page_size;      ///< Size of a disk page, in bytes.
185         pgcnt_t page_count;      ///< Number of pages on disk.
186
187         /**
188          * Page allocation array.
189          * This array must be allocated somewhere and
190          * must have enough space for page_count elements.
191          * Is used by the filesystem to represent
192          * the entire disk in memory.
193          */
194         pgcnt_t *page_array;
195
196         /**
197          * Lowest free page counter.
198          * This is the counter of the first availble free page.
199          */
200         mark_t free_start;
201
202         /**
203          * Highest free page counter.
204          * This value is the next to be used to mark a block as free.
205          */
206         mark_t free_next;
207
208         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
209         disk_size_t free_bytes;  ///< Free space on the disk.
210
211         List file_opened_list;       ///< List used to keep trace of open files.
212         /* TODO add other fields. */
213 } BattFsSuper;
214
215 typedef uint8_t filemode_t; ///< Type for file open modes.
216 typedef uint32_t file_size_t; ///< Type for file sizes.
217
218 /**
219  * Modes for battfs_fileopen.
220  * \{
221  */
222 #define BATTFS_CREATE BV(0)  ///< Create file if does not exist
223 #define BATTFS_RD     BV(1)  ///< Open file for reading
224 #define BATTFS_WR     BV(2)  ///< Open file fir writing
225 /*/}*/
226
227
228 /**
229  * Describe a BattFs file usign a KFile.
230  */
231 typedef struct KFileBattFs
232 {
233         KFile fd;           ///< KFile context
234         Node link;          ///< Link for inserting in opened file list
235         inode_t inode;      ///< inode of the opened file
236         BattFsSuper *disk;  ///< Disk context
237         filemode_t mode;    ///< File open mode
238         pgcnt_t *start;     ///< Pointer to page_array file start position.
239 } KFileBattFs;
240
241 /**
242  * Id for battfs file descriptors.
243  */
244 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
245
246 /**
247  * Macro used to cast a KFile to a KFileBattFs.
248  * Also perform dynamic type check.
249  */
250 INLINE KFileBattFs * KFILEBATTFS(KFile *fd)
251 {
252         ASSERT(fd->_type == KFT_BATTFS);
253         return (KFileBattFs *)fd;
254 }
255
256 bool battfs_init(struct BattFsSuper *d);
257 bool battfs_close(struct BattFsSuper *disk);
258
259 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
260 bool battfs_fileopen(BattFsSuper *disk, KFileBattFs *fd, inode_t inode, filemode_t mode);
261
262 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff, mark_t mark);
263
264 #endif /* FS_BATTFS_H */