2011-04-28 21:26:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_initialize.c
|
|
|
|
*
|
2018-01-03 23:03:56 +01:00
|
|
|
* Copyright (C) 2011, 2013, 2015, 2017-2018 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2011-10-31 21:23:17 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-04-28 21:26:46 +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>
|
|
|
|
|
2011-04-29 22:59:40 +02:00
|
|
|
#include <string.h>
|
2011-04-28 21:26:46 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2011-04-28 21:26:46 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct mountpt_operations nxffs_operations =
|
|
|
|
{
|
|
|
|
nxffs_open, /* open */
|
|
|
|
nxffs_close, /* close */
|
|
|
|
nxffs_read, /* read */
|
|
|
|
nxffs_write, /* write */
|
|
|
|
NULL, /* seek -- Use f_pos in struct file */
|
|
|
|
nxffs_ioctl, /* ioctl */
|
2013-01-14 16:42:03 +01:00
|
|
|
|
2011-04-28 21:26:46 +02:00
|
|
|
NULL, /* sync -- No buffered data */
|
2013-01-14 20:22:32 +01:00
|
|
|
nxffs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
nxffs_fstat, /* fstat */
|
2018-01-06 20:32:32 +01:00
|
|
|
#ifdef __NO_TRUNCATE_SUPPORT__
|
2018-01-04 17:54:54 +01:00
|
|
|
nxffs_truncate, /* truncate */
|
2018-01-06 20:32:32 +01:00
|
|
|
#else
|
|
|
|
NULL, /* truncate */
|
|
|
|
#endif
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
nxffs_opendir, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
nxffs_readdir, /* readdir */
|
|
|
|
nxffs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
nxffs_bind, /* bind */
|
|
|
|
nxffs_unbind, /* unbind */
|
|
|
|
nxffs_statfs, /* statfs */
|
|
|
|
|
|
|
|
nxffs_unlink, /* unlink */
|
|
|
|
NULL, /* mkdir -- no directories */
|
|
|
|
NULL, /* rmdir -- no directories */
|
|
|
|
NULL, /* rename -- cannot rename in place if name is longer */
|
|
|
|
nxffs_stat /* stat */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2011-04-28 21:26:46 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The magic number that appears that the beginning of each NXFFS (logical)
|
|
|
|
* block
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
const uint8_t g_blockmagic[NXFFS_MAGICSIZE] =
|
|
|
|
{
|
|
|
|
'B', 'l', 'c', 'k'
|
|
|
|
};
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
/* The magic number that appears that the beginning of each NXFFS inode */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
const uint8_t g_inodemagic[NXFFS_MAGICSIZE] =
|
|
|
|
{
|
|
|
|
'I', 'n', 'o', 'd'
|
|
|
|
};
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
/* The magic number that appears that the beginning of each NXFFS inode
|
|
|
|
* data block.
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
const uint8_t g_datamagic[NXFFS_MAGICSIZE] =
|
|
|
|
{
|
|
|
|
'D', 'a', 't', 'a'
|
|
|
|
};
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
/* If CONFIG_NXFFS_PREALLOCATED is defined, then this is the single, pre-
|
2011-04-29 22:59:40 +02:00
|
|
|
* allocated NXFFS volume instance.
|
|
|
|
*/
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2011-04-29 22:59:40 +02:00
|
|
|
struct nxffs_volume_s g_volume;
|
|
|
|
#endif
|
|
|
|
|
2011-04-28 21:26:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize to provide NXFFS on an MTD interface
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mtd - The MTD device that supports the FLASH interface.
|
2011-04-29 22:59:40 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
2011-04-28 21:26:46 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-29 22:59:40 +02:00
|
|
|
int nxffs_initialize(FAR struct mtd_dev_s *mtd)
|
2011-04-28 21:26:46 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
2013-12-03 00:19:22 +01:00
|
|
|
#ifdef CONFIG_NXFFS_SCAN_VOLUME
|
2013-12-03 13:59:22 +01:00
|
|
|
struct nxffs_blkstats_s stats;
|
2011-04-28 21:26:46 +02:00
|
|
|
off_t threshold;
|
2013-12-03 00:19:22 +01:00
|
|
|
#endif
|
2011-04-28 21:26:46 +02:00
|
|
|
int ret;
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
/* If CONFIG_NXFFS_PREALLOCATED is defined, then this is the single, pre-
|
2011-04-29 22:59:40 +02:00
|
|
|
* allocated NXFFS volume instance.
|
|
|
|
*/
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2011-04-29 22:59:40 +02:00
|
|
|
|
|
|
|
volume = &g_volume;
|
|
|
|
memset(volume, 0, sizeof(struct nxffs_volume_s));
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-04-28 21:26:46 +02:00
|
|
|
/* Allocate a NXFFS volume structure */
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
volume = kmm_zalloc(sizeof(struct nxffs_volume_s));
|
2011-04-28 21:26:46 +02:00
|
|
|
if (!volume)
|
|
|
|
{
|
2013-09-12 17:42:34 +02:00
|
|
|
return -ENOMEM;
|
2011-04-28 21:26:46 +02:00
|
|
|
}
|
2011-04-29 22:59:40 +02:00
|
|
|
#endif
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
/* Initialize the NXFFS volume structure */
|
|
|
|
|
2011-05-03 15:46:14 +02:00
|
|
|
volume->mtd = mtd;
|
|
|
|
volume->cblock = (off_t)-1;
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&volume->exclsem, 0, 1);
|
|
|
|
nxsem_init(&volume->wrsem, 0, 1);
|
2011-04-28 21:26:46 +02:00
|
|
|
|
|
|
|
/* Get the volume geometry. (casting to uintptr_t first eliminates
|
|
|
|
* complaints on some architectures where the sizeof long is different
|
|
|
|
* from the size of a pointer).
|
|
|
|
*/
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
ret = MTD_IOCTL(mtd, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)((uintptr_t)&volume->geo));
|
2011-04-28 21:26:46 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: MTD ioctl(MTDIOC_GEOMETRY) failed: %d\n", -ret);
|
2011-04-28 21:26:46 +02:00
|
|
|
goto errout_with_volume;
|
|
|
|
}
|
|
|
|
|
2011-05-03 15:46:14 +02:00
|
|
|
/* Allocate one I/O block buffer to general files system access */
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
volume->cache = (FAR uint8_t *)kmm_malloc(volume->geo.blocksize);
|
2011-04-28 21:26:46 +02:00
|
|
|
if (!volume->cache)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate an erase block buffer\n");
|
2011-04-28 21:26:46 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_volume;
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
/* Pre-allocate one, full, in-memory erase block. This is needed for
|
|
|
|
* filesystem packing (but is useful in other places as well). This buffer
|
|
|
|
* is not needed often, but is best to have pre-allocated and in-place.
|
2011-05-03 02:02:10 +02:00
|
|
|
*/
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
volume->pack = (FAR uint8_t *)kmm_malloc(volume->geo.erasesize);
|
2011-05-03 02:02:10 +02:00
|
|
|
if (!volume->pack)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate an I/O block buffer\n");
|
2011-05-03 02:02:10 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_cache;
|
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* Get the number of R/W blocks per erase block and the total number o
|
|
|
|
* R/W blocks
|
|
|
|
*/
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
volume->blkper = volume->geo.erasesize / volume->geo.blocksize;
|
|
|
|
volume->nblocks = volume->geo.neraseblocks * volume->blkper;
|
2020-07-02 16:03:27 +02:00
|
|
|
DEBUGASSERT((off_t)volume->blkper * volume->geo.blocksize ==
|
|
|
|
volume->geo.erasesize);
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
#ifdef CONFIG_NXFFS_SCAN_VOLUME
|
2011-04-28 21:26:46 +02:00
|
|
|
/* Check if there is a valid NXFFS file system on the flash */
|
|
|
|
|
|
|
|
ret = nxffs_blockstats(volume, &stats);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to collect block statistics: %d\n", -ret);
|
2011-05-03 02:02:10 +02:00
|
|
|
goto errout_with_buffer;
|
2011-04-28 21:26:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the proportion of good blocks is low or the proportion of unformatted
|
|
|
|
* blocks is high, then reformat the FLASH.
|
|
|
|
*/
|
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
threshold = (stats.nblocks * CONFIG_NXFFS_REFORMAT_THRESH) / 100;
|
2011-04-28 21:26:46 +02:00
|
|
|
if (stats.ngood < threshold || stats.nunformat > threshold)
|
|
|
|
{
|
|
|
|
/* Reformat the volume */
|
|
|
|
|
|
|
|
ret = nxffs_reformat(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to reformat the volume: %d\n", -ret);
|
2011-05-03 02:02:10 +02:00
|
|
|
goto errout_with_buffer;
|
2011-04-28 21:26:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get statistics on the re-formatted volume */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-04-28 21:26:46 +02:00
|
|
|
ret = nxffs_blockstats(volume, &stats);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to collect block statistics: %d\n", -ret);
|
2011-05-03 02:02:10 +02:00
|
|
|
goto errout_with_buffer;
|
2011-04-28 21:26:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-12-03 00:19:22 +01:00
|
|
|
#endif /* CONFIG_NXFFS_SCAN_VOLUME */
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/* Get the file system limits */
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
ret = nxffs_limits(volume);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2013-12-03 00:19:22 +01:00
|
|
|
/* We may need to format the volume. Try that before giving up. */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
fwarn("WARNING: Failed to calculate file system limits: %d\n", -ret);
|
2013-12-03 00:19:22 +01:00
|
|
|
ret = nxffs_reformat(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to reformat the volume: %d\n", -ret);
|
2013-12-03 00:19:22 +01:00
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get statistics on the re-formatted volume */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_NXFFS_SCAN_VOLUME) && defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2013-12-03 00:19:22 +01:00
|
|
|
ret = nxffs_blockstats(volume, &stats);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to collect block statistics: %d\n", -ret);
|
2013-12-03 00:19:22 +01:00
|
|
|
goto errout_with_buffer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now try to get the file system limits again */
|
|
|
|
|
|
|
|
ret = nxffs_limits(volume);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now give up */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to calculate file system limits: %d\n", -ret);
|
2011-04-28 21:26:46 +02:00
|
|
|
|
2011-05-03 02:02:10 +02:00
|
|
|
errout_with_buffer:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(volume->pack);
|
2011-05-03 02:02:10 +02:00
|
|
|
errout_with_cache:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(volume->cache);
|
2011-04-28 21:26:46 +02:00
|
|
|
errout_with_volume:
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifndef CONFIG_NXFFS_PREALLOCATED
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(volume);
|
2011-09-28 20:24:00 +02:00
|
|
|
#endif
|
2011-04-28 21:26:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_limits
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Recalculate file system limits: (1) the FLASH offset to the first,
|
|
|
|
* valid inode, and (2) the FLASH offset to the first, unused byte after
|
|
|
|
* the last inode (invalid or not).
|
|
|
|
*
|
|
|
|
* The first, lower limit must be recalculated: (1) initially, (2)
|
|
|
|
* whenever the first inode is deleted, or (3) whenever inode is moved
|
2011-04-30 19:29:40 +02:00
|
|
|
* as part of the file system packing operation.
|
2011-04-28 21:26:46 +02:00
|
|
|
*
|
|
|
|
* The second, upper limit must be (1) incremented whenever new file
|
2011-04-30 19:29:40 +02:00
|
|
|
* data is written, or (2) recalculated as part of the file system packing
|
|
|
|
* operation.
|
2011-04-28 21:26:46 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Identifies the NXFFS volume
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success. Otherwise, a negated error is returned indicating the
|
|
|
|
* nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_limits(FAR struct nxffs_volume_s *volume)
|
|
|
|
{
|
2011-04-28 23:05:39 +02:00
|
|
|
FAR struct nxffs_entry_s entry;
|
|
|
|
off_t block;
|
|
|
|
off_t offset;
|
|
|
|
bool noinodes = false;
|
|
|
|
int nerased;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the offset to the first valid block on the FLASH */
|
|
|
|
|
|
|
|
block = 0;
|
|
|
|
ret = nxffs_validblock(volume, &block);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to find a valid block: %d\n", -ret);
|
2011-04-28 23:05:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then find the first valid inode in or beyond the first valid block */
|
|
|
|
|
|
|
|
offset = block * volume->geo.blocksize;
|
|
|
|
ret = nxffs_nextentry(volume, offset, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The value -ENOENT is special. This simply means that the FLASH
|
|
|
|
* was searched to the end and no valid inode was found... the file
|
|
|
|
* system is empty (or, in more perverse cases, all inodes are
|
|
|
|
* deleted or corrupted).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ENOENT)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_nextentry failed: %d\n", -ret);
|
2011-04-28 23:05:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set a flag the just indicates that no inodes were found. Later,
|
|
|
|
* we will set the location of the first inode to be the same as
|
|
|
|
* the location of the free FLASH region.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("No inodes found\n");
|
2011-04-28 23:05:39 +02:00
|
|
|
noinodes = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the offset to the first inode */
|
|
|
|
|
|
|
|
volume->inoffset = entry.hoffset;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("First inode at offset %d\n", volume->inoffset);
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-05 20:21:44 +02:00
|
|
|
/* Discard this entry and set the next offset. */
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-05 20:21:44 +02:00
|
|
|
offset = nxffs_inodeend(volume, &entry);
|
2011-04-28 23:05:39 +02:00
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, search for the last valid entry */
|
|
|
|
|
|
|
|
if (!noinodes)
|
|
|
|
{
|
2014-11-25 20:46:14 +01:00
|
|
|
while (nxffs_nextentry(volume, offset, &entry) == OK)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2011-05-05 20:21:44 +02:00
|
|
|
/* Discard the entry and guess the next offset. */
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-05 20:21:44 +02:00
|
|
|
offset = nxffs_inodeend(volume, &entry);
|
2013-12-02 01:22:33 +01:00
|
|
|
nxffs_freeentry(&entry);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
2013-12-03 00:19:22 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Last inode before offset %d\n", offset);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No inodes were found after this offset. Now search for a block of
|
|
|
|
* erased flash.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, offset);
|
|
|
|
nerased = 0;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2011-05-04 23:59:23 +02:00
|
|
|
int ch = nxffs_getc(volume, 1);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ch < 0)
|
|
|
|
{
|
|
|
|
/* Failed to read the next byte... this could mean that the FLASH
|
|
|
|
* is full?
|
|
|
|
*/
|
|
|
|
|
2011-10-31 18:04:03 +01:00
|
|
|
if (volume->ioblock + 1 >= volume->nblocks &&
|
|
|
|
volume->iooffset + 1 >= volume->geo.blocksize)
|
|
|
|
{
|
2020-07-02 16:03:27 +02:00
|
|
|
/* Yes.. the FLASH is full. Force the offsets to the end of
|
|
|
|
* FLASH
|
|
|
|
*/
|
2011-10-31 18:04:03 +01:00
|
|
|
|
|
|
|
volume->froffset = volume->nblocks * volume->geo.blocksize;
|
2020-07-02 16:03:27 +02:00
|
|
|
finfo("Assume no free FLASH, froffset: %d\n",
|
|
|
|
volume->froffset);
|
2011-10-31 18:04:03 +01:00
|
|
|
if (noinodes)
|
|
|
|
{
|
|
|
|
volume->inoffset = volume->froffset;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("No inodes, inoffset: %d\n", volume->inoffset);
|
2011-10-31 18:04:03 +01:00
|
|
|
}
|
2013-12-03 00:19:22 +01:00
|
|
|
|
2011-10-31 18:04:03 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
/* No? Then it is some other failure that we do not know how to
|
|
|
|
* handle
|
|
|
|
*/
|
2011-10-31 18:04:03 +01:00
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_getc failed: %d\n", -ch);
|
2011-04-28 23:05:39 +02:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for another erased byte */
|
|
|
|
|
|
|
|
else if (ch == CONFIG_NXFFS_ERASEDSTATE)
|
|
|
|
{
|
|
|
|
/* If we have encountered NXFFS_NERASED number of consecutive
|
|
|
|
* erased bytes, then presume we have reached the end of valid
|
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (++nerased >= NXFFS_NERASED)
|
|
|
|
{
|
|
|
|
/* Okay.. we have a long stretch of erased FLASH in a valid
|
|
|
|
* FLASH block. Let's say that this is the beginning of
|
|
|
|
* the free FLASH region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
volume->froffset = offset;
|
2020-07-02 16:03:27 +02:00
|
|
|
finfo("Free FLASH region begins at offset: %d\n",
|
|
|
|
volume->froffset);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (noinodes)
|
|
|
|
{
|
|
|
|
volume->inoffset = offset;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("First inode at offset %d\n", volume->inoffset);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
2013-12-03 00:19:22 +01:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-17 10:34:38 +02:00
|
|
|
volume->ioblock += 1;
|
|
|
|
volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
|
|
|
|
|
|
|
|
offset = volume->ioblock * volume->geo.blocksize +
|
|
|
|
volume->iooffset;
|
2011-04-28 23:05:39 +02:00
|
|
|
nerased = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Won't get here */
|
|
|
|
|
|
|
|
return OK;
|
2011-04-28 21:26:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 22:59:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements a portion of the mount operation. Normmally,
|
|
|
|
* the bind() method allocates and initializes the mountpoint private data
|
|
|
|
* then binds the blockdriver inode to the filesystem private data. The
|
|
|
|
* final binding of the private data (containing the blockdriver) to the
|
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
* For the NXFFS, this sequence is quite different for the following
|
|
|
|
* reasons:
|
|
|
|
*
|
|
|
|
* 1. A block driver is not used. Instead, an MTD instance was provided
|
|
|
|
* to nxfs_initialize prior to mounting. So, in this sense, the NXFFS
|
|
|
|
* file system is already bound.
|
|
|
|
*
|
|
|
|
* 2. Since the volume was already bound to the MTD driver, all allocations
|
|
|
|
* and initializations have already been performed. Essentially, all
|
|
|
|
* mount operations have been bypassed and now we just need to provide
|
|
|
|
* the pre-allocated volume instance.
|
|
|
|
*
|
|
|
|
* 3. The tricky thing is that there is no mechanism to associate multiple
|
|
|
|
* NXFFS volumes to the multiple volumes bound to different MTD drivers.
|
|
|
|
* Hence, the limitation of a single NXFFS volume.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
|
|
|
{
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifndef CONFIG_NXFFS_PREALLOCATED
|
2011-04-29 22:59:40 +02:00
|
|
|
# error "No design to support dynamic allocation of volumes"
|
|
|
|
#else
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
/* If CONFIG_NXFFS_PREALLOCATED is defined, then this is the single, pre-
|
2011-04-29 22:59:40 +02:00
|
|
|
* allocated NXFFS volume instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(g_volume.cache);
|
|
|
|
*handle = &g_volume;
|
|
|
|
#endif
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-15 00:22:02 +01:00
|
|
|
int nxffs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
2011-04-29 22:59:40 +02:00
|
|
|
{
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifndef CONFIG_NXFFS_PREALLOCATED
|
2011-04-30 19:29:40 +02:00
|
|
|
# error "No design to support dynamic allocation of volumes"
|
|
|
|
#else
|
2015-03-15 00:22:02 +01:00
|
|
|
/* This implementation currently only supports unmounting if there are no
|
|
|
|
* open file references.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (flags != 0)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
return g_volume.ofiles ? -EBUSY : OK;
|
|
|
|
#endif
|
2011-04-29 22:59:40 +02:00
|
|
|
}
|