📄 core.h
字号:
/******************************************************************************
* Flash File System (ffs)
* Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
*
* ffs core functions
*
* $Id: core.h,v 1.1.1.1 2004/06/19 06:00:30 root Exp $
*
******************************************************************************/
#include "rv_defined_swe.h"
/******************************************************************************
* Compile option switches
******************************************************************************/
// FFS compiled with extra test functionality
#define FFS_TEST 1
// Default size of working buffer used by stream i/o functions
#define FFS_FD_BUF_SIZE 8192
// Default max number of simultaneous open files
#ifdef RVM_MSFE_SWE
#define FFS_FD_MAX 20
#else
#define FFS_FD_MAX 4
#endif
#define FFS_RECLAIM_NEW 1
/******************************************************************************
* Compile constants
******************************************************************************/
// FFS API version (in four-digit BCD format)
#define FFS_API_VERSION ((uint16) 0x0641)
// FFS_DRV_VERSION is in drv.h
// TMFFS protocol version is in tmffs.h
// Magic for determining (formatted) file system version. First two digits
// represent major version, bottom two digits represent minor version. An
// ffs code compiled for one major version X is compatible with any other
// format version with major = X. Minor version is incremented when adding
// new features that does not break compatibility.
#define FFS_FORMAT_VERSION (0x0210)
#define BLOCK_MAGIC_LOW ('f'<<8|'F') // "Ffs#"
#define BLOCK_MAGIC_HIGH ('#'<<8|'s')
#define BLOCK_MAGIC ((BLOCK_MAGIC_HIGH << 8)|(BLOCK_MAGIC_LOW))
// Absolute maximum number of inodes allowed
#define FFS_INODES_MAX 2048
// Default maximum number of inodes allowed
#define FFS_INODES_MAX_DEFAULT 1024
// Default number of path components (limit due to recursiveness of
// inodes_reclaim())
#define FFS_PATH_DEPTH_MAX 6
// Maximum number of blocks (flash sectors) in a ffs system. FFS_BLOCKS_MAX
// must be >= the number of blocks in the largest flash device memory
// map. It is used to allocate the number of entries in the static bstat
// array.
#define FFS_BLOCKS_MAX 128
// Default size of journal file (represented as 256'ths of the blocksize)
#define FFS_JOURNAL_SIZE_IN256THS 16 // one 16'ths of the block size.
// Without the min size will the maximum of files (fs.blocks_files_max) in
// one block be 32 files if the blocksize is 8kB!
#define FFS_JOURNAL_SIZE_MIN 1024
#define FFS_JOURNAL_NAME ".journal"
// Default max size of file name (excluding null terminator)
#define FFS_FILENAME_MAX 20
// Maximum distance in age between youngest and oldest blocks
#define FFS_DAGE_MAX 256
#define FFS_DAGE_GAIN_MIN (FFS_DAGE_MAX / 4)
#define FFS_DAGE_EARLY_WIDTH 64
// Offset on file descriptors
#define FFS_FD_OFFSET '1'
// Macros to set flags and test bits in flash memory words (negative logic)
#define BIT_SET(value, bits) ((value) & (~bits))
#define IS_BIT_SET(value, bits) (~(value) & (bits))
// Number of free inodes and journal entries to keep for "emergencies"
#define FFS_INODES_MARGIN 4
#define FFS_JOURNAL_MARGIN 4
/******************************************************************************
* Macros used in both drv.c and core.c
******************************************************************************/
// Convert a offset_t value to a block index
#define offset2block(offset) (((uint32) offset) >> dev.binfo[0].size_ld)
// Convert between offset and address
#define offset2addr(offset) (dev.base + (offset))
// Size of a block
#define blocksize(block) (1 << dev.binfo[block].size_ld)
// Test if flag is set
#define is_open_option(options, flags) ((options & flags) == flags)
// Amount of reserved space.
#define RESERVED_LOW 2 * fs.journal_size
#define RESERVED_NONE 0
// We have to saturate because a recently reclaimed inodes block could
// theoretically possess a high age
#define saturate_dage(dage) (dage > (2*FFS_DAGE_MAX) ? (2*FFS_DAGE_MAX) : dage)
/******************************************************************************
* External declarations
******************************************************************************/
extern struct fs_s fs;
extern struct block_stat_s bstat[FFS_BLOCKS_MAX];
extern struct ffs_stats_s stats;
extern const struct block_info_s *binfo;
/******************************************************************************
* Block Types
******************************************************************************/
// Block age, ie. number of times block has been erased
typedef uint16 age_t;
// Maximum age a block can have
#define BLOCK_AGE_MAX 0xFFFF
// ffs block status flags. These are stored in the first 2 bytes of
// the ffs block in the flash sector.
enum BLOCK_FLAGS {
BF_LOST = 0x80, // block is lost and will soon be erased
BF_FREE = 0x40, // free (preformatted and with block magic)
BF_DATA = 0x02, // data
BF_CLEANING = 0x01, // block is being cleaned
BF_INODES = 0x10, // block contains inodes
BF_COPYING = 0x04 // block is a coming inodes block
};
enum BLOCK_STATES {
BF_IS_EMPTY = ~(0),
BF_IS_FREE = ~(BF_FREE),
BF_IS_DATA = ~(BF_FREE | BF_DATA),
BF_IS_CLEANING = ~(BF_FREE | BF_DATA | BF_CLEANING),
BF_IS_COPYING = ~(BF_FREE | BF_COPYING),
BF_IS_INODES = ~(BF_FREE | BF_COPYING | BF_INODES),
BF_IS_INODES_LOST = ~(BF_FREE | BF_COPYING | BF_INODES | BF_LOST)
};
// Header of each FFS block
struct block_header_s {
uint16 magic_low; // 32-bit magic number
uint16 magic_high;
uint16 version; // FFS_FORMAT_VERSION used for formatting
age_t age; // number of times this block has been erased
uint16 flags; // status flags of this block (BLOCK_FLAGS)
uint16 reserved0;
uint16 reserved1;
uint16 reserved2;
};
// Important the below define MUST fit to the size of the header that is written
#define BHEADER_SIZE sizeof(struct block_header_s)
#define OLD_BLOCK_MAGIC_LOW ('S'<<8|'F') // "FS"
#define OLD_FFS_FORMAT_VERSION (0x0100) // 1.00 (in four-digit BCD format)
// Old header of each FFS block. From old/previous FFS format version
struct block_header_old_s {
uint8 flags;
uint8 copied;
uint8 magicflags;
uint8 reserved0;
uint16 magic_low;
uint16 magic_high;
uint16 reserved1;
uint16 reserved2;
};
// Block status. This struct holds the status of one ffs block This relation
// is always valid: <block size> = <used> + <lost> + <free>. The block size
// is obtained from the corresponding block_info structure. <used> and
// <lost> variables always holds a value which is a multiple of
// FFS_GRANULARITY. For inodes, <used> is number of inodes in active use,
// <lost> is number of deleted/lost inodes, <numfiles> is the index of the
// first free inode.
struct block_stat_s {
blocksize_t used; // number of used bytes
blocksize_t lost; // number of lost bytes
uint16 flags; // flash block flags (first 16 bits of each block)
uint16 objects; // number of valid objects
};
/******************************************************************************
* Object Types
******************************************************************************/
// This enum MUST be in sync with the one in ffs.h.
enum OBJECT_TYPE_E {
// remaining filetypes are in ffs.h
OT_ERASED = 0,
OT_NULL = 7,
OT_MASK = 7,
OT_MAX = 4
};
// This enum MUST be in sync with the one in ffs.h.
enum OBJECT_FLAGS_E {
// remaining object flags are in ffs.h
OF_UNDEF0 = 1<<5,
OF_UNDEF1 = 1<<6,
OF_EXACT = 1<<7, // used by control()/update_commit() interaction. This
// is *not* an object flag!
OF_ALL = OF_READONLY, // all flags allowed to be changed by user
OF_MASK = 0xF0
};
struct inode_s {
uint16 size;
uint8 reserved; // size extension?
objflags_t flags;
iref_t child; // link to first inode in dir (this inode is a dir)
iref_t sibling; // link to next inode in same directory
location_t location; // location of object
uint16 sequence; //
uint16 updates; // times this object has been updated
};
struct file_descriptor_s {
char *buf; // Write buffer
iref_t seghead; // First chunk. Contain file name and optional data
iref_t wch; // Inode of work chunk (if chunk is read to buf)
int fp; // File pointer
int wfp; // Work file pointer always points to start of wch
int size; // Size of object (all chunks and data from buf)
int8 options; // Open options
int dirty; // Indicate if buf contain valid data or not
};
/******************************************************************************
* Journal types and global fs structure
******************************************************************************/
enum JOURNAL_FLAGS {
JOURNAL_WRITING = 0x02, // journal is being written to journal file
JOURNAL_READY = 0x04, // journal has been written to journal file
JOURNAL_DONE = 0x08 // journal has been written to ffs
};
enum JOURNAL_STATES {
JOURNAL_IS_EMPTY = ~(0),
JOURNAL_IS_WRITING = ~(JOURNAL_WRITING),
JOURNAL_IS_READY = ~(JOURNAL_WRITING | JOURNAL_READY),
JOURNAL_IS_DONE = ~(JOURNAL_WRITING | JOURNAL_READY | JOURNAL_DONE)
};
// Journal entry structure. Note that the state byte *MUST* be the first
// byte of the structure!
struct journal_s {
uint8 state; // state of journal entry.
objflags_t flags; // type of object
iref_t i; // iref of object
iref_t diri; // iref of object that is this object's parent/sibling
iref_t oldi; // iref of object being replaced (only for updates)
location_t location; // object's location
uint16 size; // object's size
iref_t repli; // inode which is replaced
};
// Main ffs info struct (initialised by ffs_initialize())
struct fs_s {
struct inode_s *inodes_addr; // base address of inodes
iref_t root; // iref of root directory
bref_t inodes; // index into bstat containing inode block
bref_t newinodes; // index into bstat containing new inode block
bref_t blocks_free_min; // Number of spare blocks (0 or 1)
int filesize_max; // Max size of object data
int reserved_space; // Byte size of space reserved for journal relocation
iref_t inodes_max; // Max number of inodes possible
iref_t inodes_high; // number of inodes triggering an inodes_reclaim()
iref_t objects_max; // Max number of objects (valid inodes) allowed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -