Complete first disk scan.
[bertos.git] / fs / battfs.c
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 (implementation).
38  */
39
40 #include "battfs.h"
41
42 #include <cfg/debug.h>
43 #include <cfg/macros.h> /* MIN, MAX */
44 #include <mware/byteorder.h> /* cpu_to_xx */
45
46
47 #include <string.h> /* memset */
48
49 /**
50  * Convert from cpu endianess to filesystem endianness.
51  * \note filesystem is in little-endian format.
52  */
53 INLINE void cpu_to_battfs(struct BattFsPageHeader *hdr)
54 {
55         STATIC_ASSERT(sizeof(hdr->inode) == 1);
56         STATIC_ASSERT(sizeof(hdr->seq) == 1);
57
58         STATIC_ASSERT(sizeof(hdr->mark) == 2);
59         hdr->mark = cpu_to_le16(hdr->mark);
60
61         STATIC_ASSERT(sizeof(hdr->fill) == 2);
62         hdr->fill = cpu_to_le16(hdr->fill);
63
64         STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
65         hdr->pgoff = cpu_to_le16(hdr->pgoff);
66
67         STATIC_ASSERT(sizeof(hdr->fcs) == 2);
68         hdr->fcs = cpu_to_le16(hdr->fcs);
69
70         STATIC_ASSERT(sizeof(hdr->rfu) == 2);
71         hdr->rfu = cpu_to_le16(hdr->rfu);
72 }
73
74
75 /**
76  * Convert from filesystem endianness to cpu endianess.
77  * \note filesystem is in little-endian format.
78  */
79 INLINE void battfs_to_cpu(struct BattFsPageHeader *hdr)
80 {
81         STATIC_ASSERT(sizeof(hdr->inode) == 1);
82         STATIC_ASSERT(sizeof(hdr->seq) == 1);
83
84         STATIC_ASSERT(sizeof(hdr->mark) == 2);
85         hdr->mark = le16_to_cpu(hdr->mark);
86
87         STATIC_ASSERT(sizeof(hdr->fill) == 2);
88         hdr->fill = le16_to_cpu(hdr->fill);
89
90         STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
91         hdr->pgoff = le16_to_cpu(hdr->pgoff);
92
93         STATIC_ASSERT(sizeof(hdr->fcs) == 2);
94         hdr->fcs = le16_to_cpu(hdr->fcs);
95
96         STATIC_ASSERT(sizeof(hdr->rfu) == 2);
97         hdr->rfu = le16_to_cpu(hdr->rfu);
98 }
99
100
101 /**
102  * Read header of page \a page.
103  * \return true on success, false otherwise.
104  * \note \a hdr is dirtyed even on errors.
105  */
106 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
107 {
108         /*
109          * Read header from disk.
110          * header is actually a footer, and so
111          * resides at page end.
112          */
113         if (disk->read(disk, page, disk->page_size - sizeof(BattFsPageHeader), hdr, sizeof(BattFsPageHeader))
114             != sizeof(BattFsPageHeader))
115         {
116                 TRACEMSG("Error: page[%d]\n", page);
117                 return false;
118         }
119
120         /* Fix endianess */
121         battfs_to_cpu(hdr);
122
123         ASSERT(hdr->fill <= disk->page_size - sizeof(BattFsPageHeader));
124         return true;
125 }
126
127 /**
128  * Initialize and mount disk described by
129  * \a d.
130  * \return false on errors, true otherwise.
131  */
132 bool battfs_init(struct BattFsSuper *disk)
133 {
134         BattFsPageHeader hdr;
135         rotating_t cks;
136         pgoff_t filelen_table[BATTFS_MAX_FILES];
137
138         /* Init disk device */
139         if (!disk->open(disk))
140         {
141                 TRACEMSG("Open error\n");
142                 return false;
143         }
144
145         memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
146
147         /* Initialize min free sequence number to max value */
148         disk->min_free = MARK_PAGE_VALID;
149         /* Initialize max free sequence number to min value */
150         disk->max_free = 0;
151
152         disk->free_bytes = 0;
153         disk->disk_size = (disk_size_t)(disk->page_size - sizeof(BattFsPageHeader)) * disk->page_count;
154
155         /* Count the number of disk page per files */
156         for (pgcnt_t page = 0; page < disk->page_count; page++)
157         {
158                 if (!battfs_readHeader(disk, page, &hdr))
159                         return false;
160
161                 /* Check header FCS */
162                 rotating_init(&cks);
163                 rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
164                 if (cks == hdr.fcs)
165                 {
166                         /* Page is valid */
167                         filelen_table[hdr.inode]++;
168
169                         /* Keep trace of free space */
170                         disk->free_bytes += disk->page_size - sizeof(BattFsPageHeader) - hdr.fill;
171                 }
172                 else
173                 {
174                         /* Check if putting mark to MARK_PAGE_VALID makes fcs correct */
175                         mark_t old_mark = hdr.mark;
176                         hdr.mark = MARK_PAGE_VALID;
177                         rotating_init(&cks);
178                         rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
179                         if (cks == hdr.fcs)
180                         {
181                                 /*
182                                  * This page is a valid free page.
183                                  * Update min and max free page sequence numbers.
184                                  */
185                                 disk->min_free = MIN(disk->min_free, old_mark);
186                                 disk->max_free = MAX(disk->max_free, old_mark);
187                         }
188                         else
189                                 TRACEMSG("Page [%d] invalid, keeping as free\n", page);
190
191                         /* Increase free space */
192                         filelen_table[BATTFS_FREE_INODE]++;
193                         disk->free_bytes += disk->page_size - sizeof(BattFsPageHeader);
194                 }
195         }
196
197         /* Once here, we have filelen_table filled with file lengths */
198         #warning Complete me!
199
200         
201         return true;    
202 }
203
204