]> git.proxmox.com Git - ceph.git/blame - ceph/src/pmdk/src/libpmempool/replica.c
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / libpmempool / replica.c
CommitLineData
a4b75251
TL
1// SPDX-License-Identifier: BSD-3-Clause
2/* Copyright 2016-2020, Intel Corporation */
3
4/*
5 * replica.c -- groups all commands for replica manipulation
6 */
7
8#include "replica.h"
9
10#include <errno.h>
11#include <sys/mman.h>
12#include <errno.h>
13#include <fcntl.h>
14#include <stddef.h>
15#include <stdint.h>
16#include <string.h>
17#include <unistd.h>
18#include <libgen.h>
19
20#include "obj.h"
21#include "palloc.h"
22#include "file.h"
23#include "os.h"
24#include "out.h"
25#include "pool_hdr.h"
26#include "set.h"
27#include "util.h"
28#include "uuid.h"
29#include "shutdown_state.h"
30#include "badblocks.h"
31#include "set_badblocks.h"
32
33/*
34 * check_flags_sync -- (internal) check if flags are supported for sync
35 */
36static int
37check_flags_sync(unsigned flags)
38{
39 flags &= ~(PMEMPOOL_SYNC_DRY_RUN | PMEMPOOL_SYNC_FIX_BAD_BLOCKS);
40 return flags > 0;
41}
42
43/*
44 * check_flags_transform -- (internal) check if flags are supported for
45 * transform
46 */
47static int
48check_flags_transform(unsigned flags)
49{
50 flags &= ~PMEMPOOL_TRANSFORM_DRY_RUN;
51 return flags > 0;
52}
53
54/*
55 * replica_align_badblock_offset_length -- align offset and length
56 * of the bad block for the given part
57 */
58void
59replica_align_badblock_offset_length(size_t *offset, size_t *length,
60 struct pool_set *set_in, unsigned repn, unsigned partn)
61{
62 LOG(3, "offset %zu, length %zu, pool_set %p, replica %u, part %u",
63 *offset, *length, set_in, repn, partn);
64
65 size_t alignment = set_in->replica[repn]->part[partn].alignment;
66
67 size_t off = ALIGN_DOWN(*offset, alignment);
68 size_t len = ALIGN_UP(*length + (*offset - off), alignment);
69
70 *offset = off;
71 *length = len;
72}
73
74/*
75 * replica_get_part_data_len -- get data length for given part
76 */
77size_t
78replica_get_part_data_len(struct pool_set *set_in, unsigned repn,
79 unsigned partn)
80{
81 size_t alignment = set_in->replica[repn]->part[partn].alignment;
82 size_t hdrsize = (set_in->options & OPTION_SINGLEHDR) ? 0 : alignment;
83 return ALIGN_DOWN(set_in->replica[repn]->part[partn].filesize,
84 alignment) - ((partn == 0) ? POOL_HDR_SIZE : hdrsize);
85}
86
87/*
88 * replica_get_part_offset -- get part's offset from the beginning of replica
89 */
90uint64_t
91replica_get_part_offset(struct pool_set *set, unsigned repn, unsigned partn)
92{
93 return (uint64_t)set->replica[repn]->part[partn].addr -
94 (uint64_t)set->replica[repn]->part[0].addr;
95}
96
97/*
98 * replica_get_part_data_offset -- get data length before given part
99 */
100uint64_t
101replica_get_part_data_offset(struct pool_set *set, unsigned repn,
102 unsigned partn)
103{
104 if (partn == 0)
105 return POOL_HDR_SIZE;
106
107 return (uint64_t)set->replica[repn]->part[partn].addr -
108 (uint64_t)set->replica[repn]->part[0].addr;
109}
110
111/*
112 * replica_remove_part -- unlink part from replica
113 */
114int
115replica_remove_part(struct pool_set *set, unsigned repn, unsigned partn,
116 int fix_bad_blocks)
117{
118 LOG(3, "set %p repn %u partn %u fix_bad_blocks %i",
119 set, repn, partn, fix_bad_blocks);
120
121 struct pool_set_part *part = PART(REP(set, repn), partn);
122 if (part->fd != -1) {
123 os_close(part->fd);
124 part->fd = -1;
125 }
126
127 int olderrno = errno;
128 enum file_type type = util_file_get_type(part->path);
129 if (type == OTHER_ERROR)
130 return -1;
131
132 /* if the part is a device dax, clear its bad blocks */
133 if (type == TYPE_DEVDAX && fix_bad_blocks &&
134 badblocks_clear_all(part->path)) {
135 ERR("clearing bad blocks in device dax failed -- '%s'",
136 part->path);
137 errno = EIO;
138 return -1;
139 }
140
141 if (type == TYPE_NORMAL && util_unlink(part->path)) {
142 ERR("!removing part %u from replica %u failed",
143 partn, repn);
144 return -1;
145 }
146
147 errno = olderrno;
148 LOG(4, "Removed part %s number %u from replica %u", part->path, partn,
149 repn);
150 return 0;
151}
152
153/*
154 * create_replica_health_status -- (internal) create helping structure for
155 * storing replica's health status
156 */
157static struct replica_health_status *
158create_replica_health_status(struct pool_set *set, unsigned repn)
159{
160 LOG(3, "set %p, repn %u", set, repn);
161
162 unsigned nparts = set->replica[repn]->nparts;
163 struct replica_health_status *replica_hs;
164
165 replica_hs = Zalloc(sizeof(struct replica_health_status)
166 + nparts * sizeof(struct part_health_status));
167 if (replica_hs == NULL) {
168 ERR("!Zalloc for replica health status");
169 return NULL;
170 }
171
172 replica_hs->nparts = nparts;
173 replica_hs->nhdrs = set->replica[repn]->nhdrs;
174
175 return replica_hs;
176}
177
178/*
179 * replica_part_remove_recovery_file -- remove bad blocks' recovery file
180 */
181static int
182replica_part_remove_recovery_file(struct part_health_status *phs)
183{
184 LOG(3, "phs %p", phs);
185
186 if (phs->recovery_file_name == NULL || phs->recovery_file_exists == 0)
187 return 0;
188
189 if (os_unlink(phs->recovery_file_name) < 0) {
190 ERR("!removing the bad block recovery file failed -- '%s'",
191 phs->recovery_file_name);
192 return -1;
193 }
194
195 LOG(3, "bad block recovery file removed -- '%s'",
196 phs->recovery_file_name);
197
198 phs->recovery_file_exists = 0;
199
200 return 0;
201}
202
203/*
204 * replica_remove_all_recovery_files -- remove all recovery files
205 */
206int
207replica_remove_all_recovery_files(struct poolset_health_status *set_hs)
208{
209 LOG(3, "set_hs %p", set_hs);
210
211 int ret = 0;
212
213 for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
214 struct replica_health_status *rhs = set_hs->replica[r];
215 for (unsigned p = 0; p < rhs->nparts; ++p)
216 ret |= replica_part_remove_recovery_file(&rhs->part[p]);
217 }
218
219 return ret;
220}
221
222/*
223 * replica_free_poolset_health_status -- free memory allocated for helping
224 * structure
225 */
226void
227replica_free_poolset_health_status(struct poolset_health_status *set_hs)
228{
229 LOG(3, "set_hs %p", set_hs);
230
231 for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
232 struct replica_health_status *rep_hs = set_hs->replica[r];
233
234 for (unsigned p = 0; p < rep_hs->nparts; ++p) {
235 Free(rep_hs->part[p].recovery_file_name);
236 Free(rep_hs->part[p].bbs.bbv);
237 }
238
239 Free(set_hs->replica[r]);
240 }
241
242 Free(set_hs);
243}
244
245/*
246 * replica_create_poolset_health_status -- create helping structure for storing
247 * poolset's health status
248 */
249int
250replica_create_poolset_health_status(struct pool_set *set,
251 struct poolset_health_status **set_hsp)
252{
253 LOG(3, "set %p, set_hsp %p", set, set_hsp);
254 unsigned nreplicas = set->nreplicas;
255 struct poolset_health_status *set_hs;
256 set_hs = Zalloc(sizeof(struct poolset_health_status) +
257 nreplicas * sizeof(struct replica_health_status *));
258 if (set_hs == NULL) {
259 ERR("!Zalloc for poolset health state");
260 return -1;
261 }
262 set_hs->nreplicas = nreplicas;
263 for (unsigned i = 0; i < nreplicas; ++i) {
264 struct replica_health_status *replica_hs =
265 create_replica_health_status(set, i);
266 if (replica_hs == NULL) {
267 replica_free_poolset_health_status(set_hs);
268 return -1;
269 }
270 set_hs->replica[i] = replica_hs;
271 }
272 *set_hsp = set_hs;
273 return 0;
274}
275
276/*
277 * replica_is_part_broken -- check if part is marked as broken in the helping
278 * structure
279 */
280int
281replica_is_part_broken(unsigned repn, unsigned partn,
282 struct poolset_health_status *set_hs)
283{
284 struct replica_health_status *rhs = REP_HEALTH(set_hs, repn);
285 return (rhs->flags & IS_BROKEN) ||
286 (PART_HEALTH(rhs, partn) & IS_BROKEN);
287}
288
289/*
290 * is_replica_broken -- check if any part in the replica is marked as broken
291 */
292int
293replica_is_replica_broken(unsigned repn, struct poolset_health_status *set_hs)
294{
295 LOG(3, "repn %u, set_hs %p", repn, set_hs);
296 struct replica_health_status *r_hs = REP_HEALTH(set_hs, repn);
297 if (r_hs->flags & IS_BROKEN)
298 return 1;
299
300 for (unsigned p = 0; p < r_hs->nparts; ++p) {
301 if (replica_is_part_broken(repn, p, set_hs))
302 return 1;
303 }
304 return 0;
305}
306
307/*
308 * replica_is_replica_consistent -- check if replica is not marked as
309 * inconsistent
310 */
311int
312replica_is_replica_consistent(unsigned repn,
313 struct poolset_health_status *set_hs)
314{
315 return !(REP_HEALTH(set_hs, repn)->flags & IS_INCONSISTENT);
316}
317
318/*
319 * replica_has_bad_blocks -- check if replica has bad blocks
320 */
321int
322replica_has_bad_blocks(unsigned repn, struct poolset_health_status *set_hs)
323{
324 return REP_HEALTH(set_hs, repn)->flags & HAS_BAD_BLOCKS;
325}
326
327/*
328 * replica_part_has_bad_blocks -- check if replica's part has bad blocks
329 */
330int
331replica_part_has_bad_blocks(struct part_health_status *phs)
332{
333 return phs->flags & HAS_BAD_BLOCKS;
334}
335
336/*
337 * replica_part_has_corrupted_header -- (internal) check if replica's part
338 * has bad blocks in the header (corrupted header)
339 */
340int
341replica_part_has_corrupted_header(unsigned repn, unsigned partn,
342 struct poolset_health_status *set_hs)
343{
344 struct replica_health_status *rhs = REP_HEALTH(set_hs, repn);
345 return PART_HEALTH(rhs, partn) & HAS_CORRUPTED_HEADER;
346}
347
348/*
349 * replica_has_corrupted_header -- (internal) check if replica has bad blocks
350 * in the header (corrupted header)
351 */
352static int
353replica_has_corrupted_header(unsigned repn,
354 struct poolset_health_status *set_hs)
355{
356 return REP_HEALTH(set_hs, repn)->flags & HAS_CORRUPTED_HEADER;
357}
358
359/*
360 * replica_is_replica_healthy -- check if replica is unbroken and consistent
361 */
362int
363replica_is_replica_healthy(unsigned repn, struct poolset_health_status *set_hs)
364{
365 LOG(3, "repn %u, set_hs %p", repn, set_hs);
366
367 int ret = !replica_is_replica_broken(repn, set_hs) &&
368 replica_is_replica_consistent(repn, set_hs) &&
369 !replica_has_bad_blocks(repn, set_hs);
370
371 LOG(4, "return %i", ret);
372
373 return ret;
374}
375
376/*
377 * replica_has_healthy_header -- (internal) check if replica has healthy headers
378 */
379static int
380replica_has_healthy_header(unsigned repn, struct poolset_health_status *set_hs)
381{
382 LOG(3, "repn %u, set_hs %p", repn, set_hs);
383
384 int ret = !replica_is_replica_broken(repn, set_hs) &&
385 replica_is_replica_consistent(repn, set_hs) &&
386 !replica_has_corrupted_header(repn, set_hs);
387
388 LOG(4, "return %i", ret);
389
390 return ret;
391}
392
393/*
394 * replica_is_poolset_healthy -- check if all replicas in a poolset are not
395 * marked as broken nor inconsistent in the
396 * helping structure
397 */
398int
399replica_is_poolset_healthy(struct poolset_health_status *set_hs)
400{
401 LOG(3, "set_hs %p", set_hs);
402 for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
403 if (!replica_is_replica_healthy(r, set_hs))
404 return 0;
405 }
406 return 1;
407}
408
409/*
410 * replica_is_poolset_transformed -- check if the flag indicating a call from
411 * pmempool_transform is on
412 */
413int
414replica_is_poolset_transformed(unsigned flags)
415{
416 return flags & IS_TRANSFORMED;
417}
418
419/*
420 * replica_find_unbroken_part_with_header -- find a part number in a given
421 * replica, which is not marked as broken in the helping structure and contains
422 * a pool header
423 */
424unsigned
425replica_find_unbroken_part(unsigned repn, struct poolset_health_status *set_hs)
426{
427 LOG(3, "repn %u, set_hs %p", repn, set_hs);
428 for (unsigned p = 0; p < REP_HEALTH(set_hs, repn)->nhdrs; ++p) {
429 if (!replica_is_part_broken(repn, p, set_hs))
430 return p;
431 }
432 return UNDEF_PART;
433}
434
435/*
436 * replica_find_healthy_replica -- find a replica which is a good source of data
437 */
438unsigned
439replica_find_healthy_replica(struct poolset_health_status *set_hs)
440{
441 LOG(3, "set_hs %p", set_hs);
442
443 for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
444 if (replica_is_replica_healthy(r, set_hs)) {
445 LOG(4, "return %i", r);
446 return r;
447 }
448 }
449
450 LOG(4, "return %i", UNDEF_REPLICA);
451 return UNDEF_REPLICA;
452}
453
454/*
455 * replica_find_replica_healthy_header -- find a replica with a healthy header
456 */
457unsigned
458replica_find_replica_healthy_header(struct poolset_health_status *set_hs)
459{
460 LOG(3, "set_hs %p", set_hs);
461
462 for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
463 if (replica_has_healthy_header(r, set_hs)) {
464 LOG(4, "return %i", r);
465 return r;
466 }
467 }
468
469 LOG(4, "return %i", UNDEF_REPLICA);
470 return UNDEF_REPLICA;
471}
472
473/*
474 * replica_check_store_size -- (internal) store size from pool descriptor for
475 * replica
476 */
477static int
478replica_check_store_size(struct pool_set *set,
479 struct poolset_health_status *set_hs, unsigned repn)
480{
481 LOG(3, "set %p, set_hs %p, repn %u", set, set_hs, repn);
482 struct pool_replica *rep = set->replica[repn];
483 struct pmemobjpool pop;
484
485 if (rep->remote) {
486 memcpy(&pop.hdr, rep->part[0].hdr, sizeof(pop.hdr));
487 void *descr = (void *)((uintptr_t)&pop + POOL_HDR_SIZE);
488 if (Rpmem_read(rep->remote->rpp, descr, POOL_HDR_SIZE,
489 sizeof(pop) - POOL_HDR_SIZE, 0)) {
490 return -1;
491 }
492 } else {
493 /* round up map size to Mmap align size */
494 if (util_map_part(&rep->part[0], NULL,
495 ALIGN_UP(sizeof(pop), rep->part[0].alignment),
496 0, MAP_SHARED, 1)) {
497 return -1;
498 }
499
500 memcpy(&pop, rep->part[0].addr, sizeof(pop));
501
502 util_unmap_part(&rep->part[0]);
503 }
504
505 void *dscp = (void *)((uintptr_t)&pop + sizeof(pop.hdr));
506
507 if (!util_checksum(dscp, OBJ_DSC_P_SIZE, &pop.checksum, 0,
508 0)) {
509 set_hs->replica[repn]->flags |= IS_BROKEN;
510 return 0;
511 }
512
513 set_hs->replica[repn]->pool_size = pop.heap_offset + pop.heap_size;
514
515 return 0;
516}
517
518/*
519 * check_store_all_sizes -- (internal) store sizes from pool descriptor for all
520 * healthy replicas
521 */
522static int
523check_store_all_sizes(struct pool_set *set,
524 struct poolset_health_status *set_hs)
525{
526 LOG(3, "set %p, set_hs %p", set, set_hs);
527 for (unsigned r = 0; r < set->nreplicas; ++r) {
528 if (!replica_has_healthy_header(r, set_hs))
529 continue;
530
531 if (replica_check_store_size(set, set_hs, r))
532 return -1;
533 }
534
535 return 0;
536}
537
538/*
539 * check_and_open_poolset_part_files -- (internal) for each part in a poolset
540 * check if the part files are accessible, and if not, mark it as broken
541 * in a helping structure; then open the part file
542 */
543static int
544check_and_open_poolset_part_files(struct pool_set *set,
545 struct poolset_health_status *set_hs, unsigned flags)
546{
547 LOG(3, "set %p, set_hs %p, flags %u", set, set_hs, flags);
548 for (unsigned r = 0; r < set->nreplicas; ++r) {
549 struct pool_replica *rep = set->replica[r];
550 struct replica_health_status *rep_hs = set_hs->replica[r];
551 if (rep->remote) {
552 if (util_replica_open_remote(set, r, 0)) {
553 LOG(1, "cannot open remote replica no %u", r);
554 return -1;
555 }
556
557 unsigned nlanes = REMOTE_NLANES;
558 int ret = util_poolset_remote_open(rep, r,
559 rep->repsize, 0,
560 rep->part[0].addr,
561 rep->resvsize, &nlanes);
562 if (ret) {
563 rep_hs->flags |= IS_BROKEN;
564 LOG(1, "remote replica #%u marked as BROKEN",
565 r);
566 }
567
568 continue;
569 }
570
571 for (unsigned p = 0; p < rep->nparts; ++p) {
572 const char *path = rep->part[p].path;
573 enum file_type type = util_file_get_type(path);
574
575 if (type < 0 || os_access(path, R_OK|W_OK) != 0) {
576 LOG(1, "part file %s is not accessible", path);
577 errno = 0;
578 rep_hs->part[p].flags |= IS_BROKEN;
579 if (is_dry_run(flags))
580 continue;
581 }
582
583 if (util_part_open(&rep->part[p], 0, 0)) {
584 if (type == TYPE_DEVDAX) {
585 LOG(1,
586 "opening part on Device DAX %s failed",
587 path);
588 return -1;
589 }
590 LOG(1, "opening part %s failed", path);
591 errno = 0;
592 rep_hs->part[p].flags |= IS_BROKEN;
593 }
594 }
595 }
596 return 0;
597}
598
599/*
600 * map_all_unbroken_headers -- (internal) map all headers in a poolset,
601 * skipping those marked as broken in a helping
602 * structure
603 */
604static int
605map_all_unbroken_headers(struct pool_set *set,
606 struct poolset_health_status *set_hs)
607{
608 LOG(3, "set %p, set_hs %p", set, set_hs);
609 for (unsigned r = 0; r < set->nreplicas; ++r) {
610 struct pool_replica *rep = set->replica[r];
611 struct replica_health_status *rep_hs = set_hs->replica[r];
612 if (rep->remote)
613 continue;
614
615 for (unsigned p = 0; p < rep->nhdrs; ++p) {
616 /* skip broken parts */
617 if (replica_is_part_broken(r, p, set_hs))
618 continue;
619
620 LOG(4, "mapping header for part %u, replica %u", p, r);
621 if (util_map_hdr(&rep->part[p], MAP_SHARED, 0) != 0) {
622 LOG(1, "header mapping failed - part #%d", p);
623 rep_hs->part[p].flags |= IS_BROKEN;
624 }
625 }
626 }
627 return 0;
628}
629
630/*
631 * unmap_all_headers -- (internal) unmap all headers in a poolset
632 */
633static int
634unmap_all_headers(struct pool_set *set)
635{
636 LOG(3, "set %p", set);
637 for (unsigned r = 0; r < set->nreplicas; ++r) {
638 struct pool_replica *rep = set->replica[r];
639 util_replica_close(set, r);
640
641 if (rep->remote && rep->remote->rpp) {
642 Rpmem_close(rep->remote->rpp);
643 rep->remote->rpp = NULL;
644 }
645 }
646
647 return 0;
648}
649
650/*
651 * check_checksums_and_signatures -- (internal) check if checksums
652 * and signatures are correct for parts
653 * in a given replica
654 */
655static int
656check_checksums_and_signatures(struct pool_set *set,
657 struct poolset_health_status *set_hs)
658{
659 LOG(3, "set %p, set_hs %p", set, set_hs);
660 for (unsigned r = 0; r < set->nreplicas; ++r) {
661 struct pool_replica *rep = REP(set, r);
662 struct replica_health_status *rep_hs = REP_HEALTH(set_hs, r);
663
664 /*
665 * Checksums and signatures of remote replicas are checked
666 * during opening them on the remote side by the rpmem daemon.
667 * The local version of remote headers does not contain
668 * such data.
669 */
670 if (rep->remote)
671 continue;
672
673 for (unsigned p = 0; p < rep->nhdrs; ++p) {
674
675 /* skip broken parts */
676 if (replica_is_part_broken(r, p, set_hs))
677 continue;
678
679 /* check part's checksum */
680 LOG(4, "checking checksum for part %u, replica %u",
681 p, r);
682
683 struct pool_hdr *hdr = HDR(rep, p);
684
685 if (!util_checksum(hdr, sizeof(*hdr), &hdr->checksum, 0,
686 POOL_HDR_CSUM_END_OFF(hdr))) {
687 ERR("invalid checksum of pool header");
688 rep_hs->part[p].flags |= IS_BROKEN;
689 } else if (util_is_zeroed(hdr, sizeof(*hdr))) {
690 rep_hs->part[p].flags |= IS_BROKEN;
691 }
692
693 enum pool_type type = pool_hdr_get_type(hdr);
694 if (type == POOL_TYPE_UNKNOWN) {
695 ERR("invalid signature");
696 rep_hs->part[p].flags |= IS_BROKEN;
697 }
698 }
699 }
700 return 0;
701}
702
703/*
704 * replica_badblocks_recovery_file_save -- save bad blocks in the bad blocks
705 * recovery file before clearing them
706 */
707static int
708replica_badblocks_recovery_file_save(struct part_health_status *part_hs)
709{
710 LOG(3, "part_health_status %p", part_hs);
711
712 ASSERTeq(part_hs->recovery_file_exists, 1);
713 ASSERTne(part_hs->recovery_file_name, NULL);
714
715 struct badblocks *bbs = &part_hs->bbs;
716 char *path = part_hs->recovery_file_name;
717 int ret = -1;
718
719 int fd = os_open(path, O_WRONLY | O_TRUNC);
720 if (fd < 0) {
721 ERR("!opening bad block recovery file failed -- '%s'", path);
722 return -1;
723 }
724
725 FILE *recovery_file_name = os_fdopen(fd, "w");
726 if (recovery_file_name == NULL) {
727 ERR(
728 "!opening a file stream for bad block recovery file failed -- '%s'",
729 path);
730 os_close(fd);
731 return -1;
732 }
733
734 /* save bad blocks */
735 for (unsigned i = 0; i < bbs->bb_cnt; i++) {
736 ASSERT(bbs->bbv[i].length != 0);
737 fprintf(recovery_file_name, "%zu %zu\n",
738 bbs->bbv[i].offset, bbs->bbv[i].length);
739 }
740
741 if (fflush(recovery_file_name) == EOF) {
742 ERR("!flushing bad block recovery file failed -- '%s'", path);
743 goto exit_error;
744 }
745
746 if (os_fsync(fd) < 0) {
747 ERR("!syncing bad block recovery file failed -- '%s'", path);
748 goto exit_error;
749 }
750
751 /* save the finish flag */
752 fprintf(recovery_file_name, "0 0\n");
753
754 if (fflush(recovery_file_name) == EOF) {
755 ERR("!flushing bad block recovery file failed -- '%s'", path);
756 goto exit_error;
757 }
758
759 if (os_fsync(fd) < 0) {
760 ERR("!syncing bad block recovery file failed -- '%s'", path);
761 goto exit_error;
762 }
763
764 LOG(3, "bad blocks saved in the recovery file -- '%s'", path);
765 ret = 0;
766
767exit_error:
768 os_fclose(recovery_file_name);
769
770 return ret;
771}
772
773/*
774 * replica_part_badblocks_recovery_file_read -- read bad blocks
775 * from the bad block recovery file
776 * for the current part
777 */
778static int
779replica_part_badblocks_recovery_file_read(struct part_health_status *part_hs)
780{
781 LOG(3, "part_health_status %p", part_hs);
782
783 ASSERT(part_hs->recovery_file_exists);
784 ASSERTne(part_hs->recovery_file_name, NULL);
785
786 VEC(bbsvec, struct bad_block) bbv = VEC_INITIALIZER;
787 char *path = part_hs->recovery_file_name;
788 struct bad_block bb;
789 int ret = -1;
790
791 FILE *recovery_file = os_fopen(path, "r");
792 if (!recovery_file) {
793 ERR("!opening the recovery file for reading failed -- '%s'",
794 path);
795 return -1;
796 }
797
798 unsigned long long min_offset = 0; /* minimum possible offset */
799
800 do {
801 if (fscanf(recovery_file, "%zu %zu\n",
802 &bb.offset, &bb.length) < 2) {
803 LOG(1, "incomplete bad block recovery file -- '%s'",
804 path);
805 ret = 1;
806 goto error_exit;
807 }
808
809 if (bb.offset == 0 && bb.length == 0) {
810 /* finish_flag */
811 break;
812 }
813
814 /* check if bad blocks build an increasing sequence */
815 if (bb.offset < min_offset) {
816 ERR(
817 "wrong format of bad block recovery file (bad blocks are not sorted by the offset in ascending order) -- '%s'",
818 path);
819 errno = EINVAL;
820 ret = -1;
821 goto error_exit;
822 }
823
824 /* update the minimum possible offset */
825 min_offset = bb.offset + bb.length;
826
827 bb.nhealthy = NO_HEALTHY_REPLICA; /* unknown healthy replica */
828
829 /* add the new bad block to the vector */
830 if (VEC_PUSH_BACK(&bbv, bb))
831 goto error_exit;
832 } while (1);
833
834 part_hs->bbs.bbv = VEC_ARR(&bbv);
835 part_hs->bbs.bb_cnt = (unsigned)VEC_SIZE(&bbv);
836
837 os_fclose(recovery_file);
838
839 LOG(1, "bad blocks read from the recovery file -- '%s'", path);
840
841 return 0;
842
843error_exit:
844 VEC_DELETE(&bbv);
845 os_fclose(recovery_file);
846 return ret;
847}
848
849/* status returned by the replica_badblocks_recovery_files_check() function */
850enum badblocks_recovery_files_status {
851 RECOVERY_FILES_ERROR = -1,
852 RECOVERY_FILES_DO_NOT_EXIST = 0,
853 RECOVERY_FILES_EXIST_ALL = 1,
854 RECOVERY_FILES_NOT_ALL_EXIST = 2
855};
856
857/*
858 * replica_badblocks_recovery_files_check -- (internal) check if bad blocks
859 * recovery files exist
860 */
861static enum badblocks_recovery_files_status
862replica_badblocks_recovery_files_check(struct pool_set *set,
863 struct poolset_health_status *set_hs)
864{
865 LOG(3, "set %p, set_hs %p", set, set_hs);
866
867 int recovery_file_exists = 0;
868 int recovery_file_does_not_exist = 0;
869
870 for (unsigned r = 0; r < set->nreplicas; ++r) {
871 struct pool_replica *rep = set->replica[r];
872 struct replica_health_status *rep_hs = set_hs->replica[r];
873
874 if (rep->remote) {
875 /*
876 * Bad blocks in remote replicas currently are fixed
877 * during opening by removing and recreating
878 * the whole remote replica.
879 */
880 continue;
881 }
882
883 for (unsigned p = 0; p < rep->nparts; ++p) {
884 const char *path = PART(rep, p)->path;
885 struct part_health_status *part_hs = &rep_hs->part[p];
886
887 int exists = util_file_exists(path);
888 if (exists < 0)
889 return -1;
890
891 if (!exists) {
892 /* part file does not exist - skip it */
893 continue;
894 }
895
896 part_hs->recovery_file_name =
897 badblocks_recovery_file_alloc(set->path,
898 r, p);
899 if (part_hs->recovery_file_name == NULL) {
900 LOG(1,
901 "allocating name of bad block recovery file failed");
902 return RECOVERY_FILES_ERROR;
903 }
904
905 exists = util_file_exists(part_hs->recovery_file_name);
906 if (exists < 0)
907 return -1;
908
909 part_hs->recovery_file_exists = exists;
910
911 if (part_hs->recovery_file_exists) {
912 LOG(3, "bad block recovery file exists: %s",
913 part_hs->recovery_file_name);
914
915 recovery_file_exists = 1;
916
917 } else {
918 LOG(3,
919 "bad block recovery file does not exist: %s",
920 part_hs->recovery_file_name);
921
922 recovery_file_does_not_exist = 1;
923 }
924 }
925 }
926
927 if (recovery_file_exists) {
928 if (recovery_file_does_not_exist) {
929 LOG(4, "return RECOVERY_FILES_NOT_ALL_EXIST");
930 return RECOVERY_FILES_NOT_ALL_EXIST;
931 } else {
932 LOG(4, "return RECOVERY_FILES_EXIST_ALL");
933 return RECOVERY_FILES_EXIST_ALL;
934 }
935 }
936
937 LOG(4, "return RECOVERY_FILES_DO_NOT_EXIST");
938 return RECOVERY_FILES_DO_NOT_EXIST;
939}
940
941/*
942 * replica_badblocks_recovery_files_read -- (internal) read bad blocks from all
943 * bad block recovery files for all parts
944 */
945static int
946replica_badblocks_recovery_files_read(struct pool_set *set,
947 struct poolset_health_status *set_hs)
948{
949 LOG(3, "set %p, set_hs %p", set, set_hs);
950
951 int ret;
952
953 for (unsigned r = 0; r < set->nreplicas; ++r) {
954 struct pool_replica *rep = set->replica[r];
955 struct replica_health_status *rep_hs = set_hs->replica[r];
956
957 /* XXX: not supported yet */
958 if (rep->remote)
959 continue;
960
961 for (unsigned p = 0; p < rep->nparts; ++p) {
962 const char *path = PART(rep, p)->path;
963 struct part_health_status *part_hs = &rep_hs->part[p];
964
965 int exists = util_file_exists(path);
966 if (exists < 0)
967 return -1;
968
969 if (!exists) {
970 /* the part does not exist */
971 continue;
972 }
973
974 LOG(1,
975 "reading bad blocks from the recovery file -- '%s'",
976 part_hs->recovery_file_name);
977
978 ret = replica_part_badblocks_recovery_file_read(
979 part_hs);
980 if (ret < 0) {
981 LOG(1,
982 "reading bad blocks from the recovery file failed -- '%s'",
983 part_hs->recovery_file_name);
984 return -1;
985 }
986
987 if (ret > 0) {
988 LOG(1,
989 "incomplete bad block recovery file detected -- '%s'",
990 part_hs->recovery_file_name);
991 return 1;
992 }
993
994 if (part_hs->bbs.bb_cnt) {
995 LOG(3, "part %u contains %u bad blocks -- '%s'",
996 p, part_hs->bbs.bb_cnt, path);
997 }
998 }
999 }
1000
1001 return 0;
1002}
1003
1004/*
1005 * replica_badblocks_recovery_files_create_empty -- (internal) create one empty
1006 * bad block recovery file
1007 * for each part file
1008 */
1009static int
1010replica_badblocks_recovery_files_create_empty(struct pool_set *set,
1011 struct poolset_health_status *set_hs)
1012{
1013 LOG(3, "set %p, set_hs %p", set, set_hs);
1014
1015 struct part_health_status *part_hs;
1016 const char *path;
1017 int fd;
1018
1019 for (unsigned r = 0; r < set->nreplicas; ++r) {
1020 struct pool_replica *rep = set->replica[r];
1021 struct replica_health_status *rep_hs = set_hs->replica[r];
1022
1023 /* XXX: not supported yet */
1024 if (rep->remote)
1025 continue;
1026
1027 for (unsigned p = 0; p < rep->nparts; ++p) {
1028 part_hs = &rep_hs->part[p];
1029 path = PART(rep, p)->path;
1030
1031 if (!part_hs->recovery_file_name)
1032 continue;
1033
1034 fd = os_open(part_hs->recovery_file_name,
1035 O_RDWR | O_CREAT | O_EXCL,
1036 0600);
1037 if (fd < 0) {
1038 ERR(
1039 "!creating an empty bad block recovery file failed -- '%s' (part file '%s')",
1040 part_hs->recovery_file_name, path);
1041 return -1;
1042 }
1043
1044 os_close(fd);
1045
1046 char *file_name = Strdup(part_hs->recovery_file_name);
1047 if (file_name == NULL) {
1048 ERR("!Strdup");
1049 return -1;
1050 }
1051
1052 char *dir_name = dirname(file_name);
1053
1054 /* fsync the file's directory */
1055 if (os_fsync_dir(dir_name) < 0) {
1056 ERR(
1057 "!syncing the directory of the bad block recovery file failed -- '%s' (part file '%s')",
1058 dir_name, path);
1059 Free(file_name);
1060 return -1;
1061 }
1062
1063 Free(file_name);
1064
1065 part_hs->recovery_file_exists = 1;
1066 }
1067 }
1068
1069 return 0;
1070}
1071
1072/*
1073 * replica_badblocks_recovery_files_save -- (internal) save bad blocks
1074 * in the bad block recovery files
1075 */
1076static int
1077replica_badblocks_recovery_files_save(struct pool_set *set,
1078 struct poolset_health_status *set_hs)
1079{
1080 LOG(3, "set %p, set_hs %p", set, set_hs);
1081
1082 for (unsigned r = 0; r < set->nreplicas; ++r) {
1083 struct pool_replica *rep = set->replica[r];
1084 struct replica_health_status *rep_hs = set_hs->replica[r];
1085
1086 /* XXX: not supported yet */
1087 if (rep->remote)
1088 continue;
1089
1090 for (unsigned p = 0; p < rep->nparts; ++p) {
1091 struct part_health_status *part_hs = &rep_hs->part[p];
1092
1093 if (!part_hs->recovery_file_name)
1094 continue;
1095
1096 int ret = replica_badblocks_recovery_file_save(part_hs);
1097 if (ret < 0) {
1098 LOG(1,
1099 "opening bad block recovery file failed -- '%s'",
1100 part_hs->recovery_file_name);
1101 return -1;
1102 }
1103 }
1104 }
1105
1106 return 0;
1107}
1108
1109/*
1110 * replica_badblocks_get -- (internal) get all bad blocks and save them
1111 * in part_hs->bbs structures.
1112 * Returns 1 if any bad block was found, 0 otherwise.
1113 */
1114static int
1115replica_badblocks_get(struct pool_set *set,
1116 struct poolset_health_status *set_hs)
1117{
1118 LOG(3, "set %p, set_hs %p", set, set_hs);
1119
1120 int bad_blocks_found = 0;
1121
1122 for (unsigned r = 0; r < set->nreplicas; ++r) {
1123 struct pool_replica *rep = set->replica[r];
1124 struct replica_health_status *rep_hs = set_hs->replica[r];
1125
1126 /* XXX: not supported yet */
1127 if (rep->remote)
1128 continue;
1129
1130 for (unsigned p = 0; p < rep->nparts; ++p) {
1131 const char *path = PART(rep, p)->path;
1132 struct part_health_status *part_hs = &rep_hs->part[p];
1133
1134 int exists = util_file_exists(path);
1135 if (exists < 0)
1136 return -1;
1137
1138 if (!exists)
1139 continue;
1140
1141 int ret = badblocks_get(path, &part_hs->bbs);
1142 if (ret < 0) {
1143 ERR(
1144 "!checking the pool part for bad blocks failed -- '%s'",
1145 path);
1146 return -1;
1147 }
1148
1149 if (part_hs->bbs.bb_cnt) {
1150 LOG(3, "part %u contains %u bad blocks -- '%s'",
1151 p, part_hs->bbs.bb_cnt, path);
1152
1153 bad_blocks_found = 1;
1154 }
1155 }
1156 }
1157
1158 return bad_blocks_found;
1159}
1160
1161/*
1162 * check_badblocks_in_header -- (internal) check if bad blocks corrupted
1163 * the header
1164 */
1165static int
1166check_badblocks_in_header(struct badblocks *bbs)
1167{
1168 for (unsigned b = 0; b < bbs->bb_cnt; b++)
1169 if (bbs->bbv[b].offset < POOL_HDR_SIZE)
1170 return 1;
1171
1172 return 0;
1173}
1174
1175/*
1176 * replica_badblocks_clear -- (internal) clear all bad blocks
1177 */
1178static int
1179replica_badblocks_clear(struct pool_set *set,
1180 struct poolset_health_status *set_hs)
1181{
1182 LOG(3, "set %p, set_hs %p", set, set_hs);
1183
1184 int ret;
1185
1186 for (unsigned r = 0; r < set->nreplicas; ++r) {
1187 struct pool_replica *rep = set->replica[r];
1188 struct replica_health_status *rep_hs = set_hs->replica[r];
1189
1190 /* XXX: not supported yet */
1191 if (rep->remote)
1192 continue;
1193
1194 for (unsigned p = 0; p < rep->nparts; ++p) {
1195 const char *path = PART(rep, p)->path;
1196 struct part_health_status *part_hs = &rep_hs->part[p];
1197
1198 int exists = util_file_exists(path);
1199 if (exists < 0)
1200 return -1;
1201
1202 if (!exists) {
1203 /* the part does not exist */
1204 continue;
1205 }
1206
1207 if (part_hs->bbs.bb_cnt == 0) {
1208 /* no bad blocks found */
1209 continue;
1210 }
1211
1212 /* bad blocks were found */
1213 part_hs->flags |= HAS_BAD_BLOCKS;
1214 rep_hs->flags |= HAS_BAD_BLOCKS;
1215
1216 if (check_badblocks_in_header(&part_hs->bbs)) {
1217 part_hs->flags |= HAS_CORRUPTED_HEADER;
1218 if (p == 0)
1219 rep_hs->flags |= HAS_CORRUPTED_HEADER;
1220 }
1221
1222 ret = badblocks_clear(path, &part_hs->bbs);
1223 if (ret < 0) {
1224 LOG(1,
1225 "clearing bad blocks in replica failed -- '%s'",
1226 path);
1227 return -1;
1228 }
1229 }
1230 }
1231
1232 return 0;
1233}
1234
1235/*
1236 * replica_badblocks_check_or_clear -- (internal) check if replica contains
1237 * bad blocks when in dry run
1238 * or clear them otherwise
1239 */
1240static int
1241replica_badblocks_check_or_clear(struct pool_set *set,
1242 struct poolset_health_status *set_hs,
1243 int dry_run, int called_from_sync,
1244 int check_bad_blocks, int fix_bad_blocks)
1245{
1246 LOG(3,
1247 "set %p, set_hs %p, dry_run %i, called_from_sync %i, "
1248 "check_bad_blocks %i, fix_bad_blocks %i",
1249 set, set_hs, dry_run, called_from_sync,
1250 check_bad_blocks, fix_bad_blocks);
1251
1252#define ERR_MSG_BB \
1253 " please read the manual first and use this option\n"\
1254 " ONLY IF you are sure that you know what you are doing"
1255
1256 enum badblocks_recovery_files_status status;
1257 int ret;
1258
1259 /* check all bad block recovery files */
1260 status = replica_badblocks_recovery_files_check(set, set_hs);
1261
1262 /* phase #1 - error handling */
1263 switch (status) {
1264 case RECOVERY_FILES_ERROR:
1265 LOG(1, "checking bad block recovery files failed");
1266 return -1;
1267
1268 case RECOVERY_FILES_EXIST_ALL:
1269 case RECOVERY_FILES_NOT_ALL_EXIST:
1270 if (!called_from_sync) {
1271 ERR(
1272 "error: a bad block recovery file exists, run 'pmempool sync --bad-blocks' to fix bad blocks first");
1273 return -1;
1274 }
1275
1276 if (!fix_bad_blocks) {
1277 ERR(
1278 "error: a bad block recovery file exists, but the '--bad-blocks' option is not set\n"
1279 ERR_MSG_BB);
1280 return -1;
1281 }
1282 break;
1283
1284 default:
1285 break;
1286 };
1287
1288 /*
1289 * The pool is checked for bad blocks only if:
1290 * 1) compat feature POOL_FEAT_CHECK_BAD_BLOCKS is set
1291 * OR:
1292 * 2) the '--bad-blocks' option is set
1293 *
1294 * Bad blocks are cleared and fixed only if:
1295 * - the '--bad-blocks' option is set
1296 */
1297 if (!fix_bad_blocks && !check_bad_blocks) {
1298 LOG(3, "skipping bad blocks checking");
1299 return 0;
1300 }
1301
1302 /* phase #2 - reading recovery files */
1303 switch (status) {
1304 case RECOVERY_FILES_EXIST_ALL:
1305 /* read all bad block recovery files */
1306 ret = replica_badblocks_recovery_files_read(set, set_hs);
1307 if (ret < 0) {
1308 LOG(1, "checking bad block recovery files failed");
1309 return -1;
1310 }
1311
1312 if (ret > 0) {
1313 /* incomplete bad block recovery file was detected */
1314
1315 LOG(1,
1316 "warning: incomplete bad block recovery file detected\n"
1317 " - all recovery files will be removed");
1318
1319 /* changing status to RECOVERY_FILES_NOT_ALL_EXIST */
1320 status = RECOVERY_FILES_NOT_ALL_EXIST;
1321 }
1322 break;
1323
1324 case RECOVERY_FILES_NOT_ALL_EXIST:
1325 LOG(1,
1326 "warning: one of bad block recovery files does not exist\n"
1327 " - all recovery files will be removed");
1328 break;
1329
1330 default:
1331 break;
1332 };
1333
1334 if (status == RECOVERY_FILES_NOT_ALL_EXIST) {
1335 /*
1336 * At least one of bad block recovery files does not exist,
1337 * or an incomplete bad block recovery file was detected,
1338 * so all recovery files have to be removed.
1339 */
1340
1341 if (!dry_run) {
1342 LOG(1, "removing all bad block recovery files...");
1343 ret = replica_remove_all_recovery_files(set_hs);
1344 if (ret < 0) {
1345 LOG(1,
1346 "removing bad block recovery files failed");
1347 return -1;
1348 }
1349 } else {
1350 LOG(1, "all bad block recovery files would be removed");
1351 }
1352
1353 /* changing status to RECOVERY_FILES_DO_NOT_EXIST */
1354 status = RECOVERY_FILES_DO_NOT_EXIST;
1355 }
1356
1357 if (status == RECOVERY_FILES_DO_NOT_EXIST) {
1358 /*
1359 * There are no bad block recovery files,
1360 * so let's check bad blocks.
1361 */
1362
1363 int bad_blocks_found = replica_badblocks_get(set, set_hs);
1364 if (bad_blocks_found < 0) {
1365 if (errno == ENOTSUP) {
1366 LOG(1, BB_NOT_SUPP);
1367 return -1;
1368 }
1369
1370 LOG(1, "checking bad blocks failed");
1371 return -1;
1372 }
1373
1374 if (!bad_blocks_found) {
1375 LOG(4, "no bad blocks found");
1376 return 0;
1377 }
1378
1379 /* bad blocks were found */
1380
1381 if (!called_from_sync) {
1382 ERR(
1383 "error: bad blocks found, run 'pmempool sync --bad-blocks' to fix bad blocks first");
1384 return -1;
1385 }
1386
1387 if (!fix_bad_blocks) {
1388 ERR(
1389 "error: bad blocks found, but the '--bad-blocks' option is not set\n"
1390 ERR_MSG_BB);
1391 return -1;
1392 }
1393
1394 if (dry_run) {
1395 /* dry-run - do nothing */
1396 LOG(1, "warning: bad blocks were found");
1397 return 0;
1398 }
1399
1400 /* create one empty recovery file for each part file */
1401 ret = replica_badblocks_recovery_files_create_empty(set,
1402 set_hs);
1403 if (ret < 0) {
1404 LOG(1,
1405 "creating empty bad block recovery files failed");
1406 return -1;
1407 }
1408
1409 /* save bad blocks in recovery files */
1410 ret = replica_badblocks_recovery_files_save(set, set_hs);
1411 if (ret < 0) {
1412 LOG(1, "saving bad block recovery files failed");
1413 return -1;
1414 }
1415 }
1416
1417 if (dry_run) {
1418 /* dry-run - do nothing */
1419 LOG(1, "bad blocks would be cleared");
1420 return 0;
1421 }
1422
1423 ret = replica_badblocks_clear(set, set_hs);
1424 if (ret < 0) {
1425 ERR("clearing bad blocks failed");
1426 return -1;
1427 }
1428
1429 return 0;
1430}
1431
1432/*
1433 * check_shutdown_state -- (internal) check if replica has
1434 * healthy shutdown_state
1435 */
1436static int
1437check_shutdown_state(struct pool_set *set,
1438 struct poolset_health_status *set_hs)
1439{
1440 LOG(3, "set %p, set_hs %p", set, set_hs);
1441 for (unsigned r = 0; r < set->nreplicas; ++r) {\
1442 struct pool_replica *rep = set->replica[r];
1443 struct replica_health_status *rep_hs = set_hs->replica[r];
1444 struct pool_hdr *hdrp = HDR(rep, 0);
1445
1446 if (rep->remote)
1447 continue;
1448
1449 if (hdrp == NULL) {
1450 /* cannot verify shutdown state */
1451 rep_hs->flags |= IS_BROKEN;
1452 continue;
1453 }
1454
1455 struct shutdown_state curr_sds;
1456 shutdown_state_init(&curr_sds, NULL);
1457 for (unsigned p = 0; p < rep->nparts; ++p) {
1458 if (PART(rep, p)->fd < 0)
1459 continue;
1460
1461 if (shutdown_state_add_part(&curr_sds,
1462 PART(rep, p)->fd, NULL)) {
1463 rep_hs->flags |= IS_BROKEN;
1464 break;
1465 }
1466 }
1467
1468 if (rep_hs->flags & IS_BROKEN)
1469 continue;
1470
1471 /* make a copy of sds as we shouldn't modify a pool */
1472 struct shutdown_state pool_sds = hdrp->sds;
1473
1474 if (shutdown_state_check(&curr_sds, &pool_sds, NULL))
1475 rep_hs->flags |= IS_BROKEN;
1476
1477 }
1478 return 0;
1479}
1480
1481/*
1482 * check_uuids_between_parts -- (internal) check if uuids between adjacent
1483 * parts are consistent for a given replica
1484 */
1485static int
1486check_uuids_between_parts(struct pool_set *set, unsigned repn,
1487 struct poolset_health_status *set_hs)
1488{
1489 LOG(3, "set %p, repn %u, set_hs %p", set, repn, set_hs);
1490 struct pool_replica *rep = REP(set, repn);
1491
1492 /* check poolset_uuid consistency between replica's parts */
1493 LOG(4, "checking consistency of poolset uuid in replica %u", repn);
1494 uuid_t poolset_uuid;
1495 int uuid_stored = 0;
1496 unsigned part_stored = UNDEF_PART;
1497 for (unsigned p = 0; p < rep->nhdrs; ++p) {
1498 /* skip broken parts */
1499 if (replica_is_part_broken(repn, p, set_hs))
1500 continue;
1501
1502 if (!uuid_stored) {
1503 memcpy(poolset_uuid, HDR(rep, p)->poolset_uuid,
1504 POOL_HDR_UUID_LEN);
1505 uuid_stored = 1;
1506 part_stored = p;
1507 continue;
1508 }
1509
1510 if (uuidcmp(HDR(rep, p)->poolset_uuid, poolset_uuid)) {
1511 ERR(
1512 "different poolset uuids in parts from the same replica (repn %u, parts %u and %u) - cannot synchronize",
1513 repn, part_stored, p);
1514 errno = EINVAL;
1515 return -1;
1516 }
1517 }
1518
1519 /* check if all uuids for adjacent replicas are the same across parts */
1520 LOG(4, "checking consistency of adjacent replicas' uuids in replica %u",
1521 repn);
1522 unsigned unbroken_p = UNDEF_PART;
1523 for (unsigned p = 0; p < rep->nhdrs; ++p) {
1524 /* skip broken parts */
1525 if (replica_is_part_broken(repn, p, set_hs))
1526 continue;
1527
1528 if (unbroken_p == UNDEF_PART) {
1529 unbroken_p = p;
1530 continue;
1531 }
1532
1533 struct pool_hdr *hdrp = HDR(rep, p);
1534 int prev_differ = uuidcmp(HDR(rep, unbroken_p)->prev_repl_uuid,
1535 hdrp->prev_repl_uuid);
1536 int next_differ = uuidcmp(HDR(rep, unbroken_p)->next_repl_uuid,
1537 hdrp->next_repl_uuid);
1538
1539 if (prev_differ || next_differ) {
1540 ERR(
1541 "different adjacent replica UUID between parts (repn %u, parts %u and %u) - cannot synchronize",
1542 repn, unbroken_p, p);
1543 errno = EINVAL;
1544 return -1;
1545 }
1546 }
1547
1548 /* check parts linkage */
1549 LOG(4, "checking parts linkage in replica %u", repn);
1550 for (unsigned p = 0; p < rep->nhdrs; ++p) {
1551 /* skip broken parts */
1552 if (replica_is_part_broken(repn, p, set_hs))
1553 continue;
1554
1555 struct pool_hdr *hdrp = HDR(rep, p);
1556 struct pool_hdr *next_hdrp = HDRN(rep, p);
1557 int next_is_broken = replica_is_part_broken(repn, p + 1,
1558 set_hs);
1559
1560 if (!next_is_broken) {
1561 int next_decoupled =
1562 uuidcmp(next_hdrp->prev_part_uuid,
1563 hdrp->uuid) ||
1564 uuidcmp(hdrp->next_part_uuid, next_hdrp->uuid);
1565 if (next_decoupled) {
1566 ERR(
1567 "two consecutive unbroken parts are not linked to each other (repn %u, parts %u and %u) - cannot synchronize",
1568 repn, p, p + 1);
1569 errno = EINVAL;
1570 return -1;
1571 }
1572 }
1573 }
1574 return 0;
1575}
1576
1577/*
1578 * check_replicas_consistency -- (internal) check if all uuids within each
1579 * replica are consistent
1580 */
1581static int
1582check_replicas_consistency(struct pool_set *set,
1583 struct poolset_health_status *set_hs)
1584{
1585 LOG(3, "set %p, set_hs %p", set, set_hs);
1586 for (unsigned r = 0; r < set->nreplicas; ++r) {
1587 if (check_uuids_between_parts(set, r, set_hs))
1588 return -1;
1589 }
1590 return 0;
1591}
1592
1593/*
1594 * check_replica_options -- (internal) check if options are consistent in the
1595 * replica
1596 */
1597static int
1598check_replica_options(struct pool_set *set, unsigned repn,
1599 struct poolset_health_status *set_hs)
1600{
1601 LOG(3, "set %p, repn %u, set_hs %p", set, repn, set_hs);
1602 struct pool_replica *rep = REP(set, repn);
1603 struct replica_health_status *rep_hs = REP_HEALTH(set_hs, repn);
1604 for (unsigned p = 0; p < rep->nhdrs; ++p) {
1605 /* skip broken parts */
1606 if (replica_is_part_broken(repn, p, set_hs))
1607 continue;
1608
1609 struct pool_hdr *hdr = HDR(rep, p);
1610 if (((hdr->features.incompat & POOL_FEAT_SINGLEHDR) == 0) !=
1611 ((set->options & OPTION_SINGLEHDR) == 0)) {
1612 LOG(1,
1613 "improper options are set in part %u's header in replica %u",
1614 p, repn);
1615 rep_hs->part[p].flags |= IS_BROKEN;
1616 }
1617 }
1618 return 0;
1619}
1620
1621/*
1622 * check_options -- (internal) check if options are consistent in all replicas
1623 */
1624static int
1625check_options(struct pool_set *set, struct poolset_health_status *set_hs)
1626{
1627 LOG(3, "set %p, set_hs %p", set, set_hs);
1628 for (unsigned r = 0; r < set->nreplicas; ++r) {
1629 if (check_replica_options(set, r, set_hs))
1630 return -1;
1631 }
1632 return 0;
1633}
1634
1635/*
1636 * check_replica_poolset_uuids - (internal) check if poolset_uuid fields are
1637 * consistent among all parts of a replica;
1638 * the replica is initially considered as
1639 * consistent
1640 */
1641static int
1642check_replica_poolset_uuids(struct pool_set *set, unsigned repn,
1643 uuid_t poolset_uuid, struct poolset_health_status *set_hs)
1644{
1645 LOG(3, "set %p, repn %u, poolset_uuid %p, set_hs %p", set, repn,
1646 poolset_uuid, set_hs);
1647 struct pool_replica *rep = REP(set, repn);
1648 for (unsigned p = 0; p < rep->nhdrs; ++p) {
1649 /* skip broken parts */
1650 if (replica_is_part_broken(repn, p, set_hs))
1651 continue;
1652
1653 if (uuidcmp(HDR(rep, p)->poolset_uuid, poolset_uuid)) {
1654 /*
1655 * two internally consistent replicas have
1656 * different poolset_uuid
1657 */
1658 return -1;
1659 } else {
1660 /*
1661 * it is sufficient to check only one part
1662 * from internally consistent replica
1663 */
1664 break;
1665 }
1666 }
1667 return 0;
1668}
1669
1670/*
1671 * check_poolset_uuids -- (internal) check if poolset_uuid fields are consistent
1672 * among all internally consistent replicas
1673 */
1674static int
1675check_poolset_uuids(struct pool_set *set,
1676 struct poolset_health_status *set_hs)
1677{
1678 LOG(3, "set %p, set_hs %p", set, set_hs);
1679
1680 /* find a replica with healthy header */
1681 unsigned r_h = replica_find_replica_healthy_header(set_hs);
1682 if (r_h == UNDEF_REPLICA) {
1683 ERR("no healthy replica found");
1684 return -1;
1685 }
1686
1687 uuid_t poolset_uuid;
1688 memcpy(poolset_uuid, HDR(REP(set, r_h), 0)->poolset_uuid,
1689 POOL_HDR_UUID_LEN);
1690
1691 for (unsigned r = 0; r < set->nreplicas; ++r) {
1692 /* skip inconsistent replicas */
1693 if (!replica_is_replica_consistent(r, set_hs) || r == r_h)
1694 continue;
1695
1696 if (check_replica_poolset_uuids(set, r, poolset_uuid, set_hs)) {
1697 ERR(
1698 "inconsistent poolset uuids between replicas %u and %u - cannot synchronize",
1699 r_h, r);
1700 return -1;
1701 }
1702 }
1703 return 0;
1704}
1705
1706/*
1707 * get_replica_uuid -- (internal) get replica uuid
1708 */
1709static int
1710get_replica_uuid(struct pool_replica *rep, unsigned repn,
1711 struct poolset_health_status *set_hs, uuid_t **uuidpp)
1712{
1713 unsigned nhdrs = rep->nhdrs;
1714 if (!replica_is_part_broken(repn, 0, set_hs)) {
1715 /* the first part is not broken */
1716 *uuidpp = &HDR(rep, 0)->uuid;
1717 return 0;
1718 } else if (nhdrs > 1 && !replica_is_part_broken(repn, 1, set_hs)) {
1719 /* the second part is not broken */
1720 *uuidpp = &HDR(rep, 1)->prev_part_uuid;
1721 return 0;
1722 } else if (nhdrs > 1 &&
1723 !replica_is_part_broken(repn, nhdrs - 1, set_hs)) {
1724 /* the last part is not broken */
1725 *uuidpp = &HDR(rep, nhdrs - 1)->next_part_uuid;
1726 return 0;
1727 } else {
1728 /* cannot get replica uuid */
1729 return -1;
1730 }
1731}
1732
1733/*
1734 * check_uuids_between_replicas -- (internal) check if uuids between internally
1735 * consistent adjacent replicas are consistent
1736 */
1737static int
1738check_uuids_between_replicas(struct pool_set *set,
1739 struct poolset_health_status *set_hs)
1740{
1741 LOG(3, "set %p, set_hs %p", set, set_hs);
1742 for (unsigned r = 0; r < set->nreplicas; ++r) {
1743 /* skip comparing inconsistent pairs of replicas */
1744 if (!replica_is_replica_consistent(r, set_hs) ||
1745 !replica_is_replica_consistent(r + 1, set_hs))
1746 continue;
1747
1748 struct pool_replica *rep = REP(set, r);
1749 struct pool_replica *rep_n = REPN(set, r);
1750
1751 /* get uuids of the two adjacent replicas */
1752 uuid_t *rep_uuidp = NULL;
1753 uuid_t *rep_n_uuidp = NULL;
1754 unsigned r_n = REPN_HEALTHidx(set_hs, r);
1755 if (get_replica_uuid(rep, r, set_hs, &rep_uuidp))
1756 LOG(2, "cannot get replica uuid, replica %u", r);
1757 if (get_replica_uuid(rep_n, r_n, set_hs, &rep_n_uuidp))
1758 LOG(2, "cannot get replica uuid, replica %u", r_n);
1759
1760 /*
1761 * check if replica uuids are consistent between two adjacent
1762 * replicas
1763 */
1764 unsigned p = replica_find_unbroken_part(r, set_hs);
1765 unsigned p_n = replica_find_unbroken_part(r_n, set_hs);
1766 if (p_n != UNDEF_PART && rep_uuidp != NULL &&
1767 uuidcmp(*rep_uuidp,
1768 HDR(rep_n, p_n)->prev_repl_uuid)) {
1769 ERR(
1770 "inconsistent replica uuids between replicas %u and %u",
1771 r, r_n);
1772 return -1;
1773 }
1774 if (p != UNDEF_PART && rep_n_uuidp != NULL &&
1775 uuidcmp(*rep_n_uuidp,
1776 HDR(rep, p)->next_repl_uuid)) {
1777 ERR(
1778 "inconsistent replica uuids between replicas %u and %u",
1779 r, r_n);
1780 return -1;
1781 }
1782
1783 /*
1784 * check if replica uuids on borders of a broken replica are
1785 * consistent
1786 */
1787 unsigned r_nn = REPN_HEALTHidx(set_hs, r_n);
1788 if (set->nreplicas > 1 && p != UNDEF_PART &&
1789 replica_is_replica_broken(r_n, set_hs) &&
1790 replica_is_replica_consistent(r_nn, set_hs)) {
1791 unsigned p_nn =
1792 replica_find_unbroken_part(r_nn, set_hs);
1793 if (p_nn == UNDEF_PART) {
1794 LOG(2,
1795 "cannot compare uuids on borders of replica %u",
1796 r);
1797 continue;
1798 }
1799 struct pool_replica *rep_nn = REP(set, r_nn);
1800 if (uuidcmp(HDR(rep, p)->next_repl_uuid,
1801 HDR(rep_nn, p_nn)->prev_repl_uuid)) {
1802 ERR(
1803 "inconsistent replica uuids on borders of replica %u",
1804 r);
1805 return -1;
1806 }
1807 }
1808 }
1809 return 0;
1810}
1811
1812/*
1813 * check_replica_cycles -- (internal) check if healthy replicas form cycles
1814 * shorter than the number of all replicas
1815 */
1816static int
1817check_replica_cycles(struct pool_set *set,
1818 struct poolset_health_status *set_hs)
1819{
1820 LOG(3, "set %p, set_hs %p", set, set_hs);
1821 unsigned first_healthy;
1822 unsigned count_healthy = 0;
1823 for (unsigned r = 0; r < set->nreplicas; ++r) {
1824 if (!replica_is_replica_healthy(r, set_hs)) {
1825 count_healthy = 0;
1826 continue;
1827 }
1828
1829 if (count_healthy == 0)
1830 first_healthy = r;
1831
1832 ++count_healthy;
1833 struct pool_hdr *hdrh =
1834 PART(REP(set, first_healthy), 0)->hdr;
1835 struct pool_hdr *hdr = PART(REP(set, r), 0)->hdr;
1836 if (uuidcmp(hdrh->uuid, hdr->next_repl_uuid) == 0 &&
1837 count_healthy < set->nreplicas) {
1838 /*
1839 * Healthy replicas form a cycle shorter than
1840 * the number of all replicas; for the user it
1841 * means that:
1842 */
1843 ERR(
1844 "alien replica found (probably coming from a different poolset)");
1845 return -1;
1846 }
1847 }
1848 return 0;
1849}
1850
1851/*
1852 * check_replica_sizes -- (internal) check if all replicas are large
1853 * enough to hold data from a healthy replica
1854 */
1855static int
1856check_replica_sizes(struct pool_set *set, struct poolset_health_status *set_hs)
1857{
1858 LOG(3, "set %p, set_hs %p", set, set_hs);
1859 ssize_t pool_size = -1;
1860 for (unsigned r = 0; r < set->nreplicas; ++r) {
1861 /* skip broken replicas */
1862 if (!replica_is_replica_healthy(r, set_hs))
1863 continue;
1864
1865 /* get the size of a pool in the replica */
1866 ssize_t replica_pool_size;
1867 if (REP(set, r)->remote)
1868 /* XXX: no way to get the size of a remote pool yet */
1869 replica_pool_size = (ssize_t)set->poolsize;
1870 else
1871 replica_pool_size = replica_get_pool_size(set, r);
1872
1873 if (replica_pool_size < 0) {
1874 LOG(1, "getting pool size from replica %u failed", r);
1875 set_hs->replica[r]->flags |= IS_BROKEN;
1876 continue;
1877 }
1878
1879 /* check if the pool is bigger than minimum size */
1880 enum pool_type type = pool_hdr_get_type(HDR(REP(set, r), 0));
1881 if ((size_t)replica_pool_size < pool_get_min_size(type)) {
1882 LOG(1,
1883 "pool size from replica %u is smaller than the minimum size allowed for the pool",
1884 r);
1885 set_hs->replica[r]->flags |= IS_BROKEN;
1886 continue;
1887 }
1888
1889 /* check if each replica is big enough to hold the pool data */
1890 if (set->poolsize < (size_t)replica_pool_size) {
1891 ERR(
1892 "some replicas are too small to hold synchronized data");
1893 return -1;
1894 }
1895
1896 if (pool_size < 0) {
1897 pool_size = replica_pool_size;
1898 continue;
1899 }
1900
1901 /* check if pools in all healthy replicas are of equal size */
1902 if (pool_size != replica_pool_size) {
1903 ERR("pool sizes from different replicas differ");
1904 return -1;
1905 }
1906 }
1907 return 0;
1908}
1909
1910/*
1911 * replica_read_features -- (internal) read features from the header
1912 */
1913static int
1914replica_read_features(struct pool_set *set,
1915 struct poolset_health_status *set_hs,
1916 features_t *features)
1917{
1918 LOG(3, "set %p set_hs %p features %p", set, set_hs, features);
1919
1920 ASSERTne(features, NULL);
1921
1922 for (unsigned r = 0; r < set->nreplicas; r++) {
1923 struct pool_replica *rep = set->replica[r];
1924 struct replica_health_status *rep_hs = set_hs->replica[r];
1925
1926 if (rep->remote) {
1927 if (rep_hs->flags & IS_BROKEN)
1928 continue;
1929
1930 struct pool_hdr *hdrp = rep->part[0].hdr;
1931 memcpy(features, &hdrp->features, sizeof(*features));
1932
1933 return 0;
1934 }
1935
1936 for (unsigned p = 0; p < rep->nparts; p++) {
1937 struct pool_set_part *part = &rep->part[p];
1938
1939 if (part->fd == -1)
1940 continue;
1941
1942 if (util_map_hdr(part, MAP_SHARED, 0) != 0) {
1943 LOG(1, "header mapping failed");
1944 return -1;
1945 }
1946
1947 struct pool_hdr *hdrp = part->hdr;
1948 memcpy(features, &hdrp->features, sizeof(*features));
1949
1950 util_unmap_hdr(part);
1951
1952 return 0;
1953 }
1954 }
1955
1956 /* no healthy replica/part found */
1957 return -1;
1958}
1959
1960/*
1961 * replica_check_poolset_health -- check if a given poolset can be considered as
1962 * healthy, and store the status in a helping structure
1963 */
1964int
1965replica_check_poolset_health(struct pool_set *set,
1966 struct poolset_health_status **set_hsp,
1967 int called_from_sync, unsigned flags)
1968{
1969 LOG(3, "set %p, set_hsp %p, called_from_sync %i, flags %u",
1970 set, set_hsp, called_from_sync, flags);
1971
1972 if (replica_create_poolset_health_status(set, set_hsp)) {
1973 LOG(1, "creating poolset health status failed");
1974 return -1;
1975 }
1976
1977 struct poolset_health_status *set_hs = *set_hsp;
1978
1979 /* check if part files exist and are accessible */
1980 if (check_and_open_poolset_part_files(set, set_hs, flags)) {
1981 LOG(1, "poolset part files check failed");
1982 goto err;
1983 }
1984
1985 features_t features;
1986 int check_bad_blks;
1987 int fix_bad_blks = called_from_sync && fix_bad_blocks(flags);
1988
1989 if (fix_bad_blks) {
1990 /*
1991 * We will fix bad blocks, so we cannot read features here,
1992 * because reading could fail, because of bad blocks.
1993 * We will read features after having bad blocks fixed.
1994 *
1995 * Fixing bad blocks implies checking bad blocks.
1996 */
1997 check_bad_blks = 1;
1998 } else {
1999 /*
2000 * We will not fix bad blocks, so we have to read features here.
2001 */
2002 if (replica_read_features(set, set_hs, &features)) {
2003 LOG(1, "reading features failed");
2004 goto err;
2005 }
2006 check_bad_blks = features.compat & POOL_FEAT_CHECK_BAD_BLOCKS;
2007 }
2008
2009 /* check for bad blocks when in dry run or clear them otherwise */
2010 if (replica_badblocks_check_or_clear(set, set_hs, is_dry_run(flags),
2011 called_from_sync, check_bad_blks, fix_bad_blks)) {
2012 LOG(1, "replica bad_blocks check failed");
2013 goto err;
2014 }
2015
2016 /* read features after fixing bad blocks */
2017 if (fix_bad_blks && replica_read_features(set, set_hs, &features)) {
2018 LOG(1, "reading features failed");
2019 goto err;
2020 }
2021
2022 /* set ignore_sds flag basing on features read from the header */
2023 set->ignore_sds = !(features.incompat & POOL_FEAT_SDS);
2024
2025 /* map all headers */
2026 map_all_unbroken_headers(set, set_hs);
2027
2028 /*
2029 * Check if checksums and signatures are correct for all parts
2030 * in all replicas.
2031 */
2032 check_checksums_and_signatures(set, set_hs);
2033
2034 /* check if option flags are consistent */
2035 if (check_options(set, set_hs)) {
2036 LOG(1, "flags check failed");
2037 goto err;
2038 }
2039
2040 if (!set->ignore_sds && check_shutdown_state(set, set_hs)) {
2041 LOG(1, "replica shutdown_state check failed");
2042 goto err;
2043 }
2044
2045 /* check if uuids in parts across each replica are consistent */
2046 if (check_replicas_consistency(set, set_hs)) {
2047 LOG(1, "replica consistency check failed");
2048 goto err;
2049 }
2050
2051 /* check poolset_uuid values between replicas */
2052 if (check_poolset_uuids(set, set_hs)) {
2053 LOG(1, "poolset uuids check failed");
2054 goto err;
2055 }
2056
2057 /* check if uuids for adjacent replicas are consistent */
2058 if (check_uuids_between_replicas(set, set_hs)) {
2059 LOG(1, "replica uuids check failed");
2060 goto err;
2061 }
2062
2063 /* check if healthy replicas make up another poolset */
2064 if (check_replica_cycles(set, set_hs)) {
2065 LOG(1, "replica cycles check failed");
2066 goto err;
2067 }
2068
2069 /* check if replicas are large enough */
2070 if (check_replica_sizes(set, set_hs)) {
2071 LOG(1, "replica sizes check failed");
2072 goto err;
2073 }
2074
2075 if (check_store_all_sizes(set, set_hs)) {
2076 LOG(1, "reading pool sizes failed");
2077 goto err;
2078 }
2079
2080 unmap_all_headers(set);
2081 util_poolset_fdclose_always(set);
2082 return 0;
2083
2084err:
2085 errno = EINVAL;
2086 unmap_all_headers(set);
2087 util_poolset_fdclose_always(set);
2088 replica_free_poolset_health_status(set_hs);
2089 return -1;
2090}
2091
2092/*
2093 * replica_get_pool_size -- find the effective size (mapped) of a pool based
2094 * on metadata from given replica
2095 */
2096ssize_t
2097replica_get_pool_size(struct pool_set *set, unsigned repn)
2098{
2099 LOG(3, "set %p, repn %u", set, repn);
2100 struct pool_set_part *part = PART(REP(set, repn), 0);
2101 int should_close_part = 0;
2102 int should_unmap_part = 0;
2103 if (part->fd == -1) {
2104 if (util_part_open(part, 0, 0))
2105 return -1;
2106
2107 should_close_part = 1;
2108 }
2109
2110 if (part->addr == NULL) {
2111 if (util_map_part(part, NULL,
2112 ALIGN_UP(sizeof(PMEMobjpool), part->alignment), 0,
2113 MAP_SHARED, 1)) {
2114 util_part_fdclose(part);
2115 return -1;
2116 }
2117 should_unmap_part = 1;
2118 }
2119
2120 PMEMobjpool *pop = (PMEMobjpool *)part->addr;
2121 ssize_t ret = (ssize_t)(pop->heap_offset + pop->heap_size);
2122
2123 if (should_unmap_part)
2124 util_unmap_part(part);
2125 if (should_close_part)
2126 util_part_fdclose(part);
2127
2128 return ret;
2129}
2130
2131/*
2132 * replica_check_part_sizes -- check if all parts are large enough
2133 */
2134int
2135replica_check_part_sizes(struct pool_set *set, size_t min_size)
2136{
2137 LOG(3, "set %p, min_size %zu", set, min_size);
2138 for (unsigned r = 0; r < set->nreplicas; ++r) {
2139 struct pool_replica *rep = set->replica[r];
2140 if (rep->remote != NULL)
2141 /* skip remote replicas */
2142 continue;
2143
2144 for (unsigned p = 0; p < rep->nparts; ++p) {
2145 if (PART(rep, p)->filesize < min_size) {
2146 ERR("replica %u, part %u: file is too small",
2147 r, p);
2148 errno = EINVAL;
2149 return -1;
2150 }
2151 }
2152 }
2153 return 0;
2154}
2155
2156/*
2157 * replica_check_local_part_dir -- check if directory for the part file
2158 * exists
2159 */
2160int
2161replica_check_local_part_dir(struct pool_set *set, unsigned repn,
2162 unsigned partn)
2163{
2164 LOG(3, "set %p, repn %u, partn %u", set, repn, partn);
2165 char *path = Strdup(PART(REP(set, repn), partn)->path);
2166 const char *dir = dirname(path);
2167 os_stat_t sb;
2168 if (os_stat(dir, &sb) != 0 || !(sb.st_mode & S_IFDIR)) {
2169 ERR(
2170 "directory %s for part %u in replica %u does not exist or is not accessible",
2171 path, partn, repn);
2172 Free(path);
2173 return -1;
2174 }
2175 Free(path);
2176 return 0;
2177}
2178
2179/*
2180 * replica_check_part_dirs -- (internal) check if directories for part files
2181 * exist
2182 */
2183int
2184replica_check_part_dirs(struct pool_set *set)
2185{
2186 LOG(3, "set %p", set);
2187 for (unsigned r = 0; r < set->nreplicas; ++r) {
2188 struct pool_replica *rep = set->replica[r];
2189 if (rep->remote != NULL)
2190 /* skip remote replicas */
2191 continue;
2192
2193 for (unsigned p = 0; p < rep->nparts; ++p) {
2194 if (replica_check_local_part_dir(set, r, p))
2195 return -1;
2196 }
2197 }
2198 return 0;
2199}
2200
2201/*
2202 * replica_open_replica_part_files -- open all part files for a replica
2203 */
2204int
2205replica_open_replica_part_files(struct pool_set *set, unsigned repn)
2206{
2207 LOG(3, "set %p, repn %u", set, repn);
2208 struct pool_replica *rep = set->replica[repn];
2209 for (unsigned p = 0; p < rep->nparts; ++p) {
2210 /* skip already opened files */
2211 if (rep->part[p].fd != -1)
2212 continue;
2213
2214 if (util_part_open(&rep->part[p], 0, 0)) {
2215 LOG(1, "part files open failed for replica %u, part %u",
2216 repn, p);
2217 errno = EINVAL;
2218 goto err;
2219 }
2220 }
2221 return 0;
2222
2223err:
2224 util_replica_fdclose(set->replica[repn]);
2225 return -1;
2226}
2227
2228/*
2229 * replica_open_poolset_part_files -- open all part files for a poolset
2230 */
2231int
2232replica_open_poolset_part_files(struct pool_set *set)
2233{
2234 LOG(3, "set %p", set);
2235 for (unsigned r = 0; r < set->nreplicas; ++r) {
2236 if (set->replica[r]->remote)
2237 continue;
2238 if (replica_open_replica_part_files(set, r)) {
2239 LOG(1, "opening replica %u, part files failed", r);
2240 goto err;
2241 }
2242 }
2243
2244 return 0;
2245
2246err:
2247 util_poolset_fdclose_always(set);
2248 return -1;
2249}
2250
2251/*
2252 * pmempool_syncU -- synchronize replicas within a poolset
2253 */
2254#ifndef _WIN32
2255static inline
2256#endif
2257int
2258pmempool_syncU(const char *poolset, unsigned flags)
2259{
2260 LOG(3, "poolset %s, flags %u", poolset, flags);
2261 ASSERTne(poolset, NULL);
2262
2263 /* check if poolset has correct signature */
2264 if (util_is_poolset_file(poolset) != 1) {
2265 ERR("file is not a poolset file");
2266 goto err;
2267 }
2268
2269 /* check if flags are supported */
2270 if (check_flags_sync(flags)) {
2271 ERR("unsupported flags");
2272 errno = EINVAL;
2273 goto err;
2274 }
2275
2276 /* open poolset file */
2277 int fd = util_file_open(poolset, NULL, 0, O_RDONLY);
2278 if (fd < 0) {
2279 ERR("cannot open a poolset file");
2280 goto err;
2281 }
2282
2283 /* fill up pool_set structure */
2284 struct pool_set *set = NULL;
2285 if (util_poolset_parse(&set, poolset, fd)) {
2286 ERR("parsing input poolset failed");
2287 goto err_close_file;
2288 }
2289
2290 if (set->nreplicas == 1) {
2291 ERR("no replica(s) found in the pool set");
2292 errno = EINVAL;
2293 goto err_close_file;
2294 }
2295
2296 if (set->remote && util_remote_load()) {
2297 ERR("remote replication not available");
2298 errno = ENOTSUP;
2299 goto err_close_file;
2300 }
2301
2302 /* sync all replicas */
2303 if (replica_sync(set, NULL, flags)) {
2304 LOG(1, "synchronization failed");
2305 goto err_close_all;
2306 }
2307
2308 util_poolset_close(set, DO_NOT_DELETE_PARTS);
2309 os_close(fd);
2310 return 0;
2311
2312err_close_all:
2313 util_poolset_close(set, DO_NOT_DELETE_PARTS);
2314
2315err_close_file:
2316 os_close(fd);
2317
2318err:
2319 if (errno == 0)
2320 errno = EINVAL;
2321
2322 return -1;
2323}
2324
2325#ifndef _WIN32
2326/*
2327 * pmempool_sync -- synchronize replicas within a poolset
2328 */
2329int
2330pmempool_sync(const char *poolset, unsigned flags)
2331{
2332 return pmempool_syncU(poolset, flags);
2333}
2334#else
2335/*
2336 * pmempool_syncW -- synchronize replicas within a poolset in widechar
2337 */
2338int
2339pmempool_syncW(const wchar_t *poolset, unsigned flags)
2340{
2341 char *path = util_toUTF8(poolset);
2342 if (path == NULL) {
2343 ERR("Invalid poolest file path.");
2344 return -1;
2345 }
2346
2347 int ret = pmempool_syncU(path, flags);
2348
2349 util_free_UTF8(path);
2350 return ret;
2351}
2352#endif
2353
2354/*
2355 * pmempool_transformU -- alter poolset structure
2356 */
2357#ifndef _WIN32
2358static inline
2359#endif
2360int
2361pmempool_transformU(const char *poolset_src,
2362 const char *poolset_dst, unsigned flags)
2363{
2364 LOG(3, "poolset_src %s, poolset_dst %s, flags %u", poolset_src,
2365 poolset_dst, flags);
2366 ASSERTne(poolset_src, NULL);
2367 ASSERTne(poolset_dst, NULL);
2368
2369 /* check if the source poolset has correct signature */
2370 if (util_is_poolset_file(poolset_src) != 1) {
2371 ERR("source file is not a poolset file");
2372 goto err;
2373 }
2374
2375 /* check if the destination poolset has correct signature */
2376 if (util_is_poolset_file(poolset_dst) != 1) {
2377 ERR("destination file is not a poolset file");
2378 goto err;
2379 }
2380
2381 /* check if flags are supported */
2382 if (check_flags_transform(flags)) {
2383 ERR("unsupported flags");
2384 errno = EINVAL;
2385 goto err;
2386 }
2387
2388 /* open the source poolset file */
2389 int fd_in = util_file_open(poolset_src, NULL, 0, O_RDONLY);
2390 if (fd_in < 0) {
2391 ERR("cannot open source poolset file");
2392 goto err;
2393 }
2394
2395 /* parse the source poolset file */
2396 struct pool_set *set_in = NULL;
2397 if (util_poolset_parse(&set_in, poolset_src, fd_in)) {
2398 ERR("parsing source poolset failed");
2399 os_close(fd_in);
2400 goto err;
2401 }
2402 os_close(fd_in);
2403
2404 /* open the destination poolset file */
2405 int fd_out = util_file_open(poolset_dst, NULL, 0, O_RDONLY);
2406 if (fd_out < 0) {
2407 ERR("cannot open destination poolset file");
2408 goto err;
2409 }
2410
2411 enum del_parts_mode del = DO_NOT_DELETE_PARTS;
2412
2413 /* parse the destination poolset file */
2414 struct pool_set *set_out = NULL;
2415 if (util_poolset_parse(&set_out, poolset_dst, fd_out)) {
2416 ERR("parsing destination poolset failed");
2417 os_close(fd_out);
2418 goto err_free_poolin;
2419 }
2420 os_close(fd_out);
2421
2422 /* check if the source poolset is of a correct type */
2423 enum pool_type ptype = pool_set_type(set_in);
2424 if (ptype != POOL_TYPE_OBJ) {
2425 errno = EINVAL;
2426 ERR("transform is not supported for given pool type: %s",
2427 pool_get_pool_type_str(ptype));
2428 goto err_free_poolout;
2429 }
2430
2431 /* load remote library if needed */
2432 if (set_in->remote && util_remote_load()) {
2433 ERR("remote replication not available");
2434 goto err_free_poolout;
2435 }
2436 if (set_out->remote && util_remote_load()) {
2437 ERR("remote replication not available");
2438 goto err_free_poolout;
2439 }
2440
2441 del = is_dry_run(flags) ? DO_NOT_DELETE_PARTS : DELETE_CREATED_PARTS;
2442
2443 /* transform poolset */
2444 if (replica_transform(set_in, set_out, flags)) {
2445 LOG(1, "transformation failed");
2446 goto err_free_poolout;
2447 }
2448
2449 util_poolset_close(set_in, DO_NOT_DELETE_PARTS);
2450 util_poolset_close(set_out, DO_NOT_DELETE_PARTS);
2451 return 0;
2452
2453err_free_poolout:
2454 util_poolset_close(set_out, del);
2455
2456err_free_poolin:
2457 util_poolset_close(set_in, DO_NOT_DELETE_PARTS);
2458
2459err:
2460 if (errno == 0)
2461 errno = EINVAL;
2462
2463 return -1;
2464}
2465
2466#ifndef _WIN32
2467/*
2468 * pmempool_transform -- alter poolset structure
2469 */
2470int
2471pmempool_transform(const char *poolset_src,
2472 const char *poolset_dst, unsigned flags)
2473{
2474 return pmempool_transformU(poolset_src, poolset_dst, flags);
2475}
2476#else
2477/*
2478 * pmempool_transformW -- alter poolset structure in widechar
2479 */
2480int
2481pmempool_transformW(const wchar_t *poolset_src,
2482 const wchar_t *poolset_dst, unsigned flags)
2483{
2484 char *path_src = util_toUTF8(poolset_src);
2485 if (path_src == NULL) {
2486 ERR("Invalid source poolest file path.");
2487 return -1;
2488 }
2489
2490 char *path_dst = util_toUTF8(poolset_dst);
2491 if (path_dst == NULL) {
2492 ERR("Invalid destination poolest file path.");
2493 Free(path_src);
2494 return -1;
2495 }
2496
2497 int ret = pmempool_transformU(path_src, path_dst, flags);
2498
2499 util_free_UTF8(path_src);
2500 util_free_UTF8(path_dst);
2501 return ret;
2502}
2503#endif