[PATCH 03/10] libext2fs: always fsync the device when closing the unix IO manager

Darrick J. Wong
2025-08-21T01:13:52Z

unix_close is the last chance that libext2fs has to report write
failures to users. Although it's likely that ext2fs_close already
called ext2fs_flush and told the IO manager to flush, we could do one
more sync before we close the file descriptor. Also don't override the
fsync's errno with the close's errno.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
lib/ext2fs/unix_io.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index 14f5a0c434191a..80fff984e48224 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -1147,8 +1147,11 @@ static errcode_t unix_close(io_channel channel)
#ifndef NO_IO_CACHE
retval = flush_cached_blocks(channel, data, 0);
#endif
+ /* always fsync the device, even if flushing our own cache failed */
+ if (fsync(data->dev) != 0 && !retval)
+ retval = errno;

- if (close(data->dev) < 0)
+ if (close(data->dev) < 0 && !retval)
retval = errno;
free_cache(data);
free(data->cache);