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