Update to new names and API.
[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 uint32_t  seq_t;     ///< Type for page seq number
56 typedef rotating_t fcs_t;   ///< Type for header FCS.
57
58 /**
59  * Size required for free block allocation is at least 1 bit more
60  * than page addressing.
61  */
62 STATIC_ASSERT(sizeof(seq_t) > sizeof(pgcnt_t));
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         seq_t    seq;   ///< Page sequence number.
77
78         /**
79          * FCS (Frame Check Sequence) of the page header.
80          */
81         fcs_t fcs;
82 } BattFsPageHeader;
83
84 /**
85  * Size of the header once saved on disk.
86  * \see battfs_to_disk
87  * \see disk_to_battfs
88  */
89 #define BATTFS_HEADER_LEN 10
90
91 /**
92  * Half-size of page sequence numer.
93  */
94 #define HALF_SEQ (1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t)))
95
96 /**
97  * Max sequence number.
98  */
99 #define MAX_SEQ ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t) + 1)) - 1)
100
101
102 /**
103  * Maximum page address.
104  */
105 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
106
107 /**
108  * Max number of files.
109  */
110 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
111
112 /* Fwd decl */
113 struct BattFsSuper;
114
115 /**
116  * Sentinel used to keep trace of unset pages in disk->page_array.
117  */
118 #define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
119
120 /** Also used as an error marker sometimes */
121 #define PAGE_ERROR PAGE_UNSET_SENTINEL
122
123 /**
124  * Type interface for disk init function.
125  * \return true if all is ok, false otherwise.
126  */
127 typedef bool (*disk_open_t) (struct BattFsSuper *d);
128
129 /**
130  * Type interface for disk page read function.
131  * \a page is the page address, \a addr the address inside the page,
132  * \a size the lenght to be read.
133  * \return the number of bytes read.
134  */
135 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
136
137 /**
138  * Type interface for disk page write function.
139  * \a page is the page address, \a addr the address inside the page,
140  * \a size the lenght to be written.
141  * \return the number of bytes written.
142  */
143 typedef size_t  (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t);
144
145 /**
146  * Type interface for disk page erase function.
147  * \a page is the page address.
148  * \return true if all is ok, false otherwise.
149  */
150 typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
151
152 /**
153  * Type interface for disk deinit function.
154  * \return true if all is ok, false otherwise.
155  */
156 typedef bool (*disk_close_t) (struct BattFsSuper *d);
157
158
159 typedef uint32_t disk_size_t; ///< Type for disk sizes.
160
161 /**
162  * Context used to describe a disk.
163  * This context structure will be used to access disk.
164  * Must be initialized by hw memory driver.
165  */
166 typedef struct BattFsSuper
167 {
168         disk_open_t open;        ///< Disk init.
169         disk_page_read_t  read;  ///< Page read.
170         disk_page_write_t write; ///< Page write.
171         disk_page_erase_t erase; ///< Page erase.
172         disk_close_t close;      ///< Disk deinit.
173
174         pgaddr_t page_size;      ///< Size of a disk page, in bytes.
175         pgcnt_t page_count;      ///< Number of pages on disk.
176
177         /**
178          * Page allocation array.
179          * This array must be allocated somewhere and
180          * must have enough space for page_count elements.
181          * Is used by the filesystem to represent
182          * the entire disk in memory.
183          */
184         pgcnt_t *page_array;
185
186         /**
187          * Lowest address, in page array, for free pages.
188          * Pages above this element are free for use.
189          */
190         pgcnt_t free_page_start;
191
192         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
193         disk_size_t free_bytes;  ///< Free space on the disk.
194
195         List file_opened_list;       ///< List used to keep trace of open files.
196         /* TODO add other fields. */
197 } BattFsSuper;
198
199 typedef uint8_t filemode_t; ///< Type for file open modes.
200 typedef int32_t file_size_t; ///< Type for file sizes.
201
202 /**
203  * Modes for battfs_fileopen.
204  * \{
205  */
206 #define BATTFS_CREATE BV(0)  ///< Create file if does not exist
207 #define BATTFS_RD     BV(1)  ///< Open file for reading
208 #define BATTFS_WR     BV(2)  ///< Open file fir writing
209 /*/}*/
210
211
212 /**
213  * Describe a BattFs file usign a KFile.
214  */
215 typedef struct BattFs
216 {
217         KFile fd;           ///< KFile context
218         Node link;          ///< Link for inserting in opened file list
219         inode_t inode;      ///< inode of the opened file
220         BattFsSuper *disk;  ///< Disk context
221         filemode_t mode;    ///< File open mode
222         pgcnt_t *start;     ///< Pointer to page_array file start position.
223 } BattFs;
224
225 /**
226  * Id for battfs file descriptors.
227  */
228 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
229
230 /**
231  * Macro used to cast a KFile to a BattFS.
232  * Also perform dynamic type check.
233  */
234 INLINE BattFs * BATTFS_CAST(KFile *fd)
235 {
236         ASSERT(fd->_type == KFT_BATTFS);
237         return (BattFs *)fd;
238 }
239
240 bool battfs_init(struct BattFsSuper *d);
241 bool battfs_close(struct BattFsSuper *disk);
242
243 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
244 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
245 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
246 #endif /* FS_BATTFS_H */