From 65a20f48c6c60275c9d73109f36d3b90e1db6a7c Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 21 May 2026 00:39:03 +0200 Subject: [PATCH] Ensure to advising the remaining of the file when it's closed --- cmdline/handle.c | 4 +++- cmdline/import.c | 2 ++ cmdline/parity.c | 2 ++ cmdline/support.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ cmdline/support.h | 1 + 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/cmdline/handle.c b/cmdline/handle.c index e513b6df..73b0d181 100644 --- a/cmdline/handle.c +++ b/cmdline/handle.c @@ -196,10 +196,12 @@ int handle_close(struct snapraid_handle* handle) /* close if open */ if (handle->f != -1) { + advise_close(&handle->advise, handle->f); + ret = close(handle->f); if (ret != 0) { /* LCOV_EXCL_START */ - log_fatal(errno, "Error closing file '%s'. %s.\n", handle->file->sub, strerror(errno)); + log_fatal(errno, "Error closing file '%s'. %s.\n", handle->path, strerror(errno)); /* invalidate for error */ handle->file = 0; diff --git a/cmdline/import.c b/cmdline/import.c index 36b84621..4655c575 100644 --- a/cmdline/import.c +++ b/cmdline/import.c @@ -111,6 +111,8 @@ static void import_file(struct snapraid_state* state, const char* path, uint64_t size -= read_size; } + advise_close(&advise, f); + ret = close(f); if (ret != 0) { /* LCOV_EXCL_START */ diff --git a/cmdline/parity.c b/cmdline/parity.c index dd09b06d..989faae1 100644 --- a/cmdline/parity.c +++ b/cmdline/parity.c @@ -763,6 +763,8 @@ int parity_close(struct snapraid_parity_handle* handle) struct snapraid_split_handle* split = &handle->split_map[s]; int ret; + advise_close(&split->advise, split->f); + ret = close(split->f); if (ret != 0) { /* LCOV_EXCL_START */ diff --git a/cmdline/support.c b/cmdline/support.c index 39c6f953..ae030b0c 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -1636,6 +1636,65 @@ int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off return 0; } +void advise_close(struct advise_struct* advise, int f) +{ + data_off_t flush_offset; + data_off_t flush_size; + data_off_t discard_offset; + data_off_t discard_size; + + (void)advise; + (void)f; + (void)flush_offset; + (void)flush_size; + (void)discard_offset; + (void)discard_size; + + flush_offset = 0; + flush_size = 0; + discard_offset = 0; + discard_size = 0; + + /* assume it's a write if advise->dirty_begin != advise->dirty_end */ + switch (advise->mode) { + case ADVISE_FLUSH_WINDOW : + flush_offset = advise->dirty_begin; + flush_size = advise->dirty_end - advise->dirty_begin; + break; + case ADVISE_DISCARD_WINDOW : + discard_offset = advise->dirty_begin; + discard_size = advise->dirty_end - advise->dirty_begin; + break; + } + +#if HAVE_SYNC_FILE_RANGE + if (flush_size != 0) { + /* start writing immediately */ + sync_file_range(f, flush_offset, flush_size, SYNC_FILE_RANGE_WRITE); + } +#endif + +#if HAVE_SYNC_FILE_RANGE && HAVE_POSIX_FADVISE + if (discard_size != 0) { + int ret; + + /* send the data to the disk and wait until it's written */ + ret = sync_file_range(f, discard_offset, discard_size, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER); + if (ret != 0) { + /* LCOV_EXCL_START */ + return; + /* LCOV_EXCL_STOP */ + } + + /* flush the data from the cache */ + posix_fadvise_wrapper(f, discard_offset, discard_size, POSIX_FADV_DONTNEED); + } +#endif + + advise->dirty_begin = 0; + advise->dirty_end = 0; +} + /****************************************************************************/ /* memory */ diff --git a/cmdline/support.h b/cmdline/support.h index 04622327..9efacf42 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -419,6 +419,7 @@ int advise_flags(struct advise_struct* advise); int advise_open(struct advise_struct* advise, int f); int advise_write(struct advise_struct* advise, int f, data_off_t offset, data_off_t size); int advise_read(struct advise_struct* advise, int f, data_off_t offset, data_off_t size); +void advise_close(struct advise_struct* advise, int f); /****************************************************************************/ /* memory */