Simplify sequence numbering: now pages have a monotonically increasing sequence numnber.
[bertos.git] / bertos / 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  * \brief BattFS: a filesystem for embedded platforms (implementation).
34  *
35  * \version $Id:$
36  *
37  * \author Francesco Sacchi <batt@develer.com>
38  *
39  */
40
41 #include "battfs.h"
42
43 #include <cfg/debug.h>
44 #include <cfg/macros.h> /* MIN, MAX */
45 #include <cpu/byteorder.h> /* cpu_to_xx */
46
47 #define LOG_LEVEL       LOG_LVL_INFO
48 #define LOG_FORMAT      LOG_FMT_VERBOSE
49 #include <cfg/log.h>
50
51 #include <string.h> /* memset, memmove */
52
53
54 /**
55  * Convert from memory representation to disk structure.
56  * \note filesystem is in little-endian format.
57  */
58 INLINE void battfs_to_disk(struct BattFsPageHeader *hdr, uint8_t *buf)
59 {
60         STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
61         buf[0] = hdr->inode;
62
63         buf[1] = hdr->fill;
64         buf[2] = hdr->fill >> 8;
65
66         buf[3] = hdr->pgoff;
67         buf[4] = hdr->pgoff >> 8;
68
69         /*
70          * Sequence number is 40 bits long.
71          * No need to take care of wraparonds: the memory will die first!
72          */
73         buf[5] = hdr->seq;
74         buf[6] = hdr->seq >> 8;
75         buf[7] = hdr->seq >> 16;
76         buf[8] = hdr->seq >> 24;
77         buf[9] = hdr->seq >> 32;
78
79         /*
80          * This field must be the last one!
81          * This is needed because if the page is only partially
82          * written, we can use this to detect it.
83          */
84         buf[10] = hdr->fcs;
85         buf[11] = hdr->fcs >> 8;
86 }
87
88 /**
89  * Convert from disk structure to memory representation.
90  * \note filesystem is in little-endian format.
91  */
92 INLINE void disk_to_battfs(uint8_t *buf, struct BattFsPageHeader *hdr)
93 {
94         STATIC_ASSERT(BATTFS_HEADER_LEN == 12);
95         hdr->inode = buf[0];
96         hdr->fill = buf[2] << 8 | buf[1];
97         hdr->pgoff = buf[4] << 8 | buf[3];
98         hdr->seq = (seq_t)buf[9] << 32 | (seq_t)buf[8] << 24 | (seq_t)buf[7] << 16 | buf[6] << 8 | buf[5];
99         hdr->fcs = buf[11] << 8 | buf[10];
100 }
101
102 /**
103  * Compute the fcs of the header.
104  */
105 static fcs_t computeFcs(struct BattFsPageHeader *hdr)
106 {
107         uint8_t buf[BATTFS_HEADER_LEN];
108         fcs_t cks;
109
110         battfs_to_disk(hdr, buf);
111         rotating_init(&cks);
112         /* fcs is at the end of whole header */
113         rotating_update(buf, BATTFS_HEADER_LEN - sizeof(fcs_t), &cks);
114         return cks;
115 }
116
117
118 /**
119  * Read header of page \a page.
120  * \return true on success, false otherwise.
121  */
122 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
123 {
124         uint8_t buf[BATTFS_HEADER_LEN];
125         /*
126          * Read header from disk.
127          * Header is actually a footer, and so
128          * resides at page end.
129          */
130         if (disk->read(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
131             != BATTFS_HEADER_LEN)
132         {
133                 LOG_ERR("Error: page[%d]\n", page);
134                 return false;
135         }
136
137         /* Fill header */
138         disk_to_battfs(buf, hdr);
139
140         return true;
141 }
142
143 /**
144  * Write header of page \a page.
145  * \return true on success, false otherwise.
146  */
147 static bool battfs_writeHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
148 {
149         uint8_t buf[BATTFS_HEADER_LEN];
150
151         /* Fill buffer */
152         battfs_to_disk(hdr, buf);
153
154         /*
155          * write header to disk.
156          * Header is actually a footer, and so
157          * resides at page end.
158          */
159         if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
160             != BATTFS_HEADER_LEN)
161         {
162                 LOG_ERR("Error: page[%d]\n", page);
163                 return false;
164         }
165         return true;
166 }
167
168 /**
169  * Count the number of pages from
170  * inode 0 to \a inode in \a filelen_table.
171  */
172 static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode)
173 {
174         pgcnt_t cnt = 0;
175
176         for (inode_t i = 0; i < inode; i++)
177                 cnt += filelen_table[i];
178
179         return cnt;
180 }
181
182 /**
183  * Move all pages in page allocation array from \a src to \a src + \a offset.
184  * The number of pages moved is page_count - MAX(dst, src).
185  */
186 static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset)
187 {
188         pgcnt_t dst = src + offset;
189         memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t));
190
191         if (offset < 0)
192         {
193                 /* Fill empty space in array with sentinel */
194                 for (pgcnt_t page = disk->page_count + offset; page < disk->page_count; page++)
195                         disk->page_array[page] = PAGE_UNSET_SENTINEL;
196         }
197 }
198
199 /**
200  * Count number of pages per file on \a disk.
201  * This information is registered in \a filelen_table.
202  * Array index represent file inode, while value contained
203  * is the number of pages used by that file.
204  *
205  * \return true if ok, false on disk read errors.
206  * \note The whole disk is scanned once.
207  */
208 static bool countDiskFilePages(struct BattFsSuper *disk, pgoff_t *filelen_table)
209 {
210         BattFsPageHeader hdr;
211         disk->free_page_start = 0;
212
213         /* Count the number of disk page per file */
214         for (pgcnt_t page = 0; page < disk->page_count; page++)
215         {
216                 if (!battfs_readHeader(disk, page, &hdr))
217                         return false;
218
219                 /* Increase free space */
220                 disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
221
222                 /* Check header FCS */
223                 if (hdr.fcs == computeFcs(&hdr))
224                 {
225                         ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
226
227                         /* Page is valid and is owned by a file */
228                         filelen_table[hdr.inode]++;
229
230                         /* Keep trace of free space */
231                         disk->free_bytes -= hdr.fill;
232                         disk->free_page_start++;
233                 }
234         }
235         LOG_INFO("free_bytes:%d, free_page_start:%d\n", disk->free_bytes, disk->free_page_start);
236
237         return true;
238 }
239
240 /**
241  * Fill page allocation array of \a disk
242  * using file lenghts in \a filelen_table.
243  *
244  * The page allocation array is an array containings all file infos.
245  * Is ordered by file, and within each file is ordered by page offset
246  * inside file.
247  * e.g. : at page array[0] you will find page address of the first page
248  * of the first file (if present).
249  * Free blocks are allocated after the last file, starting from invalid ones
250  * and continuing with the marked free ones.
251  *
252  * \return true if ok, false on disk read errors.
253  * \note The whole disk is scanned once.
254  */
255 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
256 {
257         BattFsPageHeader hdr;
258         pgcnt_t curr_free_page = disk->free_page_start;
259         /* Fill page allocation array */
260         for (pgcnt_t page = 0; page < disk->page_count; page++)
261         {
262                 if (!battfs_readHeader(disk, page, &hdr))
263                         return false;
264
265                 /* Check header FCS */
266                 if (hdr.fcs == computeFcs(&hdr))
267                 {
268                         /* Compute array position */
269                         pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
270                         array_pos += hdr.pgoff;
271
272
273                         /* Check if position is already used by another page of the same file */
274                         if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
275                                 disk->page_array[array_pos] = page;
276                         else
277                         {
278                                 BattFsPageHeader hdr_prv;
279
280                                 if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_prv))
281                                         return false;
282
283                                 /* Check header FCS */
284                                 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
285
286                                 /* Only the very same page with a different seq number can be here */
287                                 ASSERT(hdr.inode == hdr_prv.inode);
288                                 ASSERT(hdr.pgoff == hdr_prv.pgoff);
289                                 ASSERT(hdr.seq != hdr_prv.seq);
290
291                                 pgcnt_t new_page, old_page;
292                                 fill_t old_fill;
293
294                                 /*
295                                  * Sequence number comparison: since
296                                  * seq is 40 bits wide, it wraps once
297                                  * every 1.1E12 times.
298                                  * The memory will not live enough to
299                                  * see a wraparound, so we can use a simple
300                                  * compare here.
301                                  */
302                                 if (hdr.seq > hdr_prv.seq)
303                                 {
304                                         /* Current header is newer than the previuos one */
305                                         old_page = disk->page_array[array_pos];
306                                         new_page = page;
307                                         old_fill = hdr_prv.fill;
308                                 }
309                                 else
310                                 {
311                                         /* Previous header is newer than the current one */
312                                         old_page = page;
313                                         new_page = disk->page_array[array_pos];
314                                         old_fill = hdr.fill;
315                                 }
316
317                                 /* Set new page */
318                                 disk->page_array[array_pos] = new_page;
319                                 /* Add free space */
320                                 disk->free_bytes += old_fill;
321                                 /* Shift all array one position to the left, overwriting duplicate page */
322                                 array_pos -= hdr.pgoff;
323                                 array_pos += filelen_table[hdr.inode];
324                                 movePages(disk, array_pos, -1);
325                                 /* Move back all indexes */
326                                 filelen_table[hdr.inode]--;
327                                 disk->free_page_start--;
328                                 curr_free_page--;
329                                 /* Set old page as free */
330                                 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
331                                 disk->page_array[curr_free_page++] = old_page;
332
333                         }
334                 }
335                 else
336                 {
337                         /* Invalid page, keep as free */
338                         ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
339                         LOG_INFO("Page %d invalid, keeping as free\n", page);
340                         disk->page_array[curr_free_page++] = page;
341                 }
342         }
343         return true;
344 }
345
346 #if 0
347 /**
348  * Find the latest version of a page, starting from the
349  * page supplied by \a page_array.
350  * The pages are read from the disk until a different
351  * inode or page offset is found.
352  * The lastest version of the page is moved in the first
353  * position of \a page_array.
354  * \return the number of old versions of the page or PAGE_ERROR
355  *         on disk read errors.
356  */
357 static pgcnt_t findLastVersion(struct BattFsSuper *disk, pgcnt_t *page_array)
358 {
359         pgcnt_t *array_start = page_array;
360         BattFsPageHeader hdr;
361         if (!battfs_readHeader(disk, *page_array++, &hdr))
362                 return PAGE_ERROR;
363
364         /* Free space: early bailout */
365         if (hdr.fcs != computeFcs(&hdr))
366                 return 0;
367
368         /*
369          * If the first page is valid,
370          * inode and pg_off in the array are taken
371          * as the current page markers.
372          */
373         inode_t curr_inode = hdr.inode;
374         pgoff_t curr_pgoff = hdr.pgoff;
375
376         /* Temps used to find the sequence number range */
377         seq_t minl = HALF_SEQ - 1;
378         seq_t maxl = 0;
379         seq_t minh = MAX_SEQ;
380         seq_t maxh = MAX_SEQ;
381         pgcnt_t lpos = 0, hpos = 0, dup_cnt = 0;
382
383         /*
384          * Find min and max values for the two
385          * half of seq_num range.
386          * With this we can find seqnum wraparounds.
387          * seq_t is a type that has at least 1 bit more than
388          * pgaddr_t. So all version of a page blocks can be numbered using
389          * at most half numbers of a seq_t type.
390          * The sequence number algorithm increments by 1 the previous seq_num
391          * every time a page is rewritten. So the sequence is
392          * guaranteed to be countiguous.
393          * Only wrap arounds may happen, but due to half size sequence limitation,
394          * there are only 4 possible situations:
395          *
396          * \verbatim
397          *    |------lower half------|-------upper half-------|
398          *
399          * 1) |------minl*****maxl---|------------------------|
400          * 2) |------minl********maxl|minh******maxh----------|
401          * 3) |----------------------|----minh*******maxh-----|
402          * 4) |minl******maxl--------|------------minh****maxh|
403          * \endverbatim
404          *
405          * Situations 1 and 3 are easy to detect, while 2 and 4 require more care.
406          */
407         do
408         {
409                 if (hdr.seq < HALF_SEQ)
410                 {
411                         minl = MIN(minl, hdr.seq);
412                         if (hdr.seq > maxl)
413                         {
414                                 maxl = hdr.seq;
415                                 lpos = dup_cnt;
416                         }
417                 }
418                 else
419                 {
420                         minh = MIN(minh, hdr.seq);
421                         if (hdr.seq > maxh)
422                         {
423                                 maxh = hdr.seq;
424                                 hpos = dup_cnt;
425                         }
426                 }
427
428                 if (!battfs_readHeader(disk, *page_array++, &hdr))
429                         return PAGE_ERROR;
430                 dup_cnt++;
431         }
432         while (curr_inode == hdr.inode && curr_pgoff == hdr.pgoff && hdr.fcs == computeFcs(&hdr));
433
434
435         /* Return early if there is only one version of the current page */
436         if (dup_cnt == 1)
437                 return 0;
438
439         /* Find the position in the array of the last version of the page */
440         pgcnt_t last_ver = hpos;
441         if (maxl >= minl)
442         {
443                 /* Valid interval found in lower half */
444                 if (maxh >= minh)
445                 {
446                         /* Valid interval also found in upper half */
447                         if (maxl != minh - 1)
448                         {
449                                 /* Interval starts in upper half and ends in lower */
450                                 ASSERT(minl == 0);
451                                 ASSERT(maxh == MAX_SEQ);
452
453                                 last_ver = lpos;
454                         }
455                 }
456                 else
457                         /*
458                          * Upper interval is invalid.
459                          * Use lower values.
460                          */
461                         last_ver = lpos;
462         }
463
464         /* Put last page version at array start position */
465         SWAP(array_start[0], array_start[last_ver]);
466
467         return dup_cnt - 1;
468 }
469
470 /**
471  * Collect old pages, removing empty spaces from \a pg_array, for a maximum len of \a pg_len.
472  * Once the collect task is completed, copy \a old_cnt pages from \a old_pages at the
473  * end of free space in pg_array.
474  */
475 void collectOldPages(pgcnt_t *pg_array, pgcnt_t pg_len, pgcnt_t *old_pages, pgcnt_t old_cnt)
476 {
477         bool copy = false;
478         pgcnt_t gap = 0;
479
480         for (pgcnt_t curr_page = 0; curr_page < pg_len; curr_page++)
481         {
482                 if (!copy)
483                 {
484                         if (pg_array[curr_page] == PAGE_UNSET_SENTINEL)
485                                 gap++;
486                         else
487                         {
488                                 pg_array[curr_page - gap] = pg_array[curr_page];
489                                 copy = true;
490                         }
491                 }
492                 else
493                 {
494                         if (pg_array[curr_page] != PAGE_UNSET_SENTINEL)
495                                 pg_array[curr_page - gap] = pg_array[curr_page];
496                         else
497                         {
498                                 gap++;
499                                 copy = false;
500                         }
501                 }
502         }
503         ASSERT(gap == old_cnt);
504         pg_array += pg_len - old_cnt;
505
506         memcpy(pg_array, old_pages, old_cnt * sizeof(pgcnt_t));
507 }
508
509 /**
510  * This function scan the page array of \a disk looking for
511  * old versions of the same page.
512  *
513  * Only the last version is kept as valid, the old ones are inserted
514  * in the free blocks heap.
515  * \return true if ok, false on disk read errors.
516  * \note The whole disk is scanned once.
517  */
518 static bool dropOldPages(struct BattFsSuper *disk)
519 {
520         #define OLD_PAGE_BUFLEN 64
521         pgcnt_t old_pages[OLD_PAGE_BUFLEN];
522         pgcnt_t old_cnt = 0;
523
524         pgcnt_t *curr_page = disk->page_array;
525         pgcnt_t *collect_start = disk->page_array;
526         pgcnt_t collect_len = disk->page_count;
527         pgcnt_t dup_pages;
528
529         do
530         {
531                 dup_pages = findLastVersion(disk, curr_page);
532                 if (dup_pages == PAGE_ERROR)
533                         return false;
534                 /* The first page is the last version */
535                 curr_page++;
536                 while (dup_pages--)
537                 {
538                         if (old_cnt >= OLD_PAGE_BUFLEN)
539                         {
540                                 collectOldPages(collect_start, collect_len, old_pages, old_cnt);
541                                 collect_len -= old_cnt;
542                                 disk->free_bytes += old_cnt * (disk->page_size - BATTFS_HEADER_LEN);
543                                 disk->free_page_start -= old_cnt;
544                                 curr_page -= old_cnt;
545                                 collect_start = curr_page;
546                                 old_cnt = 0;
547                         }
548
549                         old_pages[old_cnt++] = *curr_page;
550                         *curr_page++ = PAGE_UNSET_SENTINEL;
551                 }
552         }
553         while (curr_page < disk->page_array + disk->free_page_start);
554
555         collectOldPages(collect_start, collect_len, old_pages, old_cnt);
556         disk->free_bytes += old_cnt * (disk->page_size - BATTFS_HEADER_LEN);
557         disk->free_page_start -= old_cnt;
558
559         return true;
560 }
561 #endif
562
563 /**
564  * Initialize and mount disk described by
565  * \a disk.
566  * \return false on errors, true otherwise.
567  */
568 bool battfs_init(struct BattFsSuper *disk)
569 {
570         pgoff_t filelen_table[BATTFS_MAX_FILES];
571
572         /* Sanity check */
573         ASSERT(disk->open);
574
575         /* Init disk device */
576         if (!disk->open(disk))
577         {
578                 LOG_ERR("open error\n");
579                 return false;
580         }
581
582         /* Disk open must set all of these */
583         ASSERT(disk->read);
584         ASSERT(disk->write);
585         ASSERT(disk->erase);
586         ASSERT(disk->close);
587         ASSERT(disk->page_size);
588         ASSERT(disk->page_count);
589         ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
590         ASSERT(disk->page_array);
591
592         memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
593
594         disk->free_bytes = 0;
595         disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
596
597         /* Count pages per file */
598         if (!countDiskFilePages(disk, filelen_table))
599         {
600                 LOG_ERR("error counting file pages\n");
601                 return false;
602         }
603
604         /* Once here, we have filelen_table filled with file lengths */
605
606         /* Fill page array with sentinel */
607         for (pgcnt_t page = 0; page < disk->page_count; page++)
608                 disk->page_array[page] = PAGE_UNSET_SENTINEL;
609
610         /* Fill page allocation array using filelen_table */
611         if (!fillPageArray(disk, filelen_table))
612         {
613                 LOG_ERR("error filling page array\n");
614                 return false;
615         }
616
617         /* Init list for opened files. */
618         LIST_INIT(&disk->file_opened_list);
619         return true;
620 }
621
622 /**
623  * Flush file \a fd.
624  * \return 0 if ok, EOF on errors.
625  */
626 static int battfs_flush(struct KFile *fd)
627 {
628         (void)fd;
629         #warning TODO
630         return 0;
631 }
632
633 /**
634  * Close file \a fd.
635  * \return 0 if ok, EOF on errors.
636  */
637 static int battfs_fileclose(struct KFile *fd)
638 {
639         BattFs *fdb = BATTFS_CAST(fd);
640
641         battfs_flush(fd);
642         REMOVE(&fdb->link);
643         return 0;
644 }
645
646 /**
647  * Read from file \a fd \a size bytes in \a buf.
648  * \return The number of bytes read.
649  */
650 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
651 {
652         BattFs *fdb = BATTFS_CAST(fd);
653         uint8_t *buf = (uint8_t *)_buf;
654
655         size_t total_read = 0;
656         pgoff_t pg_offset;
657         pgaddr_t addr_offset;
658         pgaddr_t read_len;
659
660         size = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
661
662         while (size)
663         {
664                 pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
665                 addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
666                 read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
667
668                 /* Read from disk */
669                 if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
670                 {
671                         #warning TODO set error?
672                 }
673
674                 size -= read_len;
675                 fd->seek_pos += read_len;
676                 total_read += read_len;
677                 buf += read_len;
678         }
679         return total_read;
680 }
681
682
683 /**
684  * Search file \a inode in \a disk using a binary search.
685  * \return pointer to file start in disk->page_array
686  * if file exists, NULL otherwise.
687  */
688 static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode)
689 {
690         BattFsPageHeader hdr;
691         pgcnt_t first = 0, page, last = disk->page_count -1;
692         fcs_t fcs;
693
694         while (first <= last)
695         {
696                 page = (first + last) / 2;
697
698                 if (!battfs_readHeader(disk, disk->page_array[page], &hdr))
699                         return NULL;
700
701                 fcs = computeFcs(&hdr);
702                 if (hdr.fcs == fcs && hdr.inode == inode)
703                         return (&disk->page_array[page]) - hdr.pgoff;
704                 else if (hdr.fcs == fcs && hdr.inode < inode)
705                         first = page + 1;
706                 else
707                         last = page - 1;
708         }
709
710         return NULL;
711 }
712
713 /**
714  * \return true if file \a inode exists on \a disk, false otherwise.
715  */
716 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
717 {
718         return findFile(disk, inode) != NULL;
719 }
720
721 /**
722  * Count size of file \a inode on \a disk, starting at pointer \a start
723  * in disk->page_array. Size is written in \a size.
724  * \return true if all s ok, false on disk read errors.
725  */
726 static bool countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode, file_size_t *size)
727 {
728         *size = 0;
729         BattFsPageHeader hdr;
730
731         for (;;)
732         {
733                 if (!battfs_readHeader(disk, *start++, &hdr))
734                         return false;
735                 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
736                         *size += hdr.fill;
737                 else
738                         return true;
739         }
740 }
741
742 /**
743  * Open file \a inode from \a disk in \a mode.
744  * File context is stored in \a fd.
745  * \return true if ok, false otherwise.
746  */
747 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
748 {
749         Node *n;
750
751         memset(fd, 0, sizeof(*fd));
752
753         /* Search file start point in disk page array */
754         fd->start = findFile(disk, inode);
755         if (fd->start == NULL)
756         {
757                 if (!(mode & BATTFS_CREATE))
758                         return false;
759
760                 /* File does not exist, create it */
761                 BattFsPageHeader hdr;
762                 hdr.inode = inode;
763                 hdr.seq = 0;
764                 hdr.fill = 0;
765                 hdr.pgoff = 0;
766                 hdr.fcs = computeFcs(&hdr);
767                 #warning TODO: get a free block and write on disk!
768         }
769
770         /* Fill file size */
771         if (!countFileSize(disk, fd->start, inode, &fd->fd.size))
772                 return false;
773
774         /* Reset seek position */
775         fd->fd.seek_pos = 0;
776
777         /* Insert file handle in list, ordered by inode, ascending. */
778         FOREACH_NODE(n, &disk->file_opened_list)
779         {
780                 BattFs *file = containerof(n, BattFs, link);
781                 if (file->inode >= inode)
782                         break;
783         }
784         INSERT_BEFORE(&fd->link, n);
785
786         /* Fill in data */
787         fd->inode = inode;
788         fd->mode = mode;
789         fd->disk = disk;
790
791         fd->fd.close = battfs_fileclose;
792         fd->fd.flush = battfs_flush;
793         fd->fd.read = battfs_read;
794         fd->fd.reopen = kfile_genericReopen;
795         fd->fd.seek = kfile_genericSeek;
796
797 #warning TODO battfs_write, battfs_error, battfs_clearerr
798 #if 0
799         fd->fd.write = battfs_write;
800         fd->fd.error = battfs_error;
801         fd->fd.clearerr = battfs_clearerr;
802 #endif
803
804         DB(fd->fd._type = KFT_BATTFS);
805
806         return true;
807 }
808
809 /**
810  * Close \a disk.
811  */
812 bool battfs_close(struct BattFsSuper *disk)
813 {
814         Node *n;
815         int res = 0;
816
817         /* Close all open files */
818         FOREACH_NODE(n, &disk->file_opened_list)
819         {
820                 BattFs *file = containerof(n, BattFs, link);
821                 res += battfs_fileclose(&file->fd);
822         }
823
824         /* Close disk */
825         return disk->close(disk) && (res == 0);
826 }
827
828 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
829 {
830         BattFsPageHeader hdr;
831
832         hdr.inode = inode;
833         hdr.fill = fill;
834         hdr.pgoff = pgoff;
835         hdr.seq = seq;
836         hdr.fcs = computeFcs(&hdr);
837
838         if (!battfs_writeHeader(disk, page, &hdr))
839         {
840                 LOG_ERR("error writing hdr\n");
841                 return false;
842         }
843
844         return true;
845 }