Switch to new disk write interface:the disk should now supply the write buffer.
[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
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 <struct/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 uint8_t  inode_t;   ///< Type for file inodes
55 typedef uint64_t  seq_t;    ///< Type for page seq number, at least 40bits wide.
56 typedef rotating_t fcs_t;   ///< Type for header FCS.
57
58
59 /**
60  * BattFS page header, used to represent a page
61  * header in memory.
62  * To see how this is stored on disk:
63  * \see battfs_to_disk
64  * \see disk_to_battfs
65  */
66 typedef struct BattFsPageHeader
67 {
68         inode_t  inode; ///< File inode (file identifier).
69         fill_t   fill;  ///< Filled bytes in page.
70         pgoff_t  pgoff; ///< Page offset inside file.
71
72         /**
73          * Page sequence number.
74          * Every time a page is rewritten the seq number is
75          * increased by 1. seq_t is wide enough to not to perform
76          * a wrap around before the memory death.
77          * So it can be kept as it would be
78          * monotonically increasing for our needs.
79          */
80         seq_t    seq;
81
82         /**
83          * FCS (Frame Check Sequence) of the page header.
84          */
85         fcs_t fcs;
86 } BattFsPageHeader;
87
88 /**
89  * Size of the header once saved on disk.
90  * \see battfs_to_disk
91  * \see disk_to_battfs
92  */
93 #define BATTFS_HEADER_LEN 12
94
95 /**
96  * Maximum page address.
97  */
98 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
99
100 /**
101  * Max number of files.
102  */
103 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
104
105 /* Fwd decl */
106 struct BattFsSuper;
107
108 /**
109  * Sentinel used to keep trace of unset pages in disk->page_array.
110  */
111 #define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
112
113 /**
114  * Type interface for disk init function.
115  * \return true if all is ok, false otherwise.
116  */
117 typedef bool (*disk_open_t) (struct BattFsSuper *d);
118
119 /**
120  * Type interface for disk page read function.
121  * \a page is the page address, \a addr the address inside the page,
122  * \a size the lenght to be read.
123  * \return the number of bytes read.
124  */
125 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
126
127
128 /**
129  * Type interface for disk page load function.
130  * The disk should supply a buffer used for loading/saving pages.
131  * This has to be done by the disk driver because it knows memory details
132  * (e.g. some memories can have the buffer inside the memory itself).
133  * \a page is the page to be loaded from the disk in the buffer.
134  * \return true if ok, false on errors.
135  */
136 typedef bool (*disk_page_load_t) (struct BattFsSuper *d, pgcnt_t page);
137
138 /**
139  * Type interface for disk pagebuffer write function.
140  * \a addr is the address inside the current loaded page,
141  * \a size the lenght to be written.
142  * \return the number of bytes written.
143  */
144 typedef size_t  (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t);
145
146 /**
147  * Type interface for disk page save function.
148  * The disk should supply a buffer used for loading/saving pages.
149  * For details \see disk_page_load_t.
150  * \a page is the page where the buffer will be written.
151  * \return true if ok, false on errors.
152  */
153 typedef bool (*disk_page_save_t) (struct BattFsSuper *d, pgcnt_t page);
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_load_t  load;  ///< Page load.
181         disk_buffer_write_t bufferWrite; ///< Buffer write.
182         disk_page_save_t  save;  ///< Page save.
183         disk_page_erase_t erase; ///< Page erase.
184         disk_close_t close;      ///< Disk deinit.
185
186         pgaddr_t page_size;      ///< Size of a disk page, in bytes.
187         pgcnt_t page_count;      ///< Number of pages on disk.
188
189         /**
190          * Page allocation array.
191          * This array must be allocated somewhere and
192          * must have enough space for page_count elements.
193          * Is used by the filesystem to represent
194          * the entire disk in memory.
195          */
196         pgcnt_t *page_array;
197
198         /**
199          * Lowest address, in page array, for free pages.
200          * Pages above this element are free for use.
201          */
202         pgcnt_t free_page_start;
203
204         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
205         disk_size_t free_bytes;  ///< Free space on the disk.
206
207         List file_opened_list;       ///< List used to keep trace of open files.
208         /* TODO add other fields. */
209 } BattFsSuper;
210
211 typedef uint8_t filemode_t;  ///< Type for file open modes.
212 typedef int32_t file_size_t; ///< Type for file sizes.
213
214 /**
215  * Modes for battfs_fileopen.
216  * \{
217  */
218 #define BATTFS_CREATE BV(0)  ///< Create file if does not exist
219 #define BATTFS_RD     BV(1)  ///< Open file for reading
220 #define BATTFS_WR     BV(2)  ///< Open file fir writing
221 /*/}*/
222
223
224 /**
225  * Describe a BattFs file usign a KFile.
226  */
227 typedef struct BattFs
228 {
229         KFile fd;           ///< KFile context
230         Node link;          ///< Link for inserting in opened file list
231         inode_t inode;      ///< inode of the opened file
232         BattFsSuper *disk;  ///< Disk context
233         filemode_t mode;    ///< File open mode
234         pgcnt_t *start;     ///< Pointer to page_array file start position.
235 } BattFs;
236
237 /**
238  * Id for battfs file descriptors.
239  */
240 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
241
242 /**
243  * Macro used to cast a KFile to a BattFS.
244  * Also perform dynamic type check.
245  */
246 INLINE BattFs * BATTFS_CAST(KFile *fd)
247 {
248         ASSERT(fd->_type == KFT_BATTFS);
249         return (BattFs *)fd;
250 }
251
252 bool battfs_init(struct BattFsSuper *d);
253 bool battfs_close(struct BattFsSuper *disk);
254
255 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
256 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
257 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
258 #endif /* FS_BATTFS_H */