2011-04-29 22:59:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_write.c
|
|
|
|
*
|
2018-01-04 17:54:54 +01:00
|
|
|
* Copyright (C) 2011, 2013, 2017-2018 Gregory Nutt. All rights reserved.
|
2011-10-31 21:23:17 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-04-29 22:59:40 +02:00
|
|
|
*
|
|
|
|
* References: Linux/Documentation/filesystems/romfs.txt
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2011-04-30 22:06:23 +02:00
|
|
|
#include <crc32.h>
|
2011-04-29 22:59:40 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-04-29 22:59:40 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_hdrpos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the data block header. A valid location will
|
|
|
|
* have these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire header
|
|
|
|
* PLUS some meaningful amount of data (NXFFS_MINDATA).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the checks of 1) and 2).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the header position. On
|
|
|
|
* successful return, this field will hold the selected header
|
|
|
|
* position.
|
|
|
|
* size - The minimum size of the current write.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2011-05-06 17:19:07 +02:00
|
|
|
* wrfile->doffset - Flash offset to candidate data block header position
|
2011-04-30 22:06:23 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_hdrpos(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Reserve memory for the object */
|
|
|
|
|
|
|
|
ret = nxffs_wrreserve(volume, SIZEOF_NXFFS_DATA_HDR + size);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2011-05-06 17:19:07 +02:00
|
|
|
/* Save the offset to the FLASH region reserved for the data block
|
|
|
|
* header
|
|
|
|
*/
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
wrfile->doffset = nxffs_iotell(volume);
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-06 17:19:07 +02:00
|
|
|
return ret;
|
2011-04-30 22:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_hdrerased
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the data block header. A valid location will
|
|
|
|
* have these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire header
|
|
|
|
* PLUS some meaningful amount of data (NXFFS_MINDATA).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the check 3). On entry it assumes:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the header position. On
|
|
|
|
* successful return, this field will hold the selected header
|
|
|
|
* position.
|
|
|
|
* size - The minimum size of the current write.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2011-05-06 17:19:07 +02:00
|
|
|
* wrfile->doffset - Flash offset to candidate data block header position
|
2011-04-30 22:06:23 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_hdrerased(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find a valid location to save the inode header */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
ret = nxffs_wrverify(volume, SIZEOF_NXFFS_DATA_HDR + size);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2011-05-06 17:19:07 +02:00
|
|
|
/* This is where we will put the data block header */
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
wrfile->doffset = nxffs_iotell(volume);
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wralloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate FLASH memory for the data block.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Describes the open file to be written.
|
|
|
|
* size - Size of the current write operation.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_wralloc(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
bool packed;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate FLASH memory for the data block.
|
|
|
|
*
|
|
|
|
* Loop until the data block header is configured or until a failure
|
|
|
|
* occurs. Note that nothing is written to FLASH. The data block header
|
|
|
|
* is not written until either (1) the file is closed, or (2) the data
|
|
|
|
* region is fully populated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
packed = false;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
|
|
|
size_t mindata = MIN(NXFFS_MINDATA, size);
|
|
|
|
|
|
|
|
/* File a valid location to position the data block. Start with
|
|
|
|
* the first byte in the free FLASH region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_hdrpos(volume, wrfile, mindata);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Find a region of memory in the block that is fully erased */
|
|
|
|
|
|
|
|
ret = nxffs_hdrerased(volume, wrfile, mindata);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Valid memory for the data block was found. Return success. */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no valid memory is found searching to the end of the volume,
|
|
|
|
* then -ENOSPC will be returned. Other errors are not handled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ENOSPC || packed)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to find inode header memory: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -ENOSPC is a special case.. It means that the volume is full.
|
|
|
|
* Try to pack the volume in order to free up some space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_pack(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to pack the volume: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-10-31 21:23:17 +01:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* After packing the volume, froffset will be updated to point to the
|
|
|
|
* new free flash region. Try again.
|
|
|
|
*/
|
2011-10-31 21:23:17 +01:00
|
|
|
|
|
|
|
nxffs_ioseek(volume, volume->froffset);
|
2011-04-30 22:06:23 +02:00
|
|
|
packed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can't get here */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_reverify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the partial flash data block in the volume cache is valid.
|
|
|
|
* On entry, this function assumes:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* data block.
|
|
|
|
* volume->iooffset - The offset in the block to the data block.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Describes the open file to be written.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_reverify(FAR struct nxffs_volume_s *volume,
|
2018-01-06 20:32:32 +01:00
|
|
|
FAR struct nxffs_wrfile_s *wrfile)
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
|
|
|
uint32_t crc;
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
if (wrfile->datlen > 0)
|
|
|
|
{
|
|
|
|
/* Get the offset to the start of the data */
|
|
|
|
|
|
|
|
offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
|
2018-01-06 20:32:32 +01:00
|
|
|
DEBUGASSERT(offset + wrfile->datlen <= volume->geo.blocksize);
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
/* Calculate the CRC of the partial data block */
|
|
|
|
|
|
|
|
crc = crc32(&volume->cache[offset], wrfile->datlen);
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
/* It must match the previously calculated CRC value */
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
if (crc != wrfile->crc)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: CRC failure\n");
|
2011-04-30 22:06:23 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrappend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Append FLASH data to the data block.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Describes the open file to be written.
|
|
|
|
* buffer - Address of buffer of data to be written.
|
2018-01-04 17:54:54 +01:00
|
|
|
* buflen - The number of bytes remaining to be written
|
2011-04-30 22:06:23 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of bytes written is returned on success. Otherwise, a
|
|
|
|
* negated errno value is returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline ssize_t nxffs_wrappend(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
FAR const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
ssize_t maxsize;
|
|
|
|
size_t nbytestowrite;
|
|
|
|
ssize_t nbytesleft;
|
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the offset to the start of unwritten data */
|
|
|
|
|
|
|
|
offset = volume->iooffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR;
|
|
|
|
|
|
|
|
/* Determine that maximum amount of data that can be written to this
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
maxsize = volume->geo.blocksize - offset;
|
|
|
|
DEBUGASSERT(maxsize > 0);
|
|
|
|
|
|
|
|
/* But don't try to write over any unerased bytes */
|
|
|
|
|
|
|
|
maxsize = nxffs_erased(&volume->cache[offset], maxsize);
|
|
|
|
|
|
|
|
/* Write as many bytes as we can into the data buffer */
|
|
|
|
|
|
|
|
nbytestowrite = MIN(maxsize, buflen);
|
|
|
|
nbytesleft = maxsize - nbytestowrite;
|
|
|
|
|
|
|
|
if (nbytestowrite > 0)
|
|
|
|
{
|
|
|
|
/* Copy the data into the volume write cache */
|
|
|
|
|
|
|
|
memcpy(&volume->cache[offset], buffer, nbytestowrite);
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
/* Increment the number of bytes written to the data block */
|
|
|
|
|
|
|
|
wrfile->datlen += nbytestowrite;
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* Re-calculate the CRC */
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
|
|
|
|
wrfile->crc = crc32(&volume->cache[offset], wrfile->datlen);
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
/* And write the partial write block to FLASH -- unless the data
|
|
|
|
* block is full. In that case, the block will be written below.
|
|
|
|
*/
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
if (nbytesleft > 0)
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
2011-05-01 02:26:38 +02:00
|
|
|
ret = nxffs_wrcache(volume);
|
2011-05-01 01:24:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
|
2011-05-01 01:24:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-04-30 22:06:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
/* Check if the data block is now full */
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
if (nbytesleft <= 0)
|
|
|
|
{
|
|
|
|
/* The data block is full, write the block to FLASH */
|
|
|
|
|
|
|
|
ret = nxffs_wrblkhdr(volume, wrfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_wrblkdhr failed: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes written to FLASH this time */
|
|
|
|
|
|
|
|
return nbytestowrite;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:54:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_zappend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Zero-extend FLASH data to the data block.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Describes the open file to be written.
|
|
|
|
* nzeros - The number of bytes of zeroed data to be written
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-01-06 20:32:32 +01:00
|
|
|
* The number of zero bytes written is returned on success. Otherwise, a
|
2018-01-04 17:54:54 +01:00
|
|
|
* negated errno value is returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
#ifdef __NO_TRUNCATE_SUPPORT__
|
2018-01-04 17:54:54 +01:00
|
|
|
static inline ssize_t nxffs_zappend(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
off_t nzeros)
|
|
|
|
{
|
|
|
|
ssize_t maxsize;
|
|
|
|
size_t nbytestoclear;
|
2018-01-06 20:32:32 +01:00
|
|
|
ssize_t nbytesleft;
|
2018-01-04 17:54:54 +01:00
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the offset to the start of unwritten data */
|
|
|
|
|
|
|
|
offset = volume->iooffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR;
|
|
|
|
|
|
|
|
/* Determine that maximum amount of data that can be written to this
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
maxsize = volume->geo.blocksize - offset;
|
|
|
|
DEBUGASSERT(maxsize > 0);
|
|
|
|
|
|
|
|
/* Write as many bytes as we can into the data buffer */
|
|
|
|
|
|
|
|
nbytestoclear = MIN(maxsize, nzeros);
|
2018-01-06 20:32:32 +01:00
|
|
|
nbytesleft = maxsize - nbytestoclear;
|
2018-01-04 17:54:54 +01:00
|
|
|
|
|
|
|
if (nbytestoclear > 0)
|
|
|
|
{
|
2018-01-06 20:32:32 +01:00
|
|
|
/* Zero the data into the volume write cache */
|
2018-01-04 17:54:54 +01:00
|
|
|
|
|
|
|
memset(&volume->cache[offset], 0, nbytestoclear);
|
|
|
|
|
|
|
|
/* Increment the number of bytes written to the data block */
|
|
|
|
|
|
|
|
wrfile->datlen += nbytestoclear;
|
|
|
|
|
|
|
|
/* Re-calculate the CRC */
|
|
|
|
|
|
|
|
offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
|
|
|
|
wrfile->crc = crc32(&volume->cache[offset], wrfile->datlen);
|
|
|
|
|
|
|
|
/* And write the partial write block to FLASH -- unless the data
|
|
|
|
* block is full. In that case, the block will be written below.
|
|
|
|
*/
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
if (nbytesleft > 0)
|
2018-01-04 17:54:54 +01:00
|
|
|
{
|
|
|
|
ret = nxffs_wrcache(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the data block is now full */
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
if (nbytesleft <= 0)
|
2018-01-04 17:54:54 +01:00
|
|
|
{
|
|
|
|
/* The data block is full, write the block to FLASH */
|
|
|
|
|
|
|
|
ret = nxffs_wrblkhdr(volume, wrfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: nxffs_wrblkdhr failed: %d\n", -ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
return nbytestoclear;
|
2018-01-04 17:54:54 +01:00
|
|
|
}
|
2018-01-06 20:32:32 +01:00
|
|
|
#endif
|
2018-01-04 17:54:54 +01:00
|
|
|
|
2011-04-29 22:59:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is an implementation of the NuttX standard file system write
|
|
|
|
* method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile;
|
2011-05-02 03:30:30 +02:00
|
|
|
ssize_t remaining;
|
|
|
|
ssize_t nwritten;
|
|
|
|
ssize_t total;
|
2011-04-30 22:06:23 +02:00
|
|
|
int ret;
|
2011-04-29 22:59:40 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Write %d bytes to offset %d\n", buflen, filep->f_pos);
|
2011-04-29 22:59:40 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover the open file state from the struct file instance */
|
|
|
|
|
|
|
|
wrfile = (FAR struct nxffs_wrfile_s *)filep->f_priv;
|
|
|
|
|
|
|
|
/* Recover the volume state from the open file */
|
|
|
|
|
|
|
|
volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private;
|
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
|
|
|
|
/* Get exclusive access to the volume. Note that the volume exclsem
|
|
|
|
* protects the open file list.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
|
|
|
if (ret < 0)
|
2011-04-29 22:59:40 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
2011-04-29 22:59:40 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the file was opened with write access */
|
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
if ((wrfile->ofile.oflags & O_WROK) == 0)
|
2011-04-29 22:59:40 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: File not open for write access\n");
|
2011-04-29 22:59:40 +02:00
|
|
|
ret = -EACCES;
|
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* Loop until we successfully appended all of the data to the file (or an
|
|
|
|
* error occurs)
|
|
|
|
*/
|
2011-04-29 22:59:40 +02:00
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
for (total = 0; total < buflen; )
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
2011-05-02 18:14:20 +02:00
|
|
|
remaining = buflen - total;
|
2011-05-02 03:30:30 +02:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* Have we already allocated the data block? */
|
2011-04-29 22:59:40 +02:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
if (wrfile->doffset == 0)
|
|
|
|
{
|
2011-10-31 21:23:17 +01:00
|
|
|
/* No, allocate the data block now, re-packing if necessary. */
|
2011-04-29 22:59:40 +02:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
wrfile->datlen = 0;
|
2011-05-02 03:30:30 +02:00
|
|
|
ret = nxffs_wralloc(volume, wrfile, remaining);
|
2011-04-30 22:06:23 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate a data block: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to the FLASH block containing the data block */
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, wrfile->doffset);
|
2011-04-29 22:59:40 +02:00
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* Verify that the FLASH data that was previously written is still intact */
|
|
|
|
|
|
|
|
ret = nxffs_reverify(volume, wrfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to verify FLASH data block: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the data to the end of the data block and write the updated
|
|
|
|
* block to flash.
|
|
|
|
*/
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
nwritten = nxffs_wrappend(volume, wrfile, &buffer[total], remaining);
|
|
|
|
if (nwritten < 0)
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to append to FLASH to a data block: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the number of bytes remaining to be written */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
total += nwritten;
|
2011-04-29 22:59:40 +02:00
|
|
|
}
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
/* Success.. return the number of bytes written */
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
ret = total;
|
2011-05-01 01:24:48 +02:00
|
|
|
filep->f_pos = wrfile->datlen;
|
|
|
|
|
2011-04-29 22:59:40 +02:00
|
|
|
errout_with_semaphore:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->exclsem);
|
2011-04-29 22:59:40 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2018-01-04 17:54:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrextend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Zero-extend a file.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2018-01-04 17:54:54 +01:00
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* entry - Describes the new inode entry
|
|
|
|
* length - The new, extended length of the file
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the NXFFS semaphore.
|
|
|
|
* The caller has verified that the file is writable.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-01-06 20:32:32 +01:00
|
|
|
#ifdef __NO_TRUNCATE_SUPPORT__
|
2018-01-04 17:54:54 +01:00
|
|
|
int nxffs_wrextend(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile, off_t length)
|
|
|
|
{
|
|
|
|
ssize_t remaining;
|
|
|
|
ssize_t nwritten;
|
|
|
|
off_t oldsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
finfo("Extend file to %ld bytes to offset %d\n", (long)length);
|
|
|
|
DEBUGASSERT(volume != NULL && wrfile != NULL);
|
|
|
|
|
|
|
|
oldsize = wrfile->ofile.entry.datlen;
|
|
|
|
DEBUGASSERT(length > oldsize);
|
|
|
|
|
|
|
|
/* Loop until we successfully appended all of the data to the file (or an
|
|
|
|
* error occurs)
|
|
|
|
*/
|
|
|
|
|
|
|
|
remaining = length - oldsize;
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
/* Have we already allocated the data block? */
|
|
|
|
|
|
|
|
if (wrfile->doffset == 0)
|
|
|
|
{
|
|
|
|
/* No, allocate the data block now, re-packing if necessary. */
|
|
|
|
|
|
|
|
wrfile->datlen = 0;
|
|
|
|
ret = nxffs_wralloc(volume, wrfile, remaining);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate a data block: %d\n", -ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to the FLASH block containing the data block */
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, wrfile->doffset);
|
|
|
|
|
|
|
|
/* Verify that the FLASH data that was previously written is still intact */
|
|
|
|
|
|
|
|
ret = nxffs_reverify(volume, wrfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to verify FLASH data block: %d\n", -ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the data to the end of the data block and write the updated
|
|
|
|
* block to flash.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nwritten = nxffs_zappend(volume, wrfile, remaining);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to zero extend FLASH data block: %d\n", -ret);
|
|
|
|
return (int)nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the number of bytes remaining to be written */
|
|
|
|
|
|
|
|
remaining -= nwritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2018-01-06 20:32:32 +01:00
|
|
|
#endif
|
2018-01-04 17:54:54 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrreserve
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for a file system object of 'size'. A valid
|
|
|
|
* location will have these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire object
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the checks of 1) and 2). The
|
|
|
|
* end-of-filesystem offset, froffset, is update past this memory which,
|
|
|
|
* in effect, reserves the memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* size - The size of the object to be reserved.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
2020-02-23 09:50:23 +01:00
|
|
|
* candidate object position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->iooffset - The offset in the block to the candidate object
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block after
|
|
|
|
* the reserved memory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_wrreserve(FAR struct nxffs_volume_s *volume, size_t size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Seek to the beginning of the free FLASH region */
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
nxffs_ioseek(volume, volume->froffset);
|
2011-04-30 19:29:40 +02:00
|
|
|
|
|
|
|
/* Check for a seek past the end of the volume */
|
|
|
|
|
|
|
|
if (volume->ioblock >= volume->nblocks)
|
|
|
|
{
|
|
|
|
/* Return -ENOSPC to indicate that the volume is full */
|
|
|
|
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
/* Skip over block headers */
|
|
|
|
|
|
|
|
if (volume->iooffset < SIZEOF_NXFFS_BLOCK_HDR)
|
|
|
|
{
|
|
|
|
volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
|
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* Make sure that there is space there to hold the entire object */
|
|
|
|
|
|
|
|
if (volume->iooffset + size > volume->geo.blocksize)
|
|
|
|
{
|
|
|
|
/* We will need to skip to the next block. But first, check if we are
|
|
|
|
* already at the final block.
|
|
|
|
*/
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
if (volume->ioblock + 1 >= volume->nblocks)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
|
|
|
/* Return -ENOSPC to indicate that the volume is full */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: No space in last block\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is not the last block in the volume, so just seek to the
|
|
|
|
* beginning of the next, valid block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
volume->ioblock++;
|
|
|
|
ret = nxffs_validblock(volume, &volume->ioblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: No more valid blocks\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2013-12-03 02:00:59 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the pointer to the first next free FLASH memory -- reserving this
|
|
|
|
* block of memory.
|
|
|
|
*/
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
volume->froffset = nxffs_iotell(volume) + size;
|
2011-04-30 19:29:40 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrverify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the object. A valid location will have
|
|
|
|
* these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire header
|
|
|
|
* (excluding the file name which may lie in the next block).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the check 3). On entry it assumes the
|
|
|
|
* following settings (left by nxffs_wrreserve()):
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
2020-02-23 09:50:23 +01:00
|
|
|
* candidate object position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->iooffset - The offset in the block to the candidate object
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
2019-12-12 14:41:51 +01:00
|
|
|
* size - The size of the object to be verified.
|
2011-04-30 19:29:40 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* verified object position
|
|
|
|
* volume->iooffset - The offset in the block to the verified object
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_wrverify(FAR struct nxffs_volume_s *volume, size_t size)
|
|
|
|
{
|
|
|
|
uint16_t iooffset;
|
|
|
|
int nerased;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search to the very last block in the volume if we have to */
|
|
|
|
|
|
|
|
while (volume->ioblock < volume->nblocks)
|
|
|
|
{
|
|
|
|
/* Make sure that the block is in memory */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-03 15:46:14 +02:00
|
|
|
ret = nxffs_rdcache(volume, volume->ioblock);
|
2011-04-30 19:29:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-12-03 00:19:22 +01:00
|
|
|
/* Ignore the error... just skip to the next block. This should
|
|
|
|
* never happen with normal FLASH, but could occur with NAND if
|
|
|
|
* the block has uncorrectable bit errors.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to read block %d: %d\n",
|
2013-12-03 00:19:22 +01:00
|
|
|
volume->ioblock, -ret);
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search to the very end of this block if we have to */
|
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
else
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
2013-12-03 00:19:22 +01:00
|
|
|
iooffset = volume->iooffset;
|
|
|
|
nerased = 0;
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
for (i = volume->iooffset; i < volume->geo.blocksize; i++)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
2013-12-03 00:19:22 +01:00
|
|
|
/* Is this byte erased? */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
if (volume->cache[i] == CONFIG_NXFFS_ERASEDSTATE)
|
|
|
|
{
|
|
|
|
/* Yes.. increment the count of contiguous, erased bytes */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
nerased++;
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
/* Is the whole header memory erased? */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
if (nerased >= size)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Yes.. this this is where we will put the object */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
off_t offset =
|
|
|
|
volume->ioblock * volume->geo.blocksize + iooffset;
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Update the free flash offset and return success */
|
2013-12-03 00:19:22 +01:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
volume->froffset = offset + size;
|
|
|
|
return OK;
|
2013-12-03 00:19:22 +01:00
|
|
|
}
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
/* This byte is not erased! (It should be unless the block is
|
|
|
|
* bad)
|
|
|
|
*/
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nerased = 0;
|
|
|
|
iooffset = i + 1;
|
|
|
|
}
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
/* If we get here, then either (1) this block is not read-able, or
|
|
|
|
* (2) we have looked at every byte in the block and did not find
|
|
|
|
* any sequence of erased bytes long enough to hold the object.
|
|
|
|
* Skip to the next, valid block.
|
2011-04-30 19:29:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
volume->ioblock++;
|
|
|
|
ret = nxffs_validblock(volume, &volume->ioblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: No more valid blocks\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
|
2019-10-27 18:48:14 +01:00
|
|
|
volume->froffset = volume->ioblock * volume->geo.blocksize +
|
|
|
|
SIZEOF_NXFFS_BLOCK_HDR;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return -ENOSPC if there is no erased memory left in the volume for
|
|
|
|
* the object.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Not enough memory left to hold the file header\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrblkhdr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the block header information. This is done (1) whenever the end-
|
|
|
|
* block is encountered and (2) also when the file is closed in order to
|
|
|
|
* flush the final block of data to FLASH.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the state of the NXFFS volume
|
|
|
|
* wrfile - Describes the state of the open file
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success; Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_wrblkhdr(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile)
|
|
|
|
{
|
2011-04-30 22:06:23 +02:00
|
|
|
FAR struct nxffs_data_s *dathdr;
|
|
|
|
int ret;
|
|
|
|
|
2011-05-04 00:10:52 +02:00
|
|
|
/* Write the data block header to memory */
|
2011-04-30 22:06:23 +02:00
|
|
|
|
|
|
|
nxffs_ioseek(volume, wrfile->doffset);
|
|
|
|
dathdr = (FAR struct nxffs_data_s *)&volume->cache[volume->iooffset];
|
|
|
|
memcpy(dathdr->magic, g_datamagic, NXFFS_MAGICSIZE);
|
|
|
|
nxffs_wrle32(dathdr->crc, 0);
|
|
|
|
nxffs_wrle16(dathdr->datlen, wrfile->datlen);
|
|
|
|
|
|
|
|
/* Update the entire data block CRC (including the header) */
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
wrfile->crc = crc32(&volume->cache[volume->iooffset],
|
|
|
|
wrfile->datlen + SIZEOF_NXFFS_DATA_HDR);
|
2011-04-30 22:06:23 +02:00
|
|
|
nxffs_wrle32(dathdr->crc, wrfile->crc);
|
|
|
|
|
|
|
|
/* And write the data block to FLASH */
|
|
|
|
|
2011-05-01 02:26:38 +02:00
|
|
|
ret = nxffs_wrcache(volume);
|
2011-04-30 22:06:23 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
|
2011-04-30 22:06:23 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After the block has been successfully written to flash, update the inode
|
|
|
|
* statistics and reset the write state.
|
2011-05-01 01:24:48 +02:00
|
|
|
*
|
|
|
|
* volume:
|
|
|
|
* froffset - The offset the next free FLASH region. Set to just after
|
|
|
|
* the inode data block that we just wrote. This is where we will
|
|
|
|
* begin the search for the next inode header or data block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
volume->froffset = (wrfile->doffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR);
|
|
|
|
|
|
|
|
/* wrfile->file.entry:
|
|
|
|
* datlen: Total file length accumulated so far. When the file is
|
|
|
|
* closed, this will hold the file length.
|
|
|
|
* doffset: Offset to the first data block. Only the offset to the
|
|
|
|
* first data block is saved.
|
2011-04-30 22:06:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
wrfile->ofile.entry.datlen += wrfile->datlen;
|
2011-05-01 01:24:48 +02:00
|
|
|
if (wrfile->ofile.entry.doffset == 0)
|
2011-04-30 22:06:23 +02:00
|
|
|
{
|
|
|
|
wrfile->ofile.entry.doffset = wrfile->doffset;
|
|
|
|
}
|
2011-05-01 01:24:48 +02:00
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
wrfile->crc = 0;
|
|
|
|
wrfile->doffset = 0;
|
|
|
|
wrfile->datlen = 0;
|
|
|
|
return ret;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|