[PATCH 4/4] fs: fat: flush metadata updates to the block device

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Aug 25 03:10:56 PDT 2026


The disk_ioctl glue that barebox provides can take a CTRL_SYNC command,
but barebox implemented it as a no-op.

Instead, various file system callbacks did flushing manually instead
of having fat.c do it via disk_ioctl.

Implement disk_ioctl(..., CTRL_SYNC) and drop the now superfluous
flushes.

Assisted-by: Codex:gpt-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/fat/diskio.h     |  1 +
 fs/fat/fat-diskio.c |  4 ++++
 fs/fat/fat.c        | 46 ++++++++++++++-------------------------------
 3 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/fs/fat/diskio.h b/fs/fat/diskio.h
index 04a587e3bc47..86fa0f21e365 100644
--- a/fs/fat/diskio.h
+++ b/fs/fat/diskio.h
@@ -40,6 +40,7 @@ DSTATUS disk_status (FATFS *fatfs);
 DRESULT disk_read (FATFS *fatfs, BYTE*, DWORD, BYTE);
 #if	_READONLY == 0
 DRESULT disk_write (FATFS *fatfs, const BYTE*, DWORD, BYTE);
+DRESULT disk_flush (FATFS *fatfs);
 #endif
 DRESULT disk_ioctl (FATFS *fatfs, BYTE, void*);
 
diff --git a/fs/fat/fat-diskio.c b/fs/fat/fat-diskio.c
index 6ba18993b870..8e26b6b1e9bb 100644
--- a/fs/fat/fat-diskio.c
+++ b/fs/fat/fat-diskio.c
@@ -28,7 +28,11 @@ DRESULT disk_ioctl (FATFS *fat, BYTE command, void *buf)
 		*(WORD *)buf = disk_sector_size(fat);
 		return RES_OK;
 	case CTRL_SYNC:
+#if	_READONLY == 0
+		return disk_flush(fat);
+#else
 		return RES_OK;
+#endif
 	default:
 		return RES_PARERR;
 	}
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index b50867524a74..c2e6969f8109 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -78,6 +78,13 @@ DRESULT disk_write(FATFS *fat, const BYTE *buf, DWORD sector, BYTE count)
 	return RES_OK;
 }
 
+DRESULT disk_flush(FATFS *fat)
+{
+	struct fat_priv *priv = fat->userdata;
+
+	return cdev_flush(priv->cdev) ? RES_ERROR : RES_OK;
+}
+
 /* ---------------------------------------------------------------*/
 
 #ifdef CONFIG_FS_FAT_WRITE
@@ -91,51 +98,28 @@ static int fat_create(struct device *dev, const char *pathname, mode_t mode)
 	if (ret)
 		return ret;
 
-	f_close(&f_file);
-
-	return 0;
+	return f_close(&f_file);
 }
 
 static int fat_unlink(struct device *dev, const char *pathname)
 {
 	struct fat_priv *priv = dev->priv;
-	int ret;
 
-	ret = f_unlink(&priv->fat, pathname);
-	if (ret)
-		return ret;
-
-	cdev_flush(priv->cdev);
-
-	return 0;
+	return f_unlink(&priv->fat, pathname);
 }
 
 static int fat_mkdir(struct device *dev, const char *pathname)
 {
 	struct fat_priv *priv = dev->priv;
-	int ret;
 
-	ret = f_mkdir(&priv->fat, pathname);
-	if (ret)
-		return ret;
-
-	cdev_flush(priv->cdev);
-
-	return 0;
+	return f_mkdir(&priv->fat, pathname);
 }
 
 static int fat_rmdir(struct device *dev, const char *pathname)
 {
 	struct fat_priv *priv = dev->priv;
-	int ret;
 
-	ret = f_unlink(&priv->fat, pathname);
-	if (ret)
-		return ret;
-
-	cdev_flush(priv->cdev);
-
-	return 0;
+	return f_unlink(&priv->fat, pathname);
 }
 
 static int fat_write(struct file *f, const void *buf, size_t insize)
@@ -224,16 +208,14 @@ static int fat_open(struct device *dev, struct file *file, const char *filename)
 
 static int fat_close(struct device *dev, struct file *f)
 {
-	struct fat_priv *priv = dev->priv;
 	FIL *f_file = f->private_data;
+	int ret;
 
-	f_close(f_file);
+	ret = f_close(f_file);
 
 	free(f_file);
 
-	cdev_flush(priv->cdev);
-
-	return 0;
+	return ret;
 }
 
 static int fat_read(struct file *f, void *buf, size_t insize)
-- 
2.47.3




More information about the barebox mailing list