Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions fs/driver/fs_blockproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,17 @@ static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
* attempt to open() the file.
*
* Input Parameters:
* None
* devbuf - Buffer to store the generated device name
* len - Length of the buffer
*
* Returned Value:
* The allocated path to the device. This must be released by the caller
* to prevent memory links. NULL will be returned only the case where
* we fail to allocate memory.
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/

static FAR char *unique_chardev(void)
static int unique_chardev(FAR char *devbuf, size_t len)
{
struct stat statbuf;
char devbuf[16];
uint32_t devno;
int ret;

Expand All @@ -95,7 +93,7 @@ static FAR char *unique_chardev(void)
if (ret < 0)
{
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
return NULL;
return ret;
}

/* Get the next device number and release the semaphore */
Expand All @@ -106,16 +104,15 @@ static FAR char *unique_chardev(void)
/* Construct the full device number */

devno &= 0xffffff;
snprintf(devbuf, sizeof(devbuf), "/dev/tmpc%06lx",
(unsigned long)devno);
snprintf(devbuf, len, "/dev/tmpc%06lx", (unsigned long)devno);

/* Make sure that file name is not in use */

ret = nx_stat(devbuf, &statbuf, 1);
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return fs_heap_strdup(devbuf);
return OK;
}

/* It is in use, try again */
Expand Down Expand Up @@ -147,19 +144,19 @@ static FAR char *unique_chardev(void)

int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
{
char chardev[16];
struct file temp;
FAR char *chardev;
int ret;

DEBUGASSERT(blkdev);

/* Create a unique temporary file name for the character device */

chardev = unique_chardev();
if (chardev == NULL)
ret = unique_chardev(chardev, sizeof(chardev));
if (ret != OK)
{
ferr("ERROR: Failed to create temporary device name\n");
return -ENOMEM;
return ret;
}

/* Wrap the block driver with an instance of the BCH driver */
Expand All @@ -169,8 +166,7 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
{
ferr("ERROR: bchdev_register(%s, %s) failed: %d\n",
blkdev, chardev, ret);

goto errout_with_chardev;
return ret;
}

/* Open the newly created character driver */
Expand All @@ -180,15 +176,17 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
if (ret < 0)
{
ferr("ERROR: Failed to open %s: %d\n", chardev, ret);
goto errout_with_bchdev;
nx_unlink(chardev);
return ret;
}

ret = file_dup2(&temp, filep);
file_close(&temp);
if (ret < 0)
{
ferr("ERROR: Failed to dup2%s: %d\n", chardev, ret);
goto errout_with_bchdev;
nx_unlink(chardev);
return ret;
}

/* Unlink the character device name. The driver instance will persist,
Expand All @@ -200,19 +198,8 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
if (ret < 0)
{
ferr("ERROR: Failed to unlink %s: %d\n", chardev, ret);
goto errout_with_chardev;
}

/* Free the allocated character driver name. */

fs_heap_free(chardev);
return OK;

errout_with_bchdev:
nx_unlink(chardev);

errout_with_chardev:
fs_heap_free(chardev);
return ret;
}

Expand Down
43 changes: 21 additions & 22 deletions fs/driver/fs_mtdproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <nuttx/mutex.h>

#include "driver/driver.h"
#include "fs_heap.h"

/****************************************************************************
* Private Data
Expand All @@ -62,19 +61,17 @@ static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
* attempt to open() the file.
*
* Input Parameters:
* None
* devbuf - Buffer to store the generated device name
* len - Length of the buffer
*
* Returned Value:
* The allocated path to the device. This must be released by the caller
* to prevent memory links. NULL will be returned only the case where
* we fail to allocate memory.
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/

static FAR char *unique_blkdev(void)
static int unique_blkdev(FAR char *devbuf, size_t len)
{
struct stat statbuf;
char devbuf[16];
uint32_t devno;
int ret;

Expand All @@ -88,7 +85,7 @@ static FAR char *unique_blkdev(void)
if (ret < 0)
{
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
return NULL;
return ret;
}

/* Get the next device number and release the semaphore */
Expand All @@ -99,16 +96,15 @@ static FAR char *unique_blkdev(void)
/* Construct the full device number */

devno &= 0xffffff;
snprintf(devbuf, sizeof(devbuf), "/dev/tmpb%06lx",
(unsigned long)devno);
snprintf(devbuf, len, "/dev/tmpb%06lx", (unsigned long)devno);

/* Make sure that file name is not in use */

ret = nx_stat(devbuf, &statbuf, 1);
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return fs_heap_strdup(devbuf);
return OK;
}

/* It is in use, try again */
Expand Down Expand Up @@ -143,17 +139,17 @@ static FAR char *unique_blkdev(void)
int mtd_proxy(FAR const char *mtddev, int mountflags,
FAR struct inode **ppinode)
{
char blkdev[16];
FAR struct inode *mtd;
FAR char *blkdev;
int ret;

/* Create a unique temporary file name for the block device */

blkdev = unique_blkdev();
if (blkdev == NULL)
ret = unique_blkdev(blkdev, sizeof(blkdev));
if (ret != OK)
{
ferr("ERROR: Failed to create temporary device name\n");
return -ENOMEM;
return ret;
}

/* Wrap the mtd driver with an instance of the ftl driver */
Expand All @@ -162,7 +158,7 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
if (ret < 0)
{
ferr("ERROR: Failed to find %s mtd driver\n", mtddev);
goto out_with_blkdev;
return ret;
}

ret = ftl_initialize_by_path(blkdev, mtd->u.i_mtd, mountflags);
Expand All @@ -171,7 +167,7 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
{
ferr("ERROR: ftl_initialize_by_path(%s, %s) failed: %d\n",
mtddev, blkdev, ret);
goto out_with_blkdev;
return ret;
}

/* Open the newly created block driver */
Expand All @@ -180,17 +176,20 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
if (ret < 0)
{
ferr("ERROR: Failed to open %s: %d\n", blkdev, ret);
goto out_with_fltdev;
nx_unlink(blkdev);
return ret;
}

/* Unlink and free the block device name. The driver instance will
* persist, provided that CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y (otherwise,
* we have a problem here!)
*/

out_with_fltdev:
nx_unlink(blkdev);
out_with_blkdev:
fs_heap_free(blkdev);
ret = nx_unlink(blkdev);
if (ret < 0)
{
ferr("ERROR: Failed to unlink %s: %d\n", blkdev, ret);
}

return ret;
}
Loading