/*------------------------------------------------------------------------- * cfs_fsck.h * Compressed File Store (CFS) — structural integrity checker. * * Provides offline/on-demand verification of the three CFS sidecar files * against the on-disk format defined in cfs.h: * * cfs_fsck_map_file() — validate .cfsmap header + entry array * cfs_fsck_journal_file() — validate .cfsjrn header + record stream * cfs_fsck_cfp_file() — walk .cfp block headers * cfs_fsck_relation() — convenience wrapper that builds the paths and * runs all of the above for one relation * * The checkers operate purely on the format layer (cfs_format.c): they never * touch the buffer manager, smgr or the shared map cache, so they are safe to * use from utilities and recovery diagnostics. All diagnostics are emitted at * DEBUG1 / WARNING level; hard errors are counted in CfsFsckResult and the * caller decides how to react. * * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group * * IDENTIFICATION * src/include/storage/cfs/cfs_fsck.h * *------------------------------------------------------------------------- */ #ifndef CFS_FSCK_H #define CFS_FSCK_H #include "common/relpath.h" #include "storage/cfs/cfs.h" #include "storage/cfs/cfs_format.h" #include "storage/relfilelocator.h" typedef struct CfsFsckOptions { /* * Validate field-level invariants (algorithm id, compressed_size <= * BLCKSZ) on live map entries in addition to their CRC. */ bool check_fields; /* * Read the .cfp file and validate every CfsCompressedBlockHeader found * along the append-only record stream. */ bool verify_block_headers; /* * When true, a missing sidecar file is treated as a hard error; when * false it is ignored (useful for relations that have not been written * yet). */ bool require_files; } CfsFsckOptions; #define CFS_FSCK_OPTIONS_DEFAULT \ { .check_fields = true, .verify_block_headers = true, .require_files = true } /* * Cap on the per-block generation tracker used by cfs_fsck_journal_file(). * A multi-gigabyte journal can hold millions of distinct blocks, and tracking * every one unbounded would cost hundreds of MB. Past this cap the * monotonicity check degrades (it is skipped) while CRC and field validation * continue. Worst-case memory use is this many CfsFsckGenEntry hash slots * (a few tens of MB). */ #define CFS_FSCK_GEN_TRACK_MAX (1 << 20) typedef struct CfsFsckResult { /* --- .cfsmap --- */ bool map_header_ok; /* header magic/version/CRC valid */ uint64 map_entries; /* entry slots scanned */ uint64 live_entries; /* non-empty entries */ uint64 empty_entries; /* never-written slots */ uint64 raw_entries; /* live entries stored RAW */ /* --- .cfsjrn --- */ bool journal_header_ok; uint64 journal_records; /* valid records scanned */ /* --- .cfp --- */ uint64 cfp_records; /* block headers scanned */ /* --- problems found --- */ uint64 crc_failures; /* CRC mismatches */ /* * Records/headers whose fields violate the format contract (algorithm, * sizes, flags, reserved) — as opposed to a CRC mismatch. */ uint64 field_failures; uint64 truncated_records; /* short / partial records */ /* * Torn tails at the very end of a sidecar file: a partial trailing * journal record, a partial trailing .cfsmap row (a torn positional * pwrite), or a partial .cfp header / truncated record at the end of the * last (short) segment. For the journal the engine truncates the torn * tail on open; for .cfsmap the loader ignores the partial row and the * next snapshot rewrites the file; for .cfp the tail is never used by the * engine, does not interfere with reading, and compaction will reclaim * the space. All are an expected consequence of a crash, so they are * counted separately and NOT added to 'errors'. */ uint64 torn_tails; /* * Map rows whose entry_crc failed but that a journal record covers (an * UPDATE/DELETE for the block, or a shrink RESIZE dropping it). Such a * row is the expected artifact of a torn fold pwrite: the journal still * holds the block's mapping, so the next fold rewrites the row and the * engine never reports damage. Healed rows are NOT counted in 'errors'. */ uint64 healed_entries; /* map offsets pointing past .cfp EOF */ uint64 offset_out_of_range; uint64 missing_files; /* required sidecar files absent */ /* * Live map entries whose .cfp record header does not match the entry * (deep mode only: cross-check pass over both files). */ uint64 cross_check_failures; /* * generation anomalies: a live map entry or journal UPDATE record with * generation == 0 (always corruption, counted in 'errors'), or a journal * UPDATE whose generation does not exceed the previous record for the * same block (anomaly only, NOT counted in 'errors': writers serialised * by the append lock may compute the generation from a stale local map, * so a regression can appear on a healthy database). */ uint64 generation_violations; /* * True when the per-block generation tracker hit its cap * (CFS_FSCK_GEN_TRACK_MAX) and the monotonicity check was skipped for the * remainder of the scan. Not an error: CRC and field validation are * unaffected. */ bool generation_check_degraded; /* Total number of hard errors detected */ uint64 errors; } CfsFsckResult; /* Zero a result before use. */ extern void cfs_fsck_result_init(CfsFsckResult *result); /* * Optional cross-section coverage collector. cfs_fsck_relation() passes one * to the map and journal scanners so that a map row with a bad CRC is not * judged before the journal has been read: a row whose block a journal * record covers is healed, not damage (see CfsFsckResult.healed_entries). * Pass NULL to keep the standalone behaviour of each scanner: a corrupt row * is then counted immediately. The collector never keeps one entry per * journal block - only per corrupt row, under a fixed cap - so it does not * break the streaming memory contract. */ typedef struct CfsFsckCoverage CfsFsckCoverage; /* Validate a .cfsmap file. 'cfp_path' may be NULL; when present its size is * used to verify that live entries point inside the .cfp file. Returns the * number of hard errors found (0 == clean). With a coverage collector the * corrupt rows are only collected, so the return value is not final: the * caller must resolve them after the journal scan. */ extern int cfs_fsck_map_file(const char *map_path, const char *cfp_path, const CfsFsckOptions *opts, CfsFsckResult *result, CfsFsckCoverage *cov); extern int cfs_fsck_journal_file(const char *journal_path, const CfsFsckOptions *opts, CfsFsckResult *result, CfsFsckCoverage *cov); extern int cfs_fsck_cfp_file(const char *cfp_path, const CfsFsckOptions *opts, CfsFsckResult *result); /* Run map + journal + cfp checks for a relation. Returns hard error count. */ extern int cfs_fsck_relation(RelFileLocatorBackend rlocator, ForkNumber forknum, const CfsFsckOptions *opts, CfsFsckResult *result); #endif /* CFS_FSCK_H */