/*------------------------------------------------------------------------- * cfs_map_file.h * Compressed File Store (CFS) — the .cfsmap file, one module. * * Every read and write of the .cfsmap sidecar goes through this module: * the 8-byte prologue (ensure / read), positional row reads with the * shared row-classification contract, the physical resize and the two * snapshot writers (raw descriptor and durable path variant). Callers * that need handle-level state (capacity, watermark, cfp EOF) live in * cfs_storage.c and build on these primitives. * * Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group * * IDENTIFICATION * src/include/storage/cfs/cfs_map_file.h * *------------------------------------------------------------------------- */ #ifndef CFS_MAP_FILE_H #define CFS_MAP_FILE_H #include "postgres.h" #include "storage/block.h" #include "storage/cfs/cfs.h" #include "storage/cfs/cfs_io.h" #include "storage/relfilelocator.h" /* The handle type: a forward declaration keeps this header free of * cfs_storage.h (breaking the include cycle through the storage layer). * Pass CfsRelation * or struct CfsRelation * interchangeably. */ struct CfsRelation; /* * cfs_map_file_prologue_ensure - make sure the .cfsmap carries its static * 8-byte prologue. A relation created through cfs_create() has it from * the start; a handle that created the sidecars via O_CREAT (the * open-with-create path) may sit on an empty file. Row writers (the * fold) extend the file positionally, so without this the first resize * would zero-fill across a missing prologue and every later load would * see an invalid header. Only an empty file is initialised; a file that * is shorter than the prologue but not empty is a truncated header, not a * fresh map, and is reported as corruption (CFS_STORAGE_CORRUPT) without * touching a byte. */ extern int cfs_map_file_prologue_ensure(int fd); /* * cfs_map_file_prologue_read_fd — the shared prologue half of every map * load (full snapshot, lazy header and the pool's segment loader): pread * the 8-byte prologue, validate it, derive the row count from the file * size. Returns CFS_STORAGE_NOENT (missing/empty map, *nblocks = 0) or * CFS_STORAGE_ERROR on an I/O failure. A prologue that fails validation * or a non-empty file shorter than the prologue is corruption - the * single policy point every loader shares (no fallback source exists): * during recovery it raises the hard "corrupt page map snapshot" ERROR * itself; outside recovery 'rlocator'/'forknum' route the damage through * cfs_report_corruption(), so the cfs_checksum_failure GUC decides the * severity. * * cfs_map_file_prologue_read is the same read through a handle's * descriptor, path and identity. */ extern int cfs_map_file_prologue_read_fd(int fd, const char *path, RelFileLocatorBackend rlocator, ForkNumber forknum, uint64 *nblocks); extern int cfs_map_file_prologue_read(struct CfsRelation *cfs, uint64 *nblocks); /* * cfs_map_file_prologue_check — the status-returning half of the prologue * read, for checkers that classify the damage themselves instead of * aborting. Returns CFS_STORAGE_OK (*nblocks derived from the file * size), CFS_STORAGE_NOENT (empty file, *nblocks = 0), * CFS_STORAGE_CORRUPT (a non-empty file shorter than the prologue, or a * prologue that fails validation) or CFS_STORAGE_ERROR (size or read * failure); never raises. */ extern int cfs_map_file_prologue_check(int fd, uint64 *nblocks); /* * cfs_map_range_iter - stream the rows of a map file in fixed-size chunks, * so every consumer that walks a whole snapshot (fsck passes, future * reporting) shares one read pattern. 'truncated' is set when the file ends * inside a row or a read fails: a partial trailing row is the normal crash * artifact, the caller decides how loudly to report it. */ #define CFS_MAP_RANGE_CHUNK_ENTRIES 4096 typedef struct CfsMapRangeIter { int fd; uint64 nblocks; uint64 next_block; char *buf; size_t buflen; /* entries in buf */ size_t bufpos; /* next entry in buf */ bool truncated; } CfsMapRangeIter; extern void cfs_map_range_iter_init(CfsMapRangeIter *it, int fd, uint64 nblocks); extern bool cfs_map_range_iter_next(CfsMapRangeIter *it, uint64 *blocknum, const CfsMapEntry **row); extern void cfs_map_range_iter_free(CfsMapRangeIter *it); /* * cfs_map_file_read_range - read 'nblocks' positional rows starting at * 'first_block' into 'rows' and apply the shared row contract: a row with * a valid entry_crc stays as-is, an all-zero row (a sparse hole) becomes * a CRC-valid INVALID entry, and a row with a bad entry_crc that is not * all-zero becomes INVALID and is remembered in *corrupt_mask (lazily * allocated here, one byte per row of the range) so the caller's journal * replay can verify a covering record healed it. Returns * CFS_STORAGE_ERROR on an I/O failure of the read itself. */ extern int cfs_map_file_read_range(int fd, uint64 first_block, uint64 nblocks, CfsMapEntry *rows, char **corrupt_mask, uint64 corrupt_mask_len); /* * cfs_map_file_range_live_bytes - sum the payload bytes (CFS_RECORD_SIZE * units, the units the dead-space accounting uses) and the valid-row * count of the rows in [first_block, first_block + nblocks), read in * fixed chunks. Both places that drop map rows use it to charge their * payload into the dead-space counters: the in-place truncate path * (cfs_truncate) and the checkpoint's RESIZE application for the * crash-recovery window where the file is still longer than the RESIZE * target. The sum is bounded by what the truncation itself removes. * A corrupt row reads as INVALID here and adds nothing: conservative * (its payload is unknown), and compaction recomputes the total exactly. * Either output pointer may be NULL. */ extern int cfs_map_file_range_live_bytes(int fd, uint64 first_block, uint64 nblocks, uint64 *live_bytes, uint64 *live_pages); /* * cfs_map_file_resize - make the .cfsmap exactly 'nblocks' rows long and * fsync it. Runs under the append lock (checkpoint phase 2), so no * reader sees a map shorter than the logical size it is allowed to read. * * Handle-only contract: it does not update map_hdr.nblocks/map_capacity, * and no caller reads those fields afterwards; the handle stays valid and * is used for the journal reset and stats. */ extern void cfs_map_file_resize(struct CfsRelation *cfs, BlockNumber nblocks); /* * cfs_map_file_write_snapshot_fd - write the static prologue plus * 'nblocks' rows to an already-created, truncated descriptor (the caller * owns the file's lifecycle: create, fsync, rename). 'path' names the * file in error messages. Raises ERROR on a write failure; returns * CFS_STORAGE_OK otherwise. * * cfs_map_file_write_snapshot_path - the shared durability sequence of * the snapshot writers: write the prologue + entries to ".tmp", * fsync, atomically rename over 'path'. The temp file is created with * O_TRUNC, so the resulting size is exactly the prologue plus the rows. * The caller reopens its own descriptor afterwards (it may not have one). */ extern int cfs_map_file_write_snapshot_fd(int fd, const char *path, const CfsMapEntry *map, uint64 nblocks); extern int cfs_map_file_write_snapshot_path(const char *path, const CfsMapEntry *map, uint64 nblocks); #endif /* CFS_MAP_FILE_H */