Add cache read API.
[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->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
160             == BATTFS_HEADER_LEN && disk->save(disk, page)))
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.
250  *
251  * \return true if ok, false on disk read errors.
252  * \note The whole disk is scanned at max twice.
253  */
254 static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
255 {
256         BattFsPageHeader hdr;
257         pgcnt_t curr_free_page = disk->free_page_start;
258         /* Fill page allocation array */
259         for (pgcnt_t page = 0; page < disk->page_count; page++)
260         {
261                 if (!battfs_readHeader(disk, page, &hdr))
262                         return false;
263
264                 /* Check header FCS */
265                 if (hdr.fcs == computeFcs(&hdr))
266                 {
267                         /* Compute array position */
268                         pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
269                         array_pos += hdr.pgoff;
270
271
272                         /* Check if position is already used by another page of the same file */
273                         if (disk->page_array[array_pos] == PAGE_UNSET_SENTINEL)
274                                 disk->page_array[array_pos] = page;
275                         else
276                         {
277                                 BattFsPageHeader hdr_prv;
278
279                                 if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_prv))
280                                         return false;
281
282                                 /* Check header FCS */
283                                 ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
284
285                                 /* Only the very same page with a different seq number can be here */
286                                 ASSERT(hdr.inode == hdr_prv.inode);
287                                 ASSERT(hdr.pgoff == hdr_prv.pgoff);
288                                 ASSERT(hdr.seq != hdr_prv.seq);
289
290                                 pgcnt_t new_page, old_page;
291                                 fill_t old_fill;
292
293                                 /*
294                                  * Sequence number comparison: since
295                                  * seq is 40 bits wide, it wraps once
296                                  * every 1.1E12 times.
297                                  * The memory will not live enough to
298                                  * see a wraparound, so we can use a simple
299                                  * compare here.
300                                  */
301                                 if (hdr.seq > hdr_prv.seq)
302                                 {
303                                         /* Current header is newer than the previuos one */
304                                         old_page = disk->page_array[array_pos];
305                                         new_page = page;
306                                         old_fill = hdr_prv.fill;
307                                 }
308                                 else
309                                 {
310                                         /* Previous header is newer than the current one */
311                                         old_page = page;
312                                         new_page = disk->page_array[array_pos];
313                                         old_fill = hdr.fill;
314                                 }
315
316                                 /* Set new page */
317                                 disk->page_array[array_pos] = new_page;
318                                 /* Add free space */
319                                 disk->free_bytes += old_fill;
320                                 /* Shift all array one position to the left, overwriting duplicate page */
321                                 array_pos -= hdr.pgoff;
322                                 array_pos += filelen_table[hdr.inode];
323                                 movePages(disk, array_pos, -1);
324                                 /* Move back all indexes */
325                                 filelen_table[hdr.inode]--;
326                                 disk->free_page_start--;
327                                 curr_free_page--;
328                                 /* Set old page as free */
329                                 ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
330                                 disk->page_array[curr_free_page++] = old_page;
331
332                         }
333                 }
334                 else
335                 {
336                         /* Invalid page, keep as free */
337                         ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
338                         LOG_INFO("Page %d invalid, keeping as free\n", page);
339                         disk->page_array[curr_free_page++] = page;
340                 }
341         }
342         return true;
343 }
344
345
346 /**
347  * Flush the current \a disk buffer.
348  * \return true if ok, false on errors.
349  */
350 static bool battfs_flushBuffer(struct BattFsSuper *disk)
351 {
352         if (disk->cache_dirty)
353         {
354                 LOG_INFO("Flushing to disk page %d\n", disk->curr_page);
355                 if (!disk->erase(disk, disk->curr_page))
356                         return false;
357
358                 if (!disk->save(disk, disk->curr_page))
359                         return false;
360                 disk->cache_dirty = false;
361         }
362         return true;
363 }
364
365 /**
366  * Load \a new_page from \a disk in disk page buffer.
367  * If a previuos page is still dirty in the buffer, will be
368  * flushed first.
369  * \return true if ok, false on errors.
370  */
371 static bool battfs_loadPage(struct BattFsSuper *disk, pgcnt_t new_page)
372 {
373         LOG_INFO("Loading page %d\n", new_page);
374         if (disk->curr_page == new_page)
375                 return true;
376
377         if (!battfs_flushBuffer(disk))
378                 return false;
379
380         if (!disk->load(disk, new_page))
381                 return false;
382
383         disk->curr_page = new_page;
384         return true;
385 }
386
387
388 /**
389  * Initialize and mount disk described by
390  * \a disk.
391  * \return false on errors, true otherwise.
392  */
393 bool battfs_init(struct BattFsSuper *disk)
394 {
395         pgoff_t filelen_table[BATTFS_MAX_FILES];
396
397         /* Sanity check */
398         ASSERT(disk->open);
399
400         /* Init disk device */
401         if (!disk->open(disk))
402         {
403                 LOG_ERR("open error\n");
404                 return false;
405         }
406
407         /* Disk open must set all of these */
408         ASSERT(disk->read);
409         ASSERT(disk->load);
410         ASSERT(disk->bufferWrite);
411         ASSERT(disk->bufferRead);
412         ASSERT(disk->save);
413         ASSERT(disk->erase);
414         ASSERT(disk->close);
415         ASSERT(disk->page_size);
416         ASSERT(disk->page_count);
417         ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
418         ASSERT(disk->page_array);
419
420         memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
421
422         disk->free_bytes = 0;
423         disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
424
425         /* Count pages per file */
426         if (!countDiskFilePages(disk, filelen_table))
427         {
428                 LOG_ERR("error counting file pages\n");
429                 return false;
430         }
431
432         /* Once here, we have filelen_table filled with file lengths */
433
434         /* Fill page array with sentinel */
435         for (pgcnt_t page = 0; page < disk->page_count; page++)
436                 disk->page_array[page] = PAGE_UNSET_SENTINEL;
437
438         /* Fill page allocation array using filelen_table */
439         if (!fillPageArray(disk, filelen_table))
440         {
441                 LOG_ERR("error filling page array\n");
442                 return false;
443         }
444         #warning TODO: shuffle free blocks
445
446         /* Initialize page buffer cache */
447         disk->cache_dirty = false;
448         disk->curr_page = 0;
449         disk->load(disk, disk->curr_page);
450
451         /* Init list for opened files. */
452         LIST_INIT(&disk->file_opened_list);
453         return true;
454 }
455
456 /**
457  * Flush file \a fd.
458  * \return 0 if ok, EOF on errors.
459  */
460 static int battfs_flush(struct KFile *fd)
461 {
462         BattFs *fdb = BATTFS_CAST(fd);
463
464         if (battfs_flushBuffer(fdb->disk))
465                 return 0;
466         else
467                 return EOF;
468 }
469
470 /**
471  * Close file \a fd.
472  * \return 0 if ok, EOF on errors.
473  */
474 static int battfs_fileclose(struct KFile *fd)
475 {
476         BattFs *fdb = BATTFS_CAST(fd);
477
478         battfs_flush(fd);
479         REMOVE(&fdb->link);
480         return 0;
481 }
482
483
484 /**
485  * Write to file \a fd \a size bytes from \a buf.
486  * \return The number of bytes written.
487  */
488 static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
489 {
490         BattFs *fdb = BATTFS_CAST(fd);
491         const uint8_t *buf = (const uint8_t *)_buf;
492
493         size_t total_write = 0;
494         pgoff_t pg_offset;
495         pgaddr_t addr_offset;
496         pgaddr_t wr_len;
497
498         size = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
499
500         while (size)
501         {
502                 #warning TODO: outside EOF?
503
504                 pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
505                 addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
506                 wr_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
507
508
509                 if (fdb->start[pg_offset] != fdb->disk->curr_page)
510                 {
511                         if (!battfs_loadPage(fdb->disk, fdb->start[pg_offset]))
512                         {
513                                 #warning TODO set error?
514                                 return total_write;
515                         }
516
517                         /* Get a free page */
518                         fdb->disk->curr_page = fdb->disk->page_array[fdb->disk->free_page_start];
519                         movePages(fdb->disk, fdb->disk->free_page_start + 1, -1);
520
521                         /* Insert previous page in free blocks list */
522                         fdb->disk->page_array[fdb->disk->page_count - 1] = fdb->start[pg_offset];
523                         /* Assign new page */
524                         fdb->start[pg_offset] = fdb->disk->curr_page;
525                         #warning TODO: hdr have to be updated!
526                 }
527
528
529                 if (fdb->disk->bufferWrite(fdb->disk, addr_offset, buf, wr_len) != wr_len)
530                 {
531                         #warning TODO set error?
532                 }
533                 fdb->disk->cache_dirty = true;
534
535                 size -= wr_len;
536                 fd->seek_pos += wr_len;
537                 total_write += wr_len;
538                 buf += wr_len;
539                 #warning TODO: hdr have to be updated!
540         }
541         return total_write;
542
543 }
544
545
546 /**
547  * Read from file \a fd \a size bytes in \a buf.
548  * \return The number of bytes read.
549  */
550 static size_t battfs_read(struct KFile *fd, void *_buf, size_t size)
551 {
552         BattFs *fdb = BATTFS_CAST(fd);
553         uint8_t *buf = (uint8_t *)_buf;
554
555         size_t total_read = 0;
556         pgoff_t pg_offset;
557         pgaddr_t addr_offset;
558         pgaddr_t read_len;
559
560         size = MIN((kfile_off_t)size, fd->size - fd->seek_pos);
561
562         while (size)
563         {
564                 pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
565                 addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
566                 read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
567
568                 /* Try to read from cache */
569                 if (fdb->start[pg_offset] == fdb->disk->curr_page)
570                 {
571                         if (fdb->disk->bufferRead(fdb->disk, addr_offset, buf, read_len) != read_len)
572                         {
573                                 #warning TODO set error?
574                         }
575                 }
576                 /* Read from disk */
577                 else if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
578                 {
579                         #warning TODO set error?
580                 }
581
582                 size -= read_len;
583                 fd->seek_pos += read_len;
584                 total_read += read_len;
585                 buf += read_len;
586         }
587         return total_read;
588 }
589
590
591 /**
592  * Search file \a inode in \a disk using a binary search.
593  * \return pointer to file start in disk->page_array
594  * if file exists, NULL otherwise.
595  */
596 static pgcnt_t *findFile(BattFsSuper *disk, inode_t inode)
597 {
598         BattFsPageHeader hdr;
599         pgcnt_t first = 0, page, last = disk->free_page_start;
600         fcs_t fcs;
601
602         while (first < last)
603         {
604                 page = (first + last) / 2;
605
606                 if (!battfs_readHeader(disk, disk->page_array[page], &hdr))
607                         return NULL;
608
609                 fcs = computeFcs(&hdr);
610                 if (hdr.fcs == fcs && hdr.inode == inode)
611                         return (&disk->page_array[page]) - hdr.pgoff;
612                 else if (hdr.fcs == fcs && hdr.inode < inode)
613                         first = page + 1;
614                 else
615                         last = page - 1;
616         }
617
618         return NULL;
619 }
620
621 /**
622  * \return true if file \a inode exists on \a disk, false otherwise.
623  */
624 bool battfs_fileExists(BattFsSuper *disk, inode_t inode)
625 {
626         return findFile(disk, inode) != NULL;
627 }
628
629 /**
630  * Count size of file \a inode on \a disk, starting at pointer \a start
631  * in disk->page_array. Size is written in \a size.
632  * \return true if all s ok, false on disk read errors.
633  */
634 static file_size_t countFileSize(BattFsSuper *disk, pgcnt_t *start, inode_t inode)
635 {
636         file_size_t size = 0;
637         BattFsPageHeader hdr;
638
639         for (;;)
640         {
641                 if (!battfs_readHeader(disk, *start++, &hdr))
642                         return EOF;
643                 if (hdr.fcs == computeFcs(&hdr) && hdr.inode == inode)
644                         size += hdr.fill;
645                 else
646                         return size;
647         }
648 }
649
650 /**
651  * Open file \a inode from \a disk in \a mode.
652  * File context is stored in \a fd.
653  * \return true if ok, false otherwise.
654  */
655 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode)
656 {
657         Node *n;
658
659         memset(fd, 0, sizeof(*fd));
660
661         /* Search file start point in disk page array */
662         fd->start = findFile(disk, inode);
663         if (fd->start == NULL)
664         {
665                 if (!(mode & BATTFS_CREATE))
666                         return false;
667
668                 /* File does not exist, create it */
669                 BattFsPageHeader hdr;
670                 hdr.inode = inode;
671                 hdr.seq = 0;
672                 hdr.fill = 0;
673                 hdr.pgoff = 0;
674                 hdr.fcs = computeFcs(&hdr);
675                 #warning TODO: get a free block and write on disk!
676         }
677
678         /* Fill file size */
679         if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
680                 return false;
681
682         /* Reset seek position */
683         fd->fd.seek_pos = 0;
684
685         /* Insert file handle in list, ordered by inode, ascending. */
686         FOREACH_NODE(n, &disk->file_opened_list)
687         {
688                 BattFs *file = containerof(n, BattFs, link);
689                 if (file->inode >= inode)
690                         break;
691         }
692         INSERT_BEFORE(&fd->link, n);
693
694         /* Fill in data */
695         fd->inode = inode;
696         fd->mode = mode;
697         fd->disk = disk;
698
699         fd->fd.close = battfs_fileclose;
700         fd->fd.flush = battfs_flush;
701         fd->fd.read = battfs_read;
702         fd->fd.reopen = kfile_genericReopen;
703         fd->fd.seek = kfile_genericSeek;
704         fd->fd.write = battfs_write;
705
706 #warning TODO battfs_error, battfs_clearerr
707 #if 0
708         fd->fd.error = battfs_error;
709         fd->fd.clearerr = battfs_clearerr;
710 #endif
711
712         DB(fd->fd._type = KFT_BATTFS);
713
714         return true;
715 }
716
717 /**
718  * Close \a disk.
719  */
720 bool battfs_close(struct BattFsSuper *disk)
721 {
722         Node *n;
723         int res = 0;
724
725         /* Close all open files */
726         FOREACH_NODE(n, &disk->file_opened_list)
727         {
728                 BattFs *file = containerof(n, BattFs, link);
729                 res += battfs_fileclose(&file->fd);
730         }
731
732         /* Close disk */
733         return disk->close(disk) && (res == 0);
734 }
735
736 #if UNIT_TEST
737 bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff)
738 {
739         BattFsPageHeader hdr;
740
741         /* Reset page to all 0xff */
742         uint8_t buf[disk->page_size];
743         memset(buf, 0xFF, disk->page_size);
744         disk->bufferWrite(disk, 0, buf, disk->page_size);
745
746         hdr.inode = inode;
747         hdr.fill = fill;
748         hdr.pgoff = pgoff;
749         hdr.seq = seq;
750         hdr.fcs = computeFcs(&hdr);
751
752         if (!battfs_writeHeader(disk, page, &hdr))
753         {
754                 LOG_ERR("error writing hdr\n");
755                 return false;
756         }
757
758         return true;
759 }
760 #endif