fs/romfs: Call block_operations::close when romfs_bind fail

to avoid the resource leak

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2024-02-04 06:22:20 +08:00 committed by GUIDINGLI
parent 79be76a22c
commit f838987e44
2 changed files with 17 additions and 12 deletions

View File

@ -1090,12 +1090,11 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
return -ENODEV;
}
if (INODE_IS_BLOCK(blkdriver) &&
blkdriver->u.i_bops->open != NULL &&
blkdriver->u.i_bops->open(blkdriver) != OK)
if (blkdriver->u.i_bops->open != NULL &&
(ret = blkdriver->u.i_bops->open(blkdriver)) != OK)
{
ferr("ERROR: No open method\n");
return -ENODEV;
return ret;
}
/* Create an instance of the mountpt state structure */
@ -1104,7 +1103,8 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
if (!rm)
{
ferr("ERROR: Failed to allocate mountpoint structure\n");
return -ENOMEM;
ret = -ENOMEM;
goto errout;
}
/* Initialize the allocated mountpt state structure. The filesystem is
@ -1121,7 +1121,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
if (ret < 0)
{
ferr("ERROR: romfs_hwconfigure failed: %d\n", ret);
goto errout;
goto errout_with_mount;
}
/* Then complete the mount by getting the ROMFS configuratrion from
@ -1146,9 +1146,16 @@ errout_with_buffer:
fs_heap_free(rm->rm_buffer);
}
errout:
errout_with_mount:
nxrmutex_destroy(&rm->rm_lock);
fs_heap_free(rm);
errout:
if (blkdriver->u.i_bops->close != NULL)
{
blkdriver->u.i_bops->close(blkdriver);
}
return ret;
}
@ -1193,7 +1200,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
* no open file references.
*/
ret = (flags != 0) ? -ENOSYS : -EBUSY;
ret = flags ? -ENOSYS : -EBUSY;
}
else
{
@ -1224,7 +1231,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
/* Release the mountpoint private data */
if (!rm->rm_xipbase && rm->rm_buffer)
if (!rm->rm_xipbase)
{
fs_heap_free(rm->rm_buffer);
}

View File

@ -536,9 +536,7 @@ int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
/* In non-XIP mode, we have to read the data from the device */
FAR struct inode *inode = rm->rm_blkdriver;
ssize_t nsectorsread = -ENODEV;
nsectorsread =
ssize_t nsectorsread =
inode->u.i_bops->read(inode, buffer, sector, nsectors);
if (nsectorsread == (ssize_t)nsectors)