sys/vfs/hammer2/hammer2_vfsops.c
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 | /* * Copyright (c) 2011-2023 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/nlookup.h> #include <sys/vnode.h> #include <sys/mount.h> #include <sys/fcntl.h> #include <sys/vfsops.h> #include <sys/sysctl.h> #include <sys/socket.h> #include <sys/objcache.h> #include <sys/proc.h> #include <sys/lock.h> #include <sys/file.h> #include "hammer2.h" TAILQ_HEAD(hammer2_mntlist, hammer2_dev); static struct hammer2_mntlist hammer2_mntlist; struct hammer2_pfslist hammer2_pfslist; struct hammer2_pfslist hammer2_spmplist; struct lock hammer2_mntlk; int hammer2_supported_version = HAMMER2_VOL_VERSION_DEFAULT; int hammer2_debug; int hammer2_aux_flags; int hammer2_xop_nthreads; int hammer2_xop_sgroups; int hammer2_xop_xgroups; int hammer2_xop_xbase; int hammer2_xop_mod; long hammer2_debug_inode; int hammer2_cluster_meta_read = 1; /* physical read-ahead */ int hammer2_cluster_data_read = 4; /* physical read-ahead */ int hammer2_cluster_write = 0; /* physical write clustering */ int hammer2_dedup_enable = 1; int hammer2_always_compress = 0; /* always try to compress */ int hammer2_flush_pipe = 100; int hammer2_dio_count; int hammer2_dio_limit = 256; int hammer2_bulkfree_tps = 5000; int hammer2_spread_workers; int hammer2_limit_saved_depth; long hammer2_chain_allocs; long hammer2_limit_saved_chains; long hammer2_limit_dirty_chains; long hammer2_limit_dirty_inodes; long hammer2_count_modified_chains; long hammer2_iod_file_read; long hammer2_iod_meta_read; long hammer2_iod_indr_read; long hammer2_iod_fmap_read; long hammer2_iod_volu_read; long hammer2_iod_file_write; long hammer2_iod_file_wembed; long hammer2_iod_file_wzero; long hammer2_iod_file_wdedup; long hammer2_iod_meta_write; long hammer2_iod_indr_write; long hammer2_iod_fmap_write; long hammer2_iod_volu_write; static long hammer2_iod_inode_creates; static long hammer2_iod_inode_deletes; long hammer2_process_icrc32; long hammer2_process_xxhash64; MALLOC_DECLARE(M_HAMMER2_CBUFFER); MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer", "Buffer used for compression."); MALLOC_DECLARE(M_HAMMER2_DEBUFFER); MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer", "Buffer used for decompression."); SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem"); SYSCTL_INT(_vfs_hammer2, OID_AUTO, supported_version, CTLFLAG_RD, &hammer2_supported_version, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, aux_flags, CTLFLAG_RW, &hammer2_aux_flags, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW, &hammer2_debug, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, debug_inode, CTLFLAG_RW, &hammer2_debug_inode, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, spread_workers, CTLFLAG_RW, &hammer2_spread_workers, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_meta_read, CTLFLAG_RW, &hammer2_cluster_meta_read, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_data_read, CTLFLAG_RW, &hammer2_cluster_data_read, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_write, CTLFLAG_RW, &hammer2_cluster_write, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, dedup_enable, CTLFLAG_RW, &hammer2_dedup_enable, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, always_compress, CTLFLAG_RW, &hammer2_always_compress, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW, &hammer2_flush_pipe, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, bulkfree_tps, CTLFLAG_RW, &hammer2_bulkfree_tps, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_allocs, CTLFLAG_RD, &hammer2_chain_allocs, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_saved_chains, CTLFLAG_RW, &hammer2_limit_saved_chains, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, limit_saved_depth, CTLFLAG_RW, &hammer2_limit_saved_depth, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW, &hammer2_limit_dirty_chains, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_inodes, CTLFLAG_RW, &hammer2_limit_dirty_inodes, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, count_modified_chains, CTLFLAG_RD, &hammer2_count_modified_chains, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD, &hammer2_dio_count, 0, ""); SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_limit, CTLFLAG_RW, &hammer2_dio_limit, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RD, &hammer2_iod_file_read, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RD, &hammer2_iod_meta_read, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RD, &hammer2_iod_indr_read, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RD, &hammer2_iod_fmap_read, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RD, &hammer2_iod_volu_read, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RD, &hammer2_iod_file_write, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RD, &hammer2_iod_file_wembed, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RD, &hammer2_iod_file_wzero, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RD, &hammer2_iod_file_wdedup, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RD, &hammer2_iod_meta_write, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RD, &hammer2_iod_indr_write, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RD, &hammer2_iod_fmap_write, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RD, &hammer2_iod_volu_write, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_creates, CTLFLAG_RD, &hammer2_iod_inode_creates, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_deletes, CTLFLAG_RD, &hammer2_iod_inode_deletes, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_icrc32, CTLFLAG_RD, &hammer2_process_icrc32, 0, ""); SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_xxhash64, CTLFLAG_RD, &hammer2_process_xxhash64, 0, ""); static int hammer2_vfs_init(struct vfsconf *conf); static int hammer2_vfs_uninit(struct vfsconf *vfsp); static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred); static int hammer2_remount(hammer2_dev_t *, struct mount *, char *, struct ucred *); static int hammer2_recovery(hammer2_dev_t *hmp); static int hammer2_vfs_unmount(struct mount *mp, int mntflags); static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp); static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred); static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred); static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp); static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp); static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp, struct ucred **credanonp); static int hammer2_vfs_modifying(struct mount *mp); static void hammer2_update_pmps(hammer2_dev_t *hmp); static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp); static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp); static int hammer2_fixup_pfses(hammer2_dev_t *hmp); /* * HAMMER2 vfs operations. */ static struct vfsops hammer2_vfsops = { .vfs_flags = 0, .vfs_init = hammer2_vfs_init, .vfs_uninit = hammer2_vfs_uninit, .vfs_sync = hammer2_vfs_sync, .vfs_mount = hammer2_vfs_mount, .vfs_unmount = hammer2_vfs_unmount, .vfs_root = hammer2_vfs_root, .vfs_statfs = hammer2_vfs_statfs, .vfs_statvfs = hammer2_vfs_statvfs, .vfs_vget = hammer2_vfs_vget, .vfs_vptofh = hammer2_vfs_vptofh, .vfs_fhtovp = hammer2_vfs_fhtovp, .vfs_checkexp = hammer2_vfs_checkexp, .vfs_modifying = hammer2_vfs_modifying }; MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", ""); VFS_SET(hammer2_vfsops, hammer2, VFCF_MPSAFE); MODULE_VERSION(hammer2, 1); static int hammer2_vfs_init(struct vfsconf *conf) { static struct objcache_malloc_args margs_read; static struct objcache_malloc_args margs_write; static struct objcache_malloc_args margs_vop; int error; int mod; error = 0; kmalloc_raise_limit(M_HAMMER2, 0); /* unlimited */ /* * hammer2_xop_nthreads must be a multiple of ncpus, * minimum 2 * ncpus. */ mod = ncpus; hammer2_xop_mod = mod; hammer2_xop_nthreads = mod * 2; while (hammer2_xop_nthreads / mod < HAMMER2_XOPGROUPS_MIN || hammer2_xop_nthreads < HAMMER2_XOPTHREADS_MIN) { hammer2_xop_nthreads += mod; } hammer2_xop_sgroups = hammer2_xop_nthreads / mod / 2; hammer2_xop_xgroups = hammer2_xop_nthreads / mod - hammer2_xop_sgroups; hammer2_xop_xbase = hammer2_xop_sgroups * mod; /* * A large DIO cache is needed to retain dedup enablement masks. * The bulkfree code clears related masks as part of the disk block * recycling algorithm, preventing it from being used for a later * dedup. * * NOTE: A large buffer cache can actually interfere with dedup * operation because we dedup based on media physical buffers * and not logical buffers. Try to make the DIO case large * enough to avoid this problem, but also cap it. */ hammer2_dio_limit = nbuf * 2; if (hammer2_dio_limit > 100000) hammer2_dio_limit = 100000; if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref)) error = EINVAL; if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data)) error = EINVAL; if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data)) error = EINVAL; if (error) kprintf("HAMMER2 structure size mismatch; cannot continue.\n"); margs_read.objsize = 65536; margs_read.mtype = M_HAMMER2_DEBUFFER; margs_write.objsize = 32768; margs_write.mtype = M_HAMMER2_CBUFFER; margs_vop.objsize = sizeof(hammer2_xop_t); margs_vop.mtype = M_HAMMER2; /* * Note thaht for the XOPS cache we want backing store allocations * to use M_ZERO. This is not allowed in objcache_get() (to avoid * confusion), so use the backing store function that does it. This * means that initial XOPS objects are zerod but REUSED objects are * not. So we are responsible for cleaning the object up sufficiently * for our needs before objcache_put()ing it back (typically just the * FIFO indices). */ cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc, 0, 1, NULL, NULL, NULL, objcache_malloc_alloc, objcache_malloc_free, &margs_read); cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc, 0, 1, NULL, NULL, NULL, objcache_malloc_alloc, objcache_malloc_free, &margs_write); cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc, 0, 1, NULL, NULL, NULL, objcache_malloc_alloc_zero, objcache_malloc_free, &margs_vop); lockinit(&hammer2_mntlk, "mntlk", 0, 0); TAILQ_INIT(&hammer2_mntlist); TAILQ_INIT(&hammer2_pfslist); TAILQ_INIT(&hammer2_spmplist); hammer2_limit_dirty_chains = maxvnodes / 10; if (hammer2_limit_dirty_chains > HAMMER2_LIMIT_DIRTY_CHAINS) hammer2_limit_dirty_chains = HAMMER2_LIMIT_DIRTY_CHAINS; if (hammer2_limit_dirty_chains < 1000) hammer2_limit_dirty_chains = 1000; hammer2_limit_dirty_inodes = maxvnodes / 25; if (hammer2_limit_dirty_inodes < 100) hammer2_limit_dirty_inodes = 100; if (hammer2_limit_dirty_inodes > HAMMER2_LIMIT_DIRTY_INODES) hammer2_limit_dirty_inodes = HAMMER2_LIMIT_DIRTY_INODES; hammer2_limit_saved_chains = hammer2_limit_dirty_chains * 5; return (error); } static int hammer2_vfs_uninit(struct vfsconf *vfsp __unused) { objcache_destroy(cache_buffer_read); objcache_destroy(cache_buffer_write); objcache_destroy(cache_xops); return 0; } /* * Core PFS allocator. Used to allocate or reference the pmp structure * for PFS cluster mounts and the spmp structure for media (hmp) structures. * The pmp can be passed in or loaded by this function using the chain and * inode data. * * pmp->modify_tid tracks new modify_tid transaction ids for front-end * transactions. Note that synchronization does not use this field. * (typically frontend operations and synchronization cannot run on the * same PFS node at the same time). * * XXX check locking */ hammer2_pfs_t * hammer2_pfsalloc(hammer2_chain_t *chain, const hammer2_inode_data_t *ripdata, hammer2_dev_t *force_local) { hammer2_pfs_t *pmp; hammer2_inode_t *iroot; int count; int i; int j; pmp = NULL; /* * Locate or create the PFS based on the cluster id. If ripdata * is NULL this is a spmp which is unique and is always allocated. * * If the device is mounted in local mode all PFSs are considered * independent and not part of any cluster (for debugging only). */ if (ripdata) { TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { if (force_local != pmp->force_local) continue; if (force_local == NULL && bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid, sizeof(pmp->pfs_clid)) == 0) { break; } else if (force_local && pmp->pfs_names[0] && strcmp(pmp->pfs_names[0], (const char *)ripdata->filename) == 0) { break; } } } if (pmp == NULL) { pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO); pmp->force_local = force_local; hammer2_trans_manage_init(pmp); kmalloc_create_obj(&pmp->minode, "HAMMER2-inodes", sizeof(struct hammer2_inode)); lockinit(&pmp->lock, "pfslk", 0, 0); hammer2_spin_init(&pmp->blockset_spin, "h2blkset"); hammer2_inum_hash_init(pmp); hammer2_spin_init(&pmp->xop_spin, "h2xop"); TAILQ_INIT(&pmp->syncq); TAILQ_INIT(&pmp->depq); hammer2_spin_init(&pmp->list_spin, "h2pfsalloc_list"); /* * Save the last media transaction id for the flusher. Set * initial */ if (ripdata) { pmp->pfs_clid = ripdata->meta.pfs_clid; TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry); } else { pmp->flags |= HAMMER2_PMPF_SPMP; TAILQ_INSERT_TAIL(&hammer2_spmplist, pmp, mntentry); } /* * The synchronization thread may start too early, make * sure it stays frozen until we are ready to let it go. * XXX */ /* pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN | HAMMER2_THREAD_REMASTER; */ } /* * Create the PFS's root inode and any missing XOP helper threads. */ if ((iroot = pmp->iroot) == NULL) { iroot = hammer2_inode_get(pmp, NULL, 1, -1); if (ripdata) iroot->meta = ripdata->meta; pmp->iroot = iroot; hammer2_inode_ref(iroot); hammer2_inode_unlock(iroot); } /* * Stop here if no chain is passed in. */ if (chain == NULL) goto done; /* * When a chain is passed in we must add it to the PFS's root * inode, update pmp->pfs_types[], and update the syncronization * threads. * * When forcing local mode, mark the PFS as a MASTER regardless. * * At the moment empty spots can develop due to removals or failures. * Ultimately we want to re-fill these spots but doing so might * confused running code. XXX */ hammer2_inode_ref(iroot); hammer2_mtx_ex(&iroot->lock); j = iroot->cluster.nchains; if (j == HAMMER2_MAXCLUSTER) { kprintf("hammer2_pfsalloc: cluster full!\n"); /* XXX fatal error? */ } else { KKASSERT(chain->pmp == NULL); chain->pmp = pmp; hammer2_chain_ref(chain); iroot->cluster.array[j].chain = chain; if (force_local) pmp->pfs_types[j] = HAMMER2_PFSTYPE_MASTER; else pmp->pfs_types[j] = ripdata->meta.pfs_type; pmp->pfs_names[j] = kstrdup((const char *)ripdata->filename, M_HAMMER2); pmp->pfs_hmps[j] = chain->hmp; hammer2_spin_ex(&pmp->blockset_spin); pmp->pfs_iroot_blocksets[j] = chain->data->ipdata.u.blockset; hammer2_spin_unex(&pmp->blockset_spin); /* * If the PFS is already mounted we must account * for the mount_count here. */ if (pmp->mp) ++chain->hmp->mount_count; /* * May have to fixup dirty chain tracking. Previous * pmp was NULL so nothing to undo. */ if (chain->flags & HAMMER2_CHAIN_MODIFIED) hammer2_pfs_memory_inc(pmp); ++j; } iroot->cluster.nchains = j; /* * Update nmasters from any PFS inode which is part of the cluster. * It is possible that this will result in a value which is too * high. MASTER PFSs are authoritative for pfs_nmasters and will * override this value later on. * * (This informs us of masters that might not currently be * discoverable by this mount). */ if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) { pmp->pfs_nmasters = ripdata->meta.pfs_nmasters; } /* * Count visible masters. Masters are usually added with * ripdata->meta.pfs_nmasters set to 1. This detects when there * are more (XXX and must update the master inodes). */ count = 0; for (i = 0; i < iroot->cluster.nchains; ++i) { if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) ++count; } if (pmp->pfs_nmasters < count) pmp->pfs_nmasters = count; /* * Create missing synchronization and support threads. * * Single-node masters (including snapshots) have nothing to * synchronize and do not require this thread. * * Multi-node masters or any number of soft masters, slaves, copy, * or other PFS types need the thread. * * Each thread is responsible for its particular cluster index. * We use independent threads so stalls or mismatches related to * any given target do not affect other targets. */ for (i = 0; i < iroot->cluster.nchains; ++i) { /* * Single-node masters (including snapshots) have nothing * to synchronize and will make direct xops support calls, * thus they do not require this thread. * * Note that there can be thousands of snapshots. We do not * want to create thousands of threads. */ if (pmp->pfs_nmasters <= 1 && pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) { continue; } /* * Sync support thread */ if (pmp->sync_thrs[i].td == NULL) { hammer2_thr_create(&pmp->sync_thrs[i], pmp, NULL, "h2nod", i, -1, hammer2_primary_sync_thread); } } /* * Create missing Xop threads * * NOTE: We create helper threads for all mounted PFSs or any * PFSs with 2+ nodes (so the sync thread can update them, * even if not mounted). */ if (pmp->mp || iroot->cluster.nchains >= 2) hammer2_xop_helper_create(pmp); hammer2_mtx_unlock(&iroot->lock); hammer2_inode_drop(iroot); done: return pmp; } /* * Deallocate an element of a probed PFS. If destroying and this is a * MASTER, adjust nmasters. * * This function does not physically destroy the PFS element in its device * under the super-root (see hammer2_ioctl_pfs_delete()). */ void hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying) { hammer2_inode_t *iroot; hammer2_chain_t *chain; int j; /* * Cleanup our reference on iroot. iroot is (should) not be needed * by the flush code. */ iroot = pmp->iroot; if (iroot) { /* * Stop synchronizing * * XXX flush after acquiring the iroot lock. * XXX clean out the cluster index from all inode structures. */ hammer2_thr_delete(&pmp->sync_thrs[clindex]); /* * Remove the cluster index from the group. If destroying * the PFS and this is a master, adjust pfs_nmasters. */ hammer2_mtx_ex(&iroot->lock); chain = iroot->cluster.array[clindex].chain; iroot->cluster.array[clindex].chain = NULL; switch(pmp->pfs_types[clindex]) { case HAMMER2_PFSTYPE_MASTER: if (destroying && pmp->pfs_nmasters > 0) --pmp->pfs_nmasters; /* XXX adjust ripdata->meta.pfs_nmasters */ break; default: break; } pmp->pfs_types[clindex] = HAMMER2_PFSTYPE_NONE; hammer2_mtx_unlock(&iroot->lock); /* * Release the chain. */ if (chain) { atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); hammer2_chain_drop(chain); } /* * Terminate all XOP threads for the cluster index. */ if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) { hammer2_thr_delete( &pmp->xop_groups[j].thrs[clindex]); } } } } /* * Destroy a PFS, typically only occurs after the last mount on a device * has gone away. */ static void hammer2_pfsfree(hammer2_pfs_t *pmp) { hammer2_inode_t *iroot; hammer2_chain_t *chain; int chains_still_present = 0; int i; int j; /* * Cleanup our reference on iroot. iroot is (should) not be needed * by the flush code. */ if (pmp->flags & HAMMER2_PMPF_SPMP) TAILQ_REMOVE(&hammer2_spmplist, pmp, mntentry); else TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry); /* * Clean up iroot */ iroot = pmp->iroot; if (iroot) { for (i = 0; i < iroot->cluster.nchains; ++i) { hammer2_thr_delete(&pmp->sync_thrs[i]); if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) hammer2_thr_delete( &pmp->xop_groups[j].thrs[i]); } chain = iroot->cluster.array[i].chain; if (chain && !RB_EMPTY(&chain->core.rbtree)) { kprintf("hammer2: Warning pmp %p still " "has active chains\n", pmp); chains_still_present = 1; } } KASSERT(iroot->refs == 1, ("PMP->IROOT %p REFS WRONG %d", iroot, iroot->refs)); /* ref for iroot */ hammer2_inode_drop(iroot); pmp->iroot = NULL; } /* * Free remaining pmp resources */ if (chains_still_present) { kprintf("hammer2: cannot free pmp %p, still in use\n", pmp); } else { kmalloc_destroy_obj(&pmp->minode); kfree(pmp, M_HAMMER2); } } /* * Remove all references to hmp from the pfs list. Any PFS which becomes * empty is terminated and freed. * * XXX inefficient. */ static void hammer2_pfsfree_scan(hammer2_dev_t *hmp, int which) { hammer2_pfs_t *pmp; hammer2_inode_t *iroot; hammer2_chain_t *rchain; int i; int j; struct hammer2_pfslist *wlist; if (which == 0) wlist = &hammer2_pfslist; else wlist = &hammer2_spmplist; again: TAILQ_FOREACH(pmp, wlist, mntentry) { if ((iroot = pmp->iroot) == NULL) continue; /* * Determine if this PFS is affected. If it is we must * freeze all management threads and lock its iroot. * * Freezing a management thread forces it idle, operations * in-progress will be aborted and it will have to start * over again when unfrozen, or exit if told to exit. */ for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] == hmp) break; } if (i == HAMMER2_MAXCLUSTER) continue; hammer2_vfs_sync_pmp(pmp, MNT_WAIT); /* * Make sure all synchronization threads are locked * down. */ for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] == NULL) continue; hammer2_thr_freeze_async(&pmp->sync_thrs[i]); if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) { hammer2_thr_freeze_async( &pmp->xop_groups[j].thrs[i]); } } } for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] == NULL) continue; hammer2_thr_freeze(&pmp->sync_thrs[i]); if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) { hammer2_thr_freeze( &pmp->xop_groups[j].thrs[i]); } } } /* * Lock the inode and clean out matching chains. * Note that we cannot use hammer2_inode_lock_*() * here because that would attempt to validate the * cluster that we are in the middle of ripping * apart. * * WARNING! We are working directly on the inodes * embedded cluster. */ hammer2_mtx_ex(&iroot->lock); /* * Remove the chain from matching elements of the PFS. */ for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] != hmp) continue; hammer2_thr_delete(&pmp->sync_thrs[i]); if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) { hammer2_thr_delete( &pmp->xop_groups[j].thrs[i]); } } rchain = iroot->cluster.array[i].chain; iroot->cluster.array[i].chain = NULL; pmp->pfs_types[i] = HAMMER2_PFSTYPE_NONE; if (pmp->pfs_names[i]) { kfree(pmp->pfs_names[i], M_HAMMER2); pmp->pfs_names[i] = NULL; } if (rchain) { hammer2_chain_drop(rchain); /* focus hint */ if (iroot->cluster.focus == rchain) iroot->cluster.focus = NULL; } pmp->pfs_hmps[i] = NULL; } hammer2_mtx_unlock(&iroot->lock); /* * Cleanup trailing chains. Gaps may remain. */ for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) { if (pmp->pfs_hmps[i]) break; } iroot->cluster.nchains = i + 1; /* * If the PMP has no elements remaining we can destroy it. * (this will transition management threads from frozen->exit). */ if (iroot->cluster.nchains == 0) { /* * If this was the hmp's spmp, we need to clean * a little more stuff out. */ if (hmp->spmp == pmp) { hmp->spmp = NULL; hmp->vchain.pmp = NULL; hmp->fchain.pmp = NULL; } /* * Free the pmp and restart the loop */ KKASSERT(TAILQ_EMPTY(&pmp->syncq)); KKASSERT(TAILQ_EMPTY(&pmp->depq)); hammer2_pfsfree(pmp); goto again; } /* * If elements still remain we need to set the REMASTER * flag and unfreeze it. */ for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] == NULL) continue; hammer2_thr_remaster(&pmp->sync_thrs[i]); hammer2_thr_unfreeze(&pmp->sync_thrs[i]); if (pmp->xop_groups) { for (j = 0; j < hammer2_xop_nthreads; ++j) { hammer2_thr_remaster( &pmp->xop_groups[j].thrs[i]); hammer2_thr_unfreeze( &pmp->xop_groups[j].thrs[i]); } } } } } /* * Mount or remount HAMMER2 fileystem from physical media * * mountroot * mp mount point structure * path NULL * data <unused> * cred <unused> * * mount * mp mount point structure * path path to mount point * data pointer to argument structure in user space * volume volume path (device@LABEL form) * hflags user mount flags * cred user credentials * * RETURNS: 0 Success * !0 error number */ static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) { struct hammer2_mount_info info; hammer2_pfs_t *pmp; hammer2_pfs_t *spmp; hammer2_dev_t *hmp, *hmp_tmp; hammer2_dev_t *force_local; hammer2_key_t key_next; hammer2_key_t key_dummy; hammer2_key_t lhc; hammer2_chain_t *parent; hammer2_chain_t *chain; const hammer2_inode_data_t *ripdata; hammer2_devvp_list_t devvpl; hammer2_devvp_t *e, *e_tmp; struct file *fp; char devstr[MNAMELEN]; size_t size; size_t done; char *label; int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); int error; int i; hmp = NULL; pmp = NULL; label = NULL; bzero(&info, sizeof(info)); if (path) { /* * Non-root mount or updating a mount */ error = copyin(data, &info, sizeof(info)); if (error) return (error); } if (mp->mnt_flag & MNT_UPDATE) { /* * Update mount. Note that pmp->iroot->cluster is * an inode-embedded cluster and thus cannot be * directly locked. * * XXX HAMMER2 needs to implement NFS export via * mountctl. */ hammer2_cluster_t *cluster; error = 0; pmp = MPTOPMP(mp); pmp->hflags = info.hflags; cluster = &pmp->iroot->cluster; for (i = 0; i < cluster->nchains; ++i) { if (cluster->array[i].chain == NULL) continue; hmp = cluster->array[i].chain->hmp; error = hammer2_remount(hmp, mp, path, cred); if (error) break; } return error; } if (path == NULL) { /* * Root mount */ info.cluster_fd = -1; ksnprintf(devstr, sizeof(devstr), "%s", mp->mnt_stat.f_mntfromname); done = strlen(devstr) + 1; kprintf("hammer2_mount: root devstr=\"%s\"\n", devstr); } else { error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done); if (error) return (error); kprintf("hammer2_mount: devstr=\"%s\"\n", devstr); } /* * Extract device and label, automatically mount @BOOT, @ROOT, or @DATA * if no label specified, based on the partition id. Error out if no * label or device (with partition id) is specified. This is strictly * a convenience to match the default label created by newfs_hammer2, * our preference is that a label always be specified. * * NOTE: We allow 'mount @LABEL <blah>'... that is, a mount command * that does not specify a device, as long as some H2 label * has already been mounted from that device. This makes * mounting snapshots a lot easier. */ label = strchr(devstr, '@'); if (label && ((label + 1) - devstr) > done) { kprintf("hammer2_mount: bad label %s/%zd\n", devstr, done); return (EINVAL); } if (label == NULL || label[1] == 0) { char slice; if (label == NULL) label = devstr + strlen(devstr); else *label = '\0'; /* clean up trailing @ */ slice = label[-1]; switch(slice) { case 'a': label = "BOOT"; break; case 'd': label = "ROOT"; break; default: label = "DATA"; break; } } else { *label = '\0'; label++; } kprintf("hammer2_mount: device=\"%s\" label=\"%s\" rdonly=%d\n", devstr, label, ronly); /* * Initialize all device vnodes. */ TAILQ_INIT(&devvpl); error = hammer2_init_devvp(devstr, path == NULL, &devvpl); if (error) { kprintf("hammer2: failed to initialize devvp in %s\n", devstr); hammer2_cleanup_devvp(&devvpl); return error; } /* * Determine if the device has already been mounted. After this * check hmp will be non-NULL if we are doing the second or more * hammer2 mounts from the same device. */ lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); if (!TAILQ_EMPTY(&devvpl)) { /* * Match the device. Due to the way devfs works, * we may not be able to directly match the vnode pointer, * so also check to see if the underlying device matches. */ TAILQ_FOREACH(hmp_tmp, &hammer2_mntlist, mntentry) { TAILQ_FOREACH(e_tmp, &hmp_tmp->devvpl, entry) { int devvp_found = 0; TAILQ_FOREACH(e, &devvpl, entry) { KKASSERT(e->devvp); if (e_tmp->devvp == e->devvp) devvp_found = 1; if (e_tmp->devvp->v_rdev && e_tmp->devvp->v_rdev == e->devvp->v_rdev) devvp_found = 1; } if (!devvp_found) goto next_hmp; } hmp = hmp_tmp; kprintf("hammer2_mount: hmp=%p matched\n", hmp); break; next_hmp: continue; } /* * If no match this may be a fresh H2 mount, make sure * the device is not mounted on anything else. */ if (hmp == NULL) { TAILQ_FOREACH(e, &devvpl, entry) { struct vnode *devvp = e->devvp; KKASSERT(devvp); error = vfs_mountedon(devvp); if (error) { kprintf("hammer2_mount: %s mounted %d\n", e->path, error); hammer2_cleanup_devvp(&devvpl); lockmgr(&hammer2_mntlk, LK_RELEASE); return error; } } } } else { /* * Match the label to a pmp already probed. */ TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_names[i] && strcmp(pmp->pfs_names[i], label) == 0) { hmp = pmp->pfs_hmps[i]; break; } } if (hmp) break; } if (hmp == NULL) { kprintf("hammer2_mount: PFS label \"%s\" not found\n", label); hammer2_cleanup_devvp(&devvpl); lockmgr(&hammer2_mntlk, LK_RELEASE); return ENOENT; } } /* * Open the device if this isn't a secondary mount and construct * the H2 device mount (hmp). */ if (hmp == NULL) { hammer2_chain_t *schain; hammer2_xop_head_t xop; /* * Now open the device */ KKASSERT(!TAILQ_EMPTY(&devvpl)); error = hammer2_open_devvp(&devvpl, ronly); if (error) { hammer2_close_devvp(&devvpl, ronly); hammer2_cleanup_devvp(&devvpl); lockmgr(&hammer2_mntlk, LK_RELEASE); return error; } /* * Construct volumes and link with device vnodes. */ hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO); hmp->devvp = NULL; error = hammer2_init_volumes(mp, &devvpl, hmp->volumes, &hmp->voldata, &hmp->volhdrno, &hmp->devvp); if (error) { hammer2_close_devvp(&devvpl, ronly); hammer2_cleanup_devvp(&devvpl); lockmgr(&hammer2_mntlk, LK_RELEASE); kfree(hmp, M_HAMMER2); return error; } if (!hmp->devvp) { kprintf("hammer2: failed to initialize root volume\n"); hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); return EINVAL; } ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", devstr); hmp->ronly = ronly; hmp->hflags = info.hflags & HMNT2_DEVFLAGS; kmalloc_create_obj(&hmp->mchain, "HAMMER2-chains", sizeof(struct hammer2_chain)); kmalloc_create_obj(&hmp->mio, "HAMMER2-dio", sizeof(struct hammer2_io)); kmalloc_create(&hmp->mmsg, "HAMMER2-msg"); TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry); hammer2_io_hash_init(hmp); hammer2_spin_init(&hmp->list_spin, "h2mount_list"); lockinit(&hmp->vollk, "h2vol", 0, 0); lockinit(&hmp->bulklk, "h2bulk", 0, 0); lockinit(&hmp->bflock, "h2bflk", 0, 0); /* * vchain setup. vchain.data is embedded. * vchain.refs is initialized and will never drop to 0. */ hmp->vchain.hmp = hmp; hmp->vchain.refs = 1; hmp->vchain.data = (void *)&hmp->voldata; hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME; hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; hammer2_chain_init(&hmp->vchain); /* * fchain setup. fchain.data is embedded. * fchain.refs is initialized and will never drop to 0. * * The data is not used but needs to be initialized to * pass assertion muster. We use this chain primarily * as a placeholder for the freemap's top-level radix tree * so it does not interfere with the volume's topology * radix tree. */ hmp->fchain.hmp = hmp; hmp->fchain.refs = 1; hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset; hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP; hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; hmp->fchain.bref.methods = HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) | HAMMER2_ENC_COMP(HAMMER2_COMP_NONE); hammer2_chain_init(&hmp->fchain); /* * Initialize volume header related fields. */ KKASSERT(hmp->voldata.magic == HAMMER2_VOLUME_ID_HBO || hmp->voldata.magic == HAMMER2_VOLUME_ID_ABO); hmp->volsync = hmp->voldata; hmp->free_reserved = hmp->voldata.allocator_size / 20; /* * Must use hmp instead of volume header for these two * in order to handle volume versions transparently. */ if (hmp->voldata.version >= HAMMER2_VOL_VERSION_MULTI_VOLUMES) { hmp->nvolumes = hmp->voldata.nvolumes; hmp->total_size = hmp->voldata.total_size; } else { hmp->nvolumes = 1; hmp->total_size = hmp->voldata.volu_size; } KKASSERT(hmp->nvolumes > 0); /* * Move devvpl entries to hmp. */ TAILQ_INIT(&hmp->devvpl); while ((e = TAILQ_FIRST(&devvpl)) != NULL) { TAILQ_REMOVE(&devvpl, e, entry); TAILQ_INSERT_TAIL(&hmp->devvpl, e, entry); } KKASSERT(TAILQ_EMPTY(&devvpl)); KKASSERT(!TAILQ_EMPTY(&hmp->devvpl)); /* * Really important to get these right or the flush and * teardown code will get confused. */ hmp->spmp = hammer2_pfsalloc(NULL, NULL, NULL); spmp = hmp->spmp; spmp->pfs_hmps[0] = hmp; /* * Dummy-up vchain and fchain's modify_tid. mirror_tid * is inherited from the volume header. */ hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid; hmp->vchain.pmp = spmp; hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid; hmp->fchain.pmp = spmp; /* * First locate the super-root inode, which is key 0 * relative to the volume header's blockset. * * Then locate the root inode by scanning the directory keyspace * represented by the label. */ parent = hammer2_chain_lookup_init(&hmp->vchain, 0); schain = hammer2_chain_lookup(&parent, &key_dummy, HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, &error, 0); hammer2_chain_lookup_done(parent); if (schain == NULL) { kprintf("hammer2_mount: invalid super-root\n"); hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); return EINVAL; } if (schain->error) { kprintf("hammer2_mount: error %s reading super-root\n", hammer2_error_str(schain->error)); hammer2_chain_unlock(schain); hammer2_chain_drop(schain); schain = NULL; hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); return EINVAL; } /* * The super-root always uses an inode_tid of 1 when * creating PFSs. */ spmp->inode_tid = 1; spmp->modify_tid = schain->bref.modify_tid + 1; /* * Sanity-check schain's pmp and finish initialization. * Any chain belonging to the super-root topology should * have a NULL pmp (not even set to spmp). */ ripdata = &schain->data->ipdata; KKASSERT(schain->pmp == NULL); spmp->pfs_clid = ripdata->meta.pfs_clid; /* * Replace the dummy spmp->iroot with a real one. It's * easier to just do a wholesale replacement than to try * to update the chain and fixup the iroot fields. * * The returned inode is locked with the supplied cluster. */ hammer2_dummy_xop_from_chain(&xop, schain); hammer2_inode_drop(spmp->iroot); spmp->iroot = hammer2_inode_get(spmp, &xop, -1, -1); spmp->spmp_hmp = hmp; spmp->pfs_types[0] = ripdata->meta.pfs_type; spmp->ronly = ronly; hammer2_inode_ref(spmp->iroot); hammer2_inode_unlock(spmp->iroot); hammer2_cluster_unlock(&xop.cluster); hammer2_chain_drop(schain); /* do not call hammer2_cluster_drop() on an embedded cluster */ schain = NULL; /* now invalid */ /* leave spmp->iroot with one ref */ if (!hmp->ronly) { error = hammer2_recovery(hmp); if (error == 0) error |= hammer2_fixup_pfses(hmp); /* XXX do something with error */ } hammer2_update_pmps(hmp); hammer2_iocom_init(hmp); hammer2_bulkfree_init(hmp); /* * Ref the cluster management messaging descriptor. The mount * program deals with the other end of the communications pipe. * * Root mounts typically do not supply one. */ if (info.cluster_fd >= 0) { fp = holdfp(curthread, info.cluster_fd, -1); if (fp) { hammer2_cluster_reconnect(hmp, fp); } else { kprintf("hammer2_mount: bad cluster_fd!\n"); } } } else { /* hmp->devvp_list is already constructed. */ hammer2_cleanup_devvp(&devvpl); spmp = hmp->spmp; if (info.hflags & HMNT2_DEVFLAGS) { kprintf("hammer2_mount: Warning: mount flags pertaining " "to the whole device may only be specified " "on the first mount of the device: %08x\n", info.hflags & HMNT2_DEVFLAGS); } } /* * Force local mount (disassociate all PFSs from their clusters). * Used primarily for debugging. */ force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; /* * Lookup the mount point under the media-localized super-root. * Scanning hammer2_pfslist doesn't help us because it represents * PFS cluster ids which can aggregate several named PFSs together. * * cluster->pmp will incorrectly point to spmp and must be fixed * up later on. */ hammer2_inode_lock(spmp->iroot, 0); parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); lhc = hammer2_dirhash(label, strlen(label)); chain = hammer2_chain_lookup(&parent, &key_next, lhc, lhc + HAMMER2_DIRHASH_LOMASK, &error, 0); while (chain) { if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && strcmp(label, (char *)chain->data->ipdata.filename) == 0) { break; } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, lhc + HAMMER2_DIRHASH_LOMASK, &error, 0); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_inode_unlock(spmp->iroot); /* * PFS could not be found? */ if (chain == NULL) { hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); if (error) { kprintf("hammer2_mount: PFS label I/O error\n"); return EINVAL; } else { kprintf("hammer2_mount: PFS label \"%s\" not found\n", label); return ENOENT; } } /* * Acquire the pmp structure (it should have already been allocated * via hammer2_update_pmps()). */ if (chain->error) { kprintf("hammer2_mount: PFS label I/O error\n"); } else { ripdata = &chain->data->ipdata; pmp = hammer2_pfsalloc(NULL, ripdata, force_local); } hammer2_chain_unlock(chain); hammer2_chain_drop(chain); /* * PFS to mount must exist at this point. */ if (pmp == NULL) { kprintf("hammer2_mount: Failed to acquire PFS structure\n"); hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); return EINVAL; } /* * Finish the mount */ kprintf("hammer2_mount: hmp=%p pmp=%p\n", hmp, pmp); /* Check if the pmp has already been mounted. */ if (pmp->mp) { kprintf("hammer2_mount: PFS already mounted!\n"); hammer2_unmount_helper(mp, NULL, hmp); lockmgr(&hammer2_mntlk, LK_RELEASE); hammer2_vfs_unmount(mp, MNT_FORCE); return EBUSY; } pmp->ronly = ronly; pmp->hflags = info.hflags; mp->mnt_flag |= MNT_LOCAL; mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */ mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ /* * required mount structure initializations */ mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE; mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE; mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE; mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; /* * Optional fields */ mp->mnt_iosize_max = MAXPHYS; /* * Connect up mount pointers. */ hammer2_mount_helper(mp, pmp); lockmgr(&hammer2_mntlk, LK_RELEASE); /* * Finish setup */ vfs_getnewfsid(mp); vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops); vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops); vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops); if (path) { copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); } /* else root mount, already in there */ bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); if (path) { copyinstr(path, mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname) - 1, &size); } else { /* root mount */ mp->mnt_stat.f_mntonname[0] = '/'; } /* * Initial statfs to prime mnt_stat. */ hammer2_vfs_statfs(mp, &mp->mnt_stat, cred); return 0; } /* * Scan PFSs under the super-root and create hammer2_pfs structures. */ static void hammer2_update_pmps(hammer2_dev_t *hmp) { const hammer2_inode_data_t *ripdata; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_dev_t *force_local; hammer2_pfs_t *spmp; hammer2_key_t key_next; int error; /* * Force local mount (disassociate all PFSs from their clusters). * Used primarily for debugging. */ force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; /* * Lookup mount point under the media-localized super-root. * * cluster->pmp will incorrectly point to spmp and must be fixed * up later on. */ spmp = hmp->spmp; hammer2_inode_lock(spmp->iroot, 0); parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); chain = hammer2_chain_lookup(&parent, &key_next, HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, &error, 0); while (chain) { if (chain->error) { kprintf("I/O error scanning PFS labels\n"); } else if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) { kprintf("Non inode chain type %d under super-root\n", chain->bref.type); } else { ripdata = &chain->data->ipdata; hammer2_pfsalloc(chain, ripdata, force_local); } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, 0); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_inode_unlock(spmp->iroot); } static int hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused, struct ucred *cred) { hammer2_pfs_t *pmp; hammer2_volume_t *vol; struct vnode *devvp; int i, ronly_save, error, result = 0; pmp = MPTOPMP(mp); ronly_save = pmp->ronly; if (pmp->ronly == 1 && (mp->mnt_kern_flag & MNTK_WANTRDWR)) pmp->ronly = 0; else if (pmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) pmp->ronly = 1; if (!(hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR))) return 0; for (i = 0; i < hmp->nvolumes; ++i) { vol = &hmp->volumes[i]; devvp = vol->dev->devvp; KKASSERT(devvp); vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL); vn_unlock(devvp); error = 0; if (vol->id == HAMMER2_ROOT_VOLUME) { error = hammer2_recovery(hmp); if (error == 0) error |= hammer2_fixup_pfses(hmp); } vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); if (error == 0) { VOP_CLOSE(devvp, FREAD, NULL); } else { VOP_CLOSE(devvp, FREAD | FWRITE, NULL); } vn_unlock(devvp); result |= error; } if (result == 0) { kprintf("hammer2: enable read/write\n"); hmp->ronly = 0; hmp->spmp->ronly = 0; /* never used */ } else { pmp->ronly = ronly_save; } return result; } static int hammer2_vfs_unmount(struct mount *mp, int mntflags) { hammer2_pfs_t *pmp; int flags; int error = 0; pmp = MPTOPMP(mp); if (pmp == NULL) return(0); lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); /* * If mount initialization proceeded far enough we must flush * its vnodes and sync the underlying mount points. Three syncs * are required to fully flush the filesystem (freemap updates lag * by one flush, and one extra for safety). */ if (mntflags & MNT_FORCE) flags = FORCECLOSE; else flags = 0; if (pmp->iroot) { error = vflush(mp, 0, flags); if (error) goto failed; hammer2_vfs_sync(mp, MNT_WAIT); hammer2_vfs_sync(mp, MNT_WAIT); hammer2_vfs_sync(mp, MNT_WAIT); } /* * Cleanup the frontend support XOPS threads */ hammer2_xop_helper_cleanup(pmp); if (pmp->mp) hammer2_unmount_helper(mp, pmp, NULL); error = 0; failed: lockmgr(&hammer2_mntlk, LK_RELEASE); return (error); } /* * Mount helper, hook the system mount into our PFS. * The mount lock is held. * * We must bump the mount_count on related devices for any * mounted PFSs. */ static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp) { hammer2_cluster_t *cluster; hammer2_chain_t *rchain; int i; mp->mnt_data = (qaddr_t)pmp; pmp->mp = mp; /* * After pmp->mp is set we have to adjust hmp->mount_count. */ cluster = &pmp->iroot->cluster; for (i = 0; i < cluster->nchains; ++i) { rchain = cluster->array[i].chain; if (rchain == NULL) continue; ++rchain->hmp->mount_count; } /* * Create missing Xop threads */ hammer2_xop_helper_create(pmp); } /* * Unmount helper, unhook the system mount from our PFS. * The mount lock is held. * * If hmp is supplied a mount responsible for being the first to open * the block device failed and the block device and all PFSs using the * block device must be cleaned up. * * If pmp is supplied multiple devices might be backing the PFS and each * must be disconnected. This might not be the last PFS using some of the * underlying devices. Also, we have to adjust our hmp->mount_count * accounting for the devices backing the pmp which is now undergoing an * unmount. */ static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp) { hammer2_cluster_t *cluster; hammer2_chain_t *rchain; int dumpcnt; int i; /* * If no device supplied this is a high-level unmount and we have to * to disconnect the mount, adjust mount_count, and locate devices * that might now have no mounts. */ if (pmp) { KKASSERT(hmp == NULL); KKASSERT(MPTOPMP(mp) == pmp); pmp->mp = NULL; mp->mnt_data = NULL; /* * After pmp->mp is cleared we have to account for * mount_count. */ cluster = &pmp->iroot->cluster; for (i = 0; i < cluster->nchains; ++i) { rchain = cluster->array[i].chain; if (rchain == NULL) continue; --rchain->hmp->mount_count; /* scrapping hmp now may invalidate the pmp */ } again: TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { if (hmp->mount_count == 0) { hammer2_unmount_helper(NULL, NULL, hmp); goto again; } } return; } /* * Try to terminate the block device. We can't terminate it if * there are still PFSs referencing it. */ if (hmp->mount_count) return; /* * Decomission the network before we start messing with the * device and PFS. */ hammer2_iocom_uninit(hmp); hammer2_bulkfree_uninit(hmp); hammer2_pfsfree_scan(hmp, 0); /* * Cycle the volume data lock as a safety (probably not needed any * more). To ensure everything is out we need to flush at least * three times. (1) The running of the sideq can dirty the * filesystem, (2) A normal flush can dirty the freemap, and * (3) ensure that the freemap is fully synchronized. * * The next mount's recovery scan can clean everything up but we want * to leave the filesystem in a 100% clean state on a normal unmount. */ #if 0 hammer2_voldata_lock(hmp); hammer2_voldata_unlock(hmp); #endif /* * Flush whatever is left. Unmounted but modified PFS's might still * have some dirty chains on them. */ hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS); if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { hammer2_voldata_modify(hmp); hammer2_flush(&hmp->fchain, HAMMER2_FLUSH_TOP | HAMMER2_FLUSH_ALL); } hammer2_chain_unlock(&hmp->fchain); if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { hammer2_flush(&hmp->vchain, HAMMER2_FLUSH_TOP | HAMMER2_FLUSH_ALL); } hammer2_chain_unlock(&hmp->vchain); if ((hmp->vchain.flags | hmp->fchain.flags) & HAMMER2_CHAIN_FLUSH_MASK) { kprintf("hammer2_unmount: chains left over after final sync\n"); kprintf(" vchain %08x\n", hmp->vchain.flags); kprintf(" fchain %08x\n", hmp->fchain.flags); if (hammer2_debug & 0x0010) Debugger("entered debugger"); } hammer2_pfsfree_scan(hmp, 1); KKASSERT(hmp->spmp == NULL); /* * Finish up with the device vnode */ if (!TAILQ_EMPTY(&hmp->devvpl)) { hammer2_close_devvp(&hmp->devvpl, hmp->ronly); hammer2_cleanup_devvp(&hmp->devvpl); } KKASSERT(TAILQ_EMPTY(&hmp->devvpl)); /* * Clear vchain/fchain flags that might prevent final cleanup * of these chains. */ if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) { atomic_add_long(&hammer2_count_modified_chains, -1); atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED); hammer2_pfs_memory_wakeup(hmp->vchain.pmp, -1); } if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) { atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_UPDATE); } if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) { atomic_add_long(&hammer2_count_modified_chains, -1); atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_MODIFIED); hammer2_pfs_memory_wakeup(hmp->fchain.pmp, -1); } if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) { atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_UPDATE); } dumpcnt = 50; hammer2_dump_chain(&hmp->vchain, 0, 0, &dumpcnt, 'v', (u_int)-1); dumpcnt = 50; hammer2_dump_chain(&hmp->fchain, 0, 0, &dumpcnt, 'f', (u_int)-1); /* * Final drop of embedded freemap root chain to * clean up fchain.core (fchain structure is not * flagged ALLOCATED so it is cleaned out and then * left to rot). */ hammer2_chain_drop(&hmp->fchain); /* * Final drop of embedded volume root chain to clean * up vchain.core (vchain structure is not flagged * ALLOCATED so it is cleaned out and then left to * rot). */ hammer2_chain_drop(&hmp->vchain); hammer2_io_hash_cleanup_all(hmp); if (hmp->iofree_count) { kprintf("io_cleanup: %d I/O's left hanging\n", hmp->iofree_count); } TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry); kmalloc_destroy_obj(&hmp->mchain); kmalloc_destroy_obj(&hmp->mio); kmalloc_destroy(&hmp->mmsg); kfree(hmp, M_HAMMER2); } int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp) { hammer2_xop_lookup_t *xop; hammer2_pfs_t *pmp; hammer2_inode_t *ip; hammer2_tid_t inum; int error; inum = (hammer2_tid_t)ino & HAMMER2_DIRHASH_USERMSK; error = 0; pmp = MPTOPMP(mp); /* * Easy if we already have it cached */ ip = hammer2_inode_lookup(pmp, inum); if (ip) { hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); *vpp = hammer2_igetv(ip, &error); hammer2_inode_unlock(ip); hammer2_inode_drop(ip); /* from lookup */ return error; } /* * Otherwise we have to find the inode */ xop = hammer2_xop_alloc(pmp->iroot, 0); xop->lhc = inum; hammer2_xop_start(&xop->head, &hammer2_lookup_desc); error = hammer2_xop_collect(&xop->head, 0); if (error == 0) ip = hammer2_inode_get(pmp, &xop->head, -1, -1); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); if (ip) { *vpp = hammer2_igetv(ip, &error); hammer2_inode_unlock(ip); } else { *vpp = NULL; error = ENOENT; } return (error); } static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp) { hammer2_pfs_t *pmp; struct vnode *vp; int error; pmp = MPTOPMP(mp); if (pmp->iroot == NULL) { kprintf("hammer2 (%s): no root inode\n", mp->mnt_stat.f_mntfromname); *vpp = NULL; return EINVAL; } error = 0; hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); while (pmp->inode_tid == 0) { hammer2_xop_ipcluster_t *xop; const hammer2_inode_meta_t *meta; xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING); hammer2_xop_start(&xop->head, &hammer2_ipcluster_desc); error = hammer2_xop_collect(&xop->head, 0); if (error == 0) { meta = &hammer2_xop_gdata(&xop->head)->ipdata.meta; pmp->iroot->meta = *meta; pmp->inode_tid = meta->pfs_inum + 1; hammer2_xop_pdata(&xop->head); /* meta invalid */ if (pmp->inode_tid < HAMMER2_INODE_START) pmp->inode_tid = HAMMER2_INODE_START; pmp->modify_tid = xop->head.cluster.focus->bref.modify_tid + 1; #if 0 kprintf("PFS: Starting inode %jd\n", (intmax_t)pmp->inode_tid); kprintf("PMP focus good set nextino=%ld mod=%016jx\n", pmp->inode_tid, pmp->modify_tid); #endif wakeup(&pmp->iroot); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); /* * Prime the mount info. */ hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL); break; } /* * Loop, try again */ hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); hammer2_inode_unlock(pmp->iroot); error = tsleep(&pmp->iroot, PCATCH, "h2root", hz); hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); if (error == EINTR) break; } if (error) { hammer2_inode_unlock(pmp->iroot); *vpp = NULL; } else { vp = hammer2_igetv(pmp->iroot, &error); hammer2_inode_unlock(pmp->iroot); *vpp = vp; } return (error); } /* * Filesystem status * * XXX incorporate ipdata->meta.inode_quota and data_quota */ static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) { hammer2_pfs_t *pmp; hammer2_dev_t *hmp; hammer2_blockref_t bref; struct statfs tmp; int i; /* * NOTE: iroot might not have validated the cluster yet. */ pmp = MPTOPMP(mp); bzero(&tmp, sizeof(tmp)); for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { hmp = pmp->pfs_hmps[i]; if (hmp == NULL) continue; if (pmp->iroot->cluster.array[i].chain) bref = pmp->iroot->cluster.array[i].chain->bref; else bzero(&bref, sizeof(bref)); tmp.f_files = bref.embed.stats.inode_count; tmp.f_ffree = 0; tmp.f_blocks = hmp->voldata.allocator_size / mp->mnt_vstat.f_bsize; tmp.f_bfree = hmp->voldata.allocator_free / mp->mnt_vstat.f_bsize; tmp.f_bavail = tmp.f_bfree; if (cred && cred->cr_uid != 0) { uint64_t adj; /* 5% */ adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; tmp.f_blocks -= adj; tmp.f_bfree -= adj; tmp.f_bavail -= adj; } mp->mnt_stat.f_blocks = tmp.f_blocks; mp->mnt_stat.f_bfree = tmp.f_bfree; mp->mnt_stat.f_bavail = tmp.f_bavail; mp->mnt_stat.f_files = tmp.f_files; mp->mnt_stat.f_ffree = tmp.f_ffree; *sbp = mp->mnt_stat; } return (0); } static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) { hammer2_pfs_t *pmp; hammer2_dev_t *hmp; hammer2_blockref_t bref; struct statvfs tmp; int i; /* * NOTE: iroot might not have validated the cluster yet. */ pmp = MPTOPMP(mp); bzero(&tmp, sizeof(tmp)); for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { hmp = pmp->pfs_hmps[i]; if (hmp == NULL) continue; if (pmp->iroot->cluster.array[i].chain) bref = pmp->iroot->cluster.array[i].chain->bref; else bzero(&bref, sizeof(bref)); tmp.f_files = bref.embed.stats.inode_count; tmp.f_ffree = 0; tmp.f_blocks = hmp->voldata.allocator_size / mp->mnt_vstat.f_bsize; tmp.f_bfree = hmp->voldata.allocator_free / mp->mnt_vstat.f_bsize; tmp.f_bavail = tmp.f_bfree; if (cred && cred->cr_uid != 0) { uint64_t adj; /* 5% */ adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; tmp.f_blocks -= adj; tmp.f_bfree -= adj; tmp.f_bavail -= adj; } mp->mnt_vstat.f_blocks = tmp.f_blocks; mp->mnt_vstat.f_bfree = tmp.f_bfree; mp->mnt_vstat.f_bavail = tmp.f_bavail; mp->mnt_vstat.f_files = tmp.f_files; mp->mnt_vstat.f_ffree = tmp.f_ffree; *sbp = mp->mnt_vstat; } return (0); } /* * Mount-time recovery (RW mounts) * * Updates to the free block table are allowed to lag flushes by one * transaction. In case of a crash, then on a fresh mount we must do an * incremental scan of the last committed transaction id and make sure that * all related blocks have been marked allocated. */ struct hammer2_recovery_elm { TAILQ_ENTRY(hammer2_recovery_elm) entry; hammer2_chain_t *chain; hammer2_tid_t sync_tid; }; TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm); struct hammer2_recovery_info { struct hammer2_recovery_list list; hammer2_tid_t mtid; int depth; }; static int hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent, struct hammer2_recovery_info *info, hammer2_tid_t sync_tid); #define HAMMER2_RECOVERY_MAXDEPTH 10 static int hammer2_recovery(hammer2_dev_t *hmp) { struct hammer2_recovery_info info; struct hammer2_recovery_elm *elm; hammer2_chain_t *parent; hammer2_tid_t sync_tid; hammer2_tid_t mirror_tid; int error; hammer2_trans_init(hmp->spmp, 0); sync_tid = hmp->voldata.freemap_tid; mirror_tid = hmp->voldata.mirror_tid; kprintf("hammer2_mount: \"%s\": ", hmp->devrepname); if (sync_tid >= mirror_tid) { kprintf("no recovery needed\n"); } else { kprintf("freemap recovery %016jx-%016jx\n", sync_tid + 1, mirror_tid); } TAILQ_INIT(&info.list); info.depth = 0; parent = hammer2_chain_lookup_init(&hmp->vchain, 0); error = hammer2_recovery_scan(hmp, parent, &info, sync_tid); hammer2_chain_lookup_done(parent); while ((elm = TAILQ_FIRST(&info.list)) != NULL) { TAILQ_REMOVE(&info.list, elm, entry); parent = elm->chain; sync_tid = elm->sync_tid; kfree(elm, M_HAMMER2); hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); error |= hammer2_recovery_scan(hmp, parent, &info, hmp->voldata.freemap_tid); hammer2_chain_unlock(parent); hammer2_chain_drop(parent); /* drop elm->chain ref */ } hammer2_trans_done(hmp->spmp, 0); return error; } static int hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent, struct hammer2_recovery_info *info, hammer2_tid_t sync_tid) { const hammer2_inode_data_t *ripdata; hammer2_chain_t *chain; hammer2_blockref_t bref; int tmp_error; int rup_error; int error; int first; /* * Adjust freemap to ensure that the block(s) are marked allocated. */ if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) { hammer2_freemap_adjust(hmp, &parent->bref, HAMMER2_FREEMAP_DORECOVER); } /* * Check type for recursive scan */ switch(parent->bref.type) { case HAMMER2_BREF_TYPE_VOLUME: /* data already instantiated */ break; case HAMMER2_BREF_TYPE_INODE: /* * Must instantiate data for DIRECTDATA test and also * for recursion. */ hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); ripdata = &parent->data->ipdata; if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { /* not applicable to recovery scan */ hammer2_chain_unlock(parent); return 0; } hammer2_chain_unlock(parent); break; case HAMMER2_BREF_TYPE_INDIRECT: /* * Must instantiate data for recursion */ hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); hammer2_chain_unlock(parent); break; case HAMMER2_BREF_TYPE_DIRENT: case HAMMER2_BREF_TYPE_DATA: case HAMMER2_BREF_TYPE_FREEMAP: case HAMMER2_BREF_TYPE_FREEMAP_NODE: case HAMMER2_BREF_TYPE_FREEMAP_LEAF: /* not applicable to recovery scan */ return 0; break; default: return HAMMER2_ERROR_BADBREF; } /* * Defer operation if depth limit reached. */ if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) { struct hammer2_recovery_elm *elm; elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK); elm->chain = parent; elm->sync_tid = sync_tid; hammer2_chain_ref(parent); TAILQ_INSERT_TAIL(&info->list, elm, entry); /* unlocked by caller */ return(0); } /* * Recursive scan of the last flushed transaction only. We are * doing this without pmp assignments so don't leave the chains * hanging around after we are done with them. * * error Cumulative error this level only * rup_error Cumulative error for recursion * tmp_error Specific non-cumulative recursion error */ chain = NULL; first = 1; rup_error = 0; error = 0; for (;;) { error |= hammer2_chain_scan(parent, &chain, &bref, &first, HAMMER2_LOOKUP_NODATA); /* * Problem during scan or EOF */ if (error) break; /* * If this is a leaf */ if (chain == NULL) { if (bref.mirror_tid > sync_tid) { hammer2_freemap_adjust(hmp, &bref, HAMMER2_FREEMAP_DORECOVER); } continue; } /* * This may or may not be a recursive node. */ atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); if (bref.mirror_tid > sync_tid) { ++info->depth; tmp_error = hammer2_recovery_scan(hmp, chain, info, sync_tid); --info->depth; } else { tmp_error = 0; } /* * Flush the recovery at the PFS boundary to stage it for * the final flush of the super-root topology. */ if (tmp_error == 0 && (bref.flags & HAMMER2_BREF_FLAG_PFSROOT) && (chain->flags & HAMMER2_CHAIN_ONFLUSH)) { hammer2_flush(chain, HAMMER2_FLUSH_TOP | HAMMER2_FLUSH_ALL); } rup_error |= tmp_error; } return ((error | rup_error) & ~HAMMER2_ERROR_EOF); } /* * This fixes up an error introduced in earlier H2 implementations where * moving a PFS inode into an indirect block wound up causing the * HAMMER2_BREF_FLAG_PFSROOT flag in the bref to get cleared. */ static int hammer2_fixup_pfses(hammer2_dev_t *hmp) { const hammer2_inode_data_t *ripdata; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; hammer2_pfs_t *spmp; int error; error = 0; /* * Lookup mount point under the media-localized super-root. * * cluster->pmp will incorrectly point to spmp and must be fixed * up later on. */ spmp = hmp->spmp; hammer2_inode_lock(spmp->iroot, 0); parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); chain = hammer2_chain_lookup(&parent, &key_next, HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, &error, 0); while (chain) { if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) continue; if (chain->error) { kprintf("I/O error scanning PFS labels\n"); error |= chain->error; } else if ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) == 0) { int error2; ripdata = &chain->data->ipdata; hammer2_trans_init(hmp->spmp, 0); error2 = hammer2_chain_modify(chain, chain->bref.modify_tid, 0, 0); if (error2 == 0) { kprintf("hammer2: Correct mis-flagged PFS %s\n", ripdata->filename); chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT; } else { error |= error2; } hammer2_flush(chain, HAMMER2_FLUSH_TOP | HAMMER2_FLUSH_ALL); hammer2_trans_done(hmp->spmp, 0); } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, 0); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_inode_unlock(spmp->iroot); return error; } /* * Sync a mount point; this is called periodically on a per-mount basis from * the filesystem syncer, and whenever a user issues a sync. */ int hammer2_vfs_sync(struct mount *mp, int waitfor) { int error; error = hammer2_vfs_sync_pmp(MPTOPMP(mp), waitfor); return error; } /* * Because frontend operations lock vnodes before we get a chance to * lock the related inode, we can't just acquire a vnode lock without * risking a deadlock. The frontend may be holding a vnode lock while * also blocked on our SYNCQ flag while trying to get the inode lock. * * To deal with this situation we can check the vnode lock situation * after locking the inode and perform a work-around. */ int hammer2_vfs_sync_pmp(hammer2_pfs_t *pmp, int waitfor) { hammer2_inode_t *ip; hammer2_depend_t *depend; hammer2_depend_t *depend_next; struct vnode *vp; uint32_t pass2; int error; int wakecount; int dorestart; /* * Move all inodes on sideq to syncq. This will clear sideq. * This should represent all flushable inodes. These inodes * will already have refs due to being on syncq or sideq. We * must do this all at once with the spinlock held to ensure that * all inode dependencies are part of the same flush. * * We should be able to do this asynchronously from frontend * operations because we will be locking the inodes later on * to actually flush them, and that will partition any frontend * op using the same inode. Either it has already locked the * inode and we will block, or it has not yet locked the inode * and it will block until we are finished flushing that inode. * * When restarting, only move the inodes flagged as PASS2 from * SIDEQ to SYNCQ. PASS2 propagation by inode_lock4() and * inode_depend() are atomic with the spin-lock. */ hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH); #ifdef HAMMER2_DEBUG_SYNC kprintf("FILESYSTEM SYNC BOUNDARY\n"); #endif dorestart = 0; /* * Move inodes from depq to syncq, releasing the related * depend structures. */ restart: #ifdef HAMMER2_DEBUG_SYNC kprintf("FILESYSTEM SYNC RESTART (%d)\n", dorestart); #endif hammer2_trans_setflags(pmp, 0/*HAMMER2_TRANS_COPYQ*/); hammer2_trans_clearflags(pmp, HAMMER2_TRANS_RESCAN); /* * Move inodes from depq to syncq. When restarting, only depq's * marked pass2 are moved. */ hammer2_spin_ex(&pmp->list_spin); depend_next = TAILQ_FIRST(&pmp->depq); wakecount = 0; while ((depend = depend_next) != NULL) { depend_next = TAILQ_NEXT(depend, entry); if (dorestart && depend->pass2 == 0) continue; TAILQ_FOREACH(ip, &depend->sideq, entry) { KKASSERT(ip->flags & HAMMER2_INODE_SIDEQ); atomic_set_int(&ip->flags, HAMMER2_INODE_SYNCQ); atomic_clear_int(&ip->flags, HAMMER2_INODE_SIDEQ); ip->depend = NULL; } /* * NOTE: pmp->sideq_count includes both sideq and syncq */ TAILQ_CONCAT(&pmp->syncq, &depend->sideq, entry); depend->count = 0; depend->pass2 = 0; TAILQ_REMOVE(&pmp->depq, depend, entry); } hammer2_spin_unex(&pmp->list_spin); hammer2_trans_clearflags(pmp, /*HAMMER2_TRANS_COPYQ |*/ HAMMER2_TRANS_WAITING); dorestart = 0; /* * sideq_count may have dropped enough to allow us to unstall * the frontend. */ hammer2_pfs_memory_wakeup(pmp, 0); /* * Now run through all inodes on syncq. * * Flush transactions only interlock with other flush transactions. * Any conflicting frontend operations will block on the inode, but * may hold a vnode lock while doing so. */ hammer2_spin_ex(&pmp->list_spin); while ((ip = TAILQ_FIRST(&pmp->syncq)) != NULL) { /* * Remove the inode from the SYNCQ, transfer the syncq ref * to us. We must clear SYNCQ to allow any potential * front-end deadlock to proceed. We must set PASS2 so * the dependency code knows what to do. */ pass2 = ip->flags; cpu_ccfence(); if (atomic_cmpset_int(&ip->flags, pass2, (pass2 & ~(HAMMER2_INODE_SYNCQ | HAMMER2_INODE_SYNCQ_WAKEUP)) | HAMMER2_INODE_SYNCQ_PASS2) == 0) { continue; } TAILQ_REMOVE(&pmp->syncq, ip, entry); --pmp->sideq_count; hammer2_spin_unex(&pmp->list_spin); /* * Tickle anyone waiting on ip->flags or the hysteresis * on the dirty inode count. */ if (pass2 & HAMMER2_INODE_SYNCQ_WAKEUP) wakeup(&ip->flags); if (++wakecount >= hammer2_limit_dirty_inodes / 20 + 1) { wakecount = 0; hammer2_pfs_memory_wakeup(pmp, 0); } /* * Relock the inode, and we inherit a ref from the above. * We will check for a race after we acquire the vnode. */ hammer2_mtx_ex(&ip->lock); /* * We need the vp in order to vfsync() dirty buffers, so if * one isn't attached we can skip it. * * Ordering the inode lock and then the vnode lock has the * potential to deadlock. If we had left SYNCQ set that could * also deadlock us against the frontend even if we don't hold * any locks, but the latter is not a problem now since we * cleared it. igetv will temporarily release the inode lock * in a safe manner to work-around the deadlock. * * Unfortunately it is still possible to deadlock when the * frontend obtains multiple inode locks, because all the * related vnodes are already locked (nor can the vnode locks * be released and reacquired without messing up RECLAIM and * INACTIVE sequencing). * * The solution for now is to move the vp back onto SIDEQ * and set dorestart, which will restart the flush after we * exhaust the current SYNCQ. Note that additional * dependencies may build up, so we definitely need to move * the whole SIDEQ back to SYNCQ when we restart. */ vp = ip->vp; if (vp) { if (vget(vp, LK_EXCLUSIVE|LK_NOWAIT)) { /* * Failed to get the vnode, requeue the inode * (PASS2 is already set so it will be found * again on the restart). Then unlock. */ vp = NULL; dorestart |= 1; #ifdef HAMMER2_DEBUG_SYNC kprintf("inum %ld (sync delayed by vnode)\n", (long)ip->meta.inum); #endif hammer2_inode_delayed_sideq(ip); hammer2_mtx_unlock(&ip->lock); hammer2_inode_drop(ip); /* * If PASS2 was previously set we might * be looping too hard, ask for a delay * along with the restart. */ if (pass2 & HAMMER2_INODE_SYNCQ_PASS2) dorestart |= 2; hammer2_spin_ex(&pmp->list_spin); continue; } } else { vp = NULL; } /* * If the inode wound up on a SIDEQ again it will already be * prepped for another PASS2. In this situation if we flush * it now we will just wind up flushing it again in the same * syncer run, so we might as well not flush it now. */ if (ip->flags & HAMMER2_INODE_SIDEQ) { hammer2_mtx_unlock(&ip->lock); hammer2_inode_drop(ip); if (vp) vput(vp); dorestart |= 1; hammer2_spin_ex(&pmp->list_spin); continue; } /* * Ok we have the inode exclusively locked and if vp is * not NULL that will also be exclusively locked. Do the * meat of the flush. * * vp token needed for v_rbdirty_tree check / vclrisdirty * sequencing. Though we hold the vnode exclusively so * we shouldn't need to hold the token also in this case. */ if (vp) { vfsync(vp, MNT_WAIT, 1, NULL, NULL); bio_track_wait(&vp->v_track_write, 0, 0); /* XXX */ } /* * If the inode has not yet been inserted into the tree * we must do so. Then sync and flush it. The flush should * update the parent. */ if (ip->flags & HAMMER2_INODE_DELETING) { #ifdef HAMMER2_DEBUG_SYNC kprintf("inum %ld destroy\n", (long)ip->meta.inum); #endif hammer2_inode_chain_des(ip); atomic_add_long(&hammer2_iod_inode_deletes, 1); } else if (ip->flags & HAMMER2_INODE_CREATING) { #ifdef HAMMER2_DEBUG_SYNC kprintf("inum %ld insert\n", (long)ip->meta.inum); #endif hammer2_inode_chain_ins(ip); atomic_add_long(&hammer2_iod_inode_creates, 1); } #ifdef HAMMER2_DEBUG_SYNC kprintf("inum %ld chain-sync\n", (long)ip->meta.inum); #endif /* * Because I kinda messed up the design and index the inodes * under the root inode, along side the directory entries, * we can't flush the inode index under the iroot until the * end. If we do it now we might miss effects created by * other inodes on the SYNCQ. * * Do a normal (non-FSSYNC) flush instead, which allows the * vnode code to work the same. We don't want to force iroot * back onto the SIDEQ, and we also don't want the flush code * to update pfs_iroot_blocksets until the final flush later. * * XXX at the moment this will likely result in a double-flush * of the iroot chain. */ hammer2_inode_chain_sync(ip); if (ip == pmp->iroot) { hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP); } else { hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP | HAMMER2_XOP_FSSYNC); } if (vp) { lwkt_gettoken(&vp->v_token); if ((ip->flags & (HAMMER2_INODE_MODIFIED | HAMMER2_INODE_RESIZED | HAMMER2_INODE_DIRTYDATA)) == 0 && RB_EMPTY(&vp->v_rbdirty_tree) && !bio_track_active(&vp->v_track_write)) { vclrisdirty(vp); } else { hammer2_inode_delayed_sideq(ip); } lwkt_reltoken(&vp->v_token); vput(vp); vp = NULL; /* safety */ } atomic_clear_int(&ip->flags, HAMMER2_INODE_SYNCQ_PASS2); hammer2_inode_unlock(ip); /* unlock+drop */ /* ip pointer invalid */ /* * If the inode got dirted after we dropped our locks, * it will have already been moved back to the SIDEQ. */ hammer2_spin_ex(&pmp->list_spin); } hammer2_spin_unex(&pmp->list_spin); hammer2_pfs_memory_wakeup(pmp, 0); if (dorestart || (pmp->trans.flags & HAMMER2_TRANS_RESCAN)) { /* * bit 2 is set if something above thinks we might be * looping too hard, try to unclog the frontend * dependency and wait a bit before restarting. * * NOTE: The frontend could be stuck in h2memw, though * it isn't supposed to be holding vnode locks * in that case. */ if (dorestart & 2) { wakeup(&pmp->inmem_dirty_chains); tsleep(&dorestart, 0, "h2syndel", 2); } #ifdef HAMMER2_DEBUG_SYNC kprintf("FILESYSTEM SYNC STAGE 1 RESTART\n"); /*tsleep(&dorestart, 0, "h2STG1-R", hz*20);*/ #endif dorestart = 1; goto restart; } #ifdef HAMMER2_DEBUG_SYNC kprintf("FILESYSTEM SYNC STAGE 2 BEGIN\n"); /*tsleep(&dorestart, 0, "h2STG2", hz*20);*/ #endif /* * We have to flush the PFS root last, even if it does not appear to * be dirty, because all the inodes in the PFS are indexed under it. * The normal flushing of iroot above would only occur if directory * entries under the root were changed. * * Specifying VOLHDR will cause an additionl flush of hmp->spmp * for the media making up the cluster. */ if ((ip = pmp->iroot) != NULL) { hammer2_inode_ref(ip); hammer2_mtx_ex(&ip->lock); hammer2_inode_chain_sync(ip); hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP | HAMMER2_XOP_FSSYNC | HAMMER2_XOP_VOLHDR); hammer2_inode_unlock(ip); /* unlock+drop */ } #ifdef HAMMER2_DEBUG_SYNC kprintf("FILESYSTEM SYNC STAGE 2 DONE\n"); #endif /* * device bioq sync */ hammer2_bioq_sync(pmp); error = 0; /* XXX */ hammer2_trans_done(pmp, HAMMER2_TRANS_ISFLUSH); return (error); } static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp) { hammer2_inode_t *ip; KKASSERT(MAXFIDSZ >= 16); ip = VTOI(vp); fhp->fid_len = offsetof(struct fid, fid_data[16]); fhp->fid_ext = 0; ((hammer2_tid_t *)fhp->fid_data)[0] = ip->meta.inum; ((hammer2_tid_t *)fhp->fid_data)[1] = 0; return 0; } static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp) { hammer2_tid_t inum; int error; inum = ((hammer2_tid_t *)fhp->fid_data)[0] & HAMMER2_DIRHASH_USERMSK; if (vpp) { if (inum == 1) error = hammer2_vfs_root(mp, vpp); else error = hammer2_vfs_vget(mp, NULL, inum, vpp); } else { error = 0; } return error; } static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp, struct ucred **credanonp) { hammer2_pfs_t *pmp; struct netcred *np; int error; pmp = MPTOPMP(mp); np = vfs_export_lookup(mp, &pmp->export, nam); if (np) { *exflagsp = np->netc_exflags; *credanonp = &np->netc_anon; error = 0; } else { error = EACCES; } return error; } /* * This handles hysteresis on regular file flushes. Because the BIOs are * routed to a thread it is possible for an excessive number to build up * and cause long front-end stalls long before the runningbuffspace limit * is hit, so we implement hammer2_flush_pipe to control the * hysteresis. * * This is a particular problem when compression is used. */ void hammer2_lwinprog_ref(hammer2_pfs_t *pmp) { atomic_add_int(&pmp->count_lwinprog, 1); } void hammer2_lwinprog_drop(hammer2_pfs_t *pmp) { int lwinprog; lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1); if ((lwinprog & HAMMER2_LWINPROG_WAITING) && (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) { atomic_clear_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING); wakeup(&pmp->count_lwinprog); } if ((lwinprog & HAMMER2_LWINPROG_WAITING0) && (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) { atomic_clear_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING0); wakeup(&pmp->count_lwinprog); } } void hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe) { int lwinprog; int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING : HAMMER2_LWINPROG_WAITING0; for (;;) { lwinprog = pmp->count_lwinprog; cpu_ccfence(); if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) break; tsleep_interlock(&pmp->count_lwinprog, 0); atomic_set_int(&pmp->count_lwinprog, lwflag); lwinprog = pmp->count_lwinprog; if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) break; tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz); } } /* * It is possible for an excessive number of dirty chains or dirty inodes * to build up. When this occurs we start an asynchronous filesystem sync. * If the level continues to build up, we stall, waiting for it to drop, * with some hysteresis. * * This relies on the kernel calling hammer2_vfs_modifying() prior to * obtaining any vnode locks before making a modifying VOP call. */ static int hammer2_vfs_modifying(struct mount *mp) { if (mp->mnt_flag & MNT_RDONLY) return EROFS; hammer2_pfs_memory_wait(MPTOPMP(mp)); return 0; } /* * Initiate an asynchronous filesystem sync and, with hysteresis, * stall if the internal data structure count becomes too bloated. */ void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp) { uint32_t waiting; int pcatch; int error; int started; if (pmp == NULL || pmp->mp == NULL) return; started = 0; for (;;) { waiting = pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK; cpu_ccfence(); /* * Start the syncer running at 1/2 the limit to try * to avoid sleeping. */ if (waiting > hammer2_limit_dirty_chains / 2 || pmp->sideq_count > hammer2_limit_dirty_inodes / 2) { trigger_syncer(pmp->mp); } /* * Stall at the limit waiting for the counts to drop. * This code will typically be woken up once the count * drops below 3/4 the limit, or in one second. */ if (waiting < hammer2_limit_dirty_chains && pmp->sideq_count < hammer2_limit_dirty_inodes) { break; } if (started == 0) { trigger_syncer_start(pmp->mp); started = 1; } /* * Interlocked re-test, sleep, and retry. */ pcatch = curthread->td_proc ? PCATCH : 0; tsleep_interlock(&pmp->inmem_dirty_chains, pcatch); atomic_set_int(&pmp->inmem_dirty_chains, HAMMER2_DIRTYCHAIN_WAITING); if (waiting < hammer2_limit_dirty_chains && pmp->sideq_count < hammer2_limit_dirty_inodes) { break; } error = tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED | pcatch, "h2memw", hz); if (error == ERESTART) break; } if (started) trigger_syncer_stop(pmp->mp); } /* * Wake up any stalled frontend ops waiting, with hysteresis, using * 2/3 of the limit. */ void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp, int count) { uint32_t waiting; if (pmp) { waiting = atomic_fetchadd_int(&pmp->inmem_dirty_chains, count); /* don't need --waiting to test flag */ if ((waiting & HAMMER2_DIRTYCHAIN_WAITING) && (pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK) <= hammer2_limit_dirty_chains * 2 / 3 && pmp->sideq_count <= hammer2_limit_dirty_inodes * 2 / 3) { atomic_clear_int(&pmp->inmem_dirty_chains, HAMMER2_DIRTYCHAIN_WAITING); wakeup(&pmp->inmem_dirty_chains); } } } void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp) { if (pmp) { atomic_add_int(&pmp->inmem_dirty_chains, 1); } } /* * Volume header data locks */ void hammer2_voldata_lock(hammer2_dev_t *hmp) { lockmgr(&hmp->vollk, LK_EXCLUSIVE); } void hammer2_voldata_unlock(hammer2_dev_t *hmp) { lockmgr(&hmp->vollk, LK_RELEASE); } /* * Caller indicates that the volume header is being modified. Flag * the related chain and adjust its transaction id. * * The transaction id is set to voldata.mirror_tid + 1, similar to * what hammer2_chain_modify() does. Be very careful here, volume * data can be updated independently of the rest of the filesystem. */ void hammer2_voldata_modify(hammer2_dev_t *hmp) { if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) { atomic_add_long(&hammer2_count_modified_chains, 1); atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED); hammer2_pfs_memory_inc(hmp->vchain.pmp); hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid + 1; } } /* * Returns 0 if the filesystem has tons of free space * Returns 1 if the filesystem has less than 10% remaining * Returns 2 if the filesystem has less than 2%/5% (user/root) remaining. */ int hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred) { hammer2_pfs_t *pmp; hammer2_dev_t *hmp; hammer2_off_t free_reserved; hammer2_off_t free_nominal; int i; pmp = ip->pmp; if (pmp->free_ticks == 0 || pmp->free_ticks != ticks) { free_reserved = HAMMER2_SEGSIZE; free_nominal = 0x7FFFFFFFFFFFFFFFLLU; for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { hmp = pmp->pfs_hmps[i]; if (hmp == NULL) continue; if (pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER && pmp->pfs_types[i] != HAMMER2_PFSTYPE_SOFT_MASTER) continue; if (free_nominal > hmp->voldata.allocator_free) free_nominal = hmp->voldata.allocator_free; if (free_reserved < hmp->free_reserved) free_reserved = hmp->free_reserved; } /* * SMP races ok */ pmp->free_reserved = free_reserved; pmp->free_nominal = free_nominal; pmp->free_ticks = ticks; } else { free_reserved = pmp->free_reserved; free_nominal = pmp->free_nominal; } if (cred && cred->cr_uid != 0) { if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved) { return 2; } } else { if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved / 2) { return 2; } } if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved * 2) return 1; return 0; } |