/*------------------------------------------------------------------------- * * cfs_state.h * Single ownership-state judge for the CFS sidecar pair. * * Every "did the journal reset / the .cfsmap swap / the shared generation * move under this handle" question in CFS is answered HERE and only here. * Callers derive nothing themselves: they capture a snapshot (or hold a * baseline incarnation/cursor) and switch on the returned flags. The * per-caller masks are documented in cfs_state.c. * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 2026, Diasoft * * src/include/storage/cfs/cfs_state.h * *------------------------------------------------------------------------- */ #ifndef CFS_STATE_H #define CFS_STATE_H #include "storage/cfs/cfs_journal.h" #include "storage/cfs/cfs_storage.h" /* * CfsStateSnapshot — the identity of the sidecar pair as one handle sees * it at capture time. Sums up the four staleness signals CFS has: the * .cfsmap inode behind the handle, the .cfsjrn inode/size, the journal * header (incarnation + generation watermark) and the shared-cache map * generation. */ typedef struct CfsStateSnapshot { /* .cfsmap identity by the handle's descriptor. */ dev_t map_dev; ino_t map_ino; bool map_fd_valid; /* .cfsjrn identity by the handle's descriptor. */ dev_t jrn_dev; ino_t jrn_ino; off_t jrn_size; bool jrn_fd_valid; /* * Journal header fields; jrn_incarnation/jrn_watermark are 0 and * jrn_header_valid is false when the header could not be read (the * comparison semantics follow the old cfs_journal_identity fallback). */ uint16 jrn_incarnation; uint32 jrn_watermark; bool jrn_header_valid; /* * The incarnation through the raw-fields read with the handle fallback * (what cfs_journal_incarnation() returned): the baseline of the * under-append-lock phase-2 check, kept distinct from the fully validated * identity above because the two reads disagree exactly in the * torn-header window a lock-free reader must treat as "moved". */ uint16 jrn_incarnation_stamped; /* Shared-cache map generation of the relation fork. */ uint64 map_generation; } CfsStateSnapshot; /* * Verdict flags. A caller masks the subset it acts on; the judge computes * every signal so no caller ever re-derives one. */ #define CFS_STATE_OK 0 #define CFS_STATE_MAP_SWAPPED (1 << 0) /* path != fd */ #define CFS_STATE_JRN_RESET (1 << 1) /* incarnation moved */ #define CFS_STATE_JRN_SHRANK (1 << 2) /* size < cursor */ #define CFS_STATE_JRN_GREW (1 << 3) /* size > cursor */ #define CFS_STATE_JRN_HEADER_STALE (1 << 4) /* header unreadable now, * was valid at base */ #define CFS_STATE_JRN_STAT_FAILED (1 << 5) /* fstat(jrn_fd) failed */ #define CFS_STATE_JRN_IDENTITY_MOVED (1 << 6) /* dev/ino/watermark/header * validity moved vs base */ #define CFS_STATE_GEN_MOVED (1 << 7) /* shared generation moved */ /* * cfs_state_capture — snapshot the full sidecar identity of a handle: map * and journal inode pairs, journal header, shared map generation. Used as * the base of cfs_state_check(). */ extern void cfs_state_capture(CfsRelation *cfs, CfsStateSnapshot *snap); /* * cfs_state_check — compare the handle's current sidecar identity against * a base captured by cfs_state_capture(). 'cursor_off' is the journal * replay cursor the caller considers synced (the handle's * map_synced_jrn_off or an equivalent baseline); the SHRANK/GREW flags are * reported against it. When 'fresh_jhdr' is non-NULL it receives the * freshly read journal header (valid unless JRN_HEADER_STALE is set). * * Returns a CFS_STATE_* flag set; never raises. */ extern int cfs_state_check(const CfsStateSnapshot *base, CfsRelation *cfs, off_t cursor_off, CfsJournalFileHeader *fresh_jhdr); /* * cfs_state_journal_moved — the snapshot-free variant for callers that * hold a baseline incarnation (the handle's jrn_incarnation or a segment * descriptor's stamp) plus a replay cursor: compares the on-disk journal * against them. This is the whole of what cfs_map_needs_refresh(), * cfs_refresh_map() and cfs_shared_map_sync_segment() ever knew; they now * only switch on the result. 'fresh_jhdr' as in cfs_state_check(). */ extern int cfs_state_journal_moved(CfsRelation *cfs, uint16 base_incarnation, off_t cursor_off, CfsJournalFileHeader *fresh_jhdr); /* * cfs_map_file_swapped — true when the .cfsmap file was replaced * (compaction swap, immedsync snapshot) since this handle opened it: the * handle's descriptor then points at the old inode. Moved here from * cfs_storage.c: it is the map-side half of the judge, and the * CFS_STATE_MAP_SWAPPED flag is computed from it. */ extern bool cfs_map_file_swapped(CfsRelation *cfs); /* * The shared-cache generation provider. The storage layer tags freshly * opened handles with the relation's map generation but must not call * into the shared-memory layer directly (the storage -> shmem edge the * review called out); the relmeta layer installs its lookup here at * shared-memory initialisation. A NULL provider reports 0, which reads * as "no slot yet" — the same value an absent relmeta slot gives. */ typedef uint64 (*CfsGenerationProvider) (RelFileLocatorBackend rlocator, ForkNumber forknum); extern void cfs_state_set_generation_provider(CfsGenerationProvider fn); /* * cfs_state_relation_generation — the relation's current shared-cache map * generation through the installed provider (0 when none). The only * sanctioned way to read the generation outside the shmem layer. */ extern uint64 cfs_state_relation_generation(RelFileLocatorBackend rlocator, ForkNumber forknum); /* * The online-compaction liveness provider (QDB-1514), installed by the * relmeta layer like the generation provider above. The open path asks * cfs_state_compaction_active() before running the interrupted-swap * recovery: a live compaction owns its candidate files, and the recovery * must not touch them. A NULL provider reports "no compaction", which is * correct wherever the shared cache does not exist (startup) — the * startup scan is single-writer anyway. */ typedef bool (*CfsCompactionActiveProvider) (RelFileLocatorBackend rlocator, ForkNumber forknum); extern void cfs_state_set_compaction_provider(CfsCompactionActiveProvider fn); /* * cfs_state_compaction_active — is an online compaction with a live owner * running for this relation fork? The only sanctioned way to ask from the * storage layer and below. */ extern bool cfs_state_compaction_active(RelFileLocatorBackend rlocator, ForkNumber forknum); /* * The sidecar-open guard (QDB-1514), installed by the relmeta layer like * the providers above. A lock-free open of the sidecar files must * serialize against a compactor's exclusive swap phase, or the opened * descriptor set and the generation tag can straddle the swap — a mixed * old/new pair that no staleness check can detect afterwards. The guard * is a shared hold of the per-relation append lock; it is cheap (opens are * per-handle, not per-read) and skipped when this backend already holds * the lock in any mode (checkpoint, fsck, the compactor itself). * cfs_state_open_guard_acquire() returns true when the caller must call * the release; false when there is nothing to release (no provider — * single-threaded startup — or the lock is already held). */ typedef struct CfsOpenGuardOps { bool (*open_guard_acquire) (RelFileLocatorBackend rlocator, ForkNumber forknum); void (*open_guard_release) (RelFileLocatorBackend rlocator, ForkNumber forknum, bool after_error); } CfsOpenGuardOps; extern void cfs_state_set_open_guard_ops(const CfsOpenGuardOps *ops); extern bool cfs_state_open_guard_acquire(RelFileLocatorBackend rlocator, ForkNumber forknum); extern void cfs_state_open_guard_release(RelFileLocatorBackend rlocator, ForkNumber forknum, bool after_error); /* * The authoritative .cfp tail provider, installed by the relmeta layer the * same way as the generation provider (see cfs_shmem.h for the protocol * semantics of each callback). The storage layer consults and maintains * the shared tail through these hooks instead of calling into the * shared-memory layer directly; a NULL provider means "no shared tail" * (before shared-memory initialisation, or a caller with no metadata * slot), and every operation then degenerates to the historical * per-append cfs_cfp_discover_eof() walk. */ typedef struct CfsCfpTailOps { /* Fetch the trustworthy tail; false when the slot is missing/untrusted. */ bool (*tail_fetch) (RelFileLocatorBackend rlocator, ForkNumber forknum, uint64 *out_tail); /* Mark the append in progress with the current backend as owner. */ void (*tail_begin) (RelFileLocatorBackend rlocator, ForkNumber forknum); /* Publish the new tail after a successful pwrite. */ void (*tail_commit) (RelFileLocatorBackend rlocator, ForkNumber forknum, uint64 new_tail); /* Drop trust in the stored tail. */ void (*tail_dirty) (RelFileLocatorBackend rlocator, ForkNumber forknum); } CfsCfpTailOps; extern void cfs_state_set_cfp_tail_ops(const CfsCfpTailOps *ops); /* * cfs_state_cfp_tail_fetch/begin/commit/dirty — the provider-backed entry * points used by the storage layer's append path. The fetch returns false * when no provider is installed; the mutators are no-ops then. */ extern bool cfs_state_cfp_tail_fetch(RelFileLocatorBackend rlocator, ForkNumber forknum, uint64 *out_tail); extern void cfs_state_cfp_tail_begin(RelFileLocatorBackend rlocator, ForkNumber forknum); extern void cfs_state_cfp_tail_commit(RelFileLocatorBackend rlocator, ForkNumber forknum, uint64 new_tail); extern void cfs_state_cfp_tail_dirty(RelFileLocatorBackend rlocator, ForkNumber forknum); #endif /* CFS_STATE_H */