mtd: fix some unallocated and NULL pointer issues. rwb->wrflush and rwb->wrmaxblocks in rwbuffer could get unallocated values from ftl_initialize() in some configurations. Also fixes related assert:

up_assert: Assertion failed at file:rwbuffer.c line: 643

that can happen with the following configuration:

  CONFIG_FTL_WRITEBUFFER=y
  CONFIG_DRVR_WRITEBUFFER=y
  # CONFIG_FS_WRITABLE is not set

These problems are caused by CONFIG variable differences between the buffer layers. TODO: This is not a perfect solution. readahead support has similar issues.
This commit is contained in:
Juha Niskanen 2017-05-11 07:22:21 -06:00 committed by Gregory Nutt
parent 58a0b09b82
commit 0f7210b0ae
3 changed files with 45 additions and 37 deletions

View File

@ -29,7 +29,7 @@ config MTD_PARTITION
config FTL_WRITEBUFFER
bool "Enable write buffering in the FTL layer"
default n
depends on DRVR_WRITEBUFFER
depends on DRVR_WRITEBUFFER && FS_WRITABLE
config FTL_READAHEAD
bool "Enable read-ahead buffering in the FTL layer"

View File

@ -528,7 +528,7 @@ int ftl_initialize(int minor, FAR struct mtd_dev_s *mtd)
/* Allocate a FTL device structure */
dev = (struct ftl_struct_s *)kmm_malloc(sizeof(struct ftl_struct_s));
dev = (struct ftl_struct_s *)kmm_zalloc(sizeof(struct ftl_struct_s));
if (dev)
{
/* Initialize the FTL device structure */
@ -586,6 +586,9 @@ int ftl_initialize(int minor, FAR struct mtd_dev_s *mtd)
if (ret < 0)
{
ferr("ERROR: rwb_initialize failed: %d\n", ret);
#ifdef CONFIG_FS_WRITABLE
kmm_free(dev->eblock);
#endif
kmm_free(dev);
return ret;
}
@ -601,6 +604,9 @@ int ftl_initialize(int minor, FAR struct mtd_dev_s *mtd)
if (ret < 0)
{
ferr("ERROR: register_blockdriver failed: %d\n", -ret);
#ifdef CONFIG_FS_WRITABLE
kmm_free(dev->eblock);
#endif
kmm_free(dev);
}
}

View File

@ -357,57 +357,60 @@ FAR struct mtd_dev_s *mtd_rwb_initialize(FAR struct mtd_dev_s *mtd)
*/
priv = (FAR struct mtd_rwbuffer_s *)kmm_zalloc(sizeof(struct mtd_rwbuffer_s));
if (priv)
if (!priv)
{
/* Initialize the allocated structure. (unsupported methods/fields
* were already nullified by kmm_zalloc).
*/
ferr("ERROR: Failed to allocate mtd_rwbuffer\n");
return NULL;
}
priv->mtd.erase = mtd_erase; /* Our MTD erase method */
priv->mtd.bread = mtd_bread; /* Our MTD bread method */
priv->mtd.bwrite = mtd_bwrite; /* Our MTD bwrite method */
priv->mtd.read = mtd_read; /* Our MTD read method */
priv->mtd.ioctl = mtd_ioctl; /* Our MTD ioctl method */
/* Initialize the allocated structure. (unsupported methods/fields
* were already nullified by kmm_zalloc).
*/
priv->dev = mtd; /* The contained MTD instance */
priv->mtd.erase = mtd_erase; /* Our MTD erase method */
priv->mtd.bread = mtd_bread; /* Our MTD bread method */
priv->mtd.bwrite = mtd_bwrite; /* Our MTD bwrite method */
priv->mtd.read = mtd_read; /* Our MTD read method */
priv->mtd.ioctl = mtd_ioctl; /* Our MTD ioctl method */
/* Sectors per block. The erase block size must be an even multiple
* of the sector size.
*/
priv->dev = mtd; /* The contained MTD instance */
priv->spb = geo.erasesize / geo.blocksize;
DEBUGASSERT((size_t)priv->spb * geo.blocksize == geo.erasesize);
/* Sectors per block. The erase block size must be an even multiple
* of the sector size.
*/
/* Values must be provided to rwb_initialize() */
/* Supported geometry */
priv->spb = geo.erasesize / geo.blocksize;
DEBUGASSERT((size_t)priv->spb * geo.blocksize == geo.erasesize);
priv->rwb.blocksize = geo.blocksize;
priv->rwb.nblocks = geo.neraseblocks * priv->spb;
/* Values must be provided to rwb_initialize() */
/* Supported geometry */
/* Buffer setup */
priv->rwb.blocksize = geo.blocksize;
priv->rwb.nblocks = geo.neraseblocks * priv->spb;
/* Buffer setup */
#ifdef CONFIG_DRVR_WRITEBUFFER
priv->rwb.wrmaxblocks = CONFIG_MTD_NWRBLOCKS;
priv->rwb.wrmaxblocks = CONFIG_MTD_NWRBLOCKS;
#endif
#ifdef CONFIG_DRVR_READAHEAD
priv->rwb.rhmaxblocks = CONFIG_MTD_NRDBLOCKS;
priv->rwb.rhmaxblocks = CONFIG_MTD_NRDBLOCKS;
#endif
/* Callouts */
/* Callouts */
priv->rwb.dev = priv; /* Device state passed to callouts */
priv->rwb.wrflush = mtd_flush; /* Callout to flush buffer */
priv->rwb.rhreload = mtd_reload; /* Callout to reload buffer */
priv->rwb.dev = priv; /* Device state passed to callouts */
priv->rwb.wrflush = mtd_flush; /* Callout to flush buffer */
priv->rwb.rhreload = mtd_reload; /* Callout to reload buffer */
/* Initialize read-ahead/write buffering */
/* Initialize read-ahead/write buffering */
ret = rwb_initialize(&priv->rwb);
if (ret < 0)
{
ferr("ERROR: rwb_initialize failed: %d\n", ret);
kmm_free(priv);
return NULL;
}
ret = rwb_initialize(&priv->rwb);
if (ret < 0)
{
ferr("ERROR: rwb_initialize failed: %d\n", ret);
kmm_free(priv);
return NULL;
}
/* Register the MTD with the procfs system if enabled */
@ -418,7 +421,6 @@ FAR struct mtd_dev_s *mtd_rwb_initialize(FAR struct mtd_dev_s *mtd)
/* Return the implementation-specific state structure as the MTD device */
finfo("Return %p\n", priv);
return &priv->mtd;
}