1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591
|
/*
* Copyright (c) 2004, 2005 Christophe Varoqui
* Copyright (c) 2005 Kiyoshi Ueda, NEC
* Copyright (c) 2005 Benjamin Marzinski, Redhat
* Copyright (c) 2005 Edward Goggin, EMC
*/
#include "autoconfig.h"
#include <unistd.h>
#include <sys/stat.h>
#include <libdevmapper.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <linux/oom.h>
#include "mt-udev-wrap.h"
#include <urcu.h>
#include "fpin.h"
#ifdef USE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <semaphore.h>
#include <time.h>
#include <stdbool.h>
/*
* libmultipath
*/
#include "time-util.h"
/*
* libcheckers
*/
#include "checkers.h"
/*
* libmultipath
*/
#include "version.h"
#include "parser.h"
#include "vector.h"
#include "config.h"
#include "util.h"
#include "hwtable.h"
#include "defaults.h"
#include "structs.h"
#include "blacklist.h"
#include "structs_vec.h"
#include "dmparser.h"
#include "devmapper.h"
#include "sysfs.h"
#include "dict.h"
#include "discovery.h"
#include "debug.h"
#include "propsel.h"
#include "uevent.h"
#include "switchgroup.h"
#include "print.h"
#include "configure.h"
#include "prio.h"
#include "wwids.h"
#include "pgpolicies.h"
#include "log.h"
#include "uxsock.h"
#include "alias.h"
#include "mpath_cmd.h"
#include "mpath_persist.h"
#include "mpath_persist_int.h"
#include "prioritizers/alua_rtpg.h"
#include "main.h"
#include "pidfile.h"
#include "uxlsnr.h"
#include "uxclnt.h"
#include "cli.h"
#include "cli_handlers.h"
#include "lock.h"
#include "waiter.h"
#include "dmevents.h"
#include "io_err_stat.h"
#include "foreign.h"
#include "purge.h"
#include "../third-party/valgrind/drd.h"
#include "init_unwinder.h"
#define CMDSIZE 160
#define MSG_SIZE 32
static unsigned int
mpath_pr_event_handle(struct path *pp, unsigned int nr_keys_needed,
unsigned int nr_keys_wanted);
#define LOG_MSG(lvl, pp) \
do { \
if (pp->mpp && lvl <= libmp_verbosity) { \
if (pp->sysfs_state != PATH_UP) \
condlog(lvl, "%s: %s - path offline", \
pp->mpp->alias, pp->dev); \
else if (checker_selected(&pp->checker)) { \
const char *__m = \
checker_message(&pp->checker); \
\
if (strlen(__m)) \
condlog(lvl, "%s: %s - %s checker%s", \
pp->mpp->alias, \
pp->dev, \
checker_name(&pp->checker), \
__m); \
} \
} \
} while(0)
struct mpath_event_param
{
char * devname;
struct multipath *mpp;
};
int uxsock_timeout;
static int verbosity;
static int bindings_read_only;
int ignore_new_devs;
#ifdef NO_DMEVENTS_POLL
static int poll_dmevents = 0;
#else
static int poll_dmevents = 1;
#endif
/* Don't access this variable without holding config_lock */
static enum daemon_status running_state = DAEMON_INIT;
/* Don't access this variable without holding config_lock */
static bool delayed_reconfig;
/* Don't access this variable without holding config_lock */
static enum force_reload_types reconfigure_pending = FORCE_RELOAD_NONE;
pid_t daemon_pid;
static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t config_cond;
static pthread_t check_thr, purge_thr, uevent_thr, uxlsnr_thr, uevq_thr,
dmevent_thr, fpin_thr, fpin_consumer_thr;
static bool check_thr_started, purge_thr_started, uevent_thr_started,
uxlsnr_thr_started, uevq_thr_started, dmevent_thr_started,
fpin_thr_started, fpin_consumer_thr_started;
static int pid_fd = -1;
static inline enum daemon_status get_running_state(bool *pending_reconfig)
{
enum daemon_status st;
pthread_mutex_lock(&config_lock);
st = running_state;
if (pending_reconfig != NULL)
*pending_reconfig = (reconfigure_pending != FORCE_RELOAD_NONE);
pthread_mutex_unlock(&config_lock);
return st;
}
int should_exit(void)
{
return get_running_state(NULL) == DAEMON_SHUTDOWN;
}
/*
* global copy of vecs for use in sig handlers
*/
static struct vectors * gvecs;
struct config *multipath_conf;
/* Local variables */
static volatile sig_atomic_t exit_sig;
static volatile sig_atomic_t reconfig_sig;
static volatile sig_atomic_t log_reset_sig;
static const char *daemon_status_msg[DAEMON_STATUS_SIZE] = {
[DAEMON_INIT] = "init",
[DAEMON_START] = "startup",
[DAEMON_CONFIGURE] = "configure",
[DAEMON_IDLE] = "idle",
[DAEMON_RUNNING] = "running",
[DAEMON_SHUTDOWN] = "shutdown",
};
const char *
daemon_status(bool *pending_reconfig)
{
int status = get_running_state(pending_reconfig);
if (status < DAEMON_INIT || status >= DAEMON_STATUS_SIZE)
return NULL;
return daemon_status_msg[status];
}
/*
* I love you too, systemd ...
*/
#ifdef USE_SYSTEMD
static void do_sd_notify(enum daemon_status old_state,
enum daemon_status new_state)
{
char notify_msg[MSG_SIZE];
const char *msg;
static bool startup_done = false;
/*
* Checkerloop switches back and forth between idle and running state.
* No need to tell systemd each time.
* These notifications cause a lot of overhead on dbus.
*/
if ((new_state == DAEMON_IDLE || new_state == DAEMON_RUNNING) &&
(old_state == DAEMON_IDLE || old_state == DAEMON_RUNNING))
return;
if (new_state == DAEMON_IDLE || new_state == DAEMON_RUNNING)
msg = "up";
else
msg = daemon_status_msg[new_state];
if (msg && !safe_sprintf(notify_msg, "STATUS=%s", msg))
sd_notify(0, notify_msg);
if (new_state == DAEMON_SHUTDOWN) {
/* Tell systemd that we're not RELOADING any more */
if (old_state == DAEMON_CONFIGURE && startup_done)
sd_notify(0, "READY=1");
sd_notify(0, "STOPPING=1");
} else if (new_state == DAEMON_IDLE && old_state == DAEMON_CONFIGURE) {
sd_notify(0, "READY=1");
startup_done = true;
} else if (new_state == DAEMON_CONFIGURE && startup_done)
sd_notify(0, "RELOADING=1");
}
#else
static void do_sd_notify(__attribute__((unused)) enum daemon_status old_state,
__attribute__((unused)) enum daemon_status new_state)
{}
#endif
static void config_cleanup(__attribute__((unused)) void *arg)
{
pthread_mutex_unlock(&config_lock);
}
#define wait_for_state_change__(condition, ms) \
({ \
struct timespec tmo; \
int rc = 0; \
\
if (condition) { \
get_monotonic_time(&tmo); \
tmo.tv_nsec += (ms) * 1000 * 1000; \
normalize_timespec(&tmo); \
do \
rc = pthread_cond_timedwait( \
&config_cond, &config_lock, &tmo); \
while (rc == 0 && (condition)); \
} \
rc; \
})
/*
* If the current status is @oldstate, wait for at most @ms milliseconds
* for the state to change, and return the new state, which may still be
* @oldstate.
*/
enum daemon_status wait_for_state_change_if(enum daemon_status oldstate,
unsigned long ms)
{
enum daemon_status st;
if (oldstate == DAEMON_SHUTDOWN)
return DAEMON_SHUTDOWN;
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
wait_for_state_change__(running_state == oldstate, ms);
st = running_state;
pthread_cleanup_pop(1);
return st;
}
/* must be called with config_lock held */
static void post_config_state__(enum daemon_status state)
{
if (state != running_state && running_state != DAEMON_SHUTDOWN) {
enum daemon_status old_state = running_state;
running_state = state;
pthread_cond_broadcast(&config_cond);
do_sd_notify(old_state, state);
condlog(4, "daemon state %s -> %s",
daemon_status_msg[old_state], daemon_status_msg[state]);
}
}
void post_config_state(enum daemon_status state)
{
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
post_config_state__(state);
pthread_cleanup_pop(1);
}
static bool unblock_reconfigure(void)
{
bool was_delayed;
pthread_mutex_lock(&config_lock);
was_delayed = delayed_reconfig;
if (was_delayed) {
delayed_reconfig = false;
/*
* In IDLE state, make sure child() is woken up
* Otherwise it will wake up when state switches to IDLE
*/
if (running_state == DAEMON_IDLE)
post_config_state__(DAEMON_CONFIGURE);
}
pthread_mutex_unlock(&config_lock);
if (was_delayed)
condlog(3, "unblocked delayed reconfigure");
return was_delayed;
}
/*
* Make sure child() is woken up when a map is removed that multipathd
* is currently waiting for.
* Overrides libmultipath's weak symbol by the same name
*/
void remove_map_callback(struct multipath *mpp)
{
if (mpp->wait_for_udev != UDEV_WAIT_DONE)
unblock_reconfigure();
}
void schedule_reconfigure(enum force_reload_types requested_type)
{
pthread_mutex_lock(&config_lock);
pthread_cleanup_push(config_cleanup, NULL);
enum force_reload_types type;
type = (reconfigure_pending == FORCE_RELOAD_YES ||
requested_type == FORCE_RELOAD_YES) ?
FORCE_RELOAD_YES : FORCE_RELOAD_WEAK;
switch (running_state)
{
case DAEMON_SHUTDOWN:
break;
case DAEMON_IDLE:
reconfigure_pending = type;
post_config_state__(DAEMON_CONFIGURE);
break;
case DAEMON_CONFIGURE:
case DAEMON_RUNNING:
reconfigure_pending = type;
break;
default:
break;
}
pthread_cleanup_pop(1);
}
static enum daemon_status set_config_state(enum daemon_status state)
{
int rc = 0;
enum daemon_status st;
pthread_cleanup_push(config_cleanup, NULL);
pthread_mutex_lock(&config_lock);
while (rc == 0 &&
running_state != state &&
running_state != DAEMON_SHUTDOWN &&
running_state != DAEMON_IDLE) {
rc = pthread_cond_wait(&config_cond, &config_lock);
}
if (rc == 0 && running_state == DAEMON_IDLE && state != DAEMON_IDLE)
post_config_state__(state);
st = running_state;
pthread_cleanup_pop(1);
return st;
}
struct config *get_multipath_config(void)
{
rcu_read_lock();
return rcu_dereference(multipath_conf);
}
void put_multipath_config(__attribute__((unused)) void *arg)
{
rcu_read_unlock();
}
/*
* The path group orderings that this function finds acceptable are different
* from now select_path_group determines the best pathgroup. The idea here is
* to only trigger a kernel reload when it is obvious that the pathgroups would
* be out of order, even if all the paths were usable. Thus pathgroups with
* PRIO_UNDEF are skipped, and the number of enabled paths doesn't matter here.
*/
bool path_groups_in_order(struct multipath *mpp)
{
int i;
struct pathgroup *pgp;
bool seen_marginal_pg = false;
int last_prio = INT_MAX;
if (VECTOR_SIZE(mpp->pg) < 2)
return true;
vector_foreach_slot(mpp->pg, pgp, i) {
if (seen_marginal_pg && !pgp->marginal)
return false;
/* skip pgs with PRIO_UNDEF, since this is likely temporary */
if (!pgp->paths || pgp->priority == PRIO_UNDEF)
continue;
if (pgp->marginal && !seen_marginal_pg) {
seen_marginal_pg = true;
last_prio = pgp->priority;
continue;
}
if (pgp->priority > last_prio)
return false;
last_prio = pgp->priority;
}
return true;
}
static int
need_switch_pathgroup (struct multipath * mpp, bool *need_reload)
{
int bestpg;
*need_reload = false;
if (!mpp)
return 0;
if (VECTOR_SIZE(mpp->pg) < 2)
return 0;
bestpg = select_path_group(mpp);
if (mpp->pgfailback == -FAILBACK_MANUAL)
return 0;
mpp->bestpg = bestpg;
*need_reload = !path_groups_in_order(mpp);
return (*need_reload || mpp->bestpg != mpp->nextpg);
}
static void
switch_pathgroup (struct multipath * mpp)
{
mpp->stat_switchgroup++;
dm_switchgroup(mpp->alias, mpp->bestpg);
condlog(2, "%s: switch to path group #%i",
mpp->alias, mpp->bestpg);
}
static int
wait_for_events(struct multipath *mpp, struct vectors *vecs)
{
if (poll_dmevents)
return watch_dmevents(mpp->alias);
else
return start_waiter_thread(mpp, vecs);
}
static void
remove_map_and_stop_waiter(struct multipath *mpp, struct vectors *vecs)
{
/* devices are automatically removed by the dmevent polling code,
* so they don't need to be manually removed here */
condlog(3, "%s: removing map from internal tables", mpp->alias);
if (!poll_dmevents)
stop_waiter_thread(mpp);
remove_map_from_mpvec(mpp, vecs->mpvec);
remove_map(mpp, vecs->pathvec);
}
static void
remove_maps_and_stop_waiters(struct vectors *vecs)
{
int i;
struct multipath * mpp;
if (!vecs)
return;
if (!poll_dmevents) {
vector_foreach_slot(vecs->mpvec, mpp, i)
stop_waiter_thread(mpp);
}
else
unwatch_all_dmevents();
remove_maps(vecs);
}
/* This function may free paths. See check_removed_paths(). */
int refresh_multipath(struct vectors *vecs, struct multipath *mpp)
{
if (update_multipath_strings(mpp, vecs->pathvec) != DMP_OK) {
condlog(0, "%s: failed to read map from kernel", mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return 1;
}
return 0;
}
/* This function may free paths. See check_removed_paths(). */
int setup_multipath(struct vectors *vecs, struct multipath *mpp)
{
if (refresh_multipath(vecs, mpp) != 0)
return 1;
set_no_path_retry(mpp);
if (VECTOR_SIZE(mpp->paths) != 0)
dm_cancel_deferred_remove(mpp);
return 0;
}
int update_multipath (struct vectors *vecs, char *mapname)
{
struct multipath *mpp;
struct pathgroup *pgp;
struct path *pp;
int i, j;
mpp = find_mp_by_alias(vecs->mpvec, mapname);
if (!mpp) {
condlog(3, "%s: multipath map not found", mapname);
return 2;
}
if (setup_multipath(vecs, mpp))
return 1; /* mpp freed in setup_multipath */
/*
* compare checkers states with DM states
*/
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (pp->dmstate != PSTATE_FAILED)
continue;
if (pp->state != PATH_DOWN) {
struct config *conf;
int oldstate = pp->state;
unsigned int checkint;
conf = get_multipath_config();
checkint = conf->checkint;
put_multipath_config(conf);
condlog(2, "%s: mark as failed", pp->dev);
mpp->stat_path_failures++;
pp->state = PATH_DOWN;
if (oldstate == PATH_UP ||
oldstate == PATH_GHOST)
update_queue_mode_del_path(mpp);
/*
* if opportune,
* schedule the next check earlier
*/
if (pp->tick > checkint)
pp->tick = checkint;
}
}
}
return 0;
}
static bool
flush_map_nopaths(struct multipath *mpp, struct vectors *vecs) {
int r;
bool is_queueing = true;
if (mpp->features)
is_queueing = strstr(mpp->features, "queue_if_no_path");
/* It's not safe to do a remove of a map that has "queue_if_no_path"
* set, since there could be outstanding IO which will cause
* multipathd to hang while attempting the remove */
if (mpp->flush_on_last_del == FLUSH_NEVER && is_queueing) {
condlog(2, "%s: map is queueing, can't remove", mpp->alias);
return false;
}
if (mpp->flush_on_last_del == FLUSH_UNUSED &&
mpath_in_use(mpp->alias) && is_queueing) {
condlog(2, "%s: map in use and queueing, can't remove",
mpp->alias);
return false;
}
/*
* This will flush FLUSH_NEVER devices and FLUSH_UNUSED devices
* that are in use, but only if they are already marked as not
* queueing. That is just to make absolutely certain that they
* really are not queueing, like they claim.
*/
condlog(is_queueing ? 2 : 3, "%s Last path deleted, disabling queueing",
mpp->alias);
mpp->retry_tick = 0;
mpp->no_path_retry = NO_PATH_RETRY_FAIL;
mpp->disable_queueing = 1;
mpp->stat_map_failures++;
if (dm_queue_if_no_path(mpp, 0) != 0) {
condlog(0, "%s: failed to disable queueing. Not removing",
mpp->alias);
return false;
}
r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
if (r != DM_FLUSH_OK) {
if (r == DM_FLUSH_DEFERRED) {
condlog(2, "%s: devmap deferred remove", mpp->alias);
mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
}
else
condlog(0, "%s: can't flush", mpp->alias);
return false;
}
condlog(2, "%s: map flushed after removing all paths", mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return true;
}
/*
* If reg_paths in non-NULL, it is a vector of paths that libmpathpersist
* registered. If the number of registered keys is smaller than the number
* of registered paths, then likely a preempt that occurred while
* libmpathpersist was registering the key. As long as there are still some
* registered keys, treat the preempt as happening first, and make sure to
* register keys on all the paths. If the number of registered keys is at
* least as large as the number of registered paths, then no preempt happened,
* and multipathd does not need to re-register the paths that libmpathpersist
* handled
*/
void pr_register_active_paths(struct multipath *mpp, const struct vector_s *reg_paths)
{
unsigned int i, j, k, nr_keys = 0;
unsigned int wanted_nr = VECTOR_SIZE(reg_paths);
struct path *pp;
struct pathgroup *pgp;
char *pathname;
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (mpp->prflag == PR_UNSET)
return;
if (pp->state != PATH_UP && pp->state != PATH_GHOST)
continue;
if (wanted_nr && nr_keys) {
vector_foreach_slot (reg_paths, pathname, k) {
if (strcmp(pp->dev_t, pathname) == 0) {
goto skip;
}
}
}
nr_keys = mpath_pr_event_handle(pp, nr_keys, wanted_nr);
if (nr_keys && nr_keys < wanted_nr) {
/*
* Incorrect number of registered keys. Need
* to register all devices
*/
wanted_nr = 0;
}
skip:; /* a statement must follow a label on pre C23 clang */
}
}
}
static void
save_offline_paths(const struct multipath *mpp, vector offline_paths)
{
unsigned int i, j;
struct path *pp;
struct pathgroup *pgp;
vector_foreach_slot (mpp->pg, pgp, i)
vector_foreach_slot (pgp->paths, pp, j)
if (pp->initialized == INIT_OK &&
pp->sysfs_state == PATH_DOWN)
/* ignore failures storing the paths. */
store_path(offline_paths, pp);
}
static void
handle_orphaned_offline_paths(vector offline_paths)
{
unsigned int i;
struct path *pp;
vector_foreach_slot (offline_paths, pp, i)
if (pp->mpp == NULL)
pp->add_when_online = true;
}
void cleanup_reset_vec(struct vector_s **v)
{
vector_reset(*v);
}
static int
update_map (struct multipath *mpp, struct vectors *vecs, int new_map)
{
int retries = 3;
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
struct vector_s offline_paths_vec = { .allocated = 0 };
vector offline_paths __attribute__((cleanup(cleanup_reset_vec))) = &offline_paths_vec;
retry:
condlog(4, "%s: updating new map", mpp->alias);
if (adopt_paths(vecs->pathvec, mpp, NULL)) {
condlog(0, "%s: failed to adopt paths for new map update",
mpp->alias);
retries = -1;
goto fail;
}
verify_paths(mpp);
if (VECTOR_SIZE(mpp->paths) == 0 &&
flush_map_nopaths(mpp, vecs))
return 1;
mpp->action = ACT_RELOAD;
if (setup_map(mpp, ¶ms, vecs)) {
condlog(0, "%s: failed to setup new map in update", mpp->alias);
retries = -1;
goto fail;
}
if (domap(mpp, params, 1) == DOMAP_FAIL && retries-- > 0) {
condlog(0, "%s: map_udate sleep", mpp->alias);
free(params);
params = NULL;
sleep(1);
goto retry;
}
fail:
if (new_map && wait_for_events(mpp, vecs)) {
condlog(0, "%s: failed to create new map", mpp->alias);
remove_map_from_mpvec(mpp, vecs->mpvec);
remove_map(mpp, vecs->pathvec);
return 1;
}
if (new_map && retries < 0)
save_offline_paths(mpp, offline_paths);
if (setup_multipath(vecs, mpp))
return 1;
sync_map_state(mpp, false);
pr_register_active_paths(mpp, NULL);
if (VECTOR_SIZE(offline_paths) != 0)
handle_orphaned_offline_paths(offline_paths);
if (retries < 0)
condlog(0, "%s: failed reload in new map update", mpp->alias);
return 0;
}
static int add_map_without_path (struct vectors *vecs, const char *alias)
{
struct multipath __attribute__((cleanup(cleanup_multipath))) *mpp =
alloc_multipath();
char __attribute__((cleanup(cleanup_charp))) *params = NULL;
char __attribute__((cleanup(cleanup_charp))) *status = NULL;
struct config *conf;
char uuid[DM_UUID_LEN];
int rc = DMP_ERR;
if (!mpp || !(mpp->alias = strdup(alias)))
return DMP_ERR;
if ((rc = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY | MAPINFO_CHECK_UUID,
(mapid_t) { .str = mpp->alias },
(mapinfo_t) {
.uuid = uuid,
.dmi = &mpp->dmi,
.size = &mpp->size,
.target = ¶ms,
.status = &status,
})) != DMP_OK)
return rc;
strlcpy(mpp->wwid, uuid + UUID_PREFIX_LEN, sizeof(mpp->wwid));
if (!strlen(mpp->wwid))
condlog(1, "%s: adding map with empty WWID", mpp->alias);
conf = get_multipath_config();
mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
put_multipath_config(conf);
if (update_multipath_table__(mpp, vecs->pathvec, 0, params, status) != DMP_OK ||
!vector_alloc_slot(vecs->mpvec))
return DMP_ERR;
/* Make sure mpp is not cleaned up on return */
vector_set_slot(vecs->mpvec, steal_ptr(mpp));
/*
* We can't pass mpp here, steal_ptr() has just nullified it.
* vector_set_slot() just set the last slot, use that.
*/
if (update_map(VECTOR_LAST_SLOT(vecs->mpvec), vecs, 1) != 0) /* map removed */
return DMP_ERR;
return DMP_OK;
}
static int
coalesce_maps(struct vectors *vecs, vector nmpv)
{
struct multipath * ompp;
vector ompv = vecs->mpvec;
int i, reassign_maps;
struct config *conf;
conf = get_multipath_config();
reassign_maps = conf->reassign_maps;
put_multipath_config(conf);
vector_foreach_slot (ompv, ompp, i) {
condlog(3, "%s: coalesce map", ompp->alias);
if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
struct pathgroup *pgp;
struct path *pp;
int j, k;
/*
* set pp->mpp for all the old map's paths,
* so that they can be properly removed
*/
vector_foreach_slot (ompp->pg, pgp, j)
vector_foreach_slot (pgp->paths, pp, k)
if (!pp->mpp)
pp->mpp = ompp;
/*
* remove all current maps not allowed by the
* current configuration
*/
ompp->retry_tick = 0;
ompp->no_path_retry = NO_PATH_RETRY_FAIL;
ompp->disable_queueing = 1;
dm_queue_if_no_path(ompp, 0);
if (dm_flush_map(ompp->alias) != DM_FLUSH_OK) {
condlog(0, "%s: unable to flush devmap",
ompp->alias);
/*
* may be just because the device is open
*/
if (setup_multipath(vecs, ompp) != 0) {
i--;
continue;
}
if (!vector_alloc_slot(nmpv))
return 1;
vector_set_slot(nmpv, ompp);
vector_del_slot(ompv, i);
i--;
}
else {
condlog(2, "%s: multipath map removed", ompp->alias);
trigger_paths_udev_change(ompp, false);
}
} else if (reassign_maps) {
condlog(3, "%s: Reassign existing device-mapper"
" devices", ompp->alias);
dm_reassign(ompp->alias);
}
}
return 0;
}
static void
sync_maps_state(vector mpvec)
{
unsigned int i;
struct multipath *mpp;
vector_foreach_slot (mpvec, mpp, i)
sync_map_state(mpp, false);
}
int
flush_map(struct multipath * mpp, struct vectors * vecs)
{
int r = dm_suspend_and_flush_map(mpp->alias, 0);
if (r != DM_FLUSH_OK) {
if (r == DM_FLUSH_FAIL_CANT_RESTORE)
remove_feature(&mpp->features, "queue_if_no_path");
condlog(0, "%s: can't flush", mpp->alias);
return r;
}
condlog(2, "%s: map flushed", mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return 0;
}
static int
uev_add_map (struct uevent * uev, struct vectors * vecs)
{
char *alias;
int major = -1, minor = -1, rc;
condlog(3, "%s: add map (uevent)", uev->kernel);
alias = uevent_get_dm_name(uev);
if (!alias) {
condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
major = uevent_get_major(uev);
minor = uevent_get_minor(uev);
alias = dm_mapname(major, minor);
if (!alias) {
condlog(2, "%s: mapname not found for %d:%d",
uev->kernel, major, minor);
return 1;
}
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
rc = ev_add_map(uev->kernel, alias, vecs);
lock_cleanup_pop(vecs->lock);
free(alias);
return rc;
}
/*
* ev_add_map expects that the multipath device already exists in kernel
* before it is called. It just adds a device to multipathd or updates an
* existing device.
*/
int
ev_add_map (char * dev, const char * alias, struct vectors * vecs)
{
struct multipath * mpp;
int reassign_maps, rc;
struct config *conf;
mpp = find_mp_by_alias(vecs->mpvec, alias);
if (mpp) {
if (mpp->wait_for_udev == UDEV_WAIT_RELOAD) {
condlog(2, "%s: performing delayed actions",
mpp->alias);
if (update_map(mpp, vecs, 0))
/* setup multipathd removed the map */
return 1;
}
conf = get_multipath_config();
reassign_maps = conf->reassign_maps;
put_multipath_config(conf);
dm_get_info(mpp->alias, &mpp->dmi);
if (mpp->wait_for_udev != UDEV_WAIT_DONE) {
mpp->wait_for_udev = UDEV_WAIT_DONE;
if (!need_to_delay_reconfig(vecs) &&
unblock_reconfigure())
return 0;
}
/*
* Not really an error -- we generate our own uevent
* if we create a multipath mapped device as a result
* of uev_add_path
*/
if (reassign_maps) {
condlog(3, "%s: Reassign existing device-mapper devices",
alias);
dm_reassign(alias);
}
return 0;
}
condlog(2, "%s: adding map", alias);
/*
* now we can register the map
*/
if ((rc = add_map_without_path(vecs, alias)) == DMP_OK) {
condlog(2, "%s: devmap %s registered", alias, dev);
return 0;
} else if (rc == DMP_NO_MATCH) {
condlog(4, "%s: not a multipath map", alias);
return 0;
} else {
condlog(2, "%s: ev_add_map failed", dev);
return 1;
}
}
static int
uev_remove_map (struct uevent * uev, struct vectors * vecs)
{
char *alias;
int minor;
struct multipath *mpp;
condlog(3, "%s: remove map (uevent)", uev->kernel);
alias = uevent_get_dm_name(uev);
if (!alias) {
condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
return 0;
}
minor = uevent_get_minor(uev);
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
mpp = find_mp_by_minor(vecs->mpvec, minor);
if (!mpp) {
condlog(2, "%s: devmap not registered, can't remove",
uev->kernel);
goto out;
}
if (strcmp(mpp->alias, alias)) {
condlog(2, "%s: map alias mismatch: have \"%s\", got \"%s\")",
uev->kernel, mpp->alias, alias);
goto out;
}
dm_queue_if_no_path(mpp, 0);
remove_map_and_stop_waiter(mpp, vecs);
out:
lock_cleanup_pop(vecs->lock);
free(alias);
return 0;
}
static void
rescan_path(struct udev_device *ud)
{
ud = udev_device_get_parent_with_subsystem_devtype(ud, "scsi",
"scsi_device");
if (ud) {
ssize_t ret =
sysfs_attr_set_value(ud, "rescan", "1", strlen("1"));
if (ret != strlen("1"))
log_sysfs_attr_set_value(1, ret,
"%s: failed to trigger rescan",
udev_device_get_syspath(ud));
}
}
/* Returns true if the path was removed */
bool
handle_path_wwid_change(struct path *pp, struct vectors *vecs)
{
struct udev_device *udd;
static const char add[] = "add";
ssize_t ret;
char dev[FILE_NAME_SIZE];
bool removed = false;
if (!pp || !pp->udev)
return removed;
strlcpy(dev, pp->dev, sizeof(dev));
udd = udev_device_ref(pp->udev);
if (ev_remove_path(pp, vecs, 1) & REMOVE_PATH_SUCCESS) {
removed = true;
} else if (pp->mpp) {
pp->dmstate = PSTATE_FAILED;
dm_fail_path(pp->mpp->alias, pp->dev_t);
}
rescan_path(udd);
ret = sysfs_attr_set_value(udd, "uevent", add, sizeof(add) - 1);
udev_device_unref(udd);
if (ret != sizeof(add) - 1)
log_sysfs_attr_set_value(1, ret,
"%s: failed to trigger add event", dev);
return removed;
}
bool
check_path_wwid_change(struct path *pp)
{
char wwid[WWID_SIZE];
int len = 0;
size_t i;
if (!strlen(pp->wwid))
return false;
/* Get the real fresh device wwid by sgio. sysfs still has old
* data, so only get_vpd_sgio will work to get the new wwid */
len = get_vpd_sgio(pp->fd, 0x83, 0, wwid, WWID_SIZE);
if (len <= 0) {
condlog(2, "%s: failed to check wwid by sgio: len = %d",
pp->dev, len);
return false;
}
/*Strip any trailing blanks */
for (i = strlen(wwid); i > 0 && wwid[i-1] == ' '; i--);
/* no-op */
wwid[i] = '\0';
condlog(4, "%s: Got wwid %s by sgio", pp->dev, wwid);
if (strncmp(wwid, pp->wwid, WWID_SIZE)) {
condlog(0, "%s: wwid '%s' doesn't match wwid '%s' from device",
pp->dev, pp->wwid, wwid);
return true;
}
return false;
}
/*
* uev_add_path can call uev_update_path, and uev_update_path can call
* uev_add_path
*/
static int uev_update_path (struct uevent *uev, struct vectors * vecs);
static int
uev_add_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
{
struct path *pp;
int ret = 0, i;
struct config *conf;
bool partial_init = false;
condlog(3, "%s: add path (uevent)", uev->kernel);
if (strstr(uev->kernel, "..") != NULL) {
/*
* Don't allow relative device names in the pathvec
*/
condlog(0, "%s: path name is invalid", uev->kernel);
return 1;
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
pp = find_path_by_dev(vecs->pathvec, uev->kernel);
if (pp) {
int r;
struct multipath *prev_mpp = NULL;
if (pp->initialized == INIT_PARTIAL) {
partial_init = true;
goto out;
} else if (pp->initialized == INIT_REMOVED) {
condlog(3, "%s: re-adding removed path", pp->dev);
pp->initialized = INIT_NEW;
prev_mpp = pp->mpp;
if (prev_mpp == NULL)
condlog(0, "Bug: %s was in INIT_REMOVED state without being a multipath member",
pp->dev);
pp->mpp = NULL;
/* make sure get_uid() is called */
pp->wwid[0] = '\0';
} else
condlog(3,
"%s: spurious uevent, path already in pathvec",
uev->kernel);
if (!pp->mpp && !strlen(pp->wwid)) {
condlog(3, "%s: reinitialize path", uev->kernel);
udev_device_unref(pp->udev);
pp->udev = udev_device_ref(uev->udev);
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
r = pathinfo(pp, conf,
DI_ALL | DI_BLACKLIST);
pthread_cleanup_pop(1);
if (r == PATHINFO_OK && !prev_mpp)
ret = ev_add_path(pp, vecs, need_do_map);
else if (r == PATHINFO_OK &&
!strncmp(pp->wwid, prev_mpp->wwid, WWID_SIZE)) {
/*
* Path was unsuccessfully removed, but now
* re-added, and still belongs to the right map
* - all fine, reinstate asap
*/
pp->mpp = prev_mpp;
pp->tick = 1;
ret = 0;
} else if (prev_mpp) {
/*
* Bad: re-added path still hangs in wrong map
* Make another attempt to remove the path
*/
pp->mpp = prev_mpp;
if (!(ev_remove_path(pp, vecs, true) &
REMOVE_PATH_SUCCESS)) {
/*
* Failure in ev_remove_path will keep
* path in pathvec in INIT_REMOVED state
* Fail the path to make sure it isn't
* used anymore.
*/
pp->dmstate = PSTATE_FAILED;
dm_fail_path(pp->mpp->alias, pp->dev_t);
condlog(1, "%s: failed to re-add path still mapped in %s",
pp->dev, pp->mpp->alias);
ret = 1;
} else if (r == PATHINFO_OK)
/*
* Path successfully freed, move on to
* "new path" code path below
*/
pp = NULL;
} else if (r == PATHINFO_SKIPPED) {
condlog(3, "%s: remove blacklisted path",
uev->kernel);
i = find_slot(vecs->pathvec, (void *)pp);
if (i != -1)
vector_del_slot(vecs->pathvec, i);
free_path(pp);
} else {
condlog(0, "%s: failed to reinitialize path",
uev->kernel);
ret = 1;
}
}
}
if (pp)
goto out;
/*
* get path vital state
*/
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
ret = alloc_path_with_pathinfo(conf, uev->udev,
uev->wwid, DI_ALL, &pp);
pthread_cleanup_pop(1);
if (!pp) {
if (ret == PATHINFO_SKIPPED)
ret = 0;
else {
condlog(3, "%s: failed to get path info", uev->kernel);
ret = 1;
}
goto out;
}
ret = store_path(vecs->pathvec, pp);
if (!ret) {
conf = get_multipath_config();
pp->checkint = conf->checkint;
put_multipath_config(conf);
ret = ev_add_path(pp, vecs, need_do_map);
} else {
condlog(0, "%s: failed to store path info, "
"dropping event",
uev->kernel);
free_path(pp);
ret = 1;
}
out:
lock_cleanup_pop(vecs->lock);
if (partial_init)
return uev_update_path(uev, vecs);
return ret;
}
static int
sysfs_get_ro (struct path *pp)
{
int ro;
char buff[3]; /* Either "0\n\0" or "1\n\0" */
if (!pp->udev)
return -1;
if (!sysfs_attr_get_value_ok(pp->udev, "ro", buff, sizeof(buff))) {
condlog(3, "%s: Cannot read ro attribute in sysfs", pp->dev);
return -1;
}
if (sscanf(buff, "%d\n", &ro) != 1 || ro < 0 || ro > 1) {
condlog(3, "%s: Cannot parse ro attribute", pp->dev);
return -1;
}
return ro;
}
/*
* returns:
* 0: added
* 1: error
*/
int
ev_add_path (struct path * pp, struct vectors * vecs, int need_do_map)
{
struct multipath * mpp;
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
int retries = 3;
int start_waiter = 0;
int ret;
int ro;
unsigned char prflag = PR_UNSET;
/*
* need path UID to go any further
*/
if (strlen(pp->wwid) == 0) {
condlog(0, "%s: failed to get path uid", pp->dev);
goto fail; /* leave path added to pathvec */
}
mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
if (mpp && pp->size && mpp->size != pp->size) {
condlog(0, "%s: failed to add new path %s, device size mismatch", mpp->alias, pp->dev);
int i = find_slot(vecs->pathvec, (void *)pp);
if (i != -1)
vector_del_slot(vecs->pathvec, i);
free_path(pp);
return 1;
}
if (mpp)
trigger_path_udev_change(pp, true);
if (mpp && mpp->wait_for_udev != UDEV_WAIT_DONE &&
(pathcount(mpp, PATH_UP) > 0 ||
(pathcount(mpp, PATH_GHOST) > 0 &&
path_get_tpgs(pp) != TPGS_IMPLICIT &&
mpp->ghost_delay_tick <= 0))) {
/* if wait_for_udev is set and valid paths exist */
condlog(3, "%s: delaying path addition until %s is fully initialized",
pp->dev, mpp->alias);
mpp->wait_for_udev = UDEV_WAIT_RELOAD;
orphan_path(pp, "waiting for create to complete");
return 0;
}
pp->mpp = mpp;
rescan:
if (mpp) {
condlog(4,"%s: adopting all paths for path %s",
mpp->alias, pp->dev);
if (adopt_paths(vecs->pathvec, mpp, NULL) || pp->mpp != mpp ||
find_slot(mpp->paths, pp) == -1)
goto fail; /* leave path added to pathvec */
verify_paths(mpp);
mpp->action = ACT_RELOAD;
prflag = mpp->prflag;
mpath_pr_event_handle(pp, 0, 0);
} else {
if (!should_multipath(pp, vecs->pathvec, vecs->mpvec)) {
orphan_path(pp, "only one path");
return 0;
}
condlog(4,"%s: creating new map", pp->dev);
if ((mpp = add_map_with_path(vecs, pp, 1, NULL))) {
mpp->action = ACT_CREATE;
/*
* We don't depend on ACT_CREATE, as domap will
* set it to ACT_NOTHING when complete.
*/
start_waiter = 1;
}
else
goto fail; /* leave path added to pathvec */
}
/* ro check - if new path is ro, force map to be ro as well */
ro = sysfs_get_ro(pp);
if (ro == 1)
mpp->force_readonly = 1;
if (!need_do_map)
return 0;
if (!dm_map_present(mpp->alias)) {
mpp->action = ACT_CREATE;
start_waiter = 1;
}
/*
* push the map to the device-mapper
*/
if (setup_map(mpp, ¶ms, vecs)) {
condlog(0, "%s: failed to setup map for addition of new "
"path %s", mpp->alias, pp->dev);
goto fail_map;
}
/*
* reload the map for the multipath mapped device
*/
ret = domap(mpp, params, 1);
while (ret == DOMAP_RETRY && retries-- > 0) {
condlog(0, "%s: retry domap for addition of new "
"path %s", mpp->alias, pp->dev);
sleep(1);
ret = domap(mpp, params, 1);
}
if (ret == DOMAP_FAIL || ret == DOMAP_RETRY) {
condlog(0, "%s: failed in domap for addition of new "
"path %s", mpp->alias, pp->dev);
/*
* deal with asynchronous uevents :((
*/
if (mpp->action == ACT_RELOAD && retries-- > 0) {
condlog(0, "%s: ev_add_path sleep", mpp->alias);
sleep(1);
update_mpp_paths(mpp, vecs->pathvec);
free(params);
params = NULL;
goto rescan;
}
else if (mpp->action == ACT_RELOAD)
condlog(0, "%s: giving up reload", mpp->alias);
else
goto fail_map;
}
if ((mpp->action == ACT_CREATE ||
(mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
wait_for_events(mpp, vecs))
goto fail_map;
/*
* update our state from kernel regardless of create or reload
*/
if (setup_multipath(vecs, mpp))
goto fail; /* if setup_multipath fails, it removes the map */
sync_map_state(mpp, false);
if (retries >= 0) {
if ((mpp->prflag == PR_SET && prflag != PR_SET) || start_waiter)
pr_register_active_paths(mpp, NULL);
condlog(2, "%s [%s]: path added to devmap %s",
pp->dev, pp->dev_t, mpp->alias);
return 0;
} else
goto fail;
fail_map:
remove_map_from_mpvec(mpp, vecs->mpvec);
remove_map(mpp, vecs->pathvec);
fail:
orphan_path(pp, "failed to add path");
return 1;
}
static int
uev_remove_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
{
struct path *pp;
condlog(3, "%s: remove path (uevent)", uev->kernel);
delete_foreign(uev->udev);
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
pp = find_path_by_dev(vecs->pathvec, uev->kernel);
if (pp)
ev_remove_path(pp, vecs, need_do_map);
lock_cleanup_pop(vecs->lock);
if (!pp) /* Not an error; path might have been purged earlier */
condlog(0, "%s: path already removed", uev->kernel);
return 0;
}
int
ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
{
struct multipath * mpp;
int i, retval = REMOVE_PATH_SUCCESS;
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
/*
* avoid referring to the map of an orphaned path
*/
if ((mpp = pp->mpp)) {
char devt[BLK_DEV_SIZE];
/*
* Mark the path as removed. In case of success, we
* will delete it for good. Otherwise, it will be deleted
* later, unless all attempts to reload this map fail.
*/
set_path_removed(pp);
/*
* transform the mp->pg vector of vectors of paths
* into a mp->params string to feed the device-mapper
*/
if (update_mpp_paths(mpp, vecs->pathvec)) {
condlog(0, "%s: failed to update paths",
mpp->alias);
goto fail;
}
/*
* we have to explicitly remove pp from mpp->paths,
* update_mpp_paths() doesn't do that.
*/
i = find_slot(mpp->paths, pp);
if (i != -1)
vector_del_slot(mpp->paths, i);
/*
* remove the map IF removing the last path. If
* flush_map_nopaths succeeds, the path has been removed.
*/
if (VECTOR_SIZE(mpp->paths) == 0 &&
flush_map_nopaths(mpp, vecs))
goto out;
if (mpp->wait_for_udev != UDEV_WAIT_DONE) {
mpp->wait_for_udev = UDEV_WAIT_RELOAD;
retval = REMOVE_PATH_DELAY;
goto out;
}
if (!need_do_map) {
retval = REMOVE_PATH_DELAY;
goto out;
}
if (setup_map(mpp, ¶ms, vecs)) {
condlog(0, "%s: failed to setup map for"
" removal of path %s", mpp->alias, pp->dev);
goto fail;
}
/*
* reload the map
*/
mpp->action = ACT_RELOAD;
if (domap(mpp, params, 1) == DOMAP_FAIL) {
condlog(0, "%s: failed in domap for "
"removal of path %s",
mpp->alias, pp->dev);
retval = REMOVE_PATH_FAILURE;
}
/*
* update mpp state from kernel even if domap failed.
* If the path was removed from the mpp, setup_multipath will
* free the path regardless of whether it succeeds or fails
*/
strlcpy(devt, pp->dev_t, sizeof(devt));
if (setup_multipath(vecs, mpp))
return REMOVE_PATH_MAP_ERROR;
sync_map_state(mpp, false);
if (retval == REMOVE_PATH_SUCCESS)
condlog(2, "%s: path removed from map %s",
devt, mpp->alias);
} else {
/* mpp == NULL */
if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
vector_del_slot(vecs->pathvec, i);
free_path(pp);
}
out:
return retval;
fail:
condlog(0, "%s: error removing path. removing map %s", pp->dev,
mpp->alias);
remove_map_and_stop_waiter(mpp, vecs);
return REMOVE_PATH_MAP_ERROR;
}
int
finish_path_init(struct path *pp, struct vectors * vecs)
{
int r;
struct config *conf;
if (pp->udev && pp->uid_attribute && *pp->uid_attribute &&
!udev_device_get_is_initialized(pp->udev))
return 0;
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
r = pathinfo(pp, conf, DI_ALL|DI_BLACKLIST);
pthread_cleanup_pop(1);
if (r == PATHINFO_OK)
return 0;
condlog(0, "%s: error fully initializing path, removing", pp->dev);
ev_remove_path(pp, vecs, 1);
return -1;
}
static bool
needs_ro_update(struct multipath *mpp, int ro)
{
struct pathgroup * pgp;
struct path * pp;
unsigned int i, j;
if (!mpp || ro < 0)
return false;
if (!has_dm_info(mpp))
return true;
if (mpp->dmi.read_only == ro)
return false;
if (ro == 1)
return true;
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (sysfs_get_ro(pp) == 1)
return false;
}
}
return true;
}
int resize_map(struct multipath *mpp, unsigned long long size,
struct vectors * vecs)
{
int ret = 0;
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
unsigned long long orig_size = mpp->size;
mpp->size = size;
update_mpp_paths(mpp, vecs->pathvec);
if (setup_map(mpp, ¶ms, vecs) != 0) {
condlog(0, "%s: failed to setup map for resize : %s",
mpp->alias, strerror(errno));
mpp->size = orig_size;
ret = 1;
goto out;
}
mpp->action = ACT_RESIZE;
mpp->force_udev_reload = 1;
if (domap(mpp, params, 1) == DOMAP_FAIL) {
condlog(0, "%s: failed to resize map : %s", mpp->alias,
strerror(errno));
mpp->size = orig_size;
ret = 1;
}
out:
if (setup_multipath(vecs, mpp) != 0)
return 2;
sync_map_state(mpp, false);
return ret;
}
static int
uev_update_path (struct uevent *uev, struct vectors * vecs)
{
int ro, retval = 0, rc;
struct path * pp;
struct config *conf;
int needs_reinit = 0;
switch ((rc = change_foreign(uev->udev))) {
case FOREIGN_OK:
/* known foreign path, ignore event */
return 0;
case FOREIGN_IGNORED:
break;
case FOREIGN_ERR:
condlog(3, "%s: error in change_foreign", __func__);
break;
default:
condlog(1, "%s: return code %d of change_foreign is unsupported",
__func__, rc);
break;
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
pp = find_path_by_dev(vecs->pathvec, uev->kernel);
if (pp) {
struct multipath *mpp = pp->mpp;
char wwid[WWID_SIZE];
int auto_resize;
conf = get_multipath_config();
auto_resize = conf->auto_resize;
put_multipath_config(conf);
if (pp->initialized == INIT_REQUESTED_UDEV) {
needs_reinit = 1;
goto out;
}
/* Don't deal with other types of failed initialization
* now. check_path will handle it */
if (!strlen(pp->wwid) && pp->initialized != INIT_PARTIAL)
goto out;
strcpy(wwid, pp->wwid);
rc = get_uid(pp, pp->state, uev->udev, 0);
if (rc != 0)
strcpy(pp->wwid, wwid);
else if (strlen(wwid) &&
strncmp(wwid, pp->wwid, WWID_SIZE) != 0) {
condlog(0, "%s: path wwid changed from '%s' to '%s'",
uev->kernel, wwid, pp->wwid);
ev_remove_path(pp, vecs, 1);
needs_reinit = 1;
goto out;
} else if (pp->initialized == INIT_PARTIAL) {
udev_device_unref(pp->udev);
pp->udev = udev_device_ref(uev->udev);
if (finish_path_init(pp, vecs) < 0) {
retval = 1;
goto out;
}
} else {
udev_device_unref(pp->udev);
pp->udev = udev_device_ref(uev->udev);
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
condlog(1, "%s: pathinfo failed after change uevent",
uev->kernel);
pthread_cleanup_pop(1);
}
ro = uevent_get_disk_ro(uev);
if (needs_ro_update(mpp, ro)) {
condlog(2, "%s: update path write_protect to '%d' (uevent)", uev->kernel, ro);
if (mpp->wait_for_udev != UDEV_WAIT_DONE)
mpp->wait_for_udev = UDEV_WAIT_RELOAD;
else {
if (ro == 1)
pp->mpp->force_readonly = 1;
retval = reload_and_sync_map(mpp, vecs);
if (retval == 2)
condlog(2, "%s: map removed during reload", pp->dev);
else {
pp->mpp->force_readonly = 0;
condlog(2, "%s: map %s reloaded (retval %d)", uev->kernel, mpp->alias, retval);
}
}
}
if (auto_resize != AUTO_RESIZE_NEVER && mpp &&
mpp->wait_for_udev == UDEV_WAIT_DONE) {
struct pathgroup *pgp;
struct path *pp2;
unsigned int i, j;
unsigned long long orig_size = mpp->size;
if (!pp->size || pp->size == mpp->size ||
(pp->size < mpp->size &&
auto_resize == AUTO_RESIZE_GROW_ONLY))
goto out;
vector_foreach_slot(mpp->pg, pgp, i)
vector_foreach_slot (pgp->paths, pp2, j)
if (pp2->size && pp2->size != pp->size)
goto out;
retval = resize_map(mpp, pp->size, vecs);
if (retval == 2)
condlog(2, "%s: map removed during resize", pp->dev);
else if (retval == 0)
condlog(2, "%s: resized map from %llu to %llu",
mpp->alias, orig_size, pp->size);
}
}
out:
lock_cleanup_pop(vecs->lock);
if (!pp) {
/* If the path is blacklisted, print a debug/non-default verbosity message. */
if (uev->udev) {
int flag = DI_SYSFS | DI_WWID;
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
pthread_cleanup_pop(1);
if (retval == PATHINFO_SKIPPED) {
condlog(3, "%s: spurious uevent, path is blacklisted", uev->kernel);
return 0;
}
}
condlog(0, "%s: spurious uevent, path not found", uev->kernel);
}
/* pp->initialized must not be INIT_PARTIAL if needs_reinit is set */
if (needs_reinit)
retval = uev_add_path(uev, vecs, 1);
return retval;
}
static int
uev_pathfail_check(struct uevent *uev, struct vectors *vecs)
{
char *action = NULL, *devt = NULL;
struct path *pp;
int r = 1;
action = uevent_get_dm_action(uev);
if (!action)
return 1;
if (strncmp(action, "PATH_FAILED", 11))
goto out;
devt = uevent_get_dm_path(uev);
if (!devt) {
condlog(3, "%s: No DM_PATH in uevent", uev->kernel);
goto out;
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
pp = find_path_by_devt(vecs->pathvec, devt);
if (!pp)
goto out_lock;
r = io_err_stat_handle_pathfail(pp);
if (r)
condlog(3, "io_err_stat: %s: cannot handle pathfail uevent",
pp->dev);
out_lock:
lock_cleanup_pop(vecs->lock);
free(devt);
free(action);
return r;
out:
free(action);
return 1;
}
static int
map_discovery (struct vectors * vecs)
{
struct multipath * mpp;
int i;
if (dm_get_maps(vecs->mpvec))
return 1;
vector_foreach_slot (vecs->mpvec, mpp, i)
if (update_multipath_table(mpp, vecs->pathvec, DI_DISCOVERY) != DMP_OK) {
vector_del_slot(vecs->mpvec, i--);
remove_map(mpp, vecs->pathvec);
}
return 0;
}
int
uev_trigger (struct uevent * uev, void * trigger_data)
{
int r = 0;
struct vectors * vecs;
struct uevent *merge_uev, *tmp;
enum daemon_status state;
vecs = (struct vectors *)trigger_data;
pthread_cleanup_push(config_cleanup, NULL);
pthread_mutex_lock(&config_lock);
while (running_state != DAEMON_IDLE &&
running_state != DAEMON_RUNNING &&
running_state != DAEMON_SHUTDOWN)
pthread_cond_wait(&config_cond, &config_lock);
state = running_state;
pthread_cleanup_pop(1);
if (state == DAEMON_SHUTDOWN)
return 0;
/*
* device map event
* Add events are ignored here as the tables
* are not fully initialised then.
*/
if (!strncmp(uev->kernel, "dm-", 3)) {
if (!uevent_is_mpath(uev)) {
if (!strncmp(uev->action, "change", 6))
(void)add_foreign(uev->udev);
else if (!strncmp(uev->action, "remove", 6))
(void)delete_foreign(uev->udev);
goto out;
}
if (!strncmp(uev->action, "change", 6)) {
r = uev_add_map(uev, vecs);
/*
* the kernel-side dm-mpath issues a PATH_FAILED event
* when it encounters a path IO error. It is reason-
* able be the entry of path IO error accounting pro-
* cess.
*/
uev_pathfail_check(uev, vecs);
} else if (!strncmp(uev->action, "remove", 6)) {
r = uev_remove_map(uev, vecs);
}
goto out;
}
/*
* path add/remove/change event, add/remove maybe merged
*/
list_for_each_entry_safe(merge_uev, tmp, &uev->merge_node, node) {
if (!strncmp(merge_uev->action, "add", 3))
r += uev_add_path(merge_uev, vecs, 0);
if (!strncmp(merge_uev->action, "remove", 6))
r += uev_remove_path(merge_uev, vecs, 0);
}
if (!strncmp(uev->action, "add", 3))
r += uev_add_path(uev, vecs, 1);
if (!strncmp(uev->action, "remove", 6))
r += uev_remove_path(uev, vecs, 1);
if (!strncmp(uev->action, "change", 6))
r += uev_update_path(uev, vecs);
out:
return r;
}
static void rcu_unregister(__attribute__((unused)) void *param)
{
rcu_unregister_thread();
}
static void *
ueventloop (void * ap)
{
struct udev *udev = ap;
pthread_cleanup_push(rcu_unregister, NULL);
rcu_register_thread();
if (uevent_listen(udev))
condlog(0, "error starting uevent listener");
pthread_cleanup_pop(1);
return NULL;
}
static void *
uevqloop (void * ap)
{
pthread_cleanup_push(rcu_unregister, NULL);
rcu_register_thread();
if (uevent_dispatch(&uev_trigger, ap))
condlog(0, "error starting uevent dispatcher");
pthread_cleanup_pop(1);
return NULL;
}
#ifdef USE_SYSTEMD
static int get_systemd_sockets(long *ux_sock)
{
int num = sd_listen_fds(0);
if (num > 2) {
condlog(3, "sd_listen_fds returned %d fds", num);
return -1;
} else if (num == 2) {
ux_sock[0] = SD_LISTEN_FDS_START + 0;
ux_sock[1] = SD_LISTEN_FDS_START + 1;
condlog(3, "using fd %ld and %ld from sd_listen_fds", ux_sock[0], ux_sock[1]);
} else if (num == 1) {
ux_sock[0] = SD_LISTEN_FDS_START + 0;
condlog(3, "using fd %ld from sd_listen_fds", ux_sock[0]);
}
return num;
}
#else
static int get_systemd_sockets(long *ux_sock __attribute__((unused)))
{
return 0;
}
#endif
static void *
uxlsnrloop (void * ap)
{
long ux_sock[2] = {-1, -1};
int num;
const char *env_name = getenv("MULTIPATH_SOCKET_NAME");
pthread_cleanup_push(rcu_unregister, NULL);
rcu_register_thread();
num = get_systemd_sockets(ux_sock);
if (num < 1 && env_name != NULL) {
ux_sock[0] = ux_socket_listen(env_name);
num = 1;
}
if (num < 1) {
ux_sock[0] = ux_socket_listen(ABSTRACT_SOCKET);
ux_sock[1] = ux_socket_listen(PATHNAME_SOCKET);
num = 2;
}
if (ux_sock[0] == -1 && ux_sock[1] == -1) {
condlog(1, "could not create sockets: %d", errno);
exit_daemon();
goto out;
}
pthread_cleanup_push(uxsock_cleanup, (void *)ux_sock);
if (cli_init()) {
condlog(1, "Failed to init uxsock listener");
exit_daemon();
goto out_sock;
}
/* Tell main thread that thread has started */
post_config_state(DAEMON_CONFIGURE);
umask(077);
/*
* Wait for initial reconfiguration to finish, while
* handling signals
*/
while (wait_for_state_change_if(DAEMON_CONFIGURE, 50)
== DAEMON_CONFIGURE)
handle_signals(false);
uxsock_listen(num, ux_sock, ap);
out_sock:
pthread_cleanup_pop(1); /* uxsock_cleanup */
out:
pthread_cleanup_pop(1); /* rcu_unregister */
return NULL;
}
void
exit_daemon (void)
{
post_config_state(DAEMON_SHUTDOWN);
}
static void
fail_path (struct path * pp, int del_active)
{
if (!pp->mpp)
return;
condlog(2, "checker failed path %s in map %s",
pp->dev_t, pp->mpp->alias);
dm_fail_path(pp->mpp->alias, pp->dev_t);
if (del_active)
update_queue_mode_del_path(pp->mpp);
}
/*
* caller must have locked the path list before calling that function
*/
static void
reinstate_path (struct path * pp)
{
if (!pp->mpp)
return;
if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
condlog(0, "%s: reinstate failed", pp->dev_t);
else {
condlog(2, "%s: reinstated", pp->dev_t);
update_queue_mode_add_path(pp->mpp);
}
}
static void
enable_group(struct path * pp)
{
struct pathgroup * pgp;
/*
* if path is added through uev_add_path, pgindex can be unset.
* next update_strings() will set it, upon map reload event.
*
* we can safely return here, because upon map reload, all
* PG will be enabled.
*/
if (!pp->mpp->pg || !pp->pgindex)
return;
pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
if (pgp->status == PGSTATE_DISABLED) {
condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
dm_enablegroup(pp->mpp->alias, pp->pgindex);
}
}
/* This is called after a path has started working again. It the multipath
* device for this path uses the followover failback type, and this is the
* best pathgroup, and this is the first path in the pathgroup to come back
* up, then switch to this pathgroup */
static int
do_followover_should_failback(struct path * pp)
{
struct pathgroup * pgp;
struct path *pp1;
int i;
if (pp->pgindex != pp->mpp->bestpg)
return 0;
pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
vector_foreach_slot(pgp->paths, pp1, i) {
if (pp1 == pp)
continue;
if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
return 0;
}
return 1;
}
static int
followover_should_failback(struct multipath *mpp)
{
struct path *pp;
struct pathgroup * pgp;
int i, j;
if (mpp->pgfailback != -FAILBACK_FOLLOWOVER || !mpp->pg || !mpp->bestpg)
return 0;
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (pp->is_checked == CHECK_PATH_NEW_UP &&
do_followover_should_failback(pp))
return 1;
}
}
return 0;
}
/* Returns true if update_map() needs to be called */
static bool
missing_uev_wait_tick(struct multipath *mpp, bool *timed_out)
{
if (mpp->wait_for_udev != UDEV_WAIT_DONE && --mpp->uev_wait_tick <= 0) {
enum udev_wait_states wait = mpp->wait_for_udev;
mpp->wait_for_udev = UDEV_WAIT_DONE;
*timed_out = true;
condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
return wait == UDEV_WAIT_RELOAD;
}
return false;
}
static bool
ghost_delay_tick(struct multipath * mpp)
{
if (mpp->ghost_delay_tick <= 0)
return false;
if (--mpp->ghost_delay_tick <= 0) {
condlog(0, "%s: timed out waiting for active path", mpp->alias);
mpp->force_udev_reload = 1;
return true;
}
return false;
}
static bool deferred_failback_tick(struct multipath *mpp)
{
bool need_reload;
if (mpp->pgfailback <= 0 || mpp->failback_tick <= 0)
return false;
mpp->failback_tick--;
if (!mpp->failback_tick &&
need_switch_pathgroup(mpp, &need_reload)) {
if (need_reload)
return true;
else
switch_pathgroup(mpp);
}
return false;
}
static void
retry_count_tick(struct multipath *mpp)
{
if (mpp->retry_tick <= 0)
return;
mpp->stat_total_queueing_time++;
condlog(4, "%s: Retrying.. No active path", mpp->alias);
if(--mpp->retry_tick == 0) {
mpp->stat_map_failures++;
dm_queue_if_no_path(mpp, 0);
condlog(2, "%s: Disable queueing", mpp->alias);
}
}
static void
partial_retrigger_tick(vector pathvec)
{
struct path *pp;
unsigned int i;
vector_foreach_slot (pathvec, pp, i) {
if (pp->initialized == INIT_PARTIAL && pp->udev &&
pp->partial_retrigger_delay > 0 &&
--pp->partial_retrigger_delay == 0) {
const char *msg = udev_device_get_is_initialized(pp->udev) ?
"change" : "add";
ssize_t len = strlen(msg);
ssize_t ret = sysfs_attr_set_value(pp->udev, "uevent", msg,
len);
if (len != ret)
log_sysfs_attr_set_value(2, ret,
"%s: failed to trigger %s event",
pp->dev, msg);
}
}
}
#ifdef USE_SYSTEMD
static int get_watchdog_interval(void)
{
const char *envp;
long long checkint;
long pid;
envp = getenv("WATCHDOG_PID");
/* See sd_watchdog_enabled(3) */
if (envp && sscanf(envp, "%lu", &pid) == 1 && pid != daemon_pid)
return -1;
envp = getenv("WATCHDOG_USEC");
if (!envp || sscanf(envp, "%llu", &checkint) != 1 || checkint == 0)
return -1;
/*
* Value is in microseconds, and the watchdog should be triggered
* twice per interval.
*/
checkint /= 2000000;
if (checkint > INT_MAX / 2) {
condlog(1, "WatchdogSec=%lld is too high, assuming %d",
checkint * 2, INT_MAX);
checkint = INT_MAX / 2;
} else if (checkint < 1) {
condlog(1, "WatchdogSec=1 is too low, daemon will be killed by systemd!");
checkint = 1;
}
condlog(3, "enabling watchdog, interval %llds", checkint);
return checkint;
}
static void watchdog_tick(const struct timespec *time) {
static int watchdog_interval;
static struct timespec last_time;
struct timespec diff_time;
if (watchdog_interval == 0)
watchdog_interval = get_watchdog_interval();
if (watchdog_interval < 0)
return;
timespecsub(time, &last_time, &diff_time);
if (diff_time.tv_sec >= watchdog_interval) {
condlog(4, "%s: sending watchdog message", __func__);
sd_notify(0, "WATCHDOG=1");
last_time = *time;
}
}
#else
static void watchdog_tick(const struct timespec *time __attribute__((unused))) {}
#endif
static bool update_prio(struct multipath *mpp, bool refresh_all)
{
int oldpriority;
struct path *pp;
struct pathgroup * pgp;
int i, j;
bool changed = false;
bool skipped_path = false;
struct config *conf;
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (pp->state != PATH_UP && pp->state != PATH_GHOST)
continue;
/*
* refresh_all will be set if the mpp has any path
* for whom pp->marginal switched values or for whom
* pp->is_checked == CHECK_PATH_NEW_UP
*/
if (!refresh_all &&
pp->is_checked != CHECK_PATH_CHECKED) {
skipped_path = true;
continue;
}
oldpriority = pp->priority;
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
pathinfo(pp, conf, DI_PRIO);
pthread_cleanup_pop(1);
if (pp->priority != oldpriority)
changed = true;
}
}
if (!changed || !skipped_path)
return changed;
/*
* If a path changed priorities, refresh the priorities of any
* paths we skipped
*/
vector_foreach_slot (mpp->pg, pgp, i) {
vector_foreach_slot (pgp->paths, pp, j) {
if (pp->state != PATH_UP && pp->state != PATH_GHOST)
continue;
if (pp->is_checked == CHECK_PATH_CHECKED)
continue;
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
pathinfo(pp, conf, DI_PRIO);
pthread_cleanup_pop(1);
}
}
return true;
}
static int reload_map(struct vectors *vecs, struct multipath *mpp,
int is_daemon)
{
char *params __attribute__((cleanup(cleanup_charp))) = NULL;
int r;
update_mpp_paths(mpp, vecs->pathvec);
if (setup_map(mpp, ¶ms, vecs)) {
condlog(0, "%s: failed to setup map", mpp->alias);
return 1;
}
mpp->action = ACT_RELOAD;
r = domap(mpp, params, is_daemon);
if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
condlog(3, "%s: domap (%u) failure "
"for reload map", mpp->alias, r);
return 1;
}
return 0;
}
int reload_and_sync_map(struct multipath *mpp, struct vectors *vecs)
{
int ret = 0;
if (reload_map(vecs, mpp, 1))
ret = 1;
if (setup_multipath(vecs, mpp) != 0)
return 2;
sync_map_state(mpp, false);
return ret;
}
static int check_path_reinstate_state(struct path * pp) {
struct timespec curr_time;
/*
* This function is only called when the path state changes
* from "bad" to "good". pp->state reflects the *previous* state.
* If this was "bad", we know that a failure must have occurred
* beforehand, and count that.
* Note that we count path state _changes_ this way. If a path
* remains in "bad" state, failure count is not increased.
*/
if (!((pp->mpp->san_path_err_threshold > 0) &&
(pp->mpp->san_path_err_forget_rate > 0) &&
(pp->mpp->san_path_err_recovery_time >0))) {
return 0;
}
if (pp->disable_reinstate) {
/* If there are no other usable paths, reinstate the path */
if (count_active_paths(pp->mpp) == 0) {
condlog(2, "%s : reinstating path early", pp->dev);
goto reinstate_path;
}
get_monotonic_time(&curr_time);
/* If path became failed again or continue failed, should reset
* path san_path_err_forget_rate and path dis_reinstate_time to
* start a new stable check.
*/
if ((pp->state != PATH_UP) && (pp->state != PATH_GHOST) &&
(pp->state != PATH_DELAYED)) {
pp->san_path_err_forget_rate =
pp->mpp->san_path_err_forget_rate;
pp->dis_reinstate_time = curr_time.tv_sec;
}
if ((curr_time.tv_sec - pp->dis_reinstate_time ) > pp->mpp->san_path_err_recovery_time) {
condlog(2,"%s : reinstate the path after err recovery time", pp->dev);
goto reinstate_path;
}
return 1;
}
/* forget errors on a working path */
if ((pp->state == PATH_UP || pp->state == PATH_GHOST) &&
pp->path_failures > 0) {
if (pp->san_path_err_forget_rate > 0){
pp->san_path_err_forget_rate--;
} else {
/* for every san_path_err_forget_rate number of
* successful path checks decrement path_failures by 1
*/
pp->path_failures--;
pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
}
return 0;
}
/* If the path isn't recovering from a failed state, do nothing */
if (pp->state != PATH_DOWN && pp->state != PATH_SHAKY &&
pp->state != PATH_TIMEOUT)
return 0;
if (pp->path_failures == 0)
pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
pp->path_failures++;
/* if we don't know the currently time, we don't know how long to
* delay the path, so there's no point in checking if we should
*/
get_monotonic_time(&curr_time);
/* when path failures has exceeded the san_path_err_threshold
* place the path in delayed state till san_path_err_recovery_time
* so that the customer can rectify the issue within this time. After
* the completion of san_path_err_recovery_time it should
* automatically reinstate the path
* (note: we know that san_path_err_threshold > 0 here).
*/
if (pp->path_failures > (unsigned int)pp->mpp->san_path_err_threshold) {
condlog(2, "%s : hit error threshold. Delaying path reinstatement", pp->dev);
pp->dis_reinstate_time = curr_time.tv_sec;
pp->disable_reinstate = 1;
return 1;
} else {
return 0;
}
reinstate_path:
pp->path_failures = 0;
pp->disable_reinstate = 0;
pp->san_path_err_forget_rate = 0;
return 0;
}
static int
should_skip_path(struct path *pp){
if (marginal_path_check_enabled(pp->mpp)) {
if (pp->io_err_disable_reinstate && need_io_err_check(pp))
return 1;
} else if (san_path_check_enabled(pp->mpp)) {
if (check_path_reinstate_state(pp))
return 1;
}
return 0;
}
static void
start_path_check(struct path *pp)
{
struct config *conf;
if (path_sysfs_state(pp) == PATH_UP) {
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
start_checker(pp, conf, 1, PATH_UNCHECKED);
pthread_cleanup_pop(1);
} else {
checker_clear_message(&pp->checker);
condlog(3, "%s: state %s, checker not called",
pp->dev, checker_state_name(pp->sysfs_state));
}
}
static int
get_new_state(struct path *pp)
{
int newstate = pp->sysfs_state;
struct config *conf;
if (newstate == PATH_UP)
newstate = get_state(pp);
/*
* Wait for uevent for removed paths;
* some LLDDs like zfcp keep paths unavailable
* without sending uevents.
*/
if (newstate == PATH_REMOVED)
newstate = PATH_DOWN;
/*
* PATH_DISCONNECTED is an ephemeral state used to signal that a path
* has been disconnected at the storage target (LUN unmapped). We use
* it to set pp->disconnected for purge tracking, then immediately
* convert it to PATH_DOWN for normal path failure handling.
*
* This ensures PATH_DISCONNECTED never gets stored in pp->state or
* pp->chkrstate - it exists only as a transient signal from the
* checker to trigger special handling before becoming PATH_DOWN.
*/
if (newstate == PATH_DISCONNECTED) {
if (pp->mpp &&
pp->mpp->purge_disconnected == PURGE_DISCONNECTED_ON &&
pp->disconnected == NOT_DISCONNECTED) {
condlog(2, "%s: mark (%s) path for purge", pp->dev,
checker_state_name(newstate));
pp->disconnected = DISCONNECTED_READY_FOR_PURGE;
}
/* Always convert to PATH_DOWN for normal processing */
newstate = PATH_DOWN;
}
if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
condlog(2, "%s: unusable path (%s) - checker failed",
pp->dev, checker_state_name(newstate));
LOG_MSG(2, pp);
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
pathinfo(pp, conf, 0);
pthread_cleanup_pop(1);
}
return newstate;
}
/* This function may free paths. See check_removed_paths(). */
static int do_sync_mpp(struct vectors *vecs, struct multipath *mpp)
{
int i, ret;
struct path *pp;
ret = update_multipath_strings(mpp, vecs->pathvec);
if (ret != DMP_OK) {
condlog(1, "%s: %s", mpp->alias, ret == DMP_NOT_FOUND ?
"device not found" :
"couldn't synchronize with kernel state");
vector_foreach_slot (mpp->paths, pp, i)
pp->dmstate = PSTATE_UNDEF;
return ret;
}
set_no_path_retry(mpp);
return ret;
}
/* This function may free paths. See check_removed_paths(). */
static int sync_mpp(struct vectors *vecs, struct multipath *mpp, unsigned int ticks)
{
if (mpp->sync_tick)
mpp->sync_tick -= (mpp->sync_tick > ticks) ? ticks :
mpp->sync_tick;
if (mpp->sync_tick && !mpp->checker_count)
return DMP_OK;
return do_sync_mpp(vecs, mpp);
}
/*
* pp->wwid should never be empty when this function is called, but if it
* is, this function can set it.
*/
static bool new_path_wwid_changed(struct path *pp, int state)
{
char wwid[WWID_SIZE];
strlcpy(wwid, pp->wwid, WWID_SIZE);
if (get_uid(pp, state, pp->udev, 1) != 0) {
strlcpy(pp->wwid, wwid, WWID_SIZE);
return false;
}
if (strlen(wwid) && strncmp(wwid, pp->wwid, WWID_SIZE) != 0) {
strlcpy(pp->wwid, wwid, WWID_SIZE);
return true;
}
return false;
}
static int
update_path_state (struct vectors * vecs, struct path * pp)
{
int newstate;
int chkr_new_path_up = 0;
int disable_reinstate = 0;
int oldchkrstate = pp->chkrstate;
unsigned int checkint, max_checkint;
struct config *conf;
int marginal_pathgroups;
conf = get_multipath_config();
checkint = conf->checkint;
max_checkint = conf->max_checkint;
marginal_pathgroups = conf->marginal_pathgroups;
put_multipath_config(conf);
newstate = get_new_state(pp);
if (newstate == PATH_WILD || newstate == PATH_UNCHECKED)
return CHECK_PATH_SKIPPED;
/*
* Async IO in flight. Keep the previous path state
* and reschedule as soon as possible
*/
if (newstate == PATH_PENDING) {
pp->tick = 1;
return CHECK_PATH_SKIPPED;
}
if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
((pp->state != PATH_UP && pp->state != PATH_GHOST) ||
pp->dmstate == PSTATE_FAILED)) {
bool wwid_changed = false;
if (pp->initialized == INIT_NEW) {
/*
* Path was added to map while offline, mark it as
* initialized.
* DI_SYSFS was checked when the path was added
* DI_IOCTL was checked when the checker was selected
* DI_CHECKER just got checked
* DI_WWID is about to be checked
* DI_PRIO will get checked at the end of this checker
* loop
*/
pp->initialized = INIT_OK;
wwid_changed = new_path_wwid_changed(pp, newstate);
} else if (pp->recheck_wwid == RECHECK_WWID_ON)
wwid_changed = check_path_wwid_change(pp);
if (wwid_changed) {
condlog(0, "%s: path wwid change detected. Removing",
pp->dev);
return handle_path_wwid_change(pp, vecs)
? CHECK_PATH_REMOVED
: CHECK_PATH_SKIPPED;
}
}
if ((newstate != PATH_UP && newstate != PATH_GHOST &&
newstate != PATH_PENDING) && (pp->state == PATH_DELAYED)) {
/* If path state become failed again cancel path delay state */
pp->state = newstate;
/*
* path state bad again should change the check interval time
* to the shortest delay
*/
pp->checkint = checkint;
return CHECK_PATH_CHECKED;
}
if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
(san_path_check_enabled(pp->mpp) ||
marginal_path_check_enabled(pp->mpp))) {
if (should_skip_path(pp)) {
if (!pp->marginal && pp->state != PATH_DELAYED)
condlog(2, "%s: path is now marginal", pp->dev);
if (!marginal_pathgroups) {
if (marginal_path_check_enabled(pp->mpp))
/* to reschedule as soon as possible,
* so that this path can be recovered
* in time */
pp->tick = 1;
pp->state = PATH_DELAYED;
return CHECK_PATH_CHECKED;
}
if (!pp->marginal) {
pp->marginal = 1;
pp->mpp->prio_update = PRIO_UPDATE_MARGINAL;
}
} else {
if (pp->marginal || pp->state == PATH_DELAYED)
condlog(2, "%s: path is no longer marginal",
pp->dev);
if (marginal_pathgroups && pp->marginal) {
pp->marginal = 0;
pp->mpp->prio_update = PRIO_UPDATE_MARGINAL;
}
}
}
/*
* don't reinstate failed path, if its in stand-by
* and if target supports only implicit tpgs mode.
* this will prevent unnecessary i/o by dm on stand-by
* paths if there are no other active paths in map.
*/
disable_reinstate = (newstate == PATH_GHOST &&
count_active_paths(pp->mpp) == 0 &&
path_get_tpgs(pp) == TPGS_IMPLICIT) ? 1 : 0;
pp->chkrstate = newstate;
if (newstate != pp->state) {
int oldstate = pp->state;
pp->state = newstate;
LOG_MSG(1, pp);
/*
* upon state change, reset the checkint
* to the shortest delay
*/
pp->checkint = checkint;
if (newstate != PATH_UP && newstate != PATH_GHOST) {
/*
* proactively fail path in the DM
*/
if (oldstate == PATH_UP ||
oldstate == PATH_GHOST)
fail_path(pp, 1);
else
fail_path(pp, 0);
/*
* cancel scheduled failback
*/
pp->mpp->failback_tick = 0;
pp->mpp->stat_path_failures++;
return CHECK_PATH_CHECKED;
}
/* newstate == PATH_UP || newstate == PATH_GHOST */
if (pp->mpp->prflag != PR_UNSET || pp->mpp->ever_registered_pr) {
int prflag = pp->mpp->prflag;
/*
* Check Persistent Reservation.
*/
condlog(2, "%s: checking persistent "
"reservation registration", pp->dev);
mpath_pr_event_handle(pp, 0, 0);
if (pp->mpp->prflag == PR_SET && prflag != PR_SET)
pr_register_active_paths(pp->mpp, NULL);
}
/*
* reinstate this path
*/
if (!disable_reinstate)
reinstate_path(pp);
if (pp->mpp->prio_update != PRIO_UPDATE_MARGINAL)
pp->mpp->prio_update = PRIO_UPDATE_NEW_PATH;
if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
chkr_new_path_up = 1;
/*
* if at least one path is up in a group, and
* the group is disabled, re-enable it
*/
if (newstate == PATH_UP)
enable_group(pp);
}
else if (newstate == PATH_UP || newstate == PATH_GHOST) {
if ((pp->dmstate == PSTATE_FAILED ||
pp->dmstate == PSTATE_UNDEF) &&
!disable_reinstate)
/* Clear IO errors */
reinstate_path(pp);
else {
LOG_MSG(4, pp);
if (pp->checkint != max_checkint) {
/*
* double the next check delay.
* max at conf->max_checkint
*/
if (pp->checkint < (max_checkint / 2))
pp->checkint = 2 * pp->checkint;
else
pp->checkint = max_checkint;
condlog(4, "%s: delay next check %is",
pp->dev_t, pp->checkint);
}
}
}
else if (newstate != PATH_UP && newstate != PATH_GHOST) {
if (pp->dmstate == PSTATE_ACTIVE ||
pp->dmstate == PSTATE_UNDEF)
fail_path(pp, 0);
if (newstate == PATH_DOWN) {
int log_checker_err;
conf = get_multipath_config();
log_checker_err = conf->log_checker_err;
put_multipath_config(conf);
if (log_checker_err == LOG_CHKR_ERR_ONCE)
LOG_MSG(3, pp);
else
LOG_MSG(2, pp);
}
pp->checkint = checkint;
}
if (pp->mpp->prio_update == PRIO_UPDATE_NONE &&
(newstate == PATH_UP || newstate == PATH_GHOST))
pp->mpp->prio_update = PRIO_UPDATE_NORMAL;
pp->state = newstate;
return chkr_new_path_up ? CHECK_PATH_NEW_UP : CHECK_PATH_CHECKED;
}
/* Return value: true if the map needs to be reloaded */
static bool update_mpp_prio(struct multipath *mpp)
{
bool need_reload, changed;
enum prio_update_type prio_update = mpp->prio_update;
mpp->prio_update = PRIO_UPDATE_NONE;
if (mpp->wait_for_udev != UDEV_WAIT_DONE ||
prio_update == PRIO_UPDATE_NONE)
return false;
condlog(4, "prio refresh");
changed = update_prio(mpp, prio_update != PRIO_UPDATE_NORMAL);
if (prio_update == PRIO_UPDATE_MARGINAL)
return true;
if (changed && mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio &&
mpp->pgfailback == -FAILBACK_IMMEDIATE) {
condlog(2, "%s: path priorities changed. reloading",
mpp->alias);
return true;
}
if (need_switch_pathgroup(mpp, &need_reload)) {
if (mpp->pgfailback > 0 &&
(prio_update == PRIO_UPDATE_NEW_PATH ||
mpp->failback_tick <= 0))
mpp->failback_tick = mpp->pgfailback + 1;
else if (mpp->pgfailback == -FAILBACK_IMMEDIATE ||
(prio_update == PRIO_UPDATE_NEW_PATH &&
followover_should_failback(mpp))) {
if (need_reload)
return true;
else
switch_pathgroup(mpp);
}
}
return false;
}
static int
check_path (struct path * pp, unsigned int ticks)
{
if (pp->initialized == INIT_REMOVED)
return CHECK_PATH_SKIPPED;
if (pp->tick)
pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
if (pp->tick)
return CHECK_PATH_SKIPPED;
if (pp->checkint == CHECKINT_UNDEF) {
struct config *conf;
condlog(0, "%s: BUG: checkint is not set", pp->dev);
conf = get_multipath_config();
pp->checkint = conf->checkint;
put_multipath_config(conf);
}
start_path_check(pp);
return CHECK_PATH_STARTED;
}
static int
update_path(struct vectors * vecs, struct path * pp, time_t start_secs)
{
int r;
unsigned int adjust_int, max_checkint;
struct config *conf;
time_t next_idx, goal_idx;
r = update_path_state(vecs, pp);
/*
* update_path_state() removed or orphaned the path.
*/
if (r == CHECK_PATH_REMOVED || !pp->mpp)
return r;
if (pp->tick != 0) {
/* the path checker is pending */
if (pp->state != PATH_DELAYED)
pp->pending_ticks++;
else
pp->pending_ticks = 0;
return r;
}
/* schedule the next check */
pp->tick = pp->checkint;
if (pp->pending_ticks >= pp->tick)
pp->tick = 1;
else
pp->tick -= pp->pending_ticks;
pp->pending_ticks = 0;
if (pp->tick == 1)
return r;
conf = get_multipath_config();
max_checkint = conf->max_checkint;
adjust_int = conf->adjust_int;
put_multipath_config(conf);
/*
* every mpp has a goal_idx in the range of
* 0 <= goal_idx < conf->max_checkint
*
* The next check has an index, next_idx, in the range of
* 0 <= next_idx < conf->adjust_int
*
* If the difference between the goal index and the next check index
* is not a multiple of pp->checkint, then the device is not checking
* the paths at its goal index, and pp->tick will be decremented by
* one, to align it over time.
*/
goal_idx = (find_slot(vecs->mpvec, pp->mpp)) *
max_checkint / VECTOR_SIZE(vecs->mpvec);
next_idx = (start_secs + pp->tick) % adjust_int;
if ((goal_idx - next_idx) % pp->checkint != 0)
pp->tick--;
return r;
}
static int
check_uninitialized_path(struct path * pp, unsigned int ticks)
{
int retrigger_tries;
struct config *conf;
if (pp->initialized != INIT_NEW && pp->initialized != INIT_FAILED &&
pp->initialized != INIT_MISSING_UDEV &&
!(pp->initialized == INIT_OK && pp->add_when_online))
return CHECK_PATH_SKIPPED;
if (pp->tick)
pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
if (pp->tick)
return CHECK_PATH_SKIPPED;
conf = get_multipath_config();
retrigger_tries = conf->retrigger_tries;
pp->tick = conf->max_checkint;
pp->checkint = conf->checkint;
put_multipath_config(conf);
if (pp->initialized == INIT_MISSING_UDEV) {
if (pp->retriggers < retrigger_tries) {
static const char change[] = "change";
ssize_t ret;
condlog(2, "%s: triggering change event to reinitialize",
pp->dev);
pp->initialized = INIT_REQUESTED_UDEV;
pp->retriggers++;
ret = sysfs_attr_set_value(pp->udev, "uevent", change,
sizeof(change) - 1);
if (ret != sizeof(change) - 1)
log_sysfs_attr_set_value(1, ret,
"%s: failed to trigger change event",
pp->dev);
return CHECK_PATH_SKIPPED;
} else {
condlog(1, "%s: not initialized after %d udev retriggers",
pp->dev, retrigger_tries);
/*
* Make sure that the "add missing path" code path
* below may reinstate the path later, if it ever
* comes up again.
* The WWID needs not be cleared; if it was set, the
* state hadn't been INIT_MISSING_UDEV in the first
* place.
*/
pp->initialized = INIT_FAILED;
}
}
start_path_check(pp);
return CHECK_PATH_STARTED;
}
static int
update_uninitialized_path(struct vectors * vecs, struct path * pp)
{
int newstate, ret;
struct config *conf;
if (pp->initialized != INIT_NEW && pp->initialized != INIT_FAILED &&
!(pp->initialized == INIT_OK && pp->add_when_online))
return CHECK_PATH_SKIPPED;
newstate = get_new_state(pp);
if (!strlen(pp->wwid) &&
(pp->initialized == INIT_FAILED || pp->initialized == INIT_NEW) &&
(newstate == PATH_UP || newstate == PATH_GHOST)) {
condlog(2, "%s: add missing path", pp->dev);
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
pthread_cleanup_pop(1);
/* INIT_OK implies ret == PATHINFO_OK */
if (pp->initialized == INIT_OK) {
ev_add_path(pp, vecs, 1);
pp->tick = 1;
} else if (ret == PATHINFO_SKIPPED) {
int i;
condlog(1, "%s: path blacklisted. removing", pp->dev);
if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
vector_del_slot(vecs->pathvec, i);
free_path(pp);
return CHECK_PATH_REMOVED;
}
} else if (pp->initialized == INIT_OK && pp->add_when_online &&
(newstate == PATH_UP || newstate == PATH_GHOST)) {
pp->add_when_online = false;
if (can_recheck_wwid(pp) && check_path_wwid_change(pp)) {
condlog(0, "%s: path wwid change detected. Removing",
pp->dev);
return handle_path_wwid_change(pp, vecs)?
CHECK_PATH_REMOVED :
CHECK_PATH_SKIPPED;
}
ev_add_path(pp, vecs, 1);
pp->tick = 1;
}
return CHECK_PATH_CHECKED;
}
enum checker_state {
CHECKER_STARTING,
CHECKER_CHECKING_PATHS,
CHECKER_WAITING_FOR_PATHS,
CHECKER_UPDATING_PATHS,
CHECKER_FINISHED,
};
static enum checker_state
check_paths(struct vectors *vecs, unsigned int ticks)
{
unsigned int paths_checked = 0;
struct timespec diff_time, start_time, end_time;
struct path *pp;
int i;
bool need_wait = false;
get_monotonic_time(&start_time);
vector_foreach_slot(vecs->pathvec, pp, i) {
if (pp->is_checked != CHECK_PATH_UNCHECKED)
continue;
if (pp->mpp) {
pp->is_checked = check_path(pp, ticks);
if (pp->is_checked == CHECK_PATH_STARTED)
pp->mpp->checker_count++;
} else
pp->is_checked = check_uninitialized_path(pp, ticks);
if (pp->is_checked == CHECK_PATH_STARTED &&
checker_need_wait(&pp->checker))
need_wait = true;
if (++paths_checked % 128 == 0 &&
(lock_has_waiters(&vecs->lock) || waiting_clients())) {
get_monotonic_time(&end_time);
timespecsub(&end_time, &start_time, &diff_time);
if (diff_time.tv_sec > 0)
return CHECKER_CHECKING_PATHS;
}
}
return need_wait ? CHECKER_WAITING_FOR_PATHS : CHECKER_UPDATING_PATHS;
}
static enum checker_state
update_paths(struct vectors *vecs, int *num_paths_p, time_t start_secs)
{
unsigned int paths_checked = 0;
struct timespec diff_time, start_time, end_time;
struct path *pp;
int i, rc;
get_monotonic_time(&start_time);
vector_foreach_slot(vecs->pathvec, pp, i) {
if (pp->is_checked != CHECK_PATH_STARTED)
continue;
if (pp->mpp)
rc = update_path(vecs, pp, start_secs);
else
rc = update_uninitialized_path(vecs, pp);
if (rc == CHECK_PATH_REMOVED)
i--;
else {
pp->is_checked = rc;
if (rc == CHECK_PATH_CHECKED || rc == CHECK_PATH_NEW_UP)
(*num_paths_p)++;
}
if (++paths_checked % 128 == 0 &&
(lock_has_waiters(&vecs->lock) || waiting_clients())) {
get_monotonic_time(&end_time);
timespecsub(&end_time, &start_time, &diff_time);
if (diff_time.tv_sec > 0)
return CHECKER_UPDATING_PATHS;
}
}
return CHECKER_FINISHED;
}
static void enable_pathgroups(struct multipath *mpp)
{
struct pathgroup *pgp;
int i;
vector_foreach_slot(mpp->pg, pgp, i) {
struct path *pp;
int j;
if (pgp->status != PGSTATE_DISABLED)
continue;
vector_foreach_slot(pgp->paths, pp, j) {
if (!(pp->state == PATH_UP &&
(pp->is_checked == CHECK_PATH_CHECKED ||
pp->is_checked == CHECK_PATH_NEW_UP)))
continue;
if (dm_enablegroup(mpp->alias, i + 1) == 0) {
condlog(2, "%s: enabled pathgroup #%i",
mpp->alias, i + 1);
pgp->status = PGSTATE_ENABLED;
} else
condlog(2, "%s: failed to enable pathgroup #%i",
mpp->alias, i + 1);
break;
}
}
}
static void free_orphan_paths(vector pathvec)
{
struct path *pp;
int i;
vector_foreach_slot (pathvec, pp, i) {
if (!pp->mpp && (pp->initialized == INIT_REMOVED ||
pp->initialized == INIT_PARTIAL)) {
condlog(2, "%s: freeing orphan %s in %s state",
__func__, pp->dev,
pp->initialized == INIT_REMOVED ? "removed" : "partial");
vector_del_slot(pathvec, i--);
free_path(pp);
}
}
}
static void checker_finished(struct vectors *vecs, unsigned int ticks,
struct list_head *purge_list)
{
struct multipath *mpp;
bool uev_timed_out = false;
int i;
free_orphan_paths(vecs->pathvec);
vector_foreach_slot(vecs->mpvec, mpp, i) {
bool inconsistent, prio_reload, failback_reload;
bool uev_wait_reload, ghost_reload;
if (sync_mpp(vecs, mpp, ticks) == DMP_NOT_FOUND) {
remove_map_and_stop_waiter(mpp, vecs);
i--;
continue;
}
inconsistent = mpp->need_reload;
prio_reload = update_mpp_prio(mpp);
failback_reload = deferred_failback_tick(mpp);
uev_wait_reload = missing_uev_wait_tick(mpp, &uev_timed_out);
ghost_reload = ghost_delay_tick(mpp);
if (uev_wait_reload) {
if (update_map(mpp, vecs, 0)) {
/* multipath device deleted */
i--;
continue;
}
} else if (prio_reload || failback_reload || ghost_reload || inconsistent) {
if (mpp->wait_for_udev != UDEV_WAIT_DONE) {
mpp->need_reload = false;
mpp->wait_for_udev = UDEV_WAIT_RELOAD;
} else if (reload_and_sync_map(mpp, vecs) == 2) {
/* multipath device deleted */
i--;
continue;
}
} else
/* not necessary after map reloads */
enable_pathgroups(mpp);
/* need_reload was cleared in dm_addmap and then set again */
if (inconsistent && mpp->need_reload)
condlog(1, "BUG: %s; map remained in inconsistent state after reload",
mpp->alias);
retry_count_tick(mpp);
}
if (uev_timed_out && !need_to_delay_reconfig(vecs))
unblock_reconfigure();
partial_retrigger_tick(vecs->pathvec);
/*
* Build purge list for disconnected paths.
* The caller will queue it after releasing vecs->lock.
*/
build_purge_list(vecs, purge_list);
}
static void *
checkerloop (void *ap)
{
struct vectors *vecs;
struct path *pp;
struct timespec last_time;
struct config *conf;
int foreign_tick = 0;
pthread_cleanup_push(rcu_unregister, NULL);
rcu_register_thread();
mlockall(MCL_CURRENT | MCL_FUTURE);
vecs = (struct vectors *)ap;
/* Tweak start time for initial path check */
get_monotonic_time(&last_time);
last_time.tv_sec -= 1;
while (1) {
struct timespec diff_time, start_time, end_time;
int num_paths = 0, strict_timing;
unsigned int ticks = 0;
enum checker_state checker_state = CHECKER_STARTING;
LIST_HEAD(purge_list);
/*
* Cleanup handler to free purge_list if thread is cancelled.
* This prevents memory leaks during shutdown.
*/
pthread_cleanup_push(cleanup_purge_list, &purge_list);
if (set_config_state(DAEMON_RUNNING) != DAEMON_RUNNING)
/* daemon shutdown */
break;
get_monotonic_time(&start_time);
timespecsub(&start_time, &last_time, &diff_time);
condlog(4, "tick (%ld.%06lu secs)",
(long)diff_time.tv_sec, diff_time.tv_nsec / 1000);
last_time = start_time;
ticks = diff_time.tv_sec;
watchdog_tick(&start_time);
while (checker_state != CHECKER_FINISHED) {
struct multipath *mpp;
int i;
if (checker_state != CHECKER_STARTING) {
struct timespec wait = { .tv_nsec = 10000, };
if (checker_state == CHECKER_WAITING_FOR_PATHS) {
/* wait 5ms */
wait.tv_nsec = 5 * 1000 * 1000;
checker_state = CHECKER_UPDATING_PATHS;
}
nanosleep(&wait, NULL);
}
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
if (checker_state == CHECKER_STARTING) {
vector_foreach_slot(vecs->mpvec, mpp, i) {
mpp->prio_update = PRIO_UPDATE_NONE;
mpp->checker_count = 0;
}
vector_foreach_slot(vecs->pathvec, pp, i)
pp->is_checked = CHECK_PATH_UNCHECKED;
checker_state = CHECKER_CHECKING_PATHS;
}
if (checker_state == CHECKER_CHECKING_PATHS)
checker_state = check_paths(vecs, ticks);
if (checker_state == CHECKER_UPDATING_PATHS)
checker_state = update_paths(vecs, &num_paths,
start_time.tv_sec);
if (checker_state == CHECKER_FINISHED)
checker_finished(vecs, ticks, &purge_list);
lock_cleanup_pop(vecs->lock);
}
/*
* Queue purge work for disconnected paths.
* This is done after releasing vecs->lock to avoid holding
* the lock while signaling the purge thread.
*/
if (!list_empty(&purge_list)) {
pthread_cleanup_push(cleanup_mutex, &purge_mutex);
pthread_mutex_lock(&purge_mutex);
pthread_testcancel();
list_splice_tail_init(&purge_list, &purge_queue);
pthread_cond_signal(&purge_cond);
pthread_cleanup_pop(1);
}
get_monotonic_time(&end_time);
timespecsub(&end_time, &start_time, &diff_time);
if (num_paths) {
unsigned int max_checkint;
condlog(4, "checked %d path%s in %ld.%06lu secs",
num_paths, num_paths > 1 ? "s" : "",
(long)diff_time.tv_sec,
diff_time.tv_nsec / 1000);
conf = get_multipath_config();
max_checkint = conf->max_checkint;
put_multipath_config(conf);
if (diff_time.tv_sec > (time_t)max_checkint)
condlog(1, "path checkers took longer "
"than %ld seconds, consider "
"increasing max_polling_interval",
(long)diff_time.tv_sec);
}
if (foreign_tick == 0) {
conf = get_multipath_config();
foreign_tick = conf->max_checkint;
put_multipath_config(conf);
}
if (--foreign_tick == 0)
check_foreign();
post_config_state(DAEMON_IDLE);
conf = get_multipath_config();
strict_timing = conf->strict_timing;
put_multipath_config(conf);
if (!strict_timing)
sleep(1);
else {
diff_time.tv_sec = 0;
diff_time.tv_nsec =
1000UL * 1000 * 1000 - diff_time.tv_nsec;
normalize_timespec(&diff_time);
condlog(3, "waiting for %ld.%06lu secs",
(long)diff_time.tv_sec,
diff_time.tv_nsec / 1000);
if (nanosleep(&diff_time, NULL) != 0) {
condlog(3, "nanosleep failed with error %d",
errno);
conf = get_multipath_config();
conf->strict_timing = 0;
put_multipath_config(conf);
break;
}
}
/*
* Pop cleanup handler. Execute it (arg=1) to free purge_list
* at the end of each iteration.
*/
pthread_cleanup_pop(1);
}
pthread_cleanup_pop(1);
return NULL;
}
static int
configure (struct vectors * vecs, enum force_reload_types reload_type)
{
struct multipath * mpp;
struct path * pp;
vector mpvec;
int i, ret;
struct config *conf;
if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
condlog(0, "couldn't allocate path vec in configure");
return 1;
}
if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
condlog(0, "couldn't allocate multipath vec in configure");
return 1;
}
if (!(mpvec = vector_alloc())) {
condlog(0, "couldn't allocate new maps vec in configure");
return 1;
}
/*
* probe for current path (from sysfs) and map (from dm) sets
*/
ret = path_discovery(vecs->pathvec, DI_ALL);
if (ret < 0) {
condlog(0, "configure failed at path discovery");
goto fail;
}
if (should_exit())
goto fail;
conf = get_multipath_config();
pthread_cleanup_push(put_multipath_config, conf);
vector_foreach_slot (vecs->pathvec, pp, i){
if (filter_path(conf, pp) > 0){
vector_del_slot(vecs->pathvec, i);
free_path(pp);
i--;
}
}
pthread_cleanup_pop(1);
if (map_discovery(vecs)) {
condlog(0, "configure failed at map discovery");
goto fail;
}
if (should_exit())
goto fail;
ret = coalesce_paths(vecs, mpvec, NULL, reload_type, CMD_NONE);
if (ret != CP_OK) {
condlog(0, "configure failed while coalescing paths");
goto fail;
}
if (should_exit())
goto fail;
/*
* may need to remove some maps which are no longer relevant
* e.g., due to blacklist changes in conf file
*/
if (coalesce_maps(vecs, mpvec)) {
condlog(0, "configure failed while coalescing maps");
goto fail;
}
if (should_exit())
goto fail;
sync_maps_state(mpvec);
vector_foreach_slot(mpvec, mpp, i){
if (remember_wwid(mpp->wwid) == 1)
trigger_paths_udev_change(mpp, true);
pr_register_active_paths(mpp, NULL);
}
/*
* purge dm of old maps and save new set of maps formed by
* considering current path state
*/
remove_maps(vecs);
vecs->mpvec = mpvec;
/*
* start dm event waiter threads for these new maps
*/
vector_foreach_slot(vecs->mpvec, mpp, i) {
if (wait_for_events(mpp, vecs)) {
vector_del_slot(vecs->mpvec, i--);
remove_map(mpp, vecs->pathvec);
continue;
}
if (setup_multipath(vecs, mpp))
i--;
}
return 0;
fail:
vector_free(mpvec);
return 1;
}
int
need_to_delay_reconfig(struct vectors * vecs)
{
struct multipath *mpp;
int i;
if (!VECTOR_SIZE(vecs->mpvec))
return 0;
vector_foreach_slot(vecs->mpvec, mpp, i) {
if (mpp->wait_for_udev != UDEV_WAIT_DONE)
return 1;
}
return 0;
}
void rcu_free_config(struct rcu_head *head)
{
struct config *conf = container_of(head, struct config, rcu);
free_config(conf);
}
static bool reconfigure_check_uid_attrs(const struct vector_s *old_attrs,
const struct vector_s *new_attrs)
{
int i;
char *old;
if (VECTOR_SIZE(old_attrs) != VECTOR_SIZE(new_attrs))
return true;
vector_foreach_slot(old_attrs, old, i) {
char *new = VECTOR_SLOT(new_attrs, i);
if (strcmp(old, new))
return true;
}
return false;
}
static void reconfigure_check(struct config *old, struct config *new)
{
int old_marginal_pathgroups;
old_marginal_pathgroups = old->marginal_pathgroups;
if ((old_marginal_pathgroups == MARGINAL_PATHGROUP_FPIN) !=
(new->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)) {
condlog(1, "multipathd must be restarted to turn %s fpin marginal paths",
(old_marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)?
"off" : "on");
new->marginal_pathgroups = old_marginal_pathgroups;
}
if (reconfigure_check_uid_attrs(&old->uid_attrs, &new->uid_attrs)) {
int i;
void *ptr;
condlog(1, "multipathd must be restarted to change uid_attrs, keeping old values");
vector_foreach_slot(&new->uid_attrs, ptr, i)
free(ptr);
vector_reset(&new->uid_attrs);
new->uid_attrs = old->uid_attrs;
/* avoid uid_attrs being freed in rcu_free_config() */
old->uid_attrs.allocated = 0;
old->uid_attrs.slot = NULL;
}
}
static int
reconfigure (struct vectors *vecs, enum force_reload_types reload_type)
{
struct config * old, *conf;
conf = load_config(DEFAULT_CONFIGFILE);
if (!conf)
return 1;
if (verbosity)
libmp_verbosity = verbosity;
setlogmask(LOG_UPTO(libmp_verbosity + 3));
condlog(2, "%s: setting up paths and maps", __func__);
/*
* free old map and path vectors ... they use old conf state
*/
if (VECTOR_SIZE(vecs->mpvec))
remove_maps_and_stop_waiters(vecs);
free_pathvec(vecs->pathvec, FREE_PATHS);
vecs->pathvec = NULL;
delete_all_foreign();
reset_checker_classes();
if (bindings_read_only)
conf->bindings_read_only = bindings_read_only;
if (check_alias_settings(conf))
return 1;
uxsock_timeout = conf->uxsock_timeout;
old = rcu_dereference(multipath_conf);
reconfigure_check(old, conf);
conf->sequence_nr = old->sequence_nr + 1;
rcu_assign_pointer(multipath_conf, conf);
call_rcu(&old->rcu, rcu_free_config);
#ifdef FPIN_EVENT_HANDLER
fpin_clean_marginal_dev_list(NULL);
#endif
configure(vecs, reload_type);
return 0;
}
static struct vectors *
init_vecs (void)
{
struct vectors * vecs;
vecs = (struct vectors *)calloc(1, sizeof(struct vectors));
if (!vecs)
return NULL;
init_lock(&vecs->lock);
return vecs;
}
static void *
signal_set(int signo, void (*func) (int))
{
int r;
struct sigaction sig;
struct sigaction osig;
sig.sa_handler = func;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
r = sigaction(signo, &sig, &osig);
if (r < 0)
return (SIG_ERR);
else
return (osig.sa_handler);
}
void
handle_signals(bool nonfatal)
{
if (exit_sig) {
condlog(3, "exit (signal)");
exit_sig = 0;
exit_daemon();
}
if (!nonfatal)
return;
if (reconfig_sig) {
condlog(3, "reconfigure (signal)");
schedule_reconfigure(FORCE_RELOAD_WEAK);
}
if (log_reset_sig) {
condlog(3, "reset log (signal)");
if (logsink == LOGSINK_SYSLOG)
log_thread_reset();
}
reconfig_sig = 0;
log_reset_sig = 0;
}
static void
sighup(__attribute__((unused)) int sig)
{
reconfig_sig = 1;
}
static void
sigend(__attribute__((unused)) int sig)
{
exit_sig = 1;
}
static void
sigusr1(__attribute__((unused)) int sig)
{
log_reset_sig = 1;
}
static void
sigusr2(__attribute__((unused)) int sig)
{
condlog(3, "SIGUSR2 received");
}
static void
signal_init(void)
{
sigset_t set;
/* block all signals */
sigfillset(&set);
/* SIGPIPE occurs if logging fails */
sigdelset(&set, SIGPIPE);
pthread_sigmask(SIG_SETMASK, &set, NULL);
/* Other signals will be unblocked in the uxlsnr thread */
signal_set(SIGHUP, sighup);
signal_set(SIGUSR1, sigusr1);
signal_set(SIGUSR2, sigusr2);
signal_set(SIGINT, sigend);
signal_set(SIGTERM, sigend);
signal_set(SIGPIPE, sigend);
}
static void
setscheduler (void)
{
int res;
static struct sched_param sched_param;
struct rlimit rlim;
if (getrlimit(RLIMIT_RTPRIO, &rlim) < 0 || rlim.rlim_max == 0)
return;
sched_param.sched_priority = rlim.rlim_max > INT_MAX ? INT_MAX :
rlim.rlim_max;
res = sched_get_priority_max(SCHED_RR);
if (res > 0 && res < sched_param.sched_priority)
sched_param.sched_priority = res;
res = sched_setscheduler(0, SCHED_RR, &sched_param);
if (res == -1)
condlog(2, "Could not set SCHED_RR at priority %d",
sched_param.sched_priority);
return;
}
static void set_oom_adj(void)
{
FILE *fp;
if (getenv("OOMScoreAdjust")) {
condlog(3, "Using systemd provided OOMScoreAdjust");
return;
}
#ifdef OOM_SCORE_ADJ_MIN
fp = fopen("/proc/self/oom_score_adj", "w");
if (fp) {
fprintf(fp, "%i", OOM_SCORE_ADJ_MIN);
fclose(fp);
return;
}
#endif
fp = fopen("/proc/self/oom_adj", "w");
if (fp) {
fprintf(fp, "%i", OOM_ADJUST_MIN);
fclose(fp);
return;
}
condlog(0, "couldn't adjust oom score");
}
static void cleanup_pidfile(void)
{
if (pid_fd >= 0)
close(pid_fd);
condlog(3, "unlink pidfile");
unlink(DEFAULT_PIDFILE);
}
static void cleanup_conf(void) {
struct config *conf;
conf = rcu_dereference(multipath_conf);
if (!conf)
return;
rcu_assign_pointer(multipath_conf, NULL);
call_rcu(&conf->rcu, rcu_free_config);
}
static void cleanup_maps(struct vectors *vecs)
{
int queue_without_daemon, i;
struct multipath *mpp;
struct config *conf;
conf = get_multipath_config();
queue_without_daemon = conf->queue_without_daemon;
put_multipath_config(conf);
if (queue_without_daemon == QUE_NO_DAEMON_OFF)
vector_foreach_slot(vecs->mpvec, mpp, i)
dm_queue_if_no_path(mpp, 0);
remove_maps_and_stop_waiters(vecs);
vecs->mpvec = NULL;
}
static void cleanup_paths(struct vectors *vecs)
{
free_pathvec(vecs->pathvec, FREE_PATHS);
vecs->pathvec = NULL;
}
static void cleanup_vecs(void)
{
if (!gvecs)
return;
/*
* We can't take the vecs lock here, because exit() may
* have been called from the child() thread, holding the lock already.
* Anyway, by the time we get here, all threads that might access
* vecs should have been joined already (in cleanup_threads).
*/
cleanup_maps(gvecs);
cleanup_paths(gvecs);
pthread_mutex_destroy(&gvecs->lock.mutex);
free(gvecs);
gvecs = NULL;
}
static void cleanup_threads(void)
{
stop_io_err_stat_thread();
if (check_thr_started)
pthread_cancel(check_thr);
if (purge_thr_started)
pthread_cancel(purge_thr);
if (uevent_thr_started)
pthread_cancel(uevent_thr);
if (uxlsnr_thr_started)
pthread_cancel(uxlsnr_thr);
if (uevq_thr_started)
pthread_cancel(uevq_thr);
if (dmevent_thr_started)
pthread_cancel(dmevent_thr);
if (fpin_thr_started)
pthread_cancel(fpin_thr);
if (fpin_consumer_thr_started)
pthread_cancel(fpin_consumer_thr);
if (check_thr_started)
pthread_join(check_thr, NULL);
if (purge_thr_started)
pthread_join(purge_thr, NULL);
if (uevent_thr_started)
pthread_join(uevent_thr, NULL);
if (uxlsnr_thr_started)
pthread_join(uxlsnr_thr, NULL);
if (uevq_thr_started)
pthread_join(uevq_thr, NULL);
if (dmevent_thr_started)
pthread_join(dmevent_thr, NULL);
if (fpin_thr_started)
pthread_join(fpin_thr, NULL);
if (fpin_consumer_thr_started)
pthread_join(fpin_consumer_thr, NULL);
/*
* As all threads are joined now, and we're in DAEMON_SHUTDOWN
* state, no new waiter threads will be created anymore.
*/
pthread_attr_destroy(&waiter_attr);
}
#ifndef URCU_VERSION
# define URCU_VERSION 0
#endif
#if (URCU_VERSION >= 0x000800)
/*
* Use a non-default call_rcu_data for child().
*
* We do this to avoid a memory leak from liburcu.
* liburcu never frees the default rcu handler (see comments on
* call_rcu_data_free() in urcu-call-rcu-impl.h), its thread
* can't be joined with pthread_join(), leaving a memory leak.
*
* Therefore we create our own, which can be destroyed and joined.
* The cleanup handler needs to call rcu_barrier(), which is only
* available in user-space RCU v0.8 and newer. See
* https://lists.lttng.org/pipermail/lttng-dev/2021-May/029958.html
*/
static struct call_rcu_data *setup_rcu(void)
{
struct call_rcu_data *crdp;
rcu_init();
rcu_register_thread();
crdp = create_call_rcu_data(0UL, -1);
if (crdp != NULL)
set_thread_call_rcu_data(crdp);
return crdp;
}
static struct call_rcu_data *mp_rcu_data;
static void cleanup_rcu(void)
{
/* Wait for any pending RCU calls */
rcu_barrier();
if (mp_rcu_data != NULL) {
#if (URCU_VERSION < 0x000E00)
pthread_t rcu_thread;
rcu_thread = get_call_rcu_thread(mp_rcu_data);
#endif
/* detach this thread from the RCU thread */
set_thread_call_rcu_data(NULL);
synchronize_rcu();
/* tell RCU thread to exit */
call_rcu_data_free(mp_rcu_data);
#if (URCU_VERSION < 0x000E00)
pthread_join(rcu_thread, NULL);
#endif
}
rcu_unregister_thread();
}
#endif /* URCU_VERSION */
static void cleanup_child(void)
{
cleanup_threads();
cleanup_vecs();
cleanup_bindings();
if (poll_dmevents)
cleanup_dmevent_waiter();
cleanup_pidfile();
if (logsink == LOGSINK_SYSLOG)
log_thread_stop();
cleanup_conf();
}
static int sd_notify_exit(int err)
{
#ifdef USE_SYSTEMD
char msg[24];
snprintf(msg, sizeof(msg), "ERRNO=%d", err);
sd_notify(0, msg);
#endif
return err;
}
static int
child (__attribute__((unused)) void *param)
{
pthread_attr_t log_attr, misc_attr, uevent_attr;
struct vectors * vecs;
int rc;
struct config *conf;
char *envp;
enum daemon_status state = DAEMON_INIT;
int exit_code = 1;
int fpin_marginal_paths = 0;
init_unwinder();
mlockall(MCL_CURRENT | MCL_FUTURE);
signal_init();
#if (URCU_VERSION >= 0x000800)
mp_rcu_data = setup_rcu();
if (atexit(cleanup_rcu))
fprintf(stderr, "failed to register RCU cleanup handler\n");
#else
rcu_init();
#endif
if (atexit(cleanup_child))
fprintf(stderr, "failed to register cleanup handlers\n");
setup_thread_attr(&misc_attr, 64 * 1024, 0);
setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
setup_thread_attr(&waiter_attr, 32 * 1024, 1);
if (logsink == LOGSINK_SYSLOG) {
setup_thread_attr(&log_attr, 64 * 1024, 0);
log_thread_start(&log_attr);
pthread_attr_destroy(&log_attr);
}
pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
if (pid_fd < 0) {
condlog(1, "failed to create pidfile");
exit(1);
}
post_config_state(DAEMON_START);
condlog(2, "multipathd v%d.%d.%d%s: start up",
MULTIPATH_VERSION(VERSION_CODE), EXTRAVERSION);
condlog(3, "read " DEFAULT_CONFIGFILE);
if (verbosity)
libmp_verbosity = verbosity;
conf = load_config(DEFAULT_CONFIGFILE);
if (verbosity)
libmp_verbosity = verbosity;
setlogmask(LOG_UPTO(libmp_verbosity + 3));
if (!conf) {
condlog(0, "failed to load configuration");
goto failed;
}
if (bindings_read_only)
conf->bindings_read_only = bindings_read_only;
uxsock_timeout = conf->uxsock_timeout;
rcu_assign_pointer(multipath_conf, conf);
if (init_checkers()) {
condlog(0, "failed to initialize checkers");
goto failed;
}
if (init_prio()) {
condlog(0, "failed to initialize prioritizers");
goto failed;
}
/* Failing this is non-fatal */
init_foreign(conf->enable_foreign);
if (poll_dmevents)
poll_dmevents = dmevent_poll_supported();
envp = getenv("LimitNOFILE");
if (envp)
condlog(2,"Using systemd provided open fds limit of %s", envp);
else
set_max_fds(conf->max_fds);
vecs = gvecs = init_vecs();
if (!vecs)
goto failed;
setscheduler();
set_oom_adj();
#ifdef FPIN_EVENT_HANDLER
if (conf->marginal_pathgroups == MARGINAL_PATHGROUP_FPIN)
fpin_marginal_paths = 1;
#endif
/*
* Startup done, invalidate configuration
*/
conf = NULL;
pthread_cleanup_push(config_cleanup, NULL);
pthread_mutex_lock(&config_lock);
rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
if (!rc) {
/* Wait for uxlsnr startup */
while (running_state == DAEMON_START)
pthread_cond_wait(&config_cond, &config_lock);
state = running_state;
}
pthread_cleanup_pop(1);
if (rc) {
condlog(0, "failed to create cli listener: %d", rc);
goto failed;
}
else {
uxlsnr_thr_started = true;
if (state != DAEMON_CONFIGURE) {
condlog(0, "cli listener failed to start");
goto failed;
}
}
if (poll_dmevents) {
if (init_dmevent_waiter(vecs)) {
condlog(0, "failed to allocate dmevents waiter info");
goto failed;
}
if ((rc = pthread_create(&dmevent_thr, &misc_attr,
wait_dmevents, NULL))) {
condlog(0, "failed to create dmevent waiter thread: %d",
rc);
goto failed;
} else
dmevent_thr_started = true;
}
/*
* Start uevent listener early to catch events
*/
if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
condlog(0, "failed to create uevent thread: %d", rc);
goto failed;
} else
uevent_thr_started = true;
pthread_attr_destroy(&uevent_attr);
/*
* start threads
*/
if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
condlog(0,"failed to create checker loop thread: %d", rc);
goto failed;
} else
check_thr_started = true;
if ((rc = pthread_create(&purge_thr, &misc_attr, purgeloop, vecs))) {
condlog(0, "failed to create purge loop thread: %d", rc);
goto failed;
} else
purge_thr_started = true;
if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
condlog(0, "failed to create uevent dispatcher: %d", rc);
goto failed;
} else
uevq_thr_started = true;
if (fpin_marginal_paths) {
if ((rc = pthread_create(&fpin_thr, &misc_attr,
fpin_fabric_notification_receiver, NULL))) {
condlog(0, "failed to create the fpin receiver thread: %d", rc);
goto failed;
} else
fpin_thr_started = true;
if ((rc = pthread_create(&fpin_consumer_thr,
&misc_attr, fpin_els_li_consumer, vecs))) {
condlog(0, "failed to create the fpin consumer thread thread: %d", rc);
goto failed;
} else
fpin_consumer_thr_started = true;
}
pthread_attr_destroy(&misc_attr);
while (1) {
int rc = 0;
pthread_cleanup_push(config_cleanup, NULL);
pthread_mutex_lock(&config_lock);
while (running_state != DAEMON_CONFIGURE &&
running_state != DAEMON_SHUTDOWN &&
/*
* Check if another reconfigure request was scheduled
* while we last ran reconfigure().
* We have to test delayed_reconfig here
* to avoid a busy loop
*/
(reconfigure_pending == FORCE_RELOAD_NONE
|| delayed_reconfig))
pthread_cond_wait(&config_cond, &config_lock);
if (running_state != DAEMON_CONFIGURE &&
running_state != DAEMON_SHUTDOWN)
/* This sets running_state to DAEMON_CONFIGURE */
post_config_state__(DAEMON_CONFIGURE);
state = running_state;
pthread_cleanup_pop(1);
if (state == DAEMON_SHUTDOWN)
break;
/* handle DAEMON_CONFIGURE */
pthread_cleanup_push(cleanup_lock, &vecs->lock);
lock(&vecs->lock);
pthread_testcancel();
if (!need_to_delay_reconfig(vecs)) {
enum force_reload_types reload_type;
pthread_mutex_lock(&config_lock);
reload_type = reconfigure_pending == FORCE_RELOAD_YES ?
FORCE_RELOAD_YES : FORCE_RELOAD_WEAK;
reconfigure_pending = FORCE_RELOAD_NONE;
delayed_reconfig = false;
pthread_mutex_unlock(&config_lock);
rc = reconfigure(vecs, reload_type);
} else {
pthread_mutex_lock(&config_lock);
delayed_reconfig = true;
pthread_mutex_unlock(&config_lock);
condlog(3, "delaying reconfigure()");
}
lock_cleanup_pop(vecs->lock);
if (!rc)
post_config_state(DAEMON_IDLE);
else {
condlog(0, "fatal error applying configuration - aborting");
exit_daemon();
}
}
exit_code = 0;
failed:
condlog(2, "multipathd: shut down");
/* All cleanup is done in the cleanup_child() exit handler */
return sd_notify_exit(exit_code);
}
static void cleanup_close(int *pfd)
{
if (*pfd != -1 && *pfd != STDIN_FILENO && *pfd != STDOUT_FILENO &&
*pfd != STDERR_FILENO)
close(*pfd);
}
static int
daemonize(void)
{
int pid;
int dev_null_fd __attribute__((cleanup(cleanup_close))) = -1;
if( (pid = fork()) < 0){
fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
return -1;
}
else if (pid != 0)
return pid;
setsid();
if ( (pid = fork()) < 0)
fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
else if (pid != 0)
_exit(0);
if (chdir("/") < 0)
fprintf(stderr, "cannot chdir to '/', continuing\n");
dev_null_fd = open("/dev/null", O_RDWR);
if (dev_null_fd < 0){
fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
strerror(errno));
_exit(0);
}
if (dup2(dev_null_fd, STDIN_FILENO) < 0) {
fprintf(stderr, "cannot dup2 /dev/null to stdin : %s\n",
strerror(errno));
_exit(0);
}
if (dup2(dev_null_fd, STDOUT_FILENO) < 0) {
fprintf(stderr, "cannot dup2 /dev/null to stdout : %s\n",
strerror(errno));
_exit(0);
}
if (dup2(dev_null_fd, STDERR_FILENO) < 0) {
fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
strerror(errno));
_exit(0);
}
daemon_pid = getpid();
return 0;
}
int
main (int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int arg;
int err = 0;
int foreground = 0;
struct config *conf;
char *opt_k_arg = NULL;
bool opt_k = false;
ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
"Manipulated through RCU");
ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
"Suppress complaints about this scalar variable");
logsink = LOGSINK_SYSLOG;
/* make sure we don't lock any path */
if (chdir("/") < 0)
fprintf(stderr, "can't chdir to root directory : %s\n",
strerror(errno));
umask(umask(077) | 022);
pthread_cond_init_mono(&config_cond);
if (atexit(dm_lib_exit))
condlog(3, "failed to register exit handler for libdm");
libmultipath_init();
if (atexit(libmultipath_exit))
condlog(3, "failed to register exit handler for libmultipath");
libmp_udev_set_sync_support(0);
while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
switch(arg) {
case 'd':
foreground = 1;
if (logsink == LOGSINK_SYSLOG)
logsink = LOGSINK_STDERR_WITH_TIME;
break;
case 'v':
if (sizeof(optarg) > sizeof(char *) ||
!isdigit(optarg[0]))
exit(1);
libmp_verbosity = verbosity = atoi(optarg);
break;
case 's':
logsink = LOGSINK_STDERR_WITHOUT_TIME;
break;
case 'k':
opt_k = true;
opt_k_arg = optarg;
break;
case 'B':
bindings_read_only = 1;
break;
case 'n':
condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
break;
case 'w':
poll_dmevents = 0;
break;
default:
fprintf(stderr, "Invalid argument '-%c'\n",
optopt);
exit(1);
}
}
if (opt_k || optind < argc) {
char cmd[CMDSIZE];
char * s = cmd;
char * c = s;
logsink = LOGSINK_STDERR_WITH_TIME;
if (verbosity)
libmp_verbosity = verbosity;
conf = load_config(DEFAULT_CONFIGFILE);
if (!conf)
exit(1);
if (verbosity)
libmp_verbosity = verbosity;
uxsock_timeout = conf->uxsock_timeout;
memset(cmd, 0x0, CMDSIZE);
if (opt_k)
s = opt_k_arg;
else {
while (optind < argc) {
if (strchr(argv[optind], ' '))
c += snprintf(c, s + CMDSIZE - c,
"\"%s\" ", argv[optind]);
else
c += snprintf(c, s + CMDSIZE - c,
"%s ", argv[optind]);
optind++;
if (c >= s + CMDSIZE) {
fprintf(stderr, "multipathd command too large\n");
exit(1);
}
}
c += snprintf(c, s + CMDSIZE - c, "\n");
}
if (!s) {
char tmo_buf[16];
snprintf(tmo_buf, sizeof(tmo_buf), "%d",
uxsock_timeout + 100);
if (execl(BINDIR "/multipathc", "multipathc",
tmo_buf, NULL) == -1) {
condlog(0, "ERROR: failed to execute multipathc: %m");
err = 1;
}
} else
err = uxclnt(s, uxsock_timeout + 100);
free_config(conf);
return err;
}
if (getuid() != 0) {
fprintf(stderr, "need to be root\n");
exit(1);
}
if (foreground) {
if (!isatty(fileno(stdout)))
setbuf(stdout, NULL);
err = 0;
daemon_pid = getpid();
} else
err = daemonize();
if (err < 0)
/* error */
exit(1);
else if (err > 0)
/* parent dies */
exit(0);
else
/* child lives */
return (child(NULL));
}
static void check_prhold(struct multipath *mpp, struct path *pp)
{
struct prin_resp resp = {{{.prgeneration = 0}}};
int status;
if (mpp->prflag != PR_SET || mpp->prhold != PR_UNKNOWN)
return;
status = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RRES_SA, &resp, 0);
if (status != MPATH_PR_SUCCESS) {
condlog(0, "%s: pr in read reservation command failed: %d",
mpp->wwid, status);
return;
}
mpp->prhold = PR_UNSET;
if (!resp.prin_descriptor.prin_readresv.additional_length)
return;
if (memcmp(&mpp->reservation_key,
resp.prin_descriptor.prin_readresv.key, 8) == 0)
mpp->prhold = PR_SET;
}
void set_pr(struct multipath *mpp)
{
mpp->ever_registered_pr = true;
mpp->prflag = PR_SET;
}
void unset_pr(struct multipath *mpp)
{
mpp->prflag = PR_UNSET;
mpp->prhold = PR_UNSET;
mpp->sa_flags = 0;
memset(&mpp->old_pr_key, 0, 8);
}
/*
* Returns MPATH_PR_SUCCESS unless if fails to read the PR keys. If
* MPATH_PR_SUCCESS is returned, mpp->prflag will be either PR_SET or
* PR_UNSET.
*
* The number of found keys must be at least as large as *nr_keys,
* and if MPATH_PR_SUCCESS is returned and mpp->prflag is PR_SET after
* the call, *nr_keys will be set to the number of found keys. Otherwise
* if mpp->prflag is PR_UNSET it will be set to 0. If MPATH_PR_SUCCESS
* is not returned and mpp->prflag is not PR_UNSET, nr_keys will not be
* changed.
*/
static int update_map_pr(struct multipath *mpp, struct path *pp, unsigned int *nr_keys)
{
struct prin_resp resp;
unsigned int i, nr_found = 0;
int ret;
bool was_set = (mpp->prflag == PR_SET);
/* If pr is explicitly unset, it must be manually set */
if (mpp->prflag == PR_UNSET) {
*nr_keys = 0;
return MPATH_PR_SUCCESS;
}
if (!get_be64(mpp->reservation_key)) {
/* Nothing to do. Assuming pr mgmt feature is disabled*/
unset_pr(mpp);
condlog(was_set ? 2 : 4,
"%s: reservation_key not set in multipath.conf",
mpp->alias);
*nr_keys = 0;
return MPATH_PR_SUCCESS;
}
memset(&resp, 0, sizeof(resp));
ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, &resp, 0);
if (ret != MPATH_PR_SUCCESS) {
if (ret == MPATH_PR_ILLEGAL_REQ) {
unset_pr(mpp);
*nr_keys = 0;
}
condlog(0, "%s : pr in read keys service action failed Error=%d",
mpp->alias, ret);
return ret;
}
condlog(4, "%s: Multipath reservation_key: 0x%" PRIx64 " ", mpp->alias,
get_be64(mpp->reservation_key));
for (i = 0;
i < resp.prin_descriptor.prin_readkeys.additional_length / 8; i++) {
uint8_t *keyp = &resp.prin_descriptor.prin_readkeys.key_list[i * 8];
if (libmp_verbosity >= 4) {
condlog(4, "%s: PR IN READKEYS[%d] reservation key:",
mpp->alias, i);
dumpHex((char *)keyp, 8, 1);
}
/*
* If you are in the middle of updating a key (old_pr_key
* is set) check for either the new key or the old key,
* since you might be checking before any paths have
* updated their keys.
*/
if (!memcmp(&mpp->reservation_key, keyp, 8) ||
(get_be64(mpp->old_pr_key) &&
!memcmp(&mpp->old_pr_key, keyp, 8)))
nr_found++;
}
if (nr_found >= *nr_keys) {
set_pr(mpp);
condlog(was_set ? 3 : 2, "%s: %u keys found. prflag set.",
mpp->alias, nr_found);
*nr_keys = nr_found;
} else {
unset_pr(mpp);
condlog(was_set ? 1 : 3,
"%s: %u keys found. needed %u. prflag unset.",
mpp->alias, nr_found, *nr_keys);
*nr_keys = 0;
}
return MPATH_PR_SUCCESS;
}
/*
* This function is called with two numbers
*
* nr_keys_needed: the number of registered keys that should be
* seen for this device to know that the key has not been preempted while the
* path was getting registered. If 0 is passed in, update_mpath_pr is called
* before registering the key to figure out the number, assuming that at
* least one key must exist.
*
* nr_keys_wanted: Only used if nr_keys_needed is 0, so we don't know how
* many keys we currently have. If nr_keys_wanted in non-zero and the
* number of keys found by the initial call to update_map_pr() is at least
* as large as it, exit early, since we have all the keys we are expecting.
*
* The function returns the number of keys that are registered or 0 if
* it's unknown.
*/
static unsigned int
mpath_pr_event_handle(struct path *pp, unsigned int nr_keys_needed,
unsigned int nr_keys_wanted)
{
struct multipath *mpp = pp->mpp;
int ret;
struct prout_param_descriptor param = {.sa_flags = 0};
bool clear_reg = false;
if (pp->bus != SYSFS_BUS_SCSI) {
unset_pr(mpp);
return 0;
}
if (nr_keys_needed == 0) {
nr_keys_needed = 1;
if (update_map_pr(mpp, pp, &nr_keys_needed) != MPATH_PR_SUCCESS)
return 0;
if (nr_keys_wanted && nr_keys_wanted <= nr_keys_needed)
return nr_keys_needed;
}
check_prhold(mpp, pp);
if (mpp->prflag != PR_SET) {
if (!mpp->ever_registered_pr)
return 0;
/*
* This path may have been unusable and either the
* registration was cleared or the registered
* key was switched and then that new key was preempted.
* In either case, this path should not have a registration
* but it might still have one, just with a different
* key than mpp->reservation_key is currently set to.
* clear it to be sure.
*/
clear_reg = true;
}
if (!clear_reg) {
param.sa_flags = mpp->sa_flags;
memcpy(param.sa_key, &mpp->reservation_key, 8);
param.num_transportid = 0;
}
retry:
condlog(3, "%s registration for device %s:%s",
clear_reg ? "Clearing" : "Setting", pp->dev, pp->mpp->wwid);
ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, ¶m, 0);
if (ret != MPATH_PR_SUCCESS) {
condlog(0, "%s: %s reservation registration failed. Error: %d",
clear_reg ? "Clearing" : "Setting", pp->dev, ret);
} else if (!clear_reg) {
if (update_map_pr(mpp, pp, &nr_keys_needed) != MPATH_PR_SUCCESS)
return nr_keys_needed;
if (mpp->prflag != PR_SET) {
memset(¶m, 0, sizeof(param));
clear_reg = true;
goto retry;
}
return nr_keys_needed;
}
return 0;
}
|