/*------------------------------------------------------------------------- * cfs_format.h * Compressed File Store (CFS) — on-disk format serialisation and * validation . * * This header declares the routines that initialise, checksum and validate * every on-disk CFS structure defined in cfs.h: * * - CfsMapPrologue / CfsMapEntry (.cfsmap) * - CfsJournalFileHeader / CfsJournalEntry (.cfsjrn) * - CfsCompressedBlockHeader (.cfp) * * Design rules: * * * The validation helpers never raise an error themselves. They return a * CfsFormatStatus and the calling layer decides whether to ERROR, PANIC * or warn. Diagnostic tracing is emitted only at DEBUG1 level. * * * Every structure is zero-filled before population so that any inter-field * padding bytes are deterministic (zero) and therefore covered reliably * by the CRC. Compile-time StaticAssertDecl checks in cfs_format.c lock * the exact on-disk sizes and the offsets of the trailing CRC fields. * * * All checksums are CRC-32C computed with PostgreSQL's pg_crc32c * machinery, covering every byte of the structure up to (but excluding) * the trailing *_crc field. * * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group * * IDENTIFICATION * src/include/storage/cfs/cfs_format.h * *------------------------------------------------------------------------- */ #ifndef CFS_FORMAT_H #define CFS_FORMAT_H #include "storage/cfs/cfs.h" /* ---------------------------------------------------------------- * CfsFormatStatus — outcome of validating an on-disk structure. * * The values intentionally cover every failure mode listed . * CFS_FORMAT_BAD_FIELD is an extension for sanity checks that do not fit the * magic/version/crc/truncated categories (e.g. an out-of-range algorithm id * or an oversized compressed_size). * ---------------------------------------------------------------- */ typedef enum CfsFormatStatus { CFS_FORMAT_OK = 0, /* structure is valid */ CFS_FORMAT_BAD_MAGIC, /* magic number mismatch */ CFS_FORMAT_BAD_VERSION, /* unsupported/incompatible version */ CFS_FORMAT_BAD_CRC, /* CRC-32C mismatch */ CFS_FORMAT_TRUNCATED, /* not enough bytes for a full structure */ CFS_FORMAT_BAD_FIELD, /* a data field is out of range */ } CfsFormatStatus; extern const char *CfsFormatStatusString(CfsFormatStatus status); /* ---------------------------------------------------------------- * Page Map (.cfsmap) prologue helpers * ---------------------------------------------------------------- */ /* Initialise the static 8-byte .cfsmap prologue. */ extern void cfs_map_prologue_init(CfsMapPrologue *prologue); /* Validate magic, version and reserved. Never raises. */ extern CfsFormatStatus cfs_map_prologue_validate(const CfsMapPrologue *prologue); /* True when the row is all zeros: a sparse never-written block. */ extern bool cfs_map_entry_is_hole(const CfsMapEntry *e); /* ---------------------------------------------------------------- * Page Map (.cfsmap) entry helpers * ---------------------------------------------------------------- */ /* Initialise an entry to the "never written" (offset 0) state and set CRC. */ extern void cfs_map_entry_init(CfsMapEntry *e); /* (Re)compute and store e->entry_crc. */ extern void cfs_map_entry_crc_set(CfsMapEntry *e); /* Return true when the stored entry CRC matches the recomputed value. */ extern bool cfs_map_entry_crc_ok(const CfsMapEntry *e); /* * Validate entry CRC and, when 'check_fields' is true, the algorithm id and * compressed_size of live (non-empty) entries. "Never written" entries * (physical_offset == 0) are considered valid. */ extern CfsFormatStatus cfs_map_entry_validate(const CfsMapEntry *e, bool check_fields); /* * One row classification shared by the map loader, the segment loader and * fsck: a row with a valid entry_crc loads as-is; an * all-zero row is a sparse hole (a never-written block) and its INVALID * representation is legal; anything else is corruption. '*status' (when * not NULL) receives the validation status of a CORRUPT row so callers * like fsck can tell a CRC mismatch from a field-contract violation. */ typedef enum CfsMapRowClass { CFS_MAP_ROW_VALID, /* entry passed validation */ CFS_MAP_ROW_HOLE, /* all-zero: never written */ CFS_MAP_ROW_CORRUPT /* non-zero, failed validation */ } CfsMapRowClass; extern CfsMapRowClass cfs_map_row_classify(const CfsMapEntry *e, bool check_fields, CfsFormatStatus *status); /* ---------------------------------------------------------------- * Journal (.cfsjrn) header helpers * ---------------------------------------------------------------- */ extern void cfs_journal_header_init(CfsJournalFileHeader *hdr); extern void cfs_journal_header_set_crc(CfsJournalFileHeader *hdr); extern bool cfs_journal_header_crc_ok(const CfsJournalFileHeader *hdr); extern CfsFormatStatus cfs_journal_header_validate(const CfsJournalFileHeader *hdr); /* ---------------------------------------------------------------- * Journal (.cfsjrn) entry helpers * ---------------------------------------------------------------- */ /* Build an UPDATE record: block 'blocknum' now maps to 'new_entry'. */ extern void cfs_journal_entry_init_update(CfsJournalEntry *e, uint32 blocknum, const CfsMapEntry *new_entry); /* Build a DELETE record: block 'blocknum' is removed from the map. */ extern void cfs_journal_entry_init_delete(CfsJournalEntry *e, uint32 blocknum); /* Build a RESIZE record: logical block count is set to 'nblocks'. */ extern void cfs_journal_entry_init_resize(CfsJournalEntry *e, uint32 nblocks); extern void cfs_journal_entry_crc_set(CfsJournalEntry *e); extern bool cfs_journal_entry_crc_ok(const CfsJournalEntry *e); extern CfsFormatStatus cfs_journal_entry_validate(const CfsJournalEntry *e); /* ---------------------------------------------------------------- * Compressed block (.cfp) header helper * * This complements CfsVerifyBlockHeader() in cfs_compress.c by returning a * structured CfsFormatStatus instead of a boolean, which is what the format * and fsck layers need. * ---------------------------------------------------------------- */ extern CfsFormatStatus cfs_block_header_validate(const CfsCompressedBlockHeader *hdr); #endif /* CFS_FORMAT_H */