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