battfs.c File Reference

BattFS: a filesystem for embedded platforms (implementation). More...

#include "battfs.h"
#include <cfg/debug.h>
#include <cfg/macros.h>
#include <mware/byteorder.h>
#include <string.h>

Go to the source code of this file.


Functions

void battfs_to_disk (struct BattFsPageHeader *hdr, uint8_t *buf)
 Convert from memory representation to disk structure.
void disk_to_battfs (uint8_t *buf, struct BattFsPageHeader *hdr)
 Convert from disk structure to memory representation.
static fcs_t computeFcs (struct BattFsPageHeader *hdr)
 Compute the fcs of the header.
static fcs_t computeFcsFree (struct BattFsPageHeader *hdr)
 Compute the fcs of the header marked as free.
static bool battfs_readHeader (struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
 Read header of page page.
static bool battfs_writeHeader (struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
 Write header of page page.
static pgcnt_t countPages (pgoff_t *filelen_table, inode_t inode)
 Count the number of pages from inode 0 to inode in filelen_table.
static void movePages (struct BattFsSuper *disk, pgcnt_t src, int offset)
 Move all pages in page allocation array from src to src + offset.
static void insertFreePage (struct BattFsSuper *disk, mark_t mark, pgcnt_t page)
 Insert page into page allocation array of disk, using mark to compute position.
static bool battfs_markFree (struct BattFsSuper *disk, struct BattFsPageHeader *hdr, pgcnt_t page)
 Mark page of disk as free.
static void findFreeStartNext (struct BattFsSuper *disk, mark_t minl, mark_t maxl, mark_t minh, mark_t maxh)
 Determine free_start and free_next blocks for disk using minl, maxl, minh, maxh.
static bool countDiskFilePages (struct BattFsSuper *disk, pgoff_t *filelen_table)
 Count number of pages per file on disk.
static bool fillPageArray (struct BattFsSuper *disk, pgoff_t *filelen_table)
 Fill page allocation array of disk using file lenghts in filelen_table.
bool battfs_init (struct BattFsSuper *disk)
 Initialize and mount disk described by disk.
static int battfs_flush (struct KFile *fd)
 Flush file fd.
static int battfs_fileclose (struct KFile *fd)
 Close file fd.
static size_t battfs_read (struct KFile *fd, void *_buf, size_t size)
 Read from file fd size bytes in buf.
static pgcnt_tfindFile (BattFsSuper *disk, inode_t inode)
 Search file inode in disk using a binary search.
bool battfs_fileExists (BattFsSuper *disk, inode_t inode)
static bool countFileSize (BattFsSuper *disk, pgcnt_t *start, inode_t inode, file_size_t *size)
 Count size of file inode on disk, starting at pointer start in disk->page_array.
bool battfs_fileopen (BattFsSuper *disk, KFileBattFs *fd, inode_t inode, filemode_t mode)
 Open file inode from disk in mode.
bool battfs_close (struct BattFsSuper *disk)
 Close disk.

Detailed Description

BattFS: a filesystem for embedded platforms (implementation).

Version:
$Id:$
Author:
Francesco Sacchi <batt@develer.com>

Definition in file battfs.c.


Function Documentation

static int battfs_fileclose ( struct KFile fd  )  [static]

Close file fd.

Returns:
0 if ok, EOF on errors.

Definition at line 608 of file battfs.c.

bool battfs_fileExists ( BattFsSuper disk,
inode_t  inode 
)

Returns:
true if file inode exists on disk, false otherwise.

Definition at line 687 of file battfs.c.

bool battfs_fileopen ( BattFsSuper disk,
KFileBattFs fd,
inode_t  inode,
filemode_t  mode 
)

Open file inode from disk in mode.

File context is stored in fd.

Returns:
true if ok, false otherwise.

Definition at line 718 of file battfs.c.

static int battfs_flush ( struct KFile fd  )  [static]

Flush file fd.

Returns:
0 if ok, EOF on errors.

Definition at line 597 of file battfs.c.

bool battfs_init ( struct BattFsSuper disk  ) 

Initialize and mount disk described by disk.

Returns:
false on errors, true otherwise.

Definition at line 539 of file battfs.c.

static bool battfs_markFree ( struct BattFsSuper disk,
struct BattFsPageHeader hdr,
pgcnt_t  page 
) [static]

Mark page of disk as free.

Note:
free_next of disk is used as page free marker and is increased by 1.

Definition at line 248 of file battfs.c.

static size_t battfs_read ( struct KFile fd,
void *  _buf,
size_t  size 
) [static]

Read from file fd size bytes in buf.

Returns:
The number of bytes read.

Definition at line 621 of file battfs.c.

static bool battfs_readHeader ( struct BattFsSuper disk,
pgcnt_t  page,
struct BattFsPageHeader hdr 
) [static]

Read header of page page.

Returns:
true on success, false otherwise.
Note:
hdr is dirtyed even on errors.

Definition at line 147 of file battfs.c.

void battfs_to_disk ( struct BattFsPageHeader hdr,
uint8_t *  buf 
) [inline]

Convert from memory representation to disk structure.

Note:
filesystem is in little-endian format.

Definition at line 54 of file battfs.c.

static bool battfs_writeHeader ( struct BattFsSuper disk,
pgcnt_t  page,
struct BattFsPageHeader hdr 
) [static]

Write header of page page.

Returns:
true on success, false otherwise.
Note:
hdr is dirtyed even on errors.

Definition at line 173 of file battfs.c.

static bool countDiskFilePages ( struct BattFsSuper disk,
pgoff_t filelen_table 
) [static]

Count number of pages per file on disk.

This information is registered in filelen_table. Array index represent file inode, while value contained is the number of pages used by that file.

Returns:
true if ok, false on disk read errors.
Note:
The whole disk is scanned once.

Definition at line 364 of file battfs.c.

static bool countFileSize ( BattFsSuper disk,
pgcnt_t start,
inode_t  inode,
file_size_t size 
) [static]

Count size of file inode on disk, starting at pointer start in disk->page_array.

Size is written in size.

Returns:
true if all s ok, false on disk read errors.

Definition at line 697 of file battfs.c.

void disk_to_battfs ( uint8_t *  buf,
struct BattFsPageHeader hdr 
) [inline]

Convert from disk structure to memory representation.

Note:
filesystem is in little-endian format.

Definition at line 99 of file battfs.c.

static bool fillPageArray ( struct BattFsSuper disk,
pgoff_t filelen_table 
) [static]

Fill page allocation array of disk using file lenghts in filelen_table.

The page allocation array is an array containings all file infos. Is ordered by file, and within each file is ordered by page offset inside file. e.g. : at page array[0] you will find page address of the first page of the first file (if present). Free blocks are allocated after the last file, starting from invalid ones and continuing with the marked free ones.

Returns:
true if ok, false on disk read errors.
Note:
The whole disk is scanned once.

Definition at line 441 of file battfs.c.

static pgcnt_t* findFile ( BattFsSuper disk,
inode_t  inode 
) [static]

Search file inode in disk using a binary search.

Returns:
pointer to file start in disk->page_array if file exists, NULL otherwise.

Definition at line 659 of file battfs.c.

static void findFreeStartNext ( struct BattFsSuper disk,
mark_t  minl,
mark_t  maxl,
mark_t  minh,
mark_t  maxh 
) [static]

Determine free_start and free_next blocks for disk using minl, maxl, minh, maxh.

Mark_t is a type that has at least 1 bit more than pgaddr_t. So all free blocks can be numbered using at most half numbers of a mark_t type. The free blocks algorithm increments by 1 the disk->free_next every time a page becomes free. So the free block sequence is guaranteed to be countiguous. Only wrap arounds may happen, but due to half size sequence limitation, there are only 4 possible situations:

 *    |------lower half------|-------upper half-------|
 *
 * 1) |------minl*****maxl---|------------------------|
 * 2) |------minl********maxl|minh******maxh----------|
 * 3) |----------------------|----minh*******maxh-----|
 * 4) |minl******maxl--------|------------minh****maxh|
 * 

Situations 1 and 3 are easy to detect, while 2 and 4 require more care.

Definition at line 292 of file battfs.c.

static void movePages ( struct BattFsSuper disk,
pgcnt_t  src,
int  offset 
) [static]

Move all pages in page allocation array from src to src + offset.

The number of pages moved is page_count - MAX(dst, src).

Definition at line 212 of file battfs.c.