fs: add fs dump in ROMFS/LITTLEFS

add read/write double check in ROMFS/LITTLEFS file system

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
This commit is contained in:
zhaoxingyu1
2025-02-21 20:15:17 +08:00
committed by Xiang Xiao
parent 18820ef07d
commit f40dfc7234
4 changed files with 104 additions and 0 deletions
+6
View File
@@ -187,4 +187,10 @@ config FS_LITTLEFS_DISK_VERSION
0x00020000 means 2.0.
0x00020001 means 2.1.
config FS_LITTLEFS_DEBUG
bool "LITTLEFS file system read/write double check"
depends on DEBUG_FS_ERROR
---help---
Enable LITTLEFS file system read/write double check.
endif
+50
View File
@@ -30,6 +30,7 @@
#include <fcntl.h>
#include <string.h>
#include <nuttx/crc16.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/kmalloc.h>
@@ -1153,6 +1154,49 @@ static int littlefs_rewinddir(FAR struct inode *mountpt,
return ret;
}
/****************************************************************************
* Name: littlefs_check
****************************************************************************/
#ifdef CONFIG_FS_LITTLEFS_DEBUG
static void littlefs_check(FAR struct inode *drv, lfs_block_t block,
lfs_size_t size, FAR const void *buffer,
FAR struct mtd_geometry_s *geo)
{
uint16_t crc16_check;
uint16_t crc16_ori;
int ret;
crc16_ori = crc16(buffer, geo->blocksize * size);
if (INODE_IS_MTD(drv))
{
ret = MTD_BREAD(drv->u.i_mtd, block, size, (FAR void *)buffer);
}
else
{
ret = drv->u.i_bops->read(drv, (FAR void *)buffer, block, size);
}
if (ret < 0)
{
ferr("[%s] read block failed!\n", __func__);
lib_dumpbuffer("buffer:", buffer, geo->blocksize * size);
DEBUGASSERT(0);
}
else
{
crc16_check = crc16(buffer, geo->blocksize * size);
if (crc16_ori != crc16_check)
{
ferr("CRC16 check failed\n");
lib_dumpbuffer("check:", buffer, geo->blocksize * size);
DEBUGASSERT(0);
}
}
}
#endif
/****************************************************************************
* Name: littlefs_read_block
****************************************************************************/
@@ -1178,6 +1222,9 @@ static int littlefs_read_block(FAR const struct lfs_config *c,
ret = drv->u.i_bops->read(drv, buffer, block, size);
}
#ifdef CONFIG_FS_LITTLEFS_DEBUG
littlefs_check(drv, block, size, buffer, geo);
#endif
return ret >= 0 ? OK : ret;
}
@@ -1211,6 +1258,9 @@ static int littlefs_write_block(FAR const struct lfs_config *c,
ret = drv->u.i_bops->write(drv, buffer, block, size);
}
#ifdef CONFIG_FS_LITTLEFS_DEBUG
littlefs_check(drv, block, size, buffer, geo);
#endif
return ret >= 0 ? OK : ret;
}
+6
View File
@@ -27,4 +27,10 @@ config FS_ROMFS_CACHE_FILE_NSECTORS
---help---
The number of file cache sector
config FS_ROMFS_DEBUG
bool "ROMFS file system read/write double check"
depends on DEBUG_FS_ERROR
---help---
Enable ROMFS file system read/write double check.
endif
+42
View File
@@ -36,6 +36,7 @@
#include <errno.h>
#include <debug.h>
#include <nuttx/crc16.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/ioctl.h>
@@ -508,6 +509,41 @@ static int romfs_cachenode(FAR struct romfs_mountpt_s *rm,
}
#endif
/****************************************************************************
* Name: romfs_check
****************************************************************************/
#ifdef CONFIG_FS_ROMFS_DEBUG
static void romfs_check(FAR struct inode *inode, uint32_t sector,
unsigned int nsectors, FAR uint8_t *buffer,
uint16_t sectorsize)
{
uint16_t crc16_check;
uint16_t crc16_ori;
ssize_t size;
crc16_ori = crc16(buffer, sectorsize * nsectors);
size = inode->u.i_bops->read(inode, buffer, sector, nsectors);
if (size != (ssize_t)nsectors)
{
ferr("Read failed: %zd\n", size);
lib_dumpbuffer("buffer:", buffer, sectorsize * nsectors);
DEBUGASSERT(0);
}
else
{
crc16_check = crc16(buffer, sectorsize * nsectors);
if (crc16_ori != crc16_check)
{
ferr("CRC16 check failed\n");
lib_dumpbuffer("check:", buffer, sectorsize * nsectors);
DEBUGASSERT(0);
}
}
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -550,6 +586,12 @@ int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
{
ret = -EINVAL;
}
#ifdef CONFIG_FS_ROMFS_DEBUG
else
{
romfs_check(inode, sector, nsectors, buffer, rm->rm_hwsectorsize);
}
#endif
}
return ret;