Skip to content

Commit

Permalink
ftl: should pre-allocate eblock for car case
Browse files Browse the repository at this point in the history
Signed-off-by: wanggang26 <[email protected]>
  • Loading branch information
wanggang26 committed Sep 15, 2024
1 parent fc50e57 commit a2b797a
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions drivers/mtd/ftl.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ static int ftl_open(FAR struct inode *inode)
DEBUGASSERT(inode->i_private);
dev = inode->i_private;

if (dev->refs == 0)
{
/* Allocate one, in-memory erase block buffer */

dev->eblock = kmm_malloc(dev->geo.erasesize);
if (!dev->eblock)
{
ferr("ERROR: Failed to allocate an erase block buffer\n");
return -ENOMEM;
}
}

dev->refs++;
return OK;
}
Expand All @@ -237,17 +249,20 @@ static int ftl_close(FAR struct inode *inode)
rwb_flush(&dev->rwb);
#endif

if (--dev->refs == 0 && dev->unlinked)
if (--dev->refs == 0)
{
#ifdef FTL_HAVE_RWBUFFER
rwb_uninitialize(&dev->rwb);
#endif
if (dev->eblock)
{
kmm_free(dev->eblock);
}

kmm_free(dev);
if (dev->unlinked)
{
#ifdef FTL_HAVE_RWBUFFER
rwb_uninitialize(&dev->rwb);
#endif
kmm_free(dev);
}
}

return OK;
Expand Down Expand Up @@ -461,18 +476,6 @@ static ssize_t ftl_read(FAR struct inode *inode, unsigned char *buffer,
*
****************************************************************************/

static int ftl_alloc_eblock(FAR struct ftl_struct_s *dev)
{
if (dev->eblock == NULL)
{
/* Allocate one, in-memory erase block buffer */

dev->eblock = kmm_malloc(dev->geo.erasesize);
}

return dev->eblock != NULL ? OK : -ENOMEM;
}

static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,
off_t startblock, size_t nblocks)
{
Expand Down Expand Up @@ -504,13 +507,6 @@ static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,

bool short_write = (remaining < (alignedblock - startblock));

ret = ftl_alloc_eblock(dev);
if (ret < 0)
{
ferr("ERROR: Failed to allocate an erase block buffer\n");
return ret;
}

/* Read the full erase block into the buffer */

rwblock = startblock & ~mask;
Expand Down Expand Up @@ -604,13 +600,6 @@ static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,

if (remaining > 0)
{
ret = ftl_alloc_eblock(dev);
if (ret < 0)
{
ferr("ERROR: Failed to allocate an erase block buffer\n");
return ret;
}

/* Read the full erase block into the buffer */

nxfrd = ftl_mtd_bread(dev, alignedblock, dev->blkper, dev->eblock);
Expand Down Expand Up @@ -767,10 +756,6 @@ static int ftl_unlink(FAR struct inode *inode)
#ifdef FTL_HAVE_RWBUFFER
rwb_uninitialize(&dev->rwb);
#endif
if (dev->eblock)
{
kmm_free(dev->eblock);
}

kmm_free(dev);
}
Expand Down

0 comments on commit a2b797a

Please sign in to comment.