Most of packing logic is in place
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3556 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
parent
109fa7d14e
commit
b7e1a99025
|
@ -170,6 +170,14 @@
|
|||
|
||||
#define NXFFS_MAGICSIZE 4
|
||||
|
||||
/* When we allocate FLASH for a new inode data block, we will require that
|
||||
* space is available to hold this minimum number of data bytes in addition
|
||||
* to the size of the data block headeer.
|
||||
*/
|
||||
|
||||
#define NXFFS_MINDATA 16
|
||||
|
||||
|
||||
/* Internal definitions *****************************************************/
|
||||
/* If we encounter this number of erased bytes, we assume that all of the
|
||||
* flash beyond this point is erased.
|
||||
|
@ -180,11 +188,11 @@
|
|||
/* Quasi-standard definitions */
|
||||
|
||||
#ifndef MIN
|
||||
# define MIN(a,b) (a < b ? a : b)
|
||||
# define MIN(a,b) (a < b ? a : b)
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
# define MAX(a,b) (a > b ? a : b)
|
||||
# define MAX(a,b) (a > b ? a : b)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -742,6 +750,29 @@ extern int nxffs_reformat(FAR struct nxffs_volume_s *volume);
|
|||
extern FAR struct nxffs_ofile_s *nxffs_findofile(FAR struct nxffs_volume_s *volume,
|
||||
FAR const char *name);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_wrinode
|
||||
*
|
||||
* Description:
|
||||
* Write the inode header and inode file name to FLASH. This is done in
|
||||
* two contexts:
|
||||
*
|
||||
* 1. When an inode is closed, or
|
||||
* 2. As part of the file system packing logic when an inode is moved.
|
||||
*
|
||||
* Input parameters
|
||||
* volume - Describes the NXFFS volume
|
||||
* entry - Describes the indoe header to write
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success; Otherwise, a negated errno value is returned
|
||||
* indicating the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
extern int nxffs_wrinode(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_entry_s *entry);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_wrreserve
|
||||
*
|
||||
|
|
|
@ -418,7 +418,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
|
|||
/* Make sure that the length of the file name will fit in a uint8_t */
|
||||
|
||||
namlen = strlen(name);
|
||||
if (namlen > UINT8_MAX)
|
||||
if (namlen > CONFIG_NXFFS_MAXNAMLEN)
|
||||
{
|
||||
fdbg("Name is too long: %d\n", namlen);
|
||||
ret = -EINVAL;
|
||||
|
@ -785,14 +785,9 @@ static inline void nxffs_freeofile(FAR struct nxffs_volume_s *volume,
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxffs_wrclose(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_wrfile_s *wrfile)
|
||||
static inline int nxffs_wrclose(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_wrfile_s *wrfile)
|
||||
{
|
||||
FAR struct nxffs_inode_s *inode;
|
||||
off_t namblock;
|
||||
uint16_t namoffset;
|
||||
uint32_t crc;
|
||||
int namlen;
|
||||
int ret;
|
||||
|
||||
/* Is there an unfinalized write data? */
|
||||
|
@ -828,96 +823,10 @@ static int nxffs_wrclose(FAR struct nxffs_volume_s *volume,
|
|||
}
|
||||
}
|
||||
|
||||
/* Write the inode header to FLASH. First get the block where we will
|
||||
* write the file name.
|
||||
*/
|
||||
/* Write the inode header to FLASH */
|
||||
|
||||
nxffs_ioseek(volume, wrfile->ofile.entry.noffset);
|
||||
namblock = volume->ioblock;
|
||||
namoffset = volume->iooffset;
|
||||
ret = nxffs_wrinode(volume, &wrfile->ofile.entry);
|
||||
|
||||
/* Now seek to the inode header position and assure that it is in the
|
||||
* volume cache.
|
||||
*/
|
||||
|
||||
nxffs_ioseek(volume, wrfile->ofile.entry.hoffset);
|
||||
ret = nxffs_rdcache(volume, volume->ioblock);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to read inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Get the length of the inode name */
|
||||
|
||||
namlen = strlen(wrfile->ofile.entry.name);
|
||||
DEBUGASSERT(namlen < UINT8_MAX); /* This was verified earlier */
|
||||
|
||||
/* Initialize the inode header */
|
||||
|
||||
inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
|
||||
memcpy(inode->magic, g_inodemagic, NXFFS_MAGICSIZE);
|
||||
|
||||
inode->state = CONFIG_NXFFS_ERASEDSTATE;
|
||||
inode->namlen = namlen;
|
||||
|
||||
nxffs_wrle32(inode->noffs, wrfile->ofile.entry.noffset);
|
||||
nxffs_wrle32(inode->doffs, wrfile->ofile.entry.doffset);
|
||||
nxffs_wrle32(inode->utc, wrfile->ofile.entry.utc);
|
||||
nxffs_wrle32(inode->crc, 0);
|
||||
nxffs_wrle32(inode->datlen, wrfile->ofile.entry.datlen);
|
||||
|
||||
/* Calculate the CRC */
|
||||
|
||||
crc = crc32((FAR const uint8_t *)inode, SIZEOF_NXFFS_INODE_HDR);
|
||||
crc = crc32part((FAR const uint8_t *)wrfile->ofile.entry.name, namlen, crc);
|
||||
|
||||
/* Finish the inode header */
|
||||
|
||||
inode->state = INODE_STATE_FILE;
|
||||
nxffs_wrle32(inode->crc, crc);
|
||||
|
||||
/* Are the inode header and the inode name in the same block? Normally,
|
||||
* they will be in the same block. However, they could potentially be
|
||||
* far apart due to intervening bad blocks.
|
||||
*/
|
||||
|
||||
if (volume->ioblock != namblock)
|
||||
{
|
||||
/* Write the block with the inode header */
|
||||
|
||||
ret = nxffs_wrcache(volume);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to write inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure the that block containing the inode name is in the cache */
|
||||
|
||||
volume->ioblock = namblock;
|
||||
volume->iooffset = namoffset;
|
||||
ret = nxffs_rdcache(volume, volume->ioblock);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to read inode name block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
/* Finally, copy the inode name to the cache and write the inode name block */
|
||||
|
||||
memcpy(&volume->cache[namoffset], wrfile->ofile.entry.name, namlen);
|
||||
ret = nxffs_wrcache(volume);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to write inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
}
|
||||
|
||||
/* The volume is now available for other writers */
|
||||
|
||||
errout:
|
||||
|
@ -1114,3 +1023,131 @@ int nxffs_close(FAR struct file *filep)
|
|||
errout:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_wrinode
|
||||
*
|
||||
* Description:
|
||||
* Write the inode header and inode file name to FLASH. This is done in
|
||||
* two contexts:
|
||||
*
|
||||
* 1. When an inode is closed, or
|
||||
* 2. As part of the file system packing logic when an inode is moved.
|
||||
*
|
||||
* Input parameters
|
||||
* volume - Describes the NXFFS volume
|
||||
* entry - Describes the indoe header to write
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success; Otherwise, a negated errno value is returned
|
||||
* indicating the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nxffs_wrinode(FAR struct nxffs_volume_s *volume, FAR struct nxffs_entry_s *entry)
|
||||
{
|
||||
FAR struct nxffs_inode_s *inode;
|
||||
off_t namblock;
|
||||
uint16_t namoffset;
|
||||
uint32_t crc;
|
||||
int namlen;
|
||||
int ret;
|
||||
|
||||
/* Write the inode header to FLASH. First get the block where we will
|
||||
* write the file name.
|
||||
*/
|
||||
|
||||
nxffs_ioseek(volume, entry->noffset);
|
||||
namblock = volume->ioblock;
|
||||
namoffset = volume->iooffset;
|
||||
|
||||
/* Now seek to the inode header position and assure that it is in the
|
||||
* volume cache.
|
||||
*/
|
||||
|
||||
nxffs_ioseek(volume, entry->hoffset);
|
||||
ret = nxffs_rdcache(volume, volume->ioblock);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to read inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Get the length of the inode name */
|
||||
|
||||
namlen = strlen(entry->name);
|
||||
DEBUGASSERT(namlen < CONFIG_NXFFS_MAXNAMLEN); /* This was verified earlier */
|
||||
|
||||
/* Initialize the inode header */
|
||||
|
||||
inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
|
||||
memcpy(inode->magic, g_inodemagic, NXFFS_MAGICSIZE);
|
||||
|
||||
inode->state = CONFIG_NXFFS_ERASEDSTATE;
|
||||
inode->namlen = namlen;
|
||||
|
||||
nxffs_wrle32(inode->noffs, entry->noffset);
|
||||
nxffs_wrle32(inode->doffs, entry->doffset);
|
||||
nxffs_wrle32(inode->utc, entry->utc);
|
||||
nxffs_wrle32(inode->crc, 0);
|
||||
nxffs_wrle32(inode->datlen, entry->datlen);
|
||||
|
||||
/* Calculate the CRC */
|
||||
|
||||
crc = crc32((FAR const uint8_t *)inode, SIZEOF_NXFFS_INODE_HDR);
|
||||
crc = crc32part((FAR const uint8_t *)entry->name, namlen, crc);
|
||||
|
||||
/* Finish the inode header */
|
||||
|
||||
inode->state = INODE_STATE_FILE;
|
||||
nxffs_wrle32(inode->crc, crc);
|
||||
|
||||
/* Are the inode header and the inode name in the same block? Normally,
|
||||
* they will be in the same block. However, they could potentially be
|
||||
* far apart due to intervening bad blocks.
|
||||
*/
|
||||
|
||||
if (volume->ioblock != namblock)
|
||||
{
|
||||
/* Write the block with the inode header */
|
||||
|
||||
ret = nxffs_wrcache(volume);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to write inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Make sure the that block containing the inode name is in the cache */
|
||||
|
||||
volume->ioblock = namblock;
|
||||
volume->iooffset = namoffset;
|
||||
ret = nxffs_rdcache(volume, volume->ioblock);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to read inode name block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
/* Finally, copy the inode name to the cache and write the inode name block */
|
||||
|
||||
memcpy(&volume->cache[namoffset], entry->name, namlen);
|
||||
ret = nxffs_wrcache(volume);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to write inode header block %d: %d\n",
|
||||
volume->ioblock, -ret);
|
||||
}
|
||||
|
||||
/* The volume is now available for other writers */
|
||||
|
||||
errout:
|
||||
sem_post(&volume->wrsem);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -44,8 +44,11 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <crc32.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
|
||||
#include "nxffs.h"
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -59,7 +62,7 @@
|
|||
|
||||
struct nxffs_packstream_s
|
||||
{
|
||||
struct nxffs_entry_s entry; /* Described the inode header */
|
||||
struct nxffs_entry_s entry; /* Describes the inode header */
|
||||
off_t fpos; /* Current file position */
|
||||
off_t blkoffset; /* Offset to the current data block */
|
||||
uint16_t blklen; /* Size of this block */
|
||||
|
@ -81,6 +84,7 @@ struct nxffs_pack_s
|
|||
|
||||
FAR uint8_t *iobuffer; /* I/O block start position */
|
||||
off_t ioblock; /* I/O block number */
|
||||
off_t block0; /* First I/O Block number in the erase block */
|
||||
uint16_t iooffset; /* I/O block offset */
|
||||
};
|
||||
|
||||
|
@ -93,26 +97,66 @@ struct nxffs_pack_s
|
|||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_packseek
|
||||
* Name: nxffs_getblock
|
||||
*
|
||||
* Description:
|
||||
* Seek to the destination offset in the pack buffer
|
||||
* Return the I/O block number that includes the provided offset.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - The volume to be packed.
|
||||
* pack - The volume packing state structure.
|
||||
* offset - The desired offset
|
||||
* volume - Describes the NXFFS volume
|
||||
* offset - FLASH offset
|
||||
*
|
||||
* Returned Values:
|
||||
* None
|
||||
* Returned Value:
|
||||
* The I/O block number.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void nxffs_packseek(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack, off_t offset)
|
||||
static off_t nxffs_getblock(FAR struct nxffs_volume_s *volume, off_t offset)
|
||||
{
|
||||
pack->ioblock = offset / volume->geo.blocksize;
|
||||
pack->iooffset = offset - pack->ioblock * volume->geo.blocksize;
|
||||
return offset / volume->geo.blocksize;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_getoffset
|
||||
*
|
||||
* Description:
|
||||
* Given an I/O block number return the block offset corresponding to the
|
||||
* FLASH offset;
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - Describes the NXFFS volume
|
||||
* offset - FLASH offset
|
||||
*
|
||||
* Returned Value:
|
||||
* The I/O block number.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static off_t nxffs_getoffset(FAR struct nxffs_volume_s *volume,
|
||||
off_t offset, off_t block)
|
||||
{
|
||||
return offset - block * volume->geo.blocksize;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_packtell
|
||||
*
|
||||
* Description:
|
||||
* Report the current destination position in the pack buffer.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - Describes the NXFFS volume
|
||||
* pack - The volume packing state structure.
|
||||
*
|
||||
* Returned Value:
|
||||
* The offset from the beginning of FLASH to the current seek position.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static off_t nxffs_packtell(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack)
|
||||
{
|
||||
return pack->ioblock * volume->geo.blocksize + pack->iooffset;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -198,7 +242,7 @@ static inline off_t nxffs_mediacheck(FAR struct nxffs_volume_s *volume,
|
|||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_startblock
|
||||
* Name: nxffs_startpos
|
||||
*
|
||||
* Description:
|
||||
* Find the position in FLASH memory where we should begin packing. That
|
||||
|
@ -218,9 +262,9 @@ static inline off_t nxffs_mediacheck(FAR struct nxffs_volume_s *volume,
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
static inline int nxffs_startblock(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack,
|
||||
off_t offset)
|
||||
static inline int nxffs_startpos(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack,
|
||||
off_t offset)
|
||||
{
|
||||
struct nxffs_blkentry_s blkentry;
|
||||
off_t wasted;
|
||||
|
@ -244,11 +288,17 @@ static inline int nxffs_startblock(FAR struct nxffs_volume_s *volume,
|
|||
wasted = pack->src.entry.hoffset - offset;
|
||||
if (wasted > CONFIG_NXFFS_PACKTHRESHOLD)
|
||||
{
|
||||
/* This is where we must begin packing */
|
||||
/* This is where we must begin packing. Describe the destination
|
||||
* inode header (only non-zero entries need to be initialized).
|
||||
*/
|
||||
|
||||
memcpy(&pack->dest.entry, &pack->src.entry, sizeof(struct nxffs_entry_s));
|
||||
|
||||
pack->dest.entry.hoffset = offset;
|
||||
pack->dest.entry.name = pack->src.entry.name;
|
||||
pack->dest.entry.utc = pack->src.entry.utc;
|
||||
pack->dest.entry.datlen = pack->src.entry.datlen;
|
||||
|
||||
/* The destination entry now "owns" the name string */
|
||||
|
||||
pack->src.entry.name = NULL;
|
||||
return OK;
|
||||
}
|
||||
|
@ -323,6 +373,319 @@ static inline int nxffs_startblock(FAR struct nxffs_volume_s *volume,
|
|||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_srcsetup
|
||||
*
|
||||
* Description:
|
||||
* Given a valid src inode, configure the src data stream.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - The volume to be packed
|
||||
* pack - The volume packing state structure.
|
||||
* offset - FLASH offset to the data block header
|
||||
*
|
||||
* Returned Values:
|
||||
* Zero on success; Otherwise, a negated errno value is returned to
|
||||
* indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxffs_srcsetup(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack, off_t offset)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* No, start with the first data block */
|
||||
|
||||
pack->src.blkoffset = offset;
|
||||
pack->src.blkpos = 0;
|
||||
|
||||
/* Seek to the data block header, read and verify the block header */
|
||||
|
||||
ret = nxffs_rdblkhdr(volume, offset, &pack->src.blklen);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to verify the data block header: %d\n", -ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_destsetup
|
||||
*
|
||||
* Description:
|
||||
* Given a valid dest inode, configure the dest data stream.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - The volume to be packed
|
||||
* pack - The volume packing state structure.
|
||||
*
|
||||
* Returned Values:
|
||||
* Zero on success; Otherwise, a negated errno value is returned to
|
||||
* indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxffs_destsetup(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack)
|
||||
{
|
||||
size_t mindata;
|
||||
int namlen;
|
||||
|
||||
/* The destination can be in one of three of states:
|
||||
*
|
||||
* State 1: The inode position was not yet been found. This condition can
|
||||
* only occur on initial entry into nxffs_packblock() when there we no space
|
||||
* for the inode header at the end of the previous block. We must now be
|
||||
* at the beginning of a shiny new I/O block, so we should always have
|
||||
* space for a new inode header right here.
|
||||
*/
|
||||
|
||||
if (pack->dest.entry.hoffset == 0)
|
||||
{
|
||||
DEBUGASSERT(pack->iooffset + SIZEOF_NXFFS_INODE_HDR <= volume->geo.blocksize);
|
||||
pack->dest.entry.hoffset = nxffs_packtell(volume, pack);
|
||||
pack->iooffset += SIZEOF_NXFFS_INODE_HDR;
|
||||
}
|
||||
|
||||
/* State 2: inode position found, inode header not written, inode name
|
||||
* position not determined.
|
||||
*/
|
||||
|
||||
if (pack->dest.entry.noffset == 0)
|
||||
{
|
||||
/* Find the offset to the string memory. Will if fit in this block?
|
||||
* Note: iooffset has already been incremented to account for the
|
||||
* size of the inode header.
|
||||
*/
|
||||
|
||||
namlen = strlen(pack->dest.entry.name);
|
||||
if (pack->iooffset + namlen < volume->geo.blocksize)
|
||||
{
|
||||
/* No.. that inode name will not fit in this block. Return an
|
||||
* indication that we are at the end of the block and try again
|
||||
* later.
|
||||
*/
|
||||
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
/* Yes.. reserve space for the inode name (but don't write it yet) */
|
||||
|
||||
pack->dest.entry.noffset = nxffs_packtell(volume, pack);
|
||||
pack->iooffset += namlen;
|
||||
}
|
||||
|
||||
/* State 3: Inode header not-written, inode name written. Still need the position
|
||||
* of the first data block.
|
||||
*/
|
||||
|
||||
if (pack->dest.entry.doffset == 0)
|
||||
{
|
||||
/* Will the data block header plus a minimal amount of data fit in this
|
||||
* block? (or the whole file if the file is very small).
|
||||
*/
|
||||
|
||||
mindata = MIN(NXFFS_MINDATA, pack->dest.entry.datlen);
|
||||
if (pack->iooffset + SIZEOF_NXFFS_DATA_HDR + mindata < volume->geo.blocksize)
|
||||
{
|
||||
/* No.. return an indication that we are at the end of the block
|
||||
* and try again later.
|
||||
*/
|
||||
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
/* Yes.. reserve space for the data block header */
|
||||
|
||||
pack->dest.entry.doffset = nxffs_packtell(volume, pack);
|
||||
pack->iooffset += SIZEOF_NXFFS_DATA_HDR;
|
||||
}
|
||||
|
||||
/* Initialize the output data stream to start with the first data block */
|
||||
|
||||
pack->dest.blkoffset = pack->dest.entry.doffset;
|
||||
pack->dest.blkpos = 0;
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_wrinodehdr
|
||||
*
|
||||
* Description:
|
||||
* Write the destination inode header to FLASH.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - The volume to be packed
|
||||
* pack - The volume packing state structure.
|
||||
*
|
||||
* Returned Values:
|
||||
* Zero on success; Otherwise, a negated errno value is returned to
|
||||
* indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxffs_wrinodehdr(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack)
|
||||
{
|
||||
FAR struct nxffs_inode_s *inode;
|
||||
off_t ioblock;
|
||||
off_t namblock;
|
||||
uint16_t iooffset;
|
||||
uint16_t namoffset;
|
||||
uint32_t crc;
|
||||
int namlen;
|
||||
int ret;
|
||||
|
||||
/* Get positions corresponding to the inode header and inode name positions */
|
||||
|
||||
ioblock = nxffs_getblock(volume, pack->dest.entry.hoffset);
|
||||
iooffset = nxffs_getoffset(volume, pack->dest.entry.hoffset, ioblock);
|
||||
|
||||
namblock = nxffs_getblock(volume, pack->dest.entry.noffset);
|
||||
namoffset = nxffs_getoffset(volume, pack->dest.entry.noffset, namblock);
|
||||
|
||||
/* The inode header is not written until all of the inode data has been
|
||||
* packed into its new location. As a result, there are three possibilities:
|
||||
*
|
||||
* 1. The inode header lies in the current, unwritten erase block,
|
||||
* 2. The inode header resides in an earlier erase block and has already
|
||||
* been written to FLASH, but the inode name resides within the erase
|
||||
* block and has not been written to FLASH, or
|
||||
* 3. The inode header resides in an earlier erase block and has already
|
||||
* been written to FLASH (most likely case for files larger than an
|
||||
* erase block).
|
||||
*
|
||||
* Case 2 & 3: Does the inode header reside in a block before the beginning
|
||||
* of the current erase block?
|
||||
*/
|
||||
|
||||
if (ioblock < pack->block0)
|
||||
{
|
||||
/* Does the inode name also reside in a block before the beginning of
|
||||
* the current erase block?
|
||||
*/
|
||||
|
||||
if (namblock < pack->block0)
|
||||
{
|
||||
/* Yes.. this is case 3: Both the inode block header and the inode
|
||||
* name lie in an earlier erase block that has already been written
|
||||
* to FLASH. In this case, if we are very careful, we can just use
|
||||
* the standard routine to write the inode header that is called
|
||||
* during the normal file close operation:
|
||||
*/
|
||||
|
||||
ret = nxffs_wrinode(volume, &pack->dest.entry);
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Case 2: The inode header lies in an earlier erase block that
|
||||
* has been written to FLASH but the inode name is in the cache and
|
||||
* still unwritten.
|
||||
*/
|
||||
|
||||
#warning "Missing logic"
|
||||
return -ENOSYS;
|
||||
}
|
||||
}
|
||||
|
||||
/* Cases 1: Both the inode header and name are in the unwritten cache memory. */
|
||||
/* Initialize the inode header */
|
||||
|
||||
iooffset += (ioblock - pack->block0) * volume->geo.blocksize;
|
||||
inode = (FAR struct nxffs_inode_s *)&volume->pack[iooffset];
|
||||
memcpy(inode->magic, g_inodemagic, NXFFS_MAGICSIZE);
|
||||
|
||||
nxffs_wrle32(inode->noffs, pack->dest.entry.noffset);
|
||||
nxffs_wrle32(inode->doffs, pack->dest.entry.doffset);
|
||||
nxffs_wrle32(inode->utc, pack->dest.entry.utc);
|
||||
nxffs_wrle32(inode->crc, 0);
|
||||
nxffs_wrle32(inode->datlen, pack->dest.entry.datlen);
|
||||
|
||||
/* Get the length of the inode name */
|
||||
|
||||
namlen = strlen(pack->dest.entry.name);
|
||||
DEBUGASSERT(namlen < CONFIG_NXFFS_MAXNAMLEN);
|
||||
|
||||
inode->state = CONFIG_NXFFS_ERASEDSTATE;
|
||||
inode->namlen = namlen;
|
||||
|
||||
/* Calculate the CRC */
|
||||
|
||||
crc = crc32((FAR const uint8_t *)inode, SIZEOF_NXFFS_INODE_HDR);
|
||||
crc = crc32part((FAR const uint8_t *)pack->dest.entry.name, namlen, crc);
|
||||
|
||||
/* Finish the inode header */
|
||||
|
||||
inode->state = INODE_STATE_FILE;
|
||||
nxffs_wrle32(inode->crc, crc);
|
||||
|
||||
/* Write the inode name */
|
||||
|
||||
namoffset += (namblock - pack->block0) * volume->geo.blocksize;
|
||||
memcpy(&volume->pack[namoffset], pack->dest.entry.name, namlen);
|
||||
|
||||
/* Reset the dest inode information */
|
||||
|
||||
nxffs_freeentry(&pack->dest.entry);
|
||||
memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_wrdatthdr
|
||||
*
|
||||
* Description:
|
||||
* Write the destination data block header to FLASH.
|
||||
*
|
||||
* Input Parameters:
|
||||
* volume - The volume to be packed
|
||||
* pack - The volume packing state structure.
|
||||
*
|
||||
* Returned Values:
|
||||
* Zero on success; Otherwise, a negated errno value is returned to
|
||||
* indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void nxffs_wrdathdr(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack)
|
||||
{
|
||||
FAR struct nxffs_data_s *dathdr;
|
||||
off_t ioblock;
|
||||
uint16_t iooffset;
|
||||
uint32_t crc;
|
||||
|
||||
/* Get the offset in the block corresponding to the location of the inode
|
||||
* header. NOTE: This must lie in the same block as we currently have
|
||||
* buffered.
|
||||
*/
|
||||
|
||||
ioblock = nxffs_getblock(volume, pack->dest.entry.hoffset);
|
||||
iooffset = nxffs_getoffset(volume, pack->dest.entry.hoffset, ioblock);
|
||||
DEBUGASSERT(ioblock == pack->ioblock);
|
||||
|
||||
/* Write the data block header to memory */
|
||||
|
||||
dathdr = (FAR struct nxffs_data_s *)&pack->iobuffer[iooffset];
|
||||
memcpy(dathdr->magic, g_datamagic, NXFFS_MAGICSIZE);
|
||||
nxffs_wrle32(dathdr->crc, 0);
|
||||
nxffs_wrle16(dathdr->datlen, pack->dest.blklen);
|
||||
|
||||
/* Update the entire data block CRC (including the header) */
|
||||
|
||||
crc = crc32(&volume->cache[volume->iooffset], pack->dest.blklen + SIZEOF_NXFFS_DATA_HDR);
|
||||
nxffs_wrle32(dathdr->crc, crc);
|
||||
|
||||
/* Setup state to allocate the next data block */
|
||||
|
||||
pack->dest.blkoffset = 0;
|
||||
pack->dest.blklen = 0;
|
||||
pack->dest.blkpos = 0;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxffs_packblock
|
||||
*
|
||||
|
@ -343,28 +706,34 @@ static inline int nxffs_startblock(FAR struct nxffs_volume_s *volume,
|
|||
static inline int nxffs_packblock(FAR struct nxffs_volume_s *volume,
|
||||
FAR struct nxffs_pack_s *pack)
|
||||
{
|
||||
off_t offset;
|
||||
int ret;
|
||||
|
||||
/* Are we currently processing a block from the source stream? */
|
||||
|
||||
if (pack->src.blkoffset)
|
||||
{
|
||||
/* No, start with the first data block */
|
||||
/* No.. setup the source stream */
|
||||
|
||||
pack->src.blkoffset = pack->src.entry.doffset;
|
||||
pack->src.fpos = 0;
|
||||
pack->src.blkpos = 0;
|
||||
|
||||
/* Seek to the data block header, read and verify the block header */
|
||||
|
||||
ret = nxffs_rdblkhdr(volume, pack->src.blkoffset, &pack->src.blklen);
|
||||
ret = nxffs_srcsetup(volume, pack, pack->src.entry.doffset);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to verify the data block header: %d\n", -ret);
|
||||
fdbg("Failed to configure the src stream: %d\n", -ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* We enter here on a new block every time, so we always have to setup
|
||||
* the dest data stream.
|
||||
*/
|
||||
|
||||
ret = nxffs_destsetup(volume, pack);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to configure the dest stream: %d\n", -ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Loop, transferring data from the source block to the destination pack
|
||||
* buffer until either (1) the source stream is exhausted, (2) the destination
|
||||
* block is full, or (3) an error occurs.
|
||||
|
@ -374,11 +743,123 @@ static inline int nxffs_packblock(FAR struct nxffs_volume_s *volume,
|
|||
{
|
||||
/* Determine how much data is available in the dest pack buffer */
|
||||
|
||||
uint16_t destlen = volume->geo.blocksize - pack->dest.blkpos;
|
||||
|
||||
/* Dermined how much data is available in the src data block */
|
||||
|
||||
/* Transfer the data */
|
||||
#warning "Missing logic"
|
||||
return -ENOSYS;
|
||||
uint16_t srclen = pack->src.blklen - pack->src.blkpos;
|
||||
|
||||
/* Transfer the smaller of the two amounts data */
|
||||
|
||||
uint16_t xfrlen = MIN(srclen, destlen);
|
||||
nxffs_ioseek(volume, pack->src.blkoffset + pack->src.blkpos);
|
||||
memcpy(&pack->iobuffer[pack->iooffset], &volume->cache[volume->iooffset], xfrlen);
|
||||
|
||||
/* Now, either the (1) src block has been fully transferred, (2) all
|
||||
* of the source data has been transferred, of (3) the the destination
|
||||
* block is full, .. or all three.
|
||||
*
|
||||
* Check if all of the bytes in the source inode have been transferred.
|
||||
*/
|
||||
|
||||
pack->src.fpos += xfrlen;
|
||||
pack->src.blkpos += xfrlen;
|
||||
|
||||
if (pack->src.fpos >= pack->src.entry.datlen)
|
||||
{
|
||||
/* Write the final destination data block header and inode
|
||||
* headers.
|
||||
*/
|
||||
|
||||
nxffs_wrdathdr(volume, pack);
|
||||
nxffs_wrinodehdr(volume,pack);
|
||||
|
||||
/* Find the next valid source inode */
|
||||
|
||||
offset = pack->src.blkoffset + pack->src.blklen;
|
||||
ret = nxffs_nextentry(volume, offset, &pack->src.entry);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* No more valid inode entries. Just return an end-of-flash error
|
||||
* indication.
|
||||
*/
|
||||
|
||||
return -ENOSPC;
|
||||
}
|
||||
|
||||
/* Setup the new source stream */
|
||||
|
||||
ret = nxffs_srcsetup(volume, pack, pack->src.entry.doffset);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Setup the dest stream */
|
||||
|
||||
pack->dest.entry.name = pack->src.entry.name;
|
||||
pack->dest.entry.utc = pack->src.entry.utc;
|
||||
pack->dest.entry.datlen = pack->src.entry.datlen;
|
||||
pack->src.entry.name = NULL;
|
||||
|
||||
/* Is there sufficient space at the end of the I/O block to hold
|
||||
* the inode header?
|
||||
*/
|
||||
|
||||
if (pack->iooffset + SIZEOF_NXFFS_INODE_HDR > volume->geo.blocksize)
|
||||
{
|
||||
/* No, just return success... we will handle this condition when
|
||||
* this function is called on the next I/O block.
|
||||
*/
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
pack->dest.entry.hoffset = nxffs_packtell(volume, pack);
|
||||
ret = nxffs_destsetup(volume, pack);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/* Not at the end of the source data stream. Check if we are at the
|
||||
* end of the current data block.
|
||||
*/
|
||||
|
||||
else if (pack->src.blkpos >= pack->src.blklen)
|
||||
{
|
||||
struct nxffs_blkentry_s blkentry;
|
||||
|
||||
/* Yes.. find the next data block in the source input stream. */
|
||||
|
||||
ret = nxffs_nextblock(volume, offset, &blkentry);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to find next data block: %d\n", -ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Set up the source stream */
|
||||
|
||||
pack->src.blkoffset = blkentry.hoffset;
|
||||
pack->src.blklen = blkentry.datlen;
|
||||
pack->src.blkpos = 0;
|
||||
}
|
||||
|
||||
/* Check if the destination block is full */
|
||||
|
||||
pack->dest.fpos += xfrlen;
|
||||
pack->dest.blkpos += xfrlen;
|
||||
pack->iooffset += xfrlen;
|
||||
|
||||
if (pack->iooffset >= volume->geo.blocksize)
|
||||
{
|
||||
/* Yes.. Write the destination data block header and return success */
|
||||
|
||||
nxffs_wrdathdr(volume, pack);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
return -ENOSYS;
|
||||
|
@ -409,7 +890,6 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
struct nxffs_pack_s pack;
|
||||
off_t iooffset;
|
||||
off_t eblock;
|
||||
off_t block0;
|
||||
off_t block;
|
||||
int i;
|
||||
int ret;
|
||||
|
@ -432,7 +912,7 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
* begin the packing operation.
|
||||
*/
|
||||
|
||||
ret = nxffs_startblock(volume, &pack, iooffset);
|
||||
ret = nxffs_startpos(volume, &pack, iooffset);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* This is a normal situation if the volume is full */
|
||||
|
@ -448,11 +928,22 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
}
|
||||
}
|
||||
|
||||
/* Otherwise, begin pack at this src/dest block combination. Get the erase
|
||||
* block associated with the destination header offset.
|
||||
/* Otherwise, begin pack at this src/dest block combination. Initialize
|
||||
* ioblock and iooffset with the position of the first inode header.
|
||||
*/
|
||||
|
||||
nxffs_packseek(volume, &pack, pack.dest.entry.hoffset);
|
||||
pack.ioblock = nxffs_getblock(volume, pack.dest.entry.hoffset);
|
||||
pack.iooffset = nxffs_getoffset(volume, pack.dest.entry.hoffset, pack.ioblock);
|
||||
|
||||
/* Reserve space for the inode header. Note we are guaranteed by
|
||||
* nxffs_startpos() that the inode header will fit at hoffset.
|
||||
*/
|
||||
|
||||
pack.iooffset += SIZEOF_NXFFS_INODE_HDR;
|
||||
|
||||
/* Then pack all erase blocks starting with the erase block that contains
|
||||
* the ioblock and through the final erase block on the FLASH.
|
||||
*/
|
||||
|
||||
for (eblock = pack.ioblock / volume->blkper;
|
||||
eblock < volume->geo.neraseblocks;
|
||||
|
@ -460,8 +951,8 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
{
|
||||
/* Read the erase block into the pack buffer. */
|
||||
|
||||
block0 = eblock * volume->blkper;
|
||||
ret = MTD_BREAD(volume->mtd, block0, volume->blkper, volume->pack);
|
||||
pack.block0 = eblock * volume->blkper;
|
||||
ret = MTD_BREAD(volume->mtd, pack.block0, volume->blkper, volume->pack);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to read erase block %d: %d\n", eblock, -ret);
|
||||
|
@ -470,7 +961,7 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
|
||||
/* Pack each I/O block */
|
||||
|
||||
for (i = 0, block = block0, pack.iobuffer = volume->pack;
|
||||
for (i = 0, block = pack.block0, pack.iobuffer = volume->pack;
|
||||
i < volume->blkper;
|
||||
i++, block++, pack.iobuffer += volume->geo.blocksize)
|
||||
{
|
||||
|
@ -480,9 +971,16 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
|
||||
if (block >= pack.ioblock)
|
||||
{
|
||||
/* Set the I/O position. Note on the first time we get
|
||||
* pack.iooffset will hold the offset in the first I/O block
|
||||
* to the first inode header.
|
||||
*/
|
||||
|
||||
pack.ioblock = block;
|
||||
|
||||
/* Check if this is a valid block */
|
||||
/* Check if this is a valid block (it will be valid for the
|
||||
* first block.
|
||||
*/
|
||||
|
||||
if (nxffs_packvalid(&pack))
|
||||
{
|
||||
|
@ -491,16 +989,23 @@ int nxffs_pack(FAR struct nxffs_volume_s *volume)
|
|||
ret = nxffs_packblock(volume, &pack);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to pack into block %d: %d\n", block, ret);
|
||||
fdbg("Failed to pack into block %d: %d\n",
|
||||
block, ret);
|
||||
goto errout_with_pack;
|
||||
}
|
||||
}
|
||||
|
||||
/* Next time we get here, pack.iooffset will point to the
|
||||
* first byte after the block header.
|
||||
*/
|
||||
|
||||
pack.iooffset = SIZEOF_NXFFS_BLOCK_HDR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write the packed I/O block to FLASH */
|
||||
|
||||
ret = MTD_BWRITE(volume->mtd, block0, volume->blkper, volume->pack);
|
||||
ret = MTD_BWRITE(volume->mtd, pack.block0, volume->blkper, volume->pack);
|
||||
if (ret < 0)
|
||||
{
|
||||
fdbg("Failed to write erase block %d: %d\n", eblock, -ret);
|
||||
|
|
|
@ -57,13 +57,6 @@
|
|||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* When we allocate FLASH for a new inode data block, we will require that
|
||||
* space is available to hold this minimum number of data bytes in addition
|
||||
* to the size of the data block headeer.
|
||||
*/
|
||||
|
||||
#define NXFFS_MINDATA 16
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
@ -798,7 +791,7 @@ int nxffs_wrblkhdr(FAR struct nxffs_volume_s *volume,
|
|||
FAR struct nxffs_data_s *dathdr;
|
||||
int ret;
|
||||
|
||||
/* Write the dat block header to memory */
|
||||
/* Write the data block header to memory */
|
||||
|
||||
nxffs_ioseek(volume, wrfile->doffset);
|
||||
dathdr = (FAR struct nxffs_data_s *)&volume->cache[volume->iooffset];
|
||||
|
|
|
@ -63,6 +63,10 @@
|
|||
# define CONFIG_NXFFS_PACKTHRESHOLD 32
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_NXFFS_MAXNAMLEN
|
||||
# define CONFIG_NXFFS_MAXNAMLEN 255
|
||||
#endif
|
||||
|
||||
/* At present, only a single pre-allocated NXFFS volume is supported. This
|
||||
* is because here can be only a single NXFFS volume mounted at any time.
|
||||
* This has to do with the fact that we bind to an MTD driver (instead of a
|
||||
|
|
Loading…
Reference in New Issue