From 08fee9ba559ac2aaa235aecfc582c3e431ed276f Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Fri, 13 Mar 2026 21:28:29 +0000 Subject: util/nvmutil: optimise i/o handling don't call io_args multiple times get rid of do_rw get rid of IO_PWRITE and IO_PREAD we use prw() for cat now. this is OK because when calling prw() with offset zero, it's literallyy the same as just doing normal write() so the code merely no longer distinguishes positional or non-positional, and just uses prw() universally no behaviour change Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 62 +++++++++++--------------------------------------- 1 file changed, 13 insertions(+), 49 deletions(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index b1d24bdc..8347fb7e 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -331,8 +331,6 @@ static ssize_t rw_file_exact(int fd, u8 *mem, size_t len, off_t off, int rw_type); static ssize_t rw_file_once(int fd, u8 *mem, size_t len, off_t off, int rw_type, size_t rc); -static ssize_t do_rw(int fd, - u8 *mem, size_t len, off_t off, int rw_type); static ssize_t prw(int fd, void *mem, size_t nrw, off_t off, int rw_type); static off_t lseek_eintr(int fd, off_t off, int whence); @@ -432,9 +430,7 @@ static const char *argv0; enum { IO_READ, - IO_WRITE, - IO_PREAD, - IO_PWRITE + IO_WRITE }; /* @@ -562,8 +558,6 @@ typedef char assert_argc3[(ARGC_3==3)?1:-1]; typedef char assert_argc4[(ARGC_4==4)?1:-1]; typedef char assert_read[(IO_READ==0)?1:-1]; typedef char assert_write[(IO_WRITE==1)?1:-1]; -typedef char assert_pread[(IO_PREAD==2)?1:-1]; -typedef char assert_pwrite[(IO_PWRITE==3)?1:-1]; typedef char assert_rand_byte[(NUM_RANDOM_BYTES>0)?1:-1]; typedef char assert_rand_len[(NUM_RANDOM_BYTES IO_PWRITE) + if ((unsigned)rw_type > IO_WRITE) err(errno, "%s: %s: part %lu: invalid rw_type, %d", fname, rw_type_str, (unsigned long)p, rw_type); - if (rw_type == IO_PWRITE) + if (rw_type == IO_WRITE) invert = 0; /* @@ -1595,11 +1589,6 @@ rw_file_exact(int fd, u8 *mem, size_t nrw, ssize_t rv; size_t rc; - if (io_args(fd, mem, nrw, off, rw_type) == -1) { - errno = EIO; - return -1; - } - for (rc = 0, rv = 0; rc < nrw; ) { if ((rv = rw_file_once(fd, mem, nrw, off, rw_type, rc)) <= 0) return -1; @@ -1622,11 +1611,8 @@ rw_file_once(int fd, u8 *mem, size_t nrw, size_t retries_on_zero = 0; size_t max_retries = 10; - if (io_args(fd, mem, nrw, off, rw_type) == -1) - goto err_rw_file_once; - read_again: - rv = do_rw(fd, mem + rc, nrw - rc, off + rc, rw_type); + rv = prw(fd, mem + rc, nrw - rc, off + rc, rw_type); if (rv < 0 && errno == EINTR) goto read_again; @@ -1649,27 +1635,6 @@ err_rw_file_once: return -1; } -static ssize_t -do_rw(int fd, u8 *mem, - size_t nrw, off_t off, int rw_type) -{ - if (io_args(fd, mem, nrw, off, rw_type) == -1) - goto err_do_rw; - - if (rw_type == IO_READ) - return read(fd, mem, nrw); - - if (rw_type == IO_WRITE) - return write(fd, mem, nrw); - - if (rw_type == IO_PREAD || rw_type == IO_PWRITE) - return prw(fd, mem, nrw, off, rw_type); - -err_do_rw: - errno = EIO; - return -1; -} - /* * This implements a portable analog of pwrite() * and pread() - note that this version is not @@ -1686,17 +1651,11 @@ prw(int fd, void *mem, size_t nrw, off_t off_orig; ssize_t r; int saved_errno; - int prw_type; int flags; if (io_args(fd, mem, nrw, off, rw_type) == -1) goto err_prw; - prw_type = rw_type ^ IO_PREAD; - - if ((unsigned int)prw_type > IO_WRITE) - goto err_prw; - flags = fcntl(fd, F_GETFL); if (flags == -1) return -1; @@ -1717,7 +1676,12 @@ prw(int fd, void *mem, size_t nrw, return -1; do { - r = do_rw(fd, mem, nrw, off, prw_type); + if (rw_type == IO_READ) + r = read(fd, mem, nrw); + + if (rw_type == IO_WRITE) + r = write(fd, mem, nrw); + } while (r < 0 && errno == EINTR); saved_errno = errno; @@ -1760,7 +1724,7 @@ io_args(int fd, void *mem, size_t nrw, if (fd < 0 || !nrw /* prevent zero read request */ || nrw > (size_t)SSIZE_MAX /* prevent overflow */ - || (unsigned int)rw_type > IO_PWRITE) + || (unsigned int)rw_type > IO_WRITE) goto err_io_args; return 0; -- cgit v1.2.1