Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * reconstruct.c
4 : : * Reconstruct full file from incremental file and backup chain.
5 : : *
6 : : * Copyright (c) 2017-2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/bin/pg_combinebackup/reconstruct.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres_fe.h"
14 : :
15 : : #include <unistd.h>
16 : :
17 : : #include "backup/basebackup_incremental.h"
18 : : #include "common/file_perm.h"
19 : : #include "common/logging.h"
20 : : #include "copy_file.h"
21 : : #include "lib/stringinfo.h"
22 : : #include "reconstruct.h"
23 : : #include "storage/block.h"
24 : :
25 : : /*
26 : : * An rfile stores the data that we need in order to be able to use some file
27 : : * on disk for reconstruction. For any given output file, we create one rfile
28 : : * per backup that we need to consult when we constructing that output file.
29 : : *
30 : : * If we find a full version of the file in the backup chain, then only
31 : : * filename and fd are initialized; the remaining fields are 0 or NULL.
32 : : * For an incremental file, header_length, num_blocks, relative_block_numbers,
33 : : * and truncation_block_length are also set.
34 : : *
35 : : * num_blocks_read and highest_offset_read always start out as 0.
36 : : */
37 : : typedef struct rfile
38 : : {
39 : : char *filename;
40 : : int fd;
41 : : size_t header_length;
42 : : unsigned num_blocks;
43 : : BlockNumber *relative_block_numbers;
44 : : unsigned truncation_block_length;
45 : : unsigned num_blocks_read;
46 : : off_t highest_offset_read;
47 : : } rfile;
48 : :
49 : : static void debug_reconstruction(int n_source,
50 : : rfile **sources,
51 : : bool dry_run);
52 : : static unsigned find_reconstructed_block_length(const rfile *s);
53 : : static rfile *make_incremental_rfile(const char *filename);
54 : : static rfile *make_rfile(const char *filename, bool missing_ok);
55 : : static void write_reconstructed_file(const char *input_filename,
56 : : const char *output_filename,
57 : : unsigned block_length,
58 : : rfile **sourcemap,
59 : : const off_t *offsetmap,
60 : : pg_checksum_context *checksum_ctx,
61 : : CopyMethod copy_method,
62 : : bool debug,
63 : : bool dry_run);
64 : : static void read_bytes(const rfile *rf, void *buffer, size_t length);
65 : : static void write_block(int fd, const char *output_filename,
66 : : const uint8 *buffer,
67 : : pg_checksum_context *checksum_ctx);
68 : : static void read_block(const rfile *s, off_t off, uint8 *buffer);
69 : :
70 : : /*
71 : : * Reconstruct a full file from an incremental file and a chain of prior
72 : : * backups.
73 : : *
74 : : * input_filename should be the path to the incremental file, and
75 : : * output_filename should be the path where the reconstructed file is to be
76 : : * written.
77 : : *
78 : : * relative_path should be the path to the directory containing this file,
79 : : * relative to the root of the backup (NOT relative to the root of the
80 : : * tablespace). It must always end with a trailing slash. bare_file_name
81 : : * should be the name of the file within that directory, without
82 : : * "INCREMENTAL.".
83 : : *
84 : : * n_prior_backups is the number of prior backups, and prior_backup_dirs is
85 : : * an array of pathnames where those backups can be found.
86 : : */
87 : : void
1074 rhaas@postgresql.org 88 :CBC 9158 : reconstruct_from_incremental_file(char *input_filename,
89 : : char *output_filename,
90 : : char *relative_path,
91 : : char *bare_file_name,
92 : : int n_prior_backups,
93 : : char **prior_backup_dirs,
94 : : manifest_data **manifests,
95 : : char *manifest_path,
96 : : pg_checksum_type checksum_type,
97 : : int *checksum_length,
98 : : uint8 **checksum_payload,
99 : : CopyMethod copy_method,
100 : : bool debug,
101 : : bool dry_run)
102 : : {
103 : : rfile **source;
104 : 9158 : rfile *latest_source = NULL;
105 : : rfile **sourcemap;
106 : : off_t *offsetmap;
107 : : unsigned block_length;
108 : : unsigned truncation_block_length;
109 : 9158 : unsigned sidx = n_prior_backups;
110 : 9158 : bool full_copy_possible = true;
111 : 9158 : int copy_source_index = -1;
112 : 9158 : rfile *copy_source = NULL;
113 : : pg_checksum_context checksum_ctx;
114 : :
115 : : /* Sanity check the relative_path. */
754 116 [ - + ]: 9158 : Assert(relative_path[0] != '\0');
117 [ - + ]: 9158 : Assert(relative_path[strlen(relative_path) - 1] == '/');
118 : :
119 : : /*
120 : : * Every block must come either from the latest version of the file or
121 : : * from one of the prior backups.
122 : : */
274 michael@paquier.xyz 123 : 9158 : source = pg_malloc0_array(rfile *, 1 + n_prior_backups);
124 : :
125 : : /*
126 : : * Use the information from the latest incremental file to figure out how
127 : : * long the reconstructed file should be.
128 : : */
1074 rhaas@postgresql.org 129 : 9158 : latest_source = make_incremental_rfile(input_filename);
130 : 9158 : source[n_prior_backups] = latest_source;
131 : 9158 : block_length = find_reconstructed_block_length(latest_source);
9 fujii@postgresql.org 132 : 9158 : truncation_block_length = latest_source->truncation_block_length;
133 : :
134 : : /*
135 : : * For each block in the output file, we need to know from which file we
136 : : * need to obtain it and at what offset in that file it's stored.
137 : : * sourcemap gives us the first of these things, and offsetmap the latter.
138 : : */
274 michael@paquier.xyz 139 : 9158 : sourcemap = pg_malloc0_array(rfile *, block_length);
140 : 9158 : offsetmap = pg_malloc0_array(off_t, block_length);
141 : :
142 : : /*
143 : : * Every block that is present in the newest incremental file should be
144 : : * sourced from that file. If it precedes the truncation_block_length,
145 : : * it's a block that we would otherwise have had to find in an older
146 : : * backup and thus reduces the number of blocks remaining to be found by
147 : : * one; otherwise, it's an extra block that needs to be included in the
148 : : * output but would not have needed to be found in an older backup if it
149 : : * had not been present.
150 : : */
140 peter@eisentraut.org 151 [ + + ]:GNC 9199 : for (unsigned i = 0; i < latest_source->num_blocks; ++i)
152 : : {
1074 rhaas@postgresql.org 153 :CBC 41 : BlockNumber b = latest_source->relative_block_numbers[i];
154 : :
155 [ - + ]: 41 : Assert(b < block_length);
156 : 41 : sourcemap[b] = latest_source;
157 : 41 : offsetmap[b] = latest_source->header_length + (i * BLCKSZ);
158 : :
159 : : /*
160 : : * A full copy of a file from an earlier backup is only possible if no
161 : : * blocks are needed from any later incremental file.
162 : : */
163 : 41 : full_copy_possible = false;
164 : : }
165 : :
166 : : while (1)
167 : 1338 : {
168 : : char source_filename[MAXPGPATH];
169 : : rfile *s;
170 : :
171 : : /*
172 : : * Move to the next backup in the chain. If there are no more, then
173 : : * we're done.
174 : : */
175 [ + + ]: 10496 : if (sidx == 0)
176 : 1 : break;
177 : 10495 : --sidx;
178 : :
179 : : /*
180 : : * Look for the full file in the previous backup. If not found, then
181 : : * look for an incremental file instead.
182 : : */
754 183 : 10495 : snprintf(source_filename, MAXPGPATH, "%s/%s%s",
1074 184 : 10495 : prior_backup_dirs[sidx], relative_path, bare_file_name);
185 [ + + ]: 10495 : if ((s = make_rfile(source_filename, true)) == NULL)
186 : : {
754 187 : 1338 : snprintf(source_filename, MAXPGPATH, "%s/%sINCREMENTAL.%s",
1074 188 : 1338 : prior_backup_dirs[sidx], relative_path, bare_file_name);
189 : 1338 : s = make_incremental_rfile(source_filename);
190 : : }
191 : 10495 : source[sidx] = s;
192 : :
193 : : /*
194 : : * If s->header_length == 0, then this is a full file; otherwise, it's
195 : : * an incremental file.
196 : : */
197 [ + + ]: 10495 : if (s->header_length == 0)
198 : : {
199 : : struct stat sb;
200 : : BlockNumber b;
201 : : BlockNumber blocklength;
202 : :
203 : : /* We need to know the length of the file. */
204 [ - + ]: 9157 : if (fstat(s->fd, &sb) < 0)
890 peter@eisentraut.org 205 :UBC 0 : pg_fatal("could not stat file \"%s\": %m", s->filename);
206 : :
207 : : /*
208 : : * Since we found a full file, source all blocks from it that
209 : : * exist in the file.
210 : : *
211 : : * Note that there may be blocks that don't exist either in this
212 : : * file or in any incremental file but that precede
213 : : * truncation_block_length. These are, presumably, zero-filled
214 : : * blocks that result from the server extending the file but
215 : : * taking no action on those blocks that generated any WAL.
216 : : *
217 : : * Sadly, we have no way of validating that this is really what
218 : : * happened, and neither does the server. From its perspective,
219 : : * an unmodified block that contains data looks exactly the same
220 : : * as a zero-filled block that never had any data: either way,
221 : : * it's not mentioned in any WAL summary and the server has no
222 : : * reason to read it. From our perspective, all we know is that
223 : : * nobody had a reason to back up the block. That certainly means
224 : : * that the block didn't exist at the time of the full backup, but
225 : : * the supposition that it was all zeroes at the time of every
226 : : * later backup is one that we can't validate.
227 : : */
1074 rhaas@postgresql.org 228 :CBC 9157 : blocklength = sb.st_size / BLCKSZ;
9 fujii@postgresql.org 229 [ + + ]: 44162 : for (b = 0; b < truncation_block_length; ++b)
230 : : {
1074 rhaas@postgresql.org 231 [ + + + - ]: 35005 : if (sourcemap[b] == NULL && b < blocklength)
232 : : {
233 : 34964 : sourcemap[b] = s;
234 : 34964 : offsetmap[b] = b * BLCKSZ;
235 : : }
236 : : }
237 : :
238 : : /*
239 : : * If a full copy looks possible, check whether the resulting file
240 : : * should be exactly as long as the source file is. If so, a full
241 : : * copy is acceptable, otherwise not.
242 : : */
243 [ + + ]: 9157 : if (full_copy_possible)
244 : : {
245 : : uint64 expected_length;
246 : :
247 : 9128 : expected_length =
248 : 9128 : (uint64) latest_source->truncation_block_length;
249 : 9128 : expected_length *= BLCKSZ;
250 [ + + ]: 9128 : if (expected_length == sb.st_size)
251 : : {
252 : 9126 : copy_source = s;
253 : 9126 : copy_source_index = sidx;
254 : : }
255 : : }
256 : :
257 : : /* We don't need to consider any further sources. */
258 : 9157 : break;
259 : : }
260 : :
261 : : /*
262 : : * Since we found another incremental file, source all blocks from it
263 : : * that we need but don't yet have.
264 : : */
140 peter@eisentraut.org 265 [ - + ]:GNC 1338 : for (unsigned i = 0; i < s->num_blocks; ++i)
266 : : {
1074 rhaas@postgresql.org 267 :UBC 0 : BlockNumber b = s->relative_block_numbers[i];
268 : :
9 fujii@postgresql.org 269 [ # # ]: 0 : if (b < truncation_block_length &&
1074 rhaas@postgresql.org 270 [ # # ]: 0 : sourcemap[b] == NULL)
271 : : {
272 : 0 : sourcemap[b] = s;
273 : 0 : offsetmap[b] = s->header_length + (i * BLCKSZ);
274 : :
275 : : /*
276 : : * A full copy of a file from an earlier backup is only
277 : : * possible if no blocks are needed from any later incremental
278 : : * file.
279 : : */
280 : 0 : full_copy_possible = false;
281 : : }
282 : : }
283 : :
284 : : /*
285 : : * Blocks at or beyond this incremental file's truncation_block_length
286 : : * were either not present or truncated away before this backup, so
287 : : * their contents in any earlier backup are stale and must not be
288 : : * used. This also prevents copying an earlier full file whole even if
289 : : * its size happens to match latest_source->truncation_block_length.
290 : : */
9 fujii@postgresql.org 291 [ - + ]:CBC 1338 : if (s->truncation_block_length < truncation_block_length)
292 : : {
9 fujii@postgresql.org 293 :UBC 0 : truncation_block_length = s->truncation_block_length;
294 : 0 : full_copy_possible = false;
295 : : }
296 : : }
297 : :
298 : : /*
299 : : * If a checksum of the required type already exists in the
300 : : * backup_manifest for the relevant input directory, we can save some work
301 : : * by reusing that checksum instead of computing a new one.
302 : : */
1074 rhaas@postgresql.org 303 [ + + + - :CBC 9158 : if (copy_source_index >= 0 && manifests[copy_source_index] != NULL &&
+ - ]
304 : : checksum_type != CHECKSUM_TYPE_NONE)
305 : : {
306 : : manifest_file *mfile;
307 : :
308 : 9126 : mfile = manifest_files_lookup(manifests[copy_source_index]->files,
309 : : manifest_path);
310 [ - + ]: 9126 : if (mfile == NULL)
311 : : {
1074 rhaas@postgresql.org 312 :UBC 0 : char *path = psprintf("%s/backup_manifest",
313 : 0 : prior_backup_dirs[copy_source_index]);
314 : :
315 : : /*
316 : : * The directory is out of sync with the backup_manifest, so emit
317 : : * a warning.
318 : : */
890 peter@eisentraut.org 319 : 0 : pg_log_warning("manifest file \"%s\" contains no entry for file \"%s\"",
320 : : path,
321 : : manifest_path);
1074 rhaas@postgresql.org 322 : 0 : pfree(path);
323 : : }
1074 rhaas@postgresql.org 324 [ + - ]:CBC 9126 : else if (mfile->checksum_type == checksum_type)
325 : : {
326 : 9126 : *checksum_length = mfile->checksum_length;
327 : 9126 : *checksum_payload = pg_malloc(*checksum_length);
328 : 9126 : memcpy(*checksum_payload, mfile->checksum_payload,
329 : 9126 : *checksum_length);
330 : 9126 : checksum_type = CHECKSUM_TYPE_NONE;
331 : : }
332 : : }
333 : :
334 : : /* Prepare for checksum calculation, if required. */
335 : 9158 : pg_checksum_init(&checksum_ctx, checksum_type);
336 : :
337 : : /*
338 : : * If the full file can be created by copying a file from an older backup
339 : : * in the chain without needing to overwrite any blocks or truncate the
340 : : * result, then forget about performing reconstruction and just copy that
341 : : * file in its entirety.
342 : : *
343 : : * If we have only incremental files, and there's no full file at any
344 : : * point in the backup chain, something has gone wrong. Emit an error.
345 : : *
346 : : * Otherwise, reconstruct.
347 : : */
348 [ + + ]: 9158 : if (copy_source != NULL)
349 : 9126 : copy_file(copy_source->filename, output_filename,
350 : : &checksum_ctx, copy_method, dry_run);
754 351 [ + - + + ]: 32 : else if (sidx == 0 && source[0]->header_length != 0)
352 : : {
353 : 1 : pg_fatal("full backup contains unexpected incremental file \"%s\"",
354 : : source[0]->filename);
355 : : }
356 : : else
357 : : {
1074 358 : 31 : write_reconstructed_file(input_filename, output_filename,
359 : : block_length, sourcemap, offsetmap,
360 : : &checksum_ctx, copy_method,
361 : : debug, dry_run);
362 : 31 : debug_reconstruction(n_prior_backups + 1, source, dry_run);
363 : : }
364 : :
365 : : /* Save results of checksum calculation. */
366 [ + + ]: 9157 : if (checksum_type != CHECKSUM_TYPE_NONE)
367 : : {
368 : 31 : *checksum_payload = pg_malloc(PG_CHECKSUM_MAX_LENGTH);
369 : 31 : *checksum_length = pg_checksum_final(&checksum_ctx,
370 : : *checksum_payload);
371 : : }
372 : :
373 : : /*
374 : : * Close files and release memory.
375 : : */
140 peter@eisentraut.org 376 [ + + ]:GNC 28808 : for (int i = 0; i <= n_prior_backups; ++i)
377 : : {
1074 rhaas@postgresql.org 378 :CBC 19651 : rfile *s = source[i];
379 : :
380 [ - + ]: 19651 : if (s == NULL)
1074 rhaas@postgresql.org 381 :UBC 0 : continue;
1074 rhaas@postgresql.org 382 [ - + ]:CBC 19651 : if (close(s->fd) != 0)
890 peter@eisentraut.org 383 :UBC 0 : pg_fatal("could not close file \"%s\": %m", s->filename);
1074 rhaas@postgresql.org 384 [ + + ]:CBC 19651 : if (s->relative_block_numbers != NULL)
385 : 29 : pfree(s->relative_block_numbers);
386 : 19651 : pg_free(s->filename);
402 tgl@sss.pgh.pa.us 387 : 19651 : pg_free(s);
388 : : }
150 peter@eisentraut.org 389 :GNC 9157 : pg_free(sourcemap);
390 : 9157 : pg_free(offsetmap);
391 : 9157 : pg_free(source);
1074 rhaas@postgresql.org 392 :CBC 9157 : }
393 : :
394 : : /*
395 : : * Perform post-reconstruction logging and sanity checks.
396 : : */
397 : : static void
398 : 31 : debug_reconstruction(int n_source, rfile **sources, bool dry_run)
399 : : {
140 peter@eisentraut.org 400 [ + + ]:GNC 95 : for (int i = 0; i < n_source; ++i)
401 : : {
1074 rhaas@postgresql.org 402 :CBC 64 : rfile *s = sources[i];
403 : :
404 : : /* Ignore source if not used. */
405 [ - + ]: 64 : if (s == NULL)
1074 rhaas@postgresql.org 406 :UBC 0 : continue;
407 : :
408 : : /* If no data is needed from this file, we can ignore it. */
1074 rhaas@postgresql.org 409 [ + + ]:CBC 64 : if (s->num_blocks_read == 0)
410 : 4 : continue;
411 : :
412 : : /* Debug logging. */
413 [ - + ]: 60 : if (dry_run)
1074 rhaas@postgresql.org 414 [ # # ]:UBC 0 : pg_log_debug("would have read %u blocks from \"%s\"",
415 : : s->num_blocks_read, s->filename);
416 : : else
1074 rhaas@postgresql.org 417 [ + - ]:CBC 60 : pg_log_debug("read %u blocks from \"%s\"",
418 : : s->num_blocks_read, s->filename);
419 : :
420 : : /*
421 : : * In dry-run mode, we don't actually try to read data from the file,
422 : : * but we do try to verify that the file is long enough that we could
423 : : * have read the data if we'd tried.
424 : : *
425 : : * If this fails, then it means that a non-dry-run attempt would fail,
426 : : * complaining of not being able to read the required bytes from the
427 : : * file.
428 : : */
429 [ - + ]: 60 : if (dry_run)
430 : : {
431 : : struct stat sb;
432 : :
1074 rhaas@postgresql.org 433 [ # # ]:UBC 0 : if (fstat(s->fd, &sb) < 0)
890 peter@eisentraut.org 434 : 0 : pg_fatal("could not stat file \"%s\": %m", s->filename);
1074 rhaas@postgresql.org 435 [ # # ]: 0 : if (sb.st_size < s->highest_offset_read)
144 peter@eisentraut.org 436 :UNC 0 : pg_fatal("file \"%s\" is too short: expected %lld, found %lld",
437 : : s->filename,
438 : : (long long) s->highest_offset_read,
439 : : (long long) sb.st_size);
440 : : }
441 : : }
1074 rhaas@postgresql.org 442 :CBC 31 : }
443 : :
444 : : /*
445 : : * When we perform reconstruction using an incremental file, the output file
446 : : * should be at least as long as the truncation_block_length. Any blocks
447 : : * present in the incremental file increase the output length as far as is
448 : : * necessary to include those blocks.
449 : : */
450 : : static unsigned
138 peter@eisentraut.org 451 :GNC 9158 : find_reconstructed_block_length(const rfile *s)
452 : : {
1074 rhaas@postgresql.org 453 :CBC 9158 : unsigned block_length = s->truncation_block_length;
454 : : unsigned i;
455 : :
456 [ + + ]: 9199 : for (i = 0; i < s->num_blocks; ++i)
457 [ - + ]: 41 : if (s->relative_block_numbers[i] >= block_length)
1074 rhaas@postgresql.org 458 :UBC 0 : block_length = s->relative_block_numbers[i] + 1;
459 : :
1074 rhaas@postgresql.org 460 :CBC 9158 : return block_length;
461 : : }
462 : :
463 : : /*
464 : : * Initialize an incremental rfile, reading the header so that we know which
465 : : * blocks it contains.
466 : : */
467 : : static rfile *
138 peter@eisentraut.org 468 :GNC 10496 : make_incremental_rfile(const char *filename)
469 : : {
470 : : rfile *rf;
471 : : unsigned magic;
472 : :
1074 rhaas@postgresql.org 473 :CBC 10496 : rf = make_rfile(filename, false);
474 : :
475 : : /* Read and validate magic number. */
476 : 10496 : read_bytes(rf, &magic, sizeof(magic));
477 [ - + ]: 10496 : if (magic != INCREMENTAL_MAGIC)
823 peter@eisentraut.org 478 :UBC 0 : pg_fatal("file \"%s\" has bad incremental magic number (0x%x, expected 0x%x)",
479 : : filename, magic, INCREMENTAL_MAGIC);
480 : :
481 : : /* Read block count. */
1074 rhaas@postgresql.org 482 :CBC 10496 : read_bytes(rf, &rf->num_blocks, sizeof(rf->num_blocks));
483 [ - + ]: 10496 : if (rf->num_blocks > RELSEG_SIZE)
1074 rhaas@postgresql.org 484 :UBC 0 : pg_fatal("file \"%s\" has block count %u in excess of segment size %u",
485 : : filename, rf->num_blocks, RELSEG_SIZE);
486 : :
487 : : /* Read truncation block length. */
1074 rhaas@postgresql.org 488 :CBC 10496 : read_bytes(rf, &rf->truncation_block_length,
489 : : sizeof(rf->truncation_block_length));
490 [ - + ]: 10496 : if (rf->truncation_block_length > RELSEG_SIZE)
1074 rhaas@postgresql.org 491 :UBC 0 : pg_fatal("file \"%s\" has truncation block length %u in excess of segment size %u",
492 : : filename, rf->truncation_block_length, RELSEG_SIZE);
493 : :
494 : : /* Read block numbers if there are any. */
1074 rhaas@postgresql.org 495 [ + + ]:CBC 10496 : if (rf->num_blocks > 0)
496 : : {
497 : 29 : rf->relative_block_numbers =
274 michael@paquier.xyz 498 : 29 : pg_malloc0_array(BlockNumber, rf->num_blocks);
1074 rhaas@postgresql.org 499 : 29 : read_bytes(rf, rf->relative_block_numbers,
500 : 29 : sizeof(BlockNumber) * rf->num_blocks);
33 peter@eisentraut.org 501 [ + + ]: 70 : for (unsigned i = 0; i < rf->num_blocks; i++)
502 : : {
503 [ - + ]: 41 : if (rf->relative_block_numbers[i] >= RELSEG_SIZE)
33 peter@eisentraut.org 504 :UBC 0 : pg_fatal("file \"%s\" has block number %u in excess of segment size %u",
505 : : filename, rf->relative_block_numbers[i], RELSEG_SIZE);
506 : : }
507 : : }
508 : :
509 : : /* Remember length of header. */
1074 rhaas@postgresql.org 510 :CBC 10496 : rf->header_length = sizeof(magic) + sizeof(rf->num_blocks) +
511 : 10496 : sizeof(rf->truncation_block_length) +
512 : 10496 : sizeof(BlockNumber) * rf->num_blocks;
513 : :
514 : : /*
515 : : * Round header length to a multiple of BLCKSZ, so that blocks contents
516 : : * are properly aligned. Only do this when the file actually has data for
517 : : * some blocks.
518 : : */
967 tomas.vondra@postgre 519 [ + + + - ]: 10496 : if ((rf->num_blocks > 0) && ((rf->header_length % BLCKSZ) != 0))
520 : 29 : rf->header_length += (BLCKSZ - (rf->header_length % BLCKSZ));
521 : :
1074 rhaas@postgresql.org 522 : 10496 : return rf;
523 : : }
524 : :
525 : : /*
526 : : * Allocate and perform basic initialization of an rfile.
527 : : */
528 : : static rfile *
138 peter@eisentraut.org 529 :GNC 20991 : make_rfile(const char *filename, bool missing_ok)
530 : : {
531 : : rfile *rf;
532 : :
274 michael@paquier.xyz 533 :CBC 20991 : rf = pg_malloc0_object(rfile);
1074 rhaas@postgresql.org 534 : 20991 : rf->filename = pstrdup(filename);
535 [ + + ]: 20991 : if ((rf->fd = open(filename, O_RDONLY | PG_BINARY, 0)) < 0)
536 : : {
537 [ + - + - ]: 1338 : if (missing_ok && errno == ENOENT)
538 : : {
150 peter@eisentraut.org 539 :GNC 1338 : pfree(rf->filename);
1074 rhaas@postgresql.org 540 :CBC 1338 : pg_free(rf);
541 : 1338 : return NULL;
542 : : }
1074 rhaas@postgresql.org 543 :UBC 0 : pg_fatal("could not open file \"%s\": %m", filename);
544 : : }
545 : :
1074 rhaas@postgresql.org 546 :CBC 19653 : return rf;
547 : : }
548 : :
549 : : /*
550 : : * Read the indicated number of bytes from an rfile into the buffer.
551 : : */
552 : : static void
136 peter@eisentraut.org 553 :GNC 31517 : read_bytes(const rfile *rf, void *buffer, size_t length)
554 : : {
555 : : ssize_t rb;
556 : :
557 : 31517 : rb = read(rf->fd, buffer, length);
558 : :
1074 rhaas@postgresql.org 559 [ - + ]:CBC 31517 : if (rb != length)
560 : : {
1074 rhaas@postgresql.org 561 [ # # ]:UBC 0 : if (rb < 0)
562 : 0 : pg_fatal("could not read file \"%s\": %m", rf->filename);
563 : : else
136 peter@eisentraut.org 564 :UNC 0 : pg_fatal("could not read file \"%s\": read %zd of %zu",
565 : : rf->filename, rb, length);
566 : : }
1074 rhaas@postgresql.org 567 :CBC 31517 : }
568 : :
569 : : /*
570 : : * Write out a reconstructed file.
571 : : */
572 : : static void
138 peter@eisentraut.org 573 :GNC 31 : write_reconstructed_file(const char *input_filename,
574 : : const char *output_filename,
575 : : unsigned block_length,
576 : : rfile **sourcemap,
577 : : const off_t *offsetmap,
578 : : pg_checksum_context *checksum_ctx,
579 : : CopyMethod copy_method,
580 : : bool debug,
581 : : bool dry_run)
582 : : {
1074 rhaas@postgresql.org 583 :CBC 31 : int wfd = -1;
584 : : unsigned i;
585 : 31 : unsigned zero_blocks = 0;
586 : :
587 : : /* Debugging output. */
588 [ + - ]: 31 : if (debug)
589 : : {
590 : : StringInfoData debug_buf;
591 : 31 : unsigned start_of_range = 0;
592 : 31 : unsigned current_block = 0;
593 : :
594 : : /* Basic information about the output file to be produced. */
595 [ - + ]: 31 : if (dry_run)
1074 rhaas@postgresql.org 596 [ # # ]:UBC 0 : pg_log_debug("would reconstruct \"%s\" (%u blocks, checksum %s)",
597 : : output_filename, block_length,
598 : : pg_checksum_type_name(checksum_ctx->type));
599 : : else
1074 rhaas@postgresql.org 600 [ + - ]:CBC 31 : pg_log_debug("reconstructing \"%s\" (%u blocks, checksum %s)",
601 : : output_filename, block_length,
602 : : pg_checksum_type_name(checksum_ctx->type));
603 : :
604 : : /* Print out the plan for reconstructing this file. */
605 : 31 : initStringInfo(&debug_buf);
606 [ + + ]: 449 : while (current_block < block_length)
607 : : {
608 : 418 : rfile *s = sourcemap[current_block];
609 : :
610 : : /* Extend range, if possible. */
611 [ + + ]: 418 : if (current_block + 1 < block_length &&
612 [ + + ]: 387 : s == sourcemap[current_block + 1])
613 : : {
614 : 345 : ++current_block;
615 : 345 : continue;
616 : : }
617 : :
618 : : /* Add details about this range. */
619 [ - + ]: 73 : if (s == NULL)
620 : : {
1074 rhaas@postgresql.org 621 [ # # ]:UBC 0 : if (current_block == start_of_range)
622 : 0 : appendStringInfo(&debug_buf, " %u:zero", current_block);
623 : : else
624 : 0 : appendStringInfo(&debug_buf, " %u-%u:zero",
625 : : start_of_range, current_block);
626 : : }
627 : : else
628 : : {
1074 rhaas@postgresql.org 629 [ + + ]:CBC 73 : if (current_block == start_of_range)
630 : 45 : appendStringInfo(&debug_buf, " %u:%s@" UINT64_FORMAT,
631 : : current_block, s->filename,
632 : 45 : (uint64) offsetmap[current_block]);
633 : : else
634 : 28 : appendStringInfo(&debug_buf, " %u-%u:%s@" UINT64_FORMAT,
635 : : start_of_range, current_block,
636 : : s->filename,
637 : 28 : (uint64) offsetmap[current_block]);
638 : : }
639 : :
640 : : /* Begin new range. */
641 : 73 : start_of_range = ++current_block;
642 : :
643 : : /* If the output is very long or we are done, dump it now. */
644 [ + + - + ]: 73 : if (current_block == block_length || debug_buf.len > 1024)
645 : : {
646 [ + - ]: 31 : pg_log_debug("reconstruction plan:%s", debug_buf.data);
647 : 31 : resetStringInfo(&debug_buf);
648 : : }
649 : : }
650 : :
651 : : /* Free memory. */
652 : 31 : pfree(debug_buf.data);
653 : : }
654 : :
655 : : /* Open the output file, except in dry_run mode. */
656 [ + - - + ]: 62 : if (!dry_run &&
657 : 31 : (wfd = open(output_filename,
658 : : O_RDWR | PG_BINARY | O_CREAT | O_EXCL,
659 : : pg_file_create_mode)) < 0)
1074 rhaas@postgresql.org 660 :UBC 0 : pg_fatal("could not open file \"%s\": %m", output_filename);
661 : :
662 : : /* Read and write the blocks as required. */
1074 rhaas@postgresql.org 663 [ + + ]:CBC 449 : for (i = 0; i < block_length; ++i)
664 : : {
665 : : uint8 buffer[BLCKSZ];
666 : 418 : rfile *s = sourcemap[i];
667 : :
668 : : /* Update accounting information. */
669 [ - + ]: 418 : if (s == NULL)
1074 rhaas@postgresql.org 670 :UBC 0 : ++zero_blocks;
671 : : else
672 : : {
1074 rhaas@postgresql.org 673 :CBC 418 : s->num_blocks_read++;
674 : 418 : s->highest_offset_read = Max(s->highest_offset_read,
675 : : offsetmap[i] + BLCKSZ);
676 : : }
677 : :
678 : : /* Skip the rest of this in dry-run mode. */
679 [ - + ]: 418 : if (dry_run)
1074 rhaas@postgresql.org 680 :UBC 0 : continue;
681 : :
682 : : /* Read or zero-fill the block as appropriate. */
1074 rhaas@postgresql.org 683 [ - + ]:CBC 418 : if (s == NULL)
684 : : {
685 : : /*
686 : : * New block not mentioned in the WAL summary. Should have been an
687 : : * uninitialized block, so just zero-fill it.
688 : : */
1074 rhaas@postgresql.org 689 :UBC 0 : memset(buffer, 0, BLCKSZ);
690 : :
691 : : /* Write out the block, update the checksum if needed. */
967 tomas.vondra@postgre 692 : 0 : write_block(wfd, output_filename, buffer, checksum_ctx);
693 : :
694 : : /* Nothing else to do for zero-filled blocks. */
695 : 0 : continue;
696 : : }
697 : :
698 : : /* Copy the block using the appropriate copy method. */
967 tomas.vondra@postgre 699 [ + - ]:CBC 418 : if (copy_method != COPY_METHOD_COPY_FILE_RANGE)
700 : : {
701 : : /*
702 : : * Read the block from the correct source file, and then write it
703 : : * out, possibly with a checksum update.
704 : : */
705 : 418 : read_block(s, offsetmap[i], buffer);
706 : 418 : write_block(wfd, output_filename, buffer, checksum_ctx);
707 : : }
708 : : else /* use copy_file_range */
709 : : {
710 : : #if defined(HAVE_COPY_FILE_RANGE)
711 : : /* copy_file_range modifies the offset, so use a local copy */
967 tomas.vondra@postgre 712 :UBC 0 : off_t off = offsetmap[i];
713 : 0 : size_t nwritten = 0;
714 : :
715 : : /*
716 : : * Retry until we've written all the bytes (the offset is updated
717 : : * by copy_file_range, and so is the wfd file offset).
718 : : */
719 : : do
720 : : {
721 : : ssize_t wb;
722 : :
723 : 0 : wb = copy_file_range(s->fd, &off, wfd, NULL, BLCKSZ - nwritten, 0);
724 : :
725 [ # # ]: 0 : if (wb < 0)
726 : 0 : pg_fatal("error while copying file range from \"%s\" to \"%s\": %m",
727 : : input_filename, output_filename);
152 peter@eisentraut.org 728 [ # # ]: 0 : else if (wb == 0)
729 : 0 : pg_fatal("unexpected end of file while copying file range from \"%s\" to \"%s\"",
730 : : input_filename, output_filename);
731 : :
967 tomas.vondra@postgre 732 : 0 : nwritten += wb;
733 : :
734 [ # # ]: 0 : } while (BLCKSZ > nwritten);
735 : :
736 : : /*
737 : : * When checksum calculation not needed, we're done, otherwise
738 : : * read the block and pass it to the checksum calculation.
739 : : */
740 [ # # ]: 0 : if (checksum_ctx->type == CHECKSUM_TYPE_NONE)
741 : 0 : continue;
742 : :
743 : 0 : read_block(s, offsetmap[i], buffer);
744 : :
745 [ # # ]: 0 : if (pg_checksum_update(checksum_ctx, buffer, BLCKSZ) < 0)
746 : 0 : pg_fatal("could not update checksum of file \"%s\"",
747 : : output_filename);
748 : : #else
749 : : pg_fatal("copy_file_range not supported on this platform");
750 : : #endif
751 : : }
752 : : }
753 : :
754 : : /* Debugging output. */
1074 rhaas@postgresql.org 755 [ - + ]:CBC 31 : if (zero_blocks > 0)
756 : : {
1074 rhaas@postgresql.org 757 [ # # ]:UBC 0 : if (dry_run)
758 [ # # ]: 0 : pg_log_debug("would have zero-filled %u blocks", zero_blocks);
759 : : else
760 [ # # ]: 0 : pg_log_debug("zero-filled %u blocks", zero_blocks);
761 : : }
762 : :
763 : : /* Close the output file. */
1074 rhaas@postgresql.org 764 [ + - - + ]:CBC 31 : if (wfd >= 0 && close(wfd) != 0)
890 peter@eisentraut.org 765 :UBC 0 : pg_fatal("could not close file \"%s\": %m", output_filename);
1074 rhaas@postgresql.org 766 :CBC 31 : }
767 : :
768 : : /*
769 : : * Write the block into the file (using the file descriptor), and
770 : : * if needed update the checksum calculation.
771 : : *
772 : : * The buffer is expected to contain BLCKSZ bytes. The filename is
773 : : * provided only for the error message.
774 : : */
775 : : static void
138 peter@eisentraut.org 776 :GNC 418 : write_block(int fd, const char *output_filename,
777 : : const uint8 *buffer, pg_checksum_context *checksum_ctx)
778 : : {
779 : : ssize_t wb;
780 : :
967 tomas.vondra@postgre 781 [ - + ]:CBC 418 : if ((wb = write(fd, buffer, BLCKSZ)) != BLCKSZ)
782 : : {
967 tomas.vondra@postgre 783 [ # # ]:UBC 0 : if (wb < 0)
784 : 0 : pg_fatal("could not write file \"%s\": %m", output_filename);
785 : : else
136 peter@eisentraut.org 786 :UNC 0 : pg_fatal("could not write file \"%s\": wrote %zd of %zu",
787 : : output_filename, wb, (size_t) BLCKSZ);
788 : : }
789 : :
790 : : /* Update the checksum computation. */
967 tomas.vondra@postgre 791 [ - + ]:CBC 418 : if (pg_checksum_update(checksum_ctx, buffer, BLCKSZ) < 0)
967 tomas.vondra@postgre 792 :UBC 0 : pg_fatal("could not update checksum of file \"%s\"",
793 : : output_filename);
967 tomas.vondra@postgre 794 :CBC 418 : }
795 : :
796 : : /*
797 : : * Read a block of data (BLCKSZ bytes) into the buffer.
798 : : */
799 : : static void
138 peter@eisentraut.org 800 :GNC 418 : read_block(const rfile *s, off_t off, uint8 *buffer)
801 : : {
802 : : ssize_t rb;
803 : :
804 : : /* Read the block from the correct source, except if dry-run. */
967 tomas.vondra@postgre 805 :CBC 418 : rb = pg_pread(s->fd, buffer, BLCKSZ, off);
806 [ - + ]: 418 : if (rb != BLCKSZ)
807 : : {
967 tomas.vondra@postgre 808 [ # # ]:UBC 0 : if (rb < 0)
890 peter@eisentraut.org 809 : 0 : pg_fatal("could not read from file \"%s\": %m", s->filename);
810 : : else
136 peter@eisentraut.org 811 :UNC 0 : pg_fatal("could not read from file \"%s\", offset %lld: read %zd of %zu",
812 : : s->filename, (long long) off, rb, (size_t) BLCKSZ);
813 : : }
967 tomas.vondra@postgre 814 :CBC 418 : }
|