summaryrefslogtreecommitdiff
path: root/lib/activate/activate.c
blob: a82a5cbc4d20b3e6e0ae3ca00facd470f60d4787 (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
/*
 * Copyright (C) 2001-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 "lib/metadata/metadata.h"
#include "lib/activate/activate.h"
#include "lib/mm/memlock.h"
#include "lib/display/display.h"
#include "fs.h"
#include "lib/misc/lvm-exec.h"
#include "lib/misc/lvm-file.h"
#include "lib/misc/lvm-string.h"
#include "lib/commands/toolcontext.h"
#include "dev_manager.h"
#include "lib/datastruct/str_list.h"
#include "lib/config/config.h"
#include "lib/metadata/segtype.h"
#include "lib/misc/sharedlib.h"
#include "lib/metadata/metadata.h"

#include <limits.h>
#include <fcntl.h>
#include <unistd.h>

#define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)

int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
			 struct dm_list *modules)
{
	unsigned int s;
	struct lv_segment *seg2, *snap_seg;
	struct dm_list *snh;

	if (seg->segtype->ops->modules_needed &&
	    !seg->segtype->ops->modules_needed(mem, seg, modules)) {
		log_error("module string allocation failed");
		return 0;
	}

	if (lv_is_origin(seg->lv))
		dm_list_iterate(snh, &seg->lv->snapshot_segs)
			if (!list_lv_modules(mem,
					     dm_list_struct_base(snh,
							      struct lv_segment,
							      origin_list)->cow,
					     modules))
				return_0;

	if (lv_is_cow(seg->lv)) {
		snap_seg = find_snapshot(seg->lv);
		if (snap_seg->segtype->ops->modules_needed &&
		    !snap_seg->segtype->ops->modules_needed(mem, snap_seg,
							    modules)) {
			log_error("snap_seg module string allocation failed");
			return 0;
		}
	}

	for (s = 0; s < seg->area_count; s++) {
		switch (seg_type(seg, s)) {
		case AREA_LV:
			seg2 = find_seg_by_le(seg_lv(seg, s), seg_le(seg, s));
			if (seg2 && !list_segment_modules(mem, seg2, modules))
				return_0;
			break;
		case AREA_PV:
		case AREA_UNASSIGNED:
			;
		}
	}

	return 1;
}

int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
		    struct dm_list *modules)
{
	struct lv_segment *seg;

	dm_list_iterate_items(seg, &lv->segments)
		if (!list_segment_modules(mem, seg, modules))
			return_0;

	return 1;
}

static int _lv_passes_volumes_filter(struct cmd_context *cmd, const struct logical_volume *lv,
				     const struct dm_config_node *cn, const int cfg_id)
{
	const struct dm_config_value *cv;
	const char *str;
	static char config_path[PATH_MAX];
	size_t len = strlen(lv->vg->name);

	config_def_get_path(config_path, sizeof(config_path), cfg_id);
	log_verbose("%s configuration setting defined: "
		    "Checking the list to match %s.",
		    config_path, display_lvname(lv));

	for (cv = cn->v; cv; cv = cv->next) {
		if (cv->type == DM_CFG_EMPTY_ARRAY)
			goto out;
		if (cv->type != DM_CFG_STRING) {
			log_print_unless_silent("Ignoring invalid string in config file %s.",
						config_path);
			continue;
		}
		str = cv->v.str;
		if (!*str) {
			log_print_unless_silent("Ignoring empty string in config file %s.",
						config_path);
			continue;
		}

		/* Tag? */
		if (*str == '@') {
			str++;
			if (!*str) {
				log_print_unless_silent("Ignoring empty tag in config file %s",
							config_path);
				continue;
			}
			/* If any host tag matches any LV or VG tag, activate */
			if (!strcmp(str, "*")) {
				if (str_list_match_list(&cmd->tags, &lv->tags, NULL)
				    || str_list_match_list(&cmd->tags,
							   &lv->vg->tags, NULL))
					    return 1;

				continue;
			}
			/* If supplied tag matches LV or VG tag, activate */
			if (str_list_match_item(&lv->tags, str) ||
			    str_list_match_item(&lv->vg->tags, str))
				return 1;

			continue;
		}

		/* If supplied name is vgname[/lvname] */
		if ((strncmp(str, lv->vg->name, len) == 0) &&
		    (!str[len] ||
		     ((str[len] == '/') &&
		      !strcmp(str + len + 1, lv->name))))
			return 1;
	}

out:
	log_verbose("No item supplied in %s configuration setting matches %s.",
		    config_path, display_lvname(lv));

	return 0;
}

int lv_passes_auto_activation_filter(struct cmd_context *cmd, struct logical_volume *lv)
{
	const struct dm_config_node *cn;

	if (!(cn = find_config_tree_array(cmd, activation_auto_activation_volume_list_CFG, NULL))) {
		log_verbose("activation/auto_activation_volume_list configuration setting "
			    "not defined: All logical volumes will be auto-activated.");
		return 1;
	}

	return _lv_passes_volumes_filter(cmd, lv, cn, activation_auto_activation_volume_list_CFG);
}

#ifndef DEVMAPPER_SUPPORT
void set_activation(int act, int silent)
{
	static int warned = 0;

	if (warned || !act)
		return;

	log_error("Compiled without libdevmapper support. "
		  "Can't enable activation.");

	warned = 1;
}
int activation(void)
{
	return 0;
}
int library_version(char *version, size_t size)
{
	return 0;
}
int driver_version(char *version, size_t size)
{
	return 0;
}
int target_version(const char *target_name, uint32_t *maj,
		   uint32_t *min, uint32_t *patchlevel)
{
	return 0;
}
int target_present(struct cmd_context *cmd, const char *target_name,
		   int use_modprobe)
{
	return 0;
}
int lvm_dm_prefix_check(int major, int minor, const char *prefix)
{
	return 0;
}
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int use_layer,
	    struct lvinfo *info, int with_open_count, int with_read_ahead)
{
	return 0;
}
int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, int use_layer,
		    struct lvinfo *info, int with_open_count, int with_read_ahead)
{
	return 0;
}
int lv_info_with_seg_status(struct cmd_context *cmd, const struct logical_volume *lv,
			    const struct lv_segment *lv_seg, int use_layer,
			    struct lv_with_info_and_seg_status *status,
			    int with_open_count, int with_read_ahead)
{
	return 0;
}
int lv_status(struct cmd_context *cmd, const struct lv_segment *lv_seg,
	      int use_layer, struct lv_seg_status *lv_seg_status)
{
	return 0;
}
int lv_cache_status(const struct logical_volume *cache_lv,
		    struct lv_status_cache **status)
{
	return 0;
}
int lv_check_not_in_use(const struct logical_volume *lv, int error_if_used)
{
        return 0;
}
int lv_snapshot_percent(const struct logical_volume *lv, dm_percent_t *percent)
{
	return 0;
}
int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
		      int wait, dm_percent_t *percent, uint32_t *event_nr)
{
	return 0;
}
int lv_raid_percent(const struct logical_volume *lv, dm_percent_t *percent)
{
	return 0;
}
int lv_raid_data_offset(const struct logical_volume *lv, uint64_t *data_offset)
{
	return 0;
}
int lv_raid_dev_health(const struct logical_volume *lv, char **dev_health)
{
	return 0;
}
int lv_raid_dev_count(const struct logical_volume *lv, uint32_t *dev_cnt)
{
	return 0;
}
int lv_raid_mismatch_count(const struct logical_volume *lv, uint64_t *cnt)
{
	return 0;
}
int lv_raid_sync_action(const struct logical_volume *lv, char **sync_action)
{
	return 0;
}
int lv_raid_message(const struct logical_volume *lv, const char *msg)
{
	return 0;
}
int lv_thin_pool_percent(const struct logical_volume *lv, int metadata,
			 dm_percent_t *percent)
{
	return 0;
}
int lv_thin_percent(const struct logical_volume *lv, int mapped,
		    dm_percent_t *percent)
{
	return 0;
}
int lv_thin_pool_transaction_id(const struct logical_volume *lv,
				uint64_t *transaction_id)
{
	return 0;
}
int lv_thin_device_id(const struct logical_volume *lv, uint32_t *device_id)
{
	return 0;
}
int lvs_in_vg_activated(const struct volume_group *vg)
{
	return 0;
}
int lvs_in_vg_opened(const struct volume_group *vg)
{
	return 0;
}
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only, unsigned exclusive,
			 const struct logical_volume *lv, const struct logical_volume *lv_pre)
{
	return 1;
}
int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only, const struct logical_volume *lv)
{
	return 1;
}
int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only,
			unsigned exclusive, unsigned revert, const struct logical_volume *lv)
{
	return 1;
}
int lv_deactivate(struct cmd_context *cmd, const char *lvid_s, const struct logical_volume *lv)
{
	return 1;
}
int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
			 int *activate_lv, const struct logical_volume *lv)
{
	return 1;
}
int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive, int noscan,
		int temporary, const struct logical_volume *lv)
{
	return 1;
}
int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive,
			    int noscan, int temporary, const struct logical_volume *lv)
{
	return 1;
}
int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
{
	return 1;
}
int lv_deactivate_any_missing_subdevs(const struct logical_volume *lv)
{
	return 1;
}
int pv_uses_vg(struct physical_volume *pv,
	       struct volume_group *vg)
{
	return 0;
}
void activation_release(void)
{
}
void activation_exit(void)
{
}

int raid4_is_supported(struct cmd_context *cmd, const struct segment_type *segtype)
{
	return 1;
}

int lv_is_active(const struct logical_volume *lv)
{
	return 0;
}
int lv_check_transient(struct logical_volume *lv)
{
	return 1;
}
int monitor_dev_for_events(struct cmd_context *cmd, const struct logical_volume *lv,
			   const struct lv_activate_opts *laopts, int monitor)
{
	return 1;
}
/* fs.c */
void fs_unlock(void)
{
}
/* dev_manager.c */
#include "lib/activate/targets.h"
int add_areas_line(struct dev_manager *dm, struct lv_segment *seg,
		   struct dm_tree_node *node, uint32_t start_area,
		   uint32_t areas)
{
        return 0;
}
int device_is_usable(struct device *dev, struct dev_usable_check_params check)
{
        return 0;
}
int lv_has_target_type(struct dm_pool *mem, const struct logical_volume *lv,
		       const char *layer, const char *target_type)
{
        return 0;
}
#else				/* DEVMAPPER_SUPPORT */

static int _activation = 1;

void set_activation(int act, int silent)
{
	if (act == _activation)
		return;

	_activation = act;
	if (_activation)
		log_verbose("Activation enabled. Device-mapper kernel "
			    "driver will be used.");
	else if (!silent)
		log_warn("WARNING: Activation disabled. No device-mapper "
			  "interaction will be attempted.");
	else
		log_verbose("Activation disabled. No device-mapper "
			    "interaction will be attempted.");
}

int activation(void)
{
	return _activation;
}

static int _passes_activation_filter(struct cmd_context *cmd,
				     const struct logical_volume *lv)
{
	const struct dm_config_node *cn;

	if (!(cn = find_config_tree_array(cmd, activation_volume_list_CFG, NULL))) {
		log_verbose("activation/volume_list configuration setting "
			    "not defined: Checking only host tags for %s.",
			    display_lvname(lv));

		/* If no host tags defined, activate */
		if (dm_list_empty(&cmd->tags))
			return 1;

		/* If any host tag matches any LV or VG tag, activate */
		if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
		    str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
			return 1;

		log_verbose("No host tag matches %s", display_lvname(lv));

		/* Don't activate */
		return 0;
	}

	return _lv_passes_volumes_filter(cmd, lv, cn, activation_volume_list_CFG);
}

static int _passes_readonly_filter(struct cmd_context *cmd,
				   const struct logical_volume *lv)
{
	const struct dm_config_node *cn;

	if (!(cn = find_config_tree_array(cmd, activation_read_only_volume_list_CFG, NULL)))
		return 0;

	return _lv_passes_volumes_filter(cmd, lv, cn, activation_read_only_volume_list_CFG);
}

int library_version(char *version, size_t size)
{
	if (!activation())
		return 0;

	return dm_get_library_version(version, size);
}

int driver_version(char *version, size_t size)
{
	if (!activation())
		return 0;

	log_very_verbose("Getting driver version");

	return dm_driver_version(version, size);
}

int target_version(const char *target_name, uint32_t *maj,
		   uint32_t *min, uint32_t *patchlevel)
{
	int r = 0;
	struct dm_task *dmt;
	struct dm_versions *target, *last_target;

	log_very_verbose("Getting target version for %s", target_name);
	if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
		return_0;

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

	if (!dm_task_run(dmt)) {
		log_debug_activation("Failed to get %s target version", target_name);
		/* Assume this was because LIST_VERSIONS isn't supported */
		*maj = 0;
		*min = 0;
		*patchlevel = 0;
		r = 1;
		goto out;
	}

	target = dm_task_get_versions(dmt);

	do {
		last_target = target;

		if (!strcmp(target_name, target->name)) {
			r = 1;
			*maj = target->version[0];
			*min = target->version[1];
			*patchlevel = target->version[2];
			goto out;
		}

		target = (struct dm_versions *)((char *) target + target->next);
	} while (last_target != target);

      out:
	if (r)
		log_very_verbose("Found %s target "
				 "v%" PRIu32 ".%" PRIu32 ".%" PRIu32 ".",
				 target_name, *maj, *min, *patchlevel);

	dm_task_destroy(dmt);

	return r;
}

int lvm_dm_prefix_check(int major, int minor, const char *prefix)
{
	return dev_manager_check_prefix_dm_major_minor(major, minor, prefix);
}

int module_present(struct cmd_context *cmd, const char *target_name)
{
	int ret = 0;
#ifdef MODPROBE_CMD
	char module[128];
	const char *argv[] = { MODPROBE_CMD, module, NULL };
#endif
	struct stat st;
	char path[PATH_MAX];
	int i = dm_snprintf(path, sizeof(path), "%smodule/dm_%s",
			    dm_sysfs_dir(), target_name);

	if (i > 0) {
		while (path[--i] != '/')  /* stop on dm_ */
			if (path[i] == '-')
				path[i] = '_'; /* replace '-' with '_' */

		if ((lstat(path, &st) == 0) && S_ISDIR(st.st_mode)) {
			log_debug_activation("Module directory %s exists.", path);
			return 1;
		}
	}

#ifdef MODPROBE_CMD
	if (strcmp(target_name, MODULE_NAME_VDO) == 0) {
		argv[1] = target_name;		/* ATM kvdo is without dm- prefix */
		if ((ret = exec_cmd(cmd, argv, NULL, 0)))
			return ret;
	}

	if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
		log_error("module_present module name too long: %s",
			  target_name);
		return 0;
	}

	ret = exec_cmd(cmd, argv, NULL, 0);
#endif
	return ret;
}

int target_present_version(struct cmd_context *cmd, const char *target_name,
			   int use_modprobe,
			   uint32_t *maj, uint32_t *min, uint32_t *patchlevel)
{
	if (!activation()) {
		log_error(INTERNAL_ERROR "Target present version called when activation is disabled.");
		return 0;
	}
#ifdef MODPROBE_CMD
	if (use_modprobe) {
		if (target_version(target_name, maj, min, patchlevel))
			return 1;

		if (!module_present(cmd, target_name))
			return_0;
	}
#endif
	return target_version(target_name, maj, min, patchlevel);
}

int target_present(struct cmd_context *cmd, const char *target_name,
		   int use_modprobe)
{
	uint32_t maj, min, patchlevel;

	return target_present_version(cmd, target_name, use_modprobe,
				      &maj, &min, &patchlevel);
}

/*
 * When '*info' is NULL, returns 1 only when LV is active.
 * When '*info' != NULL, returns 1 when info structure is populated.
 */
static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv,
		    int use_layer, struct lvinfo *info,
		    const struct lv_segment *seg,
		    struct lv_seg_status *seg_status,
		    int with_open_count, int with_read_ahead, int with_name_check)
{
	struct dm_info dminfo;

	/*
	 * If open_count info is requested and we have to be sure our own udev
	 * transactions are finished
	 * For non-clustered locking type we are only interested for non-delete operation
	 * in progress - as only those could lead to opened files
	 */
	if (with_open_count) {
		if (fs_has_non_delete_ops())
			fs_unlock(); /* For non clustered - wait if there are non-delete ops */
	}

	/* New thin-pool has no layer, but -tpool suffix needs to be queried */
	if (!use_layer && lv_is_new_thin_pool(lv)) {
		/* Check if there isn't existing old thin pool mapping in the table */
		if (!dev_manager_info(cmd, lv, NULL, 0, 0, 0, &dminfo, NULL, NULL))
			return_0;
		if (!dminfo.exists)
			use_layer = 1;
	}

	if (seg_status) {
		/* TODO: for now it's mess with seg_status */
		seg_status->seg = seg;
	}

	if (!dev_manager_info(cmd, lv,
			      (use_layer) ? lv_layer(lv) : NULL,
			      with_open_count, with_read_ahead, with_name_check,
			      &dminfo,
			      (info) ? &info->read_ahead : NULL,
			      seg_status))
		return_0;

	if (!info)
		return dminfo.exists;

	info->exists = dminfo.exists;
	info->suspended = dminfo.suspended;
	info->open_count = dminfo.open_count;
	info->major = dminfo.major;
	info->minor = dminfo.minor;
	info->read_only = dminfo.read_only;
	info->live_table = dminfo.live_table;
	info->inactive_table = dminfo.inactive_table;

	return 1;
}

/*
 * Returns 1 if info structure populated, else 0 on failure.
 * When lvinfo* is NULL, it returns 1 if the device is locally active, 0 otherwise.
 */
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int use_layer,
	    struct lvinfo *info, int with_open_count, int with_read_ahead)
{
	if (!activation())
		return 0;

	return _lv_info(cmd, lv, use_layer, info, NULL, NULL, with_open_count, with_read_ahead, 0);
}

int lv_info_with_name_check(struct cmd_context *cmd, const struct logical_volume *lv,
			    int use_layer, struct lvinfo *info)
{
	if (!activation())
		return 0;

	return _lv_info(cmd, lv, use_layer, info, NULL, NULL, 0, 0, 1);
}

/*
 * Returns 1 if lv_with_info_and_seg_status info structure populated,
 * else 0 on failure or if device not active locally.
 *
 * When seg_status parsing had troubles it will set type to SEG_STATUS_UNKNOWN.
 *
 * Using usually one ioctl to obtain info and status.
 * More complex segment do collect info from one device,
 * but status from another device.
 *
 * TODO: further improve with more statuses (i.e. snapshot's origin/merge)
 */
int lv_info_with_seg_status(struct cmd_context *cmd,
			    const struct lv_segment *lv_seg,
			    struct lv_with_info_and_seg_status *status,
			    int with_open_count, int with_read_ahead)
{
	const struct logical_volume *olv, *lv = status->lv = lv_seg->lv;

	if (!activation())
		return 0;

	if (lv_is_used_cache_pool(lv)) {
		/* INFO is not set as cache-pool cannot be active.
		 * STATUS is collected from cache LV */
		if (!(lv_seg = get_only_segment_using_this_lv(lv)))
			return_0;
		(void) _lv_info(cmd, lv_seg->lv, 1, NULL, lv_seg, &status->seg_status, 0, 0, 0);
		return 1;
	}

	if (lv_is_thin_pool(lv)) {
		/* Always collect status for '-tpool' */
		if (_lv_info(cmd, lv, 1, &status->info, lv_seg, &status->seg_status, 0, 0, 0) &&
		    (status->seg_status.type == SEG_STATUS_THIN_POOL)) {
			/* There is -tpool device, but query 'active' state of 'fake' thin-pool */
			if (!_lv_info(cmd, lv, 0, NULL, NULL, NULL, 0, 0, 0) &&
			    !status->seg_status.thin_pool->needs_check)
				status->info.exists = 0; /* So pool LV is not active */
		}
		return 1;
	}

	if (lv_is_external_origin(lv)) {
		if (!_lv_info(cmd, lv, 0, &status->info, NULL, NULL,
			      with_open_count, with_read_ahead, 0))
			return_0;

		(void) _lv_info(cmd, lv, 1, NULL, lv_seg, &status->seg_status, 0, 0, 0);
		return 1;
	}

	if (lv_is_origin(lv)) {
		/* Query segment status for 'layered' (-real) device most of the time,
		 * only for merging snapshot, query its progress.
		 * TODO: single LV may need couple status to be exposed at once....
		 *       but this needs more logical background
		 */
		/* Show INFO for actual origin and grab status for merging origin */
		if (!_lv_info(cmd, lv, 0, &status->info, lv_seg,
			      lv_is_merging_origin(lv) ? &status->seg_status : NULL,
			      with_open_count, with_read_ahead, 0))
			return_0;

		if (status->info.exists &&
		    (status->seg_status.type != SEG_STATUS_SNAPSHOT)) /* Not merging */
			/* Grab STATUS from layered -real */
			(void) _lv_info(cmd, lv, 1, NULL, lv_seg, &status->seg_status, 0, 0, 0);
		return 1;
	}

	if (lv_is_cow(lv)) {
		if (lv_is_merging_cow(lv)) {
			olv = origin_from_cow(lv);

			if (!_lv_info(cmd, olv, 0, &status->info, first_seg(olv), &status->seg_status,
				      with_open_count, with_read_ahead, 0))
				return_0;

			if (status->seg_status.type == SEG_STATUS_SNAPSHOT ||
			    (lv_is_thin_volume(olv) && (status->seg_status.type == SEG_STATUS_THIN))) {
				log_debug_activation("Snapshot merge is in progress, querying status of %s instead.",
						     display_lvname(lv));
				/*
				 * When merge is in progress, query merging origin LV instead.
				 * COW volume is already mapped as error target in this case.
				 */
				return 1;
			}

			/* Merge not yet started, still a snapshot... */
		}
		/* Hadle fictional lvm2 snapshot and query snapshotX volume */
		lv_seg = find_snapshot(lv);
	}

	if (lv_is_vdo(lv)) {
		if (!_lv_info(cmd, lv, 0, &status->info, NULL, NULL,
			      with_open_count, with_read_ahead, 0))
			return_0;
		if (status->info.exists) {
			/* Status for VDO pool */
			(void) _lv_info(cmd, seg_lv(lv_seg, 0), 1, NULL,
					first_seg(seg_lv(lv_seg, 0)),
					&status->seg_status, 0, 0, 0);
			/* Use VDO pool segtype result for VDO segtype */
			status->seg_status.seg = lv_seg;
		}
		return 1;
	}

	if (lv_is_vdo_pool(lv)) {
		/* Always collect status for '-vpool' */
		if (_lv_info(cmd, lv, 1, &status->info, lv_seg, &status->seg_status, 0, 0, 0) &&
		    (status->seg_status.type == SEG_STATUS_VDO_POOL)) {
			/* There is -tpool device, but query 'active' state of 'fake' vdo-pool */
			if (!_lv_info(cmd, lv, 0, NULL, NULL, NULL, 0, 0, 0))
				status->info.exists = 0; /* So VDO pool LV is not active */
		}

		return 1;
	}

	return _lv_info(cmd, lv, 0, &status->info, lv_seg, &status->seg_status,
			with_open_count, with_read_ahead, 0);
}

#define OPEN_COUNT_CHECK_RETRIES 25
#define OPEN_COUNT_CHECK_USLEEP_DELAY 200000

/* Only report error if error_if_used is set */
int lv_check_not_in_use(const struct logical_volume *lv, int error_if_used)
{
	struct lvinfo info;
	unsigned int open_count_check_retries;

	if (!lv_info(lv->vg->cmd, lv, 0, &info, 1, 0) || !info.exists || !info.open_count)
		return 1;

	/* If sysfs is not used, use open_count information only. */
	if (dm_sysfs_dir()) {
		if (dm_device_has_holders(info.major, info.minor)) {
			if (error_if_used)
				log_error("Logical volume %s is used by another device.",
					  display_lvname(lv));
			else
				log_debug_activation("Logical volume %s is used by another device.",
						     display_lvname(lv));
			return 0;
		}

		if (dm_device_has_mounted_fs(info.major, info.minor)) {
			if (error_if_used)
				log_error("Logical volume %s contains a filesystem in use.",
					  display_lvname(lv));
			else
				log_debug_activation("Logical volume %s contains a filesystem in use.",
						     display_lvname(lv));
			return 0;
		}
	}

	open_count_check_retries = retry_deactivation() ? OPEN_COUNT_CHECK_RETRIES : 1;
	while (info.open_count > 0 && open_count_check_retries--) {
		if (!open_count_check_retries) {
			if (error_if_used)
				log_error("Logical volume %s in use.", display_lvname(lv));
			else
				log_debug_activation("Logical volume %s in use.", display_lvname(lv));
			return 0;
		}

		usleep(OPEN_COUNT_CHECK_USLEEP_DELAY);
		log_debug_activation("Retrying open_count check for %s.",
				     display_lvname(lv));
		if (!lv_info(lv->vg->cmd, lv, 0, &info, 1, 0)) {
			stack; /* device dissappeared? */
			break;
		}
	}

	return 1;
}

/*
 * Returns 1 if percent set, else 0 on failure.
 */
int lv_check_transient(struct logical_volume *lv)
{
	int r;
	struct dev_manager *dm;

	if (!activation())
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_transient(dm, lv)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

/*
 * Returns 1 if percent set, else 0 on failure.
 */
int lv_snapshot_percent(const struct logical_volume *lv, dm_percent_t *percent)
{
	int r;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking snapshot percent for LV %s.",
			     display_lvname(lv));

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_snapshot_percent(dm, lv, percent)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

/* FIXME Merge with snapshot_percent */
int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
		      int wait, dm_percent_t *percent, uint32_t *event_nr)
{
	int r;
	struct dev_manager *dm;

	/* If mirrored LV is temporarily shrinked to 1 area (= linear),
	 * it should be considered in-sync. */
	if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
		*percent = DM_PERCENT_100;
		return 1;
	}

	if (!lv_info(cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking mirror percent for LV %s.",
			     display_lvname(lv));

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

int lv_raid_percent(const struct logical_volume *lv, dm_percent_t *percent)
{
	return lv_mirror_percent(lv->vg->cmd, lv, 0, percent, NULL);
}

int lv_raid_data_offset(const struct logical_volume *lv, uint64_t *data_offset)
{
	int r;
	struct dev_manager *dm;
	struct dm_status_raid *status;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking raid data offset and dev sectors for LV %s/%s",
			     lv->vg->name, lv->name);
	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_raid_status(dm, lv, &status))) {
		dev_manager_destroy(dm);
		return_0;
	}

	*data_offset = status->data_offset;

	dev_manager_destroy(dm);

	return r;
}

int lv_raid_dev_health(const struct logical_volume *lv, char **dev_health)
{
	int r;
	struct dev_manager *dm;
	struct dm_status_raid *status;

	*dev_health = NULL;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_raid_status(dm, lv, &status)) ||
	    !(*dev_health = dm_pool_strdup(lv->vg->cmd->mem,
					   status->dev_health))) {
		dev_manager_destroy(dm);
		return_0;
	}

	dev_manager_destroy(dm);

	return r;
}

int lv_raid_dev_count(const struct logical_volume *lv, uint32_t *dev_cnt)
{
	struct dev_manager *dm;
	struct dm_status_raid *status;

	*dev_cnt = 0;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking raid device count for LV %s/%s",
			     lv->vg->name, lv->name);
	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!dev_manager_raid_status(dm, lv, &status)) {
		dev_manager_destroy(dm);
		return_0;
	}
	*dev_cnt = status->dev_count;

	dev_manager_destroy(dm);

	return 1;
}

int lv_raid_mismatch_count(const struct logical_volume *lv, uint64_t *cnt)
{
	struct dev_manager *dm;
	struct dm_status_raid *status;

	*cnt = 0;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking raid mismatch count for LV %s.",
			     display_lvname(lv));

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!dev_manager_raid_status(dm, lv, &status)) {
		dev_manager_destroy(dm);
		return_0;
	}
	*cnt = status->mismatch_count;

	dev_manager_destroy(dm);

	return 1;
}

int lv_raid_sync_action(const struct logical_volume *lv, char **sync_action)
{
	struct dev_manager *dm;
	struct dm_status_raid *status;
	char *action;

	*sync_action = NULL;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking raid sync_action for LV %s.",
			     display_lvname(lv));

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	/* status->sync_action can be NULL if dm-raid version < 1.5.0 */
	if (!dev_manager_raid_status(dm, lv, &status) ||
	    !status->sync_action ||
	    !(action = dm_pool_strdup(lv->vg->cmd->mem,
				      status->sync_action))) {
		dev_manager_destroy(dm);
		return_0;
	}

	*sync_action = action;

	dev_manager_destroy(dm);

	return 1;
}

int lv_raid_message(const struct logical_volume *lv, const char *msg)
{
	int r = 0;
	struct dev_manager *dm;
	struct dm_status_raid *status;

	if (!seg_is_raid(first_seg(lv))) {
		/*
		 * Make it easier for user to know what to do when
		 * they are using thinpool.
		 */
		if (lv_is_thin_pool(lv) &&
		    (lv_is_raid(seg_lv(first_seg(lv), 0)) ||
		     lv_is_raid(first_seg(lv)->metadata_lv))) {
			log_error("Thin pool data or metadata volume "
				  "must be specified. (E.g. \"%s_tdata\")",
				  display_lvname(lv));
			return 0;
		}
		log_error("%s must be a RAID logical volume to perform this action.",
			  display_lvname(lv));
		return 0;
	}

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0)) {
		log_error("Unable to send message to an inactive logical volume.");
		return 0;
	}

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_raid_status(dm, lv, &status))) {
		log_error("Failed to retrieve status of %s.",
			  display_lvname(lv));
		goto out;
	}

	if (!status->sync_action) {
		log_error("Kernel driver does not support this action: %s", msg);
		goto out;
	}

	/*
	 * Note that 'dev_manager_raid_message' allows us to pass down any
	 * currently valid message.  However, this function restricts the
	 * number of user available combinations to a minimum.  Specifically,
	 *     "idle" -> "check"
	 *     "idle" -> "repair"
	 * (The state automatically switches to "idle" when a sync process is
	 * complete.)
	 */
	if (strcmp(msg, "check") && strcmp(msg, "repair")) {
		/*
		 * MD allows "frozen" to operate in a toggling fashion.
		 * We could allow this if we like...
		 */
		log_error("\"%s\" is not a supported sync operation.", msg);
		goto out;
	}
	if (strcmp(status->sync_action, "idle")) {
		log_error("%s state is currently \"%s\".  Unable to switch to \"%s\".",
			  display_lvname(lv), status->sync_action, msg);
		goto out;
	}

	r = dev_manager_raid_message(dm, lv, msg);
out:
	dev_manager_destroy(dm);

	return r;
}

int lv_writecache_message(const struct logical_volume *lv, const char *msg)
{
	int r = 0;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0)) {
		log_error("Unable to send message to an inactive logical volume.");
		return 0;
	}

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	r = dev_manager_writecache_message(dm, lv, msg);

	dev_manager_destroy(dm);

	return r;
}

/*
 * Return dm_status_cache for cache volume, accept also cache pool
 *
 * As there are too many variable for cache volumes, and it hard
 * to make good API - so let's obtain dm_status_cache and return
 * all info we have - user just has to release struct after its use.
 */
int lv_cache_status(const struct logical_volume *cache_lv,
		    struct lv_status_cache **status)
{
	struct dev_manager *dm;
	struct lv_segment *cache_seg;

	if (lv_is_cache_pool(cache_lv)) {
		if (dm_list_empty(&cache_lv->segs_using_this_lv) ||
		    !(cache_seg = get_only_segment_using_this_lv(cache_lv))) {
			log_error(INTERNAL_ERROR "Cannot check status for unused cache pool %s.",
				  display_lvname(cache_lv));
			return 0;
		}
		cache_lv = cache_seg->lv;
	}

	if (lv_is_pending_delete(cache_lv)) {
		log_error("Cannot check status for deleted cache volume %s.",
			  display_lvname(cache_lv));
		return 0;
	}

	if (!lv_info(cache_lv->vg->cmd, cache_lv, 1, NULL, 0, 0)) {
		log_error("Cannot check status for locally inactive cache volume %s.",
			  display_lvname(cache_lv));
		return 0;
	}

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

	if (!(dm = dev_manager_create(cache_lv->vg->cmd, cache_lv->vg->name, 1)))
		return_0;

	if (!dev_manager_cache_status(dm, cache_lv, status)) {
		dev_manager_destroy(dm);
		return_0;
	}
	/* User has to call dm_pool_destroy(status->mem)! */

	return 1;
}

/*
 * Returns data or metadata percent usage, depends on metadata 0/1.
 * Returns 1 if percent set, else 0 on failure.
 */
int lv_thin_pool_percent(const struct logical_volume *lv, int metadata,
			 dm_percent_t *percent)
{
	int r;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 1, NULL, 0, 0))
		return 0;

	log_debug_activation("Checking thin %sdata percent for LV %s.",
			     (metadata) ? "meta" : "", display_lvname(lv));

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_thin_pool_percent(dm, lv, metadata, percent)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

/*
 * Returns 1 if percent set, else 0 on failure.
 */
int lv_thin_percent(const struct logical_volume *lv,
		    int mapped, dm_percent_t *percent)
{
	int r;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_thin_percent(dm, lv, mapped, percent)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

/*
 * Returns 1 if transaction_id set, else 0 on failure.
 */
int lv_thin_pool_transaction_id(const struct logical_volume *lv,
				uint64_t *transaction_id)
{
	int r;
	struct dev_manager *dm;
	struct dm_status_thin_pool *status;

	if (!lv_info(lv->vg->cmd, lv, 1, NULL, 0, 0))
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_thin_pool_status(dm, lv, &status, 0)))
		stack;
	else
		*transaction_id = status->transaction_id;

	dev_manager_destroy(dm);

	return r;
}

int lv_thin_device_id(const struct logical_volume *lv, uint32_t *device_id)
{
	int r;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 0, NULL, 0, 0))
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_thin_device_id(dm, lv, device_id)))
		stack;

	dev_manager_destroy(dm);

	return r;
}

/*
 * lv_vdo_pool_status  obtains  status information about VDO pool
 *
 * If the 'params' string has been already retrieved, use it.
 * If the mempool already exists, use it.
 *
 */
int lv_vdo_pool_status(const struct logical_volume *lv, int flush,
		       struct lv_status_vdo **vdo_status)
{
	int r = 0;
	struct dev_manager *dm;

	if (!lv_info(lv->vg->cmd, lv, 1, NULL, 0, 0))
		return 0;

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

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, !lv_is_pvmove(lv))))
		return_0;

	if (!dev_manager_vdo_pool_status(dm, lv, vdo_status, flush))
		goto_out;

	/* User has to call dm_pool_destroy(vdo_status->mem) */
	r = 1;
out:
	if (!r)
		dev_manager_destroy(dm);

	return r;
}

int lv_vdo_pool_percent(const struct logical_volume *lv, dm_percent_t *percent)
{
	struct lv_status_vdo *vdo_status;

	if (!lv_vdo_pool_status(lv, 0, &vdo_status))
		return_0;

	*percent = vdo_status->usage;
	dm_pool_destroy(vdo_status->mem);

	return 1;
}

static int _lv_active(struct cmd_context *cmd, const struct logical_volume *lv)
{
	struct lvinfo info;

	if (!lv_info(cmd, lv, 0, &info, 0, 0)) {
		log_debug("Cannot determine activation status of %s%s.",
			  display_lvname(lv),
			  activation() ? "" : " (no device driver)");
		return 0;
	}

	return info.exists;
}

static int _lv_open_count(struct cmd_context *cmd, const struct logical_volume *lv)
{
	struct lvinfo info;

	if (!lv_info(cmd, lv, 0, &info, 1, 0)) {
		stack;
		return -1;
	}

	return info.open_count;
}

static int _lv_activate_lv(const struct logical_volume *lv, struct lv_activate_opts *laopts)
{
	int r;
	struct dev_manager *dm;

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, !lv_is_pvmove(lv))))
		return_0;

	if (!(r = dev_manager_activate(dm, lv, laopts)))
		stack;

	dev_manager_destroy(dm);
	return r;
}

static int _lv_preload(const struct logical_volume *lv, struct lv_activate_opts *laopts,
		       int *flush_required)
{
	int r = 0;
	struct dev_manager *dm;
	int old_readonly = laopts->read_only;

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, !lv_is_pvmove(lv))))
		goto_out;

	laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);

	if (!(r = dev_manager_preload(dm, lv, laopts, flush_required)))
		stack;

	dev_manager_destroy(dm);

	laopts->read_only = old_readonly;
out:
	return r;
}

static int _lv_deactivate(const struct logical_volume *lv)
{
	int r;
	struct dev_manager *dm;

	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
		return_0;

	if (!(r = dev_manager_deactivate(dm, lv)))
		stack;

	dev_manager_destroy(dm);
	return r;
}

static int _lv_suspend_lv(const struct logical_volume *lv, struct lv_activate_opts *laopts,
			  int lockfs, int flush_required)
{
	int r;
	struct dev_manager *dm;

	laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);

	/*
	 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
	 * device directly, we don't want to touch the devices that use it.
	 */
	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, !lv_is_pvmove(lv))))
		return_0;

	if (!(r = dev_manager_suspend(dm, lv, laopts, lockfs, flush_required)))
		stack;

	dev_manager_destroy(dm);
	return r;
}

/*
 * These two functions return the number of visible LVs in the state,
 * or -1 on error.  FIXME Check this.
 */
int lvs_in_vg_activated(const struct volume_group *vg)
{
	struct lv_list *lvl;
	int count = 0;

	if (!activation())
		return 0;

	dm_list_iterate_items(lvl, &vg->lvs)
		if (lv_is_visible(lvl->lv))
			count += (_lv_active(vg->cmd, lvl->lv) == 1);

	log_debug_activation("Counted %d active LVs in VG %s", count, vg->name);

	return count;
}

int lvs_in_vg_opened(const struct volume_group *vg)
{
	const struct lv_list *lvl;
	int count = 0;

	if (!activation())
		return 0;

	dm_list_iterate_items(lvl, &vg->lvs)
		if (lv_is_visible(lvl->lv))
			count += (_lv_open_count(vg->cmd, lvl->lv) > 0);

	log_debug_activation("Counted %d open LVs in VG %s.", count, vg->name);

	return count;
}

/*
 * Check if "raid4" @segtype is supported by kernel.
 *
 * if segment type is not raid4, return 1.
 */
int raid4_is_supported(struct cmd_context *cmd, const struct segment_type *segtype)
{
	unsigned attrs;

	if (segtype_is_raid4(segtype) &&
	    (!segtype->ops->target_present ||
             !segtype->ops->target_present(cmd, NULL, &attrs) ||
             !(attrs & RAID_FEATURE_RAID4))) {
		log_error("RAID module does not support RAID4.");
		return 0;
	}

	return 1;
}

/*
 * The VG lock must be held to call this function.
 *
 * Returns: 0 or 1
 */
int lv_is_active(const struct logical_volume *lv)
{
	return _lv_active(lv->vg->cmd, lv);
}

#ifdef DMEVENTD
static struct dm_event_handler *_create_dm_event_handler(struct cmd_context *cmd, const char *dmuuid, const char *dso,
							 const int timeout, enum dm_event_mask mask)
{
	struct dm_event_handler *dmevh;

	if (!(dmevh = dm_event_handler_create()))
		return_NULL;

	if (!cmd->default_settings.dmeventd_executable)
		cmd->default_settings.dmeventd_executable = find_config_tree_str(cmd, dmeventd_executable_CFG, NULL);

	if (dm_event_handler_set_dmeventd_path(dmevh, cmd->default_settings.dmeventd_executable))
		goto_bad;

	if (dso && dm_event_handler_set_dso(dmevh, dso))
		goto_bad;

	if (dm_event_handler_set_uuid(dmevh, dmuuid))
		goto_bad;

	dm_event_handler_set_timeout(dmevh, timeout);
	dm_event_handler_set_event_mask(dmevh, mask);

	return dmevh;

bad:
	dm_event_handler_destroy(dmevh);

	return NULL;
}

char *get_monitor_dso_path(struct cmd_context *cmd, int id)
{
	const char *libpath = find_config_tree_str(cmd, id, NULL);
	char path[PATH_MAX];

	get_shared_library_path(cmd, libpath, path, sizeof(path));

	return strdup(path);
}

static char *_build_target_uuid(struct cmd_context *cmd, const struct logical_volume *lv)
{
	const char *layer;

	if (lv_is_thin_pool(lv))
		layer = "tpool"; /* Monitor "tpool" for the "thin pool". */
	else if (lv_is_vdo_pool(lv))
		layer = "vpool"; /* Monitor "vpool" for the "VDO pool". */
	else if (lv_is_origin(lv) || lv_is_external_origin(lv))
		layer = "real"; /* Monitor "real" for "snapshot-origin". */
	else
		layer = NULL;

	return build_dm_uuid(cmd->mem, lv, layer);
}

static int _device_registered_with_dmeventd(struct cmd_context *cmd,
					    const struct logical_volume *lv,
					    const char **dso,
					    int *pending, int *monitored)
{
	char *uuid;
	enum dm_event_mask evmask;
	struct dm_event_handler *dmevh;
	int r;

	*pending = 0;
	*monitored = 0;

	if (!(uuid = _build_target_uuid(cmd, lv)))
		return_0;

	if (!(dmevh = _create_dm_event_handler(cmd, uuid, NULL, 0, DM_EVENT_ALL_ERRORS)))
		return_0;

	if ((r = dm_event_get_registered_device(dmevh, 0))) {
		if (r == -ENOENT) {
			r = 1;
			goto out;
		}
		r = 0;
		goto_out;
	}

	/* FIXME: why do we care which 'dso' is monitoring? */
	if (dso && (*dso = dm_event_handler_get_dso(dmevh)) &&
	    !(*dso = dm_pool_strdup(cmd->mem, *dso))) {
		r = 0;
		goto_out;
	}

	evmask = dm_event_handler_get_event_mask(dmevh);
	if (evmask & DM_EVENT_REGISTRATION_PENDING) {
		*pending = 1;
		evmask &= ~DM_EVENT_REGISTRATION_PENDING;
	}

	*monitored = evmask;
	r = 1;
out:
	dm_event_handler_destroy(dmevh);

	return r;
}

int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
				    const struct logical_volume *lv,
				    int *pending, int *monitored)
{
	char *uuid;
	enum dm_event_mask evmask;
	struct dm_event_handler *dmevh;
	int r;

	*pending = 0;
	*monitored = 0;

	if (!dso)
		return_0;

	if (!(uuid = _build_target_uuid(cmd, lv)))
		return_0;

	if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
		return_0;

	if ((r = dm_event_get_registered_device(dmevh, 0))) {
		if (r == -ENOENT) {
			r = 1;
			goto out;
		}
		r = 0;
		goto_out;
	}

	evmask = dm_event_handler_get_event_mask(dmevh);
	if (evmask & DM_EVENT_REGISTRATION_PENDING) {
		*pending = 1;
		evmask &= ~DM_EVENT_REGISTRATION_PENDING;
	}

	*monitored = evmask;
	r = 1;
out:
	dm_event_handler_destroy(dmevh);

	return r;
}

int target_register_events(struct cmd_context *cmd, const char *dso, const struct logical_volume *lv,
			    int evmask __attribute__((unused)), int set, int timeout)
{
	char *uuid;
	struct dm_event_handler *dmevh;
	int r;

	if (!dso)
		return_0;

	/* We always monitor the "real" device, never the "snapshot-origin" itself. */
	if (!(uuid = _build_target_uuid(cmd, lv)))
		return_0;

	if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
					       DM_EVENT_ALL_ERRORS | (timeout ? DM_EVENT_TIMEOUT : 0))))
		return_0;

	r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);

	dm_event_handler_destroy(dmevh);

	if (!r)
		return_0;

	log_verbose("%s %s for events", set ? "Monitored" : "Unmonitored", uuid);

	return 1;
}

#endif

/*
 * Returns 0 if an attempt to (un)monitor the device failed.
 * Returns 1 otherwise.
 */
int monitor_dev_for_events(struct cmd_context *cmd, const struct logical_volume *lv,
			   const struct lv_activate_opts *laopts, int monitor)
{
#ifdef DMEVENTD
	int i, pending = 0, monitored = 0;
	int r = 1;
	struct dm_list *snh, *snht;
	struct lv_segment *seg;
	struct lv_segment *log_seg;
	int (*monitor_fn) (struct lv_segment *s, int e);
	uint32_t s;
	static const struct lv_activate_opts zlaopts = { 0 };
	struct lv_activate_opts mirr_laopts = { .origin_only = 1 };
	struct lvinfo info;
	const char *dso = NULL;
	int new_unmonitor;

	if (!laopts)
		laopts = &zlaopts;
	else
		mirr_laopts.read_only = laopts->read_only;

	/* skip dmeventd code altogether */
	if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
		return 1;

	/*
	 * Nothing to do if dmeventd configured not to be used.
	 */
	if (monitor && !dmeventd_monitor_mode())
		return 1;

	/*
	 * Activation of unused cache-pool activates metadata device as
	 * a public LV  for clearing purpose.
	 * FIXME:
	 *  As VG lock is held across whole operation unmonitored volume
	 *  is usually OK since dmeventd couldn't do anything.
	 *  However in case command would have crashed, such LV is
	 *  left unmonitored and may potentially require dmeventd.
	 */
	if (lv_is_cache_pool_data(lv) || lv_is_cache_pool_metadata(lv)) {
		if (!(seg = find_pool_seg(first_seg(lv))))
			return_0;
		if (!lv_is_used_cache_pool(seg->lv)) {
			log_debug_activation("Skipping %smonitor of %s.%s",
					     (monitor) ? "" : "un", display_lvname(lv),
					     (monitor) ? " Cache pool activation for clearing only." : "");
			return 1;
		}
	}

	/*
	 * Allow to unmonitor thin pool via explicit pool unmonitor
	 * or unmonitor before the last thin pool user deactivation
	 * Skip unmonitor, if invoked via deactivation of thin volume
	 * and there is another thin pool user (open_count > 1)
	 * FIXME  think about watch ruler influence.
	 */
	if (laopts->skip_in_use && lv_is_thin_pool(lv) &&
	    lv_info(lv->vg->cmd, lv, 1, &info, 1, 0) && (info.open_count > 1)) {
		log_debug_activation("Skipping unmonitor of opened %s (open:%d)",
				     display_lvname(lv), info.open_count);
		return 1;
	}

	/* Do not monitor snapshot that already covers origin */
	if (monitor && lv_is_cow_covering_origin(lv)) {
		log_debug_activation("Skipping monitor of snapshot larger "
				     "then origin %s.", display_lvname(lv));
		return 1;
	}

	/*
	 * In case of a snapshot device, we monitor lv->snapshot->lv,
	 * not the actual LV itself.
	 */
	if (lv_is_cow(lv) && (laopts->no_merging || !lv_is_merging_cow(lv) ||
			      lv_has_target_type(lv->vg->cmd->mem, lv, NULL, TARGET_NAME_SNAPSHOT))) {
		if (!(r = monitor_dev_for_events(cmd, lv->snapshot->lv, NULL, monitor)))
			stack;
		return r;
	}

	/*
	 * In case this LV is a snapshot origin, we instead monitor
	 * each of its respective snapshots.  The origin itself may
	 * also need to be monitored if it is a mirror, for example,
	 * so fall through to process it afterwards.
	 */
	if (!laopts->origin_only && lv_is_origin(lv))
		dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
			if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
				struct lv_segment, origin_list)->cow, NULL, monitor)) {
				stack;
				r = 0;
			}

	/*
	 * If the volume is mirrored and its log is also mirrored, monitor
	 * the log volume as well.
	 */
	if ((seg = first_seg(lv)) != NULL && seg->log_lv != NULL &&
	    (log_seg = first_seg(seg->log_lv)) != NULL &&
	    seg_is_mirrored(log_seg))
		if (!monitor_dev_for_events(cmd, seg->log_lv, NULL, monitor)) {
			stack;
			r = 0;
		}

	dm_list_iterate_items(seg, &lv->segments) {
		/* Recurse for AREA_LV */
		for (s = 0; s < seg->area_count; s++) {
			if (seg_type(seg, s) != AREA_LV)
				continue;
			if (!monitor_dev_for_events(cmd, seg_lv(seg, s), NULL,
						    monitor)) {
				stack;
				r = 0;
			}
		}

		/*
		 * If requested unmonitoring of thin volume, preserve skip_in_use flag.
		 *
		 * FIXME: code here looks like _lv_postorder()
		 */
		if (seg->pool_lv &&
		    !monitor_dev_for_events(cmd, seg->pool_lv,
					    (!monitor) ? laopts : NULL, monitor)) {
			stack;
			r = 0;
		}

		if (seg->external_lv &&
		    !monitor_dev_for_events(cmd, seg->external_lv,
					    (!monitor) ? laopts : NULL, monitor)) {
			stack;
			r = 0;
		}

		if (seg->metadata_lv &&
		    !monitor_dev_for_events(cmd, seg->metadata_lv, NULL, monitor)) {
			stack;
			r = 0;
		}

		if (!seg_monitored(seg) ||
		    (seg->status & PVMOVE) ||
		    !seg->segtype->ops->target_monitored) /* doesn't support registration */
			continue;

		if (!monitor) {
			/* When unmonitoring, obtain existing dso being used. */
			if (!_device_registered_with_dmeventd(cmd, seg_is_snapshot(seg) ? seg->cow : seg->lv,
							      &dso, &pending, &monitored)) {
				log_warn("WARNING: Failed to %smonitor %s.",
					 monitor ? "" : "un",
					 display_lvname(seg_is_snapshot(seg) ? seg->cow : seg->lv));
				return 0;
			}
		} else if (!seg->segtype->ops->target_monitored(seg, &pending, &monitored)) {
			log_warn("WARNING: Failed to %smonitor %s.",
				 monitor ? "" : "un",
				 display_lvname(seg->lv));
			return 0;
		}

		/* FIXME: We should really try again if pending */
		monitored = (pending) ? 0 : monitored;

		monitor_fn = NULL;
		new_unmonitor = 0;

		if (monitor) {
			if (monitored)
				log_verbose("%s already monitored.", display_lvname(lv));
			else if (seg->segtype->ops->target_monitor_events) {
				log_very_verbose("Monitoring %s with %s.%s", display_lvname(lv),
						 seg->segtype->dso,
						 test_mode() ? " [Test mode: skipping this]" : "");
				monitor_fn = seg->segtype->ops->target_monitor_events;
			}
		} else {
			if (!monitored)
				log_verbose("%s already not monitored.", display_lvname(lv));
			else if (dso && *dso) {
				/*
				 * Divert unmonitor away from code that depends on the new segment
				 * type instead of the existing one if it's changing.
				 */
				log_verbose("Not monitoring %s with %s%s", display_lvname(lv), dso, test_mode() ? " [Test mode: skipping this]" : "");
				new_unmonitor = 1;
			}
		}

		/* FIXME Test mode should really continue a bit further. */
		if (test_mode())
			continue;

		if (new_unmonitor) {
			if (!target_register_events(cmd, dso, seg_is_snapshot(seg) ? seg->cow : lv, 0, 0, 10)) {
				log_warn("WARNING: %s: segment unmonitoring failed.",
					 display_lvname(lv));
				return 0;
			}
		} else if (monitor_fn) {
			/* FIXME specify events */
			if (!monitor_fn(seg, 0)) {
				log_warn("WARNING: %s: %s segment monitoring function failed.",
					 display_lvname(lv), lvseg_name(seg));
				return 0;
			}
		} else
			continue;

		if (!vg_write_lock_held() && lv_is_mirror(lv)) {
			mirr_laopts.exclusive = lv_is_active(lv) ? 1 : 0;
			/*
			 * Commands vgchange and lvchange do use read-only lock when changing
			 * monitoring (--monitor y|n). All other use cases hold 'write-lock'
			 * so they skip this dm mirror table refreshing step.
			 */
			if (!_lv_activate_lv(lv, &mirr_laopts)) {
				stack;
				r = 0;
			}
		}

		/* Check [un]monitor results */
		/* Try a couple times if pending, but not forever... */
		for (i = 0;; i++) {
			pending = 0;
			if (!seg->segtype->ops->target_monitored(seg, &pending, &monitored)) {
				stack;
				r = 0;
				break;
			}
			if (!pending || i >= 40)
				break;
			log_very_verbose("%s %smonitoring still pending: waiting...",
					 display_lvname(lv), monitor ? "" : "un");
			usleep(10000 * i);
		}

		if (r)
			r = (monitored && monitor) || (!monitored && !monitor);
	}

	if (!r && !error_message_produced())
		log_warn("WARNING: %sonitoring %s failed.", monitor ? "M" : "Not m",
			 display_lvname(lv));
	return r;
#else
	return 1;
#endif
}

struct detached_lv_data {
	const struct logical_volume *lv_pre;
	struct lv_activate_opts *laopts;
	int *flush_required;
};

static int _preload_detached_lv(struct logical_volume *lv, void *data)
{
	struct detached_lv_data *detached = data;
	struct logical_volume *lv_pre;

	/* Check and preload removed raid image leg or metadata */
	if (lv_is_raid_image(lv)) {
		if ((lv_pre = find_lv_in_vg_by_lvid(detached->lv_pre->vg, &lv->lvid)) &&
		    !lv_is_raid_image(lv_pre) && lv_is_active(lv) &&
		    !_lv_preload(lv_pre, detached->laopts, detached->flush_required))
			return_0;
	} else if (lv_is_raid_metadata(lv)) {
		if ((lv_pre = find_lv_in_vg_by_lvid(detached->lv_pre->vg, &lv->lvid)) &&
		    !lv_is_raid_metadata(lv_pre) && lv_is_active(lv) &&
		    !_lv_preload(lv_pre, detached->laopts, detached->flush_required))
			return_0;
	} else if (lv_is_mirror_image(lv)) {
		if ((lv_pre = find_lv_in_vg_by_lvid(detached->lv_pre->vg, &lv->lvid)) &&
		    !lv_is_mirror_image(lv_pre) && lv_is_active(lv) &&
		    !_lv_preload(lv_pre, detached->laopts, detached->flush_required))
			return_0;
	}

	if (!lv_is_visible(lv) && (lv_pre = find_lv(detached->lv_pre->vg, lv->name)) &&
	    lv_is_visible(lv_pre)) {
		if (!_lv_preload(lv_pre, detached->laopts, detached->flush_required))
			return_0;
	}

	/* FIXME: condition here should be far more limiting to really
	 *        detect detached LVs */
	if ((lv_pre = find_lv(detached->lv_pre->vg, lv->name))) {
		if (lv_is_visible(lv_pre) && lv_is_active(lv) &&
		    !lv_is_pool(lv) &&
		    (!lv_is_cow(lv) || !lv_is_cow(lv_pre)) &&
		    !_lv_preload(lv_pre, detached->laopts, detached->flush_required))
			return_0;
	}

	return 1;
}

static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
		       struct lv_activate_opts *laopts, int error_if_not_suspended,
	               const struct logical_volume *lv, const struct logical_volume *lv_pre)
{
	const struct logical_volume *pvmove_lv = NULL;
	struct logical_volume *lv_pre_tmp, *lv_tmp;
	struct seg_list *sl;
	struct lv_segment *snap_seg;
	struct lvinfo info;
	int r = 0, lockfs = 0, flush_required = 0;
	struct detached_lv_data detached;
	struct dm_pool *mem = NULL;
	struct dm_list suspend_lvs;
	struct lv_list *lvl;
	int found;

	if (!activation())
		return 1;

	/* Ignore origin_only unless LV is origin in both old and new metadata */
	/* or LV is thin or thin pool volume */
	if (!lv_is_thin_volume(lv) && !lv_is_thin_pool(lv) &&
	    !(lv_is_origin(lv) && lv_is_origin(lv_pre)))
		laopts->origin_only = 0;

	if (test_mode()) {
		_skip("Suspending %s%s.", display_lvname(lv),
		      laopts->origin_only ? " origin without snapshots" : "");
		r = 1;
		goto out;
	}

	if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
		goto_out;

	if (!info.exists || info.suspended) {
		if (!error_if_not_suspended) {
			r = 1;
			if (info.suspended)
				critical_section_inc(cmd, "already suspended");
		}
		goto out;
	}

	lv_calculate_readahead(lv, NULL);

	/*
	 * Preload devices for the LV.
	 * If the PVMOVE LV is being removed, it's only present in the old
	 * metadata and not the new, so we must explicitly add the new
	 * tables for all the changed LVs here, as the relationships
	 * are not found by walking the new metadata.
	 */
	if (lv_is_locked(lv) && !lv_is_locked(lv_pre) &&
	    (pvmove_lv = find_pvmove_lv_in_lv(lv))) {
		/* Preload all the LVs above the PVMOVE LV */
		dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
			if (!(lv_pre_tmp = find_lv(lv_pre->vg, sl->seg->lv->name))) {
				log_error(INTERNAL_ERROR "LV %s missing from preload metadata.",
					  display_lvname(sl->seg->lv));
				goto out;
			}
			if (!_lv_preload(lv_pre_tmp, laopts, &flush_required))
				goto_out;
		}
		/* Now preload the PVMOVE LV itself */
		if (!(lv_pre_tmp = find_lv(lv_pre->vg, pvmove_lv->name))) {
			log_error(INTERNAL_ERROR "LV %s missing from preload metadata.",
				  display_lvname(pvmove_lv));
			goto out;
		}
		if (!_lv_preload(lv_pre_tmp, laopts, &flush_required))
			goto_out;

		/* Suspending 1st. LV above PVMOVE suspends whole tree */
		dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
			lv = sl->seg->lv;
			break;
		}
	} else {
		if (!_lv_preload(lv_pre, laopts, &flush_required))
			/* FIXME Revert preloading */
			goto_out;

		/*
		 * Search for existing LVs that have become detached and preload them.
		 */
		detached.lv_pre = lv_pre;
		detached.laopts = laopts;
		detached.flush_required = &flush_required;

		if (!for_each_sub_lv((struct logical_volume *)lv, &_preload_detached_lv, &detached))
			goto_out;

		/*
		 * Preload any snapshots that are being removed.
		 */
		if (!laopts->origin_only && lv_is_origin(lv)) {
			dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
				if (!(lv_pre_tmp = find_lv_in_vg_by_lvid(lv_pre->vg, &snap_seg->cow->lvid))) {
					log_error(INTERNAL_ERROR "LV %s (%s) missing from preload metadata.",
						  display_lvname(snap_seg->cow),
						  snap_seg->cow->lvid.id[1].uuid);
					goto out;
				}
				if (!lv_is_cow(lv_pre_tmp) &&
				    !_lv_preload(lv_pre_tmp, laopts, &flush_required))
					goto_out;
			}
		}
	}

	/* Flush is ATM required for the tested cases
	 * NOTE: Mirror repair requires noflush for proper repair!
	 * TODO: Relax this limiting condition further */
	if (!flush_required &&
	    (lv_is_pvmove(lv) || pvmove_lv ||
	     (!lv_is_mirror(lv) && !lv_is_thin_pool(lv) && !lv_is_thin_volume(lv)))) {
		log_debug("Requiring flush for LV %s.", display_lvname(lv));
		flush_required = 1;
	}

	if (!monitor_dev_for_events(cmd, lv, laopts, 0))
		/* FIXME Consider aborting here */
		stack;

	if (!laopts->origin_only &&
	    (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)))
		lockfs = 1;

	/* Converting non-thin LV to thin external origin ? */
	if (!lv_is_thin_volume(lv) && lv_is_thin_volume(lv_pre))
		lockfs = 1; /* Sync before conversion */

	if (laopts->origin_only && lv_is_thin_volume(lv) && lv_is_thin_volume(lv_pre))
		lockfs = 1;

	critical_section_inc(cmd, "suspending");

	if (!lv_is_locked(lv) && lv_is_locked(lv_pre) &&
	    (pvmove_lv = find_pvmove_lv_in_lv(lv_pre))) {
		/*
		 * When starting PVMOVE, suspend participating LVs first
		 * with committed metadata by looking at precommited pvmove list.
		 * In committed metadata these LVs are not connected in any way.
		 *
		 * TODO: prepare list of LVs needed to be suspended and pass them
		 *       via 'struct laopts' directly to _lv_suspend_lv() and handle this
		 *       with a single 'dmtree' call.
		 */
		if (!(mem = dm_pool_create("suspend_lvs", 128)))
			goto_out;

		/* Prepare list of all LVs for suspend ahead */
		dm_list_init(&suspend_lvs);
		dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
			lv_tmp = sl->seg->lv;
			if (lv_is_cow(lv_tmp))
				/* Never suspend COW, always has to be origin */
				lv_tmp = origin_from_cow(lv_tmp);
			found = 0;
			dm_list_iterate_items(lvl, &suspend_lvs)
				if (strcmp(lvl->lv->name, lv_tmp->name) == 0) {
					found = 1;
					break;
				}
			if (found)
				continue; /* LV is already in the list */
			if (!(lvl = dm_pool_alloc(mem, sizeof(*lvl)))) {
				log_error("lv_list alloc failed.");
				goto out;
			}
			/* Look for precommitted LV name in commmitted VG */
			if (!(lvl->lv = find_lv(lv->vg, lv_tmp->name))) {
				log_error(INTERNAL_ERROR "LV %s missing from preload metadata.",
					  display_lvname(lv_tmp));
				goto out;
			}
			dm_list_add(&suspend_lvs, &lvl->list);
		}
		dm_list_iterate_items(lvl, &suspend_lvs)
			if (!_lv_suspend_lv(lvl->lv, laopts, lockfs, 1)) {
				critical_section_dec(cmd, "failed suspend");
				goto_out; /* FIXME: resume on recovery path? */
			}
	} else  /* Standard suspend */
		if (!_lv_suspend_lv(lv, laopts, lockfs, flush_required)) {
			critical_section_dec(cmd, "failed suspend");
			goto_out;
		}

	r = 1;
out:
	if (mem)
		dm_pool_destroy(mem);

	return r;
}

/*
 * In a cluster, set exclusive to indicate that only one node is using the
 * device.  Any preloaded tables may then use non-clustered targets.
 *
 * Returns success if the device is not active
 */
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only, unsigned exclusive,
			 const struct logical_volume *lv, const struct logical_volume *lv_pre)
{
	struct lv_activate_opts laopts = {
		.origin_only = origin_only,
		.exclusive = exclusive
	};

	return _lv_suspend(cmd, lvid_s, &laopts, 0, lv, lv_pre);
}

static int _check_suspended_lv(struct logical_volume *lv, void *data)
{
	struct lvinfo info;

	if (lv_info(lv->vg->cmd, lv, 0, &info, 0, 0) && info.exists && info.suspended) {
		log_debug("Found suspended LV %s in critical section().", display_lvname(lv));
		return 0; /* There is suspended subLV in the tree */
	}

	if (lv_layer(lv) && lv_info(lv->vg->cmd, lv, 1, &info, 0, 0) && info.exists && info.suspended) {
		log_debug("Found suspended layered LV %s in critical section().", display_lvname(lv));
		return 0; /* There is suspended subLV in the tree */
	}

	return 1;
}

static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
		      struct lv_activate_opts *laopts, int error_if_not_active,
	              const struct logical_volume *lv)
{
	struct dm_list *snh;
	struct lvinfo info;
	int r = 0;

	if (!activation())
		return 1;

	if (!lv_is_origin(lv) && !lv_is_thin_volume(lv) && !lv_is_thin_pool(lv))
		laopts->origin_only = 0;

	if (test_mode()) {
		_skip("Resuming %s%s%s.", display_lvname(lv),
		      laopts->origin_only ? " without snapshots" : "",
		      laopts->revert ? " (reverting)" : "");
		r = 1;
		goto out;
	}

	log_debug_activation("Resuming LV %s%s%s%s.", display_lvname(lv),
			     error_if_not_active ? "" : " if active",
			     laopts->origin_only ?
			     (lv_is_thin_pool(lv) ? " pool only" :
			      lv_is_thin_volume(lv) ? " thin only" : " without snapshots") : "",
			     laopts->revert ? " (reverting)" : "");

	if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
		goto_out;

	if (!info.exists || !info.suspended) {
		if (error_if_not_active)
			goto_out;

		/* ATM only thin-pool with origin-only suspend does not really suspend anything
		 * it's used only for message passing to thin-pool */
		if (laopts->origin_only && lv_is_thin_pool(lv))
			critical_section_dec(cmd, "resumed");

		if (!info.suspended && critical_section()) {
			/* Validation check if any subLV is suspended */
			if (!laopts->origin_only && lv_is_origin(lv)) {
				/* Check all snapshots for this origin LV */
				dm_list_iterate(snh, &lv->snapshot_segs)
					if (!_check_suspended_lv(dm_list_struct_base(snh, struct lv_segment, origin_list)->cow, NULL))
						goto needs_resume; /* Found suspended snapshot */
			}
			if ((r = for_each_sub_lv((struct logical_volume *)lv, &_check_suspended_lv, NULL)))
				goto out; /* Nothing was found suspended */
		} else {
			r = 1;
			goto out;
		}
	}
needs_resume:
	laopts->read_only = _passes_readonly_filter(cmd, lv);
	laopts->resuming = 1;

	if (!_lv_activate_lv(lv, laopts))
		goto_out;

	critical_section_dec(cmd, "resumed");

	if (!monitor_dev_for_events(cmd, lv, laopts, 1))
		stack;

	r = 1;
out:
	return r;
}

/*
 * In a cluster, set exclusive to indicate that only one node is using the
 * device.  Any tables loaded may then use non-clustered targets.
 *
 * @origin_only
 * @exclusive   This parameter only has an affect in cluster-context.
 *              It forces local target type to be used (instead of
 *              cluster-aware type).
 * Returns success if the device is not active
 */
int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
			unsigned origin_only, unsigned exclusive,
			unsigned revert, const struct logical_volume *lv)
{
	struct lv_activate_opts laopts = {
		.origin_only = origin_only,
		.exclusive = exclusive,
		.revert = revert
	};

	return _lv_resume(cmd, lvid_s, &laopts, 0, lv);
}

int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only,
	      const struct logical_volume *lv)
{
	struct lv_activate_opts laopts = { .origin_only = origin_only, };

	return _lv_resume(cmd, lvid_s, &laopts, 1, lv);
}

static int _lv_has_open_snapshots(const struct logical_volume *lv)
{
	struct lv_segment *snap_seg;
	int r = 0;

	dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list)
		if (!lv_check_not_in_use(snap_seg->cow, 1))
			r++;

	if (r)
		log_error("LV %s has open %d snapshot(s), not deactivating.",
			  display_lvname(lv), r);

	return r;
}

int lv_deactivate(struct cmd_context *cmd, const char *lvid_s, const struct logical_volume *lv)
{
	struct lvinfo info;
	static const struct lv_activate_opts laopts = { .skip_in_use = 1 };
	struct dm_list *snh;
	int r = 0;

	if (!activation())
		return 1;

	if (test_mode()) {
		_skip("Deactivating %s.", display_lvname(lv));
		r = 1;
		goto out;
	}

	log_debug_activation("Deactivating %s.", display_lvname(lv));

	if (!lv_info(cmd, lv, 0, &info, 0, 0))
		goto_out;

	if (!info.exists) {
		r = 1;
		/* Check attached snapshot segments are also inactive */
		dm_list_iterate(snh, &lv->snapshot_segs) {
			if (!lv_info(cmd, dm_list_struct_base(snh, struct lv_segment, origin_list)->cow,
				     0, &info, 0, 0))
				goto_out;
			if (info.exists) {
				r = 0; /* Snapshot left in table? */
				break;
			}
		}

		if (lv_is_vdo_pool(lv)) {
			/* If someone has remove 'linear' mapping over VDO device
			 * we may still be able to deactivate the rest of the tree
			 * i.e. in test-suite we simulate this via 'dmsetup remove' */
			if (!lv_info(cmd, lv, 1, &info, 1, 0))
				goto_out;

			if (info.exists && !info.open_count)
				r = 0; /* Unused VDO device left in table? */
		}

		if (r)
			goto out;
	}

	if (lv_is_visible(lv) || lv_is_virtual_origin(lv) ||
	    lv_is_merging_thin_snapshot(lv)) {
		if (!lv_check_not_in_use(lv, 1))
			goto_out;

		if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
			goto_out;
	}

	if (!monitor_dev_for_events(cmd, lv, &laopts, 0))
		stack;

	critical_section_inc(cmd, "deactivating");
	r = _lv_deactivate(lv);

	/*
	 * Remove any transiently activated error
	 * devices which arean't used any more.
	 */
	if (r && lv_is_raid(lv) && !lv_deactivate_any_missing_subdevs(lv)) {
		log_error("Failed to remove temporary SubLVs from %s",
			  display_lvname(lv));
		r = 0;
	}
	critical_section_dec(cmd, "deactivated");

	if (!lv_info(cmd, lv, 0, &info, 0, 0) || info.exists) {
		/* Turn into log_error, but we do not log error */
		log_debug_activation("Deactivated volume is still %s present.",
				     display_lvname(lv));
		r = 0;
	}
out:

	return r;
}

/* Test if LV passes filter */
int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
			 int *activate, const struct logical_volume *lv)
{
	if (!activation()) {
		*activate = 1;
		return 1;
	}

	if (!_passes_activation_filter(cmd, lv)) {
		log_verbose("Not activating %s since it does not pass "
			    "activation filter.", display_lvname(lv));
		*activate = 0;
	} else
		*activate = 1;

	return 1;
}

static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
			struct lv_activate_opts *laopts, int filter,
	                const struct logical_volume *lv)
{
	struct lvinfo info;
	int r = 0;

	if (!activation())
		return 1;

	if (filter && !_passes_activation_filter(cmd, lv)) {
		log_verbose("Not activating %s since it does not pass "
			    "activation filter.", display_lvname(lv));
		r = 1;
		goto out;
	}

	if ((!lv->vg->cmd->partial_activation) && lv_is_partial(lv)) {
		if (!lv_is_raid_type(lv) || !partial_raid_lv_supports_degraded_activation(lv)) {
			log_error("Refusing activation of partial LV %s.  "
				  "Use '--activationmode partial' to override.",
				  display_lvname(lv));
			goto out;
		}

		if (!lv->vg->cmd->degraded_activation) {
			log_error("Refusing activation of partial LV %s.  "
				  "Try '--activationmode degraded'.",
				  display_lvname(lv));
			goto out;
		}
	}

	if (lv_has_unknown_segments(lv)) {
		log_error("Refusing activation of LV %s containing "
			  "an unrecognised segment.", display_lvname(lv));
		goto out;
	}

	if (lv_raid_has_visible_sublvs(lv)) {
		log_error("Refusing activation of RAID LV %s with "
			  "visible SubLVs.", display_lvname(lv));
		goto out;
	}

	if (test_mode()) {
		_skip("Activating %s.", display_lvname(lv));
		r = 1;
		goto out;
	}

	/* Component LV activation is enforced to be 'read-only' */
	/* TODO: should not apply for LVs in maintenance mode */
	if (!lv_is_visible(lv) && lv_is_component(lv)) {
		laopts->read_only = 1;
		laopts->component_lv = lv;
	} else if (filter)
		laopts->read_only = _passes_readonly_filter(cmd, lv);

	log_debug_activation("Activating %s%s%s%s%s.", display_lvname(lv),
			     laopts->exclusive ? " exclusively" : "",
			     laopts->read_only ? " read-only" : "",
			     laopts->noscan ? " noscan" : "",
			     laopts->temporary ? " temporary" : "");

	if (!lv_info_with_name_check(cmd, lv, 0, &info))
		goto_out;

	/*
	 * Nothing to do?
	 */
	if (info.exists && !info.suspended && info.live_table &&
	    (info.read_only == read_only_lv(lv, laopts, NULL))) {
		r = 1;
		log_debug_activation("LV %s is already active.", display_lvname(lv));
		goto out;
	}

	lv_calculate_readahead(lv, NULL);

	critical_section_inc(cmd, "activating");
	if (!(r = _lv_activate_lv(lv, laopts)))
		stack;
	critical_section_dec(cmd, "activated");

	if (r && !monitor_dev_for_events(cmd, lv, laopts, 1))
		stack;
out:
	return r;
}

/* Activate LV */
int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive,
		int noscan, int temporary, const struct logical_volume *lv)
{
	struct lv_activate_opts laopts = { .exclusive = exclusive,
					   .noscan = noscan,
					   .temporary = temporary };

	if (!_lv_activate(cmd, lvid_s, &laopts, 0, lv))
		return_0;

	return 1;
}

/* Activate LV only if it passes filter */
int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive,
			    int noscan, int temporary, const struct logical_volume *lv)
{
	struct lv_activate_opts laopts = { .exclusive = exclusive,
					   .noscan = noscan,
					   .temporary = temporary };

	if (!_lv_activate(cmd, lvid_s, &laopts, 1, lv))
		return_0;

	return 1;
}

int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int r;

	if (!lv) {
		r = dm_mknodes(NULL);
		fs_unlock();
		return r;
	}

	if (!activation())
		return 1;

	r = dev_manager_mknodes(lv);

	fs_unlock();

	return r;
}

/* Remove any existing, closed mapped device by @name */
static int _remove_dm_dev_by_name(const char *name)
{
	int r = 0;
	struct dm_task *dmt;
	struct dm_info info;

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

	/* Check, if the device exists. */
	if (dm_task_set_name(dmt, name) && dm_task_run(dmt) && dm_task_get_info(dmt, &info)) {
		dm_task_destroy(dmt);

		/* Ignore non-existing or open dm devices */
		if (!info.exists || info.open_count)
			return 1;

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

		if (dm_task_set_name(dmt, name))
			r = dm_task_run(dmt);
	}

	dm_task_destroy(dmt);

	return r;
}

/* Work all segments of @lv removing any existing, closed "*-missing_N_0" sub devices. */
static int _lv_remove_any_missing_subdevs(struct logical_volume *lv)
{
	if (lv) {
		uint32_t seg_no = 0;
		char name[257];
		struct lv_segment *seg;

		dm_list_iterate_items(seg, &lv->segments) {
			if (dm_snprintf(name, sizeof(name), "%s-%s-missing_%u_0", seg->lv->vg->name, seg->lv->name, seg_no) < 0)
				return_0;
			if (!_remove_dm_dev_by_name(name))
				return 0;

			seg_no++;
		}
	}

	return 1;
}

/* Remove any "*-missing_*" sub devices added by the activation layer for an rmate/rimage missing PV mapping */
int lv_deactivate_any_missing_subdevs(const struct logical_volume *lv)
{
	uint32_t s;
	struct lv_segment *seg = first_seg(lv);

	for (s = 0; s < seg->area_count; s++) {
		if (seg_type(seg, s) == AREA_LV &&
		    !_lv_remove_any_missing_subdevs(seg_lv(seg, s)))
			return 0;
		if (seg->meta_areas && seg_metatype(seg, s) == AREA_LV &&
		    !_lv_remove_any_missing_subdevs(seg_metalv(seg, s)))
			return 0;
	}

	return 1;
}

/*
 * Does PV use VG somewhere in its construction?
 * Returns 1 on failure.
 */
int pv_uses_vg(struct physical_volume *pv,
	       struct volume_group *vg)
{
	if (!activation() || !pv->dev)
		return 0;

	if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
		return 0;

	return dev_manager_device_uses_vg(pv->dev, vg);
}

void activation_release(void)
{
	if (critical_section())
		/* May leak stacked operation */
		log_error("Releasing activation in critical section.");

	fs_unlock(); /* Implicit dev_manager_release(); */
}

void activation_exit(void)
{
	activation_release();
	dev_manager_exit();
}
#endif

static int _component_cb(struct logical_volume *lv, void *data)
{
	struct logical_volume **component_lv = (struct logical_volume **) data;

	if (lv_is_locked(lv) || lv_is_pvmove(lv) ||/* ignoring */
	    /* thin-pool is special and it's using layered device */
	    (lv_is_thin_pool(lv) && pool_is_active(lv)))
		return -1;

	if (lv_is_active(lv)) {
		if (!lv_is_component(lv) || lv_is_visible(lv))
			return -1;	/* skip whole subtree */

		log_debug_activation("Found active component LV %s.", display_lvname(lv));
		*component_lv = lv;
		return 0;	/* break any further processing */
	}

	return 1;
}

/*
 * Finds out for any LV if any of its component LVs are active.
 * Function first checks if an existing LV is visible and active eventually
 * it's lock holding LV is already active. In such case sub LV cannot be
 * actived alone and no further checking is needed.
 *
 * Returns active component LV if there is such.
 */
const struct logical_volume *lv_component_is_active(const struct logical_volume *lv)
{
	const struct logical_volume *component_lv = NULL;
	const struct logical_volume *holder_lv = lv_lock_holder(lv);

	if ((holder_lv != lv) && lv_is_active(holder_lv))
                return NULL; /* Lock holding LV is active, do not check components */

	if (_component_cb((struct logical_volume *) lv, &holder_lv) == 1)
		(void) for_each_sub_lv((struct logical_volume *) lv, _component_cb,
				       (void*) &component_lv);

	return component_lv;
}

/*
 * Finds out if any LV above is active, as stacked device tree can be composed of
 * chained set of LVs.
 *
 * Returns active holder LV if there is such.
 */
const struct logical_volume *lv_holder_is_active(const struct logical_volume *lv)
{
	const struct logical_volume *holder;
	const struct seg_list *sl;

	if (lv_is_locked(lv) || lv_is_pvmove(lv))
		return NULL; /* Skip pvmove/locked LV tracking */

	dm_list_iterate_items(sl, &lv->segs_using_this_lv) {
		/* Recursive call for upper-stack holder */
		if ((holder = lv_holder_is_active(sl->seg->lv)))
			return holder;

		if (lv_is_active(sl->seg->lv)) {
			log_debug_activation("Found active holder LV %s.", display_lvname(sl->seg->lv));
			return sl->seg->lv;
		}
	}

	return NULL;
}

static int _deactivate_sub_lv_cb(struct logical_volume *lv, void *data)
{
	struct logical_volume **slv = data;

	if (lv_is_thin_pool(lv) || lv_is_external_origin(lv))
		return -1;

	if (!deactivate_lv(lv->vg->cmd, lv)) {
		*slv = lv;
		return 0;
	}

	return 1;
}

/*
 * Deactivates LV toghether with explicit deactivation call made also for all its component LVs.
 */
int deactivate_lv_with_sub_lv(const struct logical_volume *lv)
{
	struct logical_volume *flv;

	if (!deactivate_lv(lv->vg->cmd, lv)) {
		log_error("Cannot deactivate logical volume %s.",
			  display_lvname(lv));
		return 0;
	}

	if (!for_each_sub_lv((struct logical_volume *)lv, _deactivate_sub_lv_cb, &flv)) {
		log_error("Cannot deactivate subvolume %s of logical volume %s.",
			  display_lvname(flv), display_lvname(lv));
		return 0;
	}

	return 1;
}

int activate_lv(struct cmd_context *cmd, const struct logical_volume *lv)
{
	const struct logical_volume *active_lv;
	int ret;

	/*
	 * When trying activating component LV, make sure none of sub component
	 * LV or LVs that are using it are active.
	 */
	if (!lv_is_visible(lv))
		active_lv = lv_holder_is_active(lv);
	else
		active_lv = lv_component_is_active(lv);

	if (active_lv) {
		log_error("Activation of logical volume %s is prohibited while logical volume %s is active.",
			  display_lvname(lv), display_lvname(active_lv));
		ret = 0;
		goto out;
	}

	ret = lv_activate_with_filter(cmd, NULL, 0,
				      (lv->status & LV_NOSCAN) ? 1 : 0,
				      (lv->status & LV_TEMPORARY) ? 1 : 0,
				      lv_committed(lv));
out:
	return ret;
}

int deactivate_lv(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	ret = lv_deactivate(cmd, NULL, lv_committed(lv));

	return ret;
}

int suspend_lv(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	critical_section_inc(cmd, "locking for suspend");

	ret = lv_suspend_if_active(cmd, NULL, 0, 0, lv_committed(lv), lv);

	return ret;
}

int suspend_lv_origin(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	critical_section_inc(cmd, "locking for suspend");

	ret = lv_suspend_if_active(cmd, NULL, 1, 0, lv_committed(lv), lv);

	return ret;
}

int resume_lv(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	ret = lv_resume_if_active(cmd, NULL, 0, 0, 0, lv_committed(lv));

	critical_section_dec(cmd, "unlocking on resume");

	return ret;
}

int resume_lv_origin(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	ret = lv_resume_if_active(cmd, NULL, 1, 0, 0, lv_committed(lv));

	critical_section_dec(cmd, "unlocking on resume");

	return ret;
}

int revert_lv(struct cmd_context *cmd, const struct logical_volume *lv)
{
	int ret;

	ret = lv_resume_if_active(cmd, NULL, 0, 0, 1, lv_committed(lv));

	critical_section_dec(cmd, "unlocking on resume");

	return ret;
}