From 76a00c48b49e53be9a43b3c2293f36eb91abde78 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 14 Mar 2026 03:24:19 +0000 Subject: util/nvmutil: verify gbe contents after writing read it back and check. sync to disk first. Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'util/nvmutil') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index de966659..0f3158e7 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -1493,6 +1493,7 @@ rw_gbe_file_part(size_t p, int rw_type, u8 invert = command[cmd_index].invert; u8 *mem_offset; + off_t file_offset; if (rw_type < IO_PREAD || rw_type > IO_PWRITE) err(errno, "%s: %s: part %lu: invalid rw_type, %d", @@ -1506,12 +1507,38 @@ rw_gbe_file_part(size_t p, int rw_type, * E.g. read from p0 (file) to p1 (mem). */ mem_offset = gbe_mem_offset(p ^ invert, rw_type_str); + file_offset = (off_t)gbe_file_offset(p, rw_type_str); if (rw_gbe_file_exact(gbe_fd, mem_offset, - gbe_rw_size, gbe_file_offset(p, rw_type_str), - rw_type) == -1) + gbe_rw_size, file_offset, rw_type) == -1) err(errno, "%s: %s: part %lu", fname, rw_type_str, (ulong)p); + + /* + * Next, we read back what was written, + * to ensure that it was done correctly. + * NOTE: using "pad" (only cat uses it) + */ + + if (rw_type != IO_PWRITE) + return; /* skip for reads */ + + /* + * We may otherwise read from + * cache, so we must sync. + */ + if (fsync(gbe_fd) == -1) + err(errno, "%s: fsync: part %lu (post-verification)", + fname, (ulong)p); + + if (rw_gbe_file_exact(gbe_fd, pad, + gbe_rw_size, file_offset, IO_PREAD) == -1) + err(errno, "%s: pread: part %lu (post-verification)", + fname, (ulong)p); + + if (memcmp(mem_offset, pad, gbe_rw_size)) + err(errno, "%s: pwrite: corrupt write on part %lu", + fname, (ulong)p); } /* -- cgit v1.2.1