1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
|
/*
* Copyright (c) 2000-2002 Silicon Graphics, Inc.
* All Rights Reserved.
*
* 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.
*
* This program is distributed in the hope that it would 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 the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/param.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <utime.h>
#include <limits.h>
#include <time.h>
#include <xfs/handle.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <string.h>
#include <uuid/uuid.h>
#include <xfs/xfs.h>
#include "config.h"
#include "types.h"
#include "exit.h"
#include "cldmgr.h"
#include "path.h"
#include "openutil.h"
#include "getopt.h"
#include "stream.h"
#include "mlog.h"
#include "dlog.h"
#include "global.h"
#include "drive.h"
#include "media.h"
#include "content.h"
#include "content_inode.h"
#include "inomap.h"
#include "namreg.h"
#include "dirattr.h"
#include "bag.h"
#include "node.h"
#include "tree.h"
#include "libgen.h"
#include "mmap.h"
#include "exit.h"
/* structure definitions used locally ****************************************/
/* name of persistent state file
*/
#define PERS_NAME "tree"
/* orphanage specifics. ino must be otherwise unused in the dump source fs.
* zero works.
*/
#define ORPH_INO 0
#define ORPH_NAME "orphanage"
/* VM budgeting - give hash array one sixteenth, rest goes to node array
*/
#define HASHSZ_PERVM 16
/* reserve the first page for persistent state
*/
struct treePersStorage {
xfs_ino_t p_rootino;
/* ino of root
*/
nh_t p_rooth;
/* handle of root node
*/
nh_t p_orphh;
/* handle to orphanage node
*/
size64_t p_hashsz;
/* size of hash array (private to hash abstraction)
*/
size_t p_hashmask;
/* hash mask (private to hash abstraction)
*/
bool_t p_ownerpr;
/* restore directory owner/group attributes
*/
bool_t p_fullpr;
/* restoring a full level 0 non-resumed dump (can skip
* some steps)
*/
bool_t p_ignoreorphpr;
/* set if positive subtree or interactive
*/
bool_t p_restoredmpr;
/* restore DMI event settings
*/
bool_t p_truncategenpr;
/* truncate inode generation number (for compatibility
* with xfsdump format 2 and earlier)
*/
};
typedef struct treePersStorage treepers_t;
#define PERSSZ perssz
/* interactive dialog transient state
*/
#define INTER_ARGMAX 10 /* max number of args to interactive cmds */
struct inter {
size_t i_argc;
char *i_argv[INTER_ARGMAX];
nh_t i_cwdh;
char i_name[NAME_MAX + 1];
};
typedef struct inter inter_t;
/* transient state
*/
struct tran {
bool_t t_toconlypr;
/* just display table of contents; don't restore files
*/
char *t_hkdir;
/* full absolute pathname of housekeeping directory
*/
char *t_dstdir;
/* full absolute pathname of destination directory
*/
bool_t t_dstdirisxfspr;
/* destination directory is an xfs filesystem; xfs-specific
* calls can be made when needed.
*/
char *t_orphdir;
/* full absolute pathname of orphanage directory
*/
char *t_hksubtree;
/* if non-NULL, is path of hkdir relative to dstdir.
* don't restore there.
*/
int t_persfd;
/* file descriptor of the persistent state file
*/
nh_t *t_hashp;
/* pointer to mapped hash array (private to hash abstraction)
*/
char t_namebuf[NAME_MAX + 1];
/* to hold names temporarily retrieved from name registry
*/
inter_t t_inter;
/* context for interactive subtree selection
*/
};
typedef struct tran tran_t;
/* node structure. each node represents a directory entry
*/
#define NODESZ 56
struct node {
xfs_ino_t n_ino; /* 8 8 ino */
nrh_t n_nrh; /* 8 16 handle to name in name registry */
dah_t n_dah; /* 4 20 handle to directory attributes */
nh_t n_hashh; /* 4 24 hash array */
nh_t n_parh; /* 4 28 parent */
nh_t n_sibh; /* 4 32 sibling list */
nh_t n_sibprevh; /* 4 36 prev sibling list - dbl link list */
nh_t n_cldh; /* 4 40 children list */
nh_t n_lnkh; /* 4 44 hard link list */
gen_t n_gen; /* 4 48 generation count mod 0x10000 */
u_char_t n_flags; /* 1 49 action and state flags */
u_char_t n_nodehkbyte; /* 1 50 given to node abstraction */
char n_pad[6]; /* 6 56 */
};
typedef struct node node_t;
#define NF_REAL (1 << 0)
/* set when the corresponding file/dir has been created in
* the restore destination.
*/
#define NF_SUBTREE (1 << 1)
/* marks nodes in the selected subtrees.
*/
#define NF_REFED (1 << 2)
/* indicates node is still referenced according to incremental/resumed
* dump. used to detect dirents no longer used. prior to restoring
* a dump session, this flag is cleared in all nodes. during the dirent
* restoral, it is set. it may also be set during the adjustment
* for referenced but undumped directories. NOTE: nodes in the
* orphanage NEVER have this flag set.
*/
#define NF_WRITTEN (1 << 3)
/* set as soon as a non-dir node restore is begun. allows
* overwrite inhibit options to work with segmented files
*/
#define NF_ISDIR (1 << 4)
/* indicates this node is a directory. set when a directory is taken
* from the dirdump.
*/
#define NF_DUMPEDDIR (1 << 5)
/* indicates this node is a directory present in the current dirdump.
* at beginning of session, this flag is cleared in all nodes.
* then as each directory dump is read from media, the flag
* is set from the corresponding node. this allows adjustments to
* the NF_REFED flag: if a directory was not dumped, either it no
* longer exists or it has not changed. if it is referenced, we assume
* it exists, in which case if it is not dumped then all of its entries
* are referenced as well.
*/
#define NF_NEWORPH (1 << 6)
/* cleared from all nodes in the orphanage before a dump is applied.
* set if a dir is seen in the dirdump but no node exists for it.
* cleared if that dir is adopted subsequently during the dirdump.
* set if a nondir is seen in the nondir dump but no node exists for
* it. in either case a node is created and placed in the orphanage.
* during rmdir/unlink processing, nodes so marked are left alone.
* since the flag is cleared at the beginning of the next increment,
* old orphans had better be adopted, otherwise they will be unlinked.
*/
/* link list iterator context
*/
struct link_iter_context {
nh_t li_headh; /* head of hard link list */
nh_t li_prevh; /* next to last node returned by _next() */
nh_t li_lasth; /* last node returned by _next() */
bool_t li_donepr; /* set as soon as last.next null */
};
typedef struct link_iter_context link_iter_context_t;
/* used for caching parent pathname from previous Node2path result
*/
struct path_cache {
nh_t nh;
int len;
char buf[MAXPATHLEN];
};
typedef struct path_cache path_cache_t;
/* declarations of externally defined global symbols *************************/
extern void usage(void);
extern size_t pgsz;
extern size_t pgmask;
extern bool_t restore_rootdir_permissions;
/* forward declarations of locally defined static functions ******************/
static nh_t Node_alloc(xfs_ino_t ino,
gen_t gen,
nrh_t nrh,
dah_t dah,
size_t flags);
static void Node_free(nh_t *nhp);
static node_t * Node_map(nh_t nh);
static void Node_unmap(nh_t nh, node_t **npp);
static int Node2path_recurse(nh_t nh, char *buf,
int bufsz, int level);
static void adopt(nh_t parh, nh_t cldh, nrh_t nrh);
static nrh_t disown(nh_t cldh);
static void selsubtree(nh_t nh, bool_t sensepr);
static void selsubtree_recurse_down(nh_t nh, bool_t sensepr);
static nh_t link_hardh(xfs_ino_t ino, gen_t gen);
static nh_t link_nexth(nh_t nh);
static nh_t link_matchh(nh_t hardh, nh_t parh, char *name);
static void link_in(nh_t nh);
static void link_out(nh_t nh);
static void link_headiter(bool_t (*cbfp)(void *contextp, nh_t hardh),
void *contextp);
static void link_iter_init(link_iter_context_t *link_iter_contextp,
nh_t hardheadh);
static nh_t link_iter_next(link_iter_context_t *link_iter_contextp);
void link_iter_unlink(link_iter_context_t *link_iter_contextp, nh_t nh);
static bool_t hash_init(size64_t vmsz,
size64_t dircnt,
size64_t nondircnt,
char *perspath);
static bool_t hash_sync(char *perspath);
static inline size_t hash_val(xfs_ino_t ino, size_t hashmask);
static void hash_in(nh_t nh);
static void hash_out(nh_t nh);
static nh_t hash_find(xfs_ino_t ino, gen_t gen);
static void hash_iter(bool_t (*cbfp)(void *contextp, nh_t hashh),
void *contextp);
static void setdirattr(dah_t dah, char *path);
static bool_t tsi_walkpath(char *arg, nh_t rooth, nh_t cwdh,
dlog_pcbp_t pcb, void *pctxp,
nh_t *namedhp, nh_t *parhp, nh_t *cldhp,
xfs_ino_t *inop, bool_t *isdirprp, bool_t *isselpr);
static bool_t Node2path(nh_t nh, char *path, char *errmsg);
static bool_t tree_setattr_recurse(nh_t parh, char *path);
static void tsi_cmd_pwd_recurse(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp,
nh_t nh);
static int mkdir_r(char *path);
#ifdef TREE_CHK
static bool_t Node_chk(nh_t nh, nh_t *nexthashhp, nh_t *nextlnkhp);
static bool_t tree_chk2(void);
#endif /* TREE_CHK */
/* definition of locally defined global variables ****************************/
/* definition of locally defined static variables *****************************/
static treepers_t *persp = 0;
static tran_t *tranp = 0;
static char *persname = PERS_NAME;
static char *orphname = ORPH_NAME;
static xfs_ino_t orphino = ORPH_INO;
/* definition of locally defined global functions ****************************/
/* ARGSUSED */
bool_t
tree_init(char *hkdir,
char *dstdir,
bool_t toconlypr,
bool_t ownerpr,
xfs_ino_t rootino,
xfs_ino_t firstino,
xfs_ino_t lastino,
size64_t dircnt,
size64_t nondircnt,
size64_t vmsz,
bool_t fullpr,
bool_t restoredmpr,
bool_t dstdirisxfspr,
uint32_t dumpformat,
bool_t truncategenpr)
{
off64_t nodeoff;
char *perspath;
bool_t ok;
int rval;
/* sanity checks
*/
assert(!(PERSSZ % pgsz));
assert(sizeof(persp) <= PERSSZ);
assert(sizeof(node_t) <= NODESZ);
assert(!persp);
assert(!tranp);
/* allocate transient state
*/
tranp = (tran_t *)calloc(1, sizeof(tran_t));
assert(tranp);
tranp->t_toconlypr = toconlypr;
tranp->t_hkdir = hkdir;
tranp->t_dstdir = dstdir;
tranp->t_dstdirisxfspr = dstdirisxfspr;
/* allocate a char string buffer to hold the abs. pathname
* of the orphanage directory file. load it with the pathname.
*/
tranp->t_orphdir = open_pathalloc(tranp->t_dstdir,
orphname,
0);
/* determine if housekeeping dir is within the destination.
* generate a relative path containing the difference,
* else null. will not restore into there.
*/
if (strcmp(dstdir, ".")) {
tranp->t_hksubtree = path_diff(hkdir, dstdir);
} else {
tranp->t_hksubtree = 0;
}
/* create an orphanage, if it already exists, complain.
* not needed if just a table-of-contents restore.
*/
if (!tranp->t_toconlypr) {
rval = mkdir(tranp->t_orphdir, S_IRWXU);
if (rval) {
if (errno == EEXIST) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"%s already exists: "
"rm -rf prior to initating restore\n"),
tranp->t_orphdir);
} else {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"unable to create %s: %s\n"),
tranp->t_orphdir,
strerror(errno));
}
return BOOL_FALSE;
}
}
/* build a full pathname to pers. state file
*/
perspath = open_pathalloc(tranp->t_hkdir, persname, 0);
/* create the persistent state file
*/
(void)unlink(perspath);
tranp->t_persfd = open(perspath,
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR);
if (tranp->t_persfd < 0) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"could not open %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
/* mmap the persistent state
*/
assert(!(PERSSZ % pgsz));
persp = (treepers_t *) mmap_autogrow(
PERSSZ,
tranp->t_persfd,
(off64_t)0);
if (persp == (treepers_t *)-1) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"unable to map %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
/* create the hash abstraction. it will map more of the
* persistent state file.
*/
ok = hash_init(vmsz / HASHSZ_PERVM, dircnt, nondircnt, perspath);
if (!ok) {
return BOOL_FALSE;
}
/* initialize the node abstraction. let it's use of backing store
* begin immediately after the hash abstraction. give it the remainder
* of vm.
*/
assert(persp->p_hashsz <= (size64_t)(OFF64MAX - (off64_t)PERSSZ));
nodeoff = (off64_t)PERSSZ + (off64_t)persp->p_hashsz;
assert(vmsz > (size64_t)nodeoff);
ok = node_init(tranp->t_persfd,
nodeoff,
NODESZ,
(ix_t)offsetofmember(node_t, n_nodehkbyte),
sizeof(size64_t), /* node alignment */
vmsz - (size64_t)nodeoff,
dircnt + nondircnt);
if (!ok) {
return BOOL_FALSE;
}
/* extract the root ino and allocate a node for
* the root and for the orphanage. place both nodes
* in the hash list. make the orphanage a child of
* root, and indicate he is real.
*
* Note that we assume that the root inode always
* has a generation count of zero - which is true.
*/
persp->p_rootino = rootino;
persp->p_rooth = Node_alloc(rootino,
0, /* gen cnt */
NRH_NULL,
DAH_NULL,
NF_ISDIR | NF_REAL);
if (persp->p_rooth == NH_NULL)
return BOOL_FALSE;
link_in(persp->p_rooth);
persp->p_orphh = Node_alloc(orphino,
0, /* gen cnt */
namreg_add(orphname, strlen(orphname)),
DAH_NULL,
NF_ISDIR | NF_REAL);
if (persp->p_orphh == NH_NULL)
return BOOL_FALSE;
link_in(persp->p_orphh);
adopt(persp->p_rooth, persp->p_orphh, NRH_NULL);
/* record if we should attempt to restore original owner/group
*/
persp->p_ownerpr = ownerpr;
/* record if this is a full dump. can skip link processing if so
*/
persp->p_fullpr = fullpr;
/* record if DMI event settings should be restored
*/
persp->p_restoredmpr = restoredmpr;
/* record if truncated generation numbers are required
*/
if (dumpformat < GLOBAL_HDR_VERSION_3) {
persp->p_truncategenpr = BOOL_TRUE;
mlog(MLOG_NORMAL | MLOG_DEBUG | MLOG_TREE, _(
"dump format version %u used truncated inode generation numbers\n"),
dumpformat);
} else if (truncategenpr) {
persp->p_truncategenpr = BOOL_TRUE;
mlog(MLOG_NORMAL | MLOG_DEBUG | MLOG_TREE, _(
"forcing use of truncated inode generation numbers\n"));
} else {
persp->p_truncategenpr = BOOL_FALSE;
}
return BOOL_TRUE;
}
bool_t
tree_sync(char *hkdir,
char *dstdir,
bool_t toconlypr,
bool_t fullpr,
bool_t dstdirisxfspr)
{
off64_t nodeoff;
char *perspath;
bool_t ok;
int rval;
if (persp) {
return BOOL_TRUE;
}
/* sanity checks
*/
assert(!(PERSSZ % pgsz));
assert(sizeof(persp) <= PERSSZ);
assert(sizeof(node_t) <= NODESZ);
assert(!persp);
assert(!tranp);
/* allocate transient state
*/
tranp = (tran_t *)calloc(1, sizeof(tran_t));
assert(tranp);
tranp->t_toconlypr = toconlypr;
tranp->t_hkdir = hkdir;
tranp->t_dstdir = dstdir;
tranp->t_dstdirisxfspr = dstdirisxfspr;
/* allocate a char string buffer to hold the abs. pathname
* of the orphanage directory file. load it with the pathname.
*/
tranp->t_orphdir = open_pathalloc(tranp->t_dstdir,
orphname,
0);
/* determine if housekeeping dir is within the destination.
* generate a relative path containing the difference,
* else null. will not restore into there.
*/
if (strcmp(dstdir, ".")) {
tranp->t_hksubtree = path_diff(hkdir, dstdir);
} else {
tranp->t_hksubtree = 0;
}
/* re-create the orphanage (in case someone rmdir'ed it)
*/
rval = mkdir(tranp->t_orphdir, S_IRWXU);
if (rval && errno != EEXIST) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"unable to recreate %s: %s\n"),
tranp->t_orphdir,
strerror(errno));
return BOOL_FALSE;
}
/* build a full pathname to pers. state file
*/
perspath = open_pathalloc(tranp->t_hkdir, persname, 0);
/* re-open the persistent state file
*/
tranp->t_persfd = open(perspath, O_RDWR);
if (tranp->t_persfd < 0) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"could not open %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
/* mmap the persistent state
*/
assert(!(PERSSZ % pgsz));
persp = (treepers_t *) mmap_autogrow(
PERSSZ,
tranp->t_persfd,
(off64_t)0);
if (persp == (treepers_t *)-1) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"unable to map %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
/* update the fullpr field of the persistent state to match
* the input of our caller.
*/
persp->p_fullpr = fullpr;
/* regardless of the format of this dump, if the previously applied
* dump used truncated generation numbers, then we need to as well.
*/
if (persp->p_truncategenpr) {
mlog(MLOG_NORMAL | MLOG_DEBUG | MLOG_TREE, _(
"using truncated inode generation numbers for "
"compatibility with previously applied restore\n"));
}
/* rsynchronize with the hash abstraction. it will map more of the
* persistent state file.
*/
ok = hash_sync(perspath);
if (!ok) {
return BOOL_FALSE;
}
/* synchronize with the node abstraction.
*/
assert(persp->p_hashsz <= (size64_t)(OFF64MAX - (off64_t)PERSSZ));
nodeoff = (off64_t)PERSSZ + (off64_t)persp->p_hashsz;
ok = node_sync(tranp->t_persfd, nodeoff);
if (!ok) {
return BOOL_FALSE;
}
/* extract the root ino and allocate a node for
* the root and for the orphanage. place both nodes
* in the hash list. make the orphanage a child of
* root, and indicate he is real.
*/
return BOOL_TRUE;
}
bool_t
tree_check_dump_format(uint32_t dumpformat)
{
if (dumpformat < GLOBAL_HDR_VERSION_3 && !persp->p_truncategenpr) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"encountered dump format %d after a "
"restore of format %d or newer\n"),
dumpformat, GLOBAL_HDR_VERSION_3);
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"to restore this series of dumps, use the -%c "
"option on the first restore\n"),
GETOPT_FMT2COMPAT);
return BOOL_FALSE;
}
return BOOL_TRUE;
}
/* recursively descend the tree clearing REFED and DIRDUMPED and NEWORPH
* flags. force the orphanage to be refed and dumped, so we won't try
* to orphan it, and so things added to it won't look like they are
* referenced during ref adj. also null dirattr handles, since they are
* not retained across dump session restores.
*/
static void tree_marknoref_recurse(nh_t parh);
void
tree_marknoref(void)
{
tree_marknoref_recurse(persp->p_rooth);
{
node_t *orphp;
orphp = Node_map(persp->p_orphh);
orphp->n_flags |= (NF_REFED | NF_DUMPEDDIR);
assert(orphp->n_dah == DAH_NULL);
Node_unmap(persp->p_orphh, &orphp);
}
}
static void
tree_marknoref_recurse(nh_t parh)
{
nh_t cldh;
{
node_t *parp;
parp = Node_map(parh);
parp->n_flags &= ~(NF_REFED | NF_DUMPEDDIR | NF_NEWORPH);
parp->n_dah = DAH_NULL;
cldh = parp->n_cldh;
Node_unmap(parh, &parp);
}
while (cldh != NH_NULL) {
node_t *cldp;
nh_t nextcldh;
tree_marknoref_recurse(cldh); /* RECURSION */
cldp = Node_map(cldh);
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
cldh = nextcldh;
}
}
void
tree_markallsubtree(bool_t sensepr)
{
if (!sensepr) {
persp->p_ignoreorphpr = BOOL_TRUE;
}
selsubtree(persp->p_rooth, sensepr);
}
nh_t
tree_begindir(filehdr_t *fhdrp, dah_t *dahp)
{
nh_t hardh;
xfs_ino_t ino = fhdrp->fh_stat.bs_ino;
gen_t gen = fhdrp->fh_stat.bs_gen;
dah_t dah;
if (persp->p_truncategenpr) {
gen = BIGGEN2GEN(gen);
}
/* sanity check - orphino is supposed to be an unused ino!
*/
assert(ino != orphino);
/* lookup head of hardlink list
*/
hardh = link_hardh(ino, gen);
assert(ino != persp->p_rootino || hardh == persp->p_rooth);
/* already present
*/
if (hardh != NH_NULL) {
node_t *hardp;
hardp = Node_map(hardh);
if (!(hardp->n_flags & NF_ISDIR)) {
/* case 1: previously seen as dirent, now know is dir
*/
mlog(MLOG_TRACE | MLOG_TREE,
"directory %llu %u (%u): "
"upgrading to dir\n",
ino,
gen,
fhdrp->fh_stat.bs_gen);
if (!tranp->t_toconlypr) {
assert(hardp->n_dah == DAH_NULL);
hardp->n_dah = dirattr_add(fhdrp);
}
} else if (!tranp->t_toconlypr && hardp->n_dah == DAH_NULL) {
/* case 2: node is a dir, but had thrown away dirattr
*/
mlog(MLOG_TRACE | MLOG_TREE,
"directory %llu %u (%u): "
"updating\n",
ino,
gen,
fhdrp->fh_stat.bs_gen);
hardp->n_dah = dirattr_add(fhdrp);
} else {
/* case 3: already has dirattr; must be restart
*/
mlog(MLOG_TRACE | MLOG_TREE,
"directory %llu %u (%u): "
"retaining\n",
ino,
gen,
fhdrp->fh_stat.bs_gen);
}
hardp->n_flags |= NF_ISDIR;
hardp->n_flags |= NF_DUMPEDDIR;
*dahp = hardp->n_dah;
Node_unmap(hardh, &hardp);
} else {
/* case 4: first time seen
*/
mlog(MLOG_TRACE | MLOG_TREE,
"directory %llu %u (%u): "
"new\n",
ino,
gen,
fhdrp->fh_stat.bs_gen);
if (!tranp->t_toconlypr) {
dah = dirattr_add(fhdrp);
} else {
dah = DAH_NULL;
}
hardh = Node_alloc(ino,
gen,
NRH_NULL,
dah,
NF_ISDIR | NF_NEWORPH);
if (hardh == NH_NULL)
return NH_NULL;
link_in(hardh);
adopt(persp->p_orphh, hardh, NRH_NULL);
*dahp = dah;
}
return hardh;
}
rv_t
tree_addent(nh_t parh, xfs_ino_t ino, gen_t gen, char *name, size_t namelen)
{
nh_t hardh;
if (persp->p_truncategenpr) {
gen = BIGGEN2GEN(gen);
}
/* sanity check - orphino is supposed to be an unused ino!
*/
assert(ino != orphino);
/* don't allow entries named "orphanage" under root to be added
*/
if (parh == persp->p_rooth && !strcmp(name, orphname)) {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"skipping (reserved)\n",
name,
ino,
gen);
return RV_OK;
}
/* if the parent is null, just ignore
*/
if (parh == NH_NULL) {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"skipping (null parent)\n",
name,
ino,
gen);
return RV_OK;
}
/* see if one or more links to this ino already exist.
*/
hardh = link_hardh(ino, gen);
if (hardh != NH_NULL) {
node_t *hardp;
hardp = Node_map(hardh);
if (hardp->n_flags & NF_ISDIR) {
nh_t renameh;
node_t *renamep;
/* REFERENCED */
int namebuflen;
hardp->n_flags |= NF_REFED;
if (hardp->n_parh == persp->p_orphh) {
/* dir now seen as entry
* if in orph but REAL, must be pending rename
*/
if ((hardp->n_flags & NF_REAL)
&&
hardp->n_lnkh == NH_NULL) {
hardp->n_lnkh = Node_alloc(ino,
gen,
NRH_NULL,
DAH_NULL,
0);
if (hardp->n_lnkh == NH_NULL)
return RV_ERROR;
}
if (hardp->n_lnkh != NH_NULL) {
assert(hardp->n_flags & NF_REAL);
renameh = hardp->n_lnkh;
renamep = Node_map(renameh);
if (renamep->n_parh == NH_NULL) {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"adopting (dir par)\n",
name,
ino,
gen);
renamep->n_parh = parh;
}
if (renamep->n_parh != parh) {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"re-adopting (dir par)\n",
name,
ino,
gen);
renamep->n_parh = parh;
}
if (renamep->n_nrh != NRH_NULL) {
namebuflen
=
namreg_get(renamep->n_nrh,
tranp->t_namebuf,
sizeof(tranp->t_namebuf));
assert(namebuflen > 0);
if (strcmp(name,
tranp->t_namebuf)) {
mlog(MLOG_DEBUG
|
MLOG_TREE,
"dirent %s "
"%llu %u: "
"re-adopting "
"(dir name): "
"was %s\n",
name,
ino,
gen,
tranp->t_namebuf);
namreg_del(
renamep->n_nrh);
renamep->n_nrh =
NRH_NULL;
}
}
if (renamep->n_nrh == NRH_NULL) {
renamep->n_nrh
=
namreg_add(name, namelen);
renamep->n_parh = parh;
}
Node_unmap(renameh, &renamep);
} else {
nrh_t nrh;
hardp->n_flags &= ~NF_NEWORPH;
assert(hardp->n_nrh == NRH_NULL);
assert(hardp->n_parh != NH_NULL);
nrh = disown(hardh);
assert(nrh == NRH_NULL);
nrh = namreg_add(name, namelen);
adopt(parh, hardh, nrh);
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"updating (dir)\n",
name,
ino,
gen);
}
} else {
assert(hardp->n_nrh != NRH_NULL);
namebuflen
=
namreg_get(hardp->n_nrh,
tranp->t_namebuf,
sizeof(tranp->t_namebuf));
assert(namebuflen > 0);
if (hardp->n_parh == parh
&&
!strcmp(tranp->t_namebuf, name)) {
/* dir seen as entry again
*/
if (hardp->n_lnkh != NH_NULL) {
/* rescind rename
*/
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"rescind rename (dir)\n",
name,
ino,
gen);
Node_free(&hardp->n_lnkh);
} else {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"retaining (dir)\n",
name,
ino,
gen);
}
} else {
/* dir rename
*/
nh_t renameh;
node_t *renamep;
if (hardp->n_lnkh == NH_NULL) {
renameh = Node_alloc(ino,
gen,
NRH_NULL,
DAH_NULL,
0);
if (renameh == NH_NULL)
return RV_ERROR;
hardp->n_lnkh = renameh;
} else {
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"re-renaming\n",
name,
ino,
gen);
renameh = hardp->n_lnkh;
renamep = Node_map(renameh);
renamep->n_parh = NH_NULL;
if (renamep->n_nrh
!=
NRH_NULL) {
namreg_del(renamep->n_nrh);
}
renamep->n_nrh = NRH_NULL;
Node_unmap(renameh, &renamep);
}
renamep = Node_map(renameh);
assert(hardp->n_parh != NH_NULL);
if (hardp->n_parh != parh) {
/* different parent
*/
renamep->n_parh = parh;
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"renaming (parent)\n",
name,
ino,
gen);
}
if (strcmp(tranp->t_namebuf, name)) {
/* different name
*/
renamep->n_nrh =
namreg_add(name, namelen);
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"renaming (name)\n",
name,
ino,
gen);
}
Node_unmap(renameh, &renamep);
}
}
} else {
nh_t matchh;
matchh = link_matchh(hardh, parh, name);
if (matchh != NH_NULL) {
/* entry already exists
*/
node_t *matchp;
matchp = Node_map(matchh);
matchp->n_flags |= NF_REFED;
Node_unmap(matchh, &matchp);
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"retaining (nondir)\n",
name,
ino,
gen);
} else {
/* case 5: new hard link
*/
nrh_t nrh;
nh_t linkh;
nrh = namreg_add(name, namelen);
linkh = Node_alloc(ino,
gen,
NRH_NULL,
DAH_NULL,
NF_REFED);
if (linkh == NH_NULL)
return RV_ERROR;
link_in(linkh);
adopt(parh, linkh, nrh);
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"adding (link)\n",
name,
ino,
gen);
}
}
Node_unmap(hardh, &hardp);
} else {
/* case 6: new entry
*/
nrh_t nrh;
nrh = namreg_add(name, namelen);
hardh = Node_alloc(ino,
gen,
NRH_NULL,
DAH_NULL,
NF_REFED);
if (hardh == NH_NULL)
return RV_ERROR;
link_in(hardh);
adopt(parh, hardh, nrh);
mlog(MLOG_DEBUG | MLOG_TREE,
"dirent %s %llu %u: "
"adding (new)\n",
name,
ino,
gen);
}
return RV_OK;
}
/* ARGSUSED */
void
tree_enddir(nh_t dirh)
{
}
bool_t
tree_subtree_parse(bool_t sensepr, char *path)
{
nh_t namedh;
nh_t parh;
nh_t cldh;
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
bool_t ok;
/* walk the tree down this relative pathname from the root.
*/
ok = tsi_walkpath(path,
NH_NULL,
persp->p_rooth,
0,
0,
&namedh,
&parh,
&cldh,
&ino,
&isdirpr,
&isselpr);
if (!ok) {
return BOOL_FALSE;
}
/* set or clear flag in this node and all of its children,
* and ajust parentage flags.
*/
selsubtree(namedh, sensepr);
return BOOL_TRUE;
}
/* tree_post - called after the dirdump has been applied.
* first phase is to eliminate all unreferenced dirents.
* done by recursive depth-wise descent of the tree. on the way
* up, unlink or orphan unreferenced nondirs, unlink unreferenced
* dirs, orphan dirs to be renamed. if a dir is unreferenced, but
* contains referenced dirents, orphan those dirents. orphan unreferenced
* nondirs if they are the last link to an inode referenced but not real
* somewhere else in the tree. next, make new directories. then rename
* directories. finally, create hardlinks from orphanage.
*/
static bool_t noref_elim_recurse(nh_t parh,
nh_t cldh,
bool_t parrefpr,
char *path1,
char *path2);
static bool_t mkdirs_recurse(nh_t parh,
nh_t cldh,
char *path);
static bool_t rename_dirs(nh_t cldh,
char *path1,
char *path2);
static bool_t proc_hardlinks(char *path1,
char *path2);
bool_t
tree_post(char *path1, char *path2)
{
node_t *rootp;
node_t *orphp;
nh_t cldh;
bool_t ok;
/* eliminate unreferenced dirents
*/
if (!persp->p_fullpr) {
mlog(MLOG_DEBUG | MLOG_TREE,
"eliminating unreferenced directory entries\n");
rootp = Node_map(persp->p_rooth);
cldh = rootp->n_cldh;
Node_unmap(persp->p_rooth, &rootp);
ok = noref_elim_recurse(persp->p_rooth,
cldh,
BOOL_TRUE,
path1,
path2);
if (!ok) {
return BOOL_FALSE;
}
}
/* make new directories
*/
mlog(MLOG_DEBUG | MLOG_TREE,
"making new directories\n");
rootp = Node_map(persp->p_rooth);
cldh = rootp->n_cldh;
Node_unmap(persp->p_rooth, &rootp);
ok = mkdirs_recurse(persp->p_rooth, cldh, path1);
if (!ok) {
return BOOL_FALSE;
}
#ifdef TREE_CHK
assert(tree_chk());
#endif /* TREE_CHK */
/* rename directories
*/
if (!persp->p_fullpr) {
mlog(MLOG_DEBUG | MLOG_TREE,
"performing directory renames\n");
orphp = Node_map(persp->p_orphh);
cldh = orphp->n_cldh;
Node_unmap(persp->p_orphh, &orphp);
ok = rename_dirs(cldh, path1, path2);
if (!ok) {
return BOOL_FALSE;
}
}
#ifdef TREE_CHK
assert(tree_chk());
#endif /* TREE_CHK */
/* process hard links
*/
if (!persp->p_fullpr) {
mlog(MLOG_DEBUG | MLOG_TREE,
"processing hard links\n");
ok = proc_hardlinks(path1, path2);
if (!ok) {
return BOOL_FALSE;
}
}
return BOOL_TRUE;
}
/* ARGSUSED */
static bool_t
noref_elim_recurse(nh_t parh,
nh_t cldh,
bool_t parrefpr,
char *path1,
char *path2)
{
while (cldh != NH_NULL) {
node_t *cldp;
xfs_ino_t ino;
gen_t gen;
bool_t inorphanagepr;
bool_t isdirpr;
bool_t isrealpr;
bool_t isrefpr;
bool_t isrenamepr;
nh_t renameh;
nh_t grandcldh;
nh_t nextcldh;
int rval;
bool_t ok;
cldp = Node_map(cldh);
ino = cldp->n_ino;
gen = cldp->n_gen;
inorphanagepr = cldp->n_parh == persp->p_orphh;
isdirpr = (cldp->n_flags & NF_ISDIR);
isrealpr = (cldp->n_flags & NF_REAL);
isrefpr = (cldp->n_flags & NF_REFED);
isrenamepr = (isdirpr && cldp->n_lnkh != NH_NULL);
renameh = cldp->n_lnkh;
grandcldh = cldp->n_cldh;
nextcldh = cldp->n_sibh;
#ifdef TREE_DEBUG
ok = Node2path(cldh, path1, _("noref debug"));
mlog(MLOG_TRACE | MLOG_TREE,
"noref: %s: isdir = %d, isreal = %d, isref = %d, "
"isrenamedir = %d\n",
path1, isdirpr, isrealpr, isrefpr, isrenamepr);
#endif
Node_unmap(cldh, &cldp);
if (isdirpr) {
bool_t ok;
ok = noref_elim_recurse(cldh,
grandcldh,
isrefpr,
path1,
path2);
/* RECURSION */
if (!ok) {
return BOOL_FALSE;
}
if (inorphanagepr) {
cldh = nextcldh;
continue;
}
if (!isrefpr) {
nrh_t nrh;
assert(!isrenamepr);
if (isrealpr) {
ok = Node2path(cldh, path1, _("rmdir"));
if (!ok) {
cldh = nextcldh;
continue;
}
mlog(MLOG_TRACE | MLOG_TREE,
"rmdir %s\n",
path1);
rval = rmdir(path1);
if (rval) {
mlog(MLOG_NORMAL
|
MLOG_WARNING
|
MLOG_TREE, _(
"unable to rmdir %s: %s\n"),
path1,
strerror(errno));
cldh = nextcldh;
continue;
}
}
nrh = disown(cldh);
assert(nrh != NRH_NULL);
namreg_del(nrh);
link_out(cldh);
Node_free(&cldh);
}
if (isrenamepr) {
nrh_t nrh;
node_t *renamep;
assert(isrefpr);
assert(isrealpr);
ok = Node2path(cldh,
path1,
_("tmp dir rename src"));
if (!ok) {
cldh = nextcldh;
continue;
}
nrh = disown(cldh);
assert(nrh != NRH_NULL);
adopt(persp->p_orphh, cldh, NRH_NULL);
ok = Node2path(cldh,
path2,
_("tmp dir rename dst"));
if (!ok) {
/* REFERENCED */
nrh_t dummynrh;
dummynrh = disown(cldh);
assert(dummynrh == NRH_NULL);
adopt(parh, cldh, nrh);
cldh = nextcldh;
continue;
}
mlog(MLOG_TRACE | MLOG_TREE,
"rename dir %s to %s\n",
path1,
path2);
rval = rename(path1, path2);
if (rval) {
/* REFERENCED */
nrh_t dummynrh;
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"unable to rename dir %s "
"to dir %s: %s\n"),
path1,
path2,
strerror(errno));
dummynrh = disown(cldh);
assert(dummynrh == NRH_NULL);
adopt(parh, cldh, nrh);
cldh = nextcldh;
continue;
}
cldp = Node_map(cldh);
renamep = Node_map(renameh);
if (renamep->n_nrh == NRH_NULL) {
renamep->n_nrh = nrh;
} else {
namreg_del(nrh);
}
if (renamep->n_parh == NH_NULL) {
renamep->n_parh = parh;
}
cldp->n_flags |= NF_NEWORPH;
Node_unmap(renameh, &renamep);
Node_unmap(cldh, &cldp);
}
} else {
/* determine if we can unlink this node.
* if its not real, and not refed, simple.
* if real and not refed and there is at least
* one unreal refed node and no other real
* nodes around, must put this one in orphanage
* rather than unlinking it.
*/
bool_t mustorphpr;
bool_t canunlinkpr;
if (inorphanagepr) {
cldh = nextcldh;
continue;
}
mustorphpr = BOOL_FALSE;
canunlinkpr = !isrefpr && !isrealpr;
if (!isrefpr && isrealpr) {
nh_t hardh;
bool_t neededpr;
hardh = link_hardh(ino, gen);
assert(hardh != NH_NULL);
canunlinkpr = BOOL_FALSE;
neededpr = BOOL_FALSE;
/* tes@sgi.com:
* This loop seems to assume that
* REAL files come before NON-REALs
* so that we will break out first
* if we find a REAL file.
*/
while (hardh != NH_NULL) {
node_t *hardp;
bool_t hardisrefpr;
bool_t hardisrealpr;
nh_t nexthardh;
hardp = Node_map(hardh);
hardisrefpr = hardp->n_flags & NF_REFED;
hardisrealpr = hardp->n_flags & NF_REAL;
nexthardh = hardp->n_lnkh;
Node_unmap(hardh, &hardp);
if (hardh != cldh && hardisrealpr) {
break;
}
if (hardisrefpr && !hardisrealpr) {
neededpr = BOOL_TRUE;
}
hardh = nexthardh;
}
if (neededpr) {
mustorphpr = BOOL_TRUE;
}
else {
canunlinkpr = BOOL_TRUE;
}
}
if (mustorphpr) {
/* rename file to orphanage */
nrh_t nrh;
assert(!canunlinkpr);
ok = Node2path(cldh,
path1,
_("tmp nondir rename src"));
if (!ok) {
cldh = nextcldh;
continue;
}
nrh = disown(cldh);
assert(nrh != NRH_NULL);
adopt(persp->p_orphh, cldh, NRH_NULL);
ok = Node2path(cldh,
path2,
_("tmp nondir rename dst"));
if (!ok) {
/* REFERENCED */
nrh_t dummynrh;
dummynrh = disown(cldh);
assert(dummynrh == NRH_NULL);
adopt(parh, cldh, nrh);
cldh = nextcldh;
continue;
}
mlog(MLOG_TRACE | MLOG_TREE,
"rename nondir %s to %s\n",
path1,
path2);
rval = rename(path1, path2);
if (rval) {
/* REFERENCED */
nrh_t dummynrh;
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"unable to rename nondir %s "
"to nondir %s: %s\n"),
path1,
path2,
strerror(errno));
dummynrh = disown(cldh);
assert(dummynrh == NRH_NULL);
adopt(parh, cldh, nrh);
cldh = nextcldh;
continue;
}
namreg_del(nrh);
cldp = Node_map(cldh);
cldp->n_flags |= NF_NEWORPH;
Node_unmap(cldh, &cldp);
}
if (canunlinkpr) {
/* REFERENCED */
nrh_t nrh;
assert(!mustorphpr);
if (isrealpr) {
ok = Node2path(cldh, path1, _("rmdir"));
if (!ok) {
cldh = nextcldh;
continue;
}
mlog(MLOG_TRACE | MLOG_TREE,
"unlink %s\n",
path1);
rval = unlink(path1);
if (rval) {
mlog(MLOG_NORMAL
|
MLOG_WARNING
|
MLOG_TREE, _(
"unable to unlink nondir "
"%s: %s\n"),
path1,
strerror(errno));
cldh = nextcldh;
continue;
}
}
nrh = disown(cldh);
assert(nrh != NRH_NULL);
link_out(cldh);
Node_free(&cldh);
}
}
/* next!
*/
cldh = nextcldh;
}
return BOOL_TRUE;
}
/* ARGSUSED */
static bool_t
mkdirs_recurse(nh_t parh, nh_t cldh, char *path)
{
while (cldh != NH_NULL) {
node_t *cldp;
bool_t isdirpr;
bool_t isselpr;
bool_t isrealpr;
bool_t isrefpr;
nh_t grandcldh;
nh_t nextcldh;
cldp = Node_map(cldh);
isdirpr = (cldp->n_flags & NF_ISDIR);
isrealpr = (cldp->n_flags & NF_REAL);
isrefpr = (cldp->n_flags & NF_REFED);
isselpr = (cldp->n_flags & NF_SUBTREE);
grandcldh = cldp->n_cldh;
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
/* if needed, create a directory and update real flag
*/
if (isdirpr && !isrealpr && isrefpr && isselpr) {
int rval;
if (!Node2path(cldh, path, _("makedir"))) {
cldh = nextcldh;
continue;
}
if (tranp->t_toconlypr) {
rval = 0;
} else {
mlog(MLOG_TRACE | MLOG_TREE,
"mkdir %s\n",
path);
rval = mkdir(path, S_IRWXU);
}
if (rval && errno != EEXIST) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"mkdir %s failed: %s\n"),
path,
strerror(errno));
} else {
cldp = Node_map(cldh);
cldp->n_flags |= NF_REAL;
Node_unmap(cldh, &cldp);
isrealpr = BOOL_TRUE;
}
}
/* if a real selected directory, recurse
*/
if (isdirpr && isrealpr && isselpr) {
bool_t ok;
ok = mkdirs_recurse(cldh, grandcldh, path);
/* RECURSION */
if (!ok) {
return BOOL_FALSE;
}
}
/* next!
*/
cldh = nextcldh;
}
return BOOL_TRUE;
}
static bool_t
rename_dirs(nh_t cldh,
char *path1,
char *path2)
{
while (cldh != NH_NULL) {
node_t *cldp;
/* REFERENCED */
nh_t parh;
bool_t isdirpr;
nh_t renameh;
bool_t isrenamepr;
nh_t nextcldh;
nh_t newparh;
nh_t newnrh;
cldp = Node_map(cldh);
parh = cldp->n_parh;
isdirpr = cldp->n_flags & NF_ISDIR;
renameh = cldp->n_lnkh;
isrenamepr = isdirpr && renameh != NH_NULL;
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
assert(parh == persp->p_orphh);
if (isrenamepr) {
node_t *renamep;
int rval;
/* REFERENCED */
nrh_t dummynrh;
bool_t ok;
renamep = Node_map(renameh);
newparh = renamep->n_parh;
newnrh = renamep->n_nrh;
Node_unmap(renameh, &renamep);
ok = Node2path(cldh, path1, _("rename dir"));
if (!ok) {
cldh = nextcldh;
continue;
}
dummynrh = disown(cldh);
assert(dummynrh == NRH_NULL);
adopt(newparh, cldh, newnrh);
ok = Node2path(cldh, path2, _("rename dir"));
if (!ok) {
dummynrh = disown(cldh);
assert(dummynrh == newnrh);
adopt(persp->p_orphh, cldh, NRH_NULL);
cldp = Node_map(cldh);
cldp->n_nrh = NRH_NULL;
Node_unmap(cldh, &cldp);
cldh = nextcldh;
continue;
}
mlog(MLOG_TRACE | MLOG_TREE,
"rename dir %s to %s\n",
path1,
path2);
rval = rename(path1, path2);
if (rval) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"unable to rename dir %s "
"to dir %s: %s\n"),
path1,
path2,
strerror(errno));
dummynrh = disown(cldh);
assert(dummynrh == newnrh);
adopt(persp->p_orphh, cldh, NRH_NULL);
cldh = nextcldh;
continue;
}
cldp = Node_map(cldh);
cldp->n_flags &= ~NF_NEWORPH;
cldp->n_lnkh = NH_NULL;
Node_unmap(cldh, &cldp);
Node_free(&renameh);
}
/* next!
*/
cldh = nextcldh;
}
return BOOL_TRUE;
}
/* will call funcp for all links to be created. will abort if funcp
* ever returns FALSE;
*/
rv_t
tree_cb_links(xfs_ino_t ino,
gen_t gen,
int32_t ctime,
int32_t mtime,
bool_t (*funcp)(void *contextp,
bool_t linkpr,
char *path1,
char *path2),
void *contextp,
char *path1,
char *path2)
{
nh_t hardh;
nh_t nh;
char *path;
bool_t ok;
int rval;
if (persp->p_truncategenpr) {
gen = BIGGEN2GEN(gen);
}
/* find the hardhead
*/
hardh = link_hardh(ino, gen);
/* loop through all hard links, attempting to restore/link
*/
path = path1;
for (nh = hardh; nh != NH_NULL; nh = link_nexth(nh)) {
node_t *np;
u_char_t flags;
char *reasonstr;
/* get the node flags
*/
np = Node_map(nh);
flags = np->n_flags;
Node_unmap(nh, &np);
/* build a pathname
*/
ok = Node2path(nh, path, _("restore"));
if (!ok) {
continue;
}
/* skip if not in selected subtree
*/
if (!(flags & NF_SUBTREE)) {
mlog((MLOG_NITTY + 1) | MLOG_TREE,
"skipping %s (ino %llu gen %u): %s\n",
path,
ino,
gen,
"not in selected subtree");
continue;
}
/* don't restore into the housekeeping directory
*/
if (path_beginswith(path, tranp->t_hkdir)) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"cannot restore %s (ino %llu gen %u): "
"pathname contains %s\n"),
path,
ino,
gen,
tranp->t_hkdir);
continue;
}
/* check if ok to overwrite: don't check if we've already
* been here and decided overwrite ok. if ok, set flag
* so we won't check again. in fact, can't check again
* since restore changes the answer.
*/
if (!(flags & NF_WRITTEN)) {
bool_t exists;
if (!content_overwrite_ok(path,
ctime,
mtime,
&reasonstr,
&exists)) {
mlog(MLOG_TRACE | MLOG_TREE,
"skipping %s (ino %llu gen %u): %s\n",
path,
ino,
gen,
reasonstr);
continue;
} else {
np = Node_map(nh);
np->n_flags |= NF_WRITTEN;
Node_unmap(nh, &np);
/*
* We're the first stream to restore this file.
* Unlink it first to remove extended attributes
* that may have been set since the dump was
* taken.
*/
if (!tranp->t_toconlypr && exists) {
rval = unlink(path);
if (rval && errno != ENOENT) {
mlog(MLOG_NORMAL |
MLOG_WARNING, _(
"unable to unlink "
"current file prior to "
"restore: "
"%s: %s: discarding\n"),
path,
strerror(errno));
continue;
}
}
}
}
/* call callback to restore file / create hard link.
* returns !ok if should abort.
*/
if (path == path2) {
mlog(MLOG_TRACE | MLOG_TREE,
"link %s to %s (%llu %u)\n",
path1,
path2,
ino,
gen);
} else {
mlog(MLOG_TRACE | MLOG_TREE,
"restoring %s (%llu %u)\n",
path1,
ino,
gen);
}
ok = (*funcp)(contextp, path == path2, path1, path2);
if (!ok) {
return RV_NOTOK;
}
/* set flag, indicating node is now real
*/
np = Node_map(nh);
np->n_flags |= NF_REAL;
Node_unmap(nh, &np);
/* switch to second path buffer, for link paths
*/
path = path2;
}
/* if not yet peeled from tape, do so: place in orphanage if
* no references found (i.e., hard link list empty).
*/
if (path == path1) {
if (hardh != NH_NULL || persp->p_ignoreorphpr) {
/*
* The file is referenced so the path is
* valid. This means that if path begins
* with 'orphanage' the file is one of two
* things:
* 1) It's a file that really is an
* orphanage file from a previous restore
* that has now ended up on this dump tape.
* We don't really want to restore this file
* but, it's harmless to do so, it should
* happen rarely, and the path name is
* indistinguishable from ...
* 2) A file whose name was never resolved
* from root because of file corruption.
* Some granparent dir (parent dir of it's
* parent dir) was corrupted so the code that
* walks the trees was thus never able to set
* the NF_SUBTREE flag. It then ends up here
* with a non-resolved name but a valid
* hard reference. We really need to give
* the admin the opportunity to get this
* data since one corrupted dirnode would
* make the whole tree of data
* unreachable. pv698761
*/
if (persp->p_ignoreorphpr || (strncmp(ORPH_NAME, path,
strlen(ORPH_NAME)) != 0)) {
mlog(MLOG_DEBUG | MLOG_TREE,
"discarding %llu %u\n",
ino,
gen);
ok = (*funcp)(contextp, BOOL_FALSE, 0, 0);
if (!ok) {
return RV_NOTOK;
}
} else {
if (!tranp->t_toconlypr) {
char *dir;
char tmp[PATH_MAX];
strcpy(tmp, path);
dir = dirname(tmp);
mkdir_r(dir);
}
mlog (MLOG_VERBOSE | MLOG_NOTE | MLOG_TREE, _(
"ino %llu salvaging file,"
" placing in %s\n"), ino, path1);
ok = (*funcp)(contextp, path == path2,
path1, path2);
if (!ok) {
return RV_NOTOK;
}
}
} else {
mlog(MLOG_VERBOSE | MLOG_NOTE | MLOG_TREE, _(
"ino %llu gen %u not referenced: "
"placing in orphanage\n"),
ino,
gen);
nh = Node_alloc(ino,
gen,
NRH_NULL,
DAH_NULL,
NF_REAL | NF_NEWORPH);
if (nh == NH_NULL) {
mlog(MLOG_ERROR | MLOG_TREE, _(
"node allocation failed when placing ino %llu"
" in orphanage\n"), ino);
return RV_ERROR; /* allocation failed */
}
link_in(nh);
adopt(persp->p_orphh, nh, NRH_NULL);
ok = Node2path(nh, path1, _("orphan"));
assert(ok);
(void)(*funcp)(contextp, BOOL_FALSE, path1, path2);
}
}
return RV_OK;
}
/* uses flags cleared during directory restore (NF_DUMPEDDIR and NF_REFED)
* to determine what directory entries are no longer needed. this can
* be done because whenever a directory chenges, it and all of its current
* entries are dumped. so if an entry is dumped which is a dir, but that
* dir is not dumped, all of that directories entries are needed and so must
* have their REFED flag cleared.
*
* does a recursive depth-wise descent of the tree, following this rule to
* clear the REFED flags.
*/
static void tree_adjref_recurse(nh_t cldh,
bool_t pardumpedpr,
bool_t parrefedpr);
bool_t
tree_adjref(void)
{
tree_adjref_recurse(persp->p_rooth, BOOL_FALSE, BOOL_TRUE);
return BOOL_TRUE;
}
static void
tree_adjref_recurse(nh_t cldh,
bool_t pardumpedpr,
bool_t parrefedpr)
{
nh_t grandcldh;
bool_t clddumpedpr;
bool_t cldrefedpr;
{
node_t *cldp;
cldp = Node_map(cldh);
if (!pardumpedpr && parrefedpr) {
cldp->n_flags |= NF_REFED;
}
clddumpedpr = (int)cldp->n_flags & NF_DUMPEDDIR;
cldrefedpr = (int)cldp->n_flags & NF_REFED;
grandcldh = cldp->n_cldh;
Node_unmap(cldh, &cldp);
}
while (grandcldh != NH_NULL) {
node_t *grandcldp;
nh_t nextgrandcldh;
tree_adjref_recurse(grandcldh, clddumpedpr, cldrefedpr);
/* RECURSION */
grandcldp = Node_map(grandcldh);
nextgrandcldh = grandcldp->n_sibh;
Node_unmap(grandcldh, &grandcldp);
grandcldh = nextgrandcldh;
}
}
static bool_t tree_extattr_recurse(nh_t parh,
nh_t cldh,
bool_t (*cbfunc)(char *path,
dah_t dah),
char *path);
bool_t
tree_extattr(bool_t (*cbfunc)(char *path, dah_t dah), char *path)
{
node_t *rootp;
nh_t cldh;
bool_t ok;
rootp = Node_map(persp->p_rooth);
cldh = rootp->n_cldh;
Node_unmap(persp->p_rooth, &rootp);
ok = tree_extattr_recurse(persp->p_rooth, cldh, cbfunc, path);
return ok;
}
static bool_t
tree_extattr_recurse(nh_t parh,
nh_t cldh,
bool_t (*cbfunc)(char *path, dah_t dah),
char *path)
{
node_t *parp;
dah_t dah;
bool_t ok;
/* first update all children
*/
while (cldh != NH_NULL) {
node_t *cldp;
bool_t isdirpr;
bool_t isselpr;
bool_t isrealpr;
nh_t grandcldh;
nh_t nextcldh;
cldp = Node_map(cldh);
isdirpr = (cldp->n_flags & NF_ISDIR);
isrealpr = (cldp->n_flags & NF_REAL);
isselpr = (cldp->n_flags & NF_SUBTREE);
grandcldh = cldp->n_cldh;
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
/* if a real selected directory, recurse
*/
if (isdirpr && isrealpr && isselpr) {
bool_t ok;
ok = tree_extattr_recurse(cldh,
grandcldh,
cbfunc,
path);
/* RECURSION */
if (!ok) {
return BOOL_FALSE;
}
}
/* next!
*/
cldh = nextcldh;
}
/* now update self
*/
parp = Node_map(parh);
dah = parp->n_dah;
Node_unmap(parh, &parp);
if (!Node2path(parh, path, _("set dir extattr"))) {
mlog (MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"tree_extattr_recurse: Could not convert node to "
"path for %s\n"), path);
return BOOL_TRUE;
}
if (dah != DAH_NULL) {
ok = (*cbfunc)(path, dah);
} else {
ok = BOOL_TRUE;
}
return ok;
}
struct phcb {
char *path1;
char *path2;
bool_t ok;
};
typedef struct phcb phcb_t;
static bool_t proc_hardlinks_cb(void *contextp, nh_t hardheadh);
static bool_t
proc_hardlinks(char *path1, char *path2)
{
phcb_t phcb;
/* have callback invoked for all hardheads
*/
phcb.path1 = path1;
phcb.path2 = path2;
phcb.ok = BOOL_TRUE;
link_headiter(proc_hardlinks_cb, (void *)&phcb);
return phcb.ok;
}
/* called for every hard head
*
* tes@sgi.com:
* This code processes the hardlinks, extracting out a
* src_list - real & !ref
* dest_list - !real & ref
* The src_list are the entries to delete and the dst_list
* are the new_entries to create.
* 1. If we have a src and a dst then we can do a rename.
* 2. If we have extra src nodes then we can unlink them.
* 3. If we have extra dst nodes then we can link them.
* HOWEVER, the linking in of the new nodes is actually handled
* in the restore_file_cb() code which on restoral creates
* the file and all its hardlinks.
* ALSO, I can not see how the src_list can have more than
* 1 node in it, since in noref_elim_recurse() it unlinks all
* extra deleted entries (real & !ref).
* Thus, steps 2 and 3 are handled in noref_elim_recurse().
*/
static bool_t
proc_hardlinks_cb(void *contextp, nh_t hardheadh)
{
phcb_t *phcbp = (phcb_t *)contextp;
node_t *hardheadp;
xfs_ino_t ino;
gen_t gen;
bool_t isdirpr;
nh_t rnsrcheadh;
nh_t rndstheadh;
nh_t lnsrch;
nh_t nh;
link_iter_context_t link_iter_context;
bool_t ok;
int rval;
/* skip directories
*/
hardheadp = Node_map(hardheadh);
ino = hardheadp->n_ino;
gen = hardheadp->n_gen;
isdirpr = hardheadp->n_flags & NF_ISDIR;
Node_unmap(hardheadh, &hardheadp);
if (isdirpr) {
return BOOL_TRUE;
}
mlog(MLOG_DEBUG | MLOG_TREE,
"processing hardlinks to %llu %u\n",
ino,
gen);
/* first pass through hard link list: for each node, leave on
* list, unlink and place on rename src list, unlink and place on
* rename dst list, or unlink and discard. note a node available
* to link from, in case we need it.
*/
rnsrcheadh = NH_NULL;
rndstheadh = NH_NULL;
lnsrch = NH_NULL;
link_iter_init(&link_iter_context, hardheadh);
while ((nh = link_iter_next(&link_iter_context)) != NH_NULL) {
node_t *np = Node_map(nh);
bool_t isrealpr = np->n_flags & NF_REAL;
bool_t isrefpr = np->n_flags & NF_REFED;
bool_t isselpr = np->n_flags & NF_SUBTREE;
/* if unrefed, unreal, free node etc. (sel doesn't matter)
*/
if (!isrealpr && !isrefpr) {
mlog(MLOG_NITTY | MLOG_TREE,
"freeing node %x: not real, not referenced\n",
nh);
link_iter_unlink(&link_iter_context, nh);
Node_unmap(nh, &np);
(void)disown(nh);
Node_free(&nh);
continue;
}
/* not real, refed, but not selected, can't help
*/
if (!isrealpr && isrefpr && !isselpr) {
mlog(MLOG_NITTY | MLOG_TREE,
"skipping node %x: not selected\n",
nh);
Node_unmap(nh, &np);
continue;
}
/* if unreal, refed, sel, add to dst list,
*/
if (!isrealpr && isrefpr && isselpr) {
mlog(MLOG_NITTY | MLOG_TREE,
"making node %x dst: "
"not real, refed, sel\n",
nh);
link_iter_unlink(&link_iter_context, nh);
np->n_lnkh = rndstheadh;
rndstheadh = nh;
Node_unmap(nh, &np);
continue;
}
/* if real, unrefed, sel, add to src list
*/
if (isrealpr && !isrefpr && isselpr) {
mlog(MLOG_NITTY | MLOG_TREE,
"making node %x src: real, not refed, sel\n",
nh);
link_iter_unlink(&link_iter_context, nh);
np->n_lnkh = rnsrcheadh;
rnsrcheadh = nh;
Node_unmap(nh, &np);
continue;
}
/* would like to get rid of but not selected, or
* real and referenced, leave alone (sel doesn't matter).
* consider as a lnk src, since real and not going away.
*/
if (isrealpr && (isrefpr || !isselpr)) {
mlog(MLOG_NITTY | MLOG_TREE,
"skipping node %x: %s\n",
nh,
isselpr ? "real and ref" : "real and not sel");
Node_unmap(nh, &np);
if (lnsrch == NH_NULL) {
mlog(MLOG_NITTY | MLOG_TREE,
"node %x will be link src\n",
nh);
lnsrch = nh;
}
continue;
}
assert(0);
}
/* now pass through dst list, doing renames if src list not empty,
* otherwise links if a lnk src available, otherwise put back in link
* list
*/
while (rndstheadh != NH_NULL) {
nh_t dsth;
node_t *dstp;
bool_t successpr;
dsth = rndstheadh;
dstp = Node_map(dsth);
rndstheadh = dstp->n_lnkh;
dstp->n_lnkh = NH_NULL;
Node_unmap(dsth, &dstp);
/* build pathname to dst
*/
ok = Node2path(dsth, phcbp->path2, _("rename to"));
if (!ok) {
link_in(dsth);
continue;
}
successpr = BOOL_FALSE;
while (!successpr && rnsrcheadh != NH_NULL) {
nh_t srch;
nrh_t nrh;
node_t *srcp;
srch = rnsrcheadh;
srcp = Node_map(srch);
rnsrcheadh = srcp->n_lnkh;
srcp->n_lnkh = NH_NULL;
Node_unmap(srch, &srcp);
/* build a path to src
*/
ok = Node2path(srch, phcbp->path1, _("rename from"));
if (!ok) {
link_in(srch);
continue;
}
if (tranp->t_toconlypr) {
rval = 0;
} else {
mlog(MLOG_TRACE | MLOG_TREE,
"rename nondir %s to %s\n",
phcbp->path1,
phcbp->path2);
rval = rename(phcbp->path1,
phcbp->path2);
}
if (rval) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable to rename nondir "
"%s to %s: %s\n"),
phcbp->path1,
phcbp->path2,
strerror(errno));
link_in(srch);
continue;
}
nrh = disown(srch);
if (nrh != NRH_NULL) {
namreg_del(nrh);
}
Node_free(&srch);
successpr = BOOL_TRUE;
}
/* tes@sgi.com: note: loop of one iteration only
*/
while (!successpr && lnsrch != NH_NULL) {
ok = Node2path(lnsrch, phcbp->path1, _("link"));
if (!ok) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable to use %s "
"as a hard link source\n"),
phcbp->path1);
lnsrch = NH_NULL;
continue;
}
if (tranp->t_toconlypr) {
rval = 0;
} else {
mlog(MLOG_TRACE | MLOG_TREE,
"link nondir %s to %s\n",
phcbp->path1,
phcbp->path2);
rval = link(phcbp->path1,
phcbp->path2);
}
if (rval) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE,
"unable to link nondir "
"%s to %s: %s\n",
phcbp->path1,
phcbp->path2,
strerror(errno));
break;
}
successpr = BOOL_TRUE;
}
if (!successpr) {
mlog(MLOG_NITTY | MLOG_TREE,
"no link src for node %x\n",
dsth);
} else {
dstp = Node_map(dsth);
dstp->n_flags |= NF_REAL;
Node_unmap(dsth, &dstp);
}
link_in(dsth);
}
/* finally, pass through remaining src list, unlinking/disowning.
* tes@sgi.com: don't believe this will happen as this step
* should now be done in noref_elim_recurse().
*/
while (rnsrcheadh != NH_NULL) {
nh_t srch;
node_t *srcp;
bool_t ok;
srch = rnsrcheadh;
srcp = Node_map(srch);
rnsrcheadh = srcp->n_lnkh;
srcp->n_lnkh = NH_NULL;
Node_unmap(srch, &srcp);
ok = Node2path(srch, phcbp->path1, _("unlink"));
if (!ok) {
link_in(srch);
continue;
}
if (tranp->t_toconlypr) {
rval = 0;
} else {
mlog(MLOG_TRACE | MLOG_TREE,
"unlink nondir %s\n",
phcbp->path1);
rval = unlink(phcbp->path1);
}
if (rval) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable to unlink %s: %s\n"),
phcbp->path1,
strerror(errno));
link_in(srch);
} else {
nrh_t nrh;
nrh = disown(srch);
if (nrh != NRH_NULL) {
namreg_del(nrh);
}
Node_free(&srch);
}
}
return BOOL_TRUE;
}
/* traverse tree depth-wise bottom-up for dirs no longer referenced.
* if non-empty, move children to orphanage
*/
bool_t
tree_setattr(char *path)
{
bool_t ok;
node_t *rootp;
ok = tree_setattr_recurse(persp->p_rooth, path);
if (restore_rootdir_permissions && ok) {
rootp = Node_map(persp->p_rooth);
/* "." is cwd which is the destination dir */
setdirattr(rootp->n_dah, ".");
Node_unmap(persp->p_rooth, &rootp);
}
return ok;
}
static bool_t
tree_setattr_recurse(nh_t parh, char *path)
{
node_t *parp = Node_map(parh);
nh_t cldh = parp->n_cldh;
Node_unmap(parh, &parp);
while (cldh != NH_NULL) {
nh_t nextcldh;
/* get the node attributes
*/
node_t *cldp = Node_map(cldh);
bool_t isdirpr = (cldp->n_flags & NF_ISDIR);
bool_t isselpr = (cldp->n_flags & NF_SUBTREE);
bool_t isrealpr = (cldp->n_flags & NF_REAL);
dah_t dah = cldp->n_dah;
/* get next cld
*/
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
/* if is a real selected dir, go ahead.
*/
if (isdirpr && isselpr && isrealpr) {
bool_t ok;
ok = tree_setattr_recurse(cldh, path); /* RECURSION */
if (!ok) {
Node_unmap(cldh, &cldp);
return BOOL_FALSE;
}
if (dah != DAH_NULL) {
bool_t ok;
ok = Node2path(cldh, path, _("set dirattr"));
if (ok) {
setdirattr(dah, path);
}
}
}
cldh = nextcldh;
}
return BOOL_TRUE;
}
static void
setdirattr(dah_t dah, char *path)
{
mode_t mode;
struct utimbuf utimbuf;
struct fsxattr fsxattr;
int rval;
size_t hlen;
void *hanp;
int fd = -1;
if (dah == DAH_NULL)
return;
if (tranp->t_dstdirisxfspr) {
if (path_to_handle(path, &hanp, &hlen)) {
mlog(MLOG_NORMAL | MLOG_WARNING,
_("path_to_handle of %s failed:%s\n"),
path, strerror(errno));
} else {
fd = open_by_handle(hanp, hlen, O_RDONLY);
if (fd < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING,
_("open_by_handle of %s failed:%s\n"),
path, strerror(errno));
}
free_handle(hanp, hlen);
}
}
if (tranp->t_dstdirisxfspr && persp->p_restoredmpr) {
fsdmidata_t fssetdm;
fssetdm.fsd_dmevmask = dirattr_get_dmevmask(dah);
fssetdm.fsd_padding = 0; /* not used */
fssetdm.fsd_dmstate = (uint16_t)dirattr_get_dmstate(dah);
/* restore DMAPI event settings etc.
*/
rval = ioctl(fd,
XFS_IOC_FSSETDM,
(void *)&fssetdm);
if (rval) {
mlog(errno == EINVAL
?
(MLOG_NITTY + 1) | MLOG_TREE
:
MLOG_NITTY | MLOG_TREE,
"set DMI attributes"
" of %s failed: %s\n",
path,
strerror(errno));
}
}
utimbuf.actime = dirattr_get_atime(dah);
utimbuf.modtime = dirattr_get_mtime(dah);
rval = utime(path, &utimbuf);
if (rval) {
mlog(MLOG_VERBOSE | MLOG_TREE, _(
"could not set access and modification times"
" of %s: %s\n"),
path,
strerror(errno));
}
mode = dirattr_get_mode(dah);
if (persp->p_ownerpr) {
rval = chown(path,
dirattr_get_uid(dah),
dirattr_get_gid(dah));
if (rval) {
mlog(MLOG_NORMAL | MLOG_TREE, _(
"chown (uid=%d, gid=%d) %s failed: %s\n"),
dirattr_get_uid(dah),
dirattr_get_gid(dah),
path,
strerror(errno));
}
}
rval = chmod(path, mode);
if (rval) {
mlog(MLOG_NORMAL | MLOG_TREE, _(
"chmod %s failed: %s\n"),
path,
strerror(errno));
}
/* set the extended inode flags
*/
if (!tranp->t_dstdirisxfspr)
return;
memset((void *)&fsxattr, 0, sizeof(fsxattr));
fsxattr.fsx_xflags = dirattr_get_xflags(dah);
fsxattr.fsx_extsize = dirattr_get_extsize(dah);
fsxattr.fsx_projid = dirattr_get_projid(dah);
rval = ioctl(fd,
XFS_IOC_FSSETXATTR,
(void *)&fsxattr);
if (rval < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING,
_("attempt to set "
"extended attributes "
"(xflags 0x%x, "
"extsize = 0x%x, "
"projid = 0x%x) "
"of %s failed: "
"%s\n"),
fsxattr.fsx_xflags,
fsxattr.fsx_extsize,
fsxattr.fsx_projid,
path,
strerror(errno));
}
(void)close(fd);
}
/* deletes orphanage if empty, else warns
*/
bool_t
tree_delorph(void)
{
int rval;
rval = rmdir(tranp->t_orphdir);
if (rval) {
if (errno == EEXIST) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable to rmdir %s: not empty\n"),
tranp->t_orphdir);
} else {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable to rmdir %s: %s\n"),
tranp->t_orphdir,
strerror(errno));
}
}
return BOOL_TRUE;
}
/* definition of locally defined static functions ****************************/
/* interactive subtree abstraction *******************************************/
static void tsi_cmd_inst(void *, dlog_pcbp_t, void *);
static void tsi_cmd_pwd(void *, dlog_pcbp_t, void *);
static void tsi_cmd_ls(void *, dlog_pcbp_t, void *);
static void tsi_cmd_cd(void *, dlog_pcbp_t, void *);
static void tsi_cmd_add(void *, dlog_pcbp_t, void *);
static void tsi_cmd_delete(void *, dlog_pcbp_t, void *);
static void tsi_cmd_extract(void *, dlog_pcbp_t, void *);
static void tsi_cmd_quit(void *, dlog_pcbp_t, void *);
static void tsi_cmd_help(void *, dlog_pcbp_t, void *);
static void tsi_cmd_parse(char *);
static dlog_ucbp_t tsi_cmd_match(void);
#define PREAMBLEMAX 3
#define ACKMAX 3
#define POSTAMBLEMAX 3
bool_t
tree_subtree_inter(void)
{
fold_t fold;
char *preamblestr[PREAMBLEMAX];
size_t preamblecnt;
char *ackstr[ACKMAX];
size_t ackcnt;
char *postamblestr[POSTAMBLEMAX];
size_t postamblecnt;
dlog_ucbp_t cmdp;
restart:
/* make the current working directory the root of the fs
*/
tranp->t_inter.i_cwdh = persp->p_rooth;
/* begin the dialog
*/
preamblecnt = 0;
fold_init(fold, _("subtree selection dialog"), '=');
preamblestr[preamblecnt++ ] = "\n";
preamblestr[preamblecnt++] = fold;
preamblestr[preamblecnt++ ] = "\n\n";
assert(preamblecnt <= PREAMBLEMAX);
dlog_begin(preamblestr, preamblecnt);
/* execute commands until time to extract or quit. always begin with
* an implicit instructions command. if see SIGINT, give main thread
* dialog a chance to decide if a session interrupt is wanted.
*/
cmdp = tsi_cmd_inst;
do {
char buf[MAXPATHLEN];
const ix_t abortix = 1;
const ix_t sigintix = 2;
const ix_t okix = 3;
ix_t responseix;
/* execute command and get response
*/
responseix = dlog_string_query(cmdp,
0,
buf,
sizeof(buf),
0,
IXMAX, /* timeout ix */
sigintix, /* sigint ix */
abortix, /* sighup ix */
abortix, /* sigquit ix */
okix); /* ok ix */
/* ack the response
*/
ackcnt = 0;
if (responseix == sigintix) {
ackstr[ackcnt++ ] = _("keyboard interrupt\n");
} else if (responseix == abortix) {
ackstr[ackcnt++ ] = _("abort\n");
} else {
assert(responseix == okix);
}
assert(ackcnt <= ACKMAX);
dlog_string_ack(ackstr,
ackcnt);
/* exception handling
*/
if (responseix != okix) {
/* if exception, end the dialog. may restart
* if operator decidesd not to intr.
*/
postamblecnt = 0;
fold_init(fold, _("end dialog"), '-');
postamblestr[postamblecnt++ ] = "\n";
postamblestr[postamblecnt++] = fold;
postamblestr[postamblecnt++ ] = "\n\n";
assert(postamblecnt <= POSTAMBLEMAX);
dlog_end(postamblestr, postamblecnt);
/* if sighup or sigquit, immediately quit
*/
if (responseix == abortix) {
return BOOL_FALSE;
}
/* if sigint, allow main thread to decide if
* operator really wants to quit
*/
assert(responseix == sigintix);
if (cldmgr_stop_requested()) {
return BOOL_FALSE;
}
sleep(1); /* to allow main thread to begin dialog */
mlog(MLOG_NORMAL | MLOG_BARE | MLOG_TREE,
""); /* block until main thread dialog done */
sleep(1); /* let main thread ask children to die */
if (cldmgr_stop_requested()) {
return BOOL_FALSE;
}
mlog(MLOG_DEBUG | MLOG_TREE,
"retrying interactive subtree selection dialog\n");
goto restart;
}
tsi_cmd_parse(buf);
cmdp = tsi_cmd_match();
if (!cmdp) {
cmdp = tsi_cmd_help;
}
} while (cmdp != tsi_cmd_quit && cmdp != tsi_cmd_extract);
postamblecnt = 0;
fold_init(fold, _("end dialog"), '-');
postamblestr[postamblecnt++ ] = "\n";
postamblestr[postamblecnt++] = fold;
postamblestr[postamblecnt++ ] = "\n\n";
assert(postamblecnt <= POSTAMBLEMAX);
dlog_end(postamblestr, postamblecnt);
/* pv 773569 - quit is not a reason to consider session
* to be interrupted (we haven't started yet) so just unmark
* any selected directories and return */
if (cmdp == tsi_cmd_quit) {
mlog(MLOG_NORMAL, _("Unmark and quit\n"));
selsubtree(persp->p_rooth, BOOL_FALSE);
}
return BOOL_TRUE;
}
static void
tsi_cmd_inst(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
tsi_cmd_help(ctxp, pcb, pctxp);
}
static void
tsi_cmd_pwd(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
/* special case root
*/
if (tranp->t_inter.i_cwdh == persp->p_rooth) {
(*pcb )(pctxp, "cwd is fs root\n");
return;
}
/* ascend tree recursively, print path on way back
*/
tsi_cmd_pwd_recurse(ctxp, pcb, pctxp, tranp->t_inter.i_cwdh);
(*pcb )(pctxp, "\n");
}
static void
tsi_cmd_pwd_recurse(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp,
nh_t nh)
{
node_t *np;
register nh_t parh;
/* REFERENCED */
register int namelen;
nrh_t nrh;
assert(nh != NH_NULL);
np = Node_map(nh);
nrh = np->n_nrh;
parh = np->n_parh;
Node_unmap(nh, &np);
if (parh != persp->p_rooth) {
tsi_cmd_pwd_recurse(ctxp, pcb, pctxp, parh);
/* RECURSION */
(*pcb )(pctxp, "/");
}
assert(nrh != NRH_NULL);
namelen = namreg_get(nrh,
tranp->t_inter.i_name,
sizeof(tranp->t_inter.i_name));
assert(namelen > 0);
(*pcb)(pctxp, tranp->t_inter.i_name);
}
/* ARGSUSED */
static void
tsi_cmd_ls(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
size_t argc = tranp->t_inter.i_argc;
char *arg = (argc > 1) ? tranp->t_inter.i_argv[1] : 0;
bool_t ok;
nh_t cldh;
nh_t parh;
nh_t namedh;
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
/* walk the tree according to the path argument, to get
* the named node.
*/
ok = tsi_walkpath(arg,
persp->p_rooth,
tranp->t_inter.i_cwdh,
pcb,
pctxp,
&namedh,
&parh,
&cldh,
&ino,
&isdirpr,
&isselpr);
if (!ok) {
return;
}
/* if named is not a dir, just display named
*/
if (!isdirpr) {
(*pcb)(pctxp,
" %s %10llu %s%s\n",
isselpr ? "*" : " ",
ino,
tranp->t_inter.i_name,
isdirpr ? "/" : " ");
return;
}
/* iterate through the directory, printing all matching entries.
* hide the orphanage.
*/
while (cldh != NH_NULL) {
node_t *cldp;
nrh_t nrh;
nh_t nextcldh;
cldp = Node_map(cldh);
nrh = cldp->n_nrh;
nextcldh = cldp->n_sibh;
isdirpr = (cldp->n_flags & NF_ISDIR);
isselpr = (cldp->n_flags & NF_SUBTREE);
ino = cldp->n_ino;
Node_unmap(cldh, &cldp);
if (cldh != persp->p_orphh) {
/* REFERENCED */
int namelen;
namelen = namreg_get(nrh,
tranp->t_inter.i_name,
sizeof(tranp->t_inter.i_name));
assert(namelen > 0);
(*pcb)(pctxp,
" %s %10llu %s%s\n",
isselpr ? "*" : " ",
ino,
tranp->t_inter.i_name,
isdirpr ? "/" : " ");
}
cldh = nextcldh;
}
}
/* ARGSUSED */
static void
tsi_cmd_cd(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
size_t argc = tranp->t_inter.i_argc;
char *arg = (argc > 1) ? tranp->t_inter.i_argv[1] : 0;
nh_t cldh;
nh_t parh;
nh_t namedh;
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
bool_t ok;
/* walk the tree according to the path argument, to get
* the named node.
*/
ok = tsi_walkpath(arg,
persp->p_rooth,
tranp->t_inter.i_cwdh,
pcb,
pctxp,
&namedh,
&parh,
&cldh,
&ino,
&isdirpr,
&isselpr);
if (!ok) {
return;
}
/* if named is not a dir, complain
*/
if (!isdirpr) {
assert(arg);
(*pcb)(pctxp,
_("%s is not a directory\n"),
arg);
return;
}
/* change the current working directory
*/
tranp->t_inter.i_cwdh = namedh;
}
/* ARGSUSED */
static void
tsi_cmd_add(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
size_t argc = tranp->t_inter.i_argc;
char *arg = (argc > 1) ? tranp->t_inter.i_argv[1] : 0;
nh_t cldh;
nh_t parh;
nh_t namedh;
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
bool_t ok;
/* walk the tree according to the path argument, to get
* the named node.
*/
ok = tsi_walkpath(arg,
persp->p_rooth,
tranp->t_inter.i_cwdh,
pcb,
pctxp,
&namedh,
&parh,
&cldh,
&ino,
&isdirpr,
&isselpr);
if (!ok) {
return;
}
selsubtree(namedh, BOOL_TRUE);
}
/* ARGSUSED */
static void
tsi_cmd_delete(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
size_t argc = tranp->t_inter.i_argc;
char *arg = (argc > 1) ? tranp->t_inter.i_argv[1] : 0;
nh_t cldh;
nh_t parh;
nh_t namedh;
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
bool_t ok;
/* walk the tree according to the path argument, to get
* the named node.
*/
ok = tsi_walkpath(arg,
persp->p_rooth,
tranp->t_inter.i_cwdh,
pcb,
pctxp,
&namedh,
&parh,
&cldh,
&ino,
&isdirpr,
&isselpr);
if (!ok) {
return;
}
selsubtree(namedh, BOOL_FALSE);
}
/* ARGSUSED */
static void
tsi_cmd_extract(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
}
/* ARGSUSED */
static void
tsi_cmd_quit(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
}
static int parse(int slotcnt, char **slotbuf, char *string);
static void
tsi_cmd_parse(char *buf)
{
int wordcnt;
if (!buf) {
tranp->t_inter.i_argc = 0;
return;
}
wordcnt = parse(INTER_ARGMAX, tranp->t_inter.i_argv, buf);
tranp->t_inter.i_argc = (size_t)min(max(0, wordcnt), INTER_ARGMAX);
}
struct tsi_cmd_tbl {
char *tct_pattern;
char *tct_help;
dlog_ucbp_t tct_ucb;
size_t tct_argcmin;
size_t tct_argcmax;
};
typedef struct tsi_cmd_tbl tsi_cmd_tbl_t;
static tsi_cmd_tbl_t tsi_cmd_tbl[] = {
{ "pwd", "", tsi_cmd_pwd, 1, 1 },
{ "ls", "[ <path> ]", tsi_cmd_ls, 1, 2 },
{ "cd", "[ <path> ]", tsi_cmd_cd, 1, 2 },
{ "add", "[ <path> ]", tsi_cmd_add, 1, 2 },
{ "delete", "[ <path> ]", tsi_cmd_delete, 1, 2 },
{ "extract", "", tsi_cmd_extract,1, 1 },
{ "quit", "", tsi_cmd_quit, 1, 1 },
{ "help", "", tsi_cmd_help, 1, 1 },
};
static dlog_ucbp_t
tsi_cmd_match(void)
{
tsi_cmd_tbl_t *tblp = tsi_cmd_tbl;
tsi_cmd_tbl_t *tblendp = tsi_cmd_tbl
+
sizeof(tsi_cmd_tbl)
/
sizeof(tsi_cmd_tbl[0]);
if (tranp->t_inter.i_argc == 0) {
return 0;
}
for (; tblp < tblendp; tblp++) {
if (!strncmp(tranp->t_inter.i_argv[0],
tblp->tct_pattern,
strlen(tranp->t_inter.i_argv[0]))) {
break;
}
}
if (tblp == tblendp) {
return 0;
}
assert(tblp->tct_argcmin != 0);
if (tranp->t_inter.i_argc < tblp->tct_argcmin) {
return 0;
}
if (tranp->t_inter.i_argc > tblp->tct_argcmax) {
return 0;
}
return tblp->tct_ucb;
}
/* ARGSUSED */
static void
tsi_cmd_help(void *ctxp,
dlog_pcbp_t pcb,
void *pctxp)
{
tsi_cmd_tbl_t *tblp = tsi_cmd_tbl;
tsi_cmd_tbl_t *tblendp = tsi_cmd_tbl
+
sizeof(tsi_cmd_tbl)
/
sizeof(tsi_cmd_tbl[0]);
(*pcb )(pctxp, _("the following commands are available:\n"));
for (; tblp < tblendp; tblp++) {
(*pcb)(pctxp,
"\t%s %s\n",
tblp->tct_pattern,
tblp->tct_help);
}
}
/* walks the tree following the given path.
* returns FALSE if syntax error encountered.
* returns by reference handles to the named node, its parent,
* the first child in its cld list, its ino, if it is a directory,
* and if it is selected.
* optionally given a dlog print func and context, to be used for diag output.
*/
static bool_t
tsi_walkpath(char *arg, nh_t rooth, nh_t cwdh,
dlog_pcbp_t pcb, void *pctxp,
nh_t *namedhp, nh_t *parhp, nh_t *cldhp,
xfs_ino_t *inop, bool_t *isdirprp, bool_t *isselprp)
{
nh_t namedh;
nh_t parh;
nh_t cldh;
node_t *namedp;
char *path;
char nbuf[NAME_MAX + 1];
xfs_ino_t ino;
bool_t isdirpr;
bool_t isselpr;
/* correct arg if ends with slash (if arg is "/", ok)
*/
if (arg && strlen(arg) > 1 && arg[strlen(arg) - 1] == '/') {
arg[strlen(arg) - 1] = 0;
}
/* use path to walk down the path argument
*/
path = arg;
/* walk the tree beginning either at the root node
* or at the current working directory
*/
if (path && *path == '/') {
assert(rooth != NH_NULL);
namedh = rooth;
path++;
} else {
assert(cwdh != NH_NULL);
namedh = cwdh;
}
/* get the parent of the starting point, and its cld list
*/
namedp = Node_map(namedh);
parh = namedp->n_parh;
cldh = namedp->n_cldh;
ino = namedp->n_ino;
isselpr = (namedp->n_flags & NF_SUBTREE);
assert(namedp->n_flags & NF_ISDIR);
Node_unmap(namedh, &namedp);
isdirpr = BOOL_TRUE;
/* walk the tree from the starting point following the path arg.
* on leaving this loop, the following will be valid:
* namedh - the handle of the node named by path arg;
* parh - the parent of the named node;
* isdirpr - TRUE if named node is a directory;
* cldh - the first child in the named node's cld list.
*/
for (;;) {
size_t namelen;
char *strpatchp;
nh_t sibh;
/* if no path arg, break
*/
if (!path) {
break;
}
/* clean out leading slashes. these can occur if the
* path arg is ".../////..." or "////..."
*/
while (*path == '/') {
path++;
}
/* if empty path arg, break
*/
if (!strlen(path)) {
break;
}
/* copy the first name from the path, and advance
* the path pointer.
*/
namelen = strcspn(path, "/");
assert(namelen < sizeof(nbuf));
strncpy(nbuf, path, namelen);
nbuf[namelen] = 0;
path += namelen;
if (*path) {
assert(*path == '/');
strpatchp = path;
*strpatchp = 0;
path++;
} else {
strpatchp = 0;
}
/* be sure the named node is a dir
*/
if (!isdirpr) {
if (pcb) {
(*pcb)(pctxp, _(
"parent of %s is not a directory\n"),
arg);
}
return BOOL_FALSE;
}
/* special case "."
*/
if (!strcmp(nbuf, ".")) {
if (strpatchp) {
*strpatchp = '/';
}
continue;
}
/* special case ".."
*/
if (!strcmp(nbuf, "..")) {
if (parh == NH_NULL) {
if (pcb) {
(*pcb)(pctxp, _(
"%s above root\n"),
arg);
}
return BOOL_FALSE;
}
namedh = parh;
namedp = Node_map(namedh);
parh = namedp->n_parh;
cldh = namedp->n_cldh;
ino = namedp->n_ino;
isselpr = (namedp->n_flags & NF_SUBTREE);
Node_unmap(namedh, &namedp);
if (strpatchp) {
*strpatchp = '/';
}
continue;
}
/* look for child with right name
*/
sibh = cldh;
while (sibh != NH_NULL) {
node_t *sibp;
nh_t nextsibh;
nrh_t nrh;
/* REFERENCED */
int siblen;
sibp = Node_map(sibh);
nrh = sibp->n_nrh;
nextsibh = sibp->n_sibh;
cldh = sibp->n_cldh;
ino = sibp->n_ino;
isselpr = (sibp->n_flags & NF_SUBTREE);
isdirpr = (sibp->n_flags & NF_ISDIR);
Node_unmap(sibh, &sibp);
assert(nrh != NRH_NULL || sibh == persp->p_orphh);
if (nrh != NRH_NULL) {
siblen = namreg_get(nrh,
tranp->t_inter.i_name,
sizeof(tranp->t_inter.i_name));
assert(siblen > 0);
if (!strcmp(nbuf, tranp->t_inter.i_name)) {
break;
}
}
sibh = nextsibh;
}
/* if no match, complain
*/
if (sibh == NH_NULL) {
if (pcb) {
(*pcb)(pctxp, _(
"%s not found\n"),
arg);
}
return BOOL_FALSE;
}
/* continue search. cldh, ino and isdirpr
* set in inner loop above
*/
parh = namedh;
namedh = sibh;
if (strpatchp) {
*strpatchp = '/';
}
}
*namedhp = namedh;
*parhp = parh;
*cldhp = cldh;
*inop = ino;
*isdirprp = isdirpr;
*isselprp = isselpr;
return BOOL_TRUE;
}
/* Node abstraction *********************************************************/
static nh_t
Node_alloc(xfs_ino_t ino, gen_t gen, nrh_t nrh, dah_t dah, size_t flags)
{
nh_t nh;
node_t *np;
nh = node_alloc();
if (nh == NH_NULL)
return NH_NULL;
np = Node_map(nh);
np->n_ino = ino;
np->n_nrh = nrh;
np->n_dah = dah;
np->n_hashh = NH_NULL;
np->n_parh = NH_NULL;
np->n_sibh = NH_NULL;
np->n_sibprevh = NH_NULL;
np->n_cldh = NH_NULL;
np->n_lnkh = NH_NULL;
np->n_gen = gen;
np->n_flags = (u_char_t)flags;
memset(np->n_pad, 0, sizeof(np->n_pad));
Node_unmap(nh, &np);
return nh;
}
static void
Node_free(nh_t *nhp)
{
node_t *np;
np = Node_map(*nhp);
np->n_ino = 0;
np->n_gen = 0;
if (np->n_nrh != NRH_NULL) {
namreg_del(np->n_nrh);
np->n_nrh = NRH_NULL;
}
if (np->n_dah != DAH_NULL) {
dirattr_del(np->n_dah);
np->n_dah = DAH_NULL;
}
np->n_flags = 0;
np->n_parh = NH_NULL;
np->n_sibh = NH_NULL;
np->n_sibprevh = NH_NULL;
np->n_cldh = NH_NULL;
np->n_lnkh = NH_NULL;
Node_unmap(*nhp, &np);
node_free(nhp);
}
/*
* NOTE: Does error handling here and exits.
*/
static node_t *
Node_map(nh_t nh)
{
node_t *n = (node_t *)node_map(nh);
if (n == NULL) {
mlog(MLOG_ERROR | MLOG_TREE, _(
"failed to map in node (node handle: %u)\n"), nh);
exit(EXIT_ERROR);
}
return n;
}
static void
Node_unmap(nh_t nh, node_t **npp)
{
node_unmap(nh, (void **)npp);
}
/* builds a pathname for the specified node, relative to root
* returns FALSE if pathname too long
*/
static bool_t
Node2path(nh_t nh, char *path, char *errmsg)
{
int remainingcnt;
strcpy(path, "."); /* in case root node passed in */
remainingcnt = Node2path_recurse(nh, path, MAXPATHLEN, 0);
if (remainingcnt <= 0) {
node_t *np = Node_map(nh);
xfs_ino_t ino = np->n_ino;
gen_t gen = np->n_gen;
Node_unmap(nh, &np);
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"unable %s ino %llu gen %u: "
"relative pathname too long (partial %s)\n"),
errmsg,
ino,
gen,
path);
return BOOL_FALSE;
} else {
return BOOL_TRUE;
}
}
/* returns how much of the buffer remains, assuming the buffer size is
* MAXPATHLEN. always null-terminates, but null char not counted in return.
* works because the buffer size is secretly 2 * MAXPATHLEN.
*/
static int
Node2path_recurse(nh_t nh, char *buf, int bufsz, int level)
{
static __thread path_cache_t cache = { NH_NULL, 0, "" };
node_t *np;
nh_t parh;
xfs_ino_t ino;
gen_t gen;
nrh_t nrh;
char *oldbuf;
int oldbufsz;
int namelen;
/* recursion termination
*/
if (nh == persp->p_rooth) {
return bufsz;
}
/* if we have a cache hit, no need to recurse any further
*/
if (nh == cache.nh) {
assert(bufsz > cache.len);
strcpy(buf, cache.buf);
return bufsz - cache.len;
}
/* extract useful node members
*/
np = Node_map(nh);
parh = np->n_parh;
ino = np->n_ino;
gen = np->n_gen;
nrh = np->n_nrh;
Node_unmap(nh, &np);
/* build path to parent
*/
oldbuf = buf;
oldbufsz = bufsz;
bufsz = Node2path_recurse(parh, buf, bufsz, level+1); /* RECURSION */
if (bufsz <= 0) {
return bufsz;
}
buf += oldbufsz - bufsz;
/* insert slash if parent not root
*/
if (parh != persp->p_rooth) {
assert(bufsz + MAXPATHLEN >= 2);
*buf++ = '/';
*(buf + 1) = 0;
bufsz--;
if (bufsz <= 0) {
return bufsz;
}
}
/* append entry name: special case if in orphanage
*/
if (parh == persp->p_orphh) {
namelen = sprintf(buf, "%llu.%u", (unsigned long long)ino, gen);
} else if (nh == persp->p_orphh) {
namelen = sprintf(buf, "%s", orphname);
} else {
assert(nrh != NRH_NULL);
namelen = namreg_get(nrh, buf, (size_t)bufsz + MAXPATHLEN);
assert(namelen > 0);
}
/* update remaining buffer size
*/
bufsz -= namelen;
assert(bufsz + MAXPATHLEN > 0);
/* update the cache if we're the target's parent
* (and the pathname is not too long)
*/
if (level == 1 && bufsz > 0) {
cache.nh = nh;
strcpy(cache.buf, oldbuf);
cache.len = oldbufsz - bufsz;
}
/* return remaining buffer size
*/
return bufsz;
}
/* family abstraction *********************************************************/
static void
adopt(nh_t parh, nh_t cldh, nrh_t nrh)
{
node_t *parp;
node_t *cldp;
node_t *sibp;
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"adopt(parent=%llu,child=%llu,node=%llu): \n",
parh, cldh, nrh);
#endif
/* fix up our child - put at front of child list */
cldp = Node_map(cldh);
cldp->n_parh = parh;
cldp->n_nrh = nrh;
parp = Node_map(parh);
cldp->n_sibh = parp->n_cldh;
cldp->n_sibprevh = NH_NULL;
Node_unmap(cldh, &cldp);
/* fix up old first child i.e. child's new sibling */
if (parp->n_cldh != NH_NULL) { /* if parent has a child */
sibp = Node_map(parp->n_cldh);
sibp->n_sibprevh = cldh;
Node_unmap(parp->n_cldh, &sibp);
}
/* fix up parent */
parp->n_cldh = cldh;
Node_unmap(parh, &parp);
}
static nrh_t
disown(nh_t cldh)
{
node_t *cldp;
nrh_t nrh;
nh_t parh;
node_t *parp;
cldp = Node_map(cldh);
nrh = cldp->n_nrh;
parh = cldp->n_parh;
assert(parh != NH_NULL);
if (parh == NH_NULL) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"attempt to disown child "
"which has no parent!\n"));
return nrh;
}
parp = Node_map(parh);
assert(parp->n_cldh != NH_NULL);
if (parp->n_cldh == NH_NULL) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"attempt to disown child "
"when parent has no children!\n"));
return nrh;
}
if (parp->n_cldh == cldh) {
/* child is the first one in the child list */
parp->n_cldh = cldp->n_sibh;
if (cldp->n_sibh != NH_NULL) {
node_t *sibp = Node_map(cldp->n_sibh);
sibp->n_sibprevh = NH_NULL;
Node_unmap(cldp->n_sibh, &sibp);
}
} else {
/* child is further down the child list */
/* use double link list to find previous link */
nh_t prevcldh = cldp->n_sibprevh;
node_t *prevcldp;
assert(prevcldh != NH_NULL); /* must be a previous */
prevcldp = Node_map(prevcldh);
/* fix up previous */
prevcldp->n_sibh = cldp->n_sibh;
Node_unmap(prevcldh, &prevcldp);
/* fix up next */
if (cldp->n_sibh != NH_NULL) {
node_t *sibp = Node_map(cldp->n_sibh);
sibp->n_sibprevh = prevcldh;
Node_unmap(cldp->n_sibh, &sibp);
}
}
Node_unmap(parh, &parp);
cldp->n_parh = NH_NULL;
cldp->n_sibh = NH_NULL;
cldp->n_sibprevh = NH_NULL;
Node_unmap(cldh, &cldp);
return nrh;
}
/* recursively marks all nodes in subtree as selected or not selected
* for subtree restoral. adjusts ancestors flags accordingly. also adjusts
* inomap, which will be used by content.c to see if a media file contains
* any nondirs which might need to be restored.
*/
static void
selsubtree(nh_t nh, bool_t sensepr)
{
node_t *np;
nh_t parh;
/* first mark the subtree
*/
selsubtree_recurse_down(nh, sensepr);
/* get parent
*/
np = Node_map(nh);
parh = np->n_parh;
Node_unmap(nh, &np);
/* next adjust ancestory
*/
while (parh != NH_NULL) {
node_t *parp;
nh_t newparh;
parp = Node_map(parh);
if (sensepr) {
parp->n_flags |= NF_SUBTREE;
} else {
bool_t atleastonechildselpr = BOOL_FALSE;
nh_t cldh = parp->n_cldh;
while (cldh != NH_NULL) {
node_t *cldp;
nh_t nextcldh;
bool_t cldsensepr;
cldp = Node_map(cldh);
cldsensepr = (cldp->n_flags & NF_SUBTREE)
?
BOOL_TRUE
:
BOOL_FALSE;
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
if (cldsensepr) {
atleastonechildselpr = BOOL_TRUE;
break;
}
cldh = nextcldh;
}
if (!atleastonechildselpr) {
parp->n_flags &= ~NF_SUBTREE;
/* DBG could break out here (remember to unmap!)
*/
}
}
newparh = parp->n_parh;
Node_unmap(parh, &parp);
parh = newparh;
}
}
static void
selsubtree_recurse_down(nh_t nh, bool_t sensepr)
{
nh_t cldh;
/* first mark the node indicated, and get head of cld list
*/
{
node_t *np;
bool_t isdirpr;
xfs_ino_t ino;
gen_t gen;
np = Node_map(nh);
if (sensepr) {
np->n_flags |= NF_SUBTREE;
} else {
np->n_flags &= ~NF_SUBTREE;
}
cldh = np->n_cldh;
ino = np->n_ino;
gen = np->n_gen;
isdirpr = (np->n_flags & NF_ISDIR);
Node_unmap(nh, &np);
if (!isdirpr) {
if (sensepr) {
inomap_rst_add(ino);
} else {
/* check hardlist: don't del unless none needed
*/
nh_t nh;
bool_t neededpr = BOOL_FALSE;
for (nh = link_hardh(ino, gen)
;
nh != NH_NULL
;
nh = link_nexth(nh)) {
node_t *np;
u_char_t flags;
np = Node_map(nh);
flags = np->n_flags;
Node_unmap(nh, &np);
if (flags & NF_SUBTREE) {
neededpr = BOOL_TRUE;
break;
}
}
if (!neededpr) {
inomap_rst_del(ino);
}
}
}
}
/* then mark all of its children. be sure to skip the orphanage!!!
*/
while (cldh != NH_NULL) {
node_t *cldp;
nh_t nextcldh;
if (cldh != persp->p_orphh) {
selsubtree_recurse_down(cldh, sensepr);
}
cldp = Node_map(cldh);
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
cldh = nextcldh;
}
}
/* link abstraction *********************************************************/
/* returns handle to head of hard link list
*/
static nh_t
link_hardh(xfs_ino_t ino, gen_t gen)
{
return hash_find(ino, gen);
}
/* returns following node in hard link list
*/
static nh_t
link_nexth(nh_t nh)
{
node_t *np;
nh_t nexth;
np = Node_map(nh);
nexth = np->n_lnkh;
Node_unmap(nh, &np);
return nexth;
}
/* searches hard link list for exact match.
* returns hard link list head
*/
static nh_t
link_matchh(nh_t hardh, nh_t parh, char *name)
{
while (hardh != NH_NULL) {
node_t *np;
nh_t nexth;
np = Node_map(hardh);
if (np->n_parh == parh) {
/* REFERENCED */
int namelen;
namelen = namreg_get(np->n_nrh,
tranp->t_namebuf,
sizeof(tranp->t_namebuf));
assert(namelen > 0);
if (!strcmp(name, tranp->t_namebuf)) {
Node_unmap(hardh, &np);
break;
}
}
nexth = np->n_lnkh;
Node_unmap(hardh, &np);
hardh = nexth;
}
return hardh;
}
static void
link_in(nh_t nh)
{
node_t *np;
xfs_ino_t ino;
gen_t gen;
nh_t hardh;
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"link_in(%llu): map in node\n", nh);
#endif
/* map in the node to read ino and gen
*/
np = Node_map(nh);
ino = np->n_ino;
gen = np->n_gen;
/* see if one or more links already hashed.
*/
hardh = hash_find(ino, gen);
/* if not hashed, just hash it. otherwise put at end
* of hard link (lnk) list.
*/
if (hardh == NH_NULL) {
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"link_in(): hash node in for ino %llu\n", ino);
#endif
hash_in(nh);
} else {
nh_t prevh = hardh;
node_t *prevp = Node_map(prevh);
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"link_in(): put at end of hash list\n");
#endif
while (prevp->n_lnkh != NH_NULL) {
nh_t nexth = prevp->n_lnkh;
Node_unmap(prevh, &prevp);
prevh = nexth;
prevp = Node_map(prevh);
}
prevp->n_lnkh = nh;
Node_unmap(prevh, &prevp);
}
/* since always put at end of hard link list, make node's
* lnk member terminate list.
*/
np->n_lnkh = NH_NULL;
Node_unmap(nh, &np);
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"link_in(%llu): UNmap in node\n", nh);
#endif
}
static void
link_out(nh_t nh)
{
node_t *np;
xfs_ino_t ino;
gen_t gen;
nh_t hardh;
/* map in the node to read ino and gen
*/
np = Node_map(nh);
ino = np->n_ino;
gen = np->n_gen;
/* get head of hard link list
*/
hardh = hash_find(ino, gen);
assert(hardh != NH_NULL);
/* if node is at head of hard link list, hash it out and
* hash in the following node in link list, if there is one.
* otherwise, unlink from hardlink list.
*/
if (nh == hardh) {
hash_out(nh);
if (np->n_lnkh != NH_NULL) {
hash_in(np->n_lnkh);
}
} else {
nh_t prevh = hardh;
node_t *prevp = Node_map(prevh);
while (prevp->n_lnkh != nh) {
nh_t nexth = prevp->n_lnkh;
Node_unmap(prevh, &prevp);
prevh = nexth;
assert(prevh != NH_NULL);
prevp = Node_map(prevh);
}
prevp->n_lnkh = np->n_lnkh;
Node_unmap(prevh, &prevp);
}
np->n_lnkh = NH_NULL;
/* release the mapping
*/
Node_unmap(nh, &np);
}
/* invokes callback for all hardheads
* iteration aborted if callback returns FALSE
*/
static void
link_headiter(bool_t (*cbfp)(void *contextp, nh_t hardh), void *contextp)
{
hash_iter(cbfp, contextp);
}
/* iterator for a hard link list. allows deletion of the last node
* returned.
*/
static void
link_iter_init(link_iter_context_t *link_iter_contextp, nh_t hardheadh)
{
link_iter_contextp->li_headh = hardheadh;
link_iter_contextp->li_prevh = NH_NULL;
link_iter_contextp->li_lasth = NH_NULL;
link_iter_contextp->li_donepr = BOOL_FALSE;
}
static nh_t
link_iter_next(link_iter_context_t *link_iter_contextp)
{
node_t *lastp;
nh_t tmplasth;
/* if already done, return
*/
if (link_iter_contextp->li_donepr == BOOL_TRUE) {
return NH_NULL;
}
/* if no hardhead, done
*/
if (link_iter_contextp->li_headh == NH_NULL) {
link_iter_contextp->li_donepr = BOOL_TRUE;
return NH_NULL;
}
/* make tmp copy of last
*/
tmplasth = link_iter_contextp->li_lasth;
/* if no last, must be first call
*/
if (tmplasth == NH_NULL) {
assert(link_iter_contextp->li_prevh == NH_NULL);
link_iter_contextp->li_lasth = link_iter_contextp->li_headh;
return link_iter_contextp->li_lasth;
}
/* slide last into prev
*/
link_iter_contextp->li_prevh = tmplasth;
lastp = Node_map(tmplasth);
link_iter_contextp->li_lasth = lastp->n_lnkh;
Node_unmap(tmplasth, &lastp);
/* if NULL, flag done
*/
if (link_iter_contextp->li_lasth == NH_NULL) {
link_iter_contextp->li_donepr = BOOL_TRUE;
}
/* return the last handle
*/
return link_iter_contextp->li_lasth;
}
/* ARGSUSED */
void
link_iter_unlink(link_iter_context_t *link_iter_contextp, nh_t nh)
{
node_t *lastp;
nh_t nexth;
/* sanity checks
*/
assert(link_iter_contextp->li_lasth != NH_NULL);
assert(nh == link_iter_contextp->li_lasth);
/* get the next node in list
*/
lastp = Node_map(link_iter_contextp->li_lasth);
nexth = lastp->n_lnkh;
lastp->n_lnkh = NH_NULL;
Node_unmap(link_iter_contextp->li_lasth, &lastp);
if (link_iter_contextp->li_lasth == link_iter_contextp->li_headh) {
assert(link_iter_contextp->li_prevh == NH_NULL);
hash_out(link_iter_contextp->li_headh);
link_iter_contextp->li_headh = nexth;
if (nexth != NH_NULL) {
hash_in(nexth);
}
} else {
node_t *prevp;
assert(link_iter_contextp->li_prevh != NH_NULL);
prevp = Node_map(link_iter_contextp->li_prevh);
prevp->n_lnkh = nexth;
Node_unmap(link_iter_contextp->li_prevh, &prevp);
}
link_iter_contextp->li_lasth = link_iter_contextp->li_prevh;
link_iter_contextp->li_prevh = NH_NULL;
}
/* hash abstraction *********************************************************/
#define HASHLEN_MIN (pgsz / sizeof(nh_t))
static bool_t
hash_init(size64_t vmsz,
size64_t dircnt,
size64_t nondircnt,
char *perspath)
{
size64_t hashlen;
size64_t loghashlen;
size64_t vmlen;
size64_t hashlenmax;
ix_t hix;
/* sanity checks
*/
assert(pgsz % sizeof(nh_t) == 0);
/* calculate the size of the hash array. must be a power of two,
* and a multiple of the page size. don't use more than the available
* vm. but enforce a minimum.
*/
vmlen = vmsz / sizeof(nh_t);
hashlenmax = min(vmlen, SIZEMAX);
hashlen = (dircnt + nondircnt);
hashlen = max(hashlen, (size64_t)HASHLEN_MIN);
hashlen = min(hashlen, hashlenmax);
for (loghashlen = 0
;
((size64_t)1 << loghashlen) <= hashlen
;
loghashlen++)
;
assert(loghashlen > 0);
hashlen = (size64_t)1 << loghashlen;
if (hashlen > hashlenmax)
hashlen >>= 1;
assert(hashlen <= hashlenmax);
/* record hash size in persistent state
*/
persp->p_hashsz = hashlen * sizeof(nh_t);
/* map the hash array just after the persistent state header
*/
assert(persp->p_hashsz <= SIZEMAX);
assert(!(persp->p_hashsz % (size64_t)pgsz));
assert(!(PERSSZ % pgsz));
tranp->t_hashp = (nh_t *) mmap_autogrow(
(size_t)persp->p_hashsz,
tranp->t_persfd,
(off64_t)PERSSZ);
if (tranp->t_hashp == (nh_t *)-1) {
mlog(MLOG_NORMAL | MLOG_TREE, _(
"unable to mmap hash array into %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
/* initialize the hash array to all NULL node handles
*/
for (hix = 0; hix < (ix_t)hashlen; hix++) {
tranp->t_hashp[hix] = NH_NULL;
}
/* build a hash mask. this works because hashlen is a power of two.
* record in persistent state.
*/
assert(hashlen - 1 <= SIZEMAX);
persp->p_hashmask = (size_t)(hashlen - 1);
return BOOL_TRUE;
}
static bool_t
hash_sync(char *perspath)
{
size64_t hashsz;
/* sanity checks
*/
assert(pgsz % sizeof(nh_t) == 0);
/* retrieve the hash size from the persistent state
*/
hashsz = persp->p_hashsz;
assert(!(hashsz % sizeof(nh_t)));
/* map the hash array just after the persistent state header
*/
assert(hashsz <= SIZEMAX);
assert(!(hashsz % (size64_t)pgsz));
assert(!(PERSSZ % pgsz));
tranp->t_hashp = (nh_t *) mmap_autogrow(
(size_t)hashsz,
tranp->t_persfd,
(off64_t)PERSSZ);
if (tranp->t_hashp == (nh_t *)-1) {
mlog(MLOG_NORMAL | MLOG_TREE, _(
"unable to mmap hash array into %s: %s\n"),
perspath,
strerror(errno));
return BOOL_FALSE;
}
return BOOL_TRUE;
}
static inline size_t
hash_val(xfs_ino_t ino, size_t hashmask)
{
ino += ~(ino << 15);
ino ^= (ino >> 10);
ino += (ino << 3);
ino ^= (ino >> 6);
ino += ~(ino << 11);
ino ^= (ino >> 16);
return (size_t)ino & hashmask;
}
static void
hash_in(nh_t nh)
{
node_t *np;
xfs_ino_t ino;
size_t hix;
nh_t *entryp;
/* get a mapping to the node
*/
np = Node_map(nh);
/* get ino from node
*/
ino = np->n_ino;
/* assert not already in
*/
assert(hash_find(np->n_ino, np->n_gen) == NH_NULL);
/* calculate the hash index
*/
hix = hash_val(ino, persp->p_hashmask);
/* get a pointer to the indexed hash array entry
*/
entryp = &tranp->t_hashp[hix];
/* insert into the list, at the head
*/
assert(np->n_hashh == NH_NULL);
np->n_hashh = *entryp;
*entryp = nh;
/* release the mapping
*/
Node_unmap(nh, &np);
}
static void
hash_out(nh_t nh)
{
node_t *np;
xfs_ino_t ino;
nh_t hashheadh;
size_t hix;
nh_t *entryp;
/* get a mapping to the node
*/
np = Node_map(nh);
/* get the ino
*/
ino = np->n_ino;
/* get a pointer to the hash array entry
*/
hix = hash_val(ino, persp->p_hashmask);
entryp = &tranp->t_hashp[hix];
/* get the handle of the first node in the appropriate hash array
*/
hashheadh = *entryp;
assert(hashheadh != NH_NULL);
/* if node is first in list, replace entry with following node.
* otherwise, walk the list until found.
*/
if (hashheadh == nh) {
*entryp = np->n_hashh;
} else {
nh_t prevh = hashheadh;
node_t *prevp = Node_map(prevh);
while (prevp->n_hashh != nh) {
nh_t nexth = prevp->n_hashh;
Node_unmap(prevh, &prevp);
prevh = nexth;
assert(prevh != NH_NULL);
prevp = Node_map(prevh);
}
prevp->n_hashh = np->n_hashh;
Node_unmap(prevh, &prevp);
}
np->n_hashh = NH_NULL;
/* release the mapping
*/
Node_unmap(nh, &np);
}
static nh_t
hash_find(xfs_ino_t ino, gen_t gen)
{
nh_t nh;
node_t *np;
size_t hix;
/* get handle to first node in appropriate hash array
*/
hix = hash_val(ino, persp->p_hashmask);
nh = tranp->t_hashp[hix];
/* if list empty, return null handle
*/
if (nh == NH_NULL) {
return NH_NULL;
}
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"hash_find(%llu,%u): traversing hash link list\n",
ino, gen);
#endif
/* walk the list until found.
*/
np = Node_map(nh);
while (np->n_ino != ino || np->n_gen != gen) {
nh_t nextnh = np->n_hashh;
Node_unmap(nh, &np);
nh = nextnh;
if (nh == NH_NULL) {
return NH_NULL;
}
np = Node_map(nh);
}
Node_unmap(nh, &np);
#ifdef TREE_DEBUG
mlog(MLOG_DEBUG | MLOG_TREE,
"hash_find(): return nh = %llu\n", nh);
#endif
return nh;
}
/* invokes callback for all hashed nodes
* iteration aborted if callback returns FALSE
* call back may hash out and free the node, so
* must figure next node prior to calling callback.
*/
static void
hash_iter(bool_t (*cbfp)(void *contextp, nh_t hashh), void *contextp)
{
ix_t hix;
size64_t hashlen = persp->p_hashsz / sizeof(nh_t);
for (hix = 0; hix < (ix_t)hashlen; hix++) {
nh_t nh = tranp->t_hashp[hix];
while (nh != NH_NULL) {
node_t *np;
nh_t nexth;
bool_t ok;
np = Node_map(nh);
nexth = np->n_hashh;
Node_unmap(nh, &np);
ok = (*cbfp)(contextp, nh);
if (!ok) {
return;
}
nh = nexth;
}
}
}
/* misc static functions *****************************************************/
#ifdef TREE_CHK
/* use hash array to iterate through all nodes. check
* each node's hash, hardlink, namreg, dirattr, parent,
* and sibling handles.
*/
static bool_t
Node_chk(nh_t nh, nh_t *nexthashhp, nh_t *nextlnkhp)
{
node_t *np;
node_t n;
char nambuf[NAME_MAX + 1];
bool_t okaccum;
mlog(MLOG_NITTY + 1 | MLOG_TREE,
"checking node nh == 0x%x\n",
nh);
okaccum = BOOL_TRUE;
if (nexthashhp) {
*nexthashhp = NH_NULL;
}
assert(nextlnkhp);
*nextlnkhp = NH_NULL;
np = Node_map(nh);
assert(np);
n = *np;
Node_unmap(nh, &np);
if (!nexthashhp && n.n_hashh != NH_NULL) {
mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_TREE, _(
"nh 0x%x np 0x%x hash link not null\n"),
nh,
np);
okaccum = BOOL_FALSE;
}
if (n.n_hashh != NH_NULL) {
np = Node_map(n.n_hashh);
Node_unmap(n.n_hashh, &np);
}
if (n.n_lnkh != NH_NULL) {
np = Node_map(n.n_lnkh);
Node_unmap(n.n_lnkh, &np);
}
if (n.n_parh != NH_NULL) {
np = Node_map(n.n_parh);
Node_unmap(n.n_parh, &np);
}
if (n.n_cldh != NH_NULL) {
np = Node_map(n.n_cldh);
Node_unmap(n.n_cldh, &np);
}
if (n.n_sibh != NH_NULL) {
np = Node_map(n.n_sibh);
Node_unmap(n.n_sibh, &np);
}
if (n.n_nrh != NRH_NULL) {
int rval;
rval = namreg_get(n.n_nrh, nambuf, sizeof(nambuf));
assert(rval >= 0);
}
if (n.n_dah != DAH_NULL) {
(void)dirattr_get_mode(n.n_dah);
}
if (nexthashhp) {
*nexthashhp = n.n_hashh;
}
*nextlnkhp = n.n_lnkh;
return okaccum;
}
bool_t
tree_chk(void)
{
ix_t hix;
size64_t hashlen = persp->p_hashsz / sizeof(nh_t);
bool_t ok;
bool_t okaccum;
okaccum = BOOL_TRUE;
for (hix = 0; hix < (ix_t)hashlen; hix++) {
nh_t hashh = tranp->t_hashp[hix];
mlog(MLOG_NITTY + 1 | MLOG_TREE,
"checking hix %u\n",
hix);
while (hashh != NH_NULL) {
nh_t lnkh;
ok = Node_chk(hashh, &hashh, &lnkh);
if (!ok) {
okaccum = BOOL_FALSE;
}
while (lnkh != NH_NULL) {
ok = Node_chk(lnkh, 0, &lnkh);
if (!ok) {
okaccum = BOOL_FALSE;
}
}
}
}
ok = tree_chk2();
if (!ok) {
okaccum = BOOL_FALSE;
}
return okaccum;
}
static bool_t tree_chk2_recurse(nh_t cldh, nh_t parh);
static bool_t
tree_chk2(void)
{
node_t *rootp;
nh_t cldh;
bool_t ok;
mlog(MLOG_DEBUG | MLOG_TREE,
"tree hierarchy check\n");
rootp = Node_map(persp->p_rooth);
cldh = rootp->n_cldh;
Node_unmap(persp->p_rooth, &rootp);
ok = tree_chk2_recurse(cldh, persp->p_rooth);
return ok;
}
static bool_t
tree_chk2_recurse(nh_t cldh, nh_t parh)
{
bool_t okaccum = BOOL_TRUE;
assert(parh != NH_NULL);
while (cldh != NH_NULL) {
node_t *cldp;
xfs_ino_t ino;
gen_t gen;
nh_t nodeparh;
nrh_t nrh;
nh_t grandcldh;
nh_t nextcldh;
bool_t ok;
cldp = Node_map(cldh);
ino = cldp->n_ino;
gen = cldp->n_gen;
nodeparh = cldp->n_parh;
nrh = cldp->n_nrh;
grandcldh = cldp->n_cldh;
nextcldh = cldp->n_sibh;
Node_unmap(cldh, &cldp);
if (parh == persp->p_orphh) {
sprintf(tranp->t_namebuf, "%llu.%u", ino, gen);
} else if (cldh == persp->p_orphh) {
sprintf(tranp->t_namebuf, "%llu.%u", ino, gen);
} else {
int namelen;
namelen = namreg_get(nrh,
tranp->t_namebuf,
sizeof(tranp->t_namebuf));
assert(namelen >= 0);
}
if (nodeparh == NH_NULL) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"node %x %s %llu %u parent NULL\n"),
cldh,
tranp->t_namebuf,
ino,
gen);
return BOOL_FALSE;
} else if (nodeparh != parh) {
mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_TREE, _(
"node %x %s %llu %u parent mismatch: "
"nodepar %x par %x\n"),
cldh,
tranp->t_namebuf,
ino,
gen,
nodeparh,
parh);
return BOOL_FALSE;
} else {
mlog(MLOG_DEBUG | MLOG_TREE,
"node %x %s %llu %u parent %x\n",
cldh,
tranp->t_namebuf,
ino,
gen,
parh);
}
ok = tree_chk2_recurse(grandcldh, cldh);
if (!ok) {
okaccum = BOOL_FALSE;
}
cldh = nextcldh;
}
return okaccum;
}
#endif /* TREE_CHK */
static char *whites = " \t\r\n\v\f";
static int is_white(char c);
static void fix_escape(char *string, char *liter);
static void fix_quoted_span(char *string, char *liter);
static void collapse_white(char *string, char *liter);
static size_t distance_to_space(char *s, char *l);
static int
parse(int slotcnt, char **slotbuf, char *string)
{
char *liter;
char *s;
char *l;
size_t wordcnt;
/* sanity checkcs
*/
assert(slotcnt >= 0);
/* allocate a companion to the input string for identifying
* characters which are to be interpreted literally.
*/
liter = (char *)calloc(1, strlen(string) + 1);
if (!liter) {
return -1;
}
/* pass 1: collapse escape sequences, identifying characters which
* are to be interpreted literally
*/
for (s = string, l = liter; *s; s++, l++) {
if (*s == '\\' && !*l) {
fix_escape(s, l);
}
}
/* pass 2: collapse quoted spans, identifying characters which
* are to be interpreted literally
*/
for (s = string, l = liter; *s; s++, l++) {
if (*s == '\"' && ! *l) {
fix_quoted_span(s, l);
}
}
/* pass 3: collapse white space spans into a single space
*/
for (s = string, l = liter; *s; s++, l++) {
if (is_white(*s) && !*l) {
collapse_white(s, l);
}
}
/* pass 4: identify and null-terminate words
*/
wordcnt = 0;
s = string;
l = liter;
while (*s) {
size_t d;
if (wordcnt < (size_t)slotcnt) {
slotbuf[wordcnt] = s;
}
wordcnt++;
d = distance_to_space(s, l);
s += d;
l += d;
if (*s) {
*s = 0;
s++;
l++;
}
}
free((void *)liter);
return (int)wordcnt;
}
struct escape_table {
char sequence;
char substitute;
};
typedef struct escape_table escape_table_t;
static escape_table_t escape_table[] = {
{ 'n', '\n' },
{ 't', '\t' },
{ 'v', '\v' },
{ 'b', '\b' },
{ 'r', '\r' },
{ 'f', '\f' },
{ 'f', '\f' },
{ 'a', '\a' },
{ '\\', '\\' }
};
static void shrink(char *s, size_t cnt);
static int is_hex(char c);
static size_t hex_to_size(char c);
static int is_octal(char c);
static size_t octal_to_size(char c);
static void
fix_escape(char *string, char *liter)
{
escape_table_t *ep;
escape_table_t *endep;
/* first look for special escapes described in table
*/
ep = escape_table;
endep = escape_table + (sizeof(escape_table)
/
sizeof(escape_table[0]));
for (; ep < endep; ep++) {
if (string[1] == ep->sequence) {
string[0] = ep->substitute;
liter[0] = (char)1;
shrink(&string[1], 1);
shrink(&liter[1], 1);
return;
}
}
/* detect white space escapes
*/
if (is_white(string[1])) {
liter[0] = (char)1;
shrink(&string[0], 1);
shrink(&liter[1], 1);
return;
}
/* detect hex escapes (don't allow null)
*/
if (string[1] == 'x') {
size_t hexlen;
size_t accum;
accum = 0;
for (hexlen = 0
;
hexlen < 2 && is_hex(string[2 + hexlen])
;
hexlen++) {
accum *= 16;
accum += hex_to_size(string[2 + hexlen]);
}
if (hexlen && accum) {
string[0] = (char)accum;
liter[0] = (char)1;
shrink(&string[1], 1 + hexlen);
shrink(&liter[1], 1 + hexlen);
return;
}
}
/* detect octal escapes (don't allow null)
*/
if (is_octal(string[1])) {
size_t octallen;
size_t accum;
accum = octal_to_size(string[1]);
for (octallen = 1
;
octallen < 3 && is_octal(string[1 + octallen])
;
octallen++) {
accum *= 8;
accum += octal_to_size(string[1 + octallen]);
}
if (accum) {
string[0] = (char)accum;
liter[0] = (char)1;
shrink(&string[1], octallen);
shrink(&liter[1], octallen);
return;
}
}
/* didn't match any escape sequences, so assume literal
*/
liter[0] = (char)1;
}
static void
fix_quoted_span(char *string, char *liter)
{
char *s;
char *l;
/* first cover the leading quote
*/
shrink(string, 1);
shrink(liter, 1);
/* scan for the next non-literal quote, marking all
* characters in between as literal
*/
for (s = string, l = liter; *s && (*s != '\"' || *l); s++, l++) {
*l = (char)1;
}
if (*s) {
shrink(s, 1);
shrink(l, 1);
}
}
static void
collapse_white(char *string, char *liter)
{
char *s;
char *l;
size_t cnt;
cnt = 0;
for (s = string, l = liter; is_white(*s) && !*l; s++, l++) {
cnt++;
}
string[0] = ' ';
if (cnt > 1) {
shrink(&string[1], cnt - 1);
shrink(&liter[1], cnt - 1);
}
}
static size_t
distance_to_space(char *s, char *l)
{
size_t cnt;
for (cnt = 0; *s && (!is_white(*s) || *l); s++, l++) {
cnt++;
}
return cnt;
}
static void
shrink(char *s, size_t cnt)
{
strcpy(s, s + cnt);
}
static int
is_white(char c)
{
if (c && strchr(whites, (int)c)) {
return 1;
} else {
return 0;
}
}
static int
is_hex(char c)
{
if (c >= '0' && c <= '9') {
return 1;
}
if (c >= 'a' && c <= 'f') {
return 1;
}
if (c >= 'A' && c <= 'F') {
return 1;
}
return 0;
}
static size_t
hex_to_size(char c)
{
if (c >= '0' && c <= '9') {
return (size_t)(c - '0');
}
if (c >= 'a' && c <= 'f') {
return (size_t)(c - 'a') + (size_t)0xa;
}
if (c >= 'A' && c <= 'F') {
return (size_t)(c - 'A') + (size_t)0xa;
}
return 0;
}
static int
is_octal(char c)
{
if (c >= '0' && c <= '7') {
return 1;
}
return 0;
}
static size_t
octal_to_size(char c)
{
if (c >= '0' && c <= '7') {
return (size_t)(c - '0');
}
return 0;
}
static int
mkdir_r(char *path)
{
struct stat sbuf;
if (stat(path, &sbuf) < 0) {
if (mkdir_r(dirname(strdup(path))) < 0)
return -1;
return mkdir(path, 0755);
}
else if ((sbuf.st_mode & S_IFDIR) == 0) {
mlog(MLOG_TRACE | MLOG_ERROR | MLOG_TREE, _(
"%s is not a directory\n"),
path);
mlog_exit(EXIT_ERROR, RV_EXISTS);
exit(1);
}
return 0;
}
|