Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-------------------------------------------------------------------------
 * 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 */