BeRTOS
|
00001 00044 #ifndef FS_BATTFS_H 00045 #define FS_BATTFS_H 00046 00047 #include <cfg/compiler.h> // uintXX_t; STATIC_ASSERT 00048 #include <cpu/types.h> // CPU_BITS_PER_CHAR 00049 #include <algo/rotating_hash.h> 00050 #include <struct/list.h> 00051 #include <io/kfile.h> 00052 #include <io/kblock.h> 00053 00054 typedef uint16_t fill_t; 00055 typedef fill_t pgaddr_t; 00056 typedef uint16_t pgcnt_t; 00057 typedef pgcnt_t pgoff_t; 00058 typedef uint8_t inode_t; 00059 typedef uint64_t seq_t; 00060 typedef rotating_t fcs_t; 00061 00062 00070 typedef struct BattFsPageHeader 00071 { 00072 inode_t inode; 00073 fill_t fill; 00074 pgoff_t pgoff; 00075 00084 seq_t seq; 00085 00089 fcs_t fcs; 00090 } BattFsPageHeader; 00091 00097 #define BATTFS_HEADER_LEN 12 00098 00102 #define MAX_PAGE_ADDR ((1 << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1) 00103 00107 #define BATTFS_MAX_FILES (1 << (CPU_BITS_PER_CHAR * sizeof(inode_t))) 00108 00112 #define PAGE_UNSET_SENTINEL ((pgcnt_t)((1L << (CPU_BITS_PER_CHAR * sizeof(pgcnt_t))) - 1)) 00113 00114 typedef uint32_t disk_size_t; 00115 00121 typedef struct BattFsSuper 00122 { 00123 KBlock *dev; 00124 00125 pgaddr_t data_size; 00126 00133 pgcnt_t *page_array; 00134 00139 pgcnt_t free_page_start; 00140 00141 disk_size_t disk_size; 00142 disk_size_t free_bytes; 00143 00144 List file_opened_list; 00145 /* TODO add other fields. */ 00146 } BattFsSuper; 00147 00151 #define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->dev->blk_cnt) 00152 00153 typedef uint8_t filemode_t; 00154 typedef int32_t file_size_t; 00155 00160 #define BATTFS_CREATE BV(0) ///< Create file if does not exist 00161 #define BATTFS_RD BV(1) ///< Open file for reading 00162 #define BATTFS_WR BV(2) ///< Open file fir writing 00163 /*/}*/ 00164 00165 00170 #define BATTFS_NEGATIVE_SEEK_ERR BV(0) ///< Trying to read/write before file start. 00171 #define BATTFS_DISK_READ_ERR BV(1) ///< Error reading from disk device. 00172 #define BATTFS_DISK_WRITE_ERR BV(2) ///< Error writing in the disk device. 00173 #define BATTFS_DISK_SPACEOVER_ERR BV(3) ///< No more disk space available. 00174 #define BATTFS_DISK_FLUSHBUF_ERR BV(4) ///< Error flushing (writing) the current page to disk. 00175 #define BATTFS_FILE_NOT_FOUND_ERR BV(5) ///< File not found on disk. 00176 /*/}*/ 00177 00181 typedef struct BattFs 00182 { 00183 KFile fd; 00184 Node link; 00185 inode_t inode; 00186 BattFsSuper *disk; 00187 filemode_t mode; 00188 pgcnt_t *start; 00189 pgcnt_t max_off; 00190 int errors; 00191 } BattFs; 00192 00196 #define KFT_BATTFS MAKE_ID('B', 'T', 'F', 'S') 00197 00202 INLINE BattFs * BATTFS_CAST(KFile *fd) 00203 { 00204 ASSERT(fd->_type == KFT_BATTFS); 00205 return (BattFs *)fd; 00206 } 00207 00208 bool battfs_mount(struct BattFsSuper *disk, struct KBlock *dev, pgcnt_t *page_array, size_t array_size); 00209 bool battfs_fsck(struct BattFsSuper *disk); 00210 bool battfs_umount(struct BattFsSuper *disk); 00211 00212 bool battfs_fileExists(BattFsSuper *disk, inode_t inode); 00213 bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mode); 00214 00215 void battfs_writeTestBlock(KBlock *dev, pgcnt_t page, inode_t inode, seq_t seq, fill_t fill, pgoff_t pgoff); 00216 void battfs_eraseBlock(KBlock *dev, pgcnt_t page); 00217 #endif /* FS_BATTFS_H */