summaryrefslogtreecommitdiff
path: root/tools/lvconvert.c
blob: 0e928d39c0b2882dff9ef74f1b07cccc5aeef051 (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
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
/*
 * Copyright (C) 2005-2016 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "tools.h"

#include "lib/lvmpolld/polldaemon.h"
#include "lib/metadata/lv_alloc.h"
#include "lib/metadata/metadata.h"
#include "lvconvert_poll.h"

#define MAX_PDATA_ARGS	10	/* Max number of accepted args for d-m-p-d tools */

typedef enum {
	/* Split:
	 *   For a mirrored or raid LV, split mirror into two mirrors, optionally tracking
	 *     future changes to the main mirror to allow future recombination.
	 */
	CONV_SPLIT_MIRRORS = 2,

	/* Every other segment type or mirror log conversion we haven't separated out */
	CONV_OTHER = 3,
} conversion_type_t;

struct lvconvert_params {
	/* Exactly one of these 12 command categories is determined */
	int keep_mimages;	/* 2 */	/* --splitmirrors */
	/* other */		/* 3 */

	/* FIXME Eliminate all cases where more than one of the above are set then use conv_type instead */
	conversion_type_t	conv_type;

	int track_changes;	/* CONV_SPLIT_MIRRORS is set */

	int corelog;		/* Equivalent to --mirrorlog core */
	int mirrorlog;		/* Only one of corelog and mirrorlog may be set */

	int mirrors_supplied;	/* When type_str is not set, this may be set with keep_mimages for --splitmirrors */
	const char *type_str;	/* When this is set, mirrors_supplied may optionally also be set */
				/* Holds what you asked for based on --type or other arguments, else "" */

	const struct segment_type *segtype;	/* Holds what segment type you will get */

	int force;
	int yes;
	int zero;

	const char *lv_name;
	const char *lv_split_name;
	const char *lv_name_full;
	const char *vg_name;
	int wait_completion;
	int need_polling;

	uint32_t region_size;
	unsigned region_size_supplied;

	uint32_t mirrors;
	sign_t mirrors_sign;
	uint32_t stripes;
	uint32_t stripe_size;
	unsigned stripes_supplied;
	unsigned stripe_size_supplied;
	uint32_t read_ahead;

	unsigned target_attr;

	alloc_policy_t alloc;

	int pv_count;
	char **pvs;
	struct dm_list *pvh;

	struct logical_volume *lv_to_poll;
	struct dm_list idls;

	const char *origin_name;
};

struct convert_poll_id_list {
	struct dm_list list;
	struct poll_operation_id *id;
	unsigned is_merging_origin:1;
	unsigned is_merging_origin_thin:1;
};

/* FIXME Temporary function until the enum replaces the separate variables */
static void _set_conv_type(struct lvconvert_params *lp, conversion_type_t conv_type)
{
	if (lp->conv_type != CONV_OTHER)
		log_error(INTERNAL_ERROR "Changing conv_type from %d to %d.", lp->conv_type, conv_type);

	lp->conv_type = conv_type;
}

static int _raid0_type_requested(const char *type_str)
{
	return (!strcmp(type_str, SEG_TYPE_NAME_RAID0) || !strcmp(type_str, SEG_TYPE_NAME_RAID0_META));
}

/* mirror/raid* (1,10,4,5,6 and their variants) reshape */
static int _mirror_or_raid_type_requested(struct cmd_context *cmd, const char *type_str)
{
	return (arg_is_set(cmd, mirrors_ARG) || !strcmp(type_str, SEG_TYPE_NAME_MIRROR) ||
		(!strncmp(type_str, SEG_TYPE_NAME_RAID, 4) && !_raid0_type_requested(type_str)));
}

static int _linear_type_requested(const char *type_str)
{
	return (!strcmp(type_str, SEG_TYPE_NAME_LINEAR));
}

static int _striped_type_requested(const char *type_str)
{
	return (!strcmp(type_str, SEG_TYPE_NAME_STRIPED) || _linear_type_requested(type_str));
}

static int _read_conversion_type(struct cmd_context *cmd,
				 struct lvconvert_params *lp)
{

	const char *type_str = arg_str_value(cmd, type_ARG, "");

	lp->type_str =  type_str;
	if (!lp->type_str[0])
		return 1;

	/* FIXME: Check thin-pool and thin more thoroughly! */
	if (!strcmp(type_str, SEG_TYPE_NAME_SNAPSHOT) || _striped_type_requested(type_str) ||
	    !strncmp(type_str, SEG_TYPE_NAME_RAID, 4) || !strcmp(type_str, SEG_TYPE_NAME_MIRROR) ||
	    !strcmp(type_str, SEG_TYPE_NAME_CACHE_POOL) || !strcmp(type_str, SEG_TYPE_NAME_CACHE) ||
	    !strcmp(type_str, SEG_TYPE_NAME_THIN_POOL) || !strcmp(type_str, SEG_TYPE_NAME_THIN))
		return 1;

	log_error("Conversion using --type %s is not supported.", type_str);

	return 0;
}

static int _read_params(struct cmd_context *cmd, struct lvconvert_params *lp)
{
	const char *vg_name = NULL;

	if (!_read_conversion_type(cmd, lp))
		return_0;

	if (!arg_is_set(cmd, background_ARG))
		lp->wait_completion = 1;

	if (arg_is_set(cmd, corelog_ARG))
		lp->corelog = 1;

	if (arg_is_set(cmd, mirrorlog_ARG)) {
		if (lp->corelog) {
			log_error("--mirrorlog and --corelog are incompatible.");
			return 0;
		}
		lp->mirrorlog = 1;
	}

	if (arg_is_set(cmd, trackchanges_ARG))
		lp->track_changes = 1;

	/*
	 * The '--splitmirrors n' argument is equivalent to '--mirrors -n'
	 * (note the minus sign), except that it signifies the additional
	 * intent to keep the mimage that is detached, rather than
	 * discarding it.
	 */
	if (arg_is_set(cmd, splitmirrors_ARG)) {
		if ((lp->lv_split_name = arg_str_value(cmd, name_ARG, NULL))) {
			if (!validate_restricted_lvname_param(cmd, &vg_name, &lp->lv_split_name))
				return_0;
		}

		if (_mirror_or_raid_type_requested(cmd, lp->type_str)) {
			log_error("--mirrors/--type mirror/--type raid* and --splitmirrors are "
				  "mutually exclusive.");
			return 0;
		}

		if (!arg_is_set(cmd, name_ARG) && !lp->track_changes) {
			log_error("Please name the new logical volume using '--name'");
			return 0;
		}

		if ((lp->lv_split_name = arg_str_value(cmd, name_ARG, NULL))) {
			if (!validate_restricted_lvname_param(cmd, &vg_name, &lp->lv_split_name))
				return_0;
		}

		lp->keep_mimages = 1;
		_set_conv_type(lp, CONV_SPLIT_MIRRORS);
		lp->mirrors = arg_uint_value(cmd, splitmirrors_ARG, 0);
		lp->mirrors_sign = SIGN_MINUS;
	}

	/* If no other case was identified, then use of --stripes means --type striped */
	if (!arg_is_set(cmd, type_ARG) && !*lp->type_str &&
	    !lp->mirrorlog && !lp->corelog &&
	    (arg_is_set(cmd, stripes_long_ARG) || arg_is_set(cmd, stripesize_ARG)))
		lp->type_str = SEG_TYPE_NAME_STRIPED;

	if ((arg_is_set(cmd, stripes_long_ARG) || arg_is_set(cmd, stripesize_ARG)) &&
	    !(_mirror_or_raid_type_requested(cmd, lp->type_str) || _striped_type_requested(lp->type_str) ||
	      _raid0_type_requested(lp->type_str) || arg_is_set(cmd, thinpool_ARG))) {
		log_error("--stripes or --stripesize argument is only valid "
			  "with --mirrors/--type mirror/--type raid*/--type striped/--type linear, --repair and --thinpool");
		return 0;
	}

	if (arg_is_set(cmd, mirrors_ARG)) {
		/* --splitmirrors is the mechanism for detaching and keeping a mimage */
		lp->mirrors_supplied = 1;
		lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0);
		lp->mirrors_sign = arg_sign_value(cmd, mirrors_ARG, SIGN_NONE);
	}

	lp->alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);

	/*
	 * Final checking of each case:
	 *   lp->keep_mimages
	 *   --type mirror|raid  lp->mirrorlog lp->corelog
	 *   --type raid0|striped
	 */
	switch(lp->conv_type) {
	case CONV_SPLIT_MIRRORS:
                break;

	case CONV_OTHER:
		if (arg_is_set(cmd, regionsize_ARG)) {
			lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
			lp->region_size_supplied = 1;
		} else {
			lp->region_size = get_default_region_size(cmd);
			lp->region_size_supplied = 0;
		}

		if (_mirror_or_raid_type_requested(cmd, lp->type_str) ||
			   lp->mirrorlog || lp->corelog) { /* Mirrors (and some RAID functions) */
			if (arg_is_set(cmd, chunksize_ARG)) {
				log_error("--chunksize is only available with snapshots or pools.");
				return 0;
			}

			if (arg_is_set(cmd, zero_ARG)) {
				log_error("--zero is only available with snapshots or pools.");
				return 0;
			}

			/* FIXME man page says in one place that --type and --mirrors can't be mixed */
			if (lp->mirrors_supplied && !lp->mirrors)
				/* down-converting to linear/stripe? */
				lp->type_str = SEG_TYPE_NAME_STRIPED;

		} else if (_raid0_type_requested(lp->type_str) || _striped_type_requested(lp->type_str)) { /* striped or linear or raid0 */
			if (arg_from_list_is_set(cmd, "cannot be used with --type raid0 or --type striped or --type linear",
						 chunksize_ARG, corelog_ARG, mirrors_ARG, mirrorlog_ARG, zero_ARG,
						 -1))
				return_0;
		} /* else segtype will default to current type */
	}

	lp->force = arg_count(cmd, force_ARG);
	lp->yes = arg_count(cmd, yes_ARG);

	return 1;
}


static struct poll_functions _lvconvert_mirror_fns = {
	.poll_progress = poll_mirror_progress,
	.finish_copy = lvconvert_mirror_finish,
};

static struct poll_functions _lvconvert_merge_fns = {
	.poll_progress = poll_merge_progress,
	.finish_copy = lvconvert_merge_finish,
};

static struct poll_functions _lvconvert_thin_merge_fns = {
	.poll_progress = poll_thin_merge_progress,
	.finish_copy = lvconvert_merge_finish,
};

static struct poll_operation_id *_create_id(struct cmd_context *cmd,
					    const char *vg_name,
					    const char *lv_name,
					    const char *uuid)
{
	struct poll_operation_id *id;
	char lv_full_name[NAME_LEN];

	if (!vg_name || !lv_name || !uuid) {
		log_error(INTERNAL_ERROR "Wrong params for lvconvert _create_id.");
		return NULL;
	}

	if (dm_snprintf(lv_full_name, sizeof(lv_full_name), "%s/%s", vg_name, lv_name) < 0) {
		log_error(INTERNAL_ERROR "Name \"%s/%s\" is too long.", vg_name, lv_name);
		return NULL;
	}

	if (!(id = dm_pool_alloc(cmd->mem, sizeof(*id)))) {
		log_error("Poll operation ID allocation failed.");
		return NULL;
	}

	if (!(id->display_name = dm_pool_strdup(cmd->mem, lv_full_name)) ||
	    !(id->lv_name = strchr(id->display_name, '/')) ||
	    !(id->vg_name = dm_pool_strdup(cmd->mem, vg_name)) ||
	    !(id->uuid = dm_pool_strdup(cmd->mem, uuid))) {
		log_error("Failed to copy one or more poll operation ID members.");
		dm_pool_free(cmd->mem, id);
		return NULL;
	}

	id->lv_name++; /* skip over '/' */

	return id;
}

static int _lvconvert_poll_by_id(struct cmd_context *cmd, struct poll_operation_id *id,
				 unsigned background,
				 int is_merging_origin,
				 int is_merging_origin_thin)
{
	if (test_mode())
		return ECMD_PROCESSED;

	if (is_merging_origin)
		return poll_daemon(cmd, background,
				(MERGING | (is_merging_origin_thin ? THIN_VOLUME : SNAPSHOT)),
				is_merging_origin_thin ? &_lvconvert_thin_merge_fns : &_lvconvert_merge_fns,
				"Merged", id);

	return poll_daemon(cmd, background, CONVERTING,
			   &_lvconvert_mirror_fns, "Converted", id);
}

int lvconvert_poll(struct cmd_context *cmd, struct logical_volume *lv,
		   unsigned background)
{
	int r;
	struct poll_operation_id *id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s);
	int is_merging_origin = 0;
	int is_merging_origin_thin = 0;

	if (!id) {
		log_error("Failed to allocate poll identifier for lvconvert.");
		return ECMD_FAILED;
	}

	/* FIXME: check this in polling instead */
	if (lv_is_merging_origin(lv)) {
		is_merging_origin = 1;
		is_merging_origin_thin = seg_is_thin_volume(find_snapshot(lv));
	}

	r = _lvconvert_poll_by_id(cmd, id, background, is_merging_origin, is_merging_origin_thin);

	return r;
}

static int _insert_lvconvert_layer(struct cmd_context *cmd,
				   struct logical_volume *lv)
{
	char format[NAME_LEN], layer_name[NAME_LEN];
	int i;

	/*
	 * We would like to give the same number for this layer
	 * and the newly added mimage.
	 * However, LV name of newly added mimage is determined *after*
	 * the LV name of this layer is determined.
	 *
	 * So, use generate_lv_name() to generate mimage name first
	 * and take the number from it.
	 */

	if (dm_snprintf(format, sizeof(format), "%s_mimage_%%d", lv->name) < 0) {
		log_error("lvconvert: layer name creation failed.");
		return 0;
	}

	if (!generate_lv_name(lv->vg, format, layer_name, sizeof(layer_name)) ||
	    sscanf(layer_name, format, &i) != 1) {
		log_error("lvconvert: layer name generation failed.");
		return 0;
	}

	if (dm_snprintf(layer_name, sizeof(layer_name), MIRROR_SYNC_LAYER "_%d", i) < 0) {
		log_error("layer name creation failed.");
		return 0;
	}

	if (!insert_layer_for_lv(cmd, lv, 0, layer_name)) {
		log_error("Failed to insert resync layer");
		return 0;
	}

	return 1;
}

static int _failed_mirrors_count(struct logical_volume *lv)
{
	struct lv_segment *lvseg;
	int ret = 0;
	unsigned s;

	dm_list_iterate_items(lvseg, &lv->segments) {
		if (!seg_is_mirrored(lvseg))
			return -1;
		for (s = 0; s < lvseg->area_count; s++) {
			if (seg_type(lvseg, s) == AREA_LV) {
				if (is_temporary_mirror_layer(seg_lv(lvseg, s)))
					ret += _failed_mirrors_count(seg_lv(lvseg, s));
				else if (lv_is_partial(seg_lv(lvseg, s)))
					++ ret;
			}
			else if (seg_type(lvseg, s) == AREA_PV &&
				 is_missing_pv(seg_pv(lvseg, s)))
				++ret;
		}
	}

	return ret;
}

static int _failed_logs_count(struct logical_volume *lv)
{
	int ret = 0;
	unsigned s;
	struct logical_volume *log_lv = first_seg(lv)->log_lv;
	if (log_lv && lv_is_partial(log_lv)) {
		if (lv_is_mirrored(log_lv))
			ret += _failed_mirrors_count(log_lv);
		else
			ret += 1;
	}
	for (s = 0; s < first_seg(lv)->area_count; s++) {
		if (seg_type(first_seg(lv), s) == AREA_LV &&
		    is_temporary_mirror_layer(seg_lv(first_seg(lv), s)))
			ret += _failed_logs_count(seg_lv(first_seg(lv), s));
	}
	return ret;
}


static struct dm_list *_failed_pv_list(struct volume_group *vg)
{
	struct dm_list *failed_pvs;
	struct pv_list *pvl, *new_pvl;

	if (!(failed_pvs = dm_pool_alloc(vg->vgmem, sizeof(*failed_pvs)))) {
		log_error("Allocation of list of failed_pvs failed.");
		return NULL;
	}

	dm_list_init(failed_pvs);

	dm_list_iterate_items(pvl, &vg->pvs) {
		if (!is_missing_pv(pvl->pv))
			continue;

		/*
		 * Finally, --repair will remove empty PVs.
		 * But we only want remove these which are output of repair,
		 * Do not count these which are already empty here.
		 * FIXME: code should traverse PV in LV not in whole VG.
		 * FIXME: layer violation? should it depend on vgreduce --removemising?
		 */
		if (pvl->pv->pe_alloc_count == 0)
			continue;

		if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
			log_error("Allocation of failed_pvs list entry failed.");
			return NULL;
		}
		new_pvl->pv = pvl->pv;
		dm_list_add(failed_pvs, &new_pvl->list);
	}

	return failed_pvs;
}

static int _is_partial_lv(struct logical_volume *lv,
			  void *baton __attribute__((unused)))
{
	return lv_is_partial(lv);
}

/*
 * Walk down the stacked mirror LV to the original mirror LV.
 */
static struct logical_volume *_original_lv(struct logical_volume *lv)
{
	struct logical_volume *next_lv = lv, *tmp_lv;

	while ((tmp_lv = find_temporary_mirror(next_lv)))
		next_lv = tmp_lv;

	return next_lv;
}

static void _lvconvert_mirrors_repair_ask(struct cmd_context *cmd,
					  int failed_log, int failed_mirrors,
					  int *replace_log, int *replace_mirrors)
{
	const char *leg_policy, *log_policy;
	int force = arg_count(cmd, force_ARG);
	int yes = arg_count(cmd, yes_ARG);

	if (arg_is_set(cmd, usepolicies_ARG)) {
		leg_policy = find_config_tree_str(cmd, activation_mirror_image_fault_policy_CFG, NULL);
		log_policy = find_config_tree_str(cmd, activation_mirror_log_fault_policy_CFG, NULL);
		*replace_mirrors = strcmp(leg_policy, "remove");
		*replace_log = strcmp(log_policy, "remove");
		return;
	}

	if (force != PROMPT) {
		*replace_log = *replace_mirrors = 0;
		return;
	}

	*replace_log = *replace_mirrors = 1;

	if (yes)
		return;

	if (failed_log &&
	    yes_no_prompt("Attempt to replace failed mirror log? [y/n]: ") == 'n')
		*replace_log = 0;

	if (failed_mirrors &&
	    yes_no_prompt("Attempt to replace failed mirror images "
			  "(requires full device resync)? [y/n]: ") == 'n')
		*replace_mirrors = 0;
}

/*
 * _get_log_count
 * @lv: the mirror LV
 *
 * Get the number of on-disk copies of the log.
 *  0  = 'core'
 *  1  = 'disk'
 *  2+ = 'mirrored'
 */
static uint32_t _get_log_count(struct logical_volume *lv)
{
	struct logical_volume *log_lv;

	log_lv = first_seg(_original_lv(lv))->log_lv;
	if (log_lv)
		return lv_mirror_count(log_lv);

	return 0;
}

static int _lv_update_mirrored_log(struct logical_volume *lv,
				   struct dm_list *operable_pvs,
				   int log_count)
{
	int old_log_count;
	struct logical_volume *log_lv;

	/*
	 * When log_count is 0, mirrored log doesn't need to be
	 * updated here but it will be removed later.
	 */
	if (!log_count)
		return 1;

	log_lv = first_seg(_original_lv(lv))->log_lv;
	if (!log_lv || !lv_is_mirrored(log_lv))
		return 1;

	old_log_count = _get_log_count(lv);
	if (old_log_count == log_count)
		return 1;

	/* Reducing redundancy of the log */
	return remove_mirror_images(log_lv, log_count,
				    is_mirror_image_removable,
				    operable_pvs, 0U);
}

static int _lv_update_log_type(struct cmd_context *cmd,
			       struct lvconvert_params *lp,
			       struct logical_volume *lv,
			       struct dm_list *operable_pvs,
			       int log_count)
{
	int old_log_count;
	uint32_t region_size = (lp) ? lp->region_size :
		first_seg(lv)->region_size;
	alloc_policy_t alloc = (lp) ? lp->alloc : lv->alloc;
	struct logical_volume *original_lv;
	struct logical_volume *log_lv;

	old_log_count = _get_log_count(lv);
	if (old_log_count == log_count)
		return 1;

	original_lv = _original_lv(lv);
	/* Remove an existing log completely */
	if (!log_count) {
		if (!remove_mirror_log(cmd, original_lv, operable_pvs,
				       arg_count(cmd, yes_ARG) ||
				       arg_count(cmd, force_ARG)))
			return_0;
		return 1;
	}

	log_lv = first_seg(original_lv)->log_lv;

	/* Adding redundancy to the log */
	if (old_log_count < log_count) {
		if (!(region_size = adjusted_mirror_region_size(cmd, lv->vg->extent_size,
								lv->le_count,
								region_size, 0,
								vg_is_clustered(lv->vg))))
			return_0;

		if (!add_mirror_log(cmd, original_lv, log_count,
				    region_size, operable_pvs, alloc))
			return_0;
		/*
		 * FIXME: This simple approach won't work in cluster mirrors,
		 *	  but it doesn't matter because we don't support
		 *	  mirrored logs in cluster mirrors.
		 */
		if (old_log_count &&
		    !lv_update_and_reload(log_lv))
			return_0;

		return 1;
	}

	/* Reducing redundancy of the log */
	return remove_mirror_images(log_lv, log_count,
				    is_mirror_image_removable, operable_pvs, 1U);
}

/*
 * Reomove missing and empty PVs from VG, if are also in provided list
 */
static void _remove_missing_empty_pv(struct volume_group *vg, struct dm_list *remove_pvs)
{
	struct pv_list *pvl, *pvl_vg, *pvlt;
	int removed = 0;

	if (!remove_pvs)
		return;

	dm_list_iterate_items(pvl, remove_pvs) {
		dm_list_iterate_items_safe(pvl_vg, pvlt, &vg->pvs) {
			if (!id_equal(&pvl->pv->id, &pvl_vg->pv->id) ||
			    !is_missing_pv(pvl_vg->pv) ||
			    pvl_vg->pv->pe_alloc_count != 0)
				continue;

			/* FIXME: duplication of vgreduce code, move this to library */
			vg->free_count -= pvl_vg->pv->pe_count;
			vg->extent_count -= pvl_vg->pv->pe_count;
			del_pvl_from_vgs(vg, pvl_vg);
			free_pv_fid(pvl_vg->pv);

			removed++;
		}
	}

	if (removed) {
		if (!vg_write(vg) || !vg_commit(vg)) {
			stack;
			return;
		}
		log_warn("WARNING: %d missing and now unallocated Physical Volumes removed from VG.", removed);
	}
}

/*
 * _lvconvert_mirrors_parse_params
 *
 * This function performs the following:
 *  1) Gets the old values of mimage and log counts
 *  2) Parses the CLI args to find the new desired values
 *  3) Adjusts 'lp->mirrors' to the appropriate absolute value.
 *     (Remember, 'lp->mirrors' is specified in terms of the number of "copies"
 *     vs. the number of mimages.  It can also be a relative value.)
 *  4) Sets 'lp->need_polling' if collapsing
 *  5) Validates other mirror params
 *
 * Returns: 1 on success, 0 on error
 */
static int _lvconvert_mirrors_parse_params(struct cmd_context *cmd,
					   struct logical_volume *lv,
					   struct lvconvert_params *lp,
					   uint32_t *old_mimage_count,
					   uint32_t *old_log_count,
					   uint32_t *new_mimage_count,
					   uint32_t *new_log_count)
{
	*old_mimage_count = lv_mirror_count(lv);
	*old_log_count = _get_log_count(lv);

	if (lv->vg->lock_type && !strcmp(lv->vg->lock_type, "sanlock") && lp->keep_mimages) {
		/* FIXME: we need to create a sanlock lock on disk for the new LV. */
		log_error("Unable to split mirrors in VG with lock_type %s", lv->vg->lock_type);
		return 0;
	}

	/*
	 * Adjusting mimage count?
	 */
	if (!lp->mirrors_supplied && !lp->keep_mimages)
		lp->mirrors = *old_mimage_count;
	else if (lp->mirrors_sign == SIGN_PLUS)
		lp->mirrors = *old_mimage_count + lp->mirrors;
	else if (lp->mirrors_sign == SIGN_MINUS)
		lp->mirrors = (*old_mimage_count > lp->mirrors) ?
			*old_mimage_count - lp->mirrors: 0;
	else
		lp->mirrors += 1;

	*new_mimage_count = lp->mirrors;

	/* Too many mimages? */
	if (lp->mirrors > DEFAULT_MIRROR_MAX_IMAGES) {
		log_error("Only up to %d images in mirror supported currently.",
			  DEFAULT_MIRROR_MAX_IMAGES);
		return 0;
	}

	/* Did the user try to subtract more legs than available? */
	if (lp->mirrors < 1) {
		log_error("Unable to reduce images by specified amount - only %d in %s",
			  *old_mimage_count, lv->name);
		return 0;
	}

	/*
	 * FIXME: It would be nice to say what we are adjusting to, but
	 * I really don't know whether to specify the # of copies or mimages.
	 */
	if (*old_mimage_count != *new_mimage_count)
		log_verbose("Adjusting mirror image count of %s", lv->name);

	/* If region size is not given by user - use value from mirror */
	if (lv_is_mirrored(lv) && !lp->region_size_supplied) {
		lp->region_size = first_seg(lv)->region_size;
		log_debug("Copying region size %s from existing mirror.",
			  display_size(lv->vg->cmd, lp->region_size));
	}

	/*
	 * Adjust log type
	 *
	 * If we are converting from a mirror to another mirror or simply
	 * changing the log type, we start by assuming they want the log
	 * type the same and then parse the given args.  OTOH, If we are
	 * converting from linear to mirror, then we start from the default
	 * position that the user would like a 'disk' log.
	 */
	*new_log_count = (*old_mimage_count > 1) ? *old_log_count : 1;
	if (!lp->corelog && !lp->mirrorlog)
		return 1;

	*new_log_count = arg_int_value(cmd, mirrorlog_ARG, lp->corelog ? MIRROR_LOG_CORE : DEFAULT_MIRRORLOG);

	log_verbose("Setting logging type to %s.", get_mirror_log_name(*new_log_count));

	/*
	 * Region size must not change on existing mirrors
	 */
	if (arg_is_set(cmd, regionsize_ARG) && lv_is_mirrored(lv) &&
	    (lp->region_size != first_seg(lv)->region_size)) {
		log_error("Mirror log region size cannot be changed on "
			  "an existing mirror.");
		return 0;
	}

	/*
	 * For the most part, we cannot handle multi-segment mirrors. Bail out
	 * early if we have encountered one.
	 */
	if (lv_is_mirrored(lv) && dm_list_size(&lv->segments) != 1) {
		log_error("Logical volume %s has multiple "
			  "mirror segments.", display_lvname(lv));
		return 0;
	}

	return 1;
}

/*
 * _lvconvert_mirrors_aux
 *
 * Add/remove mirror images and adjust log type.  'operable_pvs'
 * are the set of PVs open to removal or allocation - depending
 * on the operation being performed.
 */
static int _lvconvert_mirrors_aux(struct cmd_context *cmd,
				  struct logical_volume *lv,
				  struct lvconvert_params *lp,
				  struct dm_list *operable_pvs,
				  uint32_t new_mimage_count,
				  uint32_t new_log_count,
				  struct dm_list *pvh)
{
	uint32_t region_size;
	struct lv_segment *seg = first_seg(lv);
	struct logical_volume *layer_lv;
	uint32_t old_mimage_count = lv_mirror_count(lv);
	uint32_t old_log_count = _get_log_count(lv);

	if ((lp->mirrors == 1) && !lv_is_mirrored(lv)) {
		log_warn("WARNING: Logical volume %s is already not mirrored.",
			 display_lvname(lv));
		return 1;
	}

	if (!(region_size = adjusted_mirror_region_size(cmd, lv->vg->extent_size,
							lv->le_count,
							lp->region_size ? : seg->region_size, 0,
							vg_is_clustered(lv->vg))))
		return_0;

	if (lv_component_is_active(lv)) {
		log_error("Cannot convert logical volume %s with active component LV(s).",
			  display_lvname(lv));
		return 0;
	}

	if (!operable_pvs)
		operable_pvs = pvh;

	/*
	 * Up-convert from linear to mirror
	 */
	if (!lv_is_mirrored(lv)) {
		/* FIXME Share code with lvcreate */

		/*
		 * FIXME should we give not only pvh, but also all PVs
		 * currently taken by the mirror? Would make more sense from
		 * user perspective.
		 */
		if (!lv_add_mirrors(cmd, lv, new_mimage_count - 1, lp->stripes,
				    lp->stripe_size, region_size, new_log_count, operable_pvs,
				    lp->alloc, MIRROR_BY_LV))
			return_0;

		if (!arg_is_set(cmd, background_ARG))
			lp->need_polling = 1;

		goto out;
	}

	/*
	 * Up-convert m-way mirror to n-way mirror
	 */
	if (new_mimage_count > old_mimage_count) {
		if (lv_is_not_synced(lv)) {
			log_error("Can't add mirror to out-of-sync mirrored "
				  "LV: use lvchange --resync first.");
			return 0;
		}

		/*
		 * We allow snapshots of mirrors, but for now, we
		 * do not allow up converting mirrors that are under
		 * snapshots.  The layering logic is somewhat complex,
		 * and preliminary test show that the conversion can't
		 * seem to get the correct %'age of completion.
		 */
		if (lv_is_origin(lv)) {
			log_error("Can't add additional mirror images to "
				  "mirror %s which is under snapshots.",
				  display_lvname(lv));
			return 0;
		}

		/*
		 * Is there already a convert in progress?  We do not
		 * currently allow more than one.
		 */
		if (find_temporary_mirror(lv) || lv_is_converting(lv)) {
			log_error("%s is already being converted.  Unable to start another conversion.",
				  display_lvname(lv));
			return 0;
		}

		/*
		 * Log addition/removal should be done before the layer
		 * insertion to make the end result consistent with
		 * linear-to-mirror conversion.
		 */
		if (!_lv_update_log_type(cmd, lp, lv,
					 operable_pvs, new_log_count))
			return_0;

		/* Insert a temporary layer for syncing,
		 * only if the original lv is using disk log. */
		if (seg->log_lv && !_insert_lvconvert_layer(cmd, lv)) {
			log_error("Failed to insert resync layer.");
			return 0;
		}

		/* FIXME: can't have multiple mlogs. force corelog. */
		if (!lv_add_mirrors(cmd, lv,
				    new_mimage_count - old_mimage_count,
				    lp->stripes, lp->stripe_size,
				    region_size, 0U, operable_pvs, lp->alloc,
				    MIRROR_BY_LV)) {
			/* FIXME: failure inside library -> move error path processing into library. */
			layer_lv = seg_lv(first_seg(lv), 0);
			if (!remove_layer_from_lv(lv, layer_lv) ||
			    (lv_is_active(lv) && !deactivate_lv(cmd, layer_lv)) ||
			    !lv_remove(layer_lv) ||
			    !vg_write(lv->vg) || !vg_commit(lv->vg)) {
				log_error("ABORTING: Failed to remove "
					  "temporary mirror layer %s.",
					  display_lvname(layer_lv));
				log_error("Manual cleanup with vgcfgrestore "
					  "and dmsetup may be required.");
				return 0;
			}

			return_0;
		}
		if (seg->log_lv)
			lv->status |= CONVERTING;
		lp->need_polling = 1;

		goto out_skip_log_convert;
	}

	/*
	 * Down-convert (reduce # of mimages).
	 */
	if (new_mimage_count < old_mimage_count) {
		uint32_t nmc = old_mimage_count - new_mimage_count;
		uint32_t nlc = (!new_log_count || lp->mirrors == 1) ? 1U : 0U;

		/* FIXME: Why did nlc used to be calculated that way? */

		/* Reduce number of mirrors */
		if (lp->keep_mimages) {
			if (lp->track_changes) {
				log_error("--trackchanges is not available "
					  "to 'mirror' segment type.");
				return 0;
			}
			if (!lv_split_mirror_images(lv, lp->lv_split_name,
						    nmc, operable_pvs))
				return_0;
		} else if (!lv_remove_mirrors(cmd, lv, nmc, nlc,
					      is_mirror_image_removable, operable_pvs, 0))
			return_0;

		goto out; /* Just in case someone puts code between */
	}

out:
	/*
	 * Converting the log type
	 */
	if (lv_is_mirrored(lv) && (old_log_count != new_log_count)) {
		if (!_lv_update_log_type(cmd, lp, lv,
					 operable_pvs, new_log_count))
			return_0;
	}

out_skip_log_convert:

	if (!lv_update_and_reload(lv))
		return_0;

	return 1;
}

int mirror_remove_missing(struct cmd_context *cmd,
			  struct logical_volume *lv, int force)
{
	struct dm_list *failed_pvs;
	int log_count = _get_log_count(lv) - _failed_logs_count(lv);

	if (!(failed_pvs = _failed_pv_list(lv->vg)))
		return_0;

	if (force && _failed_mirrors_count(lv) == (int)lv_mirror_count(lv)) {
		log_error("No usable images left in %s.", display_lvname(lv));
		return lv_remove_with_dependencies(cmd, lv, DONT_PROMPT, 0);
	}

	/*
	 * We must adjust the log first, or the entire mirror
	 * will get stuck during a suspend.
	 */
	if (!_lv_update_mirrored_log(lv, failed_pvs, log_count))
		return_0;

	if (_failed_mirrors_count(lv) > 0 &&
	    !lv_remove_mirrors(cmd, lv, _failed_mirrors_count(lv),
			       log_count ? 0U : 1U,
			       _is_partial_lv, NULL, 0))
		return_0;

	if (lv_is_mirrored(lv) &&
	    !_lv_update_log_type(cmd, NULL, lv, failed_pvs, log_count))
		return_0;

	if (!lv_update_and_reload(lv))
		return_0;

	return 1;
}

/*
 * _lvconvert_mirrors_repair
 *
 * This function operates in two phases.  First, all of the bad
 * devices are removed from the mirror.  Then, if desired by the
 * user, the devices are replaced.
 *
 * 'old_mimage_count' and 'old_log_count' are there so we know
 * what to convert to after the removal of devices.
 */
static int _lvconvert_mirrors_repair(struct cmd_context *cmd,
				     struct logical_volume *lv,
				     struct lvconvert_params *lp,
				     struct dm_list *pvh)
{
	int failed_logs;
	int failed_mimages;
	int replace_logs = 0;
	int replace_mimages = 0;
	uint32_t log_count;

	uint32_t original_mimages = lv_mirror_count(lv);
	uint32_t original_logs = _get_log_count(lv);

	cmd->partial_activation = 1;
	lp->need_polling = 0;

	lv_check_transient(lv); /* TODO check this in lib for all commands? */

	if (!lv_is_partial(lv)) {
		log_print_unless_silent("Volume %s is consistent. Nothing to repair.",
					display_lvname(lv));
		return 1;
	}

	failed_mimages = _failed_mirrors_count(lv);
	failed_logs = _failed_logs_count(lv);

	/* Retain existing region size in case we need it later */
	if (!lp->region_size)
		lp->region_size = first_seg(lv)->region_size;

	if (!mirror_remove_missing(cmd, lv, 0))
		return_0;

	if (failed_mimages)
		log_print_unless_silent("Mirror status: %d of %d images failed.",
					failed_mimages, original_mimages);

	/*
	 * Count the failed log devices
	 */
	if (failed_logs)
		log_print_unless_silent("Mirror log status: %d of %d images failed.",
					failed_logs, original_logs);

	/*
	 * Find out our policies
	 */
	_lvconvert_mirrors_repair_ask(cmd, failed_logs, failed_mimages,
				      &replace_logs, &replace_mimages);

	/*
	 * Second phase - replace faulty devices
	 */
	lp->mirrors = replace_mimages ? original_mimages : (original_mimages - failed_mimages);

	/*
	 * It does not make sense to replace the log if the volume is no longer
	 * a mirror.
	 */
	if (lp->mirrors == 1)
		replace_logs = 0;

	log_count = replace_logs ? original_logs : (original_logs - failed_logs);

	while (replace_mimages || replace_logs) {
		log_warn("WARNING: Trying to up-convert to %d images, %d logs.", lp->mirrors, log_count);
		if (_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
					   lp->mirrors, log_count, pvh))
			break;
		if (lp->mirrors > 2)
			--lp->mirrors;
		else if (log_count > 0)
			--log_count;
		else
			break; /* nowhere to go, anymore... */
	}

	if (replace_mimages && lv_mirror_count(lv) != original_mimages)
		log_warn("WARNING: Failed to replace %d of %d images in volume %s.",
			 original_mimages - lv_mirror_count(lv), original_mimages,
			 display_lvname(lv));
	if (replace_logs && _get_log_count(lv) != original_logs)
		log_warn("WARNING: Failed to replace %d of %d logs in volume %s.",
			 original_logs - _get_log_count(lv), original_logs,
			 display_lvname(lv));

	/* if (!arg_is_set(cmd, use_policies_ARG) && (lp->mirrors != old_mimage_count
						  || log_count != old_log_count))
						  return 0; */

	return 1;
}

static int _lvconvert_validate_thin(struct logical_volume *lv,
				    struct lvconvert_params *lp)
{
	if (!lv_is_thin_pool(lv) && !lv_is_thin_volume(lv))
		return 1;

	log_error("Converting thin%s segment type for %s to %s is not supported.",
		  lv_is_thin_pool(lv) ? " pool" : "",
		  display_lvname(lv), lp->segtype->name);

	if (lv_is_thin_volume(lv))
		return 0;

	/* Give advice for thin pool conversion */
	log_error("For pool data volume conversion use %s.",
		  display_lvname(seg_lv(first_seg(lv), 0)));
	log_error("For pool metadata volume conversion use %s.",
		  display_lvname(first_seg(lv)->metadata_lv));

	return 0;
}

/* Check for raid1 split trackchanges image to reject conversions on it. */
static int _raid_split_image_conversion(struct logical_volume *lv)
{
	const char *s;
	char raidlv_name[NAME_LEN];
	const struct logical_volume *tmp_lv;

	if (lv_is_raid_with_tracking(lv)) {
		log_error("Conversion of tracking raid1 LV %s is not supported.",
			  display_lvname(lv));
		return 0;
	}

	if (lv_is_raid_image(lv) &&
	    (s = strstr(lv->name, "_rimage_"))) {
		(void) dm_strncpy(raidlv_name, lv->name, s - lv->name);

		if (!(tmp_lv = find_lv(lv->vg, raidlv_name))) {
			log_error("Failed to find RaidLV of RAID subvolume %s.",
				  display_lvname(lv));
			return 0;
		}

		if (lv_is_raid_with_tracking(tmp_lv)) {
			log_error("Conversion of tracked raid1 subvolume %s is not supported.",
				  display_lvname(lv));
			return 0;
		}
	}

	return 1;
}

/*
 * _lvconvert_mirrors
 *
 * Determine what is being done.  Are we doing a conversion, repair, or
 * collapsing a stack?  Once determined, call helper functions.
 */
static int _lvconvert_mirrors(struct cmd_context *cmd,
			      struct logical_volume *lv,
			      struct lvconvert_params *lp)
{
	uint32_t old_mimage_count = 0;
	uint32_t old_log_count = 0;
	uint32_t new_mimage_count = 0;
	uint32_t new_log_count = 0;

	if (!_raid_split_image_conversion(lv))
		return_0;

	if ((lp->corelog || lp->mirrorlog) && *lp->type_str && strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) {
		log_error("--corelog and --mirrorlog are only compatible with mirror devices.");
		return 0;
	}

	if (!_lvconvert_validate_thin(lv, lp))
		return_0;

	if (lv_is_thin_type(lv)) {
		log_error("Mirror segment type cannot be used for thinpool%s.\n"
			  "Try \"%s\" segment type instead.",
			  lv_is_thin_pool_data(lv) ? "s" : " metadata",
			  SEG_TYPE_NAME_RAID1);
		return 0;
	}

	if (lv_is_cache_type(lv)) {
		log_error("Mirrors are not yet supported on cache LVs %s.",
			  display_lvname(lv));
		return 0;
	}

	if (_linear_type_requested(lp->type_str)) {
		if (arg_is_set(cmd, mirrors_ARG) && (arg_uint_value(cmd, mirrors_ARG, 0) != 0)) {
			log_error("Cannot specify mirrors with linear type.");
			return 0;
		}
		lp->mirrors_supplied = 1;
		lp->mirrors = 0;
	}

	/* Adjust mimage and/or log count */
	if (!_lvconvert_mirrors_parse_params(cmd, lv, lp,
					     &old_mimage_count, &old_log_count,
					     &new_mimage_count, &new_log_count))
		return_0;

	if (((old_mimage_count < new_mimage_count && old_log_count > new_log_count) ||
	     (old_mimage_count > new_mimage_count && old_log_count < new_log_count)) &&
	    lp->pv_count) {
		log_error("Cannot both allocate and free extents when "
			  "specifying physical volumes to use.");
		log_error("Please specify the operation in two steps.");
		return 0;
	}

	/* Nothing to do?  (Probably finishing collapse.) */
	if ((old_mimage_count == new_mimage_count) &&
	    (old_log_count == new_log_count))
		return 1;

	if (!_lvconvert_mirrors_aux(cmd, lv, lp, NULL,
				    new_mimage_count, new_log_count, lp->pvh))
		return_0;

	if (!lp->need_polling)
		log_print_unless_silent("Logical volume %s converted.",
					display_lvname(lv));
	else
		log_print_unless_silent("Logical volume %s being converted.",
					display_lvname(lv));

	return 1;
}

static int _is_valid_raid_conversion(const struct segment_type *from_segtype,
				    const struct segment_type *to_segtype)
{
	if (!from_segtype)
		return 1;

	/* linear/striped/raid0 <-> striped/raid0/linear (restriping via raid) */
	if (segtype_is_striped(from_segtype) && segtype_is_striped(to_segtype))
		return 0;

	if (from_segtype == to_segtype)
		return 1;

	if (!segtype_is_raid(from_segtype) && !segtype_is_raid(to_segtype))
		return_0;  /* Not converting to or from RAID? */

	return 1;
}

/* Check for dm-raid target supporting raid4 conversion properly. */
static int _raid4_conversion_supported(struct logical_volume *lv, struct lvconvert_params *lp)
{
	int ret = 1;
	struct lv_segment *seg = first_seg(lv);

	if (seg_is_raid4(seg))
		ret = raid4_is_supported(lv->vg->cmd, seg->segtype);
	else if (segtype_is_raid4(lp->segtype))
		ret = raid4_is_supported(lv->vg->cmd, lp->segtype);

	if (ret)
		return 1;

	log_error("Cannot convert %s LV %s to %s.",
		  lvseg_name(seg), display_lvname(lv), lp->segtype->name);
	return 0;
}

static int _lvconvert_raid(struct logical_volume *lv, struct lvconvert_params *lp)
{
	int image_count = 0;
	int images_reduced = 0;
	int type_enforced = 0;
	struct cmd_context *cmd = lv->vg->cmd;
	struct lv_segment *seg = first_seg(lv);

	if (!_raid_split_image_conversion(lv))
		return_0;

	if (_linear_type_requested(lp->type_str)) {
		if (arg_is_set(cmd, mirrors_ARG) && (arg_uint_value(cmd, mirrors_ARG, 0) != 0)) {
			log_error("Cannot specify mirrors with linear type.");
			return 0;
		}
		lp->mirrors_supplied = 1;
		lp->mirrors = 0;
	}

	if (!_lvconvert_validate_thin(lv, lp))
		return_0;

	if (!_is_valid_raid_conversion(seg->segtype, lp->segtype) &&
	    !lp->mirrors_supplied)
		goto try_new_takeover_or_reshape;

	if (seg_is_striped(seg) && !lp->mirrors_supplied)
		goto try_new_takeover_or_reshape;

	if (seg_is_linear(seg) && !lp->mirrors_supplied)
		goto try_new_takeover_or_reshape;

	/* Change number of RAID1 images */
	if (lp->mirrors_supplied || lp->keep_mimages) {
		image_count = lv_raid_image_count(lv);
		if (lp->mirrors_sign == SIGN_PLUS)
			image_count += lp->mirrors;
		else if (lp->mirrors_sign == SIGN_MINUS)
			image_count -= lp->mirrors;
		else
			image_count = lp->mirrors + 1;

		images_reduced = (image_count < lv_raid_image_count(lv));

		if (image_count < 1) {
			log_error("Unable to %s images by specified amount.",
				  lp->keep_mimages ? "split" : "reduce");
			return 0;
		}

		/* --trackchanges requires --splitmirrors which always has SIGN_MINUS */
		if (lp->track_changes && lp->mirrors != 1) {
			log_error("Exactly one image must be split off from %s when tracking changes.",
				  display_lvname(lv));
			return 0;
		}

		if (!*lp->type_str) {
			lp->type_str = SEG_TYPE_NAME_RAID1;
			if (!(lp->segtype = get_segtype_from_string(lv->vg->cmd, SEG_TYPE_NAME_RAID1)))
				return_0;
			type_enforced = 1;
		}
	}

	if ((lp->corelog || lp->mirrorlog) && strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) {
		log_error("--corelog and --mirrorlog are only compatible with mirror devices");
		return 0;
	}

	if (lp->track_changes)
		return lv_raid_split_and_track(lv, lp->yes, lp->pvh);

	if (lp->keep_mimages)
		return lv_raid_split(lv, lp->yes, lp->lv_split_name, image_count, lp->pvh);

	if (lp->mirrors_supplied) {
		if (seg_is_linear(seg) || seg_is_raid1(seg)) { /* ??? */
		if (!*lp->type_str || !strcmp(lp->type_str, SEG_TYPE_NAME_RAID1) || !strcmp(lp->type_str, SEG_TYPE_NAME_LINEAR) ||
		    (!strcmp(lp->type_str, SEG_TYPE_NAME_STRIPED) && image_count == 1)) {
			if (image_count > DEFAULT_RAID1_MAX_IMAGES) {
				log_error("Only up to %u mirrors in %s LV %s supported currently.",
					  DEFAULT_RAID1_MAX_IMAGES, lp->segtype->name, display_lvname(lv));
				return 0;
			}
			if (!seg_is_raid1(seg) && lv_raid_has_integrity(lv)) {
				log_error("Cannot add raid images with integrity for this raid level.");
				return 0;
			}
			if (!lv_raid_change_image_count(lv, lp->yes, image_count,
							(lp->region_size_supplied || !seg->region_size) ?
							lp->region_size : seg->region_size , lp->pvh))
				return_0;

			if (lv_raid_has_integrity(lv) && !images_reduced) {
				struct integrity_settings *isettings = NULL;
				if (!lv_get_raid_integrity_settings(lv, &isettings))
					return_0;
				if (!lv_add_integrity_to_raid(lv, isettings, lp->pvh, NULL))
					return_0;
			}

			log_print_unless_silent("Logical volume %s successfully converted.",
						display_lvname(lv));

			return 1;
		}
		}
		goto try_new_takeover_or_reshape;
	}

	if ((seg_is_linear(seg) || seg_is_striped(seg) || seg_is_mirrored(seg) || lv_is_raid(lv)) &&
	    (lp->type_str && lp->type_str[0])) {
		/* Activation is required later which precludes existing supported raid0 segment */
		if ((seg_is_any_raid0(seg) || segtype_is_any_raid0(lp->segtype)) &&
		    !(lp->target_attr & RAID_FEATURE_RAID0)) {
			log_error("RAID module does not support RAID0.");
			return 0;
		}

		/* Activation is required later which precludes existing supported raid4 segment */
		if (!_raid4_conversion_supported(lv, lp))
			return_0;

		/* Activation is required later which precludes existing supported raid10 segment */
		if ((seg_is_raid10(seg) || segtype_is_raid10(lp->segtype)) &&
		    !(lp->target_attr & RAID_FEATURE_RAID10)) {
			log_error("RAID module does not support RAID10.");
			return 0;
		}

		if (lv_raid_has_integrity(lv)) {
			/* FIXME: which conversions are happening here? */
			log_error("This conversion is not supported for raid with integrity.");
			return 0;
		}

		/* FIXME This needs changing globally. */
		if (!arg_is_set(cmd, stripes_long_ARG))
			lp->stripes = 0;
		if (!type_enforced && !arg_is_set(cmd, type_ARG))
		       lp->segtype = NULL;
		if (!arg_is_set(cmd, regionsize_ARG))
		       lp->region_size = 0;

		if (!lv_raid_convert(lv, lp->segtype,
				     lp->yes, lp->force, lp->stripes, lp->stripe_size_supplied, lp->stripe_size,
				     lp->region_size, lp->pvh))
			return_0;

		log_print_unless_silent("Logical volume %s successfully converted.",
					display_lvname(lv));
		return 1;
	}

try_new_takeover_or_reshape:
	if (lv_raid_has_integrity(lv)) {
		/* FIXME: which conversions are happening here? */
		log_error("This conversion is not supported for raid with integrity.");
		return 0;
	}

	if (!_raid4_conversion_supported(lv, lp))
		return 0;

	/* FIXME This needs changing globally. */
	if (!arg_is_set(cmd, stripes_long_ARG))
		lp->stripes = 0;
	if (!type_enforced && !arg_is_set(cmd, type_ARG))
	       lp->segtype = NULL;

	if (!lv_raid_convert(lv, lp->segtype,
			     lp->yes, lp->force, lp->stripes, lp->stripe_size_supplied, lp->stripe_size,
			     (lp->region_size_supplied || !seg->region_size) ?
			     lp->region_size : seg->region_size , lp->pvh))
		return_0;

	log_print_unless_silent("Logical volume %s successfully converted.",
				display_lvname(lv));
	return 1;
}

/*
 * Functions called to perform a specific operation on a specific LV type.
 *
 * _convert_<lvtype>_<operation>
 *
 * For cases where an operation does not apply to the LV itself, but
 * is implicitly redirected to a sub-LV, these functions locate the
 * correct sub-LV and call the operation on that sub-LV.  If a sub-LV
 * of the proper type is not found, these functions report the error.
 *
 * FIXME: the _lvconvert_foo() functions can be cleaned up since they
 * are now only called for valid combinations of LV type and operation.
 * After that happens, the code remaining in those functions can be
 * moved into the _convert_lvtype_operation() functions below.
 */

/*
 * Change the number of images in a mirror LV.
 * lvconvert --mirrors Number LV
 */
static int _convert_mirror_number(struct cmd_context *cmd, struct logical_volume *lv,
				  struct lvconvert_params *lp)
{
	return _lvconvert_mirrors(cmd, lv, lp);
}

/*
 * Split images from a mirror LV and use them to create a new LV.
 * lvconvert --splitmirrors Number LV
 *
 * Required options:
 * --name Name
 */

static int _convert_mirror_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv,
					struct lvconvert_params *lp)
{
	return _lvconvert_mirrors(cmd, lv, lp);
}

/*
 * Change the type of log used by a mirror LV.
 * lvconvert --mirrorlog Type LV
 */
static int _convert_mirror_log(struct cmd_context *cmd, struct logical_volume *lv,
				  struct lvconvert_params *lp)
{
	return _lvconvert_mirrors(cmd, lv, lp);
}

/*
 * Convert mirror LV to linear LV.
 * lvconvert --type linear LV
 *
 * Alternate syntax:
 * lvconvert --mirrors 0 LV
 */
static int _convert_mirror_linear(struct cmd_context *cmd, struct logical_volume *lv,
				  struct lvconvert_params *lp)
{
	return _lvconvert_mirrors(cmd, lv, lp);
}

/*
 * Convert mirror LV to raid1 LV.
 * lvconvert --type raid1 LV
 */
static int _convert_mirror_raid(struct cmd_context *cmd, struct logical_volume *lv,
				struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Change the number of images in a raid1 LV.
 * lvconvert --mirrors Number LV
 */
static int _convert_raid_number(struct cmd_context *cmd, struct logical_volume *lv,
				struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Split images from a raid1 LV and use them to create a new LV.
 * lvconvert --splitmirrors Number LV
 *
 * Required options:
 * --trackchanges | --name Name
 */
static int _convert_raid_splitmirrors(struct cmd_context *cmd, struct logical_volume *lv,
				      struct lvconvert_params *lp)
{
	/* FIXME: split the splitmirrors section out of _lvconvert_raid and call it here. */
	return _lvconvert_raid(lv, lp);
}

/*
 * Convert a raid* LV to use a different raid level.
 * lvconvert --type raid* LV
 */
static int _convert_raid_raid(struct cmd_context *cmd, struct logical_volume *lv,
			      struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Convert a raid* LV to a mirror LV.
 * lvconvert --type mirror LV
 */
static int _convert_raid_mirror(struct cmd_context *cmd, struct logical_volume *lv,
			      struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Convert a raid* LV to a striped LV.
 * lvconvert --type striped LV
 */
static int _convert_raid_striped(struct cmd_context *cmd, struct logical_volume *lv,
				 struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Convert a raid* LV to a linear LV.
 * lvconvert --type linear LV
 */
static int _convert_raid_linear(struct cmd_context *cmd, struct logical_volume *lv,
				struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

/*
 * Convert a striped/linear LV to a mirror LV.
 * lvconvert --type mirror LV
 *
 * Required options:
 * --mirrors Number
 *
 * Alternate syntax:
 * This is equivalent to above when global/mirror_segtype_default="mirror".
 * lvconvert --mirrors Number LV
 */
static int _convert_striped_mirror(struct cmd_context *cmd, struct logical_volume *lv,
				   struct lvconvert_params *lp)
{
	return _lvconvert_mirrors(cmd, lv, lp);
}

/*
 * Convert a striped/linear LV to a raid* LV.
 * lvconvert --type raid* LV
 *
 * Required options:
 * --mirrors Number
 *
 * Alternate syntax:
 * This is equivalent to above when global/mirror_segtype_default="raid1".
 * lvconvert --mirrors Number LV
 */
static int _convert_striped_raid(struct cmd_context *cmd, struct logical_volume *lv,
				 struct lvconvert_params *lp)
{
	return _lvconvert_raid(lv, lp);
}

static int _convert_mirror(struct cmd_context *cmd, struct logical_volume *lv,
			   struct lvconvert_params *lp)
{
	if (arg_is_set(cmd, mirrors_ARG))
		return _convert_mirror_number(cmd, lv, lp);

	if (arg_is_set(cmd, splitmirrors_ARG))
		return _convert_mirror_splitmirrors(cmd, lv, lp);

	if (arg_is_set(cmd, mirrorlog_ARG) || arg_is_set(cmd, corelog_ARG))
		return _convert_mirror_log(cmd, lv, lp);

	if (_linear_type_requested(lp->type_str))
		return _convert_mirror_linear(cmd, lv, lp);

	if (segtype_is_raid(lp->segtype))
		return _convert_mirror_raid(cmd, lv, lp);

	log_error("Unknown operation on mirror LV %s.", display_lvname(lv));
	return 0;
}

static int _convert_raid(struct cmd_context *cmd, struct logical_volume *lv,
			 struct lvconvert_params *lp)
{
	if (arg_is_set(cmd, mirrors_ARG))
		return _convert_raid_number(cmd, lv, lp);

	if (arg_is_set(cmd, splitmirrors_ARG))
		return _convert_raid_splitmirrors(cmd, lv, lp);

	if (segtype_is_raid(lp->segtype))
		return _convert_raid_raid(cmd, lv, lp);

	if (segtype_is_mirror(lp->segtype))
		return _convert_raid_mirror(cmd, lv, lp);

	if (!strcmp(lp->type_str, SEG_TYPE_NAME_STRIPED))
		return _convert_raid_striped(cmd, lv, lp);

	if (_linear_type_requested(lp->type_str))
		return _convert_raid_linear(cmd, lv, lp);

	log_error("Unknown operation on raid LV %s.", display_lvname(lv));
	return 0;
}

static int _convert_striped(struct cmd_context *cmd, struct logical_volume *lv,
			    struct lvconvert_params *lp)
{
	const char *mirrors_type = find_config_tree_str(cmd, global_mirror_segtype_default_CFG, NULL);
	int raid_type = *lp->type_str && !strncmp(lp->type_str, "raid", 4);

	if (!raid_type) {
		if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR))
			return _convert_striped_mirror(cmd, lv, lp);

		/* --mirrors can mean --type mirror or --type raid1 depending on config setting. */

		if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_MIRROR))
			return _convert_striped_mirror(cmd, lv, lp);
	}

	if (arg_is_set(cmd, mirrors_ARG) && mirrors_type && !strcmp(mirrors_type, SEG_TYPE_NAME_RAID1))
		return _convert_striped_raid(cmd, lv, lp);

	if (segtype_is_striped(lp->segtype) || segtype_is_raid(lp->segtype))
		return _convert_striped_raid(cmd, lv, lp);

	log_error("Unknown operation on striped or linear LV %s.", display_lvname(lv));
	return 0;
}

static int _lvconvert_raid_types(struct cmd_context *cmd, struct logical_volume *lv,
				 struct lvconvert_params *lp)
{
	struct lv_segment *seg = first_seg(lv);
	int ret = 0;

	/* If LV is inactive here, ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		return_ECMD_FAILED;

	/* Set up segtype either from type_str or else to match the existing one. */
	if (!*lp->type_str)
		lp->segtype = seg->segtype;
	else if (!(lp->segtype = get_segtype_from_string(cmd, lp->type_str)))
		goto_out;

	if (!strcmp(lp->type_str, SEG_TYPE_NAME_MIRROR)) {
		if (!lp->mirrors_supplied && !seg_is_raid1(seg)) {
			log_error("Conversions to --type mirror require -m/--mirrors");
			goto out;
		}
	}

	/* lv->segtype can't be NULL */
	if (activation() && lp->segtype->ops->target_present &&
	    !lp->segtype->ops->target_present(cmd, NULL, &lp->target_attr)) {
		log_error("%s: Required device-mapper target(s) not "
			  "detected in your kernel.", lp->segtype->name);
		goto out;
	}

	/* Process striping parameters */
	/* FIXME This is incomplete */
	if (_mirror_or_raid_type_requested(cmd, lp->type_str) || _raid0_type_requested(lp->type_str) ||
	    _striped_type_requested(lp->type_str) || lp->mirrorlog || lp->corelog) {
		if (!arg_is_set(cmd, type_ARG))
			lp->segtype = first_seg(lv)->segtype;
		/* FIXME Handle +/- adjustments too? */
		if (!get_stripe_params(cmd, lp->segtype, &lp->stripes, &lp->stripe_size, &lp->stripes_supplied, &lp->stripe_size_supplied))
			goto_out;

		if (_raid0_type_requested(lp->type_str) || _striped_type_requested(lp->type_str))
			/* FIXME Shouldn't need to override get_stripe_params which defaults to 1 stripe (i.e. linear)! */
			/* The default keeps existing number of stripes, handled inside the library code */
			if (!arg_is_set(cmd, stripes_long_ARG))
				lp->stripes = 0;
	}

	if (lv_is_cache(lv))
		lv = seg_lv(first_seg(lv), 0);

	if (lv_is_vdo_pool(lv))
		return _lvconvert_raid_types(cmd, seg_lv(first_seg(lv), 0), lp);

	if (lv_is_mirror(lv)) {
		ret = _convert_mirror(cmd, lv, lp);
		goto out;
	}

	if (lv_is_raid(lv)) {
		ret = _convert_raid(cmd, lv, lp);
		goto out;
	}

	/*
	 * FIXME: add lv_is_striped() and lv_is_linear()?
	 * This does not include raid0 which is caught by the test above.
	 * If operations differ between striped and linear, split this case.
	 */
	if (segtype_is_striped(seg->segtype) || segtype_is_linear(seg->segtype)) {
		ret = _convert_striped(cmd, lv, lp);
		goto out;
	}

	/*
	 * The intention is to explicitly check all cases above and never
	 * reach here, but this covers anything that was missed.
	 */
	log_error("Cannot convert LV %s.", display_lvname(lv));

out:
	return ret ? ECMD_PROCESSED : ECMD_FAILED;
}

static int _lvconvert_splitsnapshot(struct cmd_context *cmd, struct logical_volume *cow)
{
	struct volume_group *vg = cow->vg;
	const char *cow_name = display_lvname(cow);

	if (!lv_is_cow(cow)) {
		log_error(INTERNAL_ERROR "Volume %s is not a COW.", cow_name);
		return 0;
	}

	if (lv_is_virtual_origin(origin_from_cow(cow))) {
		log_error("Unable to split off snapshot %s with virtual origin.", cow_name);
		return 0;
	}

	if (vg_is_shared(vg)) {
		/* FIXME: we need to create a lock for the new LV. */
		log_error("Unable to split snapshots in VG with lock_type %s.", vg->lock_type);
		return 0;
	}

	if (lv_is_active(cow)) {
		if (!lv_check_not_in_use(cow, 1))
			return_0;

		if ((arg_count(cmd, force_ARG) == PROMPT) &&
		    !arg_count(cmd, yes_ARG) &&
		    lv_is_visible(cow) &&
		    lv_is_active(cow)) {
			if (yes_no_prompt("Do you really want to split off active "
					  "logical volume %s? [y/n]: ", display_lvname(cow)) == 'n') {
				log_error("Logical volume %s not split.", display_lvname(cow));
				return 0;
			}
		}
	}

	log_verbose("Splitting snapshot %s from its origin.", display_lvname(cow));

	if (!vg_remove_snapshot(cow))
		return_0;

	log_print_unless_silent("Logical Volume %s split from its origin.", display_lvname(cow));

	return 1;
}

static int _lvconvert_split_and_keep_cachevol(struct cmd_context *cmd,
				   struct logical_volume *lv,
				   struct logical_volume *lv_fast)
{
	char cvol_name[NAME_LEN];
	struct lv_segment *cache_seg = first_seg(lv);
	int cache_mode = cache_seg->cache_mode;
	int direct_detach = 0;

	if (!archive(lv->vg))
		return_0;

	log_debug("Detaching cachevol %s from LV %s.", display_lvname(lv_fast), display_lvname(lv));

	/*
	 * Allow forcible detach without activating or flushing
	 * in case the cache is corrupt/damaged/invalid.
	 * This would generally be done to rescue data from
	 * the origin if the cache could not be repaired.
	 */
	if (!lv_is_active(lv) && arg_count(cmd, force_ARG))
		direct_detach = 1;

	/*
	 * Detaching a writeback cache generally requires flushing;
	 * doing otherwise can mean data loss/corruption.
	 * If the cache devices are missing, the cache can't be
	 * flushed, so require the user to use a force option to
	 * detach the cache in this case.
	 */
	if ((cache_mode != CACHE_MODE_WRITETHROUGH) && lv_is_partial(lv_fast)) {
		if (!arg_count(cmd, force_ARG)) {
			log_warn("WARNING: writeback cache on %s is not complete and cannot be flushed.", display_lvname(lv_fast));
			log_warn("WARNING: cannot detach writeback cache from %s without --force.", display_lvname(lv));
			log_error("Conversion aborted.");
			return 0;
		}
		direct_detach = 1;
	}

	if (direct_detach) {
		log_warn("WARNING: Data may be lost by detaching writeback cache without flushing.");

		if (!arg_count(cmd, yes_ARG) &&
		    yes_no_prompt("Detach writeback cache %s from %s without flushing data?",
				  display_lvname(lv_fast), display_lvname(lv)) == 'n') {
			log_error("Conversion aborted.");
			return 0;
		}

		/* Switch internally to WRITETHROUGH which does not require flushing */
		cache_seg->cache_mode = CACHE_MODE_WRITETHROUGH;
	}

	if (!lv_cache_remove(lv))
		return_0;

	/* Cut off suffix _cvol */
	if (!drop_lvname_suffix(cvol_name, lv_fast->name, "cvol")) {
		/* likely older instance of metadata */
		log_debug("LV %s has no suffix for cachevol (skipping rename).",
			  display_lvname(lv_fast));
	} else if (!lv_uniq_rename_update(cmd, lv_fast, cvol_name, 0))
		return_0;

	if (!vg_write(lv->vg) || !vg_commit(lv->vg))
		return_0;

	return 1;
}

static int _lvconvert_split_and_remove_cachevol(struct cmd_context *cmd,
				   struct logical_volume *lv,
				   struct logical_volume *lv_fast)
{
	if (!_lvconvert_split_and_keep_cachevol(cmd, lv, lv_fast))
		return_0;

	if (lvremove_single(cmd, lv_fast, NULL) != ECMD_PROCESSED)
		return_0;

	return 1;
}

static int _lvconvert_split_and_keep_cachepool(struct cmd_context *cmd,
				   struct logical_volume *lv,
				   struct logical_volume *lv_fast)
{
	char name[NAME_LEN];

	if (!archive(lv->vg))
		return_0;

	log_debug("Detaching cachepool %s from LV %s.", display_lvname(lv_fast), display_lvname(lv));

	if (vg_missing_pv_count(lv->vg)) {
		log_error("Cannot split cache pool while PVs are missing, see --uncache to delete cache pool.");
		return 0;
	}

	if (!lv_cache_remove(lv))
		return_0;

	/* Cut off suffix _cpool */
	if (!drop_lvname_suffix(name, lv_fast->name, "cpool")) {
		/* likely older instance of metadata */
		log_debug("LV %s has no suffix for cachepool (skipping rename).",
			  display_lvname(lv_fast));
	} else if (!lv_uniq_rename_update(cmd, lv_fast, name, 0))
		return_0;

	if (!vg_write(lv->vg) || !vg_commit(lv->vg))
		return_0;

	log_print_unless_silent("Logical volume %s is not cached and %s is unused.",
				display_lvname(lv), display_lvname(lv_fast));

	return 1;
}

static int _lvconvert_split_and_remove_cachepool(struct cmd_context *cmd,
				   struct logical_volume *lv,
				   struct logical_volume *cachepool_lv)
{
	struct lv_segment *seg;
	struct logical_volume *remove_lv;

	seg = first_seg(lv);

	if (lv_is_partial(seg_lv(seg, 0))) {
		log_warn("WARNING: Cache origin logical volume %s is missing.",
			 display_lvname(seg_lv(seg, 0)));
		remove_lv = lv; /* When origin is missing, drop everything */
	} else
		remove_lv = seg->pool_lv;

	if (lv_is_partial(seg_lv(first_seg(seg->pool_lv), 0)))
		log_warn("WARNING: Cache pool data logical volume %s is missing.",
			 display_lvname(seg_lv(first_seg(seg->pool_lv), 0)));

	if (lv_is_partial(first_seg(seg->pool_lv)->metadata_lv))
		log_warn("WARNING: Cache pool metadata logical volume %s is missing.",
			 display_lvname(first_seg(seg->pool_lv)->metadata_lv));

	/* TODO: Check for failed cache as well to get prompting? */
	if (lv_is_partial(lv)) {
		if (first_seg(seg->pool_lv)->cache_mode != CACHE_MODE_WRITETHROUGH) {
			if (!arg_count(cmd, force_ARG)) {
				log_error("Conversion aborted.");
				log_error("Cannot uncache writeback cache volume %s without --force.",
					  display_lvname(lv));
				return 0;
			}
			log_warn("WARNING: Uncaching of partially missing %s cache volume %s might destroy your data.",
				 cache_mode_num_to_str(first_seg(seg->pool_lv)->cache_mode), display_lvname(lv));
		}

		if (!arg_count(cmd, yes_ARG) &&
		    yes_no_prompt("Do you really want to uncache %s with missing LVs? [y/n]: ",
				  display_lvname(lv)) == 'n') {
			log_error("Conversion aborted.");
			return 0;
		}
	}

	if (lvremove_single(cmd, remove_lv, NULL) != ECMD_PROCESSED)
		return_0;

	if (remove_lv != lv)
		log_print_unless_silent("Logical volume %s is not cached.", display_lvname(lv));

	return 1;
}

static int _lvconvert_snapshot(struct cmd_context *cmd,
			       struct logical_volume *lv,
			       const char *origin_name)
{
	struct logical_volume *org;
	const char *snap_name = display_lvname(lv);
	uint32_t chunk_size;
	int zero;

	if (strcmp(lv->name, origin_name) == 0) {
		log_error("Unable to use %s as both snapshot and origin.", snap_name);
		return 0;
	}

	chunk_size = arg_uint_value(cmd, chunksize_ARG, 8);
	if (chunk_size < 8 || chunk_size > 1024 || !is_power_of_2(chunk_size)) {
		log_error("Chunk size must be a power of 2 in the range 4K to 512K.");
		return 0;
	}

	if (!cow_has_min_chunks(lv->vg, lv->le_count, chunk_size))
		return_0;

	log_verbose("Setting chunk size to %s.", display_size(cmd, chunk_size));

	if (!(org = find_lv(lv->vg, origin_name))) {
		log_error("Couldn't find origin volume %s in Volume group %s.",
			  origin_name, lv->vg->name);
		return 0;
	}

	/*
	 * check_lv_rules() checks cannot be done via command definition
	 * rules because this LV is not processed by process_each_lv.
	 */

	/*
	 * check_lv_types() checks cannot be done via command definition
	 * LV_foo specification because this LV is not processed by process_each_lv.
	 */
	if (!validate_snapshot_origin(org))
		return_0;

	if (lv_component_is_active(org)) {
		log_error("Cannot use logical volume %s with active component LVs for snapshot origin.",
			  display_lvname(org));
		return 0;
	}

	log_warn("WARNING: Converting logical volume %s to snapshot exception store.",
		 snap_name);
	log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)");

	if (!arg_count(cmd, yes_ARG) &&
	    yes_no_prompt("Do you really want to convert %s? [y/n]: ",
			  snap_name) == 'n') {
		log_error("Conversion aborted.");
		return 0;
	}

	if (!deactivate_lv(cmd, lv)) {
		log_error("Couldn't deactivate logical volume %s.", snap_name);
		return 0;
	}

	if (first_seg(lv)->segtype->flags & SEG_CANNOT_BE_ZEROED)
		zero = 0;
	else
		zero = arg_int_value(cmd, zero_ARG, 1);

	if (!zero || !(lv->status & LVM_WRITE))
		log_warn("WARNING: %s not zeroed.", snap_name);
	else if (!activate_and_wipe_lv(lv, 0)) {
		log_error("Aborting. Failed to wipe snapshot exception store.");
		return 0;
	}

	if (!archive(lv->vg))
		return_0;

	if (!vg_add_snapshot(org, lv, NULL, org->le_count, chunk_size)) {
		log_error("Couldn't create snapshot.");
		return 0;
	}

	/* store vg on disk(s) */
	if (!lv_update_and_reload(org))
		return_0;

	log_print_unless_silent("Logical volume %s converted to snapshot.", snap_name);

	return 1;
}

static int _lvconvert_merge_old_snapshot(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct logical_volume **lv_to_poll)
{
	int merge_on_activate = 0;
	struct logical_volume *origin;
	struct lv_segment *snap_seg = find_snapshot(lv);
	struct lvinfo info;
	dm_percent_t snap_percent;

	if (!snap_seg)
		return_0;

	origin = origin_from_cow(lv);

	/* Check if merge is possible */
	if (lv_is_merging_origin(origin)) {
		log_error("Cannot merge snapshot %s into the origin %s "
			  "with merging snapshot %s.",
			  display_lvname(lv), display_lvname(origin),
			  display_lvname(snap_seg->lv));
		return 0;
	}

	if (lv_is_external_origin(origin)) {
		log_error("Cannot merge snapshot %s into "
			  "the read-only external origin %s.",
			  display_lvname(lv), display_lvname(origin));
		return 0;
	}

	if (!(origin->status & LVM_WRITE)) {
		log_error("Cannot merge snapshot %s into "
			  "the read-only origin %s. (Use lvchange -p rw).",
			  display_lvname(lv), display_lvname(origin));
		return 0;
	}

	/* FIXME: test when snapshot is remotely active */
	if (lv_info(cmd, lv, 0, &info, 1, 0)
	    && info.exists && info.live_table &&
	    (!lv_snapshot_percent(lv, &snap_percent) ||
	     snap_percent == DM_PERCENT_INVALID)) {
		log_error("Unable to merge invalidated snapshot LV %s.",
			  display_lvname(lv));
		return 0;
	}

	if (snap_seg->segtype->ops->target_present &&
	    !snap_seg->segtype->ops->target_present(cmd, snap_seg, NULL)) {
		log_error("Can't initialize snapshot merge. "
			  "Missing support in kernel?");
		return 0;
	}

	if (!archive(lv->vg))
		return_0;

	/*
	 * Prevent merge with open device(s) as it would likely lead
	 * to application/filesystem failure.  Merge on origin's next
	 * activation if either the origin or snapshot LV are currently
	 * open.
	 *
	 * FIXME testing open_count is racey; snapshot-merge target's
	 * constructor and DM should prevent appropriate devices from
	 * being open.
	 */
	if (lv_is_active(origin)) {
		if (!lv_check_not_in_use(origin, 0)) {
			log_print_unless_silent("Delaying merge since origin is open.");
			merge_on_activate = 1;
		} else if (!lv_check_not_in_use(lv, 0)) {
			log_print_unless_silent("Delaying merge since snapshot is open.");
			merge_on_activate = 1;
		}
	}

	init_snapshot_merge(snap_seg, origin);

	if (merge_on_activate) {
		/* Store and commit vg but skip starting the merge */
		if (!vg_write(lv->vg) || !vg_commit(lv->vg))
			return_0;
	} else {
		/* Perform merge */
		if (!lv_update_and_reload(origin))
			return_0;

		if (!lv_has_target_type(origin->vg->vgmem, origin, NULL,
				       TARGET_NAME_SNAPSHOT_MERGE)) {
			/* Race during table reload prevented merging */
			merge_on_activate = 1;

		} else if (!lv_is_active(origin)) {
			log_print_unless_silent("Conversion starts after activation.");
			merge_on_activate = 1;
		} else {
			*lv_to_poll = origin;
		}
	}

	if (merge_on_activate)
		log_print_unless_silent("Merging of snapshot %s will occur on "
					"next activation of %s.",
					display_lvname(lv), display_lvname(origin));
	else
		log_print_unless_silent("Merging of volume %s started.",
					display_lvname(lv));

	return 1;
}

static int _lvconvert_merge_thin_snapshot(struct cmd_context *cmd,
					  struct logical_volume *lv)
{
	int origin_is_active = 0;
	struct lv_segment *snap_seg = first_seg(lv);
	struct logical_volume *origin = snap_seg->origin;

	if (!origin) {
		log_error("%s is not a mergeable logical volume.",
			  display_lvname(lv));
		return 0;
	}

	/* Check if merge is possible */
	if (lv_is_merging_origin(origin)) {
		log_error("Cannot merge snapshot %s into the origin %s "
			  "with merging snapshot %s.",
			  display_lvname(lv), display_lvname(origin),
			  display_lvname(find_snapshot(origin)->lv));
		return 0;
	}

	if (lv_is_external_origin(origin)) {
		if (!(origin = origin_from_cow(lv)))
			log_error(INTERNAL_ERROR "%s is missing origin.",
				  display_lvname(lv));
		else
			log_error("%s is read-only external origin %s.",
				  display_lvname(lv), display_lvname(origin));
		return 0;
	}

	if (lv_is_origin(origin)) {
		log_error("Merging into the old snapshot origin %s is not supported.",
			  display_lvname(origin));
		return 0;
	}

	if (!archive(lv->vg))
		return_0;

	/*
	 * Prevent merge with open device(s) as it would likely lead
	 * to application/filesystem failure.  Merge on origin's next
	 * activation if either the origin or snapshot LV can't be
	 * deactivated.
	 */
	if (!deactivate_lv(cmd, lv))
		log_print_unless_silent("Delaying merge since snapshot is open.");
	else if ((origin_is_active = lv_is_active(origin)) &&
		 !deactivate_lv(cmd, origin))
		log_print_unless_silent("Delaying merge since origin volume is open.");
	else {
		/*
		 * Both thin snapshot and origin are inactive,
		 * replace the origin LV with its snapshot LV.
		 */
		if (!thin_merge_finish(cmd, origin, lv))
			return_0;

		log_print_unless_silent("Volume %s replaced origin %s.",
					display_lvname(origin), display_lvname(lv));

		if (origin_is_active && !activate_lv(cmd, lv)) {
			log_error("Failed to reactivate origin %s.",
				  display_lvname(lv));
			return 0;
		}

		return 1;
	}

	init_snapshot_merge(snap_seg, origin);

	/* Commit vg, merge will start with next activation */
	if (!vg_write(lv->vg) || !vg_commit(lv->vg))
		return_0;

	log_print_unless_silent("Merging of thin snapshot %s will occur on "
				"next activation of %s.",
				display_lvname(lv), display_lvname(origin));
	return 1;
}

static int _lvconvert_thin_pool_repair(struct cmd_context *cmd,
				       struct logical_volume *pool_lv,
				       struct dm_list *pvh, int poolmetadataspare)
{
	const char *dmdir = dm_dir();
	const char *thin_dump =
		find_config_tree_str_allow_empty(cmd, global_thin_dump_executable_CFG, NULL);
	const char *thin_repair =
		find_config_tree_str_allow_empty(cmd, global_thin_repair_executable_CFG, NULL);
	const struct dm_config_node *cn;
	const struct dm_config_value *cv;
	int ret = 0, status;
	int args = 0;
	const char *argv[MAX_PDATA_ARGS + 7]; /* Max supported args */
	char *dm_name, *trans_id_str;
	char meta_path[PATH_MAX];
	char pms_path[PATH_MAX];
	uint64_t trans_id;
	struct logical_volume *pmslv;
	struct logical_volume *mlv = first_seg(pool_lv)->metadata_lv;
	struct pipe_data pdata;
	FILE *f;

	if (!thin_repair || !thin_repair[0]) {
		log_error("Thin repair commnand is not configured. Repair is disabled.");
		return 0; /* Checking disabled */
	}

	pmslv = pool_lv->vg->pool_metadata_spare_lv;

	/* Check we have pool metadata spare LV */
	if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, 1))
		return_0;

	if (pmslv != pool_lv->vg->pool_metadata_spare_lv) {
		if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg))
			return_0;
		pmslv = pool_lv->vg->pool_metadata_spare_lv;
	}

	if (!(dm_name = dm_build_dm_name(cmd->mem, mlv->vg->name,
					 mlv->name, NULL)) ||
	    (dm_snprintf(meta_path, sizeof(meta_path), "%s/%s", dmdir, dm_name) < 0)) {
		log_error("Failed to build thin metadata path.");
		return 0;
	}

	if (!(dm_name = dm_build_dm_name(cmd->mem, pmslv->vg->name,
					 pmslv->name, NULL)) ||
	    (dm_snprintf(pms_path, sizeof(pms_path), "%s/%s", dmdir, dm_name) < 0)) {
		log_error("Failed to build pool metadata spare path.");
		return 0;
	}

	if (!(cn = find_config_tree_array(cmd, global_thin_repair_options_CFG, NULL))) {
		log_error(INTERNAL_ERROR "Unable to find configuration for global/thin_repair_options");
		return 0;
	}

	for (cv = cn->v; cv && args < MAX_PDATA_ARGS; cv = cv->next) {
		if (cv->type != DM_CFG_STRING) {
			log_error("Invalid string in config file: "
				  "global/thin_repair_options");
			return 0;
		}
		argv[++args] = cv->v.str;
	}

	if (args >= MAX_PDATA_ARGS) {
		log_error("Too many options for thin repair command.");
		return 0;
	}

	argv[0] = thin_repair;
	argv[++args] = "-i";
	argv[++args] = meta_path;
	argv[++args] = "-o";
	argv[++args] = pms_path;
	argv[++args] = NULL;

	if (thin_pool_is_active(pool_lv)) {
		log_error("Active pools cannot be repaired.  Use lvchange -an first.");
		return 0;
	}

	if (!activate_lv(cmd, pmslv)) {
		log_error("Cannot activate pool metadata spare volume %s.",
			  pmslv->name);
		return 0;
	}

	if (!activate_lv(cmd, mlv)) {
		log_error("Cannot activate thin pool metadata volume %s.",
			  mlv->name);
		goto deactivate_pmslv;
	}

	if (!(ret = exec_cmd(cmd, (const char * const *)argv, &status, 1))) {
		log_error("Repair of thin metadata volume of thin pool %s failed (status:%d). "
			  "Manual repair required!",
			  display_lvname(pool_lv), status);
		goto deactivate_mlv;
	}

	/* Check matching transactionId when thin-pool is used by lvm2 (transactionId != 0) */
	if (first_seg(pool_lv)->transaction_id && thin_dump[0]) {
		argv[0] = thin_dump;
		argv[1] = pms_path;
		argv[2] = NULL;

		if (!(f = pipe_open(cmd, argv, 0, &pdata)))
			log_warn("WARNING: Cannot read output from %s %s.", thin_dump, pms_path);
		else {
			/*
			 * Scan only the 1st. line for transation id.
			 * Watch out, if the thin_dump format changes
			 */
			if (fgets(meta_path, sizeof(meta_path), f) &&
			    (trans_id_str = strstr(meta_path, "transaction=\"")) &&
			    (sscanf(trans_id_str + 13, FMTu64, &trans_id) == 1) &&
			    (trans_id != first_seg(pool_lv)->transaction_id) &&
			    ((trans_id - 1) != first_seg(pool_lv)->transaction_id))
				log_error("Transaction id " FMTu64 " from pool \"%s/%s\" "
					  "does not match repaired transaction id "
					  FMTu64 " from %s.",
					  first_seg(pool_lv)->transaction_id,
					  pool_lv->vg->name, pool_lv->name, trans_id,
					  pms_path);

			(void) pipe_close(&pdata); /* killing pipe */
		}
	}

deactivate_mlv:
	if (!deactivate_lv(cmd, mlv)) {
		log_error("Cannot deactivate thin pool metadata volume %s.",
			  display_lvname(mlv));
		ret = 0;
	}

deactivate_pmslv:
	if (!deactivate_lv(cmd, pmslv)) {
		log_error("Cannot deactivate pool metadata spare volume %s.",
			  display_lvname(pmslv));
		ret = 0;
	}

	if (!ret)
		return 0;

	if (pmslv == pool_lv->vg->pool_metadata_spare_lv) {
		pool_lv->vg->pool_metadata_spare_lv = NULL;
		pmslv->status &= ~POOL_METADATA_SPARE;
		lv_set_visible(pmslv);
	}

	/* Try to allocate new pool metadata spare LV */
	if (!handle_pool_metadata_spare(pool_lv->vg, 0, pvh, poolmetadataspare))
		stack;

	if (dm_snprintf(meta_path, sizeof(meta_path), "%s_meta%%d", pool_lv->name) < 0) {
		log_error("Can't prepare new metadata name for %s.", pool_lv->name);
		return 0;
	}

	if (!generate_lv_name(pool_lv->vg, meta_path, pms_path, sizeof(pms_path))) {
		log_error("Can't generate new name for %s.", meta_path);
		return 0;
	}

	if (!detach_pool_metadata_lv(first_seg(pool_lv), &mlv))
		return_0;

	/* Swap _pmspare and _tmeta name */
	if (!swap_lv_identifiers(cmd, mlv, pmslv))
		return_0;

	if (!attach_pool_metadata_lv(first_seg(pool_lv), pmslv))
		return_0;

	/* Used _tmeta (now _pmspare) becomes _meta%d */
	if (!lv_rename_update(cmd, mlv, pms_path, 0))
		return_0;

	if (!vg_write(pool_lv->vg) || !vg_commit(pool_lv->vg))
		return_0;

	log_warn("WARNING: LV %s holds a backup of the unrepaired metadata. Use lvremove when no longer required.",
		 display_lvname(mlv));

	if (dm_list_size(&pool_lv->vg->pvs) > 1)
		log_warn("WARNING: New metadata LV %s might use different PVs.  Move it with pvmove if required.",
			 display_lvname(first_seg(pool_lv)->metadata_lv));

	return 1;
}

/* TODO: lots of similar code with  thinpool repair
 *       investigate possible better code sharing...
 */
static int _lvconvert_cache_repair(struct cmd_context *cmd,
				   struct logical_volume *cache_lv,
				   struct dm_list *pvh, int poolmetadataspare)
{
	const char *dmdir = dm_dir();
	const char *cache_repair =
		find_config_tree_str_allow_empty(cmd, global_cache_repair_executable_CFG, NULL);
	const struct dm_config_node *cn;
	const struct dm_config_value *cv;
	int ret = 0, status;
	int args = 0;
	const char *argv[MAX_PDATA_ARGS + 7]; /* Max supported args */
	char *dm_name;
	char meta_path[PATH_MAX];
	char pms_path[PATH_MAX];
	struct logical_volume *pool_lv;
	struct logical_volume *pmslv;
	struct logical_volume *mlv;

	if (lv_is_cache(cache_lv) && lv_is_cache_vol(first_seg(cache_lv)->pool_lv)) {
		log_error("Manual repair required.");
		return 0;
	}

	pool_lv = lv_is_cache_pool(cache_lv) ? cache_lv : first_seg(cache_lv)->pool_lv;
	mlv = first_seg(pool_lv)->metadata_lv;

	if (!cache_repair || !cache_repair[0]) {
		log_error("Cache repair commnand is not configured. Repair is disabled.");
		return 0; /* Checking disabled */
	}

	pmslv = cache_lv->vg->pool_metadata_spare_lv;

	/* Check we have pool metadata spare LV */
	if (!handle_pool_metadata_spare(cache_lv->vg, 0, pvh, 1))
		return_0;

	if (pmslv != cache_lv->vg->pool_metadata_spare_lv) {
		if (!vg_write(cache_lv->vg) || !vg_commit(cache_lv->vg))
			return_0;
		pmslv = cache_lv->vg->pool_metadata_spare_lv;
	}

	if (!(dm_name = dm_build_dm_name(cmd->mem, mlv->vg->name,
					 mlv->name, NULL)) ||
	    (dm_snprintf(meta_path, sizeof(meta_path), "%s/%s", dmdir, dm_name) < 0)) {
		log_error("Failed to build cache metadata path.");
		return 0;
	}

	if (!(dm_name = dm_build_dm_name(cmd->mem, pmslv->vg->name,
					 pmslv->name, NULL)) ||
	    (dm_snprintf(pms_path, sizeof(pms_path), "%s/%s", dmdir, dm_name) < 0)) {
		log_error("Failed to build pool metadata spare path.");
		return 0;
	}

	if (!(cn = find_config_tree_array(cmd, global_cache_repair_options_CFG, NULL))) {
		log_error(INTERNAL_ERROR "Unable to find configuration for global/cache_repair_options");
		return 0;
	}

	for (cv = cn->v; cv && args < MAX_PDATA_ARGS; cv = cv->next) {
		if (cv->type != DM_CFG_STRING) {
			log_error("Invalid string in config file: "
				  "global/cache_repair_options");
			return 0;
		}
		argv[++args] = cv->v.str;
	}

	if (args >= MAX_PDATA_ARGS) {
		log_error("Too many options for cache repair command.");
		return 0;
	}

	argv[0] = cache_repair;
	argv[++args] = "-i";
	argv[++args] = meta_path;
	argv[++args] = "-o";
	argv[++args] = pms_path;
	argv[++args] = NULL;

	if (lv_is_active(cache_lv)) {
		log_error("Only inactive cache can be repaired.");
		return 0;
	}

	if (!activate_lv(cmd, pmslv)) {
		log_error("Cannot activate pool metadata spare volume %s.",
			  pmslv->name);
		return 0;
	}

	if (!activate_lv(cmd, mlv)) {
		log_error("Cannot activate cache pool metadata volume %s.",
			  mlv->name);
		goto deactivate_pmslv;
	}

	if (!(ret = exec_cmd(cmd, (const char * const *)argv, &status, 1))) {
		log_error("Repair of cache metadata volume of cache %s failed (status:%d). "
			  "Manual repair required!",
			  display_lvname(cache_lv), status);
		goto deactivate_mlv;
	}

	/* TODO: any active validation of cache-pool metadata? */

deactivate_mlv:
	if (!sync_local_dev_names(cmd)) {
		log_error("Failed to sync local devices before deactivating LV %s.",
			  display_lvname(mlv));
		return 0;
	}

	if (!deactivate_lv(cmd, mlv)) {
		log_error("Cannot deactivate pool metadata volume %s.",
			  display_lvname(mlv));
		ret = 0;
	}

deactivate_pmslv:
	if (!sync_local_dev_names(cmd)) {
		log_error("Failed to sync local devices before deactivating LV %s.",
			  display_lvname(pmslv));
		return 0;
	}

	if (!deactivate_lv(cmd, pmslv)) {
		log_error("Cannot deactivate pool metadata spare volume %s.",
			  display_lvname(pmslv));
		ret = 0;
	}

	if (!ret)
		return 0;

	if (pmslv == cache_lv->vg->pool_metadata_spare_lv) {
		cache_lv->vg->pool_metadata_spare_lv = NULL;
		pmslv->status &= ~POOL_METADATA_SPARE;
		lv_set_visible(pmslv);
	}

	/* Try to allocate new pool metadata spare LV */
	if (!handle_pool_metadata_spare(cache_lv->vg, 0, pvh, poolmetadataspare))
		stack;

	if (dm_snprintf(meta_path, sizeof(meta_path), "%s_meta%%d", cache_lv->name) < 0) {
		log_error("Can't prepare new metadata name for %s.", cache_lv->name);
		return 0;
	}

	if (!generate_lv_name(cache_lv->vg, meta_path, pms_path, sizeof(pms_path))) {
		log_error("Can't generate new name for %s.", meta_path);
		return 0;
	}

	if (!detach_pool_metadata_lv(first_seg(pool_lv), &mlv))
		return_0;

	/* Swap _pmspare and _cmeta name */
	if (!swap_lv_identifiers(cmd, mlv, pmslv))
		return_0;

	if (!attach_pool_metadata_lv(first_seg(pool_lv), pmslv))
		return_0;

	/* Used _cmeta (now _pmspare) becomes _meta%d */
	if (!lv_rename_update(cmd, mlv, pms_path, 0))
		return_0;

	if (!vg_write(cache_lv->vg) || !vg_commit(cache_lv->vg))
		return_0;

	/* FIXME: just as with  thinpool repair - fix the warning
	 *        where moving doesn't make any sense (same disk storage)
         */
	log_warn("WARNING: If everything works, remove %s volume.",
		 display_lvname(mlv));

	log_warn("WARNING: Use pvmove command to move %s on the best fitting PV.",
		 display_lvname(first_seg(pool_lv)->metadata_lv));

	return 1;
}

static int _lvconvert_to_thin_with_external(struct cmd_context *cmd,
			   struct logical_volume *lv,
			   struct logical_volume *thinpool_lv)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *thin_lv;
	const char *origin_name;

	struct lvcreate_params lvc = {
		.activate = CHANGE_AEY,
		.alloc = ALLOC_INHERIT,
		.major = -1,
		.minor = -1,
		.suppress_zero_warn = 1, /* Suppress warning for this thin */
		.permission = LVM_READ,
		.pool_name = thinpool_lv->name,
		.pvh = &vg->pvs,
		.read_ahead = DM_READ_AHEAD_AUTO,
		.stripes = 1,
		.virtual_extents = lv->le_count,
	};

	if (!_raid_split_image_conversion(lv))
		return_0;

	if (lv == thinpool_lv) {
		log_error("Can't use same LV %s for thin pool and thin volume.",
			  display_lvname(thinpool_lv));
		return 0;
	}

	if ((origin_name = arg_str_value(cmd, originname_ARG, NULL)))
		if (!validate_restricted_lvname_param(cmd, &vg->name, &origin_name))
			return_0;

	/*
	 * If NULL, an auto-generated 'lvol' name is used.
	 * If set, the lv create code checks the name isn't used.
	 */
	lvc.lv_name = origin_name;

	if (vg_is_shared(vg)) {
		/*
		 * FIXME: external origins don't work in lockd VGs.
		 * Prior to the lvconvert, there's a lock associated with
		 * the uuid of the external origin LV.  After the convert,
		 * that uuid belongs to the new thin LV, and a new LV with
		 * a new uuid exists as the non-thin, readonly external LV.
		 * We'd need to remove the lock for the previous uuid
		 * (the new thin LV will have no lock), and create a new
		 * lock for the new LV uuid used by the external LV.
		 */
		log_error("Can't use lock_type %s LV as external origin.",
			  vg->lock_type);
		return 0;
	}

	dm_list_init(&lvc.tags);

	if (!thin_pool_supports_external_origin(first_seg(thinpool_lv), lv))
		return_0;

	if (!(lvc.segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_THIN)))
		return_0;

	/*
	 * New thin LV needs to be created (all messages sent to pool) In this
	 * case thin volume is created READ-ONLY and also warn about not
	 * zeroing is suppressed.
	 *
	 * The new thin LV is created with the origin_name, or an autogenerated
	 * 'lvol' name.  Then the names and ids are swapped between the thin LV
	 * and the original/external LV.  So, the thin LV gets the name and id
	 * of the original LV arg, and the original LV arg gets the origin_name
	 * or the autogenerated name.
	 */

	if (!(thin_lv = lv_create_single(vg, &lvc)))
		return_0;

	if (!deactivate_lv(cmd, thin_lv)) {
		log_error("Aborting. Unable to deactivate new LV. "
			  "Manual intervention required.");
		return 0;
	}

	/*
	 * Crashing till this point will leave plain thin volume
	 * which could be easily removed by the user after i.e. power-off
	 */

	if (!swap_lv_identifiers(cmd, thin_lv, lv)) {
		stack;
		goto revert_new_lv;
	}

	/* Preserve read-write status of original LV here */
	thin_lv->status |= (lv->status & LVM_WRITE);

	if (!attach_thin_external_origin(first_seg(thin_lv), lv)) {
		stack;
		goto revert_new_lv;
	}

	if (!lv_update_and_reload(thin_lv)) {
		stack;
		goto deactivate_and_revert_new_lv;
	}

	log_print_unless_silent("Converted %s to thin volume with external origin %s.",
				display_lvname(thin_lv), display_lvname(lv));

	return 1;

deactivate_and_revert_new_lv:
	if (!swap_lv_identifiers(cmd, thin_lv, lv))
		stack;

	if (!deactivate_lv(cmd, thin_lv)) {
		log_error("Unable to deactivate failed new LV. "
			  "Manual intervention required.");
		return 0;
	}

	if (!detach_thin_external_origin(first_seg(thin_lv)))
		return_0;

revert_new_lv:
	/* FIXME Better to revert to backup of metadata? */
	if (!lv_remove(thin_lv) || !vg_write(vg) || !vg_commit(vg))
		log_error("Manual intervention may be required to remove "
			  "abandoned LV(s) before retrying.");

	return 0;
}

static int _lvconvert_swap_pool_metadata(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct logical_volume *metadata_lv)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *prev_metadata_lv;
	struct lv_segment *seg;
	struct lv_type *lvtype;
	char meta_name[NAME_LEN];
	const char *swap_name;
	uint32_t chunk_size;
	int is_thinpool;
	int is_cachepool;
	int lvt_enum;

	is_thinpool = lv_is_thin_pool(lv);
	is_cachepool = lv_is_cache_pool(lv);
	lvt_enum = get_lvt_enum(metadata_lv);
	lvtype = get_lv_type(lvt_enum);

	if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) {
		log_error("LV %s with type %s cannot be used as a metadata LV.",
			  display_lvname(metadata_lv), lvtype ? lvtype->name : "unknown");
		return 0;
	}

	if (!lv_is_visible(metadata_lv)) {
		log_error("Can't convert internal LV %s.",
			  display_lvname(metadata_lv));
		return 0;
	}

	if (lv_is_locked(metadata_lv)) {
		log_error("Can't convert locked LV %s.",
			  display_lvname(metadata_lv));
		return 0;
	}

	if (lv_is_origin(metadata_lv) ||
	    lv_is_merging_origin(metadata_lv) ||
	    lv_is_external_origin(metadata_lv) ||
	    lv_is_virtual(metadata_lv)) {
		log_error("Pool metadata LV %s is of an unsupported type.",
			  display_lvname(metadata_lv));
		return 0;
	}

	/* FIXME cache pool */
	if (is_thinpool && thin_pool_is_active(lv)) {
		/* If any volume referencing pool active - abort here */
		log_error("Cannot convert pool %s with active volumes.",
			  display_lvname(lv));
		return 0;
	}

	if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, is_cachepool ? "_cmeta" : "_tmeta") < 0)) {
                log_error("Failed to create internal lv names, pool name is too long.");
                return 0;
        }

	seg = first_seg(lv);

	/* Normally do NOT change chunk size when swapping */

	if (arg_is_set(cmd, chunksize_ARG)) {
		chunk_size = arg_uint_value(cmd, chunksize_ARG, 0);

		if ((chunk_size != seg->chunk_size) && !dm_list_empty(&lv->segs_using_this_lv)) {
			if (arg_count(cmd, force_ARG) == PROMPT) {
				log_error("Chunk size can be only changed with --force. Conversion aborted.");
				return 0;
			}

			if (!validate_pool_chunk_size(cmd, seg->segtype, chunk_size))
				return_0;

			log_warn("WARNING: Changing chunk size %s to %s for %s pool volume.",
				 display_size(cmd, seg->chunk_size),
				 display_size(cmd, chunk_size),
				 display_lvname(lv));

			/* Ok, user has likely some serious reason for this */
			if (!arg_count(cmd, yes_ARG) &&
			    yes_no_prompt("Do you really want to change chunk size for %s pool volume? [y/n]: ",
					  display_lvname(lv)) == 'n') {
				log_error("Conversion aborted.");
				return 0;
			}
		}

		seg->chunk_size = chunk_size;
	}

	if (!arg_count(cmd, yes_ARG) &&
	    yes_no_prompt("Do you want to swap metadata of %s pool with metadata volume %s? [y/n]: ",
			  display_lvname(lv),
			  display_lvname(metadata_lv)) == 'n') {
		log_error("Conversion aborted.");
		return 0;
	}

	if (!deactivate_lv(cmd, metadata_lv)) {
		log_error("Aborting. Failed to deactivate %s.",
			  display_lvname(metadata_lv));
		return 0;
	}

	/* Swap names between old and new metadata LV */

	if (!detach_pool_metadata_lv(seg, &prev_metadata_lv))
		return_0;

	swap_name = metadata_lv->name;

	if (!lv_rename_update(cmd, metadata_lv, "pvmove_tmeta", 0))
		return_0;

	/* Give the previous metadata LV the name of the LV replacing it. */

	if (!lv_rename_update(cmd, prev_metadata_lv, swap_name, 0))
		return_0;

	/* Rename deactivated metadata LV to have _tmeta suffix */

	if (!lv_rename_update(cmd, metadata_lv, meta_name, 0))
		return_0;

	if (!attach_pool_metadata_lv(seg, metadata_lv))
		return_0;

	if (!vg_write(vg) || !vg_commit(vg))
		return_0;

	return 1;
}

/*
 * Create a new pool LV, using the lv arg as the data sub LV.
 * The metadata sub LV is either a new LV created here, or an
 * existing LV specified by --poolmetadata.
 *
 * process_single_lv is the LV currently being processed by
 * process_each_lv().  It will sometimes be the same as the
 * lv arg, and sometimes not.
 */

static int _lvconvert_to_pool(struct cmd_context *cmd,
			      struct logical_volume *lv,
			      struct logical_volume *process_single_lv,
			      int to_thinpool,
			      int to_cachepool,
			      struct dm_list *use_pvh)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *metadata_lv = NULL;  /* existing or created */
	struct logical_volume *data_lv;             /* lv arg renamed */
	struct logical_volume *pool_lv;             /* new lv created here */
	const char *pool_metadata_name;             /* user-specified lv name */
	const char *pool_name;                      /* name of original lv arg */
	char meta_name[NAME_LEN];                   /* generated sub lv name */
	char data_name[NAME_LEN];                   /* generated sub lv name */
	char converted_names[3*NAME_LEN];	    /* preserve names of converted lv */
	struct segment_type *pool_segtype;          /* thinpool or cachepool */
	struct lv_segment *seg;
	unsigned int target_attr = ~0;
	unsigned int activate_pool;
	unsigned int zero_metadata;
	uint64_t meta_size;
	uint32_t meta_extents;
	uint32_t chunk_size;
	int chunk_calc;
	cache_metadata_format_t cache_metadata_format;
	cache_mode_t cache_mode;
	const char *policy_name;
	struct dm_config_tree *policy_settings = NULL;
	int pool_metadata_spare;
	thin_crop_metadata_t crop_metadata;
	thin_discards_t discards;
	thin_zero_t zero_new_blocks;
	int error_when_full;
	int r = 0;

	/* for handling lvmlockd cases */
	char *lockd_data_args = NULL;
	char *lockd_meta_args = NULL;
	char *lockd_data_name = NULL;
	char *lockd_meta_name = NULL;
	struct id lockd_data_id;
	struct id lockd_meta_id;
	const char *str_seg_type = to_cachepool ? SEG_TYPE_NAME_CACHE_POOL : SEG_TYPE_NAME_THIN_POOL;

	if (!_raid_split_image_conversion(lv))
		return_0;

	if (lv_is_thin_pool(lv) || lv_is_cache_pool(lv)) {
		log_error(INTERNAL_ERROR "LV %s is already a pool.", display_lvname(lv));
		return 0;
	}

	pool_segtype = get_segtype_from_string(cmd, str_seg_type);

	if (!pool_segtype ||
	    !pool_segtype->ops->target_present(cmd, NULL, &target_attr)) {
		log_error("%s: Required device-mapper target(s) not detected in your kernel.",
			  str_seg_type);
		return 0;
	}

	/* Allow to have only thinpool active and restore it's active state. */
	activate_pool = to_thinpool && lv_is_active(lv);

	/* Wipe metadata_lv by default, but allow skipping this for cache pools. */
	zero_metadata = (to_cachepool) ? arg_int_value(cmd, zero_ARG, 1) : 1;

	/* An existing LV needs to have its lock freed once it becomes a data LV. */
	if (vg_is_shared(vg) && lv->lock_args) {
		lockd_data_args = dm_pool_strdup(cmd->mem, lv->lock_args);
		lockd_data_name = dm_pool_strdup(cmd->mem, lv->name);
		memcpy(&lockd_data_id, &lv->lvid.id[1], sizeof(struct id));
	}

	/*
	 * The internal LV names for pool data/meta LVs.
	 */

	if ((dm_snprintf(meta_name, sizeof(meta_name), "%s%s", lv->name, to_cachepool ? "_cmeta" : "_tmeta") < 0) ||
	    (dm_snprintf(data_name, sizeof(data_name), "%s%s", lv->name, to_cachepool ? "_cdata" : "_tdata") < 0)) {
		log_error("Failed to create internal lv names, pool name is too long.");
		return 0;
	}

	/* If LV is inactive here, ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		return 0;

	/*
	 * If an existing LV is to be used as the metadata LV,
	 * verify that it's in a usable state.  These checks are
	 * not done by command def rules because this LV is not
	 * processed by process_each_lv.
	 */

	if ((pool_metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL))) {
		if (!validate_lvname_param(cmd, &vg->name, &pool_metadata_name)) {
			log_error("Metadata LV %s not found.", pool_metadata_name);
			return 0;
		}

		if (!(metadata_lv = find_lv(vg, pool_metadata_name))) {
			log_error("Unknown pool metadata LV %s.", pool_metadata_name);
			return 0;
		}

		/* An existing LV needs to have its lock freed once it becomes a meta LV. */
		if (vg_is_shared(vg) && metadata_lv->lock_args) {
			lockd_meta_args = dm_pool_strdup(cmd->mem, metadata_lv->lock_args);
			lockd_meta_name = dm_pool_strdup(cmd->mem, metadata_lv->name);
			memcpy(&lockd_meta_id, &metadata_lv->lvid.id[1], sizeof(struct id));
		}

		if (metadata_lv == lv) {
			log_error("Can't use same LV for pool data and metadata LV %s.",
				  display_lvname(metadata_lv));
			return 0;
		}

		if (metadata_lv == process_single_lv) {
			log_error("Use a different LV for pool metadata %s.",
				  display_lvname(metadata_lv));
			return 0;
		}

		if (!lv_is_visible(metadata_lv)) {
			log_error("Can't convert internal LV %s.",
				  display_lvname(metadata_lv));
			return 0;
		}

		if (lv_is_locked(metadata_lv)) {
			log_error("Can't convert locked LV %s.",
				  display_lvname(metadata_lv));
			return 0;
		}

		if (lv_is_mirror(metadata_lv)) {
			log_error("Mirror logical volumes cannot be used for pool metadata.");
			log_print_unless_silent("Try \"%s\" segment type instead.", SEG_TYPE_NAME_RAID1);
			return 0;
		}

		/* FIXME Tidy up all these type restrictions. (Use a type whitelist?) */
		if (lv_is_cache_type(metadata_lv) ||
		    lv_is_writecache(metadata_lv) ||
		    lv_is_thin_type(metadata_lv) ||
		    lv_is_cow(metadata_lv) || lv_is_merging_cow(metadata_lv) ||
		    lv_is_origin(metadata_lv) || lv_is_merging_origin(metadata_lv) ||
		    lv_is_external_origin(metadata_lv) ||
		    lv_is_virtual(metadata_lv)) {
			log_error("Pool metadata LV %s is of an unsupported type.",
				  display_lvname(metadata_lv));
			return 0;
		}

		/* If LV is inactive here, ensure it's not active elsewhere. */
		if (!lockd_lv(cmd, metadata_lv, "ex", 0))
			return 0;
	}

	if (!get_pool_params(cmd, pool_segtype,
			     &meta_size, &pool_metadata_spare,
			     &chunk_size, &discards, &zero_new_blocks))
		goto_bad;

	if (to_cachepool &&
	    !get_cache_params(cmd, &chunk_size, &cache_metadata_format, &cache_mode, &policy_name, &policy_settings))
		goto_bad;

	if (metadata_lv)
		meta_extents = metadata_lv->le_count;
	else if (meta_size)
		meta_extents = extents_from_size(cmd, meta_size, vg->extent_size);
	else
		meta_extents = 0; /* A default will be chosen by the "update" function. */

	/*
	 * Validate and/or choose defaults for meta_extents and chunk_size,
	 * this involves some complicated calculations.
	 */

	if (to_cachepool) {
		if (!update_cache_pool_params(cmd, vg->profile, vg->extent_size,
					      pool_segtype, target_attr,
					      lv->le_count,
					      &meta_extents,
					      metadata_lv,
					      &chunk_calc,
					      &chunk_size))
			goto_bad;
	} else {
		if (!update_thin_pool_params(cmd, vg->profile, vg->extent_size,
					     pool_segtype, target_attr,
					     lv->le_count,
					     &meta_extents,
					     metadata_lv,
					     &crop_metadata,
					     &chunk_calc,
					     &chunk_size,
					     &discards, &zero_new_blocks))
			goto_bad;
	}

	if (metadata_lv && (meta_extents > metadata_lv->le_count)) {
		log_error("Pool metadata LV %s is too small (%u extents) for required metadata (%u extents).",
			  display_lvname(metadata_lv), metadata_lv->le_count, meta_extents);
		goto bad;
	}

	log_verbose("Pool metadata extents %u chunk_size %u", meta_extents, chunk_size);

	(void) dm_snprintf(converted_names, sizeof(converted_names), "%s%s%s",
			   display_lvname(lv),
			   metadata_lv ? " and " : "",
			   metadata_lv ? display_lvname(metadata_lv) : "");

	/*
	 * Verify that user wants to use these LVs.
	 */
	log_warn("WARNING: Converting %s to %s pool's data%s %s metadata wiping.",
		 converted_names,
		 to_cachepool ? "cache" : "thin",
		 metadata_lv ? " and metadata volumes" : " volume",
		 zero_metadata ? "with" : "WITHOUT");

	if (zero_metadata)
		log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)");
	else if (to_cachepool)
		log_warn("WARNING: Using mismatched cache pool metadata MAY DESTROY YOUR DATA!");

	if (!arg_count(cmd, yes_ARG) &&
	    yes_no_prompt("Do you really want to convert %s? [y/n]: ",
			  converted_names) == 'n') {
		log_error("Conversion aborted.");
		goto bad;
	}

	/*
	 * If a new metadata LV needs to be created, collect the settings for
	 * the new LV and create it.
	 *
	 * If an existing LV is used for metadata, deactivate/activate/wipe it.
	 */

	if (!metadata_lv) {
		uint32_t meta_stripes;
		uint32_t meta_stripe_size;
		uint32_t meta_readahead;
		alloc_policy_t meta_alloc;
		unsigned meta_stripes_supplied;
		unsigned meta_stripe_size_supplied;

		if (!get_stripe_params(cmd, get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED),
				       &meta_stripes,
				       &meta_stripe_size,
				       &meta_stripes_supplied,
				       &meta_stripe_size_supplied))
			goto_bad;

		meta_readahead = arg_uint_value(cmd, readahead_ARG, cmd->default_settings.read_ahead);
		meta_alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);

		if (!(metadata_lv = alloc_pool_metadata(lv,
							meta_name,
							meta_readahead,
							meta_stripes,
							meta_stripe_size,
							meta_extents,
							meta_alloc,
							use_pvh)))
			goto_bad;
	} else {
		if (!deactivate_lv(cmd, metadata_lv)) {
			log_error("Aborting. Failed to deactivate %s.",
				  display_lvname(metadata_lv));
			goto bad;
		}

		if (zero_metadata) {
			metadata_lv->status |= LV_ACTIVATION_SKIP;
			if (!activate_lv(cmd, metadata_lv)) {
				log_error("Aborting. Failed to activate metadata lv.");
				goto bad;
			}
			metadata_lv->status &= ~LV_ACTIVATION_SKIP;

			if (!wipe_lv(metadata_lv, (struct wipe_params) {
						  .do_wipe_signatures = 1,
						  .is_metadata = 1,
						  .yes = arg_count(cmd, yes_ARG),
						  .force = arg_count(cmd, force_ARG) } )) {
				log_error("Aborting. Failed to wipe metadata lv.");
				goto bad;
			}
		}
	}

	/*
	 * Deactivate the data LV and metadata LV.
	 * We are changing target type, so deactivate first.
	 */

	if (!deactivate_lv(cmd, metadata_lv)) {
		log_error("Aborting. Failed to deactivate metadata lv. "
			  "Manual intervention required.");
		goto bad;
	}

	if (!deactivate_lv(cmd, lv)) {
		log_error("Aborting. Failed to deactivate logical volume %s.",
			  display_lvname(lv));
		goto bad;
	}

	/*
	 * When the LV referenced by the original function arg "lv"
	 * is renamed, it is then referenced as "data_lv".
	 *
	 * pool_name    pool name taken from lv arg
	 * data_name    sub lv name, generated
	 * meta_name    sub lv name, generated
	 *
	 * pool_lv      new lv for pool object, created here
	 * data_lv      sub lv, was lv arg, now renamed
	 * metadata_lv  sub lv, existing or created here
	 */

	data_lv = lv;
	pool_name = lv->name; /* Use original LV name for pool name */

	/*
	 * Rename the original LV arg to the internal data LV naming scheme.
	 *
	 * Since we wish to have underlaying devs to match _[ct]data
	 * rename data LV to match pool LV subtree first,
	 * also checks for visible LV.
	 *
	 * FIXME: any more types prohibited here?
	 */

	if (!lv_rename_update(cmd, data_lv, data_name, 0))
		goto_bad;

	/*
	 * Create LV structures for the new pool LV object,
	 * and connect it to the data/meta LVs.
	 */

	if (!(pool_lv = lv_create_empty(pool_name, NULL,
					(to_cachepool ? CACHE_POOL : THIN_POOL) | VISIBLE_LV | LVM_READ | LVM_WRITE,
					ALLOC_INHERIT, vg))) {
		log_error("Creation of pool LV failed.");
		goto bad;
	}

	/* Allocate a new pool segment */
	if (!(seg = alloc_lv_segment(pool_segtype, pool_lv, 0, data_lv->le_count, 0,
				     pool_lv->status, 0, NULL, 1,
				     data_lv->le_count, 0, 0, 0, 0, NULL)))
		goto_bad;

	/* Add the new segment to the layer LV */
	dm_list_add(&pool_lv->segments, &seg->list);
	pool_lv->le_count = data_lv->le_count;
	pool_lv->size = data_lv->size;

	if (!attach_pool_data_lv(seg, data_lv))
		goto_bad;

	/*
	 * Create a new lock for a thin pool LV.  A cache pool LV has no lock.
	 * Locks are removed from existing LVs that are being converted to
	 * data and meta LVs (they are unlocked and deleted below.)
	 */
	if (vg_is_shared(vg)) {
		if (to_cachepool) {
			data_lv->lock_args = NULL;
			metadata_lv->lock_args = NULL;
		} else {
			data_lv->lock_args = NULL;
			metadata_lv->lock_args = NULL;

			if (!strcmp(vg->lock_type, "sanlock"))
				pool_lv->lock_args = "pending";
			else if (!strcmp(vg->lock_type, "dlm"))
				pool_lv->lock_args = "dlm";
			else if (!strcmp(vg->lock_type, "idm"))
				pool_lv->lock_args = "idm";
			/* The lock_args will be set in vg_write(). */
		}
	}

	/* Apply settings to the new pool seg */
	if (to_cachepool) {
		if (!cache_set_params(seg, chunk_size, cache_metadata_format, cache_mode, policy_name, policy_settings))
			goto_bad;
	} else {
		seg->transaction_id = 0;
		seg->crop_metadata = crop_metadata;
		seg->chunk_size = chunk_size;
		seg->discards = discards;
		seg->zero_new_blocks = zero_new_blocks;
		if (crop_metadata == THIN_CROP_METADATA_NO)
			pool_lv->status |= LV_CROP_METADATA;
		if (!recalculate_pool_chunk_size_with_dev_hints(pool_lv, chunk_calc))
			goto_bad;

		/* Error when full */
		if (arg_is_set(cmd, errorwhenfull_ARG))
			error_when_full = arg_uint_value(cmd, errorwhenfull_ARG, 0);
		else
			error_when_full = find_config_tree_bool(cmd, activation_error_when_full_CFG, vg->profile);
		if (error_when_full)
			pool_lv->status |= LV_ERROR_WHEN_FULL;
	}

	/*
	 * Rename deactivated metadata LV to have _tmeta suffix.
	 * Implicit checks if metadata_lv is visible.
	 */
	if (pool_metadata_name &&
	    !lv_rename_update(cmd, metadata_lv, meta_name, 0))
		goto_bad;

	if (!attach_pool_metadata_lv(seg, metadata_lv))
		goto_bad;

	if (!handle_pool_metadata_spare(vg,
					metadata_lv->le_count,
					use_pvh, pool_metadata_spare))
		goto_bad;

	if (!vg_write(vg) || !vg_commit(vg))
		goto_bad;

	if (activate_pool && !lockd_lv(cmd, pool_lv, "ex", LDLV_PERSISTENT)) {
		log_error("Failed to lock pool LV %s.", display_lvname(pool_lv));
		goto out;
	}

	if (activate_pool &&
	    !activate_lv(cmd, pool_lv)) {
		log_error("Failed to activate pool logical volume %s.",
			  display_lvname(pool_lv));
		/* Deactivate subvolumes */
		if (!deactivate_lv(cmd, seg_lv(seg, 0)))
			log_error("Failed to deactivate pool data logical volume %s.",
				  display_lvname(seg_lv(seg, 0)));
		if (!deactivate_lv(cmd, seg->metadata_lv))
			log_error("Failed to deactivate pool metadata logical volume %s.",
				  display_lvname(seg->metadata_lv));
		goto out;
	}

	r = 1;

out:
	if (r)
		log_print_unless_silent("Converted %s to %s pool.",
					converted_names, to_cachepool ? "cache" : "thin");

	/*
	 * Unlock and free the locks from existing LVs that became pool data
	 * and meta LVs.
	 */
	if (lockd_data_name) {
		if (!lockd_lv_name(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args, "un", LDLV_PERSISTENT))
			log_error("Failed to unlock pool data LV %s/%s", vg->name, lockd_data_name);
		lockd_free_lv(cmd, vg, lockd_data_name, &lockd_data_id, lockd_data_args);
	}

	if (lockd_meta_name) {
		if (!lockd_lv_name(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args, "un", LDLV_PERSISTENT))
			log_error("Failed to unlock pool metadata LV %s/%s", vg->name, lockd_meta_name);
		lockd_free_lv(cmd, vg, lockd_meta_name, &lockd_meta_id, lockd_meta_args);
	}
bad:
	if (policy_settings)
		dm_config_destroy(policy_settings);

	return r;
#if 0
revert_new_lv:
	/* TBD */
	if (!pool_metadata_lv_name) {
		if (!deactivate_lv(cmd, metadata_lv)) {
			log_error("Failed to deactivate metadata lv.");
			return 0;
		}
		if (!lv_remove(metadata_lv) || !vg_write(vg) || !vg_commit(vg))
			log_error("Manual intervention may be required to remove "
				  "abandoned LV(s) before retrying.");
	}

	return 0;
#endif
}

static int _cache_vol_attach(struct cmd_context *cmd,
			        struct logical_volume *lv,
			        struct logical_volume *lv_fast)
{
	char cvol_name[NAME_LEN];
	struct volume_group *vg = lv->vg;
	struct logical_volume *cache_lv;
	uint32_t chunk_size = 0;
	uint64_t poolmetadatasize = 0;
	cache_metadata_format_t cache_metadata_format;
	cache_mode_t cache_mode;
	const char *policy_name;
	struct dm_config_tree *policy_settings = NULL;
	char *lockd_fast_args = NULL;
	char *lockd_fast_name = NULL;
	struct id lockd_fast_id;
	int r = 0;

	if (!validate_lv_cache_create_pool(lv_fast))
		return_0;

	if (!get_cache_params(cmd, &chunk_size, &cache_metadata_format, &cache_mode, &policy_name, &policy_settings))
		goto_out;

	/*
	 * lv/cache_lv keeps the same lockd lock it had before, the lock for
	 * lv_fast is kept but is not used while it's attached, and
	 * lv_corig has no lock.  (When the cachevol is split a new lvmlockd
	 * lock does not need to be created for it again.)
	 */
	if (vg_is_shared(vg) && lv_fast->lock_args) {
		lockd_fast_args = dm_pool_strdup(cmd->mem, lv_fast->lock_args);
		lockd_fast_name = dm_pool_strdup(cmd->mem, lv_fast->name);
		memcpy(&lockd_fast_id, &lv_fast->lvid.id[1], sizeof(struct id));
	}

	/*
	 * The lvm tradition is to rename an LV with a special role-specific
	 * suffix when it becomes hidden.  Here the _cvol suffix is added to
	 * the fast LV name.  When the cache is detached, it's renamed back.
	 */
	if (dm_snprintf(cvol_name, sizeof(cvol_name), "%s_cvol", lv_fast->name) < 0) {
		log_error("Can't prepare new cachevol name for %s.", display_lvname(lv_fast));
		goto out;
	}
	if (!lv_rename_update(cmd, lv_fast, cvol_name, 0))
		goto_out;

	lv_fast->status |= LV_CACHE_VOL; /* Mark as cachevol LV */

	/*
	 * Changes the vg struct to match the desired state.
	 *
	 * - lv == cache_lv, which keeps existing lv name and id, gets new
	 *   segment with segtype "cache".
	 *
	 * - lv_fast keeps its existing name and id, becomes hidden.
	 *
	 * - lv_corig gets new name (existing name + _corig suffix),
	 *   gets new id, becomes hidden, gets segments from lv.
	 */

	if (!(cache_lv = lv_cache_create(lv_fast, lv)))
		goto_out;

	if (arg_is_set(cmd, poolmetadatasize_ARG))
		poolmetadatasize = arg_uint64_value(cmd, poolmetadatasize_ARG, 0);

	if (!cache_vol_set_params(cmd, cache_lv, lv_fast, poolmetadatasize, chunk_size, cache_metadata_format, cache_mode, policy_name, policy_settings))
		goto_out;

	if (cache_mode == CACHE_MODE_WRITEBACK) {
		log_warn("WARNING: repairing a damaged cachevol is not yet possible.");
		log_warn("WARNING: cache mode writethrough is suggested for safe operation.");
		if (!arg_count(cmd, yes_ARG) &&
		    yes_no_prompt("Continue using writeback without repair?") == 'n')
			goto_out;
	}

	/*
	 * vg_write(), suspend_lv(), vg_commit(), resume_lv(),
	 * where the old LV is suspended and the new LV is resumed.
	 */

	if (!lv_update_and_reload(cache_lv))
		goto_out;

	if (lockd_fast_name) {
		/* lockd unlock for lv_fast */
		if (!lockd_lv_name(cmd, vg, lockd_fast_name, &lockd_fast_id, lockd_fast_args, "un", LDLV_PERSISTENT))
			log_error("Failed to unlock fast LV %s/%s", vg->name, lockd_fast_name);
	}

	r = 1;
out:
	if (policy_settings)
		dm_config_destroy(policy_settings);

	return r;
}

static int _cache_pool_attach(struct cmd_context *cmd,
			      struct logical_volume *lv,
			      struct logical_volume *cachepool_lv)
{
	struct logical_volume *cache_lv;
	uint32_t chunk_size = 0;
	cache_metadata_format_t cache_metadata_format;
	cache_mode_t cache_mode;
	const char *policy_name;
	struct dm_config_tree *policy_settings = NULL;
	int r = 0;

	if (!validate_lv_cache_create_pool(cachepool_lv))
		return_0;

	if (!get_cache_params(cmd, &chunk_size, &cache_metadata_format, &cache_mode, &policy_name, &policy_settings))
		goto_bad;

	if (!archive(lv->vg))
		goto_bad;

	if (!(cache_lv = lv_cache_create(cachepool_lv, lv)))
		goto_bad;

	if (!cache_set_params(first_seg(cache_lv), chunk_size, cache_metadata_format, cache_mode, policy_name, policy_settings))
		goto_bad;

	if (!lv_update_and_reload(cache_lv))
		goto_bad;

	r = 1;
bad:
	if (policy_settings)
		dm_config_destroy(policy_settings);

	return r;
}

static struct convert_poll_id_list* _convert_poll_id_list_create(struct cmd_context *cmd,
								 const struct logical_volume *lv)
{
	struct convert_poll_id_list *idl = (struct convert_poll_id_list *) dm_pool_alloc(cmd->mem, sizeof(struct convert_poll_id_list));

	if (!idl) {
		log_error("Convert poll ID list allocation failed.");
		return NULL;
	}

	if (!(idl->id = _create_id(cmd, lv->vg->name, lv->name, lv->lvid.s))) {
		dm_pool_free(cmd->mem, idl);
		return_NULL;
	}

	idl->is_merging_origin = lv_is_merging_origin(lv);
	idl->is_merging_origin_thin = idl->is_merging_origin && seg_is_thin_volume(find_snapshot(lv));

	return idl;
}

/*
 * Data/results accumulated during processing.
 */
struct lvconvert_result {
	unsigned need_polling:1;
	unsigned wait_cleaner_writecache:1;
	unsigned active_begin:1;
	unsigned remove_cache:1;
	struct dm_list poll_idls;
};


/*
 * repair-related lvconvert utilities
 */

static int _lvconvert_repair_pvs_mirror(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle,
			struct dm_list *use_pvh)
{
	struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle;
	struct lvconvert_params lp = { 0 };
	struct convert_poll_id_list *idl;
	int ret;

	/*
	 * We want to allow cmirror active on multiple nodes to be repaired,
	 * but normal mirror to only be repaired if active exclusively here.
	 * If the LV is active it already has the necessary lock, but if not
	 * active, then require ex since we cannot know the active state on
	 * other hosts.
	 */
	if (!lv_is_active(lv)) {
		if (!lockd_lv(cmd, lv, "ex", 0))
			return_0;
	}

	/*
	 * FIXME: temporary use of lp because _lvconvert_mirrors_repair()
	 * and _aux() still use lp fields everywhere.
	 * Migrate them away from using lp (for the most part just use
	 * local variables, and check arg_values directly).
	 */

	/*
	 * Fill in any lp fields here that this fn expects to be set before
	 * it's called.  It's hard to tell what the old code expects in lp
	 * for repair; it doesn't take the stripes option, but it seems to
	 * expect lp.stripes to be set to 1.
	 */
	lp.alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, ALLOC_INHERIT);
	lp.stripes = 1;

	ret = _lvconvert_mirrors_repair(cmd, lv, &lp, use_pvh);

	if (lp.need_polling) {
		if (!lv_is_active(lv))
			log_print_unless_silent("Conversion starts after activation.");
		else {
			if (!(idl = _convert_poll_id_list_create(cmd, lv)))
				return 0;
			dm_list_add(&lr->poll_idls, &idl->list);
		}
		lr->need_polling = 1;
	}

	return ret;
}

static void _lvconvert_repair_pvs_raid_ask(struct cmd_context *cmd, int *do_it)
{
	const char *dev_policy;

	*do_it = 1;

	if (arg_is_set(cmd, usepolicies_ARG)) {
		dev_policy = find_config_tree_str(cmd, activation_raid_fault_policy_CFG, NULL);

		if (!strcmp(dev_policy, "allocate") ||
		    !strcmp(dev_policy, "replace"))
			return;

		/* else if (!strcmp(dev_policy, "anything_else")) -- no replace */
		*do_it = 0;
		return;
	}

	if (!arg_count(cmd, yes_ARG) &&
	    yes_no_prompt("Attempt to replace failed RAID images "
			  "(requires full device resync)? [y/n]: ") == 'n') {
		*do_it = 0;
	}
}

static int _lvconvert_repair_pvs_raid(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle,
			struct dm_list *use_pvh)
{
	struct dm_list *failed_pvs;
	int do_it;

	if (!lv_is_active(lv_lock_holder(lv))) {
		log_error("%s must be active to perform this operation.", display_lvname(lv));
		return 0;
	}

	lv_check_transient(lv); /* TODO check this in lib for all commands? */

	_lvconvert_repair_pvs_raid_ask(cmd, &do_it);

	if (do_it) {
		if (!(failed_pvs = _failed_pv_list(lv->vg)))
			return_0;

		if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), failed_pvs, use_pvh)) {
			log_error("Failed to replace faulty devices in %s.",
				  display_lvname(lv));
			return 0;
		}

		log_print_unless_silent("Faulty devices in %s successfully replaced.",
					display_lvname(lv));
		return 1;
	}

	/* "warn" if policy not set to replace */
	if (arg_is_set(cmd, usepolicies_ARG))
		log_warn("Use 'lvconvert --repair %s' to replace "
			 "failed device.", display_lvname(lv));
	return 1;
}

static int _lvconvert_repair_pvs(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle)
{
	struct dm_list *failed_pvs;
	struct dm_list *use_pvh;
	int ret;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
	} else
		use_pvh = &lv->vg->pvs;

	if (lv_is_raid(lv))
		ret = _lvconvert_repair_pvs_raid(cmd, lv, handle, use_pvh);
	else if (lv_is_mirror(lv))
		ret = _lvconvert_repair_pvs_mirror(cmd, lv, handle, use_pvh);
	else
		ret = 0;

	if (ret && arg_is_set(cmd, usepolicies_ARG)) {
		if ((failed_pvs = _failed_pv_list(lv->vg)))
			_remove_missing_empty_pv(lv->vg, failed_pvs);
	}

	return ret ? ECMD_PROCESSED : ECMD_FAILED;
}

static int _lvconvert_repair_cachepool_thinpool(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle)
{
	int poolmetadataspare = arg_int_value(cmd, poolmetadataspare_ARG, DEFAULT_POOL_METADATA_SPARE);
	struct dm_list *use_pvh;

	/* ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		return_0;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
	} else
		use_pvh = &lv->vg->pvs;

	if (lv_is_thin_pool(lv)) {
		if (!_lvconvert_thin_pool_repair(cmd, lv, use_pvh, poolmetadataspare))
			return_ECMD_FAILED;
	} else /* cache */ {
		if (!_lvconvert_cache_repair(cmd, lv, use_pvh, poolmetadataspare))
			return_ECMD_FAILED;
	}

	return ECMD_PROCESSED;
}

static int _lvconvert_repair_single(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle)
{
	if (lv_is_thin_pool(lv) ||
	    lv_is_cache(lv) ||
	    lv_is_cache_pool(lv))
		return _lvconvert_repair_cachepool_thinpool(cmd, lv, handle);

	if (lv_is_raid(lv) || lv_is_mirror(lv))
		return _lvconvert_repair_pvs(cmd, lv, handle);

	log_error("Unsupported volume type for repair of volume %s.",
		  display_lvname(lv));

	return ECMD_FAILED;
}

/*
 * FIXME: add option --repair-pvs to call _lvconvert_repair_pvs() directly,
 * and option --repair-thinpool to call _lvconvert_repair_thinpool().
 * and option --repair-cache to call _lvconvert_repair_cache().
 * and option --repair-cachepool to call _lvconvert_repair_cachepool().
 */
int lvconvert_repair_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	struct convert_poll_id_list *idl;
	int saved_ignore_suspended_devices;
	int ret, poll_ret;

	dm_list_init(&lr.poll_idls);

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	saved_ignore_suspended_devices = ignore_suspended_devices();
	init_ignore_suspended_devices(1);

	cmd->handles_missing_pvs = 1;

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_repair_single);

	init_ignore_suspended_devices(saved_ignore_suspended_devices);

	if (lr.need_polling) {
		dm_list_iterate_items(idl, &lr.poll_idls) {
			poll_ret = _lvconvert_poll_by_id(cmd, idl->id,
						arg_is_set(cmd, background_ARG), 0, 0);
			if (poll_ret > ret)
				ret = poll_ret;
		}
	}

	destroy_processing_handle(cmd, handle);

	return ret;
}

static int _lvconvert_replace_pv_single(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle)
{
	struct arg_value_group_list *group;
	const char *tmp_str;
	struct dm_list *use_pvh;
	struct dm_list *replace_pvh;
	char **replace_pvs;
	int replace_pv_count;
	int i;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
	} else
		use_pvh = &lv->vg->pvs;

	if (!(replace_pv_count = arg_count(cmd, replace_ARG)))
		return_ECMD_FAILED;

	if (!(replace_pvs = dm_pool_alloc(cmd->mem, sizeof(char *) * replace_pv_count)))
		return_ECMD_FAILED;

	i = 0;
	dm_list_iterate_items(group, &cmd->arg_value_groups) {
		if (!grouped_arg_is_set(group->arg_values, replace_ARG))
			continue;
		if (!(tmp_str = grouped_arg_str_value(group->arg_values, replace_ARG, NULL))) {
			log_error("Failed to get '--replace' argument");
			return ECMD_FAILED;
		}
		if (!(replace_pvs[i++] = dm_pool_strdup(cmd->mem, tmp_str)))
			return_ECMD_FAILED;
	}

	if (!(replace_pvh = create_pv_list(cmd->mem, lv->vg, replace_pv_count, replace_pvs, 0)))
		return_ECMD_FAILED;

	if (!lv_raid_replace(lv, arg_count(cmd, force_ARG), replace_pvh, use_pvh))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_replace_pv_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_replace_pv_single);

	destroy_processing_handle(cmd, handle);

	return ret;
}


/*
 * Merge a COW snapshot LV into its origin.
 */

static int _lvconvert_merge_snapshot_single(struct cmd_context *cmd,
                                       struct logical_volume *lv,
                                       struct processing_handle *handle)
{
	struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle;
	struct logical_volume *lv_to_poll = NULL;
	struct convert_poll_id_list *idl;

	if (!_lvconvert_merge_old_snapshot(cmd, lv, &lv_to_poll))
		return_ECMD_FAILED;

	if (lv_to_poll) {
		if (!(idl = _convert_poll_id_list_create(cmd, lv_to_poll)))
			return_ECMD_FAILED;
		dm_list_add(&lr->poll_idls, &idl->list);
		lr->need_polling = 1;
	}

	return ECMD_PROCESSED;
}

int lvconvert_merge_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	struct convert_poll_id_list *idl;
	int ret, poll_ret;

	dm_list_init(&lr.poll_idls);

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_merge_snapshot_single);

	if (lr.need_polling) {
		dm_list_iterate_items(idl, &lr.poll_idls) {
			poll_ret = _lvconvert_poll_by_id(cmd, idl->id,
						arg_is_set(cmd, background_ARG), 1, 0);
			if (poll_ret > ret)
				ret = poll_ret;
		}
	}

	destroy_processing_handle(cmd, handle);

	return ret;
}

/*
 * Separate a COW snapshot from its origin.
 *
 * lvconvert --splitsnapshot LV_snapshot
 * lvconvert_split_cow_snapshot
 */

static int _lvconvert_split_snapshot_single(struct cmd_context *cmd,
                                       struct logical_volume *lv,
                                       struct processing_handle *handle)
{
	if (!_lvconvert_splitsnapshot(cmd, lv))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_split_snapshot_single);
}

/*
 * Combine two LVs that were once an origin/cow pair of LVs, were then
 * separated with --splitsnapshot, and now with this command are combined again
 * into the origin/cow pair.
 *
 * This is an obscure command that has little to no real uses.
 *
 * The command has unusual handling of position args.  The first position arg
 * will become the origin LV, and is not processed by process_each_lv.  The
 * second position arg will become the cow LV and is processed by
 * process_each_lv.
 *
 * The single function can grab the origin LV from position_argv[0].
 *
 * begin with an ordinary LV foo:
 * lvcreate -n foo -L 1 vg
 *
 * create a cow snapshot of foo named foosnap:
 * lvcreate -s -L 1 -n foosnap vg/foo
 *
 * now, foo is an "origin LV" and foosnap is a "cow LV"
 * (foosnap matches LV_snapshot aka lv_is_cow)
 *
 * split the two LVs apart:
 * lvconvert --splitsnapshot vg/foosnap
 *
 * now, foo is *not* an origin LV and foosnap is *not* a cow LV
 * (foosnap does not match LV_snapshot)
 *
 * now, combine the two LVs again:
 * lvconvert --snapshot vg/foo vg/foosnap
 *
 * after this, foosnap will match LV_snapshot again.
 *
 * FIXME: when splitsnapshot is run, the previous cow LV should be
 * flagged in the metadata somehow, and then that flag should be
 * required here.  As it is now, the first and second args
 * (origin and cow) can be swapped and nothing catches it.
 */

static int _lvconvert_combine_split_snapshot_single(struct cmd_context *cmd,
                                       struct logical_volume *lv,
                                       struct processing_handle *handle)
{
	const char *origin_name = cmd->position_argv[0];

	if (vg_is_shared(lv->vg)) {
		log_error("Unable to combine split snapshots in VG with lock_type %s", lv->vg->lock_type);
		return ECMD_FAILED;
	}

	/* If origin_name includes VG name, the VG name is removed. */
	if (!validate_lvname_param(cmd, &lv->vg->name, &origin_name))
		return_ECMD_FAILED;

	if (!_lvconvert_snapshot(cmd, lv, origin_name))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_combine_split_snapshot_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	const char *vgname = NULL;
	const char *lvname1_orig;
	const char *lvname2_orig;
	const char *lvname1_split;
	char *vglv;
	int vglv_sz;

	/*
	 * Hack to accomodate an old parsing quirk that allowed the
	 * the VG name to be attached to only the LV in arg pos 1,
	 * i.e. lvconvert -s vgname/lvname lvname
	 *
	 * The LV name in arg pos 2 is the one that is processed
	 * by process_each_lv().  If that LV has no VG name, but
	 * the first LV does, then copy the VG name from arg pos 1
	 * and add it to the LV name in arg pos 2 so that the
	 * standard arg parsing in process_each_lv will find it.
	 *
	 * This is the only instance in all commands.
	 */

	lvname1_orig = cmd->position_argv[0];
	lvname2_orig = cmd->position_argv[1];

	if (strchr(lvname1_orig, '/') && !strchr(lvname2_orig, '/') && !getenv("LVM_VG_NAME")) {
		if (!(lvname1_split = dm_pool_strdup(cmd->mem, lvname1_orig)))
			return_ECMD_FAILED;

		if (!validate_lvname_param(cmd, &vgname, &lvname1_split))
			return_ECMD_FAILED;

		vglv_sz = strlen(vgname) + strlen(lvname2_orig) + 2;
		if (!(vglv = dm_pool_alloc(cmd->mem, vglv_sz)) ||
		    dm_snprintf(vglv, vglv_sz, "%s/%s", vgname, lvname2_orig) < 0) {
       			log_error("vg/lv string alloc failed.");
			return ECMD_FAILED;
		}

		/* vglv is now vgname/lvname2 and replaces lvname2_orig */

		cmd->position_argv[1] = vglv;
	}

	return process_each_lv(cmd, 1, cmd->position_argv + 1, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_combine_split_snapshot_single);
}

static int _lvconvert_start_poll_single(struct cmd_context *cmd,
                                       struct logical_volume *lv,
                                       struct processing_handle *handle)
{
	struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle;
	struct convert_poll_id_list *idl;

	if (!(idl = _convert_poll_id_list_create(cmd, lv)))
		return_ECMD_FAILED;
	dm_list_add(&lr->poll_idls, &idl->list);

	lr->need_polling = 1;

	return ECMD_PROCESSED;
}

int lvconvert_start_poll_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	struct convert_poll_id_list *idl;
	int saved_ignore_suspended_devices;
	int ret, poll_ret;

	dm_list_init(&lr.poll_idls);

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	saved_ignore_suspended_devices = ignore_suspended_devices();
	init_ignore_suspended_devices(1);

	cmd->handles_missing_pvs = 1;

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_start_poll_single);

	init_ignore_suspended_devices(saved_ignore_suspended_devices);

	if (lr.need_polling) {
		dm_list_iterate_items(idl, &lr.poll_idls) {
			poll_ret = _lvconvert_poll_by_id(cmd, idl->id,
						arg_is_set(cmd, background_ARG), 0, 0);
			if (poll_ret > ret)
				ret = poll_ret;
		}
	}

	destroy_processing_handle(cmd, handle);

	return ret;
}

static int _lvconvert_to_pool_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	struct dm_list *use_pvh = NULL;
	int to_thinpool = 0;
	int to_cachepool = 0;

	switch (cmd->command->command_enum) {
	case lvconvert_to_thinpool_CMD:
		to_thinpool = 1;
		break;
	case lvconvert_to_cachepool_CMD:
		to_cachepool = 1;
		break;
	default:
		log_error(INTERNAL_ERROR "Invalid lvconvert pool command");
		return ECMD_FAILED;
	};

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
	} else
		use_pvh = &lv->vg->pvs;

	if (!_lvconvert_to_pool(cmd, lv, lv, to_thinpool, to_cachepool, use_pvh))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

/*
 * The LV position arg is used as thinpool/cachepool data LV.
 */

int lvconvert_to_pool_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_to_pool_single);
}

#define MAX_CACHEDEVS 8

static int _lv_create_cachevol(struct cmd_context *cmd,
			       struct volume_group *vg,
			       struct logical_volume *lv,
			       struct logical_volume **cachevol_lv)
{
	char cvname[NAME_LEN];
	char format[NAME_LEN];
	struct dm_list *use_pvh;
	struct pv_list *pvl;
	const char *device_name = "";
	struct device *dev_fast;
	char *dev_argv[MAX_CACHEDEVS];
	int dev_argc = 0;
	uint64_t cache_size_sectors = 0;
	uint64_t full_size_sectors = 0;
	uint64_t pv_size_sectors;
	struct logical_volume *cachevol;
	struct arg_value_group_list *group;
	struct lvcreate_params lp = {
		.activate = CHANGE_AN,
		.alloc = ALLOC_INHERIT,
		.major = -1,
		.minor = -1,
		.permission = LVM_READ | LVM_WRITE,
		.pvh = &vg->pvs,
		.read_ahead = DM_READ_AHEAD_NONE,
		.stripes = 1,
		.vg_name = vg->name,
		.zero = 0,
		.wipe_signatures = 0,
		.suppress_zero_warn = 1,
	};

	/*
	 * If cache size is not set, and all cachedevice's are unused,
	 * then the cache size is the sum of all cachedevice sizes.
	 */
	cache_size_sectors = arg_uint64_value(cmd, cachesize_ARG, 0);

	dm_list_iterate_items(group, &cmd->arg_value_groups) {
		if (!grouped_arg_is_set(group->arg_values, cachedevice_ARG))
			continue;

		if (!(device_name = grouped_arg_str_value(group->arg_values, cachedevice_ARG, NULL)))
			break;

		if (device_name[0] == '@') {
			if (!cache_size_sectors) {
				log_error("With tag as cachedevice, --cachesize is required.");
				return 0;
			}
			goto add_dev_arg;
		}

		if (!(dev_fast = dev_cache_get(cmd, device_name, cmd->filter))) {
			log_error("Device %s not found.", device_name);
			return 0;
		}

		if (!(pvl = find_pv_in_vg(vg, device_name))) {
			log_error("PV %s not found in VG.", device_name);
			return 0;
		}

		/*
		 * If the dev is used in the VG, then require a cachesize to allocate
		 * from it.  If it is not used in the VG, then prompt asking if the
		 * entire dev should be used.
		 */
		if (!cache_size_sectors && pvl->pv->pe_alloc_count) {
			log_error("PV %s is in use, --cachesize is required.", device_name);
			return 0;
		}

		if (!cache_size_sectors) {
			pv_size_sectors = (pvl->pv->pe_count * (uint64_t)vg->extent_size);

			if (!arg_is_set(cmd, yes_ARG) &&
			    yes_no_prompt("Use all %s from %s for cache? [y/n]: ",
					  display_size(cmd, pv_size_sectors), device_name) == 'n') {
				log_print_unless_silent("Use --cachesize SizeMB to use a part of the cachedevice.");
				log_error("Conversion aborted.");
				return 0;
			}
			full_size_sectors += pv_size_sectors;
		}
 add_dev_arg:
		if (dev_argc >= MAX_CACHEDEVS) {
			log_error("Cannot allocate from more than %u cache devices.", MAX_CACHEDEVS);
			return 0;
		}

		dev_argv[dev_argc++] = (char*)device_name;
	}

	if (!cache_size_sectors)
		cache_size_sectors = full_size_sectors;

	if (!dev_argc) {
		log_error("No cachedevice specified to create a cachevol.");
		return 0;
	}

	if (!(use_pvh = create_pv_list(cmd->mem, vg, dev_argc, dev_argv, 1))) {
		log_error("cachedevice not found in VG %s.", device_name);
		return 0;
	}

	if (dm_snprintf(cvname, NAME_LEN, "%s_cache", lv->name) < 0) {
		log_error("Failed to create cachevol LV name.");
		return 0;
	}

	if (find_lv(vg, cvname)) {
		memset(format, 0, sizeof(cvname));
		memset(cvname, 0, sizeof(cvname));
		if (dm_snprintf(format, sizeof(format), "%s_cache%%d", lv->name) < 0) {
			log_error("Failed to generate cachevol LV format.");
			return 0;
		}
		if (!generate_lv_name(vg, format, cvname, sizeof(cvname))) {
			log_error("Failed to generate cachevol LV name.");
			return 0;
		}
	}

	lp.lv_name = cvname;
	lp.pvh = use_pvh;
	lp.extents = cache_size_sectors / vg->extent_size;

	log_print_unless_silent("Creating cachevol LV %s with size %s.",
				cvname, display_size(cmd, cache_size_sectors));

	dm_list_init(&lp.tags);

	if (!(lp.segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED)))
		return_0;

	if (!(cachevol = lv_create_single(vg, &lp))) {
		log_error("Failed to create cachevol LV");
		return 0;
	}

	*cachevol_lv = cachevol;
	return 1;
}

int lvconvert_cachevol_attach_single(struct cmd_context *cmd,
				     struct logical_volume *lv,
				     struct processing_handle *handle)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *lv_fast;
	const char *fast_name;

	/*
	 * User specifies an existing cachevol to use or a cachedevice
	 * to create a cachevol from.
	 */
	if ((fast_name = arg_str_value(cmd, cachevol_ARG, NULL))) {
		if (!validate_lvname_param(cmd, &vg->name, &fast_name))
			goto_bad;

		if (!(lv_fast = find_lv(vg, fast_name))) {
			log_error("LV %s not found.", fast_name);
			goto bad;
		}

		if (lv_is_cache_vol(lv_fast)) {
			log_error("LV %s is already used as a cachevol.", display_lvname(lv_fast));
			goto bad;
		}

		if (!dm_list_empty(&lv_fast->segs_using_this_lv)) {
			log_error("LV %s is already in use.", display_lvname(lv_fast));
			goto bad;
		}

		if (!arg_is_set(cmd, yes_ARG) &&
		    yes_no_prompt("Erase all existing data on %s? [y/n]: ", display_lvname(lv_fast)) == 'n') {
			log_error("Conversion aborted.");
			goto bad;
		}

		if (!lockd_lv(cmd, lv_fast, "ex", 0))
			goto_bad;
	} else {
		if (!_lv_create_cachevol(cmd, vg, lv, &lv_fast))
			goto_bad;
	}

	/* Ensure the LV is not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		goto_bad;

	if (!wipe_cache_pool(lv_fast))
		goto_bad;

	/* When the lv arg is a thinpool, redirect command to data sub lv. */

	if (lv_is_thin_pool(lv)) {
		lv = seg_lv(first_seg(lv), 0);
		log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv));
	}

	if (!_raid_split_image_conversion(lv))
		goto_bad;

	/* Attach the cache to the main LV. */

	if (!_cache_vol_attach(cmd, lv, lv_fast))
		goto_bad;

	log_print_unless_silent("Logical volume %s is now cached.", display_lvname(lv));

	return ECMD_PROCESSED;
 bad:
	return ECMD_FAILED;
}

static int _lvconvert_cachepool_attach_single(struct cmd_context *cmd,
					  struct logical_volume *lv,
					  struct processing_handle *handle)
{
	struct lv_segment *seg;
	struct volume_group *vg = lv->vg;
	struct logical_volume *cachepool_lv;
	const char *cachepool_name;

	if (!(cachepool_name = arg_str_value(cmd, cachepool_ARG, NULL)))
		goto_out;

	if (!validate_lvname_param(cmd, &vg->name, &cachepool_name))
		goto_out;

	if (!(cachepool_lv = find_lv(vg, cachepool_name))) {
		log_error("Cache pool %s not found.", cachepool_name);
		goto out;
	}

	/* Ensure the LV is not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		goto_out;

	/*
	 * If cachepool_lv is not yet a cache pool, convert it to one.
	 * If using an existing cache pool, wipe it.
	 */

	if (!lv_is_cache_pool(cachepool_lv)) {
		int lvt_enum = get_lvt_enum(cachepool_lv);
		struct lv_type *lvtype = get_lv_type(lvt_enum);

		if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) {
			log_error("LV %s with type %s cannot be converted to a cache pool.",
				  display_lvname(cachepool_lv), lvtype ? lvtype->name : "unknown");
			goto out;
		}

		if (lv_is_cache_vol(cachepool_lv)) {
			log_error("LV %s is already used as a cachevol.", display_lvname(cachepool_lv));
			goto out;
		}

		if (cachepool_lv == lv) {
			log_error("Use a different LV for cache pool LV and cache LV %s.",
				  display_lvname(cachepool_lv));
			goto out;
		}

		if (!_lvconvert_to_pool(cmd, cachepool_lv, lv, 0, 1, &vg->pvs)) {
			log_error("LV %s could not be converted to a cache pool.",
				  display_lvname(cachepool_lv));
			goto out;
		}

		/* cachepool_lv is converted into cache-pool data LV */
		if (!(seg = get_only_segment_using_this_lv(cachepool_lv))) {
			log_error(INTERNAL_ERROR "LV %s is not a cache pool data volume.",
				  display_lvname(cachepool_lv));
			goto out;
		}

		cachepool_lv = seg->lv;
	} else {
		if (!dm_list_empty(&cachepool_lv->segs_using_this_lv)) {
			log_error("Cache pool %s is already in use.", cachepool_name);
			goto out;
		}

		/* Note: requires rather deep know-how to skip zeroing */
		if (!arg_is_set(cmd, zero_ARG)) {
		       	if (!arg_is_set(cmd, yes_ARG) &&
			    yes_no_prompt("Do you want wipe existing metadata of cache pool %s? [y/n]: ",
					  display_lvname(cachepool_lv)) == 'n') {
				log_error("Conversion aborted.");
				log_error("To preserve cache metadata add option \"--zero n\".");
				log_warn("WARNING: Reusing mismatched cache pool metadata MAY DESTROY YOUR DATA!");
				goto out;
			}
			/* Wiping confirmed, go ahead */
			if (!wipe_cache_pool(cachepool_lv))
				goto_out;
		} else if (arg_int_value(cmd, zero_ARG, 0)) {
			if (!wipe_cache_pool(cachepool_lv))
				goto_out;
		} else {
			log_warn("WARNING: Reusing cache pool metadata %s for volume caching.",
				 display_lvname(cachepool_lv));
		}
	}

	/* When the lv arg is a thinpool, redirect command to data sub lv. */

	if (lv_is_thin_pool(lv)) {
		lv = seg_lv(first_seg(lv), 0);
		log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv));
	} else if (lv_is_vdo_pool(lv)) {
		lv = seg_lv(first_seg(lv), 0);
		log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv));
	}

	if (!_raid_split_image_conversion(lv))
		goto_out;

	/* Attach the cache to the main LV. */

	if (!_cache_pool_attach(cmd, lv, cachepool_lv))
		goto_out;

	log_print_unless_silent("Logical volume %s is now cached.", display_lvname(lv));

	return ECMD_PROCESSED;
 out:
	return ECMD_FAILED;
}

int lvconvert_to_cache_with_cachepool_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_cachepool_attach_single);
}

static int _lvconvert_to_thin_with_external_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *thinpool_lv;
	const char *thinpool_name;

	if (!(thinpool_name = arg_str_value(cmd, thinpool_ARG, NULL)))
		goto_out;

	if (!validate_lvname_param(cmd, &vg->name, &thinpool_name))
		goto_out;

	if (!(thinpool_lv = find_lv(vg, thinpool_name))) {
		log_error("Thin pool %s not found.", thinpool_name);
		goto out;
	}

	/* If thinpool_lv is not yet a thin pool, convert it to one. */

	if (!lv_is_thin_pool(thinpool_lv)) {
		int lvt_enum = get_lvt_enum(thinpool_lv);
		struct lv_type *lvtype = get_lv_type(lvt_enum);

		if (lvt_enum != striped_LVT && lvt_enum != linear_LVT && lvt_enum != raid_LVT) {
			log_error("LV %s with type %s cannot be converted to a thin pool.",
				  display_lvname(thinpool_lv), lvtype ? lvtype->name : "unknown");
			goto out;
		}

		if (thinpool_lv == lv) {
			log_error("Use a different LV for thin pool LV and thin LV %s.",
				  display_lvname(thinpool_lv));
			goto out;
		}

		if (!_lvconvert_to_pool(cmd, thinpool_lv, lv, 1, 0, &vg->pvs)) {
			log_error("LV %s could not be converted to a thin pool.",
				  display_lvname(thinpool_lv));
			goto out;
		}

		if (!(thinpool_lv = find_lv(vg, thinpool_name))) {
			log_error(INTERNAL_ERROR "LV %s cannot be found.", thinpool_name);
			goto out;
		}

		if (!lv_is_thin_pool(thinpool_lv)) {
			log_error(INTERNAL_ERROR "LV %s is not a thin pool.", display_lvname(thinpool_lv));
			goto out;
		}
	}

	/* If lv is a cache volume, all data must be flushed. */

	if (lv_is_cache(lv)) {
		const struct lv_segment *pool_seg = first_seg(first_seg(lv)->pool_lv);
		int is_clean;

		if (pool_seg->cache_mode != CACHE_MODE_WRITETHROUGH) {
			log_error("Cannot convert cache volume %s with %s cache mode to external origin.",
				  display_lvname(lv), get_cache_mode_name(pool_seg));
			log_error("To proceed, run 'lvchange --cachemode writethrough %s'.",
				  display_lvname(lv));
			goto out;
		}

		if (!lv_cache_wait_for_clean(lv, &is_clean))
			goto_out;

		if (!is_clean) {
			log_error("Cache %s is not clean, refusing to convert to external origin.",
				  display_lvname(lv));
			goto out;
		}
	}

	/* Convert lv to thin with external origin using thinpool_lv. */

	if (!_lvconvert_to_thin_with_external(cmd, lv, thinpool_lv))
		goto_out;

	return ECMD_PROCESSED;

 out:
	return ECMD_FAILED;
}

int lvconvert_to_thin_with_external_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_to_thin_with_external_single);
}

static int _lvconvert_swap_pool_metadata_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *metadata_lv;
	const char *metadata_name;

	if (vg_is_shared(lv->vg)) {
		/* FIXME: need to swap locks betwen LVs? */
		log_error("Unable to swap pool metadata in VG with lock_type %s", lv->vg->lock_type);
		goto out;
	}

	if (!(metadata_name = arg_str_value(cmd, poolmetadata_ARG, NULL)))
		goto_out;

	if (!validate_lvname_param(cmd, &vg->name, &metadata_name))
		goto_out;

	if (!(metadata_lv = find_lv(vg, metadata_name))) {
		log_error("Metadata LV %s not found.", metadata_name);
		goto out;
	}

	if (metadata_lv == lv) {
		log_error("Can't use same LV for pool data and metadata LV %s.",
			  display_lvname(metadata_lv));
		goto out;
	}

	if (!_lvconvert_swap_pool_metadata(cmd, lv, metadata_lv))
		goto_out;

	return ECMD_PROCESSED;

 out:
	return ECMD_FAILED;
}

int lvconvert_swap_pool_metadata_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_swap_pool_metadata_single);
}

static int _lvconvert_to_pool_or_swap_metadata_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	struct dm_list *use_pvh = NULL;
	int to_thinpool = 0;
	int to_cachepool = 0;
	int lvt_enum = get_lvt_enum(lv);
	struct lv_type *lvtype;

	switch (cmd->command->command_enum) {
	case lvconvert_to_thinpool_or_swap_metadata_CMD:
		if (lv_is_cache(lv) || lv_is_writecache(lv))
			/* For cached LV check the cache origin LV type */
			lvt_enum = get_lvt_enum(seg_lv(first_seg(lv), 0));
		to_thinpool = 1;
		break;
	case lvconvert_to_cachepool_or_swap_metadata_CMD:
		if (lv_is_cache(lv))
			goto_bad; /* Cache over cache is not supported */
		to_cachepool = 1;
		break;
	default:
		log_error(INTERNAL_ERROR "Invalid lvconvert pool command.");
		return ECMD_FAILED;
	}

	switch (lvt_enum) {
	case thinpool_LVT:
		if (!to_thinpool)
			goto_bad; /* can't accept cache-pool */
		break; /* swap thin-pool */
	case cachepool_LVT:
		if (!to_cachepool)
			goto_bad; /* can't accept thin-pool */
		break; /* swap cache-pool */
	case linear_LVT:
	case raid_LVT:
	case striped_LVT:
	case zero_LVT:
		break;
	default:
bad:
		lvtype = get_lv_type(lvt_enum);
		log_error("LV %s with type %s cannot be used as a %s pool LV.",
			  display_lvname(lv), lvtype ? lvtype->name : "unknown",
			  to_thinpool ? "thin" : "cache");
		return ECMD_FAILED;
	}

	if (lv_is_origin(lv)) {
		log_error("Cannot convert logical volume %s under snapshot.",
			  display_lvname(lv));
		return ECMD_FAILED;
	}

	if (!lv_is_visible(lv)) {
		log_error("Can't convert internal LV %s.",
			  display_lvname(lv));
		return ECMD_FAILED;
	}

	if (lv_is_locked(lv)) {
		log_error("Can't convert locked LV %s.",
			  display_lvname(lv));
		return ECMD_FAILED;
	}

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
	} else
		use_pvh = &lv->vg->pvs;

	/*
	 * We can finally determine if this command is supposed to create
	 * a pool or swap the metadata in an existing pool.
	 *
	 * This allows the ambiguous command:
	 * 'lvconvert --thinpool LV1 --poolmetadata LV2' to mean either:
	 * 1. convert LV2 to a pool using the specified meta LV2
	 * 2. swap the meta lv in LV1 with LV2
	 *
	 * In case 2, the poolmetadata option is required, but in case 1
	 * it is optional.  So, the command def is not able to validate
	 * the required/optional option, and we have to check here
	 * for missing poolmetadata in case 2.
	 */
	if (lv_is_pool(lv)) {
		if (!arg_is_set(cmd, poolmetadata_ARG)) {
			log_error("The --poolmetadata option is required to swap metadata.");
			return ECMD_FAILED;
		}
		return _lvconvert_swap_pool_metadata_single(cmd, lv, handle);
	}

	if (!_lvconvert_to_pool(cmd, lv, lv, to_thinpool, to_cachepool, use_pvh))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

/*
 * In the command variants with no position LV arg, the LV arg is taken from
 * the --thinpool/--cachepool arg, and the position args are modified to match
 * the standard command form.
 */

int lvconvert_to_pool_or_swap_metadata_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	char *pool_data_name;
	int i, p;

	switch (cmd->command->command_enum) {
	case lvconvert_to_thinpool_or_swap_metadata_CMD:
		pool_data_name = (char *)arg_str_value(cmd, thinpool_ARG, NULL);
		break;
	case lvconvert_to_cachepool_or_swap_metadata_CMD:
		pool_data_name = (char *)arg_str_value(cmd, cachepool_ARG, NULL);
		break;
	default:
		log_error(INTERNAL_ERROR "Unknown pool conversion.");
		return 0;
	};

	/* Make the LV the first position arg. */

	p = cmd->position_argc;
	for (i = 0; i < cmd->position_argc; i++)
		cmd->position_argv[p] = cmd->position_argv[p-1];

	cmd->position_argv[0] = pool_data_name;
	cmd->position_argc++;

	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_to_pool_or_swap_metadata_single);
}

static int _lvconvert_merge_thin_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	if (!_lvconvert_merge_thin_snapshot(cmd, lv))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_merge_thin_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_merge_thin_single);
}

static int _lvconvert_detach_writecache(struct cmd_context *cmd, struct processing_handle *handle,
					struct logical_volume *lv,
					struct logical_volume *lv_fast);
static int _lvconvert_detach_writecache_when_clean(struct cmd_context *cmd,
						   struct lvconvert_result *lr);

static int _lvconvert_split_cache_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	struct logical_volume *lv_main = NULL;
	struct logical_volume *lv_fast = NULL;
	struct lv_segment *seg;
	int ret = 0;

	if (lv_is_writecache(lv)) {
		lv_main = lv;
		lv_fast = first_seg(lv_main)->writecache;

	} else if (lv_is_cache(lv)) {
		lv_main = lv;
		lv_fast = first_seg(lv_main)->pool_lv;

	} else if (lv_is_cache_pool(lv)) {
		lv_fast = lv;

		if ((dm_list_size(&lv_fast->segs_using_this_lv) == 1) &&
		    (seg = get_only_segment_using_this_lv(lv_fast)) &&
		    seg_is_cache(seg))
			lv_main = seg->lv;

	} else if (lv_is_thin_pool(lv)) {
		lv_main = seg_lv(first_seg(lv), 0); /* cached _tdata */
		lv_fast = first_seg(lv_main)->pool_lv;

	} else if (lv_is_vdo_pool(lv)) {
		lv_main = seg_lv(first_seg(lv), 0); /* cached _vdata */
		lv_fast = first_seg(lv_main)->pool_lv;
	}

	if (!lv_main) {
		log_error("Cannot find LV with cache from %s.", display_lvname(lv));
		return ECMD_FAILED;
	}

	if (!lv_fast) {
		log_error("Cannot find cache %s.", display_lvname(lv));
		return ECMD_FAILED;
	}

	/* If LV is inactive here, ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv_main, "ex", 0))
		return_ECMD_FAILED;

	if (lv_is_writecache(lv_main)) {
		if (!_lvconvert_detach_writecache(cmd, handle, lv_main, lv_fast))
			return_ECMD_FAILED;

		if (cmd->command->command_enum == lvconvert_split_and_remove_cache_CMD) {
			struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle;
			/*
			 * If detach is ongoing, then the remove needs to wait
			 * until _lvconvert_detach_writecache_when_clean(),
			 * after the detach has finished. When lr->remove_cache
			 * has been set, when_clean() knows it should remove
			 * lv_fast at the end.
			 */
			if (!lr->wait_cleaner_writecache) {
				if (lvremove_single(cmd, lv_fast, NULL) != ECMD_PROCESSED)
					return_ECMD_FAILED;
			}
		}
		ret = 1;
	} else if (lv_is_cache(lv_main) && lv_is_cache_vol(lv_fast)) {
		if (cmd->command->command_enum == lvconvert_split_and_remove_cache_CMD) {
			ret = _lvconvert_split_and_remove_cachevol(cmd, lv_main, lv_fast);

			log_print_unless_silent("Logical volume %s is not cached and %s is removed.",
						display_lvname(lv), display_lvname(lv_fast));

		} else if (cmd->command->command_enum == lvconvert_split_and_keep_cache_CMD) {
			ret = _lvconvert_split_and_keep_cachevol(cmd, lv_main, lv_fast);

			log_print_unless_silent("Logical volume %s is not cached and %s is unused.",
						display_lvname(lv), display_lvname(lv_fast));

		} else
			log_error(INTERNAL_ERROR "Unknown cache split command.");

	} else if (lv_is_cache(lv_main) && lv_is_cache_pool(lv_fast)) {
		if (cmd->command->command_enum == lvconvert_split_and_remove_cache_CMD)
			ret = _lvconvert_split_and_remove_cachepool(cmd, lv_main, lv_fast);

		else if (cmd->command->command_enum == lvconvert_split_and_keep_cache_CMD)
			ret = _lvconvert_split_and_keep_cachepool(cmd, lv_main, lv_fast);

		else
			log_error(INTERNAL_ERROR "Unknown cache split command.");
	} else
		log_error(INTERNAL_ERROR "Unknown cache split command.");

	if (!ret)
		return ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_split_cache_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	int ret;

	cmd->handles_missing_pvs = 1;
	cmd->partial_activation = 1;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       handle, NULL, &_lvconvert_split_cache_single);

	destroy_processing_handle(cmd, handle);

	if (ret == ECMD_FAILED)
		return ret;

	if (lr.wait_cleaner_writecache)
		if (!_lvconvert_detach_writecache_when_clean(cmd, &lr))
			ret = ECMD_FAILED;

	return ret;
}

static int _lvconvert_raid_types_single(struct cmd_context *cmd, struct logical_volume *lv,
			     struct processing_handle *handle)
{
	struct lvconvert_params *lp = (struct lvconvert_params *) handle->custom_handle;
	struct dm_list *use_pvh;
	struct convert_poll_id_list *idl;
	int ret;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
		lp->pv_count = cmd->position_argc - 1;
	} else
		use_pvh = &lv->vg->pvs;

	lp->pvh = use_pvh;

	lp->lv_to_poll = lv;

	ret = _lvconvert_raid_types(cmd, lv, lp);

	if (ret != ECMD_PROCESSED)
		return_ECMD_FAILED;

	if (lp->need_polling) {
		/* _lvconvert() call may alter the reference in lp->lv_to_poll */
		if (!lv_is_active(lp->lv_to_poll))
			log_print_unless_silent("Conversion starts after activation.");
		else {
			if (!(idl = _convert_poll_id_list_create(cmd, lp->lv_to_poll)))
				return_ECMD_FAILED;
			dm_list_add(&lp->idls, &idl->list);
		}
	}

	return ECMD_PROCESSED;
}

static int _lvconvert_raid_types_check(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle,
			int lv_is_named_arg)
{
	int lvt_enum = get_lvt_enum(lv);
	struct lv_type *lvtype = get_lv_type(lvt_enum);

	if (!lv_is_visible(lv)) {
		if (!lv_is_cache_pool_metadata(lv) &&
		    !lv_is_cache_pool_data(lv) &&
		    !lv_is_thin_pool_metadata(lv) &&
		    !lv_is_thin_pool_data(lv) &&
		    !lv_is_vdo_pool_data(lv) &&
		    !lv_is_used_cache_pool(lv) &&
		    !lv_is_mirrored(lv) &&
		    !lv_is_raid(lv))
			goto fail_hidden;
	}

	/*
	 * FIXME: this validation could be done by command defs.
	 *
	 * Outside the standard linear/striped/mirror/raid LV
	 * types, cache is the only special LV type that is handled
	 * (the command is redirected to origin).
	 */
	switch (lvt_enum) {
	case thin_LVT:
	case thinpool_LVT:
	case cachepool_LVT:
	case snapshot_LVT:
		log_error("Operation not permitted on LV %s type %s.",
			  display_lvname(lv), lvtype ? lvtype->name : "unknown");
		return 0;
	}

	return 1;

 fail_hidden:
	log_error("Operation not permitted on hidden LV %s.", display_lvname(lv));
	return 0;
}

int lvconvert_raid_types_cmd(struct cmd_context * cmd, int argc, char **argv)
{
	int poll_ret, ret;
	int saved_ignore_suspended_devices;
	struct processing_handle *handle;
	struct convert_poll_id_list *idl;
	struct lvconvert_params lp = {
		.conv_type = CONV_OTHER,
		.target_attr = ~0,
		.idls = DM_LIST_HEAD_INIT(lp.idls),
	};

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lp;

	if (!_read_params(cmd, &lp)) {
		ret = EINVALID_CMD_LINE;
		goto_out;
	}

	saved_ignore_suspended_devices = ignore_suspended_devices();

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, &_lvconvert_raid_types_check, &_lvconvert_raid_types_single);

	init_ignore_suspended_devices(saved_ignore_suspended_devices);

	dm_list_iterate_items(idl, &lp.idls) {
		poll_ret = _lvconvert_poll_by_id(cmd, idl->id,
						 lp.wait_completion ? 0 : 1U,
						 idl->is_merging_origin,
						 idl->is_merging_origin_thin);
		if (poll_ret > ret)
			ret = poll_ret;
	}

out:
	destroy_processing_handle(cmd, handle);

	return ret;
}

/*
 * change mirror log
 */

static int _lvconvert_visible_check(struct cmd_context *cmd, struct logical_volume *lv,
			struct processing_handle *handle,
			int lv_is_named_arg)
{
	if (!lv_is_visible(lv)) {
		log_error("Operation not permitted on hidden LV %s.", display_lvname(lv));
		return ECMD_FAILED;
	}

	return ECMD_PROCESSED;
}

static int _lvconvert_change_mirrorlog_single(struct cmd_context *cmd, struct logical_volume *lv,
			     struct processing_handle *handle)
{
	struct lvconvert_params *lp = (struct lvconvert_params *) handle->custom_handle;
	struct dm_list *use_pvh;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
		lp->pv_count = cmd->position_argc - 1;
	} else
		use_pvh = &lv->vg->pvs;

	lp->pvh = use_pvh;

	/* FIXME: extract the mirrorlog functionality out of _lvconvert_raid_types()? */
	return _lvconvert_raid_types(cmd, lv, lp);
}

int lvconvert_change_mirrorlog_cmd(struct cmd_context * cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_params lp = {
		.conv_type = CONV_OTHER,
		.target_attr = ~0,
		.idls = DM_LIST_HEAD_INIT(lp.idls),
	};
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lp;

	/* FIXME: extract the relevant bits of read_params and put here. */
	if (!_read_params(cmd, &lp)) {
		ret = EINVALID_CMD_LINE;
		goto_out;
	}

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, &_lvconvert_visible_check, &_lvconvert_change_mirrorlog_single);

out:
	destroy_processing_handle(cmd, handle);

	return ret;
}

static int _lvconvert_change_region_size_single(struct cmd_context *cmd, struct logical_volume *lv,
			     struct processing_handle *handle)
{
	if (!lv_raid_change_region_size(lv, arg_is_set(cmd, yes_ARG), arg_count(cmd, force_ARG),
			                arg_int_value(cmd, regionsize_ARG, 0)))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_change_region_size_cmd(struct cmd_context * cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      NULL, &_lvconvert_visible_check, &_lvconvert_change_region_size_single);
}

/*
 * split mirror images
 */

static int _lvconvert_split_mirror_images_single(struct cmd_context *cmd, struct logical_volume *lv,
			     struct processing_handle *handle)
{
	struct lvconvert_params *lp = (struct lvconvert_params *) handle->custom_handle;
	struct dm_list *use_pvh;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, lv->vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_ECMD_FAILED;
		lp->pv_count = cmd->position_argc - 1;
	} else
		use_pvh = &lv->vg->pvs;

	lp->pvh = use_pvh;

	/* FIXME: extract the split functionality out of _lvconvert_raid_types()? */
	return _lvconvert_raid_types(cmd, lv, lp);
}

int lvconvert_split_mirror_images_cmd(struct cmd_context * cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_params lp = {
		.conv_type = CONV_OTHER,
		.target_attr = ~0,
		.idls = DM_LIST_HEAD_INIT(lp.idls),
	};
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lp;

	/* FIXME: extract the relevant bits of read_params and put here. */
	if (!_read_params(cmd, &lp)) {
		ret = EINVALID_CMD_LINE;
		goto_out;
	}

	/* FIXME: are there any hidden LVs that should be disallowed? */

	ret = process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_split_mirror_images_single);

out:
	destroy_processing_handle(cmd, handle);

	return ret;
}

/*
 * merge mirror images
 *
 * Called from both lvconvert --mergemirrors and lvconvert --merge.
 */

static int _lvconvert_merge_mirror_images_single(struct cmd_context *cmd,
                                          struct logical_volume *lv,
                                          struct processing_handle *handle)
{
	if (!lv_raid_merge(lv))
		return_ECMD_FAILED;

	return ECMD_PROCESSED;
}

int lvconvert_merge_mirror_images_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	/* arg can be a VG name, which is the standard option usage */
	cmd->cname->flags &= ~GET_VGNAME_FROM_OPTIONS;

	return process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, &_lvconvert_visible_check, &_lvconvert_merge_mirror_images_single);
}

static int _lvconvert_merge_generic_single(struct cmd_context *cmd,
					 struct logical_volume *lv,
					 struct processing_handle *handle)
{
	int ret;

	if (lv_is_cow(lv))
		ret = _lvconvert_merge_snapshot_single(cmd, lv, handle);

	else if (lv_is_thin_volume(lv))
		ret = _lvconvert_merge_thin_single(cmd, lv, handle);

	else
		ret = _lvconvert_merge_mirror_images_single(cmd, lv, handle);

	return ret;
}

int lvconvert_merge_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	struct convert_poll_id_list *idl;
	int ret, poll_ret;

	dm_list_init(&lr.poll_idls);

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	cmd->cname->flags &= ~GET_VGNAME_FROM_OPTIONS;

	ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			      handle, NULL, &_lvconvert_merge_generic_single);

	/* polling is only used by merge_snapshot */
	if (lr.need_polling) {
		dm_list_iterate_items(idl, &lr.poll_idls) {
			poll_ret = _lvconvert_poll_by_id(cmd, idl->id,
						arg_is_set(cmd, background_ARG), 1, 0);
			if (poll_ret > ret)
				ret = poll_ret;
		}
	}

	destroy_processing_handle(cmd, handle);

	return ret;
}

static int _lvconvert_to_vdopool_single(struct cmd_context *cmd,
					struct logical_volume *lv,
					struct processing_handle *handle)
{
	const char *vg_name = NULL;
	unsigned int vdo_pool_zero;
	uint64_t vdo_pool_header_size;
	struct volume_group *vg = lv->vg;
	struct logical_volume *vdo_lv;
	struct dm_vdo_target_params vdo_params; /* vdo */
	struct lvcreate_params lvc = {
		.activate = CHANGE_AEY,
		.alloc = ALLOC_INHERIT,
		.major = -1,
		.minor = -1,
		.suppress_zero_warn = 1, /* Suppress warning for this VDO */
		.permission = LVM_READ | LVM_WRITE,
		.pool_name = lv->name,
		.pvh = &vg->pvs,
		.read_ahead = arg_uint_value(cmd, readahead_ARG, DM_READ_AHEAD_AUTO),
		.stripes = 1,
		.lv_name = arg_str_value(cmd, name_ARG, NULL),
	};

	if (lvc.lv_name &&
	    !validate_restricted_lvname_param(cmd, &vg_name, &lvc.lv_name))
		goto_out;

	lvc.virtual_extents = extents_from_size(cmd,
						arg_uint64_value(cmd, virtualsize_ARG, UINT64_C(0)),
						vg->extent_size);

	if (!(lvc.segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_VDO)))
		goto_out;

	if (activation() && lvc.segtype->ops->target_present) {
		if (!lvc.segtype->ops->target_present(cmd, NULL, &lvc.target_attr)) {
			log_error("%s: Required device-mapper target(s) not detected in your kernel.",
				  lvc.segtype->name);
			goto out;
		}
	}

	if (vg_is_shared(vg)) {
		/* FIXME: need to swap locks betwen LVs? */
		log_error("Unable to convert VDO pool in VG with lock_type %s", vg->lock_type);
		goto out;
	}

	if (!fill_vdo_target_params(cmd, &vdo_params, &vdo_pool_header_size, vg->profile))
		goto_out;

	if (!get_vdo_settings(cmd, &vdo_params, NULL))
		goto_out;

	if (!activate_lv(cmd, lv)) {
		log_error("Cannot activate %s.", display_lvname(lv));
		goto out;
	}

	vdo_pool_zero = arg_int_value(cmd, zero_ARG, 1);

	log_warn("WARNING: Converting logical volume %s to VDO pool volume %s formatting.",
		 display_lvname(lv), vdo_pool_zero ? "with" : "WITHOUT");

	if (vdo_pool_zero)
		log_warn("THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)");
	else
		log_warn("WARNING: Using invalid VDO pool data MAY DESTROY YOUR DATA!");

	if (!arg_count(cmd, yes_ARG) &&
	    yes_no_prompt("Do you really want to convert %s? [y/n]: ",
			  display_lvname(lv)) == 'n') {
		log_error("Conversion aborted.");
		goto out;
	}

	if (vdo_pool_zero) {
		if (test_mode()) {
			log_verbose("Test mode: Skipping activation, zeroing and signature wiping.");
		} else if (!wipe_lv(lv, (struct wipe_params) { .do_zero = 1, .do_wipe_signatures = 1,
			     .yes = arg_count(cmd, yes_ARG),
			     .force = arg_count(cmd, force_ARG)})) {
			log_error("Aborting. Failed to wipe VDO data store.");
			goto out;
		}
	}

	if (!convert_vdo_pool_lv(lv, &vdo_params, &lvc.virtual_extents,
				 vdo_pool_zero, vdo_pool_header_size))
		goto_out;

	dm_list_init(&lvc.tags);

	if (!(vdo_lv = lv_create_single(vg, &lvc)))
		goto_out; /* FIXME: hmmm what to do now */

	log_print_unless_silent("Converted %s to VDO pool volume and created virtual %s VDO volume.",
				display_lvname(lv), display_lvname(vdo_lv));

	return ECMD_PROCESSED;

 out:
	return ECMD_FAILED;
}

int lvconvert_to_vdopool_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_to_vdopool_single);
}

int lvconvert_to_vdopool_param_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	/* Make the LV the first position arg. */
	int i, p = cmd->position_argc;

	for (i = 0; i < cmd->position_argc; i++)
		cmd->position_argv[p] = cmd->position_argv[p-1];

	cmd->position_argv[0] = (char *)arg_str_value(cmd, vdopool_ARG, NULL);
	cmd->position_argc++;

	return process_each_lv(cmd, 1, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE,
			       NULL, NULL, &_lvconvert_to_vdopool_single);
}

/*
 * Starts the detach process, and may complete it, or may defer the completion
 * if cleaning is required, by returning a poll id.  If deferred, the caller
 * will notice the poll id and call lvconvert_detach_writecache_when_clean
 * to wait for the cleaning and complete the detach.  The command can be cancelled
 * while waiting for cleaning and the same command be repeated to continue the
 * process.
 */
static int _lvconvert_detach_writecache(struct cmd_context *cmd,
					struct processing_handle *handle,
					struct logical_volume *lv,
					struct logical_volume *lv_fast)
{
	struct lvconvert_result *lr = (struct lvconvert_result *) handle->custom_handle;
	struct writecache_settings settings;
	struct convert_poll_id_list *idl;
	uint32_t block_size_sectors;
	int active_begin = 0;
	int active_clean = 0;
	int is_clean = 0;
	int noflush = 0;

	dm_list_init(&lr->poll_idls);

	memset(&settings, 0, sizeof(settings));

	if (!get_writecache_settings(cmd, &settings, &block_size_sectors)) {
		log_error("Invalid writecache settings.");
		return 0;
	}

	if (!archive(lv->vg))
		return_0;

	/*
	 * If the LV is inactive when we begin, then we want to
	 * deactivate the LV at the end.
	 */
	active_begin = lv_is_active(lv);

	if (lv_is_partial(lv_fast) || (!active_begin && arg_count(cmd, force_ARG))) {
		if (!arg_count(cmd, force_ARG)) {
			log_warn("WARNING: writecache on %s is not complete and cannot be flushed.", display_lvname(lv_fast));
			log_warn("WARNING: cannot detach writecache from %s without --force.", display_lvname(lv));
			log_error("Conversion aborted.");
			return 0;
		}

		log_warn("WARNING: Data may be lost by detaching writecache without flushing.");

		if (!arg_count(cmd, yes_ARG) &&
		     yes_no_prompt("Detach writecache %s from %s without flushing data?",
				   display_lvname(lv_fast), display_lvname(lv)) == 'n') {
			log_error("Conversion aborted.");
			return 0;
		}
		
		noflush = 1;
	}

	if (!noflush) {
		/*
		 * --cachesettings cleaner=0 means to skip the use of the cleaner
		 * and go directly to detach which will use a flush message.
		 * (This is currently the only cachesetting used during detach.)
		 */
		if (settings.cleaner_set && !settings.cleaner) {
			log_print_unless_silent("Detaching writecache skipping cleaner...");
			goto detach;
		}

		if (!writecache_cleaner_supported(cmd)) {
			log_print_unless_silent("Detaching writecache without cleaner...");
			goto detach;
		}

		if (!active_begin && !activate_lv(cmd, lv)) {
			log_error("Failed to activate LV to clean writecache.");
			return 0;
		}
		active_clean = 1;

		/*
		 * If the user ran this command previously (or set cleaner
		 * directly) the cache may already be empty and ready for
		 * detach.
		 */
		if (lv_writecache_is_clean(cmd, lv, NULL)) {
			log_print_unless_silent("Detaching writecache already clean.");
			is_clean = 1;
			goto detach;
		}

		/*
		 * If the user has not already done lvchange --cachesettings cleaner=1
		 * then do that here.  If the LV is inactive, this activates it
		 * so that cache writeback can be done.
		 */
		log_print_unless_silent("Detaching writecache setting cleaner.");

		if (!lv_writecache_set_cleaner(lv)) {
			log_error("Failed to set cleaner cachesetting to flush cache.");
			log_error("See lvchange --cachesettings cleaner=1");

			if (!active_begin && active_clean && !deactivate_lv(cmd, lv))
				stack;
			return 0;
		}

		/*
		 * The cache may have been nearly clean and will be empty with
		 * a short dely.
		 */
		usleep(10000);
		if (lv_writecache_is_clean(cmd, lv, NULL)) {
			log_print_unless_silent("Detaching writecache finished cleaning.");
			is_clean = 1;
			goto detach;
		}

		if (!(idl = _convert_poll_id_list_create(cmd, lv))) {
			log_error("Failed to monitor writecache cleaner progress.");
			return 0;
		}

		/*
		 * Monitor the writecache status until the cache is unused.
		 * This is done at the end of the command where locks are not
		 * held since the writeback can take some time.
		 */
		lr->wait_cleaner_writecache = 1;
		lr->active_begin = active_begin;

		/* The command wants to remove the cache after detaching. */
		if (cmd->command->command_enum == lvconvert_split_and_remove_cache_CMD)
			lr->remove_cache = 1;

		dm_list_add(&lr->poll_idls, &idl->list);
		return 1;
	}

 detach:

	/*
	 * If the LV was inactive before cleaning and activated to do cleaning,
	 * then deactivate before the detach.
	 */
	if (!active_begin && active_clean && !deactivate_lv(cmd, lv))
		stack;

	if (is_clean)
		noflush = 1;

	if (!lv_detach_writecache_cachevol(lv, noflush))
		return_0;

	log_print_unless_silent("Logical volume %s writecache has been detached.",
				display_lvname(lv));
	return 1;
}

/*
 * _lvconvert_detach_writecache() set the cleaner option for the LV
 * so writecache will begin writing back data from cache to origin.
 * It then saved the LV name/id (lvconvert_result/poll_id), and
 * exited process_each_lv (releasing the VG and VG lock).  Then
 * this is called to monitor the progress of the cache writeback.
 * When the cache is clean, this does the detach (writecache is removed
 * in metadata and LV in kernel is updated.)
 */
static int _lvconvert_detach_writecache_when_clean(struct cmd_context *cmd,
						   struct lvconvert_result *lr)
{
	struct convert_poll_id_list *idl;
	struct poll_operation_id *id;
	struct volume_group *vg;
	struct logical_volume *lv;
	struct logical_volume *lv_fast;
	uint32_t lockd_state, error_flags;
	uint64_t dirty;
	int ret = 0;

	idl = dm_list_item(dm_list_first(&lr->poll_idls), struct convert_poll_id_list);
	id = idl->id;

	/*
	 * TODO: we should be able to save info about the dm device for this LV
	 * and monitor the dm device status without doing vg lock/read around
	 * each check.  The vg lock/read/write would then happen only once when
	 * status was finished and we want to finish the detach.  If the dm
	 * device goes away while monitoring, it's no different and no worse
	 * than the LV going away here.
	 */

 retry:
	lockd_state = 0;
	error_flags = 0;

	if (!lockd_vg(cmd, id->vg_name, "ex", 0, &lockd_state)) {
		log_error("Detaching writecache interrupted - locking VG failed.");
		return 0;
	}

	log_debug("detach writecache check clean reading vg %s", id->vg_name);

	vg = vg_read(cmd, id->vg_name, NULL, READ_FOR_UPDATE, lockd_state, &error_flags, NULL);

	if (!vg) {
		log_error("Detaching writecache interrupted - reading VG failed.");
		goto out_lockd;
	}

	if (error_flags) {
		log_error("Detaching writecache interrupted - reading VG error %x.", error_flags);
		goto out_release;
	}

	lv = find_lv(vg, id->lv_name);

	if (lv && id->uuid && strcmp(id->uuid, (char *)&lv->lvid))
		lv = NULL;

	if (!lv) {
		log_error("Detaching writecache interrupted - LV not found.");
		goto out_release;
	}

	if (!lv_is_active(lv)) {
		log_error("Detaching writecache interrupted - LV not active.");
		goto out_release;
	}

	if (!lv_writecache_is_clean(cmd, lv, &dirty)) {
		unlock_and_release_vg(cmd, vg, vg->name);

		if (!lockd_vg(cmd, id->vg_name, "un", 0, &lockd_state))
			stack;

		log_print_unless_silent("Detaching writecache cleaning %llu blocks", (unsigned long long)dirty);
		log_print_unless_silent("This command can be cancelled and rerun to complete writecache detach.");
		sleep(5);
		goto retry;
	}

	if (!lr->active_begin) {
		/*
		 * The LV was not active to begin so we should leave it inactive at the end.
		 * It will remain inactive during detach since it's clean and doesn't need
		 * a flush message.
		 */
		if (!deactivate_lv(cmd, lv))
			stack;
	}

	log_print_unless_silent("Detaching writecache completed cleaning.");

	lv_fast = first_seg(lv)->writecache;

	/*
	 * When the cleaner has finished, we can detach with noflush since
	 * the cleaner has done the flushing.
	 */

	if (!lv_detach_writecache_cachevol(lv, 1)) {
		log_error("Detaching writecache cachevol failed.");
		goto out_release;
	}

	/*
	 * The detach was started by an uncache command that wants to remove
	 * the cachevol after detaching.
	 */
	if (lr->remove_cache) {
		if (lvremove_single(cmd, lv_fast, NULL) != ECMD_PROCESSED) {
			log_error("Removing the writecache cachevol failed.");
			goto out_release;
		}
	}

	ret = 1;

out_release:
	if (ret)
		log_print_unless_silent("Logical volume %s write cache has been detached.", display_lvname(lv));

	unlock_and_release_vg(cmd, vg, vg->name);

out_lockd:
	if (!lockd_vg(cmd, id->vg_name, "un", 0, &lockd_state))
		stack;

	return ret;
}

static int _writecache_zero(struct cmd_context *cmd, struct logical_volume *lv)
{
	struct wipe_params wp = {
		.do_wipe_signatures = 1, /* optional, to print warning if clobbering something */
		.do_zero = 1,            /* required for dm-writecache to work */
		.yes = arg_count(cmd, yes_ARG),
		.force = arg_count(cmd, force_ARG)
	};
	int ret;

	if (!(lv->status & LVM_WRITE)) {
		log_error("Cannot initialize readonly LV %s", display_lvname(lv));
		return 0;
	}

	if (test_mode())
		return 1;

	if (!activate_lv(cmd, lv)) {
		log_error("Failed to activate LV %s for zeroing.", display_lvname(lv));
		return 0;
	}

	if (!(ret = wipe_lv(lv, wp)))
		stack;

	if (!deactivate_lv(cmd, lv)) {
		log_error("Failed to deactivate LV %s for zeroing.", display_lvname(lv));
		ret = 0;
	}

	return ret;
}

static struct logical_volume *_lv_writecache_create(struct cmd_context *cmd,
					    struct logical_volume *lv,
					    struct logical_volume *lv_fast,
					    uint32_t block_size_sectors,
					    struct writecache_settings *settings)
{
	struct logical_volume *lv_wcorig;
	const struct segment_type *segtype;
	struct lv_segment *seg;

	/* should lv_fast get a new status flag indicating it's the cache in a writecache LV? */

	if (!(segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_WRITECACHE)))
		return_NULL;

	lv->status |= WRITECACHE;

	/*
	 * "lv_wcorig" is a new LV with new id, but with the segments from "lv".
	 * "lv" keeps the existing name and id, but gets a new writecache segment,
	 * in place of the segments that were moved to lv_wcorig.
	 */

	if (!(lv_wcorig = insert_layer_for_lv(cmd, lv, WRITECACHE, "_wcorig")))
		return_NULL;

	lv_set_hidden(lv_fast);

	seg = first_seg(lv);
	seg->segtype = segtype;

	seg->writecache = lv_fast;

	/* writecache_block_size is in bytes */
	seg->writecache_block_size = block_size_sectors * 512;

	memcpy(&seg->writecache_settings, settings, sizeof(struct writecache_settings));

	if (!add_seg_to_segs_using_this_lv(lv_fast, seg))
		return_NULL;

	return lv_wcorig;
}

/*
 * Currently only supports writecache block sizes 512 and 4096.
 * This could be expanded later.
 */
static int _set_writecache_block_size(struct cmd_context *cmd,
				      struct logical_volume *lv,
				      uint32_t *block_size_sectors)
{
	char pathname[PATH_MAX];
	struct dm_list pvs_list;
	struct pv_list *pvl;
	uint32_t fs_block_size = 0;
	uint32_t block_size_setting = 0;
	uint32_t block_size = 0;
	int lbs_unknown = 0, lbs_4k = 0, lbs_512 = 0;
	int pbs_unknown = 0, pbs_4k = 0, pbs_512 = 0;
	int rv = 0;

	/* This is set if the user specified a writecache block size on the command line. */
	if (*block_size_sectors)
		block_size_setting = *block_size_sectors * 512;

	dm_list_init(&pvs_list);

	if (!get_pv_list_for_lv(cmd->mem, lv, &pvs_list)) {
		log_error("Failed to build list of PVs for %s.", display_lvname(lv));
		goto bad;
	}

	dm_list_iterate_items(pvl, &pvs_list) {
		unsigned int pbs = 0;
		unsigned int lbs = 0;

		if (!dev_get_direct_block_sizes(pvl->pv->dev, &pbs, &lbs)) {
			lbs_unknown++;
			pbs_unknown++;
			continue;
		}

		if (lbs == 4096)
			lbs_4k++;
		else if (lbs == 512)
			lbs_512++;
		else
			lbs_unknown++;

		if (pbs == 4096)
			pbs_4k++;
		else if (pbs == 512)
			pbs_512++;
		else
			pbs_unknown++;
	}

	if (lbs_4k && lbs_512) {
		log_error("Writecache requires consistent logical block size for LV devices.");
		goto bad;
	}

	if (lbs_4k && block_size_setting && (block_size_setting < 4096)) {
		log_error("Writecache block size %u not allowed with device logical block size 4096.",
			  block_size_setting);
		goto bad;
	}

	/*
	 * When attaching writecache to thin pool data, the fs block sizes
	 * would need to be checked on each thin LV which isn't practical, so
	 * default to 512, and require the user to specify 4k when appropriate.
	 */
	if (lv_is_thin_pool(lv) || lv_is_thin_pool_data(lv)) {
		if (block_size_setting)
			block_size = block_size_setting;
		else
			block_size = 512;

		log_print_unless_silent("Using writecache block size %u for thin pool data, logical block size %u, physical block size %u.",
					block_size, lbs_4k ? 4096 : 512, pbs_4k ? 4096 : 512);

		goto out;
	}

	if (dm_snprintf(pathname, sizeof(pathname), "%s/%s/%s", cmd->dev_dir,
			lv->vg->name, lv->name) < 0) {
		log_error("Path name too long to get LV block size %s", display_lvname(lv));
		goto bad;
	}

	if (test_mode()) {
		log_print_unless_silent("Test mode skips checking fs block size.");
		fs_block_size = 0;
		goto skip_fs;
	}

	/*
	 * fs_block_size_and_type() returns the libblkid BLOCK_SIZE value,
	 * where libblkid has fs-specific code to set BLOCK_SIZE to the
	 * value we need here.
	 *
	 * The term "block size" here may not equate directly to what the fs
	 * calls the block size, e.g. xfs calls this the sector size (and
	 * something different the block size); while ext4 does call this
	 * value the block size, but it's possible values are not the same
	 * as xfs's, and do not seem to relate directly to the device LBS.
	 *
	 * With 512 LBS and 4K PBS, mkfs.xfs will use xfs sector size 4K.
	 */
	rv = fs_block_size_and_type(pathname, &fs_block_size, NULL, NULL);
skip_fs:
	if (!rv || !fs_block_size) {
		if (block_size_setting)
			block_size = block_size_setting;
		else
			block_size = 4096;

		log_print_unless_silent("Using writecache block size %u for unknown file system block size, logical block size %u, physical block size %u.",
					block_size, lbs_4k ? 4096 : 512, pbs_4k ? 4096 : 512);

		if (block_size != 512) {
			log_warn("WARNING: unable to detect a file system block size on %s", display_lvname(lv));
			log_warn("WARNING: using a writecache block size larger than the file system block size may corrupt the file system.");
			if (!arg_is_set(cmd, yes_ARG) &&
			    yes_no_prompt("Use writecache block size %u? [y/n]: ", block_size) == 'n')  {
				log_error("Conversion aborted.");
				goto bad;
			}
		}

		goto out;
	}

	if (!block_size_setting) {
		/* User did not specify a block size, so choose according to fs block size. */
		if (fs_block_size == 4096)
			block_size = 4096;
		else if (fs_block_size == 512)
			block_size = 512;
		else if (fs_block_size > 4096)
			block_size = 4096;
		else if (fs_block_size < 4096)
			block_size = 512;
		else
			goto_bad;
	} else {
		if (block_size_setting <= fs_block_size)
			block_size = block_size_setting;
		else {
			log_error("Writecache block size %u cannot be larger than file system block size %u.",
				  block_size_setting, fs_block_size);
			goto bad;
		}
	}

out:
	if (block_size == 512)
		*block_size_sectors = 1;
	else if (block_size == 4096)
		*block_size_sectors = 8;
	else
		goto_bad;

	return 1;
bad:
	return 0;
}

static int _check_writecache_memory(struct cmd_context *cmd, struct logical_volume *lv_fast,
				     uint32_t block_size_sectors)
{
	char line[128];
	FILE *fp;
	uint64_t cachevol_size_bytes = lv_fast->size * SECTOR_SIZE;
	uint64_t need_mem_bytes = 0;
	uint64_t proc_mem_bytes = 0;
	uint64_t need_mem_gb;
	uint64_t proc_mem_gb;
	unsigned long long proc_mem_kb = 0;

	if (!(fp = fopen("/proc/meminfo", "r")))
		goto skip_proc;

	while (fgets(line, sizeof(line), fp)) {
		if (strncmp(line, "MemTotal:", 9))
			continue;
		if (sscanf(line, "%*s%llu%*s", &proc_mem_kb) != 1)
			break;
		break;
	}
	(void)fclose(fp);

	proc_mem_bytes = proc_mem_kb * 1024;

 skip_proc:
	/* dm-writecache memory consumption per block is 88 bytes */
	if (block_size_sectors == 8) {
		need_mem_bytes = cachevol_size_bytes * 88 / 4096;
	} else if (block_size_sectors == 1) {
		need_mem_bytes = cachevol_size_bytes * 88 / 512;
	} else {
		/* shouldn't happen */
		log_warn("Unknown memory usage for unknown writecache block_size_sectors %u", block_size_sectors);
		return 1;
	}

	need_mem_gb = need_mem_bytes / 1073741824;
	proc_mem_gb = proc_mem_bytes / 1073741824;

	/*
	 * warn if writecache needs > 50% of main memory, and
	 * confirm if writecache needs > 90% of main memory.
	 */
	if (need_mem_bytes >= (proc_mem_bytes / 2)) {
		log_warn("WARNING: writecache size %s will use %llu GiB of system memory (%llu GiB).",
			  display_size(cmd, lv_fast->size),
			  (unsigned long long)need_mem_gb,
			  (unsigned long long)proc_mem_gb);

		if (need_mem_gb >= (proc_mem_gb * 9 / 10)) {
			if (!arg_is_set(cmd, yes_ARG) &&
			    yes_no_prompt("Continue adding writecache? [y/n]: ") == 'n') {
				log_error("Conversion aborted.");
				return 0;
			}
		}
	}

	return 1;
}

int lvconvert_writecache_attach_single(struct cmd_context *cmd,
					struct logical_volume *lv,
					struct processing_handle *handle)
{
	struct volume_group *vg = lv->vg;
	struct logical_volume *lv_update;
	struct logical_volume *lv_wcorig;
	struct logical_volume *lv_fast;
	struct writecache_settings settings = { 0 };
	const char *fast_name;
	uint32_t block_size_sectors = 0;
	char *lockd_fast_args = NULL;
	char *lockd_fast_name = NULL;
	struct id lockd_fast_id;
	char cvol_name[NAME_LEN];
	int is_active;

	/*
	 * User specifies an existing cachevol to use or a cachedevice
	 * to create a cachevol from.
	 */
	if ((fast_name = arg_str_value(cmd, cachevol_ARG, NULL))) {
		if (!validate_lvname_param(cmd, &vg->name, &fast_name))
			goto_bad;

		if (!(lv_fast = find_lv(vg, fast_name))) {
			log_error("LV %s not found.", fast_name);
			goto bad;
		}

		if (lv_fast == lv) {
			log_error("Invalid cachevol LV.");
			goto bad;
		}

		if (lv_is_cache_vol(lv_fast)) {
			log_error("LV %s is already used as a cachevol.", display_lvname(lv_fast));
			goto bad;
		}

		if (!seg_is_linear(first_seg(lv_fast))) {
			log_error("LV %s must be linear to use as a writecache.", display_lvname(lv_fast));
			goto bad;
		}

		/* fast LV shouldn't generally be active by itself, but just in case. */
		if (lv_is_active(lv_fast)) {
			log_error("LV %s must be inactive to attach.", display_lvname(lv_fast));
			goto bad;
		}

		if (!arg_is_set(cmd, yes_ARG) &&
		     yes_no_prompt("Erase all existing data on %s? [y/n]: ", display_lvname(lv_fast)) == 'n') {
			log_error("Conversion aborted.");
			goto bad;
		}
	} else {
		if (!_lv_create_cachevol(cmd, vg, lv, &lv_fast))
			goto_bad;
	}

	is_active = lv_is_active(lv);

	if (!get_writecache_settings(cmd, &settings, &block_size_sectors)) {
		log_error("Invalid writecache settings.");
		goto bad;
	}

	if (!is_active) {
		/* checking block size of fs on the lv requires the lv to be active */
		if (!activate_lv(cmd, lv)) {
			log_error("Failed to activate LV to check block size %s", display_lvname(lv));
			goto bad;
		}
		if (!sync_local_dev_names(cmd)) {
			log_error("Failed to sync local dev names.");
			if (!deactivate_lv(cmd, lv))
				stack;
			goto bad;
		}
	}

	if (!_set_writecache_block_size(cmd, lv, &block_size_sectors)) {
		if (!is_active && !deactivate_lv(cmd, lv))
			stack;
		goto_bad;
	}

	if (!_check_writecache_memory(cmd, lv_fast, block_size_sectors)) {
		if (!is_active && !deactivate_lv(cmd, lv))
			stack;
		goto_bad;
	}

	if (!is_active) {
		if (!deactivate_lv(cmd, lv)) {
			log_error("Failed to deactivate LV after checking block size %s", display_lvname(lv));
			goto bad;
		}
	}

	/* Ensure the LV is not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		goto_bad;
	if (fast_name && !lockd_lv(cmd, lv_fast, "ex", 0))
		goto_bad;

	/*
	 * lv keeps the same lockd lock it had before, the lock for
	 * lv_fast is kept but is not used while it's attached, and
	 * lv_wcorig gets no lock.
	 */
	if (vg_is_shared(vg) && lv_fast->lock_args) {
		lockd_fast_args = dm_pool_strdup(cmd->mem, lv_fast->lock_args);
		lockd_fast_name = dm_pool_strdup(cmd->mem, lv_fast->name);
		memcpy(&lockd_fast_id, &lv_fast->lvid.id[1], sizeof(struct id));
	}

	if (!_writecache_zero(cmd, lv_fast)) {
		log_error("LV %s could not be zeroed.", display_lvname(lv_fast));
		return ECMD_FAILED;
	}

	/*
	 * The lvm tradition is to rename an LV with a special role-specific
	 * suffix when it becomes hidden.  Here the _cvol suffix is added to
	 * the fast LV name.  When the cache is detached, it's renamed back.
	 */
	if (dm_snprintf(cvol_name, sizeof(cvol_name), "%s_cvol", lv_fast->name) < 0) {
		log_error("Can't prepare new metadata name for %s.", display_lvname(lv_fast));
		return ECMD_FAILED;
	}
	if (!lv_rename_update(cmd, lv_fast, cvol_name, 0))
		return_ECMD_FAILED;

	lv_fast->status |= LV_CACHE_VOL;

	/* When the lv arg is a thinpool, redirect update to data sub lv. */

	if (lv_is_thin_pool(lv)) {
		lv_update = seg_lv(first_seg(lv), 0);
		log_verbose("Redirecting operation to data sub LV %s.", display_lvname(lv_update));
	} else {
		lv_update = lv;
	}

	/*
	 * Changes the vg struct to match the desired state.
	 *
	 * - lv keeps existing lv name and id, gets new segment with segtype
	 *   "writecache".
	 *
	 * - lv_fast keeps its existing name and id, becomes hidden.
	 *
	 * - lv_wcorig gets new name (existing name + _wcorig suffix),
	 *   gets new id, becomes hidden, gets segments from lv.
	 */

	if (!(lv_wcorig = _lv_writecache_create(cmd, lv_update, lv_fast, block_size_sectors, &settings)))
		goto_bad;

	/*
	 * vg_write(), suspend_lv(), vg_commit(), resume_lv(),
	 * where the old LV is suspended and the new LV is resumed.
	 */

	if (!lv_update_and_reload(lv_update))
		goto_bad;

	lockd_lv(cmd, lv, "un", 0);

	if (lockd_fast_name) {
		/* lockd unlock for lv_fast */
		if (!lockd_lv_name(cmd, vg, lockd_fast_name, &lockd_fast_id, lockd_fast_args, "un", 0))
			log_error("Failed to unlock fast LV %s/%s", vg->name, lockd_fast_name);
	}

	log_print_unless_silent("Logical volume %s now has writecache.",
				display_lvname(lv));
	return ECMD_PROCESSED;
bad:
	return ECMD_FAILED;

}

int lvconvert_to_writecache_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	cmd->cname->flags &= ~GET_VGNAME_FROM_OPTIONS;

	ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, handle, NULL,
			      &lvconvert_writecache_attach_single);

	destroy_processing_handle(cmd, handle);

	return ret;
}

int lvconvert_to_cache_with_cachevol_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	struct lvconvert_result lr = { 0 };
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	handle->custom_handle = &lr;

	cmd->cname->flags &= ~GET_VGNAME_FROM_OPTIONS;

	ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, handle, NULL,
			      &lvconvert_cachevol_attach_single);

	destroy_processing_handle(cmd, handle);

	return ret;
}

static int _lvconvert_integrity_remove(struct cmd_context *cmd, struct logical_volume *lv)
{
	int ret = 0;

	if (!lv_is_integrity(lv) && !lv_is_raid(lv)) {
		log_error("LV does not have integrity.");
		return ECMD_FAILED;
	}

	/* ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		return_ECMD_FAILED;

	if (lv_is_raid(lv))
		ret = lv_remove_integrity_from_raid(lv);
	if (!ret)
		return_ECMD_FAILED;

	log_print_unless_silent("Logical volume %s has removed integrity.", display_lvname(lv));
	return ECMD_PROCESSED;
}

static int _lvconvert_integrity_add(struct cmd_context *cmd, struct logical_volume *lv,
				    struct integrity_settings *set)
{
	struct volume_group *vg = lv->vg;
	struct dm_list *use_pvh;
	int ret = 0;

	/* ensure it's not active elsewhere. */
	if (!lockd_lv(cmd, lv, "ex", 0))
		return_0;

	if (cmd->position_argc > 1) {
		/* First pos arg is required LV, remaining are optional PVs. */
		if (!(use_pvh = create_pv_list(cmd->mem, vg, cmd->position_argc - 1, cmd->position_argv + 1, 0)))
			return_0;
	} else
		use_pvh = &vg->pvs;

	if (lv_is_partial(lv)) {
		log_error("Cannot add integrity while LV is missing PVs.");
		return 0;
	}

	if (lv_is_raid(lv))
		ret = lv_add_integrity_to_raid(lv, set, use_pvh, NULL);
	if (!ret)
		return_0;

	log_print_unless_silent("Logical volume %s has added integrity.", display_lvname(lv));
	return 1;
}

static int _lvconvert_integrity_single(struct cmd_context *cmd,
					struct logical_volume *lv,
					struct processing_handle *handle)
{
	struct integrity_settings settings;
	int ret = 0;

	memset(&settings, 0, sizeof(settings));

	if (!integrity_mode_set(arg_str_value(cmd, raidintegritymode_ARG, NULL), &settings))
		return_ECMD_FAILED;

	if (arg_is_set(cmd, raidintegrityblocksize_ARG))
		settings.block_size = arg_int_value(cmd, raidintegrityblocksize_ARG, 0);

	if (arg_int_value(cmd, raidintegrity_ARG, 0))
		ret = _lvconvert_integrity_add(cmd, lv, &settings);
	else
		ret = _lvconvert_integrity_remove(cmd, lv);

	if (!ret)
		return ECMD_FAILED;
	return ECMD_PROCESSED;
}

int lvconvert_integrity_cmd(struct cmd_context *cmd, int argc, char **argv)
{
	struct processing_handle *handle;
	int ret;

	if (!(handle = init_processing_handle(cmd, NULL))) {
		log_error("Failed to initialize processing handle.");
		return ECMD_FAILED;
	}

	/* Want to be able to remove integrity from partial LV */
	cmd->handles_missing_pvs = 1;

	cmd->cname->flags &= ~GET_VGNAME_FROM_OPTIONS;

	ret = process_each_lv(cmd, cmd->position_argc, cmd->position_argv, NULL, NULL, READ_FOR_UPDATE, handle, NULL,
			      &_lvconvert_integrity_single);

	destroy_processing_handle(cmd, handle);

	return ret;
}

/*
 * All lvconvert command defs have their own function,
 * so the generic function name is unused.
 */

int lvconvert(struct cmd_context *cmd, int argc, char **argv)
{
	log_error(INTERNAL_ERROR "Missing function for command definition %d:%s.",
		  cmd->command->command_index, cmd->command->command_id);
	return ECMD_FAILED;
}