summaryrefslogtreecommitdiffstats
path: root/src/pmdk/src/libpmempool/replica.c
blob: cf38e742d6e324f6ff76a346b6ecac4ea1e1a295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */

/*
 * replica.c -- groups all commands for replica manipulation
 */

#include "replica.h"

#include <errno.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>

#include "obj.h"
#include "palloc.h"
#include "file.h"
#include "os.h"
#include "out.h"
#include "pool_hdr.h"
#include "set.h"
#include "util.h"
#include "uuid.h"
#include "shutdown_state.h"
#include "badblocks.h"
#include "set_badblocks.h"

/*
 * check_flags_sync -- (internal) check if flags are supported for sync
 */
static int
check_flags_sync(unsigned flags)
{
	flags &= ~(PMEMPOOL_SYNC_DRY_RUN | PMEMPOOL_SYNC_FIX_BAD_BLOCKS);
	return flags > 0;
}

/*
 * check_flags_transform -- (internal) check if flags are supported for
 *                          transform
 */
static int
check_flags_transform(unsigned flags)
{
	flags &= ~PMEMPOOL_TRANSFORM_DRY_RUN;
	return flags > 0;
}

/*
 * replica_align_badblock_offset_length -- align offset and length
 *                                          of the bad block for the given part
 */
void
replica_align_badblock_offset_length(size_t *offset, size_t *length,
			struct pool_set *set_in, unsigned repn, unsigned partn)
{
	LOG(3, "offset %zu, length %zu, pool_set %p, replica %u, part %u",
		*offset, *length, set_in, repn, partn);

	size_t alignment = set_in->replica[repn]->part[partn].alignment;

	size_t off = ALIGN_DOWN(*offset, alignment);
	size_t len = ALIGN_UP(*length + (*offset - off), alignment);

	*offset = off;
	*length = len;
}

/*
 * replica_get_part_data_len -- get data length for given part
 */
size_t
replica_get_part_data_len(struct pool_set *set_in, unsigned repn,
		unsigned partn)
{
	size_t alignment = set_in->replica[repn]->part[partn].alignment;
	size_t hdrsize = (set_in->options & OPTION_SINGLEHDR) ? 0 : alignment;
	return ALIGN_DOWN(set_in->replica[repn]->part[partn].filesize,
		alignment) - ((partn == 0) ? POOL_HDR_SIZE : hdrsize);
}

/*
 * replica_get_part_offset -- get part's offset from the beginning of replica
 */
uint64_t
replica_get_part_offset(struct pool_set *set, unsigned repn, unsigned partn)
{
	return (uint64_t)set->replica[repn]->part[partn].addr -
		(uint64_t)set->replica[repn]->part[0].addr;
}

/*
 * replica_get_part_data_offset -- get data length before given part
 */
uint64_t
replica_get_part_data_offset(struct pool_set *set, unsigned repn,
		unsigned partn)
{
	if (partn == 0)
		return POOL_HDR_SIZE;

	return (uint64_t)set->replica[repn]->part[partn].addr -
		(uint64_t)set->replica[repn]->part[0].addr;
}

/*
 * replica_remove_part -- unlink part from replica
 */
int
replica_remove_part(struct pool_set *set, unsigned repn, unsigned partn,
			int fix_bad_blocks)
{
	LOG(3, "set %p repn %u partn %u fix_bad_blocks %i",
		set, repn, partn, fix_bad_blocks);

	struct pool_set_part *part = PART(REP(set, repn), partn);
	if (part->fd != -1) {
		os_close(part->fd);
		part->fd = -1;
	}

	int olderrno = errno;
	enum file_type type = util_file_get_type(part->path);
	if (type == OTHER_ERROR)
		return -1;

	/* if the part is a device dax, clear its bad blocks */
	if (type == TYPE_DEVDAX && fix_bad_blocks &&
	    badblocks_clear_all(part->path)) {
		ERR("clearing bad blocks in device dax failed -- '%s'",
			part->path);
		errno = EIO;
		return -1;
	}

	if (type == TYPE_NORMAL && util_unlink(part->path)) {
		ERR("!removing part %u from replica %u failed",
				partn, repn);
		return -1;
	}

	errno = olderrno;
	LOG(4, "Removed part %s number %u from replica %u", part->path, partn,
			repn);
	return 0;
}

/*
 * create_replica_health_status -- (internal) create helping structure for
 *                                 storing replica's health status
 */
static struct replica_health_status *
create_replica_health_status(struct pool_set *set, unsigned repn)
{
	LOG(3, "set %p, repn %u", set, repn);

	unsigned nparts = set->replica[repn]->nparts;
	struct replica_health_status *replica_hs;

	replica_hs = Zalloc(sizeof(struct replica_health_status)
				+ nparts * sizeof(struct part_health_status));
	if (replica_hs == NULL) {
		ERR("!Zalloc for replica health status");
		return NULL;
	}

	replica_hs->nparts = nparts;
	replica_hs->nhdrs = set->replica[repn]->nhdrs;

	return replica_hs;
}

/*
 * replica_part_remove_recovery_file -- remove bad blocks' recovery file
 */
static int
replica_part_remove_recovery_file(struct part_health_status *phs)
{
	LOG(3, "phs %p", phs);

	if (phs->recovery_file_name == NULL || phs->recovery_file_exists == 0)
		return 0;

	if (os_unlink(phs->recovery_file_name) < 0) {
		ERR("!removing the bad block recovery file failed -- '%s'",
			phs->recovery_file_name);
		return -1;
	}

	LOG(3, "bad block recovery file removed -- '%s'",
		phs->recovery_file_name);

	phs->recovery_file_exists = 0;

	return 0;
}

/*
 * replica_remove_all_recovery_files -- remove all recovery files
 */
int
replica_remove_all_recovery_files(struct poolset_health_status *set_hs)
{
	LOG(3, "set_hs %p", set_hs);

	int ret = 0;

	for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
		struct replica_health_status *rhs = set_hs->replica[r];
		for (unsigned p = 0; p < rhs->nparts; ++p)
			ret |= replica_part_remove_recovery_file(&rhs->part[p]);
	}

	return ret;
}

/*
 * replica_free_poolset_health_status -- free memory allocated for helping
 *                                       structure
 */
void
replica_free_poolset_health_status(struct poolset_health_status *set_hs)
{
	LOG(3, "set_hs %p", set_hs);

	for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
		struct replica_health_status *rep_hs = set_hs->replica[r];

		for (unsigned p = 0; p < rep_hs->nparts; ++p) {
			Free(rep_hs->part[p].recovery_file_name);
			Free(rep_hs->part[p].bbs.bbv);
		}

		Free(set_hs->replica[r]);
	}

	Free(set_hs);
}

/*
 * replica_create_poolset_health_status -- create helping structure for storing
 *                                 poolset's health status
 */
int
replica_create_poolset_health_status(struct pool_set *set,
		struct poolset_health_status **set_hsp)
{
	LOG(3, "set %p, set_hsp %p", set, set_hsp);
	unsigned nreplicas = set->nreplicas;
	struct poolset_health_status *set_hs;
	set_hs = Zalloc(sizeof(struct poolset_health_status) +
			nreplicas * sizeof(struct replica_health_status *));
	if (set_hs == NULL) {
		ERR("!Zalloc for poolset health state");
		return -1;
	}
	set_hs->nreplicas = nreplicas;
	for (unsigned i = 0; i < nreplicas; ++i) {
		struct replica_health_status *replica_hs =
				create_replica_health_status(set, i);
		if (replica_hs == NULL) {
			replica_free_poolset_health_status(set_hs);
			return -1;
		}
		set_hs->replica[i] = replica_hs;
	}
	*set_hsp = set_hs;
	return 0;
}

/*
 * replica_is_part_broken -- check if part is marked as broken in the helping
 *                           structure
 */
int
replica_is_part_broken(unsigned repn, unsigned partn,
		struct poolset_health_status *set_hs)
{
	struct replica_health_status *rhs = REP_HEALTH(set_hs, repn);
	return (rhs->flags & IS_BROKEN) ||
		(PART_HEALTH(rhs, partn) & IS_BROKEN);
}

/*
 * is_replica_broken -- check if any part in the replica is marked as broken
 */
int
replica_is_replica_broken(unsigned repn, struct poolset_health_status *set_hs)
{
	LOG(3, "repn %u, set_hs %p", repn, set_hs);
	struct replica_health_status *r_hs = REP_HEALTH(set_hs, repn);
	if (r_hs->flags & IS_BROKEN)
		return 1;

	for (unsigned p = 0; p < r_hs->nparts; ++p) {
		if (replica_is_part_broken(repn, p, set_hs))
			return 1;
	}
	return 0;
}

/*
 * replica_is_replica_consistent -- check if replica is not marked as
 *                                  inconsistent
 */
int
replica_is_replica_consistent(unsigned repn,
		struct poolset_health_status *set_hs)
{
	return !(REP_HEALTH(set_hs, repn)->flags & IS_INCONSISTENT);
}

/*
 * replica_has_bad_blocks -- check if replica has bad blocks
 */
int
replica_has_bad_blocks(unsigned repn, struct poolset_health_status *set_hs)
{
	return REP_HEALTH(set_hs, repn)->flags & HAS_BAD_BLOCKS;
}

/*
 * replica_part_has_bad_blocks -- check if replica's part has bad blocks
 */
int
replica_part_has_bad_blocks(struct part_health_status *phs)
{
	return phs->flags & HAS_BAD_BLOCKS;
}

/*
 * replica_part_has_corrupted_header -- (internal) check if replica's part
 *                              has bad blocks in the header (corrupted header)
 */
int
replica_part_has_corrupted_header(unsigned repn, unsigned partn,
					struct poolset_health_status *set_hs)
{
	struct replica_health_status *rhs = REP_HEALTH(set_hs, repn);
	return PART_HEALTH(rhs, partn) & HAS_CORRUPTED_HEADER;
}

/*
 * replica_has_corrupted_header -- (internal) check if replica has bad blocks
 *                                 in the header (corrupted header)
 */
static int
replica_has_corrupted_header(unsigned repn,
				struct poolset_health_status *set_hs)
{
	return REP_HEALTH(set_hs, repn)->flags & HAS_CORRUPTED_HEADER;
}

/*
 * replica_is_replica_healthy -- check if replica is unbroken and consistent
 */
int
replica_is_replica_healthy(unsigned repn, struct poolset_health_status *set_hs)
{
	LOG(3, "repn %u, set_hs %p", repn, set_hs);

	int ret = !replica_is_replica_broken(repn, set_hs) &&
			replica_is_replica_consistent(repn, set_hs) &&
			!replica_has_bad_blocks(repn, set_hs);

	LOG(4, "return %i", ret);

	return ret;
}

/*
 * replica_has_healthy_header -- (internal) check if replica has healthy headers
 */
static int
replica_has_healthy_header(unsigned repn, struct poolset_health_status *set_hs)
{
	LOG(3, "repn %u, set_hs %p", repn, set_hs);

	int ret = !replica_is_replica_broken(repn, set_hs) &&
			replica_is_replica_consistent(repn, set_hs) &&
			!replica_has_corrupted_header(repn, set_hs);

	LOG(4, "return %i", ret);

	return ret;
}

/*
 * replica_is_poolset_healthy -- check if all replicas in a poolset are not
 *                               marked as broken nor inconsistent in the
 *                               helping structure
 */
int
replica_is_poolset_healthy(struct poolset_health_status *set_hs)
{
	LOG(3, "set_hs %p", set_hs);
	for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
		if (!replica_is_replica_healthy(r, set_hs))
			return 0;
	}
	return 1;
}

/*
 * replica_is_poolset_transformed -- check if the flag indicating a call from
 *                                   pmempool_transform is on
 */
int
replica_is_poolset_transformed(unsigned flags)
{
	return flags & IS_TRANSFORMED;
}

/*
 * replica_find_unbroken_part_with_header -- find a part number in a given
 * replica, which is not marked as broken in the helping structure and contains
 * a pool header
 */
unsigned
replica_find_unbroken_part(unsigned repn, struct poolset_health_status *set_hs)
{
	LOG(3, "repn %u, set_hs %p", repn, set_hs);
	for (unsigned p = 0; p < REP_HEALTH(set_hs, repn)->nhdrs; ++p) {
		if (!replica_is_part_broken(repn, p, set_hs))
			return p;
	}
	return UNDEF_PART;
}

/*
 * replica_find_healthy_replica -- find a replica which is a good source of data
 */
unsigned
replica_find_healthy_replica(struct poolset_health_status *set_hs)
{
	LOG(3, "set_hs %p", set_hs);

	for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
		if (replica_is_replica_healthy(r, set_hs)) {
			LOG(4, "return %i", r);
			return r;
		}
	}

	LOG(4, "return %i", UNDEF_REPLICA);
	return UNDEF_REPLICA;
}

/*
 * replica_find_replica_healthy_header -- find a replica with a healthy header
 */
unsigned
replica_find_replica_healthy_header(struct poolset_health_status *set_hs)
{
	LOG(3, "set_hs %p", set_hs);

	for (unsigned r = 0; r < set_hs->nreplicas; ++r) {
		if (replica_has_healthy_header(r, set_hs)) {
			LOG(4, "return %i", r);
			return r;
		}
	}

	LOG(4, "return %i", UNDEF_REPLICA);
	return UNDEF_REPLICA;
}

/*
 * replica_check_store_size -- (internal) store size from pool descriptor for
 * replica
 */
static int
replica_check_store_size(struct pool_set *set,
	struct poolset_health_status *set_hs, unsigned repn)
{
	LOG(3, "set %p, set_hs %p, repn %u", set, set_hs, repn);
	struct pool_replica *rep = set->replica[repn];
	struct pmemobjpool pop;

	if (rep->remote) {
		memcpy(&pop.hdr, rep->part[0].hdr, sizeof(pop.hdr));
		void *descr = (void *)((uintptr_t)&pop + POOL_HDR_SIZE);
		if (Rpmem_read(rep->remote->rpp, descr, POOL_HDR_SIZE,
			sizeof(pop) - POOL_HDR_SIZE, 0)) {
			return -1;
		}
	} else {
		/* round up map size to Mmap align size */
		if (util_map_part(&rep->part[0], NULL,
		    ALIGN_UP(sizeof(pop), rep->part[0].alignment),
		    0, MAP_SHARED, 1)) {
			return -1;
		}

		memcpy(&pop, rep->part[0].addr, sizeof(pop));

		util_unmap_part(&rep->part[0]);
	}

	void *dscp = (void *)((uintptr_t)&pop + sizeof(pop.hdr));

	if (!util_checksum(dscp, OBJ_DSC_P_SIZE, &pop.checksum, 0,
			0)) {
		set_hs->replica[repn]->flags |= IS_BROKEN;
		return 0;
	}

	set_hs->replica[repn]->pool_size = pop.heap_offset + pop.heap_size;

	return 0;
}

/*
 * check_store_all_sizes -- (internal) store sizes from pool descriptor for all
 * healthy replicas
 */
static int
check_store_all_sizes(struct pool_set *set,
	struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		if (!replica_has_healthy_header(r, set_hs))
			continue;

		if (replica_check_store_size(set, set_hs, r))
			return -1;
	}

	return 0;
}

/*
 * check_and_open_poolset_part_files -- (internal) for each part in a poolset
 * check if the part files are accessible, and if not, mark it as broken
 * in a helping structure; then open the part file
 */
static int
check_and_open_poolset_part_files(struct pool_set *set,
		struct poolset_health_status *set_hs, unsigned flags)
{
	LOG(3, "set %p, set_hs %p, flags %u", set, set_hs, flags);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];
		if (rep->remote) {
			if (util_replica_open_remote(set, r, 0)) {
				LOG(1, "cannot open remote replica no %u", r);
				return -1;
			}

			unsigned nlanes = REMOTE_NLANES;
			int ret = util_poolset_remote_open(rep, r,
					rep->repsize, 0,
					rep->part[0].addr,
					rep->resvsize, &nlanes);
			if (ret) {
				rep_hs->flags |= IS_BROKEN;
				LOG(1, "remote replica #%u marked as BROKEN",
					r);
			}

			continue;
		}

		for (unsigned p = 0; p < rep->nparts; ++p) {
			const char *path = rep->part[p].path;
			enum file_type type = util_file_get_type(path);

			if (type < 0 || os_access(path, R_OK|W_OK) != 0) {
				LOG(1, "part file %s is not accessible", path);
				errno = 0;
				rep_hs->part[p].flags |= IS_BROKEN;
				if (is_dry_run(flags))
					continue;
			}

			if (util_part_open(&rep->part[p], 0, 0)) {
				if (type == TYPE_DEVDAX) {
					LOG(1,
						"opening part on Device DAX %s failed",
						path);
					return -1;
				}
				LOG(1, "opening part %s failed", path);
				errno = 0;
				rep_hs->part[p].flags |= IS_BROKEN;
			}
		}
	}
	return 0;
}

/*
 * map_all_unbroken_headers -- (internal) map all headers in a poolset,
 *                             skipping those marked as broken in a helping
 *                             structure
 */
static int
map_all_unbroken_headers(struct pool_set *set,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nhdrs; ++p) {
			/* skip broken parts */
			if (replica_is_part_broken(r, p, set_hs))
				continue;

			LOG(4, "mapping header for part %u, replica %u", p, r);
			if (util_map_hdr(&rep->part[p], MAP_SHARED, 0) != 0) {
				LOG(1, "header mapping failed - part #%d", p);
				rep_hs->part[p].flags |= IS_BROKEN;
			}
		}
	}
	return 0;
}

/*
 * unmap_all_headers -- (internal) unmap all headers in a poolset
 */
static int
unmap_all_headers(struct pool_set *set)
{
	LOG(3, "set %p", set);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		util_replica_close(set, r);

		if (rep->remote && rep->remote->rpp) {
			Rpmem_close(rep->remote->rpp);
			rep->remote->rpp = NULL;
		}
	}

	return 0;
}

/*
 * check_checksums_and_signatures -- (internal) check if checksums
 *                                   and signatures are correct for parts
 *                                   in a given replica
 */
static int
check_checksums_and_signatures(struct pool_set *set,
				struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = REP(set, r);
		struct replica_health_status *rep_hs = REP_HEALTH(set_hs, r);

		/*
		 * Checksums and signatures of remote replicas are checked
		 * during opening them on the remote side by the rpmem daemon.
		 * The local version of remote headers does not contain
		 * such data.
		 */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nhdrs; ++p) {

			/* skip broken parts */
			if (replica_is_part_broken(r, p, set_hs))
				continue;

			/* check part's checksum */
			LOG(4, "checking checksum for part %u, replica %u",
					p, r);

			struct pool_hdr *hdr = HDR(rep, p);

			if (!util_checksum(hdr, sizeof(*hdr), &hdr->checksum, 0,
					POOL_HDR_CSUM_END_OFF(hdr))) {
				ERR("invalid checksum of pool header");
				rep_hs->part[p].flags |= IS_BROKEN;
			} else if (util_is_zeroed(hdr, sizeof(*hdr))) {
					rep_hs->part[p].flags |= IS_BROKEN;
			}

			enum pool_type type = pool_hdr_get_type(hdr);
			if (type == POOL_TYPE_UNKNOWN) {
				ERR("invalid signature");
				rep_hs->part[p].flags |= IS_BROKEN;
			}
		}
	}
	return 0;
}

/*
 * replica_badblocks_recovery_file_save -- save bad blocks in the bad blocks
 *                                         recovery file before clearing them
 */
static int
replica_badblocks_recovery_file_save(struct part_health_status *part_hs)
{
	LOG(3, "part_health_status %p", part_hs);

	ASSERTeq(part_hs->recovery_file_exists, 1);
	ASSERTne(part_hs->recovery_file_name, NULL);

	struct badblocks *bbs = &part_hs->bbs;
	char *path = part_hs->recovery_file_name;
	int ret = -1;

	int fd = os_open(path, O_WRONLY | O_TRUNC);
	if (fd < 0) {
		ERR("!opening bad block recovery file failed -- '%s'", path);
		return -1;
	}

	FILE *recovery_file_name = os_fdopen(fd, "w");
	if (recovery_file_name == NULL) {
		ERR(
			"!opening a file stream for bad block recovery file failed -- '%s'",
			path);
		os_close(fd);
		return -1;
	}

	/* save bad blocks */
	for (unsigned i = 0; i < bbs->bb_cnt; i++) {
		ASSERT(bbs->bbv[i].length != 0);
		fprintf(recovery_file_name, "%zu %zu\n",
			bbs->bbv[i].offset, bbs->bbv[i].length);
	}

	if (fflush(recovery_file_name) == EOF) {
		ERR("!flushing bad block recovery file failed -- '%s'", path);
		goto exit_error;
	}

	if (os_fsync(fd) < 0) {
		ERR("!syncing bad block recovery file failed -- '%s'", path);
		goto exit_error;
	}

	/* save the finish flag */
	fprintf(recovery_file_name, "0 0\n");

	if (fflush(recovery_file_name) == EOF) {
		ERR("!flushing bad block recovery file failed -- '%s'", path);
		goto exit_error;
	}

	if (os_fsync(fd) < 0) {
		ERR("!syncing bad block recovery file failed -- '%s'", path);
		goto exit_error;
	}

	LOG(3, "bad blocks saved in the recovery file -- '%s'", path);
	ret = 0;

exit_error:
	os_fclose(recovery_file_name);

	return ret;
}

/*
 * replica_part_badblocks_recovery_file_read -- read bad blocks
 *                                             from the bad block recovery file
 *                                             for the current part
 */
static int
replica_part_badblocks_recovery_file_read(struct part_health_status *part_hs)
{
	LOG(3, "part_health_status %p", part_hs);

	ASSERT(part_hs->recovery_file_exists);
	ASSERTne(part_hs->recovery_file_name, NULL);

	VEC(bbsvec, struct bad_block) bbv = VEC_INITIALIZER;
	char *path = part_hs->recovery_file_name;
	struct bad_block bb;
	int ret = -1;

	FILE *recovery_file = os_fopen(path, "r");
	if (!recovery_file) {
		ERR("!opening the recovery file for reading failed -- '%s'",
			path);
		return -1;
	}

	unsigned long long min_offset = 0; /* minimum possible offset */

	do {
		if (fscanf(recovery_file, "%zu %zu\n",
				&bb.offset, &bb.length) < 2) {
			LOG(1, "incomplete bad block recovery file -- '%s'",
				path);
			ret = 1;
			goto error_exit;
		}

		if (bb.offset == 0 && bb.length == 0) {
			/* finish_flag */
			break;
		}

		/* check if bad blocks build an increasing sequence */
		if (bb.offset < min_offset) {
			ERR(
				"wrong format of bad block recovery file (bad blocks are not sorted by the offset in ascending order) -- '%s'",
				path);
			errno = EINVAL;
			ret = -1;
			goto error_exit;
		}

		/* update the minimum possible offset */
		min_offset = bb.offset + bb.length;

		bb.nhealthy = NO_HEALTHY_REPLICA; /* unknown healthy replica */

		/* add the new bad block to the vector */
		if (VEC_PUSH_BACK(&bbv, bb))
			goto error_exit;
	} while (1);

	part_hs->bbs.bbv = VEC_ARR(&bbv);
	part_hs->bbs.bb_cnt = (unsigned)VEC_SIZE(&bbv);

	os_fclose(recovery_file);

	LOG(1, "bad blocks read from the recovery file -- '%s'", path);

	return 0;

error_exit:
	VEC_DELETE(&bbv);
	os_fclose(recovery_file);
	return ret;
}

/* status returned by the replica_badblocks_recovery_files_check() function */
enum badblocks_recovery_files_status {
	RECOVERY_FILES_ERROR = -1,
	RECOVERY_FILES_DO_NOT_EXIST = 0,
	RECOVERY_FILES_EXIST_ALL = 1,
	RECOVERY_FILES_NOT_ALL_EXIST = 2
};

/*
 * replica_badblocks_recovery_files_check -- (internal) check if bad blocks
 *                                           recovery files exist
 */
static enum badblocks_recovery_files_status
replica_badblocks_recovery_files_check(struct pool_set *set,
					struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	int recovery_file_exists = 0;
	int recovery_file_does_not_exist = 0;

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		if (rep->remote) {
			/*
			 * Bad blocks in remote replicas currently are fixed
			 * during opening by removing and recreating
			 * the whole remote replica.
			 */
			continue;
		}

		for (unsigned p = 0; p < rep->nparts; ++p) {
			const char *path = PART(rep, p)->path;
			struct part_health_status *part_hs = &rep_hs->part[p];

			int exists = util_file_exists(path);
			if (exists < 0)
				return -1;

			if (!exists) {
				/* part file does not exist - skip it */
				continue;
			}

			part_hs->recovery_file_name =
					badblocks_recovery_file_alloc(set->path,
									r, p);
			if (part_hs->recovery_file_name == NULL) {
				LOG(1,
					"allocating name of bad block recovery file failed");
				return RECOVERY_FILES_ERROR;
			}

			exists = util_file_exists(part_hs->recovery_file_name);
			if (exists < 0)
				return -1;

			part_hs->recovery_file_exists = exists;

			if (part_hs->recovery_file_exists) {
				LOG(3, "bad block recovery file exists: %s",
					part_hs->recovery_file_name);

				recovery_file_exists = 1;

			} else {
				LOG(3,
					"bad block recovery file does not exist: %s",
					part_hs->recovery_file_name);

				recovery_file_does_not_exist = 1;
			}
		}
	}

	if (recovery_file_exists) {
		if (recovery_file_does_not_exist) {
			LOG(4, "return RECOVERY_FILES_NOT_ALL_EXIST");
			return RECOVERY_FILES_NOT_ALL_EXIST;
		} else {
			LOG(4, "return RECOVERY_FILES_EXIST_ALL");
			return RECOVERY_FILES_EXIST_ALL;
		}
	}

	LOG(4, "return RECOVERY_FILES_DO_NOT_EXIST");
	return RECOVERY_FILES_DO_NOT_EXIST;
}

/*
 * replica_badblocks_recovery_files_read -- (internal) read bad blocks from all
 *                                     bad block recovery files for all parts
 */
static int
replica_badblocks_recovery_files_read(struct pool_set *set,
					struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	int ret;

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		/* XXX: not supported yet */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			const char *path = PART(rep, p)->path;
			struct part_health_status *part_hs = &rep_hs->part[p];

			int exists = util_file_exists(path);
			if (exists < 0)
				return -1;

			if (!exists) {
				/* the part does not exist */
				continue;
			}

			LOG(1,
				"reading bad blocks from the recovery file -- '%s'",
				part_hs->recovery_file_name);

			ret = replica_part_badblocks_recovery_file_read(
								part_hs);
			if (ret < 0) {
				LOG(1,
					"reading bad blocks from the recovery file failed -- '%s'",
					part_hs->recovery_file_name);
				return -1;
			}

			if (ret > 0) {
				LOG(1,
					"incomplete bad block recovery file detected -- '%s'",
					part_hs->recovery_file_name);
				return 1;
			}

			if (part_hs->bbs.bb_cnt) {
				LOG(3, "part %u contains %u bad blocks -- '%s'",
					p, part_hs->bbs.bb_cnt, path);
			}
		}
	}

	return 0;
}

/*
 * replica_badblocks_recovery_files_create_empty -- (internal) create one empty
 *                                                  bad block recovery file
 *                                                  for each part file
 */
static int
replica_badblocks_recovery_files_create_empty(struct pool_set *set,
					struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	struct part_health_status *part_hs;
	const char *path;
	int fd;

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		/* XXX: not supported yet */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			part_hs = &rep_hs->part[p];
			path = PART(rep, p)->path;

			if (!part_hs->recovery_file_name)
				continue;

			fd = os_open(part_hs->recovery_file_name,
					O_RDWR | O_CREAT | O_EXCL,
					0600);
			if (fd < 0) {
				ERR(
					"!creating an empty bad block recovery file failed -- '%s' (part file '%s')",
					part_hs->recovery_file_name, path);
				return -1;
			}

			os_close(fd);

			char *file_name = Strdup(part_hs->recovery_file_name);
			if (file_name == NULL) {
				ERR("!Strdup");
				return -1;
			}

			char *dir_name = dirname(file_name);

			/* fsync the file's directory */
			if (os_fsync_dir(dir_name) < 0) {
				ERR(
					"!syncing the directory of the bad block recovery file failed -- '%s' (part file '%s')",
					dir_name, path);
				Free(file_name);
				return -1;
			}

			Free(file_name);

			part_hs->recovery_file_exists = 1;
		}
	}

	return 0;
}

/*
 * replica_badblocks_recovery_files_save -- (internal) save bad blocks
 *                                     in the bad block recovery files
 */
static int
replica_badblocks_recovery_files_save(struct pool_set *set,
					struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		/* XXX: not supported yet */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			struct part_health_status *part_hs = &rep_hs->part[p];

			if (!part_hs->recovery_file_name)
				continue;

			int ret = replica_badblocks_recovery_file_save(part_hs);
			if (ret < 0) {
				LOG(1,
					"opening bad block recovery file failed -- '%s'",
					part_hs->recovery_file_name);
				return -1;
			}
		}
	}

	return 0;
}

/*
 * replica_badblocks_get -- (internal) get all bad blocks and save them
 *                          in part_hs->bbs structures.
 *                          Returns 1 if any bad block was found, 0 otherwise.
 */
static int
replica_badblocks_get(struct pool_set *set,
			struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	int bad_blocks_found = 0;

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		/* XXX: not supported yet */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			const char *path = PART(rep, p)->path;
			struct part_health_status *part_hs = &rep_hs->part[p];

			int exists = util_file_exists(path);
			if (exists < 0)
				return -1;

			if (!exists)
				continue;

			int ret = badblocks_get(path, &part_hs->bbs);
			if (ret < 0) {
				ERR(
					"!checking the pool part for bad blocks failed -- '%s'",
					path);
				return -1;
			}

			if (part_hs->bbs.bb_cnt) {
				LOG(3, "part %u contains %u bad blocks -- '%s'",
					p, part_hs->bbs.bb_cnt, path);

				bad_blocks_found = 1;
			}
		}
	}

	return bad_blocks_found;
}

/*
 * check_badblocks_in_header -- (internal) check if bad blocks corrupted
 *                              the header
 */
static int
check_badblocks_in_header(struct badblocks *bbs)
{
	for (unsigned b = 0; b < bbs->bb_cnt; b++)
		if (bbs->bbv[b].offset < POOL_HDR_SIZE)
			return 1;

	return 0;
}

/*
 * replica_badblocks_clear -- (internal) clear all bad blocks
 */
static int
replica_badblocks_clear(struct pool_set *set,
			struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	int ret;

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		/* XXX: not supported yet */
		if (rep->remote)
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			const char *path = PART(rep, p)->path;
			struct part_health_status *part_hs = &rep_hs->part[p];

			int exists = util_file_exists(path);
			if (exists < 0)
				return -1;

			if (!exists) {
				/* the part does not exist */
				continue;
			}

			if (part_hs->bbs.bb_cnt == 0) {
				/* no bad blocks found */
				continue;
			}

			/* bad blocks were found */
			part_hs->flags |= HAS_BAD_BLOCKS;
			rep_hs->flags |= HAS_BAD_BLOCKS;

			if (check_badblocks_in_header(&part_hs->bbs)) {
				part_hs->flags |= HAS_CORRUPTED_HEADER;
				if (p == 0)
					rep_hs->flags |= HAS_CORRUPTED_HEADER;
			}

			ret = badblocks_clear(path, &part_hs->bbs);
			if (ret < 0) {
				LOG(1,
					"clearing bad blocks in replica failed -- '%s'",
					path);
				return -1;
			}
		}
	}

	return 0;
}

/*
 * replica_badblocks_check_or_clear -- (internal) check if replica contains
 *                                     bad blocks when in dry run
 *                                     or clear them otherwise
 */
static int
replica_badblocks_check_or_clear(struct pool_set *set,
				struct poolset_health_status *set_hs,
				int dry_run, int called_from_sync,
				int check_bad_blocks, int fix_bad_blocks)
{
	LOG(3,
		"set %p, set_hs %p, dry_run %i, called_from_sync %i, "
		"check_bad_blocks %i, fix_bad_blocks %i",
		set, set_hs, dry_run, called_from_sync,
		check_bad_blocks, fix_bad_blocks);

#define ERR_MSG_BB \
	"       please read the manual first and use this option\n"\
	"       ONLY IF you are sure that you know what you are doing"

	enum badblocks_recovery_files_status status;
	int ret;

	/* check all bad block recovery files */
	status = replica_badblocks_recovery_files_check(set, set_hs);

	/* phase #1 - error handling */
	switch (status) {
	case RECOVERY_FILES_ERROR:
		LOG(1, "checking bad block recovery files failed");
		return -1;

	case RECOVERY_FILES_EXIST_ALL:
	case RECOVERY_FILES_NOT_ALL_EXIST:
		if (!called_from_sync) {
			ERR(
				"error: a bad block recovery file exists, run 'pmempool sync --bad-blocks' to fix bad blocks first");
			return -1;
		}

		if (!fix_bad_blocks) {
			ERR(
				"error: a bad block recovery file exists, but the '--bad-blocks' option is not set\n"
				ERR_MSG_BB);
			return -1;
		}
		break;

	default:
		break;
	};

	/*
	 * The pool is checked for bad blocks only if:
	 * 1) compat feature POOL_FEAT_CHECK_BAD_BLOCKS is set
	 *    OR:
	 * 2) the '--bad-blocks' option is set
	 *
	 * Bad blocks are cleared and fixed only if:
	 * - the '--bad-blocks' option is set
	 */
	if (!fix_bad_blocks && !check_bad_blocks) {
		LOG(3, "skipping bad blocks checking");
		return 0;
	}

	/* phase #2 - reading recovery files */
	switch (status) {
	case RECOVERY_FILES_EXIST_ALL:
		/* read all bad block recovery files */
		ret = replica_badblocks_recovery_files_read(set, set_hs);
		if (ret < 0) {
			LOG(1, "checking bad block recovery files failed");
			return -1;
		}

		if (ret > 0) {
			/* incomplete bad block recovery file was detected */

			LOG(1,
				"warning: incomplete bad block recovery file detected\n"
				"         - all recovery files will be removed");

			/* changing status to RECOVERY_FILES_NOT_ALL_EXIST */
			status = RECOVERY_FILES_NOT_ALL_EXIST;
		}
		break;

	case RECOVERY_FILES_NOT_ALL_EXIST:
		LOG(1,
			"warning: one of bad block recovery files does not exist\n"
			"         - all recovery files will be removed");
		break;

	default:
		break;
	};

	if (status == RECOVERY_FILES_NOT_ALL_EXIST) {
		/*
		 * At least one of bad block recovery files does not exist,
		 * or an incomplete bad block recovery file was detected,
		 * so all recovery files have to be removed.
		 */

		if (!dry_run) {
			LOG(1, "removing all bad block recovery files...");
			ret = replica_remove_all_recovery_files(set_hs);
			if (ret < 0) {
				LOG(1,
					"removing bad block recovery files failed");
				return -1;
			}
		} else {
			LOG(1, "all bad block recovery files would be removed");
		}

		/* changing status to RECOVERY_FILES_DO_NOT_EXIST */
		status = RECOVERY_FILES_DO_NOT_EXIST;
	}

	if (status == RECOVERY_FILES_DO_NOT_EXIST) {
		/*
		 * There are no bad block recovery files,
		 * so let's check bad blocks.
		 */

		int bad_blocks_found = replica_badblocks_get(set, set_hs);
		if (bad_blocks_found < 0) {
			if (errno == ENOTSUP) {
				LOG(1, BB_NOT_SUPP);
				return -1;
			}

			LOG(1, "checking bad blocks failed");
			return -1;
		}

		if (!bad_blocks_found) {
			LOG(4, "no bad blocks found");
			return 0;
		}

		/* bad blocks were found */

		if (!called_from_sync) {
			ERR(
				"error: bad blocks found, run 'pmempool sync --bad-blocks' to fix bad blocks first");
			return -1;
		}

		if (!fix_bad_blocks) {
			ERR(
				"error: bad blocks found, but the '--bad-blocks' option is not set\n"
				ERR_MSG_BB);
			return -1;
		}

		if (dry_run) {
			/* dry-run - do nothing */
			LOG(1, "warning: bad blocks were found");
			return 0;
		}

		/* create one empty recovery file for each part file */
		ret = replica_badblocks_recovery_files_create_empty(set,
								set_hs);
		if (ret < 0) {
			LOG(1,
				"creating empty bad block recovery files failed");
			return -1;
		}

		/* save bad blocks in recovery files */
		ret = replica_badblocks_recovery_files_save(set, set_hs);
		if (ret < 0) {
			LOG(1, "saving bad block recovery files failed");
			return -1;
		}
	}

	if (dry_run) {
		/* dry-run - do nothing */
		LOG(1, "bad blocks would be cleared");
		return 0;
	}

	ret = replica_badblocks_clear(set, set_hs);
	if (ret < 0) {
		ERR("clearing bad blocks failed");
		return -1;
	}

	return 0;
}

/*
 * check_shutdown_state -- (internal) check if replica has
 *			   healthy shutdown_state
 */
static int
check_shutdown_state(struct pool_set *set,
	struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {\
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];
		struct pool_hdr *hdrp = HDR(rep, 0);

		if (rep->remote)
			continue;

		if (hdrp == NULL) {
			/* cannot verify shutdown state */
			rep_hs->flags |= IS_BROKEN;
			continue;
		}

		struct shutdown_state curr_sds;
		shutdown_state_init(&curr_sds, NULL);
		for (unsigned p = 0; p < rep->nparts; ++p) {
			if (PART(rep, p)->fd < 0)
				continue;

			if (shutdown_state_add_part(&curr_sds,
					PART(rep, p)->fd, NULL)) {
				rep_hs->flags |= IS_BROKEN;
				break;
			}
		}

		if (rep_hs->flags & IS_BROKEN)
			continue;

		/* make a copy of sds as we shouldn't modify a pool */
		struct shutdown_state pool_sds = hdrp->sds;

		if (shutdown_state_check(&curr_sds, &pool_sds, NULL))
			rep_hs->flags |= IS_BROKEN;

	}
	return 0;
}

/*
 * check_uuids_between_parts -- (internal) check if uuids between adjacent
 *                              parts are consistent for a given replica
 */
static int
check_uuids_between_parts(struct pool_set *set, unsigned repn,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, repn %u, set_hs %p", set, repn, set_hs);
	struct pool_replica *rep = REP(set, repn);

	/* check poolset_uuid consistency between replica's parts */
	LOG(4, "checking consistency of poolset uuid in replica %u", repn);
	uuid_t poolset_uuid;
	int uuid_stored = 0;
	unsigned part_stored = UNDEF_PART;
	for (unsigned p = 0; p < rep->nhdrs; ++p) {
		/* skip broken parts */
		if (replica_is_part_broken(repn, p, set_hs))
			continue;

		if (!uuid_stored) {
			memcpy(poolset_uuid, HDR(rep, p)->poolset_uuid,
					POOL_HDR_UUID_LEN);
			uuid_stored = 1;
			part_stored = p;
			continue;
		}

		if (uuidcmp(HDR(rep, p)->poolset_uuid, poolset_uuid)) {
			ERR(
				"different poolset uuids in parts from the same replica (repn %u, parts %u and %u) - cannot synchronize",
				repn, part_stored, p);
			errno = EINVAL;
			return -1;
		}
	}

	/* check if all uuids for adjacent replicas are the same across parts */
	LOG(4, "checking consistency of adjacent replicas' uuids in replica %u",
			repn);
	unsigned unbroken_p = UNDEF_PART;
	for (unsigned p = 0; p < rep->nhdrs; ++p) {
		/* skip broken parts */
		if (replica_is_part_broken(repn, p, set_hs))
			continue;

		if (unbroken_p == UNDEF_PART) {
			unbroken_p = p;
			continue;
		}

		struct pool_hdr *hdrp = HDR(rep, p);
		int prev_differ = uuidcmp(HDR(rep, unbroken_p)->prev_repl_uuid,
				hdrp->prev_repl_uuid);
		int next_differ = uuidcmp(HDR(rep, unbroken_p)->next_repl_uuid,
				hdrp->next_repl_uuid);

		if (prev_differ || next_differ) {
			ERR(
				"different adjacent replica UUID between parts (repn %u, parts %u and %u) - cannot synchronize",
				repn, unbroken_p, p);
			errno = EINVAL;
			return -1;
		}
	}

	/* check parts linkage */
	LOG(4, "checking parts linkage in replica %u", repn);
	for (unsigned p = 0; p < rep->nhdrs; ++p) {
		/* skip broken parts */
		if (replica_is_part_broken(repn, p, set_hs))
			continue;

		struct pool_hdr *hdrp = HDR(rep, p);
		struct pool_hdr *next_hdrp = HDRN(rep, p);
		int next_is_broken = replica_is_part_broken(repn, p + 1,
				set_hs);

		if (!next_is_broken) {
			int next_decoupled =
				uuidcmp(next_hdrp->prev_part_uuid,
					hdrp->uuid) ||
				uuidcmp(hdrp->next_part_uuid, next_hdrp->uuid);
			if (next_decoupled) {
				ERR(
					"two consecutive unbroken parts are not linked to each other (repn %u, parts %u and %u) - cannot synchronize",
					repn, p, p + 1);
				errno = EINVAL;
				return -1;
			}
		}
	}
	return 0;
}

/*
 * check_replicas_consistency -- (internal) check if all uuids within each
 *                               replica are consistent
 */
static int
check_replicas_consistency(struct pool_set *set,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		if (check_uuids_between_parts(set, r, set_hs))
			return -1;
	}
	return 0;
}

/*
 * check_replica_options -- (internal) check if options are consistent in the
 *                          replica
 */
static int
check_replica_options(struct pool_set *set, unsigned repn,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, repn %u, set_hs %p", set, repn, set_hs);
	struct pool_replica *rep = REP(set, repn);
	struct replica_health_status *rep_hs = REP_HEALTH(set_hs, repn);
	for (unsigned p = 0; p < rep->nhdrs; ++p) {
		/* skip broken parts */
		if (replica_is_part_broken(repn, p, set_hs))
			continue;

		struct pool_hdr *hdr = HDR(rep, p);
		if (((hdr->features.incompat & POOL_FEAT_SINGLEHDR) == 0) !=
				((set->options & OPTION_SINGLEHDR) == 0)) {
			LOG(1,
				"improper options are set in part %u's header in replica %u",
				p, repn);
			rep_hs->part[p].flags |= IS_BROKEN;
		}
	}
	return 0;
}

/*
 * check_options -- (internal) check if options are consistent in all replicas
 */
static int
check_options(struct pool_set *set, struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		if (check_replica_options(set, r, set_hs))
			return -1;
	}
	return 0;
}

/*
 * check_replica_poolset_uuids - (internal) check if poolset_uuid fields are
 *                               consistent among all parts of a replica;
 *                               the replica is initially considered as
 *                               consistent
 */
static int
check_replica_poolset_uuids(struct pool_set *set, unsigned repn,
		uuid_t poolset_uuid, struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, repn %u, poolset_uuid %p, set_hs %p", set, repn,
			poolset_uuid, set_hs);
	struct pool_replica *rep = REP(set, repn);
	for (unsigned p = 0; p < rep->nhdrs; ++p) {
		/* skip broken parts */
		if (replica_is_part_broken(repn, p, set_hs))
			continue;

		if (uuidcmp(HDR(rep, p)->poolset_uuid, poolset_uuid)) {
			/*
			 * two internally consistent replicas have
			 * different poolset_uuid
			 */
			return -1;
		} else {
			/*
			 * it is sufficient to check only one part
			 * from internally consistent replica
			 */
			break;
		}
	}
	return 0;
}

/*
 * check_poolset_uuids -- (internal) check if poolset_uuid fields are consistent
 *                        among all internally consistent replicas
 */
static int
check_poolset_uuids(struct pool_set *set,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);

	/* find a replica with healthy header */
	unsigned r_h = replica_find_replica_healthy_header(set_hs);
	if (r_h == UNDEF_REPLICA) {
		ERR("no healthy replica found");
		return -1;
	}

	uuid_t poolset_uuid;
	memcpy(poolset_uuid, HDR(REP(set, r_h), 0)->poolset_uuid,
			POOL_HDR_UUID_LEN);

	for (unsigned r = 0; r < set->nreplicas; ++r) {
		/* skip inconsistent replicas */
		if (!replica_is_replica_consistent(r, set_hs) || r == r_h)
			continue;

		if (check_replica_poolset_uuids(set, r, poolset_uuid, set_hs)) {
			ERR(
				"inconsistent poolset uuids between replicas %u and %u - cannot synchronize",
				r_h, r);
			return -1;
		}
	}
	return 0;
}

/*
 * get_replica_uuid -- (internal) get replica uuid
 */
static int
get_replica_uuid(struct pool_replica *rep, unsigned repn,
		struct poolset_health_status *set_hs, uuid_t **uuidpp)
{
	unsigned nhdrs = rep->nhdrs;
	if (!replica_is_part_broken(repn, 0, set_hs)) {
		/* the first part is not broken */
		*uuidpp = &HDR(rep, 0)->uuid;
		return 0;
	} else if (nhdrs > 1 && !replica_is_part_broken(repn, 1, set_hs)) {
		/* the second part is not broken */
		*uuidpp = &HDR(rep, 1)->prev_part_uuid;
		return 0;
	} else if (nhdrs > 1 &&
			!replica_is_part_broken(repn, nhdrs - 1, set_hs)) {
		/* the last part is not broken */
		*uuidpp = &HDR(rep, nhdrs - 1)->next_part_uuid;
		return 0;
	} else {
		/* cannot get replica uuid */
		return -1;
	}
}

/*
 * check_uuids_between_replicas -- (internal) check if uuids between internally
 *                                 consistent adjacent replicas are consistent
 */
static int
check_uuids_between_replicas(struct pool_set *set,
		struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		/* skip comparing inconsistent pairs of replicas */
		if (!replica_is_replica_consistent(r, set_hs) ||
				!replica_is_replica_consistent(r + 1, set_hs))
			continue;

		struct pool_replica *rep = REP(set, r);
		struct pool_replica *rep_n = REPN(set, r);

		/* get uuids of the two adjacent replicas */
		uuid_t *rep_uuidp = NULL;
		uuid_t *rep_n_uuidp = NULL;
		unsigned r_n = REPN_HEALTHidx(set_hs, r);
		if (get_replica_uuid(rep, r, set_hs, &rep_uuidp))
			LOG(2, "cannot get replica uuid, replica %u", r);
		if (get_replica_uuid(rep_n, r_n, set_hs, &rep_n_uuidp))
			LOG(2, "cannot get replica uuid, replica %u", r_n);

		/*
		 * check if replica uuids are consistent between two adjacent
		 * replicas
		 */
		unsigned p = replica_find_unbroken_part(r, set_hs);
		unsigned p_n = replica_find_unbroken_part(r_n, set_hs);
		if (p_n != UNDEF_PART && rep_uuidp != NULL &&
				uuidcmp(*rep_uuidp,
					HDR(rep_n, p_n)->prev_repl_uuid)) {
			ERR(
				"inconsistent replica uuids between replicas %u and %u",
				r, r_n);
			return -1;
		}
		if (p != UNDEF_PART && rep_n_uuidp != NULL &&
				uuidcmp(*rep_n_uuidp,
					HDR(rep, p)->next_repl_uuid)) {
			ERR(
				"inconsistent replica uuids between replicas %u and %u",
				r, r_n);
			return -1;
		}

		/*
		 * check if replica uuids on borders of a broken replica are
		 * consistent
		 */
		unsigned r_nn = REPN_HEALTHidx(set_hs, r_n);
		if (set->nreplicas > 1 && p != UNDEF_PART &&
				replica_is_replica_broken(r_n, set_hs) &&
				replica_is_replica_consistent(r_nn, set_hs)) {
			unsigned p_nn =
				replica_find_unbroken_part(r_nn, set_hs);
			if (p_nn == UNDEF_PART) {
				LOG(2,
					"cannot compare uuids on borders of replica %u",
					r);
				continue;
			}
			struct pool_replica *rep_nn = REP(set, r_nn);
			if (uuidcmp(HDR(rep, p)->next_repl_uuid,
					HDR(rep_nn, p_nn)->prev_repl_uuid)) {
				ERR(
					"inconsistent replica uuids on borders of replica %u",
					r);
				return -1;
			}
		}
	}
	return 0;
}

/*
 * check_replica_cycles -- (internal) check if healthy replicas form cycles
 *	shorter than the number of all replicas
 */
static int
check_replica_cycles(struct pool_set *set,
	struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	unsigned first_healthy;
	unsigned count_healthy = 0;
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		if (!replica_is_replica_healthy(r, set_hs)) {
			count_healthy = 0;
			continue;
		}

		if (count_healthy == 0)
			first_healthy = r;

		++count_healthy;
		struct pool_hdr *hdrh =
			PART(REP(set, first_healthy), 0)->hdr;
		struct pool_hdr *hdr = PART(REP(set, r), 0)->hdr;
		if (uuidcmp(hdrh->uuid, hdr->next_repl_uuid) == 0 &&
			count_healthy < set->nreplicas) {
			/*
			 * Healthy replicas form a cycle shorter than
			 * the number of all replicas; for the user it
			 * means that:
			 */
			ERR(
				"alien replica found (probably coming from a different poolset)");
			return -1;
		}
	}
	return 0;
}

/*
 * check_replica_sizes -- (internal) check if all replicas are large
 *	enough to hold data from a healthy replica
 */
static int
check_replica_sizes(struct pool_set *set, struct poolset_health_status *set_hs)
{
	LOG(3, "set %p, set_hs %p", set, set_hs);
	ssize_t pool_size = -1;
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		/* skip broken replicas */
		if (!replica_is_replica_healthy(r, set_hs))
			continue;

		/* get the size of a pool in the replica */
		ssize_t replica_pool_size;
		if (REP(set, r)->remote)
			/* XXX: no way to get the size of a remote pool yet */
			replica_pool_size = (ssize_t)set->poolsize;
		else
			replica_pool_size = replica_get_pool_size(set, r);

		if (replica_pool_size < 0) {
			LOG(1, "getting pool size from replica %u failed", r);
			set_hs->replica[r]->flags |= IS_BROKEN;
			continue;
		}

		/* check if the pool is bigger than minimum size */
		enum pool_type type = pool_hdr_get_type(HDR(REP(set, r), 0));
		if ((size_t)replica_pool_size < pool_get_min_size(type)) {
			LOG(1,
				"pool size from replica %u is smaller than the minimum size allowed for the pool",
				r);
			set_hs->replica[r]->flags |= IS_BROKEN;
			continue;
		}

		/* check if each replica is big enough to hold the pool data */
		if (set->poolsize < (size_t)replica_pool_size) {
			ERR(
				"some replicas are too small to hold synchronized data");
			return -1;
		}

		if (pool_size < 0) {
			pool_size = replica_pool_size;
			continue;
		}

		/* check if pools in all healthy replicas are of equal size */
		if (pool_size != replica_pool_size) {
			ERR("pool sizes from different replicas differ");
			return -1;
		}
	}
	return 0;
}

/*
 * replica_read_features -- (internal) read features from the header
 */
static int
replica_read_features(struct pool_set *set,
			struct poolset_health_status *set_hs,
			features_t *features)
{
	LOG(3, "set %p set_hs %p features %p", set, set_hs, features);

	ASSERTne(features, NULL);

	for (unsigned r = 0; r < set->nreplicas; r++) {
		struct pool_replica *rep = set->replica[r];
		struct replica_health_status *rep_hs = set_hs->replica[r];

		if (rep->remote) {
			if (rep_hs->flags & IS_BROKEN)
				continue;

			struct pool_hdr *hdrp = rep->part[0].hdr;
			memcpy(features, &hdrp->features, sizeof(*features));

			return 0;
		}

		for (unsigned p = 0; p < rep->nparts; p++) {
			struct pool_set_part *part = &rep->part[p];

			if (part->fd == -1)
				continue;

			if (util_map_hdr(part, MAP_SHARED, 0) != 0) {
				LOG(1, "header mapping failed");
				return -1;
			}

			struct pool_hdr *hdrp = part->hdr;
			memcpy(features, &hdrp->features, sizeof(*features));

			util_unmap_hdr(part);

			return 0;
		}
	}

	/* no healthy replica/part found */
	return -1;
}

/*
 * replica_check_poolset_health -- check if a given poolset can be considered as
 *                         healthy, and store the status in a helping structure
 */
int
replica_check_poolset_health(struct pool_set *set,
		struct poolset_health_status **set_hsp,
		int called_from_sync, unsigned flags)
{
	LOG(3, "set %p, set_hsp %p, called_from_sync %i, flags %u",
		set, set_hsp, called_from_sync, flags);

	if (replica_create_poolset_health_status(set, set_hsp)) {
		LOG(1, "creating poolset health status failed");
		return -1;
	}

	struct poolset_health_status *set_hs = *set_hsp;

	/* check if part files exist and are accessible */
	if (check_and_open_poolset_part_files(set, set_hs, flags)) {
		LOG(1, "poolset part files check failed");
		goto err;
	}

	features_t features;
	int check_bad_blks;
	int fix_bad_blks = called_from_sync && fix_bad_blocks(flags);

	if (fix_bad_blks) {
		/*
		 * We will fix bad blocks, so we cannot read features here,
		 * because reading could fail, because of bad blocks.
		 * We will read features after having bad blocks fixed.
		 *
		 * Fixing bad blocks implies checking bad blocks.
		 */
		check_bad_blks = 1;
	} else {
		/*
		 * We will not fix bad blocks, so we have to read features here.
		 */
		if (replica_read_features(set, set_hs, &features)) {
			LOG(1, "reading features failed");
			goto err;
		}
		check_bad_blks = features.compat & POOL_FEAT_CHECK_BAD_BLOCKS;
	}

	/* check for bad blocks when in dry run or clear them otherwise */
	if (replica_badblocks_check_or_clear(set, set_hs, is_dry_run(flags),
			called_from_sync, check_bad_blks, fix_bad_blks)) {
		LOG(1, "replica bad_blocks check failed");
		goto err;
	}

	/* read features after fixing bad blocks */
	if (fix_bad_blks && replica_read_features(set, set_hs, &features)) {
		LOG(1, "reading features failed");
		goto err;
	}

	/* set ignore_sds flag basing on features read from the header */
	set->ignore_sds = !(features.incompat & POOL_FEAT_SDS);

	/* map all headers */
	map_all_unbroken_headers(set, set_hs);

	/*
	 * Check if checksums and signatures are correct for all parts
	 * in all replicas.
	 */
	check_checksums_and_signatures(set, set_hs);

	/* check if option flags are consistent */
	if (check_options(set, set_hs)) {
		LOG(1, "flags check failed");
		goto err;
	}

	if (!set->ignore_sds && check_shutdown_state(set, set_hs)) {
		LOG(1, "replica shutdown_state check failed");
		goto err;
	}

	/* check if uuids in parts across each replica are consistent */
	if (check_replicas_consistency(set, set_hs)) {
		LOG(1, "replica consistency check failed");
		goto err;
	}

	/* check poolset_uuid values between replicas */
	if (check_poolset_uuids(set, set_hs)) {
		LOG(1, "poolset uuids check failed");
		goto err;
	}

	/* check if uuids for adjacent replicas are consistent */
	if (check_uuids_between_replicas(set, set_hs)) {
		LOG(1, "replica uuids check failed");
		goto err;
	}

	/* check if healthy replicas make up another poolset */
	if (check_replica_cycles(set, set_hs)) {
		LOG(1, "replica cycles check failed");
		goto err;
	}

	/* check if replicas are large enough */
	if (check_replica_sizes(set, set_hs)) {
		LOG(1, "replica sizes check failed");
		goto err;
	}

	if (check_store_all_sizes(set, set_hs)) {
		LOG(1, "reading pool sizes failed");
		goto err;
	}

	unmap_all_headers(set);
	util_poolset_fdclose_always(set);
	return 0;

err:
	errno = EINVAL;
	unmap_all_headers(set);
	util_poolset_fdclose_always(set);
	replica_free_poolset_health_status(set_hs);
	return -1;
}

/*
 * replica_get_pool_size -- find the effective size (mapped) of a pool based
 *                          on metadata from given replica
 */
ssize_t
replica_get_pool_size(struct pool_set *set, unsigned repn)
{
	LOG(3, "set %p, repn %u", set, repn);
	struct pool_set_part *part = PART(REP(set, repn), 0);
	int should_close_part = 0;
	int should_unmap_part = 0;
	if (part->fd == -1) {
		if (util_part_open(part, 0, 0))
			return -1;

		should_close_part = 1;
	}

	if (part->addr == NULL) {
		if (util_map_part(part, NULL,
		    ALIGN_UP(sizeof(PMEMobjpool), part->alignment), 0,
		    MAP_SHARED, 1)) {
			util_part_fdclose(part);
			return -1;
		}
		should_unmap_part = 1;
	}

	PMEMobjpool *pop = (PMEMobjpool *)part->addr;
	ssize_t ret = (ssize_t)(pop->heap_offset + pop->heap_size);

	if (should_unmap_part)
		util_unmap_part(part);
	if (should_close_part)
		util_part_fdclose(part);

	return ret;
}

/*
 * replica_check_part_sizes -- check if all parts are large enough
 */
int
replica_check_part_sizes(struct pool_set *set, size_t min_size)
{
	LOG(3, "set %p, min_size %zu", set, min_size);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		if (rep->remote != NULL)
			/* skip remote replicas */
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			if (PART(rep, p)->filesize < min_size) {
				ERR("replica %u, part %u: file is too small",
						r, p);
				errno = EINVAL;
				return -1;
			}
		}
	}
	return 0;
}

/*
 * replica_check_local_part_dir -- check if directory for the part file
 *                                 exists
 */
int
replica_check_local_part_dir(struct pool_set *set, unsigned repn,
		unsigned partn)
{
	LOG(3, "set %p, repn %u, partn %u", set, repn, partn);
	char *path = Strdup(PART(REP(set, repn), partn)->path);
	const char *dir = dirname(path);
	os_stat_t sb;
	if (os_stat(dir, &sb) != 0 || !(sb.st_mode & S_IFDIR)) {
		ERR(
			"directory %s for part %u in replica %u does not exist or is not accessible",
			path, partn, repn);
		Free(path);
		return -1;
	}
	Free(path);
	return 0;
}

/*
 * replica_check_part_dirs -- (internal) check if directories for part files
 *	exist
 */
int
replica_check_part_dirs(struct pool_set *set)
{
	LOG(3, "set %p", set);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		struct pool_replica *rep = set->replica[r];
		if (rep->remote != NULL)
			/* skip remote replicas */
			continue;

		for (unsigned p = 0; p < rep->nparts; ++p) {
			if (replica_check_local_part_dir(set, r, p))
				return -1;
		}
	}
	return 0;
}

/*
 * replica_open_replica_part_files -- open all part files for a replica
 */
int
replica_open_replica_part_files(struct pool_set *set, unsigned repn)
{
	LOG(3, "set %p, repn %u", set, repn);
	struct pool_replica *rep = set->replica[repn];
	for (unsigned p = 0; p < rep->nparts; ++p) {
		/* skip already opened files */
		if (rep->part[p].fd != -1)
			continue;

		if (util_part_open(&rep->part[p], 0, 0)) {
			LOG(1, "part files open failed for replica %u, part %u",
					repn, p);
			errno = EINVAL;
			goto err;
		}
	}
	return 0;

err:
	util_replica_fdclose(set->replica[repn]);
	return -1;
}

/*
 * replica_open_poolset_part_files -- open all part files for a poolset
 */
int
replica_open_poolset_part_files(struct pool_set *set)
{
	LOG(3, "set %p", set);
	for (unsigned r = 0; r < set->nreplicas; ++r) {
		if (set->replica[r]->remote)
			continue;
		if (replica_open_replica_part_files(set, r)) {
			LOG(1, "opening replica %u, part files failed", r);
			goto err;
		}
	}

	return 0;

err:
	util_poolset_fdclose_always(set);
	return -1;
}

/*
 * pmempool_syncU -- synchronize replicas within a poolset
 */
#ifndef _WIN32
static inline
#endif
int
pmempool_syncU(const char *poolset, unsigned flags)
{
	LOG(3, "poolset %s, flags %u", poolset, flags);
	ASSERTne(poolset, NULL);

	/* check if poolset has correct signature */
	if (util_is_poolset_file(poolset) != 1) {
		ERR("file is not a poolset file");
		goto err;
	}

	/* check if flags are supported */
	if (check_flags_sync(flags)) {
		ERR("unsupported flags");
		errno = EINVAL;
		goto err;
	}

	/* open poolset file */
	int fd = util_file_open(poolset, NULL, 0, O_RDONLY);
	if (fd < 0) {
		ERR("cannot open a poolset file");
		goto err;
	}

	/* fill up pool_set structure */
	struct pool_set *set = NULL;
	if (util_poolset_parse(&set, poolset, fd)) {
		ERR("parsing input poolset failed");
		goto err_close_file;
	}

	if (set->nreplicas == 1) {
		ERR("no replica(s) found in the pool set");
		errno = EINVAL;
		goto err_close_file;
	}

	if (set->remote && util_remote_load()) {
		ERR("remote replication not available");
		errno = ENOTSUP;
		goto err_close_file;
	}

	/* sync all replicas */
	if (replica_sync(set, NULL, flags)) {
		LOG(1, "synchronization failed");
		goto err_close_all;
	}

	util_poolset_close(set, DO_NOT_DELETE_PARTS);
	os_close(fd);
	return 0;

err_close_all:
	util_poolset_close(set, DO_NOT_DELETE_PARTS);

err_close_file:
	os_close(fd);

err:
	if (errno == 0)
		errno = EINVAL;

	return -1;
}

#ifndef _WIN32
/*
 * pmempool_sync -- synchronize replicas within a poolset
 */
int
pmempool_sync(const char *poolset, unsigned flags)
{
	return pmempool_syncU(poolset, flags);
}
#else
/*
 * pmempool_syncW -- synchronize replicas within a poolset in widechar
 */
int
pmempool_syncW(const wchar_t *poolset, unsigned flags)
{
	char *path = util_toUTF8(poolset);
	if (path == NULL) {
		ERR("Invalid poolest file path.");
		return -1;
	}

	int ret = pmempool_syncU(path, flags);

	util_free_UTF8(path);
	return ret;
}
#endif

/*
 * pmempool_transformU -- alter poolset structure
 */
#ifndef _WIN32
static inline
#endif
int
pmempool_transformU(const char *poolset_src,
		const char *poolset_dst, unsigned flags)
{
	LOG(3, "poolset_src %s, poolset_dst %s, flags %u", poolset_src,
			poolset_dst, flags);
	ASSERTne(poolset_src, NULL);
	ASSERTne(poolset_dst, NULL);

	/* check if the source poolset has correct signature */
	if (util_is_poolset_file(poolset_src) != 1) {
		ERR("source file is not a poolset file");
		goto err;
	}

	/* check if the destination poolset has correct signature */
	if (util_is_poolset_file(poolset_dst) != 1) {
		ERR("destination file is not a poolset file");
		goto err;
	}

	/* check if flags are supported */
	if (check_flags_transform(flags)) {
		ERR("unsupported flags");
		errno = EINVAL;
		goto err;
	}

	/* open the source poolset file */
	int fd_in = util_file_open(poolset_src, NULL, 0, O_RDONLY);
	if (fd_in < 0) {
		ERR("cannot open source poolset file");
		goto err;
	}

	/* parse the source poolset file */
	struct pool_set *set_in = NULL;
	if (util_poolset_parse(&set_in, poolset_src, fd_in)) {
		ERR("parsing source poolset failed");
		os_close(fd_in);
		goto err;
	}
	os_close(fd_in);

	/* open the destination poolset file */
	int fd_out = util_file_open(poolset_dst, NULL, 0, O_RDONLY);
	if (fd_out < 0) {
		ERR("cannot open destination poolset file");
		goto err;
	}

	enum del_parts_mode del = DO_NOT_DELETE_PARTS;

	/* parse the destination poolset file */
	struct pool_set *set_out = NULL;
	if (util_poolset_parse(&set_out, poolset_dst, fd_out)) {
		ERR("parsing destination poolset failed");
		os_close(fd_out);
		goto err_free_poolin;
	}
	os_close(fd_out);

	/* check if the source poolset is of a correct type */
	enum pool_type ptype = pool_set_type(set_in);
	if (ptype != POOL_TYPE_OBJ) {
		errno = EINVAL;
		ERR("transform is not supported for given pool type: %s",
				pool_get_pool_type_str(ptype));
		goto err_free_poolout;
	}

	/* load remote library if needed */
	if (set_in->remote && util_remote_load()) {
		ERR("remote replication not available");
		goto err_free_poolout;
	}
	if (set_out->remote && util_remote_load()) {
		ERR("remote replication not available");
		goto err_free_poolout;
	}

	del = is_dry_run(flags) ? DO_NOT_DELETE_PARTS : DELETE_CREATED_PARTS;

	/* transform poolset */
	if (replica_transform(set_in, set_out, flags)) {
		LOG(1, "transformation failed");
		goto err_free_poolout;
	}

	util_poolset_close(set_in, DO_NOT_DELETE_PARTS);
	util_poolset_close(set_out, DO_NOT_DELETE_PARTS);
	return 0;

err_free_poolout:
	util_poolset_close(set_out, del);

err_free_poolin:
	util_poolset_close(set_in, DO_NOT_DELETE_PARTS);

err:
	if (errno == 0)
		errno = EINVAL;

	return -1;
}

#ifndef _WIN32
/*
 * pmempool_transform -- alter poolset structure
 */
int
pmempool_transform(const char *poolset_src,
	const char *poolset_dst, unsigned flags)
{
	return pmempool_transformU(poolset_src, poolset_dst, flags);
}
#else
/*
 * pmempool_transformW -- alter poolset structure in widechar
 */
int
pmempool_transformW(const wchar_t *poolset_src,
	const wchar_t *poolset_dst, unsigned flags)
{
	char *path_src = util_toUTF8(poolset_src);
	if (path_src == NULL) {
		ERR("Invalid source poolest file path.");
		return -1;
	}

	char *path_dst = util_toUTF8(poolset_dst);
	if (path_dst == NULL) {
		ERR("Invalid destination poolest file path.");
		Free(path_src);
		return -1;
	}

	int ret = pmempool_transformU(path_src, path_dst, flags);

	util_free_UTF8(path_src);
	util_free_UTF8(path_dst);
	return ret;
}
#endif