Refactor to use new protocol module and sipo.
[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  *
34  * \author Francesco Sacchi <batt@develer.com>
35  *
36  * \brief BattFS: a filesystem for embedded platforms (interface).
37  * TODO: Add detailed filesystem description.
38  *
39  * $WIZ$ module_name = "battfs"
40  * $WIZ$ module_depends = "rotating_hash", "kfile"
41  * $WIZ$ module_configuration = "bertos/cfg/cfg_battfs.h"
42  */
43
44 #ifndef FS_BATTFS_H
45 #define FS_BATTFS_H
46
47 #include <cfg/compiler.h> // uintXX_t; STATIC_ASSERT
48 #include <cpu/types.h> // CPU_BITS_PER_CHAR
49 #include <algo/rotating_hash.h>
50 #include <struct/list.h>
51 #include <io/kfile.h>
52 #include <io/kblock.h>
53
54 typedef uint16_t fill_t;    ///< Type for keeping trace of space filled inside a page
55 typedef fill_t   pgaddr_t;  ///< Type for addressing space inside a page
56 typedef uint16_t pgcnt_t;   ///< Type for counting pages on disk
57 typedef pgcnt_t  pgoff_t;   ///< Type for counting pages inside a file
58 typedef uint8_t  inode_t;   ///< Type for file inodes
59 typedef uint64_t  seq_t;    ///< Type for page seq number, at least 40bits wide.
60 typedef rotating_t fcs_t;   ///< Type for header FCS.
61
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         fill_t   fill;  ///< Filled bytes in page.
74         pgoff_t  pgoff; ///< Page offset inside file.
75
76         /**
77          * Page sequence number.
78          * Every time a page is rewritten the seq number is
79          * increased by 1. seq_t is wide enough to not to perform
80          * a wrap around before the memory death.
81          * So it can be kept as it would be
82          * monotonically increasing for our needs.
83          */
84         seq_t    seq;
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  * Maximum page address.
101  */
102 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
103
104 /**
105  * Max number of files.
106  */
107 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
108
109 /**
110  * Sentinel used to keep trace of unset pages in disk->page_array.
111  */
112 #define PAGE_UNSET_SENTINEL ((pgcnt_t)((1L << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1))
113
114 typedef uint32_t disk_size_t; ///< Type for disk sizes.
115
116 /**
117  * Context used to describe a disk.
118  * This context structure will be used to access disk.
119  * Must be initialized by hw memory driver.
120  */
121 typedef struct BattFsSuper
122 {
123         KBlock *dev;             ///< Block device context (physical disk).
124
125         pgaddr_t data_size;      ///< Size of space usable for data in a disk page, in bytes. The rest is used by the page header.
126         /**
127          * Page allocation array.
128          * This array must be allocated somewhere and
129          * must have enough space for page_count elements.
130          * Is used by the filesystem to represent
131          * the entire disk in memory.
132          */
133         pgcnt_t *page_array;
134
135         /**
136          * Lowest address, in page array, for free pages.
137          * Pages above this element are free for use.
138          */
139         pgcnt_t free_page_start;
140
141         disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
142         disk_size_t free_bytes;  ///< Free space on the disk.
143
144         List file_opened_list;       ///< List used to keep trace of open files.
145         /* TODO add other fields. */
146 } BattFsSuper;
147
148 /**
149  * True if space on \a disk is over.
150  */
151 #define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->dev->blk_cnt)
152
153 typedef uint8_t filemode_t;  ///< Type for file open modes.
154 typedef int32_t file_size_t; ///< Type for file sizes.
155
156 /**
157  * Modes for battfs_fileopen.
158  * \{
159  */
160 #define BATTFS_CREATE BV(0)  ///< Create file if does not exist
161 #define BATTFS_RD     BV(1)  ///< Open file for reading
162 #define BATTFS_WR     BV(2)  ///< Open file fir writing
163 /*/}*/
164
165
166 /**
167  * File errors.
168  * \{
169  */
170 #define BATTFS_NEGATIVE_SEEK_ERR   BV(0) ///< Trying to read/write before file start.
171 #define BATTFS_DISK_READ_ERR       BV(1) ///< Error reading from disk device.
172 #define BATTFS_DISK_WRITE_ERR      BV(2) ///< Error writing in the disk device.
173 #define BATTFS_DISK_SPACEOVER_ERR  BV(3) ///< No more disk space available.
174 #define BATTFS_DISK_FLUSHBUF_ERR   BV(4) ///< Error flushing (writing) the current page to disk.
175 #define BATTFS_FILE_NOT_FOUND_ERR  BV(5) ///< File not found on disk.
176 /*/}*/
177
178 /**
179  * Describe a BattFs file usign a KFile.
180  */
181 typedef struct BattFs
182 {
183         KFile fd;           ///< KFile context
184         Node link;          ///< Link for inserting in opened file list
185         inode_t inode;      ///< inode of the opened file
186         BattFsSuper *disk;  ///< Disk context
187         filemode_t mode;    ///< File open mode
188         pgcnt_t *start;     ///< Pointer to page_array file start position.
189         pgcnt_t max_off;    ///< Max page offset allocated for the file.
190         int errors;         ///< File status/errors
191 } BattFs;
192
193 /**
194  * Id for battfs file descriptors.
195  */
196 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
197
198 /**
199  * Macro used to cast a KFile to a BattFS.
200  * Also perform dynamic type check.
201  */
202 INLINE BattFs * BATTFS_CAST(KFile *fd)
203 {
204         ASSERT(fd->_type == KFT_BATTFS);
205         return (BattFs *)fd;
206 }
207
208 bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size);
209 bool battfs_fsck(struct BattFsSuper *disk);
210 bool battfs_umount(struct BattFsSuper *disk);
211
212 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
213 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
214
215 void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
216 void battfs_eraseBlock(KBlock *dev, pgcnt_t page);
217 #endif /* FS_BATTFS_H */