Move kfile interface to the io/ directory.
[bertos.git] / bertos / fs / battfs.h
index afe69f5e74ea0aa84d74d632e2db97a0b18c29e9..d38b4d4aa459ef3620de7979e6b38b86f67e3f5b 100644 (file)
  *
  * \brief BattFS: a filesystem for embedded platforms (interface).
  * TODO: Add detailed filesystem description.
+ *
+ * $WIZ$ module_name = "battfs"
+ * $WIZ$ module_depends = "rotating_hash", "kfile"
+ * $WIZ$ module_configuration = "bertos/cfg/cfg_battfs.h"
  */
 
 #ifndef FS_BATTFS_H
 #include <cfg/compiler.h> // uintXX_t; STATIC_ASSERT
 #include <cpu/types.h> // CPU_BITS_PER_CHAR
 #include <algo/rotating_hash.h>
-#include <mware/list.h>
-#include <kern/kfile.h>
+#include <struct/list.h>
+#include <io/kfile.h>
+#include <io/kblock.h>
 
 typedef uint16_t fill_t;    ///< Type for keeping trace of space filled inside a page
 typedef fill_t   pgaddr_t;  ///< Type for addressing space inside a page
 typedef uint16_t pgcnt_t;   ///< Type for counting pages on disk
 typedef pgcnt_t  pgoff_t;   ///< Type for counting pages inside a file
-typedef uint32_t mark_t;    ///< Type for marking pages as free
 typedef uint8_t  inode_t;   ///< Type for file inodes
-typedef uint8_t  seq_t;     ///< Type for page seq number
+typedef uint64_t  seq_t;    ///< Type for page seq number, at least 40bits wide.
 typedef rotating_t fcs_t;   ///< Type for header FCS.
 
-/**
- * Size required for free block allocation is at least 1 bit more
- * than page addressing.
- */
-STATIC_ASSERT(sizeof(mark_t) > sizeof(pgcnt_t));
 
 /**
  * BattFS page header, used to represent a page
@@ -72,16 +71,18 @@ STATIC_ASSERT(sizeof(mark_t) > sizeof(pgcnt_t));
 typedef struct BattFsPageHeader
 {
        inode_t  inode; ///< File inode (file identifier).
-       seq_t    seq;   ///< Page sequence number.
        fill_t   fill;  ///< Filled bytes in page.
        pgoff_t  pgoff; ///< Page offset inside file.
-       mark_t   mark;  ///< Marker used to keep trace of free/used pages.
 
        /**
-        * FCS (Frame Check Sequence) of the page header once the page
-        * as been marked as free.
+        * Page sequence number.
+        * Every time a page is rewritten the seq number is
+        * increased by 1. seq_t is wide enough to not to perform
+        * a wrap around before the memory death.
+        * So it can be kept as it would be
+        * monotonically increasing for our needs.
         */
-       fcs_t fcs_free;
+       seq_t    seq;
 
        /**
         * FCS (Frame Check Sequence) of the page header.
@@ -96,22 +97,6 @@ typedef struct BattFsPageHeader
  */
 #define BATTFS_HEADER_LEN 12
 
-/**
- * Marks for valid pages.
- * Simply set to 1 all field bits.
- * \{
- */
-#define MARK_PAGE_VALID ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t) + 1)) - 1)
-#define FCS_FREE_VALID  ((1 << (CPU_BITS_PER_CHAR * sizeof(fcs_t))) - 1)
-/* \} */
-
-
-/**
- * Half-size of free page marker.
- * Used to keep trace of free marker wrap-arounds.
- */
-#define MARK_HALF_SIZE (1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t) + 1))
-
 /**
  * Maximum page address.
  */
@@ -122,49 +107,10 @@ typedef struct BattFsPageHeader
  */
 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t)))
 
-/* Fwd decl */
-struct BattFsSuper;
-
 /**
  * Sentinel used to keep trace of unset pages in disk->page_array.
  */
-#define PAGE_UNSET_SENTINEL ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)
-
-/**
- * Type interface for disk init function.
- * \return true if all is ok, false otherwise.
- */
-typedef bool (*disk_open_t) (struct BattFsSuper *d);
-
-/**
- * Type interface for disk page read function.
- * \a page is the page address, \a addr the address inside the page,
- * \a size the lenght to be read.
- * \return the number of bytes read.
- */
-typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
-
-/**
- * Type interface for disk page write function.
- * \a page is the page address, \a addr the address inside the page,
- * \a size the lenght to be written.
- * \return the number of bytes written.
- */
-typedef size_t (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t);
-
-/**
- * Type interface for disk page erase function.
- * \a page is the page address.
- * \return true if all is ok, false otherwise.
- */
-typedef bool (*disk_page_erase_t) (struct BattFsSuper *d, pgcnt_t page);
-
-/**
- * Type interface for disk deinit function.
- * \return true if all is ok, false otherwise.
- */
-typedef bool (*disk_close_t) (struct BattFsSuper *d);
-
+#define PAGE_UNSET_SENTINEL ((pgcnt_t)((1L << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1))
 
 typedef uint32_t disk_size_t; ///< Type for disk sizes.
 
@@ -175,15 +121,9 @@ typedef uint32_t disk_size_t; ///< Type for disk sizes.
  */
 typedef struct BattFsSuper
 {
-       disk_open_t open;        ///< Disk init.
-       disk_page_read_t  read;  ///< Page read.
-       disk_page_write_t write; ///< Page write.
-       disk_page_erase_t erase; ///< Page erase.
-       disk_close_t close;      ///< Disk deinit.
-
-       pgaddr_t page_size;      ///< Size of a disk page, in bytes.
-       pgcnt_t page_count;      ///< Number of pages on disk.
+       KBlock *dev;             ///< Block device context (physical disk).
 
+       pgaddr_t data_size;      ///< Size of space usable for data in a disk page, in bytes. The rest is used by the page header.
        /**
         * Page allocation array.
         * This array must be allocated somewhere and
@@ -193,17 +133,11 @@ typedef struct BattFsSuper
         */
        pgcnt_t *page_array;
 
-        /**
-        * Lowest free page counter.
-        * This is the counter of the first availble free page.
-        */
-       mark_t free_start;
-
-        /**
-        * Highest free page counter.
-        * This value is the next to be used to mark a block as free.
+       /**
+        * Lowest address, in page array, for free pages.
+        * Pages above this element are free for use.
         */
-       mark_t free_next;
+       pgcnt_t free_page_start;
 
        disk_size_t disk_size;   ///< Size of the disk, in bytes (page_count * page_size).
        disk_size_t free_bytes;  ///< Free space on the disk.
@@ -212,8 +146,13 @@ typedef struct BattFsSuper
        /* TODO add other fields. */
 } BattFsSuper;
 
-typedef uint8_t filemode_t; ///< Type for file open modes.
-typedef uint32_t file_size_t; ///< Type for file sizes.
+/**
+ * True if space on \a disk is over.
+ */
+#define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->dev->blk_cnt)
+
+typedef uint8_t filemode_t;  ///< Type for file open modes.
+typedef int32_t file_size_t; ///< Type for file sizes.
 
 /**
  * Modes for battfs_fileopen.
@@ -225,10 +164,22 @@ typedef uint32_t file_size_t; ///< Type for file sizes.
 /*/}*/
 
 
+/**
+ * File errors.
+ * \{
+ */
+#define BATTFS_NEGATIVE_SEEK_ERR   BV(0) ///< Trying to read/write before file start.
+#define BATTFS_DISK_READ_ERR       BV(1) ///< Error reading from disk device.
+#define BATTFS_DISK_WRITE_ERR      BV(2) ///< Error writing in the disk device.
+#define BATTFS_DISK_SPACEOVER_ERR  BV(3) ///< No more disk space available.
+#define BATTFS_DISK_FLUSHBUF_ERR   BV(4) ///< Error flushing (writing) the current page to disk.
+#define BATTFS_FILE_NOT_FOUND_ERR  BV(5) ///< File not found on disk.
+/*/}*/
+
 /**
  * Describe a BattFs file usign a KFile.
  */
-typedef struct BattFsKFile
+typedef struct BattFs
 {
        KFile fd;           ///< KFile context
        Node link;          ///< Link for inserting in opened file list
@@ -236,7 +187,9 @@ typedef struct BattFsKFile
        BattFsSuper *disk;  ///< Disk context
        filemode_t mode;    ///< File open mode
        pgcnt_t *start;     ///< Pointer to page_array file start position.
-} BattFsKFile;
+       pgcnt_t max_off;    ///< Max page offset allocated for the file.
+       int errors;         ///< File status/errors
+} BattFs;
 
 /**
  * Id for battfs file descriptors.
@@ -244,21 +197,22 @@ typedef struct BattFsKFile
 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S')
 
 /**
- * Macro used to cast a KFile to a BattFsKFile.
+ * Macro used to cast a KFile to a BattFS.
  * Also perform dynamic type check.
  */
-INLINE BattFsKFile * BATTFSKFILE(KFile *fd)
+INLINE BattFs * BATTFS_CAST(KFile *fd)
 {
        ASSERT(fd->_type == KFT_BATTFS);
-       return (BattFsKFile *)fd;
+       return (BattFs *)fd;
 }
 
-bool battfs_init(struct BattFsSuper *d);
-bool battfs_close(struct BattFsSuper *disk);
+bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size);
+bool battfs_fsck(struct BattFsSuper *disk);
+bool battfs_umount(struct BattFsSuper *disk);
 
 bool battfs_fileExists(BattFsSuper *disk, inode_t inode);
-bool battfs_fileopen(BattFsSuper *disk, BattFsKFile *fd, inode_t inode, filemode_t mode);
-
-bool battfs_writeTestBlock(struct BattFsSuper *disk, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff, mark_t mark);
+bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode);
 
+void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff);
+void battfs_eraseBlock(KBlock *dev, pgcnt_t page);
 #endif /* FS_BATTFS_H */