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