Merge from branch.
[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
48 typedef uint16_t fill_t;
49 typedef fill_t   pgaddr_t;
50 typedef uint16_t pgoff_t;
51 typedef uint32_t mark_t;
52 typedef uint8_t  mark_roll_t;
53 typedef uint8_t  inode_t;
54 typedef uint8_t  seq_t;
55 typedef rotating_t fcs_t;
56
57 /**
58  * Size required for free block allocation is at least 1 bit more
59  * than page addressing.
60  */
61 STATIC_ASSERT(sizeof(mark_t) > sizeof(pgaddr_t));
62
63 /**
64  * BattFS page header, used to represent a page
65  * header in memory.
66  * To see how this is stored on disk:
67  * \see battfs_to_disk
68  * \see disk_to_battfs
69  */
70 typedef struct BattFsPageHeader
71 {
72         inode_t  inode; ///< File inode (file identifier).
73         seq_t    seq;   ///< Page sequence number.
74         fill_t   fill;  ///< Filled bytes in page.
75         pgoff_t  pgoff; ///< Page offset inside file.
76         mark_t   mark;  ///< Marker used to keep trace of free/used pages.
77
78         /**
79          * FCS (Frame Check Sequence) of the page header once the page
80          * as been marked as free.
81          */
82         fcs_t fcs_free;
83
84         /**
85          * FCS (Frame Check Sequence) of the page header.
86          */
87         fcs_t fcs;
88 } BattFsPageHeader;
89
90 /**
91  * Size of the header once saved on disk.
92  * \see battfs_to_disk
93  * \see disk_to_battfs
94  */
95 #define BATTFS_HEADER_LEN 13
96
97 /**
98  * Marks for valid pages.
99  * Simply set to 1 all field bits.
100  * \{
101  */
102 #define MARK_PAGE_VALID ((1LL << (CPU_BITS_PER_CHAR * sizeof(mark_t))) - 1)
103 #define FCS_FREE_VALID  ((1 << (CPU_BITS_PER_CHAR * sizeof(fcs_t))) - 1)
104 /* \} */
105
106
107 /**
108  * Half-size of free page marker.
109  * Used to keep trace of free marker wrap-arounds.
110  */
111 #define MARK_HALF_SIZE (1 << (CPU_BITS_PER_CHAR * sizeof(pgaddr_t) + 1))
112
113 /**
114  * Maximum page address.
115  */
116 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgaddr_t))) - 1)
117
118 /**
119  * Max number of files.
120  */
121 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
122
123 /* Fwd decl */
124 struct BattFsSuper;
125
126 /**
127  * Type for disk page addressing.
128  */
129 typedef uint16_t pgcnt_t;
130
131 /**
132  * Sentinel used to keep trace of unset pages in disk->page_array.
133  */
134 #define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
135
136 /**
137  * Type interface for disk init function.
138  * \return true if all is ok, false otherwise.
139  */
140 typedef bool (*disk_open_t) (struct BattFsSuper *d);
141
142 /**
143  * Type interface for disk page read function.
144  * \a page is the page address, \a addr the address inside the page,
145  * \a size the lenght to be read.
146  * \return the number of bytes read.
147  */
148 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
149
150 /**
151  * Type interface for disk page write function.
152  * \a page is the page address, \a addr the address inside the page,
153  * \a size the lenght to be written.
154  * \return the number of bytes written.
155  */
156 typedef size_t  (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
157
158 /**
159  * Type interface for disk page erase function.
160  * \a page is the page address.
161  * \return true if all is ok, false otherwise.
162  */
163 typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
164
165 /**
166  * Type interface for disk deinit function.
167  * \return true if all is ok, false otherwise.
168  */
169 typedef bool (*disk_close_t) (struct BattFsSuper *d);
170
171
172 typedef uint32_t disk_size_t; ///< Type for disk sizes.
173
174 /**
175  * Context used to describe a disk.
176  * This context structure will be used to access disk.
177  * Must be initialized by hw memory driver.
178  */
179 typedef struct BattFsSuper
180 {
181         disk_open_t open;        ///< Disk init.
182         disk_page_read_t  read;  ///< Page read.
183         disk_page_write_t write; ///< Page write.
184         disk_page_erase_t erase; ///< Page erase.
185         disk_close_t close;      ///< Disk deinit.
186
187         pgaddr_t page_size;      ///< Size of a disk page, in bytes.
188         pgcnt_t page_count;      ///< Number of pages on disk.
189
190         /**
191          * Page allocation array.
192          * This array must be allocated somewhere and
193          * must have enough space for page_count elements.
194          * Is used by the filesystem to represent
195          * the entire disk in memory.
196          */
197         pgcnt_t *page_array;
198
199         mark_t free_min;      ///< Lowest free page counter.
200         mark_t free_max;      ///< Highest free page counter.
201
202         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
203         disk_size_t free_bytes;  ///< Free space on the disk.
204         
205         /* TODO add other fields. */
206 } BattFsSuper;
207
208 bool battfs_init(struct BattFsSuper *d);
209
210 #endif /* FS_BATTFS_H */