/*------------------------------------------------------------------------- * cfs_format.c * Compressed File Store (CFS) — on-disk format serialisation/validation. * * Implements the helpers declared in cfs_format.h. Every init routine zero- * fills the destination structure first so that any inter-field padding is * deterministic and therefore reliably covered by the CRC. The exact on-disk * sizes and CRC field offsets are pinned at compile time below. * * Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group * * IDENTIFICATION * src/backend/storage/cfs/cfs_format.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include #include "port/pg_crc32c.h" #include "storage/cfs/cfs.h" #include "storage/cfs/cfs_format.h" /* ---------------------------------------------------------------- * Compile-time layout invariants. * * These guarantee that the on-disk structures have the expected sizes and * that the CRC fields sit where the code below assumes. Structs that contain * a uint64 field are rounded up to 8-byte alignment, hence the trailing * padding captured in the sizes below. sizeof(CfsMapEntry) must stay within * 64 bytes . * ---------------------------------------------------------------- */ StaticAssertDecl(sizeof(CfsMapEntry) <= 64, "CfsMapEntry must fit within 64 bytes"); StaticAssertDecl(sizeof(CfsCompressedBlockHeader) == 28, "CfsCompressedBlockHeader must be 28 bytes"); StaticAssertDecl(sizeof(CfsMapEntry) == 24, "CfsMapEntry must be 24 bytes"); StaticAssertDecl(sizeof(CfsJournalFileHeader) == 16, "CfsJournalFileHeader must be 16 bytes"); StaticAssertDecl(sizeof(CfsJournalEntry) == 48, "CfsJournalEntry must be 48 bytes"); StaticAssertDecl(sizeof(CfsDataFileLeadIn) == CFS_CFP_BASE_OFFSET, "CfsDataFileLeadIn must fill the .cfp lead-in exactly"); StaticAssertDecl(offsetof(CfsCompressedBlockHeader, hdr_crc) == 24, "CfsCompressedBlockHeader.hdr_crc offset"); StaticAssertDecl(offsetof(CfsMapEntry, entry_crc) == 16, "CfsMapEntry.entry_crc offset"); StaticAssertDecl(offsetof(CfsJournalFileHeader, incarnation) == 6, "CfsJournalFileHeader.incarnation offset"); StaticAssertDecl(offsetof(CfsJournalFileHeader, max_generation) == 8, "CfsJournalFileHeader.max_generation offset"); StaticAssertDecl(offsetof(CfsJournalFileHeader, hdr_crc) == 12, "CfsJournalFileHeader.hdr_crc offset"); StaticAssertDecl(offsetof(CfsJournalEntry, entry_crc) == 40, "CfsJournalEntry.entry_crc offset"); /* ---------------------------------------------------------------- * Internal CRC helper. * ---------------------------------------------------------------- */ static pg_crc32c cfs_format_crc(const void *data, size_t len) { pg_crc32c crc; INIT_CRC32C(crc); COMP_CRC32C(crc, data, len); FIN_CRC32C(crc); return crc; } /* ---------------------------------------------------------------- * Status string helper * ---------------------------------------------------------------- */ const char * CfsFormatStatusString(CfsFormatStatus status) { switch (status) { case CFS_FORMAT_OK: return "ok"; case CFS_FORMAT_BAD_MAGIC: return "bad magic number"; case CFS_FORMAT_BAD_VERSION: return "unsupported version"; case CFS_FORMAT_BAD_CRC: return "CRC mismatch"; case CFS_FORMAT_TRUNCATED: return "truncated record"; case CFS_FORMAT_BAD_FIELD: return "invalid field value"; } return "unknown"; } /* ---------------------------------------------------------------- * Page Map (.cfsmap) prologue * ---------------------------------------------------------------- */ void cfs_map_prologue_init(CfsMapPrologue *prologue) { MemSet(prologue, 0, sizeof(CfsMapPrologue)); prologue->magic = CFS_MAP_MAGIC; prologue->version = CFS_MAP_VERSION; } /* * An all-zero row is a sparse hole - the block was never written - and * is its own correct INVALID representation. A row with a bad * entry_crc that is NOT all-zero is corruption handled by the * journal-repair contract. Single classification for the loaders, the * pool and fsck. */ bool cfs_map_entry_is_hole(const CfsMapEntry *e) { return (e->physical_offset == CFS_ENTRY_INVALID_OFFSET && e->compressed_size == 0 && e->generation == 0 && e->entry_crc == 0); } CfsFormatStatus cfs_map_prologue_validate(const CfsMapPrologue *prologue) { if (prologue->magic != CFS_MAP_MAGIC) return CFS_FORMAT_BAD_MAGIC; if (prologue->version != CFS_MAP_VERSION) return CFS_FORMAT_BAD_VERSION; if (prologue->_reserved != 0) return CFS_FORMAT_BAD_FIELD; ereport(DEBUG1, (errmsg("CFS: page map prologue validated successfully"))); return CFS_FORMAT_OK; } /* ---------------------------------------------------------------- * Page Map (.cfsmap) entry * ---------------------------------------------------------------- */ void cfs_map_entry_crc_set(CfsMapEntry *e) { e->entry_crc = cfs_format_crc(e, offsetof(CfsMapEntry, entry_crc)); } bool cfs_map_entry_crc_ok(const CfsMapEntry *e) { pg_crc32c expected = cfs_format_crc(e, offsetof(CfsMapEntry, entry_crc)); return EQ_CRC32C(expected, e->entry_crc); } void cfs_map_entry_init(CfsMapEntry *e) { MemSet(e, 0, sizeof(CfsMapEntry)); e->physical_offset = CFS_ENTRY_INVALID_OFFSET; cfs_map_entry_crc_set(e); } CfsFormatStatus cfs_map_entry_validate(const CfsMapEntry *e, bool check_fields) { if (!cfs_map_entry_crc_ok(e)) return CFS_FORMAT_BAD_CRC; /* A slot that was never written is always valid. */ if (e->physical_offset == CFS_ENTRY_INVALID_OFFSET) return CFS_FORMAT_OK; if (check_fields) { if (e->compressed_size > CFS_BLCKSZ) return CFS_FORMAT_BAD_FIELD; } return CFS_FORMAT_OK; } CfsMapRowClass cfs_map_row_classify(const CfsMapEntry *e, bool check_fields, CfsFormatStatus *status) { CfsFormatStatus st = cfs_map_entry_validate(e, check_fields); if (st == CFS_FORMAT_OK) return CFS_MAP_ROW_VALID; /* All-zero rows are the sparse holes of a never-written block. */ if (cfs_map_entry_is_hole(e)) return CFS_MAP_ROW_HOLE; if (status != NULL) *status = st; return CFS_MAP_ROW_CORRUPT; } /* ---------------------------------------------------------------- * Journal (.cfsjrn) header * ---------------------------------------------------------------- */ void cfs_journal_header_set_crc(CfsJournalFileHeader *hdr) { hdr->hdr_crc = cfs_format_crc(hdr, offsetof(CfsJournalFileHeader, hdr_crc)); } bool cfs_journal_header_crc_ok(const CfsJournalFileHeader *hdr) { pg_crc32c expected = cfs_format_crc(hdr, offsetof(CfsJournalFileHeader, hdr_crc)); return EQ_CRC32C(expected, hdr->hdr_crc); } void cfs_journal_header_init(CfsJournalFileHeader *hdr) { MemSet(hdr, 0, sizeof(CfsJournalFileHeader)); hdr->magic = CFS_JRN_MAGIC; hdr->version = CFS_JRN_VERSION; cfs_journal_header_set_crc(hdr); ereport(DEBUG1, (errmsg("CFS: initialised page map journal header"))); } CfsFormatStatus cfs_journal_header_validate(const CfsJournalFileHeader *hdr) { if (hdr->magic != CFS_JRN_MAGIC) return CFS_FORMAT_BAD_MAGIC; if (hdr->version != CFS_JRN_VERSION) return CFS_FORMAT_BAD_VERSION; if (!cfs_journal_header_crc_ok(hdr)) return CFS_FORMAT_BAD_CRC; return CFS_FORMAT_OK; } /* ---------------------------------------------------------------- * Journal (.cfsjrn) entry * ---------------------------------------------------------------- */ void cfs_journal_entry_crc_set(CfsJournalEntry *e) { /* * Cover every byte up to the trailing entry_crc field. Because init * routines zero-fill the whole record, the 4 alignment bytes between * blocknum and new_entry are deterministic and included here. */ e->entry_crc = cfs_format_crc(e, offsetof(CfsJournalEntry, entry_crc)); } bool cfs_journal_entry_crc_ok(const CfsJournalEntry *e) { pg_crc32c expected = cfs_format_crc(e, offsetof(CfsJournalEntry, entry_crc)); return EQ_CRC32C(expected, e->entry_crc); } void cfs_journal_entry_init_update(CfsJournalEntry *e, uint32 blocknum, const CfsMapEntry *new_entry) { MemSet(e, 0, sizeof(CfsJournalEntry)); e->magic = CFS_JRN_MAGIC; e->op = CFS_JRN_OP_UPDATE; e->blocknum = blocknum; if (new_entry != NULL) e->new_entry = *new_entry; cfs_journal_entry_crc_set(e); } void cfs_journal_entry_init_delete(CfsJournalEntry *e, uint32 blocknum) { MemSet(e, 0, sizeof(CfsJournalEntry)); e->magic = CFS_JRN_MAGIC; e->op = CFS_JRN_OP_DELETE; e->blocknum = blocknum; cfs_journal_entry_crc_set(e); } void cfs_journal_entry_init_resize(CfsJournalEntry *e, uint32 nblocks) { MemSet(e, 0, sizeof(CfsJournalEntry)); e->magic = CFS_JRN_MAGIC; e->op = CFS_JRN_OP_RESIZE; e->blocknum = nblocks; cfs_journal_entry_crc_set(e); } CfsFormatStatus cfs_journal_entry_validate(const CfsJournalEntry *e) { if (e->magic != CFS_JRN_MAGIC) return CFS_FORMAT_BAD_MAGIC; if (e->op != CFS_JRN_OP_UPDATE && e->op != CFS_JRN_OP_DELETE && e->op != CFS_JRN_OP_RESIZE) return CFS_FORMAT_BAD_FIELD; if (!cfs_journal_entry_crc_ok(e)) return CFS_FORMAT_BAD_CRC; if (e->_reserved[0] != 0 || e->_reserved[1] != 0 || e->_reserved[2] != 0) return CFS_FORMAT_BAD_FIELD; return CFS_FORMAT_OK; } /* ---------------------------------------------------------------- * Compressed block (.cfp) header * ---------------------------------------------------------------- */ CfsFormatStatus cfs_block_header_validate(const CfsCompressedBlockHeader *hdr) { pg_crc32c expected; if (hdr->magic != CFS_BLOCK_MAGIC) return CFS_FORMAT_BAD_MAGIC; if (hdr->version != CFS_FORMAT_VERSION) return CFS_FORMAT_BAD_VERSION; if (!CFS_ALGORITHM_VALID((CfsAlgorithm) hdr->algorithm)) return CFS_FORMAT_BAD_FIELD; if (hdr->compressed_size > CFS_BLCKSZ) return CFS_FORMAT_BAD_FIELD; if (hdr->original_size != CFS_BLCKSZ) return CFS_FORMAT_BAD_FIELD; expected = cfs_format_crc(hdr, offsetof(CfsCompressedBlockHeader, hdr_crc)); if (!EQ_CRC32C(expected, hdr->hdr_crc)) return CFS_FORMAT_BAD_CRC; return CFS_FORMAT_OK; }