/*------------------------------------------------------------------------- * * cfs_io.c * Raw file I/O primitives shared by every CFS module (see cfs_io.h). * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 2026, Diasoft * * src/backend/storage/cfs/cfs_io.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include #include #include "common/file_utils.h" #include "port/pg_iovec.h" #include "storage/cfs/cfs_guc.h" #include "storage/cfs/cfs_io.h" /* * cfs_pwrite_all — write 'len' bytes to 'fd' at 'offset', retrying short * writes. Returns CFS_STORAGE_OK or CFS_STORAGE_ERROR. Used both by the * storage layer and by the compaction layer (cfs_compact.c). */ int cfs_pwrite_all(int fd, const void *buf, size_t len, off_t offset) { const char *p = (const char *) buf; size_t done = 0; /* * Debug fault injection: simulate a pwrite() failure so the test suite * can exercise the write-path error handling. No-op in production * (cfs_debug_fail_pwrite defaults to 0). */ if (cfs_debug_fail_pwrite > 0) { cfs_debug_fail_pwrite--; errno = EIO; ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite failed: %m"))); return CFS_STORAGE_ERROR; } while (done < len) { ssize_t n; n = pg_pwrite(fd, p + done, len - done, offset + done); if (n < 0) { if (errno == EINTR) continue; ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite failed: %m"))); return CFS_STORAGE_ERROR; } if (n == 0) { ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite wrote zero bytes"))); return CFS_STORAGE_ERROR; } done += n; } return CFS_STORAGE_OK; } /* * cfs_pwritev_all — the scatter/gather form of cfs_pwrite_all(): write * 'iovcnt' iovecs to 'fd' at 'offset' via pg_pwritev_with_retry (the * portable port in pg_iovec.h, never a raw pwritev), retrying short * writes and EINTR, with the same cfs_debug_fail_pwrite fault injection * and WARNING-on-failure contract as cfs_pwrite_all. A partial record * left by a failed call is the caller's torn tail, exactly like a * cfs_pwrite_all failure. * * A zero-length transfer is refused the same way cfs_pwrite_all() reports * "wrote zero bytes": the caller always sends a record header, so a zero * result can only come from a broken retry helper below us (upstream * pg_pwritev_with_retry() has no zero-progress check of its own; a * pwritev() returning 0 with a nonzero count violates POSIX and would * spin inside it before we could ever observe it). */ int cfs_pwritev_all(int fd, const struct iovec *iov, int iovcnt, off_t offset) { size_t total = 0; ssize_t n; int i; /* * Debug fault injection: simulate a pwrite() failure so the test suite * can exercise the write-path error handling. No-op in production * (cfs_debug_fail_pwrite defaults to 0). */ if (cfs_debug_fail_pwrite > 0) { cfs_debug_fail_pwrite--; errno = EIO; ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite failed: %m"))); return CFS_STORAGE_ERROR; } /* * A record always has a header: refuse an empty transfer up front with * the same "wrote zero bytes" diagnostic cfs_pwrite_all() would give, * instead of delegating a zero-length write to the retry helper. */ for (i = 0; i < iovcnt; i++) total += iov[i].iov_len; if (total == 0) { ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite wrote zero bytes"))); return CFS_STORAGE_ERROR; } do { n = pg_pwritev_with_retry(fd, iov, iovcnt, offset); } while (n < 0 && errno == EINTR); if (n < 0) { ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite failed: %m"))); return CFS_STORAGE_ERROR; } if (n == 0) { /* * Unreachable with a nonzero total and a healthy retry helper; * kept so the vector path can never lose the zero-bytes * diagnostic that the byte form reports. */ ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pwrite wrote zero bytes"))); return CFS_STORAGE_ERROR; } return CFS_STORAGE_OK; } /* * cfs_pread_all — read exactly 'len' bytes from 'fd' at 'offset' into buf. * Returns CFS_STORAGE_OK, CFS_STORAGE_NOENT on unexpected EOF, or * CFS_STORAGE_ERROR. Also used by the shared map pool to load .cfsmap * segments. */ int cfs_pread_all(int fd, void *buf, size_t len, off_t offset) { char *p = (char *) buf; size_t done = 0; while (done < len) { ssize_t n; n = pg_pread(fd, p + done, len - done, offset + done); if (n < 0) { if (errno == EINTR) continue; ereport(WARNING, (errcode_for_file_access(), errmsg("CFS: pread failed: %m"))); return CFS_STORAGE_ERROR; } if (n == 0) return CFS_STORAGE_NOENT; /* unexpected EOF */ done += n; } return CFS_STORAGE_OK; } /* * Return the current size of an open file descriptor, or -1. */ off_t cfs_fd_size(int fd) { struct stat st; if (fstat(fd, &st) != 0) return -1; return st.st_size; }