Исходный код СУБД 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*-------------------------------------------------------------------------
 * 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 <string.h>
 
#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;
}