summaryrefslogtreecommitdiff
path: root/lib/activate/dev_manager.c
blob: ac3f01718c76ba850cadc7d9ec70c5db383cd4d7 (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
/*
 * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
 * Copyright (C) 2004-2018 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 "lib/misc/lib.h"
#include "dev_manager.h"
#include "lib/misc/lvm-string.h"
#include "fs.h"
#include "lib/config/defaults.h"
#include "lib/metadata/segtype.h"
#include "lib/display/display.h"
#include "lib/commands/toolcontext.h"
#include "lib/activate/targets.h"
#include "lib/config/config.h"
#include "lib/activate/activate.h"
#include "lib/misc/lvm-exec.h"
#include "lib/datastruct/str_list.h"
#include "lib/misc/lvm-signal.h"

#include <limits.h>
#include <dirent.h>

#define MAX_TARGET_PARAMSIZE 50000
#define LVM_UDEV_NOSCAN_FLAG DM_SUBSYSTEM_UDEV_FLAG0
#define CRYPT_TEMP	"CRYPT-TEMP"
#define CRYPT_SUBDEV	"CRYPT-SUBDEV"
#define STRATIS		"stratis-"

typedef enum {
	PRELOAD,
	ACTIVATE,
	DEACTIVATE,
	SUSPEND,
	SUSPEND_WITH_LOCKFS,
	CLEAN
} action_t;

/* This list must match lib/misc/lvm-string.c:build_dm_uuid(). */
const char *uuid_suffix_list[] = { "pool", "cdata", "cmeta", "cvol", "tdata", "tmeta", "vdata", "vpool", "imeta", NULL};

struct dlid_list {
	struct dm_list list;
	const char *dlid;
	const struct logical_volume *lv;
};

struct dev_manager {
	struct dm_pool *mem;

	struct cmd_context *cmd;

	void *target_state;
	uint32_t pvmove_mirror_count;
	int flush_required;
	int activation;                 /* building activation tree */
	int suspend;			/* building suspend tree */
	unsigned track_external_lv_deps;
	unsigned track_pending_delete;
	unsigned track_pvmove_deps;

	const char *vg_name;
};

struct lv_layer {
	const struct logical_volume *lv;
	const char *old_name;
	int visible_component;
};

int read_only_lv(const struct logical_volume *lv, const struct lv_activate_opts *laopts, const char *layer)
{
	if (layer && lv_is_cow(lv))
		return 0; /* Keep snapshot's COW volume writable */

	if (lv_is_raid_image(lv) || lv_is_raid_metadata(lv))
		return 0; /* Keep RAID SubLvs writable */

	if (!layer) {
		if (lv_is_thin_pool(lv) || lv_is_vdo_pool(lv))
			return 1;
	}

	return (laopts->read_only || !(lv->status & LVM_WRITE));
}

/*
 * Low level device-layer operations.
 *
 * Unless task is DM_DEVICE_TARGET_MSG, also calls dm_task_run()
 */
static struct dm_task *_setup_task_run(int task, struct dm_info *info,
				       const char *name, const char *uuid,
				       uint32_t *event_nr,
				       uint32_t major, uint32_t minor,
				       int with_open_count,
				       int with_flush,
				       int query_inactive)
{
	char vsn[80];
	unsigned maj, min;
	struct dm_task *dmt;

	if (!(dmt = dm_task_create(task)))
		return_NULL;

	if (name && !dm_task_set_name(dmt, name))
		goto_out;

	if (uuid && *uuid && !dm_task_set_uuid(dmt, uuid))
		goto_out;

	if (event_nr && !dm_task_set_event_nr(dmt, *event_nr))
		goto_out;

	if (major && !dm_task_set_major_minor(dmt, major, minor, 1))
		goto_out;

	if (activation_checks() && !dm_task_enable_checks(dmt))
		goto_out;

	if (query_inactive && !dm_task_query_inactive_table(dmt)) {
		log_error("Failed to set query_inactive_table.");
		goto out;
	}

	if (!with_open_count && !dm_task_no_open_count(dmt))
		log_warn("WARNING: Failed to disable open_count.");

	if (!with_flush && !dm_task_no_flush(dmt))
		log_warn("WARNING: Failed to set no_flush.");

	switch (task) {
	case DM_DEVICE_TARGET_MSG:
		return dmt; /* TARGET_MSG needs more local tweaking before task_run() */
	case DM_DEVICE_LIST:
		/* Use 'newuuid' only with DM version that supports it */
		if (driver_version(vsn, sizeof(vsn)) &&
		    (sscanf(vsn, "%u.%u", &maj, &min) == 2) &&
		    (maj == 4 ? min >= 19 : maj > 4) &&
		    !dm_task_set_newuuid(dmt, " ")) // new uuid has no meaning here
			log_warn("WARNING: Failed to query uuid with LIST.");
		break;
	default:
		break;
	}

	if (!dm_task_run(dmt))
		goto_out;

	if (info && !dm_task_get_info(dmt, info))
		goto_out;

	return dmt;

out:
	dm_task_destroy(dmt);

	return NULL;
}

static int _get_segment_status_from_target_params(const char *target_name,
						  const char *params,
						  const struct dm_info *dminfo,
						  struct lv_seg_status *seg_status)
{
	const struct lv_segment *seg = seg_status->seg;
	const struct segment_type *segtype = seg->segtype;

	seg_status->type = SEG_STATUS_UNKNOWN; /* Parsing failed */

	/* Switch to snapshot segtype status logic for merging origin */
	/* This is 'dynamic' decision, both states are valid */
	if (lv_is_merging_origin(seg->lv)) {
		if (!strcmp(target_name, TARGET_NAME_SNAPSHOT_ORIGIN)) {
			seg_status->type = SEG_STATUS_NONE;
			return 1; /* Merge has not yet started */
		}
		if (!strcmp(target_name, TARGET_NAME_SNAPSHOT_MERGE) &&
		    !(segtype = get_segtype_from_string(seg->lv->vg->cmd, TARGET_NAME_SNAPSHOT)))
			return_0;
		/* Merging, parse 'snapshot' status of merge progress */
	}

	if (!params) {
		log_warn("WARNING: Cannot find matching %s segment for %s.",
			 segtype->name, display_lvname(seg_status->seg->lv));
		return 0;
	}

	/* Validate target_name segtype from DM table with lvm2 metadata segtype */
	if (!lv_is_locked(seg->lv) &&
	    strcmp(segtype->name, target_name) &&
	    /* If kernel's type isn't an exact match is it compatible? */
	    (!segtype->ops->target_status_compatible ||
	     !segtype->ops->target_status_compatible(target_name))) {
		log_warn("WARNING: Detected %s segment type does not match expected type %s for %s.",
			 target_name, segtype->name, display_lvname(seg_status->seg->lv));
		return 0;
	}

	/* TODO: move into segtype method */
	if (segtype_is_cache(segtype)) {
		if (!dm_get_status_cache(seg_status->mem, params, &(seg_status->cache)))
			return_0;
		seg_status->type = SEG_STATUS_CACHE;
	} else if (segtype_is_raid(segtype)) {
		if (!dm_get_status_raid(seg_status->mem, params, &seg_status->raid))
			return_0;
		seg_status->type = SEG_STATUS_RAID;
	} else if (segtype_is_thin_volume(segtype)) {
		if (!dm_get_status_thin(seg_status->mem, params, &seg_status->thin))
			return_0;
		seg_status->type = SEG_STATUS_THIN;
	} else if (segtype_is_thin_pool(segtype)) {
		if (!dm_get_status_thin_pool(seg_status->mem, params, &seg_status->thin_pool))
			return_0;
		seg_status->type = SEG_STATUS_THIN_POOL;
	} else if (segtype_is_snapshot(segtype)) {
		if (!dm_get_status_snapshot(seg_status->mem, params, &seg_status->snapshot))
			return_0;
		seg_status->type = SEG_STATUS_SNAPSHOT;
	} else if (segtype_is_vdo_pool(segtype)) {
		if (!parse_vdo_pool_status(seg_status->mem, seg->lv, params, dminfo, &seg_status->vdo_pool))
			return_0;
		seg_status->type = SEG_STATUS_VDO_POOL;
	} else if (segtype_is_writecache(segtype)) {
		if (!dm_get_status_writecache(seg_status->mem, params, &(seg_status->writecache)))
			return_0;
		seg_status->type = SEG_STATUS_WRITECACHE;
	} else if (segtype_is_integrity(segtype)) {
		if (!dm_get_status_integrity(seg_status->mem, params, &(seg_status->integrity)))
			return_0;
		seg_status->type = SEG_STATUS_INTEGRITY;
	} else
		/*
		 * TODO: Add support for other segment types too!
		 * Status not supported
		 */
		seg_status->type = SEG_STATUS_NONE;

	return 1;
}

typedef enum {
	INFO,	/* DM_DEVICE_INFO ioctl */
	STATUS, /* DM_DEVICE_STATUS ioctl */
} info_type_t;

/* Return length of segment depending on type and reshape_len */
static uint32_t _seg_len(const struct lv_segment *seg)
{
	uint32_t reshape_len = seg_is_raid(seg) ? ((seg->area_count - seg->segtype->parity_devs) * seg->reshape_len) : 0;

	return seg->len - reshape_len;
}

static int _info_run(const char *dlid, struct dm_info *dminfo,
		     uint32_t *read_ahead,
		     struct lv_seg_status *seg_status,
		     const char *name_check,
		     int with_open_count, int with_read_ahead,
		     uint32_t major, uint32_t minor)
{
	int r = 0;
	struct dm_task *dmt;
	int dmtask;
	int with_flush; /* TODO: arg for _info_run */
	void *target = NULL;
	uint64_t target_start, target_length, start, extent_size, length, length_crop = 0;
	char *target_name, *target_params;
	const char *devname;

	if (seg_status) {
		dmtask = DM_DEVICE_STATUS;
		with_flush = 0;
	} else {
		dmtask = DM_DEVICE_INFO;
		with_flush = 1; /* doesn't really matter */
	}

	if (!(dmt = _setup_task_run(dmtask, dminfo, NULL, dlid, 0, major, minor,
				    with_open_count, with_flush, 0)))
		return_0;

	if (name_check && dminfo->exists &&
	    (devname = dm_task_get_name(dmt)) &&
	    (strcmp(name_check, devname) != 0))
		dminfo->exists = 0;	/* mismatching name -> device does not exist */

	if (with_read_ahead && read_ahead && dminfo->exists) {
		if (!dm_task_get_read_ahead(dmt, read_ahead))
			goto_out;
	} else if (read_ahead)
		*read_ahead = DM_READ_AHEAD_NONE;

	/* Query status only for active device */
	if (seg_status && dminfo->exists) {
		extent_size = length = seg_status->seg->lv->vg->extent_size;
		start = extent_size * seg_status->seg->le;
		length *= _seg_len(seg_status->seg);

		/* Uses max DM_THIN_MAX_METADATA_SIZE sectors for metadata device */
		if (lv_is_thin_pool_metadata(seg_status->seg->lv) &&
		    (length > DM_THIN_MAX_METADATA_SIZE))
			length_crop = DM_THIN_MAX_METADATA_SIZE;

		/* Uses virtual size with headers for VDO pool device */
		if (lv_is_vdo_pool(seg_status->seg->lv))
			length = get_vdo_pool_virtual_size(seg_status->seg);

		if (lv_is_integrity(seg_status->seg->lv))
			length = seg_status->seg->integrity_data_sectors;

		do {
			target = dm_get_next_target(dmt, target, &target_start,
						    &target_length, &target_name, &target_params);

			if ((start == target_start) &&
			    ((length == target_length) ||
			     ((lv_is_vdo_pool(seg_status->seg->lv)) && /* should fit within extent size */
			      (length < target_length) && ((length + extent_size) > target_length)) ||
			     (length_crop && (length_crop == target_length))))
				break; /* Keep target_params when matching segment is found */

			target_params = NULL; /* Marking this target_params unusable */
		} while (target);

		if (!target_name ||
		    !_get_segment_status_from_target_params(target_name, target_params, dminfo, seg_status))
			stack;
	}

	r = 1;

      out:
	dm_task_destroy(dmt);

	return r;
}

/*
 * ignore_blocked_mirror_devices
 * @dev
 * @start
 * @length
 * @mirror_status_str
 *
 * When a DM 'mirror' target is created with 'block_on_error' or
 * 'handle_errors', it will block I/O if there is a device failure
 * until the mirror is reconfigured.  Thus, LVM should never attempt
 * to read labels from a mirror that has a failed device.  (LVM
 * commands are issued to repair mirrors; and if LVM is blocked
 * attempting to read a mirror, a circular dependency would be created.)
 *
 * This function is a slimmed-down version of lib/mirror/mirrored.c:
 * _mirrored_transient_status().
 *
 * If a failed device is detected in the status string, then it must be
 * determined if 'block_on_error' or 'handle_errors' was used when
 * creating the mirror.  This info can only be determined from the mirror
 * table.  The 'dev', 'start', 'length' trio allow us to correlate the
 * 'mirror_status_str' with the correct device table in order to check
 * for blocking.
 *
 * Returns: 1 if mirror should be ignored, 0 if safe to use
 */
static int _ignore_blocked_mirror_devices(struct cmd_context *cmd,
					  struct device *dev,
					  uint64_t start, uint64_t length,
					  char *mirror_status_str)
{
	struct dm_pool *mem;
	struct dm_status_mirror *sm;
	unsigned i, check_for_blocking = 0;
	uint64_t s,l;
	char *p, *params, *target_type = NULL;
	void *next = NULL;
	struct dm_task *dmt = NULL;
	int r = 0;
	struct device *tmp_dev;
	char buf[16];

	if (!(mem = dm_pool_create("blocked_mirrors", 128)))
		return_0;

	if (!dm_get_status_mirror(mem, mirror_status_str, &sm))
		goto_out;

	for (i = 0; i < sm->dev_count; ++i)
		if (sm->devs[i].health != DM_STATUS_MIRROR_ALIVE) {
			log_debug_activation("%s: Mirror image %d marked as failed.",
					     dev_name(dev), i);
			check_for_blocking = 1;
		}

	if (!check_for_blocking && sm->log_count) {
		if (sm->logs[0].health != DM_STATUS_MIRROR_ALIVE) {
			log_debug_activation("%s: Mirror log device marked as failed.",
					     dev_name(dev));
			check_for_blocking = 1;
		} else {

			if (dm_snprintf(buf, sizeof(buf), "%u:%u",
					sm->logs[0].major, sm->logs[0].minor) < 0)
				goto_out;

			if (!(tmp_dev = dev_create_file(buf, NULL, NULL, 0)))
				goto_out;

			tmp_dev->dev = MKDEV(sm->logs[0].major, sm->logs[0].minor);
			if (device_is_usable(cmd, tmp_dev, (struct dev_usable_check_params)
					     { .check_empty = 1,
					       .check_blocked = 1,
					       .check_suspended = ignore_suspended_devices(),
					       .check_error_target = 1,
					       .check_reserved = 0 }, NULL))
				goto out; /* safe to use */
			stack;
		}
	}

	if (!check_for_blocking) {
		r = 1;
		goto out;
	}

	/*
	 * We avoid another system call if we can, but if a device is
	 * dead, we have no choice but to look up the table too.
	 */
	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, NULL, NULL, NULL, NULL,
				    MAJOR(dev->dev), MINOR(dev->dev), 0, 1, 0)))
		goto_out;

	do {
		next = dm_get_next_target(dmt, next, &s, &l,
					  &target_type, &params);
		if ((s == start) && (l == length) &&
		    target_type && params) {
			if (strcmp(target_type, TARGET_NAME_MIRROR))
				goto_out;

			if (((p = strstr(params, " block_on_error")) &&
			     (p[15] == '\0' || p[15] == ' ')) ||
			    ((p = strstr(params, " handle_errors")) &&
			     (p[14] == '\0' || p[14] == ' '))) {
				log_debug_activation("%s: I/O blocked to mirror device.",
						     dev_name(dev));
				goto out;
			}
		}
	} while (next);

	r = 1;
out:
	if (dmt)
		dm_task_destroy(dmt);

	dm_pool_destroy(mem);

	return r;
}

static int _device_is_suspended(int major, int minor)
{
	struct dm_task *dmt;
	struct dm_info info;

	if (!(dmt = _setup_task_run(DM_DEVICE_INFO, &info,
				    NULL, NULL, NULL,
				    major, minor, 0, 0, 0)))
		return_0;

	dm_task_destroy(dmt);

	return (info.exists && info.suspended);
}

static int _ignore_suspended_snapshot_component(struct device *dev)
{
	struct dm_task *dmt;
	void *next = NULL;
	char *params, *target_type = NULL;
	uint64_t start, length;
	int major1, minor1, major2, minor2;
	int r = 0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, NULL,
				    NULL, NULL, NULL,
				    MAJOR(dev->dev), MINOR(dev->dev), 0, 1, 0)))
		return_0;

	do {
		next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);

		if (!target_type)
			continue;

		if (!strcmp(target_type, TARGET_NAME_SNAPSHOT)) {
			if (!params || sscanf(params, "%d:%d %d:%d", &major1, &minor1, &major2, &minor2) != 4) {
				log_warn("WARNING: Incorrect snapshot table found for %d:%d.",
					 (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
				goto out;
			}
			r = r || _device_is_suspended(major1, minor1) || _device_is_suspended(major2, minor2);
		} else if (!strcmp(target_type, TARGET_NAME_SNAPSHOT_ORIGIN)) {
			if (!params || sscanf(params, "%d:%d", &major1, &minor1) != 2) {
				log_warn("WARNING: Incorrect snapshot-origin table found for %d:%d.",
					 (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
				goto out;
			}
			r = r || _device_is_suspended(major1, minor1);
		}
	} while (next);

out:
	dm_task_destroy(dmt);

	return r;
}

static int _ignore_unusable_thins(struct device *dev)
{
	/* TODO make function for thin testing */
	struct dm_pool *mem;
	struct dm_status_thin_pool *status;
	struct dm_task *dmt = NULL;
	void *next = NULL;
	uint64_t start, length;
	char *target_type = NULL;
	char *params;
	int minor, major;
	int r = 0;

	if (!(mem = dm_pool_create("unusable_thins", 128)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, NULL, NULL, NULL, NULL,
				    MAJOR(dev->dev), MINOR(dev->dev), 0, 1, 0)))
		goto_out;

	dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
	if (!params || sscanf(params, "%d:%d", &major, &minor) != 2) {
		log_warn("WARNING: Cannot get thin-pool major:minor for thin device %d:%d.",
			  (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
		goto out;
	}
	dm_task_destroy(dmt);

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, NULL, NULL, NULL, NULL,
				    major, minor, 0, 0, 0)))
		goto_out;

	dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
	if (!dm_get_status_thin_pool(mem, params, &status))
		goto_out;

	if (status->read_only || status->out_of_data_space) {
		log_warn("WARNING: %s: Thin's thin-pool needs inspection.",
			 dev_name(dev));
		goto out;
	}

	r = 1;
out:
	if (dmt)
		dm_task_destroy(dmt);

	dm_pool_destroy(mem);

        return r;
}

static int _ignore_invalid_snapshot(const char *params)
{
	struct dm_status_snapshot *s;
	struct dm_pool *mem;
	int r = 0;

	if (!(mem = dm_pool_create("invalid snapshots", 128)))
		return_0;

	if (!dm_get_status_snapshot(mem, params, &s))
		stack;
        else
		r = s->invalid;

	dm_pool_destroy(mem);

	return r;
}

static int _ignore_frozen_raid(struct device *dev, const char *params)
{
	struct dm_status_raid *s;
	struct dm_pool *mem;
	int r = 0;

	if (!(mem = dm_pool_create("frozen raid", 128)))
		return_0;

	if (!dm_get_status_raid(mem, params, &s))
		stack;
	else if (s->sync_action && !strcmp(s->sync_action, "frozen")) {
		log_warn("WARNING: %s frozen raid device (%d:%d) needs inspection.",
			  dev_name(dev), (int)MAJOR(dev->dev), (int)MINOR(dev->dev));
		r = 1;
	}

	dm_pool_destroy(mem);

	return r;
}

static int _is_usable_uuid(const struct device *dev, const char *name, const char *uuid, int check_reserved, int check_lv, int *is_lv)
{
	char *vgname, *lvname, *layer;
	char vg_name[NAME_LEN];

	if (!check_reserved && !check_lv)
		return 1;

	if (!strncmp(uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) { /* with LVM- prefix */
		if (check_reserved) {
			/* Check internal lvm devices */
			if (strlen(uuid) > (sizeof(UUID_PREFIX) + 2 * ID_LEN)) { /* 68 with suffix */
				log_debug_activation("%s: Reserved uuid %s on internal LV device %s not usable.",
						     dev_name(dev), uuid, name);
				return 0;
			}

			/* Recognize some older reserved LVs just from the LV name (snapshot, pvmove...) */
			vgname = vg_name;
			if (!dm_strncpy(vg_name, name, sizeof(vg_name)) ||
			    !dm_split_lvm_name(NULL, NULL, &vgname, &lvname, &layer))
				return_0;

			/* FIXME: fails to handle dev aliases i.e. /dev/dm-5, replace with UUID suffix */
			if (lvname && (is_reserved_lvname(lvname) || *layer)) {
				log_debug_activation("%s: Reserved internal LV device %s/%s%s%s not usable.",
						     dev_name(dev), vgname, lvname, *layer ? "-" : "", layer);
				return 0;
			}
		}

		if (check_lv) {
			/* Skip LVs */
			if (is_lv)
				*is_lv = 1;
			return 0;
		}
	}

	if (check_reserved &&
	    (!strncmp(uuid, CRYPT_TEMP, sizeof(CRYPT_TEMP) - 1) ||
	     !strncmp(uuid, CRYPT_SUBDEV, sizeof(CRYPT_SUBDEV) - 1) ||
	     !strncmp(uuid, STRATIS, sizeof(STRATIS) - 1))) {
		/* Skip private crypto devices */
		log_debug_activation("%s: Reserved uuid %s on %s device %s not usable.",
				     dev_name(dev), uuid,
				     uuid[0] == 'C' ? "crypto" : "stratis",
				     name);
		return 0;
	}

        return 1;
}

/*
 * device_is_usable
 * @dev
 * @check_lv_names
 *
 * A device is considered not usable if it is:
 *     1) An empty device (no targets)
 *     2) A blocked mirror (i.e. a mirror with a failure and block_on_error set)
 *     3) ignore_suspended_devices is set and
 *        a) the device is suspended
 *        b) it is a snapshot origin
 *     4) an error target
 *     5) the LV name is a reserved name.
 *
 * Returns: 1 if usable, 0 otherwise
 */
int device_is_usable(struct cmd_context *cmd, struct device *dev, struct dev_usable_check_params check, int *is_lv)
{
	struct dm_task *dmt;
	struct dm_info info;
	const char *name, *uuid;
	uint64_t start, length;
	char *target_type = NULL;
	char *params;
	void *next = NULL;
	int only_error_or_zero_target = 1;
	int r = 0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, NULL, NULL,
				    MAJOR(dev->dev), MINOR(dev->dev), 0, 0, 0)))
		return_0;

	if (!info.exists)
		goto out;

	name = dm_task_get_name(dmt);
	uuid = dm_task_get_uuid(dmt);

	if (check.check_empty && !info.target_count) {
		log_debug_activation("%s: Empty device %s not usable.", dev_name(dev), name);
		goto out;
	}

	if (check.check_suspended && info.suspended) {
		log_debug_activation("%s: Suspended device %s not usable.", dev_name(dev), name);
		goto out;
	}

	if (uuid &&
	    !_is_usable_uuid(dev, name, uuid, check.check_reserved, check.check_lv, is_lv))
		goto out;

	/* FIXME Also check for mpath no paths */
	do {
		next = dm_get_next_target(dmt, next, &start, &length,
					  &target_type, &params);

		if (!target_type)
			continue;

		if (check.check_blocked && !strcmp(target_type, TARGET_NAME_MIRROR)) {
			if (ignore_lvm_mirrors()) {
				log_debug_activation("%s: Scanning mirror devices is disabled.", dev_name(dev));
				goto out;
			}
			if (!_ignore_blocked_mirror_devices(cmd, dev, start,
							    length, params)) {
				log_debug_activation("%s: Mirror device %s not usable.",
						     dev_name(dev), name);
				goto out;
			}
		}

		/*
		 * FIXME: Snapshot origin could be sitting on top of a mirror
		 * which could be blocking I/O. We should add a check for the
		 * stack here and see if there's blocked mirror underneath.
		 * Currently, mirrors used as origin or snapshot is not
		 * supported anymore and in general using mirrors in a stack
		 * is disabled by default (with a warning that if enabled,
		 * it could cause various deadlocks).
		 * Similar situation can happen with RAID devices where
		 * a RAID device can be snapshotted.
		 * If one of the RAID legs are down and we're doing
		 * lvconvert --repair, there's a time period in which
		 * snapshot components are (besides other devs) suspended.
		 * See also https://bugzilla.redhat.com/show_bug.cgi?id=1219222
		 * for an example where this causes problems.
		 *
		 * This is a quick check for now, but replace it with more
		 * robust and better check that would check the stack
		 * correctly, not just snapshots but any cobimnation possible
		 * in a stack - use proper dm tree to check this instead.
		 */
		if (check.check_suspended &&
		    (!strcmp(target_type, TARGET_NAME_SNAPSHOT) || !strcmp(target_type, TARGET_NAME_SNAPSHOT_ORIGIN)) &&
		    _ignore_suspended_snapshot_component(dev)) {
			log_debug_activation("%s: %s device %s not usable.", dev_name(dev), target_type, name);
			goto out;
		}

		if (!strcmp(target_type, TARGET_NAME_SNAPSHOT) &&
		    _ignore_invalid_snapshot(params)) {
			log_debug_activation("%s: Invalid %s device %s not usable.", dev_name(dev), target_type, name);
			goto out;
		}

		if (!strncmp(target_type, TARGET_NAME_RAID, 4) && _ignore_frozen_raid(dev, params)) {
			log_debug_activation("%s: Frozen %s device %s not usable.",
					     dev_name(dev), target_type, name);
			goto out;
		}

		/* TODO: extend check struct ? */
		if (!strcmp(target_type, TARGET_NAME_THIN) &&
		    !_ignore_unusable_thins(dev)) {
			log_debug_activation("%s: %s device %s not usable.", dev_name(dev), target_type, name);
			goto out;
		}

		if (only_error_or_zero_target &&
		    strcmp(target_type, TARGET_NAME_ERROR) &&
		    strcmp(target_type, TARGET_NAME_ZERO))
			only_error_or_zero_target = 0;
	} while (next);

	/* Skip devices consisting entirely of error or zero targets. */
	/* FIXME Deal with device stacked above error targets? */
	if (check.check_error_target && only_error_or_zero_target) {
		log_debug_activation("%s: Error device %s not usable.",
				     dev_name(dev), name);
		goto out;
	}

	/* FIXME Also check dependencies? */

	r = 1;

      out:
	dm_task_destroy(dmt);
	return r;
}

/*
 * If active LVs were activated by a version of LVM2 before 2.02.00 we must
 * perform additional checks to find them because they do not have the LVM-
 * prefix on their dm uuids.
 * As of 2.02.150, we've chosen to disable this compatibility arbitrarily if
 * we're running kernel version 3 or above.
 */
#define MIN_KERNEL_MAJOR 3

static int _original_uuid_format_check_required(struct cmd_context *cmd)
{
	static int _kernel_major = 0;

	if (!_kernel_major) {
		if ((sscanf(cmd->kernel_vsn, "%d", &_kernel_major) == 1) &&
		    (_kernel_major >= MIN_KERNEL_MAJOR))
			log_debug_activation("Skipping checks for old devices without " UUID_PREFIX
					     " dm uuid prefix (kernel vsn %d >= %d).", _kernel_major, MIN_KERNEL_MAJOR);
		else
			_kernel_major = -1;
	}

	return (_kernel_major == -1);
}

static int _info(struct cmd_context *cmd,
		 const char *name, const char *dlid,
		 int with_open_count, int with_read_ahead, int with_name_check,
		 struct dm_info *dminfo, uint32_t *read_ahead,
		 struct lv_seg_status *seg_status)
{
	char old_style_dlid[sizeof(UUID_PREFIX) + 2 * ID_LEN];
	const char *suffix, *suffix_position;
	const char *name_check = (with_name_check) ? name : NULL;
	unsigned i = 0;

	log_debug_activation("Getting device info for %s [%s].", name, dlid);

	/* Check for dlid */
	if (!_info_run(dlid, dminfo, read_ahead, seg_status, name_check,
		       with_open_count, with_read_ahead, 0, 0))
		return_0;

	if (dminfo->exists)
		return 1;

	/* Check for original version of dlid before the suffixes got added in 2.02.106 */
	if ((suffix_position = strrchr(dlid, '-'))) {
		while ((suffix = uuid_suffix_list[i++])) {
			if (strcmp(suffix_position + 1, suffix))
				continue;

			(void) dm_strncpy(old_style_dlid, dlid, sizeof(old_style_dlid));
			if (!_info_run(old_style_dlid, dminfo, read_ahead, seg_status,
				       name_check, with_open_count, with_read_ahead,
				       0, 0))
				return_0;
			if (dminfo->exists)
				return 1;
		}
	}

	/* Must we still check for the pre-2.02.00 dm uuid format? */
	if (!_original_uuid_format_check_required(cmd))
		return 1;

	/* Check for dlid before UUID_PREFIX was added */
	if (!_info_run(dlid + sizeof(UUID_PREFIX) - 1, dminfo, read_ahead, seg_status,
		       name_check, with_open_count, with_read_ahead, 0, 0))
		return_0;

	return 1;
}

int dev_manager_remove_dm_major_minor(uint32_t major, uint32_t minor)
{
	struct dm_task *dmt;
	int r = 0;

	log_verbose("Removing dm dev %u:%u", major, minor);

	if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
		return_0;

	if (!dm_task_set_major(dmt, major) || !dm_task_set_minor(dmt, minor)) {
		log_error("Failed to set device number for remove %u:%u", major, minor);
		goto out;
	}

	r = dm_task_run(dmt);
out:
	dm_task_destroy(dmt);

	return r;
}

static int _info_by_dev(uint32_t major, uint32_t minor, struct dm_info *info)
{
	return _info_run(NULL, info, NULL, NULL, NULL, 0, 0, major, minor);
}

int dev_manager_check_prefix_dm_major_minor(uint32_t major, uint32_t minor, const char *prefix)
{
	struct dm_task *dmt;
	const char *uuid;
	int r = 1;

	if (!(dmt = _setup_task_run(DM_DEVICE_INFO, NULL, NULL, NULL, 0, major, minor, 0, 0, 0)))
		return_0;

	if (!(uuid = dm_task_get_uuid(dmt)) || strncasecmp(uuid, prefix, strlen(prefix)))
		r = 0;

	dm_task_destroy(dmt);

	return r;
}

int dev_manager_get_device_list(const char *prefix, struct dm_list **devs, unsigned *devs_features)
{
	struct dm_task *dmt;
	int r = 1;

	if (!(dmt = _setup_task_run(DM_DEVICE_LIST, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0)))
		return_0;

	if (!dm_task_get_device_list(dmt, devs, devs_features)) {
		r = 0;
		goto_out;
	}

    out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_info(struct cmd_context *cmd,
		     const struct logical_volume *lv, const char *layer,
		     int with_open_count, int with_read_ahead, int with_name_check,
		     struct dm_info *dminfo, uint32_t *read_ahead,
		     struct lv_seg_status *seg_status)
{
	char *dlid, *name;
	int r = 0;

	if (!(name = dm_build_dm_name(cmd->mem, lv->vg->name, lv->name, layer)))
		return_0;

	if (!(dlid = build_dm_uuid(cmd->mem, lv, layer)))
		goto_out;

	if (!cmd->disable_dm_devs &&
	    cmd->cache_dm_devs &&
	    !dm_device_list_find_by_uuid(cmd->cache_dm_devs, dlid, NULL)) {
		log_debug("Cached as inactive %s.", name);
		if (dminfo)
			memset(dminfo, 0, sizeof(*dminfo));
		r = 1;
		goto out;
	}

	if (!(r = _info(cmd, name, dlid,
			with_open_count, with_read_ahead, with_name_check,
			dminfo, read_ahead, seg_status)))
		stack;
out:
	dm_pool_free(cmd->mem, name);

	return r;
}

static const struct dm_info *_cached_dm_info(struct dm_pool *mem,
					     struct dm_tree *dtree,
					     const struct logical_volume *lv,
					     const char *layer)
{
	char *dlid;
	const struct dm_tree_node *dnode;
	const struct dm_info *dinfo = NULL;

	if (!(dlid = build_dm_uuid(mem, lv, layer)))
		return_NULL;

	if (!(dnode = dm_tree_find_node_by_uuid(dtree, dlid)))
		goto out;

	if (!(dinfo = dm_tree_node_get_info(dnode))) {
		log_warn("WARNING: Cannot get info from tree node for %s.",
			 display_lvname(lv));
		goto out;
	}

	if (!dinfo->exists)
		dinfo = NULL;
out:
	dm_pool_free(mem, dlid);

	return dinfo;
}

int lv_has_target_type(struct dm_pool *mem, const struct logical_volume *lv,
		       const char *layer, const char *target_type)
{
	int r = 0;
	char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	void *next = NULL;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;

	if (!(dlid = build_dm_uuid(mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, 0, 0)))
		goto_bad;

	if (!info.exists)
		goto_out;

	/* If there is a preloaded table, use that in preference. */
	if (info.inactive_table) {
		dm_task_destroy(dmt);

		if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, 0, 1)))
			goto_bad;

		if (!info.exists || !info.inactive_table)
			goto_out;
	}

	do {
		next = dm_get_next_target(dmt, next, &start, &length,
					  &type, &params);
		if (type && !strncmp(type, target_type, strlen(target_type))) {
			r = 1;
			break;
		}
	} while (next);

out:
	dm_task_destroy(dmt);
bad:
	dm_pool_free(mem, dlid);

	return r;
}

static int _lv_has_thin_device_id(struct dm_pool *mem, const struct logical_volume *lv,
				  const char *layer, unsigned device_id)
{
	char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	void *next = NULL;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	unsigned id = ~0;

	if (!(dlid = build_dm_uuid(mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, &info, NULL, dlid, 0, 0, 0, 0, 1, 0)))
		goto_bad;

	if (!info.exists)
		goto_out;

	/* If there is a preloaded table, use that in preference. */
	if (info.inactive_table) {
		dm_task_destroy(dmt);

		if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, &info, NULL, dlid, 0, 0, 0, 0, 1, 1)))
			goto_bad;

		if (!info.exists || !info.inactive_table)
			goto_out;
	}

	(void) dm_get_next_target(dmt, next, &start, &length, &type, &params);

	if (!type || strcmp(type, TARGET_NAME_THIN))
		goto_out;

	if (!params || sscanf(params, "%*u:%*u %u", &id) != 1)
		goto_out;

	log_debug_activation("%soaded thin volume %s with id %u is %smatching id %u.",
			     info.inactive_table  ? "Prel" : "L",
			     display_lvname(lv), id,
			     (device_id != id) ? "not " : "", device_id);
out:
	dm_task_destroy(dmt);
bad:
	dm_pool_free(mem, dlid);

	return (device_id == id);
}

int add_linear_area_to_dtree(struct dm_tree_node *node, uint64_t size, uint32_t extent_size,
			     int use_linear_target, const char *vgname, const char *lvname)
{
	uint32_t page_size;

	/*
	 * Use striped or linear target?
	 */
	if (!use_linear_target) {
		page_size = lvm_getpagesize() >> SECTOR_SHIFT;

		/*
		 * We'll use the extent size as the stripe size.
		 * Extent size and page size are always powers of 2.
		 * The striped target requires that the stripe size is
		 * divisible by the page size.
		 */
		if (extent_size >= page_size) {
			/* Use striped target */
			if (!dm_tree_node_add_striped_target(node, size, extent_size))
				return_0;
			return 1;
		}

		/* Some exotic cases are unsupported by striped. */
		log_warn("WARNING: Using linear target for %s/%s: Striped requires extent size "
			 "(" FMTu32 " sectors) >= page size (" FMTu32 ").",
			 vgname, lvname, extent_size, page_size);
	}

	/*
	 * Use linear target.
	 */
	if (!dm_tree_node_add_linear_target(node, size))
		return_0;

	return 1;
}

static dm_percent_range_t _combine_percent(dm_percent_t a, dm_percent_t b,
					   uint32_t numerator, uint32_t denominator)
{
	if (a == LVM_PERCENT_MERGE_FAILED || b == LVM_PERCENT_MERGE_FAILED)
		return LVM_PERCENT_MERGE_FAILED;

	if (a == DM_PERCENT_INVALID || b == DM_PERCENT_INVALID)
		return DM_PERCENT_INVALID;

	if (a == DM_PERCENT_100 && b == DM_PERCENT_100)
		return DM_PERCENT_100;

	if (a == DM_PERCENT_0 && b == DM_PERCENT_0)
		return DM_PERCENT_0;

	return (dm_percent_range_t) dm_make_percent(numerator, denominator);
}

static int _percent_run(struct dev_manager *dm, const char *name,
			const char *dlid,
			const char *target_type, int wait,
			const struct logical_volume *lv, dm_percent_t *overall_percent,
			uint32_t *event_nr, int fail_if_percent_unsupported,
			int *interrupted)
{
	int r = 0;
	struct dm_task *dmt;
	struct dm_info info;
	void *next = NULL;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	const struct dm_list *segh = lv ? &lv->segments : NULL;
	struct lv_segment *seg = NULL;
	int first_time = 1;
	dm_percent_t percent = DM_PERCENT_INVALID;
	uint64_t total_numerator = 0, total_denominator = 0;
	struct segment_type *segtype;

	*overall_percent = percent;

	if (!(segtype = get_segtype_from_string(dm->cmd, target_type)))
		return_0;

	if (wait)
		sigint_allow();

	if (!(dmt = _setup_task_run(wait ? DM_DEVICE_WAITEVENT : DM_DEVICE_STATUS, &info,
				    name, dlid, event_nr, 0, 0, 0, 0, 0)))
		goto_bad;

	if (!info.exists)
		goto_out;

	if (event_nr)
		*event_nr = info.event_nr;

	do {
		next = dm_get_next_target(dmt, next, &start, &length, &type,
					  &params);
		if (lv) {
			if (!(segh = dm_list_next(&lv->segments, segh))) {
				log_error("Number of segments in active LV %s "
					  "does not match metadata.",
					  display_lvname(lv));
				goto out;
			}
			seg = dm_list_item(segh, struct lv_segment);
		}

		if (!type || !params)
			continue;

		if (strcmp(type, target_type)) {
			/* If kernel's type isn't an exact match is it compatible? */
			if (!segtype->ops->target_status_compatible ||
			    !segtype->ops->target_status_compatible(type))
				continue;
		}

		if (!segtype->ops->target_percent)
			continue;

		if (!segtype->ops->target_percent(&dm->target_state,
						  &percent, dm->mem,
						  dm->cmd, seg, params,
						  &total_numerator,
						  &total_denominator))
			goto_out;

		if (first_time) {
			*overall_percent = percent;
			first_time = 0;
		} else
			*overall_percent =
				_combine_percent(*overall_percent, percent,
						 total_numerator, total_denominator);
	} while (next);

	if (lv && dm_list_next(&lv->segments, segh)) {
		log_error("Number of segments in active LV %s does not "
			  "match metadata.", display_lvname(lv));
		goto out;
	}

	if (first_time) {
		/* above ->target_percent() was not executed! */
		/* FIXME why return PERCENT_100 et. al. in this case? */
		*overall_percent = DM_PERCENT_100;
		if (fail_if_percent_unsupported)
			goto_out;
	}

	log_debug_activation("LV percent: %s",
			     display_percent(dm->cmd, *overall_percent));
	r = 1;

    out:
	dm_task_destroy(dmt);

    bad:
	if (wait) {
		sigint_restore();

		if (sigint_caught()) {
			*interrupted = 1;
			return_0;
		}
	}

	return r;
}

static int _percent(struct dev_manager *dm, const char *name, const char *dlid,
		    const char *target_type, int wait,
		    const struct logical_volume *lv, dm_percent_t *percent,
		    uint32_t *event_nr, int fail_if_percent_unsupported)
{
	int interrupted = 0;

	if (dlid && *dlid) {
		if (_percent_run(dm, NULL, dlid, target_type, wait, lv, percent,
				 event_nr, fail_if_percent_unsupported, &interrupted))
			return 1;

		if (!interrupted &&
		    _original_uuid_format_check_required(dm->cmd) &&
		    _percent_run(dm, NULL, dlid + sizeof(UUID_PREFIX) - 1,
				 target_type, wait, lv, percent,
				 event_nr, fail_if_percent_unsupported, &interrupted))
			return 1;
	}

	if (!interrupted && name &&
	    _percent_run(dm, name, NULL, target_type, wait, lv, percent,
				 event_nr, fail_if_percent_unsupported, &interrupted))
		return 1;

	return_0;
}

/* FIXME Merge with the percent function */
int dev_manager_transient(struct dev_manager *dm, const struct logical_volume *lv)
{
	int r = 0;
	struct dm_task *dmt;
	struct dm_info info;
	void *next = NULL;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	char *dlid = NULL;
	const char *layer = lv_layer(lv);
	const struct dm_list *segh = &lv->segments;
	struct lv_segment *seg = NULL;

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, NULL, 0, 0, 0, 0, 0)))
		return_0;

	if (!info.exists)
		goto_out;

	do {
		next = dm_get_next_target(dmt, next, &start, &length, &type,
					  &params);

		if (!(segh = dm_list_next(&lv->segments, segh))) {
		    log_error("Number of segments in active LV %s "
			      "does not match metadata.", display_lvname(lv));
		    goto out;
		}
		seg = dm_list_item(segh, struct lv_segment);

		if (!type || !params)
			continue;

		if (!seg) {
			log_error(INTERNAL_ERROR "Segment is not selected.");
			goto out;
		}

		if (seg->segtype->ops->check_transient_status &&
		    !seg->segtype->ops->check_transient_status(dm->mem, seg, params))
			goto_out;

	} while (next);

	if (dm_list_next(&lv->segments, segh)) {
		log_error("Number of segments in active LV %s does not "
			  "match metadata.", display_lvname(lv));
		goto out;
	}

	r = 1;

      out:
	dm_task_destroy(dmt);
	return r;
}

/*
 * dev_manager implementation.
 */
struct dev_manager *dev_manager_create(struct cmd_context *cmd,
				       const char *vg_name,
				       unsigned track_pvmove_deps)
{
	struct dm_pool *mem;
	struct dev_manager *dm;

	if (!(mem = dm_pool_create("dev_manager", 16 * 1024)))
		return_NULL;

	if (!(dm = dm_pool_zalloc(mem, sizeof(*dm))))
		goto_bad;

	dm->cmd = cmd;
	dm->mem = mem;
	dm->vg_name = vg_name;

	/*
	 * When we manipulate (normally suspend/resume) the PVMOVE
	 * device directly, there's no need to touch the LVs above.
	 */
	dm->track_pvmove_deps = track_pvmove_deps;

	dm->target_state = NULL;

	dm_udev_set_sync_support(cmd->current_settings.udev_sync);

	return dm;

      bad:
	dm_pool_destroy(mem);

	return NULL;
}

void dev_manager_destroy(struct dev_manager *dm)
{
	dm_pool_destroy(dm->mem);
}

void dev_manager_release(void)
{
	dm_lib_release();
}

void dev_manager_exit(void)
{
	dm_lib_exit();
}

int dev_manager_snapshot_percent(struct dev_manager *dm,
				 const struct logical_volume *lv,
				 dm_percent_t *percent)
{
	const struct logical_volume *snap_lv;
	char *name;
	const char *dlid;
	int fail_if_percent_unsupported = 0;

	if (lv_is_merging_origin(lv)) {
		/*
		 * Set 'fail_if_percent_unsupported', otherwise passing
		 * unsupported LV types to _percent will lead to a default
		 * successful return with percent_range as PERCENT_100.
		 * - For a merging origin, this will result in a polldaemon
		 *   that runs infinitely (because completion is PERCENT_0)
		 * - We unfortunately don't yet _know_ if a snapshot-merge
		 *   target is active (activation is deferred if dev is open);
		 *   so we can't short-circuit origin devices based purely on
		 *   existing LVM LV attributes.
		 */
		fail_if_percent_unsupported = 1;
	}

	if (lv_is_merging_cow(lv)) {
		/* must check percent of origin for a merging snapshot */
		snap_lv = origin_from_cow(lv);
	} else
		snap_lv = lv;

	/*
	 * Build a name for the top layer.
	 */
	if (!(name = dm_build_dm_name(dm->mem, snap_lv->vg->name, snap_lv->name, NULL)))
		return_0;

	if (!(dlid = build_dm_uuid(dm->mem, snap_lv, NULL)))
		return_0;

	/*
	 * Try and get some info on this device.
	 */
	if (!_percent(dm, name, dlid, TARGET_NAME_SNAPSHOT, 0, NULL, percent,
		      NULL, fail_if_percent_unsupported))
		return_0;

	/* If the snapshot isn't available, percent will be -1 */
	return 1;
}

/* FIXME Merge with snapshot_percent, auto-detecting target type */
/* FIXME Cope with more than one target */
int dev_manager_mirror_percent(struct dev_manager *dm,
			       const struct logical_volume *lv, int wait,
			       dm_percent_t *percent, uint32_t *event_nr)
{
	char *name;
	const char *dlid;
	const char *target_type = first_seg(lv)->segtype->name;
	const char *layer = lv_layer(lv);

	/*
	 * Build a name for the top layer.
	 */
	if (!(name = dm_build_dm_name(dm->mem, lv->vg->name, lv->name, layer)))
		return_0;

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	log_debug_activation("Getting device %s status percentage for %s.",
			     target_type, name);

	if (!_percent(dm, name, dlid, target_type, wait, lv, percent, event_nr, 0))
		return_0;

	return 1;
}

int dev_manager_raid_status(struct dev_manager *dm,
			    const struct logical_volume *lv,
			    struct lv_status_raid **status, int *exists)
{
	int r = 0;
	const char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	const char *layer = lv_layer(lv);
	struct dm_status_raid *sr;

	*exists = -1;
	if (!(*status = dm_pool_zalloc(dm->mem, sizeof(struct lv_status_cache))))
		return_0;

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, 0, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking raid status for volume %s.",
			     display_lvname(lv));

	dm_get_next_target(dmt, NULL, &start, &length, &type, &params);

	if (!type || strcmp(type, TARGET_NAME_RAID)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_RAID, type ? type : "NULL");
		goto out;
	}

	/* FIXME Check there's only one target */

	if (!dm_get_status_raid(dm->mem, params, &sr))
		goto_out;

	(*status)->mem = dm->mem; /* User has to destroy this mem pool later */
	(*status)->raid = sr;
	(*status)->in_sync = dm_make_percent(sr->insync_regions, sr->total_regions);

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_raid_message(struct dev_manager *dm,
			     const struct logical_volume *lv,
			     const char *msg)
{
	int r = 0;
	const char *dlid;
	struct dm_task *dmt;
	const char *layer = lv_layer(lv);

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

	/* These are the supported RAID messages for dm-raid v1.9.0 */
	if (strcmp(msg, "idle") &&
	    strcmp(msg, "frozen") &&
	    strcmp(msg, "resync") &&
	    strcmp(msg, "recover") &&
	    strcmp(msg, "check") &&
	    strcmp(msg, "repair")) {
		log_error(INTERNAL_ERROR "Unknown RAID message: %s.", msg);
		return 0;
	}

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TARGET_MSG, NULL, NULL, dlid, 0, 0, 0, 0, 1, 0)))
		return_0;

	if (!dm_task_set_message(dmt, msg))
		goto_out;

	if (!dm_task_run(dmt))
		goto_out;

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_writecache_message(struct dev_manager *dm,
				   const struct logical_volume *lv,
				   const char *msg)
{
	int r = 0;
	const char *dlid;
	struct dm_task *dmt;
	const char *layer = lv_layer(lv);

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

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TARGET_MSG, NULL, NULL, dlid, 0, 0, 0, 0, 1, 0)))
		return_0;

	if (!dm_task_set_message(dmt, msg))
		goto_out;

	if (!dm_task_run(dmt))
		goto_out;

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_cache_status(struct dev_manager *dm,
			     const struct logical_volume *lv,
			     struct lv_status_cache **status, int *exists)
{
	int r = 0;
	const char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	struct dm_status_cache *c;

	*exists = -1;
	if (!(dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, 0, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking status for cache volume %s.",
			     display_lvname(lv));

	dm_get_next_target(dmt, NULL, &start, &length, &type, &params);

	if (!type || strcmp(type, TARGET_NAME_CACHE)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_CACHE, type ? type : "NULL");
		goto out;
	}

	/*
	 * FIXME:
	 * ->target_percent() API is able to transfer only a single value.
	 * Needs to be able to pass whole structure.
	 */
	if (!dm_get_status_cache(dm->mem, params, &c))
		goto_out;

	if (!(*status = dm_pool_zalloc(dm->mem, sizeof(struct lv_status_cache))))
		goto_out;

	(*status)->mem = dm->mem; /* User has to destroy this mem pool later */
	(*status)->cache = c;
	if (c->fail || c->error) {
		(*status)->data_usage =
			(*status)->metadata_usage =
			(*status)->dirty_usage = DM_PERCENT_INVALID;
	} else {
		(*status)->data_usage = dm_make_percent(c->used_blocks,
							c->total_blocks);
		(*status)->metadata_usage = dm_make_percent(c->metadata_used_blocks,
							    c->metadata_total_blocks);
		(*status)->dirty_usage = (c->used_blocks) ?
			dm_make_percent(c->dirty_blocks,
					c->used_blocks) : DM_PERCENT_0;
	}
	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_thin_pool_status(struct dev_manager *dm,
				 const struct logical_volume *lv, int flush,
				 struct lv_status_thin_pool **status, int *exists)
{
	struct dm_status_thin_pool *dm_status;
	const char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	int r = 0;

	*exists = -1;
	if (!(*status = dm_pool_zalloc(dm->mem, sizeof(struct lv_status_thin_pool))))
		return_0;

	/* Build dlid for the thin pool layer */
	if (!(dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, flush, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking thin pool status for LV %s.",
			     display_lvname(lv));

	dm_get_next_target(dmt, NULL, &start, &length, &type, &params);

	if (!type || strcmp(type, TARGET_NAME_THIN_POOL)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_THIN_POOL, type ? type : "NULL");
		goto out;
	}

	if (!dm_get_status_thin_pool(dm->mem, params, &dm_status))
		goto_out;

	(*status)->mem = dm->mem;
	(*status)->thin_pool = dm_status;

	if (dm_status->fail || dm_status->error) {
		(*status)->data_usage =
			(*status)->metadata_usage = DM_PERCENT_INVALID;
	} else {
		(*status)->data_usage = dm_make_percent(dm_status->used_data_blocks,
							dm_status->total_data_blocks);
		(*status)->metadata_usage = dm_make_percent(dm_status->used_metadata_blocks,
							    dm_status->total_metadata_blocks);
	}

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_thin_status(struct dev_manager *dm,
			    const struct logical_volume *lv, int flush,
			    struct lv_status_thin **status, int *exists)
{
	struct dm_status_thin *dm_status;
	const char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	uint64_t start, length;
	char *type = NULL;
	char *params = NULL;
	uint64_t csize;
	int r = 0;

	*exists = -1;
	if (!(*status = dm_pool_zalloc(dm->mem, sizeof(struct lv_status_thin))))
		return_0;

	if (!(dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, flush, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking thin status for LV %s.",
			     display_lvname(lv));

	dm_get_next_target(dmt, NULL, &start, &length, &type, &params);

	if (!type || strcmp(type, TARGET_NAME_THIN)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_THIN, type ? type : "NULL");
		goto out;
	}

	if (!dm_get_status_thin(dm->mem, params, &dm_status))
		goto_out;

	(*status)->mem = dm->mem;
	(*status)->thin = dm_status;

	if (dm_status->fail)
		(*status)->usage = DM_PERCENT_INVALID;
	else {
		/* Pool allocates whole chunk so round-up to nearest one */
		csize = first_seg(first_seg(lv)->pool_lv)->chunk_size;
		csize = ((lv->size + csize - 1) / csize) * csize;
		if (dm_status->mapped_sectors > csize) {
			log_warn("WARNING: LV %s maps %s while the size is only %s.",
				 display_lvname(lv),
				 display_size(dm->cmd, dm_status->mapped_sectors),
				 display_size(dm->cmd, csize));
			/* Don't show nonsense numbers like i.e. 1000% full */
			dm_status->mapped_sectors = csize;
		}
		(*status)->usage = dm_make_percent(dm_status->mapped_sectors, csize);
	}

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

/*
 * Explore state of running DM table to obtain currently used deviceId
 */
int dev_manager_thin_device_id(struct dev_manager *dm,
			       const struct logical_volume *lv,
			       uint32_t *device_id, int *exists)
{
	const char *dlid;
	struct dm_task *dmt;
	struct dm_info info;
	uint64_t start, length;
	char *params, *target_type = NULL;
	const char *layer = lv_layer(lv);
	int r = 0;

	*exists = -1;
	if (lv_is_merging_origin(lv) && !lv_info(lv->vg->cmd, lv, 1, NULL, 0, 0))
		/* If the merge has already happened, that table
		 * can already be using correct LV without -real layer */
		layer = NULL;

	/* Build dlid for the thin layer */
	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, &info, NULL, dlid, 0, 0, 0, 0, 1, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking device id for LV %s.",
			     display_lvname(lv));

	if (dm_get_next_target(dmt, NULL, &start, &length,
			       &target_type, &params)) {
		log_error("More then one table line found for %s.",
			  display_lvname(lv));
		goto out;
	}

	if (!target_type || strcmp(target_type, TARGET_NAME_THIN)) {
		log_error("Unexpected target type %s found for thin %s.",
			  target_type, display_lvname(lv));
		goto out;
	}

	if (!params || sscanf(params, "%*u:%*u %u", device_id) != 1) {
		log_error("Cannot parse table like parameters %s for %s.",
			  params, display_lvname(lv));
		goto out;
	}

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_vdo_pool_status(struct dev_manager *dm,
				const struct logical_volume *lv, int flush,
				struct lv_status_vdo **status, int *exists)
{
	const char *dlid;
	struct dm_info info;
	uint64_t start, length;
	struct dm_task *dmt = NULL;
	char *type = NULL;
	char *params = NULL;
	int r = 0;

	*exists = -1;
	if (!(*status = dm_pool_zalloc(dm->mem, sizeof(struct lv_status_vdo))))
		return_0;

	if (!(dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_STATUS, &info, NULL, dlid, 0, 0, 0, 0, flush, 0)))
		return_0;

	if (!(*exists = info.exists))
		goto out;

	log_debug_activation("Checking VDO pool status for LV %s.",
			     display_lvname(lv));

	if (dm_get_next_target(dmt, NULL, &start, &length, &type, &params)) {
		log_error("More then one table line found for %s.",
			  display_lvname(lv));
		goto out;
	}

	if (!type || strcmp(type, TARGET_NAME_VDO)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_VDO, type ? type : "NULL");
		goto out;
	}

	if (!parse_vdo_pool_status(dm->mem, lv, params, &info, *status))
		goto_out;

	(*status)->mem = dm->mem;

	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

int dev_manager_vdo_pool_size_config(struct dev_manager *dm,
				     const struct logical_volume *lv,
				     struct vdo_pool_size_config *cfg)
{
	const char *dlid;
	struct dm_info info;
	uint64_t start, length;
	struct dm_task *dmt = NULL;
	char *type = NULL;
	char *params = NULL;
	int r = 0;
	unsigned version = 0;

	memset(cfg, 0, sizeof(*cfg));

	if (!(dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_TABLE, &info, NULL, dlid, 0, 0, 0, 0, 0, 0)))
		return_0;

	if (!info.exists)
		goto inactive; /* VDO device is not active, should not happen here... */

	log_debug_activation("Checking VDO pool table line for LV %s.",
			     display_lvname(lv));

	if (dm_get_next_target(dmt, NULL, &start, &length, &type, &params)) {
		log_error("More then one table line found for %s.",
			  display_lvname(lv));
		goto out;
	}

	if (!type || strcmp(type, TARGET_NAME_VDO)) {
		log_error("Expected %s segment type but got %s instead.",
			  TARGET_NAME_VDO, type ? type : "NULL");
		goto out;
	}

	if (sscanf(params, "V%u %*s " FMTu64 " %*u " FMTu32,
		   &version, &cfg->physical_size, &cfg->block_map_cache_size_mb) != 3) {
		log_error("Failed to parse VDO parameters %s for LV %s.",
			  params, display_lvname(lv));
		goto out;
	}

	switch (version) {
	case 2: break;
	case 4: break;
	default: log_warn("WARNING: Unknown VDO table line version %u.", version);
	}

	cfg->virtual_size = length;
	cfg->physical_size *= 8; // From 4K unit to 512B
	cfg->block_map_cache_size_mb /= 256; // From 4K unit to MiB
	cfg->index_memory_size_mb = first_seg(lv)->vdo_params.index_memory_size_mb; // Preserved

inactive:
	r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}


/*************************/
/*  NEW CODE STARTS HERE */
/*************************/

static int _dev_manager_lv_mknodes(const struct logical_volume *lv)
{
	char *name;

	if (!(name = dm_build_dm_name(lv->vg->cmd->mem, lv->vg->name,
				      lv->name, NULL)))
		return_0;

	return fs_add_lv(lv, name);
}

static int _dev_manager_lv_rmnodes(const struct logical_volume *lv)
{
	return fs_del_lv(lv);
}

static int _lv_has_mknode(const struct logical_volume *lv)
{
	return (lv_is_visible(lv) &&
		(!lv_is_thin_pool(lv) || lv_is_new_thin_pool(lv)));
}

int dev_manager_mknodes(const struct logical_volume *lv)
{
	struct dm_info dminfo;
	struct dm_task *dmt;
	char *name;
	int r = 0;

	if (!(name = dm_build_dm_name(lv->vg->cmd->mem, lv->vg->name, lv->name, NULL)))
		return_0;

	if (!(dmt = _setup_task_run(DM_DEVICE_MKNODES, &dminfo, name, NULL, 0, 0, 0, 0, 0, 0)))
		return_0;

	if (dminfo.exists) {
		/* read-only component LV is also made visible */
		if (_lv_has_mknode(lv) || (dminfo.read_only && lv_is_component(lv)))
			r = _dev_manager_lv_mknodes(lv);
	} else
		r = _dev_manager_lv_rmnodes(lv);

	dm_task_destroy(dmt);

	return r;
}

#ifdef UDEV_SYNC_SUPPORT
/*
 * Until the DM_UEVENT_GENERATED_FLAG was introduced in kernel patch
 * 856a6f1dbd8940e72755af145ebcd806408ecedd
 * some operations could not be performed by udev, requiring our fallback code.
 */
static int _dm_driver_has_stable_udev_support(void)
{
	char vsn[80];
	unsigned maj, min, patchlevel;

	return driver_version(vsn, sizeof(vsn)) &&
	       (sscanf(vsn, "%u.%u.%u", &maj, &min, &patchlevel) == 3) &&
	       (maj == 4 ? min >= 18 : maj > 4);
}

static int _check_udev_fallback(struct cmd_context *cmd)
{
	struct config_info *settings = &cmd->current_settings;

	if (settings->udev_fallback != -1)
		goto out;

	/*
	 * Use udev fallback automatically in case udev
	 * is disabled via DM_DISABLE_UDEV environment
	 * variable or udev rules are switched off.
	 */
	settings->udev_fallback = !settings->udev_rules ? 1 :
		find_config_tree_bool(cmd, activation_verify_udev_operations_CFG, NULL);

	/* Do not rely fully on udev if the udev support is known to be incomplete. */
	if (!settings->udev_fallback && !_dm_driver_has_stable_udev_support()) {
		log_very_verbose("Kernel driver has incomplete udev support so "
				 "LVM will check and perform some operations itself.");
		settings->udev_fallback = 1;
	}
out:
	return settings->udev_fallback;
}

#else /* UDEV_SYNC_SUPPORT */

static int _check_udev_fallback(struct cmd_context *cmd)
{
	/* We must use old node/symlink creation code if not compiled with udev support at all! */
	return cmd->current_settings.udev_fallback = 1;
}

#endif /* UDEV_SYNC_SUPPORT */

static uint16_t _get_udev_flags(struct dev_manager *dm, const struct logical_volume *lv,
				const char *layer, int noscan, int temporary,
				int visible_component)
{
	uint16_t udev_flags = 0;

	/*
	 * Instruct also libdevmapper to disable udev
	 * fallback in accordance to LVM2 settings.
	 */
	if (!_check_udev_fallback(dm->cmd))
		udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;

	/*
	 * Is this top-level and visible device?
	 * If not, create just the /dev/mapper content.
	 */
	/* FIXME: add target's method for this */
	if (lv_is_new_thin_pool(lv) || visible_component)
		/* New thin-pool is regular LV with -tpool UUID suffix. */
		udev_flags |= DM_UDEV_DISABLE_DISK_RULES_FLAG |
		              DM_UDEV_DISABLE_OTHER_RULES_FLAG;
	else if (layer || !lv_is_visible(lv) || lv_is_thin_pool(lv) || lv_is_vdo_pool(lv))
		udev_flags |= DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG |
			      DM_UDEV_DISABLE_DISK_RULES_FLAG |
			      DM_UDEV_DISABLE_OTHER_RULES_FLAG;
	/*
	 * There's no need for other udev rules to touch special LVs with
	 * reserved names. We don't need to populate /dev/disk here either.
	 * Even if they happen to be visible and top-level.
	 */
	else if (is_reserved_lvname(lv->name))
		udev_flags |= DM_UDEV_DISABLE_DISK_RULES_FLAG |
			      DM_UDEV_DISABLE_OTHER_RULES_FLAG;

	/*
	 * Snapshots and origins could have the same rule applied that will
	 * give symlinks exactly the same name (e.g. a name based on
	 * filesystem UUID). We give preference to origins to make such
	 * naming deterministic (e.g. symlinks in /dev/disk/by-uuid).
	 */
	if (lv_is_cow(lv))
		udev_flags |= DM_UDEV_LOW_PRIORITY_FLAG;

	/*
	 * Finally, add flags to disable /dev/mapper and /dev/<vgname> content
	 * to be created by udev if it is requested by user's configuration.
	 * This is basically an explicit fallback to old node/symlink creation
	 * without udev.
	 */
	if (!dm->cmd->current_settings.udev_rules)
		udev_flags |= DM_UDEV_DISABLE_DM_RULES_FLAG |
			      DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG;

	/*
	 * LVM subsystem specific flags.
	 */
	if (noscan)
		udev_flags |= DM_SUBSYSTEM_UDEV_FLAG0;

	if (temporary)
		udev_flags |= DM_UDEV_DISABLE_DISK_RULES_FLAG |
			      DM_UDEV_DISABLE_OTHER_RULES_FLAG;

	return udev_flags;
}

static int _add_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
			    const struct logical_volume *lv, int origin_only);
static int _add_new_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
				const struct logical_volume *lv,
				struct lv_activate_opts *laopts,
				const char *layer);
/*
 * Check for device holders (ATM used only for removed pvmove targets)
 * and add them into dtree structures.
 * When 'laopts != NULL' add them as new nodes - which also corrects READ_AHEAD.
 * Note: correct table are already explicitelly PRELOADED.
 */
static int _check_holder(struct dev_manager *dm, struct dm_tree *dtree,
			 const struct logical_volume *lv,
			 struct lv_activate_opts *laopts,
			 uint32_t major, const char *d_name)
{
	const char *default_uuid_prefix = dm_uuid_prefix();
	const size_t default_uuid_prefix_len = strlen(default_uuid_prefix);
	const char *name;
	const char *uuid;
	struct dm_info info;
	struct dm_task *dmt;
	struct logical_volume *lv_det;
	union lvid id;
	int dev, r = 0;

	errno = 0;
	dev = strtoll(d_name + 3, NULL, 10);
	if (errno) {
		log_error("Failed to parse dm device minor number from %s.", d_name);
		return 0;
	}

	if (!(dmt = _setup_task_run(DM_DEVICE_INFO, &info, NULL, NULL, NULL,
				    major, dev, 0, 0, 0)))
		return_0;

	if (info.exists) {
		uuid = dm_task_get_uuid(dmt);
		name = dm_task_get_name(dmt);

		log_debug_activation("Checking holder of %s  %s (" FMTu32 ":" FMTu32 ") %s.",
				     display_lvname(lv), uuid, info.major, info.minor,
				     name);

		/* Skip common uuid prefix */
		if (!strncmp(default_uuid_prefix, uuid, default_uuid_prefix_len))
			uuid += default_uuid_prefix_len;

		if (!memcmp(uuid, &lv->vg->id, ID_LEN) &&
		    !dm_tree_find_node_by_uuid(dtree, uuid)) {
			/* trims any UUID suffix (i.e. -cow) */
			(void) dm_strncpy((char*)&id, uuid, 2 * sizeof(struct id) + 1);

			/* If UUID is not yet in dtree, look for matching LV */
			if (!(lv_det = find_lv_in_vg_by_lvid(lv->vg, &id))) {
				log_error("Cannot find holder with device name %s in VG %s.",
					  name, lv->vg->name);
				goto out;
			}

			if (lv_is_cow(lv_det))
				lv_det = origin_from_cow(lv_det);
			log_debug_activation("Found holder %s of %s.",
					     display_lvname(lv_det),
					     display_lvname(lv));
			if (!laopts) {
				if (!_add_lv_to_dtree(dm, dtree, lv_det, 0))
					goto_out;
			} else if (!_add_new_lv_to_dtree(dm, dtree, lv_det, laopts, 0))
					goto_out;
		}
	}

        r = 1;
out:
	dm_task_destroy(dmt);

	return r;
}

/*
 * Add exiting devices which holds given LV device open.
 * This is used in case when metadata already do not contain information
 * i.e. PVMOVE is being finished and final table is going to be resumed.
 */
static int _add_holders_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
				 const struct logical_volume *lv,
				 struct lv_activate_opts *laopts,
				 const struct dm_info *info)
{
	const char *sysfs_dir = dm_sysfs_dir();
	char sysfs_path[PATH_MAX];
	struct dirent *dirent;
	DIR *d;
	int r = 0;

	/* Sysfs path of holders */
	if (dm_snprintf(sysfs_path, sizeof(sysfs_path), "%sblock/dm-" FMTu32
			"/holders", sysfs_dir, info->minor) < 0) {
		log_error("sysfs_path dm_snprintf failed.");
		return 0;
	}

	if (!(d = opendir(sysfs_path))) {
		log_sys_error("opendir", sysfs_path);
		return 0;
	}

	while ((dirent = readdir(d)))
		/* Expects minor is added to 'dm-' prefix */
		if (!strncmp(dirent->d_name, "dm-", 3) &&
		    !_check_holder(dm, dtree, lv, laopts, info->major, dirent->d_name))
			goto_out;

	r = 1;
out:
	if (closedir(d))
		log_sys_debug("closedir", "holders");

	return r;
}

static int _add_dev_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
			     const struct logical_volume *lv, const char *layer)
{
	char *dlid, *name;
	struct dm_info info, info2;
	const struct dm_active_device *dev;

	if (!(name = dm_build_dm_name(dm->mem, lv->vg->name, lv->name, layer)))
		return_0;

	if (!(dlid = build_dm_uuid(dm->track_pending_delete ? dm->cmd->pending_delete_mem : dm->mem, lv, layer)))
		return_0;

	if (!dm->cmd->disable_dm_devs &&
	    dm->cmd->cache_dm_devs) {
		if (!dm_device_list_find_by_uuid(dm->cmd->cache_dm_devs, dlid, &dev)) {
			log_debug("Cached as not present %s.", name);
			return 1;
		}
		info = (struct dm_info) {
			.exists = 1,
			.major = dev->major,
			.minor = dev->minor,
		};
		log_debug("Cached as present %s %s (%d:%d).",
			  name, dlid, info.major, info.minor);
	} else if (!_info(dm->cmd, name, dlid, 0, 0, 0, &info, NULL, NULL))
		return_0;
	/*
	 * For top level volumes verify that existing device match
	 * requested major/minor and that major/minor pair is available for use
	 */
	if (!layer && lv->major != -1 && lv->minor != -1) {
		/*
		 * FIXME compare info.major with lv->major if multiple major support
		 */
		if (info.exists && ((int) info.minor != lv->minor)) {
			log_error("Volume %s (%" PRIu32 ":%" PRIu32")"
				  " differs from already active device "
				  "(%" PRIu32 ":%" PRIu32").",
				  display_lvname(lv), lv->major, lv->minor,
				  info.major, info.minor);
			return 0;
		}
		if (!info.exists && _info_by_dev(lv->major, lv->minor, &info2) &&
		    info2.exists) {
			log_error("The requested major:minor pair "
				  "(%" PRIu32 ":%" PRIu32") is already used.",
				  lv->major, lv->minor);
			return 0;
		}
	}

	if (info.exists && !dm_tree_add_dev_with_udev_flags(dtree, info.major, info.minor,
							    _get_udev_flags(dm, lv, layer,
									    0, 0, 0))) {
		log_error("Failed to add device (%" PRIu32 ":%" PRIu32") to dtree.",
			  info.major, info.minor);
		return 0;
	}

	if (info.exists && dm->track_pending_delete) {
		log_debug_activation("Tracking pending delete for %s (%s).",
				     display_lvname(lv), dlid);
		if (!str_list_add(dm->cmd->pending_delete_mem, &dm->cmd->pending_delete, dlid))
			return_0;
	}

	/*
	 * Find holders of existing active LV where name starts with 'pvmove',
	 * but it's not anymore PVMOVE LV and also it's not PVMOVE _mimage
	 */
	if (info.exists && !lv_is_pvmove(lv) &&
	    !strchr(lv->name, '_') && !strncmp(lv->name, "pvmove", 6))
		if (!_add_holders_to_dtree(dm, dtree, lv, NULL, &info))
			return_0;

	return 1;
}

struct pool_cb_data {
	struct dev_manager *dm;
	const struct logical_volume *pool_lv;

	int skip_zero;  /* to skip zeroed device header (check first 64B) */
	int exec;       /* which binary to call */
	int opts;
	struct {
		unsigned maj;
		unsigned min;
		unsigned patch;
	} version;
	const char *global;
};

/*
 * Simple version of check function calling 'tool -V'
 *
 * Returns 1 if the tool's version is equal or better to given.
 * Otherwise it returns 0.
 */
static int _check_tool_version(struct cmd_context *cmd, const char *tool,
			       unsigned maj, unsigned min, unsigned patch)
{
	const char *argv[] = { tool, "-V", NULL };
	struct pipe_data pdata;
	FILE *f;
	char buf[128] = { 0 };
	char *nl;
	unsigned v_maj, v_min, v_patch;
	int ret = 0;

	if (!(f = pipe_open(cmd, argv, 0, &pdata))) {
		log_warn("WARNING: Cannot read output from %s.", argv[0]);
	} else {
		if (fgets(buf, sizeof(buf) - 1, f) &&
		    (sscanf(buf, "%u.%u.%u", &v_maj, &v_min, &v_patch) == 3)) {
			if ((v_maj > maj) ||
			    ((v_maj == maj) &&
			     ((v_min > min) ||
			      (v_min == min && v_patch >= patch))))
				ret = 1;

			if ((nl = strchr(buf, '\n')))
				nl[0] = 0; /* cut newline away */

			log_verbose("Found version of %s %s is %s then requested %u.%u.%u.",
				    argv[0], buf, ret ? "better" : "older", maj, min, patch);
		} else
			log_warn("WARNING: Cannot parse output '%s' from %s.", buf, argv[0]);

		(void) pipe_close(&pdata);
	}

	return ret;
}

static int _pool_callback(struct dm_tree_node *node,
			  dm_node_callback_t type, void *cb_data)
{
	int ret, status = 0, fd;
	const struct dm_config_node *cn;
	const struct dm_config_value *cv;
	const struct pool_cb_data *data = cb_data;
	const struct logical_volume *pool_lv = data->pool_lv;
	const struct logical_volume *mlv = first_seg(pool_lv)->metadata_lv;
	struct cmd_context *cmd = pool_lv->vg->cmd;
	long buf[64 / sizeof(long)]; /* buffer for short disk header (64B) */
	int args = 0;
	char *mpath;
	const char *argv[19] = { /* Max supported 15 args */
		find_config_tree_str_allow_empty(cmd, data->exec, NULL)
	};

	if (!*argv[0]) /* *_check tool is unconfigured/disabled with "" setting */
		return 1;

	if (lv_is_cache_vol(pool_lv)) {
		if (!(mpath = lv_dmpath_suffix_dup(data->dm->mem, pool_lv, "-cmeta"))) {
			log_error("Failed to build device path for checking cachevol metadata %s.",
			  	 display_lvname(pool_lv));
			return 0;
		}
	} else {
		if (!(mpath = lv_dmpath_dup(data->dm->mem, mlv))) {
			log_error("Failed to build device path for checking pool metadata %s.",
			  	 display_lvname(mlv));
			return 0;
		}
	}

	dm_device_list_destroy(&cmd->cache_dm_devs); /* Cache no longer valid */

	log_debug("Running check command on %s", mpath);

	if (data->skip_zero) {
		if ((fd = open(mpath, O_RDONLY)) < 0) {
			log_sys_error("open", mpath);
			return 0;
		}
		/* let's assume there is no problem to read 64 bytes */
		if (read(fd, buf, sizeof(buf)) < (int)sizeof(buf)) {
			log_sys_error("read", mpath);
			if (close(fd))
				log_sys_error("close", mpath);
			return 0;
		}
		for (ret = 0; ret < (int) DM_ARRAY_SIZE(buf); ++ret)
			if (buf[ret])
				break;

		if (close(fd))
			log_sys_error("close", mpath);

		if (ret == (int) DM_ARRAY_SIZE(buf)) {
			log_debug_activation("Metadata checking skipped, detected empty disk header on %s.",
					     mpath);
			return 1;
		}
	}

	if (!(cn = find_config_tree_array(cmd, data->opts, NULL))) {
		log_error(INTERNAL_ERROR "Unable to find configuration for pool check options.");
		return 0;
	}

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

	if (args == 16) {
		log_error("Too many options for %s command.", argv[0]);
		return 0;
	}

	argv[++args] = mpath;

	if (!(ret = exec_cmd(cmd, (const char * const *)argv,
			     &status, 0))) {
		if (status == ENOENT) {
			log_warn("WARNING: Check is skipped, please install recommended missing binary %s!",
				 argv[0]);
			return 1;
		}

		if ((data->version.maj || data->version.min || data->version.patch) &&
		    !_check_tool_version(cmd, argv[0],
					 data->version.maj, data->version.min, data->version.patch)) {
			log_warn("WARNING: Check is skipped, please upgrade installed version of %s!",
				 argv[0]);
			return 1;
		}
		switch (type) {
		case DM_NODE_CALLBACK_PRELOADED:
			log_err_once("Check of pool %s failed (status:%d). "
				     "Manual repair required!",
				     display_lvname(pool_lv), status);
			break;
		default:
			log_warn("WARNING: Integrity check of metadata for pool "
				 "%s failed.", display_lvname(pool_lv));
		}
		/*
		 * FIXME: What should we do here??
		 *
		 * Maybe mark the node, so it's not activating
		 * as pool but as error/linear and let the
		 * dm tree resolve the issue.
		 */
	}

	return ret;
}

static int _pool_register_callback(struct dev_manager *dm,
				   struct dm_tree_node *node,
				   const struct logical_volume *lv)
{
	struct pool_cb_data *data;

	/* Do not skip metadata of testing even for unused thin pools */
#if 0
	/* Skip metadata testing for unused thin pool. */
	if (lv_is_thin_pool(lv) &&
	    (!first_seg(lv)->transaction_id ||
	     ((first_seg(lv)->transaction_id == 1) &&
	      pool_has_message(first_seg(lv), NULL, 0))))
		return 1;
#endif

	if (!(data = dm_pool_zalloc(dm->mem, sizeof(*data)))) {
		log_error("Failed to allocated path for callback.");
		return 0;
	}

	data->dm = dm;

	if (lv_is_thin_pool(lv)) {
		data->pool_lv = lv;
		data->skip_zero = 1;
		data->exec = global_thin_check_executable_CFG;
		data->opts = global_thin_check_options_CFG;
		data->global = "thin";
	} else if (lv_is_cache(lv)) { /* cache pool */
		data->pool_lv = first_seg(lv)->pool_lv;
		data->skip_zero = 1; /* cheap read-error detection */
		data->exec = global_cache_check_executable_CFG;
		data->opts = global_cache_check_options_CFG;
		data->global = "cache";
		if (first_seg(first_seg(lv)->pool_lv)->cache_metadata_format > 1) {
			data->version.maj = 0;
			data->version.min = 7;
		}
	} else {
		log_error(INTERNAL_ERROR "Registering unsupported pool callback.");
		return 0;
	}

	dm_tree_node_set_callback(node, _pool_callback, data);

	return 1;
}

/* Add special devices _cmeta & _cdata on top of CacheVol to dm tree */
static int _add_cvol_subdev_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
				     const struct logical_volume *lv, int meta_or_data)
{
	const char *layer = meta_or_data ? "cmeta" : "cdata";
	struct dm_pool *mem = dm->track_pending_delete ? dm->cmd->pending_delete_mem : dm->mem;
	const struct logical_volume *pool_lv = first_seg(lv)->pool_lv;
	struct lv_segment *lvseg = first_seg(lv);
	struct dm_info info;
	char *name ,*dlid;
	union lvid lvid = { { { "" } } };

	memcpy(&lvid.id[0], &lv->vg->id, sizeof(struct id));
	/* When ID is provided in form of metadata_id or data_id, otherwise use CVOL ID */
	memcpy(&lvid.id[1],
	       (meta_or_data && lvseg->metadata_id) ? lvseg->metadata_id :
	       (lvseg->data_id) ? lvseg->data_id : &pool_lv->lvid.id[1], sizeof(struct id));

	if (!(dlid = dm_build_dm_uuid(mem, UUID_PREFIX, (const char *)&lvid.s, layer)))
		return_0;

	/* Name is actually not really needed here, but aids debugging... */
	if (!(name = dm_build_dm_name(dm->mem, lv->vg->name, pool_lv->name, layer)))
		return_0;

	if (!_info(dm->cmd, name, dlid, 1, 0, 0, &info, NULL, NULL))
		return_0;

	if (info.exists) {
		if (!dm_tree_add_dev_with_udev_flags(dtree, info.major, info.minor,
						     _get_udev_flags(dm, lv, layer, 0, 0, 0))) {
			log_error("Failed to add device (%" PRIu32 ":%" PRIu32") to dtree.", info.major, info.minor);
			return 0;
		}
		if (dm->track_pending_delete) {
			log_debug_activation("Tracking pending delete for %s %s (%s).",
					     layer, display_lvname(lv), dlid);
			if (!str_list_add(mem, &dm->cmd->pending_delete, dlid))
				return_0;
		}
	}

	return 1;
}

/* Declaration to resolve suspend tree and message passing for thin-pool */
static int _add_target_to_dtree(struct dev_manager *dm,
				struct dm_tree_node *dnode,
				struct lv_segment *seg,
				struct lv_activate_opts *laopts);
/*
 * Add LV and any known dependencies
 */
static int _add_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
			    const struct logical_volume *lv, int origin_only)
{
	uint32_t s;
	struct seg_list *sl;
	struct dm_list *snh;
	struct lv_segment *seg;
	struct dm_tree_node *node;
	const char *uuid;
	const struct logical_volume *plv;

	if (lv_is_pvmove(lv) && (dm->track_pvmove_deps == 2))
		return 1; /* Avoid rechecking of already seen pvmove LV */

	if (lv_is_cache_pool(lv)) {
		if (!dm_list_empty(&lv->segs_using_this_lv)) {
			if (!_add_lv_to_dtree(dm, dtree, seg_lv(first_seg(lv), 0), 0))
				return_0;
			if (!_add_lv_to_dtree(dm, dtree, first_seg(lv)->metadata_lv, 0))
				return_0;
			/* Cache pool does not have a real device node */
			return 1;
		}
		/* Unused cache pool is activated as metadata */
	}

	if (lv_is_cache(lv) && (plv = (first_seg(lv)->pool_lv)) && lv_is_cache_vol(plv)) {
		if (!_add_cvol_subdev_to_dtree(dm, dtree, lv, 0) ||
		    !_add_cvol_subdev_to_dtree(dm, dtree, lv, 1) ||
		    !_add_dev_to_dtree(dm, dtree, plv, lv_layer(plv)))
			return_0;
	}

	if (!origin_only && !_add_dev_to_dtree(dm, dtree, lv, NULL))
		return_0;

	/* FIXME Can we avoid doing this every time? */
	/* Reused also for lv_is_external_origin(lv) */
	if (!_add_dev_to_dtree(dm, dtree, lv, "real"))
		return_0;

	if (!origin_only && !_add_dev_to_dtree(dm, dtree, lv, "cow"))
		return_0;

	if (origin_only && lv_is_thin_volume(lv)) {
		if (!_add_dev_to_dtree(dm, dtree, lv, lv_layer(lv)))
			return_0;
#if 0
		/* ? Use origin_only to avoid 'deep' thin pool suspend ? */
		/* FIXME Implement dm_tree_node_skip_childrens optimisation */
		if (!(uuid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
			return_0;
		if ((node = dm_tree_find_node_by_uuid(dtree, uuid)))
			dm_tree_node_skip_childrens(node, 1);
#endif
	}

	if (origin_only && dm->activation && dm->track_external_lv_deps &&
	    lv_is_external_origin(lv)) {
		/* Find possible users of external origin lv */
		dm->track_external_lv_deps = 0; /* avoid recursion */
		dm_list_iterate_items(sl, &lv->segs_using_this_lv)
			/* Match only external_lv users */
			if ((sl->seg->external_lv == lv) &&
			    !_add_lv_to_dtree(dm, dtree, sl->seg->lv, 1))
				return_0;
		dm->track_external_lv_deps = 1;
	}

	if (lv_is_thin_pool(lv)) {
		/*
		 * For both origin_only and !origin_only
		 * skips test for -tpool-real and tpool-cow
		 */
		if (!_add_dev_to_dtree(dm, dtree, lv, lv_layer(lv)))
			return_0;

		/*
		 * TODO: change API and move this code
		 * Could be easier to handle this in _add_dev_to_dtree()
		 * and base this according to info.exists ?
		 */
		if (!dm->activation) {
			if (!(uuid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
				return_0;
			if ((node = dm_tree_find_node_by_uuid(dtree, uuid))) {
				if (origin_only) {
					struct lv_activate_opts laopts = {
						.origin_only = 1,
						.send_messages = 1 /* Node with messages */
					};
					/*
					 * Add some messsages if right node exist in the table only
					 * when building SUSPEND tree for origin-only thin-pool.
					 *
					 * TODO: Fix call of '_add_target_to_dtree()' to add message
					 * to thin-pool node as we already know the pool node exists
					 * in the table. Any better/cleaner API way ?
					 *
					 * Probably some 'new' target method to add messages for any node?
					 */
					if (dm->suspend &&
					    !dm_list_empty(&(first_seg(lv)->thin_messages)) &&
					    !_add_target_to_dtree(dm, node, first_seg(lv), &laopts))
						return_0;
				} else {
					/* Setup callback for non-activation partial tree */
					/* Activation gets own callback when needed */
					/* TODO: extend _cached_dm_info() to return dnode */
					if (!_pool_register_callback(dm, node, lv))
						return_0;
				}
			}
		}
	}

	if (lv_is_vdo_pool(lv)) {
		/*
		 * For both origin_only and !origin_only
		 * skips test for -vpool-real and vpool-cow
		 */
		if (!_add_dev_to_dtree(dm, dtree, lv, lv_layer(lv)))
			return_0;
	}

	if (lv_is_cache(lv)) {
		if (!origin_only && !dm->activation && !dm->track_pending_delete) {
			/* Setup callback for non-activation partial tree */
			/* Activation gets own callback when needed */
			/* TODO: extend _cached_dm_info() to return dnode */
			if (!(uuid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
				return_0;
			if ((node = dm_tree_find_node_by_uuid(dtree, uuid)) &&
			    !_pool_register_callback(dm, node, lv))
				return_0;
		}
	}

	/* Add any snapshots of this LV */
	if (!origin_only && lv_is_origin(lv))
		dm_list_iterate(snh, &lv->snapshot_segs)
			if (!_add_lv_to_dtree(dm, dtree, dm_list_struct_base(snh, struct lv_segment, origin_list)->cow, 0))
				return_0;

	if (dm->activation && !origin_only && lv_is_merging_origin(lv) &&
	    !_add_lv_to_dtree(dm, dtree, find_snapshot(lv)->lv, 1))
		return_0;

	/* Add any LVs referencing a PVMOVE LV unless told not to. */
	if ((dm->track_pvmove_deps == 1) && lv_is_pvmove(lv)) {
		dm->track_pvmove_deps = 2; /* Mark as already seen */
		dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
			/* If LV is snapshot COW - whole snapshot needs reload */
			plv = lv_is_cow(sl->seg->lv) ? origin_from_cow(sl->seg->lv) : sl->seg->lv;
			if (!_add_lv_to_dtree(dm, dtree, plv, 0))
				return_0;
		}
		dm->track_pvmove_deps = 1;
	}

	if (!dm->track_pending_delete)
		dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
			if (lv_is_pending_delete(sl->seg->lv)) {
				/* LV is referenced by 'cache pending delete LV */
				dm->track_pending_delete = 1;
				if (!_add_lv_to_dtree(dm, dtree, sl->seg->lv, origin_only))
					return_0;
				dm->track_pending_delete = 0;
			}
		}

	/* Add any LVs used by segments in this LV */
	dm_list_iterate_items(seg, &lv->segments) {
		if (seg->external_lv && dm->track_external_lv_deps &&
		    !_add_lv_to_dtree(dm, dtree, seg->external_lv, 1)) /* stack */
			return_0;
		if (seg->log_lv &&
		    !_add_lv_to_dtree(dm, dtree, seg->log_lv, 0))
			return_0;
		if (seg->metadata_lv &&
		    !_add_lv_to_dtree(dm, dtree, seg->metadata_lv, 0))
			return_0;
		if (seg->writecache && seg_is_writecache(seg)) {
			if (!_add_lv_to_dtree(dm, dtree, seg->writecache, dm->activation ? origin_only : 1))
				return_0;
		}
		if (seg->integrity_meta_dev && seg_is_integrity(seg)) {
			if (!_add_lv_to_dtree(dm, dtree, seg->integrity_meta_dev, dm->activation ? origin_only : 1))
				return_0;
		}
		if (seg->pool_lv &&
		    (lv_is_cache_pool(seg->pool_lv) || lv_is_cache_vol(seg->pool_lv) || dm->track_external_lv_deps) &&
		    /* When activating and not origin_only detect linear 'overlay' over pool */
		    !_add_lv_to_dtree(dm, dtree, seg->pool_lv, dm->activation ? origin_only : 1))
			return_0;

		for (s = 0; s < seg->area_count; s++) {
			if (seg_type(seg, s) == AREA_LV && seg_lv(seg, s) &&
			    /* origin only for cache without pending delete */
			    (!dm->track_pending_delete || !lv_is_cache(lv)) &&
			    !_add_lv_to_dtree(dm, dtree, seg_lv(seg, s),
					      lv_is_vdo_pool(seg_lv(seg, s)) ? 1 : 0))
				return_0;
			if (seg_is_raid_with_meta(seg) && seg->meta_areas && seg_metalv(seg, s) &&
			    !_add_lv_to_dtree(dm, dtree, seg_metalv(seg, s), 0))
				return_0;
		}

		/* When activating, detect merging LV presence */
		if (dm->activation && seg->merge_lv &&
		    !_add_lv_to_dtree(dm, dtree, seg->merge_lv, 1))
			return_0;
	}

	return 1;
}

static struct dm_tree *_create_partial_dtree(struct dev_manager *dm, const struct logical_volume *lv, int origin_only)
{
	struct dm_tree *dtree;

	if (!(dtree = dm_tree_create())) {
		log_debug_activation("Partial dtree creation failed for %s.",
				     display_lvname(lv));
		return NULL;
	}

	dm_tree_set_optional_uuid_suffixes(dtree, &uuid_suffix_list[0]);

	if (!_add_lv_to_dtree(dm, dtree, lv, (lv_is_origin(lv) || lv_is_thin_volume(lv) || lv_is_thin_pool(lv)) ? origin_only : 0))
		goto_bad;

	return dtree;

bad:
	dm_tree_free(dtree);
	return NULL;
}

static char *_add_error_or_zero_device(struct dev_manager *dm, struct dm_tree *dtree,
				       struct lv_segment *seg, int s, int use_zero)
{
	char *dlid, *name;
	char errid[32];
	struct dm_tree_node *node;
	struct lv_segment *seg_i;
	struct dm_info info;
	int segno = -1, i = 0;
	uint64_t size = (uint64_t) _seg_len(seg) * seg->lv->vg->extent_size;

	dm_list_iterate_items(seg_i, &seg->lv->segments) {
		if (seg == seg_i) {
			segno = i;
			break;
		}
		++i;
	}

	if (segno < 0) {
		log_error(INTERNAL_ERROR "_add_error_or_zero_device called with bad segment.");
		return NULL;
	}

	sprintf(errid, "missing_%d_%d", segno, s);

	if (!(dlid = build_dm_uuid(dm->mem, seg->lv, errid)))
		return_NULL;

	if (!(name = dm_build_dm_name(dm->mem, seg->lv->vg->name,
				      seg->lv->name, errid)))
		return_NULL;

	if (!_info(dm->cmd, name, dlid, 1, 0, 0, &info, NULL, NULL))
		return_NULL;

	if (!info.exists) {
		/* Create new node */
		if (!(node = dm_tree_add_new_dev(dtree, name, dlid, 0, 0, 0, 0, 0)))
			return_NULL;

		if (use_zero) {
			if (!dm_tree_node_add_zero_target(node, size))
				return_NULL;
		} else
			if (!dm_tree_node_add_error_target(node, size))
				return_NULL;
	} else {
		/* Already exists */
		if (!dm_tree_add_dev(dtree, info.major, info.minor)) {
			log_error("Failed to add device (%" PRIu32 ":%" PRIu32") to dtree.",
				  info.major, info.minor);
			return NULL;
		}
	}

	return dlid;
}

static int _add_error_area(struct dev_manager *dm, struct dm_tree_node *node,
			   struct lv_segment *seg, int s)
{
	char *dlid;
	uint64_t extent_size = seg->lv->vg->extent_size;
	int use_zero = !strcmp(dm->cmd->stripe_filler, TARGET_NAME_ZERO) ? 1 : 0;

	if (!strcmp(dm->cmd->stripe_filler, TARGET_NAME_ERROR) || use_zero) {
		/*
		 * FIXME, the tree pointer is first field of dm_tree_node, but
		 * we don't have the struct definition available.
		 */
		struct dm_tree **tree = (struct dm_tree **) node;
		if (!(dlid = _add_error_or_zero_device(dm, *tree, seg, s, use_zero)))
			return_0;
		if (!dm_tree_node_add_target_area(node, NULL, dlid, extent_size * seg_le(seg, s)))
			return_0;
	} else
		if (!dm_tree_node_add_target_area(node, dm->cmd->stripe_filler, NULL, UINT64_C(0)))
			return_0;

	return 1;
}

static int _bad_pv_area(struct lv_segment *seg, uint32_t s)
{
	struct stat info;
	const char *name;
	struct device *dev;

	if (!seg_pvseg(seg, s))
		return 1;
	if (!seg_pv(seg, s))
		return 1;
	if (!(dev = seg_dev(seg, s)))
		return 1;
	if (dm_list_empty(&dev->aliases))
		return 1;
	/* FIXME Avoid repeating identical stat in dm_tree_node_add_target_area */
	name = dev_name(dev);
	if (stat(name, &info) < 0)
		return 1;
	if (!S_ISBLK(info.st_mode))
		return 1;
	return 0;
}

int add_areas_line(struct dev_manager *dm, struct lv_segment *seg,
		   struct dm_tree_node *node, uint32_t start_area,
		   uint32_t areas)
{
	struct cmd_context *cmd = seg->lv->vg->cmd;
	uint64_t extent_size = seg->lv->vg->extent_size;
	uint32_t s;
	char *dlid;
	const char *name;
	unsigned num_error_areas = 0;
	unsigned num_existing_areas = 0;

	for (s = start_area; s < areas; s++) {
		if (((seg_type(seg, s) == AREA_PV) && _bad_pv_area(seg, s)) ||
		    ((seg_type(seg, s) == AREA_LV) && !seg_lv(seg, s))) {
			if (!cmd->partial_activation) {
				if (!cmd->degraded_activation || !lv_is_raid_type(seg->lv)) {
					log_error("Aborting.  LV %s is incomplete and --activationmode partial was not specified.",
						  display_lvname(seg->lv));
					return 0;
				}
			}
			if (!_add_error_area(dm, node, seg, s))
				return_0;
			num_error_areas++;
		} else if (seg_type(seg, s) == AREA_PV) {
			struct device *dev = seg_dev(seg, s);
			name = dm_list_empty(&dev->aliases) ? NULL : dev_name(dev);

			if (!dm_tree_node_add_target_area(node, name, NULL,
				    (seg_pv(seg, s)->pe_start + (extent_size * seg_pe(seg, s)))))
				return_0;
			num_existing_areas++;
		} else if (seg_is_raid(seg)) {
			/*
			 * RAID can handle unassigned areas.  It simple puts
			 * '- -' in for the metadata/data device pair.  This
			 * is a valid way to indicate to the RAID target that
			 * the device is missing.
			 *
			 * If an image is marked as VISIBLE_LV and !LVM_WRITE,
			 * it means the device has temporarily been extracted
			 * from the array.  It may come back at a future date,
			 * so the bitmap must track differences.  Again, '- -'
			 * is used in the CTR table.
			 */
			if ((seg_type(seg, s) == AREA_UNASSIGNED) ||
			    (lv_is_visible(seg_lv(seg, s)) &&
			     !(seg_lv(seg, s)->status & LVM_WRITE))) {
				/* One each for metadata area and data area */
				if (!dm_tree_node_add_null_area(node, 0) ||
				    !dm_tree_node_add_null_area(node, 0))
					return_0;
				continue;
			}

			if (seg->meta_areas && seg_metalv(seg, s)) {
				if (!(dlid = build_dm_uuid(dm->mem, seg_metalv(seg, s), NULL)))
					return_0;
				if (!dm_tree_node_add_target_area(node, NULL, dlid, extent_size * seg_metale(seg, s)))
					return_0;
			} else if (!dm_tree_node_add_null_area(node, 0))
				return_0;

			if (!(dlid = build_dm_uuid(dm->mem, seg_lv(seg, s), NULL)))
				return_0;
			if (!dm_tree_node_add_target_area(node, NULL, dlid, extent_size * seg_le(seg, s)))
				return_0;
		} else if (seg_type(seg, s) == AREA_LV) {

			if (!(dlid = build_dm_uuid(dm->mem, seg_lv(seg, s), NULL)))
				return_0;
			if (!dm_tree_node_add_target_area(node, NULL, dlid, extent_size * seg_le(seg, s)))
				return_0;
		} else {
			log_error(INTERNAL_ERROR "Unassigned area found in LV %s.",
				  display_lvname(seg->lv));
			return 0;
		}
	}

        if (num_error_areas) {
		/* Thins currently do not support partial activation */
		if (lv_is_thin_type(seg->lv)) {
			log_error("Cannot activate %s: pool incomplete.",
				  display_lvname(seg->lv));
			return 0;
		}
	}

	return 1;
}

static int _add_layer_target_to_dtree(struct dev_manager *dm,
				      struct dm_tree_node *dnode,
				      const struct logical_volume *lv)
{
	const char *layer_dlid;

	if (!(layer_dlid = build_dm_uuid(dm->mem, lv, lv_layer(lv))))
		return_0;


	/* Add linear mapping over layered LV */
	/* From VDO layer expose ONLY vdo pool header, we would need to use virtual size otherwise */
	if (!add_linear_area_to_dtree(dnode, lv_is_vdo_pool(lv) ? 8 : lv->size,
				      lv->vg->extent_size,
				      lv->vg->cmd->use_linear_target,
				      lv->vg->name, lv->name) ||
	    !dm_tree_node_add_target_area(dnode, NULL, layer_dlid, 0))
		return_0;

	return 1;
}

static int _add_origin_target_to_dtree(struct dev_manager *dm,
				       struct dm_tree_node *dnode,
				       const struct logical_volume *lv)
{
	const char *real_dlid;

	if (!(real_dlid = build_dm_uuid(dm->mem, lv, "real")))
		return_0;

	if (!dm_tree_node_add_snapshot_origin_target(dnode, lv->size, real_dlid))
		return_0;

	return 1;
}

static int _add_snapshot_merge_target_to_dtree(struct dev_manager *dm,
					       struct dm_tree_node *dnode,
					       const struct logical_volume *lv)
{
	const char *origin_dlid, *cow_dlid, *merge_dlid;
	struct lv_segment *merging_snap_seg = find_snapshot(lv);

	if (!lv_is_merging_origin(lv)) {
		log_error(INTERNAL_ERROR "LV %s is not merging snapshot.",
			  display_lvname(lv));
		return 0;
	}

	if (!(origin_dlid = build_dm_uuid(dm->mem, lv, "real")))
		return_0;

	if (!(cow_dlid = build_dm_uuid(dm->mem, merging_snap_seg->cow, "cow")))
		return_0;

	if (!(merge_dlid = build_dm_uuid(dm->mem, merging_snap_seg->cow, NULL)))
		return_0;

	if (!dm_tree_node_add_snapshot_merge_target(dnode, lv->size, origin_dlid,
						    cow_dlid, merge_dlid,
						    merging_snap_seg->chunk_size))
		return_0;

	return 1;
}

static int _add_snapshot_target_to_dtree(struct dev_manager *dm,
					 struct dm_tree_node *dnode,
					 const struct logical_volume *lv,
					 struct lv_activate_opts *laopts)
{
	const char *origin_dlid;
	const char *cow_dlid;
	struct lv_segment *snap_seg;
	uint64_t size;

	if (!(snap_seg = find_snapshot(lv))) {
		log_error("Couldn't find snapshot for '%s'.",
			  display_lvname(lv));
		return 0;
	}

	if (!(origin_dlid = build_dm_uuid(dm->mem, snap_seg->origin, "real")))
		return_0;

	if (!(cow_dlid = build_dm_uuid(dm->mem, snap_seg->cow, "cow")))
		return_0;

	size = (uint64_t) snap_seg->len * snap_seg->origin->vg->extent_size;

	if (!laopts->no_merging && lv_is_merging_cow(lv)) {
		/* cow is to be merged so load the error target */
		if (!dm_tree_node_add_error_target(dnode, size))
			return_0;
	}
	else if (!dm_tree_node_add_snapshot_target(dnode, size, origin_dlid,
						   cow_dlid, 1, snap_seg->chunk_size))
		return_0;

	return 1;
}

static int _add_target_to_dtree(struct dev_manager *dm,
				struct dm_tree_node *dnode,
				struct lv_segment *seg,
				struct lv_activate_opts *laopts)
{
	uint64_t extent_size = seg->lv->vg->extent_size;

	if (!seg->segtype->ops->add_target_line) {
		log_error(INTERNAL_ERROR "_emit_target cannot handle "
			  "segment type %s.", lvseg_name(seg));
		return 0;
	}

	return seg->segtype->ops->add_target_line(dm, dm->mem, dm->cmd,
						  &dm->target_state, seg,
						  laopts, dnode,
						  extent_size * _seg_len(seg),
						  &dm->pvmove_mirror_count);
}

static int _add_new_external_lv_to_dtree(struct dev_manager *dm,
					 struct dm_tree *dtree,
					 struct logical_volume *external_lv,
					 struct lv_activate_opts *laopts)
{
	struct seg_list *sl;

	/* Do not want to recursively add externals again */
	if (!dm->track_external_lv_deps)
		return 1;

	/*
	 * Any LV can have only 1 external origin, so we will
	 * process all LVs related to this LV, and we want to
	 * skip repeated invocation of external lv processing
	 */
	dm->track_external_lv_deps = 0;

	log_debug_activation("Adding external origin LV %s and all active users.",
			     display_lvname(external_lv));

	if (!_add_new_lv_to_dtree(dm, dtree, external_lv, laopts,
				  lv_layer(external_lv)))
		return_0;

	/*
	 * Add all ACTIVE LVs using this external origin LV. This is
	 * needed because of conversion of thin which could have been
	 * also an old-snapshot to external origin.
	 */
	dm_list_iterate_items(sl, &external_lv->segs_using_this_lv)
		if ((sl->seg->external_lv == external_lv) &&
		    /* Add only active layered devices (also avoids loop) */
		    _cached_dm_info(dm->mem, dtree, sl->seg->lv,
				    lv_layer(sl->seg->lv)) &&
		    !_add_new_lv_to_dtree(dm, dtree, sl->seg->lv,
					  laopts, lv_layer(sl->seg->lv)))
			return_0;

	log_debug_activation("Finished adding external origin LV %s and all active users.",
			     display_lvname(external_lv));

	dm->track_external_lv_deps = 1;

	return 1;
}

static int _add_segment_to_dtree(struct dev_manager *dm,
				 struct dm_tree *dtree,
				 struct dm_tree_node *dnode,
				 struct lv_segment *seg,
				 struct lv_activate_opts *laopts,
				 const char *layer)
{
	uint32_t s;
	struct lv_segment *seg_present;
	const struct segment_type *segtype;
	const char *target_name;

	/* Ensure required device-mapper targets are loaded */
	seg_present = find_snapshot(seg->lv) ? : seg;
	segtype = seg_present->segtype;

	target_name = (segtype->ops->target_name ?
		       segtype->ops->target_name(seg_present, laopts) :
		       segtype->name);

	log_debug_activation("Checking kernel supports %s segment type for %s%s%s",
			     target_name, display_lvname(seg->lv),
			     layer ? "-" : "", layer ? : "");

	if (segtype->ops->target_present &&
	    !segtype->ops->target_present(seg_present->lv->vg->cmd,
					  seg_present, NULL)) {
		log_error("Can't process LV %s: %s target support missing "
			  "from kernel?", display_lvname(seg->lv), target_name);
		return 0;
	}

	/* Add external origin layer */
	if (seg->external_lv &&
	    !_add_new_external_lv_to_dtree(dm, dtree, seg->external_lv, laopts))
		return_0;

	/* Add mirror log */
	if (seg->log_lv &&
	    !_add_new_lv_to_dtree(dm, dtree, seg->log_lv, laopts, NULL))
		return_0;

	/* Add pool metadata */
	if (seg->metadata_lv &&
	    !_add_new_lv_to_dtree(dm, dtree, seg->metadata_lv, laopts, NULL))
		return_0;

	/* Add pool layer */
	if (seg->pool_lv && !laopts->origin_only &&
	    !_add_new_lv_to_dtree(dm, dtree, seg->pool_lv, laopts,
				  lv_layer(seg->pool_lv)))
		return_0;

	if (seg->writecache && !laopts->origin_only &&
	    !_add_new_lv_to_dtree(dm, dtree, seg->writecache, laopts,
				  lv_layer(seg->writecache)))
		return_0;

	if (seg->integrity_meta_dev && !laopts->origin_only &&
	    !_add_new_lv_to_dtree(dm, dtree, seg->integrity_meta_dev, laopts,
				  lv_layer(seg->integrity_meta_dev)))
		return_0;

	/* Add any LVs used by this segment */
	for (s = 0; s < seg->area_count; ++s) {
		if ((seg_type(seg, s) == AREA_LV) &&
		    /* do not bring up tracked image */
		    !lv_is_raid_image_with_tracking(seg_lv(seg, s)) &&
		    /* origin only for cache without pending delete */
		    (!dm->track_pending_delete || !seg_is_cache(seg)) &&
		    !_add_new_lv_to_dtree(dm, dtree, seg_lv(seg, s),
					  laopts,
					  lv_is_vdo_pool(seg_lv(seg, s)) ?
					  lv_layer(seg_lv(seg, s)) : NULL))
			return_0;
		if (seg_is_raid_with_meta(seg) && seg->meta_areas && seg_metalv(seg, s) &&
		    !lv_is_raid_image_with_tracking(seg_lv(seg, s)) &&
		    !_add_new_lv_to_dtree(dm, dtree, seg_metalv(seg, s),
					  laopts, NULL))
			return_0;
	}

	if (dm->track_pending_delete) {
		/* Replace target and all its used devs with error mapping */
		log_debug_activation("Using error for pending delete %s.",
				     display_lvname(seg->lv));
		if (!dm_tree_node_add_error_target(dnode, (uint64_t)seg->lv->vg->extent_size * _seg_len(seg)))
			return_0;
	} else if (!_add_target_to_dtree(dm, dnode, seg, laopts))
		return_0;

	return 1;
}

static int _add_new_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree,
				const struct logical_volume *lv, struct lv_activate_opts *laopts,
				const char *layer)
{
	struct lv_segment *seg;
	struct lv_layer *lvlayer;
	struct seg_list *sl;
	struct dm_list *snh;
	struct dm_tree_node *dnode;
	const struct dm_info *dinfo;
	char *name, *dlid;
	uint32_t max_stripe_size = UINT32_C(0);
	uint32_t read_ahead = lv->read_ahead;
	uint32_t read_ahead_flags = UINT32_C(0);
	int save_pending_delete = dm->track_pending_delete;
	int merge_in_progress = 0;

	if (!(lvlayer = dm_pool_alloc(dm->mem, sizeof(*lvlayer)))) {
		log_error("_add_new_lv_to_dtree: pool alloc failed for %s %s.",
			  display_lvname(lv), layer);
		return 0;
	}
	lvlayer->lv = lv;
	lvlayer->visible_component = (laopts->component_lv == lv) ? 1 : 0;

	log_debug_activation("Adding new LV %s%s%s to dtree", display_lvname(lv),
			     layer ? "-" : "", layer ? : "");
	/* LV with pending delete is never put new into a table */
	if (lv_is_pending_delete(lv) && !_cached_dm_info(dm->mem, dtree, lv, NULL))
		return 1; /* Replace with error only when already exists */

	if (lv_is_cache_pool(lv) &&
	    !dm_list_empty(&lv->segs_using_this_lv)) {
		/* cache pool is 'meta' LV and does not have a real device node */
		if (!_add_new_lv_to_dtree(dm, dtree, seg_lv(first_seg(lv), 0), laopts, NULL))
			return_0;
		if (!_add_new_lv_to_dtree(dm, dtree, first_seg(lv)->metadata_lv, laopts, NULL))
			return_0;
		return 1;
	}

	if (lv_is_cache(lv) && lv_is_cache_vol(first_seg(lv)->pool_lv)) {
		struct logical_volume *pool_lv = first_seg(lv)->pool_lv;
		struct lv_segment *lvseg = first_seg(lv);
		struct volume_group *vg = lv->vg;
		struct dm_tree_node *dnode_meta;
		struct dm_tree_node *dnode_data;
		union lvid lvid_meta;
		union lvid lvid_data;
		char *name_meta;
		char *name_data;
		char *dlid_meta;
		char *dlid_data;
		char *dlid_pool;
		uint64_t meta_size = first_seg(lv)->metadata_len;
		uint64_t data_size = first_seg(lv)->data_len;
		uint16_t udev_flags = _get_udev_flags(dm, lv, layer,
					     laopts->noscan, laopts->temporary,
					     0);

		if (lv_is_pending_delete(lvseg->lv))
			dm->track_pending_delete = 1;

		log_debug("Add cachevol %s to dtree before cache %s.", pool_lv->name, lv->name);

		if (!_add_new_lv_to_dtree(dm, dtree, pool_lv, laopts, lv_layer(pool_lv))) {
			log_error("Failed to add cachevol to dtree before cache.");
			return 0;
		}

		memset(&lvid_meta, 0, sizeof(lvid_meta));
		memset(&lvid_data, 0, sizeof(lvid_meta));
		memcpy(&lvid_meta.id[0], &vg->id, sizeof(struct id));
		memcpy(&lvid_meta.id[1], lvseg->metadata_id ? : &pool_lv->lvid.id[1], sizeof(struct id));
		memcpy(&lvid_data.id[0], &vg->id, sizeof(struct id));
		memcpy(&lvid_data.id[1], lvseg->data_id ? : &pool_lv->lvid.id[1], sizeof(struct id));

		if (!(dlid_meta = dm_build_dm_uuid(dm->mem, UUID_PREFIX, (const char *)&lvid_meta.s, "cmeta")))
			return_0;
		if (!(dlid_data = dm_build_dm_uuid(dm->mem, UUID_PREFIX, (const char *)&lvid_data.s, "cdata")))
			return_0;

		if (!(name_meta = dm_build_dm_name(dm->mem, vg->name, pool_lv->name, "cmeta")))
			return_0;
		if (!(name_data = dm_build_dm_name(dm->mem, vg->name, pool_lv->name, "cdata")))
			return_0;

		if (!(dlid_pool = build_dm_uuid(dm->mem, pool_lv, NULL)))
			return_0;

		/* add meta dnode */
		if (!(dnode_meta = dm_tree_add_new_dev_with_udev_flags(dtree,
								  name_meta,
								  dlid_meta,
								  -1, -1,
								  read_only_lv(lv, laopts, layer),
								  ((lv->vg->status & PRECOMMITTED) | laopts->revert) ? 1 : 0,
								  lvlayer,
								  udev_flags)))
			return_0;

		if (dm->track_pending_delete) {
			log_debug_activation("Using error for pending meta delete %s.", display_lvname(lv));
			if (!dm_tree_node_add_error_target(dnode_meta, meta_size))
				return_0;
		} else {
			/* add load_segment to meta dnode: linear, size of meta area */
			if (!add_linear_area_to_dtree(dnode_meta,
						      meta_size,
						      lv->vg->extent_size,
						      lv->vg->cmd->use_linear_target,
						      lv->vg->name, lv->name))
				return_0;

			/* add seg_area to prev load_seg: offset 0 maps to cachepool lv offset 0 */
			if (!dm_tree_node_add_target_area(dnode_meta, NULL, dlid_pool, 0))
				return_0;
		}

		/* add data dnode */
		if (!(dnode_data = dm_tree_add_new_dev_with_udev_flags(dtree,
								  name_data,
								  dlid_data,
								  -1, -1,
								  read_only_lv(lv, laopts, layer),
								  ((lv->vg->status & PRECOMMITTED) | laopts->revert) ? 1 : 0,
								  lvlayer,
								  udev_flags)))
			return_0;

		if (dm->track_pending_delete) {
			log_debug_activation("Using error for pending data delete %s.", display_lvname(lv));
			if (!dm_tree_node_add_error_target(dnode_data, data_size))
				return_0;
		} else {
			/* add load_segment to data dnode: linear, size of data area */
			if (!add_linear_area_to_dtree(dnode_data,
						      data_size,
						      lv->vg->extent_size,
						      lv->vg->cmd->use_linear_target,
						      lv->vg->name, lv->name))
				return_0;

			/* add seg_area to prev load_seg: offset 0 maps to cachepool lv after meta */
			if (!dm_tree_node_add_target_area(dnode_data, NULL, dlid_pool, meta_size))
				return_0;
		}
	}

	/* FIXME Seek a simpler way to lay out the snapshot-merge tree. */

	if (!layer && lv_is_merging_origin(lv)) {
		seg = find_snapshot(lv);
		/*
		 * Prevent merge if merge isn't currently possible:
		 * either origin or merging snapshot are open
		 * - for old snaps use "snapshot-merge" if it is already in use
		 * - open_count is always retrieved (as of dm-ioctl 4.7.0)
		 *   so just use the tree's existing nodes' info
		 */
		if ((dinfo = _cached_dm_info(dm->mem, dtree, lv, NULL))) {
			/* Merging origin LV is present, check if mergins is already running. */
			if ((seg_is_thin_volume(seg) && _lv_has_thin_device_id(dm->mem, lv, NULL, seg->device_id)) ||
			    (!seg_is_thin_volume(seg) && lv_has_target_type(dm->mem, lv, NULL, TARGET_NAME_SNAPSHOT_MERGE))) {
				log_debug_activation("Merging of snapshot volume %s to origin %s is in progress.",
						     display_lvname(seg->lv), display_lvname(seg->lv));
				merge_in_progress = 1; /* Merge is already running */
			} /* Merge is not yet running, so check if it can be started */
			else if (laopts->resuming) {
				log_debug_activation("Postponing pending snapshot merge for origin %s, "
						     "merge was not started before suspend.",
						     display_lvname(lv));
				laopts->no_merging = 1; /* Cannot be reloaded in suspend */
			} /* Non-resuming merge requires origin to be unused */
			else if (dinfo->open_count) {
				log_debug_activation("Postponing pending snapshot merge for origin %s, "
						     "origin volume is opened.",
						     display_lvname(lv));
				laopts->no_merging = 1;
			}
		}

		/* If merge would be still undecided, look as snapshot */
		if (!merge_in_progress && !laopts->no_merging &&
		    (dinfo = _cached_dm_info(dm->mem, dtree,
					     seg_is_thin_volume(seg) ?
					     seg->lv : seg->cow, NULL))) {
			if (seg_is_thin_volume(seg)) {
				/* Active thin snapshot prevents merge */
				log_debug_activation("Postponing pending snapshot merge for origin volume %s, "
						     "merging thin snapshot volume %s is active.",
						     display_lvname(lv), display_lvname(seg->lv));
				laopts->no_merging = 1;
			} else if (dinfo->open_count) {
				log_debug_activation("Postponing pending snapshot merge for origin volume %s, "
						     "merging snapshot volume %s is opened.",
						     display_lvname(lv), display_lvname(seg->lv));
				laopts->no_merging = 1;
			}
		}
	}

	if (!(name = dm_build_dm_name(dm->mem, lv->vg->name, lv->name, layer)))
		return_0;

	/* Even unused thin-pool still needs to get layered  UUID -suffix */
	if (!layer && lv_is_new_thin_pool(lv))
		layer = lv_layer(lv);

	/* Adds -real to the dm uuid of wcorig LV. */
	if (!layer && lv_is_writecache_origin(lv))
		layer = lv_layer(lv); /* "real" */

	if (!(dlid = build_dm_uuid(dm->mem, lv, layer)))
		return_0;

	/* We've already processed this node if it already has a context ptr */
	if ((dnode = dm_tree_find_node_by_uuid(dtree, dlid)) &&
	    dm_tree_node_get_context(dnode))
		return 1;

	lvlayer->lv = lv;
	lvlayer->visible_component = (laopts->component_lv == lv) ? 1 : 0;

	/*
	 * Add LV to dtree.
	 * If we're working with precommitted metadata, clear any
	 * existing inactive table left behind.
	 * Major/minor settings only apply to the visible layer.
	 */
	/* FIXME Move the clear from here until later, so we can leave
	 * identical inactive tables untouched. (For pvmove.)
	 */
	if (!(dnode = dm_tree_add_new_dev_with_udev_flags(dtree, name, dlid,
					     layer ? UINT32_C(0) : (uint32_t) lv->major,
					     layer ? UINT32_C(0) : (uint32_t) lv->minor,
					     read_only_lv(lv, laopts, layer),
					     ((lv->vg->status & PRECOMMITTED) | laopts->revert) ? 1 : 0,
					     lvlayer,
					     _get_udev_flags(dm, lv, layer, laopts->noscan, laopts->temporary,
							     lvlayer->visible_component))))
		return_0;

	/* Store existing name so we can do rename later */
	lvlayer->old_name = dm_tree_node_get_name(dnode);

	/* Create table */
	dm->pvmove_mirror_count = 0u;

	if (lv_is_pending_delete(lv))
		/* Handle LVs with pending delete */
		/* Fow now used only by cache segtype, TODO snapshots */
		dm->track_pending_delete = 1;

	/* This is unused cache-pool - make metadata accessible */
	if (lv_is_cache_pool(lv))
		lv = first_seg(lv)->metadata_lv;

	/* If this is a snapshot origin, add real LV */
	/* If this is a snapshot origin + merging snapshot, add cow + real LV */
	/* Snapshot origin could be also external origin */
	if (lv_is_origin(lv) && !layer) {
		if (!_add_new_lv_to_dtree(dm, dtree, lv, laopts, "real"))
			return_0;
		if (!laopts->no_merging && lv_is_merging_origin(lv)) {
			if (!_add_new_lv_to_dtree(dm, dtree,
						  find_snapshot(lv)->cow, laopts, "cow"))
				return_0;
			/*
			 * Must also add "real" LV for use when
			 * snapshot-merge target is added
			 */
			if (!_add_snapshot_merge_target_to_dtree(dm, dnode, lv))
				return_0;
		} else if (!_add_origin_target_to_dtree(dm, dnode, lv))
			return_0;

		/* Add any snapshots of this LV */
		dm_list_iterate(snh, &lv->snapshot_segs)
			if (!_add_new_lv_to_dtree(dm, dtree,
						  dm_list_struct_base(snh, struct lv_segment,
								      origin_list)->cow,
						  laopts, NULL))
				return_0;
	} else if (lv_is_cow(lv) && !layer) {
		if (!_add_new_lv_to_dtree(dm, dtree, lv, laopts, "cow"))
			return_0;
		if (!_add_snapshot_target_to_dtree(dm, dnode, lv, laopts))
			return_0;
	} else if (!layer && ((lv_is_thin_pool(lv) && !lv_is_new_thin_pool(lv)) ||
                              lv_is_vdo_pool(lv) ||
			      lv_is_external_origin(lv))) {
		/* External origin or 'used' Thin pool or VDO pool is using layer */
		if (!_add_new_lv_to_dtree(dm, dtree, lv, laopts, lv_layer(lv)))
			return_0;
		if (!_add_layer_target_to_dtree(dm, dnode, lv))
			return_0;
	} else {
		/* Add 'real' segments for LVs */
		dm_list_iterate_items(seg, &lv->segments) {
			if (!_add_segment_to_dtree(dm, dtree, dnode, seg, laopts, layer))
				return_0;
			if (max_stripe_size < seg->stripe_size * seg->area_count)
				max_stripe_size = seg->stripe_size * seg->area_count;
		}

		if (!layer && lv_is_vdo_pool(lv) &&
		    !_add_layer_target_to_dtree(dm, dnode, lv))
			return_0;
	}

	/* Setup thin pool callback */
	if (lv_is_thin_pool(lv) && layer &&
	    !_pool_register_callback(dm, dnode, lv))
		return_0;

	if (lv_is_cache(lv) && !lv_is_cache_vol(first_seg(lv)->pool_lv) &&
	    /* Register callback only for layer activation or non-layered cache LV */
	    (layer || !lv_layer(lv)) &&
	    /* Register callback when metadata LV is NOT already active */
	    !_cached_dm_info(dm->mem, dtree, first_seg(first_seg(lv)->pool_lv)->metadata_lv, NULL) &&
	    !_pool_register_callback(dm, dnode, lv))
		return_0;

	if (lv_is_cache(lv) && lv_is_cache_vol(first_seg(lv)->pool_lv) &&
	    /* Register callback only for layer activation or non-layered cache LV */
	    (layer || !lv_layer(lv)) &&
	    /* Register callback when cachevol LV is NOT already active */
	    !_cached_dm_info(dm->mem, dtree, first_seg(lv)->pool_lv, NULL) &&
	    !_pool_register_callback(dm, dnode, lv))
		return_0;

	/*
	 * Update tables for ANY PVMOVE holders for active LV where the name starts with 'pvmove',
	 * but it's not anymore PVMOVE LV and also it's not a PVMOVE _mimage LV.
	 * When resume happens, tables MUST be already preloaded with correct entries!
	 * (since we can't preload different table while devices are suspended)
	 */
	if (!lv_is_pvmove(lv) && !strncmp(lv->name, "pvmove", 6) && !strchr(lv->name, '_') &&
	    (dinfo = _cached_dm_info(dm->mem, dtree, lv, NULL)))
		if (!_add_holders_to_dtree(dm, dtree, lv, laopts, dinfo))
			return_0;

	if (read_ahead == DM_READ_AHEAD_AUTO) {
		/* we need RA at least twice a whole stripe - see the comment in md/raid0.c */
		read_ahead = max_stripe_size * 2;
		/* FIXME: layered device read-ahead */
		if (!read_ahead)
			lv_calculate_readahead(lv, &read_ahead);
		read_ahead_flags = DM_READ_AHEAD_MINIMUM_FLAG;
	}

	dm_tree_node_set_read_ahead(dnode, read_ahead, read_ahead_flags);

	/* Add any LVs referencing a PVMOVE LV unless told not to */
	if (dm->track_pvmove_deps && lv_is_pvmove(lv))
		dm_list_iterate_items(sl, &lv->segs_using_this_lv)
			if (!_add_new_lv_to_dtree(dm, dtree, sl->seg->lv, laopts, NULL))
				return_0;

	dm->track_pending_delete = save_pending_delete; /* restore */

	return 1;
}

/* FIXME: symlinks should be created/destroyed at the same time
 * as the kernel devices but we can't do that from within libdevmapper
 * at present so we must walk the tree twice instead. */

/*
 * Create LV symlinks for children of supplied root node.
 */
static int _create_lv_symlinks(struct dev_manager *dm, struct dm_tree_node *root)
{
	void *handle = NULL;
	struct dm_tree_node *child;
	struct lv_layer *lvlayer;
	char *old_vgname, *old_lvname, *old_layer;
	char *new_vgname, *new_lvname, *new_layer;
	const char *name;
	int r = 1;

	/* Nothing to do if udev fallback is disabled. */
	if (!_check_udev_fallback(dm->cmd)) {
		fs_set_create();
		return 1;
	}

	while ((child = dm_tree_next_child(&handle, root, 0))) {
		if (!(lvlayer = dm_tree_node_get_context(child)))
			continue;

		/* Detect rename */
		name = dm_tree_node_get_name(child);

		if (name && lvlayer->old_name && *lvlayer->old_name && strcmp(name, lvlayer->old_name)) {
			if (!dm_split_lvm_name(dm->mem, lvlayer->old_name, &old_vgname, &old_lvname, &old_layer)) {
				log_error("_create_lv_symlinks: Couldn't split up old device name %s.", lvlayer->old_name);
				return 0;
			}
			if (!dm_split_lvm_name(dm->mem, name, &new_vgname, &new_lvname, &new_layer)) {
				log_error("_create_lv_symlinks: Couldn't split up new device name %s.", name);
				return 0;
			}
			if (!fs_rename_lv(lvlayer->lv, name, old_vgname, old_lvname))
				r = 0;
			continue;
		}
		if (_lv_has_mknode(lvlayer->lv) || lvlayer->visible_component) {
			if (!_dev_manager_lv_mknodes(lvlayer->lv))
				r = 0;
			continue;
		}
		if (!_dev_manager_lv_rmnodes(lvlayer->lv))
			r = 0;
	}

	return r;
}

/*
 * Remove LV symlinks for children of supplied root node.
 */
static int _remove_lv_symlinks(struct dev_manager *dm, struct dm_tree_node *root)
{
	void *handle = NULL;
	struct dm_tree_node *child;
	char *vgname, *lvname, *layer;
	int r = 1;

	/* Nothing to do if udev fallback is disabled. */
	if (!_check_udev_fallback(dm->cmd))
		return 1;

	while ((child = dm_tree_next_child(&handle, root, 0))) {
		if (!dm_split_lvm_name(dm->mem, dm_tree_node_get_name(child), &vgname, &lvname, &layer)) {
			r = 0;
			continue;
		}

		if (!*vgname)
			continue;

		/* only top level layer has symlinks */
		if (*layer)
			continue;

		fs_del_lv_byname(dm->cmd->dev_dir, vgname, lvname,
				 dm->cmd->current_settings.udev_rules);
	}

	return r;
}

static int _clean_tree(struct dev_manager *dm, struct dm_tree_node *root, const char *non_toplevel_tree_dlid)
{
	void *handle = NULL;
	struct dm_tree_node *child;
	char *vgname, *lvname, *layer;
	const char *name, *uuid;
	struct dm_str_list *dl;

	while ((child = dm_tree_next_child(&handle, root, 0))) {
		if (!(name = dm_tree_node_get_name(child)))
			continue;

		if (!(uuid = dm_tree_node_get_uuid(child)))
			continue;

		if (!dm_split_lvm_name(dm->mem, name, &vgname, &lvname, &layer)) {
			log_error("_clean_tree: Couldn't split up device name %s.", name);
			return 0;
		}

		/* Not meant to be top level? */
		if (!*layer)
			continue;

		/* If operation was performed on a partial tree, don't remove it */
		if (non_toplevel_tree_dlid && !strcmp(non_toplevel_tree_dlid, uuid))
			continue;

		if (!(uuid = dm_pool_strdup(dm->cmd->pending_delete_mem, uuid))) {
			log_error("_clean_tree: Failed to duplicate uuid.");
			return 0;
		}

		if (!str_list_add(dm->cmd->pending_delete_mem, &dm->cmd->pending_delete, uuid))
			return_0;
	}

	/* Deactivate any tracked pending delete nodes */
	if (!dm_list_empty(&dm->cmd->pending_delete) && !dm_get_suspended_counter()) {
		fs_unlock();
		dm_tree_set_cookie(root, fs_get_cookie());
		dm_list_iterate_items(dl, &dm->cmd->pending_delete) {
			log_debug_activation("Deleting tracked UUID %s.", dl->str);
			if (!dm_tree_deactivate_children(root, dl->str, strlen(dl->str)))
				return_0;
		}
		dm_list_init(&dm->cmd->pending_delete);
		dm_pool_empty(dm->cmd->pending_delete_mem);
	}

	return 1;
}

static int _tree_action(struct dev_manager *dm, const struct logical_volume *lv,
			struct lv_activate_opts *laopts, action_t action)
{
	static const char _action_names[][24] = {
		"PRELOAD", "ACTIVATE", "DEACTIVATE", "SUSPEND", "SUSPEND_WITH_LOCKFS", "CLEAN"
	};
	const size_t DLID_SIZE = ID_LEN + sizeof(UUID_PREFIX) - 1;
	struct dm_tree *dtree;
	struct dm_tree_node *root;
	char *dlid;
	int r = 0;
	unsigned tmp_state;

	if (action < DM_ARRAY_SIZE(_action_names))
		log_debug_activation("Creating %s%s tree for %s.",
				     _action_names[action],
				     (laopts->origin_only) ? " origin-only" : "",
				     display_lvname(lv));

	/* Some LV cannot be used for top level tree */
	/* TODO: add more.... */
	if (lv_is_cache_pool(lv) && !dm_list_empty(&lv->segs_using_this_lv)) {
		log_error(INTERNAL_ERROR "Cannot create tree for %s.",
			  display_lvname(lv));
		return 0;
	}
	/* Some targets may build bigger tree for activation */
	dm->activation = ((action == PRELOAD) || (action == ACTIVATE));
	dm->suspend = (action == SUSPEND_WITH_LOCKFS) || (action == SUSPEND);
	dm->track_external_lv_deps = 1;

	/* ATM do not use caching for anything else then striped target.
	 * And also skip for CLEAN action */
	tmp_state = dm->cmd->disable_dm_devs;
	if (!seg_is_striped_target(first_seg(lv)) || (action == CLEAN))
		dm->cmd->disable_dm_devs = 1;

	dtree = _create_partial_dtree(dm, lv, laopts->origin_only);
	dm->cmd->disable_dm_devs = tmp_state;

	if (!dtree)
		return_0;

	if (!(root = dm_tree_find_node(dtree, 0, 0))) {
		log_error("Lost dependency tree root node.");
		goto out_no_root;
	}

	/* Restore fs cookie */
	dm_tree_set_cookie(root, fs_get_cookie());

	if (!(dlid = build_dm_uuid(dm->mem, lv, laopts->origin_only ? lv_layer(lv) : NULL)))
		goto_out;

	/* Only process nodes with uuid of "LVM-" plus VG id. */
	switch(action) {
	case CLEAN:
		if (retry_deactivation())
			dm_tree_retry_remove(root);
		/* Deactivate any unused non-toplevel nodes */
		if (!_clean_tree(dm, root, laopts->origin_only ? dlid : NULL))
			goto_out;
		break;
	case DEACTIVATE:
		if (retry_deactivation())
			dm_tree_retry_remove(root);
		/* Deactivate LV and all devices it references that nothing else has open. */
		if (!dm_tree_deactivate_children(root, dlid, DLID_SIZE))
			goto_out;
		if (!_remove_lv_symlinks(dm, root))
			log_warn("Failed to remove all device symlinks associated with %s.",
				 display_lvname(lv));
		break;
	case SUSPEND:
		dm_tree_skip_lockfs(root);
		if (!dm->flush_required)
			dm_tree_use_no_flush_suspend(root);
		/* Fall through */
	case SUSPEND_WITH_LOCKFS:
		if (!dm_tree_suspend_children(root, dlid, DLID_SIZE))
			goto_out;
		break;
	case PRELOAD:
	case ACTIVATE:
		/* Add all required new devices to tree */
		if (!_add_new_lv_to_dtree(dm, dtree, lv, laopts,
					  (lv_is_origin(lv) && laopts->origin_only) ? "real" :
					  (laopts->origin_only &&
					   (lv_is_thin_pool(lv) ||
					    lv_is_vdo_pool(lv))) ?
					  lv_layer(lv) : NULL))
			goto_out;

		/* Preload any devices required before any suspensions */
		if (!dm_tree_preload_children(root, dlid, DLID_SIZE))
			goto_out;

		if ((dm_tree_node_size_changed(root) < 0))
			dm->flush_required = 1;
		/* Currently keep the code require flush for any
		 * non 'thin pool/volume' and  size increase */
		else if (!lv_is_thin_volume(lv) &&
			 !lv_is_thin_pool(lv) &&
			 !lv_is_vdo(lv) &&
			 !lv_is_vdo_pool(lv) &&
			 dm_tree_node_size_changed(root))
			dm->flush_required = 1;

		if (action == ACTIVATE) {
			if (!dm_tree_activate_children(root, dlid, DLID_SIZE))
				goto_out;
			if (!_create_lv_symlinks(dm, root))
				log_warn("Failed to create symlinks for %s.",
					 display_lvname(lv));
		}

		break;
	default:
		log_error(INTERNAL_ERROR "_tree_action: Action %u not supported.", action);
		goto out;
	}
	r = 1;

out:
	/* Save fs cookie for udev settle, do not wait here */
	fs_set_cookie(dm_tree_get_cookie(root));
out_no_root:
	dm_tree_free(dtree);

	return r;
}

/* origin_only may only be set if we are resuming (not activating) an origin LV */
int dev_manager_activate(struct dev_manager *dm, const struct logical_volume *lv,
			 struct lv_activate_opts *laopts)
{
	if (!_tree_action(dm, lv, laopts, ACTIVATE))
		return_0;

	if (!_tree_action(dm, lv, laopts, CLEAN))
		return_0;

	return 1;
}

/* origin_only may only be set if we are resuming (not activating) an origin LV */
int dev_manager_preload(struct dev_manager *dm, const struct logical_volume *lv,
			struct lv_activate_opts *laopts, int *flush_required)
{
	dm->flush_required = *flush_required;

	if (!_tree_action(dm, lv, laopts, PRELOAD))
		return_0;

	*flush_required = dm->flush_required;

	return 1;
}

int dev_manager_deactivate(struct dev_manager *dm, const struct logical_volume *lv)
{
	struct lv_activate_opts laopts = { 0 };

	if (!_tree_action(dm, lv, &laopts, DEACTIVATE))
		return_0;

	return 1;
}

int dev_manager_suspend(struct dev_manager *dm, const struct logical_volume *lv,
			struct lv_activate_opts *laopts, int lockfs, int flush_required)
{
	dm->flush_required = flush_required;

	if (!_tree_action(dm, lv, laopts, lockfs ? SUSPEND_WITH_LOCKFS : SUSPEND))
		return_0;

	return 1;
}

/*
 * Does device use VG somewhere in its construction?
 * Returns 1 if uncertain.
 */
int dev_manager_device_uses_vg(struct device *dev,
			       struct volume_group *vg)
{
	struct dm_tree *dtree;
	struct dm_tree_node *root;
	char dlid[sizeof(UUID_PREFIX) + sizeof(struct id) - 1] __attribute__((aligned(8)));
	int r = 1;

	if (!(dtree = dm_tree_create())) {
		log_error("Failed to create partial dtree.");
		return r;
	}

	dm_tree_set_optional_uuid_suffixes(dtree, &uuid_suffix_list[0]);

	if (!dm_tree_add_dev(dtree, (uint32_t) MAJOR(dev->dev), (uint32_t) MINOR(dev->dev))) {
		log_error("Failed to add device %s (%" PRIu32 ":%" PRIu32") to dtree.",
			  dev_name(dev), (uint32_t) MAJOR(dev->dev), (uint32_t) MINOR(dev->dev));
		goto out;
	}

	memcpy(dlid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1);
	memcpy(dlid + sizeof(UUID_PREFIX) - 1, &vg->id.uuid[0], sizeof(vg->id));

	if (!(root = dm_tree_find_node(dtree, 0, 0))) {
		log_error("Lost dependency tree root node.");
		goto out;
	}

	if (dm_tree_children_use_uuid(root, dlid, sizeof(UUID_PREFIX) + sizeof(vg->id) - 1))
		goto_out;

	r = 0;

out:
	dm_tree_free(dtree);

	return r;
}

/*
 * crypt offset is usually the LUKS header size but can be larger.
 * The LUKS header is usually 2MB for LUKS1 and 16MB for LUKS2.
 * The offset needs to be subtracted from the LV size to get the
 * size used to resize the crypt device.
 */
int get_crypt_table_offset(dev_t crypt_devt, uint32_t *offset_bytes)
{
	struct dm_task *dmt = dm_task_create(DM_DEVICE_TABLE);
	uint64_t start, length;
	char *target_type = NULL;
	void *next = NULL;
	char *params = NULL;
	char offset_str[32] = { 0 };
	int copy_offset = 0;
	int spaces = 0;
	int i, i_off = 0;

	if (!dmt)
		return_0;

	if (!dm_task_set_major_minor(dmt, (int)MAJOR(crypt_devt), (int)MINOR(crypt_devt), 0)) {
		dm_task_destroy(dmt);
		return_0;
	}

	/* Non-blocking status read */
	if (!dm_task_no_flush(dmt))
		log_warn("WARNING: Can't set no_flush for dm status.");

	if (!dm_task_run(dmt)) {
		dm_task_destroy(dmt);
		return_0;
	}

	next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);

	if (!target_type || !params || strcmp(target_type, "crypt")) {
		dm_task_destroy(dmt);
		return_0;
	}

	/*
	 * get offset from params string:
	 * <cipher> <key> <iv_offset> <device> <offset> [<#opt_params> <opt_params>]
	 * <offset> is reported in 512 byte sectors.
	 */
	for (i = 0; i < strlen(params); i++) {
		if (params[i] == ' ') {
			spaces++;
			if (spaces == 4)
				copy_offset = 1;
			if (spaces == 5)
				break;
			continue;
		}
		if (!copy_offset)
			continue;

		offset_str[i_off++] = params[i];

		if (i_off == sizeof(offset_str)) {
			offset_str[0] = '\0';
			break;
		}
	}
	dm_task_destroy(dmt);

	if (!offset_str[0])
		return_0;

	*offset_bytes = ((uint32_t)strtoul(offset_str, NULL, 0) * 512);
	return 1;
}