Finished battfs_init.
[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 pgoff_t  mark_t;
52 typedef uint8_t  inode_t;
53 typedef uint8_t  seq_t;
54 typedef rotating_t fcs_t;
55
56 /**
57  * BattFS page header.
58  * \note fields order is chosen to comply
59  * with CPU alignment.
60  */
61 typedef struct BattFsPageHeader
62 {
63         inode_t  inode; ///< File inode (file identifier).
64         seq_t    seq;   ///< Page sequence number.
65         mark_t   mark;  ///< Marker used to keep trace of free/used pages.
66         pgoff_t  pgoff; ///< Page offset inside file.
67         fill_t   fill;  ///< Filled bytes in page.
68         uint16_t rfu;   ///< Reserved for future use, 0xFFFF for now.
69
70         /**
71          * FCS (Frame Check Sequence) of the page header.
72          * \note This field must be the last one!
73          *       This is needed because if the page is only partially
74          *       written, we can use this to detect it.
75          */
76         fcs_t fcs;
77 } BattFsPageHeader;
78 /* Ensure structure has no padding added */
79 STATIC_ASSERT(sizeof(BattFsPageHeader) == 12);
80
81
82 /**
83  * Mark for valid pages.
84  * Simply set to 1 all field bits.
85  */
86 #define MARK_PAGE_VALID ((1 << (CPU_BITS_PER_CHAR * sizeof(mark_t))) - 1)
87
88 /**
89  * Max number of files.
90  */
91 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
92
93 /* Fwd decl */
94 struct BattFsSuper;
95
96 /**
97  * Type for disk page addressing.
98  */
99 typedef uint16_t pgcnt_t;
100
101 /**
102  * Sentinel used to keep trace of unset pages in disk->pag_array.
103  */
104 #define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
105
106 /**
107  * Type interface for disk init function.
108  * \return true if all is ok, false otherwise.
109  */
110 typedef bool (*disk_open_t) (struct BattFsSuper *d);
111
112 /**
113  * Type interface for disk page read function.
114  * \a page is the page address, \a addr the address inside the page,
115  * \a size the lenght to be read.
116  * \return the number of bytes read.
117  */
118 typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
119
120 /**
121  * Type interface for disk page write function.
122  * \a page is the page address, \a addr the address inside the page,
123  * \a size the lenght to be written.
124  * \return the number of bytes written.
125  */
126 typedef size_t  (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
127
128 /**
129  * Type interface for disk page erase function.
130  * \a page is the page address.
131  * \return true if all is ok, false otherwise.
132  */
133 typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
134
135 /**
136  * Type interface for disk deinit function.
137  * \return true if all is ok, false otherwise.
138  */
139 typedef bool (*disk_close_t) (struct BattFsSuper *d);
140
141
142 typedef uint32_t disk_size_t; ///< Type for disk sizes.
143
144 /**
145  * Context used to describe a disk.
146  * This context structure will be used to access disk.
147  * Must be initialized by hw memory driver.
148  */
149 typedef struct BattFsSuper
150 {
151         disk_open_t open;        ///< Disk init.
152         disk_page_read_t  read;  ///< Page read.
153         disk_page_write_t write; ///< Page write.
154         disk_page_erase_t erase; ///< Page erase.
155         disk_close_t close;      ///< Disk deinit.
156
157         pgaddr_t page_size;      ///< Size of a disk page, in bytes.
158         pgcnt_t page_count;      ///< Number of pages on disk.
159
160         /**
161          * Page allocation array.
162          * This array must be allocated somewhere and
163          * must have enough space for page_count elements.
164          * Is used by the filesystem to represent
165          * the entire disk in memory.
166          */
167         pgcnt_t *page_array;
168
169         mark_t min_free; ///< Lowest sequence number of free page
170         mark_t max_free; ///< Highest sequence number of free page
171
172         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
173         disk_size_t free_bytes;  ///< Free space on the disk.
174         
175         /* TODO add other fields. */
176 } BattFsSuper;
177
178 bool battfs_init(struct BattFsSuper *d);
179
180 #endif /* FS_BATTFS_H */