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
|
/*
* (C) Copyright IBM Corp. 2004
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module: LVM2 Plugin
* File: evms2/engine/plugins/lvm2/options.c
*
* Routines for managing options.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <plugin.h>
#include "lvm2.h"
/**
* Create-container routines.
**/
/**
* create_container_init_task
*
* Initialize the acceptable-objects list and option-descriptor array for a
* create-container task.
**/
int create_container_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
list_anchor_t objects = NULL;
int i, rc;
LOG_ENTRY();
/* Get a list of acceptable objects for creating a new container. */
rc = get_available_objects(NULL, &objects);
if (rc) {
goto out;
}
EngFncs->merge_lists(context->acceptable_objects, objects, NULL, NULL);
/* Initialize the "Name" option. Allocate space for the string value
* now so we can just strcpy during set_option().
*/
i = LVM2_OPTION_CREATE_CONTAINER_NAME_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_CONTAINER_NAME_STR);
od->option[i].title = EngFncs->engine_strdup(_("Name for the new LVM2 container."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
od->option[i].value.s = EngFncs->engine_alloc(EVMS_NAME_SIZE + 1);
if (!od->option[i].value.s) {
rc = ENOMEM;
goto out;
}
/* Initialize the "Extent-Size" option. Wait until the objects have
* been selected during set_objects() to initialize the constraint-list.
*/
i = LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Extent-size for the new LVM2 container."));
od->option[i].tip = EngFncs->engine_strdup(_("Extent-size must be a power-of-2 and at least 8kB."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED | EVMS_OPTION_FLAGS_AUTOMATIC;
od->option[i].constraint_type = EVMS_Collection_List;
od->count = LVM2_OPTION_CREATE_CONTAINER_COUNT;
context->min_selected_objects = 1;
context->max_selected_objects = -1;
out:
EngFncs->destroy_list(objects);
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_container_set_objects
*
* Validate that the objects in the selected-objects list are valid for creating
* a new container. Make adjustments to the option-descriptor as appropriate.
**/
int create_container_set_objects(task_context_t *context,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
u_int64_t mask = 1, size = (u_int64_t) -1;
storage_object_t *object;
list_element_t iter;
int i;
LOG_ENTRY();
/* Determine the max allowed extent-size for this list of objects.
* Each object must be big enough for one extent plus metadata.
*/
LIST_FOR_EACH(context->selected_objects, iter, object) {
size = min(object->size, size);
}
/* Subtract space for the metadata. */
size -= (LVM2_LABEL_SCAN_SECTORS + LVM2_DEFAULT_MDA_SIZE);
/* Round down to a power-of-2. */
while (size & (size - 1)) {
size &= ~mask;
mask <<= 1;
}
LOG_DETAILS("Maximum allowed extent size is %"PRIu64".\n", size);
/* Update the extent-size entry in the option-descriptor. */
i = LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_IDX;
EngFncs->engine_free(od->option[i].constraint.list);
SET_POWER2_LIST64(od->option[i].constraint.list,
LVM2_MIN_EXTENT_SIZE, size);
od->option[i].value.ui64 = min(LVM2_DEFAULT_EXTENT_SIZE, size);
*effect |= EVMS_Effect_Reload_Options;
LOG_EXIT_INT(0);
return 0;
}
/**
* validate_vg_name
*
* Make sure the proposed container name is not already in use.
**/
static int validate_vg_name(char *vg_name, storage_container_t *disk_group)
{
char container_name[EVMS_NAME_SIZE+1];
int rc;
LOG_ENTRY();
if (strchr(vg_name, ' ')) {
LOG_WARNING("Container name (%s) cannot contain spaces.\n",
vg_name);
rc = EINVAL;
goto out;
}
vg_name_to_container_name(vg_name, container_name, disk_group);
rc = EngFncs->register_name(container_name);
if (rc) {
LOG_ERROR("Container name \"%s\" is already in use or "
"too long.\n", container_name);
goto out;
}
EngFncs->unregister_name(container_name);
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_container_set_option
*
* Validate the specified option-value for a create-container task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int create_container_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *object;
value_list_t *list;
u_int i;
int rc = EINVAL;
LOG_ENTRY();
switch (index) {
case LVM2_OPTION_CREATE_CONTAINER_NAME_IDX:
/* Make sure this name is not in use and isn't too long. */
object = EngFncs->first_thing(context->selected_objects, NULL);
rc = validate_vg_name(value->s, object->disk_group);
if (!rc) {
strncpy(od->option[index].value.s, value->s, EVMS_NAME_SIZE);
od->option[index].flags &= ~EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
}
break;
case LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_IDX:
/* Make sure the extent-size is a valid entry from the
* constraint-list. This will verify that the value is a
* power-of-2 and that the value is valid for all the
* selected-objects.
*/
list = od->option[index].constraint.list;
if (!list) {
break;
}
if (value->ui64 < list->value[0].ui64) {
value->ui64 = list->value[0].ui64;
*effect |= EVMS_Effect_Inexact;
} else if (value->ui64 > list->value[list->count - 1].ui64) {
value->ui64 = list->value[list->count - 1].ui64;
*effect |= EVMS_Effect_Inexact;
} else {
for (i = 0; i < list->count; i++) {
if (value->ui64 == list->value[i].ui64) {
break;
} else if (value->ui64 < list->value[i+1].ui64) {
value->ui64 = list->value[i].ui64;
*effect |= EVMS_Effect_Inexact;
break;
}
}
}
od->option[index].value.ui64 = value->ui64;
rc = 0;
break;
default:
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_container_parse_options
*
* Parse the option-values from the option-array.
**/
void create_container_parse_options(option_array_t *options,
u_int64_t *extent_size,
char **vg_name)
{
u_int i;
LOG_ENTRY();
/* Default values. */
*extent_size = LVM2_DEFAULT_EXTENT_SIZE;
*vg_name = NULL;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_CONTAINER_NAME_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_CONTAINER_NAME_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_CREATE_CONTAINER_NAME_IDX:
*vg_name = options->option[i].value.s;
LOG_DEBUG("Name option: %s\n", *vg_name);
break;
case LVM2_OPTION_CREATE_CONTAINER_EXTENT_SIZE_IDX:
*extent_size = options->option[i].value.ui64;
LOG_DEBUG("Extent-size option: %"PRIu64"\n", *extent_size);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* create_container_validate_options
*
* Verify that these options are valid for creating a new container.
**/
int create_container_validate_options(u_int64_t *extent_size,
char *vg_name,
list_anchor_t objects)
{
storage_object_t *object;
list_element_t iter;
u_int64_t mask = 1;
int rc;
LOG_ENTRY();
/* Check that the proposed name is valid. Get the disk-group
* pointer from the first object in the list.
*/
object = EngFncs->first_thing(objects, NULL);
rc = validate_vg_name(vg_name, object->disk_group);
if (rc) {
goto out;
}
/* Check that the extent-size is a power-of-2 and above the minimum
* allowed size. If it isn't, round up or down as appropriate.
*/
if (*extent_size & (*extent_size - 1)) {
while (*extent_size & (*extent_size - 1)) {
*extent_size &= ~mask;
mask <<= 1;
}
LOG_WARNING("Rounded extent-size down to %"PRIu64" sectors.\n",
*extent_size);
}
if (*extent_size < LVM2_MIN_EXTENT_SIZE) {
*extent_size = LVM2_MIN_EXTENT_SIZE;
LOG_WARNING("Rounded extent-size up to minimum allowed size of "
"%u sectors.\n", LVM2_MIN_EXTENT_SIZE);
}
/* Make sure all selected objects are large enough for the selected
* extent-size plus space for metadata.
*/
LIST_FOR_EACH(objects, iter, object) {
if (object->size < LVM2_MIN_PV_SIZE(*extent_size)) {
LOG_ERROR("Object %s is not large enough for extent-"
"size of %"PRIu64" sectors.\n",
object->name, *extent_size);
rc = ENOSPC;
}
}
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* Expand-container routines.
**/
/**
* expand_container_init_task
*
* Initialize the acceptable-objects list for an expand-container task. There
* are no options for expand-container, so no option-descriptor to initialize.
**/
int expand_container_init_task(task_context_t *context)
{
container_data_t *c_data = context->container->private_data;
list_anchor_t objects = NULL;
storage_object_t *object;
list_element_t iter;
int rc;
LOG_ENTRY();
LOG_DEBUG("Initializing an expand task for container %s.\n",
context->container->name);
/* Get a list of acceptable objects that can be added to the selected
* container. Each object must be big enough for at least one extent.
*/
rc = get_available_objects(context->container, &objects);
if (rc) {
goto out;
}
LIST_FOR_EACH(objects, iter, object) {
if (object->size >= LVM2_MIN_PV_SIZE(c_data->pe_size)) {
EngFncs->insert_thing(context->acceptable_objects,
object, INSERT_AFTER, NULL);
}
}
context->option_descriptors->count = 0;
context->min_selected_objects = 1;
context->max_selected_objects = -1;
out:
EngFncs->destroy_list(objects);
LOG_EXIT_INT(rc);
return rc;
}
/**
* expand_container_set_objects
*
* Validate that the objects in the selected-objects list can be added to the
* selected container.
**/
int expand_container_set_objects(task_context_t *context,
task_effect_t *effect)
{
container_data_t *c_data = context->container->private_data;
storage_object_t *object;
list_element_t iter;
int rc = 0;
LOG_ENTRY();
LOG_DEBUG("Setting objects for an expand of container %s.\n",
context->container->name);
LIST_FOR_EACH(context->selected_objects, iter, object){
if (object->size < LVM2_MIN_PV_SIZE(c_data->pe_size)) {
LOG_ERROR("Object %s is too small to be added to "
"container %s.\n", object->name,
context->container->name);
rc = ENOSPC;
break;
}
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* Shrink-container routines.
**/
/**
* shrink_container_init_task
*
* Initialize the acceptable-objects list for a shrink-container task. There
* are no options for shrink-container, so no option-descriptor to initialize.
**/
int shrink_container_init_task(task_context_t *context)
{
storage_container_t *container = context->container;
storage_object_t *object;
list_element_t iter;
int rc;
LOG_ENTRY();
LOG_DEBUG("Initializing a shrink task for container %s.\n",
container->name);
/* If the container only has one PV, it cannot be shrunk. */
if (EngFncs->list_count(container->objects_consumed) <= 1) {
LOG_DEBUG("Container %s only has one object. Cannot be "
"shrunk.\n", container->name);
rc = EBUSY;
goto out;
}
/* Get a list of acceptable objects that can be removed from the
* selected container. All objects that have no data regions
* mapping to them are valid.
*/
LIST_FOR_EACH(container->objects_consumed, iter, object) {
rc = can_remove_object(object);
if (!rc) {
EngFncs->insert_thing(context->acceptable_objects,
object, INSERT_AFTER, NULL);
}
}
context->option_descriptors->count = 0;
context->min_selected_objects = 1;
context->max_selected_objects = EngFncs->list_count(container->objects_consumed) - 1;
rc = 0;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* shrink_container_set_objects
*
* Validate that the objects in the selected-objects list can all be removed
* from the selected container.
**/
int shrink_container_set_objects(task_context_t *context,
task_effect_t *effect)
{
storage_container_t *container = context->container;
storage_object_t *object;
list_element_t iter;
int rc = 0;
LOG_ENTRY();
LOG_DEBUG("Setting objects for a shrink of container %s.\n",
container->name);
/* Cannot select all the objects in the container. */
if (EngFncs->list_count(context->selected_objects) >=
EngFncs->list_count(container->objects_consumed)) {
LOG_ERROR("Cannot remove all objects from container %s.\n",
container->name);
rc = EBUSY;
goto out;
}
/* Check each object in the selected-objects list. */
LIST_FOR_EACH(context->selected_objects, iter, object){
rc = can_remove_object(object);
if (rc) {
LOG_ERROR("Object %s cannot be removed from container "
"%s.\n", object->name, container->name);
goto out;
}
}
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* Create-region routines.
**/
/**
* create_region_init_task
*
* Initialize the acceptable-objects list and option-descriptor array for a
* create-region task.
**/
int create_region_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_container_t *container;
storage_object_t *freespace;
list_element_t iter;
int i, rc = 0;
LOG_ENTRY();
/* Add all non-zero-sized freespace regions
* to the acceptable-objects list.
*/
LIST_FOR_EACH(lvm2_containers, iter, container) {
freespace = get_freespace_region(container->objects_produced);
if (freespace && freespace->size > 0) {
EngFncs->insert_thing(context->acceptable_objects,
freespace, INSERT_AFTER, NULL);
}
}
/* No creates are possible if there's no freespace
* in any of the containers.
*/
if (EngFncs->list_empty(context->acceptable_objects)) {
rc = ENOSPC;
goto out;
}
/* Initialize the "Name" option. Allocate space for the string value
* now so we can just strcpy during set_option().
*/
i = LVM2_OPTION_CREATE_REGION_NAME_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_REGION_NAME_STR);
od->option[i].title = EngFncs->engine_strdup(_("Name for the new LVM2 region."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
od->option[i].value.s = EngFncs->engine_alloc(EVMS_NAME_SIZE + 1);
if (!od->option[i].value.s) {
rc = ENOMEM;
goto out;
}
/* Initialize the "Size" option. The constraint range and initial size
* cannot be filled in until a freespace region is chosen.
*/
i = LVM2_OPTION_CREATE_REGION_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_REGION_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Size for the new LVM2 region."));
od->option[i].tip = EngFncs->engine_strdup(_("Size must be a multiple of the container's "
"extent-size and cannot exceed the amount of "
"freespace in the container. If not, it will "
"be rounded down as appropriate."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
/* Initialize the "Stripes" option. The constraint range cannot be
* filled in until a freespace region is chosen. The initial number
* of stripes is always one.
*/
i = LVM2_OPTION_CREATE_REGION_STRIPES_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_REGION_STRIPES_STR);
od->option[i].title = EngFncs->engine_strdup(_("Number of stripes for the new LVM2 region."));
od->option[i].tip = EngFncs->engine_strdup(_("One stripe implies a linear region. Number "
"of stripes cannot exceed the number of "
"objects consumed by the container."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
od->option[i].value.ui64 = LVM2_DEFAULT_STRIPES;
/* Initialize the "Stripe-size" option. This option is inactive until
* the user selects more than one stripe. The constraint list cannot be
* filled in until a freespace region is chosen.
*/
i = LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Stripe-size for the new LVM2 region."));
od->option[i].tip = EngFncs->engine_strdup(_("Size of each stripe \"chunk\". Only available "
"when \"stripes\" option is greater than 1."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC |
EVMS_OPTION_FLAGS_INACTIVE;
/* Initialize the "PVs" option. The constraint list cannot be
* filled in until a freespace region is chosen.
*/
i = LVM2_OPTION_CREATE_REGION_PVS_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_CREATE_REGION_PVS_STR);
od->option[i].title = EngFncs->engine_strdup(_("Objects (PVs) to place the new LVM2 region on."));
od->option[i].tip = EngFncs->engine_strdup(_("Region will be allocated on only these objects. "
"Leave blank for automatic allocation."));
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
od->count = LVM2_OPTION_CREATE_REGION_COUNT;
context->min_selected_objects = 1;
context->max_selected_objects = 1;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_region_set_objects
*
* Validate that the freespace region in the selected-objects
* list is valid for creating a new region. Make adjustments
* to the option-descriptor as appropriate.
**/
int create_region_set_objects(task_context_t *context, task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *freespace, *object;
storage_container_t *container;
container_data_t *c_data;
list_element_t iter;
u_int32_t count;
int i, j = 0, rc = 0;
LOG_ENTRY();
/* There should only be one selected freespace region,
* so just get the first thing in the list.
*/
freespace = get_freespace_region(context->selected_objects);
if (!freespace) {
LOG_ERROR("No freespace region selected.\n");
rc = EINVAL;
goto out;
}
container = freespace->producing_container;
c_data = container->private_data;
/* Make sure there's space available in this container. */
if (freespace->size == 0) {
LOG_ERROR("No freespace avilable in container %s.\n",
container->name);
rc = ENOSPC;
goto out;
}
/* Initialize the "Size" option to the total available freespace. The
* allowable range of sizes goes from one extent to the total available
* freespace, in increments of extent-size.
*/
i = LVM2_OPTION_CREATE_REGION_SIZE_IDX;
od->option[i].value.ui64 = freespace->size;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range, c_data->pe_size,
freespace->size, c_data->pe_size);
/* Initialize the "Stripes" option's constraint-range to the total
* number of objects that have at least one unused extent.
*/
i = LVM2_OPTION_CREATE_REGION_STRIPES_IDX;
count = count_available_pvs(container->objects_consumed);
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range, 1, count, 1);
/* Initialize the "Stripe-size" option value and constraint-list. The
* stripe-size cannot be larger than the container's extent-size.
*/
i = LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX;
od->option[i].constraint_type = EVMS_Collection_List;
SET_POWER2_LIST64(od->option[i].constraint.list, LVM2_MIN_STRIPE_SIZE,
min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size));
od->option[i].value.ui64 = min(LVM2_DEFAULT_STRIPE_SIZE, c_data->pe_size);
/* Initialize the "PVs" option's constraint list. Add all objects that
* have any free extents.
*/
i = LVM2_OPTION_CREATE_REGION_PVS_IDX;
od->option[i].type = EVMS_Type_String;
od->option[i].constraint_type = EVMS_Collection_List;
od->option[i].flags |= EVMS_OPTION_FLAGS_VALUE_IS_LIST;
od->option[i].constraint.list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * count);
od->option[i].value.list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * count);
if (!od->option[i].constraint.list ||
!od->option[i].value.list) {
rc = ENOMEM;
goto out;
}
LIST_FOR_EACH(container->objects_consumed, iter, object) {
rc = count_available_extents_in_pv(object);
if (rc) {
od->option[i].constraint.list->value[j++].s = EngFncs->engine_strdup(object->name);
}
}
od->option[i].constraint.list->count = j;
od->option[i].value.list->count = 0;
*effect |= EVMS_Effect_Reload_Options;
rc = 0;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* validate_lv_name
*
* Make sure the proposed region name is not already in use.
**/
static int validate_lv_name(char *lv_name, char *container_name)
{
char region_name[EVMS_NAME_SIZE+1];
int rc;
LOG_ENTRY();
if (!lv_name) {
LOG_ERROR("No region name specified.\n");
rc = EINVAL;
goto out;
}
if (strchr(lv_name, ' ')) {
LOG_ERROR("Region name (%s) cannot contain spaces.\n", lv_name);
rc = EINVAL;
goto out;
}
lv_name_to_region_name(lv_name, region_name, container_name);
rc = EngFncs->register_name(region_name);
if (rc) {
LOG_ERROR("Region name \"%s\" is already in use or "
"too long.\n", region_name);
goto out;
}
EngFncs->unregister_name(region_name);
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_region_set_option
*
* Validate the specified option-value for a create-region task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int create_region_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *freespace;
storage_container_t *container;
container_data_t *c_data;
u_int64_t available_size, size_increment;
value_list_t *current_pvs;
list_anchor_t objects = NULL;
u_int32_t i, j, k;
int rc = 0;
LOG_ENTRY();
/* There should only be one selected freespace region,
* so just get the first thing in the list.
*/
freespace = get_freespace_region(context->selected_objects);
if (!freespace) {
LOG_ERROR("No freespace region selected.\n");
rc = EINVAL;
goto out;
}
container = freespace->producing_container;
c_data = container->private_data;
switch (index) {
/* Name */
case LVM2_OPTION_CREATE_REGION_NAME_IDX:
/* Make sure this name is not in use and that isn't too long.
* The selected name has no effect on any other options.
*/
rc = validate_lv_name(value->s, container->name);
if (!rc) {
LOG_DEBUG("Setting name option: %s\n", value->s);
strncpy(od->option[index].value.s, value->s, EVMS_NAME_SIZE);
od->option[index].flags &= ~EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
}
break;
/* Size */
case LVM2_OPTION_CREATE_REGION_SIZE_IDX:
/* Selected size must be within the constraint-range and must
* be a multiple of the constraint-range increment. The selected
* size has no effect on any other options.
*/
if (value->ui64 < od->option[index].constraint.range->min.ui64) {
value->ui64 = od->option[index].constraint.range->min.ui64;
} else if (value->ui64 > od->option[index].constraint.range->max.ui64) {
value->ui64 = od->option[index].constraint.range->max.ui64;
} else {
value->ui64 -= value->ui64 %
od->option[index].constraint.range->increment.ui64;
}
LOG_DEBUG("Setting size option: %"PRIu64" sectors\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
break;
/* Stripes */
case LVM2_OPTION_CREATE_REGION_STRIPES_IDX:
/* Selected stripes must be within the constraint-range. */
if (value->ui64 < od->option[index].constraint.range->min.ui64) {
value->ui64 = od->option[index].constraint.range->min.ui64;
} else if (value->ui64 > od->option[index].constraint.range->max.ui64) {
value->ui64 = od->option[index].constraint.range->max.ui64;
}
LOG_DEBUG("Setting stripes option: %"PRIu64"\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
/* If the number of stripes is greater than one,
* activate the stripe-size option.
*/
if (value->ui64 > 1) {
od->option[LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX].flags &= ~EVMS_OPTION_FLAGS_INACTIVE;
} else {
od->option[LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX].flags |= EVMS_OPTION_FLAGS_INACTIVE;
}
/* Adjust the "size" option's constraint range, which must be
* a multiple of the extent-size and the number of stripes. Then
* set the "size" option to re-validate the "size" value.
*/
current_pvs = od->option[LVM2_OPTION_CREATE_REGION_PVS_IDX].value.list;
objects = pv_names_to_list(current_pvs, container);
size_increment = c_data->pe_size * value->ui64;
available_size = count_available_extents_in_pvs(objects) * c_data->pe_size;
available_size -= available_size % size_increment;
EngFncs->destroy_list(objects);
/* FIXME: This doesn't take into account the actual allocation
* of extents, which might further limit the available
* size of the new region.
*/
i = LVM2_OPTION_CREATE_REGION_SIZE_IDX;
EngFncs->engine_free(od->option[i].constraint.range);
SET_RANGE64(od->option[i].constraint.range,
size_increment, available_size, size_increment);
rc = create_region_set_option(context, i,
&(od->option[i].value), effect);
break;
/* Stripe-Size */
case LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX:
/* The stripe-size must be within the constraint-list. The
* stripe-size has no effect on any other options.
*/
for (i = 0; i < od->option[index].constraint.list->count; i++) {
if (value->ui64 == od->option[index].constraint.list->value[i].ui64) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
value->ui64 = min(LVM2_DEFAULT_STRIPE_SIZE, c_data->pe_size);
}
LOG_DEBUG("Setting stripe-size option: %"PRIu64"\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
break;
/* PVs */
case LVM2_OPTION_CREATE_REGION_PVS_IDX:
/* Each selected PV must be in the constraint-list. */
for (i = 0, k = 0; i < value->list->count; i++) {
for (j = 0; j < od->option[index].constraint.list->count; j++) {
rc = strcmp(value->list->value[i].s,
od->option[index].constraint.list->value[j].s);
if (!rc) {
LOG_DEBUG("Setting PVs option entry: %s\n", value->list->value[i].s);
od->option[index].value.list->value[k++].s = EngFncs->engine_strdup(value->list->value[i].s);
break;
}
}
}
od->option[index].value.list->count = k;
/* The "stripes" option's constraint-range must be adjusted
* based on the number of selected PVs. Then set the "stripes"
* option to re-validate the "stripes" value, which will in
* turn adjust and re-validate the "size" option.
*/
i = LVM2_OPTION_CREATE_REGION_STRIPES_IDX;
if (!k) {
k = od->option[index].constraint.list->count;
}
EngFncs->engine_free(od->option[i].constraint.range);
SET_RANGE64(od->option[i].constraint.range, 1, k, 1);
rc = create_region_set_option(context, i,
&(od->option[i].value), effect);
break;
default:
rc = EINVAL;
break;
}
*effect |= EVMS_Effect_Reload_Options;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* create_region_parse_options
*
* Parse the option-values from the option-array.
**/
void create_region_parse_options(storage_container_t *container,
option_array_t *options,
char **lv_name,
u_int64_t *size,
u_int64_t *stripes,
u_int64_t *stripe_size,
list_anchor_t *objects)
{
container_data_t *c_data = container->private_data;
storage_object_t *freespace;
u_int i;
LOG_ENTRY();
/* Default values. */
freespace = get_freespace_region(container->objects_produced);
*lv_name = NULL;
*size = freespace->size;
*stripes = 1;
*stripe_size = 0;
*objects = NULL;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_REGION_NAME_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_REGION_NAME_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_REGION_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_REGION_SIZE_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_REGION_STRIPES_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_REGION_STRIPES_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_CREATE_REGION_PVS_STR)) {
options->option[i].number = LVM2_OPTION_CREATE_REGION_PVS_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_CREATE_REGION_NAME_IDX:
*lv_name = options->option[i].value.s;
LOG_DEBUG("Name option: %s\n", *lv_name);
break;
case LVM2_OPTION_CREATE_REGION_SIZE_IDX:
*size = options->option[i].value.ui64;
LOG_DEBUG("Size option: %"PRIu64"\n", *size);
break;
case LVM2_OPTION_CREATE_REGION_STRIPES_IDX:
*stripes = options->option[i].value.ui64;
LOG_DEBUG("Stripes option: %"PRIu64"\n", *stripes);
break;
case LVM2_OPTION_CREATE_REGION_STRIPE_SIZE_IDX:
*stripe_size = options->option[i].value.ui64;
LOG_DEBUG("Stripe-size option: %"PRIu64"\n", *stripe_size);
break;
case LVM2_OPTION_CREATE_REGION_PVS_IDX:
*objects = pv_names_to_list(options->option[i].value.list,
container);
LOG_DEBUG("PVs option.\n");
break;
default:
break;
}
}
/* More defaults. */
if (!*objects) {
*objects = pv_names_to_list(NULL, container);
}
if (*stripes > 1 && !*stripe_size) {
*stripe_size = min(LVM2_DEFAULT_STRIPE_SIZE, c_data->pe_size);
}
LOG_EXIT_VOID();
}
/**
* create_region_validate_options
*
* Verify that these options are valid for creating a new region in this
* container. This does not validate that an approprite extent-allocation
* exists. It simply verifies that each option adheres to the proper
* limitations.
**/
int create_region_validate_options(storage_container_t *container,
char *lv_name,
u_int64_t *size,
u_int64_t *stripes,
u_int64_t *stripe_size,
list_anchor_t objects)
{
container_data_t *c_data = container->private_data;
storage_object_t *object;
list_element_t iter1, iter2;
u_int64_t increment, count, free_extents = 0;
int rc;
LOG_ENTRY();
/* Validate the region's name. */
rc = validate_lv_name(lv_name, container->name);
if (rc) {
goto out;
}
/* All selected objects must have at least one free extent. Simply
* remove all objects that have no freespace. Count the total number
* of free-extents while we're here.
*/
LIST_FOR_EACH_SAFE(objects, iter1, iter2, object) {
count = count_available_extents_in_pv(object);
if (!count) {
EngFncs->remove_element(iter1);
LOG_DEBUG("Removing %s from objects list - no free "
"extents.\n", object->name);
}
free_extents += count;
}
if (!free_extents) {
LOG_ERROR("No freespace in list of selected objects.\n");
rc = ENOSPC;
goto out;
}
/* Number of stripes cannot exceed the number of selected objects. */
count = EngFncs->list_count(objects);
if (*stripes < 1) {
*stripes = 1;
LOG_DEBUG("Rounding number of stripes up to %"PRIu64".\n",
*stripes);
} else if (*stripes > count) {
*stripes = count;
LOG_DEBUG("Rounding number of stripes down to %"PRIu64".\n",
*stripes);
}
/* Size must be a multiple of the extent-size
* and the number of stripes.
*/
increment = *stripes * c_data->pe_size;
if (*size < increment) {
*size = increment;
LOG_DEBUG("Rounding size up to %"PRIu64".\n", *size);
} else if (*size > free_extents * c_data->pe_size) {
*size = free_extents * c_data->pe_size;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
} else if (*size % increment) {
*size -= *size % increment;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
}
/* If the region is striped, stripe-size must
* be in the proper range and a power-of-2.
*/
if (*stripes > 1) {
if (*stripe_size < LVM2_MIN_STRIPE_SIZE) {
*stripe_size = LVM2_MIN_STRIPE_SIZE;
LOG_DEBUG("Rounding stripe-size up to %"PRIu64".\n",
*stripe_size);
} else if (*stripe_size > min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size)) {
*stripe_size = min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size);
LOG_DEBUG("Rounding stripe-size down to %"PRIu64".\n",
*stripe_size);
} else if (*stripe_size & (*stripe_size - 1)) {
u_int64_t mask = 1;
while (*stripe_size & (*stripe_size - 1)) {
*stripe_size &= ~mask;
mask <<= 1;
}
LOG_DEBUG("Rounding stripe-size down to %"PRIu64".\n",
*stripe_size);
}
} else {
*stripe_size = 0;
}
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* Expand-region routines.
**/
/**
* expand_region_init_task
*
* Initialize the acceptable-objects list and option-descriptor array for an
* expand-region task.
**/
int expand_region_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *object, *region = context->object;
storage_container_t *container = region->producing_container;
container_data_t *c_data = container->private_data;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map;
list_element_t iter;
u_int64_t count, available_size, increment;
u_int64_t available_objects, stripes;
u_int32_t i, j = 0;
int rc;
LOG_ENTRY();
LOG_DEBUG("Initializing expand task for region %s.\n", region->name);
rc = can_expand_region(region);
if (rc) {
goto out;
}
available_objects = count_available_pvs(container->objects_consumed);
available_size = count_available_extents_in_pvs(container->objects_consumed) * c_data->pe_size;
r_map = EngFncs->last_thing(r_data->mappings, NULL);
if (!available_objects) {
/* No PVs with free extents. Expand is not possible. */
LOG_WARNING("No freespace in container %s.\n", container->name);
rc = ENOSPC;
goto out;
}
/* Ask the engine if it's ok to expand by the max amount. */
rc = EngFncs->can_expand_by(region, &available_size);
if (rc) {
if (rc != EAGAIN) {
LOG_ERROR("Expand of region %s rejected by the "
"engine.\n", region->name);
goto out;
}
rc = 0;
/* Make sure the size is still a multiple of the PE-size. */
LOG_DEBUG("Engine will only allow max expand size of %"PRIu64
" sectors.\n", available_size);
available_size -= available_size % c_data->pe_size;
LOG_DEBUG("Reduced max expand size to %"PRIu64" sectors.\n",
available_size);
}
/* Initialize the "PVs" option. */
i = LVM2_OPTION_EXPAND_REGION_PVS_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_EXPAND_REGION_PVS_STR);
od->option[i].title = EngFncs->engine_strdup(_("Objects (PVs) to use for the new portion of the region."));
od->option[i].tip = EngFncs->engine_strdup(_("Region will be expanded only onto these objects. "
"Leave blank for automatic allocation."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC |
EVMS_OPTION_FLAGS_VALUE_IS_LIST;
od->option[i].constraint_type = EVMS_Collection_List;
od->option[i].constraint.list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * available_objects);
od->option[i].value.list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * available_objects);
if (!od->option[i].constraint.list || !od->option[i].value.list) {
rc = ENOMEM;
goto out;
}
LIST_FOR_EACH(container->objects_consumed, iter, object) {
count = count_available_extents_in_pv(object);
if (count) {
od->option[i].constraint.list->value[j++].s = EngFncs->engine_strdup(object->name);
}
}
od->option[i].constraint.list->count = j;
od->option[i].value.list->count = 0;
/* Initialize the "Stripes" option. The initial number of stripes
* should be the same as in the last region-mapping (or the number
* of objects that have free extents, if that's less).
*/
i = LVM2_OPTION_EXPAND_REGION_STRIPES_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_EXPAND_REGION_STRIPES_STR);
od->option[i].title = EngFncs->engine_strdup(_("Number of stripes for the new portion of the region."));
od->option[i].tip = EngFncs->engine_strdup(_("One stripe implies a linear region. Number "
"of stripes cannot exceed the number of "
"objects consumed by the container."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range, 1, available_objects, 1);
od->option[i].value.ui64 = stripes
= min(r_map->stripe_count, available_objects);
/* Initialize the "Size" option. */
increment = stripes * c_data->pe_size;
available_size -= available_size % increment;
i = LVM2_OPTION_EXPAND_REGION_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_EXPAND_REGION_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Extra size for the LVM2 region."));
od->option[i].tip = EngFncs->engine_strdup(_("Extra size must be a multiple of the container's "
"extent-size and cannot exceed the amount of "
"freespace in the container. If not, it will "
"be rounded down as appropriate."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range, increment, available_size, increment);
od->option[i].value.ui64 = available_size;
/* Initialize the "Stripe-size" option. This option is
* inactive if there's only one stripe.
*/
i = LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Stripe-size for the new portion of the region."));
od->option[i].tip = EngFncs->engine_strdup(_("Size of each stripe \"chunk\". Only available "
"when \"stripes\" option is greater than 1."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC |
(stripes == 1) ? EVMS_OPTION_FLAGS_INACTIVE : 0;
od->option[i].constraint_type = EVMS_Collection_List;
SET_POWER2_LIST64(od->option[i].constraint.list, LVM2_MIN_STRIPE_SIZE,
min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size));
od->option[i].value.ui64 = r_map->stripe_size;
od->count = LVM2_OPTION_EXPAND_REGION_COUNT;
context->min_selected_objects = 0;
context->max_selected_objects = 0;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* expand_region_set_option
*
* Validate the specified option-value for an expand-region task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int expand_region_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_container_t *container = context->object->producing_container;
container_data_t *c_data = container->private_data;
value_list_t *current_pvs;
list_anchor_t objects;
u_int64_t size_increment, available_size;
u_int32_t i, j, k;
int rc = 0;
LOG_ENTRY();
switch (index) {
/* Size */
case LVM2_OPTION_EXPAND_REGION_SIZE_IDX:
/* Selected size must be within the constraint-range and must
* be a multiple of the constraint-range increment. The selected
* size has no effect on any other options.
*/
if (value->ui64 < od->option[index].constraint.range->min.ui64) {
value->ui64 = od->option[index].constraint.range->min.ui64;
} else if (value->ui64 > od->option[index].constraint.range->max.ui64) {
value->ui64 = od->option[index].constraint.range->max.ui64;
} else {
value->ui64 -= value->ui64 %
od->option[index].constraint.range->increment.ui64;
}
LOG_DEBUG("Setting size option: %"PRIu64" sectors\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
break;
/* Stripes */
case LVM2_OPTION_EXPAND_REGION_STRIPES_IDX:
/* Selected stripes must be within the constraint-range. */
if (value->ui64 < od->option[index].constraint.range->min.ui64) {
value->ui64 = od->option[index].constraint.range->min.ui64;
} else if (value->ui64 > od->option[index].constraint.range->max.ui64) {
value->ui64 = od->option[index].constraint.range->max.ui64;
}
LOG_DEBUG("Setting stripes option: %"PRIu64"\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
/* If the number of stripes is greater than one,
* activate the stripe-size option.
*/
if (value->ui64 > 1) {
od->option[LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX].flags &= ~EVMS_OPTION_FLAGS_INACTIVE;
} else {
od->option[LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX].flags |= EVMS_OPTION_FLAGS_INACTIVE;
}
/* Adjust the "size" option's constraint range, which must be
* a multiple of the extent-size and the number of stripes. Then
* set the "size" option to re-validate the "size" value.
*/
current_pvs = od->option[LVM2_OPTION_EXPAND_REGION_PVS_IDX].value.list;
objects = pv_names_to_list(current_pvs, container);
size_increment = c_data->pe_size * value->ui64;
available_size = count_available_extents_in_pvs(objects) * c_data->pe_size;
available_size -= available_size % size_increment;
EngFncs->destroy_list(objects);
/* FIXME: This doesn't take into account the actual allocation
* of extents, which might further limit the available
* size of the new region.
*/
i = LVM2_OPTION_EXPAND_REGION_SIZE_IDX;
EngFncs->engine_free(od->option[i].constraint.range);
SET_RANGE64(od->option[i].constraint.range,
size_increment, available_size, size_increment);
rc = expand_region_set_option(context, i,
&(od->option[i].value), effect);
break;
/* Stripe-Size */
case LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX:
/* The stripe-size must be within the constraint-list. The
* stripe-size has no effect on any other options.
*/
for (i = 0; i < od->option[index].constraint.list->count; i++) {
if (value->ui64 == od->option[index].constraint.list->value[i].ui64) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
value->ui64 = min(LVM2_DEFAULT_STRIPE_SIZE, c_data->pe_size);
}
LOG_DEBUG("Setting stripe-size option: %"PRIu64"\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
break;
/* PVs */
case LVM2_OPTION_EXPAND_REGION_PVS_IDX:
/* Each selected PV must be in the constraint-list. */
for (i = 0, k = 0; i < value->list->count; i++) {
for (j = 0; j < od->option[index].constraint.list->count; j++) {
rc = strcmp(value->list->value[i].s,
od->option[index].constraint.list->value[j].s);
if (!rc) {
LOG_DEBUG("Setting PVs option entry: %s\n", value->list->value[i].s);
od->option[index].value.list->value[k++].s = EngFncs->engine_strdup(value->list->value[i].s);
break;
}
}
}
od->option[index].value.list->count = k;
/* The "stripes" option's constraint-range must be adjusted
* based on the number of selected PVs. Then set the "stripes"
* option to re-validate the "stripes" value, which will in
* turn adjust and re-validate the "size" option.
*/
i = LVM2_OPTION_EXPAND_REGION_STRIPES_IDX;
if (!k) {
k = od->option[index].constraint.list->count;
}
EngFncs->engine_free(od->option[i].constraint.range);
SET_RANGE64(od->option[i].constraint.range, 1, k, 1);
rc = expand_region_set_option(context, i,
&(od->option[i].value), effect);
break;
default:
rc = EINVAL;
break;
}
*effect |= EVMS_Effect_Reload_Options;
LOG_EXIT_INT(rc);
return rc;
}
/**
* expand_region_parse_options
*
* Parse the option-values from the option-array.
**/
void expand_region_parse_options(storage_object_t *region,
option_array_t *options,
u_int64_t *size,
u_int64_t *stripes,
u_int64_t *stripe_size,
list_anchor_t *objects)
{
storage_container_t *container = region->producing_container;
container_data_t *c_data = container->private_data;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map;
storage_object_t *freespace;
u_int i;
LOG_ENTRY();
/* Default values. */
freespace = get_freespace_region(container->objects_produced);
r_map = EngFncs->last_thing(r_data->mappings, NULL);
*size = freespace->size;
*stripes = r_map->stripe_count;
*stripe_size = r_map->stripe_size;
*objects = NULL;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_EXPAND_REGION_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_EXPAND_REGION_SIZE_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_EXPAND_REGION_STRIPES_STR)) {
options->option[i].number = LVM2_OPTION_EXPAND_REGION_STRIPES_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_EXPAND_REGION_PVS_STR)) {
options->option[i].number = LVM2_OPTION_EXPAND_REGION_PVS_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_EXPAND_REGION_SIZE_IDX:
*size = options->option[i].value.ui64;
LOG_DEBUG("Size option: %"PRIu64"\n", *size);
break;
case LVM2_OPTION_EXPAND_REGION_STRIPES_IDX:
*stripes = options->option[i].value.ui64;
LOG_DEBUG("Stripes option: %"PRIu64"\n", *stripes);
break;
case LVM2_OPTION_EXPAND_REGION_STRIPE_SIZE_IDX:
*stripe_size = options->option[i].value.ui64;
LOG_DEBUG("Stripe-size option: %"PRIu64"\n", *stripe_size);
break;
case LVM2_OPTION_EXPAND_REGION_PVS_IDX:
*objects = pv_names_to_list(options->option[i].value.list,
container);
LOG_DEBUG("PVs option.\n");
break;
default:
break;
}
}
/* More defaults. */
if (!*objects) {
*objects = pv_names_to_list(NULL, container);
}
if (*stripes > 1 && !*stripe_size) {
*stripe_size = min(LVM2_DEFAULT_STRIPE_SIZE, c_data->pe_size);
}
LOG_EXIT_VOID();
}
/**
* expand_region_validate_options
*
* Verify that these options are valid for expanding this region. This does not
* validate that an approprite extent-allocation exists. It simply verifies
* that each option adheres to the proper limitations.
**/
int expand_region_validate_options(storage_object_t *region,
u_int64_t *size,
u_int64_t *stripes,
u_int64_t *stripe_size,
list_anchor_t objects)
{
storage_container_t *container = region->producing_container;
container_data_t *c_data = container->private_data;
storage_object_t *object;
list_element_t iter1, iter2;
u_int64_t increment, count, free_extents = 0;
int rc = 0;
LOG_ENTRY();
/* All selected objects must have at least one free extent. Simply
* remove all objects that have no freespace. Count the total number
* of free-extents while we're here.
*/
LIST_FOR_EACH_SAFE(objects, iter1, iter2, object) {
count = count_available_extents_in_pv(object);
if (!count) {
EngFncs->remove_element(iter1);
LOG_DEBUG("Removing %s from objects list - no free "
"extents.\n", object->name);
}
free_extents += count;
}
if (!free_extents) {
LOG_ERROR("No freespace in list of selected objects.\n");
rc = ENOSPC;
goto out;
}
/* Number of stripes cannot exceed the number of selected objects. */
count = EngFncs->list_count(objects);
if (*stripes < 1) {
*stripes = 1;
LOG_DEBUG("Rounding number of stripes up to %"PRIu64".\n",
*stripes);
} else if (*stripes > count) {
*stripes = count;
LOG_DEBUG("Rounding number of stripes down to %"PRIu64".\n",
*stripes);
}
/* Size must be a multiple of the extent-size
* and the number of stripes.
*/
increment = *stripes * c_data->pe_size;
if (*size < increment) {
*size = increment;
LOG_DEBUG("Rounding size up to %"PRIu64".\n", *size);
} else if (*size > free_extents * c_data->pe_size) {
*size = free_extents * c_data->pe_size;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
} else if (*size % increment) {
*size -= *size % increment;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
}
/* If the region is striped, stripe-size must
* be in the proper range and a power-of-2.
*/
if (*stripes > 1) {
if (*stripe_size < LVM2_MIN_STRIPE_SIZE) {
*stripe_size = LVM2_MIN_STRIPE_SIZE;
LOG_DEBUG("Rounding stripe-size up to %"PRIu64".\n",
*stripe_size);
} else if (*stripe_size > min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size)) {
*stripe_size = min(LVM2_MAX_STRIPE_SIZE, c_data->pe_size);
LOG_DEBUG("Rounding stripe-size down to %"PRIu64".\n",
*stripe_size);
} else if (*stripe_size & (*stripe_size - 1)) {
u_int64_t mask = 1;
while (*stripe_size & (*stripe_size - 1)) {
*stripe_size &= ~mask;
mask <<= 1;
}
LOG_DEBUG("Rounding stripe-size down to %"PRIu64".\n",
*stripe_size);
}
} else {
*stripe_size = 0;
}
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* Shrink-region routines.
**/
/**
* shrink_region_init_task
*
* Initialize the acceptable-objects list and option-descriptor array for a
* shrink-region task.
**/
int shrink_region_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *region = context->object;
storage_container_t *container = region->producing_container;
container_data_t *c_data = container->private_data;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map;
u_int64_t max_delta, min_delta;
u_int32_t i;
int rc;
LOG_ENTRY();
LOG_DEBUG("Initializing shrink task for region %s.\n", region->name);
rc = can_shrink_region(region);
if (rc) {
goto out;
}
r_map = EngFncs->last_thing(r_data->mappings, NULL);
min_delta = c_data->pe_size * r_map->stripe_count;
r_map = EngFncs->first_thing(r_data->mappings, NULL);
max_delta = region->size - c_data->pe_size * r_map->stripe_count;
LOG_DEBUG("Allowable range for shrink size: %"PRIu64" to %"PRIu64
" sectors in %"PRIu64" sector increments.\n",
min_delta, max_delta, c_data->pe_size);
/* Ask the engine if it's ok to shrink by the max amount. */
rc = EngFncs->can_shrink_by(region, &max_delta);
if (rc) {
if (rc != EAGAIN) {
LOG_ERROR("Shrink of region %s rejected by the "
"engine.\n", region->name);
goto out;
}
rc = 0;
/* Make sure max_delta is still a multiple of the PE-size. */
LOG_DEBUG("Engine will only allow max shrink size of %"PRIu64
" sectors.\n", max_delta);
max_delta -= max_delta %
(c_data->pe_size * r_map->stripe_count);
LOG_DEBUG("Reduced max shrink size to %"PRIu64" sectors.\n",
max_delta);
}
/* Initialize the "Size" option. */
i = LVM2_OPTION_SHRINK_REGION_SIZE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_SHRINK_REGION_SIZE_STR);
od->option[i].title = EngFncs->engine_strdup(_("Size to remove from the LVM2 region."));
od->option[i].tip = EngFncs->engine_strdup(_("Removed size must be a multiple of the "
"container's extent-size. If not, it will "
"be rounded down as appropriate."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].unit = EVMS_Unit_Sectors;
od->option[i].flags = EVMS_OPTION_FLAGS_NOT_REQUIRED |
EVMS_OPTION_FLAGS_AUTOMATIC;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range,
min_delta, max_delta, c_data->pe_size);
od->option[i].value.ui64 = min_delta;
od->count = LVM2_OPTION_SHRINK_REGION_COUNT;
context->min_selected_objects = 0;
context->max_selected_objects = 0;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* shrink_region_set_option
*
* Validate the specified option-value for a shrink-region task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int shrink_region_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
int rc = 0;
LOG_ENTRY();
switch (index) {
case LVM2_OPTION_SHRINK_REGION_SIZE_IDX:
/* Selected size must be within the constraint-range and
* must be a multiple of the constraint-range increment.
*/
if (value->ui64 < od->option[index].constraint.range->min.ui64) {
value->ui64 = od->option[index].constraint.range->min.ui64;
} else if (value->ui64 > od->option[index].constraint.range->max.ui64) {
value->ui64 = od->option[index].constraint.range->max.ui64;
} else {
value->ui64 -= value->ui64 %
od->option[index].constraint.range->increment.ui64;
}
LOG_DEBUG("Setting size option: %"PRIu64" sectors\n", value->ui64);
od->option[index].value.ui64 = value->ui64;
break;
default:
rc = EINVAL;
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* shrink_region_parse_options
*
* Parse the option-values from the option-array.
**/
void shrink_region_parse_options(storage_object_t *region,
option_array_t *options,
u_int64_t *size)
{
container_data_t *c_data = region->producing_container->private_data;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map = EngFncs->last_thing(r_data->mappings, NULL);
u_int i;
LOG_ENTRY();
/* Default values. */
*size = c_data->pe_size * r_map->stripe_count;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_SHRINK_REGION_SIZE_STR)) {
options->option[i].number = LVM2_OPTION_SHRINK_REGION_SIZE_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_SHRINK_REGION_SIZE_IDX:
*size = options->option[i].value.ui64;
LOG_DEBUG("Size option: %"PRIu64"\n", *size);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* shrink_region_validate_options
*
* Verify that these options are valid for shrinking this region.
**/
int shrink_region_validate_options(storage_object_t *region,
u_int64_t *size)
{
container_data_t *c_data = region->producing_container->private_data;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map = EngFncs->last_thing(r_data->mappings, NULL);
int rc = 0;
LOG_ENTRY();
/* Size must be a multiple of the extent-size and less than the
* size of the entire region.
*/
if (*size < c_data->pe_size * r_map->stripe_count) {
*size = c_data->pe_size * r_map->stripe_count;
LOG_DEBUG("Rounding size up to %"PRIu64".\n", *size);
} else if (*size > region->size - c_data->pe_size) {
*size = region->size - c_data->pe_size;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
} else if (*size % c_data->pe_size) {
*size -= *size % c_data->pe_size;
LOG_DEBUG("Rounding size down to %"PRIu64".\n", *size);
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* Set-region-info routines.
**/
/**
* set_region_info_init_task
*
* Initialize the option-descriptor array for a set-region-info task. There
* is no acceptable-objects list for set-info.
**/
int set_region_info_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *region = context->object;
u_int32_t i;
LOG_ENTRY();
LOG_DEBUG("Initializing set-info task for region %s.\n", region->name);
/* No info to set for freespace regions. */
if (region->data_type != DATA_TYPE) {
LOG_ERROR("No information to set for freespace region %s.\n",
region->name);
LOG_EXIT_INT(EINVAL);
return EINVAL;
}
/* Initialize the "Name" option. */
i = LVM2_OPTION_SET_REGION_INFO_NAME_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_SET_REGION_INFO_NAME_STR);
od->option[i].title = EngFncs->engine_strdup(_("New name for this LVM2 region."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
od->option[i].value.s = EngFncs->engine_alloc(EVMS_NAME_SIZE+1);
od->count = LVM2_OPTION_SET_REGION_INFO_COUNT;
context->min_selected_objects = 0;
context->max_selected_objects = 0;
LOG_EXIT_INT(0);
return 0;
}
/**
* set_region_info_set_option
*
* Validate the specified option-value for a set-region-info task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int set_region_info_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
storage_container_t *container = context->object->producing_container;
option_desc_array_t *od = context->option_descriptors;
int rc;
LOG_ENTRY();
switch (index) {
case LVM2_OPTION_SET_REGION_INFO_NAME_IDX:
/* Make sure this name is not in use and that isn't too long. */
rc = validate_lv_name(value->s, container->name);
if (!rc) {
LOG_DEBUG("Setting name option: %s\n", value->s);
strncpy(od->option[index].value.s, value->s, EVMS_NAME_SIZE);
od->option[index].flags &= ~EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
}
break;
default:
rc = EINVAL;
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* set_region_info_parse_options
*
* Parse the option-values from the option-array.
**/
void set_region_info_parse_options(storage_object_t *region,
option_array_t *options,
char **lv_name)
{
u_int i;
LOG_ENTRY();
/* Default values. */
*lv_name = NULL;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_SET_REGION_INFO_NAME_STR)) {
options->option[i].number = LVM2_OPTION_SET_REGION_INFO_NAME_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_SET_REGION_INFO_NAME_IDX:
*lv_name = options->option[i].value.s;
LOG_DEBUG("Name option: %s\n", *lv_name);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* set_region_info_validate_options
*
* Verify that these options are valid for setting info for this region.
**/
int set_region_info_validate_options(storage_object_t *region,
char *lv_name)
{
int rc;
LOG_ENTRY();
/* Validate the new region name. */
rc = validate_lv_name(lv_name, region->producing_container->name);
LOG_EXIT_INT(rc);
return rc;
}
/**
* Set-container-info routines.
**/
/**
* set_container_info_init_task
*
* Initialize the option-descriptor array for a set-container-info task. There
* is no acceptable-objects list for set-info.
**/
int set_container_info_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_container_t *container = context->container;
u_int32_t i;
LOG_ENTRY();
LOG_DEBUG("Initializing set-info task for container %s.\n",
container->name);
/* Initialize the "Name" option. */
i = LVM2_OPTION_SET_CONTAINER_INFO_NAME_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_SET_CONTAINER_INFO_NAME_STR);
od->option[i].title = EngFncs->engine_strdup(_("New name for this LVM2 container."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].flags = EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
od->option[i].value.s = EngFncs->engine_alloc(EVMS_NAME_SIZE+1);
od->count = LVM2_OPTION_SET_CONTAINER_INFO_COUNT;
context->min_selected_objects = 0;
context->max_selected_objects = 0;
LOG_EXIT_INT(0);
return 0;
}
/**
* set_container_info_set_option
*
* Validate the specified option-value for a set-container-info task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int set_container_info_set_option(task_context_t *context,
u_int32_t index,
value_t *value,
task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
int rc;
LOG_ENTRY();
switch (index) {
case LVM2_OPTION_SET_CONTAINER_INFO_NAME_IDX:
/* Make sure this name is not in use and that isn't too long. */
rc = validate_vg_name(value->s, context->container->disk_group);
if (!rc) {
LOG_DEBUG("Setting name option: %s\n", value->s);
strncpy(od->option[index].value.s, value->s, EVMS_NAME_SIZE);
od->option[index].flags &= ~EVMS_OPTION_FLAGS_NO_INITIAL_VALUE;
}
break;
default:
rc = EINVAL;
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* set_container_info_parse_options
*
* Parse the option-values from the option-array.
**/
void set_container_info_parse_options(storage_container_t *container,
option_array_t *options,
char **vg_name)
{
u_int i;
LOG_ENTRY();
/* Default values. */
*vg_name = NULL;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_SET_CONTAINER_INFO_NAME_STR)) {
options->option[i].number = LVM2_OPTION_SET_CONTAINER_INFO_NAME_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_SET_CONTAINER_INFO_NAME_IDX:
*vg_name = options->option[i].value.s;
LOG_DEBUG("Name option: %s\n", *vg_name);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* set_container_info_validate_options
*
* Verify that these options are valid for setting info for this container.
**/
int set_container_info_validate_options(storage_container_t *container,
char *vg_name)
{
int rc;
LOG_ENTRY();
/* Validate the new container name. */
rc = validate_vg_name(vg_name, container->disk_group);
LOG_EXIT_INT(rc);
return rc;
}
/**
* Split-region-mapping routines.
**/
/**
* split_region_mapping_init_task
*
* Initialize the option-descriptor array for a split-region-mapping task.
**/
int split_region_mapping_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *region = context->object;
region_data_t *r_data = region->private_data;
u_int32_t mapping_count = EngFncs->list_count(r_data->mappings);
region_mapping_t *r_map;
list_element_t iter;
u_int32_t j = 0;
int rc, i;
LOG_ENTRY();
LOG_DEBUG("Initializing split-mapping task for region %s.\n",
region->name);
/* Check that there are mappings that can be split. */
rc = can_split_a_region_mapping(region);
if (rc) {
LOG_DEBUG("No mappings can be split for region %s.\n",
region->name);
goto out;
}
/* Find the first mapping that can be split. */
LIST_FOR_EACH(r_data->mappings, iter, r_map) {
rc = can_split_region_mapping(r_map);
if (!rc) {
break;
}
j++;
}
/* "Mapping" option. Set up a constraint-range based on the number
* of mappings in this region. Every mapping may not be able to be
* split, so this may include some that aren't valid, but this
* presents a simpler list to the user. Plus, in practice it will
* be unlikely to have a mapping that cannot be split.
*/
i = LVM2_OPTION_SPLIT_MAPPING_MAP_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_SPLIT_MAPPING_MAP_STR);
od->option[i].title = EngFncs->engine_strdup(_("Index of the logical-mapping to split."));
od->option[i].tip = EngFncs->engine_strdup(_("Display extended details for this region to see information about the mappings and determine which mapping you wish to split."));
od->option[i].type = EVMS_Type_Unsigned_Int32;
od->option[i].value.ui32 = j;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE32(od->option[i].constraint.range, j, mapping_count - 1, 1);
/* "Extent" option. Assume mapping "j" in this region is initially
* selected and set up a constraint-range based on the number of
* extents in that mapping. Each side of the split mapping must have
* at least one extent, so zero is not a valid choice for this option.
*/
i = LVM2_OPTION_SPLIT_MAPPING_EXTENT_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_SPLIT_MAPPING_EXTENT_STR);
od->option[i].title = EngFncs->engine_strdup(_("Extent within the selected mapping."));
od->option[i].tip = EngFncs->engine_strdup(_("Display extended details for this region to determine where within this mapping to make the split."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].value.ui64 = r_map->stripe_count;
od->option[i].constraint_type = EVMS_Collection_Range;
SET_RANGE64(od->option[i].constraint.range, r_map->stripe_count,
r_map->le_count - r_map->stripe_count, r_map->stripe_count);
od->count = LVM2_OPTION_SPLIT_MAPPING_COUNT;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* split_region_mapping_set_option
*
* Validate the specified option-value for a split-mapping task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int split_region_mapping_set_option(task_context_t *context, u_int32_t index,
value_t *value, task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *region = context->object;
region_mapping_t *r_map;
int i, rc = EINVAL;
LOG_ENTRY();
switch (index) {
case LVM2_OPTION_SPLIT_MAPPING_MAP_IDX:
/* Make sure the selected value is
* within the acceptable range.
*/
if (value->ui32 < od->option[index].constraint.range->min.ui32 ||
value->ui32 > od->option[index].constraint.range->max.ui32) {
rc = EINVAL;
break;
}
/* Find the desired mapping and verify that it can be split. */
r_map = find_mapping_by_index(region, value->ui32);
rc = can_split_region_mapping(r_map);
if (rc) {
LOG_ERROR("Mapping %u in region %s cannot be split.\n",
value->ui32, region->name);
break;
}
LOG_DEBUG("Setting \"map\" option to %u.\n", value->ui32);
od->option[index].value.ui32 = value->ui32;
/* Reset the constraint range and initial
* value for the extent option.
*/
i = LVM2_OPTION_SPLIT_MAPPING_EXTENT_IDX;
EngFncs->engine_free(od->option[i].constraint.range);
SET_RANGE64(od->option[i].constraint.range, r_map->stripe_count,
r_map->le_count - r_map->stripe_count,
r_map->stripe_count);
od->option[i].value.ui64 = r_map->stripe_count;
*effect |= EVMS_Effect_Reload_Options;
break;
case LVM2_OPTION_SPLIT_MAPPING_EXTENT_IDX:
/* Round down to nearest stripe_count boundary. */
if (value->ui64 % od->option[index].constraint.range->increment.ui64) {
value->ui64 -= value->ui64 % od->option[index].constraint.range->increment.ui64;
*effect |= EVMS_Effect_Inexact;
}
/* Make sure the selected value is
* within the acceptable range.
*/
if (value->ui64 < od->option[index].constraint.range->min.ui64 ||
value->ui64 > od->option[index].constraint.range->max.ui64 ) {
rc = EINVAL;
break;
}
LOG_DEBUG("Setting \"extent\" option to %"PRIu64".\n",
value->ui64);
od->option[index].value.ui64 = value->ui64;
rc = 0;
break;
default:
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* split_mapping_parse_options
*
* Parse the option-values from the option-array.
**/
void split_mapping_parse_options(option_array_t *options,
u_int32_t *r_map_index,
u_int64_t *extent_index)
{
u_int i;
LOG_ENTRY();
/* Default values. */
*r_map_index = 0;
*extent_index = 1;
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_SPLIT_MAPPING_MAP_STR)) {
options->option[i].number = LVM2_OPTION_SPLIT_MAPPING_MAP_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_SPLIT_MAPPING_EXTENT_STR)) {
options->option[i].number = LVM2_OPTION_SPLIT_MAPPING_EXTENT_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_SPLIT_MAPPING_MAP_IDX:
*r_map_index = options->option[i].value.ui32;
LOG_DEBUG("Map option: %u\n", *r_map_index);
break;
case LVM2_OPTION_SPLIT_MAPPING_EXTENT_IDX:
*extent_index = options->option[i].value.ui64;
LOG_DEBUG("Extent option: %"PRIu64"\n", *extent_index);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* split_mapping_validate_options
*
* Verify that these options are valid for splitting a mapping in the
* specified region. Return a pointer to the mapping to be split if it
* is found correctly.
**/
int split_mapping_validate_options(storage_object_t *region,
u_int32_t r_map_index,
u_int64_t *extent_index,
region_mapping_t **map_to_split)
{
region_data_t *r_data = region->private_data;
region_mapping_t *r_map;
int rc;
LOG_ENTRY();
/* Find the desired region-map. */
r_map = find_mapping_by_index(region, r_map_index);
if (!r_map) {
LOG_ERROR("Mapping %u does not exist. Region %s only contains "
"%u mappings.\n", r_map_index, region->name,
EngFncs->list_count(r_data->mappings));
rc = EINVAL;
goto out;
}
/* Make sure this mapping can be split. */
rc = can_split_region_mapping(r_map);
if (rc) {
LOG_ERROR("Mapping %u in region %s cannot be split - not "
"large enough.\n", r_map_index, region->name);
goto out;
}
/* Make sure the specified extent is valid for splitting this mapping.
* It must be less than the number of extents in the mapping (but not
* zero) and a multiple of the number of stripes.
*/
if (*extent_index % r_map->stripe_count) {
/* Round down to the nearest stripe_count multiple. */
*extent_index -= *extent_index % r_map->stripe_count;
}
if (*extent_index == 0 ||
*extent_index >= r_map->le_count) {
LOG_ERROR("Extent %"PRIu64" is not a valid location to split "
"mapping %u on region %s.\n",
*extent_index, r_map_index, region->name);
rc = EINVAL;
goto out;
}
*map_to_split = r_map;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* Merge-region-mappings routines.
**/
/**
* merge_region_mappings_init_task
*
* Check if there are any mappings in this region that can be merged. There
* are no options or selected-objects, so there's not much to do here.
**/
int merge_region_mappings_init_task(task_context_t *context)
{
int rc;
LOG_ENTRY();
LOG_DEBUG("Initializing merge-mappings task for region %s.\n",
context->object->name);
rc = can_merge_region_mappings(context->object);
LOG_EXIT_INT(rc);
return rc;
}
/**
* Move-region-mapping routines.
**/
/**
* move_mapping_init_map_option_list
*
* Create a constraint list of all mappings in this region that could be
* moved. Return a pointer to the first available mapping that is found.
**/
static int move_mapping_init_map_option_list(storage_object_t *region,
value_list_t **list,
region_mapping_t **selected_r_map)
{
storage_container_t *container = region->producing_container;
region_data_t *r_data = region->private_data;
region_mapping_t *r_map;
list_element_t iter;
u_int64_t max_con_extents;
u_int32_t i;
int rc, j;
LOG_ENTRY();
*selected_r_map = NULL;
max_con_extents = max_consecutive_extents_in_container(container);
/* Allocate the constraint list. */
*list = EngFncs->engine_alloc(sizeof(value_list_t) + sizeof(value_t) *
EngFncs->list_count(r_data->mappings));
if (!*list) {
rc = ENOMEM;
goto out;
}
/* Check each mapping in this region. */
i = j = 0;
LIST_FOR_EACH(r_data->mappings, iter, r_map) {
rc = can_move_region_mapping(r_map, max_con_extents);
if (!rc) {
(*list)->value[j++].ui32 = i;
if (!*selected_r_map) {
/* Return the first valid mapping.*/
*selected_r_map = r_map;
}
}
i++;
}
(*list)->count = j;
/* Need at least one mapping which can be moved. */
rc = (*selected_r_map) ? 0 : ENOSPC;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_mapping_init_stripe_option_list
*
* Create a constraint list of all stripes in this mapping that aren't
* already scheduled to be moved.
**/
static int move_mapping_init_stripe_option_list(region_mapping_t *r_map,
value_list_t **list)
{
u_int64_t i;
int rc, j;
LOG_ENTRY();
/* Allocate the constraint list. */
*list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * r_map->stripe_count);
if (!*list) {
rc = ENOMEM;
goto out;
}
/* Each stripe can be moved as long as it
* isn't already scheduled to be moved.
*/
for (i = 0, j = 0; i < r_map->stripe_count; i++) {
rc = can_move_stripe(r_map->le_maps + i);
if (!rc) {
(*list)->value[j++].ui64 = i;
}
}
(*list)->count = j;
/* Need at least one stripe which can be moved. */
rc = j ? 0 : EBUSY;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_mapping_init_object_option_list
*
* Create a constraint list of all objects in this mapping's container that
* have enough consecutive freespace to move this mapping to. Return a pointer
* to the first valid object that is found.
**/
static int move_mapping_init_object_option_list(region_mapping_t *r_map,
value_list_t **list,
storage_object_t **selected_object)
{
storage_container_t *container = r_map->r_data->region->producing_container;
u_int64_t extents_per_stripe = r_map->le_count / r_map->stripe_count;
u_int64_t max_con_extents;
storage_object_t *object;
list_element_t iter;
int rc, i;
LOG_ENTRY();
*selected_object = NULL;
/* Allocate the constraint list. */
*list = EngFncs->engine_alloc(sizeof(value_list_t) + sizeof(value_t) *
EngFncs->list_count(container->objects_consumed));
if (!*list) {
rc = ENOMEM;
goto out;
}
/* Check every PV object in this container. */
i = 0;
LIST_FOR_EACH(container->objects_consumed, iter, object) {
max_con_extents = max_consecutive_extents_on_object(object);
if (extents_per_stripe <= max_con_extents) {
(*list)->value[i++].s = EngFncs->engine_strdup(object->name);
if (!*selected_object) {
/* Return the first valid object. */
*selected_object = object;
}
}
}
(*list)->count = i;
/* Need at least one stripe which can be moved. */
rc = (*selected_object) ? 0 : ENOSPC;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_mapping_init_extent_option_list
*
* Create a constraint list of all PEs in the selected object that begin a
* consecutive area of freespace large enough to move the specified mapping.
**/
static int move_mapping_init_extent_option_list(region_mapping_t *r_map,
storage_object_t *object,
value_list_t **list)
{
pv_data_t *pv_data = object->consuming_private_data;
u_int64_t extents_per_stripe = r_map->le_count / r_map->stripe_count;
u_int64_t i, j, max_con_extents;
int rc, k;
LOG_ENTRY();
/* Allocate the constraint list. */
*list = EngFncs->engine_alloc(sizeof(value_list_t) +
sizeof(value_t) * pv_data->pe_count);
if (!*list) {
rc = ENOMEM;
goto out;
}
for (i = 0, k = 0; i < pv_data->pe_count; i++) {
max_con_extents = consecutive_extents_at_pe(pv_data, i);
if (max_con_extents >= extents_per_stripe) {
for (j = 0; j < max_con_extents - extents_per_stripe + 1; j++) {
(*list)->value[k++].ui64 = i + j;
}
}
i += max_con_extents;
}
(*list)->count = k;
/* Need at least one destination extent. */
rc = k ? 0 : ENOSPC;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_region_mapping_init_task
*
* Initialize the option-descriptor array for a move-region-mapping task.
**/
int move_region_mapping_init_task(task_context_t *context)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *object, *region = context->object;
region_mapping_t *r_map;
int i, rc;
LOG_ENTRY();
LOG_DEBUG("Initializing move-mapping task for region %s.\n",
region->name);
/* "Mapping" option.
* Check each mapping in this region to determine which can be moved.
*/
i = LVM2_OPTION_MOVE_MAPPING_MAP_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_MOVE_MAPPING_MAP_STR);
od->option[i].title = EngFncs->engine_strdup(_("Index of the logical-mapping to move."));
od->option[i].tip = EngFncs->engine_strdup(_("Display extended details for this region to see information "
"about the mappings and determine which mapping you wish to move."));
od->option[i].type = EVMS_Type_Unsigned_Int32;
od->option[i].constraint_type = EVMS_Collection_List;
/* Set up the constraint list. */
rc = move_mapping_init_map_option_list(region,
&(od->option[i].constraint.list),
&r_map);
if (rc) {
goto out;
}
/* Set the default value to the first item in the constraint list. */
od->option[i].value.ui32 = od->option[i].constraint.list->value[0].ui32;
/* "Stripe" option.
* See which stripes in the selected mapping can be moved. If the
* selected mapping is linear, we don't need to display this option.
*/
i = LVM2_OPTION_MOVE_MAPPING_STRIPE_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_MOVE_MAPPING_STRIPE_STR);
od->option[i].title = EngFncs->engine_strdup(_("The stripe within this mapping to move."));
od->option[i].tip = EngFncs->engine_strdup(_("Only one stripe per mapping can be moved at a time. Display extended "
"details for this region to see information about the mappings and "
"determine which stripe within this mapping you wish to move."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
if (r_map->stripe_count == 1) {
od->option[i].flags |= EVMS_OPTION_FLAGS_INACTIVE;
od->option[i].value.ui64 = 0;
} else {
/* Set up the constraint list. */
od->option[i].constraint_type = EVMS_Collection_List;
rc = move_mapping_init_stripe_option_list(r_map,
&(od->option[i].constraint.list));
if (rc) {
goto out;
}
/* Set the default value to the first item in the constraint list. */
od->option[i].value.ui64 = od->option[i].constraint.list->value[0].ui64;
}
/* "Object" option.
* Find all the PVs in this container that could be
* used as a destination for moving this mapping.
*/
i = LVM2_OPTION_MOVE_MAPPING_OBJECT_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_MOVE_MAPPING_OBJECT_STR);
od->option[i].title = EngFncs->engine_strdup(_("The PV object to move this mapping to."));
od->option[i].tip = EngFncs->engine_strdup(_("Display extended details for this region's container and PVs to "
"determine which PV has space available to move this mapping."));
od->option[i].type = EVMS_Type_String;
od->option[i].min_len = 1;
od->option[i].max_len = EVMS_NAME_SIZE;
od->option[i].value.s = EngFncs->engine_alloc(EVMS_NAME_SIZE+1);
od->option[i].constraint_type = EVMS_Collection_List;
/* Set up the constraint list. */
rc = move_mapping_init_object_option_list(r_map,
&(od->option[i].constraint.list),
&object);
if (rc) {
goto out;
}
/* Set the default value to the name of the selected PV object. */
strncpy(od->option[i].value.s, object->name, EVMS_NAME_SIZE);
/* "Extent" option.
* Look for all PEs on the selected object which could
* start the destination area for moving this mapping.
*/
i = LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX;
od->option[i].name = EngFncs->engine_strdup(LVM2_OPTION_MOVE_MAPPING_EXTENT_STR);
od->option[i].title = EngFncs->engine_strdup(_("The starting PE of the destination area for the move."));
od->option[i].tip = EngFncs->engine_strdup(_("Display extended details for this region's container "
"and the selected PV object to determine where within "
"the PE-map has space available to move this mapping."));
od->option[i].type = EVMS_Type_Unsigned_Int64;
od->option[i].constraint_type = EVMS_Collection_List;
/* Set up the constraint list. */
rc = move_mapping_init_extent_option_list(r_map, object,
&(od->option[i].constraint.list));
if (rc) {
goto out;
}
/* Set the default value to the first item in the constraint list. */
od->option[i].value.ui64 = od->option[i].constraint.list->value[0].ui64;
od->count = LVM2_OPTION_MOVE_MAPPING_COUNT;
rc = 0;
out:
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_region_mapping_set_option
*
* Validate the specified option-value for a move-mapping task. If the
* option-value is valid, update the appropriate entry in the option-descriptor.
**/
int move_region_mapping_set_option(task_context_t *context, u_int32_t index,
value_t *value, task_effect_t *effect)
{
option_desc_array_t *od = context->option_descriptors;
storage_object_t *object, *region = context->object;
region_mapping_t *r_map;
int i, rc;
LOG_ENTRY();
switch (index) {
/* "Map" option. */
case LVM2_OPTION_MOVE_MAPPING_MAP_IDX:
/* Make sure the selected mapping is in the constraint-list. */
for (i = 0; i < od->option[index].constraint.list->count; i++) {
if (value->ui32 ==
od->option[index].constraint.list->value[i].ui32) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
LOG_ERROR("Invalid mapping index specified: %u.\n",
value->ui32);
rc = EINVAL;
break;
}
LOG_DEBUG("Setting \"map\" option to %u.\n", value->ui32);
od->option[index].value.ui32 = value->ui32;
/* Find the selected mapping in the region. */
r_map = find_mapping_by_index(region, value->ui32);
/* Update "stripe" option. */
index = LVM2_OPTION_MOVE_MAPPING_STRIPE_IDX;
EngFncs->engine_free(od->option[index].constraint.list);
od->option[index].constraint.list = NULL;
if (r_map->stripe_count == 1) {
od->option[index].flags |= EVMS_OPTION_FLAGS_INACTIVE;
od->option[index].constraint_type = 0;
od->option[index].value.ui64 = 0;
} else {
/* Set up a new constraint list. */
od->option[index].constraint_type = EVMS_Collection_List;
rc = move_mapping_init_stripe_option_list(r_map,
&(od->option[index].constraint.list));
if (rc) {
break;
}
/* Set the default value to the first item in the constraint list. */
od->option[index].value.ui64 = od->option[index].constraint.list->value[0].ui64;
}
/* Update "object" option. */
index = LVM2_OPTION_MOVE_MAPPING_OBJECT_IDX;
/* Set up a new constraint list. */
for (i = 0; i < od->option[index].constraint.list->count; i++) {
EngFncs->engine_free(od->option[index].constraint.list->value[i].s);
}
EngFncs->engine_free(od->option[index].constraint.list);
rc = move_mapping_init_object_option_list(r_map,
&(od->option[index].constraint.list),
&object);
if (rc) {
break;
}
/* Set the default value to the name of the selected PV object. */
strncpy(od->option[index].value.s, object->name, EVMS_NAME_SIZE);
/* Update "extent" option. */
index = LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX;
/* Set up a new constraint list. */
EngFncs->engine_free(od->option[index].constraint.list);
/* Set up the constraint list. */
rc = move_mapping_init_extent_option_list(r_map, object,
&(od->option[index].constraint.list));
if (rc) {
break;
}
/* Set the default value to the first item in the constraint list. */
od->option[index].value.ui64 = od->option[index].constraint.list->value[0].ui64;
*effect |= EVMS_Effect_Reload_Options;
break;
/* "Stripe" option. */
case LVM2_OPTION_MOVE_MAPPING_STRIPE_IDX:
/* Make sure the selected stripe is in the constraint-list. */
for (i = 0; i < od->option[index].constraint.list->count; i++) {
if (value->ui64 ==
od->option[index].constraint.list->value[i].ui64) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
LOG_ERROR("Invalid stripe index specified: %"
PRIu64".\n", value->ui64);
rc = EINVAL;
break;
}
LOG_DEBUG("Setting \"stripe\" option to %"PRIu64".\n",
value->ui64);
od->option[index].value.ui64 = value->ui64;
/* Since the selected mapping has not changed (only the stripe
* within that mapping) none of the other options are affected.
*/
rc = 0;
break;
/* "Object" option. */
case LVM2_OPTION_MOVE_MAPPING_OBJECT_IDX:
/* Make sure the selected object is in the constraint-list. */
for (i = 0; i < od->option[index].constraint.list->count; i++) {
rc = strncmp(value->s,
od->option[index].constraint.list->value[i].s,
EVMS_NAME_SIZE);
if (!rc) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
LOG_ERROR("Invalid object name specified: %s\n",
value->s);
rc = EINVAL;
break;
}
LOG_DEBUG("Setting \"object\" option to %s.\n", value->s);
strncpy(od->option[index].value.s, value->s, EVMS_NAME_SIZE);
/* Get the selected mapping and PV object. */
index = LVM2_OPTION_MOVE_MAPPING_MAP_IDX;
r_map = find_mapping_by_index(region, od->option[index].value.ui32);
object = find_pv_by_name(region->producing_container, value->s);
/* Update "extent" option. */
index = LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX;
/* Set up a new constraint list. */
EngFncs->engine_free(od->option[index].constraint.list);
/* Set up the constraint list. */
rc = move_mapping_init_extent_option_list(r_map, object,
&(od->option[index].constraint.list));
if (rc) {
break;
}
/* Set the default value to the first item in the constraint list. */
od->option[index].value.ui64 = od->option[index].constraint.list->value[0].ui64;
*effect |= EVMS_Effect_Reload_Options;
break;
/* "Extent" option. */
case LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX:
/* Make sure the selected extent is in the constraint-list. */
for (i = 0; i < od->option[index].constraint.list->count; i++) {
if (value->ui64 ==
od->option[index].constraint.list->value[i].ui64) {
break;
}
}
if (i == od->option[index].constraint.list->count) {
LOG_ERROR("Invalid physical extent specified: %"
PRIu64".\n", value->ui64);
rc = EINVAL;
break;
}
LOG_DEBUG("Setting \"extent\" option to %"PRIu64".\n",
value->ui64);
od->option[index].value.ui64 = value->ui64;
rc = 0;
break;
default:
rc = EINVAL;
break;
}
LOG_EXIT_INT(rc);
return rc;
}
/**
* move_mapping_parse_options
*
* Parse the option-values from the option-array.
**/
void move_mapping_parse_options(option_array_t *options,
u_int32_t *r_map_index,
u_int64_t *stripe_index,
char **object_name,
u_int64_t *extent_index)
{
u_int i;
/* Default values. */
*r_map_index = 0;
*stripe_index = 0;
*object_name = NULL;
*extent_index = 0;
LOG_ENTRY();
for (i = 0; i < options->count; i++) {
/* If only the name-based index is specified, get the number. */
if (!options->option[i].is_number_based) {
if (!strcmp(options->option[i].name,
LVM2_OPTION_MOVE_MAPPING_MAP_STR)) {
options->option[i].number = LVM2_OPTION_MOVE_MAPPING_MAP_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_MOVE_MAPPING_STRIPE_STR)) {
options->option[i].number = LVM2_OPTION_MOVE_MAPPING_STRIPE_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_MOVE_MAPPING_OBJECT_STR)) {
options->option[i].number = LVM2_OPTION_MOVE_MAPPING_OBJECT_IDX;
} else if (!strcmp(options->option[i].name,
LVM2_OPTION_MOVE_MAPPING_EXTENT_STR)) {
options->option[i].number = LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX;
} else {
continue;
}
}
/* Use the number-based index to record the option. */
switch (options->option[i].number) {
case LVM2_OPTION_MOVE_MAPPING_MAP_IDX:
*r_map_index = options->option[i].value.ui32;
LOG_DEBUG("Map option: %u\n", *r_map_index);
break;
case LVM2_OPTION_MOVE_MAPPING_STRIPE_IDX:
*stripe_index = options->option[i].value.ui64;
LOG_DEBUG("Stripe option: %"PRIu64"\n", *stripe_index);
break;
case LVM2_OPTION_MOVE_MAPPING_OBJECT_IDX:
*object_name = options->option[i].value.s;
LOG_DEBUG("Object option: %s\n", *object_name);
break;
case LVM2_OPTION_MOVE_MAPPING_EXTENT_IDX:
*extent_index = options->option[i].value.ui64;
LOG_DEBUG("Extent option: %"PRIu64"\n", *extent_index);
break;
default:
break;
}
}
LOG_EXIT_VOID();
}
/**
* move_mapping_validate_options
*
* Verify that the move-mapping options are valid. If they are, return
* pointers to the selected mapping and destination PV.
**/
int move_mapping_validate_options(storage_object_t *region,
u_int32_t r_map_index,
u_int64_t stripe_index,
char *object_name,
u_int64_t extent_index,
region_mapping_t **r_map,
storage_object_t **object)
{
storage_container_t *container = region->producing_container;
u_int64_t max_con_extents;
pv_data_t *pv_data;
int rc;
LOG_ENTRY();
/* Find the selected mapping. */
*r_map = find_mapping_by_index(region, r_map_index);
if (!*r_map) {
LOG_ERROR("Could not find mapping %u in region %s.\n",
r_map_index, region->name);
rc = EINVAL;
goto out;
}
/* Find the selected object. */
*object = find_pv_by_name(container, object_name);
if (!*object) {
LOG_ERROR("Could not find object %s in container %s.\n",
object_name, container->name);
rc = EINVAL;
goto out;
}
pv_data = (*object)->consuming_private_data;
/* Make sure the selected mapping can be moved. */
max_con_extents = consecutive_extents_at_pe(pv_data, extent_index);
rc = can_move_region_mapping(*r_map, max_con_extents);
if (rc) {
LOG_ERROR("Cannot move mapping %u in region %s.\n",
r_map_index, region->name);
goto out;
}
/* Make sure the selected stripe is valid and can be moved. */
if (stripe_index >= (*r_map)->stripe_count) {
LOG_ERROR("Selected stripe %"PRIu64". Mapping %u in region %s "
"only has %"PRIu64" stripes.\n", stripe_index,
r_map_index, region->name, (*r_map)->stripe_count);
rc = EINVAL;
goto out;
}
rc = can_move_stripe((*r_map)->le_maps + stripe_index);
if (rc) {
LOG_ERROR("Stripe %"PRIu64" in mapping %u in region "
"%s cannot be moved at this time.\n",
stripe_index, r_map_index, region->name);
goto out;
}
out:
LOG_EXIT_INT(rc);
return rc;
}
|