* [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files
@ 2026-08-25 10:10 Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 2/4] fs: fat: drop stray printf debug output Ahmad Fatoum
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2026-08-25 10:10 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We currently map all error codes to -EINVAL when opening or creating a
file, when the underlying functions are capable of returning more error
codes like -EISDIR or -EROFS.
Propagate the received error code to tell consumers more about what went
wrong.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/fat/fat.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 0402db1d945a..b17157d1ea34 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -89,7 +89,7 @@ static int fat_create(struct device *dev, const char *pathname, mode_t mode)
ret = f_open(&priv->fat, &f_file, pathname, FA_OPEN_ALWAYS);
if (ret)
- return -EINVAL;
+ return ret;
f_close(&f_file);
@@ -204,7 +204,7 @@ static int fat_open(struct device *dev, struct file *file, const char *filename)
ret = f_open(&priv->fat, f_file, filename, flags);
if (ret) {
free(f_file);
- return -EINVAL;
+ return ret;
}
if (file->f_flags & O_APPEND) {
@@ -212,7 +212,7 @@ static int fat_open(struct device *dev, struct file *file, const char *filename)
if (ret) {
f_close(f_file);
free(f_file);
- return -EINVAL;
+ return ret;
}
}
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/4] fs: fat: drop stray printf debug output
2026-08-25 10:10 [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Ahmad Fatoum
@ 2026-08-25 10:10 ` Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 3/4] fs: fat: use fixed-width 32-bit FatFs types Ahmad Fatoum
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2026-08-25 10:10 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
There appears to be nothing special here that warrants printing this
message irrespective of log level.
As there are no other debug prints in the file, just drop it.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/fat/ff.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/fs/fat/ff.c b/fs/fat/ff.c
index 89b4f9fa5de7..e4d83a35605f 100644
--- a/fs/fat/ff.c
+++ b/fs/fat/ff.c
@@ -1957,7 +1957,6 @@ int f_write (
fp->clust = clst; /* Update current cluster */
}
if (fp->flag & FA__DIRTY) { /* Write-back sector cache */
- printf("wr sector cache\n");
if (disk_write(fp->fs, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, -EIO);
fp->flag &= ~FA__DIRTY;
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/4] fs: fat: use fixed-width 32-bit FatFs types
2026-08-25 10:10 [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 2/4] fs: fat: drop stray printf debug output Ahmad Fatoum
@ 2026-08-25 10:10 ` Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 4/4] fs: fat: flush metadata updates to the block device Ahmad Fatoum
2026-08-28 11:53 ` [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2026-08-25 10:10 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
The comment already says LONG/ULONG/DWORD types should be 32-bit, but
barebox's long is always pointer-sized, so 64-bit on 64-bit platforms.
Use explicit 32-bit types for the FatFs fields that are written to disk
or used in sector calculations.
Assisted-by: Codex:gpt-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/fat/fat.c | 4 ++--
fs/fat/ff.c | 9 ++++++++-
fs/fat/integer.h | 8 +++++---
3 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index b17157d1ea34..b50867524a74 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -52,7 +52,7 @@ DRESULT disk_read(FATFS *fat, BYTE *buf, DWORD sector, BYTE count)
size_t len = count * sector_size;
int ret;
- debug("%s: sector: %ld count: %d\n", __func__, sector, count);
+ debug("%s: sector: %u count: %d\n", __func__, sector, count);
ret = cdev_read(priv->cdev, buf, len, (loff_t)sector * sector_size, 0);
if (ret != len)
@@ -68,7 +68,7 @@ DRESULT disk_write(FATFS *fat, const BYTE *buf, DWORD sector, BYTE count)
size_t len = count * sector_size;
int ret;
- debug("%s: buf: %p sector: %ld count: %d\n",
+ debug("%s: buf: %p sector: %u count: %d\n",
__func__, buf, sector, count);
ret = cdev_write(priv->cdev, buf, len, (loff_t)sector * sector_size, 0);
diff --git a/fs/fat/ff.c b/fs/fat/ff.c
index e4d83a35605f..2f4e3b14f0a5 100644
--- a/fs/fat/ff.c
+++ b/fs/fat/ff.c
@@ -1539,14 +1539,21 @@ static enum filetype check_fs ( /* 0:The FAT BR, 1:Valid BR but not an FAT, 2:No
DWORD *bootsec
)
{
+ unsigned long bootsec_ul = 0;
DRESULT ret;
+ enum filetype type;
/* Load boot record */
ret = disk_read(fs, fs->win, sect, 1);
if (ret)
return filetype_unknown;
- return is_fat_or_mbr(fs->win, bootsec);
+ type = is_fat_or_mbr(fs->win, &bootsec_ul);
+
+ if (bootsec)
+ *bootsec = bootsec_ul;
+
+ return type;
}
/*
diff --git a/fs/fat/integer.h b/fs/fat/integer.h
index fe94e374f356..d8b13402afe3 100644
--- a/fs/fat/integer.h
+++ b/fs/fat/integer.h
@@ -7,6 +7,8 @@
#ifndef _INTEGER
#define _INTEGER
+#include <linux/types.h>
+
/* These types must be 16-bit, 32-bit or larger integer */
typedef int INT;
typedef unsigned int UINT;
@@ -23,8 +25,8 @@ typedef unsigned short WORD;
typedef unsigned short WCHAR;
/* These types must be 32-bit integer */
-typedef long LONG;
-typedef unsigned long ULONG;
-typedef unsigned long DWORD;
+typedef s32 LONG;
+typedef u32 ULONG;
+typedef u32 DWORD;
#endif
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 4/4] fs: fat: flush metadata updates to the block device
2026-08-25 10:10 [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 2/4] fs: fat: drop stray printf debug output Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 3/4] fs: fat: use fixed-width 32-bit FatFs types Ahmad Fatoum
@ 2026-08-25 10:10 ` Ahmad Fatoum
2026-08-28 11:53 ` [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2026-08-25 10:10 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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@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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files
2026-08-25 10:10 [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Ahmad Fatoum
` (2 preceding siblings ...)
2026-08-25 10:10 ` [PATCH 4/4] fs: fat: flush metadata updates to the block device Ahmad Fatoum
@ 2026-08-28 11:53 ` Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2026-08-28 11:53 UTC (permalink / raw)
To: barebox, Ahmad Fatoum
On Tue, 25 Aug 2026 12:10:53 +0200, Ahmad Fatoum wrote:
> We currently map all error codes to -EINVAL when opening or creating a
> file, when the underlying functions are capable of returning more error
> codes like -EISDIR or -EROFS.
>
> Propagate the received error code to tell consumers more about what went
> wrong.
>
> [...]
Applied, thanks!
[1/4] fs: fat: propagate actual error code when opening/creating files
https://git.pengutronix.de/cgit/barebox/commit/?id=228a800d9599 (link may not be stable)
[2/4] fs: fat: drop stray printf debug output
https://git.pengutronix.de/cgit/barebox/commit/?id=99cee192dbee (link may not be stable)
[3/4] fs: fat: use fixed-width 32-bit FatFs types
https://git.pengutronix.de/cgit/barebox/commit/?id=f2c889bed1b2 (link may not be stable)
[4/4] fs: fat: flush metadata updates to the block device
https://git.pengutronix.de/cgit/barebox/commit/?id=6d149f6ff875 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-08-28 11:54 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-25 10:10 [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 2/4] fs: fat: drop stray printf debug output Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 3/4] fs: fat: use fixed-width 32-bit FatFs types Ahmad Fatoum
2026-08-25 10:10 ` [PATCH 4/4] fs: fat: flush metadata updates to the block device Ahmad Fatoum
2026-08-28 11:53 ` [PATCH 1/4] fs: fat: propagate actual error code when opening/creating files Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox