2023-03-30 07:04:13

by zhanchengbin

[permalink] [raw]
Subject: [PATCH v2 1/2] lib/ext2fs: add error handle in unix_flush and unix_write_byte

As you can see, a new error handling has been added for fsync, and the error
handling for unix_write_byte function has reused the error handling of
write_blk.

Signed-off-by: zhanchengbin <[email protected]>
---
lib/ext2fs/ext2_io.h | 2 ++
lib/ext2fs/unix_io.c | 37 ++++++++++++++++++++++++++-----------
2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/lib/ext2fs/ext2_io.h b/lib/ext2fs/ext2_io.h
index 679184e3..becd7078 100644
--- a/lib/ext2fs/ext2_io.h
+++ b/lib/ext2fs/ext2_io.h
@@ -56,6 +56,8 @@ struct struct_io_channel {
size_t size,
int actual_bytes_written,
errcode_t error);
+ errcode_t (*sync_error)(io_channel channel,
+ errcode_t error);
int refcount;
int flags;
long reserved[14];
diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index 3171c736..283b4eb6 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -1250,7 +1250,8 @@ static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
#ifdef ALIGN_DEBUG
printf("unix_write_byte: O_DIRECT fallback\n");
#endif
- return EXT2_ET_UNIMPLEMENTED;
+ retval = EXT2_ET_UNIMPLEMENTED;
+ goto error_out;
}

#ifndef NO_IO_CACHE
@@ -1258,19 +1259,30 @@ static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
* Flush out the cache completely
*/
if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
- return retval;
+ goto error_out;
#endif

- if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
- return errno;
+ if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0) {
+ retval = errno;
+ goto error_out;
+ }

actual = write(data->dev, buf, size);
- if (actual < 0)
- return errno;
- if (actual != size)
- return EXT2_ET_SHORT_WRITE;
-
+ if (actual < 0) {
+ retval = errno;
+ goto error_out;
+ }
+ if (actual != size) {
+ retval = EXT2_ET_SHORT_WRITE;
+ goto error_out;
+ }
return 0;
+error_out:
+ if (channel->write_error)
+ retval = (channel->write_error)(channel,
+ offset / channel->block_size, 0, buf,
+ size, actual, retval);
+ return retval;
}

/*
@@ -1289,8 +1301,11 @@ static errcode_t unix_flush(io_channel channel)
retval = flush_cached_blocks(channel, data, 0);
#endif
#ifdef HAVE_FSYNC
- if (!retval && fsync(data->dev) != 0)
- return errno;
+ if (!retval && fsync(data->dev) != 0) {
+ if (channel->sync_error)
+ retval = (channel->sync_error)(channel, errno);
+ return retval;
+ }
#endif
return retval;
}
--
2.31.1