bch: fix sector buffer invalidation issue

This commit is contained in:
wanggang26 2023-07-29 10:37:17 +08:00 committed by Xiang Xiao
parent e3d27444ef
commit 024440663d
5 changed files with 15 additions and 12 deletions

View File

@ -81,7 +81,7 @@ EXTERN const struct file_operations g_bch_fops;
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
EXTERN int bchlib_flushsector(FAR struct bchlib_s *bch); EXTERN int bchlib_flushsector(FAR struct bchlib_s *bch, bool discard);
EXTERN int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector); EXTERN int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector);
#undef EXTERN #undef EXTERN

View File

@ -168,7 +168,7 @@ static int bch_close(FAR struct file *filep)
/* Flush any dirty pages remaining in the cache */ /* Flush any dirty pages remaining in the cache */
bchlib_flushsector(bch); bchlib_flushsector(bch, false);
/* Decrement the reference count (I don't use bchlib_decref() because I /* Decrement the reference count (I don't use bchlib_decref() because I
* want the entire close operation to be atomic wrt other driver * want the entire close operation to be atomic wrt other driver
@ -424,7 +424,7 @@ static int bch_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{ {
/* Flush any dirty pages remaining in the cache */ /* Flush any dirty pages remaining in the cache */
ret = bchlib_flushsector(bch); ret = bchlib_flushsector(bch, false);
} }
break; break;

View File

@ -103,7 +103,7 @@ static int bch_cypher(FAR struct bchlib_s *bch, int encrypt)
* *
****************************************************************************/ ****************************************************************************/
int bchlib_flushsector(FAR struct bchlib_s *bch) int bchlib_flushsector(FAR struct bchlib_s *bch, bool discard)
{ {
FAR struct inode *inode; FAR struct inode *inode;
ssize_t ret = OK; ssize_t ret = OK;
@ -144,6 +144,11 @@ int bchlib_flushsector(FAR struct bchlib_s *bch)
bch->dirty = false; bch->dirty = false;
} }
if (discard)
{
bch->sector = (size_t)-1;
}
return (int)ret; return (int)ret;
} }
@ -151,7 +156,7 @@ int bchlib_flushsector(FAR struct bchlib_s *bch)
* Name: bchlib_readsector * Name: bchlib_readsector
* *
* Description: * Description:
* Flush the current contents of the sector buffer (if dirty) * Read the current sector contents into buffer
* *
* Assumptions: * Assumptions:
* Caller must assume mutual exclusion * Caller must assume mutual exclusion
@ -167,15 +172,13 @@ int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector)
{ {
inode = bch->inode; inode = bch->inode;
ret = bchlib_flushsector(bch); ret = bchlib_flushsector(bch, true);
if (ret < 0) if (ret < 0)
{ {
ferr("Flush failed: %zd\n", ret); ferr("Flush failed: %zd\n", ret);
return (int)ret; return (int)ret;
} }
bch->sector = (size_t)-1;
ret = inode->u.i_bops->read(inode, bch->buffer, sector, 1); ret = inode->u.i_bops->read(inode, bch->buffer, sector, 1);
if (ret < 0) if (ret < 0)
{ {

View File

@ -43,8 +43,7 @@
* Name: bchlib_teardown * Name: bchlib_teardown
* *
* Description: * Description:
* Setup so that the block driver referenced by 'blkdev' can be accessed * Close the block driver and free resources
* similar to a character device.
* *
****************************************************************************/ ****************************************************************************/
@ -63,7 +62,7 @@ int bchlib_teardown(FAR void *handle)
/* Flush any pending data to the block driver */ /* Flush any pending data to the block driver */
bchlib_flushsector(bch); bchlib_flushsector(bch, false);
/* Close the block driver */ /* Close the block driver */

View File

@ -133,7 +133,8 @@ ssize_t bchlib_write(FAR void *handle, FAR const char *buffer, size_t offset,
/* Flush the dirty sector to keep the sector sequence */ /* Flush the dirty sector to keep the sector sequence */
ret = bchlib_flushsector(bch); ret = bchlib_flushsector(bch, sector <= bch->sector &&
bch->sector < (sector + nsectors));
if (ret < 0) if (ret < 0)
{ {
ferr("ERROR: Flush failed: %d\n", ret); ferr("ERROR: Flush failed: %d\n", ret);