/*------------------------------------------------------------------------- * * cfs_sync.c * The CFS side of the checkpointer sync queue: fsync/unlink request * registration and the file-tag callbacks storage/sync/sync.c calls. * Moved out of cfs_smgr.c so that the storage layer can register its * writes (cfs_register_dirty_file) without depending on the smgr * bridge — this module's dependencies are the sync queue, the sidecar * path builder and raw fsync, nothing else. * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 2026, Diasoft * * src/backend/storage/cfs/cfs_sync.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include "access/xlog.h" #include "miscadmin.h" #include "storage/cfs/cfs_path.h" #include "storage/cfs/cfs_sync.h" #include "storage/fd.h" #include "storage/sync.h" /* ================================================================ * Sync handler callbacks * * CFS sidecar files (.cfp/.cfsmap/.cfsjrn) register fsync and unlink * requests through the standard sync queue so that the checkpointer * fsyncs them at checkpoint time, exactly like md.c does for base * relation segments. * ================================================================ */ /* * Resolve a CfsFileKind from a file tag's 'segno' field. Returns false when * the value is out of range. */ static bool cfs_kind_from_tag(const FileTag *ftag, CfsFileKind *kind) { if (ftag->segno >= (uint64) CFS_FILE_KIND_COUNT) return false; *kind = (CfsFileKind) ftag->segno; return true; } int cfs_syncfiletag(const FileTag *ftag, char *path) { RelFileLocatorBackend rlocator; CfsFileKind kind; char *sidecar_path; uint32 segno; int result = 0; int save_errno = 0; if (!cfs_kind_from_tag(ftag, &kind)) { errno = EINVAL; return -1; } rlocator.locator = ftag->rlocator; rlocator.backend = INVALID_PROC_NUMBER; /* * The .cfp data file is segmented (".cfp", ".cfp.1", ...): fsync every * existing segment, stopping at the first gap. Map and journal are * single-segment files. */ for (segno = 0;; segno++) { int fd; sidecar_path = cfs_make_segment_filename(rlocator, MAIN_FORKNUM, kind, segno); if (sidecar_path == NULL) { errno = EINVAL; return -1; } if (segno == 0) strlcpy(path, sidecar_path, MAXPGPATH); fd = BasicOpenFile(sidecar_path, O_RDWR | PG_BINARY); pfree(sidecar_path); if (fd < 0) { if (errno == ENOENT && segno > 0) break; /* end of the segment chain */ return -1; } result = pg_fsync(fd); save_errno = errno; close(fd); errno = save_errno; if (result != 0) return result; if (kind != CFS_FILE_DATA) break; /* single-segment kinds */ } return result; } int cfs_unlinkfiletag(const FileTag *ftag, char *path) { RelFileLocatorBackend rlocator; CfsFileKind kind; char *sidecar_path; uint32 segno; if (!cfs_kind_from_tag(ftag, &kind)) { errno = EINVAL; return -1; } rlocator.locator = ftag->rlocator; rlocator.backend = INVALID_PROC_NUMBER; /* * Like cfs_syncfiletag, walk the whole .cfp segment chain. Segments are * contiguous, so the first missing one ends the loop. */ for (segno = 0;; segno++) { sidecar_path = cfs_make_segment_filename(rlocator, MAIN_FORKNUM, kind, segno); if (sidecar_path == NULL) { errno = EINVAL; return -1; } if (segno == 0) strlcpy(path, sidecar_path, MAXPGPATH); if (unlink(sidecar_path) != 0) { int save_errno = errno; pfree(sidecar_path); if (save_errno == ENOENT) break; /* end of the segment chain */ errno = save_errno; return -1; } pfree(sidecar_path); if (kind != CFS_FILE_DATA) break; /* single-segment kinds */ } return 0; } bool cfs_filetagmatches(const FileTag *ftag, const FileTag *candidate) { /* * A filter request carries either a complete relation locator or a * database-wide locator with relNumber == 0. The former is used by * cfs_forget_sync_requests() to forget all pending sync/unlink requests * of one relation fork when it is dropped; the latter by * cfs_forget_database_sync_requests() to forget everything belonging to a * whole database, like mdfiletagmatches() does for md.c. */ if (ftag->rlocator.relNumber == 0) return ftag->rlocator.dbOid == candidate->rlocator.dbOid; return ftag->rlocator.dbOid == candidate->rlocator.dbOid && ftag->rlocator.spcOid == candidate->rlocator.spcOid && ftag->rlocator.relNumber == candidate->rlocator.relNumber; } void cfs_register_dirty_file(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind) { FileTag tag; if (RelFileLocatorBackendIsTemp(rlocator)) return; INIT_CFS_FILETAG(tag, rlocator.locator, kind); if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false)) { /* * The checkpointer queue is full — fsync locally so durability is * not lost, mirroring register_dirty_segment() in md.c. * * CFS note: md.c can fsync via an already-open fd (mdfd_vfd), but the * storage layer here only has the rlocator, so the sidecar path must * be rebuilt with cfs_make_filename() (palloc). That is forbidden * inside a critical section. When invoked from WAL redo (e.g. * XLOG_SMGR_TRUNCATE replay via cfs_smgr_truncate -> cfs_truncate), * skip the fallback: the journal record is already written, the WAL * record guarantees replayability, and the end-of-recovery checkpoint * will fsync everything. Dropping the request is safe — see * cfs_storage.h's crash-safety summary. */ if (CritSectionCount > 0) { ereport(DEBUG1, (errmsg("CFS: sync queue full during critical section; " "deferring fsync of %u/%u/%u kind %d to checkpoint", rlocator.locator.spcOid, rlocator.locator.dbOid, rlocator.locator.relNumber, (int) kind))); return; } { uint32 segno; /* Walk the whole segment chain (see cfs_syncfiletag). */ for (segno = 0;; segno++) { char *sidecar_path; int fd; sidecar_path = cfs_make_segment_filename(rlocator, forknum, kind, segno); if (sidecar_path == NULL) return; fd = BasicOpenFile(sidecar_path, O_RDWR | PG_BINARY); if (fd < 0) { pfree(sidecar_path); break; /* end of the segment chain */ } /* * Close the descriptor before reporting: with data_sync_retry * enabled the ereport is an ERROR, and the raw descriptor * would leak for the rest of the process lifetime. */ { int fsync_status = pg_fsync(fd); int save_errno = errno; close(fd); errno = save_errno; if (fsync_status < 0) ereport(data_sync_elevel(WARNING), (errcode_for_file_access(), errmsg("CFS: could not fsync sidecar \"%s\": %m", sidecar_path))); } pfree(sidecar_path); if (kind != CFS_FILE_DATA) break; /* single-segment kinds */ } } } } void cfs_register_unlink_file(RelFileLocatorBackend rlocator, ForkNumber forknum, CfsFileKind kind) { FileTag tag; if (RelFileLocatorBackendIsTemp(rlocator)) return; INIT_CFS_FILETAG(tag, rlocator.locator, kind); RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true); } void cfs_forget_sync_requests(RelFileLocatorBackend rlocator, ForkNumber forknum) { FileTag tag; if (RelFileLocatorBackendIsTemp(rlocator)) return; INIT_CFS_FILETAG(tag, rlocator.locator, CFS_FILE_DATA); RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true); } void cfs_forget_database_sync_requests(Oid dbid) { FileTag tag; RelFileLocator rlocator; rlocator.dbOid = dbid; rlocator.spcOid = 0; rlocator.relNumber = 0; INIT_CFS_FILETAG(tag, rlocator, CFS_FILE_DATA); RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true); }